blob: 327da4403a3a48fc270388c6e054e283f684236f [file] [log] [blame]
Douglas Gregor98339b92011-08-25 20:47:51 +00001//===--- 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
Douglas Gregor98339b92011-08-25 20:47:51 +000016#include "clang/AST/DeclarationName.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000017#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor479633c2013-01-23 18:53:14 +000018#include "clang/Serialization/ASTBitCodes.h"
Douglas Gregor9b8b20f2012-01-06 16:09:53 +000019#include "llvm/Support/Endian.h"
Douglas Gregor98339b92011-08-25 20:47:51 +000020#include <sys/stat.h>
Chandler Carruth55fc8732012-12-04 09:13:33 +000021#include <utility>
Douglas Gregor98339b92011-08-25 20:47:51 +000022
23namespace clang {
24
25class ASTReader;
26class HeaderSearch;
27struct HeaderFileInfo;
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +000028class FileEntry;
Douglas Gregor98339b92011-08-25 20:47:51 +000029
30namespace serialization {
31
Douglas Gregor1a4761e2011-11-30 23:21:26 +000032class ModuleFile;
Douglas Gregor98339b92011-08-25 20:47:51 +000033
34namespace reader {
35
36/// \brief Class that performs name lookup into a DeclContext stored
37/// in an AST file.
38class ASTDeclContextNameLookupTrait {
39 ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +000040 ModuleFile &F;
Douglas Gregor98339b92011-08-25 20:47:51 +000041
42public:
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
Douglas Gregor9b8b20f2012-01-06 16:09:53 +000047 typedef llvm::support::ulittle32_t LE32DeclID;
48 typedef std::pair<LE32DeclID *, LE32DeclID *> data_type;
Douglas Gregor98339b92011-08-25 20:47:51 +000049
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
Nick Lewyckyb346d2f2012-04-16 02:51:46 +000062 explicit ASTDeclContextNameLookupTrait(ASTReader &Reader, ModuleFile &F)
Douglas Gregor98339b92011-08-25 20:47:51 +000063 : 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;
Douglas Gregor98339b92011-08-25 20:47:51 +000072
Nick Lewyckyb346d2f2012-04-16 02:51:46 +000073 static std::pair<unsigned, unsigned>
Douglas Gregor98339b92011-08-25 20:47:51 +000074 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
Douglas Gregor479633c2013-01-23 18:53:14 +000082/// \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.
91class ASTIdentifierLookupTraitBase {
92public:
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
Douglas Gregor98339b92011-08-25 20:47:51 +0000117/// \brief Class that performs lookup for an identifier stored in an AST file.
Douglas Gregor479633c2013-01-23 18:53:14 +0000118class ASTIdentifierLookupTrait : public ASTIdentifierLookupTraitBase {
Douglas Gregor98339b92011-08-25 20:47:51 +0000119 ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000120 ModuleFile &F;
Douglas Gregor98339b92011-08-25 20:47:51 +0000121
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
127public:
128 typedef IdentifierInfo * data_type;
129
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000130 ASTIdentifierLookupTrait(ASTReader &Reader, ModuleFile &F,
Douglas Gregor98339b92011-08-25 20:47:51 +0000131 IdentifierInfo *II = 0)
132 : Reader(Reader), F(F), KnownII(II) { }
Douglas Gregor479633c2013-01-23 18:53:14 +0000133
134 data_type ReadData(const internal_key_type& k,
135 const unsigned char* d,
136 unsigned DataLen);
Douglas Gregor5d5051f2012-01-24 15:24:38 +0000137
138 ASTReader &getReader() const { return Reader; }
Douglas Gregor98339b92011-08-25 20:47:51 +0000139};
140
141/// \brief The on-disk hash table used to contain information about
142/// all of the identifiers in the program.
143typedef 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.
148class ASTSelectorLookupTrait {
149 ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000150 ModuleFile &F;
Douglas Gregor98339b92011-08-25 20:47:51 +0000151
152public:
153 struct data_type {
154 SelectorID ID;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000155 SmallVector<ObjCMethodDecl *, 2> Instance;
156 SmallVector<ObjCMethodDecl *, 2> Factory;
Douglas Gregor98339b92011-08-25 20:47:51 +0000157 };
158
159 typedef Selector external_key_type;
160 typedef external_key_type internal_key_type;
161
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000162 ASTSelectorLookupTrait(ASTReader &Reader, ModuleFile &F)
Douglas Gregor98339b92011-08-25 20:47:51 +0000163 : 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&
Chris Lattnera2f4ae82011-09-10 16:13:42 +0000173 GetInternalKey(const external_key_type& x) { return x; }
Douglas Gregor98339b92011-08-25 20:47:51 +0000174
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.
183typedef 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.
195class HeaderFileInfoTrait {
196 ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000197 ModuleFile &M;
Douglas Gregor98339b92011-08-25 20:47:51 +0000198 HeaderSearch *HS;
199 const char *FrameworkStrings;
Ted Kremenek4fd83a32013-02-05 06:21:59 +0000200
Douglas Gregor98339b92011-08-25 20:47:51 +0000201public:
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +0000202 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;
Douglas Gregor98339b92011-08-25 20:47:51 +0000210
211 typedef HeaderFileInfo data_type;
212
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000213 HeaderFileInfoTrait(ASTReader &Reader, ModuleFile &M, HeaderSearch *HS,
Argyrios Kyrtzidis8bd50b12013-03-06 18:12:41 +0000214 const char *FrameworkStrings)
215 : Reader(Reader), M(M), HS(HS), FrameworkStrings(FrameworkStrings) { }
Douglas Gregor98339b92011-08-25 20:47:51 +0000216
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +0000217 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);
Douglas Gregor98339b92011-08-25 20:47:51 +0000220
221 static std::pair<unsigned, unsigned>
222 ReadKeyDataLength(const unsigned char*& d);
223
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +0000224 static internal_key_type ReadKey(const unsigned char *d, unsigned);
Douglas Gregor98339b92011-08-25 20:47:51 +0000225
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +0000226 data_type ReadData(internal_key_ref,const unsigned char *d, unsigned DataLen);
Douglas Gregor98339b92011-08-25 20:47:51 +0000227};
228
229/// \brief The on-disk hash table used for known header files.
230typedef OnDiskChainedHashTable<HeaderFileInfoTrait>
231 HeaderFileInfoLookupTable;
232
233} // end namespace clang::serialization::reader
234} // end namespace clang::serialization
235} // end namespace clang
236
237
238#endif