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/Type.h"
18 #include "clang/AST/DeclarationName.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"
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 The diagnostics object that we should use to emit diagnostics.
52 /// \brief Mapping from the already-imported types in the "from" context
53 /// to the corresponding types in the "to" context.
54 llvm::DenseMap<Type *, 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<unsigned, FileID> ImportedFileIDs;
68 /// \brief Imported, anonymous tag declarations that are missing their
69 /// corresponding typedefs.
70 llvm::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 ASTImporter(Diagnostic &Diags,
78 ASTContext &ToContext, FileManager &ToFileManager,
79 ASTContext &FromContext, FileManager &FromFileManager);
81 virtual ~ASTImporter();
83 /// \brief Import the given type from the "from" context into the "to"
86 /// \returns the equivalent type in the "to" context, or a NULL type if
87 /// an error occurred.
88 QualType Import(QualType FromT);
90 /// \brief Import the given type source information from the
91 /// "from" context into the "to" context.
93 /// \returns the equivalent type source information in the "to"
94 /// context, or NULL if an error occurred.
95 TypeSourceInfo *Import(TypeSourceInfo *FromTSI);
97 /// \brief Import the given declaration from the "from" context into the
100 /// \returns the equivalent declaration in the "to" context, or a NULL type
101 /// if an error occurred.
102 Decl *Import(Decl *FromD);
104 /// \brief Import the given declaration context from the "from"
105 /// AST context into the "to" AST context.
107 /// \returns the equivalent declaration context in the "to"
108 /// context, or a NULL type if an error occurred.
109 DeclContext *ImportContext(DeclContext *FromDC);
111 /// \brief Import the given expression from the "from" context into the
114 /// \returns the equivalent expression in the "to" context, or NULL if
115 /// an error occurred.
116 Expr *Import(Expr *FromE);
118 /// \brief Import the given statement from the "from" context into the
121 /// \returns the equivalent statement in the "to" context, or NULL if
122 /// an error occurred.
123 Stmt *Import(Stmt *FromS);
125 /// \brief Import the given nested-name-specifier from the "from"
126 /// context into the "to" context.
128 /// \returns the equivalent nested-name-specifier in the "to"
129 /// context, or NULL if an error occurred.
130 NestedNameSpecifier *Import(NestedNameSpecifier *FromNNS);
132 /// \brief Import the given source location from the "from" context into
133 /// the "to" context.
135 /// \returns the equivalent source location in the "to" context, or an
136 /// invalid source location if an error occurred.
137 SourceLocation Import(SourceLocation FromLoc);
139 /// \brief Import the given source range from the "from" context into
140 /// the "to" context.
142 /// \returns the equivalent source range in the "to" context, or an
143 /// invalid source location if an error occurred.
144 SourceRange Import(SourceRange FromRange);
146 /// \brief Import the given declaration name from the "from"
147 /// context into the "to" context.
149 /// \returns the equivalent declaration name in the "to" context,
150 /// or an empty declaration name if an error occurred.
151 DeclarationName Import(DeclarationName FromName);
153 /// \brief Import the given identifier from the "from" context
154 /// into the "to" context.
156 /// \returns the equivalent identifier in the "to" context.
157 IdentifierInfo *Import(IdentifierInfo *FromId);
159 /// \brief Import the given Objective-C selector from the "from"
160 /// context into the "to" context.
162 /// \returns the equivalent selector in the "to" context.
163 Selector Import(Selector FromSel);
165 /// \brief Import the given file ID from the "from" context into the
168 /// \returns the equivalent file ID in the source manager of the "to"
170 FileID Import(FileID);
172 /// \brief Cope with a name conflict when importing a declaration into the
175 /// This routine is invoked whenever there is a name conflict while
176 /// importing a declaration. The returned name will become the name of the
177 /// imported declaration. By default, the returned name is the same as the
178 /// original name, leaving the conflict unresolve such that name lookup
179 /// for this name is likely to find an ambiguity later.
181 /// Subclasses may override this routine to resolve the conflict, e.g., by
182 /// renaming the declaration being imported.
184 /// \param Name the name of the declaration being imported, which conflicts
185 /// with other declarations.
187 /// \param DC the declaration context (in the "to" AST context) in which
188 /// the name is being imported.
190 /// \param IDNS the identifier namespace in which the name will be found.
192 /// \param Decls the set of declarations with the same name as the
193 /// declaration being imported.
195 /// \param NumDecls the number of conflicting declarations in \p Decls.
197 /// \returns the name that the newly-imported declaration should have.
198 virtual DeclarationName HandleNameConflict(DeclarationName Name,
204 /// \brief Retrieve the context that AST nodes are being imported into.
205 ASTContext &getToContext() const { return ToContext; }
207 /// \brief Retrieve the context that AST nodes are being imported from.
208 ASTContext &getFromContext() const { return FromContext; }
210 /// \brief Retrieve the file manager that AST nodes are being imported into.
211 FileManager &getToFileManager() const { return ToFileManager; }
213 /// \brief Retrieve the file manager that AST nodes are being imported from.
214 FileManager &getFromFileManager() const { return FromFileManager; }
216 /// \brief Retrieve the diagnostic formatter.
217 Diagnostic &getDiags() const { return Diags; }
219 /// \brief Report a diagnostic in the "to" context.
220 DiagnosticBuilder ToDiag(SourceLocation Loc, unsigned DiagID);
222 /// \brief Report a diagnostic in the "from" context.
223 DiagnosticBuilder FromDiag(SourceLocation Loc, unsigned DiagID);
225 /// \brief Return the set of declarations that we know are not equivalent.
226 NonEquivalentDeclSet &getNonEquivalentDecls() { return NonEquivalentDecls; }
228 /// \brief Note that we have imported the "from" declaration by mapping it
229 /// to the (potentially-newly-created) "to" declaration.
232 Decl *Imported(Decl *From, Decl *To);
234 /// \brief Determine whether the given types are structurally
236 bool IsStructurallyEquivalent(QualType From, QualType To);
240 #endif // LLVM_CLANG_AST_ASTIMPORTER_H