clang API Documentation
00001 //===--- ASTImporter.h - Importing ASTs from other Contexts -----*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines the ASTImporter class which imports AST nodes from one 00011 // context into another context. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 #ifndef LLVM_CLANG_AST_ASTIMPORTER_H 00015 #define LLVM_CLANG_AST_ASTIMPORTER_H 00016 00017 #include "clang/AST/DeclarationName.h" 00018 #include "clang/AST/Type.h" 00019 #include "clang/Basic/SourceLocation.h" 00020 #include "llvm/ADT/DenseMap.h" 00021 #include "llvm/ADT/DenseSet.h" 00022 #include "llvm/ADT/SmallVector.h" 00023 00024 namespace clang { 00025 class ASTContext; 00026 class Decl; 00027 class DeclContext; 00028 class DiagnosticsEngine; 00029 class Expr; 00030 class FileManager; 00031 class IdentifierInfo; 00032 class NestedNameSpecifier; 00033 class Stmt; 00034 class TypeSourceInfo; 00035 00036 /// \brief Imports selected nodes from one AST context into another context, 00037 /// merging AST nodes where appropriate. 00038 class ASTImporter { 00039 public: 00040 typedef llvm::DenseSet<std::pair<Decl *, Decl *> > NonEquivalentDeclSet; 00041 00042 private: 00043 /// \brief The contexts we're importing to and from. 00044 ASTContext &ToContext, &FromContext; 00045 00046 /// \brief The file managers we're importing to and from. 00047 FileManager &ToFileManager, &FromFileManager; 00048 00049 /// \brief Whether to perform a minimal import. 00050 bool Minimal; 00051 00052 /// \brief Whether the last diagnostic came from the "from" context. 00053 bool LastDiagFromFrom; 00054 00055 /// \brief Mapping from the already-imported types in the "from" context 00056 /// to the corresponding types in the "to" context. 00057 llvm::DenseMap<const Type *, const Type *> ImportedTypes; 00058 00059 /// \brief Mapping from the already-imported declarations in the "from" 00060 /// context to the corresponding declarations in the "to" context. 00061 llvm::DenseMap<Decl *, Decl *> ImportedDecls; 00062 00063 /// \brief Mapping from the already-imported statements in the "from" 00064 /// context to the corresponding statements in the "to" context. 00065 llvm::DenseMap<Stmt *, Stmt *> ImportedStmts; 00066 00067 /// \brief Mapping from the already-imported FileIDs in the "from" source 00068 /// manager to the corresponding FileIDs in the "to" source manager. 00069 llvm::DenseMap<FileID, FileID> ImportedFileIDs; 00070 00071 /// \brief Imported, anonymous tag declarations that are missing their 00072 /// corresponding typedefs. 00073 SmallVector<TagDecl *, 4> AnonTagsWithPendingTypedefs; 00074 00075 /// \brief Declaration (from, to) pairs that are known not to be equivalent 00076 /// (which we have already complained about). 00077 NonEquivalentDeclSet NonEquivalentDecls; 00078 00079 public: 00080 /// \brief Create a new AST importer. 00081 /// 00082 /// \param ToContext The context we'll be importing into. 00083 /// 00084 /// \param ToFileManager The file manager we'll be importing into. 00085 /// 00086 /// \param FromContext The context we'll be importing from. 00087 /// 00088 /// \param FromFileManager The file manager we'll be importing into. 00089 /// 00090 /// \param MinimalImport If true, the importer will attempt to import 00091 /// as little as it can, e.g., by importing declarations as forward 00092 /// declarations that can be completed at a later point. 00093 ASTImporter(ASTContext &ToContext, FileManager &ToFileManager, 00094 ASTContext &FromContext, FileManager &FromFileManager, 00095 bool MinimalImport); 00096 00097 virtual ~ASTImporter(); 00098 00099 /// \brief Whether the importer will perform a minimal import, creating 00100 /// to-be-completed forward declarations when possible. 00101 bool isMinimalImport() const { return Minimal; } 00102 00103 /// \brief Import the given type from the "from" context into the "to" 00104 /// context. 00105 /// 00106 /// \returns the equivalent type in the "to" context, or a NULL type if 00107 /// an error occurred. 00108 QualType Import(QualType FromT); 00109 00110 /// \brief Import the given type source information from the 00111 /// "from" context into the "to" context. 00112 /// 00113 /// \returns the equivalent type source information in the "to" 00114 /// context, or NULL if an error occurred. 00115 TypeSourceInfo *Import(TypeSourceInfo *FromTSI); 00116 00117 /// \brief Import the given declaration from the "from" context into the 00118 /// "to" context. 00119 /// 00120 /// \returns the equivalent declaration in the "to" context, or a NULL type 00121 /// if an error occurred. 00122 Decl *Import(Decl *FromD); 00123 00124 /// \brief Import the given declaration context from the "from" 00125 /// AST context into the "to" AST context. 00126 /// 00127 /// \returns the equivalent declaration context in the "to" 00128 /// context, or a NULL type if an error occurred. 00129 DeclContext *ImportContext(DeclContext *FromDC); 00130 00131 /// \brief Import the given expression from the "from" context into the 00132 /// "to" context. 00133 /// 00134 /// \returns the equivalent expression in the "to" context, or NULL if 00135 /// an error occurred. 00136 Expr *Import(Expr *FromE); 00137 00138 /// \brief Import the given statement from the "from" context into the 00139 /// "to" context. 00140 /// 00141 /// \returns the equivalent statement in the "to" context, or NULL if 00142 /// an error occurred. 00143 Stmt *Import(Stmt *FromS); 00144 00145 /// \brief Import the given nested-name-specifier from the "from" 00146 /// context into the "to" context. 00147 /// 00148 /// \returns the equivalent nested-name-specifier in the "to" 00149 /// context, or NULL if an error occurred. 00150 NestedNameSpecifier *Import(NestedNameSpecifier *FromNNS); 00151 00152 /// \brief Import the given nested-name-specifier from the "from" 00153 /// context into the "to" context. 00154 /// 00155 /// \returns the equivalent nested-name-specifier in the "to" 00156 /// context. 00157 NestedNameSpecifierLoc Import(NestedNameSpecifierLoc FromNNS); 00158 00159 /// \brief Import the goven template name from the "from" context into the 00160 /// "to" context. 00161 TemplateName Import(TemplateName From); 00162 00163 /// \brief Import the given source location from the "from" context into 00164 /// the "to" context. 00165 /// 00166 /// \returns the equivalent source location in the "to" context, or an 00167 /// invalid source location if an error occurred. 00168 SourceLocation Import(SourceLocation FromLoc); 00169 00170 /// \brief Import the given source range from the "from" context into 00171 /// the "to" context. 00172 /// 00173 /// \returns the equivalent source range in the "to" context, or an 00174 /// invalid source location if an error occurred. 00175 SourceRange Import(SourceRange FromRange); 00176 00177 /// \brief Import the given declaration name from the "from" 00178 /// context into the "to" context. 00179 /// 00180 /// \returns the equivalent declaration name in the "to" context, 00181 /// or an empty declaration name if an error occurred. 00182 DeclarationName Import(DeclarationName FromName); 00183 00184 /// \brief Import the given identifier from the "from" context 00185 /// into the "to" context. 00186 /// 00187 /// \returns the equivalent identifier in the "to" context. 00188 IdentifierInfo *Import(const IdentifierInfo *FromId); 00189 00190 /// \brief Import the given Objective-C selector from the "from" 00191 /// context into the "to" context. 00192 /// 00193 /// \returns the equivalent selector in the "to" context. 00194 Selector Import(Selector FromSel); 00195 00196 /// \brief Import the given file ID from the "from" context into the 00197 /// "to" context. 00198 /// 00199 /// \returns the equivalent file ID in the source manager of the "to" 00200 /// context. 00201 FileID Import(FileID); 00202 00203 /// \brief Import the definition of the given declaration, including all of 00204 /// the declarations it contains. 00205 /// 00206 /// This routine is intended to be used 00207 void ImportDefinition(Decl *From); 00208 00209 /// \brief Cope with a name conflict when importing a declaration into the 00210 /// given context. 00211 /// 00212 /// This routine is invoked whenever there is a name conflict while 00213 /// importing a declaration. The returned name will become the name of the 00214 /// imported declaration. By default, the returned name is the same as the 00215 /// original name, leaving the conflict unresolve such that name lookup 00216 /// for this name is likely to find an ambiguity later. 00217 /// 00218 /// Subclasses may override this routine to resolve the conflict, e.g., by 00219 /// renaming the declaration being imported. 00220 /// 00221 /// \param Name the name of the declaration being imported, which conflicts 00222 /// with other declarations. 00223 /// 00224 /// \param DC the declaration context (in the "to" AST context) in which 00225 /// the name is being imported. 00226 /// 00227 /// \param IDNS the identifier namespace in which the name will be found. 00228 /// 00229 /// \param Decls the set of declarations with the same name as the 00230 /// declaration being imported. 00231 /// 00232 /// \param NumDecls the number of conflicting declarations in \p Decls. 00233 /// 00234 /// \returns the name that the newly-imported declaration should have. 00235 virtual DeclarationName HandleNameConflict(DeclarationName Name, 00236 DeclContext *DC, 00237 unsigned IDNS, 00238 NamedDecl **Decls, 00239 unsigned NumDecls); 00240 00241 /// \brief Retrieve the context that AST nodes are being imported into. 00242 ASTContext &getToContext() const { return ToContext; } 00243 00244 /// \brief Retrieve the context that AST nodes are being imported from. 00245 ASTContext &getFromContext() const { return FromContext; } 00246 00247 /// \brief Retrieve the file manager that AST nodes are being imported into. 00248 FileManager &getToFileManager() const { return ToFileManager; } 00249 00250 /// \brief Retrieve the file manager that AST nodes are being imported from. 00251 FileManager &getFromFileManager() const { return FromFileManager; } 00252 00253 /// \brief Report a diagnostic in the "to" context. 00254 DiagnosticBuilder ToDiag(SourceLocation Loc, unsigned DiagID); 00255 00256 /// \brief Report a diagnostic in the "from" context. 00257 DiagnosticBuilder FromDiag(SourceLocation Loc, unsigned DiagID); 00258 00259 /// \brief Return the set of declarations that we know are not equivalent. 00260 NonEquivalentDeclSet &getNonEquivalentDecls() { return NonEquivalentDecls; } 00261 00262 /// \brief Called for ObjCInterfaceDecl, ObjCProtocolDecl, and TagDecl. 00263 /// Mark the Decl as complete, filling it in as much as possible. 00264 /// 00265 /// \param D A declaration in the "to" context. 00266 virtual void CompleteDecl(Decl* D); 00267 00268 /// \brief Note that we have imported the "from" declaration by mapping it 00269 /// to the (potentially-newly-created) "to" declaration. 00270 /// 00271 /// Subclasses can override this function to observe all of the \c From -> 00272 /// \c To declaration mappings as they are imported. 00273 virtual Decl *Imported(Decl *From, Decl *To); 00274 00275 /// \brief Called by StructuralEquivalenceContext. If a RecordDecl is 00276 /// being compared to another RecordDecl as part of import, completing the 00277 /// other RecordDecl may trigger importation of the first RecordDecl. This 00278 /// happens especially for anonymous structs. If the original of the second 00279 /// RecordDecl can be found, we can complete it without the need for 00280 /// importation, eliminating this loop. 00281 virtual Decl *GetOriginalDecl(Decl *To) { return nullptr; } 00282 00283 /// \brief Determine whether the given types are structurally 00284 /// equivalent. 00285 bool IsStructurallyEquivalent(QualType From, QualType To, 00286 bool Complain = true); 00287 }; 00288 } 00289 00290 #endif // LLVM_CLANG_AST_ASTIMPORTER_H