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     SmallVector<ObjCMethodDecl *, 2> Instance;
    156     SmallVector<ObjCMethodDecl *, 2> Factory;
    157   };
    158 
    159   typedef Selector external_key_type;
    160   typedef external_key_type internal_key_type;
    161 
    162   ASTSelectorLookupTrait(ASTReader &Reader, ModuleFile &F)
    163     : Reader(Reader), F(F) { }
    164 
    165   static bool EqualKey(const internal_key_type& a,
    166                        const internal_key_type& b) {
    167     return a == b;
    168   }
    169 
    170   static unsigned ComputeHash(Selector Sel);
    171 
    172   static const internal_key_type&
    173   GetInternalKey(const external_key_type& x) { return x; }
    174 
    175   static std::pair<unsigned, unsigned>
    176   ReadKeyDataLength(const unsigned char*& d);
    177 
    178   internal_key_type ReadKey(const unsigned char* d, unsigned);
    179   data_type ReadData(Selector, const unsigned char* d, unsigned DataLen);
    180 };
    181 
    182 /// \brief The on-disk hash table used for the global method pool.
    183 typedef OnDiskChainedHashTable<ASTSelectorLookupTrait>
    184   ASTSelectorLookupTable;
    185 
    186 /// \brief Trait class used to search the on-disk hash table containing all of
    187 /// the header search information.
    188 ///
    189 /// The on-disk hash table contains a mapping from each header path to
    190 /// information about that header (how many times it has been included, its
    191 /// controlling macro, etc.). Note that we actually hash based on the
    192 /// filename, and support "deep" comparisons of file names based on current
    193 /// inode numbers, so that the search can cope with non-normalized path names
    194 /// and symlinks.
    195 class HeaderFileInfoTrait {
    196   ASTReader &Reader;
    197   ModuleFile &M;
    198   HeaderSearch *HS;
    199   const char *FrameworkStrings;
    200 
    201 public:
    202   typedef const FileEntry *external_key_type;
    203 
    204   struct internal_key_type {
    205     off_t Size;
    206     time_t ModTime;
    207     const char *Filename;
    208   };
    209   typedef const internal_key_type &internal_key_ref;
    210 
    211   typedef HeaderFileInfo data_type;
    212 
    213   HeaderFileInfoTrait(ASTReader &Reader, ModuleFile &M, HeaderSearch *HS,
    214                       const char *FrameworkStrings)
    215   : Reader(Reader), M(M), HS(HS), FrameworkStrings(FrameworkStrings) { }
    216 
    217   static unsigned ComputeHash(internal_key_ref ikey);
    218   static internal_key_type GetInternalKey(const FileEntry *FE);
    219   bool EqualKey(internal_key_ref a, internal_key_ref b);
    220 
    221   static std::pair<unsigned, unsigned>
    222   ReadKeyDataLength(const unsigned char*& d);
    223 
    224   static internal_key_type ReadKey(const unsigned char *d, unsigned);
    225 
    226   data_type ReadData(internal_key_ref,const unsigned char *d, unsigned DataLen);
    227 };
    228 
    229 /// \brief The on-disk hash table used for known header files.
    230 typedef OnDiskChainedHashTable<HeaderFileInfoTrait>
    231   HeaderFileInfoLookupTable;
    232 
    233 } // end namespace clang::serialization::reader
    234 } // end namespace clang::serialization
    235 } // end namespace clang
    236 
    237 
    238 #endif
    239