clang  8.0.0
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/Optional.h"
29 #include "llvm/ADT/SmallVector.h"
30 #include "llvm/Support/Error.h"
31 #include <utility>
32 
33 namespace clang {
34 
35 class ASTContext;
36 class ASTImporterLookupTable;
37 class CXXBaseSpecifier;
38 class CXXCtorInitializer;
39 class Decl;
40 class DeclContext;
41 class Expr;
42 class FileManager;
43 class NamedDecl;
44 class Stmt;
45 class TagDecl;
46 class TypeSourceInfo;
47 class Attr;
48 
49  class ImportError : public llvm::ErrorInfo<ImportError> {
50  public:
51  /// \brief Kind of error when importing an AST component.
52  enum ErrorKind {
53  NameConflict, /// Naming ambiguity (likely ODR violation).
54  UnsupportedConstruct, /// Not supported node or case.
55  Unknown /// Other error.
56  };
57 
59 
60  static char ID;
61 
62  ImportError() : Error(Unknown) { }
63  ImportError(const ImportError &Other) : Error(Other.Error) { }
64  ImportError(ErrorKind Error) : Error(Error) { }
65 
66  std::string toString() const;
67 
68  void log(raw_ostream &OS) const override;
69  std::error_code convertToErrorCode() const override;
70  };
71 
72  // \brief Returns with a list of declarations started from the canonical decl
73  // then followed by subsequent decls in the translation unit.
74  // This gives a canonical list for each entry in the redecl chain.
75  // `Decl::redecls()` gives a list of decls which always start from the
76  // previous decl and the next item is actually the previous item in the order
77  // of source locations. Thus, `Decl::redecls()` gives different lists for
78  // the different entries in a given redecl chain.
80 
81  /// Imports selected nodes from one AST context into another context,
82  /// merging AST nodes where appropriate.
83  class ASTImporter {
84  friend class ASTNodeImporter;
85  public:
88  llvm::DenseMap<const CXXBaseSpecifier *, CXXBaseSpecifier *>;
89 
90  private:
91 
92  /// Pointer to the import specific lookup table, which may be shared
93  /// amongst several ASTImporter objects.
94  /// This is an externally managed resource (and should exist during the
95  /// lifetime of the ASTImporter object)
96  /// If not set then the original C/C++ lookup is used.
97  ASTImporterLookupTable *LookupTable = nullptr;
98 
99  /// The contexts we're importing to and from.
100  ASTContext &ToContext, &FromContext;
101 
102  /// The file managers we're importing to and from.
103  FileManager &ToFileManager, &FromFileManager;
104 
105  /// Whether to perform a minimal import.
106  bool Minimal;
107 
108  /// Whether the last diagnostic came from the "from" context.
109  bool LastDiagFromFrom = false;
110 
111  /// Mapping from the already-imported types in the "from" context
112  /// to the corresponding types in the "to" context.
113  llvm::DenseMap<const Type *, const Type *> ImportedTypes;
114 
115  /// Mapping from the already-imported declarations in the "from"
116  /// context to the corresponding declarations in the "to" context.
117  llvm::DenseMap<Decl *, Decl *> ImportedDecls;
118 
119  /// Mapping from the already-imported statements in the "from"
120  /// context to the corresponding statements in the "to" context.
121  llvm::DenseMap<Stmt *, Stmt *> ImportedStmts;
122 
123  /// Mapping from the already-imported FileIDs in the "from" source
124  /// manager to the corresponding FileIDs in the "to" source manager.
125  llvm::DenseMap<FileID, FileID> ImportedFileIDs;
126 
127  /// Mapping from the already-imported CXXBasesSpecifier in
128  /// the "from" source manager to the corresponding CXXBasesSpecifier
129  /// in the "to" source manager.
130  ImportedCXXBaseSpecifierMap ImportedCXXBaseSpecifiers;
131 
132  /// Declaration (from, to) pairs that are known not to be equivalent
133  /// (which we have already complained about).
134  NonEquivalentDeclSet NonEquivalentDecls;
135 
137  FoundDeclsTy findDeclsInToCtx(DeclContext *DC, DeclarationName Name);
138 
139  void AddToLookupTable(Decl *ToD);
140 
141  public:
142 
143  /// \param ToContext The context we'll be importing into.
144  ///
145  /// \param ToFileManager The file manager we'll be importing into.
146  ///
147  /// \param FromContext The context we'll be importing from.
148  ///
149  /// \param FromFileManager The file manager we'll be importing into.
150  ///
151  /// \param MinimalImport If true, the importer will attempt to import
152  /// as little as it can, e.g., by importing declarations as forward
153  /// declarations that can be completed at a later point.
154  ///
155  /// \param LookupTable The importer specific lookup table which may be
156  /// shared amongst several ASTImporter objects.
157  /// If not set then the original C/C++ lookup is used.
158  ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
159  ASTContext &FromContext, FileManager &FromFileManager,
160  bool MinimalImport,
161  ASTImporterLookupTable *LookupTable = nullptr);
162 
163  virtual ~ASTImporter();
164 
165  /// Whether the importer will perform a minimal import, creating
166  /// to-be-completed forward declarations when possible.
167  bool isMinimalImport() const { return Minimal; }
168 
169  /// \brief Import the given object, returns the result.
170  ///
171  /// \param To Import the object into this variable.
172  /// \param From Object to import.
173  /// \return Error information (success or error).
174  template <typename ImportT>
175  LLVM_NODISCARD llvm::Error importInto(ImportT &To, const ImportT &From) {
176  To = Import(From);
177  if (From && !To)
178  return llvm::make_error<ImportError>();
179  return llvm::Error::success();
180  // FIXME: this should be the final code
181  //auto ToOrErr = Import(From);
182  //if (ToOrErr)
183  // To = *ToOrErr;
184  //return ToOrErr.takeError();
185  }
186 
187  /// Import the given type from the "from" context into the "to"
188  /// context. A null type is imported as a null type (no error).
189  ///
190  /// \returns The equivalent type in the "to" context, or the import error.
191  llvm::Expected<QualType> Import_New(QualType FromT);
192  // FIXME: Remove this version.
193  QualType Import(QualType FromT);
194 
195  /// Import the given type source information from the
196  /// "from" context into the "to" context.
197  ///
198  /// \returns The equivalent type source information in the "to"
199  /// context, or the import error.
201  // FIXME: Remove this version.
202  TypeSourceInfo *Import(TypeSourceInfo *FromTSI);
203 
204  /// Import the given attribute from the "from" context into the
205  /// "to" context.
206  ///
207  /// \returns The equivalent attribute in the "to" context, or the import
208  /// error.
209  llvm::Expected<Attr *> Import_New(const Attr *FromAttr);
210  // FIXME: Remove this version.
211  Attr *Import(const Attr *FromAttr);
212 
213  /// Import the given declaration from the "from" context into the
214  /// "to" context.
215  ///
216  /// \returns The equivalent declaration in the "to" context, or the import
217  /// error.
218  llvm::Expected<Decl *> Import_New(Decl *FromD);
220  return Import_New(const_cast<Decl *>(FromD));
221  }
222  // FIXME: Remove this version.
223  Decl *Import(Decl *FromD);
224  Decl *Import(const Decl *FromD) {
225  return Import(const_cast<Decl *>(FromD));
226  }
227 
228  /// Return the copy of the given declaration in the "to" context if
229  /// it has already been imported from the "from" context. Otherwise return
230  /// NULL.
231  Decl *GetAlreadyImportedOrNull(const Decl *FromD) const;
232 
233  /// Import the given declaration context from the "from"
234  /// AST context into the "to" AST context.
235  ///
236  /// \returns the equivalent declaration context in the "to"
237  /// context, or error value.
238  llvm::Expected<DeclContext *> ImportContext(DeclContext *FromDC);
239 
240  /// Import the given expression from the "from" context into the
241  /// "to" context.
242  ///
243  /// \returns The equivalent expression in the "to" context, or the import
244  /// error.
245  llvm::Expected<Expr *> Import_New(Expr *FromE);
246  // FIXME: Remove this version.
247  Expr *Import(Expr *FromE);
248 
249  /// Import the given statement from the "from" context into the
250  /// "to" context.
251  ///
252  /// \returns The equivalent statement in the "to" context, or the import
253  /// error.
254  llvm::Expected<Stmt *> Import_New(Stmt *FromS);
255  // FIXME: Remove this version.
256  Stmt *Import(Stmt *FromS);
257 
258  /// Import the given nested-name-specifier from the "from"
259  /// context into the "to" context.
260  ///
261  /// \returns The equivalent nested-name-specifier in the "to"
262  /// context, or the import error.
264  Import_New(NestedNameSpecifier *FromNNS);
265  // FIXME: Remove this version.
266  NestedNameSpecifier *Import(NestedNameSpecifier *FromNNS);
267 
268  /// Import the given nested-name-specifier-loc from the "from"
269  /// context into the "to" context.
270  ///
271  /// \returns The equivalent nested-name-specifier-loc in the "to"
272  /// context, or the import error.
274  Import_New(NestedNameSpecifierLoc FromNNS);
275  // FIXME: Remove this version.
277 
278  /// Import the given template name from the "from" context into the
279  /// "to" context, or the import error.
281  // FIXME: Remove this version.
282  TemplateName Import(TemplateName From);
283 
284  /// Import the given source location from the "from" context into
285  /// the "to" context.
286  ///
287  /// \returns The equivalent source location in the "to" context, or the
288  /// import error.
290  // FIXME: Remove this version.
291  SourceLocation Import(SourceLocation FromLoc);
292 
293  /// Import the given source range from the "from" context into
294  /// the "to" context.
295  ///
296  /// \returns The equivalent source range in the "to" context, or the import
297  /// error.
298  llvm::Expected<SourceRange> Import_New(SourceRange FromRange);
299  // FIXME: Remove this version.
300  SourceRange Import(SourceRange FromRange);
301 
302  /// Import the given declaration name from the "from"
303  /// context into the "to" context.
304  ///
305  /// \returns The equivalent declaration name in the "to" context, or the
306  /// import error.
308  // FIXME: Remove this version.
309  DeclarationName Import(DeclarationName FromName);
310 
311  /// Import the given identifier from the "from" context
312  /// into the "to" context.
313  ///
314  /// \returns The equivalent identifier in the "to" context. Note: It
315  /// returns nullptr only if the FromId was nullptr.
316  IdentifierInfo *Import(const IdentifierInfo *FromId);
317 
318  /// Import the given Objective-C selector from the "from"
319  /// context into the "to" context.
320  ///
321  /// \returns The equivalent selector in the "to" context, or the import
322  /// error.
323  llvm::Expected<Selector> Import_New(Selector FromSel);
324  // FIXME: Remove this version.
325  Selector Import(Selector FromSel);
326 
327  /// Import the given file ID from the "from" context into the
328  /// "to" context.
329  ///
330  /// \returns The equivalent file ID in the source manager of the "to"
331  /// context, or the import error.
332  llvm::Expected<FileID> Import_New(FileID);
333  // FIXME: Remove this version.
334  FileID Import(FileID);
335 
336  /// Import the given C++ constructor initializer from the "from"
337  /// context into the "to" context.
338  ///
339  /// \returns The equivalent initializer in the "to" context, or the import
340  /// error.
342  Import_New(CXXCtorInitializer *FromInit);
343  // FIXME: Remove this version.
344  CXXCtorInitializer *Import(CXXCtorInitializer *FromInit);
345 
346  /// Import the given CXXBaseSpecifier from the "from" context into
347  /// the "to" context.
348  ///
349  /// \returns The equivalent CXXBaseSpecifier in the source manager of the
350  /// "to" context, or the import error.
352  Import_New(const CXXBaseSpecifier *FromSpec);
353  // FIXME: Remove this version.
354  CXXBaseSpecifier *Import(const CXXBaseSpecifier *FromSpec);
355 
356  /// Import the definition of the given declaration, including all of
357  /// the declarations it contains.
358  LLVM_NODISCARD llvm::Error ImportDefinition_New(Decl *From);
359 
360  // FIXME: Compatibility function.
361  // Usages of this should be changed to ImportDefinition_New.
362  void ImportDefinition(Decl *From);
363 
364  /// Cope with a name conflict when importing a declaration into the
365  /// given context.
366  ///
367  /// This routine is invoked whenever there is a name conflict while
368  /// importing a declaration. The returned name will become the name of the
369  /// imported declaration. By default, the returned name is the same as the
370  /// original name, leaving the conflict unresolve such that name lookup
371  /// for this name is likely to find an ambiguity later.
372  ///
373  /// Subclasses may override this routine to resolve the conflict, e.g., by
374  /// renaming the declaration being imported.
375  ///
376  /// \param Name the name of the declaration being imported, which conflicts
377  /// with other declarations.
378  ///
379  /// \param DC the declaration context (in the "to" AST context) in which
380  /// the name is being imported.
381  ///
382  /// \param IDNS the identifier namespace in which the name will be found.
383  ///
384  /// \param Decls the set of declarations with the same name as the
385  /// declaration being imported.
386  ///
387  /// \param NumDecls the number of conflicting declarations in \p Decls.
388  ///
389  /// \returns the name that the newly-imported declaration should have.
390  virtual DeclarationName HandleNameConflict(DeclarationName Name,
391  DeclContext *DC,
392  unsigned IDNS,
393  NamedDecl **Decls,
394  unsigned NumDecls);
395 
396  /// Retrieve the context that AST nodes are being imported into.
397  ASTContext &getToContext() const { return ToContext; }
398 
399  /// Retrieve the context that AST nodes are being imported from.
400  ASTContext &getFromContext() const { return FromContext; }
401 
402  /// Retrieve the file manager that AST nodes are being imported into.
403  FileManager &getToFileManager() const { return ToFileManager; }
404 
405  /// Retrieve the file manager that AST nodes are being imported from.
406  FileManager &getFromFileManager() const { return FromFileManager; }
407 
408  /// Report a diagnostic in the "to" context.
409  DiagnosticBuilder ToDiag(SourceLocation Loc, unsigned DiagID);
410 
411  /// Report a diagnostic in the "from" context.
412  DiagnosticBuilder FromDiag(SourceLocation Loc, unsigned DiagID);
413 
414  /// Return the set of declarations that we know are not equivalent.
415  NonEquivalentDeclSet &getNonEquivalentDecls() { return NonEquivalentDecls; }
416 
417  /// Called for ObjCInterfaceDecl, ObjCProtocolDecl, and TagDecl.
418  /// Mark the Decl as complete, filling it in as much as possible.
419  ///
420  /// \param D A declaration in the "to" context.
421  virtual void CompleteDecl(Decl* D);
422 
423  /// Subclasses can override this function to observe all of the \c From ->
424  /// \c To declaration mappings as they are imported.
425  virtual Decl *Imported(Decl *From, Decl *To) { return To; }
426 
427  /// Store and assign the imported declaration to its counterpart.
428  Decl *MapImported(Decl *From, Decl *To);
429 
430  /// Called by StructuralEquivalenceContext. If a RecordDecl is
431  /// being compared to another RecordDecl as part of import, completing the
432  /// other RecordDecl may trigger importation of the first RecordDecl. This
433  /// happens especially for anonymous structs. If the original of the second
434  /// RecordDecl can be found, we can complete it without the need for
435  /// importation, eliminating this loop.
436  virtual Decl *GetOriginalDecl(Decl *To) { return nullptr; }
437 
438  /// Determine whether the given types are structurally
439  /// equivalent.
441  bool Complain = true);
442 
443  /// Determine the index of a field in its parent record.
444  /// F should be a field (or indirect field) declaration.
445  /// \returns The index of the field in its parent context (starting from 0).
446  /// On error `None` is returned (parent context is non-record).
447  static llvm::Optional<unsigned> getFieldIndex(Decl *F);
448 
449  };
450 
451 } // namespace clang
452 
453 #endif // LLVM_CLANG_AST_ASTIMPORTER_H
static char ID
Definition: ASTImporter.h:60
Smart pointer class that efficiently represents Objective-C method names.
A (possibly-)qualified type.
Definition: Type.h:638
Implements support for file system lookup, file system caching, and directory search management...
Definition: FileManager.h:121
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:87
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:87
Not supported node or case.
Definition: ASTImporter.h:55
virtual Decl * GetOriginalDecl(Decl *To)
Called by StructuralEquivalenceContext.
Definition: ASTImporter.h:436
FileManager & getFromFileManager() const
Retrieve the file manager that AST nodes are being imported from.
Definition: ASTImporter.h:406
One of these records is kept for each identifier that is lexed.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:155
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.
void log(raw_ostream &OS) const override
Definition: ASTImporter.cpp:98
llvm::DenseMap< const CXXBaseSpecifier *, CXXBaseSpecifier * > ImportedCXXBaseSpecifierMap
Definition: ASTImporter.h:88
ASTContext & getToContext() const
Retrieve the context that AST nodes are being imported into.
Definition: ASTImporter.h:397
A little helper class used to produce diagnostics.
Definition: Diagnostic.h:1043
This represents one expression.
Definition: Expr.h:106
Represents a C++ template name within the type system.
Definition: TemplateName.h:179
llvm::SmallVector< Decl *, 2 > getCanonicalForwardRedeclChain(Decl *D)
Defines the clang::IdentifierInfo, clang::IdentifierTable, and clang::Selector interfaces.
std::error_code convertToErrorCode() const override
ImportError(ErrorKind Error)
Definition: ASTImporter.h:64
Encodes a location in the source.
static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, QualType T1, QualType T2)
Determine structural equivalence of two types.
ImportError(const ImportError &Other)
Definition: ASTImporter.h:63
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:167
FileManager & getToFileManager() const
Retrieve the file manager that AST nodes are being imported into.
Definition: ASTImporter.h:403
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:425
Naming ambiguity (likely ODR violation).
Definition: ASTImporter.h:54
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:415
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
LLVM_NODISCARD llvm::Error importInto(ImportT &To, const ImportT &From)
Import the given object, returns the result.
Definition: ASTImporter.h:175
The name of a declaration.
Imports selected nodes from one AST context into another context, merging AST nodes where appropriate...
Definition: ASTImporter.h:83
Represents a C++ base or member initializer.
Definition: DeclCXX.h:2256
Decl * Import(const Decl *FromD)
Definition: ASTImporter.h:224
Represents a base class of a C++ class.
Definition: DeclCXX.h:192
Defines the clang::SourceLocation class and associated facilities.
ASTContext & getFromContext() const
Retrieve the context that AST nodes are being imported from.
Definition: ASTImporter.h:400
llvm::Expected< Decl * > Import_New(const Decl *FromD)
Definition: ASTImporter.h:219
std::string toString() const
Definition: ASTImporter.cpp:84
A trivial tuple used to represent a source range.
This represents a decl that may have a name.
Definition: Decl.h:249
Attr - This represents one attribute.
Definition: Attr.h:44
ErrorKind
Kind of error when importing an AST component.
Definition: ASTImporter.h:52