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