blob: e658512e9a369cf823c74eab7c6aaaf9bcd18f4b [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 Gregor2cf26342009-04-09 22:27:44 +000026#include "clang/AST/Type.h"
John McCalla1ee0c52009-10-16 21:56:05 +000027#include "clang/AST/TypeLocVisitor.h"
Chris Lattner42d42b52009-04-10 21:41:48 +000028#include "clang/Lex/MacroInfo.h"
Douglas Gregor6a5a23f2010-03-19 21:51:54 +000029#include "clang/Lex/PreprocessingRecord.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000030#include "clang/Lex/Preprocessor.h"
Steve Naroff83d63c72009-04-24 20:03:17 +000031#include "clang/Lex/HeaderSearch.h"
Douglas Gregor668c1a42009-04-21 22:25:48 +000032#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000033#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000034#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000035#include "clang/Basic/FileManager.h"
Chris Lattner10e286a2010-11-23 19:19:34 +000036#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000037#include "clang/Basic/TargetInfo.h"
Douglas Gregor445e23e2009-10-05 21:07:28 +000038#include "clang/Basic/Version.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000039#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000040#include "llvm/Bitcode/BitstreamReader.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000041#include "llvm/Support/MemoryBuffer.h"
John McCall833ca992009-10-29 08:12:44 +000042#include "llvm/Support/ErrorHandling.h"
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000043#include "llvm/Support/FileSystem.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000044#include "llvm/Support/Path.h"
Michael J. Spencer3a321e22010-12-09 17:36:38 +000045#include "llvm/Support/system_error.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000046#include <algorithm>
Douglas Gregore721f952009-04-28 18:58:38 +000047#include <iterator>
Douglas Gregor2cf26342009-04-09 22:27:44 +000048#include <cstdio>
Douglas Gregor4fed3f42009-04-27 18:38:38 +000049#include <sys/stat.h>
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000050
Douglas Gregor2cf26342009-04-09 22:27:44 +000051using namespace clang;
Sebastian Redl8538e8d2010-08-18 23:57:32 +000052using namespace clang::serialization;
Douglas Gregor2cf26342009-04-09 22:27:44 +000053
54//===----------------------------------------------------------------------===//
Sebastian Redl3c7f4132010-08-18 23:57:06 +000055// PCH validator implementation
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000056//===----------------------------------------------------------------------===//
57
Sebastian Redl571db7f2010-08-18 23:56:56 +000058ASTReaderListener::~ASTReaderListener() {}
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000059
60bool
61PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts) {
62 const LangOptions &PPLangOpts = PP.getLangOptions();
63#define PARSE_LANGOPT_BENIGN(Option)
64#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
65 if (PPLangOpts.Option != LangOpts.Option) { \
66 Reader.Diag(DiagID) << LangOpts.Option << PPLangOpts.Option; \
67 return true; \
68 }
69
70 PARSE_LANGOPT_BENIGN(Trigraphs);
71 PARSE_LANGOPT_BENIGN(BCPLComment);
72 PARSE_LANGOPT_BENIGN(DollarIdents);
73 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
74 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
Chandler Carrutheb5d7b72010-04-17 20:17:31 +000075 PARSE_LANGOPT_IMPORTANT(GNUKeywords, diag::warn_pch_gnu_keywords);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000076 PARSE_LANGOPT_BENIGN(ImplicitInt);
77 PARSE_LANGOPT_BENIGN(Digraphs);
78 PARSE_LANGOPT_BENIGN(HexFloats);
79 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
80 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
Michael J. Spencerdae4ac42010-10-21 05:21:48 +000081 PARSE_LANGOPT_BENIGN(MSCVersion);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000082 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
83 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
84 PARSE_LANGOPT_BENIGN(CXXOperatorName);
85 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
86 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
87 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
Fariborz Jahanian412e7982010-02-09 19:31:38 +000088 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI2, diag::warn_pch_nonfragile_abi2);
Fariborz Jahanianf84109e2011-01-07 18:59:25 +000089 PARSE_LANGOPT_IMPORTANT(AppleKext, diag::warn_pch_apple_kext);
Ted Kremenekc32647d2010-12-23 21:35:43 +000090 PARSE_LANGOPT_IMPORTANT(ObjCDefaultSynthProperties,
91 diag::warn_pch_objc_auto_properties);
Michael J. Spencer20249a12010-10-21 03:16:25 +000092 PARSE_LANGOPT_IMPORTANT(NoConstantCFStrings,
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +000093 diag::warn_pch_no_constant_cfstrings);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000094 PARSE_LANGOPT_BENIGN(PascalStrings);
95 PARSE_LANGOPT_BENIGN(WritableStrings);
Mike Stump1eb44332009-09-09 15:08:12 +000096 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000097 diag::warn_pch_lax_vector_conversions);
Nate Begeman69cfb9b2009-06-25 22:57:40 +000098 PARSE_LANGOPT_IMPORTANT(AltiVec, diag::warn_pch_altivec);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000099 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
Daniel Dunbar73482882010-02-10 18:48:44 +0000100 PARSE_LANGOPT_IMPORTANT(SjLjExceptions, diag::warn_pch_sjlj_exceptions);
Anders Carlssonda4b7cf2011-02-19 23:53:54 +0000101 PARSE_LANGOPT_IMPORTANT(ObjCExceptions, diag::warn_pch_objc_exceptions);
Douglas Gregor6f755502011-02-01 15:15:22 +0000102 PARSE_LANGOPT_IMPORTANT(MSBitfields, diag::warn_pch_ms_bitfields);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000103 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
104 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
105 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
Mike Stump1eb44332009-09-09 15:08:12 +0000106 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000107 diag::warn_pch_thread_safe_statics);
Daniel Dunbar5345c392009-09-03 04:54:28 +0000108 PARSE_LANGOPT_IMPORTANT(POSIXThreads, diag::warn_pch_posix_threads);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000109 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
110 PARSE_LANGOPT_BENIGN(EmitAllDecls);
111 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
Chris Lattnera4d71452010-06-26 21:25:03 +0000112 PARSE_LANGOPT_BENIGN(getSignedOverflowBehavior());
Mike Stump1eb44332009-09-09 15:08:12 +0000113 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000114 diag::warn_pch_heinous_extensions);
115 // FIXME: Most of the options below are benign if the macro wasn't
116 // used. Unfortunately, this means that a PCH compiled without
117 // optimization can't be used with optimization turned on, even
118 // though the only thing that changes is whether __OPTIMIZE__ was
119 // defined... but if __OPTIMIZE__ never showed up in the header, it
120 // doesn't matter. We could consider making this some special kind
121 // of check.
122 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
123 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
124 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
125 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
126 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
127 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
128 PARSE_LANGOPT_IMPORTANT(AccessControl, diag::warn_pch_access_control);
129 PARSE_LANGOPT_IMPORTANT(CharIsSigned, diag::warn_pch_char_signed);
John Thompsona6fda122009-11-05 20:14:16 +0000130 PARSE_LANGOPT_IMPORTANT(ShortWChar, diag::warn_pch_short_wchar);
Argyrios Kyrtzidis9a2b9d72010-10-08 00:25:19 +0000131 PARSE_LANGOPT_IMPORTANT(ShortEnums, diag::warn_pch_short_enums);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000132 if ((PPLangOpts.getGCMode() != 0) != (LangOpts.getGCMode() != 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000133 Reader.Diag(diag::warn_pch_gc_mode)
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000134 << LangOpts.getGCMode() << PPLangOpts.getGCMode();
135 return true;
136 }
137 PARSE_LANGOPT_BENIGN(getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000138 PARSE_LANGOPT_IMPORTANT(getStackProtectorMode(),
139 diag::warn_pch_stack_protector);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000140 PARSE_LANGOPT_BENIGN(InstantiationDepth);
Nate Begeman69cfb9b2009-06-25 22:57:40 +0000141 PARSE_LANGOPT_IMPORTANT(OpenCL, diag::warn_pch_opencl);
Peter Collingbourne08a53262010-12-01 19:14:57 +0000142 PARSE_LANGOPT_IMPORTANT(CUDA, diag::warn_pch_cuda);
Mike Stump9c276ae2009-12-12 01:27:46 +0000143 PARSE_LANGOPT_BENIGN(CatchUndefined);
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000144 PARSE_LANGOPT_IMPORTANT(ElideConstructors, diag::warn_pch_elide_constructors);
Douglas Gregora0068fc2010-07-09 17:35:33 +0000145 PARSE_LANGOPT_BENIGN(SpellChecking);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000146 PARSE_LANGOPT_BENIGN(DefaultFPContract);
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +0000147#undef PARSE_LANGOPT_IMPORTANT
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000148#undef PARSE_LANGOPT_BENIGN
149
150 return false;
151}
152
Daniel Dunbardc3c0d22009-11-11 00:52:11 +0000153bool PCHValidator::ReadTargetTriple(llvm::StringRef Triple) {
154 if (Triple == PP.getTargetInfo().getTriple().str())
155 return false;
156
157 Reader.Diag(diag::warn_pch_target_triple)
158 << Triple << PP.getTargetInfo().getTriple().str();
159 return true;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000160}
161
Benjamin Kramer54353f42010-11-25 18:29:30 +0000162namespace {
163 struct EmptyStringRef {
164 bool operator ()(llvm::StringRef r) const { return r.empty(); }
165 };
166 struct EmptyBlock {
167 bool operator ()(const PCHPredefinesBlock &r) const {return r.Data.empty();}
168 };
169}
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000170
171static bool EqualConcatenations(llvm::SmallVector<llvm::StringRef, 2> L,
172 PCHPredefinesBlocks R) {
173 // First, sum up the lengths.
174 unsigned LL = 0, RL = 0;
175 for (unsigned I = 0, N = L.size(); I != N; ++I) {
176 LL += L[I].size();
177 }
178 for (unsigned I = 0, N = R.size(); I != N; ++I) {
179 RL += R[I].Data.size();
180 }
181 if (LL != RL)
182 return false;
183 if (LL == 0 && RL == 0)
184 return true;
185
186 // Kick out empty parts, they confuse the algorithm below.
187 L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end());
188 R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end());
189
190 // Do it the hard way. At this point, both vectors must be non-empty.
191 llvm::StringRef LR = L[0], RR = R[0].Data;
192 unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size();
Daniel Dunbarc76c9e02010-07-16 00:00:11 +0000193 (void) RN;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000194 for (;;) {
195 // Compare the current pieces.
196 if (LR.size() == RR.size()) {
197 // If they're the same length, it's pretty easy.
198 if (LR != RR)
199 return false;
200 // Both pieces are done, advance.
201 ++LI;
202 ++RI;
203 // If either string is done, they're both done, since they're the same
204 // length.
205 if (LI == LN) {
206 assert(RI == RN && "Strings not the same length after all?");
207 return true;
208 }
209 LR = L[LI];
210 RR = R[RI].Data;
211 } else if (LR.size() < RR.size()) {
212 // Right piece is longer.
213 if (!RR.startswith(LR))
214 return false;
215 ++LI;
216 assert(LI != LN && "Strings not the same length after all?");
217 RR = RR.substr(LR.size());
218 LR = L[LI];
219 } else {
220 // Left piece is longer.
221 if (!LR.startswith(RR))
222 return false;
223 ++RI;
224 assert(RI != RN && "Strings not the same length after all?");
225 LR = LR.substr(RR.size());
226 RR = R[RI].Data;
227 }
228 }
229}
230
231static std::pair<FileID, llvm::StringRef::size_type>
232FindMacro(const PCHPredefinesBlocks &Buffers, llvm::StringRef MacroDef) {
233 std::pair<FileID, llvm::StringRef::size_type> Res;
234 for (unsigned I = 0, N = Buffers.size(); I != N; ++I) {
235 Res.second = Buffers[I].Data.find(MacroDef);
236 if (Res.second != llvm::StringRef::npos) {
237 Res.first = Buffers[I].BufferID;
238 break;
239 }
240 }
241 return Res;
242}
243
244bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000245 llvm::StringRef OriginalFileName,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000246 std::string &SuggestedPredefines) {
Daniel Dunbarc7162932009-11-11 23:58:53 +0000247 // We are in the context of an implicit include, so the predefines buffer will
248 // have a #include entry for the PCH file itself (as normalized by the
249 // preprocessor initialization). Find it and skip over it in the checking
250 // below.
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000251 llvm::SmallString<256> PCHInclude;
252 PCHInclude += "#include \"";
Daniel Dunbarc7162932009-11-11 23:58:53 +0000253 PCHInclude += NormalizeDashIncludePath(OriginalFileName);
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000254 PCHInclude += "\"\n";
255 std::pair<llvm::StringRef,llvm::StringRef> Split =
256 llvm::StringRef(PP.getPredefines()).split(PCHInclude.str());
257 llvm::StringRef Left = Split.first, Right = Split.second;
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000258 if (Left == PP.getPredefines()) {
259 Error("Missing PCH include entry!");
260 return true;
261 }
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000262
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000263 // If the concatenation of all the PCH buffers is equal to the adjusted
264 // command line, we're done.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000265 llvm::SmallVector<llvm::StringRef, 2> CommandLine;
266 CommandLine.push_back(Left);
267 CommandLine.push_back(Right);
268 if (EqualConcatenations(CommandLine, Buffers))
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000269 return false;
270
271 SourceManager &SourceMgr = PP.getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000272
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000273 // The predefines buffers are different. Determine what the differences are,
274 // and whether they require us to reject the PCH file.
Daniel Dunbare6750492009-11-13 16:46:11 +0000275 llvm::SmallVector<llvm::StringRef, 8> PCHLines;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000276 for (unsigned I = 0, N = Buffers.size(); I != N; ++I)
277 Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Daniel Dunbare6750492009-11-13 16:46:11 +0000278
279 llvm::SmallVector<llvm::StringRef, 8> CmdLineLines;
280 Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000281
282 // Pick out implicit #includes after the PCH and don't consider them for
283 // validation; we will insert them into SuggestedPredefines so that the
284 // preprocessor includes them.
285 std::string IncludesAfterPCH;
286 llvm::SmallVector<llvm::StringRef, 8> AfterPCHLines;
287 Right.split(AfterPCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
288 for (unsigned i = 0, e = AfterPCHLines.size(); i != e; ++i) {
289 if (AfterPCHLines[i].startswith("#include ")) {
290 IncludesAfterPCH += AfterPCHLines[i];
291 IncludesAfterPCH += '\n';
292 } else {
293 CmdLineLines.push_back(AfterPCHLines[i]);
294 }
295 }
296
297 // Make sure we add the includes last into SuggestedPredefines before we
298 // exit this function.
299 struct AddIncludesRAII {
300 std::string &SuggestedPredefines;
301 std::string &IncludesAfterPCH;
302
303 AddIncludesRAII(std::string &SuggestedPredefines,
304 std::string &IncludesAfterPCH)
305 : SuggestedPredefines(SuggestedPredefines),
306 IncludesAfterPCH(IncludesAfterPCH) { }
307 ~AddIncludesRAII() {
308 SuggestedPredefines += IncludesAfterPCH;
309 }
310 } AddIncludes(SuggestedPredefines, IncludesAfterPCH);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000311
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000312 // Sort both sets of predefined buffer lines, since we allow some extra
313 // definitions and they may appear at any point in the output.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000314 std::sort(CmdLineLines.begin(), CmdLineLines.end());
315 std::sort(PCHLines.begin(), PCHLines.end());
316
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000317 // Determine which predefines that were used to build the PCH file are missing
318 // from the command line.
319 std::vector<llvm::StringRef> MissingPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000320 std::set_difference(PCHLines.begin(), PCHLines.end(),
321 CmdLineLines.begin(), CmdLineLines.end(),
322 std::back_inserter(MissingPredefines));
323
324 bool MissingDefines = false;
325 bool ConflictingDefines = false;
326 for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) {
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000327 llvm::StringRef Missing = MissingPredefines[I];
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000328 if (Missing.startswith("#include ")) {
329 // An -include was specified when generating the PCH; it is included in
330 // the PCH, just ignore it.
331 continue;
332 }
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000333 if (!Missing.startswith("#define ")) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000334 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
335 return true;
336 }
Mike Stump1eb44332009-09-09 15:08:12 +0000337
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000338 // This is a macro definition. Determine the name of the macro we're
339 // defining.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000340 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000341 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000342 = Missing.find_first_of("( \n\r", StartOfMacroName);
343 assert(EndOfMacroName != std::string::npos &&
344 "Couldn't find the end of the macro name");
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000345 llvm::StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000346
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000347 // Determine whether this macro was given a different definition on the
348 // command line.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000349 std::string MacroDefStart = "#define " + MacroName.str();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000350 std::string::size_type MacroDefLen = MacroDefStart.size();
Daniel Dunbare6750492009-11-13 16:46:11 +0000351 llvm::SmallVector<llvm::StringRef, 8>::iterator ConflictPos
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000352 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
353 MacroDefStart);
354 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000355 if (!ConflictPos->startswith(MacroDefStart)) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000356 // Different macro; we're done.
357 ConflictPos = CmdLineLines.end();
Mike Stump1eb44332009-09-09 15:08:12 +0000358 break;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000359 }
Mike Stump1eb44332009-09-09 15:08:12 +0000360
361 assert(ConflictPos->size() > MacroDefLen &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000362 "Invalid #define in predefines buffer?");
Mike Stump1eb44332009-09-09 15:08:12 +0000363 if ((*ConflictPos)[MacroDefLen] != ' ' &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000364 (*ConflictPos)[MacroDefLen] != '(')
365 continue; // Longer macro name; keep trying.
Mike Stump1eb44332009-09-09 15:08:12 +0000366
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000367 // We found a conflicting macro definition.
368 break;
369 }
Mike Stump1eb44332009-09-09 15:08:12 +0000370
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000371 if (ConflictPos != CmdLineLines.end()) {
372 Reader.Diag(diag::warn_cmdline_conflicting_macro_def)
373 << MacroName;
374
375 // Show the definition of this macro within the PCH file.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000376 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
377 FindMacro(Buffers, Missing);
378 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
379 SourceLocation PCHMissingLoc =
380 SourceMgr.getLocForStartOfFile(MacroLoc.first)
381 .getFileLocWithOffset(MacroLoc.second);
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000382 Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000383
384 ConflictingDefines = true;
385 continue;
386 }
Mike Stump1eb44332009-09-09 15:08:12 +0000387
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000388 // If the macro doesn't conflict, then we'll just pick up the macro
389 // definition from the PCH file. Warn the user that they made a mistake.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000390 if (ConflictingDefines)
391 continue; // Don't complain if there are already conflicting defs
Mike Stump1eb44332009-09-09 15:08:12 +0000392
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000393 if (!MissingDefines) {
394 Reader.Diag(diag::warn_cmdline_missing_macro_defs);
395 MissingDefines = true;
396 }
397
398 // Show the definition of this macro within the PCH file.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000399 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
400 FindMacro(Buffers, Missing);
401 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
402 SourceLocation PCHMissingLoc =
403 SourceMgr.getLocForStartOfFile(MacroLoc.first)
404 .getFileLocWithOffset(MacroLoc.second);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000405 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
406 }
Mike Stump1eb44332009-09-09 15:08:12 +0000407
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000408 if (ConflictingDefines)
409 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000410
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000411 // Determine what predefines were introduced based on command-line
412 // parameters that were not present when building the PCH
413 // file. Extra #defines are okay, so long as the identifiers being
414 // defined were not used within the precompiled header.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000415 std::vector<llvm::StringRef> ExtraPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000416 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
417 PCHLines.begin(), PCHLines.end(),
Mike Stump1eb44332009-09-09 15:08:12 +0000418 std::back_inserter(ExtraPredefines));
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000419 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000420 llvm::StringRef &Extra = ExtraPredefines[I];
421 if (!Extra.startswith("#define ")) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000422 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
423 return true;
424 }
425
426 // This is an extra macro definition. Determine the name of the
427 // macro we're defining.
428 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000429 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000430 = Extra.find_first_of("( \n\r", StartOfMacroName);
431 assert(EndOfMacroName != std::string::npos &&
432 "Couldn't find the end of the macro name");
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000433 llvm::StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000434
435 // Check whether this name was used somewhere in the PCH file. If
436 // so, defining it as a macro could change behavior, so we reject
437 // the PCH file.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000438 if (IdentifierInfo *II = Reader.get(MacroName)) {
Daniel Dunbar4fda42e2009-11-11 00:52:00 +0000439 Reader.Diag(diag::warn_macro_name_used_in_pch) << II;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000440 return true;
441 }
442
443 // Add this definition to the suggested predefines buffer.
444 SuggestedPredefines += Extra;
445 SuggestedPredefines += '\n';
446 }
447
448 // If we get here, it's because the predefines buffer had compatible
449 // contents. Accept the PCH file.
450 return false;
451}
452
Douglas Gregor12fab312010-03-16 16:35:32 +0000453void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
454 unsigned ID) {
455 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
456 ++NumHeaderInfos;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000457}
458
459void PCHValidator::ReadCounter(unsigned Value) {
460 PP.setCounterValue(Value);
461}
462
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000463//===----------------------------------------------------------------------===//
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000464// AST reader implementation
Douglas Gregor668c1a42009-04-21 22:25:48 +0000465//===----------------------------------------------------------------------===//
466
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000467void
Sebastian Redl571db7f2010-08-18 23:56:56 +0000468ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000469 DeserializationListener = Listener;
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000470}
471
Chris Lattner4c6f9522009-04-27 05:14:47 +0000472
Douglas Gregor668c1a42009-04-21 22:25:48 +0000473namespace {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000474class ASTSelectorLookupTrait {
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000475 ASTReader &Reader;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000476
477public:
Sebastian Redl5d050072010-08-04 17:20:04 +0000478 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000479 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +0000480 ObjCMethodList Instance, Factory;
481 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000482
483 typedef Selector external_key_type;
484 typedef external_key_type internal_key_type;
485
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000486 explicit ASTSelectorLookupTrait(ASTReader &Reader) : Reader(Reader) { }
Mike Stump1eb44332009-09-09 15:08:12 +0000487
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000488 static bool EqualKey(const internal_key_type& a,
489 const internal_key_type& b) {
490 return a == b;
491 }
Mike Stump1eb44332009-09-09 15:08:12 +0000492
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000493 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +0000494 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000495 }
Mike Stump1eb44332009-09-09 15:08:12 +0000496
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000497 // This hopefully will just get inlined and removed by the optimizer.
498 static const internal_key_type&
499 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump1eb44332009-09-09 15:08:12 +0000500
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000501 static std::pair<unsigned, unsigned>
502 ReadKeyDataLength(const unsigned char*& d) {
503 using namespace clang::io;
504 unsigned KeyLen = ReadUnalignedLE16(d);
505 unsigned DataLen = ReadUnalignedLE16(d);
506 return std::make_pair(KeyLen, DataLen);
507 }
Mike Stump1eb44332009-09-09 15:08:12 +0000508
Douglas Gregor83941df2009-04-25 17:48:32 +0000509 internal_key_type ReadKey(const unsigned char* d, unsigned) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000510 using namespace clang::io;
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000511 SelectorTable &SelTable = Reader.getContext()->Selectors;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000512 unsigned N = ReadUnalignedLE16(d);
Mike Stump1eb44332009-09-09 15:08:12 +0000513 IdentifierInfo *FirstII
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000514 = Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
515 if (N == 0)
516 return SelTable.getNullarySelector(FirstII);
517 else if (N == 1)
518 return SelTable.getUnarySelector(FirstII);
519
520 llvm::SmallVector<IdentifierInfo *, 16> Args;
521 Args.push_back(FirstII);
522 for (unsigned I = 1; I != N; ++I)
523 Args.push_back(Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)));
524
Douglas Gregor75fdb232009-05-22 22:45:36 +0000525 return SelTable.getSelector(N, Args.data());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000526 }
Mike Stump1eb44332009-09-09 15:08:12 +0000527
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000528 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen) {
529 using namespace clang::io;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000530
531 data_type Result;
532
Sebastian Redl5d050072010-08-04 17:20:04 +0000533 Result.ID = ReadUnalignedLE32(d);
534 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
535 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
536
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000537 // Load instance methods
538 ObjCMethodList *Prev = 0;
539 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000540 ObjCMethodDecl *Method
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000541 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl5d050072010-08-04 17:20:04 +0000542 if (!Result.Instance.Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000543 // This is the first method, which is the easy case.
Sebastian Redl5d050072010-08-04 17:20:04 +0000544 Result.Instance.Method = Method;
545 Prev = &Result.Instance;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000546 continue;
547 }
548
Ted Kremenek298ed872010-02-11 00:53:01 +0000549 ObjCMethodList *Mem =
550 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
551 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000552 Prev = Prev->Next;
553 }
554
555 // Load factory methods
556 Prev = 0;
557 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000558 ObjCMethodDecl *Method
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000559 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl5d050072010-08-04 17:20:04 +0000560 if (!Result.Factory.Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000561 // This is the first method, which is the easy case.
Sebastian Redl5d050072010-08-04 17:20:04 +0000562 Result.Factory.Method = Method;
563 Prev = &Result.Factory;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000564 continue;
565 }
566
Ted Kremenek298ed872010-02-11 00:53:01 +0000567 ObjCMethodList *Mem =
568 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
569 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000570 Prev = Prev->Next;
571 }
572
573 return Result;
574 }
575};
Mike Stump1eb44332009-09-09 15:08:12 +0000576
577} // end anonymous namespace
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000578
579/// \brief The on-disk hash table used for the global method pool.
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000580typedef OnDiskChainedHashTable<ASTSelectorLookupTrait>
581 ASTSelectorLookupTable;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000582
Sebastian Redlc3632732010-10-05 15:59:54 +0000583namespace clang {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000584class ASTIdentifierLookupTrait {
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000585 ASTReader &Reader;
Sebastian Redlc3632732010-10-05 15:59:54 +0000586 ASTReader::PerFileData &F;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000587
588 // If we know the IdentifierInfo in advance, it is here and we will
589 // not build a new one. Used when deserializing information about an
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000590 // identifier that was constructed before the AST file was read.
Douglas Gregor668c1a42009-04-21 22:25:48 +0000591 IdentifierInfo *KnownII;
592
593public:
594 typedef IdentifierInfo * data_type;
595
596 typedef const std::pair<const char*, unsigned> external_key_type;
597
598 typedef external_key_type internal_key_type;
599
Sebastian Redlc3632732010-10-05 15:59:54 +0000600 ASTIdentifierLookupTrait(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redld27d3fc2010-07-21 22:31:37 +0000601 IdentifierInfo *II = 0)
Sebastian Redlc3632732010-10-05 15:59:54 +0000602 : Reader(Reader), F(F), KnownII(II) { }
Mike Stump1eb44332009-09-09 15:08:12 +0000603
Douglas Gregor668c1a42009-04-21 22:25:48 +0000604 static bool EqualKey(const internal_key_type& a,
605 const internal_key_type& b) {
606 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
607 : false;
608 }
Mike Stump1eb44332009-09-09 15:08:12 +0000609
Douglas Gregor668c1a42009-04-21 22:25:48 +0000610 static unsigned ComputeHash(const internal_key_type& a) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000611 return llvm::HashString(llvm::StringRef(a.first, a.second));
Douglas Gregor668c1a42009-04-21 22:25:48 +0000612 }
Mike Stump1eb44332009-09-09 15:08:12 +0000613
Douglas Gregor668c1a42009-04-21 22:25:48 +0000614 // This hopefully will just get inlined and removed by the optimizer.
615 static const internal_key_type&
616 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump1eb44332009-09-09 15:08:12 +0000617
Douglas Gregor95f42922010-10-14 22:11:03 +0000618 // This hopefully will just get inlined and removed by the optimizer.
619 static const external_key_type&
620 GetExternalKey(const internal_key_type& x) { return x; }
621
Douglas Gregor668c1a42009-04-21 22:25:48 +0000622 static std::pair<unsigned, unsigned>
623 ReadKeyDataLength(const unsigned char*& d) {
624 using namespace clang::io;
Douglas Gregor5f8e3302009-04-25 20:26:24 +0000625 unsigned DataLen = ReadUnalignedLE16(d);
Douglas Gregord6595a42009-04-25 21:04:17 +0000626 unsigned KeyLen = ReadUnalignedLE16(d);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000627 return std::make_pair(KeyLen, DataLen);
628 }
Mike Stump1eb44332009-09-09 15:08:12 +0000629
Douglas Gregor668c1a42009-04-21 22:25:48 +0000630 static std::pair<const char*, unsigned>
631 ReadKey(const unsigned char* d, unsigned n) {
632 assert(n >= 2 && d[n-1] == '\0');
633 return std::make_pair((const char*) d, n-1);
634 }
Mike Stump1eb44332009-09-09 15:08:12 +0000635
636 IdentifierInfo *ReadData(const internal_key_type& k,
Douglas Gregor668c1a42009-04-21 22:25:48 +0000637 const unsigned char* d,
638 unsigned DataLen) {
639 using namespace clang::io;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000640 IdentID ID = ReadUnalignedLE32(d);
Douglas Gregora92193e2009-04-28 21:18:29 +0000641 bool IsInteresting = ID & 0x01;
642
643 // Wipe out the "is interesting" bit.
644 ID = ID >> 1;
645
646 if (!IsInteresting) {
Sebastian Redl083abdf2010-07-27 23:01:28 +0000647 // For uninteresting identifiers, just build the IdentifierInfo
Douglas Gregora92193e2009-04-28 21:18:29 +0000648 // and associate it with the persistent ID.
649 IdentifierInfo *II = KnownII;
650 if (!II)
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000651 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregora92193e2009-04-28 21:18:29 +0000652 Reader.SetIdentifierInfo(ID, II);
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000653 II->setIsFromAST();
Douglas Gregora92193e2009-04-28 21:18:29 +0000654 return II;
655 }
656
Douglas Gregor5998da52009-04-28 21:32:13 +0000657 unsigned Bits = ReadUnalignedLE16(d);
Douglas Gregor2deaea32009-04-22 18:49:13 +0000658 bool CPlusPlusOperatorKeyword = Bits & 0x01;
659 Bits >>= 1;
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000660 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
661 Bits >>= 1;
Douglas Gregor2deaea32009-04-22 18:49:13 +0000662 bool Poisoned = Bits & 0x01;
663 Bits >>= 1;
664 bool ExtensionToken = Bits & 0x01;
665 Bits >>= 1;
666 bool hasMacroDefinition = Bits & 0x01;
667 Bits >>= 1;
668 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
669 Bits >>= 10;
Mike Stump1eb44332009-09-09 15:08:12 +0000670
Douglas Gregor2deaea32009-04-22 18:49:13 +0000671 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregor5998da52009-04-28 21:32:13 +0000672 DataLen -= 6;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000673
674 // Build the IdentifierInfo itself and link the identifier ID with
675 // the new IdentifierInfo.
676 IdentifierInfo *II = KnownII;
677 if (!II)
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000678 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000679 Reader.SetIdentifierInfo(ID, II);
680
Douglas Gregor2deaea32009-04-22 18:49:13 +0000681 // Set or check the various bits in the IdentifierInfo structure.
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000682 // Token IDs are read-only.
683 if (HasRevertedTokenIDToIdentifier)
684 II->RevertTokenIDToIdentifier();
Douglas Gregor2deaea32009-04-22 18:49:13 +0000685 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
Mike Stump1eb44332009-09-09 15:08:12 +0000686 assert(II->isExtensionToken() == ExtensionToken &&
Douglas Gregor2deaea32009-04-22 18:49:13 +0000687 "Incorrect extension token flag");
688 (void)ExtensionToken;
689 II->setIsPoisoned(Poisoned);
690 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
691 "Incorrect C++ operator keyword flag");
692 (void)CPlusPlusOperatorKeyword;
693
Douglas Gregor37e26842009-04-21 23:56:24 +0000694 // If this identifier is a macro, deserialize the macro
695 // definition.
696 if (hasMacroDefinition) {
Douglas Gregor5998da52009-04-28 21:32:13 +0000697 uint32_t Offset = ReadUnalignedLE32(d);
Douglas Gregor295a2a62010-10-30 00:23:06 +0000698 Reader.SetIdentifierIsMacro(II, F, Offset);
Douglas Gregor5998da52009-04-28 21:32:13 +0000699 DataLen -= 4;
Douglas Gregor37e26842009-04-21 23:56:24 +0000700 }
Douglas Gregor668c1a42009-04-21 22:25:48 +0000701
702 // Read all of the declarations visible at global scope with this
703 // name.
Chris Lattner6bf690f2009-04-27 22:17:41 +0000704 if (Reader.getContext() == 0) return II;
Douglas Gregord89275b2009-07-06 18:54:52 +0000705 if (DataLen > 0) {
706 llvm::SmallVector<uint32_t, 4> DeclIDs;
707 for (; DataLen > 0; DataLen -= 4)
708 DeclIDs.push_back(ReadUnalignedLE32(d));
709 Reader.SetGloballyVisibleDecls(II, DeclIDs);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000710 }
Mike Stump1eb44332009-09-09 15:08:12 +0000711
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000712 II->setIsFromAST();
Douglas Gregor668c1a42009-04-21 22:25:48 +0000713 return II;
714 }
715};
Mike Stump1eb44332009-09-09 15:08:12 +0000716
717} // end anonymous namespace
Douglas Gregor668c1a42009-04-21 22:25:48 +0000718
719/// \brief The on-disk hash table used to contain information about
720/// all of the identifiers in the program.
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000721typedef OnDiskChainedHashTable<ASTIdentifierLookupTrait>
722 ASTIdentifierLookupTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000723
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000724namespace {
725class ASTDeclContextNameLookupTrait {
726 ASTReader &Reader;
727
728public:
729 /// \brief Pair of begin/end iterators for DeclIDs.
730 typedef std::pair<DeclID *, DeclID *> data_type;
731
732 /// \brief Special internal key for declaration names.
733 /// The hash table creates keys for comparison; we do not create
734 /// a DeclarationName for the internal key to avoid deserializing types.
735 struct DeclNameKey {
736 DeclarationName::NameKind Kind;
737 uint64_t Data;
738 DeclNameKey() : Kind((DeclarationName::NameKind)0), Data(0) { }
739 };
740
741 typedef DeclarationName external_key_type;
742 typedef DeclNameKey internal_key_type;
743
744 explicit ASTDeclContextNameLookupTrait(ASTReader &Reader) : Reader(Reader) { }
745
746 static bool EqualKey(const internal_key_type& a,
747 const internal_key_type& b) {
748 return a.Kind == b.Kind && a.Data == b.Data;
749 }
750
751 unsigned ComputeHash(const DeclNameKey &Key) const {
752 llvm::FoldingSetNodeID ID;
753 ID.AddInteger(Key.Kind);
754
755 switch (Key.Kind) {
756 case DeclarationName::Identifier:
757 case DeclarationName::CXXLiteralOperatorName:
758 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
759 break;
760 case DeclarationName::ObjCZeroArgSelector:
761 case DeclarationName::ObjCOneArgSelector:
762 case DeclarationName::ObjCMultiArgSelector:
763 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
764 break;
765 case DeclarationName::CXXConstructorName:
766 case DeclarationName::CXXDestructorName:
767 case DeclarationName::CXXConversionFunctionName:
768 ID.AddInteger((TypeID)Key.Data);
769 break;
770 case DeclarationName::CXXOperatorName:
771 ID.AddInteger((OverloadedOperatorKind)Key.Data);
772 break;
773 case DeclarationName::CXXUsingDirective:
774 break;
775 }
776
777 return ID.ComputeHash();
778 }
779
780 internal_key_type GetInternalKey(const external_key_type& Name) const {
781 DeclNameKey Key;
782 Key.Kind = Name.getNameKind();
783 switch (Name.getNameKind()) {
784 case DeclarationName::Identifier:
785 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
786 break;
787 case DeclarationName::ObjCZeroArgSelector:
788 case DeclarationName::ObjCOneArgSelector:
789 case DeclarationName::ObjCMultiArgSelector:
790 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
791 break;
792 case DeclarationName::CXXConstructorName:
793 case DeclarationName::CXXDestructorName:
794 case DeclarationName::CXXConversionFunctionName:
795 Key.Data = Reader.GetTypeID(Name.getCXXNameType());
796 break;
797 case DeclarationName::CXXOperatorName:
798 Key.Data = Name.getCXXOverloadedOperator();
799 break;
800 case DeclarationName::CXXLiteralOperatorName:
801 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
802 break;
803 case DeclarationName::CXXUsingDirective:
804 break;
805 }
Michael J. Spencer20249a12010-10-21 03:16:25 +0000806
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000807 return Key;
808 }
809
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +0000810 external_key_type GetExternalKey(const internal_key_type& Key) const {
811 ASTContext *Context = Reader.getContext();
812 switch (Key.Kind) {
813 case DeclarationName::Identifier:
814 return DeclarationName((IdentifierInfo*)Key.Data);
815
816 case DeclarationName::ObjCZeroArgSelector:
817 case DeclarationName::ObjCOneArgSelector:
818 case DeclarationName::ObjCMultiArgSelector:
819 return DeclarationName(Selector(Key.Data));
820
821 case DeclarationName::CXXConstructorName:
822 return Context->DeclarationNames.getCXXConstructorName(
823 Context->getCanonicalType(Reader.GetType(Key.Data)));
824
825 case DeclarationName::CXXDestructorName:
826 return Context->DeclarationNames.getCXXDestructorName(
827 Context->getCanonicalType(Reader.GetType(Key.Data)));
828
829 case DeclarationName::CXXConversionFunctionName:
830 return Context->DeclarationNames.getCXXConversionFunctionName(
831 Context->getCanonicalType(Reader.GetType(Key.Data)));
832
833 case DeclarationName::CXXOperatorName:
834 return Context->DeclarationNames.getCXXOperatorName(
835 (OverloadedOperatorKind)Key.Data);
836
837 case DeclarationName::CXXLiteralOperatorName:
838 return Context->DeclarationNames.getCXXLiteralOperatorName(
839 (IdentifierInfo*)Key.Data);
840
841 case DeclarationName::CXXUsingDirective:
842 return DeclarationName::getUsingDirectiveName();
843 }
844
845 llvm_unreachable("Invalid Name Kind ?");
846 }
847
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000848 static std::pair<unsigned, unsigned>
849 ReadKeyDataLength(const unsigned char*& d) {
850 using namespace clang::io;
851 unsigned KeyLen = ReadUnalignedLE16(d);
852 unsigned DataLen = ReadUnalignedLE16(d);
853 return std::make_pair(KeyLen, DataLen);
854 }
855
856 internal_key_type ReadKey(const unsigned char* d, unsigned) {
857 using namespace clang::io;
858
859 DeclNameKey Key;
860 Key.Kind = (DeclarationName::NameKind)*d++;
861 switch (Key.Kind) {
862 case DeclarationName::Identifier:
863 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
864 break;
865 case DeclarationName::ObjCZeroArgSelector:
866 case DeclarationName::ObjCOneArgSelector:
867 case DeclarationName::ObjCMultiArgSelector:
Michael J. Spencer20249a12010-10-21 03:16:25 +0000868 Key.Data =
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000869 (uint64_t)Reader.DecodeSelector(ReadUnalignedLE32(d)).getAsOpaquePtr();
870 break;
871 case DeclarationName::CXXConstructorName:
872 case DeclarationName::CXXDestructorName:
873 case DeclarationName::CXXConversionFunctionName:
874 Key.Data = ReadUnalignedLE32(d); // TypeID
875 break;
876 case DeclarationName::CXXOperatorName:
877 Key.Data = *d++; // OverloadedOperatorKind
878 break;
879 case DeclarationName::CXXLiteralOperatorName:
880 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
881 break;
882 case DeclarationName::CXXUsingDirective:
883 break;
884 }
Michael J. Spencer20249a12010-10-21 03:16:25 +0000885
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000886 return Key;
887 }
888
889 data_type ReadData(internal_key_type, const unsigned char* d,
890 unsigned DataLen) {
891 using namespace clang::io;
892 unsigned NumDecls = ReadUnalignedLE16(d);
893 DeclID *Start = (DeclID *)d;
894 return std::make_pair(Start, Start + NumDecls);
895 }
896};
897
898} // end anonymous namespace
899
900/// \brief The on-disk hash table used for the DeclContext's Name lookup table.
901typedef OnDiskChainedHashTable<ASTDeclContextNameLookupTrait>
902 ASTDeclContextNameLookupTable;
903
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000904bool ASTReader::ReadDeclContextStorage(llvm::BitstreamCursor &Cursor,
905 const std::pair<uint64_t, uint64_t> &Offsets,
906 DeclContextInfo &Info) {
907 SavedStreamPosition SavedPosition(Cursor);
908 // First the lexical decls.
909 if (Offsets.first != 0) {
910 Cursor.JumpToBit(Offsets.first);
911
912 RecordData Record;
913 const char *Blob;
914 unsigned BlobLen;
915 unsigned Code = Cursor.ReadCode();
916 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
917 if (RecCode != DECL_CONTEXT_LEXICAL) {
918 Error("Expected lexical block");
919 return true;
920 }
921
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000922 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob);
923 Info.NumLexicalDecls = BlobLen / sizeof(KindDeclIDPair);
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000924 } else {
925 Info.LexicalDecls = 0;
926 Info.NumLexicalDecls = 0;
927 }
928
929 // Now the lookup table.
930 if (Offsets.second != 0) {
931 Cursor.JumpToBit(Offsets.second);
932
933 RecordData Record;
934 const char *Blob;
935 unsigned BlobLen;
936 unsigned Code = Cursor.ReadCode();
937 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
938 if (RecCode != DECL_CONTEXT_VISIBLE) {
939 Error("Expected visible lookup table block");
940 return true;
941 }
942 Info.NameLookupTableData
943 = ASTDeclContextNameLookupTable::Create(
944 (const unsigned char *)Blob + Record[0],
945 (const unsigned char *)Blob,
946 ASTDeclContextNameLookupTrait(*this));
Sebastian Redl0ea8f7f2010-08-24 00:50:00 +0000947 } else {
948 Info.NameLookupTableData = 0;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000949 }
950
951 return false;
952}
953
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000954void ASTReader::Error(const char *Msg) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000955 Diag(diag::err_fe_pch_malformed) << Msg;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000956}
957
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000958/// \brief Tell the AST listener about the predefines buffers in the chain.
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000959bool ASTReader::CheckPredefinesBuffers() {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000960 if (Listener)
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000961 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000962 ActualOriginalFileName,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000963 SuggestedPredefines);
Douglas Gregore721f952009-04-28 18:58:38 +0000964 return false;
Douglas Gregore1d918e2009-04-10 23:10:45 +0000965}
966
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000967//===----------------------------------------------------------------------===//
968// Source Manager Deserialization
969//===----------------------------------------------------------------------===//
970
Douglas Gregorbd945002009-04-13 16:31:14 +0000971/// \brief Read the line table in the source manager block.
Sebastian Redlc3632732010-10-05 15:59:54 +0000972/// \returns true if there was an error.
973bool ASTReader::ParseLineTable(PerFileData &F,
974 llvm::SmallVectorImpl<uint64_t> &Record) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000975 unsigned Idx = 0;
976 LineTableInfo &LineTable = SourceMgr.getLineTable();
977
978 // Parse the file names
Douglas Gregorff0a9872009-04-13 17:12:42 +0000979 std::map<int, int> FileIDs;
980 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000981 // Extract the file name
982 unsigned FilenameLen = Record[Idx++];
983 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
984 Idx += FilenameLen;
Douglas Gregore650c8c2009-07-07 00:12:59 +0000985 MaybeAddSystemRootToFilename(Filename);
Mike Stump1eb44332009-09-09 15:08:12 +0000986 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
Douglas Gregorff0a9872009-04-13 17:12:42 +0000987 Filename.size());
Douglas Gregorbd945002009-04-13 16:31:14 +0000988 }
989
990 // Parse the line entries
991 std::vector<LineEntry> Entries;
992 while (Idx < Record.size()) {
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000993 int FID = Record[Idx++];
Douglas Gregorbd945002009-04-13 16:31:14 +0000994
995 // Extract the line entries
996 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000997 assert(NumEntries && "Numentries is 00000");
Douglas Gregorbd945002009-04-13 16:31:14 +0000998 Entries.clear();
999 Entries.reserve(NumEntries);
1000 for (unsigned I = 0; I != NumEntries; ++I) {
1001 unsigned FileOffset = Record[Idx++];
1002 unsigned LineNo = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +00001003 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump1eb44332009-09-09 15:08:12 +00001004 SrcMgr::CharacteristicKind FileKind
Douglas Gregorbd945002009-04-13 16:31:14 +00001005 = (SrcMgr::CharacteristicKind)Record[Idx++];
1006 unsigned IncludeOffset = Record[Idx++];
1007 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1008 FileKind, IncludeOffset));
1009 }
1010 LineTable.AddEntry(FID, Entries);
1011 }
1012
1013 return false;
1014}
1015
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001016namespace {
1017
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001018class ASTStatData {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001019public:
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001020 const ino_t ino;
1021 const dev_t dev;
1022 const mode_t mode;
1023 const time_t mtime;
1024 const off_t size;
Mike Stump1eb44332009-09-09 15:08:12 +00001025
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001026 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Chris Lattner74e976b2010-11-23 19:28:12 +00001027 : ino(i), dev(d), mode(mo), mtime(m), size(s) {}
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001028};
1029
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001030class ASTStatLookupTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001031 public:
1032 typedef const char *external_key_type;
1033 typedef const char *internal_key_type;
1034
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001035 typedef ASTStatData data_type;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001036
1037 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001038 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001039 }
1040
1041 static internal_key_type GetInternalKey(const char *path) { return path; }
1042
1043 static bool EqualKey(internal_key_type a, internal_key_type b) {
1044 return strcmp(a, b) == 0;
1045 }
1046
1047 static std::pair<unsigned, unsigned>
1048 ReadKeyDataLength(const unsigned char*& d) {
1049 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1050 unsigned DataLen = (unsigned) *d++;
1051 return std::make_pair(KeyLen + 1, DataLen);
1052 }
1053
1054 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
1055 return (const char *)d;
1056 }
1057
1058 static data_type ReadData(const internal_key_type, const unsigned char *d,
1059 unsigned /*DataLen*/) {
1060 using namespace clang::io;
1061
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001062 ino_t ino = (ino_t) ReadUnalignedLE32(d);
1063 dev_t dev = (dev_t) ReadUnalignedLE32(d);
1064 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump1eb44332009-09-09 15:08:12 +00001065 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001066 off_t size = (off_t) ReadUnalignedLE64(d);
1067 return data_type(ino, dev, mode, mtime, size);
1068 }
1069};
1070
1071/// \brief stat() cache for precompiled headers.
1072///
1073/// This cache is very similar to the stat cache used by pretokenized
1074/// headers.
Chris Lattner10e286a2010-11-23 19:19:34 +00001075class ASTStatCache : public FileSystemStatCache {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001076 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001077 CacheTy *Cache;
1078
1079 unsigned &NumStatHits, &NumStatMisses;
Mike Stump1eb44332009-09-09 15:08:12 +00001080public:
Chris Lattner74e976b2010-11-23 19:28:12 +00001081 ASTStatCache(const unsigned char *Buckets, const unsigned char *Base,
1082 unsigned &NumStatHits, unsigned &NumStatMisses)
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001083 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
1084 Cache = CacheTy::Create(Buckets, Base);
1085 }
1086
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001087 ~ASTStatCache() { delete Cache; }
Mike Stump1eb44332009-09-09 15:08:12 +00001088
Chris Lattner898a0612010-11-23 21:17:56 +00001089 LookupResult getStat(const char *Path, struct stat &StatBuf,
1090 int *FileDescriptor) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001091 // Do the lookup for the file's data in the AST file.
Chris Lattner10e286a2010-11-23 19:19:34 +00001092 CacheTy::iterator I = Cache->find(Path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001093
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001094 // If we don't get a hit in the AST file just forward to 'stat'.
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001095 if (I == Cache->end()) {
1096 ++NumStatMisses;
Chris Lattner898a0612010-11-23 21:17:56 +00001097 return statChained(Path, StatBuf, FileDescriptor);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001098 }
Mike Stump1eb44332009-09-09 15:08:12 +00001099
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001100 ++NumStatHits;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001101 ASTStatData Data = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001102
Chris Lattner10e286a2010-11-23 19:19:34 +00001103 StatBuf.st_ino = Data.ino;
1104 StatBuf.st_dev = Data.dev;
1105 StatBuf.st_mtime = Data.mtime;
1106 StatBuf.st_mode = Data.mode;
1107 StatBuf.st_size = Data.size;
Chris Lattnerd6f61112010-11-23 20:05:15 +00001108 return CacheExists;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001109 }
1110};
1111} // end anonymous namespace
1112
1113
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001114/// \brief Read a source manager block
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001115ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(PerFileData &F) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001116 using namespace SrcMgr;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001117
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001118 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001119
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001120 // Set the source-location entry cursor to the current position in
1121 // the stream. This cursor will be used to read the contents of the
1122 // source manager block initially, and then lazily read
1123 // source-location entries as needed.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001124 SLocEntryCursor = F.Stream;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001125
1126 // The stream itself is going to skip over the source manager block.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001127 if (F.Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001128 Error("malformed block record in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001129 return Failure;
1130 }
1131
1132 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001133 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001134 Error("malformed source manager block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001135 return Failure;
1136 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001137
Douglas Gregor14f79002009-04-10 03:52:48 +00001138 RecordData Record;
1139 while (true) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001140 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregor14f79002009-04-10 03:52:48 +00001141 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001142 if (SLocEntryCursor.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001143 Error("error at end of Source Manager block in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001144 return Failure;
1145 }
Douglas Gregore1d918e2009-04-10 23:10:45 +00001146 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +00001147 }
Mike Stump1eb44332009-09-09 15:08:12 +00001148
Douglas Gregor14f79002009-04-10 03:52:48 +00001149 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1150 // No known subblocks, always skip them.
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001151 SLocEntryCursor.ReadSubBlockID();
1152 if (SLocEntryCursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001153 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001154 return Failure;
1155 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001156 continue;
1157 }
Mike Stump1eb44332009-09-09 15:08:12 +00001158
Douglas Gregor14f79002009-04-10 03:52:48 +00001159 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001160 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregor14f79002009-04-10 03:52:48 +00001161 continue;
1162 }
Mike Stump1eb44332009-09-09 15:08:12 +00001163
Douglas Gregor14f79002009-04-10 03:52:48 +00001164 // Read a record.
1165 const char *BlobStart;
1166 unsigned BlobLen;
1167 Record.clear();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001168 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001169 default: // Default behavior: ignore.
1170 break;
1171
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001172 case SM_LINE_TABLE:
Sebastian Redlc3632732010-10-05 15:59:54 +00001173 if (ParseLineTable(F, Record))
Douglas Gregorbd945002009-04-13 16:31:14 +00001174 return Failure;
Chris Lattner2c78b872009-04-14 23:22:57 +00001175 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001176
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001177 case SM_SLOC_FILE_ENTRY:
1178 case SM_SLOC_BUFFER_ENTRY:
1179 case SM_SLOC_INSTANTIATION_ENTRY:
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001180 // Once we hit one of the source location entries, we're done.
1181 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +00001182 }
1183 }
1184}
1185
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001186/// \brief If a header file is not found at the path that we expect it to be
1187/// and the PCH file was moved from its original location, try to resolve the
1188/// file by assuming that header+PCH were moved together and the header is in
1189/// the same place relative to the PCH.
1190static std::string
1191resolveFileRelativeToOriginalDir(const std::string &Filename,
1192 const std::string &OriginalDir,
1193 const std::string &CurrDir) {
1194 assert(OriginalDir != CurrDir &&
1195 "No point trying to resolve the file if the PCH dir didn't change");
1196 using namespace llvm::sys;
1197 llvm::SmallString<128> filePath(Filename);
1198 fs::make_absolute(filePath);
1199 assert(path::is_absolute(OriginalDir));
1200 llvm::SmallString<128> currPCHPath(CurrDir);
1201
1202 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1203 fileDirE = path::end(path::parent_path(filePath));
1204 path::const_iterator origDirI = path::begin(OriginalDir),
1205 origDirE = path::end(OriginalDir);
1206 // Skip the common path components from filePath and OriginalDir.
1207 while (fileDirI != fileDirE && origDirI != origDirE &&
1208 *fileDirI == *origDirI) {
1209 ++fileDirI;
1210 ++origDirI;
1211 }
1212 for (; origDirI != origDirE; ++origDirI)
1213 path::append(currPCHPath, "..");
1214 path::append(currPCHPath, fileDirI, fileDirE);
1215 path::append(currPCHPath, path::filename(Filename));
1216 return currPCHPath.str();
1217}
1218
Sebastian Redl190faf72010-07-20 21:50:20 +00001219/// \brief Get a cursor that's correctly positioned for reading the source
1220/// location entry with the given ID.
Sebastian Redlc3632732010-10-05 15:59:54 +00001221ASTReader::PerFileData *ASTReader::SLocCursorForID(unsigned ID) {
Sebastian Redl190faf72010-07-20 21:50:20 +00001222 assert(ID != 0 && ID <= TotalNumSLocEntries &&
1223 "SLocCursorForID should only be called for real IDs.");
1224
1225 ID -= 1;
1226 PerFileData *F = 0;
1227 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1228 F = Chain[N - I - 1];
1229 if (ID < F->LocalNumSLocEntries)
1230 break;
1231 ID -= F->LocalNumSLocEntries;
1232 }
1233 assert(F && F->LocalNumSLocEntries > ID && "Chain corrupted");
1234
1235 F->SLocEntryCursor.JumpToBit(F->SLocOffsets[ID]);
Sebastian Redlc3632732010-10-05 15:59:54 +00001236 return F;
Sebastian Redl190faf72010-07-20 21:50:20 +00001237}
1238
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001239/// \brief Read in the source location entry with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001240ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(unsigned ID) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001241 if (ID == 0)
1242 return Success;
1243
1244 if (ID > TotalNumSLocEntries) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001245 Error("source location entry ID out-of-range for AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001246 return Failure;
1247 }
1248
Sebastian Redlc3632732010-10-05 15:59:54 +00001249 PerFileData *F = SLocCursorForID(ID);
1250 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001251
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001252 ++NumSLocEntriesRead;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001253 unsigned Code = SLocEntryCursor.ReadCode();
1254 if (Code == llvm::bitc::END_BLOCK ||
1255 Code == llvm::bitc::ENTER_SUBBLOCK ||
1256 Code == llvm::bitc::DEFINE_ABBREV) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001257 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001258 return Failure;
1259 }
1260
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001261 RecordData Record;
1262 const char *BlobStart;
1263 unsigned BlobLen;
1264 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1265 default:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001266 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001267 return Failure;
1268
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001269 case SM_SLOC_FILE_ENTRY: {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001270 std::string Filename(BlobStart, BlobStart + BlobLen);
1271 MaybeAddSystemRootToFilename(Filename);
Chris Lattner39b49bc2010-11-23 08:35:12 +00001272 const FileEntry *File = FileMgr.getFile(Filename);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001273 if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() &&
1274 OriginalDir != CurrentDir) {
1275 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1276 OriginalDir,
1277 CurrentDir);
1278 if (!resolved.empty())
1279 File = FileMgr.getFile(resolved);
1280 }
Axel Naumann04331162011-01-27 10:55:51 +00001281 if (File == 0)
1282 File = FileMgr.getVirtualFile(Filename, (off_t)Record[4],
1283 (time_t)Record[5]);
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001284 if (File == 0) {
1285 std::string ErrorStr = "could not find file '";
Douglas Gregore650c8c2009-07-07 00:12:59 +00001286 ErrorStr += Filename;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001287 ErrorStr += "' referenced by AST file";
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001288 Error(ErrorStr.c_str());
1289 return Failure;
1290 }
Mike Stump1eb44332009-09-09 15:08:12 +00001291
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001292 if (Record.size() < 6) {
Ted Kremenek1857f622010-03-18 21:23:05 +00001293 Error("source location entry is incorrect");
1294 return Failure;
1295 }
1296
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001297 if (!DisableValidation &&
1298 ((off_t)Record[4] != File->getSize()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001299#if !defined(LLVM_ON_WIN32)
1300 // In our regression testing, the Windows file system seems to
1301 // have inconsistent modification times that sometimes
1302 // erroneously trigger this error-handling path.
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001303 || (time_t)Record[5] != File->getModificationTime()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001304#endif
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001305 )) {
Douglas Gregor2d52be52010-03-21 22:49:54 +00001306 Diag(diag::err_fe_pch_file_modified)
1307 << Filename;
1308 return Failure;
1309 }
1310
Chris Lattner75dfb652010-11-23 09:19:42 +00001311 FileID FID = SourceMgr.createFileID(File, ReadSourceLocation(*F, Record[1]),
1312 (SrcMgr::CharacteristicKind)Record[2],
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001313 ID, Record[0]);
1314 if (Record[3])
1315 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile())
1316 .setHasLineDirectives();
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001317
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001318 break;
1319 }
1320
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001321 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001322 const char *Name = BlobStart;
1323 unsigned Offset = Record[0];
1324 unsigned Code = SLocEntryCursor.ReadCode();
1325 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001326 unsigned RecCode
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001327 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001328
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001329 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001330 Error("AST record has invalid code");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001331 return Failure;
1332 }
1333
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001334 llvm::MemoryBuffer *Buffer
Chris Lattnera0a270c2010-04-05 22:42:27 +00001335 = llvm::MemoryBuffer::getMemBuffer(llvm::StringRef(BlobStart, BlobLen - 1),
1336 Name);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001337 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID, Offset);
Mike Stump1eb44332009-09-09 15:08:12 +00001338
Douglas Gregor92b059e2009-04-28 20:33:11 +00001339 if (strcmp(Name, "<built-in>") == 0) {
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +00001340 PCHPredefinesBlock Block = {
1341 BufferID,
1342 llvm::StringRef(BlobStart, BlobLen - 1)
1343 };
1344 PCHPredefinesBuffers.push_back(Block);
Douglas Gregor92b059e2009-04-28 20:33:11 +00001345 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001346
1347 break;
1348 }
1349
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001350 case SM_SLOC_INSTANTIATION_ENTRY: {
Sebastian Redlc3632732010-10-05 15:59:54 +00001351 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001352 SourceMgr.createInstantiationLoc(SpellingLoc,
Sebastian Redlc3632732010-10-05 15:59:54 +00001353 ReadSourceLocation(*F, Record[2]),
1354 ReadSourceLocation(*F, Record[3]),
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001355 Record[4],
1356 ID,
1357 Record[0]);
1358 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001359 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001360 }
1361
1362 return Success;
1363}
1364
Chris Lattner6367f6d2009-04-27 01:05:14 +00001365/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1366/// specified cursor. Read the abbreviations that are at the top of the block
1367/// and then leave the cursor pointing into the block.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001368bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattner6367f6d2009-04-27 01:05:14 +00001369 unsigned BlockID) {
1370 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001371 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001372 return Failure;
1373 }
Mike Stump1eb44332009-09-09 15:08:12 +00001374
Chris Lattner6367f6d2009-04-27 01:05:14 +00001375 while (true) {
Douglas Gregorecdcb882010-10-20 22:00:55 +00001376 uint64_t Offset = Cursor.GetCurrentBitNo();
Chris Lattner6367f6d2009-04-27 01:05:14 +00001377 unsigned Code = Cursor.ReadCode();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001378
Chris Lattner6367f6d2009-04-27 01:05:14 +00001379 // We expect all abbrevs to be at the start of the block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001380 if (Code != llvm::bitc::DEFINE_ABBREV) {
1381 Cursor.JumpToBit(Offset);
Chris Lattner6367f6d2009-04-27 01:05:14 +00001382 return false;
Douglas Gregorecdcb882010-10-20 22:00:55 +00001383 }
Chris Lattner6367f6d2009-04-27 01:05:14 +00001384 Cursor.ReadAbbrevRecord();
1385 }
1386}
1387
Douglas Gregor89d99802010-11-30 06:16:57 +00001388PreprocessedEntity *ASTReader::ReadMacroRecord(PerFileData &F, uint64_t Offset) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001389 assert(PP && "Forgot to set Preprocessor ?");
Douglas Gregorecdcb882010-10-20 22:00:55 +00001390 llvm::BitstreamCursor &Stream = F.MacroCursor;
Mike Stump1eb44332009-09-09 15:08:12 +00001391
Douglas Gregor37e26842009-04-21 23:56:24 +00001392 // Keep track of where we are in the stream, then jump back there
1393 // after reading this macro.
1394 SavedStreamPosition SavedPosition(Stream);
1395
1396 Stream.JumpToBit(Offset);
1397 RecordData Record;
1398 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1399 MacroInfo *Macro = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001400
Douglas Gregor37e26842009-04-21 23:56:24 +00001401 while (true) {
1402 unsigned Code = Stream.ReadCode();
1403 switch (Code) {
1404 case llvm::bitc::END_BLOCK:
Douglas Gregor89d99802010-11-30 06:16:57 +00001405 return 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001406
1407 case llvm::bitc::ENTER_SUBBLOCK:
1408 // No known subblocks, always skip them.
1409 Stream.ReadSubBlockID();
1410 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001411 Error("malformed block record in AST file");
Douglas Gregor89d99802010-11-30 06:16:57 +00001412 return 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001413 }
1414 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001415
Douglas Gregor37e26842009-04-21 23:56:24 +00001416 case llvm::bitc::DEFINE_ABBREV:
1417 Stream.ReadAbbrevRecord();
1418 continue;
1419 default: break;
1420 }
1421
1422 // Read a record.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001423 const char *BlobStart = 0;
1424 unsigned BlobLen = 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001425 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001426 PreprocessorRecordTypes RecType =
Michael J. Spencer20249a12010-10-21 03:16:25 +00001427 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart,
Douglas Gregorecdcb882010-10-20 22:00:55 +00001428 BlobLen);
Douglas Gregor37e26842009-04-21 23:56:24 +00001429 switch (RecType) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001430 case PP_MACRO_OBJECT_LIKE:
1431 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001432 // If we already have a macro, that means that we've hit the end
1433 // of the definition of the macro we were looking for. We're
1434 // done.
1435 if (Macro)
Douglas Gregor89d99802010-11-30 06:16:57 +00001436 return 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001437
1438 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1439 if (II == 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001440 Error("macro must have a name in AST file");
Douglas Gregor89d99802010-11-30 06:16:57 +00001441 return 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001442 }
Sebastian Redlc3632732010-10-05 15:59:54 +00001443 SourceLocation Loc = ReadSourceLocation(F, Record[1]);
Douglas Gregor37e26842009-04-21 23:56:24 +00001444 bool isUsed = Record[2];
Mike Stump1eb44332009-09-09 15:08:12 +00001445
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001446 MacroInfo *MI = PP->AllocateMacroInfo(Loc);
Douglas Gregor37e26842009-04-21 23:56:24 +00001447 MI->setIsUsed(isUsed);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001448 MI->setIsFromAST();
Mike Stump1eb44332009-09-09 15:08:12 +00001449
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001450 unsigned NextIndex = 3;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001451 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregor37e26842009-04-21 23:56:24 +00001452 // Decode function-like macro info.
1453 bool isC99VarArgs = Record[3];
1454 bool isGNUVarArgs = Record[4];
1455 MacroArgs.clear();
1456 unsigned NumArgs = Record[5];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001457 NextIndex = 6 + NumArgs;
Douglas Gregor37e26842009-04-21 23:56:24 +00001458 for (unsigned i = 0; i != NumArgs; ++i)
1459 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1460
1461 // Install function-like macro info.
1462 MI->setIsFunctionLike();
1463 if (isC99VarArgs) MI->setIsC99Varargs();
1464 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor75fdb232009-05-22 22:45:36 +00001465 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001466 PP->getPreprocessorAllocator());
Douglas Gregor37e26842009-04-21 23:56:24 +00001467 }
1468
1469 // Finally, install the macro.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001470 PP->setMacroInfo(II, MI);
Douglas Gregor37e26842009-04-21 23:56:24 +00001471
1472 // Remember that we saw this macro last so that we add the tokens that
1473 // form its body to it.
1474 Macro = MI;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001475
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001476 if (NextIndex + 1 == Record.size() && PP->getPreprocessingRecord()) {
1477 // We have a macro definition. Load it now.
1478 PP->getPreprocessingRecord()->RegisterMacroDefinition(Macro,
1479 getMacroDefinition(Record[NextIndex]));
1480 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001481
Douglas Gregor37e26842009-04-21 23:56:24 +00001482 ++NumMacrosRead;
1483 break;
1484 }
Mike Stump1eb44332009-09-09 15:08:12 +00001485
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001486 case PP_TOKEN: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001487 // If we see a TOKEN before a PP_MACRO_*, then the file is
1488 // erroneous, just pretend we didn't see this.
1489 if (Macro == 0) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001490
Douglas Gregor37e26842009-04-21 23:56:24 +00001491 Token Tok;
1492 Tok.startToken();
Sebastian Redlc3632732010-10-05 15:59:54 +00001493 Tok.setLocation(ReadSourceLocation(F, Record[0]));
Douglas Gregor37e26842009-04-21 23:56:24 +00001494 Tok.setLength(Record[1]);
1495 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1496 Tok.setIdentifierInfo(II);
1497 Tok.setKind((tok::TokenKind)Record[3]);
1498 Tok.setFlag((Token::TokenFlags)Record[4]);
1499 Macro->AddTokenToBody(Tok);
1500 break;
1501 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001502 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001503 }
Douglas Gregor89d99802010-11-30 06:16:57 +00001504
1505 return 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001506}
1507
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001508PreprocessedEntity *ASTReader::LoadPreprocessedEntity(PerFileData &F) {
1509 assert(PP && "Forgot to set Preprocessor ?");
1510 unsigned Code = F.PreprocessorDetailCursor.ReadCode();
1511 switch (Code) {
1512 case llvm::bitc::END_BLOCK:
1513 return 0;
1514
1515 case llvm::bitc::ENTER_SUBBLOCK:
1516 Error("unexpected subblock record in preprocessor detail block");
1517 return 0;
1518
1519 case llvm::bitc::DEFINE_ABBREV:
1520 Error("unexpected abbrevation record in preprocessor detail block");
1521 return 0;
1522
1523 default:
1524 break;
1525 }
1526
1527 if (!PP->getPreprocessingRecord()) {
1528 Error("no preprocessing record");
1529 return 0;
1530 }
1531
1532 // Read the record.
1533 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1534 const char *BlobStart = 0;
1535 unsigned BlobLen = 0;
1536 RecordData Record;
1537 PreprocessorDetailRecordTypes RecType =
1538 (PreprocessorDetailRecordTypes)F.PreprocessorDetailCursor.ReadRecord(
1539 Code, Record, BlobStart, BlobLen);
1540 switch (RecType) {
1541 case PPD_MACRO_INSTANTIATION: {
1542 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1543 return PE;
1544
1545 MacroInstantiation *MI
1546 = new (PPRec) MacroInstantiation(DecodeIdentifierInfo(Record[3]),
1547 SourceRange(ReadSourceLocation(F, Record[1]),
1548 ReadSourceLocation(F, Record[2])),
1549 getMacroDefinition(Record[4]));
1550 PPRec.SetPreallocatedEntity(Record[0], MI);
1551 return MI;
1552 }
1553
1554 case PPD_MACRO_DEFINITION: {
1555 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1556 return PE;
1557
1558 if (Record[1] > MacroDefinitionsLoaded.size()) {
1559 Error("out-of-bounds macro definition record");
1560 return 0;
1561 }
1562
1563 // Decode the identifier info and then check again; if the macro is
1564 // still defined and associated with the identifier,
1565 IdentifierInfo *II = DecodeIdentifierInfo(Record[4]);
1566 if (!MacroDefinitionsLoaded[Record[1] - 1]) {
1567 MacroDefinition *MD
1568 = new (PPRec) MacroDefinition(II,
1569 ReadSourceLocation(F, Record[5]),
1570 SourceRange(
1571 ReadSourceLocation(F, Record[2]),
1572 ReadSourceLocation(F, Record[3])));
1573
1574 PPRec.SetPreallocatedEntity(Record[0], MD);
1575 MacroDefinitionsLoaded[Record[1] - 1] = MD;
1576
1577 if (DeserializationListener)
1578 DeserializationListener->MacroDefinitionRead(Record[1], MD);
1579 }
1580
1581 return MacroDefinitionsLoaded[Record[1] - 1];
1582 }
1583
1584 case PPD_INCLUSION_DIRECTIVE: {
1585 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1586 return PE;
1587
1588 const char *FullFileNameStart = BlobStart + Record[3];
1589 const FileEntry *File
1590 = PP->getFileManager().getFile(llvm::StringRef(FullFileNameStart,
1591 BlobLen - Record[3]));
1592
1593 // FIXME: Stable encoding
1594 InclusionDirective::InclusionKind Kind
1595 = static_cast<InclusionDirective::InclusionKind>(Record[5]);
1596 InclusionDirective *ID
1597 = new (PPRec) InclusionDirective(PPRec, Kind,
1598 llvm::StringRef(BlobStart, Record[3]),
1599 Record[4],
1600 File,
1601 SourceRange(ReadSourceLocation(F, Record[1]),
1602 ReadSourceLocation(F, Record[2])));
1603 PPRec.SetPreallocatedEntity(Record[0], ID);
1604 return ID;
1605 }
1606 }
1607
1608 Error("invalid offset in preprocessor detail block");
1609 return 0;
1610}
1611
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001612namespace {
1613 /// \brief Trait class used to search the on-disk hash table containing all of
1614 /// the header search information.
1615 ///
1616 /// The on-disk hash table contains a mapping from each header path to
1617 /// information about that header (how many times it has been included, its
1618 /// controlling macro, etc.). Note that we actually hash based on the
1619 /// filename, and support "deep" comparisons of file names based on current
1620 /// inode numbers, so that the search can cope with non-normalized path names
1621 /// and symlinks.
1622 class HeaderFileInfoTrait {
1623 const char *SearchPath;
1624 struct stat SearchPathStatBuf;
1625 llvm::Optional<int> SearchPathStatResult;
1626
1627 int StatSimpleCache(const char *Path, struct stat *StatBuf) {
1628 if (Path == SearchPath) {
1629 if (!SearchPathStatResult)
1630 SearchPathStatResult = stat(Path, &SearchPathStatBuf);
1631
1632 *StatBuf = SearchPathStatBuf;
1633 return *SearchPathStatResult;
1634 }
1635
1636 return stat(Path, StatBuf);
1637 }
1638
1639 public:
1640 typedef const char *external_key_type;
1641 typedef const char *internal_key_type;
1642
1643 typedef HeaderFileInfo data_type;
1644
1645 HeaderFileInfoTrait(const char *SearchPath = 0) : SearchPath(SearchPath) { }
1646
1647 static unsigned ComputeHash(const char *path) {
1648 return llvm::HashString(llvm::sys::path::filename(path));
1649 }
1650
1651 static internal_key_type GetInternalKey(const char *path) { return path; }
1652
1653 bool EqualKey(internal_key_type a, internal_key_type b) {
1654 if (strcmp(a, b) == 0)
1655 return true;
1656
1657 if (llvm::sys::path::filename(a) != llvm::sys::path::filename(b))
1658 return false;
1659
1660 // The file names match, but the path names don't. stat() the files to
1661 // see if they are the same.
1662 struct stat StatBufA, StatBufB;
1663 if (StatSimpleCache(a, &StatBufA) || StatSimpleCache(b, &StatBufB))
1664 return false;
1665
1666 return StatBufA.st_ino == StatBufB.st_ino;
1667 }
1668
1669 static std::pair<unsigned, unsigned>
1670 ReadKeyDataLength(const unsigned char*& d) {
1671 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1672 unsigned DataLen = (unsigned) *d++;
1673 return std::make_pair(KeyLen + 1, DataLen);
1674 }
1675
1676 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
1677 return (const char *)d;
1678 }
1679
1680 static data_type ReadData(const internal_key_type, const unsigned char *d,
1681 unsigned DataLen) {
1682 const unsigned char *End = d + DataLen;
1683 using namespace clang::io;
1684 HeaderFileInfo HFI;
1685 unsigned Flags = *d++;
1686 HFI.isImport = (Flags >> 3) & 0x01;
1687 HFI.DirInfo = (Flags >> 1) & 0x03;
1688 HFI.Resolved = Flags & 0x01;
1689 HFI.NumIncludes = ReadUnalignedLE16(d);
1690 HFI.ControllingMacroID = ReadUnalignedLE32(d);
1691 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1692 (void)End;
1693
1694 // This HeaderFileInfo was externally loaded.
1695 HFI.External = true;
1696 return HFI;
1697 }
1698 };
1699}
1700
1701/// \brief The on-disk hash table used for the global method pool.
1702typedef OnDiskChainedHashTable<HeaderFileInfoTrait>
1703 HeaderFileInfoLookupTable;
1704
Douglas Gregor295a2a62010-10-30 00:23:06 +00001705void ASTReader::SetIdentifierIsMacro(IdentifierInfo *II, PerFileData &F,
1706 uint64_t Offset) {
1707 // Note that this identifier has a macro definition.
1708 II->setHasMacroDefinition(true);
1709
1710 // Adjust the offset based on our position in the chain.
1711 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1712 if (Chain[I] == &F)
1713 break;
1714
1715 Offset += Chain[I]->SizeInBits;
1716 }
1717
1718 UnreadMacroRecordOffsets[II] = Offset;
1719}
1720
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001721void ASTReader::ReadDefinedMacros() {
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001722 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redlc3632732010-10-05 15:59:54 +00001723 PerFileData &F = *Chain[N - I - 1];
1724 llvm::BitstreamCursor &MacroCursor = F.MacroCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001725
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001726 // If there was no preprocessor block, skip this file.
1727 if (!MacroCursor.getBitStreamReader())
1728 continue;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001729
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001730 llvm::BitstreamCursor Cursor = MacroCursor;
Douglas Gregorecdcb882010-10-20 22:00:55 +00001731 Cursor.JumpToBit(F.MacroStartOffset);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001732
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001733 RecordData Record;
1734 while (true) {
1735 unsigned Code = Cursor.ReadCode();
Douglas Gregorecdcb882010-10-20 22:00:55 +00001736 if (Code == llvm::bitc::END_BLOCK)
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001737 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001738
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001739 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1740 // No known subblocks, always skip them.
1741 Cursor.ReadSubBlockID();
1742 if (Cursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001743 Error("malformed block record in AST file");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001744 return;
1745 }
1746 continue;
1747 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001748
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001749 if (Code == llvm::bitc::DEFINE_ABBREV) {
1750 Cursor.ReadAbbrevRecord();
1751 continue;
1752 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001753
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001754 // Read a record.
1755 const char *BlobStart;
1756 unsigned BlobLen;
1757 Record.clear();
1758 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1759 default: // Default behavior: ignore.
1760 break;
Douglas Gregor88a35862010-01-04 19:18:44 +00001761
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001762 case PP_MACRO_OBJECT_LIKE:
1763 case PP_MACRO_FUNCTION_LIKE:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001764 DecodeIdentifierInfo(Record[0]);
1765 break;
1766
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001767 case PP_TOKEN:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001768 // Ignore tokens.
1769 break;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001770 }
Douglas Gregor88a35862010-01-04 19:18:44 +00001771 }
1772 }
Douglas Gregor295a2a62010-10-30 00:23:06 +00001773
1774 // Drain the unread macro-record offsets map.
1775 while (!UnreadMacroRecordOffsets.empty())
1776 LoadMacroDefinition(UnreadMacroRecordOffsets.begin());
1777}
1778
1779void ASTReader::LoadMacroDefinition(
1780 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos) {
1781 assert(Pos != UnreadMacroRecordOffsets.end() && "Unknown macro definition");
1782 PerFileData *F = 0;
1783 uint64_t Offset = Pos->second;
1784 UnreadMacroRecordOffsets.erase(Pos);
1785
1786 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1787 if (Offset < Chain[I]->SizeInBits) {
1788 F = Chain[I];
1789 break;
1790 }
1791
1792 Offset -= Chain[I]->SizeInBits;
1793 }
1794 if (!F) {
1795 Error("Malformed macro record offset");
1796 return;
1797 }
1798
1799 ReadMacroRecord(*F, Offset);
1800}
1801
1802void ASTReader::LoadMacroDefinition(IdentifierInfo *II) {
1803 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos
1804 = UnreadMacroRecordOffsets.find(II);
1805 LoadMacroDefinition(Pos);
Douglas Gregor88a35862010-01-04 19:18:44 +00001806}
1807
Sebastian Redlf73c93f2010-09-15 19:54:06 +00001808MacroDefinition *ASTReader::getMacroDefinition(MacroID ID) {
Douglas Gregor77424bc2010-10-02 19:29:26 +00001809 if (ID == 0 || ID > MacroDefinitionsLoaded.size())
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001810 return 0;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001811
Douglas Gregor77424bc2010-10-02 19:29:26 +00001812 if (!MacroDefinitionsLoaded[ID - 1]) {
1813 unsigned Index = ID - 1;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001814 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1815 PerFileData &F = *Chain[N - I - 1];
1816 if (Index < F.LocalNumMacroDefinitions) {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001817 SavedStreamPosition SavedPosition(F.PreprocessorDetailCursor);
1818 F.PreprocessorDetailCursor.JumpToBit(F.MacroDefinitionOffsets[Index]);
1819 LoadPreprocessedEntity(F);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001820 break;
1821 }
1822 Index -= F.LocalNumMacroDefinitions;
1823 }
Douglas Gregor77424bc2010-10-02 19:29:26 +00001824 assert(MacroDefinitionsLoaded[ID - 1] && "Broken chain");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001825 }
1826
Douglas Gregor77424bc2010-10-02 19:29:26 +00001827 return MacroDefinitionsLoaded[ID - 1];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001828}
1829
Douglas Gregore650c8c2009-07-07 00:12:59 +00001830/// \brief If we are loading a relocatable PCH file, and the filename is
1831/// not an absolute path, add the system root to the beginning of the file
1832/// name.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001833void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001834 // If this is not a relocatable PCH file, there's nothing to do.
1835 if (!RelocatablePCH)
1836 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001837
Michael J. Spencer256053b2010-12-17 21:22:22 +00001838 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
Douglas Gregore650c8c2009-07-07 00:12:59 +00001839 return;
1840
Douglas Gregore650c8c2009-07-07 00:12:59 +00001841 if (isysroot == 0) {
1842 // If no system root was given, default to '/'
1843 Filename.insert(Filename.begin(), '/');
1844 return;
1845 }
Mike Stump1eb44332009-09-09 15:08:12 +00001846
Douglas Gregore650c8c2009-07-07 00:12:59 +00001847 unsigned Length = strlen(isysroot);
1848 if (isysroot[Length - 1] != '/')
1849 Filename.insert(Filename.begin(), '/');
Mike Stump1eb44332009-09-09 15:08:12 +00001850
Douglas Gregore650c8c2009-07-07 00:12:59 +00001851 Filename.insert(Filename.begin(), isysroot, isysroot + Length);
1852}
1853
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001854ASTReader::ASTReadResult
Sebastian Redl571db7f2010-08-18 23:56:56 +00001855ASTReader::ReadASTBlock(PerFileData &F) {
Sebastian Redl9137a522010-07-16 17:50:48 +00001856 llvm::BitstreamCursor &Stream = F.Stream;
1857
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001858 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001859 Error("malformed block record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001860 return Failure;
1861 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001862
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001863 // Read all of the records and blocks for the ASt file.
Douglas Gregor8038d512009-04-10 17:25:41 +00001864 RecordData Record;
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001865 bool First = true;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001866 while (!Stream.AtEndOfStream()) {
1867 unsigned Code = Stream.ReadCode();
1868 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001869 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001870 Error("error at end of module block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001871 return Failure;
1872 }
Chris Lattner7356a312009-04-11 21:15:38 +00001873
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001874 return Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001875 }
1876
1877 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1878 switch (Stream.ReadSubBlockID()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001879 case DECLTYPES_BLOCK_ID:
Chris Lattner6367f6d2009-04-27 01:05:14 +00001880 // We lazily load the decls block, but we want to set up the
1881 // DeclsCursor cursor to point into it. Clone our current bitcode
1882 // cursor to it, enter the block and read the abbrevs in that block.
1883 // With the main cursor, we just skip over it.
Sebastian Redl9137a522010-07-16 17:50:48 +00001884 F.DeclsCursor = Stream;
Chris Lattner6367f6d2009-04-27 01:05:14 +00001885 if (Stream.SkipBlock() || // Skip with the main cursor.
1886 // Read the abbrevs.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001887 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001888 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001889 return Failure;
1890 }
1891 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001892
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00001893 case DECL_UPDATES_BLOCK_ID:
1894 if (Stream.SkipBlock()) {
1895 Error("malformed block record in AST file");
1896 return Failure;
1897 }
1898 break;
1899
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001900 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl9137a522010-07-16 17:50:48 +00001901 F.MacroCursor = Stream;
Douglas Gregor88a35862010-01-04 19:18:44 +00001902 if (PP)
1903 PP->setExternalSource(this);
1904
Douglas Gregorecdcb882010-10-20 22:00:55 +00001905 if (Stream.SkipBlock() ||
1906 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001907 Error("malformed block record in AST file");
Chris Lattner7356a312009-04-11 21:15:38 +00001908 return Failure;
1909 }
Douglas Gregorecdcb882010-10-20 22:00:55 +00001910 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
Chris Lattner7356a312009-04-11 21:15:38 +00001911 break;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00001912
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001913 case PREPROCESSOR_DETAIL_BLOCK_ID:
1914 F.PreprocessorDetailCursor = Stream;
1915 if (Stream.SkipBlock() ||
1916 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
1917 PREPROCESSOR_DETAIL_BLOCK_ID)) {
1918 Error("malformed preprocessor detail record in AST file");
1919 return Failure;
1920 }
1921 F.PreprocessorDetailStartOffset
1922 = F.PreprocessorDetailCursor.GetCurrentBitNo();
1923 break;
1924
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001925 case SOURCE_MANAGER_BLOCK_ID:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001926 switch (ReadSourceManagerBlock(F)) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00001927 case Success:
1928 break;
1929
1930 case Failure:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001931 Error("malformed source manager block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001932 return Failure;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001933
1934 case IgnorePCH:
1935 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001936 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001937 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001938 }
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001939 First = false;
Douglas Gregor8038d512009-04-10 17:25:41 +00001940 continue;
1941 }
1942
1943 if (Code == llvm::bitc::DEFINE_ABBREV) {
1944 Stream.ReadAbbrevRecord();
1945 continue;
1946 }
1947
1948 // Read and process a record.
1949 Record.clear();
Douglas Gregor2bec0412009-04-10 21:16:55 +00001950 const char *BlobStart = 0;
1951 unsigned BlobLen = 0;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001952 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00001953 &BlobStart, &BlobLen)) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001954 default: // Default behavior: ignore.
1955 break;
1956
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001957 case METADATA: {
1958 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1959 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001960 : diag::warn_pch_version_too_new);
1961 return IgnorePCH;
1962 }
1963
1964 RelocatablePCH = Record[4];
1965 if (Listener) {
1966 std::string TargetTriple(BlobStart, BlobLen);
1967 if (Listener->ReadTargetTriple(TargetTriple))
1968 return IgnorePCH;
1969 }
1970 break;
1971 }
1972
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001973 case CHAINED_METADATA: {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001974 if (!First) {
1975 Error("CHAINED_METADATA is not first record in block");
1976 return Failure;
1977 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001978 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1979 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001980 : diag::warn_pch_version_too_new);
1981 return IgnorePCH;
1982 }
1983
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00001984 // Load the chained file, which is always a PCH file.
1985 switch(ReadASTCore(llvm::StringRef(BlobStart, BlobLen), PCH)) {
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001986 case Failure: return Failure;
1987 // If we have to ignore the dependency, we'll have to ignore this too.
1988 case IgnorePCH: return IgnorePCH;
1989 case Success: break;
1990 }
1991 break;
1992 }
1993
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001994 case TYPE_OFFSET:
Sebastian Redl12d6da02010-07-19 22:06:55 +00001995 if (F.LocalNumTypes != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001996 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001997 return Failure;
1998 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00001999 F.TypeOffsets = (const uint32_t *)BlobStart;
2000 F.LocalNumTypes = Record[0];
Douglas Gregor8038d512009-04-10 17:25:41 +00002001 break;
2002
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002003 case DECL_OFFSET:
Sebastian Redl12d6da02010-07-19 22:06:55 +00002004 if (F.LocalNumDecls != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002005 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002006 return Failure;
2007 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00002008 F.DeclOffsets = (const uint32_t *)BlobStart;
2009 F.LocalNumDecls = Record[0];
Douglas Gregor8038d512009-04-10 17:25:41 +00002010 break;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002011
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002012 case TU_UPDATE_LEXICAL: {
Sebastian Redld692af72010-07-27 18:24:41 +00002013 DeclContextInfo Info = {
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002014 /* No visible information */ 0,
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002015 reinterpret_cast<const KindDeclIDPair *>(BlobStart),
2016 BlobLen / sizeof(KindDeclIDPair)
Sebastian Redld692af72010-07-27 18:24:41 +00002017 };
Douglas Gregor3747ee72010-10-01 01:18:02 +00002018 DeclContextOffsets[Context ? Context->getTranslationUnitDecl() : 0]
2019 .push_back(Info);
Sebastian Redld692af72010-07-27 18:24:41 +00002020 break;
2021 }
2022
Sebastian Redle1dde812010-08-24 00:50:04 +00002023 case UPDATE_VISIBLE: {
2024 serialization::DeclID ID = Record[0];
2025 void *Table = ASTDeclContextNameLookupTable::Create(
2026 (const unsigned char *)BlobStart + Record[1],
2027 (const unsigned char *)BlobStart,
2028 ASTDeclContextNameLookupTrait(*this));
Douglas Gregor3747ee72010-10-01 01:18:02 +00002029 if (ID == 1 && Context) { // Is it the TU?
Sebastian Redle1dde812010-08-24 00:50:04 +00002030 DeclContextInfo Info = {
2031 Table, /* No lexical inforamtion */ 0, 0
2032 };
2033 DeclContextOffsets[Context->getTranslationUnitDecl()].push_back(Info);
2034 } else
2035 PendingVisibleUpdates[ID].push_back(Table);
2036 break;
2037 }
2038
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002039 case REDECLS_UPDATE_LATEST: {
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00002040 assert(Record.size() % 2 == 0 && "Expected pairs of DeclIDs");
2041 for (unsigned i = 0, e = Record.size(); i < e; i += 2) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002042 DeclID First = Record[i], Latest = Record[i+1];
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00002043 assert((FirstLatestDeclIDs.find(First) == FirstLatestDeclIDs.end() ||
2044 Latest > FirstLatestDeclIDs[First]) &&
2045 "The new latest is supposed to come after the previous latest");
2046 FirstLatestDeclIDs[First] = Latest;
2047 }
2048 break;
2049 }
2050
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002051 case LANGUAGE_OPTIONS:
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00002052 if (ParseLanguageOptions(Record) && !DisableValidation)
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002053 return IgnorePCH;
2054 break;
Douglas Gregor2bec0412009-04-10 21:16:55 +00002055
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002056 case IDENTIFIER_TABLE:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002057 F.IdentifierTableData = BlobStart;
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002058 if (Record[0]) {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002059 F.IdentifierLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002060 = ASTIdentifierLookupTable::Create(
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002061 (const unsigned char *)F.IdentifierTableData + Record[0],
2062 (const unsigned char *)F.IdentifierTableData,
Sebastian Redlc3632732010-10-05 15:59:54 +00002063 ASTIdentifierLookupTrait(*this, F));
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002064 if (PP)
2065 PP->getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002066 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002067 break;
2068
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002069 case IDENTIFIER_OFFSET:
Sebastian Redl2da08f92010-07-19 22:28:42 +00002070 if (F.LocalNumIdentifiers != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002071 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00002072 return Failure;
2073 }
Sebastian Redl2da08f92010-07-19 22:28:42 +00002074 F.IdentifierOffsets = (const uint32_t *)BlobStart;
2075 F.LocalNumIdentifiers = Record[0];
Douglas Gregorafaf3082009-04-11 00:14:32 +00002076 break;
Douglas Gregorfdd01722009-04-14 00:24:19 +00002077
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002078 case EXTERNAL_DEFINITIONS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002079 // Optimization for the first block.
2080 if (ExternalDefinitions.empty())
2081 ExternalDefinitions.swap(Record);
2082 else
2083 ExternalDefinitions.insert(ExternalDefinitions.end(),
2084 Record.begin(), Record.end());
Douglas Gregorfdd01722009-04-14 00:24:19 +00002085 break;
Douglas Gregor3e1af842009-04-17 22:13:46 +00002086
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002087 case SPECIAL_TYPES:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002088 // Optimization for the first block
2089 if (SpecialTypes.empty())
2090 SpecialTypes.swap(Record);
2091 else
2092 SpecialTypes.insert(SpecialTypes.end(), Record.begin(), Record.end());
Douglas Gregorad1de002009-04-18 05:55:16 +00002093 break;
2094
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002095 case STATISTICS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002096 TotalNumStatements += Record[0];
2097 TotalNumMacros += Record[1];
2098 TotalLexicalDeclContexts += Record[2];
2099 TotalVisibleDeclContexts += Record[3];
Douglas Gregor3e1af842009-04-17 22:13:46 +00002100 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002101
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002102 case TENTATIVE_DEFINITIONS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002103 // Optimization for the first block.
2104 if (TentativeDefinitions.empty())
2105 TentativeDefinitions.swap(Record);
2106 else
2107 TentativeDefinitions.insert(TentativeDefinitions.end(),
2108 Record.begin(), Record.end());
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002109 break;
Douglas Gregor14c22f22009-04-22 22:18:58 +00002110
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002111 case UNUSED_FILESCOPED_DECLS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002112 // Optimization for the first block.
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002113 if (UnusedFileScopedDecls.empty())
2114 UnusedFileScopedDecls.swap(Record);
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002115 else
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002116 UnusedFileScopedDecls.insert(UnusedFileScopedDecls.end(),
2117 Record.begin(), Record.end());
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002118 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002119
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002120 case WEAK_UNDECLARED_IDENTIFIERS:
Sebastian Redl40566802010-08-05 18:21:25 +00002121 // Later blocks overwrite earlier ones.
2122 WeakUndeclaredIdentifiers.swap(Record);
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002123 break;
2124
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002125 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002126 // Optimization for the first block.
2127 if (LocallyScopedExternalDecls.empty())
2128 LocallyScopedExternalDecls.swap(Record);
2129 else
2130 LocallyScopedExternalDecls.insert(LocallyScopedExternalDecls.end(),
2131 Record.begin(), Record.end());
Douglas Gregor14c22f22009-04-22 22:18:58 +00002132 break;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002133
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002134 case SELECTOR_OFFSETS:
Sebastian Redl059612d2010-08-03 21:58:15 +00002135 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redl725cd962010-08-04 20:40:17 +00002136 F.LocalNumSelectors = Record[0];
Douglas Gregor83941df2009-04-25 17:48:32 +00002137 break;
2138
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002139 case METHOD_POOL:
Sebastian Redl725cd962010-08-04 20:40:17 +00002140 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor83941df2009-04-25 17:48:32 +00002141 if (Record[0])
Sebastian Redl725cd962010-08-04 20:40:17 +00002142 F.SelectorLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002143 = ASTSelectorLookupTable::Create(
Sebastian Redl725cd962010-08-04 20:40:17 +00002144 F.SelectorLookupTableData + Record[0],
2145 F.SelectorLookupTableData,
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002146 ASTSelectorLookupTrait(*this));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002147 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002148 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002149
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002150 case REFERENCED_SELECTOR_POOL:
Sebastian Redlc3632732010-10-05 15:59:54 +00002151 F.ReferencedSelectorsData.swap(Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002152 break;
2153
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002154 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002155 if (!Record.empty() && Listener)
2156 Listener->ReadCounter(Record[0]);
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002157 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002158
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002159 case SOURCE_LOCATION_OFFSETS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002160 F.SLocOffsets = (const uint32_t *)BlobStart;
2161 F.LocalNumSLocEntries = Record[0];
Sebastian Redl8db9fae2010-09-22 20:19:08 +00002162 F.LocalSLocSize = Record[1];
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002163 break;
2164
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002165 case SOURCE_LOCATION_PRELOADS:
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002166 if (PreloadSLocEntries.empty())
2167 PreloadSLocEntries.swap(Record);
2168 else
2169 PreloadSLocEntries.insert(PreloadSLocEntries.end(),
2170 Record.begin(), Record.end());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002171 break;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002172
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002173 case STAT_CACHE: {
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00002174 if (!DisableStatCache) {
2175 ASTStatCache *MyStatCache =
2176 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
2177 (const unsigned char *)BlobStart,
2178 NumStatHits, NumStatMisses);
2179 FileMgr.addStatCache(MyStatCache);
2180 F.StatCache = MyStatCache;
2181 }
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002182 break;
Douglas Gregor52e71082009-10-16 18:18:30 +00002183 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002184
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002185 case EXT_VECTOR_DECLS:
Sebastian Redla9f23682010-07-28 21:38:49 +00002186 // Optimization for the first block.
2187 if (ExtVectorDecls.empty())
2188 ExtVectorDecls.swap(Record);
2189 else
2190 ExtVectorDecls.insert(ExtVectorDecls.end(),
2191 Record.begin(), Record.end());
Douglas Gregorb81c1702009-04-27 20:06:05 +00002192 break;
2193
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002194 case VTABLE_USES:
Sebastian Redl40566802010-08-05 18:21:25 +00002195 // Later tables overwrite earlier ones.
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002196 VTableUses.swap(Record);
2197 break;
2198
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002199 case DYNAMIC_CLASSES:
Sebastian Redl40566802010-08-05 18:21:25 +00002200 // Optimization for the first block.
2201 if (DynamicClasses.empty())
2202 DynamicClasses.swap(Record);
2203 else
2204 DynamicClasses.insert(DynamicClasses.end(),
2205 Record.begin(), Record.end());
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002206 break;
2207
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002208 case PENDING_IMPLICIT_INSTANTIATIONS:
Sebastian Redlc3632732010-10-05 15:59:54 +00002209 F.PendingInstantiations.swap(Record);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002210 break;
2211
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002212 case SEMA_DECL_REFS:
Sebastian Redl40566802010-08-05 18:21:25 +00002213 // Later tables overwrite earlier ones.
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002214 SemaDeclRefs.swap(Record);
2215 break;
2216
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002217 case ORIGINAL_FILE_NAME:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002218 // The primary AST will be the last to get here, so it will be the one
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002219 // that's used.
Daniel Dunbar7b5a1212009-11-11 05:29:04 +00002220 ActualOriginalFileName.assign(BlobStart, BlobLen);
2221 OriginalFileName = ActualOriginalFileName;
Douglas Gregore650c8c2009-07-07 00:12:59 +00002222 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregorb64c1932009-05-12 01:31:05 +00002223 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002224
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002225 case ORIGINAL_PCH_DIR:
2226 // The primary AST will be the last to get here, so it will be the one
2227 // that's used.
2228 OriginalDir.assign(BlobStart, BlobLen);
2229 break;
2230
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002231 case VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek974be4d2010-02-12 23:31:14 +00002232 const std::string &CurBranch = getClangFullRepositoryVersion();
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002233 llvm::StringRef ASTBranch(BlobStart, BlobLen);
2234 if (llvm::StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2235 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregor445e23e2009-10-05 21:07:28 +00002236 return IgnorePCH;
2237 }
2238 break;
2239 }
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002240
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002241 case MACRO_DEFINITION_OFFSETS:
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002242 F.MacroDefinitionOffsets = (const uint32_t *)BlobStart;
2243 F.NumPreallocatedPreprocessingEntities = Record[0];
2244 F.LocalNumMacroDefinitions = Record[1];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002245 break;
Sebastian Redl0b17c612010-08-13 00:28:03 +00002246
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002247 case DECL_UPDATE_OFFSETS: {
2248 if (Record.size() % 2 != 0) {
2249 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2250 return Failure;
2251 }
2252 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
2253 DeclUpdateOffsets[static_cast<DeclID>(Record[I])]
2254 .push_back(std::make_pair(&F, Record[I+1]));
2255 break;
2256 }
2257
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002258 case DECL_REPLACEMENTS: {
Sebastian Redl0b17c612010-08-13 00:28:03 +00002259 if (Record.size() % 2 != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002260 Error("invalid DECL_REPLACEMENTS block in AST file");
Sebastian Redl0b17c612010-08-13 00:28:03 +00002261 return Failure;
2262 }
2263 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002264 ReplacedDecls[static_cast<DeclID>(Record[I])] =
Sebastian Redl0b17c612010-08-13 00:28:03 +00002265 std::make_pair(&F, Record[I+1]);
2266 break;
2267 }
Douglas Gregor7c789c12010-10-29 22:39:52 +00002268
2269 case CXX_BASE_SPECIFIER_OFFSETS: {
2270 if (F.LocalNumCXXBaseSpecifiers != 0) {
2271 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2272 return Failure;
2273 }
2274
2275 F.LocalNumCXXBaseSpecifiers = Record[0];
2276 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart;
2277 break;
2278 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002279
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002280 case DIAG_PRAGMA_MAPPINGS:
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002281 if (Record.size() % 2 != 0) {
2282 Error("invalid DIAG_USER_MAPPINGS block in AST file");
2283 return Failure;
2284 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002285 if (PragmaDiagMappings.empty())
2286 PragmaDiagMappings.swap(Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002287 else
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002288 PragmaDiagMappings.insert(PragmaDiagMappings.end(),
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002289 Record.begin(), Record.end());
2290 break;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002291
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002292 case CUDA_SPECIAL_DECL_REFS:
2293 // Later tables overwrite earlier ones.
2294 CUDASpecialDeclRefs.swap(Record);
2295 break;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002296
2297 case HEADER_SEARCH_TABLE:
2298 F.HeaderFileInfoTableData = BlobStart;
2299 F.LocalNumHeaderFileInfos = Record[1];
2300 if (Record[0]) {
2301 F.HeaderFileInfoTable
2302 = HeaderFileInfoLookupTable::Create(
2303 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
2304 (const unsigned char *)F.HeaderFileInfoTableData);
2305 if (PP)
2306 PP->getHeaderSearchInfo().SetExternalSource(this);
2307 }
2308 break;
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002309
2310 case FP_PRAGMA_OPTIONS:
2311 // Later tables overwrite earlier ones.
2312 FPPragmaOptions.swap(Record);
2313 break;
2314
2315 case OPENCL_EXTENSIONS:
2316 // Later tables overwrite earlier ones.
2317 OpenCLExtensions.swap(Record);
2318 break;
Douglas Gregorafaf3082009-04-11 00:14:32 +00002319 }
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002320 First = false;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002321 }
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002322 Error("premature end of bitstream in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002323 return Failure;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002324}
2325
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002326ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
2327 ASTFileType Type) {
2328 switch(ReadASTCore(FileName, Type)) {
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002329 case Failure: return Failure;
2330 case IgnorePCH: return IgnorePCH;
2331 case Success: break;
2332 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002333
2334 // Here comes stuff that we only do once the entire chain is loaded.
2335
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002336 // Allocate space for loaded slocentries, identifiers, decls and types.
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002337 unsigned TotalNumIdentifiers = 0, TotalNumTypes = 0, TotalNumDecls = 0,
Sebastian Redl725cd962010-08-04 20:40:17 +00002338 TotalNumPreallocatedPreprocessingEntities = 0, TotalNumMacroDefs = 0,
2339 TotalNumSelectors = 0;
Sebastian Redl12d6da02010-07-19 22:06:55 +00002340 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002341 TotalNumSLocEntries += Chain[I]->LocalNumSLocEntries;
Sebastian Redl8db9fae2010-09-22 20:19:08 +00002342 NextSLocOffset += Chain[I]->LocalSLocSize;
Sebastian Redl2da08f92010-07-19 22:28:42 +00002343 TotalNumIdentifiers += Chain[I]->LocalNumIdentifiers;
Sebastian Redl12d6da02010-07-19 22:06:55 +00002344 TotalNumTypes += Chain[I]->LocalNumTypes;
2345 TotalNumDecls += Chain[I]->LocalNumDecls;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002346 TotalNumPreallocatedPreprocessingEntities +=
2347 Chain[I]->NumPreallocatedPreprocessingEntities;
2348 TotalNumMacroDefs += Chain[I]->LocalNumMacroDefinitions;
Sebastian Redl725cd962010-08-04 20:40:17 +00002349 TotalNumSelectors += Chain[I]->LocalNumSelectors;
Sebastian Redl12d6da02010-07-19 22:06:55 +00002350 }
Sebastian Redl8db9fae2010-09-22 20:19:08 +00002351 SourceMgr.PreallocateSLocEntries(this, TotalNumSLocEntries, NextSLocOffset);
Sebastian Redl2da08f92010-07-19 22:28:42 +00002352 IdentifiersLoaded.resize(TotalNumIdentifiers);
Sebastian Redl12d6da02010-07-19 22:06:55 +00002353 TypesLoaded.resize(TotalNumTypes);
2354 DeclsLoaded.resize(TotalNumDecls);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002355 MacroDefinitionsLoaded.resize(TotalNumMacroDefs);
2356 if (PP) {
2357 if (TotalNumIdentifiers > 0)
2358 PP->getHeaderSearchInfo().SetExternalLookup(this);
2359 if (TotalNumPreallocatedPreprocessingEntities > 0) {
2360 if (!PP->getPreprocessingRecord())
2361 PP->createPreprocessingRecord();
2362 PP->getPreprocessingRecord()->SetExternalSource(*this,
2363 TotalNumPreallocatedPreprocessingEntities);
2364 }
2365 }
Sebastian Redl725cd962010-08-04 20:40:17 +00002366 SelectorsLoaded.resize(TotalNumSelectors);
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002367 // Preload SLocEntries.
2368 for (unsigned I = 0, N = PreloadSLocEntries.size(); I != N; ++I) {
2369 ASTReadResult Result = ReadSLocEntryRecord(PreloadSLocEntries[I]);
2370 if (Result != Success)
2371 return Result;
2372 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00002373
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002374 // Check the predefines buffers.
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00002375 if (!DisableValidation && CheckPredefinesBuffers())
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002376 return IgnorePCH;
2377
2378 if (PP) {
2379 // Initialization of keywords and pragmas occurs before the
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002380 // AST file is read, so there may be some identifiers that were
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002381 // loaded into the IdentifierTable before we intercepted the
2382 // creation of identifiers. Iterate through the list of known
2383 // identifiers and determine whether we have to establish
2384 // preprocessor definitions or top-level identifier declaration
2385 // chains for those identifiers.
2386 //
2387 // We copy the IdentifierInfo pointers to a small vector first,
2388 // since de-serializing declarations or macro definitions can add
2389 // new entries into the identifier table, invalidating the
2390 // iterators.
2391 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
2392 for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(),
2393 IdEnd = PP->getIdentifierTable().end();
2394 Id != IdEnd; ++Id)
2395 Identifiers.push_back(Id->second);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002396 // We need to search the tables in all files.
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002397 for (unsigned J = 0, M = Chain.size(); J != M; ++J) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002398 ASTIdentifierLookupTable *IdTable
2399 = (ASTIdentifierLookupTable *)Chain[J]->IdentifierLookupTable;
2400 // Not all AST files necessarily have identifier tables, only the useful
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00002401 // ones.
2402 if (!IdTable)
2403 continue;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002404 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
2405 IdentifierInfo *II = Identifiers[I];
2406 // Look in the on-disk hash tables for an entry for this identifier
Sebastian Redlc3632732010-10-05 15:59:54 +00002407 ASTIdentifierLookupTrait Info(*this, *Chain[J], II);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002408 std::pair<const char*,unsigned> Key(II->getNameStart(),II->getLength());
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002409 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002410 if (Pos == IdTable->end())
2411 continue;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002412
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002413 // Dereferencing the iterator has the effect of populating the
2414 // IdentifierInfo node with the various declarations it needs.
2415 (void)*Pos;
2416 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002417 }
2418 }
2419
2420 if (Context)
2421 InitializeContext(*Context);
2422
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002423 if (DeserializationListener)
2424 DeserializationListener->ReaderInitialized(this);
2425
Douglas Gregor414cb642010-11-30 05:23:00 +00002426 // If this AST file is a precompiled preamble, then set the main file ID of
2427 // the source manager to the file source file from which the preamble was
2428 // built. This is the only valid way to use a precompiled preamble.
2429 if (Type == Preamble) {
2430 SourceLocation Loc
2431 = SourceMgr.getLocation(FileMgr.getFile(getOriginalSourceFile()), 1, 1);
2432 if (Loc.isValid()) {
2433 std::pair<FileID, unsigned> Decomposed = SourceMgr.getDecomposedLoc(Loc);
2434 SourceMgr.SetPreambleFileID(Decomposed.first);
2435 }
2436 }
2437
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002438 return Success;
2439}
2440
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002441ASTReader::ASTReadResult ASTReader::ReadASTCore(llvm::StringRef FileName,
2442 ASTFileType Type) {
Sebastian Redla866e652010-10-01 19:59:12 +00002443 PerFileData *Prev = Chain.empty() ? 0 : Chain.back();
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002444 Chain.push_back(new PerFileData(Type));
Sebastian Redl9137a522010-07-16 17:50:48 +00002445 PerFileData &F = *Chain.back();
Sebastian Redla866e652010-10-01 19:59:12 +00002446 if (Prev)
2447 Prev->NextInSource = &F;
2448 else
2449 FirstInSource = &F;
2450 F.Loaders.push_back(Prev);
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002451
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002452 // Set the AST file name.
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002453 F.FileName = FileName;
2454
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002455 if (FileName != "-") {
2456 CurrentDir = llvm::sys::path::parent_path(FileName);
2457 if (CurrentDir.empty()) CurrentDir = ".";
2458 }
2459
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002460 // Open the AST file.
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002461 //
2462 // FIXME: This shouldn't be here, we should just take a raw_ostream.
2463 std::string ErrStr;
Michael J. Spencer3a321e22010-12-09 17:36:38 +00002464 llvm::error_code ec;
2465 if (FileName == "-") {
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00002466 ec = llvm::MemoryBuffer::getSTDIN(F.Buffer);
Michael J. Spencer3a321e22010-12-09 17:36:38 +00002467 if (ec)
2468 ErrStr = ec.message();
2469 } else
Chris Lattner39b49bc2010-11-23 08:35:12 +00002470 F.Buffer.reset(FileMgr.getBufferForFile(FileName, &ErrStr));
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002471 if (!F.Buffer) {
2472 Error(ErrStr.c_str());
2473 return IgnorePCH;
2474 }
2475
2476 // Initialize the stream
2477 F.StreamFile.init((const unsigned char *)F.Buffer->getBufferStart(),
2478 (const unsigned char *)F.Buffer->getBufferEnd());
Sebastian Redl9137a522010-07-16 17:50:48 +00002479 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002480 Stream.init(F.StreamFile);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002481 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002482
2483 // Sniff for the signature.
2484 if (Stream.Read(8) != 'C' ||
2485 Stream.Read(8) != 'P' ||
2486 Stream.Read(8) != 'C' ||
2487 Stream.Read(8) != 'H') {
2488 Diag(diag::err_not_a_pch_file) << FileName;
2489 return Failure;
2490 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002491
Douglas Gregor2cf26342009-04-09 22:27:44 +00002492 while (!Stream.AtEndOfStream()) {
2493 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00002494
Douglas Gregore1d918e2009-04-10 23:10:45 +00002495 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002496 Error("invalid record at top-level of AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002497 return Failure;
2498 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002499
2500 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregor668c1a42009-04-21 22:25:48 +00002501
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002502 // We only know the AST subblock ID.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002503 switch (BlockID) {
2504 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002505 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002506 Error("malformed BlockInfoBlock in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002507 return Failure;
2508 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002509 break;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002510 case AST_BLOCK_ID:
Sebastian Redl571db7f2010-08-18 23:56:56 +00002511 switch (ReadASTBlock(F)) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002512 case Success:
2513 break;
2514
2515 case Failure:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002516 return Failure;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002517
2518 case IgnorePCH:
Douglas Gregor2bec0412009-04-10 21:16:55 +00002519 // FIXME: We could consider reading through to the end of this
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002520 // AST block, skipping subblocks, to see if there are other
2521 // AST blocks elsewhere.
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002522
2523 // Clear out any preallocated source location entries, so that
2524 // the source manager does not try to resolve them later.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002525 SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002526
2527 // Remove the stat cache.
Sebastian Redl9137a522010-07-16 17:50:48 +00002528 if (F.StatCache)
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002529 FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002530
Douglas Gregore1d918e2009-04-10 23:10:45 +00002531 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002532 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002533 break;
2534 default:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002535 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002536 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002537 return Failure;
2538 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002539 break;
2540 }
Mike Stump1eb44332009-09-09 15:08:12 +00002541 }
2542
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002543 return Success;
2544}
2545
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002546void ASTReader::setPreprocessor(Preprocessor &pp) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002547 PP = &pp;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002548
2549 unsigned TotalNum = 0;
2550 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
2551 TotalNum += Chain[I]->NumPreallocatedPreprocessingEntities;
2552 if (TotalNum) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002553 if (!PP->getPreprocessingRecord())
2554 PP->createPreprocessingRecord();
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002555 PP->getPreprocessingRecord()->SetExternalSource(*this, TotalNum);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002556 }
2557}
2558
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002559void ASTReader::InitializeContext(ASTContext &Ctx) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002560 Context = &Ctx;
2561 assert(Context && "Passed null context!");
2562
2563 assert(PP && "Forgot to set Preprocessor ?");
2564 PP->getIdentifierTable().setExternalIdentifierLookup(this);
2565 PP->getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregor88a35862010-01-04 19:18:44 +00002566 PP->setExternalSource(this);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002567 PP->getHeaderSearchInfo().SetExternalSource(this);
2568
Douglas Gregor3747ee72010-10-01 01:18:02 +00002569 // If we have an update block for the TU waiting, we have to add it before
2570 // deserializing the decl.
2571 DeclContextOffsetsMap::iterator DCU = DeclContextOffsets.find(0);
2572 if (DCU != DeclContextOffsets.end()) {
2573 // Insertion could invalidate map, so grab vector.
2574 DeclContextInfos T;
2575 T.swap(DCU->second);
2576 DeclContextOffsets.erase(DCU);
2577 DeclContextOffsets[Ctx.getTranslationUnitDecl()].swap(T);
2578 }
2579
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002580 // Load the translation unit declaration
Argyrios Kyrtzidis8871a442010-07-08 17:13:02 +00002581 GetTranslationUnitDecl();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002582
2583 // Load the special types.
2584 Context->setBuiltinVaListType(
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002585 GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST]));
2586 if (unsigned Id = SpecialTypes[SPECIAL_TYPE_OBJC_ID])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002587 Context->setObjCIdType(GetType(Id));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002588 if (unsigned Sel = SpecialTypes[SPECIAL_TYPE_OBJC_SELECTOR])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002589 Context->setObjCSelType(GetType(Sel));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002590 if (unsigned Proto = SpecialTypes[SPECIAL_TYPE_OBJC_PROTOCOL])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002591 Context->setObjCProtoType(GetType(Proto));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002592 if (unsigned Class = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002593 Context->setObjCClassType(GetType(Class));
Steve Naroff14108da2009-07-10 23:34:53 +00002594
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002595 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002596 Context->setCFConstantStringType(GetType(String));
Mike Stump1eb44332009-09-09 15:08:12 +00002597 if (unsigned FastEnum
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002598 = SpecialTypes[SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002599 Context->setObjCFastEnumerationStateType(GetType(FastEnum));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002600 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002601 QualType FileType = GetType(File);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002602 if (FileType.isNull()) {
2603 Error("FILE type is NULL");
2604 return;
2605 }
John McCall183700f2009-09-21 23:43:11 +00002606 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002607 Context->setFILEDecl(Typedef->getDecl());
2608 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002609 const TagType *Tag = FileType->getAs<TagType>();
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002610 if (!Tag) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002611 Error("Invalid FILE type in AST file");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002612 return;
2613 }
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002614 Context->setFILEDecl(Tag->getDecl());
2615 }
2616 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002617 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_jmp_buf]) {
Mike Stump782fa302009-07-28 02:25:19 +00002618 QualType Jmp_bufType = GetType(Jmp_buf);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002619 if (Jmp_bufType.isNull()) {
2620 Error("jmp_bug type is NULL");
2621 return;
2622 }
John McCall183700f2009-09-21 23:43:11 +00002623 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
Mike Stump782fa302009-07-28 02:25:19 +00002624 Context->setjmp_bufDecl(Typedef->getDecl());
2625 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002626 const TagType *Tag = Jmp_bufType->getAs<TagType>();
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002627 if (!Tag) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002628 Error("Invalid jmp_buf type in AST file");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002629 return;
2630 }
Mike Stump782fa302009-07-28 02:25:19 +00002631 Context->setjmp_bufDecl(Tag->getDecl());
2632 }
2633 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002634 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_sigjmp_buf]) {
Mike Stump782fa302009-07-28 02:25:19 +00002635 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002636 if (Sigjmp_bufType.isNull()) {
2637 Error("sigjmp_buf type is NULL");
2638 return;
2639 }
John McCall183700f2009-09-21 23:43:11 +00002640 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
Mike Stump782fa302009-07-28 02:25:19 +00002641 Context->setsigjmp_bufDecl(Typedef->getDecl());
2642 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002643 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002644 assert(Tag && "Invalid sigjmp_buf type in AST file");
Mike Stump782fa302009-07-28 02:25:19 +00002645 Context->setsigjmp_bufDecl(Tag->getDecl());
2646 }
2647 }
Mike Stump1eb44332009-09-09 15:08:12 +00002648 if (unsigned ObjCIdRedef
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002649 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION])
Douglas Gregord1571ac2009-08-21 00:27:50 +00002650 Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef);
Mike Stump1eb44332009-09-09 15:08:12 +00002651 if (unsigned ObjCClassRedef
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002652 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION])
Douglas Gregord1571ac2009-08-21 00:27:50 +00002653 Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002654 if (unsigned String = SpecialTypes[SPECIAL_TYPE_BLOCK_DESCRIPTOR])
Mike Stumpadaaad32009-10-20 02:12:22 +00002655 Context->setBlockDescriptorType(GetType(String));
Mike Stump083c25e2009-10-22 00:49:09 +00002656 if (unsigned String
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002657 = SpecialTypes[SPECIAL_TYPE_BLOCK_EXTENDED_DESCRIPTOR])
Mike Stump083c25e2009-10-22 00:49:09 +00002658 Context->setBlockDescriptorExtendedType(GetType(String));
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002659 if (unsigned ObjCSelRedef
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002660 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION])
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002661 Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002662 if (unsigned String = SpecialTypes[SPECIAL_TYPE_NS_CONSTANT_STRING])
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002663 Context->setNSConstantStringType(GetType(String));
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +00002664
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002665 if (SpecialTypes[SPECIAL_TYPE_INT128_INSTALLED])
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +00002666 Context->setInt128Installed();
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002667
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002668 ReadPragmaDiagnosticMappings(Context->getDiagnostics());
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002669
2670 // If there were any CUDA special declarations, deserialize them.
2671 if (!CUDASpecialDeclRefs.empty()) {
2672 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
2673 Context->setcudaConfigureCallDecl(
2674 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
2675 }
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002676}
2677
Douglas Gregorb64c1932009-05-12 01:31:05 +00002678/// \brief Retrieve the name of the original source file name
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002679/// directly from the AST file, without actually loading the AST
Douglas Gregorb64c1932009-05-12 01:31:05 +00002680/// file.
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002681std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +00002682 FileManager &FileMgr,
Daniel Dunbar93ebb1b2009-12-03 09:13:06 +00002683 Diagnostic &Diags) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002684 // Open the AST file.
Douglas Gregorb64c1932009-05-12 01:31:05 +00002685 std::string ErrStr;
2686 llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
Chris Lattner39b49bc2010-11-23 08:35:12 +00002687 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
Douglas Gregorb64c1932009-05-12 01:31:05 +00002688 if (!Buffer) {
Daniel Dunbar93ebb1b2009-12-03 09:13:06 +00002689 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002690 return std::string();
2691 }
2692
2693 // Initialize the stream
2694 llvm::BitstreamReader StreamFile;
2695 llvm::BitstreamCursor Stream;
Mike Stump1eb44332009-09-09 15:08:12 +00002696 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregorb64c1932009-05-12 01:31:05 +00002697 (const unsigned char *)Buffer->getBufferEnd());
2698 Stream.init(StreamFile);
2699
2700 // Sniff for the signature.
2701 if (Stream.Read(8) != 'C' ||
2702 Stream.Read(8) != 'P' ||
2703 Stream.Read(8) != 'C' ||
2704 Stream.Read(8) != 'H') {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002705 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002706 return std::string();
2707 }
2708
2709 RecordData Record;
2710 while (!Stream.AtEndOfStream()) {
2711 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00002712
Douglas Gregorb64c1932009-05-12 01:31:05 +00002713 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2714 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump1eb44332009-09-09 15:08:12 +00002715
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002716 // We only know the AST subblock ID.
Douglas Gregorb64c1932009-05-12 01:31:05 +00002717 switch (BlockID) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002718 case AST_BLOCK_ID:
2719 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002720 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002721 return std::string();
2722 }
2723 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002724
Douglas Gregorb64c1932009-05-12 01:31:05 +00002725 default:
2726 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002727 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002728 return std::string();
2729 }
2730 break;
2731 }
2732 continue;
2733 }
2734
2735 if (Code == llvm::bitc::END_BLOCK) {
2736 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002737 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002738 return std::string();
2739 }
2740 continue;
2741 }
2742
2743 if (Code == llvm::bitc::DEFINE_ABBREV) {
2744 Stream.ReadAbbrevRecord();
2745 continue;
2746 }
2747
2748 Record.clear();
2749 const char *BlobStart = 0;
2750 unsigned BlobLen = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002751 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002752 == ORIGINAL_FILE_NAME)
Douglas Gregorb64c1932009-05-12 01:31:05 +00002753 return std::string(BlobStart, BlobLen);
Mike Stump1eb44332009-09-09 15:08:12 +00002754 }
Douglas Gregorb64c1932009-05-12 01:31:05 +00002755
2756 return std::string();
2757}
2758
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002759/// \brief Parse the record that corresponds to a LangOptions data
2760/// structure.
2761///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002762/// This routine parses the language options from the AST file and then gives
2763/// them to the AST listener if one is set.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002764///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002765/// \returns true if the listener deems the file unacceptable, false otherwise.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002766bool ASTReader::ParseLanguageOptions(
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002767 const llvm::SmallVectorImpl<uint64_t> &Record) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002768 if (Listener) {
2769 LangOptions LangOpts;
Mike Stump1eb44332009-09-09 15:08:12 +00002770
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002771 #define PARSE_LANGOPT(Option) \
2772 LangOpts.Option = Record[Idx]; \
2773 ++Idx
Mike Stump1eb44332009-09-09 15:08:12 +00002774
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002775 unsigned Idx = 0;
2776 PARSE_LANGOPT(Trigraphs);
2777 PARSE_LANGOPT(BCPLComment);
2778 PARSE_LANGOPT(DollarIdents);
2779 PARSE_LANGOPT(AsmPreprocessor);
2780 PARSE_LANGOPT(GNUMode);
Chandler Carrutheb5d7b72010-04-17 20:17:31 +00002781 PARSE_LANGOPT(GNUKeywords);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002782 PARSE_LANGOPT(ImplicitInt);
2783 PARSE_LANGOPT(Digraphs);
2784 PARSE_LANGOPT(HexFloats);
2785 PARSE_LANGOPT(C99);
2786 PARSE_LANGOPT(Microsoft);
2787 PARSE_LANGOPT(CPlusPlus);
2788 PARSE_LANGOPT(CPlusPlus0x);
2789 PARSE_LANGOPT(CXXOperatorNames);
2790 PARSE_LANGOPT(ObjC1);
2791 PARSE_LANGOPT(ObjC2);
2792 PARSE_LANGOPT(ObjCNonFragileABI);
Fariborz Jahanian412e7982010-02-09 19:31:38 +00002793 PARSE_LANGOPT(ObjCNonFragileABI2);
Fariborz Jahanianf84109e2011-01-07 18:59:25 +00002794 PARSE_LANGOPT(AppleKext);
Ted Kremenekc32647d2010-12-23 21:35:43 +00002795 PARSE_LANGOPT(ObjCDefaultSynthProperties);
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +00002796 PARSE_LANGOPT(NoConstantCFStrings);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002797 PARSE_LANGOPT(PascalStrings);
2798 PARSE_LANGOPT(WritableStrings);
2799 PARSE_LANGOPT(LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +00002800 PARSE_LANGOPT(AltiVec);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002801 PARSE_LANGOPT(Exceptions);
Daniel Dunbar73482882010-02-10 18:48:44 +00002802 PARSE_LANGOPT(SjLjExceptions);
Anders Carlssonda4b7cf2011-02-19 23:53:54 +00002803 PARSE_LANGOPT(ObjCExceptions);
Douglas Gregor6f755502011-02-01 15:15:22 +00002804 PARSE_LANGOPT(MSBitfields);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002805 PARSE_LANGOPT(NeXTRuntime);
2806 PARSE_LANGOPT(Freestanding);
2807 PARSE_LANGOPT(NoBuiltin);
2808 PARSE_LANGOPT(ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +00002809 PARSE_LANGOPT(POSIXThreads);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002810 PARSE_LANGOPT(Blocks);
2811 PARSE_LANGOPT(EmitAllDecls);
2812 PARSE_LANGOPT(MathErrno);
Chris Lattnera4d71452010-06-26 21:25:03 +00002813 LangOpts.setSignedOverflowBehavior((LangOptions::SignedOverflowBehaviorTy)
2814 Record[Idx++]);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002815 PARSE_LANGOPT(HeinousExtensions);
2816 PARSE_LANGOPT(Optimize);
2817 PARSE_LANGOPT(OptimizeSize);
2818 PARSE_LANGOPT(Static);
2819 PARSE_LANGOPT(PICLevel);
2820 PARSE_LANGOPT(GNUInline);
2821 PARSE_LANGOPT(NoInline);
2822 PARSE_LANGOPT(AccessControl);
2823 PARSE_LANGOPT(CharIsSigned);
John Thompsona6fda122009-11-05 20:14:16 +00002824 PARSE_LANGOPT(ShortWChar);
Argyrios Kyrtzidisb1bdced2011-01-15 02:56:16 +00002825 PARSE_LANGOPT(ShortEnums);
Chris Lattnera4d71452010-06-26 21:25:03 +00002826 LangOpts.setGCMode((LangOptions::GCMode)Record[Idx++]);
John McCall1fb0caa2010-10-22 21:05:15 +00002827 LangOpts.setVisibilityMode((Visibility)Record[Idx++]);
Daniel Dunbarab8e2812009-09-21 04:16:19 +00002828 LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode)
Chris Lattnera4d71452010-06-26 21:25:03 +00002829 Record[Idx++]);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002830 PARSE_LANGOPT(InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +00002831 PARSE_LANGOPT(OpenCL);
Peter Collingbourne08a53262010-12-01 19:14:57 +00002832 PARSE_LANGOPT(CUDA);
Mike Stump9c276ae2009-12-12 01:27:46 +00002833 PARSE_LANGOPT(CatchUndefined);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002834 PARSE_LANGOPT(DefaultFPContract);
Mike Stump9c276ae2009-12-12 01:27:46 +00002835 // FIXME: Missing ElideConstructors?!
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002836 #undef PARSE_LANGOPT
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002837
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002838 return Listener->ReadLanguageOptions(LangOpts);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002839 }
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002840
2841 return false;
2842}
2843
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002844void ASTReader::ReadPreprocessedEntities() {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002845 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2846 PerFileData &F = *Chain[I];
2847 if (!F.PreprocessorDetailCursor.getBitStreamReader())
2848 continue;
2849
2850 SavedStreamPosition SavedPosition(F.PreprocessorDetailCursor);
2851 F.PreprocessorDetailCursor.JumpToBit(F.PreprocessorDetailStartOffset);
2852 while (LoadPreprocessedEntity(F)) { }
2853 }
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002854}
2855
Douglas Gregor0a480292011-02-11 19:46:30 +00002856PreprocessedEntity *ASTReader::ReadPreprocessedEntityAtOffset(uint64_t Offset) {
Douglas Gregor89d99802010-11-30 06:16:57 +00002857 PerFileData *F = 0;
2858 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2859 if (Offset < Chain[I]->SizeInBits) {
2860 F = Chain[I];
2861 break;
2862 }
2863
2864 Offset -= Chain[I]->SizeInBits;
2865 }
2866
2867 if (!F) {
2868 Error("Malformed preprocessed entity offset");
2869 return 0;
2870 }
2871
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002872 // Keep track of where we are in the stream, then jump back there
2873 // after reading this entity.
2874 SavedStreamPosition SavedPosition(F->PreprocessorDetailCursor);
2875 F->PreprocessorDetailCursor.JumpToBit(Offset);
2876 return LoadPreprocessedEntity(*F);
Douglas Gregor89d99802010-11-30 06:16:57 +00002877}
2878
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002879HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
2880 HeaderFileInfoTrait Trait(FE->getName());
2881 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2882 PerFileData &F = *Chain[I];
2883 HeaderFileInfoLookupTable *Table
2884 = static_cast<HeaderFileInfoLookupTable *>(F.HeaderFileInfoTable);
2885 if (!Table)
2886 continue;
2887
2888 // Look in the on-disk hash table for an entry for this file name.
2889 HeaderFileInfoLookupTable::iterator Pos = Table->find(FE->getName(),
2890 &Trait);
2891 if (Pos == Table->end())
2892 continue;
2893
2894 HeaderFileInfo HFI = *Pos;
2895 if (Listener)
2896 Listener->ReadHeaderFileInfo(HFI, FE->getUID());
2897
2898 return HFI;
2899 }
2900
2901 return HeaderFileInfo();
2902}
2903
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002904void ASTReader::ReadPragmaDiagnosticMappings(Diagnostic &Diag) {
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002905 unsigned Idx = 0;
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002906 while (Idx < PragmaDiagMappings.size()) {
2907 SourceLocation
2908 Loc = SourceLocation::getFromRawEncoding(PragmaDiagMappings[Idx++]);
2909 while (1) {
2910 assert(Idx < PragmaDiagMappings.size() &&
2911 "Invalid data, didn't find '-1' marking end of diag/map pairs");
2912 if (Idx >= PragmaDiagMappings.size())
2913 break; // Something is messed up but at least avoid infinite loop in
2914 // release build.
2915 unsigned DiagID = PragmaDiagMappings[Idx++];
2916 if (DiagID == (unsigned)-1)
2917 break; // no more diag/map pairs for this location.
2918 diag::Mapping Map = (diag::Mapping)PragmaDiagMappings[Idx++];
2919 Diag.setDiagnosticMapping(DiagID, Map, Loc);
2920 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002921 }
2922}
2923
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002924/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002925ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002926 PerFileData *F = 0;
2927 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2928 F = Chain[N - I - 1];
2929 if (Index < F->LocalNumTypes)
2930 break;
2931 Index -= F->LocalNumTypes;
2932 }
2933 assert(F && F->LocalNumTypes > Index && "Broken chain");
Sebastian Redlc3632732010-10-05 15:59:54 +00002934 return RecordLocation(F, F->TypeOffsets[Index]);
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002935}
2936
2937/// \brief Read and return the type with the given index..
Douglas Gregor2cf26342009-04-09 22:27:44 +00002938///
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002939/// The index is the type ID, shifted and minus the number of predefs. This
2940/// routine actually reads the record corresponding to the type at the given
2941/// location. It is a helper routine for GetType, which deals with reading type
2942/// IDs.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002943QualType ASTReader::ReadTypeRecord(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002944 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00002945 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00002946
Douglas Gregor0b748912009-04-14 21:18:50 +00002947 // Keep track of where we are in the stream, then jump back there
2948 // after reading this type.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002949 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregor0b748912009-04-14 21:18:50 +00002950
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00002951 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redl27372b42010-08-11 18:52:41 +00002952
Douglas Gregord89275b2009-07-06 18:54:52 +00002953 // Note that we are loading a type record.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00002954 Deserializing AType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00002955
Sebastian Redlc3632732010-10-05 15:59:54 +00002956 DeclsCursor.JumpToBit(Loc.Offset);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002957 RecordData Record;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002958 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002959 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
2960 case TYPE_EXT_QUAL: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002961 if (Record.size() != 2) {
2962 Error("Incorrect encoding of extended qualifier type");
2963 return QualType();
2964 }
Douglas Gregor6d473962009-04-15 22:00:08 +00002965 QualType Base = GetType(Record[0]);
John McCall0953e762009-09-24 19:53:00 +00002966 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[1]);
2967 return Context->getQualifiedType(Base, Quals);
Douglas Gregor6d473962009-04-15 22:00:08 +00002968 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002969
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002970 case TYPE_COMPLEX: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002971 if (Record.size() != 1) {
2972 Error("Incorrect encoding of complex type");
2973 return QualType();
2974 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002975 QualType ElemType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002976 return Context->getComplexType(ElemType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002977 }
2978
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002979 case TYPE_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002980 if (Record.size() != 1) {
2981 Error("Incorrect encoding of pointer type");
2982 return QualType();
2983 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002984 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002985 return Context->getPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002986 }
2987
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002988 case TYPE_BLOCK_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002989 if (Record.size() != 1) {
2990 Error("Incorrect encoding of block pointer type");
2991 return QualType();
2992 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002993 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002994 return Context->getBlockPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002995 }
2996
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002997 case TYPE_LVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002998 if (Record.size() != 1) {
2999 Error("Incorrect encoding of lvalue reference type");
3000 return QualType();
3001 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003002 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003003 return Context->getLValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003004 }
3005
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003006 case TYPE_RVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003007 if (Record.size() != 1) {
3008 Error("Incorrect encoding of rvalue reference type");
3009 return QualType();
3010 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003011 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003012 return Context->getRValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003013 }
3014
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003015 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidis240437b2010-07-02 11:55:15 +00003016 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003017 Error("Incorrect encoding of member pointer type");
3018 return QualType();
3019 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003020 QualType PointeeType = GetType(Record[0]);
3021 QualType ClassType = GetType(Record[1]);
Douglas Gregor1ab55e92010-12-10 17:03:06 +00003022 if (PointeeType.isNull() || ClassType.isNull())
3023 return QualType();
3024
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003025 return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregor2cf26342009-04-09 22:27:44 +00003026 }
3027
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003028 case TYPE_CONSTANT_ARRAY: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003029 QualType ElementType = GetType(Record[0]);
3030 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3031 unsigned IndexTypeQuals = Record[2];
3032 unsigned Idx = 3;
3033 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003034 return Context->getConstantArrayType(ElementType, Size,
3035 ASM, IndexTypeQuals);
3036 }
3037
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003038 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003039 QualType ElementType = GetType(Record[0]);
3040 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3041 unsigned IndexTypeQuals = Record[2];
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003042 return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003043 }
3044
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003045 case TYPE_VARIABLE_ARRAY: {
Douglas Gregor0b748912009-04-14 21:18:50 +00003046 QualType ElementType = GetType(Record[0]);
3047 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3048 unsigned IndexTypeQuals = Record[2];
Sebastian Redlc3632732010-10-05 15:59:54 +00003049 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
3050 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
3051 return Context->getVariableArrayType(ElementType, ReadExpr(*Loc.F),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003052 ASM, IndexTypeQuals,
3053 SourceRange(LBLoc, RBLoc));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003054 }
3055
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003056 case TYPE_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00003057 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003058 Error("incorrect encoding of vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003059 return QualType();
3060 }
3061
3062 QualType ElementType = GetType(Record[0]);
3063 unsigned NumElements = Record[1];
Bob Wilsone86d78c2010-11-10 21:56:12 +00003064 unsigned VecKind = Record[2];
Chris Lattner788b0fd2010-06-23 06:00:24 +00003065 return Context->getVectorType(ElementType, NumElements,
Bob Wilsone86d78c2010-11-10 21:56:12 +00003066 (VectorType::VectorKind)VecKind);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003067 }
3068
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003069 case TYPE_EXT_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00003070 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003071 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003072 return QualType();
3073 }
3074
3075 QualType ElementType = GetType(Record[0]);
3076 unsigned NumElements = Record[1];
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003077 return Context->getExtVectorType(ElementType, NumElements);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003078 }
3079
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003080 case TYPE_FUNCTION_NO_PROTO: {
Rafael Espindola425ef722010-03-30 22:15:11 +00003081 if (Record.size() != 4) {
Douglas Gregora02b1472009-04-28 21:53:25 +00003082 Error("incorrect encoding of no-proto function type");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003083 return QualType();
3084 }
3085 QualType ResultType = GetType(Record[0]);
Rafael Espindola425ef722010-03-30 22:15:11 +00003086 FunctionType::ExtInfo Info(Record[1], Record[2], (CallingConv)Record[3]);
Rafael Espindola264ba482010-03-30 20:24:48 +00003087 return Context->getFunctionNoProtoType(ResultType, Info);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003088 }
3089
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003090 case TYPE_FUNCTION_PROTO: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003091 QualType ResultType = GetType(Record[0]);
John McCalle23cf432010-12-14 08:05:40 +00003092
3093 FunctionProtoType::ExtProtoInfo EPI;
3094 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
3095 /*regparm*/ Record[2],
3096 static_cast<CallingConv>(Record[3]));
3097
Rafael Espindola425ef722010-03-30 22:15:11 +00003098 unsigned Idx = 4;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003099 unsigned NumParams = Record[Idx++];
3100 llvm::SmallVector<QualType, 16> ParamTypes;
3101 for (unsigned I = 0; I != NumParams; ++I)
3102 ParamTypes.push_back(GetType(Record[Idx++]));
John McCalle23cf432010-12-14 08:05:40 +00003103
3104 EPI.Variadic = Record[Idx++];
3105 EPI.TypeQuals = Record[Idx++];
Douglas Gregorc938c162011-01-26 05:01:58 +00003106 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
John McCalle23cf432010-12-14 08:05:40 +00003107 EPI.HasExceptionSpec = Record[Idx++];
3108 EPI.HasAnyExceptionSpec = Record[Idx++];
3109 EPI.NumExceptions = Record[Idx++];
Sebastian Redl465226e2009-05-27 22:11:52 +00003110 llvm::SmallVector<QualType, 2> Exceptions;
John McCalle23cf432010-12-14 08:05:40 +00003111 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
Sebastian Redl465226e2009-05-27 22:11:52 +00003112 Exceptions.push_back(GetType(Record[Idx++]));
John McCalle23cf432010-12-14 08:05:40 +00003113 EPI.Exceptions = Exceptions.data();
Jay Foadbeaaccd2009-05-21 09:52:38 +00003114 return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams,
John McCalle23cf432010-12-14 08:05:40 +00003115 EPI);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003116 }
3117
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003118 case TYPE_UNRESOLVED_USING:
John McCalled976492009-12-04 22:46:56 +00003119 return Context->getTypeDeclType(
3120 cast<UnresolvedUsingTypenameDecl>(GetDecl(Record[0])));
3121
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003122 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003123 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003124 Error("incorrect encoding of typedef type");
3125 return QualType();
3126 }
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003127 TypedefDecl *Decl = cast<TypedefDecl>(GetDecl(Record[0]));
3128 QualType Canonical = GetType(Record[1]);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00003129 if (!Canonical.isNull())
3130 Canonical = Context->getCanonicalType(Canonical);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003131 return Context->getTypedefType(Decl, Canonical);
3132 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003133
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003134 case TYPE_TYPEOF_EXPR:
Sebastian Redlc3632732010-10-05 15:59:54 +00003135 return Context->getTypeOfExprType(ReadExpr(*Loc.F));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003136
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003137 case TYPE_TYPEOF: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003138 if (Record.size() != 1) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003139 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003140 return QualType();
3141 }
3142 QualType UnderlyingType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003143 return Context->getTypeOfType(UnderlyingType);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003144 }
Mike Stump1eb44332009-09-09 15:08:12 +00003145
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003146 case TYPE_DECLTYPE:
Sebastian Redlc3632732010-10-05 15:59:54 +00003147 return Context->getDecltypeType(ReadExpr(*Loc.F));
Anders Carlsson395b4752009-06-24 19:06:50 +00003148
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003149 case TYPE_RECORD: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003150 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003151 Error("incorrect encoding of record type");
3152 return QualType();
3153 }
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003154 bool IsDependent = Record[0];
3155 QualType T = Context->getRecordType(cast<RecordDecl>(GetDecl(Record[1])));
John McCallf4c73712011-01-19 06:33:43 +00003156 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003157 return T;
3158 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003159
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003160 case TYPE_ENUM: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003161 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003162 Error("incorrect encoding of enum type");
3163 return QualType();
3164 }
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003165 bool IsDependent = Record[0];
3166 QualType T = Context->getEnumType(cast<EnumDecl>(GetDecl(Record[1])));
John McCallf4c73712011-01-19 06:33:43 +00003167 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003168 return T;
3169 }
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003170
John McCall9d156a72011-01-06 01:58:22 +00003171 case TYPE_ATTRIBUTED: {
3172 if (Record.size() != 3) {
3173 Error("incorrect encoding of attributed type");
3174 return QualType();
3175 }
3176 QualType modifiedType = GetType(Record[0]);
3177 QualType equivalentType = GetType(Record[1]);
3178 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
3179 return Context->getAttributedType(kind, modifiedType, equivalentType);
3180 }
3181
Abramo Bagnara075f8f12010-12-10 16:29:40 +00003182 case TYPE_PAREN: {
3183 if (Record.size() != 1) {
3184 Error("incorrect encoding of paren type");
3185 return QualType();
3186 }
3187 QualType InnerType = GetType(Record[0]);
3188 return Context->getParenType(InnerType);
3189 }
3190
Douglas Gregor7536dd52010-12-20 02:24:11 +00003191 case TYPE_PACK_EXPANSION: {
Douglas Gregorf9997a02011-02-01 15:24:58 +00003192 if (Record.size() != 2) {
Douglas Gregor7536dd52010-12-20 02:24:11 +00003193 Error("incorrect encoding of pack expansion type");
3194 return QualType();
3195 }
3196 QualType Pattern = GetType(Record[0]);
3197 if (Pattern.isNull())
3198 return QualType();
Douglas Gregorcded4f62011-01-14 17:04:44 +00003199 llvm::Optional<unsigned> NumExpansions;
3200 if (Record[1])
3201 NumExpansions = Record[1] - 1;
3202 return Context->getPackExpansionType(Pattern, NumExpansions);
Douglas Gregor7536dd52010-12-20 02:24:11 +00003203 }
3204
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003205 case TYPE_ELABORATED: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00003206 unsigned Idx = 0;
3207 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3208 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3209 QualType NamedType = GetType(Record[Idx++]);
3210 return Context->getElaboratedType(Keyword, NNS, NamedType);
John McCall7da24312009-09-05 00:15:47 +00003211 }
3212
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003213 case TYPE_OBJC_INTERFACE: {
Chris Lattnerc6fa4452009-04-22 06:45:28 +00003214 unsigned Idx = 0;
3215 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
John McCallc12c5bb2010-05-15 11:32:37 +00003216 return Context->getObjCInterfaceType(ItfD);
3217 }
3218
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003219 case TYPE_OBJC_OBJECT: {
John McCallc12c5bb2010-05-15 11:32:37 +00003220 unsigned Idx = 0;
3221 QualType Base = GetType(Record[Idx++]);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00003222 unsigned NumProtos = Record[Idx++];
3223 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
3224 for (unsigned I = 0; I != NumProtos; ++I)
3225 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
Michael J. Spencer20249a12010-10-21 03:16:25 +00003226 return Context->getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00003227 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003228
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003229 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00003230 unsigned Idx = 0;
John McCallc12c5bb2010-05-15 11:32:37 +00003231 QualType Pointee = GetType(Record[Idx++]);
3232 return Context->getObjCObjectPointerType(Pointee);
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00003233 }
Argyrios Kyrtzidis24fab412009-09-29 19:42:55 +00003234
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003235 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCall49a832b2009-10-18 09:09:24 +00003236 unsigned Idx = 0;
3237 QualType Parm = GetType(Record[Idx++]);
3238 QualType Replacement = GetType(Record[Idx++]);
3239 return
3240 Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
3241 Replacement);
3242 }
John McCall3cb0ebd2010-03-10 03:28:59 +00003243
Douglas Gregorc3069d62011-01-14 02:55:32 +00003244 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
3245 unsigned Idx = 0;
3246 QualType Parm = GetType(Record[Idx++]);
3247 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
3248 return Context->getSubstTemplateTypeParmPackType(
3249 cast<TemplateTypeParmType>(Parm),
3250 ArgPack);
3251 }
3252
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003253 case TYPE_INJECTED_CLASS_NAME: {
John McCall3cb0ebd2010-03-10 03:28:59 +00003254 CXXRecordDecl *D = cast<CXXRecordDecl>(GetDecl(Record[0]));
3255 QualType TST = GetType(Record[1]); // probably derivable
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00003256 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003257 // for AST reading, too much interdependencies.
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00003258 return
3259 QualType(new (*Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCall3cb0ebd2010-03-10 03:28:59 +00003260 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003261
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003262 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003263 unsigned Idx = 0;
3264 unsigned Depth = Record[Idx++];
3265 unsigned Index = Record[Idx++];
3266 bool Pack = Record[Idx++];
3267 IdentifierInfo *Name = GetIdentifierInfo(Record, Idx);
3268 return Context->getTemplateTypeParmType(Depth, Index, Pack, Name);
3269 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003270
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003271 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00003272 unsigned Idx = 0;
3273 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3274 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3275 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +00003276 QualType Canon = GetType(Record[Idx++]);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00003277 if (!Canon.isNull())
3278 Canon = Context->getCanonicalType(Canon);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +00003279 return Context->getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00003280 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003281
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003282 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00003283 unsigned Idx = 0;
3284 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3285 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3286 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
3287 unsigned NumArgs = Record[Idx++];
3288 llvm::SmallVector<TemplateArgument, 8> Args;
3289 Args.reserve(NumArgs);
3290 while (NumArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00003291 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00003292 return Context->getDependentTemplateSpecializationType(Keyword, NNS, Name,
3293 Args.size(), Args.data());
3294 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003295
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003296 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00003297 unsigned Idx = 0;
3298
3299 // ArrayType
3300 QualType ElementType = GetType(Record[Idx++]);
3301 ArrayType::ArraySizeModifier ASM
3302 = (ArrayType::ArraySizeModifier)Record[Idx++];
3303 unsigned IndexTypeQuals = Record[Idx++];
3304
3305 // DependentSizedArrayType
Sebastian Redlc3632732010-10-05 15:59:54 +00003306 Expr *NumElts = ReadExpr(*Loc.F);
3307 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00003308
3309 return Context->getDependentSizedArrayType(ElementType, NumElts, ASM,
3310 IndexTypeQuals, Brackets);
3311 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003312
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003313 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003314 unsigned Idx = 0;
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003315 bool IsDependent = Record[Idx++];
Douglas Gregor1aee05d2011-01-15 06:45:20 +00003316 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003317 llvm::SmallVector<TemplateArgument, 8> Args;
Sebastian Redlc3632732010-10-05 15:59:54 +00003318 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003319 QualType Canon = GetType(Record[Idx++]);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003320 QualType T;
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003321 if (Canon.isNull())
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003322 T = Context->getCanonicalTemplateSpecializationType(Name, Args.data(),
3323 Args.size());
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003324 else
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003325 T = Context->getTemplateSpecializationType(Name, Args.data(),
3326 Args.size(), Canon);
John McCallf4c73712011-01-19 06:33:43 +00003327 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003328 return T;
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003329 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003330 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003331 // Suppress a GCC warning
3332 return QualType();
3333}
3334
Sebastian Redlc3632732010-10-05 15:59:54 +00003335class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003336 ASTReader &Reader;
Sebastian Redlc3632732010-10-05 15:59:54 +00003337 ASTReader::PerFileData &F;
Sebastian Redl577d4792010-07-22 22:43:28 +00003338 llvm::BitstreamCursor &DeclsCursor;
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003339 const ASTReader::RecordData &Record;
John McCalla1ee0c52009-10-16 21:56:05 +00003340 unsigned &Idx;
3341
Sebastian Redlc3632732010-10-05 15:59:54 +00003342 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
3343 unsigned &I) {
3344 return Reader.ReadSourceLocation(F, R, I);
3345 }
3346
John McCalla1ee0c52009-10-16 21:56:05 +00003347public:
Sebastian Redlc3632732010-10-05 15:59:54 +00003348 TypeLocReader(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003349 const ASTReader::RecordData &Record, unsigned &Idx)
Sebastian Redlc3632732010-10-05 15:59:54 +00003350 : Reader(Reader), F(F), DeclsCursor(F.DeclsCursor), Record(Record), Idx(Idx)
3351 { }
John McCalla1ee0c52009-10-16 21:56:05 +00003352
John McCall51bd8032009-10-18 01:05:36 +00003353 // We want compile-time assurance that we've enumerated all of
3354 // these, so unfortunately we have to declare them first, then
3355 // define them out-of-line.
3356#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +00003357#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +00003358 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00003359#include "clang/AST/TypeLocNodes.def"
3360
John McCall51bd8032009-10-18 01:05:36 +00003361 void VisitFunctionTypeLoc(FunctionTypeLoc);
3362 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00003363};
3364
John McCall51bd8032009-10-18 01:05:36 +00003365void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCalla1ee0c52009-10-16 21:56:05 +00003366 // nothing to do
3367}
John McCall51bd8032009-10-18 01:05:36 +00003368void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003369 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
Douglas Gregorddf889a2010-01-18 18:04:31 +00003370 if (TL.needsExtraLocalData()) {
3371 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
3372 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
3373 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
3374 TL.setModeAttr(Record[Idx++]);
3375 }
John McCalla1ee0c52009-10-16 21:56:05 +00003376}
John McCall51bd8032009-10-18 01:05:36 +00003377void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003378 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003379}
John McCall51bd8032009-10-18 01:05:36 +00003380void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003381 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003382}
John McCall51bd8032009-10-18 01:05:36 +00003383void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003384 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003385}
John McCall51bd8032009-10-18 01:05:36 +00003386void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003387 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003388}
John McCall51bd8032009-10-18 01:05:36 +00003389void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003390 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003391}
John McCall51bd8032009-10-18 01:05:36 +00003392void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003393 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003394}
John McCall51bd8032009-10-18 01:05:36 +00003395void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003396 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
3397 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003398 if (Record[Idx++])
Sebastian Redlc3632732010-10-05 15:59:54 +00003399 TL.setSizeExpr(Reader.ReadExpr(F));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003400 else
John McCall51bd8032009-10-18 01:05:36 +00003401 TL.setSizeExpr(0);
3402}
3403void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
3404 VisitArrayTypeLoc(TL);
3405}
3406void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
3407 VisitArrayTypeLoc(TL);
3408}
3409void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
3410 VisitArrayTypeLoc(TL);
3411}
3412void TypeLocReader::VisitDependentSizedArrayTypeLoc(
3413 DependentSizedArrayTypeLoc TL) {
3414 VisitArrayTypeLoc(TL);
3415}
3416void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
3417 DependentSizedExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003418 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003419}
3420void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003421 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003422}
3423void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003424 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003425}
3426void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003427 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3428 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
Douglas Gregordab60ad2010-10-01 18:44:50 +00003429 TL.setTrailingReturn(Record[Idx++]);
John McCall51bd8032009-10-18 01:05:36 +00003430 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
John McCall86acc2a2009-10-23 01:28:53 +00003431 TL.setArg(i, cast_or_null<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
John McCall51bd8032009-10-18 01:05:36 +00003432 }
3433}
3434void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
3435 VisitFunctionTypeLoc(TL);
3436}
3437void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
3438 VisitFunctionTypeLoc(TL);
3439}
John McCalled976492009-12-04 22:46:56 +00003440void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003441 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalled976492009-12-04 22:46:56 +00003442}
John McCall51bd8032009-10-18 01:05:36 +00003443void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003444 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003445}
3446void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003447 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3448 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3449 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003450}
3451void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003452 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3453 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3454 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3455 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003456}
3457void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003458 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003459}
3460void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003461 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003462}
3463void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003464 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003465}
John McCall9d156a72011-01-06 01:58:22 +00003466void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
3467 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
3468 if (TL.hasAttrOperand()) {
3469 SourceRange range;
3470 range.setBegin(ReadSourceLocation(Record, Idx));
3471 range.setEnd(ReadSourceLocation(Record, Idx));
3472 TL.setAttrOperandParensRange(range);
3473 }
3474 if (TL.hasAttrExprOperand()) {
3475 if (Record[Idx++])
3476 TL.setAttrExprOperand(Reader.ReadExpr(F));
3477 else
3478 TL.setAttrExprOperand(0);
3479 } else if (TL.hasAttrEnumOperand())
3480 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
3481}
John McCall51bd8032009-10-18 01:05:36 +00003482void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003483 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003484}
John McCall49a832b2009-10-18 09:09:24 +00003485void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
3486 SubstTemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003487 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall49a832b2009-10-18 09:09:24 +00003488}
Douglas Gregorc3069d62011-01-14 02:55:32 +00003489void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
3490 SubstTemplateTypeParmPackTypeLoc TL) {
3491 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3492}
John McCall51bd8032009-10-18 01:05:36 +00003493void TypeLocReader::VisitTemplateSpecializationTypeLoc(
3494 TemplateSpecializationTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003495 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
3496 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3497 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall833ca992009-10-29 08:12:44 +00003498 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
3499 TL.setArgLocInfo(i,
Sebastian Redlc3632732010-10-05 15:59:54 +00003500 Reader.GetTemplateArgumentLocInfo(F,
3501 TL.getTypePtr()->getArg(i).getKind(),
3502 Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003503}
Abramo Bagnara075f8f12010-12-10 16:29:40 +00003504void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
3505 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3506 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3507}
Abramo Bagnara465d41b2010-05-11 21:36:43 +00003508void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003509 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3510 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003511}
John McCall3cb0ebd2010-03-10 03:28:59 +00003512void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003513 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall3cb0ebd2010-03-10 03:28:59 +00003514}
Douglas Gregor4714c122010-03-31 17:34:00 +00003515void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003516 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3517 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
3518 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003519}
John McCall33500952010-06-11 00:33:02 +00003520void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
3521 DependentTemplateSpecializationTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003522 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3523 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
3524 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3525 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3526 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall33500952010-06-11 00:33:02 +00003527 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
3528 TL.setArgLocInfo(I,
Sebastian Redlc3632732010-10-05 15:59:54 +00003529 Reader.GetTemplateArgumentLocInfo(F,
3530 TL.getTypePtr()->getArg(I).getKind(),
3531 Record, Idx));
John McCall33500952010-06-11 00:33:02 +00003532}
Douglas Gregor7536dd52010-12-20 02:24:11 +00003533void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
3534 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
3535}
John McCall51bd8032009-10-18 01:05:36 +00003536void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003537 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallc12c5bb2010-05-15 11:32:37 +00003538}
3539void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
3540 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00003541 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3542 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003543 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Sebastian Redlc3632732010-10-05 15:59:54 +00003544 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003545}
John McCall54e14c42009-10-22 22:37:11 +00003546void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003547 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall54e14c42009-10-22 22:37:11 +00003548}
John McCalla1ee0c52009-10-16 21:56:05 +00003549
Sebastian Redlc3632732010-10-05 15:59:54 +00003550TypeSourceInfo *ASTReader::GetTypeSourceInfo(PerFileData &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00003551 const RecordData &Record,
John McCalla1ee0c52009-10-16 21:56:05 +00003552 unsigned &Idx) {
3553 QualType InfoTy = GetType(Record[Idx++]);
3554 if (InfoTy.isNull())
3555 return 0;
3556
John McCalla93c9342009-12-07 02:54:59 +00003557 TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy);
Sebastian Redlc3632732010-10-05 15:59:54 +00003558 TypeLocReader TLR(*this, F, Record, Idx);
John McCalla93c9342009-12-07 02:54:59 +00003559 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCalla1ee0c52009-10-16 21:56:05 +00003560 TLR.Visit(TL);
John McCalla93c9342009-12-07 02:54:59 +00003561 return TInfo;
John McCalla1ee0c52009-10-16 21:56:05 +00003562}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003563
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003564QualType ASTReader::GetType(TypeID ID) {
John McCall0953e762009-09-24 19:53:00 +00003565 unsigned FastQuals = ID & Qualifiers::FastMask;
3566 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003567
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003568 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003569 QualType T;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003570 switch ((PredefinedTypeIDs)Index) {
3571 case PREDEF_TYPE_NULL_ID: return QualType();
3572 case PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break;
3573 case PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003574
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003575 case PREDEF_TYPE_CHAR_U_ID:
3576 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregor2cf26342009-04-09 22:27:44 +00003577 // FIXME: Check that the signedness of CharTy is correct!
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003578 T = Context->CharTy;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003579 break;
3580
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003581 case PREDEF_TYPE_UCHAR_ID: T = Context->UnsignedCharTy; break;
3582 case PREDEF_TYPE_USHORT_ID: T = Context->UnsignedShortTy; break;
3583 case PREDEF_TYPE_UINT_ID: T = Context->UnsignedIntTy; break;
3584 case PREDEF_TYPE_ULONG_ID: T = Context->UnsignedLongTy; break;
3585 case PREDEF_TYPE_ULONGLONG_ID: T = Context->UnsignedLongLongTy; break;
3586 case PREDEF_TYPE_UINT128_ID: T = Context->UnsignedInt128Ty; break;
3587 case PREDEF_TYPE_SCHAR_ID: T = Context->SignedCharTy; break;
3588 case PREDEF_TYPE_WCHAR_ID: T = Context->WCharTy; break;
3589 case PREDEF_TYPE_SHORT_ID: T = Context->ShortTy; break;
3590 case PREDEF_TYPE_INT_ID: T = Context->IntTy; break;
3591 case PREDEF_TYPE_LONG_ID: T = Context->LongTy; break;
3592 case PREDEF_TYPE_LONGLONG_ID: T = Context->LongLongTy; break;
3593 case PREDEF_TYPE_INT128_ID: T = Context->Int128Ty; break;
3594 case PREDEF_TYPE_FLOAT_ID: T = Context->FloatTy; break;
3595 case PREDEF_TYPE_DOUBLE_ID: T = Context->DoubleTy; break;
3596 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy; break;
3597 case PREDEF_TYPE_OVERLOAD_ID: T = Context->OverloadTy; break;
3598 case PREDEF_TYPE_DEPENDENT_ID: T = Context->DependentTy; break;
3599 case PREDEF_TYPE_NULLPTR_ID: T = Context->NullPtrTy; break;
3600 case PREDEF_TYPE_CHAR16_ID: T = Context->Char16Ty; break;
3601 case PREDEF_TYPE_CHAR32_ID: T = Context->Char32Ty; break;
3602 case PREDEF_TYPE_OBJC_ID: T = Context->ObjCBuiltinIdTy; break;
3603 case PREDEF_TYPE_OBJC_CLASS: T = Context->ObjCBuiltinClassTy; break;
3604 case PREDEF_TYPE_OBJC_SEL: T = Context->ObjCBuiltinSelTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003605 }
3606
3607 assert(!T.isNull() && "Unknown predefined type");
John McCall0953e762009-09-24 19:53:00 +00003608 return T.withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003609 }
3610
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003611 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003612 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl07a353c2010-07-14 20:26:45 +00003613 if (TypesLoaded[Index].isNull()) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003614 TypesLoaded[Index] = ReadTypeRecord(Index);
Douglas Gregor97475832010-10-05 18:37:06 +00003615 if (TypesLoaded[Index].isNull())
3616 return QualType();
3617
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003618 TypesLoaded[Index]->setFromAST();
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003619 TypeIdxs[TypesLoaded[Index]] = TypeIdx::fromTypeID(ID);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003620 if (DeserializationListener)
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003621 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1476ed42010-07-16 16:36:56 +00003622 TypesLoaded[Index]);
Sebastian Redl07a353c2010-07-14 20:26:45 +00003623 }
Mike Stump1eb44332009-09-09 15:08:12 +00003624
John McCall0953e762009-09-24 19:53:00 +00003625 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003626}
3627
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003628TypeID ASTReader::GetTypeID(QualType T) const {
3629 return MakeTypeID(T,
3630 std::bind1st(std::mem_fun(&ASTReader::GetTypeIdx), this));
3631}
3632
3633TypeIdx ASTReader::GetTypeIdx(QualType T) const {
3634 if (T.isNull())
3635 return TypeIdx();
3636 assert(!T.getLocalFastQualifiers());
3637
3638 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3639 // GetTypeIdx is mostly used for computing the hash of DeclarationNames and
3640 // comparing keys of ASTDeclContextNameLookupTable.
3641 // If the type didn't come from the AST file use a specially marked index
3642 // so that any hash/key comparison fail since no such index is stored
3643 // in a AST file.
3644 if (I == TypeIdxs.end())
3645 return TypeIdx(-1);
3646 return I->second;
3647}
3648
Douglas Gregor7c789c12010-10-29 22:39:52 +00003649unsigned ASTReader::getTotalNumCXXBaseSpecifiers() const {
3650 unsigned Result = 0;
3651 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
3652 Result += Chain[I]->LocalNumCXXBaseSpecifiers;
3653
3654 return Result;
3655}
3656
John McCall833ca992009-10-29 08:12:44 +00003657TemplateArgumentLocInfo
Sebastian Redlc3632732010-10-05 15:59:54 +00003658ASTReader::GetTemplateArgumentLocInfo(PerFileData &F,
3659 TemplateArgument::ArgKind Kind,
John McCall833ca992009-10-29 08:12:44 +00003660 const RecordData &Record,
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00003661 unsigned &Index) {
John McCall833ca992009-10-29 08:12:44 +00003662 switch (Kind) {
3663 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00003664 return ReadExpr(F);
John McCall833ca992009-10-29 08:12:44 +00003665 case TemplateArgument::Type:
Sebastian Redlc3632732010-10-05 15:59:54 +00003666 return GetTypeSourceInfo(F, Record, Index);
Douglas Gregor788cd062009-11-11 01:00:40 +00003667 case TemplateArgument::Template: {
Sebastian Redlc3632732010-10-05 15:59:54 +00003668 SourceRange QualifierRange = ReadSourceRange(F, Record, Index);
3669 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003670 return TemplateArgumentLocInfo(QualifierRange, TemplateNameLoc,
3671 SourceLocation());
3672 }
3673 case TemplateArgument::TemplateExpansion: {
3674 SourceRange QualifierRange = ReadSourceRange(F, Record, Index);
3675 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorba68eca2011-01-05 17:40:24 +00003676 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
3677 return TemplateArgumentLocInfo(QualifierRange, TemplateNameLoc,
3678 EllipsisLoc);
Douglas Gregor788cd062009-11-11 01:00:40 +00003679 }
John McCall833ca992009-10-29 08:12:44 +00003680 case TemplateArgument::Null:
3681 case TemplateArgument::Integral:
3682 case TemplateArgument::Declaration:
3683 case TemplateArgument::Pack:
3684 return TemplateArgumentLocInfo();
3685 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00003686 llvm_unreachable("unexpected template argument loc");
John McCall833ca992009-10-29 08:12:44 +00003687 return TemplateArgumentLocInfo();
3688}
3689
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003690TemplateArgumentLoc
Sebastian Redlc3632732010-10-05 15:59:54 +00003691ASTReader::ReadTemplateArgumentLoc(PerFileData &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00003692 const RecordData &Record, unsigned &Index) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003693 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003694
3695 if (Arg.getKind() == TemplateArgument::Expression) {
3696 if (Record[Index++]) // bool InfoHasSameExpr.
3697 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
3698 }
Sebastian Redlc3632732010-10-05 15:59:54 +00003699 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00003700 Record, Index));
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003701}
3702
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003703Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall76bd1f32010-06-01 09:23:16 +00003704 return GetDecl(ID);
3705}
3706
Douglas Gregor7c789c12010-10-29 22:39:52 +00003707uint64_t
3708ASTReader::GetCXXBaseSpecifiersOffset(serialization::CXXBaseSpecifiersID ID) {
3709 if (ID == 0)
3710 return 0;
3711
3712 --ID;
3713 uint64_t Offset = 0;
3714 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3715 if (ID < Chain[I]->LocalNumCXXBaseSpecifiers)
3716 return Offset + Chain[I]->CXXBaseSpecifiersOffsets[ID];
3717
3718 ID -= Chain[I]->LocalNumCXXBaseSpecifiers;
3719 Offset += Chain[I]->SizeInBits;
3720 }
3721
3722 assert(false && "CXXBaseSpecifiers not found");
3723 return 0;
3724}
3725
3726CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
3727 // Figure out which AST file contains this offset.
3728 PerFileData *F = 0;
3729 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3730 if (Offset < Chain[I]->SizeInBits) {
3731 F = Chain[I];
3732 break;
3733 }
3734
3735 Offset -= Chain[I]->SizeInBits;
3736 }
3737
3738 if (!F) {
3739 Error("Malformed AST file: C++ base specifiers at impossible offset");
3740 return 0;
3741 }
3742
3743 llvm::BitstreamCursor &Cursor = F->DeclsCursor;
3744 SavedStreamPosition SavedPosition(Cursor);
3745 Cursor.JumpToBit(Offset);
3746 ReadingKindTracker ReadingKind(Read_Decl, *this);
3747 RecordData Record;
3748 unsigned Code = Cursor.ReadCode();
3749 unsigned RecCode = Cursor.ReadRecord(Code, Record);
3750 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
3751 Error("Malformed AST file: missing C++ base specifiers");
3752 return 0;
3753 }
3754
3755 unsigned Idx = 0;
3756 unsigned NumBases = Record[Idx++];
3757 void *Mem = Context->Allocate(sizeof(CXXBaseSpecifier) * NumBases);
3758 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
3759 for (unsigned I = 0; I != NumBases; ++I)
3760 Bases[I] = ReadCXXBaseSpecifier(*F, Record, Idx);
3761 return Bases;
3762}
3763
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003764TranslationUnitDecl *ASTReader::GetTranslationUnitDecl() {
Sebastian Redl30c514c2010-07-14 23:45:08 +00003765 if (!DeclsLoaded[0]) {
Sebastian Redle1dde812010-08-24 00:50:04 +00003766 ReadDeclRecord(0, 1);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003767 if (DeserializationListener)
Sebastian Redl1476ed42010-07-16 16:36:56 +00003768 DeserializationListener->DeclRead(1, DeclsLoaded[0]);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003769 }
Argyrios Kyrtzidis8871a442010-07-08 17:13:02 +00003770
3771 return cast<TranslationUnitDecl>(DeclsLoaded[0]);
3772}
3773
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003774Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003775 if (ID == 0)
3776 return 0;
3777
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003778 if (ID > DeclsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003779 Error("declaration ID out-of-range for AST file");
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003780 return 0;
3781 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003782
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003783 unsigned Index = ID - 1;
Sebastian Redl30c514c2010-07-14 23:45:08 +00003784 if (!DeclsLoaded[Index]) {
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00003785 ReadDeclRecord(Index, ID);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003786 if (DeserializationListener)
3787 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
3788 }
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003789
3790 return DeclsLoaded[Index];
Douglas Gregor2cf26342009-04-09 22:27:44 +00003791}
3792
Chris Lattner887e2b32009-04-27 05:46:25 +00003793/// \brief Resolve the offset of a statement into a statement.
3794///
3795/// This operation will read a new statement from the external
3796/// source each time it is called, and is meant to be used via a
3797/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003798Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00003799 // Switch case IDs are per Decl.
3800 ClearSwitchCaseIDs();
3801
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003802 // Offset here is a global offset across the entire chain.
3803 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3804 PerFileData &F = *Chain[N - I - 1];
3805 if (Offset < F.SizeInBits) {
3806 // Since we know that this statement is part of a decl, make sure to use
3807 // the decl cursor to read it.
3808 F.DeclsCursor.JumpToBit(Offset);
Sebastian Redlc3632732010-10-05 15:59:54 +00003809 return ReadStmtFromStream(F);
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003810 }
3811 Offset -= F.SizeInBits;
3812 }
3813 llvm_unreachable("Broken chain");
Douglas Gregor250fc9c2009-04-18 00:07:54 +00003814}
3815
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003816bool ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00003817 bool (*isKindWeWant)(Decl::Kind),
John McCall76bd1f32010-06-01 09:23:16 +00003818 llvm::SmallVectorImpl<Decl*> &Decls) {
Mike Stump1eb44332009-09-09 15:08:12 +00003819 assert(DC->hasExternalLexicalStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00003820 "DeclContext has no lexical decls in storage");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003821
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003822 // There might be lexical decls in multiple parts of the chain, for the TU
3823 // at least.
Sebastian Redl4a9eb262010-09-28 02:24:44 +00003824 // DeclContextOffsets might reallocate as we load additional decls below,
3825 // so make a copy of the vector.
3826 DeclContextInfos Infos = DeclContextOffsets[DC];
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003827 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3828 I != E; ++I) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003829 // IDs can be 0 if this context doesn't contain declarations.
3830 if (!I->LexicalDecls)
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003831 continue;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003832
3833 // Load all of the declaration IDs
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00003834 for (const KindDeclIDPair *ID = I->LexicalDecls,
3835 *IDE = ID + I->NumLexicalDecls; ID != IDE; ++ID) {
3836 if (isKindWeWant && !isKindWeWant((Decl::Kind)ID->first))
3837 continue;
3838
3839 Decl *D = GetDecl(ID->second);
Sebastian Redl4a9eb262010-09-28 02:24:44 +00003840 assert(D && "Null decl in lexical decls");
3841 Decls.push_back(D);
3842 }
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003843 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003844
Douglas Gregor25123082009-04-22 22:34:57 +00003845 ++NumLexicalDeclContextsRead;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003846 return false;
3847}
3848
John McCall76bd1f32010-06-01 09:23:16 +00003849DeclContext::lookup_result
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003850ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +00003851 DeclarationName Name) {
Mike Stump1eb44332009-09-09 15:08:12 +00003852 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00003853 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003854 if (!Name)
3855 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
3856 DeclContext::lookup_iterator(0));
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003857
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003858 llvm::SmallVector<NamedDecl *, 64> Decls;
Sebastian Redl8b122732010-08-24 00:49:55 +00003859 // There might be visible decls in multiple parts of the chain, for the TU
Sebastian Redl5967d622010-08-24 00:50:16 +00003860 // and namespaces. For any given name, the last available results replace
3861 // all earlier ones. For this reason, we walk in reverse.
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003862 DeclContextInfos &Infos = DeclContextOffsets[DC];
Sebastian Redl5967d622010-08-24 00:50:16 +00003863 for (DeclContextInfos::reverse_iterator I = Infos.rbegin(), E = Infos.rend();
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003864 I != E; ++I) {
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003865 if (!I->NameLookupTableData)
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003866 continue;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003867
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003868 ASTDeclContextNameLookupTable *LookupTable =
3869 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3870 ASTDeclContextNameLookupTable::iterator Pos = LookupTable->find(Name);
3871 if (Pos == LookupTable->end())
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003872 continue;
3873
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003874 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
3875 for (; Data.first != Data.second; ++Data.first)
3876 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
Sebastian Redl5967d622010-08-24 00:50:16 +00003877 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003878 }
3879
Douglas Gregor25123082009-04-22 22:34:57 +00003880 ++NumVisibleDeclContextsRead;
John McCall76bd1f32010-06-01 09:23:16 +00003881
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003882 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall76bd1f32010-06-01 09:23:16 +00003883 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003884}
3885
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +00003886void ASTReader::MaterializeVisibleDecls(const DeclContext *DC) {
3887 assert(DC->hasExternalVisibleStorage() &&
3888 "DeclContext has no visible decls in storage");
3889
3890 llvm::SmallVector<NamedDecl *, 64> Decls;
3891 // There might be visible decls in multiple parts of the chain, for the TU
3892 // and namespaces.
3893 DeclContextInfos &Infos = DeclContextOffsets[DC];
3894 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3895 I != E; ++I) {
3896 if (!I->NameLookupTableData)
3897 continue;
3898
3899 ASTDeclContextNameLookupTable *LookupTable =
3900 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3901 for (ASTDeclContextNameLookupTable::item_iterator
3902 ItemI = LookupTable->item_begin(),
3903 ItemEnd = LookupTable->item_end() ; ItemI != ItemEnd; ++ItemI) {
3904 ASTDeclContextNameLookupTable::item_iterator::value_type Val
3905 = *ItemI;
3906 ASTDeclContextNameLookupTrait::data_type Data = Val.second;
3907 Decls.clear();
3908 for (; Data.first != Data.second; ++Data.first)
3909 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
3910 MaterializeVisibleDeclsForName(DC, Val.first, Decls);
3911 }
3912 }
3913}
3914
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003915void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003916 assert(Consumer);
3917 while (!InterestingDecls.empty()) {
3918 DeclGroupRef DG(InterestingDecls.front());
3919 InterestingDecls.pop_front();
Sebastian Redl27372b42010-08-11 18:52:41 +00003920 Consumer->HandleInterestingDecl(DG);
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003921 }
3922}
3923
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003924void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor0af2ca42009-04-22 19:09:20 +00003925 this->Consumer = Consumer;
3926
Douglas Gregorfdd01722009-04-14 00:24:19 +00003927 if (!Consumer)
3928 return;
3929
3930 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003931 // Force deserialization of this decl, which will cause it to be queued for
3932 // passing to the consumer.
Daniel Dunbar04a0b502009-09-17 03:06:44 +00003933 GetDecl(ExternalDefinitions[I]);
Douglas Gregorfdd01722009-04-14 00:24:19 +00003934 }
Douglas Gregorc62a2fe2009-04-25 00:41:30 +00003935
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003936 PassInterestingDeclsToConsumer();
Douglas Gregorfdd01722009-04-14 00:24:19 +00003937}
3938
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003939void ASTReader::PrintStats() {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003940 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregor2cf26342009-04-09 22:27:44 +00003941
Mike Stump1eb44332009-09-09 15:08:12 +00003942 unsigned NumTypesLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003943 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall0953e762009-09-24 19:53:00 +00003944 QualType());
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003945 unsigned NumDeclsLoaded
3946 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
3947 (Decl *)0);
3948 unsigned NumIdentifiersLoaded
3949 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
3950 IdentifiersLoaded.end(),
3951 (IdentifierInfo *)0);
Mike Stump1eb44332009-09-09 15:08:12 +00003952 unsigned NumSelectorsLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003953 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
3954 SelectorsLoaded.end(),
3955 Selector());
Douglas Gregor2d41cc12009-04-13 20:50:16 +00003956
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003957 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
3958 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00003959 if (TotalNumSLocEntries)
3960 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
3961 NumSLocEntriesRead, TotalNumSLocEntries,
3962 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003963 if (!TypesLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003964 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003965 NumTypesLoaded, (unsigned)TypesLoaded.size(),
3966 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
3967 if (!DeclsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003968 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003969 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
3970 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003971 if (!IdentifiersLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003972 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003973 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
3974 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Sebastian Redl725cd962010-08-04 20:40:17 +00003975 if (!SelectorsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003976 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redl725cd962010-08-04 20:40:17 +00003977 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
3978 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor83941df2009-04-25 17:48:32 +00003979 if (TotalNumStatements)
3980 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
3981 NumStatementsRead, TotalNumStatements,
3982 ((float)NumStatementsRead/TotalNumStatements * 100));
3983 if (TotalNumMacros)
3984 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
3985 NumMacrosRead, TotalNumMacros,
3986 ((float)NumMacrosRead/TotalNumMacros * 100));
3987 if (TotalLexicalDeclContexts)
3988 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
3989 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
3990 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
3991 * 100));
3992 if (TotalVisibleDeclContexts)
3993 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
3994 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
3995 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
3996 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003997 if (TotalNumMethodPoolEntries) {
Douglas Gregor83941df2009-04-25 17:48:32 +00003998 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003999 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
4000 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor83941df2009-04-25 17:48:32 +00004001 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00004002 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor83941df2009-04-25 17:48:32 +00004003 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00004004 std::fprintf(stderr, "\n");
4005}
4006
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004007void ASTReader::InitializeSema(Sema &S) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00004008 SemaObj = &S;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00004009 S.ExternalSource = this;
4010
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00004011 // Makes sure any declarations that were deserialized "too early"
4012 // still get added to the identifier's declaration chains.
Douglas Gregor76dc8892010-09-24 23:29:12 +00004013 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
4014 if (SemaObj->TUScope)
John McCalld226f652010-08-21 09:40:31 +00004015 SemaObj->TUScope->AddDecl(PreloadedDecls[I]);
Douglas Gregor76dc8892010-09-24 23:29:12 +00004016
4017 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregor668c1a42009-04-21 22:25:48 +00004018 }
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00004019 PreloadedDecls.clear();
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00004020
4021 // If there were any tentative definitions, deserialize them and add
Sebastian Redle9d12b62010-01-31 22:27:38 +00004022 // them to Sema's list of tentative definitions.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00004023 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
4024 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
Sebastian Redle9d12b62010-01-31 22:27:38 +00004025 SemaObj->TentativeDefinitions.push_back(Var);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00004026 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00004027
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00004028 // If there were any unused file scoped decls, deserialize them and add to
4029 // Sema's list of unused file scoped decls.
4030 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
4031 DeclaratorDecl *D = cast<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
4032 SemaObj->UnusedFileScopedDecls.push_back(D);
Tanya Lattnere6bbc012010-02-12 00:07:30 +00004033 }
Douglas Gregor14c22f22009-04-22 22:18:58 +00004034
4035 // If there were any locally-scoped external declarations,
4036 // deserialize them and add them to Sema's table of locally-scoped
4037 // external declarations.
4038 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
4039 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
4040 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
4041 }
Douglas Gregorb81c1702009-04-27 20:06:05 +00004042
4043 // If there were any ext_vector type declarations, deserialize them
4044 // and add them to Sema's vector of such declarations.
4045 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I)
4046 SemaObj->ExtVectorDecls.push_back(
4047 cast<TypedefDecl>(GetDecl(ExtVectorDecls[I])));
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004048
4049 // FIXME: Do VTable uses and dynamic classes deserialize too much ?
4050 // Can we cut them down before writing them ?
4051
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004052 // If there were any dynamic classes declarations, deserialize them
4053 // and add them to Sema's vector of such declarations.
4054 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I)
4055 SemaObj->DynamicClasses.push_back(
4056 cast<CXXRecordDecl>(GetDecl(DynamicClasses[I])));
Fariborz Jahanian32019832010-07-23 19:11:11 +00004057
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00004058 // Load the offsets of the declarations that Sema references.
4059 // They will be lazily deserialized when needed.
4060 if (!SemaDeclRefs.empty()) {
4061 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
4062 SemaObj->StdNamespace = SemaDeclRefs[0];
4063 SemaObj->StdBadAlloc = SemaDeclRefs[1];
4064 }
4065
Sebastian Redlc3632732010-10-05 15:59:54 +00004066 for (PerFileData *F = FirstInSource; F; F = F->NextInSource) {
4067
4068 // If there are @selector references added them to its pool. This is for
4069 // implementation of -Wselector.
4070 if (!F->ReferencedSelectorsData.empty()) {
4071 unsigned int DataSize = F->ReferencedSelectorsData.size()-1;
4072 unsigned I = 0;
4073 while (I < DataSize) {
4074 Selector Sel = DecodeSelector(F->ReferencedSelectorsData[I++]);
4075 SourceLocation SelLoc = ReadSourceLocation(
4076 *F, F->ReferencedSelectorsData, I);
4077 SemaObj->ReferencedSelectors.insert(std::make_pair(Sel, SelLoc));
4078 }
4079 }
4080
4081 // If there were any pending implicit instantiations, deserialize them
4082 // and add them to Sema's queue of such instantiations.
4083 assert(F->PendingInstantiations.size() % 2 == 0 &&
4084 "Expected pairs of entries");
4085 for (unsigned Idx = 0, N = F->PendingInstantiations.size(); Idx < N;) {
4086 ValueDecl *D=cast<ValueDecl>(GetDecl(F->PendingInstantiations[Idx++]));
4087 SourceLocation Loc = ReadSourceLocation(*F, F->PendingInstantiations,Idx);
4088 SemaObj->PendingInstantiations.push_back(std::make_pair(D, Loc));
4089 }
4090 }
4091
4092 // The two special data sets below always come from the most recent PCH,
4093 // which is at the front of the chain.
4094 PerFileData &F = *Chain.front();
4095
4096 // If there were any weak undeclared identifiers, deserialize them and add to
4097 // Sema's list of weak undeclared identifiers.
4098 if (!WeakUndeclaredIdentifiers.empty()) {
4099 unsigned Idx = 0;
4100 for (unsigned I = 0, N = WeakUndeclaredIdentifiers[Idx++]; I != N; ++I) {
4101 IdentifierInfo *WeakId = GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
4102 IdentifierInfo *AliasId= GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
4103 SourceLocation Loc = ReadSourceLocation(F, WeakUndeclaredIdentifiers,Idx);
4104 bool Used = WeakUndeclaredIdentifiers[Idx++];
4105 Sema::WeakInfo WI(AliasId, Loc);
4106 WI.setUsed(Used);
4107 SemaObj->WeakUndeclaredIdentifiers.insert(std::make_pair(WeakId, WI));
4108 }
4109 }
4110
4111 // If there were any VTable uses, deserialize the information and add it
4112 // to Sema's vector and map of VTable uses.
4113 if (!VTableUses.empty()) {
4114 unsigned Idx = 0;
4115 for (unsigned I = 0, N = VTableUses[Idx++]; I != N; ++I) {
4116 CXXRecordDecl *Class = cast<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
4117 SourceLocation Loc = ReadSourceLocation(F, VTableUses, Idx);
4118 bool DefinitionRequired = VTableUses[Idx++];
4119 SemaObj->VTableUses.push_back(std::make_pair(Class, Loc));
4120 SemaObj->VTablesUsed[Class] = DefinitionRequired;
Fariborz Jahanian32019832010-07-23 19:11:11 +00004121 }
4122 }
Peter Collingbourne84bccea2011-02-15 19:46:30 +00004123
4124 if (!FPPragmaOptions.empty()) {
4125 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
4126 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
4127 }
4128
4129 if (!OpenCLExtensions.empty()) {
4130 unsigned I = 0;
4131#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
4132#include "clang/Basic/OpenCLExtensions.def"
4133
4134 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
4135 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00004136}
4137
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004138IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Sebastian Redld8c5abb2010-08-02 18:30:12 +00004139 // Try to find this name within our on-disk hash tables. We start with the
4140 // most recent one, since that one contains the most up-to-date info.
Sebastian Redld27d3fc2010-07-21 22:31:37 +00004141 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004142 ASTIdentifierLookupTable *IdTable
4143 = (ASTIdentifierLookupTable *)Chain[I]->IdentifierLookupTable;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00004144 if (!IdTable)
4145 continue;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00004146 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004147 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00004148 if (Pos == IdTable->end())
4149 continue;
Douglas Gregor668c1a42009-04-21 22:25:48 +00004150
Sebastian Redld27d3fc2010-07-21 22:31:37 +00004151 // Dereferencing the iterator has the effect of building the
4152 // IdentifierInfo node and populating it with the various
4153 // declarations it needs.
Sebastian Redld8c5abb2010-08-02 18:30:12 +00004154 return *Pos;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00004155 }
Sebastian Redld8c5abb2010-08-02 18:30:12 +00004156 return 0;
Douglas Gregor668c1a42009-04-21 22:25:48 +00004157}
4158
Douglas Gregor95f42922010-10-14 22:11:03 +00004159namespace clang {
4160 /// \brief An identifier-lookup iterator that enumerates all of the
4161 /// identifiers stored within a set of AST files.
4162 class ASTIdentifierIterator : public IdentifierIterator {
4163 /// \brief The AST reader whose identifiers are being enumerated.
4164 const ASTReader &Reader;
4165
4166 /// \brief The current index into the chain of AST files stored in
4167 /// the AST reader.
4168 unsigned Index;
4169
4170 /// \brief The current position within the identifier lookup table
4171 /// of the current AST file.
4172 ASTIdentifierLookupTable::key_iterator Current;
4173
4174 /// \brief The end position within the identifier lookup table of
4175 /// the current AST file.
4176 ASTIdentifierLookupTable::key_iterator End;
4177
4178 public:
4179 explicit ASTIdentifierIterator(const ASTReader &Reader);
4180
4181 virtual llvm::StringRef Next();
4182 };
4183}
4184
4185ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
4186 : Reader(Reader), Index(Reader.Chain.size() - 1) {
4187 ASTIdentifierLookupTable *IdTable
4188 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
4189 Current = IdTable->key_begin();
4190 End = IdTable->key_end();
4191}
4192
4193llvm::StringRef ASTIdentifierIterator::Next() {
4194 while (Current == End) {
4195 // If we have exhausted all of our AST files, we're done.
4196 if (Index == 0)
4197 return llvm::StringRef();
4198
4199 --Index;
4200 ASTIdentifierLookupTable *IdTable
4201 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
4202 Current = IdTable->key_begin();
4203 End = IdTable->key_end();
4204 }
4205
4206 // We have any identifiers remaining in the current AST file; return
4207 // the next one.
4208 std::pair<const char*, unsigned> Key = *Current;
4209 ++Current;
4210 return llvm::StringRef(Key.first, Key.second);
4211}
4212
4213IdentifierIterator *ASTReader::getIdentifiers() const {
4214 return new ASTIdentifierIterator(*this);
4215}
4216
Mike Stump1eb44332009-09-09 15:08:12 +00004217std::pair<ObjCMethodList, ObjCMethodList>
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004218ASTReader::ReadMethodPool(Selector Sel) {
Sebastian Redl725cd962010-08-04 20:40:17 +00004219 // Find this selector in a hash table. We want to find the most recent entry.
4220 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4221 PerFileData &F = *Chain[I];
4222 if (!F.SelectorLookupTable)
4223 continue;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00004224
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004225 ASTSelectorLookupTable *PoolTable
4226 = (ASTSelectorLookupTable*)F.SelectorLookupTable;
4227 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
Sebastian Redl725cd962010-08-04 20:40:17 +00004228 if (Pos != PoolTable->end()) {
4229 ++NumSelectorsRead;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00004230 // FIXME: Not quite happy with the statistics here. We probably should
4231 // disable this tracking when called via LoadSelector.
4232 // Also, should entries without methods count as misses?
4233 ++NumMethodPoolEntriesRead;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004234 ASTSelectorLookupTrait::data_type Data = *Pos;
Sebastian Redl725cd962010-08-04 20:40:17 +00004235 if (DeserializationListener)
4236 DeserializationListener->SelectorRead(Data.ID, Sel);
4237 return std::make_pair(Data.Instance, Data.Factory);
4238 }
Douglas Gregor83941df2009-04-25 17:48:32 +00004239 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00004240
Sebastian Redlfa78dec2010-08-04 21:22:45 +00004241 ++NumMethodPoolMisses;
Sebastian Redl725cd962010-08-04 20:40:17 +00004242 return std::pair<ObjCMethodList, ObjCMethodList>();
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00004243}
4244
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004245void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redle58aa892010-08-04 18:21:41 +00004246 // It would be complicated to avoid reading the methods anyway. So don't.
4247 ReadMethodPool(Sel);
4248}
4249
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004250void ASTReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00004251 assert(ID && "Non-zero identifier ID required");
Douglas Gregora02b1472009-04-28 21:53:25 +00004252 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00004253 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004254 if (DeserializationListener)
4255 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregor668c1a42009-04-21 22:25:48 +00004256}
4257
Douglas Gregord89275b2009-07-06 18:54:52 +00004258/// \brief Set the globally-visible declarations associated with the given
4259/// identifier.
4260///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004261/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump1eb44332009-09-09 15:08:12 +00004262/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregord89275b2009-07-06 18:54:52 +00004263/// them.
4264///
4265/// \param II an IdentifierInfo that refers to one or more globally-visible
4266/// declarations.
4267///
4268/// \param DeclIDs the set of declaration IDs with the name @p II that are
4269/// visible at global scope.
4270///
4271/// \param Nonrecursive should be true to indicate that the caller knows that
4272/// this call is non-recursive, and therefore the globally-visible declarations
4273/// will not be placed onto the pending queue.
Mike Stump1eb44332009-09-09 15:08:12 +00004274void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004275ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Douglas Gregord89275b2009-07-06 18:54:52 +00004276 const llvm::SmallVectorImpl<uint32_t> &DeclIDs,
4277 bool Nonrecursive) {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004278 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregord89275b2009-07-06 18:54:52 +00004279 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
4280 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
4281 PII.II = II;
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00004282 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregord89275b2009-07-06 18:54:52 +00004283 return;
4284 }
Mike Stump1eb44332009-09-09 15:08:12 +00004285
Douglas Gregord89275b2009-07-06 18:54:52 +00004286 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
4287 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
4288 if (SemaObj) {
Douglas Gregor914ed9d2010-08-13 03:15:25 +00004289 if (SemaObj->TUScope) {
4290 // Introduce this declaration into the translation-unit scope
4291 // and add it to the declaration chain for this identifier, so
4292 // that (unqualified) name lookup will find it.
John McCalld226f652010-08-21 09:40:31 +00004293 SemaObj->TUScope->AddDecl(D);
Douglas Gregor914ed9d2010-08-13 03:15:25 +00004294 }
Douglas Gregor76dc8892010-09-24 23:29:12 +00004295 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
Douglas Gregord89275b2009-07-06 18:54:52 +00004296 } else {
4297 // Queue this declaration so that it will be added to the
4298 // translation unit scope and identifier's declaration chain
4299 // once a Sema object is known.
4300 PreloadedDecls.push_back(D);
4301 }
4302 }
4303}
4304
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004305IdentifierInfo *ASTReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00004306 if (ID == 0)
4307 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00004308
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00004309 if (IdentifiersLoaded.empty()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004310 Error("no identifier table in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00004311 return 0;
4312 }
Mike Stump1eb44332009-09-09 15:08:12 +00004313
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00004314 assert(PP && "Forgot to set Preprocessor ?");
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00004315 ID -= 1;
4316 if (!IdentifiersLoaded[ID]) {
4317 unsigned Index = ID;
4318 const char *Str = 0;
4319 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4320 PerFileData *F = Chain[N - I - 1];
4321 if (Index < F->LocalNumIdentifiers) {
4322 uint32_t Offset = F->IdentifierOffsets[Index];
4323 Str = F->IdentifierTableData + Offset;
4324 break;
4325 }
4326 Index -= F->LocalNumIdentifiers;
4327 }
4328 assert(Str && "Broken Chain");
Douglas Gregord6595a42009-04-25 21:04:17 +00004329
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004330 // All of the strings in the AST file are preceded by a 16-bit length.
4331 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenek231bc0b2009-10-23 04:45:31 +00004332 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
4333 // unsigned integers. This is important to avoid integer overflow when
4334 // we cast them to 'unsigned'.
Ted Kremenekff1ea462009-10-23 03:57:22 +00004335 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregor02fc7512009-04-28 20:01:51 +00004336 unsigned StrLen = (((unsigned) StrLenPtr[0])
4337 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00004338 IdentifiersLoaded[ID]
Kovarththanan Rajaratnam811f4262010-03-12 10:32:27 +00004339 = &PP->getIdentifierTable().get(Str, StrLen);
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004340 if (DeserializationListener)
4341 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregorafaf3082009-04-11 00:14:32 +00004342 }
Mike Stump1eb44332009-09-09 15:08:12 +00004343
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00004344 return IdentifiersLoaded[ID];
Douglas Gregor2cf26342009-04-09 22:27:44 +00004345}
4346
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004347void ASTReader::ReadSLocEntry(unsigned ID) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00004348 ReadSLocEntryRecord(ID);
4349}
4350
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004351Selector ASTReader::DecodeSelector(unsigned ID) {
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004352 if (ID == 0)
4353 return Selector();
Mike Stump1eb44332009-09-09 15:08:12 +00004354
Sebastian Redl725cd962010-08-04 20:40:17 +00004355 if (ID > SelectorsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004356 Error("selector ID out of range in AST file");
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004357 return Selector();
4358 }
Douglas Gregor83941df2009-04-25 17:48:32 +00004359
Sebastian Redl725cd962010-08-04 20:40:17 +00004360 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor83941df2009-04-25 17:48:32 +00004361 // Load this selector from the selector table.
Sebastian Redl725cd962010-08-04 20:40:17 +00004362 unsigned Idx = ID - 1;
4363 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4364 PerFileData &F = *Chain[N - I - 1];
4365 if (Idx < F.LocalNumSelectors) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004366 ASTSelectorLookupTrait Trait(*this);
Sebastian Redl725cd962010-08-04 20:40:17 +00004367 SelectorsLoaded[ID - 1] =
4368 Trait.ReadKey(F.SelectorLookupTableData + F.SelectorOffsets[Idx], 0);
4369 if (DeserializationListener)
4370 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
4371 break;
4372 }
4373 Idx -= F.LocalNumSelectors;
4374 }
Douglas Gregor83941df2009-04-25 17:48:32 +00004375 }
4376
Sebastian Redl725cd962010-08-04 20:40:17 +00004377 return SelectorsLoaded[ID - 1];
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004378}
4379
Michael J. Spencer20249a12010-10-21 03:16:25 +00004380Selector ASTReader::GetExternalSelector(uint32_t ID) {
Douglas Gregor719770d2010-04-06 17:30:22 +00004381 return DecodeSelector(ID);
4382}
4383
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004384uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redl725cd962010-08-04 20:40:17 +00004385 // ID 0 (the null selector) is considered an external selector.
4386 return getTotalNumSelectors() + 1;
Douglas Gregor719770d2010-04-06 17:30:22 +00004387}
4388
Mike Stump1eb44332009-09-09 15:08:12 +00004389DeclarationName
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004390ASTReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00004391 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
4392 switch (Kind) {
4393 case DeclarationName::Identifier:
4394 return DeclarationName(GetIdentifierInfo(Record, Idx));
4395
4396 case DeclarationName::ObjCZeroArgSelector:
4397 case DeclarationName::ObjCOneArgSelector:
4398 case DeclarationName::ObjCMultiArgSelector:
Steve Naroffa7503a72009-04-23 15:15:40 +00004399 return DeclarationName(GetSelector(Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004400
4401 case DeclarationName::CXXConstructorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00004402 return Context->DeclarationNames.getCXXConstructorName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00004403 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004404
4405 case DeclarationName::CXXDestructorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00004406 return Context->DeclarationNames.getCXXDestructorName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00004407 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004408
4409 case DeclarationName::CXXConversionFunctionName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00004410 return Context->DeclarationNames.getCXXConversionFunctionName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00004411 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004412
4413 case DeclarationName::CXXOperatorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00004414 return Context->DeclarationNames.getCXXOperatorName(
Douglas Gregor2cf26342009-04-09 22:27:44 +00004415 (OverloadedOperatorKind)Record[Idx++]);
4416
Sean Hunt3e518bd2009-11-29 07:34:05 +00004417 case DeclarationName::CXXLiteralOperatorName:
4418 return Context->DeclarationNames.getCXXLiteralOperatorName(
4419 GetIdentifierInfo(Record, Idx));
4420
Douglas Gregor2cf26342009-04-09 22:27:44 +00004421 case DeclarationName::CXXUsingDirective:
4422 return DeclarationName::getUsingDirectiveName();
4423 }
4424
4425 // Required to silence GCC warning
4426 return DeclarationName();
4427}
Douglas Gregor0a0428e2009-04-10 20:39:37 +00004428
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004429void ASTReader::ReadDeclarationNameLoc(PerFileData &F,
4430 DeclarationNameLoc &DNLoc,
4431 DeclarationName Name,
4432 const RecordData &Record, unsigned &Idx) {
4433 switch (Name.getNameKind()) {
4434 case DeclarationName::CXXConstructorName:
4435 case DeclarationName::CXXDestructorName:
4436 case DeclarationName::CXXConversionFunctionName:
4437 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
4438 break;
4439
4440 case DeclarationName::CXXOperatorName:
4441 DNLoc.CXXOperatorName.BeginOpNameLoc
4442 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4443 DNLoc.CXXOperatorName.EndOpNameLoc
4444 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4445 break;
4446
4447 case DeclarationName::CXXLiteralOperatorName:
4448 DNLoc.CXXLiteralOperatorName.OpNameLoc
4449 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4450 break;
4451
4452 case DeclarationName::Identifier:
4453 case DeclarationName::ObjCZeroArgSelector:
4454 case DeclarationName::ObjCOneArgSelector:
4455 case DeclarationName::ObjCMultiArgSelector:
4456 case DeclarationName::CXXUsingDirective:
4457 break;
4458 }
4459}
4460
4461void ASTReader::ReadDeclarationNameInfo(PerFileData &F,
4462 DeclarationNameInfo &NameInfo,
4463 const RecordData &Record, unsigned &Idx) {
4464 NameInfo.setName(ReadDeclarationName(Record, Idx));
4465 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
4466 DeclarationNameLoc DNLoc;
4467 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
4468 NameInfo.setInfo(DNLoc);
4469}
4470
4471void ASTReader::ReadQualifierInfo(PerFileData &F, QualifierInfo &Info,
4472 const RecordData &Record, unsigned &Idx) {
4473 Info.NNS = ReadNestedNameSpecifier(Record, Idx);
4474 Info.NNSRange = ReadSourceRange(F, Record, Idx);
4475 unsigned NumTPLists = Record[Idx++];
4476 Info.NumTemplParamLists = NumTPLists;
4477 if (NumTPLists) {
4478 Info.TemplParamLists = new (*Context) TemplateParameterList*[NumTPLists];
4479 for (unsigned i=0; i != NumTPLists; ++i)
4480 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
4481 }
4482}
4483
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004484TemplateName
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004485ASTReader::ReadTemplateName(PerFileData &F, const RecordData &Record,
4486 unsigned &Idx) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00004487 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004488 switch (Kind) {
4489 case TemplateName::Template:
4490 return TemplateName(cast_or_null<TemplateDecl>(GetDecl(Record[Idx++])));
4491
4492 case TemplateName::OverloadedTemplate: {
4493 unsigned size = Record[Idx++];
4494 UnresolvedSet<8> Decls;
4495 while (size--)
4496 Decls.addDecl(cast<NamedDecl>(GetDecl(Record[Idx++])));
4497
4498 return Context->getOverloadedTemplateName(Decls.begin(), Decls.end());
4499 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004500
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004501 case TemplateName::QualifiedTemplate: {
4502 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
4503 bool hasTemplKeyword = Record[Idx++];
4504 TemplateDecl *Template = cast<TemplateDecl>(GetDecl(Record[Idx++]));
4505 return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
4506 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004507
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004508 case TemplateName::DependentTemplate: {
4509 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
4510 if (Record[Idx++]) // isIdentifier
4511 return Context->getDependentTemplateName(NNS,
4512 GetIdentifierInfo(Record, Idx));
4513 return Context->getDependentTemplateName(NNS,
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004514 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004515 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004516
4517 case TemplateName::SubstTemplateTemplateParmPack: {
4518 TemplateTemplateParmDecl *Param
4519 = cast_or_null<TemplateTemplateParmDecl>(GetDecl(Record[Idx++]));
4520 if (!Param)
4521 return TemplateName();
4522
4523 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
4524 if (ArgPack.getKind() != TemplateArgument::Pack)
4525 return TemplateName();
4526
4527 return Context->getSubstTemplateTemplateParmPack(Param, ArgPack);
4528 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004529 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004530
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004531 assert(0 && "Unhandled template name kind!");
4532 return TemplateName();
4533}
4534
4535TemplateArgument
Sebastian Redlc3632732010-10-05 15:59:54 +00004536ASTReader::ReadTemplateArgument(PerFileData &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00004537 const RecordData &Record, unsigned &Idx) {
Douglas Gregora7fc9012011-01-05 18:58:31 +00004538 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
4539 switch (Kind) {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004540 case TemplateArgument::Null:
4541 return TemplateArgument();
4542 case TemplateArgument::Type:
4543 return TemplateArgument(GetType(Record[Idx++]));
4544 case TemplateArgument::Declaration:
4545 return TemplateArgument(GetDecl(Record[Idx++]));
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00004546 case TemplateArgument::Integral: {
4547 llvm::APSInt Value = ReadAPSInt(Record, Idx);
4548 QualType T = GetType(Record[Idx++]);
4549 return TemplateArgument(Value, T);
4550 }
Douglas Gregora7fc9012011-01-05 18:58:31 +00004551 case TemplateArgument::Template:
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004552 return TemplateArgument(ReadTemplateName(F, Record, Idx));
Douglas Gregora7fc9012011-01-05 18:58:31 +00004553 case TemplateArgument::TemplateExpansion: {
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004554 TemplateName Name = ReadTemplateName(F, Record, Idx);
Douglas Gregor2be29f42011-01-14 23:41:42 +00004555 llvm::Optional<unsigned> NumTemplateExpansions;
4556 if (unsigned NumExpansions = Record[Idx++])
4557 NumTemplateExpansions = NumExpansions - 1;
4558 return TemplateArgument(Name, NumTemplateExpansions);
Douglas Gregorba68eca2011-01-05 17:40:24 +00004559 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004560 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00004561 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004562 case TemplateArgument::Pack: {
4563 unsigned NumArgs = Record[Idx++];
Douglas Gregor910f8002010-11-07 23:05:16 +00004564 TemplateArgument *Args = new (*Context) TemplateArgument[NumArgs];
4565 for (unsigned I = 0; I != NumArgs; ++I)
4566 Args[I] = ReadTemplateArgument(F, Record, Idx);
4567 return TemplateArgument(Args, NumArgs);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004568 }
4569 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004570
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004571 assert(0 && "Unhandled template argument kind!");
4572 return TemplateArgument();
4573}
4574
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004575TemplateParameterList *
Sebastian Redlc3632732010-10-05 15:59:54 +00004576ASTReader::ReadTemplateParameterList(PerFileData &F,
4577 const RecordData &Record, unsigned &Idx) {
4578 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
4579 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
4580 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004581
4582 unsigned NumParams = Record[Idx++];
4583 llvm::SmallVector<NamedDecl *, 16> Params;
4584 Params.reserve(NumParams);
4585 while (NumParams--)
4586 Params.push_back(cast<NamedDecl>(GetDecl(Record[Idx++])));
Michael J. Spencer20249a12010-10-21 03:16:25 +00004587
4588 TemplateParameterList* TemplateParams =
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004589 TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc,
4590 Params.data(), Params.size(), RAngleLoc);
4591 return TemplateParams;
4592}
4593
4594void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004595ASTReader::
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004596ReadTemplateArgumentList(llvm::SmallVector<TemplateArgument, 8> &TemplArgs,
Sebastian Redlc3632732010-10-05 15:59:54 +00004597 PerFileData &F, const RecordData &Record,
4598 unsigned &Idx) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004599 unsigned NumTemplateArgs = Record[Idx++];
4600 TemplArgs.reserve(NumTemplateArgs);
4601 while (NumTemplateArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00004602 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004603}
4604
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004605/// \brief Read a UnresolvedSet structure.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004606void ASTReader::ReadUnresolvedSet(UnresolvedSetImpl &Set,
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004607 const RecordData &Record, unsigned &Idx) {
4608 unsigned NumDecls = Record[Idx++];
4609 while (NumDecls--) {
4610 NamedDecl *D = cast<NamedDecl>(GetDecl(Record[Idx++]));
4611 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
4612 Set.addDecl(D, AS);
4613 }
4614}
4615
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004616CXXBaseSpecifier
Sebastian Redlc3632732010-10-05 15:59:54 +00004617ASTReader::ReadCXXBaseSpecifier(PerFileData &F,
Nick Lewycky56062202010-07-26 16:56:01 +00004618 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004619 bool isVirtual = static_cast<bool>(Record[Idx++]);
4620 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
4621 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redlf677ea32011-02-05 19:23:19 +00004622 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00004623 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
4624 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00004625 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redlf677ea32011-02-05 19:23:19 +00004626 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
Douglas Gregorf90b27a2011-01-03 22:36:02 +00004627 EllipsisLoc);
Sebastian Redlf677ea32011-02-05 19:23:19 +00004628 Result.setInheritConstructors(inheritConstructors);
4629 return Result;
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004630}
4631
Sean Huntcbb67482011-01-08 20:30:50 +00004632std::pair<CXXCtorInitializer **, unsigned>
4633ASTReader::ReadCXXCtorInitializers(PerFileData &F, const RecordData &Record,
4634 unsigned &Idx) {
4635 CXXCtorInitializer **CtorInitializers = 0;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004636 unsigned NumInitializers = Record[Idx++];
4637 if (NumInitializers) {
4638 ASTContext &C = *getContext();
4639
Sean Huntcbb67482011-01-08 20:30:50 +00004640 CtorInitializers
4641 = new (C) CXXCtorInitializer*[NumInitializers];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004642 for (unsigned i=0; i != NumInitializers; ++i) {
4643 TypeSourceInfo *BaseClassInfo = 0;
4644 bool IsBaseVirtual = false;
4645 FieldDecl *Member = 0;
Francois Pichet00eb3f92010-12-04 09:14:42 +00004646 IndirectFieldDecl *IndirectMember = 0;
Michael J. Spencer20249a12010-10-21 03:16:25 +00004647
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004648 bool IsBaseInitializer = Record[Idx++];
4649 if (IsBaseInitializer) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004650 BaseClassInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004651 IsBaseVirtual = Record[Idx++];
4652 } else {
Francois Pichet00eb3f92010-12-04 09:14:42 +00004653 bool IsIndirectMemberInitializer = Record[Idx++];
4654 if (IsIndirectMemberInitializer)
4655 IndirectMember = cast<IndirectFieldDecl>(GetDecl(Record[Idx++]));
4656 else
4657 Member = cast<FieldDecl>(GetDecl(Record[Idx++]));
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004658 }
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00004659 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redlc3632732010-10-05 15:59:54 +00004660 Expr *Init = ReadExpr(F);
Sebastian Redlc3632732010-10-05 15:59:54 +00004661 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
4662 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004663 bool IsWritten = Record[Idx++];
4664 unsigned SourceOrderOrNumArrayIndices;
4665 llvm::SmallVector<VarDecl *, 8> Indices;
4666 if (IsWritten) {
4667 SourceOrderOrNumArrayIndices = Record[Idx++];
4668 } else {
4669 SourceOrderOrNumArrayIndices = Record[Idx++];
4670 Indices.reserve(SourceOrderOrNumArrayIndices);
4671 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
4672 Indices.push_back(cast<VarDecl>(GetDecl(Record[Idx++])));
4673 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004674
Sean Huntcbb67482011-01-08 20:30:50 +00004675 CXXCtorInitializer *BOMInit;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004676 if (IsBaseInitializer) {
Sean Huntcbb67482011-01-08 20:30:50 +00004677 BOMInit = new (C) CXXCtorInitializer(C, BaseClassInfo, IsBaseVirtual,
4678 LParenLoc, Init, RParenLoc,
4679 MemberOrEllipsisLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004680 } else if (IsWritten) {
Francois Pichet00eb3f92010-12-04 09:14:42 +00004681 if (Member)
Sean Huntcbb67482011-01-08 20:30:50 +00004682 BOMInit = new (C) CXXCtorInitializer(C, Member, MemberOrEllipsisLoc,
4683 LParenLoc, Init, RParenLoc);
Francois Pichet00eb3f92010-12-04 09:14:42 +00004684 else
Sean Huntcbb67482011-01-08 20:30:50 +00004685 BOMInit = new (C) CXXCtorInitializer(C, IndirectMember,
4686 MemberOrEllipsisLoc, LParenLoc,
4687 Init, RParenLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004688 } else {
Sean Huntcbb67482011-01-08 20:30:50 +00004689 BOMInit = CXXCtorInitializer::Create(C, Member, MemberOrEllipsisLoc,
4690 LParenLoc, Init, RParenLoc,
4691 Indices.data(), Indices.size());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004692 }
4693
Argyrios Kyrtzidisf84cde12010-09-06 19:04:27 +00004694 if (IsWritten)
4695 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Sean Huntcbb67482011-01-08 20:30:50 +00004696 CtorInitializers[i] = BOMInit;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004697 }
4698 }
4699
Sean Huntcbb67482011-01-08 20:30:50 +00004700 return std::make_pair(CtorInitializers, NumInitializers);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004701}
4702
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004703NestedNameSpecifier *
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004704ASTReader::ReadNestedNameSpecifier(const RecordData &Record, unsigned &Idx) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004705 unsigned N = Record[Idx++];
4706 NestedNameSpecifier *NNS = 0, *Prev = 0;
4707 for (unsigned I = 0; I != N; ++I) {
4708 NestedNameSpecifier::SpecifierKind Kind
4709 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
4710 switch (Kind) {
4711 case NestedNameSpecifier::Identifier: {
4712 IdentifierInfo *II = GetIdentifierInfo(Record, Idx);
4713 NNS = NestedNameSpecifier::Create(*Context, Prev, II);
4714 break;
4715 }
4716
4717 case NestedNameSpecifier::Namespace: {
4718 NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++]));
4719 NNS = NestedNameSpecifier::Create(*Context, Prev, NS);
4720 break;
4721 }
4722
4723 case NestedNameSpecifier::TypeSpec:
4724 case NestedNameSpecifier::TypeSpecWithTemplate: {
John McCallf4c73712011-01-19 06:33:43 +00004725 const Type *T = GetType(Record[Idx++]).getTypePtrOrNull();
Douglas Gregor1ab55e92010-12-10 17:03:06 +00004726 if (!T)
4727 return 0;
4728
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004729 bool Template = Record[Idx++];
4730 NNS = NestedNameSpecifier::Create(*Context, Prev, Template, T);
4731 break;
4732 }
4733
4734 case NestedNameSpecifier::Global: {
4735 NNS = NestedNameSpecifier::GlobalSpecifier(*Context);
4736 // No associated value, and there can't be a prefix.
4737 break;
4738 }
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004739 }
Argyrios Kyrtzidisd2bb2c02010-07-07 15:46:30 +00004740 Prev = NNS;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004741 }
4742 return NNS;
4743}
4744
4745SourceRange
Sebastian Redlc3632732010-10-05 15:59:54 +00004746ASTReader::ReadSourceRange(PerFileData &F, const RecordData &Record,
4747 unsigned &Idx) {
4748 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
4749 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar8ee59392010-06-02 15:47:10 +00004750 return SourceRange(beg, end);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004751}
4752
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004753/// \brief Read an integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004754llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004755 unsigned BitWidth = Record[Idx++];
4756 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
4757 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
4758 Idx += NumWords;
4759 return Result;
4760}
4761
4762/// \brief Read a signed integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004763llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004764 bool isUnsigned = Record[Idx++];
4765 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
4766}
4767
Douglas Gregor17fc2232009-04-14 21:55:33 +00004768/// \brief Read a floating-point value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004769llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00004770 return llvm::APFloat(ReadAPInt(Record, Idx));
4771}
4772
Douglas Gregor68a2eb02009-04-15 21:30:51 +00004773// \brief Read a string
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004774std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00004775 unsigned Len = Record[Idx++];
Jay Foadbeaaccd2009-05-21 09:52:38 +00004776 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00004777 Idx += Len;
4778 return Result;
4779}
4780
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004781CXXTemporary *ASTReader::ReadCXXTemporary(const RecordData &Record,
Chris Lattnerd2598362010-05-10 00:25:06 +00004782 unsigned &Idx) {
4783 CXXDestructorDecl *Decl = cast<CXXDestructorDecl>(GetDecl(Record[Idx++]));
4784 return CXXTemporary::Create(*Context, Decl);
4785}
4786
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004787DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00004788 return Diag(SourceLocation(), DiagID);
4789}
4790
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004791DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004792 return Diags.Report(Loc, DiagID);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00004793}
Douglas Gregor025452f2009-04-17 00:04:06 +00004794
Douglas Gregor668c1a42009-04-21 22:25:48 +00004795/// \brief Retrieve the identifier table associated with the
4796/// preprocessor.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004797IdentifierTable &ASTReader::getIdentifierTable() {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00004798 assert(PP && "Forgot to set Preprocessor ?");
4799 return PP->getIdentifierTable();
Douglas Gregor668c1a42009-04-21 22:25:48 +00004800}
4801
Douglas Gregor025452f2009-04-17 00:04:06 +00004802/// \brief Record that the given ID maps to the given switch-case
4803/// statement.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004804void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Douglas Gregor025452f2009-04-17 00:04:06 +00004805 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
4806 SwitchCaseStmts[ID] = SC;
4807}
4808
4809/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004810SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Douglas Gregor025452f2009-04-17 00:04:06 +00004811 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
4812 return SwitchCaseStmts[ID];
4813}
Douglas Gregor1de05fe2009-04-17 18:18:49 +00004814
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00004815void ASTReader::ClearSwitchCaseIDs() {
4816 SwitchCaseStmts.clear();
4817}
4818
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004819void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004820 assert(NumCurrentElementsDeserializing &&
4821 "FinishedDeserializing not paired with StartedDeserializing");
4822 if (NumCurrentElementsDeserializing == 1) {
Douglas Gregord89275b2009-07-06 18:54:52 +00004823 // If any identifiers with corresponding top-level declarations have
4824 // been loaded, load those declarations now.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004825 while (!PendingIdentifierInfos.empty()) {
4826 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
4827 PendingIdentifierInfos.front().DeclIDs, true);
4828 PendingIdentifierInfos.pop_front();
Douglas Gregord89275b2009-07-06 18:54:52 +00004829 }
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00004830
Argyrios Kyrtzidis0895d152011-02-12 07:50:47 +00004831 // Ready to load previous declarations of Decls that were delayed.
4832 while (!PendingPreviousDecls.empty()) {
4833 loadAndAttachPreviousDecl(PendingPreviousDecls.front().first,
4834 PendingPreviousDecls.front().second);
4835 PendingPreviousDecls.pop_front();
4836 }
4837
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00004838 // We are not in recursive loading, so it's safe to pass the "interesting"
4839 // decls to the consumer.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004840 if (Consumer)
4841 PassInterestingDeclsToConsumer();
Argyrios Kyrtzidis134db1f2010-10-24 17:26:31 +00004842
4843 assert(PendingForwardRefs.size() == 0 &&
4844 "Some forward refs did not get linked to the definition!");
Douglas Gregord89275b2009-07-06 18:54:52 +00004845 }
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004846 --NumCurrentElementsDeserializing;
Douglas Gregord89275b2009-07-06 18:54:52 +00004847}
Douglas Gregor501c1032010-08-19 00:28:17 +00004848
Sebastian Redle1dde812010-08-24 00:50:04 +00004849ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context,
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00004850 const char *isysroot, bool DisableValidation,
4851 bool DisableStatCache)
Sebastian Redle1dde812010-08-24 00:50:04 +00004852 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
4853 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
4854 Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context),
4855 Consumer(0), isysroot(isysroot), DisableValidation(DisableValidation),
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00004856 DisableStatCache(DisableStatCache), NumStatHits(0), NumStatMisses(0),
4857 NumSLocEntriesRead(0), TotalNumSLocEntries(0), NextSLocOffset(0),
4858 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
4859 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
4860 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
4861 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
4862 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
4863 NumCurrentElementsDeserializing(0)
4864{
Sebastian Redle1dde812010-08-24 00:50:04 +00004865 RelocatablePCH = false;
4866}
4867
4868ASTReader::ASTReader(SourceManager &SourceMgr, FileManager &FileMgr,
4869 Diagnostic &Diags, const char *isysroot,
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00004870 bool DisableValidation, bool DisableStatCache)
Sebastian Redle1dde812010-08-24 00:50:04 +00004871 : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr),
4872 Diags(Diags), SemaObj(0), PP(0), Context(0), Consumer(0),
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00004873 isysroot(isysroot), DisableValidation(DisableValidation),
4874 DisableStatCache(DisableStatCache), NumStatHits(0), NumStatMisses(0),
4875 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Sebastian Redl8db9fae2010-09-22 20:19:08 +00004876 NextSLocOffset(0), NumStatementsRead(0), TotalNumStatements(0),
4877 NumMacrosRead(0), TotalNumMacros(0), NumSelectorsRead(0),
4878 NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
4879 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
4880 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
4881 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
Sebastian Redle1dde812010-08-24 00:50:04 +00004882 RelocatablePCH = false;
4883}
4884
4885ASTReader::~ASTReader() {
4886 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
4887 delete Chain[e - i - 1];
4888 // Delete all visible decl lookup tables
4889 for (DeclContextOffsetsMap::iterator I = DeclContextOffsets.begin(),
4890 E = DeclContextOffsets.end();
4891 I != E; ++I) {
4892 for (DeclContextInfos::iterator J = I->second.begin(), F = I->second.end();
4893 J != F; ++J) {
4894 if (J->NameLookupTableData)
4895 delete static_cast<ASTDeclContextNameLookupTable*>(
4896 J->NameLookupTableData);
4897 }
4898 }
4899 for (DeclContextVisibleUpdatesPending::iterator
4900 I = PendingVisibleUpdates.begin(),
4901 E = PendingVisibleUpdates.end();
4902 I != E; ++I) {
4903 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
4904 F = I->second.end();
4905 J != F; ++J)
4906 delete static_cast<ASTDeclContextNameLookupTable*>(*J);
4907 }
4908}
4909
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00004910ASTReader::PerFileData::PerFileData(ASTFileType Ty)
4911 : Type(Ty), SizeInBits(0), LocalNumSLocEntries(0), SLocOffsets(0), LocalSLocSize(0),
Sebastian Redl301c9b02010-09-22 00:42:27 +00004912 LocalNumIdentifiers(0), IdentifierOffsets(0), IdentifierTableData(0),
4913 IdentifierLookupTable(0), LocalNumMacroDefinitions(0),
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00004914 MacroDefinitionOffsets(0),
4915 LocalNumHeaderFileInfos(0), HeaderFileInfoTableData(0),
4916 HeaderFileInfoTable(0),
4917 LocalNumSelectors(0), SelectorOffsets(0),
Sebastian Redl301c9b02010-09-22 00:42:27 +00004918 SelectorLookupTableData(0), SelectorLookupTable(0), LocalNumDecls(0),
Douglas Gregor7c789c12010-10-29 22:39:52 +00004919 DeclOffsets(0), LocalNumCXXBaseSpecifiers(0), CXXBaseSpecifiersOffsets(0),
4920 LocalNumTypes(0), TypeOffsets(0), StatCache(0),
Sebastian Redla866e652010-10-01 19:59:12 +00004921 NumPreallocatedPreprocessingEntities(0), NextInSource(0)
Douglas Gregor501c1032010-08-19 00:28:17 +00004922{}
4923
4924ASTReader::PerFileData::~PerFileData() {
4925 delete static_cast<ASTIdentifierLookupTable *>(IdentifierLookupTable);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00004926 delete static_cast<HeaderFileInfoLookupTable *>(HeaderFileInfoTable);
Douglas Gregor501c1032010-08-19 00:28:17 +00004927 delete static_cast<ASTSelectorLookupTable *>(SelectorLookupTable);
4928}
4929