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 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_AST_ASTIMPORTER_H
16 #define LLVM_CLANG_AST_ASTIMPORTER_H
18 #include "clang/AST/DeclarationName.h"
19 #include "clang/AST/NestedNameSpecifier.h"
20 #include "clang/AST/TemplateName.h"
21 #include "clang/AST/Type.h"
22 #include "clang/Basic/Diagnostic.h"
23 #include "clang/Basic/IdentifierTable.h"
24 #include "clang/Basic/LLVM.h"
25 #include "clang/Basic/SourceLocation.h"
26 #include "llvm/ADT/DenseMap.h"
27 #include "llvm/ADT/DenseSet.h"
28 #include "llvm/ADT/SmallVector.h"
34 class CXXBaseSpecifier;
35 class CXXCtorInitializer;
46 // \brief Returns with a list of declarations started from the canonical decl
47 // then followed by subsequent decls in the translation unit.
48 // This gives a canonical list for each entry in the redecl chain.
49 // `Decl::redecls()` gives a list of decls which always start from the
50 // previous decl and the next item is actually the previous item in the order
51 // of source locations. Thus, `Decl::redecls()` gives different lists for
52 // the different entries in a given redecl chain.
53 llvm::SmallVector<Decl*, 2> getCanonicalForwardRedeclChain(Decl* D);
55 /// Imports selected nodes from one AST context into another context,
56 /// merging AST nodes where appropriate.
59 using NonEquivalentDeclSet = llvm::DenseSet<std::pair<Decl *, Decl *>>;
60 using ImportedCXXBaseSpecifierMap =
61 llvm::DenseMap<const CXXBaseSpecifier *, CXXBaseSpecifier *>;
64 /// The contexts we're importing to and from.
65 ASTContext &ToContext, &FromContext;
67 /// The file managers we're importing to and from.
68 FileManager &ToFileManager, &FromFileManager;
70 /// Whether to perform a minimal import.
73 /// Whether the last diagnostic came from the "from" context.
74 bool LastDiagFromFrom = false;
76 /// Mapping from the already-imported types in the "from" context
77 /// to the corresponding types in the "to" context.
78 llvm::DenseMap<const Type *, const Type *> ImportedTypes;
80 /// Mapping from the already-imported declarations in the "from"
81 /// context to the corresponding declarations in the "to" context.
82 llvm::DenseMap<Decl *, Decl *> ImportedDecls;
84 /// Mapping from the already-imported statements in the "from"
85 /// context to the corresponding statements in the "to" context.
86 llvm::DenseMap<Stmt *, Stmt *> ImportedStmts;
88 /// Mapping from the already-imported FileIDs in the "from" source
89 /// manager to the corresponding FileIDs in the "to" source manager.
90 llvm::DenseMap<FileID, FileID> ImportedFileIDs;
92 /// Mapping from the already-imported CXXBasesSpecifier in
93 /// the "from" source manager to the corresponding CXXBasesSpecifier
94 /// in the "to" source manager.
95 ImportedCXXBaseSpecifierMap ImportedCXXBaseSpecifiers;
97 /// Declaration (from, to) pairs that are known not to be equivalent
98 /// (which we have already complained about).
99 NonEquivalentDeclSet NonEquivalentDecls;
102 /// Create a new AST importer.
104 /// \param ToContext The context we'll be importing into.
106 /// \param ToFileManager The file manager we'll be importing into.
108 /// \param FromContext The context we'll be importing from.
110 /// \param FromFileManager The file manager we'll be importing into.
112 /// \param MinimalImport If true, the importer will attempt to import
113 /// as little as it can, e.g., by importing declarations as forward
114 /// declarations that can be completed at a later point.
115 ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
116 ASTContext &FromContext, FileManager &FromFileManager,
119 virtual ~ASTImporter();
121 /// Whether the importer will perform a minimal import, creating
122 /// to-be-completed forward declarations when possible.
123 bool isMinimalImport() const { return Minimal; }
125 /// Import the given type from the "from" context into the "to"
128 /// \returns the equivalent type in the "to" context, or a NULL type if
129 /// an error occurred.
130 QualType Import(QualType FromT);
132 /// Import the given type source information from the
133 /// "from" context into the "to" context.
135 /// \returns the equivalent type source information in the "to"
136 /// context, or NULL if an error occurred.
137 TypeSourceInfo *Import(TypeSourceInfo *FromTSI);
139 /// Import the given attribute from the "from" context into the
142 /// \returns the equivalent attribute in the "to" context.
143 Attr *Import(const Attr *FromAttr);
145 /// Import the given declaration from the "from" context into the
148 /// \returns the equivalent declaration in the "to" context, or a NULL type
149 /// if an error occurred.
150 Decl *Import(Decl *FromD);
151 Decl *Import(const Decl *FromD) {
152 return Import(const_cast<Decl *>(FromD));
155 /// Return the copy of the given declaration in the "to" context if
156 /// it has already been imported from the "from" context. Otherwise return
158 Decl *GetAlreadyImportedOrNull(Decl *FromD);
160 /// Import the given declaration context from the "from"
161 /// AST context into the "to" AST context.
163 /// \returns the equivalent declaration context in the "to"
164 /// context, or a NULL type if an error occurred.
165 DeclContext *ImportContext(DeclContext *FromDC);
167 /// Import the given expression from the "from" context into the
170 /// \returns the equivalent expression in the "to" context, or NULL if
171 /// an error occurred.
172 Expr *Import(Expr *FromE);
174 /// Import the given statement from the "from" context into the
177 /// \returns the equivalent statement in the "to" context, or NULL if
178 /// an error occurred.
179 Stmt *Import(Stmt *FromS);
181 /// Import the given nested-name-specifier from the "from"
182 /// context into the "to" context.
184 /// \returns the equivalent nested-name-specifier in the "to"
185 /// context, or NULL if an error occurred.
186 NestedNameSpecifier *Import(NestedNameSpecifier *FromNNS);
188 /// Import the given nested-name-specifier from the "from"
189 /// context into the "to" context.
191 /// \returns the equivalent nested-name-specifier in the "to"
193 NestedNameSpecifierLoc Import(NestedNameSpecifierLoc FromNNS);
195 /// Import the goven template name from the "from" context into the
197 TemplateName Import(TemplateName From);
199 /// Import the given source location from the "from" context into
200 /// the "to" context.
202 /// \returns the equivalent source location in the "to" context, or an
203 /// invalid source location if an error occurred.
204 SourceLocation Import(SourceLocation FromLoc);
206 /// Import the given source range from the "from" context into
207 /// the "to" context.
209 /// \returns the equivalent source range in the "to" context, or an
210 /// invalid source location if an error occurred.
211 SourceRange Import(SourceRange FromRange);
213 /// Import the given declaration name from the "from"
214 /// context into the "to" context.
216 /// \returns the equivalent declaration name in the "to" context,
217 /// or an empty declaration name if an error occurred.
218 DeclarationName Import(DeclarationName FromName);
220 /// Import the given identifier from the "from" context
221 /// into the "to" context.
223 /// \returns the equivalent identifier in the "to" context.
224 IdentifierInfo *Import(const IdentifierInfo *FromId);
226 /// Import the given Objective-C selector from the "from"
227 /// context into the "to" context.
229 /// \returns the equivalent selector in the "to" context.
230 Selector Import(Selector FromSel);
232 /// Import the given file ID from the "from" context into the
235 /// \returns the equivalent file ID in the source manager of the "to"
237 FileID Import(FileID);
239 /// Import the given C++ constructor initializer from the "from"
240 /// context into the "to" context.
242 /// \returns the equivalent initializer in the "to" context.
243 CXXCtorInitializer *Import(CXXCtorInitializer *FromInit);
245 /// Import the given CXXBaseSpecifier from the "from" context into
246 /// the "to" context.
248 /// \returns the equivalent CXXBaseSpecifier in the source manager of the
250 CXXBaseSpecifier *Import(const CXXBaseSpecifier *FromSpec);
252 /// Import the definition of the given declaration, including all of
253 /// the declarations it contains.
255 /// This routine is intended to be used
256 void ImportDefinition(Decl *From);
258 /// Cope with a name conflict when importing a declaration into the
261 /// This routine is invoked whenever there is a name conflict while
262 /// importing a declaration. The returned name will become the name of the
263 /// imported declaration. By default, the returned name is the same as the
264 /// original name, leaving the conflict unresolve such that name lookup
265 /// for this name is likely to find an ambiguity later.
267 /// Subclasses may override this routine to resolve the conflict, e.g., by
268 /// renaming the declaration being imported.
270 /// \param Name the name of the declaration being imported, which conflicts
271 /// with other declarations.
273 /// \param DC the declaration context (in the "to" AST context) in which
274 /// the name is being imported.
276 /// \param IDNS the identifier namespace in which the name will be found.
278 /// \param Decls the set of declarations with the same name as the
279 /// declaration being imported.
281 /// \param NumDecls the number of conflicting declarations in \p Decls.
283 /// \returns the name that the newly-imported declaration should have.
284 virtual DeclarationName HandleNameConflict(DeclarationName Name,
290 /// Retrieve the context that AST nodes are being imported into.
291 ASTContext &getToContext() const { return ToContext; }
293 /// Retrieve the context that AST nodes are being imported from.
294 ASTContext &getFromContext() const { return FromContext; }
296 /// Retrieve the file manager that AST nodes are being imported into.
297 FileManager &getToFileManager() const { return ToFileManager; }
299 /// Retrieve the file manager that AST nodes are being imported from.
300 FileManager &getFromFileManager() const { return FromFileManager; }
302 /// Report a diagnostic in the "to" context.
303 DiagnosticBuilder ToDiag(SourceLocation Loc, unsigned DiagID);
305 /// Report a diagnostic in the "from" context.
306 DiagnosticBuilder FromDiag(SourceLocation Loc, unsigned DiagID);
308 /// Return the set of declarations that we know are not equivalent.
309 NonEquivalentDeclSet &getNonEquivalentDecls() { return NonEquivalentDecls; }
311 /// Called for ObjCInterfaceDecl, ObjCProtocolDecl, and TagDecl.
312 /// Mark the Decl as complete, filling it in as much as possible.
314 /// \param D A declaration in the "to" context.
315 virtual void CompleteDecl(Decl* D);
317 /// Subclasses can override this function to observe all of the \c From ->
318 /// \c To declaration mappings as they are imported.
319 virtual Decl *Imported(Decl *From, Decl *To) { return To; }
321 /// Store and assign the imported declaration to its counterpart.
322 Decl *MapImported(Decl *From, Decl *To);
324 /// Called by StructuralEquivalenceContext. If a RecordDecl is
325 /// being compared to another RecordDecl as part of import, completing the
326 /// other RecordDecl may trigger importation of the first RecordDecl. This
327 /// happens especially for anonymous structs. If the original of the second
328 /// RecordDecl can be found, we can complete it without the need for
329 /// importation, eliminating this loop.
330 virtual Decl *GetOriginalDecl(Decl *To) { return nullptr; }
332 /// Determine whether the given types are structurally
334 bool IsStructurallyEquivalent(QualType From, QualType To,
335 bool Complain = true);
340 #endif // LLVM_CLANG_AST_ASTIMPORTER_H