clang API Documentation

ASTReaderInternals.h
Go to the documentation of this file.
00001 //===--- ASTReaderInternals.h - AST Reader Internals ------------*- 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 provides internal definitions used in the AST reader.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 #ifndef LLVM_CLANG_SERIALIZATION_ASTREADER_INTERNALS_H
00014 #define LLVM_CLANG_SERIALIZATION_ASTREADER_INTERNALS_H
00015 
00016 #include "clang/Basic/OnDiskHashTable.h"
00017 #include "clang/AST/DeclarationName.h"
00018 #include "llvm/Support/Endian.h"
00019 #include <utility>
00020 #include <sys/stat.h>
00021 
00022 namespace clang {
00023 
00024 class ASTReader;
00025 class HeaderSearch;
00026 struct HeaderFileInfo;
00027   
00028 namespace serialization {
00029 
00030 class ModuleFile;
00031 
00032 namespace reader {
00033 
00034 /// \brief Class that performs name lookup into a DeclContext stored
00035 /// in an AST file.
00036 class ASTDeclContextNameLookupTrait {
00037   ASTReader &Reader;
00038   ModuleFile &F;
00039   
00040 public:
00041   /// \brief Pair of begin/end iterators for DeclIDs.
00042   ///
00043   /// Note that these declaration IDs are local to the module that contains this
00044   /// particular lookup t
00045   typedef llvm::support::ulittle32_t LE32DeclID;
00046   typedef std::pair<LE32DeclID *, LE32DeclID *> data_type;
00047 
00048   /// \brief Special internal key for declaration names.
00049   /// The hash table creates keys for comparison; we do not create
00050   /// a DeclarationName for the internal key to avoid deserializing types.
00051   struct DeclNameKey {
00052     DeclarationName::NameKind Kind;
00053     uint64_t Data;
00054     DeclNameKey() : Kind((DeclarationName::NameKind)0), Data(0) { }
00055   };
00056 
00057   typedef DeclarationName external_key_type;
00058   typedef DeclNameKey internal_key_type;
00059 
00060   explicit ASTDeclContextNameLookupTrait(ASTReader &Reader, ModuleFile &F)
00061     : Reader(Reader), F(F) { }
00062 
00063   static bool EqualKey(const internal_key_type& a,
00064                        const internal_key_type& b) {
00065     return a.Kind == b.Kind && a.Data == b.Data;
00066   }
00067 
00068   unsigned ComputeHash(const DeclNameKey &Key) const;
00069   internal_key_type GetInternalKey(const external_key_type& Name) const;
00070 
00071   static std::pair<unsigned, unsigned>
00072   ReadKeyDataLength(const unsigned char*& d);
00073 
00074   internal_key_type ReadKey(const unsigned char* d, unsigned);
00075 
00076   data_type ReadData(internal_key_type, const unsigned char* d,
00077                      unsigned DataLen);
00078 };
00079 
00080 /// \brief Class that performs lookup for an identifier stored in an AST file.
00081 class ASTIdentifierLookupTrait {
00082   ASTReader &Reader;
00083   ModuleFile &F;
00084   
00085   // If we know the IdentifierInfo in advance, it is here and we will
00086   // not build a new one. Used when deserializing information about an
00087   // identifier that was constructed before the AST file was read.
00088   IdentifierInfo *KnownII;
00089   
00090 public:
00091   typedef IdentifierInfo * data_type;
00092   
00093   typedef const std::pair<const char*, unsigned> external_key_type;
00094   
00095   typedef external_key_type internal_key_type;
00096   
00097   ASTIdentifierLookupTrait(ASTReader &Reader, ModuleFile &F,
00098                            IdentifierInfo *II = 0)
00099     : Reader(Reader), F(F), KnownII(II) { }
00100   
00101   static bool EqualKey(const internal_key_type& a,
00102                        const internal_key_type& b) {
00103     return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
00104     : false;
00105   }
00106   
00107   static unsigned ComputeHash(const internal_key_type& a);
00108   
00109   // This hopefully will just get inlined and removed by the optimizer.
00110   static const internal_key_type&
00111   GetInternalKey(const external_key_type& x) { return x; }
00112   
00113   // This hopefully will just get inlined and removed by the optimizer.
00114   static const external_key_type&
00115   GetExternalKey(const internal_key_type& x) { return x; }
00116  
00117   static std::pair<unsigned, unsigned>
00118   ReadKeyDataLength(const unsigned char*& d);
00119 
00120   static std::pair<const char*, unsigned>
00121   ReadKey(const unsigned char* d, unsigned n);
00122   
00123   IdentifierInfo *ReadData(const internal_key_type& k,
00124                            const unsigned char* d,
00125                            unsigned DataLen);
00126   
00127   ASTReader &getReader() const { return Reader; }
00128   
00129 };
00130   
00131 /// \brief The on-disk hash table used to contain information about
00132 /// all of the identifiers in the program.
00133 typedef OnDiskChainedHashTable<ASTIdentifierLookupTrait>
00134   ASTIdentifierLookupTable;
00135 
00136 /// \brief Class that performs lookup for a selector's entries in the global
00137 /// method pool stored in an AST file.
00138 class ASTSelectorLookupTrait {
00139   ASTReader &Reader;
00140   ModuleFile &F;
00141   
00142 public:
00143   struct data_type {
00144     SelectorID ID;
00145     llvm::SmallVector<ObjCMethodDecl *, 2> Instance;
00146     llvm::SmallVector<ObjCMethodDecl *, 2> Factory;
00147   };
00148   
00149   typedef Selector external_key_type;
00150   typedef external_key_type internal_key_type;
00151   
00152   ASTSelectorLookupTrait(ASTReader &Reader, ModuleFile &F) 
00153     : Reader(Reader), F(F) { }
00154   
00155   static bool EqualKey(const internal_key_type& a,
00156                        const internal_key_type& b) {
00157     return a == b;
00158   }
00159   
00160   static unsigned ComputeHash(Selector Sel);
00161   
00162   static const internal_key_type&
00163   GetInternalKey(const external_key_type& x) { return x; }
00164   
00165   static std::pair<unsigned, unsigned>
00166   ReadKeyDataLength(const unsigned char*& d);
00167   
00168   internal_key_type ReadKey(const unsigned char* d, unsigned);
00169   data_type ReadData(Selector, const unsigned char* d, unsigned DataLen);
00170 };
00171   
00172 /// \brief The on-disk hash table used for the global method pool.
00173 typedef OnDiskChainedHashTable<ASTSelectorLookupTrait>
00174   ASTSelectorLookupTable;
00175   
00176 /// \brief Trait class used to search the on-disk hash table containing all of
00177 /// the header search information.
00178 ///
00179 /// The on-disk hash table contains a mapping from each header path to 
00180 /// information about that header (how many times it has been included, its
00181 /// controlling macro, etc.). Note that we actually hash based on the 
00182 /// filename, and support "deep" comparisons of file names based on current
00183 /// inode numbers, so that the search can cope with non-normalized path names
00184 /// and symlinks.
00185 class HeaderFileInfoTrait {
00186   ASTReader &Reader;
00187   ModuleFile &M;
00188   HeaderSearch *HS;
00189   const char *FrameworkStrings;
00190   const char *SearchPath;
00191   struct stat SearchPathStatBuf;
00192   llvm::Optional<int> SearchPathStatResult;
00193   
00194   int StatSimpleCache(const char *Path, struct stat *StatBuf) {
00195     if (Path == SearchPath) {
00196       if (!SearchPathStatResult)
00197         SearchPathStatResult = stat(Path, &SearchPathStatBuf);
00198       
00199       *StatBuf = SearchPathStatBuf;
00200       return *SearchPathStatResult;
00201     }
00202     
00203     return stat(Path, StatBuf);
00204   }
00205   
00206 public:
00207   typedef const char *external_key_type;
00208   typedef const char *internal_key_type;
00209   
00210   typedef HeaderFileInfo data_type;
00211   
00212   HeaderFileInfoTrait(ASTReader &Reader, ModuleFile &M, HeaderSearch *HS,
00213                       const char *FrameworkStrings,
00214                       const char *SearchPath = 0) 
00215   : Reader(Reader), M(M), HS(HS), FrameworkStrings(FrameworkStrings), 
00216     SearchPath(SearchPath) { }
00217   
00218   static unsigned ComputeHash(const char *path);
00219   static internal_key_type GetInternalKey(const char *path);
00220   bool EqualKey(internal_key_type a, internal_key_type b);
00221   
00222   static std::pair<unsigned, unsigned>
00223   ReadKeyDataLength(const unsigned char*& d);
00224   
00225   static internal_key_type ReadKey(const unsigned char *d, unsigned) {
00226     return (const char *)d;
00227   }
00228   
00229   data_type ReadData(const internal_key_type, const unsigned char *d,
00230                      unsigned DataLen);
00231 };
00232 
00233 /// \brief The on-disk hash table used for known header files.
00234 typedef OnDiskChainedHashTable<HeaderFileInfoTrait>
00235   HeaderFileInfoLookupTable;
00236   
00237 } // end namespace clang::serialization::reader
00238 } // end namespace clang::serialization
00239 } // end namespace clang
00240 
00241 
00242 #endif