blob: 98dd4c2fd662ac2c75f002f2a48d61e38114b963 [file] [log] [blame]
Sebastian Redl904c9c82010-08-18 23:57:11 +00001//===--- ASTReader.cpp - AST File Reader ------------------------*- C++ -*-===//
Douglas Gregor2cf26342009-04-09 22:27:44 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Sebastian Redlc43b54c2010-08-18 23:56:43 +000010// This file defines the ASTReader class, which reads AST files.
Douglas Gregor2cf26342009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
Chris Lattner4c6f9522009-04-27 05:14:47 +000013
Sebastian Redl6ab7cd82010-08-18 23:57:17 +000014#include "clang/Serialization/ASTReader.h"
15#include "clang/Serialization/ASTDeserializationListener.h"
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +000016#include "ASTCommon.h"
Douglas Gregor0a0428e2009-04-10 20:39:37 +000017#include "clang/Frontend/FrontendDiagnostic.h"
Daniel Dunbarc7162932009-11-11 23:58:53 +000018#include "clang/Frontend/Utils.h"
Douglas Gregore737f502010-08-12 20:07:10 +000019#include "clang/Sema/Sema.h"
John McCall5f1e0942010-08-24 08:50:51 +000020#include "clang/Sema/Scope.h"
Douglas Gregorfdd01722009-04-14 00:24:19 +000021#include "clang/AST/ASTConsumer.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000022#include "clang/AST/ASTContext.h"
John McCall2a7fb272010-08-25 05:32:35 +000023#include "clang/AST/DeclTemplate.h"
Douglas Gregor0b748912009-04-14 21:18:50 +000024#include "clang/AST/Expr.h"
John McCall7a1fad32010-08-24 07:32:53 +000025#include "clang/AST/ExprCXX.h"
Douglas Gregor5f791bb2011-02-28 23:58:31 +000026#include "clang/AST/NestedNameSpecifier.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000027#include "clang/AST/Type.h"
John McCalla1ee0c52009-10-16 21:56:05 +000028#include "clang/AST/TypeLocVisitor.h"
Chris Lattner42d42b52009-04-10 21:41:48 +000029#include "clang/Lex/MacroInfo.h"
Douglas Gregor6a5a23f2010-03-19 21:51:54 +000030#include "clang/Lex/PreprocessingRecord.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000031#include "clang/Lex/Preprocessor.h"
Steve Naroff83d63c72009-04-24 20:03:17 +000032#include "clang/Lex/HeaderSearch.h"
Douglas Gregor668c1a42009-04-21 22:25:48 +000033#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000034#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000035#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000036#include "clang/Basic/FileManager.h"
Chris Lattner10e286a2010-11-23 19:19:34 +000037#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000038#include "clang/Basic/TargetInfo.h"
Douglas Gregor445e23e2009-10-05 21:07:28 +000039#include "clang/Basic/Version.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000040#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000041#include "llvm/Bitcode/BitstreamReader.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000042#include "llvm/Support/MemoryBuffer.h"
John McCall833ca992009-10-29 08:12:44 +000043#include "llvm/Support/ErrorHandling.h"
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000044#include "llvm/Support/FileSystem.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000045#include "llvm/Support/Path.h"
Michael J. Spencer3a321e22010-12-09 17:36:38 +000046#include "llvm/Support/system_error.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000047#include <algorithm>
Douglas Gregore721f952009-04-28 18:58:38 +000048#include <iterator>
Douglas Gregor2cf26342009-04-09 22:27:44 +000049#include <cstdio>
Douglas Gregor4fed3f42009-04-27 18:38:38 +000050#include <sys/stat.h>
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000051
Douglas Gregor2cf26342009-04-09 22:27:44 +000052using namespace clang;
Sebastian Redl8538e8d2010-08-18 23:57:32 +000053using namespace clang::serialization;
Douglas Gregor2cf26342009-04-09 22:27:44 +000054
55//===----------------------------------------------------------------------===//
Sebastian Redl3c7f4132010-08-18 23:57:06 +000056// PCH validator implementation
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000057//===----------------------------------------------------------------------===//
58
Sebastian Redl571db7f2010-08-18 23:56:56 +000059ASTReaderListener::~ASTReaderListener() {}
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000060
61bool
62PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts) {
63 const LangOptions &PPLangOpts = PP.getLangOptions();
64#define PARSE_LANGOPT_BENIGN(Option)
65#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
66 if (PPLangOpts.Option != LangOpts.Option) { \
67 Reader.Diag(DiagID) << LangOpts.Option << PPLangOpts.Option; \
68 return true; \
69 }
70
71 PARSE_LANGOPT_BENIGN(Trigraphs);
72 PARSE_LANGOPT_BENIGN(BCPLComment);
73 PARSE_LANGOPT_BENIGN(DollarIdents);
74 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
75 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
Chandler Carrutheb5d7b72010-04-17 20:17:31 +000076 PARSE_LANGOPT_IMPORTANT(GNUKeywords, diag::warn_pch_gnu_keywords);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000077 PARSE_LANGOPT_BENIGN(ImplicitInt);
78 PARSE_LANGOPT_BENIGN(Digraphs);
79 PARSE_LANGOPT_BENIGN(HexFloats);
80 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
81 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
Michael J. Spencerdae4ac42010-10-21 05:21:48 +000082 PARSE_LANGOPT_BENIGN(MSCVersion);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000083 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
84 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
85 PARSE_LANGOPT_BENIGN(CXXOperatorName);
86 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
87 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
88 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
Fariborz Jahanian412e7982010-02-09 19:31:38 +000089 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI2, diag::warn_pch_nonfragile_abi2);
Fariborz Jahanianf84109e2011-01-07 18:59:25 +000090 PARSE_LANGOPT_IMPORTANT(AppleKext, diag::warn_pch_apple_kext);
Ted Kremenekc32647d2010-12-23 21:35:43 +000091 PARSE_LANGOPT_IMPORTANT(ObjCDefaultSynthProperties,
92 diag::warn_pch_objc_auto_properties);
Michael J. Spencer20249a12010-10-21 03:16:25 +000093 PARSE_LANGOPT_IMPORTANT(NoConstantCFStrings,
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +000094 diag::warn_pch_no_constant_cfstrings);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000095 PARSE_LANGOPT_BENIGN(PascalStrings);
96 PARSE_LANGOPT_BENIGN(WritableStrings);
Mike Stump1eb44332009-09-09 15:08:12 +000097 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000098 diag::warn_pch_lax_vector_conversions);
Nate Begeman69cfb9b2009-06-25 22:57:40 +000099 PARSE_LANGOPT_IMPORTANT(AltiVec, diag::warn_pch_altivec);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000100 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
Anders Carlssonda4b7cf2011-02-19 23:53:54 +0000101 PARSE_LANGOPT_IMPORTANT(ObjCExceptions, diag::warn_pch_objc_exceptions);
Anders Carlsson7da99b02011-02-23 03:04:54 +0000102 PARSE_LANGOPT_IMPORTANT(CXXExceptions, diag::warn_pch_cxx_exceptions);
103 PARSE_LANGOPT_IMPORTANT(SjLjExceptions, diag::warn_pch_sjlj_exceptions);
Douglas Gregor6f755502011-02-01 15:15:22 +0000104 PARSE_LANGOPT_IMPORTANT(MSBitfields, diag::warn_pch_ms_bitfields);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000105 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
106 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
107 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
Mike Stump1eb44332009-09-09 15:08:12 +0000108 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000109 diag::warn_pch_thread_safe_statics);
Daniel Dunbar5345c392009-09-03 04:54:28 +0000110 PARSE_LANGOPT_IMPORTANT(POSIXThreads, diag::warn_pch_posix_threads);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000111 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
112 PARSE_LANGOPT_BENIGN(EmitAllDecls);
113 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
Chris Lattnera4d71452010-06-26 21:25:03 +0000114 PARSE_LANGOPT_BENIGN(getSignedOverflowBehavior());
Mike Stump1eb44332009-09-09 15:08:12 +0000115 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000116 diag::warn_pch_heinous_extensions);
117 // FIXME: Most of the options below are benign if the macro wasn't
118 // used. Unfortunately, this means that a PCH compiled without
119 // optimization can't be used with optimization turned on, even
120 // though the only thing that changes is whether __OPTIMIZE__ was
121 // defined... but if __OPTIMIZE__ never showed up in the header, it
122 // doesn't matter. We could consider making this some special kind
123 // of check.
124 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
125 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
126 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
127 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
128 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
129 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
130 PARSE_LANGOPT_IMPORTANT(AccessControl, diag::warn_pch_access_control);
131 PARSE_LANGOPT_IMPORTANT(CharIsSigned, diag::warn_pch_char_signed);
John Thompsona6fda122009-11-05 20:14:16 +0000132 PARSE_LANGOPT_IMPORTANT(ShortWChar, diag::warn_pch_short_wchar);
Argyrios Kyrtzidis9a2b9d72010-10-08 00:25:19 +0000133 PARSE_LANGOPT_IMPORTANT(ShortEnums, diag::warn_pch_short_enums);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000134 if ((PPLangOpts.getGCMode() != 0) != (LangOpts.getGCMode() != 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000135 Reader.Diag(diag::warn_pch_gc_mode)
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000136 << LangOpts.getGCMode() << PPLangOpts.getGCMode();
137 return true;
138 }
139 PARSE_LANGOPT_BENIGN(getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000140 PARSE_LANGOPT_IMPORTANT(getStackProtectorMode(),
141 diag::warn_pch_stack_protector);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000142 PARSE_LANGOPT_BENIGN(InstantiationDepth);
Nate Begeman69cfb9b2009-06-25 22:57:40 +0000143 PARSE_LANGOPT_IMPORTANT(OpenCL, diag::warn_pch_opencl);
Peter Collingbourne08a53262010-12-01 19:14:57 +0000144 PARSE_LANGOPT_IMPORTANT(CUDA, diag::warn_pch_cuda);
Mike Stump9c276ae2009-12-12 01:27:46 +0000145 PARSE_LANGOPT_BENIGN(CatchUndefined);
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000146 PARSE_LANGOPT_IMPORTANT(ElideConstructors, diag::warn_pch_elide_constructors);
Douglas Gregora0068fc2010-07-09 17:35:33 +0000147 PARSE_LANGOPT_BENIGN(SpellChecking);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000148 PARSE_LANGOPT_BENIGN(DefaultFPContract);
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +0000149#undef PARSE_LANGOPT_IMPORTANT
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000150#undef PARSE_LANGOPT_BENIGN
151
152 return false;
153}
154
Daniel Dunbardc3c0d22009-11-11 00:52:11 +0000155bool PCHValidator::ReadTargetTriple(llvm::StringRef Triple) {
156 if (Triple == PP.getTargetInfo().getTriple().str())
157 return false;
158
159 Reader.Diag(diag::warn_pch_target_triple)
160 << Triple << PP.getTargetInfo().getTriple().str();
161 return true;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000162}
163
Benjamin Kramer54353f42010-11-25 18:29:30 +0000164namespace {
165 struct EmptyStringRef {
166 bool operator ()(llvm::StringRef r) const { return r.empty(); }
167 };
168 struct EmptyBlock {
169 bool operator ()(const PCHPredefinesBlock &r) const {return r.Data.empty();}
170 };
171}
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000172
173static bool EqualConcatenations(llvm::SmallVector<llvm::StringRef, 2> L,
174 PCHPredefinesBlocks R) {
175 // First, sum up the lengths.
176 unsigned LL = 0, RL = 0;
177 for (unsigned I = 0, N = L.size(); I != N; ++I) {
178 LL += L[I].size();
179 }
180 for (unsigned I = 0, N = R.size(); I != N; ++I) {
181 RL += R[I].Data.size();
182 }
183 if (LL != RL)
184 return false;
185 if (LL == 0 && RL == 0)
186 return true;
187
188 // Kick out empty parts, they confuse the algorithm below.
189 L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end());
190 R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end());
191
192 // Do it the hard way. At this point, both vectors must be non-empty.
193 llvm::StringRef LR = L[0], RR = R[0].Data;
194 unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size();
Daniel Dunbarc76c9e02010-07-16 00:00:11 +0000195 (void) RN;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000196 for (;;) {
197 // Compare the current pieces.
198 if (LR.size() == RR.size()) {
199 // If they're the same length, it's pretty easy.
200 if (LR != RR)
201 return false;
202 // Both pieces are done, advance.
203 ++LI;
204 ++RI;
205 // If either string is done, they're both done, since they're the same
206 // length.
207 if (LI == LN) {
208 assert(RI == RN && "Strings not the same length after all?");
209 return true;
210 }
211 LR = L[LI];
212 RR = R[RI].Data;
213 } else if (LR.size() < RR.size()) {
214 // Right piece is longer.
215 if (!RR.startswith(LR))
216 return false;
217 ++LI;
218 assert(LI != LN && "Strings not the same length after all?");
219 RR = RR.substr(LR.size());
220 LR = L[LI];
221 } else {
222 // Left piece is longer.
223 if (!LR.startswith(RR))
224 return false;
225 ++RI;
226 assert(RI != RN && "Strings not the same length after all?");
227 LR = LR.substr(RR.size());
228 RR = R[RI].Data;
229 }
230 }
231}
232
233static std::pair<FileID, llvm::StringRef::size_type>
234FindMacro(const PCHPredefinesBlocks &Buffers, llvm::StringRef MacroDef) {
235 std::pair<FileID, llvm::StringRef::size_type> Res;
236 for (unsigned I = 0, N = Buffers.size(); I != N; ++I) {
237 Res.second = Buffers[I].Data.find(MacroDef);
238 if (Res.second != llvm::StringRef::npos) {
239 Res.first = Buffers[I].BufferID;
240 break;
241 }
242 }
243 return Res;
244}
245
246bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000247 llvm::StringRef OriginalFileName,
Nick Lewycky277a6e72011-02-23 21:16:44 +0000248 std::string &SuggestedPredefines,
249 FileManager &FileMgr) {
Daniel Dunbarc7162932009-11-11 23:58:53 +0000250 // We are in the context of an implicit include, so the predefines buffer will
251 // have a #include entry for the PCH file itself (as normalized by the
252 // preprocessor initialization). Find it and skip over it in the checking
253 // below.
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000254 llvm::SmallString<256> PCHInclude;
255 PCHInclude += "#include \"";
Nick Lewycky277a6e72011-02-23 21:16:44 +0000256 PCHInclude += NormalizeDashIncludePath(OriginalFileName, FileMgr);
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000257 PCHInclude += "\"\n";
258 std::pair<llvm::StringRef,llvm::StringRef> Split =
259 llvm::StringRef(PP.getPredefines()).split(PCHInclude.str());
260 llvm::StringRef Left = Split.first, Right = Split.second;
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000261 if (Left == PP.getPredefines()) {
262 Error("Missing PCH include entry!");
263 return true;
264 }
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000265
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000266 // If the concatenation of all the PCH buffers is equal to the adjusted
267 // command line, we're done.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000268 llvm::SmallVector<llvm::StringRef, 2> CommandLine;
269 CommandLine.push_back(Left);
270 CommandLine.push_back(Right);
271 if (EqualConcatenations(CommandLine, Buffers))
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000272 return false;
273
274 SourceManager &SourceMgr = PP.getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000275
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000276 // The predefines buffers are different. Determine what the differences are,
277 // and whether they require us to reject the PCH file.
Daniel Dunbare6750492009-11-13 16:46:11 +0000278 llvm::SmallVector<llvm::StringRef, 8> PCHLines;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000279 for (unsigned I = 0, N = Buffers.size(); I != N; ++I)
280 Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Daniel Dunbare6750492009-11-13 16:46:11 +0000281
282 llvm::SmallVector<llvm::StringRef, 8> CmdLineLines;
283 Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000284
285 // Pick out implicit #includes after the PCH and don't consider them for
286 // validation; we will insert them into SuggestedPredefines so that the
287 // preprocessor includes them.
288 std::string IncludesAfterPCH;
289 llvm::SmallVector<llvm::StringRef, 8> AfterPCHLines;
290 Right.split(AfterPCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
291 for (unsigned i = 0, e = AfterPCHLines.size(); i != e; ++i) {
292 if (AfterPCHLines[i].startswith("#include ")) {
293 IncludesAfterPCH += AfterPCHLines[i];
294 IncludesAfterPCH += '\n';
295 } else {
296 CmdLineLines.push_back(AfterPCHLines[i]);
297 }
298 }
299
300 // Make sure we add the includes last into SuggestedPredefines before we
301 // exit this function.
302 struct AddIncludesRAII {
303 std::string &SuggestedPredefines;
304 std::string &IncludesAfterPCH;
305
306 AddIncludesRAII(std::string &SuggestedPredefines,
307 std::string &IncludesAfterPCH)
308 : SuggestedPredefines(SuggestedPredefines),
309 IncludesAfterPCH(IncludesAfterPCH) { }
310 ~AddIncludesRAII() {
311 SuggestedPredefines += IncludesAfterPCH;
312 }
313 } AddIncludes(SuggestedPredefines, IncludesAfterPCH);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000314
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000315 // Sort both sets of predefined buffer lines, since we allow some extra
316 // definitions and they may appear at any point in the output.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000317 std::sort(CmdLineLines.begin(), CmdLineLines.end());
318 std::sort(PCHLines.begin(), PCHLines.end());
319
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000320 // Determine which predefines that were used to build the PCH file are missing
321 // from the command line.
322 std::vector<llvm::StringRef> MissingPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000323 std::set_difference(PCHLines.begin(), PCHLines.end(),
324 CmdLineLines.begin(), CmdLineLines.end(),
325 std::back_inserter(MissingPredefines));
326
327 bool MissingDefines = false;
328 bool ConflictingDefines = false;
329 for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) {
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000330 llvm::StringRef Missing = MissingPredefines[I];
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000331 if (Missing.startswith("#include ")) {
332 // An -include was specified when generating the PCH; it is included in
333 // the PCH, just ignore it.
334 continue;
335 }
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000336 if (!Missing.startswith("#define ")) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000337 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
338 return true;
339 }
Mike Stump1eb44332009-09-09 15:08:12 +0000340
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000341 // This is a macro definition. Determine the name of the macro we're
342 // defining.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000343 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000344 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000345 = Missing.find_first_of("( \n\r", StartOfMacroName);
346 assert(EndOfMacroName != std::string::npos &&
347 "Couldn't find the end of the macro name");
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000348 llvm::StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000349
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000350 // Determine whether this macro was given a different definition on the
351 // command line.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000352 std::string MacroDefStart = "#define " + MacroName.str();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000353 std::string::size_type MacroDefLen = MacroDefStart.size();
Daniel Dunbare6750492009-11-13 16:46:11 +0000354 llvm::SmallVector<llvm::StringRef, 8>::iterator ConflictPos
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000355 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
356 MacroDefStart);
357 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000358 if (!ConflictPos->startswith(MacroDefStart)) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000359 // Different macro; we're done.
360 ConflictPos = CmdLineLines.end();
Mike Stump1eb44332009-09-09 15:08:12 +0000361 break;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000362 }
Mike Stump1eb44332009-09-09 15:08:12 +0000363
364 assert(ConflictPos->size() > MacroDefLen &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000365 "Invalid #define in predefines buffer?");
Mike Stump1eb44332009-09-09 15:08:12 +0000366 if ((*ConflictPos)[MacroDefLen] != ' ' &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000367 (*ConflictPos)[MacroDefLen] != '(')
368 continue; // Longer macro name; keep trying.
Mike Stump1eb44332009-09-09 15:08:12 +0000369
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000370 // We found a conflicting macro definition.
371 break;
372 }
Mike Stump1eb44332009-09-09 15:08:12 +0000373
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000374 if (ConflictPos != CmdLineLines.end()) {
375 Reader.Diag(diag::warn_cmdline_conflicting_macro_def)
376 << MacroName;
377
378 // Show the definition of this macro within the PCH file.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000379 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
380 FindMacro(Buffers, Missing);
381 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
382 SourceLocation PCHMissingLoc =
383 SourceMgr.getLocForStartOfFile(MacroLoc.first)
384 .getFileLocWithOffset(MacroLoc.second);
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000385 Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000386
387 ConflictingDefines = true;
388 continue;
389 }
Mike Stump1eb44332009-09-09 15:08:12 +0000390
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000391 // If the macro doesn't conflict, then we'll just pick up the macro
392 // definition from the PCH file. Warn the user that they made a mistake.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000393 if (ConflictingDefines)
394 continue; // Don't complain if there are already conflicting defs
Mike Stump1eb44332009-09-09 15:08:12 +0000395
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000396 if (!MissingDefines) {
397 Reader.Diag(diag::warn_cmdline_missing_macro_defs);
398 MissingDefines = true;
399 }
400
401 // Show the definition of this macro within the PCH file.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000402 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
403 FindMacro(Buffers, Missing);
404 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
405 SourceLocation PCHMissingLoc =
406 SourceMgr.getLocForStartOfFile(MacroLoc.first)
407 .getFileLocWithOffset(MacroLoc.second);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000408 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
409 }
Mike Stump1eb44332009-09-09 15:08:12 +0000410
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000411 if (ConflictingDefines)
412 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000413
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000414 // Determine what predefines were introduced based on command-line
415 // parameters that were not present when building the PCH
416 // file. Extra #defines are okay, so long as the identifiers being
417 // defined were not used within the precompiled header.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000418 std::vector<llvm::StringRef> ExtraPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000419 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
420 PCHLines.begin(), PCHLines.end(),
Mike Stump1eb44332009-09-09 15:08:12 +0000421 std::back_inserter(ExtraPredefines));
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000422 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000423 llvm::StringRef &Extra = ExtraPredefines[I];
424 if (!Extra.startswith("#define ")) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000425 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
426 return true;
427 }
428
429 // This is an extra macro definition. Determine the name of the
430 // macro we're defining.
431 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000432 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000433 = Extra.find_first_of("( \n\r", StartOfMacroName);
434 assert(EndOfMacroName != std::string::npos &&
435 "Couldn't find the end of the macro name");
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000436 llvm::StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000437
438 // Check whether this name was used somewhere in the PCH file. If
439 // so, defining it as a macro could change behavior, so we reject
440 // the PCH file.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000441 if (IdentifierInfo *II = Reader.get(MacroName)) {
Daniel Dunbar4fda42e2009-11-11 00:52:00 +0000442 Reader.Diag(diag::warn_macro_name_used_in_pch) << II;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000443 return true;
444 }
445
446 // Add this definition to the suggested predefines buffer.
447 SuggestedPredefines += Extra;
448 SuggestedPredefines += '\n';
449 }
450
451 // If we get here, it's because the predefines buffer had compatible
452 // contents. Accept the PCH file.
453 return false;
454}
455
Douglas Gregor12fab312010-03-16 16:35:32 +0000456void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
457 unsigned ID) {
458 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
459 ++NumHeaderInfos;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000460}
461
462void PCHValidator::ReadCounter(unsigned Value) {
463 PP.setCounterValue(Value);
464}
465
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000466//===----------------------------------------------------------------------===//
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000467// AST reader implementation
Douglas Gregor668c1a42009-04-21 22:25:48 +0000468//===----------------------------------------------------------------------===//
469
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000470void
Sebastian Redl571db7f2010-08-18 23:56:56 +0000471ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000472 DeserializationListener = Listener;
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000473}
474
Chris Lattner4c6f9522009-04-27 05:14:47 +0000475
Douglas Gregor668c1a42009-04-21 22:25:48 +0000476namespace {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000477class ASTSelectorLookupTrait {
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000478 ASTReader &Reader;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000479
480public:
Sebastian Redl5d050072010-08-04 17:20:04 +0000481 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000482 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +0000483 ObjCMethodList Instance, Factory;
484 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000485
486 typedef Selector external_key_type;
487 typedef external_key_type internal_key_type;
488
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000489 explicit ASTSelectorLookupTrait(ASTReader &Reader) : Reader(Reader) { }
Mike Stump1eb44332009-09-09 15:08:12 +0000490
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000491 static bool EqualKey(const internal_key_type& a,
492 const internal_key_type& b) {
493 return a == b;
494 }
Mike Stump1eb44332009-09-09 15:08:12 +0000495
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000496 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +0000497 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000498 }
Mike Stump1eb44332009-09-09 15:08:12 +0000499
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000500 // This hopefully will just get inlined and removed by the optimizer.
501 static const internal_key_type&
502 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump1eb44332009-09-09 15:08:12 +0000503
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000504 static std::pair<unsigned, unsigned>
505 ReadKeyDataLength(const unsigned char*& d) {
506 using namespace clang::io;
507 unsigned KeyLen = ReadUnalignedLE16(d);
508 unsigned DataLen = ReadUnalignedLE16(d);
509 return std::make_pair(KeyLen, DataLen);
510 }
Mike Stump1eb44332009-09-09 15:08:12 +0000511
Douglas Gregor83941df2009-04-25 17:48:32 +0000512 internal_key_type ReadKey(const unsigned char* d, unsigned) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000513 using namespace clang::io;
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000514 SelectorTable &SelTable = Reader.getContext()->Selectors;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000515 unsigned N = ReadUnalignedLE16(d);
Mike Stump1eb44332009-09-09 15:08:12 +0000516 IdentifierInfo *FirstII
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000517 = Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
518 if (N == 0)
519 return SelTable.getNullarySelector(FirstII);
520 else if (N == 1)
521 return SelTable.getUnarySelector(FirstII);
522
523 llvm::SmallVector<IdentifierInfo *, 16> Args;
524 Args.push_back(FirstII);
525 for (unsigned I = 1; I != N; ++I)
526 Args.push_back(Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)));
527
Douglas Gregor75fdb232009-05-22 22:45:36 +0000528 return SelTable.getSelector(N, Args.data());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000529 }
Mike Stump1eb44332009-09-09 15:08:12 +0000530
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000531 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen) {
532 using namespace clang::io;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000533
534 data_type Result;
535
Sebastian Redl5d050072010-08-04 17:20:04 +0000536 Result.ID = ReadUnalignedLE32(d);
537 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
538 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
539
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000540 // Load instance methods
541 ObjCMethodList *Prev = 0;
542 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000543 ObjCMethodDecl *Method
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000544 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl5d050072010-08-04 17:20:04 +0000545 if (!Result.Instance.Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000546 // This is the first method, which is the easy case.
Sebastian Redl5d050072010-08-04 17:20:04 +0000547 Result.Instance.Method = Method;
548 Prev = &Result.Instance;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000549 continue;
550 }
551
Ted Kremenek298ed872010-02-11 00:53:01 +0000552 ObjCMethodList *Mem =
553 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
554 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000555 Prev = Prev->Next;
556 }
557
558 // Load factory methods
559 Prev = 0;
560 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000561 ObjCMethodDecl *Method
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000562 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl5d050072010-08-04 17:20:04 +0000563 if (!Result.Factory.Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000564 // This is the first method, which is the easy case.
Sebastian Redl5d050072010-08-04 17:20:04 +0000565 Result.Factory.Method = Method;
566 Prev = &Result.Factory;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000567 continue;
568 }
569
Ted Kremenek298ed872010-02-11 00:53:01 +0000570 ObjCMethodList *Mem =
571 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
572 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000573 Prev = Prev->Next;
574 }
575
576 return Result;
577 }
578};
Mike Stump1eb44332009-09-09 15:08:12 +0000579
580} // end anonymous namespace
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000581
582/// \brief The on-disk hash table used for the global method pool.
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000583typedef OnDiskChainedHashTable<ASTSelectorLookupTrait>
584 ASTSelectorLookupTable;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000585
Sebastian Redlc3632732010-10-05 15:59:54 +0000586namespace clang {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000587class ASTIdentifierLookupTrait {
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000588 ASTReader &Reader;
Sebastian Redlc3632732010-10-05 15:59:54 +0000589 ASTReader::PerFileData &F;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000590
591 // If we know the IdentifierInfo in advance, it is here and we will
592 // not build a new one. Used when deserializing information about an
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000593 // identifier that was constructed before the AST file was read.
Douglas Gregor668c1a42009-04-21 22:25:48 +0000594 IdentifierInfo *KnownII;
595
596public:
597 typedef IdentifierInfo * data_type;
598
599 typedef const std::pair<const char*, unsigned> external_key_type;
600
601 typedef external_key_type internal_key_type;
602
Sebastian Redlc3632732010-10-05 15:59:54 +0000603 ASTIdentifierLookupTrait(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redld27d3fc2010-07-21 22:31:37 +0000604 IdentifierInfo *II = 0)
Sebastian Redlc3632732010-10-05 15:59:54 +0000605 : Reader(Reader), F(F), KnownII(II) { }
Mike Stump1eb44332009-09-09 15:08:12 +0000606
Douglas Gregor668c1a42009-04-21 22:25:48 +0000607 static bool EqualKey(const internal_key_type& a,
608 const internal_key_type& b) {
609 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
610 : false;
611 }
Mike Stump1eb44332009-09-09 15:08:12 +0000612
Douglas Gregor668c1a42009-04-21 22:25:48 +0000613 static unsigned ComputeHash(const internal_key_type& a) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000614 return llvm::HashString(llvm::StringRef(a.first, a.second));
Douglas Gregor668c1a42009-04-21 22:25:48 +0000615 }
Mike Stump1eb44332009-09-09 15:08:12 +0000616
Douglas Gregor668c1a42009-04-21 22:25:48 +0000617 // This hopefully will just get inlined and removed by the optimizer.
618 static const internal_key_type&
619 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump1eb44332009-09-09 15:08:12 +0000620
Douglas Gregor95f42922010-10-14 22:11:03 +0000621 // This hopefully will just get inlined and removed by the optimizer.
622 static const external_key_type&
623 GetExternalKey(const internal_key_type& x) { return x; }
624
Douglas Gregor668c1a42009-04-21 22:25:48 +0000625 static std::pair<unsigned, unsigned>
626 ReadKeyDataLength(const unsigned char*& d) {
627 using namespace clang::io;
Douglas Gregor5f8e3302009-04-25 20:26:24 +0000628 unsigned DataLen = ReadUnalignedLE16(d);
Douglas Gregord6595a42009-04-25 21:04:17 +0000629 unsigned KeyLen = ReadUnalignedLE16(d);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000630 return std::make_pair(KeyLen, DataLen);
631 }
Mike Stump1eb44332009-09-09 15:08:12 +0000632
Douglas Gregor668c1a42009-04-21 22:25:48 +0000633 static std::pair<const char*, unsigned>
634 ReadKey(const unsigned char* d, unsigned n) {
635 assert(n >= 2 && d[n-1] == '\0');
636 return std::make_pair((const char*) d, n-1);
637 }
Mike Stump1eb44332009-09-09 15:08:12 +0000638
639 IdentifierInfo *ReadData(const internal_key_type& k,
Douglas Gregor668c1a42009-04-21 22:25:48 +0000640 const unsigned char* d,
641 unsigned DataLen) {
642 using namespace clang::io;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000643 IdentID ID = ReadUnalignedLE32(d);
Douglas Gregora92193e2009-04-28 21:18:29 +0000644 bool IsInteresting = ID & 0x01;
645
646 // Wipe out the "is interesting" bit.
647 ID = ID >> 1;
648
649 if (!IsInteresting) {
Sebastian Redl083abdf2010-07-27 23:01:28 +0000650 // For uninteresting identifiers, just build the IdentifierInfo
Douglas Gregora92193e2009-04-28 21:18:29 +0000651 // and associate it with the persistent ID.
652 IdentifierInfo *II = KnownII;
653 if (!II)
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000654 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregora92193e2009-04-28 21:18:29 +0000655 Reader.SetIdentifierInfo(ID, II);
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000656 II->setIsFromAST();
Douglas Gregora92193e2009-04-28 21:18:29 +0000657 return II;
658 }
659
Douglas Gregor5998da52009-04-28 21:32:13 +0000660 unsigned Bits = ReadUnalignedLE16(d);
Douglas Gregor2deaea32009-04-22 18:49:13 +0000661 bool CPlusPlusOperatorKeyword = Bits & 0x01;
662 Bits >>= 1;
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000663 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
664 Bits >>= 1;
Douglas Gregor2deaea32009-04-22 18:49:13 +0000665 bool Poisoned = Bits & 0x01;
666 Bits >>= 1;
667 bool ExtensionToken = Bits & 0x01;
668 Bits >>= 1;
669 bool hasMacroDefinition = Bits & 0x01;
670 Bits >>= 1;
671 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
672 Bits >>= 10;
Mike Stump1eb44332009-09-09 15:08:12 +0000673
Douglas Gregor2deaea32009-04-22 18:49:13 +0000674 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregor5998da52009-04-28 21:32:13 +0000675 DataLen -= 6;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000676
677 // Build the IdentifierInfo itself and link the identifier ID with
678 // the new IdentifierInfo.
679 IdentifierInfo *II = KnownII;
680 if (!II)
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000681 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000682 Reader.SetIdentifierInfo(ID, II);
683
Douglas Gregor2deaea32009-04-22 18:49:13 +0000684 // Set or check the various bits in the IdentifierInfo structure.
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000685 // Token IDs are read-only.
686 if (HasRevertedTokenIDToIdentifier)
687 II->RevertTokenIDToIdentifier();
Douglas Gregor2deaea32009-04-22 18:49:13 +0000688 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
Mike Stump1eb44332009-09-09 15:08:12 +0000689 assert(II->isExtensionToken() == ExtensionToken &&
Douglas Gregor2deaea32009-04-22 18:49:13 +0000690 "Incorrect extension token flag");
691 (void)ExtensionToken;
692 II->setIsPoisoned(Poisoned);
693 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
694 "Incorrect C++ operator keyword flag");
695 (void)CPlusPlusOperatorKeyword;
696
Douglas Gregor37e26842009-04-21 23:56:24 +0000697 // If this identifier is a macro, deserialize the macro
698 // definition.
699 if (hasMacroDefinition) {
Douglas Gregor5998da52009-04-28 21:32:13 +0000700 uint32_t Offset = ReadUnalignedLE32(d);
Douglas Gregor295a2a62010-10-30 00:23:06 +0000701 Reader.SetIdentifierIsMacro(II, F, Offset);
Douglas Gregor5998da52009-04-28 21:32:13 +0000702 DataLen -= 4;
Douglas Gregor37e26842009-04-21 23:56:24 +0000703 }
Douglas Gregor668c1a42009-04-21 22:25:48 +0000704
705 // Read all of the declarations visible at global scope with this
706 // name.
Chris Lattner6bf690f2009-04-27 22:17:41 +0000707 if (Reader.getContext() == 0) return II;
Douglas Gregord89275b2009-07-06 18:54:52 +0000708 if (DataLen > 0) {
709 llvm::SmallVector<uint32_t, 4> DeclIDs;
710 for (; DataLen > 0; DataLen -= 4)
711 DeclIDs.push_back(ReadUnalignedLE32(d));
712 Reader.SetGloballyVisibleDecls(II, DeclIDs);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000713 }
Mike Stump1eb44332009-09-09 15:08:12 +0000714
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000715 II->setIsFromAST();
Douglas Gregor668c1a42009-04-21 22:25:48 +0000716 return II;
717 }
718};
Mike Stump1eb44332009-09-09 15:08:12 +0000719
720} // end anonymous namespace
Douglas Gregor668c1a42009-04-21 22:25:48 +0000721
722/// \brief The on-disk hash table used to contain information about
723/// all of the identifiers in the program.
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000724typedef OnDiskChainedHashTable<ASTIdentifierLookupTrait>
725 ASTIdentifierLookupTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000726
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000727namespace {
728class ASTDeclContextNameLookupTrait {
729 ASTReader &Reader;
730
731public:
732 /// \brief Pair of begin/end iterators for DeclIDs.
733 typedef std::pair<DeclID *, DeclID *> data_type;
734
735 /// \brief Special internal key for declaration names.
736 /// The hash table creates keys for comparison; we do not create
737 /// a DeclarationName for the internal key to avoid deserializing types.
738 struct DeclNameKey {
739 DeclarationName::NameKind Kind;
740 uint64_t Data;
741 DeclNameKey() : Kind((DeclarationName::NameKind)0), Data(0) { }
742 };
743
744 typedef DeclarationName external_key_type;
745 typedef DeclNameKey internal_key_type;
746
747 explicit ASTDeclContextNameLookupTrait(ASTReader &Reader) : Reader(Reader) { }
748
749 static bool EqualKey(const internal_key_type& a,
750 const internal_key_type& b) {
751 return a.Kind == b.Kind && a.Data == b.Data;
752 }
753
754 unsigned ComputeHash(const DeclNameKey &Key) const {
755 llvm::FoldingSetNodeID ID;
756 ID.AddInteger(Key.Kind);
757
758 switch (Key.Kind) {
759 case DeclarationName::Identifier:
760 case DeclarationName::CXXLiteralOperatorName:
761 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
762 break;
763 case DeclarationName::ObjCZeroArgSelector:
764 case DeclarationName::ObjCOneArgSelector:
765 case DeclarationName::ObjCMultiArgSelector:
766 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
767 break;
768 case DeclarationName::CXXConstructorName:
769 case DeclarationName::CXXDestructorName:
770 case DeclarationName::CXXConversionFunctionName:
771 ID.AddInteger((TypeID)Key.Data);
772 break;
773 case DeclarationName::CXXOperatorName:
774 ID.AddInteger((OverloadedOperatorKind)Key.Data);
775 break;
776 case DeclarationName::CXXUsingDirective:
777 break;
778 }
779
780 return ID.ComputeHash();
781 }
782
783 internal_key_type GetInternalKey(const external_key_type& Name) const {
784 DeclNameKey Key;
785 Key.Kind = Name.getNameKind();
786 switch (Name.getNameKind()) {
787 case DeclarationName::Identifier:
788 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
789 break;
790 case DeclarationName::ObjCZeroArgSelector:
791 case DeclarationName::ObjCOneArgSelector:
792 case DeclarationName::ObjCMultiArgSelector:
793 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
794 break;
795 case DeclarationName::CXXConstructorName:
796 case DeclarationName::CXXDestructorName:
797 case DeclarationName::CXXConversionFunctionName:
798 Key.Data = Reader.GetTypeID(Name.getCXXNameType());
799 break;
800 case DeclarationName::CXXOperatorName:
801 Key.Data = Name.getCXXOverloadedOperator();
802 break;
803 case DeclarationName::CXXLiteralOperatorName:
804 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
805 break;
806 case DeclarationName::CXXUsingDirective:
807 break;
808 }
Michael J. Spencer20249a12010-10-21 03:16:25 +0000809
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000810 return Key;
811 }
812
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +0000813 external_key_type GetExternalKey(const internal_key_type& Key) const {
814 ASTContext *Context = Reader.getContext();
815 switch (Key.Kind) {
816 case DeclarationName::Identifier:
817 return DeclarationName((IdentifierInfo*)Key.Data);
818
819 case DeclarationName::ObjCZeroArgSelector:
820 case DeclarationName::ObjCOneArgSelector:
821 case DeclarationName::ObjCMultiArgSelector:
822 return DeclarationName(Selector(Key.Data));
823
824 case DeclarationName::CXXConstructorName:
825 return Context->DeclarationNames.getCXXConstructorName(
826 Context->getCanonicalType(Reader.GetType(Key.Data)));
827
828 case DeclarationName::CXXDestructorName:
829 return Context->DeclarationNames.getCXXDestructorName(
830 Context->getCanonicalType(Reader.GetType(Key.Data)));
831
832 case DeclarationName::CXXConversionFunctionName:
833 return Context->DeclarationNames.getCXXConversionFunctionName(
834 Context->getCanonicalType(Reader.GetType(Key.Data)));
835
836 case DeclarationName::CXXOperatorName:
837 return Context->DeclarationNames.getCXXOperatorName(
838 (OverloadedOperatorKind)Key.Data);
839
840 case DeclarationName::CXXLiteralOperatorName:
841 return Context->DeclarationNames.getCXXLiteralOperatorName(
842 (IdentifierInfo*)Key.Data);
843
844 case DeclarationName::CXXUsingDirective:
845 return DeclarationName::getUsingDirectiveName();
846 }
847
848 llvm_unreachable("Invalid Name Kind ?");
849 }
850
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000851 static std::pair<unsigned, unsigned>
852 ReadKeyDataLength(const unsigned char*& d) {
853 using namespace clang::io;
854 unsigned KeyLen = ReadUnalignedLE16(d);
855 unsigned DataLen = ReadUnalignedLE16(d);
856 return std::make_pair(KeyLen, DataLen);
857 }
858
859 internal_key_type ReadKey(const unsigned char* d, unsigned) {
860 using namespace clang::io;
861
862 DeclNameKey Key;
863 Key.Kind = (DeclarationName::NameKind)*d++;
864 switch (Key.Kind) {
865 case DeclarationName::Identifier:
866 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
867 break;
868 case DeclarationName::ObjCZeroArgSelector:
869 case DeclarationName::ObjCOneArgSelector:
870 case DeclarationName::ObjCMultiArgSelector:
Michael J. Spencer20249a12010-10-21 03:16:25 +0000871 Key.Data =
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000872 (uint64_t)Reader.DecodeSelector(ReadUnalignedLE32(d)).getAsOpaquePtr();
873 break;
874 case DeclarationName::CXXConstructorName:
875 case DeclarationName::CXXDestructorName:
876 case DeclarationName::CXXConversionFunctionName:
877 Key.Data = ReadUnalignedLE32(d); // TypeID
878 break;
879 case DeclarationName::CXXOperatorName:
880 Key.Data = *d++; // OverloadedOperatorKind
881 break;
882 case DeclarationName::CXXLiteralOperatorName:
883 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
884 break;
885 case DeclarationName::CXXUsingDirective:
886 break;
887 }
Michael J. Spencer20249a12010-10-21 03:16:25 +0000888
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000889 return Key;
890 }
891
892 data_type ReadData(internal_key_type, const unsigned char* d,
893 unsigned DataLen) {
894 using namespace clang::io;
895 unsigned NumDecls = ReadUnalignedLE16(d);
896 DeclID *Start = (DeclID *)d;
897 return std::make_pair(Start, Start + NumDecls);
898 }
899};
900
901} // end anonymous namespace
902
903/// \brief The on-disk hash table used for the DeclContext's Name lookup table.
904typedef OnDiskChainedHashTable<ASTDeclContextNameLookupTrait>
905 ASTDeclContextNameLookupTable;
906
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000907bool ASTReader::ReadDeclContextStorage(llvm::BitstreamCursor &Cursor,
908 const std::pair<uint64_t, uint64_t> &Offsets,
909 DeclContextInfo &Info) {
910 SavedStreamPosition SavedPosition(Cursor);
911 // First the lexical decls.
912 if (Offsets.first != 0) {
913 Cursor.JumpToBit(Offsets.first);
914
915 RecordData Record;
916 const char *Blob;
917 unsigned BlobLen;
918 unsigned Code = Cursor.ReadCode();
919 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
920 if (RecCode != DECL_CONTEXT_LEXICAL) {
921 Error("Expected lexical block");
922 return true;
923 }
924
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000925 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob);
926 Info.NumLexicalDecls = BlobLen / sizeof(KindDeclIDPair);
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000927 } else {
928 Info.LexicalDecls = 0;
929 Info.NumLexicalDecls = 0;
930 }
931
932 // Now the lookup table.
933 if (Offsets.second != 0) {
934 Cursor.JumpToBit(Offsets.second);
935
936 RecordData Record;
937 const char *Blob;
938 unsigned BlobLen;
939 unsigned Code = Cursor.ReadCode();
940 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
941 if (RecCode != DECL_CONTEXT_VISIBLE) {
942 Error("Expected visible lookup table block");
943 return true;
944 }
945 Info.NameLookupTableData
946 = ASTDeclContextNameLookupTable::Create(
947 (const unsigned char *)Blob + Record[0],
948 (const unsigned char *)Blob,
949 ASTDeclContextNameLookupTrait(*this));
Sebastian Redl0ea8f7f2010-08-24 00:50:00 +0000950 } else {
951 Info.NameLookupTableData = 0;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000952 }
953
954 return false;
955}
956
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000957void ASTReader::Error(const char *Msg) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000958 Diag(diag::err_fe_pch_malformed) << Msg;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000959}
960
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000961/// \brief Tell the AST listener about the predefines buffers in the chain.
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000962bool ASTReader::CheckPredefinesBuffers() {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000963 if (Listener)
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000964 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000965 ActualOriginalFileName,
Nick Lewycky277a6e72011-02-23 21:16:44 +0000966 SuggestedPredefines,
967 FileMgr);
Douglas Gregore721f952009-04-28 18:58:38 +0000968 return false;
Douglas Gregore1d918e2009-04-10 23:10:45 +0000969}
970
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000971//===----------------------------------------------------------------------===//
972// Source Manager Deserialization
973//===----------------------------------------------------------------------===//
974
Douglas Gregorbd945002009-04-13 16:31:14 +0000975/// \brief Read the line table in the source manager block.
Sebastian Redlc3632732010-10-05 15:59:54 +0000976/// \returns true if there was an error.
977bool ASTReader::ParseLineTable(PerFileData &F,
978 llvm::SmallVectorImpl<uint64_t> &Record) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000979 unsigned Idx = 0;
980 LineTableInfo &LineTable = SourceMgr.getLineTable();
981
982 // Parse the file names
Douglas Gregorff0a9872009-04-13 17:12:42 +0000983 std::map<int, int> FileIDs;
984 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000985 // Extract the file name
986 unsigned FilenameLen = Record[Idx++];
987 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
988 Idx += FilenameLen;
Douglas Gregore650c8c2009-07-07 00:12:59 +0000989 MaybeAddSystemRootToFilename(Filename);
Mike Stump1eb44332009-09-09 15:08:12 +0000990 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
Douglas Gregorff0a9872009-04-13 17:12:42 +0000991 Filename.size());
Douglas Gregorbd945002009-04-13 16:31:14 +0000992 }
993
994 // Parse the line entries
995 std::vector<LineEntry> Entries;
996 while (Idx < Record.size()) {
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000997 int FID = Record[Idx++];
Douglas Gregorbd945002009-04-13 16:31:14 +0000998
999 // Extract the line entries
1000 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +00001001 assert(NumEntries && "Numentries is 00000");
Douglas Gregorbd945002009-04-13 16:31:14 +00001002 Entries.clear();
1003 Entries.reserve(NumEntries);
1004 for (unsigned I = 0; I != NumEntries; ++I) {
1005 unsigned FileOffset = Record[Idx++];
1006 unsigned LineNo = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +00001007 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump1eb44332009-09-09 15:08:12 +00001008 SrcMgr::CharacteristicKind FileKind
Douglas Gregorbd945002009-04-13 16:31:14 +00001009 = (SrcMgr::CharacteristicKind)Record[Idx++];
1010 unsigned IncludeOffset = Record[Idx++];
1011 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1012 FileKind, IncludeOffset));
1013 }
1014 LineTable.AddEntry(FID, Entries);
1015 }
1016
1017 return false;
1018}
1019
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001020namespace {
1021
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001022class ASTStatData {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001023public:
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001024 const ino_t ino;
1025 const dev_t dev;
1026 const mode_t mode;
1027 const time_t mtime;
1028 const off_t size;
Mike Stump1eb44332009-09-09 15:08:12 +00001029
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001030 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Chris Lattner74e976b2010-11-23 19:28:12 +00001031 : ino(i), dev(d), mode(mo), mtime(m), size(s) {}
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001032};
1033
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001034class ASTStatLookupTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001035 public:
1036 typedef const char *external_key_type;
1037 typedef const char *internal_key_type;
1038
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001039 typedef ASTStatData data_type;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001040
1041 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001042 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001043 }
1044
1045 static internal_key_type GetInternalKey(const char *path) { return path; }
1046
1047 static bool EqualKey(internal_key_type a, internal_key_type b) {
1048 return strcmp(a, b) == 0;
1049 }
1050
1051 static std::pair<unsigned, unsigned>
1052 ReadKeyDataLength(const unsigned char*& d) {
1053 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1054 unsigned DataLen = (unsigned) *d++;
1055 return std::make_pair(KeyLen + 1, DataLen);
1056 }
1057
1058 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
1059 return (const char *)d;
1060 }
1061
1062 static data_type ReadData(const internal_key_type, const unsigned char *d,
1063 unsigned /*DataLen*/) {
1064 using namespace clang::io;
1065
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001066 ino_t ino = (ino_t) ReadUnalignedLE32(d);
1067 dev_t dev = (dev_t) ReadUnalignedLE32(d);
1068 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump1eb44332009-09-09 15:08:12 +00001069 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001070 off_t size = (off_t) ReadUnalignedLE64(d);
1071 return data_type(ino, dev, mode, mtime, size);
1072 }
1073};
1074
1075/// \brief stat() cache for precompiled headers.
1076///
1077/// This cache is very similar to the stat cache used by pretokenized
1078/// headers.
Chris Lattner10e286a2010-11-23 19:19:34 +00001079class ASTStatCache : public FileSystemStatCache {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001080 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001081 CacheTy *Cache;
1082
1083 unsigned &NumStatHits, &NumStatMisses;
Mike Stump1eb44332009-09-09 15:08:12 +00001084public:
Chris Lattner74e976b2010-11-23 19:28:12 +00001085 ASTStatCache(const unsigned char *Buckets, const unsigned char *Base,
1086 unsigned &NumStatHits, unsigned &NumStatMisses)
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001087 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
1088 Cache = CacheTy::Create(Buckets, Base);
1089 }
1090
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001091 ~ASTStatCache() { delete Cache; }
Mike Stump1eb44332009-09-09 15:08:12 +00001092
Chris Lattner898a0612010-11-23 21:17:56 +00001093 LookupResult getStat(const char *Path, struct stat &StatBuf,
1094 int *FileDescriptor) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001095 // Do the lookup for the file's data in the AST file.
Chris Lattner10e286a2010-11-23 19:19:34 +00001096 CacheTy::iterator I = Cache->find(Path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001097
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001098 // If we don't get a hit in the AST file just forward to 'stat'.
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001099 if (I == Cache->end()) {
1100 ++NumStatMisses;
Chris Lattner898a0612010-11-23 21:17:56 +00001101 return statChained(Path, StatBuf, FileDescriptor);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001102 }
Mike Stump1eb44332009-09-09 15:08:12 +00001103
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001104 ++NumStatHits;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001105 ASTStatData Data = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001106
Chris Lattner10e286a2010-11-23 19:19:34 +00001107 StatBuf.st_ino = Data.ino;
1108 StatBuf.st_dev = Data.dev;
1109 StatBuf.st_mtime = Data.mtime;
1110 StatBuf.st_mode = Data.mode;
1111 StatBuf.st_size = Data.size;
Chris Lattnerd6f61112010-11-23 20:05:15 +00001112 return CacheExists;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001113 }
1114};
1115} // end anonymous namespace
1116
1117
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001118/// \brief Read a source manager block
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001119ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(PerFileData &F) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001120 using namespace SrcMgr;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001121
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001122 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001123
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001124 // Set the source-location entry cursor to the current position in
1125 // the stream. This cursor will be used to read the contents of the
1126 // source manager block initially, and then lazily read
1127 // source-location entries as needed.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001128 SLocEntryCursor = F.Stream;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001129
1130 // The stream itself is going to skip over the source manager block.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001131 if (F.Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001132 Error("malformed block record in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001133 return Failure;
1134 }
1135
1136 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001137 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001138 Error("malformed source manager block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001139 return Failure;
1140 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001141
Douglas Gregor14f79002009-04-10 03:52:48 +00001142 RecordData Record;
1143 while (true) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001144 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregor14f79002009-04-10 03:52:48 +00001145 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001146 if (SLocEntryCursor.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001147 Error("error at end of Source Manager block in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001148 return Failure;
1149 }
Douglas Gregore1d918e2009-04-10 23:10:45 +00001150 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +00001151 }
Mike Stump1eb44332009-09-09 15:08:12 +00001152
Douglas Gregor14f79002009-04-10 03:52:48 +00001153 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1154 // No known subblocks, always skip them.
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001155 SLocEntryCursor.ReadSubBlockID();
1156 if (SLocEntryCursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001157 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001158 return Failure;
1159 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001160 continue;
1161 }
Mike Stump1eb44332009-09-09 15:08:12 +00001162
Douglas Gregor14f79002009-04-10 03:52:48 +00001163 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001164 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregor14f79002009-04-10 03:52:48 +00001165 continue;
1166 }
Mike Stump1eb44332009-09-09 15:08:12 +00001167
Douglas Gregor14f79002009-04-10 03:52:48 +00001168 // Read a record.
1169 const char *BlobStart;
1170 unsigned BlobLen;
1171 Record.clear();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001172 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001173 default: // Default behavior: ignore.
1174 break;
1175
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001176 case SM_LINE_TABLE:
Sebastian Redlc3632732010-10-05 15:59:54 +00001177 if (ParseLineTable(F, Record))
Douglas Gregorbd945002009-04-13 16:31:14 +00001178 return Failure;
Chris Lattner2c78b872009-04-14 23:22:57 +00001179 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001180
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001181 case SM_SLOC_FILE_ENTRY:
1182 case SM_SLOC_BUFFER_ENTRY:
1183 case SM_SLOC_INSTANTIATION_ENTRY:
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001184 // Once we hit one of the source location entries, we're done.
1185 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +00001186 }
1187 }
1188}
1189
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001190/// \brief If a header file is not found at the path that we expect it to be
1191/// and the PCH file was moved from its original location, try to resolve the
1192/// file by assuming that header+PCH were moved together and the header is in
1193/// the same place relative to the PCH.
1194static std::string
1195resolveFileRelativeToOriginalDir(const std::string &Filename,
1196 const std::string &OriginalDir,
1197 const std::string &CurrDir) {
1198 assert(OriginalDir != CurrDir &&
1199 "No point trying to resolve the file if the PCH dir didn't change");
1200 using namespace llvm::sys;
1201 llvm::SmallString<128> filePath(Filename);
1202 fs::make_absolute(filePath);
1203 assert(path::is_absolute(OriginalDir));
1204 llvm::SmallString<128> currPCHPath(CurrDir);
1205
1206 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1207 fileDirE = path::end(path::parent_path(filePath));
1208 path::const_iterator origDirI = path::begin(OriginalDir),
1209 origDirE = path::end(OriginalDir);
1210 // Skip the common path components from filePath and OriginalDir.
1211 while (fileDirI != fileDirE && origDirI != origDirE &&
1212 *fileDirI == *origDirI) {
1213 ++fileDirI;
1214 ++origDirI;
1215 }
1216 for (; origDirI != origDirE; ++origDirI)
1217 path::append(currPCHPath, "..");
1218 path::append(currPCHPath, fileDirI, fileDirE);
1219 path::append(currPCHPath, path::filename(Filename));
1220 return currPCHPath.str();
1221}
1222
Sebastian Redl190faf72010-07-20 21:50:20 +00001223/// \brief Get a cursor that's correctly positioned for reading the source
1224/// location entry with the given ID.
Sebastian Redlc3632732010-10-05 15:59:54 +00001225ASTReader::PerFileData *ASTReader::SLocCursorForID(unsigned ID) {
Sebastian Redl190faf72010-07-20 21:50:20 +00001226 assert(ID != 0 && ID <= TotalNumSLocEntries &&
1227 "SLocCursorForID should only be called for real IDs.");
1228
1229 ID -= 1;
1230 PerFileData *F = 0;
1231 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1232 F = Chain[N - I - 1];
1233 if (ID < F->LocalNumSLocEntries)
1234 break;
1235 ID -= F->LocalNumSLocEntries;
1236 }
1237 assert(F && F->LocalNumSLocEntries > ID && "Chain corrupted");
1238
1239 F->SLocEntryCursor.JumpToBit(F->SLocOffsets[ID]);
Sebastian Redlc3632732010-10-05 15:59:54 +00001240 return F;
Sebastian Redl190faf72010-07-20 21:50:20 +00001241}
1242
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001243/// \brief Read in the source location entry with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001244ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(unsigned ID) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001245 if (ID == 0)
1246 return Success;
1247
1248 if (ID > TotalNumSLocEntries) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001249 Error("source location entry ID out-of-range for AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001250 return Failure;
1251 }
1252
Sebastian Redlc3632732010-10-05 15:59:54 +00001253 PerFileData *F = SLocCursorForID(ID);
1254 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001255
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001256 ++NumSLocEntriesRead;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001257 unsigned Code = SLocEntryCursor.ReadCode();
1258 if (Code == llvm::bitc::END_BLOCK ||
1259 Code == llvm::bitc::ENTER_SUBBLOCK ||
1260 Code == llvm::bitc::DEFINE_ABBREV) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001261 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001262 return Failure;
1263 }
1264
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001265 RecordData Record;
1266 const char *BlobStart;
1267 unsigned BlobLen;
1268 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1269 default:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001270 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001271 return Failure;
1272
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001273 case SM_SLOC_FILE_ENTRY: {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001274 std::string Filename(BlobStart, BlobStart + BlobLen);
1275 MaybeAddSystemRootToFilename(Filename);
Chris Lattner39b49bc2010-11-23 08:35:12 +00001276 const FileEntry *File = FileMgr.getFile(Filename);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001277 if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() &&
1278 OriginalDir != CurrentDir) {
1279 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1280 OriginalDir,
1281 CurrentDir);
1282 if (!resolved.empty())
1283 File = FileMgr.getFile(resolved);
1284 }
Axel Naumann04331162011-01-27 10:55:51 +00001285 if (File == 0)
1286 File = FileMgr.getVirtualFile(Filename, (off_t)Record[4],
1287 (time_t)Record[5]);
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001288 if (File == 0) {
1289 std::string ErrorStr = "could not find file '";
Douglas Gregore650c8c2009-07-07 00:12:59 +00001290 ErrorStr += Filename;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001291 ErrorStr += "' referenced by AST file";
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001292 Error(ErrorStr.c_str());
1293 return Failure;
1294 }
Mike Stump1eb44332009-09-09 15:08:12 +00001295
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001296 if (Record.size() < 6) {
Ted Kremenek1857f622010-03-18 21:23:05 +00001297 Error("source location entry is incorrect");
1298 return Failure;
1299 }
1300
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001301 if (!DisableValidation &&
1302 ((off_t)Record[4] != File->getSize()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001303#if !defined(LLVM_ON_WIN32)
1304 // In our regression testing, the Windows file system seems to
1305 // have inconsistent modification times that sometimes
1306 // erroneously trigger this error-handling path.
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001307 || (time_t)Record[5] != File->getModificationTime()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001308#endif
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001309 )) {
Douglas Gregor2d52be52010-03-21 22:49:54 +00001310 Diag(diag::err_fe_pch_file_modified)
1311 << Filename;
1312 return Failure;
1313 }
1314
Chris Lattner75dfb652010-11-23 09:19:42 +00001315 FileID FID = SourceMgr.createFileID(File, ReadSourceLocation(*F, Record[1]),
1316 (SrcMgr::CharacteristicKind)Record[2],
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001317 ID, Record[0]);
1318 if (Record[3])
1319 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile())
1320 .setHasLineDirectives();
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001321
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001322 break;
1323 }
1324
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001325 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001326 const char *Name = BlobStart;
1327 unsigned Offset = Record[0];
1328 unsigned Code = SLocEntryCursor.ReadCode();
1329 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001330 unsigned RecCode
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001331 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001332
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001333 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001334 Error("AST record has invalid code");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001335 return Failure;
1336 }
1337
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001338 llvm::MemoryBuffer *Buffer
Chris Lattnera0a270c2010-04-05 22:42:27 +00001339 = llvm::MemoryBuffer::getMemBuffer(llvm::StringRef(BlobStart, BlobLen - 1),
1340 Name);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001341 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID, Offset);
Mike Stump1eb44332009-09-09 15:08:12 +00001342
Douglas Gregor92b059e2009-04-28 20:33:11 +00001343 if (strcmp(Name, "<built-in>") == 0) {
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +00001344 PCHPredefinesBlock Block = {
1345 BufferID,
1346 llvm::StringRef(BlobStart, BlobLen - 1)
1347 };
1348 PCHPredefinesBuffers.push_back(Block);
Douglas Gregor92b059e2009-04-28 20:33:11 +00001349 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001350
1351 break;
1352 }
1353
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001354 case SM_SLOC_INSTANTIATION_ENTRY: {
Sebastian Redlc3632732010-10-05 15:59:54 +00001355 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001356 SourceMgr.createInstantiationLoc(SpellingLoc,
Sebastian Redlc3632732010-10-05 15:59:54 +00001357 ReadSourceLocation(*F, Record[2]),
1358 ReadSourceLocation(*F, Record[3]),
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001359 Record[4],
1360 ID,
1361 Record[0]);
1362 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001363 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001364 }
1365
1366 return Success;
1367}
1368
Chris Lattner6367f6d2009-04-27 01:05:14 +00001369/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1370/// specified cursor. Read the abbreviations that are at the top of the block
1371/// and then leave the cursor pointing into the block.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001372bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattner6367f6d2009-04-27 01:05:14 +00001373 unsigned BlockID) {
1374 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001375 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001376 return Failure;
1377 }
Mike Stump1eb44332009-09-09 15:08:12 +00001378
Chris Lattner6367f6d2009-04-27 01:05:14 +00001379 while (true) {
Douglas Gregorecdcb882010-10-20 22:00:55 +00001380 uint64_t Offset = Cursor.GetCurrentBitNo();
Chris Lattner6367f6d2009-04-27 01:05:14 +00001381 unsigned Code = Cursor.ReadCode();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001382
Chris Lattner6367f6d2009-04-27 01:05:14 +00001383 // We expect all abbrevs to be at the start of the block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001384 if (Code != llvm::bitc::DEFINE_ABBREV) {
1385 Cursor.JumpToBit(Offset);
Chris Lattner6367f6d2009-04-27 01:05:14 +00001386 return false;
Douglas Gregorecdcb882010-10-20 22:00:55 +00001387 }
Chris Lattner6367f6d2009-04-27 01:05:14 +00001388 Cursor.ReadAbbrevRecord();
1389 }
1390}
1391
Douglas Gregor89d99802010-11-30 06:16:57 +00001392PreprocessedEntity *ASTReader::ReadMacroRecord(PerFileData &F, uint64_t Offset) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001393 assert(PP && "Forgot to set Preprocessor ?");
Douglas Gregorecdcb882010-10-20 22:00:55 +00001394 llvm::BitstreamCursor &Stream = F.MacroCursor;
Mike Stump1eb44332009-09-09 15:08:12 +00001395
Douglas Gregor37e26842009-04-21 23:56:24 +00001396 // Keep track of where we are in the stream, then jump back there
1397 // after reading this macro.
1398 SavedStreamPosition SavedPosition(Stream);
1399
1400 Stream.JumpToBit(Offset);
1401 RecordData Record;
1402 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1403 MacroInfo *Macro = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001404
Douglas Gregor37e26842009-04-21 23:56:24 +00001405 while (true) {
1406 unsigned Code = Stream.ReadCode();
1407 switch (Code) {
1408 case llvm::bitc::END_BLOCK:
Douglas Gregor89d99802010-11-30 06:16:57 +00001409 return 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001410
1411 case llvm::bitc::ENTER_SUBBLOCK:
1412 // No known subblocks, always skip them.
1413 Stream.ReadSubBlockID();
1414 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001415 Error("malformed block record in AST file");
Douglas Gregor89d99802010-11-30 06:16:57 +00001416 return 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001417 }
1418 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001419
Douglas Gregor37e26842009-04-21 23:56:24 +00001420 case llvm::bitc::DEFINE_ABBREV:
1421 Stream.ReadAbbrevRecord();
1422 continue;
1423 default: break;
1424 }
1425
1426 // Read a record.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001427 const char *BlobStart = 0;
1428 unsigned BlobLen = 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001429 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001430 PreprocessorRecordTypes RecType =
Michael J. Spencer20249a12010-10-21 03:16:25 +00001431 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart,
Douglas Gregorecdcb882010-10-20 22:00:55 +00001432 BlobLen);
Douglas Gregor37e26842009-04-21 23:56:24 +00001433 switch (RecType) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001434 case PP_MACRO_OBJECT_LIKE:
1435 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001436 // If we already have a macro, that means that we've hit the end
1437 // of the definition of the macro we were looking for. We're
1438 // done.
1439 if (Macro)
Douglas Gregor89d99802010-11-30 06:16:57 +00001440 return 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001441
1442 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1443 if (II == 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001444 Error("macro must have a name in AST file");
Douglas Gregor89d99802010-11-30 06:16:57 +00001445 return 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001446 }
Sebastian Redlc3632732010-10-05 15:59:54 +00001447 SourceLocation Loc = ReadSourceLocation(F, Record[1]);
Douglas Gregor37e26842009-04-21 23:56:24 +00001448 bool isUsed = Record[2];
Mike Stump1eb44332009-09-09 15:08:12 +00001449
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001450 MacroInfo *MI = PP->AllocateMacroInfo(Loc);
Douglas Gregor37e26842009-04-21 23:56:24 +00001451 MI->setIsUsed(isUsed);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001452 MI->setIsFromAST();
Mike Stump1eb44332009-09-09 15:08:12 +00001453
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001454 unsigned NextIndex = 3;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001455 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregor37e26842009-04-21 23:56:24 +00001456 // Decode function-like macro info.
1457 bool isC99VarArgs = Record[3];
1458 bool isGNUVarArgs = Record[4];
1459 MacroArgs.clear();
1460 unsigned NumArgs = Record[5];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001461 NextIndex = 6 + NumArgs;
Douglas Gregor37e26842009-04-21 23:56:24 +00001462 for (unsigned i = 0; i != NumArgs; ++i)
1463 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1464
1465 // Install function-like macro info.
1466 MI->setIsFunctionLike();
1467 if (isC99VarArgs) MI->setIsC99Varargs();
1468 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor75fdb232009-05-22 22:45:36 +00001469 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001470 PP->getPreprocessorAllocator());
Douglas Gregor37e26842009-04-21 23:56:24 +00001471 }
1472
1473 // Finally, install the macro.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001474 PP->setMacroInfo(II, MI);
Douglas Gregor37e26842009-04-21 23:56:24 +00001475
1476 // Remember that we saw this macro last so that we add the tokens that
1477 // form its body to it.
1478 Macro = MI;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001479
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001480 if (NextIndex + 1 == Record.size() && PP->getPreprocessingRecord()) {
1481 // We have a macro definition. Load it now.
1482 PP->getPreprocessingRecord()->RegisterMacroDefinition(Macro,
1483 getMacroDefinition(Record[NextIndex]));
1484 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001485
Douglas Gregor37e26842009-04-21 23:56:24 +00001486 ++NumMacrosRead;
1487 break;
1488 }
Mike Stump1eb44332009-09-09 15:08:12 +00001489
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001490 case PP_TOKEN: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001491 // If we see a TOKEN before a PP_MACRO_*, then the file is
1492 // erroneous, just pretend we didn't see this.
1493 if (Macro == 0) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001494
Douglas Gregor37e26842009-04-21 23:56:24 +00001495 Token Tok;
1496 Tok.startToken();
Sebastian Redlc3632732010-10-05 15:59:54 +00001497 Tok.setLocation(ReadSourceLocation(F, Record[0]));
Douglas Gregor37e26842009-04-21 23:56:24 +00001498 Tok.setLength(Record[1]);
1499 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1500 Tok.setIdentifierInfo(II);
1501 Tok.setKind((tok::TokenKind)Record[3]);
1502 Tok.setFlag((Token::TokenFlags)Record[4]);
1503 Macro->AddTokenToBody(Tok);
1504 break;
1505 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001506 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001507 }
Douglas Gregor89d99802010-11-30 06:16:57 +00001508
1509 return 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001510}
1511
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001512PreprocessedEntity *ASTReader::LoadPreprocessedEntity(PerFileData &F) {
1513 assert(PP && "Forgot to set Preprocessor ?");
1514 unsigned Code = F.PreprocessorDetailCursor.ReadCode();
1515 switch (Code) {
1516 case llvm::bitc::END_BLOCK:
1517 return 0;
1518
1519 case llvm::bitc::ENTER_SUBBLOCK:
1520 Error("unexpected subblock record in preprocessor detail block");
1521 return 0;
1522
1523 case llvm::bitc::DEFINE_ABBREV:
1524 Error("unexpected abbrevation record in preprocessor detail block");
1525 return 0;
1526
1527 default:
1528 break;
1529 }
1530
1531 if (!PP->getPreprocessingRecord()) {
1532 Error("no preprocessing record");
1533 return 0;
1534 }
1535
1536 // Read the record.
1537 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1538 const char *BlobStart = 0;
1539 unsigned BlobLen = 0;
1540 RecordData Record;
1541 PreprocessorDetailRecordTypes RecType =
1542 (PreprocessorDetailRecordTypes)F.PreprocessorDetailCursor.ReadRecord(
1543 Code, Record, BlobStart, BlobLen);
1544 switch (RecType) {
1545 case PPD_MACRO_INSTANTIATION: {
1546 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1547 return PE;
1548
1549 MacroInstantiation *MI
1550 = new (PPRec) MacroInstantiation(DecodeIdentifierInfo(Record[3]),
1551 SourceRange(ReadSourceLocation(F, Record[1]),
1552 ReadSourceLocation(F, Record[2])),
1553 getMacroDefinition(Record[4]));
1554 PPRec.SetPreallocatedEntity(Record[0], MI);
1555 return MI;
1556 }
1557
1558 case PPD_MACRO_DEFINITION: {
1559 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1560 return PE;
1561
1562 if (Record[1] > MacroDefinitionsLoaded.size()) {
1563 Error("out-of-bounds macro definition record");
1564 return 0;
1565 }
1566
1567 // Decode the identifier info and then check again; if the macro is
1568 // still defined and associated with the identifier,
1569 IdentifierInfo *II = DecodeIdentifierInfo(Record[4]);
1570 if (!MacroDefinitionsLoaded[Record[1] - 1]) {
1571 MacroDefinition *MD
1572 = new (PPRec) MacroDefinition(II,
1573 ReadSourceLocation(F, Record[5]),
1574 SourceRange(
1575 ReadSourceLocation(F, Record[2]),
1576 ReadSourceLocation(F, Record[3])));
1577
1578 PPRec.SetPreallocatedEntity(Record[0], MD);
1579 MacroDefinitionsLoaded[Record[1] - 1] = MD;
1580
1581 if (DeserializationListener)
1582 DeserializationListener->MacroDefinitionRead(Record[1], MD);
1583 }
1584
1585 return MacroDefinitionsLoaded[Record[1] - 1];
1586 }
1587
1588 case PPD_INCLUSION_DIRECTIVE: {
1589 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1590 return PE;
1591
1592 const char *FullFileNameStart = BlobStart + Record[3];
1593 const FileEntry *File
1594 = PP->getFileManager().getFile(llvm::StringRef(FullFileNameStart,
1595 BlobLen - Record[3]));
1596
1597 // FIXME: Stable encoding
1598 InclusionDirective::InclusionKind Kind
1599 = static_cast<InclusionDirective::InclusionKind>(Record[5]);
1600 InclusionDirective *ID
1601 = new (PPRec) InclusionDirective(PPRec, Kind,
1602 llvm::StringRef(BlobStart, Record[3]),
1603 Record[4],
1604 File,
1605 SourceRange(ReadSourceLocation(F, Record[1]),
1606 ReadSourceLocation(F, Record[2])));
1607 PPRec.SetPreallocatedEntity(Record[0], ID);
1608 return ID;
1609 }
1610 }
1611
1612 Error("invalid offset in preprocessor detail block");
1613 return 0;
1614}
1615
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001616namespace {
1617 /// \brief Trait class used to search the on-disk hash table containing all of
1618 /// the header search information.
1619 ///
1620 /// The on-disk hash table contains a mapping from each header path to
1621 /// information about that header (how many times it has been included, its
1622 /// controlling macro, etc.). Note that we actually hash based on the
1623 /// filename, and support "deep" comparisons of file names based on current
1624 /// inode numbers, so that the search can cope with non-normalized path names
1625 /// and symlinks.
1626 class HeaderFileInfoTrait {
1627 const char *SearchPath;
1628 struct stat SearchPathStatBuf;
1629 llvm::Optional<int> SearchPathStatResult;
1630
1631 int StatSimpleCache(const char *Path, struct stat *StatBuf) {
1632 if (Path == SearchPath) {
1633 if (!SearchPathStatResult)
1634 SearchPathStatResult = stat(Path, &SearchPathStatBuf);
1635
1636 *StatBuf = SearchPathStatBuf;
1637 return *SearchPathStatResult;
1638 }
1639
1640 return stat(Path, StatBuf);
1641 }
1642
1643 public:
1644 typedef const char *external_key_type;
1645 typedef const char *internal_key_type;
1646
1647 typedef HeaderFileInfo data_type;
1648
1649 HeaderFileInfoTrait(const char *SearchPath = 0) : SearchPath(SearchPath) { }
1650
1651 static unsigned ComputeHash(const char *path) {
1652 return llvm::HashString(llvm::sys::path::filename(path));
1653 }
1654
1655 static internal_key_type GetInternalKey(const char *path) { return path; }
1656
1657 bool EqualKey(internal_key_type a, internal_key_type b) {
1658 if (strcmp(a, b) == 0)
1659 return true;
1660
1661 if (llvm::sys::path::filename(a) != llvm::sys::path::filename(b))
1662 return false;
1663
1664 // The file names match, but the path names don't. stat() the files to
1665 // see if they are the same.
1666 struct stat StatBufA, StatBufB;
1667 if (StatSimpleCache(a, &StatBufA) || StatSimpleCache(b, &StatBufB))
1668 return false;
1669
1670 return StatBufA.st_ino == StatBufB.st_ino;
1671 }
1672
1673 static std::pair<unsigned, unsigned>
1674 ReadKeyDataLength(const unsigned char*& d) {
1675 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1676 unsigned DataLen = (unsigned) *d++;
1677 return std::make_pair(KeyLen + 1, DataLen);
1678 }
1679
1680 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
1681 return (const char *)d;
1682 }
1683
1684 static data_type ReadData(const internal_key_type, const unsigned char *d,
1685 unsigned DataLen) {
1686 const unsigned char *End = d + DataLen;
1687 using namespace clang::io;
1688 HeaderFileInfo HFI;
1689 unsigned Flags = *d++;
1690 HFI.isImport = (Flags >> 3) & 0x01;
1691 HFI.DirInfo = (Flags >> 1) & 0x03;
1692 HFI.Resolved = Flags & 0x01;
1693 HFI.NumIncludes = ReadUnalignedLE16(d);
1694 HFI.ControllingMacroID = ReadUnalignedLE32(d);
1695 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1696 (void)End;
1697
1698 // This HeaderFileInfo was externally loaded.
1699 HFI.External = true;
1700 return HFI;
1701 }
1702 };
1703}
1704
1705/// \brief The on-disk hash table used for the global method pool.
1706typedef OnDiskChainedHashTable<HeaderFileInfoTrait>
1707 HeaderFileInfoLookupTable;
1708
Douglas Gregor295a2a62010-10-30 00:23:06 +00001709void ASTReader::SetIdentifierIsMacro(IdentifierInfo *II, PerFileData &F,
1710 uint64_t Offset) {
1711 // Note that this identifier has a macro definition.
1712 II->setHasMacroDefinition(true);
1713
1714 // Adjust the offset based on our position in the chain.
1715 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1716 if (Chain[I] == &F)
1717 break;
1718
1719 Offset += Chain[I]->SizeInBits;
1720 }
1721
1722 UnreadMacroRecordOffsets[II] = Offset;
1723}
1724
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001725void ASTReader::ReadDefinedMacros() {
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001726 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redlc3632732010-10-05 15:59:54 +00001727 PerFileData &F = *Chain[N - I - 1];
1728 llvm::BitstreamCursor &MacroCursor = F.MacroCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001729
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001730 // If there was no preprocessor block, skip this file.
1731 if (!MacroCursor.getBitStreamReader())
1732 continue;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001733
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001734 llvm::BitstreamCursor Cursor = MacroCursor;
Douglas Gregorecdcb882010-10-20 22:00:55 +00001735 Cursor.JumpToBit(F.MacroStartOffset);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001736
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001737 RecordData Record;
1738 while (true) {
1739 unsigned Code = Cursor.ReadCode();
Douglas Gregorecdcb882010-10-20 22:00:55 +00001740 if (Code == llvm::bitc::END_BLOCK)
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001741 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001742
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001743 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1744 // No known subblocks, always skip them.
1745 Cursor.ReadSubBlockID();
1746 if (Cursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001747 Error("malformed block record in AST file");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001748 return;
1749 }
1750 continue;
1751 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001752
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001753 if (Code == llvm::bitc::DEFINE_ABBREV) {
1754 Cursor.ReadAbbrevRecord();
1755 continue;
1756 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001757
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001758 // Read a record.
1759 const char *BlobStart;
1760 unsigned BlobLen;
1761 Record.clear();
1762 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1763 default: // Default behavior: ignore.
1764 break;
Douglas Gregor88a35862010-01-04 19:18:44 +00001765
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001766 case PP_MACRO_OBJECT_LIKE:
1767 case PP_MACRO_FUNCTION_LIKE:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001768 DecodeIdentifierInfo(Record[0]);
1769 break;
1770
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001771 case PP_TOKEN:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001772 // Ignore tokens.
1773 break;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001774 }
Douglas Gregor88a35862010-01-04 19:18:44 +00001775 }
1776 }
Douglas Gregor295a2a62010-10-30 00:23:06 +00001777
1778 // Drain the unread macro-record offsets map.
1779 while (!UnreadMacroRecordOffsets.empty())
1780 LoadMacroDefinition(UnreadMacroRecordOffsets.begin());
1781}
1782
1783void ASTReader::LoadMacroDefinition(
1784 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos) {
1785 assert(Pos != UnreadMacroRecordOffsets.end() && "Unknown macro definition");
1786 PerFileData *F = 0;
1787 uint64_t Offset = Pos->second;
1788 UnreadMacroRecordOffsets.erase(Pos);
1789
1790 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1791 if (Offset < Chain[I]->SizeInBits) {
1792 F = Chain[I];
1793 break;
1794 }
1795
1796 Offset -= Chain[I]->SizeInBits;
1797 }
1798 if (!F) {
1799 Error("Malformed macro record offset");
1800 return;
1801 }
1802
1803 ReadMacroRecord(*F, Offset);
1804}
1805
1806void ASTReader::LoadMacroDefinition(IdentifierInfo *II) {
1807 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos
1808 = UnreadMacroRecordOffsets.find(II);
1809 LoadMacroDefinition(Pos);
Douglas Gregor88a35862010-01-04 19:18:44 +00001810}
1811
Sebastian Redlf73c93f2010-09-15 19:54:06 +00001812MacroDefinition *ASTReader::getMacroDefinition(MacroID ID) {
Douglas Gregor77424bc2010-10-02 19:29:26 +00001813 if (ID == 0 || ID > MacroDefinitionsLoaded.size())
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001814 return 0;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001815
Douglas Gregor77424bc2010-10-02 19:29:26 +00001816 if (!MacroDefinitionsLoaded[ID - 1]) {
1817 unsigned Index = ID - 1;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001818 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1819 PerFileData &F = *Chain[N - I - 1];
1820 if (Index < F.LocalNumMacroDefinitions) {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001821 SavedStreamPosition SavedPosition(F.PreprocessorDetailCursor);
1822 F.PreprocessorDetailCursor.JumpToBit(F.MacroDefinitionOffsets[Index]);
1823 LoadPreprocessedEntity(F);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001824 break;
1825 }
1826 Index -= F.LocalNumMacroDefinitions;
1827 }
Douglas Gregor77424bc2010-10-02 19:29:26 +00001828 assert(MacroDefinitionsLoaded[ID - 1] && "Broken chain");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001829 }
1830
Douglas Gregor77424bc2010-10-02 19:29:26 +00001831 return MacroDefinitionsLoaded[ID - 1];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001832}
1833
Douglas Gregore650c8c2009-07-07 00:12:59 +00001834/// \brief If we are loading a relocatable PCH file, and the filename is
1835/// not an absolute path, add the system root to the beginning of the file
1836/// name.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001837void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001838 // If this is not a relocatable PCH file, there's nothing to do.
1839 if (!RelocatablePCH)
1840 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001841
Michael J. Spencer256053b2010-12-17 21:22:22 +00001842 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
Douglas Gregore650c8c2009-07-07 00:12:59 +00001843 return;
1844
Douglas Gregore650c8c2009-07-07 00:12:59 +00001845 if (isysroot == 0) {
1846 // If no system root was given, default to '/'
1847 Filename.insert(Filename.begin(), '/');
1848 return;
1849 }
Mike Stump1eb44332009-09-09 15:08:12 +00001850
Douglas Gregore650c8c2009-07-07 00:12:59 +00001851 unsigned Length = strlen(isysroot);
1852 if (isysroot[Length - 1] != '/')
1853 Filename.insert(Filename.begin(), '/');
Mike Stump1eb44332009-09-09 15:08:12 +00001854
Douglas Gregore650c8c2009-07-07 00:12:59 +00001855 Filename.insert(Filename.begin(), isysroot, isysroot + Length);
1856}
1857
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001858ASTReader::ASTReadResult
Sebastian Redl571db7f2010-08-18 23:56:56 +00001859ASTReader::ReadASTBlock(PerFileData &F) {
Sebastian Redl9137a522010-07-16 17:50:48 +00001860 llvm::BitstreamCursor &Stream = F.Stream;
1861
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001862 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001863 Error("malformed block record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001864 return Failure;
1865 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001866
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001867 // Read all of the records and blocks for the ASt file.
Douglas Gregor8038d512009-04-10 17:25:41 +00001868 RecordData Record;
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001869 bool First = true;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001870 while (!Stream.AtEndOfStream()) {
1871 unsigned Code = Stream.ReadCode();
1872 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001873 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001874 Error("error at end of module block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001875 return Failure;
1876 }
Chris Lattner7356a312009-04-11 21:15:38 +00001877
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001878 return Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001879 }
1880
1881 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1882 switch (Stream.ReadSubBlockID()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001883 case DECLTYPES_BLOCK_ID:
Chris Lattner6367f6d2009-04-27 01:05:14 +00001884 // We lazily load the decls block, but we want to set up the
1885 // DeclsCursor cursor to point into it. Clone our current bitcode
1886 // cursor to it, enter the block and read the abbrevs in that block.
1887 // With the main cursor, we just skip over it.
Sebastian Redl9137a522010-07-16 17:50:48 +00001888 F.DeclsCursor = Stream;
Chris Lattner6367f6d2009-04-27 01:05:14 +00001889 if (Stream.SkipBlock() || // Skip with the main cursor.
1890 // Read the abbrevs.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001891 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001892 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001893 return Failure;
1894 }
1895 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001896
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00001897 case DECL_UPDATES_BLOCK_ID:
1898 if (Stream.SkipBlock()) {
1899 Error("malformed block record in AST file");
1900 return Failure;
1901 }
1902 break;
1903
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001904 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl9137a522010-07-16 17:50:48 +00001905 F.MacroCursor = Stream;
Douglas Gregor88a35862010-01-04 19:18:44 +00001906 if (PP)
1907 PP->setExternalSource(this);
1908
Douglas Gregorecdcb882010-10-20 22:00:55 +00001909 if (Stream.SkipBlock() ||
1910 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001911 Error("malformed block record in AST file");
Chris Lattner7356a312009-04-11 21:15:38 +00001912 return Failure;
1913 }
Douglas Gregorecdcb882010-10-20 22:00:55 +00001914 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
Chris Lattner7356a312009-04-11 21:15:38 +00001915 break;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00001916
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001917 case PREPROCESSOR_DETAIL_BLOCK_ID:
1918 F.PreprocessorDetailCursor = Stream;
1919 if (Stream.SkipBlock() ||
1920 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
1921 PREPROCESSOR_DETAIL_BLOCK_ID)) {
1922 Error("malformed preprocessor detail record in AST file");
1923 return Failure;
1924 }
1925 F.PreprocessorDetailStartOffset
1926 = F.PreprocessorDetailCursor.GetCurrentBitNo();
1927 break;
1928
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001929 case SOURCE_MANAGER_BLOCK_ID:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001930 switch (ReadSourceManagerBlock(F)) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00001931 case Success:
1932 break;
1933
1934 case Failure:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001935 Error("malformed source manager block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001936 return Failure;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001937
1938 case IgnorePCH:
1939 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001940 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001941 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001942 }
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001943 First = false;
Douglas Gregor8038d512009-04-10 17:25:41 +00001944 continue;
1945 }
1946
1947 if (Code == llvm::bitc::DEFINE_ABBREV) {
1948 Stream.ReadAbbrevRecord();
1949 continue;
1950 }
1951
1952 // Read and process a record.
1953 Record.clear();
Douglas Gregor2bec0412009-04-10 21:16:55 +00001954 const char *BlobStart = 0;
1955 unsigned BlobLen = 0;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001956 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00001957 &BlobStart, &BlobLen)) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001958 default: // Default behavior: ignore.
1959 break;
1960
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001961 case METADATA: {
1962 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1963 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001964 : diag::warn_pch_version_too_new);
1965 return IgnorePCH;
1966 }
1967
1968 RelocatablePCH = Record[4];
1969 if (Listener) {
1970 std::string TargetTriple(BlobStart, BlobLen);
1971 if (Listener->ReadTargetTriple(TargetTriple))
1972 return IgnorePCH;
1973 }
1974 break;
1975 }
1976
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001977 case CHAINED_METADATA: {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001978 if (!First) {
1979 Error("CHAINED_METADATA is not first record in block");
1980 return Failure;
1981 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001982 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1983 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001984 : diag::warn_pch_version_too_new);
1985 return IgnorePCH;
1986 }
1987
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00001988 // Load the chained file, which is always a PCH file.
1989 switch(ReadASTCore(llvm::StringRef(BlobStart, BlobLen), PCH)) {
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001990 case Failure: return Failure;
1991 // If we have to ignore the dependency, we'll have to ignore this too.
1992 case IgnorePCH: return IgnorePCH;
1993 case Success: break;
1994 }
1995 break;
1996 }
1997
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001998 case TYPE_OFFSET:
Sebastian Redl12d6da02010-07-19 22:06:55 +00001999 if (F.LocalNumTypes != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002000 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002001 return Failure;
2002 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00002003 F.TypeOffsets = (const uint32_t *)BlobStart;
2004 F.LocalNumTypes = Record[0];
Douglas Gregor8038d512009-04-10 17:25:41 +00002005 break;
2006
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002007 case DECL_OFFSET:
Sebastian Redl12d6da02010-07-19 22:06:55 +00002008 if (F.LocalNumDecls != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002009 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002010 return Failure;
2011 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00002012 F.DeclOffsets = (const uint32_t *)BlobStart;
2013 F.LocalNumDecls = Record[0];
Douglas Gregor8038d512009-04-10 17:25:41 +00002014 break;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002015
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002016 case TU_UPDATE_LEXICAL: {
Sebastian Redld692af72010-07-27 18:24:41 +00002017 DeclContextInfo Info = {
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002018 /* No visible information */ 0,
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002019 reinterpret_cast<const KindDeclIDPair *>(BlobStart),
2020 BlobLen / sizeof(KindDeclIDPair)
Sebastian Redld692af72010-07-27 18:24:41 +00002021 };
Douglas Gregor3747ee72010-10-01 01:18:02 +00002022 DeclContextOffsets[Context ? Context->getTranslationUnitDecl() : 0]
2023 .push_back(Info);
Sebastian Redld692af72010-07-27 18:24:41 +00002024 break;
2025 }
2026
Sebastian Redle1dde812010-08-24 00:50:04 +00002027 case UPDATE_VISIBLE: {
2028 serialization::DeclID ID = Record[0];
2029 void *Table = ASTDeclContextNameLookupTable::Create(
2030 (const unsigned char *)BlobStart + Record[1],
2031 (const unsigned char *)BlobStart,
2032 ASTDeclContextNameLookupTrait(*this));
Douglas Gregor3747ee72010-10-01 01:18:02 +00002033 if (ID == 1 && Context) { // Is it the TU?
Sebastian Redle1dde812010-08-24 00:50:04 +00002034 DeclContextInfo Info = {
2035 Table, /* No lexical inforamtion */ 0, 0
2036 };
2037 DeclContextOffsets[Context->getTranslationUnitDecl()].push_back(Info);
2038 } else
2039 PendingVisibleUpdates[ID].push_back(Table);
2040 break;
2041 }
2042
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002043 case REDECLS_UPDATE_LATEST: {
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00002044 assert(Record.size() % 2 == 0 && "Expected pairs of DeclIDs");
2045 for (unsigned i = 0, e = Record.size(); i < e; i += 2) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002046 DeclID First = Record[i], Latest = Record[i+1];
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00002047 assert((FirstLatestDeclIDs.find(First) == FirstLatestDeclIDs.end() ||
2048 Latest > FirstLatestDeclIDs[First]) &&
2049 "The new latest is supposed to come after the previous latest");
2050 FirstLatestDeclIDs[First] = Latest;
2051 }
2052 break;
2053 }
2054
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002055 case LANGUAGE_OPTIONS:
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00002056 if (ParseLanguageOptions(Record) && !DisableValidation)
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002057 return IgnorePCH;
2058 break;
Douglas Gregor2bec0412009-04-10 21:16:55 +00002059
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002060 case IDENTIFIER_TABLE:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002061 F.IdentifierTableData = BlobStart;
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002062 if (Record[0]) {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002063 F.IdentifierLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002064 = ASTIdentifierLookupTable::Create(
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002065 (const unsigned char *)F.IdentifierTableData + Record[0],
2066 (const unsigned char *)F.IdentifierTableData,
Sebastian Redlc3632732010-10-05 15:59:54 +00002067 ASTIdentifierLookupTrait(*this, F));
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002068 if (PP)
2069 PP->getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002070 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002071 break;
2072
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002073 case IDENTIFIER_OFFSET:
Sebastian Redl2da08f92010-07-19 22:28:42 +00002074 if (F.LocalNumIdentifiers != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002075 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00002076 return Failure;
2077 }
Sebastian Redl2da08f92010-07-19 22:28:42 +00002078 F.IdentifierOffsets = (const uint32_t *)BlobStart;
2079 F.LocalNumIdentifiers = Record[0];
Douglas Gregorafaf3082009-04-11 00:14:32 +00002080 break;
Douglas Gregorfdd01722009-04-14 00:24:19 +00002081
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002082 case EXTERNAL_DEFINITIONS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002083 // Optimization for the first block.
2084 if (ExternalDefinitions.empty())
2085 ExternalDefinitions.swap(Record);
2086 else
2087 ExternalDefinitions.insert(ExternalDefinitions.end(),
2088 Record.begin(), Record.end());
Douglas Gregorfdd01722009-04-14 00:24:19 +00002089 break;
Douglas Gregor3e1af842009-04-17 22:13:46 +00002090
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002091 case SPECIAL_TYPES:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002092 // Optimization for the first block
2093 if (SpecialTypes.empty())
2094 SpecialTypes.swap(Record);
2095 else
2096 SpecialTypes.insert(SpecialTypes.end(), Record.begin(), Record.end());
Douglas Gregorad1de002009-04-18 05:55:16 +00002097 break;
2098
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002099 case STATISTICS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002100 TotalNumStatements += Record[0];
2101 TotalNumMacros += Record[1];
2102 TotalLexicalDeclContexts += Record[2];
2103 TotalVisibleDeclContexts += Record[3];
Douglas Gregor3e1af842009-04-17 22:13:46 +00002104 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002105
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002106 case TENTATIVE_DEFINITIONS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002107 // Optimization for the first block.
2108 if (TentativeDefinitions.empty())
2109 TentativeDefinitions.swap(Record);
2110 else
2111 TentativeDefinitions.insert(TentativeDefinitions.end(),
2112 Record.begin(), Record.end());
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002113 break;
Douglas Gregor14c22f22009-04-22 22:18:58 +00002114
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002115 case UNUSED_FILESCOPED_DECLS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002116 // Optimization for the first block.
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002117 if (UnusedFileScopedDecls.empty())
2118 UnusedFileScopedDecls.swap(Record);
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002119 else
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002120 UnusedFileScopedDecls.insert(UnusedFileScopedDecls.end(),
2121 Record.begin(), Record.end());
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002122 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002123
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002124 case WEAK_UNDECLARED_IDENTIFIERS:
Sebastian Redl40566802010-08-05 18:21:25 +00002125 // Later blocks overwrite earlier ones.
2126 WeakUndeclaredIdentifiers.swap(Record);
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002127 break;
2128
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002129 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002130 // Optimization for the first block.
2131 if (LocallyScopedExternalDecls.empty())
2132 LocallyScopedExternalDecls.swap(Record);
2133 else
2134 LocallyScopedExternalDecls.insert(LocallyScopedExternalDecls.end(),
2135 Record.begin(), Record.end());
Douglas Gregor14c22f22009-04-22 22:18:58 +00002136 break;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002137
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002138 case SELECTOR_OFFSETS:
Sebastian Redl059612d2010-08-03 21:58:15 +00002139 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redl725cd962010-08-04 20:40:17 +00002140 F.LocalNumSelectors = Record[0];
Douglas Gregor83941df2009-04-25 17:48:32 +00002141 break;
2142
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002143 case METHOD_POOL:
Sebastian Redl725cd962010-08-04 20:40:17 +00002144 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor83941df2009-04-25 17:48:32 +00002145 if (Record[0])
Sebastian Redl725cd962010-08-04 20:40:17 +00002146 F.SelectorLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002147 = ASTSelectorLookupTable::Create(
Sebastian Redl725cd962010-08-04 20:40:17 +00002148 F.SelectorLookupTableData + Record[0],
2149 F.SelectorLookupTableData,
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002150 ASTSelectorLookupTrait(*this));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002151 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002152 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002153
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002154 case REFERENCED_SELECTOR_POOL:
Sebastian Redlc3632732010-10-05 15:59:54 +00002155 F.ReferencedSelectorsData.swap(Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002156 break;
2157
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002158 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002159 if (!Record.empty() && Listener)
2160 Listener->ReadCounter(Record[0]);
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002161 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002162
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002163 case SOURCE_LOCATION_OFFSETS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002164 F.SLocOffsets = (const uint32_t *)BlobStart;
2165 F.LocalNumSLocEntries = Record[0];
Sebastian Redl8db9fae2010-09-22 20:19:08 +00002166 F.LocalSLocSize = Record[1];
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002167 break;
2168
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002169 case SOURCE_LOCATION_PRELOADS:
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002170 if (PreloadSLocEntries.empty())
2171 PreloadSLocEntries.swap(Record);
2172 else
2173 PreloadSLocEntries.insert(PreloadSLocEntries.end(),
2174 Record.begin(), Record.end());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002175 break;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002176
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002177 case STAT_CACHE: {
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00002178 if (!DisableStatCache) {
2179 ASTStatCache *MyStatCache =
2180 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
2181 (const unsigned char *)BlobStart,
2182 NumStatHits, NumStatMisses);
2183 FileMgr.addStatCache(MyStatCache);
2184 F.StatCache = MyStatCache;
2185 }
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002186 break;
Douglas Gregor52e71082009-10-16 18:18:30 +00002187 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002188
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002189 case EXT_VECTOR_DECLS:
Sebastian Redla9f23682010-07-28 21:38:49 +00002190 // Optimization for the first block.
2191 if (ExtVectorDecls.empty())
2192 ExtVectorDecls.swap(Record);
2193 else
2194 ExtVectorDecls.insert(ExtVectorDecls.end(),
2195 Record.begin(), Record.end());
Douglas Gregorb81c1702009-04-27 20:06:05 +00002196 break;
2197
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002198 case VTABLE_USES:
Sebastian Redl40566802010-08-05 18:21:25 +00002199 // Later tables overwrite earlier ones.
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002200 VTableUses.swap(Record);
2201 break;
2202
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002203 case DYNAMIC_CLASSES:
Sebastian Redl40566802010-08-05 18:21:25 +00002204 // Optimization for the first block.
2205 if (DynamicClasses.empty())
2206 DynamicClasses.swap(Record);
2207 else
2208 DynamicClasses.insert(DynamicClasses.end(),
2209 Record.begin(), Record.end());
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002210 break;
2211
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002212 case PENDING_IMPLICIT_INSTANTIATIONS:
Sebastian Redlc3632732010-10-05 15:59:54 +00002213 F.PendingInstantiations.swap(Record);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002214 break;
2215
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002216 case SEMA_DECL_REFS:
Sebastian Redl40566802010-08-05 18:21:25 +00002217 // Later tables overwrite earlier ones.
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002218 SemaDeclRefs.swap(Record);
2219 break;
2220
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002221 case ORIGINAL_FILE_NAME:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002222 // The primary AST will be the last to get here, so it will be the one
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002223 // that's used.
Daniel Dunbar7b5a1212009-11-11 05:29:04 +00002224 ActualOriginalFileName.assign(BlobStart, BlobLen);
2225 OriginalFileName = ActualOriginalFileName;
Douglas Gregore650c8c2009-07-07 00:12:59 +00002226 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregorb64c1932009-05-12 01:31:05 +00002227 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002228
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002229 case ORIGINAL_PCH_DIR:
2230 // The primary AST will be the last to get here, so it will be the one
2231 // that's used.
2232 OriginalDir.assign(BlobStart, BlobLen);
2233 break;
2234
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002235 case VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek974be4d2010-02-12 23:31:14 +00002236 const std::string &CurBranch = getClangFullRepositoryVersion();
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002237 llvm::StringRef ASTBranch(BlobStart, BlobLen);
2238 if (llvm::StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2239 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregor445e23e2009-10-05 21:07:28 +00002240 return IgnorePCH;
2241 }
2242 break;
2243 }
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002244
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002245 case MACRO_DEFINITION_OFFSETS:
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002246 F.MacroDefinitionOffsets = (const uint32_t *)BlobStart;
2247 F.NumPreallocatedPreprocessingEntities = Record[0];
2248 F.LocalNumMacroDefinitions = Record[1];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002249 break;
Sebastian Redl0b17c612010-08-13 00:28:03 +00002250
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002251 case DECL_UPDATE_OFFSETS: {
2252 if (Record.size() % 2 != 0) {
2253 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2254 return Failure;
2255 }
2256 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
2257 DeclUpdateOffsets[static_cast<DeclID>(Record[I])]
2258 .push_back(std::make_pair(&F, Record[I+1]));
2259 break;
2260 }
2261
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002262 case DECL_REPLACEMENTS: {
Sebastian Redl0b17c612010-08-13 00:28:03 +00002263 if (Record.size() % 2 != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002264 Error("invalid DECL_REPLACEMENTS block in AST file");
Sebastian Redl0b17c612010-08-13 00:28:03 +00002265 return Failure;
2266 }
2267 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002268 ReplacedDecls[static_cast<DeclID>(Record[I])] =
Sebastian Redl0b17c612010-08-13 00:28:03 +00002269 std::make_pair(&F, Record[I+1]);
2270 break;
2271 }
Douglas Gregor7c789c12010-10-29 22:39:52 +00002272
2273 case CXX_BASE_SPECIFIER_OFFSETS: {
2274 if (F.LocalNumCXXBaseSpecifiers != 0) {
2275 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2276 return Failure;
2277 }
2278
2279 F.LocalNumCXXBaseSpecifiers = Record[0];
2280 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart;
2281 break;
2282 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002283
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002284 case DIAG_PRAGMA_MAPPINGS:
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002285 if (Record.size() % 2 != 0) {
2286 Error("invalid DIAG_USER_MAPPINGS block in AST file");
2287 return Failure;
2288 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002289 if (PragmaDiagMappings.empty())
2290 PragmaDiagMappings.swap(Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002291 else
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002292 PragmaDiagMappings.insert(PragmaDiagMappings.end(),
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002293 Record.begin(), Record.end());
2294 break;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002295
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002296 case CUDA_SPECIAL_DECL_REFS:
2297 // Later tables overwrite earlier ones.
2298 CUDASpecialDeclRefs.swap(Record);
2299 break;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002300
2301 case HEADER_SEARCH_TABLE:
2302 F.HeaderFileInfoTableData = BlobStart;
2303 F.LocalNumHeaderFileInfos = Record[1];
2304 if (Record[0]) {
2305 F.HeaderFileInfoTable
2306 = HeaderFileInfoLookupTable::Create(
2307 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
2308 (const unsigned char *)F.HeaderFileInfoTableData);
2309 if (PP)
2310 PP->getHeaderSearchInfo().SetExternalSource(this);
2311 }
2312 break;
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002313
2314 case FP_PRAGMA_OPTIONS:
2315 // Later tables overwrite earlier ones.
2316 FPPragmaOptions.swap(Record);
2317 break;
2318
2319 case OPENCL_EXTENSIONS:
2320 // Later tables overwrite earlier ones.
2321 OpenCLExtensions.swap(Record);
2322 break;
Douglas Gregorafaf3082009-04-11 00:14:32 +00002323 }
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002324 First = false;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002325 }
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002326 Error("premature end of bitstream in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002327 return Failure;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002328}
2329
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002330ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
2331 ASTFileType Type) {
2332 switch(ReadASTCore(FileName, Type)) {
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002333 case Failure: return Failure;
2334 case IgnorePCH: return IgnorePCH;
2335 case Success: break;
2336 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002337
2338 // Here comes stuff that we only do once the entire chain is loaded.
2339
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002340 // Allocate space for loaded slocentries, identifiers, decls and types.
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002341 unsigned TotalNumIdentifiers = 0, TotalNumTypes = 0, TotalNumDecls = 0,
Sebastian Redl725cd962010-08-04 20:40:17 +00002342 TotalNumPreallocatedPreprocessingEntities = 0, TotalNumMacroDefs = 0,
2343 TotalNumSelectors = 0;
Sebastian Redl12d6da02010-07-19 22:06:55 +00002344 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002345 TotalNumSLocEntries += Chain[I]->LocalNumSLocEntries;
Sebastian Redl8db9fae2010-09-22 20:19:08 +00002346 NextSLocOffset += Chain[I]->LocalSLocSize;
Sebastian Redl2da08f92010-07-19 22:28:42 +00002347 TotalNumIdentifiers += Chain[I]->LocalNumIdentifiers;
Sebastian Redl12d6da02010-07-19 22:06:55 +00002348 TotalNumTypes += Chain[I]->LocalNumTypes;
2349 TotalNumDecls += Chain[I]->LocalNumDecls;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002350 TotalNumPreallocatedPreprocessingEntities +=
2351 Chain[I]->NumPreallocatedPreprocessingEntities;
2352 TotalNumMacroDefs += Chain[I]->LocalNumMacroDefinitions;
Sebastian Redl725cd962010-08-04 20:40:17 +00002353 TotalNumSelectors += Chain[I]->LocalNumSelectors;
Sebastian Redl12d6da02010-07-19 22:06:55 +00002354 }
Sebastian Redl8db9fae2010-09-22 20:19:08 +00002355 SourceMgr.PreallocateSLocEntries(this, TotalNumSLocEntries, NextSLocOffset);
Sebastian Redl2da08f92010-07-19 22:28:42 +00002356 IdentifiersLoaded.resize(TotalNumIdentifiers);
Sebastian Redl12d6da02010-07-19 22:06:55 +00002357 TypesLoaded.resize(TotalNumTypes);
2358 DeclsLoaded.resize(TotalNumDecls);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002359 MacroDefinitionsLoaded.resize(TotalNumMacroDefs);
2360 if (PP) {
2361 if (TotalNumIdentifiers > 0)
2362 PP->getHeaderSearchInfo().SetExternalLookup(this);
2363 if (TotalNumPreallocatedPreprocessingEntities > 0) {
2364 if (!PP->getPreprocessingRecord())
2365 PP->createPreprocessingRecord();
2366 PP->getPreprocessingRecord()->SetExternalSource(*this,
2367 TotalNumPreallocatedPreprocessingEntities);
2368 }
2369 }
Sebastian Redl725cd962010-08-04 20:40:17 +00002370 SelectorsLoaded.resize(TotalNumSelectors);
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002371 // Preload SLocEntries.
2372 for (unsigned I = 0, N = PreloadSLocEntries.size(); I != N; ++I) {
2373 ASTReadResult Result = ReadSLocEntryRecord(PreloadSLocEntries[I]);
2374 if (Result != Success)
2375 return Result;
2376 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00002377
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002378 // Check the predefines buffers.
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00002379 if (!DisableValidation && CheckPredefinesBuffers())
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002380 return IgnorePCH;
2381
2382 if (PP) {
2383 // Initialization of keywords and pragmas occurs before the
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002384 // AST file is read, so there may be some identifiers that were
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002385 // loaded into the IdentifierTable before we intercepted the
2386 // creation of identifiers. Iterate through the list of known
2387 // identifiers and determine whether we have to establish
2388 // preprocessor definitions or top-level identifier declaration
2389 // chains for those identifiers.
2390 //
2391 // We copy the IdentifierInfo pointers to a small vector first,
2392 // since de-serializing declarations or macro definitions can add
2393 // new entries into the identifier table, invalidating the
2394 // iterators.
2395 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
2396 for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(),
2397 IdEnd = PP->getIdentifierTable().end();
2398 Id != IdEnd; ++Id)
2399 Identifiers.push_back(Id->second);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002400 // We need to search the tables in all files.
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002401 for (unsigned J = 0, M = Chain.size(); J != M; ++J) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002402 ASTIdentifierLookupTable *IdTable
2403 = (ASTIdentifierLookupTable *)Chain[J]->IdentifierLookupTable;
2404 // Not all AST files necessarily have identifier tables, only the useful
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00002405 // ones.
2406 if (!IdTable)
2407 continue;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002408 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
2409 IdentifierInfo *II = Identifiers[I];
2410 // Look in the on-disk hash tables for an entry for this identifier
Sebastian Redlc3632732010-10-05 15:59:54 +00002411 ASTIdentifierLookupTrait Info(*this, *Chain[J], II);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002412 std::pair<const char*,unsigned> Key(II->getNameStart(),II->getLength());
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002413 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002414 if (Pos == IdTable->end())
2415 continue;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002416
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002417 // Dereferencing the iterator has the effect of populating the
2418 // IdentifierInfo node with the various declarations it needs.
2419 (void)*Pos;
2420 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002421 }
2422 }
2423
2424 if (Context)
2425 InitializeContext(*Context);
2426
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002427 if (DeserializationListener)
2428 DeserializationListener->ReaderInitialized(this);
2429
Douglas Gregor414cb642010-11-30 05:23:00 +00002430 // If this AST file is a precompiled preamble, then set the main file ID of
2431 // the source manager to the file source file from which the preamble was
2432 // built. This is the only valid way to use a precompiled preamble.
2433 if (Type == Preamble) {
2434 SourceLocation Loc
2435 = SourceMgr.getLocation(FileMgr.getFile(getOriginalSourceFile()), 1, 1);
2436 if (Loc.isValid()) {
2437 std::pair<FileID, unsigned> Decomposed = SourceMgr.getDecomposedLoc(Loc);
2438 SourceMgr.SetPreambleFileID(Decomposed.first);
2439 }
2440 }
2441
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002442 return Success;
2443}
2444
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002445ASTReader::ASTReadResult ASTReader::ReadASTCore(llvm::StringRef FileName,
2446 ASTFileType Type) {
Sebastian Redla866e652010-10-01 19:59:12 +00002447 PerFileData *Prev = Chain.empty() ? 0 : Chain.back();
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002448 Chain.push_back(new PerFileData(Type));
Sebastian Redl9137a522010-07-16 17:50:48 +00002449 PerFileData &F = *Chain.back();
Sebastian Redla866e652010-10-01 19:59:12 +00002450 if (Prev)
2451 Prev->NextInSource = &F;
2452 else
2453 FirstInSource = &F;
2454 F.Loaders.push_back(Prev);
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002455
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002456 // Set the AST file name.
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002457 F.FileName = FileName;
2458
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002459 if (FileName != "-") {
2460 CurrentDir = llvm::sys::path::parent_path(FileName);
2461 if (CurrentDir.empty()) CurrentDir = ".";
2462 }
2463
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002464 // Open the AST file.
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002465 //
2466 // FIXME: This shouldn't be here, we should just take a raw_ostream.
2467 std::string ErrStr;
Michael J. Spencer3a321e22010-12-09 17:36:38 +00002468 llvm::error_code ec;
2469 if (FileName == "-") {
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00002470 ec = llvm::MemoryBuffer::getSTDIN(F.Buffer);
Michael J. Spencer3a321e22010-12-09 17:36:38 +00002471 if (ec)
2472 ErrStr = ec.message();
2473 } else
Chris Lattner39b49bc2010-11-23 08:35:12 +00002474 F.Buffer.reset(FileMgr.getBufferForFile(FileName, &ErrStr));
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002475 if (!F.Buffer) {
2476 Error(ErrStr.c_str());
2477 return IgnorePCH;
2478 }
2479
2480 // Initialize the stream
2481 F.StreamFile.init((const unsigned char *)F.Buffer->getBufferStart(),
2482 (const unsigned char *)F.Buffer->getBufferEnd());
Sebastian Redl9137a522010-07-16 17:50:48 +00002483 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002484 Stream.init(F.StreamFile);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002485 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002486
2487 // Sniff for the signature.
2488 if (Stream.Read(8) != 'C' ||
2489 Stream.Read(8) != 'P' ||
2490 Stream.Read(8) != 'C' ||
2491 Stream.Read(8) != 'H') {
2492 Diag(diag::err_not_a_pch_file) << FileName;
2493 return Failure;
2494 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002495
Douglas Gregor2cf26342009-04-09 22:27:44 +00002496 while (!Stream.AtEndOfStream()) {
2497 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00002498
Douglas Gregore1d918e2009-04-10 23:10:45 +00002499 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002500 Error("invalid record at top-level of AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002501 return Failure;
2502 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002503
2504 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregor668c1a42009-04-21 22:25:48 +00002505
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002506 // We only know the AST subblock ID.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002507 switch (BlockID) {
2508 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002509 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002510 Error("malformed BlockInfoBlock in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002511 return Failure;
2512 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002513 break;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002514 case AST_BLOCK_ID:
Sebastian Redl571db7f2010-08-18 23:56:56 +00002515 switch (ReadASTBlock(F)) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002516 case Success:
2517 break;
2518
2519 case Failure:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002520 return Failure;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002521
2522 case IgnorePCH:
Douglas Gregor2bec0412009-04-10 21:16:55 +00002523 // FIXME: We could consider reading through to the end of this
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002524 // AST block, skipping subblocks, to see if there are other
2525 // AST blocks elsewhere.
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002526
2527 // Clear out any preallocated source location entries, so that
2528 // the source manager does not try to resolve them later.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002529 SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002530
2531 // Remove the stat cache.
Sebastian Redl9137a522010-07-16 17:50:48 +00002532 if (F.StatCache)
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002533 FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002534
Douglas Gregore1d918e2009-04-10 23:10:45 +00002535 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002536 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002537 break;
2538 default:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002539 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002540 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002541 return Failure;
2542 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002543 break;
2544 }
Mike Stump1eb44332009-09-09 15:08:12 +00002545 }
2546
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002547 return Success;
2548}
2549
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002550void ASTReader::setPreprocessor(Preprocessor &pp) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002551 PP = &pp;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002552
2553 unsigned TotalNum = 0;
2554 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
2555 TotalNum += Chain[I]->NumPreallocatedPreprocessingEntities;
2556 if (TotalNum) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002557 if (!PP->getPreprocessingRecord())
2558 PP->createPreprocessingRecord();
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002559 PP->getPreprocessingRecord()->SetExternalSource(*this, TotalNum);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002560 }
2561}
2562
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002563void ASTReader::InitializeContext(ASTContext &Ctx) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002564 Context = &Ctx;
2565 assert(Context && "Passed null context!");
2566
2567 assert(PP && "Forgot to set Preprocessor ?");
2568 PP->getIdentifierTable().setExternalIdentifierLookup(this);
2569 PP->getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregor88a35862010-01-04 19:18:44 +00002570 PP->setExternalSource(this);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002571 PP->getHeaderSearchInfo().SetExternalSource(this);
2572
Douglas Gregor3747ee72010-10-01 01:18:02 +00002573 // If we have an update block for the TU waiting, we have to add it before
2574 // deserializing the decl.
2575 DeclContextOffsetsMap::iterator DCU = DeclContextOffsets.find(0);
2576 if (DCU != DeclContextOffsets.end()) {
2577 // Insertion could invalidate map, so grab vector.
2578 DeclContextInfos T;
2579 T.swap(DCU->second);
2580 DeclContextOffsets.erase(DCU);
2581 DeclContextOffsets[Ctx.getTranslationUnitDecl()].swap(T);
2582 }
2583
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002584 // Load the translation unit declaration
Argyrios Kyrtzidis8871a442010-07-08 17:13:02 +00002585 GetTranslationUnitDecl();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002586
2587 // Load the special types.
2588 Context->setBuiltinVaListType(
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002589 GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST]));
2590 if (unsigned Id = SpecialTypes[SPECIAL_TYPE_OBJC_ID])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002591 Context->setObjCIdType(GetType(Id));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002592 if (unsigned Sel = SpecialTypes[SPECIAL_TYPE_OBJC_SELECTOR])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002593 Context->setObjCSelType(GetType(Sel));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002594 if (unsigned Proto = SpecialTypes[SPECIAL_TYPE_OBJC_PROTOCOL])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002595 Context->setObjCProtoType(GetType(Proto));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002596 if (unsigned Class = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002597 Context->setObjCClassType(GetType(Class));
Steve Naroff14108da2009-07-10 23:34:53 +00002598
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002599 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002600 Context->setCFConstantStringType(GetType(String));
Mike Stump1eb44332009-09-09 15:08:12 +00002601 if (unsigned FastEnum
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002602 = SpecialTypes[SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002603 Context->setObjCFastEnumerationStateType(GetType(FastEnum));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002604 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002605 QualType FileType = GetType(File);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002606 if (FileType.isNull()) {
2607 Error("FILE type is NULL");
2608 return;
2609 }
John McCall183700f2009-09-21 23:43:11 +00002610 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002611 Context->setFILEDecl(Typedef->getDecl());
2612 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002613 const TagType *Tag = FileType->getAs<TagType>();
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002614 if (!Tag) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002615 Error("Invalid FILE type in AST file");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002616 return;
2617 }
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002618 Context->setFILEDecl(Tag->getDecl());
2619 }
2620 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002621 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_jmp_buf]) {
Mike Stump782fa302009-07-28 02:25:19 +00002622 QualType Jmp_bufType = GetType(Jmp_buf);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002623 if (Jmp_bufType.isNull()) {
2624 Error("jmp_bug type is NULL");
2625 return;
2626 }
John McCall183700f2009-09-21 23:43:11 +00002627 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
Mike Stump782fa302009-07-28 02:25:19 +00002628 Context->setjmp_bufDecl(Typedef->getDecl());
2629 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002630 const TagType *Tag = Jmp_bufType->getAs<TagType>();
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002631 if (!Tag) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002632 Error("Invalid jmp_buf type in AST file");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002633 return;
2634 }
Mike Stump782fa302009-07-28 02:25:19 +00002635 Context->setjmp_bufDecl(Tag->getDecl());
2636 }
2637 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002638 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_sigjmp_buf]) {
Mike Stump782fa302009-07-28 02:25:19 +00002639 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002640 if (Sigjmp_bufType.isNull()) {
2641 Error("sigjmp_buf type is NULL");
2642 return;
2643 }
John McCall183700f2009-09-21 23:43:11 +00002644 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
Mike Stump782fa302009-07-28 02:25:19 +00002645 Context->setsigjmp_bufDecl(Typedef->getDecl());
2646 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002647 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002648 assert(Tag && "Invalid sigjmp_buf type in AST file");
Mike Stump782fa302009-07-28 02:25:19 +00002649 Context->setsigjmp_bufDecl(Tag->getDecl());
2650 }
2651 }
Mike Stump1eb44332009-09-09 15:08:12 +00002652 if (unsigned ObjCIdRedef
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002653 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION])
Douglas Gregord1571ac2009-08-21 00:27:50 +00002654 Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef);
Mike Stump1eb44332009-09-09 15:08:12 +00002655 if (unsigned ObjCClassRedef
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002656 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION])
Douglas Gregord1571ac2009-08-21 00:27:50 +00002657 Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002658 if (unsigned String = SpecialTypes[SPECIAL_TYPE_BLOCK_DESCRIPTOR])
Mike Stumpadaaad32009-10-20 02:12:22 +00002659 Context->setBlockDescriptorType(GetType(String));
Mike Stump083c25e2009-10-22 00:49:09 +00002660 if (unsigned String
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002661 = SpecialTypes[SPECIAL_TYPE_BLOCK_EXTENDED_DESCRIPTOR])
Mike Stump083c25e2009-10-22 00:49:09 +00002662 Context->setBlockDescriptorExtendedType(GetType(String));
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002663 if (unsigned ObjCSelRedef
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002664 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION])
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002665 Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002666 if (unsigned String = SpecialTypes[SPECIAL_TYPE_NS_CONSTANT_STRING])
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002667 Context->setNSConstantStringType(GetType(String));
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +00002668
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002669 if (SpecialTypes[SPECIAL_TYPE_INT128_INSTALLED])
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +00002670 Context->setInt128Installed();
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002671
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002672 ReadPragmaDiagnosticMappings(Context->getDiagnostics());
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002673
2674 // If there were any CUDA special declarations, deserialize them.
2675 if (!CUDASpecialDeclRefs.empty()) {
2676 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
2677 Context->setcudaConfigureCallDecl(
2678 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
2679 }
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002680}
2681
Douglas Gregorb64c1932009-05-12 01:31:05 +00002682/// \brief Retrieve the name of the original source file name
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002683/// directly from the AST file, without actually loading the AST
Douglas Gregorb64c1932009-05-12 01:31:05 +00002684/// file.
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002685std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +00002686 FileManager &FileMgr,
Daniel Dunbar93ebb1b2009-12-03 09:13:06 +00002687 Diagnostic &Diags) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002688 // Open the AST file.
Douglas Gregorb64c1932009-05-12 01:31:05 +00002689 std::string ErrStr;
2690 llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
Chris Lattner39b49bc2010-11-23 08:35:12 +00002691 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
Douglas Gregorb64c1932009-05-12 01:31:05 +00002692 if (!Buffer) {
Daniel Dunbar93ebb1b2009-12-03 09:13:06 +00002693 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002694 return std::string();
2695 }
2696
2697 // Initialize the stream
2698 llvm::BitstreamReader StreamFile;
2699 llvm::BitstreamCursor Stream;
Mike Stump1eb44332009-09-09 15:08:12 +00002700 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregorb64c1932009-05-12 01:31:05 +00002701 (const unsigned char *)Buffer->getBufferEnd());
2702 Stream.init(StreamFile);
2703
2704 // Sniff for the signature.
2705 if (Stream.Read(8) != 'C' ||
2706 Stream.Read(8) != 'P' ||
2707 Stream.Read(8) != 'C' ||
2708 Stream.Read(8) != 'H') {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002709 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002710 return std::string();
2711 }
2712
2713 RecordData Record;
2714 while (!Stream.AtEndOfStream()) {
2715 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00002716
Douglas Gregorb64c1932009-05-12 01:31:05 +00002717 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2718 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump1eb44332009-09-09 15:08:12 +00002719
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002720 // We only know the AST subblock ID.
Douglas Gregorb64c1932009-05-12 01:31:05 +00002721 switch (BlockID) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002722 case AST_BLOCK_ID:
2723 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002724 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002725 return std::string();
2726 }
2727 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002728
Douglas Gregorb64c1932009-05-12 01:31:05 +00002729 default:
2730 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002731 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002732 return std::string();
2733 }
2734 break;
2735 }
2736 continue;
2737 }
2738
2739 if (Code == llvm::bitc::END_BLOCK) {
2740 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002741 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002742 return std::string();
2743 }
2744 continue;
2745 }
2746
2747 if (Code == llvm::bitc::DEFINE_ABBREV) {
2748 Stream.ReadAbbrevRecord();
2749 continue;
2750 }
2751
2752 Record.clear();
2753 const char *BlobStart = 0;
2754 unsigned BlobLen = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002755 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002756 == ORIGINAL_FILE_NAME)
Douglas Gregorb64c1932009-05-12 01:31:05 +00002757 return std::string(BlobStart, BlobLen);
Mike Stump1eb44332009-09-09 15:08:12 +00002758 }
Douglas Gregorb64c1932009-05-12 01:31:05 +00002759
2760 return std::string();
2761}
2762
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002763/// \brief Parse the record that corresponds to a LangOptions data
2764/// structure.
2765///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002766/// This routine parses the language options from the AST file and then gives
2767/// them to the AST listener if one is set.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002768///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002769/// \returns true if the listener deems the file unacceptable, false otherwise.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002770bool ASTReader::ParseLanguageOptions(
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002771 const llvm::SmallVectorImpl<uint64_t> &Record) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002772 if (Listener) {
2773 LangOptions LangOpts;
Mike Stump1eb44332009-09-09 15:08:12 +00002774
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002775 #define PARSE_LANGOPT(Option) \
2776 LangOpts.Option = Record[Idx]; \
2777 ++Idx
Mike Stump1eb44332009-09-09 15:08:12 +00002778
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002779 unsigned Idx = 0;
2780 PARSE_LANGOPT(Trigraphs);
2781 PARSE_LANGOPT(BCPLComment);
2782 PARSE_LANGOPT(DollarIdents);
2783 PARSE_LANGOPT(AsmPreprocessor);
2784 PARSE_LANGOPT(GNUMode);
Chandler Carrutheb5d7b72010-04-17 20:17:31 +00002785 PARSE_LANGOPT(GNUKeywords);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002786 PARSE_LANGOPT(ImplicitInt);
2787 PARSE_LANGOPT(Digraphs);
2788 PARSE_LANGOPT(HexFloats);
2789 PARSE_LANGOPT(C99);
2790 PARSE_LANGOPT(Microsoft);
2791 PARSE_LANGOPT(CPlusPlus);
2792 PARSE_LANGOPT(CPlusPlus0x);
2793 PARSE_LANGOPT(CXXOperatorNames);
2794 PARSE_LANGOPT(ObjC1);
2795 PARSE_LANGOPT(ObjC2);
2796 PARSE_LANGOPT(ObjCNonFragileABI);
Fariborz Jahanian412e7982010-02-09 19:31:38 +00002797 PARSE_LANGOPT(ObjCNonFragileABI2);
Fariborz Jahanianf84109e2011-01-07 18:59:25 +00002798 PARSE_LANGOPT(AppleKext);
Ted Kremenekc32647d2010-12-23 21:35:43 +00002799 PARSE_LANGOPT(ObjCDefaultSynthProperties);
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +00002800 PARSE_LANGOPT(NoConstantCFStrings);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002801 PARSE_LANGOPT(PascalStrings);
2802 PARSE_LANGOPT(WritableStrings);
2803 PARSE_LANGOPT(LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +00002804 PARSE_LANGOPT(AltiVec);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002805 PARSE_LANGOPT(Exceptions);
Anders Carlssonda4b7cf2011-02-19 23:53:54 +00002806 PARSE_LANGOPT(ObjCExceptions);
Anders Carlsson7da99b02011-02-23 03:04:54 +00002807 PARSE_LANGOPT(CXXExceptions);
2808 PARSE_LANGOPT(SjLjExceptions);
Douglas Gregor6f755502011-02-01 15:15:22 +00002809 PARSE_LANGOPT(MSBitfields);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002810 PARSE_LANGOPT(NeXTRuntime);
2811 PARSE_LANGOPT(Freestanding);
2812 PARSE_LANGOPT(NoBuiltin);
2813 PARSE_LANGOPT(ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +00002814 PARSE_LANGOPT(POSIXThreads);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002815 PARSE_LANGOPT(Blocks);
2816 PARSE_LANGOPT(EmitAllDecls);
2817 PARSE_LANGOPT(MathErrno);
Chris Lattnera4d71452010-06-26 21:25:03 +00002818 LangOpts.setSignedOverflowBehavior((LangOptions::SignedOverflowBehaviorTy)
2819 Record[Idx++]);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002820 PARSE_LANGOPT(HeinousExtensions);
2821 PARSE_LANGOPT(Optimize);
2822 PARSE_LANGOPT(OptimizeSize);
2823 PARSE_LANGOPT(Static);
2824 PARSE_LANGOPT(PICLevel);
2825 PARSE_LANGOPT(GNUInline);
2826 PARSE_LANGOPT(NoInline);
2827 PARSE_LANGOPT(AccessControl);
2828 PARSE_LANGOPT(CharIsSigned);
John Thompsona6fda122009-11-05 20:14:16 +00002829 PARSE_LANGOPT(ShortWChar);
Argyrios Kyrtzidisb1bdced2011-01-15 02:56:16 +00002830 PARSE_LANGOPT(ShortEnums);
Chris Lattnera4d71452010-06-26 21:25:03 +00002831 LangOpts.setGCMode((LangOptions::GCMode)Record[Idx++]);
John McCall1fb0caa2010-10-22 21:05:15 +00002832 LangOpts.setVisibilityMode((Visibility)Record[Idx++]);
Daniel Dunbarab8e2812009-09-21 04:16:19 +00002833 LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode)
Chris Lattnera4d71452010-06-26 21:25:03 +00002834 Record[Idx++]);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002835 PARSE_LANGOPT(InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +00002836 PARSE_LANGOPT(OpenCL);
Peter Collingbourne08a53262010-12-01 19:14:57 +00002837 PARSE_LANGOPT(CUDA);
Mike Stump9c276ae2009-12-12 01:27:46 +00002838 PARSE_LANGOPT(CatchUndefined);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002839 PARSE_LANGOPT(DefaultFPContract);
Roman Divacky1c51b1c2011-03-01 17:36:40 +00002840 PARSE_LANGOPT(ElideConstructors);
2841 PARSE_LANGOPT(SpellChecking);
Roman Divackycfe9af22011-03-01 17:40:53 +00002842 PARSE_LANGOPT(MRTD);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002843 #undef PARSE_LANGOPT
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002844
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002845 return Listener->ReadLanguageOptions(LangOpts);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002846 }
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002847
2848 return false;
2849}
2850
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002851void ASTReader::ReadPreprocessedEntities() {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002852 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2853 PerFileData &F = *Chain[I];
2854 if (!F.PreprocessorDetailCursor.getBitStreamReader())
2855 continue;
2856
2857 SavedStreamPosition SavedPosition(F.PreprocessorDetailCursor);
2858 F.PreprocessorDetailCursor.JumpToBit(F.PreprocessorDetailStartOffset);
2859 while (LoadPreprocessedEntity(F)) { }
2860 }
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002861}
2862
Douglas Gregor0a480292011-02-11 19:46:30 +00002863PreprocessedEntity *ASTReader::ReadPreprocessedEntityAtOffset(uint64_t Offset) {
Douglas Gregor89d99802010-11-30 06:16:57 +00002864 PerFileData *F = 0;
2865 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2866 if (Offset < Chain[I]->SizeInBits) {
2867 F = Chain[I];
2868 break;
2869 }
2870
2871 Offset -= Chain[I]->SizeInBits;
2872 }
2873
2874 if (!F) {
2875 Error("Malformed preprocessed entity offset");
2876 return 0;
2877 }
2878
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002879 // Keep track of where we are in the stream, then jump back there
2880 // after reading this entity.
2881 SavedStreamPosition SavedPosition(F->PreprocessorDetailCursor);
2882 F->PreprocessorDetailCursor.JumpToBit(Offset);
2883 return LoadPreprocessedEntity(*F);
Douglas Gregor89d99802010-11-30 06:16:57 +00002884}
2885
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002886HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
2887 HeaderFileInfoTrait Trait(FE->getName());
2888 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2889 PerFileData &F = *Chain[I];
2890 HeaderFileInfoLookupTable *Table
2891 = static_cast<HeaderFileInfoLookupTable *>(F.HeaderFileInfoTable);
2892 if (!Table)
2893 continue;
2894
2895 // Look in the on-disk hash table for an entry for this file name.
2896 HeaderFileInfoLookupTable::iterator Pos = Table->find(FE->getName(),
2897 &Trait);
2898 if (Pos == Table->end())
2899 continue;
2900
2901 HeaderFileInfo HFI = *Pos;
2902 if (Listener)
2903 Listener->ReadHeaderFileInfo(HFI, FE->getUID());
2904
2905 return HFI;
2906 }
2907
2908 return HeaderFileInfo();
2909}
2910
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002911void ASTReader::ReadPragmaDiagnosticMappings(Diagnostic &Diag) {
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002912 unsigned Idx = 0;
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002913 while (Idx < PragmaDiagMappings.size()) {
2914 SourceLocation
2915 Loc = SourceLocation::getFromRawEncoding(PragmaDiagMappings[Idx++]);
2916 while (1) {
2917 assert(Idx < PragmaDiagMappings.size() &&
2918 "Invalid data, didn't find '-1' marking end of diag/map pairs");
2919 if (Idx >= PragmaDiagMappings.size())
2920 break; // Something is messed up but at least avoid infinite loop in
2921 // release build.
2922 unsigned DiagID = PragmaDiagMappings[Idx++];
2923 if (DiagID == (unsigned)-1)
2924 break; // no more diag/map pairs for this location.
2925 diag::Mapping Map = (diag::Mapping)PragmaDiagMappings[Idx++];
2926 Diag.setDiagnosticMapping(DiagID, Map, Loc);
2927 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002928 }
2929}
2930
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002931/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002932ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002933 PerFileData *F = 0;
2934 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2935 F = Chain[N - I - 1];
2936 if (Index < F->LocalNumTypes)
2937 break;
2938 Index -= F->LocalNumTypes;
2939 }
2940 assert(F && F->LocalNumTypes > Index && "Broken chain");
Sebastian Redlc3632732010-10-05 15:59:54 +00002941 return RecordLocation(F, F->TypeOffsets[Index]);
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002942}
2943
2944/// \brief Read and return the type with the given index..
Douglas Gregor2cf26342009-04-09 22:27:44 +00002945///
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002946/// The index is the type ID, shifted and minus the number of predefs. This
2947/// routine actually reads the record corresponding to the type at the given
2948/// location. It is a helper routine for GetType, which deals with reading type
2949/// IDs.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002950QualType ASTReader::ReadTypeRecord(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002951 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00002952 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00002953
Douglas Gregor0b748912009-04-14 21:18:50 +00002954 // Keep track of where we are in the stream, then jump back there
2955 // after reading this type.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002956 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregor0b748912009-04-14 21:18:50 +00002957
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00002958 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redl27372b42010-08-11 18:52:41 +00002959
Douglas Gregord89275b2009-07-06 18:54:52 +00002960 // Note that we are loading a type record.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00002961 Deserializing AType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00002962
Sebastian Redlc3632732010-10-05 15:59:54 +00002963 DeclsCursor.JumpToBit(Loc.Offset);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002964 RecordData Record;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002965 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002966 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
2967 case TYPE_EXT_QUAL: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002968 if (Record.size() != 2) {
2969 Error("Incorrect encoding of extended qualifier type");
2970 return QualType();
2971 }
Douglas Gregor6d473962009-04-15 22:00:08 +00002972 QualType Base = GetType(Record[0]);
John McCall0953e762009-09-24 19:53:00 +00002973 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[1]);
2974 return Context->getQualifiedType(Base, Quals);
Douglas Gregor6d473962009-04-15 22:00:08 +00002975 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002976
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002977 case TYPE_COMPLEX: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002978 if (Record.size() != 1) {
2979 Error("Incorrect encoding of complex type");
2980 return QualType();
2981 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002982 QualType ElemType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002983 return Context->getComplexType(ElemType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002984 }
2985
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002986 case TYPE_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002987 if (Record.size() != 1) {
2988 Error("Incorrect encoding of pointer type");
2989 return QualType();
2990 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002991 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002992 return Context->getPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002993 }
2994
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002995 case TYPE_BLOCK_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002996 if (Record.size() != 1) {
2997 Error("Incorrect encoding of block pointer type");
2998 return QualType();
2999 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003000 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003001 return Context->getBlockPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003002 }
3003
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003004 case TYPE_LVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003005 if (Record.size() != 1) {
3006 Error("Incorrect encoding of lvalue reference type");
3007 return QualType();
3008 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003009 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003010 return Context->getLValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003011 }
3012
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003013 case TYPE_RVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003014 if (Record.size() != 1) {
3015 Error("Incorrect encoding of rvalue reference type");
3016 return QualType();
3017 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003018 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003019 return Context->getRValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003020 }
3021
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003022 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidis240437b2010-07-02 11:55:15 +00003023 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003024 Error("Incorrect encoding of member pointer type");
3025 return QualType();
3026 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003027 QualType PointeeType = GetType(Record[0]);
3028 QualType ClassType = GetType(Record[1]);
Douglas Gregor1ab55e92010-12-10 17:03:06 +00003029 if (PointeeType.isNull() || ClassType.isNull())
3030 return QualType();
3031
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003032 return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregor2cf26342009-04-09 22:27:44 +00003033 }
3034
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003035 case TYPE_CONSTANT_ARRAY: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003036 QualType ElementType = GetType(Record[0]);
3037 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3038 unsigned IndexTypeQuals = Record[2];
3039 unsigned Idx = 3;
3040 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003041 return Context->getConstantArrayType(ElementType, Size,
3042 ASM, IndexTypeQuals);
3043 }
3044
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003045 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003046 QualType ElementType = GetType(Record[0]);
3047 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3048 unsigned IndexTypeQuals = Record[2];
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003049 return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003050 }
3051
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003052 case TYPE_VARIABLE_ARRAY: {
Douglas Gregor0b748912009-04-14 21:18:50 +00003053 QualType ElementType = GetType(Record[0]);
3054 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3055 unsigned IndexTypeQuals = Record[2];
Sebastian Redlc3632732010-10-05 15:59:54 +00003056 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
3057 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
3058 return Context->getVariableArrayType(ElementType, ReadExpr(*Loc.F),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003059 ASM, IndexTypeQuals,
3060 SourceRange(LBLoc, RBLoc));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003061 }
3062
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003063 case TYPE_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00003064 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003065 Error("incorrect encoding of vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003066 return QualType();
3067 }
3068
3069 QualType ElementType = GetType(Record[0]);
3070 unsigned NumElements = Record[1];
Bob Wilsone86d78c2010-11-10 21:56:12 +00003071 unsigned VecKind = Record[2];
Chris Lattner788b0fd2010-06-23 06:00:24 +00003072 return Context->getVectorType(ElementType, NumElements,
Bob Wilsone86d78c2010-11-10 21:56:12 +00003073 (VectorType::VectorKind)VecKind);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003074 }
3075
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003076 case TYPE_EXT_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00003077 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003078 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003079 return QualType();
3080 }
3081
3082 QualType ElementType = GetType(Record[0]);
3083 unsigned NumElements = Record[1];
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003084 return Context->getExtVectorType(ElementType, NumElements);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003085 }
3086
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003087 case TYPE_FUNCTION_NO_PROTO: {
Rafael Espindola425ef722010-03-30 22:15:11 +00003088 if (Record.size() != 4) {
Douglas Gregora02b1472009-04-28 21:53:25 +00003089 Error("incorrect encoding of no-proto function type");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003090 return QualType();
3091 }
3092 QualType ResultType = GetType(Record[0]);
Rafael Espindola425ef722010-03-30 22:15:11 +00003093 FunctionType::ExtInfo Info(Record[1], Record[2], (CallingConv)Record[3]);
Rafael Espindola264ba482010-03-30 20:24:48 +00003094 return Context->getFunctionNoProtoType(ResultType, Info);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003095 }
3096
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003097 case TYPE_FUNCTION_PROTO: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003098 QualType ResultType = GetType(Record[0]);
John McCalle23cf432010-12-14 08:05:40 +00003099
3100 FunctionProtoType::ExtProtoInfo EPI;
3101 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
3102 /*regparm*/ Record[2],
3103 static_cast<CallingConv>(Record[3]));
3104
Rafael Espindola425ef722010-03-30 22:15:11 +00003105 unsigned Idx = 4;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003106 unsigned NumParams = Record[Idx++];
3107 llvm::SmallVector<QualType, 16> ParamTypes;
3108 for (unsigned I = 0; I != NumParams; ++I)
3109 ParamTypes.push_back(GetType(Record[Idx++]));
John McCalle23cf432010-12-14 08:05:40 +00003110
3111 EPI.Variadic = Record[Idx++];
3112 EPI.TypeQuals = Record[Idx++];
Douglas Gregorc938c162011-01-26 05:01:58 +00003113 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
John McCalle23cf432010-12-14 08:05:40 +00003114 EPI.HasExceptionSpec = Record[Idx++];
3115 EPI.HasAnyExceptionSpec = Record[Idx++];
3116 EPI.NumExceptions = Record[Idx++];
Sebastian Redl465226e2009-05-27 22:11:52 +00003117 llvm::SmallVector<QualType, 2> Exceptions;
John McCalle23cf432010-12-14 08:05:40 +00003118 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
Sebastian Redl465226e2009-05-27 22:11:52 +00003119 Exceptions.push_back(GetType(Record[Idx++]));
John McCalle23cf432010-12-14 08:05:40 +00003120 EPI.Exceptions = Exceptions.data();
Jay Foadbeaaccd2009-05-21 09:52:38 +00003121 return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams,
John McCalle23cf432010-12-14 08:05:40 +00003122 EPI);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003123 }
3124
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003125 case TYPE_UNRESOLVED_USING:
John McCalled976492009-12-04 22:46:56 +00003126 return Context->getTypeDeclType(
3127 cast<UnresolvedUsingTypenameDecl>(GetDecl(Record[0])));
3128
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003129 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003130 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003131 Error("incorrect encoding of typedef type");
3132 return QualType();
3133 }
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003134 TypedefDecl *Decl = cast<TypedefDecl>(GetDecl(Record[0]));
3135 QualType Canonical = GetType(Record[1]);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00003136 if (!Canonical.isNull())
3137 Canonical = Context->getCanonicalType(Canonical);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003138 return Context->getTypedefType(Decl, Canonical);
3139 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003140
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003141 case TYPE_TYPEOF_EXPR:
Sebastian Redlc3632732010-10-05 15:59:54 +00003142 return Context->getTypeOfExprType(ReadExpr(*Loc.F));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003143
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003144 case TYPE_TYPEOF: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003145 if (Record.size() != 1) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003146 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003147 return QualType();
3148 }
3149 QualType UnderlyingType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003150 return Context->getTypeOfType(UnderlyingType);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003151 }
Mike Stump1eb44332009-09-09 15:08:12 +00003152
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003153 case TYPE_DECLTYPE:
Sebastian Redlc3632732010-10-05 15:59:54 +00003154 return Context->getDecltypeType(ReadExpr(*Loc.F));
Anders Carlsson395b4752009-06-24 19:06:50 +00003155
Richard Smith34b41d92011-02-20 03:19:35 +00003156 case TYPE_AUTO:
3157 return Context->getAutoType(GetType(Record[0]));
3158
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003159 case TYPE_RECORD: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003160 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003161 Error("incorrect encoding of record type");
3162 return QualType();
3163 }
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003164 bool IsDependent = Record[0];
3165 QualType T = Context->getRecordType(cast<RecordDecl>(GetDecl(Record[1])));
John McCallf4c73712011-01-19 06:33:43 +00003166 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003167 return T;
3168 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003169
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003170 case TYPE_ENUM: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003171 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003172 Error("incorrect encoding of enum type");
3173 return QualType();
3174 }
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003175 bool IsDependent = Record[0];
3176 QualType T = Context->getEnumType(cast<EnumDecl>(GetDecl(Record[1])));
John McCallf4c73712011-01-19 06:33:43 +00003177 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003178 return T;
3179 }
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003180
John McCall9d156a72011-01-06 01:58:22 +00003181 case TYPE_ATTRIBUTED: {
3182 if (Record.size() != 3) {
3183 Error("incorrect encoding of attributed type");
3184 return QualType();
3185 }
3186 QualType modifiedType = GetType(Record[0]);
3187 QualType equivalentType = GetType(Record[1]);
3188 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
3189 return Context->getAttributedType(kind, modifiedType, equivalentType);
3190 }
3191
Abramo Bagnara075f8f12010-12-10 16:29:40 +00003192 case TYPE_PAREN: {
3193 if (Record.size() != 1) {
3194 Error("incorrect encoding of paren type");
3195 return QualType();
3196 }
3197 QualType InnerType = GetType(Record[0]);
3198 return Context->getParenType(InnerType);
3199 }
3200
Douglas Gregor7536dd52010-12-20 02:24:11 +00003201 case TYPE_PACK_EXPANSION: {
Douglas Gregorf9997a02011-02-01 15:24:58 +00003202 if (Record.size() != 2) {
Douglas Gregor7536dd52010-12-20 02:24:11 +00003203 Error("incorrect encoding of pack expansion type");
3204 return QualType();
3205 }
3206 QualType Pattern = GetType(Record[0]);
3207 if (Pattern.isNull())
3208 return QualType();
Douglas Gregorcded4f62011-01-14 17:04:44 +00003209 llvm::Optional<unsigned> NumExpansions;
3210 if (Record[1])
3211 NumExpansions = Record[1] - 1;
3212 return Context->getPackExpansionType(Pattern, NumExpansions);
Douglas Gregor7536dd52010-12-20 02:24:11 +00003213 }
3214
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003215 case TYPE_ELABORATED: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00003216 unsigned Idx = 0;
3217 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3218 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3219 QualType NamedType = GetType(Record[Idx++]);
3220 return Context->getElaboratedType(Keyword, NNS, NamedType);
John McCall7da24312009-09-05 00:15:47 +00003221 }
3222
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003223 case TYPE_OBJC_INTERFACE: {
Chris Lattnerc6fa4452009-04-22 06:45:28 +00003224 unsigned Idx = 0;
3225 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
John McCallc12c5bb2010-05-15 11:32:37 +00003226 return Context->getObjCInterfaceType(ItfD);
3227 }
3228
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003229 case TYPE_OBJC_OBJECT: {
John McCallc12c5bb2010-05-15 11:32:37 +00003230 unsigned Idx = 0;
3231 QualType Base = GetType(Record[Idx++]);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00003232 unsigned NumProtos = Record[Idx++];
3233 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
3234 for (unsigned I = 0; I != NumProtos; ++I)
3235 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
Michael J. Spencer20249a12010-10-21 03:16:25 +00003236 return Context->getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00003237 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003238
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003239 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00003240 unsigned Idx = 0;
John McCallc12c5bb2010-05-15 11:32:37 +00003241 QualType Pointee = GetType(Record[Idx++]);
3242 return Context->getObjCObjectPointerType(Pointee);
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00003243 }
Argyrios Kyrtzidis24fab412009-09-29 19:42:55 +00003244
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003245 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCall49a832b2009-10-18 09:09:24 +00003246 unsigned Idx = 0;
3247 QualType Parm = GetType(Record[Idx++]);
3248 QualType Replacement = GetType(Record[Idx++]);
3249 return
3250 Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
3251 Replacement);
3252 }
John McCall3cb0ebd2010-03-10 03:28:59 +00003253
Douglas Gregorc3069d62011-01-14 02:55:32 +00003254 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
3255 unsigned Idx = 0;
3256 QualType Parm = GetType(Record[Idx++]);
3257 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
3258 return Context->getSubstTemplateTypeParmPackType(
3259 cast<TemplateTypeParmType>(Parm),
3260 ArgPack);
3261 }
3262
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003263 case TYPE_INJECTED_CLASS_NAME: {
John McCall3cb0ebd2010-03-10 03:28:59 +00003264 CXXRecordDecl *D = cast<CXXRecordDecl>(GetDecl(Record[0]));
3265 QualType TST = GetType(Record[1]); // probably derivable
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00003266 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003267 // for AST reading, too much interdependencies.
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00003268 return
3269 QualType(new (*Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCall3cb0ebd2010-03-10 03:28:59 +00003270 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003271
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003272 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003273 unsigned Idx = 0;
3274 unsigned Depth = Record[Idx++];
3275 unsigned Index = Record[Idx++];
3276 bool Pack = Record[Idx++];
3277 IdentifierInfo *Name = GetIdentifierInfo(Record, Idx);
3278 return Context->getTemplateTypeParmType(Depth, Index, Pack, Name);
3279 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003280
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003281 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00003282 unsigned Idx = 0;
3283 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3284 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3285 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +00003286 QualType Canon = GetType(Record[Idx++]);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00003287 if (!Canon.isNull())
3288 Canon = Context->getCanonicalType(Canon);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +00003289 return Context->getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00003290 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003291
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003292 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00003293 unsigned Idx = 0;
3294 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3295 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3296 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
3297 unsigned NumArgs = Record[Idx++];
3298 llvm::SmallVector<TemplateArgument, 8> Args;
3299 Args.reserve(NumArgs);
3300 while (NumArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00003301 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00003302 return Context->getDependentTemplateSpecializationType(Keyword, NNS, Name,
3303 Args.size(), Args.data());
3304 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003305
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003306 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00003307 unsigned Idx = 0;
3308
3309 // ArrayType
3310 QualType ElementType = GetType(Record[Idx++]);
3311 ArrayType::ArraySizeModifier ASM
3312 = (ArrayType::ArraySizeModifier)Record[Idx++];
3313 unsigned IndexTypeQuals = Record[Idx++];
3314
3315 // DependentSizedArrayType
Sebastian Redlc3632732010-10-05 15:59:54 +00003316 Expr *NumElts = ReadExpr(*Loc.F);
3317 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00003318
3319 return Context->getDependentSizedArrayType(ElementType, NumElts, ASM,
3320 IndexTypeQuals, Brackets);
3321 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003322
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003323 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003324 unsigned Idx = 0;
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003325 bool IsDependent = Record[Idx++];
Douglas Gregor1aee05d2011-01-15 06:45:20 +00003326 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003327 llvm::SmallVector<TemplateArgument, 8> Args;
Sebastian Redlc3632732010-10-05 15:59:54 +00003328 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003329 QualType Canon = GetType(Record[Idx++]);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003330 QualType T;
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003331 if (Canon.isNull())
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003332 T = Context->getCanonicalTemplateSpecializationType(Name, Args.data(),
3333 Args.size());
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003334 else
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003335 T = Context->getTemplateSpecializationType(Name, Args.data(),
3336 Args.size(), Canon);
John McCallf4c73712011-01-19 06:33:43 +00003337 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003338 return T;
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003339 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003340 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003341 // Suppress a GCC warning
3342 return QualType();
3343}
3344
Sebastian Redlc3632732010-10-05 15:59:54 +00003345class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003346 ASTReader &Reader;
Sebastian Redlc3632732010-10-05 15:59:54 +00003347 ASTReader::PerFileData &F;
Sebastian Redl577d4792010-07-22 22:43:28 +00003348 llvm::BitstreamCursor &DeclsCursor;
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003349 const ASTReader::RecordData &Record;
John McCalla1ee0c52009-10-16 21:56:05 +00003350 unsigned &Idx;
3351
Sebastian Redlc3632732010-10-05 15:59:54 +00003352 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
3353 unsigned &I) {
3354 return Reader.ReadSourceLocation(F, R, I);
3355 }
3356
John McCalla1ee0c52009-10-16 21:56:05 +00003357public:
Sebastian Redlc3632732010-10-05 15:59:54 +00003358 TypeLocReader(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003359 const ASTReader::RecordData &Record, unsigned &Idx)
Sebastian Redlc3632732010-10-05 15:59:54 +00003360 : Reader(Reader), F(F), DeclsCursor(F.DeclsCursor), Record(Record), Idx(Idx)
3361 { }
John McCalla1ee0c52009-10-16 21:56:05 +00003362
John McCall51bd8032009-10-18 01:05:36 +00003363 // We want compile-time assurance that we've enumerated all of
3364 // these, so unfortunately we have to declare them first, then
3365 // define them out-of-line.
3366#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +00003367#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +00003368 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00003369#include "clang/AST/TypeLocNodes.def"
3370
John McCall51bd8032009-10-18 01:05:36 +00003371 void VisitFunctionTypeLoc(FunctionTypeLoc);
3372 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00003373};
3374
John McCall51bd8032009-10-18 01:05:36 +00003375void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCalla1ee0c52009-10-16 21:56:05 +00003376 // nothing to do
3377}
John McCall51bd8032009-10-18 01:05:36 +00003378void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003379 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
Douglas Gregorddf889a2010-01-18 18:04:31 +00003380 if (TL.needsExtraLocalData()) {
3381 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
3382 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
3383 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
3384 TL.setModeAttr(Record[Idx++]);
3385 }
John McCalla1ee0c52009-10-16 21:56:05 +00003386}
John McCall51bd8032009-10-18 01:05:36 +00003387void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003388 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003389}
John McCall51bd8032009-10-18 01:05:36 +00003390void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003391 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003392}
John McCall51bd8032009-10-18 01:05:36 +00003393void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003394 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003395}
John McCall51bd8032009-10-18 01:05:36 +00003396void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003397 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003398}
John McCall51bd8032009-10-18 01:05:36 +00003399void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003400 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003401}
John McCall51bd8032009-10-18 01:05:36 +00003402void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003403 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003404}
John McCall51bd8032009-10-18 01:05:36 +00003405void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003406 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
3407 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003408 if (Record[Idx++])
Sebastian Redlc3632732010-10-05 15:59:54 +00003409 TL.setSizeExpr(Reader.ReadExpr(F));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003410 else
John McCall51bd8032009-10-18 01:05:36 +00003411 TL.setSizeExpr(0);
3412}
3413void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
3414 VisitArrayTypeLoc(TL);
3415}
3416void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
3417 VisitArrayTypeLoc(TL);
3418}
3419void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
3420 VisitArrayTypeLoc(TL);
3421}
3422void TypeLocReader::VisitDependentSizedArrayTypeLoc(
3423 DependentSizedArrayTypeLoc TL) {
3424 VisitArrayTypeLoc(TL);
3425}
3426void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
3427 DependentSizedExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003428 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003429}
3430void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003431 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003432}
3433void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003434 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003435}
3436void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003437 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3438 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
Douglas Gregordab60ad2010-10-01 18:44:50 +00003439 TL.setTrailingReturn(Record[Idx++]);
John McCall51bd8032009-10-18 01:05:36 +00003440 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
John McCall86acc2a2009-10-23 01:28:53 +00003441 TL.setArg(i, cast_or_null<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
John McCall51bd8032009-10-18 01:05:36 +00003442 }
3443}
3444void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
3445 VisitFunctionTypeLoc(TL);
3446}
3447void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
3448 VisitFunctionTypeLoc(TL);
3449}
John McCalled976492009-12-04 22:46:56 +00003450void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003451 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalled976492009-12-04 22:46:56 +00003452}
John McCall51bd8032009-10-18 01:05:36 +00003453void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003454 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003455}
3456void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003457 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3458 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3459 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003460}
3461void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003462 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3463 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3464 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3465 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003466}
3467void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003468 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003469}
Richard Smith34b41d92011-02-20 03:19:35 +00003470void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
3471 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3472}
John McCall51bd8032009-10-18 01:05:36 +00003473void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003474 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003475}
3476void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003477 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003478}
John McCall9d156a72011-01-06 01:58:22 +00003479void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
3480 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
3481 if (TL.hasAttrOperand()) {
3482 SourceRange range;
3483 range.setBegin(ReadSourceLocation(Record, Idx));
3484 range.setEnd(ReadSourceLocation(Record, Idx));
3485 TL.setAttrOperandParensRange(range);
3486 }
3487 if (TL.hasAttrExprOperand()) {
3488 if (Record[Idx++])
3489 TL.setAttrExprOperand(Reader.ReadExpr(F));
3490 else
3491 TL.setAttrExprOperand(0);
3492 } else if (TL.hasAttrEnumOperand())
3493 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
3494}
John McCall51bd8032009-10-18 01:05:36 +00003495void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003496 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003497}
John McCall49a832b2009-10-18 09:09:24 +00003498void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
3499 SubstTemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003500 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall49a832b2009-10-18 09:09:24 +00003501}
Douglas Gregorc3069d62011-01-14 02:55:32 +00003502void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
3503 SubstTemplateTypeParmPackTypeLoc TL) {
3504 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3505}
John McCall51bd8032009-10-18 01:05:36 +00003506void TypeLocReader::VisitTemplateSpecializationTypeLoc(
3507 TemplateSpecializationTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003508 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
3509 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3510 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall833ca992009-10-29 08:12:44 +00003511 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
3512 TL.setArgLocInfo(i,
Sebastian Redlc3632732010-10-05 15:59:54 +00003513 Reader.GetTemplateArgumentLocInfo(F,
3514 TL.getTypePtr()->getArg(i).getKind(),
3515 Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003516}
Abramo Bagnara075f8f12010-12-10 16:29:40 +00003517void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
3518 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3519 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3520}
Abramo Bagnara465d41b2010-05-11 21:36:43 +00003521void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003522 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor9e876872011-03-01 18:12:44 +00003523 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003524}
John McCall3cb0ebd2010-03-10 03:28:59 +00003525void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003526 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall3cb0ebd2010-03-10 03:28:59 +00003527}
Douglas Gregor4714c122010-03-31 17:34:00 +00003528void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003529 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor2494dd02011-03-01 01:34:45 +00003530 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00003531 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003532}
John McCall33500952010-06-11 00:33:02 +00003533void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
3534 DependentTemplateSpecializationTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003535 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor94fdffa2011-03-01 20:11:18 +00003536 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00003537 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3538 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3539 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall33500952010-06-11 00:33:02 +00003540 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
3541 TL.setArgLocInfo(I,
Sebastian Redlc3632732010-10-05 15:59:54 +00003542 Reader.GetTemplateArgumentLocInfo(F,
3543 TL.getTypePtr()->getArg(I).getKind(),
3544 Record, Idx));
John McCall33500952010-06-11 00:33:02 +00003545}
Douglas Gregor7536dd52010-12-20 02:24:11 +00003546void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
3547 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
3548}
John McCall51bd8032009-10-18 01:05:36 +00003549void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003550 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallc12c5bb2010-05-15 11:32:37 +00003551}
3552void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
3553 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00003554 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3555 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003556 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Sebastian Redlc3632732010-10-05 15:59:54 +00003557 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003558}
John McCall54e14c42009-10-22 22:37:11 +00003559void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003560 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall54e14c42009-10-22 22:37:11 +00003561}
John McCalla1ee0c52009-10-16 21:56:05 +00003562
Sebastian Redlc3632732010-10-05 15:59:54 +00003563TypeSourceInfo *ASTReader::GetTypeSourceInfo(PerFileData &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00003564 const RecordData &Record,
John McCalla1ee0c52009-10-16 21:56:05 +00003565 unsigned &Idx) {
3566 QualType InfoTy = GetType(Record[Idx++]);
3567 if (InfoTy.isNull())
3568 return 0;
3569
John McCalla93c9342009-12-07 02:54:59 +00003570 TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy);
Sebastian Redlc3632732010-10-05 15:59:54 +00003571 TypeLocReader TLR(*this, F, Record, Idx);
John McCalla93c9342009-12-07 02:54:59 +00003572 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCalla1ee0c52009-10-16 21:56:05 +00003573 TLR.Visit(TL);
John McCalla93c9342009-12-07 02:54:59 +00003574 return TInfo;
John McCalla1ee0c52009-10-16 21:56:05 +00003575}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003576
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003577QualType ASTReader::GetType(TypeID ID) {
John McCall0953e762009-09-24 19:53:00 +00003578 unsigned FastQuals = ID & Qualifiers::FastMask;
3579 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003580
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003581 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003582 QualType T;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003583 switch ((PredefinedTypeIDs)Index) {
3584 case PREDEF_TYPE_NULL_ID: return QualType();
3585 case PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break;
3586 case PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003587
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003588 case PREDEF_TYPE_CHAR_U_ID:
3589 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregor2cf26342009-04-09 22:27:44 +00003590 // FIXME: Check that the signedness of CharTy is correct!
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003591 T = Context->CharTy;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003592 break;
3593
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003594 case PREDEF_TYPE_UCHAR_ID: T = Context->UnsignedCharTy; break;
3595 case PREDEF_TYPE_USHORT_ID: T = Context->UnsignedShortTy; break;
3596 case PREDEF_TYPE_UINT_ID: T = Context->UnsignedIntTy; break;
3597 case PREDEF_TYPE_ULONG_ID: T = Context->UnsignedLongTy; break;
3598 case PREDEF_TYPE_ULONGLONG_ID: T = Context->UnsignedLongLongTy; break;
3599 case PREDEF_TYPE_UINT128_ID: T = Context->UnsignedInt128Ty; break;
3600 case PREDEF_TYPE_SCHAR_ID: T = Context->SignedCharTy; break;
3601 case PREDEF_TYPE_WCHAR_ID: T = Context->WCharTy; break;
3602 case PREDEF_TYPE_SHORT_ID: T = Context->ShortTy; break;
3603 case PREDEF_TYPE_INT_ID: T = Context->IntTy; break;
3604 case PREDEF_TYPE_LONG_ID: T = Context->LongTy; break;
3605 case PREDEF_TYPE_LONGLONG_ID: T = Context->LongLongTy; break;
3606 case PREDEF_TYPE_INT128_ID: T = Context->Int128Ty; break;
3607 case PREDEF_TYPE_FLOAT_ID: T = Context->FloatTy; break;
3608 case PREDEF_TYPE_DOUBLE_ID: T = Context->DoubleTy; break;
3609 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy; break;
3610 case PREDEF_TYPE_OVERLOAD_ID: T = Context->OverloadTy; break;
3611 case PREDEF_TYPE_DEPENDENT_ID: T = Context->DependentTy; break;
3612 case PREDEF_TYPE_NULLPTR_ID: T = Context->NullPtrTy; break;
3613 case PREDEF_TYPE_CHAR16_ID: T = Context->Char16Ty; break;
3614 case PREDEF_TYPE_CHAR32_ID: T = Context->Char32Ty; break;
3615 case PREDEF_TYPE_OBJC_ID: T = Context->ObjCBuiltinIdTy; break;
3616 case PREDEF_TYPE_OBJC_CLASS: T = Context->ObjCBuiltinClassTy; break;
3617 case PREDEF_TYPE_OBJC_SEL: T = Context->ObjCBuiltinSelTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003618 }
3619
3620 assert(!T.isNull() && "Unknown predefined type");
John McCall0953e762009-09-24 19:53:00 +00003621 return T.withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003622 }
3623
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003624 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003625 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl07a353c2010-07-14 20:26:45 +00003626 if (TypesLoaded[Index].isNull()) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003627 TypesLoaded[Index] = ReadTypeRecord(Index);
Douglas Gregor97475832010-10-05 18:37:06 +00003628 if (TypesLoaded[Index].isNull())
3629 return QualType();
3630
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003631 TypesLoaded[Index]->setFromAST();
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003632 TypeIdxs[TypesLoaded[Index]] = TypeIdx::fromTypeID(ID);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003633 if (DeserializationListener)
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003634 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1476ed42010-07-16 16:36:56 +00003635 TypesLoaded[Index]);
Sebastian Redl07a353c2010-07-14 20:26:45 +00003636 }
Mike Stump1eb44332009-09-09 15:08:12 +00003637
John McCall0953e762009-09-24 19:53:00 +00003638 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003639}
3640
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003641TypeID ASTReader::GetTypeID(QualType T) const {
3642 return MakeTypeID(T,
3643 std::bind1st(std::mem_fun(&ASTReader::GetTypeIdx), this));
3644}
3645
3646TypeIdx ASTReader::GetTypeIdx(QualType T) const {
3647 if (T.isNull())
3648 return TypeIdx();
3649 assert(!T.getLocalFastQualifiers());
3650
3651 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3652 // GetTypeIdx is mostly used for computing the hash of DeclarationNames and
3653 // comparing keys of ASTDeclContextNameLookupTable.
3654 // If the type didn't come from the AST file use a specially marked index
3655 // so that any hash/key comparison fail since no such index is stored
3656 // in a AST file.
3657 if (I == TypeIdxs.end())
3658 return TypeIdx(-1);
3659 return I->second;
3660}
3661
Douglas Gregor7c789c12010-10-29 22:39:52 +00003662unsigned ASTReader::getTotalNumCXXBaseSpecifiers() const {
3663 unsigned Result = 0;
3664 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
3665 Result += Chain[I]->LocalNumCXXBaseSpecifiers;
3666
3667 return Result;
3668}
3669
John McCall833ca992009-10-29 08:12:44 +00003670TemplateArgumentLocInfo
Sebastian Redlc3632732010-10-05 15:59:54 +00003671ASTReader::GetTemplateArgumentLocInfo(PerFileData &F,
3672 TemplateArgument::ArgKind Kind,
John McCall833ca992009-10-29 08:12:44 +00003673 const RecordData &Record,
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00003674 unsigned &Index) {
John McCall833ca992009-10-29 08:12:44 +00003675 switch (Kind) {
3676 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00003677 return ReadExpr(F);
John McCall833ca992009-10-29 08:12:44 +00003678 case TemplateArgument::Type:
Sebastian Redlc3632732010-10-05 15:59:54 +00003679 return GetTypeSourceInfo(F, Record, Index);
Douglas Gregor788cd062009-11-11 01:00:40 +00003680 case TemplateArgument::Template: {
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003681 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
3682 Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00003683 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003684 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregora7fc9012011-01-05 18:58:31 +00003685 SourceLocation());
3686 }
3687 case TemplateArgument::TemplateExpansion: {
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003688 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
3689 Index);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003690 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorba68eca2011-01-05 17:40:24 +00003691 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003692 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregorba68eca2011-01-05 17:40:24 +00003693 EllipsisLoc);
Douglas Gregor788cd062009-11-11 01:00:40 +00003694 }
John McCall833ca992009-10-29 08:12:44 +00003695 case TemplateArgument::Null:
3696 case TemplateArgument::Integral:
3697 case TemplateArgument::Declaration:
3698 case TemplateArgument::Pack:
3699 return TemplateArgumentLocInfo();
3700 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00003701 llvm_unreachable("unexpected template argument loc");
John McCall833ca992009-10-29 08:12:44 +00003702 return TemplateArgumentLocInfo();
3703}
3704
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003705TemplateArgumentLoc
Sebastian Redlc3632732010-10-05 15:59:54 +00003706ASTReader::ReadTemplateArgumentLoc(PerFileData &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00003707 const RecordData &Record, unsigned &Index) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003708 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003709
3710 if (Arg.getKind() == TemplateArgument::Expression) {
3711 if (Record[Index++]) // bool InfoHasSameExpr.
3712 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
3713 }
Sebastian Redlc3632732010-10-05 15:59:54 +00003714 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00003715 Record, Index));
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003716}
3717
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003718Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall76bd1f32010-06-01 09:23:16 +00003719 return GetDecl(ID);
3720}
3721
Douglas Gregor7c789c12010-10-29 22:39:52 +00003722uint64_t
3723ASTReader::GetCXXBaseSpecifiersOffset(serialization::CXXBaseSpecifiersID ID) {
3724 if (ID == 0)
3725 return 0;
3726
3727 --ID;
3728 uint64_t Offset = 0;
3729 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3730 if (ID < Chain[I]->LocalNumCXXBaseSpecifiers)
3731 return Offset + Chain[I]->CXXBaseSpecifiersOffsets[ID];
3732
3733 ID -= Chain[I]->LocalNumCXXBaseSpecifiers;
3734 Offset += Chain[I]->SizeInBits;
3735 }
3736
3737 assert(false && "CXXBaseSpecifiers not found");
3738 return 0;
3739}
3740
3741CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
3742 // Figure out which AST file contains this offset.
3743 PerFileData *F = 0;
3744 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3745 if (Offset < Chain[I]->SizeInBits) {
3746 F = Chain[I];
3747 break;
3748 }
3749
3750 Offset -= Chain[I]->SizeInBits;
3751 }
3752
3753 if (!F) {
3754 Error("Malformed AST file: C++ base specifiers at impossible offset");
3755 return 0;
3756 }
3757
3758 llvm::BitstreamCursor &Cursor = F->DeclsCursor;
3759 SavedStreamPosition SavedPosition(Cursor);
3760 Cursor.JumpToBit(Offset);
3761 ReadingKindTracker ReadingKind(Read_Decl, *this);
3762 RecordData Record;
3763 unsigned Code = Cursor.ReadCode();
3764 unsigned RecCode = Cursor.ReadRecord(Code, Record);
3765 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
3766 Error("Malformed AST file: missing C++ base specifiers");
3767 return 0;
3768 }
3769
3770 unsigned Idx = 0;
3771 unsigned NumBases = Record[Idx++];
3772 void *Mem = Context->Allocate(sizeof(CXXBaseSpecifier) * NumBases);
3773 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
3774 for (unsigned I = 0; I != NumBases; ++I)
3775 Bases[I] = ReadCXXBaseSpecifier(*F, Record, Idx);
3776 return Bases;
3777}
3778
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003779TranslationUnitDecl *ASTReader::GetTranslationUnitDecl() {
Sebastian Redl30c514c2010-07-14 23:45:08 +00003780 if (!DeclsLoaded[0]) {
Sebastian Redle1dde812010-08-24 00:50:04 +00003781 ReadDeclRecord(0, 1);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003782 if (DeserializationListener)
Sebastian Redl1476ed42010-07-16 16:36:56 +00003783 DeserializationListener->DeclRead(1, DeclsLoaded[0]);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003784 }
Argyrios Kyrtzidis8871a442010-07-08 17:13:02 +00003785
3786 return cast<TranslationUnitDecl>(DeclsLoaded[0]);
3787}
3788
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003789Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003790 if (ID == 0)
3791 return 0;
3792
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003793 if (ID > DeclsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003794 Error("declaration ID out-of-range for AST file");
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003795 return 0;
3796 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003797
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003798 unsigned Index = ID - 1;
Sebastian Redl30c514c2010-07-14 23:45:08 +00003799 if (!DeclsLoaded[Index]) {
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00003800 ReadDeclRecord(Index, ID);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003801 if (DeserializationListener)
3802 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
3803 }
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003804
3805 return DeclsLoaded[Index];
Douglas Gregor2cf26342009-04-09 22:27:44 +00003806}
3807
Chris Lattner887e2b32009-04-27 05:46:25 +00003808/// \brief Resolve the offset of a statement into a statement.
3809///
3810/// This operation will read a new statement from the external
3811/// source each time it is called, and is meant to be used via a
3812/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003813Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00003814 // Switch case IDs are per Decl.
3815 ClearSwitchCaseIDs();
3816
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003817 // Offset here is a global offset across the entire chain.
3818 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3819 PerFileData &F = *Chain[N - I - 1];
3820 if (Offset < F.SizeInBits) {
3821 // Since we know that this statement is part of a decl, make sure to use
3822 // the decl cursor to read it.
3823 F.DeclsCursor.JumpToBit(Offset);
Sebastian Redlc3632732010-10-05 15:59:54 +00003824 return ReadStmtFromStream(F);
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003825 }
3826 Offset -= F.SizeInBits;
3827 }
3828 llvm_unreachable("Broken chain");
Douglas Gregor250fc9c2009-04-18 00:07:54 +00003829}
3830
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003831bool ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00003832 bool (*isKindWeWant)(Decl::Kind),
John McCall76bd1f32010-06-01 09:23:16 +00003833 llvm::SmallVectorImpl<Decl*> &Decls) {
Mike Stump1eb44332009-09-09 15:08:12 +00003834 assert(DC->hasExternalLexicalStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00003835 "DeclContext has no lexical decls in storage");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003836
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003837 // There might be lexical decls in multiple parts of the chain, for the TU
3838 // at least.
Sebastian Redl4a9eb262010-09-28 02:24:44 +00003839 // DeclContextOffsets might reallocate as we load additional decls below,
3840 // so make a copy of the vector.
3841 DeclContextInfos Infos = DeclContextOffsets[DC];
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003842 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3843 I != E; ++I) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003844 // IDs can be 0 if this context doesn't contain declarations.
3845 if (!I->LexicalDecls)
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003846 continue;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003847
3848 // Load all of the declaration IDs
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00003849 for (const KindDeclIDPair *ID = I->LexicalDecls,
3850 *IDE = ID + I->NumLexicalDecls; ID != IDE; ++ID) {
3851 if (isKindWeWant && !isKindWeWant((Decl::Kind)ID->first))
3852 continue;
3853
3854 Decl *D = GetDecl(ID->second);
Sebastian Redl4a9eb262010-09-28 02:24:44 +00003855 assert(D && "Null decl in lexical decls");
3856 Decls.push_back(D);
3857 }
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003858 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003859
Douglas Gregor25123082009-04-22 22:34:57 +00003860 ++NumLexicalDeclContextsRead;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003861 return false;
3862}
3863
John McCall76bd1f32010-06-01 09:23:16 +00003864DeclContext::lookup_result
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003865ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +00003866 DeclarationName Name) {
Mike Stump1eb44332009-09-09 15:08:12 +00003867 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00003868 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003869 if (!Name)
3870 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
3871 DeclContext::lookup_iterator(0));
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003872
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003873 llvm::SmallVector<NamedDecl *, 64> Decls;
Sebastian Redl8b122732010-08-24 00:49:55 +00003874 // There might be visible decls in multiple parts of the chain, for the TU
Sebastian Redl5967d622010-08-24 00:50:16 +00003875 // and namespaces. For any given name, the last available results replace
3876 // all earlier ones. For this reason, we walk in reverse.
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003877 DeclContextInfos &Infos = DeclContextOffsets[DC];
Sebastian Redl5967d622010-08-24 00:50:16 +00003878 for (DeclContextInfos::reverse_iterator I = Infos.rbegin(), E = Infos.rend();
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003879 I != E; ++I) {
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003880 if (!I->NameLookupTableData)
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003881 continue;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003882
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003883 ASTDeclContextNameLookupTable *LookupTable =
3884 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3885 ASTDeclContextNameLookupTable::iterator Pos = LookupTable->find(Name);
3886 if (Pos == LookupTable->end())
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003887 continue;
3888
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003889 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
3890 for (; Data.first != Data.second; ++Data.first)
3891 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
Sebastian Redl5967d622010-08-24 00:50:16 +00003892 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003893 }
3894
Douglas Gregor25123082009-04-22 22:34:57 +00003895 ++NumVisibleDeclContextsRead;
John McCall76bd1f32010-06-01 09:23:16 +00003896
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003897 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall76bd1f32010-06-01 09:23:16 +00003898 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003899}
3900
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +00003901void ASTReader::MaterializeVisibleDecls(const DeclContext *DC) {
3902 assert(DC->hasExternalVisibleStorage() &&
3903 "DeclContext has no visible decls in storage");
3904
3905 llvm::SmallVector<NamedDecl *, 64> Decls;
3906 // There might be visible decls in multiple parts of the chain, for the TU
3907 // and namespaces.
3908 DeclContextInfos &Infos = DeclContextOffsets[DC];
3909 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3910 I != E; ++I) {
3911 if (!I->NameLookupTableData)
3912 continue;
3913
3914 ASTDeclContextNameLookupTable *LookupTable =
3915 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3916 for (ASTDeclContextNameLookupTable::item_iterator
3917 ItemI = LookupTable->item_begin(),
3918 ItemEnd = LookupTable->item_end() ; ItemI != ItemEnd; ++ItemI) {
3919 ASTDeclContextNameLookupTable::item_iterator::value_type Val
3920 = *ItemI;
3921 ASTDeclContextNameLookupTrait::data_type Data = Val.second;
3922 Decls.clear();
3923 for (; Data.first != Data.second; ++Data.first)
3924 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
3925 MaterializeVisibleDeclsForName(DC, Val.first, Decls);
3926 }
3927 }
3928}
3929
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003930void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003931 assert(Consumer);
3932 while (!InterestingDecls.empty()) {
3933 DeclGroupRef DG(InterestingDecls.front());
3934 InterestingDecls.pop_front();
Sebastian Redl27372b42010-08-11 18:52:41 +00003935 Consumer->HandleInterestingDecl(DG);
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003936 }
3937}
3938
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003939void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor0af2ca42009-04-22 19:09:20 +00003940 this->Consumer = Consumer;
3941
Douglas Gregorfdd01722009-04-14 00:24:19 +00003942 if (!Consumer)
3943 return;
3944
3945 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003946 // Force deserialization of this decl, which will cause it to be queued for
3947 // passing to the consumer.
Daniel Dunbar04a0b502009-09-17 03:06:44 +00003948 GetDecl(ExternalDefinitions[I]);
Douglas Gregorfdd01722009-04-14 00:24:19 +00003949 }
Douglas Gregorc62a2fe2009-04-25 00:41:30 +00003950
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003951 PassInterestingDeclsToConsumer();
Douglas Gregorfdd01722009-04-14 00:24:19 +00003952}
3953
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003954void ASTReader::PrintStats() {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003955 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregor2cf26342009-04-09 22:27:44 +00003956
Mike Stump1eb44332009-09-09 15:08:12 +00003957 unsigned NumTypesLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003958 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall0953e762009-09-24 19:53:00 +00003959 QualType());
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003960 unsigned NumDeclsLoaded
3961 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
3962 (Decl *)0);
3963 unsigned NumIdentifiersLoaded
3964 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
3965 IdentifiersLoaded.end(),
3966 (IdentifierInfo *)0);
Mike Stump1eb44332009-09-09 15:08:12 +00003967 unsigned NumSelectorsLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003968 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
3969 SelectorsLoaded.end(),
3970 Selector());
Douglas Gregor2d41cc12009-04-13 20:50:16 +00003971
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003972 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
3973 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00003974 if (TotalNumSLocEntries)
3975 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
3976 NumSLocEntriesRead, TotalNumSLocEntries,
3977 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003978 if (!TypesLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003979 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003980 NumTypesLoaded, (unsigned)TypesLoaded.size(),
3981 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
3982 if (!DeclsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003983 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003984 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
3985 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003986 if (!IdentifiersLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003987 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003988 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
3989 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Sebastian Redl725cd962010-08-04 20:40:17 +00003990 if (!SelectorsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003991 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redl725cd962010-08-04 20:40:17 +00003992 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
3993 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor83941df2009-04-25 17:48:32 +00003994 if (TotalNumStatements)
3995 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
3996 NumStatementsRead, TotalNumStatements,
3997 ((float)NumStatementsRead/TotalNumStatements * 100));
3998 if (TotalNumMacros)
3999 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
4000 NumMacrosRead, TotalNumMacros,
4001 ((float)NumMacrosRead/TotalNumMacros * 100));
4002 if (TotalLexicalDeclContexts)
4003 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
4004 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
4005 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
4006 * 100));
4007 if (TotalVisibleDeclContexts)
4008 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
4009 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
4010 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
4011 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00004012 if (TotalNumMethodPoolEntries) {
Douglas Gregor83941df2009-04-25 17:48:32 +00004013 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redlfa78dec2010-08-04 21:22:45 +00004014 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
4015 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor83941df2009-04-25 17:48:32 +00004016 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00004017 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor83941df2009-04-25 17:48:32 +00004018 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00004019 std::fprintf(stderr, "\n");
4020}
4021
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004022void ASTReader::InitializeSema(Sema &S) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00004023 SemaObj = &S;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00004024 S.ExternalSource = this;
4025
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00004026 // Makes sure any declarations that were deserialized "too early"
4027 // still get added to the identifier's declaration chains.
Douglas Gregor76dc8892010-09-24 23:29:12 +00004028 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
4029 if (SemaObj->TUScope)
John McCalld226f652010-08-21 09:40:31 +00004030 SemaObj->TUScope->AddDecl(PreloadedDecls[I]);
Douglas Gregor76dc8892010-09-24 23:29:12 +00004031
4032 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregor668c1a42009-04-21 22:25:48 +00004033 }
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00004034 PreloadedDecls.clear();
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00004035
4036 // If there were any tentative definitions, deserialize them and add
Sebastian Redle9d12b62010-01-31 22:27:38 +00004037 // them to Sema's list of tentative definitions.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00004038 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
4039 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
Sebastian Redle9d12b62010-01-31 22:27:38 +00004040 SemaObj->TentativeDefinitions.push_back(Var);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00004041 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00004042
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00004043 // If there were any unused file scoped decls, deserialize them and add to
4044 // Sema's list of unused file scoped decls.
4045 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
4046 DeclaratorDecl *D = cast<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
4047 SemaObj->UnusedFileScopedDecls.push_back(D);
Tanya Lattnere6bbc012010-02-12 00:07:30 +00004048 }
Douglas Gregor14c22f22009-04-22 22:18:58 +00004049
4050 // If there were any locally-scoped external declarations,
4051 // deserialize them and add them to Sema's table of locally-scoped
4052 // external declarations.
4053 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
4054 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
4055 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
4056 }
Douglas Gregorb81c1702009-04-27 20:06:05 +00004057
4058 // If there were any ext_vector type declarations, deserialize them
4059 // and add them to Sema's vector of such declarations.
4060 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I)
4061 SemaObj->ExtVectorDecls.push_back(
4062 cast<TypedefDecl>(GetDecl(ExtVectorDecls[I])));
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004063
4064 // FIXME: Do VTable uses and dynamic classes deserialize too much ?
4065 // Can we cut them down before writing them ?
4066
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004067 // If there were any dynamic classes declarations, deserialize them
4068 // and add them to Sema's vector of such declarations.
4069 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I)
4070 SemaObj->DynamicClasses.push_back(
4071 cast<CXXRecordDecl>(GetDecl(DynamicClasses[I])));
Fariborz Jahanian32019832010-07-23 19:11:11 +00004072
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00004073 // Load the offsets of the declarations that Sema references.
4074 // They will be lazily deserialized when needed.
4075 if (!SemaDeclRefs.empty()) {
4076 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
4077 SemaObj->StdNamespace = SemaDeclRefs[0];
4078 SemaObj->StdBadAlloc = SemaDeclRefs[1];
4079 }
4080
Sebastian Redlc3632732010-10-05 15:59:54 +00004081 for (PerFileData *F = FirstInSource; F; F = F->NextInSource) {
4082
4083 // If there are @selector references added them to its pool. This is for
4084 // implementation of -Wselector.
4085 if (!F->ReferencedSelectorsData.empty()) {
4086 unsigned int DataSize = F->ReferencedSelectorsData.size()-1;
4087 unsigned I = 0;
4088 while (I < DataSize) {
4089 Selector Sel = DecodeSelector(F->ReferencedSelectorsData[I++]);
4090 SourceLocation SelLoc = ReadSourceLocation(
4091 *F, F->ReferencedSelectorsData, I);
4092 SemaObj->ReferencedSelectors.insert(std::make_pair(Sel, SelLoc));
4093 }
4094 }
4095
4096 // If there were any pending implicit instantiations, deserialize them
4097 // and add them to Sema's queue of such instantiations.
4098 assert(F->PendingInstantiations.size() % 2 == 0 &&
4099 "Expected pairs of entries");
4100 for (unsigned Idx = 0, N = F->PendingInstantiations.size(); Idx < N;) {
4101 ValueDecl *D=cast<ValueDecl>(GetDecl(F->PendingInstantiations[Idx++]));
4102 SourceLocation Loc = ReadSourceLocation(*F, F->PendingInstantiations,Idx);
4103 SemaObj->PendingInstantiations.push_back(std::make_pair(D, Loc));
4104 }
4105 }
4106
4107 // The two special data sets below always come from the most recent PCH,
4108 // which is at the front of the chain.
4109 PerFileData &F = *Chain.front();
4110
4111 // If there were any weak undeclared identifiers, deserialize them and add to
4112 // Sema's list of weak undeclared identifiers.
4113 if (!WeakUndeclaredIdentifiers.empty()) {
4114 unsigned Idx = 0;
4115 for (unsigned I = 0, N = WeakUndeclaredIdentifiers[Idx++]; I != N; ++I) {
4116 IdentifierInfo *WeakId = GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
4117 IdentifierInfo *AliasId= GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
4118 SourceLocation Loc = ReadSourceLocation(F, WeakUndeclaredIdentifiers,Idx);
4119 bool Used = WeakUndeclaredIdentifiers[Idx++];
4120 Sema::WeakInfo WI(AliasId, Loc);
4121 WI.setUsed(Used);
4122 SemaObj->WeakUndeclaredIdentifiers.insert(std::make_pair(WeakId, WI));
4123 }
4124 }
4125
4126 // If there were any VTable uses, deserialize the information and add it
4127 // to Sema's vector and map of VTable uses.
4128 if (!VTableUses.empty()) {
4129 unsigned Idx = 0;
4130 for (unsigned I = 0, N = VTableUses[Idx++]; I != N; ++I) {
4131 CXXRecordDecl *Class = cast<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
4132 SourceLocation Loc = ReadSourceLocation(F, VTableUses, Idx);
4133 bool DefinitionRequired = VTableUses[Idx++];
4134 SemaObj->VTableUses.push_back(std::make_pair(Class, Loc));
4135 SemaObj->VTablesUsed[Class] = DefinitionRequired;
Fariborz Jahanian32019832010-07-23 19:11:11 +00004136 }
4137 }
Peter Collingbourne84bccea2011-02-15 19:46:30 +00004138
4139 if (!FPPragmaOptions.empty()) {
4140 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
4141 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
4142 }
4143
4144 if (!OpenCLExtensions.empty()) {
4145 unsigned I = 0;
4146#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
4147#include "clang/Basic/OpenCLExtensions.def"
4148
4149 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
4150 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00004151}
4152
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004153IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Sebastian Redld8c5abb2010-08-02 18:30:12 +00004154 // Try to find this name within our on-disk hash tables. We start with the
4155 // most recent one, since that one contains the most up-to-date info.
Sebastian Redld27d3fc2010-07-21 22:31:37 +00004156 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004157 ASTIdentifierLookupTable *IdTable
4158 = (ASTIdentifierLookupTable *)Chain[I]->IdentifierLookupTable;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00004159 if (!IdTable)
4160 continue;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00004161 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004162 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00004163 if (Pos == IdTable->end())
4164 continue;
Douglas Gregor668c1a42009-04-21 22:25:48 +00004165
Sebastian Redld27d3fc2010-07-21 22:31:37 +00004166 // Dereferencing the iterator has the effect of building the
4167 // IdentifierInfo node and populating it with the various
4168 // declarations it needs.
Sebastian Redld8c5abb2010-08-02 18:30:12 +00004169 return *Pos;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00004170 }
Sebastian Redld8c5abb2010-08-02 18:30:12 +00004171 return 0;
Douglas Gregor668c1a42009-04-21 22:25:48 +00004172}
4173
Douglas Gregor95f42922010-10-14 22:11:03 +00004174namespace clang {
4175 /// \brief An identifier-lookup iterator that enumerates all of the
4176 /// identifiers stored within a set of AST files.
4177 class ASTIdentifierIterator : public IdentifierIterator {
4178 /// \brief The AST reader whose identifiers are being enumerated.
4179 const ASTReader &Reader;
4180
4181 /// \brief The current index into the chain of AST files stored in
4182 /// the AST reader.
4183 unsigned Index;
4184
4185 /// \brief The current position within the identifier lookup table
4186 /// of the current AST file.
4187 ASTIdentifierLookupTable::key_iterator Current;
4188
4189 /// \brief The end position within the identifier lookup table of
4190 /// the current AST file.
4191 ASTIdentifierLookupTable::key_iterator End;
4192
4193 public:
4194 explicit ASTIdentifierIterator(const ASTReader &Reader);
4195
4196 virtual llvm::StringRef Next();
4197 };
4198}
4199
4200ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
4201 : Reader(Reader), Index(Reader.Chain.size() - 1) {
4202 ASTIdentifierLookupTable *IdTable
4203 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
4204 Current = IdTable->key_begin();
4205 End = IdTable->key_end();
4206}
4207
4208llvm::StringRef ASTIdentifierIterator::Next() {
4209 while (Current == End) {
4210 // If we have exhausted all of our AST files, we're done.
4211 if (Index == 0)
4212 return llvm::StringRef();
4213
4214 --Index;
4215 ASTIdentifierLookupTable *IdTable
4216 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
4217 Current = IdTable->key_begin();
4218 End = IdTable->key_end();
4219 }
4220
4221 // We have any identifiers remaining in the current AST file; return
4222 // the next one.
4223 std::pair<const char*, unsigned> Key = *Current;
4224 ++Current;
4225 return llvm::StringRef(Key.first, Key.second);
4226}
4227
4228IdentifierIterator *ASTReader::getIdentifiers() const {
4229 return new ASTIdentifierIterator(*this);
4230}
4231
Mike Stump1eb44332009-09-09 15:08:12 +00004232std::pair<ObjCMethodList, ObjCMethodList>
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004233ASTReader::ReadMethodPool(Selector Sel) {
Sebastian Redl725cd962010-08-04 20:40:17 +00004234 // Find this selector in a hash table. We want to find the most recent entry.
4235 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4236 PerFileData &F = *Chain[I];
4237 if (!F.SelectorLookupTable)
4238 continue;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00004239
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004240 ASTSelectorLookupTable *PoolTable
4241 = (ASTSelectorLookupTable*)F.SelectorLookupTable;
4242 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
Sebastian Redl725cd962010-08-04 20:40:17 +00004243 if (Pos != PoolTable->end()) {
4244 ++NumSelectorsRead;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00004245 // FIXME: Not quite happy with the statistics here. We probably should
4246 // disable this tracking when called via LoadSelector.
4247 // Also, should entries without methods count as misses?
4248 ++NumMethodPoolEntriesRead;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004249 ASTSelectorLookupTrait::data_type Data = *Pos;
Sebastian Redl725cd962010-08-04 20:40:17 +00004250 if (DeserializationListener)
4251 DeserializationListener->SelectorRead(Data.ID, Sel);
4252 return std::make_pair(Data.Instance, Data.Factory);
4253 }
Douglas Gregor83941df2009-04-25 17:48:32 +00004254 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00004255
Sebastian Redlfa78dec2010-08-04 21:22:45 +00004256 ++NumMethodPoolMisses;
Sebastian Redl725cd962010-08-04 20:40:17 +00004257 return std::pair<ObjCMethodList, ObjCMethodList>();
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00004258}
4259
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004260void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redle58aa892010-08-04 18:21:41 +00004261 // It would be complicated to avoid reading the methods anyway. So don't.
4262 ReadMethodPool(Sel);
4263}
4264
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004265void ASTReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00004266 assert(ID && "Non-zero identifier ID required");
Douglas Gregora02b1472009-04-28 21:53:25 +00004267 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00004268 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004269 if (DeserializationListener)
4270 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregor668c1a42009-04-21 22:25:48 +00004271}
4272
Douglas Gregord89275b2009-07-06 18:54:52 +00004273/// \brief Set the globally-visible declarations associated with the given
4274/// identifier.
4275///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004276/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump1eb44332009-09-09 15:08:12 +00004277/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregord89275b2009-07-06 18:54:52 +00004278/// them.
4279///
4280/// \param II an IdentifierInfo that refers to one or more globally-visible
4281/// declarations.
4282///
4283/// \param DeclIDs the set of declaration IDs with the name @p II that are
4284/// visible at global scope.
4285///
4286/// \param Nonrecursive should be true to indicate that the caller knows that
4287/// this call is non-recursive, and therefore the globally-visible declarations
4288/// will not be placed onto the pending queue.
Mike Stump1eb44332009-09-09 15:08:12 +00004289void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004290ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Douglas Gregord89275b2009-07-06 18:54:52 +00004291 const llvm::SmallVectorImpl<uint32_t> &DeclIDs,
4292 bool Nonrecursive) {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004293 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregord89275b2009-07-06 18:54:52 +00004294 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
4295 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
4296 PII.II = II;
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00004297 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregord89275b2009-07-06 18:54:52 +00004298 return;
4299 }
Mike Stump1eb44332009-09-09 15:08:12 +00004300
Douglas Gregord89275b2009-07-06 18:54:52 +00004301 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
4302 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
4303 if (SemaObj) {
Douglas Gregor914ed9d2010-08-13 03:15:25 +00004304 if (SemaObj->TUScope) {
4305 // Introduce this declaration into the translation-unit scope
4306 // and add it to the declaration chain for this identifier, so
4307 // that (unqualified) name lookup will find it.
John McCalld226f652010-08-21 09:40:31 +00004308 SemaObj->TUScope->AddDecl(D);
Douglas Gregor914ed9d2010-08-13 03:15:25 +00004309 }
Douglas Gregor76dc8892010-09-24 23:29:12 +00004310 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
Douglas Gregord89275b2009-07-06 18:54:52 +00004311 } else {
4312 // Queue this declaration so that it will be added to the
4313 // translation unit scope and identifier's declaration chain
4314 // once a Sema object is known.
4315 PreloadedDecls.push_back(D);
4316 }
4317 }
4318}
4319
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004320IdentifierInfo *ASTReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00004321 if (ID == 0)
4322 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00004323
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00004324 if (IdentifiersLoaded.empty()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004325 Error("no identifier table in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00004326 return 0;
4327 }
Mike Stump1eb44332009-09-09 15:08:12 +00004328
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00004329 assert(PP && "Forgot to set Preprocessor ?");
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00004330 ID -= 1;
4331 if (!IdentifiersLoaded[ID]) {
4332 unsigned Index = ID;
4333 const char *Str = 0;
4334 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4335 PerFileData *F = Chain[N - I - 1];
4336 if (Index < F->LocalNumIdentifiers) {
4337 uint32_t Offset = F->IdentifierOffsets[Index];
4338 Str = F->IdentifierTableData + Offset;
4339 break;
4340 }
4341 Index -= F->LocalNumIdentifiers;
4342 }
4343 assert(Str && "Broken Chain");
Douglas Gregord6595a42009-04-25 21:04:17 +00004344
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004345 // All of the strings in the AST file are preceded by a 16-bit length.
4346 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenek231bc0b2009-10-23 04:45:31 +00004347 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
4348 // unsigned integers. This is important to avoid integer overflow when
4349 // we cast them to 'unsigned'.
Ted Kremenekff1ea462009-10-23 03:57:22 +00004350 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregor02fc7512009-04-28 20:01:51 +00004351 unsigned StrLen = (((unsigned) StrLenPtr[0])
4352 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00004353 IdentifiersLoaded[ID]
Kovarththanan Rajaratnam811f4262010-03-12 10:32:27 +00004354 = &PP->getIdentifierTable().get(Str, StrLen);
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004355 if (DeserializationListener)
4356 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregorafaf3082009-04-11 00:14:32 +00004357 }
Mike Stump1eb44332009-09-09 15:08:12 +00004358
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00004359 return IdentifiersLoaded[ID];
Douglas Gregor2cf26342009-04-09 22:27:44 +00004360}
4361
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004362void ASTReader::ReadSLocEntry(unsigned ID) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00004363 ReadSLocEntryRecord(ID);
4364}
4365
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004366Selector ASTReader::DecodeSelector(unsigned ID) {
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004367 if (ID == 0)
4368 return Selector();
Mike Stump1eb44332009-09-09 15:08:12 +00004369
Sebastian Redl725cd962010-08-04 20:40:17 +00004370 if (ID > SelectorsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004371 Error("selector ID out of range in AST file");
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004372 return Selector();
4373 }
Douglas Gregor83941df2009-04-25 17:48:32 +00004374
Sebastian Redl725cd962010-08-04 20:40:17 +00004375 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor83941df2009-04-25 17:48:32 +00004376 // Load this selector from the selector table.
Sebastian Redl725cd962010-08-04 20:40:17 +00004377 unsigned Idx = ID - 1;
4378 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4379 PerFileData &F = *Chain[N - I - 1];
4380 if (Idx < F.LocalNumSelectors) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004381 ASTSelectorLookupTrait Trait(*this);
Sebastian Redl725cd962010-08-04 20:40:17 +00004382 SelectorsLoaded[ID - 1] =
4383 Trait.ReadKey(F.SelectorLookupTableData + F.SelectorOffsets[Idx], 0);
4384 if (DeserializationListener)
4385 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
4386 break;
4387 }
4388 Idx -= F.LocalNumSelectors;
4389 }
Douglas Gregor83941df2009-04-25 17:48:32 +00004390 }
4391
Sebastian Redl725cd962010-08-04 20:40:17 +00004392 return SelectorsLoaded[ID - 1];
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004393}
4394
Michael J. Spencer20249a12010-10-21 03:16:25 +00004395Selector ASTReader::GetExternalSelector(uint32_t ID) {
Douglas Gregor719770d2010-04-06 17:30:22 +00004396 return DecodeSelector(ID);
4397}
4398
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004399uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redl725cd962010-08-04 20:40:17 +00004400 // ID 0 (the null selector) is considered an external selector.
4401 return getTotalNumSelectors() + 1;
Douglas Gregor719770d2010-04-06 17:30:22 +00004402}
4403
Mike Stump1eb44332009-09-09 15:08:12 +00004404DeclarationName
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004405ASTReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00004406 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
4407 switch (Kind) {
4408 case DeclarationName::Identifier:
4409 return DeclarationName(GetIdentifierInfo(Record, Idx));
4410
4411 case DeclarationName::ObjCZeroArgSelector:
4412 case DeclarationName::ObjCOneArgSelector:
4413 case DeclarationName::ObjCMultiArgSelector:
Steve Naroffa7503a72009-04-23 15:15:40 +00004414 return DeclarationName(GetSelector(Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004415
4416 case DeclarationName::CXXConstructorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00004417 return Context->DeclarationNames.getCXXConstructorName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00004418 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004419
4420 case DeclarationName::CXXDestructorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00004421 return Context->DeclarationNames.getCXXDestructorName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00004422 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004423
4424 case DeclarationName::CXXConversionFunctionName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00004425 return Context->DeclarationNames.getCXXConversionFunctionName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00004426 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004427
4428 case DeclarationName::CXXOperatorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00004429 return Context->DeclarationNames.getCXXOperatorName(
Douglas Gregor2cf26342009-04-09 22:27:44 +00004430 (OverloadedOperatorKind)Record[Idx++]);
4431
Sean Hunt3e518bd2009-11-29 07:34:05 +00004432 case DeclarationName::CXXLiteralOperatorName:
4433 return Context->DeclarationNames.getCXXLiteralOperatorName(
4434 GetIdentifierInfo(Record, Idx));
4435
Douglas Gregor2cf26342009-04-09 22:27:44 +00004436 case DeclarationName::CXXUsingDirective:
4437 return DeclarationName::getUsingDirectiveName();
4438 }
4439
4440 // Required to silence GCC warning
4441 return DeclarationName();
4442}
Douglas Gregor0a0428e2009-04-10 20:39:37 +00004443
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004444void ASTReader::ReadDeclarationNameLoc(PerFileData &F,
4445 DeclarationNameLoc &DNLoc,
4446 DeclarationName Name,
4447 const RecordData &Record, unsigned &Idx) {
4448 switch (Name.getNameKind()) {
4449 case DeclarationName::CXXConstructorName:
4450 case DeclarationName::CXXDestructorName:
4451 case DeclarationName::CXXConversionFunctionName:
4452 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
4453 break;
4454
4455 case DeclarationName::CXXOperatorName:
4456 DNLoc.CXXOperatorName.BeginOpNameLoc
4457 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4458 DNLoc.CXXOperatorName.EndOpNameLoc
4459 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4460 break;
4461
4462 case DeclarationName::CXXLiteralOperatorName:
4463 DNLoc.CXXLiteralOperatorName.OpNameLoc
4464 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4465 break;
4466
4467 case DeclarationName::Identifier:
4468 case DeclarationName::ObjCZeroArgSelector:
4469 case DeclarationName::ObjCOneArgSelector:
4470 case DeclarationName::ObjCMultiArgSelector:
4471 case DeclarationName::CXXUsingDirective:
4472 break;
4473 }
4474}
4475
4476void ASTReader::ReadDeclarationNameInfo(PerFileData &F,
4477 DeclarationNameInfo &NameInfo,
4478 const RecordData &Record, unsigned &Idx) {
4479 NameInfo.setName(ReadDeclarationName(Record, Idx));
4480 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
4481 DeclarationNameLoc DNLoc;
4482 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
4483 NameInfo.setInfo(DNLoc);
4484}
4485
4486void ASTReader::ReadQualifierInfo(PerFileData &F, QualifierInfo &Info,
4487 const RecordData &Record, unsigned &Idx) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004488 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004489 unsigned NumTPLists = Record[Idx++];
4490 Info.NumTemplParamLists = NumTPLists;
4491 if (NumTPLists) {
4492 Info.TemplParamLists = new (*Context) TemplateParameterList*[NumTPLists];
4493 for (unsigned i=0; i != NumTPLists; ++i)
4494 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
4495 }
4496}
4497
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004498TemplateName
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004499ASTReader::ReadTemplateName(PerFileData &F, const RecordData &Record,
4500 unsigned &Idx) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00004501 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004502 switch (Kind) {
4503 case TemplateName::Template:
4504 return TemplateName(cast_or_null<TemplateDecl>(GetDecl(Record[Idx++])));
4505
4506 case TemplateName::OverloadedTemplate: {
4507 unsigned size = Record[Idx++];
4508 UnresolvedSet<8> Decls;
4509 while (size--)
4510 Decls.addDecl(cast<NamedDecl>(GetDecl(Record[Idx++])));
4511
4512 return Context->getOverloadedTemplateName(Decls.begin(), Decls.end());
4513 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004514
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004515 case TemplateName::QualifiedTemplate: {
4516 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
4517 bool hasTemplKeyword = Record[Idx++];
4518 TemplateDecl *Template = cast<TemplateDecl>(GetDecl(Record[Idx++]));
4519 return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
4520 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004521
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004522 case TemplateName::DependentTemplate: {
4523 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
4524 if (Record[Idx++]) // isIdentifier
4525 return Context->getDependentTemplateName(NNS,
4526 GetIdentifierInfo(Record, Idx));
4527 return Context->getDependentTemplateName(NNS,
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004528 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004529 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004530
4531 case TemplateName::SubstTemplateTemplateParmPack: {
4532 TemplateTemplateParmDecl *Param
4533 = cast_or_null<TemplateTemplateParmDecl>(GetDecl(Record[Idx++]));
4534 if (!Param)
4535 return TemplateName();
4536
4537 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
4538 if (ArgPack.getKind() != TemplateArgument::Pack)
4539 return TemplateName();
4540
4541 return Context->getSubstTemplateTemplateParmPack(Param, ArgPack);
4542 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004543 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004544
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004545 assert(0 && "Unhandled template name kind!");
4546 return TemplateName();
4547}
4548
4549TemplateArgument
Sebastian Redlc3632732010-10-05 15:59:54 +00004550ASTReader::ReadTemplateArgument(PerFileData &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00004551 const RecordData &Record, unsigned &Idx) {
Douglas Gregora7fc9012011-01-05 18:58:31 +00004552 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
4553 switch (Kind) {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004554 case TemplateArgument::Null:
4555 return TemplateArgument();
4556 case TemplateArgument::Type:
4557 return TemplateArgument(GetType(Record[Idx++]));
4558 case TemplateArgument::Declaration:
4559 return TemplateArgument(GetDecl(Record[Idx++]));
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00004560 case TemplateArgument::Integral: {
4561 llvm::APSInt Value = ReadAPSInt(Record, Idx);
4562 QualType T = GetType(Record[Idx++]);
4563 return TemplateArgument(Value, T);
4564 }
Douglas Gregora7fc9012011-01-05 18:58:31 +00004565 case TemplateArgument::Template:
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004566 return TemplateArgument(ReadTemplateName(F, Record, Idx));
Douglas Gregora7fc9012011-01-05 18:58:31 +00004567 case TemplateArgument::TemplateExpansion: {
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004568 TemplateName Name = ReadTemplateName(F, Record, Idx);
Douglas Gregor2be29f42011-01-14 23:41:42 +00004569 llvm::Optional<unsigned> NumTemplateExpansions;
4570 if (unsigned NumExpansions = Record[Idx++])
4571 NumTemplateExpansions = NumExpansions - 1;
4572 return TemplateArgument(Name, NumTemplateExpansions);
Douglas Gregorba68eca2011-01-05 17:40:24 +00004573 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004574 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00004575 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004576 case TemplateArgument::Pack: {
4577 unsigned NumArgs = Record[Idx++];
Douglas Gregor910f8002010-11-07 23:05:16 +00004578 TemplateArgument *Args = new (*Context) TemplateArgument[NumArgs];
4579 for (unsigned I = 0; I != NumArgs; ++I)
4580 Args[I] = ReadTemplateArgument(F, Record, Idx);
4581 return TemplateArgument(Args, NumArgs);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004582 }
4583 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004584
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004585 assert(0 && "Unhandled template argument kind!");
4586 return TemplateArgument();
4587}
4588
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004589TemplateParameterList *
Sebastian Redlc3632732010-10-05 15:59:54 +00004590ASTReader::ReadTemplateParameterList(PerFileData &F,
4591 const RecordData &Record, unsigned &Idx) {
4592 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
4593 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
4594 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004595
4596 unsigned NumParams = Record[Idx++];
4597 llvm::SmallVector<NamedDecl *, 16> Params;
4598 Params.reserve(NumParams);
4599 while (NumParams--)
4600 Params.push_back(cast<NamedDecl>(GetDecl(Record[Idx++])));
Michael J. Spencer20249a12010-10-21 03:16:25 +00004601
4602 TemplateParameterList* TemplateParams =
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004603 TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc,
4604 Params.data(), Params.size(), RAngleLoc);
4605 return TemplateParams;
4606}
4607
4608void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004609ASTReader::
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004610ReadTemplateArgumentList(llvm::SmallVector<TemplateArgument, 8> &TemplArgs,
Sebastian Redlc3632732010-10-05 15:59:54 +00004611 PerFileData &F, const RecordData &Record,
4612 unsigned &Idx) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004613 unsigned NumTemplateArgs = Record[Idx++];
4614 TemplArgs.reserve(NumTemplateArgs);
4615 while (NumTemplateArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00004616 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004617}
4618
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004619/// \brief Read a UnresolvedSet structure.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004620void ASTReader::ReadUnresolvedSet(UnresolvedSetImpl &Set,
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004621 const RecordData &Record, unsigned &Idx) {
4622 unsigned NumDecls = Record[Idx++];
4623 while (NumDecls--) {
4624 NamedDecl *D = cast<NamedDecl>(GetDecl(Record[Idx++]));
4625 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
4626 Set.addDecl(D, AS);
4627 }
4628}
4629
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004630CXXBaseSpecifier
Sebastian Redlc3632732010-10-05 15:59:54 +00004631ASTReader::ReadCXXBaseSpecifier(PerFileData &F,
Nick Lewycky56062202010-07-26 16:56:01 +00004632 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004633 bool isVirtual = static_cast<bool>(Record[Idx++]);
4634 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
4635 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redlf677ea32011-02-05 19:23:19 +00004636 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00004637 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
4638 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00004639 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redlf677ea32011-02-05 19:23:19 +00004640 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
Douglas Gregorf90b27a2011-01-03 22:36:02 +00004641 EllipsisLoc);
Sebastian Redlf677ea32011-02-05 19:23:19 +00004642 Result.setInheritConstructors(inheritConstructors);
4643 return Result;
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004644}
4645
Sean Huntcbb67482011-01-08 20:30:50 +00004646std::pair<CXXCtorInitializer **, unsigned>
4647ASTReader::ReadCXXCtorInitializers(PerFileData &F, const RecordData &Record,
4648 unsigned &Idx) {
4649 CXXCtorInitializer **CtorInitializers = 0;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004650 unsigned NumInitializers = Record[Idx++];
4651 if (NumInitializers) {
4652 ASTContext &C = *getContext();
4653
Sean Huntcbb67482011-01-08 20:30:50 +00004654 CtorInitializers
4655 = new (C) CXXCtorInitializer*[NumInitializers];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004656 for (unsigned i=0; i != NumInitializers; ++i) {
4657 TypeSourceInfo *BaseClassInfo = 0;
4658 bool IsBaseVirtual = false;
4659 FieldDecl *Member = 0;
Francois Pichet00eb3f92010-12-04 09:14:42 +00004660 IndirectFieldDecl *IndirectMember = 0;
Michael J. Spencer20249a12010-10-21 03:16:25 +00004661
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004662 bool IsBaseInitializer = Record[Idx++];
4663 if (IsBaseInitializer) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004664 BaseClassInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004665 IsBaseVirtual = Record[Idx++];
4666 } else {
Francois Pichet00eb3f92010-12-04 09:14:42 +00004667 bool IsIndirectMemberInitializer = Record[Idx++];
4668 if (IsIndirectMemberInitializer)
4669 IndirectMember = cast<IndirectFieldDecl>(GetDecl(Record[Idx++]));
4670 else
4671 Member = cast<FieldDecl>(GetDecl(Record[Idx++]));
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004672 }
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00004673 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redlc3632732010-10-05 15:59:54 +00004674 Expr *Init = ReadExpr(F);
Sebastian Redlc3632732010-10-05 15:59:54 +00004675 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
4676 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004677 bool IsWritten = Record[Idx++];
4678 unsigned SourceOrderOrNumArrayIndices;
4679 llvm::SmallVector<VarDecl *, 8> Indices;
4680 if (IsWritten) {
4681 SourceOrderOrNumArrayIndices = Record[Idx++];
4682 } else {
4683 SourceOrderOrNumArrayIndices = Record[Idx++];
4684 Indices.reserve(SourceOrderOrNumArrayIndices);
4685 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
4686 Indices.push_back(cast<VarDecl>(GetDecl(Record[Idx++])));
4687 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004688
Sean Huntcbb67482011-01-08 20:30:50 +00004689 CXXCtorInitializer *BOMInit;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004690 if (IsBaseInitializer) {
Sean Huntcbb67482011-01-08 20:30:50 +00004691 BOMInit = new (C) CXXCtorInitializer(C, BaseClassInfo, IsBaseVirtual,
4692 LParenLoc, Init, RParenLoc,
4693 MemberOrEllipsisLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004694 } else if (IsWritten) {
Francois Pichet00eb3f92010-12-04 09:14:42 +00004695 if (Member)
Sean Huntcbb67482011-01-08 20:30:50 +00004696 BOMInit = new (C) CXXCtorInitializer(C, Member, MemberOrEllipsisLoc,
4697 LParenLoc, Init, RParenLoc);
Francois Pichet00eb3f92010-12-04 09:14:42 +00004698 else
Sean Huntcbb67482011-01-08 20:30:50 +00004699 BOMInit = new (C) CXXCtorInitializer(C, IndirectMember,
4700 MemberOrEllipsisLoc, LParenLoc,
4701 Init, RParenLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004702 } else {
Sean Huntcbb67482011-01-08 20:30:50 +00004703 BOMInit = CXXCtorInitializer::Create(C, Member, MemberOrEllipsisLoc,
4704 LParenLoc, Init, RParenLoc,
4705 Indices.data(), Indices.size());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004706 }
4707
Argyrios Kyrtzidisf84cde12010-09-06 19:04:27 +00004708 if (IsWritten)
4709 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Sean Huntcbb67482011-01-08 20:30:50 +00004710 CtorInitializers[i] = BOMInit;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004711 }
4712 }
4713
Sean Huntcbb67482011-01-08 20:30:50 +00004714 return std::make_pair(CtorInitializers, NumInitializers);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004715}
4716
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004717NestedNameSpecifier *
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004718ASTReader::ReadNestedNameSpecifier(const RecordData &Record, unsigned &Idx) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004719 unsigned N = Record[Idx++];
4720 NestedNameSpecifier *NNS = 0, *Prev = 0;
4721 for (unsigned I = 0; I != N; ++I) {
4722 NestedNameSpecifier::SpecifierKind Kind
4723 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
4724 switch (Kind) {
4725 case NestedNameSpecifier::Identifier: {
4726 IdentifierInfo *II = GetIdentifierInfo(Record, Idx);
4727 NNS = NestedNameSpecifier::Create(*Context, Prev, II);
4728 break;
4729 }
4730
4731 case NestedNameSpecifier::Namespace: {
4732 NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++]));
4733 NNS = NestedNameSpecifier::Create(*Context, Prev, NS);
4734 break;
4735 }
4736
Douglas Gregor14aba762011-02-24 02:36:08 +00004737 case NestedNameSpecifier::NamespaceAlias: {
4738 NamespaceAliasDecl *Alias
4739 = cast<NamespaceAliasDecl>(GetDecl(Record[Idx++]));
4740 NNS = NestedNameSpecifier::Create(*Context, Prev, Alias);
4741 break;
4742 }
4743
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004744 case NestedNameSpecifier::TypeSpec:
4745 case NestedNameSpecifier::TypeSpecWithTemplate: {
John McCallf4c73712011-01-19 06:33:43 +00004746 const Type *T = GetType(Record[Idx++]).getTypePtrOrNull();
Douglas Gregor1ab55e92010-12-10 17:03:06 +00004747 if (!T)
4748 return 0;
4749
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004750 bool Template = Record[Idx++];
4751 NNS = NestedNameSpecifier::Create(*Context, Prev, Template, T);
4752 break;
4753 }
4754
4755 case NestedNameSpecifier::Global: {
4756 NNS = NestedNameSpecifier::GlobalSpecifier(*Context);
4757 // No associated value, and there can't be a prefix.
4758 break;
4759 }
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004760 }
Argyrios Kyrtzidisd2bb2c02010-07-07 15:46:30 +00004761 Prev = NNS;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004762 }
4763 return NNS;
4764}
4765
Douglas Gregordc355712011-02-25 00:36:19 +00004766NestedNameSpecifierLoc
4767ASTReader::ReadNestedNameSpecifierLoc(PerFileData &F, const RecordData &Record,
4768 unsigned &Idx) {
4769 unsigned N = Record[Idx++];
Douglas Gregor5f791bb2011-02-28 23:58:31 +00004770 NestedNameSpecifierLocBuilder Builder;
Douglas Gregordc355712011-02-25 00:36:19 +00004771 for (unsigned I = 0; I != N; ++I) {
4772 NestedNameSpecifier::SpecifierKind Kind
4773 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
4774 switch (Kind) {
4775 case NestedNameSpecifier::Identifier: {
Douglas Gregor5f791bb2011-02-28 23:58:31 +00004776 IdentifierInfo *II = GetIdentifierInfo(Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00004777 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor5f791bb2011-02-28 23:58:31 +00004778 Builder.Extend(*Context, II, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00004779 break;
4780 }
4781
4782 case NestedNameSpecifier::Namespace: {
Douglas Gregordc355712011-02-25 00:36:19 +00004783 NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++]));
Douglas Gregordc355712011-02-25 00:36:19 +00004784 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor5f791bb2011-02-28 23:58:31 +00004785 Builder.Extend(*Context, NS, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00004786 break;
4787 }
4788
4789 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregordc355712011-02-25 00:36:19 +00004790 NamespaceAliasDecl *Alias
4791 = cast<NamespaceAliasDecl>(GetDecl(Record[Idx++]));
Douglas Gregordc355712011-02-25 00:36:19 +00004792 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor5f791bb2011-02-28 23:58:31 +00004793 Builder.Extend(*Context, Alias, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00004794 break;
4795 }
4796
4797 case NestedNameSpecifier::TypeSpec:
4798 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregordc355712011-02-25 00:36:19 +00004799 bool Template = Record[Idx++];
4800 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
4801 if (!T)
4802 return NestedNameSpecifierLoc();
Douglas Gregordc355712011-02-25 00:36:19 +00004803 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor5f791bb2011-02-28 23:58:31 +00004804
4805 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
4806 Builder.Extend(*Context,
4807 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
4808 T->getTypeLoc(), ColonColonLoc);
Douglas Gregordc355712011-02-25 00:36:19 +00004809 break;
4810 }
4811
4812 case NestedNameSpecifier::Global: {
Douglas Gregordc355712011-02-25 00:36:19 +00004813 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor5f791bb2011-02-28 23:58:31 +00004814 Builder.MakeGlobal(*Context, ColonColonLoc);
Douglas Gregordc355712011-02-25 00:36:19 +00004815 break;
4816 }
4817 }
Douglas Gregordc355712011-02-25 00:36:19 +00004818 }
4819
Douglas Gregor5f791bb2011-02-28 23:58:31 +00004820 return Builder.getWithLocInContext(*Context);
Douglas Gregordc355712011-02-25 00:36:19 +00004821}
4822
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004823SourceRange
Sebastian Redlc3632732010-10-05 15:59:54 +00004824ASTReader::ReadSourceRange(PerFileData &F, const RecordData &Record,
4825 unsigned &Idx) {
4826 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
4827 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar8ee59392010-06-02 15:47:10 +00004828 return SourceRange(beg, end);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004829}
4830
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004831/// \brief Read an integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004832llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004833 unsigned BitWidth = Record[Idx++];
4834 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
4835 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
4836 Idx += NumWords;
4837 return Result;
4838}
4839
4840/// \brief Read a signed integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004841llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004842 bool isUnsigned = Record[Idx++];
4843 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
4844}
4845
Douglas Gregor17fc2232009-04-14 21:55:33 +00004846/// \brief Read a floating-point value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004847llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00004848 return llvm::APFloat(ReadAPInt(Record, Idx));
4849}
4850
Douglas Gregor68a2eb02009-04-15 21:30:51 +00004851// \brief Read a string
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004852std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00004853 unsigned Len = Record[Idx++];
Jay Foadbeaaccd2009-05-21 09:52:38 +00004854 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00004855 Idx += Len;
4856 return Result;
4857}
4858
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004859CXXTemporary *ASTReader::ReadCXXTemporary(const RecordData &Record,
Chris Lattnerd2598362010-05-10 00:25:06 +00004860 unsigned &Idx) {
4861 CXXDestructorDecl *Decl = cast<CXXDestructorDecl>(GetDecl(Record[Idx++]));
4862 return CXXTemporary::Create(*Context, Decl);
4863}
4864
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004865DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00004866 return Diag(SourceLocation(), DiagID);
4867}
4868
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004869DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004870 return Diags.Report(Loc, DiagID);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00004871}
Douglas Gregor025452f2009-04-17 00:04:06 +00004872
Douglas Gregor668c1a42009-04-21 22:25:48 +00004873/// \brief Retrieve the identifier table associated with the
4874/// preprocessor.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004875IdentifierTable &ASTReader::getIdentifierTable() {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00004876 assert(PP && "Forgot to set Preprocessor ?");
4877 return PP->getIdentifierTable();
Douglas Gregor668c1a42009-04-21 22:25:48 +00004878}
4879
Douglas Gregor025452f2009-04-17 00:04:06 +00004880/// \brief Record that the given ID maps to the given switch-case
4881/// statement.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004882void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Douglas Gregor025452f2009-04-17 00:04:06 +00004883 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
4884 SwitchCaseStmts[ID] = SC;
4885}
4886
4887/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004888SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Douglas Gregor025452f2009-04-17 00:04:06 +00004889 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
4890 return SwitchCaseStmts[ID];
4891}
Douglas Gregor1de05fe2009-04-17 18:18:49 +00004892
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00004893void ASTReader::ClearSwitchCaseIDs() {
4894 SwitchCaseStmts.clear();
4895}
4896
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004897void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004898 assert(NumCurrentElementsDeserializing &&
4899 "FinishedDeserializing not paired with StartedDeserializing");
4900 if (NumCurrentElementsDeserializing == 1) {
Douglas Gregord89275b2009-07-06 18:54:52 +00004901 // If any identifiers with corresponding top-level declarations have
4902 // been loaded, load those declarations now.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004903 while (!PendingIdentifierInfos.empty()) {
4904 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
4905 PendingIdentifierInfos.front().DeclIDs, true);
4906 PendingIdentifierInfos.pop_front();
Douglas Gregord89275b2009-07-06 18:54:52 +00004907 }
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00004908
Argyrios Kyrtzidis0895d152011-02-12 07:50:47 +00004909 // Ready to load previous declarations of Decls that were delayed.
4910 while (!PendingPreviousDecls.empty()) {
4911 loadAndAttachPreviousDecl(PendingPreviousDecls.front().first,
4912 PendingPreviousDecls.front().second);
4913 PendingPreviousDecls.pop_front();
4914 }
4915
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00004916 // We are not in recursive loading, so it's safe to pass the "interesting"
4917 // decls to the consumer.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004918 if (Consumer)
4919 PassInterestingDeclsToConsumer();
Argyrios Kyrtzidis134db1f2010-10-24 17:26:31 +00004920
4921 assert(PendingForwardRefs.size() == 0 &&
4922 "Some forward refs did not get linked to the definition!");
Douglas Gregord89275b2009-07-06 18:54:52 +00004923 }
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004924 --NumCurrentElementsDeserializing;
Douglas Gregord89275b2009-07-06 18:54:52 +00004925}
Douglas Gregor501c1032010-08-19 00:28:17 +00004926
Sebastian Redle1dde812010-08-24 00:50:04 +00004927ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context,
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00004928 const char *isysroot, bool DisableValidation,
4929 bool DisableStatCache)
Sebastian Redle1dde812010-08-24 00:50:04 +00004930 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
4931 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
4932 Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context),
4933 Consumer(0), isysroot(isysroot), DisableValidation(DisableValidation),
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00004934 DisableStatCache(DisableStatCache), NumStatHits(0), NumStatMisses(0),
4935 NumSLocEntriesRead(0), TotalNumSLocEntries(0), NextSLocOffset(0),
4936 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
4937 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
4938 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
4939 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
4940 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
4941 NumCurrentElementsDeserializing(0)
4942{
Sebastian Redle1dde812010-08-24 00:50:04 +00004943 RelocatablePCH = false;
4944}
4945
4946ASTReader::ASTReader(SourceManager &SourceMgr, FileManager &FileMgr,
4947 Diagnostic &Diags, const char *isysroot,
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00004948 bool DisableValidation, bool DisableStatCache)
Sebastian Redle1dde812010-08-24 00:50:04 +00004949 : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr),
4950 Diags(Diags), SemaObj(0), PP(0), Context(0), Consumer(0),
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00004951 isysroot(isysroot), DisableValidation(DisableValidation),
4952 DisableStatCache(DisableStatCache), NumStatHits(0), NumStatMisses(0),
4953 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Sebastian Redl8db9fae2010-09-22 20:19:08 +00004954 NextSLocOffset(0), NumStatementsRead(0), TotalNumStatements(0),
4955 NumMacrosRead(0), TotalNumMacros(0), NumSelectorsRead(0),
4956 NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
4957 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
4958 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
4959 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
Sebastian Redle1dde812010-08-24 00:50:04 +00004960 RelocatablePCH = false;
4961}
4962
4963ASTReader::~ASTReader() {
4964 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
4965 delete Chain[e - i - 1];
4966 // Delete all visible decl lookup tables
4967 for (DeclContextOffsetsMap::iterator I = DeclContextOffsets.begin(),
4968 E = DeclContextOffsets.end();
4969 I != E; ++I) {
4970 for (DeclContextInfos::iterator J = I->second.begin(), F = I->second.end();
4971 J != F; ++J) {
4972 if (J->NameLookupTableData)
4973 delete static_cast<ASTDeclContextNameLookupTable*>(
4974 J->NameLookupTableData);
4975 }
4976 }
4977 for (DeclContextVisibleUpdatesPending::iterator
4978 I = PendingVisibleUpdates.begin(),
4979 E = PendingVisibleUpdates.end();
4980 I != E; ++I) {
4981 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
4982 F = I->second.end();
4983 J != F; ++J)
4984 delete static_cast<ASTDeclContextNameLookupTable*>(*J);
4985 }
4986}
4987
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00004988ASTReader::PerFileData::PerFileData(ASTFileType Ty)
4989 : Type(Ty), SizeInBits(0), LocalNumSLocEntries(0), SLocOffsets(0), LocalSLocSize(0),
Sebastian Redl301c9b02010-09-22 00:42:27 +00004990 LocalNumIdentifiers(0), IdentifierOffsets(0), IdentifierTableData(0),
4991 IdentifierLookupTable(0), LocalNumMacroDefinitions(0),
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00004992 MacroDefinitionOffsets(0),
4993 LocalNumHeaderFileInfos(0), HeaderFileInfoTableData(0),
4994 HeaderFileInfoTable(0),
4995 LocalNumSelectors(0), SelectorOffsets(0),
Sebastian Redl301c9b02010-09-22 00:42:27 +00004996 SelectorLookupTableData(0), SelectorLookupTable(0), LocalNumDecls(0),
Douglas Gregor7c789c12010-10-29 22:39:52 +00004997 DeclOffsets(0), LocalNumCXXBaseSpecifiers(0), CXXBaseSpecifiersOffsets(0),
4998 LocalNumTypes(0), TypeOffsets(0), StatCache(0),
Sebastian Redla866e652010-10-01 19:59:12 +00004999 NumPreallocatedPreprocessingEntities(0), NextInSource(0)
Douglas Gregor501c1032010-08-19 00:28:17 +00005000{}
5001
5002ASTReader::PerFileData::~PerFileData() {
5003 delete static_cast<ASTIdentifierLookupTable *>(IdentifierLookupTable);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00005004 delete static_cast<HeaderFileInfoLookupTable *>(HeaderFileInfoTable);
Douglas Gregor501c1032010-08-19 00:28:17 +00005005 delete static_cast<ASTSelectorLookupTable *>(SelectorLookupTable);
5006}