clang  3.9.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 #ifndef LLVM_CLANG_AST_ASTIMPORTER_H
15 #define LLVM_CLANG_AST_ASTIMPORTER_H
16 
18 #include "clang/AST/Type.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/DenseSet.h"
22 #include "llvm/ADT/SmallVector.h"
23 
24 namespace clang {
25  class ASTContext;
26  class CXXCtorInitializer;
27  class Decl;
28  class DeclContext;
29  class DiagnosticsEngine;
30  class Expr;
31  class FileManager;
32  class IdentifierInfo;
33  class NestedNameSpecifier;
34  class Stmt;
35  class TypeSourceInfo;
36 
37  /// \brief Imports selected nodes from one AST context into another context,
38  /// merging AST nodes where appropriate.
39  class ASTImporter {
40  public:
42 
43  private:
44  /// \brief The contexts we're importing to and from.
45  ASTContext &ToContext, &FromContext;
46 
47  /// \brief The file managers we're importing to and from.
48  FileManager &ToFileManager, &FromFileManager;
49 
50  /// \brief Whether to perform a minimal import.
51  bool Minimal;
52 
53  /// \brief Whether the last diagnostic came from the "from" context.
54  bool LastDiagFromFrom;
55 
56  /// \brief Mapping from the already-imported types in the "from" context
57  /// to the corresponding types in the "to" context.
58  llvm::DenseMap<const Type *, const Type *> ImportedTypes;
59 
60  /// \brief Mapping from the already-imported declarations in the "from"
61  /// context to the corresponding declarations in the "to" context.
62  llvm::DenseMap<Decl *, Decl *> ImportedDecls;
63 
64  /// \brief Mapping from the already-imported statements in the "from"
65  /// context to the corresponding statements in the "to" context.
66  llvm::DenseMap<Stmt *, Stmt *> ImportedStmts;
67 
68  /// \brief Mapping from the already-imported FileIDs in the "from" source
69  /// manager to the corresponding FileIDs in the "to" source manager.
70  llvm::DenseMap<FileID, FileID> ImportedFileIDs;
71 
72  /// \brief Imported, anonymous tag declarations that are missing their
73  /// corresponding typedefs.
74  SmallVector<TagDecl *, 4> AnonTagsWithPendingTypedefs;
75 
76  /// \brief Declaration (from, to) pairs that are known not to be equivalent
77  /// (which we have already complained about).
78  NonEquivalentDeclSet NonEquivalentDecls;
79 
80  public:
81  /// \brief Create a new AST importer.
82  ///
83  /// \param ToContext The context we'll be importing into.
84  ///
85  /// \param ToFileManager The file manager we'll be importing into.
86  ///
87  /// \param FromContext The context we'll be importing from.
88  ///
89  /// \param FromFileManager The file manager we'll be importing into.
90  ///
91  /// \param MinimalImport If true, the importer will attempt to import
92  /// as little as it can, e.g., by importing declarations as forward
93  /// declarations that can be completed at a later point.
94  ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
95  ASTContext &FromContext, FileManager &FromFileManager,
96  bool MinimalImport);
97 
98  virtual ~ASTImporter();
99 
100  /// \brief Whether the importer will perform a minimal import, creating
101  /// to-be-completed forward declarations when possible.
102  bool isMinimalImport() const { return Minimal; }
103 
104  /// \brief Import the given type from the "from" context into the "to"
105  /// context.
106  ///
107  /// \returns the equivalent type in the "to" context, or a NULL type if
108  /// an error occurred.
109  QualType Import(QualType FromT);
110 
111  /// \brief Import the given type source information from the
112  /// "from" context into the "to" context.
113  ///
114  /// \returns the equivalent type source information in the "to"
115  /// context, or NULL if an error occurred.
117 
118  /// \brief Import the given declaration from the "from" context into the
119  /// "to" context.
120  ///
121  /// \returns the equivalent declaration in the "to" context, or a NULL type
122  /// if an error occurred.
123  Decl *Import(Decl *FromD);
124 
125  /// \brief Return the copy of the given declaration in the "to" context if
126  /// it has already been imported from the "from" context. Otherwise return
127  /// NULL.
129 
130  /// \brief Import the given declaration context from the "from"
131  /// AST context into the "to" AST context.
132  ///
133  /// \returns the equivalent declaration context in the "to"
134  /// context, or a NULL type if an error occurred.
136 
137  /// \brief Import the given expression from the "from" context into the
138  /// "to" context.
139  ///
140  /// \returns the equivalent expression in the "to" context, or NULL if
141  /// an error occurred.
142  Expr *Import(Expr *FromE);
143 
144  /// \brief Import the given statement from the "from" context into the
145  /// "to" context.
146  ///
147  /// \returns the equivalent statement in the "to" context, or NULL if
148  /// an error occurred.
149  Stmt *Import(Stmt *FromS);
150 
151  /// \brief Import the given nested-name-specifier from the "from"
152  /// context into the "to" context.
153  ///
154  /// \returns the equivalent nested-name-specifier in the "to"
155  /// context, or NULL if an error occurred.
157 
158  /// \brief Import the given nested-name-specifier from the "from"
159  /// context into the "to" context.
160  ///
161  /// \returns the equivalent nested-name-specifier in the "to"
162  /// context.
164 
165  /// \brief Import the goven template name from the "from" context into the
166  /// "to" context.
168 
169  /// \brief Import the given source location from the "from" context into
170  /// the "to" context.
171  ///
172  /// \returns the equivalent source location in the "to" context, or an
173  /// invalid source location if an error occurred.
175 
176  /// \brief Import the given source range from the "from" context into
177  /// the "to" context.
178  ///
179  /// \returns the equivalent source range in the "to" context, or an
180  /// invalid source location if an error occurred.
181  SourceRange Import(SourceRange FromRange);
182 
183  /// \brief Import the given declaration name from the "from"
184  /// context into the "to" context.
185  ///
186  /// \returns the equivalent declaration name in the "to" context,
187  /// or an empty declaration name if an error occurred.
189 
190  /// \brief Import the given identifier from the "from" context
191  /// into the "to" context.
192  ///
193  /// \returns the equivalent identifier in the "to" context.
194  IdentifierInfo *Import(const IdentifierInfo *FromId);
195 
196  /// \brief Import the given Objective-C selector from the "from"
197  /// context into the "to" context.
198  ///
199  /// \returns the equivalent selector in the "to" context.
200  Selector Import(Selector FromSel);
201 
202  /// \brief Import the given file ID from the "from" context into the
203  /// "to" context.
204  ///
205  /// \returns the equivalent file ID in the source manager of the "to"
206  /// context.
208 
209  /// \brief Import the given C++ constructor initializer from the "from"
210  /// context into the "to" context.
211  ///
212  /// \returns the equivalent initializer in the "to" context.
214 
215 
216 
217  /// \brief Import the definition of the given declaration, including all of
218  /// the declarations it contains.
219  ///
220  /// This routine is intended to be used
221  void ImportDefinition(Decl *From);
222 
223  /// \brief Cope with a name conflict when importing a declaration into the
224  /// given context.
225  ///
226  /// This routine is invoked whenever there is a name conflict while
227  /// importing a declaration. The returned name will become the name of the
228  /// imported declaration. By default, the returned name is the same as the
229  /// original name, leaving the conflict unresolve such that name lookup
230  /// for this name is likely to find an ambiguity later.
231  ///
232  /// Subclasses may override this routine to resolve the conflict, e.g., by
233  /// renaming the declaration being imported.
234  ///
235  /// \param Name the name of the declaration being imported, which conflicts
236  /// with other declarations.
237  ///
238  /// \param DC the declaration context (in the "to" AST context) in which
239  /// the name is being imported.
240  ///
241  /// \param IDNS the identifier namespace in which the name will be found.
242  ///
243  /// \param Decls the set of declarations with the same name as the
244  /// declaration being imported.
245  ///
246  /// \param NumDecls the number of conflicting declarations in \p Decls.
247  ///
248  /// \returns the name that the newly-imported declaration should have.
250  DeclContext *DC,
251  unsigned IDNS,
252  NamedDecl **Decls,
253  unsigned NumDecls);
254 
255  /// \brief Retrieve the context that AST nodes are being imported into.
256  ASTContext &getToContext() const { return ToContext; }
257 
258  /// \brief Retrieve the context that AST nodes are being imported from.
259  ASTContext &getFromContext() const { return FromContext; }
260 
261  /// \brief Retrieve the file manager that AST nodes are being imported into.
262  FileManager &getToFileManager() const { return ToFileManager; }
263 
264  /// \brief Retrieve the file manager that AST nodes are being imported from.
265  FileManager &getFromFileManager() const { return FromFileManager; }
266 
267  /// \brief Report a diagnostic in the "to" context.
268  DiagnosticBuilder ToDiag(SourceLocation Loc, unsigned DiagID);
269 
270  /// \brief Report a diagnostic in the "from" context.
271  DiagnosticBuilder FromDiag(SourceLocation Loc, unsigned DiagID);
272 
273  /// \brief Return the set of declarations that we know are not equivalent.
274  NonEquivalentDeclSet &getNonEquivalentDecls() { return NonEquivalentDecls; }
275 
276  /// \brief Called for ObjCInterfaceDecl, ObjCProtocolDecl, and TagDecl.
277  /// Mark the Decl as complete, filling it in as much as possible.
278  ///
279  /// \param D A declaration in the "to" context.
280  virtual void CompleteDecl(Decl* D);
281 
282  /// \brief Note that we have imported the "from" declaration by mapping it
283  /// to the (potentially-newly-created) "to" declaration.
284  ///
285  /// Subclasses can override this function to observe all of the \c From ->
286  /// \c To declaration mappings as they are imported.
287  virtual Decl *Imported(Decl *From, Decl *To);
288 
289  /// \brief Called by StructuralEquivalenceContext. If a RecordDecl is
290  /// being compared to another RecordDecl as part of import, completing the
291  /// other RecordDecl may trigger importation of the first RecordDecl. This
292  /// happens especially for anonymous structs. If the original of the second
293  /// RecordDecl can be found, we can complete it without the need for
294  /// importation, eliminating this loop.
295  virtual Decl *GetOriginalDecl(Decl *To) { return nullptr; }
296 
297  /// \brief Determine whether the given types are structurally
298  /// equivalent.
300  bool Complain = true);
301  };
302 }
303 
304 #endif // LLVM_CLANG_AST_ASTIMPORTER_H
bool isMinimalImport() const
Whether the importer will perform a minimal import, creating to-be-completed forward declarations whe...
Definition: ASTImporter.h:102
Smart pointer class that efficiently represents Objective-C method names.
A (possibly-)qualified type.
Definition: Type.h:598
Implements support for file system lookup, file system caching, and directory search management...
Definition: FileManager.h:117
C Language Family Type Representation.
A container of type source information.
Definition: Decl.h:62
virtual Decl * GetOriginalDecl(Decl *To)
Called by StructuralEquivalenceContext.
Definition: ASTImporter.h:295
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.
class LLVM_ALIGNAS(8) DependentTemplateSpecializationType const IdentifierInfo * Name
Represents a template specialization type whose template cannot be resolved, e.g. ...
Definition: Type.h:4549
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:92
A C++ nested-name-specifier augmented with source location information.
llvm::DenseSet< std::pair< Decl *, Decl * > > NonEquivalentDeclSet
Definition: ASTImporter.h:41
bool IsStructurallyEquivalent(QualType From, QualType To, bool Complain=true)
Determine whether the given types are structurally equivalent.
ASTImporter(ASTContext &ToContext, FileManager &ToFileManager, ASTContext &FromContext, FileManager &FromFileManager, bool MinimalImport)
Create a new AST importer.
A little helper class used to produce diagnostics.
Definition: Diagnostic.h:873
friend class ASTContext
Definition: Type.h:4178
Expr - This represents one expression.
Definition: Expr.h:105
Represents a C++ template name within the type system.
Definition: TemplateName.h:176
Decl * GetAlreadyImportedOrNull(Decl *FromD)
Return the copy of the given declaration in the "to" context if it has already been imported from the...
ASTContext & getFromContext() const
Retrieve the context that AST nodes are being imported from.
Definition: ASTImporter.h:259
Encodes a location in the source.
Represents a C++ nested name specifier, such as "\::std::vector<int>::".
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:274
virtual void CompleteDecl(Decl *D)
Called for ObjCInterfaceDecl, ObjCProtocolDecl, and TagDecl.
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1135
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:39
Represents a C++ base or member initializer.
Definition: DeclCXX.h:1922
FileManager & getToFileManager() const
Retrieve the file manager that AST nodes are being imported into.
Definition: ASTImporter.h:262
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 & getToContext() const
Retrieve the context that AST nodes are being imported into.
Definition: ASTImporter.h:256
FileManager & getFromFileManager() const
Retrieve the file manager that AST nodes are being imported from.
Definition: ASTImporter.h:265
A trivial tuple used to represent a source range.
NamedDecl - This represents a decl with a name.
Definition: Decl.h:213
DeclContext * ImportContext(DeclContext *FromDC)
Import the given declaration context from the "from" AST context into the "to" AST context...