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 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: 40 typedef llvm::DenseSet<std::pair<Decl *, Decl *> > NonEquivalentDeclSet; 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. isMinimalImport()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. 115 TypeSourceInfo *Import(TypeSourceInfo *FromTSI); 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 Import the given declaration context from the "from" 125 /// AST context into the "to" AST context. 126 /// 127 /// \returns the equivalent declaration context in the "to" 128 /// context, or a NULL type if an error occurred. 129 DeclContext *ImportContext(DeclContext *FromDC); 130 131 /// \brief Import the given expression from the "from" context into the 132 /// "to" context. 133 /// 134 /// \returns the equivalent expression in the "to" context, or NULL if 135 /// an error occurred. 136 Expr *Import(Expr *FromE); 137 138 /// \brief Import the given statement from the "from" context into the 139 /// "to" context. 140 /// 141 /// \returns the equivalent statement in the "to" context, or NULL if 142 /// an error occurred. 143 Stmt *Import(Stmt *FromS); 144 145 /// \brief Import the given nested-name-specifier from the "from" 146 /// context into the "to" context. 147 /// 148 /// \returns the equivalent nested-name-specifier in the "to" 149 /// context, or NULL if an error occurred. 150 NestedNameSpecifier *Import(NestedNameSpecifier *FromNNS); 151 152 /// \brief Import the given nested-name-specifier from the "from" 153 /// context into the "to" context. 154 /// 155 /// \returns the equivalent nested-name-specifier in the "to" 156 /// context. 157 NestedNameSpecifierLoc Import(NestedNameSpecifierLoc FromNNS); 158 159 /// \brief Import the goven template name from the "from" context into the 160 /// "to" context. 161 TemplateName Import(TemplateName From); 162 163 /// \brief Import the given source location from the "from" context into 164 /// the "to" context. 165 /// 166 /// \returns the equivalent source location in the "to" context, or an 167 /// invalid source location if an error occurred. 168 SourceLocation Import(SourceLocation FromLoc); 169 170 /// \brief Import the given source range from the "from" context into 171 /// the "to" context. 172 /// 173 /// \returns the equivalent source range in the "to" context, or an 174 /// invalid source location if an error occurred. 175 SourceRange Import(SourceRange FromRange); 176 177 /// \brief Import the given declaration name from the "from" 178 /// context into the "to" context. 179 /// 180 /// \returns the equivalent declaration name in the "to" context, 181 /// or an empty declaration name if an error occurred. 182 DeclarationName Import(DeclarationName FromName); 183 184 /// \brief Import the given identifier from the "from" context 185 /// into the "to" context. 186 /// 187 /// \returns the equivalent identifier in the "to" context. 188 IdentifierInfo *Import(const IdentifierInfo *FromId); 189 190 /// \brief Import the given Objective-C selector from the "from" 191 /// context into the "to" context. 192 /// 193 /// \returns the equivalent selector in the "to" context. 194 Selector Import(Selector FromSel); 195 196 /// \brief Import the given file ID from the "from" context into the 197 /// "to" context. 198 /// 199 /// \returns the equivalent file ID in the source manager of the "to" 200 /// context. 201 FileID Import(FileID); 202 203 /// \brief Import the definition of the given declaration, including all of 204 /// the declarations it contains. 205 /// 206 /// This routine is intended to be used 207 void ImportDefinition(Decl *From); 208 209 /// \brief Cope with a name conflict when importing a declaration into the 210 /// given context. 211 /// 212 /// This routine is invoked whenever there is a name conflict while 213 /// importing a declaration. The returned name will become the name of the 214 /// imported declaration. By default, the returned name is the same as the 215 /// original name, leaving the conflict unresolve such that name lookup 216 /// for this name is likely to find an ambiguity later. 217 /// 218 /// Subclasses may override this routine to resolve the conflict, e.g., by 219 /// renaming the declaration being imported. 220 /// 221 /// \param Name the name of the declaration being imported, which conflicts 222 /// with other declarations. 223 /// 224 /// \param DC the declaration context (in the "to" AST context) in which 225 /// the name is being imported. 226 /// 227 /// \param IDNS the identifier namespace in which the name will be found. 228 /// 229 /// \param Decls the set of declarations with the same name as the 230 /// declaration being imported. 231 /// 232 /// \param NumDecls the number of conflicting declarations in \p Decls. 233 /// 234 /// \returns the name that the newly-imported declaration should have. 235 virtual DeclarationName HandleNameConflict(DeclarationName Name, 236 DeclContext *DC, 237 unsigned IDNS, 238 NamedDecl **Decls, 239 unsigned NumDecls); 240 241 /// \brief Retrieve the context that AST nodes are being imported into. getToContext()242 ASTContext &getToContext() const { return ToContext; } 243 244 /// \brief Retrieve the context that AST nodes are being imported from. getFromContext()245 ASTContext &getFromContext() const { return FromContext; } 246 247 /// \brief Retrieve the file manager that AST nodes are being imported into. getToFileManager()248 FileManager &getToFileManager() const { return ToFileManager; } 249 250 /// \brief Retrieve the file manager that AST nodes are being imported from. getFromFileManager()251 FileManager &getFromFileManager() const { return FromFileManager; } 252 253 /// \brief Report a diagnostic in the "to" context. 254 DiagnosticBuilder ToDiag(SourceLocation Loc, unsigned DiagID); 255 256 /// \brief Report a diagnostic in the "from" context. 257 DiagnosticBuilder FromDiag(SourceLocation Loc, unsigned DiagID); 258 259 /// \brief Return the set of declarations that we know are not equivalent. getNonEquivalentDecls()260 NonEquivalentDeclSet &getNonEquivalentDecls() { return NonEquivalentDecls; } 261 262 /// \brief Called for ObjCInterfaceDecl, ObjCProtocolDecl, and TagDecl. 263 /// Mark the Decl as complete, filling it in as much as possible. 264 /// 265 /// \param D A declaration in the "to" context. 266 virtual void CompleteDecl(Decl* D); 267 268 /// \brief Note that we have imported the "from" declaration by mapping it 269 /// to the (potentially-newly-created) "to" declaration. 270 /// 271 /// Subclasses can override this function to observe all of the \c From -> 272 /// \c To declaration mappings as they are imported. 273 virtual Decl *Imported(Decl *From, Decl *To); 274 275 /// \brief Called by StructuralEquivalenceContext. If a RecordDecl is 276 /// being compared to another RecordDecl as part of import, completing the 277 /// other RecordDecl may trigger importation of the first RecordDecl. This 278 /// happens especially for anonymous structs. If the original of the second 279 /// RecordDecl can be found, we can complete it without the need for 280 /// importation, eliminating this loop. GetOriginalDecl(Decl * To)281 virtual Decl *GetOriginalDecl(Decl *To) { return nullptr; } 282 283 /// \brief Determine whether the given types are structurally 284 /// equivalent. 285 bool IsStructurallyEquivalent(QualType From, QualType To, 286 bool Complain = true); 287 }; 288 } 289 290 #endif // LLVM_CLANG_AST_ASTIMPORTER_H 291