blob: 7c8415a295c1ba659c6c8f3c59a0201ac7b0c44a [file] [log] [blame]
Sebastian Redl904c9c82010-08-18 23:57:11 +00001//===--- ASTReader.cpp - AST File Reader ------------------------*- C++ -*-===//
Douglas Gregor2cf26342009-04-09 22:27:44 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Sebastian Redlc43b54c2010-08-18 23:56:43 +000010// This file defines the ASTReader class, which reads AST files.
Douglas Gregor2cf26342009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
Chris Lattner4c6f9522009-04-27 05:14:47 +000013
Sebastian Redl6ab7cd82010-08-18 23:57:17 +000014#include "clang/Serialization/ASTReader.h"
15#include "clang/Serialization/ASTDeserializationListener.h"
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +000016#include "ASTCommon.h"
Douglas Gregor0a0428e2009-04-10 20:39:37 +000017#include "clang/Frontend/FrontendDiagnostic.h"
Daniel Dunbarc7162932009-11-11 23:58:53 +000018#include "clang/Frontend/Utils.h"
Douglas Gregore737f502010-08-12 20:07:10 +000019#include "clang/Sema/Sema.h"
John McCall5f1e0942010-08-24 08:50:51 +000020#include "clang/Sema/Scope.h"
Douglas Gregorfdd01722009-04-14 00:24:19 +000021#include "clang/AST/ASTConsumer.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000022#include "clang/AST/ASTContext.h"
John McCall2a7fb272010-08-25 05:32:35 +000023#include "clang/AST/DeclTemplate.h"
Douglas Gregor0b748912009-04-14 21:18:50 +000024#include "clang/AST/Expr.h"
John McCall7a1fad32010-08-24 07:32:53 +000025#include "clang/AST/ExprCXX.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000026#include "clang/AST/Type.h"
John McCalla1ee0c52009-10-16 21:56:05 +000027#include "clang/AST/TypeLocVisitor.h"
Chris Lattner42d42b52009-04-10 21:41:48 +000028#include "clang/Lex/MacroInfo.h"
Douglas Gregor6a5a23f2010-03-19 21:51:54 +000029#include "clang/Lex/PreprocessingRecord.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000030#include "clang/Lex/Preprocessor.h"
Steve Naroff83d63c72009-04-24 20:03:17 +000031#include "clang/Lex/HeaderSearch.h"
Douglas Gregor668c1a42009-04-21 22:25:48 +000032#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000033#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000034#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000035#include "clang/Basic/FileManager.h"
Chris Lattner10e286a2010-11-23 19:19:34 +000036#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000037#include "clang/Basic/TargetInfo.h"
Douglas Gregor445e23e2009-10-05 21:07:28 +000038#include "clang/Basic/Version.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000039#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000040#include "llvm/Bitcode/BitstreamReader.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000041#include "llvm/Support/MemoryBuffer.h"
John McCall833ca992009-10-29 08:12:44 +000042#include "llvm/Support/ErrorHandling.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000043#include "llvm/Support/Path.h"
Michael J. Spencer3a321e22010-12-09 17:36:38 +000044#include "llvm/Support/system_error.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000045#include <algorithm>
Douglas Gregore721f952009-04-28 18:58:38 +000046#include <iterator>
Douglas Gregor2cf26342009-04-09 22:27:44 +000047#include <cstdio>
Douglas Gregor4fed3f42009-04-27 18:38:38 +000048#include <sys/stat.h>
Douglas Gregor2cf26342009-04-09 22:27:44 +000049using namespace clang;
Sebastian Redl8538e8d2010-08-18 23:57:32 +000050using namespace clang::serialization;
Douglas Gregor2cf26342009-04-09 22:27:44 +000051
52//===----------------------------------------------------------------------===//
Sebastian Redl3c7f4132010-08-18 23:57:06 +000053// PCH validator implementation
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000054//===----------------------------------------------------------------------===//
55
Sebastian Redl571db7f2010-08-18 23:56:56 +000056ASTReaderListener::~ASTReaderListener() {}
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000057
58bool
59PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts) {
60 const LangOptions &PPLangOpts = PP.getLangOptions();
61#define PARSE_LANGOPT_BENIGN(Option)
62#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
63 if (PPLangOpts.Option != LangOpts.Option) { \
64 Reader.Diag(DiagID) << LangOpts.Option << PPLangOpts.Option; \
65 return true; \
66 }
67
68 PARSE_LANGOPT_BENIGN(Trigraphs);
69 PARSE_LANGOPT_BENIGN(BCPLComment);
70 PARSE_LANGOPT_BENIGN(DollarIdents);
71 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
72 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
Chandler Carrutheb5d7b72010-04-17 20:17:31 +000073 PARSE_LANGOPT_IMPORTANT(GNUKeywords, diag::warn_pch_gnu_keywords);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000074 PARSE_LANGOPT_BENIGN(ImplicitInt);
75 PARSE_LANGOPT_BENIGN(Digraphs);
76 PARSE_LANGOPT_BENIGN(HexFloats);
77 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
78 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
Michael J. Spencerdae4ac42010-10-21 05:21:48 +000079 PARSE_LANGOPT_BENIGN(MSCVersion);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000080 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
81 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
82 PARSE_LANGOPT_BENIGN(CXXOperatorName);
83 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
84 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
85 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
Fariborz Jahanian412e7982010-02-09 19:31:38 +000086 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI2, diag::warn_pch_nonfragile_abi2);
Ted Kremenekc32647d2010-12-23 21:35:43 +000087 PARSE_LANGOPT_IMPORTANT(ObjCDefaultSynthProperties,
88 diag::warn_pch_objc_auto_properties);
Michael J. Spencer20249a12010-10-21 03:16:25 +000089 PARSE_LANGOPT_IMPORTANT(NoConstantCFStrings,
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +000090 diag::warn_pch_no_constant_cfstrings);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000091 PARSE_LANGOPT_BENIGN(PascalStrings);
92 PARSE_LANGOPT_BENIGN(WritableStrings);
Mike Stump1eb44332009-09-09 15:08:12 +000093 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000094 diag::warn_pch_lax_vector_conversions);
Nate Begeman69cfb9b2009-06-25 22:57:40 +000095 PARSE_LANGOPT_IMPORTANT(AltiVec, diag::warn_pch_altivec);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000096 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
Daniel Dunbar73482882010-02-10 18:48:44 +000097 PARSE_LANGOPT_IMPORTANT(SjLjExceptions, diag::warn_pch_sjlj_exceptions);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000098 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
99 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
100 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
Mike Stump1eb44332009-09-09 15:08:12 +0000101 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000102 diag::warn_pch_thread_safe_statics);
Daniel Dunbar5345c392009-09-03 04:54:28 +0000103 PARSE_LANGOPT_IMPORTANT(POSIXThreads, diag::warn_pch_posix_threads);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000104 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
105 PARSE_LANGOPT_BENIGN(EmitAllDecls);
106 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
Chris Lattnera4d71452010-06-26 21:25:03 +0000107 PARSE_LANGOPT_BENIGN(getSignedOverflowBehavior());
Mike Stump1eb44332009-09-09 15:08:12 +0000108 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000109 diag::warn_pch_heinous_extensions);
110 // FIXME: Most of the options below are benign if the macro wasn't
111 // used. Unfortunately, this means that a PCH compiled without
112 // optimization can't be used with optimization turned on, even
113 // though the only thing that changes is whether __OPTIMIZE__ was
114 // defined... but if __OPTIMIZE__ never showed up in the header, it
115 // doesn't matter. We could consider making this some special kind
116 // of check.
117 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
118 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
119 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
120 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
121 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
122 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
123 PARSE_LANGOPT_IMPORTANT(AccessControl, diag::warn_pch_access_control);
124 PARSE_LANGOPT_IMPORTANT(CharIsSigned, diag::warn_pch_char_signed);
John Thompsona6fda122009-11-05 20:14:16 +0000125 PARSE_LANGOPT_IMPORTANT(ShortWChar, diag::warn_pch_short_wchar);
Argyrios Kyrtzidis9a2b9d72010-10-08 00:25:19 +0000126 PARSE_LANGOPT_IMPORTANT(ShortEnums, diag::warn_pch_short_enums);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000127 if ((PPLangOpts.getGCMode() != 0) != (LangOpts.getGCMode() != 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000128 Reader.Diag(diag::warn_pch_gc_mode)
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000129 << LangOpts.getGCMode() << PPLangOpts.getGCMode();
130 return true;
131 }
132 PARSE_LANGOPT_BENIGN(getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000133 PARSE_LANGOPT_IMPORTANT(getStackProtectorMode(),
134 diag::warn_pch_stack_protector);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000135 PARSE_LANGOPT_BENIGN(InstantiationDepth);
Nate Begeman69cfb9b2009-06-25 22:57:40 +0000136 PARSE_LANGOPT_IMPORTANT(OpenCL, diag::warn_pch_opencl);
Peter Collingbourne08a53262010-12-01 19:14:57 +0000137 PARSE_LANGOPT_IMPORTANT(CUDA, diag::warn_pch_cuda);
Mike Stump9c276ae2009-12-12 01:27:46 +0000138 PARSE_LANGOPT_BENIGN(CatchUndefined);
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000139 PARSE_LANGOPT_IMPORTANT(ElideConstructors, diag::warn_pch_elide_constructors);
Douglas Gregora0068fc2010-07-09 17:35:33 +0000140 PARSE_LANGOPT_BENIGN(SpellChecking);
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +0000141#undef PARSE_LANGOPT_IMPORTANT
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000142#undef PARSE_LANGOPT_BENIGN
143
144 return false;
145}
146
Daniel Dunbardc3c0d22009-11-11 00:52:11 +0000147bool PCHValidator::ReadTargetTriple(llvm::StringRef Triple) {
148 if (Triple == PP.getTargetInfo().getTriple().str())
149 return false;
150
151 Reader.Diag(diag::warn_pch_target_triple)
152 << Triple << PP.getTargetInfo().getTriple().str();
153 return true;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000154}
155
Benjamin Kramer54353f42010-11-25 18:29:30 +0000156namespace {
157 struct EmptyStringRef {
158 bool operator ()(llvm::StringRef r) const { return r.empty(); }
159 };
160 struct EmptyBlock {
161 bool operator ()(const PCHPredefinesBlock &r) const {return r.Data.empty();}
162 };
163}
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000164
165static bool EqualConcatenations(llvm::SmallVector<llvm::StringRef, 2> L,
166 PCHPredefinesBlocks R) {
167 // First, sum up the lengths.
168 unsigned LL = 0, RL = 0;
169 for (unsigned I = 0, N = L.size(); I != N; ++I) {
170 LL += L[I].size();
171 }
172 for (unsigned I = 0, N = R.size(); I != N; ++I) {
173 RL += R[I].Data.size();
174 }
175 if (LL != RL)
176 return false;
177 if (LL == 0 && RL == 0)
178 return true;
179
180 // Kick out empty parts, they confuse the algorithm below.
181 L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end());
182 R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end());
183
184 // Do it the hard way. At this point, both vectors must be non-empty.
185 llvm::StringRef LR = L[0], RR = R[0].Data;
186 unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size();
Daniel Dunbarc76c9e02010-07-16 00:00:11 +0000187 (void) RN;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000188 for (;;) {
189 // Compare the current pieces.
190 if (LR.size() == RR.size()) {
191 // If they're the same length, it's pretty easy.
192 if (LR != RR)
193 return false;
194 // Both pieces are done, advance.
195 ++LI;
196 ++RI;
197 // If either string is done, they're both done, since they're the same
198 // length.
199 if (LI == LN) {
200 assert(RI == RN && "Strings not the same length after all?");
201 return true;
202 }
203 LR = L[LI];
204 RR = R[RI].Data;
205 } else if (LR.size() < RR.size()) {
206 // Right piece is longer.
207 if (!RR.startswith(LR))
208 return false;
209 ++LI;
210 assert(LI != LN && "Strings not the same length after all?");
211 RR = RR.substr(LR.size());
212 LR = L[LI];
213 } else {
214 // Left piece is longer.
215 if (!LR.startswith(RR))
216 return false;
217 ++RI;
218 assert(RI != RN && "Strings not the same length after all?");
219 LR = LR.substr(RR.size());
220 RR = R[RI].Data;
221 }
222 }
223}
224
225static std::pair<FileID, llvm::StringRef::size_type>
226FindMacro(const PCHPredefinesBlocks &Buffers, llvm::StringRef MacroDef) {
227 std::pair<FileID, llvm::StringRef::size_type> Res;
228 for (unsigned I = 0, N = Buffers.size(); I != N; ++I) {
229 Res.second = Buffers[I].Data.find(MacroDef);
230 if (Res.second != llvm::StringRef::npos) {
231 Res.first = Buffers[I].BufferID;
232 break;
233 }
234 }
235 return Res;
236}
237
238bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000239 llvm::StringRef OriginalFileName,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000240 std::string &SuggestedPredefines) {
Daniel Dunbarc7162932009-11-11 23:58:53 +0000241 // We are in the context of an implicit include, so the predefines buffer will
242 // have a #include entry for the PCH file itself (as normalized by the
243 // preprocessor initialization). Find it and skip over it in the checking
244 // below.
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000245 llvm::SmallString<256> PCHInclude;
246 PCHInclude += "#include \"";
Daniel Dunbarc7162932009-11-11 23:58:53 +0000247 PCHInclude += NormalizeDashIncludePath(OriginalFileName);
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000248 PCHInclude += "\"\n";
249 std::pair<llvm::StringRef,llvm::StringRef> Split =
250 llvm::StringRef(PP.getPredefines()).split(PCHInclude.str());
251 llvm::StringRef Left = Split.first, Right = Split.second;
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000252 if (Left == PP.getPredefines()) {
253 Error("Missing PCH include entry!");
254 return true;
255 }
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000256
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000257 // If the concatenation of all the PCH buffers is equal to the adjusted
258 // command line, we're done.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000259 llvm::SmallVector<llvm::StringRef, 2> CommandLine;
260 CommandLine.push_back(Left);
261 CommandLine.push_back(Right);
262 if (EqualConcatenations(CommandLine, Buffers))
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000263 return false;
264
265 SourceManager &SourceMgr = PP.getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000266
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000267 // The predefines buffers are different. Determine what the differences are,
268 // and whether they require us to reject the PCH file.
Daniel Dunbare6750492009-11-13 16:46:11 +0000269 llvm::SmallVector<llvm::StringRef, 8> PCHLines;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000270 for (unsigned I = 0, N = Buffers.size(); I != N; ++I)
271 Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Daniel Dunbare6750492009-11-13 16:46:11 +0000272
273 llvm::SmallVector<llvm::StringRef, 8> CmdLineLines;
274 Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000275
276 // Pick out implicit #includes after the PCH and don't consider them for
277 // validation; we will insert them into SuggestedPredefines so that the
278 // preprocessor includes them.
279 std::string IncludesAfterPCH;
280 llvm::SmallVector<llvm::StringRef, 8> AfterPCHLines;
281 Right.split(AfterPCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
282 for (unsigned i = 0, e = AfterPCHLines.size(); i != e; ++i) {
283 if (AfterPCHLines[i].startswith("#include ")) {
284 IncludesAfterPCH += AfterPCHLines[i];
285 IncludesAfterPCH += '\n';
286 } else {
287 CmdLineLines.push_back(AfterPCHLines[i]);
288 }
289 }
290
291 // Make sure we add the includes last into SuggestedPredefines before we
292 // exit this function.
293 struct AddIncludesRAII {
294 std::string &SuggestedPredefines;
295 std::string &IncludesAfterPCH;
296
297 AddIncludesRAII(std::string &SuggestedPredefines,
298 std::string &IncludesAfterPCH)
299 : SuggestedPredefines(SuggestedPredefines),
300 IncludesAfterPCH(IncludesAfterPCH) { }
301 ~AddIncludesRAII() {
302 SuggestedPredefines += IncludesAfterPCH;
303 }
304 } AddIncludes(SuggestedPredefines, IncludesAfterPCH);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000305
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000306 // Sort both sets of predefined buffer lines, since we allow some extra
307 // definitions and they may appear at any point in the output.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000308 std::sort(CmdLineLines.begin(), CmdLineLines.end());
309 std::sort(PCHLines.begin(), PCHLines.end());
310
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000311 // Determine which predefines that were used to build the PCH file are missing
312 // from the command line.
313 std::vector<llvm::StringRef> MissingPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000314 std::set_difference(PCHLines.begin(), PCHLines.end(),
315 CmdLineLines.begin(), CmdLineLines.end(),
316 std::back_inserter(MissingPredefines));
317
318 bool MissingDefines = false;
319 bool ConflictingDefines = false;
320 for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) {
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000321 llvm::StringRef Missing = MissingPredefines[I];
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000322 if (Missing.startswith("#include ")) {
323 // An -include was specified when generating the PCH; it is included in
324 // the PCH, just ignore it.
325 continue;
326 }
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000327 if (!Missing.startswith("#define ")) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000328 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
329 return true;
330 }
Mike Stump1eb44332009-09-09 15:08:12 +0000331
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000332 // This is a macro definition. Determine the name of the macro we're
333 // defining.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000334 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000335 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000336 = Missing.find_first_of("( \n\r", StartOfMacroName);
337 assert(EndOfMacroName != std::string::npos &&
338 "Couldn't find the end of the macro name");
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000339 llvm::StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000340
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000341 // Determine whether this macro was given a different definition on the
342 // command line.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000343 std::string MacroDefStart = "#define " + MacroName.str();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000344 std::string::size_type MacroDefLen = MacroDefStart.size();
Daniel Dunbare6750492009-11-13 16:46:11 +0000345 llvm::SmallVector<llvm::StringRef, 8>::iterator ConflictPos
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000346 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
347 MacroDefStart);
348 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000349 if (!ConflictPos->startswith(MacroDefStart)) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000350 // Different macro; we're done.
351 ConflictPos = CmdLineLines.end();
Mike Stump1eb44332009-09-09 15:08:12 +0000352 break;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000353 }
Mike Stump1eb44332009-09-09 15:08:12 +0000354
355 assert(ConflictPos->size() > MacroDefLen &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000356 "Invalid #define in predefines buffer?");
Mike Stump1eb44332009-09-09 15:08:12 +0000357 if ((*ConflictPos)[MacroDefLen] != ' ' &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000358 (*ConflictPos)[MacroDefLen] != '(')
359 continue; // Longer macro name; keep trying.
Mike Stump1eb44332009-09-09 15:08:12 +0000360
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000361 // We found a conflicting macro definition.
362 break;
363 }
Mike Stump1eb44332009-09-09 15:08:12 +0000364
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000365 if (ConflictPos != CmdLineLines.end()) {
366 Reader.Diag(diag::warn_cmdline_conflicting_macro_def)
367 << MacroName;
368
369 // Show the definition of this macro within the PCH file.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000370 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
371 FindMacro(Buffers, Missing);
372 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
373 SourceLocation PCHMissingLoc =
374 SourceMgr.getLocForStartOfFile(MacroLoc.first)
375 .getFileLocWithOffset(MacroLoc.second);
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000376 Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000377
378 ConflictingDefines = true;
379 continue;
380 }
Mike Stump1eb44332009-09-09 15:08:12 +0000381
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000382 // If the macro doesn't conflict, then we'll just pick up the macro
383 // definition from the PCH file. Warn the user that they made a mistake.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000384 if (ConflictingDefines)
385 continue; // Don't complain if there are already conflicting defs
Mike Stump1eb44332009-09-09 15:08:12 +0000386
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000387 if (!MissingDefines) {
388 Reader.Diag(diag::warn_cmdline_missing_macro_defs);
389 MissingDefines = true;
390 }
391
392 // Show the definition of this macro within the PCH file.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000393 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
394 FindMacro(Buffers, Missing);
395 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
396 SourceLocation PCHMissingLoc =
397 SourceMgr.getLocForStartOfFile(MacroLoc.first)
398 .getFileLocWithOffset(MacroLoc.second);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000399 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
400 }
Mike Stump1eb44332009-09-09 15:08:12 +0000401
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000402 if (ConflictingDefines)
403 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000404
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000405 // Determine what predefines were introduced based on command-line
406 // parameters that were not present when building the PCH
407 // file. Extra #defines are okay, so long as the identifiers being
408 // defined were not used within the precompiled header.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000409 std::vector<llvm::StringRef> ExtraPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000410 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
411 PCHLines.begin(), PCHLines.end(),
Mike Stump1eb44332009-09-09 15:08:12 +0000412 std::back_inserter(ExtraPredefines));
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000413 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000414 llvm::StringRef &Extra = ExtraPredefines[I];
415 if (!Extra.startswith("#define ")) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000416 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
417 return true;
418 }
419
420 // This is an extra macro definition. Determine the name of the
421 // macro we're defining.
422 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000423 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000424 = Extra.find_first_of("( \n\r", StartOfMacroName);
425 assert(EndOfMacroName != std::string::npos &&
426 "Couldn't find the end of the macro name");
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000427 llvm::StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000428
429 // Check whether this name was used somewhere in the PCH file. If
430 // so, defining it as a macro could change behavior, so we reject
431 // the PCH file.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000432 if (IdentifierInfo *II = Reader.get(MacroName)) {
Daniel Dunbar4fda42e2009-11-11 00:52:00 +0000433 Reader.Diag(diag::warn_macro_name_used_in_pch) << II;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000434 return true;
435 }
436
437 // Add this definition to the suggested predefines buffer.
438 SuggestedPredefines += Extra;
439 SuggestedPredefines += '\n';
440 }
441
442 // If we get here, it's because the predefines buffer had compatible
443 // contents. Accept the PCH file.
444 return false;
445}
446
Douglas Gregor12fab312010-03-16 16:35:32 +0000447void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
448 unsigned ID) {
449 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
450 ++NumHeaderInfos;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000451}
452
453void PCHValidator::ReadCounter(unsigned Value) {
454 PP.setCounterValue(Value);
455}
456
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000457//===----------------------------------------------------------------------===//
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000458// AST reader implementation
Douglas Gregor668c1a42009-04-21 22:25:48 +0000459//===----------------------------------------------------------------------===//
460
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000461void
Sebastian Redl571db7f2010-08-18 23:56:56 +0000462ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000463 DeserializationListener = Listener;
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000464}
465
Chris Lattner4c6f9522009-04-27 05:14:47 +0000466
Douglas Gregor668c1a42009-04-21 22:25:48 +0000467namespace {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000468class ASTSelectorLookupTrait {
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000469 ASTReader &Reader;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000470
471public:
Sebastian Redl5d050072010-08-04 17:20:04 +0000472 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000473 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +0000474 ObjCMethodList Instance, Factory;
475 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000476
477 typedef Selector external_key_type;
478 typedef external_key_type internal_key_type;
479
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000480 explicit ASTSelectorLookupTrait(ASTReader &Reader) : Reader(Reader) { }
Mike Stump1eb44332009-09-09 15:08:12 +0000481
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000482 static bool EqualKey(const internal_key_type& a,
483 const internal_key_type& b) {
484 return a == b;
485 }
Mike Stump1eb44332009-09-09 15:08:12 +0000486
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000487 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +0000488 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000489 }
Mike Stump1eb44332009-09-09 15:08:12 +0000490
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000491 // This hopefully will just get inlined and removed by the optimizer.
492 static const internal_key_type&
493 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump1eb44332009-09-09 15:08:12 +0000494
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000495 static std::pair<unsigned, unsigned>
496 ReadKeyDataLength(const unsigned char*& d) {
497 using namespace clang::io;
498 unsigned KeyLen = ReadUnalignedLE16(d);
499 unsigned DataLen = ReadUnalignedLE16(d);
500 return std::make_pair(KeyLen, DataLen);
501 }
Mike Stump1eb44332009-09-09 15:08:12 +0000502
Douglas Gregor83941df2009-04-25 17:48:32 +0000503 internal_key_type ReadKey(const unsigned char* d, unsigned) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000504 using namespace clang::io;
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000505 SelectorTable &SelTable = Reader.getContext()->Selectors;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000506 unsigned N = ReadUnalignedLE16(d);
Mike Stump1eb44332009-09-09 15:08:12 +0000507 IdentifierInfo *FirstII
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000508 = Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
509 if (N == 0)
510 return SelTable.getNullarySelector(FirstII);
511 else if (N == 1)
512 return SelTable.getUnarySelector(FirstII);
513
514 llvm::SmallVector<IdentifierInfo *, 16> Args;
515 Args.push_back(FirstII);
516 for (unsigned I = 1; I != N; ++I)
517 Args.push_back(Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)));
518
Douglas Gregor75fdb232009-05-22 22:45:36 +0000519 return SelTable.getSelector(N, Args.data());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000520 }
Mike Stump1eb44332009-09-09 15:08:12 +0000521
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000522 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen) {
523 using namespace clang::io;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000524
525 data_type Result;
526
Sebastian Redl5d050072010-08-04 17:20:04 +0000527 Result.ID = ReadUnalignedLE32(d);
528 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
529 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
530
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000531 // Load instance methods
532 ObjCMethodList *Prev = 0;
533 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000534 ObjCMethodDecl *Method
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000535 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl5d050072010-08-04 17:20:04 +0000536 if (!Result.Instance.Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000537 // This is the first method, which is the easy case.
Sebastian Redl5d050072010-08-04 17:20:04 +0000538 Result.Instance.Method = Method;
539 Prev = &Result.Instance;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000540 continue;
541 }
542
Ted Kremenek298ed872010-02-11 00:53:01 +0000543 ObjCMethodList *Mem =
544 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
545 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000546 Prev = Prev->Next;
547 }
548
549 // Load factory methods
550 Prev = 0;
551 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000552 ObjCMethodDecl *Method
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000553 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl5d050072010-08-04 17:20:04 +0000554 if (!Result.Factory.Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000555 // This is the first method, which is the easy case.
Sebastian Redl5d050072010-08-04 17:20:04 +0000556 Result.Factory.Method = Method;
557 Prev = &Result.Factory;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000558 continue;
559 }
560
Ted Kremenek298ed872010-02-11 00:53:01 +0000561 ObjCMethodList *Mem =
562 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
563 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000564 Prev = Prev->Next;
565 }
566
567 return Result;
568 }
569};
Mike Stump1eb44332009-09-09 15:08:12 +0000570
571} // end anonymous namespace
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000572
573/// \brief The on-disk hash table used for the global method pool.
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000574typedef OnDiskChainedHashTable<ASTSelectorLookupTrait>
575 ASTSelectorLookupTable;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000576
Sebastian Redlc3632732010-10-05 15:59:54 +0000577namespace clang {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000578class ASTIdentifierLookupTrait {
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000579 ASTReader &Reader;
Sebastian Redlc3632732010-10-05 15:59:54 +0000580 ASTReader::PerFileData &F;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000581
582 // If we know the IdentifierInfo in advance, it is here and we will
583 // not build a new one. Used when deserializing information about an
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000584 // identifier that was constructed before the AST file was read.
Douglas Gregor668c1a42009-04-21 22:25:48 +0000585 IdentifierInfo *KnownII;
586
587public:
588 typedef IdentifierInfo * data_type;
589
590 typedef const std::pair<const char*, unsigned> external_key_type;
591
592 typedef external_key_type internal_key_type;
593
Sebastian Redlc3632732010-10-05 15:59:54 +0000594 ASTIdentifierLookupTrait(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redld27d3fc2010-07-21 22:31:37 +0000595 IdentifierInfo *II = 0)
Sebastian Redlc3632732010-10-05 15:59:54 +0000596 : Reader(Reader), F(F), KnownII(II) { }
Mike Stump1eb44332009-09-09 15:08:12 +0000597
Douglas Gregor668c1a42009-04-21 22:25:48 +0000598 static bool EqualKey(const internal_key_type& a,
599 const internal_key_type& b) {
600 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
601 : false;
602 }
Mike Stump1eb44332009-09-09 15:08:12 +0000603
Douglas Gregor668c1a42009-04-21 22:25:48 +0000604 static unsigned ComputeHash(const internal_key_type& a) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000605 return llvm::HashString(llvm::StringRef(a.first, a.second));
Douglas Gregor668c1a42009-04-21 22:25:48 +0000606 }
Mike Stump1eb44332009-09-09 15:08:12 +0000607
Douglas Gregor668c1a42009-04-21 22:25:48 +0000608 // This hopefully will just get inlined and removed by the optimizer.
609 static const internal_key_type&
610 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump1eb44332009-09-09 15:08:12 +0000611
Douglas Gregor95f42922010-10-14 22:11:03 +0000612 // This hopefully will just get inlined and removed by the optimizer.
613 static const external_key_type&
614 GetExternalKey(const internal_key_type& x) { return x; }
615
Douglas Gregor668c1a42009-04-21 22:25:48 +0000616 static std::pair<unsigned, unsigned>
617 ReadKeyDataLength(const unsigned char*& d) {
618 using namespace clang::io;
Douglas Gregor5f8e3302009-04-25 20:26:24 +0000619 unsigned DataLen = ReadUnalignedLE16(d);
Douglas Gregord6595a42009-04-25 21:04:17 +0000620 unsigned KeyLen = ReadUnalignedLE16(d);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000621 return std::make_pair(KeyLen, DataLen);
622 }
Mike Stump1eb44332009-09-09 15:08:12 +0000623
Douglas Gregor668c1a42009-04-21 22:25:48 +0000624 static std::pair<const char*, unsigned>
625 ReadKey(const unsigned char* d, unsigned n) {
626 assert(n >= 2 && d[n-1] == '\0');
627 return std::make_pair((const char*) d, n-1);
628 }
Mike Stump1eb44332009-09-09 15:08:12 +0000629
630 IdentifierInfo *ReadData(const internal_key_type& k,
Douglas Gregor668c1a42009-04-21 22:25:48 +0000631 const unsigned char* d,
632 unsigned DataLen) {
633 using namespace clang::io;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000634 IdentID ID = ReadUnalignedLE32(d);
Douglas Gregora92193e2009-04-28 21:18:29 +0000635 bool IsInteresting = ID & 0x01;
636
637 // Wipe out the "is interesting" bit.
638 ID = ID >> 1;
639
640 if (!IsInteresting) {
Sebastian Redl083abdf2010-07-27 23:01:28 +0000641 // For uninteresting identifiers, just build the IdentifierInfo
Douglas Gregora92193e2009-04-28 21:18:29 +0000642 // and associate it with the persistent ID.
643 IdentifierInfo *II = KnownII;
644 if (!II)
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000645 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregora92193e2009-04-28 21:18:29 +0000646 Reader.SetIdentifierInfo(ID, II);
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000647 II->setIsFromAST();
Douglas Gregora92193e2009-04-28 21:18:29 +0000648 return II;
649 }
650
Douglas Gregor5998da52009-04-28 21:32:13 +0000651 unsigned Bits = ReadUnalignedLE16(d);
Douglas Gregor2deaea32009-04-22 18:49:13 +0000652 bool CPlusPlusOperatorKeyword = Bits & 0x01;
653 Bits >>= 1;
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000654 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
655 Bits >>= 1;
Douglas Gregor2deaea32009-04-22 18:49:13 +0000656 bool Poisoned = Bits & 0x01;
657 Bits >>= 1;
658 bool ExtensionToken = Bits & 0x01;
659 Bits >>= 1;
660 bool hasMacroDefinition = Bits & 0x01;
661 Bits >>= 1;
662 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
663 Bits >>= 10;
Mike Stump1eb44332009-09-09 15:08:12 +0000664
Douglas Gregor2deaea32009-04-22 18:49:13 +0000665 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregor5998da52009-04-28 21:32:13 +0000666 DataLen -= 6;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000667
668 // Build the IdentifierInfo itself and link the identifier ID with
669 // the new IdentifierInfo.
670 IdentifierInfo *II = KnownII;
671 if (!II)
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000672 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000673 Reader.SetIdentifierInfo(ID, II);
674
Douglas Gregor2deaea32009-04-22 18:49:13 +0000675 // Set or check the various bits in the IdentifierInfo structure.
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000676 // Token IDs are read-only.
677 if (HasRevertedTokenIDToIdentifier)
678 II->RevertTokenIDToIdentifier();
Douglas Gregor2deaea32009-04-22 18:49:13 +0000679 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
Mike Stump1eb44332009-09-09 15:08:12 +0000680 assert(II->isExtensionToken() == ExtensionToken &&
Douglas Gregor2deaea32009-04-22 18:49:13 +0000681 "Incorrect extension token flag");
682 (void)ExtensionToken;
683 II->setIsPoisoned(Poisoned);
684 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
685 "Incorrect C++ operator keyword flag");
686 (void)CPlusPlusOperatorKeyword;
687
Douglas Gregor37e26842009-04-21 23:56:24 +0000688 // If this identifier is a macro, deserialize the macro
689 // definition.
690 if (hasMacroDefinition) {
Douglas Gregor5998da52009-04-28 21:32:13 +0000691 uint32_t Offset = ReadUnalignedLE32(d);
Douglas Gregor295a2a62010-10-30 00:23:06 +0000692 Reader.SetIdentifierIsMacro(II, F, Offset);
Douglas Gregor5998da52009-04-28 21:32:13 +0000693 DataLen -= 4;
Douglas Gregor37e26842009-04-21 23:56:24 +0000694 }
Douglas Gregor668c1a42009-04-21 22:25:48 +0000695
696 // Read all of the declarations visible at global scope with this
697 // name.
Chris Lattner6bf690f2009-04-27 22:17:41 +0000698 if (Reader.getContext() == 0) return II;
Douglas Gregord89275b2009-07-06 18:54:52 +0000699 if (DataLen > 0) {
700 llvm::SmallVector<uint32_t, 4> DeclIDs;
701 for (; DataLen > 0; DataLen -= 4)
702 DeclIDs.push_back(ReadUnalignedLE32(d));
703 Reader.SetGloballyVisibleDecls(II, DeclIDs);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000704 }
Mike Stump1eb44332009-09-09 15:08:12 +0000705
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000706 II->setIsFromAST();
Douglas Gregor668c1a42009-04-21 22:25:48 +0000707 return II;
708 }
709};
Mike Stump1eb44332009-09-09 15:08:12 +0000710
711} // end anonymous namespace
Douglas Gregor668c1a42009-04-21 22:25:48 +0000712
713/// \brief The on-disk hash table used to contain information about
714/// all of the identifiers in the program.
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000715typedef OnDiskChainedHashTable<ASTIdentifierLookupTrait>
716 ASTIdentifierLookupTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000717
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000718namespace {
719class ASTDeclContextNameLookupTrait {
720 ASTReader &Reader;
721
722public:
723 /// \brief Pair of begin/end iterators for DeclIDs.
724 typedef std::pair<DeclID *, DeclID *> data_type;
725
726 /// \brief Special internal key for declaration names.
727 /// The hash table creates keys for comparison; we do not create
728 /// a DeclarationName for the internal key to avoid deserializing types.
729 struct DeclNameKey {
730 DeclarationName::NameKind Kind;
731 uint64_t Data;
732 DeclNameKey() : Kind((DeclarationName::NameKind)0), Data(0) { }
733 };
734
735 typedef DeclarationName external_key_type;
736 typedef DeclNameKey internal_key_type;
737
738 explicit ASTDeclContextNameLookupTrait(ASTReader &Reader) : Reader(Reader) { }
739
740 static bool EqualKey(const internal_key_type& a,
741 const internal_key_type& b) {
742 return a.Kind == b.Kind && a.Data == b.Data;
743 }
744
745 unsigned ComputeHash(const DeclNameKey &Key) const {
746 llvm::FoldingSetNodeID ID;
747 ID.AddInteger(Key.Kind);
748
749 switch (Key.Kind) {
750 case DeclarationName::Identifier:
751 case DeclarationName::CXXLiteralOperatorName:
752 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
753 break;
754 case DeclarationName::ObjCZeroArgSelector:
755 case DeclarationName::ObjCOneArgSelector:
756 case DeclarationName::ObjCMultiArgSelector:
757 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
758 break;
759 case DeclarationName::CXXConstructorName:
760 case DeclarationName::CXXDestructorName:
761 case DeclarationName::CXXConversionFunctionName:
762 ID.AddInteger((TypeID)Key.Data);
763 break;
764 case DeclarationName::CXXOperatorName:
765 ID.AddInteger((OverloadedOperatorKind)Key.Data);
766 break;
767 case DeclarationName::CXXUsingDirective:
768 break;
769 }
770
771 return ID.ComputeHash();
772 }
773
774 internal_key_type GetInternalKey(const external_key_type& Name) const {
775 DeclNameKey Key;
776 Key.Kind = Name.getNameKind();
777 switch (Name.getNameKind()) {
778 case DeclarationName::Identifier:
779 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
780 break;
781 case DeclarationName::ObjCZeroArgSelector:
782 case DeclarationName::ObjCOneArgSelector:
783 case DeclarationName::ObjCMultiArgSelector:
784 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
785 break;
786 case DeclarationName::CXXConstructorName:
787 case DeclarationName::CXXDestructorName:
788 case DeclarationName::CXXConversionFunctionName:
789 Key.Data = Reader.GetTypeID(Name.getCXXNameType());
790 break;
791 case DeclarationName::CXXOperatorName:
792 Key.Data = Name.getCXXOverloadedOperator();
793 break;
794 case DeclarationName::CXXLiteralOperatorName:
795 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
796 break;
797 case DeclarationName::CXXUsingDirective:
798 break;
799 }
Michael J. Spencer20249a12010-10-21 03:16:25 +0000800
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000801 return Key;
802 }
803
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +0000804 external_key_type GetExternalKey(const internal_key_type& Key) const {
805 ASTContext *Context = Reader.getContext();
806 switch (Key.Kind) {
807 case DeclarationName::Identifier:
808 return DeclarationName((IdentifierInfo*)Key.Data);
809
810 case DeclarationName::ObjCZeroArgSelector:
811 case DeclarationName::ObjCOneArgSelector:
812 case DeclarationName::ObjCMultiArgSelector:
813 return DeclarationName(Selector(Key.Data));
814
815 case DeclarationName::CXXConstructorName:
816 return Context->DeclarationNames.getCXXConstructorName(
817 Context->getCanonicalType(Reader.GetType(Key.Data)));
818
819 case DeclarationName::CXXDestructorName:
820 return Context->DeclarationNames.getCXXDestructorName(
821 Context->getCanonicalType(Reader.GetType(Key.Data)));
822
823 case DeclarationName::CXXConversionFunctionName:
824 return Context->DeclarationNames.getCXXConversionFunctionName(
825 Context->getCanonicalType(Reader.GetType(Key.Data)));
826
827 case DeclarationName::CXXOperatorName:
828 return Context->DeclarationNames.getCXXOperatorName(
829 (OverloadedOperatorKind)Key.Data);
830
831 case DeclarationName::CXXLiteralOperatorName:
832 return Context->DeclarationNames.getCXXLiteralOperatorName(
833 (IdentifierInfo*)Key.Data);
834
835 case DeclarationName::CXXUsingDirective:
836 return DeclarationName::getUsingDirectiveName();
837 }
838
839 llvm_unreachable("Invalid Name Kind ?");
840 }
841
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000842 static std::pair<unsigned, unsigned>
843 ReadKeyDataLength(const unsigned char*& d) {
844 using namespace clang::io;
845 unsigned KeyLen = ReadUnalignedLE16(d);
846 unsigned DataLen = ReadUnalignedLE16(d);
847 return std::make_pair(KeyLen, DataLen);
848 }
849
850 internal_key_type ReadKey(const unsigned char* d, unsigned) {
851 using namespace clang::io;
852
853 DeclNameKey Key;
854 Key.Kind = (DeclarationName::NameKind)*d++;
855 switch (Key.Kind) {
856 case DeclarationName::Identifier:
857 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
858 break;
859 case DeclarationName::ObjCZeroArgSelector:
860 case DeclarationName::ObjCOneArgSelector:
861 case DeclarationName::ObjCMultiArgSelector:
Michael J. Spencer20249a12010-10-21 03:16:25 +0000862 Key.Data =
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000863 (uint64_t)Reader.DecodeSelector(ReadUnalignedLE32(d)).getAsOpaquePtr();
864 break;
865 case DeclarationName::CXXConstructorName:
866 case DeclarationName::CXXDestructorName:
867 case DeclarationName::CXXConversionFunctionName:
868 Key.Data = ReadUnalignedLE32(d); // TypeID
869 break;
870 case DeclarationName::CXXOperatorName:
871 Key.Data = *d++; // OverloadedOperatorKind
872 break;
873 case DeclarationName::CXXLiteralOperatorName:
874 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
875 break;
876 case DeclarationName::CXXUsingDirective:
877 break;
878 }
Michael J. Spencer20249a12010-10-21 03:16:25 +0000879
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000880 return Key;
881 }
882
883 data_type ReadData(internal_key_type, const unsigned char* d,
884 unsigned DataLen) {
885 using namespace clang::io;
886 unsigned NumDecls = ReadUnalignedLE16(d);
887 DeclID *Start = (DeclID *)d;
888 return std::make_pair(Start, Start + NumDecls);
889 }
890};
891
892} // end anonymous namespace
893
894/// \brief The on-disk hash table used for the DeclContext's Name lookup table.
895typedef OnDiskChainedHashTable<ASTDeclContextNameLookupTrait>
896 ASTDeclContextNameLookupTable;
897
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000898bool ASTReader::ReadDeclContextStorage(llvm::BitstreamCursor &Cursor,
899 const std::pair<uint64_t, uint64_t> &Offsets,
900 DeclContextInfo &Info) {
901 SavedStreamPosition SavedPosition(Cursor);
902 // First the lexical decls.
903 if (Offsets.first != 0) {
904 Cursor.JumpToBit(Offsets.first);
905
906 RecordData Record;
907 const char *Blob;
908 unsigned BlobLen;
909 unsigned Code = Cursor.ReadCode();
910 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
911 if (RecCode != DECL_CONTEXT_LEXICAL) {
912 Error("Expected lexical block");
913 return true;
914 }
915
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000916 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob);
917 Info.NumLexicalDecls = BlobLen / sizeof(KindDeclIDPair);
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000918 } else {
919 Info.LexicalDecls = 0;
920 Info.NumLexicalDecls = 0;
921 }
922
923 // Now the lookup table.
924 if (Offsets.second != 0) {
925 Cursor.JumpToBit(Offsets.second);
926
927 RecordData Record;
928 const char *Blob;
929 unsigned BlobLen;
930 unsigned Code = Cursor.ReadCode();
931 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
932 if (RecCode != DECL_CONTEXT_VISIBLE) {
933 Error("Expected visible lookup table block");
934 return true;
935 }
936 Info.NameLookupTableData
937 = ASTDeclContextNameLookupTable::Create(
938 (const unsigned char *)Blob + Record[0],
939 (const unsigned char *)Blob,
940 ASTDeclContextNameLookupTrait(*this));
Sebastian Redl0ea8f7f2010-08-24 00:50:00 +0000941 } else {
942 Info.NameLookupTableData = 0;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000943 }
944
945 return false;
946}
947
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000948void ASTReader::Error(const char *Msg) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000949 Diag(diag::err_fe_pch_malformed) << Msg;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000950}
951
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000952/// \brief Tell the AST listener about the predefines buffers in the chain.
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000953bool ASTReader::CheckPredefinesBuffers() {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000954 if (Listener)
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000955 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000956 ActualOriginalFileName,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000957 SuggestedPredefines);
Douglas Gregore721f952009-04-28 18:58:38 +0000958 return false;
Douglas Gregore1d918e2009-04-10 23:10:45 +0000959}
960
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000961//===----------------------------------------------------------------------===//
962// Source Manager Deserialization
963//===----------------------------------------------------------------------===//
964
Douglas Gregorbd945002009-04-13 16:31:14 +0000965/// \brief Read the line table in the source manager block.
Sebastian Redlc3632732010-10-05 15:59:54 +0000966/// \returns true if there was an error.
967bool ASTReader::ParseLineTable(PerFileData &F,
968 llvm::SmallVectorImpl<uint64_t> &Record) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000969 unsigned Idx = 0;
970 LineTableInfo &LineTable = SourceMgr.getLineTable();
971
972 // Parse the file names
Douglas Gregorff0a9872009-04-13 17:12:42 +0000973 std::map<int, int> FileIDs;
974 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000975 // Extract the file name
976 unsigned FilenameLen = Record[Idx++];
977 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
978 Idx += FilenameLen;
Douglas Gregore650c8c2009-07-07 00:12:59 +0000979 MaybeAddSystemRootToFilename(Filename);
Mike Stump1eb44332009-09-09 15:08:12 +0000980 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
Douglas Gregorff0a9872009-04-13 17:12:42 +0000981 Filename.size());
Douglas Gregorbd945002009-04-13 16:31:14 +0000982 }
983
984 // Parse the line entries
985 std::vector<LineEntry> Entries;
986 while (Idx < Record.size()) {
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000987 int FID = Record[Idx++];
Douglas Gregorbd945002009-04-13 16:31:14 +0000988
989 // Extract the line entries
990 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000991 assert(NumEntries && "Numentries is 00000");
Douglas Gregorbd945002009-04-13 16:31:14 +0000992 Entries.clear();
993 Entries.reserve(NumEntries);
994 for (unsigned I = 0; I != NumEntries; ++I) {
995 unsigned FileOffset = Record[Idx++];
996 unsigned LineNo = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000997 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump1eb44332009-09-09 15:08:12 +0000998 SrcMgr::CharacteristicKind FileKind
Douglas Gregorbd945002009-04-13 16:31:14 +0000999 = (SrcMgr::CharacteristicKind)Record[Idx++];
1000 unsigned IncludeOffset = Record[Idx++];
1001 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1002 FileKind, IncludeOffset));
1003 }
1004 LineTable.AddEntry(FID, Entries);
1005 }
1006
1007 return false;
1008}
1009
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001010namespace {
1011
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001012class ASTStatData {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001013public:
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001014 const ino_t ino;
1015 const dev_t dev;
1016 const mode_t mode;
1017 const time_t mtime;
1018 const off_t size;
Mike Stump1eb44332009-09-09 15:08:12 +00001019
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001020 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Chris Lattner74e976b2010-11-23 19:28:12 +00001021 : ino(i), dev(d), mode(mo), mtime(m), size(s) {}
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001022};
1023
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001024class ASTStatLookupTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001025 public:
1026 typedef const char *external_key_type;
1027 typedef const char *internal_key_type;
1028
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001029 typedef ASTStatData data_type;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001030
1031 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001032 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001033 }
1034
1035 static internal_key_type GetInternalKey(const char *path) { return path; }
1036
1037 static bool EqualKey(internal_key_type a, internal_key_type b) {
1038 return strcmp(a, b) == 0;
1039 }
1040
1041 static std::pair<unsigned, unsigned>
1042 ReadKeyDataLength(const unsigned char*& d) {
1043 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1044 unsigned DataLen = (unsigned) *d++;
1045 return std::make_pair(KeyLen + 1, DataLen);
1046 }
1047
1048 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
1049 return (const char *)d;
1050 }
1051
1052 static data_type ReadData(const internal_key_type, const unsigned char *d,
1053 unsigned /*DataLen*/) {
1054 using namespace clang::io;
1055
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001056 ino_t ino = (ino_t) ReadUnalignedLE32(d);
1057 dev_t dev = (dev_t) ReadUnalignedLE32(d);
1058 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump1eb44332009-09-09 15:08:12 +00001059 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001060 off_t size = (off_t) ReadUnalignedLE64(d);
1061 return data_type(ino, dev, mode, mtime, size);
1062 }
1063};
1064
1065/// \brief stat() cache for precompiled headers.
1066///
1067/// This cache is very similar to the stat cache used by pretokenized
1068/// headers.
Chris Lattner10e286a2010-11-23 19:19:34 +00001069class ASTStatCache : public FileSystemStatCache {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001070 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001071 CacheTy *Cache;
1072
1073 unsigned &NumStatHits, &NumStatMisses;
Mike Stump1eb44332009-09-09 15:08:12 +00001074public:
Chris Lattner74e976b2010-11-23 19:28:12 +00001075 ASTStatCache(const unsigned char *Buckets, const unsigned char *Base,
1076 unsigned &NumStatHits, unsigned &NumStatMisses)
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001077 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
1078 Cache = CacheTy::Create(Buckets, Base);
1079 }
1080
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001081 ~ASTStatCache() { delete Cache; }
Mike Stump1eb44332009-09-09 15:08:12 +00001082
Chris Lattner898a0612010-11-23 21:17:56 +00001083 LookupResult getStat(const char *Path, struct stat &StatBuf,
1084 int *FileDescriptor) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001085 // Do the lookup for the file's data in the AST file.
Chris Lattner10e286a2010-11-23 19:19:34 +00001086 CacheTy::iterator I = Cache->find(Path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001087
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001088 // If we don't get a hit in the AST file just forward to 'stat'.
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001089 if (I == Cache->end()) {
1090 ++NumStatMisses;
Chris Lattner898a0612010-11-23 21:17:56 +00001091 return statChained(Path, StatBuf, FileDescriptor);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001092 }
Mike Stump1eb44332009-09-09 15:08:12 +00001093
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001094 ++NumStatHits;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001095 ASTStatData Data = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001096
Chris Lattner10e286a2010-11-23 19:19:34 +00001097 StatBuf.st_ino = Data.ino;
1098 StatBuf.st_dev = Data.dev;
1099 StatBuf.st_mtime = Data.mtime;
1100 StatBuf.st_mode = Data.mode;
1101 StatBuf.st_size = Data.size;
Chris Lattnerd6f61112010-11-23 20:05:15 +00001102 return CacheExists;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001103 }
1104};
1105} // end anonymous namespace
1106
1107
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001108/// \brief Read a source manager block
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001109ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(PerFileData &F) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001110 using namespace SrcMgr;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001111
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001112 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001113
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001114 // Set the source-location entry cursor to the current position in
1115 // the stream. This cursor will be used to read the contents of the
1116 // source manager block initially, and then lazily read
1117 // source-location entries as needed.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001118 SLocEntryCursor = F.Stream;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001119
1120 // The stream itself is going to skip over the source manager block.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001121 if (F.Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001122 Error("malformed block record in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001123 return Failure;
1124 }
1125
1126 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001127 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001128 Error("malformed source manager block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001129 return Failure;
1130 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001131
Douglas Gregor14f79002009-04-10 03:52:48 +00001132 RecordData Record;
1133 while (true) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001134 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregor14f79002009-04-10 03:52:48 +00001135 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001136 if (SLocEntryCursor.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001137 Error("error at end of Source Manager block in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001138 return Failure;
1139 }
Douglas Gregore1d918e2009-04-10 23:10:45 +00001140 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +00001141 }
Mike Stump1eb44332009-09-09 15:08:12 +00001142
Douglas Gregor14f79002009-04-10 03:52:48 +00001143 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1144 // No known subblocks, always skip them.
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001145 SLocEntryCursor.ReadSubBlockID();
1146 if (SLocEntryCursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001147 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001148 return Failure;
1149 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001150 continue;
1151 }
Mike Stump1eb44332009-09-09 15:08:12 +00001152
Douglas Gregor14f79002009-04-10 03:52:48 +00001153 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001154 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregor14f79002009-04-10 03:52:48 +00001155 continue;
1156 }
Mike Stump1eb44332009-09-09 15:08:12 +00001157
Douglas Gregor14f79002009-04-10 03:52:48 +00001158 // Read a record.
1159 const char *BlobStart;
1160 unsigned BlobLen;
1161 Record.clear();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001162 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001163 default: // Default behavior: ignore.
1164 break;
1165
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001166 case SM_LINE_TABLE:
Sebastian Redlc3632732010-10-05 15:59:54 +00001167 if (ParseLineTable(F, Record))
Douglas Gregorbd945002009-04-13 16:31:14 +00001168 return Failure;
Chris Lattner2c78b872009-04-14 23:22:57 +00001169 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001170
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001171 case SM_SLOC_FILE_ENTRY:
1172 case SM_SLOC_BUFFER_ENTRY:
1173 case SM_SLOC_INSTANTIATION_ENTRY:
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001174 // Once we hit one of the source location entries, we're done.
1175 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +00001176 }
1177 }
1178}
1179
Sebastian Redl190faf72010-07-20 21:50:20 +00001180/// \brief Get a cursor that's correctly positioned for reading the source
1181/// location entry with the given ID.
Sebastian Redlc3632732010-10-05 15:59:54 +00001182ASTReader::PerFileData *ASTReader::SLocCursorForID(unsigned ID) {
Sebastian Redl190faf72010-07-20 21:50:20 +00001183 assert(ID != 0 && ID <= TotalNumSLocEntries &&
1184 "SLocCursorForID should only be called for real IDs.");
1185
1186 ID -= 1;
1187 PerFileData *F = 0;
1188 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1189 F = Chain[N - I - 1];
1190 if (ID < F->LocalNumSLocEntries)
1191 break;
1192 ID -= F->LocalNumSLocEntries;
1193 }
1194 assert(F && F->LocalNumSLocEntries > ID && "Chain corrupted");
1195
1196 F->SLocEntryCursor.JumpToBit(F->SLocOffsets[ID]);
Sebastian Redlc3632732010-10-05 15:59:54 +00001197 return F;
Sebastian Redl190faf72010-07-20 21:50:20 +00001198}
1199
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001200/// \brief Read in the source location entry with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001201ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(unsigned ID) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001202 if (ID == 0)
1203 return Success;
1204
1205 if (ID > TotalNumSLocEntries) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001206 Error("source location entry ID out-of-range for AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001207 return Failure;
1208 }
1209
Sebastian Redlc3632732010-10-05 15:59:54 +00001210 PerFileData *F = SLocCursorForID(ID);
1211 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001212
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001213 ++NumSLocEntriesRead;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001214 unsigned Code = SLocEntryCursor.ReadCode();
1215 if (Code == llvm::bitc::END_BLOCK ||
1216 Code == llvm::bitc::ENTER_SUBBLOCK ||
1217 Code == llvm::bitc::DEFINE_ABBREV) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001218 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001219 return Failure;
1220 }
1221
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001222 RecordData Record;
1223 const char *BlobStart;
1224 unsigned BlobLen;
1225 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1226 default:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001227 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001228 return Failure;
1229
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001230 case SM_SLOC_FILE_ENTRY: {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001231 std::string Filename(BlobStart, BlobStart + BlobLen);
1232 MaybeAddSystemRootToFilename(Filename);
Chris Lattner39b49bc2010-11-23 08:35:12 +00001233 const FileEntry *File = FileMgr.getFile(Filename);
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001234 if (File == 0) {
1235 std::string ErrorStr = "could not find file '";
Douglas Gregore650c8c2009-07-07 00:12:59 +00001236 ErrorStr += Filename;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001237 ErrorStr += "' referenced by AST file";
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001238 Error(ErrorStr.c_str());
1239 return Failure;
1240 }
Mike Stump1eb44332009-09-09 15:08:12 +00001241
Douglas Gregor2d52be52010-03-21 22:49:54 +00001242 if (Record.size() < 10) {
Ted Kremenek1857f622010-03-18 21:23:05 +00001243 Error("source location entry is incorrect");
1244 return Failure;
1245 }
1246
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001247 if (!DisableValidation &&
1248 ((off_t)Record[4] != File->getSize()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001249#if !defined(LLVM_ON_WIN32)
1250 // In our regression testing, the Windows file system seems to
1251 // have inconsistent modification times that sometimes
1252 // erroneously trigger this error-handling path.
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001253 || (time_t)Record[5] != File->getModificationTime()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001254#endif
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001255 )) {
Douglas Gregor2d52be52010-03-21 22:49:54 +00001256 Diag(diag::err_fe_pch_file_modified)
1257 << Filename;
1258 return Failure;
1259 }
1260
Chris Lattner75dfb652010-11-23 09:19:42 +00001261 FileID FID = SourceMgr.createFileID(File, ReadSourceLocation(*F, Record[1]),
1262 (SrcMgr::CharacteristicKind)Record[2],
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001263 ID, Record[0]);
1264 if (Record[3])
1265 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile())
1266 .setHasLineDirectives();
1267
Douglas Gregor12fab312010-03-16 16:35:32 +00001268 // Reconstruct header-search information for this file.
1269 HeaderFileInfo HFI;
Douglas Gregor2d52be52010-03-21 22:49:54 +00001270 HFI.isImport = Record[6];
1271 HFI.DirInfo = Record[7];
1272 HFI.NumIncludes = Record[8];
1273 HFI.ControllingMacroID = Record[9];
Douglas Gregor12fab312010-03-16 16:35:32 +00001274 if (Listener)
1275 Listener->ReadHeaderFileInfo(HFI, File->getUID());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001276 break;
1277 }
1278
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001279 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001280 const char *Name = BlobStart;
1281 unsigned Offset = Record[0];
1282 unsigned Code = SLocEntryCursor.ReadCode();
1283 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001284 unsigned RecCode
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001285 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001286
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001287 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001288 Error("AST record has invalid code");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001289 return Failure;
1290 }
1291
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001292 llvm::MemoryBuffer *Buffer
Chris Lattnera0a270c2010-04-05 22:42:27 +00001293 = llvm::MemoryBuffer::getMemBuffer(llvm::StringRef(BlobStart, BlobLen - 1),
1294 Name);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001295 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID, Offset);
Mike Stump1eb44332009-09-09 15:08:12 +00001296
Douglas Gregor92b059e2009-04-28 20:33:11 +00001297 if (strcmp(Name, "<built-in>") == 0) {
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +00001298 PCHPredefinesBlock Block = {
1299 BufferID,
1300 llvm::StringRef(BlobStart, BlobLen - 1)
1301 };
1302 PCHPredefinesBuffers.push_back(Block);
Douglas Gregor92b059e2009-04-28 20:33:11 +00001303 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001304
1305 break;
1306 }
1307
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001308 case SM_SLOC_INSTANTIATION_ENTRY: {
Sebastian Redlc3632732010-10-05 15:59:54 +00001309 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001310 SourceMgr.createInstantiationLoc(SpellingLoc,
Sebastian Redlc3632732010-10-05 15:59:54 +00001311 ReadSourceLocation(*F, Record[2]),
1312 ReadSourceLocation(*F, Record[3]),
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001313 Record[4],
1314 ID,
1315 Record[0]);
1316 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001317 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001318 }
1319
1320 return Success;
1321}
1322
Chris Lattner6367f6d2009-04-27 01:05:14 +00001323/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1324/// specified cursor. Read the abbreviations that are at the top of the block
1325/// and then leave the cursor pointing into the block.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001326bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattner6367f6d2009-04-27 01:05:14 +00001327 unsigned BlockID) {
1328 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001329 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001330 return Failure;
1331 }
Mike Stump1eb44332009-09-09 15:08:12 +00001332
Chris Lattner6367f6d2009-04-27 01:05:14 +00001333 while (true) {
Douglas Gregorecdcb882010-10-20 22:00:55 +00001334 uint64_t Offset = Cursor.GetCurrentBitNo();
Chris Lattner6367f6d2009-04-27 01:05:14 +00001335 unsigned Code = Cursor.ReadCode();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001336
Chris Lattner6367f6d2009-04-27 01:05:14 +00001337 // We expect all abbrevs to be at the start of the block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001338 if (Code != llvm::bitc::DEFINE_ABBREV) {
1339 Cursor.JumpToBit(Offset);
Chris Lattner6367f6d2009-04-27 01:05:14 +00001340 return false;
Douglas Gregorecdcb882010-10-20 22:00:55 +00001341 }
Chris Lattner6367f6d2009-04-27 01:05:14 +00001342 Cursor.ReadAbbrevRecord();
1343 }
1344}
1345
Douglas Gregor89d99802010-11-30 06:16:57 +00001346PreprocessedEntity *ASTReader::ReadMacroRecord(PerFileData &F, uint64_t Offset) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001347 assert(PP && "Forgot to set Preprocessor ?");
Douglas Gregorecdcb882010-10-20 22:00:55 +00001348 llvm::BitstreamCursor &Stream = F.MacroCursor;
Mike Stump1eb44332009-09-09 15:08:12 +00001349
Douglas Gregor37e26842009-04-21 23:56:24 +00001350 // Keep track of where we are in the stream, then jump back there
1351 // after reading this macro.
1352 SavedStreamPosition SavedPosition(Stream);
1353
1354 Stream.JumpToBit(Offset);
1355 RecordData Record;
1356 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1357 MacroInfo *Macro = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001358
Douglas Gregor37e26842009-04-21 23:56:24 +00001359 while (true) {
1360 unsigned Code = Stream.ReadCode();
1361 switch (Code) {
1362 case llvm::bitc::END_BLOCK:
Douglas Gregor89d99802010-11-30 06:16:57 +00001363 return 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001364
1365 case llvm::bitc::ENTER_SUBBLOCK:
1366 // No known subblocks, always skip them.
1367 Stream.ReadSubBlockID();
1368 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001369 Error("malformed block record in AST file");
Douglas Gregor89d99802010-11-30 06:16:57 +00001370 return 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001371 }
1372 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001373
Douglas Gregor37e26842009-04-21 23:56:24 +00001374 case llvm::bitc::DEFINE_ABBREV:
1375 Stream.ReadAbbrevRecord();
1376 continue;
1377 default: break;
1378 }
1379
1380 // Read a record.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001381 const char *BlobStart = 0;
1382 unsigned BlobLen = 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001383 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001384 PreprocessorRecordTypes RecType =
Michael J. Spencer20249a12010-10-21 03:16:25 +00001385 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart,
Douglas Gregorecdcb882010-10-20 22:00:55 +00001386 BlobLen);
Douglas Gregor37e26842009-04-21 23:56:24 +00001387 switch (RecType) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001388 case PP_MACRO_OBJECT_LIKE:
1389 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001390 // If we already have a macro, that means that we've hit the end
1391 // of the definition of the macro we were looking for. We're
1392 // done.
1393 if (Macro)
Douglas Gregor89d99802010-11-30 06:16:57 +00001394 return 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001395
1396 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1397 if (II == 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001398 Error("macro must have a name in AST file");
Douglas Gregor89d99802010-11-30 06:16:57 +00001399 return 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001400 }
Sebastian Redlc3632732010-10-05 15:59:54 +00001401 SourceLocation Loc = ReadSourceLocation(F, Record[1]);
Douglas Gregor37e26842009-04-21 23:56:24 +00001402 bool isUsed = Record[2];
Mike Stump1eb44332009-09-09 15:08:12 +00001403
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001404 MacroInfo *MI = PP->AllocateMacroInfo(Loc);
Douglas Gregor37e26842009-04-21 23:56:24 +00001405 MI->setIsUsed(isUsed);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001406 MI->setIsFromAST();
Mike Stump1eb44332009-09-09 15:08:12 +00001407
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001408 unsigned NextIndex = 3;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001409 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregor37e26842009-04-21 23:56:24 +00001410 // Decode function-like macro info.
1411 bool isC99VarArgs = Record[3];
1412 bool isGNUVarArgs = Record[4];
1413 MacroArgs.clear();
1414 unsigned NumArgs = Record[5];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001415 NextIndex = 6 + NumArgs;
Douglas Gregor37e26842009-04-21 23:56:24 +00001416 for (unsigned i = 0; i != NumArgs; ++i)
1417 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1418
1419 // Install function-like macro info.
1420 MI->setIsFunctionLike();
1421 if (isC99VarArgs) MI->setIsC99Varargs();
1422 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor75fdb232009-05-22 22:45:36 +00001423 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001424 PP->getPreprocessorAllocator());
Douglas Gregor37e26842009-04-21 23:56:24 +00001425 }
1426
1427 // Finally, install the macro.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001428 PP->setMacroInfo(II, MI);
Douglas Gregor37e26842009-04-21 23:56:24 +00001429
1430 // Remember that we saw this macro last so that we add the tokens that
1431 // form its body to it.
1432 Macro = MI;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001433
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001434 if (NextIndex + 1 == Record.size() && PP->getPreprocessingRecord()) {
1435 // We have a macro definition. Load it now.
1436 PP->getPreprocessingRecord()->RegisterMacroDefinition(Macro,
1437 getMacroDefinition(Record[NextIndex]));
1438 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001439
Douglas Gregor37e26842009-04-21 23:56:24 +00001440 ++NumMacrosRead;
1441 break;
1442 }
Mike Stump1eb44332009-09-09 15:08:12 +00001443
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001444 case PP_TOKEN: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001445 // If we see a TOKEN before a PP_MACRO_*, then the file is
1446 // erroneous, just pretend we didn't see this.
1447 if (Macro == 0) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001448
Douglas Gregor37e26842009-04-21 23:56:24 +00001449 Token Tok;
1450 Tok.startToken();
Sebastian Redlc3632732010-10-05 15:59:54 +00001451 Tok.setLocation(ReadSourceLocation(F, Record[0]));
Douglas Gregor37e26842009-04-21 23:56:24 +00001452 Tok.setLength(Record[1]);
1453 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1454 Tok.setIdentifierInfo(II);
1455 Tok.setKind((tok::TokenKind)Record[3]);
1456 Tok.setFlag((Token::TokenFlags)Record[4]);
1457 Macro->AddTokenToBody(Tok);
1458 break;
1459 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001460
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001461 case PP_MACRO_INSTANTIATION: {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001462 // If we already have a macro, that means that we've hit the end
1463 // of the definition of the macro we were looking for. We're
1464 // done.
1465 if (Macro)
Douglas Gregor89d99802010-11-30 06:16:57 +00001466 return 0;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001467
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001468 if (!PP->getPreprocessingRecord()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001469 Error("missing preprocessing record in AST file");
Douglas Gregor89d99802010-11-30 06:16:57 +00001470 return 0;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001471 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001472
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001473 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
Douglas Gregor89d99802010-11-30 06:16:57 +00001474 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1475 return PE;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001476
1477 MacroInstantiation *MI
1478 = new (PPRec) MacroInstantiation(DecodeIdentifierInfo(Record[3]),
Sebastian Redlc3632732010-10-05 15:59:54 +00001479 SourceRange(ReadSourceLocation(F, Record[1]),
1480 ReadSourceLocation(F, Record[2])),
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001481 getMacroDefinition(Record[4]));
1482 PPRec.SetPreallocatedEntity(Record[0], MI);
Douglas Gregor89d99802010-11-30 06:16:57 +00001483 return MI;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001484 }
1485
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001486 case PP_MACRO_DEFINITION: {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001487 // If we already have a macro, that means that we've hit the end
1488 // of the definition of the macro we were looking for. We're
1489 // done.
1490 if (Macro)
Douglas Gregor89d99802010-11-30 06:16:57 +00001491 return 0;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001492
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001493 if (!PP->getPreprocessingRecord()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001494 Error("missing preprocessing record in AST file");
Douglas Gregor89d99802010-11-30 06:16:57 +00001495 return 0;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001496 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001497
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001498 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
Douglas Gregor89d99802010-11-30 06:16:57 +00001499 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1500 return PE;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001501
Douglas Gregor77424bc2010-10-02 19:29:26 +00001502 if (Record[1] > MacroDefinitionsLoaded.size()) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001503 Error("out-of-bounds macro definition record");
Douglas Gregor89d99802010-11-30 06:16:57 +00001504 return 0;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001505 }
1506
Douglas Gregor77424bc2010-10-02 19:29:26 +00001507 // Decode the identifier info and then check again; if the macro is
Michael J. Spencer20249a12010-10-21 03:16:25 +00001508 // still defined and associated with the identifier,
Douglas Gregor77424bc2010-10-02 19:29:26 +00001509 IdentifierInfo *II = DecodeIdentifierInfo(Record[4]);
1510 if (!MacroDefinitionsLoaded[Record[1] - 1]) {
1511 MacroDefinition *MD
1512 = new (PPRec) MacroDefinition(II,
Sebastian Redlc3632732010-10-05 15:59:54 +00001513 ReadSourceLocation(F, Record[5]),
Douglas Gregorb1a7d9a2010-10-01 20:33:34 +00001514 SourceRange(
Sebastian Redlc3632732010-10-05 15:59:54 +00001515 ReadSourceLocation(F, Record[2]),
1516 ReadSourceLocation(F, Record[3])));
Michael J. Spencer20249a12010-10-21 03:16:25 +00001517
Douglas Gregor77424bc2010-10-02 19:29:26 +00001518 PPRec.SetPreallocatedEntity(Record[0], MD);
1519 MacroDefinitionsLoaded[Record[1] - 1] = MD;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001520
Douglas Gregor77424bc2010-10-02 19:29:26 +00001521 if (DeserializationListener)
1522 DeserializationListener->MacroDefinitionRead(Record[1], MD);
1523 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001524
Douglas Gregor89d99802010-11-30 06:16:57 +00001525 return MacroDefinitionsLoaded[Record[1] - 1];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001526 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001527
Douglas Gregorecdcb882010-10-20 22:00:55 +00001528 case PP_INCLUSION_DIRECTIVE: {
1529 // If we already have a macro, that means that we've hit the end
1530 // of the definition of the macro we were looking for. We're
1531 // done.
1532 if (Macro)
Douglas Gregor89d99802010-11-30 06:16:57 +00001533 return 0;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001534
Douglas Gregorecdcb882010-10-20 22:00:55 +00001535 if (!PP->getPreprocessingRecord()) {
1536 Error("missing preprocessing record in AST file");
Douglas Gregor89d99802010-11-30 06:16:57 +00001537 return 0;
Douglas Gregorecdcb882010-10-20 22:00:55 +00001538 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001539
Douglas Gregorecdcb882010-10-20 22:00:55 +00001540 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
Douglas Gregor89d99802010-11-30 06:16:57 +00001541 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1542 return PE;
Douglas Gregorecdcb882010-10-20 22:00:55 +00001543
1544 const char *FullFileNameStart = BlobStart + Record[3];
Michael J. Spencer20249a12010-10-21 03:16:25 +00001545 const FileEntry *File
Douglas Gregor89d99802010-11-30 06:16:57 +00001546 = PP->getFileManager().getFile(llvm::StringRef(FullFileNameStart,
1547 BlobLen - Record[3]));
Michael J. Spencer20249a12010-10-21 03:16:25 +00001548
Douglas Gregorecdcb882010-10-20 22:00:55 +00001549 // FIXME: Stable encoding
1550 InclusionDirective::InclusionKind Kind
1551 = static_cast<InclusionDirective::InclusionKind>(Record[5]);
1552 InclusionDirective *ID
Douglas Gregor4ab829c2010-11-01 15:03:47 +00001553 = new (PPRec) InclusionDirective(PPRec, Kind,
Douglas Gregorecdcb882010-10-20 22:00:55 +00001554 llvm::StringRef(BlobStart, Record[3]),
1555 Record[4],
1556 File,
1557 SourceRange(ReadSourceLocation(F, Record[1]),
1558 ReadSourceLocation(F, Record[2])));
1559 PPRec.SetPreallocatedEntity(Record[0], ID);
Douglas Gregor89d99802010-11-30 06:16:57 +00001560 return ID;
Douglas Gregorecdcb882010-10-20 22:00:55 +00001561 }
Sebastian Redlb57a6242010-09-27 22:18:47 +00001562 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001563 }
Douglas Gregor89d99802010-11-30 06:16:57 +00001564
1565 return 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001566}
1567
Douglas Gregor295a2a62010-10-30 00:23:06 +00001568void ASTReader::SetIdentifierIsMacro(IdentifierInfo *II, PerFileData &F,
1569 uint64_t Offset) {
1570 // Note that this identifier has a macro definition.
1571 II->setHasMacroDefinition(true);
1572
1573 // Adjust the offset based on our position in the chain.
1574 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1575 if (Chain[I] == &F)
1576 break;
1577
1578 Offset += Chain[I]->SizeInBits;
1579 }
1580
1581 UnreadMacroRecordOffsets[II] = Offset;
1582}
1583
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001584void ASTReader::ReadDefinedMacros() {
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001585 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redlc3632732010-10-05 15:59:54 +00001586 PerFileData &F = *Chain[N - I - 1];
1587 llvm::BitstreamCursor &MacroCursor = F.MacroCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001588
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001589 // If there was no preprocessor block, skip this file.
1590 if (!MacroCursor.getBitStreamReader())
1591 continue;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001592
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001593 llvm::BitstreamCursor Cursor = MacroCursor;
Douglas Gregorecdcb882010-10-20 22:00:55 +00001594 Cursor.JumpToBit(F.MacroStartOffset);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001595
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001596 RecordData Record;
1597 while (true) {
Sebastian Redledadecc2010-09-28 02:55:49 +00001598 uint64_t Offset = Cursor.GetCurrentBitNo();
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001599 unsigned Code = Cursor.ReadCode();
Douglas Gregorecdcb882010-10-20 22:00:55 +00001600 if (Code == llvm::bitc::END_BLOCK)
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001601 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001602
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001603 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1604 // No known subblocks, always skip them.
1605 Cursor.ReadSubBlockID();
1606 if (Cursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001607 Error("malformed block record in AST file");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001608 return;
1609 }
1610 continue;
1611 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001612
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001613 if (Code == llvm::bitc::DEFINE_ABBREV) {
1614 Cursor.ReadAbbrevRecord();
1615 continue;
1616 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001617
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001618 // Read a record.
1619 const char *BlobStart;
1620 unsigned BlobLen;
1621 Record.clear();
1622 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1623 default: // Default behavior: ignore.
1624 break;
Douglas Gregor88a35862010-01-04 19:18:44 +00001625
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001626 case PP_MACRO_OBJECT_LIKE:
1627 case PP_MACRO_FUNCTION_LIKE:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001628 DecodeIdentifierInfo(Record[0]);
1629 break;
1630
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001631 case PP_TOKEN:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001632 // Ignore tokens.
1633 break;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001634
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001635 case PP_MACRO_INSTANTIATION:
1636 case PP_MACRO_DEFINITION:
Douglas Gregorecdcb882010-10-20 22:00:55 +00001637 case PP_INCLUSION_DIRECTIVE:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001638 // Read the macro record.
Sebastian Redledadecc2010-09-28 02:55:49 +00001639 // FIXME: That's a stupid way to do this. We should reuse this cursor.
Sebastian Redlc3632732010-10-05 15:59:54 +00001640 ReadMacroRecord(F, Offset);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001641 break;
1642 }
Douglas Gregor88a35862010-01-04 19:18:44 +00001643 }
1644 }
Douglas Gregor295a2a62010-10-30 00:23:06 +00001645
1646 // Drain the unread macro-record offsets map.
1647 while (!UnreadMacroRecordOffsets.empty())
1648 LoadMacroDefinition(UnreadMacroRecordOffsets.begin());
1649}
1650
1651void ASTReader::LoadMacroDefinition(
1652 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos) {
1653 assert(Pos != UnreadMacroRecordOffsets.end() && "Unknown macro definition");
1654 PerFileData *F = 0;
1655 uint64_t Offset = Pos->second;
1656 UnreadMacroRecordOffsets.erase(Pos);
1657
1658 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1659 if (Offset < Chain[I]->SizeInBits) {
1660 F = Chain[I];
1661 break;
1662 }
1663
1664 Offset -= Chain[I]->SizeInBits;
1665 }
1666 if (!F) {
1667 Error("Malformed macro record offset");
1668 return;
1669 }
1670
1671 ReadMacroRecord(*F, Offset);
1672}
1673
1674void ASTReader::LoadMacroDefinition(IdentifierInfo *II) {
1675 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos
1676 = UnreadMacroRecordOffsets.find(II);
1677 LoadMacroDefinition(Pos);
Douglas Gregor88a35862010-01-04 19:18:44 +00001678}
1679
Sebastian Redlf73c93f2010-09-15 19:54:06 +00001680MacroDefinition *ASTReader::getMacroDefinition(MacroID ID) {
Douglas Gregor77424bc2010-10-02 19:29:26 +00001681 if (ID == 0 || ID > MacroDefinitionsLoaded.size())
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001682 return 0;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001683
Douglas Gregor77424bc2010-10-02 19:29:26 +00001684 if (!MacroDefinitionsLoaded[ID - 1]) {
1685 unsigned Index = ID - 1;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001686 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1687 PerFileData &F = *Chain[N - I - 1];
1688 if (Index < F.LocalNumMacroDefinitions) {
Sebastian Redlc3632732010-10-05 15:59:54 +00001689 ReadMacroRecord(F, F.MacroDefinitionOffsets[Index]);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001690 break;
1691 }
1692 Index -= F.LocalNumMacroDefinitions;
1693 }
Douglas Gregor77424bc2010-10-02 19:29:26 +00001694 assert(MacroDefinitionsLoaded[ID - 1] && "Broken chain");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001695 }
1696
Douglas Gregor77424bc2010-10-02 19:29:26 +00001697 return MacroDefinitionsLoaded[ID - 1];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001698}
1699
Douglas Gregore650c8c2009-07-07 00:12:59 +00001700/// \brief If we are loading a relocatable PCH file, and the filename is
1701/// not an absolute path, add the system root to the beginning of the file
1702/// name.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001703void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001704 // If this is not a relocatable PCH file, there's nothing to do.
1705 if (!RelocatablePCH)
1706 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001707
Michael J. Spencer256053b2010-12-17 21:22:22 +00001708 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
Douglas Gregore650c8c2009-07-07 00:12:59 +00001709 return;
1710
Douglas Gregore650c8c2009-07-07 00:12:59 +00001711 if (isysroot == 0) {
1712 // If no system root was given, default to '/'
1713 Filename.insert(Filename.begin(), '/');
1714 return;
1715 }
Mike Stump1eb44332009-09-09 15:08:12 +00001716
Douglas Gregore650c8c2009-07-07 00:12:59 +00001717 unsigned Length = strlen(isysroot);
1718 if (isysroot[Length - 1] != '/')
1719 Filename.insert(Filename.begin(), '/');
Mike Stump1eb44332009-09-09 15:08:12 +00001720
Douglas Gregore650c8c2009-07-07 00:12:59 +00001721 Filename.insert(Filename.begin(), isysroot, isysroot + Length);
1722}
1723
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001724ASTReader::ASTReadResult
Sebastian Redl571db7f2010-08-18 23:56:56 +00001725ASTReader::ReadASTBlock(PerFileData &F) {
Sebastian Redl9137a522010-07-16 17:50:48 +00001726 llvm::BitstreamCursor &Stream = F.Stream;
1727
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001728 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001729 Error("malformed block record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001730 return Failure;
1731 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001732
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001733 // Read all of the records and blocks for the ASt file.
Douglas Gregor8038d512009-04-10 17:25:41 +00001734 RecordData Record;
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001735 bool First = true;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001736 while (!Stream.AtEndOfStream()) {
1737 unsigned Code = Stream.ReadCode();
1738 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001739 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001740 Error("error at end of module block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001741 return Failure;
1742 }
Chris Lattner7356a312009-04-11 21:15:38 +00001743
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001744 return Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001745 }
1746
1747 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1748 switch (Stream.ReadSubBlockID()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001749 case DECLTYPES_BLOCK_ID:
Chris Lattner6367f6d2009-04-27 01:05:14 +00001750 // We lazily load the decls block, but we want to set up the
1751 // DeclsCursor cursor to point into it. Clone our current bitcode
1752 // cursor to it, enter the block and read the abbrevs in that block.
1753 // With the main cursor, we just skip over it.
Sebastian Redl9137a522010-07-16 17:50:48 +00001754 F.DeclsCursor = Stream;
Chris Lattner6367f6d2009-04-27 01:05:14 +00001755 if (Stream.SkipBlock() || // Skip with the main cursor.
1756 // Read the abbrevs.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001757 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001758 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001759 return Failure;
1760 }
1761 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001762
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00001763 case DECL_UPDATES_BLOCK_ID:
1764 if (Stream.SkipBlock()) {
1765 Error("malformed block record in AST file");
1766 return Failure;
1767 }
1768 break;
1769
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001770 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl9137a522010-07-16 17:50:48 +00001771 F.MacroCursor = Stream;
Douglas Gregor88a35862010-01-04 19:18:44 +00001772 if (PP)
1773 PP->setExternalSource(this);
1774
Douglas Gregorecdcb882010-10-20 22:00:55 +00001775 if (Stream.SkipBlock() ||
1776 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001777 Error("malformed block record in AST file");
Chris Lattner7356a312009-04-11 21:15:38 +00001778 return Failure;
1779 }
Douglas Gregorecdcb882010-10-20 22:00:55 +00001780 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
Chris Lattner7356a312009-04-11 21:15:38 +00001781 break;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00001782
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001783 case SOURCE_MANAGER_BLOCK_ID:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001784 switch (ReadSourceManagerBlock(F)) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00001785 case Success:
1786 break;
1787
1788 case Failure:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001789 Error("malformed source manager block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001790 return Failure;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001791
1792 case IgnorePCH:
1793 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001794 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001795 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001796 }
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001797 First = false;
Douglas Gregor8038d512009-04-10 17:25:41 +00001798 continue;
1799 }
1800
1801 if (Code == llvm::bitc::DEFINE_ABBREV) {
1802 Stream.ReadAbbrevRecord();
1803 continue;
1804 }
1805
1806 // Read and process a record.
1807 Record.clear();
Douglas Gregor2bec0412009-04-10 21:16:55 +00001808 const char *BlobStart = 0;
1809 unsigned BlobLen = 0;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001810 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00001811 &BlobStart, &BlobLen)) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001812 default: // Default behavior: ignore.
1813 break;
1814
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001815 case METADATA: {
1816 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1817 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001818 : diag::warn_pch_version_too_new);
1819 return IgnorePCH;
1820 }
1821
1822 RelocatablePCH = Record[4];
1823 if (Listener) {
1824 std::string TargetTriple(BlobStart, BlobLen);
1825 if (Listener->ReadTargetTriple(TargetTriple))
1826 return IgnorePCH;
1827 }
1828 break;
1829 }
1830
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001831 case CHAINED_METADATA: {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001832 if (!First) {
1833 Error("CHAINED_METADATA is not first record in block");
1834 return Failure;
1835 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001836 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1837 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001838 : diag::warn_pch_version_too_new);
1839 return IgnorePCH;
1840 }
1841
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00001842 // Load the chained file, which is always a PCH file.
1843 switch(ReadASTCore(llvm::StringRef(BlobStart, BlobLen), PCH)) {
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001844 case Failure: return Failure;
1845 // If we have to ignore the dependency, we'll have to ignore this too.
1846 case IgnorePCH: return IgnorePCH;
1847 case Success: break;
1848 }
1849 break;
1850 }
1851
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001852 case TYPE_OFFSET:
Sebastian Redl12d6da02010-07-19 22:06:55 +00001853 if (F.LocalNumTypes != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001854 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001855 return Failure;
1856 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00001857 F.TypeOffsets = (const uint32_t *)BlobStart;
1858 F.LocalNumTypes = Record[0];
Douglas Gregor8038d512009-04-10 17:25:41 +00001859 break;
1860
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001861 case DECL_OFFSET:
Sebastian Redl12d6da02010-07-19 22:06:55 +00001862 if (F.LocalNumDecls != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001863 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001864 return Failure;
1865 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00001866 F.DeclOffsets = (const uint32_t *)BlobStart;
1867 F.LocalNumDecls = Record[0];
Douglas Gregor8038d512009-04-10 17:25:41 +00001868 break;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001869
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001870 case TU_UPDATE_LEXICAL: {
Sebastian Redld692af72010-07-27 18:24:41 +00001871 DeclContextInfo Info = {
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001872 /* No visible information */ 0,
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001873 reinterpret_cast<const KindDeclIDPair *>(BlobStart),
1874 BlobLen / sizeof(KindDeclIDPair)
Sebastian Redld692af72010-07-27 18:24:41 +00001875 };
Douglas Gregor3747ee72010-10-01 01:18:02 +00001876 DeclContextOffsets[Context ? Context->getTranslationUnitDecl() : 0]
1877 .push_back(Info);
Sebastian Redld692af72010-07-27 18:24:41 +00001878 break;
1879 }
1880
Sebastian Redle1dde812010-08-24 00:50:04 +00001881 case UPDATE_VISIBLE: {
1882 serialization::DeclID ID = Record[0];
1883 void *Table = ASTDeclContextNameLookupTable::Create(
1884 (const unsigned char *)BlobStart + Record[1],
1885 (const unsigned char *)BlobStart,
1886 ASTDeclContextNameLookupTrait(*this));
Douglas Gregor3747ee72010-10-01 01:18:02 +00001887 if (ID == 1 && Context) { // Is it the TU?
Sebastian Redle1dde812010-08-24 00:50:04 +00001888 DeclContextInfo Info = {
1889 Table, /* No lexical inforamtion */ 0, 0
1890 };
1891 DeclContextOffsets[Context->getTranslationUnitDecl()].push_back(Info);
1892 } else
1893 PendingVisibleUpdates[ID].push_back(Table);
1894 break;
1895 }
1896
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001897 case REDECLS_UPDATE_LATEST: {
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00001898 assert(Record.size() % 2 == 0 && "Expected pairs of DeclIDs");
1899 for (unsigned i = 0, e = Record.size(); i < e; i += 2) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001900 DeclID First = Record[i], Latest = Record[i+1];
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00001901 assert((FirstLatestDeclIDs.find(First) == FirstLatestDeclIDs.end() ||
1902 Latest > FirstLatestDeclIDs[First]) &&
1903 "The new latest is supposed to come after the previous latest");
1904 FirstLatestDeclIDs[First] = Latest;
1905 }
1906 break;
1907 }
1908
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001909 case LANGUAGE_OPTIONS:
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001910 if (ParseLanguageOptions(Record) && !DisableValidation)
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001911 return IgnorePCH;
1912 break;
Douglas Gregor2bec0412009-04-10 21:16:55 +00001913
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001914 case IDENTIFIER_TABLE:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001915 F.IdentifierTableData = BlobStart;
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001916 if (Record[0]) {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001917 F.IdentifierLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001918 = ASTIdentifierLookupTable::Create(
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001919 (const unsigned char *)F.IdentifierTableData + Record[0],
1920 (const unsigned char *)F.IdentifierTableData,
Sebastian Redlc3632732010-10-05 15:59:54 +00001921 ASTIdentifierLookupTrait(*this, F));
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001922 if (PP)
1923 PP->getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001924 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001925 break;
1926
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001927 case IDENTIFIER_OFFSET:
Sebastian Redl2da08f92010-07-19 22:28:42 +00001928 if (F.LocalNumIdentifiers != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001929 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00001930 return Failure;
1931 }
Sebastian Redl2da08f92010-07-19 22:28:42 +00001932 F.IdentifierOffsets = (const uint32_t *)BlobStart;
1933 F.LocalNumIdentifiers = Record[0];
Douglas Gregorafaf3082009-04-11 00:14:32 +00001934 break;
Douglas Gregorfdd01722009-04-14 00:24:19 +00001935
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001936 case EXTERNAL_DEFINITIONS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001937 // Optimization for the first block.
1938 if (ExternalDefinitions.empty())
1939 ExternalDefinitions.swap(Record);
1940 else
1941 ExternalDefinitions.insert(ExternalDefinitions.end(),
1942 Record.begin(), Record.end());
Douglas Gregorfdd01722009-04-14 00:24:19 +00001943 break;
Douglas Gregor3e1af842009-04-17 22:13:46 +00001944
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001945 case SPECIAL_TYPES:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001946 // Optimization for the first block
1947 if (SpecialTypes.empty())
1948 SpecialTypes.swap(Record);
1949 else
1950 SpecialTypes.insert(SpecialTypes.end(), Record.begin(), Record.end());
Douglas Gregorad1de002009-04-18 05:55:16 +00001951 break;
1952
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001953 case STATISTICS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001954 TotalNumStatements += Record[0];
1955 TotalNumMacros += Record[1];
1956 TotalLexicalDeclContexts += Record[2];
1957 TotalVisibleDeclContexts += Record[3];
Douglas Gregor3e1af842009-04-17 22:13:46 +00001958 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001959
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001960 case TENTATIVE_DEFINITIONS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001961 // Optimization for the first block.
1962 if (TentativeDefinitions.empty())
1963 TentativeDefinitions.swap(Record);
1964 else
1965 TentativeDefinitions.insert(TentativeDefinitions.end(),
1966 Record.begin(), Record.end());
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00001967 break;
Douglas Gregor14c22f22009-04-22 22:18:58 +00001968
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001969 case UNUSED_FILESCOPED_DECLS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001970 // Optimization for the first block.
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00001971 if (UnusedFileScopedDecls.empty())
1972 UnusedFileScopedDecls.swap(Record);
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001973 else
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00001974 UnusedFileScopedDecls.insert(UnusedFileScopedDecls.end(),
1975 Record.begin(), Record.end());
Tanya Lattnere6bbc012010-02-12 00:07:30 +00001976 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001977
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001978 case WEAK_UNDECLARED_IDENTIFIERS:
Sebastian Redl40566802010-08-05 18:21:25 +00001979 // Later blocks overwrite earlier ones.
1980 WeakUndeclaredIdentifiers.swap(Record);
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00001981 break;
1982
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001983 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001984 // Optimization for the first block.
1985 if (LocallyScopedExternalDecls.empty())
1986 LocallyScopedExternalDecls.swap(Record);
1987 else
1988 LocallyScopedExternalDecls.insert(LocallyScopedExternalDecls.end(),
1989 Record.begin(), Record.end());
Douglas Gregor14c22f22009-04-22 22:18:58 +00001990 break;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001991
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001992 case SELECTOR_OFFSETS:
Sebastian Redl059612d2010-08-03 21:58:15 +00001993 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redl725cd962010-08-04 20:40:17 +00001994 F.LocalNumSelectors = Record[0];
Douglas Gregor83941df2009-04-25 17:48:32 +00001995 break;
1996
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001997 case METHOD_POOL:
Sebastian Redl725cd962010-08-04 20:40:17 +00001998 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor83941df2009-04-25 17:48:32 +00001999 if (Record[0])
Sebastian Redl725cd962010-08-04 20:40:17 +00002000 F.SelectorLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002001 = ASTSelectorLookupTable::Create(
Sebastian Redl725cd962010-08-04 20:40:17 +00002002 F.SelectorLookupTableData + Record[0],
2003 F.SelectorLookupTableData,
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002004 ASTSelectorLookupTrait(*this));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002005 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002006 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002007
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002008 case REFERENCED_SELECTOR_POOL:
Sebastian Redlc3632732010-10-05 15:59:54 +00002009 F.ReferencedSelectorsData.swap(Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002010 break;
2011
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002012 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002013 if (!Record.empty() && Listener)
2014 Listener->ReadCounter(Record[0]);
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002015 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002016
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002017 case SOURCE_LOCATION_OFFSETS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002018 F.SLocOffsets = (const uint32_t *)BlobStart;
2019 F.LocalNumSLocEntries = Record[0];
Sebastian Redl8db9fae2010-09-22 20:19:08 +00002020 F.LocalSLocSize = Record[1];
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002021 break;
2022
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002023 case SOURCE_LOCATION_PRELOADS:
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002024 if (PreloadSLocEntries.empty())
2025 PreloadSLocEntries.swap(Record);
2026 else
2027 PreloadSLocEntries.insert(PreloadSLocEntries.end(),
2028 Record.begin(), Record.end());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002029 break;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002030
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002031 case STAT_CACHE: {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002032 ASTStatCache *MyStatCache =
2033 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
Douglas Gregor52e71082009-10-16 18:18:30 +00002034 (const unsigned char *)BlobStart,
2035 NumStatHits, NumStatMisses);
2036 FileMgr.addStatCache(MyStatCache);
Sebastian Redl9137a522010-07-16 17:50:48 +00002037 F.StatCache = MyStatCache;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002038 break;
Douglas Gregor52e71082009-10-16 18:18:30 +00002039 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002040
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002041 case EXT_VECTOR_DECLS:
Sebastian Redla9f23682010-07-28 21:38:49 +00002042 // Optimization for the first block.
2043 if (ExtVectorDecls.empty())
2044 ExtVectorDecls.swap(Record);
2045 else
2046 ExtVectorDecls.insert(ExtVectorDecls.end(),
2047 Record.begin(), Record.end());
Douglas Gregorb81c1702009-04-27 20:06:05 +00002048 break;
2049
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002050 case VTABLE_USES:
Sebastian Redl40566802010-08-05 18:21:25 +00002051 // Later tables overwrite earlier ones.
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002052 VTableUses.swap(Record);
2053 break;
2054
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002055 case DYNAMIC_CLASSES:
Sebastian Redl40566802010-08-05 18:21:25 +00002056 // Optimization for the first block.
2057 if (DynamicClasses.empty())
2058 DynamicClasses.swap(Record);
2059 else
2060 DynamicClasses.insert(DynamicClasses.end(),
2061 Record.begin(), Record.end());
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002062 break;
2063
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002064 case PENDING_IMPLICIT_INSTANTIATIONS:
Sebastian Redlc3632732010-10-05 15:59:54 +00002065 F.PendingInstantiations.swap(Record);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002066 break;
2067
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002068 case SEMA_DECL_REFS:
Sebastian Redl40566802010-08-05 18:21:25 +00002069 // Later tables overwrite earlier ones.
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002070 SemaDeclRefs.swap(Record);
2071 break;
2072
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002073 case ORIGINAL_FILE_NAME:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002074 // The primary AST will be the last to get here, so it will be the one
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002075 // that's used.
Daniel Dunbar7b5a1212009-11-11 05:29:04 +00002076 ActualOriginalFileName.assign(BlobStart, BlobLen);
2077 OriginalFileName = ActualOriginalFileName;
Douglas Gregore650c8c2009-07-07 00:12:59 +00002078 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregorb64c1932009-05-12 01:31:05 +00002079 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002080
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002081 case VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek974be4d2010-02-12 23:31:14 +00002082 const std::string &CurBranch = getClangFullRepositoryVersion();
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002083 llvm::StringRef ASTBranch(BlobStart, BlobLen);
2084 if (llvm::StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2085 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregor445e23e2009-10-05 21:07:28 +00002086 return IgnorePCH;
2087 }
2088 break;
2089 }
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002090
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002091 case MACRO_DEFINITION_OFFSETS:
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002092 F.MacroDefinitionOffsets = (const uint32_t *)BlobStart;
2093 F.NumPreallocatedPreprocessingEntities = Record[0];
2094 F.LocalNumMacroDefinitions = Record[1];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002095 break;
Sebastian Redl0b17c612010-08-13 00:28:03 +00002096
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002097 case DECL_UPDATE_OFFSETS: {
2098 if (Record.size() % 2 != 0) {
2099 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2100 return Failure;
2101 }
2102 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
2103 DeclUpdateOffsets[static_cast<DeclID>(Record[I])]
2104 .push_back(std::make_pair(&F, Record[I+1]));
2105 break;
2106 }
2107
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002108 case DECL_REPLACEMENTS: {
Sebastian Redl0b17c612010-08-13 00:28:03 +00002109 if (Record.size() % 2 != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002110 Error("invalid DECL_REPLACEMENTS block in AST file");
Sebastian Redl0b17c612010-08-13 00:28:03 +00002111 return Failure;
2112 }
2113 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002114 ReplacedDecls[static_cast<DeclID>(Record[I])] =
Sebastian Redl0b17c612010-08-13 00:28:03 +00002115 std::make_pair(&F, Record[I+1]);
2116 break;
2117 }
Douglas Gregor7c789c12010-10-29 22:39:52 +00002118
2119 case CXX_BASE_SPECIFIER_OFFSETS: {
2120 if (F.LocalNumCXXBaseSpecifiers != 0) {
2121 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2122 return Failure;
2123 }
2124
2125 F.LocalNumCXXBaseSpecifiers = Record[0];
2126 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart;
2127 break;
2128 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002129
2130 case DIAG_USER_MAPPINGS:
2131 if (Record.size() % 2 != 0) {
2132 Error("invalid DIAG_USER_MAPPINGS block in AST file");
2133 return Failure;
2134 }
2135 if (UserDiagMappings.empty())
2136 UserDiagMappings.swap(Record);
2137 else
2138 UserDiagMappings.insert(UserDiagMappings.end(),
2139 Record.begin(), Record.end());
2140 break;
Douglas Gregorafaf3082009-04-11 00:14:32 +00002141 }
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002142 First = false;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002143 }
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002144 Error("premature end of bitstream in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002145 return Failure;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002146}
2147
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002148ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
2149 ASTFileType Type) {
2150 switch(ReadASTCore(FileName, Type)) {
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002151 case Failure: return Failure;
2152 case IgnorePCH: return IgnorePCH;
2153 case Success: break;
2154 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002155
2156 // Here comes stuff that we only do once the entire chain is loaded.
2157
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002158 // Allocate space for loaded slocentries, identifiers, decls and types.
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002159 unsigned TotalNumIdentifiers = 0, TotalNumTypes = 0, TotalNumDecls = 0,
Sebastian Redl725cd962010-08-04 20:40:17 +00002160 TotalNumPreallocatedPreprocessingEntities = 0, TotalNumMacroDefs = 0,
2161 TotalNumSelectors = 0;
Sebastian Redl12d6da02010-07-19 22:06:55 +00002162 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002163 TotalNumSLocEntries += Chain[I]->LocalNumSLocEntries;
Sebastian Redl8db9fae2010-09-22 20:19:08 +00002164 NextSLocOffset += Chain[I]->LocalSLocSize;
Sebastian Redl2da08f92010-07-19 22:28:42 +00002165 TotalNumIdentifiers += Chain[I]->LocalNumIdentifiers;
Sebastian Redl12d6da02010-07-19 22:06:55 +00002166 TotalNumTypes += Chain[I]->LocalNumTypes;
2167 TotalNumDecls += Chain[I]->LocalNumDecls;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002168 TotalNumPreallocatedPreprocessingEntities +=
2169 Chain[I]->NumPreallocatedPreprocessingEntities;
2170 TotalNumMacroDefs += Chain[I]->LocalNumMacroDefinitions;
Sebastian Redl725cd962010-08-04 20:40:17 +00002171 TotalNumSelectors += Chain[I]->LocalNumSelectors;
Sebastian Redl12d6da02010-07-19 22:06:55 +00002172 }
Sebastian Redl8db9fae2010-09-22 20:19:08 +00002173 SourceMgr.PreallocateSLocEntries(this, TotalNumSLocEntries, NextSLocOffset);
Sebastian Redl2da08f92010-07-19 22:28:42 +00002174 IdentifiersLoaded.resize(TotalNumIdentifiers);
Sebastian Redl12d6da02010-07-19 22:06:55 +00002175 TypesLoaded.resize(TotalNumTypes);
2176 DeclsLoaded.resize(TotalNumDecls);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002177 MacroDefinitionsLoaded.resize(TotalNumMacroDefs);
2178 if (PP) {
2179 if (TotalNumIdentifiers > 0)
2180 PP->getHeaderSearchInfo().SetExternalLookup(this);
2181 if (TotalNumPreallocatedPreprocessingEntities > 0) {
2182 if (!PP->getPreprocessingRecord())
2183 PP->createPreprocessingRecord();
2184 PP->getPreprocessingRecord()->SetExternalSource(*this,
2185 TotalNumPreallocatedPreprocessingEntities);
2186 }
2187 }
Sebastian Redl725cd962010-08-04 20:40:17 +00002188 SelectorsLoaded.resize(TotalNumSelectors);
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002189 // Preload SLocEntries.
2190 for (unsigned I = 0, N = PreloadSLocEntries.size(); I != N; ++I) {
2191 ASTReadResult Result = ReadSLocEntryRecord(PreloadSLocEntries[I]);
2192 if (Result != Success)
2193 return Result;
2194 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00002195
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002196 // Check the predefines buffers.
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00002197 if (!DisableValidation && CheckPredefinesBuffers())
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002198 return IgnorePCH;
2199
2200 if (PP) {
2201 // Initialization of keywords and pragmas occurs before the
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002202 // AST file is read, so there may be some identifiers that were
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002203 // loaded into the IdentifierTable before we intercepted the
2204 // creation of identifiers. Iterate through the list of known
2205 // identifiers and determine whether we have to establish
2206 // preprocessor definitions or top-level identifier declaration
2207 // chains for those identifiers.
2208 //
2209 // We copy the IdentifierInfo pointers to a small vector first,
2210 // since de-serializing declarations or macro definitions can add
2211 // new entries into the identifier table, invalidating the
2212 // iterators.
2213 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
2214 for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(),
2215 IdEnd = PP->getIdentifierTable().end();
2216 Id != IdEnd; ++Id)
2217 Identifiers.push_back(Id->second);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002218 // We need to search the tables in all files.
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002219 for (unsigned J = 0, M = Chain.size(); J != M; ++J) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002220 ASTIdentifierLookupTable *IdTable
2221 = (ASTIdentifierLookupTable *)Chain[J]->IdentifierLookupTable;
2222 // Not all AST files necessarily have identifier tables, only the useful
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00002223 // ones.
2224 if (!IdTable)
2225 continue;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002226 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
2227 IdentifierInfo *II = Identifiers[I];
2228 // Look in the on-disk hash tables for an entry for this identifier
Sebastian Redlc3632732010-10-05 15:59:54 +00002229 ASTIdentifierLookupTrait Info(*this, *Chain[J], II);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002230 std::pair<const char*,unsigned> Key(II->getNameStart(),II->getLength());
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002231 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002232 if (Pos == IdTable->end())
2233 continue;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002234
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002235 // Dereferencing the iterator has the effect of populating the
2236 // IdentifierInfo node with the various declarations it needs.
2237 (void)*Pos;
2238 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002239 }
2240 }
2241
2242 if (Context)
2243 InitializeContext(*Context);
2244
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002245 if (DeserializationListener)
2246 DeserializationListener->ReaderInitialized(this);
2247
Douglas Gregor414cb642010-11-30 05:23:00 +00002248 // If this AST file is a precompiled preamble, then set the main file ID of
2249 // the source manager to the file source file from which the preamble was
2250 // built. This is the only valid way to use a precompiled preamble.
2251 if (Type == Preamble) {
2252 SourceLocation Loc
2253 = SourceMgr.getLocation(FileMgr.getFile(getOriginalSourceFile()), 1, 1);
2254 if (Loc.isValid()) {
2255 std::pair<FileID, unsigned> Decomposed = SourceMgr.getDecomposedLoc(Loc);
2256 SourceMgr.SetPreambleFileID(Decomposed.first);
2257 }
2258 }
2259
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002260 return Success;
2261}
2262
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002263ASTReader::ASTReadResult ASTReader::ReadASTCore(llvm::StringRef FileName,
2264 ASTFileType Type) {
Sebastian Redla866e652010-10-01 19:59:12 +00002265 PerFileData *Prev = Chain.empty() ? 0 : Chain.back();
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002266 Chain.push_back(new PerFileData(Type));
Sebastian Redl9137a522010-07-16 17:50:48 +00002267 PerFileData &F = *Chain.back();
Sebastian Redla866e652010-10-01 19:59:12 +00002268 if (Prev)
2269 Prev->NextInSource = &F;
2270 else
2271 FirstInSource = &F;
2272 F.Loaders.push_back(Prev);
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002273
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002274 // Set the AST file name.
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002275 F.FileName = FileName;
2276
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002277 // Open the AST file.
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002278 //
2279 // FIXME: This shouldn't be here, we should just take a raw_ostream.
2280 std::string ErrStr;
Michael J. Spencer3a321e22010-12-09 17:36:38 +00002281 llvm::error_code ec;
2282 if (FileName == "-") {
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00002283 ec = llvm::MemoryBuffer::getSTDIN(F.Buffer);
Michael J. Spencer3a321e22010-12-09 17:36:38 +00002284 if (ec)
2285 ErrStr = ec.message();
2286 } else
Chris Lattner39b49bc2010-11-23 08:35:12 +00002287 F.Buffer.reset(FileMgr.getBufferForFile(FileName, &ErrStr));
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002288 if (!F.Buffer) {
2289 Error(ErrStr.c_str());
2290 return IgnorePCH;
2291 }
2292
2293 // Initialize the stream
2294 F.StreamFile.init((const unsigned char *)F.Buffer->getBufferStart(),
2295 (const unsigned char *)F.Buffer->getBufferEnd());
Sebastian Redl9137a522010-07-16 17:50:48 +00002296 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002297 Stream.init(F.StreamFile);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002298 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002299
2300 // Sniff for the signature.
2301 if (Stream.Read(8) != 'C' ||
2302 Stream.Read(8) != 'P' ||
2303 Stream.Read(8) != 'C' ||
2304 Stream.Read(8) != 'H') {
2305 Diag(diag::err_not_a_pch_file) << FileName;
2306 return Failure;
2307 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002308
Douglas Gregor2cf26342009-04-09 22:27:44 +00002309 while (!Stream.AtEndOfStream()) {
2310 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00002311
Douglas Gregore1d918e2009-04-10 23:10:45 +00002312 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002313 Error("invalid record at top-level of AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002314 return Failure;
2315 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002316
2317 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregor668c1a42009-04-21 22:25:48 +00002318
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002319 // We only know the AST subblock ID.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002320 switch (BlockID) {
2321 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002322 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002323 Error("malformed BlockInfoBlock in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002324 return Failure;
2325 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002326 break;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002327 case AST_BLOCK_ID:
Sebastian Redl571db7f2010-08-18 23:56:56 +00002328 switch (ReadASTBlock(F)) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002329 case Success:
2330 break;
2331
2332 case Failure:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002333 return Failure;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002334
2335 case IgnorePCH:
Douglas Gregor2bec0412009-04-10 21:16:55 +00002336 // FIXME: We could consider reading through to the end of this
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002337 // AST block, skipping subblocks, to see if there are other
2338 // AST blocks elsewhere.
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002339
2340 // Clear out any preallocated source location entries, so that
2341 // the source manager does not try to resolve them later.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002342 SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002343
2344 // Remove the stat cache.
Sebastian Redl9137a522010-07-16 17:50:48 +00002345 if (F.StatCache)
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002346 FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002347
Douglas Gregore1d918e2009-04-10 23:10:45 +00002348 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002349 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002350 break;
2351 default:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002352 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002353 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002354 return Failure;
2355 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002356 break;
2357 }
Mike Stump1eb44332009-09-09 15:08:12 +00002358 }
2359
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002360 return Success;
2361}
2362
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002363void ASTReader::setPreprocessor(Preprocessor &pp) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002364 PP = &pp;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002365
2366 unsigned TotalNum = 0;
2367 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
2368 TotalNum += Chain[I]->NumPreallocatedPreprocessingEntities;
2369 if (TotalNum) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002370 if (!PP->getPreprocessingRecord())
2371 PP->createPreprocessingRecord();
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002372 PP->getPreprocessingRecord()->SetExternalSource(*this, TotalNum);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002373 }
2374}
2375
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002376void ASTReader::InitializeContext(ASTContext &Ctx) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002377 Context = &Ctx;
2378 assert(Context && "Passed null context!");
2379
2380 assert(PP && "Forgot to set Preprocessor ?");
2381 PP->getIdentifierTable().setExternalIdentifierLookup(this);
2382 PP->getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregor88a35862010-01-04 19:18:44 +00002383 PP->setExternalSource(this);
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002384
Douglas Gregor3747ee72010-10-01 01:18:02 +00002385 // If we have an update block for the TU waiting, we have to add it before
2386 // deserializing the decl.
2387 DeclContextOffsetsMap::iterator DCU = DeclContextOffsets.find(0);
2388 if (DCU != DeclContextOffsets.end()) {
2389 // Insertion could invalidate map, so grab vector.
2390 DeclContextInfos T;
2391 T.swap(DCU->second);
2392 DeclContextOffsets.erase(DCU);
2393 DeclContextOffsets[Ctx.getTranslationUnitDecl()].swap(T);
2394 }
2395
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002396 // Load the translation unit declaration
Argyrios Kyrtzidis8871a442010-07-08 17:13:02 +00002397 GetTranslationUnitDecl();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002398
2399 // Load the special types.
2400 Context->setBuiltinVaListType(
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002401 GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST]));
2402 if (unsigned Id = SpecialTypes[SPECIAL_TYPE_OBJC_ID])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002403 Context->setObjCIdType(GetType(Id));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002404 if (unsigned Sel = SpecialTypes[SPECIAL_TYPE_OBJC_SELECTOR])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002405 Context->setObjCSelType(GetType(Sel));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002406 if (unsigned Proto = SpecialTypes[SPECIAL_TYPE_OBJC_PROTOCOL])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002407 Context->setObjCProtoType(GetType(Proto));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002408 if (unsigned Class = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002409 Context->setObjCClassType(GetType(Class));
Steve Naroff14108da2009-07-10 23:34:53 +00002410
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002411 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002412 Context->setCFConstantStringType(GetType(String));
Mike Stump1eb44332009-09-09 15:08:12 +00002413 if (unsigned FastEnum
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002414 = SpecialTypes[SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002415 Context->setObjCFastEnumerationStateType(GetType(FastEnum));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002416 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002417 QualType FileType = GetType(File);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002418 if (FileType.isNull()) {
2419 Error("FILE type is NULL");
2420 return;
2421 }
John McCall183700f2009-09-21 23:43:11 +00002422 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002423 Context->setFILEDecl(Typedef->getDecl());
2424 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002425 const TagType *Tag = FileType->getAs<TagType>();
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002426 if (!Tag) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002427 Error("Invalid FILE type in AST file");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002428 return;
2429 }
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002430 Context->setFILEDecl(Tag->getDecl());
2431 }
2432 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002433 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_jmp_buf]) {
Mike Stump782fa302009-07-28 02:25:19 +00002434 QualType Jmp_bufType = GetType(Jmp_buf);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002435 if (Jmp_bufType.isNull()) {
2436 Error("jmp_bug type is NULL");
2437 return;
2438 }
John McCall183700f2009-09-21 23:43:11 +00002439 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
Mike Stump782fa302009-07-28 02:25:19 +00002440 Context->setjmp_bufDecl(Typedef->getDecl());
2441 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002442 const TagType *Tag = Jmp_bufType->getAs<TagType>();
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002443 if (!Tag) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002444 Error("Invalid jmp_buf type in AST file");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002445 return;
2446 }
Mike Stump782fa302009-07-28 02:25:19 +00002447 Context->setjmp_bufDecl(Tag->getDecl());
2448 }
2449 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002450 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_sigjmp_buf]) {
Mike Stump782fa302009-07-28 02:25:19 +00002451 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002452 if (Sigjmp_bufType.isNull()) {
2453 Error("sigjmp_buf type is NULL");
2454 return;
2455 }
John McCall183700f2009-09-21 23:43:11 +00002456 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
Mike Stump782fa302009-07-28 02:25:19 +00002457 Context->setsigjmp_bufDecl(Typedef->getDecl());
2458 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002459 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002460 assert(Tag && "Invalid sigjmp_buf type in AST file");
Mike Stump782fa302009-07-28 02:25:19 +00002461 Context->setsigjmp_bufDecl(Tag->getDecl());
2462 }
2463 }
Mike Stump1eb44332009-09-09 15:08:12 +00002464 if (unsigned ObjCIdRedef
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002465 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION])
Douglas Gregord1571ac2009-08-21 00:27:50 +00002466 Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef);
Mike Stump1eb44332009-09-09 15:08:12 +00002467 if (unsigned ObjCClassRedef
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002468 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION])
Douglas Gregord1571ac2009-08-21 00:27:50 +00002469 Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002470 if (unsigned String = SpecialTypes[SPECIAL_TYPE_BLOCK_DESCRIPTOR])
Mike Stumpadaaad32009-10-20 02:12:22 +00002471 Context->setBlockDescriptorType(GetType(String));
Mike Stump083c25e2009-10-22 00:49:09 +00002472 if (unsigned String
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002473 = SpecialTypes[SPECIAL_TYPE_BLOCK_EXTENDED_DESCRIPTOR])
Mike Stump083c25e2009-10-22 00:49:09 +00002474 Context->setBlockDescriptorExtendedType(GetType(String));
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002475 if (unsigned ObjCSelRedef
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002476 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION])
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002477 Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002478 if (unsigned String = SpecialTypes[SPECIAL_TYPE_NS_CONSTANT_STRING])
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002479 Context->setNSConstantStringType(GetType(String));
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +00002480
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002481 if (SpecialTypes[SPECIAL_TYPE_INT128_INSTALLED])
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +00002482 Context->setInt128Installed();
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002483
2484 ReadUserDiagnosticMappings(Context->getDiagnostics());
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002485}
2486
Douglas Gregorb64c1932009-05-12 01:31:05 +00002487/// \brief Retrieve the name of the original source file name
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002488/// directly from the AST file, without actually loading the AST
Douglas Gregorb64c1932009-05-12 01:31:05 +00002489/// file.
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002490std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +00002491 FileManager &FileMgr,
Daniel Dunbar93ebb1b2009-12-03 09:13:06 +00002492 Diagnostic &Diags) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002493 // Open the AST file.
Douglas Gregorb64c1932009-05-12 01:31:05 +00002494 std::string ErrStr;
2495 llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
Chris Lattner39b49bc2010-11-23 08:35:12 +00002496 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
Douglas Gregorb64c1932009-05-12 01:31:05 +00002497 if (!Buffer) {
Daniel Dunbar93ebb1b2009-12-03 09:13:06 +00002498 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002499 return std::string();
2500 }
2501
2502 // Initialize the stream
2503 llvm::BitstreamReader StreamFile;
2504 llvm::BitstreamCursor Stream;
Mike Stump1eb44332009-09-09 15:08:12 +00002505 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregorb64c1932009-05-12 01:31:05 +00002506 (const unsigned char *)Buffer->getBufferEnd());
2507 Stream.init(StreamFile);
2508
2509 // Sniff for the signature.
2510 if (Stream.Read(8) != 'C' ||
2511 Stream.Read(8) != 'P' ||
2512 Stream.Read(8) != 'C' ||
2513 Stream.Read(8) != 'H') {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002514 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002515 return std::string();
2516 }
2517
2518 RecordData Record;
2519 while (!Stream.AtEndOfStream()) {
2520 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00002521
Douglas Gregorb64c1932009-05-12 01:31:05 +00002522 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2523 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump1eb44332009-09-09 15:08:12 +00002524
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002525 // We only know the AST subblock ID.
Douglas Gregorb64c1932009-05-12 01:31:05 +00002526 switch (BlockID) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002527 case AST_BLOCK_ID:
2528 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002529 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002530 return std::string();
2531 }
2532 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002533
Douglas Gregorb64c1932009-05-12 01:31:05 +00002534 default:
2535 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002536 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002537 return std::string();
2538 }
2539 break;
2540 }
2541 continue;
2542 }
2543
2544 if (Code == llvm::bitc::END_BLOCK) {
2545 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002546 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002547 return std::string();
2548 }
2549 continue;
2550 }
2551
2552 if (Code == llvm::bitc::DEFINE_ABBREV) {
2553 Stream.ReadAbbrevRecord();
2554 continue;
2555 }
2556
2557 Record.clear();
2558 const char *BlobStart = 0;
2559 unsigned BlobLen = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002560 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002561 == ORIGINAL_FILE_NAME)
Douglas Gregorb64c1932009-05-12 01:31:05 +00002562 return std::string(BlobStart, BlobLen);
Mike Stump1eb44332009-09-09 15:08:12 +00002563 }
Douglas Gregorb64c1932009-05-12 01:31:05 +00002564
2565 return std::string();
2566}
2567
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002568/// \brief Parse the record that corresponds to a LangOptions data
2569/// structure.
2570///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002571/// This routine parses the language options from the AST file and then gives
2572/// them to the AST listener if one is set.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002573///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002574/// \returns true if the listener deems the file unacceptable, false otherwise.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002575bool ASTReader::ParseLanguageOptions(
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002576 const llvm::SmallVectorImpl<uint64_t> &Record) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002577 if (Listener) {
2578 LangOptions LangOpts;
Mike Stump1eb44332009-09-09 15:08:12 +00002579
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002580 #define PARSE_LANGOPT(Option) \
2581 LangOpts.Option = Record[Idx]; \
2582 ++Idx
Mike Stump1eb44332009-09-09 15:08:12 +00002583
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002584 unsigned Idx = 0;
2585 PARSE_LANGOPT(Trigraphs);
2586 PARSE_LANGOPT(BCPLComment);
2587 PARSE_LANGOPT(DollarIdents);
2588 PARSE_LANGOPT(AsmPreprocessor);
2589 PARSE_LANGOPT(GNUMode);
Chandler Carrutheb5d7b72010-04-17 20:17:31 +00002590 PARSE_LANGOPT(GNUKeywords);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002591 PARSE_LANGOPT(ImplicitInt);
2592 PARSE_LANGOPT(Digraphs);
2593 PARSE_LANGOPT(HexFloats);
2594 PARSE_LANGOPT(C99);
2595 PARSE_LANGOPT(Microsoft);
2596 PARSE_LANGOPT(CPlusPlus);
2597 PARSE_LANGOPT(CPlusPlus0x);
2598 PARSE_LANGOPT(CXXOperatorNames);
2599 PARSE_LANGOPT(ObjC1);
2600 PARSE_LANGOPT(ObjC2);
2601 PARSE_LANGOPT(ObjCNonFragileABI);
Fariborz Jahanian412e7982010-02-09 19:31:38 +00002602 PARSE_LANGOPT(ObjCNonFragileABI2);
Ted Kremenekc32647d2010-12-23 21:35:43 +00002603 PARSE_LANGOPT(ObjCDefaultSynthProperties);
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +00002604 PARSE_LANGOPT(NoConstantCFStrings);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002605 PARSE_LANGOPT(PascalStrings);
2606 PARSE_LANGOPT(WritableStrings);
2607 PARSE_LANGOPT(LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +00002608 PARSE_LANGOPT(AltiVec);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002609 PARSE_LANGOPT(Exceptions);
Daniel Dunbar73482882010-02-10 18:48:44 +00002610 PARSE_LANGOPT(SjLjExceptions);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002611 PARSE_LANGOPT(NeXTRuntime);
2612 PARSE_LANGOPT(Freestanding);
2613 PARSE_LANGOPT(NoBuiltin);
2614 PARSE_LANGOPT(ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +00002615 PARSE_LANGOPT(POSIXThreads);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002616 PARSE_LANGOPT(Blocks);
2617 PARSE_LANGOPT(EmitAllDecls);
2618 PARSE_LANGOPT(MathErrno);
Chris Lattnera4d71452010-06-26 21:25:03 +00002619 LangOpts.setSignedOverflowBehavior((LangOptions::SignedOverflowBehaviorTy)
2620 Record[Idx++]);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002621 PARSE_LANGOPT(HeinousExtensions);
2622 PARSE_LANGOPT(Optimize);
2623 PARSE_LANGOPT(OptimizeSize);
2624 PARSE_LANGOPT(Static);
2625 PARSE_LANGOPT(PICLevel);
2626 PARSE_LANGOPT(GNUInline);
2627 PARSE_LANGOPT(NoInline);
2628 PARSE_LANGOPT(AccessControl);
2629 PARSE_LANGOPT(CharIsSigned);
John Thompsona6fda122009-11-05 20:14:16 +00002630 PARSE_LANGOPT(ShortWChar);
Chris Lattnera4d71452010-06-26 21:25:03 +00002631 LangOpts.setGCMode((LangOptions::GCMode)Record[Idx++]);
John McCall1fb0caa2010-10-22 21:05:15 +00002632 LangOpts.setVisibilityMode((Visibility)Record[Idx++]);
Daniel Dunbarab8e2812009-09-21 04:16:19 +00002633 LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode)
Chris Lattnera4d71452010-06-26 21:25:03 +00002634 Record[Idx++]);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002635 PARSE_LANGOPT(InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +00002636 PARSE_LANGOPT(OpenCL);
Peter Collingbourne08a53262010-12-01 19:14:57 +00002637 PARSE_LANGOPT(CUDA);
Mike Stump9c276ae2009-12-12 01:27:46 +00002638 PARSE_LANGOPT(CatchUndefined);
2639 // FIXME: Missing ElideConstructors?!
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002640 #undef PARSE_LANGOPT
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002641
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002642 return Listener->ReadLanguageOptions(LangOpts);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002643 }
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002644
2645 return false;
2646}
2647
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002648void ASTReader::ReadPreprocessedEntities() {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002649 ReadDefinedMacros();
2650}
2651
Douglas Gregor89d99802010-11-30 06:16:57 +00002652PreprocessedEntity *ASTReader::ReadPreprocessedEntity(uint64_t Offset) {
2653 PerFileData *F = 0;
2654 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2655 if (Offset < Chain[I]->SizeInBits) {
2656 F = Chain[I];
2657 break;
2658 }
2659
2660 Offset -= Chain[I]->SizeInBits;
2661 }
2662
2663 if (!F) {
2664 Error("Malformed preprocessed entity offset");
2665 return 0;
2666 }
2667
2668 return ReadMacroRecord(*F, Offset);
2669}
2670
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002671void ASTReader::ReadUserDiagnosticMappings(Diagnostic &Diag) {
2672 unsigned Idx = 0;
2673 while (Idx < UserDiagMappings.size()) {
2674 unsigned DiagID = UserDiagMappings[Idx++];
2675 unsigned Map = UserDiagMappings[Idx++];
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00002676 Diag.setDiagnosticMappingInternal(DiagID, Map, Diag.GetCurDiagState(),
2677 /*isUser=*/true);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002678 }
2679}
2680
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002681/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002682ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002683 PerFileData *F = 0;
2684 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2685 F = Chain[N - I - 1];
2686 if (Index < F->LocalNumTypes)
2687 break;
2688 Index -= F->LocalNumTypes;
2689 }
2690 assert(F && F->LocalNumTypes > Index && "Broken chain");
Sebastian Redlc3632732010-10-05 15:59:54 +00002691 return RecordLocation(F, F->TypeOffsets[Index]);
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002692}
2693
2694/// \brief Read and return the type with the given index..
Douglas Gregor2cf26342009-04-09 22:27:44 +00002695///
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002696/// The index is the type ID, shifted and minus the number of predefs. This
2697/// routine actually reads the record corresponding to the type at the given
2698/// location. It is a helper routine for GetType, which deals with reading type
2699/// IDs.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002700QualType ASTReader::ReadTypeRecord(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002701 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00002702 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00002703
Douglas Gregor0b748912009-04-14 21:18:50 +00002704 // Keep track of where we are in the stream, then jump back there
2705 // after reading this type.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002706 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregor0b748912009-04-14 21:18:50 +00002707
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00002708 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redl27372b42010-08-11 18:52:41 +00002709
Douglas Gregord89275b2009-07-06 18:54:52 +00002710 // Note that we are loading a type record.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00002711 Deserializing AType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00002712
Sebastian Redlc3632732010-10-05 15:59:54 +00002713 DeclsCursor.JumpToBit(Loc.Offset);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002714 RecordData Record;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002715 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002716 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
2717 case TYPE_EXT_QUAL: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002718 if (Record.size() != 2) {
2719 Error("Incorrect encoding of extended qualifier type");
2720 return QualType();
2721 }
Douglas Gregor6d473962009-04-15 22:00:08 +00002722 QualType Base = GetType(Record[0]);
John McCall0953e762009-09-24 19:53:00 +00002723 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[1]);
2724 return Context->getQualifiedType(Base, Quals);
Douglas Gregor6d473962009-04-15 22:00:08 +00002725 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002726
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002727 case TYPE_COMPLEX: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002728 if (Record.size() != 1) {
2729 Error("Incorrect encoding of complex type");
2730 return QualType();
2731 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002732 QualType ElemType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002733 return Context->getComplexType(ElemType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002734 }
2735
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002736 case TYPE_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002737 if (Record.size() != 1) {
2738 Error("Incorrect encoding of pointer type");
2739 return QualType();
2740 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002741 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002742 return Context->getPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002743 }
2744
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002745 case TYPE_BLOCK_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002746 if (Record.size() != 1) {
2747 Error("Incorrect encoding of block pointer type");
2748 return QualType();
2749 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002750 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002751 return Context->getBlockPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002752 }
2753
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002754 case TYPE_LVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002755 if (Record.size() != 1) {
2756 Error("Incorrect encoding of lvalue reference type");
2757 return QualType();
2758 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002759 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002760 return Context->getLValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002761 }
2762
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002763 case TYPE_RVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002764 if (Record.size() != 1) {
2765 Error("Incorrect encoding of rvalue reference type");
2766 return QualType();
2767 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002768 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002769 return Context->getRValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002770 }
2771
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002772 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidis240437b2010-07-02 11:55:15 +00002773 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002774 Error("Incorrect encoding of member pointer type");
2775 return QualType();
2776 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002777 QualType PointeeType = GetType(Record[0]);
2778 QualType ClassType = GetType(Record[1]);
Douglas Gregor1ab55e92010-12-10 17:03:06 +00002779 if (PointeeType.isNull() || ClassType.isNull())
2780 return QualType();
2781
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002782 return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002783 }
2784
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002785 case TYPE_CONSTANT_ARRAY: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002786 QualType ElementType = GetType(Record[0]);
2787 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2788 unsigned IndexTypeQuals = Record[2];
2789 unsigned Idx = 3;
2790 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002791 return Context->getConstantArrayType(ElementType, Size,
2792 ASM, IndexTypeQuals);
2793 }
2794
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002795 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002796 QualType ElementType = GetType(Record[0]);
2797 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2798 unsigned IndexTypeQuals = Record[2];
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002799 return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002800 }
2801
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002802 case TYPE_VARIABLE_ARRAY: {
Douglas Gregor0b748912009-04-14 21:18:50 +00002803 QualType ElementType = GetType(Record[0]);
2804 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2805 unsigned IndexTypeQuals = Record[2];
Sebastian Redlc3632732010-10-05 15:59:54 +00002806 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
2807 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
2808 return Context->getVariableArrayType(ElementType, ReadExpr(*Loc.F),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002809 ASM, IndexTypeQuals,
2810 SourceRange(LBLoc, RBLoc));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002811 }
2812
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002813 case TYPE_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00002814 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002815 Error("incorrect encoding of vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002816 return QualType();
2817 }
2818
2819 QualType ElementType = GetType(Record[0]);
2820 unsigned NumElements = Record[1];
Bob Wilsone86d78c2010-11-10 21:56:12 +00002821 unsigned VecKind = Record[2];
Chris Lattner788b0fd2010-06-23 06:00:24 +00002822 return Context->getVectorType(ElementType, NumElements,
Bob Wilsone86d78c2010-11-10 21:56:12 +00002823 (VectorType::VectorKind)VecKind);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002824 }
2825
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002826 case TYPE_EXT_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00002827 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002828 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002829 return QualType();
2830 }
2831
2832 QualType ElementType = GetType(Record[0]);
2833 unsigned NumElements = Record[1];
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002834 return Context->getExtVectorType(ElementType, NumElements);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002835 }
2836
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002837 case TYPE_FUNCTION_NO_PROTO: {
Rafael Espindola425ef722010-03-30 22:15:11 +00002838 if (Record.size() != 4) {
Douglas Gregora02b1472009-04-28 21:53:25 +00002839 Error("incorrect encoding of no-proto function type");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002840 return QualType();
2841 }
2842 QualType ResultType = GetType(Record[0]);
Rafael Espindola425ef722010-03-30 22:15:11 +00002843 FunctionType::ExtInfo Info(Record[1], Record[2], (CallingConv)Record[3]);
Rafael Espindola264ba482010-03-30 20:24:48 +00002844 return Context->getFunctionNoProtoType(ResultType, Info);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002845 }
2846
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002847 case TYPE_FUNCTION_PROTO: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002848 QualType ResultType = GetType(Record[0]);
John McCalle23cf432010-12-14 08:05:40 +00002849
2850 FunctionProtoType::ExtProtoInfo EPI;
2851 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
2852 /*regparm*/ Record[2],
2853 static_cast<CallingConv>(Record[3]));
2854
Rafael Espindola425ef722010-03-30 22:15:11 +00002855 unsigned Idx = 4;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002856 unsigned NumParams = Record[Idx++];
2857 llvm::SmallVector<QualType, 16> ParamTypes;
2858 for (unsigned I = 0; I != NumParams; ++I)
2859 ParamTypes.push_back(GetType(Record[Idx++]));
John McCalle23cf432010-12-14 08:05:40 +00002860
2861 EPI.Variadic = Record[Idx++];
2862 EPI.TypeQuals = Record[Idx++];
2863 EPI.HasExceptionSpec = Record[Idx++];
2864 EPI.HasAnyExceptionSpec = Record[Idx++];
2865 EPI.NumExceptions = Record[Idx++];
Sebastian Redl465226e2009-05-27 22:11:52 +00002866 llvm::SmallVector<QualType, 2> Exceptions;
John McCalle23cf432010-12-14 08:05:40 +00002867 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
Sebastian Redl465226e2009-05-27 22:11:52 +00002868 Exceptions.push_back(GetType(Record[Idx++]));
John McCalle23cf432010-12-14 08:05:40 +00002869 EPI.Exceptions = Exceptions.data();
Jay Foadbeaaccd2009-05-21 09:52:38 +00002870 return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams,
John McCalle23cf432010-12-14 08:05:40 +00002871 EPI);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002872 }
2873
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002874 case TYPE_UNRESOLVED_USING:
John McCalled976492009-12-04 22:46:56 +00002875 return Context->getTypeDeclType(
2876 cast<UnresolvedUsingTypenameDecl>(GetDecl(Record[0])));
2877
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002878 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002879 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002880 Error("incorrect encoding of typedef type");
2881 return QualType();
2882 }
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002883 TypedefDecl *Decl = cast<TypedefDecl>(GetDecl(Record[0]));
2884 QualType Canonical = GetType(Record[1]);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00002885 if (!Canonical.isNull())
2886 Canonical = Context->getCanonicalType(Canonical);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002887 return Context->getTypedefType(Decl, Canonical);
2888 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002889
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002890 case TYPE_TYPEOF_EXPR:
Sebastian Redlc3632732010-10-05 15:59:54 +00002891 return Context->getTypeOfExprType(ReadExpr(*Loc.F));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002892
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002893 case TYPE_TYPEOF: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002894 if (Record.size() != 1) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002895 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002896 return QualType();
2897 }
2898 QualType UnderlyingType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002899 return Context->getTypeOfType(UnderlyingType);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002900 }
Mike Stump1eb44332009-09-09 15:08:12 +00002901
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002902 case TYPE_DECLTYPE:
Sebastian Redlc3632732010-10-05 15:59:54 +00002903 return Context->getDecltypeType(ReadExpr(*Loc.F));
Anders Carlsson395b4752009-06-24 19:06:50 +00002904
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002905 case TYPE_RECORD: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002906 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002907 Error("incorrect encoding of record type");
2908 return QualType();
2909 }
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002910 bool IsDependent = Record[0];
2911 QualType T = Context->getRecordType(cast<RecordDecl>(GetDecl(Record[1])));
John McCallb870b882010-10-14 21:48:26 +00002912 T->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002913 return T;
2914 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002915
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002916 case TYPE_ENUM: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002917 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002918 Error("incorrect encoding of enum type");
2919 return QualType();
2920 }
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002921 bool IsDependent = Record[0];
2922 QualType T = Context->getEnumType(cast<EnumDecl>(GetDecl(Record[1])));
John McCallb870b882010-10-14 21:48:26 +00002923 T->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002924 return T;
2925 }
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002926
John McCall9d156a72011-01-06 01:58:22 +00002927 case TYPE_ATTRIBUTED: {
2928 if (Record.size() != 3) {
2929 Error("incorrect encoding of attributed type");
2930 return QualType();
2931 }
2932 QualType modifiedType = GetType(Record[0]);
2933 QualType equivalentType = GetType(Record[1]);
2934 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
2935 return Context->getAttributedType(kind, modifiedType, equivalentType);
2936 }
2937
Abramo Bagnara075f8f12010-12-10 16:29:40 +00002938 case TYPE_PAREN: {
2939 if (Record.size() != 1) {
2940 Error("incorrect encoding of paren type");
2941 return QualType();
2942 }
2943 QualType InnerType = GetType(Record[0]);
2944 return Context->getParenType(InnerType);
2945 }
2946
Douglas Gregor7536dd52010-12-20 02:24:11 +00002947 case TYPE_PACK_EXPANSION: {
2948 if (Record.size() != 1) {
2949 Error("incorrect encoding of pack expansion type");
2950 return QualType();
2951 }
2952 QualType Pattern = GetType(Record[0]);
2953 if (Pattern.isNull())
2954 return QualType();
2955
2956 return Context->getPackExpansionType(Pattern);
2957 }
2958
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002959 case TYPE_ELABORATED: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00002960 unsigned Idx = 0;
2961 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2962 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2963 QualType NamedType = GetType(Record[Idx++]);
2964 return Context->getElaboratedType(Keyword, NNS, NamedType);
John McCall7da24312009-09-05 00:15:47 +00002965 }
2966
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002967 case TYPE_OBJC_INTERFACE: {
Chris Lattnerc6fa4452009-04-22 06:45:28 +00002968 unsigned Idx = 0;
2969 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
John McCallc12c5bb2010-05-15 11:32:37 +00002970 return Context->getObjCInterfaceType(ItfD);
2971 }
2972
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002973 case TYPE_OBJC_OBJECT: {
John McCallc12c5bb2010-05-15 11:32:37 +00002974 unsigned Idx = 0;
2975 QualType Base = GetType(Record[Idx++]);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00002976 unsigned NumProtos = Record[Idx++];
2977 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2978 for (unsigned I = 0; I != NumProtos; ++I)
2979 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
Michael J. Spencer20249a12010-10-21 03:16:25 +00002980 return Context->getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00002981 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002982
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002983 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00002984 unsigned Idx = 0;
John McCallc12c5bb2010-05-15 11:32:37 +00002985 QualType Pointee = GetType(Record[Idx++]);
2986 return Context->getObjCObjectPointerType(Pointee);
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00002987 }
Argyrios Kyrtzidis24fab412009-09-29 19:42:55 +00002988
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002989 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCall49a832b2009-10-18 09:09:24 +00002990 unsigned Idx = 0;
2991 QualType Parm = GetType(Record[Idx++]);
2992 QualType Replacement = GetType(Record[Idx++]);
2993 return
2994 Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
2995 Replacement);
2996 }
John McCall3cb0ebd2010-03-10 03:28:59 +00002997
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002998 case TYPE_INJECTED_CLASS_NAME: {
John McCall3cb0ebd2010-03-10 03:28:59 +00002999 CXXRecordDecl *D = cast<CXXRecordDecl>(GetDecl(Record[0]));
3000 QualType TST = GetType(Record[1]); // probably derivable
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00003001 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003002 // for AST reading, too much interdependencies.
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00003003 return
3004 QualType(new (*Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCall3cb0ebd2010-03-10 03:28:59 +00003005 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003006
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003007 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003008 unsigned Idx = 0;
3009 unsigned Depth = Record[Idx++];
3010 unsigned Index = Record[Idx++];
3011 bool Pack = Record[Idx++];
3012 IdentifierInfo *Name = GetIdentifierInfo(Record, Idx);
3013 return Context->getTemplateTypeParmType(Depth, Index, Pack, Name);
3014 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003015
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003016 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00003017 unsigned Idx = 0;
3018 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3019 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3020 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +00003021 QualType Canon = GetType(Record[Idx++]);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00003022 if (!Canon.isNull())
3023 Canon = Context->getCanonicalType(Canon);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +00003024 return Context->getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00003025 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003026
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003027 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00003028 unsigned Idx = 0;
3029 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3030 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3031 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
3032 unsigned NumArgs = Record[Idx++];
3033 llvm::SmallVector<TemplateArgument, 8> Args;
3034 Args.reserve(NumArgs);
3035 while (NumArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00003036 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00003037 return Context->getDependentTemplateSpecializationType(Keyword, NNS, Name,
3038 Args.size(), Args.data());
3039 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003040
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003041 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00003042 unsigned Idx = 0;
3043
3044 // ArrayType
3045 QualType ElementType = GetType(Record[Idx++]);
3046 ArrayType::ArraySizeModifier ASM
3047 = (ArrayType::ArraySizeModifier)Record[Idx++];
3048 unsigned IndexTypeQuals = Record[Idx++];
3049
3050 // DependentSizedArrayType
Sebastian Redlc3632732010-10-05 15:59:54 +00003051 Expr *NumElts = ReadExpr(*Loc.F);
3052 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00003053
3054 return Context->getDependentSizedArrayType(ElementType, NumElts, ASM,
3055 IndexTypeQuals, Brackets);
3056 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003057
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003058 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003059 unsigned Idx = 0;
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003060 bool IsDependent = Record[Idx++];
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003061 TemplateName Name = ReadTemplateName(Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003062 llvm::SmallVector<TemplateArgument, 8> Args;
Sebastian Redlc3632732010-10-05 15:59:54 +00003063 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003064 QualType Canon = GetType(Record[Idx++]);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003065 QualType T;
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003066 if (Canon.isNull())
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003067 T = Context->getCanonicalTemplateSpecializationType(Name, Args.data(),
3068 Args.size());
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003069 else
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003070 T = Context->getTemplateSpecializationType(Name, Args.data(),
3071 Args.size(), Canon);
John McCallb870b882010-10-14 21:48:26 +00003072 T->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003073 return T;
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003074 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003075 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003076 // Suppress a GCC warning
3077 return QualType();
3078}
3079
Sebastian Redlc3632732010-10-05 15:59:54 +00003080class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003081 ASTReader &Reader;
Sebastian Redlc3632732010-10-05 15:59:54 +00003082 ASTReader::PerFileData &F;
Sebastian Redl577d4792010-07-22 22:43:28 +00003083 llvm::BitstreamCursor &DeclsCursor;
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003084 const ASTReader::RecordData &Record;
John McCalla1ee0c52009-10-16 21:56:05 +00003085 unsigned &Idx;
3086
Sebastian Redlc3632732010-10-05 15:59:54 +00003087 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
3088 unsigned &I) {
3089 return Reader.ReadSourceLocation(F, R, I);
3090 }
3091
John McCalla1ee0c52009-10-16 21:56:05 +00003092public:
Sebastian Redlc3632732010-10-05 15:59:54 +00003093 TypeLocReader(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003094 const ASTReader::RecordData &Record, unsigned &Idx)
Sebastian Redlc3632732010-10-05 15:59:54 +00003095 : Reader(Reader), F(F), DeclsCursor(F.DeclsCursor), Record(Record), Idx(Idx)
3096 { }
John McCalla1ee0c52009-10-16 21:56:05 +00003097
John McCall51bd8032009-10-18 01:05:36 +00003098 // We want compile-time assurance that we've enumerated all of
3099 // these, so unfortunately we have to declare them first, then
3100 // define them out-of-line.
3101#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +00003102#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +00003103 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00003104#include "clang/AST/TypeLocNodes.def"
3105
John McCall51bd8032009-10-18 01:05:36 +00003106 void VisitFunctionTypeLoc(FunctionTypeLoc);
3107 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00003108};
3109
John McCall51bd8032009-10-18 01:05:36 +00003110void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCalla1ee0c52009-10-16 21:56:05 +00003111 // nothing to do
3112}
John McCall51bd8032009-10-18 01:05:36 +00003113void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003114 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
Douglas Gregorddf889a2010-01-18 18:04:31 +00003115 if (TL.needsExtraLocalData()) {
3116 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
3117 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
3118 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
3119 TL.setModeAttr(Record[Idx++]);
3120 }
John McCalla1ee0c52009-10-16 21:56:05 +00003121}
John McCall51bd8032009-10-18 01:05:36 +00003122void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003123 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003124}
John McCall51bd8032009-10-18 01:05:36 +00003125void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003126 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003127}
John McCall51bd8032009-10-18 01:05:36 +00003128void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003129 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003130}
John McCall51bd8032009-10-18 01:05:36 +00003131void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003132 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003133}
John McCall51bd8032009-10-18 01:05:36 +00003134void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003135 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003136}
John McCall51bd8032009-10-18 01:05:36 +00003137void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003138 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003139}
John McCall51bd8032009-10-18 01:05:36 +00003140void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003141 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
3142 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003143 if (Record[Idx++])
Sebastian Redlc3632732010-10-05 15:59:54 +00003144 TL.setSizeExpr(Reader.ReadExpr(F));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003145 else
John McCall51bd8032009-10-18 01:05:36 +00003146 TL.setSizeExpr(0);
3147}
3148void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
3149 VisitArrayTypeLoc(TL);
3150}
3151void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
3152 VisitArrayTypeLoc(TL);
3153}
3154void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
3155 VisitArrayTypeLoc(TL);
3156}
3157void TypeLocReader::VisitDependentSizedArrayTypeLoc(
3158 DependentSizedArrayTypeLoc TL) {
3159 VisitArrayTypeLoc(TL);
3160}
3161void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
3162 DependentSizedExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003163 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003164}
3165void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003166 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003167}
3168void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003169 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003170}
3171void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003172 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3173 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
Douglas Gregordab60ad2010-10-01 18:44:50 +00003174 TL.setTrailingReturn(Record[Idx++]);
John McCall51bd8032009-10-18 01:05:36 +00003175 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
John McCall86acc2a2009-10-23 01:28:53 +00003176 TL.setArg(i, cast_or_null<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
John McCall51bd8032009-10-18 01:05:36 +00003177 }
3178}
3179void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
3180 VisitFunctionTypeLoc(TL);
3181}
3182void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
3183 VisitFunctionTypeLoc(TL);
3184}
John McCalled976492009-12-04 22:46:56 +00003185void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003186 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalled976492009-12-04 22:46:56 +00003187}
John McCall51bd8032009-10-18 01:05:36 +00003188void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003189 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003190}
3191void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003192 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3193 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3194 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003195}
3196void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003197 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3198 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3199 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3200 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003201}
3202void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003203 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003204}
3205void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003206 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003207}
3208void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003209 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003210}
John McCall9d156a72011-01-06 01:58:22 +00003211void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
3212 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
3213 if (TL.hasAttrOperand()) {
3214 SourceRange range;
3215 range.setBegin(ReadSourceLocation(Record, Idx));
3216 range.setEnd(ReadSourceLocation(Record, Idx));
3217 TL.setAttrOperandParensRange(range);
3218 }
3219 if (TL.hasAttrExprOperand()) {
3220 if (Record[Idx++])
3221 TL.setAttrExprOperand(Reader.ReadExpr(F));
3222 else
3223 TL.setAttrExprOperand(0);
3224 } else if (TL.hasAttrEnumOperand())
3225 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
3226}
John McCall51bd8032009-10-18 01:05:36 +00003227void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003228 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003229}
John McCall49a832b2009-10-18 09:09:24 +00003230void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
3231 SubstTemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003232 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall49a832b2009-10-18 09:09:24 +00003233}
John McCall51bd8032009-10-18 01:05:36 +00003234void TypeLocReader::VisitTemplateSpecializationTypeLoc(
3235 TemplateSpecializationTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003236 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
3237 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3238 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall833ca992009-10-29 08:12:44 +00003239 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
3240 TL.setArgLocInfo(i,
Sebastian Redlc3632732010-10-05 15:59:54 +00003241 Reader.GetTemplateArgumentLocInfo(F,
3242 TL.getTypePtr()->getArg(i).getKind(),
3243 Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003244}
Abramo Bagnara075f8f12010-12-10 16:29:40 +00003245void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
3246 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3247 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3248}
Abramo Bagnara465d41b2010-05-11 21:36:43 +00003249void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003250 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3251 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003252}
John McCall3cb0ebd2010-03-10 03:28:59 +00003253void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003254 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall3cb0ebd2010-03-10 03:28:59 +00003255}
Douglas Gregor4714c122010-03-31 17:34:00 +00003256void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003257 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3258 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
3259 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003260}
John McCall33500952010-06-11 00:33:02 +00003261void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
3262 DependentTemplateSpecializationTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003263 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3264 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
3265 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3266 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3267 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall33500952010-06-11 00:33:02 +00003268 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
3269 TL.setArgLocInfo(I,
Sebastian Redlc3632732010-10-05 15:59:54 +00003270 Reader.GetTemplateArgumentLocInfo(F,
3271 TL.getTypePtr()->getArg(I).getKind(),
3272 Record, Idx));
John McCall33500952010-06-11 00:33:02 +00003273}
Douglas Gregor7536dd52010-12-20 02:24:11 +00003274void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
3275 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
3276}
John McCall51bd8032009-10-18 01:05:36 +00003277void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003278 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallc12c5bb2010-05-15 11:32:37 +00003279}
3280void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
3281 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00003282 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3283 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003284 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Sebastian Redlc3632732010-10-05 15:59:54 +00003285 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003286}
John McCall54e14c42009-10-22 22:37:11 +00003287void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003288 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall54e14c42009-10-22 22:37:11 +00003289}
John McCalla1ee0c52009-10-16 21:56:05 +00003290
Sebastian Redlc3632732010-10-05 15:59:54 +00003291TypeSourceInfo *ASTReader::GetTypeSourceInfo(PerFileData &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00003292 const RecordData &Record,
John McCalla1ee0c52009-10-16 21:56:05 +00003293 unsigned &Idx) {
3294 QualType InfoTy = GetType(Record[Idx++]);
3295 if (InfoTy.isNull())
3296 return 0;
3297
John McCalla93c9342009-12-07 02:54:59 +00003298 TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy);
Sebastian Redlc3632732010-10-05 15:59:54 +00003299 TypeLocReader TLR(*this, F, Record, Idx);
John McCalla93c9342009-12-07 02:54:59 +00003300 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCalla1ee0c52009-10-16 21:56:05 +00003301 TLR.Visit(TL);
John McCalla93c9342009-12-07 02:54:59 +00003302 return TInfo;
John McCalla1ee0c52009-10-16 21:56:05 +00003303}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003304
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003305QualType ASTReader::GetType(TypeID ID) {
John McCall0953e762009-09-24 19:53:00 +00003306 unsigned FastQuals = ID & Qualifiers::FastMask;
3307 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003308
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003309 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003310 QualType T;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003311 switch ((PredefinedTypeIDs)Index) {
3312 case PREDEF_TYPE_NULL_ID: return QualType();
3313 case PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break;
3314 case PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003315
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003316 case PREDEF_TYPE_CHAR_U_ID:
3317 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregor2cf26342009-04-09 22:27:44 +00003318 // FIXME: Check that the signedness of CharTy is correct!
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003319 T = Context->CharTy;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003320 break;
3321
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003322 case PREDEF_TYPE_UCHAR_ID: T = Context->UnsignedCharTy; break;
3323 case PREDEF_TYPE_USHORT_ID: T = Context->UnsignedShortTy; break;
3324 case PREDEF_TYPE_UINT_ID: T = Context->UnsignedIntTy; break;
3325 case PREDEF_TYPE_ULONG_ID: T = Context->UnsignedLongTy; break;
3326 case PREDEF_TYPE_ULONGLONG_ID: T = Context->UnsignedLongLongTy; break;
3327 case PREDEF_TYPE_UINT128_ID: T = Context->UnsignedInt128Ty; break;
3328 case PREDEF_TYPE_SCHAR_ID: T = Context->SignedCharTy; break;
3329 case PREDEF_TYPE_WCHAR_ID: T = Context->WCharTy; break;
3330 case PREDEF_TYPE_SHORT_ID: T = Context->ShortTy; break;
3331 case PREDEF_TYPE_INT_ID: T = Context->IntTy; break;
3332 case PREDEF_TYPE_LONG_ID: T = Context->LongTy; break;
3333 case PREDEF_TYPE_LONGLONG_ID: T = Context->LongLongTy; break;
3334 case PREDEF_TYPE_INT128_ID: T = Context->Int128Ty; break;
3335 case PREDEF_TYPE_FLOAT_ID: T = Context->FloatTy; break;
3336 case PREDEF_TYPE_DOUBLE_ID: T = Context->DoubleTy; break;
3337 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy; break;
3338 case PREDEF_TYPE_OVERLOAD_ID: T = Context->OverloadTy; break;
3339 case PREDEF_TYPE_DEPENDENT_ID: T = Context->DependentTy; break;
3340 case PREDEF_TYPE_NULLPTR_ID: T = Context->NullPtrTy; break;
3341 case PREDEF_TYPE_CHAR16_ID: T = Context->Char16Ty; break;
3342 case PREDEF_TYPE_CHAR32_ID: T = Context->Char32Ty; break;
3343 case PREDEF_TYPE_OBJC_ID: T = Context->ObjCBuiltinIdTy; break;
3344 case PREDEF_TYPE_OBJC_CLASS: T = Context->ObjCBuiltinClassTy; break;
3345 case PREDEF_TYPE_OBJC_SEL: T = Context->ObjCBuiltinSelTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003346 }
3347
3348 assert(!T.isNull() && "Unknown predefined type");
John McCall0953e762009-09-24 19:53:00 +00003349 return T.withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003350 }
3351
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003352 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003353 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl07a353c2010-07-14 20:26:45 +00003354 if (TypesLoaded[Index].isNull()) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003355 TypesLoaded[Index] = ReadTypeRecord(Index);
Douglas Gregor97475832010-10-05 18:37:06 +00003356 if (TypesLoaded[Index].isNull())
3357 return QualType();
3358
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003359 TypesLoaded[Index]->setFromAST();
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003360 TypeIdxs[TypesLoaded[Index]] = TypeIdx::fromTypeID(ID);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003361 if (DeserializationListener)
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003362 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1476ed42010-07-16 16:36:56 +00003363 TypesLoaded[Index]);
Sebastian Redl07a353c2010-07-14 20:26:45 +00003364 }
Mike Stump1eb44332009-09-09 15:08:12 +00003365
John McCall0953e762009-09-24 19:53:00 +00003366 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003367}
3368
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003369TypeID ASTReader::GetTypeID(QualType T) const {
3370 return MakeTypeID(T,
3371 std::bind1st(std::mem_fun(&ASTReader::GetTypeIdx), this));
3372}
3373
3374TypeIdx ASTReader::GetTypeIdx(QualType T) const {
3375 if (T.isNull())
3376 return TypeIdx();
3377 assert(!T.getLocalFastQualifiers());
3378
3379 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3380 // GetTypeIdx is mostly used for computing the hash of DeclarationNames and
3381 // comparing keys of ASTDeclContextNameLookupTable.
3382 // If the type didn't come from the AST file use a specially marked index
3383 // so that any hash/key comparison fail since no such index is stored
3384 // in a AST file.
3385 if (I == TypeIdxs.end())
3386 return TypeIdx(-1);
3387 return I->second;
3388}
3389
Douglas Gregor7c789c12010-10-29 22:39:52 +00003390unsigned ASTReader::getTotalNumCXXBaseSpecifiers() const {
3391 unsigned Result = 0;
3392 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
3393 Result += Chain[I]->LocalNumCXXBaseSpecifiers;
3394
3395 return Result;
3396}
3397
John McCall833ca992009-10-29 08:12:44 +00003398TemplateArgumentLocInfo
Sebastian Redlc3632732010-10-05 15:59:54 +00003399ASTReader::GetTemplateArgumentLocInfo(PerFileData &F,
3400 TemplateArgument::ArgKind Kind,
John McCall833ca992009-10-29 08:12:44 +00003401 const RecordData &Record,
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00003402 unsigned &Index) {
John McCall833ca992009-10-29 08:12:44 +00003403 switch (Kind) {
3404 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00003405 return ReadExpr(F);
John McCall833ca992009-10-29 08:12:44 +00003406 case TemplateArgument::Type:
Sebastian Redlc3632732010-10-05 15:59:54 +00003407 return GetTypeSourceInfo(F, Record, Index);
Douglas Gregor788cd062009-11-11 01:00:40 +00003408 case TemplateArgument::Template: {
Sebastian Redlc3632732010-10-05 15:59:54 +00003409 SourceRange QualifierRange = ReadSourceRange(F, Record, Index);
3410 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003411 return TemplateArgumentLocInfo(QualifierRange, TemplateNameLoc,
3412 SourceLocation());
3413 }
3414 case TemplateArgument::TemplateExpansion: {
3415 SourceRange QualifierRange = ReadSourceRange(F, Record, Index);
3416 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorba68eca2011-01-05 17:40:24 +00003417 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
3418 return TemplateArgumentLocInfo(QualifierRange, TemplateNameLoc,
3419 EllipsisLoc);
Douglas Gregor788cd062009-11-11 01:00:40 +00003420 }
John McCall833ca992009-10-29 08:12:44 +00003421 case TemplateArgument::Null:
3422 case TemplateArgument::Integral:
3423 case TemplateArgument::Declaration:
3424 case TemplateArgument::Pack:
3425 return TemplateArgumentLocInfo();
3426 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00003427 llvm_unreachable("unexpected template argument loc");
John McCall833ca992009-10-29 08:12:44 +00003428 return TemplateArgumentLocInfo();
3429}
3430
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003431TemplateArgumentLoc
Sebastian Redlc3632732010-10-05 15:59:54 +00003432ASTReader::ReadTemplateArgumentLoc(PerFileData &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00003433 const RecordData &Record, unsigned &Index) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003434 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003435
3436 if (Arg.getKind() == TemplateArgument::Expression) {
3437 if (Record[Index++]) // bool InfoHasSameExpr.
3438 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
3439 }
Sebastian Redlc3632732010-10-05 15:59:54 +00003440 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00003441 Record, Index));
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003442}
3443
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003444Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall76bd1f32010-06-01 09:23:16 +00003445 return GetDecl(ID);
3446}
3447
Douglas Gregor7c789c12010-10-29 22:39:52 +00003448uint64_t
3449ASTReader::GetCXXBaseSpecifiersOffset(serialization::CXXBaseSpecifiersID ID) {
3450 if (ID == 0)
3451 return 0;
3452
3453 --ID;
3454 uint64_t Offset = 0;
3455 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3456 if (ID < Chain[I]->LocalNumCXXBaseSpecifiers)
3457 return Offset + Chain[I]->CXXBaseSpecifiersOffsets[ID];
3458
3459 ID -= Chain[I]->LocalNumCXXBaseSpecifiers;
3460 Offset += Chain[I]->SizeInBits;
3461 }
3462
3463 assert(false && "CXXBaseSpecifiers not found");
3464 return 0;
3465}
3466
3467CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
3468 // Figure out which AST file contains this offset.
3469 PerFileData *F = 0;
3470 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3471 if (Offset < Chain[I]->SizeInBits) {
3472 F = Chain[I];
3473 break;
3474 }
3475
3476 Offset -= Chain[I]->SizeInBits;
3477 }
3478
3479 if (!F) {
3480 Error("Malformed AST file: C++ base specifiers at impossible offset");
3481 return 0;
3482 }
3483
3484 llvm::BitstreamCursor &Cursor = F->DeclsCursor;
3485 SavedStreamPosition SavedPosition(Cursor);
3486 Cursor.JumpToBit(Offset);
3487 ReadingKindTracker ReadingKind(Read_Decl, *this);
3488 RecordData Record;
3489 unsigned Code = Cursor.ReadCode();
3490 unsigned RecCode = Cursor.ReadRecord(Code, Record);
3491 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
3492 Error("Malformed AST file: missing C++ base specifiers");
3493 return 0;
3494 }
3495
3496 unsigned Idx = 0;
3497 unsigned NumBases = Record[Idx++];
3498 void *Mem = Context->Allocate(sizeof(CXXBaseSpecifier) * NumBases);
3499 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
3500 for (unsigned I = 0; I != NumBases; ++I)
3501 Bases[I] = ReadCXXBaseSpecifier(*F, Record, Idx);
3502 return Bases;
3503}
3504
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003505TranslationUnitDecl *ASTReader::GetTranslationUnitDecl() {
Sebastian Redl30c514c2010-07-14 23:45:08 +00003506 if (!DeclsLoaded[0]) {
Sebastian Redle1dde812010-08-24 00:50:04 +00003507 ReadDeclRecord(0, 1);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003508 if (DeserializationListener)
Sebastian Redl1476ed42010-07-16 16:36:56 +00003509 DeserializationListener->DeclRead(1, DeclsLoaded[0]);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003510 }
Argyrios Kyrtzidis8871a442010-07-08 17:13:02 +00003511
3512 return cast<TranslationUnitDecl>(DeclsLoaded[0]);
3513}
3514
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003515Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003516 if (ID == 0)
3517 return 0;
3518
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003519 if (ID > DeclsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003520 Error("declaration ID out-of-range for AST file");
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003521 return 0;
3522 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003523
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003524 unsigned Index = ID - 1;
Sebastian Redl30c514c2010-07-14 23:45:08 +00003525 if (!DeclsLoaded[Index]) {
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00003526 ReadDeclRecord(Index, ID);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003527 if (DeserializationListener)
3528 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
3529 }
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003530
3531 return DeclsLoaded[Index];
Douglas Gregor2cf26342009-04-09 22:27:44 +00003532}
3533
Chris Lattner887e2b32009-04-27 05:46:25 +00003534/// \brief Resolve the offset of a statement into a statement.
3535///
3536/// This operation will read a new statement from the external
3537/// source each time it is called, and is meant to be used via a
3538/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003539Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00003540 // Switch case IDs are per Decl.
3541 ClearSwitchCaseIDs();
3542
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003543 // Offset here is a global offset across the entire chain.
3544 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3545 PerFileData &F = *Chain[N - I - 1];
3546 if (Offset < F.SizeInBits) {
3547 // Since we know that this statement is part of a decl, make sure to use
3548 // the decl cursor to read it.
3549 F.DeclsCursor.JumpToBit(Offset);
Sebastian Redlc3632732010-10-05 15:59:54 +00003550 return ReadStmtFromStream(F);
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003551 }
3552 Offset -= F.SizeInBits;
3553 }
3554 llvm_unreachable("Broken chain");
Douglas Gregor250fc9c2009-04-18 00:07:54 +00003555}
3556
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003557bool ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00003558 bool (*isKindWeWant)(Decl::Kind),
John McCall76bd1f32010-06-01 09:23:16 +00003559 llvm::SmallVectorImpl<Decl*> &Decls) {
Mike Stump1eb44332009-09-09 15:08:12 +00003560 assert(DC->hasExternalLexicalStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00003561 "DeclContext has no lexical decls in storage");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003562
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003563 // There might be lexical decls in multiple parts of the chain, for the TU
3564 // at least.
Sebastian Redl4a9eb262010-09-28 02:24:44 +00003565 // DeclContextOffsets might reallocate as we load additional decls below,
3566 // so make a copy of the vector.
3567 DeclContextInfos Infos = DeclContextOffsets[DC];
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003568 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3569 I != E; ++I) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003570 // IDs can be 0 if this context doesn't contain declarations.
3571 if (!I->LexicalDecls)
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003572 continue;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003573
3574 // Load all of the declaration IDs
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00003575 for (const KindDeclIDPair *ID = I->LexicalDecls,
3576 *IDE = ID + I->NumLexicalDecls; ID != IDE; ++ID) {
3577 if (isKindWeWant && !isKindWeWant((Decl::Kind)ID->first))
3578 continue;
3579
3580 Decl *D = GetDecl(ID->second);
Sebastian Redl4a9eb262010-09-28 02:24:44 +00003581 assert(D && "Null decl in lexical decls");
3582 Decls.push_back(D);
3583 }
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003584 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003585
Douglas Gregor25123082009-04-22 22:34:57 +00003586 ++NumLexicalDeclContextsRead;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003587 return false;
3588}
3589
John McCall76bd1f32010-06-01 09:23:16 +00003590DeclContext::lookup_result
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003591ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +00003592 DeclarationName Name) {
Mike Stump1eb44332009-09-09 15:08:12 +00003593 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00003594 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003595 if (!Name)
3596 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
3597 DeclContext::lookup_iterator(0));
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003598
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003599 llvm::SmallVector<NamedDecl *, 64> Decls;
Sebastian Redl8b122732010-08-24 00:49:55 +00003600 // There might be visible decls in multiple parts of the chain, for the TU
Sebastian Redl5967d622010-08-24 00:50:16 +00003601 // and namespaces. For any given name, the last available results replace
3602 // all earlier ones. For this reason, we walk in reverse.
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003603 DeclContextInfos &Infos = DeclContextOffsets[DC];
Sebastian Redl5967d622010-08-24 00:50:16 +00003604 for (DeclContextInfos::reverse_iterator I = Infos.rbegin(), E = Infos.rend();
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003605 I != E; ++I) {
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003606 if (!I->NameLookupTableData)
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003607 continue;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003608
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003609 ASTDeclContextNameLookupTable *LookupTable =
3610 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3611 ASTDeclContextNameLookupTable::iterator Pos = LookupTable->find(Name);
3612 if (Pos == LookupTable->end())
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003613 continue;
3614
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003615 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
3616 for (; Data.first != Data.second; ++Data.first)
3617 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
Sebastian Redl5967d622010-08-24 00:50:16 +00003618 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003619 }
3620
Douglas Gregor25123082009-04-22 22:34:57 +00003621 ++NumVisibleDeclContextsRead;
John McCall76bd1f32010-06-01 09:23:16 +00003622
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003623 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall76bd1f32010-06-01 09:23:16 +00003624 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003625}
3626
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +00003627void ASTReader::MaterializeVisibleDecls(const DeclContext *DC) {
3628 assert(DC->hasExternalVisibleStorage() &&
3629 "DeclContext has no visible decls in storage");
3630
3631 llvm::SmallVector<NamedDecl *, 64> Decls;
3632 // There might be visible decls in multiple parts of the chain, for the TU
3633 // and namespaces.
3634 DeclContextInfos &Infos = DeclContextOffsets[DC];
3635 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3636 I != E; ++I) {
3637 if (!I->NameLookupTableData)
3638 continue;
3639
3640 ASTDeclContextNameLookupTable *LookupTable =
3641 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3642 for (ASTDeclContextNameLookupTable::item_iterator
3643 ItemI = LookupTable->item_begin(),
3644 ItemEnd = LookupTable->item_end() ; ItemI != ItemEnd; ++ItemI) {
3645 ASTDeclContextNameLookupTable::item_iterator::value_type Val
3646 = *ItemI;
3647 ASTDeclContextNameLookupTrait::data_type Data = Val.second;
3648 Decls.clear();
3649 for (; Data.first != Data.second; ++Data.first)
3650 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
3651 MaterializeVisibleDeclsForName(DC, Val.first, Decls);
3652 }
3653 }
3654}
3655
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003656void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003657 assert(Consumer);
3658 while (!InterestingDecls.empty()) {
3659 DeclGroupRef DG(InterestingDecls.front());
3660 InterestingDecls.pop_front();
Sebastian Redl27372b42010-08-11 18:52:41 +00003661 Consumer->HandleInterestingDecl(DG);
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003662 }
3663}
3664
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003665void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor0af2ca42009-04-22 19:09:20 +00003666 this->Consumer = Consumer;
3667
Douglas Gregorfdd01722009-04-14 00:24:19 +00003668 if (!Consumer)
3669 return;
3670
3671 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003672 // Force deserialization of this decl, which will cause it to be queued for
3673 // passing to the consumer.
Daniel Dunbar04a0b502009-09-17 03:06:44 +00003674 GetDecl(ExternalDefinitions[I]);
Douglas Gregorfdd01722009-04-14 00:24:19 +00003675 }
Douglas Gregorc62a2fe2009-04-25 00:41:30 +00003676
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003677 PassInterestingDeclsToConsumer();
Douglas Gregorfdd01722009-04-14 00:24:19 +00003678}
3679
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003680void ASTReader::PrintStats() {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003681 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregor2cf26342009-04-09 22:27:44 +00003682
Mike Stump1eb44332009-09-09 15:08:12 +00003683 unsigned NumTypesLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003684 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall0953e762009-09-24 19:53:00 +00003685 QualType());
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003686 unsigned NumDeclsLoaded
3687 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
3688 (Decl *)0);
3689 unsigned NumIdentifiersLoaded
3690 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
3691 IdentifiersLoaded.end(),
3692 (IdentifierInfo *)0);
Mike Stump1eb44332009-09-09 15:08:12 +00003693 unsigned NumSelectorsLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003694 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
3695 SelectorsLoaded.end(),
3696 Selector());
Douglas Gregor2d41cc12009-04-13 20:50:16 +00003697
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003698 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
3699 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00003700 if (TotalNumSLocEntries)
3701 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
3702 NumSLocEntriesRead, TotalNumSLocEntries,
3703 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003704 if (!TypesLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003705 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003706 NumTypesLoaded, (unsigned)TypesLoaded.size(),
3707 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
3708 if (!DeclsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003709 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003710 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
3711 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003712 if (!IdentifiersLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003713 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003714 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
3715 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Sebastian Redl725cd962010-08-04 20:40:17 +00003716 if (!SelectorsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003717 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redl725cd962010-08-04 20:40:17 +00003718 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
3719 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor83941df2009-04-25 17:48:32 +00003720 if (TotalNumStatements)
3721 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
3722 NumStatementsRead, TotalNumStatements,
3723 ((float)NumStatementsRead/TotalNumStatements * 100));
3724 if (TotalNumMacros)
3725 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
3726 NumMacrosRead, TotalNumMacros,
3727 ((float)NumMacrosRead/TotalNumMacros * 100));
3728 if (TotalLexicalDeclContexts)
3729 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
3730 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
3731 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
3732 * 100));
3733 if (TotalVisibleDeclContexts)
3734 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
3735 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
3736 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
3737 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003738 if (TotalNumMethodPoolEntries) {
Douglas Gregor83941df2009-04-25 17:48:32 +00003739 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003740 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
3741 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor83941df2009-04-25 17:48:32 +00003742 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003743 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor83941df2009-04-25 17:48:32 +00003744 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003745 std::fprintf(stderr, "\n");
3746}
3747
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003748void ASTReader::InitializeSema(Sema &S) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00003749 SemaObj = &S;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003750 S.ExternalSource = this;
3751
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00003752 // Makes sure any declarations that were deserialized "too early"
3753 // still get added to the identifier's declaration chains.
Douglas Gregor76dc8892010-09-24 23:29:12 +00003754 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
3755 if (SemaObj->TUScope)
John McCalld226f652010-08-21 09:40:31 +00003756 SemaObj->TUScope->AddDecl(PreloadedDecls[I]);
Douglas Gregor76dc8892010-09-24 23:29:12 +00003757
3758 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregor668c1a42009-04-21 22:25:48 +00003759 }
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00003760 PreloadedDecls.clear();
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003761
3762 // If there were any tentative definitions, deserialize them and add
Sebastian Redle9d12b62010-01-31 22:27:38 +00003763 // them to Sema's list of tentative definitions.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003764 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
3765 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
Sebastian Redle9d12b62010-01-31 22:27:38 +00003766 SemaObj->TentativeDefinitions.push_back(Var);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003767 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00003768
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003769 // If there were any unused file scoped decls, deserialize them and add to
3770 // Sema's list of unused file scoped decls.
3771 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
3772 DeclaratorDecl *D = cast<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
3773 SemaObj->UnusedFileScopedDecls.push_back(D);
Tanya Lattnere6bbc012010-02-12 00:07:30 +00003774 }
Douglas Gregor14c22f22009-04-22 22:18:58 +00003775
3776 // If there were any locally-scoped external declarations,
3777 // deserialize them and add them to Sema's table of locally-scoped
3778 // external declarations.
3779 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
3780 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
3781 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
3782 }
Douglas Gregorb81c1702009-04-27 20:06:05 +00003783
3784 // If there were any ext_vector type declarations, deserialize them
3785 // and add them to Sema's vector of such declarations.
3786 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I)
3787 SemaObj->ExtVectorDecls.push_back(
3788 cast<TypedefDecl>(GetDecl(ExtVectorDecls[I])));
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003789
3790 // FIXME: Do VTable uses and dynamic classes deserialize too much ?
3791 // Can we cut them down before writing them ?
3792
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003793 // If there were any dynamic classes declarations, deserialize them
3794 // and add them to Sema's vector of such declarations.
3795 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I)
3796 SemaObj->DynamicClasses.push_back(
3797 cast<CXXRecordDecl>(GetDecl(DynamicClasses[I])));
Fariborz Jahanian32019832010-07-23 19:11:11 +00003798
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003799 // Load the offsets of the declarations that Sema references.
3800 // They will be lazily deserialized when needed.
3801 if (!SemaDeclRefs.empty()) {
3802 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
3803 SemaObj->StdNamespace = SemaDeclRefs[0];
3804 SemaObj->StdBadAlloc = SemaDeclRefs[1];
3805 }
3806
Sebastian Redlc3632732010-10-05 15:59:54 +00003807 for (PerFileData *F = FirstInSource; F; F = F->NextInSource) {
3808
3809 // If there are @selector references added them to its pool. This is for
3810 // implementation of -Wselector.
3811 if (!F->ReferencedSelectorsData.empty()) {
3812 unsigned int DataSize = F->ReferencedSelectorsData.size()-1;
3813 unsigned I = 0;
3814 while (I < DataSize) {
3815 Selector Sel = DecodeSelector(F->ReferencedSelectorsData[I++]);
3816 SourceLocation SelLoc = ReadSourceLocation(
3817 *F, F->ReferencedSelectorsData, I);
3818 SemaObj->ReferencedSelectors.insert(std::make_pair(Sel, SelLoc));
3819 }
3820 }
3821
3822 // If there were any pending implicit instantiations, deserialize them
3823 // and add them to Sema's queue of such instantiations.
3824 assert(F->PendingInstantiations.size() % 2 == 0 &&
3825 "Expected pairs of entries");
3826 for (unsigned Idx = 0, N = F->PendingInstantiations.size(); Idx < N;) {
3827 ValueDecl *D=cast<ValueDecl>(GetDecl(F->PendingInstantiations[Idx++]));
3828 SourceLocation Loc = ReadSourceLocation(*F, F->PendingInstantiations,Idx);
3829 SemaObj->PendingInstantiations.push_back(std::make_pair(D, Loc));
3830 }
3831 }
3832
3833 // The two special data sets below always come from the most recent PCH,
3834 // which is at the front of the chain.
3835 PerFileData &F = *Chain.front();
3836
3837 // If there were any weak undeclared identifiers, deserialize them and add to
3838 // Sema's list of weak undeclared identifiers.
3839 if (!WeakUndeclaredIdentifiers.empty()) {
3840 unsigned Idx = 0;
3841 for (unsigned I = 0, N = WeakUndeclaredIdentifiers[Idx++]; I != N; ++I) {
3842 IdentifierInfo *WeakId = GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
3843 IdentifierInfo *AliasId= GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
3844 SourceLocation Loc = ReadSourceLocation(F, WeakUndeclaredIdentifiers,Idx);
3845 bool Used = WeakUndeclaredIdentifiers[Idx++];
3846 Sema::WeakInfo WI(AliasId, Loc);
3847 WI.setUsed(Used);
3848 SemaObj->WeakUndeclaredIdentifiers.insert(std::make_pair(WeakId, WI));
3849 }
3850 }
3851
3852 // If there were any VTable uses, deserialize the information and add it
3853 // to Sema's vector and map of VTable uses.
3854 if (!VTableUses.empty()) {
3855 unsigned Idx = 0;
3856 for (unsigned I = 0, N = VTableUses[Idx++]; I != N; ++I) {
3857 CXXRecordDecl *Class = cast<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
3858 SourceLocation Loc = ReadSourceLocation(F, VTableUses, Idx);
3859 bool DefinitionRequired = VTableUses[Idx++];
3860 SemaObj->VTableUses.push_back(std::make_pair(Class, Loc));
3861 SemaObj->VTablesUsed[Class] = DefinitionRequired;
Fariborz Jahanian32019832010-07-23 19:11:11 +00003862 }
3863 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00003864}
3865
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003866IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Sebastian Redld8c5abb2010-08-02 18:30:12 +00003867 // Try to find this name within our on-disk hash tables. We start with the
3868 // most recent one, since that one contains the most up-to-date info.
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003869 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003870 ASTIdentifierLookupTable *IdTable
3871 = (ASTIdentifierLookupTable *)Chain[I]->IdentifierLookupTable;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003872 if (!IdTable)
3873 continue;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003874 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003875 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003876 if (Pos == IdTable->end())
3877 continue;
Douglas Gregor668c1a42009-04-21 22:25:48 +00003878
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003879 // Dereferencing the iterator has the effect of building the
3880 // IdentifierInfo node and populating it with the various
3881 // declarations it needs.
Sebastian Redld8c5abb2010-08-02 18:30:12 +00003882 return *Pos;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003883 }
Sebastian Redld8c5abb2010-08-02 18:30:12 +00003884 return 0;
Douglas Gregor668c1a42009-04-21 22:25:48 +00003885}
3886
Douglas Gregor95f42922010-10-14 22:11:03 +00003887namespace clang {
3888 /// \brief An identifier-lookup iterator that enumerates all of the
3889 /// identifiers stored within a set of AST files.
3890 class ASTIdentifierIterator : public IdentifierIterator {
3891 /// \brief The AST reader whose identifiers are being enumerated.
3892 const ASTReader &Reader;
3893
3894 /// \brief The current index into the chain of AST files stored in
3895 /// the AST reader.
3896 unsigned Index;
3897
3898 /// \brief The current position within the identifier lookup table
3899 /// of the current AST file.
3900 ASTIdentifierLookupTable::key_iterator Current;
3901
3902 /// \brief The end position within the identifier lookup table of
3903 /// the current AST file.
3904 ASTIdentifierLookupTable::key_iterator End;
3905
3906 public:
3907 explicit ASTIdentifierIterator(const ASTReader &Reader);
3908
3909 virtual llvm::StringRef Next();
3910 };
3911}
3912
3913ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
3914 : Reader(Reader), Index(Reader.Chain.size() - 1) {
3915 ASTIdentifierLookupTable *IdTable
3916 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
3917 Current = IdTable->key_begin();
3918 End = IdTable->key_end();
3919}
3920
3921llvm::StringRef ASTIdentifierIterator::Next() {
3922 while (Current == End) {
3923 // If we have exhausted all of our AST files, we're done.
3924 if (Index == 0)
3925 return llvm::StringRef();
3926
3927 --Index;
3928 ASTIdentifierLookupTable *IdTable
3929 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
3930 Current = IdTable->key_begin();
3931 End = IdTable->key_end();
3932 }
3933
3934 // We have any identifiers remaining in the current AST file; return
3935 // the next one.
3936 std::pair<const char*, unsigned> Key = *Current;
3937 ++Current;
3938 return llvm::StringRef(Key.first, Key.second);
3939}
3940
3941IdentifierIterator *ASTReader::getIdentifiers() const {
3942 return new ASTIdentifierIterator(*this);
3943}
3944
Mike Stump1eb44332009-09-09 15:08:12 +00003945std::pair<ObjCMethodList, ObjCMethodList>
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003946ASTReader::ReadMethodPool(Selector Sel) {
Sebastian Redl725cd962010-08-04 20:40:17 +00003947 // Find this selector in a hash table. We want to find the most recent entry.
3948 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3949 PerFileData &F = *Chain[I];
3950 if (!F.SelectorLookupTable)
3951 continue;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003952
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003953 ASTSelectorLookupTable *PoolTable
3954 = (ASTSelectorLookupTable*)F.SelectorLookupTable;
3955 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
Sebastian Redl725cd962010-08-04 20:40:17 +00003956 if (Pos != PoolTable->end()) {
3957 ++NumSelectorsRead;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003958 // FIXME: Not quite happy with the statistics here. We probably should
3959 // disable this tracking when called via LoadSelector.
3960 // Also, should entries without methods count as misses?
3961 ++NumMethodPoolEntriesRead;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003962 ASTSelectorLookupTrait::data_type Data = *Pos;
Sebastian Redl725cd962010-08-04 20:40:17 +00003963 if (DeserializationListener)
3964 DeserializationListener->SelectorRead(Data.ID, Sel);
3965 return std::make_pair(Data.Instance, Data.Factory);
3966 }
Douglas Gregor83941df2009-04-25 17:48:32 +00003967 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003968
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003969 ++NumMethodPoolMisses;
Sebastian Redl725cd962010-08-04 20:40:17 +00003970 return std::pair<ObjCMethodList, ObjCMethodList>();
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003971}
3972
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003973void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redle58aa892010-08-04 18:21:41 +00003974 // It would be complicated to avoid reading the methods anyway. So don't.
3975 ReadMethodPool(Sel);
3976}
3977
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003978void ASTReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00003979 assert(ID && "Non-zero identifier ID required");
Douglas Gregora02b1472009-04-28 21:53:25 +00003980 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003981 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003982 if (DeserializationListener)
3983 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregor668c1a42009-04-21 22:25:48 +00003984}
3985
Douglas Gregord89275b2009-07-06 18:54:52 +00003986/// \brief Set the globally-visible declarations associated with the given
3987/// identifier.
3988///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003989/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump1eb44332009-09-09 15:08:12 +00003990/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregord89275b2009-07-06 18:54:52 +00003991/// them.
3992///
3993/// \param II an IdentifierInfo that refers to one or more globally-visible
3994/// declarations.
3995///
3996/// \param DeclIDs the set of declaration IDs with the name @p II that are
3997/// visible at global scope.
3998///
3999/// \param Nonrecursive should be true to indicate that the caller knows that
4000/// this call is non-recursive, and therefore the globally-visible declarations
4001/// will not be placed onto the pending queue.
Mike Stump1eb44332009-09-09 15:08:12 +00004002void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004003ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Douglas Gregord89275b2009-07-06 18:54:52 +00004004 const llvm::SmallVectorImpl<uint32_t> &DeclIDs,
4005 bool Nonrecursive) {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004006 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregord89275b2009-07-06 18:54:52 +00004007 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
4008 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
4009 PII.II = II;
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00004010 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregord89275b2009-07-06 18:54:52 +00004011 return;
4012 }
Mike Stump1eb44332009-09-09 15:08:12 +00004013
Douglas Gregord89275b2009-07-06 18:54:52 +00004014 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
4015 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
4016 if (SemaObj) {
Douglas Gregor914ed9d2010-08-13 03:15:25 +00004017 if (SemaObj->TUScope) {
4018 // Introduce this declaration into the translation-unit scope
4019 // and add it to the declaration chain for this identifier, so
4020 // that (unqualified) name lookup will find it.
John McCalld226f652010-08-21 09:40:31 +00004021 SemaObj->TUScope->AddDecl(D);
Douglas Gregor914ed9d2010-08-13 03:15:25 +00004022 }
Douglas Gregor76dc8892010-09-24 23:29:12 +00004023 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
Douglas Gregord89275b2009-07-06 18:54:52 +00004024 } else {
4025 // Queue this declaration so that it will be added to the
4026 // translation unit scope and identifier's declaration chain
4027 // once a Sema object is known.
4028 PreloadedDecls.push_back(D);
4029 }
4030 }
4031}
4032
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004033IdentifierInfo *ASTReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00004034 if (ID == 0)
4035 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00004036
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00004037 if (IdentifiersLoaded.empty()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004038 Error("no identifier table in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00004039 return 0;
4040 }
Mike Stump1eb44332009-09-09 15:08:12 +00004041
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00004042 assert(PP && "Forgot to set Preprocessor ?");
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00004043 ID -= 1;
4044 if (!IdentifiersLoaded[ID]) {
4045 unsigned Index = ID;
4046 const char *Str = 0;
4047 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4048 PerFileData *F = Chain[N - I - 1];
4049 if (Index < F->LocalNumIdentifiers) {
4050 uint32_t Offset = F->IdentifierOffsets[Index];
4051 Str = F->IdentifierTableData + Offset;
4052 break;
4053 }
4054 Index -= F->LocalNumIdentifiers;
4055 }
4056 assert(Str && "Broken Chain");
Douglas Gregord6595a42009-04-25 21:04:17 +00004057
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004058 // All of the strings in the AST file are preceded by a 16-bit length.
4059 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenek231bc0b2009-10-23 04:45:31 +00004060 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
4061 // unsigned integers. This is important to avoid integer overflow when
4062 // we cast them to 'unsigned'.
Ted Kremenekff1ea462009-10-23 03:57:22 +00004063 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregor02fc7512009-04-28 20:01:51 +00004064 unsigned StrLen = (((unsigned) StrLenPtr[0])
4065 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00004066 IdentifiersLoaded[ID]
Kovarththanan Rajaratnam811f4262010-03-12 10:32:27 +00004067 = &PP->getIdentifierTable().get(Str, StrLen);
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004068 if (DeserializationListener)
4069 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregorafaf3082009-04-11 00:14:32 +00004070 }
Mike Stump1eb44332009-09-09 15:08:12 +00004071
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00004072 return IdentifiersLoaded[ID];
Douglas Gregor2cf26342009-04-09 22:27:44 +00004073}
4074
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004075void ASTReader::ReadSLocEntry(unsigned ID) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00004076 ReadSLocEntryRecord(ID);
4077}
4078
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004079Selector ASTReader::DecodeSelector(unsigned ID) {
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004080 if (ID == 0)
4081 return Selector();
Mike Stump1eb44332009-09-09 15:08:12 +00004082
Sebastian Redl725cd962010-08-04 20:40:17 +00004083 if (ID > SelectorsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004084 Error("selector ID out of range in AST file");
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004085 return Selector();
4086 }
Douglas Gregor83941df2009-04-25 17:48:32 +00004087
Sebastian Redl725cd962010-08-04 20:40:17 +00004088 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor83941df2009-04-25 17:48:32 +00004089 // Load this selector from the selector table.
Sebastian Redl725cd962010-08-04 20:40:17 +00004090 unsigned Idx = ID - 1;
4091 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4092 PerFileData &F = *Chain[N - I - 1];
4093 if (Idx < F.LocalNumSelectors) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004094 ASTSelectorLookupTrait Trait(*this);
Sebastian Redl725cd962010-08-04 20:40:17 +00004095 SelectorsLoaded[ID - 1] =
4096 Trait.ReadKey(F.SelectorLookupTableData + F.SelectorOffsets[Idx], 0);
4097 if (DeserializationListener)
4098 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
4099 break;
4100 }
4101 Idx -= F.LocalNumSelectors;
4102 }
Douglas Gregor83941df2009-04-25 17:48:32 +00004103 }
4104
Sebastian Redl725cd962010-08-04 20:40:17 +00004105 return SelectorsLoaded[ID - 1];
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004106}
4107
Michael J. Spencer20249a12010-10-21 03:16:25 +00004108Selector ASTReader::GetExternalSelector(uint32_t ID) {
Douglas Gregor719770d2010-04-06 17:30:22 +00004109 return DecodeSelector(ID);
4110}
4111
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004112uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redl725cd962010-08-04 20:40:17 +00004113 // ID 0 (the null selector) is considered an external selector.
4114 return getTotalNumSelectors() + 1;
Douglas Gregor719770d2010-04-06 17:30:22 +00004115}
4116
Mike Stump1eb44332009-09-09 15:08:12 +00004117DeclarationName
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004118ASTReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00004119 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
4120 switch (Kind) {
4121 case DeclarationName::Identifier:
4122 return DeclarationName(GetIdentifierInfo(Record, Idx));
4123
4124 case DeclarationName::ObjCZeroArgSelector:
4125 case DeclarationName::ObjCOneArgSelector:
4126 case DeclarationName::ObjCMultiArgSelector:
Steve Naroffa7503a72009-04-23 15:15:40 +00004127 return DeclarationName(GetSelector(Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004128
4129 case DeclarationName::CXXConstructorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00004130 return Context->DeclarationNames.getCXXConstructorName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00004131 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004132
4133 case DeclarationName::CXXDestructorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00004134 return Context->DeclarationNames.getCXXDestructorName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00004135 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004136
4137 case DeclarationName::CXXConversionFunctionName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00004138 return Context->DeclarationNames.getCXXConversionFunctionName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00004139 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004140
4141 case DeclarationName::CXXOperatorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00004142 return Context->DeclarationNames.getCXXOperatorName(
Douglas Gregor2cf26342009-04-09 22:27:44 +00004143 (OverloadedOperatorKind)Record[Idx++]);
4144
Sean Hunt3e518bd2009-11-29 07:34:05 +00004145 case DeclarationName::CXXLiteralOperatorName:
4146 return Context->DeclarationNames.getCXXLiteralOperatorName(
4147 GetIdentifierInfo(Record, Idx));
4148
Douglas Gregor2cf26342009-04-09 22:27:44 +00004149 case DeclarationName::CXXUsingDirective:
4150 return DeclarationName::getUsingDirectiveName();
4151 }
4152
4153 // Required to silence GCC warning
4154 return DeclarationName();
4155}
Douglas Gregor0a0428e2009-04-10 20:39:37 +00004156
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004157void ASTReader::ReadDeclarationNameLoc(PerFileData &F,
4158 DeclarationNameLoc &DNLoc,
4159 DeclarationName Name,
4160 const RecordData &Record, unsigned &Idx) {
4161 switch (Name.getNameKind()) {
4162 case DeclarationName::CXXConstructorName:
4163 case DeclarationName::CXXDestructorName:
4164 case DeclarationName::CXXConversionFunctionName:
4165 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
4166 break;
4167
4168 case DeclarationName::CXXOperatorName:
4169 DNLoc.CXXOperatorName.BeginOpNameLoc
4170 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4171 DNLoc.CXXOperatorName.EndOpNameLoc
4172 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4173 break;
4174
4175 case DeclarationName::CXXLiteralOperatorName:
4176 DNLoc.CXXLiteralOperatorName.OpNameLoc
4177 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4178 break;
4179
4180 case DeclarationName::Identifier:
4181 case DeclarationName::ObjCZeroArgSelector:
4182 case DeclarationName::ObjCOneArgSelector:
4183 case DeclarationName::ObjCMultiArgSelector:
4184 case DeclarationName::CXXUsingDirective:
4185 break;
4186 }
4187}
4188
4189void ASTReader::ReadDeclarationNameInfo(PerFileData &F,
4190 DeclarationNameInfo &NameInfo,
4191 const RecordData &Record, unsigned &Idx) {
4192 NameInfo.setName(ReadDeclarationName(Record, Idx));
4193 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
4194 DeclarationNameLoc DNLoc;
4195 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
4196 NameInfo.setInfo(DNLoc);
4197}
4198
4199void ASTReader::ReadQualifierInfo(PerFileData &F, QualifierInfo &Info,
4200 const RecordData &Record, unsigned &Idx) {
4201 Info.NNS = ReadNestedNameSpecifier(Record, Idx);
4202 Info.NNSRange = ReadSourceRange(F, Record, Idx);
4203 unsigned NumTPLists = Record[Idx++];
4204 Info.NumTemplParamLists = NumTPLists;
4205 if (NumTPLists) {
4206 Info.TemplParamLists = new (*Context) TemplateParameterList*[NumTPLists];
4207 for (unsigned i=0; i != NumTPLists; ++i)
4208 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
4209 }
4210}
4211
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004212TemplateName
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004213ASTReader::ReadTemplateName(const RecordData &Record, unsigned &Idx) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00004214 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004215 switch (Kind) {
4216 case TemplateName::Template:
4217 return TemplateName(cast_or_null<TemplateDecl>(GetDecl(Record[Idx++])));
4218
4219 case TemplateName::OverloadedTemplate: {
4220 unsigned size = Record[Idx++];
4221 UnresolvedSet<8> Decls;
4222 while (size--)
4223 Decls.addDecl(cast<NamedDecl>(GetDecl(Record[Idx++])));
4224
4225 return Context->getOverloadedTemplateName(Decls.begin(), Decls.end());
4226 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004227
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004228 case TemplateName::QualifiedTemplate: {
4229 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
4230 bool hasTemplKeyword = Record[Idx++];
4231 TemplateDecl *Template = cast<TemplateDecl>(GetDecl(Record[Idx++]));
4232 return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
4233 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004234
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004235 case TemplateName::DependentTemplate: {
4236 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
4237 if (Record[Idx++]) // isIdentifier
4238 return Context->getDependentTemplateName(NNS,
4239 GetIdentifierInfo(Record, Idx));
4240 return Context->getDependentTemplateName(NNS,
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004241 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004242 }
4243 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004244
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004245 assert(0 && "Unhandled template name kind!");
4246 return TemplateName();
4247}
4248
4249TemplateArgument
Sebastian Redlc3632732010-10-05 15:59:54 +00004250ASTReader::ReadTemplateArgument(PerFileData &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00004251 const RecordData &Record, unsigned &Idx) {
Douglas Gregora7fc9012011-01-05 18:58:31 +00004252 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
4253 switch (Kind) {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004254 case TemplateArgument::Null:
4255 return TemplateArgument();
4256 case TemplateArgument::Type:
4257 return TemplateArgument(GetType(Record[Idx++]));
4258 case TemplateArgument::Declaration:
4259 return TemplateArgument(GetDecl(Record[Idx++]));
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00004260 case TemplateArgument::Integral: {
4261 llvm::APSInt Value = ReadAPSInt(Record, Idx);
4262 QualType T = GetType(Record[Idx++]);
4263 return TemplateArgument(Value, T);
4264 }
Douglas Gregora7fc9012011-01-05 18:58:31 +00004265 case TemplateArgument::Template:
4266 case TemplateArgument::TemplateExpansion: {
Douglas Gregorba68eca2011-01-05 17:40:24 +00004267 TemplateName Name = ReadTemplateName(Record, Idx);
Douglas Gregora7fc9012011-01-05 18:58:31 +00004268 return TemplateArgument(Name, Kind == TemplateArgument::TemplateExpansion);
Douglas Gregorba68eca2011-01-05 17:40:24 +00004269 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004270 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00004271 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004272 case TemplateArgument::Pack: {
4273 unsigned NumArgs = Record[Idx++];
Douglas Gregor910f8002010-11-07 23:05:16 +00004274 TemplateArgument *Args = new (*Context) TemplateArgument[NumArgs];
4275 for (unsigned I = 0; I != NumArgs; ++I)
4276 Args[I] = ReadTemplateArgument(F, Record, Idx);
4277 return TemplateArgument(Args, NumArgs);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004278 }
4279 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004280
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004281 assert(0 && "Unhandled template argument kind!");
4282 return TemplateArgument();
4283}
4284
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004285TemplateParameterList *
Sebastian Redlc3632732010-10-05 15:59:54 +00004286ASTReader::ReadTemplateParameterList(PerFileData &F,
4287 const RecordData &Record, unsigned &Idx) {
4288 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
4289 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
4290 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004291
4292 unsigned NumParams = Record[Idx++];
4293 llvm::SmallVector<NamedDecl *, 16> Params;
4294 Params.reserve(NumParams);
4295 while (NumParams--)
4296 Params.push_back(cast<NamedDecl>(GetDecl(Record[Idx++])));
Michael J. Spencer20249a12010-10-21 03:16:25 +00004297
4298 TemplateParameterList* TemplateParams =
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004299 TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc,
4300 Params.data(), Params.size(), RAngleLoc);
4301 return TemplateParams;
4302}
4303
4304void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004305ASTReader::
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004306ReadTemplateArgumentList(llvm::SmallVector<TemplateArgument, 8> &TemplArgs,
Sebastian Redlc3632732010-10-05 15:59:54 +00004307 PerFileData &F, const RecordData &Record,
4308 unsigned &Idx) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004309 unsigned NumTemplateArgs = Record[Idx++];
4310 TemplArgs.reserve(NumTemplateArgs);
4311 while (NumTemplateArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00004312 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004313}
4314
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004315/// \brief Read a UnresolvedSet structure.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004316void ASTReader::ReadUnresolvedSet(UnresolvedSetImpl &Set,
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004317 const RecordData &Record, unsigned &Idx) {
4318 unsigned NumDecls = Record[Idx++];
4319 while (NumDecls--) {
4320 NamedDecl *D = cast<NamedDecl>(GetDecl(Record[Idx++]));
4321 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
4322 Set.addDecl(D, AS);
4323 }
4324}
4325
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004326CXXBaseSpecifier
Sebastian Redlc3632732010-10-05 15:59:54 +00004327ASTReader::ReadCXXBaseSpecifier(PerFileData &F,
Nick Lewycky56062202010-07-26 16:56:01 +00004328 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004329 bool isVirtual = static_cast<bool>(Record[Idx++]);
4330 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
4331 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00004332 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
4333 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00004334 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
4335 return CXXBaseSpecifier(Range, isVirtual, isBaseOfClass, AS, TInfo,
4336 EllipsisLoc);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004337}
4338
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004339std::pair<CXXBaseOrMemberInitializer **, unsigned>
Sebastian Redlc3632732010-10-05 15:59:54 +00004340ASTReader::ReadCXXBaseOrMemberInitializers(PerFileData &F,
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004341 const RecordData &Record,
4342 unsigned &Idx) {
4343 CXXBaseOrMemberInitializer **BaseOrMemberInitializers = 0;
4344 unsigned NumInitializers = Record[Idx++];
4345 if (NumInitializers) {
4346 ASTContext &C = *getContext();
4347
4348 BaseOrMemberInitializers
4349 = new (C) CXXBaseOrMemberInitializer*[NumInitializers];
4350 for (unsigned i=0; i != NumInitializers; ++i) {
4351 TypeSourceInfo *BaseClassInfo = 0;
4352 bool IsBaseVirtual = false;
4353 FieldDecl *Member = 0;
Francois Pichet00eb3f92010-12-04 09:14:42 +00004354 IndirectFieldDecl *IndirectMember = 0;
Michael J. Spencer20249a12010-10-21 03:16:25 +00004355
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004356 bool IsBaseInitializer = Record[Idx++];
4357 if (IsBaseInitializer) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004358 BaseClassInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004359 IsBaseVirtual = Record[Idx++];
4360 } else {
Francois Pichet00eb3f92010-12-04 09:14:42 +00004361 bool IsIndirectMemberInitializer = Record[Idx++];
4362 if (IsIndirectMemberInitializer)
4363 IndirectMember = cast<IndirectFieldDecl>(GetDecl(Record[Idx++]));
4364 else
4365 Member = cast<FieldDecl>(GetDecl(Record[Idx++]));
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004366 }
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00004367 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redlc3632732010-10-05 15:59:54 +00004368 Expr *Init = ReadExpr(F);
Sebastian Redlc3632732010-10-05 15:59:54 +00004369 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
4370 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004371 bool IsWritten = Record[Idx++];
4372 unsigned SourceOrderOrNumArrayIndices;
4373 llvm::SmallVector<VarDecl *, 8> Indices;
4374 if (IsWritten) {
4375 SourceOrderOrNumArrayIndices = Record[Idx++];
4376 } else {
4377 SourceOrderOrNumArrayIndices = Record[Idx++];
4378 Indices.reserve(SourceOrderOrNumArrayIndices);
4379 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
4380 Indices.push_back(cast<VarDecl>(GetDecl(Record[Idx++])));
4381 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004382
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004383 CXXBaseOrMemberInitializer *BOMInit;
4384 if (IsBaseInitializer) {
4385 BOMInit = new (C) CXXBaseOrMemberInitializer(C, BaseClassInfo,
4386 IsBaseVirtual, LParenLoc,
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00004387 Init, RParenLoc,
4388 MemberOrEllipsisLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004389 } else if (IsWritten) {
Francois Pichet00eb3f92010-12-04 09:14:42 +00004390 if (Member)
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00004391 BOMInit = new (C) CXXBaseOrMemberInitializer(C, Member,
4392 MemberOrEllipsisLoc,
Francois Pichet00eb3f92010-12-04 09:14:42 +00004393 LParenLoc, Init,
4394 RParenLoc);
4395 else
4396 BOMInit = new (C) CXXBaseOrMemberInitializer(C, IndirectMember,
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00004397 MemberOrEllipsisLoc,
4398 LParenLoc,
Francois Pichet00eb3f92010-12-04 09:14:42 +00004399 Init, RParenLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004400 } else {
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00004401 BOMInit = CXXBaseOrMemberInitializer::Create(C, Member,
4402 MemberOrEllipsisLoc,
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004403 LParenLoc, Init, RParenLoc,
4404 Indices.data(),
4405 Indices.size());
4406 }
4407
Argyrios Kyrtzidisf84cde12010-09-06 19:04:27 +00004408 if (IsWritten)
4409 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004410 BaseOrMemberInitializers[i] = BOMInit;
4411 }
4412 }
4413
4414 return std::make_pair(BaseOrMemberInitializers, NumInitializers);
4415}
4416
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004417NestedNameSpecifier *
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004418ASTReader::ReadNestedNameSpecifier(const RecordData &Record, unsigned &Idx) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004419 unsigned N = Record[Idx++];
4420 NestedNameSpecifier *NNS = 0, *Prev = 0;
4421 for (unsigned I = 0; I != N; ++I) {
4422 NestedNameSpecifier::SpecifierKind Kind
4423 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
4424 switch (Kind) {
4425 case NestedNameSpecifier::Identifier: {
4426 IdentifierInfo *II = GetIdentifierInfo(Record, Idx);
4427 NNS = NestedNameSpecifier::Create(*Context, Prev, II);
4428 break;
4429 }
4430
4431 case NestedNameSpecifier::Namespace: {
4432 NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++]));
4433 NNS = NestedNameSpecifier::Create(*Context, Prev, NS);
4434 break;
4435 }
4436
4437 case NestedNameSpecifier::TypeSpec:
4438 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregor1ab55e92010-12-10 17:03:06 +00004439 Type *T = GetType(Record[Idx++]).getTypePtrOrNull();
4440 if (!T)
4441 return 0;
4442
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004443 bool Template = Record[Idx++];
4444 NNS = NestedNameSpecifier::Create(*Context, Prev, Template, T);
4445 break;
4446 }
4447
4448 case NestedNameSpecifier::Global: {
4449 NNS = NestedNameSpecifier::GlobalSpecifier(*Context);
4450 // No associated value, and there can't be a prefix.
4451 break;
4452 }
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004453 }
Argyrios Kyrtzidisd2bb2c02010-07-07 15:46:30 +00004454 Prev = NNS;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004455 }
4456 return NNS;
4457}
4458
4459SourceRange
Sebastian Redlc3632732010-10-05 15:59:54 +00004460ASTReader::ReadSourceRange(PerFileData &F, const RecordData &Record,
4461 unsigned &Idx) {
4462 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
4463 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar8ee59392010-06-02 15:47:10 +00004464 return SourceRange(beg, end);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004465}
4466
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004467/// \brief Read an integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004468llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004469 unsigned BitWidth = Record[Idx++];
4470 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
4471 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
4472 Idx += NumWords;
4473 return Result;
4474}
4475
4476/// \brief Read a signed integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004477llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004478 bool isUnsigned = Record[Idx++];
4479 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
4480}
4481
Douglas Gregor17fc2232009-04-14 21:55:33 +00004482/// \brief Read a floating-point value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004483llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00004484 return llvm::APFloat(ReadAPInt(Record, Idx));
4485}
4486
Douglas Gregor68a2eb02009-04-15 21:30:51 +00004487// \brief Read a string
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004488std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00004489 unsigned Len = Record[Idx++];
Jay Foadbeaaccd2009-05-21 09:52:38 +00004490 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00004491 Idx += Len;
4492 return Result;
4493}
4494
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004495CXXTemporary *ASTReader::ReadCXXTemporary(const RecordData &Record,
Chris Lattnerd2598362010-05-10 00:25:06 +00004496 unsigned &Idx) {
4497 CXXDestructorDecl *Decl = cast<CXXDestructorDecl>(GetDecl(Record[Idx++]));
4498 return CXXTemporary::Create(*Context, Decl);
4499}
4500
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004501DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00004502 return Diag(SourceLocation(), DiagID);
4503}
4504
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004505DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004506 return Diags.Report(Loc, DiagID);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00004507}
Douglas Gregor025452f2009-04-17 00:04:06 +00004508
Douglas Gregor668c1a42009-04-21 22:25:48 +00004509/// \brief Retrieve the identifier table associated with the
4510/// preprocessor.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004511IdentifierTable &ASTReader::getIdentifierTable() {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00004512 assert(PP && "Forgot to set Preprocessor ?");
4513 return PP->getIdentifierTable();
Douglas Gregor668c1a42009-04-21 22:25:48 +00004514}
4515
Douglas Gregor025452f2009-04-17 00:04:06 +00004516/// \brief Record that the given ID maps to the given switch-case
4517/// statement.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004518void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Douglas Gregor025452f2009-04-17 00:04:06 +00004519 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
4520 SwitchCaseStmts[ID] = SC;
4521}
4522
4523/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004524SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Douglas Gregor025452f2009-04-17 00:04:06 +00004525 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
4526 return SwitchCaseStmts[ID];
4527}
Douglas Gregor1de05fe2009-04-17 18:18:49 +00004528
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00004529void ASTReader::ClearSwitchCaseIDs() {
4530 SwitchCaseStmts.clear();
4531}
4532
Douglas Gregor1de05fe2009-04-17 18:18:49 +00004533/// \brief Record that the given label statement has been
4534/// deserialized and has the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004535void ASTReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
Mike Stump1eb44332009-09-09 15:08:12 +00004536 assert(LabelStmts.find(ID) == LabelStmts.end() &&
Douglas Gregor1de05fe2009-04-17 18:18:49 +00004537 "Deserialized label twice");
4538 LabelStmts[ID] = S;
4539
4540 // If we've already seen any goto statements that point to this
4541 // label, resolve them now.
4542 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
4543 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
4544 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
4545 Goto->second->setLabel(S);
4546 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00004547
4548 // If we've already seen any address-label statements that point to
4549 // this label, resolve them now.
4550 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
Mike Stump1eb44332009-09-09 15:08:12 +00004551 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00004552 = UnresolvedAddrLabelExprs.equal_range(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00004553 for (AddrLabelIter AddrLabel = AddrLabels.first;
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00004554 AddrLabel != AddrLabels.second; ++AddrLabel)
4555 AddrLabel->second->setLabel(S);
4556 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor1de05fe2009-04-17 18:18:49 +00004557}
4558
4559/// \brief Set the label of the given statement to the label
4560/// identified by ID.
4561///
4562/// Depending on the order in which the label and other statements
4563/// referencing that label occur, this operation may complete
4564/// immediately (updating the statement) or it may queue the
4565/// statement to be back-patched later.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004566void ASTReader::SetLabelOf(GotoStmt *S, unsigned ID) {
Douglas Gregor1de05fe2009-04-17 18:18:49 +00004567 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
4568 if (Label != LabelStmts.end()) {
4569 // We've already seen this label, so set the label of the goto and
4570 // we're done.
4571 S->setLabel(Label->second);
4572 } else {
4573 // We haven't seen this label yet, so add this goto to the set of
4574 // unresolved goto statements.
4575 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
4576 }
4577}
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00004578
4579/// \brief Set the label of the given expression to the label
4580/// identified by ID.
4581///
4582/// Depending on the order in which the label and other statements
4583/// referencing that label occur, this operation may complete
4584/// immediately (updating the statement) or it may queue the
4585/// statement to be back-patched later.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004586void ASTReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00004587 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
4588 if (Label != LabelStmts.end()) {
4589 // We've already seen this label, so set the label of the
4590 // label-address expression and we're done.
4591 S->setLabel(Label->second);
4592 } else {
4593 // We haven't seen this label yet, so add this label-address
4594 // expression to the set of unresolved label-address expressions.
4595 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
4596 }
4597}
Douglas Gregord89275b2009-07-06 18:54:52 +00004598
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004599void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004600 assert(NumCurrentElementsDeserializing &&
4601 "FinishedDeserializing not paired with StartedDeserializing");
4602 if (NumCurrentElementsDeserializing == 1) {
Douglas Gregord89275b2009-07-06 18:54:52 +00004603 // If any identifiers with corresponding top-level declarations have
4604 // been loaded, load those declarations now.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004605 while (!PendingIdentifierInfos.empty()) {
4606 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
4607 PendingIdentifierInfos.front().DeclIDs, true);
4608 PendingIdentifierInfos.pop_front();
Douglas Gregord89275b2009-07-06 18:54:52 +00004609 }
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00004610
4611 // We are not in recursive loading, so it's safe to pass the "interesting"
4612 // decls to the consumer.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004613 if (Consumer)
4614 PassInterestingDeclsToConsumer();
Argyrios Kyrtzidis134db1f2010-10-24 17:26:31 +00004615
4616 assert(PendingForwardRefs.size() == 0 &&
4617 "Some forward refs did not get linked to the definition!");
Douglas Gregord89275b2009-07-06 18:54:52 +00004618 }
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004619 --NumCurrentElementsDeserializing;
Douglas Gregord89275b2009-07-06 18:54:52 +00004620}
Douglas Gregor501c1032010-08-19 00:28:17 +00004621
Sebastian Redle1dde812010-08-24 00:50:04 +00004622ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context,
4623 const char *isysroot, bool DisableValidation)
4624 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
4625 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
4626 Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context),
4627 Consumer(0), isysroot(isysroot), DisableValidation(DisableValidation),
4628 NumStatHits(0), NumStatMisses(0), NumSLocEntriesRead(0),
Sebastian Redl8db9fae2010-09-22 20:19:08 +00004629 TotalNumSLocEntries(0), NextSLocOffset(0), NumStatementsRead(0),
4630 TotalNumStatements(0), NumMacrosRead(0), TotalNumMacros(0),
4631 NumSelectorsRead(0), NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
Sebastian Redle1dde812010-08-24 00:50:04 +00004632 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
4633 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
4634 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
4635 RelocatablePCH = false;
4636}
4637
4638ASTReader::ASTReader(SourceManager &SourceMgr, FileManager &FileMgr,
4639 Diagnostic &Diags, const char *isysroot,
4640 bool DisableValidation)
4641 : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr),
4642 Diags(Diags), SemaObj(0), PP(0), Context(0), Consumer(0),
4643 isysroot(isysroot), DisableValidation(DisableValidation), NumStatHits(0),
4644 NumStatMisses(0), NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Sebastian Redl8db9fae2010-09-22 20:19:08 +00004645 NextSLocOffset(0), NumStatementsRead(0), TotalNumStatements(0),
4646 NumMacrosRead(0), TotalNumMacros(0), NumSelectorsRead(0),
4647 NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
4648 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
4649 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
4650 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
Sebastian Redle1dde812010-08-24 00:50:04 +00004651 RelocatablePCH = false;
4652}
4653
4654ASTReader::~ASTReader() {
4655 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
4656 delete Chain[e - i - 1];
4657 // Delete all visible decl lookup tables
4658 for (DeclContextOffsetsMap::iterator I = DeclContextOffsets.begin(),
4659 E = DeclContextOffsets.end();
4660 I != E; ++I) {
4661 for (DeclContextInfos::iterator J = I->second.begin(), F = I->second.end();
4662 J != F; ++J) {
4663 if (J->NameLookupTableData)
4664 delete static_cast<ASTDeclContextNameLookupTable*>(
4665 J->NameLookupTableData);
4666 }
4667 }
4668 for (DeclContextVisibleUpdatesPending::iterator
4669 I = PendingVisibleUpdates.begin(),
4670 E = PendingVisibleUpdates.end();
4671 I != E; ++I) {
4672 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
4673 F = I->second.end();
4674 J != F; ++J)
4675 delete static_cast<ASTDeclContextNameLookupTable*>(*J);
4676 }
4677}
4678
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00004679ASTReader::PerFileData::PerFileData(ASTFileType Ty)
4680 : Type(Ty), SizeInBits(0), LocalNumSLocEntries(0), SLocOffsets(0), LocalSLocSize(0),
Sebastian Redl301c9b02010-09-22 00:42:27 +00004681 LocalNumIdentifiers(0), IdentifierOffsets(0), IdentifierTableData(0),
4682 IdentifierLookupTable(0), LocalNumMacroDefinitions(0),
4683 MacroDefinitionOffsets(0), LocalNumSelectors(0), SelectorOffsets(0),
4684 SelectorLookupTableData(0), SelectorLookupTable(0), LocalNumDecls(0),
Douglas Gregor7c789c12010-10-29 22:39:52 +00004685 DeclOffsets(0), LocalNumCXXBaseSpecifiers(0), CXXBaseSpecifiersOffsets(0),
4686 LocalNumTypes(0), TypeOffsets(0), StatCache(0),
Sebastian Redla866e652010-10-01 19:59:12 +00004687 NumPreallocatedPreprocessingEntities(0), NextInSource(0)
Douglas Gregor501c1032010-08-19 00:28:17 +00004688{}
4689
4690ASTReader::PerFileData::~PerFileData() {
4691 delete static_cast<ASTIdentifierLookupTable *>(IdentifierLookupTable);
4692 delete static_cast<ASTSelectorLookupTable *>(SelectorLookupTable);
4693}
4694