blob: 93e36069429531dec132e4294532e097e629a728 [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,
Nick Lewycky277a6e72011-02-23 21:16:44 +0000247 std::string &SuggestedPredefines,
248 FileManager &FileMgr) {
Daniel Dunbarc7162932009-11-11 23:58:53 +0000249 // We are in the context of an implicit include, so the predefines buffer will
250 // have a #include entry for the PCH file itself (as normalized by the
251 // preprocessor initialization). Find it and skip over it in the checking
252 // below.
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000253 llvm::SmallString<256> PCHInclude;
254 PCHInclude += "#include \"";
Nick Lewycky277a6e72011-02-23 21:16:44 +0000255 PCHInclude += NormalizeDashIncludePath(OriginalFileName, FileMgr);
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000256 PCHInclude += "\"\n";
257 std::pair<llvm::StringRef,llvm::StringRef> Split =
258 llvm::StringRef(PP.getPredefines()).split(PCHInclude.str());
259 llvm::StringRef Left = Split.first, Right = Split.second;
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000260 if (Left == PP.getPredefines()) {
261 Error("Missing PCH include entry!");
262 return true;
263 }
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000264
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000265 // If the concatenation of all the PCH buffers is equal to the adjusted
266 // command line, we're done.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000267 llvm::SmallVector<llvm::StringRef, 2> CommandLine;
268 CommandLine.push_back(Left);
269 CommandLine.push_back(Right);
270 if (EqualConcatenations(CommandLine, Buffers))
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000271 return false;
272
273 SourceManager &SourceMgr = PP.getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000274
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000275 // The predefines buffers are different. Determine what the differences are,
276 // and whether they require us to reject the PCH file.
Daniel Dunbare6750492009-11-13 16:46:11 +0000277 llvm::SmallVector<llvm::StringRef, 8> PCHLines;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000278 for (unsigned I = 0, N = Buffers.size(); I != N; ++I)
279 Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Daniel Dunbare6750492009-11-13 16:46:11 +0000280
281 llvm::SmallVector<llvm::StringRef, 8> CmdLineLines;
282 Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000283
284 // Pick out implicit #includes after the PCH and don't consider them for
285 // validation; we will insert them into SuggestedPredefines so that the
286 // preprocessor includes them.
287 std::string IncludesAfterPCH;
288 llvm::SmallVector<llvm::StringRef, 8> AfterPCHLines;
289 Right.split(AfterPCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
290 for (unsigned i = 0, e = AfterPCHLines.size(); i != e; ++i) {
291 if (AfterPCHLines[i].startswith("#include ")) {
292 IncludesAfterPCH += AfterPCHLines[i];
293 IncludesAfterPCH += '\n';
294 } else {
295 CmdLineLines.push_back(AfterPCHLines[i]);
296 }
297 }
298
299 // Make sure we add the includes last into SuggestedPredefines before we
300 // exit this function.
301 struct AddIncludesRAII {
302 std::string &SuggestedPredefines;
303 std::string &IncludesAfterPCH;
304
305 AddIncludesRAII(std::string &SuggestedPredefines,
306 std::string &IncludesAfterPCH)
307 : SuggestedPredefines(SuggestedPredefines),
308 IncludesAfterPCH(IncludesAfterPCH) { }
309 ~AddIncludesRAII() {
310 SuggestedPredefines += IncludesAfterPCH;
311 }
312 } AddIncludes(SuggestedPredefines, IncludesAfterPCH);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000313
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000314 // Sort both sets of predefined buffer lines, since we allow some extra
315 // definitions and they may appear at any point in the output.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000316 std::sort(CmdLineLines.begin(), CmdLineLines.end());
317 std::sort(PCHLines.begin(), PCHLines.end());
318
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000319 // Determine which predefines that were used to build the PCH file are missing
320 // from the command line.
321 std::vector<llvm::StringRef> MissingPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000322 std::set_difference(PCHLines.begin(), PCHLines.end(),
323 CmdLineLines.begin(), CmdLineLines.end(),
324 std::back_inserter(MissingPredefines));
325
326 bool MissingDefines = false;
327 bool ConflictingDefines = false;
328 for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) {
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000329 llvm::StringRef Missing = MissingPredefines[I];
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000330 if (Missing.startswith("#include ")) {
331 // An -include was specified when generating the PCH; it is included in
332 // the PCH, just ignore it.
333 continue;
334 }
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000335 if (!Missing.startswith("#define ")) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000336 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
337 return true;
338 }
Mike Stump1eb44332009-09-09 15:08:12 +0000339
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000340 // This is a macro definition. Determine the name of the macro we're
341 // defining.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000342 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000343 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000344 = Missing.find_first_of("( \n\r", StartOfMacroName);
345 assert(EndOfMacroName != std::string::npos &&
346 "Couldn't find the end of the macro name");
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000347 llvm::StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000348
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000349 // Determine whether this macro was given a different definition on the
350 // command line.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000351 std::string MacroDefStart = "#define " + MacroName.str();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000352 std::string::size_type MacroDefLen = MacroDefStart.size();
Daniel Dunbare6750492009-11-13 16:46:11 +0000353 llvm::SmallVector<llvm::StringRef, 8>::iterator ConflictPos
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000354 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
355 MacroDefStart);
356 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000357 if (!ConflictPos->startswith(MacroDefStart)) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000358 // Different macro; we're done.
359 ConflictPos = CmdLineLines.end();
Mike Stump1eb44332009-09-09 15:08:12 +0000360 break;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000361 }
Mike Stump1eb44332009-09-09 15:08:12 +0000362
363 assert(ConflictPos->size() > MacroDefLen &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000364 "Invalid #define in predefines buffer?");
Mike Stump1eb44332009-09-09 15:08:12 +0000365 if ((*ConflictPos)[MacroDefLen] != ' ' &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000366 (*ConflictPos)[MacroDefLen] != '(')
367 continue; // Longer macro name; keep trying.
Mike Stump1eb44332009-09-09 15:08:12 +0000368
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000369 // We found a conflicting macro definition.
370 break;
371 }
Mike Stump1eb44332009-09-09 15:08:12 +0000372
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000373 if (ConflictPos != CmdLineLines.end()) {
374 Reader.Diag(diag::warn_cmdline_conflicting_macro_def)
375 << MacroName;
376
377 // Show the definition of this macro within the PCH file.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000378 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
379 FindMacro(Buffers, Missing);
380 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
381 SourceLocation PCHMissingLoc =
382 SourceMgr.getLocForStartOfFile(MacroLoc.first)
383 .getFileLocWithOffset(MacroLoc.second);
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000384 Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000385
386 ConflictingDefines = true;
387 continue;
388 }
Mike Stump1eb44332009-09-09 15:08:12 +0000389
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000390 // If the macro doesn't conflict, then we'll just pick up the macro
391 // definition from the PCH file. Warn the user that they made a mistake.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000392 if (ConflictingDefines)
393 continue; // Don't complain if there are already conflicting defs
Mike Stump1eb44332009-09-09 15:08:12 +0000394
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000395 if (!MissingDefines) {
396 Reader.Diag(diag::warn_cmdline_missing_macro_defs);
397 MissingDefines = true;
398 }
399
400 // Show the definition of this macro within the PCH file.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000401 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
402 FindMacro(Buffers, Missing);
403 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
404 SourceLocation PCHMissingLoc =
405 SourceMgr.getLocForStartOfFile(MacroLoc.first)
406 .getFileLocWithOffset(MacroLoc.second);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000407 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
408 }
Mike Stump1eb44332009-09-09 15:08:12 +0000409
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000410 if (ConflictingDefines)
411 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000412
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000413 // Determine what predefines were introduced based on command-line
414 // parameters that were not present when building the PCH
415 // file. Extra #defines are okay, so long as the identifiers being
416 // defined were not used within the precompiled header.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000417 std::vector<llvm::StringRef> ExtraPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000418 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
419 PCHLines.begin(), PCHLines.end(),
Mike Stump1eb44332009-09-09 15:08:12 +0000420 std::back_inserter(ExtraPredefines));
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000421 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000422 llvm::StringRef &Extra = ExtraPredefines[I];
423 if (!Extra.startswith("#define ")) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000424 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
425 return true;
426 }
427
428 // This is an extra macro definition. Determine the name of the
429 // macro we're defining.
430 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000431 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000432 = Extra.find_first_of("( \n\r", StartOfMacroName);
433 assert(EndOfMacroName != std::string::npos &&
434 "Couldn't find the end of the macro name");
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000435 llvm::StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000436
437 // Check whether this name was used somewhere in the PCH file. If
438 // so, defining it as a macro could change behavior, so we reject
439 // the PCH file.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000440 if (IdentifierInfo *II = Reader.get(MacroName)) {
Daniel Dunbar4fda42e2009-11-11 00:52:00 +0000441 Reader.Diag(diag::warn_macro_name_used_in_pch) << II;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000442 return true;
443 }
444
445 // Add this definition to the suggested predefines buffer.
446 SuggestedPredefines += Extra;
447 SuggestedPredefines += '\n';
448 }
449
450 // If we get here, it's because the predefines buffer had compatible
451 // contents. Accept the PCH file.
452 return false;
453}
454
Douglas Gregor12fab312010-03-16 16:35:32 +0000455void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
456 unsigned ID) {
457 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
458 ++NumHeaderInfos;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000459}
460
461void PCHValidator::ReadCounter(unsigned Value) {
462 PP.setCounterValue(Value);
463}
464
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000465//===----------------------------------------------------------------------===//
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000466// AST reader implementation
Douglas Gregor668c1a42009-04-21 22:25:48 +0000467//===----------------------------------------------------------------------===//
468
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000469void
Sebastian Redl571db7f2010-08-18 23:56:56 +0000470ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000471 DeserializationListener = Listener;
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000472}
473
Chris Lattner4c6f9522009-04-27 05:14:47 +0000474
Douglas Gregor668c1a42009-04-21 22:25:48 +0000475namespace {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000476class ASTSelectorLookupTrait {
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000477 ASTReader &Reader;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000478
479public:
Sebastian Redl5d050072010-08-04 17:20:04 +0000480 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000481 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +0000482 ObjCMethodList Instance, Factory;
483 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000484
485 typedef Selector external_key_type;
486 typedef external_key_type internal_key_type;
487
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000488 explicit ASTSelectorLookupTrait(ASTReader &Reader) : Reader(Reader) { }
Mike Stump1eb44332009-09-09 15:08:12 +0000489
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000490 static bool EqualKey(const internal_key_type& a,
491 const internal_key_type& b) {
492 return a == b;
493 }
Mike Stump1eb44332009-09-09 15:08:12 +0000494
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000495 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +0000496 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000497 }
Mike Stump1eb44332009-09-09 15:08:12 +0000498
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000499 // This hopefully will just get inlined and removed by the optimizer.
500 static const internal_key_type&
501 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump1eb44332009-09-09 15:08:12 +0000502
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000503 static std::pair<unsigned, unsigned>
504 ReadKeyDataLength(const unsigned char*& d) {
505 using namespace clang::io;
506 unsigned KeyLen = ReadUnalignedLE16(d);
507 unsigned DataLen = ReadUnalignedLE16(d);
508 return std::make_pair(KeyLen, DataLen);
509 }
Mike Stump1eb44332009-09-09 15:08:12 +0000510
Douglas Gregor83941df2009-04-25 17:48:32 +0000511 internal_key_type ReadKey(const unsigned char* d, unsigned) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000512 using namespace clang::io;
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000513 SelectorTable &SelTable = Reader.getContext()->Selectors;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000514 unsigned N = ReadUnalignedLE16(d);
Mike Stump1eb44332009-09-09 15:08:12 +0000515 IdentifierInfo *FirstII
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000516 = Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
517 if (N == 0)
518 return SelTable.getNullarySelector(FirstII);
519 else if (N == 1)
520 return SelTable.getUnarySelector(FirstII);
521
522 llvm::SmallVector<IdentifierInfo *, 16> Args;
523 Args.push_back(FirstII);
524 for (unsigned I = 1; I != N; ++I)
525 Args.push_back(Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)));
526
Douglas Gregor75fdb232009-05-22 22:45:36 +0000527 return SelTable.getSelector(N, Args.data());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000528 }
Mike Stump1eb44332009-09-09 15:08:12 +0000529
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000530 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen) {
531 using namespace clang::io;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000532
533 data_type Result;
534
Sebastian Redl5d050072010-08-04 17:20:04 +0000535 Result.ID = ReadUnalignedLE32(d);
536 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
537 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
538
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000539 // Load instance methods
540 ObjCMethodList *Prev = 0;
541 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000542 ObjCMethodDecl *Method
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000543 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl5d050072010-08-04 17:20:04 +0000544 if (!Result.Instance.Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000545 // This is the first method, which is the easy case.
Sebastian Redl5d050072010-08-04 17:20:04 +0000546 Result.Instance.Method = Method;
547 Prev = &Result.Instance;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000548 continue;
549 }
550
Ted Kremenek298ed872010-02-11 00:53:01 +0000551 ObjCMethodList *Mem =
552 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
553 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000554 Prev = Prev->Next;
555 }
556
557 // Load factory methods
558 Prev = 0;
559 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000560 ObjCMethodDecl *Method
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000561 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl5d050072010-08-04 17:20:04 +0000562 if (!Result.Factory.Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000563 // This is the first method, which is the easy case.
Sebastian Redl5d050072010-08-04 17:20:04 +0000564 Result.Factory.Method = Method;
565 Prev = &Result.Factory;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000566 continue;
567 }
568
Ted Kremenek298ed872010-02-11 00:53:01 +0000569 ObjCMethodList *Mem =
570 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
571 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000572 Prev = Prev->Next;
573 }
574
575 return Result;
576 }
577};
Mike Stump1eb44332009-09-09 15:08:12 +0000578
579} // end anonymous namespace
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000580
581/// \brief The on-disk hash table used for the global method pool.
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000582typedef OnDiskChainedHashTable<ASTSelectorLookupTrait>
583 ASTSelectorLookupTable;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000584
Sebastian Redlc3632732010-10-05 15:59:54 +0000585namespace clang {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000586class ASTIdentifierLookupTrait {
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000587 ASTReader &Reader;
Sebastian Redlc3632732010-10-05 15:59:54 +0000588 ASTReader::PerFileData &F;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000589
590 // If we know the IdentifierInfo in advance, it is here and we will
591 // not build a new one. Used when deserializing information about an
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000592 // identifier that was constructed before the AST file was read.
Douglas Gregor668c1a42009-04-21 22:25:48 +0000593 IdentifierInfo *KnownII;
594
595public:
596 typedef IdentifierInfo * data_type;
597
598 typedef const std::pair<const char*, unsigned> external_key_type;
599
600 typedef external_key_type internal_key_type;
601
Sebastian Redlc3632732010-10-05 15:59:54 +0000602 ASTIdentifierLookupTrait(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redld27d3fc2010-07-21 22:31:37 +0000603 IdentifierInfo *II = 0)
Sebastian Redlc3632732010-10-05 15:59:54 +0000604 : Reader(Reader), F(F), KnownII(II) { }
Mike Stump1eb44332009-09-09 15:08:12 +0000605
Douglas Gregor668c1a42009-04-21 22:25:48 +0000606 static bool EqualKey(const internal_key_type& a,
607 const internal_key_type& b) {
608 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
609 : false;
610 }
Mike Stump1eb44332009-09-09 15:08:12 +0000611
Douglas Gregor668c1a42009-04-21 22:25:48 +0000612 static unsigned ComputeHash(const internal_key_type& a) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000613 return llvm::HashString(llvm::StringRef(a.first, a.second));
Douglas Gregor668c1a42009-04-21 22:25:48 +0000614 }
Mike Stump1eb44332009-09-09 15:08:12 +0000615
Douglas Gregor668c1a42009-04-21 22:25:48 +0000616 // This hopefully will just get inlined and removed by the optimizer.
617 static const internal_key_type&
618 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump1eb44332009-09-09 15:08:12 +0000619
Douglas Gregor95f42922010-10-14 22:11:03 +0000620 // This hopefully will just get inlined and removed by the optimizer.
621 static const external_key_type&
622 GetExternalKey(const internal_key_type& x) { return x; }
623
Douglas Gregor668c1a42009-04-21 22:25:48 +0000624 static std::pair<unsigned, unsigned>
625 ReadKeyDataLength(const unsigned char*& d) {
626 using namespace clang::io;
Douglas Gregor5f8e3302009-04-25 20:26:24 +0000627 unsigned DataLen = ReadUnalignedLE16(d);
Douglas Gregord6595a42009-04-25 21:04:17 +0000628 unsigned KeyLen = ReadUnalignedLE16(d);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000629 return std::make_pair(KeyLen, DataLen);
630 }
Mike Stump1eb44332009-09-09 15:08:12 +0000631
Douglas Gregor668c1a42009-04-21 22:25:48 +0000632 static std::pair<const char*, unsigned>
633 ReadKey(const unsigned char* d, unsigned n) {
634 assert(n >= 2 && d[n-1] == '\0');
635 return std::make_pair((const char*) d, n-1);
636 }
Mike Stump1eb44332009-09-09 15:08:12 +0000637
638 IdentifierInfo *ReadData(const internal_key_type& k,
Douglas Gregor668c1a42009-04-21 22:25:48 +0000639 const unsigned char* d,
640 unsigned DataLen) {
641 using namespace clang::io;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000642 IdentID ID = ReadUnalignedLE32(d);
Douglas Gregora92193e2009-04-28 21:18:29 +0000643 bool IsInteresting = ID & 0x01;
644
645 // Wipe out the "is interesting" bit.
646 ID = ID >> 1;
647
648 if (!IsInteresting) {
Sebastian Redl083abdf2010-07-27 23:01:28 +0000649 // For uninteresting identifiers, just build the IdentifierInfo
Douglas Gregora92193e2009-04-28 21:18:29 +0000650 // and associate it with the persistent ID.
651 IdentifierInfo *II = KnownII;
652 if (!II)
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000653 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregora92193e2009-04-28 21:18:29 +0000654 Reader.SetIdentifierInfo(ID, II);
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000655 II->setIsFromAST();
Douglas Gregora92193e2009-04-28 21:18:29 +0000656 return II;
657 }
658
Douglas Gregor5998da52009-04-28 21:32:13 +0000659 unsigned Bits = ReadUnalignedLE16(d);
Douglas Gregor2deaea32009-04-22 18:49:13 +0000660 bool CPlusPlusOperatorKeyword = Bits & 0x01;
661 Bits >>= 1;
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000662 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
663 Bits >>= 1;
Douglas Gregor2deaea32009-04-22 18:49:13 +0000664 bool Poisoned = Bits & 0x01;
665 Bits >>= 1;
666 bool ExtensionToken = Bits & 0x01;
667 Bits >>= 1;
668 bool hasMacroDefinition = Bits & 0x01;
669 Bits >>= 1;
670 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
671 Bits >>= 10;
Mike Stump1eb44332009-09-09 15:08:12 +0000672
Douglas Gregor2deaea32009-04-22 18:49:13 +0000673 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregor5998da52009-04-28 21:32:13 +0000674 DataLen -= 6;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000675
676 // Build the IdentifierInfo itself and link the identifier ID with
677 // the new IdentifierInfo.
678 IdentifierInfo *II = KnownII;
679 if (!II)
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000680 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000681 Reader.SetIdentifierInfo(ID, II);
682
Douglas Gregor2deaea32009-04-22 18:49:13 +0000683 // Set or check the various bits in the IdentifierInfo structure.
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000684 // Token IDs are read-only.
685 if (HasRevertedTokenIDToIdentifier)
686 II->RevertTokenIDToIdentifier();
Douglas Gregor2deaea32009-04-22 18:49:13 +0000687 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
Mike Stump1eb44332009-09-09 15:08:12 +0000688 assert(II->isExtensionToken() == ExtensionToken &&
Douglas Gregor2deaea32009-04-22 18:49:13 +0000689 "Incorrect extension token flag");
690 (void)ExtensionToken;
691 II->setIsPoisoned(Poisoned);
692 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
693 "Incorrect C++ operator keyword flag");
694 (void)CPlusPlusOperatorKeyword;
695
Douglas Gregor37e26842009-04-21 23:56:24 +0000696 // If this identifier is a macro, deserialize the macro
697 // definition.
698 if (hasMacroDefinition) {
Douglas Gregor5998da52009-04-28 21:32:13 +0000699 uint32_t Offset = ReadUnalignedLE32(d);
Douglas Gregor295a2a62010-10-30 00:23:06 +0000700 Reader.SetIdentifierIsMacro(II, F, Offset);
Douglas Gregor5998da52009-04-28 21:32:13 +0000701 DataLen -= 4;
Douglas Gregor37e26842009-04-21 23:56:24 +0000702 }
Douglas Gregor668c1a42009-04-21 22:25:48 +0000703
704 // Read all of the declarations visible at global scope with this
705 // name.
Chris Lattner6bf690f2009-04-27 22:17:41 +0000706 if (Reader.getContext() == 0) return II;
Douglas Gregord89275b2009-07-06 18:54:52 +0000707 if (DataLen > 0) {
708 llvm::SmallVector<uint32_t, 4> DeclIDs;
709 for (; DataLen > 0; DataLen -= 4)
710 DeclIDs.push_back(ReadUnalignedLE32(d));
711 Reader.SetGloballyVisibleDecls(II, DeclIDs);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000712 }
Mike Stump1eb44332009-09-09 15:08:12 +0000713
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000714 II->setIsFromAST();
Douglas Gregor668c1a42009-04-21 22:25:48 +0000715 return II;
716 }
717};
Mike Stump1eb44332009-09-09 15:08:12 +0000718
719} // end anonymous namespace
Douglas Gregor668c1a42009-04-21 22:25:48 +0000720
721/// \brief The on-disk hash table used to contain information about
722/// all of the identifiers in the program.
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000723typedef OnDiskChainedHashTable<ASTIdentifierLookupTrait>
724 ASTIdentifierLookupTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000725
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000726namespace {
727class ASTDeclContextNameLookupTrait {
728 ASTReader &Reader;
729
730public:
731 /// \brief Pair of begin/end iterators for DeclIDs.
732 typedef std::pair<DeclID *, DeclID *> data_type;
733
734 /// \brief Special internal key for declaration names.
735 /// The hash table creates keys for comparison; we do not create
736 /// a DeclarationName for the internal key to avoid deserializing types.
737 struct DeclNameKey {
738 DeclarationName::NameKind Kind;
739 uint64_t Data;
740 DeclNameKey() : Kind((DeclarationName::NameKind)0), Data(0) { }
741 };
742
743 typedef DeclarationName external_key_type;
744 typedef DeclNameKey internal_key_type;
745
746 explicit ASTDeclContextNameLookupTrait(ASTReader &Reader) : Reader(Reader) { }
747
748 static bool EqualKey(const internal_key_type& a,
749 const internal_key_type& b) {
750 return a.Kind == b.Kind && a.Data == b.Data;
751 }
752
753 unsigned ComputeHash(const DeclNameKey &Key) const {
754 llvm::FoldingSetNodeID ID;
755 ID.AddInteger(Key.Kind);
756
757 switch (Key.Kind) {
758 case DeclarationName::Identifier:
759 case DeclarationName::CXXLiteralOperatorName:
760 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
761 break;
762 case DeclarationName::ObjCZeroArgSelector:
763 case DeclarationName::ObjCOneArgSelector:
764 case DeclarationName::ObjCMultiArgSelector:
765 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
766 break;
767 case DeclarationName::CXXConstructorName:
768 case DeclarationName::CXXDestructorName:
769 case DeclarationName::CXXConversionFunctionName:
770 ID.AddInteger((TypeID)Key.Data);
771 break;
772 case DeclarationName::CXXOperatorName:
773 ID.AddInteger((OverloadedOperatorKind)Key.Data);
774 break;
775 case DeclarationName::CXXUsingDirective:
776 break;
777 }
778
779 return ID.ComputeHash();
780 }
781
782 internal_key_type GetInternalKey(const external_key_type& Name) const {
783 DeclNameKey Key;
784 Key.Kind = Name.getNameKind();
785 switch (Name.getNameKind()) {
786 case DeclarationName::Identifier:
787 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
788 break;
789 case DeclarationName::ObjCZeroArgSelector:
790 case DeclarationName::ObjCOneArgSelector:
791 case DeclarationName::ObjCMultiArgSelector:
792 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
793 break;
794 case DeclarationName::CXXConstructorName:
795 case DeclarationName::CXXDestructorName:
796 case DeclarationName::CXXConversionFunctionName:
797 Key.Data = Reader.GetTypeID(Name.getCXXNameType());
798 break;
799 case DeclarationName::CXXOperatorName:
800 Key.Data = Name.getCXXOverloadedOperator();
801 break;
802 case DeclarationName::CXXLiteralOperatorName:
803 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
804 break;
805 case DeclarationName::CXXUsingDirective:
806 break;
807 }
Michael J. Spencer20249a12010-10-21 03:16:25 +0000808
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000809 return Key;
810 }
811
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +0000812 external_key_type GetExternalKey(const internal_key_type& Key) const {
813 ASTContext *Context = Reader.getContext();
814 switch (Key.Kind) {
815 case DeclarationName::Identifier:
816 return DeclarationName((IdentifierInfo*)Key.Data);
817
818 case DeclarationName::ObjCZeroArgSelector:
819 case DeclarationName::ObjCOneArgSelector:
820 case DeclarationName::ObjCMultiArgSelector:
821 return DeclarationName(Selector(Key.Data));
822
823 case DeclarationName::CXXConstructorName:
824 return Context->DeclarationNames.getCXXConstructorName(
825 Context->getCanonicalType(Reader.GetType(Key.Data)));
826
827 case DeclarationName::CXXDestructorName:
828 return Context->DeclarationNames.getCXXDestructorName(
829 Context->getCanonicalType(Reader.GetType(Key.Data)));
830
831 case DeclarationName::CXXConversionFunctionName:
832 return Context->DeclarationNames.getCXXConversionFunctionName(
833 Context->getCanonicalType(Reader.GetType(Key.Data)));
834
835 case DeclarationName::CXXOperatorName:
836 return Context->DeclarationNames.getCXXOperatorName(
837 (OverloadedOperatorKind)Key.Data);
838
839 case DeclarationName::CXXLiteralOperatorName:
840 return Context->DeclarationNames.getCXXLiteralOperatorName(
841 (IdentifierInfo*)Key.Data);
842
843 case DeclarationName::CXXUsingDirective:
844 return DeclarationName::getUsingDirectiveName();
845 }
846
847 llvm_unreachable("Invalid Name Kind ?");
848 }
849
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000850 static std::pair<unsigned, unsigned>
851 ReadKeyDataLength(const unsigned char*& d) {
852 using namespace clang::io;
853 unsigned KeyLen = ReadUnalignedLE16(d);
854 unsigned DataLen = ReadUnalignedLE16(d);
855 return std::make_pair(KeyLen, DataLen);
856 }
857
858 internal_key_type ReadKey(const unsigned char* d, unsigned) {
859 using namespace clang::io;
860
861 DeclNameKey Key;
862 Key.Kind = (DeclarationName::NameKind)*d++;
863 switch (Key.Kind) {
864 case DeclarationName::Identifier:
865 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
866 break;
867 case DeclarationName::ObjCZeroArgSelector:
868 case DeclarationName::ObjCOneArgSelector:
869 case DeclarationName::ObjCMultiArgSelector:
Michael J. Spencer20249a12010-10-21 03:16:25 +0000870 Key.Data =
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000871 (uint64_t)Reader.DecodeSelector(ReadUnalignedLE32(d)).getAsOpaquePtr();
872 break;
873 case DeclarationName::CXXConstructorName:
874 case DeclarationName::CXXDestructorName:
875 case DeclarationName::CXXConversionFunctionName:
876 Key.Data = ReadUnalignedLE32(d); // TypeID
877 break;
878 case DeclarationName::CXXOperatorName:
879 Key.Data = *d++; // OverloadedOperatorKind
880 break;
881 case DeclarationName::CXXLiteralOperatorName:
882 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
883 break;
884 case DeclarationName::CXXUsingDirective:
885 break;
886 }
Michael J. Spencer20249a12010-10-21 03:16:25 +0000887
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000888 return Key;
889 }
890
891 data_type ReadData(internal_key_type, const unsigned char* d,
892 unsigned DataLen) {
893 using namespace clang::io;
894 unsigned NumDecls = ReadUnalignedLE16(d);
895 DeclID *Start = (DeclID *)d;
896 return std::make_pair(Start, Start + NumDecls);
897 }
898};
899
900} // end anonymous namespace
901
902/// \brief The on-disk hash table used for the DeclContext's Name lookup table.
903typedef OnDiskChainedHashTable<ASTDeclContextNameLookupTrait>
904 ASTDeclContextNameLookupTable;
905
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000906bool ASTReader::ReadDeclContextStorage(llvm::BitstreamCursor &Cursor,
907 const std::pair<uint64_t, uint64_t> &Offsets,
908 DeclContextInfo &Info) {
909 SavedStreamPosition SavedPosition(Cursor);
910 // First the lexical decls.
911 if (Offsets.first != 0) {
912 Cursor.JumpToBit(Offsets.first);
913
914 RecordData Record;
915 const char *Blob;
916 unsigned BlobLen;
917 unsigned Code = Cursor.ReadCode();
918 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
919 if (RecCode != DECL_CONTEXT_LEXICAL) {
920 Error("Expected lexical block");
921 return true;
922 }
923
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000924 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob);
925 Info.NumLexicalDecls = BlobLen / sizeof(KindDeclIDPair);
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000926 } else {
927 Info.LexicalDecls = 0;
928 Info.NumLexicalDecls = 0;
929 }
930
931 // Now the lookup table.
932 if (Offsets.second != 0) {
933 Cursor.JumpToBit(Offsets.second);
934
935 RecordData Record;
936 const char *Blob;
937 unsigned BlobLen;
938 unsigned Code = Cursor.ReadCode();
939 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
940 if (RecCode != DECL_CONTEXT_VISIBLE) {
941 Error("Expected visible lookup table block");
942 return true;
943 }
944 Info.NameLookupTableData
945 = ASTDeclContextNameLookupTable::Create(
946 (const unsigned char *)Blob + Record[0],
947 (const unsigned char *)Blob,
948 ASTDeclContextNameLookupTrait(*this));
Sebastian Redl0ea8f7f2010-08-24 00:50:00 +0000949 } else {
950 Info.NameLookupTableData = 0;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000951 }
952
953 return false;
954}
955
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000956void ASTReader::Error(const char *Msg) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000957 Diag(diag::err_fe_pch_malformed) << Msg;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000958}
959
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000960/// \brief Tell the AST listener about the predefines buffers in the chain.
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000961bool ASTReader::CheckPredefinesBuffers() {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000962 if (Listener)
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000963 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000964 ActualOriginalFileName,
Nick Lewycky277a6e72011-02-23 21:16:44 +0000965 SuggestedPredefines,
966 FileMgr);
Douglas Gregore721f952009-04-28 18:58:38 +0000967 return false;
Douglas Gregore1d918e2009-04-10 23:10:45 +0000968}
969
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000970//===----------------------------------------------------------------------===//
971// Source Manager Deserialization
972//===----------------------------------------------------------------------===//
973
Douglas Gregorbd945002009-04-13 16:31:14 +0000974/// \brief Read the line table in the source manager block.
Sebastian Redlc3632732010-10-05 15:59:54 +0000975/// \returns true if there was an error.
976bool ASTReader::ParseLineTable(PerFileData &F,
977 llvm::SmallVectorImpl<uint64_t> &Record) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000978 unsigned Idx = 0;
979 LineTableInfo &LineTable = SourceMgr.getLineTable();
980
981 // Parse the file names
Douglas Gregorff0a9872009-04-13 17:12:42 +0000982 std::map<int, int> FileIDs;
983 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000984 // Extract the file name
985 unsigned FilenameLen = Record[Idx++];
986 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
987 Idx += FilenameLen;
Douglas Gregore650c8c2009-07-07 00:12:59 +0000988 MaybeAddSystemRootToFilename(Filename);
Mike Stump1eb44332009-09-09 15:08:12 +0000989 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
Douglas Gregorff0a9872009-04-13 17:12:42 +0000990 Filename.size());
Douglas Gregorbd945002009-04-13 16:31:14 +0000991 }
992
993 // Parse the line entries
994 std::vector<LineEntry> Entries;
995 while (Idx < Record.size()) {
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000996 int FID = Record[Idx++];
Douglas Gregorbd945002009-04-13 16:31:14 +0000997
998 // Extract the line entries
999 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +00001000 assert(NumEntries && "Numentries is 00000");
Douglas Gregorbd945002009-04-13 16:31:14 +00001001 Entries.clear();
1002 Entries.reserve(NumEntries);
1003 for (unsigned I = 0; I != NumEntries; ++I) {
1004 unsigned FileOffset = Record[Idx++];
1005 unsigned LineNo = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +00001006 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump1eb44332009-09-09 15:08:12 +00001007 SrcMgr::CharacteristicKind FileKind
Douglas Gregorbd945002009-04-13 16:31:14 +00001008 = (SrcMgr::CharacteristicKind)Record[Idx++];
1009 unsigned IncludeOffset = Record[Idx++];
1010 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1011 FileKind, IncludeOffset));
1012 }
1013 LineTable.AddEntry(FID, Entries);
1014 }
1015
1016 return false;
1017}
1018
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001019namespace {
1020
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001021class ASTStatData {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001022public:
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001023 const ino_t ino;
1024 const dev_t dev;
1025 const mode_t mode;
1026 const time_t mtime;
1027 const off_t size;
Mike Stump1eb44332009-09-09 15:08:12 +00001028
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001029 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Chris Lattner74e976b2010-11-23 19:28:12 +00001030 : ino(i), dev(d), mode(mo), mtime(m), size(s) {}
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001031};
1032
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001033class ASTStatLookupTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001034 public:
1035 typedef const char *external_key_type;
1036 typedef const char *internal_key_type;
1037
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001038 typedef ASTStatData data_type;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001039
1040 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001041 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001042 }
1043
1044 static internal_key_type GetInternalKey(const char *path) { return path; }
1045
1046 static bool EqualKey(internal_key_type a, internal_key_type b) {
1047 return strcmp(a, b) == 0;
1048 }
1049
1050 static std::pair<unsigned, unsigned>
1051 ReadKeyDataLength(const unsigned char*& d) {
1052 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1053 unsigned DataLen = (unsigned) *d++;
1054 return std::make_pair(KeyLen + 1, DataLen);
1055 }
1056
1057 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
1058 return (const char *)d;
1059 }
1060
1061 static data_type ReadData(const internal_key_type, const unsigned char *d,
1062 unsigned /*DataLen*/) {
1063 using namespace clang::io;
1064
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001065 ino_t ino = (ino_t) ReadUnalignedLE32(d);
1066 dev_t dev = (dev_t) ReadUnalignedLE32(d);
1067 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump1eb44332009-09-09 15:08:12 +00001068 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001069 off_t size = (off_t) ReadUnalignedLE64(d);
1070 return data_type(ino, dev, mode, mtime, size);
1071 }
1072};
1073
1074/// \brief stat() cache for precompiled headers.
1075///
1076/// This cache is very similar to the stat cache used by pretokenized
1077/// headers.
Chris Lattner10e286a2010-11-23 19:19:34 +00001078class ASTStatCache : public FileSystemStatCache {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001079 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001080 CacheTy *Cache;
1081
1082 unsigned &NumStatHits, &NumStatMisses;
Mike Stump1eb44332009-09-09 15:08:12 +00001083public:
Chris Lattner74e976b2010-11-23 19:28:12 +00001084 ASTStatCache(const unsigned char *Buckets, const unsigned char *Base,
1085 unsigned &NumStatHits, unsigned &NumStatMisses)
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001086 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
1087 Cache = CacheTy::Create(Buckets, Base);
1088 }
1089
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001090 ~ASTStatCache() { delete Cache; }
Mike Stump1eb44332009-09-09 15:08:12 +00001091
Chris Lattner898a0612010-11-23 21:17:56 +00001092 LookupResult getStat(const char *Path, struct stat &StatBuf,
1093 int *FileDescriptor) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001094 // Do the lookup for the file's data in the AST file.
Chris Lattner10e286a2010-11-23 19:19:34 +00001095 CacheTy::iterator I = Cache->find(Path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001096
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001097 // If we don't get a hit in the AST file just forward to 'stat'.
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001098 if (I == Cache->end()) {
1099 ++NumStatMisses;
Chris Lattner898a0612010-11-23 21:17:56 +00001100 return statChained(Path, StatBuf, FileDescriptor);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001101 }
Mike Stump1eb44332009-09-09 15:08:12 +00001102
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001103 ++NumStatHits;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001104 ASTStatData Data = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001105
Chris Lattner10e286a2010-11-23 19:19:34 +00001106 StatBuf.st_ino = Data.ino;
1107 StatBuf.st_dev = Data.dev;
1108 StatBuf.st_mtime = Data.mtime;
1109 StatBuf.st_mode = Data.mode;
1110 StatBuf.st_size = Data.size;
Chris Lattnerd6f61112010-11-23 20:05:15 +00001111 return CacheExists;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001112 }
1113};
1114} // end anonymous namespace
1115
1116
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001117/// \brief Read a source manager block
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001118ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(PerFileData &F) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001119 using namespace SrcMgr;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001120
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001121 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001122
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001123 // Set the source-location entry cursor to the current position in
1124 // the stream. This cursor will be used to read the contents of the
1125 // source manager block initially, and then lazily read
1126 // source-location entries as needed.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001127 SLocEntryCursor = F.Stream;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001128
1129 // The stream itself is going to skip over the source manager block.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001130 if (F.Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001131 Error("malformed block record in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001132 return Failure;
1133 }
1134
1135 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001136 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001137 Error("malformed source manager block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001138 return Failure;
1139 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001140
Douglas Gregor14f79002009-04-10 03:52:48 +00001141 RecordData Record;
1142 while (true) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001143 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregor14f79002009-04-10 03:52:48 +00001144 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001145 if (SLocEntryCursor.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001146 Error("error at end of Source Manager block in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001147 return Failure;
1148 }
Douglas Gregore1d918e2009-04-10 23:10:45 +00001149 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +00001150 }
Mike Stump1eb44332009-09-09 15:08:12 +00001151
Douglas Gregor14f79002009-04-10 03:52:48 +00001152 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1153 // No known subblocks, always skip them.
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001154 SLocEntryCursor.ReadSubBlockID();
1155 if (SLocEntryCursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001156 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001157 return Failure;
1158 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001159 continue;
1160 }
Mike Stump1eb44332009-09-09 15:08:12 +00001161
Douglas Gregor14f79002009-04-10 03:52:48 +00001162 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001163 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregor14f79002009-04-10 03:52:48 +00001164 continue;
1165 }
Mike Stump1eb44332009-09-09 15:08:12 +00001166
Douglas Gregor14f79002009-04-10 03:52:48 +00001167 // Read a record.
1168 const char *BlobStart;
1169 unsigned BlobLen;
1170 Record.clear();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001171 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001172 default: // Default behavior: ignore.
1173 break;
1174
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001175 case SM_LINE_TABLE:
Sebastian Redlc3632732010-10-05 15:59:54 +00001176 if (ParseLineTable(F, Record))
Douglas Gregorbd945002009-04-13 16:31:14 +00001177 return Failure;
Chris Lattner2c78b872009-04-14 23:22:57 +00001178 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001179
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001180 case SM_SLOC_FILE_ENTRY:
1181 case SM_SLOC_BUFFER_ENTRY:
1182 case SM_SLOC_INSTANTIATION_ENTRY:
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001183 // Once we hit one of the source location entries, we're done.
1184 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +00001185 }
1186 }
1187}
1188
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001189/// \brief If a header file is not found at the path that we expect it to be
1190/// and the PCH file was moved from its original location, try to resolve the
1191/// file by assuming that header+PCH were moved together and the header is in
1192/// the same place relative to the PCH.
1193static std::string
1194resolveFileRelativeToOriginalDir(const std::string &Filename,
1195 const std::string &OriginalDir,
1196 const std::string &CurrDir) {
1197 assert(OriginalDir != CurrDir &&
1198 "No point trying to resolve the file if the PCH dir didn't change");
1199 using namespace llvm::sys;
1200 llvm::SmallString<128> filePath(Filename);
1201 fs::make_absolute(filePath);
1202 assert(path::is_absolute(OriginalDir));
1203 llvm::SmallString<128> currPCHPath(CurrDir);
1204
1205 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1206 fileDirE = path::end(path::parent_path(filePath));
1207 path::const_iterator origDirI = path::begin(OriginalDir),
1208 origDirE = path::end(OriginalDir);
1209 // Skip the common path components from filePath and OriginalDir.
1210 while (fileDirI != fileDirE && origDirI != origDirE &&
1211 *fileDirI == *origDirI) {
1212 ++fileDirI;
1213 ++origDirI;
1214 }
1215 for (; origDirI != origDirE; ++origDirI)
1216 path::append(currPCHPath, "..");
1217 path::append(currPCHPath, fileDirI, fileDirE);
1218 path::append(currPCHPath, path::filename(Filename));
1219 return currPCHPath.str();
1220}
1221
Sebastian Redl190faf72010-07-20 21:50:20 +00001222/// \brief Get a cursor that's correctly positioned for reading the source
1223/// location entry with the given ID.
Sebastian Redlc3632732010-10-05 15:59:54 +00001224ASTReader::PerFileData *ASTReader::SLocCursorForID(unsigned ID) {
Sebastian Redl190faf72010-07-20 21:50:20 +00001225 assert(ID != 0 && ID <= TotalNumSLocEntries &&
1226 "SLocCursorForID should only be called for real IDs.");
1227
1228 ID -= 1;
1229 PerFileData *F = 0;
1230 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1231 F = Chain[N - I - 1];
1232 if (ID < F->LocalNumSLocEntries)
1233 break;
1234 ID -= F->LocalNumSLocEntries;
1235 }
1236 assert(F && F->LocalNumSLocEntries > ID && "Chain corrupted");
1237
1238 F->SLocEntryCursor.JumpToBit(F->SLocOffsets[ID]);
Sebastian Redlc3632732010-10-05 15:59:54 +00001239 return F;
Sebastian Redl190faf72010-07-20 21:50:20 +00001240}
1241
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001242/// \brief Read in the source location entry with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001243ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(unsigned ID) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001244 if (ID == 0)
1245 return Success;
1246
1247 if (ID > TotalNumSLocEntries) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001248 Error("source location entry ID out-of-range for AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001249 return Failure;
1250 }
1251
Sebastian Redlc3632732010-10-05 15:59:54 +00001252 PerFileData *F = SLocCursorForID(ID);
1253 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001254
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001255 ++NumSLocEntriesRead;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001256 unsigned Code = SLocEntryCursor.ReadCode();
1257 if (Code == llvm::bitc::END_BLOCK ||
1258 Code == llvm::bitc::ENTER_SUBBLOCK ||
1259 Code == llvm::bitc::DEFINE_ABBREV) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001260 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001261 return Failure;
1262 }
1263
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001264 RecordData Record;
1265 const char *BlobStart;
1266 unsigned BlobLen;
1267 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1268 default:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001269 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001270 return Failure;
1271
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001272 case SM_SLOC_FILE_ENTRY: {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001273 std::string Filename(BlobStart, BlobStart + BlobLen);
1274 MaybeAddSystemRootToFilename(Filename);
Chris Lattner39b49bc2010-11-23 08:35:12 +00001275 const FileEntry *File = FileMgr.getFile(Filename);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001276 if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() &&
1277 OriginalDir != CurrentDir) {
1278 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1279 OriginalDir,
1280 CurrentDir);
1281 if (!resolved.empty())
1282 File = FileMgr.getFile(resolved);
1283 }
Axel Naumann04331162011-01-27 10:55:51 +00001284 if (File == 0)
1285 File = FileMgr.getVirtualFile(Filename, (off_t)Record[4],
1286 (time_t)Record[5]);
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001287 if (File == 0) {
1288 std::string ErrorStr = "could not find file '";
Douglas Gregore650c8c2009-07-07 00:12:59 +00001289 ErrorStr += Filename;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001290 ErrorStr += "' referenced by AST file";
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001291 Error(ErrorStr.c_str());
1292 return Failure;
1293 }
Mike Stump1eb44332009-09-09 15:08:12 +00001294
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001295 if (Record.size() < 6) {
Ted Kremenek1857f622010-03-18 21:23:05 +00001296 Error("source location entry is incorrect");
1297 return Failure;
1298 }
1299
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001300 if (!DisableValidation &&
1301 ((off_t)Record[4] != File->getSize()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001302#if !defined(LLVM_ON_WIN32)
1303 // In our regression testing, the Windows file system seems to
1304 // have inconsistent modification times that sometimes
1305 // erroneously trigger this error-handling path.
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001306 || (time_t)Record[5] != File->getModificationTime()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001307#endif
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001308 )) {
Douglas Gregor2d52be52010-03-21 22:49:54 +00001309 Diag(diag::err_fe_pch_file_modified)
1310 << Filename;
1311 return Failure;
1312 }
1313
Chris Lattner75dfb652010-11-23 09:19:42 +00001314 FileID FID = SourceMgr.createFileID(File, ReadSourceLocation(*F, Record[1]),
1315 (SrcMgr::CharacteristicKind)Record[2],
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001316 ID, Record[0]);
1317 if (Record[3])
1318 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile())
1319 .setHasLineDirectives();
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001320
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001321 break;
1322 }
1323
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001324 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001325 const char *Name = BlobStart;
1326 unsigned Offset = Record[0];
1327 unsigned Code = SLocEntryCursor.ReadCode();
1328 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001329 unsigned RecCode
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001330 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001331
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001332 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001333 Error("AST record has invalid code");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001334 return Failure;
1335 }
1336
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001337 llvm::MemoryBuffer *Buffer
Chris Lattnera0a270c2010-04-05 22:42:27 +00001338 = llvm::MemoryBuffer::getMemBuffer(llvm::StringRef(BlobStart, BlobLen - 1),
1339 Name);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001340 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID, Offset);
Mike Stump1eb44332009-09-09 15:08:12 +00001341
Douglas Gregor92b059e2009-04-28 20:33:11 +00001342 if (strcmp(Name, "<built-in>") == 0) {
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +00001343 PCHPredefinesBlock Block = {
1344 BufferID,
1345 llvm::StringRef(BlobStart, BlobLen - 1)
1346 };
1347 PCHPredefinesBuffers.push_back(Block);
Douglas Gregor92b059e2009-04-28 20:33:11 +00001348 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001349
1350 break;
1351 }
1352
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001353 case SM_SLOC_INSTANTIATION_ENTRY: {
Sebastian Redlc3632732010-10-05 15:59:54 +00001354 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001355 SourceMgr.createInstantiationLoc(SpellingLoc,
Sebastian Redlc3632732010-10-05 15:59:54 +00001356 ReadSourceLocation(*F, Record[2]),
1357 ReadSourceLocation(*F, Record[3]),
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001358 Record[4],
1359 ID,
1360 Record[0]);
1361 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001362 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001363 }
1364
1365 return Success;
1366}
1367
Chris Lattner6367f6d2009-04-27 01:05:14 +00001368/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1369/// specified cursor. Read the abbreviations that are at the top of the block
1370/// and then leave the cursor pointing into the block.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001371bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattner6367f6d2009-04-27 01:05:14 +00001372 unsigned BlockID) {
1373 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001374 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001375 return Failure;
1376 }
Mike Stump1eb44332009-09-09 15:08:12 +00001377
Chris Lattner6367f6d2009-04-27 01:05:14 +00001378 while (true) {
Douglas Gregorecdcb882010-10-20 22:00:55 +00001379 uint64_t Offset = Cursor.GetCurrentBitNo();
Chris Lattner6367f6d2009-04-27 01:05:14 +00001380 unsigned Code = Cursor.ReadCode();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001381
Chris Lattner6367f6d2009-04-27 01:05:14 +00001382 // We expect all abbrevs to be at the start of the block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001383 if (Code != llvm::bitc::DEFINE_ABBREV) {
1384 Cursor.JumpToBit(Offset);
Chris Lattner6367f6d2009-04-27 01:05:14 +00001385 return false;
Douglas Gregorecdcb882010-10-20 22:00:55 +00001386 }
Chris Lattner6367f6d2009-04-27 01:05:14 +00001387 Cursor.ReadAbbrevRecord();
1388 }
1389}
1390
Douglas Gregor89d99802010-11-30 06:16:57 +00001391PreprocessedEntity *ASTReader::ReadMacroRecord(PerFileData &F, uint64_t Offset) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001392 assert(PP && "Forgot to set Preprocessor ?");
Douglas Gregorecdcb882010-10-20 22:00:55 +00001393 llvm::BitstreamCursor &Stream = F.MacroCursor;
Mike Stump1eb44332009-09-09 15:08:12 +00001394
Douglas Gregor37e26842009-04-21 23:56:24 +00001395 // Keep track of where we are in the stream, then jump back there
1396 // after reading this macro.
1397 SavedStreamPosition SavedPosition(Stream);
1398
1399 Stream.JumpToBit(Offset);
1400 RecordData Record;
1401 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1402 MacroInfo *Macro = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001403
Douglas Gregor37e26842009-04-21 23:56:24 +00001404 while (true) {
1405 unsigned Code = Stream.ReadCode();
1406 switch (Code) {
1407 case llvm::bitc::END_BLOCK:
Douglas Gregor89d99802010-11-30 06:16:57 +00001408 return 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001409
1410 case llvm::bitc::ENTER_SUBBLOCK:
1411 // No known subblocks, always skip them.
1412 Stream.ReadSubBlockID();
1413 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001414 Error("malformed block record in AST file");
Douglas Gregor89d99802010-11-30 06:16:57 +00001415 return 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001416 }
1417 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001418
Douglas Gregor37e26842009-04-21 23:56:24 +00001419 case llvm::bitc::DEFINE_ABBREV:
1420 Stream.ReadAbbrevRecord();
1421 continue;
1422 default: break;
1423 }
1424
1425 // Read a record.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001426 const char *BlobStart = 0;
1427 unsigned BlobLen = 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001428 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001429 PreprocessorRecordTypes RecType =
Michael J. Spencer20249a12010-10-21 03:16:25 +00001430 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart,
Douglas Gregorecdcb882010-10-20 22:00:55 +00001431 BlobLen);
Douglas Gregor37e26842009-04-21 23:56:24 +00001432 switch (RecType) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001433 case PP_MACRO_OBJECT_LIKE:
1434 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001435 // If we already have a macro, that means that we've hit the end
1436 // of the definition of the macro we were looking for. We're
1437 // done.
1438 if (Macro)
Douglas Gregor89d99802010-11-30 06:16:57 +00001439 return 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001440
1441 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1442 if (II == 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001443 Error("macro must have a name in AST file");
Douglas Gregor89d99802010-11-30 06:16:57 +00001444 return 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001445 }
Sebastian Redlc3632732010-10-05 15:59:54 +00001446 SourceLocation Loc = ReadSourceLocation(F, Record[1]);
Douglas Gregor37e26842009-04-21 23:56:24 +00001447 bool isUsed = Record[2];
Mike Stump1eb44332009-09-09 15:08:12 +00001448
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001449 MacroInfo *MI = PP->AllocateMacroInfo(Loc);
Douglas Gregor37e26842009-04-21 23:56:24 +00001450 MI->setIsUsed(isUsed);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001451 MI->setIsFromAST();
Mike Stump1eb44332009-09-09 15:08:12 +00001452
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001453 unsigned NextIndex = 3;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001454 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregor37e26842009-04-21 23:56:24 +00001455 // Decode function-like macro info.
1456 bool isC99VarArgs = Record[3];
1457 bool isGNUVarArgs = Record[4];
1458 MacroArgs.clear();
1459 unsigned NumArgs = Record[5];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001460 NextIndex = 6 + NumArgs;
Douglas Gregor37e26842009-04-21 23:56:24 +00001461 for (unsigned i = 0; i != NumArgs; ++i)
1462 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1463
1464 // Install function-like macro info.
1465 MI->setIsFunctionLike();
1466 if (isC99VarArgs) MI->setIsC99Varargs();
1467 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor75fdb232009-05-22 22:45:36 +00001468 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001469 PP->getPreprocessorAllocator());
Douglas Gregor37e26842009-04-21 23:56:24 +00001470 }
1471
1472 // Finally, install the macro.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001473 PP->setMacroInfo(II, MI);
Douglas Gregor37e26842009-04-21 23:56:24 +00001474
1475 // Remember that we saw this macro last so that we add the tokens that
1476 // form its body to it.
1477 Macro = MI;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001478
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001479 if (NextIndex + 1 == Record.size() && PP->getPreprocessingRecord()) {
1480 // We have a macro definition. Load it now.
1481 PP->getPreprocessingRecord()->RegisterMacroDefinition(Macro,
1482 getMacroDefinition(Record[NextIndex]));
1483 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001484
Douglas Gregor37e26842009-04-21 23:56:24 +00001485 ++NumMacrosRead;
1486 break;
1487 }
Mike Stump1eb44332009-09-09 15:08:12 +00001488
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001489 case PP_TOKEN: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001490 // If we see a TOKEN before a PP_MACRO_*, then the file is
1491 // erroneous, just pretend we didn't see this.
1492 if (Macro == 0) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001493
Douglas Gregor37e26842009-04-21 23:56:24 +00001494 Token Tok;
1495 Tok.startToken();
Sebastian Redlc3632732010-10-05 15:59:54 +00001496 Tok.setLocation(ReadSourceLocation(F, Record[0]));
Douglas Gregor37e26842009-04-21 23:56:24 +00001497 Tok.setLength(Record[1]);
1498 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1499 Tok.setIdentifierInfo(II);
1500 Tok.setKind((tok::TokenKind)Record[3]);
1501 Tok.setFlag((Token::TokenFlags)Record[4]);
1502 Macro->AddTokenToBody(Tok);
1503 break;
1504 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001505 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001506 }
Douglas Gregor89d99802010-11-30 06:16:57 +00001507
1508 return 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001509}
1510
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001511PreprocessedEntity *ASTReader::LoadPreprocessedEntity(PerFileData &F) {
1512 assert(PP && "Forgot to set Preprocessor ?");
1513 unsigned Code = F.PreprocessorDetailCursor.ReadCode();
1514 switch (Code) {
1515 case llvm::bitc::END_BLOCK:
1516 return 0;
1517
1518 case llvm::bitc::ENTER_SUBBLOCK:
1519 Error("unexpected subblock record in preprocessor detail block");
1520 return 0;
1521
1522 case llvm::bitc::DEFINE_ABBREV:
1523 Error("unexpected abbrevation record in preprocessor detail block");
1524 return 0;
1525
1526 default:
1527 break;
1528 }
1529
1530 if (!PP->getPreprocessingRecord()) {
1531 Error("no preprocessing record");
1532 return 0;
1533 }
1534
1535 // Read the record.
1536 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1537 const char *BlobStart = 0;
1538 unsigned BlobLen = 0;
1539 RecordData Record;
1540 PreprocessorDetailRecordTypes RecType =
1541 (PreprocessorDetailRecordTypes)F.PreprocessorDetailCursor.ReadRecord(
1542 Code, Record, BlobStart, BlobLen);
1543 switch (RecType) {
1544 case PPD_MACRO_INSTANTIATION: {
1545 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1546 return PE;
1547
1548 MacroInstantiation *MI
1549 = new (PPRec) MacroInstantiation(DecodeIdentifierInfo(Record[3]),
1550 SourceRange(ReadSourceLocation(F, Record[1]),
1551 ReadSourceLocation(F, Record[2])),
1552 getMacroDefinition(Record[4]));
1553 PPRec.SetPreallocatedEntity(Record[0], MI);
1554 return MI;
1555 }
1556
1557 case PPD_MACRO_DEFINITION: {
1558 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1559 return PE;
1560
1561 if (Record[1] > MacroDefinitionsLoaded.size()) {
1562 Error("out-of-bounds macro definition record");
1563 return 0;
1564 }
1565
1566 // Decode the identifier info and then check again; if the macro is
1567 // still defined and associated with the identifier,
1568 IdentifierInfo *II = DecodeIdentifierInfo(Record[4]);
1569 if (!MacroDefinitionsLoaded[Record[1] - 1]) {
1570 MacroDefinition *MD
1571 = new (PPRec) MacroDefinition(II,
1572 ReadSourceLocation(F, Record[5]),
1573 SourceRange(
1574 ReadSourceLocation(F, Record[2]),
1575 ReadSourceLocation(F, Record[3])));
1576
1577 PPRec.SetPreallocatedEntity(Record[0], MD);
1578 MacroDefinitionsLoaded[Record[1] - 1] = MD;
1579
1580 if (DeserializationListener)
1581 DeserializationListener->MacroDefinitionRead(Record[1], MD);
1582 }
1583
1584 return MacroDefinitionsLoaded[Record[1] - 1];
1585 }
1586
1587 case PPD_INCLUSION_DIRECTIVE: {
1588 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1589 return PE;
1590
1591 const char *FullFileNameStart = BlobStart + Record[3];
1592 const FileEntry *File
1593 = PP->getFileManager().getFile(llvm::StringRef(FullFileNameStart,
1594 BlobLen - Record[3]));
1595
1596 // FIXME: Stable encoding
1597 InclusionDirective::InclusionKind Kind
1598 = static_cast<InclusionDirective::InclusionKind>(Record[5]);
1599 InclusionDirective *ID
1600 = new (PPRec) InclusionDirective(PPRec, Kind,
1601 llvm::StringRef(BlobStart, Record[3]),
1602 Record[4],
1603 File,
1604 SourceRange(ReadSourceLocation(F, Record[1]),
1605 ReadSourceLocation(F, Record[2])));
1606 PPRec.SetPreallocatedEntity(Record[0], ID);
1607 return ID;
1608 }
1609 }
1610
1611 Error("invalid offset in preprocessor detail block");
1612 return 0;
1613}
1614
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001615namespace {
1616 /// \brief Trait class used to search the on-disk hash table containing all of
1617 /// the header search information.
1618 ///
1619 /// The on-disk hash table contains a mapping from each header path to
1620 /// information about that header (how many times it has been included, its
1621 /// controlling macro, etc.). Note that we actually hash based on the
1622 /// filename, and support "deep" comparisons of file names based on current
1623 /// inode numbers, so that the search can cope with non-normalized path names
1624 /// and symlinks.
1625 class HeaderFileInfoTrait {
1626 const char *SearchPath;
1627 struct stat SearchPathStatBuf;
1628 llvm::Optional<int> SearchPathStatResult;
1629
1630 int StatSimpleCache(const char *Path, struct stat *StatBuf) {
1631 if (Path == SearchPath) {
1632 if (!SearchPathStatResult)
1633 SearchPathStatResult = stat(Path, &SearchPathStatBuf);
1634
1635 *StatBuf = SearchPathStatBuf;
1636 return *SearchPathStatResult;
1637 }
1638
1639 return stat(Path, StatBuf);
1640 }
1641
1642 public:
1643 typedef const char *external_key_type;
1644 typedef const char *internal_key_type;
1645
1646 typedef HeaderFileInfo data_type;
1647
1648 HeaderFileInfoTrait(const char *SearchPath = 0) : SearchPath(SearchPath) { }
1649
1650 static unsigned ComputeHash(const char *path) {
1651 return llvm::HashString(llvm::sys::path::filename(path));
1652 }
1653
1654 static internal_key_type GetInternalKey(const char *path) { return path; }
1655
1656 bool EqualKey(internal_key_type a, internal_key_type b) {
1657 if (strcmp(a, b) == 0)
1658 return true;
1659
1660 if (llvm::sys::path::filename(a) != llvm::sys::path::filename(b))
1661 return false;
1662
1663 // The file names match, but the path names don't. stat() the files to
1664 // see if they are the same.
1665 struct stat StatBufA, StatBufB;
1666 if (StatSimpleCache(a, &StatBufA) || StatSimpleCache(b, &StatBufB))
1667 return false;
1668
1669 return StatBufA.st_ino == StatBufB.st_ino;
1670 }
1671
1672 static std::pair<unsigned, unsigned>
1673 ReadKeyDataLength(const unsigned char*& d) {
1674 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1675 unsigned DataLen = (unsigned) *d++;
1676 return std::make_pair(KeyLen + 1, DataLen);
1677 }
1678
1679 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
1680 return (const char *)d;
1681 }
1682
1683 static data_type ReadData(const internal_key_type, const unsigned char *d,
1684 unsigned DataLen) {
1685 const unsigned char *End = d + DataLen;
1686 using namespace clang::io;
1687 HeaderFileInfo HFI;
1688 unsigned Flags = *d++;
1689 HFI.isImport = (Flags >> 3) & 0x01;
1690 HFI.DirInfo = (Flags >> 1) & 0x03;
1691 HFI.Resolved = Flags & 0x01;
1692 HFI.NumIncludes = ReadUnalignedLE16(d);
1693 HFI.ControllingMacroID = ReadUnalignedLE32(d);
1694 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1695 (void)End;
1696
1697 // This HeaderFileInfo was externally loaded.
1698 HFI.External = true;
1699 return HFI;
1700 }
1701 };
1702}
1703
1704/// \brief The on-disk hash table used for the global method pool.
1705typedef OnDiskChainedHashTable<HeaderFileInfoTrait>
1706 HeaderFileInfoLookupTable;
1707
Douglas Gregor295a2a62010-10-30 00:23:06 +00001708void ASTReader::SetIdentifierIsMacro(IdentifierInfo *II, PerFileData &F,
1709 uint64_t Offset) {
1710 // Note that this identifier has a macro definition.
1711 II->setHasMacroDefinition(true);
1712
1713 // Adjust the offset based on our position in the chain.
1714 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1715 if (Chain[I] == &F)
1716 break;
1717
1718 Offset += Chain[I]->SizeInBits;
1719 }
1720
1721 UnreadMacroRecordOffsets[II] = Offset;
1722}
1723
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001724void ASTReader::ReadDefinedMacros() {
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001725 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redlc3632732010-10-05 15:59:54 +00001726 PerFileData &F = *Chain[N - I - 1];
1727 llvm::BitstreamCursor &MacroCursor = F.MacroCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001728
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001729 // If there was no preprocessor block, skip this file.
1730 if (!MacroCursor.getBitStreamReader())
1731 continue;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001732
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001733 llvm::BitstreamCursor Cursor = MacroCursor;
Douglas Gregorecdcb882010-10-20 22:00:55 +00001734 Cursor.JumpToBit(F.MacroStartOffset);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001735
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001736 RecordData Record;
1737 while (true) {
1738 unsigned Code = Cursor.ReadCode();
Douglas Gregorecdcb882010-10-20 22:00:55 +00001739 if (Code == llvm::bitc::END_BLOCK)
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001740 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001741
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001742 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1743 // No known subblocks, always skip them.
1744 Cursor.ReadSubBlockID();
1745 if (Cursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001746 Error("malformed block record in AST file");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001747 return;
1748 }
1749 continue;
1750 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001751
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001752 if (Code == llvm::bitc::DEFINE_ABBREV) {
1753 Cursor.ReadAbbrevRecord();
1754 continue;
1755 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001756
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001757 // Read a record.
1758 const char *BlobStart;
1759 unsigned BlobLen;
1760 Record.clear();
1761 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1762 default: // Default behavior: ignore.
1763 break;
Douglas Gregor88a35862010-01-04 19:18:44 +00001764
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001765 case PP_MACRO_OBJECT_LIKE:
1766 case PP_MACRO_FUNCTION_LIKE:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001767 DecodeIdentifierInfo(Record[0]);
1768 break;
1769
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001770 case PP_TOKEN:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001771 // Ignore tokens.
1772 break;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001773 }
Douglas Gregor88a35862010-01-04 19:18:44 +00001774 }
1775 }
Douglas Gregor295a2a62010-10-30 00:23:06 +00001776
1777 // Drain the unread macro-record offsets map.
1778 while (!UnreadMacroRecordOffsets.empty())
1779 LoadMacroDefinition(UnreadMacroRecordOffsets.begin());
1780}
1781
1782void ASTReader::LoadMacroDefinition(
1783 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos) {
1784 assert(Pos != UnreadMacroRecordOffsets.end() && "Unknown macro definition");
1785 PerFileData *F = 0;
1786 uint64_t Offset = Pos->second;
1787 UnreadMacroRecordOffsets.erase(Pos);
1788
1789 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1790 if (Offset < Chain[I]->SizeInBits) {
1791 F = Chain[I];
1792 break;
1793 }
1794
1795 Offset -= Chain[I]->SizeInBits;
1796 }
1797 if (!F) {
1798 Error("Malformed macro record offset");
1799 return;
1800 }
1801
1802 ReadMacroRecord(*F, Offset);
1803}
1804
1805void ASTReader::LoadMacroDefinition(IdentifierInfo *II) {
1806 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos
1807 = UnreadMacroRecordOffsets.find(II);
1808 LoadMacroDefinition(Pos);
Douglas Gregor88a35862010-01-04 19:18:44 +00001809}
1810
Sebastian Redlf73c93f2010-09-15 19:54:06 +00001811MacroDefinition *ASTReader::getMacroDefinition(MacroID ID) {
Douglas Gregor77424bc2010-10-02 19:29:26 +00001812 if (ID == 0 || ID > MacroDefinitionsLoaded.size())
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001813 return 0;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001814
Douglas Gregor77424bc2010-10-02 19:29:26 +00001815 if (!MacroDefinitionsLoaded[ID - 1]) {
1816 unsigned Index = ID - 1;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001817 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1818 PerFileData &F = *Chain[N - I - 1];
1819 if (Index < F.LocalNumMacroDefinitions) {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001820 SavedStreamPosition SavedPosition(F.PreprocessorDetailCursor);
1821 F.PreprocessorDetailCursor.JumpToBit(F.MacroDefinitionOffsets[Index]);
1822 LoadPreprocessedEntity(F);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001823 break;
1824 }
1825 Index -= F.LocalNumMacroDefinitions;
1826 }
Douglas Gregor77424bc2010-10-02 19:29:26 +00001827 assert(MacroDefinitionsLoaded[ID - 1] && "Broken chain");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001828 }
1829
Douglas Gregor77424bc2010-10-02 19:29:26 +00001830 return MacroDefinitionsLoaded[ID - 1];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001831}
1832
Douglas Gregore650c8c2009-07-07 00:12:59 +00001833/// \brief If we are loading a relocatable PCH file, and the filename is
1834/// not an absolute path, add the system root to the beginning of the file
1835/// name.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001836void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001837 // If this is not a relocatable PCH file, there's nothing to do.
1838 if (!RelocatablePCH)
1839 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001840
Michael J. Spencer256053b2010-12-17 21:22:22 +00001841 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
Douglas Gregore650c8c2009-07-07 00:12:59 +00001842 return;
1843
Douglas Gregore650c8c2009-07-07 00:12:59 +00001844 if (isysroot == 0) {
1845 // If no system root was given, default to '/'
1846 Filename.insert(Filename.begin(), '/');
1847 return;
1848 }
Mike Stump1eb44332009-09-09 15:08:12 +00001849
Douglas Gregore650c8c2009-07-07 00:12:59 +00001850 unsigned Length = strlen(isysroot);
1851 if (isysroot[Length - 1] != '/')
1852 Filename.insert(Filename.begin(), '/');
Mike Stump1eb44332009-09-09 15:08:12 +00001853
Douglas Gregore650c8c2009-07-07 00:12:59 +00001854 Filename.insert(Filename.begin(), isysroot, isysroot + Length);
1855}
1856
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001857ASTReader::ASTReadResult
Sebastian Redl571db7f2010-08-18 23:56:56 +00001858ASTReader::ReadASTBlock(PerFileData &F) {
Sebastian Redl9137a522010-07-16 17:50:48 +00001859 llvm::BitstreamCursor &Stream = F.Stream;
1860
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001861 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001862 Error("malformed block record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001863 return Failure;
1864 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001865
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001866 // Read all of the records and blocks for the ASt file.
Douglas Gregor8038d512009-04-10 17:25:41 +00001867 RecordData Record;
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001868 bool First = true;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001869 while (!Stream.AtEndOfStream()) {
1870 unsigned Code = Stream.ReadCode();
1871 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001872 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001873 Error("error at end of module block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001874 return Failure;
1875 }
Chris Lattner7356a312009-04-11 21:15:38 +00001876
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001877 return Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001878 }
1879
1880 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1881 switch (Stream.ReadSubBlockID()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001882 case DECLTYPES_BLOCK_ID:
Chris Lattner6367f6d2009-04-27 01:05:14 +00001883 // We lazily load the decls block, but we want to set up the
1884 // DeclsCursor cursor to point into it. Clone our current bitcode
1885 // cursor to it, enter the block and read the abbrevs in that block.
1886 // With the main cursor, we just skip over it.
Sebastian Redl9137a522010-07-16 17:50:48 +00001887 F.DeclsCursor = Stream;
Chris Lattner6367f6d2009-04-27 01:05:14 +00001888 if (Stream.SkipBlock() || // Skip with the main cursor.
1889 // Read the abbrevs.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001890 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001891 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001892 return Failure;
1893 }
1894 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001895
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00001896 case DECL_UPDATES_BLOCK_ID:
1897 if (Stream.SkipBlock()) {
1898 Error("malformed block record in AST file");
1899 return Failure;
1900 }
1901 break;
1902
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001903 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl9137a522010-07-16 17:50:48 +00001904 F.MacroCursor = Stream;
Douglas Gregor88a35862010-01-04 19:18:44 +00001905 if (PP)
1906 PP->setExternalSource(this);
1907
Douglas Gregorecdcb882010-10-20 22:00:55 +00001908 if (Stream.SkipBlock() ||
1909 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001910 Error("malformed block record in AST file");
Chris Lattner7356a312009-04-11 21:15:38 +00001911 return Failure;
1912 }
Douglas Gregorecdcb882010-10-20 22:00:55 +00001913 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
Chris Lattner7356a312009-04-11 21:15:38 +00001914 break;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00001915
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001916 case PREPROCESSOR_DETAIL_BLOCK_ID:
1917 F.PreprocessorDetailCursor = Stream;
1918 if (Stream.SkipBlock() ||
1919 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
1920 PREPROCESSOR_DETAIL_BLOCK_ID)) {
1921 Error("malformed preprocessor detail record in AST file");
1922 return Failure;
1923 }
1924 F.PreprocessorDetailStartOffset
1925 = F.PreprocessorDetailCursor.GetCurrentBitNo();
1926 break;
1927
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001928 case SOURCE_MANAGER_BLOCK_ID:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001929 switch (ReadSourceManagerBlock(F)) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00001930 case Success:
1931 break;
1932
1933 case Failure:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001934 Error("malformed source manager block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001935 return Failure;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001936
1937 case IgnorePCH:
1938 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001939 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001940 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001941 }
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001942 First = false;
Douglas Gregor8038d512009-04-10 17:25:41 +00001943 continue;
1944 }
1945
1946 if (Code == llvm::bitc::DEFINE_ABBREV) {
1947 Stream.ReadAbbrevRecord();
1948 continue;
1949 }
1950
1951 // Read and process a record.
1952 Record.clear();
Douglas Gregor2bec0412009-04-10 21:16:55 +00001953 const char *BlobStart = 0;
1954 unsigned BlobLen = 0;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001955 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00001956 &BlobStart, &BlobLen)) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001957 default: // Default behavior: ignore.
1958 break;
1959
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001960 case METADATA: {
1961 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1962 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001963 : diag::warn_pch_version_too_new);
1964 return IgnorePCH;
1965 }
1966
1967 RelocatablePCH = Record[4];
1968 if (Listener) {
1969 std::string TargetTriple(BlobStart, BlobLen);
1970 if (Listener->ReadTargetTriple(TargetTriple))
1971 return IgnorePCH;
1972 }
1973 break;
1974 }
1975
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001976 case CHAINED_METADATA: {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001977 if (!First) {
1978 Error("CHAINED_METADATA is not first record in block");
1979 return Failure;
1980 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001981 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1982 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001983 : diag::warn_pch_version_too_new);
1984 return IgnorePCH;
1985 }
1986
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00001987 // Load the chained file, which is always a PCH file.
1988 switch(ReadASTCore(llvm::StringRef(BlobStart, BlobLen), PCH)) {
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001989 case Failure: return Failure;
1990 // If we have to ignore the dependency, we'll have to ignore this too.
1991 case IgnorePCH: return IgnorePCH;
1992 case Success: break;
1993 }
1994 break;
1995 }
1996
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001997 case TYPE_OFFSET:
Sebastian Redl12d6da02010-07-19 22:06:55 +00001998 if (F.LocalNumTypes != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001999 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002000 return Failure;
2001 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00002002 F.TypeOffsets = (const uint32_t *)BlobStart;
2003 F.LocalNumTypes = Record[0];
Douglas Gregor8038d512009-04-10 17:25:41 +00002004 break;
2005
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002006 case DECL_OFFSET:
Sebastian Redl12d6da02010-07-19 22:06:55 +00002007 if (F.LocalNumDecls != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002008 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002009 return Failure;
2010 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00002011 F.DeclOffsets = (const uint32_t *)BlobStart;
2012 F.LocalNumDecls = Record[0];
Douglas Gregor8038d512009-04-10 17:25:41 +00002013 break;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002014
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002015 case TU_UPDATE_LEXICAL: {
Sebastian Redld692af72010-07-27 18:24:41 +00002016 DeclContextInfo Info = {
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002017 /* No visible information */ 0,
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002018 reinterpret_cast<const KindDeclIDPair *>(BlobStart),
2019 BlobLen / sizeof(KindDeclIDPair)
Sebastian Redld692af72010-07-27 18:24:41 +00002020 };
Douglas Gregor3747ee72010-10-01 01:18:02 +00002021 DeclContextOffsets[Context ? Context->getTranslationUnitDecl() : 0]
2022 .push_back(Info);
Sebastian Redld692af72010-07-27 18:24:41 +00002023 break;
2024 }
2025
Sebastian Redle1dde812010-08-24 00:50:04 +00002026 case UPDATE_VISIBLE: {
2027 serialization::DeclID ID = Record[0];
2028 void *Table = ASTDeclContextNameLookupTable::Create(
2029 (const unsigned char *)BlobStart + Record[1],
2030 (const unsigned char *)BlobStart,
2031 ASTDeclContextNameLookupTrait(*this));
Douglas Gregor3747ee72010-10-01 01:18:02 +00002032 if (ID == 1 && Context) { // Is it the TU?
Sebastian Redle1dde812010-08-24 00:50:04 +00002033 DeclContextInfo Info = {
2034 Table, /* No lexical inforamtion */ 0, 0
2035 };
2036 DeclContextOffsets[Context->getTranslationUnitDecl()].push_back(Info);
2037 } else
2038 PendingVisibleUpdates[ID].push_back(Table);
2039 break;
2040 }
2041
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002042 case REDECLS_UPDATE_LATEST: {
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00002043 assert(Record.size() % 2 == 0 && "Expected pairs of DeclIDs");
2044 for (unsigned i = 0, e = Record.size(); i < e; i += 2) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002045 DeclID First = Record[i], Latest = Record[i+1];
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00002046 assert((FirstLatestDeclIDs.find(First) == FirstLatestDeclIDs.end() ||
2047 Latest > FirstLatestDeclIDs[First]) &&
2048 "The new latest is supposed to come after the previous latest");
2049 FirstLatestDeclIDs[First] = Latest;
2050 }
2051 break;
2052 }
2053
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002054 case LANGUAGE_OPTIONS:
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00002055 if (ParseLanguageOptions(Record) && !DisableValidation)
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002056 return IgnorePCH;
2057 break;
Douglas Gregor2bec0412009-04-10 21:16:55 +00002058
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002059 case IDENTIFIER_TABLE:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002060 F.IdentifierTableData = BlobStart;
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002061 if (Record[0]) {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002062 F.IdentifierLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002063 = ASTIdentifierLookupTable::Create(
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002064 (const unsigned char *)F.IdentifierTableData + Record[0],
2065 (const unsigned char *)F.IdentifierTableData,
Sebastian Redlc3632732010-10-05 15:59:54 +00002066 ASTIdentifierLookupTrait(*this, F));
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002067 if (PP)
2068 PP->getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002069 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002070 break;
2071
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002072 case IDENTIFIER_OFFSET:
Sebastian Redl2da08f92010-07-19 22:28:42 +00002073 if (F.LocalNumIdentifiers != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002074 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00002075 return Failure;
2076 }
Sebastian Redl2da08f92010-07-19 22:28:42 +00002077 F.IdentifierOffsets = (const uint32_t *)BlobStart;
2078 F.LocalNumIdentifiers = Record[0];
Douglas Gregorafaf3082009-04-11 00:14:32 +00002079 break;
Douglas Gregorfdd01722009-04-14 00:24:19 +00002080
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002081 case EXTERNAL_DEFINITIONS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002082 // Optimization for the first block.
2083 if (ExternalDefinitions.empty())
2084 ExternalDefinitions.swap(Record);
2085 else
2086 ExternalDefinitions.insert(ExternalDefinitions.end(),
2087 Record.begin(), Record.end());
Douglas Gregorfdd01722009-04-14 00:24:19 +00002088 break;
Douglas Gregor3e1af842009-04-17 22:13:46 +00002089
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002090 case SPECIAL_TYPES:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002091 // Optimization for the first block
2092 if (SpecialTypes.empty())
2093 SpecialTypes.swap(Record);
2094 else
2095 SpecialTypes.insert(SpecialTypes.end(), Record.begin(), Record.end());
Douglas Gregorad1de002009-04-18 05:55:16 +00002096 break;
2097
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002098 case STATISTICS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002099 TotalNumStatements += Record[0];
2100 TotalNumMacros += Record[1];
2101 TotalLexicalDeclContexts += Record[2];
2102 TotalVisibleDeclContexts += Record[3];
Douglas Gregor3e1af842009-04-17 22:13:46 +00002103 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002104
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002105 case TENTATIVE_DEFINITIONS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002106 // Optimization for the first block.
2107 if (TentativeDefinitions.empty())
2108 TentativeDefinitions.swap(Record);
2109 else
2110 TentativeDefinitions.insert(TentativeDefinitions.end(),
2111 Record.begin(), Record.end());
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002112 break;
Douglas Gregor14c22f22009-04-22 22:18:58 +00002113
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002114 case UNUSED_FILESCOPED_DECLS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002115 // Optimization for the first block.
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002116 if (UnusedFileScopedDecls.empty())
2117 UnusedFileScopedDecls.swap(Record);
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002118 else
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002119 UnusedFileScopedDecls.insert(UnusedFileScopedDecls.end(),
2120 Record.begin(), Record.end());
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002121 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002122
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002123 case WEAK_UNDECLARED_IDENTIFIERS:
Sebastian Redl40566802010-08-05 18:21:25 +00002124 // Later blocks overwrite earlier ones.
2125 WeakUndeclaredIdentifiers.swap(Record);
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002126 break;
2127
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002128 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002129 // Optimization for the first block.
2130 if (LocallyScopedExternalDecls.empty())
2131 LocallyScopedExternalDecls.swap(Record);
2132 else
2133 LocallyScopedExternalDecls.insert(LocallyScopedExternalDecls.end(),
2134 Record.begin(), Record.end());
Douglas Gregor14c22f22009-04-22 22:18:58 +00002135 break;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002136
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002137 case SELECTOR_OFFSETS:
Sebastian Redl059612d2010-08-03 21:58:15 +00002138 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redl725cd962010-08-04 20:40:17 +00002139 F.LocalNumSelectors = Record[0];
Douglas Gregor83941df2009-04-25 17:48:32 +00002140 break;
2141
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002142 case METHOD_POOL:
Sebastian Redl725cd962010-08-04 20:40:17 +00002143 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor83941df2009-04-25 17:48:32 +00002144 if (Record[0])
Sebastian Redl725cd962010-08-04 20:40:17 +00002145 F.SelectorLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002146 = ASTSelectorLookupTable::Create(
Sebastian Redl725cd962010-08-04 20:40:17 +00002147 F.SelectorLookupTableData + Record[0],
2148 F.SelectorLookupTableData,
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002149 ASTSelectorLookupTrait(*this));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002150 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002151 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002152
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002153 case REFERENCED_SELECTOR_POOL:
Sebastian Redlc3632732010-10-05 15:59:54 +00002154 F.ReferencedSelectorsData.swap(Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002155 break;
2156
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002157 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002158 if (!Record.empty() && Listener)
2159 Listener->ReadCounter(Record[0]);
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002160 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002161
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002162 case SOURCE_LOCATION_OFFSETS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002163 F.SLocOffsets = (const uint32_t *)BlobStart;
2164 F.LocalNumSLocEntries = Record[0];
Sebastian Redl8db9fae2010-09-22 20:19:08 +00002165 F.LocalSLocSize = Record[1];
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002166 break;
2167
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002168 case SOURCE_LOCATION_PRELOADS:
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002169 if (PreloadSLocEntries.empty())
2170 PreloadSLocEntries.swap(Record);
2171 else
2172 PreloadSLocEntries.insert(PreloadSLocEntries.end(),
2173 Record.begin(), Record.end());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002174 break;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002175
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002176 case STAT_CACHE: {
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00002177 if (!DisableStatCache) {
2178 ASTStatCache *MyStatCache =
2179 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
2180 (const unsigned char *)BlobStart,
2181 NumStatHits, NumStatMisses);
2182 FileMgr.addStatCache(MyStatCache);
2183 F.StatCache = MyStatCache;
2184 }
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002185 break;
Douglas Gregor52e71082009-10-16 18:18:30 +00002186 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002187
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002188 case EXT_VECTOR_DECLS:
Sebastian Redla9f23682010-07-28 21:38:49 +00002189 // Optimization for the first block.
2190 if (ExtVectorDecls.empty())
2191 ExtVectorDecls.swap(Record);
2192 else
2193 ExtVectorDecls.insert(ExtVectorDecls.end(),
2194 Record.begin(), Record.end());
Douglas Gregorb81c1702009-04-27 20:06:05 +00002195 break;
2196
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002197 case VTABLE_USES:
Sebastian Redl40566802010-08-05 18:21:25 +00002198 // Later tables overwrite earlier ones.
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002199 VTableUses.swap(Record);
2200 break;
2201
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002202 case DYNAMIC_CLASSES:
Sebastian Redl40566802010-08-05 18:21:25 +00002203 // Optimization for the first block.
2204 if (DynamicClasses.empty())
2205 DynamicClasses.swap(Record);
2206 else
2207 DynamicClasses.insert(DynamicClasses.end(),
2208 Record.begin(), Record.end());
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002209 break;
2210
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002211 case PENDING_IMPLICIT_INSTANTIATIONS:
Sebastian Redlc3632732010-10-05 15:59:54 +00002212 F.PendingInstantiations.swap(Record);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002213 break;
2214
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002215 case SEMA_DECL_REFS:
Sebastian Redl40566802010-08-05 18:21:25 +00002216 // Later tables overwrite earlier ones.
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002217 SemaDeclRefs.swap(Record);
2218 break;
2219
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002220 case ORIGINAL_FILE_NAME:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002221 // The primary AST will be the last to get here, so it will be the one
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002222 // that's used.
Daniel Dunbar7b5a1212009-11-11 05:29:04 +00002223 ActualOriginalFileName.assign(BlobStart, BlobLen);
2224 OriginalFileName = ActualOriginalFileName;
Douglas Gregore650c8c2009-07-07 00:12:59 +00002225 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregorb64c1932009-05-12 01:31:05 +00002226 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002227
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002228 case ORIGINAL_PCH_DIR:
2229 // The primary AST will be the last to get here, so it will be the one
2230 // that's used.
2231 OriginalDir.assign(BlobStart, BlobLen);
2232 break;
2233
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002234 case VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek974be4d2010-02-12 23:31:14 +00002235 const std::string &CurBranch = getClangFullRepositoryVersion();
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002236 llvm::StringRef ASTBranch(BlobStart, BlobLen);
2237 if (llvm::StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2238 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregor445e23e2009-10-05 21:07:28 +00002239 return IgnorePCH;
2240 }
2241 break;
2242 }
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002243
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002244 case MACRO_DEFINITION_OFFSETS:
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002245 F.MacroDefinitionOffsets = (const uint32_t *)BlobStart;
2246 F.NumPreallocatedPreprocessingEntities = Record[0];
2247 F.LocalNumMacroDefinitions = Record[1];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002248 break;
Sebastian Redl0b17c612010-08-13 00:28:03 +00002249
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002250 case DECL_UPDATE_OFFSETS: {
2251 if (Record.size() % 2 != 0) {
2252 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2253 return Failure;
2254 }
2255 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
2256 DeclUpdateOffsets[static_cast<DeclID>(Record[I])]
2257 .push_back(std::make_pair(&F, Record[I+1]));
2258 break;
2259 }
2260
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002261 case DECL_REPLACEMENTS: {
Sebastian Redl0b17c612010-08-13 00:28:03 +00002262 if (Record.size() % 2 != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002263 Error("invalid DECL_REPLACEMENTS block in AST file");
Sebastian Redl0b17c612010-08-13 00:28:03 +00002264 return Failure;
2265 }
2266 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002267 ReplacedDecls[static_cast<DeclID>(Record[I])] =
Sebastian Redl0b17c612010-08-13 00:28:03 +00002268 std::make_pair(&F, Record[I+1]);
2269 break;
2270 }
Douglas Gregor7c789c12010-10-29 22:39:52 +00002271
2272 case CXX_BASE_SPECIFIER_OFFSETS: {
2273 if (F.LocalNumCXXBaseSpecifiers != 0) {
2274 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2275 return Failure;
2276 }
2277
2278 F.LocalNumCXXBaseSpecifiers = Record[0];
2279 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart;
2280 break;
2281 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002282
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002283 case DIAG_PRAGMA_MAPPINGS:
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002284 if (Record.size() % 2 != 0) {
2285 Error("invalid DIAG_USER_MAPPINGS block in AST file");
2286 return Failure;
2287 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002288 if (PragmaDiagMappings.empty())
2289 PragmaDiagMappings.swap(Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002290 else
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002291 PragmaDiagMappings.insert(PragmaDiagMappings.end(),
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002292 Record.begin(), Record.end());
2293 break;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002294
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002295 case CUDA_SPECIAL_DECL_REFS:
2296 // Later tables overwrite earlier ones.
2297 CUDASpecialDeclRefs.swap(Record);
2298 break;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002299
2300 case HEADER_SEARCH_TABLE:
2301 F.HeaderFileInfoTableData = BlobStart;
2302 F.LocalNumHeaderFileInfos = Record[1];
2303 if (Record[0]) {
2304 F.HeaderFileInfoTable
2305 = HeaderFileInfoLookupTable::Create(
2306 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
2307 (const unsigned char *)F.HeaderFileInfoTableData);
2308 if (PP)
2309 PP->getHeaderSearchInfo().SetExternalSource(this);
2310 }
2311 break;
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002312
2313 case FP_PRAGMA_OPTIONS:
2314 // Later tables overwrite earlier ones.
2315 FPPragmaOptions.swap(Record);
2316 break;
2317
2318 case OPENCL_EXTENSIONS:
2319 // Later tables overwrite earlier ones.
2320 OpenCLExtensions.swap(Record);
2321 break;
Douglas Gregorafaf3082009-04-11 00:14:32 +00002322 }
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002323 First = false;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002324 }
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002325 Error("premature end of bitstream in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002326 return Failure;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002327}
2328
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002329ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
2330 ASTFileType Type) {
2331 switch(ReadASTCore(FileName, Type)) {
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002332 case Failure: return Failure;
2333 case IgnorePCH: return IgnorePCH;
2334 case Success: break;
2335 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002336
2337 // Here comes stuff that we only do once the entire chain is loaded.
2338
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002339 // Allocate space for loaded slocentries, identifiers, decls and types.
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002340 unsigned TotalNumIdentifiers = 0, TotalNumTypes = 0, TotalNumDecls = 0,
Sebastian Redl725cd962010-08-04 20:40:17 +00002341 TotalNumPreallocatedPreprocessingEntities = 0, TotalNumMacroDefs = 0,
2342 TotalNumSelectors = 0;
Sebastian Redl12d6da02010-07-19 22:06:55 +00002343 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002344 TotalNumSLocEntries += Chain[I]->LocalNumSLocEntries;
Sebastian Redl8db9fae2010-09-22 20:19:08 +00002345 NextSLocOffset += Chain[I]->LocalSLocSize;
Sebastian Redl2da08f92010-07-19 22:28:42 +00002346 TotalNumIdentifiers += Chain[I]->LocalNumIdentifiers;
Sebastian Redl12d6da02010-07-19 22:06:55 +00002347 TotalNumTypes += Chain[I]->LocalNumTypes;
2348 TotalNumDecls += Chain[I]->LocalNumDecls;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002349 TotalNumPreallocatedPreprocessingEntities +=
2350 Chain[I]->NumPreallocatedPreprocessingEntities;
2351 TotalNumMacroDefs += Chain[I]->LocalNumMacroDefinitions;
Sebastian Redl725cd962010-08-04 20:40:17 +00002352 TotalNumSelectors += Chain[I]->LocalNumSelectors;
Sebastian Redl12d6da02010-07-19 22:06:55 +00002353 }
Sebastian Redl8db9fae2010-09-22 20:19:08 +00002354 SourceMgr.PreallocateSLocEntries(this, TotalNumSLocEntries, NextSLocOffset);
Sebastian Redl2da08f92010-07-19 22:28:42 +00002355 IdentifiersLoaded.resize(TotalNumIdentifiers);
Sebastian Redl12d6da02010-07-19 22:06:55 +00002356 TypesLoaded.resize(TotalNumTypes);
2357 DeclsLoaded.resize(TotalNumDecls);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002358 MacroDefinitionsLoaded.resize(TotalNumMacroDefs);
2359 if (PP) {
2360 if (TotalNumIdentifiers > 0)
2361 PP->getHeaderSearchInfo().SetExternalLookup(this);
2362 if (TotalNumPreallocatedPreprocessingEntities > 0) {
2363 if (!PP->getPreprocessingRecord())
2364 PP->createPreprocessingRecord();
2365 PP->getPreprocessingRecord()->SetExternalSource(*this,
2366 TotalNumPreallocatedPreprocessingEntities);
2367 }
2368 }
Sebastian Redl725cd962010-08-04 20:40:17 +00002369 SelectorsLoaded.resize(TotalNumSelectors);
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002370 // Preload SLocEntries.
2371 for (unsigned I = 0, N = PreloadSLocEntries.size(); I != N; ++I) {
2372 ASTReadResult Result = ReadSLocEntryRecord(PreloadSLocEntries[I]);
2373 if (Result != Success)
2374 return Result;
2375 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00002376
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002377 // Check the predefines buffers.
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00002378 if (!DisableValidation && CheckPredefinesBuffers())
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002379 return IgnorePCH;
2380
2381 if (PP) {
2382 // Initialization of keywords and pragmas occurs before the
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002383 // AST file is read, so there may be some identifiers that were
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002384 // loaded into the IdentifierTable before we intercepted the
2385 // creation of identifiers. Iterate through the list of known
2386 // identifiers and determine whether we have to establish
2387 // preprocessor definitions or top-level identifier declaration
2388 // chains for those identifiers.
2389 //
2390 // We copy the IdentifierInfo pointers to a small vector first,
2391 // since de-serializing declarations or macro definitions can add
2392 // new entries into the identifier table, invalidating the
2393 // iterators.
2394 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
2395 for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(),
2396 IdEnd = PP->getIdentifierTable().end();
2397 Id != IdEnd; ++Id)
2398 Identifiers.push_back(Id->second);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002399 // We need to search the tables in all files.
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002400 for (unsigned J = 0, M = Chain.size(); J != M; ++J) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002401 ASTIdentifierLookupTable *IdTable
2402 = (ASTIdentifierLookupTable *)Chain[J]->IdentifierLookupTable;
2403 // Not all AST files necessarily have identifier tables, only the useful
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00002404 // ones.
2405 if (!IdTable)
2406 continue;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002407 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
2408 IdentifierInfo *II = Identifiers[I];
2409 // Look in the on-disk hash tables for an entry for this identifier
Sebastian Redlc3632732010-10-05 15:59:54 +00002410 ASTIdentifierLookupTrait Info(*this, *Chain[J], II);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002411 std::pair<const char*,unsigned> Key(II->getNameStart(),II->getLength());
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002412 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002413 if (Pos == IdTable->end())
2414 continue;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002415
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002416 // Dereferencing the iterator has the effect of populating the
2417 // IdentifierInfo node with the various declarations it needs.
2418 (void)*Pos;
2419 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002420 }
2421 }
2422
2423 if (Context)
2424 InitializeContext(*Context);
2425
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002426 if (DeserializationListener)
2427 DeserializationListener->ReaderInitialized(this);
2428
Douglas Gregor414cb642010-11-30 05:23:00 +00002429 // If this AST file is a precompiled preamble, then set the main file ID of
2430 // the source manager to the file source file from which the preamble was
2431 // built. This is the only valid way to use a precompiled preamble.
2432 if (Type == Preamble) {
2433 SourceLocation Loc
2434 = SourceMgr.getLocation(FileMgr.getFile(getOriginalSourceFile()), 1, 1);
2435 if (Loc.isValid()) {
2436 std::pair<FileID, unsigned> Decomposed = SourceMgr.getDecomposedLoc(Loc);
2437 SourceMgr.SetPreambleFileID(Decomposed.first);
2438 }
2439 }
2440
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002441 return Success;
2442}
2443
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002444ASTReader::ASTReadResult ASTReader::ReadASTCore(llvm::StringRef FileName,
2445 ASTFileType Type) {
Sebastian Redla866e652010-10-01 19:59:12 +00002446 PerFileData *Prev = Chain.empty() ? 0 : Chain.back();
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002447 Chain.push_back(new PerFileData(Type));
Sebastian Redl9137a522010-07-16 17:50:48 +00002448 PerFileData &F = *Chain.back();
Sebastian Redla866e652010-10-01 19:59:12 +00002449 if (Prev)
2450 Prev->NextInSource = &F;
2451 else
2452 FirstInSource = &F;
2453 F.Loaders.push_back(Prev);
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002454
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002455 // Set the AST file name.
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002456 F.FileName = FileName;
2457
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002458 if (FileName != "-") {
2459 CurrentDir = llvm::sys::path::parent_path(FileName);
2460 if (CurrentDir.empty()) CurrentDir = ".";
2461 }
2462
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002463 // Open the AST file.
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002464 //
2465 // FIXME: This shouldn't be here, we should just take a raw_ostream.
2466 std::string ErrStr;
Michael J. Spencer3a321e22010-12-09 17:36:38 +00002467 llvm::error_code ec;
2468 if (FileName == "-") {
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00002469 ec = llvm::MemoryBuffer::getSTDIN(F.Buffer);
Michael J. Spencer3a321e22010-12-09 17:36:38 +00002470 if (ec)
2471 ErrStr = ec.message();
2472 } else
Chris Lattner39b49bc2010-11-23 08:35:12 +00002473 F.Buffer.reset(FileMgr.getBufferForFile(FileName, &ErrStr));
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002474 if (!F.Buffer) {
2475 Error(ErrStr.c_str());
2476 return IgnorePCH;
2477 }
2478
2479 // Initialize the stream
2480 F.StreamFile.init((const unsigned char *)F.Buffer->getBufferStart(),
2481 (const unsigned char *)F.Buffer->getBufferEnd());
Sebastian Redl9137a522010-07-16 17:50:48 +00002482 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002483 Stream.init(F.StreamFile);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002484 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002485
2486 // Sniff for the signature.
2487 if (Stream.Read(8) != 'C' ||
2488 Stream.Read(8) != 'P' ||
2489 Stream.Read(8) != 'C' ||
2490 Stream.Read(8) != 'H') {
2491 Diag(diag::err_not_a_pch_file) << FileName;
2492 return Failure;
2493 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002494
Douglas Gregor2cf26342009-04-09 22:27:44 +00002495 while (!Stream.AtEndOfStream()) {
2496 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00002497
Douglas Gregore1d918e2009-04-10 23:10:45 +00002498 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002499 Error("invalid record at top-level of AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002500 return Failure;
2501 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002502
2503 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregor668c1a42009-04-21 22:25:48 +00002504
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002505 // We only know the AST subblock ID.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002506 switch (BlockID) {
2507 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002508 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002509 Error("malformed BlockInfoBlock in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002510 return Failure;
2511 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002512 break;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002513 case AST_BLOCK_ID:
Sebastian Redl571db7f2010-08-18 23:56:56 +00002514 switch (ReadASTBlock(F)) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002515 case Success:
2516 break;
2517
2518 case Failure:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002519 return Failure;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002520
2521 case IgnorePCH:
Douglas Gregor2bec0412009-04-10 21:16:55 +00002522 // FIXME: We could consider reading through to the end of this
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002523 // AST block, skipping subblocks, to see if there are other
2524 // AST blocks elsewhere.
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002525
2526 // Clear out any preallocated source location entries, so that
2527 // the source manager does not try to resolve them later.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002528 SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002529
2530 // Remove the stat cache.
Sebastian Redl9137a522010-07-16 17:50:48 +00002531 if (F.StatCache)
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002532 FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002533
Douglas Gregore1d918e2009-04-10 23:10:45 +00002534 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002535 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002536 break;
2537 default:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002538 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002539 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002540 return Failure;
2541 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002542 break;
2543 }
Mike Stump1eb44332009-09-09 15:08:12 +00002544 }
2545
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002546 return Success;
2547}
2548
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002549void ASTReader::setPreprocessor(Preprocessor &pp) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002550 PP = &pp;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002551
2552 unsigned TotalNum = 0;
2553 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
2554 TotalNum += Chain[I]->NumPreallocatedPreprocessingEntities;
2555 if (TotalNum) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002556 if (!PP->getPreprocessingRecord())
2557 PP->createPreprocessingRecord();
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002558 PP->getPreprocessingRecord()->SetExternalSource(*this, TotalNum);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002559 }
2560}
2561
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002562void ASTReader::InitializeContext(ASTContext &Ctx) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002563 Context = &Ctx;
2564 assert(Context && "Passed null context!");
2565
2566 assert(PP && "Forgot to set Preprocessor ?");
2567 PP->getIdentifierTable().setExternalIdentifierLookup(this);
2568 PP->getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregor88a35862010-01-04 19:18:44 +00002569 PP->setExternalSource(this);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002570 PP->getHeaderSearchInfo().SetExternalSource(this);
2571
Douglas Gregor3747ee72010-10-01 01:18:02 +00002572 // If we have an update block for the TU waiting, we have to add it before
2573 // deserializing the decl.
2574 DeclContextOffsetsMap::iterator DCU = DeclContextOffsets.find(0);
2575 if (DCU != DeclContextOffsets.end()) {
2576 // Insertion could invalidate map, so grab vector.
2577 DeclContextInfos T;
2578 T.swap(DCU->second);
2579 DeclContextOffsets.erase(DCU);
2580 DeclContextOffsets[Ctx.getTranslationUnitDecl()].swap(T);
2581 }
2582
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002583 // Load the translation unit declaration
Argyrios Kyrtzidis8871a442010-07-08 17:13:02 +00002584 GetTranslationUnitDecl();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002585
2586 // Load the special types.
2587 Context->setBuiltinVaListType(
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002588 GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST]));
2589 if (unsigned Id = SpecialTypes[SPECIAL_TYPE_OBJC_ID])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002590 Context->setObjCIdType(GetType(Id));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002591 if (unsigned Sel = SpecialTypes[SPECIAL_TYPE_OBJC_SELECTOR])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002592 Context->setObjCSelType(GetType(Sel));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002593 if (unsigned Proto = SpecialTypes[SPECIAL_TYPE_OBJC_PROTOCOL])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002594 Context->setObjCProtoType(GetType(Proto));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002595 if (unsigned Class = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002596 Context->setObjCClassType(GetType(Class));
Steve Naroff14108da2009-07-10 23:34:53 +00002597
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002598 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002599 Context->setCFConstantStringType(GetType(String));
Mike Stump1eb44332009-09-09 15:08:12 +00002600 if (unsigned FastEnum
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002601 = SpecialTypes[SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002602 Context->setObjCFastEnumerationStateType(GetType(FastEnum));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002603 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002604 QualType FileType = GetType(File);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002605 if (FileType.isNull()) {
2606 Error("FILE type is NULL");
2607 return;
2608 }
John McCall183700f2009-09-21 23:43:11 +00002609 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002610 Context->setFILEDecl(Typedef->getDecl());
2611 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002612 const TagType *Tag = FileType->getAs<TagType>();
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002613 if (!Tag) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002614 Error("Invalid FILE type in AST file");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002615 return;
2616 }
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002617 Context->setFILEDecl(Tag->getDecl());
2618 }
2619 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002620 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_jmp_buf]) {
Mike Stump782fa302009-07-28 02:25:19 +00002621 QualType Jmp_bufType = GetType(Jmp_buf);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002622 if (Jmp_bufType.isNull()) {
2623 Error("jmp_bug type is NULL");
2624 return;
2625 }
John McCall183700f2009-09-21 23:43:11 +00002626 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
Mike Stump782fa302009-07-28 02:25:19 +00002627 Context->setjmp_bufDecl(Typedef->getDecl());
2628 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002629 const TagType *Tag = Jmp_bufType->getAs<TagType>();
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002630 if (!Tag) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002631 Error("Invalid jmp_buf type in AST file");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002632 return;
2633 }
Mike Stump782fa302009-07-28 02:25:19 +00002634 Context->setjmp_bufDecl(Tag->getDecl());
2635 }
2636 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002637 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_sigjmp_buf]) {
Mike Stump782fa302009-07-28 02:25:19 +00002638 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002639 if (Sigjmp_bufType.isNull()) {
2640 Error("sigjmp_buf type is NULL");
2641 return;
2642 }
John McCall183700f2009-09-21 23:43:11 +00002643 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
Mike Stump782fa302009-07-28 02:25:19 +00002644 Context->setsigjmp_bufDecl(Typedef->getDecl());
2645 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002646 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002647 assert(Tag && "Invalid sigjmp_buf type in AST file");
Mike Stump782fa302009-07-28 02:25:19 +00002648 Context->setsigjmp_bufDecl(Tag->getDecl());
2649 }
2650 }
Mike Stump1eb44332009-09-09 15:08:12 +00002651 if (unsigned ObjCIdRedef
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002652 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION])
Douglas Gregord1571ac2009-08-21 00:27:50 +00002653 Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef);
Mike Stump1eb44332009-09-09 15:08:12 +00002654 if (unsigned ObjCClassRedef
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002655 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION])
Douglas Gregord1571ac2009-08-21 00:27:50 +00002656 Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002657 if (unsigned String = SpecialTypes[SPECIAL_TYPE_BLOCK_DESCRIPTOR])
Mike Stumpadaaad32009-10-20 02:12:22 +00002658 Context->setBlockDescriptorType(GetType(String));
Mike Stump083c25e2009-10-22 00:49:09 +00002659 if (unsigned String
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002660 = SpecialTypes[SPECIAL_TYPE_BLOCK_EXTENDED_DESCRIPTOR])
Mike Stump083c25e2009-10-22 00:49:09 +00002661 Context->setBlockDescriptorExtendedType(GetType(String));
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002662 if (unsigned ObjCSelRedef
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002663 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION])
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002664 Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002665 if (unsigned String = SpecialTypes[SPECIAL_TYPE_NS_CONSTANT_STRING])
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002666 Context->setNSConstantStringType(GetType(String));
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +00002667
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002668 if (SpecialTypes[SPECIAL_TYPE_INT128_INSTALLED])
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +00002669 Context->setInt128Installed();
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002670
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002671 ReadPragmaDiagnosticMappings(Context->getDiagnostics());
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002672
2673 // If there were any CUDA special declarations, deserialize them.
2674 if (!CUDASpecialDeclRefs.empty()) {
2675 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
2676 Context->setcudaConfigureCallDecl(
2677 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
2678 }
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002679}
2680
Douglas Gregorb64c1932009-05-12 01:31:05 +00002681/// \brief Retrieve the name of the original source file name
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002682/// directly from the AST file, without actually loading the AST
Douglas Gregorb64c1932009-05-12 01:31:05 +00002683/// file.
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002684std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +00002685 FileManager &FileMgr,
Daniel Dunbar93ebb1b2009-12-03 09:13:06 +00002686 Diagnostic &Diags) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002687 // Open the AST file.
Douglas Gregorb64c1932009-05-12 01:31:05 +00002688 std::string ErrStr;
2689 llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
Chris Lattner39b49bc2010-11-23 08:35:12 +00002690 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
Douglas Gregorb64c1932009-05-12 01:31:05 +00002691 if (!Buffer) {
Daniel Dunbar93ebb1b2009-12-03 09:13:06 +00002692 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002693 return std::string();
2694 }
2695
2696 // Initialize the stream
2697 llvm::BitstreamReader StreamFile;
2698 llvm::BitstreamCursor Stream;
Mike Stump1eb44332009-09-09 15:08:12 +00002699 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregorb64c1932009-05-12 01:31:05 +00002700 (const unsigned char *)Buffer->getBufferEnd());
2701 Stream.init(StreamFile);
2702
2703 // Sniff for the signature.
2704 if (Stream.Read(8) != 'C' ||
2705 Stream.Read(8) != 'P' ||
2706 Stream.Read(8) != 'C' ||
2707 Stream.Read(8) != 'H') {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002708 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002709 return std::string();
2710 }
2711
2712 RecordData Record;
2713 while (!Stream.AtEndOfStream()) {
2714 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00002715
Douglas Gregorb64c1932009-05-12 01:31:05 +00002716 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2717 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump1eb44332009-09-09 15:08:12 +00002718
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002719 // We only know the AST subblock ID.
Douglas Gregorb64c1932009-05-12 01:31:05 +00002720 switch (BlockID) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002721 case AST_BLOCK_ID:
2722 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002723 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002724 return std::string();
2725 }
2726 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002727
Douglas Gregorb64c1932009-05-12 01:31:05 +00002728 default:
2729 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002730 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002731 return std::string();
2732 }
2733 break;
2734 }
2735 continue;
2736 }
2737
2738 if (Code == llvm::bitc::END_BLOCK) {
2739 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002740 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002741 return std::string();
2742 }
2743 continue;
2744 }
2745
2746 if (Code == llvm::bitc::DEFINE_ABBREV) {
2747 Stream.ReadAbbrevRecord();
2748 continue;
2749 }
2750
2751 Record.clear();
2752 const char *BlobStart = 0;
2753 unsigned BlobLen = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002754 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002755 == ORIGINAL_FILE_NAME)
Douglas Gregorb64c1932009-05-12 01:31:05 +00002756 return std::string(BlobStart, BlobLen);
Mike Stump1eb44332009-09-09 15:08:12 +00002757 }
Douglas Gregorb64c1932009-05-12 01:31:05 +00002758
2759 return std::string();
2760}
2761
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002762/// \brief Parse the record that corresponds to a LangOptions data
2763/// structure.
2764///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002765/// This routine parses the language options from the AST file and then gives
2766/// them to the AST listener if one is set.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002767///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002768/// \returns true if the listener deems the file unacceptable, false otherwise.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002769bool ASTReader::ParseLanguageOptions(
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002770 const llvm::SmallVectorImpl<uint64_t> &Record) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002771 if (Listener) {
2772 LangOptions LangOpts;
Mike Stump1eb44332009-09-09 15:08:12 +00002773
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002774 #define PARSE_LANGOPT(Option) \
2775 LangOpts.Option = Record[Idx]; \
2776 ++Idx
Mike Stump1eb44332009-09-09 15:08:12 +00002777
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002778 unsigned Idx = 0;
2779 PARSE_LANGOPT(Trigraphs);
2780 PARSE_LANGOPT(BCPLComment);
2781 PARSE_LANGOPT(DollarIdents);
2782 PARSE_LANGOPT(AsmPreprocessor);
2783 PARSE_LANGOPT(GNUMode);
Chandler Carrutheb5d7b72010-04-17 20:17:31 +00002784 PARSE_LANGOPT(GNUKeywords);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002785 PARSE_LANGOPT(ImplicitInt);
2786 PARSE_LANGOPT(Digraphs);
2787 PARSE_LANGOPT(HexFloats);
2788 PARSE_LANGOPT(C99);
2789 PARSE_LANGOPT(Microsoft);
2790 PARSE_LANGOPT(CPlusPlus);
2791 PARSE_LANGOPT(CPlusPlus0x);
2792 PARSE_LANGOPT(CXXOperatorNames);
2793 PARSE_LANGOPT(ObjC1);
2794 PARSE_LANGOPT(ObjC2);
2795 PARSE_LANGOPT(ObjCNonFragileABI);
Fariborz Jahanian412e7982010-02-09 19:31:38 +00002796 PARSE_LANGOPT(ObjCNonFragileABI2);
Fariborz Jahanianf84109e2011-01-07 18:59:25 +00002797 PARSE_LANGOPT(AppleKext);
Ted Kremenekc32647d2010-12-23 21:35:43 +00002798 PARSE_LANGOPT(ObjCDefaultSynthProperties);
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +00002799 PARSE_LANGOPT(NoConstantCFStrings);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002800 PARSE_LANGOPT(PascalStrings);
2801 PARSE_LANGOPT(WritableStrings);
2802 PARSE_LANGOPT(LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +00002803 PARSE_LANGOPT(AltiVec);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002804 PARSE_LANGOPT(Exceptions);
Anders Carlssonda4b7cf2011-02-19 23:53:54 +00002805 PARSE_LANGOPT(ObjCExceptions);
Anders Carlsson7da99b02011-02-23 03:04:54 +00002806 PARSE_LANGOPT(CXXExceptions);
2807 PARSE_LANGOPT(SjLjExceptions);
Douglas Gregor6f755502011-02-01 15:15:22 +00002808 PARSE_LANGOPT(MSBitfields);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002809 PARSE_LANGOPT(NeXTRuntime);
2810 PARSE_LANGOPT(Freestanding);
2811 PARSE_LANGOPT(NoBuiltin);
2812 PARSE_LANGOPT(ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +00002813 PARSE_LANGOPT(POSIXThreads);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002814 PARSE_LANGOPT(Blocks);
2815 PARSE_LANGOPT(EmitAllDecls);
2816 PARSE_LANGOPT(MathErrno);
Chris Lattnera4d71452010-06-26 21:25:03 +00002817 LangOpts.setSignedOverflowBehavior((LangOptions::SignedOverflowBehaviorTy)
2818 Record[Idx++]);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002819 PARSE_LANGOPT(HeinousExtensions);
2820 PARSE_LANGOPT(Optimize);
2821 PARSE_LANGOPT(OptimizeSize);
2822 PARSE_LANGOPT(Static);
2823 PARSE_LANGOPT(PICLevel);
2824 PARSE_LANGOPT(GNUInline);
2825 PARSE_LANGOPT(NoInline);
2826 PARSE_LANGOPT(AccessControl);
2827 PARSE_LANGOPT(CharIsSigned);
John Thompsona6fda122009-11-05 20:14:16 +00002828 PARSE_LANGOPT(ShortWChar);
Argyrios Kyrtzidisb1bdced2011-01-15 02:56:16 +00002829 PARSE_LANGOPT(ShortEnums);
Chris Lattnera4d71452010-06-26 21:25:03 +00002830 LangOpts.setGCMode((LangOptions::GCMode)Record[Idx++]);
John McCall1fb0caa2010-10-22 21:05:15 +00002831 LangOpts.setVisibilityMode((Visibility)Record[Idx++]);
Daniel Dunbarab8e2812009-09-21 04:16:19 +00002832 LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode)
Chris Lattnera4d71452010-06-26 21:25:03 +00002833 Record[Idx++]);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002834 PARSE_LANGOPT(InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +00002835 PARSE_LANGOPT(OpenCL);
Peter Collingbourne08a53262010-12-01 19:14:57 +00002836 PARSE_LANGOPT(CUDA);
Mike Stump9c276ae2009-12-12 01:27:46 +00002837 PARSE_LANGOPT(CatchUndefined);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002838 PARSE_LANGOPT(DefaultFPContract);
Mike Stump9c276ae2009-12-12 01:27:46 +00002839 // FIXME: Missing ElideConstructors?!
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002840 #undef PARSE_LANGOPT
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002841
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002842 return Listener->ReadLanguageOptions(LangOpts);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002843 }
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002844
2845 return false;
2846}
2847
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002848void ASTReader::ReadPreprocessedEntities() {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002849 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2850 PerFileData &F = *Chain[I];
2851 if (!F.PreprocessorDetailCursor.getBitStreamReader())
2852 continue;
2853
2854 SavedStreamPosition SavedPosition(F.PreprocessorDetailCursor);
2855 F.PreprocessorDetailCursor.JumpToBit(F.PreprocessorDetailStartOffset);
2856 while (LoadPreprocessedEntity(F)) { }
2857 }
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002858}
2859
Douglas Gregor0a480292011-02-11 19:46:30 +00002860PreprocessedEntity *ASTReader::ReadPreprocessedEntityAtOffset(uint64_t Offset) {
Douglas Gregor89d99802010-11-30 06:16:57 +00002861 PerFileData *F = 0;
2862 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2863 if (Offset < Chain[I]->SizeInBits) {
2864 F = Chain[I];
2865 break;
2866 }
2867
2868 Offset -= Chain[I]->SizeInBits;
2869 }
2870
2871 if (!F) {
2872 Error("Malformed preprocessed entity offset");
2873 return 0;
2874 }
2875
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002876 // Keep track of where we are in the stream, then jump back there
2877 // after reading this entity.
2878 SavedStreamPosition SavedPosition(F->PreprocessorDetailCursor);
2879 F->PreprocessorDetailCursor.JumpToBit(Offset);
2880 return LoadPreprocessedEntity(*F);
Douglas Gregor89d99802010-11-30 06:16:57 +00002881}
2882
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002883HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
2884 HeaderFileInfoTrait Trait(FE->getName());
2885 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2886 PerFileData &F = *Chain[I];
2887 HeaderFileInfoLookupTable *Table
2888 = static_cast<HeaderFileInfoLookupTable *>(F.HeaderFileInfoTable);
2889 if (!Table)
2890 continue;
2891
2892 // Look in the on-disk hash table for an entry for this file name.
2893 HeaderFileInfoLookupTable::iterator Pos = Table->find(FE->getName(),
2894 &Trait);
2895 if (Pos == Table->end())
2896 continue;
2897
2898 HeaderFileInfo HFI = *Pos;
2899 if (Listener)
2900 Listener->ReadHeaderFileInfo(HFI, FE->getUID());
2901
2902 return HFI;
2903 }
2904
2905 return HeaderFileInfo();
2906}
2907
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002908void ASTReader::ReadPragmaDiagnosticMappings(Diagnostic &Diag) {
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002909 unsigned Idx = 0;
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002910 while (Idx < PragmaDiagMappings.size()) {
2911 SourceLocation
2912 Loc = SourceLocation::getFromRawEncoding(PragmaDiagMappings[Idx++]);
2913 while (1) {
2914 assert(Idx < PragmaDiagMappings.size() &&
2915 "Invalid data, didn't find '-1' marking end of diag/map pairs");
2916 if (Idx >= PragmaDiagMappings.size())
2917 break; // Something is messed up but at least avoid infinite loop in
2918 // release build.
2919 unsigned DiagID = PragmaDiagMappings[Idx++];
2920 if (DiagID == (unsigned)-1)
2921 break; // no more diag/map pairs for this location.
2922 diag::Mapping Map = (diag::Mapping)PragmaDiagMappings[Idx++];
2923 Diag.setDiagnosticMapping(DiagID, Map, Loc);
2924 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002925 }
2926}
2927
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002928/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002929ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002930 PerFileData *F = 0;
2931 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2932 F = Chain[N - I - 1];
2933 if (Index < F->LocalNumTypes)
2934 break;
2935 Index -= F->LocalNumTypes;
2936 }
2937 assert(F && F->LocalNumTypes > Index && "Broken chain");
Sebastian Redlc3632732010-10-05 15:59:54 +00002938 return RecordLocation(F, F->TypeOffsets[Index]);
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002939}
2940
2941/// \brief Read and return the type with the given index..
Douglas Gregor2cf26342009-04-09 22:27:44 +00002942///
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002943/// The index is the type ID, shifted and minus the number of predefs. This
2944/// routine actually reads the record corresponding to the type at the given
2945/// location. It is a helper routine for GetType, which deals with reading type
2946/// IDs.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002947QualType ASTReader::ReadTypeRecord(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002948 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00002949 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00002950
Douglas Gregor0b748912009-04-14 21:18:50 +00002951 // Keep track of where we are in the stream, then jump back there
2952 // after reading this type.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002953 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregor0b748912009-04-14 21:18:50 +00002954
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00002955 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redl27372b42010-08-11 18:52:41 +00002956
Douglas Gregord89275b2009-07-06 18:54:52 +00002957 // Note that we are loading a type record.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00002958 Deserializing AType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00002959
Sebastian Redlc3632732010-10-05 15:59:54 +00002960 DeclsCursor.JumpToBit(Loc.Offset);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002961 RecordData Record;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002962 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002963 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
2964 case TYPE_EXT_QUAL: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002965 if (Record.size() != 2) {
2966 Error("Incorrect encoding of extended qualifier type");
2967 return QualType();
2968 }
Douglas Gregor6d473962009-04-15 22:00:08 +00002969 QualType Base = GetType(Record[0]);
John McCall0953e762009-09-24 19:53:00 +00002970 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[1]);
2971 return Context->getQualifiedType(Base, Quals);
Douglas Gregor6d473962009-04-15 22:00:08 +00002972 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002973
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002974 case TYPE_COMPLEX: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002975 if (Record.size() != 1) {
2976 Error("Incorrect encoding of complex type");
2977 return QualType();
2978 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002979 QualType ElemType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002980 return Context->getComplexType(ElemType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002981 }
2982
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002983 case TYPE_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002984 if (Record.size() != 1) {
2985 Error("Incorrect encoding of pointer type");
2986 return QualType();
2987 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002988 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002989 return Context->getPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002990 }
2991
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002992 case TYPE_BLOCK_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002993 if (Record.size() != 1) {
2994 Error("Incorrect encoding of block pointer type");
2995 return QualType();
2996 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002997 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002998 return Context->getBlockPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002999 }
3000
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003001 case TYPE_LVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003002 if (Record.size() != 1) {
3003 Error("Incorrect encoding of lvalue reference type");
3004 return QualType();
3005 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003006 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003007 return Context->getLValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003008 }
3009
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003010 case TYPE_RVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003011 if (Record.size() != 1) {
3012 Error("Incorrect encoding of rvalue reference type");
3013 return QualType();
3014 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003015 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003016 return Context->getRValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003017 }
3018
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003019 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidis240437b2010-07-02 11:55:15 +00003020 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003021 Error("Incorrect encoding of member pointer type");
3022 return QualType();
3023 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003024 QualType PointeeType = GetType(Record[0]);
3025 QualType ClassType = GetType(Record[1]);
Douglas Gregor1ab55e92010-12-10 17:03:06 +00003026 if (PointeeType.isNull() || ClassType.isNull())
3027 return QualType();
3028
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003029 return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregor2cf26342009-04-09 22:27:44 +00003030 }
3031
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003032 case TYPE_CONSTANT_ARRAY: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003033 QualType ElementType = GetType(Record[0]);
3034 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3035 unsigned IndexTypeQuals = Record[2];
3036 unsigned Idx = 3;
3037 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003038 return Context->getConstantArrayType(ElementType, Size,
3039 ASM, IndexTypeQuals);
3040 }
3041
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003042 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003043 QualType ElementType = GetType(Record[0]);
3044 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3045 unsigned IndexTypeQuals = Record[2];
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003046 return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003047 }
3048
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003049 case TYPE_VARIABLE_ARRAY: {
Douglas Gregor0b748912009-04-14 21:18:50 +00003050 QualType ElementType = GetType(Record[0]);
3051 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3052 unsigned IndexTypeQuals = Record[2];
Sebastian Redlc3632732010-10-05 15:59:54 +00003053 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
3054 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
3055 return Context->getVariableArrayType(ElementType, ReadExpr(*Loc.F),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003056 ASM, IndexTypeQuals,
3057 SourceRange(LBLoc, RBLoc));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003058 }
3059
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003060 case TYPE_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00003061 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003062 Error("incorrect encoding of vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003063 return QualType();
3064 }
3065
3066 QualType ElementType = GetType(Record[0]);
3067 unsigned NumElements = Record[1];
Bob Wilsone86d78c2010-11-10 21:56:12 +00003068 unsigned VecKind = Record[2];
Chris Lattner788b0fd2010-06-23 06:00:24 +00003069 return Context->getVectorType(ElementType, NumElements,
Bob Wilsone86d78c2010-11-10 21:56:12 +00003070 (VectorType::VectorKind)VecKind);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003071 }
3072
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003073 case TYPE_EXT_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00003074 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003075 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003076 return QualType();
3077 }
3078
3079 QualType ElementType = GetType(Record[0]);
3080 unsigned NumElements = Record[1];
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003081 return Context->getExtVectorType(ElementType, NumElements);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003082 }
3083
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003084 case TYPE_FUNCTION_NO_PROTO: {
Rafael Espindola425ef722010-03-30 22:15:11 +00003085 if (Record.size() != 4) {
Douglas Gregora02b1472009-04-28 21:53:25 +00003086 Error("incorrect encoding of no-proto function type");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003087 return QualType();
3088 }
3089 QualType ResultType = GetType(Record[0]);
Rafael Espindola425ef722010-03-30 22:15:11 +00003090 FunctionType::ExtInfo Info(Record[1], Record[2], (CallingConv)Record[3]);
Rafael Espindola264ba482010-03-30 20:24:48 +00003091 return Context->getFunctionNoProtoType(ResultType, Info);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003092 }
3093
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003094 case TYPE_FUNCTION_PROTO: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003095 QualType ResultType = GetType(Record[0]);
John McCalle23cf432010-12-14 08:05:40 +00003096
3097 FunctionProtoType::ExtProtoInfo EPI;
3098 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
3099 /*regparm*/ Record[2],
3100 static_cast<CallingConv>(Record[3]));
3101
Rafael Espindola425ef722010-03-30 22:15:11 +00003102 unsigned Idx = 4;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003103 unsigned NumParams = Record[Idx++];
3104 llvm::SmallVector<QualType, 16> ParamTypes;
3105 for (unsigned I = 0; I != NumParams; ++I)
3106 ParamTypes.push_back(GetType(Record[Idx++]));
John McCalle23cf432010-12-14 08:05:40 +00003107
3108 EPI.Variadic = Record[Idx++];
3109 EPI.TypeQuals = Record[Idx++];
Douglas Gregorc938c162011-01-26 05:01:58 +00003110 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
John McCalle23cf432010-12-14 08:05:40 +00003111 EPI.HasExceptionSpec = Record[Idx++];
3112 EPI.HasAnyExceptionSpec = Record[Idx++];
3113 EPI.NumExceptions = Record[Idx++];
Sebastian Redl465226e2009-05-27 22:11:52 +00003114 llvm::SmallVector<QualType, 2> Exceptions;
John McCalle23cf432010-12-14 08:05:40 +00003115 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
Sebastian Redl465226e2009-05-27 22:11:52 +00003116 Exceptions.push_back(GetType(Record[Idx++]));
John McCalle23cf432010-12-14 08:05:40 +00003117 EPI.Exceptions = Exceptions.data();
Jay Foadbeaaccd2009-05-21 09:52:38 +00003118 return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams,
John McCalle23cf432010-12-14 08:05:40 +00003119 EPI);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003120 }
3121
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003122 case TYPE_UNRESOLVED_USING:
John McCalled976492009-12-04 22:46:56 +00003123 return Context->getTypeDeclType(
3124 cast<UnresolvedUsingTypenameDecl>(GetDecl(Record[0])));
3125
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003126 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003127 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003128 Error("incorrect encoding of typedef type");
3129 return QualType();
3130 }
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003131 TypedefDecl *Decl = cast<TypedefDecl>(GetDecl(Record[0]));
3132 QualType Canonical = GetType(Record[1]);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00003133 if (!Canonical.isNull())
3134 Canonical = Context->getCanonicalType(Canonical);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003135 return Context->getTypedefType(Decl, Canonical);
3136 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003137
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003138 case TYPE_TYPEOF_EXPR:
Sebastian Redlc3632732010-10-05 15:59:54 +00003139 return Context->getTypeOfExprType(ReadExpr(*Loc.F));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003140
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003141 case TYPE_TYPEOF: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003142 if (Record.size() != 1) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003143 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003144 return QualType();
3145 }
3146 QualType UnderlyingType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003147 return Context->getTypeOfType(UnderlyingType);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003148 }
Mike Stump1eb44332009-09-09 15:08:12 +00003149
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003150 case TYPE_DECLTYPE:
Sebastian Redlc3632732010-10-05 15:59:54 +00003151 return Context->getDecltypeType(ReadExpr(*Loc.F));
Anders Carlsson395b4752009-06-24 19:06:50 +00003152
Richard Smith34b41d92011-02-20 03:19:35 +00003153 case TYPE_AUTO:
3154 return Context->getAutoType(GetType(Record[0]));
3155
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003156 case TYPE_RECORD: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003157 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003158 Error("incorrect encoding of record type");
3159 return QualType();
3160 }
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003161 bool IsDependent = Record[0];
3162 QualType T = Context->getRecordType(cast<RecordDecl>(GetDecl(Record[1])));
John McCallf4c73712011-01-19 06:33:43 +00003163 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003164 return T;
3165 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003166
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003167 case TYPE_ENUM: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003168 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003169 Error("incorrect encoding of enum type");
3170 return QualType();
3171 }
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003172 bool IsDependent = Record[0];
3173 QualType T = Context->getEnumType(cast<EnumDecl>(GetDecl(Record[1])));
John McCallf4c73712011-01-19 06:33:43 +00003174 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003175 return T;
3176 }
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003177
John McCall9d156a72011-01-06 01:58:22 +00003178 case TYPE_ATTRIBUTED: {
3179 if (Record.size() != 3) {
3180 Error("incorrect encoding of attributed type");
3181 return QualType();
3182 }
3183 QualType modifiedType = GetType(Record[0]);
3184 QualType equivalentType = GetType(Record[1]);
3185 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
3186 return Context->getAttributedType(kind, modifiedType, equivalentType);
3187 }
3188
Abramo Bagnara075f8f12010-12-10 16:29:40 +00003189 case TYPE_PAREN: {
3190 if (Record.size() != 1) {
3191 Error("incorrect encoding of paren type");
3192 return QualType();
3193 }
3194 QualType InnerType = GetType(Record[0]);
3195 return Context->getParenType(InnerType);
3196 }
3197
Douglas Gregor7536dd52010-12-20 02:24:11 +00003198 case TYPE_PACK_EXPANSION: {
Douglas Gregorf9997a02011-02-01 15:24:58 +00003199 if (Record.size() != 2) {
Douglas Gregor7536dd52010-12-20 02:24:11 +00003200 Error("incorrect encoding of pack expansion type");
3201 return QualType();
3202 }
3203 QualType Pattern = GetType(Record[0]);
3204 if (Pattern.isNull())
3205 return QualType();
Douglas Gregorcded4f62011-01-14 17:04:44 +00003206 llvm::Optional<unsigned> NumExpansions;
3207 if (Record[1])
3208 NumExpansions = Record[1] - 1;
3209 return Context->getPackExpansionType(Pattern, NumExpansions);
Douglas Gregor7536dd52010-12-20 02:24:11 +00003210 }
3211
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003212 case TYPE_ELABORATED: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00003213 unsigned Idx = 0;
3214 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3215 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3216 QualType NamedType = GetType(Record[Idx++]);
3217 return Context->getElaboratedType(Keyword, NNS, NamedType);
John McCall7da24312009-09-05 00:15:47 +00003218 }
3219
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003220 case TYPE_OBJC_INTERFACE: {
Chris Lattnerc6fa4452009-04-22 06:45:28 +00003221 unsigned Idx = 0;
3222 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
John McCallc12c5bb2010-05-15 11:32:37 +00003223 return Context->getObjCInterfaceType(ItfD);
3224 }
3225
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003226 case TYPE_OBJC_OBJECT: {
John McCallc12c5bb2010-05-15 11:32:37 +00003227 unsigned Idx = 0;
3228 QualType Base = GetType(Record[Idx++]);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00003229 unsigned NumProtos = Record[Idx++];
3230 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
3231 for (unsigned I = 0; I != NumProtos; ++I)
3232 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
Michael J. Spencer20249a12010-10-21 03:16:25 +00003233 return Context->getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00003234 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003235
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003236 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00003237 unsigned Idx = 0;
John McCallc12c5bb2010-05-15 11:32:37 +00003238 QualType Pointee = GetType(Record[Idx++]);
3239 return Context->getObjCObjectPointerType(Pointee);
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00003240 }
Argyrios Kyrtzidis24fab412009-09-29 19:42:55 +00003241
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003242 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCall49a832b2009-10-18 09:09:24 +00003243 unsigned Idx = 0;
3244 QualType Parm = GetType(Record[Idx++]);
3245 QualType Replacement = GetType(Record[Idx++]);
3246 return
3247 Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
3248 Replacement);
3249 }
John McCall3cb0ebd2010-03-10 03:28:59 +00003250
Douglas Gregorc3069d62011-01-14 02:55:32 +00003251 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
3252 unsigned Idx = 0;
3253 QualType Parm = GetType(Record[Idx++]);
3254 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
3255 return Context->getSubstTemplateTypeParmPackType(
3256 cast<TemplateTypeParmType>(Parm),
3257 ArgPack);
3258 }
3259
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003260 case TYPE_INJECTED_CLASS_NAME: {
John McCall3cb0ebd2010-03-10 03:28:59 +00003261 CXXRecordDecl *D = cast<CXXRecordDecl>(GetDecl(Record[0]));
3262 QualType TST = GetType(Record[1]); // probably derivable
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00003263 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003264 // for AST reading, too much interdependencies.
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00003265 return
3266 QualType(new (*Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCall3cb0ebd2010-03-10 03:28:59 +00003267 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003268
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003269 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003270 unsigned Idx = 0;
3271 unsigned Depth = Record[Idx++];
3272 unsigned Index = Record[Idx++];
3273 bool Pack = Record[Idx++];
3274 IdentifierInfo *Name = GetIdentifierInfo(Record, Idx);
3275 return Context->getTemplateTypeParmType(Depth, Index, Pack, Name);
3276 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003277
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003278 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00003279 unsigned Idx = 0;
3280 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3281 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3282 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +00003283 QualType Canon = GetType(Record[Idx++]);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00003284 if (!Canon.isNull())
3285 Canon = Context->getCanonicalType(Canon);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +00003286 return Context->getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00003287 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003288
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003289 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00003290 unsigned Idx = 0;
3291 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3292 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3293 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
3294 unsigned NumArgs = Record[Idx++];
3295 llvm::SmallVector<TemplateArgument, 8> Args;
3296 Args.reserve(NumArgs);
3297 while (NumArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00003298 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00003299 return Context->getDependentTemplateSpecializationType(Keyword, NNS, Name,
3300 Args.size(), Args.data());
3301 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003302
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003303 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00003304 unsigned Idx = 0;
3305
3306 // ArrayType
3307 QualType ElementType = GetType(Record[Idx++]);
3308 ArrayType::ArraySizeModifier ASM
3309 = (ArrayType::ArraySizeModifier)Record[Idx++];
3310 unsigned IndexTypeQuals = Record[Idx++];
3311
3312 // DependentSizedArrayType
Sebastian Redlc3632732010-10-05 15:59:54 +00003313 Expr *NumElts = ReadExpr(*Loc.F);
3314 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00003315
3316 return Context->getDependentSizedArrayType(ElementType, NumElts, ASM,
3317 IndexTypeQuals, Brackets);
3318 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003319
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003320 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003321 unsigned Idx = 0;
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003322 bool IsDependent = Record[Idx++];
Douglas Gregor1aee05d2011-01-15 06:45:20 +00003323 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003324 llvm::SmallVector<TemplateArgument, 8> Args;
Sebastian Redlc3632732010-10-05 15:59:54 +00003325 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003326 QualType Canon = GetType(Record[Idx++]);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003327 QualType T;
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003328 if (Canon.isNull())
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003329 T = Context->getCanonicalTemplateSpecializationType(Name, Args.data(),
3330 Args.size());
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003331 else
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003332 T = Context->getTemplateSpecializationType(Name, Args.data(),
3333 Args.size(), Canon);
John McCallf4c73712011-01-19 06:33:43 +00003334 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003335 return T;
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003336 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003337 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003338 // Suppress a GCC warning
3339 return QualType();
3340}
3341
Sebastian Redlc3632732010-10-05 15:59:54 +00003342class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003343 ASTReader &Reader;
Sebastian Redlc3632732010-10-05 15:59:54 +00003344 ASTReader::PerFileData &F;
Sebastian Redl577d4792010-07-22 22:43:28 +00003345 llvm::BitstreamCursor &DeclsCursor;
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003346 const ASTReader::RecordData &Record;
John McCalla1ee0c52009-10-16 21:56:05 +00003347 unsigned &Idx;
3348
Sebastian Redlc3632732010-10-05 15:59:54 +00003349 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
3350 unsigned &I) {
3351 return Reader.ReadSourceLocation(F, R, I);
3352 }
3353
John McCalla1ee0c52009-10-16 21:56:05 +00003354public:
Sebastian Redlc3632732010-10-05 15:59:54 +00003355 TypeLocReader(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003356 const ASTReader::RecordData &Record, unsigned &Idx)
Sebastian Redlc3632732010-10-05 15:59:54 +00003357 : Reader(Reader), F(F), DeclsCursor(F.DeclsCursor), Record(Record), Idx(Idx)
3358 { }
John McCalla1ee0c52009-10-16 21:56:05 +00003359
John McCall51bd8032009-10-18 01:05:36 +00003360 // We want compile-time assurance that we've enumerated all of
3361 // these, so unfortunately we have to declare them first, then
3362 // define them out-of-line.
3363#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +00003364#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +00003365 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00003366#include "clang/AST/TypeLocNodes.def"
3367
John McCall51bd8032009-10-18 01:05:36 +00003368 void VisitFunctionTypeLoc(FunctionTypeLoc);
3369 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00003370};
3371
John McCall51bd8032009-10-18 01:05:36 +00003372void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCalla1ee0c52009-10-16 21:56:05 +00003373 // nothing to do
3374}
John McCall51bd8032009-10-18 01:05:36 +00003375void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003376 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
Douglas Gregorddf889a2010-01-18 18:04:31 +00003377 if (TL.needsExtraLocalData()) {
3378 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
3379 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
3380 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
3381 TL.setModeAttr(Record[Idx++]);
3382 }
John McCalla1ee0c52009-10-16 21:56:05 +00003383}
John McCall51bd8032009-10-18 01:05:36 +00003384void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003385 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003386}
John McCall51bd8032009-10-18 01:05:36 +00003387void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003388 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003389}
John McCall51bd8032009-10-18 01:05:36 +00003390void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003391 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003392}
John McCall51bd8032009-10-18 01:05:36 +00003393void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003394 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003395}
John McCall51bd8032009-10-18 01:05:36 +00003396void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003397 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003398}
John McCall51bd8032009-10-18 01:05:36 +00003399void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003400 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003401}
John McCall51bd8032009-10-18 01:05:36 +00003402void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003403 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
3404 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003405 if (Record[Idx++])
Sebastian Redlc3632732010-10-05 15:59:54 +00003406 TL.setSizeExpr(Reader.ReadExpr(F));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003407 else
John McCall51bd8032009-10-18 01:05:36 +00003408 TL.setSizeExpr(0);
3409}
3410void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
3411 VisitArrayTypeLoc(TL);
3412}
3413void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
3414 VisitArrayTypeLoc(TL);
3415}
3416void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
3417 VisitArrayTypeLoc(TL);
3418}
3419void TypeLocReader::VisitDependentSizedArrayTypeLoc(
3420 DependentSizedArrayTypeLoc TL) {
3421 VisitArrayTypeLoc(TL);
3422}
3423void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
3424 DependentSizedExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003425 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003426}
3427void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003428 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003429}
3430void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003431 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003432}
3433void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003434 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3435 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
Douglas Gregordab60ad2010-10-01 18:44:50 +00003436 TL.setTrailingReturn(Record[Idx++]);
John McCall51bd8032009-10-18 01:05:36 +00003437 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
John McCall86acc2a2009-10-23 01:28:53 +00003438 TL.setArg(i, cast_or_null<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
John McCall51bd8032009-10-18 01:05:36 +00003439 }
3440}
3441void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
3442 VisitFunctionTypeLoc(TL);
3443}
3444void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
3445 VisitFunctionTypeLoc(TL);
3446}
John McCalled976492009-12-04 22:46:56 +00003447void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003448 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalled976492009-12-04 22:46:56 +00003449}
John McCall51bd8032009-10-18 01:05:36 +00003450void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003451 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003452}
3453void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003454 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3455 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3456 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003457}
3458void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003459 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3460 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3461 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3462 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003463}
3464void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003465 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003466}
Richard Smith34b41d92011-02-20 03:19:35 +00003467void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
3468 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3469}
John McCall51bd8032009-10-18 01:05:36 +00003470void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003471 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003472}
3473void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003474 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003475}
John McCall9d156a72011-01-06 01:58:22 +00003476void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
3477 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
3478 if (TL.hasAttrOperand()) {
3479 SourceRange range;
3480 range.setBegin(ReadSourceLocation(Record, Idx));
3481 range.setEnd(ReadSourceLocation(Record, Idx));
3482 TL.setAttrOperandParensRange(range);
3483 }
3484 if (TL.hasAttrExprOperand()) {
3485 if (Record[Idx++])
3486 TL.setAttrExprOperand(Reader.ReadExpr(F));
3487 else
3488 TL.setAttrExprOperand(0);
3489 } else if (TL.hasAttrEnumOperand())
3490 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
3491}
John McCall51bd8032009-10-18 01:05:36 +00003492void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003493 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003494}
John McCall49a832b2009-10-18 09:09:24 +00003495void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
3496 SubstTemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003497 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall49a832b2009-10-18 09:09:24 +00003498}
Douglas Gregorc3069d62011-01-14 02:55:32 +00003499void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
3500 SubstTemplateTypeParmPackTypeLoc TL) {
3501 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3502}
John McCall51bd8032009-10-18 01:05:36 +00003503void TypeLocReader::VisitTemplateSpecializationTypeLoc(
3504 TemplateSpecializationTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003505 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
3506 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3507 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall833ca992009-10-29 08:12:44 +00003508 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
3509 TL.setArgLocInfo(i,
Sebastian Redlc3632732010-10-05 15:59:54 +00003510 Reader.GetTemplateArgumentLocInfo(F,
3511 TL.getTypePtr()->getArg(i).getKind(),
3512 Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003513}
Abramo Bagnara075f8f12010-12-10 16:29:40 +00003514void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
3515 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3516 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3517}
Abramo Bagnara465d41b2010-05-11 21:36:43 +00003518void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003519 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3520 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003521}
John McCall3cb0ebd2010-03-10 03:28:59 +00003522void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003523 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall3cb0ebd2010-03-10 03:28:59 +00003524}
Douglas Gregor4714c122010-03-31 17:34:00 +00003525void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003526 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3527 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
3528 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003529}
John McCall33500952010-06-11 00:33:02 +00003530void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
3531 DependentTemplateSpecializationTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003532 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3533 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
3534 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3535 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3536 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall33500952010-06-11 00:33:02 +00003537 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
3538 TL.setArgLocInfo(I,
Sebastian Redlc3632732010-10-05 15:59:54 +00003539 Reader.GetTemplateArgumentLocInfo(F,
3540 TL.getTypePtr()->getArg(I).getKind(),
3541 Record, Idx));
John McCall33500952010-06-11 00:33:02 +00003542}
Douglas Gregor7536dd52010-12-20 02:24:11 +00003543void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
3544 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
3545}
John McCall51bd8032009-10-18 01:05:36 +00003546void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003547 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallc12c5bb2010-05-15 11:32:37 +00003548}
3549void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
3550 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00003551 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3552 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003553 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Sebastian Redlc3632732010-10-05 15:59:54 +00003554 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003555}
John McCall54e14c42009-10-22 22:37:11 +00003556void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003557 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall54e14c42009-10-22 22:37:11 +00003558}
John McCalla1ee0c52009-10-16 21:56:05 +00003559
Sebastian Redlc3632732010-10-05 15:59:54 +00003560TypeSourceInfo *ASTReader::GetTypeSourceInfo(PerFileData &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00003561 const RecordData &Record,
John McCalla1ee0c52009-10-16 21:56:05 +00003562 unsigned &Idx) {
3563 QualType InfoTy = GetType(Record[Idx++]);
3564 if (InfoTy.isNull())
3565 return 0;
3566
John McCalla93c9342009-12-07 02:54:59 +00003567 TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy);
Sebastian Redlc3632732010-10-05 15:59:54 +00003568 TypeLocReader TLR(*this, F, Record, Idx);
John McCalla93c9342009-12-07 02:54:59 +00003569 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCalla1ee0c52009-10-16 21:56:05 +00003570 TLR.Visit(TL);
John McCalla93c9342009-12-07 02:54:59 +00003571 return TInfo;
John McCalla1ee0c52009-10-16 21:56:05 +00003572}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003573
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003574QualType ASTReader::GetType(TypeID ID) {
John McCall0953e762009-09-24 19:53:00 +00003575 unsigned FastQuals = ID & Qualifiers::FastMask;
3576 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003577
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003578 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003579 QualType T;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003580 switch ((PredefinedTypeIDs)Index) {
3581 case PREDEF_TYPE_NULL_ID: return QualType();
3582 case PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break;
3583 case PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003584
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003585 case PREDEF_TYPE_CHAR_U_ID:
3586 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregor2cf26342009-04-09 22:27:44 +00003587 // FIXME: Check that the signedness of CharTy is correct!
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003588 T = Context->CharTy;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003589 break;
3590
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003591 case PREDEF_TYPE_UCHAR_ID: T = Context->UnsignedCharTy; break;
3592 case PREDEF_TYPE_USHORT_ID: T = Context->UnsignedShortTy; break;
3593 case PREDEF_TYPE_UINT_ID: T = Context->UnsignedIntTy; break;
3594 case PREDEF_TYPE_ULONG_ID: T = Context->UnsignedLongTy; break;
3595 case PREDEF_TYPE_ULONGLONG_ID: T = Context->UnsignedLongLongTy; break;
3596 case PREDEF_TYPE_UINT128_ID: T = Context->UnsignedInt128Ty; break;
3597 case PREDEF_TYPE_SCHAR_ID: T = Context->SignedCharTy; break;
3598 case PREDEF_TYPE_WCHAR_ID: T = Context->WCharTy; break;
3599 case PREDEF_TYPE_SHORT_ID: T = Context->ShortTy; break;
3600 case PREDEF_TYPE_INT_ID: T = Context->IntTy; break;
3601 case PREDEF_TYPE_LONG_ID: T = Context->LongTy; break;
3602 case PREDEF_TYPE_LONGLONG_ID: T = Context->LongLongTy; break;
3603 case PREDEF_TYPE_INT128_ID: T = Context->Int128Ty; break;
3604 case PREDEF_TYPE_FLOAT_ID: T = Context->FloatTy; break;
3605 case PREDEF_TYPE_DOUBLE_ID: T = Context->DoubleTy; break;
3606 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy; break;
3607 case PREDEF_TYPE_OVERLOAD_ID: T = Context->OverloadTy; break;
3608 case PREDEF_TYPE_DEPENDENT_ID: T = Context->DependentTy; break;
3609 case PREDEF_TYPE_NULLPTR_ID: T = Context->NullPtrTy; break;
3610 case PREDEF_TYPE_CHAR16_ID: T = Context->Char16Ty; break;
3611 case PREDEF_TYPE_CHAR32_ID: T = Context->Char32Ty; break;
3612 case PREDEF_TYPE_OBJC_ID: T = Context->ObjCBuiltinIdTy; break;
3613 case PREDEF_TYPE_OBJC_CLASS: T = Context->ObjCBuiltinClassTy; break;
3614 case PREDEF_TYPE_OBJC_SEL: T = Context->ObjCBuiltinSelTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003615 }
3616
3617 assert(!T.isNull() && "Unknown predefined type");
John McCall0953e762009-09-24 19:53:00 +00003618 return T.withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003619 }
3620
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003621 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003622 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl07a353c2010-07-14 20:26:45 +00003623 if (TypesLoaded[Index].isNull()) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003624 TypesLoaded[Index] = ReadTypeRecord(Index);
Douglas Gregor97475832010-10-05 18:37:06 +00003625 if (TypesLoaded[Index].isNull())
3626 return QualType();
3627
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003628 TypesLoaded[Index]->setFromAST();
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003629 TypeIdxs[TypesLoaded[Index]] = TypeIdx::fromTypeID(ID);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003630 if (DeserializationListener)
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003631 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1476ed42010-07-16 16:36:56 +00003632 TypesLoaded[Index]);
Sebastian Redl07a353c2010-07-14 20:26:45 +00003633 }
Mike Stump1eb44332009-09-09 15:08:12 +00003634
John McCall0953e762009-09-24 19:53:00 +00003635 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003636}
3637
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003638TypeID ASTReader::GetTypeID(QualType T) const {
3639 return MakeTypeID(T,
3640 std::bind1st(std::mem_fun(&ASTReader::GetTypeIdx), this));
3641}
3642
3643TypeIdx ASTReader::GetTypeIdx(QualType T) const {
3644 if (T.isNull())
3645 return TypeIdx();
3646 assert(!T.getLocalFastQualifiers());
3647
3648 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3649 // GetTypeIdx is mostly used for computing the hash of DeclarationNames and
3650 // comparing keys of ASTDeclContextNameLookupTable.
3651 // If the type didn't come from the AST file use a specially marked index
3652 // so that any hash/key comparison fail since no such index is stored
3653 // in a AST file.
3654 if (I == TypeIdxs.end())
3655 return TypeIdx(-1);
3656 return I->second;
3657}
3658
Douglas Gregor7c789c12010-10-29 22:39:52 +00003659unsigned ASTReader::getTotalNumCXXBaseSpecifiers() const {
3660 unsigned Result = 0;
3661 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
3662 Result += Chain[I]->LocalNumCXXBaseSpecifiers;
3663
3664 return Result;
3665}
3666
John McCall833ca992009-10-29 08:12:44 +00003667TemplateArgumentLocInfo
Sebastian Redlc3632732010-10-05 15:59:54 +00003668ASTReader::GetTemplateArgumentLocInfo(PerFileData &F,
3669 TemplateArgument::ArgKind Kind,
John McCall833ca992009-10-29 08:12:44 +00003670 const RecordData &Record,
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00003671 unsigned &Index) {
John McCall833ca992009-10-29 08:12:44 +00003672 switch (Kind) {
3673 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00003674 return ReadExpr(F);
John McCall833ca992009-10-29 08:12:44 +00003675 case TemplateArgument::Type:
Sebastian Redlc3632732010-10-05 15:59:54 +00003676 return GetTypeSourceInfo(F, Record, Index);
Douglas Gregor788cd062009-11-11 01:00:40 +00003677 case TemplateArgument::Template: {
Sebastian Redlc3632732010-10-05 15:59:54 +00003678 SourceRange QualifierRange = ReadSourceRange(F, Record, Index);
3679 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003680 return TemplateArgumentLocInfo(QualifierRange, TemplateNameLoc,
3681 SourceLocation());
3682 }
3683 case TemplateArgument::TemplateExpansion: {
3684 SourceRange QualifierRange = ReadSourceRange(F, Record, Index);
3685 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorba68eca2011-01-05 17:40:24 +00003686 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
3687 return TemplateArgumentLocInfo(QualifierRange, TemplateNameLoc,
3688 EllipsisLoc);
Douglas Gregor788cd062009-11-11 01:00:40 +00003689 }
John McCall833ca992009-10-29 08:12:44 +00003690 case TemplateArgument::Null:
3691 case TemplateArgument::Integral:
3692 case TemplateArgument::Declaration:
3693 case TemplateArgument::Pack:
3694 return TemplateArgumentLocInfo();
3695 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00003696 llvm_unreachable("unexpected template argument loc");
John McCall833ca992009-10-29 08:12:44 +00003697 return TemplateArgumentLocInfo();
3698}
3699
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003700TemplateArgumentLoc
Sebastian Redlc3632732010-10-05 15:59:54 +00003701ASTReader::ReadTemplateArgumentLoc(PerFileData &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00003702 const RecordData &Record, unsigned &Index) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003703 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003704
3705 if (Arg.getKind() == TemplateArgument::Expression) {
3706 if (Record[Index++]) // bool InfoHasSameExpr.
3707 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
3708 }
Sebastian Redlc3632732010-10-05 15:59:54 +00003709 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00003710 Record, Index));
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003711}
3712
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003713Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall76bd1f32010-06-01 09:23:16 +00003714 return GetDecl(ID);
3715}
3716
Douglas Gregor7c789c12010-10-29 22:39:52 +00003717uint64_t
3718ASTReader::GetCXXBaseSpecifiersOffset(serialization::CXXBaseSpecifiersID ID) {
3719 if (ID == 0)
3720 return 0;
3721
3722 --ID;
3723 uint64_t Offset = 0;
3724 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3725 if (ID < Chain[I]->LocalNumCXXBaseSpecifiers)
3726 return Offset + Chain[I]->CXXBaseSpecifiersOffsets[ID];
3727
3728 ID -= Chain[I]->LocalNumCXXBaseSpecifiers;
3729 Offset += Chain[I]->SizeInBits;
3730 }
3731
3732 assert(false && "CXXBaseSpecifiers not found");
3733 return 0;
3734}
3735
3736CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
3737 // Figure out which AST file contains this offset.
3738 PerFileData *F = 0;
3739 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3740 if (Offset < Chain[I]->SizeInBits) {
3741 F = Chain[I];
3742 break;
3743 }
3744
3745 Offset -= Chain[I]->SizeInBits;
3746 }
3747
3748 if (!F) {
3749 Error("Malformed AST file: C++ base specifiers at impossible offset");
3750 return 0;
3751 }
3752
3753 llvm::BitstreamCursor &Cursor = F->DeclsCursor;
3754 SavedStreamPosition SavedPosition(Cursor);
3755 Cursor.JumpToBit(Offset);
3756 ReadingKindTracker ReadingKind(Read_Decl, *this);
3757 RecordData Record;
3758 unsigned Code = Cursor.ReadCode();
3759 unsigned RecCode = Cursor.ReadRecord(Code, Record);
3760 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
3761 Error("Malformed AST file: missing C++ base specifiers");
3762 return 0;
3763 }
3764
3765 unsigned Idx = 0;
3766 unsigned NumBases = Record[Idx++];
3767 void *Mem = Context->Allocate(sizeof(CXXBaseSpecifier) * NumBases);
3768 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
3769 for (unsigned I = 0; I != NumBases; ++I)
3770 Bases[I] = ReadCXXBaseSpecifier(*F, Record, Idx);
3771 return Bases;
3772}
3773
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003774TranslationUnitDecl *ASTReader::GetTranslationUnitDecl() {
Sebastian Redl30c514c2010-07-14 23:45:08 +00003775 if (!DeclsLoaded[0]) {
Sebastian Redle1dde812010-08-24 00:50:04 +00003776 ReadDeclRecord(0, 1);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003777 if (DeserializationListener)
Sebastian Redl1476ed42010-07-16 16:36:56 +00003778 DeserializationListener->DeclRead(1, DeclsLoaded[0]);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003779 }
Argyrios Kyrtzidis8871a442010-07-08 17:13:02 +00003780
3781 return cast<TranslationUnitDecl>(DeclsLoaded[0]);
3782}
3783
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003784Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003785 if (ID == 0)
3786 return 0;
3787
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003788 if (ID > DeclsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003789 Error("declaration ID out-of-range for AST file");
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003790 return 0;
3791 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003792
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003793 unsigned Index = ID - 1;
Sebastian Redl30c514c2010-07-14 23:45:08 +00003794 if (!DeclsLoaded[Index]) {
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00003795 ReadDeclRecord(Index, ID);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003796 if (DeserializationListener)
3797 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
3798 }
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003799
3800 return DeclsLoaded[Index];
Douglas Gregor2cf26342009-04-09 22:27:44 +00003801}
3802
Chris Lattner887e2b32009-04-27 05:46:25 +00003803/// \brief Resolve the offset of a statement into a statement.
3804///
3805/// This operation will read a new statement from the external
3806/// source each time it is called, and is meant to be used via a
3807/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003808Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00003809 // Switch case IDs are per Decl.
3810 ClearSwitchCaseIDs();
3811
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003812 // Offset here is a global offset across the entire chain.
3813 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3814 PerFileData &F = *Chain[N - I - 1];
3815 if (Offset < F.SizeInBits) {
3816 // Since we know that this statement is part of a decl, make sure to use
3817 // the decl cursor to read it.
3818 F.DeclsCursor.JumpToBit(Offset);
Sebastian Redlc3632732010-10-05 15:59:54 +00003819 return ReadStmtFromStream(F);
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003820 }
3821 Offset -= F.SizeInBits;
3822 }
3823 llvm_unreachable("Broken chain");
Douglas Gregor250fc9c2009-04-18 00:07:54 +00003824}
3825
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003826bool ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00003827 bool (*isKindWeWant)(Decl::Kind),
John McCall76bd1f32010-06-01 09:23:16 +00003828 llvm::SmallVectorImpl<Decl*> &Decls) {
Mike Stump1eb44332009-09-09 15:08:12 +00003829 assert(DC->hasExternalLexicalStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00003830 "DeclContext has no lexical decls in storage");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003831
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003832 // There might be lexical decls in multiple parts of the chain, for the TU
3833 // at least.
Sebastian Redl4a9eb262010-09-28 02:24:44 +00003834 // DeclContextOffsets might reallocate as we load additional decls below,
3835 // so make a copy of the vector.
3836 DeclContextInfos Infos = DeclContextOffsets[DC];
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003837 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3838 I != E; ++I) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003839 // IDs can be 0 if this context doesn't contain declarations.
3840 if (!I->LexicalDecls)
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003841 continue;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003842
3843 // Load all of the declaration IDs
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00003844 for (const KindDeclIDPair *ID = I->LexicalDecls,
3845 *IDE = ID + I->NumLexicalDecls; ID != IDE; ++ID) {
3846 if (isKindWeWant && !isKindWeWant((Decl::Kind)ID->first))
3847 continue;
3848
3849 Decl *D = GetDecl(ID->second);
Sebastian Redl4a9eb262010-09-28 02:24:44 +00003850 assert(D && "Null decl in lexical decls");
3851 Decls.push_back(D);
3852 }
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003853 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003854
Douglas Gregor25123082009-04-22 22:34:57 +00003855 ++NumLexicalDeclContextsRead;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003856 return false;
3857}
3858
John McCall76bd1f32010-06-01 09:23:16 +00003859DeclContext::lookup_result
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003860ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +00003861 DeclarationName Name) {
Mike Stump1eb44332009-09-09 15:08:12 +00003862 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00003863 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003864 if (!Name)
3865 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
3866 DeclContext::lookup_iterator(0));
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003867
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003868 llvm::SmallVector<NamedDecl *, 64> Decls;
Sebastian Redl8b122732010-08-24 00:49:55 +00003869 // There might be visible decls in multiple parts of the chain, for the TU
Sebastian Redl5967d622010-08-24 00:50:16 +00003870 // and namespaces. For any given name, the last available results replace
3871 // all earlier ones. For this reason, we walk in reverse.
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003872 DeclContextInfos &Infos = DeclContextOffsets[DC];
Sebastian Redl5967d622010-08-24 00:50:16 +00003873 for (DeclContextInfos::reverse_iterator I = Infos.rbegin(), E = Infos.rend();
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003874 I != E; ++I) {
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003875 if (!I->NameLookupTableData)
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003876 continue;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003877
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003878 ASTDeclContextNameLookupTable *LookupTable =
3879 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3880 ASTDeclContextNameLookupTable::iterator Pos = LookupTable->find(Name);
3881 if (Pos == LookupTable->end())
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003882 continue;
3883
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003884 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
3885 for (; Data.first != Data.second; ++Data.first)
3886 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
Sebastian Redl5967d622010-08-24 00:50:16 +00003887 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003888 }
3889
Douglas Gregor25123082009-04-22 22:34:57 +00003890 ++NumVisibleDeclContextsRead;
John McCall76bd1f32010-06-01 09:23:16 +00003891
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003892 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall76bd1f32010-06-01 09:23:16 +00003893 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003894}
3895
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +00003896void ASTReader::MaterializeVisibleDecls(const DeclContext *DC) {
3897 assert(DC->hasExternalVisibleStorage() &&
3898 "DeclContext has no visible decls in storage");
3899
3900 llvm::SmallVector<NamedDecl *, 64> Decls;
3901 // There might be visible decls in multiple parts of the chain, for the TU
3902 // and namespaces.
3903 DeclContextInfos &Infos = DeclContextOffsets[DC];
3904 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3905 I != E; ++I) {
3906 if (!I->NameLookupTableData)
3907 continue;
3908
3909 ASTDeclContextNameLookupTable *LookupTable =
3910 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3911 for (ASTDeclContextNameLookupTable::item_iterator
3912 ItemI = LookupTable->item_begin(),
3913 ItemEnd = LookupTable->item_end() ; ItemI != ItemEnd; ++ItemI) {
3914 ASTDeclContextNameLookupTable::item_iterator::value_type Val
3915 = *ItemI;
3916 ASTDeclContextNameLookupTrait::data_type Data = Val.second;
3917 Decls.clear();
3918 for (; Data.first != Data.second; ++Data.first)
3919 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
3920 MaterializeVisibleDeclsForName(DC, Val.first, Decls);
3921 }
3922 }
3923}
3924
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003925void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003926 assert(Consumer);
3927 while (!InterestingDecls.empty()) {
3928 DeclGroupRef DG(InterestingDecls.front());
3929 InterestingDecls.pop_front();
Sebastian Redl27372b42010-08-11 18:52:41 +00003930 Consumer->HandleInterestingDecl(DG);
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003931 }
3932}
3933
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003934void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor0af2ca42009-04-22 19:09:20 +00003935 this->Consumer = Consumer;
3936
Douglas Gregorfdd01722009-04-14 00:24:19 +00003937 if (!Consumer)
3938 return;
3939
3940 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003941 // Force deserialization of this decl, which will cause it to be queued for
3942 // passing to the consumer.
Daniel Dunbar04a0b502009-09-17 03:06:44 +00003943 GetDecl(ExternalDefinitions[I]);
Douglas Gregorfdd01722009-04-14 00:24:19 +00003944 }
Douglas Gregorc62a2fe2009-04-25 00:41:30 +00003945
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003946 PassInterestingDeclsToConsumer();
Douglas Gregorfdd01722009-04-14 00:24:19 +00003947}
3948
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003949void ASTReader::PrintStats() {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003950 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregor2cf26342009-04-09 22:27:44 +00003951
Mike Stump1eb44332009-09-09 15:08:12 +00003952 unsigned NumTypesLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003953 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall0953e762009-09-24 19:53:00 +00003954 QualType());
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003955 unsigned NumDeclsLoaded
3956 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
3957 (Decl *)0);
3958 unsigned NumIdentifiersLoaded
3959 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
3960 IdentifiersLoaded.end(),
3961 (IdentifierInfo *)0);
Mike Stump1eb44332009-09-09 15:08:12 +00003962 unsigned NumSelectorsLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003963 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
3964 SelectorsLoaded.end(),
3965 Selector());
Douglas Gregor2d41cc12009-04-13 20:50:16 +00003966
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003967 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
3968 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00003969 if (TotalNumSLocEntries)
3970 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
3971 NumSLocEntriesRead, TotalNumSLocEntries,
3972 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003973 if (!TypesLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003974 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003975 NumTypesLoaded, (unsigned)TypesLoaded.size(),
3976 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
3977 if (!DeclsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003978 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003979 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
3980 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003981 if (!IdentifiersLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003982 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003983 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
3984 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Sebastian Redl725cd962010-08-04 20:40:17 +00003985 if (!SelectorsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003986 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redl725cd962010-08-04 20:40:17 +00003987 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
3988 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor83941df2009-04-25 17:48:32 +00003989 if (TotalNumStatements)
3990 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
3991 NumStatementsRead, TotalNumStatements,
3992 ((float)NumStatementsRead/TotalNumStatements * 100));
3993 if (TotalNumMacros)
3994 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
3995 NumMacrosRead, TotalNumMacros,
3996 ((float)NumMacrosRead/TotalNumMacros * 100));
3997 if (TotalLexicalDeclContexts)
3998 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
3999 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
4000 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
4001 * 100));
4002 if (TotalVisibleDeclContexts)
4003 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
4004 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
4005 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
4006 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00004007 if (TotalNumMethodPoolEntries) {
Douglas Gregor83941df2009-04-25 17:48:32 +00004008 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redlfa78dec2010-08-04 21:22:45 +00004009 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
4010 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor83941df2009-04-25 17:48:32 +00004011 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00004012 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor83941df2009-04-25 17:48:32 +00004013 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00004014 std::fprintf(stderr, "\n");
4015}
4016
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004017void ASTReader::InitializeSema(Sema &S) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00004018 SemaObj = &S;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00004019 S.ExternalSource = this;
4020
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00004021 // Makes sure any declarations that were deserialized "too early"
4022 // still get added to the identifier's declaration chains.
Douglas Gregor76dc8892010-09-24 23:29:12 +00004023 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
4024 if (SemaObj->TUScope)
John McCalld226f652010-08-21 09:40:31 +00004025 SemaObj->TUScope->AddDecl(PreloadedDecls[I]);
Douglas Gregor76dc8892010-09-24 23:29:12 +00004026
4027 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregor668c1a42009-04-21 22:25:48 +00004028 }
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00004029 PreloadedDecls.clear();
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00004030
4031 // If there were any tentative definitions, deserialize them and add
Sebastian Redle9d12b62010-01-31 22:27:38 +00004032 // them to Sema's list of tentative definitions.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00004033 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
4034 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
Sebastian Redle9d12b62010-01-31 22:27:38 +00004035 SemaObj->TentativeDefinitions.push_back(Var);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00004036 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00004037
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00004038 // If there were any unused file scoped decls, deserialize them and add to
4039 // Sema's list of unused file scoped decls.
4040 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
4041 DeclaratorDecl *D = cast<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
4042 SemaObj->UnusedFileScopedDecls.push_back(D);
Tanya Lattnere6bbc012010-02-12 00:07:30 +00004043 }
Douglas Gregor14c22f22009-04-22 22:18:58 +00004044
4045 // If there were any locally-scoped external declarations,
4046 // deserialize them and add them to Sema's table of locally-scoped
4047 // external declarations.
4048 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
4049 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
4050 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
4051 }
Douglas Gregorb81c1702009-04-27 20:06:05 +00004052
4053 // If there were any ext_vector type declarations, deserialize them
4054 // and add them to Sema's vector of such declarations.
4055 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I)
4056 SemaObj->ExtVectorDecls.push_back(
4057 cast<TypedefDecl>(GetDecl(ExtVectorDecls[I])));
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004058
4059 // FIXME: Do VTable uses and dynamic classes deserialize too much ?
4060 // Can we cut them down before writing them ?
4061
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004062 // If there were any dynamic classes declarations, deserialize them
4063 // and add them to Sema's vector of such declarations.
4064 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I)
4065 SemaObj->DynamicClasses.push_back(
4066 cast<CXXRecordDecl>(GetDecl(DynamicClasses[I])));
Fariborz Jahanian32019832010-07-23 19:11:11 +00004067
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00004068 // Load the offsets of the declarations that Sema references.
4069 // They will be lazily deserialized when needed.
4070 if (!SemaDeclRefs.empty()) {
4071 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
4072 SemaObj->StdNamespace = SemaDeclRefs[0];
4073 SemaObj->StdBadAlloc = SemaDeclRefs[1];
4074 }
4075
Sebastian Redlc3632732010-10-05 15:59:54 +00004076 for (PerFileData *F = FirstInSource; F; F = F->NextInSource) {
4077
4078 // If there are @selector references added them to its pool. This is for
4079 // implementation of -Wselector.
4080 if (!F->ReferencedSelectorsData.empty()) {
4081 unsigned int DataSize = F->ReferencedSelectorsData.size()-1;
4082 unsigned I = 0;
4083 while (I < DataSize) {
4084 Selector Sel = DecodeSelector(F->ReferencedSelectorsData[I++]);
4085 SourceLocation SelLoc = ReadSourceLocation(
4086 *F, F->ReferencedSelectorsData, I);
4087 SemaObj->ReferencedSelectors.insert(std::make_pair(Sel, SelLoc));
4088 }
4089 }
4090
4091 // If there were any pending implicit instantiations, deserialize them
4092 // and add them to Sema's queue of such instantiations.
4093 assert(F->PendingInstantiations.size() % 2 == 0 &&
4094 "Expected pairs of entries");
4095 for (unsigned Idx = 0, N = F->PendingInstantiations.size(); Idx < N;) {
4096 ValueDecl *D=cast<ValueDecl>(GetDecl(F->PendingInstantiations[Idx++]));
4097 SourceLocation Loc = ReadSourceLocation(*F, F->PendingInstantiations,Idx);
4098 SemaObj->PendingInstantiations.push_back(std::make_pair(D, Loc));
4099 }
4100 }
4101
4102 // The two special data sets below always come from the most recent PCH,
4103 // which is at the front of the chain.
4104 PerFileData &F = *Chain.front();
4105
4106 // If there were any weak undeclared identifiers, deserialize them and add to
4107 // Sema's list of weak undeclared identifiers.
4108 if (!WeakUndeclaredIdentifiers.empty()) {
4109 unsigned Idx = 0;
4110 for (unsigned I = 0, N = WeakUndeclaredIdentifiers[Idx++]; I != N; ++I) {
4111 IdentifierInfo *WeakId = GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
4112 IdentifierInfo *AliasId= GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
4113 SourceLocation Loc = ReadSourceLocation(F, WeakUndeclaredIdentifiers,Idx);
4114 bool Used = WeakUndeclaredIdentifiers[Idx++];
4115 Sema::WeakInfo WI(AliasId, Loc);
4116 WI.setUsed(Used);
4117 SemaObj->WeakUndeclaredIdentifiers.insert(std::make_pair(WeakId, WI));
4118 }
4119 }
4120
4121 // If there were any VTable uses, deserialize the information and add it
4122 // to Sema's vector and map of VTable uses.
4123 if (!VTableUses.empty()) {
4124 unsigned Idx = 0;
4125 for (unsigned I = 0, N = VTableUses[Idx++]; I != N; ++I) {
4126 CXXRecordDecl *Class = cast<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
4127 SourceLocation Loc = ReadSourceLocation(F, VTableUses, Idx);
4128 bool DefinitionRequired = VTableUses[Idx++];
4129 SemaObj->VTableUses.push_back(std::make_pair(Class, Loc));
4130 SemaObj->VTablesUsed[Class] = DefinitionRequired;
Fariborz Jahanian32019832010-07-23 19:11:11 +00004131 }
4132 }
Peter Collingbourne84bccea2011-02-15 19:46:30 +00004133
4134 if (!FPPragmaOptions.empty()) {
4135 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
4136 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
4137 }
4138
4139 if (!OpenCLExtensions.empty()) {
4140 unsigned I = 0;
4141#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
4142#include "clang/Basic/OpenCLExtensions.def"
4143
4144 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
4145 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00004146}
4147
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004148IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Sebastian Redld8c5abb2010-08-02 18:30:12 +00004149 // Try to find this name within our on-disk hash tables. We start with the
4150 // most recent one, since that one contains the most up-to-date info.
Sebastian Redld27d3fc2010-07-21 22:31:37 +00004151 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004152 ASTIdentifierLookupTable *IdTable
4153 = (ASTIdentifierLookupTable *)Chain[I]->IdentifierLookupTable;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00004154 if (!IdTable)
4155 continue;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00004156 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004157 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00004158 if (Pos == IdTable->end())
4159 continue;
Douglas Gregor668c1a42009-04-21 22:25:48 +00004160
Sebastian Redld27d3fc2010-07-21 22:31:37 +00004161 // Dereferencing the iterator has the effect of building the
4162 // IdentifierInfo node and populating it with the various
4163 // declarations it needs.
Sebastian Redld8c5abb2010-08-02 18:30:12 +00004164 return *Pos;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00004165 }
Sebastian Redld8c5abb2010-08-02 18:30:12 +00004166 return 0;
Douglas Gregor668c1a42009-04-21 22:25:48 +00004167}
4168
Douglas Gregor95f42922010-10-14 22:11:03 +00004169namespace clang {
4170 /// \brief An identifier-lookup iterator that enumerates all of the
4171 /// identifiers stored within a set of AST files.
4172 class ASTIdentifierIterator : public IdentifierIterator {
4173 /// \brief The AST reader whose identifiers are being enumerated.
4174 const ASTReader &Reader;
4175
4176 /// \brief The current index into the chain of AST files stored in
4177 /// the AST reader.
4178 unsigned Index;
4179
4180 /// \brief The current position within the identifier lookup table
4181 /// of the current AST file.
4182 ASTIdentifierLookupTable::key_iterator Current;
4183
4184 /// \brief The end position within the identifier lookup table of
4185 /// the current AST file.
4186 ASTIdentifierLookupTable::key_iterator End;
4187
4188 public:
4189 explicit ASTIdentifierIterator(const ASTReader &Reader);
4190
4191 virtual llvm::StringRef Next();
4192 };
4193}
4194
4195ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
4196 : Reader(Reader), Index(Reader.Chain.size() - 1) {
4197 ASTIdentifierLookupTable *IdTable
4198 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
4199 Current = IdTable->key_begin();
4200 End = IdTable->key_end();
4201}
4202
4203llvm::StringRef ASTIdentifierIterator::Next() {
4204 while (Current == End) {
4205 // If we have exhausted all of our AST files, we're done.
4206 if (Index == 0)
4207 return llvm::StringRef();
4208
4209 --Index;
4210 ASTIdentifierLookupTable *IdTable
4211 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
4212 Current = IdTable->key_begin();
4213 End = IdTable->key_end();
4214 }
4215
4216 // We have any identifiers remaining in the current AST file; return
4217 // the next one.
4218 std::pair<const char*, unsigned> Key = *Current;
4219 ++Current;
4220 return llvm::StringRef(Key.first, Key.second);
4221}
4222
4223IdentifierIterator *ASTReader::getIdentifiers() const {
4224 return new ASTIdentifierIterator(*this);
4225}
4226
Mike Stump1eb44332009-09-09 15:08:12 +00004227std::pair<ObjCMethodList, ObjCMethodList>
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004228ASTReader::ReadMethodPool(Selector Sel) {
Sebastian Redl725cd962010-08-04 20:40:17 +00004229 // Find this selector in a hash table. We want to find the most recent entry.
4230 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4231 PerFileData &F = *Chain[I];
4232 if (!F.SelectorLookupTable)
4233 continue;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00004234
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004235 ASTSelectorLookupTable *PoolTable
4236 = (ASTSelectorLookupTable*)F.SelectorLookupTable;
4237 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
Sebastian Redl725cd962010-08-04 20:40:17 +00004238 if (Pos != PoolTable->end()) {
4239 ++NumSelectorsRead;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00004240 // FIXME: Not quite happy with the statistics here. We probably should
4241 // disable this tracking when called via LoadSelector.
4242 // Also, should entries without methods count as misses?
4243 ++NumMethodPoolEntriesRead;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004244 ASTSelectorLookupTrait::data_type Data = *Pos;
Sebastian Redl725cd962010-08-04 20:40:17 +00004245 if (DeserializationListener)
4246 DeserializationListener->SelectorRead(Data.ID, Sel);
4247 return std::make_pair(Data.Instance, Data.Factory);
4248 }
Douglas Gregor83941df2009-04-25 17:48:32 +00004249 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00004250
Sebastian Redlfa78dec2010-08-04 21:22:45 +00004251 ++NumMethodPoolMisses;
Sebastian Redl725cd962010-08-04 20:40:17 +00004252 return std::pair<ObjCMethodList, ObjCMethodList>();
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00004253}
4254
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004255void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redle58aa892010-08-04 18:21:41 +00004256 // It would be complicated to avoid reading the methods anyway. So don't.
4257 ReadMethodPool(Sel);
4258}
4259
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004260void ASTReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00004261 assert(ID && "Non-zero identifier ID required");
Douglas Gregora02b1472009-04-28 21:53:25 +00004262 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00004263 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004264 if (DeserializationListener)
4265 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregor668c1a42009-04-21 22:25:48 +00004266}
4267
Douglas Gregord89275b2009-07-06 18:54:52 +00004268/// \brief Set the globally-visible declarations associated with the given
4269/// identifier.
4270///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004271/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump1eb44332009-09-09 15:08:12 +00004272/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregord89275b2009-07-06 18:54:52 +00004273/// them.
4274///
4275/// \param II an IdentifierInfo that refers to one or more globally-visible
4276/// declarations.
4277///
4278/// \param DeclIDs the set of declaration IDs with the name @p II that are
4279/// visible at global scope.
4280///
4281/// \param Nonrecursive should be true to indicate that the caller knows that
4282/// this call is non-recursive, and therefore the globally-visible declarations
4283/// will not be placed onto the pending queue.
Mike Stump1eb44332009-09-09 15:08:12 +00004284void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004285ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Douglas Gregord89275b2009-07-06 18:54:52 +00004286 const llvm::SmallVectorImpl<uint32_t> &DeclIDs,
4287 bool Nonrecursive) {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004288 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregord89275b2009-07-06 18:54:52 +00004289 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
4290 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
4291 PII.II = II;
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00004292 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregord89275b2009-07-06 18:54:52 +00004293 return;
4294 }
Mike Stump1eb44332009-09-09 15:08:12 +00004295
Douglas Gregord89275b2009-07-06 18:54:52 +00004296 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
4297 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
4298 if (SemaObj) {
Douglas Gregor914ed9d2010-08-13 03:15:25 +00004299 if (SemaObj->TUScope) {
4300 // Introduce this declaration into the translation-unit scope
4301 // and add it to the declaration chain for this identifier, so
4302 // that (unqualified) name lookup will find it.
John McCalld226f652010-08-21 09:40:31 +00004303 SemaObj->TUScope->AddDecl(D);
Douglas Gregor914ed9d2010-08-13 03:15:25 +00004304 }
Douglas Gregor76dc8892010-09-24 23:29:12 +00004305 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
Douglas Gregord89275b2009-07-06 18:54:52 +00004306 } else {
4307 // Queue this declaration so that it will be added to the
4308 // translation unit scope and identifier's declaration chain
4309 // once a Sema object is known.
4310 PreloadedDecls.push_back(D);
4311 }
4312 }
4313}
4314
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004315IdentifierInfo *ASTReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00004316 if (ID == 0)
4317 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00004318
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00004319 if (IdentifiersLoaded.empty()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004320 Error("no identifier table in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00004321 return 0;
4322 }
Mike Stump1eb44332009-09-09 15:08:12 +00004323
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00004324 assert(PP && "Forgot to set Preprocessor ?");
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00004325 ID -= 1;
4326 if (!IdentifiersLoaded[ID]) {
4327 unsigned Index = ID;
4328 const char *Str = 0;
4329 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4330 PerFileData *F = Chain[N - I - 1];
4331 if (Index < F->LocalNumIdentifiers) {
4332 uint32_t Offset = F->IdentifierOffsets[Index];
4333 Str = F->IdentifierTableData + Offset;
4334 break;
4335 }
4336 Index -= F->LocalNumIdentifiers;
4337 }
4338 assert(Str && "Broken Chain");
Douglas Gregord6595a42009-04-25 21:04:17 +00004339
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004340 // All of the strings in the AST file are preceded by a 16-bit length.
4341 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenek231bc0b2009-10-23 04:45:31 +00004342 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
4343 // unsigned integers. This is important to avoid integer overflow when
4344 // we cast them to 'unsigned'.
Ted Kremenekff1ea462009-10-23 03:57:22 +00004345 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregor02fc7512009-04-28 20:01:51 +00004346 unsigned StrLen = (((unsigned) StrLenPtr[0])
4347 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00004348 IdentifiersLoaded[ID]
Kovarththanan Rajaratnam811f4262010-03-12 10:32:27 +00004349 = &PP->getIdentifierTable().get(Str, StrLen);
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004350 if (DeserializationListener)
4351 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregorafaf3082009-04-11 00:14:32 +00004352 }
Mike Stump1eb44332009-09-09 15:08:12 +00004353
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00004354 return IdentifiersLoaded[ID];
Douglas Gregor2cf26342009-04-09 22:27:44 +00004355}
4356
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004357void ASTReader::ReadSLocEntry(unsigned ID) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00004358 ReadSLocEntryRecord(ID);
4359}
4360
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004361Selector ASTReader::DecodeSelector(unsigned ID) {
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004362 if (ID == 0)
4363 return Selector();
Mike Stump1eb44332009-09-09 15:08:12 +00004364
Sebastian Redl725cd962010-08-04 20:40:17 +00004365 if (ID > SelectorsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004366 Error("selector ID out of range in AST file");
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004367 return Selector();
4368 }
Douglas Gregor83941df2009-04-25 17:48:32 +00004369
Sebastian Redl725cd962010-08-04 20:40:17 +00004370 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor83941df2009-04-25 17:48:32 +00004371 // Load this selector from the selector table.
Sebastian Redl725cd962010-08-04 20:40:17 +00004372 unsigned Idx = ID - 1;
4373 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4374 PerFileData &F = *Chain[N - I - 1];
4375 if (Idx < F.LocalNumSelectors) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004376 ASTSelectorLookupTrait Trait(*this);
Sebastian Redl725cd962010-08-04 20:40:17 +00004377 SelectorsLoaded[ID - 1] =
4378 Trait.ReadKey(F.SelectorLookupTableData + F.SelectorOffsets[Idx], 0);
4379 if (DeserializationListener)
4380 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
4381 break;
4382 }
4383 Idx -= F.LocalNumSelectors;
4384 }
Douglas Gregor83941df2009-04-25 17:48:32 +00004385 }
4386
Sebastian Redl725cd962010-08-04 20:40:17 +00004387 return SelectorsLoaded[ID - 1];
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004388}
4389
Michael J. Spencer20249a12010-10-21 03:16:25 +00004390Selector ASTReader::GetExternalSelector(uint32_t ID) {
Douglas Gregor719770d2010-04-06 17:30:22 +00004391 return DecodeSelector(ID);
4392}
4393
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004394uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redl725cd962010-08-04 20:40:17 +00004395 // ID 0 (the null selector) is considered an external selector.
4396 return getTotalNumSelectors() + 1;
Douglas Gregor719770d2010-04-06 17:30:22 +00004397}
4398
Mike Stump1eb44332009-09-09 15:08:12 +00004399DeclarationName
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004400ASTReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00004401 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
4402 switch (Kind) {
4403 case DeclarationName::Identifier:
4404 return DeclarationName(GetIdentifierInfo(Record, Idx));
4405
4406 case DeclarationName::ObjCZeroArgSelector:
4407 case DeclarationName::ObjCOneArgSelector:
4408 case DeclarationName::ObjCMultiArgSelector:
Steve Naroffa7503a72009-04-23 15:15:40 +00004409 return DeclarationName(GetSelector(Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004410
4411 case DeclarationName::CXXConstructorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00004412 return Context->DeclarationNames.getCXXConstructorName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00004413 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004414
4415 case DeclarationName::CXXDestructorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00004416 return Context->DeclarationNames.getCXXDestructorName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00004417 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004418
4419 case DeclarationName::CXXConversionFunctionName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00004420 return Context->DeclarationNames.getCXXConversionFunctionName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00004421 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004422
4423 case DeclarationName::CXXOperatorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00004424 return Context->DeclarationNames.getCXXOperatorName(
Douglas Gregor2cf26342009-04-09 22:27:44 +00004425 (OverloadedOperatorKind)Record[Idx++]);
4426
Sean Hunt3e518bd2009-11-29 07:34:05 +00004427 case DeclarationName::CXXLiteralOperatorName:
4428 return Context->DeclarationNames.getCXXLiteralOperatorName(
4429 GetIdentifierInfo(Record, Idx));
4430
Douglas Gregor2cf26342009-04-09 22:27:44 +00004431 case DeclarationName::CXXUsingDirective:
4432 return DeclarationName::getUsingDirectiveName();
4433 }
4434
4435 // Required to silence GCC warning
4436 return DeclarationName();
4437}
Douglas Gregor0a0428e2009-04-10 20:39:37 +00004438
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004439void ASTReader::ReadDeclarationNameLoc(PerFileData &F,
4440 DeclarationNameLoc &DNLoc,
4441 DeclarationName Name,
4442 const RecordData &Record, unsigned &Idx) {
4443 switch (Name.getNameKind()) {
4444 case DeclarationName::CXXConstructorName:
4445 case DeclarationName::CXXDestructorName:
4446 case DeclarationName::CXXConversionFunctionName:
4447 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
4448 break;
4449
4450 case DeclarationName::CXXOperatorName:
4451 DNLoc.CXXOperatorName.BeginOpNameLoc
4452 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4453 DNLoc.CXXOperatorName.EndOpNameLoc
4454 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4455 break;
4456
4457 case DeclarationName::CXXLiteralOperatorName:
4458 DNLoc.CXXLiteralOperatorName.OpNameLoc
4459 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4460 break;
4461
4462 case DeclarationName::Identifier:
4463 case DeclarationName::ObjCZeroArgSelector:
4464 case DeclarationName::ObjCOneArgSelector:
4465 case DeclarationName::ObjCMultiArgSelector:
4466 case DeclarationName::CXXUsingDirective:
4467 break;
4468 }
4469}
4470
4471void ASTReader::ReadDeclarationNameInfo(PerFileData &F,
4472 DeclarationNameInfo &NameInfo,
4473 const RecordData &Record, unsigned &Idx) {
4474 NameInfo.setName(ReadDeclarationName(Record, Idx));
4475 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
4476 DeclarationNameLoc DNLoc;
4477 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
4478 NameInfo.setInfo(DNLoc);
4479}
4480
4481void ASTReader::ReadQualifierInfo(PerFileData &F, QualifierInfo &Info,
4482 const RecordData &Record, unsigned &Idx) {
4483 Info.NNS = ReadNestedNameSpecifier(Record, Idx);
4484 Info.NNSRange = ReadSourceRange(F, Record, Idx);
4485 unsigned NumTPLists = Record[Idx++];
4486 Info.NumTemplParamLists = NumTPLists;
4487 if (NumTPLists) {
4488 Info.TemplParamLists = new (*Context) TemplateParameterList*[NumTPLists];
4489 for (unsigned i=0; i != NumTPLists; ++i)
4490 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
4491 }
4492}
4493
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004494TemplateName
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004495ASTReader::ReadTemplateName(PerFileData &F, const RecordData &Record,
4496 unsigned &Idx) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00004497 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004498 switch (Kind) {
4499 case TemplateName::Template:
4500 return TemplateName(cast_or_null<TemplateDecl>(GetDecl(Record[Idx++])));
4501
4502 case TemplateName::OverloadedTemplate: {
4503 unsigned size = Record[Idx++];
4504 UnresolvedSet<8> Decls;
4505 while (size--)
4506 Decls.addDecl(cast<NamedDecl>(GetDecl(Record[Idx++])));
4507
4508 return Context->getOverloadedTemplateName(Decls.begin(), Decls.end());
4509 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004510
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004511 case TemplateName::QualifiedTemplate: {
4512 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
4513 bool hasTemplKeyword = Record[Idx++];
4514 TemplateDecl *Template = cast<TemplateDecl>(GetDecl(Record[Idx++]));
4515 return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
4516 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004517
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004518 case TemplateName::DependentTemplate: {
4519 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
4520 if (Record[Idx++]) // isIdentifier
4521 return Context->getDependentTemplateName(NNS,
4522 GetIdentifierInfo(Record, Idx));
4523 return Context->getDependentTemplateName(NNS,
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004524 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004525 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004526
4527 case TemplateName::SubstTemplateTemplateParmPack: {
4528 TemplateTemplateParmDecl *Param
4529 = cast_or_null<TemplateTemplateParmDecl>(GetDecl(Record[Idx++]));
4530 if (!Param)
4531 return TemplateName();
4532
4533 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
4534 if (ArgPack.getKind() != TemplateArgument::Pack)
4535 return TemplateName();
4536
4537 return Context->getSubstTemplateTemplateParmPack(Param, ArgPack);
4538 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004539 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004540
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004541 assert(0 && "Unhandled template name kind!");
4542 return TemplateName();
4543}
4544
4545TemplateArgument
Sebastian Redlc3632732010-10-05 15:59:54 +00004546ASTReader::ReadTemplateArgument(PerFileData &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00004547 const RecordData &Record, unsigned &Idx) {
Douglas Gregora7fc9012011-01-05 18:58:31 +00004548 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
4549 switch (Kind) {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004550 case TemplateArgument::Null:
4551 return TemplateArgument();
4552 case TemplateArgument::Type:
4553 return TemplateArgument(GetType(Record[Idx++]));
4554 case TemplateArgument::Declaration:
4555 return TemplateArgument(GetDecl(Record[Idx++]));
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00004556 case TemplateArgument::Integral: {
4557 llvm::APSInt Value = ReadAPSInt(Record, Idx);
4558 QualType T = GetType(Record[Idx++]);
4559 return TemplateArgument(Value, T);
4560 }
Douglas Gregora7fc9012011-01-05 18:58:31 +00004561 case TemplateArgument::Template:
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004562 return TemplateArgument(ReadTemplateName(F, Record, Idx));
Douglas Gregora7fc9012011-01-05 18:58:31 +00004563 case TemplateArgument::TemplateExpansion: {
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004564 TemplateName Name = ReadTemplateName(F, Record, Idx);
Douglas Gregor2be29f42011-01-14 23:41:42 +00004565 llvm::Optional<unsigned> NumTemplateExpansions;
4566 if (unsigned NumExpansions = Record[Idx++])
4567 NumTemplateExpansions = NumExpansions - 1;
4568 return TemplateArgument(Name, NumTemplateExpansions);
Douglas Gregorba68eca2011-01-05 17:40:24 +00004569 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004570 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00004571 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004572 case TemplateArgument::Pack: {
4573 unsigned NumArgs = Record[Idx++];
Douglas Gregor910f8002010-11-07 23:05:16 +00004574 TemplateArgument *Args = new (*Context) TemplateArgument[NumArgs];
4575 for (unsigned I = 0; I != NumArgs; ++I)
4576 Args[I] = ReadTemplateArgument(F, Record, Idx);
4577 return TemplateArgument(Args, NumArgs);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004578 }
4579 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004580
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004581 assert(0 && "Unhandled template argument kind!");
4582 return TemplateArgument();
4583}
4584
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004585TemplateParameterList *
Sebastian Redlc3632732010-10-05 15:59:54 +00004586ASTReader::ReadTemplateParameterList(PerFileData &F,
4587 const RecordData &Record, unsigned &Idx) {
4588 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
4589 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
4590 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004591
4592 unsigned NumParams = Record[Idx++];
4593 llvm::SmallVector<NamedDecl *, 16> Params;
4594 Params.reserve(NumParams);
4595 while (NumParams--)
4596 Params.push_back(cast<NamedDecl>(GetDecl(Record[Idx++])));
Michael J. Spencer20249a12010-10-21 03:16:25 +00004597
4598 TemplateParameterList* TemplateParams =
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004599 TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc,
4600 Params.data(), Params.size(), RAngleLoc);
4601 return TemplateParams;
4602}
4603
4604void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004605ASTReader::
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004606ReadTemplateArgumentList(llvm::SmallVector<TemplateArgument, 8> &TemplArgs,
Sebastian Redlc3632732010-10-05 15:59:54 +00004607 PerFileData &F, const RecordData &Record,
4608 unsigned &Idx) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004609 unsigned NumTemplateArgs = Record[Idx++];
4610 TemplArgs.reserve(NumTemplateArgs);
4611 while (NumTemplateArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00004612 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004613}
4614
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004615/// \brief Read a UnresolvedSet structure.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004616void ASTReader::ReadUnresolvedSet(UnresolvedSetImpl &Set,
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004617 const RecordData &Record, unsigned &Idx) {
4618 unsigned NumDecls = Record[Idx++];
4619 while (NumDecls--) {
4620 NamedDecl *D = cast<NamedDecl>(GetDecl(Record[Idx++]));
4621 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
4622 Set.addDecl(D, AS);
4623 }
4624}
4625
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004626CXXBaseSpecifier
Sebastian Redlc3632732010-10-05 15:59:54 +00004627ASTReader::ReadCXXBaseSpecifier(PerFileData &F,
Nick Lewycky56062202010-07-26 16:56:01 +00004628 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004629 bool isVirtual = static_cast<bool>(Record[Idx++]);
4630 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
4631 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redlf677ea32011-02-05 19:23:19 +00004632 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00004633 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
4634 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00004635 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redlf677ea32011-02-05 19:23:19 +00004636 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
Douglas Gregorf90b27a2011-01-03 22:36:02 +00004637 EllipsisLoc);
Sebastian Redlf677ea32011-02-05 19:23:19 +00004638 Result.setInheritConstructors(inheritConstructors);
4639 return Result;
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004640}
4641
Sean Huntcbb67482011-01-08 20:30:50 +00004642std::pair<CXXCtorInitializer **, unsigned>
4643ASTReader::ReadCXXCtorInitializers(PerFileData &F, const RecordData &Record,
4644 unsigned &Idx) {
4645 CXXCtorInitializer **CtorInitializers = 0;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004646 unsigned NumInitializers = Record[Idx++];
4647 if (NumInitializers) {
4648 ASTContext &C = *getContext();
4649
Sean Huntcbb67482011-01-08 20:30:50 +00004650 CtorInitializers
4651 = new (C) CXXCtorInitializer*[NumInitializers];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004652 for (unsigned i=0; i != NumInitializers; ++i) {
4653 TypeSourceInfo *BaseClassInfo = 0;
4654 bool IsBaseVirtual = false;
4655 FieldDecl *Member = 0;
Francois Pichet00eb3f92010-12-04 09:14:42 +00004656 IndirectFieldDecl *IndirectMember = 0;
Michael J. Spencer20249a12010-10-21 03:16:25 +00004657
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004658 bool IsBaseInitializer = Record[Idx++];
4659 if (IsBaseInitializer) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004660 BaseClassInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004661 IsBaseVirtual = Record[Idx++];
4662 } else {
Francois Pichet00eb3f92010-12-04 09:14:42 +00004663 bool IsIndirectMemberInitializer = Record[Idx++];
4664 if (IsIndirectMemberInitializer)
4665 IndirectMember = cast<IndirectFieldDecl>(GetDecl(Record[Idx++]));
4666 else
4667 Member = cast<FieldDecl>(GetDecl(Record[Idx++]));
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004668 }
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00004669 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redlc3632732010-10-05 15:59:54 +00004670 Expr *Init = ReadExpr(F);
Sebastian Redlc3632732010-10-05 15:59:54 +00004671 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
4672 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004673 bool IsWritten = Record[Idx++];
4674 unsigned SourceOrderOrNumArrayIndices;
4675 llvm::SmallVector<VarDecl *, 8> Indices;
4676 if (IsWritten) {
4677 SourceOrderOrNumArrayIndices = Record[Idx++];
4678 } else {
4679 SourceOrderOrNumArrayIndices = Record[Idx++];
4680 Indices.reserve(SourceOrderOrNumArrayIndices);
4681 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
4682 Indices.push_back(cast<VarDecl>(GetDecl(Record[Idx++])));
4683 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004684
Sean Huntcbb67482011-01-08 20:30:50 +00004685 CXXCtorInitializer *BOMInit;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004686 if (IsBaseInitializer) {
Sean Huntcbb67482011-01-08 20:30:50 +00004687 BOMInit = new (C) CXXCtorInitializer(C, BaseClassInfo, IsBaseVirtual,
4688 LParenLoc, Init, RParenLoc,
4689 MemberOrEllipsisLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004690 } else if (IsWritten) {
Francois Pichet00eb3f92010-12-04 09:14:42 +00004691 if (Member)
Sean Huntcbb67482011-01-08 20:30:50 +00004692 BOMInit = new (C) CXXCtorInitializer(C, Member, MemberOrEllipsisLoc,
4693 LParenLoc, Init, RParenLoc);
Francois Pichet00eb3f92010-12-04 09:14:42 +00004694 else
Sean Huntcbb67482011-01-08 20:30:50 +00004695 BOMInit = new (C) CXXCtorInitializer(C, IndirectMember,
4696 MemberOrEllipsisLoc, LParenLoc,
4697 Init, RParenLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004698 } else {
Sean Huntcbb67482011-01-08 20:30:50 +00004699 BOMInit = CXXCtorInitializer::Create(C, Member, MemberOrEllipsisLoc,
4700 LParenLoc, Init, RParenLoc,
4701 Indices.data(), Indices.size());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004702 }
4703
Argyrios Kyrtzidisf84cde12010-09-06 19:04:27 +00004704 if (IsWritten)
4705 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Sean Huntcbb67482011-01-08 20:30:50 +00004706 CtorInitializers[i] = BOMInit;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004707 }
4708 }
4709
Sean Huntcbb67482011-01-08 20:30:50 +00004710 return std::make_pair(CtorInitializers, NumInitializers);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004711}
4712
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004713NestedNameSpecifier *
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004714ASTReader::ReadNestedNameSpecifier(const RecordData &Record, unsigned &Idx) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004715 unsigned N = Record[Idx++];
4716 NestedNameSpecifier *NNS = 0, *Prev = 0;
4717 for (unsigned I = 0; I != N; ++I) {
4718 NestedNameSpecifier::SpecifierKind Kind
4719 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
4720 switch (Kind) {
4721 case NestedNameSpecifier::Identifier: {
4722 IdentifierInfo *II = GetIdentifierInfo(Record, Idx);
4723 NNS = NestedNameSpecifier::Create(*Context, Prev, II);
4724 break;
4725 }
4726
4727 case NestedNameSpecifier::Namespace: {
4728 NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++]));
4729 NNS = NestedNameSpecifier::Create(*Context, Prev, NS);
4730 break;
4731 }
4732
Douglas Gregor14aba762011-02-24 02:36:08 +00004733 case NestedNameSpecifier::NamespaceAlias: {
4734 NamespaceAliasDecl *Alias
4735 = cast<NamespaceAliasDecl>(GetDecl(Record[Idx++]));
4736 NNS = NestedNameSpecifier::Create(*Context, Prev, Alias);
4737 break;
4738 }
4739
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004740 case NestedNameSpecifier::TypeSpec:
4741 case NestedNameSpecifier::TypeSpecWithTemplate: {
John McCallf4c73712011-01-19 06:33:43 +00004742 const Type *T = GetType(Record[Idx++]).getTypePtrOrNull();
Douglas Gregor1ab55e92010-12-10 17:03:06 +00004743 if (!T)
4744 return 0;
4745
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004746 bool Template = Record[Idx++];
4747 NNS = NestedNameSpecifier::Create(*Context, Prev, Template, T);
4748 break;
4749 }
4750
4751 case NestedNameSpecifier::Global: {
4752 NNS = NestedNameSpecifier::GlobalSpecifier(*Context);
4753 // No associated value, and there can't be a prefix.
4754 break;
4755 }
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004756 }
Argyrios Kyrtzidisd2bb2c02010-07-07 15:46:30 +00004757 Prev = NNS;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004758 }
4759 return NNS;
4760}
4761
Douglas Gregordc355712011-02-25 00:36:19 +00004762NestedNameSpecifierLoc
4763ASTReader::ReadNestedNameSpecifierLoc(PerFileData &F, const RecordData &Record,
4764 unsigned &Idx) {
4765 unsigned N = Record[Idx++];
4766 NestedNameSpecifier *NNS = 0, *Prev = 0;
4767 llvm::SmallVector<char, 32> LocationData;
4768 for (unsigned I = 0; I != N; ++I) {
4769 NestedNameSpecifier::SpecifierKind Kind
4770 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
4771 switch (Kind) {
4772 case NestedNameSpecifier::Identifier: {
4773 // Nested-name-specifier
4774 IdentifierInfo *II = GetIdentifierInfo(Record, Idx);
4775 NNS = NestedNameSpecifier::Create(*Context, Prev, II);
4776
4777 // Location information
4778 SourceRange Range = ReadSourceRange(F, Record, Idx);
4779 unsigned RawStart = Range.getBegin().getRawEncoding();
4780 unsigned RawEnd = Range.getEnd().getRawEncoding();
4781 LocationData.append(reinterpret_cast<char*>(&RawStart),
4782 reinterpret_cast<char*>(&RawStart) +sizeof(unsigned));
4783 LocationData.append(reinterpret_cast<char*>(&RawEnd),
4784 reinterpret_cast<char*>(&RawEnd) + sizeof(unsigned));
4785 break;
4786 }
4787
4788 case NestedNameSpecifier::Namespace: {
4789 // Nested-name-specifier
4790 NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++]));
4791 NNS = NestedNameSpecifier::Create(*Context, Prev, NS);
4792
4793 // Location information
4794 SourceRange Range = ReadSourceRange(F, Record, Idx);
4795 unsigned RawStart = Range.getBegin().getRawEncoding();
4796 unsigned RawEnd = Range.getEnd().getRawEncoding();
4797 LocationData.append(reinterpret_cast<char*>(&RawStart),
4798 reinterpret_cast<char*>(&RawStart) +sizeof(unsigned));
4799 LocationData.append(reinterpret_cast<char*>(&RawEnd),
4800 reinterpret_cast<char*>(&RawEnd) + sizeof(unsigned));
4801 break;
4802 }
4803
4804 case NestedNameSpecifier::NamespaceAlias: {
4805 // Nested-name-specifier
4806 NamespaceAliasDecl *Alias
4807 = cast<NamespaceAliasDecl>(GetDecl(Record[Idx++]));
4808 NNS = NestedNameSpecifier::Create(*Context, Prev, Alias);
4809
4810 // Location information
4811 SourceRange Range = ReadSourceRange(F, Record, Idx);
4812 unsigned RawStart = Range.getBegin().getRawEncoding();
4813 unsigned RawEnd = Range.getEnd().getRawEncoding();
4814 LocationData.append(reinterpret_cast<char*>(&RawStart),
4815 reinterpret_cast<char*>(&RawStart) +sizeof(unsigned));
4816 LocationData.append(reinterpret_cast<char*>(&RawEnd),
4817 reinterpret_cast<char*>(&RawEnd) + sizeof(unsigned));
4818
4819 break;
4820 }
4821
4822 case NestedNameSpecifier::TypeSpec:
4823 case NestedNameSpecifier::TypeSpecWithTemplate: {
4824 // Nested-name-specifier
4825 bool Template = Record[Idx++];
4826 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
4827 if (!T)
4828 return NestedNameSpecifierLoc();
4829 NNS = NestedNameSpecifier::Create(*Context, Prev, Template,
4830 T->getType().getTypePtr());
4831
4832 // Location information.
4833 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
4834 unsigned RawLocation = ColonColonLoc.getRawEncoding();
4835 void *OpaqueTypeData = T->getTypeLoc().getOpaqueData();
4836 LocationData.append(reinterpret_cast<char*>(&OpaqueTypeData),
4837 (reinterpret_cast<char*>(&OpaqueTypeData)
4838 + sizeof(void *)));
4839 LocationData.append(reinterpret_cast<char*>(&RawLocation),
4840 (reinterpret_cast<char*>(&RawLocation) +
4841 sizeof(unsigned)));
4842 break;
4843 }
4844
4845 case NestedNameSpecifier::Global: {
4846 // Nested-name-specifier
4847 NNS = NestedNameSpecifier::GlobalSpecifier(*Context);
4848
4849 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
4850 unsigned RawLocation = ColonColonLoc.getRawEncoding();
4851 LocationData.append(reinterpret_cast<char*>(&RawLocation),
4852 (reinterpret_cast<char*>(&RawLocation) +
4853 sizeof(unsigned)));
4854 break;
4855 }
4856 }
4857 Prev = NNS;
4858 }
4859
4860 void *Mem = Context->Allocate(LocationData.size(), llvm::alignOf<void*>());
4861 memcpy(Mem, LocationData.data(), LocationData.size());
4862 return NestedNameSpecifierLoc(NNS, Mem);
4863}
4864
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004865SourceRange
Sebastian Redlc3632732010-10-05 15:59:54 +00004866ASTReader::ReadSourceRange(PerFileData &F, const RecordData &Record,
4867 unsigned &Idx) {
4868 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
4869 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar8ee59392010-06-02 15:47:10 +00004870 return SourceRange(beg, end);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004871}
4872
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004873/// \brief Read an integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004874llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004875 unsigned BitWidth = Record[Idx++];
4876 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
4877 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
4878 Idx += NumWords;
4879 return Result;
4880}
4881
4882/// \brief Read a signed integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004883llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004884 bool isUnsigned = Record[Idx++];
4885 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
4886}
4887
Douglas Gregor17fc2232009-04-14 21:55:33 +00004888/// \brief Read a floating-point value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004889llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00004890 return llvm::APFloat(ReadAPInt(Record, Idx));
4891}
4892
Douglas Gregor68a2eb02009-04-15 21:30:51 +00004893// \brief Read a string
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004894std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00004895 unsigned Len = Record[Idx++];
Jay Foadbeaaccd2009-05-21 09:52:38 +00004896 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00004897 Idx += Len;
4898 return Result;
4899}
4900
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004901CXXTemporary *ASTReader::ReadCXXTemporary(const RecordData &Record,
Chris Lattnerd2598362010-05-10 00:25:06 +00004902 unsigned &Idx) {
4903 CXXDestructorDecl *Decl = cast<CXXDestructorDecl>(GetDecl(Record[Idx++]));
4904 return CXXTemporary::Create(*Context, Decl);
4905}
4906
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004907DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00004908 return Diag(SourceLocation(), DiagID);
4909}
4910
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004911DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004912 return Diags.Report(Loc, DiagID);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00004913}
Douglas Gregor025452f2009-04-17 00:04:06 +00004914
Douglas Gregor668c1a42009-04-21 22:25:48 +00004915/// \brief Retrieve the identifier table associated with the
4916/// preprocessor.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004917IdentifierTable &ASTReader::getIdentifierTable() {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00004918 assert(PP && "Forgot to set Preprocessor ?");
4919 return PP->getIdentifierTable();
Douglas Gregor668c1a42009-04-21 22:25:48 +00004920}
4921
Douglas Gregor025452f2009-04-17 00:04:06 +00004922/// \brief Record that the given ID maps to the given switch-case
4923/// statement.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004924void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Douglas Gregor025452f2009-04-17 00:04:06 +00004925 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
4926 SwitchCaseStmts[ID] = SC;
4927}
4928
4929/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004930SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Douglas Gregor025452f2009-04-17 00:04:06 +00004931 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
4932 return SwitchCaseStmts[ID];
4933}
Douglas Gregor1de05fe2009-04-17 18:18:49 +00004934
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00004935void ASTReader::ClearSwitchCaseIDs() {
4936 SwitchCaseStmts.clear();
4937}
4938
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004939void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004940 assert(NumCurrentElementsDeserializing &&
4941 "FinishedDeserializing not paired with StartedDeserializing");
4942 if (NumCurrentElementsDeserializing == 1) {
Douglas Gregord89275b2009-07-06 18:54:52 +00004943 // If any identifiers with corresponding top-level declarations have
4944 // been loaded, load those declarations now.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004945 while (!PendingIdentifierInfos.empty()) {
4946 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
4947 PendingIdentifierInfos.front().DeclIDs, true);
4948 PendingIdentifierInfos.pop_front();
Douglas Gregord89275b2009-07-06 18:54:52 +00004949 }
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00004950
Argyrios Kyrtzidis0895d152011-02-12 07:50:47 +00004951 // Ready to load previous declarations of Decls that were delayed.
4952 while (!PendingPreviousDecls.empty()) {
4953 loadAndAttachPreviousDecl(PendingPreviousDecls.front().first,
4954 PendingPreviousDecls.front().second);
4955 PendingPreviousDecls.pop_front();
4956 }
4957
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00004958 // We are not in recursive loading, so it's safe to pass the "interesting"
4959 // decls to the consumer.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004960 if (Consumer)
4961 PassInterestingDeclsToConsumer();
Argyrios Kyrtzidis134db1f2010-10-24 17:26:31 +00004962
4963 assert(PendingForwardRefs.size() == 0 &&
4964 "Some forward refs did not get linked to the definition!");
Douglas Gregord89275b2009-07-06 18:54:52 +00004965 }
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004966 --NumCurrentElementsDeserializing;
Douglas Gregord89275b2009-07-06 18:54:52 +00004967}
Douglas Gregor501c1032010-08-19 00:28:17 +00004968
Sebastian Redle1dde812010-08-24 00:50:04 +00004969ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context,
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00004970 const char *isysroot, bool DisableValidation,
4971 bool DisableStatCache)
Sebastian Redle1dde812010-08-24 00:50:04 +00004972 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
4973 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
4974 Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context),
4975 Consumer(0), isysroot(isysroot), DisableValidation(DisableValidation),
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00004976 DisableStatCache(DisableStatCache), NumStatHits(0), NumStatMisses(0),
4977 NumSLocEntriesRead(0), TotalNumSLocEntries(0), NextSLocOffset(0),
4978 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
4979 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
4980 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
4981 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
4982 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
4983 NumCurrentElementsDeserializing(0)
4984{
Sebastian Redle1dde812010-08-24 00:50:04 +00004985 RelocatablePCH = false;
4986}
4987
4988ASTReader::ASTReader(SourceManager &SourceMgr, FileManager &FileMgr,
4989 Diagnostic &Diags, const char *isysroot,
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00004990 bool DisableValidation, bool DisableStatCache)
Sebastian Redle1dde812010-08-24 00:50:04 +00004991 : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr),
4992 Diags(Diags), SemaObj(0), PP(0), Context(0), Consumer(0),
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00004993 isysroot(isysroot), DisableValidation(DisableValidation),
4994 DisableStatCache(DisableStatCache), NumStatHits(0), NumStatMisses(0),
4995 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Sebastian Redl8db9fae2010-09-22 20:19:08 +00004996 NextSLocOffset(0), NumStatementsRead(0), TotalNumStatements(0),
4997 NumMacrosRead(0), TotalNumMacros(0), NumSelectorsRead(0),
4998 NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
4999 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
5000 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
5001 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
Sebastian Redle1dde812010-08-24 00:50:04 +00005002 RelocatablePCH = false;
5003}
5004
5005ASTReader::~ASTReader() {
5006 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
5007 delete Chain[e - i - 1];
5008 // Delete all visible decl lookup tables
5009 for (DeclContextOffsetsMap::iterator I = DeclContextOffsets.begin(),
5010 E = DeclContextOffsets.end();
5011 I != E; ++I) {
5012 for (DeclContextInfos::iterator J = I->second.begin(), F = I->second.end();
5013 J != F; ++J) {
5014 if (J->NameLookupTableData)
5015 delete static_cast<ASTDeclContextNameLookupTable*>(
5016 J->NameLookupTableData);
5017 }
5018 }
5019 for (DeclContextVisibleUpdatesPending::iterator
5020 I = PendingVisibleUpdates.begin(),
5021 E = PendingVisibleUpdates.end();
5022 I != E; ++I) {
5023 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
5024 F = I->second.end();
5025 J != F; ++J)
5026 delete static_cast<ASTDeclContextNameLookupTable*>(*J);
5027 }
5028}
5029
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00005030ASTReader::PerFileData::PerFileData(ASTFileType Ty)
5031 : Type(Ty), SizeInBits(0), LocalNumSLocEntries(0), SLocOffsets(0), LocalSLocSize(0),
Sebastian Redl301c9b02010-09-22 00:42:27 +00005032 LocalNumIdentifiers(0), IdentifierOffsets(0), IdentifierTableData(0),
5033 IdentifierLookupTable(0), LocalNumMacroDefinitions(0),
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00005034 MacroDefinitionOffsets(0),
5035 LocalNumHeaderFileInfos(0), HeaderFileInfoTableData(0),
5036 HeaderFileInfoTable(0),
5037 LocalNumSelectors(0), SelectorOffsets(0),
Sebastian Redl301c9b02010-09-22 00:42:27 +00005038 SelectorLookupTableData(0), SelectorLookupTable(0), LocalNumDecls(0),
Douglas Gregor7c789c12010-10-29 22:39:52 +00005039 DeclOffsets(0), LocalNumCXXBaseSpecifiers(0), CXXBaseSpecifiersOffsets(0),
5040 LocalNumTypes(0), TypeOffsets(0), StatCache(0),
Sebastian Redla866e652010-10-01 19:59:12 +00005041 NumPreallocatedPreprocessingEntities(0), NextInSource(0)
Douglas Gregor501c1032010-08-19 00:28:17 +00005042{}
5043
5044ASTReader::PerFileData::~PerFileData() {
5045 delete static_cast<ASTIdentifierLookupTable *>(IdentifierLookupTable);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00005046 delete static_cast<HeaderFileInfoLookupTable *>(HeaderFileInfoTable);
Douglas Gregor501c1032010-08-19 00:28:17 +00005047 delete static_cast<ASTSelectorLookupTable *>(SelectorLookupTable);
5048}