clang  8.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  // \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.
54 
55  /// Imports selected nodes from one AST context into another context,
56  /// merging AST nodes where appropriate.
57  class ASTImporter {
58  public:
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.
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.
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.
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.
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.
194 
195  /// Import the goven template name from the "from" context into the
196  /// "to" context.
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.
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.
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.
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.
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.
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.
335  bool Complain = true);
336 
337  /// Determine the index of a field in its parent record.
338  /// F should be a field (or indirect field) declaration.
339  /// \returns The index of the field in its parent context, starting from 1.
340  /// 0 is returned on error (parent context is non-record).
341  static unsigned getFieldIndex(Decl *F);
342 
343  };
344 
345 } // namespace clang
346 
347 #endif // LLVM_CLANG_AST_ASTIMPORTER_H
Smart pointer class that efficiently represents Objective-C method names.
A (possibly-)qualified type.
Definition: Type.h:642
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:330
FileManager & getFromFileManager() const
Retrieve the file manager that AST nodes are being imported from.
Definition: ASTImporter.h:300
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:154
A C++ nested-name-specifier augmented with source location information.
static unsigned getFieldIndex(Decl *F)
Determine the index of a field in its parent record.
Definition: ASTImporter.cpp:74
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:61
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:291
Decl * MapImported(Decl *From, Decl *To)
Store and assign the imported declaration to its counterpart.
A little helper class used to produce diagnostics.
Definition: Diagnostic.h:1042
This represents one expression.
Definition: Expr.h:105
Represents a C++ template name within the type system.
Definition: TemplateName.h:178
llvm::SmallVector< Decl *, 2 > getCanonicalForwardRedeclChain(Decl *D)
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:123
FileManager & getToFileManager() const
Retrieve the file manager that AST nodes are being imported into.
Definition: ASTImporter.h:297
virtual Decl * Imported(Decl *From, Decl *To)
Subclasses can override this function to observe all of the From -> To declaration mappings as they a...
Definition: ASTImporter.h:319
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:309
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:1267
DeclarationName - The name of a declaration.
Imports selected nodes from one AST context into another context, merging AST nodes where appropriate...
Definition: ASTImporter.h:57
Represents a C++ base or member initializer.
Definition: DeclCXX.h:2250
Decl * Import(const Decl *FromD)
Definition: ASTImporter.h:151
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:294
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