Home | History | Annotate | Download | only in AST
      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 Mapping from the already-imported types in the "from" context
     53     /// to the corresponding types in the "to" context.
     54     llvm::DenseMap<const Type *, const Type *> ImportedTypes;
     55 
     56     /// \brief Mapping from the already-imported declarations in the "from"
     57     /// context to the corresponding declarations in the "to" context.
     58     llvm::DenseMap<Decl *, Decl *> ImportedDecls;
     59 
     60     /// \brief Mapping from the already-imported statements in the "from"
     61     /// context to the corresponding statements in the "to" context.
     62     llvm::DenseMap<Stmt *, Stmt *> ImportedStmts;
     63 
     64     /// \brief Mapping from the already-imported FileIDs in the "from" source
     65     /// manager to the corresponding FileIDs in the "to" source manager.
     66     llvm::DenseMap<FileID, FileID> ImportedFileIDs;
     67 
     68     /// \brief Imported, anonymous tag declarations that are missing their
     69     /// corresponding typedefs.
     70     SmallVector<TagDecl *, 4> AnonTagsWithPendingTypedefs;
     71 
     72     /// \brief Declaration (from, to) pairs that are known not to be equivalent
     73     /// (which we have already complained about).
     74     NonEquivalentDeclSet NonEquivalentDecls;
     75 
     76   public:
     77     /// \brief Create a new AST importer.
     78     ///
     79     /// \param ToContext The context we'll be importing into.
     80     ///
     81     /// \param ToFileManager The file manager we'll be importing into.
     82     ///
     83     /// \param FromContext The context we'll be importing from.
     84     ///
     85     /// \param FromFileManager The file manager we'll be importing into.
     86     ///
     87     /// \param MinimalImport If true, the importer will attempt to import
     88     /// as little as it can, e.g., by importing declarations as forward
     89     /// declarations that can be completed at a later point.
     90     ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
     91                 ASTContext &FromContext, FileManager &FromFileManager,
     92                 bool MinimalImport);
     93 
     94     virtual ~ASTImporter();
     95 
     96     /// \brief Whether the importer will perform a minimal import, creating
     97     /// to-be-completed forward declarations when possible.
     98     bool isMinimalImport() const { return Minimal; }
     99 
    100     /// \brief Import the given type from the "from" context into the "to"
    101     /// context.
    102     ///
    103     /// \returns the equivalent type in the "to" context, or a NULL type if
    104     /// an error occurred.
    105     QualType Import(QualType FromT);
    106 
    107     /// \brief Import the given type source information from the
    108     /// "from" context into the "to" context.
    109     ///
    110     /// \returns the equivalent type source information in the "to"
    111     /// context, or NULL if an error occurred.
    112     TypeSourceInfo *Import(TypeSourceInfo *FromTSI);
    113 
    114     /// \brief Import the given declaration from the "from" context into the
    115     /// "to" context.
    116     ///
    117     /// \returns the equivalent declaration in the "to" context, or a NULL type
    118     /// if an error occurred.
    119     Decl *Import(Decl *FromD);
    120 
    121     /// \brief Import the given declaration context from the "from"
    122     /// AST context into the "to" AST context.
    123     ///
    124     /// \returns the equivalent declaration context in the "to"
    125     /// context, or a NULL type if an error occurred.
    126     DeclContext *ImportContext(DeclContext *FromDC);
    127 
    128     /// \brief Import the given expression from the "from" context into the
    129     /// "to" context.
    130     ///
    131     /// \returns the equivalent expression in the "to" context, or NULL if
    132     /// an error occurred.
    133     Expr *Import(Expr *FromE);
    134 
    135     /// \brief Import the given statement from the "from" context into the
    136     /// "to" context.
    137     ///
    138     /// \returns the equivalent statement in the "to" context, or NULL if
    139     /// an error occurred.
    140     Stmt *Import(Stmt *FromS);
    141 
    142     /// \brief Import the given nested-name-specifier from the "from"
    143     /// context into the "to" context.
    144     ///
    145     /// \returns the equivalent nested-name-specifier in the "to"
    146     /// context, or NULL if an error occurred.
    147     NestedNameSpecifier *Import(NestedNameSpecifier *FromNNS);
    148 
    149     /// \brief Import the given nested-name-specifier from the "from"
    150     /// context into the "to" context.
    151     ///
    152     /// \returns the equivalent nested-name-specifier in the "to"
    153     /// context.
    154     NestedNameSpecifierLoc Import(NestedNameSpecifierLoc FromNNS);
    155 
    156     /// \brief Import the goven template name from the "from" context into the
    157     /// "to" context.
    158     TemplateName Import(TemplateName From);
    159 
    160     /// \brief Import the given source location from the "from" context into
    161     /// the "to" context.
    162     ///
    163     /// \returns the equivalent source location in the "to" context, or an
    164     /// invalid source location if an error occurred.
    165     SourceLocation Import(SourceLocation FromLoc);
    166 
    167     /// \brief Import the given source range from the "from" context into
    168     /// the "to" context.
    169     ///
    170     /// \returns the equivalent source range in the "to" context, or an
    171     /// invalid source location if an error occurred.
    172     SourceRange Import(SourceRange FromRange);
    173 
    174     /// \brief Import the given declaration name from the "from"
    175     /// context into the "to" context.
    176     ///
    177     /// \returns the equivalent declaration name in the "to" context,
    178     /// or an empty declaration name if an error occurred.
    179     DeclarationName Import(DeclarationName FromName);
    180 
    181     /// \brief Import the given identifier from the "from" context
    182     /// into the "to" context.
    183     ///
    184     /// \returns the equivalent identifier in the "to" context.
    185     IdentifierInfo *Import(const IdentifierInfo *FromId);
    186 
    187     /// \brief Import the given Objective-C selector from the "from"
    188     /// context into the "to" context.
    189     ///
    190     /// \returns the equivalent selector in the "to" context.
    191     Selector Import(Selector FromSel);
    192 
    193     /// \brief Import the given file ID from the "from" context into the
    194     /// "to" context.
    195     ///
    196     /// \returns the equivalent file ID in the source manager of the "to"
    197     /// context.
    198     FileID Import(FileID);
    199 
    200     /// \brief Import the definition of the given declaration, including all of
    201     /// the declarations it contains.
    202     ///
    203     /// This routine is intended to be used
    204     void ImportDefinition(Decl *From);
    205 
    206     /// \brief Cope with a name conflict when importing a declaration into the
    207     /// given context.
    208     ///
    209     /// This routine is invoked whenever there is a name conflict while
    210     /// importing a declaration. The returned name will become the name of the
    211     /// imported declaration. By default, the returned name is the same as the
    212     /// original name, leaving the conflict unresolve such that name lookup
    213     /// for this name is likely to find an ambiguity later.
    214     ///
    215     /// Subclasses may override this routine to resolve the conflict, e.g., by
    216     /// renaming the declaration being imported.
    217     ///
    218     /// \param Name the name of the declaration being imported, which conflicts
    219     /// with other declarations.
    220     ///
    221     /// \param DC the declaration context (in the "to" AST context) in which
    222     /// the name is being imported.
    223     ///
    224     /// \param IDNS the identifier namespace in which the name will be found.
    225     ///
    226     /// \param Decls the set of declarations with the same name as the
    227     /// declaration being imported.
    228     ///
    229     /// \param NumDecls the number of conflicting declarations in \p Decls.
    230     ///
    231     /// \returns the name that the newly-imported declaration should have.
    232     virtual DeclarationName HandleNameConflict(DeclarationName Name,
    233                                                DeclContext *DC,
    234                                                unsigned IDNS,
    235                                                NamedDecl **Decls,
    236                                                unsigned NumDecls);
    237 
    238     /// \brief Retrieve the context that AST nodes are being imported into.
    239     ASTContext &getToContext() const { return ToContext; }
    240 
    241     /// \brief Retrieve the context that AST nodes are being imported from.
    242     ASTContext &getFromContext() const { return FromContext; }
    243 
    244     /// \brief Retrieve the file manager that AST nodes are being imported into.
    245     FileManager &getToFileManager() const { return ToFileManager; }
    246 
    247     /// \brief Retrieve the file manager that AST nodes are being imported from.
    248     FileManager &getFromFileManager() const { return FromFileManager; }
    249 
    250     /// \brief Report a diagnostic in the "to" context.
    251     DiagnosticBuilder ToDiag(SourceLocation Loc, unsigned DiagID);
    252 
    253     /// \brief Report a diagnostic in the "from" context.
    254     DiagnosticBuilder FromDiag(SourceLocation Loc, unsigned DiagID);
    255 
    256     /// \brief Return the set of declarations that we know are not equivalent.
    257     NonEquivalentDeclSet &getNonEquivalentDecls() { return NonEquivalentDecls; }
    258 
    259     /// \brief Called for ObjCInterfaceDecl, ObjCProtocolDecl, and TagDecl.
    260     /// Mark the Decl as complete, filling it in as much as possible.
    261     ///
    262     /// \param D A declaration in the "to" context.
    263     virtual void CompleteDecl(Decl* D);
    264 
    265     /// \brief Note that we have imported the "from" declaration by mapping it
    266     /// to the (potentially-newly-created) "to" declaration.
    267     ///
    268     /// Subclasses can override this function to observe all of the \c From ->
    269     /// \c To declaration mappings as they are imported.
    270     virtual Decl *Imported(Decl *From, Decl *To);
    271 
    272     /// \brief Determine whether the given types are structurally
    273     /// equivalent.
    274     bool IsStructurallyEquivalent(QualType From, QualType To);
    275   };
    276 }
    277 
    278 #endif // LLVM_CLANG_AST_ASTIMPORTER_H
    279