blob: 150ad17901f9956b18e0fbfe6f75ad70bdddbc5b [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);
Anders Carlssonda4b7cf2011-02-19 23:53:54 +0000100 PARSE_LANGOPT_IMPORTANT(ObjCExceptions, diag::warn_pch_objc_exceptions);
Anders Carlsson7da99b02011-02-23 03:04:54 +0000101 PARSE_LANGOPT_IMPORTANT(CXXExceptions, diag::warn_pch_cxx_exceptions);
102 PARSE_LANGOPT_IMPORTANT(SjLjExceptions, diag::warn_pch_sjlj_exceptions);
Douglas Gregor6f755502011-02-01 15:15:22 +0000103 PARSE_LANGOPT_IMPORTANT(MSBitfields, diag::warn_pch_ms_bitfields);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000104 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
105 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
106 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
Mike Stump1eb44332009-09-09 15:08:12 +0000107 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000108 diag::warn_pch_thread_safe_statics);
Daniel Dunbar5345c392009-09-03 04:54:28 +0000109 PARSE_LANGOPT_IMPORTANT(POSIXThreads, diag::warn_pch_posix_threads);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000110 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
111 PARSE_LANGOPT_BENIGN(EmitAllDecls);
112 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
Chris Lattnera4d71452010-06-26 21:25:03 +0000113 PARSE_LANGOPT_BENIGN(getSignedOverflowBehavior());
Mike Stump1eb44332009-09-09 15:08:12 +0000114 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000115 diag::warn_pch_heinous_extensions);
116 // FIXME: Most of the options below are benign if the macro wasn't
117 // used. Unfortunately, this means that a PCH compiled without
118 // optimization can't be used with optimization turned on, even
119 // though the only thing that changes is whether __OPTIMIZE__ was
120 // defined... but if __OPTIMIZE__ never showed up in the header, it
121 // doesn't matter. We could consider making this some special kind
122 // of check.
123 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
124 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
125 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
126 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
127 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
128 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
129 PARSE_LANGOPT_IMPORTANT(AccessControl, diag::warn_pch_access_control);
130 PARSE_LANGOPT_IMPORTANT(CharIsSigned, diag::warn_pch_char_signed);
John Thompsona6fda122009-11-05 20:14:16 +0000131 PARSE_LANGOPT_IMPORTANT(ShortWChar, diag::warn_pch_short_wchar);
Argyrios Kyrtzidis9a2b9d72010-10-08 00:25:19 +0000132 PARSE_LANGOPT_IMPORTANT(ShortEnums, diag::warn_pch_short_enums);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000133 if ((PPLangOpts.getGCMode() != 0) != (LangOpts.getGCMode() != 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000134 Reader.Diag(diag::warn_pch_gc_mode)
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000135 << LangOpts.getGCMode() << PPLangOpts.getGCMode();
136 return true;
137 }
138 PARSE_LANGOPT_BENIGN(getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000139 PARSE_LANGOPT_IMPORTANT(getStackProtectorMode(),
140 diag::warn_pch_stack_protector);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000141 PARSE_LANGOPT_BENIGN(InstantiationDepth);
Nate Begeman69cfb9b2009-06-25 22:57:40 +0000142 PARSE_LANGOPT_IMPORTANT(OpenCL, diag::warn_pch_opencl);
Peter Collingbourne08a53262010-12-01 19:14:57 +0000143 PARSE_LANGOPT_IMPORTANT(CUDA, diag::warn_pch_cuda);
Mike Stump9c276ae2009-12-12 01:27:46 +0000144 PARSE_LANGOPT_BENIGN(CatchUndefined);
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000145 PARSE_LANGOPT_IMPORTANT(ElideConstructors, diag::warn_pch_elide_constructors);
Douglas Gregora0068fc2010-07-09 17:35:33 +0000146 PARSE_LANGOPT_BENIGN(SpellChecking);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000147 PARSE_LANGOPT_BENIGN(DefaultFPContract);
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +0000148#undef PARSE_LANGOPT_IMPORTANT
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000149#undef PARSE_LANGOPT_BENIGN
150
151 return false;
152}
153
Daniel Dunbardc3c0d22009-11-11 00:52:11 +0000154bool PCHValidator::ReadTargetTriple(llvm::StringRef Triple) {
155 if (Triple == PP.getTargetInfo().getTriple().str())
156 return false;
157
158 Reader.Diag(diag::warn_pch_target_triple)
159 << Triple << PP.getTargetInfo().getTriple().str();
160 return true;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000161}
162
Benjamin Kramer54353f42010-11-25 18:29:30 +0000163namespace {
164 struct EmptyStringRef {
165 bool operator ()(llvm::StringRef r) const { return r.empty(); }
166 };
167 struct EmptyBlock {
168 bool operator ()(const PCHPredefinesBlock &r) const {return r.Data.empty();}
169 };
170}
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000171
172static bool EqualConcatenations(llvm::SmallVector<llvm::StringRef, 2> L,
173 PCHPredefinesBlocks R) {
174 // First, sum up the lengths.
175 unsigned LL = 0, RL = 0;
176 for (unsigned I = 0, N = L.size(); I != N; ++I) {
177 LL += L[I].size();
178 }
179 for (unsigned I = 0, N = R.size(); I != N; ++I) {
180 RL += R[I].Data.size();
181 }
182 if (LL != RL)
183 return false;
184 if (LL == 0 && RL == 0)
185 return true;
186
187 // Kick out empty parts, they confuse the algorithm below.
188 L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end());
189 R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end());
190
191 // Do it the hard way. At this point, both vectors must be non-empty.
192 llvm::StringRef LR = L[0], RR = R[0].Data;
193 unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size();
Daniel Dunbarc76c9e02010-07-16 00:00:11 +0000194 (void) RN;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000195 for (;;) {
196 // Compare the current pieces.
197 if (LR.size() == RR.size()) {
198 // If they're the same length, it's pretty easy.
199 if (LR != RR)
200 return false;
201 // Both pieces are done, advance.
202 ++LI;
203 ++RI;
204 // If either string is done, they're both done, since they're the same
205 // length.
206 if (LI == LN) {
207 assert(RI == RN && "Strings not the same length after all?");
208 return true;
209 }
210 LR = L[LI];
211 RR = R[RI].Data;
212 } else if (LR.size() < RR.size()) {
213 // Right piece is longer.
214 if (!RR.startswith(LR))
215 return false;
216 ++LI;
217 assert(LI != LN && "Strings not the same length after all?");
218 RR = RR.substr(LR.size());
219 LR = L[LI];
220 } else {
221 // Left piece is longer.
222 if (!LR.startswith(RR))
223 return false;
224 ++RI;
225 assert(RI != RN && "Strings not the same length after all?");
226 LR = LR.substr(RR.size());
227 RR = R[RI].Data;
228 }
229 }
230}
231
232static std::pair<FileID, llvm::StringRef::size_type>
233FindMacro(const PCHPredefinesBlocks &Buffers, llvm::StringRef MacroDef) {
234 std::pair<FileID, llvm::StringRef::size_type> Res;
235 for (unsigned I = 0, N = Buffers.size(); I != N; ++I) {
236 Res.second = Buffers[I].Data.find(MacroDef);
237 if (Res.second != llvm::StringRef::npos) {
238 Res.first = Buffers[I].BufferID;
239 break;
240 }
241 }
242 return Res;
243}
244
245bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000246 llvm::StringRef OriginalFileName,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000247 std::string &SuggestedPredefines) {
Daniel Dunbarc7162932009-11-11 23:58:53 +0000248 // We are in the context of an implicit include, so the predefines buffer will
249 // have a #include entry for the PCH file itself (as normalized by the
250 // preprocessor initialization). Find it and skip over it in the checking
251 // below.
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000252 llvm::SmallString<256> PCHInclude;
253 PCHInclude += "#include \"";
Daniel Dunbarc7162932009-11-11 23:58:53 +0000254 PCHInclude += NormalizeDashIncludePath(OriginalFileName);
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000255 PCHInclude += "\"\n";
256 std::pair<llvm::StringRef,llvm::StringRef> Split =
257 llvm::StringRef(PP.getPredefines()).split(PCHInclude.str());
258 llvm::StringRef Left = Split.first, Right = Split.second;
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000259 if (Left == PP.getPredefines()) {
260 Error("Missing PCH include entry!");
261 return true;
262 }
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000263
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000264 // If the concatenation of all the PCH buffers is equal to the adjusted
265 // command line, we're done.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000266 llvm::SmallVector<llvm::StringRef, 2> CommandLine;
267 CommandLine.push_back(Left);
268 CommandLine.push_back(Right);
269 if (EqualConcatenations(CommandLine, Buffers))
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000270 return false;
271
272 SourceManager &SourceMgr = PP.getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000273
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000274 // The predefines buffers are different. Determine what the differences are,
275 // and whether they require us to reject the PCH file.
Daniel Dunbare6750492009-11-13 16:46:11 +0000276 llvm::SmallVector<llvm::StringRef, 8> PCHLines;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000277 for (unsigned I = 0, N = Buffers.size(); I != N; ++I)
278 Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Daniel Dunbare6750492009-11-13 16:46:11 +0000279
280 llvm::SmallVector<llvm::StringRef, 8> CmdLineLines;
281 Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000282
283 // Pick out implicit #includes after the PCH and don't consider them for
284 // validation; we will insert them into SuggestedPredefines so that the
285 // preprocessor includes them.
286 std::string IncludesAfterPCH;
287 llvm::SmallVector<llvm::StringRef, 8> AfterPCHLines;
288 Right.split(AfterPCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
289 for (unsigned i = 0, e = AfterPCHLines.size(); i != e; ++i) {
290 if (AfterPCHLines[i].startswith("#include ")) {
291 IncludesAfterPCH += AfterPCHLines[i];
292 IncludesAfterPCH += '\n';
293 } else {
294 CmdLineLines.push_back(AfterPCHLines[i]);
295 }
296 }
297
298 // Make sure we add the includes last into SuggestedPredefines before we
299 // exit this function.
300 struct AddIncludesRAII {
301 std::string &SuggestedPredefines;
302 std::string &IncludesAfterPCH;
303
304 AddIncludesRAII(std::string &SuggestedPredefines,
305 std::string &IncludesAfterPCH)
306 : SuggestedPredefines(SuggestedPredefines),
307 IncludesAfterPCH(IncludesAfterPCH) { }
308 ~AddIncludesRAII() {
309 SuggestedPredefines += IncludesAfterPCH;
310 }
311 } AddIncludes(SuggestedPredefines, IncludesAfterPCH);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000312
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000313 // Sort both sets of predefined buffer lines, since we allow some extra
314 // definitions and they may appear at any point in the output.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000315 std::sort(CmdLineLines.begin(), CmdLineLines.end());
316 std::sort(PCHLines.begin(), PCHLines.end());
317
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000318 // Determine which predefines that were used to build the PCH file are missing
319 // from the command line.
320 std::vector<llvm::StringRef> MissingPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000321 std::set_difference(PCHLines.begin(), PCHLines.end(),
322 CmdLineLines.begin(), CmdLineLines.end(),
323 std::back_inserter(MissingPredefines));
324
325 bool MissingDefines = false;
326 bool ConflictingDefines = false;
327 for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) {
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000328 llvm::StringRef Missing = MissingPredefines[I];
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000329 if (Missing.startswith("#include ")) {
330 // An -include was specified when generating the PCH; it is included in
331 // the PCH, just ignore it.
332 continue;
333 }
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000334 if (!Missing.startswith("#define ")) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000335 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
336 return true;
337 }
Mike Stump1eb44332009-09-09 15:08:12 +0000338
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000339 // This is a macro definition. Determine the name of the macro we're
340 // defining.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000341 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000342 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000343 = Missing.find_first_of("( \n\r", StartOfMacroName);
344 assert(EndOfMacroName != std::string::npos &&
345 "Couldn't find the end of the macro name");
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000346 llvm::StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000347
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000348 // Determine whether this macro was given a different definition on the
349 // command line.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000350 std::string MacroDefStart = "#define " + MacroName.str();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000351 std::string::size_type MacroDefLen = MacroDefStart.size();
Daniel Dunbare6750492009-11-13 16:46:11 +0000352 llvm::SmallVector<llvm::StringRef, 8>::iterator ConflictPos
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000353 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
354 MacroDefStart);
355 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000356 if (!ConflictPos->startswith(MacroDefStart)) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000357 // Different macro; we're done.
358 ConflictPos = CmdLineLines.end();
Mike Stump1eb44332009-09-09 15:08:12 +0000359 break;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000360 }
Mike Stump1eb44332009-09-09 15:08:12 +0000361
362 assert(ConflictPos->size() > MacroDefLen &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000363 "Invalid #define in predefines buffer?");
Mike Stump1eb44332009-09-09 15:08:12 +0000364 if ((*ConflictPos)[MacroDefLen] != ' ' &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000365 (*ConflictPos)[MacroDefLen] != '(')
366 continue; // Longer macro name; keep trying.
Mike Stump1eb44332009-09-09 15:08:12 +0000367
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000368 // We found a conflicting macro definition.
369 break;
370 }
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000372 if (ConflictPos != CmdLineLines.end()) {
373 Reader.Diag(diag::warn_cmdline_conflicting_macro_def)
374 << MacroName;
375
376 // Show the definition of this macro within the PCH file.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000377 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
378 FindMacro(Buffers, Missing);
379 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
380 SourceLocation PCHMissingLoc =
381 SourceMgr.getLocForStartOfFile(MacroLoc.first)
382 .getFileLocWithOffset(MacroLoc.second);
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000383 Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000384
385 ConflictingDefines = true;
386 continue;
387 }
Mike Stump1eb44332009-09-09 15:08:12 +0000388
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000389 // If the macro doesn't conflict, then we'll just pick up the macro
390 // definition from the PCH file. Warn the user that they made a mistake.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000391 if (ConflictingDefines)
392 continue; // Don't complain if there are already conflicting defs
Mike Stump1eb44332009-09-09 15:08:12 +0000393
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000394 if (!MissingDefines) {
395 Reader.Diag(diag::warn_cmdline_missing_macro_defs);
396 MissingDefines = true;
397 }
398
399 // Show the definition of this macro within the PCH file.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000400 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
401 FindMacro(Buffers, Missing);
402 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
403 SourceLocation PCHMissingLoc =
404 SourceMgr.getLocForStartOfFile(MacroLoc.first)
405 .getFileLocWithOffset(MacroLoc.second);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000406 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
407 }
Mike Stump1eb44332009-09-09 15:08:12 +0000408
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000409 if (ConflictingDefines)
410 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000411
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000412 // Determine what predefines were introduced based on command-line
413 // parameters that were not present when building the PCH
414 // file. Extra #defines are okay, so long as the identifiers being
415 // defined were not used within the precompiled header.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000416 std::vector<llvm::StringRef> ExtraPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000417 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
418 PCHLines.begin(), PCHLines.end(),
Mike Stump1eb44332009-09-09 15:08:12 +0000419 std::back_inserter(ExtraPredefines));
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000420 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000421 llvm::StringRef &Extra = ExtraPredefines[I];
422 if (!Extra.startswith("#define ")) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000423 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
424 return true;
425 }
426
427 // This is an extra macro definition. Determine the name of the
428 // macro we're defining.
429 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000430 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000431 = Extra.find_first_of("( \n\r", StartOfMacroName);
432 assert(EndOfMacroName != std::string::npos &&
433 "Couldn't find the end of the macro name");
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000434 llvm::StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000435
436 // Check whether this name was used somewhere in the PCH file. If
437 // so, defining it as a macro could change behavior, so we reject
438 // the PCH file.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000439 if (IdentifierInfo *II = Reader.get(MacroName)) {
Daniel Dunbar4fda42e2009-11-11 00:52:00 +0000440 Reader.Diag(diag::warn_macro_name_used_in_pch) << II;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000441 return true;
442 }
443
444 // Add this definition to the suggested predefines buffer.
445 SuggestedPredefines += Extra;
446 SuggestedPredefines += '\n';
447 }
448
449 // If we get here, it's because the predefines buffer had compatible
450 // contents. Accept the PCH file.
451 return false;
452}
453
Douglas Gregor12fab312010-03-16 16:35:32 +0000454void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
455 unsigned ID) {
456 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
457 ++NumHeaderInfos;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000458}
459
460void PCHValidator::ReadCounter(unsigned Value) {
461 PP.setCounterValue(Value);
462}
463
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000464//===----------------------------------------------------------------------===//
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000465// AST reader implementation
Douglas Gregor668c1a42009-04-21 22:25:48 +0000466//===----------------------------------------------------------------------===//
467
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000468void
Sebastian Redl571db7f2010-08-18 23:56:56 +0000469ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000470 DeserializationListener = Listener;
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000471}
472
Chris Lattner4c6f9522009-04-27 05:14:47 +0000473
Douglas Gregor668c1a42009-04-21 22:25:48 +0000474namespace {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000475class ASTSelectorLookupTrait {
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000476 ASTReader &Reader;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000477
478public:
Sebastian Redl5d050072010-08-04 17:20:04 +0000479 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000480 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +0000481 ObjCMethodList Instance, Factory;
482 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000483
484 typedef Selector external_key_type;
485 typedef external_key_type internal_key_type;
486
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000487 explicit ASTSelectorLookupTrait(ASTReader &Reader) : Reader(Reader) { }
Mike Stump1eb44332009-09-09 15:08:12 +0000488
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000489 static bool EqualKey(const internal_key_type& a,
490 const internal_key_type& b) {
491 return a == b;
492 }
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000494 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +0000495 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000496 }
Mike Stump1eb44332009-09-09 15:08:12 +0000497
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000498 // This hopefully will just get inlined and removed by the optimizer.
499 static const internal_key_type&
500 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump1eb44332009-09-09 15:08:12 +0000501
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000502 static std::pair<unsigned, unsigned>
503 ReadKeyDataLength(const unsigned char*& d) {
504 using namespace clang::io;
505 unsigned KeyLen = ReadUnalignedLE16(d);
506 unsigned DataLen = ReadUnalignedLE16(d);
507 return std::make_pair(KeyLen, DataLen);
508 }
Mike Stump1eb44332009-09-09 15:08:12 +0000509
Douglas Gregor83941df2009-04-25 17:48:32 +0000510 internal_key_type ReadKey(const unsigned char* d, unsigned) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000511 using namespace clang::io;
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000512 SelectorTable &SelTable = Reader.getContext()->Selectors;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000513 unsigned N = ReadUnalignedLE16(d);
Mike Stump1eb44332009-09-09 15:08:12 +0000514 IdentifierInfo *FirstII
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000515 = Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
516 if (N == 0)
517 return SelTable.getNullarySelector(FirstII);
518 else if (N == 1)
519 return SelTable.getUnarySelector(FirstII);
520
521 llvm::SmallVector<IdentifierInfo *, 16> Args;
522 Args.push_back(FirstII);
523 for (unsigned I = 1; I != N; ++I)
524 Args.push_back(Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)));
525
Douglas Gregor75fdb232009-05-22 22:45:36 +0000526 return SelTable.getSelector(N, Args.data());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000527 }
Mike Stump1eb44332009-09-09 15:08:12 +0000528
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000529 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen) {
530 using namespace clang::io;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000531
532 data_type Result;
533
Sebastian Redl5d050072010-08-04 17:20:04 +0000534 Result.ID = ReadUnalignedLE32(d);
535 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
536 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
537
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000538 // Load instance methods
539 ObjCMethodList *Prev = 0;
540 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000541 ObjCMethodDecl *Method
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000542 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl5d050072010-08-04 17:20:04 +0000543 if (!Result.Instance.Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000544 // This is the first method, which is the easy case.
Sebastian Redl5d050072010-08-04 17:20:04 +0000545 Result.Instance.Method = Method;
546 Prev = &Result.Instance;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000547 continue;
548 }
549
Ted Kremenek298ed872010-02-11 00:53:01 +0000550 ObjCMethodList *Mem =
551 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
552 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000553 Prev = Prev->Next;
554 }
555
556 // Load factory methods
557 Prev = 0;
558 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000559 ObjCMethodDecl *Method
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000560 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl5d050072010-08-04 17:20:04 +0000561 if (!Result.Factory.Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000562 // This is the first method, which is the easy case.
Sebastian Redl5d050072010-08-04 17:20:04 +0000563 Result.Factory.Method = Method;
564 Prev = &Result.Factory;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000565 continue;
566 }
567
Ted Kremenek298ed872010-02-11 00:53:01 +0000568 ObjCMethodList *Mem =
569 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
570 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000571 Prev = Prev->Next;
572 }
573
574 return Result;
575 }
576};
Mike Stump1eb44332009-09-09 15:08:12 +0000577
578} // end anonymous namespace
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000579
580/// \brief The on-disk hash table used for the global method pool.
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000581typedef OnDiskChainedHashTable<ASTSelectorLookupTrait>
582 ASTSelectorLookupTable;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000583
Sebastian Redlc3632732010-10-05 15:59:54 +0000584namespace clang {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000585class ASTIdentifierLookupTrait {
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000586 ASTReader &Reader;
Sebastian Redlc3632732010-10-05 15:59:54 +0000587 ASTReader::PerFileData &F;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000588
589 // If we know the IdentifierInfo in advance, it is here and we will
590 // not build a new one. Used when deserializing information about an
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000591 // identifier that was constructed before the AST file was read.
Douglas Gregor668c1a42009-04-21 22:25:48 +0000592 IdentifierInfo *KnownII;
593
594public:
595 typedef IdentifierInfo * data_type;
596
597 typedef const std::pair<const char*, unsigned> external_key_type;
598
599 typedef external_key_type internal_key_type;
600
Sebastian Redlc3632732010-10-05 15:59:54 +0000601 ASTIdentifierLookupTrait(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redld27d3fc2010-07-21 22:31:37 +0000602 IdentifierInfo *II = 0)
Sebastian Redlc3632732010-10-05 15:59:54 +0000603 : Reader(Reader), F(F), KnownII(II) { }
Mike Stump1eb44332009-09-09 15:08:12 +0000604
Douglas Gregor668c1a42009-04-21 22:25:48 +0000605 static bool EqualKey(const internal_key_type& a,
606 const internal_key_type& b) {
607 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
608 : false;
609 }
Mike Stump1eb44332009-09-09 15:08:12 +0000610
Douglas Gregor668c1a42009-04-21 22:25:48 +0000611 static unsigned ComputeHash(const internal_key_type& a) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000612 return llvm::HashString(llvm::StringRef(a.first, a.second));
Douglas Gregor668c1a42009-04-21 22:25:48 +0000613 }
Mike Stump1eb44332009-09-09 15:08:12 +0000614
Douglas Gregor668c1a42009-04-21 22:25:48 +0000615 // This hopefully will just get inlined and removed by the optimizer.
616 static const internal_key_type&
617 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump1eb44332009-09-09 15:08:12 +0000618
Douglas Gregor95f42922010-10-14 22:11:03 +0000619 // This hopefully will just get inlined and removed by the optimizer.
620 static const external_key_type&
621 GetExternalKey(const internal_key_type& x) { return x; }
622
Douglas Gregor668c1a42009-04-21 22:25:48 +0000623 static std::pair<unsigned, unsigned>
624 ReadKeyDataLength(const unsigned char*& d) {
625 using namespace clang::io;
Douglas Gregor5f8e3302009-04-25 20:26:24 +0000626 unsigned DataLen = ReadUnalignedLE16(d);
Douglas Gregord6595a42009-04-25 21:04:17 +0000627 unsigned KeyLen = ReadUnalignedLE16(d);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000628 return std::make_pair(KeyLen, DataLen);
629 }
Mike Stump1eb44332009-09-09 15:08:12 +0000630
Douglas Gregor668c1a42009-04-21 22:25:48 +0000631 static std::pair<const char*, unsigned>
632 ReadKey(const unsigned char* d, unsigned n) {
633 assert(n >= 2 && d[n-1] == '\0');
634 return std::make_pair((const char*) d, n-1);
635 }
Mike Stump1eb44332009-09-09 15:08:12 +0000636
637 IdentifierInfo *ReadData(const internal_key_type& k,
Douglas Gregor668c1a42009-04-21 22:25:48 +0000638 const unsigned char* d,
639 unsigned DataLen) {
640 using namespace clang::io;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000641 IdentID ID = ReadUnalignedLE32(d);
Douglas Gregora92193e2009-04-28 21:18:29 +0000642 bool IsInteresting = ID & 0x01;
643
644 // Wipe out the "is interesting" bit.
645 ID = ID >> 1;
646
647 if (!IsInteresting) {
Sebastian Redl083abdf2010-07-27 23:01:28 +0000648 // For uninteresting identifiers, just build the IdentifierInfo
Douglas Gregora92193e2009-04-28 21:18:29 +0000649 // and associate it with the persistent ID.
650 IdentifierInfo *II = KnownII;
651 if (!II)
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000652 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregora92193e2009-04-28 21:18:29 +0000653 Reader.SetIdentifierInfo(ID, II);
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000654 II->setIsFromAST();
Douglas Gregora92193e2009-04-28 21:18:29 +0000655 return II;
656 }
657
Douglas Gregor5998da52009-04-28 21:32:13 +0000658 unsigned Bits = ReadUnalignedLE16(d);
Douglas Gregor2deaea32009-04-22 18:49:13 +0000659 bool CPlusPlusOperatorKeyword = Bits & 0x01;
660 Bits >>= 1;
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000661 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
662 Bits >>= 1;
Douglas Gregor2deaea32009-04-22 18:49:13 +0000663 bool Poisoned = Bits & 0x01;
664 Bits >>= 1;
665 bool ExtensionToken = Bits & 0x01;
666 Bits >>= 1;
667 bool hasMacroDefinition = Bits & 0x01;
668 Bits >>= 1;
669 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
670 Bits >>= 10;
Mike Stump1eb44332009-09-09 15:08:12 +0000671
Douglas Gregor2deaea32009-04-22 18:49:13 +0000672 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregor5998da52009-04-28 21:32:13 +0000673 DataLen -= 6;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000674
675 // Build the IdentifierInfo itself and link the identifier ID with
676 // the new IdentifierInfo.
677 IdentifierInfo *II = KnownII;
678 if (!II)
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000679 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000680 Reader.SetIdentifierInfo(ID, II);
681
Douglas Gregor2deaea32009-04-22 18:49:13 +0000682 // Set or check the various bits in the IdentifierInfo structure.
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000683 // Token IDs are read-only.
684 if (HasRevertedTokenIDToIdentifier)
685 II->RevertTokenIDToIdentifier();
Douglas Gregor2deaea32009-04-22 18:49:13 +0000686 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
Mike Stump1eb44332009-09-09 15:08:12 +0000687 assert(II->isExtensionToken() == ExtensionToken &&
Douglas Gregor2deaea32009-04-22 18:49:13 +0000688 "Incorrect extension token flag");
689 (void)ExtensionToken;
690 II->setIsPoisoned(Poisoned);
691 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
692 "Incorrect C++ operator keyword flag");
693 (void)CPlusPlusOperatorKeyword;
694
Douglas Gregor37e26842009-04-21 23:56:24 +0000695 // If this identifier is a macro, deserialize the macro
696 // definition.
697 if (hasMacroDefinition) {
Douglas Gregor5998da52009-04-28 21:32:13 +0000698 uint32_t Offset = ReadUnalignedLE32(d);
Douglas Gregor295a2a62010-10-30 00:23:06 +0000699 Reader.SetIdentifierIsMacro(II, F, Offset);
Douglas Gregor5998da52009-04-28 21:32:13 +0000700 DataLen -= 4;
Douglas Gregor37e26842009-04-21 23:56:24 +0000701 }
Douglas Gregor668c1a42009-04-21 22:25:48 +0000702
703 // Read all of the declarations visible at global scope with this
704 // name.
Chris Lattner6bf690f2009-04-27 22:17:41 +0000705 if (Reader.getContext() == 0) return II;
Douglas Gregord89275b2009-07-06 18:54:52 +0000706 if (DataLen > 0) {
707 llvm::SmallVector<uint32_t, 4> DeclIDs;
708 for (; DataLen > 0; DataLen -= 4)
709 DeclIDs.push_back(ReadUnalignedLE32(d));
710 Reader.SetGloballyVisibleDecls(II, DeclIDs);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000711 }
Mike Stump1eb44332009-09-09 15:08:12 +0000712
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000713 II->setIsFromAST();
Douglas Gregor668c1a42009-04-21 22:25:48 +0000714 return II;
715 }
716};
Mike Stump1eb44332009-09-09 15:08:12 +0000717
718} // end anonymous namespace
Douglas Gregor668c1a42009-04-21 22:25:48 +0000719
720/// \brief The on-disk hash table used to contain information about
721/// all of the identifiers in the program.
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000722typedef OnDiskChainedHashTable<ASTIdentifierLookupTrait>
723 ASTIdentifierLookupTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000724
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000725namespace {
726class ASTDeclContextNameLookupTrait {
727 ASTReader &Reader;
728
729public:
730 /// \brief Pair of begin/end iterators for DeclIDs.
731 typedef std::pair<DeclID *, DeclID *> data_type;
732
733 /// \brief Special internal key for declaration names.
734 /// The hash table creates keys for comparison; we do not create
735 /// a DeclarationName for the internal key to avoid deserializing types.
736 struct DeclNameKey {
737 DeclarationName::NameKind Kind;
738 uint64_t Data;
739 DeclNameKey() : Kind((DeclarationName::NameKind)0), Data(0) { }
740 };
741
742 typedef DeclarationName external_key_type;
743 typedef DeclNameKey internal_key_type;
744
745 explicit ASTDeclContextNameLookupTrait(ASTReader &Reader) : Reader(Reader) { }
746
747 static bool EqualKey(const internal_key_type& a,
748 const internal_key_type& b) {
749 return a.Kind == b.Kind && a.Data == b.Data;
750 }
751
752 unsigned ComputeHash(const DeclNameKey &Key) const {
753 llvm::FoldingSetNodeID ID;
754 ID.AddInteger(Key.Kind);
755
756 switch (Key.Kind) {
757 case DeclarationName::Identifier:
758 case DeclarationName::CXXLiteralOperatorName:
759 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
760 break;
761 case DeclarationName::ObjCZeroArgSelector:
762 case DeclarationName::ObjCOneArgSelector:
763 case DeclarationName::ObjCMultiArgSelector:
764 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
765 break;
766 case DeclarationName::CXXConstructorName:
767 case DeclarationName::CXXDestructorName:
768 case DeclarationName::CXXConversionFunctionName:
769 ID.AddInteger((TypeID)Key.Data);
770 break;
771 case DeclarationName::CXXOperatorName:
772 ID.AddInteger((OverloadedOperatorKind)Key.Data);
773 break;
774 case DeclarationName::CXXUsingDirective:
775 break;
776 }
777
778 return ID.ComputeHash();
779 }
780
781 internal_key_type GetInternalKey(const external_key_type& Name) const {
782 DeclNameKey Key;
783 Key.Kind = Name.getNameKind();
784 switch (Name.getNameKind()) {
785 case DeclarationName::Identifier:
786 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
787 break;
788 case DeclarationName::ObjCZeroArgSelector:
789 case DeclarationName::ObjCOneArgSelector:
790 case DeclarationName::ObjCMultiArgSelector:
791 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
792 break;
793 case DeclarationName::CXXConstructorName:
794 case DeclarationName::CXXDestructorName:
795 case DeclarationName::CXXConversionFunctionName:
796 Key.Data = Reader.GetTypeID(Name.getCXXNameType());
797 break;
798 case DeclarationName::CXXOperatorName:
799 Key.Data = Name.getCXXOverloadedOperator();
800 break;
801 case DeclarationName::CXXLiteralOperatorName:
802 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
803 break;
804 case DeclarationName::CXXUsingDirective:
805 break;
806 }
Michael J. Spencer20249a12010-10-21 03:16:25 +0000807
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000808 return Key;
809 }
810
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +0000811 external_key_type GetExternalKey(const internal_key_type& Key) const {
812 ASTContext *Context = Reader.getContext();
813 switch (Key.Kind) {
814 case DeclarationName::Identifier:
815 return DeclarationName((IdentifierInfo*)Key.Data);
816
817 case DeclarationName::ObjCZeroArgSelector:
818 case DeclarationName::ObjCOneArgSelector:
819 case DeclarationName::ObjCMultiArgSelector:
820 return DeclarationName(Selector(Key.Data));
821
822 case DeclarationName::CXXConstructorName:
823 return Context->DeclarationNames.getCXXConstructorName(
824 Context->getCanonicalType(Reader.GetType(Key.Data)));
825
826 case DeclarationName::CXXDestructorName:
827 return Context->DeclarationNames.getCXXDestructorName(
828 Context->getCanonicalType(Reader.GetType(Key.Data)));
829
830 case DeclarationName::CXXConversionFunctionName:
831 return Context->DeclarationNames.getCXXConversionFunctionName(
832 Context->getCanonicalType(Reader.GetType(Key.Data)));
833
834 case DeclarationName::CXXOperatorName:
835 return Context->DeclarationNames.getCXXOperatorName(
836 (OverloadedOperatorKind)Key.Data);
837
838 case DeclarationName::CXXLiteralOperatorName:
839 return Context->DeclarationNames.getCXXLiteralOperatorName(
840 (IdentifierInfo*)Key.Data);
841
842 case DeclarationName::CXXUsingDirective:
843 return DeclarationName::getUsingDirectiveName();
844 }
845
846 llvm_unreachable("Invalid Name Kind ?");
847 }
848
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000849 static std::pair<unsigned, unsigned>
850 ReadKeyDataLength(const unsigned char*& d) {
851 using namespace clang::io;
852 unsigned KeyLen = ReadUnalignedLE16(d);
853 unsigned DataLen = ReadUnalignedLE16(d);
854 return std::make_pair(KeyLen, DataLen);
855 }
856
857 internal_key_type ReadKey(const unsigned char* d, unsigned) {
858 using namespace clang::io;
859
860 DeclNameKey Key;
861 Key.Kind = (DeclarationName::NameKind)*d++;
862 switch (Key.Kind) {
863 case DeclarationName::Identifier:
864 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
865 break;
866 case DeclarationName::ObjCZeroArgSelector:
867 case DeclarationName::ObjCOneArgSelector:
868 case DeclarationName::ObjCMultiArgSelector:
Michael J. Spencer20249a12010-10-21 03:16:25 +0000869 Key.Data =
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000870 (uint64_t)Reader.DecodeSelector(ReadUnalignedLE32(d)).getAsOpaquePtr();
871 break;
872 case DeclarationName::CXXConstructorName:
873 case DeclarationName::CXXDestructorName:
874 case DeclarationName::CXXConversionFunctionName:
875 Key.Data = ReadUnalignedLE32(d); // TypeID
876 break;
877 case DeclarationName::CXXOperatorName:
878 Key.Data = *d++; // OverloadedOperatorKind
879 break;
880 case DeclarationName::CXXLiteralOperatorName:
881 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
882 break;
883 case DeclarationName::CXXUsingDirective:
884 break;
885 }
Michael J. Spencer20249a12010-10-21 03:16:25 +0000886
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000887 return Key;
888 }
889
890 data_type ReadData(internal_key_type, const unsigned char* d,
891 unsigned DataLen) {
892 using namespace clang::io;
893 unsigned NumDecls = ReadUnalignedLE16(d);
894 DeclID *Start = (DeclID *)d;
895 return std::make_pair(Start, Start + NumDecls);
896 }
897};
898
899} // end anonymous namespace
900
901/// \brief The on-disk hash table used for the DeclContext's Name lookup table.
902typedef OnDiskChainedHashTable<ASTDeclContextNameLookupTrait>
903 ASTDeclContextNameLookupTable;
904
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000905bool ASTReader::ReadDeclContextStorage(llvm::BitstreamCursor &Cursor,
906 const std::pair<uint64_t, uint64_t> &Offsets,
907 DeclContextInfo &Info) {
908 SavedStreamPosition SavedPosition(Cursor);
909 // First the lexical decls.
910 if (Offsets.first != 0) {
911 Cursor.JumpToBit(Offsets.first);
912
913 RecordData Record;
914 const char *Blob;
915 unsigned BlobLen;
916 unsigned Code = Cursor.ReadCode();
917 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
918 if (RecCode != DECL_CONTEXT_LEXICAL) {
919 Error("Expected lexical block");
920 return true;
921 }
922
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000923 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob);
924 Info.NumLexicalDecls = BlobLen / sizeof(KindDeclIDPair);
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000925 } else {
926 Info.LexicalDecls = 0;
927 Info.NumLexicalDecls = 0;
928 }
929
930 // Now the lookup table.
931 if (Offsets.second != 0) {
932 Cursor.JumpToBit(Offsets.second);
933
934 RecordData Record;
935 const char *Blob;
936 unsigned BlobLen;
937 unsigned Code = Cursor.ReadCode();
938 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
939 if (RecCode != DECL_CONTEXT_VISIBLE) {
940 Error("Expected visible lookup table block");
941 return true;
942 }
943 Info.NameLookupTableData
944 = ASTDeclContextNameLookupTable::Create(
945 (const unsigned char *)Blob + Record[0],
946 (const unsigned char *)Blob,
947 ASTDeclContextNameLookupTrait(*this));
Sebastian Redl0ea8f7f2010-08-24 00:50:00 +0000948 } else {
949 Info.NameLookupTableData = 0;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000950 }
951
952 return false;
953}
954
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000955void ASTReader::Error(const char *Msg) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000956 Diag(diag::err_fe_pch_malformed) << Msg;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000957}
958
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000959/// \brief Tell the AST listener about the predefines buffers in the chain.
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000960bool ASTReader::CheckPredefinesBuffers() {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000961 if (Listener)
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000962 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000963 ActualOriginalFileName,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000964 SuggestedPredefines);
Douglas Gregore721f952009-04-28 18:58:38 +0000965 return false;
Douglas Gregore1d918e2009-04-10 23:10:45 +0000966}
967
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000968//===----------------------------------------------------------------------===//
969// Source Manager Deserialization
970//===----------------------------------------------------------------------===//
971
Douglas Gregorbd945002009-04-13 16:31:14 +0000972/// \brief Read the line table in the source manager block.
Sebastian Redlc3632732010-10-05 15:59:54 +0000973/// \returns true if there was an error.
974bool ASTReader::ParseLineTable(PerFileData &F,
975 llvm::SmallVectorImpl<uint64_t> &Record) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000976 unsigned Idx = 0;
977 LineTableInfo &LineTable = SourceMgr.getLineTable();
978
979 // Parse the file names
Douglas Gregorff0a9872009-04-13 17:12:42 +0000980 std::map<int, int> FileIDs;
981 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000982 // Extract the file name
983 unsigned FilenameLen = Record[Idx++];
984 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
985 Idx += FilenameLen;
Douglas Gregore650c8c2009-07-07 00:12:59 +0000986 MaybeAddSystemRootToFilename(Filename);
Mike Stump1eb44332009-09-09 15:08:12 +0000987 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
Douglas Gregorff0a9872009-04-13 17:12:42 +0000988 Filename.size());
Douglas Gregorbd945002009-04-13 16:31:14 +0000989 }
990
991 // Parse the line entries
992 std::vector<LineEntry> Entries;
993 while (Idx < Record.size()) {
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000994 int FID = Record[Idx++];
Douglas Gregorbd945002009-04-13 16:31:14 +0000995
996 // Extract the line entries
997 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000998 assert(NumEntries && "Numentries is 00000");
Douglas Gregorbd945002009-04-13 16:31:14 +0000999 Entries.clear();
1000 Entries.reserve(NumEntries);
1001 for (unsigned I = 0; I != NumEntries; ++I) {
1002 unsigned FileOffset = Record[Idx++];
1003 unsigned LineNo = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +00001004 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump1eb44332009-09-09 15:08:12 +00001005 SrcMgr::CharacteristicKind FileKind
Douglas Gregorbd945002009-04-13 16:31:14 +00001006 = (SrcMgr::CharacteristicKind)Record[Idx++];
1007 unsigned IncludeOffset = Record[Idx++];
1008 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1009 FileKind, IncludeOffset));
1010 }
1011 LineTable.AddEntry(FID, Entries);
1012 }
1013
1014 return false;
1015}
1016
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001017namespace {
1018
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001019class ASTStatData {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001020public:
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001021 const ino_t ino;
1022 const dev_t dev;
1023 const mode_t mode;
1024 const time_t mtime;
1025 const off_t size;
Mike Stump1eb44332009-09-09 15:08:12 +00001026
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001027 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Chris Lattner74e976b2010-11-23 19:28:12 +00001028 : ino(i), dev(d), mode(mo), mtime(m), size(s) {}
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001029};
1030
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001031class ASTStatLookupTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001032 public:
1033 typedef const char *external_key_type;
1034 typedef const char *internal_key_type;
1035
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001036 typedef ASTStatData data_type;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001037
1038 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001039 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001040 }
1041
1042 static internal_key_type GetInternalKey(const char *path) { return path; }
1043
1044 static bool EqualKey(internal_key_type a, internal_key_type b) {
1045 return strcmp(a, b) == 0;
1046 }
1047
1048 static std::pair<unsigned, unsigned>
1049 ReadKeyDataLength(const unsigned char*& d) {
1050 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1051 unsigned DataLen = (unsigned) *d++;
1052 return std::make_pair(KeyLen + 1, DataLen);
1053 }
1054
1055 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
1056 return (const char *)d;
1057 }
1058
1059 static data_type ReadData(const internal_key_type, const unsigned char *d,
1060 unsigned /*DataLen*/) {
1061 using namespace clang::io;
1062
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001063 ino_t ino = (ino_t) ReadUnalignedLE32(d);
1064 dev_t dev = (dev_t) ReadUnalignedLE32(d);
1065 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump1eb44332009-09-09 15:08:12 +00001066 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001067 off_t size = (off_t) ReadUnalignedLE64(d);
1068 return data_type(ino, dev, mode, mtime, size);
1069 }
1070};
1071
1072/// \brief stat() cache for precompiled headers.
1073///
1074/// This cache is very similar to the stat cache used by pretokenized
1075/// headers.
Chris Lattner10e286a2010-11-23 19:19:34 +00001076class ASTStatCache : public FileSystemStatCache {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001077 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001078 CacheTy *Cache;
1079
1080 unsigned &NumStatHits, &NumStatMisses;
Mike Stump1eb44332009-09-09 15:08:12 +00001081public:
Chris Lattner74e976b2010-11-23 19:28:12 +00001082 ASTStatCache(const unsigned char *Buckets, const unsigned char *Base,
1083 unsigned &NumStatHits, unsigned &NumStatMisses)
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001084 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
1085 Cache = CacheTy::Create(Buckets, Base);
1086 }
1087
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001088 ~ASTStatCache() { delete Cache; }
Mike Stump1eb44332009-09-09 15:08:12 +00001089
Chris Lattner898a0612010-11-23 21:17:56 +00001090 LookupResult getStat(const char *Path, struct stat &StatBuf,
1091 int *FileDescriptor) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001092 // Do the lookup for the file's data in the AST file.
Chris Lattner10e286a2010-11-23 19:19:34 +00001093 CacheTy::iterator I = Cache->find(Path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001094
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001095 // If we don't get a hit in the AST file just forward to 'stat'.
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001096 if (I == Cache->end()) {
1097 ++NumStatMisses;
Chris Lattner898a0612010-11-23 21:17:56 +00001098 return statChained(Path, StatBuf, FileDescriptor);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001099 }
Mike Stump1eb44332009-09-09 15:08:12 +00001100
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001101 ++NumStatHits;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001102 ASTStatData Data = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001103
Chris Lattner10e286a2010-11-23 19:19:34 +00001104 StatBuf.st_ino = Data.ino;
1105 StatBuf.st_dev = Data.dev;
1106 StatBuf.st_mtime = Data.mtime;
1107 StatBuf.st_mode = Data.mode;
1108 StatBuf.st_size = Data.size;
Chris Lattnerd6f61112010-11-23 20:05:15 +00001109 return CacheExists;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001110 }
1111};
1112} // end anonymous namespace
1113
1114
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001115/// \brief Read a source manager block
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001116ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(PerFileData &F) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001117 using namespace SrcMgr;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001118
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001119 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001120
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001121 // Set the source-location entry cursor to the current position in
1122 // the stream. This cursor will be used to read the contents of the
1123 // source manager block initially, and then lazily read
1124 // source-location entries as needed.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001125 SLocEntryCursor = F.Stream;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001126
1127 // The stream itself is going to skip over the source manager block.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001128 if (F.Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001129 Error("malformed block record in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001130 return Failure;
1131 }
1132
1133 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001134 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001135 Error("malformed source manager block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001136 return Failure;
1137 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001138
Douglas Gregor14f79002009-04-10 03:52:48 +00001139 RecordData Record;
1140 while (true) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001141 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregor14f79002009-04-10 03:52:48 +00001142 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001143 if (SLocEntryCursor.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001144 Error("error at end of Source Manager block in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001145 return Failure;
1146 }
Douglas Gregore1d918e2009-04-10 23:10:45 +00001147 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +00001148 }
Mike Stump1eb44332009-09-09 15:08:12 +00001149
Douglas Gregor14f79002009-04-10 03:52:48 +00001150 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1151 // No known subblocks, always skip them.
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001152 SLocEntryCursor.ReadSubBlockID();
1153 if (SLocEntryCursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001154 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001155 return Failure;
1156 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001157 continue;
1158 }
Mike Stump1eb44332009-09-09 15:08:12 +00001159
Douglas Gregor14f79002009-04-10 03:52:48 +00001160 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001161 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregor14f79002009-04-10 03:52:48 +00001162 continue;
1163 }
Mike Stump1eb44332009-09-09 15:08:12 +00001164
Douglas Gregor14f79002009-04-10 03:52:48 +00001165 // Read a record.
1166 const char *BlobStart;
1167 unsigned BlobLen;
1168 Record.clear();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001169 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001170 default: // Default behavior: ignore.
1171 break;
1172
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001173 case SM_LINE_TABLE:
Sebastian Redlc3632732010-10-05 15:59:54 +00001174 if (ParseLineTable(F, Record))
Douglas Gregorbd945002009-04-13 16:31:14 +00001175 return Failure;
Chris Lattner2c78b872009-04-14 23:22:57 +00001176 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001177
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001178 case SM_SLOC_FILE_ENTRY:
1179 case SM_SLOC_BUFFER_ENTRY:
1180 case SM_SLOC_INSTANTIATION_ENTRY:
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001181 // Once we hit one of the source location entries, we're done.
1182 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +00001183 }
1184 }
1185}
1186
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001187/// \brief If a header file is not found at the path that we expect it to be
1188/// and the PCH file was moved from its original location, try to resolve the
1189/// file by assuming that header+PCH were moved together and the header is in
1190/// the same place relative to the PCH.
1191static std::string
1192resolveFileRelativeToOriginalDir(const std::string &Filename,
1193 const std::string &OriginalDir,
1194 const std::string &CurrDir) {
1195 assert(OriginalDir != CurrDir &&
1196 "No point trying to resolve the file if the PCH dir didn't change");
1197 using namespace llvm::sys;
1198 llvm::SmallString<128> filePath(Filename);
1199 fs::make_absolute(filePath);
1200 assert(path::is_absolute(OriginalDir));
1201 llvm::SmallString<128> currPCHPath(CurrDir);
1202
1203 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1204 fileDirE = path::end(path::parent_path(filePath));
1205 path::const_iterator origDirI = path::begin(OriginalDir),
1206 origDirE = path::end(OriginalDir);
1207 // Skip the common path components from filePath and OriginalDir.
1208 while (fileDirI != fileDirE && origDirI != origDirE &&
1209 *fileDirI == *origDirI) {
1210 ++fileDirI;
1211 ++origDirI;
1212 }
1213 for (; origDirI != origDirE; ++origDirI)
1214 path::append(currPCHPath, "..");
1215 path::append(currPCHPath, fileDirI, fileDirE);
1216 path::append(currPCHPath, path::filename(Filename));
1217 return currPCHPath.str();
1218}
1219
Sebastian Redl190faf72010-07-20 21:50:20 +00001220/// \brief Get a cursor that's correctly positioned for reading the source
1221/// location entry with the given ID.
Sebastian Redlc3632732010-10-05 15:59:54 +00001222ASTReader::PerFileData *ASTReader::SLocCursorForID(unsigned ID) {
Sebastian Redl190faf72010-07-20 21:50:20 +00001223 assert(ID != 0 && ID <= TotalNumSLocEntries &&
1224 "SLocCursorForID should only be called for real IDs.");
1225
1226 ID -= 1;
1227 PerFileData *F = 0;
1228 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1229 F = Chain[N - I - 1];
1230 if (ID < F->LocalNumSLocEntries)
1231 break;
1232 ID -= F->LocalNumSLocEntries;
1233 }
1234 assert(F && F->LocalNumSLocEntries > ID && "Chain corrupted");
1235
1236 F->SLocEntryCursor.JumpToBit(F->SLocOffsets[ID]);
Sebastian Redlc3632732010-10-05 15:59:54 +00001237 return F;
Sebastian Redl190faf72010-07-20 21:50:20 +00001238}
1239
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001240/// \brief Read in the source location entry with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001241ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(unsigned ID) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001242 if (ID == 0)
1243 return Success;
1244
1245 if (ID > TotalNumSLocEntries) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001246 Error("source location entry ID out-of-range for AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001247 return Failure;
1248 }
1249
Sebastian Redlc3632732010-10-05 15:59:54 +00001250 PerFileData *F = SLocCursorForID(ID);
1251 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001252
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001253 ++NumSLocEntriesRead;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001254 unsigned Code = SLocEntryCursor.ReadCode();
1255 if (Code == llvm::bitc::END_BLOCK ||
1256 Code == llvm::bitc::ENTER_SUBBLOCK ||
1257 Code == llvm::bitc::DEFINE_ABBREV) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001258 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001259 return Failure;
1260 }
1261
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001262 RecordData Record;
1263 const char *BlobStart;
1264 unsigned BlobLen;
1265 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1266 default:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001267 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001268 return Failure;
1269
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001270 case SM_SLOC_FILE_ENTRY: {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001271 std::string Filename(BlobStart, BlobStart + BlobLen);
1272 MaybeAddSystemRootToFilename(Filename);
Chris Lattner39b49bc2010-11-23 08:35:12 +00001273 const FileEntry *File = FileMgr.getFile(Filename);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001274 if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() &&
1275 OriginalDir != CurrentDir) {
1276 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1277 OriginalDir,
1278 CurrentDir);
1279 if (!resolved.empty())
1280 File = FileMgr.getFile(resolved);
1281 }
Axel Naumann04331162011-01-27 10:55:51 +00001282 if (File == 0)
1283 File = FileMgr.getVirtualFile(Filename, (off_t)Record[4],
1284 (time_t)Record[5]);
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001285 if (File == 0) {
1286 std::string ErrorStr = "could not find file '";
Douglas Gregore650c8c2009-07-07 00:12:59 +00001287 ErrorStr += Filename;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001288 ErrorStr += "' referenced by AST file";
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001289 Error(ErrorStr.c_str());
1290 return Failure;
1291 }
Mike Stump1eb44332009-09-09 15:08:12 +00001292
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001293 if (Record.size() < 6) {
Ted Kremenek1857f622010-03-18 21:23:05 +00001294 Error("source location entry is incorrect");
1295 return Failure;
1296 }
1297
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001298 if (!DisableValidation &&
1299 ((off_t)Record[4] != File->getSize()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001300#if !defined(LLVM_ON_WIN32)
1301 // In our regression testing, the Windows file system seems to
1302 // have inconsistent modification times that sometimes
1303 // erroneously trigger this error-handling path.
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001304 || (time_t)Record[5] != File->getModificationTime()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001305#endif
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001306 )) {
Douglas Gregor2d52be52010-03-21 22:49:54 +00001307 Diag(diag::err_fe_pch_file_modified)
1308 << Filename;
1309 return Failure;
1310 }
1311
Chris Lattner75dfb652010-11-23 09:19:42 +00001312 FileID FID = SourceMgr.createFileID(File, ReadSourceLocation(*F, Record[1]),
1313 (SrcMgr::CharacteristicKind)Record[2],
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001314 ID, Record[0]);
1315 if (Record[3])
1316 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile())
1317 .setHasLineDirectives();
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001318
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001319 break;
1320 }
1321
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001322 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001323 const char *Name = BlobStart;
1324 unsigned Offset = Record[0];
1325 unsigned Code = SLocEntryCursor.ReadCode();
1326 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001327 unsigned RecCode
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001328 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001329
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001330 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001331 Error("AST record has invalid code");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001332 return Failure;
1333 }
1334
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001335 llvm::MemoryBuffer *Buffer
Chris Lattnera0a270c2010-04-05 22:42:27 +00001336 = llvm::MemoryBuffer::getMemBuffer(llvm::StringRef(BlobStart, BlobLen - 1),
1337 Name);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001338 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID, Offset);
Mike Stump1eb44332009-09-09 15:08:12 +00001339
Douglas Gregor92b059e2009-04-28 20:33:11 +00001340 if (strcmp(Name, "<built-in>") == 0) {
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +00001341 PCHPredefinesBlock Block = {
1342 BufferID,
1343 llvm::StringRef(BlobStart, BlobLen - 1)
1344 };
1345 PCHPredefinesBuffers.push_back(Block);
Douglas Gregor92b059e2009-04-28 20:33:11 +00001346 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001347
1348 break;
1349 }
1350
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001351 case SM_SLOC_INSTANTIATION_ENTRY: {
Sebastian Redlc3632732010-10-05 15:59:54 +00001352 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001353 SourceMgr.createInstantiationLoc(SpellingLoc,
Sebastian Redlc3632732010-10-05 15:59:54 +00001354 ReadSourceLocation(*F, Record[2]),
1355 ReadSourceLocation(*F, Record[3]),
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001356 Record[4],
1357 ID,
1358 Record[0]);
1359 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001360 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001361 }
1362
1363 return Success;
1364}
1365
Chris Lattner6367f6d2009-04-27 01:05:14 +00001366/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1367/// specified cursor. Read the abbreviations that are at the top of the block
1368/// and then leave the cursor pointing into the block.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001369bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattner6367f6d2009-04-27 01:05:14 +00001370 unsigned BlockID) {
1371 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001372 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001373 return Failure;
1374 }
Mike Stump1eb44332009-09-09 15:08:12 +00001375
Chris Lattner6367f6d2009-04-27 01:05:14 +00001376 while (true) {
Douglas Gregorecdcb882010-10-20 22:00:55 +00001377 uint64_t Offset = Cursor.GetCurrentBitNo();
Chris Lattner6367f6d2009-04-27 01:05:14 +00001378 unsigned Code = Cursor.ReadCode();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001379
Chris Lattner6367f6d2009-04-27 01:05:14 +00001380 // We expect all abbrevs to be at the start of the block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001381 if (Code != llvm::bitc::DEFINE_ABBREV) {
1382 Cursor.JumpToBit(Offset);
Chris Lattner6367f6d2009-04-27 01:05:14 +00001383 return false;
Douglas Gregorecdcb882010-10-20 22:00:55 +00001384 }
Chris Lattner6367f6d2009-04-27 01:05:14 +00001385 Cursor.ReadAbbrevRecord();
1386 }
1387}
1388
Douglas Gregor89d99802010-11-30 06:16:57 +00001389PreprocessedEntity *ASTReader::ReadMacroRecord(PerFileData &F, uint64_t Offset) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001390 assert(PP && "Forgot to set Preprocessor ?");
Douglas Gregorecdcb882010-10-20 22:00:55 +00001391 llvm::BitstreamCursor &Stream = F.MacroCursor;
Mike Stump1eb44332009-09-09 15:08:12 +00001392
Douglas Gregor37e26842009-04-21 23:56:24 +00001393 // Keep track of where we are in the stream, then jump back there
1394 // after reading this macro.
1395 SavedStreamPosition SavedPosition(Stream);
1396
1397 Stream.JumpToBit(Offset);
1398 RecordData Record;
1399 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1400 MacroInfo *Macro = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001401
Douglas Gregor37e26842009-04-21 23:56:24 +00001402 while (true) {
1403 unsigned Code = Stream.ReadCode();
1404 switch (Code) {
1405 case llvm::bitc::END_BLOCK:
Douglas Gregor89d99802010-11-30 06:16:57 +00001406 return 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001407
1408 case llvm::bitc::ENTER_SUBBLOCK:
1409 // No known subblocks, always skip them.
1410 Stream.ReadSubBlockID();
1411 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001412 Error("malformed block record in AST file");
Douglas Gregor89d99802010-11-30 06:16:57 +00001413 return 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001414 }
1415 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001416
Douglas Gregor37e26842009-04-21 23:56:24 +00001417 case llvm::bitc::DEFINE_ABBREV:
1418 Stream.ReadAbbrevRecord();
1419 continue;
1420 default: break;
1421 }
1422
1423 // Read a record.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001424 const char *BlobStart = 0;
1425 unsigned BlobLen = 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001426 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001427 PreprocessorRecordTypes RecType =
Michael J. Spencer20249a12010-10-21 03:16:25 +00001428 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart,
Douglas Gregorecdcb882010-10-20 22:00:55 +00001429 BlobLen);
Douglas Gregor37e26842009-04-21 23:56:24 +00001430 switch (RecType) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001431 case PP_MACRO_OBJECT_LIKE:
1432 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001433 // If we already have a macro, that means that we've hit the end
1434 // of the definition of the macro we were looking for. We're
1435 // done.
1436 if (Macro)
Douglas Gregor89d99802010-11-30 06:16:57 +00001437 return 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001438
1439 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1440 if (II == 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001441 Error("macro must have a name in AST file");
Douglas Gregor89d99802010-11-30 06:16:57 +00001442 return 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001443 }
Sebastian Redlc3632732010-10-05 15:59:54 +00001444 SourceLocation Loc = ReadSourceLocation(F, Record[1]);
Douglas Gregor37e26842009-04-21 23:56:24 +00001445 bool isUsed = Record[2];
Mike Stump1eb44332009-09-09 15:08:12 +00001446
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001447 MacroInfo *MI = PP->AllocateMacroInfo(Loc);
Douglas Gregor37e26842009-04-21 23:56:24 +00001448 MI->setIsUsed(isUsed);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001449 MI->setIsFromAST();
Mike Stump1eb44332009-09-09 15:08:12 +00001450
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001451 unsigned NextIndex = 3;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001452 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregor37e26842009-04-21 23:56:24 +00001453 // Decode function-like macro info.
1454 bool isC99VarArgs = Record[3];
1455 bool isGNUVarArgs = Record[4];
1456 MacroArgs.clear();
1457 unsigned NumArgs = Record[5];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001458 NextIndex = 6 + NumArgs;
Douglas Gregor37e26842009-04-21 23:56:24 +00001459 for (unsigned i = 0; i != NumArgs; ++i)
1460 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1461
1462 // Install function-like macro info.
1463 MI->setIsFunctionLike();
1464 if (isC99VarArgs) MI->setIsC99Varargs();
1465 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor75fdb232009-05-22 22:45:36 +00001466 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001467 PP->getPreprocessorAllocator());
Douglas Gregor37e26842009-04-21 23:56:24 +00001468 }
1469
1470 // Finally, install the macro.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001471 PP->setMacroInfo(II, MI);
Douglas Gregor37e26842009-04-21 23:56:24 +00001472
1473 // Remember that we saw this macro last so that we add the tokens that
1474 // form its body to it.
1475 Macro = MI;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001476
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001477 if (NextIndex + 1 == Record.size() && PP->getPreprocessingRecord()) {
1478 // We have a macro definition. Load it now.
1479 PP->getPreprocessingRecord()->RegisterMacroDefinition(Macro,
1480 getMacroDefinition(Record[NextIndex]));
1481 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001482
Douglas Gregor37e26842009-04-21 23:56:24 +00001483 ++NumMacrosRead;
1484 break;
1485 }
Mike Stump1eb44332009-09-09 15:08:12 +00001486
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001487 case PP_TOKEN: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001488 // If we see a TOKEN before a PP_MACRO_*, then the file is
1489 // erroneous, just pretend we didn't see this.
1490 if (Macro == 0) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001491
Douglas Gregor37e26842009-04-21 23:56:24 +00001492 Token Tok;
1493 Tok.startToken();
Sebastian Redlc3632732010-10-05 15:59:54 +00001494 Tok.setLocation(ReadSourceLocation(F, Record[0]));
Douglas Gregor37e26842009-04-21 23:56:24 +00001495 Tok.setLength(Record[1]);
1496 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1497 Tok.setIdentifierInfo(II);
1498 Tok.setKind((tok::TokenKind)Record[3]);
1499 Tok.setFlag((Token::TokenFlags)Record[4]);
1500 Macro->AddTokenToBody(Tok);
1501 break;
1502 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001503 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001504 }
Douglas Gregor89d99802010-11-30 06:16:57 +00001505
1506 return 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001507}
1508
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001509PreprocessedEntity *ASTReader::LoadPreprocessedEntity(PerFileData &F) {
1510 assert(PP && "Forgot to set Preprocessor ?");
1511 unsigned Code = F.PreprocessorDetailCursor.ReadCode();
1512 switch (Code) {
1513 case llvm::bitc::END_BLOCK:
1514 return 0;
1515
1516 case llvm::bitc::ENTER_SUBBLOCK:
1517 Error("unexpected subblock record in preprocessor detail block");
1518 return 0;
1519
1520 case llvm::bitc::DEFINE_ABBREV:
1521 Error("unexpected abbrevation record in preprocessor detail block");
1522 return 0;
1523
1524 default:
1525 break;
1526 }
1527
1528 if (!PP->getPreprocessingRecord()) {
1529 Error("no preprocessing record");
1530 return 0;
1531 }
1532
1533 // Read the record.
1534 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1535 const char *BlobStart = 0;
1536 unsigned BlobLen = 0;
1537 RecordData Record;
1538 PreprocessorDetailRecordTypes RecType =
1539 (PreprocessorDetailRecordTypes)F.PreprocessorDetailCursor.ReadRecord(
1540 Code, Record, BlobStart, BlobLen);
1541 switch (RecType) {
1542 case PPD_MACRO_INSTANTIATION: {
1543 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1544 return PE;
1545
1546 MacroInstantiation *MI
1547 = new (PPRec) MacroInstantiation(DecodeIdentifierInfo(Record[3]),
1548 SourceRange(ReadSourceLocation(F, Record[1]),
1549 ReadSourceLocation(F, Record[2])),
1550 getMacroDefinition(Record[4]));
1551 PPRec.SetPreallocatedEntity(Record[0], MI);
1552 return MI;
1553 }
1554
1555 case PPD_MACRO_DEFINITION: {
1556 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1557 return PE;
1558
1559 if (Record[1] > MacroDefinitionsLoaded.size()) {
1560 Error("out-of-bounds macro definition record");
1561 return 0;
1562 }
1563
1564 // Decode the identifier info and then check again; if the macro is
1565 // still defined and associated with the identifier,
1566 IdentifierInfo *II = DecodeIdentifierInfo(Record[4]);
1567 if (!MacroDefinitionsLoaded[Record[1] - 1]) {
1568 MacroDefinition *MD
1569 = new (PPRec) MacroDefinition(II,
1570 ReadSourceLocation(F, Record[5]),
1571 SourceRange(
1572 ReadSourceLocation(F, Record[2]),
1573 ReadSourceLocation(F, Record[3])));
1574
1575 PPRec.SetPreallocatedEntity(Record[0], MD);
1576 MacroDefinitionsLoaded[Record[1] - 1] = MD;
1577
1578 if (DeserializationListener)
1579 DeserializationListener->MacroDefinitionRead(Record[1], MD);
1580 }
1581
1582 return MacroDefinitionsLoaded[Record[1] - 1];
1583 }
1584
1585 case PPD_INCLUSION_DIRECTIVE: {
1586 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1587 return PE;
1588
1589 const char *FullFileNameStart = BlobStart + Record[3];
1590 const FileEntry *File
1591 = PP->getFileManager().getFile(llvm::StringRef(FullFileNameStart,
1592 BlobLen - Record[3]));
1593
1594 // FIXME: Stable encoding
1595 InclusionDirective::InclusionKind Kind
1596 = static_cast<InclusionDirective::InclusionKind>(Record[5]);
1597 InclusionDirective *ID
1598 = new (PPRec) InclusionDirective(PPRec, Kind,
1599 llvm::StringRef(BlobStart, Record[3]),
1600 Record[4],
1601 File,
1602 SourceRange(ReadSourceLocation(F, Record[1]),
1603 ReadSourceLocation(F, Record[2])));
1604 PPRec.SetPreallocatedEntity(Record[0], ID);
1605 return ID;
1606 }
1607 }
1608
1609 Error("invalid offset in preprocessor detail block");
1610 return 0;
1611}
1612
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001613namespace {
1614 /// \brief Trait class used to search the on-disk hash table containing all of
1615 /// the header search information.
1616 ///
1617 /// The on-disk hash table contains a mapping from each header path to
1618 /// information about that header (how many times it has been included, its
1619 /// controlling macro, etc.). Note that we actually hash based on the
1620 /// filename, and support "deep" comparisons of file names based on current
1621 /// inode numbers, so that the search can cope with non-normalized path names
1622 /// and symlinks.
1623 class HeaderFileInfoTrait {
1624 const char *SearchPath;
1625 struct stat SearchPathStatBuf;
1626 llvm::Optional<int> SearchPathStatResult;
1627
1628 int StatSimpleCache(const char *Path, struct stat *StatBuf) {
1629 if (Path == SearchPath) {
1630 if (!SearchPathStatResult)
1631 SearchPathStatResult = stat(Path, &SearchPathStatBuf);
1632
1633 *StatBuf = SearchPathStatBuf;
1634 return *SearchPathStatResult;
1635 }
1636
1637 return stat(Path, StatBuf);
1638 }
1639
1640 public:
1641 typedef const char *external_key_type;
1642 typedef const char *internal_key_type;
1643
1644 typedef HeaderFileInfo data_type;
1645
1646 HeaderFileInfoTrait(const char *SearchPath = 0) : SearchPath(SearchPath) { }
1647
1648 static unsigned ComputeHash(const char *path) {
1649 return llvm::HashString(llvm::sys::path::filename(path));
1650 }
1651
1652 static internal_key_type GetInternalKey(const char *path) { return path; }
1653
1654 bool EqualKey(internal_key_type a, internal_key_type b) {
1655 if (strcmp(a, b) == 0)
1656 return true;
1657
1658 if (llvm::sys::path::filename(a) != llvm::sys::path::filename(b))
1659 return false;
1660
1661 // The file names match, but the path names don't. stat() the files to
1662 // see if they are the same.
1663 struct stat StatBufA, StatBufB;
1664 if (StatSimpleCache(a, &StatBufA) || StatSimpleCache(b, &StatBufB))
1665 return false;
1666
1667 return StatBufA.st_ino == StatBufB.st_ino;
1668 }
1669
1670 static std::pair<unsigned, unsigned>
1671 ReadKeyDataLength(const unsigned char*& d) {
1672 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1673 unsigned DataLen = (unsigned) *d++;
1674 return std::make_pair(KeyLen + 1, DataLen);
1675 }
1676
1677 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
1678 return (const char *)d;
1679 }
1680
1681 static data_type ReadData(const internal_key_type, const unsigned char *d,
1682 unsigned DataLen) {
1683 const unsigned char *End = d + DataLen;
1684 using namespace clang::io;
1685 HeaderFileInfo HFI;
1686 unsigned Flags = *d++;
1687 HFI.isImport = (Flags >> 3) & 0x01;
1688 HFI.DirInfo = (Flags >> 1) & 0x03;
1689 HFI.Resolved = Flags & 0x01;
1690 HFI.NumIncludes = ReadUnalignedLE16(d);
1691 HFI.ControllingMacroID = ReadUnalignedLE32(d);
1692 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1693 (void)End;
1694
1695 // This HeaderFileInfo was externally loaded.
1696 HFI.External = true;
1697 return HFI;
1698 }
1699 };
1700}
1701
1702/// \brief The on-disk hash table used for the global method pool.
1703typedef OnDiskChainedHashTable<HeaderFileInfoTrait>
1704 HeaderFileInfoLookupTable;
1705
Douglas Gregor295a2a62010-10-30 00:23:06 +00001706void ASTReader::SetIdentifierIsMacro(IdentifierInfo *II, PerFileData &F,
1707 uint64_t Offset) {
1708 // Note that this identifier has a macro definition.
1709 II->setHasMacroDefinition(true);
1710
1711 // Adjust the offset based on our position in the chain.
1712 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1713 if (Chain[I] == &F)
1714 break;
1715
1716 Offset += Chain[I]->SizeInBits;
1717 }
1718
1719 UnreadMacroRecordOffsets[II] = Offset;
1720}
1721
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001722void ASTReader::ReadDefinedMacros() {
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001723 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redlc3632732010-10-05 15:59:54 +00001724 PerFileData &F = *Chain[N - I - 1];
1725 llvm::BitstreamCursor &MacroCursor = F.MacroCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001726
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001727 // If there was no preprocessor block, skip this file.
1728 if (!MacroCursor.getBitStreamReader())
1729 continue;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001730
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001731 llvm::BitstreamCursor Cursor = MacroCursor;
Douglas Gregorecdcb882010-10-20 22:00:55 +00001732 Cursor.JumpToBit(F.MacroStartOffset);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001733
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001734 RecordData Record;
1735 while (true) {
1736 unsigned Code = Cursor.ReadCode();
Douglas Gregorecdcb882010-10-20 22:00:55 +00001737 if (Code == llvm::bitc::END_BLOCK)
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001738 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001739
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001740 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1741 // No known subblocks, always skip them.
1742 Cursor.ReadSubBlockID();
1743 if (Cursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001744 Error("malformed block record in AST file");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001745 return;
1746 }
1747 continue;
1748 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001749
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001750 if (Code == llvm::bitc::DEFINE_ABBREV) {
1751 Cursor.ReadAbbrevRecord();
1752 continue;
1753 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001754
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001755 // Read a record.
1756 const char *BlobStart;
1757 unsigned BlobLen;
1758 Record.clear();
1759 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1760 default: // Default behavior: ignore.
1761 break;
Douglas Gregor88a35862010-01-04 19:18:44 +00001762
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001763 case PP_MACRO_OBJECT_LIKE:
1764 case PP_MACRO_FUNCTION_LIKE:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001765 DecodeIdentifierInfo(Record[0]);
1766 break;
1767
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001768 case PP_TOKEN:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001769 // Ignore tokens.
1770 break;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001771 }
Douglas Gregor88a35862010-01-04 19:18:44 +00001772 }
1773 }
Douglas Gregor295a2a62010-10-30 00:23:06 +00001774
1775 // Drain the unread macro-record offsets map.
1776 while (!UnreadMacroRecordOffsets.empty())
1777 LoadMacroDefinition(UnreadMacroRecordOffsets.begin());
1778}
1779
1780void ASTReader::LoadMacroDefinition(
1781 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos) {
1782 assert(Pos != UnreadMacroRecordOffsets.end() && "Unknown macro definition");
1783 PerFileData *F = 0;
1784 uint64_t Offset = Pos->second;
1785 UnreadMacroRecordOffsets.erase(Pos);
1786
1787 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1788 if (Offset < Chain[I]->SizeInBits) {
1789 F = Chain[I];
1790 break;
1791 }
1792
1793 Offset -= Chain[I]->SizeInBits;
1794 }
1795 if (!F) {
1796 Error("Malformed macro record offset");
1797 return;
1798 }
1799
1800 ReadMacroRecord(*F, Offset);
1801}
1802
1803void ASTReader::LoadMacroDefinition(IdentifierInfo *II) {
1804 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos
1805 = UnreadMacroRecordOffsets.find(II);
1806 LoadMacroDefinition(Pos);
Douglas Gregor88a35862010-01-04 19:18:44 +00001807}
1808
Sebastian Redlf73c93f2010-09-15 19:54:06 +00001809MacroDefinition *ASTReader::getMacroDefinition(MacroID ID) {
Douglas Gregor77424bc2010-10-02 19:29:26 +00001810 if (ID == 0 || ID > MacroDefinitionsLoaded.size())
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001811 return 0;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001812
Douglas Gregor77424bc2010-10-02 19:29:26 +00001813 if (!MacroDefinitionsLoaded[ID - 1]) {
1814 unsigned Index = ID - 1;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001815 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1816 PerFileData &F = *Chain[N - I - 1];
1817 if (Index < F.LocalNumMacroDefinitions) {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001818 SavedStreamPosition SavedPosition(F.PreprocessorDetailCursor);
1819 F.PreprocessorDetailCursor.JumpToBit(F.MacroDefinitionOffsets[Index]);
1820 LoadPreprocessedEntity(F);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001821 break;
1822 }
1823 Index -= F.LocalNumMacroDefinitions;
1824 }
Douglas Gregor77424bc2010-10-02 19:29:26 +00001825 assert(MacroDefinitionsLoaded[ID - 1] && "Broken chain");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001826 }
1827
Douglas Gregor77424bc2010-10-02 19:29:26 +00001828 return MacroDefinitionsLoaded[ID - 1];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001829}
1830
Douglas Gregore650c8c2009-07-07 00:12:59 +00001831/// \brief If we are loading a relocatable PCH file, and the filename is
1832/// not an absolute path, add the system root to the beginning of the file
1833/// name.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001834void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001835 // If this is not a relocatable PCH file, there's nothing to do.
1836 if (!RelocatablePCH)
1837 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001838
Michael J. Spencer256053b2010-12-17 21:22:22 +00001839 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
Douglas Gregore650c8c2009-07-07 00:12:59 +00001840 return;
1841
Douglas Gregore650c8c2009-07-07 00:12:59 +00001842 if (isysroot == 0) {
1843 // If no system root was given, default to '/'
1844 Filename.insert(Filename.begin(), '/');
1845 return;
1846 }
Mike Stump1eb44332009-09-09 15:08:12 +00001847
Douglas Gregore650c8c2009-07-07 00:12:59 +00001848 unsigned Length = strlen(isysroot);
1849 if (isysroot[Length - 1] != '/')
1850 Filename.insert(Filename.begin(), '/');
Mike Stump1eb44332009-09-09 15:08:12 +00001851
Douglas Gregore650c8c2009-07-07 00:12:59 +00001852 Filename.insert(Filename.begin(), isysroot, isysroot + Length);
1853}
1854
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001855ASTReader::ASTReadResult
Sebastian Redl571db7f2010-08-18 23:56:56 +00001856ASTReader::ReadASTBlock(PerFileData &F) {
Sebastian Redl9137a522010-07-16 17:50:48 +00001857 llvm::BitstreamCursor &Stream = F.Stream;
1858
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001859 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001860 Error("malformed block record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001861 return Failure;
1862 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001863
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001864 // Read all of the records and blocks for the ASt file.
Douglas Gregor8038d512009-04-10 17:25:41 +00001865 RecordData Record;
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001866 bool First = true;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001867 while (!Stream.AtEndOfStream()) {
1868 unsigned Code = Stream.ReadCode();
1869 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001870 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001871 Error("error at end of module block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001872 return Failure;
1873 }
Chris Lattner7356a312009-04-11 21:15:38 +00001874
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001875 return Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001876 }
1877
1878 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1879 switch (Stream.ReadSubBlockID()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001880 case DECLTYPES_BLOCK_ID:
Chris Lattner6367f6d2009-04-27 01:05:14 +00001881 // We lazily load the decls block, but we want to set up the
1882 // DeclsCursor cursor to point into it. Clone our current bitcode
1883 // cursor to it, enter the block and read the abbrevs in that block.
1884 // With the main cursor, we just skip over it.
Sebastian Redl9137a522010-07-16 17:50:48 +00001885 F.DeclsCursor = Stream;
Chris Lattner6367f6d2009-04-27 01:05:14 +00001886 if (Stream.SkipBlock() || // Skip with the main cursor.
1887 // Read the abbrevs.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001888 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001889 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001890 return Failure;
1891 }
1892 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001893
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00001894 case DECL_UPDATES_BLOCK_ID:
1895 if (Stream.SkipBlock()) {
1896 Error("malformed block record in AST file");
1897 return Failure;
1898 }
1899 break;
1900
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001901 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl9137a522010-07-16 17:50:48 +00001902 F.MacroCursor = Stream;
Douglas Gregor88a35862010-01-04 19:18:44 +00001903 if (PP)
1904 PP->setExternalSource(this);
1905
Douglas Gregorecdcb882010-10-20 22:00:55 +00001906 if (Stream.SkipBlock() ||
1907 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001908 Error("malformed block record in AST file");
Chris Lattner7356a312009-04-11 21:15:38 +00001909 return Failure;
1910 }
Douglas Gregorecdcb882010-10-20 22:00:55 +00001911 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
Chris Lattner7356a312009-04-11 21:15:38 +00001912 break;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00001913
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001914 case PREPROCESSOR_DETAIL_BLOCK_ID:
1915 F.PreprocessorDetailCursor = Stream;
1916 if (Stream.SkipBlock() ||
1917 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
1918 PREPROCESSOR_DETAIL_BLOCK_ID)) {
1919 Error("malformed preprocessor detail record in AST file");
1920 return Failure;
1921 }
1922 F.PreprocessorDetailStartOffset
1923 = F.PreprocessorDetailCursor.GetCurrentBitNo();
1924 break;
1925
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001926 case SOURCE_MANAGER_BLOCK_ID:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001927 switch (ReadSourceManagerBlock(F)) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00001928 case Success:
1929 break;
1930
1931 case Failure:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001932 Error("malformed source manager block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001933 return Failure;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001934
1935 case IgnorePCH:
1936 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001937 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001938 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001939 }
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001940 First = false;
Douglas Gregor8038d512009-04-10 17:25:41 +00001941 continue;
1942 }
1943
1944 if (Code == llvm::bitc::DEFINE_ABBREV) {
1945 Stream.ReadAbbrevRecord();
1946 continue;
1947 }
1948
1949 // Read and process a record.
1950 Record.clear();
Douglas Gregor2bec0412009-04-10 21:16:55 +00001951 const char *BlobStart = 0;
1952 unsigned BlobLen = 0;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001953 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00001954 &BlobStart, &BlobLen)) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001955 default: // Default behavior: ignore.
1956 break;
1957
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001958 case METADATA: {
1959 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1960 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001961 : diag::warn_pch_version_too_new);
1962 return IgnorePCH;
1963 }
1964
1965 RelocatablePCH = Record[4];
1966 if (Listener) {
1967 std::string TargetTriple(BlobStart, BlobLen);
1968 if (Listener->ReadTargetTriple(TargetTriple))
1969 return IgnorePCH;
1970 }
1971 break;
1972 }
1973
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001974 case CHAINED_METADATA: {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001975 if (!First) {
1976 Error("CHAINED_METADATA is not first record in block");
1977 return Failure;
1978 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001979 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1980 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001981 : diag::warn_pch_version_too_new);
1982 return IgnorePCH;
1983 }
1984
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00001985 // Load the chained file, which is always a PCH file.
1986 switch(ReadASTCore(llvm::StringRef(BlobStart, BlobLen), PCH)) {
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001987 case Failure: return Failure;
1988 // If we have to ignore the dependency, we'll have to ignore this too.
1989 case IgnorePCH: return IgnorePCH;
1990 case Success: break;
1991 }
1992 break;
1993 }
1994
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001995 case TYPE_OFFSET:
Sebastian Redl12d6da02010-07-19 22:06:55 +00001996 if (F.LocalNumTypes != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001997 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001998 return Failure;
1999 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00002000 F.TypeOffsets = (const uint32_t *)BlobStart;
2001 F.LocalNumTypes = Record[0];
Douglas Gregor8038d512009-04-10 17:25:41 +00002002 break;
2003
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002004 case DECL_OFFSET:
Sebastian Redl12d6da02010-07-19 22:06:55 +00002005 if (F.LocalNumDecls != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002006 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002007 return Failure;
2008 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00002009 F.DeclOffsets = (const uint32_t *)BlobStart;
2010 F.LocalNumDecls = Record[0];
Douglas Gregor8038d512009-04-10 17:25:41 +00002011 break;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002012
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002013 case TU_UPDATE_LEXICAL: {
Sebastian Redld692af72010-07-27 18:24:41 +00002014 DeclContextInfo Info = {
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002015 /* No visible information */ 0,
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002016 reinterpret_cast<const KindDeclIDPair *>(BlobStart),
2017 BlobLen / sizeof(KindDeclIDPair)
Sebastian Redld692af72010-07-27 18:24:41 +00002018 };
Douglas Gregor3747ee72010-10-01 01:18:02 +00002019 DeclContextOffsets[Context ? Context->getTranslationUnitDecl() : 0]
2020 .push_back(Info);
Sebastian Redld692af72010-07-27 18:24:41 +00002021 break;
2022 }
2023
Sebastian Redle1dde812010-08-24 00:50:04 +00002024 case UPDATE_VISIBLE: {
2025 serialization::DeclID ID = Record[0];
2026 void *Table = ASTDeclContextNameLookupTable::Create(
2027 (const unsigned char *)BlobStart + Record[1],
2028 (const unsigned char *)BlobStart,
2029 ASTDeclContextNameLookupTrait(*this));
Douglas Gregor3747ee72010-10-01 01:18:02 +00002030 if (ID == 1 && Context) { // Is it the TU?
Sebastian Redle1dde812010-08-24 00:50:04 +00002031 DeclContextInfo Info = {
2032 Table, /* No lexical inforamtion */ 0, 0
2033 };
2034 DeclContextOffsets[Context->getTranslationUnitDecl()].push_back(Info);
2035 } else
2036 PendingVisibleUpdates[ID].push_back(Table);
2037 break;
2038 }
2039
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002040 case REDECLS_UPDATE_LATEST: {
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00002041 assert(Record.size() % 2 == 0 && "Expected pairs of DeclIDs");
2042 for (unsigned i = 0, e = Record.size(); i < e; i += 2) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002043 DeclID First = Record[i], Latest = Record[i+1];
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00002044 assert((FirstLatestDeclIDs.find(First) == FirstLatestDeclIDs.end() ||
2045 Latest > FirstLatestDeclIDs[First]) &&
2046 "The new latest is supposed to come after the previous latest");
2047 FirstLatestDeclIDs[First] = Latest;
2048 }
2049 break;
2050 }
2051
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002052 case LANGUAGE_OPTIONS:
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00002053 if (ParseLanguageOptions(Record) && !DisableValidation)
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002054 return IgnorePCH;
2055 break;
Douglas Gregor2bec0412009-04-10 21:16:55 +00002056
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002057 case IDENTIFIER_TABLE:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002058 F.IdentifierTableData = BlobStart;
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002059 if (Record[0]) {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002060 F.IdentifierLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002061 = ASTIdentifierLookupTable::Create(
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002062 (const unsigned char *)F.IdentifierTableData + Record[0],
2063 (const unsigned char *)F.IdentifierTableData,
Sebastian Redlc3632732010-10-05 15:59:54 +00002064 ASTIdentifierLookupTrait(*this, F));
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002065 if (PP)
2066 PP->getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002067 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002068 break;
2069
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002070 case IDENTIFIER_OFFSET:
Sebastian Redl2da08f92010-07-19 22:28:42 +00002071 if (F.LocalNumIdentifiers != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002072 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00002073 return Failure;
2074 }
Sebastian Redl2da08f92010-07-19 22:28:42 +00002075 F.IdentifierOffsets = (const uint32_t *)BlobStart;
2076 F.LocalNumIdentifiers = Record[0];
Douglas Gregorafaf3082009-04-11 00:14:32 +00002077 break;
Douglas Gregorfdd01722009-04-14 00:24:19 +00002078
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002079 case EXTERNAL_DEFINITIONS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002080 // Optimization for the first block.
2081 if (ExternalDefinitions.empty())
2082 ExternalDefinitions.swap(Record);
2083 else
2084 ExternalDefinitions.insert(ExternalDefinitions.end(),
2085 Record.begin(), Record.end());
Douglas Gregorfdd01722009-04-14 00:24:19 +00002086 break;
Douglas Gregor3e1af842009-04-17 22:13:46 +00002087
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002088 case SPECIAL_TYPES:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002089 // Optimization for the first block
2090 if (SpecialTypes.empty())
2091 SpecialTypes.swap(Record);
2092 else
2093 SpecialTypes.insert(SpecialTypes.end(), Record.begin(), Record.end());
Douglas Gregorad1de002009-04-18 05:55:16 +00002094 break;
2095
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002096 case STATISTICS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002097 TotalNumStatements += Record[0];
2098 TotalNumMacros += Record[1];
2099 TotalLexicalDeclContexts += Record[2];
2100 TotalVisibleDeclContexts += Record[3];
Douglas Gregor3e1af842009-04-17 22:13:46 +00002101 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002102
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002103 case TENTATIVE_DEFINITIONS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002104 // Optimization for the first block.
2105 if (TentativeDefinitions.empty())
2106 TentativeDefinitions.swap(Record);
2107 else
2108 TentativeDefinitions.insert(TentativeDefinitions.end(),
2109 Record.begin(), Record.end());
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002110 break;
Douglas Gregor14c22f22009-04-22 22:18:58 +00002111
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002112 case UNUSED_FILESCOPED_DECLS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002113 // Optimization for the first block.
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002114 if (UnusedFileScopedDecls.empty())
2115 UnusedFileScopedDecls.swap(Record);
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002116 else
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002117 UnusedFileScopedDecls.insert(UnusedFileScopedDecls.end(),
2118 Record.begin(), Record.end());
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002119 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002120
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002121 case WEAK_UNDECLARED_IDENTIFIERS:
Sebastian Redl40566802010-08-05 18:21:25 +00002122 // Later blocks overwrite earlier ones.
2123 WeakUndeclaredIdentifiers.swap(Record);
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002124 break;
2125
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002126 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002127 // Optimization for the first block.
2128 if (LocallyScopedExternalDecls.empty())
2129 LocallyScopedExternalDecls.swap(Record);
2130 else
2131 LocallyScopedExternalDecls.insert(LocallyScopedExternalDecls.end(),
2132 Record.begin(), Record.end());
Douglas Gregor14c22f22009-04-22 22:18:58 +00002133 break;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002134
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002135 case SELECTOR_OFFSETS:
Sebastian Redl059612d2010-08-03 21:58:15 +00002136 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redl725cd962010-08-04 20:40:17 +00002137 F.LocalNumSelectors = Record[0];
Douglas Gregor83941df2009-04-25 17:48:32 +00002138 break;
2139
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002140 case METHOD_POOL:
Sebastian Redl725cd962010-08-04 20:40:17 +00002141 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor83941df2009-04-25 17:48:32 +00002142 if (Record[0])
Sebastian Redl725cd962010-08-04 20:40:17 +00002143 F.SelectorLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002144 = ASTSelectorLookupTable::Create(
Sebastian Redl725cd962010-08-04 20:40:17 +00002145 F.SelectorLookupTableData + Record[0],
2146 F.SelectorLookupTableData,
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002147 ASTSelectorLookupTrait(*this));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002148 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002149 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002150
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002151 case REFERENCED_SELECTOR_POOL:
Sebastian Redlc3632732010-10-05 15:59:54 +00002152 F.ReferencedSelectorsData.swap(Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002153 break;
2154
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002155 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002156 if (!Record.empty() && Listener)
2157 Listener->ReadCounter(Record[0]);
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002158 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002159
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002160 case SOURCE_LOCATION_OFFSETS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002161 F.SLocOffsets = (const uint32_t *)BlobStart;
2162 F.LocalNumSLocEntries = Record[0];
Sebastian Redl8db9fae2010-09-22 20:19:08 +00002163 F.LocalSLocSize = Record[1];
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002164 break;
2165
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002166 case SOURCE_LOCATION_PRELOADS:
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002167 if (PreloadSLocEntries.empty())
2168 PreloadSLocEntries.swap(Record);
2169 else
2170 PreloadSLocEntries.insert(PreloadSLocEntries.end(),
2171 Record.begin(), Record.end());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002172 break;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002173
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002174 case STAT_CACHE: {
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00002175 if (!DisableStatCache) {
2176 ASTStatCache *MyStatCache =
2177 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
2178 (const unsigned char *)BlobStart,
2179 NumStatHits, NumStatMisses);
2180 FileMgr.addStatCache(MyStatCache);
2181 F.StatCache = MyStatCache;
2182 }
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002183 break;
Douglas Gregor52e71082009-10-16 18:18:30 +00002184 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002185
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002186 case EXT_VECTOR_DECLS:
Sebastian Redla9f23682010-07-28 21:38:49 +00002187 // Optimization for the first block.
2188 if (ExtVectorDecls.empty())
2189 ExtVectorDecls.swap(Record);
2190 else
2191 ExtVectorDecls.insert(ExtVectorDecls.end(),
2192 Record.begin(), Record.end());
Douglas Gregorb81c1702009-04-27 20:06:05 +00002193 break;
2194
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002195 case VTABLE_USES:
Sebastian Redl40566802010-08-05 18:21:25 +00002196 // Later tables overwrite earlier ones.
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002197 VTableUses.swap(Record);
2198 break;
2199
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002200 case DYNAMIC_CLASSES:
Sebastian Redl40566802010-08-05 18:21:25 +00002201 // Optimization for the first block.
2202 if (DynamicClasses.empty())
2203 DynamicClasses.swap(Record);
2204 else
2205 DynamicClasses.insert(DynamicClasses.end(),
2206 Record.begin(), Record.end());
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002207 break;
2208
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002209 case PENDING_IMPLICIT_INSTANTIATIONS:
Sebastian Redlc3632732010-10-05 15:59:54 +00002210 F.PendingInstantiations.swap(Record);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002211 break;
2212
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002213 case SEMA_DECL_REFS:
Sebastian Redl40566802010-08-05 18:21:25 +00002214 // Later tables overwrite earlier ones.
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002215 SemaDeclRefs.swap(Record);
2216 break;
2217
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002218 case ORIGINAL_FILE_NAME:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002219 // The primary AST will be the last to get here, so it will be the one
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002220 // that's used.
Daniel Dunbar7b5a1212009-11-11 05:29:04 +00002221 ActualOriginalFileName.assign(BlobStart, BlobLen);
2222 OriginalFileName = ActualOriginalFileName;
Douglas Gregore650c8c2009-07-07 00:12:59 +00002223 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregorb64c1932009-05-12 01:31:05 +00002224 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002225
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002226 case ORIGINAL_PCH_DIR:
2227 // The primary AST will be the last to get here, so it will be the one
2228 // that's used.
2229 OriginalDir.assign(BlobStart, BlobLen);
2230 break;
2231
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002232 case VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek974be4d2010-02-12 23:31:14 +00002233 const std::string &CurBranch = getClangFullRepositoryVersion();
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002234 llvm::StringRef ASTBranch(BlobStart, BlobLen);
2235 if (llvm::StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2236 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregor445e23e2009-10-05 21:07:28 +00002237 return IgnorePCH;
2238 }
2239 break;
2240 }
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002241
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002242 case MACRO_DEFINITION_OFFSETS:
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002243 F.MacroDefinitionOffsets = (const uint32_t *)BlobStart;
2244 F.NumPreallocatedPreprocessingEntities = Record[0];
2245 F.LocalNumMacroDefinitions = Record[1];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002246 break;
Sebastian Redl0b17c612010-08-13 00:28:03 +00002247
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002248 case DECL_UPDATE_OFFSETS: {
2249 if (Record.size() % 2 != 0) {
2250 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2251 return Failure;
2252 }
2253 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
2254 DeclUpdateOffsets[static_cast<DeclID>(Record[I])]
2255 .push_back(std::make_pair(&F, Record[I+1]));
2256 break;
2257 }
2258
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002259 case DECL_REPLACEMENTS: {
Sebastian Redl0b17c612010-08-13 00:28:03 +00002260 if (Record.size() % 2 != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002261 Error("invalid DECL_REPLACEMENTS block in AST file");
Sebastian Redl0b17c612010-08-13 00:28:03 +00002262 return Failure;
2263 }
2264 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002265 ReplacedDecls[static_cast<DeclID>(Record[I])] =
Sebastian Redl0b17c612010-08-13 00:28:03 +00002266 std::make_pair(&F, Record[I+1]);
2267 break;
2268 }
Douglas Gregor7c789c12010-10-29 22:39:52 +00002269
2270 case CXX_BASE_SPECIFIER_OFFSETS: {
2271 if (F.LocalNumCXXBaseSpecifiers != 0) {
2272 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2273 return Failure;
2274 }
2275
2276 F.LocalNumCXXBaseSpecifiers = Record[0];
2277 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart;
2278 break;
2279 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002280
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002281 case DIAG_PRAGMA_MAPPINGS:
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002282 if (Record.size() % 2 != 0) {
2283 Error("invalid DIAG_USER_MAPPINGS block in AST file");
2284 return Failure;
2285 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002286 if (PragmaDiagMappings.empty())
2287 PragmaDiagMappings.swap(Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002288 else
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002289 PragmaDiagMappings.insert(PragmaDiagMappings.end(),
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002290 Record.begin(), Record.end());
2291 break;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002292
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002293 case CUDA_SPECIAL_DECL_REFS:
2294 // Later tables overwrite earlier ones.
2295 CUDASpecialDeclRefs.swap(Record);
2296 break;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002297
2298 case HEADER_SEARCH_TABLE:
2299 F.HeaderFileInfoTableData = BlobStart;
2300 F.LocalNumHeaderFileInfos = Record[1];
2301 if (Record[0]) {
2302 F.HeaderFileInfoTable
2303 = HeaderFileInfoLookupTable::Create(
2304 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
2305 (const unsigned char *)F.HeaderFileInfoTableData);
2306 if (PP)
2307 PP->getHeaderSearchInfo().SetExternalSource(this);
2308 }
2309 break;
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002310
2311 case FP_PRAGMA_OPTIONS:
2312 // Later tables overwrite earlier ones.
2313 FPPragmaOptions.swap(Record);
2314 break;
2315
2316 case OPENCL_EXTENSIONS:
2317 // Later tables overwrite earlier ones.
2318 OpenCLExtensions.swap(Record);
2319 break;
Douglas Gregorafaf3082009-04-11 00:14:32 +00002320 }
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002321 First = false;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002322 }
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002323 Error("premature end of bitstream in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002324 return Failure;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002325}
2326
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002327ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
2328 ASTFileType Type) {
2329 switch(ReadASTCore(FileName, Type)) {
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002330 case Failure: return Failure;
2331 case IgnorePCH: return IgnorePCH;
2332 case Success: break;
2333 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002334
2335 // Here comes stuff that we only do once the entire chain is loaded.
2336
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002337 // Allocate space for loaded slocentries, identifiers, decls and types.
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002338 unsigned TotalNumIdentifiers = 0, TotalNumTypes = 0, TotalNumDecls = 0,
Sebastian Redl725cd962010-08-04 20:40:17 +00002339 TotalNumPreallocatedPreprocessingEntities = 0, TotalNumMacroDefs = 0,
2340 TotalNumSelectors = 0;
Sebastian Redl12d6da02010-07-19 22:06:55 +00002341 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002342 TotalNumSLocEntries += Chain[I]->LocalNumSLocEntries;
Sebastian Redl8db9fae2010-09-22 20:19:08 +00002343 NextSLocOffset += Chain[I]->LocalSLocSize;
Sebastian Redl2da08f92010-07-19 22:28:42 +00002344 TotalNumIdentifiers += Chain[I]->LocalNumIdentifiers;
Sebastian Redl12d6da02010-07-19 22:06:55 +00002345 TotalNumTypes += Chain[I]->LocalNumTypes;
2346 TotalNumDecls += Chain[I]->LocalNumDecls;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002347 TotalNumPreallocatedPreprocessingEntities +=
2348 Chain[I]->NumPreallocatedPreprocessingEntities;
2349 TotalNumMacroDefs += Chain[I]->LocalNumMacroDefinitions;
Sebastian Redl725cd962010-08-04 20:40:17 +00002350 TotalNumSelectors += Chain[I]->LocalNumSelectors;
Sebastian Redl12d6da02010-07-19 22:06:55 +00002351 }
Sebastian Redl8db9fae2010-09-22 20:19:08 +00002352 SourceMgr.PreallocateSLocEntries(this, TotalNumSLocEntries, NextSLocOffset);
Sebastian Redl2da08f92010-07-19 22:28:42 +00002353 IdentifiersLoaded.resize(TotalNumIdentifiers);
Sebastian Redl12d6da02010-07-19 22:06:55 +00002354 TypesLoaded.resize(TotalNumTypes);
2355 DeclsLoaded.resize(TotalNumDecls);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002356 MacroDefinitionsLoaded.resize(TotalNumMacroDefs);
2357 if (PP) {
2358 if (TotalNumIdentifiers > 0)
2359 PP->getHeaderSearchInfo().SetExternalLookup(this);
2360 if (TotalNumPreallocatedPreprocessingEntities > 0) {
2361 if (!PP->getPreprocessingRecord())
2362 PP->createPreprocessingRecord();
2363 PP->getPreprocessingRecord()->SetExternalSource(*this,
2364 TotalNumPreallocatedPreprocessingEntities);
2365 }
2366 }
Sebastian Redl725cd962010-08-04 20:40:17 +00002367 SelectorsLoaded.resize(TotalNumSelectors);
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002368 // Preload SLocEntries.
2369 for (unsigned I = 0, N = PreloadSLocEntries.size(); I != N; ++I) {
2370 ASTReadResult Result = ReadSLocEntryRecord(PreloadSLocEntries[I]);
2371 if (Result != Success)
2372 return Result;
2373 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00002374
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002375 // Check the predefines buffers.
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00002376 if (!DisableValidation && CheckPredefinesBuffers())
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002377 return IgnorePCH;
2378
2379 if (PP) {
2380 // Initialization of keywords and pragmas occurs before the
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002381 // AST file is read, so there may be some identifiers that were
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002382 // loaded into the IdentifierTable before we intercepted the
2383 // creation of identifiers. Iterate through the list of known
2384 // identifiers and determine whether we have to establish
2385 // preprocessor definitions or top-level identifier declaration
2386 // chains for those identifiers.
2387 //
2388 // We copy the IdentifierInfo pointers to a small vector first,
2389 // since de-serializing declarations or macro definitions can add
2390 // new entries into the identifier table, invalidating the
2391 // iterators.
2392 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
2393 for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(),
2394 IdEnd = PP->getIdentifierTable().end();
2395 Id != IdEnd; ++Id)
2396 Identifiers.push_back(Id->second);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002397 // We need to search the tables in all files.
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002398 for (unsigned J = 0, M = Chain.size(); J != M; ++J) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002399 ASTIdentifierLookupTable *IdTable
2400 = (ASTIdentifierLookupTable *)Chain[J]->IdentifierLookupTable;
2401 // Not all AST files necessarily have identifier tables, only the useful
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00002402 // ones.
2403 if (!IdTable)
2404 continue;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002405 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
2406 IdentifierInfo *II = Identifiers[I];
2407 // Look in the on-disk hash tables for an entry for this identifier
Sebastian Redlc3632732010-10-05 15:59:54 +00002408 ASTIdentifierLookupTrait Info(*this, *Chain[J], II);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002409 std::pair<const char*,unsigned> Key(II->getNameStart(),II->getLength());
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002410 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002411 if (Pos == IdTable->end())
2412 continue;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002413
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002414 // Dereferencing the iterator has the effect of populating the
2415 // IdentifierInfo node with the various declarations it needs.
2416 (void)*Pos;
2417 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002418 }
2419 }
2420
2421 if (Context)
2422 InitializeContext(*Context);
2423
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002424 if (DeserializationListener)
2425 DeserializationListener->ReaderInitialized(this);
2426
Douglas Gregor414cb642010-11-30 05:23:00 +00002427 // If this AST file is a precompiled preamble, then set the main file ID of
2428 // the source manager to the file source file from which the preamble was
2429 // built. This is the only valid way to use a precompiled preamble.
2430 if (Type == Preamble) {
2431 SourceLocation Loc
2432 = SourceMgr.getLocation(FileMgr.getFile(getOriginalSourceFile()), 1, 1);
2433 if (Loc.isValid()) {
2434 std::pair<FileID, unsigned> Decomposed = SourceMgr.getDecomposedLoc(Loc);
2435 SourceMgr.SetPreambleFileID(Decomposed.first);
2436 }
2437 }
2438
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002439 return Success;
2440}
2441
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002442ASTReader::ASTReadResult ASTReader::ReadASTCore(llvm::StringRef FileName,
2443 ASTFileType Type) {
Sebastian Redla866e652010-10-01 19:59:12 +00002444 PerFileData *Prev = Chain.empty() ? 0 : Chain.back();
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002445 Chain.push_back(new PerFileData(Type));
Sebastian Redl9137a522010-07-16 17:50:48 +00002446 PerFileData &F = *Chain.back();
Sebastian Redla866e652010-10-01 19:59:12 +00002447 if (Prev)
2448 Prev->NextInSource = &F;
2449 else
2450 FirstInSource = &F;
2451 F.Loaders.push_back(Prev);
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002452
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002453 // Set the AST file name.
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002454 F.FileName = FileName;
2455
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002456 if (FileName != "-") {
2457 CurrentDir = llvm::sys::path::parent_path(FileName);
2458 if (CurrentDir.empty()) CurrentDir = ".";
2459 }
2460
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002461 // Open the AST file.
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002462 //
2463 // FIXME: This shouldn't be here, we should just take a raw_ostream.
2464 std::string ErrStr;
Michael J. Spencer3a321e22010-12-09 17:36:38 +00002465 llvm::error_code ec;
2466 if (FileName == "-") {
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00002467 ec = llvm::MemoryBuffer::getSTDIN(F.Buffer);
Michael J. Spencer3a321e22010-12-09 17:36:38 +00002468 if (ec)
2469 ErrStr = ec.message();
2470 } else
Chris Lattner39b49bc2010-11-23 08:35:12 +00002471 F.Buffer.reset(FileMgr.getBufferForFile(FileName, &ErrStr));
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002472 if (!F.Buffer) {
2473 Error(ErrStr.c_str());
2474 return IgnorePCH;
2475 }
2476
2477 // Initialize the stream
2478 F.StreamFile.init((const unsigned char *)F.Buffer->getBufferStart(),
2479 (const unsigned char *)F.Buffer->getBufferEnd());
Sebastian Redl9137a522010-07-16 17:50:48 +00002480 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002481 Stream.init(F.StreamFile);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002482 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002483
2484 // Sniff for the signature.
2485 if (Stream.Read(8) != 'C' ||
2486 Stream.Read(8) != 'P' ||
2487 Stream.Read(8) != 'C' ||
2488 Stream.Read(8) != 'H') {
2489 Diag(diag::err_not_a_pch_file) << FileName;
2490 return Failure;
2491 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002492
Douglas Gregor2cf26342009-04-09 22:27:44 +00002493 while (!Stream.AtEndOfStream()) {
2494 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00002495
Douglas Gregore1d918e2009-04-10 23:10:45 +00002496 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002497 Error("invalid record at top-level of AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002498 return Failure;
2499 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002500
2501 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregor668c1a42009-04-21 22:25:48 +00002502
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002503 // We only know the AST subblock ID.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002504 switch (BlockID) {
2505 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002506 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002507 Error("malformed BlockInfoBlock in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002508 return Failure;
2509 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002510 break;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002511 case AST_BLOCK_ID:
Sebastian Redl571db7f2010-08-18 23:56:56 +00002512 switch (ReadASTBlock(F)) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002513 case Success:
2514 break;
2515
2516 case Failure:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002517 return Failure;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002518
2519 case IgnorePCH:
Douglas Gregor2bec0412009-04-10 21:16:55 +00002520 // FIXME: We could consider reading through to the end of this
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002521 // AST block, skipping subblocks, to see if there are other
2522 // AST blocks elsewhere.
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002523
2524 // Clear out any preallocated source location entries, so that
2525 // the source manager does not try to resolve them later.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002526 SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002527
2528 // Remove the stat cache.
Sebastian Redl9137a522010-07-16 17:50:48 +00002529 if (F.StatCache)
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002530 FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002531
Douglas Gregore1d918e2009-04-10 23:10:45 +00002532 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002533 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002534 break;
2535 default:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002536 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002537 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002538 return Failure;
2539 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002540 break;
2541 }
Mike Stump1eb44332009-09-09 15:08:12 +00002542 }
2543
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002544 return Success;
2545}
2546
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002547void ASTReader::setPreprocessor(Preprocessor &pp) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002548 PP = &pp;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002549
2550 unsigned TotalNum = 0;
2551 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
2552 TotalNum += Chain[I]->NumPreallocatedPreprocessingEntities;
2553 if (TotalNum) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002554 if (!PP->getPreprocessingRecord())
2555 PP->createPreprocessingRecord();
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002556 PP->getPreprocessingRecord()->SetExternalSource(*this, TotalNum);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002557 }
2558}
2559
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002560void ASTReader::InitializeContext(ASTContext &Ctx) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002561 Context = &Ctx;
2562 assert(Context && "Passed null context!");
2563
2564 assert(PP && "Forgot to set Preprocessor ?");
2565 PP->getIdentifierTable().setExternalIdentifierLookup(this);
2566 PP->getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregor88a35862010-01-04 19:18:44 +00002567 PP->setExternalSource(this);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002568 PP->getHeaderSearchInfo().SetExternalSource(this);
2569
Douglas Gregor3747ee72010-10-01 01:18:02 +00002570 // If we have an update block for the TU waiting, we have to add it before
2571 // deserializing the decl.
2572 DeclContextOffsetsMap::iterator DCU = DeclContextOffsets.find(0);
2573 if (DCU != DeclContextOffsets.end()) {
2574 // Insertion could invalidate map, so grab vector.
2575 DeclContextInfos T;
2576 T.swap(DCU->second);
2577 DeclContextOffsets.erase(DCU);
2578 DeclContextOffsets[Ctx.getTranslationUnitDecl()].swap(T);
2579 }
2580
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002581 // Load the translation unit declaration
Argyrios Kyrtzidis8871a442010-07-08 17:13:02 +00002582 GetTranslationUnitDecl();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002583
2584 // Load the special types.
2585 Context->setBuiltinVaListType(
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002586 GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST]));
2587 if (unsigned Id = SpecialTypes[SPECIAL_TYPE_OBJC_ID])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002588 Context->setObjCIdType(GetType(Id));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002589 if (unsigned Sel = SpecialTypes[SPECIAL_TYPE_OBJC_SELECTOR])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002590 Context->setObjCSelType(GetType(Sel));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002591 if (unsigned Proto = SpecialTypes[SPECIAL_TYPE_OBJC_PROTOCOL])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002592 Context->setObjCProtoType(GetType(Proto));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002593 if (unsigned Class = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002594 Context->setObjCClassType(GetType(Class));
Steve Naroff14108da2009-07-10 23:34:53 +00002595
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002596 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002597 Context->setCFConstantStringType(GetType(String));
Mike Stump1eb44332009-09-09 15:08:12 +00002598 if (unsigned FastEnum
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002599 = SpecialTypes[SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002600 Context->setObjCFastEnumerationStateType(GetType(FastEnum));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002601 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002602 QualType FileType = GetType(File);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002603 if (FileType.isNull()) {
2604 Error("FILE type is NULL");
2605 return;
2606 }
John McCall183700f2009-09-21 23:43:11 +00002607 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002608 Context->setFILEDecl(Typedef->getDecl());
2609 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002610 const TagType *Tag = FileType->getAs<TagType>();
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002611 if (!Tag) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002612 Error("Invalid FILE type in AST file");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002613 return;
2614 }
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002615 Context->setFILEDecl(Tag->getDecl());
2616 }
2617 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002618 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_jmp_buf]) {
Mike Stump782fa302009-07-28 02:25:19 +00002619 QualType Jmp_bufType = GetType(Jmp_buf);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002620 if (Jmp_bufType.isNull()) {
2621 Error("jmp_bug type is NULL");
2622 return;
2623 }
John McCall183700f2009-09-21 23:43:11 +00002624 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
Mike Stump782fa302009-07-28 02:25:19 +00002625 Context->setjmp_bufDecl(Typedef->getDecl());
2626 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002627 const TagType *Tag = Jmp_bufType->getAs<TagType>();
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002628 if (!Tag) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002629 Error("Invalid jmp_buf type in AST file");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002630 return;
2631 }
Mike Stump782fa302009-07-28 02:25:19 +00002632 Context->setjmp_bufDecl(Tag->getDecl());
2633 }
2634 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002635 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_sigjmp_buf]) {
Mike Stump782fa302009-07-28 02:25:19 +00002636 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002637 if (Sigjmp_bufType.isNull()) {
2638 Error("sigjmp_buf type is NULL");
2639 return;
2640 }
John McCall183700f2009-09-21 23:43:11 +00002641 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
Mike Stump782fa302009-07-28 02:25:19 +00002642 Context->setsigjmp_bufDecl(Typedef->getDecl());
2643 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002644 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002645 assert(Tag && "Invalid sigjmp_buf type in AST file");
Mike Stump782fa302009-07-28 02:25:19 +00002646 Context->setsigjmp_bufDecl(Tag->getDecl());
2647 }
2648 }
Mike Stump1eb44332009-09-09 15:08:12 +00002649 if (unsigned ObjCIdRedef
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002650 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION])
Douglas Gregord1571ac2009-08-21 00:27:50 +00002651 Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef);
Mike Stump1eb44332009-09-09 15:08:12 +00002652 if (unsigned ObjCClassRedef
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002653 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION])
Douglas Gregord1571ac2009-08-21 00:27:50 +00002654 Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002655 if (unsigned String = SpecialTypes[SPECIAL_TYPE_BLOCK_DESCRIPTOR])
Mike Stumpadaaad32009-10-20 02:12:22 +00002656 Context->setBlockDescriptorType(GetType(String));
Mike Stump083c25e2009-10-22 00:49:09 +00002657 if (unsigned String
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002658 = SpecialTypes[SPECIAL_TYPE_BLOCK_EXTENDED_DESCRIPTOR])
Mike Stump083c25e2009-10-22 00:49:09 +00002659 Context->setBlockDescriptorExtendedType(GetType(String));
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002660 if (unsigned ObjCSelRedef
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002661 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION])
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002662 Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002663 if (unsigned String = SpecialTypes[SPECIAL_TYPE_NS_CONSTANT_STRING])
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002664 Context->setNSConstantStringType(GetType(String));
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +00002665
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002666 if (SpecialTypes[SPECIAL_TYPE_INT128_INSTALLED])
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +00002667 Context->setInt128Installed();
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002668
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002669 ReadPragmaDiagnosticMappings(Context->getDiagnostics());
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002670
2671 // If there were any CUDA special declarations, deserialize them.
2672 if (!CUDASpecialDeclRefs.empty()) {
2673 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
2674 Context->setcudaConfigureCallDecl(
2675 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
2676 }
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002677}
2678
Douglas Gregorb64c1932009-05-12 01:31:05 +00002679/// \brief Retrieve the name of the original source file name
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002680/// directly from the AST file, without actually loading the AST
Douglas Gregorb64c1932009-05-12 01:31:05 +00002681/// file.
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002682std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +00002683 FileManager &FileMgr,
Daniel Dunbar93ebb1b2009-12-03 09:13:06 +00002684 Diagnostic &Diags) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002685 // Open the AST file.
Douglas Gregorb64c1932009-05-12 01:31:05 +00002686 std::string ErrStr;
2687 llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
Chris Lattner39b49bc2010-11-23 08:35:12 +00002688 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
Douglas Gregorb64c1932009-05-12 01:31:05 +00002689 if (!Buffer) {
Daniel Dunbar93ebb1b2009-12-03 09:13:06 +00002690 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002691 return std::string();
2692 }
2693
2694 // Initialize the stream
2695 llvm::BitstreamReader StreamFile;
2696 llvm::BitstreamCursor Stream;
Mike Stump1eb44332009-09-09 15:08:12 +00002697 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregorb64c1932009-05-12 01:31:05 +00002698 (const unsigned char *)Buffer->getBufferEnd());
2699 Stream.init(StreamFile);
2700
2701 // Sniff for the signature.
2702 if (Stream.Read(8) != 'C' ||
2703 Stream.Read(8) != 'P' ||
2704 Stream.Read(8) != 'C' ||
2705 Stream.Read(8) != 'H') {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002706 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002707 return std::string();
2708 }
2709
2710 RecordData Record;
2711 while (!Stream.AtEndOfStream()) {
2712 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00002713
Douglas Gregorb64c1932009-05-12 01:31:05 +00002714 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2715 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump1eb44332009-09-09 15:08:12 +00002716
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002717 // We only know the AST subblock ID.
Douglas Gregorb64c1932009-05-12 01:31:05 +00002718 switch (BlockID) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002719 case AST_BLOCK_ID:
2720 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002721 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002722 return std::string();
2723 }
2724 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002725
Douglas Gregorb64c1932009-05-12 01:31:05 +00002726 default:
2727 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002728 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002729 return std::string();
2730 }
2731 break;
2732 }
2733 continue;
2734 }
2735
2736 if (Code == llvm::bitc::END_BLOCK) {
2737 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002738 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002739 return std::string();
2740 }
2741 continue;
2742 }
2743
2744 if (Code == llvm::bitc::DEFINE_ABBREV) {
2745 Stream.ReadAbbrevRecord();
2746 continue;
2747 }
2748
2749 Record.clear();
2750 const char *BlobStart = 0;
2751 unsigned BlobLen = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002752 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002753 == ORIGINAL_FILE_NAME)
Douglas Gregorb64c1932009-05-12 01:31:05 +00002754 return std::string(BlobStart, BlobLen);
Mike Stump1eb44332009-09-09 15:08:12 +00002755 }
Douglas Gregorb64c1932009-05-12 01:31:05 +00002756
2757 return std::string();
2758}
2759
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002760/// \brief Parse the record that corresponds to a LangOptions data
2761/// structure.
2762///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002763/// This routine parses the language options from the AST file and then gives
2764/// them to the AST listener if one is set.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002765///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002766/// \returns true if the listener deems the file unacceptable, false otherwise.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002767bool ASTReader::ParseLanguageOptions(
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002768 const llvm::SmallVectorImpl<uint64_t> &Record) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002769 if (Listener) {
2770 LangOptions LangOpts;
Mike Stump1eb44332009-09-09 15:08:12 +00002771
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002772 #define PARSE_LANGOPT(Option) \
2773 LangOpts.Option = Record[Idx]; \
2774 ++Idx
Mike Stump1eb44332009-09-09 15:08:12 +00002775
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002776 unsigned Idx = 0;
2777 PARSE_LANGOPT(Trigraphs);
2778 PARSE_LANGOPT(BCPLComment);
2779 PARSE_LANGOPT(DollarIdents);
2780 PARSE_LANGOPT(AsmPreprocessor);
2781 PARSE_LANGOPT(GNUMode);
Chandler Carrutheb5d7b72010-04-17 20:17:31 +00002782 PARSE_LANGOPT(GNUKeywords);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002783 PARSE_LANGOPT(ImplicitInt);
2784 PARSE_LANGOPT(Digraphs);
2785 PARSE_LANGOPT(HexFloats);
2786 PARSE_LANGOPT(C99);
2787 PARSE_LANGOPT(Microsoft);
2788 PARSE_LANGOPT(CPlusPlus);
2789 PARSE_LANGOPT(CPlusPlus0x);
2790 PARSE_LANGOPT(CXXOperatorNames);
2791 PARSE_LANGOPT(ObjC1);
2792 PARSE_LANGOPT(ObjC2);
2793 PARSE_LANGOPT(ObjCNonFragileABI);
Fariborz Jahanian412e7982010-02-09 19:31:38 +00002794 PARSE_LANGOPT(ObjCNonFragileABI2);
Fariborz Jahanianf84109e2011-01-07 18:59:25 +00002795 PARSE_LANGOPT(AppleKext);
Ted Kremenekc32647d2010-12-23 21:35:43 +00002796 PARSE_LANGOPT(ObjCDefaultSynthProperties);
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +00002797 PARSE_LANGOPT(NoConstantCFStrings);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002798 PARSE_LANGOPT(PascalStrings);
2799 PARSE_LANGOPT(WritableStrings);
2800 PARSE_LANGOPT(LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +00002801 PARSE_LANGOPT(AltiVec);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002802 PARSE_LANGOPT(Exceptions);
Anders Carlssonda4b7cf2011-02-19 23:53:54 +00002803 PARSE_LANGOPT(ObjCExceptions);
Anders Carlsson7da99b02011-02-23 03:04:54 +00002804 PARSE_LANGOPT(CXXExceptions);
2805 PARSE_LANGOPT(SjLjExceptions);
Douglas Gregor6f755502011-02-01 15:15:22 +00002806 PARSE_LANGOPT(MSBitfields);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002807 PARSE_LANGOPT(NeXTRuntime);
2808 PARSE_LANGOPT(Freestanding);
2809 PARSE_LANGOPT(NoBuiltin);
2810 PARSE_LANGOPT(ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +00002811 PARSE_LANGOPT(POSIXThreads);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002812 PARSE_LANGOPT(Blocks);
2813 PARSE_LANGOPT(EmitAllDecls);
2814 PARSE_LANGOPT(MathErrno);
Chris Lattnera4d71452010-06-26 21:25:03 +00002815 LangOpts.setSignedOverflowBehavior((LangOptions::SignedOverflowBehaviorTy)
2816 Record[Idx++]);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002817 PARSE_LANGOPT(HeinousExtensions);
2818 PARSE_LANGOPT(Optimize);
2819 PARSE_LANGOPT(OptimizeSize);
2820 PARSE_LANGOPT(Static);
2821 PARSE_LANGOPT(PICLevel);
2822 PARSE_LANGOPT(GNUInline);
2823 PARSE_LANGOPT(NoInline);
2824 PARSE_LANGOPT(AccessControl);
2825 PARSE_LANGOPT(CharIsSigned);
John Thompsona6fda122009-11-05 20:14:16 +00002826 PARSE_LANGOPT(ShortWChar);
Argyrios Kyrtzidisb1bdced2011-01-15 02:56:16 +00002827 PARSE_LANGOPT(ShortEnums);
Chris Lattnera4d71452010-06-26 21:25:03 +00002828 LangOpts.setGCMode((LangOptions::GCMode)Record[Idx++]);
John McCall1fb0caa2010-10-22 21:05:15 +00002829 LangOpts.setVisibilityMode((Visibility)Record[Idx++]);
Daniel Dunbarab8e2812009-09-21 04:16:19 +00002830 LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode)
Chris Lattnera4d71452010-06-26 21:25:03 +00002831 Record[Idx++]);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002832 PARSE_LANGOPT(InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +00002833 PARSE_LANGOPT(OpenCL);
Peter Collingbourne08a53262010-12-01 19:14:57 +00002834 PARSE_LANGOPT(CUDA);
Mike Stump9c276ae2009-12-12 01:27:46 +00002835 PARSE_LANGOPT(CatchUndefined);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002836 PARSE_LANGOPT(DefaultFPContract);
Mike Stump9c276ae2009-12-12 01:27:46 +00002837 // FIXME: Missing ElideConstructors?!
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002838 #undef PARSE_LANGOPT
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002839
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002840 return Listener->ReadLanguageOptions(LangOpts);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002841 }
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002842
2843 return false;
2844}
2845
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002846void ASTReader::ReadPreprocessedEntities() {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002847 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2848 PerFileData &F = *Chain[I];
2849 if (!F.PreprocessorDetailCursor.getBitStreamReader())
2850 continue;
2851
2852 SavedStreamPosition SavedPosition(F.PreprocessorDetailCursor);
2853 F.PreprocessorDetailCursor.JumpToBit(F.PreprocessorDetailStartOffset);
2854 while (LoadPreprocessedEntity(F)) { }
2855 }
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002856}
2857
Douglas Gregor0a480292011-02-11 19:46:30 +00002858PreprocessedEntity *ASTReader::ReadPreprocessedEntityAtOffset(uint64_t Offset) {
Douglas Gregor89d99802010-11-30 06:16:57 +00002859 PerFileData *F = 0;
2860 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2861 if (Offset < Chain[I]->SizeInBits) {
2862 F = Chain[I];
2863 break;
2864 }
2865
2866 Offset -= Chain[I]->SizeInBits;
2867 }
2868
2869 if (!F) {
2870 Error("Malformed preprocessed entity offset");
2871 return 0;
2872 }
2873
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002874 // Keep track of where we are in the stream, then jump back there
2875 // after reading this entity.
2876 SavedStreamPosition SavedPosition(F->PreprocessorDetailCursor);
2877 F->PreprocessorDetailCursor.JumpToBit(Offset);
2878 return LoadPreprocessedEntity(*F);
Douglas Gregor89d99802010-11-30 06:16:57 +00002879}
2880
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002881HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
2882 HeaderFileInfoTrait Trait(FE->getName());
2883 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2884 PerFileData &F = *Chain[I];
2885 HeaderFileInfoLookupTable *Table
2886 = static_cast<HeaderFileInfoLookupTable *>(F.HeaderFileInfoTable);
2887 if (!Table)
2888 continue;
2889
2890 // Look in the on-disk hash table for an entry for this file name.
2891 HeaderFileInfoLookupTable::iterator Pos = Table->find(FE->getName(),
2892 &Trait);
2893 if (Pos == Table->end())
2894 continue;
2895
2896 HeaderFileInfo HFI = *Pos;
2897 if (Listener)
2898 Listener->ReadHeaderFileInfo(HFI, FE->getUID());
2899
2900 return HFI;
2901 }
2902
2903 return HeaderFileInfo();
2904}
2905
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002906void ASTReader::ReadPragmaDiagnosticMappings(Diagnostic &Diag) {
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002907 unsigned Idx = 0;
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002908 while (Idx < PragmaDiagMappings.size()) {
2909 SourceLocation
2910 Loc = SourceLocation::getFromRawEncoding(PragmaDiagMappings[Idx++]);
2911 while (1) {
2912 assert(Idx < PragmaDiagMappings.size() &&
2913 "Invalid data, didn't find '-1' marking end of diag/map pairs");
2914 if (Idx >= PragmaDiagMappings.size())
2915 break; // Something is messed up but at least avoid infinite loop in
2916 // release build.
2917 unsigned DiagID = PragmaDiagMappings[Idx++];
2918 if (DiagID == (unsigned)-1)
2919 break; // no more diag/map pairs for this location.
2920 diag::Mapping Map = (diag::Mapping)PragmaDiagMappings[Idx++];
2921 Diag.setDiagnosticMapping(DiagID, Map, Loc);
2922 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002923 }
2924}
2925
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002926/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002927ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002928 PerFileData *F = 0;
2929 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2930 F = Chain[N - I - 1];
2931 if (Index < F->LocalNumTypes)
2932 break;
2933 Index -= F->LocalNumTypes;
2934 }
2935 assert(F && F->LocalNumTypes > Index && "Broken chain");
Sebastian Redlc3632732010-10-05 15:59:54 +00002936 return RecordLocation(F, F->TypeOffsets[Index]);
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002937}
2938
2939/// \brief Read and return the type with the given index..
Douglas Gregor2cf26342009-04-09 22:27:44 +00002940///
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002941/// The index is the type ID, shifted and minus the number of predefs. This
2942/// routine actually reads the record corresponding to the type at the given
2943/// location. It is a helper routine for GetType, which deals with reading type
2944/// IDs.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002945QualType ASTReader::ReadTypeRecord(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002946 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00002947 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00002948
Douglas Gregor0b748912009-04-14 21:18:50 +00002949 // Keep track of where we are in the stream, then jump back there
2950 // after reading this type.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002951 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregor0b748912009-04-14 21:18:50 +00002952
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00002953 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redl27372b42010-08-11 18:52:41 +00002954
Douglas Gregord89275b2009-07-06 18:54:52 +00002955 // Note that we are loading a type record.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00002956 Deserializing AType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00002957
Sebastian Redlc3632732010-10-05 15:59:54 +00002958 DeclsCursor.JumpToBit(Loc.Offset);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002959 RecordData Record;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002960 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002961 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
2962 case TYPE_EXT_QUAL: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002963 if (Record.size() != 2) {
2964 Error("Incorrect encoding of extended qualifier type");
2965 return QualType();
2966 }
Douglas Gregor6d473962009-04-15 22:00:08 +00002967 QualType Base = GetType(Record[0]);
John McCall0953e762009-09-24 19:53:00 +00002968 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[1]);
2969 return Context->getQualifiedType(Base, Quals);
Douglas Gregor6d473962009-04-15 22:00:08 +00002970 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002971
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002972 case TYPE_COMPLEX: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002973 if (Record.size() != 1) {
2974 Error("Incorrect encoding of complex type");
2975 return QualType();
2976 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002977 QualType ElemType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002978 return Context->getComplexType(ElemType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002979 }
2980
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002981 case TYPE_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002982 if (Record.size() != 1) {
2983 Error("Incorrect encoding of pointer type");
2984 return QualType();
2985 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002986 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002987 return Context->getPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002988 }
2989
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002990 case TYPE_BLOCK_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002991 if (Record.size() != 1) {
2992 Error("Incorrect encoding of block pointer type");
2993 return QualType();
2994 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002995 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002996 return Context->getBlockPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002997 }
2998
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002999 case TYPE_LVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003000 if (Record.size() != 1) {
3001 Error("Incorrect encoding of lvalue reference type");
3002 return QualType();
3003 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003004 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003005 return Context->getLValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003006 }
3007
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003008 case TYPE_RVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003009 if (Record.size() != 1) {
3010 Error("Incorrect encoding of rvalue reference type");
3011 return QualType();
3012 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003013 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003014 return Context->getRValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003015 }
3016
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003017 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidis240437b2010-07-02 11:55:15 +00003018 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003019 Error("Incorrect encoding of member pointer type");
3020 return QualType();
3021 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003022 QualType PointeeType = GetType(Record[0]);
3023 QualType ClassType = GetType(Record[1]);
Douglas Gregor1ab55e92010-12-10 17:03:06 +00003024 if (PointeeType.isNull() || ClassType.isNull())
3025 return QualType();
3026
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003027 return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregor2cf26342009-04-09 22:27:44 +00003028 }
3029
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003030 case TYPE_CONSTANT_ARRAY: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003031 QualType ElementType = GetType(Record[0]);
3032 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3033 unsigned IndexTypeQuals = Record[2];
3034 unsigned Idx = 3;
3035 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003036 return Context->getConstantArrayType(ElementType, Size,
3037 ASM, IndexTypeQuals);
3038 }
3039
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003040 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003041 QualType ElementType = GetType(Record[0]);
3042 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3043 unsigned IndexTypeQuals = Record[2];
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003044 return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003045 }
3046
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003047 case TYPE_VARIABLE_ARRAY: {
Douglas Gregor0b748912009-04-14 21:18:50 +00003048 QualType ElementType = GetType(Record[0]);
3049 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3050 unsigned IndexTypeQuals = Record[2];
Sebastian Redlc3632732010-10-05 15:59:54 +00003051 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
3052 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
3053 return Context->getVariableArrayType(ElementType, ReadExpr(*Loc.F),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003054 ASM, IndexTypeQuals,
3055 SourceRange(LBLoc, RBLoc));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003056 }
3057
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003058 case TYPE_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00003059 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003060 Error("incorrect encoding of vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003061 return QualType();
3062 }
3063
3064 QualType ElementType = GetType(Record[0]);
3065 unsigned NumElements = Record[1];
Bob Wilsone86d78c2010-11-10 21:56:12 +00003066 unsigned VecKind = Record[2];
Chris Lattner788b0fd2010-06-23 06:00:24 +00003067 return Context->getVectorType(ElementType, NumElements,
Bob Wilsone86d78c2010-11-10 21:56:12 +00003068 (VectorType::VectorKind)VecKind);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003069 }
3070
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003071 case TYPE_EXT_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00003072 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003073 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003074 return QualType();
3075 }
3076
3077 QualType ElementType = GetType(Record[0]);
3078 unsigned NumElements = Record[1];
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003079 return Context->getExtVectorType(ElementType, NumElements);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003080 }
3081
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003082 case TYPE_FUNCTION_NO_PROTO: {
Rafael Espindola425ef722010-03-30 22:15:11 +00003083 if (Record.size() != 4) {
Douglas Gregora02b1472009-04-28 21:53:25 +00003084 Error("incorrect encoding of no-proto function type");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003085 return QualType();
3086 }
3087 QualType ResultType = GetType(Record[0]);
Rafael Espindola425ef722010-03-30 22:15:11 +00003088 FunctionType::ExtInfo Info(Record[1], Record[2], (CallingConv)Record[3]);
Rafael Espindola264ba482010-03-30 20:24:48 +00003089 return Context->getFunctionNoProtoType(ResultType, Info);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003090 }
3091
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003092 case TYPE_FUNCTION_PROTO: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003093 QualType ResultType = GetType(Record[0]);
John McCalle23cf432010-12-14 08:05:40 +00003094
3095 FunctionProtoType::ExtProtoInfo EPI;
3096 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
3097 /*regparm*/ Record[2],
3098 static_cast<CallingConv>(Record[3]));
3099
Rafael Espindola425ef722010-03-30 22:15:11 +00003100 unsigned Idx = 4;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003101 unsigned NumParams = Record[Idx++];
3102 llvm::SmallVector<QualType, 16> ParamTypes;
3103 for (unsigned I = 0; I != NumParams; ++I)
3104 ParamTypes.push_back(GetType(Record[Idx++]));
John McCalle23cf432010-12-14 08:05:40 +00003105
3106 EPI.Variadic = Record[Idx++];
3107 EPI.TypeQuals = Record[Idx++];
Douglas Gregorc938c162011-01-26 05:01:58 +00003108 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
John McCalle23cf432010-12-14 08:05:40 +00003109 EPI.HasExceptionSpec = Record[Idx++];
3110 EPI.HasAnyExceptionSpec = Record[Idx++];
3111 EPI.NumExceptions = Record[Idx++];
Sebastian Redl465226e2009-05-27 22:11:52 +00003112 llvm::SmallVector<QualType, 2> Exceptions;
John McCalle23cf432010-12-14 08:05:40 +00003113 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
Sebastian Redl465226e2009-05-27 22:11:52 +00003114 Exceptions.push_back(GetType(Record[Idx++]));
John McCalle23cf432010-12-14 08:05:40 +00003115 EPI.Exceptions = Exceptions.data();
Jay Foadbeaaccd2009-05-21 09:52:38 +00003116 return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams,
John McCalle23cf432010-12-14 08:05:40 +00003117 EPI);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003118 }
3119
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003120 case TYPE_UNRESOLVED_USING:
John McCalled976492009-12-04 22:46:56 +00003121 return Context->getTypeDeclType(
3122 cast<UnresolvedUsingTypenameDecl>(GetDecl(Record[0])));
3123
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003124 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003125 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003126 Error("incorrect encoding of typedef type");
3127 return QualType();
3128 }
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003129 TypedefDecl *Decl = cast<TypedefDecl>(GetDecl(Record[0]));
3130 QualType Canonical = GetType(Record[1]);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00003131 if (!Canonical.isNull())
3132 Canonical = Context->getCanonicalType(Canonical);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003133 return Context->getTypedefType(Decl, Canonical);
3134 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003135
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003136 case TYPE_TYPEOF_EXPR:
Sebastian Redlc3632732010-10-05 15:59:54 +00003137 return Context->getTypeOfExprType(ReadExpr(*Loc.F));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003138
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003139 case TYPE_TYPEOF: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003140 if (Record.size() != 1) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003141 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003142 return QualType();
3143 }
3144 QualType UnderlyingType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003145 return Context->getTypeOfType(UnderlyingType);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003146 }
Mike Stump1eb44332009-09-09 15:08:12 +00003147
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003148 case TYPE_DECLTYPE:
Sebastian Redlc3632732010-10-05 15:59:54 +00003149 return Context->getDecltypeType(ReadExpr(*Loc.F));
Anders Carlsson395b4752009-06-24 19:06:50 +00003150
Richard Smith34b41d92011-02-20 03:19:35 +00003151 case TYPE_AUTO:
3152 return Context->getAutoType(GetType(Record[0]));
3153
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003154 case TYPE_RECORD: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003155 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003156 Error("incorrect encoding of record type");
3157 return QualType();
3158 }
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003159 bool IsDependent = Record[0];
3160 QualType T = Context->getRecordType(cast<RecordDecl>(GetDecl(Record[1])));
John McCallf4c73712011-01-19 06:33:43 +00003161 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003162 return T;
3163 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003164
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003165 case TYPE_ENUM: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003166 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003167 Error("incorrect encoding of enum type");
3168 return QualType();
3169 }
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003170 bool IsDependent = Record[0];
3171 QualType T = Context->getEnumType(cast<EnumDecl>(GetDecl(Record[1])));
John McCallf4c73712011-01-19 06:33:43 +00003172 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003173 return T;
3174 }
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003175
John McCall9d156a72011-01-06 01:58:22 +00003176 case TYPE_ATTRIBUTED: {
3177 if (Record.size() != 3) {
3178 Error("incorrect encoding of attributed type");
3179 return QualType();
3180 }
3181 QualType modifiedType = GetType(Record[0]);
3182 QualType equivalentType = GetType(Record[1]);
3183 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
3184 return Context->getAttributedType(kind, modifiedType, equivalentType);
3185 }
3186
Abramo Bagnara075f8f12010-12-10 16:29:40 +00003187 case TYPE_PAREN: {
3188 if (Record.size() != 1) {
3189 Error("incorrect encoding of paren type");
3190 return QualType();
3191 }
3192 QualType InnerType = GetType(Record[0]);
3193 return Context->getParenType(InnerType);
3194 }
3195
Douglas Gregor7536dd52010-12-20 02:24:11 +00003196 case TYPE_PACK_EXPANSION: {
Douglas Gregorf9997a02011-02-01 15:24:58 +00003197 if (Record.size() != 2) {
Douglas Gregor7536dd52010-12-20 02:24:11 +00003198 Error("incorrect encoding of pack expansion type");
3199 return QualType();
3200 }
3201 QualType Pattern = GetType(Record[0]);
3202 if (Pattern.isNull())
3203 return QualType();
Douglas Gregorcded4f62011-01-14 17:04:44 +00003204 llvm::Optional<unsigned> NumExpansions;
3205 if (Record[1])
3206 NumExpansions = Record[1] - 1;
3207 return Context->getPackExpansionType(Pattern, NumExpansions);
Douglas Gregor7536dd52010-12-20 02:24:11 +00003208 }
3209
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003210 case TYPE_ELABORATED: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00003211 unsigned Idx = 0;
3212 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3213 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3214 QualType NamedType = GetType(Record[Idx++]);
3215 return Context->getElaboratedType(Keyword, NNS, NamedType);
John McCall7da24312009-09-05 00:15:47 +00003216 }
3217
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003218 case TYPE_OBJC_INTERFACE: {
Chris Lattnerc6fa4452009-04-22 06:45:28 +00003219 unsigned Idx = 0;
3220 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
John McCallc12c5bb2010-05-15 11:32:37 +00003221 return Context->getObjCInterfaceType(ItfD);
3222 }
3223
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003224 case TYPE_OBJC_OBJECT: {
John McCallc12c5bb2010-05-15 11:32:37 +00003225 unsigned Idx = 0;
3226 QualType Base = GetType(Record[Idx++]);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00003227 unsigned NumProtos = Record[Idx++];
3228 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
3229 for (unsigned I = 0; I != NumProtos; ++I)
3230 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
Michael J. Spencer20249a12010-10-21 03:16:25 +00003231 return Context->getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00003232 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003233
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003234 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00003235 unsigned Idx = 0;
John McCallc12c5bb2010-05-15 11:32:37 +00003236 QualType Pointee = GetType(Record[Idx++]);
3237 return Context->getObjCObjectPointerType(Pointee);
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00003238 }
Argyrios Kyrtzidis24fab412009-09-29 19:42:55 +00003239
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003240 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCall49a832b2009-10-18 09:09:24 +00003241 unsigned Idx = 0;
3242 QualType Parm = GetType(Record[Idx++]);
3243 QualType Replacement = GetType(Record[Idx++]);
3244 return
3245 Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
3246 Replacement);
3247 }
John McCall3cb0ebd2010-03-10 03:28:59 +00003248
Douglas Gregorc3069d62011-01-14 02:55:32 +00003249 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
3250 unsigned Idx = 0;
3251 QualType Parm = GetType(Record[Idx++]);
3252 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
3253 return Context->getSubstTemplateTypeParmPackType(
3254 cast<TemplateTypeParmType>(Parm),
3255 ArgPack);
3256 }
3257
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003258 case TYPE_INJECTED_CLASS_NAME: {
John McCall3cb0ebd2010-03-10 03:28:59 +00003259 CXXRecordDecl *D = cast<CXXRecordDecl>(GetDecl(Record[0]));
3260 QualType TST = GetType(Record[1]); // probably derivable
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00003261 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003262 // for AST reading, too much interdependencies.
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00003263 return
3264 QualType(new (*Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCall3cb0ebd2010-03-10 03:28:59 +00003265 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003266
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003267 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003268 unsigned Idx = 0;
3269 unsigned Depth = Record[Idx++];
3270 unsigned Index = Record[Idx++];
3271 bool Pack = Record[Idx++];
3272 IdentifierInfo *Name = GetIdentifierInfo(Record, Idx);
3273 return Context->getTemplateTypeParmType(Depth, Index, Pack, Name);
3274 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003275
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003276 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00003277 unsigned Idx = 0;
3278 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3279 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3280 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +00003281 QualType Canon = GetType(Record[Idx++]);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00003282 if (!Canon.isNull())
3283 Canon = Context->getCanonicalType(Canon);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +00003284 return Context->getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00003285 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003286
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003287 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00003288 unsigned Idx = 0;
3289 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3290 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3291 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
3292 unsigned NumArgs = Record[Idx++];
3293 llvm::SmallVector<TemplateArgument, 8> Args;
3294 Args.reserve(NumArgs);
3295 while (NumArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00003296 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00003297 return Context->getDependentTemplateSpecializationType(Keyword, NNS, Name,
3298 Args.size(), Args.data());
3299 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003300
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003301 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00003302 unsigned Idx = 0;
3303
3304 // ArrayType
3305 QualType ElementType = GetType(Record[Idx++]);
3306 ArrayType::ArraySizeModifier ASM
3307 = (ArrayType::ArraySizeModifier)Record[Idx++];
3308 unsigned IndexTypeQuals = Record[Idx++];
3309
3310 // DependentSizedArrayType
Sebastian Redlc3632732010-10-05 15:59:54 +00003311 Expr *NumElts = ReadExpr(*Loc.F);
3312 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00003313
3314 return Context->getDependentSizedArrayType(ElementType, NumElts, ASM,
3315 IndexTypeQuals, Brackets);
3316 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003317
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003318 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003319 unsigned Idx = 0;
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003320 bool IsDependent = Record[Idx++];
Douglas Gregor1aee05d2011-01-15 06:45:20 +00003321 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003322 llvm::SmallVector<TemplateArgument, 8> Args;
Sebastian Redlc3632732010-10-05 15:59:54 +00003323 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003324 QualType Canon = GetType(Record[Idx++]);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003325 QualType T;
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003326 if (Canon.isNull())
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003327 T = Context->getCanonicalTemplateSpecializationType(Name, Args.data(),
3328 Args.size());
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003329 else
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003330 T = Context->getTemplateSpecializationType(Name, Args.data(),
3331 Args.size(), Canon);
John McCallf4c73712011-01-19 06:33:43 +00003332 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003333 return T;
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003334 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003335 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003336 // Suppress a GCC warning
3337 return QualType();
3338}
3339
Sebastian Redlc3632732010-10-05 15:59:54 +00003340class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003341 ASTReader &Reader;
Sebastian Redlc3632732010-10-05 15:59:54 +00003342 ASTReader::PerFileData &F;
Sebastian Redl577d4792010-07-22 22:43:28 +00003343 llvm::BitstreamCursor &DeclsCursor;
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003344 const ASTReader::RecordData &Record;
John McCalla1ee0c52009-10-16 21:56:05 +00003345 unsigned &Idx;
3346
Sebastian Redlc3632732010-10-05 15:59:54 +00003347 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
3348 unsigned &I) {
3349 return Reader.ReadSourceLocation(F, R, I);
3350 }
3351
John McCalla1ee0c52009-10-16 21:56:05 +00003352public:
Sebastian Redlc3632732010-10-05 15:59:54 +00003353 TypeLocReader(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003354 const ASTReader::RecordData &Record, unsigned &Idx)
Sebastian Redlc3632732010-10-05 15:59:54 +00003355 : Reader(Reader), F(F), DeclsCursor(F.DeclsCursor), Record(Record), Idx(Idx)
3356 { }
John McCalla1ee0c52009-10-16 21:56:05 +00003357
John McCall51bd8032009-10-18 01:05:36 +00003358 // We want compile-time assurance that we've enumerated all of
3359 // these, so unfortunately we have to declare them first, then
3360 // define them out-of-line.
3361#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +00003362#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +00003363 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00003364#include "clang/AST/TypeLocNodes.def"
3365
John McCall51bd8032009-10-18 01:05:36 +00003366 void VisitFunctionTypeLoc(FunctionTypeLoc);
3367 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00003368};
3369
John McCall51bd8032009-10-18 01:05:36 +00003370void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCalla1ee0c52009-10-16 21:56:05 +00003371 // nothing to do
3372}
John McCall51bd8032009-10-18 01:05:36 +00003373void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003374 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
Douglas Gregorddf889a2010-01-18 18:04:31 +00003375 if (TL.needsExtraLocalData()) {
3376 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
3377 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
3378 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
3379 TL.setModeAttr(Record[Idx++]);
3380 }
John McCalla1ee0c52009-10-16 21:56:05 +00003381}
John McCall51bd8032009-10-18 01:05:36 +00003382void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003383 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003384}
John McCall51bd8032009-10-18 01:05:36 +00003385void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003386 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003387}
John McCall51bd8032009-10-18 01:05:36 +00003388void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003389 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003390}
John McCall51bd8032009-10-18 01:05:36 +00003391void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003392 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003393}
John McCall51bd8032009-10-18 01:05:36 +00003394void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003395 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003396}
John McCall51bd8032009-10-18 01:05:36 +00003397void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003398 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003399}
John McCall51bd8032009-10-18 01:05:36 +00003400void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003401 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
3402 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003403 if (Record[Idx++])
Sebastian Redlc3632732010-10-05 15:59:54 +00003404 TL.setSizeExpr(Reader.ReadExpr(F));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003405 else
John McCall51bd8032009-10-18 01:05:36 +00003406 TL.setSizeExpr(0);
3407}
3408void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
3409 VisitArrayTypeLoc(TL);
3410}
3411void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
3412 VisitArrayTypeLoc(TL);
3413}
3414void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
3415 VisitArrayTypeLoc(TL);
3416}
3417void TypeLocReader::VisitDependentSizedArrayTypeLoc(
3418 DependentSizedArrayTypeLoc TL) {
3419 VisitArrayTypeLoc(TL);
3420}
3421void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
3422 DependentSizedExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003423 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003424}
3425void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003426 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003427}
3428void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003429 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003430}
3431void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003432 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3433 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
Douglas Gregordab60ad2010-10-01 18:44:50 +00003434 TL.setTrailingReturn(Record[Idx++]);
John McCall51bd8032009-10-18 01:05:36 +00003435 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
John McCall86acc2a2009-10-23 01:28:53 +00003436 TL.setArg(i, cast_or_null<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
John McCall51bd8032009-10-18 01:05:36 +00003437 }
3438}
3439void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
3440 VisitFunctionTypeLoc(TL);
3441}
3442void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
3443 VisitFunctionTypeLoc(TL);
3444}
John McCalled976492009-12-04 22:46:56 +00003445void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003446 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalled976492009-12-04 22:46:56 +00003447}
John McCall51bd8032009-10-18 01:05:36 +00003448void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003449 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003450}
3451void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc 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));
John McCall51bd8032009-10-18 01:05:36 +00003455}
3456void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003457 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3458 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3459 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3460 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003461}
3462void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003463 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003464}
Richard Smith34b41d92011-02-20 03:19:35 +00003465void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
3466 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3467}
John McCall51bd8032009-10-18 01:05:36 +00003468void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003469 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003470}
3471void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003472 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003473}
John McCall9d156a72011-01-06 01:58:22 +00003474void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
3475 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
3476 if (TL.hasAttrOperand()) {
3477 SourceRange range;
3478 range.setBegin(ReadSourceLocation(Record, Idx));
3479 range.setEnd(ReadSourceLocation(Record, Idx));
3480 TL.setAttrOperandParensRange(range);
3481 }
3482 if (TL.hasAttrExprOperand()) {
3483 if (Record[Idx++])
3484 TL.setAttrExprOperand(Reader.ReadExpr(F));
3485 else
3486 TL.setAttrExprOperand(0);
3487 } else if (TL.hasAttrEnumOperand())
3488 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
3489}
John McCall51bd8032009-10-18 01:05:36 +00003490void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003491 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003492}
John McCall49a832b2009-10-18 09:09:24 +00003493void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
3494 SubstTemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003495 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall49a832b2009-10-18 09:09:24 +00003496}
Douglas Gregorc3069d62011-01-14 02:55:32 +00003497void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
3498 SubstTemplateTypeParmPackTypeLoc TL) {
3499 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3500}
John McCall51bd8032009-10-18 01:05:36 +00003501void TypeLocReader::VisitTemplateSpecializationTypeLoc(
3502 TemplateSpecializationTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003503 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
3504 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3505 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall833ca992009-10-29 08:12:44 +00003506 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
3507 TL.setArgLocInfo(i,
Sebastian Redlc3632732010-10-05 15:59:54 +00003508 Reader.GetTemplateArgumentLocInfo(F,
3509 TL.getTypePtr()->getArg(i).getKind(),
3510 Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003511}
Abramo Bagnara075f8f12010-12-10 16:29:40 +00003512void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
3513 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3514 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3515}
Abramo Bagnara465d41b2010-05-11 21:36:43 +00003516void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003517 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3518 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003519}
John McCall3cb0ebd2010-03-10 03:28:59 +00003520void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003521 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall3cb0ebd2010-03-10 03:28:59 +00003522}
Douglas Gregor4714c122010-03-31 17:34:00 +00003523void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003524 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3525 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
3526 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003527}
John McCall33500952010-06-11 00:33:02 +00003528void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
3529 DependentTemplateSpecializationTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003530 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3531 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
3532 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3533 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3534 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall33500952010-06-11 00:33:02 +00003535 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
3536 TL.setArgLocInfo(I,
Sebastian Redlc3632732010-10-05 15:59:54 +00003537 Reader.GetTemplateArgumentLocInfo(F,
3538 TL.getTypePtr()->getArg(I).getKind(),
3539 Record, Idx));
John McCall33500952010-06-11 00:33:02 +00003540}
Douglas Gregor7536dd52010-12-20 02:24:11 +00003541void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
3542 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
3543}
John McCall51bd8032009-10-18 01:05:36 +00003544void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003545 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallc12c5bb2010-05-15 11:32:37 +00003546}
3547void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
3548 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00003549 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3550 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003551 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Sebastian Redlc3632732010-10-05 15:59:54 +00003552 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003553}
John McCall54e14c42009-10-22 22:37:11 +00003554void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003555 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall54e14c42009-10-22 22:37:11 +00003556}
John McCalla1ee0c52009-10-16 21:56:05 +00003557
Sebastian Redlc3632732010-10-05 15:59:54 +00003558TypeSourceInfo *ASTReader::GetTypeSourceInfo(PerFileData &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00003559 const RecordData &Record,
John McCalla1ee0c52009-10-16 21:56:05 +00003560 unsigned &Idx) {
3561 QualType InfoTy = GetType(Record[Idx++]);
3562 if (InfoTy.isNull())
3563 return 0;
3564
John McCalla93c9342009-12-07 02:54:59 +00003565 TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy);
Sebastian Redlc3632732010-10-05 15:59:54 +00003566 TypeLocReader TLR(*this, F, Record, Idx);
John McCalla93c9342009-12-07 02:54:59 +00003567 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCalla1ee0c52009-10-16 21:56:05 +00003568 TLR.Visit(TL);
John McCalla93c9342009-12-07 02:54:59 +00003569 return TInfo;
John McCalla1ee0c52009-10-16 21:56:05 +00003570}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003571
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003572QualType ASTReader::GetType(TypeID ID) {
John McCall0953e762009-09-24 19:53:00 +00003573 unsigned FastQuals = ID & Qualifiers::FastMask;
3574 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003575
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003576 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003577 QualType T;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003578 switch ((PredefinedTypeIDs)Index) {
3579 case PREDEF_TYPE_NULL_ID: return QualType();
3580 case PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break;
3581 case PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003582
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003583 case PREDEF_TYPE_CHAR_U_ID:
3584 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregor2cf26342009-04-09 22:27:44 +00003585 // FIXME: Check that the signedness of CharTy is correct!
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003586 T = Context->CharTy;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003587 break;
3588
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003589 case PREDEF_TYPE_UCHAR_ID: T = Context->UnsignedCharTy; break;
3590 case PREDEF_TYPE_USHORT_ID: T = Context->UnsignedShortTy; break;
3591 case PREDEF_TYPE_UINT_ID: T = Context->UnsignedIntTy; break;
3592 case PREDEF_TYPE_ULONG_ID: T = Context->UnsignedLongTy; break;
3593 case PREDEF_TYPE_ULONGLONG_ID: T = Context->UnsignedLongLongTy; break;
3594 case PREDEF_TYPE_UINT128_ID: T = Context->UnsignedInt128Ty; break;
3595 case PREDEF_TYPE_SCHAR_ID: T = Context->SignedCharTy; break;
3596 case PREDEF_TYPE_WCHAR_ID: T = Context->WCharTy; break;
3597 case PREDEF_TYPE_SHORT_ID: T = Context->ShortTy; break;
3598 case PREDEF_TYPE_INT_ID: T = Context->IntTy; break;
3599 case PREDEF_TYPE_LONG_ID: T = Context->LongTy; break;
3600 case PREDEF_TYPE_LONGLONG_ID: T = Context->LongLongTy; break;
3601 case PREDEF_TYPE_INT128_ID: T = Context->Int128Ty; break;
3602 case PREDEF_TYPE_FLOAT_ID: T = Context->FloatTy; break;
3603 case PREDEF_TYPE_DOUBLE_ID: T = Context->DoubleTy; break;
3604 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy; break;
3605 case PREDEF_TYPE_OVERLOAD_ID: T = Context->OverloadTy; break;
3606 case PREDEF_TYPE_DEPENDENT_ID: T = Context->DependentTy; break;
3607 case PREDEF_TYPE_NULLPTR_ID: T = Context->NullPtrTy; break;
3608 case PREDEF_TYPE_CHAR16_ID: T = Context->Char16Ty; break;
3609 case PREDEF_TYPE_CHAR32_ID: T = Context->Char32Ty; break;
3610 case PREDEF_TYPE_OBJC_ID: T = Context->ObjCBuiltinIdTy; break;
3611 case PREDEF_TYPE_OBJC_CLASS: T = Context->ObjCBuiltinClassTy; break;
3612 case PREDEF_TYPE_OBJC_SEL: T = Context->ObjCBuiltinSelTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003613 }
3614
3615 assert(!T.isNull() && "Unknown predefined type");
John McCall0953e762009-09-24 19:53:00 +00003616 return T.withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003617 }
3618
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003619 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003620 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl07a353c2010-07-14 20:26:45 +00003621 if (TypesLoaded[Index].isNull()) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003622 TypesLoaded[Index] = ReadTypeRecord(Index);
Douglas Gregor97475832010-10-05 18:37:06 +00003623 if (TypesLoaded[Index].isNull())
3624 return QualType();
3625
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003626 TypesLoaded[Index]->setFromAST();
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003627 TypeIdxs[TypesLoaded[Index]] = TypeIdx::fromTypeID(ID);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003628 if (DeserializationListener)
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003629 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1476ed42010-07-16 16:36:56 +00003630 TypesLoaded[Index]);
Sebastian Redl07a353c2010-07-14 20:26:45 +00003631 }
Mike Stump1eb44332009-09-09 15:08:12 +00003632
John McCall0953e762009-09-24 19:53:00 +00003633 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003634}
3635
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003636TypeID ASTReader::GetTypeID(QualType T) const {
3637 return MakeTypeID(T,
3638 std::bind1st(std::mem_fun(&ASTReader::GetTypeIdx), this));
3639}
3640
3641TypeIdx ASTReader::GetTypeIdx(QualType T) const {
3642 if (T.isNull())
3643 return TypeIdx();
3644 assert(!T.getLocalFastQualifiers());
3645
3646 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3647 // GetTypeIdx is mostly used for computing the hash of DeclarationNames and
3648 // comparing keys of ASTDeclContextNameLookupTable.
3649 // If the type didn't come from the AST file use a specially marked index
3650 // so that any hash/key comparison fail since no such index is stored
3651 // in a AST file.
3652 if (I == TypeIdxs.end())
3653 return TypeIdx(-1);
3654 return I->second;
3655}
3656
Douglas Gregor7c789c12010-10-29 22:39:52 +00003657unsigned ASTReader::getTotalNumCXXBaseSpecifiers() const {
3658 unsigned Result = 0;
3659 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
3660 Result += Chain[I]->LocalNumCXXBaseSpecifiers;
3661
3662 return Result;
3663}
3664
John McCall833ca992009-10-29 08:12:44 +00003665TemplateArgumentLocInfo
Sebastian Redlc3632732010-10-05 15:59:54 +00003666ASTReader::GetTemplateArgumentLocInfo(PerFileData &F,
3667 TemplateArgument::ArgKind Kind,
John McCall833ca992009-10-29 08:12:44 +00003668 const RecordData &Record,
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00003669 unsigned &Index) {
John McCall833ca992009-10-29 08:12:44 +00003670 switch (Kind) {
3671 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00003672 return ReadExpr(F);
John McCall833ca992009-10-29 08:12:44 +00003673 case TemplateArgument::Type:
Sebastian Redlc3632732010-10-05 15:59:54 +00003674 return GetTypeSourceInfo(F, Record, Index);
Douglas Gregor788cd062009-11-11 01:00:40 +00003675 case TemplateArgument::Template: {
Sebastian Redlc3632732010-10-05 15:59:54 +00003676 SourceRange QualifierRange = ReadSourceRange(F, Record, Index);
3677 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003678 return TemplateArgumentLocInfo(QualifierRange, TemplateNameLoc,
3679 SourceLocation());
3680 }
3681 case TemplateArgument::TemplateExpansion: {
3682 SourceRange QualifierRange = ReadSourceRange(F, Record, Index);
3683 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorba68eca2011-01-05 17:40:24 +00003684 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
3685 return TemplateArgumentLocInfo(QualifierRange, TemplateNameLoc,
3686 EllipsisLoc);
Douglas Gregor788cd062009-11-11 01:00:40 +00003687 }
John McCall833ca992009-10-29 08:12:44 +00003688 case TemplateArgument::Null:
3689 case TemplateArgument::Integral:
3690 case TemplateArgument::Declaration:
3691 case TemplateArgument::Pack:
3692 return TemplateArgumentLocInfo();
3693 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00003694 llvm_unreachable("unexpected template argument loc");
John McCall833ca992009-10-29 08:12:44 +00003695 return TemplateArgumentLocInfo();
3696}
3697
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003698TemplateArgumentLoc
Sebastian Redlc3632732010-10-05 15:59:54 +00003699ASTReader::ReadTemplateArgumentLoc(PerFileData &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00003700 const RecordData &Record, unsigned &Index) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003701 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003702
3703 if (Arg.getKind() == TemplateArgument::Expression) {
3704 if (Record[Index++]) // bool InfoHasSameExpr.
3705 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
3706 }
Sebastian Redlc3632732010-10-05 15:59:54 +00003707 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00003708 Record, Index));
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003709}
3710
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003711Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall76bd1f32010-06-01 09:23:16 +00003712 return GetDecl(ID);
3713}
3714
Douglas Gregor7c789c12010-10-29 22:39:52 +00003715uint64_t
3716ASTReader::GetCXXBaseSpecifiersOffset(serialization::CXXBaseSpecifiersID ID) {
3717 if (ID == 0)
3718 return 0;
3719
3720 --ID;
3721 uint64_t Offset = 0;
3722 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3723 if (ID < Chain[I]->LocalNumCXXBaseSpecifiers)
3724 return Offset + Chain[I]->CXXBaseSpecifiersOffsets[ID];
3725
3726 ID -= Chain[I]->LocalNumCXXBaseSpecifiers;
3727 Offset += Chain[I]->SizeInBits;
3728 }
3729
3730 assert(false && "CXXBaseSpecifiers not found");
3731 return 0;
3732}
3733
3734CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
3735 // Figure out which AST file contains this offset.
3736 PerFileData *F = 0;
3737 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3738 if (Offset < Chain[I]->SizeInBits) {
3739 F = Chain[I];
3740 break;
3741 }
3742
3743 Offset -= Chain[I]->SizeInBits;
3744 }
3745
3746 if (!F) {
3747 Error("Malformed AST file: C++ base specifiers at impossible offset");
3748 return 0;
3749 }
3750
3751 llvm::BitstreamCursor &Cursor = F->DeclsCursor;
3752 SavedStreamPosition SavedPosition(Cursor);
3753 Cursor.JumpToBit(Offset);
3754 ReadingKindTracker ReadingKind(Read_Decl, *this);
3755 RecordData Record;
3756 unsigned Code = Cursor.ReadCode();
3757 unsigned RecCode = Cursor.ReadRecord(Code, Record);
3758 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
3759 Error("Malformed AST file: missing C++ base specifiers");
3760 return 0;
3761 }
3762
3763 unsigned Idx = 0;
3764 unsigned NumBases = Record[Idx++];
3765 void *Mem = Context->Allocate(sizeof(CXXBaseSpecifier) * NumBases);
3766 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
3767 for (unsigned I = 0; I != NumBases; ++I)
3768 Bases[I] = ReadCXXBaseSpecifier(*F, Record, Idx);
3769 return Bases;
3770}
3771
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003772TranslationUnitDecl *ASTReader::GetTranslationUnitDecl() {
Sebastian Redl30c514c2010-07-14 23:45:08 +00003773 if (!DeclsLoaded[0]) {
Sebastian Redle1dde812010-08-24 00:50:04 +00003774 ReadDeclRecord(0, 1);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003775 if (DeserializationListener)
Sebastian Redl1476ed42010-07-16 16:36:56 +00003776 DeserializationListener->DeclRead(1, DeclsLoaded[0]);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003777 }
Argyrios Kyrtzidis8871a442010-07-08 17:13:02 +00003778
3779 return cast<TranslationUnitDecl>(DeclsLoaded[0]);
3780}
3781
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003782Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003783 if (ID == 0)
3784 return 0;
3785
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003786 if (ID > DeclsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003787 Error("declaration ID out-of-range for AST file");
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003788 return 0;
3789 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003790
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003791 unsigned Index = ID - 1;
Sebastian Redl30c514c2010-07-14 23:45:08 +00003792 if (!DeclsLoaded[Index]) {
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00003793 ReadDeclRecord(Index, ID);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003794 if (DeserializationListener)
3795 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
3796 }
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003797
3798 return DeclsLoaded[Index];
Douglas Gregor2cf26342009-04-09 22:27:44 +00003799}
3800
Chris Lattner887e2b32009-04-27 05:46:25 +00003801/// \brief Resolve the offset of a statement into a statement.
3802///
3803/// This operation will read a new statement from the external
3804/// source each time it is called, and is meant to be used via a
3805/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003806Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00003807 // Switch case IDs are per Decl.
3808 ClearSwitchCaseIDs();
3809
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003810 // Offset here is a global offset across the entire chain.
3811 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3812 PerFileData &F = *Chain[N - I - 1];
3813 if (Offset < F.SizeInBits) {
3814 // Since we know that this statement is part of a decl, make sure to use
3815 // the decl cursor to read it.
3816 F.DeclsCursor.JumpToBit(Offset);
Sebastian Redlc3632732010-10-05 15:59:54 +00003817 return ReadStmtFromStream(F);
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003818 }
3819 Offset -= F.SizeInBits;
3820 }
3821 llvm_unreachable("Broken chain");
Douglas Gregor250fc9c2009-04-18 00:07:54 +00003822}
3823
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003824bool ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00003825 bool (*isKindWeWant)(Decl::Kind),
John McCall76bd1f32010-06-01 09:23:16 +00003826 llvm::SmallVectorImpl<Decl*> &Decls) {
Mike Stump1eb44332009-09-09 15:08:12 +00003827 assert(DC->hasExternalLexicalStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00003828 "DeclContext has no lexical decls in storage");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003829
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003830 // There might be lexical decls in multiple parts of the chain, for the TU
3831 // at least.
Sebastian Redl4a9eb262010-09-28 02:24:44 +00003832 // DeclContextOffsets might reallocate as we load additional decls below,
3833 // so make a copy of the vector.
3834 DeclContextInfos Infos = DeclContextOffsets[DC];
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003835 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3836 I != E; ++I) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003837 // IDs can be 0 if this context doesn't contain declarations.
3838 if (!I->LexicalDecls)
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003839 continue;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003840
3841 // Load all of the declaration IDs
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00003842 for (const KindDeclIDPair *ID = I->LexicalDecls,
3843 *IDE = ID + I->NumLexicalDecls; ID != IDE; ++ID) {
3844 if (isKindWeWant && !isKindWeWant((Decl::Kind)ID->first))
3845 continue;
3846
3847 Decl *D = GetDecl(ID->second);
Sebastian Redl4a9eb262010-09-28 02:24:44 +00003848 assert(D && "Null decl in lexical decls");
3849 Decls.push_back(D);
3850 }
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003851 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003852
Douglas Gregor25123082009-04-22 22:34:57 +00003853 ++NumLexicalDeclContextsRead;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003854 return false;
3855}
3856
John McCall76bd1f32010-06-01 09:23:16 +00003857DeclContext::lookup_result
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003858ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +00003859 DeclarationName Name) {
Mike Stump1eb44332009-09-09 15:08:12 +00003860 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00003861 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003862 if (!Name)
3863 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
3864 DeclContext::lookup_iterator(0));
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003865
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003866 llvm::SmallVector<NamedDecl *, 64> Decls;
Sebastian Redl8b122732010-08-24 00:49:55 +00003867 // There might be visible decls in multiple parts of the chain, for the TU
Sebastian Redl5967d622010-08-24 00:50:16 +00003868 // and namespaces. For any given name, the last available results replace
3869 // all earlier ones. For this reason, we walk in reverse.
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003870 DeclContextInfos &Infos = DeclContextOffsets[DC];
Sebastian Redl5967d622010-08-24 00:50:16 +00003871 for (DeclContextInfos::reverse_iterator I = Infos.rbegin(), E = Infos.rend();
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003872 I != E; ++I) {
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003873 if (!I->NameLookupTableData)
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003874 continue;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003875
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003876 ASTDeclContextNameLookupTable *LookupTable =
3877 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3878 ASTDeclContextNameLookupTable::iterator Pos = LookupTable->find(Name);
3879 if (Pos == LookupTable->end())
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003880 continue;
3881
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003882 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
3883 for (; Data.first != Data.second; ++Data.first)
3884 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
Sebastian Redl5967d622010-08-24 00:50:16 +00003885 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003886 }
3887
Douglas Gregor25123082009-04-22 22:34:57 +00003888 ++NumVisibleDeclContextsRead;
John McCall76bd1f32010-06-01 09:23:16 +00003889
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003890 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall76bd1f32010-06-01 09:23:16 +00003891 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003892}
3893
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +00003894void ASTReader::MaterializeVisibleDecls(const DeclContext *DC) {
3895 assert(DC->hasExternalVisibleStorage() &&
3896 "DeclContext has no visible decls in storage");
3897
3898 llvm::SmallVector<NamedDecl *, 64> Decls;
3899 // There might be visible decls in multiple parts of the chain, for the TU
3900 // and namespaces.
3901 DeclContextInfos &Infos = DeclContextOffsets[DC];
3902 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3903 I != E; ++I) {
3904 if (!I->NameLookupTableData)
3905 continue;
3906
3907 ASTDeclContextNameLookupTable *LookupTable =
3908 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3909 for (ASTDeclContextNameLookupTable::item_iterator
3910 ItemI = LookupTable->item_begin(),
3911 ItemEnd = LookupTable->item_end() ; ItemI != ItemEnd; ++ItemI) {
3912 ASTDeclContextNameLookupTable::item_iterator::value_type Val
3913 = *ItemI;
3914 ASTDeclContextNameLookupTrait::data_type Data = Val.second;
3915 Decls.clear();
3916 for (; Data.first != Data.second; ++Data.first)
3917 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
3918 MaterializeVisibleDeclsForName(DC, Val.first, Decls);
3919 }
3920 }
3921}
3922
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003923void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003924 assert(Consumer);
3925 while (!InterestingDecls.empty()) {
3926 DeclGroupRef DG(InterestingDecls.front());
3927 InterestingDecls.pop_front();
Sebastian Redl27372b42010-08-11 18:52:41 +00003928 Consumer->HandleInterestingDecl(DG);
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003929 }
3930}
3931
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003932void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor0af2ca42009-04-22 19:09:20 +00003933 this->Consumer = Consumer;
3934
Douglas Gregorfdd01722009-04-14 00:24:19 +00003935 if (!Consumer)
3936 return;
3937
3938 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003939 // Force deserialization of this decl, which will cause it to be queued for
3940 // passing to the consumer.
Daniel Dunbar04a0b502009-09-17 03:06:44 +00003941 GetDecl(ExternalDefinitions[I]);
Douglas Gregorfdd01722009-04-14 00:24:19 +00003942 }
Douglas Gregorc62a2fe2009-04-25 00:41:30 +00003943
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003944 PassInterestingDeclsToConsumer();
Douglas Gregorfdd01722009-04-14 00:24:19 +00003945}
3946
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003947void ASTReader::PrintStats() {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003948 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregor2cf26342009-04-09 22:27:44 +00003949
Mike Stump1eb44332009-09-09 15:08:12 +00003950 unsigned NumTypesLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003951 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall0953e762009-09-24 19:53:00 +00003952 QualType());
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003953 unsigned NumDeclsLoaded
3954 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
3955 (Decl *)0);
3956 unsigned NumIdentifiersLoaded
3957 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
3958 IdentifiersLoaded.end(),
3959 (IdentifierInfo *)0);
Mike Stump1eb44332009-09-09 15:08:12 +00003960 unsigned NumSelectorsLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003961 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
3962 SelectorsLoaded.end(),
3963 Selector());
Douglas Gregor2d41cc12009-04-13 20:50:16 +00003964
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003965 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
3966 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00003967 if (TotalNumSLocEntries)
3968 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
3969 NumSLocEntriesRead, TotalNumSLocEntries,
3970 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003971 if (!TypesLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003972 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003973 NumTypesLoaded, (unsigned)TypesLoaded.size(),
3974 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
3975 if (!DeclsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003976 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003977 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
3978 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003979 if (!IdentifiersLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003980 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003981 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
3982 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Sebastian Redl725cd962010-08-04 20:40:17 +00003983 if (!SelectorsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003984 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redl725cd962010-08-04 20:40:17 +00003985 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
3986 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor83941df2009-04-25 17:48:32 +00003987 if (TotalNumStatements)
3988 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
3989 NumStatementsRead, TotalNumStatements,
3990 ((float)NumStatementsRead/TotalNumStatements * 100));
3991 if (TotalNumMacros)
3992 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
3993 NumMacrosRead, TotalNumMacros,
3994 ((float)NumMacrosRead/TotalNumMacros * 100));
3995 if (TotalLexicalDeclContexts)
3996 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
3997 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
3998 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
3999 * 100));
4000 if (TotalVisibleDeclContexts)
4001 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
4002 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
4003 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
4004 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00004005 if (TotalNumMethodPoolEntries) {
Douglas Gregor83941df2009-04-25 17:48:32 +00004006 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redlfa78dec2010-08-04 21:22:45 +00004007 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
4008 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor83941df2009-04-25 17:48:32 +00004009 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00004010 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor83941df2009-04-25 17:48:32 +00004011 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00004012 std::fprintf(stderr, "\n");
4013}
4014
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004015void ASTReader::InitializeSema(Sema &S) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00004016 SemaObj = &S;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00004017 S.ExternalSource = this;
4018
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00004019 // Makes sure any declarations that were deserialized "too early"
4020 // still get added to the identifier's declaration chains.
Douglas Gregor76dc8892010-09-24 23:29:12 +00004021 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
4022 if (SemaObj->TUScope)
John McCalld226f652010-08-21 09:40:31 +00004023 SemaObj->TUScope->AddDecl(PreloadedDecls[I]);
Douglas Gregor76dc8892010-09-24 23:29:12 +00004024
4025 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregor668c1a42009-04-21 22:25:48 +00004026 }
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00004027 PreloadedDecls.clear();
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00004028
4029 // If there were any tentative definitions, deserialize them and add
Sebastian Redle9d12b62010-01-31 22:27:38 +00004030 // them to Sema's list of tentative definitions.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00004031 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
4032 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
Sebastian Redle9d12b62010-01-31 22:27:38 +00004033 SemaObj->TentativeDefinitions.push_back(Var);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00004034 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00004035
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00004036 // If there were any unused file scoped decls, deserialize them and add to
4037 // Sema's list of unused file scoped decls.
4038 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
4039 DeclaratorDecl *D = cast<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
4040 SemaObj->UnusedFileScopedDecls.push_back(D);
Tanya Lattnere6bbc012010-02-12 00:07:30 +00004041 }
Douglas Gregor14c22f22009-04-22 22:18:58 +00004042
4043 // If there were any locally-scoped external declarations,
4044 // deserialize them and add them to Sema's table of locally-scoped
4045 // external declarations.
4046 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
4047 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
4048 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
4049 }
Douglas Gregorb81c1702009-04-27 20:06:05 +00004050
4051 // If there were any ext_vector type declarations, deserialize them
4052 // and add them to Sema's vector of such declarations.
4053 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I)
4054 SemaObj->ExtVectorDecls.push_back(
4055 cast<TypedefDecl>(GetDecl(ExtVectorDecls[I])));
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004056
4057 // FIXME: Do VTable uses and dynamic classes deserialize too much ?
4058 // Can we cut them down before writing them ?
4059
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004060 // If there were any dynamic classes declarations, deserialize them
4061 // and add them to Sema's vector of such declarations.
4062 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I)
4063 SemaObj->DynamicClasses.push_back(
4064 cast<CXXRecordDecl>(GetDecl(DynamicClasses[I])));
Fariborz Jahanian32019832010-07-23 19:11:11 +00004065
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00004066 // Load the offsets of the declarations that Sema references.
4067 // They will be lazily deserialized when needed.
4068 if (!SemaDeclRefs.empty()) {
4069 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
4070 SemaObj->StdNamespace = SemaDeclRefs[0];
4071 SemaObj->StdBadAlloc = SemaDeclRefs[1];
4072 }
4073
Sebastian Redlc3632732010-10-05 15:59:54 +00004074 for (PerFileData *F = FirstInSource; F; F = F->NextInSource) {
4075
4076 // If there are @selector references added them to its pool. This is for
4077 // implementation of -Wselector.
4078 if (!F->ReferencedSelectorsData.empty()) {
4079 unsigned int DataSize = F->ReferencedSelectorsData.size()-1;
4080 unsigned I = 0;
4081 while (I < DataSize) {
4082 Selector Sel = DecodeSelector(F->ReferencedSelectorsData[I++]);
4083 SourceLocation SelLoc = ReadSourceLocation(
4084 *F, F->ReferencedSelectorsData, I);
4085 SemaObj->ReferencedSelectors.insert(std::make_pair(Sel, SelLoc));
4086 }
4087 }
4088
4089 // If there were any pending implicit instantiations, deserialize them
4090 // and add them to Sema's queue of such instantiations.
4091 assert(F->PendingInstantiations.size() % 2 == 0 &&
4092 "Expected pairs of entries");
4093 for (unsigned Idx = 0, N = F->PendingInstantiations.size(); Idx < N;) {
4094 ValueDecl *D=cast<ValueDecl>(GetDecl(F->PendingInstantiations[Idx++]));
4095 SourceLocation Loc = ReadSourceLocation(*F, F->PendingInstantiations,Idx);
4096 SemaObj->PendingInstantiations.push_back(std::make_pair(D, Loc));
4097 }
4098 }
4099
4100 // The two special data sets below always come from the most recent PCH,
4101 // which is at the front of the chain.
4102 PerFileData &F = *Chain.front();
4103
4104 // If there were any weak undeclared identifiers, deserialize them and add to
4105 // Sema's list of weak undeclared identifiers.
4106 if (!WeakUndeclaredIdentifiers.empty()) {
4107 unsigned Idx = 0;
4108 for (unsigned I = 0, N = WeakUndeclaredIdentifiers[Idx++]; I != N; ++I) {
4109 IdentifierInfo *WeakId = GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
4110 IdentifierInfo *AliasId= GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
4111 SourceLocation Loc = ReadSourceLocation(F, WeakUndeclaredIdentifiers,Idx);
4112 bool Used = WeakUndeclaredIdentifiers[Idx++];
4113 Sema::WeakInfo WI(AliasId, Loc);
4114 WI.setUsed(Used);
4115 SemaObj->WeakUndeclaredIdentifiers.insert(std::make_pair(WeakId, WI));
4116 }
4117 }
4118
4119 // If there were any VTable uses, deserialize the information and add it
4120 // to Sema's vector and map of VTable uses.
4121 if (!VTableUses.empty()) {
4122 unsigned Idx = 0;
4123 for (unsigned I = 0, N = VTableUses[Idx++]; I != N; ++I) {
4124 CXXRecordDecl *Class = cast<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
4125 SourceLocation Loc = ReadSourceLocation(F, VTableUses, Idx);
4126 bool DefinitionRequired = VTableUses[Idx++];
4127 SemaObj->VTableUses.push_back(std::make_pair(Class, Loc));
4128 SemaObj->VTablesUsed[Class] = DefinitionRequired;
Fariborz Jahanian32019832010-07-23 19:11:11 +00004129 }
4130 }
Peter Collingbourne84bccea2011-02-15 19:46:30 +00004131
4132 if (!FPPragmaOptions.empty()) {
4133 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
4134 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
4135 }
4136
4137 if (!OpenCLExtensions.empty()) {
4138 unsigned I = 0;
4139#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
4140#include "clang/Basic/OpenCLExtensions.def"
4141
4142 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
4143 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00004144}
4145
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004146IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Sebastian Redld8c5abb2010-08-02 18:30:12 +00004147 // Try to find this name within our on-disk hash tables. We start with the
4148 // most recent one, since that one contains the most up-to-date info.
Sebastian Redld27d3fc2010-07-21 22:31:37 +00004149 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004150 ASTIdentifierLookupTable *IdTable
4151 = (ASTIdentifierLookupTable *)Chain[I]->IdentifierLookupTable;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00004152 if (!IdTable)
4153 continue;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00004154 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004155 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00004156 if (Pos == IdTable->end())
4157 continue;
Douglas Gregor668c1a42009-04-21 22:25:48 +00004158
Sebastian Redld27d3fc2010-07-21 22:31:37 +00004159 // Dereferencing the iterator has the effect of building the
4160 // IdentifierInfo node and populating it with the various
4161 // declarations it needs.
Sebastian Redld8c5abb2010-08-02 18:30:12 +00004162 return *Pos;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00004163 }
Sebastian Redld8c5abb2010-08-02 18:30:12 +00004164 return 0;
Douglas Gregor668c1a42009-04-21 22:25:48 +00004165}
4166
Douglas Gregor95f42922010-10-14 22:11:03 +00004167namespace clang {
4168 /// \brief An identifier-lookup iterator that enumerates all of the
4169 /// identifiers stored within a set of AST files.
4170 class ASTIdentifierIterator : public IdentifierIterator {
4171 /// \brief The AST reader whose identifiers are being enumerated.
4172 const ASTReader &Reader;
4173
4174 /// \brief The current index into the chain of AST files stored in
4175 /// the AST reader.
4176 unsigned Index;
4177
4178 /// \brief The current position within the identifier lookup table
4179 /// of the current AST file.
4180 ASTIdentifierLookupTable::key_iterator Current;
4181
4182 /// \brief The end position within the identifier lookup table of
4183 /// the current AST file.
4184 ASTIdentifierLookupTable::key_iterator End;
4185
4186 public:
4187 explicit ASTIdentifierIterator(const ASTReader &Reader);
4188
4189 virtual llvm::StringRef Next();
4190 };
4191}
4192
4193ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
4194 : Reader(Reader), Index(Reader.Chain.size() - 1) {
4195 ASTIdentifierLookupTable *IdTable
4196 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
4197 Current = IdTable->key_begin();
4198 End = IdTable->key_end();
4199}
4200
4201llvm::StringRef ASTIdentifierIterator::Next() {
4202 while (Current == End) {
4203 // If we have exhausted all of our AST files, we're done.
4204 if (Index == 0)
4205 return llvm::StringRef();
4206
4207 --Index;
4208 ASTIdentifierLookupTable *IdTable
4209 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
4210 Current = IdTable->key_begin();
4211 End = IdTable->key_end();
4212 }
4213
4214 // We have any identifiers remaining in the current AST file; return
4215 // the next one.
4216 std::pair<const char*, unsigned> Key = *Current;
4217 ++Current;
4218 return llvm::StringRef(Key.first, Key.second);
4219}
4220
4221IdentifierIterator *ASTReader::getIdentifiers() const {
4222 return new ASTIdentifierIterator(*this);
4223}
4224
Mike Stump1eb44332009-09-09 15:08:12 +00004225std::pair<ObjCMethodList, ObjCMethodList>
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004226ASTReader::ReadMethodPool(Selector Sel) {
Sebastian Redl725cd962010-08-04 20:40:17 +00004227 // Find this selector in a hash table. We want to find the most recent entry.
4228 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4229 PerFileData &F = *Chain[I];
4230 if (!F.SelectorLookupTable)
4231 continue;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00004232
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004233 ASTSelectorLookupTable *PoolTable
4234 = (ASTSelectorLookupTable*)F.SelectorLookupTable;
4235 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
Sebastian Redl725cd962010-08-04 20:40:17 +00004236 if (Pos != PoolTable->end()) {
4237 ++NumSelectorsRead;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00004238 // FIXME: Not quite happy with the statistics here. We probably should
4239 // disable this tracking when called via LoadSelector.
4240 // Also, should entries without methods count as misses?
4241 ++NumMethodPoolEntriesRead;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004242 ASTSelectorLookupTrait::data_type Data = *Pos;
Sebastian Redl725cd962010-08-04 20:40:17 +00004243 if (DeserializationListener)
4244 DeserializationListener->SelectorRead(Data.ID, Sel);
4245 return std::make_pair(Data.Instance, Data.Factory);
4246 }
Douglas Gregor83941df2009-04-25 17:48:32 +00004247 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00004248
Sebastian Redlfa78dec2010-08-04 21:22:45 +00004249 ++NumMethodPoolMisses;
Sebastian Redl725cd962010-08-04 20:40:17 +00004250 return std::pair<ObjCMethodList, ObjCMethodList>();
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00004251}
4252
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004253void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redle58aa892010-08-04 18:21:41 +00004254 // It would be complicated to avoid reading the methods anyway. So don't.
4255 ReadMethodPool(Sel);
4256}
4257
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004258void ASTReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00004259 assert(ID && "Non-zero identifier ID required");
Douglas Gregora02b1472009-04-28 21:53:25 +00004260 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00004261 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004262 if (DeserializationListener)
4263 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregor668c1a42009-04-21 22:25:48 +00004264}
4265
Douglas Gregord89275b2009-07-06 18:54:52 +00004266/// \brief Set the globally-visible declarations associated with the given
4267/// identifier.
4268///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004269/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump1eb44332009-09-09 15:08:12 +00004270/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregord89275b2009-07-06 18:54:52 +00004271/// them.
4272///
4273/// \param II an IdentifierInfo that refers to one or more globally-visible
4274/// declarations.
4275///
4276/// \param DeclIDs the set of declaration IDs with the name @p II that are
4277/// visible at global scope.
4278///
4279/// \param Nonrecursive should be true to indicate that the caller knows that
4280/// this call is non-recursive, and therefore the globally-visible declarations
4281/// will not be placed onto the pending queue.
Mike Stump1eb44332009-09-09 15:08:12 +00004282void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004283ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Douglas Gregord89275b2009-07-06 18:54:52 +00004284 const llvm::SmallVectorImpl<uint32_t> &DeclIDs,
4285 bool Nonrecursive) {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004286 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregord89275b2009-07-06 18:54:52 +00004287 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
4288 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
4289 PII.II = II;
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00004290 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregord89275b2009-07-06 18:54:52 +00004291 return;
4292 }
Mike Stump1eb44332009-09-09 15:08:12 +00004293
Douglas Gregord89275b2009-07-06 18:54:52 +00004294 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
4295 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
4296 if (SemaObj) {
Douglas Gregor914ed9d2010-08-13 03:15:25 +00004297 if (SemaObj->TUScope) {
4298 // Introduce this declaration into the translation-unit scope
4299 // and add it to the declaration chain for this identifier, so
4300 // that (unqualified) name lookup will find it.
John McCalld226f652010-08-21 09:40:31 +00004301 SemaObj->TUScope->AddDecl(D);
Douglas Gregor914ed9d2010-08-13 03:15:25 +00004302 }
Douglas Gregor76dc8892010-09-24 23:29:12 +00004303 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
Douglas Gregord89275b2009-07-06 18:54:52 +00004304 } else {
4305 // Queue this declaration so that it will be added to the
4306 // translation unit scope and identifier's declaration chain
4307 // once a Sema object is known.
4308 PreloadedDecls.push_back(D);
4309 }
4310 }
4311}
4312
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004313IdentifierInfo *ASTReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00004314 if (ID == 0)
4315 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00004316
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00004317 if (IdentifiersLoaded.empty()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004318 Error("no identifier table in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00004319 return 0;
4320 }
Mike Stump1eb44332009-09-09 15:08:12 +00004321
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00004322 assert(PP && "Forgot to set Preprocessor ?");
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00004323 ID -= 1;
4324 if (!IdentifiersLoaded[ID]) {
4325 unsigned Index = ID;
4326 const char *Str = 0;
4327 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4328 PerFileData *F = Chain[N - I - 1];
4329 if (Index < F->LocalNumIdentifiers) {
4330 uint32_t Offset = F->IdentifierOffsets[Index];
4331 Str = F->IdentifierTableData + Offset;
4332 break;
4333 }
4334 Index -= F->LocalNumIdentifiers;
4335 }
4336 assert(Str && "Broken Chain");
Douglas Gregord6595a42009-04-25 21:04:17 +00004337
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004338 // All of the strings in the AST file are preceded by a 16-bit length.
4339 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenek231bc0b2009-10-23 04:45:31 +00004340 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
4341 // unsigned integers. This is important to avoid integer overflow when
4342 // we cast them to 'unsigned'.
Ted Kremenekff1ea462009-10-23 03:57:22 +00004343 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregor02fc7512009-04-28 20:01:51 +00004344 unsigned StrLen = (((unsigned) StrLenPtr[0])
4345 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00004346 IdentifiersLoaded[ID]
Kovarththanan Rajaratnam811f4262010-03-12 10:32:27 +00004347 = &PP->getIdentifierTable().get(Str, StrLen);
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004348 if (DeserializationListener)
4349 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregorafaf3082009-04-11 00:14:32 +00004350 }
Mike Stump1eb44332009-09-09 15:08:12 +00004351
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00004352 return IdentifiersLoaded[ID];
Douglas Gregor2cf26342009-04-09 22:27:44 +00004353}
4354
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004355void ASTReader::ReadSLocEntry(unsigned ID) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00004356 ReadSLocEntryRecord(ID);
4357}
4358
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004359Selector ASTReader::DecodeSelector(unsigned ID) {
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004360 if (ID == 0)
4361 return Selector();
Mike Stump1eb44332009-09-09 15:08:12 +00004362
Sebastian Redl725cd962010-08-04 20:40:17 +00004363 if (ID > SelectorsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004364 Error("selector ID out of range in AST file");
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004365 return Selector();
4366 }
Douglas Gregor83941df2009-04-25 17:48:32 +00004367
Sebastian Redl725cd962010-08-04 20:40:17 +00004368 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor83941df2009-04-25 17:48:32 +00004369 // Load this selector from the selector table.
Sebastian Redl725cd962010-08-04 20:40:17 +00004370 unsigned Idx = ID - 1;
4371 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4372 PerFileData &F = *Chain[N - I - 1];
4373 if (Idx < F.LocalNumSelectors) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004374 ASTSelectorLookupTrait Trait(*this);
Sebastian Redl725cd962010-08-04 20:40:17 +00004375 SelectorsLoaded[ID - 1] =
4376 Trait.ReadKey(F.SelectorLookupTableData + F.SelectorOffsets[Idx], 0);
4377 if (DeserializationListener)
4378 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
4379 break;
4380 }
4381 Idx -= F.LocalNumSelectors;
4382 }
Douglas Gregor83941df2009-04-25 17:48:32 +00004383 }
4384
Sebastian Redl725cd962010-08-04 20:40:17 +00004385 return SelectorsLoaded[ID - 1];
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004386}
4387
Michael J. Spencer20249a12010-10-21 03:16:25 +00004388Selector ASTReader::GetExternalSelector(uint32_t ID) {
Douglas Gregor719770d2010-04-06 17:30:22 +00004389 return DecodeSelector(ID);
4390}
4391
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004392uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redl725cd962010-08-04 20:40:17 +00004393 // ID 0 (the null selector) is considered an external selector.
4394 return getTotalNumSelectors() + 1;
Douglas Gregor719770d2010-04-06 17:30:22 +00004395}
4396
Mike Stump1eb44332009-09-09 15:08:12 +00004397DeclarationName
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004398ASTReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00004399 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
4400 switch (Kind) {
4401 case DeclarationName::Identifier:
4402 return DeclarationName(GetIdentifierInfo(Record, Idx));
4403
4404 case DeclarationName::ObjCZeroArgSelector:
4405 case DeclarationName::ObjCOneArgSelector:
4406 case DeclarationName::ObjCMultiArgSelector:
Steve Naroffa7503a72009-04-23 15:15:40 +00004407 return DeclarationName(GetSelector(Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004408
4409 case DeclarationName::CXXConstructorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00004410 return Context->DeclarationNames.getCXXConstructorName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00004411 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004412
4413 case DeclarationName::CXXDestructorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00004414 return Context->DeclarationNames.getCXXDestructorName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00004415 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004416
4417 case DeclarationName::CXXConversionFunctionName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00004418 return Context->DeclarationNames.getCXXConversionFunctionName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00004419 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004420
4421 case DeclarationName::CXXOperatorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00004422 return Context->DeclarationNames.getCXXOperatorName(
Douglas Gregor2cf26342009-04-09 22:27:44 +00004423 (OverloadedOperatorKind)Record[Idx++]);
4424
Sean Hunt3e518bd2009-11-29 07:34:05 +00004425 case DeclarationName::CXXLiteralOperatorName:
4426 return Context->DeclarationNames.getCXXLiteralOperatorName(
4427 GetIdentifierInfo(Record, Idx));
4428
Douglas Gregor2cf26342009-04-09 22:27:44 +00004429 case DeclarationName::CXXUsingDirective:
4430 return DeclarationName::getUsingDirectiveName();
4431 }
4432
4433 // Required to silence GCC warning
4434 return DeclarationName();
4435}
Douglas Gregor0a0428e2009-04-10 20:39:37 +00004436
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004437void ASTReader::ReadDeclarationNameLoc(PerFileData &F,
4438 DeclarationNameLoc &DNLoc,
4439 DeclarationName Name,
4440 const RecordData &Record, unsigned &Idx) {
4441 switch (Name.getNameKind()) {
4442 case DeclarationName::CXXConstructorName:
4443 case DeclarationName::CXXDestructorName:
4444 case DeclarationName::CXXConversionFunctionName:
4445 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
4446 break;
4447
4448 case DeclarationName::CXXOperatorName:
4449 DNLoc.CXXOperatorName.BeginOpNameLoc
4450 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4451 DNLoc.CXXOperatorName.EndOpNameLoc
4452 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4453 break;
4454
4455 case DeclarationName::CXXLiteralOperatorName:
4456 DNLoc.CXXLiteralOperatorName.OpNameLoc
4457 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4458 break;
4459
4460 case DeclarationName::Identifier:
4461 case DeclarationName::ObjCZeroArgSelector:
4462 case DeclarationName::ObjCOneArgSelector:
4463 case DeclarationName::ObjCMultiArgSelector:
4464 case DeclarationName::CXXUsingDirective:
4465 break;
4466 }
4467}
4468
4469void ASTReader::ReadDeclarationNameInfo(PerFileData &F,
4470 DeclarationNameInfo &NameInfo,
4471 const RecordData &Record, unsigned &Idx) {
4472 NameInfo.setName(ReadDeclarationName(Record, Idx));
4473 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
4474 DeclarationNameLoc DNLoc;
4475 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
4476 NameInfo.setInfo(DNLoc);
4477}
4478
4479void ASTReader::ReadQualifierInfo(PerFileData &F, QualifierInfo &Info,
4480 const RecordData &Record, unsigned &Idx) {
4481 Info.NNS = ReadNestedNameSpecifier(Record, Idx);
4482 Info.NNSRange = ReadSourceRange(F, Record, Idx);
4483 unsigned NumTPLists = Record[Idx++];
4484 Info.NumTemplParamLists = NumTPLists;
4485 if (NumTPLists) {
4486 Info.TemplParamLists = new (*Context) TemplateParameterList*[NumTPLists];
4487 for (unsigned i=0; i != NumTPLists; ++i)
4488 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
4489 }
4490}
4491
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004492TemplateName
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004493ASTReader::ReadTemplateName(PerFileData &F, const RecordData &Record,
4494 unsigned &Idx) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00004495 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004496 switch (Kind) {
4497 case TemplateName::Template:
4498 return TemplateName(cast_or_null<TemplateDecl>(GetDecl(Record[Idx++])));
4499
4500 case TemplateName::OverloadedTemplate: {
4501 unsigned size = Record[Idx++];
4502 UnresolvedSet<8> Decls;
4503 while (size--)
4504 Decls.addDecl(cast<NamedDecl>(GetDecl(Record[Idx++])));
4505
4506 return Context->getOverloadedTemplateName(Decls.begin(), Decls.end());
4507 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004508
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004509 case TemplateName::QualifiedTemplate: {
4510 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
4511 bool hasTemplKeyword = Record[Idx++];
4512 TemplateDecl *Template = cast<TemplateDecl>(GetDecl(Record[Idx++]));
4513 return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
4514 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004515
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004516 case TemplateName::DependentTemplate: {
4517 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
4518 if (Record[Idx++]) // isIdentifier
4519 return Context->getDependentTemplateName(NNS,
4520 GetIdentifierInfo(Record, Idx));
4521 return Context->getDependentTemplateName(NNS,
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004522 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004523 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004524
4525 case TemplateName::SubstTemplateTemplateParmPack: {
4526 TemplateTemplateParmDecl *Param
4527 = cast_or_null<TemplateTemplateParmDecl>(GetDecl(Record[Idx++]));
4528 if (!Param)
4529 return TemplateName();
4530
4531 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
4532 if (ArgPack.getKind() != TemplateArgument::Pack)
4533 return TemplateName();
4534
4535 return Context->getSubstTemplateTemplateParmPack(Param, ArgPack);
4536 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004537 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004538
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004539 assert(0 && "Unhandled template name kind!");
4540 return TemplateName();
4541}
4542
4543TemplateArgument
Sebastian Redlc3632732010-10-05 15:59:54 +00004544ASTReader::ReadTemplateArgument(PerFileData &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00004545 const RecordData &Record, unsigned &Idx) {
Douglas Gregora7fc9012011-01-05 18:58:31 +00004546 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
4547 switch (Kind) {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004548 case TemplateArgument::Null:
4549 return TemplateArgument();
4550 case TemplateArgument::Type:
4551 return TemplateArgument(GetType(Record[Idx++]));
4552 case TemplateArgument::Declaration:
4553 return TemplateArgument(GetDecl(Record[Idx++]));
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00004554 case TemplateArgument::Integral: {
4555 llvm::APSInt Value = ReadAPSInt(Record, Idx);
4556 QualType T = GetType(Record[Idx++]);
4557 return TemplateArgument(Value, T);
4558 }
Douglas Gregora7fc9012011-01-05 18:58:31 +00004559 case TemplateArgument::Template:
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004560 return TemplateArgument(ReadTemplateName(F, Record, Idx));
Douglas Gregora7fc9012011-01-05 18:58:31 +00004561 case TemplateArgument::TemplateExpansion: {
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004562 TemplateName Name = ReadTemplateName(F, Record, Idx);
Douglas Gregor2be29f42011-01-14 23:41:42 +00004563 llvm::Optional<unsigned> NumTemplateExpansions;
4564 if (unsigned NumExpansions = Record[Idx++])
4565 NumTemplateExpansions = NumExpansions - 1;
4566 return TemplateArgument(Name, NumTemplateExpansions);
Douglas Gregorba68eca2011-01-05 17:40:24 +00004567 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004568 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00004569 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004570 case TemplateArgument::Pack: {
4571 unsigned NumArgs = Record[Idx++];
Douglas Gregor910f8002010-11-07 23:05:16 +00004572 TemplateArgument *Args = new (*Context) TemplateArgument[NumArgs];
4573 for (unsigned I = 0; I != NumArgs; ++I)
4574 Args[I] = ReadTemplateArgument(F, Record, Idx);
4575 return TemplateArgument(Args, NumArgs);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004576 }
4577 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004578
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004579 assert(0 && "Unhandled template argument kind!");
4580 return TemplateArgument();
4581}
4582
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004583TemplateParameterList *
Sebastian Redlc3632732010-10-05 15:59:54 +00004584ASTReader::ReadTemplateParameterList(PerFileData &F,
4585 const RecordData &Record, unsigned &Idx) {
4586 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
4587 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
4588 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004589
4590 unsigned NumParams = Record[Idx++];
4591 llvm::SmallVector<NamedDecl *, 16> Params;
4592 Params.reserve(NumParams);
4593 while (NumParams--)
4594 Params.push_back(cast<NamedDecl>(GetDecl(Record[Idx++])));
Michael J. Spencer20249a12010-10-21 03:16:25 +00004595
4596 TemplateParameterList* TemplateParams =
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004597 TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc,
4598 Params.data(), Params.size(), RAngleLoc);
4599 return TemplateParams;
4600}
4601
4602void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004603ASTReader::
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004604ReadTemplateArgumentList(llvm::SmallVector<TemplateArgument, 8> &TemplArgs,
Sebastian Redlc3632732010-10-05 15:59:54 +00004605 PerFileData &F, const RecordData &Record,
4606 unsigned &Idx) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004607 unsigned NumTemplateArgs = Record[Idx++];
4608 TemplArgs.reserve(NumTemplateArgs);
4609 while (NumTemplateArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00004610 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004611}
4612
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004613/// \brief Read a UnresolvedSet structure.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004614void ASTReader::ReadUnresolvedSet(UnresolvedSetImpl &Set,
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004615 const RecordData &Record, unsigned &Idx) {
4616 unsigned NumDecls = Record[Idx++];
4617 while (NumDecls--) {
4618 NamedDecl *D = cast<NamedDecl>(GetDecl(Record[Idx++]));
4619 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
4620 Set.addDecl(D, AS);
4621 }
4622}
4623
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004624CXXBaseSpecifier
Sebastian Redlc3632732010-10-05 15:59:54 +00004625ASTReader::ReadCXXBaseSpecifier(PerFileData &F,
Nick Lewycky56062202010-07-26 16:56:01 +00004626 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004627 bool isVirtual = static_cast<bool>(Record[Idx++]);
4628 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
4629 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redlf677ea32011-02-05 19:23:19 +00004630 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00004631 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
4632 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00004633 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redlf677ea32011-02-05 19:23:19 +00004634 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
Douglas Gregorf90b27a2011-01-03 22:36:02 +00004635 EllipsisLoc);
Sebastian Redlf677ea32011-02-05 19:23:19 +00004636 Result.setInheritConstructors(inheritConstructors);
4637 return Result;
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004638}
4639
Sean Huntcbb67482011-01-08 20:30:50 +00004640std::pair<CXXCtorInitializer **, unsigned>
4641ASTReader::ReadCXXCtorInitializers(PerFileData &F, const RecordData &Record,
4642 unsigned &Idx) {
4643 CXXCtorInitializer **CtorInitializers = 0;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004644 unsigned NumInitializers = Record[Idx++];
4645 if (NumInitializers) {
4646 ASTContext &C = *getContext();
4647
Sean Huntcbb67482011-01-08 20:30:50 +00004648 CtorInitializers
4649 = new (C) CXXCtorInitializer*[NumInitializers];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004650 for (unsigned i=0; i != NumInitializers; ++i) {
4651 TypeSourceInfo *BaseClassInfo = 0;
4652 bool IsBaseVirtual = false;
4653 FieldDecl *Member = 0;
Francois Pichet00eb3f92010-12-04 09:14:42 +00004654 IndirectFieldDecl *IndirectMember = 0;
Michael J. Spencer20249a12010-10-21 03:16:25 +00004655
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004656 bool IsBaseInitializer = Record[Idx++];
4657 if (IsBaseInitializer) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004658 BaseClassInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004659 IsBaseVirtual = Record[Idx++];
4660 } else {
Francois Pichet00eb3f92010-12-04 09:14:42 +00004661 bool IsIndirectMemberInitializer = Record[Idx++];
4662 if (IsIndirectMemberInitializer)
4663 IndirectMember = cast<IndirectFieldDecl>(GetDecl(Record[Idx++]));
4664 else
4665 Member = cast<FieldDecl>(GetDecl(Record[Idx++]));
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004666 }
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00004667 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redlc3632732010-10-05 15:59:54 +00004668 Expr *Init = ReadExpr(F);
Sebastian Redlc3632732010-10-05 15:59:54 +00004669 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
4670 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004671 bool IsWritten = Record[Idx++];
4672 unsigned SourceOrderOrNumArrayIndices;
4673 llvm::SmallVector<VarDecl *, 8> Indices;
4674 if (IsWritten) {
4675 SourceOrderOrNumArrayIndices = Record[Idx++];
4676 } else {
4677 SourceOrderOrNumArrayIndices = Record[Idx++];
4678 Indices.reserve(SourceOrderOrNumArrayIndices);
4679 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
4680 Indices.push_back(cast<VarDecl>(GetDecl(Record[Idx++])));
4681 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004682
Sean Huntcbb67482011-01-08 20:30:50 +00004683 CXXCtorInitializer *BOMInit;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004684 if (IsBaseInitializer) {
Sean Huntcbb67482011-01-08 20:30:50 +00004685 BOMInit = new (C) CXXCtorInitializer(C, BaseClassInfo, IsBaseVirtual,
4686 LParenLoc, Init, RParenLoc,
4687 MemberOrEllipsisLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004688 } else if (IsWritten) {
Francois Pichet00eb3f92010-12-04 09:14:42 +00004689 if (Member)
Sean Huntcbb67482011-01-08 20:30:50 +00004690 BOMInit = new (C) CXXCtorInitializer(C, Member, MemberOrEllipsisLoc,
4691 LParenLoc, Init, RParenLoc);
Francois Pichet00eb3f92010-12-04 09:14:42 +00004692 else
Sean Huntcbb67482011-01-08 20:30:50 +00004693 BOMInit = new (C) CXXCtorInitializer(C, IndirectMember,
4694 MemberOrEllipsisLoc, LParenLoc,
4695 Init, RParenLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004696 } else {
Sean Huntcbb67482011-01-08 20:30:50 +00004697 BOMInit = CXXCtorInitializer::Create(C, Member, MemberOrEllipsisLoc,
4698 LParenLoc, Init, RParenLoc,
4699 Indices.data(), Indices.size());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004700 }
4701
Argyrios Kyrtzidisf84cde12010-09-06 19:04:27 +00004702 if (IsWritten)
4703 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Sean Huntcbb67482011-01-08 20:30:50 +00004704 CtorInitializers[i] = BOMInit;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004705 }
4706 }
4707
Sean Huntcbb67482011-01-08 20:30:50 +00004708 return std::make_pair(CtorInitializers, NumInitializers);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004709}
4710
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004711NestedNameSpecifier *
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004712ASTReader::ReadNestedNameSpecifier(const RecordData &Record, unsigned &Idx) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004713 unsigned N = Record[Idx++];
4714 NestedNameSpecifier *NNS = 0, *Prev = 0;
4715 for (unsigned I = 0; I != N; ++I) {
4716 NestedNameSpecifier::SpecifierKind Kind
4717 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
4718 switch (Kind) {
4719 case NestedNameSpecifier::Identifier: {
4720 IdentifierInfo *II = GetIdentifierInfo(Record, Idx);
4721 NNS = NestedNameSpecifier::Create(*Context, Prev, II);
4722 break;
4723 }
4724
4725 case NestedNameSpecifier::Namespace: {
4726 NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++]));
4727 NNS = NestedNameSpecifier::Create(*Context, Prev, NS);
4728 break;
4729 }
4730
4731 case NestedNameSpecifier::TypeSpec:
4732 case NestedNameSpecifier::TypeSpecWithTemplate: {
John McCallf4c73712011-01-19 06:33:43 +00004733 const Type *T = GetType(Record[Idx++]).getTypePtrOrNull();
Douglas Gregor1ab55e92010-12-10 17:03:06 +00004734 if (!T)
4735 return 0;
4736
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004737 bool Template = Record[Idx++];
4738 NNS = NestedNameSpecifier::Create(*Context, Prev, Template, T);
4739 break;
4740 }
4741
4742 case NestedNameSpecifier::Global: {
4743 NNS = NestedNameSpecifier::GlobalSpecifier(*Context);
4744 // No associated value, and there can't be a prefix.
4745 break;
4746 }
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004747 }
Argyrios Kyrtzidisd2bb2c02010-07-07 15:46:30 +00004748 Prev = NNS;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004749 }
4750 return NNS;
4751}
4752
4753SourceRange
Sebastian Redlc3632732010-10-05 15:59:54 +00004754ASTReader::ReadSourceRange(PerFileData &F, const RecordData &Record,
4755 unsigned &Idx) {
4756 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
4757 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar8ee59392010-06-02 15:47:10 +00004758 return SourceRange(beg, end);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004759}
4760
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004761/// \brief Read an integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004762llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004763 unsigned BitWidth = Record[Idx++];
4764 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
4765 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
4766 Idx += NumWords;
4767 return Result;
4768}
4769
4770/// \brief Read a signed integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004771llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004772 bool isUnsigned = Record[Idx++];
4773 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
4774}
4775
Douglas Gregor17fc2232009-04-14 21:55:33 +00004776/// \brief Read a floating-point value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004777llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00004778 return llvm::APFloat(ReadAPInt(Record, Idx));
4779}
4780
Douglas Gregor68a2eb02009-04-15 21:30:51 +00004781// \brief Read a string
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004782std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00004783 unsigned Len = Record[Idx++];
Jay Foadbeaaccd2009-05-21 09:52:38 +00004784 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00004785 Idx += Len;
4786 return Result;
4787}
4788
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004789CXXTemporary *ASTReader::ReadCXXTemporary(const RecordData &Record,
Chris Lattnerd2598362010-05-10 00:25:06 +00004790 unsigned &Idx) {
4791 CXXDestructorDecl *Decl = cast<CXXDestructorDecl>(GetDecl(Record[Idx++]));
4792 return CXXTemporary::Create(*Context, Decl);
4793}
4794
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004795DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00004796 return Diag(SourceLocation(), DiagID);
4797}
4798
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004799DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004800 return Diags.Report(Loc, DiagID);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00004801}
Douglas Gregor025452f2009-04-17 00:04:06 +00004802
Douglas Gregor668c1a42009-04-21 22:25:48 +00004803/// \brief Retrieve the identifier table associated with the
4804/// preprocessor.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004805IdentifierTable &ASTReader::getIdentifierTable() {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00004806 assert(PP && "Forgot to set Preprocessor ?");
4807 return PP->getIdentifierTable();
Douglas Gregor668c1a42009-04-21 22:25:48 +00004808}
4809
Douglas Gregor025452f2009-04-17 00:04:06 +00004810/// \brief Record that the given ID maps to the given switch-case
4811/// statement.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004812void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Douglas Gregor025452f2009-04-17 00:04:06 +00004813 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
4814 SwitchCaseStmts[ID] = SC;
4815}
4816
4817/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004818SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Douglas Gregor025452f2009-04-17 00:04:06 +00004819 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
4820 return SwitchCaseStmts[ID];
4821}
Douglas Gregor1de05fe2009-04-17 18:18:49 +00004822
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00004823void ASTReader::ClearSwitchCaseIDs() {
4824 SwitchCaseStmts.clear();
4825}
4826
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004827void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004828 assert(NumCurrentElementsDeserializing &&
4829 "FinishedDeserializing not paired with StartedDeserializing");
4830 if (NumCurrentElementsDeserializing == 1) {
Douglas Gregord89275b2009-07-06 18:54:52 +00004831 // If any identifiers with corresponding top-level declarations have
4832 // been loaded, load those declarations now.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004833 while (!PendingIdentifierInfos.empty()) {
4834 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
4835 PendingIdentifierInfos.front().DeclIDs, true);
4836 PendingIdentifierInfos.pop_front();
Douglas Gregord89275b2009-07-06 18:54:52 +00004837 }
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00004838
Argyrios Kyrtzidis0895d152011-02-12 07:50:47 +00004839 // Ready to load previous declarations of Decls that were delayed.
4840 while (!PendingPreviousDecls.empty()) {
4841 loadAndAttachPreviousDecl(PendingPreviousDecls.front().first,
4842 PendingPreviousDecls.front().second);
4843 PendingPreviousDecls.pop_front();
4844 }
4845
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00004846 // We are not in recursive loading, so it's safe to pass the "interesting"
4847 // decls to the consumer.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004848 if (Consumer)
4849 PassInterestingDeclsToConsumer();
Argyrios Kyrtzidis134db1f2010-10-24 17:26:31 +00004850
4851 assert(PendingForwardRefs.size() == 0 &&
4852 "Some forward refs did not get linked to the definition!");
Douglas Gregord89275b2009-07-06 18:54:52 +00004853 }
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004854 --NumCurrentElementsDeserializing;
Douglas Gregord89275b2009-07-06 18:54:52 +00004855}
Douglas Gregor501c1032010-08-19 00:28:17 +00004856
Sebastian Redle1dde812010-08-24 00:50:04 +00004857ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context,
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00004858 const char *isysroot, bool DisableValidation,
4859 bool DisableStatCache)
Sebastian Redle1dde812010-08-24 00:50:04 +00004860 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
4861 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
4862 Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context),
4863 Consumer(0), isysroot(isysroot), DisableValidation(DisableValidation),
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00004864 DisableStatCache(DisableStatCache), NumStatHits(0), NumStatMisses(0),
4865 NumSLocEntriesRead(0), TotalNumSLocEntries(0), NextSLocOffset(0),
4866 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
4867 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
4868 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
4869 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
4870 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
4871 NumCurrentElementsDeserializing(0)
4872{
Sebastian Redle1dde812010-08-24 00:50:04 +00004873 RelocatablePCH = false;
4874}
4875
4876ASTReader::ASTReader(SourceManager &SourceMgr, FileManager &FileMgr,
4877 Diagnostic &Diags, const char *isysroot,
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00004878 bool DisableValidation, bool DisableStatCache)
Sebastian Redle1dde812010-08-24 00:50:04 +00004879 : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr),
4880 Diags(Diags), SemaObj(0), PP(0), Context(0), Consumer(0),
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00004881 isysroot(isysroot), DisableValidation(DisableValidation),
4882 DisableStatCache(DisableStatCache), NumStatHits(0), NumStatMisses(0),
4883 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Sebastian Redl8db9fae2010-09-22 20:19:08 +00004884 NextSLocOffset(0), NumStatementsRead(0), TotalNumStatements(0),
4885 NumMacrosRead(0), TotalNumMacros(0), NumSelectorsRead(0),
4886 NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
4887 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
4888 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
4889 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
Sebastian Redle1dde812010-08-24 00:50:04 +00004890 RelocatablePCH = false;
4891}
4892
4893ASTReader::~ASTReader() {
4894 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
4895 delete Chain[e - i - 1];
4896 // Delete all visible decl lookup tables
4897 for (DeclContextOffsetsMap::iterator I = DeclContextOffsets.begin(),
4898 E = DeclContextOffsets.end();
4899 I != E; ++I) {
4900 for (DeclContextInfos::iterator J = I->second.begin(), F = I->second.end();
4901 J != F; ++J) {
4902 if (J->NameLookupTableData)
4903 delete static_cast<ASTDeclContextNameLookupTable*>(
4904 J->NameLookupTableData);
4905 }
4906 }
4907 for (DeclContextVisibleUpdatesPending::iterator
4908 I = PendingVisibleUpdates.begin(),
4909 E = PendingVisibleUpdates.end();
4910 I != E; ++I) {
4911 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
4912 F = I->second.end();
4913 J != F; ++J)
4914 delete static_cast<ASTDeclContextNameLookupTable*>(*J);
4915 }
4916}
4917
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00004918ASTReader::PerFileData::PerFileData(ASTFileType Ty)
4919 : Type(Ty), SizeInBits(0), LocalNumSLocEntries(0), SLocOffsets(0), LocalSLocSize(0),
Sebastian Redl301c9b02010-09-22 00:42:27 +00004920 LocalNumIdentifiers(0), IdentifierOffsets(0), IdentifierTableData(0),
4921 IdentifierLookupTable(0), LocalNumMacroDefinitions(0),
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00004922 MacroDefinitionOffsets(0),
4923 LocalNumHeaderFileInfos(0), HeaderFileInfoTableData(0),
4924 HeaderFileInfoTable(0),
4925 LocalNumSelectors(0), SelectorOffsets(0),
Sebastian Redl301c9b02010-09-22 00:42:27 +00004926 SelectorLookupTableData(0), SelectorLookupTable(0), LocalNumDecls(0),
Douglas Gregor7c789c12010-10-29 22:39:52 +00004927 DeclOffsets(0), LocalNumCXXBaseSpecifiers(0), CXXBaseSpecifiersOffsets(0),
4928 LocalNumTypes(0), TypeOffsets(0), StatCache(0),
Sebastian Redla866e652010-10-01 19:59:12 +00004929 NumPreallocatedPreprocessingEntities(0), NextInSource(0)
Douglas Gregor501c1032010-08-19 00:28:17 +00004930{}
4931
4932ASTReader::PerFileData::~PerFileData() {
4933 delete static_cast<ASTIdentifierLookupTable *>(IdentifierLookupTable);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00004934 delete static_cast<HeaderFileInfoLookupTable *>(HeaderFileInfoTable);
Douglas Gregor501c1032010-08-19 00:28:17 +00004935 delete static_cast<ASTSelectorLookupTable *>(SelectorLookupTable);
4936}
4937