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 Return the copy of the given declaration in the "to" context if
125 /// it has already been imported from the "from" context. Otherwise return
127 Decl *GetAlreadyImportedOrNull(Decl *FromD);
129 /// \brief Import the given declaration context from the "from"
130 /// AST context into the "to" AST context.
132 /// \returns the equivalent declaration context in the "to"
133 /// context, or a NULL type if an error occurred.
134 DeclContext *ImportContext(DeclContext *FromDC);
136 /// \brief Import the given expression from the "from" context into the
139 /// \returns the equivalent expression in the "to" context, or NULL if
140 /// an error occurred.
141 Expr *Import(Expr *FromE);
143 /// \brief Import the given statement from the "from" context into the
146 /// \returns the equivalent statement in the "to" context, or NULL if
147 /// an error occurred.
148 Stmt *Import(Stmt *FromS);
150 /// \brief Import the given nested-name-specifier from the "from"
151 /// context into the "to" context.
153 /// \returns the equivalent nested-name-specifier in the "to"
154 /// context, or NULL if an error occurred.
155 NestedNameSpecifier *Import(NestedNameSpecifier *FromNNS);
157 /// \brief Import the given nested-name-specifier from the "from"
158 /// context into the "to" context.
160 /// \returns the equivalent nested-name-specifier in the "to"
162 NestedNameSpecifierLoc Import(NestedNameSpecifierLoc FromNNS);
164 /// \brief Import the goven template name from the "from" context into the
166 TemplateName Import(TemplateName From);
168 /// \brief Import the given source location from the "from" context into
169 /// the "to" context.
171 /// \returns the equivalent source location in the "to" context, or an
172 /// invalid source location if an error occurred.
173 SourceLocation Import(SourceLocation FromLoc);
175 /// \brief Import the given source range from the "from" context into
176 /// the "to" context.
178 /// \returns the equivalent source range in the "to" context, or an
179 /// invalid source location if an error occurred.
180 SourceRange Import(SourceRange FromRange);
182 /// \brief Import the given declaration name from the "from"
183 /// context into the "to" context.
185 /// \returns the equivalent declaration name in the "to" context,
186 /// or an empty declaration name if an error occurred.
187 DeclarationName Import(DeclarationName FromName);
189 /// \brief Import the given identifier from the "from" context
190 /// into the "to" context.
192 /// \returns the equivalent identifier in the "to" context.
193 IdentifierInfo *Import(const IdentifierInfo *FromId);
195 /// \brief Import the given Objective-C selector from the "from"
196 /// context into the "to" context.
198 /// \returns the equivalent selector in the "to" context.
199 Selector Import(Selector FromSel);
201 /// \brief Import the given file ID from the "from" context into the
204 /// \returns the equivalent file ID in the source manager of the "to"
206 FileID Import(FileID);
208 /// \brief Import the definition of the given declaration, including all of
209 /// the declarations it contains.
211 /// This routine is intended to be used
212 void ImportDefinition(Decl *From);
214 /// \brief Cope with a name conflict when importing a declaration into the
217 /// This routine is invoked whenever there is a name conflict while
218 /// importing a declaration. The returned name will become the name of the
219 /// imported declaration. By default, the returned name is the same as the
220 /// original name, leaving the conflict unresolve such that name lookup
221 /// for this name is likely to find an ambiguity later.
223 /// Subclasses may override this routine to resolve the conflict, e.g., by
224 /// renaming the declaration being imported.
226 /// \param Name the name of the declaration being imported, which conflicts
227 /// with other declarations.
229 /// \param DC the declaration context (in the "to" AST context) in which
230 /// the name is being imported.
232 /// \param IDNS the identifier namespace in which the name will be found.
234 /// \param Decls the set of declarations with the same name as the
235 /// declaration being imported.
237 /// \param NumDecls the number of conflicting declarations in \p Decls.
239 /// \returns the name that the newly-imported declaration should have.
240 virtual DeclarationName HandleNameConflict(DeclarationName Name,
246 /// \brief Retrieve the context that AST nodes are being imported into.
247 ASTContext &getToContext() const { return ToContext; }
249 /// \brief Retrieve the context that AST nodes are being imported from.
250 ASTContext &getFromContext() const { return FromContext; }
252 /// \brief Retrieve the file manager that AST nodes are being imported into.
253 FileManager &getToFileManager() const { return ToFileManager; }
255 /// \brief Retrieve the file manager that AST nodes are being imported from.
256 FileManager &getFromFileManager() const { return FromFileManager; }
258 /// \brief Report a diagnostic in the "to" context.
259 DiagnosticBuilder ToDiag(SourceLocation Loc, unsigned DiagID);
261 /// \brief Report a diagnostic in the "from" context.
262 DiagnosticBuilder FromDiag(SourceLocation Loc, unsigned DiagID);
264 /// \brief Return the set of declarations that we know are not equivalent.
265 NonEquivalentDeclSet &getNonEquivalentDecls() { return NonEquivalentDecls; }
267 /// \brief Called for ObjCInterfaceDecl, ObjCProtocolDecl, and TagDecl.
268 /// Mark the Decl as complete, filling it in as much as possible.
270 /// \param D A declaration in the "to" context.
271 virtual void CompleteDecl(Decl* D);
273 /// \brief Note that we have imported the "from" declaration by mapping it
274 /// to the (potentially-newly-created) "to" declaration.
276 /// Subclasses can override this function to observe all of the \c From ->
277 /// \c To declaration mappings as they are imported.
278 virtual Decl *Imported(Decl *From, Decl *To);
280 /// \brief Called by StructuralEquivalenceContext. If a RecordDecl is
281 /// being compared to another RecordDecl as part of import, completing the
282 /// other RecordDecl may trigger importation of the first RecordDecl. This
283 /// happens especially for anonymous structs. If the original of the second
284 /// RecordDecl can be found, we can complete it without the need for
285 /// importation, eliminating this loop.
286 virtual Decl *GetOriginalDecl(Decl *To) { return nullptr; }
288 /// \brief Determine whether the given types are structurally
290 bool IsStructurallyEquivalent(QualType From, QualType To,
291 bool Complain = true);
295 #endif // LLVM_CLANG_AST_ASTIMPORTER_H