ASTReaderInternals.h 8.0 KB

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