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 Whether the last diagnostic came from the "from" context.
53 bool LastDiagFromFrom;
55 /// \brief Mapping from the already-imported types in the "from" context
56 /// to the corresponding types in the "to" context.
57 llvm::DenseMap<const Type *, const Type *> ImportedTypes;
59 /// \brief Mapping from the already-imported declarations in the "from"
60 /// context to the corresponding declarations in the "to" context.
61 llvm::DenseMap<Decl *, Decl *> ImportedDecls;
63 /// \brief Mapping from the already-imported statements in the "from"
64 /// context to the corresponding statements in the "to" context.
65 llvm::DenseMap<Stmt *, Stmt *> ImportedStmts;
67 /// \brief Mapping from the already-imported FileIDs in the "from" source
68 /// manager to the corresponding FileIDs in the "to" source manager.
69 llvm::DenseMap<FileID, FileID> ImportedFileIDs;
71 /// \brief Imported, anonymous tag declarations that are missing their
72 /// corresponding typedefs.
73 SmallVector<TagDecl *, 4> AnonTagsWithPendingTypedefs;
75 /// \brief Declaration (from, to) pairs that are known not to be equivalent
76 /// (which we have already complained about).
77 NonEquivalentDeclSet NonEquivalentDecls;
80 /// \brief Create a new AST importer.
82 /// \param ToContext The context we'll be importing into.
84 /// \param ToFileManager The file manager we'll be importing into.
86 /// \param FromContext The context we'll be importing from.
88 /// \param FromFileManager The file manager we'll be importing into.
90 /// \param MinimalImport If true, the importer will attempt to import
91 /// as little as it can, e.g., by importing declarations as forward
92 /// declarations that can be completed at a later point.
93 ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
94 ASTContext &FromContext, FileManager &FromFileManager,
97 virtual ~ASTImporter();
99 /// \brief Whether the importer will perform a minimal import, creating
100 /// to-be-completed forward declarations when possible.
101 bool isMinimalImport() const { return Minimal; }
103 /// \brief Import the given type from the "from" context into the "to"
106 /// \returns the equivalent type in the "to" context, or a NULL type if
107 /// an error occurred.
108 QualType Import(QualType FromT);
110 /// \brief Import the given type source information from the
111 /// "from" context into the "to" context.
113 /// \returns the equivalent type source information in the "to"
114 /// context, or NULL if an error occurred.
115 TypeSourceInfo *Import(TypeSourceInfo *FromTSI);
117 /// \brief Import the given declaration from the "from" context into the
120 /// \returns the equivalent declaration in the "to" context, or a NULL type
121 /// if an error occurred.
122 Decl *Import(Decl *FromD);
124 /// \brief Import the given declaration context from the "from"
125 /// AST context into the "to" AST context.
127 /// \returns the equivalent declaration context in the "to"
128 /// context, or a NULL type if an error occurred.
129 DeclContext *ImportContext(DeclContext *FromDC);
131 /// \brief Import the given expression from the "from" context into the
134 /// \returns the equivalent expression in the "to" context, or NULL if
135 /// an error occurred.
136 Expr *Import(Expr *FromE);
138 /// \brief Import the given statement from the "from" context into the
141 /// \returns the equivalent statement in the "to" context, or NULL if
142 /// an error occurred.
143 Stmt *Import(Stmt *FromS);
145 /// \brief Import the given nested-name-specifier from the "from"
146 /// context into the "to" context.
148 /// \returns the equivalent nested-name-specifier in the "to"
149 /// context, or NULL if an error occurred.
150 NestedNameSpecifier *Import(NestedNameSpecifier *FromNNS);
152 /// \brief Import the given nested-name-specifier from the "from"
153 /// context into the "to" context.
155 /// \returns the equivalent nested-name-specifier in the "to"
157 NestedNameSpecifierLoc Import(NestedNameSpecifierLoc FromNNS);
159 /// \brief Import the goven template name from the "from" context into the
161 TemplateName Import(TemplateName From);
163 /// \brief Import the given source location from the "from" context into
164 /// the "to" context.
166 /// \returns the equivalent source location in the "to" context, or an
167 /// invalid source location if an error occurred.
168 SourceLocation Import(SourceLocation FromLoc);
170 /// \brief Import the given source range from the "from" context into
171 /// the "to" context.
173 /// \returns the equivalent source range in the "to" context, or an
174 /// invalid source location if an error occurred.
175 SourceRange Import(SourceRange FromRange);
177 /// \brief Import the given declaration name from the "from"
178 /// context into the "to" context.
180 /// \returns the equivalent declaration name in the "to" context,
181 /// or an empty declaration name if an error occurred.
182 DeclarationName Import(DeclarationName FromName);
184 /// \brief Import the given identifier from the "from" context
185 /// into the "to" context.
187 /// \returns the equivalent identifier in the "to" context.
188 IdentifierInfo *Import(const IdentifierInfo *FromId);
190 /// \brief Import the given Objective-C selector from the "from"
191 /// context into the "to" context.
193 /// \returns the equivalent selector in the "to" context.
194 Selector Import(Selector FromSel);
196 /// \brief Import the given file ID from the "from" context into the
199 /// \returns the equivalent file ID in the source manager of the "to"
201 FileID Import(FileID);
203 /// \brief Import the definition of the given declaration, including all of
204 /// the declarations it contains.
206 /// This routine is intended to be used
207 void ImportDefinition(Decl *From);
209 /// \brief Cope with a name conflict when importing a declaration into the
212 /// This routine is invoked whenever there is a name conflict while
213 /// importing a declaration. The returned name will become the name of the
214 /// imported declaration. By default, the returned name is the same as the
215 /// original name, leaving the conflict unresolve such that name lookup
216 /// for this name is likely to find an ambiguity later.
218 /// Subclasses may override this routine to resolve the conflict, e.g., by
219 /// renaming the declaration being imported.
221 /// \param Name the name of the declaration being imported, which conflicts
222 /// with other declarations.
224 /// \param DC the declaration context (in the "to" AST context) in which
225 /// the name is being imported.
227 /// \param IDNS the identifier namespace in which the name will be found.
229 /// \param Decls the set of declarations with the same name as the
230 /// declaration being imported.
232 /// \param NumDecls the number of conflicting declarations in \p Decls.
234 /// \returns the name that the newly-imported declaration should have.
235 virtual DeclarationName HandleNameConflict(DeclarationName Name,
241 /// \brief Retrieve the context that AST nodes are being imported into.
242 ASTContext &getToContext() const { return ToContext; }
244 /// \brief Retrieve the context that AST nodes are being imported from.
245 ASTContext &getFromContext() const { return FromContext; }
247 /// \brief Retrieve the file manager that AST nodes are being imported into.
248 FileManager &getToFileManager() const { return ToFileManager; }
250 /// \brief Retrieve the file manager that AST nodes are being imported from.
251 FileManager &getFromFileManager() const { return FromFileManager; }
253 /// \brief Report a diagnostic in the "to" context.
254 DiagnosticBuilder ToDiag(SourceLocation Loc, unsigned DiagID);
256 /// \brief Report a diagnostic in the "from" context.
257 DiagnosticBuilder FromDiag(SourceLocation Loc, unsigned DiagID);
259 /// \brief Return the set of declarations that we know are not equivalent.
260 NonEquivalentDeclSet &getNonEquivalentDecls() { return NonEquivalentDecls; }
262 /// \brief Called for ObjCInterfaceDecl, ObjCProtocolDecl, and TagDecl.
263 /// Mark the Decl as complete, filling it in as much as possible.
265 /// \param D A declaration in the "to" context.
266 virtual void CompleteDecl(Decl* D);
268 /// \brief Note that we have imported the "from" declaration by mapping it
269 /// to the (potentially-newly-created) "to" declaration.
271 /// Subclasses can override this function to observe all of the \c From ->
272 /// \c To declaration mappings as they are imported.
273 virtual Decl *Imported(Decl *From, Decl *To);
275 /// \brief Called by StructuralEquivalenceContext. If a RecordDecl is
276 /// being compared to another RecordDecl as part of import, completing the
277 /// other RecordDecl may trigger importation of the first RecordDecl. This
278 /// happens especially for anonymous structs. If the original of the second
279 /// RecordDecl can be found, we can complete it without the need for
280 /// importation, eliminating this loop.
281 virtual Decl *GetOriginalDecl(Decl *To) { return NULL; }
283 /// \brief Determine whether the given types are structurally
285 bool IsStructurallyEquivalent(QualType From, QualType To,
286 bool Complain = true);
290 #endif // LLVM_CLANG_AST_ASTIMPORTER_H