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