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