blob: fcf09b7bd566dee6d199297ed054e68880ca5c6c [file] [log] [blame]
Sebastian Redl904c9c82010-08-18 23:57:11 +00001//===--- ASTReader.cpp - AST File Reader ------------------------*- C++ -*-===//
Douglas Gregor2cf26342009-04-09 22:27:44 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Sebastian Redlc43b54c2010-08-18 23:56:43 +000010// This file defines the ASTReader class, which reads AST files.
Douglas Gregor2cf26342009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
Chris Lattner4c6f9522009-04-27 05:14:47 +000013
Sebastian Redl6ab7cd82010-08-18 23:57:17 +000014#include "clang/Serialization/ASTReader.h"
15#include "clang/Serialization/ASTDeserializationListener.h"
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +000016#include "ASTCommon.h"
Douglas Gregor0a0428e2009-04-10 20:39:37 +000017#include "clang/Frontend/FrontendDiagnostic.h"
Daniel Dunbarc7162932009-11-11 23:58:53 +000018#include "clang/Frontend/Utils.h"
Douglas Gregore737f502010-08-12 20:07:10 +000019#include "clang/Sema/Sema.h"
John McCall5f1e0942010-08-24 08:50:51 +000020#include "clang/Sema/Scope.h"
Douglas Gregorfdd01722009-04-14 00:24:19 +000021#include "clang/AST/ASTConsumer.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000022#include "clang/AST/ASTContext.h"
John McCall2a7fb272010-08-25 05:32:35 +000023#include "clang/AST/DeclTemplate.h"
Douglas Gregor0b748912009-04-14 21:18:50 +000024#include "clang/AST/Expr.h"
John McCall7a1fad32010-08-24 07:32:53 +000025#include "clang/AST/ExprCXX.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000026#include "clang/AST/Type.h"
John McCalla1ee0c52009-10-16 21:56:05 +000027#include "clang/AST/TypeLocVisitor.h"
Chris Lattner42d42b52009-04-10 21:41:48 +000028#include "clang/Lex/MacroInfo.h"
Douglas Gregor6a5a23f2010-03-19 21:51:54 +000029#include "clang/Lex/PreprocessingRecord.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000030#include "clang/Lex/Preprocessor.h"
Steve Naroff83d63c72009-04-24 20:03:17 +000031#include "clang/Lex/HeaderSearch.h"
Douglas Gregor668c1a42009-04-21 22:25:48 +000032#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000033#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000034#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000035#include "clang/Basic/FileManager.h"
Chris Lattner10e286a2010-11-23 19:19:34 +000036#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000037#include "clang/Basic/TargetInfo.h"
Douglas Gregor445e23e2009-10-05 21:07:28 +000038#include "clang/Basic/Version.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000039#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000040#include "llvm/Bitcode/BitstreamReader.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000041#include "llvm/Support/MemoryBuffer.h"
John McCall833ca992009-10-29 08:12:44 +000042#include "llvm/Support/ErrorHandling.h"
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000043#include "llvm/Support/FileSystem.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000044#include "llvm/Support/Path.h"
Michael J. Spencer3a321e22010-12-09 17:36:38 +000045#include "llvm/Support/system_error.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000046#include <algorithm>
Douglas Gregore721f952009-04-28 18:58:38 +000047#include <iterator>
Douglas Gregor2cf26342009-04-09 22:27:44 +000048#include <cstdio>
Douglas Gregor4fed3f42009-04-27 18:38:38 +000049#include <sys/stat.h>
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000050
Douglas Gregor2cf26342009-04-09 22:27:44 +000051using namespace clang;
Sebastian Redl8538e8d2010-08-18 23:57:32 +000052using namespace clang::serialization;
Douglas Gregor2cf26342009-04-09 22:27:44 +000053
54//===----------------------------------------------------------------------===//
Sebastian Redl3c7f4132010-08-18 23:57:06 +000055// PCH validator implementation
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000056//===----------------------------------------------------------------------===//
57
Sebastian Redl571db7f2010-08-18 23:56:56 +000058ASTReaderListener::~ASTReaderListener() {}
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000059
60bool
61PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts) {
62 const LangOptions &PPLangOpts = PP.getLangOptions();
63#define PARSE_LANGOPT_BENIGN(Option)
64#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
65 if (PPLangOpts.Option != LangOpts.Option) { \
66 Reader.Diag(DiagID) << LangOpts.Option << PPLangOpts.Option; \
67 return true; \
68 }
69
70 PARSE_LANGOPT_BENIGN(Trigraphs);
71 PARSE_LANGOPT_BENIGN(BCPLComment);
72 PARSE_LANGOPT_BENIGN(DollarIdents);
73 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
74 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
Chandler Carrutheb5d7b72010-04-17 20:17:31 +000075 PARSE_LANGOPT_IMPORTANT(GNUKeywords, diag::warn_pch_gnu_keywords);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000076 PARSE_LANGOPT_BENIGN(ImplicitInt);
77 PARSE_LANGOPT_BENIGN(Digraphs);
78 PARSE_LANGOPT_BENIGN(HexFloats);
79 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
80 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
Michael J. Spencerdae4ac42010-10-21 05:21:48 +000081 PARSE_LANGOPT_BENIGN(MSCVersion);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000082 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
83 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
84 PARSE_LANGOPT_BENIGN(CXXOperatorName);
85 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
86 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
87 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
Fariborz Jahanian412e7982010-02-09 19:31:38 +000088 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI2, diag::warn_pch_nonfragile_abi2);
Fariborz Jahanianf84109e2011-01-07 18:59:25 +000089 PARSE_LANGOPT_IMPORTANT(AppleKext, diag::warn_pch_apple_kext);
Ted Kremenekc32647d2010-12-23 21:35:43 +000090 PARSE_LANGOPT_IMPORTANT(ObjCDefaultSynthProperties,
91 diag::warn_pch_objc_auto_properties);
Michael J. Spencer20249a12010-10-21 03:16:25 +000092 PARSE_LANGOPT_IMPORTANT(NoConstantCFStrings,
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +000093 diag::warn_pch_no_constant_cfstrings);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000094 PARSE_LANGOPT_BENIGN(PascalStrings);
95 PARSE_LANGOPT_BENIGN(WritableStrings);
Mike Stump1eb44332009-09-09 15:08:12 +000096 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000097 diag::warn_pch_lax_vector_conversions);
Nate Begeman69cfb9b2009-06-25 22:57:40 +000098 PARSE_LANGOPT_IMPORTANT(AltiVec, diag::warn_pch_altivec);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000099 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
Daniel Dunbar73482882010-02-10 18:48:44 +0000100 PARSE_LANGOPT_IMPORTANT(SjLjExceptions, diag::warn_pch_sjlj_exceptions);
Douglas Gregor6f755502011-02-01 15:15:22 +0000101 PARSE_LANGOPT_IMPORTANT(MSBitfields, diag::warn_pch_ms_bitfields);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000102 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
103 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
104 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
Mike Stump1eb44332009-09-09 15:08:12 +0000105 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000106 diag::warn_pch_thread_safe_statics);
Daniel Dunbar5345c392009-09-03 04:54:28 +0000107 PARSE_LANGOPT_IMPORTANT(POSIXThreads, diag::warn_pch_posix_threads);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000108 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
109 PARSE_LANGOPT_BENIGN(EmitAllDecls);
110 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
Chris Lattnera4d71452010-06-26 21:25:03 +0000111 PARSE_LANGOPT_BENIGN(getSignedOverflowBehavior());
Mike Stump1eb44332009-09-09 15:08:12 +0000112 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000113 diag::warn_pch_heinous_extensions);
114 // FIXME: Most of the options below are benign if the macro wasn't
115 // used. Unfortunately, this means that a PCH compiled without
116 // optimization can't be used with optimization turned on, even
117 // though the only thing that changes is whether __OPTIMIZE__ was
118 // defined... but if __OPTIMIZE__ never showed up in the header, it
119 // doesn't matter. We could consider making this some special kind
120 // of check.
121 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
122 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
123 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
124 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
125 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
126 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
127 PARSE_LANGOPT_IMPORTANT(AccessControl, diag::warn_pch_access_control);
128 PARSE_LANGOPT_IMPORTANT(CharIsSigned, diag::warn_pch_char_signed);
John Thompsona6fda122009-11-05 20:14:16 +0000129 PARSE_LANGOPT_IMPORTANT(ShortWChar, diag::warn_pch_short_wchar);
Argyrios Kyrtzidis9a2b9d72010-10-08 00:25:19 +0000130 PARSE_LANGOPT_IMPORTANT(ShortEnums, diag::warn_pch_short_enums);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000131 if ((PPLangOpts.getGCMode() != 0) != (LangOpts.getGCMode() != 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000132 Reader.Diag(diag::warn_pch_gc_mode)
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000133 << LangOpts.getGCMode() << PPLangOpts.getGCMode();
134 return true;
135 }
136 PARSE_LANGOPT_BENIGN(getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000137 PARSE_LANGOPT_IMPORTANT(getStackProtectorMode(),
138 diag::warn_pch_stack_protector);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000139 PARSE_LANGOPT_BENIGN(InstantiationDepth);
Nate Begeman69cfb9b2009-06-25 22:57:40 +0000140 PARSE_LANGOPT_IMPORTANT(OpenCL, diag::warn_pch_opencl);
Peter Collingbourne08a53262010-12-01 19:14:57 +0000141 PARSE_LANGOPT_IMPORTANT(CUDA, diag::warn_pch_cuda);
Mike Stump9c276ae2009-12-12 01:27:46 +0000142 PARSE_LANGOPT_BENIGN(CatchUndefined);
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000143 PARSE_LANGOPT_IMPORTANT(ElideConstructors, diag::warn_pch_elide_constructors);
Douglas Gregora0068fc2010-07-09 17:35:33 +0000144 PARSE_LANGOPT_BENIGN(SpellChecking);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000145 PARSE_LANGOPT_BENIGN(DefaultFPContract);
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +0000146#undef PARSE_LANGOPT_IMPORTANT
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000147#undef PARSE_LANGOPT_BENIGN
148
149 return false;
150}
151
Daniel Dunbardc3c0d22009-11-11 00:52:11 +0000152bool PCHValidator::ReadTargetTriple(llvm::StringRef Triple) {
153 if (Triple == PP.getTargetInfo().getTriple().str())
154 return false;
155
156 Reader.Diag(diag::warn_pch_target_triple)
157 << Triple << PP.getTargetInfo().getTriple().str();
158 return true;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000159}
160
Benjamin Kramer54353f42010-11-25 18:29:30 +0000161namespace {
162 struct EmptyStringRef {
163 bool operator ()(llvm::StringRef r) const { return r.empty(); }
164 };
165 struct EmptyBlock {
166 bool operator ()(const PCHPredefinesBlock &r) const {return r.Data.empty();}
167 };
168}
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000169
170static bool EqualConcatenations(llvm::SmallVector<llvm::StringRef, 2> L,
171 PCHPredefinesBlocks R) {
172 // First, sum up the lengths.
173 unsigned LL = 0, RL = 0;
174 for (unsigned I = 0, N = L.size(); I != N; ++I) {
175 LL += L[I].size();
176 }
177 for (unsigned I = 0, N = R.size(); I != N; ++I) {
178 RL += R[I].Data.size();
179 }
180 if (LL != RL)
181 return false;
182 if (LL == 0 && RL == 0)
183 return true;
184
185 // Kick out empty parts, they confuse the algorithm below.
186 L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end());
187 R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end());
188
189 // Do it the hard way. At this point, both vectors must be non-empty.
190 llvm::StringRef LR = L[0], RR = R[0].Data;
191 unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size();
Daniel Dunbarc76c9e02010-07-16 00:00:11 +0000192 (void) RN;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000193 for (;;) {
194 // Compare the current pieces.
195 if (LR.size() == RR.size()) {
196 // If they're the same length, it's pretty easy.
197 if (LR != RR)
198 return false;
199 // Both pieces are done, advance.
200 ++LI;
201 ++RI;
202 // If either string is done, they're both done, since they're the same
203 // length.
204 if (LI == LN) {
205 assert(RI == RN && "Strings not the same length after all?");
206 return true;
207 }
208 LR = L[LI];
209 RR = R[RI].Data;
210 } else if (LR.size() < RR.size()) {
211 // Right piece is longer.
212 if (!RR.startswith(LR))
213 return false;
214 ++LI;
215 assert(LI != LN && "Strings not the same length after all?");
216 RR = RR.substr(LR.size());
217 LR = L[LI];
218 } else {
219 // Left piece is longer.
220 if (!LR.startswith(RR))
221 return false;
222 ++RI;
223 assert(RI != RN && "Strings not the same length after all?");
224 LR = LR.substr(RR.size());
225 RR = R[RI].Data;
226 }
227 }
228}
229
230static std::pair<FileID, llvm::StringRef::size_type>
231FindMacro(const PCHPredefinesBlocks &Buffers, llvm::StringRef MacroDef) {
232 std::pair<FileID, llvm::StringRef::size_type> Res;
233 for (unsigned I = 0, N = Buffers.size(); I != N; ++I) {
234 Res.second = Buffers[I].Data.find(MacroDef);
235 if (Res.second != llvm::StringRef::npos) {
236 Res.first = Buffers[I].BufferID;
237 break;
238 }
239 }
240 return Res;
241}
242
243bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000244 llvm::StringRef OriginalFileName,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000245 std::string &SuggestedPredefines) {
Daniel Dunbarc7162932009-11-11 23:58:53 +0000246 // We are in the context of an implicit include, so the predefines buffer will
247 // have a #include entry for the PCH file itself (as normalized by the
248 // preprocessor initialization). Find it and skip over it in the checking
249 // below.
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000250 llvm::SmallString<256> PCHInclude;
251 PCHInclude += "#include \"";
Daniel Dunbarc7162932009-11-11 23:58:53 +0000252 PCHInclude += NormalizeDashIncludePath(OriginalFileName);
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000253 PCHInclude += "\"\n";
254 std::pair<llvm::StringRef,llvm::StringRef> Split =
255 llvm::StringRef(PP.getPredefines()).split(PCHInclude.str());
256 llvm::StringRef Left = Split.first, Right = Split.second;
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000257 if (Left == PP.getPredefines()) {
258 Error("Missing PCH include entry!");
259 return true;
260 }
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000261
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000262 // If the concatenation of all the PCH buffers is equal to the adjusted
263 // command line, we're done.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000264 llvm::SmallVector<llvm::StringRef, 2> CommandLine;
265 CommandLine.push_back(Left);
266 CommandLine.push_back(Right);
267 if (EqualConcatenations(CommandLine, Buffers))
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000268 return false;
269
270 SourceManager &SourceMgr = PP.getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000271
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000272 // The predefines buffers are different. Determine what the differences are,
273 // and whether they require us to reject the PCH file.
Daniel Dunbare6750492009-11-13 16:46:11 +0000274 llvm::SmallVector<llvm::StringRef, 8> PCHLines;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000275 for (unsigned I = 0, N = Buffers.size(); I != N; ++I)
276 Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Daniel Dunbare6750492009-11-13 16:46:11 +0000277
278 llvm::SmallVector<llvm::StringRef, 8> CmdLineLines;
279 Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000280
281 // Pick out implicit #includes after the PCH and don't consider them for
282 // validation; we will insert them into SuggestedPredefines so that the
283 // preprocessor includes them.
284 std::string IncludesAfterPCH;
285 llvm::SmallVector<llvm::StringRef, 8> AfterPCHLines;
286 Right.split(AfterPCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
287 for (unsigned i = 0, e = AfterPCHLines.size(); i != e; ++i) {
288 if (AfterPCHLines[i].startswith("#include ")) {
289 IncludesAfterPCH += AfterPCHLines[i];
290 IncludesAfterPCH += '\n';
291 } else {
292 CmdLineLines.push_back(AfterPCHLines[i]);
293 }
294 }
295
296 // Make sure we add the includes last into SuggestedPredefines before we
297 // exit this function.
298 struct AddIncludesRAII {
299 std::string &SuggestedPredefines;
300 std::string &IncludesAfterPCH;
301
302 AddIncludesRAII(std::string &SuggestedPredefines,
303 std::string &IncludesAfterPCH)
304 : SuggestedPredefines(SuggestedPredefines),
305 IncludesAfterPCH(IncludesAfterPCH) { }
306 ~AddIncludesRAII() {
307 SuggestedPredefines += IncludesAfterPCH;
308 }
309 } AddIncludes(SuggestedPredefines, IncludesAfterPCH);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000310
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000311 // Sort both sets of predefined buffer lines, since we allow some extra
312 // definitions and they may appear at any point in the output.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000313 std::sort(CmdLineLines.begin(), CmdLineLines.end());
314 std::sort(PCHLines.begin(), PCHLines.end());
315
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000316 // Determine which predefines that were used to build the PCH file are missing
317 // from the command line.
318 std::vector<llvm::StringRef> MissingPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000319 std::set_difference(PCHLines.begin(), PCHLines.end(),
320 CmdLineLines.begin(), CmdLineLines.end(),
321 std::back_inserter(MissingPredefines));
322
323 bool MissingDefines = false;
324 bool ConflictingDefines = false;
325 for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) {
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000326 llvm::StringRef Missing = MissingPredefines[I];
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000327 if (Missing.startswith("#include ")) {
328 // An -include was specified when generating the PCH; it is included in
329 // the PCH, just ignore it.
330 continue;
331 }
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000332 if (!Missing.startswith("#define ")) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000333 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
334 return true;
335 }
Mike Stump1eb44332009-09-09 15:08:12 +0000336
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000337 // This is a macro definition. Determine the name of the macro we're
338 // defining.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000339 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000340 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000341 = Missing.find_first_of("( \n\r", StartOfMacroName);
342 assert(EndOfMacroName != std::string::npos &&
343 "Couldn't find the end of the macro name");
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000344 llvm::StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000345
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000346 // Determine whether this macro was given a different definition on the
347 // command line.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000348 std::string MacroDefStart = "#define " + MacroName.str();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000349 std::string::size_type MacroDefLen = MacroDefStart.size();
Daniel Dunbare6750492009-11-13 16:46:11 +0000350 llvm::SmallVector<llvm::StringRef, 8>::iterator ConflictPos
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000351 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
352 MacroDefStart);
353 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000354 if (!ConflictPos->startswith(MacroDefStart)) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000355 // Different macro; we're done.
356 ConflictPos = CmdLineLines.end();
Mike Stump1eb44332009-09-09 15:08:12 +0000357 break;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000358 }
Mike Stump1eb44332009-09-09 15:08:12 +0000359
360 assert(ConflictPos->size() > MacroDefLen &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000361 "Invalid #define in predefines buffer?");
Mike Stump1eb44332009-09-09 15:08:12 +0000362 if ((*ConflictPos)[MacroDefLen] != ' ' &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000363 (*ConflictPos)[MacroDefLen] != '(')
364 continue; // Longer macro name; keep trying.
Mike Stump1eb44332009-09-09 15:08:12 +0000365
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000366 // We found a conflicting macro definition.
367 break;
368 }
Mike Stump1eb44332009-09-09 15:08:12 +0000369
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000370 if (ConflictPos != CmdLineLines.end()) {
371 Reader.Diag(diag::warn_cmdline_conflicting_macro_def)
372 << MacroName;
373
374 // Show the definition of this macro within the PCH file.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000375 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
376 FindMacro(Buffers, Missing);
377 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
378 SourceLocation PCHMissingLoc =
379 SourceMgr.getLocForStartOfFile(MacroLoc.first)
380 .getFileLocWithOffset(MacroLoc.second);
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000381 Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000382
383 ConflictingDefines = true;
384 continue;
385 }
Mike Stump1eb44332009-09-09 15:08:12 +0000386
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000387 // If the macro doesn't conflict, then we'll just pick up the macro
388 // definition from the PCH file. Warn the user that they made a mistake.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000389 if (ConflictingDefines)
390 continue; // Don't complain if there are already conflicting defs
Mike Stump1eb44332009-09-09 15:08:12 +0000391
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000392 if (!MissingDefines) {
393 Reader.Diag(diag::warn_cmdline_missing_macro_defs);
394 MissingDefines = true;
395 }
396
397 // Show the definition of this macro within the PCH file.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000398 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
399 FindMacro(Buffers, Missing);
400 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
401 SourceLocation PCHMissingLoc =
402 SourceMgr.getLocForStartOfFile(MacroLoc.first)
403 .getFileLocWithOffset(MacroLoc.second);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000404 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
405 }
Mike Stump1eb44332009-09-09 15:08:12 +0000406
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000407 if (ConflictingDefines)
408 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000409
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000410 // Determine what predefines were introduced based on command-line
411 // parameters that were not present when building the PCH
412 // file. Extra #defines are okay, so long as the identifiers being
413 // defined were not used within the precompiled header.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000414 std::vector<llvm::StringRef> ExtraPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000415 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
416 PCHLines.begin(), PCHLines.end(),
Mike Stump1eb44332009-09-09 15:08:12 +0000417 std::back_inserter(ExtraPredefines));
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000418 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000419 llvm::StringRef &Extra = ExtraPredefines[I];
420 if (!Extra.startswith("#define ")) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000421 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
422 return true;
423 }
424
425 // This is an extra macro definition. Determine the name of the
426 // macro we're defining.
427 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000428 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000429 = Extra.find_first_of("( \n\r", StartOfMacroName);
430 assert(EndOfMacroName != std::string::npos &&
431 "Couldn't find the end of the macro name");
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000432 llvm::StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000433
434 // Check whether this name was used somewhere in the PCH file. If
435 // so, defining it as a macro could change behavior, so we reject
436 // the PCH file.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000437 if (IdentifierInfo *II = Reader.get(MacroName)) {
Daniel Dunbar4fda42e2009-11-11 00:52:00 +0000438 Reader.Diag(diag::warn_macro_name_used_in_pch) << II;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000439 return true;
440 }
441
442 // Add this definition to the suggested predefines buffer.
443 SuggestedPredefines += Extra;
444 SuggestedPredefines += '\n';
445 }
446
447 // If we get here, it's because the predefines buffer had compatible
448 // contents. Accept the PCH file.
449 return false;
450}
451
Douglas Gregor12fab312010-03-16 16:35:32 +0000452void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
453 unsigned ID) {
454 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
455 ++NumHeaderInfos;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000456}
457
458void PCHValidator::ReadCounter(unsigned Value) {
459 PP.setCounterValue(Value);
460}
461
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000462//===----------------------------------------------------------------------===//
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000463// AST reader implementation
Douglas Gregor668c1a42009-04-21 22:25:48 +0000464//===----------------------------------------------------------------------===//
465
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000466void
Sebastian Redl571db7f2010-08-18 23:56:56 +0000467ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000468 DeserializationListener = Listener;
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000469}
470
Chris Lattner4c6f9522009-04-27 05:14:47 +0000471
Douglas Gregor668c1a42009-04-21 22:25:48 +0000472namespace {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000473class ASTSelectorLookupTrait {
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000474 ASTReader &Reader;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000475
476public:
Sebastian Redl5d050072010-08-04 17:20:04 +0000477 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000478 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +0000479 ObjCMethodList Instance, Factory;
480 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000481
482 typedef Selector external_key_type;
483 typedef external_key_type internal_key_type;
484
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000485 explicit ASTSelectorLookupTrait(ASTReader &Reader) : Reader(Reader) { }
Mike Stump1eb44332009-09-09 15:08:12 +0000486
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000487 static bool EqualKey(const internal_key_type& a,
488 const internal_key_type& b) {
489 return a == b;
490 }
Mike Stump1eb44332009-09-09 15:08:12 +0000491
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000492 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +0000493 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000494 }
Mike Stump1eb44332009-09-09 15:08:12 +0000495
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000496 // This hopefully will just get inlined and removed by the optimizer.
497 static const internal_key_type&
498 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump1eb44332009-09-09 15:08:12 +0000499
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000500 static std::pair<unsigned, unsigned>
501 ReadKeyDataLength(const unsigned char*& d) {
502 using namespace clang::io;
503 unsigned KeyLen = ReadUnalignedLE16(d);
504 unsigned DataLen = ReadUnalignedLE16(d);
505 return std::make_pair(KeyLen, DataLen);
506 }
Mike Stump1eb44332009-09-09 15:08:12 +0000507
Douglas Gregor83941df2009-04-25 17:48:32 +0000508 internal_key_type ReadKey(const unsigned char* d, unsigned) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000509 using namespace clang::io;
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000510 SelectorTable &SelTable = Reader.getContext()->Selectors;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000511 unsigned N = ReadUnalignedLE16(d);
Mike Stump1eb44332009-09-09 15:08:12 +0000512 IdentifierInfo *FirstII
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000513 = Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
514 if (N == 0)
515 return SelTable.getNullarySelector(FirstII);
516 else if (N == 1)
517 return SelTable.getUnarySelector(FirstII);
518
519 llvm::SmallVector<IdentifierInfo *, 16> Args;
520 Args.push_back(FirstII);
521 for (unsigned I = 1; I != N; ++I)
522 Args.push_back(Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)));
523
Douglas Gregor75fdb232009-05-22 22:45:36 +0000524 return SelTable.getSelector(N, Args.data());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000525 }
Mike Stump1eb44332009-09-09 15:08:12 +0000526
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000527 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen) {
528 using namespace clang::io;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000529
530 data_type Result;
531
Sebastian Redl5d050072010-08-04 17:20:04 +0000532 Result.ID = ReadUnalignedLE32(d);
533 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
534 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
535
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000536 // Load instance methods
537 ObjCMethodList *Prev = 0;
538 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000539 ObjCMethodDecl *Method
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000540 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl5d050072010-08-04 17:20:04 +0000541 if (!Result.Instance.Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000542 // This is the first method, which is the easy case.
Sebastian Redl5d050072010-08-04 17:20:04 +0000543 Result.Instance.Method = Method;
544 Prev = &Result.Instance;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000545 continue;
546 }
547
Ted Kremenek298ed872010-02-11 00:53:01 +0000548 ObjCMethodList *Mem =
549 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
550 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000551 Prev = Prev->Next;
552 }
553
554 // Load factory methods
555 Prev = 0;
556 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000557 ObjCMethodDecl *Method
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000558 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl5d050072010-08-04 17:20:04 +0000559 if (!Result.Factory.Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000560 // This is the first method, which is the easy case.
Sebastian Redl5d050072010-08-04 17:20:04 +0000561 Result.Factory.Method = Method;
562 Prev = &Result.Factory;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000563 continue;
564 }
565
Ted Kremenek298ed872010-02-11 00:53:01 +0000566 ObjCMethodList *Mem =
567 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
568 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000569 Prev = Prev->Next;
570 }
571
572 return Result;
573 }
574};
Mike Stump1eb44332009-09-09 15:08:12 +0000575
576} // end anonymous namespace
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000577
578/// \brief The on-disk hash table used for the global method pool.
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000579typedef OnDiskChainedHashTable<ASTSelectorLookupTrait>
580 ASTSelectorLookupTable;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000581
Sebastian Redlc3632732010-10-05 15:59:54 +0000582namespace clang {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000583class ASTIdentifierLookupTrait {
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000584 ASTReader &Reader;
Sebastian Redlc3632732010-10-05 15:59:54 +0000585 ASTReader::PerFileData &F;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000586
587 // If we know the IdentifierInfo in advance, it is here and we will
588 // not build a new one. Used when deserializing information about an
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000589 // identifier that was constructed before the AST file was read.
Douglas Gregor668c1a42009-04-21 22:25:48 +0000590 IdentifierInfo *KnownII;
591
592public:
593 typedef IdentifierInfo * data_type;
594
595 typedef const std::pair<const char*, unsigned> external_key_type;
596
597 typedef external_key_type internal_key_type;
598
Sebastian Redlc3632732010-10-05 15:59:54 +0000599 ASTIdentifierLookupTrait(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redld27d3fc2010-07-21 22:31:37 +0000600 IdentifierInfo *II = 0)
Sebastian Redlc3632732010-10-05 15:59:54 +0000601 : Reader(Reader), F(F), KnownII(II) { }
Mike Stump1eb44332009-09-09 15:08:12 +0000602
Douglas Gregor668c1a42009-04-21 22:25:48 +0000603 static bool EqualKey(const internal_key_type& a,
604 const internal_key_type& b) {
605 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
606 : false;
607 }
Mike Stump1eb44332009-09-09 15:08:12 +0000608
Douglas Gregor668c1a42009-04-21 22:25:48 +0000609 static unsigned ComputeHash(const internal_key_type& a) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000610 return llvm::HashString(llvm::StringRef(a.first, a.second));
Douglas Gregor668c1a42009-04-21 22:25:48 +0000611 }
Mike Stump1eb44332009-09-09 15:08:12 +0000612
Douglas Gregor668c1a42009-04-21 22:25:48 +0000613 // This hopefully will just get inlined and removed by the optimizer.
614 static const internal_key_type&
615 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump1eb44332009-09-09 15:08:12 +0000616
Douglas Gregor95f42922010-10-14 22:11:03 +0000617 // This hopefully will just get inlined and removed by the optimizer.
618 static const external_key_type&
619 GetExternalKey(const internal_key_type& x) { return x; }
620
Douglas Gregor668c1a42009-04-21 22:25:48 +0000621 static std::pair<unsigned, unsigned>
622 ReadKeyDataLength(const unsigned char*& d) {
623 using namespace clang::io;
Douglas Gregor5f8e3302009-04-25 20:26:24 +0000624 unsigned DataLen = ReadUnalignedLE16(d);
Douglas Gregord6595a42009-04-25 21:04:17 +0000625 unsigned KeyLen = ReadUnalignedLE16(d);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000626 return std::make_pair(KeyLen, DataLen);
627 }
Mike Stump1eb44332009-09-09 15:08:12 +0000628
Douglas Gregor668c1a42009-04-21 22:25:48 +0000629 static std::pair<const char*, unsigned>
630 ReadKey(const unsigned char* d, unsigned n) {
631 assert(n >= 2 && d[n-1] == '\0');
632 return std::make_pair((const char*) d, n-1);
633 }
Mike Stump1eb44332009-09-09 15:08:12 +0000634
635 IdentifierInfo *ReadData(const internal_key_type& k,
Douglas Gregor668c1a42009-04-21 22:25:48 +0000636 const unsigned char* d,
637 unsigned DataLen) {
638 using namespace clang::io;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000639 IdentID ID = ReadUnalignedLE32(d);
Douglas Gregora92193e2009-04-28 21:18:29 +0000640 bool IsInteresting = ID & 0x01;
641
642 // Wipe out the "is interesting" bit.
643 ID = ID >> 1;
644
645 if (!IsInteresting) {
Sebastian Redl083abdf2010-07-27 23:01:28 +0000646 // For uninteresting identifiers, just build the IdentifierInfo
Douglas Gregora92193e2009-04-28 21:18:29 +0000647 // and associate it with the persistent ID.
648 IdentifierInfo *II = KnownII;
649 if (!II)
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000650 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregora92193e2009-04-28 21:18:29 +0000651 Reader.SetIdentifierInfo(ID, II);
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000652 II->setIsFromAST();
Douglas Gregora92193e2009-04-28 21:18:29 +0000653 return II;
654 }
655
Douglas Gregor5998da52009-04-28 21:32:13 +0000656 unsigned Bits = ReadUnalignedLE16(d);
Douglas Gregor2deaea32009-04-22 18:49:13 +0000657 bool CPlusPlusOperatorKeyword = Bits & 0x01;
658 Bits >>= 1;
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000659 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
660 Bits >>= 1;
Douglas Gregor2deaea32009-04-22 18:49:13 +0000661 bool Poisoned = Bits & 0x01;
662 Bits >>= 1;
663 bool ExtensionToken = Bits & 0x01;
664 Bits >>= 1;
665 bool hasMacroDefinition = Bits & 0x01;
666 Bits >>= 1;
667 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
668 Bits >>= 10;
Mike Stump1eb44332009-09-09 15:08:12 +0000669
Douglas Gregor2deaea32009-04-22 18:49:13 +0000670 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregor5998da52009-04-28 21:32:13 +0000671 DataLen -= 6;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000672
673 // Build the IdentifierInfo itself and link the identifier ID with
674 // the new IdentifierInfo.
675 IdentifierInfo *II = KnownII;
676 if (!II)
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000677 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000678 Reader.SetIdentifierInfo(ID, II);
679
Douglas Gregor2deaea32009-04-22 18:49:13 +0000680 // Set or check the various bits in the IdentifierInfo structure.
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000681 // Token IDs are read-only.
682 if (HasRevertedTokenIDToIdentifier)
683 II->RevertTokenIDToIdentifier();
Douglas Gregor2deaea32009-04-22 18:49:13 +0000684 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
Mike Stump1eb44332009-09-09 15:08:12 +0000685 assert(II->isExtensionToken() == ExtensionToken &&
Douglas Gregor2deaea32009-04-22 18:49:13 +0000686 "Incorrect extension token flag");
687 (void)ExtensionToken;
688 II->setIsPoisoned(Poisoned);
689 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
690 "Incorrect C++ operator keyword flag");
691 (void)CPlusPlusOperatorKeyword;
692
Douglas Gregor37e26842009-04-21 23:56:24 +0000693 // If this identifier is a macro, deserialize the macro
694 // definition.
695 if (hasMacroDefinition) {
Douglas Gregor5998da52009-04-28 21:32:13 +0000696 uint32_t Offset = ReadUnalignedLE32(d);
Douglas Gregor295a2a62010-10-30 00:23:06 +0000697 Reader.SetIdentifierIsMacro(II, F, Offset);
Douglas Gregor5998da52009-04-28 21:32:13 +0000698 DataLen -= 4;
Douglas Gregor37e26842009-04-21 23:56:24 +0000699 }
Douglas Gregor668c1a42009-04-21 22:25:48 +0000700
701 // Read all of the declarations visible at global scope with this
702 // name.
Chris Lattner6bf690f2009-04-27 22:17:41 +0000703 if (Reader.getContext() == 0) return II;
Douglas Gregord89275b2009-07-06 18:54:52 +0000704 if (DataLen > 0) {
705 llvm::SmallVector<uint32_t, 4> DeclIDs;
706 for (; DataLen > 0; DataLen -= 4)
707 DeclIDs.push_back(ReadUnalignedLE32(d));
708 Reader.SetGloballyVisibleDecls(II, DeclIDs);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000709 }
Mike Stump1eb44332009-09-09 15:08:12 +0000710
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000711 II->setIsFromAST();
Douglas Gregor668c1a42009-04-21 22:25:48 +0000712 return II;
713 }
714};
Mike Stump1eb44332009-09-09 15:08:12 +0000715
716} // end anonymous namespace
Douglas Gregor668c1a42009-04-21 22:25:48 +0000717
718/// \brief The on-disk hash table used to contain information about
719/// all of the identifiers in the program.
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000720typedef OnDiskChainedHashTable<ASTIdentifierLookupTrait>
721 ASTIdentifierLookupTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000722
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000723namespace {
724class ASTDeclContextNameLookupTrait {
725 ASTReader &Reader;
726
727public:
728 /// \brief Pair of begin/end iterators for DeclIDs.
729 typedef std::pair<DeclID *, DeclID *> data_type;
730
731 /// \brief Special internal key for declaration names.
732 /// The hash table creates keys for comparison; we do not create
733 /// a DeclarationName for the internal key to avoid deserializing types.
734 struct DeclNameKey {
735 DeclarationName::NameKind Kind;
736 uint64_t Data;
737 DeclNameKey() : Kind((DeclarationName::NameKind)0), Data(0) { }
738 };
739
740 typedef DeclarationName external_key_type;
741 typedef DeclNameKey internal_key_type;
742
743 explicit ASTDeclContextNameLookupTrait(ASTReader &Reader) : Reader(Reader) { }
744
745 static bool EqualKey(const internal_key_type& a,
746 const internal_key_type& b) {
747 return a.Kind == b.Kind && a.Data == b.Data;
748 }
749
750 unsigned ComputeHash(const DeclNameKey &Key) const {
751 llvm::FoldingSetNodeID ID;
752 ID.AddInteger(Key.Kind);
753
754 switch (Key.Kind) {
755 case DeclarationName::Identifier:
756 case DeclarationName::CXXLiteralOperatorName:
757 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
758 break;
759 case DeclarationName::ObjCZeroArgSelector:
760 case DeclarationName::ObjCOneArgSelector:
761 case DeclarationName::ObjCMultiArgSelector:
762 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
763 break;
764 case DeclarationName::CXXConstructorName:
765 case DeclarationName::CXXDestructorName:
766 case DeclarationName::CXXConversionFunctionName:
767 ID.AddInteger((TypeID)Key.Data);
768 break;
769 case DeclarationName::CXXOperatorName:
770 ID.AddInteger((OverloadedOperatorKind)Key.Data);
771 break;
772 case DeclarationName::CXXUsingDirective:
773 break;
774 }
775
776 return ID.ComputeHash();
777 }
778
779 internal_key_type GetInternalKey(const external_key_type& Name) const {
780 DeclNameKey Key;
781 Key.Kind = Name.getNameKind();
782 switch (Name.getNameKind()) {
783 case DeclarationName::Identifier:
784 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
785 break;
786 case DeclarationName::ObjCZeroArgSelector:
787 case DeclarationName::ObjCOneArgSelector:
788 case DeclarationName::ObjCMultiArgSelector:
789 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
790 break;
791 case DeclarationName::CXXConstructorName:
792 case DeclarationName::CXXDestructorName:
793 case DeclarationName::CXXConversionFunctionName:
794 Key.Data = Reader.GetTypeID(Name.getCXXNameType());
795 break;
796 case DeclarationName::CXXOperatorName:
797 Key.Data = Name.getCXXOverloadedOperator();
798 break;
799 case DeclarationName::CXXLiteralOperatorName:
800 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
801 break;
802 case DeclarationName::CXXUsingDirective:
803 break;
804 }
Michael J. Spencer20249a12010-10-21 03:16:25 +0000805
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000806 return Key;
807 }
808
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +0000809 external_key_type GetExternalKey(const internal_key_type& Key) const {
810 ASTContext *Context = Reader.getContext();
811 switch (Key.Kind) {
812 case DeclarationName::Identifier:
813 return DeclarationName((IdentifierInfo*)Key.Data);
814
815 case DeclarationName::ObjCZeroArgSelector:
816 case DeclarationName::ObjCOneArgSelector:
817 case DeclarationName::ObjCMultiArgSelector:
818 return DeclarationName(Selector(Key.Data));
819
820 case DeclarationName::CXXConstructorName:
821 return Context->DeclarationNames.getCXXConstructorName(
822 Context->getCanonicalType(Reader.GetType(Key.Data)));
823
824 case DeclarationName::CXXDestructorName:
825 return Context->DeclarationNames.getCXXDestructorName(
826 Context->getCanonicalType(Reader.GetType(Key.Data)));
827
828 case DeclarationName::CXXConversionFunctionName:
829 return Context->DeclarationNames.getCXXConversionFunctionName(
830 Context->getCanonicalType(Reader.GetType(Key.Data)));
831
832 case DeclarationName::CXXOperatorName:
833 return Context->DeclarationNames.getCXXOperatorName(
834 (OverloadedOperatorKind)Key.Data);
835
836 case DeclarationName::CXXLiteralOperatorName:
837 return Context->DeclarationNames.getCXXLiteralOperatorName(
838 (IdentifierInfo*)Key.Data);
839
840 case DeclarationName::CXXUsingDirective:
841 return DeclarationName::getUsingDirectiveName();
842 }
843
844 llvm_unreachable("Invalid Name Kind ?");
845 }
846
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000847 static std::pair<unsigned, unsigned>
848 ReadKeyDataLength(const unsigned char*& d) {
849 using namespace clang::io;
850 unsigned KeyLen = ReadUnalignedLE16(d);
851 unsigned DataLen = ReadUnalignedLE16(d);
852 return std::make_pair(KeyLen, DataLen);
853 }
854
855 internal_key_type ReadKey(const unsigned char* d, unsigned) {
856 using namespace clang::io;
857
858 DeclNameKey Key;
859 Key.Kind = (DeclarationName::NameKind)*d++;
860 switch (Key.Kind) {
861 case DeclarationName::Identifier:
862 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
863 break;
864 case DeclarationName::ObjCZeroArgSelector:
865 case DeclarationName::ObjCOneArgSelector:
866 case DeclarationName::ObjCMultiArgSelector:
Michael J. Spencer20249a12010-10-21 03:16:25 +0000867 Key.Data =
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000868 (uint64_t)Reader.DecodeSelector(ReadUnalignedLE32(d)).getAsOpaquePtr();
869 break;
870 case DeclarationName::CXXConstructorName:
871 case DeclarationName::CXXDestructorName:
872 case DeclarationName::CXXConversionFunctionName:
873 Key.Data = ReadUnalignedLE32(d); // TypeID
874 break;
875 case DeclarationName::CXXOperatorName:
876 Key.Data = *d++; // OverloadedOperatorKind
877 break;
878 case DeclarationName::CXXLiteralOperatorName:
879 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
880 break;
881 case DeclarationName::CXXUsingDirective:
882 break;
883 }
Michael J. Spencer20249a12010-10-21 03:16:25 +0000884
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000885 return Key;
886 }
887
888 data_type ReadData(internal_key_type, const unsigned char* d,
889 unsigned DataLen) {
890 using namespace clang::io;
891 unsigned NumDecls = ReadUnalignedLE16(d);
892 DeclID *Start = (DeclID *)d;
893 return std::make_pair(Start, Start + NumDecls);
894 }
895};
896
897} // end anonymous namespace
898
899/// \brief The on-disk hash table used for the DeclContext's Name lookup table.
900typedef OnDiskChainedHashTable<ASTDeclContextNameLookupTrait>
901 ASTDeclContextNameLookupTable;
902
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000903bool ASTReader::ReadDeclContextStorage(llvm::BitstreamCursor &Cursor,
904 const std::pair<uint64_t, uint64_t> &Offsets,
905 DeclContextInfo &Info) {
906 SavedStreamPosition SavedPosition(Cursor);
907 // First the lexical decls.
908 if (Offsets.first != 0) {
909 Cursor.JumpToBit(Offsets.first);
910
911 RecordData Record;
912 const char *Blob;
913 unsigned BlobLen;
914 unsigned Code = Cursor.ReadCode();
915 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
916 if (RecCode != DECL_CONTEXT_LEXICAL) {
917 Error("Expected lexical block");
918 return true;
919 }
920
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000921 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob);
922 Info.NumLexicalDecls = BlobLen / sizeof(KindDeclIDPair);
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000923 } else {
924 Info.LexicalDecls = 0;
925 Info.NumLexicalDecls = 0;
926 }
927
928 // Now the lookup table.
929 if (Offsets.second != 0) {
930 Cursor.JumpToBit(Offsets.second);
931
932 RecordData Record;
933 const char *Blob;
934 unsigned BlobLen;
935 unsigned Code = Cursor.ReadCode();
936 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
937 if (RecCode != DECL_CONTEXT_VISIBLE) {
938 Error("Expected visible lookup table block");
939 return true;
940 }
941 Info.NameLookupTableData
942 = ASTDeclContextNameLookupTable::Create(
943 (const unsigned char *)Blob + Record[0],
944 (const unsigned char *)Blob,
945 ASTDeclContextNameLookupTrait(*this));
Sebastian Redl0ea8f7f2010-08-24 00:50:00 +0000946 } else {
947 Info.NameLookupTableData = 0;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000948 }
949
950 return false;
951}
952
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000953void ASTReader::Error(const char *Msg) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000954 Diag(diag::err_fe_pch_malformed) << Msg;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000955}
956
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000957/// \brief Tell the AST listener about the predefines buffers in the chain.
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000958bool ASTReader::CheckPredefinesBuffers() {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000959 if (Listener)
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000960 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000961 ActualOriginalFileName,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000962 SuggestedPredefines);
Douglas Gregore721f952009-04-28 18:58:38 +0000963 return false;
Douglas Gregore1d918e2009-04-10 23:10:45 +0000964}
965
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000966//===----------------------------------------------------------------------===//
967// Source Manager Deserialization
968//===----------------------------------------------------------------------===//
969
Douglas Gregorbd945002009-04-13 16:31:14 +0000970/// \brief Read the line table in the source manager block.
Sebastian Redlc3632732010-10-05 15:59:54 +0000971/// \returns true if there was an error.
972bool ASTReader::ParseLineTable(PerFileData &F,
973 llvm::SmallVectorImpl<uint64_t> &Record) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000974 unsigned Idx = 0;
975 LineTableInfo &LineTable = SourceMgr.getLineTable();
976
977 // Parse the file names
Douglas Gregorff0a9872009-04-13 17:12:42 +0000978 std::map<int, int> FileIDs;
979 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000980 // Extract the file name
981 unsigned FilenameLen = Record[Idx++];
982 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
983 Idx += FilenameLen;
Douglas Gregore650c8c2009-07-07 00:12:59 +0000984 MaybeAddSystemRootToFilename(Filename);
Mike Stump1eb44332009-09-09 15:08:12 +0000985 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
Douglas Gregorff0a9872009-04-13 17:12:42 +0000986 Filename.size());
Douglas Gregorbd945002009-04-13 16:31:14 +0000987 }
988
989 // Parse the line entries
990 std::vector<LineEntry> Entries;
991 while (Idx < Record.size()) {
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000992 int FID = Record[Idx++];
Douglas Gregorbd945002009-04-13 16:31:14 +0000993
994 // Extract the line entries
995 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000996 assert(NumEntries && "Numentries is 00000");
Douglas Gregorbd945002009-04-13 16:31:14 +0000997 Entries.clear();
998 Entries.reserve(NumEntries);
999 for (unsigned I = 0; I != NumEntries; ++I) {
1000 unsigned FileOffset = Record[Idx++];
1001 unsigned LineNo = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +00001002 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump1eb44332009-09-09 15:08:12 +00001003 SrcMgr::CharacteristicKind FileKind
Douglas Gregorbd945002009-04-13 16:31:14 +00001004 = (SrcMgr::CharacteristicKind)Record[Idx++];
1005 unsigned IncludeOffset = Record[Idx++];
1006 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1007 FileKind, IncludeOffset));
1008 }
1009 LineTable.AddEntry(FID, Entries);
1010 }
1011
1012 return false;
1013}
1014
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001015namespace {
1016
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001017class ASTStatData {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001018public:
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001019 const ino_t ino;
1020 const dev_t dev;
1021 const mode_t mode;
1022 const time_t mtime;
1023 const off_t size;
Mike Stump1eb44332009-09-09 15:08:12 +00001024
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001025 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Chris Lattner74e976b2010-11-23 19:28:12 +00001026 : ino(i), dev(d), mode(mo), mtime(m), size(s) {}
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001027};
1028
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001029class ASTStatLookupTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001030 public:
1031 typedef const char *external_key_type;
1032 typedef const char *internal_key_type;
1033
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001034 typedef ASTStatData data_type;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001035
1036 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001037 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001038 }
1039
1040 static internal_key_type GetInternalKey(const char *path) { return path; }
1041
1042 static bool EqualKey(internal_key_type a, internal_key_type b) {
1043 return strcmp(a, b) == 0;
1044 }
1045
1046 static std::pair<unsigned, unsigned>
1047 ReadKeyDataLength(const unsigned char*& d) {
1048 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1049 unsigned DataLen = (unsigned) *d++;
1050 return std::make_pair(KeyLen + 1, DataLen);
1051 }
1052
1053 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
1054 return (const char *)d;
1055 }
1056
1057 static data_type ReadData(const internal_key_type, const unsigned char *d,
1058 unsigned /*DataLen*/) {
1059 using namespace clang::io;
1060
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001061 ino_t ino = (ino_t) ReadUnalignedLE32(d);
1062 dev_t dev = (dev_t) ReadUnalignedLE32(d);
1063 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump1eb44332009-09-09 15:08:12 +00001064 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001065 off_t size = (off_t) ReadUnalignedLE64(d);
1066 return data_type(ino, dev, mode, mtime, size);
1067 }
1068};
1069
1070/// \brief stat() cache for precompiled headers.
1071///
1072/// This cache is very similar to the stat cache used by pretokenized
1073/// headers.
Chris Lattner10e286a2010-11-23 19:19:34 +00001074class ASTStatCache : public FileSystemStatCache {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001075 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001076 CacheTy *Cache;
1077
1078 unsigned &NumStatHits, &NumStatMisses;
Mike Stump1eb44332009-09-09 15:08:12 +00001079public:
Chris Lattner74e976b2010-11-23 19:28:12 +00001080 ASTStatCache(const unsigned char *Buckets, const unsigned char *Base,
1081 unsigned &NumStatHits, unsigned &NumStatMisses)
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001082 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
1083 Cache = CacheTy::Create(Buckets, Base);
1084 }
1085
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001086 ~ASTStatCache() { delete Cache; }
Mike Stump1eb44332009-09-09 15:08:12 +00001087
Chris Lattner898a0612010-11-23 21:17:56 +00001088 LookupResult getStat(const char *Path, struct stat &StatBuf,
1089 int *FileDescriptor) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001090 // Do the lookup for the file's data in the AST file.
Chris Lattner10e286a2010-11-23 19:19:34 +00001091 CacheTy::iterator I = Cache->find(Path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001092
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001093 // If we don't get a hit in the AST file just forward to 'stat'.
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001094 if (I == Cache->end()) {
1095 ++NumStatMisses;
Chris Lattner898a0612010-11-23 21:17:56 +00001096 return statChained(Path, StatBuf, FileDescriptor);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001097 }
Mike Stump1eb44332009-09-09 15:08:12 +00001098
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001099 ++NumStatHits;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001100 ASTStatData Data = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001101
Chris Lattner10e286a2010-11-23 19:19:34 +00001102 StatBuf.st_ino = Data.ino;
1103 StatBuf.st_dev = Data.dev;
1104 StatBuf.st_mtime = Data.mtime;
1105 StatBuf.st_mode = Data.mode;
1106 StatBuf.st_size = Data.size;
Chris Lattnerd6f61112010-11-23 20:05:15 +00001107 return CacheExists;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001108 }
1109};
1110} // end anonymous namespace
1111
1112
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001113/// \brief Read a source manager block
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001114ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(PerFileData &F) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001115 using namespace SrcMgr;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001116
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001117 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001118
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001119 // Set the source-location entry cursor to the current position in
1120 // the stream. This cursor will be used to read the contents of the
1121 // source manager block initially, and then lazily read
1122 // source-location entries as needed.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001123 SLocEntryCursor = F.Stream;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001124
1125 // The stream itself is going to skip over the source manager block.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001126 if (F.Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001127 Error("malformed block record in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001128 return Failure;
1129 }
1130
1131 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001132 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001133 Error("malformed source manager block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001134 return Failure;
1135 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001136
Douglas Gregor14f79002009-04-10 03:52:48 +00001137 RecordData Record;
1138 while (true) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001139 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregor14f79002009-04-10 03:52:48 +00001140 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001141 if (SLocEntryCursor.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001142 Error("error at end of Source Manager block in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001143 return Failure;
1144 }
Douglas Gregore1d918e2009-04-10 23:10:45 +00001145 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +00001146 }
Mike Stump1eb44332009-09-09 15:08:12 +00001147
Douglas Gregor14f79002009-04-10 03:52:48 +00001148 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1149 // No known subblocks, always skip them.
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001150 SLocEntryCursor.ReadSubBlockID();
1151 if (SLocEntryCursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001152 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001153 return Failure;
1154 }
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 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001159 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregor14f79002009-04-10 03:52:48 +00001160 continue;
1161 }
Mike Stump1eb44332009-09-09 15:08:12 +00001162
Douglas Gregor14f79002009-04-10 03:52:48 +00001163 // Read a record.
1164 const char *BlobStart;
1165 unsigned BlobLen;
1166 Record.clear();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001167 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001168 default: // Default behavior: ignore.
1169 break;
1170
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001171 case SM_LINE_TABLE:
Sebastian Redlc3632732010-10-05 15:59:54 +00001172 if (ParseLineTable(F, Record))
Douglas Gregorbd945002009-04-13 16:31:14 +00001173 return Failure;
Chris Lattner2c78b872009-04-14 23:22:57 +00001174 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001175
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001176 case SM_SLOC_FILE_ENTRY:
1177 case SM_SLOC_BUFFER_ENTRY:
1178 case SM_SLOC_INSTANTIATION_ENTRY:
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001179 // Once we hit one of the source location entries, we're done.
1180 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +00001181 }
1182 }
1183}
1184
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001185/// \brief If a header file is not found at the path that we expect it to be
1186/// and the PCH file was moved from its original location, try to resolve the
1187/// file by assuming that header+PCH were moved together and the header is in
1188/// the same place relative to the PCH.
1189static std::string
1190resolveFileRelativeToOriginalDir(const std::string &Filename,
1191 const std::string &OriginalDir,
1192 const std::string &CurrDir) {
1193 assert(OriginalDir != CurrDir &&
1194 "No point trying to resolve the file if the PCH dir didn't change");
1195 using namespace llvm::sys;
1196 llvm::SmallString<128> filePath(Filename);
1197 fs::make_absolute(filePath);
1198 assert(path::is_absolute(OriginalDir));
1199 llvm::SmallString<128> currPCHPath(CurrDir);
1200
1201 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1202 fileDirE = path::end(path::parent_path(filePath));
1203 path::const_iterator origDirI = path::begin(OriginalDir),
1204 origDirE = path::end(OriginalDir);
1205 // Skip the common path components from filePath and OriginalDir.
1206 while (fileDirI != fileDirE && origDirI != origDirE &&
1207 *fileDirI == *origDirI) {
1208 ++fileDirI;
1209 ++origDirI;
1210 }
1211 for (; origDirI != origDirE; ++origDirI)
1212 path::append(currPCHPath, "..");
1213 path::append(currPCHPath, fileDirI, fileDirE);
1214 path::append(currPCHPath, path::filename(Filename));
1215 return currPCHPath.str();
1216}
1217
Sebastian Redl190faf72010-07-20 21:50:20 +00001218/// \brief Get a cursor that's correctly positioned for reading the source
1219/// location entry with the given ID.
Sebastian Redlc3632732010-10-05 15:59:54 +00001220ASTReader::PerFileData *ASTReader::SLocCursorForID(unsigned ID) {
Sebastian Redl190faf72010-07-20 21:50:20 +00001221 assert(ID != 0 && ID <= TotalNumSLocEntries &&
1222 "SLocCursorForID should only be called for real IDs.");
1223
1224 ID -= 1;
1225 PerFileData *F = 0;
1226 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1227 F = Chain[N - I - 1];
1228 if (ID < F->LocalNumSLocEntries)
1229 break;
1230 ID -= F->LocalNumSLocEntries;
1231 }
1232 assert(F && F->LocalNumSLocEntries > ID && "Chain corrupted");
1233
1234 F->SLocEntryCursor.JumpToBit(F->SLocOffsets[ID]);
Sebastian Redlc3632732010-10-05 15:59:54 +00001235 return F;
Sebastian Redl190faf72010-07-20 21:50:20 +00001236}
1237
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001238/// \brief Read in the source location entry with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001239ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(unsigned ID) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001240 if (ID == 0)
1241 return Success;
1242
1243 if (ID > TotalNumSLocEntries) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001244 Error("source location entry ID out-of-range for AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001245 return Failure;
1246 }
1247
Sebastian Redlc3632732010-10-05 15:59:54 +00001248 PerFileData *F = SLocCursorForID(ID);
1249 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001250
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001251 ++NumSLocEntriesRead;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001252 unsigned Code = SLocEntryCursor.ReadCode();
1253 if (Code == llvm::bitc::END_BLOCK ||
1254 Code == llvm::bitc::ENTER_SUBBLOCK ||
1255 Code == llvm::bitc::DEFINE_ABBREV) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001256 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001257 return Failure;
1258 }
1259
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001260 RecordData Record;
1261 const char *BlobStart;
1262 unsigned BlobLen;
1263 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1264 default:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001265 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001266 return Failure;
1267
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001268 case SM_SLOC_FILE_ENTRY: {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001269 std::string Filename(BlobStart, BlobStart + BlobLen);
1270 MaybeAddSystemRootToFilename(Filename);
Chris Lattner39b49bc2010-11-23 08:35:12 +00001271 const FileEntry *File = FileMgr.getFile(Filename);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001272 if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() &&
1273 OriginalDir != CurrentDir) {
1274 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1275 OriginalDir,
1276 CurrentDir);
1277 if (!resolved.empty())
1278 File = FileMgr.getFile(resolved);
1279 }
Axel Naumann04331162011-01-27 10:55:51 +00001280 if (File == 0)
1281 File = FileMgr.getVirtualFile(Filename, (off_t)Record[4],
1282 (time_t)Record[5]);
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001283 if (File == 0) {
1284 std::string ErrorStr = "could not find file '";
Douglas Gregore650c8c2009-07-07 00:12:59 +00001285 ErrorStr += Filename;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001286 ErrorStr += "' referenced by AST file";
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001287 Error(ErrorStr.c_str());
1288 return Failure;
1289 }
Mike Stump1eb44332009-09-09 15:08:12 +00001290
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001291 if (Record.size() < 6) {
Ted Kremenek1857f622010-03-18 21:23:05 +00001292 Error("source location entry is incorrect");
1293 return Failure;
1294 }
1295
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001296 if (!DisableValidation &&
1297 ((off_t)Record[4] != File->getSize()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001298#if !defined(LLVM_ON_WIN32)
1299 // In our regression testing, the Windows file system seems to
1300 // have inconsistent modification times that sometimes
1301 // erroneously trigger this error-handling path.
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001302 || (time_t)Record[5] != File->getModificationTime()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001303#endif
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001304 )) {
Douglas Gregor2d52be52010-03-21 22:49:54 +00001305 Diag(diag::err_fe_pch_file_modified)
1306 << Filename;
1307 return Failure;
1308 }
1309
Chris Lattner75dfb652010-11-23 09:19:42 +00001310 FileID FID = SourceMgr.createFileID(File, ReadSourceLocation(*F, Record[1]),
1311 (SrcMgr::CharacteristicKind)Record[2],
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001312 ID, Record[0]);
1313 if (Record[3])
1314 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile())
1315 .setHasLineDirectives();
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001316
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001317 break;
1318 }
1319
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001320 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001321 const char *Name = BlobStart;
1322 unsigned Offset = Record[0];
1323 unsigned Code = SLocEntryCursor.ReadCode();
1324 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001325 unsigned RecCode
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001326 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001327
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001328 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001329 Error("AST record has invalid code");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001330 return Failure;
1331 }
1332
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001333 llvm::MemoryBuffer *Buffer
Chris Lattnera0a270c2010-04-05 22:42:27 +00001334 = llvm::MemoryBuffer::getMemBuffer(llvm::StringRef(BlobStart, BlobLen - 1),
1335 Name);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001336 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID, Offset);
Mike Stump1eb44332009-09-09 15:08:12 +00001337
Douglas Gregor92b059e2009-04-28 20:33:11 +00001338 if (strcmp(Name, "<built-in>") == 0) {
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +00001339 PCHPredefinesBlock Block = {
1340 BufferID,
1341 llvm::StringRef(BlobStart, BlobLen - 1)
1342 };
1343 PCHPredefinesBuffers.push_back(Block);
Douglas Gregor92b059e2009-04-28 20:33:11 +00001344 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001345
1346 break;
1347 }
1348
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001349 case SM_SLOC_INSTANTIATION_ENTRY: {
Sebastian Redlc3632732010-10-05 15:59:54 +00001350 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001351 SourceMgr.createInstantiationLoc(SpellingLoc,
Sebastian Redlc3632732010-10-05 15:59:54 +00001352 ReadSourceLocation(*F, Record[2]),
1353 ReadSourceLocation(*F, Record[3]),
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001354 Record[4],
1355 ID,
1356 Record[0]);
1357 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001358 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001359 }
1360
1361 return Success;
1362}
1363
Chris Lattner6367f6d2009-04-27 01:05:14 +00001364/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1365/// specified cursor. Read the abbreviations that are at the top of the block
1366/// and then leave the cursor pointing into the block.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001367bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattner6367f6d2009-04-27 01:05:14 +00001368 unsigned BlockID) {
1369 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001370 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001371 return Failure;
1372 }
Mike Stump1eb44332009-09-09 15:08:12 +00001373
Chris Lattner6367f6d2009-04-27 01:05:14 +00001374 while (true) {
Douglas Gregorecdcb882010-10-20 22:00:55 +00001375 uint64_t Offset = Cursor.GetCurrentBitNo();
Chris Lattner6367f6d2009-04-27 01:05:14 +00001376 unsigned Code = Cursor.ReadCode();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001377
Chris Lattner6367f6d2009-04-27 01:05:14 +00001378 // We expect all abbrevs to be at the start of the block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001379 if (Code != llvm::bitc::DEFINE_ABBREV) {
1380 Cursor.JumpToBit(Offset);
Chris Lattner6367f6d2009-04-27 01:05:14 +00001381 return false;
Douglas Gregorecdcb882010-10-20 22:00:55 +00001382 }
Chris Lattner6367f6d2009-04-27 01:05:14 +00001383 Cursor.ReadAbbrevRecord();
1384 }
1385}
1386
Douglas Gregor89d99802010-11-30 06:16:57 +00001387PreprocessedEntity *ASTReader::ReadMacroRecord(PerFileData &F, uint64_t Offset) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001388 assert(PP && "Forgot to set Preprocessor ?");
Douglas Gregorecdcb882010-10-20 22:00:55 +00001389 llvm::BitstreamCursor &Stream = F.MacroCursor;
Mike Stump1eb44332009-09-09 15:08:12 +00001390
Douglas Gregor37e26842009-04-21 23:56:24 +00001391 // Keep track of where we are in the stream, then jump back there
1392 // after reading this macro.
1393 SavedStreamPosition SavedPosition(Stream);
1394
1395 Stream.JumpToBit(Offset);
1396 RecordData Record;
1397 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1398 MacroInfo *Macro = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001399
Douglas Gregor37e26842009-04-21 23:56:24 +00001400 while (true) {
1401 unsigned Code = Stream.ReadCode();
1402 switch (Code) {
1403 case llvm::bitc::END_BLOCK:
Douglas Gregor89d99802010-11-30 06:16:57 +00001404 return 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001405
1406 case llvm::bitc::ENTER_SUBBLOCK:
1407 // No known subblocks, always skip them.
1408 Stream.ReadSubBlockID();
1409 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001410 Error("malformed block record in AST file");
Douglas Gregor89d99802010-11-30 06:16:57 +00001411 return 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001412 }
1413 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001414
Douglas Gregor37e26842009-04-21 23:56:24 +00001415 case llvm::bitc::DEFINE_ABBREV:
1416 Stream.ReadAbbrevRecord();
1417 continue;
1418 default: break;
1419 }
1420
1421 // Read a record.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001422 const char *BlobStart = 0;
1423 unsigned BlobLen = 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001424 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001425 PreprocessorRecordTypes RecType =
Michael J. Spencer20249a12010-10-21 03:16:25 +00001426 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart,
Douglas Gregorecdcb882010-10-20 22:00:55 +00001427 BlobLen);
Douglas Gregor37e26842009-04-21 23:56:24 +00001428 switch (RecType) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001429 case PP_MACRO_OBJECT_LIKE:
1430 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001431 // If we already have a macro, that means that we've hit the end
1432 // of the definition of the macro we were looking for. We're
1433 // done.
1434 if (Macro)
Douglas Gregor89d99802010-11-30 06:16:57 +00001435 return 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001436
1437 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1438 if (II == 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001439 Error("macro must have a name in AST file");
Douglas Gregor89d99802010-11-30 06:16:57 +00001440 return 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001441 }
Sebastian Redlc3632732010-10-05 15:59:54 +00001442 SourceLocation Loc = ReadSourceLocation(F, Record[1]);
Douglas Gregor37e26842009-04-21 23:56:24 +00001443 bool isUsed = Record[2];
Mike Stump1eb44332009-09-09 15:08:12 +00001444
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001445 MacroInfo *MI = PP->AllocateMacroInfo(Loc);
Douglas Gregor37e26842009-04-21 23:56:24 +00001446 MI->setIsUsed(isUsed);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001447 MI->setIsFromAST();
Mike Stump1eb44332009-09-09 15:08:12 +00001448
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001449 unsigned NextIndex = 3;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001450 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregor37e26842009-04-21 23:56:24 +00001451 // Decode function-like macro info.
1452 bool isC99VarArgs = Record[3];
1453 bool isGNUVarArgs = Record[4];
1454 MacroArgs.clear();
1455 unsigned NumArgs = Record[5];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001456 NextIndex = 6 + NumArgs;
Douglas Gregor37e26842009-04-21 23:56:24 +00001457 for (unsigned i = 0; i != NumArgs; ++i)
1458 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1459
1460 // Install function-like macro info.
1461 MI->setIsFunctionLike();
1462 if (isC99VarArgs) MI->setIsC99Varargs();
1463 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor75fdb232009-05-22 22:45:36 +00001464 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001465 PP->getPreprocessorAllocator());
Douglas Gregor37e26842009-04-21 23:56:24 +00001466 }
1467
1468 // Finally, install the macro.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001469 PP->setMacroInfo(II, MI);
Douglas Gregor37e26842009-04-21 23:56:24 +00001470
1471 // Remember that we saw this macro last so that we add the tokens that
1472 // form its body to it.
1473 Macro = MI;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001474
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001475 if (NextIndex + 1 == Record.size() && PP->getPreprocessingRecord()) {
1476 // We have a macro definition. Load it now.
1477 PP->getPreprocessingRecord()->RegisterMacroDefinition(Macro,
1478 getMacroDefinition(Record[NextIndex]));
1479 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001480
Douglas Gregor37e26842009-04-21 23:56:24 +00001481 ++NumMacrosRead;
1482 break;
1483 }
Mike Stump1eb44332009-09-09 15:08:12 +00001484
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001485 case PP_TOKEN: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001486 // If we see a TOKEN before a PP_MACRO_*, then the file is
1487 // erroneous, just pretend we didn't see this.
1488 if (Macro == 0) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001489
Douglas Gregor37e26842009-04-21 23:56:24 +00001490 Token Tok;
1491 Tok.startToken();
Sebastian Redlc3632732010-10-05 15:59:54 +00001492 Tok.setLocation(ReadSourceLocation(F, Record[0]));
Douglas Gregor37e26842009-04-21 23:56:24 +00001493 Tok.setLength(Record[1]);
1494 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1495 Tok.setIdentifierInfo(II);
1496 Tok.setKind((tok::TokenKind)Record[3]);
1497 Tok.setFlag((Token::TokenFlags)Record[4]);
1498 Macro->AddTokenToBody(Tok);
1499 break;
1500 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001501 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001502 }
Douglas Gregor89d99802010-11-30 06:16:57 +00001503
1504 return 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001505}
1506
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001507PreprocessedEntity *ASTReader::LoadPreprocessedEntity(PerFileData &F) {
1508 assert(PP && "Forgot to set Preprocessor ?");
1509 unsigned Code = F.PreprocessorDetailCursor.ReadCode();
1510 switch (Code) {
1511 case llvm::bitc::END_BLOCK:
1512 return 0;
1513
1514 case llvm::bitc::ENTER_SUBBLOCK:
1515 Error("unexpected subblock record in preprocessor detail block");
1516 return 0;
1517
1518 case llvm::bitc::DEFINE_ABBREV:
1519 Error("unexpected abbrevation record in preprocessor detail block");
1520 return 0;
1521
1522 default:
1523 break;
1524 }
1525
1526 if (!PP->getPreprocessingRecord()) {
1527 Error("no preprocessing record");
1528 return 0;
1529 }
1530
1531 // Read the record.
1532 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1533 const char *BlobStart = 0;
1534 unsigned BlobLen = 0;
1535 RecordData Record;
1536 PreprocessorDetailRecordTypes RecType =
1537 (PreprocessorDetailRecordTypes)F.PreprocessorDetailCursor.ReadRecord(
1538 Code, Record, BlobStart, BlobLen);
1539 switch (RecType) {
1540 case PPD_MACRO_INSTANTIATION: {
1541 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1542 return PE;
1543
1544 MacroInstantiation *MI
1545 = new (PPRec) MacroInstantiation(DecodeIdentifierInfo(Record[3]),
1546 SourceRange(ReadSourceLocation(F, Record[1]),
1547 ReadSourceLocation(F, Record[2])),
1548 getMacroDefinition(Record[4]));
1549 PPRec.SetPreallocatedEntity(Record[0], MI);
1550 return MI;
1551 }
1552
1553 case PPD_MACRO_DEFINITION: {
1554 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1555 return PE;
1556
1557 if (Record[1] > MacroDefinitionsLoaded.size()) {
1558 Error("out-of-bounds macro definition record");
1559 return 0;
1560 }
1561
1562 // Decode the identifier info and then check again; if the macro is
1563 // still defined and associated with the identifier,
1564 IdentifierInfo *II = DecodeIdentifierInfo(Record[4]);
1565 if (!MacroDefinitionsLoaded[Record[1] - 1]) {
1566 MacroDefinition *MD
1567 = new (PPRec) MacroDefinition(II,
1568 ReadSourceLocation(F, Record[5]),
1569 SourceRange(
1570 ReadSourceLocation(F, Record[2]),
1571 ReadSourceLocation(F, Record[3])));
1572
1573 PPRec.SetPreallocatedEntity(Record[0], MD);
1574 MacroDefinitionsLoaded[Record[1] - 1] = MD;
1575
1576 if (DeserializationListener)
1577 DeserializationListener->MacroDefinitionRead(Record[1], MD);
1578 }
1579
1580 return MacroDefinitionsLoaded[Record[1] - 1];
1581 }
1582
1583 case PPD_INCLUSION_DIRECTIVE: {
1584 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0]))
1585 return PE;
1586
1587 const char *FullFileNameStart = BlobStart + Record[3];
1588 const FileEntry *File
1589 = PP->getFileManager().getFile(llvm::StringRef(FullFileNameStart,
1590 BlobLen - Record[3]));
1591
1592 // FIXME: Stable encoding
1593 InclusionDirective::InclusionKind Kind
1594 = static_cast<InclusionDirective::InclusionKind>(Record[5]);
1595 InclusionDirective *ID
1596 = new (PPRec) InclusionDirective(PPRec, Kind,
1597 llvm::StringRef(BlobStart, Record[3]),
1598 Record[4],
1599 File,
1600 SourceRange(ReadSourceLocation(F, Record[1]),
1601 ReadSourceLocation(F, Record[2])));
1602 PPRec.SetPreallocatedEntity(Record[0], ID);
1603 return ID;
1604 }
1605 }
1606
1607 Error("invalid offset in preprocessor detail block");
1608 return 0;
1609}
1610
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001611namespace {
1612 /// \brief Trait class used to search the on-disk hash table containing all of
1613 /// the header search information.
1614 ///
1615 /// The on-disk hash table contains a mapping from each header path to
1616 /// information about that header (how many times it has been included, its
1617 /// controlling macro, etc.). Note that we actually hash based on the
1618 /// filename, and support "deep" comparisons of file names based on current
1619 /// inode numbers, so that the search can cope with non-normalized path names
1620 /// and symlinks.
1621 class HeaderFileInfoTrait {
1622 const char *SearchPath;
1623 struct stat SearchPathStatBuf;
1624 llvm::Optional<int> SearchPathStatResult;
1625
1626 int StatSimpleCache(const char *Path, struct stat *StatBuf) {
1627 if (Path == SearchPath) {
1628 if (!SearchPathStatResult)
1629 SearchPathStatResult = stat(Path, &SearchPathStatBuf);
1630
1631 *StatBuf = SearchPathStatBuf;
1632 return *SearchPathStatResult;
1633 }
1634
1635 return stat(Path, StatBuf);
1636 }
1637
1638 public:
1639 typedef const char *external_key_type;
1640 typedef const char *internal_key_type;
1641
1642 typedef HeaderFileInfo data_type;
1643
1644 HeaderFileInfoTrait(const char *SearchPath = 0) : SearchPath(SearchPath) { }
1645
1646 static unsigned ComputeHash(const char *path) {
1647 return llvm::HashString(llvm::sys::path::filename(path));
1648 }
1649
1650 static internal_key_type GetInternalKey(const char *path) { return path; }
1651
1652 bool EqualKey(internal_key_type a, internal_key_type b) {
1653 if (strcmp(a, b) == 0)
1654 return true;
1655
1656 if (llvm::sys::path::filename(a) != llvm::sys::path::filename(b))
1657 return false;
1658
1659 // The file names match, but the path names don't. stat() the files to
1660 // see if they are the same.
1661 struct stat StatBufA, StatBufB;
1662 if (StatSimpleCache(a, &StatBufA) || StatSimpleCache(b, &StatBufB))
1663 return false;
1664
1665 return StatBufA.st_ino == StatBufB.st_ino;
1666 }
1667
1668 static std::pair<unsigned, unsigned>
1669 ReadKeyDataLength(const unsigned char*& d) {
1670 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1671 unsigned DataLen = (unsigned) *d++;
1672 return std::make_pair(KeyLen + 1, DataLen);
1673 }
1674
1675 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
1676 return (const char *)d;
1677 }
1678
1679 static data_type ReadData(const internal_key_type, const unsigned char *d,
1680 unsigned DataLen) {
1681 const unsigned char *End = d + DataLen;
1682 using namespace clang::io;
1683 HeaderFileInfo HFI;
1684 unsigned Flags = *d++;
1685 HFI.isImport = (Flags >> 3) & 0x01;
1686 HFI.DirInfo = (Flags >> 1) & 0x03;
1687 HFI.Resolved = Flags & 0x01;
1688 HFI.NumIncludes = ReadUnalignedLE16(d);
1689 HFI.ControllingMacroID = ReadUnalignedLE32(d);
1690 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1691 (void)End;
1692
1693 // This HeaderFileInfo was externally loaded.
1694 HFI.External = true;
1695 return HFI;
1696 }
1697 };
1698}
1699
1700/// \brief The on-disk hash table used for the global method pool.
1701typedef OnDiskChainedHashTable<HeaderFileInfoTrait>
1702 HeaderFileInfoLookupTable;
1703
Douglas Gregor295a2a62010-10-30 00:23:06 +00001704void ASTReader::SetIdentifierIsMacro(IdentifierInfo *II, PerFileData &F,
1705 uint64_t Offset) {
1706 // Note that this identifier has a macro definition.
1707 II->setHasMacroDefinition(true);
1708
1709 // Adjust the offset based on our position in the chain.
1710 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1711 if (Chain[I] == &F)
1712 break;
1713
1714 Offset += Chain[I]->SizeInBits;
1715 }
1716
1717 UnreadMacroRecordOffsets[II] = Offset;
1718}
1719
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001720void ASTReader::ReadDefinedMacros() {
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001721 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redlc3632732010-10-05 15:59:54 +00001722 PerFileData &F = *Chain[N - I - 1];
1723 llvm::BitstreamCursor &MacroCursor = F.MacroCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001724
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001725 // If there was no preprocessor block, skip this file.
1726 if (!MacroCursor.getBitStreamReader())
1727 continue;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001728
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001729 llvm::BitstreamCursor Cursor = MacroCursor;
Douglas Gregorecdcb882010-10-20 22:00:55 +00001730 Cursor.JumpToBit(F.MacroStartOffset);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001731
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001732 RecordData Record;
1733 while (true) {
1734 unsigned Code = Cursor.ReadCode();
Douglas Gregorecdcb882010-10-20 22:00:55 +00001735 if (Code == llvm::bitc::END_BLOCK)
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001736 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001737
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001738 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1739 // No known subblocks, always skip them.
1740 Cursor.ReadSubBlockID();
1741 if (Cursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001742 Error("malformed block record in AST file");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001743 return;
1744 }
1745 continue;
1746 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001747
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001748 if (Code == llvm::bitc::DEFINE_ABBREV) {
1749 Cursor.ReadAbbrevRecord();
1750 continue;
1751 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001752
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001753 // Read a record.
1754 const char *BlobStart;
1755 unsigned BlobLen;
1756 Record.clear();
1757 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1758 default: // Default behavior: ignore.
1759 break;
Douglas Gregor88a35862010-01-04 19:18:44 +00001760
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001761 case PP_MACRO_OBJECT_LIKE:
1762 case PP_MACRO_FUNCTION_LIKE:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001763 DecodeIdentifierInfo(Record[0]);
1764 break;
1765
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001766 case PP_TOKEN:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001767 // Ignore tokens.
1768 break;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001769 }
Douglas Gregor88a35862010-01-04 19:18:44 +00001770 }
1771 }
Douglas Gregor295a2a62010-10-30 00:23:06 +00001772
1773 // Drain the unread macro-record offsets map.
1774 while (!UnreadMacroRecordOffsets.empty())
1775 LoadMacroDefinition(UnreadMacroRecordOffsets.begin());
1776}
1777
1778void ASTReader::LoadMacroDefinition(
1779 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos) {
1780 assert(Pos != UnreadMacroRecordOffsets.end() && "Unknown macro definition");
1781 PerFileData *F = 0;
1782 uint64_t Offset = Pos->second;
1783 UnreadMacroRecordOffsets.erase(Pos);
1784
1785 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1786 if (Offset < Chain[I]->SizeInBits) {
1787 F = Chain[I];
1788 break;
1789 }
1790
1791 Offset -= Chain[I]->SizeInBits;
1792 }
1793 if (!F) {
1794 Error("Malformed macro record offset");
1795 return;
1796 }
1797
1798 ReadMacroRecord(*F, Offset);
1799}
1800
1801void ASTReader::LoadMacroDefinition(IdentifierInfo *II) {
1802 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos
1803 = UnreadMacroRecordOffsets.find(II);
1804 LoadMacroDefinition(Pos);
Douglas Gregor88a35862010-01-04 19:18:44 +00001805}
1806
Sebastian Redlf73c93f2010-09-15 19:54:06 +00001807MacroDefinition *ASTReader::getMacroDefinition(MacroID ID) {
Douglas Gregor77424bc2010-10-02 19:29:26 +00001808 if (ID == 0 || ID > MacroDefinitionsLoaded.size())
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001809 return 0;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001810
Douglas Gregor77424bc2010-10-02 19:29:26 +00001811 if (!MacroDefinitionsLoaded[ID - 1]) {
1812 unsigned Index = ID - 1;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001813 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1814 PerFileData &F = *Chain[N - I - 1];
1815 if (Index < F.LocalNumMacroDefinitions) {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001816 SavedStreamPosition SavedPosition(F.PreprocessorDetailCursor);
1817 F.PreprocessorDetailCursor.JumpToBit(F.MacroDefinitionOffsets[Index]);
1818 LoadPreprocessedEntity(F);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001819 break;
1820 }
1821 Index -= F.LocalNumMacroDefinitions;
1822 }
Douglas Gregor77424bc2010-10-02 19:29:26 +00001823 assert(MacroDefinitionsLoaded[ID - 1] && "Broken chain");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001824 }
1825
Douglas Gregor77424bc2010-10-02 19:29:26 +00001826 return MacroDefinitionsLoaded[ID - 1];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001827}
1828
Douglas Gregore650c8c2009-07-07 00:12:59 +00001829/// \brief If we are loading a relocatable PCH file, and the filename is
1830/// not an absolute path, add the system root to the beginning of the file
1831/// name.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001832void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001833 // If this is not a relocatable PCH file, there's nothing to do.
1834 if (!RelocatablePCH)
1835 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001836
Michael J. Spencer256053b2010-12-17 21:22:22 +00001837 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
Douglas Gregore650c8c2009-07-07 00:12:59 +00001838 return;
1839
Douglas Gregore650c8c2009-07-07 00:12:59 +00001840 if (isysroot == 0) {
1841 // If no system root was given, default to '/'
1842 Filename.insert(Filename.begin(), '/');
1843 return;
1844 }
Mike Stump1eb44332009-09-09 15:08:12 +00001845
Douglas Gregore650c8c2009-07-07 00:12:59 +00001846 unsigned Length = strlen(isysroot);
1847 if (isysroot[Length - 1] != '/')
1848 Filename.insert(Filename.begin(), '/');
Mike Stump1eb44332009-09-09 15:08:12 +00001849
Douglas Gregore650c8c2009-07-07 00:12:59 +00001850 Filename.insert(Filename.begin(), isysroot, isysroot + Length);
1851}
1852
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001853ASTReader::ASTReadResult
Sebastian Redl571db7f2010-08-18 23:56:56 +00001854ASTReader::ReadASTBlock(PerFileData &F) {
Sebastian Redl9137a522010-07-16 17:50:48 +00001855 llvm::BitstreamCursor &Stream = F.Stream;
1856
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001857 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001858 Error("malformed block record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001859 return Failure;
1860 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001861
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001862 // Read all of the records and blocks for the ASt file.
Douglas Gregor8038d512009-04-10 17:25:41 +00001863 RecordData Record;
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001864 bool First = true;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001865 while (!Stream.AtEndOfStream()) {
1866 unsigned Code = Stream.ReadCode();
1867 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001868 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001869 Error("error at end of module block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001870 return Failure;
1871 }
Chris Lattner7356a312009-04-11 21:15:38 +00001872
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001873 return Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001874 }
1875
1876 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1877 switch (Stream.ReadSubBlockID()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001878 case DECLTYPES_BLOCK_ID:
Chris Lattner6367f6d2009-04-27 01:05:14 +00001879 // We lazily load the decls block, but we want to set up the
1880 // DeclsCursor cursor to point into it. Clone our current bitcode
1881 // cursor to it, enter the block and read the abbrevs in that block.
1882 // With the main cursor, we just skip over it.
Sebastian Redl9137a522010-07-16 17:50:48 +00001883 F.DeclsCursor = Stream;
Chris Lattner6367f6d2009-04-27 01:05:14 +00001884 if (Stream.SkipBlock() || // Skip with the main cursor.
1885 // Read the abbrevs.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001886 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001887 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001888 return Failure;
1889 }
1890 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001891
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00001892 case DECL_UPDATES_BLOCK_ID:
1893 if (Stream.SkipBlock()) {
1894 Error("malformed block record in AST file");
1895 return Failure;
1896 }
1897 break;
1898
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001899 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl9137a522010-07-16 17:50:48 +00001900 F.MacroCursor = Stream;
Douglas Gregor88a35862010-01-04 19:18:44 +00001901 if (PP)
1902 PP->setExternalSource(this);
1903
Douglas Gregorecdcb882010-10-20 22:00:55 +00001904 if (Stream.SkipBlock() ||
1905 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001906 Error("malformed block record in AST file");
Chris Lattner7356a312009-04-11 21:15:38 +00001907 return Failure;
1908 }
Douglas Gregorecdcb882010-10-20 22:00:55 +00001909 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
Chris Lattner7356a312009-04-11 21:15:38 +00001910 break;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00001911
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001912 case PREPROCESSOR_DETAIL_BLOCK_ID:
1913 F.PreprocessorDetailCursor = Stream;
1914 if (Stream.SkipBlock() ||
1915 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
1916 PREPROCESSOR_DETAIL_BLOCK_ID)) {
1917 Error("malformed preprocessor detail record in AST file");
1918 return Failure;
1919 }
1920 F.PreprocessorDetailStartOffset
1921 = F.PreprocessorDetailCursor.GetCurrentBitNo();
1922 break;
1923
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001924 case SOURCE_MANAGER_BLOCK_ID:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001925 switch (ReadSourceManagerBlock(F)) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00001926 case Success:
1927 break;
1928
1929 case Failure:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001930 Error("malformed source manager block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001931 return Failure;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001932
1933 case IgnorePCH:
1934 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001935 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001936 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001937 }
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001938 First = false;
Douglas Gregor8038d512009-04-10 17:25:41 +00001939 continue;
1940 }
1941
1942 if (Code == llvm::bitc::DEFINE_ABBREV) {
1943 Stream.ReadAbbrevRecord();
1944 continue;
1945 }
1946
1947 // Read and process a record.
1948 Record.clear();
Douglas Gregor2bec0412009-04-10 21:16:55 +00001949 const char *BlobStart = 0;
1950 unsigned BlobLen = 0;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001951 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00001952 &BlobStart, &BlobLen)) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001953 default: // Default behavior: ignore.
1954 break;
1955
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001956 case METADATA: {
1957 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1958 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001959 : diag::warn_pch_version_too_new);
1960 return IgnorePCH;
1961 }
1962
1963 RelocatablePCH = Record[4];
1964 if (Listener) {
1965 std::string TargetTriple(BlobStart, BlobLen);
1966 if (Listener->ReadTargetTriple(TargetTriple))
1967 return IgnorePCH;
1968 }
1969 break;
1970 }
1971
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001972 case CHAINED_METADATA: {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001973 if (!First) {
1974 Error("CHAINED_METADATA is not first record in block");
1975 return Failure;
1976 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001977 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1978 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001979 : diag::warn_pch_version_too_new);
1980 return IgnorePCH;
1981 }
1982
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00001983 // Load the chained file, which is always a PCH file.
1984 switch(ReadASTCore(llvm::StringRef(BlobStart, BlobLen), PCH)) {
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001985 case Failure: return Failure;
1986 // If we have to ignore the dependency, we'll have to ignore this too.
1987 case IgnorePCH: return IgnorePCH;
1988 case Success: break;
1989 }
1990 break;
1991 }
1992
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001993 case TYPE_OFFSET:
Sebastian Redl12d6da02010-07-19 22:06:55 +00001994 if (F.LocalNumTypes != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001995 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001996 return Failure;
1997 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00001998 F.TypeOffsets = (const uint32_t *)BlobStart;
1999 F.LocalNumTypes = Record[0];
Douglas Gregor8038d512009-04-10 17:25:41 +00002000 break;
2001
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002002 case DECL_OFFSET:
Sebastian Redl12d6da02010-07-19 22:06:55 +00002003 if (F.LocalNumDecls != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002004 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002005 return Failure;
2006 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00002007 F.DeclOffsets = (const uint32_t *)BlobStart;
2008 F.LocalNumDecls = Record[0];
Douglas Gregor8038d512009-04-10 17:25:41 +00002009 break;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002010
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002011 case TU_UPDATE_LEXICAL: {
Sebastian Redld692af72010-07-27 18:24:41 +00002012 DeclContextInfo Info = {
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002013 /* No visible information */ 0,
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002014 reinterpret_cast<const KindDeclIDPair *>(BlobStart),
2015 BlobLen / sizeof(KindDeclIDPair)
Sebastian Redld692af72010-07-27 18:24:41 +00002016 };
Douglas Gregor3747ee72010-10-01 01:18:02 +00002017 DeclContextOffsets[Context ? Context->getTranslationUnitDecl() : 0]
2018 .push_back(Info);
Sebastian Redld692af72010-07-27 18:24:41 +00002019 break;
2020 }
2021
Sebastian Redle1dde812010-08-24 00:50:04 +00002022 case UPDATE_VISIBLE: {
2023 serialization::DeclID ID = Record[0];
2024 void *Table = ASTDeclContextNameLookupTable::Create(
2025 (const unsigned char *)BlobStart + Record[1],
2026 (const unsigned char *)BlobStart,
2027 ASTDeclContextNameLookupTrait(*this));
Douglas Gregor3747ee72010-10-01 01:18:02 +00002028 if (ID == 1 && Context) { // Is it the TU?
Sebastian Redle1dde812010-08-24 00:50:04 +00002029 DeclContextInfo Info = {
2030 Table, /* No lexical inforamtion */ 0, 0
2031 };
2032 DeclContextOffsets[Context->getTranslationUnitDecl()].push_back(Info);
2033 } else
2034 PendingVisibleUpdates[ID].push_back(Table);
2035 break;
2036 }
2037
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002038 case REDECLS_UPDATE_LATEST: {
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00002039 assert(Record.size() % 2 == 0 && "Expected pairs of DeclIDs");
2040 for (unsigned i = 0, e = Record.size(); i < e; i += 2) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002041 DeclID First = Record[i], Latest = Record[i+1];
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00002042 assert((FirstLatestDeclIDs.find(First) == FirstLatestDeclIDs.end() ||
2043 Latest > FirstLatestDeclIDs[First]) &&
2044 "The new latest is supposed to come after the previous latest");
2045 FirstLatestDeclIDs[First] = Latest;
2046 }
2047 break;
2048 }
2049
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002050 case LANGUAGE_OPTIONS:
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00002051 if (ParseLanguageOptions(Record) && !DisableValidation)
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002052 return IgnorePCH;
2053 break;
Douglas Gregor2bec0412009-04-10 21:16:55 +00002054
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002055 case IDENTIFIER_TABLE:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002056 F.IdentifierTableData = BlobStart;
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002057 if (Record[0]) {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002058 F.IdentifierLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002059 = ASTIdentifierLookupTable::Create(
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002060 (const unsigned char *)F.IdentifierTableData + Record[0],
2061 (const unsigned char *)F.IdentifierTableData,
Sebastian Redlc3632732010-10-05 15:59:54 +00002062 ASTIdentifierLookupTrait(*this, F));
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002063 if (PP)
2064 PP->getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002065 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002066 break;
2067
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002068 case IDENTIFIER_OFFSET:
Sebastian Redl2da08f92010-07-19 22:28:42 +00002069 if (F.LocalNumIdentifiers != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002070 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00002071 return Failure;
2072 }
Sebastian Redl2da08f92010-07-19 22:28:42 +00002073 F.IdentifierOffsets = (const uint32_t *)BlobStart;
2074 F.LocalNumIdentifiers = Record[0];
Douglas Gregorafaf3082009-04-11 00:14:32 +00002075 break;
Douglas Gregorfdd01722009-04-14 00:24:19 +00002076
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002077 case EXTERNAL_DEFINITIONS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002078 // Optimization for the first block.
2079 if (ExternalDefinitions.empty())
2080 ExternalDefinitions.swap(Record);
2081 else
2082 ExternalDefinitions.insert(ExternalDefinitions.end(),
2083 Record.begin(), Record.end());
Douglas Gregorfdd01722009-04-14 00:24:19 +00002084 break;
Douglas Gregor3e1af842009-04-17 22:13:46 +00002085
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002086 case SPECIAL_TYPES:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002087 // Optimization for the first block
2088 if (SpecialTypes.empty())
2089 SpecialTypes.swap(Record);
2090 else
2091 SpecialTypes.insert(SpecialTypes.end(), Record.begin(), Record.end());
Douglas Gregorad1de002009-04-18 05:55:16 +00002092 break;
2093
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002094 case STATISTICS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002095 TotalNumStatements += Record[0];
2096 TotalNumMacros += Record[1];
2097 TotalLexicalDeclContexts += Record[2];
2098 TotalVisibleDeclContexts += Record[3];
Douglas Gregor3e1af842009-04-17 22:13:46 +00002099 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002100
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002101 case TENTATIVE_DEFINITIONS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002102 // Optimization for the first block.
2103 if (TentativeDefinitions.empty())
2104 TentativeDefinitions.swap(Record);
2105 else
2106 TentativeDefinitions.insert(TentativeDefinitions.end(),
2107 Record.begin(), Record.end());
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002108 break;
Douglas Gregor14c22f22009-04-22 22:18:58 +00002109
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002110 case UNUSED_FILESCOPED_DECLS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002111 // Optimization for the first block.
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002112 if (UnusedFileScopedDecls.empty())
2113 UnusedFileScopedDecls.swap(Record);
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002114 else
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002115 UnusedFileScopedDecls.insert(UnusedFileScopedDecls.end(),
2116 Record.begin(), Record.end());
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002117 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002118
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002119 case WEAK_UNDECLARED_IDENTIFIERS:
Sebastian Redl40566802010-08-05 18:21:25 +00002120 // Later blocks overwrite earlier ones.
2121 WeakUndeclaredIdentifiers.swap(Record);
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002122 break;
2123
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002124 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002125 // Optimization for the first block.
2126 if (LocallyScopedExternalDecls.empty())
2127 LocallyScopedExternalDecls.swap(Record);
2128 else
2129 LocallyScopedExternalDecls.insert(LocallyScopedExternalDecls.end(),
2130 Record.begin(), Record.end());
Douglas Gregor14c22f22009-04-22 22:18:58 +00002131 break;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002132
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002133 case SELECTOR_OFFSETS:
Sebastian Redl059612d2010-08-03 21:58:15 +00002134 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redl725cd962010-08-04 20:40:17 +00002135 F.LocalNumSelectors = Record[0];
Douglas Gregor83941df2009-04-25 17:48:32 +00002136 break;
2137
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002138 case METHOD_POOL:
Sebastian Redl725cd962010-08-04 20:40:17 +00002139 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor83941df2009-04-25 17:48:32 +00002140 if (Record[0])
Sebastian Redl725cd962010-08-04 20:40:17 +00002141 F.SelectorLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002142 = ASTSelectorLookupTable::Create(
Sebastian Redl725cd962010-08-04 20:40:17 +00002143 F.SelectorLookupTableData + Record[0],
2144 F.SelectorLookupTableData,
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002145 ASTSelectorLookupTrait(*this));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002146 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002147 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002148
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002149 case REFERENCED_SELECTOR_POOL:
Sebastian Redlc3632732010-10-05 15:59:54 +00002150 F.ReferencedSelectorsData.swap(Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002151 break;
2152
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002153 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002154 if (!Record.empty() && Listener)
2155 Listener->ReadCounter(Record[0]);
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002156 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002157
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002158 case SOURCE_LOCATION_OFFSETS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002159 F.SLocOffsets = (const uint32_t *)BlobStart;
2160 F.LocalNumSLocEntries = Record[0];
Sebastian Redl8db9fae2010-09-22 20:19:08 +00002161 F.LocalSLocSize = Record[1];
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002162 break;
2163
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002164 case SOURCE_LOCATION_PRELOADS:
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002165 if (PreloadSLocEntries.empty())
2166 PreloadSLocEntries.swap(Record);
2167 else
2168 PreloadSLocEntries.insert(PreloadSLocEntries.end(),
2169 Record.begin(), Record.end());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002170 break;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002171
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002172 case STAT_CACHE: {
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00002173 if (!DisableStatCache) {
2174 ASTStatCache *MyStatCache =
2175 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
2176 (const unsigned char *)BlobStart,
2177 NumStatHits, NumStatMisses);
2178 FileMgr.addStatCache(MyStatCache);
2179 F.StatCache = MyStatCache;
2180 }
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002181 break;
Douglas Gregor52e71082009-10-16 18:18:30 +00002182 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002183
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002184 case EXT_VECTOR_DECLS:
Sebastian Redla9f23682010-07-28 21:38:49 +00002185 // Optimization for the first block.
2186 if (ExtVectorDecls.empty())
2187 ExtVectorDecls.swap(Record);
2188 else
2189 ExtVectorDecls.insert(ExtVectorDecls.end(),
2190 Record.begin(), Record.end());
Douglas Gregorb81c1702009-04-27 20:06:05 +00002191 break;
2192
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002193 case VTABLE_USES:
Sebastian Redl40566802010-08-05 18:21:25 +00002194 // Later tables overwrite earlier ones.
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002195 VTableUses.swap(Record);
2196 break;
2197
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002198 case DYNAMIC_CLASSES:
Sebastian Redl40566802010-08-05 18:21:25 +00002199 // Optimization for the first block.
2200 if (DynamicClasses.empty())
2201 DynamicClasses.swap(Record);
2202 else
2203 DynamicClasses.insert(DynamicClasses.end(),
2204 Record.begin(), Record.end());
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002205 break;
2206
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002207 case PENDING_IMPLICIT_INSTANTIATIONS:
Sebastian Redlc3632732010-10-05 15:59:54 +00002208 F.PendingInstantiations.swap(Record);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002209 break;
2210
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002211 case SEMA_DECL_REFS:
Sebastian Redl40566802010-08-05 18:21:25 +00002212 // Later tables overwrite earlier ones.
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002213 SemaDeclRefs.swap(Record);
2214 break;
2215
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002216 case ORIGINAL_FILE_NAME:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002217 // The primary AST will be the last to get here, so it will be the one
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002218 // that's used.
Daniel Dunbar7b5a1212009-11-11 05:29:04 +00002219 ActualOriginalFileName.assign(BlobStart, BlobLen);
2220 OriginalFileName = ActualOriginalFileName;
Douglas Gregore650c8c2009-07-07 00:12:59 +00002221 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregorb64c1932009-05-12 01:31:05 +00002222 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002223
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002224 case ORIGINAL_PCH_DIR:
2225 // The primary AST will be the last to get here, so it will be the one
2226 // that's used.
2227 OriginalDir.assign(BlobStart, BlobLen);
2228 break;
2229
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002230 case VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek974be4d2010-02-12 23:31:14 +00002231 const std::string &CurBranch = getClangFullRepositoryVersion();
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002232 llvm::StringRef ASTBranch(BlobStart, BlobLen);
2233 if (llvm::StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2234 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregor445e23e2009-10-05 21:07:28 +00002235 return IgnorePCH;
2236 }
2237 break;
2238 }
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002239
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002240 case MACRO_DEFINITION_OFFSETS:
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002241 F.MacroDefinitionOffsets = (const uint32_t *)BlobStart;
2242 F.NumPreallocatedPreprocessingEntities = Record[0];
2243 F.LocalNumMacroDefinitions = Record[1];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002244 break;
Sebastian Redl0b17c612010-08-13 00:28:03 +00002245
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002246 case DECL_UPDATE_OFFSETS: {
2247 if (Record.size() % 2 != 0) {
2248 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2249 return Failure;
2250 }
2251 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
2252 DeclUpdateOffsets[static_cast<DeclID>(Record[I])]
2253 .push_back(std::make_pair(&F, Record[I+1]));
2254 break;
2255 }
2256
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002257 case DECL_REPLACEMENTS: {
Sebastian Redl0b17c612010-08-13 00:28:03 +00002258 if (Record.size() % 2 != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002259 Error("invalid DECL_REPLACEMENTS block in AST file");
Sebastian Redl0b17c612010-08-13 00:28:03 +00002260 return Failure;
2261 }
2262 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002263 ReplacedDecls[static_cast<DeclID>(Record[I])] =
Sebastian Redl0b17c612010-08-13 00:28:03 +00002264 std::make_pair(&F, Record[I+1]);
2265 break;
2266 }
Douglas Gregor7c789c12010-10-29 22:39:52 +00002267
2268 case CXX_BASE_SPECIFIER_OFFSETS: {
2269 if (F.LocalNumCXXBaseSpecifiers != 0) {
2270 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2271 return Failure;
2272 }
2273
2274 F.LocalNumCXXBaseSpecifiers = Record[0];
2275 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart;
2276 break;
2277 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002278
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002279 case DIAG_PRAGMA_MAPPINGS:
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002280 if (Record.size() % 2 != 0) {
2281 Error("invalid DIAG_USER_MAPPINGS block in AST file");
2282 return Failure;
2283 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002284 if (PragmaDiagMappings.empty())
2285 PragmaDiagMappings.swap(Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002286 else
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002287 PragmaDiagMappings.insert(PragmaDiagMappings.end(),
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002288 Record.begin(), Record.end());
2289 break;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002290
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002291 case CUDA_SPECIAL_DECL_REFS:
2292 // Later tables overwrite earlier ones.
2293 CUDASpecialDeclRefs.swap(Record);
2294 break;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002295
2296 case HEADER_SEARCH_TABLE:
2297 F.HeaderFileInfoTableData = BlobStart;
2298 F.LocalNumHeaderFileInfos = Record[1];
2299 if (Record[0]) {
2300 F.HeaderFileInfoTable
2301 = HeaderFileInfoLookupTable::Create(
2302 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
2303 (const unsigned char *)F.HeaderFileInfoTableData);
2304 if (PP)
2305 PP->getHeaderSearchInfo().SetExternalSource(this);
2306 }
2307 break;
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002308
2309 case FP_PRAGMA_OPTIONS:
2310 // Later tables overwrite earlier ones.
2311 FPPragmaOptions.swap(Record);
2312 break;
2313
2314 case OPENCL_EXTENSIONS:
2315 // Later tables overwrite earlier ones.
2316 OpenCLExtensions.swap(Record);
2317 break;
Douglas Gregorafaf3082009-04-11 00:14:32 +00002318 }
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002319 First = false;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002320 }
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002321 Error("premature end of bitstream in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002322 return Failure;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002323}
2324
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002325ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
2326 ASTFileType Type) {
2327 switch(ReadASTCore(FileName, Type)) {
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002328 case Failure: return Failure;
2329 case IgnorePCH: return IgnorePCH;
2330 case Success: break;
2331 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002332
2333 // Here comes stuff that we only do once the entire chain is loaded.
2334
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002335 // Allocate space for loaded slocentries, identifiers, decls and types.
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002336 unsigned TotalNumIdentifiers = 0, TotalNumTypes = 0, TotalNumDecls = 0,
Sebastian Redl725cd962010-08-04 20:40:17 +00002337 TotalNumPreallocatedPreprocessingEntities = 0, TotalNumMacroDefs = 0,
2338 TotalNumSelectors = 0;
Sebastian Redl12d6da02010-07-19 22:06:55 +00002339 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002340 TotalNumSLocEntries += Chain[I]->LocalNumSLocEntries;
Sebastian Redl8db9fae2010-09-22 20:19:08 +00002341 NextSLocOffset += Chain[I]->LocalSLocSize;
Sebastian Redl2da08f92010-07-19 22:28:42 +00002342 TotalNumIdentifiers += Chain[I]->LocalNumIdentifiers;
Sebastian Redl12d6da02010-07-19 22:06:55 +00002343 TotalNumTypes += Chain[I]->LocalNumTypes;
2344 TotalNumDecls += Chain[I]->LocalNumDecls;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002345 TotalNumPreallocatedPreprocessingEntities +=
2346 Chain[I]->NumPreallocatedPreprocessingEntities;
2347 TotalNumMacroDefs += Chain[I]->LocalNumMacroDefinitions;
Sebastian Redl725cd962010-08-04 20:40:17 +00002348 TotalNumSelectors += Chain[I]->LocalNumSelectors;
Sebastian Redl12d6da02010-07-19 22:06:55 +00002349 }
Sebastian Redl8db9fae2010-09-22 20:19:08 +00002350 SourceMgr.PreallocateSLocEntries(this, TotalNumSLocEntries, NextSLocOffset);
Sebastian Redl2da08f92010-07-19 22:28:42 +00002351 IdentifiersLoaded.resize(TotalNumIdentifiers);
Sebastian Redl12d6da02010-07-19 22:06:55 +00002352 TypesLoaded.resize(TotalNumTypes);
2353 DeclsLoaded.resize(TotalNumDecls);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002354 MacroDefinitionsLoaded.resize(TotalNumMacroDefs);
2355 if (PP) {
2356 if (TotalNumIdentifiers > 0)
2357 PP->getHeaderSearchInfo().SetExternalLookup(this);
2358 if (TotalNumPreallocatedPreprocessingEntities > 0) {
2359 if (!PP->getPreprocessingRecord())
2360 PP->createPreprocessingRecord();
2361 PP->getPreprocessingRecord()->SetExternalSource(*this,
2362 TotalNumPreallocatedPreprocessingEntities);
2363 }
2364 }
Sebastian Redl725cd962010-08-04 20:40:17 +00002365 SelectorsLoaded.resize(TotalNumSelectors);
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002366 // Preload SLocEntries.
2367 for (unsigned I = 0, N = PreloadSLocEntries.size(); I != N; ++I) {
2368 ASTReadResult Result = ReadSLocEntryRecord(PreloadSLocEntries[I]);
2369 if (Result != Success)
2370 return Result;
2371 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00002372
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002373 // Check the predefines buffers.
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00002374 if (!DisableValidation && CheckPredefinesBuffers())
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002375 return IgnorePCH;
2376
2377 if (PP) {
2378 // Initialization of keywords and pragmas occurs before the
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002379 // AST file is read, so there may be some identifiers that were
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002380 // loaded into the IdentifierTable before we intercepted the
2381 // creation of identifiers. Iterate through the list of known
2382 // identifiers and determine whether we have to establish
2383 // preprocessor definitions or top-level identifier declaration
2384 // chains for those identifiers.
2385 //
2386 // We copy the IdentifierInfo pointers to a small vector first,
2387 // since de-serializing declarations or macro definitions can add
2388 // new entries into the identifier table, invalidating the
2389 // iterators.
2390 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
2391 for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(),
2392 IdEnd = PP->getIdentifierTable().end();
2393 Id != IdEnd; ++Id)
2394 Identifiers.push_back(Id->second);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002395 // We need to search the tables in all files.
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002396 for (unsigned J = 0, M = Chain.size(); J != M; ++J) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002397 ASTIdentifierLookupTable *IdTable
2398 = (ASTIdentifierLookupTable *)Chain[J]->IdentifierLookupTable;
2399 // Not all AST files necessarily have identifier tables, only the useful
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00002400 // ones.
2401 if (!IdTable)
2402 continue;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002403 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
2404 IdentifierInfo *II = Identifiers[I];
2405 // Look in the on-disk hash tables for an entry for this identifier
Sebastian Redlc3632732010-10-05 15:59:54 +00002406 ASTIdentifierLookupTrait Info(*this, *Chain[J], II);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002407 std::pair<const char*,unsigned> Key(II->getNameStart(),II->getLength());
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002408 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002409 if (Pos == IdTable->end())
2410 continue;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002411
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002412 // Dereferencing the iterator has the effect of populating the
2413 // IdentifierInfo node with the various declarations it needs.
2414 (void)*Pos;
2415 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002416 }
2417 }
2418
2419 if (Context)
2420 InitializeContext(*Context);
2421
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002422 if (DeserializationListener)
2423 DeserializationListener->ReaderInitialized(this);
2424
Douglas Gregor414cb642010-11-30 05:23:00 +00002425 // If this AST file is a precompiled preamble, then set the main file ID of
2426 // the source manager to the file source file from which the preamble was
2427 // built. This is the only valid way to use a precompiled preamble.
2428 if (Type == Preamble) {
2429 SourceLocation Loc
2430 = SourceMgr.getLocation(FileMgr.getFile(getOriginalSourceFile()), 1, 1);
2431 if (Loc.isValid()) {
2432 std::pair<FileID, unsigned> Decomposed = SourceMgr.getDecomposedLoc(Loc);
2433 SourceMgr.SetPreambleFileID(Decomposed.first);
2434 }
2435 }
2436
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002437 return Success;
2438}
2439
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002440ASTReader::ASTReadResult ASTReader::ReadASTCore(llvm::StringRef FileName,
2441 ASTFileType Type) {
Sebastian Redla866e652010-10-01 19:59:12 +00002442 PerFileData *Prev = Chain.empty() ? 0 : Chain.back();
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002443 Chain.push_back(new PerFileData(Type));
Sebastian Redl9137a522010-07-16 17:50:48 +00002444 PerFileData &F = *Chain.back();
Sebastian Redla866e652010-10-01 19:59:12 +00002445 if (Prev)
2446 Prev->NextInSource = &F;
2447 else
2448 FirstInSource = &F;
2449 F.Loaders.push_back(Prev);
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002450
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002451 // Set the AST file name.
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002452 F.FileName = FileName;
2453
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002454 if (FileName != "-") {
2455 CurrentDir = llvm::sys::path::parent_path(FileName);
2456 if (CurrentDir.empty()) CurrentDir = ".";
2457 }
2458
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002459 // Open the AST file.
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002460 //
2461 // FIXME: This shouldn't be here, we should just take a raw_ostream.
2462 std::string ErrStr;
Michael J. Spencer3a321e22010-12-09 17:36:38 +00002463 llvm::error_code ec;
2464 if (FileName == "-") {
Michael J. Spencer4eeebc42010-12-16 03:28:14 +00002465 ec = llvm::MemoryBuffer::getSTDIN(F.Buffer);
Michael J. Spencer3a321e22010-12-09 17:36:38 +00002466 if (ec)
2467 ErrStr = ec.message();
2468 } else
Chris Lattner39b49bc2010-11-23 08:35:12 +00002469 F.Buffer.reset(FileMgr.getBufferForFile(FileName, &ErrStr));
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002470 if (!F.Buffer) {
2471 Error(ErrStr.c_str());
2472 return IgnorePCH;
2473 }
2474
2475 // Initialize the stream
2476 F.StreamFile.init((const unsigned char *)F.Buffer->getBufferStart(),
2477 (const unsigned char *)F.Buffer->getBufferEnd());
Sebastian Redl9137a522010-07-16 17:50:48 +00002478 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002479 Stream.init(F.StreamFile);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002480 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002481
2482 // Sniff for the signature.
2483 if (Stream.Read(8) != 'C' ||
2484 Stream.Read(8) != 'P' ||
2485 Stream.Read(8) != 'C' ||
2486 Stream.Read(8) != 'H') {
2487 Diag(diag::err_not_a_pch_file) << FileName;
2488 return Failure;
2489 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002490
Douglas Gregor2cf26342009-04-09 22:27:44 +00002491 while (!Stream.AtEndOfStream()) {
2492 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00002493
Douglas Gregore1d918e2009-04-10 23:10:45 +00002494 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002495 Error("invalid record at top-level of AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002496 return Failure;
2497 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002498
2499 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregor668c1a42009-04-21 22:25:48 +00002500
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002501 // We only know the AST subblock ID.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002502 switch (BlockID) {
2503 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002504 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002505 Error("malformed BlockInfoBlock in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002506 return Failure;
2507 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002508 break;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002509 case AST_BLOCK_ID:
Sebastian Redl571db7f2010-08-18 23:56:56 +00002510 switch (ReadASTBlock(F)) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002511 case Success:
2512 break;
2513
2514 case Failure:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002515 return Failure;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002516
2517 case IgnorePCH:
Douglas Gregor2bec0412009-04-10 21:16:55 +00002518 // FIXME: We could consider reading through to the end of this
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002519 // AST block, skipping subblocks, to see if there are other
2520 // AST blocks elsewhere.
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002521
2522 // Clear out any preallocated source location entries, so that
2523 // the source manager does not try to resolve them later.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002524 SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002525
2526 // Remove the stat cache.
Sebastian Redl9137a522010-07-16 17:50:48 +00002527 if (F.StatCache)
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002528 FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002529
Douglas Gregore1d918e2009-04-10 23:10:45 +00002530 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002531 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002532 break;
2533 default:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002534 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002535 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002536 return Failure;
2537 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002538 break;
2539 }
Mike Stump1eb44332009-09-09 15:08:12 +00002540 }
2541
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002542 return Success;
2543}
2544
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002545void ASTReader::setPreprocessor(Preprocessor &pp) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002546 PP = &pp;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002547
2548 unsigned TotalNum = 0;
2549 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
2550 TotalNum += Chain[I]->NumPreallocatedPreprocessingEntities;
2551 if (TotalNum) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002552 if (!PP->getPreprocessingRecord())
2553 PP->createPreprocessingRecord();
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002554 PP->getPreprocessingRecord()->SetExternalSource(*this, TotalNum);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002555 }
2556}
2557
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002558void ASTReader::InitializeContext(ASTContext &Ctx) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002559 Context = &Ctx;
2560 assert(Context && "Passed null context!");
2561
2562 assert(PP && "Forgot to set Preprocessor ?");
2563 PP->getIdentifierTable().setExternalIdentifierLookup(this);
2564 PP->getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregor88a35862010-01-04 19:18:44 +00002565 PP->setExternalSource(this);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002566 PP->getHeaderSearchInfo().SetExternalSource(this);
2567
Douglas Gregor3747ee72010-10-01 01:18:02 +00002568 // If we have an update block for the TU waiting, we have to add it before
2569 // deserializing the decl.
2570 DeclContextOffsetsMap::iterator DCU = DeclContextOffsets.find(0);
2571 if (DCU != DeclContextOffsets.end()) {
2572 // Insertion could invalidate map, so grab vector.
2573 DeclContextInfos T;
2574 T.swap(DCU->second);
2575 DeclContextOffsets.erase(DCU);
2576 DeclContextOffsets[Ctx.getTranslationUnitDecl()].swap(T);
2577 }
2578
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002579 // Load the translation unit declaration
Argyrios Kyrtzidis8871a442010-07-08 17:13:02 +00002580 GetTranslationUnitDecl();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002581
2582 // Load the special types.
2583 Context->setBuiltinVaListType(
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002584 GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST]));
2585 if (unsigned Id = SpecialTypes[SPECIAL_TYPE_OBJC_ID])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002586 Context->setObjCIdType(GetType(Id));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002587 if (unsigned Sel = SpecialTypes[SPECIAL_TYPE_OBJC_SELECTOR])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002588 Context->setObjCSelType(GetType(Sel));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002589 if (unsigned Proto = SpecialTypes[SPECIAL_TYPE_OBJC_PROTOCOL])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002590 Context->setObjCProtoType(GetType(Proto));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002591 if (unsigned Class = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002592 Context->setObjCClassType(GetType(Class));
Steve Naroff14108da2009-07-10 23:34:53 +00002593
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002594 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002595 Context->setCFConstantStringType(GetType(String));
Mike Stump1eb44332009-09-09 15:08:12 +00002596 if (unsigned FastEnum
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002597 = SpecialTypes[SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002598 Context->setObjCFastEnumerationStateType(GetType(FastEnum));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002599 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002600 QualType FileType = GetType(File);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002601 if (FileType.isNull()) {
2602 Error("FILE type is NULL");
2603 return;
2604 }
John McCall183700f2009-09-21 23:43:11 +00002605 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002606 Context->setFILEDecl(Typedef->getDecl());
2607 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002608 const TagType *Tag = FileType->getAs<TagType>();
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002609 if (!Tag) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002610 Error("Invalid FILE type in AST file");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002611 return;
2612 }
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002613 Context->setFILEDecl(Tag->getDecl());
2614 }
2615 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002616 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_jmp_buf]) {
Mike Stump782fa302009-07-28 02:25:19 +00002617 QualType Jmp_bufType = GetType(Jmp_buf);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002618 if (Jmp_bufType.isNull()) {
2619 Error("jmp_bug type is NULL");
2620 return;
2621 }
John McCall183700f2009-09-21 23:43:11 +00002622 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
Mike Stump782fa302009-07-28 02:25:19 +00002623 Context->setjmp_bufDecl(Typedef->getDecl());
2624 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002625 const TagType *Tag = Jmp_bufType->getAs<TagType>();
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002626 if (!Tag) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002627 Error("Invalid jmp_buf type in AST file");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002628 return;
2629 }
Mike Stump782fa302009-07-28 02:25:19 +00002630 Context->setjmp_bufDecl(Tag->getDecl());
2631 }
2632 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002633 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_sigjmp_buf]) {
Mike Stump782fa302009-07-28 02:25:19 +00002634 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002635 if (Sigjmp_bufType.isNull()) {
2636 Error("sigjmp_buf type is NULL");
2637 return;
2638 }
John McCall183700f2009-09-21 23:43:11 +00002639 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
Mike Stump782fa302009-07-28 02:25:19 +00002640 Context->setsigjmp_bufDecl(Typedef->getDecl());
2641 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002642 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002643 assert(Tag && "Invalid sigjmp_buf type in AST file");
Mike Stump782fa302009-07-28 02:25:19 +00002644 Context->setsigjmp_bufDecl(Tag->getDecl());
2645 }
2646 }
Mike Stump1eb44332009-09-09 15:08:12 +00002647 if (unsigned ObjCIdRedef
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002648 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION])
Douglas Gregord1571ac2009-08-21 00:27:50 +00002649 Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef);
Mike Stump1eb44332009-09-09 15:08:12 +00002650 if (unsigned ObjCClassRedef
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002651 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION])
Douglas Gregord1571ac2009-08-21 00:27:50 +00002652 Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002653 if (unsigned String = SpecialTypes[SPECIAL_TYPE_BLOCK_DESCRIPTOR])
Mike Stumpadaaad32009-10-20 02:12:22 +00002654 Context->setBlockDescriptorType(GetType(String));
Mike Stump083c25e2009-10-22 00:49:09 +00002655 if (unsigned String
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002656 = SpecialTypes[SPECIAL_TYPE_BLOCK_EXTENDED_DESCRIPTOR])
Mike Stump083c25e2009-10-22 00:49:09 +00002657 Context->setBlockDescriptorExtendedType(GetType(String));
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002658 if (unsigned ObjCSelRedef
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002659 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION])
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002660 Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002661 if (unsigned String = SpecialTypes[SPECIAL_TYPE_NS_CONSTANT_STRING])
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002662 Context->setNSConstantStringType(GetType(String));
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +00002663
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002664 if (SpecialTypes[SPECIAL_TYPE_INT128_INSTALLED])
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +00002665 Context->setInt128Installed();
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002666
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002667 ReadPragmaDiagnosticMappings(Context->getDiagnostics());
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002668
2669 // If there were any CUDA special declarations, deserialize them.
2670 if (!CUDASpecialDeclRefs.empty()) {
2671 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
2672 Context->setcudaConfigureCallDecl(
2673 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
2674 }
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002675}
2676
Douglas Gregorb64c1932009-05-12 01:31:05 +00002677/// \brief Retrieve the name of the original source file name
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002678/// directly from the AST file, without actually loading the AST
Douglas Gregorb64c1932009-05-12 01:31:05 +00002679/// file.
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002680std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +00002681 FileManager &FileMgr,
Daniel Dunbar93ebb1b2009-12-03 09:13:06 +00002682 Diagnostic &Diags) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002683 // Open the AST file.
Douglas Gregorb64c1932009-05-12 01:31:05 +00002684 std::string ErrStr;
2685 llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
Chris Lattner39b49bc2010-11-23 08:35:12 +00002686 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
Douglas Gregorb64c1932009-05-12 01:31:05 +00002687 if (!Buffer) {
Daniel Dunbar93ebb1b2009-12-03 09:13:06 +00002688 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002689 return std::string();
2690 }
2691
2692 // Initialize the stream
2693 llvm::BitstreamReader StreamFile;
2694 llvm::BitstreamCursor Stream;
Mike Stump1eb44332009-09-09 15:08:12 +00002695 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregorb64c1932009-05-12 01:31:05 +00002696 (const unsigned char *)Buffer->getBufferEnd());
2697 Stream.init(StreamFile);
2698
2699 // Sniff for the signature.
2700 if (Stream.Read(8) != 'C' ||
2701 Stream.Read(8) != 'P' ||
2702 Stream.Read(8) != 'C' ||
2703 Stream.Read(8) != 'H') {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002704 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002705 return std::string();
2706 }
2707
2708 RecordData Record;
2709 while (!Stream.AtEndOfStream()) {
2710 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00002711
Douglas Gregorb64c1932009-05-12 01:31:05 +00002712 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2713 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump1eb44332009-09-09 15:08:12 +00002714
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002715 // We only know the AST subblock ID.
Douglas Gregorb64c1932009-05-12 01:31:05 +00002716 switch (BlockID) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002717 case AST_BLOCK_ID:
2718 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002719 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002720 return std::string();
2721 }
2722 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002723
Douglas Gregorb64c1932009-05-12 01:31:05 +00002724 default:
2725 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002726 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002727 return std::string();
2728 }
2729 break;
2730 }
2731 continue;
2732 }
2733
2734 if (Code == llvm::bitc::END_BLOCK) {
2735 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002736 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002737 return std::string();
2738 }
2739 continue;
2740 }
2741
2742 if (Code == llvm::bitc::DEFINE_ABBREV) {
2743 Stream.ReadAbbrevRecord();
2744 continue;
2745 }
2746
2747 Record.clear();
2748 const char *BlobStart = 0;
2749 unsigned BlobLen = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002750 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002751 == ORIGINAL_FILE_NAME)
Douglas Gregorb64c1932009-05-12 01:31:05 +00002752 return std::string(BlobStart, BlobLen);
Mike Stump1eb44332009-09-09 15:08:12 +00002753 }
Douglas Gregorb64c1932009-05-12 01:31:05 +00002754
2755 return std::string();
2756}
2757
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002758/// \brief Parse the record that corresponds to a LangOptions data
2759/// structure.
2760///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002761/// This routine parses the language options from the AST file and then gives
2762/// them to the AST listener if one is set.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002763///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002764/// \returns true if the listener deems the file unacceptable, false otherwise.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002765bool ASTReader::ParseLanguageOptions(
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002766 const llvm::SmallVectorImpl<uint64_t> &Record) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002767 if (Listener) {
2768 LangOptions LangOpts;
Mike Stump1eb44332009-09-09 15:08:12 +00002769
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002770 #define PARSE_LANGOPT(Option) \
2771 LangOpts.Option = Record[Idx]; \
2772 ++Idx
Mike Stump1eb44332009-09-09 15:08:12 +00002773
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002774 unsigned Idx = 0;
2775 PARSE_LANGOPT(Trigraphs);
2776 PARSE_LANGOPT(BCPLComment);
2777 PARSE_LANGOPT(DollarIdents);
2778 PARSE_LANGOPT(AsmPreprocessor);
2779 PARSE_LANGOPT(GNUMode);
Chandler Carrutheb5d7b72010-04-17 20:17:31 +00002780 PARSE_LANGOPT(GNUKeywords);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002781 PARSE_LANGOPT(ImplicitInt);
2782 PARSE_LANGOPT(Digraphs);
2783 PARSE_LANGOPT(HexFloats);
2784 PARSE_LANGOPT(C99);
2785 PARSE_LANGOPT(Microsoft);
2786 PARSE_LANGOPT(CPlusPlus);
2787 PARSE_LANGOPT(CPlusPlus0x);
2788 PARSE_LANGOPT(CXXOperatorNames);
2789 PARSE_LANGOPT(ObjC1);
2790 PARSE_LANGOPT(ObjC2);
2791 PARSE_LANGOPT(ObjCNonFragileABI);
Fariborz Jahanian412e7982010-02-09 19:31:38 +00002792 PARSE_LANGOPT(ObjCNonFragileABI2);
Fariborz Jahanianf84109e2011-01-07 18:59:25 +00002793 PARSE_LANGOPT(AppleKext);
Ted Kremenekc32647d2010-12-23 21:35:43 +00002794 PARSE_LANGOPT(ObjCDefaultSynthProperties);
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +00002795 PARSE_LANGOPT(NoConstantCFStrings);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002796 PARSE_LANGOPT(PascalStrings);
2797 PARSE_LANGOPT(WritableStrings);
2798 PARSE_LANGOPT(LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +00002799 PARSE_LANGOPT(AltiVec);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002800 PARSE_LANGOPT(Exceptions);
Daniel Dunbar73482882010-02-10 18:48:44 +00002801 PARSE_LANGOPT(SjLjExceptions);
Douglas Gregor6f755502011-02-01 15:15:22 +00002802 PARSE_LANGOPT(MSBitfields);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002803 PARSE_LANGOPT(NeXTRuntime);
2804 PARSE_LANGOPT(Freestanding);
2805 PARSE_LANGOPT(NoBuiltin);
2806 PARSE_LANGOPT(ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +00002807 PARSE_LANGOPT(POSIXThreads);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002808 PARSE_LANGOPT(Blocks);
2809 PARSE_LANGOPT(EmitAllDecls);
2810 PARSE_LANGOPT(MathErrno);
Chris Lattnera4d71452010-06-26 21:25:03 +00002811 LangOpts.setSignedOverflowBehavior((LangOptions::SignedOverflowBehaviorTy)
2812 Record[Idx++]);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002813 PARSE_LANGOPT(HeinousExtensions);
2814 PARSE_LANGOPT(Optimize);
2815 PARSE_LANGOPT(OptimizeSize);
2816 PARSE_LANGOPT(Static);
2817 PARSE_LANGOPT(PICLevel);
2818 PARSE_LANGOPT(GNUInline);
2819 PARSE_LANGOPT(NoInline);
2820 PARSE_LANGOPT(AccessControl);
2821 PARSE_LANGOPT(CharIsSigned);
John Thompsona6fda122009-11-05 20:14:16 +00002822 PARSE_LANGOPT(ShortWChar);
Argyrios Kyrtzidisb1bdced2011-01-15 02:56:16 +00002823 PARSE_LANGOPT(ShortEnums);
Chris Lattnera4d71452010-06-26 21:25:03 +00002824 LangOpts.setGCMode((LangOptions::GCMode)Record[Idx++]);
John McCall1fb0caa2010-10-22 21:05:15 +00002825 LangOpts.setVisibilityMode((Visibility)Record[Idx++]);
Daniel Dunbarab8e2812009-09-21 04:16:19 +00002826 LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode)
Chris Lattnera4d71452010-06-26 21:25:03 +00002827 Record[Idx++]);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002828 PARSE_LANGOPT(InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +00002829 PARSE_LANGOPT(OpenCL);
Peter Collingbourne08a53262010-12-01 19:14:57 +00002830 PARSE_LANGOPT(CUDA);
Mike Stump9c276ae2009-12-12 01:27:46 +00002831 PARSE_LANGOPT(CatchUndefined);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002832 PARSE_LANGOPT(DefaultFPContract);
Mike Stump9c276ae2009-12-12 01:27:46 +00002833 // FIXME: Missing ElideConstructors?!
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002834 #undef PARSE_LANGOPT
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002835
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002836 return Listener->ReadLanguageOptions(LangOpts);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002837 }
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002838
2839 return false;
2840}
2841
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002842void ASTReader::ReadPreprocessedEntities() {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002843 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2844 PerFileData &F = *Chain[I];
2845 if (!F.PreprocessorDetailCursor.getBitStreamReader())
2846 continue;
2847
2848 SavedStreamPosition SavedPosition(F.PreprocessorDetailCursor);
2849 F.PreprocessorDetailCursor.JumpToBit(F.PreprocessorDetailStartOffset);
2850 while (LoadPreprocessedEntity(F)) { }
2851 }
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002852}
2853
Douglas Gregor0a480292011-02-11 19:46:30 +00002854PreprocessedEntity *ASTReader::ReadPreprocessedEntityAtOffset(uint64_t Offset) {
Douglas Gregor89d99802010-11-30 06:16:57 +00002855 PerFileData *F = 0;
2856 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2857 if (Offset < Chain[I]->SizeInBits) {
2858 F = Chain[I];
2859 break;
2860 }
2861
2862 Offset -= Chain[I]->SizeInBits;
2863 }
2864
2865 if (!F) {
2866 Error("Malformed preprocessed entity offset");
2867 return 0;
2868 }
2869
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002870 // Keep track of where we are in the stream, then jump back there
2871 // after reading this entity.
2872 SavedStreamPosition SavedPosition(F->PreprocessorDetailCursor);
2873 F->PreprocessorDetailCursor.JumpToBit(Offset);
2874 return LoadPreprocessedEntity(*F);
Douglas Gregor89d99802010-11-30 06:16:57 +00002875}
2876
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002877HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
2878 HeaderFileInfoTrait Trait(FE->getName());
2879 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2880 PerFileData &F = *Chain[I];
2881 HeaderFileInfoLookupTable *Table
2882 = static_cast<HeaderFileInfoLookupTable *>(F.HeaderFileInfoTable);
2883 if (!Table)
2884 continue;
2885
2886 // Look in the on-disk hash table for an entry for this file name.
2887 HeaderFileInfoLookupTable::iterator Pos = Table->find(FE->getName(),
2888 &Trait);
2889 if (Pos == Table->end())
2890 continue;
2891
2892 HeaderFileInfo HFI = *Pos;
2893 if (Listener)
2894 Listener->ReadHeaderFileInfo(HFI, FE->getUID());
2895
2896 return HFI;
2897 }
2898
2899 return HeaderFileInfo();
2900}
2901
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002902void ASTReader::ReadPragmaDiagnosticMappings(Diagnostic &Diag) {
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002903 unsigned Idx = 0;
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002904 while (Idx < PragmaDiagMappings.size()) {
2905 SourceLocation
2906 Loc = SourceLocation::getFromRawEncoding(PragmaDiagMappings[Idx++]);
2907 while (1) {
2908 assert(Idx < PragmaDiagMappings.size() &&
2909 "Invalid data, didn't find '-1' marking end of diag/map pairs");
2910 if (Idx >= PragmaDiagMappings.size())
2911 break; // Something is messed up but at least avoid infinite loop in
2912 // release build.
2913 unsigned DiagID = PragmaDiagMappings[Idx++];
2914 if (DiagID == (unsigned)-1)
2915 break; // no more diag/map pairs for this location.
2916 diag::Mapping Map = (diag::Mapping)PragmaDiagMappings[Idx++];
2917 Diag.setDiagnosticMapping(DiagID, Map, Loc);
2918 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002919 }
2920}
2921
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002922/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002923ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002924 PerFileData *F = 0;
2925 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2926 F = Chain[N - I - 1];
2927 if (Index < F->LocalNumTypes)
2928 break;
2929 Index -= F->LocalNumTypes;
2930 }
2931 assert(F && F->LocalNumTypes > Index && "Broken chain");
Sebastian Redlc3632732010-10-05 15:59:54 +00002932 return RecordLocation(F, F->TypeOffsets[Index]);
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002933}
2934
2935/// \brief Read and return the type with the given index..
Douglas Gregor2cf26342009-04-09 22:27:44 +00002936///
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002937/// The index is the type ID, shifted and minus the number of predefs. This
2938/// routine actually reads the record corresponding to the type at the given
2939/// location. It is a helper routine for GetType, which deals with reading type
2940/// IDs.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002941QualType ASTReader::ReadTypeRecord(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002942 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00002943 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00002944
Douglas Gregor0b748912009-04-14 21:18:50 +00002945 // Keep track of where we are in the stream, then jump back there
2946 // after reading this type.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002947 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregor0b748912009-04-14 21:18:50 +00002948
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00002949 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redl27372b42010-08-11 18:52:41 +00002950
Douglas Gregord89275b2009-07-06 18:54:52 +00002951 // Note that we are loading a type record.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00002952 Deserializing AType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00002953
Sebastian Redlc3632732010-10-05 15:59:54 +00002954 DeclsCursor.JumpToBit(Loc.Offset);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002955 RecordData Record;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002956 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002957 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
2958 case TYPE_EXT_QUAL: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002959 if (Record.size() != 2) {
2960 Error("Incorrect encoding of extended qualifier type");
2961 return QualType();
2962 }
Douglas Gregor6d473962009-04-15 22:00:08 +00002963 QualType Base = GetType(Record[0]);
John McCall0953e762009-09-24 19:53:00 +00002964 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[1]);
2965 return Context->getQualifiedType(Base, Quals);
Douglas Gregor6d473962009-04-15 22:00:08 +00002966 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002967
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002968 case TYPE_COMPLEX: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002969 if (Record.size() != 1) {
2970 Error("Incorrect encoding of complex type");
2971 return QualType();
2972 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002973 QualType ElemType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002974 return Context->getComplexType(ElemType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002975 }
2976
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002977 case TYPE_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002978 if (Record.size() != 1) {
2979 Error("Incorrect encoding of pointer type");
2980 return QualType();
2981 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002982 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002983 return Context->getPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002984 }
2985
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002986 case TYPE_BLOCK_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002987 if (Record.size() != 1) {
2988 Error("Incorrect encoding of block pointer type");
2989 return QualType();
2990 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002991 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002992 return Context->getBlockPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002993 }
2994
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002995 case TYPE_LVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002996 if (Record.size() != 1) {
2997 Error("Incorrect encoding of lvalue reference type");
2998 return QualType();
2999 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003000 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003001 return Context->getLValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003002 }
3003
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003004 case TYPE_RVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003005 if (Record.size() != 1) {
3006 Error("Incorrect encoding of rvalue reference type");
3007 return QualType();
3008 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003009 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003010 return Context->getRValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003011 }
3012
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003013 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidis240437b2010-07-02 11:55:15 +00003014 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003015 Error("Incorrect encoding of member pointer type");
3016 return QualType();
3017 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003018 QualType PointeeType = GetType(Record[0]);
3019 QualType ClassType = GetType(Record[1]);
Douglas Gregor1ab55e92010-12-10 17:03:06 +00003020 if (PointeeType.isNull() || ClassType.isNull())
3021 return QualType();
3022
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003023 return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregor2cf26342009-04-09 22:27:44 +00003024 }
3025
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003026 case TYPE_CONSTANT_ARRAY: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003027 QualType ElementType = GetType(Record[0]);
3028 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3029 unsigned IndexTypeQuals = Record[2];
3030 unsigned Idx = 3;
3031 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003032 return Context->getConstantArrayType(ElementType, Size,
3033 ASM, IndexTypeQuals);
3034 }
3035
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003036 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003037 QualType ElementType = GetType(Record[0]);
3038 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3039 unsigned IndexTypeQuals = Record[2];
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003040 return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003041 }
3042
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003043 case TYPE_VARIABLE_ARRAY: {
Douglas Gregor0b748912009-04-14 21:18:50 +00003044 QualType ElementType = GetType(Record[0]);
3045 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3046 unsigned IndexTypeQuals = Record[2];
Sebastian Redlc3632732010-10-05 15:59:54 +00003047 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
3048 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
3049 return Context->getVariableArrayType(ElementType, ReadExpr(*Loc.F),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003050 ASM, IndexTypeQuals,
3051 SourceRange(LBLoc, RBLoc));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003052 }
3053
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003054 case TYPE_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00003055 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003056 Error("incorrect encoding of vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003057 return QualType();
3058 }
3059
3060 QualType ElementType = GetType(Record[0]);
3061 unsigned NumElements = Record[1];
Bob Wilsone86d78c2010-11-10 21:56:12 +00003062 unsigned VecKind = Record[2];
Chris Lattner788b0fd2010-06-23 06:00:24 +00003063 return Context->getVectorType(ElementType, NumElements,
Bob Wilsone86d78c2010-11-10 21:56:12 +00003064 (VectorType::VectorKind)VecKind);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003065 }
3066
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003067 case TYPE_EXT_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00003068 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003069 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003070 return QualType();
3071 }
3072
3073 QualType ElementType = GetType(Record[0]);
3074 unsigned NumElements = Record[1];
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003075 return Context->getExtVectorType(ElementType, NumElements);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003076 }
3077
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003078 case TYPE_FUNCTION_NO_PROTO: {
Rafael Espindola425ef722010-03-30 22:15:11 +00003079 if (Record.size() != 4) {
Douglas Gregora02b1472009-04-28 21:53:25 +00003080 Error("incorrect encoding of no-proto function type");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003081 return QualType();
3082 }
3083 QualType ResultType = GetType(Record[0]);
Rafael Espindola425ef722010-03-30 22:15:11 +00003084 FunctionType::ExtInfo Info(Record[1], Record[2], (CallingConv)Record[3]);
Rafael Espindola264ba482010-03-30 20:24:48 +00003085 return Context->getFunctionNoProtoType(ResultType, Info);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003086 }
3087
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003088 case TYPE_FUNCTION_PROTO: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003089 QualType ResultType = GetType(Record[0]);
John McCalle23cf432010-12-14 08:05:40 +00003090
3091 FunctionProtoType::ExtProtoInfo EPI;
3092 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
3093 /*regparm*/ Record[2],
3094 static_cast<CallingConv>(Record[3]));
3095
Rafael Espindola425ef722010-03-30 22:15:11 +00003096 unsigned Idx = 4;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003097 unsigned NumParams = Record[Idx++];
3098 llvm::SmallVector<QualType, 16> ParamTypes;
3099 for (unsigned I = 0; I != NumParams; ++I)
3100 ParamTypes.push_back(GetType(Record[Idx++]));
John McCalle23cf432010-12-14 08:05:40 +00003101
3102 EPI.Variadic = Record[Idx++];
3103 EPI.TypeQuals = Record[Idx++];
Douglas Gregorc938c162011-01-26 05:01:58 +00003104 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
John McCalle23cf432010-12-14 08:05:40 +00003105 EPI.HasExceptionSpec = Record[Idx++];
3106 EPI.HasAnyExceptionSpec = Record[Idx++];
3107 EPI.NumExceptions = Record[Idx++];
Sebastian Redl465226e2009-05-27 22:11:52 +00003108 llvm::SmallVector<QualType, 2> Exceptions;
John McCalle23cf432010-12-14 08:05:40 +00003109 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
Sebastian Redl465226e2009-05-27 22:11:52 +00003110 Exceptions.push_back(GetType(Record[Idx++]));
John McCalle23cf432010-12-14 08:05:40 +00003111 EPI.Exceptions = Exceptions.data();
Jay Foadbeaaccd2009-05-21 09:52:38 +00003112 return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams,
John McCalle23cf432010-12-14 08:05:40 +00003113 EPI);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003114 }
3115
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003116 case TYPE_UNRESOLVED_USING:
John McCalled976492009-12-04 22:46:56 +00003117 return Context->getTypeDeclType(
3118 cast<UnresolvedUsingTypenameDecl>(GetDecl(Record[0])));
3119
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003120 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003121 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003122 Error("incorrect encoding of typedef type");
3123 return QualType();
3124 }
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003125 TypedefDecl *Decl = cast<TypedefDecl>(GetDecl(Record[0]));
3126 QualType Canonical = GetType(Record[1]);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00003127 if (!Canonical.isNull())
3128 Canonical = Context->getCanonicalType(Canonical);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003129 return Context->getTypedefType(Decl, Canonical);
3130 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003131
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003132 case TYPE_TYPEOF_EXPR:
Sebastian Redlc3632732010-10-05 15:59:54 +00003133 return Context->getTypeOfExprType(ReadExpr(*Loc.F));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003134
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003135 case TYPE_TYPEOF: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003136 if (Record.size() != 1) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003137 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003138 return QualType();
3139 }
3140 QualType UnderlyingType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003141 return Context->getTypeOfType(UnderlyingType);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003142 }
Mike Stump1eb44332009-09-09 15:08:12 +00003143
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003144 case TYPE_DECLTYPE:
Sebastian Redlc3632732010-10-05 15:59:54 +00003145 return Context->getDecltypeType(ReadExpr(*Loc.F));
Anders Carlsson395b4752009-06-24 19:06:50 +00003146
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003147 case TYPE_RECORD: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003148 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003149 Error("incorrect encoding of record type");
3150 return QualType();
3151 }
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003152 bool IsDependent = Record[0];
3153 QualType T = Context->getRecordType(cast<RecordDecl>(GetDecl(Record[1])));
John McCallf4c73712011-01-19 06:33:43 +00003154 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003155 return T;
3156 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003157
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003158 case TYPE_ENUM: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003159 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003160 Error("incorrect encoding of enum type");
3161 return QualType();
3162 }
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003163 bool IsDependent = Record[0];
3164 QualType T = Context->getEnumType(cast<EnumDecl>(GetDecl(Record[1])));
John McCallf4c73712011-01-19 06:33:43 +00003165 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003166 return T;
3167 }
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003168
John McCall9d156a72011-01-06 01:58:22 +00003169 case TYPE_ATTRIBUTED: {
3170 if (Record.size() != 3) {
3171 Error("incorrect encoding of attributed type");
3172 return QualType();
3173 }
3174 QualType modifiedType = GetType(Record[0]);
3175 QualType equivalentType = GetType(Record[1]);
3176 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
3177 return Context->getAttributedType(kind, modifiedType, equivalentType);
3178 }
3179
Abramo Bagnara075f8f12010-12-10 16:29:40 +00003180 case TYPE_PAREN: {
3181 if (Record.size() != 1) {
3182 Error("incorrect encoding of paren type");
3183 return QualType();
3184 }
3185 QualType InnerType = GetType(Record[0]);
3186 return Context->getParenType(InnerType);
3187 }
3188
Douglas Gregor7536dd52010-12-20 02:24:11 +00003189 case TYPE_PACK_EXPANSION: {
Douglas Gregorf9997a02011-02-01 15:24:58 +00003190 if (Record.size() != 2) {
Douglas Gregor7536dd52010-12-20 02:24:11 +00003191 Error("incorrect encoding of pack expansion type");
3192 return QualType();
3193 }
3194 QualType Pattern = GetType(Record[0]);
3195 if (Pattern.isNull())
3196 return QualType();
Douglas Gregorcded4f62011-01-14 17:04:44 +00003197 llvm::Optional<unsigned> NumExpansions;
3198 if (Record[1])
3199 NumExpansions = Record[1] - 1;
3200 return Context->getPackExpansionType(Pattern, NumExpansions);
Douglas Gregor7536dd52010-12-20 02:24:11 +00003201 }
3202
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003203 case TYPE_ELABORATED: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00003204 unsigned Idx = 0;
3205 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3206 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3207 QualType NamedType = GetType(Record[Idx++]);
3208 return Context->getElaboratedType(Keyword, NNS, NamedType);
John McCall7da24312009-09-05 00:15:47 +00003209 }
3210
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003211 case TYPE_OBJC_INTERFACE: {
Chris Lattnerc6fa4452009-04-22 06:45:28 +00003212 unsigned Idx = 0;
3213 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
John McCallc12c5bb2010-05-15 11:32:37 +00003214 return Context->getObjCInterfaceType(ItfD);
3215 }
3216
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003217 case TYPE_OBJC_OBJECT: {
John McCallc12c5bb2010-05-15 11:32:37 +00003218 unsigned Idx = 0;
3219 QualType Base = GetType(Record[Idx++]);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00003220 unsigned NumProtos = Record[Idx++];
3221 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
3222 for (unsigned I = 0; I != NumProtos; ++I)
3223 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
Michael J. Spencer20249a12010-10-21 03:16:25 +00003224 return Context->getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00003225 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003226
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003227 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00003228 unsigned Idx = 0;
John McCallc12c5bb2010-05-15 11:32:37 +00003229 QualType Pointee = GetType(Record[Idx++]);
3230 return Context->getObjCObjectPointerType(Pointee);
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00003231 }
Argyrios Kyrtzidis24fab412009-09-29 19:42:55 +00003232
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003233 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCall49a832b2009-10-18 09:09:24 +00003234 unsigned Idx = 0;
3235 QualType Parm = GetType(Record[Idx++]);
3236 QualType Replacement = GetType(Record[Idx++]);
3237 return
3238 Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
3239 Replacement);
3240 }
John McCall3cb0ebd2010-03-10 03:28:59 +00003241
Douglas Gregorc3069d62011-01-14 02:55:32 +00003242 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
3243 unsigned Idx = 0;
3244 QualType Parm = GetType(Record[Idx++]);
3245 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
3246 return Context->getSubstTemplateTypeParmPackType(
3247 cast<TemplateTypeParmType>(Parm),
3248 ArgPack);
3249 }
3250
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003251 case TYPE_INJECTED_CLASS_NAME: {
John McCall3cb0ebd2010-03-10 03:28:59 +00003252 CXXRecordDecl *D = cast<CXXRecordDecl>(GetDecl(Record[0]));
3253 QualType TST = GetType(Record[1]); // probably derivable
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00003254 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003255 // for AST reading, too much interdependencies.
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00003256 return
3257 QualType(new (*Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCall3cb0ebd2010-03-10 03:28:59 +00003258 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003259
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003260 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003261 unsigned Idx = 0;
3262 unsigned Depth = Record[Idx++];
3263 unsigned Index = Record[Idx++];
3264 bool Pack = Record[Idx++];
3265 IdentifierInfo *Name = GetIdentifierInfo(Record, Idx);
3266 return Context->getTemplateTypeParmType(Depth, Index, Pack, Name);
3267 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003268
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003269 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00003270 unsigned Idx = 0;
3271 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3272 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3273 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +00003274 QualType Canon = GetType(Record[Idx++]);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00003275 if (!Canon.isNull())
3276 Canon = Context->getCanonicalType(Canon);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +00003277 return Context->getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00003278 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003279
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003280 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00003281 unsigned Idx = 0;
3282 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
3283 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3284 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
3285 unsigned NumArgs = Record[Idx++];
3286 llvm::SmallVector<TemplateArgument, 8> Args;
3287 Args.reserve(NumArgs);
3288 while (NumArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00003289 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00003290 return Context->getDependentTemplateSpecializationType(Keyword, NNS, Name,
3291 Args.size(), Args.data());
3292 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003293
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003294 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00003295 unsigned Idx = 0;
3296
3297 // ArrayType
3298 QualType ElementType = GetType(Record[Idx++]);
3299 ArrayType::ArraySizeModifier ASM
3300 = (ArrayType::ArraySizeModifier)Record[Idx++];
3301 unsigned IndexTypeQuals = Record[Idx++];
3302
3303 // DependentSizedArrayType
Sebastian Redlc3632732010-10-05 15:59:54 +00003304 Expr *NumElts = ReadExpr(*Loc.F);
3305 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00003306
3307 return Context->getDependentSizedArrayType(ElementType, NumElts, ASM,
3308 IndexTypeQuals, Brackets);
3309 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003310
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003311 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003312 unsigned Idx = 0;
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003313 bool IsDependent = Record[Idx++];
Douglas Gregor1aee05d2011-01-15 06:45:20 +00003314 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003315 llvm::SmallVector<TemplateArgument, 8> Args;
Sebastian Redlc3632732010-10-05 15:59:54 +00003316 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003317 QualType Canon = GetType(Record[Idx++]);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003318 QualType T;
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003319 if (Canon.isNull())
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003320 T = Context->getCanonicalTemplateSpecializationType(Name, Args.data(),
3321 Args.size());
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003322 else
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003323 T = Context->getTemplateSpecializationType(Name, Args.data(),
3324 Args.size(), Canon);
John McCallf4c73712011-01-19 06:33:43 +00003325 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003326 return T;
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003327 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003328 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003329 // Suppress a GCC warning
3330 return QualType();
3331}
3332
Sebastian Redlc3632732010-10-05 15:59:54 +00003333class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003334 ASTReader &Reader;
Sebastian Redlc3632732010-10-05 15:59:54 +00003335 ASTReader::PerFileData &F;
Sebastian Redl577d4792010-07-22 22:43:28 +00003336 llvm::BitstreamCursor &DeclsCursor;
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003337 const ASTReader::RecordData &Record;
John McCalla1ee0c52009-10-16 21:56:05 +00003338 unsigned &Idx;
3339
Sebastian Redlc3632732010-10-05 15:59:54 +00003340 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
3341 unsigned &I) {
3342 return Reader.ReadSourceLocation(F, R, I);
3343 }
3344
John McCalla1ee0c52009-10-16 21:56:05 +00003345public:
Sebastian Redlc3632732010-10-05 15:59:54 +00003346 TypeLocReader(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003347 const ASTReader::RecordData &Record, unsigned &Idx)
Sebastian Redlc3632732010-10-05 15:59:54 +00003348 : Reader(Reader), F(F), DeclsCursor(F.DeclsCursor), Record(Record), Idx(Idx)
3349 { }
John McCalla1ee0c52009-10-16 21:56:05 +00003350
John McCall51bd8032009-10-18 01:05:36 +00003351 // We want compile-time assurance that we've enumerated all of
3352 // these, so unfortunately we have to declare them first, then
3353 // define them out-of-line.
3354#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +00003355#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +00003356 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00003357#include "clang/AST/TypeLocNodes.def"
3358
John McCall51bd8032009-10-18 01:05:36 +00003359 void VisitFunctionTypeLoc(FunctionTypeLoc);
3360 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00003361};
3362
John McCall51bd8032009-10-18 01:05:36 +00003363void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCalla1ee0c52009-10-16 21:56:05 +00003364 // nothing to do
3365}
John McCall51bd8032009-10-18 01:05:36 +00003366void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003367 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
Douglas Gregorddf889a2010-01-18 18:04:31 +00003368 if (TL.needsExtraLocalData()) {
3369 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
3370 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
3371 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
3372 TL.setModeAttr(Record[Idx++]);
3373 }
John McCalla1ee0c52009-10-16 21:56:05 +00003374}
John McCall51bd8032009-10-18 01:05:36 +00003375void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003376 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003377}
John McCall51bd8032009-10-18 01:05:36 +00003378void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003379 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003380}
John McCall51bd8032009-10-18 01:05:36 +00003381void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003382 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003383}
John McCall51bd8032009-10-18 01:05:36 +00003384void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003385 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003386}
John McCall51bd8032009-10-18 01:05:36 +00003387void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003388 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003389}
John McCall51bd8032009-10-18 01:05:36 +00003390void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003391 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003392}
John McCall51bd8032009-10-18 01:05:36 +00003393void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003394 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
3395 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003396 if (Record[Idx++])
Sebastian Redlc3632732010-10-05 15:59:54 +00003397 TL.setSizeExpr(Reader.ReadExpr(F));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003398 else
John McCall51bd8032009-10-18 01:05:36 +00003399 TL.setSizeExpr(0);
3400}
3401void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
3402 VisitArrayTypeLoc(TL);
3403}
3404void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
3405 VisitArrayTypeLoc(TL);
3406}
3407void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
3408 VisitArrayTypeLoc(TL);
3409}
3410void TypeLocReader::VisitDependentSizedArrayTypeLoc(
3411 DependentSizedArrayTypeLoc TL) {
3412 VisitArrayTypeLoc(TL);
3413}
3414void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
3415 DependentSizedExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003416 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003417}
3418void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003419 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003420}
3421void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003422 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003423}
3424void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003425 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3426 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
Douglas Gregordab60ad2010-10-01 18:44:50 +00003427 TL.setTrailingReturn(Record[Idx++]);
John McCall51bd8032009-10-18 01:05:36 +00003428 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
John McCall86acc2a2009-10-23 01:28:53 +00003429 TL.setArg(i, cast_or_null<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
John McCall51bd8032009-10-18 01:05:36 +00003430 }
3431}
3432void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
3433 VisitFunctionTypeLoc(TL);
3434}
3435void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
3436 VisitFunctionTypeLoc(TL);
3437}
John McCalled976492009-12-04 22:46:56 +00003438void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003439 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalled976492009-12-04 22:46:56 +00003440}
John McCall51bd8032009-10-18 01:05:36 +00003441void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003442 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003443}
3444void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003445 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3446 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3447 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003448}
3449void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003450 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3451 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3452 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3453 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003454}
3455void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003456 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003457}
3458void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003459 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003460}
3461void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003462 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003463}
John McCall9d156a72011-01-06 01:58:22 +00003464void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
3465 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
3466 if (TL.hasAttrOperand()) {
3467 SourceRange range;
3468 range.setBegin(ReadSourceLocation(Record, Idx));
3469 range.setEnd(ReadSourceLocation(Record, Idx));
3470 TL.setAttrOperandParensRange(range);
3471 }
3472 if (TL.hasAttrExprOperand()) {
3473 if (Record[Idx++])
3474 TL.setAttrExprOperand(Reader.ReadExpr(F));
3475 else
3476 TL.setAttrExprOperand(0);
3477 } else if (TL.hasAttrEnumOperand())
3478 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
3479}
John McCall51bd8032009-10-18 01:05:36 +00003480void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003481 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003482}
John McCall49a832b2009-10-18 09:09:24 +00003483void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
3484 SubstTemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003485 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall49a832b2009-10-18 09:09:24 +00003486}
Douglas Gregorc3069d62011-01-14 02:55:32 +00003487void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
3488 SubstTemplateTypeParmPackTypeLoc TL) {
3489 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3490}
John McCall51bd8032009-10-18 01:05:36 +00003491void TypeLocReader::VisitTemplateSpecializationTypeLoc(
3492 TemplateSpecializationTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003493 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
3494 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3495 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall833ca992009-10-29 08:12:44 +00003496 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
3497 TL.setArgLocInfo(i,
Sebastian Redlc3632732010-10-05 15:59:54 +00003498 Reader.GetTemplateArgumentLocInfo(F,
3499 TL.getTypePtr()->getArg(i).getKind(),
3500 Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003501}
Abramo Bagnara075f8f12010-12-10 16:29:40 +00003502void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
3503 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3504 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3505}
Abramo Bagnara465d41b2010-05-11 21:36:43 +00003506void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003507 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3508 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003509}
John McCall3cb0ebd2010-03-10 03:28:59 +00003510void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003511 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall3cb0ebd2010-03-10 03:28:59 +00003512}
Douglas Gregor4714c122010-03-31 17:34:00 +00003513void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003514 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3515 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
3516 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003517}
John McCall33500952010-06-11 00:33:02 +00003518void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
3519 DependentTemplateSpecializationTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003520 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3521 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
3522 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3523 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3524 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall33500952010-06-11 00:33:02 +00003525 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
3526 TL.setArgLocInfo(I,
Sebastian Redlc3632732010-10-05 15:59:54 +00003527 Reader.GetTemplateArgumentLocInfo(F,
3528 TL.getTypePtr()->getArg(I).getKind(),
3529 Record, Idx));
John McCall33500952010-06-11 00:33:02 +00003530}
Douglas Gregor7536dd52010-12-20 02:24:11 +00003531void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
3532 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
3533}
John McCall51bd8032009-10-18 01:05:36 +00003534void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003535 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallc12c5bb2010-05-15 11:32:37 +00003536}
3537void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
3538 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00003539 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3540 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003541 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Sebastian Redlc3632732010-10-05 15:59:54 +00003542 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003543}
John McCall54e14c42009-10-22 22:37:11 +00003544void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003545 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall54e14c42009-10-22 22:37:11 +00003546}
John McCalla1ee0c52009-10-16 21:56:05 +00003547
Sebastian Redlc3632732010-10-05 15:59:54 +00003548TypeSourceInfo *ASTReader::GetTypeSourceInfo(PerFileData &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00003549 const RecordData &Record,
John McCalla1ee0c52009-10-16 21:56:05 +00003550 unsigned &Idx) {
3551 QualType InfoTy = GetType(Record[Idx++]);
3552 if (InfoTy.isNull())
3553 return 0;
3554
John McCalla93c9342009-12-07 02:54:59 +00003555 TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy);
Sebastian Redlc3632732010-10-05 15:59:54 +00003556 TypeLocReader TLR(*this, F, Record, Idx);
John McCalla93c9342009-12-07 02:54:59 +00003557 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCalla1ee0c52009-10-16 21:56:05 +00003558 TLR.Visit(TL);
John McCalla93c9342009-12-07 02:54:59 +00003559 return TInfo;
John McCalla1ee0c52009-10-16 21:56:05 +00003560}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003561
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003562QualType ASTReader::GetType(TypeID ID) {
John McCall0953e762009-09-24 19:53:00 +00003563 unsigned FastQuals = ID & Qualifiers::FastMask;
3564 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003565
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003566 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003567 QualType T;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003568 switch ((PredefinedTypeIDs)Index) {
3569 case PREDEF_TYPE_NULL_ID: return QualType();
3570 case PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break;
3571 case PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003572
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003573 case PREDEF_TYPE_CHAR_U_ID:
3574 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregor2cf26342009-04-09 22:27:44 +00003575 // FIXME: Check that the signedness of CharTy is correct!
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003576 T = Context->CharTy;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003577 break;
3578
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003579 case PREDEF_TYPE_UCHAR_ID: T = Context->UnsignedCharTy; break;
3580 case PREDEF_TYPE_USHORT_ID: T = Context->UnsignedShortTy; break;
3581 case PREDEF_TYPE_UINT_ID: T = Context->UnsignedIntTy; break;
3582 case PREDEF_TYPE_ULONG_ID: T = Context->UnsignedLongTy; break;
3583 case PREDEF_TYPE_ULONGLONG_ID: T = Context->UnsignedLongLongTy; break;
3584 case PREDEF_TYPE_UINT128_ID: T = Context->UnsignedInt128Ty; break;
3585 case PREDEF_TYPE_SCHAR_ID: T = Context->SignedCharTy; break;
3586 case PREDEF_TYPE_WCHAR_ID: T = Context->WCharTy; break;
3587 case PREDEF_TYPE_SHORT_ID: T = Context->ShortTy; break;
3588 case PREDEF_TYPE_INT_ID: T = Context->IntTy; break;
3589 case PREDEF_TYPE_LONG_ID: T = Context->LongTy; break;
3590 case PREDEF_TYPE_LONGLONG_ID: T = Context->LongLongTy; break;
3591 case PREDEF_TYPE_INT128_ID: T = Context->Int128Ty; break;
3592 case PREDEF_TYPE_FLOAT_ID: T = Context->FloatTy; break;
3593 case PREDEF_TYPE_DOUBLE_ID: T = Context->DoubleTy; break;
3594 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy; break;
3595 case PREDEF_TYPE_OVERLOAD_ID: T = Context->OverloadTy; break;
3596 case PREDEF_TYPE_DEPENDENT_ID: T = Context->DependentTy; break;
3597 case PREDEF_TYPE_NULLPTR_ID: T = Context->NullPtrTy; break;
3598 case PREDEF_TYPE_CHAR16_ID: T = Context->Char16Ty; break;
3599 case PREDEF_TYPE_CHAR32_ID: T = Context->Char32Ty; break;
3600 case PREDEF_TYPE_OBJC_ID: T = Context->ObjCBuiltinIdTy; break;
3601 case PREDEF_TYPE_OBJC_CLASS: T = Context->ObjCBuiltinClassTy; break;
3602 case PREDEF_TYPE_OBJC_SEL: T = Context->ObjCBuiltinSelTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003603 }
3604
3605 assert(!T.isNull() && "Unknown predefined type");
John McCall0953e762009-09-24 19:53:00 +00003606 return T.withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003607 }
3608
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003609 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003610 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl07a353c2010-07-14 20:26:45 +00003611 if (TypesLoaded[Index].isNull()) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003612 TypesLoaded[Index] = ReadTypeRecord(Index);
Douglas Gregor97475832010-10-05 18:37:06 +00003613 if (TypesLoaded[Index].isNull())
3614 return QualType();
3615
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003616 TypesLoaded[Index]->setFromAST();
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003617 TypeIdxs[TypesLoaded[Index]] = TypeIdx::fromTypeID(ID);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003618 if (DeserializationListener)
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003619 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1476ed42010-07-16 16:36:56 +00003620 TypesLoaded[Index]);
Sebastian Redl07a353c2010-07-14 20:26:45 +00003621 }
Mike Stump1eb44332009-09-09 15:08:12 +00003622
John McCall0953e762009-09-24 19:53:00 +00003623 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003624}
3625
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003626TypeID ASTReader::GetTypeID(QualType T) const {
3627 return MakeTypeID(T,
3628 std::bind1st(std::mem_fun(&ASTReader::GetTypeIdx), this));
3629}
3630
3631TypeIdx ASTReader::GetTypeIdx(QualType T) const {
3632 if (T.isNull())
3633 return TypeIdx();
3634 assert(!T.getLocalFastQualifiers());
3635
3636 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3637 // GetTypeIdx is mostly used for computing the hash of DeclarationNames and
3638 // comparing keys of ASTDeclContextNameLookupTable.
3639 // If the type didn't come from the AST file use a specially marked index
3640 // so that any hash/key comparison fail since no such index is stored
3641 // in a AST file.
3642 if (I == TypeIdxs.end())
3643 return TypeIdx(-1);
3644 return I->second;
3645}
3646
Douglas Gregor7c789c12010-10-29 22:39:52 +00003647unsigned ASTReader::getTotalNumCXXBaseSpecifiers() const {
3648 unsigned Result = 0;
3649 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
3650 Result += Chain[I]->LocalNumCXXBaseSpecifiers;
3651
3652 return Result;
3653}
3654
John McCall833ca992009-10-29 08:12:44 +00003655TemplateArgumentLocInfo
Sebastian Redlc3632732010-10-05 15:59:54 +00003656ASTReader::GetTemplateArgumentLocInfo(PerFileData &F,
3657 TemplateArgument::ArgKind Kind,
John McCall833ca992009-10-29 08:12:44 +00003658 const RecordData &Record,
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00003659 unsigned &Index) {
John McCall833ca992009-10-29 08:12:44 +00003660 switch (Kind) {
3661 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00003662 return ReadExpr(F);
John McCall833ca992009-10-29 08:12:44 +00003663 case TemplateArgument::Type:
Sebastian Redlc3632732010-10-05 15:59:54 +00003664 return GetTypeSourceInfo(F, Record, Index);
Douglas Gregor788cd062009-11-11 01:00:40 +00003665 case TemplateArgument::Template: {
Sebastian Redlc3632732010-10-05 15:59:54 +00003666 SourceRange QualifierRange = ReadSourceRange(F, Record, Index);
3667 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003668 return TemplateArgumentLocInfo(QualifierRange, TemplateNameLoc,
3669 SourceLocation());
3670 }
3671 case TemplateArgument::TemplateExpansion: {
3672 SourceRange QualifierRange = ReadSourceRange(F, Record, Index);
3673 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorba68eca2011-01-05 17:40:24 +00003674 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
3675 return TemplateArgumentLocInfo(QualifierRange, TemplateNameLoc,
3676 EllipsisLoc);
Douglas Gregor788cd062009-11-11 01:00:40 +00003677 }
John McCall833ca992009-10-29 08:12:44 +00003678 case TemplateArgument::Null:
3679 case TemplateArgument::Integral:
3680 case TemplateArgument::Declaration:
3681 case TemplateArgument::Pack:
3682 return TemplateArgumentLocInfo();
3683 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00003684 llvm_unreachable("unexpected template argument loc");
John McCall833ca992009-10-29 08:12:44 +00003685 return TemplateArgumentLocInfo();
3686}
3687
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003688TemplateArgumentLoc
Sebastian Redlc3632732010-10-05 15:59:54 +00003689ASTReader::ReadTemplateArgumentLoc(PerFileData &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00003690 const RecordData &Record, unsigned &Index) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003691 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003692
3693 if (Arg.getKind() == TemplateArgument::Expression) {
3694 if (Record[Index++]) // bool InfoHasSameExpr.
3695 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
3696 }
Sebastian Redlc3632732010-10-05 15:59:54 +00003697 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00003698 Record, Index));
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003699}
3700
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003701Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall76bd1f32010-06-01 09:23:16 +00003702 return GetDecl(ID);
3703}
3704
Douglas Gregor7c789c12010-10-29 22:39:52 +00003705uint64_t
3706ASTReader::GetCXXBaseSpecifiersOffset(serialization::CXXBaseSpecifiersID ID) {
3707 if (ID == 0)
3708 return 0;
3709
3710 --ID;
3711 uint64_t Offset = 0;
3712 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3713 if (ID < Chain[I]->LocalNumCXXBaseSpecifiers)
3714 return Offset + Chain[I]->CXXBaseSpecifiersOffsets[ID];
3715
3716 ID -= Chain[I]->LocalNumCXXBaseSpecifiers;
3717 Offset += Chain[I]->SizeInBits;
3718 }
3719
3720 assert(false && "CXXBaseSpecifiers not found");
3721 return 0;
3722}
3723
3724CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
3725 // Figure out which AST file contains this offset.
3726 PerFileData *F = 0;
3727 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3728 if (Offset < Chain[I]->SizeInBits) {
3729 F = Chain[I];
3730 break;
3731 }
3732
3733 Offset -= Chain[I]->SizeInBits;
3734 }
3735
3736 if (!F) {
3737 Error("Malformed AST file: C++ base specifiers at impossible offset");
3738 return 0;
3739 }
3740
3741 llvm::BitstreamCursor &Cursor = F->DeclsCursor;
3742 SavedStreamPosition SavedPosition(Cursor);
3743 Cursor.JumpToBit(Offset);
3744 ReadingKindTracker ReadingKind(Read_Decl, *this);
3745 RecordData Record;
3746 unsigned Code = Cursor.ReadCode();
3747 unsigned RecCode = Cursor.ReadRecord(Code, Record);
3748 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
3749 Error("Malformed AST file: missing C++ base specifiers");
3750 return 0;
3751 }
3752
3753 unsigned Idx = 0;
3754 unsigned NumBases = Record[Idx++];
3755 void *Mem = Context->Allocate(sizeof(CXXBaseSpecifier) * NumBases);
3756 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
3757 for (unsigned I = 0; I != NumBases; ++I)
3758 Bases[I] = ReadCXXBaseSpecifier(*F, Record, Idx);
3759 return Bases;
3760}
3761
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003762TranslationUnitDecl *ASTReader::GetTranslationUnitDecl() {
Sebastian Redl30c514c2010-07-14 23:45:08 +00003763 if (!DeclsLoaded[0]) {
Sebastian Redle1dde812010-08-24 00:50:04 +00003764 ReadDeclRecord(0, 1);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003765 if (DeserializationListener)
Sebastian Redl1476ed42010-07-16 16:36:56 +00003766 DeserializationListener->DeclRead(1, DeclsLoaded[0]);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003767 }
Argyrios Kyrtzidis8871a442010-07-08 17:13:02 +00003768
3769 return cast<TranslationUnitDecl>(DeclsLoaded[0]);
3770}
3771
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003772Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003773 if (ID == 0)
3774 return 0;
3775
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003776 if (ID > DeclsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003777 Error("declaration ID out-of-range for AST file");
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003778 return 0;
3779 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003780
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003781 unsigned Index = ID - 1;
Sebastian Redl30c514c2010-07-14 23:45:08 +00003782 if (!DeclsLoaded[Index]) {
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00003783 ReadDeclRecord(Index, ID);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003784 if (DeserializationListener)
3785 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
3786 }
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003787
3788 return DeclsLoaded[Index];
Douglas Gregor2cf26342009-04-09 22:27:44 +00003789}
3790
Chris Lattner887e2b32009-04-27 05:46:25 +00003791/// \brief Resolve the offset of a statement into a statement.
3792///
3793/// This operation will read a new statement from the external
3794/// source each time it is called, and is meant to be used via a
3795/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003796Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00003797 // Switch case IDs are per Decl.
3798 ClearSwitchCaseIDs();
3799
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003800 // Offset here is a global offset across the entire chain.
3801 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3802 PerFileData &F = *Chain[N - I - 1];
3803 if (Offset < F.SizeInBits) {
3804 // Since we know that this statement is part of a decl, make sure to use
3805 // the decl cursor to read it.
3806 F.DeclsCursor.JumpToBit(Offset);
Sebastian Redlc3632732010-10-05 15:59:54 +00003807 return ReadStmtFromStream(F);
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003808 }
3809 Offset -= F.SizeInBits;
3810 }
3811 llvm_unreachable("Broken chain");
Douglas Gregor250fc9c2009-04-18 00:07:54 +00003812}
3813
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003814bool ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00003815 bool (*isKindWeWant)(Decl::Kind),
John McCall76bd1f32010-06-01 09:23:16 +00003816 llvm::SmallVectorImpl<Decl*> &Decls) {
Mike Stump1eb44332009-09-09 15:08:12 +00003817 assert(DC->hasExternalLexicalStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00003818 "DeclContext has no lexical decls in storage");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003819
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003820 // There might be lexical decls in multiple parts of the chain, for the TU
3821 // at least.
Sebastian Redl4a9eb262010-09-28 02:24:44 +00003822 // DeclContextOffsets might reallocate as we load additional decls below,
3823 // so make a copy of the vector.
3824 DeclContextInfos Infos = DeclContextOffsets[DC];
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003825 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3826 I != E; ++I) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003827 // IDs can be 0 if this context doesn't contain declarations.
3828 if (!I->LexicalDecls)
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003829 continue;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003830
3831 // Load all of the declaration IDs
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00003832 for (const KindDeclIDPair *ID = I->LexicalDecls,
3833 *IDE = ID + I->NumLexicalDecls; ID != IDE; ++ID) {
3834 if (isKindWeWant && !isKindWeWant((Decl::Kind)ID->first))
3835 continue;
3836
3837 Decl *D = GetDecl(ID->second);
Sebastian Redl4a9eb262010-09-28 02:24:44 +00003838 assert(D && "Null decl in lexical decls");
3839 Decls.push_back(D);
3840 }
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003841 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003842
Douglas Gregor25123082009-04-22 22:34:57 +00003843 ++NumLexicalDeclContextsRead;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003844 return false;
3845}
3846
John McCall76bd1f32010-06-01 09:23:16 +00003847DeclContext::lookup_result
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003848ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +00003849 DeclarationName Name) {
Mike Stump1eb44332009-09-09 15:08:12 +00003850 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00003851 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003852 if (!Name)
3853 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
3854 DeclContext::lookup_iterator(0));
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003855
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003856 llvm::SmallVector<NamedDecl *, 64> Decls;
Sebastian Redl8b122732010-08-24 00:49:55 +00003857 // There might be visible decls in multiple parts of the chain, for the TU
Sebastian Redl5967d622010-08-24 00:50:16 +00003858 // and namespaces. For any given name, the last available results replace
3859 // all earlier ones. For this reason, we walk in reverse.
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003860 DeclContextInfos &Infos = DeclContextOffsets[DC];
Sebastian Redl5967d622010-08-24 00:50:16 +00003861 for (DeclContextInfos::reverse_iterator I = Infos.rbegin(), E = Infos.rend();
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003862 I != E; ++I) {
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003863 if (!I->NameLookupTableData)
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003864 continue;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003865
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003866 ASTDeclContextNameLookupTable *LookupTable =
3867 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3868 ASTDeclContextNameLookupTable::iterator Pos = LookupTable->find(Name);
3869 if (Pos == LookupTable->end())
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003870 continue;
3871
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003872 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
3873 for (; Data.first != Data.second; ++Data.first)
3874 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
Sebastian Redl5967d622010-08-24 00:50:16 +00003875 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003876 }
3877
Douglas Gregor25123082009-04-22 22:34:57 +00003878 ++NumVisibleDeclContextsRead;
John McCall76bd1f32010-06-01 09:23:16 +00003879
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003880 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall76bd1f32010-06-01 09:23:16 +00003881 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003882}
3883
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +00003884void ASTReader::MaterializeVisibleDecls(const DeclContext *DC) {
3885 assert(DC->hasExternalVisibleStorage() &&
3886 "DeclContext has no visible decls in storage");
3887
3888 llvm::SmallVector<NamedDecl *, 64> Decls;
3889 // There might be visible decls in multiple parts of the chain, for the TU
3890 // and namespaces.
3891 DeclContextInfos &Infos = DeclContextOffsets[DC];
3892 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3893 I != E; ++I) {
3894 if (!I->NameLookupTableData)
3895 continue;
3896
3897 ASTDeclContextNameLookupTable *LookupTable =
3898 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3899 for (ASTDeclContextNameLookupTable::item_iterator
3900 ItemI = LookupTable->item_begin(),
3901 ItemEnd = LookupTable->item_end() ; ItemI != ItemEnd; ++ItemI) {
3902 ASTDeclContextNameLookupTable::item_iterator::value_type Val
3903 = *ItemI;
3904 ASTDeclContextNameLookupTrait::data_type Data = Val.second;
3905 Decls.clear();
3906 for (; Data.first != Data.second; ++Data.first)
3907 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
3908 MaterializeVisibleDeclsForName(DC, Val.first, Decls);
3909 }
3910 }
3911}
3912
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003913void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003914 assert(Consumer);
3915 while (!InterestingDecls.empty()) {
3916 DeclGroupRef DG(InterestingDecls.front());
3917 InterestingDecls.pop_front();
Sebastian Redl27372b42010-08-11 18:52:41 +00003918 Consumer->HandleInterestingDecl(DG);
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003919 }
3920}
3921
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003922void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor0af2ca42009-04-22 19:09:20 +00003923 this->Consumer = Consumer;
3924
Douglas Gregorfdd01722009-04-14 00:24:19 +00003925 if (!Consumer)
3926 return;
3927
3928 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003929 // Force deserialization of this decl, which will cause it to be queued for
3930 // passing to the consumer.
Daniel Dunbar04a0b502009-09-17 03:06:44 +00003931 GetDecl(ExternalDefinitions[I]);
Douglas Gregorfdd01722009-04-14 00:24:19 +00003932 }
Douglas Gregorc62a2fe2009-04-25 00:41:30 +00003933
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003934 PassInterestingDeclsToConsumer();
Douglas Gregorfdd01722009-04-14 00:24:19 +00003935}
3936
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003937void ASTReader::PrintStats() {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003938 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregor2cf26342009-04-09 22:27:44 +00003939
Mike Stump1eb44332009-09-09 15:08:12 +00003940 unsigned NumTypesLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003941 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall0953e762009-09-24 19:53:00 +00003942 QualType());
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003943 unsigned NumDeclsLoaded
3944 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
3945 (Decl *)0);
3946 unsigned NumIdentifiersLoaded
3947 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
3948 IdentifiersLoaded.end(),
3949 (IdentifierInfo *)0);
Mike Stump1eb44332009-09-09 15:08:12 +00003950 unsigned NumSelectorsLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003951 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
3952 SelectorsLoaded.end(),
3953 Selector());
Douglas Gregor2d41cc12009-04-13 20:50:16 +00003954
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003955 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
3956 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00003957 if (TotalNumSLocEntries)
3958 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
3959 NumSLocEntriesRead, TotalNumSLocEntries,
3960 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003961 if (!TypesLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003962 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003963 NumTypesLoaded, (unsigned)TypesLoaded.size(),
3964 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
3965 if (!DeclsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003966 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003967 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
3968 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003969 if (!IdentifiersLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003970 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003971 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
3972 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Sebastian Redl725cd962010-08-04 20:40:17 +00003973 if (!SelectorsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003974 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redl725cd962010-08-04 20:40:17 +00003975 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
3976 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor83941df2009-04-25 17:48:32 +00003977 if (TotalNumStatements)
3978 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
3979 NumStatementsRead, TotalNumStatements,
3980 ((float)NumStatementsRead/TotalNumStatements * 100));
3981 if (TotalNumMacros)
3982 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
3983 NumMacrosRead, TotalNumMacros,
3984 ((float)NumMacrosRead/TotalNumMacros * 100));
3985 if (TotalLexicalDeclContexts)
3986 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
3987 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
3988 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
3989 * 100));
3990 if (TotalVisibleDeclContexts)
3991 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
3992 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
3993 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
3994 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003995 if (TotalNumMethodPoolEntries) {
Douglas Gregor83941df2009-04-25 17:48:32 +00003996 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003997 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
3998 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor83941df2009-04-25 17:48:32 +00003999 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00004000 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor83941df2009-04-25 17:48:32 +00004001 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00004002 std::fprintf(stderr, "\n");
4003}
4004
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004005void ASTReader::InitializeSema(Sema &S) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00004006 SemaObj = &S;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00004007 S.ExternalSource = this;
4008
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00004009 // Makes sure any declarations that were deserialized "too early"
4010 // still get added to the identifier's declaration chains.
Douglas Gregor76dc8892010-09-24 23:29:12 +00004011 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
4012 if (SemaObj->TUScope)
John McCalld226f652010-08-21 09:40:31 +00004013 SemaObj->TUScope->AddDecl(PreloadedDecls[I]);
Douglas Gregor76dc8892010-09-24 23:29:12 +00004014
4015 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregor668c1a42009-04-21 22:25:48 +00004016 }
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00004017 PreloadedDecls.clear();
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00004018
4019 // If there were any tentative definitions, deserialize them and add
Sebastian Redle9d12b62010-01-31 22:27:38 +00004020 // them to Sema's list of tentative definitions.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00004021 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
4022 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
Sebastian Redle9d12b62010-01-31 22:27:38 +00004023 SemaObj->TentativeDefinitions.push_back(Var);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00004024 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00004025
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00004026 // If there were any unused file scoped decls, deserialize them and add to
4027 // Sema's list of unused file scoped decls.
4028 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
4029 DeclaratorDecl *D = cast<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
4030 SemaObj->UnusedFileScopedDecls.push_back(D);
Tanya Lattnere6bbc012010-02-12 00:07:30 +00004031 }
Douglas Gregor14c22f22009-04-22 22:18:58 +00004032
4033 // If there were any locally-scoped external declarations,
4034 // deserialize them and add them to Sema's table of locally-scoped
4035 // external declarations.
4036 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
4037 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
4038 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
4039 }
Douglas Gregorb81c1702009-04-27 20:06:05 +00004040
4041 // If there were any ext_vector type declarations, deserialize them
4042 // and add them to Sema's vector of such declarations.
4043 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I)
4044 SemaObj->ExtVectorDecls.push_back(
4045 cast<TypedefDecl>(GetDecl(ExtVectorDecls[I])));
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004046
4047 // FIXME: Do VTable uses and dynamic classes deserialize too much ?
4048 // Can we cut them down before writing them ?
4049
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004050 // If there were any dynamic classes declarations, deserialize them
4051 // and add them to Sema's vector of such declarations.
4052 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I)
4053 SemaObj->DynamicClasses.push_back(
4054 cast<CXXRecordDecl>(GetDecl(DynamicClasses[I])));
Fariborz Jahanian32019832010-07-23 19:11:11 +00004055
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00004056 // Load the offsets of the declarations that Sema references.
4057 // They will be lazily deserialized when needed.
4058 if (!SemaDeclRefs.empty()) {
4059 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
4060 SemaObj->StdNamespace = SemaDeclRefs[0];
4061 SemaObj->StdBadAlloc = SemaDeclRefs[1];
4062 }
4063
Sebastian Redlc3632732010-10-05 15:59:54 +00004064 for (PerFileData *F = FirstInSource; F; F = F->NextInSource) {
4065
4066 // If there are @selector references added them to its pool. This is for
4067 // implementation of -Wselector.
4068 if (!F->ReferencedSelectorsData.empty()) {
4069 unsigned int DataSize = F->ReferencedSelectorsData.size()-1;
4070 unsigned I = 0;
4071 while (I < DataSize) {
4072 Selector Sel = DecodeSelector(F->ReferencedSelectorsData[I++]);
4073 SourceLocation SelLoc = ReadSourceLocation(
4074 *F, F->ReferencedSelectorsData, I);
4075 SemaObj->ReferencedSelectors.insert(std::make_pair(Sel, SelLoc));
4076 }
4077 }
4078
4079 // If there were any pending implicit instantiations, deserialize them
4080 // and add them to Sema's queue of such instantiations.
4081 assert(F->PendingInstantiations.size() % 2 == 0 &&
4082 "Expected pairs of entries");
4083 for (unsigned Idx = 0, N = F->PendingInstantiations.size(); Idx < N;) {
4084 ValueDecl *D=cast<ValueDecl>(GetDecl(F->PendingInstantiations[Idx++]));
4085 SourceLocation Loc = ReadSourceLocation(*F, F->PendingInstantiations,Idx);
4086 SemaObj->PendingInstantiations.push_back(std::make_pair(D, Loc));
4087 }
4088 }
4089
4090 // The two special data sets below always come from the most recent PCH,
4091 // which is at the front of the chain.
4092 PerFileData &F = *Chain.front();
4093
4094 // If there were any weak undeclared identifiers, deserialize them and add to
4095 // Sema's list of weak undeclared identifiers.
4096 if (!WeakUndeclaredIdentifiers.empty()) {
4097 unsigned Idx = 0;
4098 for (unsigned I = 0, N = WeakUndeclaredIdentifiers[Idx++]; I != N; ++I) {
4099 IdentifierInfo *WeakId = GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
4100 IdentifierInfo *AliasId= GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
4101 SourceLocation Loc = ReadSourceLocation(F, WeakUndeclaredIdentifiers,Idx);
4102 bool Used = WeakUndeclaredIdentifiers[Idx++];
4103 Sema::WeakInfo WI(AliasId, Loc);
4104 WI.setUsed(Used);
4105 SemaObj->WeakUndeclaredIdentifiers.insert(std::make_pair(WeakId, WI));
4106 }
4107 }
4108
4109 // If there were any VTable uses, deserialize the information and add it
4110 // to Sema's vector and map of VTable uses.
4111 if (!VTableUses.empty()) {
4112 unsigned Idx = 0;
4113 for (unsigned I = 0, N = VTableUses[Idx++]; I != N; ++I) {
4114 CXXRecordDecl *Class = cast<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
4115 SourceLocation Loc = ReadSourceLocation(F, VTableUses, Idx);
4116 bool DefinitionRequired = VTableUses[Idx++];
4117 SemaObj->VTableUses.push_back(std::make_pair(Class, Loc));
4118 SemaObj->VTablesUsed[Class] = DefinitionRequired;
Fariborz Jahanian32019832010-07-23 19:11:11 +00004119 }
4120 }
Peter Collingbourne84bccea2011-02-15 19:46:30 +00004121
4122 if (!FPPragmaOptions.empty()) {
4123 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
4124 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
4125 }
4126
4127 if (!OpenCLExtensions.empty()) {
4128 unsigned I = 0;
4129#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
4130#include "clang/Basic/OpenCLExtensions.def"
4131
4132 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
4133 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00004134}
4135
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004136IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Sebastian Redld8c5abb2010-08-02 18:30:12 +00004137 // Try to find this name within our on-disk hash tables. We start with the
4138 // most recent one, since that one contains the most up-to-date info.
Sebastian Redld27d3fc2010-07-21 22:31:37 +00004139 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004140 ASTIdentifierLookupTable *IdTable
4141 = (ASTIdentifierLookupTable *)Chain[I]->IdentifierLookupTable;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00004142 if (!IdTable)
4143 continue;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00004144 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004145 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00004146 if (Pos == IdTable->end())
4147 continue;
Douglas Gregor668c1a42009-04-21 22:25:48 +00004148
Sebastian Redld27d3fc2010-07-21 22:31:37 +00004149 // Dereferencing the iterator has the effect of building the
4150 // IdentifierInfo node and populating it with the various
4151 // declarations it needs.
Sebastian Redld8c5abb2010-08-02 18:30:12 +00004152 return *Pos;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00004153 }
Sebastian Redld8c5abb2010-08-02 18:30:12 +00004154 return 0;
Douglas Gregor668c1a42009-04-21 22:25:48 +00004155}
4156
Douglas Gregor95f42922010-10-14 22:11:03 +00004157namespace clang {
4158 /// \brief An identifier-lookup iterator that enumerates all of the
4159 /// identifiers stored within a set of AST files.
4160 class ASTIdentifierIterator : public IdentifierIterator {
4161 /// \brief The AST reader whose identifiers are being enumerated.
4162 const ASTReader &Reader;
4163
4164 /// \brief The current index into the chain of AST files stored in
4165 /// the AST reader.
4166 unsigned Index;
4167
4168 /// \brief The current position within the identifier lookup table
4169 /// of the current AST file.
4170 ASTIdentifierLookupTable::key_iterator Current;
4171
4172 /// \brief The end position within the identifier lookup table of
4173 /// the current AST file.
4174 ASTIdentifierLookupTable::key_iterator End;
4175
4176 public:
4177 explicit ASTIdentifierIterator(const ASTReader &Reader);
4178
4179 virtual llvm::StringRef Next();
4180 };
4181}
4182
4183ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
4184 : Reader(Reader), Index(Reader.Chain.size() - 1) {
4185 ASTIdentifierLookupTable *IdTable
4186 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
4187 Current = IdTable->key_begin();
4188 End = IdTable->key_end();
4189}
4190
4191llvm::StringRef ASTIdentifierIterator::Next() {
4192 while (Current == End) {
4193 // If we have exhausted all of our AST files, we're done.
4194 if (Index == 0)
4195 return llvm::StringRef();
4196
4197 --Index;
4198 ASTIdentifierLookupTable *IdTable
4199 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
4200 Current = IdTable->key_begin();
4201 End = IdTable->key_end();
4202 }
4203
4204 // We have any identifiers remaining in the current AST file; return
4205 // the next one.
4206 std::pair<const char*, unsigned> Key = *Current;
4207 ++Current;
4208 return llvm::StringRef(Key.first, Key.second);
4209}
4210
4211IdentifierIterator *ASTReader::getIdentifiers() const {
4212 return new ASTIdentifierIterator(*this);
4213}
4214
Mike Stump1eb44332009-09-09 15:08:12 +00004215std::pair<ObjCMethodList, ObjCMethodList>
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004216ASTReader::ReadMethodPool(Selector Sel) {
Sebastian Redl725cd962010-08-04 20:40:17 +00004217 // Find this selector in a hash table. We want to find the most recent entry.
4218 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4219 PerFileData &F = *Chain[I];
4220 if (!F.SelectorLookupTable)
4221 continue;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00004222
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004223 ASTSelectorLookupTable *PoolTable
4224 = (ASTSelectorLookupTable*)F.SelectorLookupTable;
4225 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
Sebastian Redl725cd962010-08-04 20:40:17 +00004226 if (Pos != PoolTable->end()) {
4227 ++NumSelectorsRead;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00004228 // FIXME: Not quite happy with the statistics here. We probably should
4229 // disable this tracking when called via LoadSelector.
4230 // Also, should entries without methods count as misses?
4231 ++NumMethodPoolEntriesRead;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004232 ASTSelectorLookupTrait::data_type Data = *Pos;
Sebastian Redl725cd962010-08-04 20:40:17 +00004233 if (DeserializationListener)
4234 DeserializationListener->SelectorRead(Data.ID, Sel);
4235 return std::make_pair(Data.Instance, Data.Factory);
4236 }
Douglas Gregor83941df2009-04-25 17:48:32 +00004237 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00004238
Sebastian Redlfa78dec2010-08-04 21:22:45 +00004239 ++NumMethodPoolMisses;
Sebastian Redl725cd962010-08-04 20:40:17 +00004240 return std::pair<ObjCMethodList, ObjCMethodList>();
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00004241}
4242
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004243void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redle58aa892010-08-04 18:21:41 +00004244 // It would be complicated to avoid reading the methods anyway. So don't.
4245 ReadMethodPool(Sel);
4246}
4247
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004248void ASTReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00004249 assert(ID && "Non-zero identifier ID required");
Douglas Gregora02b1472009-04-28 21:53:25 +00004250 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00004251 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004252 if (DeserializationListener)
4253 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregor668c1a42009-04-21 22:25:48 +00004254}
4255
Douglas Gregord89275b2009-07-06 18:54:52 +00004256/// \brief Set the globally-visible declarations associated with the given
4257/// identifier.
4258///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004259/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump1eb44332009-09-09 15:08:12 +00004260/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregord89275b2009-07-06 18:54:52 +00004261/// them.
4262///
4263/// \param II an IdentifierInfo that refers to one or more globally-visible
4264/// declarations.
4265///
4266/// \param DeclIDs the set of declaration IDs with the name @p II that are
4267/// visible at global scope.
4268///
4269/// \param Nonrecursive should be true to indicate that the caller knows that
4270/// this call is non-recursive, and therefore the globally-visible declarations
4271/// will not be placed onto the pending queue.
Mike Stump1eb44332009-09-09 15:08:12 +00004272void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004273ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Douglas Gregord89275b2009-07-06 18:54:52 +00004274 const llvm::SmallVectorImpl<uint32_t> &DeclIDs,
4275 bool Nonrecursive) {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004276 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregord89275b2009-07-06 18:54:52 +00004277 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
4278 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
4279 PII.II = II;
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00004280 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregord89275b2009-07-06 18:54:52 +00004281 return;
4282 }
Mike Stump1eb44332009-09-09 15:08:12 +00004283
Douglas Gregord89275b2009-07-06 18:54:52 +00004284 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
4285 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
4286 if (SemaObj) {
Douglas Gregor914ed9d2010-08-13 03:15:25 +00004287 if (SemaObj->TUScope) {
4288 // Introduce this declaration into the translation-unit scope
4289 // and add it to the declaration chain for this identifier, so
4290 // that (unqualified) name lookup will find it.
John McCalld226f652010-08-21 09:40:31 +00004291 SemaObj->TUScope->AddDecl(D);
Douglas Gregor914ed9d2010-08-13 03:15:25 +00004292 }
Douglas Gregor76dc8892010-09-24 23:29:12 +00004293 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
Douglas Gregord89275b2009-07-06 18:54:52 +00004294 } else {
4295 // Queue this declaration so that it will be added to the
4296 // translation unit scope and identifier's declaration chain
4297 // once a Sema object is known.
4298 PreloadedDecls.push_back(D);
4299 }
4300 }
4301}
4302
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004303IdentifierInfo *ASTReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00004304 if (ID == 0)
4305 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00004306
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00004307 if (IdentifiersLoaded.empty()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004308 Error("no identifier table in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00004309 return 0;
4310 }
Mike Stump1eb44332009-09-09 15:08:12 +00004311
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00004312 assert(PP && "Forgot to set Preprocessor ?");
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00004313 ID -= 1;
4314 if (!IdentifiersLoaded[ID]) {
4315 unsigned Index = ID;
4316 const char *Str = 0;
4317 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4318 PerFileData *F = Chain[N - I - 1];
4319 if (Index < F->LocalNumIdentifiers) {
4320 uint32_t Offset = F->IdentifierOffsets[Index];
4321 Str = F->IdentifierTableData + Offset;
4322 break;
4323 }
4324 Index -= F->LocalNumIdentifiers;
4325 }
4326 assert(Str && "Broken Chain");
Douglas Gregord6595a42009-04-25 21:04:17 +00004327
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004328 // All of the strings in the AST file are preceded by a 16-bit length.
4329 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenek231bc0b2009-10-23 04:45:31 +00004330 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
4331 // unsigned integers. This is important to avoid integer overflow when
4332 // we cast them to 'unsigned'.
Ted Kremenekff1ea462009-10-23 03:57:22 +00004333 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregor02fc7512009-04-28 20:01:51 +00004334 unsigned StrLen = (((unsigned) StrLenPtr[0])
4335 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00004336 IdentifiersLoaded[ID]
Kovarththanan Rajaratnam811f4262010-03-12 10:32:27 +00004337 = &PP->getIdentifierTable().get(Str, StrLen);
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004338 if (DeserializationListener)
4339 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregorafaf3082009-04-11 00:14:32 +00004340 }
Mike Stump1eb44332009-09-09 15:08:12 +00004341
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00004342 return IdentifiersLoaded[ID];
Douglas Gregor2cf26342009-04-09 22:27:44 +00004343}
4344
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004345void ASTReader::ReadSLocEntry(unsigned ID) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00004346 ReadSLocEntryRecord(ID);
4347}
4348
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004349Selector ASTReader::DecodeSelector(unsigned ID) {
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004350 if (ID == 0)
4351 return Selector();
Mike Stump1eb44332009-09-09 15:08:12 +00004352
Sebastian Redl725cd962010-08-04 20:40:17 +00004353 if (ID > SelectorsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004354 Error("selector ID out of range in AST file");
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004355 return Selector();
4356 }
Douglas Gregor83941df2009-04-25 17:48:32 +00004357
Sebastian Redl725cd962010-08-04 20:40:17 +00004358 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor83941df2009-04-25 17:48:32 +00004359 // Load this selector from the selector table.
Sebastian Redl725cd962010-08-04 20:40:17 +00004360 unsigned Idx = ID - 1;
4361 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
4362 PerFileData &F = *Chain[N - I - 1];
4363 if (Idx < F.LocalNumSelectors) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004364 ASTSelectorLookupTrait Trait(*this);
Sebastian Redl725cd962010-08-04 20:40:17 +00004365 SelectorsLoaded[ID - 1] =
4366 Trait.ReadKey(F.SelectorLookupTableData + F.SelectorOffsets[Idx], 0);
4367 if (DeserializationListener)
4368 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
4369 break;
4370 }
4371 Idx -= F.LocalNumSelectors;
4372 }
Douglas Gregor83941df2009-04-25 17:48:32 +00004373 }
4374
Sebastian Redl725cd962010-08-04 20:40:17 +00004375 return SelectorsLoaded[ID - 1];
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004376}
4377
Michael J. Spencer20249a12010-10-21 03:16:25 +00004378Selector ASTReader::GetExternalSelector(uint32_t ID) {
Douglas Gregor719770d2010-04-06 17:30:22 +00004379 return DecodeSelector(ID);
4380}
4381
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004382uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redl725cd962010-08-04 20:40:17 +00004383 // ID 0 (the null selector) is considered an external selector.
4384 return getTotalNumSelectors() + 1;
Douglas Gregor719770d2010-04-06 17:30:22 +00004385}
4386
Mike Stump1eb44332009-09-09 15:08:12 +00004387DeclarationName
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004388ASTReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00004389 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
4390 switch (Kind) {
4391 case DeclarationName::Identifier:
4392 return DeclarationName(GetIdentifierInfo(Record, Idx));
4393
4394 case DeclarationName::ObjCZeroArgSelector:
4395 case DeclarationName::ObjCOneArgSelector:
4396 case DeclarationName::ObjCMultiArgSelector:
Steve Naroffa7503a72009-04-23 15:15:40 +00004397 return DeclarationName(GetSelector(Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004398
4399 case DeclarationName::CXXConstructorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00004400 return Context->DeclarationNames.getCXXConstructorName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00004401 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004402
4403 case DeclarationName::CXXDestructorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00004404 return Context->DeclarationNames.getCXXDestructorName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00004405 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004406
4407 case DeclarationName::CXXConversionFunctionName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00004408 return Context->DeclarationNames.getCXXConversionFunctionName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00004409 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004410
4411 case DeclarationName::CXXOperatorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00004412 return Context->DeclarationNames.getCXXOperatorName(
Douglas Gregor2cf26342009-04-09 22:27:44 +00004413 (OverloadedOperatorKind)Record[Idx++]);
4414
Sean Hunt3e518bd2009-11-29 07:34:05 +00004415 case DeclarationName::CXXLiteralOperatorName:
4416 return Context->DeclarationNames.getCXXLiteralOperatorName(
4417 GetIdentifierInfo(Record, Idx));
4418
Douglas Gregor2cf26342009-04-09 22:27:44 +00004419 case DeclarationName::CXXUsingDirective:
4420 return DeclarationName::getUsingDirectiveName();
4421 }
4422
4423 // Required to silence GCC warning
4424 return DeclarationName();
4425}
Douglas Gregor0a0428e2009-04-10 20:39:37 +00004426
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004427void ASTReader::ReadDeclarationNameLoc(PerFileData &F,
4428 DeclarationNameLoc &DNLoc,
4429 DeclarationName Name,
4430 const RecordData &Record, unsigned &Idx) {
4431 switch (Name.getNameKind()) {
4432 case DeclarationName::CXXConstructorName:
4433 case DeclarationName::CXXDestructorName:
4434 case DeclarationName::CXXConversionFunctionName:
4435 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
4436 break;
4437
4438 case DeclarationName::CXXOperatorName:
4439 DNLoc.CXXOperatorName.BeginOpNameLoc
4440 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4441 DNLoc.CXXOperatorName.EndOpNameLoc
4442 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4443 break;
4444
4445 case DeclarationName::CXXLiteralOperatorName:
4446 DNLoc.CXXLiteralOperatorName.OpNameLoc
4447 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4448 break;
4449
4450 case DeclarationName::Identifier:
4451 case DeclarationName::ObjCZeroArgSelector:
4452 case DeclarationName::ObjCOneArgSelector:
4453 case DeclarationName::ObjCMultiArgSelector:
4454 case DeclarationName::CXXUsingDirective:
4455 break;
4456 }
4457}
4458
4459void ASTReader::ReadDeclarationNameInfo(PerFileData &F,
4460 DeclarationNameInfo &NameInfo,
4461 const RecordData &Record, unsigned &Idx) {
4462 NameInfo.setName(ReadDeclarationName(Record, Idx));
4463 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
4464 DeclarationNameLoc DNLoc;
4465 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
4466 NameInfo.setInfo(DNLoc);
4467}
4468
4469void ASTReader::ReadQualifierInfo(PerFileData &F, QualifierInfo &Info,
4470 const RecordData &Record, unsigned &Idx) {
4471 Info.NNS = ReadNestedNameSpecifier(Record, Idx);
4472 Info.NNSRange = ReadSourceRange(F, Record, Idx);
4473 unsigned NumTPLists = Record[Idx++];
4474 Info.NumTemplParamLists = NumTPLists;
4475 if (NumTPLists) {
4476 Info.TemplParamLists = new (*Context) TemplateParameterList*[NumTPLists];
4477 for (unsigned i=0; i != NumTPLists; ++i)
4478 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
4479 }
4480}
4481
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004482TemplateName
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004483ASTReader::ReadTemplateName(PerFileData &F, const RecordData &Record,
4484 unsigned &Idx) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00004485 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004486 switch (Kind) {
4487 case TemplateName::Template:
4488 return TemplateName(cast_or_null<TemplateDecl>(GetDecl(Record[Idx++])));
4489
4490 case TemplateName::OverloadedTemplate: {
4491 unsigned size = Record[Idx++];
4492 UnresolvedSet<8> Decls;
4493 while (size--)
4494 Decls.addDecl(cast<NamedDecl>(GetDecl(Record[Idx++])));
4495
4496 return Context->getOverloadedTemplateName(Decls.begin(), Decls.end());
4497 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004498
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004499 case TemplateName::QualifiedTemplate: {
4500 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
4501 bool hasTemplKeyword = Record[Idx++];
4502 TemplateDecl *Template = cast<TemplateDecl>(GetDecl(Record[Idx++]));
4503 return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
4504 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004505
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004506 case TemplateName::DependentTemplate: {
4507 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
4508 if (Record[Idx++]) // isIdentifier
4509 return Context->getDependentTemplateName(NNS,
4510 GetIdentifierInfo(Record, Idx));
4511 return Context->getDependentTemplateName(NNS,
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004512 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004513 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004514
4515 case TemplateName::SubstTemplateTemplateParmPack: {
4516 TemplateTemplateParmDecl *Param
4517 = cast_or_null<TemplateTemplateParmDecl>(GetDecl(Record[Idx++]));
4518 if (!Param)
4519 return TemplateName();
4520
4521 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
4522 if (ArgPack.getKind() != TemplateArgument::Pack)
4523 return TemplateName();
4524
4525 return Context->getSubstTemplateTemplateParmPack(Param, ArgPack);
4526 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004527 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004528
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004529 assert(0 && "Unhandled template name kind!");
4530 return TemplateName();
4531}
4532
4533TemplateArgument
Sebastian Redlc3632732010-10-05 15:59:54 +00004534ASTReader::ReadTemplateArgument(PerFileData &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00004535 const RecordData &Record, unsigned &Idx) {
Douglas Gregora7fc9012011-01-05 18:58:31 +00004536 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
4537 switch (Kind) {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004538 case TemplateArgument::Null:
4539 return TemplateArgument();
4540 case TemplateArgument::Type:
4541 return TemplateArgument(GetType(Record[Idx++]));
4542 case TemplateArgument::Declaration:
4543 return TemplateArgument(GetDecl(Record[Idx++]));
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00004544 case TemplateArgument::Integral: {
4545 llvm::APSInt Value = ReadAPSInt(Record, Idx);
4546 QualType T = GetType(Record[Idx++]);
4547 return TemplateArgument(Value, T);
4548 }
Douglas Gregora7fc9012011-01-05 18:58:31 +00004549 case TemplateArgument::Template:
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004550 return TemplateArgument(ReadTemplateName(F, Record, Idx));
Douglas Gregora7fc9012011-01-05 18:58:31 +00004551 case TemplateArgument::TemplateExpansion: {
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004552 TemplateName Name = ReadTemplateName(F, Record, Idx);
Douglas Gregor2be29f42011-01-14 23:41:42 +00004553 llvm::Optional<unsigned> NumTemplateExpansions;
4554 if (unsigned NumExpansions = Record[Idx++])
4555 NumTemplateExpansions = NumExpansions - 1;
4556 return TemplateArgument(Name, NumTemplateExpansions);
Douglas Gregorba68eca2011-01-05 17:40:24 +00004557 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004558 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00004559 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004560 case TemplateArgument::Pack: {
4561 unsigned NumArgs = Record[Idx++];
Douglas Gregor910f8002010-11-07 23:05:16 +00004562 TemplateArgument *Args = new (*Context) TemplateArgument[NumArgs];
4563 for (unsigned I = 0; I != NumArgs; ++I)
4564 Args[I] = ReadTemplateArgument(F, Record, Idx);
4565 return TemplateArgument(Args, NumArgs);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004566 }
4567 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004568
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004569 assert(0 && "Unhandled template argument kind!");
4570 return TemplateArgument();
4571}
4572
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004573TemplateParameterList *
Sebastian Redlc3632732010-10-05 15:59:54 +00004574ASTReader::ReadTemplateParameterList(PerFileData &F,
4575 const RecordData &Record, unsigned &Idx) {
4576 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
4577 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
4578 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004579
4580 unsigned NumParams = Record[Idx++];
4581 llvm::SmallVector<NamedDecl *, 16> Params;
4582 Params.reserve(NumParams);
4583 while (NumParams--)
4584 Params.push_back(cast<NamedDecl>(GetDecl(Record[Idx++])));
Michael J. Spencer20249a12010-10-21 03:16:25 +00004585
4586 TemplateParameterList* TemplateParams =
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004587 TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc,
4588 Params.data(), Params.size(), RAngleLoc);
4589 return TemplateParams;
4590}
4591
4592void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004593ASTReader::
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004594ReadTemplateArgumentList(llvm::SmallVector<TemplateArgument, 8> &TemplArgs,
Sebastian Redlc3632732010-10-05 15:59:54 +00004595 PerFileData &F, const RecordData &Record,
4596 unsigned &Idx) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004597 unsigned NumTemplateArgs = Record[Idx++];
4598 TemplArgs.reserve(NumTemplateArgs);
4599 while (NumTemplateArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00004600 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004601}
4602
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004603/// \brief Read a UnresolvedSet structure.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004604void ASTReader::ReadUnresolvedSet(UnresolvedSetImpl &Set,
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004605 const RecordData &Record, unsigned &Idx) {
4606 unsigned NumDecls = Record[Idx++];
4607 while (NumDecls--) {
4608 NamedDecl *D = cast<NamedDecl>(GetDecl(Record[Idx++]));
4609 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
4610 Set.addDecl(D, AS);
4611 }
4612}
4613
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004614CXXBaseSpecifier
Sebastian Redlc3632732010-10-05 15:59:54 +00004615ASTReader::ReadCXXBaseSpecifier(PerFileData &F,
Nick Lewycky56062202010-07-26 16:56:01 +00004616 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004617 bool isVirtual = static_cast<bool>(Record[Idx++]);
4618 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
4619 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redlf677ea32011-02-05 19:23:19 +00004620 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00004621 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
4622 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00004623 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redlf677ea32011-02-05 19:23:19 +00004624 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
Douglas Gregorf90b27a2011-01-03 22:36:02 +00004625 EllipsisLoc);
Sebastian Redlf677ea32011-02-05 19:23:19 +00004626 Result.setInheritConstructors(inheritConstructors);
4627 return Result;
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004628}
4629
Sean Huntcbb67482011-01-08 20:30:50 +00004630std::pair<CXXCtorInitializer **, unsigned>
4631ASTReader::ReadCXXCtorInitializers(PerFileData &F, const RecordData &Record,
4632 unsigned &Idx) {
4633 CXXCtorInitializer **CtorInitializers = 0;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004634 unsigned NumInitializers = Record[Idx++];
4635 if (NumInitializers) {
4636 ASTContext &C = *getContext();
4637
Sean Huntcbb67482011-01-08 20:30:50 +00004638 CtorInitializers
4639 = new (C) CXXCtorInitializer*[NumInitializers];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004640 for (unsigned i=0; i != NumInitializers; ++i) {
4641 TypeSourceInfo *BaseClassInfo = 0;
4642 bool IsBaseVirtual = false;
4643 FieldDecl *Member = 0;
Francois Pichet00eb3f92010-12-04 09:14:42 +00004644 IndirectFieldDecl *IndirectMember = 0;
Michael J. Spencer20249a12010-10-21 03:16:25 +00004645
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004646 bool IsBaseInitializer = Record[Idx++];
4647 if (IsBaseInitializer) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004648 BaseClassInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004649 IsBaseVirtual = Record[Idx++];
4650 } else {
Francois Pichet00eb3f92010-12-04 09:14:42 +00004651 bool IsIndirectMemberInitializer = Record[Idx++];
4652 if (IsIndirectMemberInitializer)
4653 IndirectMember = cast<IndirectFieldDecl>(GetDecl(Record[Idx++]));
4654 else
4655 Member = cast<FieldDecl>(GetDecl(Record[Idx++]));
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004656 }
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00004657 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redlc3632732010-10-05 15:59:54 +00004658 Expr *Init = ReadExpr(F);
Sebastian Redlc3632732010-10-05 15:59:54 +00004659 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
4660 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004661 bool IsWritten = Record[Idx++];
4662 unsigned SourceOrderOrNumArrayIndices;
4663 llvm::SmallVector<VarDecl *, 8> Indices;
4664 if (IsWritten) {
4665 SourceOrderOrNumArrayIndices = Record[Idx++];
4666 } else {
4667 SourceOrderOrNumArrayIndices = Record[Idx++];
4668 Indices.reserve(SourceOrderOrNumArrayIndices);
4669 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
4670 Indices.push_back(cast<VarDecl>(GetDecl(Record[Idx++])));
4671 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004672
Sean Huntcbb67482011-01-08 20:30:50 +00004673 CXXCtorInitializer *BOMInit;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004674 if (IsBaseInitializer) {
Sean Huntcbb67482011-01-08 20:30:50 +00004675 BOMInit = new (C) CXXCtorInitializer(C, BaseClassInfo, IsBaseVirtual,
4676 LParenLoc, Init, RParenLoc,
4677 MemberOrEllipsisLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004678 } else if (IsWritten) {
Francois Pichet00eb3f92010-12-04 09:14:42 +00004679 if (Member)
Sean Huntcbb67482011-01-08 20:30:50 +00004680 BOMInit = new (C) CXXCtorInitializer(C, Member, MemberOrEllipsisLoc,
4681 LParenLoc, Init, RParenLoc);
Francois Pichet00eb3f92010-12-04 09:14:42 +00004682 else
Sean Huntcbb67482011-01-08 20:30:50 +00004683 BOMInit = new (C) CXXCtorInitializer(C, IndirectMember,
4684 MemberOrEllipsisLoc, LParenLoc,
4685 Init, RParenLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004686 } else {
Sean Huntcbb67482011-01-08 20:30:50 +00004687 BOMInit = CXXCtorInitializer::Create(C, Member, MemberOrEllipsisLoc,
4688 LParenLoc, Init, RParenLoc,
4689 Indices.data(), Indices.size());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004690 }
4691
Argyrios Kyrtzidisf84cde12010-09-06 19:04:27 +00004692 if (IsWritten)
4693 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Sean Huntcbb67482011-01-08 20:30:50 +00004694 CtorInitializers[i] = BOMInit;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004695 }
4696 }
4697
Sean Huntcbb67482011-01-08 20:30:50 +00004698 return std::make_pair(CtorInitializers, NumInitializers);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004699}
4700
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004701NestedNameSpecifier *
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004702ASTReader::ReadNestedNameSpecifier(const RecordData &Record, unsigned &Idx) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004703 unsigned N = Record[Idx++];
4704 NestedNameSpecifier *NNS = 0, *Prev = 0;
4705 for (unsigned I = 0; I != N; ++I) {
4706 NestedNameSpecifier::SpecifierKind Kind
4707 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
4708 switch (Kind) {
4709 case NestedNameSpecifier::Identifier: {
4710 IdentifierInfo *II = GetIdentifierInfo(Record, Idx);
4711 NNS = NestedNameSpecifier::Create(*Context, Prev, II);
4712 break;
4713 }
4714
4715 case NestedNameSpecifier::Namespace: {
4716 NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++]));
4717 NNS = NestedNameSpecifier::Create(*Context, Prev, NS);
4718 break;
4719 }
4720
4721 case NestedNameSpecifier::TypeSpec:
4722 case NestedNameSpecifier::TypeSpecWithTemplate: {
John McCallf4c73712011-01-19 06:33:43 +00004723 const Type *T = GetType(Record[Idx++]).getTypePtrOrNull();
Douglas Gregor1ab55e92010-12-10 17:03:06 +00004724 if (!T)
4725 return 0;
4726
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004727 bool Template = Record[Idx++];
4728 NNS = NestedNameSpecifier::Create(*Context, Prev, Template, T);
4729 break;
4730 }
4731
4732 case NestedNameSpecifier::Global: {
4733 NNS = NestedNameSpecifier::GlobalSpecifier(*Context);
4734 // No associated value, and there can't be a prefix.
4735 break;
4736 }
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004737 }
Argyrios Kyrtzidisd2bb2c02010-07-07 15:46:30 +00004738 Prev = NNS;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004739 }
4740 return NNS;
4741}
4742
4743SourceRange
Sebastian Redlc3632732010-10-05 15:59:54 +00004744ASTReader::ReadSourceRange(PerFileData &F, const RecordData &Record,
4745 unsigned &Idx) {
4746 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
4747 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar8ee59392010-06-02 15:47:10 +00004748 return SourceRange(beg, end);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004749}
4750
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004751/// \brief Read an integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004752llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004753 unsigned BitWidth = Record[Idx++];
4754 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
4755 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
4756 Idx += NumWords;
4757 return Result;
4758}
4759
4760/// \brief Read a signed integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004761llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004762 bool isUnsigned = Record[Idx++];
4763 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
4764}
4765
Douglas Gregor17fc2232009-04-14 21:55:33 +00004766/// \brief Read a floating-point value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004767llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00004768 return llvm::APFloat(ReadAPInt(Record, Idx));
4769}
4770
Douglas Gregor68a2eb02009-04-15 21:30:51 +00004771// \brief Read a string
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004772std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00004773 unsigned Len = Record[Idx++];
Jay Foadbeaaccd2009-05-21 09:52:38 +00004774 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00004775 Idx += Len;
4776 return Result;
4777}
4778
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004779CXXTemporary *ASTReader::ReadCXXTemporary(const RecordData &Record,
Chris Lattnerd2598362010-05-10 00:25:06 +00004780 unsigned &Idx) {
4781 CXXDestructorDecl *Decl = cast<CXXDestructorDecl>(GetDecl(Record[Idx++]));
4782 return CXXTemporary::Create(*Context, Decl);
4783}
4784
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004785DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00004786 return Diag(SourceLocation(), DiagID);
4787}
4788
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004789DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004790 return Diags.Report(Loc, DiagID);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00004791}
Douglas Gregor025452f2009-04-17 00:04:06 +00004792
Douglas Gregor668c1a42009-04-21 22:25:48 +00004793/// \brief Retrieve the identifier table associated with the
4794/// preprocessor.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004795IdentifierTable &ASTReader::getIdentifierTable() {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00004796 assert(PP && "Forgot to set Preprocessor ?");
4797 return PP->getIdentifierTable();
Douglas Gregor668c1a42009-04-21 22:25:48 +00004798}
4799
Douglas Gregor025452f2009-04-17 00:04:06 +00004800/// \brief Record that the given ID maps to the given switch-case
4801/// statement.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004802void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Douglas Gregor025452f2009-04-17 00:04:06 +00004803 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
4804 SwitchCaseStmts[ID] = SC;
4805}
4806
4807/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004808SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Douglas Gregor025452f2009-04-17 00:04:06 +00004809 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
4810 return SwitchCaseStmts[ID];
4811}
Douglas Gregor1de05fe2009-04-17 18:18:49 +00004812
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00004813void ASTReader::ClearSwitchCaseIDs() {
4814 SwitchCaseStmts.clear();
4815}
4816
Douglas Gregor1de05fe2009-04-17 18:18:49 +00004817/// \brief Record that the given label statement has been
4818/// deserialized and has the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004819void ASTReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
Mike Stump1eb44332009-09-09 15:08:12 +00004820 assert(LabelStmts.find(ID) == LabelStmts.end() &&
Douglas Gregor1de05fe2009-04-17 18:18:49 +00004821 "Deserialized label twice");
4822 LabelStmts[ID] = S;
4823
4824 // If we've already seen any goto statements that point to this
4825 // label, resolve them now.
4826 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
4827 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
4828 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
4829 Goto->second->setLabel(S);
4830 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00004831
4832 // If we've already seen any address-label statements that point to
4833 // this label, resolve them now.
4834 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
Mike Stump1eb44332009-09-09 15:08:12 +00004835 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00004836 = UnresolvedAddrLabelExprs.equal_range(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00004837 for (AddrLabelIter AddrLabel = AddrLabels.first;
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00004838 AddrLabel != AddrLabels.second; ++AddrLabel)
4839 AddrLabel->second->setLabel(S);
4840 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor1de05fe2009-04-17 18:18:49 +00004841}
4842
4843/// \brief Set the label of the given statement to the label
4844/// identified by ID.
4845///
4846/// Depending on the order in which the label and other statements
4847/// referencing that label occur, this operation may complete
4848/// immediately (updating the statement) or it may queue the
4849/// statement to be back-patched later.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004850void ASTReader::SetLabelOf(GotoStmt *S, unsigned ID) {
Douglas Gregor1de05fe2009-04-17 18:18:49 +00004851 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
4852 if (Label != LabelStmts.end()) {
4853 // We've already seen this label, so set the label of the goto and
4854 // we're done.
4855 S->setLabel(Label->second);
4856 } else {
4857 // We haven't seen this label yet, so add this goto to the set of
4858 // unresolved goto statements.
4859 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
4860 }
4861}
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00004862
4863/// \brief Set the label of the given expression to the label
4864/// identified by ID.
4865///
4866/// Depending on the order in which the label and other statements
4867/// referencing that label occur, this operation may complete
4868/// immediately (updating the statement) or it may queue the
4869/// statement to be back-patched later.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004870void ASTReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00004871 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
4872 if (Label != LabelStmts.end()) {
4873 // We've already seen this label, so set the label of the
4874 // label-address expression and we're done.
4875 S->setLabel(Label->second);
4876 } else {
4877 // We haven't seen this label yet, so add this label-address
4878 // expression to the set of unresolved label-address expressions.
4879 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
4880 }
4881}
Douglas Gregord89275b2009-07-06 18:54:52 +00004882
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004883void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004884 assert(NumCurrentElementsDeserializing &&
4885 "FinishedDeserializing not paired with StartedDeserializing");
4886 if (NumCurrentElementsDeserializing == 1) {
Douglas Gregord89275b2009-07-06 18:54:52 +00004887 // If any identifiers with corresponding top-level declarations have
4888 // been loaded, load those declarations now.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004889 while (!PendingIdentifierInfos.empty()) {
4890 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
4891 PendingIdentifierInfos.front().DeclIDs, true);
4892 PendingIdentifierInfos.pop_front();
Douglas Gregord89275b2009-07-06 18:54:52 +00004893 }
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00004894
Argyrios Kyrtzidis0895d152011-02-12 07:50:47 +00004895 // Ready to load previous declarations of Decls that were delayed.
4896 while (!PendingPreviousDecls.empty()) {
4897 loadAndAttachPreviousDecl(PendingPreviousDecls.front().first,
4898 PendingPreviousDecls.front().second);
4899 PendingPreviousDecls.pop_front();
4900 }
4901
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00004902 // We are not in recursive loading, so it's safe to pass the "interesting"
4903 // decls to the consumer.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004904 if (Consumer)
4905 PassInterestingDeclsToConsumer();
Argyrios Kyrtzidis134db1f2010-10-24 17:26:31 +00004906
4907 assert(PendingForwardRefs.size() == 0 &&
4908 "Some forward refs did not get linked to the definition!");
Douglas Gregord89275b2009-07-06 18:54:52 +00004909 }
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004910 --NumCurrentElementsDeserializing;
Douglas Gregord89275b2009-07-06 18:54:52 +00004911}
Douglas Gregor501c1032010-08-19 00:28:17 +00004912
Sebastian Redle1dde812010-08-24 00:50:04 +00004913ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context,
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00004914 const char *isysroot, bool DisableValidation,
4915 bool DisableStatCache)
Sebastian Redle1dde812010-08-24 00:50:04 +00004916 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
4917 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
4918 Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context),
4919 Consumer(0), isysroot(isysroot), DisableValidation(DisableValidation),
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00004920 DisableStatCache(DisableStatCache), NumStatHits(0), NumStatMisses(0),
4921 NumSLocEntriesRead(0), TotalNumSLocEntries(0), NextSLocOffset(0),
4922 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
4923 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
4924 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
4925 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
4926 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
4927 NumCurrentElementsDeserializing(0)
4928{
Sebastian Redle1dde812010-08-24 00:50:04 +00004929 RelocatablePCH = false;
4930}
4931
4932ASTReader::ASTReader(SourceManager &SourceMgr, FileManager &FileMgr,
4933 Diagnostic &Diags, const char *isysroot,
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00004934 bool DisableValidation, bool DisableStatCache)
Sebastian Redle1dde812010-08-24 00:50:04 +00004935 : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr),
4936 Diags(Diags), SemaObj(0), PP(0), Context(0), Consumer(0),
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00004937 isysroot(isysroot), DisableValidation(DisableValidation),
4938 DisableStatCache(DisableStatCache), NumStatHits(0), NumStatMisses(0),
4939 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Sebastian Redl8db9fae2010-09-22 20:19:08 +00004940 NextSLocOffset(0), NumStatementsRead(0), TotalNumStatements(0),
4941 NumMacrosRead(0), TotalNumMacros(0), NumSelectorsRead(0),
4942 NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
4943 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
4944 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
4945 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
Sebastian Redle1dde812010-08-24 00:50:04 +00004946 RelocatablePCH = false;
4947}
4948
4949ASTReader::~ASTReader() {
4950 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
4951 delete Chain[e - i - 1];
4952 // Delete all visible decl lookup tables
4953 for (DeclContextOffsetsMap::iterator I = DeclContextOffsets.begin(),
4954 E = DeclContextOffsets.end();
4955 I != E; ++I) {
4956 for (DeclContextInfos::iterator J = I->second.begin(), F = I->second.end();
4957 J != F; ++J) {
4958 if (J->NameLookupTableData)
4959 delete static_cast<ASTDeclContextNameLookupTable*>(
4960 J->NameLookupTableData);
4961 }
4962 }
4963 for (DeclContextVisibleUpdatesPending::iterator
4964 I = PendingVisibleUpdates.begin(),
4965 E = PendingVisibleUpdates.end();
4966 I != E; ++I) {
4967 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
4968 F = I->second.end();
4969 J != F; ++J)
4970 delete static_cast<ASTDeclContextNameLookupTable*>(*J);
4971 }
4972}
4973
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00004974ASTReader::PerFileData::PerFileData(ASTFileType Ty)
4975 : Type(Ty), SizeInBits(0), LocalNumSLocEntries(0), SLocOffsets(0), LocalSLocSize(0),
Sebastian Redl301c9b02010-09-22 00:42:27 +00004976 LocalNumIdentifiers(0), IdentifierOffsets(0), IdentifierTableData(0),
4977 IdentifierLookupTable(0), LocalNumMacroDefinitions(0),
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00004978 MacroDefinitionOffsets(0),
4979 LocalNumHeaderFileInfos(0), HeaderFileInfoTableData(0),
4980 HeaderFileInfoTable(0),
4981 LocalNumSelectors(0), SelectorOffsets(0),
Sebastian Redl301c9b02010-09-22 00:42:27 +00004982 SelectorLookupTableData(0), SelectorLookupTable(0), LocalNumDecls(0),
Douglas Gregor7c789c12010-10-29 22:39:52 +00004983 DeclOffsets(0), LocalNumCXXBaseSpecifiers(0), CXXBaseSpecifiersOffsets(0),
4984 LocalNumTypes(0), TypeOffsets(0), StatCache(0),
Sebastian Redla866e652010-10-01 19:59:12 +00004985 NumPreallocatedPreprocessingEntities(0), NextInSource(0)
Douglas Gregor501c1032010-08-19 00:28:17 +00004986{}
4987
4988ASTReader::PerFileData::~PerFileData() {
4989 delete static_cast<ASTIdentifierLookupTable *>(IdentifierLookupTable);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00004990 delete static_cast<HeaderFileInfoLookupTable *>(HeaderFileInfoTable);
Douglas Gregor501c1032010-08-19 00:28:17 +00004991 delete static_cast<ASTSelectorLookupTable *>(SelectorLookupTable);
4992}
4993