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 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: 41 typedef llvm::DenseSet<std::pair<Decl *, Decl *> > NonEquivalentDeclSet; 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. isMinimalImport()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. 116 TypeSourceInfo *Import(TypeSourceInfo *FromTSI); 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. 128 Decl *GetAlreadyImportedOrNull(Decl *FromD); 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. 135 DeclContext *ImportContext(DeclContext *FromDC); 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. 156 NestedNameSpecifier *Import(NestedNameSpecifier *FromNNS); 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. 163 NestedNameSpecifierLoc Import(NestedNameSpecifierLoc FromNNS); 164 165 /// \brief Import the goven template name from the "from" context into the 166 /// "to" context. 167 TemplateName Import(TemplateName From); 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. 174 SourceLocation Import(SourceLocation FromLoc); 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. 188 DeclarationName Import(DeclarationName FromName); 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. 207 FileID Import(FileID); 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. 213 CXXCtorInitializer *Import(CXXCtorInitializer *FromInit); 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. 249 virtual DeclarationName HandleNameConflict(DeclarationName Name, 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. getToContext()256 ASTContext &getToContext() const { return ToContext; } 257 258 /// \brief Retrieve the context that AST nodes are being imported from. getFromContext()259 ASTContext &getFromContext() const { return FromContext; } 260 261 /// \brief Retrieve the file manager that AST nodes are being imported into. getToFileManager()262 FileManager &getToFileManager() const { return ToFileManager; } 263 264 /// \brief Retrieve the file manager that AST nodes are being imported from. getFromFileManager()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. getNonEquivalentDecls()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. GetOriginalDecl(Decl * To)295 virtual Decl *GetOriginalDecl(Decl *To) { return nullptr; } 296 297 /// \brief Determine whether the given types are structurally 298 /// equivalent. 299 bool IsStructurallyEquivalent(QualType From, QualType To, 300 bool Complain = true); 301 }; 302 } 303 304 #endif // LLVM_CLANG_AST_ASTIMPORTER_H 305