blob: 60eeb6c3085da1aafddce85799f7c2a26ee072f5 [file] [log] [blame]
Sebastian Redl3b3c8742010-08-18 23:57:11 +00001//===--- ASTReader.cpp - AST File Reader ------------------------*- C++ -*-===//
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002//
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//
Sebastian Redl2c499f62010-08-18 23:56:43 +000010// This file defines the ASTReader class, which reads AST files.
Douglas Gregoref84c4b2009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
Chris Lattner92ba5ff2009-04-27 05:14:47 +000013
Sebastian Redlf5b13462010-08-18 23:57:17 +000014#include "clang/Serialization/ASTReader.h"
15#include "clang/Serialization/ASTDeserializationListener.h"
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +000016#include "ASTCommon.h"
Douglas Gregor55abb232009-04-10 20:39:37 +000017#include "clang/Frontend/FrontendDiagnostic.h"
Daniel Dunbar732ef8a2009-11-11 23:58:53 +000018#include "clang/Frontend/Utils.h"
Douglas Gregorc3a6ade2010-08-12 20:07:10 +000019#include "clang/Sema/Sema.h"
John McCallcc14d1f2010-08-24 08:50:51 +000020#include "clang/Sema/Scope.h"
Douglas Gregor1a0d0b92009-04-14 00:24:19 +000021#include "clang/AST/ASTConsumer.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000022#include "clang/AST/ASTContext.h"
John McCall19c1bfd2010-08-25 05:32:35 +000023#include "clang/AST/DeclTemplate.h"
Douglas Gregorfeb84b02009-04-14 21:18:50 +000024#include "clang/AST/Expr.h"
John McCallbfd822c2010-08-24 07:32:53 +000025#include "clang/AST/ExprCXX.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000026#include "clang/AST/Type.h"
John McCall8f115c62009-10-16 21:56:05 +000027#include "clang/AST/TypeLocVisitor.h"
Chris Lattner34321bc2009-04-10 21:41:48 +000028#include "clang/Lex/MacroInfo.h"
Douglas Gregoraae92242010-03-19 21:51:54 +000029#include "clang/Lex/PreprocessingRecord.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000030#include "clang/Lex/Preprocessor.h"
Steve Naroff3fa455a2009-04-24 20:03:17 +000031#include "clang/Lex/HeaderSearch.h"
Douglas Gregora868bbd2009-04-21 22:25:48 +000032#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000033#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000034#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000035#include "clang/Basic/FileManager.h"
Chris Lattner226efd32010-11-23 19:19:34 +000036#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000037#include "clang/Basic/TargetInfo.h"
Douglas Gregord54f3a12009-10-05 21:07:28 +000038#include "clang/Basic/Version.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000039#include "llvm/ADT/StringExtras.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000040#include "llvm/Bitcode/BitstreamReader.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000041#include "llvm/Support/MemoryBuffer.h"
John McCall0ad16662009-10-29 08:12:44 +000042#include "llvm/Support/ErrorHandling.h"
Douglas Gregor09b69892011-02-10 17:09:37 +000043#include "llvm/Support/FileSystem.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000044#include "llvm/Support/Path.h"
Michael J. Spencerf25faaa2010-12-09 17:36:38 +000045#include "llvm/Support/system_error.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000046#include <algorithm>
Douglas Gregorc379c072009-04-28 18:58:38 +000047#include <iterator>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000048#include <cstdio>
Douglas Gregorc5046832009-04-27 18:38:38 +000049#include <sys/stat.h>
Douglas Gregor09b69892011-02-10 17:09:37 +000050
Douglas Gregoref84c4b2009-04-09 22:27:44 +000051using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000052using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000053
54//===----------------------------------------------------------------------===//
Sebastian Redld44cd6a2010-08-18 23:57:06 +000055// PCH validator implementation
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000056//===----------------------------------------------------------------------===//
57
Sebastian Redl3e31c722010-08-18 23:56:56 +000058ASTReaderListener::~ASTReaderListener() {}
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000059
60bool
61PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts) {
62 const LangOptions &PPLangOpts = PP.getLangOptions();
63#define PARSE_LANGOPT_BENIGN(Option)
64#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
65 if (PPLangOpts.Option != LangOpts.Option) { \
66 Reader.Diag(DiagID) << LangOpts.Option << PPLangOpts.Option; \
67 return true; \
68 }
69
70 PARSE_LANGOPT_BENIGN(Trigraphs);
71 PARSE_LANGOPT_BENIGN(BCPLComment);
72 PARSE_LANGOPT_BENIGN(DollarIdents);
73 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
74 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
Chandler Carruthe03aa552010-04-17 20:17:31 +000075 PARSE_LANGOPT_IMPORTANT(GNUKeywords, diag::warn_pch_gnu_keywords);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000076 PARSE_LANGOPT_BENIGN(ImplicitInt);
77 PARSE_LANGOPT_BENIGN(Digraphs);
78 PARSE_LANGOPT_BENIGN(HexFloats);
79 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
80 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
Michael J. Spencer4992ca4b2010-10-21 05:21:48 +000081 PARSE_LANGOPT_BENIGN(MSCVersion);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000082 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
83 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
84 PARSE_LANGOPT_BENIGN(CXXOperatorName);
85 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
86 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
87 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
Fariborz Jahanian45878032010-02-09 19:31:38 +000088 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI2, diag::warn_pch_nonfragile_abi2);
Fariborz Jahanian13f3b2f2011-01-07 18:59:25 +000089 PARSE_LANGOPT_IMPORTANT(AppleKext, diag::warn_pch_apple_kext);
Ted Kremenek1d56c9e2010-12-23 21:35:43 +000090 PARSE_LANGOPT_IMPORTANT(ObjCDefaultSynthProperties,
91 diag::warn_pch_objc_auto_properties);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +000092 PARSE_LANGOPT_IMPORTANT(NoConstantCFStrings,
Fariborz Jahanian62c56022010-04-22 21:01:59 +000093 diag::warn_pch_no_constant_cfstrings);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000094 PARSE_LANGOPT_BENIGN(PascalStrings);
95 PARSE_LANGOPT_BENIGN(WritableStrings);
Mike Stump11289f42009-09-09 15:08:12 +000096 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000097 diag::warn_pch_lax_vector_conversions);
Nate Begeman9d905792009-06-25 22:57:40 +000098 PARSE_LANGOPT_IMPORTANT(AltiVec, diag::warn_pch_altivec);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000099 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
Daniel Dunbar925152c2010-02-10 18:48:44 +0000100 PARSE_LANGOPT_IMPORTANT(SjLjExceptions, diag::warn_pch_sjlj_exceptions);
Douglas Gregordbe39272011-02-01 15:15:22 +0000101 PARSE_LANGOPT_IMPORTANT(MSBitfields, diag::warn_pch_ms_bitfields);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000102 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
103 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
104 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
Mike Stump11289f42009-09-09 15:08:12 +0000105 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000106 diag::warn_pch_thread_safe_statics);
Daniel Dunbara77eaeb2009-09-03 04:54:28 +0000107 PARSE_LANGOPT_IMPORTANT(POSIXThreads, diag::warn_pch_posix_threads);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000108 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
109 PARSE_LANGOPT_BENIGN(EmitAllDecls);
110 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
Chris Lattner51924e512010-06-26 21:25:03 +0000111 PARSE_LANGOPT_BENIGN(getSignedOverflowBehavior());
Mike Stump11289f42009-09-09 15:08:12 +0000112 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000113 diag::warn_pch_heinous_extensions);
114 // FIXME: Most of the options below are benign if the macro wasn't
115 // used. Unfortunately, this means that a PCH compiled without
116 // optimization can't be used with optimization turned on, even
117 // though the only thing that changes is whether __OPTIMIZE__ was
118 // defined... but if __OPTIMIZE__ never showed up in the header, it
119 // doesn't matter. We could consider making this some special kind
120 // of check.
121 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
122 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
123 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
124 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
125 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
126 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
127 PARSE_LANGOPT_IMPORTANT(AccessControl, diag::warn_pch_access_control);
128 PARSE_LANGOPT_IMPORTANT(CharIsSigned, diag::warn_pch_char_signed);
John Thompsoned4e2952009-11-05 20:14:16 +0000129 PARSE_LANGOPT_IMPORTANT(ShortWChar, diag::warn_pch_short_wchar);
Argyrios Kyrtzidis74825bc2010-10-08 00:25:19 +0000130 PARSE_LANGOPT_IMPORTANT(ShortEnums, diag::warn_pch_short_enums);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000131 if ((PPLangOpts.getGCMode() != 0) != (LangOpts.getGCMode() != 0)) {
Mike Stump11289f42009-09-09 15:08:12 +0000132 Reader.Diag(diag::warn_pch_gc_mode)
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000133 << LangOpts.getGCMode() << PPLangOpts.getGCMode();
134 return true;
135 }
136 PARSE_LANGOPT_BENIGN(getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +0000137 PARSE_LANGOPT_IMPORTANT(getStackProtectorMode(),
138 diag::warn_pch_stack_protector);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000139 PARSE_LANGOPT_BENIGN(InstantiationDepth);
Nate Begeman9d905792009-06-25 22:57:40 +0000140 PARSE_LANGOPT_IMPORTANT(OpenCL, diag::warn_pch_opencl);
Peter Collingbourne546d0792010-12-01 19:14:57 +0000141 PARSE_LANGOPT_IMPORTANT(CUDA, diag::warn_pch_cuda);
Mike Stumpd9546382009-12-12 01:27:46 +0000142 PARSE_LANGOPT_BENIGN(CatchUndefined);
Daniel Dunbar143021e2009-09-21 04:16:19 +0000143 PARSE_LANGOPT_IMPORTANT(ElideConstructors, diag::warn_pch_elide_constructors);
Douglas Gregor8ed0c0b2010-07-09 17:35:33 +0000144 PARSE_LANGOPT_BENIGN(SpellChecking);
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +0000145#undef PARSE_LANGOPT_IMPORTANT
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000146#undef PARSE_LANGOPT_BENIGN
147
148 return false;
149}
150
Daniel Dunbar20a682d2009-11-11 00:52:11 +0000151bool PCHValidator::ReadTargetTriple(llvm::StringRef Triple) {
152 if (Triple == PP.getTargetInfo().getTriple().str())
153 return false;
154
155 Reader.Diag(diag::warn_pch_target_triple)
156 << Triple << PP.getTargetInfo().getTriple().str();
157 return true;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000158}
159
Benjamin Kramer90b5b682010-11-25 18:29:30 +0000160namespace {
161 struct EmptyStringRef {
162 bool operator ()(llvm::StringRef r) const { return r.empty(); }
163 };
164 struct EmptyBlock {
165 bool operator ()(const PCHPredefinesBlock &r) const {return r.Data.empty();}
166 };
167}
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000168
169static bool EqualConcatenations(llvm::SmallVector<llvm::StringRef, 2> L,
170 PCHPredefinesBlocks R) {
171 // First, sum up the lengths.
172 unsigned LL = 0, RL = 0;
173 for (unsigned I = 0, N = L.size(); I != N; ++I) {
174 LL += L[I].size();
175 }
176 for (unsigned I = 0, N = R.size(); I != N; ++I) {
177 RL += R[I].Data.size();
178 }
179 if (LL != RL)
180 return false;
181 if (LL == 0 && RL == 0)
182 return true;
183
184 // Kick out empty parts, they confuse the algorithm below.
185 L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end());
186 R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end());
187
188 // Do it the hard way. At this point, both vectors must be non-empty.
189 llvm::StringRef LR = L[0], RR = R[0].Data;
190 unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size();
Daniel Dunbar01ad0a72010-07-16 00:00:11 +0000191 (void) RN;
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000192 for (;;) {
193 // Compare the current pieces.
194 if (LR.size() == RR.size()) {
195 // If they're the same length, it's pretty easy.
196 if (LR != RR)
197 return false;
198 // Both pieces are done, advance.
199 ++LI;
200 ++RI;
201 // If either string is done, they're both done, since they're the same
202 // length.
203 if (LI == LN) {
204 assert(RI == RN && "Strings not the same length after all?");
205 return true;
206 }
207 LR = L[LI];
208 RR = R[RI].Data;
209 } else if (LR.size() < RR.size()) {
210 // Right piece is longer.
211 if (!RR.startswith(LR))
212 return false;
213 ++LI;
214 assert(LI != LN && "Strings not the same length after all?");
215 RR = RR.substr(LR.size());
216 LR = L[LI];
217 } else {
218 // Left piece is longer.
219 if (!LR.startswith(RR))
220 return false;
221 ++RI;
222 assert(RI != RN && "Strings not the same length after all?");
223 LR = LR.substr(RR.size());
224 RR = R[RI].Data;
225 }
226 }
227}
228
229static std::pair<FileID, llvm::StringRef::size_type>
230FindMacro(const PCHPredefinesBlocks &Buffers, llvm::StringRef MacroDef) {
231 std::pair<FileID, llvm::StringRef::size_type> Res;
232 for (unsigned I = 0, N = Buffers.size(); I != N; ++I) {
233 Res.second = Buffers[I].Data.find(MacroDef);
234 if (Res.second != llvm::StringRef::npos) {
235 Res.first = Buffers[I].BufferID;
236 break;
237 }
238 }
239 return Res;
240}
241
242bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000243 llvm::StringRef OriginalFileName,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000244 std::string &SuggestedPredefines) {
Daniel Dunbar732ef8a2009-11-11 23:58:53 +0000245 // We are in the context of an implicit include, so the predefines buffer will
246 // have a #include entry for the PCH file itself (as normalized by the
247 // preprocessor initialization). Find it and skip over it in the checking
248 // below.
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000249 llvm::SmallString<256> PCHInclude;
250 PCHInclude += "#include \"";
Daniel Dunbar732ef8a2009-11-11 23:58:53 +0000251 PCHInclude += NormalizeDashIncludePath(OriginalFileName);
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000252 PCHInclude += "\"\n";
253 std::pair<llvm::StringRef,llvm::StringRef> Split =
254 llvm::StringRef(PP.getPredefines()).split(PCHInclude.str());
255 llvm::StringRef Left = Split.first, Right = Split.second;
Ted Kremenek1ff615c2010-03-18 00:56:54 +0000256 if (Left == PP.getPredefines()) {
257 Error("Missing PCH include entry!");
258 return true;
259 }
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000260
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000261 // If the concatenation of all the PCH buffers is equal to the adjusted
262 // command line, we're done.
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000263 llvm::SmallVector<llvm::StringRef, 2> CommandLine;
264 CommandLine.push_back(Left);
265 CommandLine.push_back(Right);
266 if (EqualConcatenations(CommandLine, Buffers))
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000267 return false;
268
269 SourceManager &SourceMgr = PP.getSourceManager();
Mike Stump11289f42009-09-09 15:08:12 +0000270
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000271 // The predefines buffers are different. Determine what the differences are,
272 // and whether they require us to reject the PCH file.
Daniel Dunbar045f917e2009-11-13 16:46:11 +0000273 llvm::SmallVector<llvm::StringRef, 8> PCHLines;
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000274 for (unsigned I = 0, N = Buffers.size(); I != N; ++I)
275 Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Daniel Dunbar045f917e2009-11-13 16:46:11 +0000276
277 llvm::SmallVector<llvm::StringRef, 8> CmdLineLines;
278 Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Argyrios Kyrtzidis58c65412010-09-30 16:53:50 +0000279
280 // Pick out implicit #includes after the PCH and don't consider them for
281 // validation; we will insert them into SuggestedPredefines so that the
282 // preprocessor includes them.
283 std::string IncludesAfterPCH;
284 llvm::SmallVector<llvm::StringRef, 8> AfterPCHLines;
285 Right.split(AfterPCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
286 for (unsigned i = 0, e = AfterPCHLines.size(); i != e; ++i) {
287 if (AfterPCHLines[i].startswith("#include ")) {
288 IncludesAfterPCH += AfterPCHLines[i];
289 IncludesAfterPCH += '\n';
290 } else {
291 CmdLineLines.push_back(AfterPCHLines[i]);
292 }
293 }
294
295 // Make sure we add the includes last into SuggestedPredefines before we
296 // exit this function.
297 struct AddIncludesRAII {
298 std::string &SuggestedPredefines;
299 std::string &IncludesAfterPCH;
300
301 AddIncludesRAII(std::string &SuggestedPredefines,
302 std::string &IncludesAfterPCH)
303 : SuggestedPredefines(SuggestedPredefines),
304 IncludesAfterPCH(IncludesAfterPCH) { }
305 ~AddIncludesRAII() {
306 SuggestedPredefines += IncludesAfterPCH;
307 }
308 } AddIncludes(SuggestedPredefines, IncludesAfterPCH);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000309
Daniel Dunbar499baed2009-11-11 05:26:28 +0000310 // Sort both sets of predefined buffer lines, since we allow some extra
311 // definitions and they may appear at any point in the output.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000312 std::sort(CmdLineLines.begin(), CmdLineLines.end());
313 std::sort(PCHLines.begin(), PCHLines.end());
314
Daniel Dunbar499baed2009-11-11 05:26:28 +0000315 // Determine which predefines that were used to build the PCH file are missing
316 // from the command line.
317 std::vector<llvm::StringRef> MissingPredefines;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000318 std::set_difference(PCHLines.begin(), PCHLines.end(),
319 CmdLineLines.begin(), CmdLineLines.end(),
320 std::back_inserter(MissingPredefines));
321
322 bool MissingDefines = false;
323 bool ConflictingDefines = false;
324 for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) {
Daniel Dunbar499baed2009-11-11 05:26:28 +0000325 llvm::StringRef Missing = MissingPredefines[I];
Argyrios Kyrtzidis58c65412010-09-30 16:53:50 +0000326 if (Missing.startswith("#include ")) {
327 // An -include was specified when generating the PCH; it is included in
328 // the PCH, just ignore it.
329 continue;
330 }
Daniel Dunbar499baed2009-11-11 05:26:28 +0000331 if (!Missing.startswith("#define ")) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000332 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
333 return true;
334 }
Mike Stump11289f42009-09-09 15:08:12 +0000335
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000336 // This is a macro definition. Determine the name of the macro we're
337 // defining.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000338 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump11289f42009-09-09 15:08:12 +0000339 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000340 = Missing.find_first_of("( \n\r", StartOfMacroName);
341 assert(EndOfMacroName != std::string::npos &&
342 "Couldn't find the end of the macro name");
Daniel Dunbar499baed2009-11-11 05:26:28 +0000343 llvm::StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000344
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000345 // Determine whether this macro was given a different definition on the
346 // command line.
Daniel Dunbar499baed2009-11-11 05:26:28 +0000347 std::string MacroDefStart = "#define " + MacroName.str();
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000348 std::string::size_type MacroDefLen = MacroDefStart.size();
Daniel Dunbar045f917e2009-11-13 16:46:11 +0000349 llvm::SmallVector<llvm::StringRef, 8>::iterator ConflictPos
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000350 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
351 MacroDefStart);
352 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000353 if (!ConflictPos->startswith(MacroDefStart)) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000354 // Different macro; we're done.
355 ConflictPos = CmdLineLines.end();
Mike Stump11289f42009-09-09 15:08:12 +0000356 break;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000357 }
Mike Stump11289f42009-09-09 15:08:12 +0000358
359 assert(ConflictPos->size() > MacroDefLen &&
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000360 "Invalid #define in predefines buffer?");
Mike Stump11289f42009-09-09 15:08:12 +0000361 if ((*ConflictPos)[MacroDefLen] != ' ' &&
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000362 (*ConflictPos)[MacroDefLen] != '(')
363 continue; // Longer macro name; keep trying.
Mike Stump11289f42009-09-09 15:08:12 +0000364
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000365 // We found a conflicting macro definition.
366 break;
367 }
Mike Stump11289f42009-09-09 15:08:12 +0000368
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000369 if (ConflictPos != CmdLineLines.end()) {
370 Reader.Diag(diag::warn_cmdline_conflicting_macro_def)
371 << MacroName;
372
373 // Show the definition of this macro within the PCH file.
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000374 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
375 FindMacro(Buffers, Missing);
376 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
377 SourceLocation PCHMissingLoc =
378 SourceMgr.getLocForStartOfFile(MacroLoc.first)
379 .getFileLocWithOffset(MacroLoc.second);
Daniel Dunbar499baed2009-11-11 05:26:28 +0000380 Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000381
382 ConflictingDefines = true;
383 continue;
384 }
Mike Stump11289f42009-09-09 15:08:12 +0000385
Daniel Dunbar8665c7e2009-11-11 03:45:59 +0000386 // If the macro doesn't conflict, then we'll just pick up the macro
387 // definition from the PCH file. Warn the user that they made a mistake.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000388 if (ConflictingDefines)
389 continue; // Don't complain if there are already conflicting defs
Mike Stump11289f42009-09-09 15:08:12 +0000390
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000391 if (!MissingDefines) {
392 Reader.Diag(diag::warn_cmdline_missing_macro_defs);
393 MissingDefines = true;
394 }
395
396 // Show the definition of this macro within the PCH file.
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000397 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
398 FindMacro(Buffers, Missing);
399 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
400 SourceLocation PCHMissingLoc =
401 SourceMgr.getLocForStartOfFile(MacroLoc.first)
402 .getFileLocWithOffset(MacroLoc.second);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000403 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
404 }
Mike Stump11289f42009-09-09 15:08:12 +0000405
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000406 if (ConflictingDefines)
407 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000408
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000409 // Determine what predefines were introduced based on command-line
410 // parameters that were not present when building the PCH
411 // file. Extra #defines are okay, so long as the identifiers being
412 // defined were not used within the precompiled header.
Daniel Dunbar499baed2009-11-11 05:26:28 +0000413 std::vector<llvm::StringRef> ExtraPredefines;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000414 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
415 PCHLines.begin(), PCHLines.end(),
Mike Stump11289f42009-09-09 15:08:12 +0000416 std::back_inserter(ExtraPredefines));
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000417 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
Daniel Dunbar499baed2009-11-11 05:26:28 +0000418 llvm::StringRef &Extra = ExtraPredefines[I];
419 if (!Extra.startswith("#define ")) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000420 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
421 return true;
422 }
423
424 // This is an extra macro definition. Determine the name of the
425 // macro we're defining.
426 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump11289f42009-09-09 15:08:12 +0000427 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000428 = Extra.find_first_of("( \n\r", StartOfMacroName);
429 assert(EndOfMacroName != std::string::npos &&
430 "Couldn't find the end of the macro name");
Daniel Dunbar499baed2009-11-11 05:26:28 +0000431 llvm::StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000432
433 // Check whether this name was used somewhere in the PCH file. If
434 // so, defining it as a macro could change behavior, so we reject
435 // the PCH file.
Daniel Dunbar499baed2009-11-11 05:26:28 +0000436 if (IdentifierInfo *II = Reader.get(MacroName)) {
Daniel Dunbar045c92f2009-11-11 00:52:00 +0000437 Reader.Diag(diag::warn_macro_name_used_in_pch) << II;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000438 return true;
439 }
440
441 // Add this definition to the suggested predefines buffer.
442 SuggestedPredefines += Extra;
443 SuggestedPredefines += '\n';
444 }
445
446 // If we get here, it's because the predefines buffer had compatible
447 // contents. Accept the PCH file.
448 return false;
449}
450
Douglas Gregor5712ebc2010-03-16 16:35:32 +0000451void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
452 unsigned ID) {
453 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
454 ++NumHeaderInfos;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000455}
456
457void PCHValidator::ReadCounter(unsigned Value) {
458 PP.setCounterValue(Value);
459}
460
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000461//===----------------------------------------------------------------------===//
Sebastian Redl2c499f62010-08-18 23:56:43 +0000462// AST reader implementation
Douglas Gregora868bbd2009-04-21 22:25:48 +0000463//===----------------------------------------------------------------------===//
464
Sebastian Redl07a89a82010-07-30 00:29:29 +0000465void
Sebastian Redl3e31c722010-08-18 23:56:56 +0000466ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
Sebastian Redl07a89a82010-07-30 00:29:29 +0000467 DeserializationListener = Listener;
Sebastian Redl07a89a82010-07-30 00:29:29 +0000468}
469
Chris Lattner92ba5ff2009-04-27 05:14:47 +0000470
Douglas Gregora868bbd2009-04-21 22:25:48 +0000471namespace {
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000472class ASTSelectorLookupTrait {
Sebastian Redl2c499f62010-08-18 23:56:43 +0000473 ASTReader &Reader;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000474
475public:
Sebastian Redl834bb972010-08-04 17:20:04 +0000476 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +0000477 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +0000478 ObjCMethodList Instance, Factory;
479 };
Douglas Gregorc78d3462009-04-24 21:10:55 +0000480
481 typedef Selector external_key_type;
482 typedef external_key_type internal_key_type;
483
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000484 explicit ASTSelectorLookupTrait(ASTReader &Reader) : Reader(Reader) { }
Mike Stump11289f42009-09-09 15:08:12 +0000485
Douglas Gregorc78d3462009-04-24 21:10:55 +0000486 static bool EqualKey(const internal_key_type& a,
487 const internal_key_type& b) {
488 return a == b;
489 }
Mike Stump11289f42009-09-09 15:08:12 +0000490
Douglas Gregorc78d3462009-04-24 21:10:55 +0000491 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +0000492 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000493 }
Mike Stump11289f42009-09-09 15:08:12 +0000494
Douglas Gregorc78d3462009-04-24 21:10:55 +0000495 // This hopefully will just get inlined and removed by the optimizer.
496 static const internal_key_type&
497 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump11289f42009-09-09 15:08:12 +0000498
Douglas Gregorc78d3462009-04-24 21:10:55 +0000499 static std::pair<unsigned, unsigned>
500 ReadKeyDataLength(const unsigned char*& d) {
501 using namespace clang::io;
502 unsigned KeyLen = ReadUnalignedLE16(d);
503 unsigned DataLen = ReadUnalignedLE16(d);
504 return std::make_pair(KeyLen, DataLen);
505 }
Mike Stump11289f42009-09-09 15:08:12 +0000506
Douglas Gregor95c13f52009-04-25 17:48:32 +0000507 internal_key_type ReadKey(const unsigned char* d, unsigned) {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000508 using namespace clang::io;
Chris Lattner8575daa2009-04-27 21:45:14 +0000509 SelectorTable &SelTable = Reader.getContext()->Selectors;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000510 unsigned N = ReadUnalignedLE16(d);
Mike Stump11289f42009-09-09 15:08:12 +0000511 IdentifierInfo *FirstII
Douglas Gregorc78d3462009-04-24 21:10:55 +0000512 = Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
513 if (N == 0)
514 return SelTable.getNullarySelector(FirstII);
515 else if (N == 1)
516 return SelTable.getUnarySelector(FirstII);
517
518 llvm::SmallVector<IdentifierInfo *, 16> Args;
519 Args.push_back(FirstII);
520 for (unsigned I = 1; I != N; ++I)
521 Args.push_back(Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)));
522
Douglas Gregor038c3382009-05-22 22:45:36 +0000523 return SelTable.getSelector(N, Args.data());
Douglas Gregorc78d3462009-04-24 21:10:55 +0000524 }
Mike Stump11289f42009-09-09 15:08:12 +0000525
Douglas Gregorc78d3462009-04-24 21:10:55 +0000526 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen) {
527 using namespace clang::io;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000528
529 data_type Result;
530
Sebastian Redl834bb972010-08-04 17:20:04 +0000531 Result.ID = ReadUnalignedLE32(d);
532 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
533 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
534
Douglas Gregorc78d3462009-04-24 21:10:55 +0000535 // Load instance methods
536 ObjCMethodList *Prev = 0;
537 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000538 ObjCMethodDecl *Method
Douglas Gregorc78d3462009-04-24 21:10:55 +0000539 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl834bb972010-08-04 17:20:04 +0000540 if (!Result.Instance.Method) {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000541 // This is the first method, which is the easy case.
Sebastian Redl834bb972010-08-04 17:20:04 +0000542 Result.Instance.Method = Method;
543 Prev = &Result.Instance;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000544 continue;
545 }
546
Ted Kremenekda4abf12010-02-11 00:53:01 +0000547 ObjCMethodList *Mem =
548 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
549 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000550 Prev = Prev->Next;
551 }
552
553 // Load factory methods
554 Prev = 0;
555 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000556 ObjCMethodDecl *Method
Douglas Gregorc78d3462009-04-24 21:10:55 +0000557 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl834bb972010-08-04 17:20:04 +0000558 if (!Result.Factory.Method) {
Douglas Gregorc78d3462009-04-24 21:10:55 +0000559 // This is the first method, which is the easy case.
Sebastian Redl834bb972010-08-04 17:20:04 +0000560 Result.Factory.Method = Method;
561 Prev = &Result.Factory;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000562 continue;
563 }
564
Ted Kremenekda4abf12010-02-11 00:53:01 +0000565 ObjCMethodList *Mem =
566 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
567 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000568 Prev = Prev->Next;
569 }
570
571 return Result;
572 }
573};
Mike Stump11289f42009-09-09 15:08:12 +0000574
575} // end anonymous namespace
Douglas Gregorc78d3462009-04-24 21:10:55 +0000576
577/// \brief The on-disk hash table used for the global method pool.
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000578typedef OnDiskChainedHashTable<ASTSelectorLookupTrait>
579 ASTSelectorLookupTable;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000580
Sebastian Redl2c373b92010-10-05 15:59:54 +0000581namespace clang {
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000582class ASTIdentifierLookupTrait {
Sebastian Redl2c499f62010-08-18 23:56:43 +0000583 ASTReader &Reader;
Sebastian Redl2c373b92010-10-05 15:59:54 +0000584 ASTReader::PerFileData &F;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000585
586 // If we know the IdentifierInfo in advance, it is here and we will
587 // not build a new one. Used when deserializing information about an
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000588 // identifier that was constructed before the AST file was read.
Douglas Gregora868bbd2009-04-21 22:25:48 +0000589 IdentifierInfo *KnownII;
590
591public:
592 typedef IdentifierInfo * data_type;
593
594 typedef const std::pair<const char*, unsigned> external_key_type;
595
596 typedef external_key_type internal_key_type;
597
Sebastian Redl2c373b92010-10-05 15:59:54 +0000598 ASTIdentifierLookupTrait(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redl4e6c5672010-07-21 22:31:37 +0000599 IdentifierInfo *II = 0)
Sebastian Redl2c373b92010-10-05 15:59:54 +0000600 : Reader(Reader), F(F), KnownII(II) { }
Mike Stump11289f42009-09-09 15:08:12 +0000601
Douglas Gregora868bbd2009-04-21 22:25:48 +0000602 static bool EqualKey(const internal_key_type& a,
603 const internal_key_type& b) {
604 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
605 : false;
606 }
Mike Stump11289f42009-09-09 15:08:12 +0000607
Douglas Gregora868bbd2009-04-21 22:25:48 +0000608 static unsigned ComputeHash(const internal_key_type& a) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000609 return llvm::HashString(llvm::StringRef(a.first, a.second));
Douglas Gregora868bbd2009-04-21 22:25:48 +0000610 }
Mike Stump11289f42009-09-09 15:08:12 +0000611
Douglas Gregora868bbd2009-04-21 22:25:48 +0000612 // This hopefully will just get inlined and removed by the optimizer.
613 static const internal_key_type&
614 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump11289f42009-09-09 15:08:12 +0000615
Douglas Gregor57756ea2010-10-14 22:11:03 +0000616 // This hopefully will just get inlined and removed by the optimizer.
617 static const external_key_type&
618 GetExternalKey(const internal_key_type& x) { return x; }
619
Douglas Gregora868bbd2009-04-21 22:25:48 +0000620 static std::pair<unsigned, unsigned>
621 ReadKeyDataLength(const unsigned char*& d) {
622 using namespace clang::io;
Douglas Gregor6b7bf5a2009-04-25 20:26:24 +0000623 unsigned DataLen = ReadUnalignedLE16(d);
Douglas Gregor5287b4e2009-04-25 21:04:17 +0000624 unsigned KeyLen = ReadUnalignedLE16(d);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000625 return std::make_pair(KeyLen, DataLen);
626 }
Mike Stump11289f42009-09-09 15:08:12 +0000627
Douglas Gregora868bbd2009-04-21 22:25:48 +0000628 static std::pair<const char*, unsigned>
629 ReadKey(const unsigned char* d, unsigned n) {
630 assert(n >= 2 && d[n-1] == '\0');
631 return std::make_pair((const char*) d, n-1);
632 }
Mike Stump11289f42009-09-09 15:08:12 +0000633
634 IdentifierInfo *ReadData(const internal_key_type& k,
Douglas Gregora868bbd2009-04-21 22:25:48 +0000635 const unsigned char* d,
636 unsigned DataLen) {
637 using namespace clang::io;
Sebastian Redl539c5062010-08-18 23:57:32 +0000638 IdentID ID = ReadUnalignedLE32(d);
Douglas Gregor1d583f22009-04-28 21:18:29 +0000639 bool IsInteresting = ID & 0x01;
640
641 // Wipe out the "is interesting" bit.
642 ID = ID >> 1;
643
644 if (!IsInteresting) {
Sebastian Redl98912122010-07-27 23:01:28 +0000645 // For uninteresting identifiers, just build the IdentifierInfo
Douglas Gregor1d583f22009-04-28 21:18:29 +0000646 // and associate it with the persistent ID.
647 IdentifierInfo *II = KnownII;
648 if (!II)
Sebastian Redl07a89a82010-07-30 00:29:29 +0000649 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregor1d583f22009-04-28 21:18:29 +0000650 Reader.SetIdentifierInfo(ID, II);
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000651 II->setIsFromAST();
Douglas Gregor1d583f22009-04-28 21:18:29 +0000652 return II;
653 }
654
Douglas Gregorb9256522009-04-28 21:32:13 +0000655 unsigned Bits = ReadUnalignedLE16(d);
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000656 bool CPlusPlusOperatorKeyword = Bits & 0x01;
657 Bits >>= 1;
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +0000658 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
659 Bits >>= 1;
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000660 bool Poisoned = Bits & 0x01;
661 Bits >>= 1;
662 bool ExtensionToken = Bits & 0x01;
663 Bits >>= 1;
664 bool hasMacroDefinition = Bits & 0x01;
665 Bits >>= 1;
666 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
667 Bits >>= 10;
Mike Stump11289f42009-09-09 15:08:12 +0000668
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000669 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregorb9256522009-04-28 21:32:13 +0000670 DataLen -= 6;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000671
672 // Build the IdentifierInfo itself and link the identifier ID with
673 // the new IdentifierInfo.
674 IdentifierInfo *II = KnownII;
675 if (!II)
Sebastian Redl07a89a82010-07-30 00:29:29 +0000676 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000677 Reader.SetIdentifierInfo(ID, II);
678
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000679 // Set or check the various bits in the IdentifierInfo structure.
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +0000680 // Token IDs are read-only.
681 if (HasRevertedTokenIDToIdentifier)
682 II->RevertTokenIDToIdentifier();
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000683 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
Mike Stump11289f42009-09-09 15:08:12 +0000684 assert(II->isExtensionToken() == ExtensionToken &&
Douglas Gregor4621c6a2009-04-22 18:49:13 +0000685 "Incorrect extension token flag");
686 (void)ExtensionToken;
687 II->setIsPoisoned(Poisoned);
688 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
689 "Incorrect C++ operator keyword flag");
690 (void)CPlusPlusOperatorKeyword;
691
Douglas Gregorc3366a52009-04-21 23:56:24 +0000692 // If this identifier is a macro, deserialize the macro
693 // definition.
694 if (hasMacroDefinition) {
Douglas Gregorb9256522009-04-28 21:32:13 +0000695 uint32_t Offset = ReadUnalignedLE32(d);
Douglas Gregor5ef9e332010-10-30 00:23:06 +0000696 Reader.SetIdentifierIsMacro(II, F, Offset);
Douglas Gregorb9256522009-04-28 21:32:13 +0000697 DataLen -= 4;
Douglas Gregorc3366a52009-04-21 23:56:24 +0000698 }
Douglas Gregora868bbd2009-04-21 22:25:48 +0000699
700 // Read all of the declarations visible at global scope with this
701 // name.
Chris Lattner1d728882009-04-27 22:17:41 +0000702 if (Reader.getContext() == 0) return II;
Douglas Gregor1342e842009-07-06 18:54:52 +0000703 if (DataLen > 0) {
704 llvm::SmallVector<uint32_t, 4> DeclIDs;
705 for (; DataLen > 0; DataLen -= 4)
706 DeclIDs.push_back(ReadUnalignedLE32(d));
707 Reader.SetGloballyVisibleDecls(II, DeclIDs);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000708 }
Mike Stump11289f42009-09-09 15:08:12 +0000709
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000710 II->setIsFromAST();
Douglas Gregora868bbd2009-04-21 22:25:48 +0000711 return II;
712 }
713};
Mike Stump11289f42009-09-09 15:08:12 +0000714
715} // end anonymous namespace
Douglas Gregora868bbd2009-04-21 22:25:48 +0000716
717/// \brief The on-disk hash table used to contain information about
718/// all of the identifiers in the program.
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000719typedef OnDiskChainedHashTable<ASTIdentifierLookupTrait>
720 ASTIdentifierLookupTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000721
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000722namespace {
723class ASTDeclContextNameLookupTrait {
724 ASTReader &Reader;
725
726public:
727 /// \brief Pair of begin/end iterators for DeclIDs.
728 typedef std::pair<DeclID *, DeclID *> data_type;
729
730 /// \brief Special internal key for declaration names.
731 /// The hash table creates keys for comparison; we do not create
732 /// a DeclarationName for the internal key to avoid deserializing types.
733 struct DeclNameKey {
734 DeclarationName::NameKind Kind;
735 uint64_t Data;
736 DeclNameKey() : Kind((DeclarationName::NameKind)0), Data(0) { }
737 };
738
739 typedef DeclarationName external_key_type;
740 typedef DeclNameKey internal_key_type;
741
742 explicit ASTDeclContextNameLookupTrait(ASTReader &Reader) : Reader(Reader) { }
743
744 static bool EqualKey(const internal_key_type& a,
745 const internal_key_type& b) {
746 return a.Kind == b.Kind && a.Data == b.Data;
747 }
748
749 unsigned ComputeHash(const DeclNameKey &Key) const {
750 llvm::FoldingSetNodeID ID;
751 ID.AddInteger(Key.Kind);
752
753 switch (Key.Kind) {
754 case DeclarationName::Identifier:
755 case DeclarationName::CXXLiteralOperatorName:
756 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
757 break;
758 case DeclarationName::ObjCZeroArgSelector:
759 case DeclarationName::ObjCOneArgSelector:
760 case DeclarationName::ObjCMultiArgSelector:
761 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
762 break;
763 case DeclarationName::CXXConstructorName:
764 case DeclarationName::CXXDestructorName:
765 case DeclarationName::CXXConversionFunctionName:
766 ID.AddInteger((TypeID)Key.Data);
767 break;
768 case DeclarationName::CXXOperatorName:
769 ID.AddInteger((OverloadedOperatorKind)Key.Data);
770 break;
771 case DeclarationName::CXXUsingDirective:
772 break;
773 }
774
775 return ID.ComputeHash();
776 }
777
778 internal_key_type GetInternalKey(const external_key_type& Name) const {
779 DeclNameKey Key;
780 Key.Kind = Name.getNameKind();
781 switch (Name.getNameKind()) {
782 case DeclarationName::Identifier:
783 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
784 break;
785 case DeclarationName::ObjCZeroArgSelector:
786 case DeclarationName::ObjCOneArgSelector:
787 case DeclarationName::ObjCMultiArgSelector:
788 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
789 break;
790 case DeclarationName::CXXConstructorName:
791 case DeclarationName::CXXDestructorName:
792 case DeclarationName::CXXConversionFunctionName:
793 Key.Data = Reader.GetTypeID(Name.getCXXNameType());
794 break;
795 case DeclarationName::CXXOperatorName:
796 Key.Data = Name.getCXXOverloadedOperator();
797 break;
798 case DeclarationName::CXXLiteralOperatorName:
799 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
800 break;
801 case DeclarationName::CXXUsingDirective:
802 break;
803 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000804
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000805 return Key;
806 }
807
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +0000808 external_key_type GetExternalKey(const internal_key_type& Key) const {
809 ASTContext *Context = Reader.getContext();
810 switch (Key.Kind) {
811 case DeclarationName::Identifier:
812 return DeclarationName((IdentifierInfo*)Key.Data);
813
814 case DeclarationName::ObjCZeroArgSelector:
815 case DeclarationName::ObjCOneArgSelector:
816 case DeclarationName::ObjCMultiArgSelector:
817 return DeclarationName(Selector(Key.Data));
818
819 case DeclarationName::CXXConstructorName:
820 return Context->DeclarationNames.getCXXConstructorName(
821 Context->getCanonicalType(Reader.GetType(Key.Data)));
822
823 case DeclarationName::CXXDestructorName:
824 return Context->DeclarationNames.getCXXDestructorName(
825 Context->getCanonicalType(Reader.GetType(Key.Data)));
826
827 case DeclarationName::CXXConversionFunctionName:
828 return Context->DeclarationNames.getCXXConversionFunctionName(
829 Context->getCanonicalType(Reader.GetType(Key.Data)));
830
831 case DeclarationName::CXXOperatorName:
832 return Context->DeclarationNames.getCXXOperatorName(
833 (OverloadedOperatorKind)Key.Data);
834
835 case DeclarationName::CXXLiteralOperatorName:
836 return Context->DeclarationNames.getCXXLiteralOperatorName(
837 (IdentifierInfo*)Key.Data);
838
839 case DeclarationName::CXXUsingDirective:
840 return DeclarationName::getUsingDirectiveName();
841 }
842
843 llvm_unreachable("Invalid Name Kind ?");
844 }
845
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000846 static std::pair<unsigned, unsigned>
847 ReadKeyDataLength(const unsigned char*& d) {
848 using namespace clang::io;
849 unsigned KeyLen = ReadUnalignedLE16(d);
850 unsigned DataLen = ReadUnalignedLE16(d);
851 return std::make_pair(KeyLen, DataLen);
852 }
853
854 internal_key_type ReadKey(const unsigned char* d, unsigned) {
855 using namespace clang::io;
856
857 DeclNameKey Key;
858 Key.Kind = (DeclarationName::NameKind)*d++;
859 switch (Key.Kind) {
860 case DeclarationName::Identifier:
861 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
862 break;
863 case DeclarationName::ObjCZeroArgSelector:
864 case DeclarationName::ObjCOneArgSelector:
865 case DeclarationName::ObjCMultiArgSelector:
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000866 Key.Data =
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000867 (uint64_t)Reader.DecodeSelector(ReadUnalignedLE32(d)).getAsOpaquePtr();
868 break;
869 case DeclarationName::CXXConstructorName:
870 case DeclarationName::CXXDestructorName:
871 case DeclarationName::CXXConversionFunctionName:
872 Key.Data = ReadUnalignedLE32(d); // TypeID
873 break;
874 case DeclarationName::CXXOperatorName:
875 Key.Data = *d++; // OverloadedOperatorKind
876 break;
877 case DeclarationName::CXXLiteralOperatorName:
878 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
879 break;
880 case DeclarationName::CXXUsingDirective:
881 break;
882 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000883
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000884 return Key;
885 }
886
887 data_type ReadData(internal_key_type, const unsigned char* d,
888 unsigned DataLen) {
889 using namespace clang::io;
890 unsigned NumDecls = ReadUnalignedLE16(d);
891 DeclID *Start = (DeclID *)d;
892 return std::make_pair(Start, Start + NumDecls);
893 }
894};
895
896} // end anonymous namespace
897
898/// \brief The on-disk hash table used for the DeclContext's Name lookup table.
899typedef OnDiskChainedHashTable<ASTDeclContextNameLookupTrait>
900 ASTDeclContextNameLookupTable;
901
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000902bool ASTReader::ReadDeclContextStorage(llvm::BitstreamCursor &Cursor,
903 const std::pair<uint64_t, uint64_t> &Offsets,
904 DeclContextInfo &Info) {
905 SavedStreamPosition SavedPosition(Cursor);
906 // First the lexical decls.
907 if (Offsets.first != 0) {
908 Cursor.JumpToBit(Offsets.first);
909
910 RecordData Record;
911 const char *Blob;
912 unsigned BlobLen;
913 unsigned Code = Cursor.ReadCode();
914 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
915 if (RecCode != DECL_CONTEXT_LEXICAL) {
916 Error("Expected lexical block");
917 return true;
918 }
919
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000920 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob);
921 Info.NumLexicalDecls = BlobLen / sizeof(KindDeclIDPair);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000922 } else {
923 Info.LexicalDecls = 0;
924 Info.NumLexicalDecls = 0;
925 }
926
927 // Now the lookup table.
928 if (Offsets.second != 0) {
929 Cursor.JumpToBit(Offsets.second);
930
931 RecordData Record;
932 const char *Blob;
933 unsigned BlobLen;
934 unsigned Code = Cursor.ReadCode();
935 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
936 if (RecCode != DECL_CONTEXT_VISIBLE) {
937 Error("Expected visible lookup table block");
938 return true;
939 }
940 Info.NameLookupTableData
941 = ASTDeclContextNameLookupTable::Create(
942 (const unsigned char *)Blob + Record[0],
943 (const unsigned char *)Blob,
944 ASTDeclContextNameLookupTrait(*this));
Sebastian Redl9d8f58b2010-08-24 00:50:00 +0000945 } else {
946 Info.NameLookupTableData = 0;
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000947 }
948
949 return false;
950}
951
Sebastian Redl2c499f62010-08-18 23:56:43 +0000952void ASTReader::Error(const char *Msg) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +0000953 Diag(diag::err_fe_pch_malformed) << Msg;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000954}
955
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000956/// \brief Tell the AST listener about the predefines buffers in the chain.
Sebastian Redl2c499f62010-08-18 23:56:43 +0000957bool ASTReader::CheckPredefinesBuffers() {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000958 if (Listener)
Sebastian Redl75fbb3b2010-07-14 17:49:11 +0000959 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
Daniel Dunbar000c4ff2009-11-11 05:29:04 +0000960 ActualOriginalFileName,
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000961 SuggestedPredefines);
Douglas Gregorc379c072009-04-28 18:58:38 +0000962 return false;
Douglas Gregor92863e42009-04-10 23:10:45 +0000963}
964
Douglas Gregorc5046832009-04-27 18:38:38 +0000965//===----------------------------------------------------------------------===//
966// Source Manager Deserialization
967//===----------------------------------------------------------------------===//
968
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000969/// \brief Read the line table in the source manager block.
Sebastian Redl2c373b92010-10-05 15:59:54 +0000970/// \returns true if there was an error.
971bool ASTReader::ParseLineTable(PerFileData &F,
972 llvm::SmallVectorImpl<uint64_t> &Record) {
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000973 unsigned Idx = 0;
974 LineTableInfo &LineTable = SourceMgr.getLineTable();
975
976 // Parse the file names
Douglas Gregora8854652009-04-13 17:12:42 +0000977 std::map<int, int> FileIDs;
978 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000979 // Extract the file name
980 unsigned FilenameLen = Record[Idx++];
981 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
982 Idx += FilenameLen;
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000983 MaybeAddSystemRootToFilename(Filename);
Mike Stump11289f42009-09-09 15:08:12 +0000984 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
Douglas Gregora8854652009-04-13 17:12:42 +0000985 Filename.size());
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000986 }
987
988 // Parse the line entries
989 std::vector<LineEntry> Entries;
990 while (Idx < Record.size()) {
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +0000991 int FID = Record[Idx++];
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000992
993 // Extract the line entries
994 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +0000995 assert(NumEntries && "Numentries is 00000");
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000996 Entries.clear();
997 Entries.reserve(NumEntries);
998 for (unsigned I = 0; I != NumEntries; ++I) {
999 unsigned FileOffset = Record[Idx++];
1000 unsigned LineNo = Record[Idx++];
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +00001001 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump11289f42009-09-09 15:08:12 +00001002 SrcMgr::CharacteristicKind FileKind
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001003 = (SrcMgr::CharacteristicKind)Record[Idx++];
1004 unsigned IncludeOffset = Record[Idx++];
1005 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1006 FileKind, IncludeOffset));
1007 }
1008 LineTable.AddEntry(FID, Entries);
1009 }
1010
1011 return false;
1012}
1013
Douglas Gregorc5046832009-04-27 18:38:38 +00001014namespace {
1015
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001016class ASTStatData {
Douglas Gregorc5046832009-04-27 18:38:38 +00001017public:
Douglas Gregorc5046832009-04-27 18:38:38 +00001018 const ino_t ino;
1019 const dev_t dev;
1020 const mode_t mode;
1021 const time_t mtime;
1022 const off_t size;
Mike Stump11289f42009-09-09 15:08:12 +00001023
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001024 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Chris Lattner2a6fa472010-11-23 19:28:12 +00001025 : ino(i), dev(d), mode(mo), mtime(m), size(s) {}
Douglas Gregorc5046832009-04-27 18:38:38 +00001026};
1027
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001028class ASTStatLookupTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +00001029 public:
1030 typedef const char *external_key_type;
1031 typedef const char *internal_key_type;
1032
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001033 typedef ASTStatData data_type;
Douglas Gregorc5046832009-04-27 18:38:38 +00001034
1035 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001036 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +00001037 }
1038
1039 static internal_key_type GetInternalKey(const char *path) { return path; }
1040
1041 static bool EqualKey(internal_key_type a, internal_key_type b) {
1042 return strcmp(a, b) == 0;
1043 }
1044
1045 static std::pair<unsigned, unsigned>
1046 ReadKeyDataLength(const unsigned char*& d) {
1047 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1048 unsigned DataLen = (unsigned) *d++;
1049 return std::make_pair(KeyLen + 1, DataLen);
1050 }
1051
1052 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
1053 return (const char *)d;
1054 }
1055
1056 static data_type ReadData(const internal_key_type, const unsigned char *d,
1057 unsigned /*DataLen*/) {
1058 using namespace clang::io;
1059
Douglas Gregorc5046832009-04-27 18:38:38 +00001060 ino_t ino = (ino_t) ReadUnalignedLE32(d);
1061 dev_t dev = (dev_t) ReadUnalignedLE32(d);
1062 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump11289f42009-09-09 15:08:12 +00001063 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregorc5046832009-04-27 18:38:38 +00001064 off_t size = (off_t) ReadUnalignedLE64(d);
1065 return data_type(ino, dev, mode, mtime, size);
1066 }
1067};
1068
1069/// \brief stat() cache for precompiled headers.
1070///
1071/// This cache is very similar to the stat cache used by pretokenized
1072/// headers.
Chris Lattner226efd32010-11-23 19:19:34 +00001073class ASTStatCache : public FileSystemStatCache {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001074 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
Douglas Gregorc5046832009-04-27 18:38:38 +00001075 CacheTy *Cache;
1076
1077 unsigned &NumStatHits, &NumStatMisses;
Mike Stump11289f42009-09-09 15:08:12 +00001078public:
Chris Lattner2a6fa472010-11-23 19:28:12 +00001079 ASTStatCache(const unsigned char *Buckets, const unsigned char *Base,
1080 unsigned &NumStatHits, unsigned &NumStatMisses)
Douglas Gregorc5046832009-04-27 18:38:38 +00001081 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
1082 Cache = CacheTy::Create(Buckets, Base);
1083 }
1084
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001085 ~ASTStatCache() { delete Cache; }
Mike Stump11289f42009-09-09 15:08:12 +00001086
Chris Lattnerdd278432010-11-23 21:17:56 +00001087 LookupResult getStat(const char *Path, struct stat &StatBuf,
1088 int *FileDescriptor) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001089 // Do the lookup for the file's data in the AST file.
Chris Lattner226efd32010-11-23 19:19:34 +00001090 CacheTy::iterator I = Cache->find(Path);
Douglas Gregorc5046832009-04-27 18:38:38 +00001091
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001092 // If we don't get a hit in the AST file just forward to 'stat'.
Douglas Gregorc5046832009-04-27 18:38:38 +00001093 if (I == Cache->end()) {
1094 ++NumStatMisses;
Chris Lattnerdd278432010-11-23 21:17:56 +00001095 return statChained(Path, StatBuf, FileDescriptor);
Douglas Gregorc5046832009-04-27 18:38:38 +00001096 }
Mike Stump11289f42009-09-09 15:08:12 +00001097
Douglas Gregorc5046832009-04-27 18:38:38 +00001098 ++NumStatHits;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001099 ASTStatData Data = *I;
Mike Stump11289f42009-09-09 15:08:12 +00001100
Chris Lattner226efd32010-11-23 19:19:34 +00001101 StatBuf.st_ino = Data.ino;
1102 StatBuf.st_dev = Data.dev;
1103 StatBuf.st_mtime = Data.mtime;
1104 StatBuf.st_mode = Data.mode;
1105 StatBuf.st_size = Data.size;
Chris Lattner8f0583d2010-11-23 20:05:15 +00001106 return CacheExists;
Douglas Gregorc5046832009-04-27 18:38:38 +00001107 }
1108};
1109} // end anonymous namespace
1110
1111
Sebastian Redl393f8b72010-07-19 20:52:06 +00001112/// \brief Read a source manager block
Sebastian Redl2c499f62010-08-18 23:56:43 +00001113ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(PerFileData &F) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001114 using namespace SrcMgr;
Douglas Gregor258ae542009-04-27 06:38:32 +00001115
Sebastian Redl393f8b72010-07-19 20:52:06 +00001116 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001117
Douglas Gregor258ae542009-04-27 06:38:32 +00001118 // Set the source-location entry cursor to the current position in
1119 // the stream. This cursor will be used to read the contents of the
1120 // source manager block initially, and then lazily read
1121 // source-location entries as needed.
Sebastian Redl393f8b72010-07-19 20:52:06 +00001122 SLocEntryCursor = F.Stream;
Douglas Gregor258ae542009-04-27 06:38:32 +00001123
1124 // The stream itself is going to skip over the source manager block.
Sebastian Redl393f8b72010-07-19 20:52:06 +00001125 if (F.Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001126 Error("malformed block record in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001127 return Failure;
1128 }
1129
1130 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001131 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001132 Error("malformed source manager block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001133 return Failure;
1134 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001135
Douglas Gregora7f71a92009-04-10 03:52:48 +00001136 RecordData Record;
1137 while (true) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001138 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregora7f71a92009-04-10 03:52:48 +00001139 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001140 if (SLocEntryCursor.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001141 Error("error at end of Source Manager block in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001142 return Failure;
1143 }
Douglas Gregor92863e42009-04-10 23:10:45 +00001144 return Success;
Douglas Gregora7f71a92009-04-10 03:52:48 +00001145 }
Mike Stump11289f42009-09-09 15:08:12 +00001146
Douglas Gregora7f71a92009-04-10 03:52:48 +00001147 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1148 // No known subblocks, always skip them.
Douglas Gregor258ae542009-04-27 06:38:32 +00001149 SLocEntryCursor.ReadSubBlockID();
1150 if (SLocEntryCursor.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001151 Error("malformed block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00001152 return Failure;
1153 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001154 continue;
1155 }
Mike Stump11289f42009-09-09 15:08:12 +00001156
Douglas Gregora7f71a92009-04-10 03:52:48 +00001157 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001158 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregora7f71a92009-04-10 03:52:48 +00001159 continue;
1160 }
Mike Stump11289f42009-09-09 15:08:12 +00001161
Douglas Gregora7f71a92009-04-10 03:52:48 +00001162 // Read a record.
1163 const char *BlobStart;
1164 unsigned BlobLen;
1165 Record.clear();
Douglas Gregor258ae542009-04-27 06:38:32 +00001166 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001167 default: // Default behavior: ignore.
1168 break;
1169
Sebastian Redl539c5062010-08-18 23:57:32 +00001170 case SM_LINE_TABLE:
Sebastian Redl2c373b92010-10-05 15:59:54 +00001171 if (ParseLineTable(F, Record))
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001172 return Failure;
Chris Lattner184e65d2009-04-14 23:22:57 +00001173 break;
Douglas Gregoreda6a892009-04-26 00:07:37 +00001174
Sebastian Redl539c5062010-08-18 23:57:32 +00001175 case SM_SLOC_FILE_ENTRY:
1176 case SM_SLOC_BUFFER_ENTRY:
1177 case SM_SLOC_INSTANTIATION_ENTRY:
Douglas Gregor258ae542009-04-27 06:38:32 +00001178 // Once we hit one of the source location entries, we're done.
1179 return Success;
Douglas Gregora7f71a92009-04-10 03:52:48 +00001180 }
1181 }
1182}
1183
Sebastian Redl06750302010-07-20 21:50:20 +00001184/// \brief Get a cursor that's correctly positioned for reading the source
1185/// location entry with the given ID.
Sebastian Redl2c373b92010-10-05 15:59:54 +00001186ASTReader::PerFileData *ASTReader::SLocCursorForID(unsigned ID) {
Sebastian Redl06750302010-07-20 21:50:20 +00001187 assert(ID != 0 && ID <= TotalNumSLocEntries &&
1188 "SLocCursorForID should only be called for real IDs.");
1189
1190 ID -= 1;
1191 PerFileData *F = 0;
1192 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1193 F = Chain[N - I - 1];
1194 if (ID < F->LocalNumSLocEntries)
1195 break;
1196 ID -= F->LocalNumSLocEntries;
1197 }
1198 assert(F && F->LocalNumSLocEntries > ID && "Chain corrupted");
1199
1200 F->SLocEntryCursor.JumpToBit(F->SLocOffsets[ID]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00001201 return F;
Sebastian Redl06750302010-07-20 21:50:20 +00001202}
1203
Douglas Gregor258ae542009-04-27 06:38:32 +00001204/// \brief Read in the source location entry with the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001205ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(unsigned ID) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001206 if (ID == 0)
1207 return Success;
1208
1209 if (ID > TotalNumSLocEntries) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001210 Error("source location entry ID out-of-range for AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001211 return Failure;
1212 }
1213
Sebastian Redl2c373b92010-10-05 15:59:54 +00001214 PerFileData *F = SLocCursorForID(ID);
1215 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001216
Douglas Gregor258ae542009-04-27 06:38:32 +00001217 ++NumSLocEntriesRead;
Douglas Gregor258ae542009-04-27 06:38:32 +00001218 unsigned Code = SLocEntryCursor.ReadCode();
1219 if (Code == llvm::bitc::END_BLOCK ||
1220 Code == llvm::bitc::ENTER_SUBBLOCK ||
1221 Code == llvm::bitc::DEFINE_ABBREV) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001222 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001223 return Failure;
1224 }
1225
Douglas Gregor258ae542009-04-27 06:38:32 +00001226 RecordData Record;
1227 const char *BlobStart;
1228 unsigned BlobLen;
1229 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1230 default:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001231 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor258ae542009-04-27 06:38:32 +00001232 return Failure;
1233
Sebastian Redl539c5062010-08-18 23:57:32 +00001234 case SM_SLOC_FILE_ENTRY: {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001235 std::string Filename(BlobStart, BlobStart + BlobLen);
1236 MaybeAddSystemRootToFilename(Filename);
Chris Lattner5159f612010-11-23 08:35:12 +00001237 const FileEntry *File = FileMgr.getFile(Filename);
Axel Naumann63fbaed2011-01-27 10:55:51 +00001238 if (File == 0)
1239 File = FileMgr.getVirtualFile(Filename, (off_t)Record[4],
1240 (time_t)Record[5]);
Chris Lattnerd20dc872009-06-15 04:35:16 +00001241 if (File == 0) {
1242 std::string ErrorStr = "could not find file '";
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001243 ErrorStr += Filename;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001244 ErrorStr += "' referenced by AST file";
Chris Lattnerd20dc872009-06-15 04:35:16 +00001245 Error(ErrorStr.c_str());
1246 return Failure;
1247 }
Mike Stump11289f42009-09-09 15:08:12 +00001248
Douglas Gregor09b69892011-02-10 17:09:37 +00001249 if (Record.size() < 6) {
Ted Kremenekabb1ddd2010-03-18 21:23:05 +00001250 Error("source location entry is incorrect");
1251 return Failure;
1252 }
1253
Douglas Gregorce3a8292010-07-27 00:27:13 +00001254 if (!DisableValidation &&
1255 ((off_t)Record[4] != File->getSize()
Douglas Gregor08288f22010-04-09 15:54:22 +00001256#if !defined(LLVM_ON_WIN32)
1257 // In our regression testing, the Windows file system seems to
1258 // have inconsistent modification times that sometimes
1259 // erroneously trigger this error-handling path.
Douglas Gregorce3a8292010-07-27 00:27:13 +00001260 || (time_t)Record[5] != File->getModificationTime()
Douglas Gregor08288f22010-04-09 15:54:22 +00001261#endif
Douglas Gregorce3a8292010-07-27 00:27:13 +00001262 )) {
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001263 Diag(diag::err_fe_pch_file_modified)
1264 << Filename;
1265 return Failure;
1266 }
1267
Chris Lattner26b5c192010-11-23 09:19:42 +00001268 FileID FID = SourceMgr.createFileID(File, ReadSourceLocation(*F, Record[1]),
1269 (SrcMgr::CharacteristicKind)Record[2],
Douglas Gregor258ae542009-04-27 06:38:32 +00001270 ID, Record[0]);
1271 if (Record[3])
1272 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile())
1273 .setHasLineDirectives();
Douglas Gregor09b69892011-02-10 17:09:37 +00001274
Douglas Gregor258ae542009-04-27 06:38:32 +00001275 break;
1276 }
1277
Sebastian Redl539c5062010-08-18 23:57:32 +00001278 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor258ae542009-04-27 06:38:32 +00001279 const char *Name = BlobStart;
1280 unsigned Offset = Record[0];
1281 unsigned Code = SLocEntryCursor.ReadCode();
1282 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00001283 unsigned RecCode
Douglas Gregor258ae542009-04-27 06:38:32 +00001284 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00001285
Sebastian Redl539c5062010-08-18 23:57:32 +00001286 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001287 Error("AST record has invalid code");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00001288 return Failure;
1289 }
1290
Douglas Gregor258ae542009-04-27 06:38:32 +00001291 llvm::MemoryBuffer *Buffer
Chris Lattner58c79342010-04-05 22:42:27 +00001292 = llvm::MemoryBuffer::getMemBuffer(llvm::StringRef(BlobStart, BlobLen - 1),
1293 Name);
Douglas Gregor258ae542009-04-27 06:38:32 +00001294 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID, Offset);
Mike Stump11289f42009-09-09 15:08:12 +00001295
Douglas Gregore6648fb2009-04-28 20:33:11 +00001296 if (strcmp(Name, "<built-in>") == 0) {
Sebastian Redl75fbb3b2010-07-14 17:49:11 +00001297 PCHPredefinesBlock Block = {
1298 BufferID,
1299 llvm::StringRef(BlobStart, BlobLen - 1)
1300 };
1301 PCHPredefinesBuffers.push_back(Block);
Douglas Gregore6648fb2009-04-28 20:33:11 +00001302 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001303
1304 break;
1305 }
1306
Sebastian Redl539c5062010-08-18 23:57:32 +00001307 case SM_SLOC_INSTANTIATION_ENTRY: {
Sebastian Redl2c373b92010-10-05 15:59:54 +00001308 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
Douglas Gregor258ae542009-04-27 06:38:32 +00001309 SourceMgr.createInstantiationLoc(SpellingLoc,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001310 ReadSourceLocation(*F, Record[2]),
1311 ReadSourceLocation(*F, Record[3]),
Douglas Gregor258ae542009-04-27 06:38:32 +00001312 Record[4],
1313 ID,
1314 Record[0]);
1315 break;
Mike Stump11289f42009-09-09 15:08:12 +00001316 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001317 }
1318
1319 return Success;
1320}
1321
Chris Lattnere78a6be2009-04-27 01:05:14 +00001322/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1323/// specified cursor. Read the abbreviations that are at the top of the block
1324/// and then leave the cursor pointing into the block.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001325bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattnere78a6be2009-04-27 01:05:14 +00001326 unsigned BlockID) {
1327 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001328 Error("malformed block record in AST file");
Chris Lattnere78a6be2009-04-27 01:05:14 +00001329 return Failure;
1330 }
Mike Stump11289f42009-09-09 15:08:12 +00001331
Chris Lattnere78a6be2009-04-27 01:05:14 +00001332 while (true) {
Douglas Gregor796d76a2010-10-20 22:00:55 +00001333 uint64_t Offset = Cursor.GetCurrentBitNo();
Chris Lattnere78a6be2009-04-27 01:05:14 +00001334 unsigned Code = Cursor.ReadCode();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001335
Chris Lattnere78a6be2009-04-27 01:05:14 +00001336 // We expect all abbrevs to be at the start of the block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001337 if (Code != llvm::bitc::DEFINE_ABBREV) {
1338 Cursor.JumpToBit(Offset);
Chris Lattnere78a6be2009-04-27 01:05:14 +00001339 return false;
Douglas Gregor796d76a2010-10-20 22:00:55 +00001340 }
Chris Lattnere78a6be2009-04-27 01:05:14 +00001341 Cursor.ReadAbbrevRecord();
1342 }
1343}
1344
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001345PreprocessedEntity *ASTReader::ReadMacroRecord(PerFileData &F, uint64_t Offset) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001346 assert(PP && "Forgot to set Preprocessor ?");
Douglas Gregor796d76a2010-10-20 22:00:55 +00001347 llvm::BitstreamCursor &Stream = F.MacroCursor;
Mike Stump11289f42009-09-09 15:08:12 +00001348
Douglas Gregorc3366a52009-04-21 23:56:24 +00001349 // Keep track of where we are in the stream, then jump back there
1350 // after reading this macro.
1351 SavedStreamPosition SavedPosition(Stream);
1352
1353 Stream.JumpToBit(Offset);
1354 RecordData Record;
1355 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1356 MacroInfo *Macro = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001357
Douglas Gregorc3366a52009-04-21 23:56:24 +00001358 while (true) {
1359 unsigned Code = Stream.ReadCode();
1360 switch (Code) {
1361 case llvm::bitc::END_BLOCK:
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001362 return 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001363
1364 case llvm::bitc::ENTER_SUBBLOCK:
1365 // No known subblocks, always skip them.
1366 Stream.ReadSubBlockID();
1367 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001368 Error("malformed block record in AST file");
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001369 return 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001370 }
1371 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001372
Douglas Gregorc3366a52009-04-21 23:56:24 +00001373 case llvm::bitc::DEFINE_ABBREV:
1374 Stream.ReadAbbrevRecord();
1375 continue;
1376 default: break;
1377 }
1378
1379 // Read a record.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001380 const char *BlobStart = 0;
1381 unsigned BlobLen = 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001382 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001383 PreprocessorRecordTypes RecType =
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001384 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart,
Douglas Gregor796d76a2010-10-20 22:00:55 +00001385 BlobLen);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001386 switch (RecType) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001387 case PP_MACRO_OBJECT_LIKE:
1388 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001389 // If we already have a macro, that means that we've hit the end
1390 // of the definition of the macro we were looking for. We're
1391 // done.
1392 if (Macro)
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001393 return 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001394
1395 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1396 if (II == 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001397 Error("macro must have a name in AST file");
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001398 return 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001399 }
Sebastian Redl2c373b92010-10-05 15:59:54 +00001400 SourceLocation Loc = ReadSourceLocation(F, Record[1]);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001401 bool isUsed = Record[2];
Mike Stump11289f42009-09-09 15:08:12 +00001402
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001403 MacroInfo *MI = PP->AllocateMacroInfo(Loc);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001404 MI->setIsUsed(isUsed);
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001405 MI->setIsFromAST();
Mike Stump11289f42009-09-09 15:08:12 +00001406
Douglas Gregoraae92242010-03-19 21:51:54 +00001407 unsigned NextIndex = 3;
Sebastian Redl539c5062010-08-18 23:57:32 +00001408 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001409 // Decode function-like macro info.
1410 bool isC99VarArgs = Record[3];
1411 bool isGNUVarArgs = Record[4];
1412 MacroArgs.clear();
1413 unsigned NumArgs = Record[5];
Douglas Gregoraae92242010-03-19 21:51:54 +00001414 NextIndex = 6 + NumArgs;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001415 for (unsigned i = 0; i != NumArgs; ++i)
1416 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1417
1418 // Install function-like macro info.
1419 MI->setIsFunctionLike();
1420 if (isC99VarArgs) MI->setIsC99Varargs();
1421 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor038c3382009-05-22 22:45:36 +00001422 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001423 PP->getPreprocessorAllocator());
Douglas Gregorc3366a52009-04-21 23:56:24 +00001424 }
1425
1426 // Finally, install the macro.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00001427 PP->setMacroInfo(II, MI);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001428
1429 // Remember that we saw this macro last so that we add the tokens that
1430 // form its body to it.
1431 Macro = MI;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001432
Douglas Gregoraae92242010-03-19 21:51:54 +00001433 if (NextIndex + 1 == Record.size() && PP->getPreprocessingRecord()) {
1434 // We have a macro definition. Load it now.
1435 PP->getPreprocessingRecord()->RegisterMacroDefinition(Macro,
1436 getMacroDefinition(Record[NextIndex]));
1437 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001438
Douglas Gregorc3366a52009-04-21 23:56:24 +00001439 ++NumMacrosRead;
1440 break;
1441 }
Mike Stump11289f42009-09-09 15:08:12 +00001442
Sebastian Redl539c5062010-08-18 23:57:32 +00001443 case PP_TOKEN: {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001444 // If we see a TOKEN before a PP_MACRO_*, then the file is
1445 // erroneous, just pretend we didn't see this.
1446 if (Macro == 0) break;
Mike Stump11289f42009-09-09 15:08:12 +00001447
Douglas Gregorc3366a52009-04-21 23:56:24 +00001448 Token Tok;
1449 Tok.startToken();
Sebastian Redl2c373b92010-10-05 15:59:54 +00001450 Tok.setLocation(ReadSourceLocation(F, Record[0]));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001451 Tok.setLength(Record[1]);
1452 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1453 Tok.setIdentifierInfo(II);
1454 Tok.setKind((tok::TokenKind)Record[3]);
1455 Tok.setFlag((Token::TokenFlags)Record[4]);
1456 Macro->AddTokenToBody(Tok);
1457 break;
1458 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00001459 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001460 }
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001461
1462 return 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001463}
1464
Douglas Gregor92a96f52011-02-08 21:58:10 +00001465PreprocessedEntity *ASTReader::LoadPreprocessedEntity(PerFileData &F) {
1466 assert(PP && "Forgot to set Preprocessor ?");
1467 unsigned Code = F.PreprocessorDetailCursor.ReadCode();
1468 switch (Code) {
1469 case llvm::bitc::END_BLOCK:
1470 return 0;
1471
1472 case llvm::bitc::ENTER_SUBBLOCK:
1473 Error("unexpected subblock record in preprocessor detail block");
1474 return 0;
1475
1476 case llvm::bitc::DEFINE_ABBREV:
1477 Error("unexpected abbrevation record in preprocessor detail block");
1478 return 0;
1479
1480 default:
1481 break;
1482 }
1483
1484 if (!PP->getPreprocessingRecord()) {
1485 Error("no preprocessing record");
1486 return 0;
1487 }
1488
1489 // Read the record.
1490 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1491 const char *BlobStart = 0;
1492 unsigned BlobLen = 0;
1493 RecordData Record;
1494 PreprocessorDetailRecordTypes RecType =
1495 (PreprocessorDetailRecordTypes)F.PreprocessorDetailCursor.ReadRecord(
1496 Code, Record, BlobStart, BlobLen);
1497 switch (RecType) {
1498 case PPD_MACRO_INSTANTIATION: {
1499 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1500 return PE;
1501
1502 MacroInstantiation *MI
1503 = new (PPRec) MacroInstantiation(DecodeIdentifierInfo(Record[3]),
1504 SourceRange(ReadSourceLocation(F, Record[1]),
1505 ReadSourceLocation(F, Record[2])),
1506 getMacroDefinition(Record[4]));
1507 PPRec.SetPreallocatedEntity(Record[0], MI);
1508 return MI;
1509 }
1510
1511 case PPD_MACRO_DEFINITION: {
1512 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1513 return PE;
1514
1515 if (Record[1] > MacroDefinitionsLoaded.size()) {
1516 Error("out-of-bounds macro definition record");
1517 return 0;
1518 }
1519
1520 // Decode the identifier info and then check again; if the macro is
1521 // still defined and associated with the identifier,
1522 IdentifierInfo *II = DecodeIdentifierInfo(Record[4]);
1523 if (!MacroDefinitionsLoaded[Record[1] - 1]) {
1524 MacroDefinition *MD
1525 = new (PPRec) MacroDefinition(II,
1526 ReadSourceLocation(F, Record[5]),
1527 SourceRange(
1528 ReadSourceLocation(F, Record[2]),
1529 ReadSourceLocation(F, Record[3])));
1530
1531 PPRec.SetPreallocatedEntity(Record[0], MD);
1532 MacroDefinitionsLoaded[Record[1] - 1] = MD;
1533
1534 if (DeserializationListener)
1535 DeserializationListener->MacroDefinitionRead(Record[1], MD);
1536 }
1537
1538 return MacroDefinitionsLoaded[Record[1] - 1];
1539 }
1540
1541 case PPD_INCLUSION_DIRECTIVE: {
1542 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1543 return PE;
1544
1545 const char *FullFileNameStart = BlobStart + Record[3];
1546 const FileEntry *File
1547 = PP->getFileManager().getFile(llvm::StringRef(FullFileNameStart,
1548 BlobLen - Record[3]));
1549
1550 // FIXME: Stable encoding
1551 InclusionDirective::InclusionKind Kind
1552 = static_cast<InclusionDirective::InclusionKind>(Record[5]);
1553 InclusionDirective *ID
1554 = new (PPRec) InclusionDirective(PPRec, Kind,
1555 llvm::StringRef(BlobStart, Record[3]),
1556 Record[4],
1557 File,
1558 SourceRange(ReadSourceLocation(F, Record[1]),
1559 ReadSourceLocation(F, Record[2])));
1560 PPRec.SetPreallocatedEntity(Record[0], ID);
1561 return ID;
1562 }
1563 }
1564
1565 Error("invalid offset in preprocessor detail block");
1566 return 0;
1567}
1568
Douglas Gregor09b69892011-02-10 17:09:37 +00001569namespace {
1570 /// \brief Trait class used to search the on-disk hash table containing all of
1571 /// the header search information.
1572 ///
1573 /// The on-disk hash table contains a mapping from each header path to
1574 /// information about that header (how many times it has been included, its
1575 /// controlling macro, etc.). Note that we actually hash based on the
1576 /// filename, and support "deep" comparisons of file names based on current
1577 /// inode numbers, so that the search can cope with non-normalized path names
1578 /// and symlinks.
1579 class HeaderFileInfoTrait {
1580 const char *SearchPath;
1581 struct stat SearchPathStatBuf;
1582 llvm::Optional<int> SearchPathStatResult;
1583
1584 int StatSimpleCache(const char *Path, struct stat *StatBuf) {
1585 if (Path == SearchPath) {
1586 if (!SearchPathStatResult)
1587 SearchPathStatResult = stat(Path, &SearchPathStatBuf);
1588
1589 *StatBuf = SearchPathStatBuf;
1590 return *SearchPathStatResult;
1591 }
1592
1593 return stat(Path, StatBuf);
1594 }
1595
1596 public:
1597 typedef const char *external_key_type;
1598 typedef const char *internal_key_type;
1599
1600 typedef HeaderFileInfo data_type;
1601
1602 HeaderFileInfoTrait(const char *SearchPath = 0) : SearchPath(SearchPath) { }
1603
1604 static unsigned ComputeHash(const char *path) {
1605 return llvm::HashString(llvm::sys::path::filename(path));
1606 }
1607
1608 static internal_key_type GetInternalKey(const char *path) { return path; }
1609
1610 bool EqualKey(internal_key_type a, internal_key_type b) {
1611 if (strcmp(a, b) == 0)
1612 return true;
1613
1614 if (llvm::sys::path::filename(a) != llvm::sys::path::filename(b))
1615 return false;
1616
1617 // The file names match, but the path names don't. stat() the files to
1618 // see if they are the same.
1619 struct stat StatBufA, StatBufB;
1620 if (StatSimpleCache(a, &StatBufA) || StatSimpleCache(b, &StatBufB))
1621 return false;
1622
1623 return StatBufA.st_ino == StatBufB.st_ino;
1624 }
1625
1626 static std::pair<unsigned, unsigned>
1627 ReadKeyDataLength(const unsigned char*& d) {
1628 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1629 unsigned DataLen = (unsigned) *d++;
1630 return std::make_pair(KeyLen + 1, DataLen);
1631 }
1632
1633 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
1634 return (const char *)d;
1635 }
1636
1637 static data_type ReadData(const internal_key_type, const unsigned char *d,
1638 unsigned DataLen) {
1639 const unsigned char *End = d + DataLen;
1640 using namespace clang::io;
1641 HeaderFileInfo HFI;
1642 unsigned Flags = *d++;
1643 HFI.isImport = (Flags >> 3) & 0x01;
1644 HFI.DirInfo = (Flags >> 1) & 0x03;
1645 HFI.Resolved = Flags & 0x01;
1646 HFI.NumIncludes = ReadUnalignedLE16(d);
1647 HFI.ControllingMacroID = ReadUnalignedLE32(d);
1648 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1649 (void)End;
1650
1651 // This HeaderFileInfo was externally loaded.
1652 HFI.External = true;
1653 return HFI;
1654 }
1655 };
1656}
1657
1658/// \brief The on-disk hash table used for the global method pool.
1659typedef OnDiskChainedHashTable<HeaderFileInfoTrait>
1660 HeaderFileInfoLookupTable;
1661
Douglas Gregor5ef9e332010-10-30 00:23:06 +00001662void ASTReader::SetIdentifierIsMacro(IdentifierInfo *II, PerFileData &F,
1663 uint64_t Offset) {
1664 // Note that this identifier has a macro definition.
1665 II->setHasMacroDefinition(true);
1666
1667 // Adjust the offset based on our position in the chain.
1668 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1669 if (Chain[I] == &F)
1670 break;
1671
1672 Offset += Chain[I]->SizeInBits;
1673 }
1674
1675 UnreadMacroRecordOffsets[II] = Offset;
1676}
1677
Sebastian Redl2c499f62010-08-18 23:56:43 +00001678void ASTReader::ReadDefinedMacros() {
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001679 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00001680 PerFileData &F = *Chain[N - I - 1];
1681 llvm::BitstreamCursor &MacroCursor = F.MacroCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001682
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001683 // If there was no preprocessor block, skip this file.
1684 if (!MacroCursor.getBitStreamReader())
1685 continue;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001686
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001687 llvm::BitstreamCursor Cursor = MacroCursor;
Douglas Gregor796d76a2010-10-20 22:00:55 +00001688 Cursor.JumpToBit(F.MacroStartOffset);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001689
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001690 RecordData Record;
1691 while (true) {
1692 unsigned Code = Cursor.ReadCode();
Douglas Gregor796d76a2010-10-20 22:00:55 +00001693 if (Code == llvm::bitc::END_BLOCK)
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001694 break;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001695
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001696 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1697 // No known subblocks, always skip them.
1698 Cursor.ReadSubBlockID();
1699 if (Cursor.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001700 Error("malformed block record in AST file");
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001701 return;
1702 }
1703 continue;
1704 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001705
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001706 if (Code == llvm::bitc::DEFINE_ABBREV) {
1707 Cursor.ReadAbbrevRecord();
1708 continue;
1709 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001710
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001711 // Read a record.
1712 const char *BlobStart;
1713 unsigned BlobLen;
1714 Record.clear();
1715 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1716 default: // Default behavior: ignore.
1717 break;
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001718
Sebastian Redl539c5062010-08-18 23:57:32 +00001719 case PP_MACRO_OBJECT_LIKE:
1720 case PP_MACRO_FUNCTION_LIKE:
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001721 DecodeIdentifierInfo(Record[0]);
1722 break;
1723
Sebastian Redl539c5062010-08-18 23:57:32 +00001724 case PP_TOKEN:
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001725 // Ignore tokens.
1726 break;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001727 }
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001728 }
1729 }
Douglas Gregor5ef9e332010-10-30 00:23:06 +00001730
1731 // Drain the unread macro-record offsets map.
1732 while (!UnreadMacroRecordOffsets.empty())
1733 LoadMacroDefinition(UnreadMacroRecordOffsets.begin());
1734}
1735
1736void ASTReader::LoadMacroDefinition(
1737 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos) {
1738 assert(Pos != UnreadMacroRecordOffsets.end() && "Unknown macro definition");
1739 PerFileData *F = 0;
1740 uint64_t Offset = Pos->second;
1741 UnreadMacroRecordOffsets.erase(Pos);
1742
1743 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1744 if (Offset < Chain[I]->SizeInBits) {
1745 F = Chain[I];
1746 break;
1747 }
1748
1749 Offset -= Chain[I]->SizeInBits;
1750 }
1751 if (!F) {
1752 Error("Malformed macro record offset");
1753 return;
1754 }
1755
1756 ReadMacroRecord(*F, Offset);
1757}
1758
1759void ASTReader::LoadMacroDefinition(IdentifierInfo *II) {
1760 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos
1761 = UnreadMacroRecordOffsets.find(II);
1762 LoadMacroDefinition(Pos);
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001763}
1764
Sebastian Redl50e26582010-09-15 19:54:06 +00001765MacroDefinition *ASTReader::getMacroDefinition(MacroID ID) {
Douglas Gregor91096292010-10-02 19:29:26 +00001766 if (ID == 0 || ID > MacroDefinitionsLoaded.size())
Douglas Gregoraae92242010-03-19 21:51:54 +00001767 return 0;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001768
Douglas Gregor91096292010-10-02 19:29:26 +00001769 if (!MacroDefinitionsLoaded[ID - 1]) {
1770 unsigned Index = ID - 1;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001771 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1772 PerFileData &F = *Chain[N - I - 1];
1773 if (Index < F.LocalNumMacroDefinitions) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00001774 SavedStreamPosition SavedPosition(F.PreprocessorDetailCursor);
1775 F.PreprocessorDetailCursor.JumpToBit(F.MacroDefinitionOffsets[Index]);
1776 LoadPreprocessedEntity(F);
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001777 break;
1778 }
1779 Index -= F.LocalNumMacroDefinitions;
1780 }
Douglas Gregor91096292010-10-02 19:29:26 +00001781 assert(MacroDefinitionsLoaded[ID - 1] && "Broken chain");
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001782 }
1783
Douglas Gregor91096292010-10-02 19:29:26 +00001784 return MacroDefinitionsLoaded[ID - 1];
Douglas Gregoraae92242010-03-19 21:51:54 +00001785}
1786
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001787/// \brief If we are loading a relocatable PCH file, and the filename is
1788/// not an absolute path, add the system root to the beginning of the file
1789/// name.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001790void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001791 // If this is not a relocatable PCH file, there's nothing to do.
1792 if (!RelocatablePCH)
1793 return;
Mike Stump11289f42009-09-09 15:08:12 +00001794
Michael J. Spencerf28df4c2010-12-17 21:22:22 +00001795 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001796 return;
1797
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001798 if (isysroot == 0) {
1799 // If no system root was given, default to '/'
1800 Filename.insert(Filename.begin(), '/');
1801 return;
1802 }
Mike Stump11289f42009-09-09 15:08:12 +00001803
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001804 unsigned Length = strlen(isysroot);
1805 if (isysroot[Length - 1] != '/')
1806 Filename.insert(Filename.begin(), '/');
Mike Stump11289f42009-09-09 15:08:12 +00001807
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001808 Filename.insert(Filename.begin(), isysroot, isysroot + Length);
1809}
1810
Sebastian Redl2c499f62010-08-18 23:56:43 +00001811ASTReader::ASTReadResult
Sebastian Redl3e31c722010-08-18 23:56:56 +00001812ASTReader::ReadASTBlock(PerFileData &F) {
Sebastian Redl34522812010-07-16 17:50:48 +00001813 llvm::BitstreamCursor &Stream = F.Stream;
1814
Sebastian Redl539c5062010-08-18 23:57:32 +00001815 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001816 Error("malformed block record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001817 return Failure;
1818 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001819
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001820 // Read all of the records and blocks for the ASt file.
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001821 RecordData Record;
Sebastian Redl393f8b72010-07-19 20:52:06 +00001822 bool First = true;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001823 while (!Stream.AtEndOfStream()) {
1824 unsigned Code = Stream.ReadCode();
1825 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor55abb232009-04-10 20:39:37 +00001826 if (Stream.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001827 Error("error at end of module block in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001828 return Failure;
1829 }
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001830
Douglas Gregor55abb232009-04-10 20:39:37 +00001831 return Success;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001832 }
1833
1834 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1835 switch (Stream.ReadSubBlockID()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001836 case DECLTYPES_BLOCK_ID:
Chris Lattnere78a6be2009-04-27 01:05:14 +00001837 // We lazily load the decls block, but we want to set up the
1838 // DeclsCursor cursor to point into it. Clone our current bitcode
1839 // cursor to it, enter the block and read the abbrevs in that block.
1840 // With the main cursor, we just skip over it.
Sebastian Redl34522812010-07-16 17:50:48 +00001841 F.DeclsCursor = Stream;
Chris Lattnere78a6be2009-04-27 01:05:14 +00001842 if (Stream.SkipBlock() || // Skip with the main cursor.
1843 // Read the abbrevs.
Sebastian Redl539c5062010-08-18 23:57:32 +00001844 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001845 Error("malformed block record in AST file");
Chris Lattnere78a6be2009-04-27 01:05:14 +00001846 return Failure;
1847 }
1848 break;
Mike Stump11289f42009-09-09 15:08:12 +00001849
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00001850 case DECL_UPDATES_BLOCK_ID:
1851 if (Stream.SkipBlock()) {
1852 Error("malformed block record in AST file");
1853 return Failure;
1854 }
1855 break;
1856
Sebastian Redl539c5062010-08-18 23:57:32 +00001857 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl34522812010-07-16 17:50:48 +00001858 F.MacroCursor = Stream;
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001859 if (PP)
1860 PP->setExternalSource(this);
1861
Douglas Gregor796d76a2010-10-20 22:00:55 +00001862 if (Stream.SkipBlock() ||
1863 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001864 Error("malformed block record in AST file");
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001865 return Failure;
1866 }
Douglas Gregor796d76a2010-10-20 22:00:55 +00001867 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001868 break;
Steve Naroff2ddea052009-04-23 10:39:46 +00001869
Douglas Gregor92a96f52011-02-08 21:58:10 +00001870 case PREPROCESSOR_DETAIL_BLOCK_ID:
1871 F.PreprocessorDetailCursor = Stream;
1872 if (Stream.SkipBlock() ||
1873 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
1874 PREPROCESSOR_DETAIL_BLOCK_ID)) {
1875 Error("malformed preprocessor detail record in AST file");
1876 return Failure;
1877 }
1878 F.PreprocessorDetailStartOffset
1879 = F.PreprocessorDetailCursor.GetCurrentBitNo();
1880 break;
1881
Sebastian Redl539c5062010-08-18 23:57:32 +00001882 case SOURCE_MANAGER_BLOCK_ID:
Sebastian Redl393f8b72010-07-19 20:52:06 +00001883 switch (ReadSourceManagerBlock(F)) {
Douglas Gregor92863e42009-04-10 23:10:45 +00001884 case Success:
1885 break;
1886
1887 case Failure:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001888 Error("malformed source manager block in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001889 return Failure;
Douglas Gregor92863e42009-04-10 23:10:45 +00001890
1891 case IgnorePCH:
1892 return IgnorePCH;
Douglas Gregor55abb232009-04-10 20:39:37 +00001893 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001894 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001895 }
Sebastian Redl393f8b72010-07-19 20:52:06 +00001896 First = false;
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001897 continue;
1898 }
1899
1900 if (Code == llvm::bitc::DEFINE_ABBREV) {
1901 Stream.ReadAbbrevRecord();
1902 continue;
1903 }
1904
1905 // Read and process a record.
1906 Record.clear();
Douglas Gregorbfbde532009-04-10 21:16:55 +00001907 const char *BlobStart = 0;
1908 unsigned BlobLen = 0;
Sebastian Redl539c5062010-08-18 23:57:32 +00001909 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001910 &BlobStart, &BlobLen)) {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001911 default: // Default behavior: ignore.
1912 break;
1913
Sebastian Redl539c5062010-08-18 23:57:32 +00001914 case METADATA: {
1915 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1916 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001917 : diag::warn_pch_version_too_new);
1918 return IgnorePCH;
1919 }
1920
1921 RelocatablePCH = Record[4];
1922 if (Listener) {
1923 std::string TargetTriple(BlobStart, BlobLen);
1924 if (Listener->ReadTargetTriple(TargetTriple))
1925 return IgnorePCH;
1926 }
1927 break;
1928 }
1929
Sebastian Redl539c5062010-08-18 23:57:32 +00001930 case CHAINED_METADATA: {
Sebastian Redl393f8b72010-07-19 20:52:06 +00001931 if (!First) {
1932 Error("CHAINED_METADATA is not first record in block");
1933 return Failure;
1934 }
Sebastian Redl539c5062010-08-18 23:57:32 +00001935 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1936 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001937 : diag::warn_pch_version_too_new);
1938 return IgnorePCH;
1939 }
1940
Sebastian Redl009e7f22010-10-05 16:15:19 +00001941 // Load the chained file, which is always a PCH file.
1942 switch(ReadASTCore(llvm::StringRef(BlobStart, BlobLen), PCH)) {
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00001943 case Failure: return Failure;
1944 // If we have to ignore the dependency, we'll have to ignore this too.
1945 case IgnorePCH: return IgnorePCH;
1946 case Success: break;
1947 }
1948 break;
1949 }
1950
Sebastian Redl539c5062010-08-18 23:57:32 +00001951 case TYPE_OFFSET:
Sebastian Redl9e687992010-07-19 22:06:55 +00001952 if (F.LocalNumTypes != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001953 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001954 return Failure;
1955 }
Sebastian Redl9e687992010-07-19 22:06:55 +00001956 F.TypeOffsets = (const uint32_t *)BlobStart;
1957 F.LocalNumTypes = Record[0];
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001958 break;
1959
Sebastian Redl539c5062010-08-18 23:57:32 +00001960 case DECL_OFFSET:
Sebastian Redl9e687992010-07-19 22:06:55 +00001961 if (F.LocalNumDecls != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001962 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00001963 return Failure;
1964 }
Sebastian Redl9e687992010-07-19 22:06:55 +00001965 F.DeclOffsets = (const uint32_t *)BlobStart;
1966 F.LocalNumDecls = Record[0];
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001967 break;
Douglas Gregor55abb232009-04-10 20:39:37 +00001968
Sebastian Redl539c5062010-08-18 23:57:32 +00001969 case TU_UPDATE_LEXICAL: {
Sebastian Redl4b1f4902010-07-27 18:24:41 +00001970 DeclContextInfo Info = {
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00001971 /* No visible information */ 0,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001972 reinterpret_cast<const KindDeclIDPair *>(BlobStart),
1973 BlobLen / sizeof(KindDeclIDPair)
Sebastian Redl4b1f4902010-07-27 18:24:41 +00001974 };
Douglas Gregoraa433012010-10-01 01:18:02 +00001975 DeclContextOffsets[Context ? Context->getTranslationUnitDecl() : 0]
1976 .push_back(Info);
Sebastian Redl4b1f4902010-07-27 18:24:41 +00001977 break;
1978 }
1979
Sebastian Redld7dce0a2010-08-24 00:50:04 +00001980 case UPDATE_VISIBLE: {
1981 serialization::DeclID ID = Record[0];
1982 void *Table = ASTDeclContextNameLookupTable::Create(
1983 (const unsigned char *)BlobStart + Record[1],
1984 (const unsigned char *)BlobStart,
1985 ASTDeclContextNameLookupTrait(*this));
Douglas Gregoraa433012010-10-01 01:18:02 +00001986 if (ID == 1 && Context) { // Is it the TU?
Sebastian Redld7dce0a2010-08-24 00:50:04 +00001987 DeclContextInfo Info = {
1988 Table, /* No lexical inforamtion */ 0, 0
1989 };
1990 DeclContextOffsets[Context->getTranslationUnitDecl()].push_back(Info);
1991 } else
1992 PendingVisibleUpdates[ID].push_back(Table);
1993 break;
1994 }
1995
Sebastian Redl539c5062010-08-18 23:57:32 +00001996 case REDECLS_UPDATE_LATEST: {
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00001997 assert(Record.size() % 2 == 0 && "Expected pairs of DeclIDs");
1998 for (unsigned i = 0, e = Record.size(); i < e; i += 2) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001999 DeclID First = Record[i], Latest = Record[i+1];
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002000 assert((FirstLatestDeclIDs.find(First) == FirstLatestDeclIDs.end() ||
2001 Latest > FirstLatestDeclIDs[First]) &&
2002 "The new latest is supposed to come after the previous latest");
2003 FirstLatestDeclIDs[First] = Latest;
2004 }
2005 break;
2006 }
2007
Sebastian Redl539c5062010-08-18 23:57:32 +00002008 case LANGUAGE_OPTIONS:
Douglas Gregorce3a8292010-07-27 00:27:13 +00002009 if (ParseLanguageOptions(Record) && !DisableValidation)
Douglas Gregor55abb232009-04-10 20:39:37 +00002010 return IgnorePCH;
2011 break;
Douglas Gregorbfbde532009-04-10 21:16:55 +00002012
Sebastian Redl539c5062010-08-18 23:57:32 +00002013 case IDENTIFIER_TABLE:
Sebastian Redl393f8b72010-07-19 20:52:06 +00002014 F.IdentifierTableData = BlobStart;
Douglas Gregor0e149972009-04-25 19:10:14 +00002015 if (Record[0]) {
Sebastian Redl393f8b72010-07-19 20:52:06 +00002016 F.IdentifierLookupTable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002017 = ASTIdentifierLookupTable::Create(
Sebastian Redl393f8b72010-07-19 20:52:06 +00002018 (const unsigned char *)F.IdentifierTableData + Record[0],
2019 (const unsigned char *)F.IdentifierTableData,
Sebastian Redl2c373b92010-10-05 15:59:54 +00002020 ASTIdentifierLookupTrait(*this, F));
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002021 if (PP)
2022 PP->getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor0e149972009-04-25 19:10:14 +00002023 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002024 break;
2025
Sebastian Redl539c5062010-08-18 23:57:32 +00002026 case IDENTIFIER_OFFSET:
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002027 if (F.LocalNumIdentifiers != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002028 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002029 return Failure;
2030 }
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002031 F.IdentifierOffsets = (const uint32_t *)BlobStart;
2032 F.LocalNumIdentifiers = Record[0];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002033 break;
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002034
Sebastian Redl539c5062010-08-18 23:57:32 +00002035 case EXTERNAL_DEFINITIONS:
Sebastian Redlb293a452010-07-20 21:20:32 +00002036 // Optimization for the first block.
2037 if (ExternalDefinitions.empty())
2038 ExternalDefinitions.swap(Record);
2039 else
2040 ExternalDefinitions.insert(ExternalDefinitions.end(),
2041 Record.begin(), Record.end());
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002042 break;
Douglas Gregor08f01292009-04-17 22:13:46 +00002043
Sebastian Redl539c5062010-08-18 23:57:32 +00002044 case SPECIAL_TYPES:
Sebastian Redlb293a452010-07-20 21:20:32 +00002045 // Optimization for the first block
2046 if (SpecialTypes.empty())
2047 SpecialTypes.swap(Record);
2048 else
2049 SpecialTypes.insert(SpecialTypes.end(), Record.begin(), Record.end());
Douglas Gregor652d82a2009-04-18 05:55:16 +00002050 break;
2051
Sebastian Redl539c5062010-08-18 23:57:32 +00002052 case STATISTICS:
Sebastian Redlb293a452010-07-20 21:20:32 +00002053 TotalNumStatements += Record[0];
2054 TotalNumMacros += Record[1];
2055 TotalLexicalDeclContexts += Record[2];
2056 TotalVisibleDeclContexts += Record[3];
Douglas Gregor08f01292009-04-17 22:13:46 +00002057 break;
Douglas Gregor258ae542009-04-27 06:38:32 +00002058
Sebastian Redl539c5062010-08-18 23:57:32 +00002059 case TENTATIVE_DEFINITIONS:
Sebastian Redlb293a452010-07-20 21:20:32 +00002060 // Optimization for the first block.
2061 if (TentativeDefinitions.empty())
2062 TentativeDefinitions.swap(Record);
2063 else
2064 TentativeDefinitions.insert(TentativeDefinitions.end(),
2065 Record.begin(), Record.end());
Douglas Gregord4df8652009-04-22 22:02:47 +00002066 break;
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002067
Sebastian Redl539c5062010-08-18 23:57:32 +00002068 case UNUSED_FILESCOPED_DECLS:
Sebastian Redlb293a452010-07-20 21:20:32 +00002069 // Optimization for the first block.
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002070 if (UnusedFileScopedDecls.empty())
2071 UnusedFileScopedDecls.swap(Record);
Sebastian Redlb293a452010-07-20 21:20:32 +00002072 else
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002073 UnusedFileScopedDecls.insert(UnusedFileScopedDecls.end(),
2074 Record.begin(), Record.end());
Tanya Lattner90073802010-02-12 00:07:30 +00002075 break;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00002076
Sebastian Redl539c5062010-08-18 23:57:32 +00002077 case WEAK_UNDECLARED_IDENTIFIERS:
Sebastian Redl08aca90252010-08-05 18:21:25 +00002078 // Later blocks overwrite earlier ones.
2079 WeakUndeclaredIdentifiers.swap(Record);
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002080 break;
2081
Sebastian Redl539c5062010-08-18 23:57:32 +00002082 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Sebastian Redlb293a452010-07-20 21:20:32 +00002083 // Optimization for the first block.
2084 if (LocallyScopedExternalDecls.empty())
2085 LocallyScopedExternalDecls.swap(Record);
2086 else
2087 LocallyScopedExternalDecls.insert(LocallyScopedExternalDecls.end(),
2088 Record.begin(), Record.end());
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002089 break;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002090
Sebastian Redl539c5062010-08-18 23:57:32 +00002091 case SELECTOR_OFFSETS:
Sebastian Redla19a67f2010-08-03 21:58:15 +00002092 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redlada023c2010-08-04 20:40:17 +00002093 F.LocalNumSelectors = Record[0];
Douglas Gregor95c13f52009-04-25 17:48:32 +00002094 break;
2095
Sebastian Redl539c5062010-08-18 23:57:32 +00002096 case METHOD_POOL:
Sebastian Redlada023c2010-08-04 20:40:17 +00002097 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor95c13f52009-04-25 17:48:32 +00002098 if (Record[0])
Sebastian Redlada023c2010-08-04 20:40:17 +00002099 F.SelectorLookupTable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002100 = ASTSelectorLookupTable::Create(
Sebastian Redlada023c2010-08-04 20:40:17 +00002101 F.SelectorLookupTableData + Record[0],
2102 F.SelectorLookupTableData,
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002103 ASTSelectorLookupTrait(*this));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002104 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorc78d3462009-04-24 21:10:55 +00002105 break;
Douglas Gregoreda6a892009-04-26 00:07:37 +00002106
Sebastian Redl96371b42010-09-22 00:42:30 +00002107 case REFERENCED_SELECTOR_POOL:
Sebastian Redl2c373b92010-10-05 15:59:54 +00002108 F.ReferencedSelectorsData.swap(Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002109 break;
2110
Sebastian Redl539c5062010-08-18 23:57:32 +00002111 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002112 if (!Record.empty() && Listener)
2113 Listener->ReadCounter(Record[0]);
Douglas Gregoreda6a892009-04-26 00:07:37 +00002114 break;
Douglas Gregor258ae542009-04-27 06:38:32 +00002115
Sebastian Redl539c5062010-08-18 23:57:32 +00002116 case SOURCE_LOCATION_OFFSETS:
Sebastian Redlb293a452010-07-20 21:20:32 +00002117 F.SLocOffsets = (const uint32_t *)BlobStart;
2118 F.LocalNumSLocEntries = Record[0];
Sebastian Redlc1d035f2010-09-22 20:19:08 +00002119 F.LocalSLocSize = Record[1];
Douglas Gregor258ae542009-04-27 06:38:32 +00002120 break;
2121
Sebastian Redl539c5062010-08-18 23:57:32 +00002122 case SOURCE_LOCATION_PRELOADS:
Sebastian Redl96371b42010-09-22 00:42:30 +00002123 if (PreloadSLocEntries.empty())
2124 PreloadSLocEntries.swap(Record);
2125 else
2126 PreloadSLocEntries.insert(PreloadSLocEntries.end(),
2127 Record.begin(), Record.end());
Douglas Gregor258ae542009-04-27 06:38:32 +00002128 break;
Douglas Gregorc5046832009-04-27 18:38:38 +00002129
Sebastian Redl539c5062010-08-18 23:57:32 +00002130 case STAT_CACHE: {
Douglas Gregor606c4ac2011-02-05 19:42:43 +00002131 if (!DisableStatCache) {
2132 ASTStatCache *MyStatCache =
2133 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
2134 (const unsigned char *)BlobStart,
2135 NumStatHits, NumStatMisses);
2136 FileMgr.addStatCache(MyStatCache);
2137 F.StatCache = MyStatCache;
2138 }
Douglas Gregorc5046832009-04-27 18:38:38 +00002139 break;
Douglas Gregord2eb58a2009-10-16 18:18:30 +00002140 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00002141
Sebastian Redl539c5062010-08-18 23:57:32 +00002142 case EXT_VECTOR_DECLS:
Sebastian Redl04f5c312010-07-28 21:38:49 +00002143 // Optimization for the first block.
2144 if (ExtVectorDecls.empty())
2145 ExtVectorDecls.swap(Record);
2146 else
2147 ExtVectorDecls.insert(ExtVectorDecls.end(),
2148 Record.begin(), Record.end());
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002149 break;
2150
Sebastian Redl539c5062010-08-18 23:57:32 +00002151 case VTABLE_USES:
Sebastian Redl08aca90252010-08-05 18:21:25 +00002152 // Later tables overwrite earlier ones.
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002153 VTableUses.swap(Record);
2154 break;
2155
Sebastian Redl539c5062010-08-18 23:57:32 +00002156 case DYNAMIC_CLASSES:
Sebastian Redl08aca90252010-08-05 18:21:25 +00002157 // Optimization for the first block.
2158 if (DynamicClasses.empty())
2159 DynamicClasses.swap(Record);
2160 else
2161 DynamicClasses.insert(DynamicClasses.end(),
2162 Record.begin(), Record.end());
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002163 break;
2164
Sebastian Redl539c5062010-08-18 23:57:32 +00002165 case PENDING_IMPLICIT_INSTANTIATIONS:
Sebastian Redl2c373b92010-10-05 15:59:54 +00002166 F.PendingInstantiations.swap(Record);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002167 break;
2168
Sebastian Redl539c5062010-08-18 23:57:32 +00002169 case SEMA_DECL_REFS:
Sebastian Redl08aca90252010-08-05 18:21:25 +00002170 // Later tables overwrite earlier ones.
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002171 SemaDeclRefs.swap(Record);
2172 break;
2173
Sebastian Redl539c5062010-08-18 23:57:32 +00002174 case ORIGINAL_FILE_NAME:
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002175 // The primary AST will be the last to get here, so it will be the one
Sebastian Redlb293a452010-07-20 21:20:32 +00002176 // that's used.
Daniel Dunbar000c4ff2009-11-11 05:29:04 +00002177 ActualOriginalFileName.assign(BlobStart, BlobLen);
2178 OriginalFileName = ActualOriginalFileName;
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002179 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregor45fe0362009-05-12 01:31:05 +00002180 break;
Mike Stump11289f42009-09-09 15:08:12 +00002181
Sebastian Redl539c5062010-08-18 23:57:32 +00002182 case VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek8bd09292010-02-12 23:31:14 +00002183 const std::string &CurBranch = getClangFullRepositoryVersion();
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002184 llvm::StringRef ASTBranch(BlobStart, BlobLen);
2185 if (llvm::StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2186 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregord54f3a12009-10-05 21:07:28 +00002187 return IgnorePCH;
2188 }
2189 break;
2190 }
Sebastian Redlfa061442010-07-21 20:07:32 +00002191
Sebastian Redl539c5062010-08-18 23:57:32 +00002192 case MACRO_DEFINITION_OFFSETS:
Sebastian Redlfa061442010-07-21 20:07:32 +00002193 F.MacroDefinitionOffsets = (const uint32_t *)BlobStart;
2194 F.NumPreallocatedPreprocessingEntities = Record[0];
2195 F.LocalNumMacroDefinitions = Record[1];
Douglas Gregoraae92242010-03-19 21:51:54 +00002196 break;
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002197
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002198 case DECL_UPDATE_OFFSETS: {
2199 if (Record.size() % 2 != 0) {
2200 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2201 return Failure;
2202 }
2203 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
2204 DeclUpdateOffsets[static_cast<DeclID>(Record[I])]
2205 .push_back(std::make_pair(&F, Record[I+1]));
2206 break;
2207 }
2208
Sebastian Redl539c5062010-08-18 23:57:32 +00002209 case DECL_REPLACEMENTS: {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002210 if (Record.size() % 2 != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002211 Error("invalid DECL_REPLACEMENTS block in AST file");
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002212 return Failure;
2213 }
2214 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Sebastian Redl539c5062010-08-18 23:57:32 +00002215 ReplacedDecls[static_cast<DeclID>(Record[I])] =
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002216 std::make_pair(&F, Record[I+1]);
2217 break;
2218 }
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002219
2220 case CXX_BASE_SPECIFIER_OFFSETS: {
2221 if (F.LocalNumCXXBaseSpecifiers != 0) {
2222 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2223 return Failure;
2224 }
2225
2226 F.LocalNumCXXBaseSpecifiers = Record[0];
2227 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart;
2228 break;
2229 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002230
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002231 case DIAG_PRAGMA_MAPPINGS:
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002232 if (Record.size() % 2 != 0) {
2233 Error("invalid DIAG_USER_MAPPINGS block in AST file");
2234 return Failure;
2235 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002236 if (PragmaDiagMappings.empty())
2237 PragmaDiagMappings.swap(Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002238 else
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002239 PragmaDiagMappings.insert(PragmaDiagMappings.end(),
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002240 Record.begin(), Record.end());
2241 break;
Douglas Gregor09b69892011-02-10 17:09:37 +00002242
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002243 case CUDA_SPECIAL_DECL_REFS:
2244 // Later tables overwrite earlier ones.
2245 CUDASpecialDeclRefs.swap(Record);
2246 break;
Douglas Gregor09b69892011-02-10 17:09:37 +00002247
2248 case HEADER_SEARCH_TABLE:
2249 F.HeaderFileInfoTableData = BlobStart;
2250 F.LocalNumHeaderFileInfos = Record[1];
2251 if (Record[0]) {
2252 F.HeaderFileInfoTable
2253 = HeaderFileInfoLookupTable::Create(
2254 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
2255 (const unsigned char *)F.HeaderFileInfoTableData);
2256 if (PP)
2257 PP->getHeaderSearchInfo().SetExternalSource(this);
2258 }
2259 break;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002260 }
Sebastian Redl393f8b72010-07-19 20:52:06 +00002261 First = false;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002262 }
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002263 Error("premature end of bitstream in AST file");
Douglas Gregor55abb232009-04-10 20:39:37 +00002264 return Failure;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002265}
2266
Sebastian Redl009e7f22010-10-05 16:15:19 +00002267ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
2268 ASTFileType Type) {
2269 switch(ReadASTCore(FileName, Type)) {
Sebastian Redl2abc0382010-07-16 20:41:52 +00002270 case Failure: return Failure;
2271 case IgnorePCH: return IgnorePCH;
2272 case Success: break;
2273 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002274
2275 // Here comes stuff that we only do once the entire chain is loaded.
2276
Sebastian Redl96371b42010-09-22 00:42:30 +00002277 // Allocate space for loaded slocentries, identifiers, decls and types.
Sebastian Redlfa061442010-07-21 20:07:32 +00002278 unsigned TotalNumIdentifiers = 0, TotalNumTypes = 0, TotalNumDecls = 0,
Sebastian Redlada023c2010-08-04 20:40:17 +00002279 TotalNumPreallocatedPreprocessingEntities = 0, TotalNumMacroDefs = 0,
2280 TotalNumSelectors = 0;
Sebastian Redl9e687992010-07-19 22:06:55 +00002281 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl96371b42010-09-22 00:42:30 +00002282 TotalNumSLocEntries += Chain[I]->LocalNumSLocEntries;
Sebastian Redlc1d035f2010-09-22 20:19:08 +00002283 NextSLocOffset += Chain[I]->LocalSLocSize;
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002284 TotalNumIdentifiers += Chain[I]->LocalNumIdentifiers;
Sebastian Redl9e687992010-07-19 22:06:55 +00002285 TotalNumTypes += Chain[I]->LocalNumTypes;
2286 TotalNumDecls += Chain[I]->LocalNumDecls;
Sebastian Redlfa061442010-07-21 20:07:32 +00002287 TotalNumPreallocatedPreprocessingEntities +=
2288 Chain[I]->NumPreallocatedPreprocessingEntities;
2289 TotalNumMacroDefs += Chain[I]->LocalNumMacroDefinitions;
Sebastian Redlada023c2010-08-04 20:40:17 +00002290 TotalNumSelectors += Chain[I]->LocalNumSelectors;
Sebastian Redl9e687992010-07-19 22:06:55 +00002291 }
Sebastian Redlc1d035f2010-09-22 20:19:08 +00002292 SourceMgr.PreallocateSLocEntries(this, TotalNumSLocEntries, NextSLocOffset);
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002293 IdentifiersLoaded.resize(TotalNumIdentifiers);
Sebastian Redl9e687992010-07-19 22:06:55 +00002294 TypesLoaded.resize(TotalNumTypes);
2295 DeclsLoaded.resize(TotalNumDecls);
Sebastian Redlfa061442010-07-21 20:07:32 +00002296 MacroDefinitionsLoaded.resize(TotalNumMacroDefs);
2297 if (PP) {
2298 if (TotalNumIdentifiers > 0)
2299 PP->getHeaderSearchInfo().SetExternalLookup(this);
2300 if (TotalNumPreallocatedPreprocessingEntities > 0) {
2301 if (!PP->getPreprocessingRecord())
2302 PP->createPreprocessingRecord();
2303 PP->getPreprocessingRecord()->SetExternalSource(*this,
2304 TotalNumPreallocatedPreprocessingEntities);
2305 }
2306 }
Sebastian Redlada023c2010-08-04 20:40:17 +00002307 SelectorsLoaded.resize(TotalNumSelectors);
Sebastian Redl96371b42010-09-22 00:42:30 +00002308 // Preload SLocEntries.
2309 for (unsigned I = 0, N = PreloadSLocEntries.size(); I != N; ++I) {
2310 ASTReadResult Result = ReadSLocEntryRecord(PreloadSLocEntries[I]);
2311 if (Result != Success)
2312 return Result;
2313 }
Sebastian Redl9e687992010-07-19 22:06:55 +00002314
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002315 // Check the predefines buffers.
Douglas Gregorce3a8292010-07-27 00:27:13 +00002316 if (!DisableValidation && CheckPredefinesBuffers())
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002317 return IgnorePCH;
2318
2319 if (PP) {
2320 // Initialization of keywords and pragmas occurs before the
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002321 // AST file is read, so there may be some identifiers that were
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002322 // loaded into the IdentifierTable before we intercepted the
2323 // creation of identifiers. Iterate through the list of known
2324 // identifiers and determine whether we have to establish
2325 // preprocessor definitions or top-level identifier declaration
2326 // chains for those identifiers.
2327 //
2328 // We copy the IdentifierInfo pointers to a small vector first,
2329 // since de-serializing declarations or macro definitions can add
2330 // new entries into the identifier table, invalidating the
2331 // iterators.
2332 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
2333 for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(),
2334 IdEnd = PP->getIdentifierTable().end();
2335 Id != IdEnd; ++Id)
2336 Identifiers.push_back(Id->second);
Sebastian Redlfa061442010-07-21 20:07:32 +00002337 // We need to search the tables in all files.
Sebastian Redlfa061442010-07-21 20:07:32 +00002338 for (unsigned J = 0, M = Chain.size(); J != M; ++J) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002339 ASTIdentifierLookupTable *IdTable
2340 = (ASTIdentifierLookupTable *)Chain[J]->IdentifierLookupTable;
2341 // Not all AST files necessarily have identifier tables, only the useful
Sebastian Redl5c415f32010-07-22 17:01:13 +00002342 // ones.
2343 if (!IdTable)
2344 continue;
Sebastian Redlfa061442010-07-21 20:07:32 +00002345 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
2346 IdentifierInfo *II = Identifiers[I];
2347 // Look in the on-disk hash tables for an entry for this identifier
Sebastian Redl2c373b92010-10-05 15:59:54 +00002348 ASTIdentifierLookupTrait Info(*this, *Chain[J], II);
Sebastian Redlfa061442010-07-21 20:07:32 +00002349 std::pair<const char*,unsigned> Key(II->getNameStart(),II->getLength());
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002350 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
Sebastian Redlb293a452010-07-20 21:20:32 +00002351 if (Pos == IdTable->end())
2352 continue;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002353
Sebastian Redlb293a452010-07-20 21:20:32 +00002354 // Dereferencing the iterator has the effect of populating the
2355 // IdentifierInfo node with the various declarations it needs.
2356 (void)*Pos;
2357 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002358 }
2359 }
2360
2361 if (Context)
2362 InitializeContext(*Context);
2363
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002364 if (DeserializationListener)
2365 DeserializationListener->ReaderInitialized(this);
2366
Douglas Gregor936a5b42010-11-30 05:23:00 +00002367 // If this AST file is a precompiled preamble, then set the main file ID of
2368 // the source manager to the file source file from which the preamble was
2369 // built. This is the only valid way to use a precompiled preamble.
2370 if (Type == Preamble) {
2371 SourceLocation Loc
2372 = SourceMgr.getLocation(FileMgr.getFile(getOriginalSourceFile()), 1, 1);
2373 if (Loc.isValid()) {
2374 std::pair<FileID, unsigned> Decomposed = SourceMgr.getDecomposedLoc(Loc);
2375 SourceMgr.SetPreambleFileID(Decomposed.first);
2376 }
2377 }
2378
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002379 return Success;
2380}
2381
Sebastian Redl009e7f22010-10-05 16:15:19 +00002382ASTReader::ASTReadResult ASTReader::ReadASTCore(llvm::StringRef FileName,
2383 ASTFileType Type) {
Sebastian Redl3f6b7532010-10-01 19:59:12 +00002384 PerFileData *Prev = Chain.empty() ? 0 : Chain.back();
Sebastian Redl009e7f22010-10-05 16:15:19 +00002385 Chain.push_back(new PerFileData(Type));
Sebastian Redl34522812010-07-16 17:50:48 +00002386 PerFileData &F = *Chain.back();
Sebastian Redl3f6b7532010-10-01 19:59:12 +00002387 if (Prev)
2388 Prev->NextInSource = &F;
2389 else
2390 FirstInSource = &F;
2391 F.Loaders.push_back(Prev);
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002392
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002393 // Set the AST file name.
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002394 F.FileName = FileName;
2395
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002396 // Open the AST file.
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002397 //
2398 // FIXME: This shouldn't be here, we should just take a raw_ostream.
2399 std::string ErrStr;
Michael J. Spencerf25faaa2010-12-09 17:36:38 +00002400 llvm::error_code ec;
2401 if (FileName == "-") {
Michael J. Spencerd9da7a12010-12-16 03:28:14 +00002402 ec = llvm::MemoryBuffer::getSTDIN(F.Buffer);
Michael J. Spencerf25faaa2010-12-09 17:36:38 +00002403 if (ec)
2404 ErrStr = ec.message();
2405 } else
Chris Lattner5159f612010-11-23 08:35:12 +00002406 F.Buffer.reset(FileMgr.getBufferForFile(FileName, &ErrStr));
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002407 if (!F.Buffer) {
2408 Error(ErrStr.c_str());
2409 return IgnorePCH;
2410 }
2411
2412 // Initialize the stream
2413 F.StreamFile.init((const unsigned char *)F.Buffer->getBufferStart(),
2414 (const unsigned char *)F.Buffer->getBufferEnd());
Sebastian Redl34522812010-07-16 17:50:48 +00002415 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002416 Stream.init(F.StreamFile);
Sebastian Redlfa061442010-07-21 20:07:32 +00002417 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002418
2419 // Sniff for the signature.
2420 if (Stream.Read(8) != 'C' ||
2421 Stream.Read(8) != 'P' ||
2422 Stream.Read(8) != 'C' ||
2423 Stream.Read(8) != 'H') {
2424 Diag(diag::err_not_a_pch_file) << FileName;
2425 return Failure;
2426 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002427
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002428 while (!Stream.AtEndOfStream()) {
2429 unsigned Code = Stream.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00002430
Douglas Gregor92863e42009-04-10 23:10:45 +00002431 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002432 Error("invalid record at top-level of AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002433 return Failure;
2434 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002435
2436 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregora868bbd2009-04-21 22:25:48 +00002437
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002438 // We only know the AST subblock ID.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002439 switch (BlockID) {
2440 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregor92863e42009-04-10 23:10:45 +00002441 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002442 Error("malformed BlockInfoBlock in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002443 return Failure;
2444 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002445 break;
Sebastian Redl539c5062010-08-18 23:57:32 +00002446 case AST_BLOCK_ID:
Sebastian Redl3e31c722010-08-18 23:56:56 +00002447 switch (ReadASTBlock(F)) {
Douglas Gregor55abb232009-04-10 20:39:37 +00002448 case Success:
2449 break;
2450
2451 case Failure:
Douglas Gregor92863e42009-04-10 23:10:45 +00002452 return Failure;
Douglas Gregor55abb232009-04-10 20:39:37 +00002453
2454 case IgnorePCH:
Douglas Gregorbfbde532009-04-10 21:16:55 +00002455 // FIXME: We could consider reading through to the end of this
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002456 // AST block, skipping subblocks, to see if there are other
2457 // AST blocks elsewhere.
Douglas Gregor0bc12932009-04-27 21:28:04 +00002458
2459 // Clear out any preallocated source location entries, so that
2460 // the source manager does not try to resolve them later.
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002461 SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor0bc12932009-04-27 21:28:04 +00002462
2463 // Remove the stat cache.
Sebastian Redl34522812010-07-16 17:50:48 +00002464 if (F.StatCache)
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002465 FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
Douglas Gregor0bc12932009-04-27 21:28:04 +00002466
Douglas Gregor92863e42009-04-10 23:10:45 +00002467 return IgnorePCH;
Douglas Gregor55abb232009-04-10 20:39:37 +00002468 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002469 break;
2470 default:
Douglas Gregor92863e42009-04-10 23:10:45 +00002471 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002472 Error("malformed block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002473 return Failure;
2474 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002475 break;
2476 }
Mike Stump11289f42009-09-09 15:08:12 +00002477 }
2478
Sebastian Redl2abc0382010-07-16 20:41:52 +00002479 return Success;
2480}
2481
Sebastian Redl2c499f62010-08-18 23:56:43 +00002482void ASTReader::setPreprocessor(Preprocessor &pp) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002483 PP = &pp;
Sebastian Redlfa061442010-07-21 20:07:32 +00002484
2485 unsigned TotalNum = 0;
2486 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
2487 TotalNum += Chain[I]->NumPreallocatedPreprocessingEntities;
2488 if (TotalNum) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002489 if (!PP->getPreprocessingRecord())
2490 PP->createPreprocessingRecord();
Sebastian Redlfa061442010-07-21 20:07:32 +00002491 PP->getPreprocessingRecord()->SetExternalSource(*this, TotalNum);
Douglas Gregoraae92242010-03-19 21:51:54 +00002492 }
2493}
2494
Sebastian Redl2c499f62010-08-18 23:56:43 +00002495void ASTReader::InitializeContext(ASTContext &Ctx) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002496 Context = &Ctx;
2497 assert(Context && "Passed null context!");
2498
2499 assert(PP && "Forgot to set Preprocessor ?");
2500 PP->getIdentifierTable().setExternalIdentifierLookup(this);
2501 PP->getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregor9882a5a2010-01-04 19:18:44 +00002502 PP->setExternalSource(this);
Douglas Gregor09b69892011-02-10 17:09:37 +00002503 PP->getHeaderSearchInfo().SetExternalSource(this);
2504
Douglas Gregoraa433012010-10-01 01:18:02 +00002505 // If we have an update block for the TU waiting, we have to add it before
2506 // deserializing the decl.
2507 DeclContextOffsetsMap::iterator DCU = DeclContextOffsets.find(0);
2508 if (DCU != DeclContextOffsets.end()) {
2509 // Insertion could invalidate map, so grab vector.
2510 DeclContextInfos T;
2511 T.swap(DCU->second);
2512 DeclContextOffsets.erase(DCU);
2513 DeclContextOffsets[Ctx.getTranslationUnitDecl()].swap(T);
2514 }
2515
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002516 // Load the translation unit declaration
Argyrios Kyrtzidis7e8996c2010-07-08 17:13:02 +00002517 GetTranslationUnitDecl();
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002518
2519 // Load the special types.
2520 Context->setBuiltinVaListType(
Sebastian Redl539c5062010-08-18 23:57:32 +00002521 GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST]));
2522 if (unsigned Id = SpecialTypes[SPECIAL_TYPE_OBJC_ID])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002523 Context->setObjCIdType(GetType(Id));
Sebastian Redl539c5062010-08-18 23:57:32 +00002524 if (unsigned Sel = SpecialTypes[SPECIAL_TYPE_OBJC_SELECTOR])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002525 Context->setObjCSelType(GetType(Sel));
Sebastian Redl539c5062010-08-18 23:57:32 +00002526 if (unsigned Proto = SpecialTypes[SPECIAL_TYPE_OBJC_PROTOCOL])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002527 Context->setObjCProtoType(GetType(Proto));
Sebastian Redl539c5062010-08-18 23:57:32 +00002528 if (unsigned Class = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002529 Context->setObjCClassType(GetType(Class));
Steve Naroff7cae42b2009-07-10 23:34:53 +00002530
Sebastian Redl539c5062010-08-18 23:57:32 +00002531 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002532 Context->setCFConstantStringType(GetType(String));
Mike Stump11289f42009-09-09 15:08:12 +00002533 if (unsigned FastEnum
Sebastian Redl539c5062010-08-18 23:57:32 +00002534 = SpecialTypes[SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002535 Context->setObjCFastEnumerationStateType(GetType(FastEnum));
Sebastian Redl539c5062010-08-18 23:57:32 +00002536 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
Douglas Gregor27821ce2009-07-07 16:35:42 +00002537 QualType FileType = GetType(File);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002538 if (FileType.isNull()) {
2539 Error("FILE type is NULL");
2540 return;
2541 }
John McCall9dd450b2009-09-21 23:43:11 +00002542 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
Douglas Gregor27821ce2009-07-07 16:35:42 +00002543 Context->setFILEDecl(Typedef->getDecl());
2544 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002545 const TagType *Tag = FileType->getAs<TagType>();
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002546 if (!Tag) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002547 Error("Invalid FILE type in AST file");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002548 return;
2549 }
Douglas Gregor27821ce2009-07-07 16:35:42 +00002550 Context->setFILEDecl(Tag->getDecl());
2551 }
2552 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002553 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_jmp_buf]) {
Mike Stumpa4de80b2009-07-28 02:25:19 +00002554 QualType Jmp_bufType = GetType(Jmp_buf);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002555 if (Jmp_bufType.isNull()) {
2556 Error("jmp_bug type is NULL");
2557 return;
2558 }
John McCall9dd450b2009-09-21 23:43:11 +00002559 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
Mike Stumpa4de80b2009-07-28 02:25:19 +00002560 Context->setjmp_bufDecl(Typedef->getDecl());
2561 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002562 const TagType *Tag = Jmp_bufType->getAs<TagType>();
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002563 if (!Tag) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002564 Error("Invalid jmp_buf type in AST file");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002565 return;
2566 }
Mike Stumpa4de80b2009-07-28 02:25:19 +00002567 Context->setjmp_bufDecl(Tag->getDecl());
2568 }
2569 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002570 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_sigjmp_buf]) {
Mike Stumpa4de80b2009-07-28 02:25:19 +00002571 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002572 if (Sigjmp_bufType.isNull()) {
2573 Error("sigjmp_buf type is NULL");
2574 return;
2575 }
John McCall9dd450b2009-09-21 23:43:11 +00002576 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
Mike Stumpa4de80b2009-07-28 02:25:19 +00002577 Context->setsigjmp_bufDecl(Typedef->getDecl());
2578 else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002579 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002580 assert(Tag && "Invalid sigjmp_buf type in AST file");
Mike Stumpa4de80b2009-07-28 02:25:19 +00002581 Context->setsigjmp_bufDecl(Tag->getDecl());
2582 }
2583 }
Mike Stump11289f42009-09-09 15:08:12 +00002584 if (unsigned ObjCIdRedef
Sebastian Redl539c5062010-08-18 23:57:32 +00002585 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION])
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002586 Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef);
Mike Stump11289f42009-09-09 15:08:12 +00002587 if (unsigned ObjCClassRedef
Sebastian Redl539c5062010-08-18 23:57:32 +00002588 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION])
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002589 Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef);
Sebastian Redl539c5062010-08-18 23:57:32 +00002590 if (unsigned String = SpecialTypes[SPECIAL_TYPE_BLOCK_DESCRIPTOR])
Mike Stumpd0153282009-10-20 02:12:22 +00002591 Context->setBlockDescriptorType(GetType(String));
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002592 if (unsigned String
Sebastian Redl539c5062010-08-18 23:57:32 +00002593 = SpecialTypes[SPECIAL_TYPE_BLOCK_EXTENDED_DESCRIPTOR])
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002594 Context->setBlockDescriptorExtendedType(GetType(String));
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002595 if (unsigned ObjCSelRedef
Sebastian Redl539c5062010-08-18 23:57:32 +00002596 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION])
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002597 Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef);
Sebastian Redl539c5062010-08-18 23:57:32 +00002598 if (unsigned String = SpecialTypes[SPECIAL_TYPE_NS_CONSTANT_STRING])
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002599 Context->setNSConstantStringType(GetType(String));
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002600
Sebastian Redl539c5062010-08-18 23:57:32 +00002601 if (SpecialTypes[SPECIAL_TYPE_INT128_INSTALLED])
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002602 Context->setInt128Installed();
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002603
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002604 ReadPragmaDiagnosticMappings(Context->getDiagnostics());
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002605
2606 // If there were any CUDA special declarations, deserialize them.
2607 if (!CUDASpecialDeclRefs.empty()) {
2608 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
2609 Context->setcudaConfigureCallDecl(
2610 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
2611 }
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002612}
2613
Douglas Gregor45fe0362009-05-12 01:31:05 +00002614/// \brief Retrieve the name of the original source file name
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002615/// directly from the AST file, without actually loading the AST
Douglas Gregor45fe0362009-05-12 01:31:05 +00002616/// file.
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002617std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +00002618 FileManager &FileMgr,
Daniel Dunbar3b951482009-12-03 09:13:06 +00002619 Diagnostic &Diags) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002620 // Open the AST file.
Douglas Gregor45fe0362009-05-12 01:31:05 +00002621 std::string ErrStr;
2622 llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
Chris Lattner5159f612010-11-23 08:35:12 +00002623 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
Douglas Gregor45fe0362009-05-12 01:31:05 +00002624 if (!Buffer) {
Daniel Dunbar3b951482009-12-03 09:13:06 +00002625 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002626 return std::string();
2627 }
2628
2629 // Initialize the stream
2630 llvm::BitstreamReader StreamFile;
2631 llvm::BitstreamCursor Stream;
Mike Stump11289f42009-09-09 15:08:12 +00002632 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregor45fe0362009-05-12 01:31:05 +00002633 (const unsigned char *)Buffer->getBufferEnd());
2634 Stream.init(StreamFile);
2635
2636 // Sniff for the signature.
2637 if (Stream.Read(8) != 'C' ||
2638 Stream.Read(8) != 'P' ||
2639 Stream.Read(8) != 'C' ||
2640 Stream.Read(8) != 'H') {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002641 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002642 return std::string();
2643 }
2644
2645 RecordData Record;
2646 while (!Stream.AtEndOfStream()) {
2647 unsigned Code = Stream.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00002648
Douglas Gregor45fe0362009-05-12 01:31:05 +00002649 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2650 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump11289f42009-09-09 15:08:12 +00002651
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002652 // We only know the AST subblock ID.
Douglas Gregor45fe0362009-05-12 01:31:05 +00002653 switch (BlockID) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002654 case AST_BLOCK_ID:
2655 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002656 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002657 return std::string();
2658 }
2659 break;
Mike Stump11289f42009-09-09 15:08:12 +00002660
Douglas Gregor45fe0362009-05-12 01:31:05 +00002661 default:
2662 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002663 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002664 return std::string();
2665 }
2666 break;
2667 }
2668 continue;
2669 }
2670
2671 if (Code == llvm::bitc::END_BLOCK) {
2672 if (Stream.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002673 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00002674 return std::string();
2675 }
2676 continue;
2677 }
2678
2679 if (Code == llvm::bitc::DEFINE_ABBREV) {
2680 Stream.ReadAbbrevRecord();
2681 continue;
2682 }
2683
2684 Record.clear();
2685 const char *BlobStart = 0;
2686 unsigned BlobLen = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002687 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Sebastian Redl539c5062010-08-18 23:57:32 +00002688 == ORIGINAL_FILE_NAME)
Douglas Gregor45fe0362009-05-12 01:31:05 +00002689 return std::string(BlobStart, BlobLen);
Mike Stump11289f42009-09-09 15:08:12 +00002690 }
Douglas Gregor45fe0362009-05-12 01:31:05 +00002691
2692 return std::string();
2693}
2694
Douglas Gregor55abb232009-04-10 20:39:37 +00002695/// \brief Parse the record that corresponds to a LangOptions data
2696/// structure.
2697///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002698/// This routine parses the language options from the AST file and then gives
2699/// them to the AST listener if one is set.
Douglas Gregor55abb232009-04-10 20:39:37 +00002700///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002701/// \returns true if the listener deems the file unacceptable, false otherwise.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002702bool ASTReader::ParseLanguageOptions(
Douglas Gregor55abb232009-04-10 20:39:37 +00002703 const llvm::SmallVectorImpl<uint64_t> &Record) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002704 if (Listener) {
2705 LangOptions LangOpts;
Mike Stump11289f42009-09-09 15:08:12 +00002706
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002707 #define PARSE_LANGOPT(Option) \
2708 LangOpts.Option = Record[Idx]; \
2709 ++Idx
Mike Stump11289f42009-09-09 15:08:12 +00002710
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002711 unsigned Idx = 0;
2712 PARSE_LANGOPT(Trigraphs);
2713 PARSE_LANGOPT(BCPLComment);
2714 PARSE_LANGOPT(DollarIdents);
2715 PARSE_LANGOPT(AsmPreprocessor);
2716 PARSE_LANGOPT(GNUMode);
Chandler Carruthe03aa552010-04-17 20:17:31 +00002717 PARSE_LANGOPT(GNUKeywords);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002718 PARSE_LANGOPT(ImplicitInt);
2719 PARSE_LANGOPT(Digraphs);
2720 PARSE_LANGOPT(HexFloats);
2721 PARSE_LANGOPT(C99);
2722 PARSE_LANGOPT(Microsoft);
2723 PARSE_LANGOPT(CPlusPlus);
2724 PARSE_LANGOPT(CPlusPlus0x);
2725 PARSE_LANGOPT(CXXOperatorNames);
2726 PARSE_LANGOPT(ObjC1);
2727 PARSE_LANGOPT(ObjC2);
2728 PARSE_LANGOPT(ObjCNonFragileABI);
Fariborz Jahanian45878032010-02-09 19:31:38 +00002729 PARSE_LANGOPT(ObjCNonFragileABI2);
Fariborz Jahanian13f3b2f2011-01-07 18:59:25 +00002730 PARSE_LANGOPT(AppleKext);
Ted Kremenek1d56c9e2010-12-23 21:35:43 +00002731 PARSE_LANGOPT(ObjCDefaultSynthProperties);
Fariborz Jahanian62c56022010-04-22 21:01:59 +00002732 PARSE_LANGOPT(NoConstantCFStrings);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002733 PARSE_LANGOPT(PascalStrings);
2734 PARSE_LANGOPT(WritableStrings);
2735 PARSE_LANGOPT(LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +00002736 PARSE_LANGOPT(AltiVec);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002737 PARSE_LANGOPT(Exceptions);
Daniel Dunbar925152c2010-02-10 18:48:44 +00002738 PARSE_LANGOPT(SjLjExceptions);
Douglas Gregordbe39272011-02-01 15:15:22 +00002739 PARSE_LANGOPT(MSBitfields);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002740 PARSE_LANGOPT(NeXTRuntime);
2741 PARSE_LANGOPT(Freestanding);
2742 PARSE_LANGOPT(NoBuiltin);
2743 PARSE_LANGOPT(ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +00002744 PARSE_LANGOPT(POSIXThreads);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002745 PARSE_LANGOPT(Blocks);
2746 PARSE_LANGOPT(EmitAllDecls);
2747 PARSE_LANGOPT(MathErrno);
Chris Lattner51924e512010-06-26 21:25:03 +00002748 LangOpts.setSignedOverflowBehavior((LangOptions::SignedOverflowBehaviorTy)
2749 Record[Idx++]);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002750 PARSE_LANGOPT(HeinousExtensions);
2751 PARSE_LANGOPT(Optimize);
2752 PARSE_LANGOPT(OptimizeSize);
2753 PARSE_LANGOPT(Static);
2754 PARSE_LANGOPT(PICLevel);
2755 PARSE_LANGOPT(GNUInline);
2756 PARSE_LANGOPT(NoInline);
2757 PARSE_LANGOPT(AccessControl);
2758 PARSE_LANGOPT(CharIsSigned);
John Thompsoned4e2952009-11-05 20:14:16 +00002759 PARSE_LANGOPT(ShortWChar);
Argyrios Kyrtzidisa88942a2011-01-15 02:56:16 +00002760 PARSE_LANGOPT(ShortEnums);
Chris Lattner51924e512010-06-26 21:25:03 +00002761 LangOpts.setGCMode((LangOptions::GCMode)Record[Idx++]);
John McCall457a04e2010-10-22 21:05:15 +00002762 LangOpts.setVisibilityMode((Visibility)Record[Idx++]);
Daniel Dunbar143021e2009-09-21 04:16:19 +00002763 LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode)
Chris Lattner51924e512010-06-26 21:25:03 +00002764 Record[Idx++]);
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002765 PARSE_LANGOPT(InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +00002766 PARSE_LANGOPT(OpenCL);
Peter Collingbourne546d0792010-12-01 19:14:57 +00002767 PARSE_LANGOPT(CUDA);
Mike Stumpd9546382009-12-12 01:27:46 +00002768 PARSE_LANGOPT(CatchUndefined);
2769 // FIXME: Missing ElideConstructors?!
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002770 #undef PARSE_LANGOPT
Douglas Gregor55abb232009-04-10 20:39:37 +00002771
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002772 return Listener->ReadLanguageOptions(LangOpts);
Douglas Gregor55abb232009-04-10 20:39:37 +00002773 }
Douglas Gregor55abb232009-04-10 20:39:37 +00002774
2775 return false;
2776}
2777
Sebastian Redl2c499f62010-08-18 23:56:43 +00002778void ASTReader::ReadPreprocessedEntities() {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002779 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2780 PerFileData &F = *Chain[I];
2781 if (!F.PreprocessorDetailCursor.getBitStreamReader())
2782 continue;
2783
2784 SavedStreamPosition SavedPosition(F.PreprocessorDetailCursor);
2785 F.PreprocessorDetailCursor.JumpToBit(F.PreprocessorDetailStartOffset);
2786 while (LoadPreprocessedEntity(F)) { }
2787 }
Douglas Gregoraae92242010-03-19 21:51:54 +00002788}
2789
Douglas Gregorf88e35b2010-11-30 06:16:57 +00002790PreprocessedEntity *ASTReader::ReadPreprocessedEntity(uint64_t Offset) {
2791 PerFileData *F = 0;
2792 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2793 if (Offset < Chain[I]->SizeInBits) {
2794 F = Chain[I];
2795 break;
2796 }
2797
2798 Offset -= Chain[I]->SizeInBits;
2799 }
2800
2801 if (!F) {
2802 Error("Malformed preprocessed entity offset");
2803 return 0;
2804 }
2805
Douglas Gregor92a96f52011-02-08 21:58:10 +00002806 // Keep track of where we are in the stream, then jump back there
2807 // after reading this entity.
2808 SavedStreamPosition SavedPosition(F->PreprocessorDetailCursor);
2809 F->PreprocessorDetailCursor.JumpToBit(Offset);
2810 return LoadPreprocessedEntity(*F);
Douglas Gregorf88e35b2010-11-30 06:16:57 +00002811}
2812
Douglas Gregor09b69892011-02-10 17:09:37 +00002813HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
2814 HeaderFileInfoTrait Trait(FE->getName());
2815 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2816 PerFileData &F = *Chain[I];
2817 HeaderFileInfoLookupTable *Table
2818 = static_cast<HeaderFileInfoLookupTable *>(F.HeaderFileInfoTable);
2819 if (!Table)
2820 continue;
2821
2822 // Look in the on-disk hash table for an entry for this file name.
2823 HeaderFileInfoLookupTable::iterator Pos = Table->find(FE->getName(),
2824 &Trait);
2825 if (Pos == Table->end())
2826 continue;
2827
2828 HeaderFileInfo HFI = *Pos;
2829 if (Listener)
2830 Listener->ReadHeaderFileInfo(HFI, FE->getUID());
2831
2832 return HFI;
2833 }
2834
2835 return HeaderFileInfo();
2836}
2837
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002838void ASTReader::ReadPragmaDiagnosticMappings(Diagnostic &Diag) {
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002839 unsigned Idx = 0;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002840 while (Idx < PragmaDiagMappings.size()) {
2841 SourceLocation
2842 Loc = SourceLocation::getFromRawEncoding(PragmaDiagMappings[Idx++]);
2843 while (1) {
2844 assert(Idx < PragmaDiagMappings.size() &&
2845 "Invalid data, didn't find '-1' marking end of diag/map pairs");
2846 if (Idx >= PragmaDiagMappings.size())
2847 break; // Something is messed up but at least avoid infinite loop in
2848 // release build.
2849 unsigned DiagID = PragmaDiagMappings[Idx++];
2850 if (DiagID == (unsigned)-1)
2851 break; // no more diag/map pairs for this location.
2852 diag::Mapping Map = (diag::Mapping)PragmaDiagMappings[Idx++];
2853 Diag.setDiagnosticMapping(DiagID, Map, Loc);
2854 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002855 }
2856}
2857
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002858/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002859ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002860 PerFileData *F = 0;
2861 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2862 F = Chain[N - I - 1];
2863 if (Index < F->LocalNumTypes)
2864 break;
2865 Index -= F->LocalNumTypes;
2866 }
2867 assert(F && F->LocalNumTypes > Index && "Broken chain");
Sebastian Redl2c373b92010-10-05 15:59:54 +00002868 return RecordLocation(F, F->TypeOffsets[Index]);
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002869}
2870
2871/// \brief Read and return the type with the given index..
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002872///
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002873/// The index is the type ID, shifted and minus the number of predefs. This
2874/// routine actually reads the record corresponding to the type at the given
2875/// location. It is a helper routine for GetType, which deals with reading type
2876/// IDs.
Sebastian Redl2c499f62010-08-18 23:56:43 +00002877QualType ASTReader::ReadTypeRecord(unsigned Index) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00002878 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redl2c373b92010-10-05 15:59:54 +00002879 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00002880
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002881 // Keep track of where we are in the stream, then jump back there
2882 // after reading this type.
Douglas Gregor12bfa382009-10-17 00:13:19 +00002883 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002884
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00002885 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redleaa4ade2010-08-11 18:52:41 +00002886
Douglas Gregor1342e842009-07-06 18:54:52 +00002887 // Note that we are loading a type record.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00002888 Deserializing AType(this);
Mike Stump11289f42009-09-09 15:08:12 +00002889
Sebastian Redl2c373b92010-10-05 15:59:54 +00002890 DeclsCursor.JumpToBit(Loc.Offset);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002891 RecordData Record;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002892 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl539c5062010-08-18 23:57:32 +00002893 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
2894 case TYPE_EXT_QUAL: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002895 if (Record.size() != 2) {
2896 Error("Incorrect encoding of extended qualifier type");
2897 return QualType();
2898 }
Douglas Gregor455b8f42009-04-15 22:00:08 +00002899 QualType Base = GetType(Record[0]);
John McCall8ccfcb52009-09-24 19:53:00 +00002900 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[1]);
2901 return Context->getQualifiedType(Base, Quals);
Douglas Gregor455b8f42009-04-15 22:00:08 +00002902 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002903
Sebastian Redl539c5062010-08-18 23:57:32 +00002904 case TYPE_COMPLEX: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002905 if (Record.size() != 1) {
2906 Error("Incorrect encoding of complex type");
2907 return QualType();
2908 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002909 QualType ElemType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002910 return Context->getComplexType(ElemType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002911 }
2912
Sebastian Redl539c5062010-08-18 23:57:32 +00002913 case TYPE_POINTER: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002914 if (Record.size() != 1) {
2915 Error("Incorrect encoding of pointer type");
2916 return QualType();
2917 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002918 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002919 return Context->getPointerType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002920 }
2921
Sebastian Redl539c5062010-08-18 23:57:32 +00002922 case TYPE_BLOCK_POINTER: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002923 if (Record.size() != 1) {
2924 Error("Incorrect encoding of block pointer type");
2925 return QualType();
2926 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002927 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002928 return Context->getBlockPointerType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002929 }
2930
Sebastian Redl539c5062010-08-18 23:57:32 +00002931 case TYPE_LVALUE_REFERENCE: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002932 if (Record.size() != 1) {
2933 Error("Incorrect encoding of lvalue reference type");
2934 return QualType();
2935 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002936 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002937 return Context->getLValueReferenceType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002938 }
2939
Sebastian Redl539c5062010-08-18 23:57:32 +00002940 case TYPE_RVALUE_REFERENCE: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002941 if (Record.size() != 1) {
2942 Error("Incorrect encoding of rvalue reference type");
2943 return QualType();
2944 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002945 QualType PointeeType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00002946 return Context->getRValueReferenceType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002947 }
2948
Sebastian Redl539c5062010-08-18 23:57:32 +00002949 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidisee776bc2010-07-02 11:55:15 +00002950 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00002951 Error("Incorrect encoding of member pointer type");
2952 return QualType();
2953 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002954 QualType PointeeType = GetType(Record[0]);
2955 QualType ClassType = GetType(Record[1]);
Douglas Gregor0cdc8322010-12-10 17:03:06 +00002956 if (PointeeType.isNull() || ClassType.isNull())
2957 return QualType();
2958
Chris Lattner8575daa2009-04-27 21:45:14 +00002959 return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002960 }
2961
Sebastian Redl539c5062010-08-18 23:57:32 +00002962 case TYPE_CONSTANT_ARRAY: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002963 QualType ElementType = GetType(Record[0]);
2964 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2965 unsigned IndexTypeQuals = Record[2];
2966 unsigned Idx = 3;
2967 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor04318252009-07-06 15:59:29 +00002968 return Context->getConstantArrayType(ElementType, Size,
2969 ASM, IndexTypeQuals);
2970 }
2971
Sebastian Redl539c5062010-08-18 23:57:32 +00002972 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002973 QualType ElementType = GetType(Record[0]);
2974 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2975 unsigned IndexTypeQuals = Record[2];
Chris Lattner8575daa2009-04-27 21:45:14 +00002976 return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002977 }
2978
Sebastian Redl539c5062010-08-18 23:57:32 +00002979 case TYPE_VARIABLE_ARRAY: {
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002980 QualType ElementType = GetType(Record[0]);
2981 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2982 unsigned IndexTypeQuals = Record[2];
Sebastian Redl2c373b92010-10-05 15:59:54 +00002983 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
2984 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
2985 return Context->getVariableArrayType(ElementType, ReadExpr(*Loc.F),
Douglas Gregor04318252009-07-06 15:59:29 +00002986 ASM, IndexTypeQuals,
2987 SourceRange(LBLoc, RBLoc));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002988 }
2989
Sebastian Redl539c5062010-08-18 23:57:32 +00002990 case TYPE_VECTOR: {
Chris Lattner37141f42010-06-23 06:00:24 +00002991 if (Record.size() != 3) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002992 Error("incorrect encoding of vector type in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00002993 return QualType();
2994 }
2995
2996 QualType ElementType = GetType(Record[0]);
2997 unsigned NumElements = Record[1];
Bob Wilsonaeb56442010-11-10 21:56:12 +00002998 unsigned VecKind = Record[2];
Chris Lattner37141f42010-06-23 06:00:24 +00002999 return Context->getVectorType(ElementType, NumElements,
Bob Wilsonaeb56442010-11-10 21:56:12 +00003000 (VectorType::VectorKind)VecKind);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003001 }
3002
Sebastian Redl539c5062010-08-18 23:57:32 +00003003 case TYPE_EXT_VECTOR: {
Chris Lattner37141f42010-06-23 06:00:24 +00003004 if (Record.size() != 3) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003005 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003006 return QualType();
3007 }
3008
3009 QualType ElementType = GetType(Record[0]);
3010 unsigned NumElements = Record[1];
Chris Lattner8575daa2009-04-27 21:45:14 +00003011 return Context->getExtVectorType(ElementType, NumElements);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003012 }
3013
Sebastian Redl539c5062010-08-18 23:57:32 +00003014 case TYPE_FUNCTION_NO_PROTO: {
Rafael Espindola49b85ab2010-03-30 22:15:11 +00003015 if (Record.size() != 4) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00003016 Error("incorrect encoding of no-proto function type");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003017 return QualType();
3018 }
3019 QualType ResultType = GetType(Record[0]);
Rafael Espindola49b85ab2010-03-30 22:15:11 +00003020 FunctionType::ExtInfo Info(Record[1], Record[2], (CallingConv)Record[3]);
Rafael Espindolac50c27c2010-03-30 20:24:48 +00003021 return Context->getFunctionNoProtoType(ResultType, Info);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003022 }
3023
Sebastian Redl539c5062010-08-18 23:57:32 +00003024 case TYPE_FUNCTION_PROTO: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003025 QualType ResultType = GetType(Record[0]);
John McCalldb40c7f2010-12-14 08:05:40 +00003026
3027 FunctionProtoType::ExtProtoInfo EPI;
3028 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
3029 /*regparm*/ Record[2],
3030 static_cast<CallingConv>(Record[3]));
3031
Rafael Espindola49b85ab2010-03-30 22:15:11 +00003032 unsigned Idx = 4;
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003033 unsigned NumParams = Record[Idx++];
3034 llvm::SmallVector<QualType, 16> ParamTypes;
3035 for (unsigned I = 0; I != NumParams; ++I)
3036 ParamTypes.push_back(GetType(Record[Idx++]));
John McCalldb40c7f2010-12-14 08:05:40 +00003037
3038 EPI.Variadic = Record[Idx++];
3039 EPI.TypeQuals = Record[Idx++];
Douglas Gregordb9d6642011-01-26 05:01:58 +00003040 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
John McCalldb40c7f2010-12-14 08:05:40 +00003041 EPI.HasExceptionSpec = Record[Idx++];
3042 EPI.HasAnyExceptionSpec = Record[Idx++];
3043 EPI.NumExceptions = Record[Idx++];
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00003044 llvm::SmallVector<QualType, 2> Exceptions;
John McCalldb40c7f2010-12-14 08:05:40 +00003045 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00003046 Exceptions.push_back(GetType(Record[Idx++]));
John McCalldb40c7f2010-12-14 08:05:40 +00003047 EPI.Exceptions = Exceptions.data();
Jay Foad7d0479f2009-05-21 09:52:38 +00003048 return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams,
John McCalldb40c7f2010-12-14 08:05:40 +00003049 EPI);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003050 }
3051
Sebastian Redl539c5062010-08-18 23:57:32 +00003052 case TYPE_UNRESOLVED_USING:
John McCallb96ec562009-12-04 22:46:56 +00003053 return Context->getTypeDeclType(
3054 cast<UnresolvedUsingTypenameDecl>(GetDecl(Record[0])));
3055
Sebastian Redl539c5062010-08-18 23:57:32 +00003056 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00003057 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003058 Error("incorrect encoding of typedef type");
3059 return QualType();
3060 }
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00003061 TypedefDecl *Decl = cast<TypedefDecl>(GetDecl(Record[0]));
3062 QualType Canonical = GetType(Record[1]);
Douglas Gregorf86c9392010-10-26 00:51:02 +00003063 if (!Canonical.isNull())
3064 Canonical = Context->getCanonicalType(Canonical);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00003065 return Context->getTypedefType(Decl, Canonical);
3066 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003067
Sebastian Redl539c5062010-08-18 23:57:32 +00003068 case TYPE_TYPEOF_EXPR:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003069 return Context->getTypeOfExprType(ReadExpr(*Loc.F));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003070
Sebastian Redl539c5062010-08-18 23:57:32 +00003071 case TYPE_TYPEOF: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003072 if (Record.size() != 1) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003073 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003074 return QualType();
3075 }
3076 QualType UnderlyingType = GetType(Record[0]);
Chris Lattner8575daa2009-04-27 21:45:14 +00003077 return Context->getTypeOfType(UnderlyingType);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003078 }
Mike Stump11289f42009-09-09 15:08:12 +00003079
Sebastian Redl539c5062010-08-18 23:57:32 +00003080 case TYPE_DECLTYPE:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003081 return Context->getDecltypeType(ReadExpr(*Loc.F));
Anders Carlsson81df7b82009-06-24 19:06:50 +00003082
Sebastian Redl539c5062010-08-18 23:57:32 +00003083 case TYPE_RECORD: {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003084 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003085 Error("incorrect encoding of record type");
3086 return QualType();
3087 }
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003088 bool IsDependent = Record[0];
3089 QualType T = Context->getRecordType(cast<RecordDecl>(GetDecl(Record[1])));
John McCall424cec92011-01-19 06:33:43 +00003090 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003091 return T;
3092 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003093
Sebastian Redl539c5062010-08-18 23:57:32 +00003094 case TYPE_ENUM: {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003095 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003096 Error("incorrect encoding of enum type");
3097 return QualType();
3098 }
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003099 bool IsDependent = Record[0];
3100 QualType T = Context->getEnumType(cast<EnumDecl>(GetDecl(Record[1])));
John McCall424cec92011-01-19 06:33:43 +00003101 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003102 return T;
3103 }
Douglas Gregor1daeb692009-04-13 18:14:40 +00003104
John McCall81904512011-01-06 01:58:22 +00003105 case TYPE_ATTRIBUTED: {
3106 if (Record.size() != 3) {
3107 Error("incorrect encoding of attributed type");
3108 return QualType();
3109 }
3110 QualType modifiedType = GetType(Record[0]);
3111 QualType equivalentType = GetType(Record[1]);
3112 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
3113 return Context->getAttributedType(kind, modifiedType, equivalentType);
3114 }
3115
Abramo Bagnara924a8f32010-12-10 16:29:40 +00003116 case TYPE_PAREN: {
3117 if (Record.size() != 1) {
3118 Error("incorrect encoding of paren type");
3119 return QualType();
3120 }
3121 QualType InnerType = GetType(Record[0]);
3122 return Context->getParenType(InnerType);
3123 }
3124
Douglas Gregord2fa7662010-12-20 02:24:11 +00003125 case TYPE_PACK_EXPANSION: {
Douglas Gregor17328502011-02-01 15:24:58 +00003126 if (Record.size() != 2) {
Douglas Gregord2fa7662010-12-20 02:24:11 +00003127 Error("incorrect encoding of pack expansion type");
3128 return QualType();
3129 }
3130 QualType Pattern = GetType(Record[0]);
3131 if (Pattern.isNull())
3132 return QualType();
Douglas Gregor0dca5fd2011-01-14 17:04:44 +00003133 llvm::Optional<unsigned> NumExpansions;
3134 if (Record[1])
3135 NumExpansions = Record[1] - 1;
3136 return Context->getPackExpansionType(Pattern, NumExpansions);
Douglas Gregord2fa7662010-12-20 02:24:11 +00003137 }
3138
Sebastian Redl539c5062010-08-18 23:57:32 +00003139 case TYPE_ELABORATED: {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00003140 unsigned Idx = 0;
3141 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3142 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3143 QualType NamedType = GetType(Record[Idx++]);
3144 return Context->getElaboratedType(Keyword, NNS, NamedType);
John McCallfcc33b02009-09-05 00:15:47 +00003145 }
3146
Sebastian Redl539c5062010-08-18 23:57:32 +00003147 case TYPE_OBJC_INTERFACE: {
Chris Lattner587cbe12009-04-22 06:45:28 +00003148 unsigned Idx = 0;
3149 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
John McCall8b07ec22010-05-15 11:32:37 +00003150 return Context->getObjCInterfaceType(ItfD);
3151 }
3152
Sebastian Redl539c5062010-08-18 23:57:32 +00003153 case TYPE_OBJC_OBJECT: {
John McCall8b07ec22010-05-15 11:32:37 +00003154 unsigned Idx = 0;
3155 QualType Base = GetType(Record[Idx++]);
Chris Lattner587cbe12009-04-22 06:45:28 +00003156 unsigned NumProtos = Record[Idx++];
3157 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
3158 for (unsigned I = 0; I != NumProtos; ++I)
3159 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003160 return Context->getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattner587cbe12009-04-22 06:45:28 +00003161 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00003162
Sebastian Redl539c5062010-08-18 23:57:32 +00003163 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattner6e054af2009-04-22 06:40:03 +00003164 unsigned Idx = 0;
John McCall8b07ec22010-05-15 11:32:37 +00003165 QualType Pointee = GetType(Record[Idx++]);
3166 return Context->getObjCObjectPointerType(Pointee);
Chris Lattner6e054af2009-04-22 06:40:03 +00003167 }
Argyrios Kyrtzidisa7a36df2009-09-29 19:42:55 +00003168
Sebastian Redl539c5062010-08-18 23:57:32 +00003169 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCallcebee162009-10-18 09:09:24 +00003170 unsigned Idx = 0;
3171 QualType Parm = GetType(Record[Idx++]);
3172 QualType Replacement = GetType(Record[Idx++]);
3173 return
3174 Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
3175 Replacement);
3176 }
John McCalle78aac42010-03-10 03:28:59 +00003177
Douglas Gregorada4b792011-01-14 02:55:32 +00003178 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
3179 unsigned Idx = 0;
3180 QualType Parm = GetType(Record[Idx++]);
3181 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
3182 return Context->getSubstTemplateTypeParmPackType(
3183 cast<TemplateTypeParmType>(Parm),
3184 ArgPack);
3185 }
3186
Sebastian Redl539c5062010-08-18 23:57:32 +00003187 case TYPE_INJECTED_CLASS_NAME: {
John McCalle78aac42010-03-10 03:28:59 +00003188 CXXRecordDecl *D = cast<CXXRecordDecl>(GetDecl(Record[0]));
3189 QualType TST = GetType(Record[1]); // probably derivable
Argyrios Kyrtzidisdab33c52010-07-02 11:55:20 +00003190 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003191 // for AST reading, too much interdependencies.
Argyrios Kyrtzidisdab33c52010-07-02 11:55:20 +00003192 return
3193 QualType(new (*Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCalle78aac42010-03-10 03:28:59 +00003194 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003195
Sebastian Redl539c5062010-08-18 23:57:32 +00003196 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003197 unsigned Idx = 0;
3198 unsigned Depth = Record[Idx++];
3199 unsigned Index = Record[Idx++];
3200 bool Pack = Record[Idx++];
3201 IdentifierInfo *Name = GetIdentifierInfo(Record, Idx);
3202 return Context->getTemplateTypeParmType(Depth, Index, Pack, Name);
3203 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003204
Sebastian Redl539c5062010-08-18 23:57:32 +00003205 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00003206 unsigned Idx = 0;
3207 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3208 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3209 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +00003210 QualType Canon = GetType(Record[Idx++]);
Douglas Gregorf86c9392010-10-26 00:51:02 +00003211 if (!Canon.isNull())
3212 Canon = Context->getCanonicalType(Canon);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +00003213 return Context->getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00003214 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003215
Sebastian Redl539c5062010-08-18 23:57:32 +00003216 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00003217 unsigned Idx = 0;
3218 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3219 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3220 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
3221 unsigned NumArgs = Record[Idx++];
3222 llvm::SmallVector<TemplateArgument, 8> Args;
3223 Args.reserve(NumArgs);
3224 while (NumArgs--)
Sebastian Redl2c373b92010-10-05 15:59:54 +00003225 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00003226 return Context->getDependentTemplateSpecializationType(Keyword, NNS, Name,
3227 Args.size(), Args.data());
3228 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003229
Sebastian Redl539c5062010-08-18 23:57:32 +00003230 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00003231 unsigned Idx = 0;
3232
3233 // ArrayType
3234 QualType ElementType = GetType(Record[Idx++]);
3235 ArrayType::ArraySizeModifier ASM
3236 = (ArrayType::ArraySizeModifier)Record[Idx++];
3237 unsigned IndexTypeQuals = Record[Idx++];
3238
3239 // DependentSizedArrayType
Sebastian Redl2c373b92010-10-05 15:59:54 +00003240 Expr *NumElts = ReadExpr(*Loc.F);
3241 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00003242
3243 return Context->getDependentSizedArrayType(ElementType, NumElts, ASM,
3244 IndexTypeQuals, Brackets);
3245 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003246
Sebastian Redl539c5062010-08-18 23:57:32 +00003247 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003248 unsigned Idx = 0;
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003249 bool IsDependent = Record[Idx++];
Douglas Gregor5590be02011-01-15 06:45:20 +00003250 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003251 llvm::SmallVector<TemplateArgument, 8> Args;
Sebastian Redl2c373b92010-10-05 15:59:54 +00003252 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003253 QualType Canon = GetType(Record[Idx++]);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003254 QualType T;
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00003255 if (Canon.isNull())
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003256 T = Context->getCanonicalTemplateSpecializationType(Name, Args.data(),
3257 Args.size());
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00003258 else
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003259 T = Context->getTemplateSpecializationType(Name, Args.data(),
3260 Args.size(), Canon);
John McCall424cec92011-01-19 06:33:43 +00003261 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00003262 return T;
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00003263 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003264 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003265 // Suppress a GCC warning
3266 return QualType();
3267}
3268
Sebastian Redl2c373b92010-10-05 15:59:54 +00003269class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redl2c499f62010-08-18 23:56:43 +00003270 ASTReader &Reader;
Sebastian Redl2c373b92010-10-05 15:59:54 +00003271 ASTReader::PerFileData &F;
Sebastian Redlc67764e2010-07-22 22:43:28 +00003272 llvm::BitstreamCursor &DeclsCursor;
Sebastian Redl2c499f62010-08-18 23:56:43 +00003273 const ASTReader::RecordData &Record;
John McCall8f115c62009-10-16 21:56:05 +00003274 unsigned &Idx;
3275
Sebastian Redl2c373b92010-10-05 15:59:54 +00003276 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
3277 unsigned &I) {
3278 return Reader.ReadSourceLocation(F, R, I);
3279 }
3280
John McCall8f115c62009-10-16 21:56:05 +00003281public:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003282 TypeLocReader(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redl2c499f62010-08-18 23:56:43 +00003283 const ASTReader::RecordData &Record, unsigned &Idx)
Sebastian Redl2c373b92010-10-05 15:59:54 +00003284 : Reader(Reader), F(F), DeclsCursor(F.DeclsCursor), Record(Record), Idx(Idx)
3285 { }
John McCall8f115c62009-10-16 21:56:05 +00003286
John McCall17001972009-10-18 01:05:36 +00003287 // We want compile-time assurance that we've enumerated all of
3288 // these, so unfortunately we have to declare them first, then
3289 // define them out-of-line.
3290#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +00003291#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +00003292 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +00003293#include "clang/AST/TypeLocNodes.def"
3294
John McCall17001972009-10-18 01:05:36 +00003295 void VisitFunctionTypeLoc(FunctionTypeLoc);
3296 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCall8f115c62009-10-16 21:56:05 +00003297};
3298
John McCall17001972009-10-18 01:05:36 +00003299void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCall8f115c62009-10-16 21:56:05 +00003300 // nothing to do
3301}
John McCall17001972009-10-18 01:05:36 +00003302void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003303 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
Douglas Gregorc9b7a592010-01-18 18:04:31 +00003304 if (TL.needsExtraLocalData()) {
3305 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
3306 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
3307 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
3308 TL.setModeAttr(Record[Idx++]);
3309 }
John McCall8f115c62009-10-16 21:56:05 +00003310}
John McCall17001972009-10-18 01:05:36 +00003311void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003312 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003313}
John McCall17001972009-10-18 01:05:36 +00003314void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003315 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003316}
John McCall17001972009-10-18 01:05:36 +00003317void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003318 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003319}
John McCall17001972009-10-18 01:05:36 +00003320void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003321 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003322}
John McCall17001972009-10-18 01:05:36 +00003323void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003324 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003325}
John McCall17001972009-10-18 01:05:36 +00003326void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003327 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003328}
John McCall17001972009-10-18 01:05:36 +00003329void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003330 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
3331 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003332 if (Record[Idx++])
Sebastian Redl2c373b92010-10-05 15:59:54 +00003333 TL.setSizeExpr(Reader.ReadExpr(F));
Douglas Gregor12bfa382009-10-17 00:13:19 +00003334 else
John McCall17001972009-10-18 01:05:36 +00003335 TL.setSizeExpr(0);
3336}
3337void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
3338 VisitArrayTypeLoc(TL);
3339}
3340void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
3341 VisitArrayTypeLoc(TL);
3342}
3343void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
3344 VisitArrayTypeLoc(TL);
3345}
3346void TypeLocReader::VisitDependentSizedArrayTypeLoc(
3347 DependentSizedArrayTypeLoc TL) {
3348 VisitArrayTypeLoc(TL);
3349}
3350void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
3351 DependentSizedExtVectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003352 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003353}
3354void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003355 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003356}
3357void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003358 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003359}
3360void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003361 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3362 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor7fb25412010-10-01 18:44:50 +00003363 TL.setTrailingReturn(Record[Idx++]);
John McCall17001972009-10-18 01:05:36 +00003364 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
John McCalle6347002009-10-23 01:28:53 +00003365 TL.setArg(i, cast_or_null<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
John McCall17001972009-10-18 01:05:36 +00003366 }
3367}
3368void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
3369 VisitFunctionTypeLoc(TL);
3370}
3371void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
3372 VisitFunctionTypeLoc(TL);
3373}
John McCallb96ec562009-12-04 22:46:56 +00003374void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003375 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallb96ec562009-12-04 22:46:56 +00003376}
John McCall17001972009-10-18 01:05:36 +00003377void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003378 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003379}
3380void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003381 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3382 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3383 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003384}
3385void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003386 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3387 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3388 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3389 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003390}
3391void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003392 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003393}
3394void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003395 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003396}
3397void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003398 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003399}
John McCall81904512011-01-06 01:58:22 +00003400void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
3401 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
3402 if (TL.hasAttrOperand()) {
3403 SourceRange range;
3404 range.setBegin(ReadSourceLocation(Record, Idx));
3405 range.setEnd(ReadSourceLocation(Record, Idx));
3406 TL.setAttrOperandParensRange(range);
3407 }
3408 if (TL.hasAttrExprOperand()) {
3409 if (Record[Idx++])
3410 TL.setAttrExprOperand(Reader.ReadExpr(F));
3411 else
3412 TL.setAttrExprOperand(0);
3413 } else if (TL.hasAttrEnumOperand())
3414 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
3415}
John McCall17001972009-10-18 01:05:36 +00003416void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003417 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003418}
John McCallcebee162009-10-18 09:09:24 +00003419void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
3420 SubstTemplateTypeParmTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003421 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallcebee162009-10-18 09:09:24 +00003422}
Douglas Gregorada4b792011-01-14 02:55:32 +00003423void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
3424 SubstTemplateTypeParmPackTypeLoc TL) {
3425 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3426}
John McCall17001972009-10-18 01:05:36 +00003427void TypeLocReader::VisitTemplateSpecializationTypeLoc(
3428 TemplateSpecializationTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003429 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
3430 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3431 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall0ad16662009-10-29 08:12:44 +00003432 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
3433 TL.setArgLocInfo(i,
Sebastian Redl2c373b92010-10-05 15:59:54 +00003434 Reader.GetTemplateArgumentLocInfo(F,
3435 TL.getTypePtr()->getArg(i).getKind(),
3436 Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003437}
Abramo Bagnara924a8f32010-12-10 16:29:40 +00003438void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
3439 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3440 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3441}
Abramo Bagnara6150c882010-05-11 21:36:43 +00003442void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003443 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3444 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003445}
John McCalle78aac42010-03-10 03:28:59 +00003446void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003447 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalle78aac42010-03-10 03:28:59 +00003448}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00003449void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003450 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3451 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
3452 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003453}
John McCallc392f372010-06-11 00:33:02 +00003454void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
3455 DependentTemplateSpecializationTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003456 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3457 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
3458 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3459 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3460 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCallc392f372010-06-11 00:33:02 +00003461 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
3462 TL.setArgLocInfo(I,
Sebastian Redl2c373b92010-10-05 15:59:54 +00003463 Reader.GetTemplateArgumentLocInfo(F,
3464 TL.getTypePtr()->getArg(I).getKind(),
3465 Record, Idx));
John McCallc392f372010-06-11 00:33:02 +00003466}
Douglas Gregord2fa7662010-12-20 02:24:11 +00003467void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
3468 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
3469}
John McCall17001972009-10-18 01:05:36 +00003470void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003471 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall8b07ec22010-05-15 11:32:37 +00003472}
3473void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
3474 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003475 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3476 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00003477 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Sebastian Redl2c373b92010-10-05 15:59:54 +00003478 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00003479}
John McCallfc93cf92009-10-22 22:37:11 +00003480void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003481 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCallfc93cf92009-10-22 22:37:11 +00003482}
John McCall8f115c62009-10-16 21:56:05 +00003483
Sebastian Redl2c373b92010-10-05 15:59:54 +00003484TypeSourceInfo *ASTReader::GetTypeSourceInfo(PerFileData &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00003485 const RecordData &Record,
John McCall8f115c62009-10-16 21:56:05 +00003486 unsigned &Idx) {
3487 QualType InfoTy = GetType(Record[Idx++]);
3488 if (InfoTy.isNull())
3489 return 0;
3490
John McCallbcd03502009-12-07 02:54:59 +00003491 TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003492 TypeLocReader TLR(*this, F, Record, Idx);
John McCallbcd03502009-12-07 02:54:59 +00003493 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCall8f115c62009-10-16 21:56:05 +00003494 TLR.Visit(TL);
John McCallbcd03502009-12-07 02:54:59 +00003495 return TInfo;
John McCall8f115c62009-10-16 21:56:05 +00003496}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003497
Sebastian Redl539c5062010-08-18 23:57:32 +00003498QualType ASTReader::GetType(TypeID ID) {
John McCall8ccfcb52009-09-24 19:53:00 +00003499 unsigned FastQuals = ID & Qualifiers::FastMask;
3500 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003501
Sebastian Redl539c5062010-08-18 23:57:32 +00003502 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003503 QualType T;
Sebastian Redl539c5062010-08-18 23:57:32 +00003504 switch ((PredefinedTypeIDs)Index) {
3505 case PREDEF_TYPE_NULL_ID: return QualType();
3506 case PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break;
3507 case PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003508
Sebastian Redl539c5062010-08-18 23:57:32 +00003509 case PREDEF_TYPE_CHAR_U_ID:
3510 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003511 // FIXME: Check that the signedness of CharTy is correct!
Chris Lattner8575daa2009-04-27 21:45:14 +00003512 T = Context->CharTy;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003513 break;
3514
Sebastian Redl539c5062010-08-18 23:57:32 +00003515 case PREDEF_TYPE_UCHAR_ID: T = Context->UnsignedCharTy; break;
3516 case PREDEF_TYPE_USHORT_ID: T = Context->UnsignedShortTy; break;
3517 case PREDEF_TYPE_UINT_ID: T = Context->UnsignedIntTy; break;
3518 case PREDEF_TYPE_ULONG_ID: T = Context->UnsignedLongTy; break;
3519 case PREDEF_TYPE_ULONGLONG_ID: T = Context->UnsignedLongLongTy; break;
3520 case PREDEF_TYPE_UINT128_ID: T = Context->UnsignedInt128Ty; break;
3521 case PREDEF_TYPE_SCHAR_ID: T = Context->SignedCharTy; break;
3522 case PREDEF_TYPE_WCHAR_ID: T = Context->WCharTy; break;
3523 case PREDEF_TYPE_SHORT_ID: T = Context->ShortTy; break;
3524 case PREDEF_TYPE_INT_ID: T = Context->IntTy; break;
3525 case PREDEF_TYPE_LONG_ID: T = Context->LongTy; break;
3526 case PREDEF_TYPE_LONGLONG_ID: T = Context->LongLongTy; break;
3527 case PREDEF_TYPE_INT128_ID: T = Context->Int128Ty; break;
3528 case PREDEF_TYPE_FLOAT_ID: T = Context->FloatTy; break;
3529 case PREDEF_TYPE_DOUBLE_ID: T = Context->DoubleTy; break;
3530 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy; break;
3531 case PREDEF_TYPE_OVERLOAD_ID: T = Context->OverloadTy; break;
3532 case PREDEF_TYPE_DEPENDENT_ID: T = Context->DependentTy; break;
3533 case PREDEF_TYPE_NULLPTR_ID: T = Context->NullPtrTy; break;
3534 case PREDEF_TYPE_CHAR16_ID: T = Context->Char16Ty; break;
3535 case PREDEF_TYPE_CHAR32_ID: T = Context->Char32Ty; break;
3536 case PREDEF_TYPE_OBJC_ID: T = Context->ObjCBuiltinIdTy; break;
3537 case PREDEF_TYPE_OBJC_CLASS: T = Context->ObjCBuiltinClassTy; break;
3538 case PREDEF_TYPE_OBJC_SEL: T = Context->ObjCBuiltinSelTy; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003539 }
3540
3541 assert(!T.isNull() && "Unknown predefined type");
John McCall8ccfcb52009-09-24 19:53:00 +00003542 return T.withFastQualifiers(FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003543 }
3544
Sebastian Redl539c5062010-08-18 23:57:32 +00003545 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003546 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl409183f2010-07-14 20:26:45 +00003547 if (TypesLoaded[Index].isNull()) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00003548 TypesLoaded[Index] = ReadTypeRecord(Index);
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003549 if (TypesLoaded[Index].isNull())
3550 return QualType();
3551
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003552 TypesLoaded[Index]->setFromAST();
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003553 TypeIdxs[TypesLoaded[Index]] = TypeIdx::fromTypeID(ID);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003554 if (DeserializationListener)
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003555 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003556 TypesLoaded[Index]);
Sebastian Redl409183f2010-07-14 20:26:45 +00003557 }
Mike Stump11289f42009-09-09 15:08:12 +00003558
John McCall8ccfcb52009-09-24 19:53:00 +00003559 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003560}
3561
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003562TypeID ASTReader::GetTypeID(QualType T) const {
3563 return MakeTypeID(T,
3564 std::bind1st(std::mem_fun(&ASTReader::GetTypeIdx), this));
3565}
3566
3567TypeIdx ASTReader::GetTypeIdx(QualType T) const {
3568 if (T.isNull())
3569 return TypeIdx();
3570 assert(!T.getLocalFastQualifiers());
3571
3572 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3573 // GetTypeIdx is mostly used for computing the hash of DeclarationNames and
3574 // comparing keys of ASTDeclContextNameLookupTable.
3575 // If the type didn't come from the AST file use a specially marked index
3576 // so that any hash/key comparison fail since no such index is stored
3577 // in a AST file.
3578 if (I == TypeIdxs.end())
3579 return TypeIdx(-1);
3580 return I->second;
3581}
3582
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003583unsigned ASTReader::getTotalNumCXXBaseSpecifiers() const {
3584 unsigned Result = 0;
3585 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
3586 Result += Chain[I]->LocalNumCXXBaseSpecifiers;
3587
3588 return Result;
3589}
3590
John McCall0ad16662009-10-29 08:12:44 +00003591TemplateArgumentLocInfo
Sebastian Redl2c373b92010-10-05 15:59:54 +00003592ASTReader::GetTemplateArgumentLocInfo(PerFileData &F,
3593 TemplateArgument::ArgKind Kind,
John McCall0ad16662009-10-29 08:12:44 +00003594 const RecordData &Record,
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003595 unsigned &Index) {
John McCall0ad16662009-10-29 08:12:44 +00003596 switch (Kind) {
3597 case TemplateArgument::Expression:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003598 return ReadExpr(F);
John McCall0ad16662009-10-29 08:12:44 +00003599 case TemplateArgument::Type:
Sebastian Redl2c373b92010-10-05 15:59:54 +00003600 return GetTypeSourceInfo(F, Record, Index);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003601 case TemplateArgument::Template: {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003602 SourceRange QualifierRange = ReadSourceRange(F, Record, Index);
3603 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003604 return TemplateArgumentLocInfo(QualifierRange, TemplateNameLoc,
3605 SourceLocation());
3606 }
3607 case TemplateArgument::TemplateExpansion: {
3608 SourceRange QualifierRange = ReadSourceRange(F, Record, Index);
3609 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregoreb29d182011-01-05 17:40:24 +00003610 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
3611 return TemplateArgumentLocInfo(QualifierRange, TemplateNameLoc,
3612 EllipsisLoc);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003613 }
John McCall0ad16662009-10-29 08:12:44 +00003614 case TemplateArgument::Null:
3615 case TemplateArgument::Integral:
3616 case TemplateArgument::Declaration:
3617 case TemplateArgument::Pack:
3618 return TemplateArgumentLocInfo();
3619 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +00003620 llvm_unreachable("unexpected template argument loc");
John McCall0ad16662009-10-29 08:12:44 +00003621 return TemplateArgumentLocInfo();
3622}
3623
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003624TemplateArgumentLoc
Sebastian Redl2c373b92010-10-05 15:59:54 +00003625ASTReader::ReadTemplateArgumentLoc(PerFileData &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00003626 const RecordData &Record, unsigned &Index) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00003627 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003628
3629 if (Arg.getKind() == TemplateArgument::Expression) {
3630 if (Record[Index++]) // bool InfoHasSameExpr.
3631 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
3632 }
Sebastian Redl2c373b92010-10-05 15:59:54 +00003633 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00003634 Record, Index));
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003635}
3636
Sebastian Redl2c499f62010-08-18 23:56:43 +00003637Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall75b960e2010-06-01 09:23:16 +00003638 return GetDecl(ID);
3639}
3640
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003641uint64_t
3642ASTReader::GetCXXBaseSpecifiersOffset(serialization::CXXBaseSpecifiersID ID) {
3643 if (ID == 0)
3644 return 0;
3645
3646 --ID;
3647 uint64_t Offset = 0;
3648 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3649 if (ID < Chain[I]->LocalNumCXXBaseSpecifiers)
3650 return Offset + Chain[I]->CXXBaseSpecifiersOffsets[ID];
3651
3652 ID -= Chain[I]->LocalNumCXXBaseSpecifiers;
3653 Offset += Chain[I]->SizeInBits;
3654 }
3655
3656 assert(false && "CXXBaseSpecifiers not found");
3657 return 0;
3658}
3659
3660CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
3661 // Figure out which AST file contains this offset.
3662 PerFileData *F = 0;
3663 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3664 if (Offset < Chain[I]->SizeInBits) {
3665 F = Chain[I];
3666 break;
3667 }
3668
3669 Offset -= Chain[I]->SizeInBits;
3670 }
3671
3672 if (!F) {
3673 Error("Malformed AST file: C++ base specifiers at impossible offset");
3674 return 0;
3675 }
3676
3677 llvm::BitstreamCursor &Cursor = F->DeclsCursor;
3678 SavedStreamPosition SavedPosition(Cursor);
3679 Cursor.JumpToBit(Offset);
3680 ReadingKindTracker ReadingKind(Read_Decl, *this);
3681 RecordData Record;
3682 unsigned Code = Cursor.ReadCode();
3683 unsigned RecCode = Cursor.ReadRecord(Code, Record);
3684 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
3685 Error("Malformed AST file: missing C++ base specifiers");
3686 return 0;
3687 }
3688
3689 unsigned Idx = 0;
3690 unsigned NumBases = Record[Idx++];
3691 void *Mem = Context->Allocate(sizeof(CXXBaseSpecifier) * NumBases);
3692 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
3693 for (unsigned I = 0; I != NumBases; ++I)
3694 Bases[I] = ReadCXXBaseSpecifier(*F, Record, Idx);
3695 return Bases;
3696}
3697
Sebastian Redl2c499f62010-08-18 23:56:43 +00003698TranslationUnitDecl *ASTReader::GetTranslationUnitDecl() {
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003699 if (!DeclsLoaded[0]) {
Sebastian Redld7dce0a2010-08-24 00:50:04 +00003700 ReadDeclRecord(0, 1);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003701 if (DeserializationListener)
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003702 DeserializationListener->DeclRead(1, DeclsLoaded[0]);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003703 }
Argyrios Kyrtzidis7e8996c2010-07-08 17:13:02 +00003704
3705 return cast<TranslationUnitDecl>(DeclsLoaded[0]);
3706}
3707
Sebastian Redl539c5062010-08-18 23:57:32 +00003708Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003709 if (ID == 0)
3710 return 0;
3711
Douglas Gregor745ed142009-04-25 18:35:21 +00003712 if (ID > DeclsLoaded.size()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003713 Error("declaration ID out-of-range for AST file");
Douglas Gregor745ed142009-04-25 18:35:21 +00003714 return 0;
3715 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003716
Douglas Gregor745ed142009-04-25 18:35:21 +00003717 unsigned Index = ID - 1;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003718 if (!DeclsLoaded[Index]) {
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00003719 ReadDeclRecord(Index, ID);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003720 if (DeserializationListener)
3721 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
3722 }
Douglas Gregor745ed142009-04-25 18:35:21 +00003723
3724 return DeclsLoaded[Index];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003725}
3726
Chris Lattner9c28af02009-04-27 05:46:25 +00003727/// \brief Resolve the offset of a statement into a statement.
3728///
3729/// This operation will read a new statement from the external
3730/// source each time it is called, and is meant to be used via a
3731/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redl2c499f62010-08-18 23:56:43 +00003732Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Argyrios Kyrtzidisd9f526f2010-10-28 09:29:32 +00003733 // Switch case IDs are per Decl.
3734 ClearSwitchCaseIDs();
3735
Sebastian Redl5c415f32010-07-22 17:01:13 +00003736 // Offset here is a global offset across the entire chain.
3737 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3738 PerFileData &F = *Chain[N - I - 1];
3739 if (Offset < F.SizeInBits) {
3740 // Since we know that this statement is part of a decl, make sure to use
3741 // the decl cursor to read it.
3742 F.DeclsCursor.JumpToBit(Offset);
Sebastian Redl2c373b92010-10-05 15:59:54 +00003743 return ReadStmtFromStream(F);
Sebastian Redl5c415f32010-07-22 17:01:13 +00003744 }
3745 Offset -= F.SizeInBits;
3746 }
3747 llvm_unreachable("Broken chain");
Douglas Gregor3c3aa612009-04-18 00:07:54 +00003748}
3749
Sebastian Redl2c499f62010-08-18 23:56:43 +00003750bool ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003751 bool (*isKindWeWant)(Decl::Kind),
John McCall75b960e2010-06-01 09:23:16 +00003752 llvm::SmallVectorImpl<Decl*> &Decls) {
Mike Stump11289f42009-09-09 15:08:12 +00003753 assert(DC->hasExternalLexicalStorage() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003754 "DeclContext has no lexical decls in storage");
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003755
Sebastian Redl5c415f32010-07-22 17:01:13 +00003756 // There might be lexical decls in multiple parts of the chain, for the TU
3757 // at least.
Sebastian Redlda6a21c2010-09-28 02:24:44 +00003758 // DeclContextOffsets might reallocate as we load additional decls below,
3759 // so make a copy of the vector.
3760 DeclContextInfos Infos = DeclContextOffsets[DC];
Sebastian Redl5c415f32010-07-22 17:01:13 +00003761 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3762 I != E; ++I) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003763 // IDs can be 0 if this context doesn't contain declarations.
3764 if (!I->LexicalDecls)
Sebastian Redl5c415f32010-07-22 17:01:13 +00003765 continue;
Sebastian Redl5c415f32010-07-22 17:01:13 +00003766
3767 // Load all of the declaration IDs
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003768 for (const KindDeclIDPair *ID = I->LexicalDecls,
3769 *IDE = ID + I->NumLexicalDecls; ID != IDE; ++ID) {
3770 if (isKindWeWant && !isKindWeWant((Decl::Kind)ID->first))
3771 continue;
3772
3773 Decl *D = GetDecl(ID->second);
Sebastian Redlda6a21c2010-09-28 02:24:44 +00003774 assert(D && "Null decl in lexical decls");
3775 Decls.push_back(D);
3776 }
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003777 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003778
Douglas Gregora57c3ab2009-04-22 22:34:57 +00003779 ++NumLexicalDeclContextsRead;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003780 return false;
3781}
3782
John McCall75b960e2010-06-01 09:23:16 +00003783DeclContext::lookup_result
Sebastian Redl2c499f62010-08-18 23:56:43 +00003784ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall75b960e2010-06-01 09:23:16 +00003785 DeclarationName Name) {
Mike Stump11289f42009-09-09 15:08:12 +00003786 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003787 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003788 if (!Name)
3789 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
3790 DeclContext::lookup_iterator(0));
Ted Kremenek1ff615c2010-03-18 00:56:54 +00003791
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003792 llvm::SmallVector<NamedDecl *, 64> Decls;
Sebastian Redl471ac2f2010-08-24 00:49:55 +00003793 // There might be visible decls in multiple parts of the chain, for the TU
Sebastian Redl9617e7e2010-08-24 00:50:16 +00003794 // and namespaces. For any given name, the last available results replace
3795 // all earlier ones. For this reason, we walk in reverse.
Sebastian Redl5c415f32010-07-22 17:01:13 +00003796 DeclContextInfos &Infos = DeclContextOffsets[DC];
Sebastian Redl9617e7e2010-08-24 00:50:16 +00003797 for (DeclContextInfos::reverse_iterator I = Infos.rbegin(), E = Infos.rend();
Sebastian Redl5c415f32010-07-22 17:01:13 +00003798 I != E; ++I) {
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003799 if (!I->NameLookupTableData)
Sebastian Redl5c415f32010-07-22 17:01:13 +00003800 continue;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003801
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003802 ASTDeclContextNameLookupTable *LookupTable =
3803 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3804 ASTDeclContextNameLookupTable::iterator Pos = LookupTable->find(Name);
3805 if (Pos == LookupTable->end())
Sebastian Redl5c415f32010-07-22 17:01:13 +00003806 continue;
3807
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003808 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
3809 for (; Data.first != Data.second; ++Data.first)
3810 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
Sebastian Redl9617e7e2010-08-24 00:50:16 +00003811 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003812 }
3813
Douglas Gregora57c3ab2009-04-22 22:34:57 +00003814 ++NumVisibleDeclContextsRead;
John McCall75b960e2010-06-01 09:23:16 +00003815
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003816 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall75b960e2010-06-01 09:23:16 +00003817 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003818}
3819
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00003820void ASTReader::MaterializeVisibleDecls(const DeclContext *DC) {
3821 assert(DC->hasExternalVisibleStorage() &&
3822 "DeclContext has no visible decls in storage");
3823
3824 llvm::SmallVector<NamedDecl *, 64> Decls;
3825 // There might be visible decls in multiple parts of the chain, for the TU
3826 // and namespaces.
3827 DeclContextInfos &Infos = DeclContextOffsets[DC];
3828 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3829 I != E; ++I) {
3830 if (!I->NameLookupTableData)
3831 continue;
3832
3833 ASTDeclContextNameLookupTable *LookupTable =
3834 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3835 for (ASTDeclContextNameLookupTable::item_iterator
3836 ItemI = LookupTable->item_begin(),
3837 ItemEnd = LookupTable->item_end() ; ItemI != ItemEnd; ++ItemI) {
3838 ASTDeclContextNameLookupTable::item_iterator::value_type Val
3839 = *ItemI;
3840 ASTDeclContextNameLookupTrait::data_type Data = Val.second;
3841 Decls.clear();
3842 for (; Data.first != Data.second; ++Data.first)
3843 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
3844 MaterializeVisibleDeclsForName(DC, Val.first, Decls);
3845 }
3846 }
3847}
3848
Sebastian Redl2c499f62010-08-18 23:56:43 +00003849void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003850 assert(Consumer);
3851 while (!InterestingDecls.empty()) {
3852 DeclGroupRef DG(InterestingDecls.front());
3853 InterestingDecls.pop_front();
Sebastian Redleaa4ade2010-08-11 18:52:41 +00003854 Consumer->HandleInterestingDecl(DG);
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003855 }
3856}
3857
Sebastian Redl2c499f62010-08-18 23:56:43 +00003858void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregorb985eeb2009-04-22 19:09:20 +00003859 this->Consumer = Consumer;
3860
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00003861 if (!Consumer)
3862 return;
3863
3864 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003865 // Force deserialization of this decl, which will cause it to be queued for
3866 // passing to the consumer.
Daniel Dunbar865c2a72009-09-17 03:06:44 +00003867 GetDecl(ExternalDefinitions[I]);
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00003868 }
Douglas Gregorf005eac2009-04-25 00:41:30 +00003869
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00003870 PassInterestingDeclsToConsumer();
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00003871}
3872
Sebastian Redl2c499f62010-08-18 23:56:43 +00003873void ASTReader::PrintStats() {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003874 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003875
Mike Stump11289f42009-09-09 15:08:12 +00003876 unsigned NumTypesLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00003877 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall8ccfcb52009-09-24 19:53:00 +00003878 QualType());
Douglas Gregor0e149972009-04-25 19:10:14 +00003879 unsigned NumDeclsLoaded
3880 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
3881 (Decl *)0);
3882 unsigned NumIdentifiersLoaded
3883 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
3884 IdentifiersLoaded.end(),
3885 (IdentifierInfo *)0);
Mike Stump11289f42009-09-09 15:08:12 +00003886 unsigned NumSelectorsLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00003887 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
3888 SelectorsLoaded.end(),
3889 Selector());
Douglas Gregorc3b1dd12009-04-13 20:50:16 +00003890
Douglas Gregorc5046832009-04-27 18:38:38 +00003891 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
3892 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor258ae542009-04-27 06:38:32 +00003893 if (TotalNumSLocEntries)
3894 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
3895 NumSLocEntriesRead, TotalNumSLocEntries,
3896 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor745ed142009-04-25 18:35:21 +00003897 if (!TypesLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00003898 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00003899 NumTypesLoaded, (unsigned)TypesLoaded.size(),
3900 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
3901 if (!DeclsLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00003902 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00003903 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
3904 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor0e149972009-04-25 19:10:14 +00003905 if (!IdentifiersLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00003906 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor0e149972009-04-25 19:10:14 +00003907 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
3908 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Sebastian Redlada023c2010-08-04 20:40:17 +00003909 if (!SelectorsLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00003910 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redlada023c2010-08-04 20:40:17 +00003911 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
3912 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003913 if (TotalNumStatements)
3914 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
3915 NumStatementsRead, TotalNumStatements,
3916 ((float)NumStatementsRead/TotalNumStatements * 100));
3917 if (TotalNumMacros)
3918 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
3919 NumMacrosRead, TotalNumMacros,
3920 ((float)NumMacrosRead/TotalNumMacros * 100));
3921 if (TotalLexicalDeclContexts)
3922 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
3923 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
3924 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
3925 * 100));
3926 if (TotalVisibleDeclContexts)
3927 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
3928 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
3929 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
3930 * 100));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003931 if (TotalNumMethodPoolEntries) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00003932 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003933 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
3934 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor95c13f52009-04-25 17:48:32 +00003935 * 100));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003936 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor95c13f52009-04-25 17:48:32 +00003937 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003938 std::fprintf(stderr, "\n");
3939}
3940
Sebastian Redl2c499f62010-08-18 23:56:43 +00003941void ASTReader::InitializeSema(Sema &S) {
Douglas Gregora868bbd2009-04-21 22:25:48 +00003942 SemaObj = &S;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003943 S.ExternalSource = this;
3944
Douglas Gregor7cd60f72009-04-22 21:15:06 +00003945 // Makes sure any declarations that were deserialized "too early"
3946 // still get added to the identifier's declaration chains.
Douglas Gregor2fb99df2010-09-24 23:29:12 +00003947 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
3948 if (SemaObj->TUScope)
John McCall48871652010-08-21 09:40:31 +00003949 SemaObj->TUScope->AddDecl(PreloadedDecls[I]);
Douglas Gregor2fb99df2010-09-24 23:29:12 +00003950
3951 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003952 }
Douglas Gregor7cd60f72009-04-22 21:15:06 +00003953 PreloadedDecls.clear();
Douglas Gregord4df8652009-04-22 22:02:47 +00003954
3955 // If there were any tentative definitions, deserialize them and add
Sebastian Redl35351a92010-01-31 22:27:38 +00003956 // them to Sema's list of tentative definitions.
Douglas Gregord4df8652009-04-22 22:02:47 +00003957 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
3958 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
Sebastian Redl35351a92010-01-31 22:27:38 +00003959 SemaObj->TentativeDefinitions.push_back(Var);
Douglas Gregord4df8652009-04-22 22:02:47 +00003960 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00003961
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00003962 // If there were any unused file scoped decls, deserialize them and add to
3963 // Sema's list of unused file scoped decls.
3964 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
3965 DeclaratorDecl *D = cast<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
3966 SemaObj->UnusedFileScopedDecls.push_back(D);
Tanya Lattner90073802010-02-12 00:07:30 +00003967 }
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003968
3969 // If there were any locally-scoped external declarations,
3970 // deserialize them and add them to Sema's table of locally-scoped
3971 // external declarations.
3972 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
3973 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
3974 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
3975 }
Douglas Gregor61cac2b2009-04-27 20:06:05 +00003976
3977 // If there were any ext_vector type declarations, deserialize them
3978 // and add them to Sema's vector of such declarations.
3979 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I)
3980 SemaObj->ExtVectorDecls.push_back(
3981 cast<TypedefDecl>(GetDecl(ExtVectorDecls[I])));
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003982
3983 // FIXME: Do VTable uses and dynamic classes deserialize too much ?
3984 // Can we cut them down before writing them ?
3985
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003986 // If there were any dynamic classes declarations, deserialize them
3987 // and add them to Sema's vector of such declarations.
3988 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I)
3989 SemaObj->DynamicClasses.push_back(
3990 cast<CXXRecordDecl>(GetDecl(DynamicClasses[I])));
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003991
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00003992 // Load the offsets of the declarations that Sema references.
3993 // They will be lazily deserialized when needed.
3994 if (!SemaDeclRefs.empty()) {
3995 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
3996 SemaObj->StdNamespace = SemaDeclRefs[0];
3997 SemaObj->StdBadAlloc = SemaDeclRefs[1];
3998 }
3999
Sebastian Redl2c373b92010-10-05 15:59:54 +00004000 for (PerFileData *F = FirstInSource; F; F = F->NextInSource) {
4001
4002 // If there are @selector references added them to its pool. This is for
4003 // implementation of -Wselector.
4004 if (!F->ReferencedSelectorsData.empty()) {
4005 unsigned int DataSize = F->ReferencedSelectorsData.size()-1;
4006 unsigned I = 0;
4007 while (I < DataSize) {
4008 Selector Sel = DecodeSelector(F->ReferencedSelectorsData[I++]);
4009 SourceLocation SelLoc = ReadSourceLocation(
4010 *F, F->ReferencedSelectorsData, I);
4011 SemaObj->ReferencedSelectors.insert(std::make_pair(Sel, SelLoc));
4012 }
4013 }
4014
4015 // If there were any pending implicit instantiations, deserialize them
4016 // and add them to Sema's queue of such instantiations.
4017 assert(F->PendingInstantiations.size() % 2 == 0 &&
4018 "Expected pairs of entries");
4019 for (unsigned Idx = 0, N = F->PendingInstantiations.size(); Idx < N;) {
4020 ValueDecl *D=cast<ValueDecl>(GetDecl(F->PendingInstantiations[Idx++]));
4021 SourceLocation Loc = ReadSourceLocation(*F, F->PendingInstantiations,Idx);
4022 SemaObj->PendingInstantiations.push_back(std::make_pair(D, Loc));
4023 }
4024 }
4025
4026 // The two special data sets below always come from the most recent PCH,
4027 // which is at the front of the chain.
4028 PerFileData &F = *Chain.front();
4029
4030 // If there were any weak undeclared identifiers, deserialize them and add to
4031 // Sema's list of weak undeclared identifiers.
4032 if (!WeakUndeclaredIdentifiers.empty()) {
4033 unsigned Idx = 0;
4034 for (unsigned I = 0, N = WeakUndeclaredIdentifiers[Idx++]; I != N; ++I) {
4035 IdentifierInfo *WeakId = GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
4036 IdentifierInfo *AliasId= GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
4037 SourceLocation Loc = ReadSourceLocation(F, WeakUndeclaredIdentifiers,Idx);
4038 bool Used = WeakUndeclaredIdentifiers[Idx++];
4039 Sema::WeakInfo WI(AliasId, Loc);
4040 WI.setUsed(Used);
4041 SemaObj->WeakUndeclaredIdentifiers.insert(std::make_pair(WeakId, WI));
4042 }
4043 }
4044
4045 // If there were any VTable uses, deserialize the information and add it
4046 // to Sema's vector and map of VTable uses.
4047 if (!VTableUses.empty()) {
4048 unsigned Idx = 0;
4049 for (unsigned I = 0, N = VTableUses[Idx++]; I != N; ++I) {
4050 CXXRecordDecl *Class = cast<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
4051 SourceLocation Loc = ReadSourceLocation(F, VTableUses, Idx);
4052 bool DefinitionRequired = VTableUses[Idx++];
4053 SemaObj->VTableUses.push_back(std::make_pair(Class, Loc));
4054 SemaObj->VTablesUsed[Class] = DefinitionRequired;
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00004055 }
4056 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00004057}
4058
Sebastian Redl2c499f62010-08-18 23:56:43 +00004059IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Sebastian Redl78f51772010-08-02 18:30:12 +00004060 // Try to find this name within our on-disk hash tables. We start with the
4061 // most recent one, since that one contains the most up-to-date info.
Sebastian Redl4e6c5672010-07-21 22:31:37 +00004062 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004063 ASTIdentifierLookupTable *IdTable
4064 = (ASTIdentifierLookupTable *)Chain[I]->IdentifierLookupTable;
Sebastian Redl5c415f32010-07-22 17:01:13 +00004065 if (!IdTable)
4066 continue;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00004067 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004068 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key);
Sebastian Redl4e6c5672010-07-21 22:31:37 +00004069 if (Pos == IdTable->end())
4070 continue;
Douglas Gregora868bbd2009-04-21 22:25:48 +00004071
Sebastian Redl4e6c5672010-07-21 22:31:37 +00004072 // Dereferencing the iterator has the effect of building the
4073 // IdentifierInfo node and populating it with the various
4074 // declarations it needs.
Sebastian Redl78f51772010-08-02 18:30:12 +00004075 return *Pos;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00004076 }
Sebastian Redl78f51772010-08-02 18:30:12 +00004077 return 0;
Douglas Gregora868bbd2009-04-21 22:25:48 +00004078}
4079
Douglas Gregor57756ea2010-10-14 22:11:03 +00004080namespace clang {
4081 /// \brief An identifier-lookup iterator that enumerates all of the
4082 /// identifiers stored within a set of AST files.
4083 class ASTIdentifierIterator : public IdentifierIterator {
4084 /// \brief The AST reader whose identifiers are being enumerated.
4085 const ASTReader &Reader;
4086
4087 /// \brief The current index into the chain of AST files stored in
4088 /// the AST reader.
4089 unsigned Index;
4090
4091 /// \brief The current position within the identifier lookup table
4092 /// of the current AST file.
4093 ASTIdentifierLookupTable::key_iterator Current;
4094
4095 /// \brief The end position within the identifier lookup table of
4096 /// the current AST file.
4097 ASTIdentifierLookupTable::key_iterator End;
4098
4099 public:
4100 explicit ASTIdentifierIterator(const ASTReader &Reader);
4101
4102 virtual llvm::StringRef Next();
4103 };
4104}
4105
4106ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
4107 : Reader(Reader), Index(Reader.Chain.size() - 1) {
4108 ASTIdentifierLookupTable *IdTable
4109 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
4110 Current = IdTable->key_begin();
4111 End = IdTable->key_end();
4112}
4113
4114llvm::StringRef ASTIdentifierIterator::Next() {
4115 while (Current == End) {
4116 // If we have exhausted all of our AST files, we're done.
4117 if (Index == 0)
4118 return llvm::StringRef();
4119
4120 --Index;
4121 ASTIdentifierLookupTable *IdTable
4122 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
4123 Current = IdTable->key_begin();
4124 End = IdTable->key_end();
4125 }
4126
4127 // We have any identifiers remaining in the current AST file; return
4128 // the next one.
4129 std::pair<const char*, unsigned> Key = *Current;
4130 ++Current;
4131 return llvm::StringRef(Key.first, Key.second);
4132}
4133
4134IdentifierIterator *ASTReader::getIdentifiers() const {
4135 return new ASTIdentifierIterator(*this);
4136}
4137
Mike Stump11289f42009-09-09 15:08:12 +00004138std::pair<ObjCMethodList, ObjCMethodList>
Sebastian Redl2c499f62010-08-18 23:56:43 +00004139ASTReader::ReadMethodPool(Selector Sel) {
Sebastian Redlada023c2010-08-04 20:40:17 +00004140 // Find this selector in a hash table. We want to find the most recent entry.
4141 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4142 PerFileData &F = *Chain[I];
4143 if (!F.SelectorLookupTable)
4144 continue;
Douglas Gregorc78d3462009-04-24 21:10:55 +00004145
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004146 ASTSelectorLookupTable *PoolTable
4147 = (ASTSelectorLookupTable*)F.SelectorLookupTable;
4148 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
Sebastian Redlada023c2010-08-04 20:40:17 +00004149 if (Pos != PoolTable->end()) {
4150 ++NumSelectorsRead;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004151 // FIXME: Not quite happy with the statistics here. We probably should
4152 // disable this tracking when called via LoadSelector.
4153 // Also, should entries without methods count as misses?
4154 ++NumMethodPoolEntriesRead;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004155 ASTSelectorLookupTrait::data_type Data = *Pos;
Sebastian Redlada023c2010-08-04 20:40:17 +00004156 if (DeserializationListener)
4157 DeserializationListener->SelectorRead(Data.ID, Sel);
4158 return std::make_pair(Data.Instance, Data.Factory);
4159 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00004160 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00004161
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00004162 ++NumMethodPoolMisses;
Sebastian Redlada023c2010-08-04 20:40:17 +00004163 return std::pair<ObjCMethodList, ObjCMethodList>();
Douglas Gregorc78d3462009-04-24 21:10:55 +00004164}
4165
Sebastian Redl2c499f62010-08-18 23:56:43 +00004166void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00004167 // It would be complicated to avoid reading the methods anyway. So don't.
4168 ReadMethodPool(Sel);
4169}
4170
Sebastian Redl2c499f62010-08-18 23:56:43 +00004171void ASTReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) {
Douglas Gregora868bbd2009-04-21 22:25:48 +00004172 assert(ID && "Non-zero identifier ID required");
Douglas Gregor6f00bf82009-04-28 21:53:25 +00004173 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor0e149972009-04-25 19:10:14 +00004174 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlff4a2952010-07-23 23:49:55 +00004175 if (DeserializationListener)
4176 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregora868bbd2009-04-21 22:25:48 +00004177}
4178
Douglas Gregor1342e842009-07-06 18:54:52 +00004179/// \brief Set the globally-visible declarations associated with the given
4180/// identifier.
4181///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004182/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump11289f42009-09-09 15:08:12 +00004183/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregor1342e842009-07-06 18:54:52 +00004184/// them.
4185///
4186/// \param II an IdentifierInfo that refers to one or more globally-visible
4187/// declarations.
4188///
4189/// \param DeclIDs the set of declaration IDs with the name @p II that are
4190/// visible at global scope.
4191///
4192/// \param Nonrecursive should be true to indicate that the caller knows that
4193/// this call is non-recursive, and therefore the globally-visible declarations
4194/// will not be placed onto the pending queue.
Mike Stump11289f42009-09-09 15:08:12 +00004195void
Sebastian Redl2c499f62010-08-18 23:56:43 +00004196ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Douglas Gregor1342e842009-07-06 18:54:52 +00004197 const llvm::SmallVectorImpl<uint32_t> &DeclIDs,
4198 bool Nonrecursive) {
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004199 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregor1342e842009-07-06 18:54:52 +00004200 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
4201 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
4202 PII.II = II;
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00004203 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregor1342e842009-07-06 18:54:52 +00004204 return;
4205 }
Mike Stump11289f42009-09-09 15:08:12 +00004206
Douglas Gregor1342e842009-07-06 18:54:52 +00004207 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
4208 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
4209 if (SemaObj) {
Douglas Gregor6fd55e02010-08-13 03:15:25 +00004210 if (SemaObj->TUScope) {
4211 // Introduce this declaration into the translation-unit scope
4212 // and add it to the declaration chain for this identifier, so
4213 // that (unqualified) name lookup will find it.
John McCall48871652010-08-21 09:40:31 +00004214 SemaObj->TUScope->AddDecl(D);
Douglas Gregor6fd55e02010-08-13 03:15:25 +00004215 }
Douglas Gregor2fb99df2010-09-24 23:29:12 +00004216 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
Douglas Gregor1342e842009-07-06 18:54:52 +00004217 } else {
4218 // Queue this declaration so that it will be added to the
4219 // translation unit scope and identifier's declaration chain
4220 // once a Sema object is known.
4221 PreloadedDecls.push_back(D);
4222 }
4223 }
4224}
4225
Sebastian Redl2c499f62010-08-18 23:56:43 +00004226IdentifierInfo *ASTReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004227 if (ID == 0)
4228 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00004229
Sebastian Redlc713b962010-07-21 00:46:22 +00004230 if (IdentifiersLoaded.empty()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004231 Error("no identifier table in AST file");
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004232 return 0;
4233 }
Mike Stump11289f42009-09-09 15:08:12 +00004234
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00004235 assert(PP && "Forgot to set Preprocessor ?");
Sebastian Redlc713b962010-07-21 00:46:22 +00004236 ID -= 1;
4237 if (!IdentifiersLoaded[ID]) {
4238 unsigned Index = ID;
4239 const char *Str = 0;
4240 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4241 PerFileData *F = Chain[N - I - 1];
4242 if (Index < F->LocalNumIdentifiers) {
4243 uint32_t Offset = F->IdentifierOffsets[Index];
4244 Str = F->IdentifierTableData + Offset;
4245 break;
4246 }
4247 Index -= F->LocalNumIdentifiers;
4248 }
4249 assert(Str && "Broken Chain");
Douglas Gregor5287b4e2009-04-25 21:04:17 +00004250
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004251 // All of the strings in the AST file are preceded by a 16-bit length.
4252 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenekca42a512009-10-23 04:45:31 +00004253 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
4254 // unsigned integers. This is important to avoid integer overflow when
4255 // we cast them to 'unsigned'.
Ted Kremenek49c52322009-10-23 03:57:22 +00004256 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregorab4df582009-04-28 20:01:51 +00004257 unsigned StrLen = (((unsigned) StrLenPtr[0])
4258 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redlc713b962010-07-21 00:46:22 +00004259 IdentifiersLoaded[ID]
Kovarththanan Rajaratnama3b09592010-03-12 10:32:27 +00004260 = &PP->getIdentifierTable().get(Str, StrLen);
Sebastian Redlff4a2952010-07-23 23:49:55 +00004261 if (DeserializationListener)
4262 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004263 }
Mike Stump11289f42009-09-09 15:08:12 +00004264
Sebastian Redlc713b962010-07-21 00:46:22 +00004265 return IdentifiersLoaded[ID];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004266}
4267
Sebastian Redl2c499f62010-08-18 23:56:43 +00004268void ASTReader::ReadSLocEntry(unsigned ID) {
Douglas Gregor258ae542009-04-27 06:38:32 +00004269 ReadSLocEntryRecord(ID);
4270}
4271
Sebastian Redl2c499f62010-08-18 23:56:43 +00004272Selector ASTReader::DecodeSelector(unsigned ID) {
Steve Naroff2ddea052009-04-23 10:39:46 +00004273 if (ID == 0)
4274 return Selector();
Mike Stump11289f42009-09-09 15:08:12 +00004275
Sebastian Redlada023c2010-08-04 20:40:17 +00004276 if (ID > SelectorsLoaded.size()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004277 Error("selector ID out of range in AST file");
Steve Naroff2ddea052009-04-23 10:39:46 +00004278 return Selector();
4279 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00004280
Sebastian Redlada023c2010-08-04 20:40:17 +00004281 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00004282 // Load this selector from the selector table.
Sebastian Redlada023c2010-08-04 20:40:17 +00004283 unsigned Idx = ID - 1;
4284 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4285 PerFileData &F = *Chain[N - I - 1];
4286 if (Idx < F.LocalNumSelectors) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004287 ASTSelectorLookupTrait Trait(*this);
Sebastian Redlada023c2010-08-04 20:40:17 +00004288 SelectorsLoaded[ID - 1] =
4289 Trait.ReadKey(F.SelectorLookupTableData + F.SelectorOffsets[Idx], 0);
4290 if (DeserializationListener)
4291 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
4292 break;
4293 }
4294 Idx -= F.LocalNumSelectors;
4295 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00004296 }
4297
Sebastian Redlada023c2010-08-04 20:40:17 +00004298 return SelectorsLoaded[ID - 1];
Steve Naroff2ddea052009-04-23 10:39:46 +00004299}
4300
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004301Selector ASTReader::GetExternalSelector(uint32_t ID) {
Douglas Gregord720daf2010-04-06 17:30:22 +00004302 return DecodeSelector(ID);
4303}
4304
Sebastian Redl2c499f62010-08-18 23:56:43 +00004305uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redlada023c2010-08-04 20:40:17 +00004306 // ID 0 (the null selector) is considered an external selector.
4307 return getTotalNumSelectors() + 1;
Douglas Gregord720daf2010-04-06 17:30:22 +00004308}
4309
Mike Stump11289f42009-09-09 15:08:12 +00004310DeclarationName
Sebastian Redl2c499f62010-08-18 23:56:43 +00004311ASTReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004312 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
4313 switch (Kind) {
4314 case DeclarationName::Identifier:
4315 return DeclarationName(GetIdentifierInfo(Record, Idx));
4316
4317 case DeclarationName::ObjCZeroArgSelector:
4318 case DeclarationName::ObjCOneArgSelector:
4319 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff3c301dc2009-04-23 15:15:40 +00004320 return DeclarationName(GetSelector(Record, Idx));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004321
4322 case DeclarationName::CXXConstructorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004323 return Context->DeclarationNames.getCXXConstructorName(
Douglas Gregor2211d342009-08-05 05:36:45 +00004324 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004325
4326 case DeclarationName::CXXDestructorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004327 return Context->DeclarationNames.getCXXDestructorName(
Douglas Gregor2211d342009-08-05 05:36:45 +00004328 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004329
4330 case DeclarationName::CXXConversionFunctionName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004331 return Context->DeclarationNames.getCXXConversionFunctionName(
Douglas Gregor2211d342009-08-05 05:36:45 +00004332 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004333
4334 case DeclarationName::CXXOperatorName:
Chris Lattner8575daa2009-04-27 21:45:14 +00004335 return Context->DeclarationNames.getCXXOperatorName(
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004336 (OverloadedOperatorKind)Record[Idx++]);
4337
Alexis Hunt3d221f22009-11-29 07:34:05 +00004338 case DeclarationName::CXXLiteralOperatorName:
4339 return Context->DeclarationNames.getCXXLiteralOperatorName(
4340 GetIdentifierInfo(Record, Idx));
4341
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004342 case DeclarationName::CXXUsingDirective:
4343 return DeclarationName::getUsingDirectiveName();
4344 }
4345
4346 // Required to silence GCC warning
4347 return DeclarationName();
4348}
Douglas Gregor55abb232009-04-10 20:39:37 +00004349
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004350void ASTReader::ReadDeclarationNameLoc(PerFileData &F,
4351 DeclarationNameLoc &DNLoc,
4352 DeclarationName Name,
4353 const RecordData &Record, unsigned &Idx) {
4354 switch (Name.getNameKind()) {
4355 case DeclarationName::CXXConstructorName:
4356 case DeclarationName::CXXDestructorName:
4357 case DeclarationName::CXXConversionFunctionName:
4358 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
4359 break;
4360
4361 case DeclarationName::CXXOperatorName:
4362 DNLoc.CXXOperatorName.BeginOpNameLoc
4363 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4364 DNLoc.CXXOperatorName.EndOpNameLoc
4365 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4366 break;
4367
4368 case DeclarationName::CXXLiteralOperatorName:
4369 DNLoc.CXXLiteralOperatorName.OpNameLoc
4370 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4371 break;
4372
4373 case DeclarationName::Identifier:
4374 case DeclarationName::ObjCZeroArgSelector:
4375 case DeclarationName::ObjCOneArgSelector:
4376 case DeclarationName::ObjCMultiArgSelector:
4377 case DeclarationName::CXXUsingDirective:
4378 break;
4379 }
4380}
4381
4382void ASTReader::ReadDeclarationNameInfo(PerFileData &F,
4383 DeclarationNameInfo &NameInfo,
4384 const RecordData &Record, unsigned &Idx) {
4385 NameInfo.setName(ReadDeclarationName(Record, Idx));
4386 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
4387 DeclarationNameLoc DNLoc;
4388 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
4389 NameInfo.setInfo(DNLoc);
4390}
4391
4392void ASTReader::ReadQualifierInfo(PerFileData &F, QualifierInfo &Info,
4393 const RecordData &Record, unsigned &Idx) {
4394 Info.NNS = ReadNestedNameSpecifier(Record, Idx);
4395 Info.NNSRange = ReadSourceRange(F, Record, Idx);
4396 unsigned NumTPLists = Record[Idx++];
4397 Info.NumTemplParamLists = NumTPLists;
4398 if (NumTPLists) {
4399 Info.TemplParamLists = new (*Context) TemplateParameterList*[NumTPLists];
4400 for (unsigned i=0; i != NumTPLists; ++i)
4401 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
4402 }
4403}
4404
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004405TemplateName
Douglas Gregor5590be02011-01-15 06:45:20 +00004406ASTReader::ReadTemplateName(PerFileData &F, const RecordData &Record,
4407 unsigned &Idx) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004408 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004409 switch (Kind) {
4410 case TemplateName::Template:
4411 return TemplateName(cast_or_null<TemplateDecl>(GetDecl(Record[Idx++])));
4412
4413 case TemplateName::OverloadedTemplate: {
4414 unsigned size = Record[Idx++];
4415 UnresolvedSet<8> Decls;
4416 while (size--)
4417 Decls.addDecl(cast<NamedDecl>(GetDecl(Record[Idx++])));
4418
4419 return Context->getOverloadedTemplateName(Decls.begin(), Decls.end());
4420 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004421
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004422 case TemplateName::QualifiedTemplate: {
4423 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
4424 bool hasTemplKeyword = Record[Idx++];
4425 TemplateDecl *Template = cast<TemplateDecl>(GetDecl(Record[Idx++]));
4426 return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
4427 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004428
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004429 case TemplateName::DependentTemplate: {
4430 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
4431 if (Record[Idx++]) // isIdentifier
4432 return Context->getDependentTemplateName(NNS,
4433 GetIdentifierInfo(Record, Idx));
4434 return Context->getDependentTemplateName(NNS,
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004435 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004436 }
Douglas Gregor5590be02011-01-15 06:45:20 +00004437
4438 case TemplateName::SubstTemplateTemplateParmPack: {
4439 TemplateTemplateParmDecl *Param
4440 = cast_or_null<TemplateTemplateParmDecl>(GetDecl(Record[Idx++]));
4441 if (!Param)
4442 return TemplateName();
4443
4444 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
4445 if (ArgPack.getKind() != TemplateArgument::Pack)
4446 return TemplateName();
4447
4448 return Context->getSubstTemplateTemplateParmPack(Param, ArgPack);
4449 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004450 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004451
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004452 assert(0 && "Unhandled template name kind!");
4453 return TemplateName();
4454}
4455
4456TemplateArgument
Sebastian Redl2c373b92010-10-05 15:59:54 +00004457ASTReader::ReadTemplateArgument(PerFileData &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00004458 const RecordData &Record, unsigned &Idx) {
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004459 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
4460 switch (Kind) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004461 case TemplateArgument::Null:
4462 return TemplateArgument();
4463 case TemplateArgument::Type:
4464 return TemplateArgument(GetType(Record[Idx++]));
4465 case TemplateArgument::Declaration:
4466 return TemplateArgument(GetDecl(Record[Idx++]));
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +00004467 case TemplateArgument::Integral: {
4468 llvm::APSInt Value = ReadAPSInt(Record, Idx);
4469 QualType T = GetType(Record[Idx++]);
4470 return TemplateArgument(Value, T);
4471 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004472 case TemplateArgument::Template:
Douglas Gregor5590be02011-01-15 06:45:20 +00004473 return TemplateArgument(ReadTemplateName(F, Record, Idx));
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004474 case TemplateArgument::TemplateExpansion: {
Douglas Gregor5590be02011-01-15 06:45:20 +00004475 TemplateName Name = ReadTemplateName(F, Record, Idx);
Douglas Gregore1d60df2011-01-14 23:41:42 +00004476 llvm::Optional<unsigned> NumTemplateExpansions;
4477 if (unsigned NumExpansions = Record[Idx++])
4478 NumTemplateExpansions = NumExpansions - 1;
4479 return TemplateArgument(Name, NumTemplateExpansions);
Douglas Gregoreb29d182011-01-05 17:40:24 +00004480 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004481 case TemplateArgument::Expression:
Sebastian Redl2c373b92010-10-05 15:59:54 +00004482 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004483 case TemplateArgument::Pack: {
4484 unsigned NumArgs = Record[Idx++];
Douglas Gregor1ccc8412010-11-07 23:05:16 +00004485 TemplateArgument *Args = new (*Context) TemplateArgument[NumArgs];
4486 for (unsigned I = 0; I != NumArgs; ++I)
4487 Args[I] = ReadTemplateArgument(F, Record, Idx);
4488 return TemplateArgument(Args, NumArgs);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004489 }
4490 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004491
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004492 assert(0 && "Unhandled template argument kind!");
4493 return TemplateArgument();
4494}
4495
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004496TemplateParameterList *
Sebastian Redl2c373b92010-10-05 15:59:54 +00004497ASTReader::ReadTemplateParameterList(PerFileData &F,
4498 const RecordData &Record, unsigned &Idx) {
4499 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
4500 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
4501 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004502
4503 unsigned NumParams = Record[Idx++];
4504 llvm::SmallVector<NamedDecl *, 16> Params;
4505 Params.reserve(NumParams);
4506 while (NumParams--)
4507 Params.push_back(cast<NamedDecl>(GetDecl(Record[Idx++])));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004508
4509 TemplateParameterList* TemplateParams =
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004510 TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc,
4511 Params.data(), Params.size(), RAngleLoc);
4512 return TemplateParams;
4513}
4514
4515void
Sebastian Redl2c499f62010-08-18 23:56:43 +00004516ASTReader::
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004517ReadTemplateArgumentList(llvm::SmallVector<TemplateArgument, 8> &TemplArgs,
Sebastian Redl2c373b92010-10-05 15:59:54 +00004518 PerFileData &F, const RecordData &Record,
4519 unsigned &Idx) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004520 unsigned NumTemplateArgs = Record[Idx++];
4521 TemplArgs.reserve(NumTemplateArgs);
4522 while (NumTemplateArgs--)
Sebastian Redl2c373b92010-10-05 15:59:54 +00004523 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004524}
4525
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004526/// \brief Read a UnresolvedSet structure.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004527void ASTReader::ReadUnresolvedSet(UnresolvedSetImpl &Set,
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004528 const RecordData &Record, unsigned &Idx) {
4529 unsigned NumDecls = Record[Idx++];
4530 while (NumDecls--) {
4531 NamedDecl *D = cast<NamedDecl>(GetDecl(Record[Idx++]));
4532 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
4533 Set.addDecl(D, AS);
4534 }
4535}
4536
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004537CXXBaseSpecifier
Sebastian Redl2c373b92010-10-05 15:59:54 +00004538ASTReader::ReadCXXBaseSpecifier(PerFileData &F,
Nick Lewycky19b9f952010-07-26 16:56:01 +00004539 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004540 bool isVirtual = static_cast<bool>(Record[Idx++]);
4541 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
4542 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redl08905022011-02-05 19:23:19 +00004543 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00004544 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
4545 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor752a5952011-01-03 22:36:02 +00004546 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redl08905022011-02-05 19:23:19 +00004547 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
Douglas Gregor752a5952011-01-03 22:36:02 +00004548 EllipsisLoc);
Sebastian Redl08905022011-02-05 19:23:19 +00004549 Result.setInheritConstructors(inheritConstructors);
4550 return Result;
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004551}
4552
Alexis Hunt1d792652011-01-08 20:30:50 +00004553std::pair<CXXCtorInitializer **, unsigned>
4554ASTReader::ReadCXXCtorInitializers(PerFileData &F, const RecordData &Record,
4555 unsigned &Idx) {
4556 CXXCtorInitializer **CtorInitializers = 0;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004557 unsigned NumInitializers = Record[Idx++];
4558 if (NumInitializers) {
4559 ASTContext &C = *getContext();
4560
Alexis Hunt1d792652011-01-08 20:30:50 +00004561 CtorInitializers
4562 = new (C) CXXCtorInitializer*[NumInitializers];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004563 for (unsigned i=0; i != NumInitializers; ++i) {
4564 TypeSourceInfo *BaseClassInfo = 0;
4565 bool IsBaseVirtual = false;
4566 FieldDecl *Member = 0;
Francois Pichetd583da02010-12-04 09:14:42 +00004567 IndirectFieldDecl *IndirectMember = 0;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004568
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004569 bool IsBaseInitializer = Record[Idx++];
4570 if (IsBaseInitializer) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004571 BaseClassInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004572 IsBaseVirtual = Record[Idx++];
4573 } else {
Francois Pichetd583da02010-12-04 09:14:42 +00004574 bool IsIndirectMemberInitializer = Record[Idx++];
4575 if (IsIndirectMemberInitializer)
4576 IndirectMember = cast<IndirectFieldDecl>(GetDecl(Record[Idx++]));
4577 else
4578 Member = cast<FieldDecl>(GetDecl(Record[Idx++]));
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004579 }
Douglas Gregor44e7df62011-01-04 00:32:56 +00004580 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redl2c373b92010-10-05 15:59:54 +00004581 Expr *Init = ReadExpr(F);
Sebastian Redl2c373b92010-10-05 15:59:54 +00004582 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
4583 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004584 bool IsWritten = Record[Idx++];
4585 unsigned SourceOrderOrNumArrayIndices;
4586 llvm::SmallVector<VarDecl *, 8> Indices;
4587 if (IsWritten) {
4588 SourceOrderOrNumArrayIndices = Record[Idx++];
4589 } else {
4590 SourceOrderOrNumArrayIndices = Record[Idx++];
4591 Indices.reserve(SourceOrderOrNumArrayIndices);
4592 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
4593 Indices.push_back(cast<VarDecl>(GetDecl(Record[Idx++])));
4594 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004595
Alexis Hunt1d792652011-01-08 20:30:50 +00004596 CXXCtorInitializer *BOMInit;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004597 if (IsBaseInitializer) {
Alexis Hunt1d792652011-01-08 20:30:50 +00004598 BOMInit = new (C) CXXCtorInitializer(C, BaseClassInfo, IsBaseVirtual,
4599 LParenLoc, Init, RParenLoc,
4600 MemberOrEllipsisLoc);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004601 } else if (IsWritten) {
Francois Pichetd583da02010-12-04 09:14:42 +00004602 if (Member)
Alexis Hunt1d792652011-01-08 20:30:50 +00004603 BOMInit = new (C) CXXCtorInitializer(C, Member, MemberOrEllipsisLoc,
4604 LParenLoc, Init, RParenLoc);
Francois Pichetd583da02010-12-04 09:14:42 +00004605 else
Alexis Hunt1d792652011-01-08 20:30:50 +00004606 BOMInit = new (C) CXXCtorInitializer(C, IndirectMember,
4607 MemberOrEllipsisLoc, LParenLoc,
4608 Init, RParenLoc);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004609 } else {
Alexis Hunt1d792652011-01-08 20:30:50 +00004610 BOMInit = CXXCtorInitializer::Create(C, Member, MemberOrEllipsisLoc,
4611 LParenLoc, Init, RParenLoc,
4612 Indices.data(), Indices.size());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004613 }
4614
Argyrios Kyrtzidisd05f3e32010-09-06 19:04:27 +00004615 if (IsWritten)
4616 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Alexis Hunt1d792652011-01-08 20:30:50 +00004617 CtorInitializers[i] = BOMInit;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004618 }
4619 }
4620
Alexis Hunt1d792652011-01-08 20:30:50 +00004621 return std::make_pair(CtorInitializers, NumInitializers);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004622}
4623
Chris Lattnerca025db2010-05-07 21:43:38 +00004624NestedNameSpecifier *
Sebastian Redl2c499f62010-08-18 23:56:43 +00004625ASTReader::ReadNestedNameSpecifier(const RecordData &Record, unsigned &Idx) {
Chris Lattnerca025db2010-05-07 21:43:38 +00004626 unsigned N = Record[Idx++];
4627 NestedNameSpecifier *NNS = 0, *Prev = 0;
4628 for (unsigned I = 0; I != N; ++I) {
4629 NestedNameSpecifier::SpecifierKind Kind
4630 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
4631 switch (Kind) {
4632 case NestedNameSpecifier::Identifier: {
4633 IdentifierInfo *II = GetIdentifierInfo(Record, Idx);
4634 NNS = NestedNameSpecifier::Create(*Context, Prev, II);
4635 break;
4636 }
4637
4638 case NestedNameSpecifier::Namespace: {
4639 NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++]));
4640 NNS = NestedNameSpecifier::Create(*Context, Prev, NS);
4641 break;
4642 }
4643
4644 case NestedNameSpecifier::TypeSpec:
4645 case NestedNameSpecifier::TypeSpecWithTemplate: {
John McCall424cec92011-01-19 06:33:43 +00004646 const Type *T = GetType(Record[Idx++]).getTypePtrOrNull();
Douglas Gregor0cdc8322010-12-10 17:03:06 +00004647 if (!T)
4648 return 0;
4649
Chris Lattnerca025db2010-05-07 21:43:38 +00004650 bool Template = Record[Idx++];
4651 NNS = NestedNameSpecifier::Create(*Context, Prev, Template, T);
4652 break;
4653 }
4654
4655 case NestedNameSpecifier::Global: {
4656 NNS = NestedNameSpecifier::GlobalSpecifier(*Context);
4657 // No associated value, and there can't be a prefix.
4658 break;
4659 }
Chris Lattnerca025db2010-05-07 21:43:38 +00004660 }
Argyrios Kyrtzidisad65c692010-07-07 15:46:30 +00004661 Prev = NNS;
Chris Lattnerca025db2010-05-07 21:43:38 +00004662 }
4663 return NNS;
4664}
4665
4666SourceRange
Sebastian Redl2c373b92010-10-05 15:59:54 +00004667ASTReader::ReadSourceRange(PerFileData &F, const RecordData &Record,
4668 unsigned &Idx) {
4669 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
4670 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar6d3bc082010-06-02 15:47:10 +00004671 return SourceRange(beg, end);
Chris Lattnerca025db2010-05-07 21:43:38 +00004672}
4673
Douglas Gregor1daeb692009-04-13 18:14:40 +00004674/// \brief Read an integral value
Sebastian Redl2c499f62010-08-18 23:56:43 +00004675llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00004676 unsigned BitWidth = Record[Idx++];
4677 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
4678 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
4679 Idx += NumWords;
4680 return Result;
4681}
4682
4683/// \brief Read a signed integral value
Sebastian Redl2c499f62010-08-18 23:56:43 +00004684llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00004685 bool isUnsigned = Record[Idx++];
4686 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
4687}
4688
Douglas Gregore0a3a512009-04-14 21:55:33 +00004689/// \brief Read a floating-point value
Sebastian Redl2c499f62010-08-18 23:56:43 +00004690llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00004691 return llvm::APFloat(ReadAPInt(Record, Idx));
4692}
4693
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004694// \brief Read a string
Sebastian Redl2c499f62010-08-18 23:56:43 +00004695std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004696 unsigned Len = Record[Idx++];
Jay Foad7d0479f2009-05-21 09:52:38 +00004697 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004698 Idx += Len;
4699 return Result;
4700}
4701
Sebastian Redl2c499f62010-08-18 23:56:43 +00004702CXXTemporary *ASTReader::ReadCXXTemporary(const RecordData &Record,
Chris Lattnercba86142010-05-10 00:25:06 +00004703 unsigned &Idx) {
4704 CXXDestructorDecl *Decl = cast<CXXDestructorDecl>(GetDecl(Record[Idx++]));
4705 return CXXTemporary::Create(*Context, Decl);
4706}
4707
Sebastian Redl2c499f62010-08-18 23:56:43 +00004708DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregor92863e42009-04-10 23:10:45 +00004709 return Diag(SourceLocation(), DiagID);
4710}
4711
Sebastian Redl2c499f62010-08-18 23:56:43 +00004712DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004713 return Diags.Report(Loc, DiagID);
Douglas Gregor55abb232009-04-10 20:39:37 +00004714}
Douglas Gregora9af1d12009-04-17 00:04:06 +00004715
Douglas Gregora868bbd2009-04-21 22:25:48 +00004716/// \brief Retrieve the identifier table associated with the
4717/// preprocessor.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004718IdentifierTable &ASTReader::getIdentifierTable() {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00004719 assert(PP && "Forgot to set Preprocessor ?");
4720 return PP->getIdentifierTable();
Douglas Gregora868bbd2009-04-21 22:25:48 +00004721}
4722
Douglas Gregora9af1d12009-04-17 00:04:06 +00004723/// \brief Record that the given ID maps to the given switch-case
4724/// statement.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004725void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Douglas Gregora9af1d12009-04-17 00:04:06 +00004726 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
4727 SwitchCaseStmts[ID] = SC;
4728}
4729
4730/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004731SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Douglas Gregora9af1d12009-04-17 00:04:06 +00004732 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
4733 return SwitchCaseStmts[ID];
4734}
Douglas Gregor6cc68a42009-04-17 18:18:49 +00004735
Argyrios Kyrtzidisd9f526f2010-10-28 09:29:32 +00004736void ASTReader::ClearSwitchCaseIDs() {
4737 SwitchCaseStmts.clear();
4738}
4739
Douglas Gregor6cc68a42009-04-17 18:18:49 +00004740/// \brief Record that the given label statement has been
4741/// deserialized and has the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004742void ASTReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
Mike Stump11289f42009-09-09 15:08:12 +00004743 assert(LabelStmts.find(ID) == LabelStmts.end() &&
Douglas Gregor6cc68a42009-04-17 18:18:49 +00004744 "Deserialized label twice");
4745 LabelStmts[ID] = S;
4746
4747 // If we've already seen any goto statements that point to this
4748 // label, resolve them now.
4749 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
4750 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
4751 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
4752 Goto->second->setLabel(S);
4753 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor779d8652009-04-17 18:58:21 +00004754
4755 // If we've already seen any address-label statements that point to
4756 // this label, resolve them now.
4757 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
Mike Stump11289f42009-09-09 15:08:12 +00004758 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
Douglas Gregor779d8652009-04-17 18:58:21 +00004759 = UnresolvedAddrLabelExprs.equal_range(ID);
Mike Stump11289f42009-09-09 15:08:12 +00004760 for (AddrLabelIter AddrLabel = AddrLabels.first;
Douglas Gregor779d8652009-04-17 18:58:21 +00004761 AddrLabel != AddrLabels.second; ++AddrLabel)
4762 AddrLabel->second->setLabel(S);
4763 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor6cc68a42009-04-17 18:18:49 +00004764}
4765
4766/// \brief Set the label of the given statement to the label
4767/// identified by ID.
4768///
4769/// Depending on the order in which the label and other statements
4770/// referencing that label occur, this operation may complete
4771/// immediately (updating the statement) or it may queue the
4772/// statement to be back-patched later.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004773void ASTReader::SetLabelOf(GotoStmt *S, unsigned ID) {
Douglas Gregor6cc68a42009-04-17 18:18:49 +00004774 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
4775 if (Label != LabelStmts.end()) {
4776 // We've already seen this label, so set the label of the goto and
4777 // we're done.
4778 S->setLabel(Label->second);
4779 } else {
4780 // We haven't seen this label yet, so add this goto to the set of
4781 // unresolved goto statements.
4782 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
4783 }
4784}
Douglas Gregor779d8652009-04-17 18:58:21 +00004785
4786/// \brief Set the label of the given expression to the label
4787/// identified by ID.
4788///
4789/// Depending on the order in which the label and other statements
4790/// referencing that label occur, this operation may complete
4791/// immediately (updating the statement) or it may queue the
4792/// statement to be back-patched later.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004793void ASTReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
Douglas Gregor779d8652009-04-17 18:58:21 +00004794 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
4795 if (Label != LabelStmts.end()) {
4796 // We've already seen this label, so set the label of the
4797 // label-address expression and we're done.
4798 S->setLabel(Label->second);
4799 } else {
4800 // We haven't seen this label yet, so add this label-address
4801 // expression to the set of unresolved label-address expressions.
4802 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
4803 }
4804}
Douglas Gregor1342e842009-07-06 18:54:52 +00004805
Sebastian Redl2c499f62010-08-18 23:56:43 +00004806void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004807 assert(NumCurrentElementsDeserializing &&
4808 "FinishedDeserializing not paired with StartedDeserializing");
4809 if (NumCurrentElementsDeserializing == 1) {
Douglas Gregor1342e842009-07-06 18:54:52 +00004810 // If any identifiers with corresponding top-level declarations have
4811 // been loaded, load those declarations now.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004812 while (!PendingIdentifierInfos.empty()) {
4813 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
4814 PendingIdentifierInfos.front().DeclIDs, true);
4815 PendingIdentifierInfos.pop_front();
Douglas Gregor1342e842009-07-06 18:54:52 +00004816 }
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00004817
4818 // We are not in recursive loading, so it's safe to pass the "interesting"
4819 // decls to the consumer.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004820 if (Consumer)
4821 PassInterestingDeclsToConsumer();
Argyrios Kyrtzidisad5f95c2010-10-24 17:26:31 +00004822
4823 assert(PendingForwardRefs.size() == 0 &&
4824 "Some forward refs did not get linked to the definition!");
Douglas Gregor1342e842009-07-06 18:54:52 +00004825 }
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004826 --NumCurrentElementsDeserializing;
Douglas Gregor1342e842009-07-06 18:54:52 +00004827}
Douglas Gregorb473b072010-08-19 00:28:17 +00004828
Sebastian Redld7dce0a2010-08-24 00:50:04 +00004829ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context,
Douglas Gregor606c4ac2011-02-05 19:42:43 +00004830 const char *isysroot, bool DisableValidation,
4831 bool DisableStatCache)
Sebastian Redld7dce0a2010-08-24 00:50:04 +00004832 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
4833 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
4834 Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context),
4835 Consumer(0), isysroot(isysroot), DisableValidation(DisableValidation),
Douglas Gregor606c4ac2011-02-05 19:42:43 +00004836 DisableStatCache(DisableStatCache), NumStatHits(0), NumStatMisses(0),
4837 NumSLocEntriesRead(0), TotalNumSLocEntries(0), NextSLocOffset(0),
4838 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
4839 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
4840 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
4841 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
4842 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
4843 NumCurrentElementsDeserializing(0)
4844{
Sebastian Redld7dce0a2010-08-24 00:50:04 +00004845 RelocatablePCH = false;
4846}
4847
4848ASTReader::ASTReader(SourceManager &SourceMgr, FileManager &FileMgr,
4849 Diagnostic &Diags, const char *isysroot,
Douglas Gregor606c4ac2011-02-05 19:42:43 +00004850 bool DisableValidation, bool DisableStatCache)
Sebastian Redld7dce0a2010-08-24 00:50:04 +00004851 : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr),
4852 Diags(Diags), SemaObj(0), PP(0), Context(0), Consumer(0),
Douglas Gregor606c4ac2011-02-05 19:42:43 +00004853 isysroot(isysroot), DisableValidation(DisableValidation),
4854 DisableStatCache(DisableStatCache), NumStatHits(0), NumStatMisses(0),
4855 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Sebastian Redlc1d035f2010-09-22 20:19:08 +00004856 NextSLocOffset(0), NumStatementsRead(0), TotalNumStatements(0),
4857 NumMacrosRead(0), TotalNumMacros(0), NumSelectorsRead(0),
4858 NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
4859 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
4860 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
4861 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
Sebastian Redld7dce0a2010-08-24 00:50:04 +00004862 RelocatablePCH = false;
4863}
4864
4865ASTReader::~ASTReader() {
4866 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
4867 delete Chain[e - i - 1];
4868 // Delete all visible decl lookup tables
4869 for (DeclContextOffsetsMap::iterator I = DeclContextOffsets.begin(),
4870 E = DeclContextOffsets.end();
4871 I != E; ++I) {
4872 for (DeclContextInfos::iterator J = I->second.begin(), F = I->second.end();
4873 J != F; ++J) {
4874 if (J->NameLookupTableData)
4875 delete static_cast<ASTDeclContextNameLookupTable*>(
4876 J->NameLookupTableData);
4877 }
4878 }
4879 for (DeclContextVisibleUpdatesPending::iterator
4880 I = PendingVisibleUpdates.begin(),
4881 E = PendingVisibleUpdates.end();
4882 I != E; ++I) {
4883 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
4884 F = I->second.end();
4885 J != F; ++J)
4886 delete static_cast<ASTDeclContextNameLookupTable*>(*J);
4887 }
4888}
4889
Sebastian Redl009e7f22010-10-05 16:15:19 +00004890ASTReader::PerFileData::PerFileData(ASTFileType Ty)
4891 : Type(Ty), SizeInBits(0), LocalNumSLocEntries(0), SLocOffsets(0), LocalSLocSize(0),
Sebastian Redl949fe9e2010-09-22 00:42:27 +00004892 LocalNumIdentifiers(0), IdentifierOffsets(0), IdentifierTableData(0),
4893 IdentifierLookupTable(0), LocalNumMacroDefinitions(0),
Douglas Gregor09b69892011-02-10 17:09:37 +00004894 MacroDefinitionOffsets(0),
4895 LocalNumHeaderFileInfos(0), HeaderFileInfoTableData(0),
4896 HeaderFileInfoTable(0),
4897 LocalNumSelectors(0), SelectorOffsets(0),
Sebastian Redl949fe9e2010-09-22 00:42:27 +00004898 SelectorLookupTableData(0), SelectorLookupTable(0), LocalNumDecls(0),
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004899 DeclOffsets(0), LocalNumCXXBaseSpecifiers(0), CXXBaseSpecifiersOffsets(0),
4900 LocalNumTypes(0), TypeOffsets(0), StatCache(0),
Sebastian Redl3f6b7532010-10-01 19:59:12 +00004901 NumPreallocatedPreprocessingEntities(0), NextInSource(0)
Douglas Gregorb473b072010-08-19 00:28:17 +00004902{}
4903
4904ASTReader::PerFileData::~PerFileData() {
4905 delete static_cast<ASTIdentifierLookupTable *>(IdentifierLookupTable);
Douglas Gregor09b69892011-02-10 17:09:37 +00004906 delete static_cast<HeaderFileInfoLookupTable *>(HeaderFileInfoTable);
Douglas Gregorb473b072010-08-19 00:28:17 +00004907 delete static_cast<ASTSelectorLookupTable *>(SelectorLookupTable);
4908}
4909