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"
28 class DiagnosticsEngine;
32 class NestedNameSpecifier;
36 /// \brief Imports selected nodes from one AST context into another context,
37 /// merging AST nodes where appropriate.
40 typedef llvm::DenseSet<std::pair<Decl *, Decl *> > NonEquivalentDeclSet;
43 /// \brief The contexts we're importing to and from.
44 ASTContext &ToContext, &FromContext;
46 /// \brief The file managers we're importing to and from.
47 FileManager &ToFileManager, &FromFileManager;
49 /// \brief Whether to perform a minimal import.
52 /// \brief Mapping from the already-imported types in the "from" context
53 /// to the corresponding types in the "to" context.
54 llvm::DenseMap<const Type *, const Type *> ImportedTypes;
56 /// \brief Mapping from the already-imported declarations in the "from"
57 /// context to the corresponding declarations in the "to" context.
58 llvm::DenseMap<Decl *, Decl *> ImportedDecls;
60 /// \brief Mapping from the already-imported statements in the "from"
61 /// context to the corresponding statements in the "to" context.
62 llvm::DenseMap<Stmt *, Stmt *> ImportedStmts;
64 /// \brief Mapping from the already-imported FileIDs in the "from" source
65 /// manager to the corresponding FileIDs in the "to" source manager.
66 llvm::DenseMap<FileID, FileID> ImportedFileIDs;
68 /// \brief Imported, anonymous tag declarations that are missing their
69 /// corresponding typedefs.
70 SmallVector<TagDecl *, 4> AnonTagsWithPendingTypedefs;
72 /// \brief Declaration (from, to) pairs that are known not to be equivalent
73 /// (which we have already complained about).
74 NonEquivalentDeclSet NonEquivalentDecls;
77 /// \brief Create a new AST importer.
79 /// \param ToContext The context we'll be importing into.
81 /// \param ToFileManager The file manager we'll be importing into.
83 /// \param FromContext The context we'll be importing from.
85 /// \param FromFileManager The file manager we'll be importing into.
87 /// \param MinimalImport If true, the importer will attempt to import
88 /// as little as it can, e.g., by importing declarations as forward
89 /// declarations that can be completed at a later point.
90 ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
91 ASTContext &FromContext, FileManager &FromFileManager,
94 virtual ~ASTImporter();
96 /// \brief Whether the importer will perform a minimal import, creating
97 /// to-be-completed forward declarations when possible.
98 bool isMinimalImport() const { return Minimal; }
100 /// \brief Import the given type from the "from" context into the "to"
103 /// \returns the equivalent type in the "to" context, or a NULL type if
104 /// an error occurred.
105 QualType Import(QualType FromT);
107 /// \brief Import the given type source information from the
108 /// "from" context into the "to" context.
110 /// \returns the equivalent type source information in the "to"
111 /// context, or NULL if an error occurred.
112 TypeSourceInfo *Import(TypeSourceInfo *FromTSI);
114 /// \brief Import the given declaration from the "from" context into the
117 /// \returns the equivalent declaration in the "to" context, or a NULL type
118 /// if an error occurred.
119 Decl *Import(Decl *FromD);
121 /// \brief Import the given declaration context from the "from"
122 /// AST context into the "to" AST context.
124 /// \returns the equivalent declaration context in the "to"
125 /// context, or a NULL type if an error occurred.
126 DeclContext *ImportContext(DeclContext *FromDC);
128 /// \brief Import the given expression from the "from" context into the
131 /// \returns the equivalent expression in the "to" context, or NULL if
132 /// an error occurred.
133 Expr *Import(Expr *FromE);
135 /// \brief Import the given statement from the "from" context into the
138 /// \returns the equivalent statement in the "to" context, or NULL if
139 /// an error occurred.
140 Stmt *Import(Stmt *FromS);
142 /// \brief Import the given nested-name-specifier from the "from"
143 /// context into the "to" context.
145 /// \returns the equivalent nested-name-specifier in the "to"
146 /// context, or NULL if an error occurred.
147 NestedNameSpecifier *Import(NestedNameSpecifier *FromNNS);
149 /// \brief Import the given nested-name-specifier from the "from"
150 /// context into the "to" context.
152 /// \returns the equivalent nested-name-specifier in the "to"
154 NestedNameSpecifierLoc Import(NestedNameSpecifierLoc FromNNS);
156 /// \brief Import the goven template name from the "from" context into the
158 TemplateName Import(TemplateName From);
160 /// \brief Import the given source location from the "from" context into
161 /// the "to" context.
163 /// \returns the equivalent source location in the "to" context, or an
164 /// invalid source location if an error occurred.
165 SourceLocation Import(SourceLocation FromLoc);
167 /// \brief Import the given source range from the "from" context into
168 /// the "to" context.
170 /// \returns the equivalent source range in the "to" context, or an
171 /// invalid source location if an error occurred.
172 SourceRange Import(SourceRange FromRange);
174 /// \brief Import the given declaration name from the "from"
175 /// context into the "to" context.
177 /// \returns the equivalent declaration name in the "to" context,
178 /// or an empty declaration name if an error occurred.
179 DeclarationName Import(DeclarationName FromName);
181 /// \brief Import the given identifier from the "from" context
182 /// into the "to" context.
184 /// \returns the equivalent identifier in the "to" context.
185 IdentifierInfo *Import(const IdentifierInfo *FromId);
187 /// \brief Import the given Objective-C selector from the "from"
188 /// context into the "to" context.
190 /// \returns the equivalent selector in the "to" context.
191 Selector Import(Selector FromSel);
193 /// \brief Import the given file ID from the "from" context into the
196 /// \returns the equivalent file ID in the source manager of the "to"
198 FileID Import(FileID);
200 /// \brief Import the definition of the given declaration, including all of
201 /// the declarations it contains.
203 /// This routine is intended to be used
204 void ImportDefinition(Decl *From);
206 /// \brief Cope with a name conflict when importing a declaration into the
209 /// This routine is invoked whenever there is a name conflict while
210 /// importing a declaration. The returned name will become the name of the
211 /// imported declaration. By default, the returned name is the same as the
212 /// original name, leaving the conflict unresolve such that name lookup
213 /// for this name is likely to find an ambiguity later.
215 /// Subclasses may override this routine to resolve the conflict, e.g., by
216 /// renaming the declaration being imported.
218 /// \param Name the name of the declaration being imported, which conflicts
219 /// with other declarations.
221 /// \param DC the declaration context (in the "to" AST context) in which
222 /// the name is being imported.
224 /// \param IDNS the identifier namespace in which the name will be found.
226 /// \param Decls the set of declarations with the same name as the
227 /// declaration being imported.
229 /// \param NumDecls the number of conflicting declarations in \p Decls.
231 /// \returns the name that the newly-imported declaration should have.
232 virtual DeclarationName HandleNameConflict(DeclarationName Name,
238 /// \brief Retrieve the context that AST nodes are being imported into.
239 ASTContext &getToContext() const { return ToContext; }
241 /// \brief Retrieve the context that AST nodes are being imported from.
242 ASTContext &getFromContext() const { return FromContext; }
244 /// \brief Retrieve the file manager that AST nodes are being imported into.
245 FileManager &getToFileManager() const { return ToFileManager; }
247 /// \brief Retrieve the file manager that AST nodes are being imported from.
248 FileManager &getFromFileManager() const { return FromFileManager; }
250 /// \brief Report a diagnostic in the "to" context.
251 DiagnosticBuilder ToDiag(SourceLocation Loc, unsigned DiagID);
253 /// \brief Report a diagnostic in the "from" context.
254 DiagnosticBuilder FromDiag(SourceLocation Loc, unsigned DiagID);
256 /// \brief Return the set of declarations that we know are not equivalent.
257 NonEquivalentDeclSet &getNonEquivalentDecls() { return NonEquivalentDecls; }
259 /// \brief Note that we have imported the "from" declaration by mapping it
260 /// to the (potentially-newly-created) "to" declaration.
262 /// Subclasses can override this function to observe all of the \c From ->
263 /// \c To declaration mappings as they are imported.
264 virtual Decl *Imported(Decl *From, Decl *To);
266 /// \brief Determine whether the given types are structurally
268 bool IsStructurallyEquivalent(QualType From, QualType To);
272 #endif // LLVM_CLANG_AST_ASTIMPORTER_H