Home | History | Annotate | Download | only in Serialization
      1 //===--- ASTReaderInternals.h - AST Reader Internals ------------*- 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 provides internal definitions used in the AST reader.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 #ifndef LLVM_CLANG_SERIALIZATION_ASTREADER_INTERNALS_H
     14 #define LLVM_CLANG_SERIALIZATION_ASTREADER_INTERNALS_H
     15 
     16 #include "clang/AST/DeclarationName.h"
     17 #include "clang/Basic/OnDiskHashTable.h"
     18 #include "clang/Serialization/ASTBitCodes.h"
     19 #include "llvm/Support/Endian.h"
     20 #include <sys/stat.h>
     21 #include <utility>
     22 
     23 namespace clang {
     24 
     25 class ASTReader;
     26 class HeaderSearch;
     27 struct HeaderFileInfo;
     28 class FileEntry;
     29 
     30 namespace serialization {
     31 
     32 class ModuleFile;
     33 
     34 namespace reader {
     35 
     36 /// \brief Class that performs name lookup into a DeclContext stored
     37 /// in an AST file.
     38 class ASTDeclContextNameLookupTrait {
     39   ASTReader &Reader;
     40   ModuleFile &F;
     41 
     42 public:
     43   /// \brief Pair of begin/end iterators for DeclIDs.
     44   ///
     45   /// Note that these declaration IDs are local to the module that contains this
     46   /// particular lookup t
     47   typedef llvm::support::ulittle32_t LE32DeclID;
     48   typedef std::pair<LE32DeclID *, LE32DeclID *> data_type;
     49 
     50   /// \brief Special internal key for declaration names.
     51   /// The hash table creates keys for comparison; we do not create
     52   /// a DeclarationName for the internal key to avoid deserializing types.
     53   struct DeclNameKey {
     54     DeclarationName::NameKind Kind;
     55     uint64_t Data;
     56     DeclNameKey() : Kind((DeclarationName::NameKind)0), Data(0) { }
     57   };
     58 
     59   typedef DeclarationName external_key_type;
     60   typedef DeclNameKey internal_key_type;
     61 
     62   explicit ASTDeclContextNameLookupTrait(ASTReader &Reader, ModuleFile &F)
     63     : Reader(Reader), F(F) { }
     64 
     65   static bool EqualKey(const internal_key_type& a,
     66                        const internal_key_type& b) {
     67     return a.Kind == b.Kind && a.Data == b.Data;
     68   }
     69 
     70   unsigned ComputeHash(const DeclNameKey &Key) const;
     71   internal_key_type GetInternalKey(const external_key_type& Name) const;
     72 
     73   static std::pair<unsigned, unsigned>
     74   ReadKeyDataLength(const unsigned char*& d);
     75 
     76   internal_key_type ReadKey(const unsigned char* d, unsigned);
     77 
     78   data_type ReadData(internal_key_type, const unsigned char* d,
     79                      unsigned DataLen);
     80 };
     81 
     82 /// \brief Base class for the trait describing the on-disk hash table for the
     83 /// identifiers in an AST file.
     84 ///
     85 /// This class is not useful by itself; rather, it provides common
     86 /// functionality for accessing the on-disk hash table of identifiers
     87 /// in an AST file. Different subclasses customize that functionality
     88 /// based on what information they are interested in. Those subclasses
     89 /// must provide the \c data_type typedef and the ReadData operation,
     90 /// only.
     91 class ASTIdentifierLookupTraitBase {
     92 public:
     93   typedef StringRef external_key_type;
     94   typedef StringRef internal_key_type;
     95 
     96 
     97   static bool EqualKey(const internal_key_type& a, const internal_key_type& b) {
     98     return a == b;
     99   }
    100 
    101   static unsigned ComputeHash(const internal_key_type& a);
    102 
    103   static std::pair<unsigned, unsigned>
    104   ReadKeyDataLength(const unsigned char*& d);
    105 
    106   // This hopefully will just get inlined and removed by the optimizer.
    107   static const internal_key_type&
    108   GetInternalKey(const external_key_type& x) { return x; }
    109 
    110   // This hopefully will just get inlined and removed by the optimizer.
    111   static const external_key_type&
    112   GetExternalKey(const internal_key_type& x) { return x; }
    113 
    114   static internal_key_type ReadKey(const unsigned char* d, unsigned n);
    115 };
    116 
    117 /// \brief Class that performs lookup for an identifier stored in an AST file.
    118 class ASTIdentifierLookupTrait : public ASTIdentifierLookupTraitBase {
    119   ASTReader &Reader;
    120   ModuleFile &F;
    121 
    122   // If we know the IdentifierInfo in advance, it is here and we will
    123   // not build a new one. Used when deserializing information about an
    124   // identifier that was constructed before the AST file was read.
    125   IdentifierInfo *KnownII;
    126 
    127 public:
    128   typedef IdentifierInfo * data_type;
    129 
    130   ASTIdentifierLookupTrait(ASTReader &Reader, ModuleFile &F,
    131                            IdentifierInfo *II = 0)
    132     : Reader(Reader), F(F), KnownII(II) { }
    133 
    134   data_type ReadData(const internal_key_type& k,
    135                      const unsigned char* d,
    136                      unsigned DataLen);
    137 
    138   ASTReader &getReader() const { return Reader; }
    139 };
    140 
    141 /// \brief The on-disk hash table used to contain information about
    142 /// all of the identifiers in the program.
    143 typedef OnDiskChainedHashTable<ASTIdentifierLookupTrait>
    144   ASTIdentifierLookupTable;
    145 
    146 /// \brief Class that performs lookup for a selector's entries in the global
    147 /// method pool stored in an AST file.
    148 class ASTSelectorLookupTrait {
    149   ASTReader &Reader;
    150   ModuleFile &F;
    151 
    152 public:
    153   struct data_type {
    154     SelectorID ID;
    155     unsigned InstanceBits;
    156     unsigned FactoryBits;
    157     SmallVector<ObjCMethodDecl *, 2> Instance;
    158     SmallVector<ObjCMethodDecl *, 2> Factory;
    159   };
    160 
    161   typedef Selector external_key_type;
    162   typedef external_key_type internal_key_type;
    163 
    164   ASTSelectorLookupTrait(ASTReader &Reader, ModuleFile &F)
    165     : Reader(Reader), F(F) { }
    166 
    167   static bool EqualKey(const internal_key_type& a,
    168                        const internal_key_type& b) {
    169     return a == b;
    170   }
    171 
    172   static unsigned ComputeHash(Selector Sel);
    173 
    174   static const internal_key_type&
    175   GetInternalKey(const external_key_type& x) { return x; }
    176 
    177   static std::pair<unsigned, unsigned>
    178   ReadKeyDataLength(const unsigned char*& d);
    179 
    180   internal_key_type ReadKey(const unsigned char* d, unsigned);
    181   data_type ReadData(Selector, const unsigned char* d, unsigned DataLen);
    182 };
    183 
    184 /// \brief The on-disk hash table used for the global method pool.
    185 typedef OnDiskChainedHashTable<ASTSelectorLookupTrait>
    186   ASTSelectorLookupTable;
    187 
    188 /// \brief Trait class used to search the on-disk hash table containing all of
    189 /// the header search information.
    190 ///
    191 /// The on-disk hash table contains a mapping from each header path to
    192 /// information about that header (how many times it has been included, its
    193 /// controlling macro, etc.). Note that we actually hash based on the
    194 /// filename, and support "deep" comparisons of file names based on current
    195 /// inode numbers, so that the search can cope with non-normalized path names
    196 /// and symlinks.
    197 class HeaderFileInfoTrait {
    198   ASTReader &Reader;
    199   ModuleFile &M;
    200   HeaderSearch *HS;
    201   const char *FrameworkStrings;
    202 
    203 public:
    204   typedef const FileEntry *external_key_type;
    205 
    206   struct internal_key_type {
    207     off_t Size;
    208     time_t ModTime;
    209     const char *Filename;
    210   };
    211   typedef const internal_key_type &internal_key_ref;
    212 
    213   typedef HeaderFileInfo data_type;
    214 
    215   HeaderFileInfoTrait(ASTReader &Reader, ModuleFile &M, HeaderSearch *HS,
    216                       const char *FrameworkStrings)
    217   : Reader(Reader), M(M), HS(HS), FrameworkStrings(FrameworkStrings) { }
    218 
    219   static unsigned ComputeHash(internal_key_ref ikey);
    220   static internal_key_type GetInternalKey(const FileEntry *FE);
    221   bool EqualKey(internal_key_ref a, internal_key_ref b);
    222 
    223   static std::pair<unsigned, unsigned>
    224   ReadKeyDataLength(const unsigned char*& d);
    225 
    226   static internal_key_type ReadKey(const unsigned char *d, unsigned);
    227 
    228   data_type ReadData(internal_key_ref,const unsigned char *d, unsigned DataLen);
    229 };
    230 
    231 /// \brief The on-disk hash table used for known header files.
    232 typedef OnDiskChainedHashTable<HeaderFileInfoTrait>
    233   HeaderFileInfoLookupTable;
    234 
    235 } // end namespace clang::serialization::reader
    236 } // end namespace clang::serialization
    237 } // end namespace clang
    238 
    239 
    240 #endif
    241