1 //===--- ASTImporter.h - Importing ASTs from other Contexts -----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This file defines the ASTImporter class which imports AST nodes from one
11 //  context into another context.
12 //
13 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_AST_ASTIMPORTER_H
15 #define LLVM_CLANG_AST_ASTIMPORTER_H
16 
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"
23 
24 namespace clang {
25   class ASTContext;
26   class Decl;
27   class DeclContext;
28   class DiagnosticsEngine;
29   class Expr;
30   class FileManager;
31   class IdentifierInfo;
32   class NestedNameSpecifier;
33   class Stmt;
34   class TypeSourceInfo;
35 
36   /// \brief Imports selected nodes from one AST context into another context,
37   /// merging AST nodes where appropriate.
38   class ASTImporter {
39   public:
40     typedef llvm::DenseSet<std::pair<Decl *, Decl *> > NonEquivalentDeclSet;
41 
42   private:
43     /// \brief The contexts we're importing to and from.
44     ASTContext &ToContext, &FromContext;
45 
46     /// \brief The file managers we're importing to and from.
47     FileManager &ToFileManager, &FromFileManager;
48 
49     /// \brief Whether to perform a minimal import.
50     bool Minimal;
51 
52     /// \brief Whether the last diagnostic came from the "from" context.
53     bool LastDiagFromFrom;
54 
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;
58 
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;
62 
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;
66 
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;
70 
71     /// \brief Imported, anonymous tag declarations that are missing their
72     /// corresponding typedefs.
73     SmallVector<TagDecl *, 4> AnonTagsWithPendingTypedefs;
74 
75     /// \brief Declaration (from, to) pairs that are known not to be equivalent
76     /// (which we have already complained about).
77     NonEquivalentDeclSet NonEquivalentDecls;
78 
79   public:
80     /// \brief Create a new AST importer.
81     ///
82     /// \param ToContext The context we'll be importing into.
83     ///
84     /// \param ToFileManager The file manager we'll be importing into.
85     ///
86     /// \param FromContext The context we'll be importing from.
87     ///
88     /// \param FromFileManager The file manager we'll be importing into.
89     ///
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,
95                 bool MinimalImport);
96 
97     virtual ~ASTImporter();
98 
99     /// \brief Whether the importer will perform a minimal import, creating
100     /// to-be-completed forward declarations when possible.
isMinimalImport()101     bool isMinimalImport() const { return Minimal; }
102 
103     /// \brief Import the given type from the "from" context into the "to"
104     /// context.
105     ///
106     /// \returns the equivalent type in the "to" context, or a NULL type if
107     /// an error occurred.
108     QualType Import(QualType FromT);
109 
110     /// \brief Import the given type source information from the
111     /// "from" context into the "to" context.
112     ///
113     /// \returns the equivalent type source information in the "to"
114     /// context, or NULL if an error occurred.
115     TypeSourceInfo *Import(TypeSourceInfo *FromTSI);
116 
117     /// \brief Import the given declaration from the "from" context into the
118     /// "to" context.
119     ///
120     /// \returns the equivalent declaration in the "to" context, or a NULL type
121     /// if an error occurred.
122     Decl *Import(Decl *FromD);
123 
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
126     /// NULL.
127     Decl *GetAlreadyImportedOrNull(Decl *FromD);
128 
129     /// \brief Import the given declaration context from the "from"
130     /// AST context into the "to" AST context.
131     ///
132     /// \returns the equivalent declaration context in the "to"
133     /// context, or a NULL type if an error occurred.
134     DeclContext *ImportContext(DeclContext *FromDC);
135 
136     /// \brief Import the given expression from the "from" context into the
137     /// "to" context.
138     ///
139     /// \returns the equivalent expression in the "to" context, or NULL if
140     /// an error occurred.
141     Expr *Import(Expr *FromE);
142 
143     /// \brief Import the given statement from the "from" context into the
144     /// "to" context.
145     ///
146     /// \returns the equivalent statement in the "to" context, or NULL if
147     /// an error occurred.
148     Stmt *Import(Stmt *FromS);
149 
150     /// \brief Import the given nested-name-specifier from the "from"
151     /// context into the "to" context.
152     ///
153     /// \returns the equivalent nested-name-specifier in the "to"
154     /// context, or NULL if an error occurred.
155     NestedNameSpecifier *Import(NestedNameSpecifier *FromNNS);
156 
157     /// \brief Import the given nested-name-specifier from the "from"
158     /// context into the "to" context.
159     ///
160     /// \returns the equivalent nested-name-specifier in the "to"
161     /// context.
162     NestedNameSpecifierLoc Import(NestedNameSpecifierLoc FromNNS);
163 
164     /// \brief Import the goven template name from the "from" context into the
165     /// "to" context.
166     TemplateName Import(TemplateName From);
167 
168     /// \brief Import the given source location from the "from" context into
169     /// the "to" context.
170     ///
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);
174 
175     /// \brief Import the given source range from the "from" context into
176     /// the "to" context.
177     ///
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);
181 
182     /// \brief Import the given declaration name from the "from"
183     /// context into the "to" context.
184     ///
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);
188 
189     /// \brief Import the given identifier from the "from" context
190     /// into the "to" context.
191     ///
192     /// \returns the equivalent identifier in the "to" context.
193     IdentifierInfo *Import(const IdentifierInfo *FromId);
194 
195     /// \brief Import the given Objective-C selector from the "from"
196     /// context into the "to" context.
197     ///
198     /// \returns the equivalent selector in the "to" context.
199     Selector Import(Selector FromSel);
200 
201     /// \brief Import the given file ID from the "from" context into the
202     /// "to" context.
203     ///
204     /// \returns the equivalent file ID in the source manager of the "to"
205     /// context.
206     FileID Import(FileID);
207 
208     /// \brief Import the definition of the given declaration, including all of
209     /// the declarations it contains.
210     ///
211     /// This routine is intended to be used
212     void ImportDefinition(Decl *From);
213 
214     /// \brief Cope with a name conflict when importing a declaration into the
215     /// given context.
216     ///
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.
222     ///
223     /// Subclasses may override this routine to resolve the conflict, e.g., by
224     /// renaming the declaration being imported.
225     ///
226     /// \param Name the name of the declaration being imported, which conflicts
227     /// with other declarations.
228     ///
229     /// \param DC the declaration context (in the "to" AST context) in which
230     /// the name is being imported.
231     ///
232     /// \param IDNS the identifier namespace in which the name will be found.
233     ///
234     /// \param Decls the set of declarations with the same name as the
235     /// declaration being imported.
236     ///
237     /// \param NumDecls the number of conflicting declarations in \p Decls.
238     ///
239     /// \returns the name that the newly-imported declaration should have.
240     virtual DeclarationName HandleNameConflict(DeclarationName Name,
241                                                DeclContext *DC,
242                                                unsigned IDNS,
243                                                NamedDecl **Decls,
244                                                unsigned NumDecls);
245 
246     /// \brief Retrieve the context that AST nodes are being imported into.
getToContext()247     ASTContext &getToContext() const { return ToContext; }
248 
249     /// \brief Retrieve the context that AST nodes are being imported from.
getFromContext()250     ASTContext &getFromContext() const { return FromContext; }
251 
252     /// \brief Retrieve the file manager that AST nodes are being imported into.
getToFileManager()253     FileManager &getToFileManager() const { return ToFileManager; }
254 
255     /// \brief Retrieve the file manager that AST nodes are being imported from.
getFromFileManager()256     FileManager &getFromFileManager() const { return FromFileManager; }
257 
258     /// \brief Report a diagnostic in the "to" context.
259     DiagnosticBuilder ToDiag(SourceLocation Loc, unsigned DiagID);
260 
261     /// \brief Report a diagnostic in the "from" context.
262     DiagnosticBuilder FromDiag(SourceLocation Loc, unsigned DiagID);
263 
264     /// \brief Return the set of declarations that we know are not equivalent.
getNonEquivalentDecls()265     NonEquivalentDeclSet &getNonEquivalentDecls() { return NonEquivalentDecls; }
266 
267     /// \brief Called for ObjCInterfaceDecl, ObjCProtocolDecl, and TagDecl.
268     /// Mark the Decl as complete, filling it in as much as possible.
269     ///
270     /// \param D A declaration in the "to" context.
271     virtual void CompleteDecl(Decl* D);
272 
273     /// \brief Note that we have imported the "from" declaration by mapping it
274     /// to the (potentially-newly-created) "to" declaration.
275     ///
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);
279 
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.
GetOriginalDecl(Decl * To)286     virtual Decl *GetOriginalDecl(Decl *To) { return nullptr; }
287 
288     /// \brief Determine whether the given types are structurally
289     /// equivalent.
290     bool IsStructurallyEquivalent(QualType From, QualType To,
291                                   bool Complain = true);
292   };
293 }
294 
295 #endif // LLVM_CLANG_AST_ASTIMPORTER_H
296