blob: 5a4c59e3cde247e1fd82367a52159ef4be694141 [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);
Chandler Carruth0d2d1bc2011-04-23 20:05:38 +0000132 PARSE_LANGOPT_IMPORTANT(Deprecated, diag::warn_pch_deprecated);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000133 PARSE_LANGOPT_IMPORTANT(AccessControl, diag::warn_pch_access_control);
134 PARSE_LANGOPT_IMPORTANT(CharIsSigned, diag::warn_pch_char_signed);
John Thompsona6fda122009-11-05 20:14:16 +0000135 PARSE_LANGOPT_IMPORTANT(ShortWChar, diag::warn_pch_short_wchar);
Argyrios Kyrtzidis9a2b9d72010-10-08 00:25:19 +0000136 PARSE_LANGOPT_IMPORTANT(ShortEnums, diag::warn_pch_short_enums);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000137 if ((PPLangOpts.getGCMode() != 0) != (LangOpts.getGCMode() != 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000138 Reader.Diag(diag::warn_pch_gc_mode)
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000139 << LangOpts.getGCMode() << PPLangOpts.getGCMode();
140 return true;
141 }
142 PARSE_LANGOPT_BENIGN(getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000143 PARSE_LANGOPT_IMPORTANT(getStackProtectorMode(),
144 diag::warn_pch_stack_protector);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000145 PARSE_LANGOPT_BENIGN(InstantiationDepth);
Nate Begeman69cfb9b2009-06-25 22:57:40 +0000146 PARSE_LANGOPT_IMPORTANT(OpenCL, diag::warn_pch_opencl);
Peter Collingbourne08a53262010-12-01 19:14:57 +0000147 PARSE_LANGOPT_IMPORTANT(CUDA, diag::warn_pch_cuda);
Mike Stump9c276ae2009-12-12 01:27:46 +0000148 PARSE_LANGOPT_BENIGN(CatchUndefined);
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000149 PARSE_LANGOPT_IMPORTANT(ElideConstructors, diag::warn_pch_elide_constructors);
Douglas Gregora0068fc2010-07-09 17:35:33 +0000150 PARSE_LANGOPT_BENIGN(SpellChecking);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000151 PARSE_LANGOPT_BENIGN(DefaultFPContract);
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +0000152#undef PARSE_LANGOPT_IMPORTANT
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000153#undef PARSE_LANGOPT_BENIGN
154
155 return false;
156}
157
Daniel Dunbardc3c0d22009-11-11 00:52:11 +0000158bool PCHValidator::ReadTargetTriple(llvm::StringRef Triple) {
159 if (Triple == PP.getTargetInfo().getTriple().str())
160 return false;
161
162 Reader.Diag(diag::warn_pch_target_triple)
163 << Triple << PP.getTargetInfo().getTriple().str();
164 return true;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000165}
166
Benjamin Kramer54353f42010-11-25 18:29:30 +0000167namespace {
168 struct EmptyStringRef {
169 bool operator ()(llvm::StringRef r) const { return r.empty(); }
170 };
171 struct EmptyBlock {
172 bool operator ()(const PCHPredefinesBlock &r) const {return r.Data.empty();}
173 };
174}
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000175
176static bool EqualConcatenations(llvm::SmallVector<llvm::StringRef, 2> L,
177 PCHPredefinesBlocks R) {
178 // First, sum up the lengths.
179 unsigned LL = 0, RL = 0;
180 for (unsigned I = 0, N = L.size(); I != N; ++I) {
181 LL += L[I].size();
182 }
183 for (unsigned I = 0, N = R.size(); I != N; ++I) {
184 RL += R[I].Data.size();
185 }
186 if (LL != RL)
187 return false;
188 if (LL == 0 && RL == 0)
189 return true;
190
191 // Kick out empty parts, they confuse the algorithm below.
192 L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end());
193 R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end());
194
195 // Do it the hard way. At this point, both vectors must be non-empty.
196 llvm::StringRef LR = L[0], RR = R[0].Data;
197 unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size();
Daniel Dunbarc76c9e02010-07-16 00:00:11 +0000198 (void) RN;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000199 for (;;) {
200 // Compare the current pieces.
201 if (LR.size() == RR.size()) {
202 // If they're the same length, it's pretty easy.
203 if (LR != RR)
204 return false;
205 // Both pieces are done, advance.
206 ++LI;
207 ++RI;
208 // If either string is done, they're both done, since they're the same
209 // length.
210 if (LI == LN) {
211 assert(RI == RN && "Strings not the same length after all?");
212 return true;
213 }
214 LR = L[LI];
215 RR = R[RI].Data;
216 } else if (LR.size() < RR.size()) {
217 // Right piece is longer.
218 if (!RR.startswith(LR))
219 return false;
220 ++LI;
221 assert(LI != LN && "Strings not the same length after all?");
222 RR = RR.substr(LR.size());
223 LR = L[LI];
224 } else {
225 // Left piece is longer.
226 if (!LR.startswith(RR))
227 return false;
228 ++RI;
229 assert(RI != RN && "Strings not the same length after all?");
230 LR = LR.substr(RR.size());
231 RR = R[RI].Data;
232 }
233 }
234}
235
236static std::pair<FileID, llvm::StringRef::size_type>
237FindMacro(const PCHPredefinesBlocks &Buffers, llvm::StringRef MacroDef) {
238 std::pair<FileID, llvm::StringRef::size_type> Res;
239 for (unsigned I = 0, N = Buffers.size(); I != N; ++I) {
240 Res.second = Buffers[I].Data.find(MacroDef);
241 if (Res.second != llvm::StringRef::npos) {
242 Res.first = Buffers[I].BufferID;
243 break;
244 }
245 }
246 return Res;
247}
248
249bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000250 llvm::StringRef OriginalFileName,
Nick Lewycky277a6e72011-02-23 21:16:44 +0000251 std::string &SuggestedPredefines,
252 FileManager &FileMgr) {
Daniel Dunbarc7162932009-11-11 23:58:53 +0000253 // We are in the context of an implicit include, so the predefines buffer will
254 // have a #include entry for the PCH file itself (as normalized by the
255 // preprocessor initialization). Find it and skip over it in the checking
256 // below.
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000257 llvm::SmallString<256> PCHInclude;
258 PCHInclude += "#include \"";
Nick Lewycky277a6e72011-02-23 21:16:44 +0000259 PCHInclude += NormalizeDashIncludePath(OriginalFileName, FileMgr);
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000260 PCHInclude += "\"\n";
261 std::pair<llvm::StringRef,llvm::StringRef> Split =
262 llvm::StringRef(PP.getPredefines()).split(PCHInclude.str());
263 llvm::StringRef Left = Split.first, Right = Split.second;
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000264 if (Left == PP.getPredefines()) {
265 Error("Missing PCH include entry!");
266 return true;
267 }
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000268
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000269 // If the concatenation of all the PCH buffers is equal to the adjusted
270 // command line, we're done.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000271 llvm::SmallVector<llvm::StringRef, 2> CommandLine;
272 CommandLine.push_back(Left);
273 CommandLine.push_back(Right);
274 if (EqualConcatenations(CommandLine, Buffers))
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000275 return false;
276
277 SourceManager &SourceMgr = PP.getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000278
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000279 // The predefines buffers are different. Determine what the differences are,
280 // and whether they require us to reject the PCH file.
Daniel Dunbare6750492009-11-13 16:46:11 +0000281 llvm::SmallVector<llvm::StringRef, 8> PCHLines;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000282 for (unsigned I = 0, N = Buffers.size(); I != N; ++I)
283 Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Daniel Dunbare6750492009-11-13 16:46:11 +0000284
285 llvm::SmallVector<llvm::StringRef, 8> CmdLineLines;
286 Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000287
288 // Pick out implicit #includes after the PCH and don't consider them for
289 // validation; we will insert them into SuggestedPredefines so that the
290 // preprocessor includes them.
291 std::string IncludesAfterPCH;
292 llvm::SmallVector<llvm::StringRef, 8> AfterPCHLines;
293 Right.split(AfterPCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
294 for (unsigned i = 0, e = AfterPCHLines.size(); i != e; ++i) {
295 if (AfterPCHLines[i].startswith("#include ")) {
296 IncludesAfterPCH += AfterPCHLines[i];
297 IncludesAfterPCH += '\n';
298 } else {
299 CmdLineLines.push_back(AfterPCHLines[i]);
300 }
301 }
302
303 // Make sure we add the includes last into SuggestedPredefines before we
304 // exit this function.
305 struct AddIncludesRAII {
306 std::string &SuggestedPredefines;
307 std::string &IncludesAfterPCH;
308
309 AddIncludesRAII(std::string &SuggestedPredefines,
310 std::string &IncludesAfterPCH)
311 : SuggestedPredefines(SuggestedPredefines),
312 IncludesAfterPCH(IncludesAfterPCH) { }
313 ~AddIncludesRAII() {
314 SuggestedPredefines += IncludesAfterPCH;
315 }
316 } AddIncludes(SuggestedPredefines, IncludesAfterPCH);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000317
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000318 // Sort both sets of predefined buffer lines, since we allow some extra
319 // definitions and they may appear at any point in the output.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000320 std::sort(CmdLineLines.begin(), CmdLineLines.end());
321 std::sort(PCHLines.begin(), PCHLines.end());
322
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000323 // Determine which predefines that were used to build the PCH file are missing
324 // from the command line.
325 std::vector<llvm::StringRef> MissingPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000326 std::set_difference(PCHLines.begin(), PCHLines.end(),
327 CmdLineLines.begin(), CmdLineLines.end(),
328 std::back_inserter(MissingPredefines));
329
330 bool MissingDefines = false;
331 bool ConflictingDefines = false;
332 for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) {
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000333 llvm::StringRef Missing = MissingPredefines[I];
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000334 if (Missing.startswith("#include ")) {
335 // An -include was specified when generating the PCH; it is included in
336 // the PCH, just ignore it.
337 continue;
338 }
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000339 if (!Missing.startswith("#define ")) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000340 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
341 return true;
342 }
Mike Stump1eb44332009-09-09 15:08:12 +0000343
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000344 // This is a macro definition. Determine the name of the macro we're
345 // defining.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000346 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000347 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000348 = Missing.find_first_of("( \n\r", StartOfMacroName);
349 assert(EndOfMacroName != std::string::npos &&
350 "Couldn't find the end of the macro name");
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000351 llvm::StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000352
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000353 // Determine whether this macro was given a different definition on the
354 // command line.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000355 std::string MacroDefStart = "#define " + MacroName.str();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000356 std::string::size_type MacroDefLen = MacroDefStart.size();
Daniel Dunbare6750492009-11-13 16:46:11 +0000357 llvm::SmallVector<llvm::StringRef, 8>::iterator ConflictPos
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000358 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
359 MacroDefStart);
360 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000361 if (!ConflictPos->startswith(MacroDefStart)) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000362 // Different macro; we're done.
363 ConflictPos = CmdLineLines.end();
Mike Stump1eb44332009-09-09 15:08:12 +0000364 break;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000365 }
Mike Stump1eb44332009-09-09 15:08:12 +0000366
367 assert(ConflictPos->size() > MacroDefLen &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000368 "Invalid #define in predefines buffer?");
Mike Stump1eb44332009-09-09 15:08:12 +0000369 if ((*ConflictPos)[MacroDefLen] != ' ' &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000370 (*ConflictPos)[MacroDefLen] != '(')
371 continue; // Longer macro name; keep trying.
Mike Stump1eb44332009-09-09 15:08:12 +0000372
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000373 // We found a conflicting macro definition.
374 break;
375 }
Mike Stump1eb44332009-09-09 15:08:12 +0000376
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000377 if (ConflictPos != CmdLineLines.end()) {
378 Reader.Diag(diag::warn_cmdline_conflicting_macro_def)
379 << MacroName;
380
381 // Show the definition of this macro within the PCH file.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000382 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
383 FindMacro(Buffers, Missing);
384 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
385 SourceLocation PCHMissingLoc =
386 SourceMgr.getLocForStartOfFile(MacroLoc.first)
387 .getFileLocWithOffset(MacroLoc.second);
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000388 Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000389
390 ConflictingDefines = true;
391 continue;
392 }
Mike Stump1eb44332009-09-09 15:08:12 +0000393
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000394 // If the macro doesn't conflict, then we'll just pick up the macro
395 // definition from the PCH file. Warn the user that they made a mistake.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000396 if (ConflictingDefines)
397 continue; // Don't complain if there are already conflicting defs
Mike Stump1eb44332009-09-09 15:08:12 +0000398
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000399 if (!MissingDefines) {
400 Reader.Diag(diag::warn_cmdline_missing_macro_defs);
401 MissingDefines = true;
402 }
403
404 // Show the definition of this macro within the PCH file.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000405 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
406 FindMacro(Buffers, Missing);
407 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
408 SourceLocation PCHMissingLoc =
409 SourceMgr.getLocForStartOfFile(MacroLoc.first)
410 .getFileLocWithOffset(MacroLoc.second);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000411 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
412 }
Mike Stump1eb44332009-09-09 15:08:12 +0000413
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000414 if (ConflictingDefines)
415 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000416
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000417 // Determine what predefines were introduced based on command-line
418 // parameters that were not present when building the PCH
419 // file. Extra #defines are okay, so long as the identifiers being
420 // defined were not used within the precompiled header.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000421 std::vector<llvm::StringRef> ExtraPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000422 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
423 PCHLines.begin(), PCHLines.end(),
Mike Stump1eb44332009-09-09 15:08:12 +0000424 std::back_inserter(ExtraPredefines));
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000425 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000426 llvm::StringRef &Extra = ExtraPredefines[I];
427 if (!Extra.startswith("#define ")) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000428 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
429 return true;
430 }
431
432 // This is an extra macro definition. Determine the name of the
433 // macro we're defining.
434 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000435 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000436 = Extra.find_first_of("( \n\r", StartOfMacroName);
437 assert(EndOfMacroName != std::string::npos &&
438 "Couldn't find the end of the macro name");
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000439 llvm::StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000440
441 // Check whether this name was used somewhere in the PCH file. If
442 // so, defining it as a macro could change behavior, so we reject
443 // the PCH file.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000444 if (IdentifierInfo *II = Reader.get(MacroName)) {
Daniel Dunbar4fda42e2009-11-11 00:52:00 +0000445 Reader.Diag(diag::warn_macro_name_used_in_pch) << II;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000446 return true;
447 }
448
449 // Add this definition to the suggested predefines buffer.
450 SuggestedPredefines += Extra;
451 SuggestedPredefines += '\n';
452 }
453
454 // If we get here, it's because the predefines buffer had compatible
455 // contents. Accept the PCH file.
456 return false;
457}
458
Douglas Gregor12fab312010-03-16 16:35:32 +0000459void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
460 unsigned ID) {
461 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
462 ++NumHeaderInfos;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000463}
464
465void PCHValidator::ReadCounter(unsigned Value) {
466 PP.setCounterValue(Value);
467}
468
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000469//===----------------------------------------------------------------------===//
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000470// AST reader implementation
Douglas Gregor668c1a42009-04-21 22:25:48 +0000471//===----------------------------------------------------------------------===//
472
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000473void
Sebastian Redl571db7f2010-08-18 23:56:56 +0000474ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000475 DeserializationListener = Listener;
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000476}
477
Chris Lattner4c6f9522009-04-27 05:14:47 +0000478
Douglas Gregor668c1a42009-04-21 22:25:48 +0000479namespace {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000480class ASTSelectorLookupTrait {
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000481 ASTReader &Reader;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000482
483public:
Sebastian Redl5d050072010-08-04 17:20:04 +0000484 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000485 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +0000486 ObjCMethodList Instance, Factory;
487 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000488
489 typedef Selector external_key_type;
490 typedef external_key_type internal_key_type;
491
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000492 explicit ASTSelectorLookupTrait(ASTReader &Reader) : Reader(Reader) { }
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000494 static bool EqualKey(const internal_key_type& a,
495 const internal_key_type& b) {
496 return a == b;
497 }
Mike Stump1eb44332009-09-09 15:08:12 +0000498
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000499 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +0000500 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000501 }
Mike Stump1eb44332009-09-09 15:08:12 +0000502
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000503 // This hopefully will just get inlined and removed by the optimizer.
504 static const internal_key_type&
505 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump1eb44332009-09-09 15:08:12 +0000506
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000507 static std::pair<unsigned, unsigned>
508 ReadKeyDataLength(const unsigned char*& d) {
509 using namespace clang::io;
510 unsigned KeyLen = ReadUnalignedLE16(d);
511 unsigned DataLen = ReadUnalignedLE16(d);
512 return std::make_pair(KeyLen, DataLen);
513 }
Mike Stump1eb44332009-09-09 15:08:12 +0000514
Douglas Gregor83941df2009-04-25 17:48:32 +0000515 internal_key_type ReadKey(const unsigned char* d, unsigned) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000516 using namespace clang::io;
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000517 SelectorTable &SelTable = Reader.getContext()->Selectors;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000518 unsigned N = ReadUnalignedLE16(d);
Mike Stump1eb44332009-09-09 15:08:12 +0000519 IdentifierInfo *FirstII
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000520 = Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
521 if (N == 0)
522 return SelTable.getNullarySelector(FirstII);
523 else if (N == 1)
524 return SelTable.getUnarySelector(FirstII);
525
526 llvm::SmallVector<IdentifierInfo *, 16> Args;
527 Args.push_back(FirstII);
528 for (unsigned I = 1; I != N; ++I)
529 Args.push_back(Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)));
530
Douglas Gregor75fdb232009-05-22 22:45:36 +0000531 return SelTable.getSelector(N, Args.data());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000532 }
Mike Stump1eb44332009-09-09 15:08:12 +0000533
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000534 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen) {
535 using namespace clang::io;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000536
537 data_type Result;
538
Sebastian Redl5d050072010-08-04 17:20:04 +0000539 Result.ID = ReadUnalignedLE32(d);
540 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
541 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
542
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000543 // Load instance methods
544 ObjCMethodList *Prev = 0;
545 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000546 ObjCMethodDecl *Method
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000547 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl5d050072010-08-04 17:20:04 +0000548 if (!Result.Instance.Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000549 // This is the first method, which is the easy case.
Sebastian Redl5d050072010-08-04 17:20:04 +0000550 Result.Instance.Method = Method;
551 Prev = &Result.Instance;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000552 continue;
553 }
554
Ted Kremenek298ed872010-02-11 00:53:01 +0000555 ObjCMethodList *Mem =
556 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
557 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000558 Prev = Prev->Next;
559 }
560
561 // Load factory methods
562 Prev = 0;
563 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000564 ObjCMethodDecl *Method
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000565 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl5d050072010-08-04 17:20:04 +0000566 if (!Result.Factory.Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000567 // This is the first method, which is the easy case.
Sebastian Redl5d050072010-08-04 17:20:04 +0000568 Result.Factory.Method = Method;
569 Prev = &Result.Factory;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000570 continue;
571 }
572
Ted Kremenek298ed872010-02-11 00:53:01 +0000573 ObjCMethodList *Mem =
574 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
575 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000576 Prev = Prev->Next;
577 }
578
579 return Result;
580 }
581};
Mike Stump1eb44332009-09-09 15:08:12 +0000582
583} // end anonymous namespace
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000584
585/// \brief The on-disk hash table used for the global method pool.
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000586typedef OnDiskChainedHashTable<ASTSelectorLookupTrait>
587 ASTSelectorLookupTable;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000588
Sebastian Redlc3632732010-10-05 15:59:54 +0000589namespace clang {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000590class ASTIdentifierLookupTrait {
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000591 ASTReader &Reader;
Sebastian Redlc3632732010-10-05 15:59:54 +0000592 ASTReader::PerFileData &F;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000593
594 // If we know the IdentifierInfo in advance, it is here and we will
595 // not build a new one. Used when deserializing information about an
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000596 // identifier that was constructed before the AST file was read.
Douglas Gregor668c1a42009-04-21 22:25:48 +0000597 IdentifierInfo *KnownII;
598
599public:
600 typedef IdentifierInfo * data_type;
601
602 typedef const std::pair<const char*, unsigned> external_key_type;
603
604 typedef external_key_type internal_key_type;
605
Sebastian Redlc3632732010-10-05 15:59:54 +0000606 ASTIdentifierLookupTrait(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redld27d3fc2010-07-21 22:31:37 +0000607 IdentifierInfo *II = 0)
Sebastian Redlc3632732010-10-05 15:59:54 +0000608 : Reader(Reader), F(F), KnownII(II) { }
Mike Stump1eb44332009-09-09 15:08:12 +0000609
Douglas Gregor668c1a42009-04-21 22:25:48 +0000610 static bool EqualKey(const internal_key_type& a,
611 const internal_key_type& b) {
612 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
613 : false;
614 }
Mike Stump1eb44332009-09-09 15:08:12 +0000615
Douglas Gregor668c1a42009-04-21 22:25:48 +0000616 static unsigned ComputeHash(const internal_key_type& a) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000617 return llvm::HashString(llvm::StringRef(a.first, a.second));
Douglas Gregor668c1a42009-04-21 22:25:48 +0000618 }
Mike Stump1eb44332009-09-09 15:08:12 +0000619
Douglas Gregor668c1a42009-04-21 22:25:48 +0000620 // This hopefully will just get inlined and removed by the optimizer.
621 static const internal_key_type&
622 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump1eb44332009-09-09 15:08:12 +0000623
Douglas Gregor95f42922010-10-14 22:11:03 +0000624 // This hopefully will just get inlined and removed by the optimizer.
625 static const external_key_type&
626 GetExternalKey(const internal_key_type& x) { return x; }
627
Douglas Gregor668c1a42009-04-21 22:25:48 +0000628 static std::pair<unsigned, unsigned>
629 ReadKeyDataLength(const unsigned char*& d) {
630 using namespace clang::io;
Douglas Gregor5f8e3302009-04-25 20:26:24 +0000631 unsigned DataLen = ReadUnalignedLE16(d);
Douglas Gregord6595a42009-04-25 21:04:17 +0000632 unsigned KeyLen = ReadUnalignedLE16(d);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000633 return std::make_pair(KeyLen, DataLen);
634 }
Mike Stump1eb44332009-09-09 15:08:12 +0000635
Douglas Gregor668c1a42009-04-21 22:25:48 +0000636 static std::pair<const char*, unsigned>
637 ReadKey(const unsigned char* d, unsigned n) {
638 assert(n >= 2 && d[n-1] == '\0');
639 return std::make_pair((const char*) d, n-1);
640 }
Mike Stump1eb44332009-09-09 15:08:12 +0000641
642 IdentifierInfo *ReadData(const internal_key_type& k,
Douglas Gregor668c1a42009-04-21 22:25:48 +0000643 const unsigned char* d,
644 unsigned DataLen) {
645 using namespace clang::io;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000646 IdentID ID = ReadUnalignedLE32(d);
Douglas Gregora92193e2009-04-28 21:18:29 +0000647 bool IsInteresting = ID & 0x01;
648
649 // Wipe out the "is interesting" bit.
650 ID = ID >> 1;
651
652 if (!IsInteresting) {
Sebastian Redl083abdf2010-07-27 23:01:28 +0000653 // For uninteresting identifiers, just build the IdentifierInfo
Douglas Gregora92193e2009-04-28 21:18:29 +0000654 // and associate it with the persistent ID.
655 IdentifierInfo *II = KnownII;
656 if (!II)
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000657 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregora92193e2009-04-28 21:18:29 +0000658 Reader.SetIdentifierInfo(ID, II);
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000659 II->setIsFromAST();
Douglas Gregora92193e2009-04-28 21:18:29 +0000660 return II;
661 }
662
Douglas Gregor5998da52009-04-28 21:32:13 +0000663 unsigned Bits = ReadUnalignedLE16(d);
Douglas Gregor2deaea32009-04-22 18:49:13 +0000664 bool CPlusPlusOperatorKeyword = Bits & 0x01;
665 Bits >>= 1;
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000666 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
667 Bits >>= 1;
Douglas Gregor2deaea32009-04-22 18:49:13 +0000668 bool Poisoned = Bits & 0x01;
669 Bits >>= 1;
670 bool ExtensionToken = Bits & 0x01;
671 Bits >>= 1;
672 bool hasMacroDefinition = Bits & 0x01;
673 Bits >>= 1;
674 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
675 Bits >>= 10;
Mike Stump1eb44332009-09-09 15:08:12 +0000676
Douglas Gregor2deaea32009-04-22 18:49:13 +0000677 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregor5998da52009-04-28 21:32:13 +0000678 DataLen -= 6;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000679
680 // Build the IdentifierInfo itself and link the identifier ID with
681 // the new IdentifierInfo.
682 IdentifierInfo *II = KnownII;
683 if (!II)
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000684 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000685 Reader.SetIdentifierInfo(ID, II);
686
Douglas Gregor2deaea32009-04-22 18:49:13 +0000687 // Set or check the various bits in the IdentifierInfo structure.
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000688 // Token IDs are read-only.
689 if (HasRevertedTokenIDToIdentifier)
690 II->RevertTokenIDToIdentifier();
Douglas Gregor2deaea32009-04-22 18:49:13 +0000691 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
Mike Stump1eb44332009-09-09 15:08:12 +0000692 assert(II->isExtensionToken() == ExtensionToken &&
Douglas Gregor2deaea32009-04-22 18:49:13 +0000693 "Incorrect extension token flag");
694 (void)ExtensionToken;
695 II->setIsPoisoned(Poisoned);
696 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
697 "Incorrect C++ operator keyword flag");
698 (void)CPlusPlusOperatorKeyword;
699
Douglas Gregor37e26842009-04-21 23:56:24 +0000700 // If this identifier is a macro, deserialize the macro
701 // definition.
702 if (hasMacroDefinition) {
Douglas Gregor5998da52009-04-28 21:32:13 +0000703 uint32_t Offset = ReadUnalignedLE32(d);
Douglas Gregor295a2a62010-10-30 00:23:06 +0000704 Reader.SetIdentifierIsMacro(II, F, Offset);
Douglas Gregor5998da52009-04-28 21:32:13 +0000705 DataLen -= 4;
Douglas Gregor37e26842009-04-21 23:56:24 +0000706 }
Douglas Gregor668c1a42009-04-21 22:25:48 +0000707
708 // Read all of the declarations visible at global scope with this
709 // name.
Chris Lattner6bf690f2009-04-27 22:17:41 +0000710 if (Reader.getContext() == 0) return II;
Douglas Gregord89275b2009-07-06 18:54:52 +0000711 if (DataLen > 0) {
712 llvm::SmallVector<uint32_t, 4> DeclIDs;
713 for (; DataLen > 0; DataLen -= 4)
714 DeclIDs.push_back(ReadUnalignedLE32(d));
715 Reader.SetGloballyVisibleDecls(II, DeclIDs);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000716 }
Mike Stump1eb44332009-09-09 15:08:12 +0000717
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000718 II->setIsFromAST();
Douglas Gregor668c1a42009-04-21 22:25:48 +0000719 return II;
720 }
721};
Mike Stump1eb44332009-09-09 15:08:12 +0000722
723} // end anonymous namespace
Douglas Gregor668c1a42009-04-21 22:25:48 +0000724
725/// \brief The on-disk hash table used to contain information about
726/// all of the identifiers in the program.
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000727typedef OnDiskChainedHashTable<ASTIdentifierLookupTrait>
728 ASTIdentifierLookupTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000729
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000730namespace {
731class ASTDeclContextNameLookupTrait {
732 ASTReader &Reader;
733
734public:
735 /// \brief Pair of begin/end iterators for DeclIDs.
736 typedef std::pair<DeclID *, DeclID *> data_type;
737
738 /// \brief Special internal key for declaration names.
739 /// The hash table creates keys for comparison; we do not create
740 /// a DeclarationName for the internal key to avoid deserializing types.
741 struct DeclNameKey {
742 DeclarationName::NameKind Kind;
743 uint64_t Data;
744 DeclNameKey() : Kind((DeclarationName::NameKind)0), Data(0) { }
745 };
746
747 typedef DeclarationName external_key_type;
748 typedef DeclNameKey internal_key_type;
749
750 explicit ASTDeclContextNameLookupTrait(ASTReader &Reader) : Reader(Reader) { }
751
752 static bool EqualKey(const internal_key_type& a,
753 const internal_key_type& b) {
754 return a.Kind == b.Kind && a.Data == b.Data;
755 }
756
757 unsigned ComputeHash(const DeclNameKey &Key) const {
758 llvm::FoldingSetNodeID ID;
759 ID.AddInteger(Key.Kind);
760
761 switch (Key.Kind) {
762 case DeclarationName::Identifier:
763 case DeclarationName::CXXLiteralOperatorName:
764 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
765 break;
766 case DeclarationName::ObjCZeroArgSelector:
767 case DeclarationName::ObjCOneArgSelector:
768 case DeclarationName::ObjCMultiArgSelector:
769 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
770 break;
771 case DeclarationName::CXXConstructorName:
772 case DeclarationName::CXXDestructorName:
773 case DeclarationName::CXXConversionFunctionName:
774 ID.AddInteger((TypeID)Key.Data);
775 break;
776 case DeclarationName::CXXOperatorName:
777 ID.AddInteger((OverloadedOperatorKind)Key.Data);
778 break;
779 case DeclarationName::CXXUsingDirective:
780 break;
781 }
782
783 return ID.ComputeHash();
784 }
785
786 internal_key_type GetInternalKey(const external_key_type& Name) const {
787 DeclNameKey Key;
788 Key.Kind = Name.getNameKind();
789 switch (Name.getNameKind()) {
790 case DeclarationName::Identifier:
791 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
792 break;
793 case DeclarationName::ObjCZeroArgSelector:
794 case DeclarationName::ObjCOneArgSelector:
795 case DeclarationName::ObjCMultiArgSelector:
796 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
797 break;
798 case DeclarationName::CXXConstructorName:
799 case DeclarationName::CXXDestructorName:
800 case DeclarationName::CXXConversionFunctionName:
801 Key.Data = Reader.GetTypeID(Name.getCXXNameType());
802 break;
803 case DeclarationName::CXXOperatorName:
804 Key.Data = Name.getCXXOverloadedOperator();
805 break;
806 case DeclarationName::CXXLiteralOperatorName:
807 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
808 break;
809 case DeclarationName::CXXUsingDirective:
810 break;
811 }
Michael J. Spencer20249a12010-10-21 03:16:25 +0000812
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000813 return Key;
814 }
815
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +0000816 external_key_type GetExternalKey(const internal_key_type& Key) const {
817 ASTContext *Context = Reader.getContext();
818 switch (Key.Kind) {
819 case DeclarationName::Identifier:
820 return DeclarationName((IdentifierInfo*)Key.Data);
821
822 case DeclarationName::ObjCZeroArgSelector:
823 case DeclarationName::ObjCOneArgSelector:
824 case DeclarationName::ObjCMultiArgSelector:
825 return DeclarationName(Selector(Key.Data));
826
827 case DeclarationName::CXXConstructorName:
828 return Context->DeclarationNames.getCXXConstructorName(
829 Context->getCanonicalType(Reader.GetType(Key.Data)));
830
831 case DeclarationName::CXXDestructorName:
832 return Context->DeclarationNames.getCXXDestructorName(
833 Context->getCanonicalType(Reader.GetType(Key.Data)));
834
835 case DeclarationName::CXXConversionFunctionName:
836 return Context->DeclarationNames.getCXXConversionFunctionName(
837 Context->getCanonicalType(Reader.GetType(Key.Data)));
838
839 case DeclarationName::CXXOperatorName:
840 return Context->DeclarationNames.getCXXOperatorName(
841 (OverloadedOperatorKind)Key.Data);
842
843 case DeclarationName::CXXLiteralOperatorName:
844 return Context->DeclarationNames.getCXXLiteralOperatorName(
845 (IdentifierInfo*)Key.Data);
846
847 case DeclarationName::CXXUsingDirective:
848 return DeclarationName::getUsingDirectiveName();
849 }
850
851 llvm_unreachable("Invalid Name Kind ?");
852 }
853
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000854 static std::pair<unsigned, unsigned>
855 ReadKeyDataLength(const unsigned char*& d) {
856 using namespace clang::io;
857 unsigned KeyLen = ReadUnalignedLE16(d);
858 unsigned DataLen = ReadUnalignedLE16(d);
859 return std::make_pair(KeyLen, DataLen);
860 }
861
862 internal_key_type ReadKey(const unsigned char* d, unsigned) {
863 using namespace clang::io;
864
865 DeclNameKey Key;
866 Key.Kind = (DeclarationName::NameKind)*d++;
867 switch (Key.Kind) {
868 case DeclarationName::Identifier:
869 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
870 break;
871 case DeclarationName::ObjCZeroArgSelector:
872 case DeclarationName::ObjCOneArgSelector:
873 case DeclarationName::ObjCMultiArgSelector:
Michael J. Spencer20249a12010-10-21 03:16:25 +0000874 Key.Data =
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000875 (uint64_t)Reader.DecodeSelector(ReadUnalignedLE32(d)).getAsOpaquePtr();
876 break;
877 case DeclarationName::CXXConstructorName:
878 case DeclarationName::CXXDestructorName:
879 case DeclarationName::CXXConversionFunctionName:
880 Key.Data = ReadUnalignedLE32(d); // TypeID
881 break;
882 case DeclarationName::CXXOperatorName:
883 Key.Data = *d++; // OverloadedOperatorKind
884 break;
885 case DeclarationName::CXXLiteralOperatorName:
886 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
887 break;
888 case DeclarationName::CXXUsingDirective:
889 break;
890 }
Michael J. Spencer20249a12010-10-21 03:16:25 +0000891
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000892 return Key;
893 }
894
895 data_type ReadData(internal_key_type, const unsigned char* d,
896 unsigned DataLen) {
897 using namespace clang::io;
898 unsigned NumDecls = ReadUnalignedLE16(d);
899 DeclID *Start = (DeclID *)d;
900 return std::make_pair(Start, Start + NumDecls);
901 }
902};
903
904} // end anonymous namespace
905
906/// \brief The on-disk hash table used for the DeclContext's Name lookup table.
907typedef OnDiskChainedHashTable<ASTDeclContextNameLookupTrait>
908 ASTDeclContextNameLookupTable;
909
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000910bool ASTReader::ReadDeclContextStorage(llvm::BitstreamCursor &Cursor,
911 const std::pair<uint64_t, uint64_t> &Offsets,
912 DeclContextInfo &Info) {
913 SavedStreamPosition SavedPosition(Cursor);
914 // First the lexical decls.
915 if (Offsets.first != 0) {
916 Cursor.JumpToBit(Offsets.first);
917
918 RecordData Record;
919 const char *Blob;
920 unsigned BlobLen;
921 unsigned Code = Cursor.ReadCode();
922 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
923 if (RecCode != DECL_CONTEXT_LEXICAL) {
924 Error("Expected lexical block");
925 return true;
926 }
927
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000928 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob);
929 Info.NumLexicalDecls = BlobLen / sizeof(KindDeclIDPair);
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000930 } else {
931 Info.LexicalDecls = 0;
932 Info.NumLexicalDecls = 0;
933 }
934
935 // Now the lookup table.
936 if (Offsets.second != 0) {
937 Cursor.JumpToBit(Offsets.second);
938
939 RecordData Record;
940 const char *Blob;
941 unsigned BlobLen;
942 unsigned Code = Cursor.ReadCode();
943 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
944 if (RecCode != DECL_CONTEXT_VISIBLE) {
945 Error("Expected visible lookup table block");
946 return true;
947 }
948 Info.NameLookupTableData
949 = ASTDeclContextNameLookupTable::Create(
950 (const unsigned char *)Blob + Record[0],
951 (const unsigned char *)Blob,
952 ASTDeclContextNameLookupTrait(*this));
Sebastian Redl0ea8f7f2010-08-24 00:50:00 +0000953 } else {
954 Info.NameLookupTableData = 0;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000955 }
956
957 return false;
958}
959
Argyrios Kyrtzidis8d8f2c22011-04-25 22:23:56 +0000960void ASTReader::Error(llvm::StringRef Msg) {
961 Error(diag::err_fe_pch_malformed, Msg);
962}
963
964void ASTReader::Error(unsigned DiagID,
965 llvm::StringRef Arg1, llvm::StringRef Arg2) {
966 if (Diags.isDiagnosticInFlight())
967 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
968 else
969 Diag(DiagID) << Arg1 << Arg2;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000970}
971
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000972/// \brief Tell the AST listener about the predefines buffers in the chain.
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000973bool ASTReader::CheckPredefinesBuffers() {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000974 if (Listener)
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000975 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000976 ActualOriginalFileName,
Nick Lewycky277a6e72011-02-23 21:16:44 +0000977 SuggestedPredefines,
978 FileMgr);
Douglas Gregore721f952009-04-28 18:58:38 +0000979 return false;
Douglas Gregore1d918e2009-04-10 23:10:45 +0000980}
981
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000982//===----------------------------------------------------------------------===//
983// Source Manager Deserialization
984//===----------------------------------------------------------------------===//
985
Douglas Gregorbd945002009-04-13 16:31:14 +0000986/// \brief Read the line table in the source manager block.
Sebastian Redlc3632732010-10-05 15:59:54 +0000987/// \returns true if there was an error.
988bool ASTReader::ParseLineTable(PerFileData &F,
989 llvm::SmallVectorImpl<uint64_t> &Record) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000990 unsigned Idx = 0;
991 LineTableInfo &LineTable = SourceMgr.getLineTable();
992
993 // Parse the file names
Douglas Gregorff0a9872009-04-13 17:12:42 +0000994 std::map<int, int> FileIDs;
995 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000996 // Extract the file name
997 unsigned FilenameLen = Record[Idx++];
998 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
999 Idx += FilenameLen;
Douglas Gregore650c8c2009-07-07 00:12:59 +00001000 MaybeAddSystemRootToFilename(Filename);
Mike Stump1eb44332009-09-09 15:08:12 +00001001 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
Douglas Gregorff0a9872009-04-13 17:12:42 +00001002 Filename.size());
Douglas Gregorbd945002009-04-13 16:31:14 +00001003 }
1004
1005 // Parse the line entries
1006 std::vector<LineEntry> Entries;
1007 while (Idx < Record.size()) {
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +00001008 int FID = Record[Idx++];
Douglas Gregorbd945002009-04-13 16:31:14 +00001009
1010 // Extract the line entries
1011 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +00001012 assert(NumEntries && "Numentries is 00000");
Douglas Gregorbd945002009-04-13 16:31:14 +00001013 Entries.clear();
1014 Entries.reserve(NumEntries);
1015 for (unsigned I = 0; I != NumEntries; ++I) {
1016 unsigned FileOffset = Record[Idx++];
1017 unsigned LineNo = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +00001018 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump1eb44332009-09-09 15:08:12 +00001019 SrcMgr::CharacteristicKind FileKind
Douglas Gregorbd945002009-04-13 16:31:14 +00001020 = (SrcMgr::CharacteristicKind)Record[Idx++];
1021 unsigned IncludeOffset = Record[Idx++];
1022 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1023 FileKind, IncludeOffset));
1024 }
1025 LineTable.AddEntry(FID, Entries);
1026 }
1027
1028 return false;
1029}
1030
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001031namespace {
1032
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001033class ASTStatData {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001034public:
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001035 const ino_t ino;
1036 const dev_t dev;
1037 const mode_t mode;
1038 const time_t mtime;
1039 const off_t size;
Mike Stump1eb44332009-09-09 15:08:12 +00001040
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001041 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Chris Lattner74e976b2010-11-23 19:28:12 +00001042 : ino(i), dev(d), mode(mo), mtime(m), size(s) {}
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001043};
1044
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001045class ASTStatLookupTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001046 public:
1047 typedef const char *external_key_type;
1048 typedef const char *internal_key_type;
1049
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001050 typedef ASTStatData data_type;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001051
1052 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001053 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001054 }
1055
1056 static internal_key_type GetInternalKey(const char *path) { return path; }
1057
1058 static bool EqualKey(internal_key_type a, internal_key_type b) {
1059 return strcmp(a, b) == 0;
1060 }
1061
1062 static std::pair<unsigned, unsigned>
1063 ReadKeyDataLength(const unsigned char*& d) {
1064 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1065 unsigned DataLen = (unsigned) *d++;
1066 return std::make_pair(KeyLen + 1, DataLen);
1067 }
1068
1069 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
1070 return (const char *)d;
1071 }
1072
1073 static data_type ReadData(const internal_key_type, const unsigned char *d,
1074 unsigned /*DataLen*/) {
1075 using namespace clang::io;
1076
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001077 ino_t ino = (ino_t) ReadUnalignedLE32(d);
1078 dev_t dev = (dev_t) ReadUnalignedLE32(d);
1079 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump1eb44332009-09-09 15:08:12 +00001080 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001081 off_t size = (off_t) ReadUnalignedLE64(d);
1082 return data_type(ino, dev, mode, mtime, size);
1083 }
1084};
1085
1086/// \brief stat() cache for precompiled headers.
1087///
1088/// This cache is very similar to the stat cache used by pretokenized
1089/// headers.
Chris Lattner10e286a2010-11-23 19:19:34 +00001090class ASTStatCache : public FileSystemStatCache {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001091 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001092 CacheTy *Cache;
1093
1094 unsigned &NumStatHits, &NumStatMisses;
Mike Stump1eb44332009-09-09 15:08:12 +00001095public:
Chris Lattner74e976b2010-11-23 19:28:12 +00001096 ASTStatCache(const unsigned char *Buckets, const unsigned char *Base,
1097 unsigned &NumStatHits, unsigned &NumStatMisses)
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001098 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
1099 Cache = CacheTy::Create(Buckets, Base);
1100 }
1101
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001102 ~ASTStatCache() { delete Cache; }
Mike Stump1eb44332009-09-09 15:08:12 +00001103
Chris Lattner898a0612010-11-23 21:17:56 +00001104 LookupResult getStat(const char *Path, struct stat &StatBuf,
1105 int *FileDescriptor) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001106 // Do the lookup for the file's data in the AST file.
Chris Lattner10e286a2010-11-23 19:19:34 +00001107 CacheTy::iterator I = Cache->find(Path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001108
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001109 // If we don't get a hit in the AST file just forward to 'stat'.
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001110 if (I == Cache->end()) {
1111 ++NumStatMisses;
Chris Lattner898a0612010-11-23 21:17:56 +00001112 return statChained(Path, StatBuf, FileDescriptor);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001113 }
Mike Stump1eb44332009-09-09 15:08:12 +00001114
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001115 ++NumStatHits;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001116 ASTStatData Data = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001117
Chris Lattner10e286a2010-11-23 19:19:34 +00001118 StatBuf.st_ino = Data.ino;
1119 StatBuf.st_dev = Data.dev;
1120 StatBuf.st_mtime = Data.mtime;
1121 StatBuf.st_mode = Data.mode;
1122 StatBuf.st_size = Data.size;
Chris Lattnerd6f61112010-11-23 20:05:15 +00001123 return CacheExists;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001124 }
1125};
1126} // end anonymous namespace
1127
1128
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001129/// \brief Read a source manager block
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001130ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(PerFileData &F) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001131 using namespace SrcMgr;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001132
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001133 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001134
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001135 // Set the source-location entry cursor to the current position in
1136 // the stream. This cursor will be used to read the contents of the
1137 // source manager block initially, and then lazily read
1138 // source-location entries as needed.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001139 SLocEntryCursor = F.Stream;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001140
1141 // The stream itself is going to skip over the source manager block.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001142 if (F.Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001143 Error("malformed block record in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001144 return Failure;
1145 }
1146
1147 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001148 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001149 Error("malformed source manager block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001150 return Failure;
1151 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001152
Douglas Gregor14f79002009-04-10 03:52:48 +00001153 RecordData Record;
1154 while (true) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001155 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregor14f79002009-04-10 03:52:48 +00001156 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001157 if (SLocEntryCursor.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001158 Error("error at end of Source Manager block in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001159 return Failure;
1160 }
Douglas Gregore1d918e2009-04-10 23:10:45 +00001161 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +00001162 }
Mike Stump1eb44332009-09-09 15:08:12 +00001163
Douglas Gregor14f79002009-04-10 03:52:48 +00001164 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1165 // No known subblocks, always skip them.
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001166 SLocEntryCursor.ReadSubBlockID();
1167 if (SLocEntryCursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001168 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001169 return Failure;
1170 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001171 continue;
1172 }
Mike Stump1eb44332009-09-09 15:08:12 +00001173
Douglas Gregor14f79002009-04-10 03:52:48 +00001174 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001175 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregor14f79002009-04-10 03:52:48 +00001176 continue;
1177 }
Mike Stump1eb44332009-09-09 15:08:12 +00001178
Douglas Gregor14f79002009-04-10 03:52:48 +00001179 // Read a record.
1180 const char *BlobStart;
1181 unsigned BlobLen;
1182 Record.clear();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001183 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001184 default: // Default behavior: ignore.
1185 break;
1186
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001187 case SM_LINE_TABLE:
Sebastian Redlc3632732010-10-05 15:59:54 +00001188 if (ParseLineTable(F, Record))
Douglas Gregorbd945002009-04-13 16:31:14 +00001189 return Failure;
Chris Lattner2c78b872009-04-14 23:22:57 +00001190 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001191
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001192 case SM_SLOC_FILE_ENTRY:
1193 case SM_SLOC_BUFFER_ENTRY:
1194 case SM_SLOC_INSTANTIATION_ENTRY:
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001195 // Once we hit one of the source location entries, we're done.
1196 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +00001197 }
1198 }
1199}
1200
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001201/// \brief If a header file is not found at the path that we expect it to be
1202/// and the PCH file was moved from its original location, try to resolve the
1203/// file by assuming that header+PCH were moved together and the header is in
1204/// the same place relative to the PCH.
1205static std::string
1206resolveFileRelativeToOriginalDir(const std::string &Filename,
1207 const std::string &OriginalDir,
1208 const std::string &CurrDir) {
1209 assert(OriginalDir != CurrDir &&
1210 "No point trying to resolve the file if the PCH dir didn't change");
1211 using namespace llvm::sys;
1212 llvm::SmallString<128> filePath(Filename);
1213 fs::make_absolute(filePath);
1214 assert(path::is_absolute(OriginalDir));
1215 llvm::SmallString<128> currPCHPath(CurrDir);
1216
1217 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1218 fileDirE = path::end(path::parent_path(filePath));
1219 path::const_iterator origDirI = path::begin(OriginalDir),
1220 origDirE = path::end(OriginalDir);
1221 // Skip the common path components from filePath and OriginalDir.
1222 while (fileDirI != fileDirE && origDirI != origDirE &&
1223 *fileDirI == *origDirI) {
1224 ++fileDirI;
1225 ++origDirI;
1226 }
1227 for (; origDirI != origDirE; ++origDirI)
1228 path::append(currPCHPath, "..");
1229 path::append(currPCHPath, fileDirI, fileDirE);
1230 path::append(currPCHPath, path::filename(Filename));
1231 return currPCHPath.str();
1232}
1233
Sebastian Redl190faf72010-07-20 21:50:20 +00001234/// \brief Get a cursor that's correctly positioned for reading the source
1235/// location entry with the given ID.
Sebastian Redlc3632732010-10-05 15:59:54 +00001236ASTReader::PerFileData *ASTReader::SLocCursorForID(unsigned ID) {
Sebastian Redl190faf72010-07-20 21:50:20 +00001237 assert(ID != 0 && ID <= TotalNumSLocEntries &&
1238 "SLocCursorForID should only be called for real IDs.");
1239
1240 ID -= 1;
1241 PerFileData *F = 0;
1242 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1243 F = Chain[N - I - 1];
1244 if (ID < F->LocalNumSLocEntries)
1245 break;
1246 ID -= F->LocalNumSLocEntries;
1247 }
1248 assert(F && F->LocalNumSLocEntries > ID && "Chain corrupted");
1249
1250 F->SLocEntryCursor.JumpToBit(F->SLocOffsets[ID]);
Sebastian Redlc3632732010-10-05 15:59:54 +00001251 return F;
Sebastian Redl190faf72010-07-20 21:50:20 +00001252}
1253
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001254/// \brief Read in the source location entry with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001255ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(unsigned ID) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001256 if (ID == 0)
1257 return Success;
1258
1259 if (ID > TotalNumSLocEntries) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001260 Error("source location entry ID out-of-range for AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001261 return Failure;
1262 }
1263
Sebastian Redlc3632732010-10-05 15:59:54 +00001264 PerFileData *F = SLocCursorForID(ID);
1265 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001266
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001267 ++NumSLocEntriesRead;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001268 unsigned Code = SLocEntryCursor.ReadCode();
1269 if (Code == llvm::bitc::END_BLOCK ||
1270 Code == llvm::bitc::ENTER_SUBBLOCK ||
1271 Code == llvm::bitc::DEFINE_ABBREV) {
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 }
1275
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001276 RecordData Record;
1277 const char *BlobStart;
1278 unsigned BlobLen;
1279 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1280 default:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001281 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001282 return Failure;
1283
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001284 case SM_SLOC_FILE_ENTRY: {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001285 std::string Filename(BlobStart, BlobStart + BlobLen);
1286 MaybeAddSystemRootToFilename(Filename);
Chris Lattner39b49bc2010-11-23 08:35:12 +00001287 const FileEntry *File = FileMgr.getFile(Filename);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001288 if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() &&
1289 OriginalDir != CurrentDir) {
1290 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1291 OriginalDir,
1292 CurrentDir);
1293 if (!resolved.empty())
1294 File = FileMgr.getFile(resolved);
1295 }
Axel Naumann04331162011-01-27 10:55:51 +00001296 if (File == 0)
1297 File = FileMgr.getVirtualFile(Filename, (off_t)Record[4],
1298 (time_t)Record[5]);
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001299 if (File == 0) {
1300 std::string ErrorStr = "could not find file '";
Douglas Gregore650c8c2009-07-07 00:12:59 +00001301 ErrorStr += Filename;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001302 ErrorStr += "' referenced by AST file";
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001303 Error(ErrorStr.c_str());
1304 return Failure;
1305 }
Mike Stump1eb44332009-09-09 15:08:12 +00001306
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001307 if (Record.size() < 6) {
Ted Kremenek1857f622010-03-18 21:23:05 +00001308 Error("source location entry is incorrect");
1309 return Failure;
1310 }
1311
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001312 if (!DisableValidation &&
1313 ((off_t)Record[4] != File->getSize()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001314#if !defined(LLVM_ON_WIN32)
1315 // In our regression testing, the Windows file system seems to
1316 // have inconsistent modification times that sometimes
1317 // erroneously trigger this error-handling path.
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001318 || (time_t)Record[5] != File->getModificationTime()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001319#endif
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001320 )) {
Argyrios Kyrtzidis8d8f2c22011-04-25 22:23:56 +00001321 Error(diag::err_fe_pch_file_modified, Filename);
Douglas Gregor2d52be52010-03-21 22:49:54 +00001322 return Failure;
1323 }
1324
Chris Lattner75dfb652010-11-23 09:19:42 +00001325 FileID FID = SourceMgr.createFileID(File, ReadSourceLocation(*F, Record[1]),
1326 (SrcMgr::CharacteristicKind)Record[2],
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001327 ID, Record[0]);
1328 if (Record[3])
1329 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile())
1330 .setHasLineDirectives();
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001331
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001332 break;
1333 }
1334
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001335 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001336 const char *Name = BlobStart;
1337 unsigned Offset = Record[0];
1338 unsigned Code = SLocEntryCursor.ReadCode();
1339 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001340 unsigned RecCode
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001341 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001342
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001343 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001344 Error("AST record has invalid code");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001345 return Failure;
1346 }
1347
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001348 llvm::MemoryBuffer *Buffer
Chris Lattnera0a270c2010-04-05 22:42:27 +00001349 = llvm::MemoryBuffer::getMemBuffer(llvm::StringRef(BlobStart, BlobLen - 1),
1350 Name);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001351 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID, Offset);
Mike Stump1eb44332009-09-09 15:08:12 +00001352
Douglas Gregor92b059e2009-04-28 20:33:11 +00001353 if (strcmp(Name, "<built-in>") == 0) {
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +00001354 PCHPredefinesBlock Block = {
1355 BufferID,
1356 llvm::StringRef(BlobStart, BlobLen - 1)
1357 };
1358 PCHPredefinesBuffers.push_back(Block);
Douglas Gregor92b059e2009-04-28 20:33:11 +00001359 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001360
1361 break;
1362 }
1363
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001364 case SM_SLOC_INSTANTIATION_ENTRY: {
Sebastian Redlc3632732010-10-05 15:59:54 +00001365 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001366 SourceMgr.createInstantiationLoc(SpellingLoc,
Sebastian Redlc3632732010-10-05 15:59:54 +00001367 ReadSourceLocation(*F, Record[2]),
1368 ReadSourceLocation(*F, Record[3]),
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001369 Record[4],
1370 ID,
1371 Record[0]);
1372 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001373 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001374 }
1375
1376 return Success;
1377}
1378
Chris Lattner6367f6d2009-04-27 01:05:14 +00001379/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1380/// specified cursor. Read the abbreviations that are at the top of the block
1381/// and then leave the cursor pointing into the block.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001382bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattner6367f6d2009-04-27 01:05:14 +00001383 unsigned BlockID) {
1384 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001385 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001386 return Failure;
1387 }
Mike Stump1eb44332009-09-09 15:08:12 +00001388
Chris Lattner6367f6d2009-04-27 01:05:14 +00001389 while (true) {
Douglas Gregorecdcb882010-10-20 22:00:55 +00001390 uint64_t Offset = Cursor.GetCurrentBitNo();
Chris Lattner6367f6d2009-04-27 01:05:14 +00001391 unsigned Code = Cursor.ReadCode();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001392
Chris Lattner6367f6d2009-04-27 01:05:14 +00001393 // We expect all abbrevs to be at the start of the block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001394 if (Code != llvm::bitc::DEFINE_ABBREV) {
1395 Cursor.JumpToBit(Offset);
Chris Lattner6367f6d2009-04-27 01:05:14 +00001396 return false;
Douglas Gregorecdcb882010-10-20 22:00:55 +00001397 }
Chris Lattner6367f6d2009-04-27 01:05:14 +00001398 Cursor.ReadAbbrevRecord();
1399 }
1400}
1401
Douglas Gregor89d99802010-11-30 06:16:57 +00001402PreprocessedEntity *ASTReader::ReadMacroRecord(PerFileData &F, uint64_t Offset) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001403 assert(PP && "Forgot to set Preprocessor ?");
Douglas Gregorecdcb882010-10-20 22:00:55 +00001404 llvm::BitstreamCursor &Stream = F.MacroCursor;
Mike Stump1eb44332009-09-09 15:08:12 +00001405
Douglas Gregor37e26842009-04-21 23:56:24 +00001406 // Keep track of where we are in the stream, then jump back there
1407 // after reading this macro.
1408 SavedStreamPosition SavedPosition(Stream);
1409
1410 Stream.JumpToBit(Offset);
1411 RecordData Record;
1412 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1413 MacroInfo *Macro = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001414
Douglas Gregor37e26842009-04-21 23:56:24 +00001415 while (true) {
1416 unsigned Code = Stream.ReadCode();
1417 switch (Code) {
1418 case llvm::bitc::END_BLOCK:
Douglas Gregor89d99802010-11-30 06:16:57 +00001419 return 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001420
1421 case llvm::bitc::ENTER_SUBBLOCK:
1422 // No known subblocks, always skip them.
1423 Stream.ReadSubBlockID();
1424 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001425 Error("malformed block record in AST file");
Douglas Gregor89d99802010-11-30 06:16:57 +00001426 return 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001427 }
1428 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001429
Douglas Gregor37e26842009-04-21 23:56:24 +00001430 case llvm::bitc::DEFINE_ABBREV:
1431 Stream.ReadAbbrevRecord();
1432 continue;
1433 default: break;
1434 }
1435
1436 // Read a record.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001437 const char *BlobStart = 0;
1438 unsigned BlobLen = 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001439 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001440 PreprocessorRecordTypes RecType =
Michael J. Spencer20249a12010-10-21 03:16:25 +00001441 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart,
Douglas Gregorecdcb882010-10-20 22:00:55 +00001442 BlobLen);
Douglas Gregor37e26842009-04-21 23:56:24 +00001443 switch (RecType) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001444 case PP_MACRO_OBJECT_LIKE:
1445 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001446 // If we already have a macro, that means that we've hit the end
1447 // of the definition of the macro we were looking for. We're
1448 // done.
1449 if (Macro)
Douglas Gregor89d99802010-11-30 06:16:57 +00001450 return 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001451
1452 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1453 if (II == 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001454 Error("macro must have a name in AST file");
Douglas Gregor89d99802010-11-30 06:16:57 +00001455 return 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001456 }
Sebastian Redlc3632732010-10-05 15:59:54 +00001457 SourceLocation Loc = ReadSourceLocation(F, Record[1]);
Douglas Gregor37e26842009-04-21 23:56:24 +00001458 bool isUsed = Record[2];
Mike Stump1eb44332009-09-09 15:08:12 +00001459
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001460 MacroInfo *MI = PP->AllocateMacroInfo(Loc);
Douglas Gregor37e26842009-04-21 23:56:24 +00001461 MI->setIsUsed(isUsed);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001462 MI->setIsFromAST();
Mike Stump1eb44332009-09-09 15:08:12 +00001463
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001464 unsigned NextIndex = 3;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001465 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregor37e26842009-04-21 23:56:24 +00001466 // Decode function-like macro info.
1467 bool isC99VarArgs = Record[3];
1468 bool isGNUVarArgs = Record[4];
1469 MacroArgs.clear();
1470 unsigned NumArgs = Record[5];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001471 NextIndex = 6 + NumArgs;
Douglas Gregor37e26842009-04-21 23:56:24 +00001472 for (unsigned i = 0; i != NumArgs; ++i)
1473 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1474
1475 // Install function-like macro info.
1476 MI->setIsFunctionLike();
1477 if (isC99VarArgs) MI->setIsC99Varargs();
1478 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor75fdb232009-05-22 22:45:36 +00001479 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001480 PP->getPreprocessorAllocator());
Douglas Gregor37e26842009-04-21 23:56:24 +00001481 }
1482
1483 // Finally, install the macro.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001484 PP->setMacroInfo(II, MI);
Douglas Gregor37e26842009-04-21 23:56:24 +00001485
1486 // Remember that we saw this macro last so that we add the tokens that
1487 // form its body to it.
1488 Macro = MI;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001489
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001490 if (NextIndex + 1 == Record.size() && PP->getPreprocessingRecord()) {
1491 // We have a macro definition. Load it now.
1492 PP->getPreprocessingRecord()->RegisterMacroDefinition(Macro,
1493 getMacroDefinition(Record[NextIndex]));
1494 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001495
Douglas Gregor37e26842009-04-21 23:56:24 +00001496 ++NumMacrosRead;
1497 break;
1498 }
Mike Stump1eb44332009-09-09 15:08:12 +00001499
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001500 case PP_TOKEN: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001501 // If we see a TOKEN before a PP_MACRO_*, then the file is
1502 // erroneous, just pretend we didn't see this.
1503 if (Macro == 0) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001504
Douglas Gregor37e26842009-04-21 23:56:24 +00001505 Token Tok;
1506 Tok.startToken();
Sebastian Redlc3632732010-10-05 15:59:54 +00001507 Tok.setLocation(ReadSourceLocation(F, Record[0]));
Douglas Gregor37e26842009-04-21 23:56:24 +00001508 Tok.setLength(Record[1]);
1509 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1510 Tok.setIdentifierInfo(II);
1511 Tok.setKind((tok::TokenKind)Record[3]);
1512 Tok.setFlag((Token::TokenFlags)Record[4]);
1513 Macro->AddTokenToBody(Tok);
1514 break;
1515 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001516 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001517 }
Douglas Gregor89d99802010-11-30 06:16:57 +00001518
1519 return 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001520}
1521
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001522PreprocessedEntity *ASTReader::LoadPreprocessedEntity(PerFileData &F) {
1523 assert(PP && "Forgot to set Preprocessor ?");
1524 unsigned Code = F.PreprocessorDetailCursor.ReadCode();
1525 switch (Code) {
1526 case llvm::bitc::END_BLOCK:
1527 return 0;
1528
1529 case llvm::bitc::ENTER_SUBBLOCK:
1530 Error("unexpected subblock record in preprocessor detail block");
1531 return 0;
1532
1533 case llvm::bitc::DEFINE_ABBREV:
1534 Error("unexpected abbrevation record in preprocessor detail block");
1535 return 0;
1536
1537 default:
1538 break;
1539 }
1540
1541 if (!PP->getPreprocessingRecord()) {
1542 Error("no preprocessing record");
1543 return 0;
1544 }
1545
1546 // Read the record.
1547 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1548 const char *BlobStart = 0;
1549 unsigned BlobLen = 0;
1550 RecordData Record;
1551 PreprocessorDetailRecordTypes RecType =
1552 (PreprocessorDetailRecordTypes)F.PreprocessorDetailCursor.ReadRecord(
1553 Code, Record, BlobStart, BlobLen);
1554 switch (RecType) {
1555 case PPD_MACRO_INSTANTIATION: {
1556 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1557 return PE;
1558
1559 MacroInstantiation *MI
1560 = new (PPRec) MacroInstantiation(DecodeIdentifierInfo(Record[3]),
1561 SourceRange(ReadSourceLocation(F, Record[1]),
1562 ReadSourceLocation(F, Record[2])),
1563 getMacroDefinition(Record[4]));
1564 PPRec.SetPreallocatedEntity(Record[0], MI);
1565 return MI;
1566 }
1567
1568 case PPD_MACRO_DEFINITION: {
1569 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1570 return PE;
1571
1572 if (Record[1] > MacroDefinitionsLoaded.size()) {
1573 Error("out-of-bounds macro definition record");
1574 return 0;
1575 }
1576
1577 // Decode the identifier info and then check again; if the macro is
1578 // still defined and associated with the identifier,
1579 IdentifierInfo *II = DecodeIdentifierInfo(Record[4]);
1580 if (!MacroDefinitionsLoaded[Record[1] - 1]) {
1581 MacroDefinition *MD
1582 = new (PPRec) MacroDefinition(II,
1583 ReadSourceLocation(F, Record[5]),
1584 SourceRange(
1585 ReadSourceLocation(F, Record[2]),
1586 ReadSourceLocation(F, Record[3])));
1587
1588 PPRec.SetPreallocatedEntity(Record[0], MD);
1589 MacroDefinitionsLoaded[Record[1] - 1] = MD;
1590
1591 if (DeserializationListener)
1592 DeserializationListener->MacroDefinitionRead(Record[1], MD);
1593 }
1594
1595 return MacroDefinitionsLoaded[Record[1] - 1];
1596 }
1597
1598 case PPD_INCLUSION_DIRECTIVE: {
1599 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1600 return PE;
1601
1602 const char *FullFileNameStart = BlobStart + Record[3];
1603 const FileEntry *File
1604 = PP->getFileManager().getFile(llvm::StringRef(FullFileNameStart,
1605 BlobLen - Record[3]));
1606
1607 // FIXME: Stable encoding
1608 InclusionDirective::InclusionKind Kind
1609 = static_cast<InclusionDirective::InclusionKind>(Record[5]);
1610 InclusionDirective *ID
1611 = new (PPRec) InclusionDirective(PPRec, Kind,
1612 llvm::StringRef(BlobStart, Record[3]),
1613 Record[4],
1614 File,
1615 SourceRange(ReadSourceLocation(F, Record[1]),
1616 ReadSourceLocation(F, Record[2])));
1617 PPRec.SetPreallocatedEntity(Record[0], ID);
1618 return ID;
1619 }
1620 }
1621
1622 Error("invalid offset in preprocessor detail block");
1623 return 0;
1624}
1625
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001626namespace {
1627 /// \brief Trait class used to search the on-disk hash table containing all of
1628 /// the header search information.
1629 ///
1630 /// The on-disk hash table contains a mapping from each header path to
1631 /// information about that header (how many times it has been included, its
1632 /// controlling macro, etc.). Note that we actually hash based on the
1633 /// filename, and support "deep" comparisons of file names based on current
1634 /// inode numbers, so that the search can cope with non-normalized path names
1635 /// and symlinks.
1636 class HeaderFileInfoTrait {
1637 const char *SearchPath;
1638 struct stat SearchPathStatBuf;
1639 llvm::Optional<int> SearchPathStatResult;
1640
1641 int StatSimpleCache(const char *Path, struct stat *StatBuf) {
1642 if (Path == SearchPath) {
1643 if (!SearchPathStatResult)
1644 SearchPathStatResult = stat(Path, &SearchPathStatBuf);
1645
1646 *StatBuf = SearchPathStatBuf;
1647 return *SearchPathStatResult;
1648 }
1649
1650 return stat(Path, StatBuf);
1651 }
1652
1653 public:
1654 typedef const char *external_key_type;
1655 typedef const char *internal_key_type;
1656
1657 typedef HeaderFileInfo data_type;
1658
1659 HeaderFileInfoTrait(const char *SearchPath = 0) : SearchPath(SearchPath) { }
1660
1661 static unsigned ComputeHash(const char *path) {
1662 return llvm::HashString(llvm::sys::path::filename(path));
1663 }
1664
1665 static internal_key_type GetInternalKey(const char *path) { return path; }
1666
1667 bool EqualKey(internal_key_type a, internal_key_type b) {
1668 if (strcmp(a, b) == 0)
1669 return true;
1670
1671 if (llvm::sys::path::filename(a) != llvm::sys::path::filename(b))
1672 return false;
1673
1674 // The file names match, but the path names don't. stat() the files to
1675 // see if they are the same.
1676 struct stat StatBufA, StatBufB;
1677 if (StatSimpleCache(a, &StatBufA) || StatSimpleCache(b, &StatBufB))
1678 return false;
1679
1680 return StatBufA.st_ino == StatBufB.st_ino;
1681 }
1682
1683 static std::pair<unsigned, unsigned>
1684 ReadKeyDataLength(const unsigned char*& d) {
1685 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1686 unsigned DataLen = (unsigned) *d++;
1687 return std::make_pair(KeyLen + 1, DataLen);
1688 }
1689
1690 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
1691 return (const char *)d;
1692 }
1693
1694 static data_type ReadData(const internal_key_type, const unsigned char *d,
1695 unsigned DataLen) {
1696 const unsigned char *End = d + DataLen;
1697 using namespace clang::io;
1698 HeaderFileInfo HFI;
1699 unsigned Flags = *d++;
Douglas Gregordd3e5542011-05-04 00:14:37 +00001700 HFI.isImport = (Flags >> 4) & 0x01;
1701 HFI.isPragmaOnce = (Flags >> 3) & 0x01;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001702 HFI.DirInfo = (Flags >> 1) & 0x03;
1703 HFI.Resolved = Flags & 0x01;
1704 HFI.NumIncludes = ReadUnalignedLE16(d);
1705 HFI.ControllingMacroID = ReadUnalignedLE32(d);
1706 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1707 (void)End;
1708
1709 // This HeaderFileInfo was externally loaded.
1710 HFI.External = true;
1711 return HFI;
1712 }
1713 };
1714}
1715
1716/// \brief The on-disk hash table used for the global method pool.
1717typedef OnDiskChainedHashTable<HeaderFileInfoTrait>
1718 HeaderFileInfoLookupTable;
1719
Douglas Gregor295a2a62010-10-30 00:23:06 +00001720void ASTReader::SetIdentifierIsMacro(IdentifierInfo *II, PerFileData &F,
1721 uint64_t Offset) {
1722 // Note that this identifier has a macro definition.
1723 II->setHasMacroDefinition(true);
1724
1725 // Adjust the offset based on our position in the chain.
1726 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1727 if (Chain[I] == &F)
1728 break;
1729
1730 Offset += Chain[I]->SizeInBits;
1731 }
1732
1733 UnreadMacroRecordOffsets[II] = Offset;
1734}
1735
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001736void ASTReader::ReadDefinedMacros() {
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001737 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redlc3632732010-10-05 15:59:54 +00001738 PerFileData &F = *Chain[N - I - 1];
1739 llvm::BitstreamCursor &MacroCursor = F.MacroCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001740
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001741 // If there was no preprocessor block, skip this file.
1742 if (!MacroCursor.getBitStreamReader())
1743 continue;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001744
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001745 llvm::BitstreamCursor Cursor = MacroCursor;
Douglas Gregorecdcb882010-10-20 22:00:55 +00001746 Cursor.JumpToBit(F.MacroStartOffset);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001747
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001748 RecordData Record;
1749 while (true) {
1750 unsigned Code = Cursor.ReadCode();
Douglas Gregorecdcb882010-10-20 22:00:55 +00001751 if (Code == llvm::bitc::END_BLOCK)
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001752 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001753
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001754 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1755 // No known subblocks, always skip them.
1756 Cursor.ReadSubBlockID();
1757 if (Cursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001758 Error("malformed block record in AST file");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001759 return;
1760 }
1761 continue;
1762 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001763
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001764 if (Code == llvm::bitc::DEFINE_ABBREV) {
1765 Cursor.ReadAbbrevRecord();
1766 continue;
1767 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001768
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001769 // Read a record.
1770 const char *BlobStart;
1771 unsigned BlobLen;
1772 Record.clear();
1773 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1774 default: // Default behavior: ignore.
1775 break;
Douglas Gregor88a35862010-01-04 19:18:44 +00001776
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001777 case PP_MACRO_OBJECT_LIKE:
1778 case PP_MACRO_FUNCTION_LIKE:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001779 DecodeIdentifierInfo(Record[0]);
1780 break;
1781
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001782 case PP_TOKEN:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001783 // Ignore tokens.
1784 break;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001785 }
Douglas Gregor88a35862010-01-04 19:18:44 +00001786 }
1787 }
Douglas Gregor295a2a62010-10-30 00:23:06 +00001788
1789 // Drain the unread macro-record offsets map.
1790 while (!UnreadMacroRecordOffsets.empty())
1791 LoadMacroDefinition(UnreadMacroRecordOffsets.begin());
1792}
1793
1794void ASTReader::LoadMacroDefinition(
1795 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos) {
1796 assert(Pos != UnreadMacroRecordOffsets.end() && "Unknown macro definition");
1797 PerFileData *F = 0;
1798 uint64_t Offset = Pos->second;
1799 UnreadMacroRecordOffsets.erase(Pos);
1800
1801 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1802 if (Offset < Chain[I]->SizeInBits) {
1803 F = Chain[I];
1804 break;
1805 }
1806
1807 Offset -= Chain[I]->SizeInBits;
1808 }
1809 if (!F) {
1810 Error("Malformed macro record offset");
1811 return;
1812 }
1813
1814 ReadMacroRecord(*F, Offset);
1815}
1816
1817void ASTReader::LoadMacroDefinition(IdentifierInfo *II) {
1818 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos
1819 = UnreadMacroRecordOffsets.find(II);
1820 LoadMacroDefinition(Pos);
Douglas Gregor88a35862010-01-04 19:18:44 +00001821}
1822
Sebastian Redlf73c93f2010-09-15 19:54:06 +00001823MacroDefinition *ASTReader::getMacroDefinition(MacroID ID) {
Douglas Gregor77424bc2010-10-02 19:29:26 +00001824 if (ID == 0 || ID > MacroDefinitionsLoaded.size())
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001825 return 0;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001826
Douglas Gregor77424bc2010-10-02 19:29:26 +00001827 if (!MacroDefinitionsLoaded[ID - 1]) {
1828 unsigned Index = ID - 1;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001829 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1830 PerFileData &F = *Chain[N - I - 1];
1831 if (Index < F.LocalNumMacroDefinitions) {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001832 SavedStreamPosition SavedPosition(F.PreprocessorDetailCursor);
1833 F.PreprocessorDetailCursor.JumpToBit(F.MacroDefinitionOffsets[Index]);
1834 LoadPreprocessedEntity(F);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001835 break;
1836 }
1837 Index -= F.LocalNumMacroDefinitions;
1838 }
Douglas Gregor77424bc2010-10-02 19:29:26 +00001839 assert(MacroDefinitionsLoaded[ID - 1] && "Broken chain");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001840 }
1841
Douglas Gregor77424bc2010-10-02 19:29:26 +00001842 return MacroDefinitionsLoaded[ID - 1];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001843}
1844
Douglas Gregore650c8c2009-07-07 00:12:59 +00001845/// \brief If we are loading a relocatable PCH file, and the filename is
1846/// not an absolute path, add the system root to the beginning of the file
1847/// name.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001848void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001849 // If this is not a relocatable PCH file, there's nothing to do.
1850 if (!RelocatablePCH)
1851 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001852
Michael J. Spencer256053b2010-12-17 21:22:22 +00001853 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
Douglas Gregore650c8c2009-07-07 00:12:59 +00001854 return;
1855
Douglas Gregore650c8c2009-07-07 00:12:59 +00001856 if (isysroot == 0) {
1857 // If no system root was given, default to '/'
1858 Filename.insert(Filename.begin(), '/');
1859 return;
1860 }
Mike Stump1eb44332009-09-09 15:08:12 +00001861
Douglas Gregore650c8c2009-07-07 00:12:59 +00001862 unsigned Length = strlen(isysroot);
1863 if (isysroot[Length - 1] != '/')
1864 Filename.insert(Filename.begin(), '/');
Mike Stump1eb44332009-09-09 15:08:12 +00001865
Douglas Gregore650c8c2009-07-07 00:12:59 +00001866 Filename.insert(Filename.begin(), isysroot, isysroot + Length);
1867}
1868
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001869ASTReader::ASTReadResult
Sebastian Redl571db7f2010-08-18 23:56:56 +00001870ASTReader::ReadASTBlock(PerFileData &F) {
Sebastian Redl9137a522010-07-16 17:50:48 +00001871 llvm::BitstreamCursor &Stream = F.Stream;
1872
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001873 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001874 Error("malformed block record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001875 return Failure;
1876 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001877
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001878 // Read all of the records and blocks for the ASt file.
Douglas Gregor8038d512009-04-10 17:25:41 +00001879 RecordData Record;
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001880 bool First = true;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001881 while (!Stream.AtEndOfStream()) {
1882 unsigned Code = Stream.ReadCode();
1883 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001884 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001885 Error("error at end of module block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001886 return Failure;
1887 }
Chris Lattner7356a312009-04-11 21:15:38 +00001888
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001889 return Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001890 }
1891
1892 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1893 switch (Stream.ReadSubBlockID()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001894 case DECLTYPES_BLOCK_ID:
Chris Lattner6367f6d2009-04-27 01:05:14 +00001895 // We lazily load the decls block, but we want to set up the
1896 // DeclsCursor cursor to point into it. Clone our current bitcode
1897 // cursor to it, enter the block and read the abbrevs in that block.
1898 // With the main cursor, we just skip over it.
Sebastian Redl9137a522010-07-16 17:50:48 +00001899 F.DeclsCursor = Stream;
Chris Lattner6367f6d2009-04-27 01:05:14 +00001900 if (Stream.SkipBlock() || // Skip with the main cursor.
1901 // Read the abbrevs.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001902 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001903 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001904 return Failure;
1905 }
1906 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001907
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00001908 case DECL_UPDATES_BLOCK_ID:
1909 if (Stream.SkipBlock()) {
1910 Error("malformed block record in AST file");
1911 return Failure;
1912 }
1913 break;
1914
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001915 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl9137a522010-07-16 17:50:48 +00001916 F.MacroCursor = Stream;
Douglas Gregor88a35862010-01-04 19:18:44 +00001917 if (PP)
1918 PP->setExternalSource(this);
1919
Douglas Gregorecdcb882010-10-20 22:00:55 +00001920 if (Stream.SkipBlock() ||
1921 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001922 Error("malformed block record in AST file");
Chris Lattner7356a312009-04-11 21:15:38 +00001923 return Failure;
1924 }
Douglas Gregorecdcb882010-10-20 22:00:55 +00001925 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
Chris Lattner7356a312009-04-11 21:15:38 +00001926 break;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00001927
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001928 case PREPROCESSOR_DETAIL_BLOCK_ID:
1929 F.PreprocessorDetailCursor = Stream;
1930 if (Stream.SkipBlock() ||
1931 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
1932 PREPROCESSOR_DETAIL_BLOCK_ID)) {
1933 Error("malformed preprocessor detail record in AST file");
1934 return Failure;
1935 }
1936 F.PreprocessorDetailStartOffset
1937 = F.PreprocessorDetailCursor.GetCurrentBitNo();
1938 break;
1939
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001940 case SOURCE_MANAGER_BLOCK_ID:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001941 switch (ReadSourceManagerBlock(F)) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00001942 case Success:
1943 break;
1944
1945 case Failure:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001946 Error("malformed source manager block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001947 return Failure;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001948
1949 case IgnorePCH:
1950 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001951 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001952 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001953 }
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001954 First = false;
Douglas Gregor8038d512009-04-10 17:25:41 +00001955 continue;
1956 }
1957
1958 if (Code == llvm::bitc::DEFINE_ABBREV) {
1959 Stream.ReadAbbrevRecord();
1960 continue;
1961 }
1962
1963 // Read and process a record.
1964 Record.clear();
Douglas Gregor2bec0412009-04-10 21:16:55 +00001965 const char *BlobStart = 0;
1966 unsigned BlobLen = 0;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001967 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00001968 &BlobStart, &BlobLen)) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001969 default: // Default behavior: ignore.
1970 break;
1971
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001972 case METADATA: {
1973 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1974 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001975 : diag::warn_pch_version_too_new);
1976 return IgnorePCH;
1977 }
1978
1979 RelocatablePCH = Record[4];
1980 if (Listener) {
1981 std::string TargetTriple(BlobStart, BlobLen);
1982 if (Listener->ReadTargetTriple(TargetTriple))
1983 return IgnorePCH;
1984 }
1985 break;
1986 }
1987
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001988 case CHAINED_METADATA: {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001989 if (!First) {
1990 Error("CHAINED_METADATA is not first record in block");
1991 return Failure;
1992 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001993 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1994 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001995 : diag::warn_pch_version_too_new);
1996 return IgnorePCH;
1997 }
1998
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00001999 // Load the chained file, which is always a PCH file.
2000 switch(ReadASTCore(llvm::StringRef(BlobStart, BlobLen), PCH)) {
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002001 case Failure: return Failure;
2002 // If we have to ignore the dependency, we'll have to ignore this too.
2003 case IgnorePCH: return IgnorePCH;
2004 case Success: break;
2005 }
2006 break;
2007 }
2008
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002009 case TYPE_OFFSET:
Sebastian Redl12d6da02010-07-19 22:06:55 +00002010 if (F.LocalNumTypes != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002011 Error("duplicate TYPE_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.TypeOffsets = (const uint32_t *)BlobStart;
2015 F.LocalNumTypes = Record[0];
Douglas Gregor8038d512009-04-10 17:25:41 +00002016 break;
2017
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002018 case DECL_OFFSET:
Sebastian Redl12d6da02010-07-19 22:06:55 +00002019 if (F.LocalNumDecls != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002020 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002021 return Failure;
2022 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00002023 F.DeclOffsets = (const uint32_t *)BlobStart;
2024 F.LocalNumDecls = Record[0];
Douglas Gregor8038d512009-04-10 17:25:41 +00002025 break;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002026
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002027 case TU_UPDATE_LEXICAL: {
Sebastian Redld692af72010-07-27 18:24:41 +00002028 DeclContextInfo Info = {
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002029 /* No visible information */ 0,
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002030 reinterpret_cast<const KindDeclIDPair *>(BlobStart),
2031 BlobLen / sizeof(KindDeclIDPair)
Sebastian Redld692af72010-07-27 18:24:41 +00002032 };
Douglas Gregor3747ee72010-10-01 01:18:02 +00002033 DeclContextOffsets[Context ? Context->getTranslationUnitDecl() : 0]
2034 .push_back(Info);
Sebastian Redld692af72010-07-27 18:24:41 +00002035 break;
2036 }
2037
Sebastian Redle1dde812010-08-24 00:50:04 +00002038 case UPDATE_VISIBLE: {
2039 serialization::DeclID ID = Record[0];
2040 void *Table = ASTDeclContextNameLookupTable::Create(
2041 (const unsigned char *)BlobStart + Record[1],
2042 (const unsigned char *)BlobStart,
2043 ASTDeclContextNameLookupTrait(*this));
Douglas Gregor3747ee72010-10-01 01:18:02 +00002044 if (ID == 1 && Context) { // Is it the TU?
Sebastian Redle1dde812010-08-24 00:50:04 +00002045 DeclContextInfo Info = {
2046 Table, /* No lexical inforamtion */ 0, 0
2047 };
2048 DeclContextOffsets[Context->getTranslationUnitDecl()].push_back(Info);
2049 } else
2050 PendingVisibleUpdates[ID].push_back(Table);
2051 break;
2052 }
2053
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002054 case REDECLS_UPDATE_LATEST: {
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00002055 assert(Record.size() % 2 == 0 && "Expected pairs of DeclIDs");
2056 for (unsigned i = 0, e = Record.size(); i < e; i += 2) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002057 DeclID First = Record[i], Latest = Record[i+1];
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00002058 assert((FirstLatestDeclIDs.find(First) == FirstLatestDeclIDs.end() ||
2059 Latest > FirstLatestDeclIDs[First]) &&
2060 "The new latest is supposed to come after the previous latest");
2061 FirstLatestDeclIDs[First] = Latest;
2062 }
2063 break;
2064 }
2065
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002066 case LANGUAGE_OPTIONS:
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00002067 if (ParseLanguageOptions(Record) && !DisableValidation)
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002068 return IgnorePCH;
2069 break;
Douglas Gregor2bec0412009-04-10 21:16:55 +00002070
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002071 case IDENTIFIER_TABLE:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002072 F.IdentifierTableData = BlobStart;
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002073 if (Record[0]) {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002074 F.IdentifierLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002075 = ASTIdentifierLookupTable::Create(
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002076 (const unsigned char *)F.IdentifierTableData + Record[0],
2077 (const unsigned char *)F.IdentifierTableData,
Sebastian Redlc3632732010-10-05 15:59:54 +00002078 ASTIdentifierLookupTrait(*this, F));
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002079 if (PP)
2080 PP->getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002081 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002082 break;
2083
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002084 case IDENTIFIER_OFFSET:
Sebastian Redl2da08f92010-07-19 22:28:42 +00002085 if (F.LocalNumIdentifiers != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002086 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00002087 return Failure;
2088 }
Sebastian Redl2da08f92010-07-19 22:28:42 +00002089 F.IdentifierOffsets = (const uint32_t *)BlobStart;
2090 F.LocalNumIdentifiers = Record[0];
Douglas Gregorafaf3082009-04-11 00:14:32 +00002091 break;
Douglas Gregorfdd01722009-04-14 00:24:19 +00002092
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002093 case EXTERNAL_DEFINITIONS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002094 // Optimization for the first block.
2095 if (ExternalDefinitions.empty())
2096 ExternalDefinitions.swap(Record);
2097 else
2098 ExternalDefinitions.insert(ExternalDefinitions.end(),
2099 Record.begin(), Record.end());
Douglas Gregorfdd01722009-04-14 00:24:19 +00002100 break;
Douglas Gregor3e1af842009-04-17 22:13:46 +00002101
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002102 case SPECIAL_TYPES:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002103 // Optimization for the first block
2104 if (SpecialTypes.empty())
2105 SpecialTypes.swap(Record);
2106 else
2107 SpecialTypes.insert(SpecialTypes.end(), Record.begin(), Record.end());
Douglas Gregorad1de002009-04-18 05:55:16 +00002108 break;
2109
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002110 case STATISTICS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002111 TotalNumStatements += Record[0];
2112 TotalNumMacros += Record[1];
2113 TotalLexicalDeclContexts += Record[2];
2114 TotalVisibleDeclContexts += Record[3];
Douglas Gregor3e1af842009-04-17 22:13:46 +00002115 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002116
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002117 case TENTATIVE_DEFINITIONS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002118 // Optimization for the first block.
2119 if (TentativeDefinitions.empty())
2120 TentativeDefinitions.swap(Record);
2121 else
2122 TentativeDefinitions.insert(TentativeDefinitions.end(),
2123 Record.begin(), Record.end());
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002124 break;
Douglas Gregor14c22f22009-04-22 22:18:58 +00002125
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002126 case UNUSED_FILESCOPED_DECLS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002127 // Optimization for the first block.
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002128 if (UnusedFileScopedDecls.empty())
2129 UnusedFileScopedDecls.swap(Record);
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002130 else
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002131 UnusedFileScopedDecls.insert(UnusedFileScopedDecls.end(),
2132 Record.begin(), Record.end());
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002133 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002134
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002135 case WEAK_UNDECLARED_IDENTIFIERS:
Sebastian Redl40566802010-08-05 18:21:25 +00002136 // Later blocks overwrite earlier ones.
2137 WeakUndeclaredIdentifiers.swap(Record);
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002138 break;
2139
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002140 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002141 // Optimization for the first block.
2142 if (LocallyScopedExternalDecls.empty())
2143 LocallyScopedExternalDecls.swap(Record);
2144 else
2145 LocallyScopedExternalDecls.insert(LocallyScopedExternalDecls.end(),
2146 Record.begin(), Record.end());
Douglas Gregor14c22f22009-04-22 22:18:58 +00002147 break;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002148
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002149 case SELECTOR_OFFSETS:
Sebastian Redl059612d2010-08-03 21:58:15 +00002150 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redl725cd962010-08-04 20:40:17 +00002151 F.LocalNumSelectors = Record[0];
Douglas Gregor83941df2009-04-25 17:48:32 +00002152 break;
2153
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002154 case METHOD_POOL:
Sebastian Redl725cd962010-08-04 20:40:17 +00002155 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor83941df2009-04-25 17:48:32 +00002156 if (Record[0])
Sebastian Redl725cd962010-08-04 20:40:17 +00002157 F.SelectorLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002158 = ASTSelectorLookupTable::Create(
Sebastian Redl725cd962010-08-04 20:40:17 +00002159 F.SelectorLookupTableData + Record[0],
2160 F.SelectorLookupTableData,
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002161 ASTSelectorLookupTrait(*this));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002162 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002163 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002164
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002165 case REFERENCED_SELECTOR_POOL:
Sebastian Redlc3632732010-10-05 15:59:54 +00002166 F.ReferencedSelectorsData.swap(Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002167 break;
2168
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002169 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002170 if (!Record.empty() && Listener)
2171 Listener->ReadCounter(Record[0]);
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002172 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002173
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002174 case SOURCE_LOCATION_OFFSETS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002175 F.SLocOffsets = (const uint32_t *)BlobStart;
2176 F.LocalNumSLocEntries = Record[0];
Sebastian Redl8db9fae2010-09-22 20:19:08 +00002177 F.LocalSLocSize = Record[1];
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002178 break;
2179
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002180 case SOURCE_LOCATION_PRELOADS:
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002181 if (PreloadSLocEntries.empty())
2182 PreloadSLocEntries.swap(Record);
2183 else
2184 PreloadSLocEntries.insert(PreloadSLocEntries.end(),
2185 Record.begin(), Record.end());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002186 break;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002187
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002188 case STAT_CACHE: {
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00002189 if (!DisableStatCache) {
2190 ASTStatCache *MyStatCache =
2191 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
2192 (const unsigned char *)BlobStart,
2193 NumStatHits, NumStatMisses);
2194 FileMgr.addStatCache(MyStatCache);
2195 F.StatCache = MyStatCache;
2196 }
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002197 break;
Douglas Gregor52e71082009-10-16 18:18:30 +00002198 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002199
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002200 case EXT_VECTOR_DECLS:
Sebastian Redla9f23682010-07-28 21:38:49 +00002201 // Optimization for the first block.
2202 if (ExtVectorDecls.empty())
2203 ExtVectorDecls.swap(Record);
2204 else
2205 ExtVectorDecls.insert(ExtVectorDecls.end(),
2206 Record.begin(), Record.end());
Douglas Gregorb81c1702009-04-27 20:06:05 +00002207 break;
2208
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002209 case VTABLE_USES:
Sebastian Redl40566802010-08-05 18:21:25 +00002210 // Later tables overwrite earlier ones.
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002211 VTableUses.swap(Record);
2212 break;
2213
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002214 case DYNAMIC_CLASSES:
Sebastian Redl40566802010-08-05 18:21:25 +00002215 // Optimization for the first block.
2216 if (DynamicClasses.empty())
2217 DynamicClasses.swap(Record);
2218 else
2219 DynamicClasses.insert(DynamicClasses.end(),
2220 Record.begin(), Record.end());
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002221 break;
2222
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002223 case PENDING_IMPLICIT_INSTANTIATIONS:
Sebastian Redlc3632732010-10-05 15:59:54 +00002224 F.PendingInstantiations.swap(Record);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002225 break;
2226
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002227 case SEMA_DECL_REFS:
Sebastian Redl40566802010-08-05 18:21:25 +00002228 // Later tables overwrite earlier ones.
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002229 SemaDeclRefs.swap(Record);
2230 break;
2231
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002232 case ORIGINAL_FILE_NAME:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002233 // The primary AST will be the last to get here, so it will be the one
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002234 // that's used.
Daniel Dunbar7b5a1212009-11-11 05:29:04 +00002235 ActualOriginalFileName.assign(BlobStart, BlobLen);
2236 OriginalFileName = ActualOriginalFileName;
Douglas Gregore650c8c2009-07-07 00:12:59 +00002237 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregorb64c1932009-05-12 01:31:05 +00002238 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002239
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002240 case ORIGINAL_PCH_DIR:
2241 // The primary AST will be the last to get here, so it will be the one
2242 // that's used.
2243 OriginalDir.assign(BlobStart, BlobLen);
2244 break;
2245
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002246 case VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek974be4d2010-02-12 23:31:14 +00002247 const std::string &CurBranch = getClangFullRepositoryVersion();
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002248 llvm::StringRef ASTBranch(BlobStart, BlobLen);
2249 if (llvm::StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2250 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregor445e23e2009-10-05 21:07:28 +00002251 return IgnorePCH;
2252 }
2253 break;
2254 }
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002255
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002256 case MACRO_DEFINITION_OFFSETS:
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002257 F.MacroDefinitionOffsets = (const uint32_t *)BlobStart;
2258 F.NumPreallocatedPreprocessingEntities = Record[0];
2259 F.LocalNumMacroDefinitions = Record[1];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002260 break;
Sebastian Redl0b17c612010-08-13 00:28:03 +00002261
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002262 case DECL_UPDATE_OFFSETS: {
2263 if (Record.size() % 2 != 0) {
2264 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2265 return Failure;
2266 }
2267 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
2268 DeclUpdateOffsets[static_cast<DeclID>(Record[I])]
2269 .push_back(std::make_pair(&F, Record[I+1]));
2270 break;
2271 }
2272
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002273 case DECL_REPLACEMENTS: {
Sebastian Redl0b17c612010-08-13 00:28:03 +00002274 if (Record.size() % 2 != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002275 Error("invalid DECL_REPLACEMENTS block in AST file");
Sebastian Redl0b17c612010-08-13 00:28:03 +00002276 return Failure;
2277 }
2278 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002279 ReplacedDecls[static_cast<DeclID>(Record[I])] =
Sebastian Redl0b17c612010-08-13 00:28:03 +00002280 std::make_pair(&F, Record[I+1]);
2281 break;
2282 }
Douglas Gregor7c789c12010-10-29 22:39:52 +00002283
2284 case CXX_BASE_SPECIFIER_OFFSETS: {
2285 if (F.LocalNumCXXBaseSpecifiers != 0) {
2286 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2287 return Failure;
2288 }
2289
2290 F.LocalNumCXXBaseSpecifiers = Record[0];
2291 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart;
2292 break;
2293 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002294
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002295 case DIAG_PRAGMA_MAPPINGS:
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002296 if (Record.size() % 2 != 0) {
2297 Error("invalid DIAG_USER_MAPPINGS block in AST file");
2298 return Failure;
2299 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002300 if (PragmaDiagMappings.empty())
2301 PragmaDiagMappings.swap(Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002302 else
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002303 PragmaDiagMappings.insert(PragmaDiagMappings.end(),
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002304 Record.begin(), Record.end());
2305 break;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002306
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002307 case CUDA_SPECIAL_DECL_REFS:
2308 // Later tables overwrite earlier ones.
2309 CUDASpecialDeclRefs.swap(Record);
2310 break;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002311
2312 case HEADER_SEARCH_TABLE:
2313 F.HeaderFileInfoTableData = BlobStart;
2314 F.LocalNumHeaderFileInfos = Record[1];
2315 if (Record[0]) {
2316 F.HeaderFileInfoTable
2317 = HeaderFileInfoLookupTable::Create(
2318 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
2319 (const unsigned char *)F.HeaderFileInfoTableData);
2320 if (PP)
2321 PP->getHeaderSearchInfo().SetExternalSource(this);
2322 }
2323 break;
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002324
2325 case FP_PRAGMA_OPTIONS:
2326 // Later tables overwrite earlier ones.
2327 FPPragmaOptions.swap(Record);
2328 break;
2329
2330 case OPENCL_EXTENSIONS:
2331 // Later tables overwrite earlier ones.
2332 OpenCLExtensions.swap(Record);
2333 break;
Douglas Gregorafaf3082009-04-11 00:14:32 +00002334 }
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002335 First = false;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002336 }
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002337 Error("premature end of bitstream in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002338 return Failure;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002339}
2340
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002341ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
2342 ASTFileType Type) {
2343 switch(ReadASTCore(FileName, Type)) {
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002344 case Failure: return Failure;
2345 case IgnorePCH: return IgnorePCH;
2346 case Success: break;
2347 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002348
2349 // Here comes stuff that we only do once the entire chain is loaded.
2350
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002351 // Allocate space for loaded slocentries, identifiers, decls and types.
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002352 unsigned TotalNumIdentifiers = 0, TotalNumTypes = 0, TotalNumDecls = 0,
Sebastian Redl725cd962010-08-04 20:40:17 +00002353 TotalNumPreallocatedPreprocessingEntities = 0, TotalNumMacroDefs = 0,
2354 TotalNumSelectors = 0;
Sebastian Redl12d6da02010-07-19 22:06:55 +00002355 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002356 TotalNumSLocEntries += Chain[I]->LocalNumSLocEntries;
Sebastian Redl8db9fae2010-09-22 20:19:08 +00002357 NextSLocOffset += Chain[I]->LocalSLocSize;
Sebastian Redl2da08f92010-07-19 22:28:42 +00002358 TotalNumIdentifiers += Chain[I]->LocalNumIdentifiers;
Sebastian Redl12d6da02010-07-19 22:06:55 +00002359 TotalNumTypes += Chain[I]->LocalNumTypes;
2360 TotalNumDecls += Chain[I]->LocalNumDecls;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002361 TotalNumPreallocatedPreprocessingEntities +=
2362 Chain[I]->NumPreallocatedPreprocessingEntities;
2363 TotalNumMacroDefs += Chain[I]->LocalNumMacroDefinitions;
Sebastian Redl725cd962010-08-04 20:40:17 +00002364 TotalNumSelectors += Chain[I]->LocalNumSelectors;
Sebastian Redl12d6da02010-07-19 22:06:55 +00002365 }
Sebastian Redl8db9fae2010-09-22 20:19:08 +00002366 SourceMgr.PreallocateSLocEntries(this, TotalNumSLocEntries, NextSLocOffset);
Sebastian Redl2da08f92010-07-19 22:28:42 +00002367 IdentifiersLoaded.resize(TotalNumIdentifiers);
Sebastian Redl12d6da02010-07-19 22:06:55 +00002368 TypesLoaded.resize(TotalNumTypes);
2369 DeclsLoaded.resize(TotalNumDecls);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002370 MacroDefinitionsLoaded.resize(TotalNumMacroDefs);
2371 if (PP) {
2372 if (TotalNumIdentifiers > 0)
2373 PP->getHeaderSearchInfo().SetExternalLookup(this);
2374 if (TotalNumPreallocatedPreprocessingEntities > 0) {
2375 if (!PP->getPreprocessingRecord())
2376 PP->createPreprocessingRecord();
2377 PP->getPreprocessingRecord()->SetExternalSource(*this,
2378 TotalNumPreallocatedPreprocessingEntities);
2379 }
2380 }
Sebastian Redl725cd962010-08-04 20:40:17 +00002381 SelectorsLoaded.resize(TotalNumSelectors);
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002382 // Preload SLocEntries.
2383 for (unsigned I = 0, N = PreloadSLocEntries.size(); I != N; ++I) {
2384 ASTReadResult Result = ReadSLocEntryRecord(PreloadSLocEntries[I]);
2385 if (Result != Success)
2386 return Result;
2387 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00002388
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002389 // Check the predefines buffers.
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00002390 if (!DisableValidation && CheckPredefinesBuffers())
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002391 return IgnorePCH;
2392
2393 if (PP) {
2394 // Initialization of keywords and pragmas occurs before the
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002395 // AST file is read, so there may be some identifiers that were
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002396 // loaded into the IdentifierTable before we intercepted the
2397 // creation of identifiers. Iterate through the list of known
2398 // identifiers and determine whether we have to establish
2399 // preprocessor definitions or top-level identifier declaration
2400 // chains for those identifiers.
2401 //
2402 // We copy the IdentifierInfo pointers to a small vector first,
2403 // since de-serializing declarations or macro definitions can add
2404 // new entries into the identifier table, invalidating the
2405 // iterators.
2406 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
2407 for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(),
2408 IdEnd = PP->getIdentifierTable().end();
2409 Id != IdEnd; ++Id)
2410 Identifiers.push_back(Id->second);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002411 // We need to search the tables in all files.
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002412 for (unsigned J = 0, M = Chain.size(); J != M; ++J) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002413 ASTIdentifierLookupTable *IdTable
2414 = (ASTIdentifierLookupTable *)Chain[J]->IdentifierLookupTable;
2415 // Not all AST files necessarily have identifier tables, only the useful
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00002416 // ones.
2417 if (!IdTable)
2418 continue;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002419 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
2420 IdentifierInfo *II = Identifiers[I];
2421 // Look in the on-disk hash tables for an entry for this identifier
Sebastian Redlc3632732010-10-05 15:59:54 +00002422 ASTIdentifierLookupTrait Info(*this, *Chain[J], II);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002423 std::pair<const char*,unsigned> Key(II->getNameStart(),II->getLength());
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002424 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002425 if (Pos == IdTable->end())
2426 continue;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002427
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002428 // Dereferencing the iterator has the effect of populating the
2429 // IdentifierInfo node with the various declarations it needs.
2430 (void)*Pos;
2431 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002432 }
2433 }
2434
2435 if (Context)
2436 InitializeContext(*Context);
2437
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002438 if (DeserializationListener)
2439 DeserializationListener->ReaderInitialized(this);
2440
Douglas Gregor414cb642010-11-30 05:23:00 +00002441 // If this AST file is a precompiled preamble, then set the main file ID of
2442 // the source manager to the file source file from which the preamble was
2443 // built. This is the only valid way to use a precompiled preamble.
2444 if (Type == Preamble) {
2445 SourceLocation Loc
2446 = SourceMgr.getLocation(FileMgr.getFile(getOriginalSourceFile()), 1, 1);
2447 if (Loc.isValid()) {
2448 std::pair<FileID, unsigned> Decomposed = SourceMgr.getDecomposedLoc(Loc);
2449 SourceMgr.SetPreambleFileID(Decomposed.first);
2450 }
2451 }
2452
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002453 return Success;
2454}
2455
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002456ASTReader::ASTReadResult ASTReader::ReadASTCore(llvm::StringRef FileName,
2457 ASTFileType Type) {
Sebastian Redla866e652010-10-01 19:59:12 +00002458 PerFileData *Prev = Chain.empty() ? 0 : Chain.back();
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002459 Chain.push_back(new PerFileData(Type));
Sebastian Redl9137a522010-07-16 17:50:48 +00002460 PerFileData &F = *Chain.back();
Sebastian Redla866e652010-10-01 19:59:12 +00002461 if (Prev)
2462 Prev->NextInSource = &F;
2463 else
2464 FirstInSource = &F;
2465 F.Loaders.push_back(Prev);
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002466
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002467 // Set the AST file name.
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002468 F.FileName = FileName;
2469
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002470 if (FileName != "-") {
2471 CurrentDir = llvm::sys::path::parent_path(FileName);
2472 if (CurrentDir.empty()) CurrentDir = ".";
2473 }
2474
Argyrios Kyrtzidisb0f4b9a2011-03-09 17:21:42 +00002475 if (!ASTBuffers.empty()) {
Sebastian Redl56558372011-04-14 14:07:41 +00002476 F.Buffer.reset(ASTBuffers.back());
2477 ASTBuffers.pop_back();
Argyrios Kyrtzidisb0f4b9a2011-03-09 17:21:42 +00002478 assert(F.Buffer && "Passed null buffer");
2479 } else {
2480 // Open the AST file.
2481 //
2482 // FIXME: This shouldn't be here, we should just take a raw_ostream.
2483 std::string ErrStr;
2484 llvm::error_code ec;
2485 if (FileName == "-") {
2486 ec = llvm::MemoryBuffer::getSTDIN(F.Buffer);
2487 if (ec)
2488 ErrStr = ec.message();
2489 } else
2490 F.Buffer.reset(FileMgr.getBufferForFile(FileName, &ErrStr));
2491 if (!F.Buffer) {
2492 Error(ErrStr.c_str());
2493 return IgnorePCH;
2494 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002495 }
2496
2497 // Initialize the stream
2498 F.StreamFile.init((const unsigned char *)F.Buffer->getBufferStart(),
2499 (const unsigned char *)F.Buffer->getBufferEnd());
Sebastian Redl9137a522010-07-16 17:50:48 +00002500 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002501 Stream.init(F.StreamFile);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002502 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002503
2504 // Sniff for the signature.
2505 if (Stream.Read(8) != 'C' ||
2506 Stream.Read(8) != 'P' ||
2507 Stream.Read(8) != 'C' ||
2508 Stream.Read(8) != 'H') {
2509 Diag(diag::err_not_a_pch_file) << FileName;
2510 return Failure;
2511 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002512
Douglas Gregor2cf26342009-04-09 22:27:44 +00002513 while (!Stream.AtEndOfStream()) {
2514 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00002515
Douglas Gregore1d918e2009-04-10 23:10:45 +00002516 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002517 Error("invalid record at top-level of AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002518 return Failure;
2519 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002520
2521 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregor668c1a42009-04-21 22:25:48 +00002522
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002523 // We only know the AST subblock ID.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002524 switch (BlockID) {
2525 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002526 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002527 Error("malformed BlockInfoBlock in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002528 return Failure;
2529 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002530 break;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002531 case AST_BLOCK_ID:
Sebastian Redl571db7f2010-08-18 23:56:56 +00002532 switch (ReadASTBlock(F)) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002533 case Success:
2534 break;
2535
2536 case Failure:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002537 return Failure;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002538
2539 case IgnorePCH:
Douglas Gregor2bec0412009-04-10 21:16:55 +00002540 // FIXME: We could consider reading through to the end of this
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002541 // AST block, skipping subblocks, to see if there are other
2542 // AST blocks elsewhere.
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002543
2544 // Clear out any preallocated source location entries, so that
2545 // the source manager does not try to resolve them later.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002546 SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002547
2548 // Remove the stat cache.
Sebastian Redl9137a522010-07-16 17:50:48 +00002549 if (F.StatCache)
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002550 FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002551
Douglas Gregore1d918e2009-04-10 23:10:45 +00002552 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002553 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002554 break;
2555 default:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002556 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002557 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002558 return Failure;
2559 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002560 break;
2561 }
Mike Stump1eb44332009-09-09 15:08:12 +00002562 }
2563
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002564 return Success;
2565}
2566
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002567void ASTReader::setPreprocessor(Preprocessor &pp) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002568 PP = &pp;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002569
2570 unsigned TotalNum = 0;
2571 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
2572 TotalNum += Chain[I]->NumPreallocatedPreprocessingEntities;
2573 if (TotalNum) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002574 if (!PP->getPreprocessingRecord())
2575 PP->createPreprocessingRecord();
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002576 PP->getPreprocessingRecord()->SetExternalSource(*this, TotalNum);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002577 }
2578}
2579
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002580void ASTReader::InitializeContext(ASTContext &Ctx) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002581 Context = &Ctx;
2582 assert(Context && "Passed null context!");
2583
2584 assert(PP && "Forgot to set Preprocessor ?");
2585 PP->getIdentifierTable().setExternalIdentifierLookup(this);
2586 PP->getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregor88a35862010-01-04 19:18:44 +00002587 PP->setExternalSource(this);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002588 PP->getHeaderSearchInfo().SetExternalSource(this);
2589
Douglas Gregor3747ee72010-10-01 01:18:02 +00002590 // If we have an update block for the TU waiting, we have to add it before
2591 // deserializing the decl.
2592 DeclContextOffsetsMap::iterator DCU = DeclContextOffsets.find(0);
2593 if (DCU != DeclContextOffsets.end()) {
2594 // Insertion could invalidate map, so grab vector.
2595 DeclContextInfos T;
2596 T.swap(DCU->second);
2597 DeclContextOffsets.erase(DCU);
2598 DeclContextOffsets[Ctx.getTranslationUnitDecl()].swap(T);
2599 }
2600
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002601 // Load the translation unit declaration
Argyrios Kyrtzidis8871a442010-07-08 17:13:02 +00002602 GetTranslationUnitDecl();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002603
2604 // Load the special types.
2605 Context->setBuiltinVaListType(
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002606 GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST]));
2607 if (unsigned Id = SpecialTypes[SPECIAL_TYPE_OBJC_ID])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002608 Context->setObjCIdType(GetType(Id));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002609 if (unsigned Sel = SpecialTypes[SPECIAL_TYPE_OBJC_SELECTOR])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002610 Context->setObjCSelType(GetType(Sel));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002611 if (unsigned Proto = SpecialTypes[SPECIAL_TYPE_OBJC_PROTOCOL])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002612 Context->setObjCProtoType(GetType(Proto));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002613 if (unsigned Class = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002614 Context->setObjCClassType(GetType(Class));
Steve Naroff14108da2009-07-10 23:34:53 +00002615
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002616 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002617 Context->setCFConstantStringType(GetType(String));
Mike Stump1eb44332009-09-09 15:08:12 +00002618 if (unsigned FastEnum
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002619 = SpecialTypes[SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002620 Context->setObjCFastEnumerationStateType(GetType(FastEnum));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002621 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002622 QualType FileType = GetType(File);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002623 if (FileType.isNull()) {
2624 Error("FILE type is NULL");
2625 return;
2626 }
John McCall183700f2009-09-21 23:43:11 +00002627 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002628 Context->setFILEDecl(Typedef->getDecl());
2629 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002630 const TagType *Tag = FileType->getAs<TagType>();
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002631 if (!Tag) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002632 Error("Invalid FILE type in AST file");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002633 return;
2634 }
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002635 Context->setFILEDecl(Tag->getDecl());
2636 }
2637 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002638 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_jmp_buf]) {
Mike Stump782fa302009-07-28 02:25:19 +00002639 QualType Jmp_bufType = GetType(Jmp_buf);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002640 if (Jmp_bufType.isNull()) {
2641 Error("jmp_bug type is NULL");
2642 return;
2643 }
John McCall183700f2009-09-21 23:43:11 +00002644 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
Mike Stump782fa302009-07-28 02:25:19 +00002645 Context->setjmp_bufDecl(Typedef->getDecl());
2646 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002647 const TagType *Tag = Jmp_bufType->getAs<TagType>();
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002648 if (!Tag) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002649 Error("Invalid jmp_buf type in AST file");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002650 return;
2651 }
Mike Stump782fa302009-07-28 02:25:19 +00002652 Context->setjmp_bufDecl(Tag->getDecl());
2653 }
2654 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002655 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_sigjmp_buf]) {
Mike Stump782fa302009-07-28 02:25:19 +00002656 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002657 if (Sigjmp_bufType.isNull()) {
2658 Error("sigjmp_buf type is NULL");
2659 return;
2660 }
John McCall183700f2009-09-21 23:43:11 +00002661 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
Mike Stump782fa302009-07-28 02:25:19 +00002662 Context->setsigjmp_bufDecl(Typedef->getDecl());
2663 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002664 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002665 assert(Tag && "Invalid sigjmp_buf type in AST file");
Mike Stump782fa302009-07-28 02:25:19 +00002666 Context->setsigjmp_bufDecl(Tag->getDecl());
2667 }
2668 }
Mike Stump1eb44332009-09-09 15:08:12 +00002669 if (unsigned ObjCIdRedef
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002670 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION])
Douglas Gregord1571ac2009-08-21 00:27:50 +00002671 Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef);
Mike Stump1eb44332009-09-09 15:08:12 +00002672 if (unsigned ObjCClassRedef
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002673 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION])
Douglas Gregord1571ac2009-08-21 00:27:50 +00002674 Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002675 if (unsigned String = SpecialTypes[SPECIAL_TYPE_BLOCK_DESCRIPTOR])
Mike Stumpadaaad32009-10-20 02:12:22 +00002676 Context->setBlockDescriptorType(GetType(String));
Mike Stump083c25e2009-10-22 00:49:09 +00002677 if (unsigned String
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002678 = SpecialTypes[SPECIAL_TYPE_BLOCK_EXTENDED_DESCRIPTOR])
Mike Stump083c25e2009-10-22 00:49:09 +00002679 Context->setBlockDescriptorExtendedType(GetType(String));
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002680 if (unsigned ObjCSelRedef
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002681 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION])
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002682 Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002683 if (unsigned String = SpecialTypes[SPECIAL_TYPE_NS_CONSTANT_STRING])
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002684 Context->setNSConstantStringType(GetType(String));
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +00002685
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002686 if (SpecialTypes[SPECIAL_TYPE_INT128_INSTALLED])
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +00002687 Context->setInt128Installed();
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002688
Richard Smithad762fc2011-04-14 22:09:26 +00002689 if (unsigned AutoDeduct = SpecialTypes[SPECIAL_TYPE_AUTO_DEDUCT])
2690 Context->AutoDeductTy = GetType(AutoDeduct);
2691 if (unsigned AutoRRefDeduct = SpecialTypes[SPECIAL_TYPE_AUTO_RREF_DEDUCT])
2692 Context->AutoRRefDeductTy = GetType(AutoRRefDeduct);
2693
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002694 ReadPragmaDiagnosticMappings(Context->getDiagnostics());
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002695
2696 // If there were any CUDA special declarations, deserialize them.
2697 if (!CUDASpecialDeclRefs.empty()) {
2698 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
2699 Context->setcudaConfigureCallDecl(
2700 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
2701 }
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002702}
2703
Douglas Gregorb64c1932009-05-12 01:31:05 +00002704/// \brief Retrieve the name of the original source file name
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002705/// directly from the AST file, without actually loading the AST
Douglas Gregorb64c1932009-05-12 01:31:05 +00002706/// file.
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002707std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +00002708 FileManager &FileMgr,
Daniel Dunbar93ebb1b2009-12-03 09:13:06 +00002709 Diagnostic &Diags) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002710 // Open the AST file.
Douglas Gregorb64c1932009-05-12 01:31:05 +00002711 std::string ErrStr;
2712 llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
Chris Lattner39b49bc2010-11-23 08:35:12 +00002713 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
Douglas Gregorb64c1932009-05-12 01:31:05 +00002714 if (!Buffer) {
Daniel Dunbar93ebb1b2009-12-03 09:13:06 +00002715 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002716 return std::string();
2717 }
2718
2719 // Initialize the stream
2720 llvm::BitstreamReader StreamFile;
2721 llvm::BitstreamCursor Stream;
Mike Stump1eb44332009-09-09 15:08:12 +00002722 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregorb64c1932009-05-12 01:31:05 +00002723 (const unsigned char *)Buffer->getBufferEnd());
2724 Stream.init(StreamFile);
2725
2726 // Sniff for the signature.
2727 if (Stream.Read(8) != 'C' ||
2728 Stream.Read(8) != 'P' ||
2729 Stream.Read(8) != 'C' ||
2730 Stream.Read(8) != 'H') {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002731 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002732 return std::string();
2733 }
2734
2735 RecordData Record;
2736 while (!Stream.AtEndOfStream()) {
2737 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00002738
Douglas Gregorb64c1932009-05-12 01:31:05 +00002739 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2740 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump1eb44332009-09-09 15:08:12 +00002741
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002742 // We only know the AST subblock ID.
Douglas Gregorb64c1932009-05-12 01:31:05 +00002743 switch (BlockID) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002744 case AST_BLOCK_ID:
2745 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002746 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002747 return std::string();
2748 }
2749 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002750
Douglas Gregorb64c1932009-05-12 01:31:05 +00002751 default:
2752 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002753 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002754 return std::string();
2755 }
2756 break;
2757 }
2758 continue;
2759 }
2760
2761 if (Code == llvm::bitc::END_BLOCK) {
2762 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002763 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002764 return std::string();
2765 }
2766 continue;
2767 }
2768
2769 if (Code == llvm::bitc::DEFINE_ABBREV) {
2770 Stream.ReadAbbrevRecord();
2771 continue;
2772 }
2773
2774 Record.clear();
2775 const char *BlobStart = 0;
2776 unsigned BlobLen = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002777 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002778 == ORIGINAL_FILE_NAME)
Douglas Gregorb64c1932009-05-12 01:31:05 +00002779 return std::string(BlobStart, BlobLen);
Mike Stump1eb44332009-09-09 15:08:12 +00002780 }
Douglas Gregorb64c1932009-05-12 01:31:05 +00002781
2782 return std::string();
2783}
2784
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002785/// \brief Parse the record that corresponds to a LangOptions data
2786/// structure.
2787///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002788/// This routine parses the language options from the AST file and then gives
2789/// them to the AST listener if one is set.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002790///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002791/// \returns true if the listener deems the file unacceptable, false otherwise.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002792bool ASTReader::ParseLanguageOptions(
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002793 const llvm::SmallVectorImpl<uint64_t> &Record) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002794 if (Listener) {
2795 LangOptions LangOpts;
Mike Stump1eb44332009-09-09 15:08:12 +00002796
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002797 #define PARSE_LANGOPT(Option) \
2798 LangOpts.Option = Record[Idx]; \
2799 ++Idx
Mike Stump1eb44332009-09-09 15:08:12 +00002800
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002801 unsigned Idx = 0;
2802 PARSE_LANGOPT(Trigraphs);
2803 PARSE_LANGOPT(BCPLComment);
2804 PARSE_LANGOPT(DollarIdents);
2805 PARSE_LANGOPT(AsmPreprocessor);
2806 PARSE_LANGOPT(GNUMode);
Chandler Carrutheb5d7b72010-04-17 20:17:31 +00002807 PARSE_LANGOPT(GNUKeywords);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002808 PARSE_LANGOPT(ImplicitInt);
2809 PARSE_LANGOPT(Digraphs);
2810 PARSE_LANGOPT(HexFloats);
2811 PARSE_LANGOPT(C99);
Peter Collingbourne7e7fbd02011-04-15 00:35:23 +00002812 PARSE_LANGOPT(C1X);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002813 PARSE_LANGOPT(Microsoft);
2814 PARSE_LANGOPT(CPlusPlus);
2815 PARSE_LANGOPT(CPlusPlus0x);
2816 PARSE_LANGOPT(CXXOperatorNames);
2817 PARSE_LANGOPT(ObjC1);
2818 PARSE_LANGOPT(ObjC2);
2819 PARSE_LANGOPT(ObjCNonFragileABI);
Fariborz Jahanian412e7982010-02-09 19:31:38 +00002820 PARSE_LANGOPT(ObjCNonFragileABI2);
Fariborz Jahanianf84109e2011-01-07 18:59:25 +00002821 PARSE_LANGOPT(AppleKext);
Ted Kremenekc32647d2010-12-23 21:35:43 +00002822 PARSE_LANGOPT(ObjCDefaultSynthProperties);
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +00002823 PARSE_LANGOPT(NoConstantCFStrings);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002824 PARSE_LANGOPT(PascalStrings);
2825 PARSE_LANGOPT(WritableStrings);
2826 PARSE_LANGOPT(LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +00002827 PARSE_LANGOPT(AltiVec);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002828 PARSE_LANGOPT(Exceptions);
Anders Carlssonda4b7cf2011-02-19 23:53:54 +00002829 PARSE_LANGOPT(ObjCExceptions);
Anders Carlsson7da99b02011-02-23 03:04:54 +00002830 PARSE_LANGOPT(CXXExceptions);
2831 PARSE_LANGOPT(SjLjExceptions);
Douglas Gregor6f755502011-02-01 15:15:22 +00002832 PARSE_LANGOPT(MSBitfields);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002833 PARSE_LANGOPT(NeXTRuntime);
2834 PARSE_LANGOPT(Freestanding);
2835 PARSE_LANGOPT(NoBuiltin);
2836 PARSE_LANGOPT(ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +00002837 PARSE_LANGOPT(POSIXThreads);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002838 PARSE_LANGOPT(Blocks);
2839 PARSE_LANGOPT(EmitAllDecls);
2840 PARSE_LANGOPT(MathErrno);
Chris Lattnera4d71452010-06-26 21:25:03 +00002841 LangOpts.setSignedOverflowBehavior((LangOptions::SignedOverflowBehaviorTy)
2842 Record[Idx++]);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002843 PARSE_LANGOPT(HeinousExtensions);
2844 PARSE_LANGOPT(Optimize);
2845 PARSE_LANGOPT(OptimizeSize);
2846 PARSE_LANGOPT(Static);
2847 PARSE_LANGOPT(PICLevel);
2848 PARSE_LANGOPT(GNUInline);
2849 PARSE_LANGOPT(NoInline);
Chandler Carruth0d2d1bc2011-04-23 20:05:38 +00002850 PARSE_LANGOPT(Deprecated);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002851 PARSE_LANGOPT(AccessControl);
2852 PARSE_LANGOPT(CharIsSigned);
John Thompsona6fda122009-11-05 20:14:16 +00002853 PARSE_LANGOPT(ShortWChar);
Argyrios Kyrtzidisb1bdced2011-01-15 02:56:16 +00002854 PARSE_LANGOPT(ShortEnums);
Chris Lattnera4d71452010-06-26 21:25:03 +00002855 LangOpts.setGCMode((LangOptions::GCMode)Record[Idx++]);
John McCall1fb0caa2010-10-22 21:05:15 +00002856 LangOpts.setVisibilityMode((Visibility)Record[Idx++]);
Daniel Dunbarab8e2812009-09-21 04:16:19 +00002857 LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode)
Chris Lattnera4d71452010-06-26 21:25:03 +00002858 Record[Idx++]);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002859 PARSE_LANGOPT(InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +00002860 PARSE_LANGOPT(OpenCL);
Peter Collingbourne08a53262010-12-01 19:14:57 +00002861 PARSE_LANGOPT(CUDA);
Mike Stump9c276ae2009-12-12 01:27:46 +00002862 PARSE_LANGOPT(CatchUndefined);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002863 PARSE_LANGOPT(DefaultFPContract);
Roman Divacky1c51b1c2011-03-01 17:36:40 +00002864 PARSE_LANGOPT(ElideConstructors);
2865 PARSE_LANGOPT(SpellChecking);
Roman Divackycfe9af22011-03-01 17:40:53 +00002866 PARSE_LANGOPT(MRTD);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002867 #undef PARSE_LANGOPT
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002868
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002869 return Listener->ReadLanguageOptions(LangOpts);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002870 }
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002871
2872 return false;
2873}
2874
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002875void ASTReader::ReadPreprocessedEntities() {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002876 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2877 PerFileData &F = *Chain[I];
2878 if (!F.PreprocessorDetailCursor.getBitStreamReader())
2879 continue;
2880
2881 SavedStreamPosition SavedPosition(F.PreprocessorDetailCursor);
2882 F.PreprocessorDetailCursor.JumpToBit(F.PreprocessorDetailStartOffset);
2883 while (LoadPreprocessedEntity(F)) { }
2884 }
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002885}
2886
Douglas Gregor0a480292011-02-11 19:46:30 +00002887PreprocessedEntity *ASTReader::ReadPreprocessedEntityAtOffset(uint64_t Offset) {
Douglas Gregor89d99802010-11-30 06:16:57 +00002888 PerFileData *F = 0;
2889 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2890 if (Offset < Chain[I]->SizeInBits) {
2891 F = Chain[I];
2892 break;
2893 }
2894
2895 Offset -= Chain[I]->SizeInBits;
2896 }
2897
2898 if (!F) {
2899 Error("Malformed preprocessed entity offset");
2900 return 0;
2901 }
2902
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002903 // Keep track of where we are in the stream, then jump back there
2904 // after reading this entity.
2905 SavedStreamPosition SavedPosition(F->PreprocessorDetailCursor);
2906 F->PreprocessorDetailCursor.JumpToBit(Offset);
2907 return LoadPreprocessedEntity(*F);
Douglas Gregor89d99802010-11-30 06:16:57 +00002908}
2909
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002910HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
2911 HeaderFileInfoTrait Trait(FE->getName());
2912 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2913 PerFileData &F = *Chain[I];
2914 HeaderFileInfoLookupTable *Table
2915 = static_cast<HeaderFileInfoLookupTable *>(F.HeaderFileInfoTable);
2916 if (!Table)
2917 continue;
2918
2919 // Look in the on-disk hash table for an entry for this file name.
2920 HeaderFileInfoLookupTable::iterator Pos = Table->find(FE->getName(),
2921 &Trait);
2922 if (Pos == Table->end())
2923 continue;
2924
2925 HeaderFileInfo HFI = *Pos;
2926 if (Listener)
2927 Listener->ReadHeaderFileInfo(HFI, FE->getUID());
2928
2929 return HFI;
2930 }
2931
2932 return HeaderFileInfo();
2933}
2934
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002935void ASTReader::ReadPragmaDiagnosticMappings(Diagnostic &Diag) {
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002936 unsigned Idx = 0;
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002937 while (Idx < PragmaDiagMappings.size()) {
2938 SourceLocation
2939 Loc = SourceLocation::getFromRawEncoding(PragmaDiagMappings[Idx++]);
2940 while (1) {
2941 assert(Idx < PragmaDiagMappings.size() &&
2942 "Invalid data, didn't find '-1' marking end of diag/map pairs");
2943 if (Idx >= PragmaDiagMappings.size())
2944 break; // Something is messed up but at least avoid infinite loop in
2945 // release build.
2946 unsigned DiagID = PragmaDiagMappings[Idx++];
2947 if (DiagID == (unsigned)-1)
2948 break; // no more diag/map pairs for this location.
2949 diag::Mapping Map = (diag::Mapping)PragmaDiagMappings[Idx++];
2950 Diag.setDiagnosticMapping(DiagID, Map, Loc);
2951 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002952 }
2953}
2954
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002955/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002956ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002957 PerFileData *F = 0;
2958 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2959 F = Chain[N - I - 1];
2960 if (Index < F->LocalNumTypes)
2961 break;
2962 Index -= F->LocalNumTypes;
2963 }
2964 assert(F && F->LocalNumTypes > Index && "Broken chain");
Sebastian Redlc3632732010-10-05 15:59:54 +00002965 return RecordLocation(F, F->TypeOffsets[Index]);
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002966}
2967
2968/// \brief Read and return the type with the given index..
Douglas Gregor2cf26342009-04-09 22:27:44 +00002969///
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002970/// The index is the type ID, shifted and minus the number of predefs. This
2971/// routine actually reads the record corresponding to the type at the given
2972/// location. It is a helper routine for GetType, which deals with reading type
2973/// IDs.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002974QualType ASTReader::ReadTypeRecord(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002975 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00002976 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00002977
Douglas Gregor0b748912009-04-14 21:18:50 +00002978 // Keep track of where we are in the stream, then jump back there
2979 // after reading this type.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002980 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregor0b748912009-04-14 21:18:50 +00002981
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00002982 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redl27372b42010-08-11 18:52:41 +00002983
Douglas Gregord89275b2009-07-06 18:54:52 +00002984 // Note that we are loading a type record.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00002985 Deserializing AType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00002986
Sebastian Redlc3632732010-10-05 15:59:54 +00002987 DeclsCursor.JumpToBit(Loc.Offset);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002988 RecordData Record;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002989 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002990 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
2991 case TYPE_EXT_QUAL: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002992 if (Record.size() != 2) {
2993 Error("Incorrect encoding of extended qualifier type");
2994 return QualType();
2995 }
Douglas Gregor6d473962009-04-15 22:00:08 +00002996 QualType Base = GetType(Record[0]);
John McCall0953e762009-09-24 19:53:00 +00002997 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[1]);
2998 return Context->getQualifiedType(Base, Quals);
Douglas Gregor6d473962009-04-15 22:00:08 +00002999 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003000
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003001 case TYPE_COMPLEX: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003002 if (Record.size() != 1) {
3003 Error("Incorrect encoding of complex type");
3004 return QualType();
3005 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003006 QualType ElemType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003007 return Context->getComplexType(ElemType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003008 }
3009
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003010 case TYPE_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003011 if (Record.size() != 1) {
3012 Error("Incorrect encoding of pointer type");
3013 return QualType();
3014 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003015 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003016 return Context->getPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003017 }
3018
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003019 case TYPE_BLOCK_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003020 if (Record.size() != 1) {
3021 Error("Incorrect encoding of block pointer type");
3022 return QualType();
3023 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003024 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003025 return Context->getBlockPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003026 }
3027
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003028 case TYPE_LVALUE_REFERENCE: {
Richard Smithdf1550f2011-04-12 10:38:03 +00003029 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003030 Error("Incorrect encoding of lvalue reference type");
3031 return QualType();
3032 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003033 QualType PointeeType = GetType(Record[0]);
Richard Smithdf1550f2011-04-12 10:38:03 +00003034 return Context->getLValueReferenceType(PointeeType, Record[1]);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003035 }
3036
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003037 case TYPE_RVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003038 if (Record.size() != 1) {
3039 Error("Incorrect encoding of rvalue reference type");
3040 return QualType();
3041 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003042 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003043 return Context->getRValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003044 }
3045
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003046 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidis240437b2010-07-02 11:55:15 +00003047 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003048 Error("Incorrect encoding of member pointer type");
3049 return QualType();
3050 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003051 QualType PointeeType = GetType(Record[0]);
3052 QualType ClassType = GetType(Record[1]);
Douglas Gregor1ab55e92010-12-10 17:03:06 +00003053 if (PointeeType.isNull() || ClassType.isNull())
3054 return QualType();
3055
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003056 return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregor2cf26342009-04-09 22:27:44 +00003057 }
3058
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003059 case TYPE_CONSTANT_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];
3063 unsigned Idx = 3;
3064 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003065 return Context->getConstantArrayType(ElementType, Size,
3066 ASM, IndexTypeQuals);
3067 }
3068
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003069 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003070 QualType ElementType = GetType(Record[0]);
3071 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3072 unsigned IndexTypeQuals = Record[2];
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003073 return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003074 }
3075
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003076 case TYPE_VARIABLE_ARRAY: {
Douglas Gregor0b748912009-04-14 21:18:50 +00003077 QualType ElementType = GetType(Record[0]);
3078 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3079 unsigned IndexTypeQuals = Record[2];
Sebastian Redlc3632732010-10-05 15:59:54 +00003080 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
3081 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
3082 return Context->getVariableArrayType(ElementType, ReadExpr(*Loc.F),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003083 ASM, IndexTypeQuals,
3084 SourceRange(LBLoc, RBLoc));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003085 }
3086
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003087 case TYPE_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00003088 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003089 Error("incorrect encoding of vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003090 return QualType();
3091 }
3092
3093 QualType ElementType = GetType(Record[0]);
3094 unsigned NumElements = Record[1];
Bob Wilsone86d78c2010-11-10 21:56:12 +00003095 unsigned VecKind = Record[2];
Chris Lattner788b0fd2010-06-23 06:00:24 +00003096 return Context->getVectorType(ElementType, NumElements,
Bob Wilsone86d78c2010-11-10 21:56:12 +00003097 (VectorType::VectorKind)VecKind);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003098 }
3099
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003100 case TYPE_EXT_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00003101 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003102 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003103 return QualType();
3104 }
3105
3106 QualType ElementType = GetType(Record[0]);
3107 unsigned NumElements = Record[1];
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003108 return Context->getExtVectorType(ElementType, NumElements);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003109 }
3110
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003111 case TYPE_FUNCTION_NO_PROTO: {
Eli Friedmana49218e2011-04-09 08:18:08 +00003112 if (Record.size() != 5) {
Douglas Gregora02b1472009-04-28 21:53:25 +00003113 Error("incorrect encoding of no-proto function type");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003114 return QualType();
3115 }
3116 QualType ResultType = GetType(Record[0]);
Eli Friedmana49218e2011-04-09 08:18:08 +00003117 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3], (CallingConv)Record[4]);
Rafael Espindola264ba482010-03-30 20:24:48 +00003118 return Context->getFunctionNoProtoType(ResultType, Info);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003119 }
3120
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003121 case TYPE_FUNCTION_PROTO: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003122 QualType ResultType = GetType(Record[0]);
John McCalle23cf432010-12-14 08:05:40 +00003123
3124 FunctionProtoType::ExtProtoInfo EPI;
3125 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
Eli Friedmana49218e2011-04-09 08:18:08 +00003126 /*hasregparm*/ Record[2],
3127 /*regparm*/ Record[3],
3128 static_cast<CallingConv>(Record[4]));
John McCalle23cf432010-12-14 08:05:40 +00003129
Eli Friedmana49218e2011-04-09 08:18:08 +00003130 unsigned Idx = 5;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003131 unsigned NumParams = Record[Idx++];
3132 llvm::SmallVector<QualType, 16> ParamTypes;
3133 for (unsigned I = 0; I != NumParams; ++I)
3134 ParamTypes.push_back(GetType(Record[Idx++]));
John McCalle23cf432010-12-14 08:05:40 +00003135
3136 EPI.Variadic = Record[Idx++];
3137 EPI.TypeQuals = Record[Idx++];
Douglas Gregorc938c162011-01-26 05:01:58 +00003138 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
Sebastian Redl60618fa2011-03-12 11:50:43 +00003139 ExceptionSpecificationType EST =
3140 static_cast<ExceptionSpecificationType>(Record[Idx++]);
3141 EPI.ExceptionSpecType = EST;
3142 if (EST == EST_Dynamic) {
3143 EPI.NumExceptions = Record[Idx++];
3144 llvm::SmallVector<QualType, 2> Exceptions;
3145 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
3146 Exceptions.push_back(GetType(Record[Idx++]));
3147 EPI.Exceptions = Exceptions.data();
3148 } else if (EST == EST_ComputedNoexcept) {
3149 EPI.NoexceptExpr = ReadExpr(*Loc.F);
3150 }
Jay Foadbeaaccd2009-05-21 09:52:38 +00003151 return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams,
John McCalle23cf432010-12-14 08:05:40 +00003152 EPI);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003153 }
3154
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003155 case TYPE_UNRESOLVED_USING:
John McCalled976492009-12-04 22:46:56 +00003156 return Context->getTypeDeclType(
3157 cast<UnresolvedUsingTypenameDecl>(GetDecl(Record[0])));
3158
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003159 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003160 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003161 Error("incorrect encoding of typedef type");
3162 return QualType();
3163 }
Richard Smith162e1c12011-04-15 14:24:37 +00003164 TypedefNameDecl *Decl = cast<TypedefNameDecl>(GetDecl(Record[0]));
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003165 QualType Canonical = GetType(Record[1]);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00003166 if (!Canonical.isNull())
3167 Canonical = Context->getCanonicalType(Canonical);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003168 return Context->getTypedefType(Decl, Canonical);
3169 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003170
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003171 case TYPE_TYPEOF_EXPR:
Sebastian Redlc3632732010-10-05 15:59:54 +00003172 return Context->getTypeOfExprType(ReadExpr(*Loc.F));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003173
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003174 case TYPE_TYPEOF: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003175 if (Record.size() != 1) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003176 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003177 return QualType();
3178 }
3179 QualType UnderlyingType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003180 return Context->getTypeOfType(UnderlyingType);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003181 }
Mike Stump1eb44332009-09-09 15:08:12 +00003182
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003183 case TYPE_DECLTYPE:
Sebastian Redlc3632732010-10-05 15:59:54 +00003184 return Context->getDecltypeType(ReadExpr(*Loc.F));
Anders Carlsson395b4752009-06-24 19:06:50 +00003185
Richard Smith34b41d92011-02-20 03:19:35 +00003186 case TYPE_AUTO:
3187 return Context->getAutoType(GetType(Record[0]));
3188
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003189 case TYPE_RECORD: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003190 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003191 Error("incorrect encoding of record type");
3192 return QualType();
3193 }
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003194 bool IsDependent = Record[0];
3195 QualType T = Context->getRecordType(cast<RecordDecl>(GetDecl(Record[1])));
John McCallf4c73712011-01-19 06:33:43 +00003196 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003197 return T;
3198 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003199
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003200 case TYPE_ENUM: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003201 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003202 Error("incorrect encoding of enum type");
3203 return QualType();
3204 }
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003205 bool IsDependent = Record[0];
3206 QualType T = Context->getEnumType(cast<EnumDecl>(GetDecl(Record[1])));
John McCallf4c73712011-01-19 06:33:43 +00003207 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003208 return T;
3209 }
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003210
John McCall9d156a72011-01-06 01:58:22 +00003211 case TYPE_ATTRIBUTED: {
3212 if (Record.size() != 3) {
3213 Error("incorrect encoding of attributed type");
3214 return QualType();
3215 }
3216 QualType modifiedType = GetType(Record[0]);
3217 QualType equivalentType = GetType(Record[1]);
3218 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
3219 return Context->getAttributedType(kind, modifiedType, equivalentType);
3220 }
3221
Abramo Bagnara075f8f12010-12-10 16:29:40 +00003222 case TYPE_PAREN: {
3223 if (Record.size() != 1) {
3224 Error("incorrect encoding of paren type");
3225 return QualType();
3226 }
3227 QualType InnerType = GetType(Record[0]);
3228 return Context->getParenType(InnerType);
3229 }
3230
Douglas Gregor7536dd52010-12-20 02:24:11 +00003231 case TYPE_PACK_EXPANSION: {
Douglas Gregorf9997a02011-02-01 15:24:58 +00003232 if (Record.size() != 2) {
Douglas Gregor7536dd52010-12-20 02:24:11 +00003233 Error("incorrect encoding of pack expansion type");
3234 return QualType();
3235 }
3236 QualType Pattern = GetType(Record[0]);
3237 if (Pattern.isNull())
3238 return QualType();
Douglas Gregorcded4f62011-01-14 17:04:44 +00003239 llvm::Optional<unsigned> NumExpansions;
3240 if (Record[1])
3241 NumExpansions = Record[1] - 1;
3242 return Context->getPackExpansionType(Pattern, NumExpansions);
Douglas Gregor7536dd52010-12-20 02:24:11 +00003243 }
3244
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003245 case TYPE_ELABORATED: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00003246 unsigned Idx = 0;
3247 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3248 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3249 QualType NamedType = GetType(Record[Idx++]);
3250 return Context->getElaboratedType(Keyword, NNS, NamedType);
John McCall7da24312009-09-05 00:15:47 +00003251 }
3252
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003253 case TYPE_OBJC_INTERFACE: {
Chris Lattnerc6fa4452009-04-22 06:45:28 +00003254 unsigned Idx = 0;
3255 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
John McCallc12c5bb2010-05-15 11:32:37 +00003256 return Context->getObjCInterfaceType(ItfD);
3257 }
3258
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003259 case TYPE_OBJC_OBJECT: {
John McCallc12c5bb2010-05-15 11:32:37 +00003260 unsigned Idx = 0;
3261 QualType Base = GetType(Record[Idx++]);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00003262 unsigned NumProtos = Record[Idx++];
3263 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
3264 for (unsigned I = 0; I != NumProtos; ++I)
3265 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
Michael J. Spencer20249a12010-10-21 03:16:25 +00003266 return Context->getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00003267 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003268
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003269 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00003270 unsigned Idx = 0;
John McCallc12c5bb2010-05-15 11:32:37 +00003271 QualType Pointee = GetType(Record[Idx++]);
3272 return Context->getObjCObjectPointerType(Pointee);
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00003273 }
Argyrios Kyrtzidis24fab412009-09-29 19:42:55 +00003274
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003275 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCall49a832b2009-10-18 09:09:24 +00003276 unsigned Idx = 0;
3277 QualType Parm = GetType(Record[Idx++]);
3278 QualType Replacement = GetType(Record[Idx++]);
3279 return
3280 Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
3281 Replacement);
3282 }
John McCall3cb0ebd2010-03-10 03:28:59 +00003283
Douglas Gregorc3069d62011-01-14 02:55:32 +00003284 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
3285 unsigned Idx = 0;
3286 QualType Parm = GetType(Record[Idx++]);
3287 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
3288 return Context->getSubstTemplateTypeParmPackType(
3289 cast<TemplateTypeParmType>(Parm),
3290 ArgPack);
3291 }
3292
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003293 case TYPE_INJECTED_CLASS_NAME: {
John McCall3cb0ebd2010-03-10 03:28:59 +00003294 CXXRecordDecl *D = cast<CXXRecordDecl>(GetDecl(Record[0]));
3295 QualType TST = GetType(Record[1]); // probably derivable
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00003296 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003297 // for AST reading, too much interdependencies.
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00003298 return
3299 QualType(new (*Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCall3cb0ebd2010-03-10 03:28:59 +00003300 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003301
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003302 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003303 unsigned Idx = 0;
3304 unsigned Depth = Record[Idx++];
3305 unsigned Index = Record[Idx++];
3306 bool Pack = Record[Idx++];
Chandler Carruth4fb86f82011-05-01 00:51:33 +00003307 TemplateTypeParmDecl *D =
3308 cast_or_null<TemplateTypeParmDecl>(GetDecl(Record[Idx++]));
3309 return Context->getTemplateTypeParmType(Depth, Index, Pack, D);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003310 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003311
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003312 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00003313 unsigned Idx = 0;
3314 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3315 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3316 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +00003317 QualType Canon = GetType(Record[Idx++]);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00003318 if (!Canon.isNull())
3319 Canon = Context->getCanonicalType(Canon);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +00003320 return Context->getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00003321 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003322
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003323 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00003324 unsigned Idx = 0;
3325 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3326 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3327 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
3328 unsigned NumArgs = Record[Idx++];
3329 llvm::SmallVector<TemplateArgument, 8> Args;
3330 Args.reserve(NumArgs);
3331 while (NumArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00003332 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00003333 return Context->getDependentTemplateSpecializationType(Keyword, NNS, Name,
3334 Args.size(), Args.data());
3335 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003336
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003337 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00003338 unsigned Idx = 0;
3339
3340 // ArrayType
3341 QualType ElementType = GetType(Record[Idx++]);
3342 ArrayType::ArraySizeModifier ASM
3343 = (ArrayType::ArraySizeModifier)Record[Idx++];
3344 unsigned IndexTypeQuals = Record[Idx++];
3345
3346 // DependentSizedArrayType
Sebastian Redlc3632732010-10-05 15:59:54 +00003347 Expr *NumElts = ReadExpr(*Loc.F);
3348 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00003349
3350 return Context->getDependentSizedArrayType(ElementType, NumElts, ASM,
3351 IndexTypeQuals, Brackets);
3352 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003353
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003354 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003355 unsigned Idx = 0;
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003356 bool IsDependent = Record[Idx++];
Douglas Gregor1aee05d2011-01-15 06:45:20 +00003357 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003358 llvm::SmallVector<TemplateArgument, 8> Args;
Sebastian Redlc3632732010-10-05 15:59:54 +00003359 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003360 QualType Canon = GetType(Record[Idx++]);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003361 QualType T;
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003362 if (Canon.isNull())
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003363 T = Context->getCanonicalTemplateSpecializationType(Name, Args.data(),
3364 Args.size());
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003365 else
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003366 T = Context->getTemplateSpecializationType(Name, Args.data(),
3367 Args.size(), Canon);
John McCallf4c73712011-01-19 06:33:43 +00003368 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003369 return T;
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003370 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003371 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003372 // Suppress a GCC warning
3373 return QualType();
3374}
3375
Sebastian Redlc3632732010-10-05 15:59:54 +00003376class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003377 ASTReader &Reader;
Sebastian Redlc3632732010-10-05 15:59:54 +00003378 ASTReader::PerFileData &F;
Sebastian Redl577d4792010-07-22 22:43:28 +00003379 llvm::BitstreamCursor &DeclsCursor;
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003380 const ASTReader::RecordData &Record;
John McCalla1ee0c52009-10-16 21:56:05 +00003381 unsigned &Idx;
3382
Sebastian Redlc3632732010-10-05 15:59:54 +00003383 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
3384 unsigned &I) {
3385 return Reader.ReadSourceLocation(F, R, I);
3386 }
3387
John McCalla1ee0c52009-10-16 21:56:05 +00003388public:
Sebastian Redlc3632732010-10-05 15:59:54 +00003389 TypeLocReader(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003390 const ASTReader::RecordData &Record, unsigned &Idx)
Sebastian Redlc3632732010-10-05 15:59:54 +00003391 : Reader(Reader), F(F), DeclsCursor(F.DeclsCursor), Record(Record), Idx(Idx)
3392 { }
John McCalla1ee0c52009-10-16 21:56:05 +00003393
John McCall51bd8032009-10-18 01:05:36 +00003394 // We want compile-time assurance that we've enumerated all of
3395 // these, so unfortunately we have to declare them first, then
3396 // define them out-of-line.
3397#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +00003398#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +00003399 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00003400#include "clang/AST/TypeLocNodes.def"
3401
John McCall51bd8032009-10-18 01:05:36 +00003402 void VisitFunctionTypeLoc(FunctionTypeLoc);
3403 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00003404};
3405
John McCall51bd8032009-10-18 01:05:36 +00003406void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCalla1ee0c52009-10-16 21:56:05 +00003407 // nothing to do
3408}
John McCall51bd8032009-10-18 01:05:36 +00003409void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003410 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
Douglas Gregorddf889a2010-01-18 18:04:31 +00003411 if (TL.needsExtraLocalData()) {
3412 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
3413 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
3414 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
3415 TL.setModeAttr(Record[Idx++]);
3416 }
John McCalla1ee0c52009-10-16 21:56:05 +00003417}
John McCall51bd8032009-10-18 01:05:36 +00003418void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003419 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003420}
John McCall51bd8032009-10-18 01:05:36 +00003421void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003422 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003423}
John McCall51bd8032009-10-18 01:05:36 +00003424void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003425 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003426}
John McCall51bd8032009-10-18 01:05:36 +00003427void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003428 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003429}
John McCall51bd8032009-10-18 01:05:36 +00003430void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003431 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003432}
John McCall51bd8032009-10-18 01:05:36 +00003433void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003434 TL.setStarLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +00003435 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003436}
John McCall51bd8032009-10-18 01:05:36 +00003437void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003438 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
3439 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003440 if (Record[Idx++])
Sebastian Redlc3632732010-10-05 15:59:54 +00003441 TL.setSizeExpr(Reader.ReadExpr(F));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003442 else
John McCall51bd8032009-10-18 01:05:36 +00003443 TL.setSizeExpr(0);
3444}
3445void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
3446 VisitArrayTypeLoc(TL);
3447}
3448void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
3449 VisitArrayTypeLoc(TL);
3450}
3451void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
3452 VisitArrayTypeLoc(TL);
3453}
3454void TypeLocReader::VisitDependentSizedArrayTypeLoc(
3455 DependentSizedArrayTypeLoc TL) {
3456 VisitArrayTypeLoc(TL);
3457}
3458void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
3459 DependentSizedExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003460 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003461}
3462void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003463 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003464}
3465void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003466 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003467}
3468void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnara796aa442011-03-12 11:17:06 +00003469 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
3470 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
Douglas Gregordab60ad2010-10-01 18:44:50 +00003471 TL.setTrailingReturn(Record[Idx++]);
John McCall51bd8032009-10-18 01:05:36 +00003472 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
John McCall86acc2a2009-10-23 01:28:53 +00003473 TL.setArg(i, cast_or_null<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
John McCall51bd8032009-10-18 01:05:36 +00003474 }
3475}
3476void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
3477 VisitFunctionTypeLoc(TL);
3478}
3479void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
3480 VisitFunctionTypeLoc(TL);
3481}
John McCalled976492009-12-04 22:46:56 +00003482void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003483 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalled976492009-12-04 22:46:56 +00003484}
John McCall51bd8032009-10-18 01:05:36 +00003485void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003486 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003487}
3488void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003489 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3490 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3491 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003492}
3493void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003494 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3495 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3496 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3497 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003498}
3499void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003500 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003501}
Richard Smith34b41d92011-02-20 03:19:35 +00003502void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
3503 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3504}
John McCall51bd8032009-10-18 01:05:36 +00003505void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003506 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003507}
3508void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003509 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003510}
John McCall9d156a72011-01-06 01:58:22 +00003511void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
3512 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
3513 if (TL.hasAttrOperand()) {
3514 SourceRange range;
3515 range.setBegin(ReadSourceLocation(Record, Idx));
3516 range.setEnd(ReadSourceLocation(Record, Idx));
3517 TL.setAttrOperandParensRange(range);
3518 }
3519 if (TL.hasAttrExprOperand()) {
3520 if (Record[Idx++])
3521 TL.setAttrExprOperand(Reader.ReadExpr(F));
3522 else
3523 TL.setAttrExprOperand(0);
3524 } else if (TL.hasAttrEnumOperand())
3525 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
3526}
John McCall51bd8032009-10-18 01:05:36 +00003527void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003528 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003529}
John McCall49a832b2009-10-18 09:09:24 +00003530void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
3531 SubstTemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003532 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall49a832b2009-10-18 09:09:24 +00003533}
Douglas Gregorc3069d62011-01-14 02:55:32 +00003534void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
3535 SubstTemplateTypeParmPackTypeLoc TL) {
3536 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3537}
John McCall51bd8032009-10-18 01:05:36 +00003538void TypeLocReader::VisitTemplateSpecializationTypeLoc(
3539 TemplateSpecializationTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003540 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
3541 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3542 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall833ca992009-10-29 08:12:44 +00003543 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
3544 TL.setArgLocInfo(i,
Sebastian Redlc3632732010-10-05 15:59:54 +00003545 Reader.GetTemplateArgumentLocInfo(F,
3546 TL.getTypePtr()->getArg(i).getKind(),
3547 Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003548}
Abramo Bagnara075f8f12010-12-10 16:29:40 +00003549void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
3550 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3551 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3552}
Abramo Bagnara465d41b2010-05-11 21:36:43 +00003553void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003554 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor9e876872011-03-01 18:12:44 +00003555 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003556}
John McCall3cb0ebd2010-03-10 03:28:59 +00003557void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003558 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall3cb0ebd2010-03-10 03:28:59 +00003559}
Douglas Gregor4714c122010-03-31 17:34:00 +00003560void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003561 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor2494dd02011-03-01 01:34:45 +00003562 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00003563 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003564}
John McCall33500952010-06-11 00:33:02 +00003565void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
3566 DependentTemplateSpecializationTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003567 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor94fdffa2011-03-01 20:11:18 +00003568 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00003569 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3570 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3571 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall33500952010-06-11 00:33:02 +00003572 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
3573 TL.setArgLocInfo(I,
Sebastian Redlc3632732010-10-05 15:59:54 +00003574 Reader.GetTemplateArgumentLocInfo(F,
3575 TL.getTypePtr()->getArg(I).getKind(),
3576 Record, Idx));
John McCall33500952010-06-11 00:33:02 +00003577}
Douglas Gregor7536dd52010-12-20 02:24:11 +00003578void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
3579 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
3580}
John McCall51bd8032009-10-18 01:05:36 +00003581void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003582 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallc12c5bb2010-05-15 11:32:37 +00003583}
3584void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
3585 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00003586 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3587 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003588 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Sebastian Redlc3632732010-10-05 15:59:54 +00003589 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003590}
John McCall54e14c42009-10-22 22:37:11 +00003591void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003592 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall54e14c42009-10-22 22:37:11 +00003593}
John McCalla1ee0c52009-10-16 21:56:05 +00003594
Sebastian Redlc3632732010-10-05 15:59:54 +00003595TypeSourceInfo *ASTReader::GetTypeSourceInfo(PerFileData &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00003596 const RecordData &Record,
John McCalla1ee0c52009-10-16 21:56:05 +00003597 unsigned &Idx) {
3598 QualType InfoTy = GetType(Record[Idx++]);
3599 if (InfoTy.isNull())
3600 return 0;
3601
John McCalla93c9342009-12-07 02:54:59 +00003602 TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy);
Sebastian Redlc3632732010-10-05 15:59:54 +00003603 TypeLocReader TLR(*this, F, Record, Idx);
John McCalla93c9342009-12-07 02:54:59 +00003604 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCalla1ee0c52009-10-16 21:56:05 +00003605 TLR.Visit(TL);
John McCalla93c9342009-12-07 02:54:59 +00003606 return TInfo;
John McCalla1ee0c52009-10-16 21:56:05 +00003607}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003608
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003609QualType ASTReader::GetType(TypeID ID) {
John McCall0953e762009-09-24 19:53:00 +00003610 unsigned FastQuals = ID & Qualifiers::FastMask;
3611 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003612
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003613 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003614 QualType T;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003615 switch ((PredefinedTypeIDs)Index) {
3616 case PREDEF_TYPE_NULL_ID: return QualType();
3617 case PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break;
3618 case PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003619
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003620 case PREDEF_TYPE_CHAR_U_ID:
3621 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregor2cf26342009-04-09 22:27:44 +00003622 // FIXME: Check that the signedness of CharTy is correct!
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003623 T = Context->CharTy;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003624 break;
3625
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003626 case PREDEF_TYPE_UCHAR_ID: T = Context->UnsignedCharTy; break;
3627 case PREDEF_TYPE_USHORT_ID: T = Context->UnsignedShortTy; break;
3628 case PREDEF_TYPE_UINT_ID: T = Context->UnsignedIntTy; break;
3629 case PREDEF_TYPE_ULONG_ID: T = Context->UnsignedLongTy; break;
3630 case PREDEF_TYPE_ULONGLONG_ID: T = Context->UnsignedLongLongTy; break;
3631 case PREDEF_TYPE_UINT128_ID: T = Context->UnsignedInt128Ty; break;
3632 case PREDEF_TYPE_SCHAR_ID: T = Context->SignedCharTy; break;
3633 case PREDEF_TYPE_WCHAR_ID: T = Context->WCharTy; break;
3634 case PREDEF_TYPE_SHORT_ID: T = Context->ShortTy; break;
3635 case PREDEF_TYPE_INT_ID: T = Context->IntTy; break;
3636 case PREDEF_TYPE_LONG_ID: T = Context->LongTy; break;
3637 case PREDEF_TYPE_LONGLONG_ID: T = Context->LongLongTy; break;
3638 case PREDEF_TYPE_INT128_ID: T = Context->Int128Ty; break;
3639 case PREDEF_TYPE_FLOAT_ID: T = Context->FloatTy; break;
3640 case PREDEF_TYPE_DOUBLE_ID: T = Context->DoubleTy; break;
3641 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy; break;
3642 case PREDEF_TYPE_OVERLOAD_ID: T = Context->OverloadTy; break;
John McCall864c0412011-04-26 20:42:42 +00003643 case PREDEF_TYPE_BOUND_MEMBER: T = Context->BoundMemberTy; break;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003644 case PREDEF_TYPE_DEPENDENT_ID: T = Context->DependentTy; break;
John McCall1de4d4e2011-04-07 08:22:57 +00003645 case PREDEF_TYPE_UNKNOWN_ANY: T = Context->UnknownAnyTy; break;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003646 case PREDEF_TYPE_NULLPTR_ID: T = Context->NullPtrTy; break;
3647 case PREDEF_TYPE_CHAR16_ID: T = Context->Char16Ty; break;
3648 case PREDEF_TYPE_CHAR32_ID: T = Context->Char32Ty; break;
3649 case PREDEF_TYPE_OBJC_ID: T = Context->ObjCBuiltinIdTy; break;
3650 case PREDEF_TYPE_OBJC_CLASS: T = Context->ObjCBuiltinClassTy; break;
3651 case PREDEF_TYPE_OBJC_SEL: T = Context->ObjCBuiltinSelTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003652 }
3653
3654 assert(!T.isNull() && "Unknown predefined type");
John McCall0953e762009-09-24 19:53:00 +00003655 return T.withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003656 }
3657
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003658 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003659 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl07a353c2010-07-14 20:26:45 +00003660 if (TypesLoaded[Index].isNull()) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003661 TypesLoaded[Index] = ReadTypeRecord(Index);
Douglas Gregor97475832010-10-05 18:37:06 +00003662 if (TypesLoaded[Index].isNull())
3663 return QualType();
3664
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003665 TypesLoaded[Index]->setFromAST();
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003666 TypeIdxs[TypesLoaded[Index]] = TypeIdx::fromTypeID(ID);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003667 if (DeserializationListener)
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003668 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1476ed42010-07-16 16:36:56 +00003669 TypesLoaded[Index]);
Sebastian Redl07a353c2010-07-14 20:26:45 +00003670 }
Mike Stump1eb44332009-09-09 15:08:12 +00003671
John McCall0953e762009-09-24 19:53:00 +00003672 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003673}
3674
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003675TypeID ASTReader::GetTypeID(QualType T) const {
3676 return MakeTypeID(T,
3677 std::bind1st(std::mem_fun(&ASTReader::GetTypeIdx), this));
3678}
3679
3680TypeIdx ASTReader::GetTypeIdx(QualType T) const {
3681 if (T.isNull())
3682 return TypeIdx();
3683 assert(!T.getLocalFastQualifiers());
3684
3685 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3686 // GetTypeIdx is mostly used for computing the hash of DeclarationNames and
3687 // comparing keys of ASTDeclContextNameLookupTable.
3688 // If the type didn't come from the AST file use a specially marked index
3689 // so that any hash/key comparison fail since no such index is stored
3690 // in a AST file.
3691 if (I == TypeIdxs.end())
3692 return TypeIdx(-1);
3693 return I->second;
3694}
3695
Douglas Gregor7c789c12010-10-29 22:39:52 +00003696unsigned ASTReader::getTotalNumCXXBaseSpecifiers() const {
3697 unsigned Result = 0;
3698 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
3699 Result += Chain[I]->LocalNumCXXBaseSpecifiers;
3700
3701 return Result;
3702}
3703
John McCall833ca992009-10-29 08:12:44 +00003704TemplateArgumentLocInfo
Sebastian Redlc3632732010-10-05 15:59:54 +00003705ASTReader::GetTemplateArgumentLocInfo(PerFileData &F,
3706 TemplateArgument::ArgKind Kind,
John McCall833ca992009-10-29 08:12:44 +00003707 const RecordData &Record,
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00003708 unsigned &Index) {
John McCall833ca992009-10-29 08:12:44 +00003709 switch (Kind) {
3710 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00003711 return ReadExpr(F);
John McCall833ca992009-10-29 08:12:44 +00003712 case TemplateArgument::Type:
Sebastian Redlc3632732010-10-05 15:59:54 +00003713 return GetTypeSourceInfo(F, Record, Index);
Douglas Gregor788cd062009-11-11 01:00:40 +00003714 case TemplateArgument::Template: {
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003715 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
3716 Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00003717 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003718 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregora7fc9012011-01-05 18:58:31 +00003719 SourceLocation());
3720 }
3721 case TemplateArgument::TemplateExpansion: {
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003722 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
3723 Index);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003724 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorba68eca2011-01-05 17:40:24 +00003725 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003726 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregorba68eca2011-01-05 17:40:24 +00003727 EllipsisLoc);
Douglas Gregor788cd062009-11-11 01:00:40 +00003728 }
John McCall833ca992009-10-29 08:12:44 +00003729 case TemplateArgument::Null:
3730 case TemplateArgument::Integral:
3731 case TemplateArgument::Declaration:
3732 case TemplateArgument::Pack:
3733 return TemplateArgumentLocInfo();
3734 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00003735 llvm_unreachable("unexpected template argument loc");
John McCall833ca992009-10-29 08:12:44 +00003736 return TemplateArgumentLocInfo();
3737}
3738
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003739TemplateArgumentLoc
Sebastian Redlc3632732010-10-05 15:59:54 +00003740ASTReader::ReadTemplateArgumentLoc(PerFileData &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00003741 const RecordData &Record, unsigned &Index) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003742 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003743
3744 if (Arg.getKind() == TemplateArgument::Expression) {
3745 if (Record[Index++]) // bool InfoHasSameExpr.
3746 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
3747 }
Sebastian Redlc3632732010-10-05 15:59:54 +00003748 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00003749 Record, Index));
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003750}
3751
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003752Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall76bd1f32010-06-01 09:23:16 +00003753 return GetDecl(ID);
3754}
3755
Douglas Gregor7c789c12010-10-29 22:39:52 +00003756uint64_t
3757ASTReader::GetCXXBaseSpecifiersOffset(serialization::CXXBaseSpecifiersID ID) {
3758 if (ID == 0)
3759 return 0;
3760
3761 --ID;
3762 uint64_t Offset = 0;
3763 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Anders Carlssonf25330b2011-03-09 05:09:32 +00003764 PerFileData &F = *Chain[N - I - 1];
3765
3766 if (ID < F.LocalNumCXXBaseSpecifiers)
3767 return Offset + F.CXXBaseSpecifiersOffsets[ID];
Douglas Gregor7c789c12010-10-29 22:39:52 +00003768
Anders Carlssonf25330b2011-03-09 05:09:32 +00003769 ID -= F.LocalNumCXXBaseSpecifiers;
3770 Offset += F.SizeInBits;
Douglas Gregor7c789c12010-10-29 22:39:52 +00003771 }
3772
3773 assert(false && "CXXBaseSpecifiers not found");
3774 return 0;
3775}
3776
3777CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
3778 // Figure out which AST file contains this offset.
3779 PerFileData *F = 0;
3780 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Anders Carlssonf25330b2011-03-09 05:09:32 +00003781 if (Offset < Chain[N - I - 1]->SizeInBits) {
3782 F = Chain[N - I - 1];
Douglas Gregor7c789c12010-10-29 22:39:52 +00003783 break;
3784 }
3785
Anders Carlssonf25330b2011-03-09 05:09:32 +00003786 Offset -= Chain[N - I - 1]->SizeInBits;
Douglas Gregor7c789c12010-10-29 22:39:52 +00003787 }
Anders Carlssonf25330b2011-03-09 05:09:32 +00003788
Douglas Gregor7c789c12010-10-29 22:39:52 +00003789 if (!F) {
3790 Error("Malformed AST file: C++ base specifiers at impossible offset");
3791 return 0;
3792 }
3793
3794 llvm::BitstreamCursor &Cursor = F->DeclsCursor;
3795 SavedStreamPosition SavedPosition(Cursor);
3796 Cursor.JumpToBit(Offset);
3797 ReadingKindTracker ReadingKind(Read_Decl, *this);
3798 RecordData Record;
3799 unsigned Code = Cursor.ReadCode();
3800 unsigned RecCode = Cursor.ReadRecord(Code, Record);
3801 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
3802 Error("Malformed AST file: missing C++ base specifiers");
3803 return 0;
3804 }
3805
3806 unsigned Idx = 0;
3807 unsigned NumBases = Record[Idx++];
3808 void *Mem = Context->Allocate(sizeof(CXXBaseSpecifier) * NumBases);
3809 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
3810 for (unsigned I = 0; I != NumBases; ++I)
3811 Bases[I] = ReadCXXBaseSpecifier(*F, Record, Idx);
3812 return Bases;
3813}
3814
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003815TranslationUnitDecl *ASTReader::GetTranslationUnitDecl() {
Sebastian Redl30c514c2010-07-14 23:45:08 +00003816 if (!DeclsLoaded[0]) {
Sebastian Redle1dde812010-08-24 00:50:04 +00003817 ReadDeclRecord(0, 1);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003818 if (DeserializationListener)
Sebastian Redl1476ed42010-07-16 16:36:56 +00003819 DeserializationListener->DeclRead(1, DeclsLoaded[0]);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003820 }
Argyrios Kyrtzidis8871a442010-07-08 17:13:02 +00003821
3822 return cast<TranslationUnitDecl>(DeclsLoaded[0]);
3823}
3824
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003825Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003826 if (ID == 0)
3827 return 0;
3828
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003829 if (ID > DeclsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003830 Error("declaration ID out-of-range for AST file");
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003831 return 0;
3832 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003833
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003834 unsigned Index = ID - 1;
Sebastian Redl30c514c2010-07-14 23:45:08 +00003835 if (!DeclsLoaded[Index]) {
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00003836 ReadDeclRecord(Index, ID);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003837 if (DeserializationListener)
3838 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
3839 }
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003840
3841 return DeclsLoaded[Index];
Douglas Gregor2cf26342009-04-09 22:27:44 +00003842}
3843
Chris Lattner887e2b32009-04-27 05:46:25 +00003844/// \brief Resolve the offset of a statement into a statement.
3845///
3846/// This operation will read a new statement from the external
3847/// source each time it is called, and is meant to be used via a
3848/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003849Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00003850 // Switch case IDs are per Decl.
3851 ClearSwitchCaseIDs();
3852
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003853 // Offset here is a global offset across the entire chain.
3854 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3855 PerFileData &F = *Chain[N - I - 1];
3856 if (Offset < F.SizeInBits) {
3857 // Since we know that this statement is part of a decl, make sure to use
3858 // the decl cursor to read it.
3859 F.DeclsCursor.JumpToBit(Offset);
Sebastian Redlc3632732010-10-05 15:59:54 +00003860 return ReadStmtFromStream(F);
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003861 }
3862 Offset -= F.SizeInBits;
3863 }
3864 llvm_unreachable("Broken chain");
Douglas Gregor250fc9c2009-04-18 00:07:54 +00003865}
3866
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003867bool ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00003868 bool (*isKindWeWant)(Decl::Kind),
John McCall76bd1f32010-06-01 09:23:16 +00003869 llvm::SmallVectorImpl<Decl*> &Decls) {
Mike Stump1eb44332009-09-09 15:08:12 +00003870 assert(DC->hasExternalLexicalStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00003871 "DeclContext has no lexical decls in storage");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003872
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003873 // There might be lexical decls in multiple parts of the chain, for the TU
3874 // at least.
Sebastian Redl4a9eb262010-09-28 02:24:44 +00003875 // DeclContextOffsets might reallocate as we load additional decls below,
3876 // so make a copy of the vector.
3877 DeclContextInfos Infos = DeclContextOffsets[DC];
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003878 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3879 I != E; ++I) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003880 // IDs can be 0 if this context doesn't contain declarations.
3881 if (!I->LexicalDecls)
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003882 continue;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003883
3884 // Load all of the declaration IDs
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00003885 for (const KindDeclIDPair *ID = I->LexicalDecls,
3886 *IDE = ID + I->NumLexicalDecls; ID != IDE; ++ID) {
3887 if (isKindWeWant && !isKindWeWant((Decl::Kind)ID->first))
3888 continue;
3889
3890 Decl *D = GetDecl(ID->second);
Sebastian Redl4a9eb262010-09-28 02:24:44 +00003891 assert(D && "Null decl in lexical decls");
3892 Decls.push_back(D);
3893 }
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003894 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003895
Douglas Gregor25123082009-04-22 22:34:57 +00003896 ++NumLexicalDeclContextsRead;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003897 return false;
3898}
3899
John McCall76bd1f32010-06-01 09:23:16 +00003900DeclContext::lookup_result
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003901ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +00003902 DeclarationName Name) {
Mike Stump1eb44332009-09-09 15:08:12 +00003903 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00003904 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003905 if (!Name)
3906 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
3907 DeclContext::lookup_iterator(0));
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003908
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003909 llvm::SmallVector<NamedDecl *, 64> Decls;
Sebastian Redl8b122732010-08-24 00:49:55 +00003910 // There might be visible decls in multiple parts of the chain, for the TU
Sebastian Redl5967d622010-08-24 00:50:16 +00003911 // and namespaces. For any given name, the last available results replace
3912 // all earlier ones. For this reason, we walk in reverse.
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003913 DeclContextInfos &Infos = DeclContextOffsets[DC];
Sebastian Redl5967d622010-08-24 00:50:16 +00003914 for (DeclContextInfos::reverse_iterator I = Infos.rbegin(), E = Infos.rend();
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003915 I != E; ++I) {
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003916 if (!I->NameLookupTableData)
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003917 continue;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003918
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003919 ASTDeclContextNameLookupTable *LookupTable =
3920 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3921 ASTDeclContextNameLookupTable::iterator Pos = LookupTable->find(Name);
3922 if (Pos == LookupTable->end())
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003923 continue;
3924
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003925 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
3926 for (; Data.first != Data.second; ++Data.first)
3927 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
Sebastian Redl5967d622010-08-24 00:50:16 +00003928 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003929 }
3930
Douglas Gregor25123082009-04-22 22:34:57 +00003931 ++NumVisibleDeclContextsRead;
John McCall76bd1f32010-06-01 09:23:16 +00003932
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003933 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall76bd1f32010-06-01 09:23:16 +00003934 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003935}
3936
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +00003937void ASTReader::MaterializeVisibleDecls(const DeclContext *DC) {
3938 assert(DC->hasExternalVisibleStorage() &&
3939 "DeclContext has no visible decls in storage");
3940
3941 llvm::SmallVector<NamedDecl *, 64> Decls;
3942 // There might be visible decls in multiple parts of the chain, for the TU
3943 // and namespaces.
3944 DeclContextInfos &Infos = DeclContextOffsets[DC];
3945 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3946 I != E; ++I) {
3947 if (!I->NameLookupTableData)
3948 continue;
3949
3950 ASTDeclContextNameLookupTable *LookupTable =
3951 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3952 for (ASTDeclContextNameLookupTable::item_iterator
3953 ItemI = LookupTable->item_begin(),
3954 ItemEnd = LookupTable->item_end() ; ItemI != ItemEnd; ++ItemI) {
3955 ASTDeclContextNameLookupTable::item_iterator::value_type Val
3956 = *ItemI;
3957 ASTDeclContextNameLookupTrait::data_type Data = Val.second;
3958 Decls.clear();
3959 for (; Data.first != Data.second; ++Data.first)
3960 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
3961 MaterializeVisibleDeclsForName(DC, Val.first, Decls);
3962 }
3963 }
3964}
3965
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003966void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003967 assert(Consumer);
3968 while (!InterestingDecls.empty()) {
3969 DeclGroupRef DG(InterestingDecls.front());
3970 InterestingDecls.pop_front();
Sebastian Redl27372b42010-08-11 18:52:41 +00003971 Consumer->HandleInterestingDecl(DG);
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003972 }
3973}
3974
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003975void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor0af2ca42009-04-22 19:09:20 +00003976 this->Consumer = Consumer;
3977
Douglas Gregorfdd01722009-04-14 00:24:19 +00003978 if (!Consumer)
3979 return;
3980
3981 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003982 // Force deserialization of this decl, which will cause it to be queued for
3983 // passing to the consumer.
Daniel Dunbar04a0b502009-09-17 03:06:44 +00003984 GetDecl(ExternalDefinitions[I]);
Douglas Gregorfdd01722009-04-14 00:24:19 +00003985 }
Douglas Gregorc62a2fe2009-04-25 00:41:30 +00003986
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003987 PassInterestingDeclsToConsumer();
Douglas Gregorfdd01722009-04-14 00:24:19 +00003988}
3989
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003990void ASTReader::PrintStats() {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003991 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregor2cf26342009-04-09 22:27:44 +00003992
Mike Stump1eb44332009-09-09 15:08:12 +00003993 unsigned NumTypesLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003994 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall0953e762009-09-24 19:53:00 +00003995 QualType());
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003996 unsigned NumDeclsLoaded
3997 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
3998 (Decl *)0);
3999 unsigned NumIdentifiersLoaded
4000 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
4001 IdentifiersLoaded.end(),
4002 (IdentifierInfo *)0);
Mike Stump1eb44332009-09-09 15:08:12 +00004003 unsigned NumSelectorsLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00004004 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
4005 SelectorsLoaded.end(),
4006 Selector());
Douglas Gregor2d41cc12009-04-13 20:50:16 +00004007
Douglas Gregor4fed3f42009-04-27 18:38:38 +00004008 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
4009 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00004010 if (TotalNumSLocEntries)
4011 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
4012 NumSLocEntriesRead, TotalNumSLocEntries,
4013 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004014 if (!TypesLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00004015 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004016 NumTypesLoaded, (unsigned)TypesLoaded.size(),
4017 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
4018 if (!DeclsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00004019 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004020 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
4021 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00004022 if (!IdentifiersLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00004023 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00004024 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
4025 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Sebastian Redl725cd962010-08-04 20:40:17 +00004026 if (!SelectorsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00004027 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redl725cd962010-08-04 20:40:17 +00004028 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
4029 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor83941df2009-04-25 17:48:32 +00004030 if (TotalNumStatements)
4031 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
4032 NumStatementsRead, TotalNumStatements,
4033 ((float)NumStatementsRead/TotalNumStatements * 100));
4034 if (TotalNumMacros)
4035 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
4036 NumMacrosRead, TotalNumMacros,
4037 ((float)NumMacrosRead/TotalNumMacros * 100));
4038 if (TotalLexicalDeclContexts)
4039 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
4040 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
4041 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
4042 * 100));
4043 if (TotalVisibleDeclContexts)
4044 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
4045 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
4046 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
4047 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00004048 if (TotalNumMethodPoolEntries) {
Douglas Gregor83941df2009-04-25 17:48:32 +00004049 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redlfa78dec2010-08-04 21:22:45 +00004050 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
4051 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor83941df2009-04-25 17:48:32 +00004052 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00004053 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor83941df2009-04-25 17:48:32 +00004054 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00004055 std::fprintf(stderr, "\n");
4056}
4057
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00004058/// Return the amount of memory used by memory buffers, breaking down
4059/// by heap-backed versus mmap'ed memory.
4060void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
4061 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
4062 if (llvm::MemoryBuffer *buf = Chain[i]->Buffer.get()) {
4063 size_t bytes = buf->getBufferSize();
4064 switch (buf->getBufferKind()) {
4065 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
4066 sizes.malloc_bytes += bytes;
4067 break;
4068 case llvm::MemoryBuffer::MemoryBuffer_MMap:
4069 sizes.mmap_bytes += bytes;
4070 break;
4071 }
4072 }
4073}
4074
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004075void ASTReader::InitializeSema(Sema &S) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00004076 SemaObj = &S;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00004077 S.ExternalSource = this;
4078
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00004079 // Makes sure any declarations that were deserialized "too early"
4080 // still get added to the identifier's declaration chains.
Douglas Gregor76dc8892010-09-24 23:29:12 +00004081 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
4082 if (SemaObj->TUScope)
John McCalld226f652010-08-21 09:40:31 +00004083 SemaObj->TUScope->AddDecl(PreloadedDecls[I]);
Douglas Gregor76dc8892010-09-24 23:29:12 +00004084
4085 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregor668c1a42009-04-21 22:25:48 +00004086 }
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00004087 PreloadedDecls.clear();
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00004088
4089 // If there were any tentative definitions, deserialize them and add
Sebastian Redle9d12b62010-01-31 22:27:38 +00004090 // them to Sema's list of tentative definitions.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00004091 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
4092 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
Sebastian Redle9d12b62010-01-31 22:27:38 +00004093 SemaObj->TentativeDefinitions.push_back(Var);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00004094 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00004095
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00004096 // If there were any unused file scoped decls, deserialize them and add to
4097 // Sema's list of unused file scoped decls.
4098 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
4099 DeclaratorDecl *D = cast<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
4100 SemaObj->UnusedFileScopedDecls.push_back(D);
Tanya Lattnere6bbc012010-02-12 00:07:30 +00004101 }
Douglas Gregor14c22f22009-04-22 22:18:58 +00004102
4103 // If there were any locally-scoped external declarations,
4104 // deserialize them and add them to Sema's table of locally-scoped
4105 // external declarations.
4106 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
4107 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
4108 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
4109 }
Douglas Gregorb81c1702009-04-27 20:06:05 +00004110
4111 // If there were any ext_vector type declarations, deserialize them
4112 // and add them to Sema's vector of such declarations.
4113 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I)
4114 SemaObj->ExtVectorDecls.push_back(
Richard Smith162e1c12011-04-15 14:24:37 +00004115 cast<TypedefNameDecl>(GetDecl(ExtVectorDecls[I])));
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004116
4117 // FIXME: Do VTable uses and dynamic classes deserialize too much ?
4118 // Can we cut them down before writing them ?
4119
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004120 // If there were any dynamic classes declarations, deserialize them
4121 // and add them to Sema's vector of such declarations.
4122 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I)
4123 SemaObj->DynamicClasses.push_back(
4124 cast<CXXRecordDecl>(GetDecl(DynamicClasses[I])));
Fariborz Jahanian32019832010-07-23 19:11:11 +00004125
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00004126 // Load the offsets of the declarations that Sema references.
4127 // They will be lazily deserialized when needed.
4128 if (!SemaDeclRefs.empty()) {
4129 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
4130 SemaObj->StdNamespace = SemaDeclRefs[0];
4131 SemaObj->StdBadAlloc = SemaDeclRefs[1];
4132 }
4133
Sebastian Redlc3632732010-10-05 15:59:54 +00004134 for (PerFileData *F = FirstInSource; F; F = F->NextInSource) {
4135
4136 // If there are @selector references added them to its pool. This is for
4137 // implementation of -Wselector.
4138 if (!F->ReferencedSelectorsData.empty()) {
4139 unsigned int DataSize = F->ReferencedSelectorsData.size()-1;
4140 unsigned I = 0;
4141 while (I < DataSize) {
4142 Selector Sel = DecodeSelector(F->ReferencedSelectorsData[I++]);
4143 SourceLocation SelLoc = ReadSourceLocation(
4144 *F, F->ReferencedSelectorsData, I);
4145 SemaObj->ReferencedSelectors.insert(std::make_pair(Sel, SelLoc));
4146 }
4147 }
Sebastian Redlc3632732010-10-05 15:59:54 +00004148 }
4149
Sebastian Redlc1d3ffb2011-04-24 16:27:30 +00004150 // The special data sets below always come from the most recent PCH,
Sebastian Redlc3632732010-10-05 15:59:54 +00004151 // which is at the front of the chain.
4152 PerFileData &F = *Chain.front();
4153
Sebastian Redlc1d3ffb2011-04-24 16:27:30 +00004154 // If there were any pending implicit instantiations, deserialize them
4155 // and add them to Sema's queue of such instantiations.
4156 assert(F.PendingInstantiations.size() % 2 == 0 &&
4157 "Expected pairs of entries");
4158 for (unsigned Idx = 0, N = F.PendingInstantiations.size(); Idx < N;) {
4159 ValueDecl *D=cast<ValueDecl>(GetDecl(F.PendingInstantiations[Idx++]));
4160 SourceLocation Loc = ReadSourceLocation(F, F.PendingInstantiations,Idx);
4161 SemaObj->PendingInstantiations.push_back(std::make_pair(D, Loc));
4162 }
4163
Sebastian Redlc3632732010-10-05 15:59:54 +00004164 // If there were any weak undeclared identifiers, deserialize them and add to
4165 // Sema's list of weak undeclared identifiers.
4166 if (!WeakUndeclaredIdentifiers.empty()) {
4167 unsigned Idx = 0;
4168 for (unsigned I = 0, N = WeakUndeclaredIdentifiers[Idx++]; I != N; ++I) {
4169 IdentifierInfo *WeakId = GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
4170 IdentifierInfo *AliasId= GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
4171 SourceLocation Loc = ReadSourceLocation(F, WeakUndeclaredIdentifiers,Idx);
4172 bool Used = WeakUndeclaredIdentifiers[Idx++];
4173 Sema::WeakInfo WI(AliasId, Loc);
4174 WI.setUsed(Used);
4175 SemaObj->WeakUndeclaredIdentifiers.insert(std::make_pair(WeakId, WI));
4176 }
4177 }
4178
4179 // If there were any VTable uses, deserialize the information and add it
4180 // to Sema's vector and map of VTable uses.
4181 if (!VTableUses.empty()) {
4182 unsigned Idx = 0;
4183 for (unsigned I = 0, N = VTableUses[Idx++]; I != N; ++I) {
4184 CXXRecordDecl *Class = cast<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
4185 SourceLocation Loc = ReadSourceLocation(F, VTableUses, Idx);
4186 bool DefinitionRequired = VTableUses[Idx++];
4187 SemaObj->VTableUses.push_back(std::make_pair(Class, Loc));
4188 SemaObj->VTablesUsed[Class] = DefinitionRequired;
Fariborz Jahanian32019832010-07-23 19:11:11 +00004189 }
4190 }
Peter Collingbourne84bccea2011-02-15 19:46:30 +00004191
4192 if (!FPPragmaOptions.empty()) {
4193 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
4194 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
4195 }
4196
4197 if (!OpenCLExtensions.empty()) {
4198 unsigned I = 0;
4199#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
4200#include "clang/Basic/OpenCLExtensions.def"
4201
4202 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
4203 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00004204}
4205
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004206IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Sebastian Redld8c5abb2010-08-02 18:30:12 +00004207 // Try to find this name within our on-disk hash tables. We start with the
4208 // most recent one, since that one contains the most up-to-date info.
Sebastian Redld27d3fc2010-07-21 22:31:37 +00004209 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004210 ASTIdentifierLookupTable *IdTable
4211 = (ASTIdentifierLookupTable *)Chain[I]->IdentifierLookupTable;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00004212 if (!IdTable)
4213 continue;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00004214 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004215 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00004216 if (Pos == IdTable->end())
4217 continue;
Douglas Gregor668c1a42009-04-21 22:25:48 +00004218
Sebastian Redld27d3fc2010-07-21 22:31:37 +00004219 // Dereferencing the iterator has the effect of building the
4220 // IdentifierInfo node and populating it with the various
4221 // declarations it needs.
Sebastian Redld8c5abb2010-08-02 18:30:12 +00004222 return *Pos;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00004223 }
Sebastian Redld8c5abb2010-08-02 18:30:12 +00004224 return 0;
Douglas Gregor668c1a42009-04-21 22:25:48 +00004225}
4226
Douglas Gregor95f42922010-10-14 22:11:03 +00004227namespace clang {
4228 /// \brief An identifier-lookup iterator that enumerates all of the
4229 /// identifiers stored within a set of AST files.
4230 class ASTIdentifierIterator : public IdentifierIterator {
4231 /// \brief The AST reader whose identifiers are being enumerated.
4232 const ASTReader &Reader;
4233
4234 /// \brief The current index into the chain of AST files stored in
4235 /// the AST reader.
4236 unsigned Index;
4237
4238 /// \brief The current position within the identifier lookup table
4239 /// of the current AST file.
4240 ASTIdentifierLookupTable::key_iterator Current;
4241
4242 /// \brief The end position within the identifier lookup table of
4243 /// the current AST file.
4244 ASTIdentifierLookupTable::key_iterator End;
4245
4246 public:
4247 explicit ASTIdentifierIterator(const ASTReader &Reader);
4248
4249 virtual llvm::StringRef Next();
4250 };
4251}
4252
4253ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
4254 : Reader(Reader), Index(Reader.Chain.size() - 1) {
4255 ASTIdentifierLookupTable *IdTable
4256 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
4257 Current = IdTable->key_begin();
4258 End = IdTable->key_end();
4259}
4260
4261llvm::StringRef ASTIdentifierIterator::Next() {
4262 while (Current == End) {
4263 // If we have exhausted all of our AST files, we're done.
4264 if (Index == 0)
4265 return llvm::StringRef();
4266
4267 --Index;
4268 ASTIdentifierLookupTable *IdTable
4269 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
4270 Current = IdTable->key_begin();
4271 End = IdTable->key_end();
4272 }
4273
4274 // We have any identifiers remaining in the current AST file; return
4275 // the next one.
4276 std::pair<const char*, unsigned> Key = *Current;
4277 ++Current;
4278 return llvm::StringRef(Key.first, Key.second);
4279}
4280
4281IdentifierIterator *ASTReader::getIdentifiers() const {
4282 return new ASTIdentifierIterator(*this);
4283}
4284
Mike Stump1eb44332009-09-09 15:08:12 +00004285std::pair<ObjCMethodList, ObjCMethodList>
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004286ASTReader::ReadMethodPool(Selector Sel) {
Sebastian Redl725cd962010-08-04 20:40:17 +00004287 // Find this selector in a hash table. We want to find the most recent entry.
4288 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4289 PerFileData &F = *Chain[I];
4290 if (!F.SelectorLookupTable)
4291 continue;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00004292
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004293 ASTSelectorLookupTable *PoolTable
4294 = (ASTSelectorLookupTable*)F.SelectorLookupTable;
4295 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
Sebastian Redl725cd962010-08-04 20:40:17 +00004296 if (Pos != PoolTable->end()) {
4297 ++NumSelectorsRead;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00004298 // FIXME: Not quite happy with the statistics here. We probably should
4299 // disable this tracking when called via LoadSelector.
4300 // Also, should entries without methods count as misses?
4301 ++NumMethodPoolEntriesRead;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004302 ASTSelectorLookupTrait::data_type Data = *Pos;
Sebastian Redl725cd962010-08-04 20:40:17 +00004303 if (DeserializationListener)
4304 DeserializationListener->SelectorRead(Data.ID, Sel);
4305 return std::make_pair(Data.Instance, Data.Factory);
4306 }
Douglas Gregor83941df2009-04-25 17:48:32 +00004307 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00004308
Sebastian Redlfa78dec2010-08-04 21:22:45 +00004309 ++NumMethodPoolMisses;
Sebastian Redl725cd962010-08-04 20:40:17 +00004310 return std::pair<ObjCMethodList, ObjCMethodList>();
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00004311}
4312
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004313void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redle58aa892010-08-04 18:21:41 +00004314 // It would be complicated to avoid reading the methods anyway. So don't.
4315 ReadMethodPool(Sel);
4316}
4317
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004318void ASTReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00004319 assert(ID && "Non-zero identifier ID required");
Douglas Gregora02b1472009-04-28 21:53:25 +00004320 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00004321 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004322 if (DeserializationListener)
4323 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregor668c1a42009-04-21 22:25:48 +00004324}
4325
Douglas Gregord89275b2009-07-06 18:54:52 +00004326/// \brief Set the globally-visible declarations associated with the given
4327/// identifier.
4328///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004329/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump1eb44332009-09-09 15:08:12 +00004330/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregord89275b2009-07-06 18:54:52 +00004331/// them.
4332///
4333/// \param II an IdentifierInfo that refers to one or more globally-visible
4334/// declarations.
4335///
4336/// \param DeclIDs the set of declaration IDs with the name @p II that are
4337/// visible at global scope.
4338///
4339/// \param Nonrecursive should be true to indicate that the caller knows that
4340/// this call is non-recursive, and therefore the globally-visible declarations
4341/// will not be placed onto the pending queue.
Mike Stump1eb44332009-09-09 15:08:12 +00004342void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004343ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Douglas Gregord89275b2009-07-06 18:54:52 +00004344 const llvm::SmallVectorImpl<uint32_t> &DeclIDs,
4345 bool Nonrecursive) {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004346 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregord89275b2009-07-06 18:54:52 +00004347 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
4348 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
4349 PII.II = II;
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00004350 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregord89275b2009-07-06 18:54:52 +00004351 return;
4352 }
Mike Stump1eb44332009-09-09 15:08:12 +00004353
Douglas Gregord89275b2009-07-06 18:54:52 +00004354 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
4355 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
4356 if (SemaObj) {
Douglas Gregor914ed9d2010-08-13 03:15:25 +00004357 if (SemaObj->TUScope) {
4358 // Introduce this declaration into the translation-unit scope
4359 // and add it to the declaration chain for this identifier, so
4360 // that (unqualified) name lookup will find it.
John McCalld226f652010-08-21 09:40:31 +00004361 SemaObj->TUScope->AddDecl(D);
Douglas Gregor914ed9d2010-08-13 03:15:25 +00004362 }
Douglas Gregor76dc8892010-09-24 23:29:12 +00004363 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
Douglas Gregord89275b2009-07-06 18:54:52 +00004364 } else {
4365 // Queue this declaration so that it will be added to the
4366 // translation unit scope and identifier's declaration chain
4367 // once a Sema object is known.
4368 PreloadedDecls.push_back(D);
4369 }
4370 }
4371}
4372
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004373IdentifierInfo *ASTReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00004374 if (ID == 0)
4375 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00004376
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00004377 if (IdentifiersLoaded.empty()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004378 Error("no identifier table in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00004379 return 0;
4380 }
Mike Stump1eb44332009-09-09 15:08:12 +00004381
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00004382 assert(PP && "Forgot to set Preprocessor ?");
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00004383 ID -= 1;
4384 if (!IdentifiersLoaded[ID]) {
4385 unsigned Index = ID;
4386 const char *Str = 0;
4387 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4388 PerFileData *F = Chain[N - I - 1];
4389 if (Index < F->LocalNumIdentifiers) {
4390 uint32_t Offset = F->IdentifierOffsets[Index];
4391 Str = F->IdentifierTableData + Offset;
4392 break;
4393 }
4394 Index -= F->LocalNumIdentifiers;
4395 }
4396 assert(Str && "Broken Chain");
Douglas Gregord6595a42009-04-25 21:04:17 +00004397
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004398 // All of the strings in the AST file are preceded by a 16-bit length.
4399 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenek231bc0b2009-10-23 04:45:31 +00004400 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
4401 // unsigned integers. This is important to avoid integer overflow when
4402 // we cast them to 'unsigned'.
Ted Kremenekff1ea462009-10-23 03:57:22 +00004403 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregor02fc7512009-04-28 20:01:51 +00004404 unsigned StrLen = (((unsigned) StrLenPtr[0])
4405 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00004406 IdentifiersLoaded[ID]
Kovarththanan Rajaratnam811f4262010-03-12 10:32:27 +00004407 = &PP->getIdentifierTable().get(Str, StrLen);
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004408 if (DeserializationListener)
4409 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregorafaf3082009-04-11 00:14:32 +00004410 }
Mike Stump1eb44332009-09-09 15:08:12 +00004411
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00004412 return IdentifiersLoaded[ID];
Douglas Gregor2cf26342009-04-09 22:27:44 +00004413}
4414
Douglas Gregore23ac652011-04-20 00:21:03 +00004415bool ASTReader::ReadSLocEntry(unsigned ID) {
4416 return ReadSLocEntryRecord(ID) != Success;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00004417}
4418
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004419Selector ASTReader::DecodeSelector(unsigned ID) {
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004420 if (ID == 0)
4421 return Selector();
Mike Stump1eb44332009-09-09 15:08:12 +00004422
Sebastian Redl725cd962010-08-04 20:40:17 +00004423 if (ID > SelectorsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004424 Error("selector ID out of range in AST file");
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004425 return Selector();
4426 }
Douglas Gregor83941df2009-04-25 17:48:32 +00004427
Sebastian Redl725cd962010-08-04 20:40:17 +00004428 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor83941df2009-04-25 17:48:32 +00004429 // Load this selector from the selector table.
Sebastian Redl725cd962010-08-04 20:40:17 +00004430 unsigned Idx = ID - 1;
4431 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4432 PerFileData &F = *Chain[N - I - 1];
4433 if (Idx < F.LocalNumSelectors) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004434 ASTSelectorLookupTrait Trait(*this);
Sebastian Redl725cd962010-08-04 20:40:17 +00004435 SelectorsLoaded[ID - 1] =
4436 Trait.ReadKey(F.SelectorLookupTableData + F.SelectorOffsets[Idx], 0);
4437 if (DeserializationListener)
4438 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
4439 break;
4440 }
4441 Idx -= F.LocalNumSelectors;
4442 }
Douglas Gregor83941df2009-04-25 17:48:32 +00004443 }
4444
Sebastian Redl725cd962010-08-04 20:40:17 +00004445 return SelectorsLoaded[ID - 1];
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004446}
4447
Michael J. Spencer20249a12010-10-21 03:16:25 +00004448Selector ASTReader::GetExternalSelector(uint32_t ID) {
Douglas Gregor719770d2010-04-06 17:30:22 +00004449 return DecodeSelector(ID);
4450}
4451
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004452uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redl725cd962010-08-04 20:40:17 +00004453 // ID 0 (the null selector) is considered an external selector.
4454 return getTotalNumSelectors() + 1;
Douglas Gregor719770d2010-04-06 17:30:22 +00004455}
4456
Mike Stump1eb44332009-09-09 15:08:12 +00004457DeclarationName
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004458ASTReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00004459 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
4460 switch (Kind) {
4461 case DeclarationName::Identifier:
4462 return DeclarationName(GetIdentifierInfo(Record, Idx));
4463
4464 case DeclarationName::ObjCZeroArgSelector:
4465 case DeclarationName::ObjCOneArgSelector:
4466 case DeclarationName::ObjCMultiArgSelector:
Steve Naroffa7503a72009-04-23 15:15:40 +00004467 return DeclarationName(GetSelector(Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004468
4469 case DeclarationName::CXXConstructorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00004470 return Context->DeclarationNames.getCXXConstructorName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00004471 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004472
4473 case DeclarationName::CXXDestructorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00004474 return Context->DeclarationNames.getCXXDestructorName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00004475 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004476
4477 case DeclarationName::CXXConversionFunctionName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00004478 return Context->DeclarationNames.getCXXConversionFunctionName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00004479 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004480
4481 case DeclarationName::CXXOperatorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00004482 return Context->DeclarationNames.getCXXOperatorName(
Douglas Gregor2cf26342009-04-09 22:27:44 +00004483 (OverloadedOperatorKind)Record[Idx++]);
4484
Sean Hunt3e518bd2009-11-29 07:34:05 +00004485 case DeclarationName::CXXLiteralOperatorName:
4486 return Context->DeclarationNames.getCXXLiteralOperatorName(
4487 GetIdentifierInfo(Record, Idx));
4488
Douglas Gregor2cf26342009-04-09 22:27:44 +00004489 case DeclarationName::CXXUsingDirective:
4490 return DeclarationName::getUsingDirectiveName();
4491 }
4492
4493 // Required to silence GCC warning
4494 return DeclarationName();
4495}
Douglas Gregor0a0428e2009-04-10 20:39:37 +00004496
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004497void ASTReader::ReadDeclarationNameLoc(PerFileData &F,
4498 DeclarationNameLoc &DNLoc,
4499 DeclarationName Name,
4500 const RecordData &Record, unsigned &Idx) {
4501 switch (Name.getNameKind()) {
4502 case DeclarationName::CXXConstructorName:
4503 case DeclarationName::CXXDestructorName:
4504 case DeclarationName::CXXConversionFunctionName:
4505 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
4506 break;
4507
4508 case DeclarationName::CXXOperatorName:
4509 DNLoc.CXXOperatorName.BeginOpNameLoc
4510 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4511 DNLoc.CXXOperatorName.EndOpNameLoc
4512 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4513 break;
4514
4515 case DeclarationName::CXXLiteralOperatorName:
4516 DNLoc.CXXLiteralOperatorName.OpNameLoc
4517 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4518 break;
4519
4520 case DeclarationName::Identifier:
4521 case DeclarationName::ObjCZeroArgSelector:
4522 case DeclarationName::ObjCOneArgSelector:
4523 case DeclarationName::ObjCMultiArgSelector:
4524 case DeclarationName::CXXUsingDirective:
4525 break;
4526 }
4527}
4528
4529void ASTReader::ReadDeclarationNameInfo(PerFileData &F,
4530 DeclarationNameInfo &NameInfo,
4531 const RecordData &Record, unsigned &Idx) {
4532 NameInfo.setName(ReadDeclarationName(Record, Idx));
4533 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
4534 DeclarationNameLoc DNLoc;
4535 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
4536 NameInfo.setInfo(DNLoc);
4537}
4538
4539void ASTReader::ReadQualifierInfo(PerFileData &F, QualifierInfo &Info,
4540 const RecordData &Record, unsigned &Idx) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004541 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004542 unsigned NumTPLists = Record[Idx++];
4543 Info.NumTemplParamLists = NumTPLists;
4544 if (NumTPLists) {
4545 Info.TemplParamLists = new (*Context) TemplateParameterList*[NumTPLists];
4546 for (unsigned i=0; i != NumTPLists; ++i)
4547 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
4548 }
4549}
4550
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004551TemplateName
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004552ASTReader::ReadTemplateName(PerFileData &F, const RecordData &Record,
4553 unsigned &Idx) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00004554 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004555 switch (Kind) {
4556 case TemplateName::Template:
4557 return TemplateName(cast_or_null<TemplateDecl>(GetDecl(Record[Idx++])));
4558
4559 case TemplateName::OverloadedTemplate: {
4560 unsigned size = Record[Idx++];
4561 UnresolvedSet<8> Decls;
4562 while (size--)
4563 Decls.addDecl(cast<NamedDecl>(GetDecl(Record[Idx++])));
4564
4565 return Context->getOverloadedTemplateName(Decls.begin(), Decls.end());
4566 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004567
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004568 case TemplateName::QualifiedTemplate: {
4569 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
4570 bool hasTemplKeyword = Record[Idx++];
4571 TemplateDecl *Template = cast<TemplateDecl>(GetDecl(Record[Idx++]));
4572 return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
4573 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004574
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004575 case TemplateName::DependentTemplate: {
4576 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
4577 if (Record[Idx++]) // isIdentifier
4578 return Context->getDependentTemplateName(NNS,
4579 GetIdentifierInfo(Record, Idx));
4580 return Context->getDependentTemplateName(NNS,
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004581 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004582 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004583
4584 case TemplateName::SubstTemplateTemplateParmPack: {
4585 TemplateTemplateParmDecl *Param
4586 = cast_or_null<TemplateTemplateParmDecl>(GetDecl(Record[Idx++]));
4587 if (!Param)
4588 return TemplateName();
4589
4590 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
4591 if (ArgPack.getKind() != TemplateArgument::Pack)
4592 return TemplateName();
4593
4594 return Context->getSubstTemplateTemplateParmPack(Param, ArgPack);
4595 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004596 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004597
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004598 assert(0 && "Unhandled template name kind!");
4599 return TemplateName();
4600}
4601
4602TemplateArgument
Sebastian Redlc3632732010-10-05 15:59:54 +00004603ASTReader::ReadTemplateArgument(PerFileData &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00004604 const RecordData &Record, unsigned &Idx) {
Douglas Gregora7fc9012011-01-05 18:58:31 +00004605 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
4606 switch (Kind) {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004607 case TemplateArgument::Null:
4608 return TemplateArgument();
4609 case TemplateArgument::Type:
4610 return TemplateArgument(GetType(Record[Idx++]));
4611 case TemplateArgument::Declaration:
4612 return TemplateArgument(GetDecl(Record[Idx++]));
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00004613 case TemplateArgument::Integral: {
4614 llvm::APSInt Value = ReadAPSInt(Record, Idx);
4615 QualType T = GetType(Record[Idx++]);
4616 return TemplateArgument(Value, T);
4617 }
Douglas Gregora7fc9012011-01-05 18:58:31 +00004618 case TemplateArgument::Template:
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004619 return TemplateArgument(ReadTemplateName(F, Record, Idx));
Douglas Gregora7fc9012011-01-05 18:58:31 +00004620 case TemplateArgument::TemplateExpansion: {
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004621 TemplateName Name = ReadTemplateName(F, Record, Idx);
Douglas Gregor2be29f42011-01-14 23:41:42 +00004622 llvm::Optional<unsigned> NumTemplateExpansions;
4623 if (unsigned NumExpansions = Record[Idx++])
4624 NumTemplateExpansions = NumExpansions - 1;
4625 return TemplateArgument(Name, NumTemplateExpansions);
Douglas Gregorba68eca2011-01-05 17:40:24 +00004626 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004627 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00004628 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004629 case TemplateArgument::Pack: {
4630 unsigned NumArgs = Record[Idx++];
Douglas Gregor910f8002010-11-07 23:05:16 +00004631 TemplateArgument *Args = new (*Context) TemplateArgument[NumArgs];
4632 for (unsigned I = 0; I != NumArgs; ++I)
4633 Args[I] = ReadTemplateArgument(F, Record, Idx);
4634 return TemplateArgument(Args, NumArgs);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004635 }
4636 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004637
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004638 assert(0 && "Unhandled template argument kind!");
4639 return TemplateArgument();
4640}
4641
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004642TemplateParameterList *
Sebastian Redlc3632732010-10-05 15:59:54 +00004643ASTReader::ReadTemplateParameterList(PerFileData &F,
4644 const RecordData &Record, unsigned &Idx) {
4645 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
4646 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
4647 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004648
4649 unsigned NumParams = Record[Idx++];
4650 llvm::SmallVector<NamedDecl *, 16> Params;
4651 Params.reserve(NumParams);
4652 while (NumParams--)
4653 Params.push_back(cast<NamedDecl>(GetDecl(Record[Idx++])));
Michael J. Spencer20249a12010-10-21 03:16:25 +00004654
4655 TemplateParameterList* TemplateParams =
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004656 TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc,
4657 Params.data(), Params.size(), RAngleLoc);
4658 return TemplateParams;
4659}
4660
4661void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004662ASTReader::
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004663ReadTemplateArgumentList(llvm::SmallVector<TemplateArgument, 8> &TemplArgs,
Sebastian Redlc3632732010-10-05 15:59:54 +00004664 PerFileData &F, const RecordData &Record,
4665 unsigned &Idx) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004666 unsigned NumTemplateArgs = Record[Idx++];
4667 TemplArgs.reserve(NumTemplateArgs);
4668 while (NumTemplateArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00004669 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004670}
4671
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004672/// \brief Read a UnresolvedSet structure.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004673void ASTReader::ReadUnresolvedSet(UnresolvedSetImpl &Set,
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004674 const RecordData &Record, unsigned &Idx) {
4675 unsigned NumDecls = Record[Idx++];
4676 while (NumDecls--) {
4677 NamedDecl *D = cast<NamedDecl>(GetDecl(Record[Idx++]));
4678 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
4679 Set.addDecl(D, AS);
4680 }
4681}
4682
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004683CXXBaseSpecifier
Sebastian Redlc3632732010-10-05 15:59:54 +00004684ASTReader::ReadCXXBaseSpecifier(PerFileData &F,
Nick Lewycky56062202010-07-26 16:56:01 +00004685 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004686 bool isVirtual = static_cast<bool>(Record[Idx++]);
4687 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
4688 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redlf677ea32011-02-05 19:23:19 +00004689 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00004690 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
4691 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00004692 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redlf677ea32011-02-05 19:23:19 +00004693 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
Douglas Gregorf90b27a2011-01-03 22:36:02 +00004694 EllipsisLoc);
Sebastian Redlf677ea32011-02-05 19:23:19 +00004695 Result.setInheritConstructors(inheritConstructors);
4696 return Result;
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004697}
4698
Sean Huntcbb67482011-01-08 20:30:50 +00004699std::pair<CXXCtorInitializer **, unsigned>
4700ASTReader::ReadCXXCtorInitializers(PerFileData &F, const RecordData &Record,
4701 unsigned &Idx) {
4702 CXXCtorInitializer **CtorInitializers = 0;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004703 unsigned NumInitializers = Record[Idx++];
4704 if (NumInitializers) {
4705 ASTContext &C = *getContext();
4706
Sean Huntcbb67482011-01-08 20:30:50 +00004707 CtorInitializers
4708 = new (C) CXXCtorInitializer*[NumInitializers];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004709 for (unsigned i=0; i != NumInitializers; ++i) {
4710 TypeSourceInfo *BaseClassInfo = 0;
4711 bool IsBaseVirtual = false;
4712 FieldDecl *Member = 0;
Francois Pichet00eb3f92010-12-04 09:14:42 +00004713 IndirectFieldDecl *IndirectMember = 0;
Michael J. Spencer20249a12010-10-21 03:16:25 +00004714
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004715 bool IsBaseInitializer = Record[Idx++];
4716 if (IsBaseInitializer) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004717 BaseClassInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004718 IsBaseVirtual = Record[Idx++];
4719 } else {
Francois Pichet00eb3f92010-12-04 09:14:42 +00004720 bool IsIndirectMemberInitializer = Record[Idx++];
4721 if (IsIndirectMemberInitializer)
4722 IndirectMember = cast<IndirectFieldDecl>(GetDecl(Record[Idx++]));
4723 else
4724 Member = cast<FieldDecl>(GetDecl(Record[Idx++]));
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004725 }
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00004726 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redlc3632732010-10-05 15:59:54 +00004727 Expr *Init = ReadExpr(F);
Sebastian Redlc3632732010-10-05 15:59:54 +00004728 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
4729 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004730 bool IsWritten = Record[Idx++];
4731 unsigned SourceOrderOrNumArrayIndices;
4732 llvm::SmallVector<VarDecl *, 8> Indices;
4733 if (IsWritten) {
4734 SourceOrderOrNumArrayIndices = Record[Idx++];
4735 } else {
4736 SourceOrderOrNumArrayIndices = Record[Idx++];
4737 Indices.reserve(SourceOrderOrNumArrayIndices);
4738 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
4739 Indices.push_back(cast<VarDecl>(GetDecl(Record[Idx++])));
4740 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004741
Sean Huntcbb67482011-01-08 20:30:50 +00004742 CXXCtorInitializer *BOMInit;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004743 if (IsBaseInitializer) {
Sean Huntcbb67482011-01-08 20:30:50 +00004744 BOMInit = new (C) CXXCtorInitializer(C, BaseClassInfo, IsBaseVirtual,
4745 LParenLoc, Init, RParenLoc,
4746 MemberOrEllipsisLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004747 } else if (IsWritten) {
Francois Pichet00eb3f92010-12-04 09:14:42 +00004748 if (Member)
Sean Huntcbb67482011-01-08 20:30:50 +00004749 BOMInit = new (C) CXXCtorInitializer(C, Member, MemberOrEllipsisLoc,
4750 LParenLoc, Init, RParenLoc);
Francois Pichet00eb3f92010-12-04 09:14:42 +00004751 else
Sean Huntcbb67482011-01-08 20:30:50 +00004752 BOMInit = new (C) CXXCtorInitializer(C, IndirectMember,
4753 MemberOrEllipsisLoc, LParenLoc,
4754 Init, RParenLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004755 } else {
Sean Huntcbb67482011-01-08 20:30:50 +00004756 BOMInit = CXXCtorInitializer::Create(C, Member, MemberOrEllipsisLoc,
4757 LParenLoc, Init, RParenLoc,
4758 Indices.data(), Indices.size());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004759 }
4760
Argyrios Kyrtzidisf84cde12010-09-06 19:04:27 +00004761 if (IsWritten)
4762 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Sean Huntcbb67482011-01-08 20:30:50 +00004763 CtorInitializers[i] = BOMInit;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004764 }
4765 }
4766
Sean Huntcbb67482011-01-08 20:30:50 +00004767 return std::make_pair(CtorInitializers, NumInitializers);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004768}
4769
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004770NestedNameSpecifier *
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004771ASTReader::ReadNestedNameSpecifier(const RecordData &Record, unsigned &Idx) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004772 unsigned N = Record[Idx++];
4773 NestedNameSpecifier *NNS = 0, *Prev = 0;
4774 for (unsigned I = 0; I != N; ++I) {
4775 NestedNameSpecifier::SpecifierKind Kind
4776 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
4777 switch (Kind) {
4778 case NestedNameSpecifier::Identifier: {
4779 IdentifierInfo *II = GetIdentifierInfo(Record, Idx);
4780 NNS = NestedNameSpecifier::Create(*Context, Prev, II);
4781 break;
4782 }
4783
4784 case NestedNameSpecifier::Namespace: {
4785 NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++]));
4786 NNS = NestedNameSpecifier::Create(*Context, Prev, NS);
4787 break;
4788 }
4789
Douglas Gregor14aba762011-02-24 02:36:08 +00004790 case NestedNameSpecifier::NamespaceAlias: {
4791 NamespaceAliasDecl *Alias
4792 = cast<NamespaceAliasDecl>(GetDecl(Record[Idx++]));
4793 NNS = NestedNameSpecifier::Create(*Context, Prev, Alias);
4794 break;
4795 }
4796
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004797 case NestedNameSpecifier::TypeSpec:
4798 case NestedNameSpecifier::TypeSpecWithTemplate: {
John McCallf4c73712011-01-19 06:33:43 +00004799 const Type *T = GetType(Record[Idx++]).getTypePtrOrNull();
Douglas Gregor1ab55e92010-12-10 17:03:06 +00004800 if (!T)
4801 return 0;
4802
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004803 bool Template = Record[Idx++];
4804 NNS = NestedNameSpecifier::Create(*Context, Prev, Template, T);
4805 break;
4806 }
4807
4808 case NestedNameSpecifier::Global: {
4809 NNS = NestedNameSpecifier::GlobalSpecifier(*Context);
4810 // No associated value, and there can't be a prefix.
4811 break;
4812 }
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004813 }
Argyrios Kyrtzidisd2bb2c02010-07-07 15:46:30 +00004814 Prev = NNS;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004815 }
4816 return NNS;
4817}
4818
Douglas Gregordc355712011-02-25 00:36:19 +00004819NestedNameSpecifierLoc
4820ASTReader::ReadNestedNameSpecifierLoc(PerFileData &F, const RecordData &Record,
4821 unsigned &Idx) {
4822 unsigned N = Record[Idx++];
Douglas Gregor5f791bb2011-02-28 23:58:31 +00004823 NestedNameSpecifierLocBuilder Builder;
Douglas Gregordc355712011-02-25 00:36:19 +00004824 for (unsigned I = 0; I != N; ++I) {
4825 NestedNameSpecifier::SpecifierKind Kind
4826 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
4827 switch (Kind) {
4828 case NestedNameSpecifier::Identifier: {
Douglas Gregor5f791bb2011-02-28 23:58:31 +00004829 IdentifierInfo *II = GetIdentifierInfo(Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00004830 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor5f791bb2011-02-28 23:58:31 +00004831 Builder.Extend(*Context, II, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00004832 break;
4833 }
4834
4835 case NestedNameSpecifier::Namespace: {
Douglas Gregordc355712011-02-25 00:36:19 +00004836 NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++]));
Douglas Gregordc355712011-02-25 00:36:19 +00004837 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor5f791bb2011-02-28 23:58:31 +00004838 Builder.Extend(*Context, NS, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00004839 break;
4840 }
4841
4842 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregordc355712011-02-25 00:36:19 +00004843 NamespaceAliasDecl *Alias
4844 = cast<NamespaceAliasDecl>(GetDecl(Record[Idx++]));
Douglas Gregordc355712011-02-25 00:36:19 +00004845 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor5f791bb2011-02-28 23:58:31 +00004846 Builder.Extend(*Context, Alias, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00004847 break;
4848 }
4849
4850 case NestedNameSpecifier::TypeSpec:
4851 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregordc355712011-02-25 00:36:19 +00004852 bool Template = Record[Idx++];
4853 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
4854 if (!T)
4855 return NestedNameSpecifierLoc();
Douglas Gregordc355712011-02-25 00:36:19 +00004856 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor5f791bb2011-02-28 23:58:31 +00004857
4858 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
4859 Builder.Extend(*Context,
4860 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
4861 T->getTypeLoc(), ColonColonLoc);
Douglas Gregordc355712011-02-25 00:36:19 +00004862 break;
4863 }
4864
4865 case NestedNameSpecifier::Global: {
Douglas Gregordc355712011-02-25 00:36:19 +00004866 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor5f791bb2011-02-28 23:58:31 +00004867 Builder.MakeGlobal(*Context, ColonColonLoc);
Douglas Gregordc355712011-02-25 00:36:19 +00004868 break;
4869 }
4870 }
Douglas Gregordc355712011-02-25 00:36:19 +00004871 }
4872
Douglas Gregor5f791bb2011-02-28 23:58:31 +00004873 return Builder.getWithLocInContext(*Context);
Douglas Gregordc355712011-02-25 00:36:19 +00004874}
4875
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004876SourceRange
Sebastian Redlc3632732010-10-05 15:59:54 +00004877ASTReader::ReadSourceRange(PerFileData &F, const RecordData &Record,
4878 unsigned &Idx) {
4879 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
4880 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar8ee59392010-06-02 15:47:10 +00004881 return SourceRange(beg, end);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004882}
4883
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004884/// \brief Read an integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004885llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004886 unsigned BitWidth = Record[Idx++];
4887 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
4888 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
4889 Idx += NumWords;
4890 return Result;
4891}
4892
4893/// \brief Read a signed integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004894llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004895 bool isUnsigned = Record[Idx++];
4896 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
4897}
4898
Douglas Gregor17fc2232009-04-14 21:55:33 +00004899/// \brief Read a floating-point value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004900llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00004901 return llvm::APFloat(ReadAPInt(Record, Idx));
4902}
4903
Douglas Gregor68a2eb02009-04-15 21:30:51 +00004904// \brief Read a string
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004905std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00004906 unsigned Len = Record[Idx++];
Jay Foadbeaaccd2009-05-21 09:52:38 +00004907 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00004908 Idx += Len;
4909 return Result;
4910}
4911
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00004912VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
4913 unsigned &Idx) {
4914 unsigned Major = Record[Idx++];
4915 unsigned Minor = Record[Idx++];
4916 unsigned Subminor = Record[Idx++];
4917 if (Minor == 0)
4918 return VersionTuple(Major);
4919 if (Subminor == 0)
4920 return VersionTuple(Major, Minor - 1);
4921 return VersionTuple(Major, Minor - 1, Subminor - 1);
4922}
4923
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004924CXXTemporary *ASTReader::ReadCXXTemporary(const RecordData &Record,
Chris Lattnerd2598362010-05-10 00:25:06 +00004925 unsigned &Idx) {
4926 CXXDestructorDecl *Decl = cast<CXXDestructorDecl>(GetDecl(Record[Idx++]));
4927 return CXXTemporary::Create(*Context, Decl);
4928}
4929
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004930DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00004931 return Diag(SourceLocation(), DiagID);
4932}
4933
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004934DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004935 return Diags.Report(Loc, DiagID);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00004936}
Douglas Gregor025452f2009-04-17 00:04:06 +00004937
Douglas Gregor668c1a42009-04-21 22:25:48 +00004938/// \brief Retrieve the identifier table associated with the
4939/// preprocessor.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004940IdentifierTable &ASTReader::getIdentifierTable() {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00004941 assert(PP && "Forgot to set Preprocessor ?");
4942 return PP->getIdentifierTable();
Douglas Gregor668c1a42009-04-21 22:25:48 +00004943}
4944
Douglas Gregor025452f2009-04-17 00:04:06 +00004945/// \brief Record that the given ID maps to the given switch-case
4946/// statement.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004947void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Douglas Gregor025452f2009-04-17 00:04:06 +00004948 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
4949 SwitchCaseStmts[ID] = SC;
4950}
4951
4952/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004953SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Douglas Gregor025452f2009-04-17 00:04:06 +00004954 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
4955 return SwitchCaseStmts[ID];
4956}
Douglas Gregor1de05fe2009-04-17 18:18:49 +00004957
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00004958void ASTReader::ClearSwitchCaseIDs() {
4959 SwitchCaseStmts.clear();
4960}
4961
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004962void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004963 assert(NumCurrentElementsDeserializing &&
4964 "FinishedDeserializing not paired with StartedDeserializing");
4965 if (NumCurrentElementsDeserializing == 1) {
Douglas Gregord89275b2009-07-06 18:54:52 +00004966 // If any identifiers with corresponding top-level declarations have
4967 // been loaded, load those declarations now.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004968 while (!PendingIdentifierInfos.empty()) {
4969 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
4970 PendingIdentifierInfos.front().DeclIDs, true);
4971 PendingIdentifierInfos.pop_front();
Douglas Gregord89275b2009-07-06 18:54:52 +00004972 }
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00004973
Argyrios Kyrtzidis0895d152011-02-12 07:50:47 +00004974 // Ready to load previous declarations of Decls that were delayed.
4975 while (!PendingPreviousDecls.empty()) {
4976 loadAndAttachPreviousDecl(PendingPreviousDecls.front().first,
4977 PendingPreviousDecls.front().second);
4978 PendingPreviousDecls.pop_front();
4979 }
4980
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00004981 // We are not in recursive loading, so it's safe to pass the "interesting"
4982 // decls to the consumer.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004983 if (Consumer)
4984 PassInterestingDeclsToConsumer();
Argyrios Kyrtzidis134db1f2010-10-24 17:26:31 +00004985
4986 assert(PendingForwardRefs.size() == 0 &&
4987 "Some forward refs did not get linked to the definition!");
Douglas Gregord89275b2009-07-06 18:54:52 +00004988 }
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004989 --NumCurrentElementsDeserializing;
Douglas Gregord89275b2009-07-06 18:54:52 +00004990}
Douglas Gregor501c1032010-08-19 00:28:17 +00004991
Sebastian Redle1dde812010-08-24 00:50:04 +00004992ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context,
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00004993 const char *isysroot, bool DisableValidation,
4994 bool DisableStatCache)
Sebastian Redle1dde812010-08-24 00:50:04 +00004995 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
4996 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
4997 Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context),
4998 Consumer(0), isysroot(isysroot), DisableValidation(DisableValidation),
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00004999 DisableStatCache(DisableStatCache), NumStatHits(0), NumStatMisses(0),
5000 NumSLocEntriesRead(0), TotalNumSLocEntries(0), NextSLocOffset(0),
5001 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
5002 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
5003 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
5004 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
5005 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
5006 NumCurrentElementsDeserializing(0)
5007{
Sebastian Redle1dde812010-08-24 00:50:04 +00005008 RelocatablePCH = false;
5009}
5010
5011ASTReader::ASTReader(SourceManager &SourceMgr, FileManager &FileMgr,
5012 Diagnostic &Diags, const char *isysroot,
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00005013 bool DisableValidation, bool DisableStatCache)
Sebastian Redle1dde812010-08-24 00:50:04 +00005014 : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr),
5015 Diags(Diags), SemaObj(0), PP(0), Context(0), Consumer(0),
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00005016 isysroot(isysroot), DisableValidation(DisableValidation),
5017 DisableStatCache(DisableStatCache), NumStatHits(0), NumStatMisses(0),
5018 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Sebastian Redl8db9fae2010-09-22 20:19:08 +00005019 NextSLocOffset(0), NumStatementsRead(0), TotalNumStatements(0),
5020 NumMacrosRead(0), TotalNumMacros(0), NumSelectorsRead(0),
5021 NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
5022 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
5023 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
5024 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
Sebastian Redle1dde812010-08-24 00:50:04 +00005025 RelocatablePCH = false;
5026}
5027
5028ASTReader::~ASTReader() {
5029 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
5030 delete Chain[e - i - 1];
5031 // Delete all visible decl lookup tables
5032 for (DeclContextOffsetsMap::iterator I = DeclContextOffsets.begin(),
5033 E = DeclContextOffsets.end();
5034 I != E; ++I) {
5035 for (DeclContextInfos::iterator J = I->second.begin(), F = I->second.end();
5036 J != F; ++J) {
5037 if (J->NameLookupTableData)
5038 delete static_cast<ASTDeclContextNameLookupTable*>(
5039 J->NameLookupTableData);
5040 }
5041 }
5042 for (DeclContextVisibleUpdatesPending::iterator
5043 I = PendingVisibleUpdates.begin(),
5044 E = PendingVisibleUpdates.end();
5045 I != E; ++I) {
5046 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
5047 F = I->second.end();
5048 J != F; ++J)
5049 delete static_cast<ASTDeclContextNameLookupTable*>(*J);
5050 }
5051}
5052
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00005053ASTReader::PerFileData::PerFileData(ASTFileType Ty)
5054 : Type(Ty), SizeInBits(0), LocalNumSLocEntries(0), SLocOffsets(0), LocalSLocSize(0),
Sebastian Redl301c9b02010-09-22 00:42:27 +00005055 LocalNumIdentifiers(0), IdentifierOffsets(0), IdentifierTableData(0),
5056 IdentifierLookupTable(0), LocalNumMacroDefinitions(0),
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00005057 MacroDefinitionOffsets(0),
5058 LocalNumHeaderFileInfos(0), HeaderFileInfoTableData(0),
5059 HeaderFileInfoTable(0),
5060 LocalNumSelectors(0), SelectorOffsets(0),
Sebastian Redl301c9b02010-09-22 00:42:27 +00005061 SelectorLookupTableData(0), SelectorLookupTable(0), LocalNumDecls(0),
Douglas Gregor7c789c12010-10-29 22:39:52 +00005062 DeclOffsets(0), LocalNumCXXBaseSpecifiers(0), CXXBaseSpecifiersOffsets(0),
5063 LocalNumTypes(0), TypeOffsets(0), StatCache(0),
Sebastian Redla866e652010-10-01 19:59:12 +00005064 NumPreallocatedPreprocessingEntities(0), NextInSource(0)
Douglas Gregor501c1032010-08-19 00:28:17 +00005065{}
5066
5067ASTReader::PerFileData::~PerFileData() {
5068 delete static_cast<ASTIdentifierLookupTable *>(IdentifierLookupTable);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00005069 delete static_cast<HeaderFileInfoLookupTable *>(HeaderFileInfoTable);
Douglas Gregor501c1032010-08-19 00:28:17 +00005070 delete static_cast<ASTSelectorLookupTable *>(SelectorLookupTable);
5071}