blob: 4994b4944e21c912cf8729b731d8509514620a64 [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"
Daniel Dunbard5b21972009-11-18 19:50:41 +000043#include "llvm/System/Path.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000044#include <algorithm>
Douglas Gregore721f952009-04-28 18:58:38 +000045#include <iterator>
Douglas Gregor2cf26342009-04-09 22:27:44 +000046#include <cstdio>
Douglas Gregor4fed3f42009-04-27 18:38:38 +000047#include <sys/stat.h>
Douglas Gregor2cf26342009-04-09 22:27:44 +000048using namespace clang;
Sebastian Redl8538e8d2010-08-18 23:57:32 +000049using namespace clang::serialization;
Douglas Gregor2cf26342009-04-09 22:27:44 +000050
51//===----------------------------------------------------------------------===//
Sebastian Redl3c7f4132010-08-18 23:57:06 +000052// PCH validator implementation
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000053//===----------------------------------------------------------------------===//
54
Sebastian Redl571db7f2010-08-18 23:56:56 +000055ASTReaderListener::~ASTReaderListener() {}
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000056
57bool
58PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts) {
59 const LangOptions &PPLangOpts = PP.getLangOptions();
60#define PARSE_LANGOPT_BENIGN(Option)
61#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
62 if (PPLangOpts.Option != LangOpts.Option) { \
63 Reader.Diag(DiagID) << LangOpts.Option << PPLangOpts.Option; \
64 return true; \
65 }
66
67 PARSE_LANGOPT_BENIGN(Trigraphs);
68 PARSE_LANGOPT_BENIGN(BCPLComment);
69 PARSE_LANGOPT_BENIGN(DollarIdents);
70 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
71 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
Chandler Carrutheb5d7b72010-04-17 20:17:31 +000072 PARSE_LANGOPT_IMPORTANT(GNUKeywords, diag::warn_pch_gnu_keywords);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000073 PARSE_LANGOPT_BENIGN(ImplicitInt);
74 PARSE_LANGOPT_BENIGN(Digraphs);
75 PARSE_LANGOPT_BENIGN(HexFloats);
76 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
77 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
Michael J. Spencerdae4ac42010-10-21 05:21:48 +000078 PARSE_LANGOPT_BENIGN(MSCVersion);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000079 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
80 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
81 PARSE_LANGOPT_BENIGN(CXXOperatorName);
82 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
83 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
84 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
Fariborz Jahanian412e7982010-02-09 19:31:38 +000085 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI2, diag::warn_pch_nonfragile_abi2);
Michael J. Spencer20249a12010-10-21 03:16:25 +000086 PARSE_LANGOPT_IMPORTANT(NoConstantCFStrings,
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +000087 diag::warn_pch_no_constant_cfstrings);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000088 PARSE_LANGOPT_BENIGN(PascalStrings);
89 PARSE_LANGOPT_BENIGN(WritableStrings);
Mike Stump1eb44332009-09-09 15:08:12 +000090 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000091 diag::warn_pch_lax_vector_conversions);
Nate Begeman69cfb9b2009-06-25 22:57:40 +000092 PARSE_LANGOPT_IMPORTANT(AltiVec, diag::warn_pch_altivec);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000093 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
Daniel Dunbar73482882010-02-10 18:48:44 +000094 PARSE_LANGOPT_IMPORTANT(SjLjExceptions, diag::warn_pch_sjlj_exceptions);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000095 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
96 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
97 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
Mike Stump1eb44332009-09-09 15:08:12 +000098 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000099 diag::warn_pch_thread_safe_statics);
Daniel Dunbar5345c392009-09-03 04:54:28 +0000100 PARSE_LANGOPT_IMPORTANT(POSIXThreads, diag::warn_pch_posix_threads);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000101 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
102 PARSE_LANGOPT_BENIGN(EmitAllDecls);
103 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
Chris Lattnera4d71452010-06-26 21:25:03 +0000104 PARSE_LANGOPT_BENIGN(getSignedOverflowBehavior());
Mike Stump1eb44332009-09-09 15:08:12 +0000105 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000106 diag::warn_pch_heinous_extensions);
107 // FIXME: Most of the options below are benign if the macro wasn't
108 // used. Unfortunately, this means that a PCH compiled without
109 // optimization can't be used with optimization turned on, even
110 // though the only thing that changes is whether __OPTIMIZE__ was
111 // defined... but if __OPTIMIZE__ never showed up in the header, it
112 // doesn't matter. We could consider making this some special kind
113 // of check.
114 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
115 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
116 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
117 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
118 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
119 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
120 PARSE_LANGOPT_IMPORTANT(AccessControl, diag::warn_pch_access_control);
121 PARSE_LANGOPT_IMPORTANT(CharIsSigned, diag::warn_pch_char_signed);
John Thompsona6fda122009-11-05 20:14:16 +0000122 PARSE_LANGOPT_IMPORTANT(ShortWChar, diag::warn_pch_short_wchar);
Argyrios Kyrtzidis9a2b9d72010-10-08 00:25:19 +0000123 PARSE_LANGOPT_IMPORTANT(ShortEnums, diag::warn_pch_short_enums);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000124 if ((PPLangOpts.getGCMode() != 0) != (LangOpts.getGCMode() != 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000125 Reader.Diag(diag::warn_pch_gc_mode)
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000126 << LangOpts.getGCMode() << PPLangOpts.getGCMode();
127 return true;
128 }
129 PARSE_LANGOPT_BENIGN(getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000130 PARSE_LANGOPT_IMPORTANT(getStackProtectorMode(),
131 diag::warn_pch_stack_protector);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000132 PARSE_LANGOPT_BENIGN(InstantiationDepth);
Nate Begeman69cfb9b2009-06-25 22:57:40 +0000133 PARSE_LANGOPT_IMPORTANT(OpenCL, diag::warn_pch_opencl);
Mike Stump9c276ae2009-12-12 01:27:46 +0000134 PARSE_LANGOPT_BENIGN(CatchUndefined);
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000135 PARSE_LANGOPT_IMPORTANT(ElideConstructors, diag::warn_pch_elide_constructors);
Douglas Gregora0068fc2010-07-09 17:35:33 +0000136 PARSE_LANGOPT_BENIGN(SpellChecking);
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +0000137#undef PARSE_LANGOPT_IMPORTANT
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000138#undef PARSE_LANGOPT_BENIGN
139
140 return false;
141}
142
Daniel Dunbardc3c0d22009-11-11 00:52:11 +0000143bool PCHValidator::ReadTargetTriple(llvm::StringRef Triple) {
144 if (Triple == PP.getTargetInfo().getTriple().str())
145 return false;
146
147 Reader.Diag(diag::warn_pch_target_triple)
148 << Triple << PP.getTargetInfo().getTriple().str();
149 return true;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000150}
151
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000152struct EmptyStringRef {
Benjamin Kramerec1b1cc2010-07-14 23:19:41 +0000153 bool operator ()(llvm::StringRef r) const { return r.empty(); }
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000154};
155struct EmptyBlock {
156 bool operator ()(const PCHPredefinesBlock &r) const { return r.Data.empty(); }
157};
158
159static bool EqualConcatenations(llvm::SmallVector<llvm::StringRef, 2> L,
160 PCHPredefinesBlocks R) {
161 // First, sum up the lengths.
162 unsigned LL = 0, RL = 0;
163 for (unsigned I = 0, N = L.size(); I != N; ++I) {
164 LL += L[I].size();
165 }
166 for (unsigned I = 0, N = R.size(); I != N; ++I) {
167 RL += R[I].Data.size();
168 }
169 if (LL != RL)
170 return false;
171 if (LL == 0 && RL == 0)
172 return true;
173
174 // Kick out empty parts, they confuse the algorithm below.
175 L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end());
176 R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end());
177
178 // Do it the hard way. At this point, both vectors must be non-empty.
179 llvm::StringRef LR = L[0], RR = R[0].Data;
180 unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size();
Daniel Dunbarc76c9e02010-07-16 00:00:11 +0000181 (void) RN;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000182 for (;;) {
183 // Compare the current pieces.
184 if (LR.size() == RR.size()) {
185 // If they're the same length, it's pretty easy.
186 if (LR != RR)
187 return false;
188 // Both pieces are done, advance.
189 ++LI;
190 ++RI;
191 // If either string is done, they're both done, since they're the same
192 // length.
193 if (LI == LN) {
194 assert(RI == RN && "Strings not the same length after all?");
195 return true;
196 }
197 LR = L[LI];
198 RR = R[RI].Data;
199 } else if (LR.size() < RR.size()) {
200 // Right piece is longer.
201 if (!RR.startswith(LR))
202 return false;
203 ++LI;
204 assert(LI != LN && "Strings not the same length after all?");
205 RR = RR.substr(LR.size());
206 LR = L[LI];
207 } else {
208 // Left piece is longer.
209 if (!LR.startswith(RR))
210 return false;
211 ++RI;
212 assert(RI != RN && "Strings not the same length after all?");
213 LR = LR.substr(RR.size());
214 RR = R[RI].Data;
215 }
216 }
217}
218
219static std::pair<FileID, llvm::StringRef::size_type>
220FindMacro(const PCHPredefinesBlocks &Buffers, llvm::StringRef MacroDef) {
221 std::pair<FileID, llvm::StringRef::size_type> Res;
222 for (unsigned I = 0, N = Buffers.size(); I != N; ++I) {
223 Res.second = Buffers[I].Data.find(MacroDef);
224 if (Res.second != llvm::StringRef::npos) {
225 Res.first = Buffers[I].BufferID;
226 break;
227 }
228 }
229 return Res;
230}
231
232bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000233 llvm::StringRef OriginalFileName,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000234 std::string &SuggestedPredefines) {
Daniel Dunbarc7162932009-11-11 23:58:53 +0000235 // We are in the context of an implicit include, so the predefines buffer will
236 // have a #include entry for the PCH file itself (as normalized by the
237 // preprocessor initialization). Find it and skip over it in the checking
238 // below.
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000239 llvm::SmallString<256> PCHInclude;
240 PCHInclude += "#include \"";
Daniel Dunbarc7162932009-11-11 23:58:53 +0000241 PCHInclude += NormalizeDashIncludePath(OriginalFileName);
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000242 PCHInclude += "\"\n";
243 std::pair<llvm::StringRef,llvm::StringRef> Split =
244 llvm::StringRef(PP.getPredefines()).split(PCHInclude.str());
245 llvm::StringRef Left = Split.first, Right = Split.second;
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000246 if (Left == PP.getPredefines()) {
247 Error("Missing PCH include entry!");
248 return true;
249 }
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000250
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000251 // If the concatenation of all the PCH buffers is equal to the adjusted
252 // command line, we're done.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000253 llvm::SmallVector<llvm::StringRef, 2> CommandLine;
254 CommandLine.push_back(Left);
255 CommandLine.push_back(Right);
256 if (EqualConcatenations(CommandLine, Buffers))
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000257 return false;
258
259 SourceManager &SourceMgr = PP.getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000260
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000261 // The predefines buffers are different. Determine what the differences are,
262 // and whether they require us to reject the PCH file.
Daniel Dunbare6750492009-11-13 16:46:11 +0000263 llvm::SmallVector<llvm::StringRef, 8> PCHLines;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000264 for (unsigned I = 0, N = Buffers.size(); I != N; ++I)
265 Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Daniel Dunbare6750492009-11-13 16:46:11 +0000266
267 llvm::SmallVector<llvm::StringRef, 8> CmdLineLines;
268 Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000269
270 // Pick out implicit #includes after the PCH and don't consider them for
271 // validation; we will insert them into SuggestedPredefines so that the
272 // preprocessor includes them.
273 std::string IncludesAfterPCH;
274 llvm::SmallVector<llvm::StringRef, 8> AfterPCHLines;
275 Right.split(AfterPCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
276 for (unsigned i = 0, e = AfterPCHLines.size(); i != e; ++i) {
277 if (AfterPCHLines[i].startswith("#include ")) {
278 IncludesAfterPCH += AfterPCHLines[i];
279 IncludesAfterPCH += '\n';
280 } else {
281 CmdLineLines.push_back(AfterPCHLines[i]);
282 }
283 }
284
285 // Make sure we add the includes last into SuggestedPredefines before we
286 // exit this function.
287 struct AddIncludesRAII {
288 std::string &SuggestedPredefines;
289 std::string &IncludesAfterPCH;
290
291 AddIncludesRAII(std::string &SuggestedPredefines,
292 std::string &IncludesAfterPCH)
293 : SuggestedPredefines(SuggestedPredefines),
294 IncludesAfterPCH(IncludesAfterPCH) { }
295 ~AddIncludesRAII() {
296 SuggestedPredefines += IncludesAfterPCH;
297 }
298 } AddIncludes(SuggestedPredefines, IncludesAfterPCH);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000299
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000300 // Sort both sets of predefined buffer lines, since we allow some extra
301 // definitions and they may appear at any point in the output.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000302 std::sort(CmdLineLines.begin(), CmdLineLines.end());
303 std::sort(PCHLines.begin(), PCHLines.end());
304
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000305 // Determine which predefines that were used to build the PCH file are missing
306 // from the command line.
307 std::vector<llvm::StringRef> MissingPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000308 std::set_difference(PCHLines.begin(), PCHLines.end(),
309 CmdLineLines.begin(), CmdLineLines.end(),
310 std::back_inserter(MissingPredefines));
311
312 bool MissingDefines = false;
313 bool ConflictingDefines = false;
314 for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) {
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000315 llvm::StringRef Missing = MissingPredefines[I];
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000316 if (Missing.startswith("#include ")) {
317 // An -include was specified when generating the PCH; it is included in
318 // the PCH, just ignore it.
319 continue;
320 }
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000321 if (!Missing.startswith("#define ")) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000322 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
323 return true;
324 }
Mike Stump1eb44332009-09-09 15:08:12 +0000325
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000326 // This is a macro definition. Determine the name of the macro we're
327 // defining.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000328 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000329 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000330 = Missing.find_first_of("( \n\r", StartOfMacroName);
331 assert(EndOfMacroName != std::string::npos &&
332 "Couldn't find the end of the macro name");
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000333 llvm::StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000334
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000335 // Determine whether this macro was given a different definition on the
336 // command line.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000337 std::string MacroDefStart = "#define " + MacroName.str();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000338 std::string::size_type MacroDefLen = MacroDefStart.size();
Daniel Dunbare6750492009-11-13 16:46:11 +0000339 llvm::SmallVector<llvm::StringRef, 8>::iterator ConflictPos
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000340 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
341 MacroDefStart);
342 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000343 if (!ConflictPos->startswith(MacroDefStart)) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000344 // Different macro; we're done.
345 ConflictPos = CmdLineLines.end();
Mike Stump1eb44332009-09-09 15:08:12 +0000346 break;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000347 }
Mike Stump1eb44332009-09-09 15:08:12 +0000348
349 assert(ConflictPos->size() > MacroDefLen &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000350 "Invalid #define in predefines buffer?");
Mike Stump1eb44332009-09-09 15:08:12 +0000351 if ((*ConflictPos)[MacroDefLen] != ' ' &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000352 (*ConflictPos)[MacroDefLen] != '(')
353 continue; // Longer macro name; keep trying.
Mike Stump1eb44332009-09-09 15:08:12 +0000354
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000355 // We found a conflicting macro definition.
356 break;
357 }
Mike Stump1eb44332009-09-09 15:08:12 +0000358
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000359 if (ConflictPos != CmdLineLines.end()) {
360 Reader.Diag(diag::warn_cmdline_conflicting_macro_def)
361 << MacroName;
362
363 // Show the definition of this macro within the PCH file.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000364 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
365 FindMacro(Buffers, Missing);
366 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
367 SourceLocation PCHMissingLoc =
368 SourceMgr.getLocForStartOfFile(MacroLoc.first)
369 .getFileLocWithOffset(MacroLoc.second);
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000370 Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000371
372 ConflictingDefines = true;
373 continue;
374 }
Mike Stump1eb44332009-09-09 15:08:12 +0000375
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000376 // If the macro doesn't conflict, then we'll just pick up the macro
377 // definition from the PCH file. Warn the user that they made a mistake.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000378 if (ConflictingDefines)
379 continue; // Don't complain if there are already conflicting defs
Mike Stump1eb44332009-09-09 15:08:12 +0000380
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000381 if (!MissingDefines) {
382 Reader.Diag(diag::warn_cmdline_missing_macro_defs);
383 MissingDefines = true;
384 }
385
386 // Show the definition of this macro within the PCH file.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000387 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
388 FindMacro(Buffers, Missing);
389 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
390 SourceLocation PCHMissingLoc =
391 SourceMgr.getLocForStartOfFile(MacroLoc.first)
392 .getFileLocWithOffset(MacroLoc.second);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000393 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
394 }
Mike Stump1eb44332009-09-09 15:08:12 +0000395
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000396 if (ConflictingDefines)
397 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000398
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000399 // Determine what predefines were introduced based on command-line
400 // parameters that were not present when building the PCH
401 // file. Extra #defines are okay, so long as the identifiers being
402 // defined were not used within the precompiled header.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000403 std::vector<llvm::StringRef> ExtraPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000404 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
405 PCHLines.begin(), PCHLines.end(),
Mike Stump1eb44332009-09-09 15:08:12 +0000406 std::back_inserter(ExtraPredefines));
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000407 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000408 llvm::StringRef &Extra = ExtraPredefines[I];
409 if (!Extra.startswith("#define ")) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000410 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
411 return true;
412 }
413
414 // This is an extra macro definition. Determine the name of the
415 // macro we're defining.
416 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000417 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000418 = Extra.find_first_of("( \n\r", StartOfMacroName);
419 assert(EndOfMacroName != std::string::npos &&
420 "Couldn't find the end of the macro name");
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000421 llvm::StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000422
423 // Check whether this name was used somewhere in the PCH file. If
424 // so, defining it as a macro could change behavior, so we reject
425 // the PCH file.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000426 if (IdentifierInfo *II = Reader.get(MacroName)) {
Daniel Dunbar4fda42e2009-11-11 00:52:00 +0000427 Reader.Diag(diag::warn_macro_name_used_in_pch) << II;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000428 return true;
429 }
430
431 // Add this definition to the suggested predefines buffer.
432 SuggestedPredefines += Extra;
433 SuggestedPredefines += '\n';
434 }
435
436 // If we get here, it's because the predefines buffer had compatible
437 // contents. Accept the PCH file.
438 return false;
439}
440
Douglas Gregor12fab312010-03-16 16:35:32 +0000441void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
442 unsigned ID) {
443 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
444 ++NumHeaderInfos;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000445}
446
447void PCHValidator::ReadCounter(unsigned Value) {
448 PP.setCounterValue(Value);
449}
450
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000451//===----------------------------------------------------------------------===//
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000452// AST reader implementation
Douglas Gregor668c1a42009-04-21 22:25:48 +0000453//===----------------------------------------------------------------------===//
454
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000455void
Sebastian Redl571db7f2010-08-18 23:56:56 +0000456ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000457 DeserializationListener = Listener;
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000458}
459
Chris Lattner4c6f9522009-04-27 05:14:47 +0000460
Douglas Gregor668c1a42009-04-21 22:25:48 +0000461namespace {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000462class ASTSelectorLookupTrait {
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000463 ASTReader &Reader;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000464
465public:
Sebastian Redl5d050072010-08-04 17:20:04 +0000466 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000467 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +0000468 ObjCMethodList Instance, Factory;
469 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000470
471 typedef Selector external_key_type;
472 typedef external_key_type internal_key_type;
473
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000474 explicit ASTSelectorLookupTrait(ASTReader &Reader) : Reader(Reader) { }
Mike Stump1eb44332009-09-09 15:08:12 +0000475
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000476 static bool EqualKey(const internal_key_type& a,
477 const internal_key_type& b) {
478 return a == b;
479 }
Mike Stump1eb44332009-09-09 15:08:12 +0000480
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000481 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +0000482 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000483 }
Mike Stump1eb44332009-09-09 15:08:12 +0000484
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000485 // This hopefully will just get inlined and removed by the optimizer.
486 static const internal_key_type&
487 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump1eb44332009-09-09 15:08:12 +0000488
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000489 static std::pair<unsigned, unsigned>
490 ReadKeyDataLength(const unsigned char*& d) {
491 using namespace clang::io;
492 unsigned KeyLen = ReadUnalignedLE16(d);
493 unsigned DataLen = ReadUnalignedLE16(d);
494 return std::make_pair(KeyLen, DataLen);
495 }
Mike Stump1eb44332009-09-09 15:08:12 +0000496
Douglas Gregor83941df2009-04-25 17:48:32 +0000497 internal_key_type ReadKey(const unsigned char* d, unsigned) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000498 using namespace clang::io;
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000499 SelectorTable &SelTable = Reader.getContext()->Selectors;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000500 unsigned N = ReadUnalignedLE16(d);
Mike Stump1eb44332009-09-09 15:08:12 +0000501 IdentifierInfo *FirstII
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000502 = Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
503 if (N == 0)
504 return SelTable.getNullarySelector(FirstII);
505 else if (N == 1)
506 return SelTable.getUnarySelector(FirstII);
507
508 llvm::SmallVector<IdentifierInfo *, 16> Args;
509 Args.push_back(FirstII);
510 for (unsigned I = 1; I != N; ++I)
511 Args.push_back(Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)));
512
Douglas Gregor75fdb232009-05-22 22:45:36 +0000513 return SelTable.getSelector(N, Args.data());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000514 }
Mike Stump1eb44332009-09-09 15:08:12 +0000515
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000516 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen) {
517 using namespace clang::io;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000518
519 data_type Result;
520
Sebastian Redl5d050072010-08-04 17:20:04 +0000521 Result.ID = ReadUnalignedLE32(d);
522 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
523 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
524
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000525 // Load instance methods
526 ObjCMethodList *Prev = 0;
527 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000528 ObjCMethodDecl *Method
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000529 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl5d050072010-08-04 17:20:04 +0000530 if (!Result.Instance.Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000531 // This is the first method, which is the easy case.
Sebastian Redl5d050072010-08-04 17:20:04 +0000532 Result.Instance.Method = Method;
533 Prev = &Result.Instance;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000534 continue;
535 }
536
Ted Kremenek298ed872010-02-11 00:53:01 +0000537 ObjCMethodList *Mem =
538 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
539 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000540 Prev = Prev->Next;
541 }
542
543 // Load factory methods
544 Prev = 0;
545 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000546 ObjCMethodDecl *Method
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000547 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl5d050072010-08-04 17:20:04 +0000548 if (!Result.Factory.Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000549 // This is the first method, which is the easy case.
Sebastian Redl5d050072010-08-04 17:20:04 +0000550 Result.Factory.Method = Method;
551 Prev = &Result.Factory;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000552 continue;
553 }
554
Ted Kremenek298ed872010-02-11 00:53:01 +0000555 ObjCMethodList *Mem =
556 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
557 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000558 Prev = Prev->Next;
559 }
560
561 return Result;
562 }
563};
Mike Stump1eb44332009-09-09 15:08:12 +0000564
565} // end anonymous namespace
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000566
567/// \brief The on-disk hash table used for the global method pool.
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000568typedef OnDiskChainedHashTable<ASTSelectorLookupTrait>
569 ASTSelectorLookupTable;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000570
Sebastian Redlc3632732010-10-05 15:59:54 +0000571namespace clang {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000572class ASTIdentifierLookupTrait {
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000573 ASTReader &Reader;
Sebastian Redlc3632732010-10-05 15:59:54 +0000574 ASTReader::PerFileData &F;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000575
576 // If we know the IdentifierInfo in advance, it is here and we will
577 // not build a new one. Used when deserializing information about an
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000578 // identifier that was constructed before the AST file was read.
Douglas Gregor668c1a42009-04-21 22:25:48 +0000579 IdentifierInfo *KnownII;
580
581public:
582 typedef IdentifierInfo * data_type;
583
584 typedef const std::pair<const char*, unsigned> external_key_type;
585
586 typedef external_key_type internal_key_type;
587
Sebastian Redlc3632732010-10-05 15:59:54 +0000588 ASTIdentifierLookupTrait(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redld27d3fc2010-07-21 22:31:37 +0000589 IdentifierInfo *II = 0)
Sebastian Redlc3632732010-10-05 15:59:54 +0000590 : Reader(Reader), F(F), KnownII(II) { }
Mike Stump1eb44332009-09-09 15:08:12 +0000591
Douglas Gregor668c1a42009-04-21 22:25:48 +0000592 static bool EqualKey(const internal_key_type& a,
593 const internal_key_type& b) {
594 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
595 : false;
596 }
Mike Stump1eb44332009-09-09 15:08:12 +0000597
Douglas Gregor668c1a42009-04-21 22:25:48 +0000598 static unsigned ComputeHash(const internal_key_type& a) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000599 return llvm::HashString(llvm::StringRef(a.first, a.second));
Douglas Gregor668c1a42009-04-21 22:25:48 +0000600 }
Mike Stump1eb44332009-09-09 15:08:12 +0000601
Douglas Gregor668c1a42009-04-21 22:25:48 +0000602 // This hopefully will just get inlined and removed by the optimizer.
603 static const internal_key_type&
604 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump1eb44332009-09-09 15:08:12 +0000605
Douglas Gregor95f42922010-10-14 22:11:03 +0000606 // This hopefully will just get inlined and removed by the optimizer.
607 static const external_key_type&
608 GetExternalKey(const internal_key_type& x) { return x; }
609
Douglas Gregor668c1a42009-04-21 22:25:48 +0000610 static std::pair<unsigned, unsigned>
611 ReadKeyDataLength(const unsigned char*& d) {
612 using namespace clang::io;
Douglas Gregor5f8e3302009-04-25 20:26:24 +0000613 unsigned DataLen = ReadUnalignedLE16(d);
Douglas Gregord6595a42009-04-25 21:04:17 +0000614 unsigned KeyLen = ReadUnalignedLE16(d);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000615 return std::make_pair(KeyLen, DataLen);
616 }
Mike Stump1eb44332009-09-09 15:08:12 +0000617
Douglas Gregor668c1a42009-04-21 22:25:48 +0000618 static std::pair<const char*, unsigned>
619 ReadKey(const unsigned char* d, unsigned n) {
620 assert(n >= 2 && d[n-1] == '\0');
621 return std::make_pair((const char*) d, n-1);
622 }
Mike Stump1eb44332009-09-09 15:08:12 +0000623
624 IdentifierInfo *ReadData(const internal_key_type& k,
Douglas Gregor668c1a42009-04-21 22:25:48 +0000625 const unsigned char* d,
626 unsigned DataLen) {
627 using namespace clang::io;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000628 IdentID ID = ReadUnalignedLE32(d);
Douglas Gregora92193e2009-04-28 21:18:29 +0000629 bool IsInteresting = ID & 0x01;
630
631 // Wipe out the "is interesting" bit.
632 ID = ID >> 1;
633
634 if (!IsInteresting) {
Sebastian Redl083abdf2010-07-27 23:01:28 +0000635 // For uninteresting identifiers, just build the IdentifierInfo
Douglas Gregora92193e2009-04-28 21:18:29 +0000636 // and associate it with the persistent ID.
637 IdentifierInfo *II = KnownII;
638 if (!II)
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000639 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregora92193e2009-04-28 21:18:29 +0000640 Reader.SetIdentifierInfo(ID, II);
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000641 II->setIsFromAST();
Douglas Gregora92193e2009-04-28 21:18:29 +0000642 return II;
643 }
644
Douglas Gregor5998da52009-04-28 21:32:13 +0000645 unsigned Bits = ReadUnalignedLE16(d);
Douglas Gregor2deaea32009-04-22 18:49:13 +0000646 bool CPlusPlusOperatorKeyword = Bits & 0x01;
647 Bits >>= 1;
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000648 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
649 Bits >>= 1;
Douglas Gregor2deaea32009-04-22 18:49:13 +0000650 bool Poisoned = Bits & 0x01;
651 Bits >>= 1;
652 bool ExtensionToken = Bits & 0x01;
653 Bits >>= 1;
654 bool hasMacroDefinition = Bits & 0x01;
655 Bits >>= 1;
656 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
657 Bits >>= 10;
Mike Stump1eb44332009-09-09 15:08:12 +0000658
Douglas Gregor2deaea32009-04-22 18:49:13 +0000659 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregor5998da52009-04-28 21:32:13 +0000660 DataLen -= 6;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000661
662 // Build the IdentifierInfo itself and link the identifier ID with
663 // the new IdentifierInfo.
664 IdentifierInfo *II = KnownII;
665 if (!II)
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000666 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000667 Reader.SetIdentifierInfo(ID, II);
668
Douglas Gregor2deaea32009-04-22 18:49:13 +0000669 // Set or check the various bits in the IdentifierInfo structure.
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000670 // Token IDs are read-only.
671 if (HasRevertedTokenIDToIdentifier)
672 II->RevertTokenIDToIdentifier();
Douglas Gregor2deaea32009-04-22 18:49:13 +0000673 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
Mike Stump1eb44332009-09-09 15:08:12 +0000674 assert(II->isExtensionToken() == ExtensionToken &&
Douglas Gregor2deaea32009-04-22 18:49:13 +0000675 "Incorrect extension token flag");
676 (void)ExtensionToken;
677 II->setIsPoisoned(Poisoned);
678 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
679 "Incorrect C++ operator keyword flag");
680 (void)CPlusPlusOperatorKeyword;
681
Douglas Gregor37e26842009-04-21 23:56:24 +0000682 // If this identifier is a macro, deserialize the macro
683 // definition.
684 if (hasMacroDefinition) {
Douglas Gregor5998da52009-04-28 21:32:13 +0000685 uint32_t Offset = ReadUnalignedLE32(d);
Douglas Gregor295a2a62010-10-30 00:23:06 +0000686 Reader.SetIdentifierIsMacro(II, F, Offset);
Douglas Gregor5998da52009-04-28 21:32:13 +0000687 DataLen -= 4;
Douglas Gregor37e26842009-04-21 23:56:24 +0000688 }
Douglas Gregor668c1a42009-04-21 22:25:48 +0000689
690 // Read all of the declarations visible at global scope with this
691 // name.
Chris Lattner6bf690f2009-04-27 22:17:41 +0000692 if (Reader.getContext() == 0) return II;
Douglas Gregord89275b2009-07-06 18:54:52 +0000693 if (DataLen > 0) {
694 llvm::SmallVector<uint32_t, 4> DeclIDs;
695 for (; DataLen > 0; DataLen -= 4)
696 DeclIDs.push_back(ReadUnalignedLE32(d));
697 Reader.SetGloballyVisibleDecls(II, DeclIDs);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000698 }
Mike Stump1eb44332009-09-09 15:08:12 +0000699
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000700 II->setIsFromAST();
Douglas Gregor668c1a42009-04-21 22:25:48 +0000701 return II;
702 }
703};
Mike Stump1eb44332009-09-09 15:08:12 +0000704
705} // end anonymous namespace
Douglas Gregor668c1a42009-04-21 22:25:48 +0000706
707/// \brief The on-disk hash table used to contain information about
708/// all of the identifiers in the program.
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000709typedef OnDiskChainedHashTable<ASTIdentifierLookupTrait>
710 ASTIdentifierLookupTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000711
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000712namespace {
713class ASTDeclContextNameLookupTrait {
714 ASTReader &Reader;
715
716public:
717 /// \brief Pair of begin/end iterators for DeclIDs.
718 typedef std::pair<DeclID *, DeclID *> data_type;
719
720 /// \brief Special internal key for declaration names.
721 /// The hash table creates keys for comparison; we do not create
722 /// a DeclarationName for the internal key to avoid deserializing types.
723 struct DeclNameKey {
724 DeclarationName::NameKind Kind;
725 uint64_t Data;
726 DeclNameKey() : Kind((DeclarationName::NameKind)0), Data(0) { }
727 };
728
729 typedef DeclarationName external_key_type;
730 typedef DeclNameKey internal_key_type;
731
732 explicit ASTDeclContextNameLookupTrait(ASTReader &Reader) : Reader(Reader) { }
733
734 static bool EqualKey(const internal_key_type& a,
735 const internal_key_type& b) {
736 return a.Kind == b.Kind && a.Data == b.Data;
737 }
738
739 unsigned ComputeHash(const DeclNameKey &Key) const {
740 llvm::FoldingSetNodeID ID;
741 ID.AddInteger(Key.Kind);
742
743 switch (Key.Kind) {
744 case DeclarationName::Identifier:
745 case DeclarationName::CXXLiteralOperatorName:
746 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
747 break;
748 case DeclarationName::ObjCZeroArgSelector:
749 case DeclarationName::ObjCOneArgSelector:
750 case DeclarationName::ObjCMultiArgSelector:
751 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
752 break;
753 case DeclarationName::CXXConstructorName:
754 case DeclarationName::CXXDestructorName:
755 case DeclarationName::CXXConversionFunctionName:
756 ID.AddInteger((TypeID)Key.Data);
757 break;
758 case DeclarationName::CXXOperatorName:
759 ID.AddInteger((OverloadedOperatorKind)Key.Data);
760 break;
761 case DeclarationName::CXXUsingDirective:
762 break;
763 }
764
765 return ID.ComputeHash();
766 }
767
768 internal_key_type GetInternalKey(const external_key_type& Name) const {
769 DeclNameKey Key;
770 Key.Kind = Name.getNameKind();
771 switch (Name.getNameKind()) {
772 case DeclarationName::Identifier:
773 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
774 break;
775 case DeclarationName::ObjCZeroArgSelector:
776 case DeclarationName::ObjCOneArgSelector:
777 case DeclarationName::ObjCMultiArgSelector:
778 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
779 break;
780 case DeclarationName::CXXConstructorName:
781 case DeclarationName::CXXDestructorName:
782 case DeclarationName::CXXConversionFunctionName:
783 Key.Data = Reader.GetTypeID(Name.getCXXNameType());
784 break;
785 case DeclarationName::CXXOperatorName:
786 Key.Data = Name.getCXXOverloadedOperator();
787 break;
788 case DeclarationName::CXXLiteralOperatorName:
789 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
790 break;
791 case DeclarationName::CXXUsingDirective:
792 break;
793 }
Michael J. Spencer20249a12010-10-21 03:16:25 +0000794
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000795 return Key;
796 }
797
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +0000798 external_key_type GetExternalKey(const internal_key_type& Key) const {
799 ASTContext *Context = Reader.getContext();
800 switch (Key.Kind) {
801 case DeclarationName::Identifier:
802 return DeclarationName((IdentifierInfo*)Key.Data);
803
804 case DeclarationName::ObjCZeroArgSelector:
805 case DeclarationName::ObjCOneArgSelector:
806 case DeclarationName::ObjCMultiArgSelector:
807 return DeclarationName(Selector(Key.Data));
808
809 case DeclarationName::CXXConstructorName:
810 return Context->DeclarationNames.getCXXConstructorName(
811 Context->getCanonicalType(Reader.GetType(Key.Data)));
812
813 case DeclarationName::CXXDestructorName:
814 return Context->DeclarationNames.getCXXDestructorName(
815 Context->getCanonicalType(Reader.GetType(Key.Data)));
816
817 case DeclarationName::CXXConversionFunctionName:
818 return Context->DeclarationNames.getCXXConversionFunctionName(
819 Context->getCanonicalType(Reader.GetType(Key.Data)));
820
821 case DeclarationName::CXXOperatorName:
822 return Context->DeclarationNames.getCXXOperatorName(
823 (OverloadedOperatorKind)Key.Data);
824
825 case DeclarationName::CXXLiteralOperatorName:
826 return Context->DeclarationNames.getCXXLiteralOperatorName(
827 (IdentifierInfo*)Key.Data);
828
829 case DeclarationName::CXXUsingDirective:
830 return DeclarationName::getUsingDirectiveName();
831 }
832
833 llvm_unreachable("Invalid Name Kind ?");
834 }
835
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000836 static std::pair<unsigned, unsigned>
837 ReadKeyDataLength(const unsigned char*& d) {
838 using namespace clang::io;
839 unsigned KeyLen = ReadUnalignedLE16(d);
840 unsigned DataLen = ReadUnalignedLE16(d);
841 return std::make_pair(KeyLen, DataLen);
842 }
843
844 internal_key_type ReadKey(const unsigned char* d, unsigned) {
845 using namespace clang::io;
846
847 DeclNameKey Key;
848 Key.Kind = (DeclarationName::NameKind)*d++;
849 switch (Key.Kind) {
850 case DeclarationName::Identifier:
851 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
852 break;
853 case DeclarationName::ObjCZeroArgSelector:
854 case DeclarationName::ObjCOneArgSelector:
855 case DeclarationName::ObjCMultiArgSelector:
Michael J. Spencer20249a12010-10-21 03:16:25 +0000856 Key.Data =
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000857 (uint64_t)Reader.DecodeSelector(ReadUnalignedLE32(d)).getAsOpaquePtr();
858 break;
859 case DeclarationName::CXXConstructorName:
860 case DeclarationName::CXXDestructorName:
861 case DeclarationName::CXXConversionFunctionName:
862 Key.Data = ReadUnalignedLE32(d); // TypeID
863 break;
864 case DeclarationName::CXXOperatorName:
865 Key.Data = *d++; // OverloadedOperatorKind
866 break;
867 case DeclarationName::CXXLiteralOperatorName:
868 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
869 break;
870 case DeclarationName::CXXUsingDirective:
871 break;
872 }
Michael J. Spencer20249a12010-10-21 03:16:25 +0000873
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000874 return Key;
875 }
876
877 data_type ReadData(internal_key_type, const unsigned char* d,
878 unsigned DataLen) {
879 using namespace clang::io;
880 unsigned NumDecls = ReadUnalignedLE16(d);
881 DeclID *Start = (DeclID *)d;
882 return std::make_pair(Start, Start + NumDecls);
883 }
884};
885
886} // end anonymous namespace
887
888/// \brief The on-disk hash table used for the DeclContext's Name lookup table.
889typedef OnDiskChainedHashTable<ASTDeclContextNameLookupTrait>
890 ASTDeclContextNameLookupTable;
891
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000892bool ASTReader::ReadDeclContextStorage(llvm::BitstreamCursor &Cursor,
893 const std::pair<uint64_t, uint64_t> &Offsets,
894 DeclContextInfo &Info) {
895 SavedStreamPosition SavedPosition(Cursor);
896 // First the lexical decls.
897 if (Offsets.first != 0) {
898 Cursor.JumpToBit(Offsets.first);
899
900 RecordData Record;
901 const char *Blob;
902 unsigned BlobLen;
903 unsigned Code = Cursor.ReadCode();
904 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
905 if (RecCode != DECL_CONTEXT_LEXICAL) {
906 Error("Expected lexical block");
907 return true;
908 }
909
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000910 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob);
911 Info.NumLexicalDecls = BlobLen / sizeof(KindDeclIDPair);
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000912 } else {
913 Info.LexicalDecls = 0;
914 Info.NumLexicalDecls = 0;
915 }
916
917 // Now the lookup table.
918 if (Offsets.second != 0) {
919 Cursor.JumpToBit(Offsets.second);
920
921 RecordData Record;
922 const char *Blob;
923 unsigned BlobLen;
924 unsigned Code = Cursor.ReadCode();
925 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
926 if (RecCode != DECL_CONTEXT_VISIBLE) {
927 Error("Expected visible lookup table block");
928 return true;
929 }
930 Info.NameLookupTableData
931 = ASTDeclContextNameLookupTable::Create(
932 (const unsigned char *)Blob + Record[0],
933 (const unsigned char *)Blob,
934 ASTDeclContextNameLookupTrait(*this));
Sebastian Redl0ea8f7f2010-08-24 00:50:00 +0000935 } else {
936 Info.NameLookupTableData = 0;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000937 }
938
939 return false;
940}
941
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000942void ASTReader::Error(const char *Msg) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000943 Diag(diag::err_fe_pch_malformed) << Msg;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000944}
945
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000946/// \brief Tell the AST listener about the predefines buffers in the chain.
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000947bool ASTReader::CheckPredefinesBuffers() {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000948 if (Listener)
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000949 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000950 ActualOriginalFileName,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000951 SuggestedPredefines);
Douglas Gregore721f952009-04-28 18:58:38 +0000952 return false;
Douglas Gregore1d918e2009-04-10 23:10:45 +0000953}
954
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000955//===----------------------------------------------------------------------===//
956// Source Manager Deserialization
957//===----------------------------------------------------------------------===//
958
Douglas Gregorbd945002009-04-13 16:31:14 +0000959/// \brief Read the line table in the source manager block.
Sebastian Redlc3632732010-10-05 15:59:54 +0000960/// \returns true if there was an error.
961bool ASTReader::ParseLineTable(PerFileData &F,
962 llvm::SmallVectorImpl<uint64_t> &Record) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000963 unsigned Idx = 0;
964 LineTableInfo &LineTable = SourceMgr.getLineTable();
965
966 // Parse the file names
Douglas Gregorff0a9872009-04-13 17:12:42 +0000967 std::map<int, int> FileIDs;
968 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000969 // Extract the file name
970 unsigned FilenameLen = Record[Idx++];
971 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
972 Idx += FilenameLen;
Douglas Gregore650c8c2009-07-07 00:12:59 +0000973 MaybeAddSystemRootToFilename(Filename);
Mike Stump1eb44332009-09-09 15:08:12 +0000974 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
Douglas Gregorff0a9872009-04-13 17:12:42 +0000975 Filename.size());
Douglas Gregorbd945002009-04-13 16:31:14 +0000976 }
977
978 // Parse the line entries
979 std::vector<LineEntry> Entries;
980 while (Idx < Record.size()) {
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000981 int FID = Record[Idx++];
Douglas Gregorbd945002009-04-13 16:31:14 +0000982
983 // Extract the line entries
984 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000985 assert(NumEntries && "Numentries is 00000");
Douglas Gregorbd945002009-04-13 16:31:14 +0000986 Entries.clear();
987 Entries.reserve(NumEntries);
988 for (unsigned I = 0; I != NumEntries; ++I) {
989 unsigned FileOffset = Record[Idx++];
990 unsigned LineNo = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000991 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump1eb44332009-09-09 15:08:12 +0000992 SrcMgr::CharacteristicKind FileKind
Douglas Gregorbd945002009-04-13 16:31:14 +0000993 = (SrcMgr::CharacteristicKind)Record[Idx++];
994 unsigned IncludeOffset = Record[Idx++];
995 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
996 FileKind, IncludeOffset));
997 }
998 LineTable.AddEntry(FID, Entries);
999 }
1000
1001 return false;
1002}
1003
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001004namespace {
1005
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001006class ASTStatData {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001007public:
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001008 const ino_t ino;
1009 const dev_t dev;
1010 const mode_t mode;
1011 const time_t mtime;
1012 const off_t size;
Mike Stump1eb44332009-09-09 15:08:12 +00001013
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001014 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Chris Lattner74e976b2010-11-23 19:28:12 +00001015 : ino(i), dev(d), mode(mo), mtime(m), size(s) {}
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001016};
1017
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001018class ASTStatLookupTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001019 public:
1020 typedef const char *external_key_type;
1021 typedef const char *internal_key_type;
1022
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001023 typedef ASTStatData data_type;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001024
1025 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001026 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001027 }
1028
1029 static internal_key_type GetInternalKey(const char *path) { return path; }
1030
1031 static bool EqualKey(internal_key_type a, internal_key_type b) {
1032 return strcmp(a, b) == 0;
1033 }
1034
1035 static std::pair<unsigned, unsigned>
1036 ReadKeyDataLength(const unsigned char*& d) {
1037 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1038 unsigned DataLen = (unsigned) *d++;
1039 return std::make_pair(KeyLen + 1, DataLen);
1040 }
1041
1042 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
1043 return (const char *)d;
1044 }
1045
1046 static data_type ReadData(const internal_key_type, const unsigned char *d,
1047 unsigned /*DataLen*/) {
1048 using namespace clang::io;
1049
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001050 ino_t ino = (ino_t) ReadUnalignedLE32(d);
1051 dev_t dev = (dev_t) ReadUnalignedLE32(d);
1052 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump1eb44332009-09-09 15:08:12 +00001053 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001054 off_t size = (off_t) ReadUnalignedLE64(d);
1055 return data_type(ino, dev, mode, mtime, size);
1056 }
1057};
1058
1059/// \brief stat() cache for precompiled headers.
1060///
1061/// This cache is very similar to the stat cache used by pretokenized
1062/// headers.
Chris Lattner10e286a2010-11-23 19:19:34 +00001063class ASTStatCache : public FileSystemStatCache {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001064 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001065 CacheTy *Cache;
1066
1067 unsigned &NumStatHits, &NumStatMisses;
Mike Stump1eb44332009-09-09 15:08:12 +00001068public:
Chris Lattner74e976b2010-11-23 19:28:12 +00001069 ASTStatCache(const unsigned char *Buckets, const unsigned char *Base,
1070 unsigned &NumStatHits, unsigned &NumStatMisses)
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001071 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
1072 Cache = CacheTy::Create(Buckets, Base);
1073 }
1074
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001075 ~ASTStatCache() { delete Cache; }
Mike Stump1eb44332009-09-09 15:08:12 +00001076
Chris Lattner898a0612010-11-23 21:17:56 +00001077 LookupResult getStat(const char *Path, struct stat &StatBuf,
1078 int *FileDescriptor) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001079 // Do the lookup for the file's data in the AST file.
Chris Lattner10e286a2010-11-23 19:19:34 +00001080 CacheTy::iterator I = Cache->find(Path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001081
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001082 // If we don't get a hit in the AST file just forward to 'stat'.
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001083 if (I == Cache->end()) {
1084 ++NumStatMisses;
Chris Lattner898a0612010-11-23 21:17:56 +00001085 return statChained(Path, StatBuf, FileDescriptor);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001086 }
Mike Stump1eb44332009-09-09 15:08:12 +00001087
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001088 ++NumStatHits;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001089 ASTStatData Data = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001090
Chris Lattner10e286a2010-11-23 19:19:34 +00001091 StatBuf.st_ino = Data.ino;
1092 StatBuf.st_dev = Data.dev;
1093 StatBuf.st_mtime = Data.mtime;
1094 StatBuf.st_mode = Data.mode;
1095 StatBuf.st_size = Data.size;
Chris Lattnerd6f61112010-11-23 20:05:15 +00001096 return CacheExists;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001097 }
1098};
1099} // end anonymous namespace
1100
1101
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001102/// \brief Read a source manager block
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001103ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(PerFileData &F) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001104 using namespace SrcMgr;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001105
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001106 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001107
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001108 // Set the source-location entry cursor to the current position in
1109 // the stream. This cursor will be used to read the contents of the
1110 // source manager block initially, and then lazily read
1111 // source-location entries as needed.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001112 SLocEntryCursor = F.Stream;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001113
1114 // The stream itself is going to skip over the source manager block.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001115 if (F.Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001116 Error("malformed block record in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001117 return Failure;
1118 }
1119
1120 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001121 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001122 Error("malformed source manager block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001123 return Failure;
1124 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001125
Douglas Gregor14f79002009-04-10 03:52:48 +00001126 RecordData Record;
1127 while (true) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001128 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregor14f79002009-04-10 03:52:48 +00001129 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001130 if (SLocEntryCursor.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001131 Error("error at end of Source Manager block in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001132 return Failure;
1133 }
Douglas Gregore1d918e2009-04-10 23:10:45 +00001134 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +00001135 }
Mike Stump1eb44332009-09-09 15:08:12 +00001136
Douglas Gregor14f79002009-04-10 03:52:48 +00001137 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1138 // No known subblocks, always skip them.
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001139 SLocEntryCursor.ReadSubBlockID();
1140 if (SLocEntryCursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001141 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001142 return Failure;
1143 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001144 continue;
1145 }
Mike Stump1eb44332009-09-09 15:08:12 +00001146
Douglas Gregor14f79002009-04-10 03:52:48 +00001147 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001148 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregor14f79002009-04-10 03:52:48 +00001149 continue;
1150 }
Mike Stump1eb44332009-09-09 15:08:12 +00001151
Douglas Gregor14f79002009-04-10 03:52:48 +00001152 // Read a record.
1153 const char *BlobStart;
1154 unsigned BlobLen;
1155 Record.clear();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001156 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001157 default: // Default behavior: ignore.
1158 break;
1159
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001160 case SM_LINE_TABLE:
Sebastian Redlc3632732010-10-05 15:59:54 +00001161 if (ParseLineTable(F, Record))
Douglas Gregorbd945002009-04-13 16:31:14 +00001162 return Failure;
Chris Lattner2c78b872009-04-14 23:22:57 +00001163 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001164
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001165 case SM_SLOC_FILE_ENTRY:
1166 case SM_SLOC_BUFFER_ENTRY:
1167 case SM_SLOC_INSTANTIATION_ENTRY:
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001168 // Once we hit one of the source location entries, we're done.
1169 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +00001170 }
1171 }
1172}
1173
Sebastian Redl190faf72010-07-20 21:50:20 +00001174/// \brief Get a cursor that's correctly positioned for reading the source
1175/// location entry with the given ID.
Sebastian Redlc3632732010-10-05 15:59:54 +00001176ASTReader::PerFileData *ASTReader::SLocCursorForID(unsigned ID) {
Sebastian Redl190faf72010-07-20 21:50:20 +00001177 assert(ID != 0 && ID <= TotalNumSLocEntries &&
1178 "SLocCursorForID should only be called for real IDs.");
1179
1180 ID -= 1;
1181 PerFileData *F = 0;
1182 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1183 F = Chain[N - I - 1];
1184 if (ID < F->LocalNumSLocEntries)
1185 break;
1186 ID -= F->LocalNumSLocEntries;
1187 }
1188 assert(F && F->LocalNumSLocEntries > ID && "Chain corrupted");
1189
1190 F->SLocEntryCursor.JumpToBit(F->SLocOffsets[ID]);
Sebastian Redlc3632732010-10-05 15:59:54 +00001191 return F;
Sebastian Redl190faf72010-07-20 21:50:20 +00001192}
1193
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001194/// \brief Read in the source location entry with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001195ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(unsigned ID) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001196 if (ID == 0)
1197 return Success;
1198
1199 if (ID > TotalNumSLocEntries) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001200 Error("source location entry ID out-of-range for AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001201 return Failure;
1202 }
1203
Sebastian Redlc3632732010-10-05 15:59:54 +00001204 PerFileData *F = SLocCursorForID(ID);
1205 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001206
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001207 ++NumSLocEntriesRead;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001208 unsigned Code = SLocEntryCursor.ReadCode();
1209 if (Code == llvm::bitc::END_BLOCK ||
1210 Code == llvm::bitc::ENTER_SUBBLOCK ||
1211 Code == llvm::bitc::DEFINE_ABBREV) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001212 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001213 return Failure;
1214 }
1215
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001216 RecordData Record;
1217 const char *BlobStart;
1218 unsigned BlobLen;
1219 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1220 default:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001221 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001222 return Failure;
1223
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001224 case SM_SLOC_FILE_ENTRY: {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001225 std::string Filename(BlobStart, BlobStart + BlobLen);
1226 MaybeAddSystemRootToFilename(Filename);
Chris Lattner39b49bc2010-11-23 08:35:12 +00001227 const FileEntry *File = FileMgr.getFile(Filename);
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001228 if (File == 0) {
1229 std::string ErrorStr = "could not find file '";
Douglas Gregore650c8c2009-07-07 00:12:59 +00001230 ErrorStr += Filename;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001231 ErrorStr += "' referenced by AST file";
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001232 Error(ErrorStr.c_str());
1233 return Failure;
1234 }
Mike Stump1eb44332009-09-09 15:08:12 +00001235
Douglas Gregor2d52be52010-03-21 22:49:54 +00001236 if (Record.size() < 10) {
Ted Kremenek1857f622010-03-18 21:23:05 +00001237 Error("source location entry is incorrect");
1238 return Failure;
1239 }
1240
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001241 if (!DisableValidation &&
1242 ((off_t)Record[4] != File->getSize()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001243#if !defined(LLVM_ON_WIN32)
1244 // In our regression testing, the Windows file system seems to
1245 // have inconsistent modification times that sometimes
1246 // erroneously trigger this error-handling path.
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001247 || (time_t)Record[5] != File->getModificationTime()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001248#endif
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001249 )) {
Douglas Gregor2d52be52010-03-21 22:49:54 +00001250 Diag(diag::err_fe_pch_file_modified)
1251 << Filename;
1252 return Failure;
1253 }
1254
Chris Lattner75dfb652010-11-23 09:19:42 +00001255 FileID FID = SourceMgr.createFileID(File, ReadSourceLocation(*F, Record[1]),
1256 (SrcMgr::CharacteristicKind)Record[2],
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001257 ID, Record[0]);
1258 if (Record[3])
1259 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile())
1260 .setHasLineDirectives();
1261
Douglas Gregor12fab312010-03-16 16:35:32 +00001262 // Reconstruct header-search information for this file.
1263 HeaderFileInfo HFI;
Douglas Gregor2d52be52010-03-21 22:49:54 +00001264 HFI.isImport = Record[6];
1265 HFI.DirInfo = Record[7];
1266 HFI.NumIncludes = Record[8];
1267 HFI.ControllingMacroID = Record[9];
Douglas Gregor12fab312010-03-16 16:35:32 +00001268 if (Listener)
1269 Listener->ReadHeaderFileInfo(HFI, File->getUID());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001270 break;
1271 }
1272
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001273 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001274 const char *Name = BlobStart;
1275 unsigned Offset = Record[0];
1276 unsigned Code = SLocEntryCursor.ReadCode();
1277 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001278 unsigned RecCode
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001279 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001280
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001281 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001282 Error("AST record has invalid code");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001283 return Failure;
1284 }
1285
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001286 llvm::MemoryBuffer *Buffer
Chris Lattnera0a270c2010-04-05 22:42:27 +00001287 = llvm::MemoryBuffer::getMemBuffer(llvm::StringRef(BlobStart, BlobLen - 1),
1288 Name);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001289 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID, Offset);
Mike Stump1eb44332009-09-09 15:08:12 +00001290
Douglas Gregor92b059e2009-04-28 20:33:11 +00001291 if (strcmp(Name, "<built-in>") == 0) {
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +00001292 PCHPredefinesBlock Block = {
1293 BufferID,
1294 llvm::StringRef(BlobStart, BlobLen - 1)
1295 };
1296 PCHPredefinesBuffers.push_back(Block);
Douglas Gregor92b059e2009-04-28 20:33:11 +00001297 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001298
1299 break;
1300 }
1301
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001302 case SM_SLOC_INSTANTIATION_ENTRY: {
Sebastian Redlc3632732010-10-05 15:59:54 +00001303 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001304 SourceMgr.createInstantiationLoc(SpellingLoc,
Sebastian Redlc3632732010-10-05 15:59:54 +00001305 ReadSourceLocation(*F, Record[2]),
1306 ReadSourceLocation(*F, Record[3]),
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001307 Record[4],
1308 ID,
1309 Record[0]);
1310 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001311 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001312 }
1313
1314 return Success;
1315}
1316
Chris Lattner6367f6d2009-04-27 01:05:14 +00001317/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1318/// specified cursor. Read the abbreviations that are at the top of the block
1319/// and then leave the cursor pointing into the block.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001320bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattner6367f6d2009-04-27 01:05:14 +00001321 unsigned BlockID) {
1322 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001323 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001324 return Failure;
1325 }
Mike Stump1eb44332009-09-09 15:08:12 +00001326
Chris Lattner6367f6d2009-04-27 01:05:14 +00001327 while (true) {
Douglas Gregorecdcb882010-10-20 22:00:55 +00001328 uint64_t Offset = Cursor.GetCurrentBitNo();
Chris Lattner6367f6d2009-04-27 01:05:14 +00001329 unsigned Code = Cursor.ReadCode();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001330
Chris Lattner6367f6d2009-04-27 01:05:14 +00001331 // We expect all abbrevs to be at the start of the block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001332 if (Code != llvm::bitc::DEFINE_ABBREV) {
1333 Cursor.JumpToBit(Offset);
Chris Lattner6367f6d2009-04-27 01:05:14 +00001334 return false;
Douglas Gregorecdcb882010-10-20 22:00:55 +00001335 }
Chris Lattner6367f6d2009-04-27 01:05:14 +00001336 Cursor.ReadAbbrevRecord();
1337 }
1338}
1339
Sebastian Redlc3632732010-10-05 15:59:54 +00001340void ASTReader::ReadMacroRecord(PerFileData &F, uint64_t Offset) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001341 assert(PP && "Forgot to set Preprocessor ?");
Douglas Gregorecdcb882010-10-20 22:00:55 +00001342 llvm::BitstreamCursor &Stream = F.MacroCursor;
Mike Stump1eb44332009-09-09 15:08:12 +00001343
Douglas Gregor37e26842009-04-21 23:56:24 +00001344 // Keep track of where we are in the stream, then jump back there
1345 // after reading this macro.
1346 SavedStreamPosition SavedPosition(Stream);
1347
1348 Stream.JumpToBit(Offset);
1349 RecordData Record;
1350 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1351 MacroInfo *Macro = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001352
Douglas Gregor37e26842009-04-21 23:56:24 +00001353 while (true) {
1354 unsigned Code = Stream.ReadCode();
1355 switch (Code) {
1356 case llvm::bitc::END_BLOCK:
1357 return;
1358
1359 case llvm::bitc::ENTER_SUBBLOCK:
1360 // No known subblocks, always skip them.
1361 Stream.ReadSubBlockID();
1362 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001363 Error("malformed block record in AST file");
Douglas Gregor37e26842009-04-21 23:56:24 +00001364 return;
1365 }
1366 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001367
Douglas Gregor37e26842009-04-21 23:56:24 +00001368 case llvm::bitc::DEFINE_ABBREV:
1369 Stream.ReadAbbrevRecord();
1370 continue;
1371 default: break;
1372 }
1373
1374 // Read a record.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001375 const char *BlobStart = 0;
1376 unsigned BlobLen = 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001377 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001378 PreprocessorRecordTypes RecType =
Michael J. Spencer20249a12010-10-21 03:16:25 +00001379 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart,
Douglas Gregorecdcb882010-10-20 22:00:55 +00001380 BlobLen);
Douglas Gregor37e26842009-04-21 23:56:24 +00001381 switch (RecType) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001382 case PP_MACRO_OBJECT_LIKE:
1383 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001384 // If we already have a macro, that means that we've hit the end
1385 // of the definition of the macro we were looking for. We're
1386 // done.
1387 if (Macro)
1388 return;
1389
1390 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1391 if (II == 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001392 Error("macro must have a name in AST file");
Douglas Gregor37e26842009-04-21 23:56:24 +00001393 return;
1394 }
Sebastian Redlc3632732010-10-05 15:59:54 +00001395 SourceLocation Loc = ReadSourceLocation(F, Record[1]);
Douglas Gregor37e26842009-04-21 23:56:24 +00001396 bool isUsed = Record[2];
Mike Stump1eb44332009-09-09 15:08:12 +00001397
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001398 MacroInfo *MI = PP->AllocateMacroInfo(Loc);
Douglas Gregor37e26842009-04-21 23:56:24 +00001399 MI->setIsUsed(isUsed);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001400 MI->setIsFromAST();
Mike Stump1eb44332009-09-09 15:08:12 +00001401
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001402 unsigned NextIndex = 3;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001403 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregor37e26842009-04-21 23:56:24 +00001404 // Decode function-like macro info.
1405 bool isC99VarArgs = Record[3];
1406 bool isGNUVarArgs = Record[4];
1407 MacroArgs.clear();
1408 unsigned NumArgs = Record[5];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001409 NextIndex = 6 + NumArgs;
Douglas Gregor37e26842009-04-21 23:56:24 +00001410 for (unsigned i = 0; i != NumArgs; ++i)
1411 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1412
1413 // Install function-like macro info.
1414 MI->setIsFunctionLike();
1415 if (isC99VarArgs) MI->setIsC99Varargs();
1416 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor75fdb232009-05-22 22:45:36 +00001417 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001418 PP->getPreprocessorAllocator());
Douglas Gregor37e26842009-04-21 23:56:24 +00001419 }
1420
1421 // Finally, install the macro.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001422 PP->setMacroInfo(II, MI);
Douglas Gregor37e26842009-04-21 23:56:24 +00001423
1424 // Remember that we saw this macro last so that we add the tokens that
1425 // form its body to it.
1426 Macro = MI;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001427
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001428 if (NextIndex + 1 == Record.size() && PP->getPreprocessingRecord()) {
1429 // We have a macro definition. Load it now.
1430 PP->getPreprocessingRecord()->RegisterMacroDefinition(Macro,
1431 getMacroDefinition(Record[NextIndex]));
1432 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001433
Douglas Gregor37e26842009-04-21 23:56:24 +00001434 ++NumMacrosRead;
1435 break;
1436 }
Mike Stump1eb44332009-09-09 15:08:12 +00001437
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001438 case PP_TOKEN: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001439 // If we see a TOKEN before a PP_MACRO_*, then the file is
1440 // erroneous, just pretend we didn't see this.
1441 if (Macro == 0) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001442
Douglas Gregor37e26842009-04-21 23:56:24 +00001443 Token Tok;
1444 Tok.startToken();
Sebastian Redlc3632732010-10-05 15:59:54 +00001445 Tok.setLocation(ReadSourceLocation(F, Record[0]));
Douglas Gregor37e26842009-04-21 23:56:24 +00001446 Tok.setLength(Record[1]);
1447 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1448 Tok.setIdentifierInfo(II);
1449 Tok.setKind((tok::TokenKind)Record[3]);
1450 Tok.setFlag((Token::TokenFlags)Record[4]);
1451 Macro->AddTokenToBody(Tok);
1452 break;
1453 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001454
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001455 case PP_MACRO_INSTANTIATION: {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001456 // If we already have a macro, that means that we've hit the end
1457 // of the definition of the macro we were looking for. We're
1458 // done.
1459 if (Macro)
1460 return;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001461
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001462 if (!PP->getPreprocessingRecord()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001463 Error("missing preprocessing record in AST file");
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001464 return;
1465 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001466
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001467 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1468 if (PPRec.getPreprocessedEntity(Record[0]))
1469 return;
1470
1471 MacroInstantiation *MI
1472 = new (PPRec) MacroInstantiation(DecodeIdentifierInfo(Record[3]),
Sebastian Redlc3632732010-10-05 15:59:54 +00001473 SourceRange(ReadSourceLocation(F, Record[1]),
1474 ReadSourceLocation(F, Record[2])),
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001475 getMacroDefinition(Record[4]));
1476 PPRec.SetPreallocatedEntity(Record[0], MI);
1477 return;
1478 }
1479
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001480 case PP_MACRO_DEFINITION: {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001481 // If we already have a macro, that means that we've hit the end
1482 // of the definition of the macro we were looking for. We're
1483 // done.
1484 if (Macro)
1485 return;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001486
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001487 if (!PP->getPreprocessingRecord()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001488 Error("missing preprocessing record in AST file");
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001489 return;
1490 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001491
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001492 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1493 if (PPRec.getPreprocessedEntity(Record[0]))
1494 return;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001495
Douglas Gregor77424bc2010-10-02 19:29:26 +00001496 if (Record[1] > MacroDefinitionsLoaded.size()) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001497 Error("out-of-bounds macro definition record");
1498 return;
1499 }
1500
Douglas Gregor77424bc2010-10-02 19:29:26 +00001501 // Decode the identifier info and then check again; if the macro is
Michael J. Spencer20249a12010-10-21 03:16:25 +00001502 // still defined and associated with the identifier,
Douglas Gregor77424bc2010-10-02 19:29:26 +00001503 IdentifierInfo *II = DecodeIdentifierInfo(Record[4]);
1504 if (!MacroDefinitionsLoaded[Record[1] - 1]) {
1505 MacroDefinition *MD
1506 = new (PPRec) MacroDefinition(II,
Sebastian Redlc3632732010-10-05 15:59:54 +00001507 ReadSourceLocation(F, Record[5]),
Douglas Gregorb1a7d9a2010-10-01 20:33:34 +00001508 SourceRange(
Sebastian Redlc3632732010-10-05 15:59:54 +00001509 ReadSourceLocation(F, Record[2]),
1510 ReadSourceLocation(F, Record[3])));
Michael J. Spencer20249a12010-10-21 03:16:25 +00001511
Douglas Gregor77424bc2010-10-02 19:29:26 +00001512 PPRec.SetPreallocatedEntity(Record[0], MD);
1513 MacroDefinitionsLoaded[Record[1] - 1] = MD;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001514
Douglas Gregor77424bc2010-10-02 19:29:26 +00001515 if (DeserializationListener)
1516 DeserializationListener->MacroDefinitionRead(Record[1], MD);
1517 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001518
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001519 return;
1520 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001521
Douglas Gregorecdcb882010-10-20 22:00:55 +00001522 case PP_INCLUSION_DIRECTIVE: {
1523 // If we already have a macro, that means that we've hit the end
1524 // of the definition of the macro we were looking for. We're
1525 // done.
1526 if (Macro)
1527 return;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001528
Douglas Gregorecdcb882010-10-20 22:00:55 +00001529 if (!PP->getPreprocessingRecord()) {
1530 Error("missing preprocessing record in AST file");
1531 return;
1532 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001533
Douglas Gregorecdcb882010-10-20 22:00:55 +00001534 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1535 if (PPRec.getPreprocessedEntity(Record[0]))
1536 return;
1537
1538 const char *FullFileNameStart = BlobStart + Record[3];
Michael J. Spencer20249a12010-10-21 03:16:25 +00001539 const FileEntry *File
Chris Lattner65382272010-11-21 09:55:08 +00001540 = PP->getFileManager().getFile(llvm::StringRef(FullFileNameStart,
Chris Lattner39b49bc2010-11-23 08:35:12 +00001541 BlobLen - Record[3]));
Michael J. Spencer20249a12010-10-21 03:16:25 +00001542
Douglas Gregorecdcb882010-10-20 22:00:55 +00001543 // FIXME: Stable encoding
1544 InclusionDirective::InclusionKind Kind
1545 = static_cast<InclusionDirective::InclusionKind>(Record[5]);
1546 InclusionDirective *ID
Douglas Gregor4ab829c2010-11-01 15:03:47 +00001547 = new (PPRec) InclusionDirective(PPRec, Kind,
Douglas Gregorecdcb882010-10-20 22:00:55 +00001548 llvm::StringRef(BlobStart, Record[3]),
1549 Record[4],
1550 File,
1551 SourceRange(ReadSourceLocation(F, Record[1]),
1552 ReadSourceLocation(F, Record[2])));
1553 PPRec.SetPreallocatedEntity(Record[0], ID);
1554 return;
1555 }
Sebastian Redlb57a6242010-09-27 22:18:47 +00001556 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001557 }
1558}
1559
Douglas Gregor295a2a62010-10-30 00:23:06 +00001560void ASTReader::SetIdentifierIsMacro(IdentifierInfo *II, PerFileData &F,
1561 uint64_t Offset) {
1562 // Note that this identifier has a macro definition.
1563 II->setHasMacroDefinition(true);
1564
1565 // Adjust the offset based on our position in the chain.
1566 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1567 if (Chain[I] == &F)
1568 break;
1569
1570 Offset += Chain[I]->SizeInBits;
1571 }
1572
1573 UnreadMacroRecordOffsets[II] = Offset;
1574}
1575
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001576void ASTReader::ReadDefinedMacros() {
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001577 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redlc3632732010-10-05 15:59:54 +00001578 PerFileData &F = *Chain[N - I - 1];
1579 llvm::BitstreamCursor &MacroCursor = F.MacroCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001580
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001581 // If there was no preprocessor block, skip this file.
1582 if (!MacroCursor.getBitStreamReader())
1583 continue;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001584
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001585 llvm::BitstreamCursor Cursor = MacroCursor;
Douglas Gregorecdcb882010-10-20 22:00:55 +00001586 Cursor.JumpToBit(F.MacroStartOffset);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001587
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001588 RecordData Record;
1589 while (true) {
Sebastian Redledadecc2010-09-28 02:55:49 +00001590 uint64_t Offset = Cursor.GetCurrentBitNo();
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001591 unsigned Code = Cursor.ReadCode();
Douglas Gregorecdcb882010-10-20 22:00:55 +00001592 if (Code == llvm::bitc::END_BLOCK)
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001593 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001594
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001595 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1596 // No known subblocks, always skip them.
1597 Cursor.ReadSubBlockID();
1598 if (Cursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001599 Error("malformed block record in AST file");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001600 return;
1601 }
1602 continue;
1603 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001604
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001605 if (Code == llvm::bitc::DEFINE_ABBREV) {
1606 Cursor.ReadAbbrevRecord();
1607 continue;
1608 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001609
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001610 // Read a record.
1611 const char *BlobStart;
1612 unsigned BlobLen;
1613 Record.clear();
1614 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1615 default: // Default behavior: ignore.
1616 break;
Douglas Gregor88a35862010-01-04 19:18:44 +00001617
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001618 case PP_MACRO_OBJECT_LIKE:
1619 case PP_MACRO_FUNCTION_LIKE:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001620 DecodeIdentifierInfo(Record[0]);
1621 break;
1622
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001623 case PP_TOKEN:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001624 // Ignore tokens.
1625 break;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001626
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001627 case PP_MACRO_INSTANTIATION:
1628 case PP_MACRO_DEFINITION:
Douglas Gregorecdcb882010-10-20 22:00:55 +00001629 case PP_INCLUSION_DIRECTIVE:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001630 // Read the macro record.
Sebastian Redledadecc2010-09-28 02:55:49 +00001631 // FIXME: That's a stupid way to do this. We should reuse this cursor.
Sebastian Redlc3632732010-10-05 15:59:54 +00001632 ReadMacroRecord(F, Offset);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001633 break;
1634 }
Douglas Gregor88a35862010-01-04 19:18:44 +00001635 }
1636 }
Douglas Gregor295a2a62010-10-30 00:23:06 +00001637
1638 // Drain the unread macro-record offsets map.
1639 while (!UnreadMacroRecordOffsets.empty())
1640 LoadMacroDefinition(UnreadMacroRecordOffsets.begin());
1641}
1642
1643void ASTReader::LoadMacroDefinition(
1644 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos) {
1645 assert(Pos != UnreadMacroRecordOffsets.end() && "Unknown macro definition");
1646 PerFileData *F = 0;
1647 uint64_t Offset = Pos->second;
1648 UnreadMacroRecordOffsets.erase(Pos);
1649
1650 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1651 if (Offset < Chain[I]->SizeInBits) {
1652 F = Chain[I];
1653 break;
1654 }
1655
1656 Offset -= Chain[I]->SizeInBits;
1657 }
1658 if (!F) {
1659 Error("Malformed macro record offset");
1660 return;
1661 }
1662
1663 ReadMacroRecord(*F, Offset);
1664}
1665
1666void ASTReader::LoadMacroDefinition(IdentifierInfo *II) {
1667 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos
1668 = UnreadMacroRecordOffsets.find(II);
1669 LoadMacroDefinition(Pos);
Douglas Gregor88a35862010-01-04 19:18:44 +00001670}
1671
Sebastian Redlf73c93f2010-09-15 19:54:06 +00001672MacroDefinition *ASTReader::getMacroDefinition(MacroID ID) {
Douglas Gregor77424bc2010-10-02 19:29:26 +00001673 if (ID == 0 || ID > MacroDefinitionsLoaded.size())
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001674 return 0;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001675
Douglas Gregor77424bc2010-10-02 19:29:26 +00001676 if (!MacroDefinitionsLoaded[ID - 1]) {
1677 unsigned Index = ID - 1;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001678 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1679 PerFileData &F = *Chain[N - I - 1];
1680 if (Index < F.LocalNumMacroDefinitions) {
Sebastian Redlc3632732010-10-05 15:59:54 +00001681 ReadMacroRecord(F, F.MacroDefinitionOffsets[Index]);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001682 break;
1683 }
1684 Index -= F.LocalNumMacroDefinitions;
1685 }
Douglas Gregor77424bc2010-10-02 19:29:26 +00001686 assert(MacroDefinitionsLoaded[ID - 1] && "Broken chain");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001687 }
1688
Douglas Gregor77424bc2010-10-02 19:29:26 +00001689 return MacroDefinitionsLoaded[ID - 1];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001690}
1691
Douglas Gregore650c8c2009-07-07 00:12:59 +00001692/// \brief If we are loading a relocatable PCH file, and the filename is
1693/// not an absolute path, add the system root to the beginning of the file
1694/// name.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001695void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001696 // If this is not a relocatable PCH file, there's nothing to do.
1697 if (!RelocatablePCH)
1698 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001699
Daniel Dunbard5b21972009-11-18 19:50:41 +00001700 if (Filename.empty() || llvm::sys::Path(Filename).isAbsolute())
Douglas Gregore650c8c2009-07-07 00:12:59 +00001701 return;
1702
Douglas Gregore650c8c2009-07-07 00:12:59 +00001703 if (isysroot == 0) {
1704 // If no system root was given, default to '/'
1705 Filename.insert(Filename.begin(), '/');
1706 return;
1707 }
Mike Stump1eb44332009-09-09 15:08:12 +00001708
Douglas Gregore650c8c2009-07-07 00:12:59 +00001709 unsigned Length = strlen(isysroot);
1710 if (isysroot[Length - 1] != '/')
1711 Filename.insert(Filename.begin(), '/');
Mike Stump1eb44332009-09-09 15:08:12 +00001712
Douglas Gregore650c8c2009-07-07 00:12:59 +00001713 Filename.insert(Filename.begin(), isysroot, isysroot + Length);
1714}
1715
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001716ASTReader::ASTReadResult
Sebastian Redl571db7f2010-08-18 23:56:56 +00001717ASTReader::ReadASTBlock(PerFileData &F) {
Sebastian Redl9137a522010-07-16 17:50:48 +00001718 llvm::BitstreamCursor &Stream = F.Stream;
1719
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001720 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001721 Error("malformed block record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001722 return Failure;
1723 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001724
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001725 // Read all of the records and blocks for the ASt file.
Douglas Gregor8038d512009-04-10 17:25:41 +00001726 RecordData Record;
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001727 bool First = true;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001728 while (!Stream.AtEndOfStream()) {
1729 unsigned Code = Stream.ReadCode();
1730 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001731 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001732 Error("error at end of module block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001733 return Failure;
1734 }
Chris Lattner7356a312009-04-11 21:15:38 +00001735
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001736 return Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001737 }
1738
1739 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1740 switch (Stream.ReadSubBlockID()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001741 case DECLTYPES_BLOCK_ID:
Chris Lattner6367f6d2009-04-27 01:05:14 +00001742 // We lazily load the decls block, but we want to set up the
1743 // DeclsCursor cursor to point into it. Clone our current bitcode
1744 // cursor to it, enter the block and read the abbrevs in that block.
1745 // With the main cursor, we just skip over it.
Sebastian Redl9137a522010-07-16 17:50:48 +00001746 F.DeclsCursor = Stream;
Chris Lattner6367f6d2009-04-27 01:05:14 +00001747 if (Stream.SkipBlock() || // Skip with the main cursor.
1748 // Read the abbrevs.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001749 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001750 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001751 return Failure;
1752 }
1753 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001754
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00001755 case DECL_UPDATES_BLOCK_ID:
1756 if (Stream.SkipBlock()) {
1757 Error("malformed block record in AST file");
1758 return Failure;
1759 }
1760 break;
1761
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001762 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl9137a522010-07-16 17:50:48 +00001763 F.MacroCursor = Stream;
Douglas Gregor88a35862010-01-04 19:18:44 +00001764 if (PP)
1765 PP->setExternalSource(this);
1766
Douglas Gregorecdcb882010-10-20 22:00:55 +00001767 if (Stream.SkipBlock() ||
1768 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001769 Error("malformed block record in AST file");
Chris Lattner7356a312009-04-11 21:15:38 +00001770 return Failure;
1771 }
Douglas Gregorecdcb882010-10-20 22:00:55 +00001772 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
Chris Lattner7356a312009-04-11 21:15:38 +00001773 break;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00001774
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001775 case SOURCE_MANAGER_BLOCK_ID:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001776 switch (ReadSourceManagerBlock(F)) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00001777 case Success:
1778 break;
1779
1780 case Failure:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001781 Error("malformed source manager block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001782 return Failure;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001783
1784 case IgnorePCH:
1785 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001786 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001787 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001788 }
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001789 First = false;
Douglas Gregor8038d512009-04-10 17:25:41 +00001790 continue;
1791 }
1792
1793 if (Code == llvm::bitc::DEFINE_ABBREV) {
1794 Stream.ReadAbbrevRecord();
1795 continue;
1796 }
1797
1798 // Read and process a record.
1799 Record.clear();
Douglas Gregor2bec0412009-04-10 21:16:55 +00001800 const char *BlobStart = 0;
1801 unsigned BlobLen = 0;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001802 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00001803 &BlobStart, &BlobLen)) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001804 default: // Default behavior: ignore.
1805 break;
1806
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001807 case METADATA: {
1808 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1809 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001810 : diag::warn_pch_version_too_new);
1811 return IgnorePCH;
1812 }
1813
1814 RelocatablePCH = Record[4];
1815 if (Listener) {
1816 std::string TargetTriple(BlobStart, BlobLen);
1817 if (Listener->ReadTargetTriple(TargetTriple))
1818 return IgnorePCH;
1819 }
1820 break;
1821 }
1822
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001823 case CHAINED_METADATA: {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001824 if (!First) {
1825 Error("CHAINED_METADATA is not first record in block");
1826 return Failure;
1827 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001828 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1829 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001830 : diag::warn_pch_version_too_new);
1831 return IgnorePCH;
1832 }
1833
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00001834 // Load the chained file, which is always a PCH file.
1835 switch(ReadASTCore(llvm::StringRef(BlobStart, BlobLen), PCH)) {
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001836 case Failure: return Failure;
1837 // If we have to ignore the dependency, we'll have to ignore this too.
1838 case IgnorePCH: return IgnorePCH;
1839 case Success: break;
1840 }
1841 break;
1842 }
1843
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001844 case TYPE_OFFSET:
Sebastian Redl12d6da02010-07-19 22:06:55 +00001845 if (F.LocalNumTypes != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001846 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001847 return Failure;
1848 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00001849 F.TypeOffsets = (const uint32_t *)BlobStart;
1850 F.LocalNumTypes = Record[0];
Douglas Gregor8038d512009-04-10 17:25:41 +00001851 break;
1852
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001853 case DECL_OFFSET:
Sebastian Redl12d6da02010-07-19 22:06:55 +00001854 if (F.LocalNumDecls != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001855 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001856 return Failure;
1857 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00001858 F.DeclOffsets = (const uint32_t *)BlobStart;
1859 F.LocalNumDecls = Record[0];
Douglas Gregor8038d512009-04-10 17:25:41 +00001860 break;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001861
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001862 case TU_UPDATE_LEXICAL: {
Sebastian Redld692af72010-07-27 18:24:41 +00001863 DeclContextInfo Info = {
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001864 /* No visible information */ 0,
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001865 reinterpret_cast<const KindDeclIDPair *>(BlobStart),
1866 BlobLen / sizeof(KindDeclIDPair)
Sebastian Redld692af72010-07-27 18:24:41 +00001867 };
Douglas Gregor3747ee72010-10-01 01:18:02 +00001868 DeclContextOffsets[Context ? Context->getTranslationUnitDecl() : 0]
1869 .push_back(Info);
Sebastian Redld692af72010-07-27 18:24:41 +00001870 break;
1871 }
1872
Sebastian Redle1dde812010-08-24 00:50:04 +00001873 case UPDATE_VISIBLE: {
1874 serialization::DeclID ID = Record[0];
1875 void *Table = ASTDeclContextNameLookupTable::Create(
1876 (const unsigned char *)BlobStart + Record[1],
1877 (const unsigned char *)BlobStart,
1878 ASTDeclContextNameLookupTrait(*this));
Douglas Gregor3747ee72010-10-01 01:18:02 +00001879 if (ID == 1 && Context) { // Is it the TU?
Sebastian Redle1dde812010-08-24 00:50:04 +00001880 DeclContextInfo Info = {
1881 Table, /* No lexical inforamtion */ 0, 0
1882 };
1883 DeclContextOffsets[Context->getTranslationUnitDecl()].push_back(Info);
1884 } else
1885 PendingVisibleUpdates[ID].push_back(Table);
1886 break;
1887 }
1888
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001889 case REDECLS_UPDATE_LATEST: {
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00001890 assert(Record.size() % 2 == 0 && "Expected pairs of DeclIDs");
1891 for (unsigned i = 0, e = Record.size(); i < e; i += 2) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001892 DeclID First = Record[i], Latest = Record[i+1];
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00001893 assert((FirstLatestDeclIDs.find(First) == FirstLatestDeclIDs.end() ||
1894 Latest > FirstLatestDeclIDs[First]) &&
1895 "The new latest is supposed to come after the previous latest");
1896 FirstLatestDeclIDs[First] = Latest;
1897 }
1898 break;
1899 }
1900
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001901 case LANGUAGE_OPTIONS:
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001902 if (ParseLanguageOptions(Record) && !DisableValidation)
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001903 return IgnorePCH;
1904 break;
Douglas Gregor2bec0412009-04-10 21:16:55 +00001905
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001906 case IDENTIFIER_TABLE:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001907 F.IdentifierTableData = BlobStart;
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001908 if (Record[0]) {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001909 F.IdentifierLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001910 = ASTIdentifierLookupTable::Create(
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001911 (const unsigned char *)F.IdentifierTableData + Record[0],
1912 (const unsigned char *)F.IdentifierTableData,
Sebastian Redlc3632732010-10-05 15:59:54 +00001913 ASTIdentifierLookupTrait(*this, F));
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001914 if (PP)
1915 PP->getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001916 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001917 break;
1918
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001919 case IDENTIFIER_OFFSET:
Sebastian Redl2da08f92010-07-19 22:28:42 +00001920 if (F.LocalNumIdentifiers != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001921 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00001922 return Failure;
1923 }
Sebastian Redl2da08f92010-07-19 22:28:42 +00001924 F.IdentifierOffsets = (const uint32_t *)BlobStart;
1925 F.LocalNumIdentifiers = Record[0];
Douglas Gregorafaf3082009-04-11 00:14:32 +00001926 break;
Douglas Gregorfdd01722009-04-14 00:24:19 +00001927
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001928 case EXTERNAL_DEFINITIONS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001929 // Optimization for the first block.
1930 if (ExternalDefinitions.empty())
1931 ExternalDefinitions.swap(Record);
1932 else
1933 ExternalDefinitions.insert(ExternalDefinitions.end(),
1934 Record.begin(), Record.end());
Douglas Gregorfdd01722009-04-14 00:24:19 +00001935 break;
Douglas Gregor3e1af842009-04-17 22:13:46 +00001936
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001937 case SPECIAL_TYPES:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001938 // Optimization for the first block
1939 if (SpecialTypes.empty())
1940 SpecialTypes.swap(Record);
1941 else
1942 SpecialTypes.insert(SpecialTypes.end(), Record.begin(), Record.end());
Douglas Gregorad1de002009-04-18 05:55:16 +00001943 break;
1944
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001945 case STATISTICS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001946 TotalNumStatements += Record[0];
1947 TotalNumMacros += Record[1];
1948 TotalLexicalDeclContexts += Record[2];
1949 TotalVisibleDeclContexts += Record[3];
Douglas Gregor3e1af842009-04-17 22:13:46 +00001950 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001951
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001952 case TENTATIVE_DEFINITIONS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001953 // Optimization for the first block.
1954 if (TentativeDefinitions.empty())
1955 TentativeDefinitions.swap(Record);
1956 else
1957 TentativeDefinitions.insert(TentativeDefinitions.end(),
1958 Record.begin(), Record.end());
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00001959 break;
Douglas Gregor14c22f22009-04-22 22:18:58 +00001960
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001961 case UNUSED_FILESCOPED_DECLS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001962 // Optimization for the first block.
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00001963 if (UnusedFileScopedDecls.empty())
1964 UnusedFileScopedDecls.swap(Record);
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001965 else
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00001966 UnusedFileScopedDecls.insert(UnusedFileScopedDecls.end(),
1967 Record.begin(), Record.end());
Tanya Lattnere6bbc012010-02-12 00:07:30 +00001968 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001969
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001970 case WEAK_UNDECLARED_IDENTIFIERS:
Sebastian Redl40566802010-08-05 18:21:25 +00001971 // Later blocks overwrite earlier ones.
1972 WeakUndeclaredIdentifiers.swap(Record);
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00001973 break;
1974
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001975 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001976 // Optimization for the first block.
1977 if (LocallyScopedExternalDecls.empty())
1978 LocallyScopedExternalDecls.swap(Record);
1979 else
1980 LocallyScopedExternalDecls.insert(LocallyScopedExternalDecls.end(),
1981 Record.begin(), Record.end());
Douglas Gregor14c22f22009-04-22 22:18:58 +00001982 break;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001983
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001984 case SELECTOR_OFFSETS:
Sebastian Redl059612d2010-08-03 21:58:15 +00001985 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redl725cd962010-08-04 20:40:17 +00001986 F.LocalNumSelectors = Record[0];
Douglas Gregor83941df2009-04-25 17:48:32 +00001987 break;
1988
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001989 case METHOD_POOL:
Sebastian Redl725cd962010-08-04 20:40:17 +00001990 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor83941df2009-04-25 17:48:32 +00001991 if (Record[0])
Sebastian Redl725cd962010-08-04 20:40:17 +00001992 F.SelectorLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001993 = ASTSelectorLookupTable::Create(
Sebastian Redl725cd962010-08-04 20:40:17 +00001994 F.SelectorLookupTableData + Record[0],
1995 F.SelectorLookupTableData,
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001996 ASTSelectorLookupTrait(*this));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00001997 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001998 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001999
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002000 case REFERENCED_SELECTOR_POOL:
Sebastian Redlc3632732010-10-05 15:59:54 +00002001 F.ReferencedSelectorsData.swap(Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002002 break;
2003
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002004 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002005 if (!Record.empty() && Listener)
2006 Listener->ReadCounter(Record[0]);
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002007 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002008
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002009 case SOURCE_LOCATION_OFFSETS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002010 F.SLocOffsets = (const uint32_t *)BlobStart;
2011 F.LocalNumSLocEntries = Record[0];
Sebastian Redl8db9fae2010-09-22 20:19:08 +00002012 F.LocalSLocSize = Record[1];
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002013 break;
2014
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002015 case SOURCE_LOCATION_PRELOADS:
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002016 if (PreloadSLocEntries.empty())
2017 PreloadSLocEntries.swap(Record);
2018 else
2019 PreloadSLocEntries.insert(PreloadSLocEntries.end(),
2020 Record.begin(), Record.end());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002021 break;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002022
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002023 case STAT_CACHE: {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002024 ASTStatCache *MyStatCache =
2025 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
Douglas Gregor52e71082009-10-16 18:18:30 +00002026 (const unsigned char *)BlobStart,
2027 NumStatHits, NumStatMisses);
2028 FileMgr.addStatCache(MyStatCache);
Sebastian Redl9137a522010-07-16 17:50:48 +00002029 F.StatCache = MyStatCache;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002030 break;
Douglas Gregor52e71082009-10-16 18:18:30 +00002031 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002032
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002033 case EXT_VECTOR_DECLS:
Sebastian Redla9f23682010-07-28 21:38:49 +00002034 // Optimization for the first block.
2035 if (ExtVectorDecls.empty())
2036 ExtVectorDecls.swap(Record);
2037 else
2038 ExtVectorDecls.insert(ExtVectorDecls.end(),
2039 Record.begin(), Record.end());
Douglas Gregorb81c1702009-04-27 20:06:05 +00002040 break;
2041
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002042 case VTABLE_USES:
Sebastian Redl40566802010-08-05 18:21:25 +00002043 // Later tables overwrite earlier ones.
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002044 VTableUses.swap(Record);
2045 break;
2046
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002047 case DYNAMIC_CLASSES:
Sebastian Redl40566802010-08-05 18:21:25 +00002048 // Optimization for the first block.
2049 if (DynamicClasses.empty())
2050 DynamicClasses.swap(Record);
2051 else
2052 DynamicClasses.insert(DynamicClasses.end(),
2053 Record.begin(), Record.end());
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002054 break;
2055
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002056 case PENDING_IMPLICIT_INSTANTIATIONS:
Sebastian Redlc3632732010-10-05 15:59:54 +00002057 F.PendingInstantiations.swap(Record);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002058 break;
2059
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002060 case SEMA_DECL_REFS:
Sebastian Redl40566802010-08-05 18:21:25 +00002061 // Later tables overwrite earlier ones.
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002062 SemaDeclRefs.swap(Record);
2063 break;
2064
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002065 case ORIGINAL_FILE_NAME:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002066 // The primary AST will be the last to get here, so it will be the one
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002067 // that's used.
Daniel Dunbar7b5a1212009-11-11 05:29:04 +00002068 ActualOriginalFileName.assign(BlobStart, BlobLen);
2069 OriginalFileName = ActualOriginalFileName;
Douglas Gregore650c8c2009-07-07 00:12:59 +00002070 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregorb64c1932009-05-12 01:31:05 +00002071 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002072
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002073 case VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek974be4d2010-02-12 23:31:14 +00002074 const std::string &CurBranch = getClangFullRepositoryVersion();
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002075 llvm::StringRef ASTBranch(BlobStart, BlobLen);
2076 if (llvm::StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2077 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregor445e23e2009-10-05 21:07:28 +00002078 return IgnorePCH;
2079 }
2080 break;
2081 }
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002082
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002083 case MACRO_DEFINITION_OFFSETS:
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002084 F.MacroDefinitionOffsets = (const uint32_t *)BlobStart;
2085 F.NumPreallocatedPreprocessingEntities = Record[0];
2086 F.LocalNumMacroDefinitions = Record[1];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002087 break;
Sebastian Redl0b17c612010-08-13 00:28:03 +00002088
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002089 case DECL_UPDATE_OFFSETS: {
2090 if (Record.size() % 2 != 0) {
2091 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2092 return Failure;
2093 }
2094 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
2095 DeclUpdateOffsets[static_cast<DeclID>(Record[I])]
2096 .push_back(std::make_pair(&F, Record[I+1]));
2097 break;
2098 }
2099
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002100 case DECL_REPLACEMENTS: {
Sebastian Redl0b17c612010-08-13 00:28:03 +00002101 if (Record.size() % 2 != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002102 Error("invalid DECL_REPLACEMENTS block in AST file");
Sebastian Redl0b17c612010-08-13 00:28:03 +00002103 return Failure;
2104 }
2105 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002106 ReplacedDecls[static_cast<DeclID>(Record[I])] =
Sebastian Redl0b17c612010-08-13 00:28:03 +00002107 std::make_pair(&F, Record[I+1]);
2108 break;
2109 }
Douglas Gregor7c789c12010-10-29 22:39:52 +00002110
2111 case CXX_BASE_SPECIFIER_OFFSETS: {
2112 if (F.LocalNumCXXBaseSpecifiers != 0) {
2113 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2114 return Failure;
2115 }
2116
2117 F.LocalNumCXXBaseSpecifiers = Record[0];
2118 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart;
2119 break;
2120 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002121
2122 case DIAG_USER_MAPPINGS:
2123 if (Record.size() % 2 != 0) {
2124 Error("invalid DIAG_USER_MAPPINGS block in AST file");
2125 return Failure;
2126 }
2127 if (UserDiagMappings.empty())
2128 UserDiagMappings.swap(Record);
2129 else
2130 UserDiagMappings.insert(UserDiagMappings.end(),
2131 Record.begin(), Record.end());
2132 break;
Douglas Gregorafaf3082009-04-11 00:14:32 +00002133 }
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002134 First = false;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002135 }
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002136 Error("premature end of bitstream in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002137 return Failure;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002138}
2139
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002140ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
2141 ASTFileType Type) {
2142 switch(ReadASTCore(FileName, Type)) {
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002143 case Failure: return Failure;
2144 case IgnorePCH: return IgnorePCH;
2145 case Success: break;
2146 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002147
2148 // Here comes stuff that we only do once the entire chain is loaded.
2149
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002150 // Allocate space for loaded slocentries, identifiers, decls and types.
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002151 unsigned TotalNumIdentifiers = 0, TotalNumTypes = 0, TotalNumDecls = 0,
Sebastian Redl725cd962010-08-04 20:40:17 +00002152 TotalNumPreallocatedPreprocessingEntities = 0, TotalNumMacroDefs = 0,
2153 TotalNumSelectors = 0;
Sebastian Redl12d6da02010-07-19 22:06:55 +00002154 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002155 TotalNumSLocEntries += Chain[I]->LocalNumSLocEntries;
Sebastian Redl8db9fae2010-09-22 20:19:08 +00002156 NextSLocOffset += Chain[I]->LocalSLocSize;
Sebastian Redl2da08f92010-07-19 22:28:42 +00002157 TotalNumIdentifiers += Chain[I]->LocalNumIdentifiers;
Sebastian Redl12d6da02010-07-19 22:06:55 +00002158 TotalNumTypes += Chain[I]->LocalNumTypes;
2159 TotalNumDecls += Chain[I]->LocalNumDecls;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002160 TotalNumPreallocatedPreprocessingEntities +=
2161 Chain[I]->NumPreallocatedPreprocessingEntities;
2162 TotalNumMacroDefs += Chain[I]->LocalNumMacroDefinitions;
Sebastian Redl725cd962010-08-04 20:40:17 +00002163 TotalNumSelectors += Chain[I]->LocalNumSelectors;
Sebastian Redl12d6da02010-07-19 22:06:55 +00002164 }
Sebastian Redl8db9fae2010-09-22 20:19:08 +00002165 SourceMgr.PreallocateSLocEntries(this, TotalNumSLocEntries, NextSLocOffset);
Sebastian Redl2da08f92010-07-19 22:28:42 +00002166 IdentifiersLoaded.resize(TotalNumIdentifiers);
Sebastian Redl12d6da02010-07-19 22:06:55 +00002167 TypesLoaded.resize(TotalNumTypes);
2168 DeclsLoaded.resize(TotalNumDecls);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002169 MacroDefinitionsLoaded.resize(TotalNumMacroDefs);
2170 if (PP) {
2171 if (TotalNumIdentifiers > 0)
2172 PP->getHeaderSearchInfo().SetExternalLookup(this);
2173 if (TotalNumPreallocatedPreprocessingEntities > 0) {
2174 if (!PP->getPreprocessingRecord())
2175 PP->createPreprocessingRecord();
2176 PP->getPreprocessingRecord()->SetExternalSource(*this,
2177 TotalNumPreallocatedPreprocessingEntities);
2178 }
2179 }
Sebastian Redl725cd962010-08-04 20:40:17 +00002180 SelectorsLoaded.resize(TotalNumSelectors);
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002181 // Preload SLocEntries.
2182 for (unsigned I = 0, N = PreloadSLocEntries.size(); I != N; ++I) {
2183 ASTReadResult Result = ReadSLocEntryRecord(PreloadSLocEntries[I]);
2184 if (Result != Success)
2185 return Result;
2186 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00002187
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002188 // Check the predefines buffers.
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00002189 if (!DisableValidation && CheckPredefinesBuffers())
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002190 return IgnorePCH;
2191
2192 if (PP) {
2193 // Initialization of keywords and pragmas occurs before the
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002194 // AST file is read, so there may be some identifiers that were
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002195 // loaded into the IdentifierTable before we intercepted the
2196 // creation of identifiers. Iterate through the list of known
2197 // identifiers and determine whether we have to establish
2198 // preprocessor definitions or top-level identifier declaration
2199 // chains for those identifiers.
2200 //
2201 // We copy the IdentifierInfo pointers to a small vector first,
2202 // since de-serializing declarations or macro definitions can add
2203 // new entries into the identifier table, invalidating the
2204 // iterators.
2205 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
2206 for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(),
2207 IdEnd = PP->getIdentifierTable().end();
2208 Id != IdEnd; ++Id)
2209 Identifiers.push_back(Id->second);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002210 // We need to search the tables in all files.
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002211 for (unsigned J = 0, M = Chain.size(); J != M; ++J) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002212 ASTIdentifierLookupTable *IdTable
2213 = (ASTIdentifierLookupTable *)Chain[J]->IdentifierLookupTable;
2214 // Not all AST files necessarily have identifier tables, only the useful
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00002215 // ones.
2216 if (!IdTable)
2217 continue;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002218 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
2219 IdentifierInfo *II = Identifiers[I];
2220 // Look in the on-disk hash tables for an entry for this identifier
Sebastian Redlc3632732010-10-05 15:59:54 +00002221 ASTIdentifierLookupTrait Info(*this, *Chain[J], II);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002222 std::pair<const char*,unsigned> Key(II->getNameStart(),II->getLength());
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002223 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002224 if (Pos == IdTable->end())
2225 continue;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002226
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002227 // Dereferencing the iterator has the effect of populating the
2228 // IdentifierInfo node with the various declarations it needs.
2229 (void)*Pos;
2230 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002231 }
2232 }
2233
2234 if (Context)
2235 InitializeContext(*Context);
2236
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002237 if (DeserializationListener)
2238 DeserializationListener->ReaderInitialized(this);
2239
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002240 return Success;
2241}
2242
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002243ASTReader::ASTReadResult ASTReader::ReadASTCore(llvm::StringRef FileName,
2244 ASTFileType Type) {
Sebastian Redla866e652010-10-01 19:59:12 +00002245 PerFileData *Prev = Chain.empty() ? 0 : Chain.back();
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002246 Chain.push_back(new PerFileData(Type));
Sebastian Redl9137a522010-07-16 17:50:48 +00002247 PerFileData &F = *Chain.back();
Sebastian Redla866e652010-10-01 19:59:12 +00002248 if (Prev)
2249 Prev->NextInSource = &F;
2250 else
2251 FirstInSource = &F;
2252 F.Loaders.push_back(Prev);
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002253
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002254 // Set the AST file name.
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002255 F.FileName = FileName;
2256
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002257 // Open the AST file.
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002258 //
2259 // FIXME: This shouldn't be here, we should just take a raw_ostream.
2260 std::string ErrStr;
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +00002261 if (FileName == "-")
2262 F.Buffer.reset(llvm::MemoryBuffer::getSTDIN(&ErrStr));
2263 else
Chris Lattner39b49bc2010-11-23 08:35:12 +00002264 F.Buffer.reset(FileMgr.getBufferForFile(FileName, &ErrStr));
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002265 if (!F.Buffer) {
2266 Error(ErrStr.c_str());
2267 return IgnorePCH;
2268 }
2269
2270 // Initialize the stream
2271 F.StreamFile.init((const unsigned char *)F.Buffer->getBufferStart(),
2272 (const unsigned char *)F.Buffer->getBufferEnd());
Sebastian Redl9137a522010-07-16 17:50:48 +00002273 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002274 Stream.init(F.StreamFile);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002275 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002276
2277 // Sniff for the signature.
2278 if (Stream.Read(8) != 'C' ||
2279 Stream.Read(8) != 'P' ||
2280 Stream.Read(8) != 'C' ||
2281 Stream.Read(8) != 'H') {
2282 Diag(diag::err_not_a_pch_file) << FileName;
2283 return Failure;
2284 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002285
Douglas Gregor2cf26342009-04-09 22:27:44 +00002286 while (!Stream.AtEndOfStream()) {
2287 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00002288
Douglas Gregore1d918e2009-04-10 23:10:45 +00002289 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002290 Error("invalid record at top-level of AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002291 return Failure;
2292 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002293
2294 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregor668c1a42009-04-21 22:25:48 +00002295
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002296 // We only know the AST subblock ID.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002297 switch (BlockID) {
2298 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002299 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002300 Error("malformed BlockInfoBlock in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002301 return Failure;
2302 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002303 break;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002304 case AST_BLOCK_ID:
Sebastian Redl571db7f2010-08-18 23:56:56 +00002305 switch (ReadASTBlock(F)) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002306 case Success:
2307 break;
2308
2309 case Failure:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002310 return Failure;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002311
2312 case IgnorePCH:
Douglas Gregor2bec0412009-04-10 21:16:55 +00002313 // FIXME: We could consider reading through to the end of this
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002314 // AST block, skipping subblocks, to see if there are other
2315 // AST blocks elsewhere.
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002316
2317 // Clear out any preallocated source location entries, so that
2318 // the source manager does not try to resolve them later.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002319 SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002320
2321 // Remove the stat cache.
Sebastian Redl9137a522010-07-16 17:50:48 +00002322 if (F.StatCache)
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002323 FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002324
Douglas Gregore1d918e2009-04-10 23:10:45 +00002325 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002326 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002327 break;
2328 default:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002329 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002330 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002331 return Failure;
2332 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002333 break;
2334 }
Mike Stump1eb44332009-09-09 15:08:12 +00002335 }
2336
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002337 return Success;
2338}
2339
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002340void ASTReader::setPreprocessor(Preprocessor &pp) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002341 PP = &pp;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002342
2343 unsigned TotalNum = 0;
2344 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
2345 TotalNum += Chain[I]->NumPreallocatedPreprocessingEntities;
2346 if (TotalNum) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002347 if (!PP->getPreprocessingRecord())
2348 PP->createPreprocessingRecord();
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002349 PP->getPreprocessingRecord()->SetExternalSource(*this, TotalNum);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002350 }
2351}
2352
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002353void ASTReader::InitializeContext(ASTContext &Ctx) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002354 Context = &Ctx;
2355 assert(Context && "Passed null context!");
2356
2357 assert(PP && "Forgot to set Preprocessor ?");
2358 PP->getIdentifierTable().setExternalIdentifierLookup(this);
2359 PP->getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregor88a35862010-01-04 19:18:44 +00002360 PP->setExternalSource(this);
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002361
Douglas Gregor3747ee72010-10-01 01:18:02 +00002362 // If we have an update block for the TU waiting, we have to add it before
2363 // deserializing the decl.
2364 DeclContextOffsetsMap::iterator DCU = DeclContextOffsets.find(0);
2365 if (DCU != DeclContextOffsets.end()) {
2366 // Insertion could invalidate map, so grab vector.
2367 DeclContextInfos T;
2368 T.swap(DCU->second);
2369 DeclContextOffsets.erase(DCU);
2370 DeclContextOffsets[Ctx.getTranslationUnitDecl()].swap(T);
2371 }
2372
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002373 // Load the translation unit declaration
Argyrios Kyrtzidis8871a442010-07-08 17:13:02 +00002374 GetTranslationUnitDecl();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002375
2376 // Load the special types.
2377 Context->setBuiltinVaListType(
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002378 GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST]));
2379 if (unsigned Id = SpecialTypes[SPECIAL_TYPE_OBJC_ID])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002380 Context->setObjCIdType(GetType(Id));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002381 if (unsigned Sel = SpecialTypes[SPECIAL_TYPE_OBJC_SELECTOR])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002382 Context->setObjCSelType(GetType(Sel));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002383 if (unsigned Proto = SpecialTypes[SPECIAL_TYPE_OBJC_PROTOCOL])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002384 Context->setObjCProtoType(GetType(Proto));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002385 if (unsigned Class = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002386 Context->setObjCClassType(GetType(Class));
Steve Naroff14108da2009-07-10 23:34:53 +00002387
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002388 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002389 Context->setCFConstantStringType(GetType(String));
Mike Stump1eb44332009-09-09 15:08:12 +00002390 if (unsigned FastEnum
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002391 = SpecialTypes[SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002392 Context->setObjCFastEnumerationStateType(GetType(FastEnum));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002393 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002394 QualType FileType = GetType(File);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002395 if (FileType.isNull()) {
2396 Error("FILE type is NULL");
2397 return;
2398 }
John McCall183700f2009-09-21 23:43:11 +00002399 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002400 Context->setFILEDecl(Typedef->getDecl());
2401 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002402 const TagType *Tag = FileType->getAs<TagType>();
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002403 if (!Tag) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002404 Error("Invalid FILE type in AST file");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002405 return;
2406 }
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002407 Context->setFILEDecl(Tag->getDecl());
2408 }
2409 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002410 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_jmp_buf]) {
Mike Stump782fa302009-07-28 02:25:19 +00002411 QualType Jmp_bufType = GetType(Jmp_buf);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002412 if (Jmp_bufType.isNull()) {
2413 Error("jmp_bug type is NULL");
2414 return;
2415 }
John McCall183700f2009-09-21 23:43:11 +00002416 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
Mike Stump782fa302009-07-28 02:25:19 +00002417 Context->setjmp_bufDecl(Typedef->getDecl());
2418 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002419 const TagType *Tag = Jmp_bufType->getAs<TagType>();
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002420 if (!Tag) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002421 Error("Invalid jmp_buf type in AST file");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002422 return;
2423 }
Mike Stump782fa302009-07-28 02:25:19 +00002424 Context->setjmp_bufDecl(Tag->getDecl());
2425 }
2426 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002427 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_sigjmp_buf]) {
Mike Stump782fa302009-07-28 02:25:19 +00002428 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002429 if (Sigjmp_bufType.isNull()) {
2430 Error("sigjmp_buf type is NULL");
2431 return;
2432 }
John McCall183700f2009-09-21 23:43:11 +00002433 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
Mike Stump782fa302009-07-28 02:25:19 +00002434 Context->setsigjmp_bufDecl(Typedef->getDecl());
2435 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002436 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002437 assert(Tag && "Invalid sigjmp_buf type in AST file");
Mike Stump782fa302009-07-28 02:25:19 +00002438 Context->setsigjmp_bufDecl(Tag->getDecl());
2439 }
2440 }
Mike Stump1eb44332009-09-09 15:08:12 +00002441 if (unsigned ObjCIdRedef
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002442 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION])
Douglas Gregord1571ac2009-08-21 00:27:50 +00002443 Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef);
Mike Stump1eb44332009-09-09 15:08:12 +00002444 if (unsigned ObjCClassRedef
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002445 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION])
Douglas Gregord1571ac2009-08-21 00:27:50 +00002446 Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002447 if (unsigned String = SpecialTypes[SPECIAL_TYPE_BLOCK_DESCRIPTOR])
Mike Stumpadaaad32009-10-20 02:12:22 +00002448 Context->setBlockDescriptorType(GetType(String));
Mike Stump083c25e2009-10-22 00:49:09 +00002449 if (unsigned String
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002450 = SpecialTypes[SPECIAL_TYPE_BLOCK_EXTENDED_DESCRIPTOR])
Mike Stump083c25e2009-10-22 00:49:09 +00002451 Context->setBlockDescriptorExtendedType(GetType(String));
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002452 if (unsigned ObjCSelRedef
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002453 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION])
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002454 Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002455 if (unsigned String = SpecialTypes[SPECIAL_TYPE_NS_CONSTANT_STRING])
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002456 Context->setNSConstantStringType(GetType(String));
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +00002457
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002458 if (SpecialTypes[SPECIAL_TYPE_INT128_INSTALLED])
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +00002459 Context->setInt128Installed();
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002460
2461 ReadUserDiagnosticMappings(Context->getDiagnostics());
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002462}
2463
Douglas Gregorb64c1932009-05-12 01:31:05 +00002464/// \brief Retrieve the name of the original source file name
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002465/// directly from the AST file, without actually loading the AST
Douglas Gregorb64c1932009-05-12 01:31:05 +00002466/// file.
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002467std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +00002468 FileManager &FileMgr,
Daniel Dunbar93ebb1b2009-12-03 09:13:06 +00002469 Diagnostic &Diags) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002470 // Open the AST file.
Douglas Gregorb64c1932009-05-12 01:31:05 +00002471 std::string ErrStr;
2472 llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
Chris Lattner39b49bc2010-11-23 08:35:12 +00002473 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
Douglas Gregorb64c1932009-05-12 01:31:05 +00002474 if (!Buffer) {
Daniel Dunbar93ebb1b2009-12-03 09:13:06 +00002475 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002476 return std::string();
2477 }
2478
2479 // Initialize the stream
2480 llvm::BitstreamReader StreamFile;
2481 llvm::BitstreamCursor Stream;
Mike Stump1eb44332009-09-09 15:08:12 +00002482 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregorb64c1932009-05-12 01:31:05 +00002483 (const unsigned char *)Buffer->getBufferEnd());
2484 Stream.init(StreamFile);
2485
2486 // Sniff for the signature.
2487 if (Stream.Read(8) != 'C' ||
2488 Stream.Read(8) != 'P' ||
2489 Stream.Read(8) != 'C' ||
2490 Stream.Read(8) != 'H') {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002491 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002492 return std::string();
2493 }
2494
2495 RecordData Record;
2496 while (!Stream.AtEndOfStream()) {
2497 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00002498
Douglas Gregorb64c1932009-05-12 01:31:05 +00002499 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2500 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump1eb44332009-09-09 15:08:12 +00002501
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002502 // We only know the AST subblock ID.
Douglas Gregorb64c1932009-05-12 01:31:05 +00002503 switch (BlockID) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002504 case AST_BLOCK_ID:
2505 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002506 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002507 return std::string();
2508 }
2509 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002510
Douglas Gregorb64c1932009-05-12 01:31:05 +00002511 default:
2512 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002513 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002514 return std::string();
2515 }
2516 break;
2517 }
2518 continue;
2519 }
2520
2521 if (Code == llvm::bitc::END_BLOCK) {
2522 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002523 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002524 return std::string();
2525 }
2526 continue;
2527 }
2528
2529 if (Code == llvm::bitc::DEFINE_ABBREV) {
2530 Stream.ReadAbbrevRecord();
2531 continue;
2532 }
2533
2534 Record.clear();
2535 const char *BlobStart = 0;
2536 unsigned BlobLen = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002537 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002538 == ORIGINAL_FILE_NAME)
Douglas Gregorb64c1932009-05-12 01:31:05 +00002539 return std::string(BlobStart, BlobLen);
Mike Stump1eb44332009-09-09 15:08:12 +00002540 }
Douglas Gregorb64c1932009-05-12 01:31:05 +00002541
2542 return std::string();
2543}
2544
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002545/// \brief Parse the record that corresponds to a LangOptions data
2546/// structure.
2547///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002548/// This routine parses the language options from the AST file and then gives
2549/// them to the AST listener if one is set.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002550///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002551/// \returns true if the listener deems the file unacceptable, false otherwise.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002552bool ASTReader::ParseLanguageOptions(
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002553 const llvm::SmallVectorImpl<uint64_t> &Record) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002554 if (Listener) {
2555 LangOptions LangOpts;
Mike Stump1eb44332009-09-09 15:08:12 +00002556
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002557 #define PARSE_LANGOPT(Option) \
2558 LangOpts.Option = Record[Idx]; \
2559 ++Idx
Mike Stump1eb44332009-09-09 15:08:12 +00002560
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002561 unsigned Idx = 0;
2562 PARSE_LANGOPT(Trigraphs);
2563 PARSE_LANGOPT(BCPLComment);
2564 PARSE_LANGOPT(DollarIdents);
2565 PARSE_LANGOPT(AsmPreprocessor);
2566 PARSE_LANGOPT(GNUMode);
Chandler Carrutheb5d7b72010-04-17 20:17:31 +00002567 PARSE_LANGOPT(GNUKeywords);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002568 PARSE_LANGOPT(ImplicitInt);
2569 PARSE_LANGOPT(Digraphs);
2570 PARSE_LANGOPT(HexFloats);
2571 PARSE_LANGOPT(C99);
2572 PARSE_LANGOPT(Microsoft);
2573 PARSE_LANGOPT(CPlusPlus);
2574 PARSE_LANGOPT(CPlusPlus0x);
2575 PARSE_LANGOPT(CXXOperatorNames);
2576 PARSE_LANGOPT(ObjC1);
2577 PARSE_LANGOPT(ObjC2);
2578 PARSE_LANGOPT(ObjCNonFragileABI);
Fariborz Jahanian412e7982010-02-09 19:31:38 +00002579 PARSE_LANGOPT(ObjCNonFragileABI2);
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +00002580 PARSE_LANGOPT(NoConstantCFStrings);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002581 PARSE_LANGOPT(PascalStrings);
2582 PARSE_LANGOPT(WritableStrings);
2583 PARSE_LANGOPT(LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +00002584 PARSE_LANGOPT(AltiVec);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002585 PARSE_LANGOPT(Exceptions);
Daniel Dunbar73482882010-02-10 18:48:44 +00002586 PARSE_LANGOPT(SjLjExceptions);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002587 PARSE_LANGOPT(NeXTRuntime);
2588 PARSE_LANGOPT(Freestanding);
2589 PARSE_LANGOPT(NoBuiltin);
2590 PARSE_LANGOPT(ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +00002591 PARSE_LANGOPT(POSIXThreads);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002592 PARSE_LANGOPT(Blocks);
2593 PARSE_LANGOPT(EmitAllDecls);
2594 PARSE_LANGOPT(MathErrno);
Chris Lattnera4d71452010-06-26 21:25:03 +00002595 LangOpts.setSignedOverflowBehavior((LangOptions::SignedOverflowBehaviorTy)
2596 Record[Idx++]);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002597 PARSE_LANGOPT(HeinousExtensions);
2598 PARSE_LANGOPT(Optimize);
2599 PARSE_LANGOPT(OptimizeSize);
2600 PARSE_LANGOPT(Static);
2601 PARSE_LANGOPT(PICLevel);
2602 PARSE_LANGOPT(GNUInline);
2603 PARSE_LANGOPT(NoInline);
2604 PARSE_LANGOPT(AccessControl);
2605 PARSE_LANGOPT(CharIsSigned);
John Thompsona6fda122009-11-05 20:14:16 +00002606 PARSE_LANGOPT(ShortWChar);
Chris Lattnera4d71452010-06-26 21:25:03 +00002607 LangOpts.setGCMode((LangOptions::GCMode)Record[Idx++]);
John McCall1fb0caa2010-10-22 21:05:15 +00002608 LangOpts.setVisibilityMode((Visibility)Record[Idx++]);
Daniel Dunbarab8e2812009-09-21 04:16:19 +00002609 LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode)
Chris Lattnera4d71452010-06-26 21:25:03 +00002610 Record[Idx++]);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002611 PARSE_LANGOPT(InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +00002612 PARSE_LANGOPT(OpenCL);
Mike Stump9c276ae2009-12-12 01:27:46 +00002613 PARSE_LANGOPT(CatchUndefined);
2614 // FIXME: Missing ElideConstructors?!
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002615 #undef PARSE_LANGOPT
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002616
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002617 return Listener->ReadLanguageOptions(LangOpts);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002618 }
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002619
2620 return false;
2621}
2622
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002623void ASTReader::ReadPreprocessedEntities() {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002624 ReadDefinedMacros();
2625}
2626
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002627void ASTReader::ReadUserDiagnosticMappings(Diagnostic &Diag) {
2628 unsigned Idx = 0;
2629 while (Idx < UserDiagMappings.size()) {
2630 unsigned DiagID = UserDiagMappings[Idx++];
2631 unsigned Map = UserDiagMappings[Idx++];
2632 Diag.setDiagnosticMappingInternal(DiagID, Map, /*isUser=*/true);
2633 }
2634}
2635
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002636/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002637ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002638 PerFileData *F = 0;
2639 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2640 F = Chain[N - I - 1];
2641 if (Index < F->LocalNumTypes)
2642 break;
2643 Index -= F->LocalNumTypes;
2644 }
2645 assert(F && F->LocalNumTypes > Index && "Broken chain");
Sebastian Redlc3632732010-10-05 15:59:54 +00002646 return RecordLocation(F, F->TypeOffsets[Index]);
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002647}
2648
2649/// \brief Read and return the type with the given index..
Douglas Gregor2cf26342009-04-09 22:27:44 +00002650///
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002651/// The index is the type ID, shifted and minus the number of predefs. This
2652/// routine actually reads the record corresponding to the type at the given
2653/// location. It is a helper routine for GetType, which deals with reading type
2654/// IDs.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002655QualType ASTReader::ReadTypeRecord(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002656 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00002657 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00002658
Douglas Gregor0b748912009-04-14 21:18:50 +00002659 // Keep track of where we are in the stream, then jump back there
2660 // after reading this type.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002661 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregor0b748912009-04-14 21:18:50 +00002662
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00002663 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redl27372b42010-08-11 18:52:41 +00002664
Douglas Gregord89275b2009-07-06 18:54:52 +00002665 // Note that we are loading a type record.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00002666 Deserializing AType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00002667
Sebastian Redlc3632732010-10-05 15:59:54 +00002668 DeclsCursor.JumpToBit(Loc.Offset);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002669 RecordData Record;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002670 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002671 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
2672 case TYPE_EXT_QUAL: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002673 if (Record.size() != 2) {
2674 Error("Incorrect encoding of extended qualifier type");
2675 return QualType();
2676 }
Douglas Gregor6d473962009-04-15 22:00:08 +00002677 QualType Base = GetType(Record[0]);
John McCall0953e762009-09-24 19:53:00 +00002678 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[1]);
2679 return Context->getQualifiedType(Base, Quals);
Douglas Gregor6d473962009-04-15 22:00:08 +00002680 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002681
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002682 case TYPE_COMPLEX: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002683 if (Record.size() != 1) {
2684 Error("Incorrect encoding of complex type");
2685 return QualType();
2686 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002687 QualType ElemType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002688 return Context->getComplexType(ElemType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002689 }
2690
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002691 case TYPE_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002692 if (Record.size() != 1) {
2693 Error("Incorrect encoding of pointer type");
2694 return QualType();
2695 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002696 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002697 return Context->getPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002698 }
2699
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002700 case TYPE_BLOCK_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002701 if (Record.size() != 1) {
2702 Error("Incorrect encoding of block pointer type");
2703 return QualType();
2704 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002705 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002706 return Context->getBlockPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002707 }
2708
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002709 case TYPE_LVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002710 if (Record.size() != 1) {
2711 Error("Incorrect encoding of lvalue reference type");
2712 return QualType();
2713 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002714 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002715 return Context->getLValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002716 }
2717
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002718 case TYPE_RVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002719 if (Record.size() != 1) {
2720 Error("Incorrect encoding of rvalue reference type");
2721 return QualType();
2722 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002723 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002724 return Context->getRValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002725 }
2726
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002727 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidis240437b2010-07-02 11:55:15 +00002728 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002729 Error("Incorrect encoding of member pointer type");
2730 return QualType();
2731 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002732 QualType PointeeType = GetType(Record[0]);
2733 QualType ClassType = GetType(Record[1]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002734 return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002735 }
2736
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002737 case TYPE_CONSTANT_ARRAY: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002738 QualType ElementType = GetType(Record[0]);
2739 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2740 unsigned IndexTypeQuals = Record[2];
2741 unsigned Idx = 3;
2742 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002743 return Context->getConstantArrayType(ElementType, Size,
2744 ASM, IndexTypeQuals);
2745 }
2746
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002747 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002748 QualType ElementType = GetType(Record[0]);
2749 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2750 unsigned IndexTypeQuals = Record[2];
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002751 return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002752 }
2753
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002754 case TYPE_VARIABLE_ARRAY: {
Douglas Gregor0b748912009-04-14 21:18:50 +00002755 QualType ElementType = GetType(Record[0]);
2756 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2757 unsigned IndexTypeQuals = Record[2];
Sebastian Redlc3632732010-10-05 15:59:54 +00002758 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
2759 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
2760 return Context->getVariableArrayType(ElementType, ReadExpr(*Loc.F),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002761 ASM, IndexTypeQuals,
2762 SourceRange(LBLoc, RBLoc));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002763 }
2764
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002765 case TYPE_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00002766 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002767 Error("incorrect encoding of vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002768 return QualType();
2769 }
2770
2771 QualType ElementType = GetType(Record[0]);
2772 unsigned NumElements = Record[1];
Bob Wilsone86d78c2010-11-10 21:56:12 +00002773 unsigned VecKind = Record[2];
Chris Lattner788b0fd2010-06-23 06:00:24 +00002774 return Context->getVectorType(ElementType, NumElements,
Bob Wilsone86d78c2010-11-10 21:56:12 +00002775 (VectorType::VectorKind)VecKind);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002776 }
2777
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002778 case TYPE_EXT_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00002779 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002780 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002781 return QualType();
2782 }
2783
2784 QualType ElementType = GetType(Record[0]);
2785 unsigned NumElements = Record[1];
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002786 return Context->getExtVectorType(ElementType, NumElements);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002787 }
2788
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002789 case TYPE_FUNCTION_NO_PROTO: {
Rafael Espindola425ef722010-03-30 22:15:11 +00002790 if (Record.size() != 4) {
Douglas Gregora02b1472009-04-28 21:53:25 +00002791 Error("incorrect encoding of no-proto function type");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002792 return QualType();
2793 }
2794 QualType ResultType = GetType(Record[0]);
Rafael Espindola425ef722010-03-30 22:15:11 +00002795 FunctionType::ExtInfo Info(Record[1], Record[2], (CallingConv)Record[3]);
Rafael Espindola264ba482010-03-30 20:24:48 +00002796 return Context->getFunctionNoProtoType(ResultType, Info);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002797 }
2798
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002799 case TYPE_FUNCTION_PROTO: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002800 QualType ResultType = GetType(Record[0]);
Douglas Gregor91236662009-12-22 18:11:50 +00002801 bool NoReturn = Record[1];
Rafael Espindola425ef722010-03-30 22:15:11 +00002802 unsigned RegParm = Record[2];
2803 CallingConv CallConv = (CallingConv)Record[3];
2804 unsigned Idx = 4;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002805 unsigned NumParams = Record[Idx++];
2806 llvm::SmallVector<QualType, 16> ParamTypes;
2807 for (unsigned I = 0; I != NumParams; ++I)
2808 ParamTypes.push_back(GetType(Record[Idx++]));
2809 bool isVariadic = Record[Idx++];
2810 unsigned Quals = Record[Idx++];
Sebastian Redl465226e2009-05-27 22:11:52 +00002811 bool hasExceptionSpec = Record[Idx++];
2812 bool hasAnyExceptionSpec = Record[Idx++];
2813 unsigned NumExceptions = Record[Idx++];
2814 llvm::SmallVector<QualType, 2> Exceptions;
2815 for (unsigned I = 0; I != NumExceptions; ++I)
2816 Exceptions.push_back(GetType(Record[Idx++]));
Jay Foadbeaaccd2009-05-21 09:52:38 +00002817 return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams,
Sebastian Redl465226e2009-05-27 22:11:52 +00002818 isVariadic, Quals, hasExceptionSpec,
2819 hasAnyExceptionSpec, NumExceptions,
Rafael Espindola264ba482010-03-30 20:24:48 +00002820 Exceptions.data(),
Rafael Espindola425ef722010-03-30 22:15:11 +00002821 FunctionType::ExtInfo(NoReturn, RegParm,
2822 CallConv));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002823 }
2824
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002825 case TYPE_UNRESOLVED_USING:
John McCalled976492009-12-04 22:46:56 +00002826 return Context->getTypeDeclType(
2827 cast<UnresolvedUsingTypenameDecl>(GetDecl(Record[0])));
2828
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002829 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002830 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002831 Error("incorrect encoding of typedef type");
2832 return QualType();
2833 }
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002834 TypedefDecl *Decl = cast<TypedefDecl>(GetDecl(Record[0]));
2835 QualType Canonical = GetType(Record[1]);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00002836 if (!Canonical.isNull())
2837 Canonical = Context->getCanonicalType(Canonical);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002838 return Context->getTypedefType(Decl, Canonical);
2839 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002840
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002841 case TYPE_TYPEOF_EXPR:
Sebastian Redlc3632732010-10-05 15:59:54 +00002842 return Context->getTypeOfExprType(ReadExpr(*Loc.F));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002843
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002844 case TYPE_TYPEOF: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002845 if (Record.size() != 1) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002846 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002847 return QualType();
2848 }
2849 QualType UnderlyingType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002850 return Context->getTypeOfType(UnderlyingType);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002851 }
Mike Stump1eb44332009-09-09 15:08:12 +00002852
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002853 case TYPE_DECLTYPE:
Sebastian Redlc3632732010-10-05 15:59:54 +00002854 return Context->getDecltypeType(ReadExpr(*Loc.F));
Anders Carlsson395b4752009-06-24 19:06:50 +00002855
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002856 case TYPE_RECORD: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002857 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002858 Error("incorrect encoding of record type");
2859 return QualType();
2860 }
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002861 bool IsDependent = Record[0];
2862 QualType T = Context->getRecordType(cast<RecordDecl>(GetDecl(Record[1])));
John McCallb870b882010-10-14 21:48:26 +00002863 T->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002864 return T;
2865 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002866
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002867 case TYPE_ENUM: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002868 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002869 Error("incorrect encoding of enum type");
2870 return QualType();
2871 }
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002872 bool IsDependent = Record[0];
2873 QualType T = Context->getEnumType(cast<EnumDecl>(GetDecl(Record[1])));
John McCallb870b882010-10-14 21:48:26 +00002874 T->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002875 return T;
2876 }
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002877
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002878 case TYPE_ELABORATED: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00002879 unsigned Idx = 0;
2880 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2881 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2882 QualType NamedType = GetType(Record[Idx++]);
2883 return Context->getElaboratedType(Keyword, NNS, NamedType);
John McCall7da24312009-09-05 00:15:47 +00002884 }
2885
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002886 case TYPE_OBJC_INTERFACE: {
Chris Lattnerc6fa4452009-04-22 06:45:28 +00002887 unsigned Idx = 0;
2888 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
John McCallc12c5bb2010-05-15 11:32:37 +00002889 return Context->getObjCInterfaceType(ItfD);
2890 }
2891
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002892 case TYPE_OBJC_OBJECT: {
John McCallc12c5bb2010-05-15 11:32:37 +00002893 unsigned Idx = 0;
2894 QualType Base = GetType(Record[Idx++]);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00002895 unsigned NumProtos = Record[Idx++];
2896 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2897 for (unsigned I = 0; I != NumProtos; ++I)
2898 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
Michael J. Spencer20249a12010-10-21 03:16:25 +00002899 return Context->getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00002900 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002901
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002902 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00002903 unsigned Idx = 0;
John McCallc12c5bb2010-05-15 11:32:37 +00002904 QualType Pointee = GetType(Record[Idx++]);
2905 return Context->getObjCObjectPointerType(Pointee);
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00002906 }
Argyrios Kyrtzidis24fab412009-09-29 19:42:55 +00002907
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002908 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCall49a832b2009-10-18 09:09:24 +00002909 unsigned Idx = 0;
2910 QualType Parm = GetType(Record[Idx++]);
2911 QualType Replacement = GetType(Record[Idx++]);
2912 return
2913 Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
2914 Replacement);
2915 }
John McCall3cb0ebd2010-03-10 03:28:59 +00002916
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002917 case TYPE_INJECTED_CLASS_NAME: {
John McCall3cb0ebd2010-03-10 03:28:59 +00002918 CXXRecordDecl *D = cast<CXXRecordDecl>(GetDecl(Record[0]));
2919 QualType TST = GetType(Record[1]); // probably derivable
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00002920 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002921 // for AST reading, too much interdependencies.
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00002922 return
2923 QualType(new (*Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCall3cb0ebd2010-03-10 03:28:59 +00002924 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00002925
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002926 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00002927 unsigned Idx = 0;
2928 unsigned Depth = Record[Idx++];
2929 unsigned Index = Record[Idx++];
2930 bool Pack = Record[Idx++];
2931 IdentifierInfo *Name = GetIdentifierInfo(Record, Idx);
2932 return Context->getTemplateTypeParmType(Depth, Index, Pack, Name);
2933 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00002934
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002935 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00002936 unsigned Idx = 0;
2937 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2938 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2939 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +00002940 QualType Canon = GetType(Record[Idx++]);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00002941 if (!Canon.isNull())
2942 Canon = Context->getCanonicalType(Canon);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +00002943 return Context->getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00002944 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00002945
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002946 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00002947 unsigned Idx = 0;
2948 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2949 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2950 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
2951 unsigned NumArgs = Record[Idx++];
2952 llvm::SmallVector<TemplateArgument, 8> Args;
2953 Args.reserve(NumArgs);
2954 while (NumArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00002955 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00002956 return Context->getDependentTemplateSpecializationType(Keyword, NNS, Name,
2957 Args.size(), Args.data());
2958 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00002959
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002960 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00002961 unsigned Idx = 0;
2962
2963 // ArrayType
2964 QualType ElementType = GetType(Record[Idx++]);
2965 ArrayType::ArraySizeModifier ASM
2966 = (ArrayType::ArraySizeModifier)Record[Idx++];
2967 unsigned IndexTypeQuals = Record[Idx++];
2968
2969 // DependentSizedArrayType
Sebastian Redlc3632732010-10-05 15:59:54 +00002970 Expr *NumElts = ReadExpr(*Loc.F);
2971 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00002972
2973 return Context->getDependentSizedArrayType(ElementType, NumElts, ASM,
2974 IndexTypeQuals, Brackets);
2975 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00002976
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002977 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00002978 unsigned Idx = 0;
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002979 bool IsDependent = Record[Idx++];
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00002980 TemplateName Name = ReadTemplateName(Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00002981 llvm::SmallVector<TemplateArgument, 8> Args;
Sebastian Redlc3632732010-10-05 15:59:54 +00002982 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00002983 QualType Canon = GetType(Record[Idx++]);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002984 QualType T;
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002985 if (Canon.isNull())
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002986 T = Context->getCanonicalTemplateSpecializationType(Name, Args.data(),
2987 Args.size());
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002988 else
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002989 T = Context->getTemplateSpecializationType(Name, Args.data(),
2990 Args.size(), Canon);
John McCallb870b882010-10-14 21:48:26 +00002991 T->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002992 return T;
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00002993 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002994 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002995 // Suppress a GCC warning
2996 return QualType();
2997}
2998
Sebastian Redlc3632732010-10-05 15:59:54 +00002999class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003000 ASTReader &Reader;
Sebastian Redlc3632732010-10-05 15:59:54 +00003001 ASTReader::PerFileData &F;
Sebastian Redl577d4792010-07-22 22:43:28 +00003002 llvm::BitstreamCursor &DeclsCursor;
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003003 const ASTReader::RecordData &Record;
John McCalla1ee0c52009-10-16 21:56:05 +00003004 unsigned &Idx;
3005
Sebastian Redlc3632732010-10-05 15:59:54 +00003006 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
3007 unsigned &I) {
3008 return Reader.ReadSourceLocation(F, R, I);
3009 }
3010
John McCalla1ee0c52009-10-16 21:56:05 +00003011public:
Sebastian Redlc3632732010-10-05 15:59:54 +00003012 TypeLocReader(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003013 const ASTReader::RecordData &Record, unsigned &Idx)
Sebastian Redlc3632732010-10-05 15:59:54 +00003014 : Reader(Reader), F(F), DeclsCursor(F.DeclsCursor), Record(Record), Idx(Idx)
3015 { }
John McCalla1ee0c52009-10-16 21:56:05 +00003016
John McCall51bd8032009-10-18 01:05:36 +00003017 // We want compile-time assurance that we've enumerated all of
3018 // these, so unfortunately we have to declare them first, then
3019 // define them out-of-line.
3020#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +00003021#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +00003022 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00003023#include "clang/AST/TypeLocNodes.def"
3024
John McCall51bd8032009-10-18 01:05:36 +00003025 void VisitFunctionTypeLoc(FunctionTypeLoc);
3026 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00003027};
3028
John McCall51bd8032009-10-18 01:05:36 +00003029void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCalla1ee0c52009-10-16 21:56:05 +00003030 // nothing to do
3031}
John McCall51bd8032009-10-18 01:05:36 +00003032void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003033 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
Douglas Gregorddf889a2010-01-18 18:04:31 +00003034 if (TL.needsExtraLocalData()) {
3035 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
3036 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
3037 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
3038 TL.setModeAttr(Record[Idx++]);
3039 }
John McCalla1ee0c52009-10-16 21:56:05 +00003040}
John McCall51bd8032009-10-18 01:05:36 +00003041void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003042 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003043}
John McCall51bd8032009-10-18 01:05:36 +00003044void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003045 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003046}
John McCall51bd8032009-10-18 01:05:36 +00003047void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003048 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003049}
John McCall51bd8032009-10-18 01:05:36 +00003050void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003051 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003052}
John McCall51bd8032009-10-18 01:05:36 +00003053void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003054 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003055}
John McCall51bd8032009-10-18 01:05:36 +00003056void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003057 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003058}
John McCall51bd8032009-10-18 01:05:36 +00003059void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003060 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
3061 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003062 if (Record[Idx++])
Sebastian Redlc3632732010-10-05 15:59:54 +00003063 TL.setSizeExpr(Reader.ReadExpr(F));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003064 else
John McCall51bd8032009-10-18 01:05:36 +00003065 TL.setSizeExpr(0);
3066}
3067void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
3068 VisitArrayTypeLoc(TL);
3069}
3070void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
3071 VisitArrayTypeLoc(TL);
3072}
3073void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
3074 VisitArrayTypeLoc(TL);
3075}
3076void TypeLocReader::VisitDependentSizedArrayTypeLoc(
3077 DependentSizedArrayTypeLoc TL) {
3078 VisitArrayTypeLoc(TL);
3079}
3080void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
3081 DependentSizedExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003082 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003083}
3084void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003085 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003086}
3087void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003088 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003089}
3090void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003091 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3092 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
Douglas Gregordab60ad2010-10-01 18:44:50 +00003093 TL.setTrailingReturn(Record[Idx++]);
John McCall51bd8032009-10-18 01:05:36 +00003094 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
John McCall86acc2a2009-10-23 01:28:53 +00003095 TL.setArg(i, cast_or_null<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
John McCall51bd8032009-10-18 01:05:36 +00003096 }
3097}
3098void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
3099 VisitFunctionTypeLoc(TL);
3100}
3101void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
3102 VisitFunctionTypeLoc(TL);
3103}
John McCalled976492009-12-04 22:46:56 +00003104void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003105 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalled976492009-12-04 22:46:56 +00003106}
John McCall51bd8032009-10-18 01:05:36 +00003107void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003108 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003109}
3110void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003111 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3112 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3113 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003114}
3115void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003116 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3117 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3118 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3119 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003120}
3121void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003122 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003123}
3124void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003125 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003126}
3127void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003128 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003129}
John McCall51bd8032009-10-18 01:05:36 +00003130void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003131 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003132}
John McCall49a832b2009-10-18 09:09:24 +00003133void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
3134 SubstTemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003135 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall49a832b2009-10-18 09:09:24 +00003136}
John McCall51bd8032009-10-18 01:05:36 +00003137void TypeLocReader::VisitTemplateSpecializationTypeLoc(
3138 TemplateSpecializationTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003139 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
3140 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3141 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall833ca992009-10-29 08:12:44 +00003142 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
3143 TL.setArgLocInfo(i,
Sebastian Redlc3632732010-10-05 15:59:54 +00003144 Reader.GetTemplateArgumentLocInfo(F,
3145 TL.getTypePtr()->getArg(i).getKind(),
3146 Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003147}
Abramo Bagnara465d41b2010-05-11 21:36:43 +00003148void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003149 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3150 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003151}
John McCall3cb0ebd2010-03-10 03:28:59 +00003152void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003153 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall3cb0ebd2010-03-10 03:28:59 +00003154}
Douglas Gregor4714c122010-03-31 17:34:00 +00003155void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003156 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3157 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
3158 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003159}
John McCall33500952010-06-11 00:33:02 +00003160void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
3161 DependentTemplateSpecializationTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003162 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3163 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
3164 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3165 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3166 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall33500952010-06-11 00:33:02 +00003167 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
3168 TL.setArgLocInfo(I,
Sebastian Redlc3632732010-10-05 15:59:54 +00003169 Reader.GetTemplateArgumentLocInfo(F,
3170 TL.getTypePtr()->getArg(I).getKind(),
3171 Record, Idx));
John McCall33500952010-06-11 00:33:02 +00003172}
John McCall51bd8032009-10-18 01:05:36 +00003173void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003174 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallc12c5bb2010-05-15 11:32:37 +00003175}
3176void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
3177 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00003178 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3179 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003180 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Sebastian Redlc3632732010-10-05 15:59:54 +00003181 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003182}
John McCall54e14c42009-10-22 22:37:11 +00003183void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003184 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall54e14c42009-10-22 22:37:11 +00003185}
John McCalla1ee0c52009-10-16 21:56:05 +00003186
Sebastian Redlc3632732010-10-05 15:59:54 +00003187TypeSourceInfo *ASTReader::GetTypeSourceInfo(PerFileData &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00003188 const RecordData &Record,
John McCalla1ee0c52009-10-16 21:56:05 +00003189 unsigned &Idx) {
3190 QualType InfoTy = GetType(Record[Idx++]);
3191 if (InfoTy.isNull())
3192 return 0;
3193
John McCalla93c9342009-12-07 02:54:59 +00003194 TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy);
Sebastian Redlc3632732010-10-05 15:59:54 +00003195 TypeLocReader TLR(*this, F, Record, Idx);
John McCalla93c9342009-12-07 02:54:59 +00003196 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCalla1ee0c52009-10-16 21:56:05 +00003197 TLR.Visit(TL);
John McCalla93c9342009-12-07 02:54:59 +00003198 return TInfo;
John McCalla1ee0c52009-10-16 21:56:05 +00003199}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003200
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003201QualType ASTReader::GetType(TypeID ID) {
John McCall0953e762009-09-24 19:53:00 +00003202 unsigned FastQuals = ID & Qualifiers::FastMask;
3203 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003204
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003205 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003206 QualType T;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003207 switch ((PredefinedTypeIDs)Index) {
3208 case PREDEF_TYPE_NULL_ID: return QualType();
3209 case PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break;
3210 case PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003211
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003212 case PREDEF_TYPE_CHAR_U_ID:
3213 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregor2cf26342009-04-09 22:27:44 +00003214 // FIXME: Check that the signedness of CharTy is correct!
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003215 T = Context->CharTy;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003216 break;
3217
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003218 case PREDEF_TYPE_UCHAR_ID: T = Context->UnsignedCharTy; break;
3219 case PREDEF_TYPE_USHORT_ID: T = Context->UnsignedShortTy; break;
3220 case PREDEF_TYPE_UINT_ID: T = Context->UnsignedIntTy; break;
3221 case PREDEF_TYPE_ULONG_ID: T = Context->UnsignedLongTy; break;
3222 case PREDEF_TYPE_ULONGLONG_ID: T = Context->UnsignedLongLongTy; break;
3223 case PREDEF_TYPE_UINT128_ID: T = Context->UnsignedInt128Ty; break;
3224 case PREDEF_TYPE_SCHAR_ID: T = Context->SignedCharTy; break;
3225 case PREDEF_TYPE_WCHAR_ID: T = Context->WCharTy; break;
3226 case PREDEF_TYPE_SHORT_ID: T = Context->ShortTy; break;
3227 case PREDEF_TYPE_INT_ID: T = Context->IntTy; break;
3228 case PREDEF_TYPE_LONG_ID: T = Context->LongTy; break;
3229 case PREDEF_TYPE_LONGLONG_ID: T = Context->LongLongTy; break;
3230 case PREDEF_TYPE_INT128_ID: T = Context->Int128Ty; break;
3231 case PREDEF_TYPE_FLOAT_ID: T = Context->FloatTy; break;
3232 case PREDEF_TYPE_DOUBLE_ID: T = Context->DoubleTy; break;
3233 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy; break;
3234 case PREDEF_TYPE_OVERLOAD_ID: T = Context->OverloadTy; break;
3235 case PREDEF_TYPE_DEPENDENT_ID: T = Context->DependentTy; break;
3236 case PREDEF_TYPE_NULLPTR_ID: T = Context->NullPtrTy; break;
3237 case PREDEF_TYPE_CHAR16_ID: T = Context->Char16Ty; break;
3238 case PREDEF_TYPE_CHAR32_ID: T = Context->Char32Ty; break;
3239 case PREDEF_TYPE_OBJC_ID: T = Context->ObjCBuiltinIdTy; break;
3240 case PREDEF_TYPE_OBJC_CLASS: T = Context->ObjCBuiltinClassTy; break;
3241 case PREDEF_TYPE_OBJC_SEL: T = Context->ObjCBuiltinSelTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003242 }
3243
3244 assert(!T.isNull() && "Unknown predefined type");
John McCall0953e762009-09-24 19:53:00 +00003245 return T.withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003246 }
3247
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003248 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003249 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl07a353c2010-07-14 20:26:45 +00003250 if (TypesLoaded[Index].isNull()) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003251 TypesLoaded[Index] = ReadTypeRecord(Index);
Douglas Gregor97475832010-10-05 18:37:06 +00003252 if (TypesLoaded[Index].isNull())
3253 return QualType();
3254
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003255 TypesLoaded[Index]->setFromAST();
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003256 TypeIdxs[TypesLoaded[Index]] = TypeIdx::fromTypeID(ID);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003257 if (DeserializationListener)
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003258 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1476ed42010-07-16 16:36:56 +00003259 TypesLoaded[Index]);
Sebastian Redl07a353c2010-07-14 20:26:45 +00003260 }
Mike Stump1eb44332009-09-09 15:08:12 +00003261
John McCall0953e762009-09-24 19:53:00 +00003262 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003263}
3264
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003265TypeID ASTReader::GetTypeID(QualType T) const {
3266 return MakeTypeID(T,
3267 std::bind1st(std::mem_fun(&ASTReader::GetTypeIdx), this));
3268}
3269
3270TypeIdx ASTReader::GetTypeIdx(QualType T) const {
3271 if (T.isNull())
3272 return TypeIdx();
3273 assert(!T.getLocalFastQualifiers());
3274
3275 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3276 // GetTypeIdx is mostly used for computing the hash of DeclarationNames and
3277 // comparing keys of ASTDeclContextNameLookupTable.
3278 // If the type didn't come from the AST file use a specially marked index
3279 // so that any hash/key comparison fail since no such index is stored
3280 // in a AST file.
3281 if (I == TypeIdxs.end())
3282 return TypeIdx(-1);
3283 return I->second;
3284}
3285
Douglas Gregor7c789c12010-10-29 22:39:52 +00003286unsigned ASTReader::getTotalNumCXXBaseSpecifiers() const {
3287 unsigned Result = 0;
3288 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
3289 Result += Chain[I]->LocalNumCXXBaseSpecifiers;
3290
3291 return Result;
3292}
3293
John McCall833ca992009-10-29 08:12:44 +00003294TemplateArgumentLocInfo
Sebastian Redlc3632732010-10-05 15:59:54 +00003295ASTReader::GetTemplateArgumentLocInfo(PerFileData &F,
3296 TemplateArgument::ArgKind Kind,
John McCall833ca992009-10-29 08:12:44 +00003297 const RecordData &Record,
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00003298 unsigned &Index) {
John McCall833ca992009-10-29 08:12:44 +00003299 switch (Kind) {
3300 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00003301 return ReadExpr(F);
John McCall833ca992009-10-29 08:12:44 +00003302 case TemplateArgument::Type:
Sebastian Redlc3632732010-10-05 15:59:54 +00003303 return GetTypeSourceInfo(F, Record, Index);
Douglas Gregor788cd062009-11-11 01:00:40 +00003304 case TemplateArgument::Template: {
Sebastian Redlc3632732010-10-05 15:59:54 +00003305 SourceRange QualifierRange = ReadSourceRange(F, Record, Index);
3306 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003307 return TemplateArgumentLocInfo(QualifierRange, TemplateNameLoc);
Douglas Gregor788cd062009-11-11 01:00:40 +00003308 }
John McCall833ca992009-10-29 08:12:44 +00003309 case TemplateArgument::Null:
3310 case TemplateArgument::Integral:
3311 case TemplateArgument::Declaration:
3312 case TemplateArgument::Pack:
3313 return TemplateArgumentLocInfo();
3314 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00003315 llvm_unreachable("unexpected template argument loc");
John McCall833ca992009-10-29 08:12:44 +00003316 return TemplateArgumentLocInfo();
3317}
3318
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003319TemplateArgumentLoc
Sebastian Redlc3632732010-10-05 15:59:54 +00003320ASTReader::ReadTemplateArgumentLoc(PerFileData &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00003321 const RecordData &Record, unsigned &Index) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003322 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003323
3324 if (Arg.getKind() == TemplateArgument::Expression) {
3325 if (Record[Index++]) // bool InfoHasSameExpr.
3326 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
3327 }
Sebastian Redlc3632732010-10-05 15:59:54 +00003328 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00003329 Record, Index));
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003330}
3331
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003332Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall76bd1f32010-06-01 09:23:16 +00003333 return GetDecl(ID);
3334}
3335
Douglas Gregor7c789c12010-10-29 22:39:52 +00003336uint64_t
3337ASTReader::GetCXXBaseSpecifiersOffset(serialization::CXXBaseSpecifiersID ID) {
3338 if (ID == 0)
3339 return 0;
3340
3341 --ID;
3342 uint64_t Offset = 0;
3343 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3344 if (ID < Chain[I]->LocalNumCXXBaseSpecifiers)
3345 return Offset + Chain[I]->CXXBaseSpecifiersOffsets[ID];
3346
3347 ID -= Chain[I]->LocalNumCXXBaseSpecifiers;
3348 Offset += Chain[I]->SizeInBits;
3349 }
3350
3351 assert(false && "CXXBaseSpecifiers not found");
3352 return 0;
3353}
3354
3355CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
3356 // Figure out which AST file contains this offset.
3357 PerFileData *F = 0;
3358 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3359 if (Offset < Chain[I]->SizeInBits) {
3360 F = Chain[I];
3361 break;
3362 }
3363
3364 Offset -= Chain[I]->SizeInBits;
3365 }
3366
3367 if (!F) {
3368 Error("Malformed AST file: C++ base specifiers at impossible offset");
3369 return 0;
3370 }
3371
3372 llvm::BitstreamCursor &Cursor = F->DeclsCursor;
3373 SavedStreamPosition SavedPosition(Cursor);
3374 Cursor.JumpToBit(Offset);
3375 ReadingKindTracker ReadingKind(Read_Decl, *this);
3376 RecordData Record;
3377 unsigned Code = Cursor.ReadCode();
3378 unsigned RecCode = Cursor.ReadRecord(Code, Record);
3379 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
3380 Error("Malformed AST file: missing C++ base specifiers");
3381 return 0;
3382 }
3383
3384 unsigned Idx = 0;
3385 unsigned NumBases = Record[Idx++];
3386 void *Mem = Context->Allocate(sizeof(CXXBaseSpecifier) * NumBases);
3387 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
3388 for (unsigned I = 0; I != NumBases; ++I)
3389 Bases[I] = ReadCXXBaseSpecifier(*F, Record, Idx);
3390 return Bases;
3391}
3392
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003393TranslationUnitDecl *ASTReader::GetTranslationUnitDecl() {
Sebastian Redl30c514c2010-07-14 23:45:08 +00003394 if (!DeclsLoaded[0]) {
Sebastian Redle1dde812010-08-24 00:50:04 +00003395 ReadDeclRecord(0, 1);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003396 if (DeserializationListener)
Sebastian Redl1476ed42010-07-16 16:36:56 +00003397 DeserializationListener->DeclRead(1, DeclsLoaded[0]);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003398 }
Argyrios Kyrtzidis8871a442010-07-08 17:13:02 +00003399
3400 return cast<TranslationUnitDecl>(DeclsLoaded[0]);
3401}
3402
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003403Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003404 if (ID == 0)
3405 return 0;
3406
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003407 if (ID > DeclsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003408 Error("declaration ID out-of-range for AST file");
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003409 return 0;
3410 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003411
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003412 unsigned Index = ID - 1;
Sebastian Redl30c514c2010-07-14 23:45:08 +00003413 if (!DeclsLoaded[Index]) {
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00003414 ReadDeclRecord(Index, ID);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003415 if (DeserializationListener)
3416 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
3417 }
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003418
3419 return DeclsLoaded[Index];
Douglas Gregor2cf26342009-04-09 22:27:44 +00003420}
3421
Chris Lattner887e2b32009-04-27 05:46:25 +00003422/// \brief Resolve the offset of a statement into a statement.
3423///
3424/// This operation will read a new statement from the external
3425/// source each time it is called, and is meant to be used via a
3426/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003427Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00003428 // Switch case IDs are per Decl.
3429 ClearSwitchCaseIDs();
3430
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003431 // Offset here is a global offset across the entire chain.
3432 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3433 PerFileData &F = *Chain[N - I - 1];
3434 if (Offset < F.SizeInBits) {
3435 // Since we know that this statement is part of a decl, make sure to use
3436 // the decl cursor to read it.
3437 F.DeclsCursor.JumpToBit(Offset);
Sebastian Redlc3632732010-10-05 15:59:54 +00003438 return ReadStmtFromStream(F);
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003439 }
3440 Offset -= F.SizeInBits;
3441 }
3442 llvm_unreachable("Broken chain");
Douglas Gregor250fc9c2009-04-18 00:07:54 +00003443}
3444
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003445bool ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00003446 bool (*isKindWeWant)(Decl::Kind),
John McCall76bd1f32010-06-01 09:23:16 +00003447 llvm::SmallVectorImpl<Decl*> &Decls) {
Mike Stump1eb44332009-09-09 15:08:12 +00003448 assert(DC->hasExternalLexicalStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00003449 "DeclContext has no lexical decls in storage");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003450
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003451 // There might be lexical decls in multiple parts of the chain, for the TU
3452 // at least.
Sebastian Redl4a9eb262010-09-28 02:24:44 +00003453 // DeclContextOffsets might reallocate as we load additional decls below,
3454 // so make a copy of the vector.
3455 DeclContextInfos Infos = DeclContextOffsets[DC];
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003456 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3457 I != E; ++I) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003458 // IDs can be 0 if this context doesn't contain declarations.
3459 if (!I->LexicalDecls)
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003460 continue;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003461
3462 // Load all of the declaration IDs
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00003463 for (const KindDeclIDPair *ID = I->LexicalDecls,
3464 *IDE = ID + I->NumLexicalDecls; ID != IDE; ++ID) {
3465 if (isKindWeWant && !isKindWeWant((Decl::Kind)ID->first))
3466 continue;
3467
3468 Decl *D = GetDecl(ID->second);
Sebastian Redl4a9eb262010-09-28 02:24:44 +00003469 assert(D && "Null decl in lexical decls");
3470 Decls.push_back(D);
3471 }
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003472 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003473
Douglas Gregor25123082009-04-22 22:34:57 +00003474 ++NumLexicalDeclContextsRead;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003475 return false;
3476}
3477
John McCall76bd1f32010-06-01 09:23:16 +00003478DeclContext::lookup_result
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003479ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +00003480 DeclarationName Name) {
Mike Stump1eb44332009-09-09 15:08:12 +00003481 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00003482 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003483 if (!Name)
3484 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
3485 DeclContext::lookup_iterator(0));
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003486
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003487 llvm::SmallVector<NamedDecl *, 64> Decls;
Sebastian Redl8b122732010-08-24 00:49:55 +00003488 // There might be visible decls in multiple parts of the chain, for the TU
Sebastian Redl5967d622010-08-24 00:50:16 +00003489 // and namespaces. For any given name, the last available results replace
3490 // all earlier ones. For this reason, we walk in reverse.
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003491 DeclContextInfos &Infos = DeclContextOffsets[DC];
Sebastian Redl5967d622010-08-24 00:50:16 +00003492 for (DeclContextInfos::reverse_iterator I = Infos.rbegin(), E = Infos.rend();
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003493 I != E; ++I) {
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003494 if (!I->NameLookupTableData)
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003495 continue;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003496
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003497 ASTDeclContextNameLookupTable *LookupTable =
3498 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3499 ASTDeclContextNameLookupTable::iterator Pos = LookupTable->find(Name);
3500 if (Pos == LookupTable->end())
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003501 continue;
3502
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003503 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
3504 for (; Data.first != Data.second; ++Data.first)
3505 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
Sebastian Redl5967d622010-08-24 00:50:16 +00003506 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003507 }
3508
Douglas Gregor25123082009-04-22 22:34:57 +00003509 ++NumVisibleDeclContextsRead;
John McCall76bd1f32010-06-01 09:23:16 +00003510
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003511 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall76bd1f32010-06-01 09:23:16 +00003512 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003513}
3514
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +00003515void ASTReader::MaterializeVisibleDecls(const DeclContext *DC) {
3516 assert(DC->hasExternalVisibleStorage() &&
3517 "DeclContext has no visible decls in storage");
3518
3519 llvm::SmallVector<NamedDecl *, 64> Decls;
3520 // There might be visible decls in multiple parts of the chain, for the TU
3521 // and namespaces.
3522 DeclContextInfos &Infos = DeclContextOffsets[DC];
3523 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3524 I != E; ++I) {
3525 if (!I->NameLookupTableData)
3526 continue;
3527
3528 ASTDeclContextNameLookupTable *LookupTable =
3529 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3530 for (ASTDeclContextNameLookupTable::item_iterator
3531 ItemI = LookupTable->item_begin(),
3532 ItemEnd = LookupTable->item_end() ; ItemI != ItemEnd; ++ItemI) {
3533 ASTDeclContextNameLookupTable::item_iterator::value_type Val
3534 = *ItemI;
3535 ASTDeclContextNameLookupTrait::data_type Data = Val.second;
3536 Decls.clear();
3537 for (; Data.first != Data.second; ++Data.first)
3538 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
3539 MaterializeVisibleDeclsForName(DC, Val.first, Decls);
3540 }
3541 }
3542}
3543
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003544void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003545 assert(Consumer);
3546 while (!InterestingDecls.empty()) {
3547 DeclGroupRef DG(InterestingDecls.front());
3548 InterestingDecls.pop_front();
Sebastian Redl27372b42010-08-11 18:52:41 +00003549 Consumer->HandleInterestingDecl(DG);
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003550 }
3551}
3552
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003553void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor0af2ca42009-04-22 19:09:20 +00003554 this->Consumer = Consumer;
3555
Douglas Gregorfdd01722009-04-14 00:24:19 +00003556 if (!Consumer)
3557 return;
3558
3559 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003560 // Force deserialization of this decl, which will cause it to be queued for
3561 // passing to the consumer.
Daniel Dunbar04a0b502009-09-17 03:06:44 +00003562 GetDecl(ExternalDefinitions[I]);
Douglas Gregorfdd01722009-04-14 00:24:19 +00003563 }
Douglas Gregorc62a2fe2009-04-25 00:41:30 +00003564
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003565 PassInterestingDeclsToConsumer();
Douglas Gregorfdd01722009-04-14 00:24:19 +00003566}
3567
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003568void ASTReader::PrintStats() {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003569 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregor2cf26342009-04-09 22:27:44 +00003570
Mike Stump1eb44332009-09-09 15:08:12 +00003571 unsigned NumTypesLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003572 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall0953e762009-09-24 19:53:00 +00003573 QualType());
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003574 unsigned NumDeclsLoaded
3575 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
3576 (Decl *)0);
3577 unsigned NumIdentifiersLoaded
3578 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
3579 IdentifiersLoaded.end(),
3580 (IdentifierInfo *)0);
Mike Stump1eb44332009-09-09 15:08:12 +00003581 unsigned NumSelectorsLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003582 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
3583 SelectorsLoaded.end(),
3584 Selector());
Douglas Gregor2d41cc12009-04-13 20:50:16 +00003585
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003586 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
3587 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00003588 if (TotalNumSLocEntries)
3589 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
3590 NumSLocEntriesRead, TotalNumSLocEntries,
3591 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003592 if (!TypesLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003593 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003594 NumTypesLoaded, (unsigned)TypesLoaded.size(),
3595 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
3596 if (!DeclsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003597 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003598 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
3599 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003600 if (!IdentifiersLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003601 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003602 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
3603 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Sebastian Redl725cd962010-08-04 20:40:17 +00003604 if (!SelectorsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003605 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redl725cd962010-08-04 20:40:17 +00003606 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
3607 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor83941df2009-04-25 17:48:32 +00003608 if (TotalNumStatements)
3609 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
3610 NumStatementsRead, TotalNumStatements,
3611 ((float)NumStatementsRead/TotalNumStatements * 100));
3612 if (TotalNumMacros)
3613 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
3614 NumMacrosRead, TotalNumMacros,
3615 ((float)NumMacrosRead/TotalNumMacros * 100));
3616 if (TotalLexicalDeclContexts)
3617 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
3618 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
3619 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
3620 * 100));
3621 if (TotalVisibleDeclContexts)
3622 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
3623 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
3624 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
3625 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003626 if (TotalNumMethodPoolEntries) {
Douglas Gregor83941df2009-04-25 17:48:32 +00003627 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003628 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
3629 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor83941df2009-04-25 17:48:32 +00003630 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003631 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor83941df2009-04-25 17:48:32 +00003632 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003633 std::fprintf(stderr, "\n");
3634}
3635
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003636void ASTReader::InitializeSema(Sema &S) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00003637 SemaObj = &S;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003638 S.ExternalSource = this;
3639
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00003640 // Makes sure any declarations that were deserialized "too early"
3641 // still get added to the identifier's declaration chains.
Douglas Gregor76dc8892010-09-24 23:29:12 +00003642 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
3643 if (SemaObj->TUScope)
John McCalld226f652010-08-21 09:40:31 +00003644 SemaObj->TUScope->AddDecl(PreloadedDecls[I]);
Douglas Gregor76dc8892010-09-24 23:29:12 +00003645
3646 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregor668c1a42009-04-21 22:25:48 +00003647 }
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00003648 PreloadedDecls.clear();
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003649
3650 // If there were any tentative definitions, deserialize them and add
Sebastian Redle9d12b62010-01-31 22:27:38 +00003651 // them to Sema's list of tentative definitions.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003652 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
3653 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
Sebastian Redle9d12b62010-01-31 22:27:38 +00003654 SemaObj->TentativeDefinitions.push_back(Var);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003655 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00003656
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003657 // If there were any unused file scoped decls, deserialize them and add to
3658 // Sema's list of unused file scoped decls.
3659 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
3660 DeclaratorDecl *D = cast<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
3661 SemaObj->UnusedFileScopedDecls.push_back(D);
Tanya Lattnere6bbc012010-02-12 00:07:30 +00003662 }
Douglas Gregor14c22f22009-04-22 22:18:58 +00003663
3664 // If there were any locally-scoped external declarations,
3665 // deserialize them and add them to Sema's table of locally-scoped
3666 // external declarations.
3667 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
3668 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
3669 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
3670 }
Douglas Gregorb81c1702009-04-27 20:06:05 +00003671
3672 // If there were any ext_vector type declarations, deserialize them
3673 // and add them to Sema's vector of such declarations.
3674 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I)
3675 SemaObj->ExtVectorDecls.push_back(
3676 cast<TypedefDecl>(GetDecl(ExtVectorDecls[I])));
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003677
3678 // FIXME: Do VTable uses and dynamic classes deserialize too much ?
3679 // Can we cut them down before writing them ?
3680
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003681 // If there were any dynamic classes declarations, deserialize them
3682 // and add them to Sema's vector of such declarations.
3683 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I)
3684 SemaObj->DynamicClasses.push_back(
3685 cast<CXXRecordDecl>(GetDecl(DynamicClasses[I])));
Fariborz Jahanian32019832010-07-23 19:11:11 +00003686
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003687 // Load the offsets of the declarations that Sema references.
3688 // They will be lazily deserialized when needed.
3689 if (!SemaDeclRefs.empty()) {
3690 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
3691 SemaObj->StdNamespace = SemaDeclRefs[0];
3692 SemaObj->StdBadAlloc = SemaDeclRefs[1];
3693 }
3694
Sebastian Redlc3632732010-10-05 15:59:54 +00003695 for (PerFileData *F = FirstInSource; F; F = F->NextInSource) {
3696
3697 // If there are @selector references added them to its pool. This is for
3698 // implementation of -Wselector.
3699 if (!F->ReferencedSelectorsData.empty()) {
3700 unsigned int DataSize = F->ReferencedSelectorsData.size()-1;
3701 unsigned I = 0;
3702 while (I < DataSize) {
3703 Selector Sel = DecodeSelector(F->ReferencedSelectorsData[I++]);
3704 SourceLocation SelLoc = ReadSourceLocation(
3705 *F, F->ReferencedSelectorsData, I);
3706 SemaObj->ReferencedSelectors.insert(std::make_pair(Sel, SelLoc));
3707 }
3708 }
3709
3710 // If there were any pending implicit instantiations, deserialize them
3711 // and add them to Sema's queue of such instantiations.
3712 assert(F->PendingInstantiations.size() % 2 == 0 &&
3713 "Expected pairs of entries");
3714 for (unsigned Idx = 0, N = F->PendingInstantiations.size(); Idx < N;) {
3715 ValueDecl *D=cast<ValueDecl>(GetDecl(F->PendingInstantiations[Idx++]));
3716 SourceLocation Loc = ReadSourceLocation(*F, F->PendingInstantiations,Idx);
3717 SemaObj->PendingInstantiations.push_back(std::make_pair(D, Loc));
3718 }
3719 }
3720
3721 // The two special data sets below always come from the most recent PCH,
3722 // which is at the front of the chain.
3723 PerFileData &F = *Chain.front();
3724
3725 // If there were any weak undeclared identifiers, deserialize them and add to
3726 // Sema's list of weak undeclared identifiers.
3727 if (!WeakUndeclaredIdentifiers.empty()) {
3728 unsigned Idx = 0;
3729 for (unsigned I = 0, N = WeakUndeclaredIdentifiers[Idx++]; I != N; ++I) {
3730 IdentifierInfo *WeakId = GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
3731 IdentifierInfo *AliasId= GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
3732 SourceLocation Loc = ReadSourceLocation(F, WeakUndeclaredIdentifiers,Idx);
3733 bool Used = WeakUndeclaredIdentifiers[Idx++];
3734 Sema::WeakInfo WI(AliasId, Loc);
3735 WI.setUsed(Used);
3736 SemaObj->WeakUndeclaredIdentifiers.insert(std::make_pair(WeakId, WI));
3737 }
3738 }
3739
3740 // If there were any VTable uses, deserialize the information and add it
3741 // to Sema's vector and map of VTable uses.
3742 if (!VTableUses.empty()) {
3743 unsigned Idx = 0;
3744 for (unsigned I = 0, N = VTableUses[Idx++]; I != N; ++I) {
3745 CXXRecordDecl *Class = cast<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
3746 SourceLocation Loc = ReadSourceLocation(F, VTableUses, Idx);
3747 bool DefinitionRequired = VTableUses[Idx++];
3748 SemaObj->VTableUses.push_back(std::make_pair(Class, Loc));
3749 SemaObj->VTablesUsed[Class] = DefinitionRequired;
Fariborz Jahanian32019832010-07-23 19:11:11 +00003750 }
3751 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00003752}
3753
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003754IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Sebastian Redld8c5abb2010-08-02 18:30:12 +00003755 // Try to find this name within our on-disk hash tables. We start with the
3756 // most recent one, since that one contains the most up-to-date info.
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003757 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003758 ASTIdentifierLookupTable *IdTable
3759 = (ASTIdentifierLookupTable *)Chain[I]->IdentifierLookupTable;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003760 if (!IdTable)
3761 continue;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003762 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003763 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003764 if (Pos == IdTable->end())
3765 continue;
Douglas Gregor668c1a42009-04-21 22:25:48 +00003766
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003767 // Dereferencing the iterator has the effect of building the
3768 // IdentifierInfo node and populating it with the various
3769 // declarations it needs.
Sebastian Redld8c5abb2010-08-02 18:30:12 +00003770 return *Pos;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003771 }
Sebastian Redld8c5abb2010-08-02 18:30:12 +00003772 return 0;
Douglas Gregor668c1a42009-04-21 22:25:48 +00003773}
3774
Douglas Gregor95f42922010-10-14 22:11:03 +00003775namespace clang {
3776 /// \brief An identifier-lookup iterator that enumerates all of the
3777 /// identifiers stored within a set of AST files.
3778 class ASTIdentifierIterator : public IdentifierIterator {
3779 /// \brief The AST reader whose identifiers are being enumerated.
3780 const ASTReader &Reader;
3781
3782 /// \brief The current index into the chain of AST files stored in
3783 /// the AST reader.
3784 unsigned Index;
3785
3786 /// \brief The current position within the identifier lookup table
3787 /// of the current AST file.
3788 ASTIdentifierLookupTable::key_iterator Current;
3789
3790 /// \brief The end position within the identifier lookup table of
3791 /// the current AST file.
3792 ASTIdentifierLookupTable::key_iterator End;
3793
3794 public:
3795 explicit ASTIdentifierIterator(const ASTReader &Reader);
3796
3797 virtual llvm::StringRef Next();
3798 };
3799}
3800
3801ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
3802 : Reader(Reader), Index(Reader.Chain.size() - 1) {
3803 ASTIdentifierLookupTable *IdTable
3804 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
3805 Current = IdTable->key_begin();
3806 End = IdTable->key_end();
3807}
3808
3809llvm::StringRef ASTIdentifierIterator::Next() {
3810 while (Current == End) {
3811 // If we have exhausted all of our AST files, we're done.
3812 if (Index == 0)
3813 return llvm::StringRef();
3814
3815 --Index;
3816 ASTIdentifierLookupTable *IdTable
3817 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
3818 Current = IdTable->key_begin();
3819 End = IdTable->key_end();
3820 }
3821
3822 // We have any identifiers remaining in the current AST file; return
3823 // the next one.
3824 std::pair<const char*, unsigned> Key = *Current;
3825 ++Current;
3826 return llvm::StringRef(Key.first, Key.second);
3827}
3828
3829IdentifierIterator *ASTReader::getIdentifiers() const {
3830 return new ASTIdentifierIterator(*this);
3831}
3832
Mike Stump1eb44332009-09-09 15:08:12 +00003833std::pair<ObjCMethodList, ObjCMethodList>
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003834ASTReader::ReadMethodPool(Selector Sel) {
Sebastian Redl725cd962010-08-04 20:40:17 +00003835 // Find this selector in a hash table. We want to find the most recent entry.
3836 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3837 PerFileData &F = *Chain[I];
3838 if (!F.SelectorLookupTable)
3839 continue;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003840
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003841 ASTSelectorLookupTable *PoolTable
3842 = (ASTSelectorLookupTable*)F.SelectorLookupTable;
3843 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
Sebastian Redl725cd962010-08-04 20:40:17 +00003844 if (Pos != PoolTable->end()) {
3845 ++NumSelectorsRead;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003846 // FIXME: Not quite happy with the statistics here. We probably should
3847 // disable this tracking when called via LoadSelector.
3848 // Also, should entries without methods count as misses?
3849 ++NumMethodPoolEntriesRead;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003850 ASTSelectorLookupTrait::data_type Data = *Pos;
Sebastian Redl725cd962010-08-04 20:40:17 +00003851 if (DeserializationListener)
3852 DeserializationListener->SelectorRead(Data.ID, Sel);
3853 return std::make_pair(Data.Instance, Data.Factory);
3854 }
Douglas Gregor83941df2009-04-25 17:48:32 +00003855 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003856
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003857 ++NumMethodPoolMisses;
Sebastian Redl725cd962010-08-04 20:40:17 +00003858 return std::pair<ObjCMethodList, ObjCMethodList>();
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003859}
3860
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003861void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redle58aa892010-08-04 18:21:41 +00003862 // It would be complicated to avoid reading the methods anyway. So don't.
3863 ReadMethodPool(Sel);
3864}
3865
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003866void ASTReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00003867 assert(ID && "Non-zero identifier ID required");
Douglas Gregora02b1472009-04-28 21:53:25 +00003868 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003869 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003870 if (DeserializationListener)
3871 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregor668c1a42009-04-21 22:25:48 +00003872}
3873
Douglas Gregord89275b2009-07-06 18:54:52 +00003874/// \brief Set the globally-visible declarations associated with the given
3875/// identifier.
3876///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003877/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump1eb44332009-09-09 15:08:12 +00003878/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregord89275b2009-07-06 18:54:52 +00003879/// them.
3880///
3881/// \param II an IdentifierInfo that refers to one or more globally-visible
3882/// declarations.
3883///
3884/// \param DeclIDs the set of declaration IDs with the name @p II that are
3885/// visible at global scope.
3886///
3887/// \param Nonrecursive should be true to indicate that the caller knows that
3888/// this call is non-recursive, and therefore the globally-visible declarations
3889/// will not be placed onto the pending queue.
Mike Stump1eb44332009-09-09 15:08:12 +00003890void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003891ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Douglas Gregord89275b2009-07-06 18:54:52 +00003892 const llvm::SmallVectorImpl<uint32_t> &DeclIDs,
3893 bool Nonrecursive) {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00003894 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregord89275b2009-07-06 18:54:52 +00003895 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
3896 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
3897 PII.II = II;
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00003898 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregord89275b2009-07-06 18:54:52 +00003899 return;
3900 }
Mike Stump1eb44332009-09-09 15:08:12 +00003901
Douglas Gregord89275b2009-07-06 18:54:52 +00003902 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
3903 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
3904 if (SemaObj) {
Douglas Gregor914ed9d2010-08-13 03:15:25 +00003905 if (SemaObj->TUScope) {
3906 // Introduce this declaration into the translation-unit scope
3907 // and add it to the declaration chain for this identifier, so
3908 // that (unqualified) name lookup will find it.
John McCalld226f652010-08-21 09:40:31 +00003909 SemaObj->TUScope->AddDecl(D);
Douglas Gregor914ed9d2010-08-13 03:15:25 +00003910 }
Douglas Gregor76dc8892010-09-24 23:29:12 +00003911 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
Douglas Gregord89275b2009-07-06 18:54:52 +00003912 } else {
3913 // Queue this declaration so that it will be added to the
3914 // translation unit scope and identifier's declaration chain
3915 // once a Sema object is known.
3916 PreloadedDecls.push_back(D);
3917 }
3918 }
3919}
3920
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003921IdentifierInfo *ASTReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00003922 if (ID == 0)
3923 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003924
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00003925 if (IdentifiersLoaded.empty()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003926 Error("no identifier table in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00003927 return 0;
3928 }
Mike Stump1eb44332009-09-09 15:08:12 +00003929
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003930 assert(PP && "Forgot to set Preprocessor ?");
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00003931 ID -= 1;
3932 if (!IdentifiersLoaded[ID]) {
3933 unsigned Index = ID;
3934 const char *Str = 0;
3935 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3936 PerFileData *F = Chain[N - I - 1];
3937 if (Index < F->LocalNumIdentifiers) {
3938 uint32_t Offset = F->IdentifierOffsets[Index];
3939 Str = F->IdentifierTableData + Offset;
3940 break;
3941 }
3942 Index -= F->LocalNumIdentifiers;
3943 }
3944 assert(Str && "Broken Chain");
Douglas Gregord6595a42009-04-25 21:04:17 +00003945
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003946 // All of the strings in the AST file are preceded by a 16-bit length.
3947 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenek231bc0b2009-10-23 04:45:31 +00003948 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
3949 // unsigned integers. This is important to avoid integer overflow when
3950 // we cast them to 'unsigned'.
Ted Kremenekff1ea462009-10-23 03:57:22 +00003951 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregor02fc7512009-04-28 20:01:51 +00003952 unsigned StrLen = (((unsigned) StrLenPtr[0])
3953 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00003954 IdentifiersLoaded[ID]
Kovarththanan Rajaratnam811f4262010-03-12 10:32:27 +00003955 = &PP->getIdentifierTable().get(Str, StrLen);
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003956 if (DeserializationListener)
3957 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregorafaf3082009-04-11 00:14:32 +00003958 }
Mike Stump1eb44332009-09-09 15:08:12 +00003959
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00003960 return IdentifiersLoaded[ID];
Douglas Gregor2cf26342009-04-09 22:27:44 +00003961}
3962
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003963void ASTReader::ReadSLocEntry(unsigned ID) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00003964 ReadSLocEntryRecord(ID);
3965}
3966
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003967Selector ASTReader::DecodeSelector(unsigned ID) {
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003968 if (ID == 0)
3969 return Selector();
Mike Stump1eb44332009-09-09 15:08:12 +00003970
Sebastian Redl725cd962010-08-04 20:40:17 +00003971 if (ID > SelectorsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003972 Error("selector ID out of range in AST file");
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003973 return Selector();
3974 }
Douglas Gregor83941df2009-04-25 17:48:32 +00003975
Sebastian Redl725cd962010-08-04 20:40:17 +00003976 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor83941df2009-04-25 17:48:32 +00003977 // Load this selector from the selector table.
Sebastian Redl725cd962010-08-04 20:40:17 +00003978 unsigned Idx = ID - 1;
3979 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3980 PerFileData &F = *Chain[N - I - 1];
3981 if (Idx < F.LocalNumSelectors) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003982 ASTSelectorLookupTrait Trait(*this);
Sebastian Redl725cd962010-08-04 20:40:17 +00003983 SelectorsLoaded[ID - 1] =
3984 Trait.ReadKey(F.SelectorLookupTableData + F.SelectorOffsets[Idx], 0);
3985 if (DeserializationListener)
3986 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
3987 break;
3988 }
3989 Idx -= F.LocalNumSelectors;
3990 }
Douglas Gregor83941df2009-04-25 17:48:32 +00003991 }
3992
Sebastian Redl725cd962010-08-04 20:40:17 +00003993 return SelectorsLoaded[ID - 1];
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003994}
3995
Michael J. Spencer20249a12010-10-21 03:16:25 +00003996Selector ASTReader::GetExternalSelector(uint32_t ID) {
Douglas Gregor719770d2010-04-06 17:30:22 +00003997 return DecodeSelector(ID);
3998}
3999
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004000uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redl725cd962010-08-04 20:40:17 +00004001 // ID 0 (the null selector) is considered an external selector.
4002 return getTotalNumSelectors() + 1;
Douglas Gregor719770d2010-04-06 17:30:22 +00004003}
4004
Mike Stump1eb44332009-09-09 15:08:12 +00004005DeclarationName
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004006ASTReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00004007 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
4008 switch (Kind) {
4009 case DeclarationName::Identifier:
4010 return DeclarationName(GetIdentifierInfo(Record, Idx));
4011
4012 case DeclarationName::ObjCZeroArgSelector:
4013 case DeclarationName::ObjCOneArgSelector:
4014 case DeclarationName::ObjCMultiArgSelector:
Steve Naroffa7503a72009-04-23 15:15:40 +00004015 return DeclarationName(GetSelector(Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004016
4017 case DeclarationName::CXXConstructorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00004018 return Context->DeclarationNames.getCXXConstructorName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00004019 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004020
4021 case DeclarationName::CXXDestructorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00004022 return Context->DeclarationNames.getCXXDestructorName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00004023 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004024
4025 case DeclarationName::CXXConversionFunctionName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00004026 return Context->DeclarationNames.getCXXConversionFunctionName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00004027 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004028
4029 case DeclarationName::CXXOperatorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00004030 return Context->DeclarationNames.getCXXOperatorName(
Douglas Gregor2cf26342009-04-09 22:27:44 +00004031 (OverloadedOperatorKind)Record[Idx++]);
4032
Sean Hunt3e518bd2009-11-29 07:34:05 +00004033 case DeclarationName::CXXLiteralOperatorName:
4034 return Context->DeclarationNames.getCXXLiteralOperatorName(
4035 GetIdentifierInfo(Record, Idx));
4036
Douglas Gregor2cf26342009-04-09 22:27:44 +00004037 case DeclarationName::CXXUsingDirective:
4038 return DeclarationName::getUsingDirectiveName();
4039 }
4040
4041 // Required to silence GCC warning
4042 return DeclarationName();
4043}
Douglas Gregor0a0428e2009-04-10 20:39:37 +00004044
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004045void ASTReader::ReadDeclarationNameLoc(PerFileData &F,
4046 DeclarationNameLoc &DNLoc,
4047 DeclarationName Name,
4048 const RecordData &Record, unsigned &Idx) {
4049 switch (Name.getNameKind()) {
4050 case DeclarationName::CXXConstructorName:
4051 case DeclarationName::CXXDestructorName:
4052 case DeclarationName::CXXConversionFunctionName:
4053 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
4054 break;
4055
4056 case DeclarationName::CXXOperatorName:
4057 DNLoc.CXXOperatorName.BeginOpNameLoc
4058 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4059 DNLoc.CXXOperatorName.EndOpNameLoc
4060 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4061 break;
4062
4063 case DeclarationName::CXXLiteralOperatorName:
4064 DNLoc.CXXLiteralOperatorName.OpNameLoc
4065 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4066 break;
4067
4068 case DeclarationName::Identifier:
4069 case DeclarationName::ObjCZeroArgSelector:
4070 case DeclarationName::ObjCOneArgSelector:
4071 case DeclarationName::ObjCMultiArgSelector:
4072 case DeclarationName::CXXUsingDirective:
4073 break;
4074 }
4075}
4076
4077void ASTReader::ReadDeclarationNameInfo(PerFileData &F,
4078 DeclarationNameInfo &NameInfo,
4079 const RecordData &Record, unsigned &Idx) {
4080 NameInfo.setName(ReadDeclarationName(Record, Idx));
4081 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
4082 DeclarationNameLoc DNLoc;
4083 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
4084 NameInfo.setInfo(DNLoc);
4085}
4086
4087void ASTReader::ReadQualifierInfo(PerFileData &F, QualifierInfo &Info,
4088 const RecordData &Record, unsigned &Idx) {
4089 Info.NNS = ReadNestedNameSpecifier(Record, Idx);
4090 Info.NNSRange = ReadSourceRange(F, Record, Idx);
4091 unsigned NumTPLists = Record[Idx++];
4092 Info.NumTemplParamLists = NumTPLists;
4093 if (NumTPLists) {
4094 Info.TemplParamLists = new (*Context) TemplateParameterList*[NumTPLists];
4095 for (unsigned i=0; i != NumTPLists; ++i)
4096 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
4097 }
4098}
4099
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004100TemplateName
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004101ASTReader::ReadTemplateName(const RecordData &Record, unsigned &Idx) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00004102 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004103 switch (Kind) {
4104 case TemplateName::Template:
4105 return TemplateName(cast_or_null<TemplateDecl>(GetDecl(Record[Idx++])));
4106
4107 case TemplateName::OverloadedTemplate: {
4108 unsigned size = Record[Idx++];
4109 UnresolvedSet<8> Decls;
4110 while (size--)
4111 Decls.addDecl(cast<NamedDecl>(GetDecl(Record[Idx++])));
4112
4113 return Context->getOverloadedTemplateName(Decls.begin(), Decls.end());
4114 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004115
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004116 case TemplateName::QualifiedTemplate: {
4117 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
4118 bool hasTemplKeyword = Record[Idx++];
4119 TemplateDecl *Template = cast<TemplateDecl>(GetDecl(Record[Idx++]));
4120 return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
4121 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004122
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004123 case TemplateName::DependentTemplate: {
4124 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
4125 if (Record[Idx++]) // isIdentifier
4126 return Context->getDependentTemplateName(NNS,
4127 GetIdentifierInfo(Record, Idx));
4128 return Context->getDependentTemplateName(NNS,
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004129 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004130 }
4131 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004132
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004133 assert(0 && "Unhandled template name kind!");
4134 return TemplateName();
4135}
4136
4137TemplateArgument
Sebastian Redlc3632732010-10-05 15:59:54 +00004138ASTReader::ReadTemplateArgument(PerFileData &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00004139 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004140 switch ((TemplateArgument::ArgKind)Record[Idx++]) {
4141 case TemplateArgument::Null:
4142 return TemplateArgument();
4143 case TemplateArgument::Type:
4144 return TemplateArgument(GetType(Record[Idx++]));
4145 case TemplateArgument::Declaration:
4146 return TemplateArgument(GetDecl(Record[Idx++]));
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00004147 case TemplateArgument::Integral: {
4148 llvm::APSInt Value = ReadAPSInt(Record, Idx);
4149 QualType T = GetType(Record[Idx++]);
4150 return TemplateArgument(Value, T);
4151 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004152 case TemplateArgument::Template:
4153 return TemplateArgument(ReadTemplateName(Record, Idx));
4154 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00004155 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004156 case TemplateArgument::Pack: {
4157 unsigned NumArgs = Record[Idx++];
Douglas Gregor910f8002010-11-07 23:05:16 +00004158 TemplateArgument *Args = new (*Context) TemplateArgument[NumArgs];
4159 for (unsigned I = 0; I != NumArgs; ++I)
4160 Args[I] = ReadTemplateArgument(F, Record, Idx);
4161 return TemplateArgument(Args, NumArgs);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004162 }
4163 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004164
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004165 assert(0 && "Unhandled template argument kind!");
4166 return TemplateArgument();
4167}
4168
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004169TemplateParameterList *
Sebastian Redlc3632732010-10-05 15:59:54 +00004170ASTReader::ReadTemplateParameterList(PerFileData &F,
4171 const RecordData &Record, unsigned &Idx) {
4172 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
4173 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
4174 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004175
4176 unsigned NumParams = Record[Idx++];
4177 llvm::SmallVector<NamedDecl *, 16> Params;
4178 Params.reserve(NumParams);
4179 while (NumParams--)
4180 Params.push_back(cast<NamedDecl>(GetDecl(Record[Idx++])));
Michael J. Spencer20249a12010-10-21 03:16:25 +00004181
4182 TemplateParameterList* TemplateParams =
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004183 TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc,
4184 Params.data(), Params.size(), RAngleLoc);
4185 return TemplateParams;
4186}
4187
4188void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004189ASTReader::
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004190ReadTemplateArgumentList(llvm::SmallVector<TemplateArgument, 8> &TemplArgs,
Sebastian Redlc3632732010-10-05 15:59:54 +00004191 PerFileData &F, const RecordData &Record,
4192 unsigned &Idx) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004193 unsigned NumTemplateArgs = Record[Idx++];
4194 TemplArgs.reserve(NumTemplateArgs);
4195 while (NumTemplateArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00004196 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004197}
4198
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004199/// \brief Read a UnresolvedSet structure.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004200void ASTReader::ReadUnresolvedSet(UnresolvedSetImpl &Set,
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004201 const RecordData &Record, unsigned &Idx) {
4202 unsigned NumDecls = Record[Idx++];
4203 while (NumDecls--) {
4204 NamedDecl *D = cast<NamedDecl>(GetDecl(Record[Idx++]));
4205 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
4206 Set.addDecl(D, AS);
4207 }
4208}
4209
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004210CXXBaseSpecifier
Sebastian Redlc3632732010-10-05 15:59:54 +00004211ASTReader::ReadCXXBaseSpecifier(PerFileData &F,
Nick Lewycky56062202010-07-26 16:56:01 +00004212 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004213 bool isVirtual = static_cast<bool>(Record[Idx++]);
4214 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
4215 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00004216 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
4217 SourceRange Range = ReadSourceRange(F, Record, Idx);
Nick Lewycky56062202010-07-26 16:56:01 +00004218 return CXXBaseSpecifier(Range, isVirtual, isBaseOfClass, AS, TInfo);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004219}
4220
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004221std::pair<CXXBaseOrMemberInitializer **, unsigned>
Sebastian Redlc3632732010-10-05 15:59:54 +00004222ASTReader::ReadCXXBaseOrMemberInitializers(PerFileData &F,
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004223 const RecordData &Record,
4224 unsigned &Idx) {
4225 CXXBaseOrMemberInitializer **BaseOrMemberInitializers = 0;
4226 unsigned NumInitializers = Record[Idx++];
4227 if (NumInitializers) {
4228 ASTContext &C = *getContext();
4229
4230 BaseOrMemberInitializers
4231 = new (C) CXXBaseOrMemberInitializer*[NumInitializers];
4232 for (unsigned i=0; i != NumInitializers; ++i) {
4233 TypeSourceInfo *BaseClassInfo = 0;
4234 bool IsBaseVirtual = false;
4235 FieldDecl *Member = 0;
Michael J. Spencer20249a12010-10-21 03:16:25 +00004236
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004237 bool IsBaseInitializer = Record[Idx++];
4238 if (IsBaseInitializer) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004239 BaseClassInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004240 IsBaseVirtual = Record[Idx++];
4241 } else {
4242 Member = cast<FieldDecl>(GetDecl(Record[Idx++]));
4243 }
Sebastian Redlc3632732010-10-05 15:59:54 +00004244 SourceLocation MemberLoc = ReadSourceLocation(F, Record, Idx);
4245 Expr *Init = ReadExpr(F);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004246 FieldDecl *AnonUnionMember
4247 = cast_or_null<FieldDecl>(GetDecl(Record[Idx++]));
Sebastian Redlc3632732010-10-05 15:59:54 +00004248 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
4249 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004250 bool IsWritten = Record[Idx++];
4251 unsigned SourceOrderOrNumArrayIndices;
4252 llvm::SmallVector<VarDecl *, 8> Indices;
4253 if (IsWritten) {
4254 SourceOrderOrNumArrayIndices = Record[Idx++];
4255 } else {
4256 SourceOrderOrNumArrayIndices = Record[Idx++];
4257 Indices.reserve(SourceOrderOrNumArrayIndices);
4258 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
4259 Indices.push_back(cast<VarDecl>(GetDecl(Record[Idx++])));
4260 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004261
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004262 CXXBaseOrMemberInitializer *BOMInit;
4263 if (IsBaseInitializer) {
4264 BOMInit = new (C) CXXBaseOrMemberInitializer(C, BaseClassInfo,
4265 IsBaseVirtual, LParenLoc,
4266 Init, RParenLoc);
4267 } else if (IsWritten) {
4268 BOMInit = new (C) CXXBaseOrMemberInitializer(C, Member, MemberLoc,
4269 LParenLoc, Init, RParenLoc);
4270 } else {
4271 BOMInit = CXXBaseOrMemberInitializer::Create(C, Member, MemberLoc,
4272 LParenLoc, Init, RParenLoc,
4273 Indices.data(),
4274 Indices.size());
4275 }
4276
Argyrios Kyrtzidisf84cde12010-09-06 19:04:27 +00004277 if (IsWritten)
4278 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004279 BOMInit->setAnonUnionMember(AnonUnionMember);
4280 BaseOrMemberInitializers[i] = BOMInit;
4281 }
4282 }
4283
4284 return std::make_pair(BaseOrMemberInitializers, NumInitializers);
4285}
4286
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004287NestedNameSpecifier *
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004288ASTReader::ReadNestedNameSpecifier(const RecordData &Record, unsigned &Idx) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004289 unsigned N = Record[Idx++];
4290 NestedNameSpecifier *NNS = 0, *Prev = 0;
4291 for (unsigned I = 0; I != N; ++I) {
4292 NestedNameSpecifier::SpecifierKind Kind
4293 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
4294 switch (Kind) {
4295 case NestedNameSpecifier::Identifier: {
4296 IdentifierInfo *II = GetIdentifierInfo(Record, Idx);
4297 NNS = NestedNameSpecifier::Create(*Context, Prev, II);
4298 break;
4299 }
4300
4301 case NestedNameSpecifier::Namespace: {
4302 NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++]));
4303 NNS = NestedNameSpecifier::Create(*Context, Prev, NS);
4304 break;
4305 }
4306
4307 case NestedNameSpecifier::TypeSpec:
4308 case NestedNameSpecifier::TypeSpecWithTemplate: {
4309 Type *T = GetType(Record[Idx++]).getTypePtr();
4310 bool Template = Record[Idx++];
4311 NNS = NestedNameSpecifier::Create(*Context, Prev, Template, T);
4312 break;
4313 }
4314
4315 case NestedNameSpecifier::Global: {
4316 NNS = NestedNameSpecifier::GlobalSpecifier(*Context);
4317 // No associated value, and there can't be a prefix.
4318 break;
4319 }
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004320 }
Argyrios Kyrtzidisd2bb2c02010-07-07 15:46:30 +00004321 Prev = NNS;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004322 }
4323 return NNS;
4324}
4325
4326SourceRange
Sebastian Redlc3632732010-10-05 15:59:54 +00004327ASTReader::ReadSourceRange(PerFileData &F, const RecordData &Record,
4328 unsigned &Idx) {
4329 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
4330 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar8ee59392010-06-02 15:47:10 +00004331 return SourceRange(beg, end);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004332}
4333
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004334/// \brief Read an integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004335llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004336 unsigned BitWidth = Record[Idx++];
4337 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
4338 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
4339 Idx += NumWords;
4340 return Result;
4341}
4342
4343/// \brief Read a signed integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004344llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004345 bool isUnsigned = Record[Idx++];
4346 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
4347}
4348
Douglas Gregor17fc2232009-04-14 21:55:33 +00004349/// \brief Read a floating-point value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004350llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00004351 return llvm::APFloat(ReadAPInt(Record, Idx));
4352}
4353
Douglas Gregor68a2eb02009-04-15 21:30:51 +00004354// \brief Read a string
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004355std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00004356 unsigned Len = Record[Idx++];
Jay Foadbeaaccd2009-05-21 09:52:38 +00004357 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00004358 Idx += Len;
4359 return Result;
4360}
4361
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004362CXXTemporary *ASTReader::ReadCXXTemporary(const RecordData &Record,
Chris Lattnerd2598362010-05-10 00:25:06 +00004363 unsigned &Idx) {
4364 CXXDestructorDecl *Decl = cast<CXXDestructorDecl>(GetDecl(Record[Idx++]));
4365 return CXXTemporary::Create(*Context, Decl);
4366}
4367
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004368DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00004369 return Diag(SourceLocation(), DiagID);
4370}
4371
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004372DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004373 return Diags.Report(Loc, DiagID);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00004374}
Douglas Gregor025452f2009-04-17 00:04:06 +00004375
Douglas Gregor668c1a42009-04-21 22:25:48 +00004376/// \brief Retrieve the identifier table associated with the
4377/// preprocessor.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004378IdentifierTable &ASTReader::getIdentifierTable() {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00004379 assert(PP && "Forgot to set Preprocessor ?");
4380 return PP->getIdentifierTable();
Douglas Gregor668c1a42009-04-21 22:25:48 +00004381}
4382
Douglas Gregor025452f2009-04-17 00:04:06 +00004383/// \brief Record that the given ID maps to the given switch-case
4384/// statement.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004385void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Douglas Gregor025452f2009-04-17 00:04:06 +00004386 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
4387 SwitchCaseStmts[ID] = SC;
4388}
4389
4390/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004391SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Douglas Gregor025452f2009-04-17 00:04:06 +00004392 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
4393 return SwitchCaseStmts[ID];
4394}
Douglas Gregor1de05fe2009-04-17 18:18:49 +00004395
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00004396void ASTReader::ClearSwitchCaseIDs() {
4397 SwitchCaseStmts.clear();
4398}
4399
Douglas Gregor1de05fe2009-04-17 18:18:49 +00004400/// \brief Record that the given label statement has been
4401/// deserialized and has the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004402void ASTReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
Mike Stump1eb44332009-09-09 15:08:12 +00004403 assert(LabelStmts.find(ID) == LabelStmts.end() &&
Douglas Gregor1de05fe2009-04-17 18:18:49 +00004404 "Deserialized label twice");
4405 LabelStmts[ID] = S;
4406
4407 // If we've already seen any goto statements that point to this
4408 // label, resolve them now.
4409 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
4410 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
4411 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
4412 Goto->second->setLabel(S);
4413 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00004414
4415 // If we've already seen any address-label statements that point to
4416 // this label, resolve them now.
4417 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
Mike Stump1eb44332009-09-09 15:08:12 +00004418 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00004419 = UnresolvedAddrLabelExprs.equal_range(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00004420 for (AddrLabelIter AddrLabel = AddrLabels.first;
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00004421 AddrLabel != AddrLabels.second; ++AddrLabel)
4422 AddrLabel->second->setLabel(S);
4423 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor1de05fe2009-04-17 18:18:49 +00004424}
4425
4426/// \brief Set the label of the given statement to the label
4427/// identified by ID.
4428///
4429/// Depending on the order in which the label and other statements
4430/// referencing that label occur, this operation may complete
4431/// immediately (updating the statement) or it may queue the
4432/// statement to be back-patched later.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004433void ASTReader::SetLabelOf(GotoStmt *S, unsigned ID) {
Douglas Gregor1de05fe2009-04-17 18:18:49 +00004434 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
4435 if (Label != LabelStmts.end()) {
4436 // We've already seen this label, so set the label of the goto and
4437 // we're done.
4438 S->setLabel(Label->second);
4439 } else {
4440 // We haven't seen this label yet, so add this goto to the set of
4441 // unresolved goto statements.
4442 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
4443 }
4444}
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00004445
4446/// \brief Set the label of the given expression to the label
4447/// identified by ID.
4448///
4449/// Depending on the order in which the label and other statements
4450/// referencing that label occur, this operation may complete
4451/// immediately (updating the statement) or it may queue the
4452/// statement to be back-patched later.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004453void ASTReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00004454 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
4455 if (Label != LabelStmts.end()) {
4456 // We've already seen this label, so set the label of the
4457 // label-address expression and we're done.
4458 S->setLabel(Label->second);
4459 } else {
4460 // We haven't seen this label yet, so add this label-address
4461 // expression to the set of unresolved label-address expressions.
4462 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
4463 }
4464}
Douglas Gregord89275b2009-07-06 18:54:52 +00004465
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004466void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004467 assert(NumCurrentElementsDeserializing &&
4468 "FinishedDeserializing not paired with StartedDeserializing");
4469 if (NumCurrentElementsDeserializing == 1) {
Douglas Gregord89275b2009-07-06 18:54:52 +00004470 // If any identifiers with corresponding top-level declarations have
4471 // been loaded, load those declarations now.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004472 while (!PendingIdentifierInfos.empty()) {
4473 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
4474 PendingIdentifierInfos.front().DeclIDs, true);
4475 PendingIdentifierInfos.pop_front();
Douglas Gregord89275b2009-07-06 18:54:52 +00004476 }
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00004477
4478 // We are not in recursive loading, so it's safe to pass the "interesting"
4479 // decls to the consumer.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004480 if (Consumer)
4481 PassInterestingDeclsToConsumer();
Argyrios Kyrtzidis134db1f2010-10-24 17:26:31 +00004482
4483 assert(PendingForwardRefs.size() == 0 &&
4484 "Some forward refs did not get linked to the definition!");
Douglas Gregord89275b2009-07-06 18:54:52 +00004485 }
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004486 --NumCurrentElementsDeserializing;
Douglas Gregord89275b2009-07-06 18:54:52 +00004487}
Douglas Gregor501c1032010-08-19 00:28:17 +00004488
Sebastian Redle1dde812010-08-24 00:50:04 +00004489ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context,
4490 const char *isysroot, bool DisableValidation)
4491 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
4492 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
4493 Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context),
4494 Consumer(0), isysroot(isysroot), DisableValidation(DisableValidation),
4495 NumStatHits(0), NumStatMisses(0), NumSLocEntriesRead(0),
Sebastian Redl8db9fae2010-09-22 20:19:08 +00004496 TotalNumSLocEntries(0), NextSLocOffset(0), NumStatementsRead(0),
4497 TotalNumStatements(0), NumMacrosRead(0), TotalNumMacros(0),
4498 NumSelectorsRead(0), NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
Sebastian Redle1dde812010-08-24 00:50:04 +00004499 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
4500 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
4501 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
4502 RelocatablePCH = false;
4503}
4504
4505ASTReader::ASTReader(SourceManager &SourceMgr, FileManager &FileMgr,
4506 Diagnostic &Diags, const char *isysroot,
4507 bool DisableValidation)
4508 : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr),
4509 Diags(Diags), SemaObj(0), PP(0), Context(0), Consumer(0),
4510 isysroot(isysroot), DisableValidation(DisableValidation), NumStatHits(0),
4511 NumStatMisses(0), NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Sebastian Redl8db9fae2010-09-22 20:19:08 +00004512 NextSLocOffset(0), NumStatementsRead(0), TotalNumStatements(0),
4513 NumMacrosRead(0), TotalNumMacros(0), NumSelectorsRead(0),
4514 NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
4515 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
4516 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
4517 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
Sebastian Redle1dde812010-08-24 00:50:04 +00004518 RelocatablePCH = false;
4519}
4520
4521ASTReader::~ASTReader() {
4522 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
4523 delete Chain[e - i - 1];
4524 // Delete all visible decl lookup tables
4525 for (DeclContextOffsetsMap::iterator I = DeclContextOffsets.begin(),
4526 E = DeclContextOffsets.end();
4527 I != E; ++I) {
4528 for (DeclContextInfos::iterator J = I->second.begin(), F = I->second.end();
4529 J != F; ++J) {
4530 if (J->NameLookupTableData)
4531 delete static_cast<ASTDeclContextNameLookupTable*>(
4532 J->NameLookupTableData);
4533 }
4534 }
4535 for (DeclContextVisibleUpdatesPending::iterator
4536 I = PendingVisibleUpdates.begin(),
4537 E = PendingVisibleUpdates.end();
4538 I != E; ++I) {
4539 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
4540 F = I->second.end();
4541 J != F; ++J)
4542 delete static_cast<ASTDeclContextNameLookupTable*>(*J);
4543 }
4544}
4545
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00004546ASTReader::PerFileData::PerFileData(ASTFileType Ty)
4547 : Type(Ty), SizeInBits(0), LocalNumSLocEntries(0), SLocOffsets(0), LocalSLocSize(0),
Sebastian Redl301c9b02010-09-22 00:42:27 +00004548 LocalNumIdentifiers(0), IdentifierOffsets(0), IdentifierTableData(0),
4549 IdentifierLookupTable(0), LocalNumMacroDefinitions(0),
4550 MacroDefinitionOffsets(0), LocalNumSelectors(0), SelectorOffsets(0),
4551 SelectorLookupTableData(0), SelectorLookupTable(0), LocalNumDecls(0),
Douglas Gregor7c789c12010-10-29 22:39:52 +00004552 DeclOffsets(0), LocalNumCXXBaseSpecifiers(0), CXXBaseSpecifiersOffsets(0),
4553 LocalNumTypes(0), TypeOffsets(0), StatCache(0),
Sebastian Redla866e652010-10-01 19:59:12 +00004554 NumPreallocatedPreprocessingEntities(0), NextInSource(0)
Douglas Gregor501c1032010-08-19 00:28:17 +00004555{}
4556
4557ASTReader::PerFileData::~PerFileData() {
4558 delete static_cast<ASTIdentifierLookupTable *>(IdentifierLookupTable);
4559 delete static_cast<ASTSelectorLookupTable *>(SelectorLookupTable);
4560}
4561