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"
31 class FileSystemOptions
;
33 class NestedNameSpecifier
;
37 /// \brief Imports selected nodes from one AST context into another context,
38 /// merging AST nodes where appropriate.
41 typedef llvm::DenseSet
<std::pair
<Decl
*, Decl
*> > NonEquivalentDeclSet
;
44 /// \brief The contexts we're importing to and from.
45 ASTContext
&ToContext
, &FromContext
;
47 /// \brief The file managers we're importing to and from.
48 FileManager
&ToFileManager
, &FromFileManager
;
50 const FileSystemOptions
&ToFileSystemOpts
, &FromFileSystemOpts
;
52 /// \brief The diagnostics object that we should use to emit diagnostics.
55 /// \brief Mapping from the already-imported types in the "from" context
56 /// to the corresponding types in the "to" context.
57 llvm::DenseMap
<Type
*, 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 llvm::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 ASTImporter(Diagnostic
&Diags
,
81 ASTContext
&ToContext
, FileManager
&ToFileManager
,
82 const FileSystemOptions
&ToFileSystemOpts
,
83 ASTContext
&FromContext
, FileManager
&FromFileManager
,
84 const FileSystemOptions
&FromFileSystemOpts
);
86 virtual ~ASTImporter();
88 /// \brief Import the given type from the "from" context into the "to"
91 /// \returns the equivalent type in the "to" context, or a NULL type if
92 /// an error occurred.
93 QualType
Import(QualType FromT
);
95 /// \brief Import the given type source information from the
96 /// "from" context into the "to" context.
98 /// \returns the equivalent type source information in the "to"
99 /// context, or NULL if an error occurred.
100 TypeSourceInfo
*Import(TypeSourceInfo
*FromTSI
);
102 /// \brief Import the given declaration from the "from" context into the
105 /// \returns the equivalent declaration in the "to" context, or a NULL type
106 /// if an error occurred.
107 Decl
*Import(Decl
*FromD
);
109 /// \brief Import the given declaration context from the "from"
110 /// AST context into the "to" AST context.
112 /// \returns the equivalent declaration context in the "to"
113 /// context, or a NULL type if an error occurred.
114 DeclContext
*ImportContext(DeclContext
*FromDC
);
116 /// \brief Import the given expression from the "from" context into the
119 /// \returns the equivalent expression in the "to" context, or NULL if
120 /// an error occurred.
121 Expr
*Import(Expr
*FromE
);
123 /// \brief Import the given statement from the "from" context into the
126 /// \returns the equivalent statement in the "to" context, or NULL if
127 /// an error occurred.
128 Stmt
*Import(Stmt
*FromS
);
130 /// \brief Import the given nested-name-specifier from the "from"
131 /// context into the "to" context.
133 /// \returns the equivalent nested-name-specifier in the "to"
134 /// context, or NULL if an error occurred.
135 NestedNameSpecifier
*Import(NestedNameSpecifier
*FromNNS
);
137 /// \brief Import the given source location from the "from" context into
138 /// the "to" context.
140 /// \returns the equivalent source location in the "to" context, or an
141 /// invalid source location if an error occurred.
142 SourceLocation
Import(SourceLocation FromLoc
);
144 /// \brief Import the given source range from the "from" context into
145 /// the "to" context.
147 /// \returns the equivalent source range in the "to" context, or an
148 /// invalid source location if an error occurred.
149 SourceRange
Import(SourceRange FromRange
);
151 /// \brief Import the given declaration name from the "from"
152 /// context into the "to" context.
154 /// \returns the equivalent declaration name in the "to" context,
155 /// or an empty declaration name if an error occurred.
156 DeclarationName
Import(DeclarationName FromName
);
158 /// \brief Import the given identifier from the "from" context
159 /// into the "to" context.
161 /// \returns the equivalent identifier in the "to" context.
162 IdentifierInfo
*Import(IdentifierInfo
*FromId
);
164 /// \brief Import the given Objective-C selector from the "from"
165 /// context into the "to" context.
167 /// \returns the equivalent selector in the "to" context.
168 Selector
Import(Selector FromSel
);
170 /// \brief Import the given file ID from the "from" context into the
173 /// \returns the equivalent file ID in the source manager of the "to"
175 FileID
Import(FileID
);
177 /// \brief Cope with a name conflict when importing a declaration into the
180 /// This routine is invoked whenever there is a name conflict while
181 /// importing a declaration. The returned name will become the name of the
182 /// imported declaration. By default, the returned name is the same as the
183 /// original name, leaving the conflict unresolve such that name lookup
184 /// for this name is likely to find an ambiguity later.
186 /// Subclasses may override this routine to resolve the conflict, e.g., by
187 /// renaming the declaration being imported.
189 /// \param Name the name of the declaration being imported, which conflicts
190 /// with other declarations.
192 /// \param DC the declaration context (in the "to" AST context) in which
193 /// the name is being imported.
195 /// \param IDNS the identifier namespace in which the name will be found.
197 /// \param Decls the set of declarations with the same name as the
198 /// declaration being imported.
200 /// \param NumDecls the number of conflicting declarations in \p Decls.
202 /// \returns the name that the newly-imported declaration should have.
203 virtual DeclarationName
HandleNameConflict(DeclarationName Name
,
209 /// \brief Retrieve the context that AST nodes are being imported into.
210 ASTContext
&getToContext() const { return ToContext
; }
212 /// \brief Retrieve the context that AST nodes are being imported from.
213 ASTContext
&getFromContext() const { return FromContext
; }
215 /// \brief Retrieve the file manager that AST nodes are being imported into.
216 FileManager
&getToFileManager() const { return ToFileManager
; }
218 /// \brief Retrieve the file manager that AST nodes are being imported from.
219 FileManager
&getFromFileManager() const { return FromFileManager
; }
221 /// \brief Retrieve the diagnostic formatter.
222 Diagnostic
&getDiags() const { return Diags
; }
224 /// \brief Report a diagnostic in the "to" context.
225 DiagnosticBuilder
ToDiag(SourceLocation Loc
, unsigned DiagID
);
227 /// \brief Report a diagnostic in the "from" context.
228 DiagnosticBuilder
FromDiag(SourceLocation Loc
, unsigned DiagID
);
230 /// \brief Return the set of declarations that we know are not equivalent.
231 NonEquivalentDeclSet
&getNonEquivalentDecls() { return NonEquivalentDecls
; }
233 /// \brief Note that we have imported the "from" declaration by mapping it
234 /// to the (potentially-newly-created) "to" declaration.
237 Decl
*Imported(Decl
*From
, Decl
*To
);
239 /// \brief Determine whether the given types are structurally
241 bool IsStructurallyEquivalent(QualType From
, QualType To
);
245 #endif // LLVM_CLANG_AST_ASTIMPORTER_H