blob: 66b4890d2a86705c76fef899512bf2aa3403e8e6 [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);
Michael J. Spencer20249a12010-10-21 03:16:25 +000095 PARSE_LANGOPT_IMPORTANT(NoConstantCFStrings,
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +000096 diag::warn_pch_no_constant_cfstrings);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000097 PARSE_LANGOPT_BENIGN(PascalStrings);
98 PARSE_LANGOPT_BENIGN(WritableStrings);
Mike Stump1eb44332009-09-09 15:08:12 +000099 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000100 diag::warn_pch_lax_vector_conversions);
Nate Begeman69cfb9b2009-06-25 22:57:40 +0000101 PARSE_LANGOPT_IMPORTANT(AltiVec, diag::warn_pch_altivec);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000102 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
Anders Carlssonda4b7cf2011-02-19 23:53:54 +0000103 PARSE_LANGOPT_IMPORTANT(ObjCExceptions, diag::warn_pch_objc_exceptions);
Anders Carlsson7da99b02011-02-23 03:04:54 +0000104 PARSE_LANGOPT_IMPORTANT(CXXExceptions, diag::warn_pch_cxx_exceptions);
105 PARSE_LANGOPT_IMPORTANT(SjLjExceptions, diag::warn_pch_sjlj_exceptions);
Douglas Gregor6f755502011-02-01 15:15:22 +0000106 PARSE_LANGOPT_IMPORTANT(MSBitfields, diag::warn_pch_ms_bitfields);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000107 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
108 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
109 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
Mike Stump1eb44332009-09-09 15:08:12 +0000110 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000111 diag::warn_pch_thread_safe_statics);
Daniel Dunbar5345c392009-09-03 04:54:28 +0000112 PARSE_LANGOPT_IMPORTANT(POSIXThreads, diag::warn_pch_posix_threads);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000113 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
114 PARSE_LANGOPT_BENIGN(EmitAllDecls);
115 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
Chris Lattnera4d71452010-06-26 21:25:03 +0000116 PARSE_LANGOPT_BENIGN(getSignedOverflowBehavior());
Mike Stump1eb44332009-09-09 15:08:12 +0000117 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000118 diag::warn_pch_heinous_extensions);
119 // FIXME: Most of the options below are benign if the macro wasn't
120 // used. Unfortunately, this means that a PCH compiled without
121 // optimization can't be used with optimization turned on, even
122 // though the only thing that changes is whether __OPTIMIZE__ was
123 // defined... but if __OPTIMIZE__ never showed up in the header, it
124 // doesn't matter. We could consider making this some special kind
125 // of check.
126 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
127 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
128 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
129 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
130 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
131 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
132 PARSE_LANGOPT_IMPORTANT(AccessControl, diag::warn_pch_access_control);
133 PARSE_LANGOPT_IMPORTANT(CharIsSigned, diag::warn_pch_char_signed);
John Thompsona6fda122009-11-05 20:14:16 +0000134 PARSE_LANGOPT_IMPORTANT(ShortWChar, diag::warn_pch_short_wchar);
Argyrios Kyrtzidis9a2b9d72010-10-08 00:25:19 +0000135 PARSE_LANGOPT_IMPORTANT(ShortEnums, diag::warn_pch_short_enums);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000136 if ((PPLangOpts.getGCMode() != 0) != (LangOpts.getGCMode() != 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000137 Reader.Diag(diag::warn_pch_gc_mode)
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000138 << LangOpts.getGCMode() << PPLangOpts.getGCMode();
139 return true;
140 }
141 PARSE_LANGOPT_BENIGN(getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000142 PARSE_LANGOPT_IMPORTANT(getStackProtectorMode(),
143 diag::warn_pch_stack_protector);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000144 PARSE_LANGOPT_BENIGN(InstantiationDepth);
Nate Begeman69cfb9b2009-06-25 22:57:40 +0000145 PARSE_LANGOPT_IMPORTANT(OpenCL, diag::warn_pch_opencl);
Peter Collingbourne08a53262010-12-01 19:14:57 +0000146 PARSE_LANGOPT_IMPORTANT(CUDA, diag::warn_pch_cuda);
Mike Stump9c276ae2009-12-12 01:27:46 +0000147 PARSE_LANGOPT_BENIGN(CatchUndefined);
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000148 PARSE_LANGOPT_IMPORTANT(ElideConstructors, diag::warn_pch_elide_constructors);
Douglas Gregora0068fc2010-07-09 17:35:33 +0000149 PARSE_LANGOPT_BENIGN(SpellChecking);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000150 PARSE_LANGOPT_BENIGN(DefaultFPContract);
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +0000151#undef PARSE_LANGOPT_IMPORTANT
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000152#undef PARSE_LANGOPT_BENIGN
153
154 return false;
155}
156
Daniel Dunbardc3c0d22009-11-11 00:52:11 +0000157bool PCHValidator::ReadTargetTriple(llvm::StringRef Triple) {
158 if (Triple == PP.getTargetInfo().getTriple().str())
159 return false;
160
161 Reader.Diag(diag::warn_pch_target_triple)
162 << Triple << PP.getTargetInfo().getTriple().str();
163 return true;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000164}
165
Benjamin Kramer54353f42010-11-25 18:29:30 +0000166namespace {
167 struct EmptyStringRef {
168 bool operator ()(llvm::StringRef r) const { return r.empty(); }
169 };
170 struct EmptyBlock {
171 bool operator ()(const PCHPredefinesBlock &r) const {return r.Data.empty();}
172 };
173}
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000174
175static bool EqualConcatenations(llvm::SmallVector<llvm::StringRef, 2> L,
176 PCHPredefinesBlocks R) {
177 // First, sum up the lengths.
178 unsigned LL = 0, RL = 0;
179 for (unsigned I = 0, N = L.size(); I != N; ++I) {
180 LL += L[I].size();
181 }
182 for (unsigned I = 0, N = R.size(); I != N; ++I) {
183 RL += R[I].Data.size();
184 }
185 if (LL != RL)
186 return false;
187 if (LL == 0 && RL == 0)
188 return true;
189
190 // Kick out empty parts, they confuse the algorithm below.
191 L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end());
192 R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end());
193
194 // Do it the hard way. At this point, both vectors must be non-empty.
195 llvm::StringRef LR = L[0], RR = R[0].Data;
196 unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size();
Daniel Dunbarc76c9e02010-07-16 00:00:11 +0000197 (void) RN;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000198 for (;;) {
199 // Compare the current pieces.
200 if (LR.size() == RR.size()) {
201 // If they're the same length, it's pretty easy.
202 if (LR != RR)
203 return false;
204 // Both pieces are done, advance.
205 ++LI;
206 ++RI;
207 // If either string is done, they're both done, since they're the same
208 // length.
209 if (LI == LN) {
210 assert(RI == RN && "Strings not the same length after all?");
211 return true;
212 }
213 LR = L[LI];
214 RR = R[RI].Data;
215 } else if (LR.size() < RR.size()) {
216 // Right piece is longer.
217 if (!RR.startswith(LR))
218 return false;
219 ++LI;
220 assert(LI != LN && "Strings not the same length after all?");
221 RR = RR.substr(LR.size());
222 LR = L[LI];
223 } else {
224 // Left piece is longer.
225 if (!LR.startswith(RR))
226 return false;
227 ++RI;
228 assert(RI != RN && "Strings not the same length after all?");
229 LR = LR.substr(RR.size());
230 RR = R[RI].Data;
231 }
232 }
233}
234
235static std::pair<FileID, llvm::StringRef::size_type>
236FindMacro(const PCHPredefinesBlocks &Buffers, llvm::StringRef MacroDef) {
237 std::pair<FileID, llvm::StringRef::size_type> Res;
238 for (unsigned I = 0, N = Buffers.size(); I != N; ++I) {
239 Res.second = Buffers[I].Data.find(MacroDef);
240 if (Res.second != llvm::StringRef::npos) {
241 Res.first = Buffers[I].BufferID;
242 break;
243 }
244 }
245 return Res;
246}
247
248bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000249 llvm::StringRef OriginalFileName,
Nick Lewycky277a6e72011-02-23 21:16:44 +0000250 std::string &SuggestedPredefines,
251 FileManager &FileMgr) {
Daniel Dunbarc7162932009-11-11 23:58:53 +0000252 // We are in the context of an implicit include, so the predefines buffer will
253 // have a #include entry for the PCH file itself (as normalized by the
254 // preprocessor initialization). Find it and skip over it in the checking
255 // below.
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000256 llvm::SmallString<256> PCHInclude;
257 PCHInclude += "#include \"";
Nick Lewycky277a6e72011-02-23 21:16:44 +0000258 PCHInclude += NormalizeDashIncludePath(OriginalFileName, FileMgr);
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000259 PCHInclude += "\"\n";
260 std::pair<llvm::StringRef,llvm::StringRef> Split =
261 llvm::StringRef(PP.getPredefines()).split(PCHInclude.str());
262 llvm::StringRef Left = Split.first, Right = Split.second;
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000263 if (Left == PP.getPredefines()) {
264 Error("Missing PCH include entry!");
265 return true;
266 }
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000267
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000268 // If the concatenation of all the PCH buffers is equal to the adjusted
269 // command line, we're done.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000270 llvm::SmallVector<llvm::StringRef, 2> CommandLine;
271 CommandLine.push_back(Left);
272 CommandLine.push_back(Right);
273 if (EqualConcatenations(CommandLine, Buffers))
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000274 return false;
275
276 SourceManager &SourceMgr = PP.getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000277
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000278 // The predefines buffers are different. Determine what the differences are,
279 // and whether they require us to reject the PCH file.
Daniel Dunbare6750492009-11-13 16:46:11 +0000280 llvm::SmallVector<llvm::StringRef, 8> PCHLines;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000281 for (unsigned I = 0, N = Buffers.size(); I != N; ++I)
282 Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Daniel Dunbare6750492009-11-13 16:46:11 +0000283
284 llvm::SmallVector<llvm::StringRef, 8> CmdLineLines;
285 Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000286
287 // Pick out implicit #includes after the PCH and don't consider them for
288 // validation; we will insert them into SuggestedPredefines so that the
289 // preprocessor includes them.
290 std::string IncludesAfterPCH;
291 llvm::SmallVector<llvm::StringRef, 8> AfterPCHLines;
292 Right.split(AfterPCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
293 for (unsigned i = 0, e = AfterPCHLines.size(); i != e; ++i) {
294 if (AfterPCHLines[i].startswith("#include ")) {
295 IncludesAfterPCH += AfterPCHLines[i];
296 IncludesAfterPCH += '\n';
297 } else {
298 CmdLineLines.push_back(AfterPCHLines[i]);
299 }
300 }
301
302 // Make sure we add the includes last into SuggestedPredefines before we
303 // exit this function.
304 struct AddIncludesRAII {
305 std::string &SuggestedPredefines;
306 std::string &IncludesAfterPCH;
307
308 AddIncludesRAII(std::string &SuggestedPredefines,
309 std::string &IncludesAfterPCH)
310 : SuggestedPredefines(SuggestedPredefines),
311 IncludesAfterPCH(IncludesAfterPCH) { }
312 ~AddIncludesRAII() {
313 SuggestedPredefines += IncludesAfterPCH;
314 }
315 } AddIncludes(SuggestedPredefines, IncludesAfterPCH);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000316
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000317 // Sort both sets of predefined buffer lines, since we allow some extra
318 // definitions and they may appear at any point in the output.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000319 std::sort(CmdLineLines.begin(), CmdLineLines.end());
320 std::sort(PCHLines.begin(), PCHLines.end());
321
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000322 // Determine which predefines that were used to build the PCH file are missing
323 // from the command line.
324 std::vector<llvm::StringRef> MissingPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000325 std::set_difference(PCHLines.begin(), PCHLines.end(),
326 CmdLineLines.begin(), CmdLineLines.end(),
327 std::back_inserter(MissingPredefines));
328
329 bool MissingDefines = false;
330 bool ConflictingDefines = false;
331 for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) {
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000332 llvm::StringRef Missing = MissingPredefines[I];
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000333 if (Missing.startswith("#include ")) {
334 // An -include was specified when generating the PCH; it is included in
335 // the PCH, just ignore it.
336 continue;
337 }
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000338 if (!Missing.startswith("#define ")) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000339 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
340 return true;
341 }
Mike Stump1eb44332009-09-09 15:08:12 +0000342
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000343 // This is a macro definition. Determine the name of the macro we're
344 // defining.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000345 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000346 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000347 = Missing.find_first_of("( \n\r", StartOfMacroName);
348 assert(EndOfMacroName != std::string::npos &&
349 "Couldn't find the end of the macro name");
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000350 llvm::StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000351
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000352 // Determine whether this macro was given a different definition on the
353 // command line.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000354 std::string MacroDefStart = "#define " + MacroName.str();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000355 std::string::size_type MacroDefLen = MacroDefStart.size();
Daniel Dunbare6750492009-11-13 16:46:11 +0000356 llvm::SmallVector<llvm::StringRef, 8>::iterator ConflictPos
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000357 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
358 MacroDefStart);
359 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000360 if (!ConflictPos->startswith(MacroDefStart)) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000361 // Different macro; we're done.
362 ConflictPos = CmdLineLines.end();
Mike Stump1eb44332009-09-09 15:08:12 +0000363 break;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000364 }
Mike Stump1eb44332009-09-09 15:08:12 +0000365
366 assert(ConflictPos->size() > MacroDefLen &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000367 "Invalid #define in predefines buffer?");
Mike Stump1eb44332009-09-09 15:08:12 +0000368 if ((*ConflictPos)[MacroDefLen] != ' ' &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000369 (*ConflictPos)[MacroDefLen] != '(')
370 continue; // Longer macro name; keep trying.
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000372 // We found a conflicting macro definition.
373 break;
374 }
Mike Stump1eb44332009-09-09 15:08:12 +0000375
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000376 if (ConflictPos != CmdLineLines.end()) {
377 Reader.Diag(diag::warn_cmdline_conflicting_macro_def)
378 << MacroName;
379
380 // Show the definition of this macro within the PCH file.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000381 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
382 FindMacro(Buffers, Missing);
383 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
384 SourceLocation PCHMissingLoc =
385 SourceMgr.getLocForStartOfFile(MacroLoc.first)
386 .getFileLocWithOffset(MacroLoc.second);
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000387 Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000388
389 ConflictingDefines = true;
390 continue;
391 }
Mike Stump1eb44332009-09-09 15:08:12 +0000392
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000393 // If the macro doesn't conflict, then we'll just pick up the macro
394 // definition from the PCH file. Warn the user that they made a mistake.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000395 if (ConflictingDefines)
396 continue; // Don't complain if there are already conflicting defs
Mike Stump1eb44332009-09-09 15:08:12 +0000397
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000398 if (!MissingDefines) {
399 Reader.Diag(diag::warn_cmdline_missing_macro_defs);
400 MissingDefines = true;
401 }
402
403 // Show the definition of this macro within the PCH file.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000404 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
405 FindMacro(Buffers, Missing);
406 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
407 SourceLocation PCHMissingLoc =
408 SourceMgr.getLocForStartOfFile(MacroLoc.first)
409 .getFileLocWithOffset(MacroLoc.second);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000410 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
411 }
Mike Stump1eb44332009-09-09 15:08:12 +0000412
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000413 if (ConflictingDefines)
414 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000415
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000416 // Determine what predefines were introduced based on command-line
417 // parameters that were not present when building the PCH
418 // file. Extra #defines are okay, so long as the identifiers being
419 // defined were not used within the precompiled header.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000420 std::vector<llvm::StringRef> ExtraPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000421 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
422 PCHLines.begin(), PCHLines.end(),
Mike Stump1eb44332009-09-09 15:08:12 +0000423 std::back_inserter(ExtraPredefines));
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000424 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000425 llvm::StringRef &Extra = ExtraPredefines[I];
426 if (!Extra.startswith("#define ")) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000427 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
428 return true;
429 }
430
431 // This is an extra macro definition. Determine the name of the
432 // macro we're defining.
433 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000434 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000435 = Extra.find_first_of("( \n\r", StartOfMacroName);
436 assert(EndOfMacroName != std::string::npos &&
437 "Couldn't find the end of the macro name");
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000438 llvm::StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000439
440 // Check whether this name was used somewhere in the PCH file. If
441 // so, defining it as a macro could change behavior, so we reject
442 // the PCH file.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000443 if (IdentifierInfo *II = Reader.get(MacroName)) {
Daniel Dunbar4fda42e2009-11-11 00:52:00 +0000444 Reader.Diag(diag::warn_macro_name_used_in_pch) << II;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000445 return true;
446 }
447
448 // Add this definition to the suggested predefines buffer.
449 SuggestedPredefines += Extra;
450 SuggestedPredefines += '\n';
451 }
452
453 // If we get here, it's because the predefines buffer had compatible
454 // contents. Accept the PCH file.
455 return false;
456}
457
Douglas Gregor12fab312010-03-16 16:35:32 +0000458void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
459 unsigned ID) {
460 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
461 ++NumHeaderInfos;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000462}
463
464void PCHValidator::ReadCounter(unsigned Value) {
465 PP.setCounterValue(Value);
466}
467
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000468//===----------------------------------------------------------------------===//
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000469// AST reader implementation
Douglas Gregor668c1a42009-04-21 22:25:48 +0000470//===----------------------------------------------------------------------===//
471
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000472void
Sebastian Redl571db7f2010-08-18 23:56:56 +0000473ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000474 DeserializationListener = Listener;
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000475}
476
Chris Lattner4c6f9522009-04-27 05:14:47 +0000477
Douglas Gregor668c1a42009-04-21 22:25:48 +0000478namespace {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000479class ASTSelectorLookupTrait {
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000480 ASTReader &Reader;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000481
482public:
Sebastian Redl5d050072010-08-04 17:20:04 +0000483 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000484 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +0000485 ObjCMethodList Instance, Factory;
486 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000487
488 typedef Selector external_key_type;
489 typedef external_key_type internal_key_type;
490
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000491 explicit ASTSelectorLookupTrait(ASTReader &Reader) : Reader(Reader) { }
Mike Stump1eb44332009-09-09 15:08:12 +0000492
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000493 static bool EqualKey(const internal_key_type& a,
494 const internal_key_type& b) {
495 return a == b;
496 }
Mike Stump1eb44332009-09-09 15:08:12 +0000497
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000498 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +0000499 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000500 }
Mike Stump1eb44332009-09-09 15:08:12 +0000501
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000502 // This hopefully will just get inlined and removed by the optimizer.
503 static const internal_key_type&
504 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump1eb44332009-09-09 15:08:12 +0000505
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000506 static std::pair<unsigned, unsigned>
507 ReadKeyDataLength(const unsigned char*& d) {
508 using namespace clang::io;
509 unsigned KeyLen = ReadUnalignedLE16(d);
510 unsigned DataLen = ReadUnalignedLE16(d);
511 return std::make_pair(KeyLen, DataLen);
512 }
Mike Stump1eb44332009-09-09 15:08:12 +0000513
Douglas Gregor83941df2009-04-25 17:48:32 +0000514 internal_key_type ReadKey(const unsigned char* d, unsigned) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000515 using namespace clang::io;
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000516 SelectorTable &SelTable = Reader.getContext()->Selectors;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000517 unsigned N = ReadUnalignedLE16(d);
Mike Stump1eb44332009-09-09 15:08:12 +0000518 IdentifierInfo *FirstII
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000519 = Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
520 if (N == 0)
521 return SelTable.getNullarySelector(FirstII);
522 else if (N == 1)
523 return SelTable.getUnarySelector(FirstII);
524
525 llvm::SmallVector<IdentifierInfo *, 16> Args;
526 Args.push_back(FirstII);
527 for (unsigned I = 1; I != N; ++I)
528 Args.push_back(Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)));
529
Douglas Gregor75fdb232009-05-22 22:45:36 +0000530 return SelTable.getSelector(N, Args.data());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000531 }
Mike Stump1eb44332009-09-09 15:08:12 +0000532
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000533 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen) {
534 using namespace clang::io;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000535
536 data_type Result;
537
Sebastian Redl5d050072010-08-04 17:20:04 +0000538 Result.ID = ReadUnalignedLE32(d);
539 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
540 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
541
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000542 // Load instance methods
543 ObjCMethodList *Prev = 0;
544 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000545 ObjCMethodDecl *Method
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000546 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl5d050072010-08-04 17:20:04 +0000547 if (!Result.Instance.Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000548 // This is the first method, which is the easy case.
Sebastian Redl5d050072010-08-04 17:20:04 +0000549 Result.Instance.Method = Method;
550 Prev = &Result.Instance;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000551 continue;
552 }
553
Ted Kremenek298ed872010-02-11 00:53:01 +0000554 ObjCMethodList *Mem =
555 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
556 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000557 Prev = Prev->Next;
558 }
559
560 // Load factory methods
561 Prev = 0;
562 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000563 ObjCMethodDecl *Method
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000564 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl5d050072010-08-04 17:20:04 +0000565 if (!Result.Factory.Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000566 // This is the first method, which is the easy case.
Sebastian Redl5d050072010-08-04 17:20:04 +0000567 Result.Factory.Method = Method;
568 Prev = &Result.Factory;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000569 continue;
570 }
571
Ted Kremenek298ed872010-02-11 00:53:01 +0000572 ObjCMethodList *Mem =
573 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
574 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000575 Prev = Prev->Next;
576 }
577
578 return Result;
579 }
580};
Mike Stump1eb44332009-09-09 15:08:12 +0000581
582} // end anonymous namespace
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000583
584/// \brief The on-disk hash table used for the global method pool.
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000585typedef OnDiskChainedHashTable<ASTSelectorLookupTrait>
586 ASTSelectorLookupTable;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000587
Sebastian Redlc3632732010-10-05 15:59:54 +0000588namespace clang {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000589class ASTIdentifierLookupTrait {
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000590 ASTReader &Reader;
Sebastian Redlc3632732010-10-05 15:59:54 +0000591 ASTReader::PerFileData &F;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000592
593 // If we know the IdentifierInfo in advance, it is here and we will
594 // not build a new one. Used when deserializing information about an
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000595 // identifier that was constructed before the AST file was read.
Douglas Gregor668c1a42009-04-21 22:25:48 +0000596 IdentifierInfo *KnownII;
597
598public:
599 typedef IdentifierInfo * data_type;
600
601 typedef const std::pair<const char*, unsigned> external_key_type;
602
603 typedef external_key_type internal_key_type;
604
Sebastian Redlc3632732010-10-05 15:59:54 +0000605 ASTIdentifierLookupTrait(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redld27d3fc2010-07-21 22:31:37 +0000606 IdentifierInfo *II = 0)
Sebastian Redlc3632732010-10-05 15:59:54 +0000607 : Reader(Reader), F(F), KnownII(II) { }
Mike Stump1eb44332009-09-09 15:08:12 +0000608
Douglas Gregor668c1a42009-04-21 22:25:48 +0000609 static bool EqualKey(const internal_key_type& a,
610 const internal_key_type& b) {
611 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
612 : false;
613 }
Mike Stump1eb44332009-09-09 15:08:12 +0000614
Douglas Gregor668c1a42009-04-21 22:25:48 +0000615 static unsigned ComputeHash(const internal_key_type& a) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000616 return llvm::HashString(llvm::StringRef(a.first, a.second));
Douglas Gregor668c1a42009-04-21 22:25:48 +0000617 }
Mike Stump1eb44332009-09-09 15:08:12 +0000618
Douglas Gregor668c1a42009-04-21 22:25:48 +0000619 // This hopefully will just get inlined and removed by the optimizer.
620 static const internal_key_type&
621 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump1eb44332009-09-09 15:08:12 +0000622
Douglas Gregor95f42922010-10-14 22:11:03 +0000623 // This hopefully will just get inlined and removed by the optimizer.
624 static const external_key_type&
625 GetExternalKey(const internal_key_type& x) { return x; }
626
Douglas Gregor668c1a42009-04-21 22:25:48 +0000627 static std::pair<unsigned, unsigned>
628 ReadKeyDataLength(const unsigned char*& d) {
629 using namespace clang::io;
Douglas Gregor5f8e3302009-04-25 20:26:24 +0000630 unsigned DataLen = ReadUnalignedLE16(d);
Douglas Gregord6595a42009-04-25 21:04:17 +0000631 unsigned KeyLen = ReadUnalignedLE16(d);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000632 return std::make_pair(KeyLen, DataLen);
633 }
Mike Stump1eb44332009-09-09 15:08:12 +0000634
Douglas Gregor668c1a42009-04-21 22:25:48 +0000635 static std::pair<const char*, unsigned>
636 ReadKey(const unsigned char* d, unsigned n) {
637 assert(n >= 2 && d[n-1] == '\0');
638 return std::make_pair((const char*) d, n-1);
639 }
Mike Stump1eb44332009-09-09 15:08:12 +0000640
641 IdentifierInfo *ReadData(const internal_key_type& k,
Douglas Gregor668c1a42009-04-21 22:25:48 +0000642 const unsigned char* d,
643 unsigned DataLen) {
644 using namespace clang::io;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000645 IdentID ID = ReadUnalignedLE32(d);
Douglas Gregora92193e2009-04-28 21:18:29 +0000646 bool IsInteresting = ID & 0x01;
647
648 // Wipe out the "is interesting" bit.
649 ID = ID >> 1;
650
651 if (!IsInteresting) {
Sebastian Redl083abdf2010-07-27 23:01:28 +0000652 // For uninteresting identifiers, just build the IdentifierInfo
Douglas Gregora92193e2009-04-28 21:18:29 +0000653 // and associate it with the persistent ID.
654 IdentifierInfo *II = KnownII;
655 if (!II)
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000656 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregora92193e2009-04-28 21:18:29 +0000657 Reader.SetIdentifierInfo(ID, II);
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000658 II->setIsFromAST();
Douglas Gregora92193e2009-04-28 21:18:29 +0000659 return II;
660 }
661
Douglas Gregor5998da52009-04-28 21:32:13 +0000662 unsigned Bits = ReadUnalignedLE16(d);
Douglas Gregor2deaea32009-04-22 18:49:13 +0000663 bool CPlusPlusOperatorKeyword = Bits & 0x01;
664 Bits >>= 1;
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000665 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
666 Bits >>= 1;
Douglas Gregor2deaea32009-04-22 18:49:13 +0000667 bool Poisoned = Bits & 0x01;
668 Bits >>= 1;
669 bool ExtensionToken = Bits & 0x01;
670 Bits >>= 1;
671 bool hasMacroDefinition = Bits & 0x01;
672 Bits >>= 1;
673 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
674 Bits >>= 10;
Mike Stump1eb44332009-09-09 15:08:12 +0000675
Douglas Gregor2deaea32009-04-22 18:49:13 +0000676 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregor5998da52009-04-28 21:32:13 +0000677 DataLen -= 6;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000678
679 // Build the IdentifierInfo itself and link the identifier ID with
680 // the new IdentifierInfo.
681 IdentifierInfo *II = KnownII;
682 if (!II)
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000683 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000684 Reader.SetIdentifierInfo(ID, II);
685
Douglas Gregor2deaea32009-04-22 18:49:13 +0000686 // Set or check the various bits in the IdentifierInfo structure.
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000687 // Token IDs are read-only.
688 if (HasRevertedTokenIDToIdentifier)
689 II->RevertTokenIDToIdentifier();
Douglas Gregor2deaea32009-04-22 18:49:13 +0000690 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
Mike Stump1eb44332009-09-09 15:08:12 +0000691 assert(II->isExtensionToken() == ExtensionToken &&
Douglas Gregor2deaea32009-04-22 18:49:13 +0000692 "Incorrect extension token flag");
693 (void)ExtensionToken;
694 II->setIsPoisoned(Poisoned);
695 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
696 "Incorrect C++ operator keyword flag");
697 (void)CPlusPlusOperatorKeyword;
698
Douglas Gregor37e26842009-04-21 23:56:24 +0000699 // If this identifier is a macro, deserialize the macro
700 // definition.
701 if (hasMacroDefinition) {
Douglas Gregor5998da52009-04-28 21:32:13 +0000702 uint32_t Offset = ReadUnalignedLE32(d);
Douglas Gregor295a2a62010-10-30 00:23:06 +0000703 Reader.SetIdentifierIsMacro(II, F, Offset);
Douglas Gregor5998da52009-04-28 21:32:13 +0000704 DataLen -= 4;
Douglas Gregor37e26842009-04-21 23:56:24 +0000705 }
Douglas Gregor668c1a42009-04-21 22:25:48 +0000706
707 // Read all of the declarations visible at global scope with this
708 // name.
Chris Lattner6bf690f2009-04-27 22:17:41 +0000709 if (Reader.getContext() == 0) return II;
Douglas Gregord89275b2009-07-06 18:54:52 +0000710 if (DataLen > 0) {
711 llvm::SmallVector<uint32_t, 4> DeclIDs;
712 for (; DataLen > 0; DataLen -= 4)
713 DeclIDs.push_back(ReadUnalignedLE32(d));
714 Reader.SetGloballyVisibleDecls(II, DeclIDs);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000715 }
Mike Stump1eb44332009-09-09 15:08:12 +0000716
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000717 II->setIsFromAST();
Douglas Gregor668c1a42009-04-21 22:25:48 +0000718 return II;
719 }
720};
Mike Stump1eb44332009-09-09 15:08:12 +0000721
722} // end anonymous namespace
Douglas Gregor668c1a42009-04-21 22:25:48 +0000723
724/// \brief The on-disk hash table used to contain information about
725/// all of the identifiers in the program.
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000726typedef OnDiskChainedHashTable<ASTIdentifierLookupTrait>
727 ASTIdentifierLookupTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000728
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000729namespace {
730class ASTDeclContextNameLookupTrait {
731 ASTReader &Reader;
732
733public:
734 /// \brief Pair of begin/end iterators for DeclIDs.
735 typedef std::pair<DeclID *, DeclID *> data_type;
736
737 /// \brief Special internal key for declaration names.
738 /// The hash table creates keys for comparison; we do not create
739 /// a DeclarationName for the internal key to avoid deserializing types.
740 struct DeclNameKey {
741 DeclarationName::NameKind Kind;
742 uint64_t Data;
743 DeclNameKey() : Kind((DeclarationName::NameKind)0), Data(0) { }
744 };
745
746 typedef DeclarationName external_key_type;
747 typedef DeclNameKey internal_key_type;
748
749 explicit ASTDeclContextNameLookupTrait(ASTReader &Reader) : Reader(Reader) { }
750
751 static bool EqualKey(const internal_key_type& a,
752 const internal_key_type& b) {
753 return a.Kind == b.Kind && a.Data == b.Data;
754 }
755
756 unsigned ComputeHash(const DeclNameKey &Key) const {
757 llvm::FoldingSetNodeID ID;
758 ID.AddInteger(Key.Kind);
759
760 switch (Key.Kind) {
761 case DeclarationName::Identifier:
762 case DeclarationName::CXXLiteralOperatorName:
763 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
764 break;
765 case DeclarationName::ObjCZeroArgSelector:
766 case DeclarationName::ObjCOneArgSelector:
767 case DeclarationName::ObjCMultiArgSelector:
768 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
769 break;
770 case DeclarationName::CXXConstructorName:
771 case DeclarationName::CXXDestructorName:
772 case DeclarationName::CXXConversionFunctionName:
773 ID.AddInteger((TypeID)Key.Data);
774 break;
775 case DeclarationName::CXXOperatorName:
776 ID.AddInteger((OverloadedOperatorKind)Key.Data);
777 break;
778 case DeclarationName::CXXUsingDirective:
779 break;
780 }
781
782 return ID.ComputeHash();
783 }
784
785 internal_key_type GetInternalKey(const external_key_type& Name) const {
786 DeclNameKey Key;
787 Key.Kind = Name.getNameKind();
788 switch (Name.getNameKind()) {
789 case DeclarationName::Identifier:
790 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
791 break;
792 case DeclarationName::ObjCZeroArgSelector:
793 case DeclarationName::ObjCOneArgSelector:
794 case DeclarationName::ObjCMultiArgSelector:
795 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
796 break;
797 case DeclarationName::CXXConstructorName:
798 case DeclarationName::CXXDestructorName:
799 case DeclarationName::CXXConversionFunctionName:
800 Key.Data = Reader.GetTypeID(Name.getCXXNameType());
801 break;
802 case DeclarationName::CXXOperatorName:
803 Key.Data = Name.getCXXOverloadedOperator();
804 break;
805 case DeclarationName::CXXLiteralOperatorName:
806 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
807 break;
808 case DeclarationName::CXXUsingDirective:
809 break;
810 }
Michael J. Spencer20249a12010-10-21 03:16:25 +0000811
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000812 return Key;
813 }
814
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +0000815 external_key_type GetExternalKey(const internal_key_type& Key) const {
816 ASTContext *Context = Reader.getContext();
817 switch (Key.Kind) {
818 case DeclarationName::Identifier:
819 return DeclarationName((IdentifierInfo*)Key.Data);
820
821 case DeclarationName::ObjCZeroArgSelector:
822 case DeclarationName::ObjCOneArgSelector:
823 case DeclarationName::ObjCMultiArgSelector:
824 return DeclarationName(Selector(Key.Data));
825
826 case DeclarationName::CXXConstructorName:
827 return Context->DeclarationNames.getCXXConstructorName(
828 Context->getCanonicalType(Reader.GetType(Key.Data)));
829
830 case DeclarationName::CXXDestructorName:
831 return Context->DeclarationNames.getCXXDestructorName(
832 Context->getCanonicalType(Reader.GetType(Key.Data)));
833
834 case DeclarationName::CXXConversionFunctionName:
835 return Context->DeclarationNames.getCXXConversionFunctionName(
836 Context->getCanonicalType(Reader.GetType(Key.Data)));
837
838 case DeclarationName::CXXOperatorName:
839 return Context->DeclarationNames.getCXXOperatorName(
840 (OverloadedOperatorKind)Key.Data);
841
842 case DeclarationName::CXXLiteralOperatorName:
843 return Context->DeclarationNames.getCXXLiteralOperatorName(
844 (IdentifierInfo*)Key.Data);
845
846 case DeclarationName::CXXUsingDirective:
847 return DeclarationName::getUsingDirectiveName();
848 }
849
850 llvm_unreachable("Invalid Name Kind ?");
851 }
852
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000853 static std::pair<unsigned, unsigned>
854 ReadKeyDataLength(const unsigned char*& d) {
855 using namespace clang::io;
856 unsigned KeyLen = ReadUnalignedLE16(d);
857 unsigned DataLen = ReadUnalignedLE16(d);
858 return std::make_pair(KeyLen, DataLen);
859 }
860
861 internal_key_type ReadKey(const unsigned char* d, unsigned) {
862 using namespace clang::io;
863
864 DeclNameKey Key;
865 Key.Kind = (DeclarationName::NameKind)*d++;
866 switch (Key.Kind) {
867 case DeclarationName::Identifier:
868 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
869 break;
870 case DeclarationName::ObjCZeroArgSelector:
871 case DeclarationName::ObjCOneArgSelector:
872 case DeclarationName::ObjCMultiArgSelector:
Michael J. Spencer20249a12010-10-21 03:16:25 +0000873 Key.Data =
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000874 (uint64_t)Reader.DecodeSelector(ReadUnalignedLE32(d)).getAsOpaquePtr();
875 break;
876 case DeclarationName::CXXConstructorName:
877 case DeclarationName::CXXDestructorName:
878 case DeclarationName::CXXConversionFunctionName:
879 Key.Data = ReadUnalignedLE32(d); // TypeID
880 break;
881 case DeclarationName::CXXOperatorName:
882 Key.Data = *d++; // OverloadedOperatorKind
883 break;
884 case DeclarationName::CXXLiteralOperatorName:
885 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
886 break;
887 case DeclarationName::CXXUsingDirective:
888 break;
889 }
Michael J. Spencer20249a12010-10-21 03:16:25 +0000890
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000891 return Key;
892 }
893
894 data_type ReadData(internal_key_type, const unsigned char* d,
895 unsigned DataLen) {
896 using namespace clang::io;
897 unsigned NumDecls = ReadUnalignedLE16(d);
898 DeclID *Start = (DeclID *)d;
899 return std::make_pair(Start, Start + NumDecls);
900 }
901};
902
903} // end anonymous namespace
904
905/// \brief The on-disk hash table used for the DeclContext's Name lookup table.
906typedef OnDiskChainedHashTable<ASTDeclContextNameLookupTrait>
907 ASTDeclContextNameLookupTable;
908
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000909bool ASTReader::ReadDeclContextStorage(llvm::BitstreamCursor &Cursor,
910 const std::pair<uint64_t, uint64_t> &Offsets,
911 DeclContextInfo &Info) {
912 SavedStreamPosition SavedPosition(Cursor);
913 // First the lexical decls.
914 if (Offsets.first != 0) {
915 Cursor.JumpToBit(Offsets.first);
916
917 RecordData Record;
918 const char *Blob;
919 unsigned BlobLen;
920 unsigned Code = Cursor.ReadCode();
921 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
922 if (RecCode != DECL_CONTEXT_LEXICAL) {
923 Error("Expected lexical block");
924 return true;
925 }
926
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000927 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob);
928 Info.NumLexicalDecls = BlobLen / sizeof(KindDeclIDPair);
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000929 } else {
930 Info.LexicalDecls = 0;
931 Info.NumLexicalDecls = 0;
932 }
933
934 // Now the lookup table.
935 if (Offsets.second != 0) {
936 Cursor.JumpToBit(Offsets.second);
937
938 RecordData Record;
939 const char *Blob;
940 unsigned BlobLen;
941 unsigned Code = Cursor.ReadCode();
942 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
943 if (RecCode != DECL_CONTEXT_VISIBLE) {
944 Error("Expected visible lookup table block");
945 return true;
946 }
947 Info.NameLookupTableData
948 = ASTDeclContextNameLookupTable::Create(
949 (const unsigned char *)Blob + Record[0],
950 (const unsigned char *)Blob,
951 ASTDeclContextNameLookupTrait(*this));
Sebastian Redl0ea8f7f2010-08-24 00:50:00 +0000952 } else {
953 Info.NameLookupTableData = 0;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000954 }
955
956 return false;
957}
958
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000959void ASTReader::Error(const char *Msg) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000960 Diag(diag::err_fe_pch_malformed) << Msg;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000961}
962
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000963/// \brief Tell the AST listener about the predefines buffers in the chain.
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000964bool ASTReader::CheckPredefinesBuffers() {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000965 if (Listener)
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000966 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000967 ActualOriginalFileName,
Nick Lewycky277a6e72011-02-23 21:16:44 +0000968 SuggestedPredefines,
969 FileMgr);
Douglas Gregore721f952009-04-28 18:58:38 +0000970 return false;
Douglas Gregore1d918e2009-04-10 23:10:45 +0000971}
972
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000973//===----------------------------------------------------------------------===//
974// Source Manager Deserialization
975//===----------------------------------------------------------------------===//
976
Douglas Gregorbd945002009-04-13 16:31:14 +0000977/// \brief Read the line table in the source manager block.
Sebastian Redlc3632732010-10-05 15:59:54 +0000978/// \returns true if there was an error.
979bool ASTReader::ParseLineTable(PerFileData &F,
980 llvm::SmallVectorImpl<uint64_t> &Record) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000981 unsigned Idx = 0;
982 LineTableInfo &LineTable = SourceMgr.getLineTable();
983
984 // Parse the file names
Douglas Gregorff0a9872009-04-13 17:12:42 +0000985 std::map<int, int> FileIDs;
986 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000987 // Extract the file name
988 unsigned FilenameLen = Record[Idx++];
989 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
990 Idx += FilenameLen;
Douglas Gregore650c8c2009-07-07 00:12:59 +0000991 MaybeAddSystemRootToFilename(Filename);
Mike Stump1eb44332009-09-09 15:08:12 +0000992 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
Douglas Gregorff0a9872009-04-13 17:12:42 +0000993 Filename.size());
Douglas Gregorbd945002009-04-13 16:31:14 +0000994 }
995
996 // Parse the line entries
997 std::vector<LineEntry> Entries;
998 while (Idx < Record.size()) {
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000999 int FID = Record[Idx++];
Douglas Gregorbd945002009-04-13 16:31:14 +00001000
1001 // Extract the line entries
1002 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +00001003 assert(NumEntries && "Numentries is 00000");
Douglas Gregorbd945002009-04-13 16:31:14 +00001004 Entries.clear();
1005 Entries.reserve(NumEntries);
1006 for (unsigned I = 0; I != NumEntries; ++I) {
1007 unsigned FileOffset = Record[Idx++];
1008 unsigned LineNo = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +00001009 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump1eb44332009-09-09 15:08:12 +00001010 SrcMgr::CharacteristicKind FileKind
Douglas Gregorbd945002009-04-13 16:31:14 +00001011 = (SrcMgr::CharacteristicKind)Record[Idx++];
1012 unsigned IncludeOffset = Record[Idx++];
1013 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1014 FileKind, IncludeOffset));
1015 }
1016 LineTable.AddEntry(FID, Entries);
1017 }
1018
1019 return false;
1020}
1021
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001022namespace {
1023
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001024class ASTStatData {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001025public:
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001026 const ino_t ino;
1027 const dev_t dev;
1028 const mode_t mode;
1029 const time_t mtime;
1030 const off_t size;
Mike Stump1eb44332009-09-09 15:08:12 +00001031
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001032 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Chris Lattner74e976b2010-11-23 19:28:12 +00001033 : ino(i), dev(d), mode(mo), mtime(m), size(s) {}
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001034};
1035
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001036class ASTStatLookupTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001037 public:
1038 typedef const char *external_key_type;
1039 typedef const char *internal_key_type;
1040
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001041 typedef ASTStatData data_type;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001042
1043 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001044 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001045 }
1046
1047 static internal_key_type GetInternalKey(const char *path) { return path; }
1048
1049 static bool EqualKey(internal_key_type a, internal_key_type b) {
1050 return strcmp(a, b) == 0;
1051 }
1052
1053 static std::pair<unsigned, unsigned>
1054 ReadKeyDataLength(const unsigned char*& d) {
1055 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1056 unsigned DataLen = (unsigned) *d++;
1057 return std::make_pair(KeyLen + 1, DataLen);
1058 }
1059
1060 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
1061 return (const char *)d;
1062 }
1063
1064 static data_type ReadData(const internal_key_type, const unsigned char *d,
1065 unsigned /*DataLen*/) {
1066 using namespace clang::io;
1067
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001068 ino_t ino = (ino_t) ReadUnalignedLE32(d);
1069 dev_t dev = (dev_t) ReadUnalignedLE32(d);
1070 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump1eb44332009-09-09 15:08:12 +00001071 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001072 off_t size = (off_t) ReadUnalignedLE64(d);
1073 return data_type(ino, dev, mode, mtime, size);
1074 }
1075};
1076
1077/// \brief stat() cache for precompiled headers.
1078///
1079/// This cache is very similar to the stat cache used by pretokenized
1080/// headers.
Chris Lattner10e286a2010-11-23 19:19:34 +00001081class ASTStatCache : public FileSystemStatCache {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001082 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001083 CacheTy *Cache;
1084
1085 unsigned &NumStatHits, &NumStatMisses;
Mike Stump1eb44332009-09-09 15:08:12 +00001086public:
Chris Lattner74e976b2010-11-23 19:28:12 +00001087 ASTStatCache(const unsigned char *Buckets, const unsigned char *Base,
1088 unsigned &NumStatHits, unsigned &NumStatMisses)
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001089 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
1090 Cache = CacheTy::Create(Buckets, Base);
1091 }
1092
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001093 ~ASTStatCache() { delete Cache; }
Mike Stump1eb44332009-09-09 15:08:12 +00001094
Chris Lattner898a0612010-11-23 21:17:56 +00001095 LookupResult getStat(const char *Path, struct stat &StatBuf,
1096 int *FileDescriptor) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001097 // Do the lookup for the file's data in the AST file.
Chris Lattner10e286a2010-11-23 19:19:34 +00001098 CacheTy::iterator I = Cache->find(Path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001099
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001100 // If we don't get a hit in the AST file just forward to 'stat'.
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001101 if (I == Cache->end()) {
1102 ++NumStatMisses;
Chris Lattner898a0612010-11-23 21:17:56 +00001103 return statChained(Path, StatBuf, FileDescriptor);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001104 }
Mike Stump1eb44332009-09-09 15:08:12 +00001105
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001106 ++NumStatHits;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001107 ASTStatData Data = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001108
Chris Lattner10e286a2010-11-23 19:19:34 +00001109 StatBuf.st_ino = Data.ino;
1110 StatBuf.st_dev = Data.dev;
1111 StatBuf.st_mtime = Data.mtime;
1112 StatBuf.st_mode = Data.mode;
1113 StatBuf.st_size = Data.size;
Chris Lattnerd6f61112010-11-23 20:05:15 +00001114 return CacheExists;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001115 }
1116};
1117} // end anonymous namespace
1118
1119
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001120/// \brief Read a source manager block
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001121ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(PerFileData &F) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001122 using namespace SrcMgr;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001123
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001124 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001125
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001126 // Set the source-location entry cursor to the current position in
1127 // the stream. This cursor will be used to read the contents of the
1128 // source manager block initially, and then lazily read
1129 // source-location entries as needed.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001130 SLocEntryCursor = F.Stream;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001131
1132 // The stream itself is going to skip over the source manager block.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001133 if (F.Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001134 Error("malformed block record in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001135 return Failure;
1136 }
1137
1138 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001139 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001140 Error("malformed source manager block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001141 return Failure;
1142 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001143
Douglas Gregor14f79002009-04-10 03:52:48 +00001144 RecordData Record;
1145 while (true) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001146 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregor14f79002009-04-10 03:52:48 +00001147 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001148 if (SLocEntryCursor.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001149 Error("error at end of Source Manager block in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001150 return Failure;
1151 }
Douglas Gregore1d918e2009-04-10 23:10:45 +00001152 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +00001153 }
Mike Stump1eb44332009-09-09 15:08:12 +00001154
Douglas Gregor14f79002009-04-10 03:52:48 +00001155 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1156 // No known subblocks, always skip them.
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001157 SLocEntryCursor.ReadSubBlockID();
1158 if (SLocEntryCursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001159 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001160 return Failure;
1161 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001162 continue;
1163 }
Mike Stump1eb44332009-09-09 15:08:12 +00001164
Douglas Gregor14f79002009-04-10 03:52:48 +00001165 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001166 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregor14f79002009-04-10 03:52:48 +00001167 continue;
1168 }
Mike Stump1eb44332009-09-09 15:08:12 +00001169
Douglas Gregor14f79002009-04-10 03:52:48 +00001170 // Read a record.
1171 const char *BlobStart;
1172 unsigned BlobLen;
1173 Record.clear();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001174 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001175 default: // Default behavior: ignore.
1176 break;
1177
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001178 case SM_LINE_TABLE:
Sebastian Redlc3632732010-10-05 15:59:54 +00001179 if (ParseLineTable(F, Record))
Douglas Gregorbd945002009-04-13 16:31:14 +00001180 return Failure;
Chris Lattner2c78b872009-04-14 23:22:57 +00001181 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001182
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001183 case SM_SLOC_FILE_ENTRY:
1184 case SM_SLOC_BUFFER_ENTRY:
1185 case SM_SLOC_INSTANTIATION_ENTRY:
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001186 // Once we hit one of the source location entries, we're done.
1187 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +00001188 }
1189 }
1190}
1191
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001192/// \brief If a header file is not found at the path that we expect it to be
1193/// and the PCH file was moved from its original location, try to resolve the
1194/// file by assuming that header+PCH were moved together and the header is in
1195/// the same place relative to the PCH.
1196static std::string
1197resolveFileRelativeToOriginalDir(const std::string &Filename,
1198 const std::string &OriginalDir,
1199 const std::string &CurrDir) {
1200 assert(OriginalDir != CurrDir &&
1201 "No point trying to resolve the file if the PCH dir didn't change");
1202 using namespace llvm::sys;
1203 llvm::SmallString<128> filePath(Filename);
1204 fs::make_absolute(filePath);
1205 assert(path::is_absolute(OriginalDir));
1206 llvm::SmallString<128> currPCHPath(CurrDir);
1207
1208 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1209 fileDirE = path::end(path::parent_path(filePath));
1210 path::const_iterator origDirI = path::begin(OriginalDir),
1211 origDirE = path::end(OriginalDir);
1212 // Skip the common path components from filePath and OriginalDir.
1213 while (fileDirI != fileDirE && origDirI != origDirE &&
1214 *fileDirI == *origDirI) {
1215 ++fileDirI;
1216 ++origDirI;
1217 }
1218 for (; origDirI != origDirE; ++origDirI)
1219 path::append(currPCHPath, "..");
1220 path::append(currPCHPath, fileDirI, fileDirE);
1221 path::append(currPCHPath, path::filename(Filename));
1222 return currPCHPath.str();
1223}
1224
Sebastian Redl190faf72010-07-20 21:50:20 +00001225/// \brief Get a cursor that's correctly positioned for reading the source
1226/// location entry with the given ID.
Sebastian Redlc3632732010-10-05 15:59:54 +00001227ASTReader::PerFileData *ASTReader::SLocCursorForID(unsigned ID) {
Sebastian Redl190faf72010-07-20 21:50:20 +00001228 assert(ID != 0 && ID <= TotalNumSLocEntries &&
1229 "SLocCursorForID should only be called for real IDs.");
1230
1231 ID -= 1;
1232 PerFileData *F = 0;
1233 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1234 F = Chain[N - I - 1];
1235 if (ID < F->LocalNumSLocEntries)
1236 break;
1237 ID -= F->LocalNumSLocEntries;
1238 }
1239 assert(F && F->LocalNumSLocEntries > ID && "Chain corrupted");
1240
1241 F->SLocEntryCursor.JumpToBit(F->SLocOffsets[ID]);
Sebastian Redlc3632732010-10-05 15:59:54 +00001242 return F;
Sebastian Redl190faf72010-07-20 21:50:20 +00001243}
1244
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001245/// \brief Read in the source location entry with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001246ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(unsigned ID) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001247 if (ID == 0)
1248 return Success;
1249
1250 if (ID > TotalNumSLocEntries) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001251 Error("source location entry ID out-of-range for AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001252 return Failure;
1253 }
1254
Sebastian Redlc3632732010-10-05 15:59:54 +00001255 PerFileData *F = SLocCursorForID(ID);
1256 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001257
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001258 ++NumSLocEntriesRead;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001259 unsigned Code = SLocEntryCursor.ReadCode();
1260 if (Code == llvm::bitc::END_BLOCK ||
1261 Code == llvm::bitc::ENTER_SUBBLOCK ||
1262 Code == llvm::bitc::DEFINE_ABBREV) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001263 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001264 return Failure;
1265 }
1266
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001267 RecordData Record;
1268 const char *BlobStart;
1269 unsigned BlobLen;
1270 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1271 default:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001272 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001273 return Failure;
1274
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001275 case SM_SLOC_FILE_ENTRY: {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001276 std::string Filename(BlobStart, BlobStart + BlobLen);
1277 MaybeAddSystemRootToFilename(Filename);
Chris Lattner39b49bc2010-11-23 08:35:12 +00001278 const FileEntry *File = FileMgr.getFile(Filename);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001279 if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() &&
1280 OriginalDir != CurrentDir) {
1281 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1282 OriginalDir,
1283 CurrentDir);
1284 if (!resolved.empty())
1285 File = FileMgr.getFile(resolved);
1286 }
Axel Naumann04331162011-01-27 10:55:51 +00001287 if (File == 0)
1288 File = FileMgr.getVirtualFile(Filename, (off_t)Record[4],
1289 (time_t)Record[5]);
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001290 if (File == 0) {
1291 std::string ErrorStr = "could not find file '";
Douglas Gregore650c8c2009-07-07 00:12:59 +00001292 ErrorStr += Filename;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001293 ErrorStr += "' referenced by AST file";
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001294 Error(ErrorStr.c_str());
1295 return Failure;
1296 }
Mike Stump1eb44332009-09-09 15:08:12 +00001297
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001298 if (Record.size() < 6) {
Ted Kremenek1857f622010-03-18 21:23:05 +00001299 Error("source location entry is incorrect");
1300 return Failure;
1301 }
1302
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001303 if (!DisableValidation &&
1304 ((off_t)Record[4] != File->getSize()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001305#if !defined(LLVM_ON_WIN32)
1306 // In our regression testing, the Windows file system seems to
1307 // have inconsistent modification times that sometimes
1308 // erroneously trigger this error-handling path.
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001309 || (time_t)Record[5] != File->getModificationTime()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001310#endif
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001311 )) {
Douglas Gregor2d52be52010-03-21 22:49:54 +00001312 Diag(diag::err_fe_pch_file_modified)
1313 << Filename;
1314 return Failure;
1315 }
1316
Chris Lattner75dfb652010-11-23 09:19:42 +00001317 FileID FID = SourceMgr.createFileID(File, ReadSourceLocation(*F, Record[1]),
1318 (SrcMgr::CharacteristicKind)Record[2],
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001319 ID, Record[0]);
1320 if (Record[3])
1321 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile())
1322 .setHasLineDirectives();
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001323
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001324 break;
1325 }
1326
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001327 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001328 const char *Name = BlobStart;
1329 unsigned Offset = Record[0];
1330 unsigned Code = SLocEntryCursor.ReadCode();
1331 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001332 unsigned RecCode
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001333 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001334
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001335 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001336 Error("AST record has invalid code");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001337 return Failure;
1338 }
1339
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001340 llvm::MemoryBuffer *Buffer
Chris Lattnera0a270c2010-04-05 22:42:27 +00001341 = llvm::MemoryBuffer::getMemBuffer(llvm::StringRef(BlobStart, BlobLen - 1),
1342 Name);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001343 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID, Offset);
Mike Stump1eb44332009-09-09 15:08:12 +00001344
Douglas Gregor92b059e2009-04-28 20:33:11 +00001345 if (strcmp(Name, "<built-in>") == 0) {
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +00001346 PCHPredefinesBlock Block = {
1347 BufferID,
1348 llvm::StringRef(BlobStart, BlobLen - 1)
1349 };
1350 PCHPredefinesBuffers.push_back(Block);
Douglas Gregor92b059e2009-04-28 20:33:11 +00001351 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001352
1353 break;
1354 }
1355
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001356 case SM_SLOC_INSTANTIATION_ENTRY: {
Sebastian Redlc3632732010-10-05 15:59:54 +00001357 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001358 SourceMgr.createInstantiationLoc(SpellingLoc,
Sebastian Redlc3632732010-10-05 15:59:54 +00001359 ReadSourceLocation(*F, Record[2]),
1360 ReadSourceLocation(*F, Record[3]),
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001361 Record[4],
1362 ID,
1363 Record[0]);
1364 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001365 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001366 }
1367
1368 return Success;
1369}
1370
Chris Lattner6367f6d2009-04-27 01:05:14 +00001371/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1372/// specified cursor. Read the abbreviations that are at the top of the block
1373/// and then leave the cursor pointing into the block.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001374bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattner6367f6d2009-04-27 01:05:14 +00001375 unsigned BlockID) {
1376 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001377 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001378 return Failure;
1379 }
Mike Stump1eb44332009-09-09 15:08:12 +00001380
Chris Lattner6367f6d2009-04-27 01:05:14 +00001381 while (true) {
Douglas Gregorecdcb882010-10-20 22:00:55 +00001382 uint64_t Offset = Cursor.GetCurrentBitNo();
Chris Lattner6367f6d2009-04-27 01:05:14 +00001383 unsigned Code = Cursor.ReadCode();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001384
Chris Lattner6367f6d2009-04-27 01:05:14 +00001385 // We expect all abbrevs to be at the start of the block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001386 if (Code != llvm::bitc::DEFINE_ABBREV) {
1387 Cursor.JumpToBit(Offset);
Chris Lattner6367f6d2009-04-27 01:05:14 +00001388 return false;
Douglas Gregorecdcb882010-10-20 22:00:55 +00001389 }
Chris Lattner6367f6d2009-04-27 01:05:14 +00001390 Cursor.ReadAbbrevRecord();
1391 }
1392}
1393
Douglas Gregor89d99802010-11-30 06:16:57 +00001394PreprocessedEntity *ASTReader::ReadMacroRecord(PerFileData &F, uint64_t Offset) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001395 assert(PP && "Forgot to set Preprocessor ?");
Douglas Gregorecdcb882010-10-20 22:00:55 +00001396 llvm::BitstreamCursor &Stream = F.MacroCursor;
Mike Stump1eb44332009-09-09 15:08:12 +00001397
Douglas Gregor37e26842009-04-21 23:56:24 +00001398 // Keep track of where we are in the stream, then jump back there
1399 // after reading this macro.
1400 SavedStreamPosition SavedPosition(Stream);
1401
1402 Stream.JumpToBit(Offset);
1403 RecordData Record;
1404 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1405 MacroInfo *Macro = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001406
Douglas Gregor37e26842009-04-21 23:56:24 +00001407 while (true) {
1408 unsigned Code = Stream.ReadCode();
1409 switch (Code) {
1410 case llvm::bitc::END_BLOCK:
Douglas Gregor89d99802010-11-30 06:16:57 +00001411 return 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001412
1413 case llvm::bitc::ENTER_SUBBLOCK:
1414 // No known subblocks, always skip them.
1415 Stream.ReadSubBlockID();
1416 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001417 Error("malformed block record in AST file");
Douglas Gregor89d99802010-11-30 06:16:57 +00001418 return 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001419 }
1420 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001421
Douglas Gregor37e26842009-04-21 23:56:24 +00001422 case llvm::bitc::DEFINE_ABBREV:
1423 Stream.ReadAbbrevRecord();
1424 continue;
1425 default: break;
1426 }
1427
1428 // Read a record.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001429 const char *BlobStart = 0;
1430 unsigned BlobLen = 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001431 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001432 PreprocessorRecordTypes RecType =
Michael J. Spencer20249a12010-10-21 03:16:25 +00001433 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart,
Douglas Gregorecdcb882010-10-20 22:00:55 +00001434 BlobLen);
Douglas Gregor37e26842009-04-21 23:56:24 +00001435 switch (RecType) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001436 case PP_MACRO_OBJECT_LIKE:
1437 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001438 // If we already have a macro, that means that we've hit the end
1439 // of the definition of the macro we were looking for. We're
1440 // done.
1441 if (Macro)
Douglas Gregor89d99802010-11-30 06:16:57 +00001442 return 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001443
1444 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1445 if (II == 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001446 Error("macro must have a name in AST file");
Douglas Gregor89d99802010-11-30 06:16:57 +00001447 return 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001448 }
Sebastian Redlc3632732010-10-05 15:59:54 +00001449 SourceLocation Loc = ReadSourceLocation(F, Record[1]);
Douglas Gregor37e26842009-04-21 23:56:24 +00001450 bool isUsed = Record[2];
Mike Stump1eb44332009-09-09 15:08:12 +00001451
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001452 MacroInfo *MI = PP->AllocateMacroInfo(Loc);
Douglas Gregor37e26842009-04-21 23:56:24 +00001453 MI->setIsUsed(isUsed);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001454 MI->setIsFromAST();
Mike Stump1eb44332009-09-09 15:08:12 +00001455
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001456 unsigned NextIndex = 3;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001457 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregor37e26842009-04-21 23:56:24 +00001458 // Decode function-like macro info.
1459 bool isC99VarArgs = Record[3];
1460 bool isGNUVarArgs = Record[4];
1461 MacroArgs.clear();
1462 unsigned NumArgs = Record[5];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001463 NextIndex = 6 + NumArgs;
Douglas Gregor37e26842009-04-21 23:56:24 +00001464 for (unsigned i = 0; i != NumArgs; ++i)
1465 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1466
1467 // Install function-like macro info.
1468 MI->setIsFunctionLike();
1469 if (isC99VarArgs) MI->setIsC99Varargs();
1470 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor75fdb232009-05-22 22:45:36 +00001471 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001472 PP->getPreprocessorAllocator());
Douglas Gregor37e26842009-04-21 23:56:24 +00001473 }
1474
1475 // Finally, install the macro.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001476 PP->setMacroInfo(II, MI);
Douglas Gregor37e26842009-04-21 23:56:24 +00001477
1478 // Remember that we saw this macro last so that we add the tokens that
1479 // form its body to it.
1480 Macro = MI;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001481
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001482 if (NextIndex + 1 == Record.size() && PP->getPreprocessingRecord()) {
1483 // We have a macro definition. Load it now.
1484 PP->getPreprocessingRecord()->RegisterMacroDefinition(Macro,
1485 getMacroDefinition(Record[NextIndex]));
1486 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001487
Douglas Gregor37e26842009-04-21 23:56:24 +00001488 ++NumMacrosRead;
1489 break;
1490 }
Mike Stump1eb44332009-09-09 15:08:12 +00001491
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001492 case PP_TOKEN: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001493 // If we see a TOKEN before a PP_MACRO_*, then the file is
1494 // erroneous, just pretend we didn't see this.
1495 if (Macro == 0) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001496
Douglas Gregor37e26842009-04-21 23:56:24 +00001497 Token Tok;
1498 Tok.startToken();
Sebastian Redlc3632732010-10-05 15:59:54 +00001499 Tok.setLocation(ReadSourceLocation(F, Record[0]));
Douglas Gregor37e26842009-04-21 23:56:24 +00001500 Tok.setLength(Record[1]);
1501 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1502 Tok.setIdentifierInfo(II);
1503 Tok.setKind((tok::TokenKind)Record[3]);
1504 Tok.setFlag((Token::TokenFlags)Record[4]);
1505 Macro->AddTokenToBody(Tok);
1506 break;
1507 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001508 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001509 }
Douglas Gregor89d99802010-11-30 06:16:57 +00001510
1511 return 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001512}
1513
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001514PreprocessedEntity *ASTReader::LoadPreprocessedEntity(PerFileData &F) {
1515 assert(PP && "Forgot to set Preprocessor ?");
1516 unsigned Code = F.PreprocessorDetailCursor.ReadCode();
1517 switch (Code) {
1518 case llvm::bitc::END_BLOCK:
1519 return 0;
1520
1521 case llvm::bitc::ENTER_SUBBLOCK:
1522 Error("unexpected subblock record in preprocessor detail block");
1523 return 0;
1524
1525 case llvm::bitc::DEFINE_ABBREV:
1526 Error("unexpected abbrevation record in preprocessor detail block");
1527 return 0;
1528
1529 default:
1530 break;
1531 }
1532
1533 if (!PP->getPreprocessingRecord()) {
1534 Error("no preprocessing record");
1535 return 0;
1536 }
1537
1538 // Read the record.
1539 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1540 const char *BlobStart = 0;
1541 unsigned BlobLen = 0;
1542 RecordData Record;
1543 PreprocessorDetailRecordTypes RecType =
1544 (PreprocessorDetailRecordTypes)F.PreprocessorDetailCursor.ReadRecord(
1545 Code, Record, BlobStart, BlobLen);
1546 switch (RecType) {
1547 case PPD_MACRO_INSTANTIATION: {
1548 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1549 return PE;
1550
1551 MacroInstantiation *MI
1552 = new (PPRec) MacroInstantiation(DecodeIdentifierInfo(Record[3]),
1553 SourceRange(ReadSourceLocation(F, Record[1]),
1554 ReadSourceLocation(F, Record[2])),
1555 getMacroDefinition(Record[4]));
1556 PPRec.SetPreallocatedEntity(Record[0], MI);
1557 return MI;
1558 }
1559
1560 case PPD_MACRO_DEFINITION: {
1561 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1562 return PE;
1563
1564 if (Record[1] > MacroDefinitionsLoaded.size()) {
1565 Error("out-of-bounds macro definition record");
1566 return 0;
1567 }
1568
1569 // Decode the identifier info and then check again; if the macro is
1570 // still defined and associated with the identifier,
1571 IdentifierInfo *II = DecodeIdentifierInfo(Record[4]);
1572 if (!MacroDefinitionsLoaded[Record[1] - 1]) {
1573 MacroDefinition *MD
1574 = new (PPRec) MacroDefinition(II,
1575 ReadSourceLocation(F, Record[5]),
1576 SourceRange(
1577 ReadSourceLocation(F, Record[2]),
1578 ReadSourceLocation(F, Record[3])));
1579
1580 PPRec.SetPreallocatedEntity(Record[0], MD);
1581 MacroDefinitionsLoaded[Record[1] - 1] = MD;
1582
1583 if (DeserializationListener)
1584 DeserializationListener->MacroDefinitionRead(Record[1], MD);
1585 }
1586
1587 return MacroDefinitionsLoaded[Record[1] - 1];
1588 }
1589
1590 case PPD_INCLUSION_DIRECTIVE: {
1591 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1592 return PE;
1593
1594 const char *FullFileNameStart = BlobStart + Record[3];
1595 const FileEntry *File
1596 = PP->getFileManager().getFile(llvm::StringRef(FullFileNameStart,
1597 BlobLen - Record[3]));
1598
1599 // FIXME: Stable encoding
1600 InclusionDirective::InclusionKind Kind
1601 = static_cast<InclusionDirective::InclusionKind>(Record[5]);
1602 InclusionDirective *ID
1603 = new (PPRec) InclusionDirective(PPRec, Kind,
1604 llvm::StringRef(BlobStart, Record[3]),
1605 Record[4],
1606 File,
1607 SourceRange(ReadSourceLocation(F, Record[1]),
1608 ReadSourceLocation(F, Record[2])));
1609 PPRec.SetPreallocatedEntity(Record[0], ID);
1610 return ID;
1611 }
1612 }
1613
1614 Error("invalid offset in preprocessor detail block");
1615 return 0;
1616}
1617
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001618namespace {
1619 /// \brief Trait class used to search the on-disk hash table containing all of
1620 /// the header search information.
1621 ///
1622 /// The on-disk hash table contains a mapping from each header path to
1623 /// information about that header (how many times it has been included, its
1624 /// controlling macro, etc.). Note that we actually hash based on the
1625 /// filename, and support "deep" comparisons of file names based on current
1626 /// inode numbers, so that the search can cope with non-normalized path names
1627 /// and symlinks.
1628 class HeaderFileInfoTrait {
1629 const char *SearchPath;
1630 struct stat SearchPathStatBuf;
1631 llvm::Optional<int> SearchPathStatResult;
1632
1633 int StatSimpleCache(const char *Path, struct stat *StatBuf) {
1634 if (Path == SearchPath) {
1635 if (!SearchPathStatResult)
1636 SearchPathStatResult = stat(Path, &SearchPathStatBuf);
1637
1638 *StatBuf = SearchPathStatBuf;
1639 return *SearchPathStatResult;
1640 }
1641
1642 return stat(Path, StatBuf);
1643 }
1644
1645 public:
1646 typedef const char *external_key_type;
1647 typedef const char *internal_key_type;
1648
1649 typedef HeaderFileInfo data_type;
1650
1651 HeaderFileInfoTrait(const char *SearchPath = 0) : SearchPath(SearchPath) { }
1652
1653 static unsigned ComputeHash(const char *path) {
1654 return llvm::HashString(llvm::sys::path::filename(path));
1655 }
1656
1657 static internal_key_type GetInternalKey(const char *path) { return path; }
1658
1659 bool EqualKey(internal_key_type a, internal_key_type b) {
1660 if (strcmp(a, b) == 0)
1661 return true;
1662
1663 if (llvm::sys::path::filename(a) != llvm::sys::path::filename(b))
1664 return false;
1665
1666 // The file names match, but the path names don't. stat() the files to
1667 // see if they are the same.
1668 struct stat StatBufA, StatBufB;
1669 if (StatSimpleCache(a, &StatBufA) || StatSimpleCache(b, &StatBufB))
1670 return false;
1671
1672 return StatBufA.st_ino == StatBufB.st_ino;
1673 }
1674
1675 static std::pair<unsigned, unsigned>
1676 ReadKeyDataLength(const unsigned char*& d) {
1677 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1678 unsigned DataLen = (unsigned) *d++;
1679 return std::make_pair(KeyLen + 1, DataLen);
1680 }
1681
1682 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
1683 return (const char *)d;
1684 }
1685
1686 static data_type ReadData(const internal_key_type, const unsigned char *d,
1687 unsigned DataLen) {
1688 const unsigned char *End = d + DataLen;
1689 using namespace clang::io;
1690 HeaderFileInfo HFI;
1691 unsigned Flags = *d++;
1692 HFI.isImport = (Flags >> 3) & 0x01;
1693 HFI.DirInfo = (Flags >> 1) & 0x03;
1694 HFI.Resolved = Flags & 0x01;
1695 HFI.NumIncludes = ReadUnalignedLE16(d);
1696 HFI.ControllingMacroID = ReadUnalignedLE32(d);
1697 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1698 (void)End;
1699
1700 // This HeaderFileInfo was externally loaded.
1701 HFI.External = true;
1702 return HFI;
1703 }
1704 };
1705}
1706
1707/// \brief The on-disk hash table used for the global method pool.
1708typedef OnDiskChainedHashTable<HeaderFileInfoTrait>
1709 HeaderFileInfoLookupTable;
1710
Douglas Gregor295a2a62010-10-30 00:23:06 +00001711void ASTReader::SetIdentifierIsMacro(IdentifierInfo *II, PerFileData &F,
1712 uint64_t Offset) {
1713 // Note that this identifier has a macro definition.
1714 II->setHasMacroDefinition(true);
1715
1716 // Adjust the offset based on our position in the chain.
1717 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1718 if (Chain[I] == &F)
1719 break;
1720
1721 Offset += Chain[I]->SizeInBits;
1722 }
1723
1724 UnreadMacroRecordOffsets[II] = Offset;
1725}
1726
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001727void ASTReader::ReadDefinedMacros() {
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001728 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redlc3632732010-10-05 15:59:54 +00001729 PerFileData &F = *Chain[N - I - 1];
1730 llvm::BitstreamCursor &MacroCursor = F.MacroCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001731
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001732 // If there was no preprocessor block, skip this file.
1733 if (!MacroCursor.getBitStreamReader())
1734 continue;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001735
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001736 llvm::BitstreamCursor Cursor = MacroCursor;
Douglas Gregorecdcb882010-10-20 22:00:55 +00001737 Cursor.JumpToBit(F.MacroStartOffset);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001738
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001739 RecordData Record;
1740 while (true) {
1741 unsigned Code = Cursor.ReadCode();
Douglas Gregorecdcb882010-10-20 22:00:55 +00001742 if (Code == llvm::bitc::END_BLOCK)
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001743 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001744
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001745 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1746 // No known subblocks, always skip them.
1747 Cursor.ReadSubBlockID();
1748 if (Cursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001749 Error("malformed block record in AST file");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001750 return;
1751 }
1752 continue;
1753 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001754
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001755 if (Code == llvm::bitc::DEFINE_ABBREV) {
1756 Cursor.ReadAbbrevRecord();
1757 continue;
1758 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001759
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001760 // Read a record.
1761 const char *BlobStart;
1762 unsigned BlobLen;
1763 Record.clear();
1764 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1765 default: // Default behavior: ignore.
1766 break;
Douglas Gregor88a35862010-01-04 19:18:44 +00001767
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001768 case PP_MACRO_OBJECT_LIKE:
1769 case PP_MACRO_FUNCTION_LIKE:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001770 DecodeIdentifierInfo(Record[0]);
1771 break;
1772
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001773 case PP_TOKEN:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001774 // Ignore tokens.
1775 break;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001776 }
Douglas Gregor88a35862010-01-04 19:18:44 +00001777 }
1778 }
Douglas Gregor295a2a62010-10-30 00:23:06 +00001779
1780 // Drain the unread macro-record offsets map.
1781 while (!UnreadMacroRecordOffsets.empty())
1782 LoadMacroDefinition(UnreadMacroRecordOffsets.begin());
1783}
1784
1785void ASTReader::LoadMacroDefinition(
1786 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos) {
1787 assert(Pos != UnreadMacroRecordOffsets.end() && "Unknown macro definition");
1788 PerFileData *F = 0;
1789 uint64_t Offset = Pos->second;
1790 UnreadMacroRecordOffsets.erase(Pos);
1791
1792 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1793 if (Offset < Chain[I]->SizeInBits) {
1794 F = Chain[I];
1795 break;
1796 }
1797
1798 Offset -= Chain[I]->SizeInBits;
1799 }
1800 if (!F) {
1801 Error("Malformed macro record offset");
1802 return;
1803 }
1804
1805 ReadMacroRecord(*F, Offset);
1806}
1807
1808void ASTReader::LoadMacroDefinition(IdentifierInfo *II) {
1809 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos
1810 = UnreadMacroRecordOffsets.find(II);
1811 LoadMacroDefinition(Pos);
Douglas Gregor88a35862010-01-04 19:18:44 +00001812}
1813
Sebastian Redlf73c93f2010-09-15 19:54:06 +00001814MacroDefinition *ASTReader::getMacroDefinition(MacroID ID) {
Douglas Gregor77424bc2010-10-02 19:29:26 +00001815 if (ID == 0 || ID > MacroDefinitionsLoaded.size())
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001816 return 0;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001817
Douglas Gregor77424bc2010-10-02 19:29:26 +00001818 if (!MacroDefinitionsLoaded[ID - 1]) {
1819 unsigned Index = ID - 1;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001820 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1821 PerFileData &F = *Chain[N - I - 1];
1822 if (Index < F.LocalNumMacroDefinitions) {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001823 SavedStreamPosition SavedPosition(F.PreprocessorDetailCursor);
1824 F.PreprocessorDetailCursor.JumpToBit(F.MacroDefinitionOffsets[Index]);
1825 LoadPreprocessedEntity(F);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001826 break;
1827 }
1828 Index -= F.LocalNumMacroDefinitions;
1829 }
Douglas Gregor77424bc2010-10-02 19:29:26 +00001830 assert(MacroDefinitionsLoaded[ID - 1] && "Broken chain");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001831 }
1832
Douglas Gregor77424bc2010-10-02 19:29:26 +00001833 return MacroDefinitionsLoaded[ID - 1];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001834}
1835
Douglas Gregore650c8c2009-07-07 00:12:59 +00001836/// \brief If we are loading a relocatable PCH file, and the filename is
1837/// not an absolute path, add the system root to the beginning of the file
1838/// name.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001839void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001840 // If this is not a relocatable PCH file, there's nothing to do.
1841 if (!RelocatablePCH)
1842 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001843
Michael J. Spencer256053b2010-12-17 21:22:22 +00001844 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
Douglas Gregore650c8c2009-07-07 00:12:59 +00001845 return;
1846
Douglas Gregore650c8c2009-07-07 00:12:59 +00001847 if (isysroot == 0) {
1848 // If no system root was given, default to '/'
1849 Filename.insert(Filename.begin(), '/');
1850 return;
1851 }
Mike Stump1eb44332009-09-09 15:08:12 +00001852
Douglas Gregore650c8c2009-07-07 00:12:59 +00001853 unsigned Length = strlen(isysroot);
1854 if (isysroot[Length - 1] != '/')
1855 Filename.insert(Filename.begin(), '/');
Mike Stump1eb44332009-09-09 15:08:12 +00001856
Douglas Gregore650c8c2009-07-07 00:12:59 +00001857 Filename.insert(Filename.begin(), isysroot, isysroot + Length);
1858}
1859
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001860ASTReader::ASTReadResult
Sebastian Redl571db7f2010-08-18 23:56:56 +00001861ASTReader::ReadASTBlock(PerFileData &F) {
Sebastian Redl9137a522010-07-16 17:50:48 +00001862 llvm::BitstreamCursor &Stream = F.Stream;
1863
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001864 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001865 Error("malformed block record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001866 return Failure;
1867 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001868
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001869 // Read all of the records and blocks for the ASt file.
Douglas Gregor8038d512009-04-10 17:25:41 +00001870 RecordData Record;
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001871 bool First = true;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001872 while (!Stream.AtEndOfStream()) {
1873 unsigned Code = Stream.ReadCode();
1874 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001875 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001876 Error("error at end of module block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001877 return Failure;
1878 }
Chris Lattner7356a312009-04-11 21:15:38 +00001879
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001880 return Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001881 }
1882
1883 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1884 switch (Stream.ReadSubBlockID()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001885 case DECLTYPES_BLOCK_ID:
Chris Lattner6367f6d2009-04-27 01:05:14 +00001886 // We lazily load the decls block, but we want to set up the
1887 // DeclsCursor cursor to point into it. Clone our current bitcode
1888 // cursor to it, enter the block and read the abbrevs in that block.
1889 // With the main cursor, we just skip over it.
Sebastian Redl9137a522010-07-16 17:50:48 +00001890 F.DeclsCursor = Stream;
Chris Lattner6367f6d2009-04-27 01:05:14 +00001891 if (Stream.SkipBlock() || // Skip with the main cursor.
1892 // Read the abbrevs.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001893 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001894 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001895 return Failure;
1896 }
1897 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001898
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00001899 case DECL_UPDATES_BLOCK_ID:
1900 if (Stream.SkipBlock()) {
1901 Error("malformed block record in AST file");
1902 return Failure;
1903 }
1904 break;
1905
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001906 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl9137a522010-07-16 17:50:48 +00001907 F.MacroCursor = Stream;
Douglas Gregor88a35862010-01-04 19:18:44 +00001908 if (PP)
1909 PP->setExternalSource(this);
1910
Douglas Gregorecdcb882010-10-20 22:00:55 +00001911 if (Stream.SkipBlock() ||
1912 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001913 Error("malformed block record in AST file");
Chris Lattner7356a312009-04-11 21:15:38 +00001914 return Failure;
1915 }
Douglas Gregorecdcb882010-10-20 22:00:55 +00001916 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
Chris Lattner7356a312009-04-11 21:15:38 +00001917 break;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00001918
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001919 case PREPROCESSOR_DETAIL_BLOCK_ID:
1920 F.PreprocessorDetailCursor = Stream;
1921 if (Stream.SkipBlock() ||
1922 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
1923 PREPROCESSOR_DETAIL_BLOCK_ID)) {
1924 Error("malformed preprocessor detail record in AST file");
1925 return Failure;
1926 }
1927 F.PreprocessorDetailStartOffset
1928 = F.PreprocessorDetailCursor.GetCurrentBitNo();
1929 break;
1930
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001931 case SOURCE_MANAGER_BLOCK_ID:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001932 switch (ReadSourceManagerBlock(F)) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00001933 case Success:
1934 break;
1935
1936 case Failure:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001937 Error("malformed source manager block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001938 return Failure;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001939
1940 case IgnorePCH:
1941 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001942 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001943 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001944 }
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001945 First = false;
Douglas Gregor8038d512009-04-10 17:25:41 +00001946 continue;
1947 }
1948
1949 if (Code == llvm::bitc::DEFINE_ABBREV) {
1950 Stream.ReadAbbrevRecord();
1951 continue;
1952 }
1953
1954 // Read and process a record.
1955 Record.clear();
Douglas Gregor2bec0412009-04-10 21:16:55 +00001956 const char *BlobStart = 0;
1957 unsigned BlobLen = 0;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001958 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00001959 &BlobStart, &BlobLen)) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001960 default: // Default behavior: ignore.
1961 break;
1962
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001963 case METADATA: {
1964 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1965 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001966 : diag::warn_pch_version_too_new);
1967 return IgnorePCH;
1968 }
1969
1970 RelocatablePCH = Record[4];
1971 if (Listener) {
1972 std::string TargetTriple(BlobStart, BlobLen);
1973 if (Listener->ReadTargetTriple(TargetTriple))
1974 return IgnorePCH;
1975 }
1976 break;
1977 }
1978
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001979 case CHAINED_METADATA: {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001980 if (!First) {
1981 Error("CHAINED_METADATA is not first record in block");
1982 return Failure;
1983 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001984 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1985 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001986 : diag::warn_pch_version_too_new);
1987 return IgnorePCH;
1988 }
1989
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00001990 // Load the chained file, which is always a PCH file.
1991 switch(ReadASTCore(llvm::StringRef(BlobStart, BlobLen), PCH)) {
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001992 case Failure: return Failure;
1993 // If we have to ignore the dependency, we'll have to ignore this too.
1994 case IgnorePCH: return IgnorePCH;
1995 case Success: break;
1996 }
1997 break;
1998 }
1999
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002000 case TYPE_OFFSET:
Sebastian Redl12d6da02010-07-19 22:06:55 +00002001 if (F.LocalNumTypes != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002002 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002003 return Failure;
2004 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00002005 F.TypeOffsets = (const uint32_t *)BlobStart;
2006 F.LocalNumTypes = Record[0];
Douglas Gregor8038d512009-04-10 17:25:41 +00002007 break;
2008
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002009 case DECL_OFFSET:
Sebastian Redl12d6da02010-07-19 22:06:55 +00002010 if (F.LocalNumDecls != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002011 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002012 return Failure;
2013 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00002014 F.DeclOffsets = (const uint32_t *)BlobStart;
2015 F.LocalNumDecls = Record[0];
Douglas Gregor8038d512009-04-10 17:25:41 +00002016 break;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002017
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002018 case TU_UPDATE_LEXICAL: {
Sebastian Redld692af72010-07-27 18:24:41 +00002019 DeclContextInfo Info = {
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002020 /* No visible information */ 0,
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002021 reinterpret_cast<const KindDeclIDPair *>(BlobStart),
2022 BlobLen / sizeof(KindDeclIDPair)
Sebastian Redld692af72010-07-27 18:24:41 +00002023 };
Douglas Gregor3747ee72010-10-01 01:18:02 +00002024 DeclContextOffsets[Context ? Context->getTranslationUnitDecl() : 0]
2025 .push_back(Info);
Sebastian Redld692af72010-07-27 18:24:41 +00002026 break;
2027 }
2028
Sebastian Redle1dde812010-08-24 00:50:04 +00002029 case UPDATE_VISIBLE: {
2030 serialization::DeclID ID = Record[0];
2031 void *Table = ASTDeclContextNameLookupTable::Create(
2032 (const unsigned char *)BlobStart + Record[1],
2033 (const unsigned char *)BlobStart,
2034 ASTDeclContextNameLookupTrait(*this));
Douglas Gregor3747ee72010-10-01 01:18:02 +00002035 if (ID == 1 && Context) { // Is it the TU?
Sebastian Redle1dde812010-08-24 00:50:04 +00002036 DeclContextInfo Info = {
2037 Table, /* No lexical inforamtion */ 0, 0
2038 };
2039 DeclContextOffsets[Context->getTranslationUnitDecl()].push_back(Info);
2040 } else
2041 PendingVisibleUpdates[ID].push_back(Table);
2042 break;
2043 }
2044
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002045 case REDECLS_UPDATE_LATEST: {
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00002046 assert(Record.size() % 2 == 0 && "Expected pairs of DeclIDs");
2047 for (unsigned i = 0, e = Record.size(); i < e; i += 2) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002048 DeclID First = Record[i], Latest = Record[i+1];
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00002049 assert((FirstLatestDeclIDs.find(First) == FirstLatestDeclIDs.end() ||
2050 Latest > FirstLatestDeclIDs[First]) &&
2051 "The new latest is supposed to come after the previous latest");
2052 FirstLatestDeclIDs[First] = Latest;
2053 }
2054 break;
2055 }
2056
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002057 case LANGUAGE_OPTIONS:
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00002058 if (ParseLanguageOptions(Record) && !DisableValidation)
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002059 return IgnorePCH;
2060 break;
Douglas Gregor2bec0412009-04-10 21:16:55 +00002061
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002062 case IDENTIFIER_TABLE:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002063 F.IdentifierTableData = BlobStart;
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002064 if (Record[0]) {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002065 F.IdentifierLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002066 = ASTIdentifierLookupTable::Create(
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002067 (const unsigned char *)F.IdentifierTableData + Record[0],
2068 (const unsigned char *)F.IdentifierTableData,
Sebastian Redlc3632732010-10-05 15:59:54 +00002069 ASTIdentifierLookupTrait(*this, F));
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002070 if (PP)
2071 PP->getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002072 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002073 break;
2074
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002075 case IDENTIFIER_OFFSET:
Sebastian Redl2da08f92010-07-19 22:28:42 +00002076 if (F.LocalNumIdentifiers != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002077 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00002078 return Failure;
2079 }
Sebastian Redl2da08f92010-07-19 22:28:42 +00002080 F.IdentifierOffsets = (const uint32_t *)BlobStart;
2081 F.LocalNumIdentifiers = Record[0];
Douglas Gregorafaf3082009-04-11 00:14:32 +00002082 break;
Douglas Gregorfdd01722009-04-14 00:24:19 +00002083
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002084 case EXTERNAL_DEFINITIONS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002085 // Optimization for the first block.
2086 if (ExternalDefinitions.empty())
2087 ExternalDefinitions.swap(Record);
2088 else
2089 ExternalDefinitions.insert(ExternalDefinitions.end(),
2090 Record.begin(), Record.end());
Douglas Gregorfdd01722009-04-14 00:24:19 +00002091 break;
Douglas Gregor3e1af842009-04-17 22:13:46 +00002092
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002093 case SPECIAL_TYPES:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002094 // Optimization for the first block
2095 if (SpecialTypes.empty())
2096 SpecialTypes.swap(Record);
2097 else
2098 SpecialTypes.insert(SpecialTypes.end(), Record.begin(), Record.end());
Douglas Gregorad1de002009-04-18 05:55:16 +00002099 break;
2100
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002101 case STATISTICS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002102 TotalNumStatements += Record[0];
2103 TotalNumMacros += Record[1];
2104 TotalLexicalDeclContexts += Record[2];
2105 TotalVisibleDeclContexts += Record[3];
Douglas Gregor3e1af842009-04-17 22:13:46 +00002106 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002107
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002108 case TENTATIVE_DEFINITIONS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002109 // Optimization for the first block.
2110 if (TentativeDefinitions.empty())
2111 TentativeDefinitions.swap(Record);
2112 else
2113 TentativeDefinitions.insert(TentativeDefinitions.end(),
2114 Record.begin(), Record.end());
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002115 break;
Douglas Gregor14c22f22009-04-22 22:18:58 +00002116
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002117 case UNUSED_FILESCOPED_DECLS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002118 // Optimization for the first block.
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002119 if (UnusedFileScopedDecls.empty())
2120 UnusedFileScopedDecls.swap(Record);
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002121 else
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002122 UnusedFileScopedDecls.insert(UnusedFileScopedDecls.end(),
2123 Record.begin(), Record.end());
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002124 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002125
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002126 case WEAK_UNDECLARED_IDENTIFIERS:
Sebastian Redl40566802010-08-05 18:21:25 +00002127 // Later blocks overwrite earlier ones.
2128 WeakUndeclaredIdentifiers.swap(Record);
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002129 break;
2130
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002131 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002132 // Optimization for the first block.
2133 if (LocallyScopedExternalDecls.empty())
2134 LocallyScopedExternalDecls.swap(Record);
2135 else
2136 LocallyScopedExternalDecls.insert(LocallyScopedExternalDecls.end(),
2137 Record.begin(), Record.end());
Douglas Gregor14c22f22009-04-22 22:18:58 +00002138 break;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002139
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002140 case SELECTOR_OFFSETS:
Sebastian Redl059612d2010-08-03 21:58:15 +00002141 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redl725cd962010-08-04 20:40:17 +00002142 F.LocalNumSelectors = Record[0];
Douglas Gregor83941df2009-04-25 17:48:32 +00002143 break;
2144
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002145 case METHOD_POOL:
Sebastian Redl725cd962010-08-04 20:40:17 +00002146 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor83941df2009-04-25 17:48:32 +00002147 if (Record[0])
Sebastian Redl725cd962010-08-04 20:40:17 +00002148 F.SelectorLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002149 = ASTSelectorLookupTable::Create(
Sebastian Redl725cd962010-08-04 20:40:17 +00002150 F.SelectorLookupTableData + Record[0],
2151 F.SelectorLookupTableData,
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002152 ASTSelectorLookupTrait(*this));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002153 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002154 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002155
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002156 case REFERENCED_SELECTOR_POOL:
Sebastian Redlc3632732010-10-05 15:59:54 +00002157 F.ReferencedSelectorsData.swap(Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002158 break;
2159
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002160 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002161 if (!Record.empty() && Listener)
2162 Listener->ReadCounter(Record[0]);
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002163 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002164
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002165 case SOURCE_LOCATION_OFFSETS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002166 F.SLocOffsets = (const uint32_t *)BlobStart;
2167 F.LocalNumSLocEntries = Record[0];
Sebastian Redl8db9fae2010-09-22 20:19:08 +00002168 F.LocalSLocSize = Record[1];
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002169 break;
2170
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002171 case SOURCE_LOCATION_PRELOADS:
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002172 if (PreloadSLocEntries.empty())
2173 PreloadSLocEntries.swap(Record);
2174 else
2175 PreloadSLocEntries.insert(PreloadSLocEntries.end(),
2176 Record.begin(), Record.end());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002177 break;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002178
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002179 case STAT_CACHE: {
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00002180 if (!DisableStatCache) {
2181 ASTStatCache *MyStatCache =
2182 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
2183 (const unsigned char *)BlobStart,
2184 NumStatHits, NumStatMisses);
2185 FileMgr.addStatCache(MyStatCache);
2186 F.StatCache = MyStatCache;
2187 }
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002188 break;
Douglas Gregor52e71082009-10-16 18:18:30 +00002189 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002190
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002191 case EXT_VECTOR_DECLS:
Sebastian Redla9f23682010-07-28 21:38:49 +00002192 // Optimization for the first block.
2193 if (ExtVectorDecls.empty())
2194 ExtVectorDecls.swap(Record);
2195 else
2196 ExtVectorDecls.insert(ExtVectorDecls.end(),
2197 Record.begin(), Record.end());
Douglas Gregorb81c1702009-04-27 20:06:05 +00002198 break;
2199
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002200 case VTABLE_USES:
Sebastian Redl40566802010-08-05 18:21:25 +00002201 // Later tables overwrite earlier ones.
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002202 VTableUses.swap(Record);
2203 break;
2204
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002205 case DYNAMIC_CLASSES:
Sebastian Redl40566802010-08-05 18:21:25 +00002206 // Optimization for the first block.
2207 if (DynamicClasses.empty())
2208 DynamicClasses.swap(Record);
2209 else
2210 DynamicClasses.insert(DynamicClasses.end(),
2211 Record.begin(), Record.end());
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002212 break;
2213
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002214 case PENDING_IMPLICIT_INSTANTIATIONS:
Sebastian Redlc3632732010-10-05 15:59:54 +00002215 F.PendingInstantiations.swap(Record);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002216 break;
2217
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002218 case SEMA_DECL_REFS:
Sebastian Redl40566802010-08-05 18:21:25 +00002219 // Later tables overwrite earlier ones.
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002220 SemaDeclRefs.swap(Record);
2221 break;
2222
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002223 case ORIGINAL_FILE_NAME:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002224 // The primary AST will be the last to get here, so it will be the one
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002225 // that's used.
Daniel Dunbar7b5a1212009-11-11 05:29:04 +00002226 ActualOriginalFileName.assign(BlobStart, BlobLen);
2227 OriginalFileName = ActualOriginalFileName;
Douglas Gregore650c8c2009-07-07 00:12:59 +00002228 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregorb64c1932009-05-12 01:31:05 +00002229 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002230
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002231 case ORIGINAL_PCH_DIR:
2232 // The primary AST will be the last to get here, so it will be the one
2233 // that's used.
2234 OriginalDir.assign(BlobStart, BlobLen);
2235 break;
2236
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002237 case VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek974be4d2010-02-12 23:31:14 +00002238 const std::string &CurBranch = getClangFullRepositoryVersion();
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002239 llvm::StringRef ASTBranch(BlobStart, BlobLen);
2240 if (llvm::StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2241 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregor445e23e2009-10-05 21:07:28 +00002242 return IgnorePCH;
2243 }
2244 break;
2245 }
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002246
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002247 case MACRO_DEFINITION_OFFSETS:
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002248 F.MacroDefinitionOffsets = (const uint32_t *)BlobStart;
2249 F.NumPreallocatedPreprocessingEntities = Record[0];
2250 F.LocalNumMacroDefinitions = Record[1];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002251 break;
Sebastian Redl0b17c612010-08-13 00:28:03 +00002252
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002253 case DECL_UPDATE_OFFSETS: {
2254 if (Record.size() % 2 != 0) {
2255 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2256 return Failure;
2257 }
2258 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
2259 DeclUpdateOffsets[static_cast<DeclID>(Record[I])]
2260 .push_back(std::make_pair(&F, Record[I+1]));
2261 break;
2262 }
2263
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002264 case DECL_REPLACEMENTS: {
Sebastian Redl0b17c612010-08-13 00:28:03 +00002265 if (Record.size() % 2 != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002266 Error("invalid DECL_REPLACEMENTS block in AST file");
Sebastian Redl0b17c612010-08-13 00:28:03 +00002267 return Failure;
2268 }
2269 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002270 ReplacedDecls[static_cast<DeclID>(Record[I])] =
Sebastian Redl0b17c612010-08-13 00:28:03 +00002271 std::make_pair(&F, Record[I+1]);
2272 break;
2273 }
Douglas Gregor7c789c12010-10-29 22:39:52 +00002274
2275 case CXX_BASE_SPECIFIER_OFFSETS: {
2276 if (F.LocalNumCXXBaseSpecifiers != 0) {
2277 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2278 return Failure;
2279 }
2280
2281 F.LocalNumCXXBaseSpecifiers = Record[0];
2282 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart;
2283 break;
2284 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002285
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002286 case DIAG_PRAGMA_MAPPINGS:
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002287 if (Record.size() % 2 != 0) {
2288 Error("invalid DIAG_USER_MAPPINGS block in AST file");
2289 return Failure;
2290 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002291 if (PragmaDiagMappings.empty())
2292 PragmaDiagMappings.swap(Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002293 else
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002294 PragmaDiagMappings.insert(PragmaDiagMappings.end(),
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002295 Record.begin(), Record.end());
2296 break;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002297
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002298 case CUDA_SPECIAL_DECL_REFS:
2299 // Later tables overwrite earlier ones.
2300 CUDASpecialDeclRefs.swap(Record);
2301 break;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002302
2303 case HEADER_SEARCH_TABLE:
2304 F.HeaderFileInfoTableData = BlobStart;
2305 F.LocalNumHeaderFileInfos = Record[1];
2306 if (Record[0]) {
2307 F.HeaderFileInfoTable
2308 = HeaderFileInfoLookupTable::Create(
2309 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
2310 (const unsigned char *)F.HeaderFileInfoTableData);
2311 if (PP)
2312 PP->getHeaderSearchInfo().SetExternalSource(this);
2313 }
2314 break;
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002315
2316 case FP_PRAGMA_OPTIONS:
2317 // Later tables overwrite earlier ones.
2318 FPPragmaOptions.swap(Record);
2319 break;
2320
2321 case OPENCL_EXTENSIONS:
2322 // Later tables overwrite earlier ones.
2323 OpenCLExtensions.swap(Record);
2324 break;
Douglas Gregorafaf3082009-04-11 00:14:32 +00002325 }
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002326 First = false;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002327 }
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002328 Error("premature end of bitstream in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002329 return Failure;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002330}
2331
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002332ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
2333 ASTFileType Type) {
2334 switch(ReadASTCore(FileName, Type)) {
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002335 case Failure: return Failure;
2336 case IgnorePCH: return IgnorePCH;
2337 case Success: break;
2338 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002339
2340 // Here comes stuff that we only do once the entire chain is loaded.
2341
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002342 // Allocate space for loaded slocentries, identifiers, decls and types.
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002343 unsigned TotalNumIdentifiers = 0, TotalNumTypes = 0, TotalNumDecls = 0,
Sebastian Redl725cd962010-08-04 20:40:17 +00002344 TotalNumPreallocatedPreprocessingEntities = 0, TotalNumMacroDefs = 0,
2345 TotalNumSelectors = 0;
Sebastian Redl12d6da02010-07-19 22:06:55 +00002346 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002347 TotalNumSLocEntries += Chain[I]->LocalNumSLocEntries;
Sebastian Redl8db9fae2010-09-22 20:19:08 +00002348 NextSLocOffset += Chain[I]->LocalSLocSize;
Sebastian Redl2da08f92010-07-19 22:28:42 +00002349 TotalNumIdentifiers += Chain[I]->LocalNumIdentifiers;
Sebastian Redl12d6da02010-07-19 22:06:55 +00002350 TotalNumTypes += Chain[I]->LocalNumTypes;
2351 TotalNumDecls += Chain[I]->LocalNumDecls;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002352 TotalNumPreallocatedPreprocessingEntities +=
2353 Chain[I]->NumPreallocatedPreprocessingEntities;
2354 TotalNumMacroDefs += Chain[I]->LocalNumMacroDefinitions;
Sebastian Redl725cd962010-08-04 20:40:17 +00002355 TotalNumSelectors += Chain[I]->LocalNumSelectors;
Sebastian Redl12d6da02010-07-19 22:06:55 +00002356 }
Sebastian Redl8db9fae2010-09-22 20:19:08 +00002357 SourceMgr.PreallocateSLocEntries(this, TotalNumSLocEntries, NextSLocOffset);
Sebastian Redl2da08f92010-07-19 22:28:42 +00002358 IdentifiersLoaded.resize(TotalNumIdentifiers);
Sebastian Redl12d6da02010-07-19 22:06:55 +00002359 TypesLoaded.resize(TotalNumTypes);
2360 DeclsLoaded.resize(TotalNumDecls);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002361 MacroDefinitionsLoaded.resize(TotalNumMacroDefs);
2362 if (PP) {
2363 if (TotalNumIdentifiers > 0)
2364 PP->getHeaderSearchInfo().SetExternalLookup(this);
2365 if (TotalNumPreallocatedPreprocessingEntities > 0) {
2366 if (!PP->getPreprocessingRecord())
2367 PP->createPreprocessingRecord();
2368 PP->getPreprocessingRecord()->SetExternalSource(*this,
2369 TotalNumPreallocatedPreprocessingEntities);
2370 }
2371 }
Sebastian Redl725cd962010-08-04 20:40:17 +00002372 SelectorsLoaded.resize(TotalNumSelectors);
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002373 // Preload SLocEntries.
2374 for (unsigned I = 0, N = PreloadSLocEntries.size(); I != N; ++I) {
2375 ASTReadResult Result = ReadSLocEntryRecord(PreloadSLocEntries[I]);
2376 if (Result != Success)
2377 return Result;
2378 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00002379
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002380 // Check the predefines buffers.
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00002381 if (!DisableValidation && CheckPredefinesBuffers())
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002382 return IgnorePCH;
2383
2384 if (PP) {
2385 // Initialization of keywords and pragmas occurs before the
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002386 // AST file is read, so there may be some identifiers that were
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002387 // loaded into the IdentifierTable before we intercepted the
2388 // creation of identifiers. Iterate through the list of known
2389 // identifiers and determine whether we have to establish
2390 // preprocessor definitions or top-level identifier declaration
2391 // chains for those identifiers.
2392 //
2393 // We copy the IdentifierInfo pointers to a small vector first,
2394 // since de-serializing declarations or macro definitions can add
2395 // new entries into the identifier table, invalidating the
2396 // iterators.
2397 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
2398 for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(),
2399 IdEnd = PP->getIdentifierTable().end();
2400 Id != IdEnd; ++Id)
2401 Identifiers.push_back(Id->second);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002402 // We need to search the tables in all files.
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002403 for (unsigned J = 0, M = Chain.size(); J != M; ++J) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002404 ASTIdentifierLookupTable *IdTable
2405 = (ASTIdentifierLookupTable *)Chain[J]->IdentifierLookupTable;
2406 // Not all AST files necessarily have identifier tables, only the useful
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00002407 // ones.
2408 if (!IdTable)
2409 continue;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002410 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
2411 IdentifierInfo *II = Identifiers[I];
2412 // Look in the on-disk hash tables for an entry for this identifier
Sebastian Redlc3632732010-10-05 15:59:54 +00002413 ASTIdentifierLookupTrait Info(*this, *Chain[J], II);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002414 std::pair<const char*,unsigned> Key(II->getNameStart(),II->getLength());
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002415 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002416 if (Pos == IdTable->end())
2417 continue;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002418
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002419 // Dereferencing the iterator has the effect of populating the
2420 // IdentifierInfo node with the various declarations it needs.
2421 (void)*Pos;
2422 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002423 }
2424 }
2425
2426 if (Context)
2427 InitializeContext(*Context);
2428
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002429 if (DeserializationListener)
2430 DeserializationListener->ReaderInitialized(this);
2431
Douglas Gregor414cb642010-11-30 05:23:00 +00002432 // If this AST file is a precompiled preamble, then set the main file ID of
2433 // the source manager to the file source file from which the preamble was
2434 // built. This is the only valid way to use a precompiled preamble.
2435 if (Type == Preamble) {
2436 SourceLocation Loc
2437 = SourceMgr.getLocation(FileMgr.getFile(getOriginalSourceFile()), 1, 1);
2438 if (Loc.isValid()) {
2439 std::pair<FileID, unsigned> Decomposed = SourceMgr.getDecomposedLoc(Loc);
2440 SourceMgr.SetPreambleFileID(Decomposed.first);
2441 }
2442 }
2443
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002444 return Success;
2445}
2446
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002447ASTReader::ASTReadResult ASTReader::ReadASTCore(llvm::StringRef FileName,
2448 ASTFileType Type) {
Sebastian Redla866e652010-10-01 19:59:12 +00002449 PerFileData *Prev = Chain.empty() ? 0 : Chain.back();
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002450 Chain.push_back(new PerFileData(Type));
Sebastian Redl9137a522010-07-16 17:50:48 +00002451 PerFileData &F = *Chain.back();
Sebastian Redla866e652010-10-01 19:59:12 +00002452 if (Prev)
2453 Prev->NextInSource = &F;
2454 else
2455 FirstInSource = &F;
2456 F.Loaders.push_back(Prev);
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002457
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002458 // Set the AST file name.
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002459 F.FileName = FileName;
2460
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002461 if (FileName != "-") {
2462 CurrentDir = llvm::sys::path::parent_path(FileName);
2463 if (CurrentDir.empty()) CurrentDir = ".";
2464 }
2465
Argyrios Kyrtzidisb0f4b9a2011-03-09 17:21:42 +00002466 if (!ASTBuffers.empty()) {
Sebastian Redl56558372011-04-14 14:07:41 +00002467 F.Buffer.reset(ASTBuffers.back());
2468 ASTBuffers.pop_back();
Argyrios Kyrtzidisb0f4b9a2011-03-09 17:21:42 +00002469 assert(F.Buffer && "Passed null buffer");
2470 } else {
2471 // Open the AST file.
2472 //
2473 // FIXME: This shouldn't be here, we should just take a raw_ostream.
2474 std::string ErrStr;
2475 llvm::error_code ec;
2476 if (FileName == "-") {
2477 ec = llvm::MemoryBuffer::getSTDIN(F.Buffer);
2478 if (ec)
2479 ErrStr = ec.message();
2480 } else
2481 F.Buffer.reset(FileMgr.getBufferForFile(FileName, &ErrStr));
2482 if (!F.Buffer) {
2483 Error(ErrStr.c_str());
2484 return IgnorePCH;
2485 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002486 }
2487
2488 // Initialize the stream
2489 F.StreamFile.init((const unsigned char *)F.Buffer->getBufferStart(),
2490 (const unsigned char *)F.Buffer->getBufferEnd());
Sebastian Redl9137a522010-07-16 17:50:48 +00002491 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002492 Stream.init(F.StreamFile);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002493 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002494
2495 // Sniff for the signature.
2496 if (Stream.Read(8) != 'C' ||
2497 Stream.Read(8) != 'P' ||
2498 Stream.Read(8) != 'C' ||
2499 Stream.Read(8) != 'H') {
2500 Diag(diag::err_not_a_pch_file) << FileName;
2501 return Failure;
2502 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002503
Douglas Gregor2cf26342009-04-09 22:27:44 +00002504 while (!Stream.AtEndOfStream()) {
2505 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00002506
Douglas Gregore1d918e2009-04-10 23:10:45 +00002507 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002508 Error("invalid record at top-level of AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002509 return Failure;
2510 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002511
2512 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregor668c1a42009-04-21 22:25:48 +00002513
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002514 // We only know the AST subblock ID.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002515 switch (BlockID) {
2516 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002517 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002518 Error("malformed BlockInfoBlock in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002519 return Failure;
2520 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002521 break;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002522 case AST_BLOCK_ID:
Sebastian Redl571db7f2010-08-18 23:56:56 +00002523 switch (ReadASTBlock(F)) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002524 case Success:
2525 break;
2526
2527 case Failure:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002528 return Failure;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002529
2530 case IgnorePCH:
Douglas Gregor2bec0412009-04-10 21:16:55 +00002531 // FIXME: We could consider reading through to the end of this
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002532 // AST block, skipping subblocks, to see if there are other
2533 // AST blocks elsewhere.
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002534
2535 // Clear out any preallocated source location entries, so that
2536 // the source manager does not try to resolve them later.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002537 SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002538
2539 // Remove the stat cache.
Sebastian Redl9137a522010-07-16 17:50:48 +00002540 if (F.StatCache)
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002541 FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002542
Douglas Gregore1d918e2009-04-10 23:10:45 +00002543 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002544 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002545 break;
2546 default:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002547 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002548 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002549 return Failure;
2550 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002551 break;
2552 }
Mike Stump1eb44332009-09-09 15:08:12 +00002553 }
2554
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002555 return Success;
2556}
2557
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002558void ASTReader::setPreprocessor(Preprocessor &pp) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002559 PP = &pp;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002560
2561 unsigned TotalNum = 0;
2562 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
2563 TotalNum += Chain[I]->NumPreallocatedPreprocessingEntities;
2564 if (TotalNum) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002565 if (!PP->getPreprocessingRecord())
2566 PP->createPreprocessingRecord();
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002567 PP->getPreprocessingRecord()->SetExternalSource(*this, TotalNum);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002568 }
2569}
2570
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002571void ASTReader::InitializeContext(ASTContext &Ctx) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002572 Context = &Ctx;
2573 assert(Context && "Passed null context!");
2574
2575 assert(PP && "Forgot to set Preprocessor ?");
2576 PP->getIdentifierTable().setExternalIdentifierLookup(this);
2577 PP->getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregor88a35862010-01-04 19:18:44 +00002578 PP->setExternalSource(this);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002579 PP->getHeaderSearchInfo().SetExternalSource(this);
2580
Douglas Gregor3747ee72010-10-01 01:18:02 +00002581 // If we have an update block for the TU waiting, we have to add it before
2582 // deserializing the decl.
2583 DeclContextOffsetsMap::iterator DCU = DeclContextOffsets.find(0);
2584 if (DCU != DeclContextOffsets.end()) {
2585 // Insertion could invalidate map, so grab vector.
2586 DeclContextInfos T;
2587 T.swap(DCU->second);
2588 DeclContextOffsets.erase(DCU);
2589 DeclContextOffsets[Ctx.getTranslationUnitDecl()].swap(T);
2590 }
2591
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002592 // Load the translation unit declaration
Argyrios Kyrtzidis8871a442010-07-08 17:13:02 +00002593 GetTranslationUnitDecl();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002594
2595 // Load the special types.
2596 Context->setBuiltinVaListType(
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002597 GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST]));
2598 if (unsigned Id = SpecialTypes[SPECIAL_TYPE_OBJC_ID])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002599 Context->setObjCIdType(GetType(Id));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002600 if (unsigned Sel = SpecialTypes[SPECIAL_TYPE_OBJC_SELECTOR])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002601 Context->setObjCSelType(GetType(Sel));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002602 if (unsigned Proto = SpecialTypes[SPECIAL_TYPE_OBJC_PROTOCOL])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002603 Context->setObjCProtoType(GetType(Proto));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002604 if (unsigned Class = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002605 Context->setObjCClassType(GetType(Class));
Steve Naroff14108da2009-07-10 23:34:53 +00002606
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002607 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002608 Context->setCFConstantStringType(GetType(String));
Mike Stump1eb44332009-09-09 15:08:12 +00002609 if (unsigned FastEnum
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002610 = SpecialTypes[SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002611 Context->setObjCFastEnumerationStateType(GetType(FastEnum));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002612 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002613 QualType FileType = GetType(File);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002614 if (FileType.isNull()) {
2615 Error("FILE type is NULL");
2616 return;
2617 }
John McCall183700f2009-09-21 23:43:11 +00002618 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002619 Context->setFILEDecl(Typedef->getDecl());
2620 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002621 const TagType *Tag = FileType->getAs<TagType>();
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002622 if (!Tag) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002623 Error("Invalid FILE type in AST file");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002624 return;
2625 }
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002626 Context->setFILEDecl(Tag->getDecl());
2627 }
2628 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002629 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_jmp_buf]) {
Mike Stump782fa302009-07-28 02:25:19 +00002630 QualType Jmp_bufType = GetType(Jmp_buf);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002631 if (Jmp_bufType.isNull()) {
2632 Error("jmp_bug type is NULL");
2633 return;
2634 }
John McCall183700f2009-09-21 23:43:11 +00002635 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
Mike Stump782fa302009-07-28 02:25:19 +00002636 Context->setjmp_bufDecl(Typedef->getDecl());
2637 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002638 const TagType *Tag = Jmp_bufType->getAs<TagType>();
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002639 if (!Tag) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002640 Error("Invalid jmp_buf type in AST file");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002641 return;
2642 }
Mike Stump782fa302009-07-28 02:25:19 +00002643 Context->setjmp_bufDecl(Tag->getDecl());
2644 }
2645 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002646 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_sigjmp_buf]) {
Mike Stump782fa302009-07-28 02:25:19 +00002647 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002648 if (Sigjmp_bufType.isNull()) {
2649 Error("sigjmp_buf type is NULL");
2650 return;
2651 }
John McCall183700f2009-09-21 23:43:11 +00002652 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
Mike Stump782fa302009-07-28 02:25:19 +00002653 Context->setsigjmp_bufDecl(Typedef->getDecl());
2654 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002655 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002656 assert(Tag && "Invalid sigjmp_buf type in AST file");
Mike Stump782fa302009-07-28 02:25:19 +00002657 Context->setsigjmp_bufDecl(Tag->getDecl());
2658 }
2659 }
Mike Stump1eb44332009-09-09 15:08:12 +00002660 if (unsigned ObjCIdRedef
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002661 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION])
Douglas Gregord1571ac2009-08-21 00:27:50 +00002662 Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef);
Mike Stump1eb44332009-09-09 15:08:12 +00002663 if (unsigned ObjCClassRedef
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002664 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION])
Douglas Gregord1571ac2009-08-21 00:27:50 +00002665 Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002666 if (unsigned String = SpecialTypes[SPECIAL_TYPE_BLOCK_DESCRIPTOR])
Mike Stumpadaaad32009-10-20 02:12:22 +00002667 Context->setBlockDescriptorType(GetType(String));
Mike Stump083c25e2009-10-22 00:49:09 +00002668 if (unsigned String
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002669 = SpecialTypes[SPECIAL_TYPE_BLOCK_EXTENDED_DESCRIPTOR])
Mike Stump083c25e2009-10-22 00:49:09 +00002670 Context->setBlockDescriptorExtendedType(GetType(String));
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002671 if (unsigned ObjCSelRedef
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002672 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION])
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002673 Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002674 if (unsigned String = SpecialTypes[SPECIAL_TYPE_NS_CONSTANT_STRING])
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002675 Context->setNSConstantStringType(GetType(String));
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +00002676
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002677 if (SpecialTypes[SPECIAL_TYPE_INT128_INSTALLED])
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +00002678 Context->setInt128Installed();
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002679
Richard Smithad762fc2011-04-14 22:09:26 +00002680 if (unsigned AutoDeduct = SpecialTypes[SPECIAL_TYPE_AUTO_DEDUCT])
2681 Context->AutoDeductTy = GetType(AutoDeduct);
2682 if (unsigned AutoRRefDeduct = SpecialTypes[SPECIAL_TYPE_AUTO_RREF_DEDUCT])
2683 Context->AutoRRefDeductTy = GetType(AutoRRefDeduct);
2684
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002685 ReadPragmaDiagnosticMappings(Context->getDiagnostics());
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002686
2687 // If there were any CUDA special declarations, deserialize them.
2688 if (!CUDASpecialDeclRefs.empty()) {
2689 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
2690 Context->setcudaConfigureCallDecl(
2691 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
2692 }
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002693}
2694
Douglas Gregorb64c1932009-05-12 01:31:05 +00002695/// \brief Retrieve the name of the original source file name
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002696/// directly from the AST file, without actually loading the AST
Douglas Gregorb64c1932009-05-12 01:31:05 +00002697/// file.
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002698std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +00002699 FileManager &FileMgr,
Daniel Dunbar93ebb1b2009-12-03 09:13:06 +00002700 Diagnostic &Diags) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002701 // Open the AST file.
Douglas Gregorb64c1932009-05-12 01:31:05 +00002702 std::string ErrStr;
2703 llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
Chris Lattner39b49bc2010-11-23 08:35:12 +00002704 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
Douglas Gregorb64c1932009-05-12 01:31:05 +00002705 if (!Buffer) {
Daniel Dunbar93ebb1b2009-12-03 09:13:06 +00002706 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002707 return std::string();
2708 }
2709
2710 // Initialize the stream
2711 llvm::BitstreamReader StreamFile;
2712 llvm::BitstreamCursor Stream;
Mike Stump1eb44332009-09-09 15:08:12 +00002713 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregorb64c1932009-05-12 01:31:05 +00002714 (const unsigned char *)Buffer->getBufferEnd());
2715 Stream.init(StreamFile);
2716
2717 // Sniff for the signature.
2718 if (Stream.Read(8) != 'C' ||
2719 Stream.Read(8) != 'P' ||
2720 Stream.Read(8) != 'C' ||
2721 Stream.Read(8) != 'H') {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002722 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002723 return std::string();
2724 }
2725
2726 RecordData Record;
2727 while (!Stream.AtEndOfStream()) {
2728 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00002729
Douglas Gregorb64c1932009-05-12 01:31:05 +00002730 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2731 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump1eb44332009-09-09 15:08:12 +00002732
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002733 // We only know the AST subblock ID.
Douglas Gregorb64c1932009-05-12 01:31:05 +00002734 switch (BlockID) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002735 case AST_BLOCK_ID:
2736 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002737 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002738 return std::string();
2739 }
2740 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002741
Douglas Gregorb64c1932009-05-12 01:31:05 +00002742 default:
2743 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002744 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002745 return std::string();
2746 }
2747 break;
2748 }
2749 continue;
2750 }
2751
2752 if (Code == llvm::bitc::END_BLOCK) {
2753 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002754 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002755 return std::string();
2756 }
2757 continue;
2758 }
2759
2760 if (Code == llvm::bitc::DEFINE_ABBREV) {
2761 Stream.ReadAbbrevRecord();
2762 continue;
2763 }
2764
2765 Record.clear();
2766 const char *BlobStart = 0;
2767 unsigned BlobLen = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002768 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002769 == ORIGINAL_FILE_NAME)
Douglas Gregorb64c1932009-05-12 01:31:05 +00002770 return std::string(BlobStart, BlobLen);
Mike Stump1eb44332009-09-09 15:08:12 +00002771 }
Douglas Gregorb64c1932009-05-12 01:31:05 +00002772
2773 return std::string();
2774}
2775
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002776/// \brief Parse the record that corresponds to a LangOptions data
2777/// structure.
2778///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002779/// This routine parses the language options from the AST file and then gives
2780/// them to the AST listener if one is set.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002781///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002782/// \returns true if the listener deems the file unacceptable, false otherwise.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002783bool ASTReader::ParseLanguageOptions(
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002784 const llvm::SmallVectorImpl<uint64_t> &Record) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002785 if (Listener) {
2786 LangOptions LangOpts;
Mike Stump1eb44332009-09-09 15:08:12 +00002787
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002788 #define PARSE_LANGOPT(Option) \
2789 LangOpts.Option = Record[Idx]; \
2790 ++Idx
Mike Stump1eb44332009-09-09 15:08:12 +00002791
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002792 unsigned Idx = 0;
2793 PARSE_LANGOPT(Trigraphs);
2794 PARSE_LANGOPT(BCPLComment);
2795 PARSE_LANGOPT(DollarIdents);
2796 PARSE_LANGOPT(AsmPreprocessor);
2797 PARSE_LANGOPT(GNUMode);
Chandler Carrutheb5d7b72010-04-17 20:17:31 +00002798 PARSE_LANGOPT(GNUKeywords);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002799 PARSE_LANGOPT(ImplicitInt);
2800 PARSE_LANGOPT(Digraphs);
2801 PARSE_LANGOPT(HexFloats);
2802 PARSE_LANGOPT(C99);
Peter Collingbourne7e7fbd02011-04-15 00:35:23 +00002803 PARSE_LANGOPT(C1X);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002804 PARSE_LANGOPT(Microsoft);
2805 PARSE_LANGOPT(CPlusPlus);
2806 PARSE_LANGOPT(CPlusPlus0x);
2807 PARSE_LANGOPT(CXXOperatorNames);
2808 PARSE_LANGOPT(ObjC1);
2809 PARSE_LANGOPT(ObjC2);
2810 PARSE_LANGOPT(ObjCNonFragileABI);
Fariborz Jahanian412e7982010-02-09 19:31:38 +00002811 PARSE_LANGOPT(ObjCNonFragileABI2);
Fariborz Jahanianf84109e2011-01-07 18:59:25 +00002812 PARSE_LANGOPT(AppleKext);
Ted Kremenekc32647d2010-12-23 21:35:43 +00002813 PARSE_LANGOPT(ObjCDefaultSynthProperties);
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +00002814 PARSE_LANGOPT(NoConstantCFStrings);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002815 PARSE_LANGOPT(PascalStrings);
2816 PARSE_LANGOPT(WritableStrings);
2817 PARSE_LANGOPT(LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +00002818 PARSE_LANGOPT(AltiVec);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002819 PARSE_LANGOPT(Exceptions);
Anders Carlssonda4b7cf2011-02-19 23:53:54 +00002820 PARSE_LANGOPT(ObjCExceptions);
Anders Carlsson7da99b02011-02-23 03:04:54 +00002821 PARSE_LANGOPT(CXXExceptions);
2822 PARSE_LANGOPT(SjLjExceptions);
Douglas Gregor6f755502011-02-01 15:15:22 +00002823 PARSE_LANGOPT(MSBitfields);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002824 PARSE_LANGOPT(NeXTRuntime);
2825 PARSE_LANGOPT(Freestanding);
2826 PARSE_LANGOPT(NoBuiltin);
2827 PARSE_LANGOPT(ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +00002828 PARSE_LANGOPT(POSIXThreads);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002829 PARSE_LANGOPT(Blocks);
2830 PARSE_LANGOPT(EmitAllDecls);
2831 PARSE_LANGOPT(MathErrno);
Chris Lattnera4d71452010-06-26 21:25:03 +00002832 LangOpts.setSignedOverflowBehavior((LangOptions::SignedOverflowBehaviorTy)
2833 Record[Idx++]);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002834 PARSE_LANGOPT(HeinousExtensions);
2835 PARSE_LANGOPT(Optimize);
2836 PARSE_LANGOPT(OptimizeSize);
2837 PARSE_LANGOPT(Static);
2838 PARSE_LANGOPT(PICLevel);
2839 PARSE_LANGOPT(GNUInline);
2840 PARSE_LANGOPT(NoInline);
2841 PARSE_LANGOPT(AccessControl);
2842 PARSE_LANGOPT(CharIsSigned);
John Thompsona6fda122009-11-05 20:14:16 +00002843 PARSE_LANGOPT(ShortWChar);
Argyrios Kyrtzidisb1bdced2011-01-15 02:56:16 +00002844 PARSE_LANGOPT(ShortEnums);
Chris Lattnera4d71452010-06-26 21:25:03 +00002845 LangOpts.setGCMode((LangOptions::GCMode)Record[Idx++]);
John McCall1fb0caa2010-10-22 21:05:15 +00002846 LangOpts.setVisibilityMode((Visibility)Record[Idx++]);
Daniel Dunbarab8e2812009-09-21 04:16:19 +00002847 LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode)
Chris Lattnera4d71452010-06-26 21:25:03 +00002848 Record[Idx++]);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002849 PARSE_LANGOPT(InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +00002850 PARSE_LANGOPT(OpenCL);
Peter Collingbourne08a53262010-12-01 19:14:57 +00002851 PARSE_LANGOPT(CUDA);
Mike Stump9c276ae2009-12-12 01:27:46 +00002852 PARSE_LANGOPT(CatchUndefined);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002853 PARSE_LANGOPT(DefaultFPContract);
Roman Divacky1c51b1c2011-03-01 17:36:40 +00002854 PARSE_LANGOPT(ElideConstructors);
2855 PARSE_LANGOPT(SpellChecking);
Roman Divackycfe9af22011-03-01 17:40:53 +00002856 PARSE_LANGOPT(MRTD);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002857 #undef PARSE_LANGOPT
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002858
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002859 return Listener->ReadLanguageOptions(LangOpts);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002860 }
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002861
2862 return false;
2863}
2864
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002865void ASTReader::ReadPreprocessedEntities() {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002866 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2867 PerFileData &F = *Chain[I];
2868 if (!F.PreprocessorDetailCursor.getBitStreamReader())
2869 continue;
2870
2871 SavedStreamPosition SavedPosition(F.PreprocessorDetailCursor);
2872 F.PreprocessorDetailCursor.JumpToBit(F.PreprocessorDetailStartOffset);
2873 while (LoadPreprocessedEntity(F)) { }
2874 }
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002875}
2876
Douglas Gregor0a480292011-02-11 19:46:30 +00002877PreprocessedEntity *ASTReader::ReadPreprocessedEntityAtOffset(uint64_t Offset) {
Douglas Gregor89d99802010-11-30 06:16:57 +00002878 PerFileData *F = 0;
2879 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2880 if (Offset < Chain[I]->SizeInBits) {
2881 F = Chain[I];
2882 break;
2883 }
2884
2885 Offset -= Chain[I]->SizeInBits;
2886 }
2887
2888 if (!F) {
2889 Error("Malformed preprocessed entity offset");
2890 return 0;
2891 }
2892
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002893 // Keep track of where we are in the stream, then jump back there
2894 // after reading this entity.
2895 SavedStreamPosition SavedPosition(F->PreprocessorDetailCursor);
2896 F->PreprocessorDetailCursor.JumpToBit(Offset);
2897 return LoadPreprocessedEntity(*F);
Douglas Gregor89d99802010-11-30 06:16:57 +00002898}
2899
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002900HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
2901 HeaderFileInfoTrait Trait(FE->getName());
2902 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2903 PerFileData &F = *Chain[I];
2904 HeaderFileInfoLookupTable *Table
2905 = static_cast<HeaderFileInfoLookupTable *>(F.HeaderFileInfoTable);
2906 if (!Table)
2907 continue;
2908
2909 // Look in the on-disk hash table for an entry for this file name.
2910 HeaderFileInfoLookupTable::iterator Pos = Table->find(FE->getName(),
2911 &Trait);
2912 if (Pos == Table->end())
2913 continue;
2914
2915 HeaderFileInfo HFI = *Pos;
2916 if (Listener)
2917 Listener->ReadHeaderFileInfo(HFI, FE->getUID());
2918
2919 return HFI;
2920 }
2921
2922 return HeaderFileInfo();
2923}
2924
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002925void ASTReader::ReadPragmaDiagnosticMappings(Diagnostic &Diag) {
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002926 unsigned Idx = 0;
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002927 while (Idx < PragmaDiagMappings.size()) {
2928 SourceLocation
2929 Loc = SourceLocation::getFromRawEncoding(PragmaDiagMappings[Idx++]);
2930 while (1) {
2931 assert(Idx < PragmaDiagMappings.size() &&
2932 "Invalid data, didn't find '-1' marking end of diag/map pairs");
2933 if (Idx >= PragmaDiagMappings.size())
2934 break; // Something is messed up but at least avoid infinite loop in
2935 // release build.
2936 unsigned DiagID = PragmaDiagMappings[Idx++];
2937 if (DiagID == (unsigned)-1)
2938 break; // no more diag/map pairs for this location.
2939 diag::Mapping Map = (diag::Mapping)PragmaDiagMappings[Idx++];
2940 Diag.setDiagnosticMapping(DiagID, Map, Loc);
2941 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002942 }
2943}
2944
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002945/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002946ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002947 PerFileData *F = 0;
2948 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2949 F = Chain[N - I - 1];
2950 if (Index < F->LocalNumTypes)
2951 break;
2952 Index -= F->LocalNumTypes;
2953 }
2954 assert(F && F->LocalNumTypes > Index && "Broken chain");
Sebastian Redlc3632732010-10-05 15:59:54 +00002955 return RecordLocation(F, F->TypeOffsets[Index]);
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002956}
2957
2958/// \brief Read and return the type with the given index..
Douglas Gregor2cf26342009-04-09 22:27:44 +00002959///
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002960/// The index is the type ID, shifted and minus the number of predefs. This
2961/// routine actually reads the record corresponding to the type at the given
2962/// location. It is a helper routine for GetType, which deals with reading type
2963/// IDs.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002964QualType ASTReader::ReadTypeRecord(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002965 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00002966 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00002967
Douglas Gregor0b748912009-04-14 21:18:50 +00002968 // Keep track of where we are in the stream, then jump back there
2969 // after reading this type.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002970 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregor0b748912009-04-14 21:18:50 +00002971
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00002972 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redl27372b42010-08-11 18:52:41 +00002973
Douglas Gregord89275b2009-07-06 18:54:52 +00002974 // Note that we are loading a type record.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00002975 Deserializing AType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00002976
Sebastian Redlc3632732010-10-05 15:59:54 +00002977 DeclsCursor.JumpToBit(Loc.Offset);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002978 RecordData Record;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002979 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002980 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
2981 case TYPE_EXT_QUAL: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002982 if (Record.size() != 2) {
2983 Error("Incorrect encoding of extended qualifier type");
2984 return QualType();
2985 }
Douglas Gregor6d473962009-04-15 22:00:08 +00002986 QualType Base = GetType(Record[0]);
John McCall0953e762009-09-24 19:53:00 +00002987 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[1]);
2988 return Context->getQualifiedType(Base, Quals);
Douglas Gregor6d473962009-04-15 22:00:08 +00002989 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002990
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002991 case TYPE_COMPLEX: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002992 if (Record.size() != 1) {
2993 Error("Incorrect encoding of complex type");
2994 return QualType();
2995 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002996 QualType ElemType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002997 return Context->getComplexType(ElemType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002998 }
2999
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003000 case TYPE_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003001 if (Record.size() != 1) {
3002 Error("Incorrect encoding of pointer type");
3003 return QualType();
3004 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003005 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003006 return Context->getPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003007 }
3008
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003009 case TYPE_BLOCK_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003010 if (Record.size() != 1) {
3011 Error("Incorrect encoding of block pointer type");
3012 return QualType();
3013 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003014 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003015 return Context->getBlockPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003016 }
3017
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003018 case TYPE_LVALUE_REFERENCE: {
Richard Smithdf1550f2011-04-12 10:38:03 +00003019 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003020 Error("Incorrect encoding of lvalue reference type");
3021 return QualType();
3022 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003023 QualType PointeeType = GetType(Record[0]);
Richard Smithdf1550f2011-04-12 10:38:03 +00003024 return Context->getLValueReferenceType(PointeeType, Record[1]);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003025 }
3026
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003027 case TYPE_RVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003028 if (Record.size() != 1) {
3029 Error("Incorrect encoding of rvalue reference type");
3030 return QualType();
3031 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003032 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003033 return Context->getRValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003034 }
3035
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003036 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidis240437b2010-07-02 11:55:15 +00003037 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003038 Error("Incorrect encoding of member pointer type");
3039 return QualType();
3040 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003041 QualType PointeeType = GetType(Record[0]);
3042 QualType ClassType = GetType(Record[1]);
Douglas Gregor1ab55e92010-12-10 17:03:06 +00003043 if (PointeeType.isNull() || ClassType.isNull())
3044 return QualType();
3045
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003046 return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregor2cf26342009-04-09 22:27:44 +00003047 }
3048
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003049 case TYPE_CONSTANT_ARRAY: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003050 QualType ElementType = GetType(Record[0]);
3051 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3052 unsigned IndexTypeQuals = Record[2];
3053 unsigned Idx = 3;
3054 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003055 return Context->getConstantArrayType(ElementType, Size,
3056 ASM, IndexTypeQuals);
3057 }
3058
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003059 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003060 QualType ElementType = GetType(Record[0]);
3061 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3062 unsigned IndexTypeQuals = Record[2];
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003063 return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003064 }
3065
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003066 case TYPE_VARIABLE_ARRAY: {
Douglas Gregor0b748912009-04-14 21:18:50 +00003067 QualType ElementType = GetType(Record[0]);
3068 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3069 unsigned IndexTypeQuals = Record[2];
Sebastian Redlc3632732010-10-05 15:59:54 +00003070 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
3071 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
3072 return Context->getVariableArrayType(ElementType, ReadExpr(*Loc.F),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003073 ASM, IndexTypeQuals,
3074 SourceRange(LBLoc, RBLoc));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003075 }
3076
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003077 case TYPE_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00003078 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003079 Error("incorrect encoding of vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003080 return QualType();
3081 }
3082
3083 QualType ElementType = GetType(Record[0]);
3084 unsigned NumElements = Record[1];
Bob Wilsone86d78c2010-11-10 21:56:12 +00003085 unsigned VecKind = Record[2];
Chris Lattner788b0fd2010-06-23 06:00:24 +00003086 return Context->getVectorType(ElementType, NumElements,
Bob Wilsone86d78c2010-11-10 21:56:12 +00003087 (VectorType::VectorKind)VecKind);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003088 }
3089
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003090 case TYPE_EXT_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00003091 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003092 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003093 return QualType();
3094 }
3095
3096 QualType ElementType = GetType(Record[0]);
3097 unsigned NumElements = Record[1];
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003098 return Context->getExtVectorType(ElementType, NumElements);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003099 }
3100
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003101 case TYPE_FUNCTION_NO_PROTO: {
Eli Friedmana49218e2011-04-09 08:18:08 +00003102 if (Record.size() != 5) {
Douglas Gregora02b1472009-04-28 21:53:25 +00003103 Error("incorrect encoding of no-proto function type");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003104 return QualType();
3105 }
3106 QualType ResultType = GetType(Record[0]);
Eli Friedmana49218e2011-04-09 08:18:08 +00003107 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3], (CallingConv)Record[4]);
Rafael Espindola264ba482010-03-30 20:24:48 +00003108 return Context->getFunctionNoProtoType(ResultType, Info);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003109 }
3110
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003111 case TYPE_FUNCTION_PROTO: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003112 QualType ResultType = GetType(Record[0]);
John McCalle23cf432010-12-14 08:05:40 +00003113
3114 FunctionProtoType::ExtProtoInfo EPI;
3115 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
Eli Friedmana49218e2011-04-09 08:18:08 +00003116 /*hasregparm*/ Record[2],
3117 /*regparm*/ Record[3],
3118 static_cast<CallingConv>(Record[4]));
John McCalle23cf432010-12-14 08:05:40 +00003119
Eli Friedmana49218e2011-04-09 08:18:08 +00003120 unsigned Idx = 5;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003121 unsigned NumParams = Record[Idx++];
3122 llvm::SmallVector<QualType, 16> ParamTypes;
3123 for (unsigned I = 0; I != NumParams; ++I)
3124 ParamTypes.push_back(GetType(Record[Idx++]));
John McCalle23cf432010-12-14 08:05:40 +00003125
3126 EPI.Variadic = Record[Idx++];
3127 EPI.TypeQuals = Record[Idx++];
Douglas Gregorc938c162011-01-26 05:01:58 +00003128 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
Sebastian Redl60618fa2011-03-12 11:50:43 +00003129 ExceptionSpecificationType EST =
3130 static_cast<ExceptionSpecificationType>(Record[Idx++]);
3131 EPI.ExceptionSpecType = EST;
3132 if (EST == EST_Dynamic) {
3133 EPI.NumExceptions = Record[Idx++];
3134 llvm::SmallVector<QualType, 2> Exceptions;
3135 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
3136 Exceptions.push_back(GetType(Record[Idx++]));
3137 EPI.Exceptions = Exceptions.data();
3138 } else if (EST == EST_ComputedNoexcept) {
3139 EPI.NoexceptExpr = ReadExpr(*Loc.F);
3140 }
Jay Foadbeaaccd2009-05-21 09:52:38 +00003141 return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams,
John McCalle23cf432010-12-14 08:05:40 +00003142 EPI);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003143 }
3144
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003145 case TYPE_UNRESOLVED_USING:
John McCalled976492009-12-04 22:46:56 +00003146 return Context->getTypeDeclType(
3147 cast<UnresolvedUsingTypenameDecl>(GetDecl(Record[0])));
3148
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003149 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003150 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003151 Error("incorrect encoding of typedef type");
3152 return QualType();
3153 }
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003154 TypedefDecl *Decl = cast<TypedefDecl>(GetDecl(Record[0]));
3155 QualType Canonical = GetType(Record[1]);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00003156 if (!Canonical.isNull())
3157 Canonical = Context->getCanonicalType(Canonical);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003158 return Context->getTypedefType(Decl, Canonical);
3159 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003160
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003161 case TYPE_TYPEOF_EXPR:
Sebastian Redlc3632732010-10-05 15:59:54 +00003162 return Context->getTypeOfExprType(ReadExpr(*Loc.F));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003163
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003164 case TYPE_TYPEOF: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003165 if (Record.size() != 1) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003166 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003167 return QualType();
3168 }
3169 QualType UnderlyingType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003170 return Context->getTypeOfType(UnderlyingType);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003171 }
Mike Stump1eb44332009-09-09 15:08:12 +00003172
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003173 case TYPE_DECLTYPE:
Sebastian Redlc3632732010-10-05 15:59:54 +00003174 return Context->getDecltypeType(ReadExpr(*Loc.F));
Anders Carlsson395b4752009-06-24 19:06:50 +00003175
Richard Smith34b41d92011-02-20 03:19:35 +00003176 case TYPE_AUTO:
3177 return Context->getAutoType(GetType(Record[0]));
3178
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003179 case TYPE_RECORD: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003180 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003181 Error("incorrect encoding of record type");
3182 return QualType();
3183 }
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003184 bool IsDependent = Record[0];
3185 QualType T = Context->getRecordType(cast<RecordDecl>(GetDecl(Record[1])));
John McCallf4c73712011-01-19 06:33:43 +00003186 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003187 return T;
3188 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003189
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003190 case TYPE_ENUM: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003191 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003192 Error("incorrect encoding of enum type");
3193 return QualType();
3194 }
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003195 bool IsDependent = Record[0];
3196 QualType T = Context->getEnumType(cast<EnumDecl>(GetDecl(Record[1])));
John McCallf4c73712011-01-19 06:33:43 +00003197 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003198 return T;
3199 }
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003200
John McCall9d156a72011-01-06 01:58:22 +00003201 case TYPE_ATTRIBUTED: {
3202 if (Record.size() != 3) {
3203 Error("incorrect encoding of attributed type");
3204 return QualType();
3205 }
3206 QualType modifiedType = GetType(Record[0]);
3207 QualType equivalentType = GetType(Record[1]);
3208 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
3209 return Context->getAttributedType(kind, modifiedType, equivalentType);
3210 }
3211
Abramo Bagnara075f8f12010-12-10 16:29:40 +00003212 case TYPE_PAREN: {
3213 if (Record.size() != 1) {
3214 Error("incorrect encoding of paren type");
3215 return QualType();
3216 }
3217 QualType InnerType = GetType(Record[0]);
3218 return Context->getParenType(InnerType);
3219 }
3220
Douglas Gregor7536dd52010-12-20 02:24:11 +00003221 case TYPE_PACK_EXPANSION: {
Douglas Gregorf9997a02011-02-01 15:24:58 +00003222 if (Record.size() != 2) {
Douglas Gregor7536dd52010-12-20 02:24:11 +00003223 Error("incorrect encoding of pack expansion type");
3224 return QualType();
3225 }
3226 QualType Pattern = GetType(Record[0]);
3227 if (Pattern.isNull())
3228 return QualType();
Douglas Gregorcded4f62011-01-14 17:04:44 +00003229 llvm::Optional<unsigned> NumExpansions;
3230 if (Record[1])
3231 NumExpansions = Record[1] - 1;
3232 return Context->getPackExpansionType(Pattern, NumExpansions);
Douglas Gregor7536dd52010-12-20 02:24:11 +00003233 }
3234
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003235 case TYPE_ELABORATED: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00003236 unsigned Idx = 0;
3237 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3238 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3239 QualType NamedType = GetType(Record[Idx++]);
3240 return Context->getElaboratedType(Keyword, NNS, NamedType);
John McCall7da24312009-09-05 00:15:47 +00003241 }
3242
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003243 case TYPE_OBJC_INTERFACE: {
Chris Lattnerc6fa4452009-04-22 06:45:28 +00003244 unsigned Idx = 0;
3245 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
John McCallc12c5bb2010-05-15 11:32:37 +00003246 return Context->getObjCInterfaceType(ItfD);
3247 }
3248
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003249 case TYPE_OBJC_OBJECT: {
John McCallc12c5bb2010-05-15 11:32:37 +00003250 unsigned Idx = 0;
3251 QualType Base = GetType(Record[Idx++]);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00003252 unsigned NumProtos = Record[Idx++];
3253 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
3254 for (unsigned I = 0; I != NumProtos; ++I)
3255 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
Michael J. Spencer20249a12010-10-21 03:16:25 +00003256 return Context->getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00003257 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003258
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003259 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00003260 unsigned Idx = 0;
John McCallc12c5bb2010-05-15 11:32:37 +00003261 QualType Pointee = GetType(Record[Idx++]);
3262 return Context->getObjCObjectPointerType(Pointee);
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00003263 }
Argyrios Kyrtzidis24fab412009-09-29 19:42:55 +00003264
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003265 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCall49a832b2009-10-18 09:09:24 +00003266 unsigned Idx = 0;
3267 QualType Parm = GetType(Record[Idx++]);
3268 QualType Replacement = GetType(Record[Idx++]);
3269 return
3270 Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
3271 Replacement);
3272 }
John McCall3cb0ebd2010-03-10 03:28:59 +00003273
Douglas Gregorc3069d62011-01-14 02:55:32 +00003274 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
3275 unsigned Idx = 0;
3276 QualType Parm = GetType(Record[Idx++]);
3277 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
3278 return Context->getSubstTemplateTypeParmPackType(
3279 cast<TemplateTypeParmType>(Parm),
3280 ArgPack);
3281 }
3282
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003283 case TYPE_INJECTED_CLASS_NAME: {
John McCall3cb0ebd2010-03-10 03:28:59 +00003284 CXXRecordDecl *D = cast<CXXRecordDecl>(GetDecl(Record[0]));
3285 QualType TST = GetType(Record[1]); // probably derivable
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00003286 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003287 // for AST reading, too much interdependencies.
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00003288 return
3289 QualType(new (*Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCall3cb0ebd2010-03-10 03:28:59 +00003290 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003291
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003292 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003293 unsigned Idx = 0;
3294 unsigned Depth = Record[Idx++];
3295 unsigned Index = Record[Idx++];
3296 bool Pack = Record[Idx++];
3297 IdentifierInfo *Name = GetIdentifierInfo(Record, Idx);
3298 return Context->getTemplateTypeParmType(Depth, Index, Pack, Name);
3299 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003300
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003301 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00003302 unsigned Idx = 0;
3303 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3304 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3305 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +00003306 QualType Canon = GetType(Record[Idx++]);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00003307 if (!Canon.isNull())
3308 Canon = Context->getCanonicalType(Canon);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +00003309 return Context->getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00003310 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003311
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003312 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00003313 unsigned Idx = 0;
3314 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3315 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3316 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
3317 unsigned NumArgs = Record[Idx++];
3318 llvm::SmallVector<TemplateArgument, 8> Args;
3319 Args.reserve(NumArgs);
3320 while (NumArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00003321 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00003322 return Context->getDependentTemplateSpecializationType(Keyword, NNS, Name,
3323 Args.size(), Args.data());
3324 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003325
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003326 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00003327 unsigned Idx = 0;
3328
3329 // ArrayType
3330 QualType ElementType = GetType(Record[Idx++]);
3331 ArrayType::ArraySizeModifier ASM
3332 = (ArrayType::ArraySizeModifier)Record[Idx++];
3333 unsigned IndexTypeQuals = Record[Idx++];
3334
3335 // DependentSizedArrayType
Sebastian Redlc3632732010-10-05 15:59:54 +00003336 Expr *NumElts = ReadExpr(*Loc.F);
3337 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00003338
3339 return Context->getDependentSizedArrayType(ElementType, NumElts, ASM,
3340 IndexTypeQuals, Brackets);
3341 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003342
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003343 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003344 unsigned Idx = 0;
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003345 bool IsDependent = Record[Idx++];
Douglas Gregor1aee05d2011-01-15 06:45:20 +00003346 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003347 llvm::SmallVector<TemplateArgument, 8> Args;
Sebastian Redlc3632732010-10-05 15:59:54 +00003348 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003349 QualType Canon = GetType(Record[Idx++]);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003350 QualType T;
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003351 if (Canon.isNull())
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003352 T = Context->getCanonicalTemplateSpecializationType(Name, Args.data(),
3353 Args.size());
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003354 else
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003355 T = Context->getTemplateSpecializationType(Name, Args.data(),
3356 Args.size(), Canon);
John McCallf4c73712011-01-19 06:33:43 +00003357 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003358 return T;
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003359 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003360 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003361 // Suppress a GCC warning
3362 return QualType();
3363}
3364
Sebastian Redlc3632732010-10-05 15:59:54 +00003365class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003366 ASTReader &Reader;
Sebastian Redlc3632732010-10-05 15:59:54 +00003367 ASTReader::PerFileData &F;
Sebastian Redl577d4792010-07-22 22:43:28 +00003368 llvm::BitstreamCursor &DeclsCursor;
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003369 const ASTReader::RecordData &Record;
John McCalla1ee0c52009-10-16 21:56:05 +00003370 unsigned &Idx;
3371
Sebastian Redlc3632732010-10-05 15:59:54 +00003372 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
3373 unsigned &I) {
3374 return Reader.ReadSourceLocation(F, R, I);
3375 }
3376
John McCalla1ee0c52009-10-16 21:56:05 +00003377public:
Sebastian Redlc3632732010-10-05 15:59:54 +00003378 TypeLocReader(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003379 const ASTReader::RecordData &Record, unsigned &Idx)
Sebastian Redlc3632732010-10-05 15:59:54 +00003380 : Reader(Reader), F(F), DeclsCursor(F.DeclsCursor), Record(Record), Idx(Idx)
3381 { }
John McCalla1ee0c52009-10-16 21:56:05 +00003382
John McCall51bd8032009-10-18 01:05:36 +00003383 // We want compile-time assurance that we've enumerated all of
3384 // these, so unfortunately we have to declare them first, then
3385 // define them out-of-line.
3386#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +00003387#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +00003388 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00003389#include "clang/AST/TypeLocNodes.def"
3390
John McCall51bd8032009-10-18 01:05:36 +00003391 void VisitFunctionTypeLoc(FunctionTypeLoc);
3392 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00003393};
3394
John McCall51bd8032009-10-18 01:05:36 +00003395void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCalla1ee0c52009-10-16 21:56:05 +00003396 // nothing to do
3397}
John McCall51bd8032009-10-18 01:05:36 +00003398void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003399 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
Douglas Gregorddf889a2010-01-18 18:04:31 +00003400 if (TL.needsExtraLocalData()) {
3401 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
3402 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
3403 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
3404 TL.setModeAttr(Record[Idx++]);
3405 }
John McCalla1ee0c52009-10-16 21:56:05 +00003406}
John McCall51bd8032009-10-18 01:05:36 +00003407void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003408 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003409}
John McCall51bd8032009-10-18 01:05:36 +00003410void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003411 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003412}
John McCall51bd8032009-10-18 01:05:36 +00003413void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003414 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003415}
John McCall51bd8032009-10-18 01:05:36 +00003416void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003417 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003418}
John McCall51bd8032009-10-18 01:05:36 +00003419void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003420 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003421}
John McCall51bd8032009-10-18 01:05:36 +00003422void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003423 TL.setStarLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +00003424 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003425}
John McCall51bd8032009-10-18 01:05:36 +00003426void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003427 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
3428 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003429 if (Record[Idx++])
Sebastian Redlc3632732010-10-05 15:59:54 +00003430 TL.setSizeExpr(Reader.ReadExpr(F));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003431 else
John McCall51bd8032009-10-18 01:05:36 +00003432 TL.setSizeExpr(0);
3433}
3434void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
3435 VisitArrayTypeLoc(TL);
3436}
3437void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
3438 VisitArrayTypeLoc(TL);
3439}
3440void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
3441 VisitArrayTypeLoc(TL);
3442}
3443void TypeLocReader::VisitDependentSizedArrayTypeLoc(
3444 DependentSizedArrayTypeLoc TL) {
3445 VisitArrayTypeLoc(TL);
3446}
3447void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
3448 DependentSizedExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003449 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003450}
3451void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003452 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003453}
3454void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003455 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003456}
3457void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnara796aa442011-03-12 11:17:06 +00003458 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
3459 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
Douglas Gregordab60ad2010-10-01 18:44:50 +00003460 TL.setTrailingReturn(Record[Idx++]);
John McCall51bd8032009-10-18 01:05:36 +00003461 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
John McCall86acc2a2009-10-23 01:28:53 +00003462 TL.setArg(i, cast_or_null<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
John McCall51bd8032009-10-18 01:05:36 +00003463 }
3464}
3465void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
3466 VisitFunctionTypeLoc(TL);
3467}
3468void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
3469 VisitFunctionTypeLoc(TL);
3470}
John McCalled976492009-12-04 22:46:56 +00003471void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003472 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalled976492009-12-04 22:46:56 +00003473}
John McCall51bd8032009-10-18 01:05:36 +00003474void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003475 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003476}
3477void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003478 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3479 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3480 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003481}
3482void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003483 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3484 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3485 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3486 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003487}
3488void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003489 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003490}
Richard Smith34b41d92011-02-20 03:19:35 +00003491void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
3492 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3493}
John McCall51bd8032009-10-18 01:05:36 +00003494void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003495 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003496}
3497void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003498 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003499}
John McCall9d156a72011-01-06 01:58:22 +00003500void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
3501 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
3502 if (TL.hasAttrOperand()) {
3503 SourceRange range;
3504 range.setBegin(ReadSourceLocation(Record, Idx));
3505 range.setEnd(ReadSourceLocation(Record, Idx));
3506 TL.setAttrOperandParensRange(range);
3507 }
3508 if (TL.hasAttrExprOperand()) {
3509 if (Record[Idx++])
3510 TL.setAttrExprOperand(Reader.ReadExpr(F));
3511 else
3512 TL.setAttrExprOperand(0);
3513 } else if (TL.hasAttrEnumOperand())
3514 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
3515}
John McCall51bd8032009-10-18 01:05:36 +00003516void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003517 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003518}
John McCall49a832b2009-10-18 09:09:24 +00003519void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
3520 SubstTemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003521 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall49a832b2009-10-18 09:09:24 +00003522}
Douglas Gregorc3069d62011-01-14 02:55:32 +00003523void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
3524 SubstTemplateTypeParmPackTypeLoc TL) {
3525 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3526}
John McCall51bd8032009-10-18 01:05:36 +00003527void TypeLocReader::VisitTemplateSpecializationTypeLoc(
3528 TemplateSpecializationTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003529 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
3530 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3531 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall833ca992009-10-29 08:12:44 +00003532 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
3533 TL.setArgLocInfo(i,
Sebastian Redlc3632732010-10-05 15:59:54 +00003534 Reader.GetTemplateArgumentLocInfo(F,
3535 TL.getTypePtr()->getArg(i).getKind(),
3536 Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003537}
Abramo Bagnara075f8f12010-12-10 16:29:40 +00003538void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
3539 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3540 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3541}
Abramo Bagnara465d41b2010-05-11 21:36:43 +00003542void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003543 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor9e876872011-03-01 18:12:44 +00003544 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003545}
John McCall3cb0ebd2010-03-10 03:28:59 +00003546void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003547 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall3cb0ebd2010-03-10 03:28:59 +00003548}
Douglas Gregor4714c122010-03-31 17:34:00 +00003549void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003550 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor2494dd02011-03-01 01:34:45 +00003551 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00003552 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003553}
John McCall33500952010-06-11 00:33:02 +00003554void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
3555 DependentTemplateSpecializationTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003556 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor94fdffa2011-03-01 20:11:18 +00003557 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00003558 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3559 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3560 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall33500952010-06-11 00:33:02 +00003561 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
3562 TL.setArgLocInfo(I,
Sebastian Redlc3632732010-10-05 15:59:54 +00003563 Reader.GetTemplateArgumentLocInfo(F,
3564 TL.getTypePtr()->getArg(I).getKind(),
3565 Record, Idx));
John McCall33500952010-06-11 00:33:02 +00003566}
Douglas Gregor7536dd52010-12-20 02:24:11 +00003567void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
3568 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
3569}
John McCall51bd8032009-10-18 01:05:36 +00003570void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003571 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallc12c5bb2010-05-15 11:32:37 +00003572}
3573void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
3574 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00003575 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3576 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003577 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Sebastian Redlc3632732010-10-05 15:59:54 +00003578 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003579}
John McCall54e14c42009-10-22 22:37:11 +00003580void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003581 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall54e14c42009-10-22 22:37:11 +00003582}
John McCalla1ee0c52009-10-16 21:56:05 +00003583
Sebastian Redlc3632732010-10-05 15:59:54 +00003584TypeSourceInfo *ASTReader::GetTypeSourceInfo(PerFileData &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00003585 const RecordData &Record,
John McCalla1ee0c52009-10-16 21:56:05 +00003586 unsigned &Idx) {
3587 QualType InfoTy = GetType(Record[Idx++]);
3588 if (InfoTy.isNull())
3589 return 0;
3590
John McCalla93c9342009-12-07 02:54:59 +00003591 TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy);
Sebastian Redlc3632732010-10-05 15:59:54 +00003592 TypeLocReader TLR(*this, F, Record, Idx);
John McCalla93c9342009-12-07 02:54:59 +00003593 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCalla1ee0c52009-10-16 21:56:05 +00003594 TLR.Visit(TL);
John McCalla93c9342009-12-07 02:54:59 +00003595 return TInfo;
John McCalla1ee0c52009-10-16 21:56:05 +00003596}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003597
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003598QualType ASTReader::GetType(TypeID ID) {
John McCall0953e762009-09-24 19:53:00 +00003599 unsigned FastQuals = ID & Qualifiers::FastMask;
3600 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003601
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003602 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003603 QualType T;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003604 switch ((PredefinedTypeIDs)Index) {
3605 case PREDEF_TYPE_NULL_ID: return QualType();
3606 case PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break;
3607 case PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003608
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003609 case PREDEF_TYPE_CHAR_U_ID:
3610 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregor2cf26342009-04-09 22:27:44 +00003611 // FIXME: Check that the signedness of CharTy is correct!
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003612 T = Context->CharTy;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003613 break;
3614
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003615 case PREDEF_TYPE_UCHAR_ID: T = Context->UnsignedCharTy; break;
3616 case PREDEF_TYPE_USHORT_ID: T = Context->UnsignedShortTy; break;
3617 case PREDEF_TYPE_UINT_ID: T = Context->UnsignedIntTy; break;
3618 case PREDEF_TYPE_ULONG_ID: T = Context->UnsignedLongTy; break;
3619 case PREDEF_TYPE_ULONGLONG_ID: T = Context->UnsignedLongLongTy; break;
3620 case PREDEF_TYPE_UINT128_ID: T = Context->UnsignedInt128Ty; break;
3621 case PREDEF_TYPE_SCHAR_ID: T = Context->SignedCharTy; break;
3622 case PREDEF_TYPE_WCHAR_ID: T = Context->WCharTy; break;
3623 case PREDEF_TYPE_SHORT_ID: T = Context->ShortTy; break;
3624 case PREDEF_TYPE_INT_ID: T = Context->IntTy; break;
3625 case PREDEF_TYPE_LONG_ID: T = Context->LongTy; break;
3626 case PREDEF_TYPE_LONGLONG_ID: T = Context->LongLongTy; break;
3627 case PREDEF_TYPE_INT128_ID: T = Context->Int128Ty; break;
3628 case PREDEF_TYPE_FLOAT_ID: T = Context->FloatTy; break;
3629 case PREDEF_TYPE_DOUBLE_ID: T = Context->DoubleTy; break;
3630 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy; break;
3631 case PREDEF_TYPE_OVERLOAD_ID: T = Context->OverloadTy; break;
3632 case PREDEF_TYPE_DEPENDENT_ID: T = Context->DependentTy; break;
John McCall1de4d4e2011-04-07 08:22:57 +00003633 case PREDEF_TYPE_UNKNOWN_ANY: T = Context->UnknownAnyTy; break;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003634 case PREDEF_TYPE_NULLPTR_ID: T = Context->NullPtrTy; break;
3635 case PREDEF_TYPE_CHAR16_ID: T = Context->Char16Ty; break;
3636 case PREDEF_TYPE_CHAR32_ID: T = Context->Char32Ty; break;
3637 case PREDEF_TYPE_OBJC_ID: T = Context->ObjCBuiltinIdTy; break;
3638 case PREDEF_TYPE_OBJC_CLASS: T = Context->ObjCBuiltinClassTy; break;
3639 case PREDEF_TYPE_OBJC_SEL: T = Context->ObjCBuiltinSelTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003640 }
3641
3642 assert(!T.isNull() && "Unknown predefined type");
John McCall0953e762009-09-24 19:53:00 +00003643 return T.withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003644 }
3645
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003646 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003647 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl07a353c2010-07-14 20:26:45 +00003648 if (TypesLoaded[Index].isNull()) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003649 TypesLoaded[Index] = ReadTypeRecord(Index);
Douglas Gregor97475832010-10-05 18:37:06 +00003650 if (TypesLoaded[Index].isNull())
3651 return QualType();
3652
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003653 TypesLoaded[Index]->setFromAST();
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003654 TypeIdxs[TypesLoaded[Index]] = TypeIdx::fromTypeID(ID);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003655 if (DeserializationListener)
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003656 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1476ed42010-07-16 16:36:56 +00003657 TypesLoaded[Index]);
Sebastian Redl07a353c2010-07-14 20:26:45 +00003658 }
Mike Stump1eb44332009-09-09 15:08:12 +00003659
John McCall0953e762009-09-24 19:53:00 +00003660 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003661}
3662
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003663TypeID ASTReader::GetTypeID(QualType T) const {
3664 return MakeTypeID(T,
3665 std::bind1st(std::mem_fun(&ASTReader::GetTypeIdx), this));
3666}
3667
3668TypeIdx ASTReader::GetTypeIdx(QualType T) const {
3669 if (T.isNull())
3670 return TypeIdx();
3671 assert(!T.getLocalFastQualifiers());
3672
3673 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3674 // GetTypeIdx is mostly used for computing the hash of DeclarationNames and
3675 // comparing keys of ASTDeclContextNameLookupTable.
3676 // If the type didn't come from the AST file use a specially marked index
3677 // so that any hash/key comparison fail since no such index is stored
3678 // in a AST file.
3679 if (I == TypeIdxs.end())
3680 return TypeIdx(-1);
3681 return I->second;
3682}
3683
Douglas Gregor7c789c12010-10-29 22:39:52 +00003684unsigned ASTReader::getTotalNumCXXBaseSpecifiers() const {
3685 unsigned Result = 0;
3686 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
3687 Result += Chain[I]->LocalNumCXXBaseSpecifiers;
3688
3689 return Result;
3690}
3691
John McCall833ca992009-10-29 08:12:44 +00003692TemplateArgumentLocInfo
Sebastian Redlc3632732010-10-05 15:59:54 +00003693ASTReader::GetTemplateArgumentLocInfo(PerFileData &F,
3694 TemplateArgument::ArgKind Kind,
John McCall833ca992009-10-29 08:12:44 +00003695 const RecordData &Record,
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00003696 unsigned &Index) {
John McCall833ca992009-10-29 08:12:44 +00003697 switch (Kind) {
3698 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00003699 return ReadExpr(F);
John McCall833ca992009-10-29 08:12:44 +00003700 case TemplateArgument::Type:
Sebastian Redlc3632732010-10-05 15:59:54 +00003701 return GetTypeSourceInfo(F, Record, Index);
Douglas Gregor788cd062009-11-11 01:00:40 +00003702 case TemplateArgument::Template: {
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003703 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
3704 Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00003705 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003706 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregora7fc9012011-01-05 18:58:31 +00003707 SourceLocation());
3708 }
3709 case TemplateArgument::TemplateExpansion: {
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003710 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
3711 Index);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003712 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorba68eca2011-01-05 17:40:24 +00003713 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003714 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregorba68eca2011-01-05 17:40:24 +00003715 EllipsisLoc);
Douglas Gregor788cd062009-11-11 01:00:40 +00003716 }
John McCall833ca992009-10-29 08:12:44 +00003717 case TemplateArgument::Null:
3718 case TemplateArgument::Integral:
3719 case TemplateArgument::Declaration:
3720 case TemplateArgument::Pack:
3721 return TemplateArgumentLocInfo();
3722 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00003723 llvm_unreachable("unexpected template argument loc");
John McCall833ca992009-10-29 08:12:44 +00003724 return TemplateArgumentLocInfo();
3725}
3726
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003727TemplateArgumentLoc
Sebastian Redlc3632732010-10-05 15:59:54 +00003728ASTReader::ReadTemplateArgumentLoc(PerFileData &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00003729 const RecordData &Record, unsigned &Index) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003730 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003731
3732 if (Arg.getKind() == TemplateArgument::Expression) {
3733 if (Record[Index++]) // bool InfoHasSameExpr.
3734 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
3735 }
Sebastian Redlc3632732010-10-05 15:59:54 +00003736 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00003737 Record, Index));
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003738}
3739
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003740Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall76bd1f32010-06-01 09:23:16 +00003741 return GetDecl(ID);
3742}
3743
Douglas Gregor7c789c12010-10-29 22:39:52 +00003744uint64_t
3745ASTReader::GetCXXBaseSpecifiersOffset(serialization::CXXBaseSpecifiersID ID) {
3746 if (ID == 0)
3747 return 0;
3748
3749 --ID;
3750 uint64_t Offset = 0;
3751 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Anders Carlssonf25330b2011-03-09 05:09:32 +00003752 PerFileData &F = *Chain[N - I - 1];
3753
3754 if (ID < F.LocalNumCXXBaseSpecifiers)
3755 return Offset + F.CXXBaseSpecifiersOffsets[ID];
Douglas Gregor7c789c12010-10-29 22:39:52 +00003756
Anders Carlssonf25330b2011-03-09 05:09:32 +00003757 ID -= F.LocalNumCXXBaseSpecifiers;
3758 Offset += F.SizeInBits;
Douglas Gregor7c789c12010-10-29 22:39:52 +00003759 }
3760
3761 assert(false && "CXXBaseSpecifiers not found");
3762 return 0;
3763}
3764
3765CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
3766 // Figure out which AST file contains this offset.
3767 PerFileData *F = 0;
3768 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Anders Carlssonf25330b2011-03-09 05:09:32 +00003769 if (Offset < Chain[N - I - 1]->SizeInBits) {
3770 F = Chain[N - I - 1];
Douglas Gregor7c789c12010-10-29 22:39:52 +00003771 break;
3772 }
3773
Anders Carlssonf25330b2011-03-09 05:09:32 +00003774 Offset -= Chain[N - I - 1]->SizeInBits;
Douglas Gregor7c789c12010-10-29 22:39:52 +00003775 }
Anders Carlssonf25330b2011-03-09 05:09:32 +00003776
Douglas Gregor7c789c12010-10-29 22:39:52 +00003777 if (!F) {
3778 Error("Malformed AST file: C++ base specifiers at impossible offset");
3779 return 0;
3780 }
3781
3782 llvm::BitstreamCursor &Cursor = F->DeclsCursor;
3783 SavedStreamPosition SavedPosition(Cursor);
3784 Cursor.JumpToBit(Offset);
3785 ReadingKindTracker ReadingKind(Read_Decl, *this);
3786 RecordData Record;
3787 unsigned Code = Cursor.ReadCode();
3788 unsigned RecCode = Cursor.ReadRecord(Code, Record);
3789 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
3790 Error("Malformed AST file: missing C++ base specifiers");
3791 return 0;
3792 }
3793
3794 unsigned Idx = 0;
3795 unsigned NumBases = Record[Idx++];
3796 void *Mem = Context->Allocate(sizeof(CXXBaseSpecifier) * NumBases);
3797 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
3798 for (unsigned I = 0; I != NumBases; ++I)
3799 Bases[I] = ReadCXXBaseSpecifier(*F, Record, Idx);
3800 return Bases;
3801}
3802
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003803TranslationUnitDecl *ASTReader::GetTranslationUnitDecl() {
Sebastian Redl30c514c2010-07-14 23:45:08 +00003804 if (!DeclsLoaded[0]) {
Sebastian Redle1dde812010-08-24 00:50:04 +00003805 ReadDeclRecord(0, 1);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003806 if (DeserializationListener)
Sebastian Redl1476ed42010-07-16 16:36:56 +00003807 DeserializationListener->DeclRead(1, DeclsLoaded[0]);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003808 }
Argyrios Kyrtzidis8871a442010-07-08 17:13:02 +00003809
3810 return cast<TranslationUnitDecl>(DeclsLoaded[0]);
3811}
3812
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003813Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003814 if (ID == 0)
3815 return 0;
3816
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003817 if (ID > DeclsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003818 Error("declaration ID out-of-range for AST file");
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003819 return 0;
3820 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003821
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003822 unsigned Index = ID - 1;
Sebastian Redl30c514c2010-07-14 23:45:08 +00003823 if (!DeclsLoaded[Index]) {
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00003824 ReadDeclRecord(Index, ID);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003825 if (DeserializationListener)
3826 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
3827 }
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003828
3829 return DeclsLoaded[Index];
Douglas Gregor2cf26342009-04-09 22:27:44 +00003830}
3831
Chris Lattner887e2b32009-04-27 05:46:25 +00003832/// \brief Resolve the offset of a statement into a statement.
3833///
3834/// This operation will read a new statement from the external
3835/// source each time it is called, and is meant to be used via a
3836/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003837Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00003838 // Switch case IDs are per Decl.
3839 ClearSwitchCaseIDs();
3840
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003841 // Offset here is a global offset across the entire chain.
3842 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3843 PerFileData &F = *Chain[N - I - 1];
3844 if (Offset < F.SizeInBits) {
3845 // Since we know that this statement is part of a decl, make sure to use
3846 // the decl cursor to read it.
3847 F.DeclsCursor.JumpToBit(Offset);
Sebastian Redlc3632732010-10-05 15:59:54 +00003848 return ReadStmtFromStream(F);
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003849 }
3850 Offset -= F.SizeInBits;
3851 }
3852 llvm_unreachable("Broken chain");
Douglas Gregor250fc9c2009-04-18 00:07:54 +00003853}
3854
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003855bool ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00003856 bool (*isKindWeWant)(Decl::Kind),
John McCall76bd1f32010-06-01 09:23:16 +00003857 llvm::SmallVectorImpl<Decl*> &Decls) {
Mike Stump1eb44332009-09-09 15:08:12 +00003858 assert(DC->hasExternalLexicalStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00003859 "DeclContext has no lexical decls in storage");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003860
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003861 // There might be lexical decls in multiple parts of the chain, for the TU
3862 // at least.
Sebastian Redl4a9eb262010-09-28 02:24:44 +00003863 // DeclContextOffsets might reallocate as we load additional decls below,
3864 // so make a copy of the vector.
3865 DeclContextInfos Infos = DeclContextOffsets[DC];
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003866 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3867 I != E; ++I) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003868 // IDs can be 0 if this context doesn't contain declarations.
3869 if (!I->LexicalDecls)
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003870 continue;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003871
3872 // Load all of the declaration IDs
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00003873 for (const KindDeclIDPair *ID = I->LexicalDecls,
3874 *IDE = ID + I->NumLexicalDecls; ID != IDE; ++ID) {
3875 if (isKindWeWant && !isKindWeWant((Decl::Kind)ID->first))
3876 continue;
3877
3878 Decl *D = GetDecl(ID->second);
Sebastian Redl4a9eb262010-09-28 02:24:44 +00003879 assert(D && "Null decl in lexical decls");
3880 Decls.push_back(D);
3881 }
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003882 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003883
Douglas Gregor25123082009-04-22 22:34:57 +00003884 ++NumLexicalDeclContextsRead;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003885 return false;
3886}
3887
John McCall76bd1f32010-06-01 09:23:16 +00003888DeclContext::lookup_result
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003889ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +00003890 DeclarationName Name) {
Mike Stump1eb44332009-09-09 15:08:12 +00003891 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00003892 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003893 if (!Name)
3894 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
3895 DeclContext::lookup_iterator(0));
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003896
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003897 llvm::SmallVector<NamedDecl *, 64> Decls;
Sebastian Redl8b122732010-08-24 00:49:55 +00003898 // There might be visible decls in multiple parts of the chain, for the TU
Sebastian Redl5967d622010-08-24 00:50:16 +00003899 // and namespaces. For any given name, the last available results replace
3900 // all earlier ones. For this reason, we walk in reverse.
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003901 DeclContextInfos &Infos = DeclContextOffsets[DC];
Sebastian Redl5967d622010-08-24 00:50:16 +00003902 for (DeclContextInfos::reverse_iterator I = Infos.rbegin(), E = Infos.rend();
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003903 I != E; ++I) {
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003904 if (!I->NameLookupTableData)
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003905 continue;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003906
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003907 ASTDeclContextNameLookupTable *LookupTable =
3908 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3909 ASTDeclContextNameLookupTable::iterator Pos = LookupTable->find(Name);
3910 if (Pos == LookupTable->end())
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003911 continue;
3912
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003913 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
3914 for (; Data.first != Data.second; ++Data.first)
3915 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
Sebastian Redl5967d622010-08-24 00:50:16 +00003916 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003917 }
3918
Douglas Gregor25123082009-04-22 22:34:57 +00003919 ++NumVisibleDeclContextsRead;
John McCall76bd1f32010-06-01 09:23:16 +00003920
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003921 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall76bd1f32010-06-01 09:23:16 +00003922 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003923}
3924
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +00003925void ASTReader::MaterializeVisibleDecls(const DeclContext *DC) {
3926 assert(DC->hasExternalVisibleStorage() &&
3927 "DeclContext has no visible decls in storage");
3928
3929 llvm::SmallVector<NamedDecl *, 64> Decls;
3930 // There might be visible decls in multiple parts of the chain, for the TU
3931 // and namespaces.
3932 DeclContextInfos &Infos = DeclContextOffsets[DC];
3933 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3934 I != E; ++I) {
3935 if (!I->NameLookupTableData)
3936 continue;
3937
3938 ASTDeclContextNameLookupTable *LookupTable =
3939 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3940 for (ASTDeclContextNameLookupTable::item_iterator
3941 ItemI = LookupTable->item_begin(),
3942 ItemEnd = LookupTable->item_end() ; ItemI != ItemEnd; ++ItemI) {
3943 ASTDeclContextNameLookupTable::item_iterator::value_type Val
3944 = *ItemI;
3945 ASTDeclContextNameLookupTrait::data_type Data = Val.second;
3946 Decls.clear();
3947 for (; Data.first != Data.second; ++Data.first)
3948 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
3949 MaterializeVisibleDeclsForName(DC, Val.first, Decls);
3950 }
3951 }
3952}
3953
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003954void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003955 assert(Consumer);
3956 while (!InterestingDecls.empty()) {
3957 DeclGroupRef DG(InterestingDecls.front());
3958 InterestingDecls.pop_front();
Sebastian Redl27372b42010-08-11 18:52:41 +00003959 Consumer->HandleInterestingDecl(DG);
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003960 }
3961}
3962
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003963void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor0af2ca42009-04-22 19:09:20 +00003964 this->Consumer = Consumer;
3965
Douglas Gregorfdd01722009-04-14 00:24:19 +00003966 if (!Consumer)
3967 return;
3968
3969 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003970 // Force deserialization of this decl, which will cause it to be queued for
3971 // passing to the consumer.
Daniel Dunbar04a0b502009-09-17 03:06:44 +00003972 GetDecl(ExternalDefinitions[I]);
Douglas Gregorfdd01722009-04-14 00:24:19 +00003973 }
Douglas Gregorc62a2fe2009-04-25 00:41:30 +00003974
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003975 PassInterestingDeclsToConsumer();
Douglas Gregorfdd01722009-04-14 00:24:19 +00003976}
3977
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003978void ASTReader::PrintStats() {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003979 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregor2cf26342009-04-09 22:27:44 +00003980
Mike Stump1eb44332009-09-09 15:08:12 +00003981 unsigned NumTypesLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003982 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall0953e762009-09-24 19:53:00 +00003983 QualType());
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003984 unsigned NumDeclsLoaded
3985 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
3986 (Decl *)0);
3987 unsigned NumIdentifiersLoaded
3988 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
3989 IdentifiersLoaded.end(),
3990 (IdentifierInfo *)0);
Mike Stump1eb44332009-09-09 15:08:12 +00003991 unsigned NumSelectorsLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003992 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
3993 SelectorsLoaded.end(),
3994 Selector());
Douglas Gregor2d41cc12009-04-13 20:50:16 +00003995
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003996 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
3997 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00003998 if (TotalNumSLocEntries)
3999 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
4000 NumSLocEntriesRead, TotalNumSLocEntries,
4001 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004002 if (!TypesLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00004003 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004004 NumTypesLoaded, (unsigned)TypesLoaded.size(),
4005 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
4006 if (!DeclsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00004007 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004008 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
4009 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00004010 if (!IdentifiersLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00004011 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00004012 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
4013 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Sebastian Redl725cd962010-08-04 20:40:17 +00004014 if (!SelectorsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00004015 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redl725cd962010-08-04 20:40:17 +00004016 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
4017 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor83941df2009-04-25 17:48:32 +00004018 if (TotalNumStatements)
4019 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
4020 NumStatementsRead, TotalNumStatements,
4021 ((float)NumStatementsRead/TotalNumStatements * 100));
4022 if (TotalNumMacros)
4023 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
4024 NumMacrosRead, TotalNumMacros,
4025 ((float)NumMacrosRead/TotalNumMacros * 100));
4026 if (TotalLexicalDeclContexts)
4027 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
4028 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
4029 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
4030 * 100));
4031 if (TotalVisibleDeclContexts)
4032 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
4033 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
4034 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
4035 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00004036 if (TotalNumMethodPoolEntries) {
Douglas Gregor83941df2009-04-25 17:48:32 +00004037 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redlfa78dec2010-08-04 21:22:45 +00004038 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
4039 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor83941df2009-04-25 17:48:32 +00004040 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00004041 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor83941df2009-04-25 17:48:32 +00004042 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00004043 std::fprintf(stderr, "\n");
4044}
4045
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004046void ASTReader::InitializeSema(Sema &S) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00004047 SemaObj = &S;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00004048 S.ExternalSource = this;
4049
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00004050 // Makes sure any declarations that were deserialized "too early"
4051 // still get added to the identifier's declaration chains.
Douglas Gregor76dc8892010-09-24 23:29:12 +00004052 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
4053 if (SemaObj->TUScope)
John McCalld226f652010-08-21 09:40:31 +00004054 SemaObj->TUScope->AddDecl(PreloadedDecls[I]);
Douglas Gregor76dc8892010-09-24 23:29:12 +00004055
4056 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregor668c1a42009-04-21 22:25:48 +00004057 }
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00004058 PreloadedDecls.clear();
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00004059
4060 // If there were any tentative definitions, deserialize them and add
Sebastian Redle9d12b62010-01-31 22:27:38 +00004061 // them to Sema's list of tentative definitions.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00004062 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
4063 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
Sebastian Redle9d12b62010-01-31 22:27:38 +00004064 SemaObj->TentativeDefinitions.push_back(Var);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00004065 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00004066
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00004067 // If there were any unused file scoped decls, deserialize them and add to
4068 // Sema's list of unused file scoped decls.
4069 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
4070 DeclaratorDecl *D = cast<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
4071 SemaObj->UnusedFileScopedDecls.push_back(D);
Tanya Lattnere6bbc012010-02-12 00:07:30 +00004072 }
Douglas Gregor14c22f22009-04-22 22:18:58 +00004073
4074 // If there were any locally-scoped external declarations,
4075 // deserialize them and add them to Sema's table of locally-scoped
4076 // external declarations.
4077 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
4078 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
4079 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
4080 }
Douglas Gregorb81c1702009-04-27 20:06:05 +00004081
4082 // If there were any ext_vector type declarations, deserialize them
4083 // and add them to Sema's vector of such declarations.
4084 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I)
4085 SemaObj->ExtVectorDecls.push_back(
4086 cast<TypedefDecl>(GetDecl(ExtVectorDecls[I])));
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004087
4088 // FIXME: Do VTable uses and dynamic classes deserialize too much ?
4089 // Can we cut them down before writing them ?
4090
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004091 // If there were any dynamic classes declarations, deserialize them
4092 // and add them to Sema's vector of such declarations.
4093 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I)
4094 SemaObj->DynamicClasses.push_back(
4095 cast<CXXRecordDecl>(GetDecl(DynamicClasses[I])));
Fariborz Jahanian32019832010-07-23 19:11:11 +00004096
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00004097 // Load the offsets of the declarations that Sema references.
4098 // They will be lazily deserialized when needed.
4099 if (!SemaDeclRefs.empty()) {
4100 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
4101 SemaObj->StdNamespace = SemaDeclRefs[0];
4102 SemaObj->StdBadAlloc = SemaDeclRefs[1];
4103 }
4104
Sebastian Redlc3632732010-10-05 15:59:54 +00004105 for (PerFileData *F = FirstInSource; F; F = F->NextInSource) {
4106
4107 // If there are @selector references added them to its pool. This is for
4108 // implementation of -Wselector.
4109 if (!F->ReferencedSelectorsData.empty()) {
4110 unsigned int DataSize = F->ReferencedSelectorsData.size()-1;
4111 unsigned I = 0;
4112 while (I < DataSize) {
4113 Selector Sel = DecodeSelector(F->ReferencedSelectorsData[I++]);
4114 SourceLocation SelLoc = ReadSourceLocation(
4115 *F, F->ReferencedSelectorsData, I);
4116 SemaObj->ReferencedSelectors.insert(std::make_pair(Sel, SelLoc));
4117 }
4118 }
4119
4120 // If there were any pending implicit instantiations, deserialize them
4121 // and add them to Sema's queue of such instantiations.
4122 assert(F->PendingInstantiations.size() % 2 == 0 &&
4123 "Expected pairs of entries");
4124 for (unsigned Idx = 0, N = F->PendingInstantiations.size(); Idx < N;) {
4125 ValueDecl *D=cast<ValueDecl>(GetDecl(F->PendingInstantiations[Idx++]));
4126 SourceLocation Loc = ReadSourceLocation(*F, F->PendingInstantiations,Idx);
4127 SemaObj->PendingInstantiations.push_back(std::make_pair(D, Loc));
4128 }
4129 }
4130
4131 // The two special data sets below always come from the most recent PCH,
4132 // which is at the front of the chain.
4133 PerFileData &F = *Chain.front();
4134
4135 // If there were any weak undeclared identifiers, deserialize them and add to
4136 // Sema's list of weak undeclared identifiers.
4137 if (!WeakUndeclaredIdentifiers.empty()) {
4138 unsigned Idx = 0;
4139 for (unsigned I = 0, N = WeakUndeclaredIdentifiers[Idx++]; I != N; ++I) {
4140 IdentifierInfo *WeakId = GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
4141 IdentifierInfo *AliasId= GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
4142 SourceLocation Loc = ReadSourceLocation(F, WeakUndeclaredIdentifiers,Idx);
4143 bool Used = WeakUndeclaredIdentifiers[Idx++];
4144 Sema::WeakInfo WI(AliasId, Loc);
4145 WI.setUsed(Used);
4146 SemaObj->WeakUndeclaredIdentifiers.insert(std::make_pair(WeakId, WI));
4147 }
4148 }
4149
4150 // If there were any VTable uses, deserialize the information and add it
4151 // to Sema's vector and map of VTable uses.
4152 if (!VTableUses.empty()) {
4153 unsigned Idx = 0;
4154 for (unsigned I = 0, N = VTableUses[Idx++]; I != N; ++I) {
4155 CXXRecordDecl *Class = cast<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
4156 SourceLocation Loc = ReadSourceLocation(F, VTableUses, Idx);
4157 bool DefinitionRequired = VTableUses[Idx++];
4158 SemaObj->VTableUses.push_back(std::make_pair(Class, Loc));
4159 SemaObj->VTablesUsed[Class] = DefinitionRequired;
Fariborz Jahanian32019832010-07-23 19:11:11 +00004160 }
4161 }
Peter Collingbourne84bccea2011-02-15 19:46:30 +00004162
4163 if (!FPPragmaOptions.empty()) {
4164 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
4165 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
4166 }
4167
4168 if (!OpenCLExtensions.empty()) {
4169 unsigned I = 0;
4170#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
4171#include "clang/Basic/OpenCLExtensions.def"
4172
4173 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
4174 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00004175}
4176
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004177IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Sebastian Redld8c5abb2010-08-02 18:30:12 +00004178 // Try to find this name within our on-disk hash tables. We start with the
4179 // most recent one, since that one contains the most up-to-date info.
Sebastian Redld27d3fc2010-07-21 22:31:37 +00004180 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004181 ASTIdentifierLookupTable *IdTable
4182 = (ASTIdentifierLookupTable *)Chain[I]->IdentifierLookupTable;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00004183 if (!IdTable)
4184 continue;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00004185 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004186 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00004187 if (Pos == IdTable->end())
4188 continue;
Douglas Gregor668c1a42009-04-21 22:25:48 +00004189
Sebastian Redld27d3fc2010-07-21 22:31:37 +00004190 // Dereferencing the iterator has the effect of building the
4191 // IdentifierInfo node and populating it with the various
4192 // declarations it needs.
Sebastian Redld8c5abb2010-08-02 18:30:12 +00004193 return *Pos;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00004194 }
Sebastian Redld8c5abb2010-08-02 18:30:12 +00004195 return 0;
Douglas Gregor668c1a42009-04-21 22:25:48 +00004196}
4197
Douglas Gregor95f42922010-10-14 22:11:03 +00004198namespace clang {
4199 /// \brief An identifier-lookup iterator that enumerates all of the
4200 /// identifiers stored within a set of AST files.
4201 class ASTIdentifierIterator : public IdentifierIterator {
4202 /// \brief The AST reader whose identifiers are being enumerated.
4203 const ASTReader &Reader;
4204
4205 /// \brief The current index into the chain of AST files stored in
4206 /// the AST reader.
4207 unsigned Index;
4208
4209 /// \brief The current position within the identifier lookup table
4210 /// of the current AST file.
4211 ASTIdentifierLookupTable::key_iterator Current;
4212
4213 /// \brief The end position within the identifier lookup table of
4214 /// the current AST file.
4215 ASTIdentifierLookupTable::key_iterator End;
4216
4217 public:
4218 explicit ASTIdentifierIterator(const ASTReader &Reader);
4219
4220 virtual llvm::StringRef Next();
4221 };
4222}
4223
4224ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
4225 : Reader(Reader), Index(Reader.Chain.size() - 1) {
4226 ASTIdentifierLookupTable *IdTable
4227 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
4228 Current = IdTable->key_begin();
4229 End = IdTable->key_end();
4230}
4231
4232llvm::StringRef ASTIdentifierIterator::Next() {
4233 while (Current == End) {
4234 // If we have exhausted all of our AST files, we're done.
4235 if (Index == 0)
4236 return llvm::StringRef();
4237
4238 --Index;
4239 ASTIdentifierLookupTable *IdTable
4240 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
4241 Current = IdTable->key_begin();
4242 End = IdTable->key_end();
4243 }
4244
4245 // We have any identifiers remaining in the current AST file; return
4246 // the next one.
4247 std::pair<const char*, unsigned> Key = *Current;
4248 ++Current;
4249 return llvm::StringRef(Key.first, Key.second);
4250}
4251
4252IdentifierIterator *ASTReader::getIdentifiers() const {
4253 return new ASTIdentifierIterator(*this);
4254}
4255
Mike Stump1eb44332009-09-09 15:08:12 +00004256std::pair<ObjCMethodList, ObjCMethodList>
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004257ASTReader::ReadMethodPool(Selector Sel) {
Sebastian Redl725cd962010-08-04 20:40:17 +00004258 // Find this selector in a hash table. We want to find the most recent entry.
4259 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4260 PerFileData &F = *Chain[I];
4261 if (!F.SelectorLookupTable)
4262 continue;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00004263
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004264 ASTSelectorLookupTable *PoolTable
4265 = (ASTSelectorLookupTable*)F.SelectorLookupTable;
4266 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
Sebastian Redl725cd962010-08-04 20:40:17 +00004267 if (Pos != PoolTable->end()) {
4268 ++NumSelectorsRead;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00004269 // FIXME: Not quite happy with the statistics here. We probably should
4270 // disable this tracking when called via LoadSelector.
4271 // Also, should entries without methods count as misses?
4272 ++NumMethodPoolEntriesRead;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004273 ASTSelectorLookupTrait::data_type Data = *Pos;
Sebastian Redl725cd962010-08-04 20:40:17 +00004274 if (DeserializationListener)
4275 DeserializationListener->SelectorRead(Data.ID, Sel);
4276 return std::make_pair(Data.Instance, Data.Factory);
4277 }
Douglas Gregor83941df2009-04-25 17:48:32 +00004278 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00004279
Sebastian Redlfa78dec2010-08-04 21:22:45 +00004280 ++NumMethodPoolMisses;
Sebastian Redl725cd962010-08-04 20:40:17 +00004281 return std::pair<ObjCMethodList, ObjCMethodList>();
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00004282}
4283
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004284void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redle58aa892010-08-04 18:21:41 +00004285 // It would be complicated to avoid reading the methods anyway. So don't.
4286 ReadMethodPool(Sel);
4287}
4288
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004289void ASTReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00004290 assert(ID && "Non-zero identifier ID required");
Douglas Gregora02b1472009-04-28 21:53:25 +00004291 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00004292 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004293 if (DeserializationListener)
4294 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregor668c1a42009-04-21 22:25:48 +00004295}
4296
Douglas Gregord89275b2009-07-06 18:54:52 +00004297/// \brief Set the globally-visible declarations associated with the given
4298/// identifier.
4299///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004300/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump1eb44332009-09-09 15:08:12 +00004301/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregord89275b2009-07-06 18:54:52 +00004302/// them.
4303///
4304/// \param II an IdentifierInfo that refers to one or more globally-visible
4305/// declarations.
4306///
4307/// \param DeclIDs the set of declaration IDs with the name @p II that are
4308/// visible at global scope.
4309///
4310/// \param Nonrecursive should be true to indicate that the caller knows that
4311/// this call is non-recursive, and therefore the globally-visible declarations
4312/// will not be placed onto the pending queue.
Mike Stump1eb44332009-09-09 15:08:12 +00004313void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004314ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Douglas Gregord89275b2009-07-06 18:54:52 +00004315 const llvm::SmallVectorImpl<uint32_t> &DeclIDs,
4316 bool Nonrecursive) {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004317 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregord89275b2009-07-06 18:54:52 +00004318 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
4319 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
4320 PII.II = II;
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00004321 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregord89275b2009-07-06 18:54:52 +00004322 return;
4323 }
Mike Stump1eb44332009-09-09 15:08:12 +00004324
Douglas Gregord89275b2009-07-06 18:54:52 +00004325 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
4326 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
4327 if (SemaObj) {
Douglas Gregor914ed9d2010-08-13 03:15:25 +00004328 if (SemaObj->TUScope) {
4329 // Introduce this declaration into the translation-unit scope
4330 // and add it to the declaration chain for this identifier, so
4331 // that (unqualified) name lookup will find it.
John McCalld226f652010-08-21 09:40:31 +00004332 SemaObj->TUScope->AddDecl(D);
Douglas Gregor914ed9d2010-08-13 03:15:25 +00004333 }
Douglas Gregor76dc8892010-09-24 23:29:12 +00004334 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
Douglas Gregord89275b2009-07-06 18:54:52 +00004335 } else {
4336 // Queue this declaration so that it will be added to the
4337 // translation unit scope and identifier's declaration chain
4338 // once a Sema object is known.
4339 PreloadedDecls.push_back(D);
4340 }
4341 }
4342}
4343
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004344IdentifierInfo *ASTReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00004345 if (ID == 0)
4346 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00004347
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00004348 if (IdentifiersLoaded.empty()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004349 Error("no identifier table in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00004350 return 0;
4351 }
Mike Stump1eb44332009-09-09 15:08:12 +00004352
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00004353 assert(PP && "Forgot to set Preprocessor ?");
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00004354 ID -= 1;
4355 if (!IdentifiersLoaded[ID]) {
4356 unsigned Index = ID;
4357 const char *Str = 0;
4358 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4359 PerFileData *F = Chain[N - I - 1];
4360 if (Index < F->LocalNumIdentifiers) {
4361 uint32_t Offset = F->IdentifierOffsets[Index];
4362 Str = F->IdentifierTableData + Offset;
4363 break;
4364 }
4365 Index -= F->LocalNumIdentifiers;
4366 }
4367 assert(Str && "Broken Chain");
Douglas Gregord6595a42009-04-25 21:04:17 +00004368
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004369 // All of the strings in the AST file are preceded by a 16-bit length.
4370 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenek231bc0b2009-10-23 04:45:31 +00004371 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
4372 // unsigned integers. This is important to avoid integer overflow when
4373 // we cast them to 'unsigned'.
Ted Kremenekff1ea462009-10-23 03:57:22 +00004374 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregor02fc7512009-04-28 20:01:51 +00004375 unsigned StrLen = (((unsigned) StrLenPtr[0])
4376 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00004377 IdentifiersLoaded[ID]
Kovarththanan Rajaratnam811f4262010-03-12 10:32:27 +00004378 = &PP->getIdentifierTable().get(Str, StrLen);
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004379 if (DeserializationListener)
4380 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregorafaf3082009-04-11 00:14:32 +00004381 }
Mike Stump1eb44332009-09-09 15:08:12 +00004382
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00004383 return IdentifiersLoaded[ID];
Douglas Gregor2cf26342009-04-09 22:27:44 +00004384}
4385
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004386void ASTReader::ReadSLocEntry(unsigned ID) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00004387 ReadSLocEntryRecord(ID);
4388}
4389
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004390Selector ASTReader::DecodeSelector(unsigned ID) {
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004391 if (ID == 0)
4392 return Selector();
Mike Stump1eb44332009-09-09 15:08:12 +00004393
Sebastian Redl725cd962010-08-04 20:40:17 +00004394 if (ID > SelectorsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004395 Error("selector ID out of range in AST file");
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004396 return Selector();
4397 }
Douglas Gregor83941df2009-04-25 17:48:32 +00004398
Sebastian Redl725cd962010-08-04 20:40:17 +00004399 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor83941df2009-04-25 17:48:32 +00004400 // Load this selector from the selector table.
Sebastian Redl725cd962010-08-04 20:40:17 +00004401 unsigned Idx = ID - 1;
4402 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4403 PerFileData &F = *Chain[N - I - 1];
4404 if (Idx < F.LocalNumSelectors) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004405 ASTSelectorLookupTrait Trait(*this);
Sebastian Redl725cd962010-08-04 20:40:17 +00004406 SelectorsLoaded[ID - 1] =
4407 Trait.ReadKey(F.SelectorLookupTableData + F.SelectorOffsets[Idx], 0);
4408 if (DeserializationListener)
4409 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
4410 break;
4411 }
4412 Idx -= F.LocalNumSelectors;
4413 }
Douglas Gregor83941df2009-04-25 17:48:32 +00004414 }
4415
Sebastian Redl725cd962010-08-04 20:40:17 +00004416 return SelectorsLoaded[ID - 1];
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004417}
4418
Michael J. Spencer20249a12010-10-21 03:16:25 +00004419Selector ASTReader::GetExternalSelector(uint32_t ID) {
Douglas Gregor719770d2010-04-06 17:30:22 +00004420 return DecodeSelector(ID);
4421}
4422
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004423uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redl725cd962010-08-04 20:40:17 +00004424 // ID 0 (the null selector) is considered an external selector.
4425 return getTotalNumSelectors() + 1;
Douglas Gregor719770d2010-04-06 17:30:22 +00004426}
4427
Mike Stump1eb44332009-09-09 15:08:12 +00004428DeclarationName
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004429ASTReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00004430 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
4431 switch (Kind) {
4432 case DeclarationName::Identifier:
4433 return DeclarationName(GetIdentifierInfo(Record, Idx));
4434
4435 case DeclarationName::ObjCZeroArgSelector:
4436 case DeclarationName::ObjCOneArgSelector:
4437 case DeclarationName::ObjCMultiArgSelector:
Steve Naroffa7503a72009-04-23 15:15:40 +00004438 return DeclarationName(GetSelector(Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004439
4440 case DeclarationName::CXXConstructorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00004441 return Context->DeclarationNames.getCXXConstructorName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00004442 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004443
4444 case DeclarationName::CXXDestructorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00004445 return Context->DeclarationNames.getCXXDestructorName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00004446 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004447
4448 case DeclarationName::CXXConversionFunctionName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00004449 return Context->DeclarationNames.getCXXConversionFunctionName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00004450 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004451
4452 case DeclarationName::CXXOperatorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00004453 return Context->DeclarationNames.getCXXOperatorName(
Douglas Gregor2cf26342009-04-09 22:27:44 +00004454 (OverloadedOperatorKind)Record[Idx++]);
4455
Sean Hunt3e518bd2009-11-29 07:34:05 +00004456 case DeclarationName::CXXLiteralOperatorName:
4457 return Context->DeclarationNames.getCXXLiteralOperatorName(
4458 GetIdentifierInfo(Record, Idx));
4459
Douglas Gregor2cf26342009-04-09 22:27:44 +00004460 case DeclarationName::CXXUsingDirective:
4461 return DeclarationName::getUsingDirectiveName();
4462 }
4463
4464 // Required to silence GCC warning
4465 return DeclarationName();
4466}
Douglas Gregor0a0428e2009-04-10 20:39:37 +00004467
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004468void ASTReader::ReadDeclarationNameLoc(PerFileData &F,
4469 DeclarationNameLoc &DNLoc,
4470 DeclarationName Name,
4471 const RecordData &Record, unsigned &Idx) {
4472 switch (Name.getNameKind()) {
4473 case DeclarationName::CXXConstructorName:
4474 case DeclarationName::CXXDestructorName:
4475 case DeclarationName::CXXConversionFunctionName:
4476 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
4477 break;
4478
4479 case DeclarationName::CXXOperatorName:
4480 DNLoc.CXXOperatorName.BeginOpNameLoc
4481 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4482 DNLoc.CXXOperatorName.EndOpNameLoc
4483 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4484 break;
4485
4486 case DeclarationName::CXXLiteralOperatorName:
4487 DNLoc.CXXLiteralOperatorName.OpNameLoc
4488 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4489 break;
4490
4491 case DeclarationName::Identifier:
4492 case DeclarationName::ObjCZeroArgSelector:
4493 case DeclarationName::ObjCOneArgSelector:
4494 case DeclarationName::ObjCMultiArgSelector:
4495 case DeclarationName::CXXUsingDirective:
4496 break;
4497 }
4498}
4499
4500void ASTReader::ReadDeclarationNameInfo(PerFileData &F,
4501 DeclarationNameInfo &NameInfo,
4502 const RecordData &Record, unsigned &Idx) {
4503 NameInfo.setName(ReadDeclarationName(Record, Idx));
4504 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
4505 DeclarationNameLoc DNLoc;
4506 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
4507 NameInfo.setInfo(DNLoc);
4508}
4509
4510void ASTReader::ReadQualifierInfo(PerFileData &F, QualifierInfo &Info,
4511 const RecordData &Record, unsigned &Idx) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004512 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004513 unsigned NumTPLists = Record[Idx++];
4514 Info.NumTemplParamLists = NumTPLists;
4515 if (NumTPLists) {
4516 Info.TemplParamLists = new (*Context) TemplateParameterList*[NumTPLists];
4517 for (unsigned i=0; i != NumTPLists; ++i)
4518 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
4519 }
4520}
4521
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004522TemplateName
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004523ASTReader::ReadTemplateName(PerFileData &F, const RecordData &Record,
4524 unsigned &Idx) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00004525 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004526 switch (Kind) {
4527 case TemplateName::Template:
4528 return TemplateName(cast_or_null<TemplateDecl>(GetDecl(Record[Idx++])));
4529
4530 case TemplateName::OverloadedTemplate: {
4531 unsigned size = Record[Idx++];
4532 UnresolvedSet<8> Decls;
4533 while (size--)
4534 Decls.addDecl(cast<NamedDecl>(GetDecl(Record[Idx++])));
4535
4536 return Context->getOverloadedTemplateName(Decls.begin(), Decls.end());
4537 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004538
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004539 case TemplateName::QualifiedTemplate: {
4540 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
4541 bool hasTemplKeyword = Record[Idx++];
4542 TemplateDecl *Template = cast<TemplateDecl>(GetDecl(Record[Idx++]));
4543 return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
4544 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004545
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004546 case TemplateName::DependentTemplate: {
4547 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
4548 if (Record[Idx++]) // isIdentifier
4549 return Context->getDependentTemplateName(NNS,
4550 GetIdentifierInfo(Record, Idx));
4551 return Context->getDependentTemplateName(NNS,
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004552 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004553 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004554
4555 case TemplateName::SubstTemplateTemplateParmPack: {
4556 TemplateTemplateParmDecl *Param
4557 = cast_or_null<TemplateTemplateParmDecl>(GetDecl(Record[Idx++]));
4558 if (!Param)
4559 return TemplateName();
4560
4561 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
4562 if (ArgPack.getKind() != TemplateArgument::Pack)
4563 return TemplateName();
4564
4565 return Context->getSubstTemplateTemplateParmPack(Param, ArgPack);
4566 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004567 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004568
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004569 assert(0 && "Unhandled template name kind!");
4570 return TemplateName();
4571}
4572
4573TemplateArgument
Sebastian Redlc3632732010-10-05 15:59:54 +00004574ASTReader::ReadTemplateArgument(PerFileData &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00004575 const RecordData &Record, unsigned &Idx) {
Douglas Gregora7fc9012011-01-05 18:58:31 +00004576 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
4577 switch (Kind) {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004578 case TemplateArgument::Null:
4579 return TemplateArgument();
4580 case TemplateArgument::Type:
4581 return TemplateArgument(GetType(Record[Idx++]));
4582 case TemplateArgument::Declaration:
4583 return TemplateArgument(GetDecl(Record[Idx++]));
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00004584 case TemplateArgument::Integral: {
4585 llvm::APSInt Value = ReadAPSInt(Record, Idx);
4586 QualType T = GetType(Record[Idx++]);
4587 return TemplateArgument(Value, T);
4588 }
Douglas Gregora7fc9012011-01-05 18:58:31 +00004589 case TemplateArgument::Template:
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004590 return TemplateArgument(ReadTemplateName(F, Record, Idx));
Douglas Gregora7fc9012011-01-05 18:58:31 +00004591 case TemplateArgument::TemplateExpansion: {
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004592 TemplateName Name = ReadTemplateName(F, Record, Idx);
Douglas Gregor2be29f42011-01-14 23:41:42 +00004593 llvm::Optional<unsigned> NumTemplateExpansions;
4594 if (unsigned NumExpansions = Record[Idx++])
4595 NumTemplateExpansions = NumExpansions - 1;
4596 return TemplateArgument(Name, NumTemplateExpansions);
Douglas Gregorba68eca2011-01-05 17:40:24 +00004597 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004598 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00004599 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004600 case TemplateArgument::Pack: {
4601 unsigned NumArgs = Record[Idx++];
Douglas Gregor910f8002010-11-07 23:05:16 +00004602 TemplateArgument *Args = new (*Context) TemplateArgument[NumArgs];
4603 for (unsigned I = 0; I != NumArgs; ++I)
4604 Args[I] = ReadTemplateArgument(F, Record, Idx);
4605 return TemplateArgument(Args, NumArgs);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004606 }
4607 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004608
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004609 assert(0 && "Unhandled template argument kind!");
4610 return TemplateArgument();
4611}
4612
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004613TemplateParameterList *
Sebastian Redlc3632732010-10-05 15:59:54 +00004614ASTReader::ReadTemplateParameterList(PerFileData &F,
4615 const RecordData &Record, unsigned &Idx) {
4616 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
4617 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
4618 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004619
4620 unsigned NumParams = Record[Idx++];
4621 llvm::SmallVector<NamedDecl *, 16> Params;
4622 Params.reserve(NumParams);
4623 while (NumParams--)
4624 Params.push_back(cast<NamedDecl>(GetDecl(Record[Idx++])));
Michael J. Spencer20249a12010-10-21 03:16:25 +00004625
4626 TemplateParameterList* TemplateParams =
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004627 TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc,
4628 Params.data(), Params.size(), RAngleLoc);
4629 return TemplateParams;
4630}
4631
4632void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004633ASTReader::
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004634ReadTemplateArgumentList(llvm::SmallVector<TemplateArgument, 8> &TemplArgs,
Sebastian Redlc3632732010-10-05 15:59:54 +00004635 PerFileData &F, const RecordData &Record,
4636 unsigned &Idx) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004637 unsigned NumTemplateArgs = Record[Idx++];
4638 TemplArgs.reserve(NumTemplateArgs);
4639 while (NumTemplateArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00004640 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004641}
4642
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004643/// \brief Read a UnresolvedSet structure.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004644void ASTReader::ReadUnresolvedSet(UnresolvedSetImpl &Set,
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004645 const RecordData &Record, unsigned &Idx) {
4646 unsigned NumDecls = Record[Idx++];
4647 while (NumDecls--) {
4648 NamedDecl *D = cast<NamedDecl>(GetDecl(Record[Idx++]));
4649 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
4650 Set.addDecl(D, AS);
4651 }
4652}
4653
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004654CXXBaseSpecifier
Sebastian Redlc3632732010-10-05 15:59:54 +00004655ASTReader::ReadCXXBaseSpecifier(PerFileData &F,
Nick Lewycky56062202010-07-26 16:56:01 +00004656 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004657 bool isVirtual = static_cast<bool>(Record[Idx++]);
4658 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
4659 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redlf677ea32011-02-05 19:23:19 +00004660 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00004661 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
4662 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00004663 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redlf677ea32011-02-05 19:23:19 +00004664 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
Douglas Gregorf90b27a2011-01-03 22:36:02 +00004665 EllipsisLoc);
Sebastian Redlf677ea32011-02-05 19:23:19 +00004666 Result.setInheritConstructors(inheritConstructors);
4667 return Result;
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004668}
4669
Sean Huntcbb67482011-01-08 20:30:50 +00004670std::pair<CXXCtorInitializer **, unsigned>
4671ASTReader::ReadCXXCtorInitializers(PerFileData &F, const RecordData &Record,
4672 unsigned &Idx) {
4673 CXXCtorInitializer **CtorInitializers = 0;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004674 unsigned NumInitializers = Record[Idx++];
4675 if (NumInitializers) {
4676 ASTContext &C = *getContext();
4677
Sean Huntcbb67482011-01-08 20:30:50 +00004678 CtorInitializers
4679 = new (C) CXXCtorInitializer*[NumInitializers];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004680 for (unsigned i=0; i != NumInitializers; ++i) {
4681 TypeSourceInfo *BaseClassInfo = 0;
4682 bool IsBaseVirtual = false;
4683 FieldDecl *Member = 0;
Francois Pichet00eb3f92010-12-04 09:14:42 +00004684 IndirectFieldDecl *IndirectMember = 0;
Michael J. Spencer20249a12010-10-21 03:16:25 +00004685
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004686 bool IsBaseInitializer = Record[Idx++];
4687 if (IsBaseInitializer) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004688 BaseClassInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004689 IsBaseVirtual = Record[Idx++];
4690 } else {
Francois Pichet00eb3f92010-12-04 09:14:42 +00004691 bool IsIndirectMemberInitializer = Record[Idx++];
4692 if (IsIndirectMemberInitializer)
4693 IndirectMember = cast<IndirectFieldDecl>(GetDecl(Record[Idx++]));
4694 else
4695 Member = cast<FieldDecl>(GetDecl(Record[Idx++]));
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004696 }
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00004697 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redlc3632732010-10-05 15:59:54 +00004698 Expr *Init = ReadExpr(F);
Sebastian Redlc3632732010-10-05 15:59:54 +00004699 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
4700 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004701 bool IsWritten = Record[Idx++];
4702 unsigned SourceOrderOrNumArrayIndices;
4703 llvm::SmallVector<VarDecl *, 8> Indices;
4704 if (IsWritten) {
4705 SourceOrderOrNumArrayIndices = Record[Idx++];
4706 } else {
4707 SourceOrderOrNumArrayIndices = Record[Idx++];
4708 Indices.reserve(SourceOrderOrNumArrayIndices);
4709 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
4710 Indices.push_back(cast<VarDecl>(GetDecl(Record[Idx++])));
4711 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004712
Sean Huntcbb67482011-01-08 20:30:50 +00004713 CXXCtorInitializer *BOMInit;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004714 if (IsBaseInitializer) {
Sean Huntcbb67482011-01-08 20:30:50 +00004715 BOMInit = new (C) CXXCtorInitializer(C, BaseClassInfo, IsBaseVirtual,
4716 LParenLoc, Init, RParenLoc,
4717 MemberOrEllipsisLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004718 } else if (IsWritten) {
Francois Pichet00eb3f92010-12-04 09:14:42 +00004719 if (Member)
Sean Huntcbb67482011-01-08 20:30:50 +00004720 BOMInit = new (C) CXXCtorInitializer(C, Member, MemberOrEllipsisLoc,
4721 LParenLoc, Init, RParenLoc);
Francois Pichet00eb3f92010-12-04 09:14:42 +00004722 else
Sean Huntcbb67482011-01-08 20:30:50 +00004723 BOMInit = new (C) CXXCtorInitializer(C, IndirectMember,
4724 MemberOrEllipsisLoc, LParenLoc,
4725 Init, RParenLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004726 } else {
Sean Huntcbb67482011-01-08 20:30:50 +00004727 BOMInit = CXXCtorInitializer::Create(C, Member, MemberOrEllipsisLoc,
4728 LParenLoc, Init, RParenLoc,
4729 Indices.data(), Indices.size());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004730 }
4731
Argyrios Kyrtzidisf84cde12010-09-06 19:04:27 +00004732 if (IsWritten)
4733 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Sean Huntcbb67482011-01-08 20:30:50 +00004734 CtorInitializers[i] = BOMInit;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004735 }
4736 }
4737
Sean Huntcbb67482011-01-08 20:30:50 +00004738 return std::make_pair(CtorInitializers, NumInitializers);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004739}
4740
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004741NestedNameSpecifier *
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004742ASTReader::ReadNestedNameSpecifier(const RecordData &Record, unsigned &Idx) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004743 unsigned N = Record[Idx++];
4744 NestedNameSpecifier *NNS = 0, *Prev = 0;
4745 for (unsigned I = 0; I != N; ++I) {
4746 NestedNameSpecifier::SpecifierKind Kind
4747 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
4748 switch (Kind) {
4749 case NestedNameSpecifier::Identifier: {
4750 IdentifierInfo *II = GetIdentifierInfo(Record, Idx);
4751 NNS = NestedNameSpecifier::Create(*Context, Prev, II);
4752 break;
4753 }
4754
4755 case NestedNameSpecifier::Namespace: {
4756 NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++]));
4757 NNS = NestedNameSpecifier::Create(*Context, Prev, NS);
4758 break;
4759 }
4760
Douglas Gregor14aba762011-02-24 02:36:08 +00004761 case NestedNameSpecifier::NamespaceAlias: {
4762 NamespaceAliasDecl *Alias
4763 = cast<NamespaceAliasDecl>(GetDecl(Record[Idx++]));
4764 NNS = NestedNameSpecifier::Create(*Context, Prev, Alias);
4765 break;
4766 }
4767
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004768 case NestedNameSpecifier::TypeSpec:
4769 case NestedNameSpecifier::TypeSpecWithTemplate: {
John McCallf4c73712011-01-19 06:33:43 +00004770 const Type *T = GetType(Record[Idx++]).getTypePtrOrNull();
Douglas Gregor1ab55e92010-12-10 17:03:06 +00004771 if (!T)
4772 return 0;
4773
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004774 bool Template = Record[Idx++];
4775 NNS = NestedNameSpecifier::Create(*Context, Prev, Template, T);
4776 break;
4777 }
4778
4779 case NestedNameSpecifier::Global: {
4780 NNS = NestedNameSpecifier::GlobalSpecifier(*Context);
4781 // No associated value, and there can't be a prefix.
4782 break;
4783 }
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004784 }
Argyrios Kyrtzidisd2bb2c02010-07-07 15:46:30 +00004785 Prev = NNS;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004786 }
4787 return NNS;
4788}
4789
Douglas Gregordc355712011-02-25 00:36:19 +00004790NestedNameSpecifierLoc
4791ASTReader::ReadNestedNameSpecifierLoc(PerFileData &F, const RecordData &Record,
4792 unsigned &Idx) {
4793 unsigned N = Record[Idx++];
Douglas Gregor5f791bb2011-02-28 23:58:31 +00004794 NestedNameSpecifierLocBuilder Builder;
Douglas Gregordc355712011-02-25 00:36:19 +00004795 for (unsigned I = 0; I != N; ++I) {
4796 NestedNameSpecifier::SpecifierKind Kind
4797 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
4798 switch (Kind) {
4799 case NestedNameSpecifier::Identifier: {
Douglas Gregor5f791bb2011-02-28 23:58:31 +00004800 IdentifierInfo *II = GetIdentifierInfo(Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00004801 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor5f791bb2011-02-28 23:58:31 +00004802 Builder.Extend(*Context, II, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00004803 break;
4804 }
4805
4806 case NestedNameSpecifier::Namespace: {
Douglas Gregordc355712011-02-25 00:36:19 +00004807 NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++]));
Douglas Gregordc355712011-02-25 00:36:19 +00004808 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor5f791bb2011-02-28 23:58:31 +00004809 Builder.Extend(*Context, NS, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00004810 break;
4811 }
4812
4813 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregordc355712011-02-25 00:36:19 +00004814 NamespaceAliasDecl *Alias
4815 = cast<NamespaceAliasDecl>(GetDecl(Record[Idx++]));
Douglas Gregordc355712011-02-25 00:36:19 +00004816 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor5f791bb2011-02-28 23:58:31 +00004817 Builder.Extend(*Context, Alias, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00004818 break;
4819 }
4820
4821 case NestedNameSpecifier::TypeSpec:
4822 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregordc355712011-02-25 00:36:19 +00004823 bool Template = Record[Idx++];
4824 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
4825 if (!T)
4826 return NestedNameSpecifierLoc();
Douglas Gregordc355712011-02-25 00:36:19 +00004827 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor5f791bb2011-02-28 23:58:31 +00004828
4829 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
4830 Builder.Extend(*Context,
4831 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
4832 T->getTypeLoc(), ColonColonLoc);
Douglas Gregordc355712011-02-25 00:36:19 +00004833 break;
4834 }
4835
4836 case NestedNameSpecifier::Global: {
Douglas Gregordc355712011-02-25 00:36:19 +00004837 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor5f791bb2011-02-28 23:58:31 +00004838 Builder.MakeGlobal(*Context, ColonColonLoc);
Douglas Gregordc355712011-02-25 00:36:19 +00004839 break;
4840 }
4841 }
Douglas Gregordc355712011-02-25 00:36:19 +00004842 }
4843
Douglas Gregor5f791bb2011-02-28 23:58:31 +00004844 return Builder.getWithLocInContext(*Context);
Douglas Gregordc355712011-02-25 00:36:19 +00004845}
4846
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004847SourceRange
Sebastian Redlc3632732010-10-05 15:59:54 +00004848ASTReader::ReadSourceRange(PerFileData &F, const RecordData &Record,
4849 unsigned &Idx) {
4850 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
4851 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar8ee59392010-06-02 15:47:10 +00004852 return SourceRange(beg, end);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004853}
4854
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004855/// \brief Read an integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004856llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004857 unsigned BitWidth = Record[Idx++];
4858 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
4859 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
4860 Idx += NumWords;
4861 return Result;
4862}
4863
4864/// \brief Read a signed integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004865llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004866 bool isUnsigned = Record[Idx++];
4867 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
4868}
4869
Douglas Gregor17fc2232009-04-14 21:55:33 +00004870/// \brief Read a floating-point value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004871llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00004872 return llvm::APFloat(ReadAPInt(Record, Idx));
4873}
4874
Douglas Gregor68a2eb02009-04-15 21:30:51 +00004875// \brief Read a string
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004876std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00004877 unsigned Len = Record[Idx++];
Jay Foadbeaaccd2009-05-21 09:52:38 +00004878 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00004879 Idx += Len;
4880 return Result;
4881}
4882
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00004883VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
4884 unsigned &Idx) {
4885 unsigned Major = Record[Idx++];
4886 unsigned Minor = Record[Idx++];
4887 unsigned Subminor = Record[Idx++];
4888 if (Minor == 0)
4889 return VersionTuple(Major);
4890 if (Subminor == 0)
4891 return VersionTuple(Major, Minor - 1);
4892 return VersionTuple(Major, Minor - 1, Subminor - 1);
4893}
4894
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004895CXXTemporary *ASTReader::ReadCXXTemporary(const RecordData &Record,
Chris Lattnerd2598362010-05-10 00:25:06 +00004896 unsigned &Idx) {
4897 CXXDestructorDecl *Decl = cast<CXXDestructorDecl>(GetDecl(Record[Idx++]));
4898 return CXXTemporary::Create(*Context, Decl);
4899}
4900
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004901DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00004902 return Diag(SourceLocation(), DiagID);
4903}
4904
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004905DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004906 return Diags.Report(Loc, DiagID);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00004907}
Douglas Gregor025452f2009-04-17 00:04:06 +00004908
Douglas Gregor668c1a42009-04-21 22:25:48 +00004909/// \brief Retrieve the identifier table associated with the
4910/// preprocessor.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004911IdentifierTable &ASTReader::getIdentifierTable() {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00004912 assert(PP && "Forgot to set Preprocessor ?");
4913 return PP->getIdentifierTable();
Douglas Gregor668c1a42009-04-21 22:25:48 +00004914}
4915
Douglas Gregor025452f2009-04-17 00:04:06 +00004916/// \brief Record that the given ID maps to the given switch-case
4917/// statement.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004918void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Douglas Gregor025452f2009-04-17 00:04:06 +00004919 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
4920 SwitchCaseStmts[ID] = SC;
4921}
4922
4923/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004924SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Douglas Gregor025452f2009-04-17 00:04:06 +00004925 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
4926 return SwitchCaseStmts[ID];
4927}
Douglas Gregor1de05fe2009-04-17 18:18:49 +00004928
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00004929void ASTReader::ClearSwitchCaseIDs() {
4930 SwitchCaseStmts.clear();
4931}
4932
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004933void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004934 assert(NumCurrentElementsDeserializing &&
4935 "FinishedDeserializing not paired with StartedDeserializing");
4936 if (NumCurrentElementsDeserializing == 1) {
Douglas Gregord89275b2009-07-06 18:54:52 +00004937 // If any identifiers with corresponding top-level declarations have
4938 // been loaded, load those declarations now.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004939 while (!PendingIdentifierInfos.empty()) {
4940 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
4941 PendingIdentifierInfos.front().DeclIDs, true);
4942 PendingIdentifierInfos.pop_front();
Douglas Gregord89275b2009-07-06 18:54:52 +00004943 }
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00004944
Argyrios Kyrtzidis0895d152011-02-12 07:50:47 +00004945 // Ready to load previous declarations of Decls that were delayed.
4946 while (!PendingPreviousDecls.empty()) {
4947 loadAndAttachPreviousDecl(PendingPreviousDecls.front().first,
4948 PendingPreviousDecls.front().second);
4949 PendingPreviousDecls.pop_front();
4950 }
4951
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00004952 // We are not in recursive loading, so it's safe to pass the "interesting"
4953 // decls to the consumer.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004954 if (Consumer)
4955 PassInterestingDeclsToConsumer();
Argyrios Kyrtzidis134db1f2010-10-24 17:26:31 +00004956
4957 assert(PendingForwardRefs.size() == 0 &&
4958 "Some forward refs did not get linked to the definition!");
Douglas Gregord89275b2009-07-06 18:54:52 +00004959 }
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004960 --NumCurrentElementsDeserializing;
Douglas Gregord89275b2009-07-06 18:54:52 +00004961}
Douglas Gregor501c1032010-08-19 00:28:17 +00004962
Sebastian Redle1dde812010-08-24 00:50:04 +00004963ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context,
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00004964 const char *isysroot, bool DisableValidation,
4965 bool DisableStatCache)
Sebastian Redle1dde812010-08-24 00:50:04 +00004966 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
4967 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
4968 Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context),
4969 Consumer(0), isysroot(isysroot), DisableValidation(DisableValidation),
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00004970 DisableStatCache(DisableStatCache), NumStatHits(0), NumStatMisses(0),
4971 NumSLocEntriesRead(0), TotalNumSLocEntries(0), NextSLocOffset(0),
4972 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
4973 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
4974 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
4975 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
4976 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
4977 NumCurrentElementsDeserializing(0)
4978{
Sebastian Redle1dde812010-08-24 00:50:04 +00004979 RelocatablePCH = false;
4980}
4981
4982ASTReader::ASTReader(SourceManager &SourceMgr, FileManager &FileMgr,
4983 Diagnostic &Diags, const char *isysroot,
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00004984 bool DisableValidation, bool DisableStatCache)
Sebastian Redle1dde812010-08-24 00:50:04 +00004985 : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr),
4986 Diags(Diags), SemaObj(0), PP(0), Context(0), Consumer(0),
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00004987 isysroot(isysroot), DisableValidation(DisableValidation),
4988 DisableStatCache(DisableStatCache), NumStatHits(0), NumStatMisses(0),
4989 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Sebastian Redl8db9fae2010-09-22 20:19:08 +00004990 NextSLocOffset(0), NumStatementsRead(0), TotalNumStatements(0),
4991 NumMacrosRead(0), TotalNumMacros(0), NumSelectorsRead(0),
4992 NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
4993 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
4994 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
4995 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
Sebastian Redle1dde812010-08-24 00:50:04 +00004996 RelocatablePCH = false;
4997}
4998
4999ASTReader::~ASTReader() {
5000 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
5001 delete Chain[e - i - 1];
5002 // Delete all visible decl lookup tables
5003 for (DeclContextOffsetsMap::iterator I = DeclContextOffsets.begin(),
5004 E = DeclContextOffsets.end();
5005 I != E; ++I) {
5006 for (DeclContextInfos::iterator J = I->second.begin(), F = I->second.end();
5007 J != F; ++J) {
5008 if (J->NameLookupTableData)
5009 delete static_cast<ASTDeclContextNameLookupTable*>(
5010 J->NameLookupTableData);
5011 }
5012 }
5013 for (DeclContextVisibleUpdatesPending::iterator
5014 I = PendingVisibleUpdates.begin(),
5015 E = PendingVisibleUpdates.end();
5016 I != E; ++I) {
5017 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
5018 F = I->second.end();
5019 J != F; ++J)
5020 delete static_cast<ASTDeclContextNameLookupTable*>(*J);
5021 }
5022}
5023
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00005024ASTReader::PerFileData::PerFileData(ASTFileType Ty)
5025 : Type(Ty), SizeInBits(0), LocalNumSLocEntries(0), SLocOffsets(0), LocalSLocSize(0),
Sebastian Redl301c9b02010-09-22 00:42:27 +00005026 LocalNumIdentifiers(0), IdentifierOffsets(0), IdentifierTableData(0),
5027 IdentifierLookupTable(0), LocalNumMacroDefinitions(0),
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00005028 MacroDefinitionOffsets(0),
5029 LocalNumHeaderFileInfos(0), HeaderFileInfoTableData(0),
5030 HeaderFileInfoTable(0),
5031 LocalNumSelectors(0), SelectorOffsets(0),
Sebastian Redl301c9b02010-09-22 00:42:27 +00005032 SelectorLookupTableData(0), SelectorLookupTable(0), LocalNumDecls(0),
Douglas Gregor7c789c12010-10-29 22:39:52 +00005033 DeclOffsets(0), LocalNumCXXBaseSpecifiers(0), CXXBaseSpecifiersOffsets(0),
5034 LocalNumTypes(0), TypeOffsets(0), StatCache(0),
Sebastian Redla866e652010-10-01 19:59:12 +00005035 NumPreallocatedPreprocessingEntities(0), NextInSource(0)
Douglas Gregor501c1032010-08-19 00:28:17 +00005036{}
5037
5038ASTReader::PerFileData::~PerFileData() {
5039 delete static_cast<ASTIdentifierLookupTable *>(IdentifierLookupTable);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00005040 delete static_cast<HeaderFileInfoLookupTable *>(HeaderFileInfoTable);
Douglas Gregor501c1032010-08-19 00:28:17 +00005041 delete static_cast<ASTSelectorLookupTable *>(SelectorLookupTable);
5042}