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;
27 class CXXBaseSpecifier;
30 class DiagnosticsEngine;
34 class NestedNameSpecifier;
38 /// \brief Imports selected nodes from one AST context into another context,
39 /// merging AST nodes where appropriate.
42 typedef llvm::DenseSet<std::pair<Decl *, Decl *> > NonEquivalentDeclSet;
43 typedef llvm::DenseMap<const CXXBaseSpecifier *, CXXBaseSpecifier *>
44 ImportedCXXBaseSpecifierMap;
47 /// \brief The contexts we're importing to and from.
48 ASTContext &ToContext, &FromContext;
50 /// \brief The file managers we're importing to and from.
51 FileManager &ToFileManager, &FromFileManager;
53 /// \brief Whether to perform a minimal import.
56 /// \brief Whether the last diagnostic came from the "from" context.
57 bool LastDiagFromFrom;
59 /// \brief Mapping from the already-imported types in the "from" context
60 /// to the corresponding types in the "to" context.
61 llvm::DenseMap<const Type *, const Type *> ImportedTypes;
63 /// \brief Mapping from the already-imported declarations in the "from"
64 /// context to the corresponding declarations in the "to" context.
65 llvm::DenseMap<Decl *, Decl *> ImportedDecls;
67 /// \brief Mapping from the already-imported statements in the "from"
68 /// context to the corresponding statements in the "to" context.
69 llvm::DenseMap<Stmt *, Stmt *> ImportedStmts;
71 /// \brief Mapping from the already-imported FileIDs in the "from" source
72 /// manager to the corresponding FileIDs in the "to" source manager.
73 llvm::DenseMap<FileID, FileID> ImportedFileIDs;
75 /// \brief Mapping from the already-imported CXXBasesSpecifier in
76 /// the "from" source manager to the corresponding CXXBasesSpecifier
77 /// in the "to" source manager.
78 ImportedCXXBaseSpecifierMap ImportedCXXBaseSpecifiers;
80 /// \brief Imported, anonymous tag declarations that are missing their
81 /// corresponding typedefs.
82 SmallVector<TagDecl *, 4> AnonTagsWithPendingTypedefs;
84 /// \brief Declaration (from, to) pairs that are known not to be equivalent
85 /// (which we have already complained about).
86 NonEquivalentDeclSet NonEquivalentDecls;
89 /// \brief Create a new AST importer.
91 /// \param ToContext The context we'll be importing into.
93 /// \param ToFileManager The file manager we'll be importing into.
95 /// \param FromContext The context we'll be importing from.
97 /// \param FromFileManager The file manager we'll be importing into.
99 /// \param MinimalImport If true, the importer will attempt to import
100 /// as little as it can, e.g., by importing declarations as forward
101 /// declarations that can be completed at a later point.
102 ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
103 ASTContext &FromContext, FileManager &FromFileManager,
106 virtual ~ASTImporter();
108 /// \brief Whether the importer will perform a minimal import, creating
109 /// to-be-completed forward declarations when possible.
110 bool isMinimalImport() const { return Minimal; }
112 /// \brief Import the given type from the "from" context into the "to"
115 /// \returns the equivalent type in the "to" context, or a NULL type if
116 /// an error occurred.
117 QualType Import(QualType FromT);
119 /// \brief Import the given type source information from the
120 /// "from" context into the "to" context.
122 /// \returns the equivalent type source information in the "to"
123 /// context, or NULL if an error occurred.
124 TypeSourceInfo *Import(TypeSourceInfo *FromTSI);
126 /// \brief Import the given declaration from the "from" context into the
129 /// \returns the equivalent declaration in the "to" context, or a NULL type
130 /// if an error occurred.
131 Decl *Import(Decl *FromD);
133 /// \brief Return the copy of the given declaration in the "to" context if
134 /// it has already been imported from the "from" context. Otherwise return
136 Decl *GetAlreadyImportedOrNull(Decl *FromD);
138 /// \brief Import the given declaration context from the "from"
139 /// AST context into the "to" AST context.
141 /// \returns the equivalent declaration context in the "to"
142 /// context, or a NULL type if an error occurred.
143 DeclContext *ImportContext(DeclContext *FromDC);
145 /// \brief Import the given expression from the "from" context into the
148 /// \returns the equivalent expression in the "to" context, or NULL if
149 /// an error occurred.
150 Expr *Import(Expr *FromE);
152 /// \brief Import the given statement from the "from" context into the
155 /// \returns the equivalent statement in the "to" context, or NULL if
156 /// an error occurred.
157 Stmt *Import(Stmt *FromS);
159 /// \brief Import the given nested-name-specifier from the "from"
160 /// context into the "to" context.
162 /// \returns the equivalent nested-name-specifier in the "to"
163 /// context, or NULL if an error occurred.
164 NestedNameSpecifier *Import(NestedNameSpecifier *FromNNS);
166 /// \brief Import the given nested-name-specifier from the "from"
167 /// context into the "to" context.
169 /// \returns the equivalent nested-name-specifier in the "to"
171 NestedNameSpecifierLoc Import(NestedNameSpecifierLoc FromNNS);
173 /// \brief Import the goven template name from the "from" context into the
175 TemplateName Import(TemplateName From);
177 /// \brief Import the given source location from the "from" context into
178 /// the "to" context.
180 /// \returns the equivalent source location in the "to" context, or an
181 /// invalid source location if an error occurred.
182 SourceLocation Import(SourceLocation FromLoc);
184 /// \brief Import the given source range from the "from" context into
185 /// the "to" context.
187 /// \returns the equivalent source range in the "to" context, or an
188 /// invalid source location if an error occurred.
189 SourceRange Import(SourceRange FromRange);
191 /// \brief Import the given declaration name from the "from"
192 /// context into the "to" context.
194 /// \returns the equivalent declaration name in the "to" context,
195 /// or an empty declaration name if an error occurred.
196 DeclarationName Import(DeclarationName FromName);
198 /// \brief Import the given identifier from the "from" context
199 /// into the "to" context.
201 /// \returns the equivalent identifier in the "to" context.
202 IdentifierInfo *Import(const IdentifierInfo *FromId);
204 /// \brief Import the given Objective-C selector from the "from"
205 /// context into the "to" context.
207 /// \returns the equivalent selector in the "to" context.
208 Selector Import(Selector FromSel);
210 /// \brief Import the given file ID from the "from" context into the
213 /// \returns the equivalent file ID in the source manager of the "to"
215 FileID Import(FileID);
217 /// \brief Import the given C++ constructor initializer from the "from"
218 /// context into the "to" context.
220 /// \returns the equivalent initializer in the "to" context.
221 CXXCtorInitializer *Import(CXXCtorInitializer *FromInit);
223 /// \brief Import the given CXXBaseSpecifier from the "from" context into
224 /// the "to" context.
226 /// \returns the equivalent CXXBaseSpecifier in the source manager of the
228 CXXBaseSpecifier *Import(const CXXBaseSpecifier *FromSpec);
230 /// \brief Import the definition of the given declaration, including all of
231 /// the declarations it contains.
233 /// This routine is intended to be used
234 void ImportDefinition(Decl *From);
236 /// \brief Cope with a name conflict when importing a declaration into the
239 /// This routine is invoked whenever there is a name conflict while
240 /// importing a declaration. The returned name will become the name of the
241 /// imported declaration. By default, the returned name is the same as the
242 /// original name, leaving the conflict unresolve such that name lookup
243 /// for this name is likely to find an ambiguity later.
245 /// Subclasses may override this routine to resolve the conflict, e.g., by
246 /// renaming the declaration being imported.
248 /// \param Name the name of the declaration being imported, which conflicts
249 /// with other declarations.
251 /// \param DC the declaration context (in the "to" AST context) in which
252 /// the name is being imported.
254 /// \param IDNS the identifier namespace in which the name will be found.
256 /// \param Decls the set of declarations with the same name as the
257 /// declaration being imported.
259 /// \param NumDecls the number of conflicting declarations in \p Decls.
261 /// \returns the name that the newly-imported declaration should have.
262 virtual DeclarationName HandleNameConflict(DeclarationName Name,
268 /// \brief Retrieve the context that AST nodes are being imported into.
269 ASTContext &getToContext() const { return ToContext; }
271 /// \brief Retrieve the context that AST nodes are being imported from.
272 ASTContext &getFromContext() const { return FromContext; }
274 /// \brief Retrieve the file manager that AST nodes are being imported into.
275 FileManager &getToFileManager() const { return ToFileManager; }
277 /// \brief Retrieve the file manager that AST nodes are being imported from.
278 FileManager &getFromFileManager() const { return FromFileManager; }
280 /// \brief Report a diagnostic in the "to" context.
281 DiagnosticBuilder ToDiag(SourceLocation Loc, unsigned DiagID);
283 /// \brief Report a diagnostic in the "from" context.
284 DiagnosticBuilder FromDiag(SourceLocation Loc, unsigned DiagID);
286 /// \brief Return the set of declarations that we know are not equivalent.
287 NonEquivalentDeclSet &getNonEquivalentDecls() { return NonEquivalentDecls; }
289 /// \brief Called for ObjCInterfaceDecl, ObjCProtocolDecl, and TagDecl.
290 /// Mark the Decl as complete, filling it in as much as possible.
292 /// \param D A declaration in the "to" context.
293 virtual void CompleteDecl(Decl* D);
295 /// \brief Note that we have imported the "from" declaration by mapping it
296 /// to the (potentially-newly-created) "to" declaration.
298 /// Subclasses can override this function to observe all of the \c From ->
299 /// \c To declaration mappings as they are imported.
300 virtual Decl *Imported(Decl *From, Decl *To);
302 /// \brief Called by StructuralEquivalenceContext. If a RecordDecl is
303 /// being compared to another RecordDecl as part of import, completing the
304 /// other RecordDecl may trigger importation of the first RecordDecl. This
305 /// happens especially for anonymous structs. If the original of the second
306 /// RecordDecl can be found, we can complete it without the need for
307 /// importation, eliminating this loop.
308 virtual Decl *GetOriginalDecl(Decl *To) { return nullptr; }
310 /// \brief Determine whether the given types are structurally
312 bool IsStructurallyEquivalent(QualType From, QualType To,
313 bool Complain = true);
317 #endif // LLVM_CLANG_AST_ASTIMPORTER_H