]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/include/clang/AST/ASTImporter.h
Merge clang 7.0.1 and several follow-up changes
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / include / clang / AST / ASTImporter.h
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
15 #ifndef LLVM_CLANG_AST_ASTIMPORTER_H
16 #define LLVM_CLANG_AST_ASTIMPORTER_H
17
18 #include "clang/AST/DeclarationName.h"
19 #include "clang/AST/NestedNameSpecifier.h"
20 #include "clang/AST/TemplateName.h"
21 #include "clang/AST/Type.h"
22 #include "clang/Basic/Diagnostic.h"
23 #include "clang/Basic/IdentifierTable.h"
24 #include "clang/Basic/LLVM.h"
25 #include "clang/Basic/SourceLocation.h"
26 #include "llvm/ADT/DenseMap.h"
27 #include "llvm/ADT/DenseSet.h"
28 #include "llvm/ADT/SmallVector.h"
29 #include <utility>
30
31 namespace clang {
32
33 class ASTContext;
34 class CXXBaseSpecifier;
35 class CXXCtorInitializer;
36 class Decl;
37 class DeclContext;
38 class Expr;
39 class FileManager;
40 class NamedDecl;
41 class Stmt;
42 class TagDecl;
43 class TypeSourceInfo;
44 class Attr;
45
46   // \brief Returns with a list of declarations started from the canonical decl
47   // then followed by subsequent decls in the translation unit.
48   // This gives a canonical list for each entry in the redecl chain.
49   // `Decl::redecls()` gives a list of decls which always start from the
50   // previous decl and the next item is actually the previous item in the order
51   // of source locations.  Thus, `Decl::redecls()` gives different lists for
52   // the different entries in a given redecl chain.
53   llvm::SmallVector<Decl*, 2> getCanonicalForwardRedeclChain(Decl* D);
54
55   /// Imports selected nodes from one AST context into another context,
56   /// merging AST nodes where appropriate.
57   class ASTImporter {
58   public:
59     using NonEquivalentDeclSet = llvm::DenseSet<std::pair<Decl *, Decl *>>;
60     using ImportedCXXBaseSpecifierMap =
61         llvm::DenseMap<const CXXBaseSpecifier *, CXXBaseSpecifier *>;
62
63   private:
64     /// The contexts we're importing to and from.
65     ASTContext &ToContext, &FromContext;
66
67     /// The file managers we're importing to and from.
68     FileManager &ToFileManager, &FromFileManager;
69
70     /// Whether to perform a minimal import.
71     bool Minimal;
72
73     /// Whether the last diagnostic came from the "from" context.
74     bool LastDiagFromFrom = false;
75
76     /// Mapping from the already-imported types in the "from" context
77     /// to the corresponding types in the "to" context.
78     llvm::DenseMap<const Type *, const Type *> ImportedTypes;
79
80     /// Mapping from the already-imported declarations in the "from"
81     /// context to the corresponding declarations in the "to" context.
82     llvm::DenseMap<Decl *, Decl *> ImportedDecls;
83
84     /// Mapping from the already-imported statements in the "from"
85     /// context to the corresponding statements in the "to" context.
86     llvm::DenseMap<Stmt *, Stmt *> ImportedStmts;
87
88     /// Mapping from the already-imported FileIDs in the "from" source
89     /// manager to the corresponding FileIDs in the "to" source manager.
90     llvm::DenseMap<FileID, FileID> ImportedFileIDs;
91
92     /// Mapping from the already-imported CXXBasesSpecifier in
93     ///  the "from" source manager to the corresponding CXXBasesSpecifier
94     ///  in the "to" source manager.
95     ImportedCXXBaseSpecifierMap ImportedCXXBaseSpecifiers;
96
97     /// Declaration (from, to) pairs that are known not to be equivalent
98     /// (which we have already complained about).
99     NonEquivalentDeclSet NonEquivalentDecls;
100
101   public:
102     /// Create a new AST importer.
103     ///
104     /// \param ToContext The context we'll be importing into.
105     ///
106     /// \param ToFileManager The file manager we'll be importing into.
107     ///
108     /// \param FromContext The context we'll be importing from.
109     ///
110     /// \param FromFileManager The file manager we'll be importing into.
111     ///
112     /// \param MinimalImport If true, the importer will attempt to import
113     /// as little as it can, e.g., by importing declarations as forward
114     /// declarations that can be completed at a later point.
115     ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
116                 ASTContext &FromContext, FileManager &FromFileManager,
117                 bool MinimalImport);
118
119     virtual ~ASTImporter();
120
121     /// Whether the importer will perform a minimal import, creating
122     /// to-be-completed forward declarations when possible.
123     bool isMinimalImport() const { return Minimal; }
124
125     /// Import the given type from the "from" context into the "to"
126     /// context.
127     ///
128     /// \returns the equivalent type in the "to" context, or a NULL type if
129     /// an error occurred.
130     QualType Import(QualType FromT);
131
132     /// Import the given type source information from the
133     /// "from" context into the "to" context.
134     ///
135     /// \returns the equivalent type source information in the "to"
136     /// context, or NULL if an error occurred.
137     TypeSourceInfo *Import(TypeSourceInfo *FromTSI);
138
139     /// Import the given attribute from the "from" context into the
140     /// "to" context.
141     ///
142     /// \returns the equivalent attribute in the "to" context.
143     Attr *Import(const Attr *FromAttr);
144
145     /// Import the given declaration from the "from" context into the
146     /// "to" context.
147     ///
148     /// \returns the equivalent declaration in the "to" context, or a NULL type
149     /// if an error occurred.
150     Decl *Import(Decl *FromD);
151     Decl *Import(const Decl *FromD) {
152       return Import(const_cast<Decl *>(FromD));
153     }
154
155     /// Return the copy of the given declaration in the "to" context if
156     /// it has already been imported from the "from" context.  Otherwise return
157     /// NULL.
158     Decl *GetAlreadyImportedOrNull(Decl *FromD);
159
160     /// Import the given declaration context from the "from"
161     /// AST context into the "to" AST context.
162     ///
163     /// \returns the equivalent declaration context in the "to"
164     /// context, or a NULL type if an error occurred.
165     DeclContext *ImportContext(DeclContext *FromDC);
166
167     /// Import the given expression from the "from" context into the
168     /// "to" context.
169     ///
170     /// \returns the equivalent expression in the "to" context, or NULL if
171     /// an error occurred.
172     Expr *Import(Expr *FromE);
173
174     /// Import the given statement from the "from" context into the
175     /// "to" context.
176     ///
177     /// \returns the equivalent statement in the "to" context, or NULL if
178     /// an error occurred.
179     Stmt *Import(Stmt *FromS);
180
181     /// Import the given nested-name-specifier from the "from"
182     /// context into the "to" context.
183     ///
184     /// \returns the equivalent nested-name-specifier in the "to"
185     /// context, or NULL if an error occurred.
186     NestedNameSpecifier *Import(NestedNameSpecifier *FromNNS);
187
188     /// Import the given nested-name-specifier from the "from"
189     /// context into the "to" context.
190     ///
191     /// \returns the equivalent nested-name-specifier in the "to"
192     /// context.
193     NestedNameSpecifierLoc Import(NestedNameSpecifierLoc FromNNS);
194
195     /// Import the goven template name from the "from" context into the
196     /// "to" context.
197     TemplateName Import(TemplateName From);
198
199     /// Import the given source location from the "from" context into
200     /// the "to" context.
201     ///
202     /// \returns the equivalent source location in the "to" context, or an
203     /// invalid source location if an error occurred.
204     SourceLocation Import(SourceLocation FromLoc);
205
206     /// Import the given source range from the "from" context into
207     /// the "to" context.
208     ///
209     /// \returns the equivalent source range in the "to" context, or an
210     /// invalid source location if an error occurred.
211     SourceRange Import(SourceRange FromRange);
212
213     /// Import the given declaration name from the "from"
214     /// context into the "to" context.
215     ///
216     /// \returns the equivalent declaration name in the "to" context,
217     /// or an empty declaration name if an error occurred.
218     DeclarationName Import(DeclarationName FromName);
219
220     /// Import the given identifier from the "from" context
221     /// into the "to" context.
222     ///
223     /// \returns the equivalent identifier in the "to" context.
224     IdentifierInfo *Import(const IdentifierInfo *FromId);
225
226     /// Import the given Objective-C selector from the "from"
227     /// context into the "to" context.
228     ///
229     /// \returns the equivalent selector in the "to" context.
230     Selector Import(Selector FromSel);
231
232     /// Import the given file ID from the "from" context into the
233     /// "to" context.
234     ///
235     /// \returns the equivalent file ID in the source manager of the "to"
236     /// context.
237     FileID Import(FileID);
238
239     /// Import the given C++ constructor initializer from the "from"
240     /// context into the "to" context.
241     ///
242     /// \returns the equivalent initializer in the "to" context.
243     CXXCtorInitializer *Import(CXXCtorInitializer *FromInit);
244
245     /// Import the given CXXBaseSpecifier from the "from" context into
246     /// the "to" context.
247     ///
248     /// \returns the equivalent CXXBaseSpecifier in the source manager of the
249     /// "to" context.
250     CXXBaseSpecifier *Import(const CXXBaseSpecifier *FromSpec);
251
252     /// Import the definition of the given declaration, including all of
253     /// the declarations it contains.
254     ///
255     /// This routine is intended to be used
256     void ImportDefinition(Decl *From);
257
258     /// Cope with a name conflict when importing a declaration into the
259     /// given context.
260     ///
261     /// This routine is invoked whenever there is a name conflict while
262     /// importing a declaration. The returned name will become the name of the
263     /// imported declaration. By default, the returned name is the same as the
264     /// original name, leaving the conflict unresolve such that name lookup
265     /// for this name is likely to find an ambiguity later.
266     ///
267     /// Subclasses may override this routine to resolve the conflict, e.g., by
268     /// renaming the declaration being imported.
269     ///
270     /// \param Name the name of the declaration being imported, which conflicts
271     /// with other declarations.
272     ///
273     /// \param DC the declaration context (in the "to" AST context) in which
274     /// the name is being imported.
275     ///
276     /// \param IDNS the identifier namespace in which the name will be found.
277     ///
278     /// \param Decls the set of declarations with the same name as the
279     /// declaration being imported.
280     ///
281     /// \param NumDecls the number of conflicting declarations in \p Decls.
282     ///
283     /// \returns the name that the newly-imported declaration should have.
284     virtual DeclarationName HandleNameConflict(DeclarationName Name,
285                                                DeclContext *DC,
286                                                unsigned IDNS,
287                                                NamedDecl **Decls,
288                                                unsigned NumDecls);
289
290     /// Retrieve the context that AST nodes are being imported into.
291     ASTContext &getToContext() const { return ToContext; }
292
293     /// Retrieve the context that AST nodes are being imported from.
294     ASTContext &getFromContext() const { return FromContext; }
295
296     /// Retrieve the file manager that AST nodes are being imported into.
297     FileManager &getToFileManager() const { return ToFileManager; }
298
299     /// Retrieve the file manager that AST nodes are being imported from.
300     FileManager &getFromFileManager() const { return FromFileManager; }
301
302     /// Report a diagnostic in the "to" context.
303     DiagnosticBuilder ToDiag(SourceLocation Loc, unsigned DiagID);
304
305     /// Report a diagnostic in the "from" context.
306     DiagnosticBuilder FromDiag(SourceLocation Loc, unsigned DiagID);
307
308     /// Return the set of declarations that we know are not equivalent.
309     NonEquivalentDeclSet &getNonEquivalentDecls() { return NonEquivalentDecls; }
310
311     /// Called for ObjCInterfaceDecl, ObjCProtocolDecl, and TagDecl.
312     /// Mark the Decl as complete, filling it in as much as possible.
313     ///
314     /// \param D A declaration in the "to" context.
315     virtual void CompleteDecl(Decl* D);
316
317     /// Subclasses can override this function to observe all of the \c From ->
318     /// \c To declaration mappings as they are imported.
319     virtual Decl *Imported(Decl *From, Decl *To) { return To; }
320
321     /// Store and assign the imported declaration to its counterpart.
322     Decl *MapImported(Decl *From, Decl *To);
323
324     /// Called by StructuralEquivalenceContext.  If a RecordDecl is
325     /// being compared to another RecordDecl as part of import, completing the
326     /// other RecordDecl may trigger importation of the first RecordDecl. This
327     /// happens especially for anonymous structs.  If the original of the second
328     /// RecordDecl can be found, we can complete it without the need for
329     /// importation, eliminating this loop.
330     virtual Decl *GetOriginalDecl(Decl *To) { return nullptr; }
331
332     /// Determine whether the given types are structurally
333     /// equivalent.
334     bool IsStructurallyEquivalent(QualType From, QualType To,
335                                   bool Complain = true);
336   };
337
338 } // namespace clang
339
340 #endif // LLVM_CLANG_AST_ASTIMPORTER_H