1 //===--- ASTImporter.h - Importing ASTs from other Contexts -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the ASTImporter class which imports AST nodes from one
11 // context into another context.
13 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_AST_ASTIMPORTER_H
15 #define LLVM_CLANG_AST_ASTIMPORTER_H
17 #include "clang/AST/DeclarationName.h"
18 #include "clang/AST/Type.h"
19 #include "clang/Basic/SourceLocation.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/DenseSet.h"
22 #include "llvm/ADT/SmallVector.h"
26 class CXXCtorInitializer;
29 class DiagnosticsEngine;
33 class NestedNameSpecifier;
37 /// \brief Imports selected nodes from one AST context into another context,
38 /// merging AST nodes where appropriate.
41 typedef llvm::DenseSet<std::pair<Decl *, Decl *> > NonEquivalentDeclSet;
44 /// \brief The contexts we're importing to and from.
45 ASTContext &ToContext, &FromContext;
47 /// \brief The file managers we're importing to and from.
48 FileManager &ToFileManager, &FromFileManager;
50 /// \brief Whether to perform a minimal import.
53 /// \brief Whether the last diagnostic came from the "from" context.
54 bool LastDiagFromFrom;
56 /// \brief Mapping from the already-imported types in the "from" context
57 /// to the corresponding types in the "to" context.
58 llvm::DenseMap<const Type *, const Type *> ImportedTypes;
60 /// \brief Mapping from the already-imported declarations in the "from"
61 /// context to the corresponding declarations in the "to" context.
62 llvm::DenseMap<Decl *, Decl *> ImportedDecls;
64 /// \brief Mapping from the already-imported statements in the "from"
65 /// context to the corresponding statements in the "to" context.
66 llvm::DenseMap<Stmt *, Stmt *> ImportedStmts;
68 /// \brief Mapping from the already-imported FileIDs in the "from" source
69 /// manager to the corresponding FileIDs in the "to" source manager.
70 llvm::DenseMap<FileID, FileID> ImportedFileIDs;
72 /// \brief Imported, anonymous tag declarations that are missing their
73 /// corresponding typedefs.
74 SmallVector<TagDecl *, 4> AnonTagsWithPendingTypedefs;
76 /// \brief Declaration (from, to) pairs that are known not to be equivalent
77 /// (which we have already complained about).
78 NonEquivalentDeclSet NonEquivalentDecls;
81 /// \brief Create a new AST importer.
83 /// \param ToContext The context we'll be importing into.
85 /// \param ToFileManager The file manager we'll be importing into.
87 /// \param FromContext The context we'll be importing from.
89 /// \param FromFileManager The file manager we'll be importing into.
91 /// \param MinimalImport If true, the importer will attempt to import
92 /// as little as it can, e.g., by importing declarations as forward
93 /// declarations that can be completed at a later point.
94 ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
95 ASTContext &FromContext, FileManager &FromFileManager,
98 virtual ~ASTImporter();
100 /// \brief Whether the importer will perform a minimal import, creating
101 /// to-be-completed forward declarations when possible.
102 bool isMinimalImport() const { return Minimal; }
104 /// \brief Import the given type from the "from" context into the "to"
107 /// \returns the equivalent type in the "to" context, or a NULL type if
108 /// an error occurred.
109 QualType Import(QualType FromT);
111 /// \brief Import the given type source information from the
112 /// "from" context into the "to" context.
114 /// \returns the equivalent type source information in the "to"
115 /// context, or NULL if an error occurred.
116 TypeSourceInfo *Import(TypeSourceInfo *FromTSI);
118 /// \brief Import the given declaration from the "from" context into the
121 /// \returns the equivalent declaration in the "to" context, or a NULL type
122 /// if an error occurred.
123 Decl *Import(Decl *FromD);
125 /// \brief Return the copy of the given declaration in the "to" context if
126 /// it has already been imported from the "from" context. Otherwise return
128 Decl *GetAlreadyImportedOrNull(Decl *FromD);
130 /// \brief Import the given declaration context from the "from"
131 /// AST context into the "to" AST context.
133 /// \returns the equivalent declaration context in the "to"
134 /// context, or a NULL type if an error occurred.
135 DeclContext *ImportContext(DeclContext *FromDC);
137 /// \brief Import the given expression from the "from" context into the
140 /// \returns the equivalent expression in the "to" context, or NULL if
141 /// an error occurred.
142 Expr *Import(Expr *FromE);
144 /// \brief Import the given statement from the "from" context into the
147 /// \returns the equivalent statement in the "to" context, or NULL if
148 /// an error occurred.
149 Stmt *Import(Stmt *FromS);
151 /// \brief Import the given nested-name-specifier from the "from"
152 /// context into the "to" context.
154 /// \returns the equivalent nested-name-specifier in the "to"
155 /// context, or NULL if an error occurred.
156 NestedNameSpecifier *Import(NestedNameSpecifier *FromNNS);
158 /// \brief Import the given nested-name-specifier from the "from"
159 /// context into the "to" context.
161 /// \returns the equivalent nested-name-specifier in the "to"
163 NestedNameSpecifierLoc Import(NestedNameSpecifierLoc FromNNS);
165 /// \brief Import the goven template name from the "from" context into the
167 TemplateName Import(TemplateName From);
169 /// \brief Import the given source location from the "from" context into
170 /// the "to" context.
172 /// \returns the equivalent source location in the "to" context, or an
173 /// invalid source location if an error occurred.
174 SourceLocation Import(SourceLocation FromLoc);
176 /// \brief Import the given source range from the "from" context into
177 /// the "to" context.
179 /// \returns the equivalent source range in the "to" context, or an
180 /// invalid source location if an error occurred.
181 SourceRange Import(SourceRange FromRange);
183 /// \brief Import the given declaration name from the "from"
184 /// context into the "to" context.
186 /// \returns the equivalent declaration name in the "to" context,
187 /// or an empty declaration name if an error occurred.
188 DeclarationName Import(DeclarationName FromName);
190 /// \brief Import the given identifier from the "from" context
191 /// into the "to" context.
193 /// \returns the equivalent identifier in the "to" context.
194 IdentifierInfo *Import(const IdentifierInfo *FromId);
196 /// \brief Import the given Objective-C selector from the "from"
197 /// context into the "to" context.
199 /// \returns the equivalent selector in the "to" context.
200 Selector Import(Selector FromSel);
202 /// \brief Import the given file ID from the "from" context into the
205 /// \returns the equivalent file ID in the source manager of the "to"
207 FileID Import(FileID);
209 /// \brief Import the given C++ constructor initializer from the "from"
210 /// context into the "to" context.
212 /// \returns the equivalent initializer in the "to" context.
213 CXXCtorInitializer *Import(CXXCtorInitializer *FromInit);
217 /// \brief Import the definition of the given declaration, including all of
218 /// the declarations it contains.
220 /// This routine is intended to be used
221 void ImportDefinition(Decl *From);
223 /// \brief Cope with a name conflict when importing a declaration into the
226 /// This routine is invoked whenever there is a name conflict while
227 /// importing a declaration. The returned name will become the name of the
228 /// imported declaration. By default, the returned name is the same as the
229 /// original name, leaving the conflict unresolve such that name lookup
230 /// for this name is likely to find an ambiguity later.
232 /// Subclasses may override this routine to resolve the conflict, e.g., by
233 /// renaming the declaration being imported.
235 /// \param Name the name of the declaration being imported, which conflicts
236 /// with other declarations.
238 /// \param DC the declaration context (in the "to" AST context) in which
239 /// the name is being imported.
241 /// \param IDNS the identifier namespace in which the name will be found.
243 /// \param Decls the set of declarations with the same name as the
244 /// declaration being imported.
246 /// \param NumDecls the number of conflicting declarations in \p Decls.
248 /// \returns the name that the newly-imported declaration should have.
249 virtual DeclarationName HandleNameConflict(DeclarationName Name,
255 /// \brief Retrieve the context that AST nodes are being imported into.
256 ASTContext &getToContext() const { return ToContext; }
258 /// \brief Retrieve the context that AST nodes are being imported from.
259 ASTContext &getFromContext() const { return FromContext; }
261 /// \brief Retrieve the file manager that AST nodes are being imported into.
262 FileManager &getToFileManager() const { return ToFileManager; }
264 /// \brief Retrieve the file manager that AST nodes are being imported from.
265 FileManager &getFromFileManager() const { return FromFileManager; }
267 /// \brief Report a diagnostic in the "to" context.
268 DiagnosticBuilder ToDiag(SourceLocation Loc, unsigned DiagID);
270 /// \brief Report a diagnostic in the "from" context.
271 DiagnosticBuilder FromDiag(SourceLocation Loc, unsigned DiagID);
273 /// \brief Return the set of declarations that we know are not equivalent.
274 NonEquivalentDeclSet &getNonEquivalentDecls() { return NonEquivalentDecls; }
276 /// \brief Called for ObjCInterfaceDecl, ObjCProtocolDecl, and TagDecl.
277 /// Mark the Decl as complete, filling it in as much as possible.
279 /// \param D A declaration in the "to" context.
280 virtual void CompleteDecl(Decl* D);
282 /// \brief Note that we have imported the "from" declaration by mapping it
283 /// to the (potentially-newly-created) "to" declaration.
285 /// Subclasses can override this function to observe all of the \c From ->
286 /// \c To declaration mappings as they are imported.
287 virtual Decl *Imported(Decl *From, Decl *To);
289 /// \brief Called by StructuralEquivalenceContext. If a RecordDecl is
290 /// being compared to another RecordDecl as part of import, completing the
291 /// other RecordDecl may trigger importation of the first RecordDecl. This
292 /// happens especially for anonymous structs. If the original of the second
293 /// RecordDecl can be found, we can complete it without the need for
294 /// importation, eliminating this loop.
295 virtual Decl *GetOriginalDecl(Decl *To) { return nullptr; }
297 /// \brief Determine whether the given types are structurally
299 bool IsStructurallyEquivalent(QualType From, QualType To,
300 bool Complain = true);
304 #endif // LLVM_CLANG_AST_ASTIMPORTER_H