1203955Srdivacky//===--- ASTImporter.h - Importing ASTs from other Contexts -----*- C++ -*-===// 2203955Srdivacky// 3203955Srdivacky// The LLVM Compiler Infrastructure 4203955Srdivacky// 5203955Srdivacky// This file is distributed under the University of Illinois Open Source 6203955Srdivacky// License. See LICENSE.TXT for details. 7203955Srdivacky// 8203955Srdivacky//===----------------------------------------------------------------------===// 9203955Srdivacky// 10203955Srdivacky// This file defines the ASTImporter class which imports AST nodes from one 11203955Srdivacky// context into another context. 12203955Srdivacky// 13203955Srdivacky//===----------------------------------------------------------------------===// 14203955Srdivacky#ifndef LLVM_CLANG_AST_ASTIMPORTER_H 15203955Srdivacky#define LLVM_CLANG_AST_ASTIMPORTER_H 16203955Srdivacky 17212904Sdim#include "clang/AST/DeclarationName.h" 18203955Srdivacky#include "clang/AST/Type.h" 19203955Srdivacky#include "clang/Basic/SourceLocation.h" 20203955Srdivacky#include "llvm/ADT/DenseMap.h" 21203955Srdivacky#include "llvm/ADT/DenseSet.h" 22203955Srdivacky#include "llvm/ADT/SmallVector.h" 23203955Srdivacky 24203955Srdivackynamespace clang { 25203955Srdivacky class ASTContext; 26203955Srdivacky class Decl; 27203955Srdivacky class DeclContext; 28226633Sdim class DiagnosticsEngine; 29203955Srdivacky class Expr; 30203955Srdivacky class FileManager; 31203955Srdivacky class IdentifierInfo; 32203955Srdivacky class NestedNameSpecifier; 33203955Srdivacky class Stmt; 34203955Srdivacky class TypeSourceInfo; 35203955Srdivacky 36203955Srdivacky /// \brief Imports selected nodes from one AST context into another context, 37203955Srdivacky /// merging AST nodes where appropriate. 38203955Srdivacky class ASTImporter { 39203955Srdivacky public: 40203955Srdivacky typedef llvm::DenseSet<std::pair<Decl *, Decl *> > NonEquivalentDeclSet; 41203955Srdivacky 42203955Srdivacky private: 43203955Srdivacky /// \brief The contexts we're importing to and from. 44203955Srdivacky ASTContext &ToContext, &FromContext; 45203955Srdivacky 46203955Srdivacky /// \brief The file managers we're importing to and from. 47203955Srdivacky FileManager &ToFileManager, &FromFileManager; 48218893Sdim 49218893Sdim /// \brief Whether to perform a minimal import. 50218893Sdim bool Minimal; 51249423Sdim 52249423Sdim /// \brief Whether the last diagnostic came from the "from" context. 53249423Sdim bool LastDiagFromFrom; 54203955Srdivacky 55203955Srdivacky /// \brief Mapping from the already-imported types in the "from" context 56203955Srdivacky /// to the corresponding types in the "to" context. 57218893Sdim llvm::DenseMap<const Type *, const Type *> ImportedTypes; 58203955Srdivacky 59203955Srdivacky /// \brief Mapping from the already-imported declarations in the "from" 60203955Srdivacky /// context to the corresponding declarations in the "to" context. 61203955Srdivacky llvm::DenseMap<Decl *, Decl *> ImportedDecls; 62203955Srdivacky 63203955Srdivacky /// \brief Mapping from the already-imported statements in the "from" 64203955Srdivacky /// context to the corresponding statements in the "to" context. 65203955Srdivacky llvm::DenseMap<Stmt *, Stmt *> ImportedStmts; 66203955Srdivacky 67203955Srdivacky /// \brief Mapping from the already-imported FileIDs in the "from" source 68203955Srdivacky /// manager to the corresponding FileIDs in the "to" source manager. 69218893Sdim llvm::DenseMap<FileID, FileID> ImportedFileIDs; 70203955Srdivacky 71203955Srdivacky /// \brief Imported, anonymous tag declarations that are missing their 72203955Srdivacky /// corresponding typedefs. 73226633Sdim SmallVector<TagDecl *, 4> AnonTagsWithPendingTypedefs; 74203955Srdivacky 75203955Srdivacky /// \brief Declaration (from, to) pairs that are known not to be equivalent 76203955Srdivacky /// (which we have already complained about). 77203955Srdivacky NonEquivalentDeclSet NonEquivalentDecls; 78203955Srdivacky 79203955Srdivacky public: 80218893Sdim /// \brief Create a new AST importer. 81218893Sdim /// 82218893Sdim /// \param ToContext The context we'll be importing into. 83218893Sdim /// 84218893Sdim /// \param ToFileManager The file manager we'll be importing into. 85218893Sdim /// 86218893Sdim /// \param FromContext The context we'll be importing from. 87218893Sdim /// 88218893Sdim /// \param FromFileManager The file manager we'll be importing into. 89218893Sdim /// 90218893Sdim /// \param MinimalImport If true, the importer will attempt to import 91218893Sdim /// as little as it can, e.g., by importing declarations as forward 92218893Sdim /// declarations that can be completed at a later point. 93218893Sdim ASTImporter(ASTContext &ToContext, FileManager &ToFileManager, 94218893Sdim ASTContext &FromContext, FileManager &FromFileManager, 95218893Sdim bool MinimalImport); 96203955Srdivacky 97203955Srdivacky virtual ~ASTImporter(); 98203955Srdivacky 99218893Sdim /// \brief Whether the importer will perform a minimal import, creating 100218893Sdim /// to-be-completed forward declarations when possible. 101218893Sdim bool isMinimalImport() const { return Minimal; } 102218893Sdim 103203955Srdivacky /// \brief Import the given type from the "from" context into the "to" 104203955Srdivacky /// context. 105203955Srdivacky /// 106203955Srdivacky /// \returns the equivalent type in the "to" context, or a NULL type if 107203955Srdivacky /// an error occurred. 108203955Srdivacky QualType Import(QualType FromT); 109203955Srdivacky 110203955Srdivacky /// \brief Import the given type source information from the 111203955Srdivacky /// "from" context into the "to" context. 112203955Srdivacky /// 113203955Srdivacky /// \returns the equivalent type source information in the "to" 114203955Srdivacky /// context, or NULL if an error occurred. 115203955Srdivacky TypeSourceInfo *Import(TypeSourceInfo *FromTSI); 116203955Srdivacky 117203955Srdivacky /// \brief Import the given declaration from the "from" context into the 118203955Srdivacky /// "to" context. 119203955Srdivacky /// 120203955Srdivacky /// \returns the equivalent declaration in the "to" context, or a NULL type 121203955Srdivacky /// if an error occurred. 122203955Srdivacky Decl *Import(Decl *FromD); 123203955Srdivacky 124203955Srdivacky /// \brief Import the given declaration context from the "from" 125203955Srdivacky /// AST context into the "to" AST context. 126203955Srdivacky /// 127203955Srdivacky /// \returns the equivalent declaration context in the "to" 128203955Srdivacky /// context, or a NULL type if an error occurred. 129203955Srdivacky DeclContext *ImportContext(DeclContext *FromDC); 130203955Srdivacky 131203955Srdivacky /// \brief Import the given expression from the "from" context into the 132203955Srdivacky /// "to" context. 133203955Srdivacky /// 134203955Srdivacky /// \returns the equivalent expression in the "to" context, or NULL if 135203955Srdivacky /// an error occurred. 136203955Srdivacky Expr *Import(Expr *FromE); 137203955Srdivacky 138203955Srdivacky /// \brief Import the given statement from the "from" context into the 139203955Srdivacky /// "to" context. 140203955Srdivacky /// 141203955Srdivacky /// \returns the equivalent statement in the "to" context, or NULL if 142203955Srdivacky /// an error occurred. 143203955Srdivacky Stmt *Import(Stmt *FromS); 144203955Srdivacky 145203955Srdivacky /// \brief Import the given nested-name-specifier from the "from" 146203955Srdivacky /// context into the "to" context. 147203955Srdivacky /// 148203955Srdivacky /// \returns the equivalent nested-name-specifier in the "to" 149203955Srdivacky /// context, or NULL if an error occurred. 150203955Srdivacky NestedNameSpecifier *Import(NestedNameSpecifier *FromNNS); 151219077Sdim 152219077Sdim /// \brief Import the given nested-name-specifier from the "from" 153219077Sdim /// context into the "to" context. 154219077Sdim /// 155219077Sdim /// \returns the equivalent nested-name-specifier in the "to" 156219077Sdim /// context. 157219077Sdim NestedNameSpecifierLoc Import(NestedNameSpecifierLoc FromNNS); 158219077Sdim 159218893Sdim /// \brief Import the goven template name from the "from" context into the 160218893Sdim /// "to" context. 161218893Sdim TemplateName Import(TemplateName From); 162218893Sdim 163203955Srdivacky /// \brief Import the given source location from the "from" context into 164203955Srdivacky /// the "to" context. 165203955Srdivacky /// 166203955Srdivacky /// \returns the equivalent source location in the "to" context, or an 167203955Srdivacky /// invalid source location if an error occurred. 168203955Srdivacky SourceLocation Import(SourceLocation FromLoc); 169203955Srdivacky 170203955Srdivacky /// \brief Import the given source range from the "from" context into 171203955Srdivacky /// the "to" context. 172203955Srdivacky /// 173203955Srdivacky /// \returns the equivalent source range in the "to" context, or an 174203955Srdivacky /// invalid source location if an error occurred. 175203955Srdivacky SourceRange Import(SourceRange FromRange); 176203955Srdivacky 177203955Srdivacky /// \brief Import the given declaration name from the "from" 178203955Srdivacky /// context into the "to" context. 179203955Srdivacky /// 180203955Srdivacky /// \returns the equivalent declaration name in the "to" context, 181203955Srdivacky /// or an empty declaration name if an error occurred. 182203955Srdivacky DeclarationName Import(DeclarationName FromName); 183203955Srdivacky 184203955Srdivacky /// \brief Import the given identifier from the "from" context 185203955Srdivacky /// into the "to" context. 186203955Srdivacky /// 187203955Srdivacky /// \returns the equivalent identifier in the "to" context. 188218893Sdim IdentifierInfo *Import(const IdentifierInfo *FromId); 189203955Srdivacky 190204643Srdivacky /// \brief Import the given Objective-C selector from the "from" 191204643Srdivacky /// context into the "to" context. 192204643Srdivacky /// 193204643Srdivacky /// \returns the equivalent selector in the "to" context. 194204643Srdivacky Selector Import(Selector FromSel); 195204643Srdivacky 196203955Srdivacky /// \brief Import the given file ID from the "from" context into the 197203955Srdivacky /// "to" context. 198203955Srdivacky /// 199203955Srdivacky /// \returns the equivalent file ID in the source manager of the "to" 200203955Srdivacky /// context. 201203955Srdivacky FileID Import(FileID); 202203955Srdivacky 203218893Sdim /// \brief Import the definition of the given declaration, including all of 204218893Sdim /// the declarations it contains. 205218893Sdim /// 206218893Sdim /// This routine is intended to be used 207218893Sdim void ImportDefinition(Decl *From); 208218893Sdim 209203955Srdivacky /// \brief Cope with a name conflict when importing a declaration into the 210203955Srdivacky /// given context. 211203955Srdivacky /// 212203955Srdivacky /// This routine is invoked whenever there is a name conflict while 213203955Srdivacky /// importing a declaration. The returned name will become the name of the 214203955Srdivacky /// imported declaration. By default, the returned name is the same as the 215203955Srdivacky /// original name, leaving the conflict unresolve such that name lookup 216203955Srdivacky /// for this name is likely to find an ambiguity later. 217203955Srdivacky /// 218203955Srdivacky /// Subclasses may override this routine to resolve the conflict, e.g., by 219203955Srdivacky /// renaming the declaration being imported. 220203955Srdivacky /// 221203955Srdivacky /// \param Name the name of the declaration being imported, which conflicts 222203955Srdivacky /// with other declarations. 223203955Srdivacky /// 224203955Srdivacky /// \param DC the declaration context (in the "to" AST context) in which 225203955Srdivacky /// the name is being imported. 226203955Srdivacky /// 227203955Srdivacky /// \param IDNS the identifier namespace in which the name will be found. 228203955Srdivacky /// 229203955Srdivacky /// \param Decls the set of declarations with the same name as the 230203955Srdivacky /// declaration being imported. 231203955Srdivacky /// 232203955Srdivacky /// \param NumDecls the number of conflicting declarations in \p Decls. 233203955Srdivacky /// 234203955Srdivacky /// \returns the name that the newly-imported declaration should have. 235203955Srdivacky virtual DeclarationName HandleNameConflict(DeclarationName Name, 236203955Srdivacky DeclContext *DC, 237203955Srdivacky unsigned IDNS, 238203955Srdivacky NamedDecl **Decls, 239203955Srdivacky unsigned NumDecls); 240203955Srdivacky 241203955Srdivacky /// \brief Retrieve the context that AST nodes are being imported into. 242203955Srdivacky ASTContext &getToContext() const { return ToContext; } 243203955Srdivacky 244203955Srdivacky /// \brief Retrieve the context that AST nodes are being imported from. 245203955Srdivacky ASTContext &getFromContext() const { return FromContext; } 246203955Srdivacky 247203955Srdivacky /// \brief Retrieve the file manager that AST nodes are being imported into. 248203955Srdivacky FileManager &getToFileManager() const { return ToFileManager; } 249203955Srdivacky 250203955Srdivacky /// \brief Retrieve the file manager that AST nodes are being imported from. 251203955Srdivacky FileManager &getFromFileManager() const { return FromFileManager; } 252203955Srdivacky 253203955Srdivacky /// \brief Report a diagnostic in the "to" context. 254203955Srdivacky DiagnosticBuilder ToDiag(SourceLocation Loc, unsigned DiagID); 255203955Srdivacky 256203955Srdivacky /// \brief Report a diagnostic in the "from" context. 257203955Srdivacky DiagnosticBuilder FromDiag(SourceLocation Loc, unsigned DiagID); 258203955Srdivacky 259203955Srdivacky /// \brief Return the set of declarations that we know are not equivalent. 260203955Srdivacky NonEquivalentDeclSet &getNonEquivalentDecls() { return NonEquivalentDecls; } 261234353Sdim 262234353Sdim /// \brief Called for ObjCInterfaceDecl, ObjCProtocolDecl, and TagDecl. 263234353Sdim /// Mark the Decl as complete, filling it in as much as possible. 264234353Sdim /// 265234353Sdim /// \param D A declaration in the "to" context. 266234353Sdim virtual void CompleteDecl(Decl* D); 267203955Srdivacky 268203955Srdivacky /// \brief Note that we have imported the "from" declaration by mapping it 269203955Srdivacky /// to the (potentially-newly-created) "to" declaration. 270203955Srdivacky /// 271218893Sdim /// Subclasses can override this function to observe all of the \c From -> 272218893Sdim /// \c To declaration mappings as they are imported. 273218893Sdim virtual Decl *Imported(Decl *From, Decl *To); 274263508Sdim 275263508Sdim /// \brief Called by StructuralEquivalenceContext. If a RecordDecl is 276263508Sdim /// being compared to another RecordDecl as part of import, completing the 277263508Sdim /// other RecordDecl may trigger importation of the first RecordDecl. This 278263508Sdim /// happens especially for anonymous structs. If the original of the second 279263508Sdim /// RecordDecl can be found, we can complete it without the need for 280263508Sdim /// importation, eliminating this loop. 281263508Sdim virtual Decl *GetOriginalDecl(Decl *To) { return NULL; } 282203955Srdivacky 283203955Srdivacky /// \brief Determine whether the given types are structurally 284203955Srdivacky /// equivalent. 285239462Sdim bool IsStructurallyEquivalent(QualType From, QualType To, 286239462Sdim bool Complain = true); 287203955Srdivacky }; 288203955Srdivacky} 289203955Srdivacky 290203955Srdivacky#endif // LLVM_CLANG_AST_ASTIMPORTER_H 291