blob: e37083ab034512f8b6549abf88bcac16cfec0afe [file] [log] [blame]
Sebastian Redl904c9c82010-08-18 23:57:11 +00001//===--- ASTReader.cpp - AST File Reader ------------------------*- C++ -*-===//
Douglas Gregor2cf26342009-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 Redlc43b54c2010-08-18 23:56:43 +000010// This file defines the ASTReader class, which reads AST files.
Douglas Gregor2cf26342009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
Chris Lattner4c6f9522009-04-27 05:14:47 +000013
Sebastian Redl6ab7cd82010-08-18 23:57:17 +000014#include "clang/Serialization/ASTReader.h"
15#include "clang/Serialization/ASTDeserializationListener.h"
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +000016#include "ASTCommon.h"
Douglas Gregor0a0428e2009-04-10 20:39:37 +000017#include "clang/Frontend/FrontendDiagnostic.h"
Daniel Dunbarc7162932009-11-11 23:58:53 +000018#include "clang/Frontend/Utils.h"
Douglas Gregore737f502010-08-12 20:07:10 +000019#include "clang/Sema/Sema.h"
John McCall5f1e0942010-08-24 08:50:51 +000020#include "clang/Sema/Scope.h"
Douglas Gregorfdd01722009-04-14 00:24:19 +000021#include "clang/AST/ASTConsumer.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000022#include "clang/AST/ASTContext.h"
John McCall2a7fb272010-08-25 05:32:35 +000023#include "clang/AST/DeclTemplate.h"
Douglas Gregor0b748912009-04-14 21:18:50 +000024#include "clang/AST/Expr.h"
John McCall7a1fad32010-08-24 07:32:53 +000025#include "clang/AST/ExprCXX.h"
Douglas Gregor5f791bb2011-02-28 23:58:31 +000026#include "clang/AST/NestedNameSpecifier.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000027#include "clang/AST/Type.h"
John McCalla1ee0c52009-10-16 21:56:05 +000028#include "clang/AST/TypeLocVisitor.h"
Chris Lattner42d42b52009-04-10 21:41:48 +000029#include "clang/Lex/MacroInfo.h"
Douglas Gregor6a5a23f2010-03-19 21:51:54 +000030#include "clang/Lex/PreprocessingRecord.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000031#include "clang/Lex/Preprocessor.h"
Steve Naroff83d63c72009-04-24 20:03:17 +000032#include "clang/Lex/HeaderSearch.h"
Douglas Gregor668c1a42009-04-21 22:25:48 +000033#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000034#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000035#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000036#include "clang/Basic/FileManager.h"
Chris Lattner10e286a2010-11-23 19:19:34 +000037#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000038#include "clang/Basic/TargetInfo.h"
Douglas Gregor445e23e2009-10-05 21:07:28 +000039#include "clang/Basic/Version.h"
Douglas Gregor0a0d2b12011-03-23 00:50:03 +000040#include "clang/Basic/VersionTuple.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000041#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000042#include "llvm/Bitcode/BitstreamReader.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000043#include "llvm/Support/MemoryBuffer.h"
John McCall833ca992009-10-29 08:12:44 +000044#include "llvm/Support/ErrorHandling.h"
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000045#include "llvm/Support/FileSystem.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000046#include "llvm/Support/Path.h"
Michael J. Spencer3a321e22010-12-09 17:36:38 +000047#include "llvm/Support/system_error.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000048#include <algorithm>
Douglas Gregore721f952009-04-28 18:58:38 +000049#include <iterator>
Douglas Gregor2cf26342009-04-09 22:27:44 +000050#include <cstdio>
Douglas Gregor4fed3f42009-04-27 18:38:38 +000051#include <sys/stat.h>
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000052
Douglas Gregor2cf26342009-04-09 22:27:44 +000053using namespace clang;
Sebastian Redl8538e8d2010-08-18 23:57:32 +000054using namespace clang::serialization;
Douglas Gregor2cf26342009-04-09 22:27:44 +000055
56//===----------------------------------------------------------------------===//
Sebastian Redl3c7f4132010-08-18 23:57:06 +000057// PCH validator implementation
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000058//===----------------------------------------------------------------------===//
59
Sebastian Redl571db7f2010-08-18 23:56:56 +000060ASTReaderListener::~ASTReaderListener() {}
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000061
62bool
63PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts) {
64 const LangOptions &PPLangOpts = PP.getLangOptions();
65#define PARSE_LANGOPT_BENIGN(Option)
66#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
67 if (PPLangOpts.Option != LangOpts.Option) { \
68 Reader.Diag(DiagID) << LangOpts.Option << PPLangOpts.Option; \
69 return true; \
70 }
71
72 PARSE_LANGOPT_BENIGN(Trigraphs);
73 PARSE_LANGOPT_BENIGN(BCPLComment);
74 PARSE_LANGOPT_BENIGN(DollarIdents);
75 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
76 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
Chandler Carrutheb5d7b72010-04-17 20:17:31 +000077 PARSE_LANGOPT_IMPORTANT(GNUKeywords, diag::warn_pch_gnu_keywords);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000078 PARSE_LANGOPT_BENIGN(ImplicitInt);
79 PARSE_LANGOPT_BENIGN(Digraphs);
80 PARSE_LANGOPT_BENIGN(HexFloats);
81 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
Peter Collingbourne7e7fbd02011-04-15 00:35:23 +000082 PARSE_LANGOPT_IMPORTANT(C1X, diag::warn_pch_c1x);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000083 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
Michael J. Spencerdae4ac42010-10-21 05:21:48 +000084 PARSE_LANGOPT_BENIGN(MSCVersion);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000085 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
86 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
87 PARSE_LANGOPT_BENIGN(CXXOperatorName);
88 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
89 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
90 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
Fariborz Jahanian412e7982010-02-09 19:31:38 +000091 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI2, diag::warn_pch_nonfragile_abi2);
Fariborz Jahanianf84109e2011-01-07 18:59:25 +000092 PARSE_LANGOPT_IMPORTANT(AppleKext, diag::warn_pch_apple_kext);
Ted Kremenekc32647d2010-12-23 21:35:43 +000093 PARSE_LANGOPT_IMPORTANT(ObjCDefaultSynthProperties,
94 diag::warn_pch_objc_auto_properties);
Douglas Gregor74da19f2011-06-14 23:20:43 +000095 PARSE_LANGOPT_BENIGN(ObjCInferRelatedResultType)
Michael J. Spencer20249a12010-10-21 03:16:25 +000096 PARSE_LANGOPT_IMPORTANT(NoConstantCFStrings,
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +000097 diag::warn_pch_no_constant_cfstrings);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000098 PARSE_LANGOPT_BENIGN(PascalStrings);
99 PARSE_LANGOPT_BENIGN(WritableStrings);
Mike Stump1eb44332009-09-09 15:08:12 +0000100 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000101 diag::warn_pch_lax_vector_conversions);
Nate Begeman69cfb9b2009-06-25 22:57:40 +0000102 PARSE_LANGOPT_IMPORTANT(AltiVec, diag::warn_pch_altivec);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000103 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
Anders Carlssonda4b7cf2011-02-19 23:53:54 +0000104 PARSE_LANGOPT_IMPORTANT(ObjCExceptions, diag::warn_pch_objc_exceptions);
Anders Carlsson7da99b02011-02-23 03:04:54 +0000105 PARSE_LANGOPT_IMPORTANT(CXXExceptions, diag::warn_pch_cxx_exceptions);
106 PARSE_LANGOPT_IMPORTANT(SjLjExceptions, diag::warn_pch_sjlj_exceptions);
Douglas Gregor6f755502011-02-01 15:15:22 +0000107 PARSE_LANGOPT_IMPORTANT(MSBitfields, diag::warn_pch_ms_bitfields);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000108 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
109 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
110 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
Mike Stump1eb44332009-09-09 15:08:12 +0000111 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000112 diag::warn_pch_thread_safe_statics);
Daniel Dunbar5345c392009-09-03 04:54:28 +0000113 PARSE_LANGOPT_IMPORTANT(POSIXThreads, diag::warn_pch_posix_threads);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000114 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
115 PARSE_LANGOPT_BENIGN(EmitAllDecls);
116 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
Chris Lattnera4d71452010-06-26 21:25:03 +0000117 PARSE_LANGOPT_BENIGN(getSignedOverflowBehavior());
Mike Stump1eb44332009-09-09 15:08:12 +0000118 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000119 diag::warn_pch_heinous_extensions);
120 // FIXME: Most of the options below are benign if the macro wasn't
121 // used. Unfortunately, this means that a PCH compiled without
122 // optimization can't be used with optimization turned on, even
123 // though the only thing that changes is whether __OPTIMIZE__ was
124 // defined... but if __OPTIMIZE__ never showed up in the header, it
125 // doesn't matter. We could consider making this some special kind
126 // of check.
127 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
128 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
129 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
130 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
131 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
132 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
Chandler Carruth0d2d1bc2011-04-23 20:05:38 +0000133 PARSE_LANGOPT_IMPORTANT(Deprecated, diag::warn_pch_deprecated);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000134 PARSE_LANGOPT_IMPORTANT(AccessControl, diag::warn_pch_access_control);
135 PARSE_LANGOPT_IMPORTANT(CharIsSigned, diag::warn_pch_char_signed);
John Thompsona6fda122009-11-05 20:14:16 +0000136 PARSE_LANGOPT_IMPORTANT(ShortWChar, diag::warn_pch_short_wchar);
Argyrios Kyrtzidis9a2b9d72010-10-08 00:25:19 +0000137 PARSE_LANGOPT_IMPORTANT(ShortEnums, diag::warn_pch_short_enums);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000138 if ((PPLangOpts.getGCMode() != 0) != (LangOpts.getGCMode() != 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000139 Reader.Diag(diag::warn_pch_gc_mode)
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000140 << LangOpts.getGCMode() << PPLangOpts.getGCMode();
141 return true;
142 }
143 PARSE_LANGOPT_BENIGN(getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000144 PARSE_LANGOPT_IMPORTANT(getStackProtectorMode(),
145 diag::warn_pch_stack_protector);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000146 PARSE_LANGOPT_BENIGN(InstantiationDepth);
Nate Begeman69cfb9b2009-06-25 22:57:40 +0000147 PARSE_LANGOPT_IMPORTANT(OpenCL, diag::warn_pch_opencl);
Peter Collingbourne08a53262010-12-01 19:14:57 +0000148 PARSE_LANGOPT_IMPORTANT(CUDA, diag::warn_pch_cuda);
Mike Stump9c276ae2009-12-12 01:27:46 +0000149 PARSE_LANGOPT_BENIGN(CatchUndefined);
John McCallf85e1932011-06-15 23:02:42 +0000150 PARSE_LANGOPT_BENIGN(DefaultFPContract);
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000151 PARSE_LANGOPT_IMPORTANT(ElideConstructors, diag::warn_pch_elide_constructors);
Douglas Gregora0068fc2010-07-09 17:35:33 +0000152 PARSE_LANGOPT_BENIGN(SpellChecking);
John McCallf85e1932011-06-15 23:02:42 +0000153 PARSE_LANGOPT_IMPORTANT(ObjCAutoRefCount, diag::warn_pch_auto_ref_count);
154 PARSE_LANGOPT_BENIGN(ObjCInferRelatedReturnType);
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +0000155#undef PARSE_LANGOPT_IMPORTANT
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000156#undef PARSE_LANGOPT_BENIGN
157
158 return false;
159}
160
Daniel Dunbardc3c0d22009-11-11 00:52:11 +0000161bool PCHValidator::ReadTargetTriple(llvm::StringRef Triple) {
162 if (Triple == PP.getTargetInfo().getTriple().str())
163 return false;
164
165 Reader.Diag(diag::warn_pch_target_triple)
166 << Triple << PP.getTargetInfo().getTriple().str();
167 return true;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000168}
169
Benjamin Kramer54353f42010-11-25 18:29:30 +0000170namespace {
171 struct EmptyStringRef {
172 bool operator ()(llvm::StringRef r) const { return r.empty(); }
173 };
174 struct EmptyBlock {
175 bool operator ()(const PCHPredefinesBlock &r) const {return r.Data.empty();}
176 };
177}
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000178
179static bool EqualConcatenations(llvm::SmallVector<llvm::StringRef, 2> L,
180 PCHPredefinesBlocks R) {
181 // First, sum up the lengths.
182 unsigned LL = 0, RL = 0;
183 for (unsigned I = 0, N = L.size(); I != N; ++I) {
184 LL += L[I].size();
185 }
186 for (unsigned I = 0, N = R.size(); I != N; ++I) {
187 RL += R[I].Data.size();
188 }
189 if (LL != RL)
190 return false;
191 if (LL == 0 && RL == 0)
192 return true;
193
194 // Kick out empty parts, they confuse the algorithm below.
195 L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end());
196 R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end());
197
198 // Do it the hard way. At this point, both vectors must be non-empty.
199 llvm::StringRef LR = L[0], RR = R[0].Data;
200 unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size();
Daniel Dunbarc76c9e02010-07-16 00:00:11 +0000201 (void) RN;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000202 for (;;) {
203 // Compare the current pieces.
204 if (LR.size() == RR.size()) {
205 // If they're the same length, it's pretty easy.
206 if (LR != RR)
207 return false;
208 // Both pieces are done, advance.
209 ++LI;
210 ++RI;
211 // If either string is done, they're both done, since they're the same
212 // length.
213 if (LI == LN) {
214 assert(RI == RN && "Strings not the same length after all?");
215 return true;
216 }
217 LR = L[LI];
218 RR = R[RI].Data;
219 } else if (LR.size() < RR.size()) {
220 // Right piece is longer.
221 if (!RR.startswith(LR))
222 return false;
223 ++LI;
224 assert(LI != LN && "Strings not the same length after all?");
225 RR = RR.substr(LR.size());
226 LR = L[LI];
227 } else {
228 // Left piece is longer.
229 if (!LR.startswith(RR))
230 return false;
231 ++RI;
232 assert(RI != RN && "Strings not the same length after all?");
233 LR = LR.substr(RR.size());
234 RR = R[RI].Data;
235 }
236 }
237}
238
239static std::pair<FileID, llvm::StringRef::size_type>
240FindMacro(const PCHPredefinesBlocks &Buffers, llvm::StringRef MacroDef) {
241 std::pair<FileID, llvm::StringRef::size_type> Res;
242 for (unsigned I = 0, N = Buffers.size(); I != N; ++I) {
243 Res.second = Buffers[I].Data.find(MacroDef);
244 if (Res.second != llvm::StringRef::npos) {
245 Res.first = Buffers[I].BufferID;
246 break;
247 }
248 }
249 return Res;
250}
251
252bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000253 llvm::StringRef OriginalFileName,
Nick Lewycky277a6e72011-02-23 21:16:44 +0000254 std::string &SuggestedPredefines,
255 FileManager &FileMgr) {
Daniel Dunbarc7162932009-11-11 23:58:53 +0000256 // We are in the context of an implicit include, so the predefines buffer will
257 // have a #include entry for the PCH file itself (as normalized by the
258 // preprocessor initialization). Find it and skip over it in the checking
259 // below.
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000260 llvm::SmallString<256> PCHInclude;
261 PCHInclude += "#include \"";
Nick Lewycky277a6e72011-02-23 21:16:44 +0000262 PCHInclude += NormalizeDashIncludePath(OriginalFileName, FileMgr);
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000263 PCHInclude += "\"\n";
264 std::pair<llvm::StringRef,llvm::StringRef> Split =
265 llvm::StringRef(PP.getPredefines()).split(PCHInclude.str());
266 llvm::StringRef Left = Split.first, Right = Split.second;
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000267 if (Left == PP.getPredefines()) {
268 Error("Missing PCH include entry!");
269 return true;
270 }
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000271
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000272 // If the concatenation of all the PCH buffers is equal to the adjusted
273 // command line, we're done.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000274 llvm::SmallVector<llvm::StringRef, 2> CommandLine;
275 CommandLine.push_back(Left);
276 CommandLine.push_back(Right);
277 if (EqualConcatenations(CommandLine, Buffers))
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000278 return false;
279
280 SourceManager &SourceMgr = PP.getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000281
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000282 // The predefines buffers are different. Determine what the differences are,
283 // and whether they require us to reject the PCH file.
Daniel Dunbare6750492009-11-13 16:46:11 +0000284 llvm::SmallVector<llvm::StringRef, 8> PCHLines;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000285 for (unsigned I = 0, N = Buffers.size(); I != N; ++I)
286 Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Daniel Dunbare6750492009-11-13 16:46:11 +0000287
288 llvm::SmallVector<llvm::StringRef, 8> CmdLineLines;
289 Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000290
291 // Pick out implicit #includes after the PCH and don't consider them for
292 // validation; we will insert them into SuggestedPredefines so that the
293 // preprocessor includes them.
294 std::string IncludesAfterPCH;
295 llvm::SmallVector<llvm::StringRef, 8> AfterPCHLines;
296 Right.split(AfterPCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
297 for (unsigned i = 0, e = AfterPCHLines.size(); i != e; ++i) {
298 if (AfterPCHLines[i].startswith("#include ")) {
299 IncludesAfterPCH += AfterPCHLines[i];
300 IncludesAfterPCH += '\n';
301 } else {
302 CmdLineLines.push_back(AfterPCHLines[i]);
303 }
304 }
305
306 // Make sure we add the includes last into SuggestedPredefines before we
307 // exit this function.
308 struct AddIncludesRAII {
309 std::string &SuggestedPredefines;
310 std::string &IncludesAfterPCH;
311
312 AddIncludesRAII(std::string &SuggestedPredefines,
313 std::string &IncludesAfterPCH)
314 : SuggestedPredefines(SuggestedPredefines),
315 IncludesAfterPCH(IncludesAfterPCH) { }
316 ~AddIncludesRAII() {
317 SuggestedPredefines += IncludesAfterPCH;
318 }
319 } AddIncludes(SuggestedPredefines, IncludesAfterPCH);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000320
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000321 // Sort both sets of predefined buffer lines, since we allow some extra
322 // definitions and they may appear at any point in the output.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000323 std::sort(CmdLineLines.begin(), CmdLineLines.end());
324 std::sort(PCHLines.begin(), PCHLines.end());
325
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000326 // Determine which predefines that were used to build the PCH file are missing
327 // from the command line.
328 std::vector<llvm::StringRef> MissingPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000329 std::set_difference(PCHLines.begin(), PCHLines.end(),
330 CmdLineLines.begin(), CmdLineLines.end(),
331 std::back_inserter(MissingPredefines));
332
333 bool MissingDefines = false;
334 bool ConflictingDefines = false;
335 for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) {
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000336 llvm::StringRef Missing = MissingPredefines[I];
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000337 if (Missing.startswith("#include ")) {
338 // An -include was specified when generating the PCH; it is included in
339 // the PCH, just ignore it.
340 continue;
341 }
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000342 if (!Missing.startswith("#define ")) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000343 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
344 return true;
345 }
Mike Stump1eb44332009-09-09 15:08:12 +0000346
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000347 // This is a macro definition. Determine the name of the macro we're
348 // defining.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000349 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000350 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000351 = Missing.find_first_of("( \n\r", StartOfMacroName);
352 assert(EndOfMacroName != std::string::npos &&
353 "Couldn't find the end of the macro name");
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000354 llvm::StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000355
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000356 // Determine whether this macro was given a different definition on the
357 // command line.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000358 std::string MacroDefStart = "#define " + MacroName.str();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000359 std::string::size_type MacroDefLen = MacroDefStart.size();
Daniel Dunbare6750492009-11-13 16:46:11 +0000360 llvm::SmallVector<llvm::StringRef, 8>::iterator ConflictPos
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000361 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
362 MacroDefStart);
363 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000364 if (!ConflictPos->startswith(MacroDefStart)) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000365 // Different macro; we're done.
366 ConflictPos = CmdLineLines.end();
Mike Stump1eb44332009-09-09 15:08:12 +0000367 break;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000368 }
Mike Stump1eb44332009-09-09 15:08:12 +0000369
370 assert(ConflictPos->size() > MacroDefLen &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000371 "Invalid #define in predefines buffer?");
Mike Stump1eb44332009-09-09 15:08:12 +0000372 if ((*ConflictPos)[MacroDefLen] != ' ' &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000373 (*ConflictPos)[MacroDefLen] != '(')
374 continue; // Longer macro name; keep trying.
Mike Stump1eb44332009-09-09 15:08:12 +0000375
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000376 // We found a conflicting macro definition.
377 break;
378 }
Mike Stump1eb44332009-09-09 15:08:12 +0000379
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000380 if (ConflictPos != CmdLineLines.end()) {
381 Reader.Diag(diag::warn_cmdline_conflicting_macro_def)
382 << MacroName;
383
384 // Show the definition of this macro within the PCH file.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000385 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
386 FindMacro(Buffers, Missing);
387 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
388 SourceLocation PCHMissingLoc =
389 SourceMgr.getLocForStartOfFile(MacroLoc.first)
390 .getFileLocWithOffset(MacroLoc.second);
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000391 Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000392
393 ConflictingDefines = true;
394 continue;
395 }
Mike Stump1eb44332009-09-09 15:08:12 +0000396
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000397 // If the macro doesn't conflict, then we'll just pick up the macro
398 // definition from the PCH file. Warn the user that they made a mistake.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000399 if (ConflictingDefines)
400 continue; // Don't complain if there are already conflicting defs
Mike Stump1eb44332009-09-09 15:08:12 +0000401
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000402 if (!MissingDefines) {
403 Reader.Diag(diag::warn_cmdline_missing_macro_defs);
404 MissingDefines = true;
405 }
406
407 // Show the definition of this macro within the PCH file.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000408 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
409 FindMacro(Buffers, Missing);
410 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
411 SourceLocation PCHMissingLoc =
412 SourceMgr.getLocForStartOfFile(MacroLoc.first)
413 .getFileLocWithOffset(MacroLoc.second);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000414 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
415 }
Mike Stump1eb44332009-09-09 15:08:12 +0000416
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000417 if (ConflictingDefines)
418 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000419
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000420 // Determine what predefines were introduced based on command-line
421 // parameters that were not present when building the PCH
422 // file. Extra #defines are okay, so long as the identifiers being
423 // defined were not used within the precompiled header.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000424 std::vector<llvm::StringRef> ExtraPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000425 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
426 PCHLines.begin(), PCHLines.end(),
Mike Stump1eb44332009-09-09 15:08:12 +0000427 std::back_inserter(ExtraPredefines));
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000428 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000429 llvm::StringRef &Extra = ExtraPredefines[I];
430 if (!Extra.startswith("#define ")) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000431 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
432 return true;
433 }
434
435 // This is an extra macro definition. Determine the name of the
436 // macro we're defining.
437 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000438 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000439 = Extra.find_first_of("( \n\r", StartOfMacroName);
440 assert(EndOfMacroName != std::string::npos &&
441 "Couldn't find the end of the macro name");
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000442 llvm::StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000443
444 // Check whether this name was used somewhere in the PCH file. If
445 // so, defining it as a macro could change behavior, so we reject
446 // the PCH file.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000447 if (IdentifierInfo *II = Reader.get(MacroName)) {
Daniel Dunbar4fda42e2009-11-11 00:52:00 +0000448 Reader.Diag(diag::warn_macro_name_used_in_pch) << II;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000449 return true;
450 }
451
452 // Add this definition to the suggested predefines buffer.
453 SuggestedPredefines += Extra;
454 SuggestedPredefines += '\n';
455 }
456
457 // If we get here, it's because the predefines buffer had compatible
458 // contents. Accept the PCH file.
459 return false;
460}
461
Douglas Gregor12fab312010-03-16 16:35:32 +0000462void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
463 unsigned ID) {
464 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
465 ++NumHeaderInfos;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000466}
467
468void PCHValidator::ReadCounter(unsigned Value) {
469 PP.setCounterValue(Value);
470}
471
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000472//===----------------------------------------------------------------------===//
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000473// AST reader implementation
Douglas Gregor668c1a42009-04-21 22:25:48 +0000474//===----------------------------------------------------------------------===//
475
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000476void
Sebastian Redl571db7f2010-08-18 23:56:56 +0000477ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000478 DeserializationListener = Listener;
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000479}
480
Chris Lattner4c6f9522009-04-27 05:14:47 +0000481
Douglas Gregor668c1a42009-04-21 22:25:48 +0000482namespace {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000483class ASTSelectorLookupTrait {
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000484 ASTReader &Reader;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000485
486public:
Sebastian Redl5d050072010-08-04 17:20:04 +0000487 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000488 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +0000489 ObjCMethodList Instance, Factory;
490 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000491
492 typedef Selector external_key_type;
493 typedef external_key_type internal_key_type;
494
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000495 explicit ASTSelectorLookupTrait(ASTReader &Reader) : Reader(Reader) { }
Mike Stump1eb44332009-09-09 15:08:12 +0000496
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000497 static bool EqualKey(const internal_key_type& a,
498 const internal_key_type& b) {
499 return a == b;
500 }
Mike Stump1eb44332009-09-09 15:08:12 +0000501
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000502 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +0000503 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000504 }
Mike Stump1eb44332009-09-09 15:08:12 +0000505
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000506 // This hopefully will just get inlined and removed by the optimizer.
507 static const internal_key_type&
508 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump1eb44332009-09-09 15:08:12 +0000509
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000510 static std::pair<unsigned, unsigned>
511 ReadKeyDataLength(const unsigned char*& d) {
512 using namespace clang::io;
513 unsigned KeyLen = ReadUnalignedLE16(d);
514 unsigned DataLen = ReadUnalignedLE16(d);
515 return std::make_pair(KeyLen, DataLen);
516 }
Mike Stump1eb44332009-09-09 15:08:12 +0000517
Douglas Gregor83941df2009-04-25 17:48:32 +0000518 internal_key_type ReadKey(const unsigned char* d, unsigned) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000519 using namespace clang::io;
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000520 SelectorTable &SelTable = Reader.getContext()->Selectors;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000521 unsigned N = ReadUnalignedLE16(d);
Mike Stump1eb44332009-09-09 15:08:12 +0000522 IdentifierInfo *FirstII
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000523 = Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
524 if (N == 0)
525 return SelTable.getNullarySelector(FirstII);
526 else if (N == 1)
527 return SelTable.getUnarySelector(FirstII);
528
529 llvm::SmallVector<IdentifierInfo *, 16> Args;
530 Args.push_back(FirstII);
531 for (unsigned I = 1; I != N; ++I)
532 Args.push_back(Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)));
533
Douglas Gregor75fdb232009-05-22 22:45:36 +0000534 return SelTable.getSelector(N, Args.data());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000535 }
Mike Stump1eb44332009-09-09 15:08:12 +0000536
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000537 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen) {
538 using namespace clang::io;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000539
540 data_type Result;
541
Sebastian Redl5d050072010-08-04 17:20:04 +0000542 Result.ID = ReadUnalignedLE32(d);
543 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
544 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
545
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000546 // Load instance methods
547 ObjCMethodList *Prev = 0;
548 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000549 ObjCMethodDecl *Method
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000550 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl5d050072010-08-04 17:20:04 +0000551 if (!Result.Instance.Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000552 // This is the first method, which is the easy case.
Sebastian Redl5d050072010-08-04 17:20:04 +0000553 Result.Instance.Method = Method;
554 Prev = &Result.Instance;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000555 continue;
556 }
557
Ted Kremenek298ed872010-02-11 00:53:01 +0000558 ObjCMethodList *Mem =
559 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
560 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000561 Prev = Prev->Next;
562 }
563
564 // Load factory methods
565 Prev = 0;
566 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000567 ObjCMethodDecl *Method
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000568 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl5d050072010-08-04 17:20:04 +0000569 if (!Result.Factory.Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000570 // This is the first method, which is the easy case.
Sebastian Redl5d050072010-08-04 17:20:04 +0000571 Result.Factory.Method = Method;
572 Prev = &Result.Factory;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000573 continue;
574 }
575
Ted Kremenek298ed872010-02-11 00:53:01 +0000576 ObjCMethodList *Mem =
577 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
578 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000579 Prev = Prev->Next;
580 }
581
582 return Result;
583 }
584};
Mike Stump1eb44332009-09-09 15:08:12 +0000585
586} // end anonymous namespace
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000587
588/// \brief The on-disk hash table used for the global method pool.
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000589typedef OnDiskChainedHashTable<ASTSelectorLookupTrait>
590 ASTSelectorLookupTable;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000591
Sebastian Redlc3632732010-10-05 15:59:54 +0000592namespace clang {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000593class ASTIdentifierLookupTrait {
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000594 ASTReader &Reader;
Sebastian Redlc3632732010-10-05 15:59:54 +0000595 ASTReader::PerFileData &F;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000596
597 // If we know the IdentifierInfo in advance, it is here and we will
598 // not build a new one. Used when deserializing information about an
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000599 // identifier that was constructed before the AST file was read.
Douglas Gregor668c1a42009-04-21 22:25:48 +0000600 IdentifierInfo *KnownII;
601
602public:
603 typedef IdentifierInfo * data_type;
604
605 typedef const std::pair<const char*, unsigned> external_key_type;
606
607 typedef external_key_type internal_key_type;
608
Sebastian Redlc3632732010-10-05 15:59:54 +0000609 ASTIdentifierLookupTrait(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redld27d3fc2010-07-21 22:31:37 +0000610 IdentifierInfo *II = 0)
Sebastian Redlc3632732010-10-05 15:59:54 +0000611 : Reader(Reader), F(F), KnownII(II) { }
Mike Stump1eb44332009-09-09 15:08:12 +0000612
Douglas Gregor668c1a42009-04-21 22:25:48 +0000613 static bool EqualKey(const internal_key_type& a,
614 const internal_key_type& b) {
615 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
616 : false;
617 }
Mike Stump1eb44332009-09-09 15:08:12 +0000618
Douglas Gregor668c1a42009-04-21 22:25:48 +0000619 static unsigned ComputeHash(const internal_key_type& a) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000620 return llvm::HashString(llvm::StringRef(a.first, a.second));
Douglas Gregor668c1a42009-04-21 22:25:48 +0000621 }
Mike Stump1eb44332009-09-09 15:08:12 +0000622
Douglas Gregor668c1a42009-04-21 22:25:48 +0000623 // This hopefully will just get inlined and removed by the optimizer.
624 static const internal_key_type&
625 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump1eb44332009-09-09 15:08:12 +0000626
Douglas Gregor95f42922010-10-14 22:11:03 +0000627 // This hopefully will just get inlined and removed by the optimizer.
628 static const external_key_type&
629 GetExternalKey(const internal_key_type& x) { return x; }
630
Douglas Gregor668c1a42009-04-21 22:25:48 +0000631 static std::pair<unsigned, unsigned>
632 ReadKeyDataLength(const unsigned char*& d) {
633 using namespace clang::io;
Douglas Gregor5f8e3302009-04-25 20:26:24 +0000634 unsigned DataLen = ReadUnalignedLE16(d);
Douglas Gregord6595a42009-04-25 21:04:17 +0000635 unsigned KeyLen = ReadUnalignedLE16(d);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000636 return std::make_pair(KeyLen, DataLen);
637 }
Mike Stump1eb44332009-09-09 15:08:12 +0000638
Douglas Gregor668c1a42009-04-21 22:25:48 +0000639 static std::pair<const char*, unsigned>
640 ReadKey(const unsigned char* d, unsigned n) {
641 assert(n >= 2 && d[n-1] == '\0');
642 return std::make_pair((const char*) d, n-1);
643 }
Mike Stump1eb44332009-09-09 15:08:12 +0000644
645 IdentifierInfo *ReadData(const internal_key_type& k,
Douglas Gregor668c1a42009-04-21 22:25:48 +0000646 const unsigned char* d,
647 unsigned DataLen) {
648 using namespace clang::io;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000649 IdentID ID = ReadUnalignedLE32(d);
Douglas Gregora92193e2009-04-28 21:18:29 +0000650 bool IsInteresting = ID & 0x01;
651
652 // Wipe out the "is interesting" bit.
653 ID = ID >> 1;
654
655 if (!IsInteresting) {
Sebastian Redl083abdf2010-07-27 23:01:28 +0000656 // For uninteresting identifiers, just build the IdentifierInfo
Douglas Gregora92193e2009-04-28 21:18:29 +0000657 // and associate it with the persistent ID.
658 IdentifierInfo *II = KnownII;
659 if (!II)
Jay Foad65aa6882011-06-21 15:13:30 +0000660 II = &Reader.getIdentifierTable().getOwn(llvm::StringRef(k.first,
661 k.second));
Douglas Gregora92193e2009-04-28 21:18:29 +0000662 Reader.SetIdentifierInfo(ID, II);
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000663 II->setIsFromAST();
Douglas Gregora92193e2009-04-28 21:18:29 +0000664 return II;
665 }
666
Douglas Gregor5998da52009-04-28 21:32:13 +0000667 unsigned Bits = ReadUnalignedLE16(d);
Douglas Gregor2deaea32009-04-22 18:49:13 +0000668 bool CPlusPlusOperatorKeyword = Bits & 0x01;
669 Bits >>= 1;
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000670 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
671 Bits >>= 1;
Douglas Gregor2deaea32009-04-22 18:49:13 +0000672 bool Poisoned = Bits & 0x01;
673 Bits >>= 1;
674 bool ExtensionToken = Bits & 0x01;
675 Bits >>= 1;
676 bool hasMacroDefinition = Bits & 0x01;
677 Bits >>= 1;
678 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
679 Bits >>= 10;
Mike Stump1eb44332009-09-09 15:08:12 +0000680
Douglas Gregor2deaea32009-04-22 18:49:13 +0000681 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregor5998da52009-04-28 21:32:13 +0000682 DataLen -= 6;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000683
684 // Build the IdentifierInfo itself and link the identifier ID with
685 // the new IdentifierInfo.
686 IdentifierInfo *II = KnownII;
687 if (!II)
Jay Foad65aa6882011-06-21 15:13:30 +0000688 II = &Reader.getIdentifierTable().getOwn(llvm::StringRef(k.first,
689 k.second));
Douglas Gregor668c1a42009-04-21 22:25:48 +0000690 Reader.SetIdentifierInfo(ID, II);
691
Douglas Gregor2deaea32009-04-22 18:49:13 +0000692 // Set or check the various bits in the IdentifierInfo structure.
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000693 // Token IDs are read-only.
694 if (HasRevertedTokenIDToIdentifier)
695 II->RevertTokenIDToIdentifier();
Douglas Gregor2deaea32009-04-22 18:49:13 +0000696 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
Mike Stump1eb44332009-09-09 15:08:12 +0000697 assert(II->isExtensionToken() == ExtensionToken &&
Douglas Gregor2deaea32009-04-22 18:49:13 +0000698 "Incorrect extension token flag");
699 (void)ExtensionToken;
700 II->setIsPoisoned(Poisoned);
701 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
702 "Incorrect C++ operator keyword flag");
703 (void)CPlusPlusOperatorKeyword;
704
Douglas Gregor37e26842009-04-21 23:56:24 +0000705 // If this identifier is a macro, deserialize the macro
706 // definition.
707 if (hasMacroDefinition) {
Douglas Gregor5998da52009-04-28 21:32:13 +0000708 uint32_t Offset = ReadUnalignedLE32(d);
Douglas Gregor295a2a62010-10-30 00:23:06 +0000709 Reader.SetIdentifierIsMacro(II, F, Offset);
Douglas Gregor5998da52009-04-28 21:32:13 +0000710 DataLen -= 4;
Douglas Gregor37e26842009-04-21 23:56:24 +0000711 }
Douglas Gregor668c1a42009-04-21 22:25:48 +0000712
713 // Read all of the declarations visible at global scope with this
714 // name.
Chris Lattner6bf690f2009-04-27 22:17:41 +0000715 if (Reader.getContext() == 0) return II;
Douglas Gregord89275b2009-07-06 18:54:52 +0000716 if (DataLen > 0) {
717 llvm::SmallVector<uint32_t, 4> DeclIDs;
718 for (; DataLen > 0; DataLen -= 4)
719 DeclIDs.push_back(ReadUnalignedLE32(d));
720 Reader.SetGloballyVisibleDecls(II, DeclIDs);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000721 }
Mike Stump1eb44332009-09-09 15:08:12 +0000722
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000723 II->setIsFromAST();
Douglas Gregor668c1a42009-04-21 22:25:48 +0000724 return II;
725 }
726};
Mike Stump1eb44332009-09-09 15:08:12 +0000727
728} // end anonymous namespace
Douglas Gregor668c1a42009-04-21 22:25:48 +0000729
730/// \brief The on-disk hash table used to contain information about
731/// all of the identifiers in the program.
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000732typedef OnDiskChainedHashTable<ASTIdentifierLookupTrait>
733 ASTIdentifierLookupTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000734
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000735namespace {
736class ASTDeclContextNameLookupTrait {
737 ASTReader &Reader;
738
739public:
740 /// \brief Pair of begin/end iterators for DeclIDs.
741 typedef std::pair<DeclID *, DeclID *> data_type;
742
743 /// \brief Special internal key for declaration names.
744 /// The hash table creates keys for comparison; we do not create
745 /// a DeclarationName for the internal key to avoid deserializing types.
746 struct DeclNameKey {
747 DeclarationName::NameKind Kind;
748 uint64_t Data;
749 DeclNameKey() : Kind((DeclarationName::NameKind)0), Data(0) { }
750 };
751
752 typedef DeclarationName external_key_type;
753 typedef DeclNameKey internal_key_type;
754
755 explicit ASTDeclContextNameLookupTrait(ASTReader &Reader) : Reader(Reader) { }
756
757 static bool EqualKey(const internal_key_type& a,
758 const internal_key_type& b) {
759 return a.Kind == b.Kind && a.Data == b.Data;
760 }
761
762 unsigned ComputeHash(const DeclNameKey &Key) const {
763 llvm::FoldingSetNodeID ID;
764 ID.AddInteger(Key.Kind);
765
766 switch (Key.Kind) {
767 case DeclarationName::Identifier:
768 case DeclarationName::CXXLiteralOperatorName:
769 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
770 break;
771 case DeclarationName::ObjCZeroArgSelector:
772 case DeclarationName::ObjCOneArgSelector:
773 case DeclarationName::ObjCMultiArgSelector:
774 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
775 break;
776 case DeclarationName::CXXConstructorName:
777 case DeclarationName::CXXDestructorName:
778 case DeclarationName::CXXConversionFunctionName:
779 ID.AddInteger((TypeID)Key.Data);
780 break;
781 case DeclarationName::CXXOperatorName:
782 ID.AddInteger((OverloadedOperatorKind)Key.Data);
783 break;
784 case DeclarationName::CXXUsingDirective:
785 break;
786 }
787
788 return ID.ComputeHash();
789 }
790
791 internal_key_type GetInternalKey(const external_key_type& Name) const {
792 DeclNameKey Key;
793 Key.Kind = Name.getNameKind();
794 switch (Name.getNameKind()) {
795 case DeclarationName::Identifier:
796 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
797 break;
798 case DeclarationName::ObjCZeroArgSelector:
799 case DeclarationName::ObjCOneArgSelector:
800 case DeclarationName::ObjCMultiArgSelector:
801 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
802 break;
803 case DeclarationName::CXXConstructorName:
804 case DeclarationName::CXXDestructorName:
805 case DeclarationName::CXXConversionFunctionName:
806 Key.Data = Reader.GetTypeID(Name.getCXXNameType());
807 break;
808 case DeclarationName::CXXOperatorName:
809 Key.Data = Name.getCXXOverloadedOperator();
810 break;
811 case DeclarationName::CXXLiteralOperatorName:
812 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
813 break;
814 case DeclarationName::CXXUsingDirective:
815 break;
816 }
Michael J. Spencer20249a12010-10-21 03:16:25 +0000817
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000818 return Key;
819 }
820
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +0000821 external_key_type GetExternalKey(const internal_key_type& Key) const {
822 ASTContext *Context = Reader.getContext();
823 switch (Key.Kind) {
824 case DeclarationName::Identifier:
825 return DeclarationName((IdentifierInfo*)Key.Data);
826
827 case DeclarationName::ObjCZeroArgSelector:
828 case DeclarationName::ObjCOneArgSelector:
829 case DeclarationName::ObjCMultiArgSelector:
830 return DeclarationName(Selector(Key.Data));
831
832 case DeclarationName::CXXConstructorName:
833 return Context->DeclarationNames.getCXXConstructorName(
834 Context->getCanonicalType(Reader.GetType(Key.Data)));
835
836 case DeclarationName::CXXDestructorName:
837 return Context->DeclarationNames.getCXXDestructorName(
838 Context->getCanonicalType(Reader.GetType(Key.Data)));
839
840 case DeclarationName::CXXConversionFunctionName:
841 return Context->DeclarationNames.getCXXConversionFunctionName(
842 Context->getCanonicalType(Reader.GetType(Key.Data)));
843
844 case DeclarationName::CXXOperatorName:
845 return Context->DeclarationNames.getCXXOperatorName(
846 (OverloadedOperatorKind)Key.Data);
847
848 case DeclarationName::CXXLiteralOperatorName:
849 return Context->DeclarationNames.getCXXLiteralOperatorName(
850 (IdentifierInfo*)Key.Data);
851
852 case DeclarationName::CXXUsingDirective:
853 return DeclarationName::getUsingDirectiveName();
854 }
855
856 llvm_unreachable("Invalid Name Kind ?");
857 }
858
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000859 static std::pair<unsigned, unsigned>
860 ReadKeyDataLength(const unsigned char*& d) {
861 using namespace clang::io;
862 unsigned KeyLen = ReadUnalignedLE16(d);
863 unsigned DataLen = ReadUnalignedLE16(d);
864 return std::make_pair(KeyLen, DataLen);
865 }
866
867 internal_key_type ReadKey(const unsigned char* d, unsigned) {
868 using namespace clang::io;
869
870 DeclNameKey Key;
871 Key.Kind = (DeclarationName::NameKind)*d++;
872 switch (Key.Kind) {
873 case DeclarationName::Identifier:
874 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
875 break;
876 case DeclarationName::ObjCZeroArgSelector:
877 case DeclarationName::ObjCOneArgSelector:
878 case DeclarationName::ObjCMultiArgSelector:
Michael J. Spencer20249a12010-10-21 03:16:25 +0000879 Key.Data =
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000880 (uint64_t)Reader.DecodeSelector(ReadUnalignedLE32(d)).getAsOpaquePtr();
881 break;
882 case DeclarationName::CXXConstructorName:
883 case DeclarationName::CXXDestructorName:
884 case DeclarationName::CXXConversionFunctionName:
885 Key.Data = ReadUnalignedLE32(d); // TypeID
886 break;
887 case DeclarationName::CXXOperatorName:
888 Key.Data = *d++; // OverloadedOperatorKind
889 break;
890 case DeclarationName::CXXLiteralOperatorName:
891 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
892 break;
893 case DeclarationName::CXXUsingDirective:
894 break;
895 }
Michael J. Spencer20249a12010-10-21 03:16:25 +0000896
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000897 return Key;
898 }
899
900 data_type ReadData(internal_key_type, const unsigned char* d,
901 unsigned DataLen) {
902 using namespace clang::io;
903 unsigned NumDecls = ReadUnalignedLE16(d);
904 DeclID *Start = (DeclID *)d;
905 return std::make_pair(Start, Start + NumDecls);
906 }
907};
908
909} // end anonymous namespace
910
911/// \brief The on-disk hash table used for the DeclContext's Name lookup table.
912typedef OnDiskChainedHashTable<ASTDeclContextNameLookupTrait>
913 ASTDeclContextNameLookupTable;
914
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000915bool ASTReader::ReadDeclContextStorage(llvm::BitstreamCursor &Cursor,
916 const std::pair<uint64_t, uint64_t> &Offsets,
917 DeclContextInfo &Info) {
918 SavedStreamPosition SavedPosition(Cursor);
919 // First the lexical decls.
920 if (Offsets.first != 0) {
921 Cursor.JumpToBit(Offsets.first);
922
923 RecordData Record;
924 const char *Blob;
925 unsigned BlobLen;
926 unsigned Code = Cursor.ReadCode();
927 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
928 if (RecCode != DECL_CONTEXT_LEXICAL) {
929 Error("Expected lexical block");
930 return true;
931 }
932
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000933 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob);
934 Info.NumLexicalDecls = BlobLen / sizeof(KindDeclIDPair);
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000935 } else {
936 Info.LexicalDecls = 0;
937 Info.NumLexicalDecls = 0;
938 }
939
940 // Now the lookup table.
941 if (Offsets.second != 0) {
942 Cursor.JumpToBit(Offsets.second);
943
944 RecordData Record;
945 const char *Blob;
946 unsigned BlobLen;
947 unsigned Code = Cursor.ReadCode();
948 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
949 if (RecCode != DECL_CONTEXT_VISIBLE) {
950 Error("Expected visible lookup table block");
951 return true;
952 }
953 Info.NameLookupTableData
954 = ASTDeclContextNameLookupTable::Create(
955 (const unsigned char *)Blob + Record[0],
956 (const unsigned char *)Blob,
957 ASTDeclContextNameLookupTrait(*this));
Sebastian Redl0ea8f7f2010-08-24 00:50:00 +0000958 } else {
959 Info.NameLookupTableData = 0;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000960 }
961
962 return false;
963}
964
Argyrios Kyrtzidis8d8f2c22011-04-25 22:23:56 +0000965void ASTReader::Error(llvm::StringRef Msg) {
966 Error(diag::err_fe_pch_malformed, Msg);
967}
968
969void ASTReader::Error(unsigned DiagID,
970 llvm::StringRef Arg1, llvm::StringRef Arg2) {
971 if (Diags.isDiagnosticInFlight())
972 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
973 else
974 Diag(DiagID) << Arg1 << Arg2;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000975}
976
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000977/// \brief Tell the AST listener about the predefines buffers in the chain.
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000978bool ASTReader::CheckPredefinesBuffers() {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000979 if (Listener)
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000980 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000981 ActualOriginalFileName,
Nick Lewycky277a6e72011-02-23 21:16:44 +0000982 SuggestedPredefines,
983 FileMgr);
Douglas Gregore721f952009-04-28 18:58:38 +0000984 return false;
Douglas Gregore1d918e2009-04-10 23:10:45 +0000985}
986
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000987//===----------------------------------------------------------------------===//
988// Source Manager Deserialization
989//===----------------------------------------------------------------------===//
990
Douglas Gregorbd945002009-04-13 16:31:14 +0000991/// \brief Read the line table in the source manager block.
Sebastian Redlc3632732010-10-05 15:59:54 +0000992/// \returns true if there was an error.
993bool ASTReader::ParseLineTable(PerFileData &F,
994 llvm::SmallVectorImpl<uint64_t> &Record) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000995 unsigned Idx = 0;
996 LineTableInfo &LineTable = SourceMgr.getLineTable();
997
998 // Parse the file names
Douglas Gregorff0a9872009-04-13 17:12:42 +0000999 std::map<int, int> FileIDs;
1000 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregorbd945002009-04-13 16:31:14 +00001001 // Extract the file name
1002 unsigned FilenameLen = Record[Idx++];
1003 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
1004 Idx += FilenameLen;
Douglas Gregore650c8c2009-07-07 00:12:59 +00001005 MaybeAddSystemRootToFilename(Filename);
Jay Foad65aa6882011-06-21 15:13:30 +00001006 FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
Douglas Gregorbd945002009-04-13 16:31:14 +00001007 }
1008
1009 // Parse the line entries
1010 std::vector<LineEntry> Entries;
1011 while (Idx < Record.size()) {
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +00001012 int FID = Record[Idx++];
Douglas Gregorbd945002009-04-13 16:31:14 +00001013
1014 // Extract the line entries
1015 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +00001016 assert(NumEntries && "Numentries is 00000");
Douglas Gregorbd945002009-04-13 16:31:14 +00001017 Entries.clear();
1018 Entries.reserve(NumEntries);
1019 for (unsigned I = 0; I != NumEntries; ++I) {
1020 unsigned FileOffset = Record[Idx++];
1021 unsigned LineNo = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +00001022 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump1eb44332009-09-09 15:08:12 +00001023 SrcMgr::CharacteristicKind FileKind
Douglas Gregorbd945002009-04-13 16:31:14 +00001024 = (SrcMgr::CharacteristicKind)Record[Idx++];
1025 unsigned IncludeOffset = Record[Idx++];
1026 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1027 FileKind, IncludeOffset));
1028 }
1029 LineTable.AddEntry(FID, Entries);
1030 }
1031
1032 return false;
1033}
1034
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001035namespace {
1036
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001037class ASTStatData {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001038public:
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001039 const ino_t ino;
1040 const dev_t dev;
1041 const mode_t mode;
1042 const time_t mtime;
1043 const off_t size;
Mike Stump1eb44332009-09-09 15:08:12 +00001044
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001045 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Chris Lattner74e976b2010-11-23 19:28:12 +00001046 : ino(i), dev(d), mode(mo), mtime(m), size(s) {}
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001047};
1048
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001049class ASTStatLookupTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001050 public:
1051 typedef const char *external_key_type;
1052 typedef const char *internal_key_type;
1053
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001054 typedef ASTStatData data_type;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001055
1056 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001057 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001058 }
1059
1060 static internal_key_type GetInternalKey(const char *path) { return path; }
1061
1062 static bool EqualKey(internal_key_type a, internal_key_type b) {
1063 return strcmp(a, b) == 0;
1064 }
1065
1066 static std::pair<unsigned, unsigned>
1067 ReadKeyDataLength(const unsigned char*& d) {
1068 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1069 unsigned DataLen = (unsigned) *d++;
1070 return std::make_pair(KeyLen + 1, DataLen);
1071 }
1072
1073 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
1074 return (const char *)d;
1075 }
1076
1077 static data_type ReadData(const internal_key_type, const unsigned char *d,
1078 unsigned /*DataLen*/) {
1079 using namespace clang::io;
1080
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001081 ino_t ino = (ino_t) ReadUnalignedLE32(d);
1082 dev_t dev = (dev_t) ReadUnalignedLE32(d);
1083 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump1eb44332009-09-09 15:08:12 +00001084 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001085 off_t size = (off_t) ReadUnalignedLE64(d);
1086 return data_type(ino, dev, mode, mtime, size);
1087 }
1088};
1089
1090/// \brief stat() cache for precompiled headers.
1091///
1092/// This cache is very similar to the stat cache used by pretokenized
1093/// headers.
Chris Lattner10e286a2010-11-23 19:19:34 +00001094class ASTStatCache : public FileSystemStatCache {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001095 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001096 CacheTy *Cache;
1097
1098 unsigned &NumStatHits, &NumStatMisses;
Mike Stump1eb44332009-09-09 15:08:12 +00001099public:
Chris Lattner74e976b2010-11-23 19:28:12 +00001100 ASTStatCache(const unsigned char *Buckets, const unsigned char *Base,
1101 unsigned &NumStatHits, unsigned &NumStatMisses)
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001102 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
1103 Cache = CacheTy::Create(Buckets, Base);
1104 }
1105
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001106 ~ASTStatCache() { delete Cache; }
Mike Stump1eb44332009-09-09 15:08:12 +00001107
Chris Lattner898a0612010-11-23 21:17:56 +00001108 LookupResult getStat(const char *Path, struct stat &StatBuf,
1109 int *FileDescriptor) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001110 // Do the lookup for the file's data in the AST file.
Chris Lattner10e286a2010-11-23 19:19:34 +00001111 CacheTy::iterator I = Cache->find(Path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001112
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001113 // If we don't get a hit in the AST file just forward to 'stat'.
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001114 if (I == Cache->end()) {
1115 ++NumStatMisses;
Chris Lattner898a0612010-11-23 21:17:56 +00001116 return statChained(Path, StatBuf, FileDescriptor);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001117 }
Mike Stump1eb44332009-09-09 15:08:12 +00001118
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001119 ++NumStatHits;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001120 ASTStatData Data = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001121
Chris Lattner10e286a2010-11-23 19:19:34 +00001122 StatBuf.st_ino = Data.ino;
1123 StatBuf.st_dev = Data.dev;
1124 StatBuf.st_mtime = Data.mtime;
1125 StatBuf.st_mode = Data.mode;
1126 StatBuf.st_size = Data.size;
Chris Lattnerd6f61112010-11-23 20:05:15 +00001127 return CacheExists;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001128 }
1129};
1130} // end anonymous namespace
1131
1132
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001133/// \brief Read a source manager block
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001134ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(PerFileData &F) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001135 using namespace SrcMgr;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001136
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001137 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001138
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001139 // Set the source-location entry cursor to the current position in
1140 // the stream. This cursor will be used to read the contents of the
1141 // source manager block initially, and then lazily read
1142 // source-location entries as needed.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001143 SLocEntryCursor = F.Stream;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001144
1145 // The stream itself is going to skip over the source manager block.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001146 if (F.Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001147 Error("malformed block record in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001148 return Failure;
1149 }
1150
1151 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001152 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001153 Error("malformed source manager block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001154 return Failure;
1155 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001156
Douglas Gregor14f79002009-04-10 03:52:48 +00001157 RecordData Record;
1158 while (true) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001159 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregor14f79002009-04-10 03:52:48 +00001160 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001161 if (SLocEntryCursor.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001162 Error("error at end of Source Manager block in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001163 return Failure;
1164 }
Douglas Gregore1d918e2009-04-10 23:10:45 +00001165 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +00001166 }
Mike Stump1eb44332009-09-09 15:08:12 +00001167
Douglas Gregor14f79002009-04-10 03:52:48 +00001168 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1169 // No known subblocks, always skip them.
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001170 SLocEntryCursor.ReadSubBlockID();
1171 if (SLocEntryCursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001172 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001173 return Failure;
1174 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001175 continue;
1176 }
Mike Stump1eb44332009-09-09 15:08:12 +00001177
Douglas Gregor14f79002009-04-10 03:52:48 +00001178 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001179 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregor14f79002009-04-10 03:52:48 +00001180 continue;
1181 }
Mike Stump1eb44332009-09-09 15:08:12 +00001182
Douglas Gregor14f79002009-04-10 03:52:48 +00001183 // Read a record.
1184 const char *BlobStart;
1185 unsigned BlobLen;
1186 Record.clear();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001187 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001188 default: // Default behavior: ignore.
1189 break;
1190
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001191 case SM_LINE_TABLE:
Sebastian Redlc3632732010-10-05 15:59:54 +00001192 if (ParseLineTable(F, Record))
Douglas Gregorbd945002009-04-13 16:31:14 +00001193 return Failure;
Chris Lattner2c78b872009-04-14 23:22:57 +00001194 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001195
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001196 case SM_SLOC_FILE_ENTRY:
1197 case SM_SLOC_BUFFER_ENTRY:
1198 case SM_SLOC_INSTANTIATION_ENTRY:
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001199 // Once we hit one of the source location entries, we're done.
1200 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +00001201 }
1202 }
1203}
1204
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001205/// \brief If a header file is not found at the path that we expect it to be
1206/// and the PCH file was moved from its original location, try to resolve the
1207/// file by assuming that header+PCH were moved together and the header is in
1208/// the same place relative to the PCH.
1209static std::string
1210resolveFileRelativeToOriginalDir(const std::string &Filename,
1211 const std::string &OriginalDir,
1212 const std::string &CurrDir) {
1213 assert(OriginalDir != CurrDir &&
1214 "No point trying to resolve the file if the PCH dir didn't change");
1215 using namespace llvm::sys;
1216 llvm::SmallString<128> filePath(Filename);
1217 fs::make_absolute(filePath);
1218 assert(path::is_absolute(OriginalDir));
1219 llvm::SmallString<128> currPCHPath(CurrDir);
1220
1221 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1222 fileDirE = path::end(path::parent_path(filePath));
1223 path::const_iterator origDirI = path::begin(OriginalDir),
1224 origDirE = path::end(OriginalDir);
1225 // Skip the common path components from filePath and OriginalDir.
1226 while (fileDirI != fileDirE && origDirI != origDirE &&
1227 *fileDirI == *origDirI) {
1228 ++fileDirI;
1229 ++origDirI;
1230 }
1231 for (; origDirI != origDirE; ++origDirI)
1232 path::append(currPCHPath, "..");
1233 path::append(currPCHPath, fileDirI, fileDirE);
1234 path::append(currPCHPath, path::filename(Filename));
1235 return currPCHPath.str();
1236}
1237
Sebastian Redl190faf72010-07-20 21:50:20 +00001238/// \brief Get a cursor that's correctly positioned for reading the source
1239/// location entry with the given ID.
Sebastian Redlc3632732010-10-05 15:59:54 +00001240ASTReader::PerFileData *ASTReader::SLocCursorForID(unsigned ID) {
Sebastian Redl190faf72010-07-20 21:50:20 +00001241 assert(ID != 0 && ID <= TotalNumSLocEntries &&
1242 "SLocCursorForID should only be called for real IDs.");
1243
1244 ID -= 1;
1245 PerFileData *F = 0;
1246 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1247 F = Chain[N - I - 1];
1248 if (ID < F->LocalNumSLocEntries)
1249 break;
1250 ID -= F->LocalNumSLocEntries;
1251 }
1252 assert(F && F->LocalNumSLocEntries > ID && "Chain corrupted");
1253
1254 F->SLocEntryCursor.JumpToBit(F->SLocOffsets[ID]);
Sebastian Redlc3632732010-10-05 15:59:54 +00001255 return F;
Sebastian Redl190faf72010-07-20 21:50:20 +00001256}
1257
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001258/// \brief Read in the source location entry with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001259ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(unsigned ID) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001260 if (ID == 0)
1261 return Success;
1262
1263 if (ID > TotalNumSLocEntries) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001264 Error("source location entry ID out-of-range for AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001265 return Failure;
1266 }
1267
Sebastian Redlc3632732010-10-05 15:59:54 +00001268 PerFileData *F = SLocCursorForID(ID);
1269 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001270
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001271 ++NumSLocEntriesRead;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001272 unsigned Code = SLocEntryCursor.ReadCode();
1273 if (Code == llvm::bitc::END_BLOCK ||
1274 Code == llvm::bitc::ENTER_SUBBLOCK ||
1275 Code == llvm::bitc::DEFINE_ABBREV) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001276 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001277 return Failure;
1278 }
1279
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001280 RecordData Record;
1281 const char *BlobStart;
1282 unsigned BlobLen;
1283 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1284 default:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001285 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001286 return Failure;
1287
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001288 case SM_SLOC_FILE_ENTRY: {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001289 std::string Filename(BlobStart, BlobStart + BlobLen);
1290 MaybeAddSystemRootToFilename(Filename);
Chris Lattner39b49bc2010-11-23 08:35:12 +00001291 const FileEntry *File = FileMgr.getFile(Filename);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001292 if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() &&
1293 OriginalDir != CurrentDir) {
1294 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1295 OriginalDir,
1296 CurrentDir);
1297 if (!resolved.empty())
1298 File = FileMgr.getFile(resolved);
1299 }
Axel Naumann04331162011-01-27 10:55:51 +00001300 if (File == 0)
1301 File = FileMgr.getVirtualFile(Filename, (off_t)Record[4],
1302 (time_t)Record[5]);
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001303 if (File == 0) {
1304 std::string ErrorStr = "could not find file '";
Douglas Gregore650c8c2009-07-07 00:12:59 +00001305 ErrorStr += Filename;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001306 ErrorStr += "' referenced by AST file";
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001307 Error(ErrorStr.c_str());
1308 return Failure;
1309 }
Mike Stump1eb44332009-09-09 15:08:12 +00001310
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001311 if (Record.size() < 6) {
Ted Kremenek1857f622010-03-18 21:23:05 +00001312 Error("source location entry is incorrect");
1313 return Failure;
1314 }
1315
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001316 if (!DisableValidation &&
1317 ((off_t)Record[4] != File->getSize()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001318#if !defined(LLVM_ON_WIN32)
1319 // In our regression testing, the Windows file system seems to
1320 // have inconsistent modification times that sometimes
1321 // erroneously trigger this error-handling path.
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001322 || (time_t)Record[5] != File->getModificationTime()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001323#endif
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001324 )) {
Argyrios Kyrtzidis8d8f2c22011-04-25 22:23:56 +00001325 Error(diag::err_fe_pch_file_modified, Filename);
Douglas Gregor2d52be52010-03-21 22:49:54 +00001326 return Failure;
1327 }
1328
Chris Lattner75dfb652010-11-23 09:19:42 +00001329 FileID FID = SourceMgr.createFileID(File, ReadSourceLocation(*F, Record[1]),
1330 (SrcMgr::CharacteristicKind)Record[2],
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001331 ID, Record[0]);
1332 if (Record[3])
1333 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile())
1334 .setHasLineDirectives();
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001335
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001336 break;
1337 }
1338
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001339 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001340 const char *Name = BlobStart;
1341 unsigned Offset = Record[0];
1342 unsigned Code = SLocEntryCursor.ReadCode();
1343 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001344 unsigned RecCode
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001345 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001346
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001347 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001348 Error("AST record has invalid code");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001349 return Failure;
1350 }
1351
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001352 llvm::MemoryBuffer *Buffer
Chris Lattnera0a270c2010-04-05 22:42:27 +00001353 = llvm::MemoryBuffer::getMemBuffer(llvm::StringRef(BlobStart, BlobLen - 1),
1354 Name);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001355 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID, Offset);
Mike Stump1eb44332009-09-09 15:08:12 +00001356
Douglas Gregor92b059e2009-04-28 20:33:11 +00001357 if (strcmp(Name, "<built-in>") == 0) {
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +00001358 PCHPredefinesBlock Block = {
1359 BufferID,
1360 llvm::StringRef(BlobStart, BlobLen - 1)
1361 };
1362 PCHPredefinesBuffers.push_back(Block);
Douglas Gregor92b059e2009-04-28 20:33:11 +00001363 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001364
1365 break;
1366 }
1367
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001368 case SM_SLOC_INSTANTIATION_ENTRY: {
Sebastian Redlc3632732010-10-05 15:59:54 +00001369 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001370 SourceMgr.createInstantiationLoc(SpellingLoc,
Sebastian Redlc3632732010-10-05 15:59:54 +00001371 ReadSourceLocation(*F, Record[2]),
1372 ReadSourceLocation(*F, Record[3]),
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001373 Record[4],
1374 ID,
1375 Record[0]);
1376 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001377 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001378 }
1379
1380 return Success;
1381}
1382
Chris Lattner6367f6d2009-04-27 01:05:14 +00001383/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1384/// specified cursor. Read the abbreviations that are at the top of the block
1385/// and then leave the cursor pointing into the block.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001386bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattner6367f6d2009-04-27 01:05:14 +00001387 unsigned BlockID) {
1388 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001389 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001390 return Failure;
1391 }
Mike Stump1eb44332009-09-09 15:08:12 +00001392
Chris Lattner6367f6d2009-04-27 01:05:14 +00001393 while (true) {
Douglas Gregorecdcb882010-10-20 22:00:55 +00001394 uint64_t Offset = Cursor.GetCurrentBitNo();
Chris Lattner6367f6d2009-04-27 01:05:14 +00001395 unsigned Code = Cursor.ReadCode();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001396
Chris Lattner6367f6d2009-04-27 01:05:14 +00001397 // We expect all abbrevs to be at the start of the block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001398 if (Code != llvm::bitc::DEFINE_ABBREV) {
1399 Cursor.JumpToBit(Offset);
Chris Lattner6367f6d2009-04-27 01:05:14 +00001400 return false;
Douglas Gregorecdcb882010-10-20 22:00:55 +00001401 }
Chris Lattner6367f6d2009-04-27 01:05:14 +00001402 Cursor.ReadAbbrevRecord();
1403 }
1404}
1405
Douglas Gregor89d99802010-11-30 06:16:57 +00001406PreprocessedEntity *ASTReader::ReadMacroRecord(PerFileData &F, uint64_t Offset) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001407 assert(PP && "Forgot to set Preprocessor ?");
Douglas Gregorecdcb882010-10-20 22:00:55 +00001408 llvm::BitstreamCursor &Stream = F.MacroCursor;
Mike Stump1eb44332009-09-09 15:08:12 +00001409
Douglas Gregor37e26842009-04-21 23:56:24 +00001410 // Keep track of where we are in the stream, then jump back there
1411 // after reading this macro.
1412 SavedStreamPosition SavedPosition(Stream);
1413
1414 Stream.JumpToBit(Offset);
1415 RecordData Record;
1416 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1417 MacroInfo *Macro = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001418
Douglas Gregor37e26842009-04-21 23:56:24 +00001419 while (true) {
1420 unsigned Code = Stream.ReadCode();
1421 switch (Code) {
1422 case llvm::bitc::END_BLOCK:
Douglas Gregor89d99802010-11-30 06:16:57 +00001423 return 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001424
1425 case llvm::bitc::ENTER_SUBBLOCK:
1426 // No known subblocks, always skip them.
1427 Stream.ReadSubBlockID();
1428 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001429 Error("malformed block record in AST file");
Douglas Gregor89d99802010-11-30 06:16:57 +00001430 return 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001431 }
1432 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001433
Douglas Gregor37e26842009-04-21 23:56:24 +00001434 case llvm::bitc::DEFINE_ABBREV:
1435 Stream.ReadAbbrevRecord();
1436 continue;
1437 default: break;
1438 }
1439
1440 // Read a record.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001441 const char *BlobStart = 0;
1442 unsigned BlobLen = 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001443 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001444 PreprocessorRecordTypes RecType =
Michael J. Spencer20249a12010-10-21 03:16:25 +00001445 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart,
Douglas Gregorecdcb882010-10-20 22:00:55 +00001446 BlobLen);
Douglas Gregor37e26842009-04-21 23:56:24 +00001447 switch (RecType) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001448 case PP_MACRO_OBJECT_LIKE:
1449 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001450 // If we already have a macro, that means that we've hit the end
1451 // of the definition of the macro we were looking for. We're
1452 // done.
1453 if (Macro)
Douglas Gregor89d99802010-11-30 06:16:57 +00001454 return 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001455
1456 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1457 if (II == 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001458 Error("macro must have a name in AST file");
Douglas Gregor89d99802010-11-30 06:16:57 +00001459 return 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001460 }
Sebastian Redlc3632732010-10-05 15:59:54 +00001461 SourceLocation Loc = ReadSourceLocation(F, Record[1]);
Douglas Gregor37e26842009-04-21 23:56:24 +00001462 bool isUsed = Record[2];
Mike Stump1eb44332009-09-09 15:08:12 +00001463
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001464 MacroInfo *MI = PP->AllocateMacroInfo(Loc);
Douglas Gregor37e26842009-04-21 23:56:24 +00001465 MI->setIsUsed(isUsed);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001466 MI->setIsFromAST();
Mike Stump1eb44332009-09-09 15:08:12 +00001467
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001468 unsigned NextIndex = 3;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001469 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregor37e26842009-04-21 23:56:24 +00001470 // Decode function-like macro info.
1471 bool isC99VarArgs = Record[3];
1472 bool isGNUVarArgs = Record[4];
1473 MacroArgs.clear();
1474 unsigned NumArgs = Record[5];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001475 NextIndex = 6 + NumArgs;
Douglas Gregor37e26842009-04-21 23:56:24 +00001476 for (unsigned i = 0; i != NumArgs; ++i)
1477 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1478
1479 // Install function-like macro info.
1480 MI->setIsFunctionLike();
1481 if (isC99VarArgs) MI->setIsC99Varargs();
1482 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor75fdb232009-05-22 22:45:36 +00001483 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001484 PP->getPreprocessorAllocator());
Douglas Gregor37e26842009-04-21 23:56:24 +00001485 }
1486
1487 // Finally, install the macro.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001488 PP->setMacroInfo(II, MI);
Douglas Gregor37e26842009-04-21 23:56:24 +00001489
1490 // Remember that we saw this macro last so that we add the tokens that
1491 // form its body to it.
1492 Macro = MI;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001493
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001494 if (NextIndex + 1 == Record.size() && PP->getPreprocessingRecord()) {
1495 // We have a macro definition. Load it now.
1496 PP->getPreprocessingRecord()->RegisterMacroDefinition(Macro,
1497 getMacroDefinition(Record[NextIndex]));
1498 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001499
Douglas Gregor37e26842009-04-21 23:56:24 +00001500 ++NumMacrosRead;
1501 break;
1502 }
Mike Stump1eb44332009-09-09 15:08:12 +00001503
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001504 case PP_TOKEN: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001505 // If we see a TOKEN before a PP_MACRO_*, then the file is
1506 // erroneous, just pretend we didn't see this.
1507 if (Macro == 0) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001508
Douglas Gregor37e26842009-04-21 23:56:24 +00001509 Token Tok;
1510 Tok.startToken();
Sebastian Redlc3632732010-10-05 15:59:54 +00001511 Tok.setLocation(ReadSourceLocation(F, Record[0]));
Douglas Gregor37e26842009-04-21 23:56:24 +00001512 Tok.setLength(Record[1]);
1513 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1514 Tok.setIdentifierInfo(II);
1515 Tok.setKind((tok::TokenKind)Record[3]);
1516 Tok.setFlag((Token::TokenFlags)Record[4]);
1517 Macro->AddTokenToBody(Tok);
1518 break;
1519 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001520 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001521 }
Douglas Gregor89d99802010-11-30 06:16:57 +00001522
1523 return 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001524}
1525
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001526PreprocessedEntity *ASTReader::LoadPreprocessedEntity(PerFileData &F) {
1527 assert(PP && "Forgot to set Preprocessor ?");
1528 unsigned Code = F.PreprocessorDetailCursor.ReadCode();
1529 switch (Code) {
1530 case llvm::bitc::END_BLOCK:
1531 return 0;
1532
1533 case llvm::bitc::ENTER_SUBBLOCK:
1534 Error("unexpected subblock record in preprocessor detail block");
1535 return 0;
1536
1537 case llvm::bitc::DEFINE_ABBREV:
1538 Error("unexpected abbrevation record in preprocessor detail block");
1539 return 0;
1540
1541 default:
1542 break;
1543 }
1544
1545 if (!PP->getPreprocessingRecord()) {
1546 Error("no preprocessing record");
1547 return 0;
1548 }
1549
1550 // Read the record.
1551 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1552 const char *BlobStart = 0;
1553 unsigned BlobLen = 0;
1554 RecordData Record;
1555 PreprocessorDetailRecordTypes RecType =
1556 (PreprocessorDetailRecordTypes)F.PreprocessorDetailCursor.ReadRecord(
1557 Code, Record, BlobStart, BlobLen);
1558 switch (RecType) {
1559 case PPD_MACRO_INSTANTIATION: {
1560 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1561 return PE;
1562
Chandler Carruth9e5bb852011-07-14 08:20:46 +00001563 MacroExpansion *ME =
1564 new (PPRec) MacroExpansion(DecodeIdentifierInfo(Record[3]),
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001565 SourceRange(ReadSourceLocation(F, Record[1]),
1566 ReadSourceLocation(F, Record[2])),
Chandler Carruth9e5bb852011-07-14 08:20:46 +00001567 getMacroDefinition(Record[4]));
1568 PPRec.SetPreallocatedEntity(Record[0], ME);
1569 return ME;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001570 }
1571
1572 case PPD_MACRO_DEFINITION: {
1573 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1574 return PE;
1575
1576 if (Record[1] > MacroDefinitionsLoaded.size()) {
1577 Error("out-of-bounds macro definition record");
1578 return 0;
1579 }
1580
1581 // Decode the identifier info and then check again; if the macro is
1582 // still defined and associated with the identifier,
1583 IdentifierInfo *II = DecodeIdentifierInfo(Record[4]);
1584 if (!MacroDefinitionsLoaded[Record[1] - 1]) {
1585 MacroDefinition *MD
1586 = new (PPRec) MacroDefinition(II,
1587 ReadSourceLocation(F, Record[5]),
1588 SourceRange(
1589 ReadSourceLocation(F, Record[2]),
1590 ReadSourceLocation(F, Record[3])));
1591
1592 PPRec.SetPreallocatedEntity(Record[0], MD);
1593 MacroDefinitionsLoaded[Record[1] - 1] = MD;
1594
1595 if (DeserializationListener)
1596 DeserializationListener->MacroDefinitionRead(Record[1], MD);
1597 }
1598
1599 return MacroDefinitionsLoaded[Record[1] - 1];
1600 }
1601
1602 case PPD_INCLUSION_DIRECTIVE: {
1603 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1604 return PE;
1605
1606 const char *FullFileNameStart = BlobStart + Record[3];
1607 const FileEntry *File
1608 = PP->getFileManager().getFile(llvm::StringRef(FullFileNameStart,
1609 BlobLen - Record[3]));
1610
1611 // FIXME: Stable encoding
1612 InclusionDirective::InclusionKind Kind
1613 = static_cast<InclusionDirective::InclusionKind>(Record[5]);
1614 InclusionDirective *ID
1615 = new (PPRec) InclusionDirective(PPRec, Kind,
1616 llvm::StringRef(BlobStart, Record[3]),
1617 Record[4],
1618 File,
1619 SourceRange(ReadSourceLocation(F, Record[1]),
1620 ReadSourceLocation(F, Record[2])));
1621 PPRec.SetPreallocatedEntity(Record[0], ID);
1622 return ID;
1623 }
1624 }
1625
1626 Error("invalid offset in preprocessor detail block");
1627 return 0;
1628}
1629
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001630namespace {
1631 /// \brief Trait class used to search the on-disk hash table containing all of
1632 /// the header search information.
1633 ///
1634 /// The on-disk hash table contains a mapping from each header path to
1635 /// information about that header (how many times it has been included, its
1636 /// controlling macro, etc.). Note that we actually hash based on the
1637 /// filename, and support "deep" comparisons of file names based on current
1638 /// inode numbers, so that the search can cope with non-normalized path names
1639 /// and symlinks.
1640 class HeaderFileInfoTrait {
1641 const char *SearchPath;
1642 struct stat SearchPathStatBuf;
1643 llvm::Optional<int> SearchPathStatResult;
1644
1645 int StatSimpleCache(const char *Path, struct stat *StatBuf) {
1646 if (Path == SearchPath) {
1647 if (!SearchPathStatResult)
1648 SearchPathStatResult = stat(Path, &SearchPathStatBuf);
1649
1650 *StatBuf = SearchPathStatBuf;
1651 return *SearchPathStatResult;
1652 }
1653
1654 return stat(Path, StatBuf);
1655 }
1656
1657 public:
1658 typedef const char *external_key_type;
1659 typedef const char *internal_key_type;
1660
1661 typedef HeaderFileInfo data_type;
1662
1663 HeaderFileInfoTrait(const char *SearchPath = 0) : SearchPath(SearchPath) { }
1664
1665 static unsigned ComputeHash(const char *path) {
1666 return llvm::HashString(llvm::sys::path::filename(path));
1667 }
1668
1669 static internal_key_type GetInternalKey(const char *path) { return path; }
1670
1671 bool EqualKey(internal_key_type a, internal_key_type b) {
1672 if (strcmp(a, b) == 0)
1673 return true;
1674
1675 if (llvm::sys::path::filename(a) != llvm::sys::path::filename(b))
1676 return false;
1677
1678 // The file names match, but the path names don't. stat() the files to
1679 // see if they are the same.
1680 struct stat StatBufA, StatBufB;
1681 if (StatSimpleCache(a, &StatBufA) || StatSimpleCache(b, &StatBufB))
1682 return false;
1683
1684 return StatBufA.st_ino == StatBufB.st_ino;
1685 }
1686
1687 static std::pair<unsigned, unsigned>
1688 ReadKeyDataLength(const unsigned char*& d) {
1689 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1690 unsigned DataLen = (unsigned) *d++;
1691 return std::make_pair(KeyLen + 1, DataLen);
1692 }
1693
1694 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
1695 return (const char *)d;
1696 }
1697
1698 static data_type ReadData(const internal_key_type, const unsigned char *d,
1699 unsigned DataLen) {
1700 const unsigned char *End = d + DataLen;
1701 using namespace clang::io;
1702 HeaderFileInfo HFI;
1703 unsigned Flags = *d++;
Douglas Gregordd3e5542011-05-04 00:14:37 +00001704 HFI.isImport = (Flags >> 4) & 0x01;
1705 HFI.isPragmaOnce = (Flags >> 3) & 0x01;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001706 HFI.DirInfo = (Flags >> 1) & 0x03;
1707 HFI.Resolved = Flags & 0x01;
1708 HFI.NumIncludes = ReadUnalignedLE16(d);
1709 HFI.ControllingMacroID = ReadUnalignedLE32(d);
1710 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1711 (void)End;
1712
1713 // This HeaderFileInfo was externally loaded.
1714 HFI.External = true;
1715 return HFI;
1716 }
1717 };
1718}
1719
1720/// \brief The on-disk hash table used for the global method pool.
1721typedef OnDiskChainedHashTable<HeaderFileInfoTrait>
1722 HeaderFileInfoLookupTable;
1723
Douglas Gregor295a2a62010-10-30 00:23:06 +00001724void ASTReader::SetIdentifierIsMacro(IdentifierInfo *II, PerFileData &F,
1725 uint64_t Offset) {
1726 // Note that this identifier has a macro definition.
1727 II->setHasMacroDefinition(true);
1728
1729 // Adjust the offset based on our position in the chain.
1730 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1731 if (Chain[I] == &F)
1732 break;
1733
1734 Offset += Chain[I]->SizeInBits;
1735 }
1736
1737 UnreadMacroRecordOffsets[II] = Offset;
1738}
1739
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001740void ASTReader::ReadDefinedMacros() {
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001741 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redlc3632732010-10-05 15:59:54 +00001742 PerFileData &F = *Chain[N - I - 1];
1743 llvm::BitstreamCursor &MacroCursor = F.MacroCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001744
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001745 // If there was no preprocessor block, skip this file.
1746 if (!MacroCursor.getBitStreamReader())
1747 continue;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001748
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001749 llvm::BitstreamCursor Cursor = MacroCursor;
Douglas Gregorecdcb882010-10-20 22:00:55 +00001750 Cursor.JumpToBit(F.MacroStartOffset);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001751
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001752 RecordData Record;
1753 while (true) {
1754 unsigned Code = Cursor.ReadCode();
Douglas Gregorecdcb882010-10-20 22:00:55 +00001755 if (Code == llvm::bitc::END_BLOCK)
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001756 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001757
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001758 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1759 // No known subblocks, always skip them.
1760 Cursor.ReadSubBlockID();
1761 if (Cursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001762 Error("malformed block record in AST file");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001763 return;
1764 }
1765 continue;
1766 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001767
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001768 if (Code == llvm::bitc::DEFINE_ABBREV) {
1769 Cursor.ReadAbbrevRecord();
1770 continue;
1771 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001772
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001773 // Read a record.
1774 const char *BlobStart;
1775 unsigned BlobLen;
1776 Record.clear();
1777 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1778 default: // Default behavior: ignore.
1779 break;
Douglas Gregor88a35862010-01-04 19:18:44 +00001780
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001781 case PP_MACRO_OBJECT_LIKE:
1782 case PP_MACRO_FUNCTION_LIKE:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001783 DecodeIdentifierInfo(Record[0]);
1784 break;
1785
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001786 case PP_TOKEN:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001787 // Ignore tokens.
1788 break;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001789 }
Douglas Gregor88a35862010-01-04 19:18:44 +00001790 }
1791 }
Douglas Gregor295a2a62010-10-30 00:23:06 +00001792
1793 // Drain the unread macro-record offsets map.
1794 while (!UnreadMacroRecordOffsets.empty())
1795 LoadMacroDefinition(UnreadMacroRecordOffsets.begin());
1796}
1797
1798void ASTReader::LoadMacroDefinition(
1799 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos) {
1800 assert(Pos != UnreadMacroRecordOffsets.end() && "Unknown macro definition");
1801 PerFileData *F = 0;
1802 uint64_t Offset = Pos->second;
1803 UnreadMacroRecordOffsets.erase(Pos);
1804
1805 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1806 if (Offset < Chain[I]->SizeInBits) {
1807 F = Chain[I];
1808 break;
1809 }
1810
1811 Offset -= Chain[I]->SizeInBits;
1812 }
1813 if (!F) {
1814 Error("Malformed macro record offset");
1815 return;
1816 }
1817
1818 ReadMacroRecord(*F, Offset);
1819}
1820
1821void ASTReader::LoadMacroDefinition(IdentifierInfo *II) {
1822 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos
1823 = UnreadMacroRecordOffsets.find(II);
1824 LoadMacroDefinition(Pos);
Douglas Gregor88a35862010-01-04 19:18:44 +00001825}
1826
Sebastian Redlf73c93f2010-09-15 19:54:06 +00001827MacroDefinition *ASTReader::getMacroDefinition(MacroID ID) {
Douglas Gregor77424bc2010-10-02 19:29:26 +00001828 if (ID == 0 || ID > MacroDefinitionsLoaded.size())
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001829 return 0;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001830
Douglas Gregor77424bc2010-10-02 19:29:26 +00001831 if (!MacroDefinitionsLoaded[ID - 1]) {
1832 unsigned Index = ID - 1;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001833 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1834 PerFileData &F = *Chain[N - I - 1];
1835 if (Index < F.LocalNumMacroDefinitions) {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001836 SavedStreamPosition SavedPosition(F.PreprocessorDetailCursor);
1837 F.PreprocessorDetailCursor.JumpToBit(F.MacroDefinitionOffsets[Index]);
1838 LoadPreprocessedEntity(F);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001839 break;
1840 }
1841 Index -= F.LocalNumMacroDefinitions;
1842 }
Douglas Gregor77424bc2010-10-02 19:29:26 +00001843 assert(MacroDefinitionsLoaded[ID - 1] && "Broken chain");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001844 }
1845
Douglas Gregor77424bc2010-10-02 19:29:26 +00001846 return MacroDefinitionsLoaded[ID - 1];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001847}
1848
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00001849const FileEntry *ASTReader::getFileEntry(llvm::StringRef filenameStrRef) {
1850 std::string Filename = filenameStrRef;
1851 MaybeAddSystemRootToFilename(Filename);
1852 const FileEntry *File = FileMgr.getFile(Filename);
1853 if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() &&
1854 OriginalDir != CurrentDir) {
1855 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1856 OriginalDir,
1857 CurrentDir);
1858 if (!resolved.empty())
1859 File = FileMgr.getFile(resolved);
1860 }
1861
1862 return File;
1863}
1864
Douglas Gregore650c8c2009-07-07 00:12:59 +00001865/// \brief If we are loading a relocatable PCH file, and the filename is
1866/// not an absolute path, add the system root to the beginning of the file
1867/// name.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001868void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001869 // If this is not a relocatable PCH file, there's nothing to do.
1870 if (!RelocatablePCH)
1871 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001872
Michael J. Spencer256053b2010-12-17 21:22:22 +00001873 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
Douglas Gregore650c8c2009-07-07 00:12:59 +00001874 return;
1875
Douglas Gregore650c8c2009-07-07 00:12:59 +00001876 if (isysroot == 0) {
1877 // If no system root was given, default to '/'
1878 Filename.insert(Filename.begin(), '/');
1879 return;
1880 }
Mike Stump1eb44332009-09-09 15:08:12 +00001881
Douglas Gregore650c8c2009-07-07 00:12:59 +00001882 unsigned Length = strlen(isysroot);
1883 if (isysroot[Length - 1] != '/')
1884 Filename.insert(Filename.begin(), '/');
Mike Stump1eb44332009-09-09 15:08:12 +00001885
Douglas Gregore650c8c2009-07-07 00:12:59 +00001886 Filename.insert(Filename.begin(), isysroot, isysroot + Length);
1887}
1888
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001889ASTReader::ASTReadResult
Sebastian Redl571db7f2010-08-18 23:56:56 +00001890ASTReader::ReadASTBlock(PerFileData &F) {
Sebastian Redl9137a522010-07-16 17:50:48 +00001891 llvm::BitstreamCursor &Stream = F.Stream;
1892
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001893 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001894 Error("malformed block record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001895 return Failure;
1896 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001897
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001898 // Read all of the records and blocks for the ASt file.
Douglas Gregor8038d512009-04-10 17:25:41 +00001899 RecordData Record;
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001900 bool First = true;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001901 while (!Stream.AtEndOfStream()) {
1902 unsigned Code = Stream.ReadCode();
1903 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001904 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001905 Error("error at end of module block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001906 return Failure;
1907 }
Chris Lattner7356a312009-04-11 21:15:38 +00001908
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001909 return Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001910 }
1911
1912 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1913 switch (Stream.ReadSubBlockID()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001914 case DECLTYPES_BLOCK_ID:
Chris Lattner6367f6d2009-04-27 01:05:14 +00001915 // We lazily load the decls block, but we want to set up the
1916 // DeclsCursor cursor to point into it. Clone our current bitcode
1917 // cursor to it, enter the block and read the abbrevs in that block.
1918 // With the main cursor, we just skip over it.
Sebastian Redl9137a522010-07-16 17:50:48 +00001919 F.DeclsCursor = Stream;
Chris Lattner6367f6d2009-04-27 01:05:14 +00001920 if (Stream.SkipBlock() || // Skip with the main cursor.
1921 // Read the abbrevs.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001922 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001923 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001924 return Failure;
1925 }
1926 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001927
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00001928 case DECL_UPDATES_BLOCK_ID:
1929 if (Stream.SkipBlock()) {
1930 Error("malformed block record in AST file");
1931 return Failure;
1932 }
1933 break;
1934
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001935 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl9137a522010-07-16 17:50:48 +00001936 F.MacroCursor = Stream;
Douglas Gregor88a35862010-01-04 19:18:44 +00001937 if (PP)
1938 PP->setExternalSource(this);
1939
Douglas Gregorecdcb882010-10-20 22:00:55 +00001940 if (Stream.SkipBlock() ||
1941 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001942 Error("malformed block record in AST file");
Chris Lattner7356a312009-04-11 21:15:38 +00001943 return Failure;
1944 }
Douglas Gregorecdcb882010-10-20 22:00:55 +00001945 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
Chris Lattner7356a312009-04-11 21:15:38 +00001946 break;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00001947
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001948 case PREPROCESSOR_DETAIL_BLOCK_ID:
1949 F.PreprocessorDetailCursor = Stream;
1950 if (Stream.SkipBlock() ||
1951 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
1952 PREPROCESSOR_DETAIL_BLOCK_ID)) {
1953 Error("malformed preprocessor detail record in AST file");
1954 return Failure;
1955 }
1956 F.PreprocessorDetailStartOffset
1957 = F.PreprocessorDetailCursor.GetCurrentBitNo();
1958 break;
1959
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001960 case SOURCE_MANAGER_BLOCK_ID:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001961 switch (ReadSourceManagerBlock(F)) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00001962 case Success:
1963 break;
1964
1965 case Failure:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001966 Error("malformed source manager block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001967 return Failure;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001968
1969 case IgnorePCH:
1970 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001971 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001972 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001973 }
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001974 First = false;
Douglas Gregor8038d512009-04-10 17:25:41 +00001975 continue;
1976 }
1977
1978 if (Code == llvm::bitc::DEFINE_ABBREV) {
1979 Stream.ReadAbbrevRecord();
1980 continue;
1981 }
1982
1983 // Read and process a record.
1984 Record.clear();
Douglas Gregor2bec0412009-04-10 21:16:55 +00001985 const char *BlobStart = 0;
1986 unsigned BlobLen = 0;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001987 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00001988 &BlobStart, &BlobLen)) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001989 default: // Default behavior: ignore.
1990 break;
1991
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001992 case METADATA: {
1993 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1994 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001995 : diag::warn_pch_version_too_new);
1996 return IgnorePCH;
1997 }
1998
1999 RelocatablePCH = Record[4];
2000 if (Listener) {
2001 std::string TargetTriple(BlobStart, BlobLen);
2002 if (Listener->ReadTargetTriple(TargetTriple))
2003 return IgnorePCH;
2004 }
2005 break;
2006 }
2007
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002008 case CHAINED_METADATA: {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002009 if (!First) {
2010 Error("CHAINED_METADATA is not first record in block");
2011 return Failure;
2012 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002013 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2014 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002015 : diag::warn_pch_version_too_new);
2016 return IgnorePCH;
2017 }
2018
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002019 // Load the chained file, which is always a PCH file.
2020 switch(ReadASTCore(llvm::StringRef(BlobStart, BlobLen), PCH)) {
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002021 case Failure: return Failure;
2022 // If we have to ignore the dependency, we'll have to ignore this too.
2023 case IgnorePCH: return IgnorePCH;
2024 case Success: break;
2025 }
2026 break;
2027 }
2028
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002029 case TYPE_OFFSET:
Sebastian Redl12d6da02010-07-19 22:06:55 +00002030 if (F.LocalNumTypes != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002031 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002032 return Failure;
2033 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00002034 F.TypeOffsets = (const uint32_t *)BlobStart;
2035 F.LocalNumTypes = Record[0];
Douglas Gregor8038d512009-04-10 17:25:41 +00002036 break;
2037
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002038 case DECL_OFFSET:
Sebastian Redl12d6da02010-07-19 22:06:55 +00002039 if (F.LocalNumDecls != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002040 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002041 return Failure;
2042 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00002043 F.DeclOffsets = (const uint32_t *)BlobStart;
2044 F.LocalNumDecls = Record[0];
Douglas Gregor8038d512009-04-10 17:25:41 +00002045 break;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002046
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002047 case TU_UPDATE_LEXICAL: {
Sebastian Redld692af72010-07-27 18:24:41 +00002048 DeclContextInfo Info = {
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002049 /* No visible information */ 0,
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002050 reinterpret_cast<const KindDeclIDPair *>(BlobStart),
2051 BlobLen / sizeof(KindDeclIDPair)
Sebastian Redld692af72010-07-27 18:24:41 +00002052 };
Douglas Gregor3747ee72010-10-01 01:18:02 +00002053 DeclContextOffsets[Context ? Context->getTranslationUnitDecl() : 0]
2054 .push_back(Info);
Sebastian Redld692af72010-07-27 18:24:41 +00002055 break;
2056 }
2057
Sebastian Redle1dde812010-08-24 00:50:04 +00002058 case UPDATE_VISIBLE: {
2059 serialization::DeclID ID = Record[0];
2060 void *Table = ASTDeclContextNameLookupTable::Create(
2061 (const unsigned char *)BlobStart + Record[1],
2062 (const unsigned char *)BlobStart,
2063 ASTDeclContextNameLookupTrait(*this));
Douglas Gregor3747ee72010-10-01 01:18:02 +00002064 if (ID == 1 && Context) { // Is it the TU?
Sebastian Redle1dde812010-08-24 00:50:04 +00002065 DeclContextInfo Info = {
2066 Table, /* No lexical inforamtion */ 0, 0
2067 };
2068 DeclContextOffsets[Context->getTranslationUnitDecl()].push_back(Info);
2069 } else
2070 PendingVisibleUpdates[ID].push_back(Table);
2071 break;
2072 }
2073
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002074 case REDECLS_UPDATE_LATEST: {
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00002075 assert(Record.size() % 2 == 0 && "Expected pairs of DeclIDs");
2076 for (unsigned i = 0, e = Record.size(); i < e; i += 2) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002077 DeclID First = Record[i], Latest = Record[i+1];
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00002078 assert((FirstLatestDeclIDs.find(First) == FirstLatestDeclIDs.end() ||
2079 Latest > FirstLatestDeclIDs[First]) &&
2080 "The new latest is supposed to come after the previous latest");
2081 FirstLatestDeclIDs[First] = Latest;
2082 }
2083 break;
2084 }
2085
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002086 case LANGUAGE_OPTIONS:
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00002087 if (ParseLanguageOptions(Record) && !DisableValidation)
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002088 return IgnorePCH;
2089 break;
Douglas Gregor2bec0412009-04-10 21:16:55 +00002090
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002091 case IDENTIFIER_TABLE:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002092 F.IdentifierTableData = BlobStart;
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002093 if (Record[0]) {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002094 F.IdentifierLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002095 = ASTIdentifierLookupTable::Create(
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002096 (const unsigned char *)F.IdentifierTableData + Record[0],
2097 (const unsigned char *)F.IdentifierTableData,
Sebastian Redlc3632732010-10-05 15:59:54 +00002098 ASTIdentifierLookupTrait(*this, F));
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002099 if (PP)
2100 PP->getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002101 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002102 break;
2103
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002104 case IDENTIFIER_OFFSET:
Sebastian Redl2da08f92010-07-19 22:28:42 +00002105 if (F.LocalNumIdentifiers != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002106 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00002107 return Failure;
2108 }
Sebastian Redl2da08f92010-07-19 22:28:42 +00002109 F.IdentifierOffsets = (const uint32_t *)BlobStart;
2110 F.LocalNumIdentifiers = Record[0];
Douglas Gregorafaf3082009-04-11 00:14:32 +00002111 break;
Douglas Gregorfdd01722009-04-14 00:24:19 +00002112
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002113 case EXTERNAL_DEFINITIONS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002114 // Optimization for the first block.
2115 if (ExternalDefinitions.empty())
2116 ExternalDefinitions.swap(Record);
2117 else
2118 ExternalDefinitions.insert(ExternalDefinitions.end(),
2119 Record.begin(), Record.end());
Douglas Gregorfdd01722009-04-14 00:24:19 +00002120 break;
Douglas Gregor3e1af842009-04-17 22:13:46 +00002121
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002122 case SPECIAL_TYPES:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002123 // Optimization for the first block
2124 if (SpecialTypes.empty())
2125 SpecialTypes.swap(Record);
2126 else
2127 SpecialTypes.insert(SpecialTypes.end(), Record.begin(), Record.end());
Douglas Gregorad1de002009-04-18 05:55:16 +00002128 break;
2129
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002130 case STATISTICS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002131 TotalNumStatements += Record[0];
2132 TotalNumMacros += Record[1];
2133 TotalLexicalDeclContexts += Record[2];
2134 TotalVisibleDeclContexts += Record[3];
Douglas Gregor3e1af842009-04-17 22:13:46 +00002135 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002136
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002137 case UNUSED_FILESCOPED_DECLS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002138 // Optimization for the first block.
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002139 if (UnusedFileScopedDecls.empty())
2140 UnusedFileScopedDecls.swap(Record);
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002141 else
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002142 UnusedFileScopedDecls.insert(UnusedFileScopedDecls.end(),
2143 Record.begin(), Record.end());
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002144 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002145
Sean Huntebcbe1d2011-05-04 23:29:54 +00002146 case DELEGATING_CTORS:
2147 // Optimization for the first block.
2148 if (DelegatingCtorDecls.empty())
2149 DelegatingCtorDecls.swap(Record);
2150 else
2151 DelegatingCtorDecls.insert(DelegatingCtorDecls.end(),
2152 Record.begin(), Record.end());
2153 break;
2154
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002155 case WEAK_UNDECLARED_IDENTIFIERS:
Sebastian Redl40566802010-08-05 18:21:25 +00002156 // Later blocks overwrite earlier ones.
2157 WeakUndeclaredIdentifiers.swap(Record);
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002158 break;
2159
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002160 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002161 // Optimization for the first block.
2162 if (LocallyScopedExternalDecls.empty())
2163 LocallyScopedExternalDecls.swap(Record);
2164 else
2165 LocallyScopedExternalDecls.insert(LocallyScopedExternalDecls.end(),
2166 Record.begin(), Record.end());
Douglas Gregor14c22f22009-04-22 22:18:58 +00002167 break;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002168
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002169 case SELECTOR_OFFSETS:
Sebastian Redl059612d2010-08-03 21:58:15 +00002170 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redl725cd962010-08-04 20:40:17 +00002171 F.LocalNumSelectors = Record[0];
Douglas Gregor83941df2009-04-25 17:48:32 +00002172 break;
2173
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002174 case METHOD_POOL:
Sebastian Redl725cd962010-08-04 20:40:17 +00002175 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor83941df2009-04-25 17:48:32 +00002176 if (Record[0])
Sebastian Redl725cd962010-08-04 20:40:17 +00002177 F.SelectorLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002178 = ASTSelectorLookupTable::Create(
Sebastian Redl725cd962010-08-04 20:40:17 +00002179 F.SelectorLookupTableData + Record[0],
2180 F.SelectorLookupTableData,
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002181 ASTSelectorLookupTrait(*this));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002182 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002183 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002184
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002185 case REFERENCED_SELECTOR_POOL:
Sebastian Redlc3632732010-10-05 15:59:54 +00002186 F.ReferencedSelectorsData.swap(Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002187 break;
2188
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002189 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002190 if (!Record.empty() && Listener)
2191 Listener->ReadCounter(Record[0]);
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002192 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002193
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002194 case SOURCE_LOCATION_OFFSETS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002195 F.SLocOffsets = (const uint32_t *)BlobStart;
2196 F.LocalNumSLocEntries = Record[0];
Sebastian Redl8db9fae2010-09-22 20:19:08 +00002197 F.LocalSLocSize = Record[1];
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002198 break;
2199
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00002200 case FILE_SOURCE_LOCATION_OFFSETS:
2201 F.SLocFileOffsets = (const uint32_t *)BlobStart;
2202 F.LocalNumSLocFileEntries = Record[0];
2203 break;
2204
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002205 case SOURCE_LOCATION_PRELOADS:
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002206 if (PreloadSLocEntries.empty())
2207 PreloadSLocEntries.swap(Record);
2208 else
2209 PreloadSLocEntries.insert(PreloadSLocEntries.end(),
2210 Record.begin(), Record.end());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002211 break;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002212
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002213 case STAT_CACHE: {
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00002214 if (!DisableStatCache) {
2215 ASTStatCache *MyStatCache =
2216 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
2217 (const unsigned char *)BlobStart,
2218 NumStatHits, NumStatMisses);
2219 FileMgr.addStatCache(MyStatCache);
2220 F.StatCache = MyStatCache;
2221 }
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002222 break;
Douglas Gregor52e71082009-10-16 18:18:30 +00002223 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002224
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002225 case EXT_VECTOR_DECLS:
Sebastian Redla9f23682010-07-28 21:38:49 +00002226 // Optimization for the first block.
2227 if (ExtVectorDecls.empty())
2228 ExtVectorDecls.swap(Record);
2229 else
2230 ExtVectorDecls.insert(ExtVectorDecls.end(),
2231 Record.begin(), Record.end());
Douglas Gregorb81c1702009-04-27 20:06:05 +00002232 break;
2233
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002234 case VTABLE_USES:
Sebastian Redl40566802010-08-05 18:21:25 +00002235 // Later tables overwrite earlier ones.
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002236 VTableUses.swap(Record);
2237 break;
2238
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002239 case DYNAMIC_CLASSES:
Sebastian Redl40566802010-08-05 18:21:25 +00002240 // Optimization for the first block.
2241 if (DynamicClasses.empty())
2242 DynamicClasses.swap(Record);
2243 else
2244 DynamicClasses.insert(DynamicClasses.end(),
2245 Record.begin(), Record.end());
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002246 break;
2247
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002248 case PENDING_IMPLICIT_INSTANTIATIONS:
Sebastian Redlc3632732010-10-05 15:59:54 +00002249 F.PendingInstantiations.swap(Record);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002250 break;
2251
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002252 case SEMA_DECL_REFS:
Sebastian Redl40566802010-08-05 18:21:25 +00002253 // Later tables overwrite earlier ones.
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002254 SemaDeclRefs.swap(Record);
2255 break;
2256
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002257 case ORIGINAL_FILE_NAME:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002258 // The primary AST will be the last to get here, so it will be the one
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002259 // that's used.
Daniel Dunbar7b5a1212009-11-11 05:29:04 +00002260 ActualOriginalFileName.assign(BlobStart, BlobLen);
2261 OriginalFileName = ActualOriginalFileName;
Douglas Gregore650c8c2009-07-07 00:12:59 +00002262 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregorb64c1932009-05-12 01:31:05 +00002263 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002264
Douglas Gregor31d375f2011-05-06 21:43:30 +00002265 case ORIGINAL_FILE_ID:
2266 OriginalFileID = FileID::get(Record[0]);
2267 break;
2268
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002269 case ORIGINAL_PCH_DIR:
2270 // The primary AST will be the last to get here, so it will be the one
2271 // that's used.
2272 OriginalDir.assign(BlobStart, BlobLen);
2273 break;
2274
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002275 case VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek974be4d2010-02-12 23:31:14 +00002276 const std::string &CurBranch = getClangFullRepositoryVersion();
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002277 llvm::StringRef ASTBranch(BlobStart, BlobLen);
2278 if (llvm::StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2279 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregor445e23e2009-10-05 21:07:28 +00002280 return IgnorePCH;
2281 }
2282 break;
2283 }
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002284
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002285 case MACRO_DEFINITION_OFFSETS:
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002286 F.MacroDefinitionOffsets = (const uint32_t *)BlobStart;
2287 F.NumPreallocatedPreprocessingEntities = Record[0];
2288 F.LocalNumMacroDefinitions = Record[1];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002289 break;
Sebastian Redl0b17c612010-08-13 00:28:03 +00002290
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002291 case DECL_UPDATE_OFFSETS: {
2292 if (Record.size() % 2 != 0) {
2293 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2294 return Failure;
2295 }
2296 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
2297 DeclUpdateOffsets[static_cast<DeclID>(Record[I])]
2298 .push_back(std::make_pair(&F, Record[I+1]));
2299 break;
2300 }
2301
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002302 case DECL_REPLACEMENTS: {
Sebastian Redl0b17c612010-08-13 00:28:03 +00002303 if (Record.size() % 2 != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002304 Error("invalid DECL_REPLACEMENTS block in AST file");
Sebastian Redl0b17c612010-08-13 00:28:03 +00002305 return Failure;
2306 }
2307 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002308 ReplacedDecls[static_cast<DeclID>(Record[I])] =
Sebastian Redl0b17c612010-08-13 00:28:03 +00002309 std::make_pair(&F, Record[I+1]);
2310 break;
2311 }
Douglas Gregor7c789c12010-10-29 22:39:52 +00002312
2313 case CXX_BASE_SPECIFIER_OFFSETS: {
2314 if (F.LocalNumCXXBaseSpecifiers != 0) {
2315 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2316 return Failure;
2317 }
2318
2319 F.LocalNumCXXBaseSpecifiers = Record[0];
2320 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart;
2321 break;
2322 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002323
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002324 case DIAG_PRAGMA_MAPPINGS:
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002325 if (Record.size() % 2 != 0) {
2326 Error("invalid DIAG_USER_MAPPINGS block in AST file");
2327 return Failure;
2328 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002329 if (PragmaDiagMappings.empty())
2330 PragmaDiagMappings.swap(Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002331 else
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002332 PragmaDiagMappings.insert(PragmaDiagMappings.end(),
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002333 Record.begin(), Record.end());
2334 break;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002335
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002336 case CUDA_SPECIAL_DECL_REFS:
2337 // Later tables overwrite earlier ones.
2338 CUDASpecialDeclRefs.swap(Record);
2339 break;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002340
2341 case HEADER_SEARCH_TABLE:
2342 F.HeaderFileInfoTableData = BlobStart;
2343 F.LocalNumHeaderFileInfos = Record[1];
2344 if (Record[0]) {
2345 F.HeaderFileInfoTable
2346 = HeaderFileInfoLookupTable::Create(
2347 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
2348 (const unsigned char *)F.HeaderFileInfoTableData);
2349 if (PP)
2350 PP->getHeaderSearchInfo().SetExternalSource(this);
2351 }
2352 break;
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002353
2354 case FP_PRAGMA_OPTIONS:
2355 // Later tables overwrite earlier ones.
2356 FPPragmaOptions.swap(Record);
2357 break;
2358
2359 case OPENCL_EXTENSIONS:
2360 // Later tables overwrite earlier ones.
2361 OpenCLExtensions.swap(Record);
2362 break;
Sean Huntebcbe1d2011-05-04 23:29:54 +00002363
2364 case TENTATIVE_DEFINITIONS:
2365 // Optimization for the first block.
2366 if (TentativeDefinitions.empty())
2367 TentativeDefinitions.swap(Record);
2368 else
2369 TentativeDefinitions.insert(TentativeDefinitions.end(),
2370 Record.begin(), Record.end());
2371 break;
Douglas Gregord8bba9c2011-06-28 16:20:02 +00002372
2373 case KNOWN_NAMESPACES:
2374 // Optimization for the first block.
2375 if (KnownNamespaces.empty())
2376 KnownNamespaces.swap(Record);
2377 else
2378 KnownNamespaces.insert(KnownNamespaces.end(),
2379 Record.begin(), Record.end());
2380 break;
Douglas Gregorafaf3082009-04-11 00:14:32 +00002381 }
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002382 First = false;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002383 }
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002384 Error("premature end of bitstream in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002385 return Failure;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002386}
2387
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002388ASTReader::ASTReadResult ASTReader::validateFileEntries() {
2389 for (unsigned CI = 0, CN = Chain.size(); CI != CN; ++CI) {
2390 PerFileData *F = Chain[CI];
2391 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
2392
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00002393 for (unsigned i = 0, e = F->LocalNumSLocFileEntries; i != e; ++i) {
2394 SLocEntryCursor.JumpToBit(F->SLocFileOffsets[i]);
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002395 unsigned Code = SLocEntryCursor.ReadCode();
2396 if (Code == llvm::bitc::END_BLOCK ||
2397 Code == llvm::bitc::ENTER_SUBBLOCK ||
2398 Code == llvm::bitc::DEFINE_ABBREV) {
2399 Error("incorrectly-formatted source location entry in AST file");
2400 return Failure;
2401 }
2402
2403 RecordData Record;
2404 const char *BlobStart;
2405 unsigned BlobLen;
2406 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
2407 default:
2408 Error("incorrectly-formatted source location entry in AST file");
2409 return Failure;
2410
2411 case SM_SLOC_FILE_ENTRY: {
2412 llvm::StringRef Filename(BlobStart, BlobLen);
2413 const FileEntry *File = getFileEntry(Filename);
2414
2415 if (File == 0) {
2416 std::string ErrorStr = "could not find file '";
2417 ErrorStr += Filename;
2418 ErrorStr += "' referenced by AST file";
2419 Error(ErrorStr.c_str());
2420 return IgnorePCH;
2421 }
2422
2423 if (Record.size() < 6) {
2424 Error("source location entry is incorrect");
2425 return Failure;
2426 }
2427
2428 // The stat info from the FileEntry came from the cached stat
2429 // info of the PCH, so we cannot trust it.
2430 struct stat StatBuf;
2431 if (::stat(File->getName(), &StatBuf) != 0) {
2432 StatBuf.st_size = File->getSize();
2433 StatBuf.st_mtime = File->getModificationTime();
2434 }
2435
2436 if (((off_t)Record[4] != StatBuf.st_size
2437#if !defined(LLVM_ON_WIN32)
2438 // In our regression testing, the Windows file system seems to
2439 // have inconsistent modification times that sometimes
2440 // erroneously trigger this error-handling path.
2441 || (time_t)Record[5] != StatBuf.st_mtime
2442#endif
2443 )) {
2444 Error(diag::err_fe_pch_file_modified, Filename);
2445 return IgnorePCH;
2446 }
2447
2448 break;
2449 }
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002450 }
2451 }
2452 }
2453
2454 return Success;
2455}
2456
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002457ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
2458 ASTFileType Type) {
2459 switch(ReadASTCore(FileName, Type)) {
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002460 case Failure: return Failure;
2461 case IgnorePCH: return IgnorePCH;
2462 case Success: break;
2463 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002464
2465 // Here comes stuff that we only do once the entire chain is loaded.
2466
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002467 if (!DisableValidation) {
2468 switch(validateFileEntries()) {
2469 case Failure: return Failure;
2470 case IgnorePCH: return IgnorePCH;
2471 case Success: break;
2472 }
2473 }
2474
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002475 // Allocate space for loaded slocentries, identifiers, decls and types.
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002476 unsigned TotalNumIdentifiers = 0, TotalNumTypes = 0, TotalNumDecls = 0,
Sebastian Redl725cd962010-08-04 20:40:17 +00002477 TotalNumPreallocatedPreprocessingEntities = 0, TotalNumMacroDefs = 0,
2478 TotalNumSelectors = 0;
Sebastian Redl12d6da02010-07-19 22:06:55 +00002479 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002480 TotalNumSLocEntries += Chain[I]->LocalNumSLocEntries;
Sebastian Redl8db9fae2010-09-22 20:19:08 +00002481 NextSLocOffset += Chain[I]->LocalSLocSize;
Sebastian Redl2da08f92010-07-19 22:28:42 +00002482 TotalNumIdentifiers += Chain[I]->LocalNumIdentifiers;
Sebastian Redl12d6da02010-07-19 22:06:55 +00002483 TotalNumTypes += Chain[I]->LocalNumTypes;
2484 TotalNumDecls += Chain[I]->LocalNumDecls;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002485 TotalNumPreallocatedPreprocessingEntities +=
2486 Chain[I]->NumPreallocatedPreprocessingEntities;
2487 TotalNumMacroDefs += Chain[I]->LocalNumMacroDefinitions;
Sebastian Redl725cd962010-08-04 20:40:17 +00002488 TotalNumSelectors += Chain[I]->LocalNumSelectors;
Sebastian Redl12d6da02010-07-19 22:06:55 +00002489 }
Sebastian Redl8db9fae2010-09-22 20:19:08 +00002490 SourceMgr.PreallocateSLocEntries(this, TotalNumSLocEntries, NextSLocOffset);
Sebastian Redl2da08f92010-07-19 22:28:42 +00002491 IdentifiersLoaded.resize(TotalNumIdentifiers);
Sebastian Redl12d6da02010-07-19 22:06:55 +00002492 TypesLoaded.resize(TotalNumTypes);
2493 DeclsLoaded.resize(TotalNumDecls);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002494 MacroDefinitionsLoaded.resize(TotalNumMacroDefs);
2495 if (PP) {
2496 if (TotalNumIdentifiers > 0)
2497 PP->getHeaderSearchInfo().SetExternalLookup(this);
2498 if (TotalNumPreallocatedPreprocessingEntities > 0) {
2499 if (!PP->getPreprocessingRecord())
Douglas Gregordca8ee82011-05-06 16:33:08 +00002500 PP->createPreprocessingRecord(true);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002501 PP->getPreprocessingRecord()->SetExternalSource(*this,
2502 TotalNumPreallocatedPreprocessingEntities);
2503 }
2504 }
Sebastian Redl725cd962010-08-04 20:40:17 +00002505 SelectorsLoaded.resize(TotalNumSelectors);
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002506 // Preload SLocEntries.
2507 for (unsigned I = 0, N = PreloadSLocEntries.size(); I != N; ++I) {
2508 ASTReadResult Result = ReadSLocEntryRecord(PreloadSLocEntries[I]);
2509 if (Result != Success)
2510 return Result;
2511 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00002512
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002513 // Check the predefines buffers.
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00002514 if (!DisableValidation && CheckPredefinesBuffers())
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002515 return IgnorePCH;
2516
2517 if (PP) {
2518 // Initialization of keywords and pragmas occurs before the
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002519 // AST file is read, so there may be some identifiers that were
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002520 // loaded into the IdentifierTable before we intercepted the
2521 // creation of identifiers. Iterate through the list of known
2522 // identifiers and determine whether we have to establish
2523 // preprocessor definitions or top-level identifier declaration
2524 // chains for those identifiers.
2525 //
2526 // We copy the IdentifierInfo pointers to a small vector first,
2527 // since de-serializing declarations or macro definitions can add
2528 // new entries into the identifier table, invalidating the
2529 // iterators.
2530 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
2531 for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(),
2532 IdEnd = PP->getIdentifierTable().end();
2533 Id != IdEnd; ++Id)
2534 Identifiers.push_back(Id->second);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002535 // We need to search the tables in all files.
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002536 for (unsigned J = 0, M = Chain.size(); J != M; ++J) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002537 ASTIdentifierLookupTable *IdTable
2538 = (ASTIdentifierLookupTable *)Chain[J]->IdentifierLookupTable;
2539 // Not all AST files necessarily have identifier tables, only the useful
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00002540 // ones.
2541 if (!IdTable)
2542 continue;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002543 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
2544 IdentifierInfo *II = Identifiers[I];
2545 // Look in the on-disk hash tables for an entry for this identifier
Sebastian Redlc3632732010-10-05 15:59:54 +00002546 ASTIdentifierLookupTrait Info(*this, *Chain[J], II);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002547 std::pair<const char*,unsigned> Key(II->getNameStart(),II->getLength());
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002548 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002549 if (Pos == IdTable->end())
2550 continue;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002551
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002552 // Dereferencing the iterator has the effect of populating the
2553 // IdentifierInfo node with the various declarations it needs.
2554 (void)*Pos;
2555 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002556 }
2557 }
2558
2559 if (Context)
2560 InitializeContext(*Context);
2561
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002562 if (DeserializationListener)
2563 DeserializationListener->ReaderInitialized(this);
2564
Douglas Gregor414cb642010-11-30 05:23:00 +00002565 // If this AST file is a precompiled preamble, then set the main file ID of
2566 // the source manager to the file source file from which the preamble was
2567 // built. This is the only valid way to use a precompiled preamble.
2568 if (Type == Preamble) {
Douglas Gregor31d375f2011-05-06 21:43:30 +00002569 if (OriginalFileID.isInvalid()) {
2570 SourceLocation Loc
2571 = SourceMgr.getLocation(FileMgr.getFile(getOriginalSourceFile()), 1, 1);
2572 if (Loc.isValid())
2573 OriginalFileID = SourceMgr.getDecomposedLoc(Loc).first;
Douglas Gregor414cb642010-11-30 05:23:00 +00002574 }
Douglas Gregor31d375f2011-05-06 21:43:30 +00002575
2576 if (!OriginalFileID.isInvalid())
2577 SourceMgr.SetPreambleFileID(OriginalFileID);
Douglas Gregor414cb642010-11-30 05:23:00 +00002578 }
2579
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002580 return Success;
2581}
2582
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002583ASTReader::ASTReadResult ASTReader::ReadASTCore(llvm::StringRef FileName,
2584 ASTFileType Type) {
Sebastian Redla866e652010-10-01 19:59:12 +00002585 PerFileData *Prev = Chain.empty() ? 0 : Chain.back();
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002586 Chain.push_back(new PerFileData(Type));
Sebastian Redl9137a522010-07-16 17:50:48 +00002587 PerFileData &F = *Chain.back();
Sebastian Redla866e652010-10-01 19:59:12 +00002588 if (Prev)
2589 Prev->NextInSource = &F;
2590 else
2591 FirstInSource = &F;
2592 F.Loaders.push_back(Prev);
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002593
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002594 // Set the AST file name.
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002595 F.FileName = FileName;
2596
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002597 if (FileName != "-") {
2598 CurrentDir = llvm::sys::path::parent_path(FileName);
2599 if (CurrentDir.empty()) CurrentDir = ".";
2600 }
2601
Argyrios Kyrtzidisb0f4b9a2011-03-09 17:21:42 +00002602 if (!ASTBuffers.empty()) {
Sebastian Redl56558372011-04-14 14:07:41 +00002603 F.Buffer.reset(ASTBuffers.back());
2604 ASTBuffers.pop_back();
Argyrios Kyrtzidisb0f4b9a2011-03-09 17:21:42 +00002605 assert(F.Buffer && "Passed null buffer");
2606 } else {
2607 // Open the AST file.
2608 //
2609 // FIXME: This shouldn't be here, we should just take a raw_ostream.
2610 std::string ErrStr;
2611 llvm::error_code ec;
2612 if (FileName == "-") {
2613 ec = llvm::MemoryBuffer::getSTDIN(F.Buffer);
2614 if (ec)
2615 ErrStr = ec.message();
2616 } else
2617 F.Buffer.reset(FileMgr.getBufferForFile(FileName, &ErrStr));
2618 if (!F.Buffer) {
2619 Error(ErrStr.c_str());
2620 return IgnorePCH;
2621 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002622 }
2623
2624 // Initialize the stream
2625 F.StreamFile.init((const unsigned char *)F.Buffer->getBufferStart(),
2626 (const unsigned char *)F.Buffer->getBufferEnd());
Sebastian Redl9137a522010-07-16 17:50:48 +00002627 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002628 Stream.init(F.StreamFile);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002629 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002630
2631 // Sniff for the signature.
2632 if (Stream.Read(8) != 'C' ||
2633 Stream.Read(8) != 'P' ||
2634 Stream.Read(8) != 'C' ||
2635 Stream.Read(8) != 'H') {
2636 Diag(diag::err_not_a_pch_file) << FileName;
2637 return Failure;
2638 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002639
Douglas Gregor2cf26342009-04-09 22:27:44 +00002640 while (!Stream.AtEndOfStream()) {
2641 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00002642
Douglas Gregore1d918e2009-04-10 23:10:45 +00002643 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002644 Error("invalid record at top-level of AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002645 return Failure;
2646 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002647
2648 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregor668c1a42009-04-21 22:25:48 +00002649
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002650 // We only know the AST subblock ID.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002651 switch (BlockID) {
2652 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002653 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002654 Error("malformed BlockInfoBlock in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002655 return Failure;
2656 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002657 break;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002658 case AST_BLOCK_ID:
Sebastian Redl571db7f2010-08-18 23:56:56 +00002659 switch (ReadASTBlock(F)) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002660 case Success:
2661 break;
2662
2663 case Failure:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002664 return Failure;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002665
2666 case IgnorePCH:
Douglas Gregor2bec0412009-04-10 21:16:55 +00002667 // FIXME: We could consider reading through to the end of this
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002668 // AST block, skipping subblocks, to see if there are other
2669 // AST blocks elsewhere.
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002670
2671 // Clear out any preallocated source location entries, so that
2672 // the source manager does not try to resolve them later.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002673 SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002674
2675 // Remove the stat cache.
Sebastian Redl9137a522010-07-16 17:50:48 +00002676 if (F.StatCache)
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002677 FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002678
Douglas Gregore1d918e2009-04-10 23:10:45 +00002679 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002680 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002681 break;
2682 default:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002683 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002684 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002685 return Failure;
2686 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002687 break;
2688 }
Mike Stump1eb44332009-09-09 15:08:12 +00002689 }
2690
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002691 return Success;
2692}
2693
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002694void ASTReader::setPreprocessor(Preprocessor &pp) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002695 PP = &pp;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002696
2697 unsigned TotalNum = 0;
2698 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
2699 TotalNum += Chain[I]->NumPreallocatedPreprocessingEntities;
2700 if (TotalNum) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002701 if (!PP->getPreprocessingRecord())
Douglas Gregordca8ee82011-05-06 16:33:08 +00002702 PP->createPreprocessingRecord(true);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002703 PP->getPreprocessingRecord()->SetExternalSource(*this, TotalNum);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002704 }
2705}
2706
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002707void ASTReader::InitializeContext(ASTContext &Ctx) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002708 Context = &Ctx;
2709 assert(Context && "Passed null context!");
2710
2711 assert(PP && "Forgot to set Preprocessor ?");
2712 PP->getIdentifierTable().setExternalIdentifierLookup(this);
2713 PP->getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregor88a35862010-01-04 19:18:44 +00002714 PP->setExternalSource(this);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002715 PP->getHeaderSearchInfo().SetExternalSource(this);
2716
Douglas Gregor3747ee72010-10-01 01:18:02 +00002717 // If we have an update block for the TU waiting, we have to add it before
2718 // deserializing the decl.
2719 DeclContextOffsetsMap::iterator DCU = DeclContextOffsets.find(0);
2720 if (DCU != DeclContextOffsets.end()) {
2721 // Insertion could invalidate map, so grab vector.
2722 DeclContextInfos T;
2723 T.swap(DCU->second);
2724 DeclContextOffsets.erase(DCU);
2725 DeclContextOffsets[Ctx.getTranslationUnitDecl()].swap(T);
2726 }
2727
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002728 // Load the translation unit declaration
Argyrios Kyrtzidis8871a442010-07-08 17:13:02 +00002729 GetTranslationUnitDecl();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002730
2731 // Load the special types.
2732 Context->setBuiltinVaListType(
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002733 GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST]));
2734 if (unsigned Id = SpecialTypes[SPECIAL_TYPE_OBJC_ID])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002735 Context->setObjCIdType(GetType(Id));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002736 if (unsigned Sel = SpecialTypes[SPECIAL_TYPE_OBJC_SELECTOR])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002737 Context->setObjCSelType(GetType(Sel));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002738 if (unsigned Proto = SpecialTypes[SPECIAL_TYPE_OBJC_PROTOCOL])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002739 Context->setObjCProtoType(GetType(Proto));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002740 if (unsigned Class = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002741 Context->setObjCClassType(GetType(Class));
Steve Naroff14108da2009-07-10 23:34:53 +00002742
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002743 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002744 Context->setCFConstantStringType(GetType(String));
Mike Stump1eb44332009-09-09 15:08:12 +00002745 if (unsigned FastEnum
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002746 = SpecialTypes[SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002747 Context->setObjCFastEnumerationStateType(GetType(FastEnum));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002748 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002749 QualType FileType = GetType(File);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002750 if (FileType.isNull()) {
2751 Error("FILE type is NULL");
2752 return;
2753 }
John McCall183700f2009-09-21 23:43:11 +00002754 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002755 Context->setFILEDecl(Typedef->getDecl());
2756 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002757 const TagType *Tag = FileType->getAs<TagType>();
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002758 if (!Tag) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002759 Error("Invalid FILE type in AST file");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002760 return;
2761 }
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002762 Context->setFILEDecl(Tag->getDecl());
2763 }
2764 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002765 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_jmp_buf]) {
Mike Stump782fa302009-07-28 02:25:19 +00002766 QualType Jmp_bufType = GetType(Jmp_buf);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002767 if (Jmp_bufType.isNull()) {
2768 Error("jmp_bug type is NULL");
2769 return;
2770 }
John McCall183700f2009-09-21 23:43:11 +00002771 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
Mike Stump782fa302009-07-28 02:25:19 +00002772 Context->setjmp_bufDecl(Typedef->getDecl());
2773 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002774 const TagType *Tag = Jmp_bufType->getAs<TagType>();
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002775 if (!Tag) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002776 Error("Invalid jmp_buf type in AST file");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002777 return;
2778 }
Mike Stump782fa302009-07-28 02:25:19 +00002779 Context->setjmp_bufDecl(Tag->getDecl());
2780 }
2781 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002782 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_sigjmp_buf]) {
Mike Stump782fa302009-07-28 02:25:19 +00002783 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002784 if (Sigjmp_bufType.isNull()) {
2785 Error("sigjmp_buf type is NULL");
2786 return;
2787 }
John McCall183700f2009-09-21 23:43:11 +00002788 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
Mike Stump782fa302009-07-28 02:25:19 +00002789 Context->setsigjmp_bufDecl(Typedef->getDecl());
2790 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002791 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002792 assert(Tag && "Invalid sigjmp_buf type in AST file");
Mike Stump782fa302009-07-28 02:25:19 +00002793 Context->setsigjmp_bufDecl(Tag->getDecl());
2794 }
2795 }
Mike Stump1eb44332009-09-09 15:08:12 +00002796 if (unsigned ObjCIdRedef
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002797 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION])
Douglas Gregord1571ac2009-08-21 00:27:50 +00002798 Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef);
Mike Stump1eb44332009-09-09 15:08:12 +00002799 if (unsigned ObjCClassRedef
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002800 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION])
Douglas Gregord1571ac2009-08-21 00:27:50 +00002801 Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002802 if (unsigned String = SpecialTypes[SPECIAL_TYPE_BLOCK_DESCRIPTOR])
Mike Stumpadaaad32009-10-20 02:12:22 +00002803 Context->setBlockDescriptorType(GetType(String));
Mike Stump083c25e2009-10-22 00:49:09 +00002804 if (unsigned String
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002805 = SpecialTypes[SPECIAL_TYPE_BLOCK_EXTENDED_DESCRIPTOR])
Mike Stump083c25e2009-10-22 00:49:09 +00002806 Context->setBlockDescriptorExtendedType(GetType(String));
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002807 if (unsigned ObjCSelRedef
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002808 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION])
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002809 Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002810 if (unsigned String = SpecialTypes[SPECIAL_TYPE_NS_CONSTANT_STRING])
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002811 Context->setNSConstantStringType(GetType(String));
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +00002812
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002813 if (SpecialTypes[SPECIAL_TYPE_INT128_INSTALLED])
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +00002814 Context->setInt128Installed();
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002815
Richard Smithad762fc2011-04-14 22:09:26 +00002816 if (unsigned AutoDeduct = SpecialTypes[SPECIAL_TYPE_AUTO_DEDUCT])
2817 Context->AutoDeductTy = GetType(AutoDeduct);
2818 if (unsigned AutoRRefDeduct = SpecialTypes[SPECIAL_TYPE_AUTO_RREF_DEDUCT])
2819 Context->AutoRRefDeductTy = GetType(AutoRRefDeduct);
2820
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002821 ReadPragmaDiagnosticMappings(Context->getDiagnostics());
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002822
2823 // If there were any CUDA special declarations, deserialize them.
2824 if (!CUDASpecialDeclRefs.empty()) {
2825 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
2826 Context->setcudaConfigureCallDecl(
2827 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
2828 }
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002829}
2830
Douglas Gregorb64c1932009-05-12 01:31:05 +00002831/// \brief Retrieve the name of the original source file name
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002832/// directly from the AST file, without actually loading the AST
Douglas Gregorb64c1932009-05-12 01:31:05 +00002833/// file.
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002834std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +00002835 FileManager &FileMgr,
Daniel Dunbar93ebb1b2009-12-03 09:13:06 +00002836 Diagnostic &Diags) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002837 // Open the AST file.
Douglas Gregorb64c1932009-05-12 01:31:05 +00002838 std::string ErrStr;
2839 llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
Chris Lattner39b49bc2010-11-23 08:35:12 +00002840 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
Douglas Gregorb64c1932009-05-12 01:31:05 +00002841 if (!Buffer) {
Daniel Dunbar93ebb1b2009-12-03 09:13:06 +00002842 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002843 return std::string();
2844 }
2845
2846 // Initialize the stream
2847 llvm::BitstreamReader StreamFile;
2848 llvm::BitstreamCursor Stream;
Mike Stump1eb44332009-09-09 15:08:12 +00002849 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregorb64c1932009-05-12 01:31:05 +00002850 (const unsigned char *)Buffer->getBufferEnd());
2851 Stream.init(StreamFile);
2852
2853 // Sniff for the signature.
2854 if (Stream.Read(8) != 'C' ||
2855 Stream.Read(8) != 'P' ||
2856 Stream.Read(8) != 'C' ||
2857 Stream.Read(8) != 'H') {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002858 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002859 return std::string();
2860 }
2861
2862 RecordData Record;
2863 while (!Stream.AtEndOfStream()) {
2864 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00002865
Douglas Gregorb64c1932009-05-12 01:31:05 +00002866 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2867 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump1eb44332009-09-09 15:08:12 +00002868
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002869 // We only know the AST subblock ID.
Douglas Gregorb64c1932009-05-12 01:31:05 +00002870 switch (BlockID) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002871 case AST_BLOCK_ID:
2872 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002873 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002874 return std::string();
2875 }
2876 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002877
Douglas Gregorb64c1932009-05-12 01:31:05 +00002878 default:
2879 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002880 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002881 return std::string();
2882 }
2883 break;
2884 }
2885 continue;
2886 }
2887
2888 if (Code == llvm::bitc::END_BLOCK) {
2889 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002890 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002891 return std::string();
2892 }
2893 continue;
2894 }
2895
2896 if (Code == llvm::bitc::DEFINE_ABBREV) {
2897 Stream.ReadAbbrevRecord();
2898 continue;
2899 }
2900
2901 Record.clear();
2902 const char *BlobStart = 0;
2903 unsigned BlobLen = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002904 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002905 == ORIGINAL_FILE_NAME)
Douglas Gregorb64c1932009-05-12 01:31:05 +00002906 return std::string(BlobStart, BlobLen);
Mike Stump1eb44332009-09-09 15:08:12 +00002907 }
Douglas Gregorb64c1932009-05-12 01:31:05 +00002908
2909 return std::string();
2910}
2911
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002912/// \brief Parse the record that corresponds to a LangOptions data
2913/// structure.
2914///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002915/// This routine parses the language options from the AST file and then gives
2916/// them to the AST listener if one is set.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002917///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002918/// \returns true if the listener deems the file unacceptable, false otherwise.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002919bool ASTReader::ParseLanguageOptions(
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002920 const llvm::SmallVectorImpl<uint64_t> &Record) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002921 if (Listener) {
2922 LangOptions LangOpts;
Mike Stump1eb44332009-09-09 15:08:12 +00002923
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002924 #define PARSE_LANGOPT(Option) \
2925 LangOpts.Option = Record[Idx]; \
2926 ++Idx
Mike Stump1eb44332009-09-09 15:08:12 +00002927
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002928 unsigned Idx = 0;
2929 PARSE_LANGOPT(Trigraphs);
2930 PARSE_LANGOPT(BCPLComment);
2931 PARSE_LANGOPT(DollarIdents);
2932 PARSE_LANGOPT(AsmPreprocessor);
2933 PARSE_LANGOPT(GNUMode);
Chandler Carrutheb5d7b72010-04-17 20:17:31 +00002934 PARSE_LANGOPT(GNUKeywords);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002935 PARSE_LANGOPT(ImplicitInt);
2936 PARSE_LANGOPT(Digraphs);
2937 PARSE_LANGOPT(HexFloats);
2938 PARSE_LANGOPT(C99);
Peter Collingbourne7e7fbd02011-04-15 00:35:23 +00002939 PARSE_LANGOPT(C1X);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002940 PARSE_LANGOPT(Microsoft);
2941 PARSE_LANGOPT(CPlusPlus);
2942 PARSE_LANGOPT(CPlusPlus0x);
2943 PARSE_LANGOPT(CXXOperatorNames);
2944 PARSE_LANGOPT(ObjC1);
2945 PARSE_LANGOPT(ObjC2);
2946 PARSE_LANGOPT(ObjCNonFragileABI);
Fariborz Jahanian412e7982010-02-09 19:31:38 +00002947 PARSE_LANGOPT(ObjCNonFragileABI2);
Fariborz Jahanianf84109e2011-01-07 18:59:25 +00002948 PARSE_LANGOPT(AppleKext);
Ted Kremenekc32647d2010-12-23 21:35:43 +00002949 PARSE_LANGOPT(ObjCDefaultSynthProperties);
Douglas Gregor74da19f2011-06-14 23:20:43 +00002950 PARSE_LANGOPT(ObjCInferRelatedResultType);
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +00002951 PARSE_LANGOPT(NoConstantCFStrings);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002952 PARSE_LANGOPT(PascalStrings);
2953 PARSE_LANGOPT(WritableStrings);
2954 PARSE_LANGOPT(LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +00002955 PARSE_LANGOPT(AltiVec);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002956 PARSE_LANGOPT(Exceptions);
Anders Carlssonda4b7cf2011-02-19 23:53:54 +00002957 PARSE_LANGOPT(ObjCExceptions);
Anders Carlsson7da99b02011-02-23 03:04:54 +00002958 PARSE_LANGOPT(CXXExceptions);
2959 PARSE_LANGOPT(SjLjExceptions);
Douglas Gregor6f755502011-02-01 15:15:22 +00002960 PARSE_LANGOPT(MSBitfields);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002961 PARSE_LANGOPT(NeXTRuntime);
2962 PARSE_LANGOPT(Freestanding);
2963 PARSE_LANGOPT(NoBuiltin);
2964 PARSE_LANGOPT(ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +00002965 PARSE_LANGOPT(POSIXThreads);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002966 PARSE_LANGOPT(Blocks);
2967 PARSE_LANGOPT(EmitAllDecls);
2968 PARSE_LANGOPT(MathErrno);
Chris Lattnera4d71452010-06-26 21:25:03 +00002969 LangOpts.setSignedOverflowBehavior((LangOptions::SignedOverflowBehaviorTy)
2970 Record[Idx++]);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002971 PARSE_LANGOPT(HeinousExtensions);
2972 PARSE_LANGOPT(Optimize);
2973 PARSE_LANGOPT(OptimizeSize);
2974 PARSE_LANGOPT(Static);
2975 PARSE_LANGOPT(PICLevel);
2976 PARSE_LANGOPT(GNUInline);
2977 PARSE_LANGOPT(NoInline);
Chandler Carruth0d2d1bc2011-04-23 20:05:38 +00002978 PARSE_LANGOPT(Deprecated);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002979 PARSE_LANGOPT(AccessControl);
2980 PARSE_LANGOPT(CharIsSigned);
John Thompsona6fda122009-11-05 20:14:16 +00002981 PARSE_LANGOPT(ShortWChar);
Argyrios Kyrtzidisb1bdced2011-01-15 02:56:16 +00002982 PARSE_LANGOPT(ShortEnums);
Chris Lattnera4d71452010-06-26 21:25:03 +00002983 LangOpts.setGCMode((LangOptions::GCMode)Record[Idx++]);
John McCall1fb0caa2010-10-22 21:05:15 +00002984 LangOpts.setVisibilityMode((Visibility)Record[Idx++]);
Daniel Dunbarab8e2812009-09-21 04:16:19 +00002985 LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode)
Chris Lattnera4d71452010-06-26 21:25:03 +00002986 Record[Idx++]);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002987 PARSE_LANGOPT(InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +00002988 PARSE_LANGOPT(OpenCL);
Peter Collingbourne08a53262010-12-01 19:14:57 +00002989 PARSE_LANGOPT(CUDA);
Mike Stump9c276ae2009-12-12 01:27:46 +00002990 PARSE_LANGOPT(CatchUndefined);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002991 PARSE_LANGOPT(DefaultFPContract);
Roman Divacky1c51b1c2011-03-01 17:36:40 +00002992 PARSE_LANGOPT(ElideConstructors);
2993 PARSE_LANGOPT(SpellChecking);
Roman Divackycfe9af22011-03-01 17:40:53 +00002994 PARSE_LANGOPT(MRTD);
John McCallf85e1932011-06-15 23:02:42 +00002995 PARSE_LANGOPT(ObjCAutoRefCount);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002996 #undef PARSE_LANGOPT
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002997
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002998 return Listener->ReadLanguageOptions(LangOpts);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002999 }
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003000
3001 return false;
3002}
3003
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003004void ASTReader::ReadPreprocessedEntities() {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00003005 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3006 PerFileData &F = *Chain[I];
3007 if (!F.PreprocessorDetailCursor.getBitStreamReader())
3008 continue;
3009
3010 SavedStreamPosition SavedPosition(F.PreprocessorDetailCursor);
3011 F.PreprocessorDetailCursor.JumpToBit(F.PreprocessorDetailStartOffset);
3012 while (LoadPreprocessedEntity(F)) { }
3013 }
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00003014}
3015
Douglas Gregor0a480292011-02-11 19:46:30 +00003016PreprocessedEntity *ASTReader::ReadPreprocessedEntityAtOffset(uint64_t Offset) {
Douglas Gregor89d99802010-11-30 06:16:57 +00003017 PerFileData *F = 0;
3018 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3019 if (Offset < Chain[I]->SizeInBits) {
3020 F = Chain[I];
3021 break;
3022 }
3023
3024 Offset -= Chain[I]->SizeInBits;
3025 }
3026
3027 if (!F) {
3028 Error("Malformed preprocessed entity offset");
3029 return 0;
3030 }
3031
Douglas Gregor4800a5c2011-02-08 21:58:10 +00003032 // Keep track of where we are in the stream, then jump back there
3033 // after reading this entity.
3034 SavedStreamPosition SavedPosition(F->PreprocessorDetailCursor);
3035 F->PreprocessorDetailCursor.JumpToBit(Offset);
3036 return LoadPreprocessedEntity(*F);
Douglas Gregor89d99802010-11-30 06:16:57 +00003037}
3038
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003039HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
3040 HeaderFileInfoTrait Trait(FE->getName());
3041 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3042 PerFileData &F = *Chain[I];
3043 HeaderFileInfoLookupTable *Table
3044 = static_cast<HeaderFileInfoLookupTable *>(F.HeaderFileInfoTable);
3045 if (!Table)
3046 continue;
3047
3048 // Look in the on-disk hash table for an entry for this file name.
3049 HeaderFileInfoLookupTable::iterator Pos = Table->find(FE->getName(),
3050 &Trait);
3051 if (Pos == Table->end())
3052 continue;
3053
3054 HeaderFileInfo HFI = *Pos;
3055 if (Listener)
3056 Listener->ReadHeaderFileInfo(HFI, FE->getUID());
3057
3058 return HFI;
3059 }
3060
3061 return HeaderFileInfo();
3062}
3063
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00003064void ASTReader::ReadPragmaDiagnosticMappings(Diagnostic &Diag) {
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00003065 unsigned Idx = 0;
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00003066 while (Idx < PragmaDiagMappings.size()) {
3067 SourceLocation
3068 Loc = SourceLocation::getFromRawEncoding(PragmaDiagMappings[Idx++]);
3069 while (1) {
3070 assert(Idx < PragmaDiagMappings.size() &&
3071 "Invalid data, didn't find '-1' marking end of diag/map pairs");
3072 if (Idx >= PragmaDiagMappings.size())
3073 break; // Something is messed up but at least avoid infinite loop in
3074 // release build.
3075 unsigned DiagID = PragmaDiagMappings[Idx++];
3076 if (DiagID == (unsigned)-1)
3077 break; // no more diag/map pairs for this location.
3078 diag::Mapping Map = (diag::Mapping)PragmaDiagMappings[Idx++];
3079 Diag.setDiagnosticMapping(DiagID, Map, Loc);
3080 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00003081 }
3082}
3083
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003084/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003085ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003086 PerFileData *F = 0;
3087 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3088 F = Chain[N - I - 1];
3089 if (Index < F->LocalNumTypes)
3090 break;
3091 Index -= F->LocalNumTypes;
3092 }
3093 assert(F && F->LocalNumTypes > Index && "Broken chain");
Sebastian Redlc3632732010-10-05 15:59:54 +00003094 return RecordLocation(F, F->TypeOffsets[Index]);
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003095}
3096
3097/// \brief Read and return the type with the given index..
Douglas Gregor2cf26342009-04-09 22:27:44 +00003098///
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003099/// The index is the type ID, shifted and minus the number of predefs. This
3100/// routine actually reads the record corresponding to the type at the given
3101/// location. It is a helper routine for GetType, which deals with reading type
3102/// IDs.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003103QualType ASTReader::ReadTypeRecord(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003104 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00003105 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00003106
Douglas Gregor0b748912009-04-14 21:18:50 +00003107 // Keep track of where we are in the stream, then jump back there
3108 // after reading this type.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003109 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregor0b748912009-04-14 21:18:50 +00003110
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00003111 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redl27372b42010-08-11 18:52:41 +00003112
Douglas Gregord89275b2009-07-06 18:54:52 +00003113 // Note that we are loading a type record.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00003114 Deserializing AType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00003115
Sebastian Redlc3632732010-10-05 15:59:54 +00003116 DeclsCursor.JumpToBit(Loc.Offset);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003117 RecordData Record;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003118 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003119 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
3120 case TYPE_EXT_QUAL: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003121 if (Record.size() != 2) {
3122 Error("Incorrect encoding of extended qualifier type");
3123 return QualType();
3124 }
Douglas Gregor6d473962009-04-15 22:00:08 +00003125 QualType Base = GetType(Record[0]);
John McCall0953e762009-09-24 19:53:00 +00003126 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[1]);
3127 return Context->getQualifiedType(Base, Quals);
Douglas Gregor6d473962009-04-15 22:00:08 +00003128 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003129
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003130 case TYPE_COMPLEX: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003131 if (Record.size() != 1) {
3132 Error("Incorrect encoding of complex type");
3133 return QualType();
3134 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003135 QualType ElemType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003136 return Context->getComplexType(ElemType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003137 }
3138
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003139 case TYPE_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003140 if (Record.size() != 1) {
3141 Error("Incorrect encoding of pointer type");
3142 return QualType();
3143 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003144 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003145 return Context->getPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003146 }
3147
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003148 case TYPE_BLOCK_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003149 if (Record.size() != 1) {
3150 Error("Incorrect encoding of block pointer type");
3151 return QualType();
3152 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003153 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003154 return Context->getBlockPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003155 }
3156
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003157 case TYPE_LVALUE_REFERENCE: {
Richard Smithdf1550f2011-04-12 10:38:03 +00003158 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003159 Error("Incorrect encoding of lvalue reference type");
3160 return QualType();
3161 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003162 QualType PointeeType = GetType(Record[0]);
Richard Smithdf1550f2011-04-12 10:38:03 +00003163 return Context->getLValueReferenceType(PointeeType, Record[1]);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003164 }
3165
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003166 case TYPE_RVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003167 if (Record.size() != 1) {
3168 Error("Incorrect encoding of rvalue reference type");
3169 return QualType();
3170 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003171 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003172 return Context->getRValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003173 }
3174
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003175 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidis240437b2010-07-02 11:55:15 +00003176 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003177 Error("Incorrect encoding of member pointer type");
3178 return QualType();
3179 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003180 QualType PointeeType = GetType(Record[0]);
3181 QualType ClassType = GetType(Record[1]);
Douglas Gregor1ab55e92010-12-10 17:03:06 +00003182 if (PointeeType.isNull() || ClassType.isNull())
3183 return QualType();
3184
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003185 return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregor2cf26342009-04-09 22:27:44 +00003186 }
3187
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003188 case TYPE_CONSTANT_ARRAY: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003189 QualType ElementType = GetType(Record[0]);
3190 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3191 unsigned IndexTypeQuals = Record[2];
3192 unsigned Idx = 3;
3193 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003194 return Context->getConstantArrayType(ElementType, Size,
3195 ASM, IndexTypeQuals);
3196 }
3197
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003198 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003199 QualType ElementType = GetType(Record[0]);
3200 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3201 unsigned IndexTypeQuals = Record[2];
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003202 return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003203 }
3204
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003205 case TYPE_VARIABLE_ARRAY: {
Douglas Gregor0b748912009-04-14 21:18:50 +00003206 QualType ElementType = GetType(Record[0]);
3207 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3208 unsigned IndexTypeQuals = Record[2];
Sebastian Redlc3632732010-10-05 15:59:54 +00003209 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
3210 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
3211 return Context->getVariableArrayType(ElementType, ReadExpr(*Loc.F),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003212 ASM, IndexTypeQuals,
3213 SourceRange(LBLoc, RBLoc));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003214 }
3215
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003216 case TYPE_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00003217 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003218 Error("incorrect encoding of vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003219 return QualType();
3220 }
3221
3222 QualType ElementType = GetType(Record[0]);
3223 unsigned NumElements = Record[1];
Bob Wilsone86d78c2010-11-10 21:56:12 +00003224 unsigned VecKind = Record[2];
Chris Lattner788b0fd2010-06-23 06:00:24 +00003225 return Context->getVectorType(ElementType, NumElements,
Bob Wilsone86d78c2010-11-10 21:56:12 +00003226 (VectorType::VectorKind)VecKind);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003227 }
3228
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003229 case TYPE_EXT_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00003230 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003231 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003232 return QualType();
3233 }
3234
3235 QualType ElementType = GetType(Record[0]);
3236 unsigned NumElements = Record[1];
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003237 return Context->getExtVectorType(ElementType, NumElements);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003238 }
3239
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003240 case TYPE_FUNCTION_NO_PROTO: {
John McCallf85e1932011-06-15 23:02:42 +00003241 if (Record.size() != 6) {
Douglas Gregora02b1472009-04-28 21:53:25 +00003242 Error("incorrect encoding of no-proto function type");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003243 return QualType();
3244 }
3245 QualType ResultType = GetType(Record[0]);
John McCallf85e1932011-06-15 23:02:42 +00003246 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
3247 (CallingConv)Record[4], Record[5]);
Rafael Espindola264ba482010-03-30 20:24:48 +00003248 return Context->getFunctionNoProtoType(ResultType, Info);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003249 }
3250
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003251 case TYPE_FUNCTION_PROTO: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003252 QualType ResultType = GetType(Record[0]);
John McCalle23cf432010-12-14 08:05:40 +00003253
3254 FunctionProtoType::ExtProtoInfo EPI;
3255 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
Eli Friedmana49218e2011-04-09 08:18:08 +00003256 /*hasregparm*/ Record[2],
3257 /*regparm*/ Record[3],
John McCallf85e1932011-06-15 23:02:42 +00003258 static_cast<CallingConv>(Record[4]),
3259 /*produces*/ Record[5]);
John McCalle23cf432010-12-14 08:05:40 +00003260
John McCallf85e1932011-06-15 23:02:42 +00003261 unsigned Idx = 6;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003262 unsigned NumParams = Record[Idx++];
3263 llvm::SmallVector<QualType, 16> ParamTypes;
3264 for (unsigned I = 0; I != NumParams; ++I)
3265 ParamTypes.push_back(GetType(Record[Idx++]));
John McCalle23cf432010-12-14 08:05:40 +00003266
3267 EPI.Variadic = Record[Idx++];
3268 EPI.TypeQuals = Record[Idx++];
Douglas Gregorc938c162011-01-26 05:01:58 +00003269 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
Sebastian Redl60618fa2011-03-12 11:50:43 +00003270 ExceptionSpecificationType EST =
3271 static_cast<ExceptionSpecificationType>(Record[Idx++]);
3272 EPI.ExceptionSpecType = EST;
3273 if (EST == EST_Dynamic) {
3274 EPI.NumExceptions = Record[Idx++];
3275 llvm::SmallVector<QualType, 2> Exceptions;
3276 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
3277 Exceptions.push_back(GetType(Record[Idx++]));
3278 EPI.Exceptions = Exceptions.data();
3279 } else if (EST == EST_ComputedNoexcept) {
3280 EPI.NoexceptExpr = ReadExpr(*Loc.F);
3281 }
Jay Foadbeaaccd2009-05-21 09:52:38 +00003282 return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams,
John McCalle23cf432010-12-14 08:05:40 +00003283 EPI);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003284 }
3285
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003286 case TYPE_UNRESOLVED_USING:
John McCalled976492009-12-04 22:46:56 +00003287 return Context->getTypeDeclType(
3288 cast<UnresolvedUsingTypenameDecl>(GetDecl(Record[0])));
3289
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003290 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003291 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003292 Error("incorrect encoding of typedef type");
3293 return QualType();
3294 }
Richard Smith162e1c12011-04-15 14:24:37 +00003295 TypedefNameDecl *Decl = cast<TypedefNameDecl>(GetDecl(Record[0]));
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003296 QualType Canonical = GetType(Record[1]);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00003297 if (!Canonical.isNull())
3298 Canonical = Context->getCanonicalType(Canonical);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003299 return Context->getTypedefType(Decl, Canonical);
3300 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003301
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003302 case TYPE_TYPEOF_EXPR:
Sebastian Redlc3632732010-10-05 15:59:54 +00003303 return Context->getTypeOfExprType(ReadExpr(*Loc.F));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003304
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003305 case TYPE_TYPEOF: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003306 if (Record.size() != 1) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003307 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003308 return QualType();
3309 }
3310 QualType UnderlyingType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003311 return Context->getTypeOfType(UnderlyingType);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003312 }
Mike Stump1eb44332009-09-09 15:08:12 +00003313
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003314 case TYPE_DECLTYPE:
Sebastian Redlc3632732010-10-05 15:59:54 +00003315 return Context->getDecltypeType(ReadExpr(*Loc.F));
Anders Carlsson395b4752009-06-24 19:06:50 +00003316
Sean Huntca63c202011-05-24 22:41:36 +00003317 case TYPE_UNARY_TRANSFORM: {
3318 QualType BaseType = GetType(Record[0]);
3319 QualType UnderlyingType = GetType(Record[1]);
3320 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
3321 return Context->getUnaryTransformType(BaseType, UnderlyingType, UKind);
3322 }
3323
Richard Smith34b41d92011-02-20 03:19:35 +00003324 case TYPE_AUTO:
3325 return Context->getAutoType(GetType(Record[0]));
3326
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003327 case TYPE_RECORD: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003328 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003329 Error("incorrect encoding of record type");
3330 return QualType();
3331 }
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003332 bool IsDependent = Record[0];
3333 QualType T = Context->getRecordType(cast<RecordDecl>(GetDecl(Record[1])));
John McCallf4c73712011-01-19 06:33:43 +00003334 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003335 return T;
3336 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003337
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003338 case TYPE_ENUM: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003339 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003340 Error("incorrect encoding of enum type");
3341 return QualType();
3342 }
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003343 bool IsDependent = Record[0];
3344 QualType T = Context->getEnumType(cast<EnumDecl>(GetDecl(Record[1])));
John McCallf4c73712011-01-19 06:33:43 +00003345 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003346 return T;
3347 }
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003348
John McCall9d156a72011-01-06 01:58:22 +00003349 case TYPE_ATTRIBUTED: {
3350 if (Record.size() != 3) {
3351 Error("incorrect encoding of attributed type");
3352 return QualType();
3353 }
3354 QualType modifiedType = GetType(Record[0]);
3355 QualType equivalentType = GetType(Record[1]);
3356 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
3357 return Context->getAttributedType(kind, modifiedType, equivalentType);
3358 }
3359
Abramo Bagnara075f8f12010-12-10 16:29:40 +00003360 case TYPE_PAREN: {
3361 if (Record.size() != 1) {
3362 Error("incorrect encoding of paren type");
3363 return QualType();
3364 }
3365 QualType InnerType = GetType(Record[0]);
3366 return Context->getParenType(InnerType);
3367 }
3368
Douglas Gregor7536dd52010-12-20 02:24:11 +00003369 case TYPE_PACK_EXPANSION: {
Douglas Gregorf9997a02011-02-01 15:24:58 +00003370 if (Record.size() != 2) {
Douglas Gregor7536dd52010-12-20 02:24:11 +00003371 Error("incorrect encoding of pack expansion type");
3372 return QualType();
3373 }
3374 QualType Pattern = GetType(Record[0]);
3375 if (Pattern.isNull())
3376 return QualType();
Douglas Gregorcded4f62011-01-14 17:04:44 +00003377 llvm::Optional<unsigned> NumExpansions;
3378 if (Record[1])
3379 NumExpansions = Record[1] - 1;
3380 return Context->getPackExpansionType(Pattern, NumExpansions);
Douglas Gregor7536dd52010-12-20 02:24:11 +00003381 }
3382
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003383 case TYPE_ELABORATED: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00003384 unsigned Idx = 0;
3385 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3386 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3387 QualType NamedType = GetType(Record[Idx++]);
3388 return Context->getElaboratedType(Keyword, NNS, NamedType);
John McCall7da24312009-09-05 00:15:47 +00003389 }
3390
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003391 case TYPE_OBJC_INTERFACE: {
Chris Lattnerc6fa4452009-04-22 06:45:28 +00003392 unsigned Idx = 0;
3393 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
John McCallc12c5bb2010-05-15 11:32:37 +00003394 return Context->getObjCInterfaceType(ItfD);
3395 }
3396
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003397 case TYPE_OBJC_OBJECT: {
John McCallc12c5bb2010-05-15 11:32:37 +00003398 unsigned Idx = 0;
3399 QualType Base = GetType(Record[Idx++]);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00003400 unsigned NumProtos = Record[Idx++];
3401 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
3402 for (unsigned I = 0; I != NumProtos; ++I)
3403 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
Michael J. Spencer20249a12010-10-21 03:16:25 +00003404 return Context->getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00003405 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003406
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003407 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00003408 unsigned Idx = 0;
John McCallc12c5bb2010-05-15 11:32:37 +00003409 QualType Pointee = GetType(Record[Idx++]);
3410 return Context->getObjCObjectPointerType(Pointee);
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00003411 }
Argyrios Kyrtzidis24fab412009-09-29 19:42:55 +00003412
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003413 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCall49a832b2009-10-18 09:09:24 +00003414 unsigned Idx = 0;
3415 QualType Parm = GetType(Record[Idx++]);
3416 QualType Replacement = GetType(Record[Idx++]);
3417 return
3418 Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
3419 Replacement);
3420 }
John McCall3cb0ebd2010-03-10 03:28:59 +00003421
Douglas Gregorc3069d62011-01-14 02:55:32 +00003422 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
3423 unsigned Idx = 0;
3424 QualType Parm = GetType(Record[Idx++]);
3425 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
3426 return Context->getSubstTemplateTypeParmPackType(
3427 cast<TemplateTypeParmType>(Parm),
3428 ArgPack);
3429 }
3430
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003431 case TYPE_INJECTED_CLASS_NAME: {
John McCall3cb0ebd2010-03-10 03:28:59 +00003432 CXXRecordDecl *D = cast<CXXRecordDecl>(GetDecl(Record[0]));
3433 QualType TST = GetType(Record[1]); // probably derivable
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00003434 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003435 // for AST reading, too much interdependencies.
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00003436 return
3437 QualType(new (*Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCall3cb0ebd2010-03-10 03:28:59 +00003438 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003439
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003440 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003441 unsigned Idx = 0;
3442 unsigned Depth = Record[Idx++];
3443 unsigned Index = Record[Idx++];
3444 bool Pack = Record[Idx++];
Chandler Carruth4fb86f82011-05-01 00:51:33 +00003445 TemplateTypeParmDecl *D =
3446 cast_or_null<TemplateTypeParmDecl>(GetDecl(Record[Idx++]));
3447 return Context->getTemplateTypeParmType(Depth, Index, Pack, D);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003448 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003449
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003450 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00003451 unsigned Idx = 0;
3452 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3453 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3454 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +00003455 QualType Canon = GetType(Record[Idx++]);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00003456 if (!Canon.isNull())
3457 Canon = Context->getCanonicalType(Canon);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +00003458 return Context->getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00003459 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003460
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003461 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00003462 unsigned Idx = 0;
3463 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3464 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3465 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
3466 unsigned NumArgs = Record[Idx++];
3467 llvm::SmallVector<TemplateArgument, 8> Args;
3468 Args.reserve(NumArgs);
3469 while (NumArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00003470 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00003471 return Context->getDependentTemplateSpecializationType(Keyword, NNS, Name,
3472 Args.size(), Args.data());
3473 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003474
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003475 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00003476 unsigned Idx = 0;
3477
3478 // ArrayType
3479 QualType ElementType = GetType(Record[Idx++]);
3480 ArrayType::ArraySizeModifier ASM
3481 = (ArrayType::ArraySizeModifier)Record[Idx++];
3482 unsigned IndexTypeQuals = Record[Idx++];
3483
3484 // DependentSizedArrayType
Sebastian Redlc3632732010-10-05 15:59:54 +00003485 Expr *NumElts = ReadExpr(*Loc.F);
3486 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00003487
3488 return Context->getDependentSizedArrayType(ElementType, NumElts, ASM,
3489 IndexTypeQuals, Brackets);
3490 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003491
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003492 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003493 unsigned Idx = 0;
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003494 bool IsDependent = Record[Idx++];
Douglas Gregor1aee05d2011-01-15 06:45:20 +00003495 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003496 llvm::SmallVector<TemplateArgument, 8> Args;
Sebastian Redlc3632732010-10-05 15:59:54 +00003497 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
Richard Smith3e4c6c42011-05-05 21:57:07 +00003498 QualType Underlying = GetType(Record[Idx++]);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003499 QualType T;
Richard Smith3e4c6c42011-05-05 21:57:07 +00003500 if (Underlying.isNull())
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003501 T = Context->getCanonicalTemplateSpecializationType(Name, Args.data(),
3502 Args.size());
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003503 else
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003504 T = Context->getTemplateSpecializationType(Name, Args.data(),
Richard Smith3e4c6c42011-05-05 21:57:07 +00003505 Args.size(), Underlying);
John McCallf4c73712011-01-19 06:33:43 +00003506 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003507 return T;
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003508 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003509 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003510 // Suppress a GCC warning
3511 return QualType();
3512}
3513
Sebastian Redlc3632732010-10-05 15:59:54 +00003514class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003515 ASTReader &Reader;
Sebastian Redlc3632732010-10-05 15:59:54 +00003516 ASTReader::PerFileData &F;
Sebastian Redl577d4792010-07-22 22:43:28 +00003517 llvm::BitstreamCursor &DeclsCursor;
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003518 const ASTReader::RecordData &Record;
John McCalla1ee0c52009-10-16 21:56:05 +00003519 unsigned &Idx;
3520
Sebastian Redlc3632732010-10-05 15:59:54 +00003521 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
3522 unsigned &I) {
3523 return Reader.ReadSourceLocation(F, R, I);
3524 }
3525
John McCalla1ee0c52009-10-16 21:56:05 +00003526public:
Sebastian Redlc3632732010-10-05 15:59:54 +00003527 TypeLocReader(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003528 const ASTReader::RecordData &Record, unsigned &Idx)
Sebastian Redlc3632732010-10-05 15:59:54 +00003529 : Reader(Reader), F(F), DeclsCursor(F.DeclsCursor), Record(Record), Idx(Idx)
3530 { }
John McCalla1ee0c52009-10-16 21:56:05 +00003531
John McCall51bd8032009-10-18 01:05:36 +00003532 // We want compile-time assurance that we've enumerated all of
3533 // these, so unfortunately we have to declare them first, then
3534 // define them out-of-line.
3535#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +00003536#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +00003537 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00003538#include "clang/AST/TypeLocNodes.def"
3539
John McCall51bd8032009-10-18 01:05:36 +00003540 void VisitFunctionTypeLoc(FunctionTypeLoc);
3541 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00003542};
3543
John McCall51bd8032009-10-18 01:05:36 +00003544void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCalla1ee0c52009-10-16 21:56:05 +00003545 // nothing to do
3546}
John McCall51bd8032009-10-18 01:05:36 +00003547void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003548 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
Douglas Gregorddf889a2010-01-18 18:04:31 +00003549 if (TL.needsExtraLocalData()) {
3550 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
3551 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
3552 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
3553 TL.setModeAttr(Record[Idx++]);
3554 }
John McCalla1ee0c52009-10-16 21:56:05 +00003555}
John McCall51bd8032009-10-18 01:05:36 +00003556void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003557 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003558}
John McCall51bd8032009-10-18 01:05:36 +00003559void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003560 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003561}
John McCall51bd8032009-10-18 01:05:36 +00003562void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003563 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003564}
John McCall51bd8032009-10-18 01:05:36 +00003565void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003566 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003567}
John McCall51bd8032009-10-18 01:05:36 +00003568void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003569 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003570}
John McCall51bd8032009-10-18 01:05:36 +00003571void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003572 TL.setStarLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +00003573 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003574}
John McCall51bd8032009-10-18 01:05:36 +00003575void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003576 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
3577 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003578 if (Record[Idx++])
Sebastian Redlc3632732010-10-05 15:59:54 +00003579 TL.setSizeExpr(Reader.ReadExpr(F));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003580 else
John McCall51bd8032009-10-18 01:05:36 +00003581 TL.setSizeExpr(0);
3582}
3583void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
3584 VisitArrayTypeLoc(TL);
3585}
3586void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
3587 VisitArrayTypeLoc(TL);
3588}
3589void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
3590 VisitArrayTypeLoc(TL);
3591}
3592void TypeLocReader::VisitDependentSizedArrayTypeLoc(
3593 DependentSizedArrayTypeLoc TL) {
3594 VisitArrayTypeLoc(TL);
3595}
3596void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
3597 DependentSizedExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003598 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003599}
3600void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003601 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003602}
3603void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003604 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003605}
3606void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnara796aa442011-03-12 11:17:06 +00003607 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
3608 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
Douglas Gregordab60ad2010-10-01 18:44:50 +00003609 TL.setTrailingReturn(Record[Idx++]);
John McCall51bd8032009-10-18 01:05:36 +00003610 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
John McCall86acc2a2009-10-23 01:28:53 +00003611 TL.setArg(i, cast_or_null<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
John McCall51bd8032009-10-18 01:05:36 +00003612 }
3613}
3614void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
3615 VisitFunctionTypeLoc(TL);
3616}
3617void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
3618 VisitFunctionTypeLoc(TL);
3619}
John McCalled976492009-12-04 22:46:56 +00003620void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003621 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalled976492009-12-04 22:46:56 +00003622}
John McCall51bd8032009-10-18 01:05:36 +00003623void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003624 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003625}
3626void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003627 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3628 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3629 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003630}
3631void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003632 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3633 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3634 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3635 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003636}
3637void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003638 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003639}
Sean Huntca63c202011-05-24 22:41:36 +00003640void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
3641 TL.setKWLoc(ReadSourceLocation(Record, Idx));
3642 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3643 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3644 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
3645}
Richard Smith34b41d92011-02-20 03:19:35 +00003646void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
3647 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3648}
John McCall51bd8032009-10-18 01:05:36 +00003649void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003650 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003651}
3652void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003653 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003654}
John McCall9d156a72011-01-06 01:58:22 +00003655void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
3656 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
3657 if (TL.hasAttrOperand()) {
3658 SourceRange range;
3659 range.setBegin(ReadSourceLocation(Record, Idx));
3660 range.setEnd(ReadSourceLocation(Record, Idx));
3661 TL.setAttrOperandParensRange(range);
3662 }
3663 if (TL.hasAttrExprOperand()) {
3664 if (Record[Idx++])
3665 TL.setAttrExprOperand(Reader.ReadExpr(F));
3666 else
3667 TL.setAttrExprOperand(0);
3668 } else if (TL.hasAttrEnumOperand())
3669 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
3670}
John McCall51bd8032009-10-18 01:05:36 +00003671void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003672 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003673}
John McCall49a832b2009-10-18 09:09:24 +00003674void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
3675 SubstTemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003676 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall49a832b2009-10-18 09:09:24 +00003677}
Douglas Gregorc3069d62011-01-14 02:55:32 +00003678void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
3679 SubstTemplateTypeParmPackTypeLoc TL) {
3680 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3681}
John McCall51bd8032009-10-18 01:05:36 +00003682void TypeLocReader::VisitTemplateSpecializationTypeLoc(
3683 TemplateSpecializationTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003684 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
3685 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3686 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall833ca992009-10-29 08:12:44 +00003687 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
3688 TL.setArgLocInfo(i,
Sebastian Redlc3632732010-10-05 15:59:54 +00003689 Reader.GetTemplateArgumentLocInfo(F,
3690 TL.getTypePtr()->getArg(i).getKind(),
3691 Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003692}
Abramo Bagnara075f8f12010-12-10 16:29:40 +00003693void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
3694 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3695 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3696}
Abramo Bagnara465d41b2010-05-11 21:36:43 +00003697void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003698 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor9e876872011-03-01 18:12:44 +00003699 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003700}
John McCall3cb0ebd2010-03-10 03:28:59 +00003701void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003702 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall3cb0ebd2010-03-10 03:28:59 +00003703}
Douglas Gregor4714c122010-03-31 17:34:00 +00003704void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003705 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor2494dd02011-03-01 01:34:45 +00003706 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00003707 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003708}
John McCall33500952010-06-11 00:33:02 +00003709void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
3710 DependentTemplateSpecializationTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003711 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor94fdffa2011-03-01 20:11:18 +00003712 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00003713 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3714 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3715 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall33500952010-06-11 00:33:02 +00003716 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
3717 TL.setArgLocInfo(I,
Sebastian Redlc3632732010-10-05 15:59:54 +00003718 Reader.GetTemplateArgumentLocInfo(F,
3719 TL.getTypePtr()->getArg(I).getKind(),
3720 Record, Idx));
John McCall33500952010-06-11 00:33:02 +00003721}
Douglas Gregor7536dd52010-12-20 02:24:11 +00003722void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
3723 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
3724}
John McCall51bd8032009-10-18 01:05:36 +00003725void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003726 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallc12c5bb2010-05-15 11:32:37 +00003727}
3728void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
3729 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00003730 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3731 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003732 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Sebastian Redlc3632732010-10-05 15:59:54 +00003733 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003734}
John McCall54e14c42009-10-22 22:37:11 +00003735void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003736 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall54e14c42009-10-22 22:37:11 +00003737}
John McCalla1ee0c52009-10-16 21:56:05 +00003738
Sebastian Redlc3632732010-10-05 15:59:54 +00003739TypeSourceInfo *ASTReader::GetTypeSourceInfo(PerFileData &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00003740 const RecordData &Record,
John McCalla1ee0c52009-10-16 21:56:05 +00003741 unsigned &Idx) {
3742 QualType InfoTy = GetType(Record[Idx++]);
3743 if (InfoTy.isNull())
3744 return 0;
3745
John McCalla93c9342009-12-07 02:54:59 +00003746 TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy);
Sebastian Redlc3632732010-10-05 15:59:54 +00003747 TypeLocReader TLR(*this, F, Record, Idx);
John McCalla93c9342009-12-07 02:54:59 +00003748 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCalla1ee0c52009-10-16 21:56:05 +00003749 TLR.Visit(TL);
John McCalla93c9342009-12-07 02:54:59 +00003750 return TInfo;
John McCalla1ee0c52009-10-16 21:56:05 +00003751}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003752
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003753QualType ASTReader::GetType(TypeID ID) {
John McCall0953e762009-09-24 19:53:00 +00003754 unsigned FastQuals = ID & Qualifiers::FastMask;
3755 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003756
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003757 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003758 QualType T;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003759 switch ((PredefinedTypeIDs)Index) {
3760 case PREDEF_TYPE_NULL_ID: return QualType();
3761 case PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break;
3762 case PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003763
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003764 case PREDEF_TYPE_CHAR_U_ID:
3765 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregor2cf26342009-04-09 22:27:44 +00003766 // FIXME: Check that the signedness of CharTy is correct!
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003767 T = Context->CharTy;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003768 break;
3769
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003770 case PREDEF_TYPE_UCHAR_ID: T = Context->UnsignedCharTy; break;
3771 case PREDEF_TYPE_USHORT_ID: T = Context->UnsignedShortTy; break;
3772 case PREDEF_TYPE_UINT_ID: T = Context->UnsignedIntTy; break;
3773 case PREDEF_TYPE_ULONG_ID: T = Context->UnsignedLongTy; break;
3774 case PREDEF_TYPE_ULONGLONG_ID: T = Context->UnsignedLongLongTy; break;
3775 case PREDEF_TYPE_UINT128_ID: T = Context->UnsignedInt128Ty; break;
3776 case PREDEF_TYPE_SCHAR_ID: T = Context->SignedCharTy; break;
3777 case PREDEF_TYPE_WCHAR_ID: T = Context->WCharTy; break;
3778 case PREDEF_TYPE_SHORT_ID: T = Context->ShortTy; break;
3779 case PREDEF_TYPE_INT_ID: T = Context->IntTy; break;
3780 case PREDEF_TYPE_LONG_ID: T = Context->LongTy; break;
3781 case PREDEF_TYPE_LONGLONG_ID: T = Context->LongLongTy; break;
3782 case PREDEF_TYPE_INT128_ID: T = Context->Int128Ty; break;
3783 case PREDEF_TYPE_FLOAT_ID: T = Context->FloatTy; break;
3784 case PREDEF_TYPE_DOUBLE_ID: T = Context->DoubleTy; break;
3785 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy; break;
3786 case PREDEF_TYPE_OVERLOAD_ID: T = Context->OverloadTy; break;
John McCall864c0412011-04-26 20:42:42 +00003787 case PREDEF_TYPE_BOUND_MEMBER: T = Context->BoundMemberTy; break;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003788 case PREDEF_TYPE_DEPENDENT_ID: T = Context->DependentTy; break;
John McCall1de4d4e2011-04-07 08:22:57 +00003789 case PREDEF_TYPE_UNKNOWN_ANY: T = Context->UnknownAnyTy; break;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003790 case PREDEF_TYPE_NULLPTR_ID: T = Context->NullPtrTy; break;
3791 case PREDEF_TYPE_CHAR16_ID: T = Context->Char16Ty; break;
3792 case PREDEF_TYPE_CHAR32_ID: T = Context->Char32Ty; break;
3793 case PREDEF_TYPE_OBJC_ID: T = Context->ObjCBuiltinIdTy; break;
3794 case PREDEF_TYPE_OBJC_CLASS: T = Context->ObjCBuiltinClassTy; break;
3795 case PREDEF_TYPE_OBJC_SEL: T = Context->ObjCBuiltinSelTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003796 }
3797
3798 assert(!T.isNull() && "Unknown predefined type");
John McCall0953e762009-09-24 19:53:00 +00003799 return T.withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003800 }
3801
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003802 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003803 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl07a353c2010-07-14 20:26:45 +00003804 if (TypesLoaded[Index].isNull()) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003805 TypesLoaded[Index] = ReadTypeRecord(Index);
Douglas Gregor97475832010-10-05 18:37:06 +00003806 if (TypesLoaded[Index].isNull())
3807 return QualType();
3808
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003809 TypesLoaded[Index]->setFromAST();
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003810 TypeIdxs[TypesLoaded[Index]] = TypeIdx::fromTypeID(ID);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003811 if (DeserializationListener)
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003812 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1476ed42010-07-16 16:36:56 +00003813 TypesLoaded[Index]);
Sebastian Redl07a353c2010-07-14 20:26:45 +00003814 }
Mike Stump1eb44332009-09-09 15:08:12 +00003815
John McCall0953e762009-09-24 19:53:00 +00003816 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003817}
3818
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003819TypeID ASTReader::GetTypeID(QualType T) const {
3820 return MakeTypeID(T,
3821 std::bind1st(std::mem_fun(&ASTReader::GetTypeIdx), this));
3822}
3823
3824TypeIdx ASTReader::GetTypeIdx(QualType T) const {
3825 if (T.isNull())
3826 return TypeIdx();
3827 assert(!T.getLocalFastQualifiers());
3828
3829 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3830 // GetTypeIdx is mostly used for computing the hash of DeclarationNames and
3831 // comparing keys of ASTDeclContextNameLookupTable.
3832 // If the type didn't come from the AST file use a specially marked index
3833 // so that any hash/key comparison fail since no such index is stored
3834 // in a AST file.
3835 if (I == TypeIdxs.end())
3836 return TypeIdx(-1);
3837 return I->second;
3838}
3839
Douglas Gregor7c789c12010-10-29 22:39:52 +00003840unsigned ASTReader::getTotalNumCXXBaseSpecifiers() const {
3841 unsigned Result = 0;
3842 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
3843 Result += Chain[I]->LocalNumCXXBaseSpecifiers;
3844
3845 return Result;
3846}
3847
John McCall833ca992009-10-29 08:12:44 +00003848TemplateArgumentLocInfo
Sebastian Redlc3632732010-10-05 15:59:54 +00003849ASTReader::GetTemplateArgumentLocInfo(PerFileData &F,
3850 TemplateArgument::ArgKind Kind,
John McCall833ca992009-10-29 08:12:44 +00003851 const RecordData &Record,
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00003852 unsigned &Index) {
John McCall833ca992009-10-29 08:12:44 +00003853 switch (Kind) {
3854 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00003855 return ReadExpr(F);
John McCall833ca992009-10-29 08:12:44 +00003856 case TemplateArgument::Type:
Sebastian Redlc3632732010-10-05 15:59:54 +00003857 return GetTypeSourceInfo(F, Record, Index);
Douglas Gregor788cd062009-11-11 01:00:40 +00003858 case TemplateArgument::Template: {
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003859 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
3860 Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00003861 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003862 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregora7fc9012011-01-05 18:58:31 +00003863 SourceLocation());
3864 }
3865 case TemplateArgument::TemplateExpansion: {
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003866 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
3867 Index);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003868 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorba68eca2011-01-05 17:40:24 +00003869 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003870 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregorba68eca2011-01-05 17:40:24 +00003871 EllipsisLoc);
Douglas Gregor788cd062009-11-11 01:00:40 +00003872 }
John McCall833ca992009-10-29 08:12:44 +00003873 case TemplateArgument::Null:
3874 case TemplateArgument::Integral:
3875 case TemplateArgument::Declaration:
3876 case TemplateArgument::Pack:
3877 return TemplateArgumentLocInfo();
3878 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00003879 llvm_unreachable("unexpected template argument loc");
John McCall833ca992009-10-29 08:12:44 +00003880 return TemplateArgumentLocInfo();
3881}
3882
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003883TemplateArgumentLoc
Sebastian Redlc3632732010-10-05 15:59:54 +00003884ASTReader::ReadTemplateArgumentLoc(PerFileData &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00003885 const RecordData &Record, unsigned &Index) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003886 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003887
3888 if (Arg.getKind() == TemplateArgument::Expression) {
3889 if (Record[Index++]) // bool InfoHasSameExpr.
3890 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
3891 }
Sebastian Redlc3632732010-10-05 15:59:54 +00003892 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00003893 Record, Index));
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003894}
3895
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003896Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall76bd1f32010-06-01 09:23:16 +00003897 return GetDecl(ID);
3898}
3899
Douglas Gregor7c789c12010-10-29 22:39:52 +00003900uint64_t
3901ASTReader::GetCXXBaseSpecifiersOffset(serialization::CXXBaseSpecifiersID ID) {
3902 if (ID == 0)
3903 return 0;
3904
3905 --ID;
3906 uint64_t Offset = 0;
3907 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Anders Carlssonf25330b2011-03-09 05:09:32 +00003908 PerFileData &F = *Chain[N - I - 1];
3909
3910 if (ID < F.LocalNumCXXBaseSpecifiers)
3911 return Offset + F.CXXBaseSpecifiersOffsets[ID];
Douglas Gregor7c789c12010-10-29 22:39:52 +00003912
Anders Carlssonf25330b2011-03-09 05:09:32 +00003913 ID -= F.LocalNumCXXBaseSpecifiers;
3914 Offset += F.SizeInBits;
Douglas Gregor7c789c12010-10-29 22:39:52 +00003915 }
3916
3917 assert(false && "CXXBaseSpecifiers not found");
3918 return 0;
3919}
3920
3921CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
3922 // Figure out which AST file contains this offset.
3923 PerFileData *F = 0;
3924 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Anders Carlssonf25330b2011-03-09 05:09:32 +00003925 if (Offset < Chain[N - I - 1]->SizeInBits) {
3926 F = Chain[N - I - 1];
Douglas Gregor7c789c12010-10-29 22:39:52 +00003927 break;
3928 }
3929
Anders Carlssonf25330b2011-03-09 05:09:32 +00003930 Offset -= Chain[N - I - 1]->SizeInBits;
Douglas Gregor7c789c12010-10-29 22:39:52 +00003931 }
Anders Carlssonf25330b2011-03-09 05:09:32 +00003932
Douglas Gregor7c789c12010-10-29 22:39:52 +00003933 if (!F) {
3934 Error("Malformed AST file: C++ base specifiers at impossible offset");
3935 return 0;
3936 }
3937
3938 llvm::BitstreamCursor &Cursor = F->DeclsCursor;
3939 SavedStreamPosition SavedPosition(Cursor);
3940 Cursor.JumpToBit(Offset);
3941 ReadingKindTracker ReadingKind(Read_Decl, *this);
3942 RecordData Record;
3943 unsigned Code = Cursor.ReadCode();
3944 unsigned RecCode = Cursor.ReadRecord(Code, Record);
3945 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
3946 Error("Malformed AST file: missing C++ base specifiers");
3947 return 0;
3948 }
3949
3950 unsigned Idx = 0;
3951 unsigned NumBases = Record[Idx++];
3952 void *Mem = Context->Allocate(sizeof(CXXBaseSpecifier) * NumBases);
3953 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
3954 for (unsigned I = 0; I != NumBases; ++I)
3955 Bases[I] = ReadCXXBaseSpecifier(*F, Record, Idx);
3956 return Bases;
3957}
3958
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003959TranslationUnitDecl *ASTReader::GetTranslationUnitDecl() {
Sebastian Redl30c514c2010-07-14 23:45:08 +00003960 if (!DeclsLoaded[0]) {
Sebastian Redle1dde812010-08-24 00:50:04 +00003961 ReadDeclRecord(0, 1);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003962 if (DeserializationListener)
Sebastian Redl1476ed42010-07-16 16:36:56 +00003963 DeserializationListener->DeclRead(1, DeclsLoaded[0]);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003964 }
Argyrios Kyrtzidis8871a442010-07-08 17:13:02 +00003965
3966 return cast<TranslationUnitDecl>(DeclsLoaded[0]);
3967}
3968
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003969Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003970 if (ID == 0)
3971 return 0;
3972
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003973 if (ID > DeclsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003974 Error("declaration ID out-of-range for AST file");
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003975 return 0;
3976 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003977
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003978 unsigned Index = ID - 1;
Sebastian Redl30c514c2010-07-14 23:45:08 +00003979 if (!DeclsLoaded[Index]) {
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00003980 ReadDeclRecord(Index, ID);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003981 if (DeserializationListener)
3982 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
3983 }
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003984
3985 return DeclsLoaded[Index];
Douglas Gregor2cf26342009-04-09 22:27:44 +00003986}
3987
Chris Lattner887e2b32009-04-27 05:46:25 +00003988/// \brief Resolve the offset of a statement into a statement.
3989///
3990/// This operation will read a new statement from the external
3991/// source each time it is called, and is meant to be used via a
3992/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003993Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00003994 // Switch case IDs are per Decl.
3995 ClearSwitchCaseIDs();
3996
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003997 // Offset here is a global offset across the entire chain.
3998 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3999 PerFileData &F = *Chain[N - I - 1];
4000 if (Offset < F.SizeInBits) {
4001 // Since we know that this statement is part of a decl, make sure to use
4002 // the decl cursor to read it.
4003 F.DeclsCursor.JumpToBit(Offset);
Sebastian Redlc3632732010-10-05 15:59:54 +00004004 return ReadStmtFromStream(F);
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00004005 }
4006 Offset -= F.SizeInBits;
4007 }
4008 llvm_unreachable("Broken chain");
Douglas Gregor250fc9c2009-04-18 00:07:54 +00004009}
4010
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004011bool ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00004012 bool (*isKindWeWant)(Decl::Kind),
John McCall76bd1f32010-06-01 09:23:16 +00004013 llvm::SmallVectorImpl<Decl*> &Decls) {
Mike Stump1eb44332009-09-09 15:08:12 +00004014 assert(DC->hasExternalLexicalStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00004015 "DeclContext has no lexical decls in storage");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004016
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00004017 // There might be lexical decls in multiple parts of the chain, for the TU
4018 // at least.
Sebastian Redl4a9eb262010-09-28 02:24:44 +00004019 // DeclContextOffsets might reallocate as we load additional decls below,
4020 // so make a copy of the vector.
4021 DeclContextInfos Infos = DeclContextOffsets[DC];
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00004022 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
4023 I != E; ++I) {
Sebastian Redl681d7232010-07-27 00:17:23 +00004024 // IDs can be 0 if this context doesn't contain declarations.
4025 if (!I->LexicalDecls)
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00004026 continue;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00004027
4028 // Load all of the declaration IDs
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00004029 for (const KindDeclIDPair *ID = I->LexicalDecls,
4030 *IDE = ID + I->NumLexicalDecls; ID != IDE; ++ID) {
4031 if (isKindWeWant && !isKindWeWant((Decl::Kind)ID->first))
4032 continue;
4033
4034 Decl *D = GetDecl(ID->second);
Sebastian Redl4a9eb262010-09-28 02:24:44 +00004035 assert(D && "Null decl in lexical decls");
4036 Decls.push_back(D);
4037 }
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004038 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00004039
Douglas Gregor25123082009-04-22 22:34:57 +00004040 ++NumLexicalDeclContextsRead;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004041 return false;
4042}
4043
John McCall76bd1f32010-06-01 09:23:16 +00004044DeclContext::lookup_result
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004045ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +00004046 DeclarationName Name) {
Mike Stump1eb44332009-09-09 15:08:12 +00004047 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00004048 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00004049 if (!Name)
4050 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
4051 DeclContext::lookup_iterator(0));
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004052
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00004053 llvm::SmallVector<NamedDecl *, 64> Decls;
Sebastian Redl8b122732010-08-24 00:49:55 +00004054 // There might be visible decls in multiple parts of the chain, for the TU
Sebastian Redl5967d622010-08-24 00:50:16 +00004055 // and namespaces. For any given name, the last available results replace
4056 // all earlier ones. For this reason, we walk in reverse.
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00004057 DeclContextInfos &Infos = DeclContextOffsets[DC];
Sebastian Redl5967d622010-08-24 00:50:16 +00004058 for (DeclContextInfos::reverse_iterator I = Infos.rbegin(), E = Infos.rend();
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00004059 I != E; ++I) {
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00004060 if (!I->NameLookupTableData)
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00004061 continue;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004062
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00004063 ASTDeclContextNameLookupTable *LookupTable =
4064 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
4065 ASTDeclContextNameLookupTable::iterator Pos = LookupTable->find(Name);
4066 if (Pos == LookupTable->end())
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00004067 continue;
4068
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00004069 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
4070 for (; Data.first != Data.second; ++Data.first)
4071 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
Sebastian Redl5967d622010-08-24 00:50:16 +00004072 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004073 }
4074
Douglas Gregor25123082009-04-22 22:34:57 +00004075 ++NumVisibleDeclContextsRead;
John McCall76bd1f32010-06-01 09:23:16 +00004076
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00004077 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall76bd1f32010-06-01 09:23:16 +00004078 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004079}
4080
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +00004081void ASTReader::MaterializeVisibleDecls(const DeclContext *DC) {
4082 assert(DC->hasExternalVisibleStorage() &&
4083 "DeclContext has no visible decls in storage");
4084
4085 llvm::SmallVector<NamedDecl *, 64> Decls;
4086 // There might be visible decls in multiple parts of the chain, for the TU
4087 // and namespaces.
4088 DeclContextInfos &Infos = DeclContextOffsets[DC];
4089 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
4090 I != E; ++I) {
4091 if (!I->NameLookupTableData)
4092 continue;
4093
4094 ASTDeclContextNameLookupTable *LookupTable =
4095 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
4096 for (ASTDeclContextNameLookupTable::item_iterator
4097 ItemI = LookupTable->item_begin(),
4098 ItemEnd = LookupTable->item_end() ; ItemI != ItemEnd; ++ItemI) {
4099 ASTDeclContextNameLookupTable::item_iterator::value_type Val
4100 = *ItemI;
4101 ASTDeclContextNameLookupTrait::data_type Data = Val.second;
4102 Decls.clear();
4103 for (; Data.first != Data.second; ++Data.first)
4104 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
4105 MaterializeVisibleDeclsForName(DC, Val.first, Decls);
4106 }
4107 }
4108}
4109
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004110void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00004111 assert(Consumer);
4112 while (!InterestingDecls.empty()) {
4113 DeclGroupRef DG(InterestingDecls.front());
4114 InterestingDecls.pop_front();
Sebastian Redl27372b42010-08-11 18:52:41 +00004115 Consumer->HandleInterestingDecl(DG);
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00004116 }
4117}
4118
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004119void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor0af2ca42009-04-22 19:09:20 +00004120 this->Consumer = Consumer;
4121
Douglas Gregorfdd01722009-04-14 00:24:19 +00004122 if (!Consumer)
4123 return;
4124
4125 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00004126 // Force deserialization of this decl, which will cause it to be queued for
4127 // passing to the consumer.
Daniel Dunbar04a0b502009-09-17 03:06:44 +00004128 GetDecl(ExternalDefinitions[I]);
Douglas Gregorfdd01722009-04-14 00:24:19 +00004129 }
Douglas Gregorc62a2fe2009-04-25 00:41:30 +00004130
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00004131 PassInterestingDeclsToConsumer();
Douglas Gregorfdd01722009-04-14 00:24:19 +00004132}
4133
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004134void ASTReader::PrintStats() {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004135 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregor2cf26342009-04-09 22:27:44 +00004136
Mike Stump1eb44332009-09-09 15:08:12 +00004137 unsigned NumTypesLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00004138 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall0953e762009-09-24 19:53:00 +00004139 QualType());
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00004140 unsigned NumDeclsLoaded
4141 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
4142 (Decl *)0);
4143 unsigned NumIdentifiersLoaded
4144 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
4145 IdentifiersLoaded.end(),
4146 (IdentifierInfo *)0);
Mike Stump1eb44332009-09-09 15:08:12 +00004147 unsigned NumSelectorsLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00004148 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
4149 SelectorsLoaded.end(),
4150 Selector());
Douglas Gregor2d41cc12009-04-13 20:50:16 +00004151
Douglas Gregor4fed3f42009-04-27 18:38:38 +00004152 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
4153 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00004154 if (TotalNumSLocEntries)
4155 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
4156 NumSLocEntriesRead, TotalNumSLocEntries,
4157 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004158 if (!TypesLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00004159 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004160 NumTypesLoaded, (unsigned)TypesLoaded.size(),
4161 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
4162 if (!DeclsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00004163 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004164 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
4165 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00004166 if (!IdentifiersLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00004167 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00004168 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
4169 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Sebastian Redl725cd962010-08-04 20:40:17 +00004170 if (!SelectorsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00004171 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redl725cd962010-08-04 20:40:17 +00004172 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
4173 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor83941df2009-04-25 17:48:32 +00004174 if (TotalNumStatements)
4175 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
4176 NumStatementsRead, TotalNumStatements,
4177 ((float)NumStatementsRead/TotalNumStatements * 100));
4178 if (TotalNumMacros)
4179 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
4180 NumMacrosRead, TotalNumMacros,
4181 ((float)NumMacrosRead/TotalNumMacros * 100));
4182 if (TotalLexicalDeclContexts)
4183 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
4184 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
4185 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
4186 * 100));
4187 if (TotalVisibleDeclContexts)
4188 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
4189 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
4190 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
4191 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00004192 if (TotalNumMethodPoolEntries) {
Douglas Gregor83941df2009-04-25 17:48:32 +00004193 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redlfa78dec2010-08-04 21:22:45 +00004194 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
4195 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor83941df2009-04-25 17:48:32 +00004196 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00004197 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor83941df2009-04-25 17:48:32 +00004198 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00004199 std::fprintf(stderr, "\n");
4200}
4201
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00004202/// Return the amount of memory used by memory buffers, breaking down
4203/// by heap-backed versus mmap'ed memory.
4204void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
4205 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
4206 if (llvm::MemoryBuffer *buf = Chain[i]->Buffer.get()) {
4207 size_t bytes = buf->getBufferSize();
4208 switch (buf->getBufferKind()) {
4209 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
4210 sizes.malloc_bytes += bytes;
4211 break;
4212 case llvm::MemoryBuffer::MemoryBuffer_MMap:
4213 sizes.mmap_bytes += bytes;
4214 break;
4215 }
4216 }
4217}
4218
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004219void ASTReader::InitializeSema(Sema &S) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00004220 SemaObj = &S;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00004221 S.ExternalSource = this;
4222
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00004223 // Makes sure any declarations that were deserialized "too early"
4224 // still get added to the identifier's declaration chains.
Douglas Gregor76dc8892010-09-24 23:29:12 +00004225 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
4226 if (SemaObj->TUScope)
John McCalld226f652010-08-21 09:40:31 +00004227 SemaObj->TUScope->AddDecl(PreloadedDecls[I]);
Douglas Gregor76dc8892010-09-24 23:29:12 +00004228
4229 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregor668c1a42009-04-21 22:25:48 +00004230 }
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00004231 PreloadedDecls.clear();
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00004232
4233 // If there were any tentative definitions, deserialize them and add
Sebastian Redle9d12b62010-01-31 22:27:38 +00004234 // them to Sema's list of tentative definitions.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00004235 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
4236 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
Sebastian Redle9d12b62010-01-31 22:27:38 +00004237 SemaObj->TentativeDefinitions.push_back(Var);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00004238 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00004239
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00004240 // If there were any unused file scoped decls, deserialize them and add to
4241 // Sema's list of unused file scoped decls.
4242 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
4243 DeclaratorDecl *D = cast<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
4244 SemaObj->UnusedFileScopedDecls.push_back(D);
Tanya Lattnere6bbc012010-02-12 00:07:30 +00004245 }
Douglas Gregor14c22f22009-04-22 22:18:58 +00004246
Sean Huntebcbe1d2011-05-04 23:29:54 +00004247 // If there were any delegating constructors, add them to Sema's list
4248 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
4249 CXXConstructorDecl *D
4250 = cast<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
4251 SemaObj->DelegatingCtorDecls.push_back(D);
4252 }
4253
Douglas Gregor14c22f22009-04-22 22:18:58 +00004254 // If there were any locally-scoped external declarations,
4255 // deserialize them and add them to Sema's table of locally-scoped
4256 // external declarations.
4257 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
4258 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
4259 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
4260 }
Douglas Gregorb81c1702009-04-27 20:06:05 +00004261
4262 // If there were any ext_vector type declarations, deserialize them
4263 // and add them to Sema's vector of such declarations.
4264 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I)
4265 SemaObj->ExtVectorDecls.push_back(
Richard Smith162e1c12011-04-15 14:24:37 +00004266 cast<TypedefNameDecl>(GetDecl(ExtVectorDecls[I])));
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004267
4268 // FIXME: Do VTable uses and dynamic classes deserialize too much ?
4269 // Can we cut them down before writing them ?
4270
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004271 // If there were any dynamic classes declarations, deserialize them
4272 // and add them to Sema's vector of such declarations.
4273 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I)
4274 SemaObj->DynamicClasses.push_back(
4275 cast<CXXRecordDecl>(GetDecl(DynamicClasses[I])));
Fariborz Jahanian32019832010-07-23 19:11:11 +00004276
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00004277 // Load the offsets of the declarations that Sema references.
4278 // They will be lazily deserialized when needed.
4279 if (!SemaDeclRefs.empty()) {
4280 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
4281 SemaObj->StdNamespace = SemaDeclRefs[0];
4282 SemaObj->StdBadAlloc = SemaDeclRefs[1];
4283 }
4284
Sebastian Redlc3632732010-10-05 15:59:54 +00004285 for (PerFileData *F = FirstInSource; F; F = F->NextInSource) {
4286
4287 // If there are @selector references added them to its pool. This is for
4288 // implementation of -Wselector.
4289 if (!F->ReferencedSelectorsData.empty()) {
4290 unsigned int DataSize = F->ReferencedSelectorsData.size()-1;
4291 unsigned I = 0;
4292 while (I < DataSize) {
4293 Selector Sel = DecodeSelector(F->ReferencedSelectorsData[I++]);
4294 SourceLocation SelLoc = ReadSourceLocation(
4295 *F, F->ReferencedSelectorsData, I);
4296 SemaObj->ReferencedSelectors.insert(std::make_pair(Sel, SelLoc));
4297 }
4298 }
Sebastian Redlc3632732010-10-05 15:59:54 +00004299 }
4300
Sebastian Redlc1d3ffb2011-04-24 16:27:30 +00004301 // The special data sets below always come from the most recent PCH,
Sebastian Redlc3632732010-10-05 15:59:54 +00004302 // which is at the front of the chain.
4303 PerFileData &F = *Chain.front();
4304
Sebastian Redlc1d3ffb2011-04-24 16:27:30 +00004305 // If there were any pending implicit instantiations, deserialize them
4306 // and add them to Sema's queue of such instantiations.
4307 assert(F.PendingInstantiations.size() % 2 == 0 &&
4308 "Expected pairs of entries");
4309 for (unsigned Idx = 0, N = F.PendingInstantiations.size(); Idx < N;) {
4310 ValueDecl *D=cast<ValueDecl>(GetDecl(F.PendingInstantiations[Idx++]));
4311 SourceLocation Loc = ReadSourceLocation(F, F.PendingInstantiations,Idx);
4312 SemaObj->PendingInstantiations.push_back(std::make_pair(D, Loc));
4313 }
4314
Sebastian Redlc3632732010-10-05 15:59:54 +00004315 // If there were any weak undeclared identifiers, deserialize them and add to
4316 // Sema's list of weak undeclared identifiers.
4317 if (!WeakUndeclaredIdentifiers.empty()) {
4318 unsigned Idx = 0;
4319 for (unsigned I = 0, N = WeakUndeclaredIdentifiers[Idx++]; I != N; ++I) {
4320 IdentifierInfo *WeakId = GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
4321 IdentifierInfo *AliasId= GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
4322 SourceLocation Loc = ReadSourceLocation(F, WeakUndeclaredIdentifiers,Idx);
4323 bool Used = WeakUndeclaredIdentifiers[Idx++];
4324 Sema::WeakInfo WI(AliasId, Loc);
4325 WI.setUsed(Used);
4326 SemaObj->WeakUndeclaredIdentifiers.insert(std::make_pair(WeakId, WI));
4327 }
4328 }
4329
4330 // If there were any VTable uses, deserialize the information and add it
4331 // to Sema's vector and map of VTable uses.
4332 if (!VTableUses.empty()) {
4333 unsigned Idx = 0;
4334 for (unsigned I = 0, N = VTableUses[Idx++]; I != N; ++I) {
4335 CXXRecordDecl *Class = cast<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
4336 SourceLocation Loc = ReadSourceLocation(F, VTableUses, Idx);
4337 bool DefinitionRequired = VTableUses[Idx++];
4338 SemaObj->VTableUses.push_back(std::make_pair(Class, Loc));
4339 SemaObj->VTablesUsed[Class] = DefinitionRequired;
Fariborz Jahanian32019832010-07-23 19:11:11 +00004340 }
4341 }
Peter Collingbourne84bccea2011-02-15 19:46:30 +00004342
4343 if (!FPPragmaOptions.empty()) {
4344 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
4345 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
4346 }
4347
4348 if (!OpenCLExtensions.empty()) {
4349 unsigned I = 0;
4350#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
4351#include "clang/Basic/OpenCLExtensions.def"
4352
4353 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
4354 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00004355}
4356
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004357IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Sebastian Redld8c5abb2010-08-02 18:30:12 +00004358 // Try to find this name within our on-disk hash tables. We start with the
4359 // most recent one, since that one contains the most up-to-date info.
Sebastian Redld27d3fc2010-07-21 22:31:37 +00004360 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004361 ASTIdentifierLookupTable *IdTable
4362 = (ASTIdentifierLookupTable *)Chain[I]->IdentifierLookupTable;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00004363 if (!IdTable)
4364 continue;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00004365 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004366 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00004367 if (Pos == IdTable->end())
4368 continue;
Douglas Gregor668c1a42009-04-21 22:25:48 +00004369
Sebastian Redld27d3fc2010-07-21 22:31:37 +00004370 // Dereferencing the iterator has the effect of building the
4371 // IdentifierInfo node and populating it with the various
4372 // declarations it needs.
Sebastian Redld8c5abb2010-08-02 18:30:12 +00004373 return *Pos;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00004374 }
Sebastian Redld8c5abb2010-08-02 18:30:12 +00004375 return 0;
Douglas Gregor668c1a42009-04-21 22:25:48 +00004376}
4377
Douglas Gregor95f42922010-10-14 22:11:03 +00004378namespace clang {
4379 /// \brief An identifier-lookup iterator that enumerates all of the
4380 /// identifiers stored within a set of AST files.
4381 class ASTIdentifierIterator : public IdentifierIterator {
4382 /// \brief The AST reader whose identifiers are being enumerated.
4383 const ASTReader &Reader;
4384
4385 /// \brief The current index into the chain of AST files stored in
4386 /// the AST reader.
4387 unsigned Index;
4388
4389 /// \brief The current position within the identifier lookup table
4390 /// of the current AST file.
4391 ASTIdentifierLookupTable::key_iterator Current;
4392
4393 /// \brief The end position within the identifier lookup table of
4394 /// the current AST file.
4395 ASTIdentifierLookupTable::key_iterator End;
4396
4397 public:
4398 explicit ASTIdentifierIterator(const ASTReader &Reader);
4399
4400 virtual llvm::StringRef Next();
4401 };
4402}
4403
4404ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
4405 : Reader(Reader), Index(Reader.Chain.size() - 1) {
4406 ASTIdentifierLookupTable *IdTable
4407 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
4408 Current = IdTable->key_begin();
4409 End = IdTable->key_end();
4410}
4411
4412llvm::StringRef ASTIdentifierIterator::Next() {
4413 while (Current == End) {
4414 // If we have exhausted all of our AST files, we're done.
4415 if (Index == 0)
4416 return llvm::StringRef();
4417
4418 --Index;
4419 ASTIdentifierLookupTable *IdTable
4420 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
4421 Current = IdTable->key_begin();
4422 End = IdTable->key_end();
4423 }
4424
4425 // We have any identifiers remaining in the current AST file; return
4426 // the next one.
4427 std::pair<const char*, unsigned> Key = *Current;
4428 ++Current;
4429 return llvm::StringRef(Key.first, Key.second);
4430}
4431
4432IdentifierIterator *ASTReader::getIdentifiers() const {
4433 return new ASTIdentifierIterator(*this);
4434}
4435
Mike Stump1eb44332009-09-09 15:08:12 +00004436std::pair<ObjCMethodList, ObjCMethodList>
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004437ASTReader::ReadMethodPool(Selector Sel) {
Sebastian Redl725cd962010-08-04 20:40:17 +00004438 // Find this selector in a hash table. We want to find the most recent entry.
4439 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4440 PerFileData &F = *Chain[I];
4441 if (!F.SelectorLookupTable)
4442 continue;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00004443
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004444 ASTSelectorLookupTable *PoolTable
4445 = (ASTSelectorLookupTable*)F.SelectorLookupTable;
4446 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
Sebastian Redl725cd962010-08-04 20:40:17 +00004447 if (Pos != PoolTable->end()) {
4448 ++NumSelectorsRead;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00004449 // FIXME: Not quite happy with the statistics here. We probably should
4450 // disable this tracking when called via LoadSelector.
4451 // Also, should entries without methods count as misses?
4452 ++NumMethodPoolEntriesRead;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004453 ASTSelectorLookupTrait::data_type Data = *Pos;
Sebastian Redl725cd962010-08-04 20:40:17 +00004454 if (DeserializationListener)
4455 DeserializationListener->SelectorRead(Data.ID, Sel);
4456 return std::make_pair(Data.Instance, Data.Factory);
4457 }
Douglas Gregor83941df2009-04-25 17:48:32 +00004458 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00004459
Sebastian Redlfa78dec2010-08-04 21:22:45 +00004460 ++NumMethodPoolMisses;
Sebastian Redl725cd962010-08-04 20:40:17 +00004461 return std::pair<ObjCMethodList, ObjCMethodList>();
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00004462}
4463
Douglas Gregord8bba9c2011-06-28 16:20:02 +00004464void ASTReader::ReadKnownNamespaces(
4465 llvm::SmallVectorImpl<NamespaceDecl *> &Namespaces) {
4466 Namespaces.clear();
4467
4468 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
4469 if (NamespaceDecl *Namespace
4470 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
4471 Namespaces.push_back(Namespace);
4472 }
4473}
4474
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004475void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redle58aa892010-08-04 18:21:41 +00004476 // It would be complicated to avoid reading the methods anyway. So don't.
4477 ReadMethodPool(Sel);
4478}
4479
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004480void ASTReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00004481 assert(ID && "Non-zero identifier ID required");
Douglas Gregora02b1472009-04-28 21:53:25 +00004482 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00004483 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004484 if (DeserializationListener)
4485 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregor668c1a42009-04-21 22:25:48 +00004486}
4487
Douglas Gregord89275b2009-07-06 18:54:52 +00004488/// \brief Set the globally-visible declarations associated with the given
4489/// identifier.
4490///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004491/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump1eb44332009-09-09 15:08:12 +00004492/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregord89275b2009-07-06 18:54:52 +00004493/// them.
4494///
4495/// \param II an IdentifierInfo that refers to one or more globally-visible
4496/// declarations.
4497///
4498/// \param DeclIDs the set of declaration IDs with the name @p II that are
4499/// visible at global scope.
4500///
4501/// \param Nonrecursive should be true to indicate that the caller knows that
4502/// this call is non-recursive, and therefore the globally-visible declarations
4503/// will not be placed onto the pending queue.
Mike Stump1eb44332009-09-09 15:08:12 +00004504void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004505ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Douglas Gregord89275b2009-07-06 18:54:52 +00004506 const llvm::SmallVectorImpl<uint32_t> &DeclIDs,
4507 bool Nonrecursive) {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004508 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregord89275b2009-07-06 18:54:52 +00004509 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
4510 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
4511 PII.II = II;
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00004512 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregord89275b2009-07-06 18:54:52 +00004513 return;
4514 }
Mike Stump1eb44332009-09-09 15:08:12 +00004515
Douglas Gregord89275b2009-07-06 18:54:52 +00004516 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
4517 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
4518 if (SemaObj) {
Douglas Gregor914ed9d2010-08-13 03:15:25 +00004519 if (SemaObj->TUScope) {
4520 // Introduce this declaration into the translation-unit scope
4521 // and add it to the declaration chain for this identifier, so
4522 // that (unqualified) name lookup will find it.
John McCalld226f652010-08-21 09:40:31 +00004523 SemaObj->TUScope->AddDecl(D);
Douglas Gregor914ed9d2010-08-13 03:15:25 +00004524 }
Douglas Gregor76dc8892010-09-24 23:29:12 +00004525 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
Douglas Gregord89275b2009-07-06 18:54:52 +00004526 } else {
4527 // Queue this declaration so that it will be added to the
4528 // translation unit scope and identifier's declaration chain
4529 // once a Sema object is known.
4530 PreloadedDecls.push_back(D);
4531 }
4532 }
4533}
4534
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004535IdentifierInfo *ASTReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00004536 if (ID == 0)
4537 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00004538
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00004539 if (IdentifiersLoaded.empty()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004540 Error("no identifier table in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00004541 return 0;
4542 }
Mike Stump1eb44332009-09-09 15:08:12 +00004543
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00004544 assert(PP && "Forgot to set Preprocessor ?");
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00004545 ID -= 1;
4546 if (!IdentifiersLoaded[ID]) {
4547 unsigned Index = ID;
4548 const char *Str = 0;
4549 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4550 PerFileData *F = Chain[N - I - 1];
4551 if (Index < F->LocalNumIdentifiers) {
4552 uint32_t Offset = F->IdentifierOffsets[Index];
4553 Str = F->IdentifierTableData + Offset;
4554 break;
4555 }
4556 Index -= F->LocalNumIdentifiers;
4557 }
4558 assert(Str && "Broken Chain");
Douglas Gregord6595a42009-04-25 21:04:17 +00004559
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004560 // All of the strings in the AST file are preceded by a 16-bit length.
4561 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenek231bc0b2009-10-23 04:45:31 +00004562 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
4563 // unsigned integers. This is important to avoid integer overflow when
4564 // we cast them to 'unsigned'.
Ted Kremenekff1ea462009-10-23 03:57:22 +00004565 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregor02fc7512009-04-28 20:01:51 +00004566 unsigned StrLen = (((unsigned) StrLenPtr[0])
4567 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00004568 IdentifiersLoaded[ID]
Jay Foad65aa6882011-06-21 15:13:30 +00004569 = &PP->getIdentifierTable().get(llvm::StringRef(Str, StrLen));
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004570 if (DeserializationListener)
4571 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregorafaf3082009-04-11 00:14:32 +00004572 }
Mike Stump1eb44332009-09-09 15:08:12 +00004573
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00004574 return IdentifiersLoaded[ID];
Douglas Gregor2cf26342009-04-09 22:27:44 +00004575}
4576
Douglas Gregore23ac652011-04-20 00:21:03 +00004577bool ASTReader::ReadSLocEntry(unsigned ID) {
4578 return ReadSLocEntryRecord(ID) != Success;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00004579}
4580
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004581Selector ASTReader::DecodeSelector(unsigned ID) {
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004582 if (ID == 0)
4583 return Selector();
Mike Stump1eb44332009-09-09 15:08:12 +00004584
Sebastian Redl725cd962010-08-04 20:40:17 +00004585 if (ID > SelectorsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004586 Error("selector ID out of range in AST file");
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004587 return Selector();
4588 }
Douglas Gregor83941df2009-04-25 17:48:32 +00004589
Sebastian Redl725cd962010-08-04 20:40:17 +00004590 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor83941df2009-04-25 17:48:32 +00004591 // Load this selector from the selector table.
Sebastian Redl725cd962010-08-04 20:40:17 +00004592 unsigned Idx = ID - 1;
4593 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4594 PerFileData &F = *Chain[N - I - 1];
4595 if (Idx < F.LocalNumSelectors) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004596 ASTSelectorLookupTrait Trait(*this);
Sebastian Redl725cd962010-08-04 20:40:17 +00004597 SelectorsLoaded[ID - 1] =
4598 Trait.ReadKey(F.SelectorLookupTableData + F.SelectorOffsets[Idx], 0);
4599 if (DeserializationListener)
4600 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
4601 break;
4602 }
4603 Idx -= F.LocalNumSelectors;
4604 }
Douglas Gregor83941df2009-04-25 17:48:32 +00004605 }
4606
Sebastian Redl725cd962010-08-04 20:40:17 +00004607 return SelectorsLoaded[ID - 1];
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004608}
4609
Michael J. Spencer20249a12010-10-21 03:16:25 +00004610Selector ASTReader::GetExternalSelector(uint32_t ID) {
Douglas Gregor719770d2010-04-06 17:30:22 +00004611 return DecodeSelector(ID);
4612}
4613
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004614uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redl725cd962010-08-04 20:40:17 +00004615 // ID 0 (the null selector) is considered an external selector.
4616 return getTotalNumSelectors() + 1;
Douglas Gregor719770d2010-04-06 17:30:22 +00004617}
4618
Mike Stump1eb44332009-09-09 15:08:12 +00004619DeclarationName
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004620ASTReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00004621 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
4622 switch (Kind) {
4623 case DeclarationName::Identifier:
4624 return DeclarationName(GetIdentifierInfo(Record, Idx));
4625
4626 case DeclarationName::ObjCZeroArgSelector:
4627 case DeclarationName::ObjCOneArgSelector:
4628 case DeclarationName::ObjCMultiArgSelector:
Steve Naroffa7503a72009-04-23 15:15:40 +00004629 return DeclarationName(GetSelector(Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004630
4631 case DeclarationName::CXXConstructorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00004632 return Context->DeclarationNames.getCXXConstructorName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00004633 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004634
4635 case DeclarationName::CXXDestructorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00004636 return Context->DeclarationNames.getCXXDestructorName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00004637 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004638
4639 case DeclarationName::CXXConversionFunctionName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00004640 return Context->DeclarationNames.getCXXConversionFunctionName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00004641 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004642
4643 case DeclarationName::CXXOperatorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00004644 return Context->DeclarationNames.getCXXOperatorName(
Douglas Gregor2cf26342009-04-09 22:27:44 +00004645 (OverloadedOperatorKind)Record[Idx++]);
4646
Sean Hunt3e518bd2009-11-29 07:34:05 +00004647 case DeclarationName::CXXLiteralOperatorName:
4648 return Context->DeclarationNames.getCXXLiteralOperatorName(
4649 GetIdentifierInfo(Record, Idx));
4650
Douglas Gregor2cf26342009-04-09 22:27:44 +00004651 case DeclarationName::CXXUsingDirective:
4652 return DeclarationName::getUsingDirectiveName();
4653 }
4654
4655 // Required to silence GCC warning
4656 return DeclarationName();
4657}
Douglas Gregor0a0428e2009-04-10 20:39:37 +00004658
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004659void ASTReader::ReadDeclarationNameLoc(PerFileData &F,
4660 DeclarationNameLoc &DNLoc,
4661 DeclarationName Name,
4662 const RecordData &Record, unsigned &Idx) {
4663 switch (Name.getNameKind()) {
4664 case DeclarationName::CXXConstructorName:
4665 case DeclarationName::CXXDestructorName:
4666 case DeclarationName::CXXConversionFunctionName:
4667 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
4668 break;
4669
4670 case DeclarationName::CXXOperatorName:
4671 DNLoc.CXXOperatorName.BeginOpNameLoc
4672 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4673 DNLoc.CXXOperatorName.EndOpNameLoc
4674 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4675 break;
4676
4677 case DeclarationName::CXXLiteralOperatorName:
4678 DNLoc.CXXLiteralOperatorName.OpNameLoc
4679 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4680 break;
4681
4682 case DeclarationName::Identifier:
4683 case DeclarationName::ObjCZeroArgSelector:
4684 case DeclarationName::ObjCOneArgSelector:
4685 case DeclarationName::ObjCMultiArgSelector:
4686 case DeclarationName::CXXUsingDirective:
4687 break;
4688 }
4689}
4690
4691void ASTReader::ReadDeclarationNameInfo(PerFileData &F,
4692 DeclarationNameInfo &NameInfo,
4693 const RecordData &Record, unsigned &Idx) {
4694 NameInfo.setName(ReadDeclarationName(Record, Idx));
4695 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
4696 DeclarationNameLoc DNLoc;
4697 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
4698 NameInfo.setInfo(DNLoc);
4699}
4700
4701void ASTReader::ReadQualifierInfo(PerFileData &F, QualifierInfo &Info,
4702 const RecordData &Record, unsigned &Idx) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004703 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004704 unsigned NumTPLists = Record[Idx++];
4705 Info.NumTemplParamLists = NumTPLists;
4706 if (NumTPLists) {
4707 Info.TemplParamLists = new (*Context) TemplateParameterList*[NumTPLists];
4708 for (unsigned i=0; i != NumTPLists; ++i)
4709 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
4710 }
4711}
4712
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004713TemplateName
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004714ASTReader::ReadTemplateName(PerFileData &F, const RecordData &Record,
4715 unsigned &Idx) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00004716 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004717 switch (Kind) {
4718 case TemplateName::Template:
4719 return TemplateName(cast_or_null<TemplateDecl>(GetDecl(Record[Idx++])));
4720
4721 case TemplateName::OverloadedTemplate: {
4722 unsigned size = Record[Idx++];
4723 UnresolvedSet<8> Decls;
4724 while (size--)
4725 Decls.addDecl(cast<NamedDecl>(GetDecl(Record[Idx++])));
4726
4727 return Context->getOverloadedTemplateName(Decls.begin(), Decls.end());
4728 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004729
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004730 case TemplateName::QualifiedTemplate: {
4731 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
4732 bool hasTemplKeyword = Record[Idx++];
4733 TemplateDecl *Template = cast<TemplateDecl>(GetDecl(Record[Idx++]));
4734 return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
4735 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004736
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004737 case TemplateName::DependentTemplate: {
4738 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
4739 if (Record[Idx++]) // isIdentifier
4740 return Context->getDependentTemplateName(NNS,
4741 GetIdentifierInfo(Record, Idx));
4742 return Context->getDependentTemplateName(NNS,
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004743 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004744 }
John McCall14606042011-06-30 08:33:18 +00004745
4746 case TemplateName::SubstTemplateTemplateParm: {
4747 TemplateTemplateParmDecl *param
4748 = cast_or_null<TemplateTemplateParmDecl>(GetDecl(Record[Idx++]));
4749 if (!param) return TemplateName();
4750 TemplateName replacement = ReadTemplateName(F, Record, Idx);
4751 return Context->getSubstTemplateTemplateParm(param, replacement);
4752 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004753
4754 case TemplateName::SubstTemplateTemplateParmPack: {
4755 TemplateTemplateParmDecl *Param
4756 = cast_or_null<TemplateTemplateParmDecl>(GetDecl(Record[Idx++]));
4757 if (!Param)
4758 return TemplateName();
4759
4760 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
4761 if (ArgPack.getKind() != TemplateArgument::Pack)
4762 return TemplateName();
4763
4764 return Context->getSubstTemplateTemplateParmPack(Param, ArgPack);
4765 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004766 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004767
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004768 assert(0 && "Unhandled template name kind!");
4769 return TemplateName();
4770}
4771
4772TemplateArgument
Sebastian Redlc3632732010-10-05 15:59:54 +00004773ASTReader::ReadTemplateArgument(PerFileData &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00004774 const RecordData &Record, unsigned &Idx) {
Douglas Gregora7fc9012011-01-05 18:58:31 +00004775 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
4776 switch (Kind) {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004777 case TemplateArgument::Null:
4778 return TemplateArgument();
4779 case TemplateArgument::Type:
4780 return TemplateArgument(GetType(Record[Idx++]));
4781 case TemplateArgument::Declaration:
4782 return TemplateArgument(GetDecl(Record[Idx++]));
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00004783 case TemplateArgument::Integral: {
4784 llvm::APSInt Value = ReadAPSInt(Record, Idx);
4785 QualType T = GetType(Record[Idx++]);
4786 return TemplateArgument(Value, T);
4787 }
Douglas Gregora7fc9012011-01-05 18:58:31 +00004788 case TemplateArgument::Template:
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004789 return TemplateArgument(ReadTemplateName(F, Record, Idx));
Douglas Gregora7fc9012011-01-05 18:58:31 +00004790 case TemplateArgument::TemplateExpansion: {
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004791 TemplateName Name = ReadTemplateName(F, Record, Idx);
Douglas Gregor2be29f42011-01-14 23:41:42 +00004792 llvm::Optional<unsigned> NumTemplateExpansions;
4793 if (unsigned NumExpansions = Record[Idx++])
4794 NumTemplateExpansions = NumExpansions - 1;
4795 return TemplateArgument(Name, NumTemplateExpansions);
Douglas Gregorba68eca2011-01-05 17:40:24 +00004796 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004797 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00004798 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004799 case TemplateArgument::Pack: {
4800 unsigned NumArgs = Record[Idx++];
Douglas Gregor910f8002010-11-07 23:05:16 +00004801 TemplateArgument *Args = new (*Context) TemplateArgument[NumArgs];
4802 for (unsigned I = 0; I != NumArgs; ++I)
4803 Args[I] = ReadTemplateArgument(F, Record, Idx);
4804 return TemplateArgument(Args, NumArgs);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004805 }
4806 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004807
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004808 assert(0 && "Unhandled template argument kind!");
4809 return TemplateArgument();
4810}
4811
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004812TemplateParameterList *
Sebastian Redlc3632732010-10-05 15:59:54 +00004813ASTReader::ReadTemplateParameterList(PerFileData &F,
4814 const RecordData &Record, unsigned &Idx) {
4815 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
4816 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
4817 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004818
4819 unsigned NumParams = Record[Idx++];
4820 llvm::SmallVector<NamedDecl *, 16> Params;
4821 Params.reserve(NumParams);
4822 while (NumParams--)
4823 Params.push_back(cast<NamedDecl>(GetDecl(Record[Idx++])));
Michael J. Spencer20249a12010-10-21 03:16:25 +00004824
4825 TemplateParameterList* TemplateParams =
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004826 TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc,
4827 Params.data(), Params.size(), RAngleLoc);
4828 return TemplateParams;
4829}
4830
4831void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004832ASTReader::
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004833ReadTemplateArgumentList(llvm::SmallVector<TemplateArgument, 8> &TemplArgs,
Sebastian Redlc3632732010-10-05 15:59:54 +00004834 PerFileData &F, const RecordData &Record,
4835 unsigned &Idx) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004836 unsigned NumTemplateArgs = Record[Idx++];
4837 TemplArgs.reserve(NumTemplateArgs);
4838 while (NumTemplateArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00004839 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004840}
4841
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004842/// \brief Read a UnresolvedSet structure.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004843void ASTReader::ReadUnresolvedSet(UnresolvedSetImpl &Set,
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004844 const RecordData &Record, unsigned &Idx) {
4845 unsigned NumDecls = Record[Idx++];
4846 while (NumDecls--) {
4847 NamedDecl *D = cast<NamedDecl>(GetDecl(Record[Idx++]));
4848 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
4849 Set.addDecl(D, AS);
4850 }
4851}
4852
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004853CXXBaseSpecifier
Sebastian Redlc3632732010-10-05 15:59:54 +00004854ASTReader::ReadCXXBaseSpecifier(PerFileData &F,
Nick Lewycky56062202010-07-26 16:56:01 +00004855 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004856 bool isVirtual = static_cast<bool>(Record[Idx++]);
4857 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
4858 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redlf677ea32011-02-05 19:23:19 +00004859 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00004860 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
4861 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00004862 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redlf677ea32011-02-05 19:23:19 +00004863 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
Douglas Gregorf90b27a2011-01-03 22:36:02 +00004864 EllipsisLoc);
Sebastian Redlf677ea32011-02-05 19:23:19 +00004865 Result.setInheritConstructors(inheritConstructors);
4866 return Result;
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004867}
4868
Sean Huntcbb67482011-01-08 20:30:50 +00004869std::pair<CXXCtorInitializer **, unsigned>
4870ASTReader::ReadCXXCtorInitializers(PerFileData &F, const RecordData &Record,
4871 unsigned &Idx) {
4872 CXXCtorInitializer **CtorInitializers = 0;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004873 unsigned NumInitializers = Record[Idx++];
4874 if (NumInitializers) {
4875 ASTContext &C = *getContext();
4876
Sean Huntcbb67482011-01-08 20:30:50 +00004877 CtorInitializers
4878 = new (C) CXXCtorInitializer*[NumInitializers];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004879 for (unsigned i=0; i != NumInitializers; ++i) {
4880 TypeSourceInfo *BaseClassInfo = 0;
4881 bool IsBaseVirtual = false;
4882 FieldDecl *Member = 0;
Francois Pichet00eb3f92010-12-04 09:14:42 +00004883 IndirectFieldDecl *IndirectMember = 0;
Sean Hunt156b6402011-05-04 01:19:08 +00004884 CXXConstructorDecl *Target = 0;
Michael J. Spencer20249a12010-10-21 03:16:25 +00004885
Sean Hunt156b6402011-05-04 01:19:08 +00004886 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
4887 switch (Type) {
4888 case CTOR_INITIALIZER_BASE:
Sebastian Redlc3632732010-10-05 15:59:54 +00004889 BaseClassInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004890 IsBaseVirtual = Record[Idx++];
Sean Hunt156b6402011-05-04 01:19:08 +00004891 break;
4892
4893 case CTOR_INITIALIZER_DELEGATING:
4894 Target = cast<CXXConstructorDecl>(GetDecl(Record[Idx++]));
4895 break;
4896
4897 case CTOR_INITIALIZER_MEMBER:
4898 Member = cast<FieldDecl>(GetDecl(Record[Idx++]));
4899 break;
4900
4901 case CTOR_INITIALIZER_INDIRECT_MEMBER:
4902 IndirectMember = cast<IndirectFieldDecl>(GetDecl(Record[Idx++]));
4903 break;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004904 }
Sean Hunt156b6402011-05-04 01:19:08 +00004905
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00004906 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redlc3632732010-10-05 15:59:54 +00004907 Expr *Init = ReadExpr(F);
Sebastian Redlc3632732010-10-05 15:59:54 +00004908 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
4909 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004910 bool IsWritten = Record[Idx++];
4911 unsigned SourceOrderOrNumArrayIndices;
4912 llvm::SmallVector<VarDecl *, 8> Indices;
4913 if (IsWritten) {
4914 SourceOrderOrNumArrayIndices = Record[Idx++];
4915 } else {
4916 SourceOrderOrNumArrayIndices = Record[Idx++];
4917 Indices.reserve(SourceOrderOrNumArrayIndices);
4918 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
4919 Indices.push_back(cast<VarDecl>(GetDecl(Record[Idx++])));
4920 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004921
Sean Huntcbb67482011-01-08 20:30:50 +00004922 CXXCtorInitializer *BOMInit;
Sean Hunt156b6402011-05-04 01:19:08 +00004923 if (Type == CTOR_INITIALIZER_BASE) {
Sean Huntcbb67482011-01-08 20:30:50 +00004924 BOMInit = new (C) CXXCtorInitializer(C, BaseClassInfo, IsBaseVirtual,
4925 LParenLoc, Init, RParenLoc,
4926 MemberOrEllipsisLoc);
Sean Hunt156b6402011-05-04 01:19:08 +00004927 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
4928 BOMInit = new (C) CXXCtorInitializer(C, MemberOrEllipsisLoc, LParenLoc,
4929 Target, Init, RParenLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004930 } else if (IsWritten) {
Francois Pichet00eb3f92010-12-04 09:14:42 +00004931 if (Member)
Sean Huntcbb67482011-01-08 20:30:50 +00004932 BOMInit = new (C) CXXCtorInitializer(C, Member, MemberOrEllipsisLoc,
4933 LParenLoc, Init, RParenLoc);
Francois Pichet00eb3f92010-12-04 09:14:42 +00004934 else
Sean Huntcbb67482011-01-08 20:30:50 +00004935 BOMInit = new (C) CXXCtorInitializer(C, IndirectMember,
4936 MemberOrEllipsisLoc, LParenLoc,
4937 Init, RParenLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004938 } else {
Sean Huntcbb67482011-01-08 20:30:50 +00004939 BOMInit = CXXCtorInitializer::Create(C, Member, MemberOrEllipsisLoc,
4940 LParenLoc, Init, RParenLoc,
4941 Indices.data(), Indices.size());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004942 }
4943
Argyrios Kyrtzidisf84cde12010-09-06 19:04:27 +00004944 if (IsWritten)
4945 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Sean Huntcbb67482011-01-08 20:30:50 +00004946 CtorInitializers[i] = BOMInit;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004947 }
4948 }
4949
Sean Huntcbb67482011-01-08 20:30:50 +00004950 return std::make_pair(CtorInitializers, NumInitializers);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004951}
4952
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004953NestedNameSpecifier *
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004954ASTReader::ReadNestedNameSpecifier(const RecordData &Record, unsigned &Idx) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004955 unsigned N = Record[Idx++];
4956 NestedNameSpecifier *NNS = 0, *Prev = 0;
4957 for (unsigned I = 0; I != N; ++I) {
4958 NestedNameSpecifier::SpecifierKind Kind
4959 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
4960 switch (Kind) {
4961 case NestedNameSpecifier::Identifier: {
4962 IdentifierInfo *II = GetIdentifierInfo(Record, Idx);
4963 NNS = NestedNameSpecifier::Create(*Context, Prev, II);
4964 break;
4965 }
4966
4967 case NestedNameSpecifier::Namespace: {
4968 NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++]));
4969 NNS = NestedNameSpecifier::Create(*Context, Prev, NS);
4970 break;
4971 }
4972
Douglas Gregor14aba762011-02-24 02:36:08 +00004973 case NestedNameSpecifier::NamespaceAlias: {
4974 NamespaceAliasDecl *Alias
4975 = cast<NamespaceAliasDecl>(GetDecl(Record[Idx++]));
4976 NNS = NestedNameSpecifier::Create(*Context, Prev, Alias);
4977 break;
4978 }
4979
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004980 case NestedNameSpecifier::TypeSpec:
4981 case NestedNameSpecifier::TypeSpecWithTemplate: {
John McCallf4c73712011-01-19 06:33:43 +00004982 const Type *T = GetType(Record[Idx++]).getTypePtrOrNull();
Douglas Gregor1ab55e92010-12-10 17:03:06 +00004983 if (!T)
4984 return 0;
4985
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004986 bool Template = Record[Idx++];
4987 NNS = NestedNameSpecifier::Create(*Context, Prev, Template, T);
4988 break;
4989 }
4990
4991 case NestedNameSpecifier::Global: {
4992 NNS = NestedNameSpecifier::GlobalSpecifier(*Context);
4993 // No associated value, and there can't be a prefix.
4994 break;
4995 }
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004996 }
Argyrios Kyrtzidisd2bb2c02010-07-07 15:46:30 +00004997 Prev = NNS;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004998 }
4999 return NNS;
5000}
5001
Douglas Gregordc355712011-02-25 00:36:19 +00005002NestedNameSpecifierLoc
5003ASTReader::ReadNestedNameSpecifierLoc(PerFileData &F, const RecordData &Record,
5004 unsigned &Idx) {
5005 unsigned N = Record[Idx++];
Douglas Gregor5f791bb2011-02-28 23:58:31 +00005006 NestedNameSpecifierLocBuilder Builder;
Douglas Gregordc355712011-02-25 00:36:19 +00005007 for (unsigned I = 0; I != N; ++I) {
5008 NestedNameSpecifier::SpecifierKind Kind
5009 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
5010 switch (Kind) {
5011 case NestedNameSpecifier::Identifier: {
Douglas Gregor5f791bb2011-02-28 23:58:31 +00005012 IdentifierInfo *II = GetIdentifierInfo(Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00005013 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor5f791bb2011-02-28 23:58:31 +00005014 Builder.Extend(*Context, II, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00005015 break;
5016 }
5017
5018 case NestedNameSpecifier::Namespace: {
Douglas Gregordc355712011-02-25 00:36:19 +00005019 NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++]));
Douglas Gregordc355712011-02-25 00:36:19 +00005020 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor5f791bb2011-02-28 23:58:31 +00005021 Builder.Extend(*Context, NS, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00005022 break;
5023 }
5024
5025 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregordc355712011-02-25 00:36:19 +00005026 NamespaceAliasDecl *Alias
5027 = cast<NamespaceAliasDecl>(GetDecl(Record[Idx++]));
Douglas Gregordc355712011-02-25 00:36:19 +00005028 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor5f791bb2011-02-28 23:58:31 +00005029 Builder.Extend(*Context, Alias, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00005030 break;
5031 }
5032
5033 case NestedNameSpecifier::TypeSpec:
5034 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregordc355712011-02-25 00:36:19 +00005035 bool Template = Record[Idx++];
5036 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
5037 if (!T)
5038 return NestedNameSpecifierLoc();
Douglas Gregordc355712011-02-25 00:36:19 +00005039 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor5f791bb2011-02-28 23:58:31 +00005040
5041 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
5042 Builder.Extend(*Context,
5043 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
5044 T->getTypeLoc(), ColonColonLoc);
Douglas Gregordc355712011-02-25 00:36:19 +00005045 break;
5046 }
5047
5048 case NestedNameSpecifier::Global: {
Douglas Gregordc355712011-02-25 00:36:19 +00005049 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor5f791bb2011-02-28 23:58:31 +00005050 Builder.MakeGlobal(*Context, ColonColonLoc);
Douglas Gregordc355712011-02-25 00:36:19 +00005051 break;
5052 }
5053 }
Douglas Gregordc355712011-02-25 00:36:19 +00005054 }
5055
Douglas Gregor5f791bb2011-02-28 23:58:31 +00005056 return Builder.getWithLocInContext(*Context);
Douglas Gregordc355712011-02-25 00:36:19 +00005057}
5058
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005059SourceRange
Sebastian Redlc3632732010-10-05 15:59:54 +00005060ASTReader::ReadSourceRange(PerFileData &F, const RecordData &Record,
5061 unsigned &Idx) {
5062 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
5063 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar8ee59392010-06-02 15:47:10 +00005064 return SourceRange(beg, end);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005065}
5066
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00005067/// \brief Read an integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005068llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00005069 unsigned BitWidth = Record[Idx++];
5070 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
5071 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
5072 Idx += NumWords;
5073 return Result;
5074}
5075
5076/// \brief Read a signed integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005077llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00005078 bool isUnsigned = Record[Idx++];
5079 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
5080}
5081
Douglas Gregor17fc2232009-04-14 21:55:33 +00005082/// \brief Read a floating-point value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005083llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00005084 return llvm::APFloat(ReadAPInt(Record, Idx));
5085}
5086
Douglas Gregor68a2eb02009-04-15 21:30:51 +00005087// \brief Read a string
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005088std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00005089 unsigned Len = Record[Idx++];
Jay Foadbeaaccd2009-05-21 09:52:38 +00005090 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00005091 Idx += Len;
5092 return Result;
5093}
5094
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005095VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
5096 unsigned &Idx) {
5097 unsigned Major = Record[Idx++];
5098 unsigned Minor = Record[Idx++];
5099 unsigned Subminor = Record[Idx++];
5100 if (Minor == 0)
5101 return VersionTuple(Major);
5102 if (Subminor == 0)
5103 return VersionTuple(Major, Minor - 1);
5104 return VersionTuple(Major, Minor - 1, Subminor - 1);
5105}
5106
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005107CXXTemporary *ASTReader::ReadCXXTemporary(const RecordData &Record,
Chris Lattnerd2598362010-05-10 00:25:06 +00005108 unsigned &Idx) {
5109 CXXDestructorDecl *Decl = cast<CXXDestructorDecl>(GetDecl(Record[Idx++]));
5110 return CXXTemporary::Create(*Context, Decl);
5111}
5112
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005113DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00005114 return Diag(SourceLocation(), DiagID);
5115}
5116
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005117DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00005118 return Diags.Report(Loc, DiagID);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00005119}
Douglas Gregor025452f2009-04-17 00:04:06 +00005120
Douglas Gregor668c1a42009-04-21 22:25:48 +00005121/// \brief Retrieve the identifier table associated with the
5122/// preprocessor.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005123IdentifierTable &ASTReader::getIdentifierTable() {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00005124 assert(PP && "Forgot to set Preprocessor ?");
5125 return PP->getIdentifierTable();
Douglas Gregor668c1a42009-04-21 22:25:48 +00005126}
5127
Douglas Gregor025452f2009-04-17 00:04:06 +00005128/// \brief Record that the given ID maps to the given switch-case
5129/// statement.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005130void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Douglas Gregor025452f2009-04-17 00:04:06 +00005131 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
5132 SwitchCaseStmts[ID] = SC;
5133}
5134
5135/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005136SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Douglas Gregor025452f2009-04-17 00:04:06 +00005137 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
5138 return SwitchCaseStmts[ID];
5139}
Douglas Gregor1de05fe2009-04-17 18:18:49 +00005140
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00005141void ASTReader::ClearSwitchCaseIDs() {
5142 SwitchCaseStmts.clear();
5143}
5144
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005145void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00005146 assert(NumCurrentElementsDeserializing &&
5147 "FinishedDeserializing not paired with StartedDeserializing");
5148 if (NumCurrentElementsDeserializing == 1) {
Douglas Gregord89275b2009-07-06 18:54:52 +00005149 // If any identifiers with corresponding top-level declarations have
5150 // been loaded, load those declarations now.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00005151 while (!PendingIdentifierInfos.empty()) {
5152 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
5153 PendingIdentifierInfos.front().DeclIDs, true);
5154 PendingIdentifierInfos.pop_front();
Douglas Gregord89275b2009-07-06 18:54:52 +00005155 }
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005156
Argyrios Kyrtzidis0895d152011-02-12 07:50:47 +00005157 // Ready to load previous declarations of Decls that were delayed.
5158 while (!PendingPreviousDecls.empty()) {
5159 loadAndAttachPreviousDecl(PendingPreviousDecls.front().first,
5160 PendingPreviousDecls.front().second);
5161 PendingPreviousDecls.pop_front();
5162 }
5163
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005164 // We are not in recursive loading, so it's safe to pass the "interesting"
5165 // decls to the consumer.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00005166 if (Consumer)
5167 PassInterestingDeclsToConsumer();
Argyrios Kyrtzidis134db1f2010-10-24 17:26:31 +00005168
5169 assert(PendingForwardRefs.size() == 0 &&
5170 "Some forward refs did not get linked to the definition!");
Douglas Gregord89275b2009-07-06 18:54:52 +00005171 }
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00005172 --NumCurrentElementsDeserializing;
Douglas Gregord89275b2009-07-06 18:54:52 +00005173}
Douglas Gregor501c1032010-08-19 00:28:17 +00005174
Sebastian Redle1dde812010-08-24 00:50:04 +00005175ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context,
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00005176 const char *isysroot, bool DisableValidation,
5177 bool DisableStatCache)
Sebastian Redle1dde812010-08-24 00:50:04 +00005178 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
5179 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
5180 Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context),
5181 Consumer(0), isysroot(isysroot), DisableValidation(DisableValidation),
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00005182 DisableStatCache(DisableStatCache), NumStatHits(0), NumStatMisses(0),
5183 NumSLocEntriesRead(0), TotalNumSLocEntries(0), NextSLocOffset(0),
5184 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
5185 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
5186 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
5187 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
5188 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
5189 NumCurrentElementsDeserializing(0)
5190{
Sebastian Redle1dde812010-08-24 00:50:04 +00005191 RelocatablePCH = false;
5192}
5193
5194ASTReader::ASTReader(SourceManager &SourceMgr, FileManager &FileMgr,
5195 Diagnostic &Diags, const char *isysroot,
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00005196 bool DisableValidation, bool DisableStatCache)
Sebastian Redle1dde812010-08-24 00:50:04 +00005197 : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr),
5198 Diags(Diags), SemaObj(0), PP(0), Context(0), Consumer(0),
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00005199 isysroot(isysroot), DisableValidation(DisableValidation),
5200 DisableStatCache(DisableStatCache), NumStatHits(0), NumStatMisses(0),
5201 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Sebastian Redl8db9fae2010-09-22 20:19:08 +00005202 NextSLocOffset(0), NumStatementsRead(0), TotalNumStatements(0),
5203 NumMacrosRead(0), TotalNumMacros(0), NumSelectorsRead(0),
5204 NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
5205 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
5206 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
5207 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
Sebastian Redle1dde812010-08-24 00:50:04 +00005208 RelocatablePCH = false;
5209}
5210
5211ASTReader::~ASTReader() {
5212 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
5213 delete Chain[e - i - 1];
5214 // Delete all visible decl lookup tables
5215 for (DeclContextOffsetsMap::iterator I = DeclContextOffsets.begin(),
5216 E = DeclContextOffsets.end();
5217 I != E; ++I) {
5218 for (DeclContextInfos::iterator J = I->second.begin(), F = I->second.end();
5219 J != F; ++J) {
5220 if (J->NameLookupTableData)
5221 delete static_cast<ASTDeclContextNameLookupTable*>(
5222 J->NameLookupTableData);
5223 }
5224 }
5225 for (DeclContextVisibleUpdatesPending::iterator
5226 I = PendingVisibleUpdates.begin(),
5227 E = PendingVisibleUpdates.end();
5228 I != E; ++I) {
5229 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
5230 F = I->second.end();
5231 J != F; ++J)
5232 delete static_cast<ASTDeclContextNameLookupTable*>(*J);
5233 }
5234}
5235
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00005236ASTReader::PerFileData::PerFileData(ASTFileType Ty)
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00005237 : Type(Ty), SizeInBits(0), LocalNumSLocEntries(0), SLocOffsets(0),
5238 SLocFileOffsets(0), LocalSLocSize(0),
Sebastian Redl301c9b02010-09-22 00:42:27 +00005239 LocalNumIdentifiers(0), IdentifierOffsets(0), IdentifierTableData(0),
5240 IdentifierLookupTable(0), LocalNumMacroDefinitions(0),
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00005241 MacroDefinitionOffsets(0),
5242 LocalNumHeaderFileInfos(0), HeaderFileInfoTableData(0),
5243 HeaderFileInfoTable(0),
5244 LocalNumSelectors(0), SelectorOffsets(0),
Sebastian Redl301c9b02010-09-22 00:42:27 +00005245 SelectorLookupTableData(0), SelectorLookupTable(0), LocalNumDecls(0),
Douglas Gregor7c789c12010-10-29 22:39:52 +00005246 DeclOffsets(0), LocalNumCXXBaseSpecifiers(0), CXXBaseSpecifiersOffsets(0),
5247 LocalNumTypes(0), TypeOffsets(0), StatCache(0),
Sebastian Redla866e652010-10-01 19:59:12 +00005248 NumPreallocatedPreprocessingEntities(0), NextInSource(0)
Douglas Gregor501c1032010-08-19 00:28:17 +00005249{}
5250
5251ASTReader::PerFileData::~PerFileData() {
5252 delete static_cast<ASTIdentifierLookupTable *>(IdentifierLookupTable);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00005253 delete static_cast<HeaderFileInfoLookupTable *>(HeaderFileInfoTable);
Douglas Gregor501c1032010-08-19 00:28:17 +00005254 delete static_cast<ASTSelectorLookupTable *>(SelectorLookupTable);
5255}