clang API Documentation

ASTImporter.h
Go to the documentation of this file.
00001 //===--- ASTImporter.h - Importing ASTs from other Contexts -----*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  This file defines the ASTImporter class which imports AST nodes from one
00011 //  context into another context.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 #ifndef LLVM_CLANG_AST_ASTIMPORTER_H
00015 #define LLVM_CLANG_AST_ASTIMPORTER_H
00016 
00017 #include "clang/AST/DeclarationName.h"
00018 #include "clang/AST/Type.h"
00019 #include "clang/Basic/SourceLocation.h"
00020 #include "llvm/ADT/DenseMap.h"
00021 #include "llvm/ADT/DenseSet.h"
00022 #include "llvm/ADT/SmallVector.h"
00023 
00024 namespace clang {
00025   class ASTContext;
00026   class Decl;
00027   class DeclContext;
00028   class DiagnosticsEngine;
00029   class Expr;
00030   class FileManager;
00031   class IdentifierInfo;
00032   class NestedNameSpecifier;
00033   class Stmt;
00034   class TypeSourceInfo;
00035   
00036   /// \brief Imports selected nodes from one AST context into another context,
00037   /// merging AST nodes where appropriate.
00038   class ASTImporter {
00039   public:
00040     typedef llvm::DenseSet<std::pair<Decl *, Decl *> > NonEquivalentDeclSet;
00041     
00042   private:
00043     /// \brief The contexts we're importing to and from.
00044     ASTContext &ToContext, &FromContext;
00045     
00046     /// \brief The file managers we're importing to and from.
00047     FileManager &ToFileManager, &FromFileManager;
00048 
00049     /// \brief Whether to perform a minimal import.
00050     bool Minimal;
00051     
00052     /// \brief Mapping from the already-imported types in the "from" context
00053     /// to the corresponding types in the "to" context.
00054     llvm::DenseMap<const Type *, const Type *> ImportedTypes;
00055     
00056     /// \brief Mapping from the already-imported declarations in the "from"
00057     /// context to the corresponding declarations in the "to" context.
00058     llvm::DenseMap<Decl *, Decl *> ImportedDecls;
00059 
00060     /// \brief Mapping from the already-imported statements in the "from"
00061     /// context to the corresponding statements in the "to" context.
00062     llvm::DenseMap<Stmt *, Stmt *> ImportedStmts;
00063 
00064     /// \brief Mapping from the already-imported FileIDs in the "from" source
00065     /// manager to the corresponding FileIDs in the "to" source manager.
00066     llvm::DenseMap<FileID, FileID> ImportedFileIDs;
00067     
00068     /// \brief Imported, anonymous tag declarations that are missing their 
00069     /// corresponding typedefs.
00070     SmallVector<TagDecl *, 4> AnonTagsWithPendingTypedefs;
00071     
00072     /// \brief Declaration (from, to) pairs that are known not to be equivalent
00073     /// (which we have already complained about).
00074     NonEquivalentDeclSet NonEquivalentDecls;
00075     
00076   public:
00077     /// \brief Create a new AST importer.
00078     ///
00079     /// \param ToContext The context we'll be importing into.
00080     ///
00081     /// \param ToFileManager The file manager we'll be importing into.
00082     ///
00083     /// \param FromContext The context we'll be importing from.
00084     ///
00085     /// \param FromFileManager The file manager we'll be importing into.
00086     ///
00087     /// \param MinimalImport If true, the importer will attempt to import
00088     /// as little as it can, e.g., by importing declarations as forward
00089     /// declarations that can be completed at a later point.
00090     ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
00091                 ASTContext &FromContext, FileManager &FromFileManager,
00092                 bool MinimalImport);
00093     
00094     virtual ~ASTImporter();
00095     
00096     /// \brief Whether the importer will perform a minimal import, creating
00097     /// to-be-completed forward declarations when possible.
00098     bool isMinimalImport() const { return Minimal; }
00099     
00100     /// \brief Import the given type from the "from" context into the "to"
00101     /// context.
00102     ///
00103     /// \returns the equivalent type in the "to" context, or a NULL type if
00104     /// an error occurred.
00105     QualType Import(QualType FromT);
00106 
00107     /// \brief Import the given type source information from the
00108     /// "from" context into the "to" context.
00109     ///
00110     /// \returns the equivalent type source information in the "to"
00111     /// context, or NULL if an error occurred.
00112     TypeSourceInfo *Import(TypeSourceInfo *FromTSI);
00113 
00114     /// \brief Import the given declaration from the "from" context into the 
00115     /// "to" context.
00116     ///
00117     /// \returns the equivalent declaration in the "to" context, or a NULL type 
00118     /// if an error occurred.
00119     Decl *Import(Decl *FromD);
00120 
00121     /// \brief Import the given declaration context from the "from"
00122     /// AST context into the "to" AST context.
00123     ///
00124     /// \returns the equivalent declaration context in the "to"
00125     /// context, or a NULL type if an error occurred.
00126     DeclContext *ImportContext(DeclContext *FromDC);
00127     
00128     /// \brief Import the given expression from the "from" context into the
00129     /// "to" context.
00130     ///
00131     /// \returns the equivalent expression in the "to" context, or NULL if
00132     /// an error occurred.
00133     Expr *Import(Expr *FromE);
00134 
00135     /// \brief Import the given statement from the "from" context into the
00136     /// "to" context.
00137     ///
00138     /// \returns the equivalent statement in the "to" context, or NULL if
00139     /// an error occurred.
00140     Stmt *Import(Stmt *FromS);
00141 
00142     /// \brief Import the given nested-name-specifier from the "from"
00143     /// context into the "to" context.
00144     ///
00145     /// \returns the equivalent nested-name-specifier in the "to"
00146     /// context, or NULL if an error occurred.
00147     NestedNameSpecifier *Import(NestedNameSpecifier *FromNNS);
00148 
00149     /// \brief Import the given nested-name-specifier from the "from"
00150     /// context into the "to" context.
00151     ///
00152     /// \returns the equivalent nested-name-specifier in the "to"
00153     /// context.
00154     NestedNameSpecifierLoc Import(NestedNameSpecifierLoc FromNNS);
00155 
00156     /// \brief Import the goven template name from the "from" context into the
00157     /// "to" context.
00158     TemplateName Import(TemplateName From);
00159     
00160     /// \brief Import the given source location from the "from" context into
00161     /// the "to" context.
00162     ///
00163     /// \returns the equivalent source location in the "to" context, or an
00164     /// invalid source location if an error occurred.
00165     SourceLocation Import(SourceLocation FromLoc);
00166 
00167     /// \brief Import the given source range from the "from" context into
00168     /// the "to" context.
00169     ///
00170     /// \returns the equivalent source range in the "to" context, or an
00171     /// invalid source location if an error occurred.
00172     SourceRange Import(SourceRange FromRange);
00173 
00174     /// \brief Import the given declaration name from the "from"
00175     /// context into the "to" context.
00176     ///
00177     /// \returns the equivalent declaration name in the "to" context,
00178     /// or an empty declaration name if an error occurred.
00179     DeclarationName Import(DeclarationName FromName);
00180 
00181     /// \brief Import the given identifier from the "from" context
00182     /// into the "to" context.
00183     ///
00184     /// \returns the equivalent identifier in the "to" context.
00185     IdentifierInfo *Import(const IdentifierInfo *FromId);
00186 
00187     /// \brief Import the given Objective-C selector from the "from"
00188     /// context into the "to" context.
00189     ///
00190     /// \returns the equivalent selector in the "to" context.
00191     Selector Import(Selector FromSel);
00192 
00193     /// \brief Import the given file ID from the "from" context into the 
00194     /// "to" context.
00195     ///
00196     /// \returns the equivalent file ID in the source manager of the "to"
00197     /// context.
00198     FileID Import(FileID);
00199     
00200     /// \brief Import the definition of the given declaration, including all of
00201     /// the declarations it contains.
00202     ///
00203     /// This routine is intended to be used 
00204     void ImportDefinition(Decl *From);
00205 
00206     /// \brief Cope with a name conflict when importing a declaration into the
00207     /// given context.
00208     ///
00209     /// This routine is invoked whenever there is a name conflict while 
00210     /// importing a declaration. The returned name will become the name of the
00211     /// imported declaration. By default, the returned name is the same as the
00212     /// original name, leaving the conflict unresolve such that name lookup
00213     /// for this name is likely to find an ambiguity later.
00214     ///
00215     /// Subclasses may override this routine to resolve the conflict, e.g., by
00216     /// renaming the declaration being imported.
00217     ///
00218     /// \param Name the name of the declaration being imported, which conflicts
00219     /// with other declarations.
00220     ///
00221     /// \param DC the declaration context (in the "to" AST context) in which 
00222     /// the name is being imported.
00223     ///
00224     /// \param IDNS the identifier namespace in which the name will be found.
00225     ///
00226     /// \param Decls the set of declarations with the same name as the
00227     /// declaration being imported.
00228     ///
00229     /// \param NumDecls the number of conflicting declarations in \p Decls.
00230     ///
00231     /// \returns the name that the newly-imported declaration should have.
00232     virtual DeclarationName HandleNameConflict(DeclarationName Name,
00233                                                DeclContext *DC,
00234                                                unsigned IDNS,
00235                                                NamedDecl **Decls,
00236                                                unsigned NumDecls);
00237     
00238     /// \brief Retrieve the context that AST nodes are being imported into.
00239     ASTContext &getToContext() const { return ToContext; }
00240     
00241     /// \brief Retrieve the context that AST nodes are being imported from.
00242     ASTContext &getFromContext() const { return FromContext; }
00243     
00244     /// \brief Retrieve the file manager that AST nodes are being imported into.
00245     FileManager &getToFileManager() const { return ToFileManager; }
00246 
00247     /// \brief Retrieve the file manager that AST nodes are being imported from.
00248     FileManager &getFromFileManager() const { return FromFileManager; }
00249     
00250     /// \brief Report a diagnostic in the "to" context.
00251     DiagnosticBuilder ToDiag(SourceLocation Loc, unsigned DiagID);
00252     
00253     /// \brief Report a diagnostic in the "from" context.
00254     DiagnosticBuilder FromDiag(SourceLocation Loc, unsigned DiagID);
00255     
00256     /// \brief Return the set of declarations that we know are not equivalent.
00257     NonEquivalentDeclSet &getNonEquivalentDecls() { return NonEquivalentDecls; }
00258 
00259     /// \brief Called for ObjCInterfaceDecl, ObjCProtocolDecl, and TagDecl.
00260     /// Mark the Decl as complete, filling it in as much as possible.
00261     ///
00262     /// \param D A declaration in the "to" context.
00263     virtual void CompleteDecl(Decl* D);
00264     
00265     /// \brief Note that we have imported the "from" declaration by mapping it
00266     /// to the (potentially-newly-created) "to" declaration.
00267     ///
00268     /// Subclasses can override this function to observe all of the \c From ->
00269     /// \c To declaration mappings as they are imported.
00270     virtual Decl *Imported(Decl *From, Decl *To);
00271     
00272     /// \brief Determine whether the given types are structurally
00273     /// equivalent.
00274     bool IsStructurallyEquivalent(QualType From, QualType To);    
00275   };
00276 }
00277 
00278 #endif // LLVM_CLANG_AST_ASTIMPORTER_H