clang  7.0.0svn
ASTImporter.h
Go to the documentation of this file.
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 
20 #include "clang/AST/TemplateName.h"
21 #include "clang/AST/Type.h"
22 #include "clang/Basic/Diagnostic.h"
24 #include "clang/Basic/LLVM.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  /// Imports selected nodes from one AST context into another context,
47  /// merging AST nodes where appropriate.
48  class ASTImporter {
49  public:
52  llvm::DenseMap<const CXXBaseSpecifier *, CXXBaseSpecifier *>;
53 
54  private:
55  /// The contexts we're importing to and from.
56  ASTContext &ToContext, &FromContext;
57 
58  /// The file managers we're importing to and from.
59  FileManager &ToFileManager, &FromFileManager;
60 
61  /// Whether to perform a minimal import.
62  bool Minimal;
63 
64  /// Whether the last diagnostic came from the "from" context.
65  bool LastDiagFromFrom = false;
66 
67  /// Mapping from the already-imported types in the "from" context
68  /// to the corresponding types in the "to" context.
69  llvm::DenseMap<const Type *, const Type *> ImportedTypes;
70 
71  /// Mapping from the already-imported declarations in the "from"
72  /// context to the corresponding declarations in the "to" context.
73  llvm::DenseMap<Decl *, Decl *> ImportedDecls;
74 
75  /// Mapping from the already-imported statements in the "from"
76  /// context to the corresponding statements in the "to" context.
77  llvm::DenseMap<Stmt *, Stmt *> ImportedStmts;
78 
79  /// Mapping from the already-imported FileIDs in the "from" source
80  /// manager to the corresponding FileIDs in the "to" source manager.
81  llvm::DenseMap<FileID, FileID> ImportedFileIDs;
82 
83  /// Mapping from the already-imported CXXBasesSpecifier in
84  /// the "from" source manager to the corresponding CXXBasesSpecifier
85  /// in the "to" source manager.
86  ImportedCXXBaseSpecifierMap ImportedCXXBaseSpecifiers;
87 
88  /// Declaration (from, to) pairs that are known not to be equivalent
89  /// (which we have already complained about).
90  NonEquivalentDeclSet NonEquivalentDecls;
91 
92  public:
93  /// Create a new AST importer.
94  ///
95  /// \param ToContext The context we'll be importing into.
96  ///
97  /// \param ToFileManager The file manager we'll be importing into.
98  ///
99  /// \param FromContext The context we'll be importing from.
100  ///
101  /// \param FromFileManager The file manager we'll be importing into.
102  ///
103  /// \param MinimalImport If true, the importer will attempt to import
104  /// as little as it can, e.g., by importing declarations as forward
105  /// declarations that can be completed at a later point.
106  ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
107  ASTContext &FromContext, FileManager &FromFileManager,
108  bool MinimalImport);
109 
110  virtual ~ASTImporter();
111 
112  /// Whether the importer will perform a minimal import, creating
113  /// to-be-completed forward declarations when possible.
114  bool isMinimalImport() const { return Minimal; }
115 
116  /// Import the given type from the "from" context into the "to"
117  /// context.
118  ///
119  /// \returns the equivalent type in the "to" context, or a NULL type if
120  /// an error occurred.
121  QualType Import(QualType FromT);
122 
123  /// Import the given type source information from the
124  /// "from" context into the "to" context.
125  ///
126  /// \returns the equivalent type source information in the "to"
127  /// context, or NULL if an error occurred.
129 
130  /// Import the given attribute from the "from" context into the
131  /// "to" context.
132  ///
133  /// \returns the equivalent attribute in the "to" context.
134  Attr *Import(const Attr *FromAttr);
135 
136  /// Import the given declaration from the "from" context into the
137  /// "to" context.
138  ///
139  /// \returns the equivalent declaration in the "to" context, or a NULL type
140  /// if an error occurred.
141  Decl *Import(Decl *FromD);
142  Decl *Import(const Decl *FromD) {
143  return Import(const_cast<Decl *>(FromD));
144  }
145 
146  /// Return the copy of the given declaration in the "to" context if
147  /// it has already been imported from the "from" context. Otherwise return
148  /// NULL.
150 
151  /// Import the given declaration context from the "from"
152  /// AST context into the "to" AST context.
153  ///
154  /// \returns the equivalent declaration context in the "to"
155  /// context, or a NULL type if an error occurred.
157 
158  /// Import the given expression from the "from" context into the
159  /// "to" context.
160  ///
161  /// \returns the equivalent expression in the "to" context, or NULL if
162  /// an error occurred.
163  Expr *Import(Expr *FromE);
164 
165  /// Import the given statement from the "from" context into the
166  /// "to" context.
167  ///
168  /// \returns the equivalent statement in the "to" context, or NULL if
169  /// an error occurred.
170  Stmt *Import(Stmt *FromS);
171 
172  /// Import the given nested-name-specifier from the "from"
173  /// context into the "to" context.
174  ///
175  /// \returns the equivalent nested-name-specifier in the "to"
176  /// context, or NULL if an error occurred.
178 
179  /// Import the given nested-name-specifier from the "from"
180  /// context into the "to" context.
181  ///
182  /// \returns the equivalent nested-name-specifier in the "to"
183  /// context.
185 
186  /// Import the goven template name from the "from" context into the
187  /// "to" context.
189 
190  /// Import the given source location from the "from" context into
191  /// the "to" context.
192  ///
193  /// \returns the equivalent source location in the "to" context, or an
194  /// invalid source location if an error occurred.
196 
197  /// Import the given source range from the "from" context into
198  /// the "to" context.
199  ///
200  /// \returns the equivalent source range in the "to" context, or an
201  /// invalid source location if an error occurred.
202  SourceRange Import(SourceRange FromRange);
203 
204  /// Import the given declaration name from the "from"
205  /// context into the "to" context.
206  ///
207  /// \returns the equivalent declaration name in the "to" context,
208  /// or an empty declaration name if an error occurred.
210 
211  /// Import the given identifier from the "from" context
212  /// into the "to" context.
213  ///
214  /// \returns the equivalent identifier in the "to" context.
215  IdentifierInfo *Import(const IdentifierInfo *FromId);
216 
217  /// Import the given Objective-C selector from the "from"
218  /// context into the "to" context.
219  ///
220  /// \returns the equivalent selector in the "to" context.
221  Selector Import(Selector FromSel);
222 
223  /// Import the given file ID from the "from" context into the
224  /// "to" context.
225  ///
226  /// \returns the equivalent file ID in the source manager of the "to"
227  /// context.
229 
230  /// Import the given C++ constructor initializer from the "from"
231  /// context into the "to" context.
232  ///
233  /// \returns the equivalent initializer in the "to" context.
235 
236  /// Import the given CXXBaseSpecifier from the "from" context into
237  /// the "to" context.
238  ///
239  /// \returns the equivalent CXXBaseSpecifier in the source manager of the
240  /// "to" context.
241  CXXBaseSpecifier *Import(const CXXBaseSpecifier *FromSpec);
242 
243  /// Import the definition of the given declaration, including all of
244  /// the declarations it contains.
245  ///
246  /// This routine is intended to be used
247  void ImportDefinition(Decl *From);
248 
249  /// Cope with a name conflict when importing a declaration into the
250  /// given context.
251  ///
252  /// This routine is invoked whenever there is a name conflict while
253  /// importing a declaration. The returned name will become the name of the
254  /// imported declaration. By default, the returned name is the same as the
255  /// original name, leaving the conflict unresolve such that name lookup
256  /// for this name is likely to find an ambiguity later.
257  ///
258  /// Subclasses may override this routine to resolve the conflict, e.g., by
259  /// renaming the declaration being imported.
260  ///
261  /// \param Name the name of the declaration being imported, which conflicts
262  /// with other declarations.
263  ///
264  /// \param DC the declaration context (in the "to" AST context) in which
265  /// the name is being imported.
266  ///
267  /// \param IDNS the identifier namespace in which the name will be found.
268  ///
269  /// \param Decls the set of declarations with the same name as the
270  /// declaration being imported.
271  ///
272  /// \param NumDecls the number of conflicting declarations in \p Decls.
273  ///
274  /// \returns the name that the newly-imported declaration should have.
276  DeclContext *DC,
277  unsigned IDNS,
278  NamedDecl **Decls,
279  unsigned NumDecls);
280 
281  /// Retrieve the context that AST nodes are being imported into.
282  ASTContext &getToContext() const { return ToContext; }
283 
284  /// Retrieve the context that AST nodes are being imported from.
285  ASTContext &getFromContext() const { return FromContext; }
286 
287  /// Retrieve the file manager that AST nodes are being imported into.
288  FileManager &getToFileManager() const { return ToFileManager; }
289 
290  /// Retrieve the file manager that AST nodes are being imported from.
291  FileManager &getFromFileManager() const { return FromFileManager; }
292 
293  /// Report a diagnostic in the "to" context.
294  DiagnosticBuilder ToDiag(SourceLocation Loc, unsigned DiagID);
295 
296  /// Report a diagnostic in the "from" context.
297  DiagnosticBuilder FromDiag(SourceLocation Loc, unsigned DiagID);
298 
299  /// Return the set of declarations that we know are not equivalent.
300  NonEquivalentDeclSet &getNonEquivalentDecls() { return NonEquivalentDecls; }
301 
302  /// Called for ObjCInterfaceDecl, ObjCProtocolDecl, and TagDecl.
303  /// Mark the Decl as complete, filling it in as much as possible.
304  ///
305  /// \param D A declaration in the "to" context.
306  virtual void CompleteDecl(Decl* D);
307 
308  /// Note that we have imported the "from" declaration by mapping it
309  /// to the (potentially-newly-created) "to" declaration.
310  ///
311  /// Subclasses can override this function to observe all of the \c From ->
312  /// \c To declaration mappings as they are imported.
313  virtual Decl *Imported(Decl *From, Decl *To);
314 
315  /// Called by StructuralEquivalenceContext. If a RecordDecl is
316  /// being compared to another RecordDecl as part of import, completing the
317  /// other RecordDecl may trigger importation of the first RecordDecl. This
318  /// happens especially for anonymous structs. If the original of the second
319  /// RecordDecl can be found, we can complete it without the need for
320  /// importation, eliminating this loop.
321  virtual Decl *GetOriginalDecl(Decl *To) { return nullptr; }
322 
323  /// Determine whether the given types are structurally
324  /// equivalent.
326  bool Complain = true);
327  };
328 
329 } // namespace clang
330 
331 #endif // LLVM_CLANG_AST_ASTIMPORTER_H
Smart pointer class that efficiently represents Objective-C method names.
A (possibly-)qualified type.
Definition: Type.h:654
Implements support for file system lookup, file system caching, and directory search management...
Definition: FileManager.h:116
Stmt - This represents one statement.
Definition: Stmt.h:66
C Language Family Type Representation.
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
A container of type source information.
Definition: Decl.h:86
virtual Decl * GetOriginalDecl(Decl *To)
Called by StructuralEquivalenceContext.
Definition: ASTImporter.h:321
FileManager & getFromFileManager() const
Retrieve the file manager that AST nodes are being imported from.
Definition: ASTImporter.h:291
DiagnosticBuilder ToDiag(SourceLocation Loc, unsigned DiagID)
Report a diagnostic in the "to" context.
QualType Import(QualType FromT)
Import the given type from the "from" context into the "to" context.
virtual DeclarationName HandleNameConflict(DeclarationName Name, DeclContext *DC, unsigned IDNS, NamedDecl **Decls, unsigned NumDecls)
Cope with a name conflict when importing a declaration into the given context.
One of these records is kept for each identifier that is lexed.
DiagnosticBuilder FromDiag(SourceLocation Loc, unsigned DiagID)
Report a diagnostic in the "from" context.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:150
A C++ nested-name-specifier augmented with source location information.
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
Defines the Diagnostic-related interfaces.
bool IsStructurallyEquivalent(QualType From, QualType To, bool Complain=true)
Determine whether the given types are structurally equivalent.
llvm::DenseMap< const CXXBaseSpecifier *, CXXBaseSpecifier * > ImportedCXXBaseSpecifierMap
Definition: ASTImporter.h:52
ASTImporter(ASTContext &ToContext, FileManager &ToFileManager, ASTContext &FromContext, FileManager &FromFileManager, bool MinimalImport)
Create a new AST importer.
ASTContext & getToContext() const
Retrieve the context that AST nodes are being imported into.
Definition: ASTImporter.h:282
A little helper class used to produce diagnostics.
Definition: Diagnostic.h:1042
Expr - This represents one expression.
Definition: Expr.h:106
Represents a C++ template name within the type system.
Definition: TemplateName.h:178
Defines the clang::IdentifierInfo, clang::IdentifierTable, and clang::Selector interfaces.
Decl * GetAlreadyImportedOrNull(Decl *FromD)
Return the copy of the given declaration in the "to" context if it has already been imported from the...
virtual ~ASTImporter()
Encodes a location in the source.
Represents a C++ nested name specifier, such as "\::std::vector<int>::".
bool isMinimalImport() const
Whether the importer will perform a minimal import, creating to-be-completed forward declarations whe...
Definition: ASTImporter.h:114
FileManager & getToFileManager() const
Retrieve the file manager that AST nodes are being imported into.
Definition: ASTImporter.h:288
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
NonEquivalentDeclSet & getNonEquivalentDecls()
Return the set of declarations that we know are not equivalent.
Definition: ASTImporter.h:300
virtual void CompleteDecl(Decl *D)
Called for ObjCInterfaceDecl, ObjCProtocolDecl, and TagDecl.
Dataflow Directional Tag Classes.
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1262
virtual Decl * Imported(Decl *From, Decl *To)
Note that we have imported the "from" declaration by mapping it to the (potentially-newly-created) "t...
DeclarationName - The name of a declaration.
Imports selected nodes from one AST context into another context, merging AST nodes where appropriate...
Definition: ASTImporter.h:48
Represents a C++ base or member initializer.
Definition: DeclCXX.h:2238
Decl * Import(const Decl *FromD)
Definition: ASTImporter.h:142
Represents a base class of a C++ class.
Definition: DeclCXX.h:192
void ImportDefinition(Decl *From)
Import the definition of the given declaration, including all of the declarations it contains...
Defines the clang::SourceLocation class and associated facilities.
ASTContext & getFromContext() const
Retrieve the context that AST nodes are being imported from.
Definition: ASTImporter.h:285
A trivial tuple used to represent a source range.
This represents a decl that may have a name.
Definition: Decl.h:248
DeclContext * ImportContext(DeclContext *FromDC)
Import the given declaration context from the "from" AST context into the "to" AST context...
Attr - This represents one attribute.
Definition: Attr.h:43