blob: 23f2eaac66795185ae21268c0686084aff682154 [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"
Douglas Gregor2bec0412009-04-10 21:16:55 +000036#include "clang/Basic/TargetInfo.h"
Douglas Gregor445e23e2009-10-05 21:07:28 +000037#include "clang/Basic/Version.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000038#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000039#include "llvm/Bitcode/BitstreamReader.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000040#include "llvm/Support/MemoryBuffer.h"
John McCall833ca992009-10-29 08:12:44 +000041#include "llvm/Support/ErrorHandling.h"
Daniel Dunbard5b21972009-11-18 19:50:41 +000042#include "llvm/System/Path.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000043#include <algorithm>
Douglas Gregore721f952009-04-28 18:58:38 +000044#include <iterator>
Douglas Gregor2cf26342009-04-09 22:27:44 +000045#include <cstdio>
Douglas Gregor4fed3f42009-04-27 18:38:38 +000046#include <sys/stat.h>
Douglas Gregor2cf26342009-04-09 22:27:44 +000047using namespace clang;
Sebastian Redl8538e8d2010-08-18 23:57:32 +000048using namespace clang::serialization;
Douglas Gregor2cf26342009-04-09 22:27:44 +000049
50//===----------------------------------------------------------------------===//
Sebastian Redl3c7f4132010-08-18 23:57:06 +000051// PCH validator implementation
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000052//===----------------------------------------------------------------------===//
53
Sebastian Redl571db7f2010-08-18 23:56:56 +000054ASTReaderListener::~ASTReaderListener() {}
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000055
56bool
57PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts) {
58 const LangOptions &PPLangOpts = PP.getLangOptions();
59#define PARSE_LANGOPT_BENIGN(Option)
60#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
61 if (PPLangOpts.Option != LangOpts.Option) { \
62 Reader.Diag(DiagID) << LangOpts.Option << PPLangOpts.Option; \
63 return true; \
64 }
65
66 PARSE_LANGOPT_BENIGN(Trigraphs);
67 PARSE_LANGOPT_BENIGN(BCPLComment);
68 PARSE_LANGOPT_BENIGN(DollarIdents);
69 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
70 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
Chandler Carrutheb5d7b72010-04-17 20:17:31 +000071 PARSE_LANGOPT_IMPORTANT(GNUKeywords, diag::warn_pch_gnu_keywords);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000072 PARSE_LANGOPT_BENIGN(ImplicitInt);
73 PARSE_LANGOPT_BENIGN(Digraphs);
74 PARSE_LANGOPT_BENIGN(HexFloats);
75 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
76 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
77 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
78 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
79 PARSE_LANGOPT_BENIGN(CXXOperatorName);
80 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
81 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
82 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
Fariborz Jahanian412e7982010-02-09 19:31:38 +000083 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI2, diag::warn_pch_nonfragile_abi2);
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +000084 PARSE_LANGOPT_IMPORTANT(NoConstantCFStrings,
85 diag::warn_pch_no_constant_cfstrings);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000086 PARSE_LANGOPT_BENIGN(PascalStrings);
87 PARSE_LANGOPT_BENIGN(WritableStrings);
Mike Stump1eb44332009-09-09 15:08:12 +000088 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000089 diag::warn_pch_lax_vector_conversions);
Nate Begeman69cfb9b2009-06-25 22:57:40 +000090 PARSE_LANGOPT_IMPORTANT(AltiVec, diag::warn_pch_altivec);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000091 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
Daniel Dunbar73482882010-02-10 18:48:44 +000092 PARSE_LANGOPT_IMPORTANT(SjLjExceptions, diag::warn_pch_sjlj_exceptions);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000093 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
94 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
95 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
Mike Stump1eb44332009-09-09 15:08:12 +000096 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000097 diag::warn_pch_thread_safe_statics);
Daniel Dunbar5345c392009-09-03 04:54:28 +000098 PARSE_LANGOPT_IMPORTANT(POSIXThreads, diag::warn_pch_posix_threads);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000099 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
100 PARSE_LANGOPT_BENIGN(EmitAllDecls);
101 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
Chris Lattnera4d71452010-06-26 21:25:03 +0000102 PARSE_LANGOPT_BENIGN(getSignedOverflowBehavior());
Mike Stump1eb44332009-09-09 15:08:12 +0000103 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000104 diag::warn_pch_heinous_extensions);
105 // FIXME: Most of the options below are benign if the macro wasn't
106 // used. Unfortunately, this means that a PCH compiled without
107 // optimization can't be used with optimization turned on, even
108 // though the only thing that changes is whether __OPTIMIZE__ was
109 // defined... but if __OPTIMIZE__ never showed up in the header, it
110 // doesn't matter. We could consider making this some special kind
111 // of check.
112 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
113 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
114 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
115 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
116 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
117 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
118 PARSE_LANGOPT_IMPORTANT(AccessControl, diag::warn_pch_access_control);
119 PARSE_LANGOPT_IMPORTANT(CharIsSigned, diag::warn_pch_char_signed);
John Thompsona6fda122009-11-05 20:14:16 +0000120 PARSE_LANGOPT_IMPORTANT(ShortWChar, diag::warn_pch_short_wchar);
Argyrios Kyrtzidis9a2b9d72010-10-08 00:25:19 +0000121 PARSE_LANGOPT_IMPORTANT(ShortEnums, diag::warn_pch_short_enums);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000122 if ((PPLangOpts.getGCMode() != 0) != (LangOpts.getGCMode() != 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000123 Reader.Diag(diag::warn_pch_gc_mode)
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000124 << LangOpts.getGCMode() << PPLangOpts.getGCMode();
125 return true;
126 }
127 PARSE_LANGOPT_BENIGN(getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000128 PARSE_LANGOPT_IMPORTANT(getStackProtectorMode(),
129 diag::warn_pch_stack_protector);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000130 PARSE_LANGOPT_BENIGN(InstantiationDepth);
Nate Begeman69cfb9b2009-06-25 22:57:40 +0000131 PARSE_LANGOPT_IMPORTANT(OpenCL, diag::warn_pch_opencl);
Mike Stump9c276ae2009-12-12 01:27:46 +0000132 PARSE_LANGOPT_BENIGN(CatchUndefined);
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000133 PARSE_LANGOPT_IMPORTANT(ElideConstructors, diag::warn_pch_elide_constructors);
Douglas Gregora0068fc2010-07-09 17:35:33 +0000134 PARSE_LANGOPT_BENIGN(SpellChecking);
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +0000135#undef PARSE_LANGOPT_IMPORTANT
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000136#undef PARSE_LANGOPT_BENIGN
137
138 return false;
139}
140
Daniel Dunbardc3c0d22009-11-11 00:52:11 +0000141bool PCHValidator::ReadTargetTriple(llvm::StringRef Triple) {
142 if (Triple == PP.getTargetInfo().getTriple().str())
143 return false;
144
145 Reader.Diag(diag::warn_pch_target_triple)
146 << Triple << PP.getTargetInfo().getTriple().str();
147 return true;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000148}
149
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000150struct EmptyStringRef {
Benjamin Kramerec1b1cc2010-07-14 23:19:41 +0000151 bool operator ()(llvm::StringRef r) const { return r.empty(); }
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000152};
153struct EmptyBlock {
154 bool operator ()(const PCHPredefinesBlock &r) const { return r.Data.empty(); }
155};
156
157static bool EqualConcatenations(llvm::SmallVector<llvm::StringRef, 2> L,
158 PCHPredefinesBlocks R) {
159 // First, sum up the lengths.
160 unsigned LL = 0, RL = 0;
161 for (unsigned I = 0, N = L.size(); I != N; ++I) {
162 LL += L[I].size();
163 }
164 for (unsigned I = 0, N = R.size(); I != N; ++I) {
165 RL += R[I].Data.size();
166 }
167 if (LL != RL)
168 return false;
169 if (LL == 0 && RL == 0)
170 return true;
171
172 // Kick out empty parts, they confuse the algorithm below.
173 L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end());
174 R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end());
175
176 // Do it the hard way. At this point, both vectors must be non-empty.
177 llvm::StringRef LR = L[0], RR = R[0].Data;
178 unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size();
Daniel Dunbarc76c9e02010-07-16 00:00:11 +0000179 (void) RN;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000180 for (;;) {
181 // Compare the current pieces.
182 if (LR.size() == RR.size()) {
183 // If they're the same length, it's pretty easy.
184 if (LR != RR)
185 return false;
186 // Both pieces are done, advance.
187 ++LI;
188 ++RI;
189 // If either string is done, they're both done, since they're the same
190 // length.
191 if (LI == LN) {
192 assert(RI == RN && "Strings not the same length after all?");
193 return true;
194 }
195 LR = L[LI];
196 RR = R[RI].Data;
197 } else if (LR.size() < RR.size()) {
198 // Right piece is longer.
199 if (!RR.startswith(LR))
200 return false;
201 ++LI;
202 assert(LI != LN && "Strings not the same length after all?");
203 RR = RR.substr(LR.size());
204 LR = L[LI];
205 } else {
206 // Left piece is longer.
207 if (!LR.startswith(RR))
208 return false;
209 ++RI;
210 assert(RI != RN && "Strings not the same length after all?");
211 LR = LR.substr(RR.size());
212 RR = R[RI].Data;
213 }
214 }
215}
216
217static std::pair<FileID, llvm::StringRef::size_type>
218FindMacro(const PCHPredefinesBlocks &Buffers, llvm::StringRef MacroDef) {
219 std::pair<FileID, llvm::StringRef::size_type> Res;
220 for (unsigned I = 0, N = Buffers.size(); I != N; ++I) {
221 Res.second = Buffers[I].Data.find(MacroDef);
222 if (Res.second != llvm::StringRef::npos) {
223 Res.first = Buffers[I].BufferID;
224 break;
225 }
226 }
227 return Res;
228}
229
230bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000231 llvm::StringRef OriginalFileName,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000232 std::string &SuggestedPredefines) {
Daniel Dunbarc7162932009-11-11 23:58:53 +0000233 // We are in the context of an implicit include, so the predefines buffer will
234 // have a #include entry for the PCH file itself (as normalized by the
235 // preprocessor initialization). Find it and skip over it in the checking
236 // below.
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000237 llvm::SmallString<256> PCHInclude;
238 PCHInclude += "#include \"";
Daniel Dunbarc7162932009-11-11 23:58:53 +0000239 PCHInclude += NormalizeDashIncludePath(OriginalFileName);
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000240 PCHInclude += "\"\n";
241 std::pair<llvm::StringRef,llvm::StringRef> Split =
242 llvm::StringRef(PP.getPredefines()).split(PCHInclude.str());
243 llvm::StringRef Left = Split.first, Right = Split.second;
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000244 if (Left == PP.getPredefines()) {
245 Error("Missing PCH include entry!");
246 return true;
247 }
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000248
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000249 // If the concatenation of all the PCH buffers is equal to the adjusted
250 // command line, we're done.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000251 llvm::SmallVector<llvm::StringRef, 2> CommandLine;
252 CommandLine.push_back(Left);
253 CommandLine.push_back(Right);
254 if (EqualConcatenations(CommandLine, Buffers))
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000255 return false;
256
257 SourceManager &SourceMgr = PP.getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000258
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000259 // The predefines buffers are different. Determine what the differences are,
260 // and whether they require us to reject the PCH file.
Daniel Dunbare6750492009-11-13 16:46:11 +0000261 llvm::SmallVector<llvm::StringRef, 8> PCHLines;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000262 for (unsigned I = 0, N = Buffers.size(); I != N; ++I)
263 Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Daniel Dunbare6750492009-11-13 16:46:11 +0000264
265 llvm::SmallVector<llvm::StringRef, 8> CmdLineLines;
266 Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000267
268 // Pick out implicit #includes after the PCH and don't consider them for
269 // validation; we will insert them into SuggestedPredefines so that the
270 // preprocessor includes them.
271 std::string IncludesAfterPCH;
272 llvm::SmallVector<llvm::StringRef, 8> AfterPCHLines;
273 Right.split(AfterPCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
274 for (unsigned i = 0, e = AfterPCHLines.size(); i != e; ++i) {
275 if (AfterPCHLines[i].startswith("#include ")) {
276 IncludesAfterPCH += AfterPCHLines[i];
277 IncludesAfterPCH += '\n';
278 } else {
279 CmdLineLines.push_back(AfterPCHLines[i]);
280 }
281 }
282
283 // Make sure we add the includes last into SuggestedPredefines before we
284 // exit this function.
285 struct AddIncludesRAII {
286 std::string &SuggestedPredefines;
287 std::string &IncludesAfterPCH;
288
289 AddIncludesRAII(std::string &SuggestedPredefines,
290 std::string &IncludesAfterPCH)
291 : SuggestedPredefines(SuggestedPredefines),
292 IncludesAfterPCH(IncludesAfterPCH) { }
293 ~AddIncludesRAII() {
294 SuggestedPredefines += IncludesAfterPCH;
295 }
296 } AddIncludes(SuggestedPredefines, IncludesAfterPCH);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000297
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000298 // Sort both sets of predefined buffer lines, since we allow some extra
299 // definitions and they may appear at any point in the output.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000300 std::sort(CmdLineLines.begin(), CmdLineLines.end());
301 std::sort(PCHLines.begin(), PCHLines.end());
302
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000303 // Determine which predefines that were used to build the PCH file are missing
304 // from the command line.
305 std::vector<llvm::StringRef> MissingPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000306 std::set_difference(PCHLines.begin(), PCHLines.end(),
307 CmdLineLines.begin(), CmdLineLines.end(),
308 std::back_inserter(MissingPredefines));
309
310 bool MissingDefines = false;
311 bool ConflictingDefines = false;
312 for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) {
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000313 llvm::StringRef Missing = MissingPredefines[I];
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000314 if (Missing.startswith("#include ")) {
315 // An -include was specified when generating the PCH; it is included in
316 // the PCH, just ignore it.
317 continue;
318 }
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000319 if (!Missing.startswith("#define ")) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000320 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
321 return true;
322 }
Mike Stump1eb44332009-09-09 15:08:12 +0000323
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000324 // This is a macro definition. Determine the name of the macro we're
325 // defining.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000326 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000327 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000328 = Missing.find_first_of("( \n\r", StartOfMacroName);
329 assert(EndOfMacroName != std::string::npos &&
330 "Couldn't find the end of the macro name");
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000331 llvm::StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000332
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000333 // Determine whether this macro was given a different definition on the
334 // command line.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000335 std::string MacroDefStart = "#define " + MacroName.str();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000336 std::string::size_type MacroDefLen = MacroDefStart.size();
Daniel Dunbare6750492009-11-13 16:46:11 +0000337 llvm::SmallVector<llvm::StringRef, 8>::iterator ConflictPos
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000338 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
339 MacroDefStart);
340 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000341 if (!ConflictPos->startswith(MacroDefStart)) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000342 // Different macro; we're done.
343 ConflictPos = CmdLineLines.end();
Mike Stump1eb44332009-09-09 15:08:12 +0000344 break;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000345 }
Mike Stump1eb44332009-09-09 15:08:12 +0000346
347 assert(ConflictPos->size() > MacroDefLen &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000348 "Invalid #define in predefines buffer?");
Mike Stump1eb44332009-09-09 15:08:12 +0000349 if ((*ConflictPos)[MacroDefLen] != ' ' &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000350 (*ConflictPos)[MacroDefLen] != '(')
351 continue; // Longer macro name; keep trying.
Mike Stump1eb44332009-09-09 15:08:12 +0000352
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000353 // We found a conflicting macro definition.
354 break;
355 }
Mike Stump1eb44332009-09-09 15:08:12 +0000356
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000357 if (ConflictPos != CmdLineLines.end()) {
358 Reader.Diag(diag::warn_cmdline_conflicting_macro_def)
359 << MacroName;
360
361 // Show the definition of this macro within the PCH file.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000362 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
363 FindMacro(Buffers, Missing);
364 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
365 SourceLocation PCHMissingLoc =
366 SourceMgr.getLocForStartOfFile(MacroLoc.first)
367 .getFileLocWithOffset(MacroLoc.second);
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000368 Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000369
370 ConflictingDefines = true;
371 continue;
372 }
Mike Stump1eb44332009-09-09 15:08:12 +0000373
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000374 // If the macro doesn't conflict, then we'll just pick up the macro
375 // definition from the PCH file. Warn the user that they made a mistake.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000376 if (ConflictingDefines)
377 continue; // Don't complain if there are already conflicting defs
Mike Stump1eb44332009-09-09 15:08:12 +0000378
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000379 if (!MissingDefines) {
380 Reader.Diag(diag::warn_cmdline_missing_macro_defs);
381 MissingDefines = true;
382 }
383
384 // Show the definition of this macro within the PCH file.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000385 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
386 FindMacro(Buffers, Missing);
387 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
388 SourceLocation PCHMissingLoc =
389 SourceMgr.getLocForStartOfFile(MacroLoc.first)
390 .getFileLocWithOffset(MacroLoc.second);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000391 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
392 }
Mike Stump1eb44332009-09-09 15:08:12 +0000393
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000394 if (ConflictingDefines)
395 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000396
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000397 // Determine what predefines were introduced based on command-line
398 // parameters that were not present when building the PCH
399 // file. Extra #defines are okay, so long as the identifiers being
400 // defined were not used within the precompiled header.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000401 std::vector<llvm::StringRef> ExtraPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000402 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
403 PCHLines.begin(), PCHLines.end(),
Mike Stump1eb44332009-09-09 15:08:12 +0000404 std::back_inserter(ExtraPredefines));
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000405 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000406 llvm::StringRef &Extra = ExtraPredefines[I];
407 if (!Extra.startswith("#define ")) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000408 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
409 return true;
410 }
411
412 // This is an extra macro definition. Determine the name of the
413 // macro we're defining.
414 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000415 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000416 = Extra.find_first_of("( \n\r", StartOfMacroName);
417 assert(EndOfMacroName != std::string::npos &&
418 "Couldn't find the end of the macro name");
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000419 llvm::StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000420
421 // Check whether this name was used somewhere in the PCH file. If
422 // so, defining it as a macro could change behavior, so we reject
423 // the PCH file.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000424 if (IdentifierInfo *II = Reader.get(MacroName)) {
Daniel Dunbar4fda42e2009-11-11 00:52:00 +0000425 Reader.Diag(diag::warn_macro_name_used_in_pch) << II;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000426 return true;
427 }
428
429 // Add this definition to the suggested predefines buffer.
430 SuggestedPredefines += Extra;
431 SuggestedPredefines += '\n';
432 }
433
434 // If we get here, it's because the predefines buffer had compatible
435 // contents. Accept the PCH file.
436 return false;
437}
438
Douglas Gregor12fab312010-03-16 16:35:32 +0000439void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
440 unsigned ID) {
441 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
442 ++NumHeaderInfos;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000443}
444
445void PCHValidator::ReadCounter(unsigned Value) {
446 PP.setCounterValue(Value);
447}
448
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000449//===----------------------------------------------------------------------===//
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000450// AST reader implementation
Douglas Gregor668c1a42009-04-21 22:25:48 +0000451//===----------------------------------------------------------------------===//
452
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000453void
Sebastian Redl571db7f2010-08-18 23:56:56 +0000454ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000455 DeserializationListener = Listener;
456 if (DeserializationListener)
457 DeserializationListener->SetReader(this);
458}
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 Gregor668c1a42009-04-21 22:25:48 +0000606 static std::pair<unsigned, unsigned>
607 ReadKeyDataLength(const unsigned char*& d) {
608 using namespace clang::io;
Douglas Gregor5f8e3302009-04-25 20:26:24 +0000609 unsigned DataLen = ReadUnalignedLE16(d);
Douglas Gregord6595a42009-04-25 21:04:17 +0000610 unsigned KeyLen = ReadUnalignedLE16(d);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000611 return std::make_pair(KeyLen, DataLen);
612 }
Mike Stump1eb44332009-09-09 15:08:12 +0000613
Douglas Gregor668c1a42009-04-21 22:25:48 +0000614 static std::pair<const char*, unsigned>
615 ReadKey(const unsigned char* d, unsigned n) {
616 assert(n >= 2 && d[n-1] == '\0');
617 return std::make_pair((const char*) d, n-1);
618 }
Mike Stump1eb44332009-09-09 15:08:12 +0000619
620 IdentifierInfo *ReadData(const internal_key_type& k,
Douglas Gregor668c1a42009-04-21 22:25:48 +0000621 const unsigned char* d,
622 unsigned DataLen) {
623 using namespace clang::io;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000624 IdentID ID = ReadUnalignedLE32(d);
Douglas Gregora92193e2009-04-28 21:18:29 +0000625 bool IsInteresting = ID & 0x01;
626
627 // Wipe out the "is interesting" bit.
628 ID = ID >> 1;
629
630 if (!IsInteresting) {
Sebastian Redl083abdf2010-07-27 23:01:28 +0000631 // For uninteresting identifiers, just build the IdentifierInfo
Douglas Gregora92193e2009-04-28 21:18:29 +0000632 // and associate it with the persistent ID.
633 IdentifierInfo *II = KnownII;
634 if (!II)
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000635 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregora92193e2009-04-28 21:18:29 +0000636 Reader.SetIdentifierInfo(ID, II);
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000637 II->setIsFromAST();
Douglas Gregora92193e2009-04-28 21:18:29 +0000638 return II;
639 }
640
Douglas Gregor5998da52009-04-28 21:32:13 +0000641 unsigned Bits = ReadUnalignedLE16(d);
Douglas Gregor2deaea32009-04-22 18:49:13 +0000642 bool CPlusPlusOperatorKeyword = Bits & 0x01;
643 Bits >>= 1;
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000644 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
645 Bits >>= 1;
Douglas Gregor2deaea32009-04-22 18:49:13 +0000646 bool Poisoned = Bits & 0x01;
647 Bits >>= 1;
648 bool ExtensionToken = Bits & 0x01;
649 Bits >>= 1;
650 bool hasMacroDefinition = Bits & 0x01;
651 Bits >>= 1;
652 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
653 Bits >>= 10;
Mike Stump1eb44332009-09-09 15:08:12 +0000654
Douglas Gregor2deaea32009-04-22 18:49:13 +0000655 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregor5998da52009-04-28 21:32:13 +0000656 DataLen -= 6;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000657
658 // Build the IdentifierInfo itself and link the identifier ID with
659 // the new IdentifierInfo.
660 IdentifierInfo *II = KnownII;
661 if (!II)
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000662 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000663 Reader.SetIdentifierInfo(ID, II);
664
Douglas Gregor2deaea32009-04-22 18:49:13 +0000665 // Set or check the various bits in the IdentifierInfo structure.
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000666 // Token IDs are read-only.
667 if (HasRevertedTokenIDToIdentifier)
668 II->RevertTokenIDToIdentifier();
Douglas Gregor2deaea32009-04-22 18:49:13 +0000669 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
Mike Stump1eb44332009-09-09 15:08:12 +0000670 assert(II->isExtensionToken() == ExtensionToken &&
Douglas Gregor2deaea32009-04-22 18:49:13 +0000671 "Incorrect extension token flag");
672 (void)ExtensionToken;
673 II->setIsPoisoned(Poisoned);
674 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
675 "Incorrect C++ operator keyword flag");
676 (void)CPlusPlusOperatorKeyword;
677
Douglas Gregor37e26842009-04-21 23:56:24 +0000678 // If this identifier is a macro, deserialize the macro
679 // definition.
680 if (hasMacroDefinition) {
Douglas Gregor5998da52009-04-28 21:32:13 +0000681 uint32_t Offset = ReadUnalignedLE32(d);
Sebastian Redlc3632732010-10-05 15:59:54 +0000682 Reader.ReadMacroRecord(F, Offset);
Douglas Gregor5998da52009-04-28 21:32:13 +0000683 DataLen -= 4;
Douglas Gregor37e26842009-04-21 23:56:24 +0000684 }
Douglas Gregor668c1a42009-04-21 22:25:48 +0000685
686 // Read all of the declarations visible at global scope with this
687 // name.
Chris Lattner6bf690f2009-04-27 22:17:41 +0000688 if (Reader.getContext() == 0) return II;
Douglas Gregord89275b2009-07-06 18:54:52 +0000689 if (DataLen > 0) {
690 llvm::SmallVector<uint32_t, 4> DeclIDs;
691 for (; DataLen > 0; DataLen -= 4)
692 DeclIDs.push_back(ReadUnalignedLE32(d));
693 Reader.SetGloballyVisibleDecls(II, DeclIDs);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000694 }
Mike Stump1eb44332009-09-09 15:08:12 +0000695
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000696 II->setIsFromAST();
Douglas Gregor668c1a42009-04-21 22:25:48 +0000697 return II;
698 }
699};
Mike Stump1eb44332009-09-09 15:08:12 +0000700
701} // end anonymous namespace
Douglas Gregor668c1a42009-04-21 22:25:48 +0000702
703/// \brief The on-disk hash table used to contain information about
704/// all of the identifiers in the program.
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000705typedef OnDiskChainedHashTable<ASTIdentifierLookupTrait>
706 ASTIdentifierLookupTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000707
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000708namespace {
709class ASTDeclContextNameLookupTrait {
710 ASTReader &Reader;
711
712public:
713 /// \brief Pair of begin/end iterators for DeclIDs.
714 typedef std::pair<DeclID *, DeclID *> data_type;
715
716 /// \brief Special internal key for declaration names.
717 /// The hash table creates keys for comparison; we do not create
718 /// a DeclarationName for the internal key to avoid deserializing types.
719 struct DeclNameKey {
720 DeclarationName::NameKind Kind;
721 uint64_t Data;
722 DeclNameKey() : Kind((DeclarationName::NameKind)0), Data(0) { }
723 };
724
725 typedef DeclarationName external_key_type;
726 typedef DeclNameKey internal_key_type;
727
728 explicit ASTDeclContextNameLookupTrait(ASTReader &Reader) : Reader(Reader) { }
729
730 static bool EqualKey(const internal_key_type& a,
731 const internal_key_type& b) {
732 return a.Kind == b.Kind && a.Data == b.Data;
733 }
734
735 unsigned ComputeHash(const DeclNameKey &Key) const {
736 llvm::FoldingSetNodeID ID;
737 ID.AddInteger(Key.Kind);
738
739 switch (Key.Kind) {
740 case DeclarationName::Identifier:
741 case DeclarationName::CXXLiteralOperatorName:
742 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
743 break;
744 case DeclarationName::ObjCZeroArgSelector:
745 case DeclarationName::ObjCOneArgSelector:
746 case DeclarationName::ObjCMultiArgSelector:
747 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
748 break;
749 case DeclarationName::CXXConstructorName:
750 case DeclarationName::CXXDestructorName:
751 case DeclarationName::CXXConversionFunctionName:
752 ID.AddInteger((TypeID)Key.Data);
753 break;
754 case DeclarationName::CXXOperatorName:
755 ID.AddInteger((OverloadedOperatorKind)Key.Data);
756 break;
757 case DeclarationName::CXXUsingDirective:
758 break;
759 }
760
761 return ID.ComputeHash();
762 }
763
764 internal_key_type GetInternalKey(const external_key_type& Name) const {
765 DeclNameKey Key;
766 Key.Kind = Name.getNameKind();
767 switch (Name.getNameKind()) {
768 case DeclarationName::Identifier:
769 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
770 break;
771 case DeclarationName::ObjCZeroArgSelector:
772 case DeclarationName::ObjCOneArgSelector:
773 case DeclarationName::ObjCMultiArgSelector:
774 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
775 break;
776 case DeclarationName::CXXConstructorName:
777 case DeclarationName::CXXDestructorName:
778 case DeclarationName::CXXConversionFunctionName:
779 Key.Data = Reader.GetTypeID(Name.getCXXNameType());
780 break;
781 case DeclarationName::CXXOperatorName:
782 Key.Data = Name.getCXXOverloadedOperator();
783 break;
784 case DeclarationName::CXXLiteralOperatorName:
785 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
786 break;
787 case DeclarationName::CXXUsingDirective:
788 break;
789 }
790
791 return Key;
792 }
793
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +0000794 external_key_type GetExternalKey(const internal_key_type& Key) const {
795 ASTContext *Context = Reader.getContext();
796 switch (Key.Kind) {
797 case DeclarationName::Identifier:
798 return DeclarationName((IdentifierInfo*)Key.Data);
799
800 case DeclarationName::ObjCZeroArgSelector:
801 case DeclarationName::ObjCOneArgSelector:
802 case DeclarationName::ObjCMultiArgSelector:
803 return DeclarationName(Selector(Key.Data));
804
805 case DeclarationName::CXXConstructorName:
806 return Context->DeclarationNames.getCXXConstructorName(
807 Context->getCanonicalType(Reader.GetType(Key.Data)));
808
809 case DeclarationName::CXXDestructorName:
810 return Context->DeclarationNames.getCXXDestructorName(
811 Context->getCanonicalType(Reader.GetType(Key.Data)));
812
813 case DeclarationName::CXXConversionFunctionName:
814 return Context->DeclarationNames.getCXXConversionFunctionName(
815 Context->getCanonicalType(Reader.GetType(Key.Data)));
816
817 case DeclarationName::CXXOperatorName:
818 return Context->DeclarationNames.getCXXOperatorName(
819 (OverloadedOperatorKind)Key.Data);
820
821 case DeclarationName::CXXLiteralOperatorName:
822 return Context->DeclarationNames.getCXXLiteralOperatorName(
823 (IdentifierInfo*)Key.Data);
824
825 case DeclarationName::CXXUsingDirective:
826 return DeclarationName::getUsingDirectiveName();
827 }
828
829 llvm_unreachable("Invalid Name Kind ?");
830 }
831
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000832 static std::pair<unsigned, unsigned>
833 ReadKeyDataLength(const unsigned char*& d) {
834 using namespace clang::io;
835 unsigned KeyLen = ReadUnalignedLE16(d);
836 unsigned DataLen = ReadUnalignedLE16(d);
837 return std::make_pair(KeyLen, DataLen);
838 }
839
840 internal_key_type ReadKey(const unsigned char* d, unsigned) {
841 using namespace clang::io;
842
843 DeclNameKey Key;
844 Key.Kind = (DeclarationName::NameKind)*d++;
845 switch (Key.Kind) {
846 case DeclarationName::Identifier:
847 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
848 break;
849 case DeclarationName::ObjCZeroArgSelector:
850 case DeclarationName::ObjCOneArgSelector:
851 case DeclarationName::ObjCMultiArgSelector:
852 Key.Data =
853 (uint64_t)Reader.DecodeSelector(ReadUnalignedLE32(d)).getAsOpaquePtr();
854 break;
855 case DeclarationName::CXXConstructorName:
856 case DeclarationName::CXXDestructorName:
857 case DeclarationName::CXXConversionFunctionName:
858 Key.Data = ReadUnalignedLE32(d); // TypeID
859 break;
860 case DeclarationName::CXXOperatorName:
861 Key.Data = *d++; // OverloadedOperatorKind
862 break;
863 case DeclarationName::CXXLiteralOperatorName:
864 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
865 break;
866 case DeclarationName::CXXUsingDirective:
867 break;
868 }
869
870 return Key;
871 }
872
873 data_type ReadData(internal_key_type, const unsigned char* d,
874 unsigned DataLen) {
875 using namespace clang::io;
876 unsigned NumDecls = ReadUnalignedLE16(d);
877 DeclID *Start = (DeclID *)d;
878 return std::make_pair(Start, Start + NumDecls);
879 }
880};
881
882} // end anonymous namespace
883
884/// \brief The on-disk hash table used for the DeclContext's Name lookup table.
885typedef OnDiskChainedHashTable<ASTDeclContextNameLookupTrait>
886 ASTDeclContextNameLookupTable;
887
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000888bool ASTReader::ReadDeclContextStorage(llvm::BitstreamCursor &Cursor,
889 const std::pair<uint64_t, uint64_t> &Offsets,
890 DeclContextInfo &Info) {
891 SavedStreamPosition SavedPosition(Cursor);
892 // First the lexical decls.
893 if (Offsets.first != 0) {
894 Cursor.JumpToBit(Offsets.first);
895
896 RecordData Record;
897 const char *Blob;
898 unsigned BlobLen;
899 unsigned Code = Cursor.ReadCode();
900 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
901 if (RecCode != DECL_CONTEXT_LEXICAL) {
902 Error("Expected lexical block");
903 return true;
904 }
905
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000906 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob);
907 Info.NumLexicalDecls = BlobLen / sizeof(KindDeclIDPair);
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000908 } else {
909 Info.LexicalDecls = 0;
910 Info.NumLexicalDecls = 0;
911 }
912
913 // Now the lookup table.
914 if (Offsets.second != 0) {
915 Cursor.JumpToBit(Offsets.second);
916
917 RecordData Record;
918 const char *Blob;
919 unsigned BlobLen;
920 unsigned Code = Cursor.ReadCode();
921 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
922 if (RecCode != DECL_CONTEXT_VISIBLE) {
923 Error("Expected visible lookup table block");
924 return true;
925 }
926 Info.NameLookupTableData
927 = ASTDeclContextNameLookupTable::Create(
928 (const unsigned char *)Blob + Record[0],
929 (const unsigned char *)Blob,
930 ASTDeclContextNameLookupTrait(*this));
Sebastian Redl0ea8f7f2010-08-24 00:50:00 +0000931 } else {
932 Info.NameLookupTableData = 0;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000933 }
934
935 return false;
936}
937
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000938void ASTReader::Error(const char *Msg) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000939 Diag(diag::err_fe_pch_malformed) << Msg;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000940}
941
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000942/// \brief Tell the AST listener about the predefines buffers in the chain.
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000943bool ASTReader::CheckPredefinesBuffers() {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000944 if (Listener)
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000945 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000946 ActualOriginalFileName,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000947 SuggestedPredefines);
Douglas Gregore721f952009-04-28 18:58:38 +0000948 return false;
Douglas Gregore1d918e2009-04-10 23:10:45 +0000949}
950
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000951//===----------------------------------------------------------------------===//
952// Source Manager Deserialization
953//===----------------------------------------------------------------------===//
954
Douglas Gregorbd945002009-04-13 16:31:14 +0000955/// \brief Read the line table in the source manager block.
Sebastian Redlc3632732010-10-05 15:59:54 +0000956/// \returns true if there was an error.
957bool ASTReader::ParseLineTable(PerFileData &F,
958 llvm::SmallVectorImpl<uint64_t> &Record) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000959 unsigned Idx = 0;
960 LineTableInfo &LineTable = SourceMgr.getLineTable();
961
962 // Parse the file names
Douglas Gregorff0a9872009-04-13 17:12:42 +0000963 std::map<int, int> FileIDs;
964 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000965 // Extract the file name
966 unsigned FilenameLen = Record[Idx++];
967 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
968 Idx += FilenameLen;
Douglas Gregore650c8c2009-07-07 00:12:59 +0000969 MaybeAddSystemRootToFilename(Filename);
Mike Stump1eb44332009-09-09 15:08:12 +0000970 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
Douglas Gregorff0a9872009-04-13 17:12:42 +0000971 Filename.size());
Douglas Gregorbd945002009-04-13 16:31:14 +0000972 }
973
974 // Parse the line entries
975 std::vector<LineEntry> Entries;
976 while (Idx < Record.size()) {
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000977 int FID = Record[Idx++];
Douglas Gregorbd945002009-04-13 16:31:14 +0000978
979 // Extract the line entries
980 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000981 assert(NumEntries && "Numentries is 00000");
Douglas Gregorbd945002009-04-13 16:31:14 +0000982 Entries.clear();
983 Entries.reserve(NumEntries);
984 for (unsigned I = 0; I != NumEntries; ++I) {
985 unsigned FileOffset = Record[Idx++];
986 unsigned LineNo = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000987 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump1eb44332009-09-09 15:08:12 +0000988 SrcMgr::CharacteristicKind FileKind
Douglas Gregorbd945002009-04-13 16:31:14 +0000989 = (SrcMgr::CharacteristicKind)Record[Idx++];
990 unsigned IncludeOffset = Record[Idx++];
991 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
992 FileKind, IncludeOffset));
993 }
994 LineTable.AddEntry(FID, Entries);
995 }
996
997 return false;
998}
999
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001000namespace {
1001
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001002class ASTStatData {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001003public:
1004 const bool hasStat;
1005 const ino_t ino;
1006 const dev_t dev;
1007 const mode_t mode;
1008 const time_t mtime;
1009 const off_t size;
Mike Stump1eb44332009-09-09 15:08:12 +00001010
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001011 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Mike Stump1eb44332009-09-09 15:08:12 +00001012 : hasStat(true), ino(i), dev(d), mode(mo), mtime(m), size(s) {}
1013
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001014 ASTStatData()
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001015 : hasStat(false), ino(0), dev(0), mode(0), mtime(0), size(0) {}
1016};
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
1050 if (*d++ == 1)
1051 return data_type();
1052
1053 ino_t ino = (ino_t) ReadUnalignedLE32(d);
1054 dev_t dev = (dev_t) ReadUnalignedLE32(d);
1055 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump1eb44332009-09-09 15:08:12 +00001056 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001057 off_t size = (off_t) ReadUnalignedLE64(d);
1058 return data_type(ino, dev, mode, mtime, size);
1059 }
1060};
1061
1062/// \brief stat() cache for precompiled headers.
1063///
1064/// This cache is very similar to the stat cache used by pretokenized
1065/// headers.
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001066class ASTStatCache : public StatSysCallCache {
1067 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001068 CacheTy *Cache;
1069
1070 unsigned &NumStatHits, &NumStatMisses;
Mike Stump1eb44332009-09-09 15:08:12 +00001071public:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001072 ASTStatCache(const unsigned char *Buckets,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001073 const unsigned char *Base,
1074 unsigned &NumStatHits,
Mike Stump1eb44332009-09-09 15:08:12 +00001075 unsigned &NumStatMisses)
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001076 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
1077 Cache = CacheTy::Create(Buckets, Base);
1078 }
1079
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001080 ~ASTStatCache() { delete Cache; }
Mike Stump1eb44332009-09-09 15:08:12 +00001081
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001082 int stat(const char *path, struct stat *buf) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001083 // Do the lookup for the file's data in the AST file.
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001084 CacheTy::iterator I = Cache->find(path);
1085
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001086 // If we don't get a hit in the AST file just forward to 'stat'.
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001087 if (I == Cache->end()) {
1088 ++NumStatMisses;
Douglas Gregor52e71082009-10-16 18:18:30 +00001089 return StatSysCallCache::stat(path, buf);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001090 }
Mike Stump1eb44332009-09-09 15:08:12 +00001091
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001092 ++NumStatHits;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001093 ASTStatData Data = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001094
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001095 if (!Data.hasStat)
1096 return 1;
1097
1098 buf->st_ino = Data.ino;
1099 buf->st_dev = Data.dev;
1100 buf->st_mtime = Data.mtime;
1101 buf->st_mode = Data.mode;
1102 buf->st_size = Data.size;
1103 return 0;
1104 }
1105};
1106} // end anonymous namespace
1107
1108
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001109/// \brief Read a source manager block
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001110ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(PerFileData &F) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001111 using namespace SrcMgr;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001112
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001113 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001114
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001115 // Set the source-location entry cursor to the current position in
1116 // the stream. This cursor will be used to read the contents of the
1117 // source manager block initially, and then lazily read
1118 // source-location entries as needed.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001119 SLocEntryCursor = F.Stream;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001120
1121 // The stream itself is going to skip over the source manager block.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001122 if (F.Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001123 Error("malformed block record in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001124 return Failure;
1125 }
1126
1127 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001128 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001129 Error("malformed source manager block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001130 return Failure;
1131 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001132
Douglas Gregor14f79002009-04-10 03:52:48 +00001133 RecordData Record;
1134 while (true) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001135 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregor14f79002009-04-10 03:52:48 +00001136 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001137 if (SLocEntryCursor.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001138 Error("error at end of Source Manager block in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001139 return Failure;
1140 }
Douglas Gregore1d918e2009-04-10 23:10:45 +00001141 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +00001142 }
Mike Stump1eb44332009-09-09 15:08:12 +00001143
Douglas Gregor14f79002009-04-10 03:52:48 +00001144 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1145 // No known subblocks, always skip them.
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001146 SLocEntryCursor.ReadSubBlockID();
1147 if (SLocEntryCursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001148 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001149 return Failure;
1150 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001151 continue;
1152 }
Mike Stump1eb44332009-09-09 15:08:12 +00001153
Douglas Gregor14f79002009-04-10 03:52:48 +00001154 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001155 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregor14f79002009-04-10 03:52:48 +00001156 continue;
1157 }
Mike Stump1eb44332009-09-09 15:08:12 +00001158
Douglas Gregor14f79002009-04-10 03:52:48 +00001159 // Read a record.
1160 const char *BlobStart;
1161 unsigned BlobLen;
1162 Record.clear();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001163 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001164 default: // Default behavior: ignore.
1165 break;
1166
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001167 case SM_LINE_TABLE:
Sebastian Redlc3632732010-10-05 15:59:54 +00001168 if (ParseLineTable(F, Record))
Douglas Gregorbd945002009-04-13 16:31:14 +00001169 return Failure;
Chris Lattner2c78b872009-04-14 23:22:57 +00001170 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001171
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001172 case SM_SLOC_FILE_ENTRY:
1173 case SM_SLOC_BUFFER_ENTRY:
1174 case SM_SLOC_INSTANTIATION_ENTRY:
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001175 // Once we hit one of the source location entries, we're done.
1176 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +00001177 }
1178 }
1179}
1180
Sebastian Redl190faf72010-07-20 21:50:20 +00001181/// \brief Get a cursor that's correctly positioned for reading the source
1182/// location entry with the given ID.
Sebastian Redlc3632732010-10-05 15:59:54 +00001183ASTReader::PerFileData *ASTReader::SLocCursorForID(unsigned ID) {
Sebastian Redl190faf72010-07-20 21:50:20 +00001184 assert(ID != 0 && ID <= TotalNumSLocEntries &&
1185 "SLocCursorForID should only be called for real IDs.");
1186
1187 ID -= 1;
1188 PerFileData *F = 0;
1189 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1190 F = Chain[N - I - 1];
1191 if (ID < F->LocalNumSLocEntries)
1192 break;
1193 ID -= F->LocalNumSLocEntries;
1194 }
1195 assert(F && F->LocalNumSLocEntries > ID && "Chain corrupted");
1196
1197 F->SLocEntryCursor.JumpToBit(F->SLocOffsets[ID]);
Sebastian Redlc3632732010-10-05 15:59:54 +00001198 return F;
Sebastian Redl190faf72010-07-20 21:50:20 +00001199}
1200
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001201/// \brief Read in the source location entry with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001202ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(unsigned ID) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001203 if (ID == 0)
1204 return Success;
1205
1206 if (ID > TotalNumSLocEntries) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001207 Error("source location entry ID out-of-range for AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001208 return Failure;
1209 }
1210
Sebastian Redlc3632732010-10-05 15:59:54 +00001211 PerFileData *F = SLocCursorForID(ID);
1212 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001213
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001214 ++NumSLocEntriesRead;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001215 unsigned Code = SLocEntryCursor.ReadCode();
1216 if (Code == llvm::bitc::END_BLOCK ||
1217 Code == llvm::bitc::ENTER_SUBBLOCK ||
1218 Code == llvm::bitc::DEFINE_ABBREV) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001219 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001220 return Failure;
1221 }
1222
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001223 RecordData Record;
1224 const char *BlobStart;
1225 unsigned BlobLen;
1226 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1227 default:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001228 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001229 return Failure;
1230
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001231 case SM_SLOC_FILE_ENTRY: {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001232 std::string Filename(BlobStart, BlobStart + BlobLen);
1233 MaybeAddSystemRootToFilename(Filename);
1234 const FileEntry *File = FileMgr.getFile(Filename);
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001235 if (File == 0) {
1236 std::string ErrorStr = "could not find file '";
Douglas Gregore650c8c2009-07-07 00:12:59 +00001237 ErrorStr += Filename;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001238 ErrorStr += "' referenced by AST file";
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001239 Error(ErrorStr.c_str());
1240 return Failure;
1241 }
Mike Stump1eb44332009-09-09 15:08:12 +00001242
Douglas Gregor2d52be52010-03-21 22:49:54 +00001243 if (Record.size() < 10) {
Ted Kremenek1857f622010-03-18 21:23:05 +00001244 Error("source location entry is incorrect");
1245 return Failure;
1246 }
1247
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001248 if (!DisableValidation &&
1249 ((off_t)Record[4] != File->getSize()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001250#if !defined(LLVM_ON_WIN32)
1251 // In our regression testing, the Windows file system seems to
1252 // have inconsistent modification times that sometimes
1253 // erroneously trigger this error-handling path.
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001254 || (time_t)Record[5] != File->getModificationTime()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001255#endif
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001256 )) {
Douglas Gregor2d52be52010-03-21 22:49:54 +00001257 Diag(diag::err_fe_pch_file_modified)
1258 << Filename;
1259 return Failure;
1260 }
1261
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001262 FileID FID = SourceMgr.createFileID(File,
Sebastian Redlc3632732010-10-05 15:59:54 +00001263 ReadSourceLocation(*F, Record[1]),
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001264 (SrcMgr::CharacteristicKind)Record[2],
1265 ID, Record[0]);
1266 if (Record[3])
1267 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile())
1268 .setHasLineDirectives();
1269
Douglas Gregor12fab312010-03-16 16:35:32 +00001270 // Reconstruct header-search information for this file.
1271 HeaderFileInfo HFI;
Douglas Gregor2d52be52010-03-21 22:49:54 +00001272 HFI.isImport = Record[6];
1273 HFI.DirInfo = Record[7];
1274 HFI.NumIncludes = Record[8];
1275 HFI.ControllingMacroID = Record[9];
Douglas Gregor12fab312010-03-16 16:35:32 +00001276 if (Listener)
1277 Listener->ReadHeaderFileInfo(HFI, File->getUID());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001278 break;
1279 }
1280
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001281 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001282 const char *Name = BlobStart;
1283 unsigned Offset = Record[0];
1284 unsigned Code = SLocEntryCursor.ReadCode();
1285 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001286 unsigned RecCode
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001287 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001288
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001289 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001290 Error("AST record has invalid code");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001291 return Failure;
1292 }
1293
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001294 llvm::MemoryBuffer *Buffer
Chris Lattnera0a270c2010-04-05 22:42:27 +00001295 = llvm::MemoryBuffer::getMemBuffer(llvm::StringRef(BlobStart, BlobLen - 1),
1296 Name);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001297 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID, Offset);
Mike Stump1eb44332009-09-09 15:08:12 +00001298
Douglas Gregor92b059e2009-04-28 20:33:11 +00001299 if (strcmp(Name, "<built-in>") == 0) {
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +00001300 PCHPredefinesBlock Block = {
1301 BufferID,
1302 llvm::StringRef(BlobStart, BlobLen - 1)
1303 };
1304 PCHPredefinesBuffers.push_back(Block);
Douglas Gregor92b059e2009-04-28 20:33:11 +00001305 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001306
1307 break;
1308 }
1309
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001310 case SM_SLOC_INSTANTIATION_ENTRY: {
Sebastian Redlc3632732010-10-05 15:59:54 +00001311 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001312 SourceMgr.createInstantiationLoc(SpellingLoc,
Sebastian Redlc3632732010-10-05 15:59:54 +00001313 ReadSourceLocation(*F, Record[2]),
1314 ReadSourceLocation(*F, Record[3]),
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001315 Record[4],
1316 ID,
1317 Record[0]);
1318 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001319 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001320 }
1321
1322 return Success;
1323}
1324
Chris Lattner6367f6d2009-04-27 01:05:14 +00001325/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1326/// specified cursor. Read the abbreviations that are at the top of the block
1327/// and then leave the cursor pointing into the block.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001328bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattner6367f6d2009-04-27 01:05:14 +00001329 unsigned BlockID) {
1330 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001331 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001332 return Failure;
1333 }
Mike Stump1eb44332009-09-09 15:08:12 +00001334
Chris Lattner6367f6d2009-04-27 01:05:14 +00001335 while (true) {
1336 unsigned Code = Cursor.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00001337
Chris Lattner6367f6d2009-04-27 01:05:14 +00001338 // We expect all abbrevs to be at the start of the block.
1339 if (Code != llvm::bitc::DEFINE_ABBREV)
1340 return false;
1341 Cursor.ReadAbbrevRecord();
1342 }
1343}
1344
Sebastian Redlc3632732010-10-05 15:59:54 +00001345void ASTReader::ReadMacroRecord(PerFileData &F, uint64_t Offset) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001346 assert(PP && "Forgot to set Preprocessor ?");
Sebastian Redlc3632732010-10-05 15:59:54 +00001347 llvm::BitstreamCursor &Stream = F.Stream;
Mike Stump1eb44332009-09-09 15:08:12 +00001348
Douglas Gregor37e26842009-04-21 23:56:24 +00001349 // Keep track of where we are in the stream, then jump back there
1350 // after reading this macro.
1351 SavedStreamPosition SavedPosition(Stream);
1352
1353 Stream.JumpToBit(Offset);
1354 RecordData Record;
1355 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1356 MacroInfo *Macro = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001357
Douglas Gregor37e26842009-04-21 23:56:24 +00001358 while (true) {
1359 unsigned Code = Stream.ReadCode();
1360 switch (Code) {
1361 case llvm::bitc::END_BLOCK:
1362 return;
1363
1364 case llvm::bitc::ENTER_SUBBLOCK:
1365 // No known subblocks, always skip them.
1366 Stream.ReadSubBlockID();
1367 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001368 Error("malformed block record in AST file");
Douglas Gregor37e26842009-04-21 23:56:24 +00001369 return;
1370 }
1371 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001372
Douglas Gregor37e26842009-04-21 23:56:24 +00001373 case llvm::bitc::DEFINE_ABBREV:
1374 Stream.ReadAbbrevRecord();
1375 continue;
1376 default: break;
1377 }
1378
1379 // Read a record.
1380 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001381 PreprocessorRecordTypes RecType =
1382 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
Douglas Gregor37e26842009-04-21 23:56:24 +00001383 switch (RecType) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001384 case PP_MACRO_OBJECT_LIKE:
1385 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001386 // If we already have a macro, that means that we've hit the end
1387 // of the definition of the macro we were looking for. We're
1388 // done.
1389 if (Macro)
1390 return;
1391
1392 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1393 if (II == 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001394 Error("macro must have a name in AST file");
Douglas Gregor37e26842009-04-21 23:56:24 +00001395 return;
1396 }
Sebastian Redlc3632732010-10-05 15:59:54 +00001397 SourceLocation Loc = ReadSourceLocation(F, Record[1]);
Douglas Gregor37e26842009-04-21 23:56:24 +00001398 bool isUsed = Record[2];
Mike Stump1eb44332009-09-09 15:08:12 +00001399
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001400 MacroInfo *MI = PP->AllocateMacroInfo(Loc);
Douglas Gregor37e26842009-04-21 23:56:24 +00001401 MI->setIsUsed(isUsed);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001402 MI->setIsFromAST();
Mike Stump1eb44332009-09-09 15:08:12 +00001403
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001404 unsigned NextIndex = 3;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001405 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregor37e26842009-04-21 23:56:24 +00001406 // Decode function-like macro info.
1407 bool isC99VarArgs = Record[3];
1408 bool isGNUVarArgs = Record[4];
1409 MacroArgs.clear();
1410 unsigned NumArgs = Record[5];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001411 NextIndex = 6 + NumArgs;
Douglas Gregor37e26842009-04-21 23:56:24 +00001412 for (unsigned i = 0; i != NumArgs; ++i)
1413 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1414
1415 // Install function-like macro info.
1416 MI->setIsFunctionLike();
1417 if (isC99VarArgs) MI->setIsC99Varargs();
1418 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor75fdb232009-05-22 22:45:36 +00001419 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001420 PP->getPreprocessorAllocator());
Douglas Gregor37e26842009-04-21 23:56:24 +00001421 }
1422
1423 // Finally, install the macro.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001424 PP->setMacroInfo(II, MI);
Douglas Gregor37e26842009-04-21 23:56:24 +00001425
1426 // Remember that we saw this macro last so that we add the tokens that
1427 // form its body to it.
1428 Macro = MI;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001429
1430 if (NextIndex + 1 == Record.size() && PP->getPreprocessingRecord()) {
1431 // We have a macro definition. Load it now.
1432 PP->getPreprocessingRecord()->RegisterMacroDefinition(Macro,
1433 getMacroDefinition(Record[NextIndex]));
1434 }
1435
Douglas Gregor37e26842009-04-21 23:56:24 +00001436 ++NumMacrosRead;
1437 break;
1438 }
Mike Stump1eb44332009-09-09 15:08:12 +00001439
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001440 case PP_TOKEN: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001441 // If we see a TOKEN before a PP_MACRO_*, then the file is
1442 // erroneous, just pretend we didn't see this.
1443 if (Macro == 0) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001444
Douglas Gregor37e26842009-04-21 23:56:24 +00001445 Token Tok;
1446 Tok.startToken();
Sebastian Redlc3632732010-10-05 15:59:54 +00001447 Tok.setLocation(ReadSourceLocation(F, Record[0]));
Douglas Gregor37e26842009-04-21 23:56:24 +00001448 Tok.setLength(Record[1]);
1449 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1450 Tok.setIdentifierInfo(II);
1451 Tok.setKind((tok::TokenKind)Record[3]);
1452 Tok.setFlag((Token::TokenFlags)Record[4]);
1453 Macro->AddTokenToBody(Tok);
1454 break;
1455 }
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001456
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001457 case PP_MACRO_INSTANTIATION: {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001458 // If we already have a macro, that means that we've hit the end
1459 // of the definition of the macro we were looking for. We're
1460 // done.
1461 if (Macro)
1462 return;
1463
1464 if (!PP->getPreprocessingRecord()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001465 Error("missing preprocessing record in AST file");
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001466 return;
1467 }
1468
1469 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1470 if (PPRec.getPreprocessedEntity(Record[0]))
1471 return;
1472
1473 MacroInstantiation *MI
1474 = new (PPRec) MacroInstantiation(DecodeIdentifierInfo(Record[3]),
Sebastian Redlc3632732010-10-05 15:59:54 +00001475 SourceRange(ReadSourceLocation(F, Record[1]),
1476 ReadSourceLocation(F, Record[2])),
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001477 getMacroDefinition(Record[4]));
1478 PPRec.SetPreallocatedEntity(Record[0], MI);
1479 return;
1480 }
1481
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001482 case PP_MACRO_DEFINITION: {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001483 // If we already have a macro, that means that we've hit the end
1484 // of the definition of the macro we were looking for. We're
1485 // done.
1486 if (Macro)
1487 return;
1488
1489 if (!PP->getPreprocessingRecord()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001490 Error("missing preprocessing record in AST file");
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001491 return;
1492 }
1493
1494 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1495 if (PPRec.getPreprocessedEntity(Record[0]))
1496 return;
1497
Douglas Gregor77424bc2010-10-02 19:29:26 +00001498 if (Record[1] > MacroDefinitionsLoaded.size()) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001499 Error("out-of-bounds macro definition record");
1500 return;
1501 }
1502
Douglas Gregor77424bc2010-10-02 19:29:26 +00001503 // Decode the identifier info and then check again; if the macro is
1504 // still defined and associated with the identifier,
1505 IdentifierInfo *II = DecodeIdentifierInfo(Record[4]);
1506 if (!MacroDefinitionsLoaded[Record[1] - 1]) {
1507 MacroDefinition *MD
1508 = new (PPRec) MacroDefinition(II,
Sebastian Redlc3632732010-10-05 15:59:54 +00001509 ReadSourceLocation(F, Record[5]),
Douglas Gregorb1a7d9a2010-10-01 20:33:34 +00001510 SourceRange(
Sebastian Redlc3632732010-10-05 15:59:54 +00001511 ReadSourceLocation(F, Record[2]),
1512 ReadSourceLocation(F, Record[3])));
Douglas Gregor77424bc2010-10-02 19:29:26 +00001513
1514 PPRec.SetPreallocatedEntity(Record[0], MD);
1515 MacroDefinitionsLoaded[Record[1] - 1] = MD;
1516
1517 if (DeserializationListener)
1518 DeserializationListener->MacroDefinitionRead(Record[1], MD);
1519 }
1520
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001521 return;
1522 }
Sebastian Redlb57a6242010-09-27 22:18:47 +00001523 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001524 }
1525}
1526
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001527void ASTReader::ReadDefinedMacros() {
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001528 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redlc3632732010-10-05 15:59:54 +00001529 PerFileData &F = *Chain[N - I - 1];
1530 llvm::BitstreamCursor &MacroCursor = F.MacroCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001531
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001532 // If there was no preprocessor block, skip this file.
1533 if (!MacroCursor.getBitStreamReader())
1534 continue;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001535
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001536 llvm::BitstreamCursor Cursor = MacroCursor;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001537 if (Cursor.EnterSubBlock(PREPROCESSOR_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001538 Error("malformed preprocessor block record in AST file");
Douglas Gregor88a35862010-01-04 19:18:44 +00001539 return;
1540 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001541
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001542 RecordData Record;
1543 while (true) {
Sebastian Redledadecc2010-09-28 02:55:49 +00001544 uint64_t Offset = Cursor.GetCurrentBitNo();
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001545 unsigned Code = Cursor.ReadCode();
1546 if (Code == llvm::bitc::END_BLOCK) {
1547 if (Cursor.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001548 Error("error at end of preprocessor block in AST file");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001549 return;
1550 }
1551 break;
Douglas Gregor88a35862010-01-04 19:18:44 +00001552 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001553
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001554 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1555 // No known subblocks, always skip them.
1556 Cursor.ReadSubBlockID();
1557 if (Cursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001558 Error("malformed block record in AST file");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001559 return;
1560 }
1561 continue;
1562 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001563
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001564 if (Code == llvm::bitc::DEFINE_ABBREV) {
1565 Cursor.ReadAbbrevRecord();
1566 continue;
1567 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001568
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001569 // Read a record.
1570 const char *BlobStart;
1571 unsigned BlobLen;
1572 Record.clear();
1573 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1574 default: // Default behavior: ignore.
1575 break;
Douglas Gregor88a35862010-01-04 19:18:44 +00001576
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001577 case PP_MACRO_OBJECT_LIKE:
1578 case PP_MACRO_FUNCTION_LIKE:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001579 DecodeIdentifierInfo(Record[0]);
1580 break;
1581
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001582 case PP_TOKEN:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001583 // Ignore tokens.
1584 break;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001585
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001586 case PP_MACRO_INSTANTIATION:
1587 case PP_MACRO_DEFINITION:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001588 // Read the macro record.
Sebastian Redledadecc2010-09-28 02:55:49 +00001589 // FIXME: That's a stupid way to do this. We should reuse this cursor.
Sebastian Redlc3632732010-10-05 15:59:54 +00001590 ReadMacroRecord(F, Offset);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001591 break;
1592 }
Douglas Gregor88a35862010-01-04 19:18:44 +00001593 }
1594 }
1595}
1596
Sebastian Redlf73c93f2010-09-15 19:54:06 +00001597MacroDefinition *ASTReader::getMacroDefinition(MacroID ID) {
Douglas Gregor77424bc2010-10-02 19:29:26 +00001598 if (ID == 0 || ID > MacroDefinitionsLoaded.size())
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001599 return 0;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001600
Douglas Gregor77424bc2010-10-02 19:29:26 +00001601 if (!MacroDefinitionsLoaded[ID - 1]) {
1602 unsigned Index = ID - 1;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001603 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1604 PerFileData &F = *Chain[N - I - 1];
1605 if (Index < F.LocalNumMacroDefinitions) {
Sebastian Redlc3632732010-10-05 15:59:54 +00001606 ReadMacroRecord(F, F.MacroDefinitionOffsets[Index]);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001607 break;
1608 }
1609 Index -= F.LocalNumMacroDefinitions;
1610 }
Douglas Gregor77424bc2010-10-02 19:29:26 +00001611 assert(MacroDefinitionsLoaded[ID - 1] && "Broken chain");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001612 }
1613
Douglas Gregor77424bc2010-10-02 19:29:26 +00001614 return MacroDefinitionsLoaded[ID - 1];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001615}
1616
Douglas Gregore650c8c2009-07-07 00:12:59 +00001617/// \brief If we are loading a relocatable PCH file, and the filename is
1618/// not an absolute path, add the system root to the beginning of the file
1619/// name.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001620void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001621 // If this is not a relocatable PCH file, there's nothing to do.
1622 if (!RelocatablePCH)
1623 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001624
Daniel Dunbard5b21972009-11-18 19:50:41 +00001625 if (Filename.empty() || llvm::sys::Path(Filename).isAbsolute())
Douglas Gregore650c8c2009-07-07 00:12:59 +00001626 return;
1627
Douglas Gregore650c8c2009-07-07 00:12:59 +00001628 if (isysroot == 0) {
1629 // If no system root was given, default to '/'
1630 Filename.insert(Filename.begin(), '/');
1631 return;
1632 }
Mike Stump1eb44332009-09-09 15:08:12 +00001633
Douglas Gregore650c8c2009-07-07 00:12:59 +00001634 unsigned Length = strlen(isysroot);
1635 if (isysroot[Length - 1] != '/')
1636 Filename.insert(Filename.begin(), '/');
Mike Stump1eb44332009-09-09 15:08:12 +00001637
Douglas Gregore650c8c2009-07-07 00:12:59 +00001638 Filename.insert(Filename.begin(), isysroot, isysroot + Length);
1639}
1640
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001641ASTReader::ASTReadResult
Sebastian Redl571db7f2010-08-18 23:56:56 +00001642ASTReader::ReadASTBlock(PerFileData &F) {
Sebastian Redl9137a522010-07-16 17:50:48 +00001643 llvm::BitstreamCursor &Stream = F.Stream;
1644
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001645 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001646 Error("malformed block record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001647 return Failure;
1648 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001649
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001650 // Read all of the records and blocks for the ASt file.
Douglas Gregor8038d512009-04-10 17:25:41 +00001651 RecordData Record;
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001652 bool First = true;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001653 while (!Stream.AtEndOfStream()) {
1654 unsigned Code = Stream.ReadCode();
1655 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001656 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001657 Error("error at end of module block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001658 return Failure;
1659 }
Chris Lattner7356a312009-04-11 21:15:38 +00001660
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001661 return Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001662 }
1663
1664 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1665 switch (Stream.ReadSubBlockID()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001666 case DECLTYPES_BLOCK_ID:
Chris Lattner6367f6d2009-04-27 01:05:14 +00001667 // We lazily load the decls block, but we want to set up the
1668 // DeclsCursor cursor to point into it. Clone our current bitcode
1669 // cursor to it, enter the block and read the abbrevs in that block.
1670 // With the main cursor, we just skip over it.
Sebastian Redl9137a522010-07-16 17:50:48 +00001671 F.DeclsCursor = Stream;
Chris Lattner6367f6d2009-04-27 01:05:14 +00001672 if (Stream.SkipBlock() || // Skip with the main cursor.
1673 // Read the abbrevs.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001674 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001675 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001676 return Failure;
1677 }
1678 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001679
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001680 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl9137a522010-07-16 17:50:48 +00001681 F.MacroCursor = Stream;
Douglas Gregor88a35862010-01-04 19:18:44 +00001682 if (PP)
1683 PP->setExternalSource(this);
1684
Chris Lattner7356a312009-04-11 21:15:38 +00001685 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001686 Error("malformed block record in AST file");
Chris Lattner7356a312009-04-11 21:15:38 +00001687 return Failure;
1688 }
1689 break;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00001690
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001691 case SOURCE_MANAGER_BLOCK_ID:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001692 switch (ReadSourceManagerBlock(F)) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00001693 case Success:
1694 break;
1695
1696 case Failure:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001697 Error("malformed source manager block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001698 return Failure;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001699
1700 case IgnorePCH:
1701 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001702 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001703 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001704 }
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001705 First = false;
Douglas Gregor8038d512009-04-10 17:25:41 +00001706 continue;
1707 }
1708
1709 if (Code == llvm::bitc::DEFINE_ABBREV) {
1710 Stream.ReadAbbrevRecord();
1711 continue;
1712 }
1713
1714 // Read and process a record.
1715 Record.clear();
Douglas Gregor2bec0412009-04-10 21:16:55 +00001716 const char *BlobStart = 0;
1717 unsigned BlobLen = 0;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001718 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00001719 &BlobStart, &BlobLen)) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001720 default: // Default behavior: ignore.
1721 break;
1722
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001723 case METADATA: {
1724 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1725 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001726 : diag::warn_pch_version_too_new);
1727 return IgnorePCH;
1728 }
1729
1730 RelocatablePCH = Record[4];
1731 if (Listener) {
1732 std::string TargetTriple(BlobStart, BlobLen);
1733 if (Listener->ReadTargetTriple(TargetTriple))
1734 return IgnorePCH;
1735 }
1736 break;
1737 }
1738
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001739 case CHAINED_METADATA: {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001740 if (!First) {
1741 Error("CHAINED_METADATA is not first record in block");
1742 return Failure;
1743 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001744 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1745 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001746 : diag::warn_pch_version_too_new);
1747 return IgnorePCH;
1748 }
1749
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00001750 // Load the chained file, which is always a PCH file.
1751 switch(ReadASTCore(llvm::StringRef(BlobStart, BlobLen), PCH)) {
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001752 case Failure: return Failure;
1753 // If we have to ignore the dependency, we'll have to ignore this too.
1754 case IgnorePCH: return IgnorePCH;
1755 case Success: break;
1756 }
1757 break;
1758 }
1759
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001760 case TYPE_OFFSET:
Sebastian Redl12d6da02010-07-19 22:06:55 +00001761 if (F.LocalNumTypes != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001762 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001763 return Failure;
1764 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00001765 F.TypeOffsets = (const uint32_t *)BlobStart;
1766 F.LocalNumTypes = Record[0];
Douglas Gregor8038d512009-04-10 17:25:41 +00001767 break;
1768
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001769 case DECL_OFFSET:
Sebastian Redl12d6da02010-07-19 22:06:55 +00001770 if (F.LocalNumDecls != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001771 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001772 return Failure;
1773 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00001774 F.DeclOffsets = (const uint32_t *)BlobStart;
1775 F.LocalNumDecls = Record[0];
Douglas Gregor8038d512009-04-10 17:25:41 +00001776 break;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001777
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001778 case TU_UPDATE_LEXICAL: {
Sebastian Redld692af72010-07-27 18:24:41 +00001779 DeclContextInfo Info = {
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001780 /* No visible information */ 0,
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001781 reinterpret_cast<const KindDeclIDPair *>(BlobStart),
1782 BlobLen / sizeof(KindDeclIDPair)
Sebastian Redld692af72010-07-27 18:24:41 +00001783 };
Douglas Gregor3747ee72010-10-01 01:18:02 +00001784 DeclContextOffsets[Context ? Context->getTranslationUnitDecl() : 0]
1785 .push_back(Info);
Sebastian Redld692af72010-07-27 18:24:41 +00001786 break;
1787 }
1788
Sebastian Redle1dde812010-08-24 00:50:04 +00001789 case UPDATE_VISIBLE: {
1790 serialization::DeclID ID = Record[0];
1791 void *Table = ASTDeclContextNameLookupTable::Create(
1792 (const unsigned char *)BlobStart + Record[1],
1793 (const unsigned char *)BlobStart,
1794 ASTDeclContextNameLookupTrait(*this));
Douglas Gregor3747ee72010-10-01 01:18:02 +00001795 if (ID == 1 && Context) { // Is it the TU?
Sebastian Redle1dde812010-08-24 00:50:04 +00001796 DeclContextInfo Info = {
1797 Table, /* No lexical inforamtion */ 0, 0
1798 };
1799 DeclContextOffsets[Context->getTranslationUnitDecl()].push_back(Info);
1800 } else
1801 PendingVisibleUpdates[ID].push_back(Table);
1802 break;
1803 }
1804
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001805 case REDECLS_UPDATE_LATEST: {
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00001806 assert(Record.size() % 2 == 0 && "Expected pairs of DeclIDs");
1807 for (unsigned i = 0, e = Record.size(); i < e; i += 2) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001808 DeclID First = Record[i], Latest = Record[i+1];
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00001809 assert((FirstLatestDeclIDs.find(First) == FirstLatestDeclIDs.end() ||
1810 Latest > FirstLatestDeclIDs[First]) &&
1811 "The new latest is supposed to come after the previous latest");
1812 FirstLatestDeclIDs[First] = Latest;
1813 }
1814 break;
1815 }
1816
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001817 case LANGUAGE_OPTIONS:
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001818 if (ParseLanguageOptions(Record) && !DisableValidation)
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001819 return IgnorePCH;
1820 break;
Douglas Gregor2bec0412009-04-10 21:16:55 +00001821
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001822 case IDENTIFIER_TABLE:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001823 F.IdentifierTableData = BlobStart;
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001824 if (Record[0]) {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001825 F.IdentifierLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001826 = ASTIdentifierLookupTable::Create(
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001827 (const unsigned char *)F.IdentifierTableData + Record[0],
1828 (const unsigned char *)F.IdentifierTableData,
Sebastian Redlc3632732010-10-05 15:59:54 +00001829 ASTIdentifierLookupTrait(*this, F));
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001830 if (PP)
1831 PP->getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001832 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001833 break;
1834
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001835 case IDENTIFIER_OFFSET:
Sebastian Redl2da08f92010-07-19 22:28:42 +00001836 if (F.LocalNumIdentifiers != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001837 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00001838 return Failure;
1839 }
Sebastian Redl2da08f92010-07-19 22:28:42 +00001840 F.IdentifierOffsets = (const uint32_t *)BlobStart;
1841 F.LocalNumIdentifiers = Record[0];
Douglas Gregorafaf3082009-04-11 00:14:32 +00001842 break;
Douglas Gregorfdd01722009-04-14 00:24:19 +00001843
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001844 case EXTERNAL_DEFINITIONS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001845 // Optimization for the first block.
1846 if (ExternalDefinitions.empty())
1847 ExternalDefinitions.swap(Record);
1848 else
1849 ExternalDefinitions.insert(ExternalDefinitions.end(),
1850 Record.begin(), Record.end());
Douglas Gregorfdd01722009-04-14 00:24:19 +00001851 break;
Douglas Gregor3e1af842009-04-17 22:13:46 +00001852
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001853 case SPECIAL_TYPES:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001854 // Optimization for the first block
1855 if (SpecialTypes.empty())
1856 SpecialTypes.swap(Record);
1857 else
1858 SpecialTypes.insert(SpecialTypes.end(), Record.begin(), Record.end());
Douglas Gregorad1de002009-04-18 05:55:16 +00001859 break;
1860
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001861 case STATISTICS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001862 TotalNumStatements += Record[0];
1863 TotalNumMacros += Record[1];
1864 TotalLexicalDeclContexts += Record[2];
1865 TotalVisibleDeclContexts += Record[3];
Douglas Gregor3e1af842009-04-17 22:13:46 +00001866 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001867
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001868 case TENTATIVE_DEFINITIONS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001869 // Optimization for the first block.
1870 if (TentativeDefinitions.empty())
1871 TentativeDefinitions.swap(Record);
1872 else
1873 TentativeDefinitions.insert(TentativeDefinitions.end(),
1874 Record.begin(), Record.end());
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00001875 break;
Douglas Gregor14c22f22009-04-22 22:18:58 +00001876
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001877 case UNUSED_FILESCOPED_DECLS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001878 // Optimization for the first block.
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00001879 if (UnusedFileScopedDecls.empty())
1880 UnusedFileScopedDecls.swap(Record);
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001881 else
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00001882 UnusedFileScopedDecls.insert(UnusedFileScopedDecls.end(),
1883 Record.begin(), Record.end());
Tanya Lattnere6bbc012010-02-12 00:07:30 +00001884 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001885
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001886 case WEAK_UNDECLARED_IDENTIFIERS:
Sebastian Redl40566802010-08-05 18:21:25 +00001887 // Later blocks overwrite earlier ones.
1888 WeakUndeclaredIdentifiers.swap(Record);
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00001889 break;
1890
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001891 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001892 // Optimization for the first block.
1893 if (LocallyScopedExternalDecls.empty())
1894 LocallyScopedExternalDecls.swap(Record);
1895 else
1896 LocallyScopedExternalDecls.insert(LocallyScopedExternalDecls.end(),
1897 Record.begin(), Record.end());
Douglas Gregor14c22f22009-04-22 22:18:58 +00001898 break;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001899
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001900 case SELECTOR_OFFSETS:
Sebastian Redl059612d2010-08-03 21:58:15 +00001901 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redl725cd962010-08-04 20:40:17 +00001902 F.LocalNumSelectors = Record[0];
Douglas Gregor83941df2009-04-25 17:48:32 +00001903 break;
1904
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001905 case METHOD_POOL:
Sebastian Redl725cd962010-08-04 20:40:17 +00001906 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor83941df2009-04-25 17:48:32 +00001907 if (Record[0])
Sebastian Redl725cd962010-08-04 20:40:17 +00001908 F.SelectorLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001909 = ASTSelectorLookupTable::Create(
Sebastian Redl725cd962010-08-04 20:40:17 +00001910 F.SelectorLookupTableData + Record[0],
1911 F.SelectorLookupTableData,
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001912 ASTSelectorLookupTrait(*this));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00001913 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001914 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001915
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00001916 case REFERENCED_SELECTOR_POOL:
Sebastian Redlc3632732010-10-05 15:59:54 +00001917 F.ReferencedSelectorsData.swap(Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00001918 break;
1919
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001920 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001921 if (!Record.empty() && Listener)
1922 Listener->ReadCounter(Record[0]);
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001923 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001924
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001925 case SOURCE_LOCATION_OFFSETS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001926 F.SLocOffsets = (const uint32_t *)BlobStart;
1927 F.LocalNumSLocEntries = Record[0];
Sebastian Redl8db9fae2010-09-22 20:19:08 +00001928 F.LocalSLocSize = Record[1];
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001929 break;
1930
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001931 case SOURCE_LOCATION_PRELOADS:
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00001932 if (PreloadSLocEntries.empty())
1933 PreloadSLocEntries.swap(Record);
1934 else
1935 PreloadSLocEntries.insert(PreloadSLocEntries.end(),
1936 Record.begin(), Record.end());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001937 break;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001938
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001939 case STAT_CACHE: {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001940 ASTStatCache *MyStatCache =
1941 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
Douglas Gregor52e71082009-10-16 18:18:30 +00001942 (const unsigned char *)BlobStart,
1943 NumStatHits, NumStatMisses);
1944 FileMgr.addStatCache(MyStatCache);
Sebastian Redl9137a522010-07-16 17:50:48 +00001945 F.StatCache = MyStatCache;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001946 break;
Douglas Gregor52e71082009-10-16 18:18:30 +00001947 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001948
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001949 case EXT_VECTOR_DECLS:
Sebastian Redla9f23682010-07-28 21:38:49 +00001950 // Optimization for the first block.
1951 if (ExtVectorDecls.empty())
1952 ExtVectorDecls.swap(Record);
1953 else
1954 ExtVectorDecls.insert(ExtVectorDecls.end(),
1955 Record.begin(), Record.end());
Douglas Gregorb81c1702009-04-27 20:06:05 +00001956 break;
1957
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001958 case VTABLE_USES:
Sebastian Redl40566802010-08-05 18:21:25 +00001959 // Later tables overwrite earlier ones.
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00001960 VTableUses.swap(Record);
1961 break;
1962
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001963 case DYNAMIC_CLASSES:
Sebastian Redl40566802010-08-05 18:21:25 +00001964 // Optimization for the first block.
1965 if (DynamicClasses.empty())
1966 DynamicClasses.swap(Record);
1967 else
1968 DynamicClasses.insert(DynamicClasses.end(),
1969 Record.begin(), Record.end());
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00001970 break;
1971
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001972 case PENDING_IMPLICIT_INSTANTIATIONS:
Sebastian Redlc3632732010-10-05 15:59:54 +00001973 F.PendingInstantiations.swap(Record);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00001974 break;
1975
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001976 case SEMA_DECL_REFS:
Sebastian Redl40566802010-08-05 18:21:25 +00001977 // Later tables overwrite earlier ones.
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00001978 SemaDeclRefs.swap(Record);
1979 break;
1980
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001981 case ORIGINAL_FILE_NAME:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001982 // The primary AST will be the last to get here, so it will be the one
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001983 // that's used.
Daniel Dunbar7b5a1212009-11-11 05:29:04 +00001984 ActualOriginalFileName.assign(BlobStart, BlobLen);
1985 OriginalFileName = ActualOriginalFileName;
Douglas Gregore650c8c2009-07-07 00:12:59 +00001986 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001987 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001988
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001989 case VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek974be4d2010-02-12 23:31:14 +00001990 const std::string &CurBranch = getClangFullRepositoryVersion();
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001991 llvm::StringRef ASTBranch(BlobStart, BlobLen);
1992 if (llvm::StringRef(CurBranch) != ASTBranch && !DisableValidation) {
1993 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregor445e23e2009-10-05 21:07:28 +00001994 return IgnorePCH;
1995 }
1996 break;
1997 }
Sebastian Redl04e6fd42010-07-21 20:07:32 +00001998
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001999 case MACRO_DEFINITION_OFFSETS:
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002000 F.MacroDefinitionOffsets = (const uint32_t *)BlobStart;
2001 F.NumPreallocatedPreprocessingEntities = Record[0];
2002 F.LocalNumMacroDefinitions = Record[1];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002003 break;
Sebastian Redl0b17c612010-08-13 00:28:03 +00002004
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002005 case DECL_REPLACEMENTS: {
Sebastian Redl0b17c612010-08-13 00:28:03 +00002006 if (Record.size() % 2 != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002007 Error("invalid DECL_REPLACEMENTS block in AST file");
Sebastian Redl0b17c612010-08-13 00:28:03 +00002008 return Failure;
2009 }
2010 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002011 ReplacedDecls[static_cast<DeclID>(Record[I])] =
Sebastian Redl0b17c612010-08-13 00:28:03 +00002012 std::make_pair(&F, Record[I+1]);
2013 break;
2014 }
Sebastian Redl6e50e002010-08-24 22:50:19 +00002015
2016 case ADDITIONAL_TEMPLATE_SPECIALIZATIONS: {
2017 AdditionalTemplateSpecializations &ATS =
2018 AdditionalTemplateSpecializationsPending[Record[0]];
2019 ATS.insert(ATS.end(), Record.begin()+1, Record.end());
2020 break;
2021 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002022 }
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002023 First = false;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002024 }
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002025 Error("premature end of bitstream in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002026 return Failure;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002027}
2028
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002029ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
2030 ASTFileType Type) {
2031 switch(ReadASTCore(FileName, Type)) {
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002032 case Failure: return Failure;
2033 case IgnorePCH: return IgnorePCH;
2034 case Success: break;
2035 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002036
2037 // Here comes stuff that we only do once the entire chain is loaded.
2038
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002039 // Allocate space for loaded slocentries, identifiers, decls and types.
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002040 unsigned TotalNumIdentifiers = 0, TotalNumTypes = 0, TotalNumDecls = 0,
Sebastian Redl725cd962010-08-04 20:40:17 +00002041 TotalNumPreallocatedPreprocessingEntities = 0, TotalNumMacroDefs = 0,
2042 TotalNumSelectors = 0;
Sebastian Redl12d6da02010-07-19 22:06:55 +00002043 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002044 TotalNumSLocEntries += Chain[I]->LocalNumSLocEntries;
Sebastian Redl8db9fae2010-09-22 20:19:08 +00002045 NextSLocOffset += Chain[I]->LocalSLocSize;
Sebastian Redl2da08f92010-07-19 22:28:42 +00002046 TotalNumIdentifiers += Chain[I]->LocalNumIdentifiers;
Sebastian Redl12d6da02010-07-19 22:06:55 +00002047 TotalNumTypes += Chain[I]->LocalNumTypes;
2048 TotalNumDecls += Chain[I]->LocalNumDecls;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002049 TotalNumPreallocatedPreprocessingEntities +=
2050 Chain[I]->NumPreallocatedPreprocessingEntities;
2051 TotalNumMacroDefs += Chain[I]->LocalNumMacroDefinitions;
Sebastian Redl725cd962010-08-04 20:40:17 +00002052 TotalNumSelectors += Chain[I]->LocalNumSelectors;
Sebastian Redl12d6da02010-07-19 22:06:55 +00002053 }
Sebastian Redl8db9fae2010-09-22 20:19:08 +00002054 SourceMgr.PreallocateSLocEntries(this, TotalNumSLocEntries, NextSLocOffset);
Sebastian Redl2da08f92010-07-19 22:28:42 +00002055 IdentifiersLoaded.resize(TotalNumIdentifiers);
Sebastian Redl12d6da02010-07-19 22:06:55 +00002056 TypesLoaded.resize(TotalNumTypes);
2057 DeclsLoaded.resize(TotalNumDecls);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002058 MacroDefinitionsLoaded.resize(TotalNumMacroDefs);
2059 if (PP) {
2060 if (TotalNumIdentifiers > 0)
2061 PP->getHeaderSearchInfo().SetExternalLookup(this);
2062 if (TotalNumPreallocatedPreprocessingEntities > 0) {
2063 if (!PP->getPreprocessingRecord())
2064 PP->createPreprocessingRecord();
2065 PP->getPreprocessingRecord()->SetExternalSource(*this,
2066 TotalNumPreallocatedPreprocessingEntities);
2067 }
2068 }
Sebastian Redl725cd962010-08-04 20:40:17 +00002069 SelectorsLoaded.resize(TotalNumSelectors);
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002070 // Preload SLocEntries.
2071 for (unsigned I = 0, N = PreloadSLocEntries.size(); I != N; ++I) {
2072 ASTReadResult Result = ReadSLocEntryRecord(PreloadSLocEntries[I]);
2073 if (Result != Success)
2074 return Result;
2075 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00002076
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002077 // Check the predefines buffers.
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00002078 if (!DisableValidation && CheckPredefinesBuffers())
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002079 return IgnorePCH;
2080
2081 if (PP) {
2082 // Initialization of keywords and pragmas occurs before the
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002083 // AST file is read, so there may be some identifiers that were
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002084 // loaded into the IdentifierTable before we intercepted the
2085 // creation of identifiers. Iterate through the list of known
2086 // identifiers and determine whether we have to establish
2087 // preprocessor definitions or top-level identifier declaration
2088 // chains for those identifiers.
2089 //
2090 // We copy the IdentifierInfo pointers to a small vector first,
2091 // since de-serializing declarations or macro definitions can add
2092 // new entries into the identifier table, invalidating the
2093 // iterators.
2094 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
2095 for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(),
2096 IdEnd = PP->getIdentifierTable().end();
2097 Id != IdEnd; ++Id)
2098 Identifiers.push_back(Id->second);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002099 // We need to search the tables in all files.
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002100 for (unsigned J = 0, M = Chain.size(); J != M; ++J) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002101 ASTIdentifierLookupTable *IdTable
2102 = (ASTIdentifierLookupTable *)Chain[J]->IdentifierLookupTable;
2103 // Not all AST files necessarily have identifier tables, only the useful
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00002104 // ones.
2105 if (!IdTable)
2106 continue;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002107 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
2108 IdentifierInfo *II = Identifiers[I];
2109 // Look in the on-disk hash tables for an entry for this identifier
Sebastian Redlc3632732010-10-05 15:59:54 +00002110 ASTIdentifierLookupTrait Info(*this, *Chain[J], II);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002111 std::pair<const char*,unsigned> Key(II->getNameStart(),II->getLength());
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002112 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002113 if (Pos == IdTable->end())
2114 continue;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002115
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002116 // Dereferencing the iterator has the effect of populating the
2117 // IdentifierInfo node with the various declarations it needs.
2118 (void)*Pos;
2119 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002120 }
2121 }
2122
2123 if (Context)
2124 InitializeContext(*Context);
2125
2126 return Success;
2127}
2128
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002129ASTReader::ASTReadResult ASTReader::ReadASTCore(llvm::StringRef FileName,
2130 ASTFileType Type) {
Sebastian Redla866e652010-10-01 19:59:12 +00002131 PerFileData *Prev = Chain.empty() ? 0 : Chain.back();
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002132 Chain.push_back(new PerFileData(Type));
Sebastian Redl9137a522010-07-16 17:50:48 +00002133 PerFileData &F = *Chain.back();
Sebastian Redla866e652010-10-01 19:59:12 +00002134 if (Prev)
2135 Prev->NextInSource = &F;
2136 else
2137 FirstInSource = &F;
2138 F.Loaders.push_back(Prev);
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002139
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002140 // Set the AST file name.
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002141 F.FileName = FileName;
2142
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002143 // Open the AST file.
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002144 //
2145 // FIXME: This shouldn't be here, we should just take a raw_ostream.
2146 std::string ErrStr;
2147 F.Buffer.reset(llvm::MemoryBuffer::getFileOrSTDIN(FileName, &ErrStr));
2148 if (!F.Buffer) {
2149 Error(ErrStr.c_str());
2150 return IgnorePCH;
2151 }
2152
2153 // Initialize the stream
2154 F.StreamFile.init((const unsigned char *)F.Buffer->getBufferStart(),
2155 (const unsigned char *)F.Buffer->getBufferEnd());
Sebastian Redl9137a522010-07-16 17:50:48 +00002156 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002157 Stream.init(F.StreamFile);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002158 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002159
2160 // Sniff for the signature.
2161 if (Stream.Read(8) != 'C' ||
2162 Stream.Read(8) != 'P' ||
2163 Stream.Read(8) != 'C' ||
2164 Stream.Read(8) != 'H') {
2165 Diag(diag::err_not_a_pch_file) << FileName;
2166 return Failure;
2167 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002168
Douglas Gregor2cf26342009-04-09 22:27:44 +00002169 while (!Stream.AtEndOfStream()) {
2170 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00002171
Douglas Gregore1d918e2009-04-10 23:10:45 +00002172 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002173 Error("invalid record at top-level of AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002174 return Failure;
2175 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002176
2177 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregor668c1a42009-04-21 22:25:48 +00002178
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002179 // We only know the AST subblock ID.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002180 switch (BlockID) {
2181 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002182 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002183 Error("malformed BlockInfoBlock in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002184 return Failure;
2185 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002186 break;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002187 case AST_BLOCK_ID:
Sebastian Redl571db7f2010-08-18 23:56:56 +00002188 switch (ReadASTBlock(F)) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002189 case Success:
2190 break;
2191
2192 case Failure:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002193 return Failure;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002194
2195 case IgnorePCH:
Douglas Gregor2bec0412009-04-10 21:16:55 +00002196 // FIXME: We could consider reading through to the end of this
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002197 // AST block, skipping subblocks, to see if there are other
2198 // AST blocks elsewhere.
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002199
2200 // Clear out any preallocated source location entries, so that
2201 // the source manager does not try to resolve them later.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002202 SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002203
2204 // Remove the stat cache.
Sebastian Redl9137a522010-07-16 17:50:48 +00002205 if (F.StatCache)
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002206 FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002207
Douglas Gregore1d918e2009-04-10 23:10:45 +00002208 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002209 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002210 break;
2211 default:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002212 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002213 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002214 return Failure;
2215 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002216 break;
2217 }
Mike Stump1eb44332009-09-09 15:08:12 +00002218 }
2219
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002220 return Success;
2221}
2222
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002223void ASTReader::setPreprocessor(Preprocessor &pp) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002224 PP = &pp;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002225
2226 unsigned TotalNum = 0;
2227 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
2228 TotalNum += Chain[I]->NumPreallocatedPreprocessingEntities;
2229 if (TotalNum) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002230 if (!PP->getPreprocessingRecord())
2231 PP->createPreprocessingRecord();
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002232 PP->getPreprocessingRecord()->SetExternalSource(*this, TotalNum);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002233 }
2234}
2235
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002236void ASTReader::InitializeContext(ASTContext &Ctx) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002237 Context = &Ctx;
2238 assert(Context && "Passed null context!");
2239
2240 assert(PP && "Forgot to set Preprocessor ?");
2241 PP->getIdentifierTable().setExternalIdentifierLookup(this);
2242 PP->getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregor88a35862010-01-04 19:18:44 +00002243 PP->setExternalSource(this);
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002244
Douglas Gregor3747ee72010-10-01 01:18:02 +00002245 // If we have an update block for the TU waiting, we have to add it before
2246 // deserializing the decl.
2247 DeclContextOffsetsMap::iterator DCU = DeclContextOffsets.find(0);
2248 if (DCU != DeclContextOffsets.end()) {
2249 // Insertion could invalidate map, so grab vector.
2250 DeclContextInfos T;
2251 T.swap(DCU->second);
2252 DeclContextOffsets.erase(DCU);
2253 DeclContextOffsets[Ctx.getTranslationUnitDecl()].swap(T);
2254 }
2255
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002256 // Load the translation unit declaration
Argyrios Kyrtzidis8871a442010-07-08 17:13:02 +00002257 GetTranslationUnitDecl();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002258
2259 // Load the special types.
2260 Context->setBuiltinVaListType(
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002261 GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST]));
2262 if (unsigned Id = SpecialTypes[SPECIAL_TYPE_OBJC_ID])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002263 Context->setObjCIdType(GetType(Id));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002264 if (unsigned Sel = SpecialTypes[SPECIAL_TYPE_OBJC_SELECTOR])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002265 Context->setObjCSelType(GetType(Sel));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002266 if (unsigned Proto = SpecialTypes[SPECIAL_TYPE_OBJC_PROTOCOL])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002267 Context->setObjCProtoType(GetType(Proto));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002268 if (unsigned Class = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002269 Context->setObjCClassType(GetType(Class));
Steve Naroff14108da2009-07-10 23:34:53 +00002270
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002271 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002272 Context->setCFConstantStringType(GetType(String));
Mike Stump1eb44332009-09-09 15:08:12 +00002273 if (unsigned FastEnum
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002274 = SpecialTypes[SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002275 Context->setObjCFastEnumerationStateType(GetType(FastEnum));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002276 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002277 QualType FileType = GetType(File);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002278 if (FileType.isNull()) {
2279 Error("FILE type is NULL");
2280 return;
2281 }
John McCall183700f2009-09-21 23:43:11 +00002282 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002283 Context->setFILEDecl(Typedef->getDecl());
2284 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002285 const TagType *Tag = FileType->getAs<TagType>();
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002286 if (!Tag) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002287 Error("Invalid FILE type in AST file");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002288 return;
2289 }
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002290 Context->setFILEDecl(Tag->getDecl());
2291 }
2292 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002293 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_jmp_buf]) {
Mike Stump782fa302009-07-28 02:25:19 +00002294 QualType Jmp_bufType = GetType(Jmp_buf);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002295 if (Jmp_bufType.isNull()) {
2296 Error("jmp_bug type is NULL");
2297 return;
2298 }
John McCall183700f2009-09-21 23:43:11 +00002299 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
Mike Stump782fa302009-07-28 02:25:19 +00002300 Context->setjmp_bufDecl(Typedef->getDecl());
2301 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002302 const TagType *Tag = Jmp_bufType->getAs<TagType>();
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002303 if (!Tag) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002304 Error("Invalid jmp_buf type in AST file");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002305 return;
2306 }
Mike Stump782fa302009-07-28 02:25:19 +00002307 Context->setjmp_bufDecl(Tag->getDecl());
2308 }
2309 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002310 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_sigjmp_buf]) {
Mike Stump782fa302009-07-28 02:25:19 +00002311 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002312 if (Sigjmp_bufType.isNull()) {
2313 Error("sigjmp_buf type is NULL");
2314 return;
2315 }
John McCall183700f2009-09-21 23:43:11 +00002316 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
Mike Stump782fa302009-07-28 02:25:19 +00002317 Context->setsigjmp_bufDecl(Typedef->getDecl());
2318 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002319 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002320 assert(Tag && "Invalid sigjmp_buf type in AST file");
Mike Stump782fa302009-07-28 02:25:19 +00002321 Context->setsigjmp_bufDecl(Tag->getDecl());
2322 }
2323 }
Mike Stump1eb44332009-09-09 15:08:12 +00002324 if (unsigned ObjCIdRedef
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002325 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION])
Douglas Gregord1571ac2009-08-21 00:27:50 +00002326 Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef);
Mike Stump1eb44332009-09-09 15:08:12 +00002327 if (unsigned ObjCClassRedef
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002328 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION])
Douglas Gregord1571ac2009-08-21 00:27:50 +00002329 Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002330 if (unsigned String = SpecialTypes[SPECIAL_TYPE_BLOCK_DESCRIPTOR])
Mike Stumpadaaad32009-10-20 02:12:22 +00002331 Context->setBlockDescriptorType(GetType(String));
Mike Stump083c25e2009-10-22 00:49:09 +00002332 if (unsigned String
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002333 = SpecialTypes[SPECIAL_TYPE_BLOCK_EXTENDED_DESCRIPTOR])
Mike Stump083c25e2009-10-22 00:49:09 +00002334 Context->setBlockDescriptorExtendedType(GetType(String));
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002335 if (unsigned ObjCSelRedef
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002336 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION])
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002337 Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002338 if (unsigned String = SpecialTypes[SPECIAL_TYPE_NS_CONSTANT_STRING])
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002339 Context->setNSConstantStringType(GetType(String));
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +00002340
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002341 if (SpecialTypes[SPECIAL_TYPE_INT128_INSTALLED])
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +00002342 Context->setInt128Installed();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002343}
2344
Douglas Gregorb64c1932009-05-12 01:31:05 +00002345/// \brief Retrieve the name of the original source file name
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002346/// directly from the AST file, without actually loading the AST
Douglas Gregorb64c1932009-05-12 01:31:05 +00002347/// file.
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002348std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Daniel Dunbar93ebb1b2009-12-03 09:13:06 +00002349 Diagnostic &Diags) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002350 // Open the AST file.
Douglas Gregorb64c1932009-05-12 01:31:05 +00002351 std::string ErrStr;
2352 llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002353 Buffer.reset(llvm::MemoryBuffer::getFile(ASTFileName.c_str(), &ErrStr));
Douglas Gregorb64c1932009-05-12 01:31:05 +00002354 if (!Buffer) {
Daniel Dunbar93ebb1b2009-12-03 09:13:06 +00002355 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002356 return std::string();
2357 }
2358
2359 // Initialize the stream
2360 llvm::BitstreamReader StreamFile;
2361 llvm::BitstreamCursor Stream;
Mike Stump1eb44332009-09-09 15:08:12 +00002362 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregorb64c1932009-05-12 01:31:05 +00002363 (const unsigned char *)Buffer->getBufferEnd());
2364 Stream.init(StreamFile);
2365
2366 // Sniff for the signature.
2367 if (Stream.Read(8) != 'C' ||
2368 Stream.Read(8) != 'P' ||
2369 Stream.Read(8) != 'C' ||
2370 Stream.Read(8) != 'H') {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002371 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002372 return std::string();
2373 }
2374
2375 RecordData Record;
2376 while (!Stream.AtEndOfStream()) {
2377 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00002378
Douglas Gregorb64c1932009-05-12 01:31:05 +00002379 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2380 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump1eb44332009-09-09 15:08:12 +00002381
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002382 // We only know the AST subblock ID.
Douglas Gregorb64c1932009-05-12 01:31:05 +00002383 switch (BlockID) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002384 case AST_BLOCK_ID:
2385 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002386 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002387 return std::string();
2388 }
2389 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002390
Douglas Gregorb64c1932009-05-12 01:31:05 +00002391 default:
2392 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002393 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002394 return std::string();
2395 }
2396 break;
2397 }
2398 continue;
2399 }
2400
2401 if (Code == llvm::bitc::END_BLOCK) {
2402 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002403 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002404 return std::string();
2405 }
2406 continue;
2407 }
2408
2409 if (Code == llvm::bitc::DEFINE_ABBREV) {
2410 Stream.ReadAbbrevRecord();
2411 continue;
2412 }
2413
2414 Record.clear();
2415 const char *BlobStart = 0;
2416 unsigned BlobLen = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002417 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002418 == ORIGINAL_FILE_NAME)
Douglas Gregorb64c1932009-05-12 01:31:05 +00002419 return std::string(BlobStart, BlobLen);
Mike Stump1eb44332009-09-09 15:08:12 +00002420 }
Douglas Gregorb64c1932009-05-12 01:31:05 +00002421
2422 return std::string();
2423}
2424
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002425/// \brief Parse the record that corresponds to a LangOptions data
2426/// structure.
2427///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002428/// This routine parses the language options from the AST file and then gives
2429/// them to the AST listener if one is set.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002430///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002431/// \returns true if the listener deems the file unacceptable, false otherwise.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002432bool ASTReader::ParseLanguageOptions(
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002433 const llvm::SmallVectorImpl<uint64_t> &Record) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002434 if (Listener) {
2435 LangOptions LangOpts;
Mike Stump1eb44332009-09-09 15:08:12 +00002436
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002437 #define PARSE_LANGOPT(Option) \
2438 LangOpts.Option = Record[Idx]; \
2439 ++Idx
Mike Stump1eb44332009-09-09 15:08:12 +00002440
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002441 unsigned Idx = 0;
2442 PARSE_LANGOPT(Trigraphs);
2443 PARSE_LANGOPT(BCPLComment);
2444 PARSE_LANGOPT(DollarIdents);
2445 PARSE_LANGOPT(AsmPreprocessor);
2446 PARSE_LANGOPT(GNUMode);
Chandler Carrutheb5d7b72010-04-17 20:17:31 +00002447 PARSE_LANGOPT(GNUKeywords);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002448 PARSE_LANGOPT(ImplicitInt);
2449 PARSE_LANGOPT(Digraphs);
2450 PARSE_LANGOPT(HexFloats);
2451 PARSE_LANGOPT(C99);
2452 PARSE_LANGOPT(Microsoft);
2453 PARSE_LANGOPT(CPlusPlus);
2454 PARSE_LANGOPT(CPlusPlus0x);
2455 PARSE_LANGOPT(CXXOperatorNames);
2456 PARSE_LANGOPT(ObjC1);
2457 PARSE_LANGOPT(ObjC2);
2458 PARSE_LANGOPT(ObjCNonFragileABI);
Fariborz Jahanian412e7982010-02-09 19:31:38 +00002459 PARSE_LANGOPT(ObjCNonFragileABI2);
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +00002460 PARSE_LANGOPT(NoConstantCFStrings);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002461 PARSE_LANGOPT(PascalStrings);
2462 PARSE_LANGOPT(WritableStrings);
2463 PARSE_LANGOPT(LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +00002464 PARSE_LANGOPT(AltiVec);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002465 PARSE_LANGOPT(Exceptions);
Daniel Dunbar73482882010-02-10 18:48:44 +00002466 PARSE_LANGOPT(SjLjExceptions);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002467 PARSE_LANGOPT(NeXTRuntime);
2468 PARSE_LANGOPT(Freestanding);
2469 PARSE_LANGOPT(NoBuiltin);
2470 PARSE_LANGOPT(ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +00002471 PARSE_LANGOPT(POSIXThreads);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002472 PARSE_LANGOPT(Blocks);
2473 PARSE_LANGOPT(EmitAllDecls);
2474 PARSE_LANGOPT(MathErrno);
Chris Lattnera4d71452010-06-26 21:25:03 +00002475 LangOpts.setSignedOverflowBehavior((LangOptions::SignedOverflowBehaviorTy)
2476 Record[Idx++]);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002477 PARSE_LANGOPT(HeinousExtensions);
2478 PARSE_LANGOPT(Optimize);
2479 PARSE_LANGOPT(OptimizeSize);
2480 PARSE_LANGOPT(Static);
2481 PARSE_LANGOPT(PICLevel);
2482 PARSE_LANGOPT(GNUInline);
2483 PARSE_LANGOPT(NoInline);
2484 PARSE_LANGOPT(AccessControl);
2485 PARSE_LANGOPT(CharIsSigned);
John Thompsona6fda122009-11-05 20:14:16 +00002486 PARSE_LANGOPT(ShortWChar);
Chris Lattnera4d71452010-06-26 21:25:03 +00002487 LangOpts.setGCMode((LangOptions::GCMode)Record[Idx++]);
2488 LangOpts.setVisibilityMode((LangOptions::VisibilityMode)Record[Idx++]);
Daniel Dunbarab8e2812009-09-21 04:16:19 +00002489 LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode)
Chris Lattnera4d71452010-06-26 21:25:03 +00002490 Record[Idx++]);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002491 PARSE_LANGOPT(InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +00002492 PARSE_LANGOPT(OpenCL);
Mike Stump9c276ae2009-12-12 01:27:46 +00002493 PARSE_LANGOPT(CatchUndefined);
2494 // FIXME: Missing ElideConstructors?!
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002495 #undef PARSE_LANGOPT
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002496
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002497 return Listener->ReadLanguageOptions(LangOpts);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002498 }
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002499
2500 return false;
2501}
2502
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002503void ASTReader::ReadPreprocessedEntities() {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002504 ReadDefinedMacros();
2505}
2506
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002507/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002508ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002509 PerFileData *F = 0;
2510 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2511 F = Chain[N - I - 1];
2512 if (Index < F->LocalNumTypes)
2513 break;
2514 Index -= F->LocalNumTypes;
2515 }
2516 assert(F && F->LocalNumTypes > Index && "Broken chain");
Sebastian Redlc3632732010-10-05 15:59:54 +00002517 return RecordLocation(F, F->TypeOffsets[Index]);
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002518}
2519
2520/// \brief Read and return the type with the given index..
Douglas Gregor2cf26342009-04-09 22:27:44 +00002521///
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002522/// The index is the type ID, shifted and minus the number of predefs. This
2523/// routine actually reads the record corresponding to the type at the given
2524/// location. It is a helper routine for GetType, which deals with reading type
2525/// IDs.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002526QualType ASTReader::ReadTypeRecord(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002527 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00002528 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00002529
Douglas Gregor0b748912009-04-14 21:18:50 +00002530 // Keep track of where we are in the stream, then jump back there
2531 // after reading this type.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002532 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregor0b748912009-04-14 21:18:50 +00002533
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00002534 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redl27372b42010-08-11 18:52:41 +00002535
Douglas Gregord89275b2009-07-06 18:54:52 +00002536 // Note that we are loading a type record.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00002537 Deserializing AType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00002538
Sebastian Redlc3632732010-10-05 15:59:54 +00002539 DeclsCursor.JumpToBit(Loc.Offset);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002540 RecordData Record;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002541 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002542 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
2543 case TYPE_EXT_QUAL: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002544 if (Record.size() != 2) {
2545 Error("Incorrect encoding of extended qualifier type");
2546 return QualType();
2547 }
Douglas Gregor6d473962009-04-15 22:00:08 +00002548 QualType Base = GetType(Record[0]);
John McCall0953e762009-09-24 19:53:00 +00002549 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[1]);
2550 return Context->getQualifiedType(Base, Quals);
Douglas Gregor6d473962009-04-15 22:00:08 +00002551 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002552
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002553 case TYPE_COMPLEX: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002554 if (Record.size() != 1) {
2555 Error("Incorrect encoding of complex type");
2556 return QualType();
2557 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002558 QualType ElemType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002559 return Context->getComplexType(ElemType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002560 }
2561
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002562 case TYPE_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002563 if (Record.size() != 1) {
2564 Error("Incorrect encoding of pointer type");
2565 return QualType();
2566 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002567 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002568 return Context->getPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002569 }
2570
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002571 case TYPE_BLOCK_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002572 if (Record.size() != 1) {
2573 Error("Incorrect encoding of block pointer type");
2574 return QualType();
2575 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002576 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002577 return Context->getBlockPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002578 }
2579
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002580 case TYPE_LVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002581 if (Record.size() != 1) {
2582 Error("Incorrect encoding of lvalue reference type");
2583 return QualType();
2584 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002585 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002586 return Context->getLValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002587 }
2588
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002589 case TYPE_RVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002590 if (Record.size() != 1) {
2591 Error("Incorrect encoding of rvalue reference type");
2592 return QualType();
2593 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002594 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002595 return Context->getRValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002596 }
2597
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002598 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidis240437b2010-07-02 11:55:15 +00002599 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002600 Error("Incorrect encoding of member pointer type");
2601 return QualType();
2602 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002603 QualType PointeeType = GetType(Record[0]);
2604 QualType ClassType = GetType(Record[1]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002605 return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002606 }
2607
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002608 case TYPE_CONSTANT_ARRAY: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002609 QualType ElementType = GetType(Record[0]);
2610 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2611 unsigned IndexTypeQuals = Record[2];
2612 unsigned Idx = 3;
2613 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002614 return Context->getConstantArrayType(ElementType, Size,
2615 ASM, IndexTypeQuals);
2616 }
2617
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002618 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002619 QualType ElementType = GetType(Record[0]);
2620 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2621 unsigned IndexTypeQuals = Record[2];
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002622 return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002623 }
2624
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002625 case TYPE_VARIABLE_ARRAY: {
Douglas Gregor0b748912009-04-14 21:18:50 +00002626 QualType ElementType = GetType(Record[0]);
2627 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2628 unsigned IndexTypeQuals = Record[2];
Sebastian Redlc3632732010-10-05 15:59:54 +00002629 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
2630 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
2631 return Context->getVariableArrayType(ElementType, ReadExpr(*Loc.F),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002632 ASM, IndexTypeQuals,
2633 SourceRange(LBLoc, RBLoc));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002634 }
2635
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002636 case TYPE_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00002637 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002638 Error("incorrect encoding of vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002639 return QualType();
2640 }
2641
2642 QualType ElementType = GetType(Record[0]);
2643 unsigned NumElements = Record[1];
Chris Lattner788b0fd2010-06-23 06:00:24 +00002644 unsigned AltiVecSpec = Record[2];
2645 return Context->getVectorType(ElementType, NumElements,
2646 (VectorType::AltiVecSpecific)AltiVecSpec);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002647 }
2648
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002649 case TYPE_EXT_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00002650 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002651 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002652 return QualType();
2653 }
2654
2655 QualType ElementType = GetType(Record[0]);
2656 unsigned NumElements = Record[1];
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002657 return Context->getExtVectorType(ElementType, NumElements);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002658 }
2659
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002660 case TYPE_FUNCTION_NO_PROTO: {
Rafael Espindola425ef722010-03-30 22:15:11 +00002661 if (Record.size() != 4) {
Douglas Gregora02b1472009-04-28 21:53:25 +00002662 Error("incorrect encoding of no-proto function type");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002663 return QualType();
2664 }
2665 QualType ResultType = GetType(Record[0]);
Rafael Espindola425ef722010-03-30 22:15:11 +00002666 FunctionType::ExtInfo Info(Record[1], Record[2], (CallingConv)Record[3]);
Rafael Espindola264ba482010-03-30 20:24:48 +00002667 return Context->getFunctionNoProtoType(ResultType, Info);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002668 }
2669
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002670 case TYPE_FUNCTION_PROTO: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002671 QualType ResultType = GetType(Record[0]);
Douglas Gregor91236662009-12-22 18:11:50 +00002672 bool NoReturn = Record[1];
Rafael Espindola425ef722010-03-30 22:15:11 +00002673 unsigned RegParm = Record[2];
2674 CallingConv CallConv = (CallingConv)Record[3];
2675 unsigned Idx = 4;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002676 unsigned NumParams = Record[Idx++];
2677 llvm::SmallVector<QualType, 16> ParamTypes;
2678 for (unsigned I = 0; I != NumParams; ++I)
2679 ParamTypes.push_back(GetType(Record[Idx++]));
2680 bool isVariadic = Record[Idx++];
2681 unsigned Quals = Record[Idx++];
Sebastian Redl465226e2009-05-27 22:11:52 +00002682 bool hasExceptionSpec = Record[Idx++];
2683 bool hasAnyExceptionSpec = Record[Idx++];
2684 unsigned NumExceptions = Record[Idx++];
2685 llvm::SmallVector<QualType, 2> Exceptions;
2686 for (unsigned I = 0; I != NumExceptions; ++I)
2687 Exceptions.push_back(GetType(Record[Idx++]));
Jay Foadbeaaccd2009-05-21 09:52:38 +00002688 return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams,
Sebastian Redl465226e2009-05-27 22:11:52 +00002689 isVariadic, Quals, hasExceptionSpec,
2690 hasAnyExceptionSpec, NumExceptions,
Rafael Espindola264ba482010-03-30 20:24:48 +00002691 Exceptions.data(),
Rafael Espindola425ef722010-03-30 22:15:11 +00002692 FunctionType::ExtInfo(NoReturn, RegParm,
2693 CallConv));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002694 }
2695
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002696 case TYPE_UNRESOLVED_USING:
John McCalled976492009-12-04 22:46:56 +00002697 return Context->getTypeDeclType(
2698 cast<UnresolvedUsingTypenameDecl>(GetDecl(Record[0])));
2699
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002700 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002701 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002702 Error("incorrect encoding of typedef type");
2703 return QualType();
2704 }
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002705 TypedefDecl *Decl = cast<TypedefDecl>(GetDecl(Record[0]));
2706 QualType Canonical = GetType(Record[1]);
2707 return Context->getTypedefType(Decl, Canonical);
2708 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002709
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002710 case TYPE_TYPEOF_EXPR:
Sebastian Redlc3632732010-10-05 15:59:54 +00002711 return Context->getTypeOfExprType(ReadExpr(*Loc.F));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002712
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002713 case TYPE_TYPEOF: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002714 if (Record.size() != 1) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002715 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002716 return QualType();
2717 }
2718 QualType UnderlyingType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002719 return Context->getTypeOfType(UnderlyingType);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002720 }
Mike Stump1eb44332009-09-09 15:08:12 +00002721
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002722 case TYPE_DECLTYPE:
Sebastian Redlc3632732010-10-05 15:59:54 +00002723 return Context->getDecltypeType(ReadExpr(*Loc.F));
Anders Carlsson395b4752009-06-24 19:06:50 +00002724
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002725 case TYPE_RECORD: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002726 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002727 Error("incorrect encoding of record type");
2728 return QualType();
2729 }
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002730 bool IsDependent = Record[0];
2731 QualType T = Context->getRecordType(cast<RecordDecl>(GetDecl(Record[1])));
2732 T->Dependent = IsDependent;
2733 return T;
2734 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002735
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002736 case TYPE_ENUM: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002737 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002738 Error("incorrect encoding of enum type");
2739 return QualType();
2740 }
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002741 bool IsDependent = Record[0];
2742 QualType T = Context->getEnumType(cast<EnumDecl>(GetDecl(Record[1])));
2743 T->Dependent = IsDependent;
2744 return T;
2745 }
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002746
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002747 case TYPE_ELABORATED: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00002748 unsigned Idx = 0;
2749 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2750 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2751 QualType NamedType = GetType(Record[Idx++]);
2752 return Context->getElaboratedType(Keyword, NNS, NamedType);
John McCall7da24312009-09-05 00:15:47 +00002753 }
2754
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002755 case TYPE_OBJC_INTERFACE: {
Chris Lattnerc6fa4452009-04-22 06:45:28 +00002756 unsigned Idx = 0;
2757 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
John McCallc12c5bb2010-05-15 11:32:37 +00002758 return Context->getObjCInterfaceType(ItfD);
2759 }
2760
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002761 case TYPE_OBJC_OBJECT: {
John McCallc12c5bb2010-05-15 11:32:37 +00002762 unsigned Idx = 0;
2763 QualType Base = GetType(Record[Idx++]);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00002764 unsigned NumProtos = Record[Idx++];
2765 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2766 for (unsigned I = 0; I != NumProtos; ++I)
2767 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
John McCallc12c5bb2010-05-15 11:32:37 +00002768 return Context->getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00002769 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002770
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002771 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00002772 unsigned Idx = 0;
John McCallc12c5bb2010-05-15 11:32:37 +00002773 QualType Pointee = GetType(Record[Idx++]);
2774 return Context->getObjCObjectPointerType(Pointee);
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00002775 }
Argyrios Kyrtzidis24fab412009-09-29 19:42:55 +00002776
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002777 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCall49a832b2009-10-18 09:09:24 +00002778 unsigned Idx = 0;
2779 QualType Parm = GetType(Record[Idx++]);
2780 QualType Replacement = GetType(Record[Idx++]);
2781 return
2782 Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
2783 Replacement);
2784 }
John McCall3cb0ebd2010-03-10 03:28:59 +00002785
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002786 case TYPE_INJECTED_CLASS_NAME: {
John McCall3cb0ebd2010-03-10 03:28:59 +00002787 CXXRecordDecl *D = cast<CXXRecordDecl>(GetDecl(Record[0]));
2788 QualType TST = GetType(Record[1]); // probably derivable
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00002789 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002790 // for AST reading, too much interdependencies.
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00002791 return
2792 QualType(new (*Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCall3cb0ebd2010-03-10 03:28:59 +00002793 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00002794
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002795 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00002796 unsigned Idx = 0;
2797 unsigned Depth = Record[Idx++];
2798 unsigned Index = Record[Idx++];
2799 bool Pack = Record[Idx++];
2800 IdentifierInfo *Name = GetIdentifierInfo(Record, Idx);
2801 return Context->getTemplateTypeParmType(Depth, Index, Pack, Name);
2802 }
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00002803
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002804 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00002805 unsigned Idx = 0;
2806 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2807 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2808 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +00002809 QualType Canon = GetType(Record[Idx++]);
2810 return Context->getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00002811 }
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00002812
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002813 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00002814 unsigned Idx = 0;
2815 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2816 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2817 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
2818 unsigned NumArgs = Record[Idx++];
2819 llvm::SmallVector<TemplateArgument, 8> Args;
2820 Args.reserve(NumArgs);
2821 while (NumArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00002822 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00002823 return Context->getDependentTemplateSpecializationType(Keyword, NNS, Name,
2824 Args.size(), Args.data());
2825 }
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00002826
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002827 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00002828 unsigned Idx = 0;
2829
2830 // ArrayType
2831 QualType ElementType = GetType(Record[Idx++]);
2832 ArrayType::ArraySizeModifier ASM
2833 = (ArrayType::ArraySizeModifier)Record[Idx++];
2834 unsigned IndexTypeQuals = Record[Idx++];
2835
2836 // DependentSizedArrayType
Sebastian Redlc3632732010-10-05 15:59:54 +00002837 Expr *NumElts = ReadExpr(*Loc.F);
2838 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00002839
2840 return Context->getDependentSizedArrayType(ElementType, NumElts, ASM,
2841 IndexTypeQuals, Brackets);
2842 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00002843
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002844 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00002845 unsigned Idx = 0;
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002846 bool IsDependent = Record[Idx++];
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00002847 TemplateName Name = ReadTemplateName(Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00002848 llvm::SmallVector<TemplateArgument, 8> Args;
Sebastian Redlc3632732010-10-05 15:59:54 +00002849 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00002850 QualType Canon = GetType(Record[Idx++]);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002851 QualType T;
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002852 if (Canon.isNull())
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002853 T = Context->getCanonicalTemplateSpecializationType(Name, Args.data(),
2854 Args.size());
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002855 else
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002856 T = Context->getTemplateSpecializationType(Name, Args.data(),
2857 Args.size(), Canon);
2858 T->Dependent = IsDependent;
2859 return T;
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00002860 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002861 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002862 // Suppress a GCC warning
2863 return QualType();
2864}
2865
Sebastian Redlc3632732010-10-05 15:59:54 +00002866class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002867 ASTReader &Reader;
Sebastian Redlc3632732010-10-05 15:59:54 +00002868 ASTReader::PerFileData &F;
Sebastian Redl577d4792010-07-22 22:43:28 +00002869 llvm::BitstreamCursor &DeclsCursor;
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002870 const ASTReader::RecordData &Record;
John McCalla1ee0c52009-10-16 21:56:05 +00002871 unsigned &Idx;
2872
Sebastian Redlc3632732010-10-05 15:59:54 +00002873 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
2874 unsigned &I) {
2875 return Reader.ReadSourceLocation(F, R, I);
2876 }
2877
John McCalla1ee0c52009-10-16 21:56:05 +00002878public:
Sebastian Redlc3632732010-10-05 15:59:54 +00002879 TypeLocReader(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002880 const ASTReader::RecordData &Record, unsigned &Idx)
Sebastian Redlc3632732010-10-05 15:59:54 +00002881 : Reader(Reader), F(F), DeclsCursor(F.DeclsCursor), Record(Record), Idx(Idx)
2882 { }
John McCalla1ee0c52009-10-16 21:56:05 +00002883
John McCall51bd8032009-10-18 01:05:36 +00002884 // We want compile-time assurance that we've enumerated all of
2885 // these, so unfortunately we have to declare them first, then
2886 // define them out-of-line.
2887#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +00002888#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +00002889 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00002890#include "clang/AST/TypeLocNodes.def"
2891
John McCall51bd8032009-10-18 01:05:36 +00002892 void VisitFunctionTypeLoc(FunctionTypeLoc);
2893 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00002894};
2895
John McCall51bd8032009-10-18 01:05:36 +00002896void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCalla1ee0c52009-10-16 21:56:05 +00002897 // nothing to do
2898}
John McCall51bd8032009-10-18 01:05:36 +00002899void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00002900 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
Douglas Gregorddf889a2010-01-18 18:04:31 +00002901 if (TL.needsExtraLocalData()) {
2902 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
2903 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
2904 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
2905 TL.setModeAttr(Record[Idx++]);
2906 }
John McCalla1ee0c52009-10-16 21:56:05 +00002907}
John McCall51bd8032009-10-18 01:05:36 +00002908void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00002909 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00002910}
John McCall51bd8032009-10-18 01:05:36 +00002911void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00002912 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00002913}
John McCall51bd8032009-10-18 01:05:36 +00002914void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00002915 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00002916}
John McCall51bd8032009-10-18 01:05:36 +00002917void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00002918 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00002919}
John McCall51bd8032009-10-18 01:05:36 +00002920void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00002921 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00002922}
John McCall51bd8032009-10-18 01:05:36 +00002923void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00002924 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00002925}
John McCall51bd8032009-10-18 01:05:36 +00002926void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00002927 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
2928 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00002929 if (Record[Idx++])
Sebastian Redlc3632732010-10-05 15:59:54 +00002930 TL.setSizeExpr(Reader.ReadExpr(F));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002931 else
John McCall51bd8032009-10-18 01:05:36 +00002932 TL.setSizeExpr(0);
2933}
2934void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
2935 VisitArrayTypeLoc(TL);
2936}
2937void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
2938 VisitArrayTypeLoc(TL);
2939}
2940void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
2941 VisitArrayTypeLoc(TL);
2942}
2943void TypeLocReader::VisitDependentSizedArrayTypeLoc(
2944 DependentSizedArrayTypeLoc TL) {
2945 VisitArrayTypeLoc(TL);
2946}
2947void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
2948 DependentSizedExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00002949 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00002950}
2951void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00002952 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00002953}
2954void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00002955 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00002956}
2957void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00002958 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
2959 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
Douglas Gregordab60ad2010-10-01 18:44:50 +00002960 TL.setTrailingReturn(Record[Idx++]);
John McCall51bd8032009-10-18 01:05:36 +00002961 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
John McCall86acc2a2009-10-23 01:28:53 +00002962 TL.setArg(i, cast_or_null<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
John McCall51bd8032009-10-18 01:05:36 +00002963 }
2964}
2965void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
2966 VisitFunctionTypeLoc(TL);
2967}
2968void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
2969 VisitFunctionTypeLoc(TL);
2970}
John McCalled976492009-12-04 22:46:56 +00002971void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00002972 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalled976492009-12-04 22:46:56 +00002973}
John McCall51bd8032009-10-18 01:05:36 +00002974void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00002975 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00002976}
2977void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00002978 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
2979 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
2980 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00002981}
2982void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00002983 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
2984 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
2985 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
2986 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00002987}
2988void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00002989 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00002990}
2991void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00002992 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00002993}
2994void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00002995 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00002996}
John McCall51bd8032009-10-18 01:05:36 +00002997void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00002998 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00002999}
John McCall49a832b2009-10-18 09:09:24 +00003000void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
3001 SubstTemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003002 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall49a832b2009-10-18 09:09:24 +00003003}
John McCall51bd8032009-10-18 01:05:36 +00003004void TypeLocReader::VisitTemplateSpecializationTypeLoc(
3005 TemplateSpecializationTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003006 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
3007 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3008 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall833ca992009-10-29 08:12:44 +00003009 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
3010 TL.setArgLocInfo(i,
Sebastian Redlc3632732010-10-05 15:59:54 +00003011 Reader.GetTemplateArgumentLocInfo(F,
3012 TL.getTypePtr()->getArg(i).getKind(),
3013 Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003014}
Abramo Bagnara465d41b2010-05-11 21:36:43 +00003015void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003016 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3017 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003018}
John McCall3cb0ebd2010-03-10 03:28:59 +00003019void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003020 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall3cb0ebd2010-03-10 03:28:59 +00003021}
Douglas Gregor4714c122010-03-31 17:34:00 +00003022void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003023 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3024 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
3025 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003026}
John McCall33500952010-06-11 00:33:02 +00003027void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
3028 DependentTemplateSpecializationTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003029 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3030 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
3031 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3032 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3033 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall33500952010-06-11 00:33:02 +00003034 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
3035 TL.setArgLocInfo(I,
Sebastian Redlc3632732010-10-05 15:59:54 +00003036 Reader.GetTemplateArgumentLocInfo(F,
3037 TL.getTypePtr()->getArg(I).getKind(),
3038 Record, Idx));
John McCall33500952010-06-11 00:33:02 +00003039}
John McCall51bd8032009-10-18 01:05:36 +00003040void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003041 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallc12c5bb2010-05-15 11:32:37 +00003042}
3043void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
3044 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00003045 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3046 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003047 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Sebastian Redlc3632732010-10-05 15:59:54 +00003048 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003049}
John McCall54e14c42009-10-22 22:37:11 +00003050void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003051 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall54e14c42009-10-22 22:37:11 +00003052}
John McCalla1ee0c52009-10-16 21:56:05 +00003053
Sebastian Redlc3632732010-10-05 15:59:54 +00003054TypeSourceInfo *ASTReader::GetTypeSourceInfo(PerFileData &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00003055 const RecordData &Record,
John McCalla1ee0c52009-10-16 21:56:05 +00003056 unsigned &Idx) {
3057 QualType InfoTy = GetType(Record[Idx++]);
3058 if (InfoTy.isNull())
3059 return 0;
3060
John McCalla93c9342009-12-07 02:54:59 +00003061 TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy);
Sebastian Redlc3632732010-10-05 15:59:54 +00003062 TypeLocReader TLR(*this, F, Record, Idx);
John McCalla93c9342009-12-07 02:54:59 +00003063 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCalla1ee0c52009-10-16 21:56:05 +00003064 TLR.Visit(TL);
John McCalla93c9342009-12-07 02:54:59 +00003065 return TInfo;
John McCalla1ee0c52009-10-16 21:56:05 +00003066}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003067
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003068QualType ASTReader::GetType(TypeID ID) {
John McCall0953e762009-09-24 19:53:00 +00003069 unsigned FastQuals = ID & Qualifiers::FastMask;
3070 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003071
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003072 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003073 QualType T;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003074 switch ((PredefinedTypeIDs)Index) {
3075 case PREDEF_TYPE_NULL_ID: return QualType();
3076 case PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break;
3077 case PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003078
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003079 case PREDEF_TYPE_CHAR_U_ID:
3080 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregor2cf26342009-04-09 22:27:44 +00003081 // FIXME: Check that the signedness of CharTy is correct!
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003082 T = Context->CharTy;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003083 break;
3084
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003085 case PREDEF_TYPE_UCHAR_ID: T = Context->UnsignedCharTy; break;
3086 case PREDEF_TYPE_USHORT_ID: T = Context->UnsignedShortTy; break;
3087 case PREDEF_TYPE_UINT_ID: T = Context->UnsignedIntTy; break;
3088 case PREDEF_TYPE_ULONG_ID: T = Context->UnsignedLongTy; break;
3089 case PREDEF_TYPE_ULONGLONG_ID: T = Context->UnsignedLongLongTy; break;
3090 case PREDEF_TYPE_UINT128_ID: T = Context->UnsignedInt128Ty; break;
3091 case PREDEF_TYPE_SCHAR_ID: T = Context->SignedCharTy; break;
3092 case PREDEF_TYPE_WCHAR_ID: T = Context->WCharTy; break;
3093 case PREDEF_TYPE_SHORT_ID: T = Context->ShortTy; break;
3094 case PREDEF_TYPE_INT_ID: T = Context->IntTy; break;
3095 case PREDEF_TYPE_LONG_ID: T = Context->LongTy; break;
3096 case PREDEF_TYPE_LONGLONG_ID: T = Context->LongLongTy; break;
3097 case PREDEF_TYPE_INT128_ID: T = Context->Int128Ty; break;
3098 case PREDEF_TYPE_FLOAT_ID: T = Context->FloatTy; break;
3099 case PREDEF_TYPE_DOUBLE_ID: T = Context->DoubleTy; break;
3100 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy; break;
3101 case PREDEF_TYPE_OVERLOAD_ID: T = Context->OverloadTy; break;
3102 case PREDEF_TYPE_DEPENDENT_ID: T = Context->DependentTy; break;
3103 case PREDEF_TYPE_NULLPTR_ID: T = Context->NullPtrTy; break;
3104 case PREDEF_TYPE_CHAR16_ID: T = Context->Char16Ty; break;
3105 case PREDEF_TYPE_CHAR32_ID: T = Context->Char32Ty; break;
3106 case PREDEF_TYPE_OBJC_ID: T = Context->ObjCBuiltinIdTy; break;
3107 case PREDEF_TYPE_OBJC_CLASS: T = Context->ObjCBuiltinClassTy; break;
3108 case PREDEF_TYPE_OBJC_SEL: T = Context->ObjCBuiltinSelTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003109 }
3110
3111 assert(!T.isNull() && "Unknown predefined type");
John McCall0953e762009-09-24 19:53:00 +00003112 return T.withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003113 }
3114
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003115 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003116 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl07a353c2010-07-14 20:26:45 +00003117 if (TypesLoaded[Index].isNull()) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003118 TypesLoaded[Index] = ReadTypeRecord(Index);
Douglas Gregor97475832010-10-05 18:37:06 +00003119 if (TypesLoaded[Index].isNull())
3120 return QualType();
3121
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003122 TypesLoaded[Index]->setFromAST();
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003123 TypeIdxs[TypesLoaded[Index]] = TypeIdx::fromTypeID(ID);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003124 if (DeserializationListener)
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003125 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1476ed42010-07-16 16:36:56 +00003126 TypesLoaded[Index]);
Sebastian Redl07a353c2010-07-14 20:26:45 +00003127 }
Mike Stump1eb44332009-09-09 15:08:12 +00003128
John McCall0953e762009-09-24 19:53:00 +00003129 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003130}
3131
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003132TypeID ASTReader::GetTypeID(QualType T) const {
3133 return MakeTypeID(T,
3134 std::bind1st(std::mem_fun(&ASTReader::GetTypeIdx), this));
3135}
3136
3137TypeIdx ASTReader::GetTypeIdx(QualType T) const {
3138 if (T.isNull())
3139 return TypeIdx();
3140 assert(!T.getLocalFastQualifiers());
3141
3142 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3143 // GetTypeIdx is mostly used for computing the hash of DeclarationNames and
3144 // comparing keys of ASTDeclContextNameLookupTable.
3145 // If the type didn't come from the AST file use a specially marked index
3146 // so that any hash/key comparison fail since no such index is stored
3147 // in a AST file.
3148 if (I == TypeIdxs.end())
3149 return TypeIdx(-1);
3150 return I->second;
3151}
3152
John McCall833ca992009-10-29 08:12:44 +00003153TemplateArgumentLocInfo
Sebastian Redlc3632732010-10-05 15:59:54 +00003154ASTReader::GetTemplateArgumentLocInfo(PerFileData &F,
3155 TemplateArgument::ArgKind Kind,
John McCall833ca992009-10-29 08:12:44 +00003156 const RecordData &Record,
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00003157 unsigned &Index) {
John McCall833ca992009-10-29 08:12:44 +00003158 switch (Kind) {
3159 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00003160 return ReadExpr(F);
John McCall833ca992009-10-29 08:12:44 +00003161 case TemplateArgument::Type:
Sebastian Redlc3632732010-10-05 15:59:54 +00003162 return GetTypeSourceInfo(F, Record, Index);
Douglas Gregor788cd062009-11-11 01:00:40 +00003163 case TemplateArgument::Template: {
Sebastian Redlc3632732010-10-05 15:59:54 +00003164 SourceRange QualifierRange = ReadSourceRange(F, Record, Index);
3165 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003166 return TemplateArgumentLocInfo(QualifierRange, TemplateNameLoc);
Douglas Gregor788cd062009-11-11 01:00:40 +00003167 }
John McCall833ca992009-10-29 08:12:44 +00003168 case TemplateArgument::Null:
3169 case TemplateArgument::Integral:
3170 case TemplateArgument::Declaration:
3171 case TemplateArgument::Pack:
3172 return TemplateArgumentLocInfo();
3173 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00003174 llvm_unreachable("unexpected template argument loc");
John McCall833ca992009-10-29 08:12:44 +00003175 return TemplateArgumentLocInfo();
3176}
3177
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003178TemplateArgumentLoc
Sebastian Redlc3632732010-10-05 15:59:54 +00003179ASTReader::ReadTemplateArgumentLoc(PerFileData &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00003180 const RecordData &Record, unsigned &Index) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003181 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003182
3183 if (Arg.getKind() == TemplateArgument::Expression) {
3184 if (Record[Index++]) // bool InfoHasSameExpr.
3185 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
3186 }
Sebastian Redlc3632732010-10-05 15:59:54 +00003187 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00003188 Record, Index));
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003189}
3190
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003191Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall76bd1f32010-06-01 09:23:16 +00003192 return GetDecl(ID);
3193}
3194
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003195TranslationUnitDecl *ASTReader::GetTranslationUnitDecl() {
Sebastian Redl30c514c2010-07-14 23:45:08 +00003196 if (!DeclsLoaded[0]) {
Sebastian Redle1dde812010-08-24 00:50:04 +00003197 ReadDeclRecord(0, 1);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003198 if (DeserializationListener)
Sebastian Redl1476ed42010-07-16 16:36:56 +00003199 DeserializationListener->DeclRead(1, DeclsLoaded[0]);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003200 }
Argyrios Kyrtzidis8871a442010-07-08 17:13:02 +00003201
3202 return cast<TranslationUnitDecl>(DeclsLoaded[0]);
3203}
3204
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003205Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003206 if (ID == 0)
3207 return 0;
3208
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003209 if (ID > DeclsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003210 Error("declaration ID out-of-range for AST file");
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003211 return 0;
3212 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003213
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003214 unsigned Index = ID - 1;
Sebastian Redl30c514c2010-07-14 23:45:08 +00003215 if (!DeclsLoaded[Index]) {
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00003216 ReadDeclRecord(Index, ID);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003217 if (DeserializationListener)
3218 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
3219 }
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003220
3221 return DeclsLoaded[Index];
Douglas Gregor2cf26342009-04-09 22:27:44 +00003222}
3223
Chris Lattner887e2b32009-04-27 05:46:25 +00003224/// \brief Resolve the offset of a statement into a statement.
3225///
3226/// This operation will read a new statement from the external
3227/// source each time it is called, and is meant to be used via a
3228/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003229Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003230 // Offset here is a global offset across the entire chain.
3231 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3232 PerFileData &F = *Chain[N - I - 1];
3233 if (Offset < F.SizeInBits) {
3234 // Since we know that this statement is part of a decl, make sure to use
3235 // the decl cursor to read it.
3236 F.DeclsCursor.JumpToBit(Offset);
Sebastian Redlc3632732010-10-05 15:59:54 +00003237 return ReadStmtFromStream(F);
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003238 }
3239 Offset -= F.SizeInBits;
3240 }
3241 llvm_unreachable("Broken chain");
Douglas Gregor250fc9c2009-04-18 00:07:54 +00003242}
3243
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003244bool ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00003245 bool (*isKindWeWant)(Decl::Kind),
John McCall76bd1f32010-06-01 09:23:16 +00003246 llvm::SmallVectorImpl<Decl*> &Decls) {
Mike Stump1eb44332009-09-09 15:08:12 +00003247 assert(DC->hasExternalLexicalStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00003248 "DeclContext has no lexical decls in storage");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003249
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003250 // There might be lexical decls in multiple parts of the chain, for the TU
3251 // at least.
Sebastian Redl4a9eb262010-09-28 02:24:44 +00003252 // DeclContextOffsets might reallocate as we load additional decls below,
3253 // so make a copy of the vector.
3254 DeclContextInfos Infos = DeclContextOffsets[DC];
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003255 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3256 I != E; ++I) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003257 // IDs can be 0 if this context doesn't contain declarations.
3258 if (!I->LexicalDecls)
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003259 continue;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003260
3261 // Load all of the declaration IDs
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00003262 for (const KindDeclIDPair *ID = I->LexicalDecls,
3263 *IDE = ID + I->NumLexicalDecls; ID != IDE; ++ID) {
3264 if (isKindWeWant && !isKindWeWant((Decl::Kind)ID->first))
3265 continue;
3266
3267 Decl *D = GetDecl(ID->second);
Sebastian Redl4a9eb262010-09-28 02:24:44 +00003268 assert(D && "Null decl in lexical decls");
3269 Decls.push_back(D);
3270 }
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003271 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003272
Douglas Gregor25123082009-04-22 22:34:57 +00003273 ++NumLexicalDeclContextsRead;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003274 return false;
3275}
3276
John McCall76bd1f32010-06-01 09:23:16 +00003277DeclContext::lookup_result
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003278ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +00003279 DeclarationName Name) {
Mike Stump1eb44332009-09-09 15:08:12 +00003280 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00003281 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003282 if (!Name)
3283 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
3284 DeclContext::lookup_iterator(0));
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003285
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003286 llvm::SmallVector<NamedDecl *, 64> Decls;
Sebastian Redl8b122732010-08-24 00:49:55 +00003287 // There might be visible decls in multiple parts of the chain, for the TU
Sebastian Redl5967d622010-08-24 00:50:16 +00003288 // and namespaces. For any given name, the last available results replace
3289 // all earlier ones. For this reason, we walk in reverse.
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003290 DeclContextInfos &Infos = DeclContextOffsets[DC];
Sebastian Redl5967d622010-08-24 00:50:16 +00003291 for (DeclContextInfos::reverse_iterator I = Infos.rbegin(), E = Infos.rend();
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003292 I != E; ++I) {
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003293 if (!I->NameLookupTableData)
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003294 continue;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003295
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003296 ASTDeclContextNameLookupTable *LookupTable =
3297 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3298 ASTDeclContextNameLookupTable::iterator Pos = LookupTable->find(Name);
3299 if (Pos == LookupTable->end())
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003300 continue;
3301
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003302 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
3303 for (; Data.first != Data.second; ++Data.first)
3304 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
Sebastian Redl5967d622010-08-24 00:50:16 +00003305 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003306 }
3307
Douglas Gregor25123082009-04-22 22:34:57 +00003308 ++NumVisibleDeclContextsRead;
John McCall76bd1f32010-06-01 09:23:16 +00003309
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003310 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall76bd1f32010-06-01 09:23:16 +00003311 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003312}
3313
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +00003314void ASTReader::MaterializeVisibleDecls(const DeclContext *DC) {
3315 assert(DC->hasExternalVisibleStorage() &&
3316 "DeclContext has no visible decls in storage");
3317
3318 llvm::SmallVector<NamedDecl *, 64> Decls;
3319 // There might be visible decls in multiple parts of the chain, for the TU
3320 // and namespaces.
3321 DeclContextInfos &Infos = DeclContextOffsets[DC];
3322 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3323 I != E; ++I) {
3324 if (!I->NameLookupTableData)
3325 continue;
3326
3327 ASTDeclContextNameLookupTable *LookupTable =
3328 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3329 for (ASTDeclContextNameLookupTable::item_iterator
3330 ItemI = LookupTable->item_begin(),
3331 ItemEnd = LookupTable->item_end() ; ItemI != ItemEnd; ++ItemI) {
3332 ASTDeclContextNameLookupTable::item_iterator::value_type Val
3333 = *ItemI;
3334 ASTDeclContextNameLookupTrait::data_type Data = Val.second;
3335 Decls.clear();
3336 for (; Data.first != Data.second; ++Data.first)
3337 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
3338 MaterializeVisibleDeclsForName(DC, Val.first, Decls);
3339 }
3340 }
3341}
3342
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003343void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003344 assert(Consumer);
3345 while (!InterestingDecls.empty()) {
3346 DeclGroupRef DG(InterestingDecls.front());
3347 InterestingDecls.pop_front();
Sebastian Redl27372b42010-08-11 18:52:41 +00003348 Consumer->HandleInterestingDecl(DG);
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003349 }
3350}
3351
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003352void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor0af2ca42009-04-22 19:09:20 +00003353 this->Consumer = Consumer;
3354
Douglas Gregorfdd01722009-04-14 00:24:19 +00003355 if (!Consumer)
3356 return;
3357
3358 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003359 // Force deserialization of this decl, which will cause it to be queued for
3360 // passing to the consumer.
Daniel Dunbar04a0b502009-09-17 03:06:44 +00003361 GetDecl(ExternalDefinitions[I]);
Douglas Gregorfdd01722009-04-14 00:24:19 +00003362 }
Douglas Gregorc62a2fe2009-04-25 00:41:30 +00003363
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003364 PassInterestingDeclsToConsumer();
Douglas Gregorfdd01722009-04-14 00:24:19 +00003365}
3366
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003367void ASTReader::PrintStats() {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003368 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregor2cf26342009-04-09 22:27:44 +00003369
Mike Stump1eb44332009-09-09 15:08:12 +00003370 unsigned NumTypesLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003371 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall0953e762009-09-24 19:53:00 +00003372 QualType());
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003373 unsigned NumDeclsLoaded
3374 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
3375 (Decl *)0);
3376 unsigned NumIdentifiersLoaded
3377 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
3378 IdentifiersLoaded.end(),
3379 (IdentifierInfo *)0);
Mike Stump1eb44332009-09-09 15:08:12 +00003380 unsigned NumSelectorsLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003381 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
3382 SelectorsLoaded.end(),
3383 Selector());
Douglas Gregor2d41cc12009-04-13 20:50:16 +00003384
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003385 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
3386 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00003387 if (TotalNumSLocEntries)
3388 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
3389 NumSLocEntriesRead, TotalNumSLocEntries,
3390 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003391 if (!TypesLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003392 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003393 NumTypesLoaded, (unsigned)TypesLoaded.size(),
3394 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
3395 if (!DeclsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003396 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003397 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
3398 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003399 if (!IdentifiersLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003400 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003401 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
3402 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Sebastian Redl725cd962010-08-04 20:40:17 +00003403 if (!SelectorsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003404 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redl725cd962010-08-04 20:40:17 +00003405 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
3406 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor83941df2009-04-25 17:48:32 +00003407 if (TotalNumStatements)
3408 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
3409 NumStatementsRead, TotalNumStatements,
3410 ((float)NumStatementsRead/TotalNumStatements * 100));
3411 if (TotalNumMacros)
3412 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
3413 NumMacrosRead, TotalNumMacros,
3414 ((float)NumMacrosRead/TotalNumMacros * 100));
3415 if (TotalLexicalDeclContexts)
3416 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
3417 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
3418 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
3419 * 100));
3420 if (TotalVisibleDeclContexts)
3421 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
3422 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
3423 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
3424 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003425 if (TotalNumMethodPoolEntries) {
Douglas Gregor83941df2009-04-25 17:48:32 +00003426 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003427 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
3428 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor83941df2009-04-25 17:48:32 +00003429 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003430 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor83941df2009-04-25 17:48:32 +00003431 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003432 std::fprintf(stderr, "\n");
3433}
3434
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003435void ASTReader::InitializeSema(Sema &S) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00003436 SemaObj = &S;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003437 S.ExternalSource = this;
3438
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00003439 // Makes sure any declarations that were deserialized "too early"
3440 // still get added to the identifier's declaration chains.
Douglas Gregor76dc8892010-09-24 23:29:12 +00003441 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
3442 if (SemaObj->TUScope)
John McCalld226f652010-08-21 09:40:31 +00003443 SemaObj->TUScope->AddDecl(PreloadedDecls[I]);
Douglas Gregor76dc8892010-09-24 23:29:12 +00003444
3445 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregor668c1a42009-04-21 22:25:48 +00003446 }
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00003447 PreloadedDecls.clear();
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003448
3449 // If there were any tentative definitions, deserialize them and add
Sebastian Redle9d12b62010-01-31 22:27:38 +00003450 // them to Sema's list of tentative definitions.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003451 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
3452 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
Sebastian Redle9d12b62010-01-31 22:27:38 +00003453 SemaObj->TentativeDefinitions.push_back(Var);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003454 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00003455
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003456 // If there were any unused file scoped decls, deserialize them and add to
3457 // Sema's list of unused file scoped decls.
3458 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
3459 DeclaratorDecl *D = cast<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
3460 SemaObj->UnusedFileScopedDecls.push_back(D);
Tanya Lattnere6bbc012010-02-12 00:07:30 +00003461 }
Douglas Gregor14c22f22009-04-22 22:18:58 +00003462
3463 // If there were any locally-scoped external declarations,
3464 // deserialize them and add them to Sema's table of locally-scoped
3465 // external declarations.
3466 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
3467 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
3468 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
3469 }
Douglas Gregorb81c1702009-04-27 20:06:05 +00003470
3471 // If there were any ext_vector type declarations, deserialize them
3472 // and add them to Sema's vector of such declarations.
3473 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I)
3474 SemaObj->ExtVectorDecls.push_back(
3475 cast<TypedefDecl>(GetDecl(ExtVectorDecls[I])));
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003476
3477 // FIXME: Do VTable uses and dynamic classes deserialize too much ?
3478 // Can we cut them down before writing them ?
3479
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003480 // If there were any dynamic classes declarations, deserialize them
3481 // and add them to Sema's vector of such declarations.
3482 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I)
3483 SemaObj->DynamicClasses.push_back(
3484 cast<CXXRecordDecl>(GetDecl(DynamicClasses[I])));
Fariborz Jahanian32019832010-07-23 19:11:11 +00003485
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003486 // Load the offsets of the declarations that Sema references.
3487 // They will be lazily deserialized when needed.
3488 if (!SemaDeclRefs.empty()) {
3489 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
3490 SemaObj->StdNamespace = SemaDeclRefs[0];
3491 SemaObj->StdBadAlloc = SemaDeclRefs[1];
3492 }
3493
Sebastian Redlc3632732010-10-05 15:59:54 +00003494 for (PerFileData *F = FirstInSource; F; F = F->NextInSource) {
3495
3496 // If there are @selector references added them to its pool. This is for
3497 // implementation of -Wselector.
3498 if (!F->ReferencedSelectorsData.empty()) {
3499 unsigned int DataSize = F->ReferencedSelectorsData.size()-1;
3500 unsigned I = 0;
3501 while (I < DataSize) {
3502 Selector Sel = DecodeSelector(F->ReferencedSelectorsData[I++]);
3503 SourceLocation SelLoc = ReadSourceLocation(
3504 *F, F->ReferencedSelectorsData, I);
3505 SemaObj->ReferencedSelectors.insert(std::make_pair(Sel, SelLoc));
3506 }
3507 }
3508
3509 // If there were any pending implicit instantiations, deserialize them
3510 // and add them to Sema's queue of such instantiations.
3511 assert(F->PendingInstantiations.size() % 2 == 0 &&
3512 "Expected pairs of entries");
3513 for (unsigned Idx = 0, N = F->PendingInstantiations.size(); Idx < N;) {
3514 ValueDecl *D=cast<ValueDecl>(GetDecl(F->PendingInstantiations[Idx++]));
3515 SourceLocation Loc = ReadSourceLocation(*F, F->PendingInstantiations,Idx);
3516 SemaObj->PendingInstantiations.push_back(std::make_pair(D, Loc));
3517 }
3518 }
3519
3520 // The two special data sets below always come from the most recent PCH,
3521 // which is at the front of the chain.
3522 PerFileData &F = *Chain.front();
3523
3524 // If there were any weak undeclared identifiers, deserialize them and add to
3525 // Sema's list of weak undeclared identifiers.
3526 if (!WeakUndeclaredIdentifiers.empty()) {
3527 unsigned Idx = 0;
3528 for (unsigned I = 0, N = WeakUndeclaredIdentifiers[Idx++]; I != N; ++I) {
3529 IdentifierInfo *WeakId = GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
3530 IdentifierInfo *AliasId= GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
3531 SourceLocation Loc = ReadSourceLocation(F, WeakUndeclaredIdentifiers,Idx);
3532 bool Used = WeakUndeclaredIdentifiers[Idx++];
3533 Sema::WeakInfo WI(AliasId, Loc);
3534 WI.setUsed(Used);
3535 SemaObj->WeakUndeclaredIdentifiers.insert(std::make_pair(WeakId, WI));
3536 }
3537 }
3538
3539 // If there were any VTable uses, deserialize the information and add it
3540 // to Sema's vector and map of VTable uses.
3541 if (!VTableUses.empty()) {
3542 unsigned Idx = 0;
3543 for (unsigned I = 0, N = VTableUses[Idx++]; I != N; ++I) {
3544 CXXRecordDecl *Class = cast<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
3545 SourceLocation Loc = ReadSourceLocation(F, VTableUses, Idx);
3546 bool DefinitionRequired = VTableUses[Idx++];
3547 SemaObj->VTableUses.push_back(std::make_pair(Class, Loc));
3548 SemaObj->VTablesUsed[Class] = DefinitionRequired;
Fariborz Jahanian32019832010-07-23 19:11:11 +00003549 }
3550 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00003551}
3552
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003553IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Sebastian Redld8c5abb2010-08-02 18:30:12 +00003554 // Try to find this name within our on-disk hash tables. We start with the
3555 // most recent one, since that one contains the most up-to-date info.
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003556 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003557 ASTIdentifierLookupTable *IdTable
3558 = (ASTIdentifierLookupTable *)Chain[I]->IdentifierLookupTable;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003559 if (!IdTable)
3560 continue;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003561 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003562 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003563 if (Pos == IdTable->end())
3564 continue;
Douglas Gregor668c1a42009-04-21 22:25:48 +00003565
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003566 // Dereferencing the iterator has the effect of building the
3567 // IdentifierInfo node and populating it with the various
3568 // declarations it needs.
Sebastian Redld8c5abb2010-08-02 18:30:12 +00003569 return *Pos;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003570 }
Sebastian Redld8c5abb2010-08-02 18:30:12 +00003571 return 0;
Douglas Gregor668c1a42009-04-21 22:25:48 +00003572}
3573
Mike Stump1eb44332009-09-09 15:08:12 +00003574std::pair<ObjCMethodList, ObjCMethodList>
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003575ASTReader::ReadMethodPool(Selector Sel) {
Sebastian Redl725cd962010-08-04 20:40:17 +00003576 // Find this selector in a hash table. We want to find the most recent entry.
3577 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3578 PerFileData &F = *Chain[I];
3579 if (!F.SelectorLookupTable)
3580 continue;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003581
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003582 ASTSelectorLookupTable *PoolTable
3583 = (ASTSelectorLookupTable*)F.SelectorLookupTable;
3584 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
Sebastian Redl725cd962010-08-04 20:40:17 +00003585 if (Pos != PoolTable->end()) {
3586 ++NumSelectorsRead;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003587 // FIXME: Not quite happy with the statistics here. We probably should
3588 // disable this tracking when called via LoadSelector.
3589 // Also, should entries without methods count as misses?
3590 ++NumMethodPoolEntriesRead;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003591 ASTSelectorLookupTrait::data_type Data = *Pos;
Sebastian Redl725cd962010-08-04 20:40:17 +00003592 if (DeserializationListener)
3593 DeserializationListener->SelectorRead(Data.ID, Sel);
3594 return std::make_pair(Data.Instance, Data.Factory);
3595 }
Douglas Gregor83941df2009-04-25 17:48:32 +00003596 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003597
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003598 ++NumMethodPoolMisses;
Sebastian Redl725cd962010-08-04 20:40:17 +00003599 return std::pair<ObjCMethodList, ObjCMethodList>();
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003600}
3601
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003602void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redle58aa892010-08-04 18:21:41 +00003603 // It would be complicated to avoid reading the methods anyway. So don't.
3604 ReadMethodPool(Sel);
3605}
3606
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003607void ASTReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00003608 assert(ID && "Non-zero identifier ID required");
Douglas Gregora02b1472009-04-28 21:53:25 +00003609 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003610 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003611 if (DeserializationListener)
3612 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregor668c1a42009-04-21 22:25:48 +00003613}
3614
Douglas Gregord89275b2009-07-06 18:54:52 +00003615/// \brief Set the globally-visible declarations associated with the given
3616/// identifier.
3617///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003618/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump1eb44332009-09-09 15:08:12 +00003619/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregord89275b2009-07-06 18:54:52 +00003620/// them.
3621///
3622/// \param II an IdentifierInfo that refers to one or more globally-visible
3623/// declarations.
3624///
3625/// \param DeclIDs the set of declaration IDs with the name @p II that are
3626/// visible at global scope.
3627///
3628/// \param Nonrecursive should be true to indicate that the caller knows that
3629/// this call is non-recursive, and therefore the globally-visible declarations
3630/// will not be placed onto the pending queue.
Mike Stump1eb44332009-09-09 15:08:12 +00003631void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003632ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Douglas Gregord89275b2009-07-06 18:54:52 +00003633 const llvm::SmallVectorImpl<uint32_t> &DeclIDs,
3634 bool Nonrecursive) {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00003635 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregord89275b2009-07-06 18:54:52 +00003636 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
3637 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
3638 PII.II = II;
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00003639 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregord89275b2009-07-06 18:54:52 +00003640 return;
3641 }
Mike Stump1eb44332009-09-09 15:08:12 +00003642
Douglas Gregord89275b2009-07-06 18:54:52 +00003643 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
3644 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
3645 if (SemaObj) {
Douglas Gregor914ed9d2010-08-13 03:15:25 +00003646 if (SemaObj->TUScope) {
3647 // Introduce this declaration into the translation-unit scope
3648 // and add it to the declaration chain for this identifier, so
3649 // that (unqualified) name lookup will find it.
John McCalld226f652010-08-21 09:40:31 +00003650 SemaObj->TUScope->AddDecl(D);
Douglas Gregor914ed9d2010-08-13 03:15:25 +00003651 }
Douglas Gregor76dc8892010-09-24 23:29:12 +00003652 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
Douglas Gregord89275b2009-07-06 18:54:52 +00003653 } else {
3654 // Queue this declaration so that it will be added to the
3655 // translation unit scope and identifier's declaration chain
3656 // once a Sema object is known.
3657 PreloadedDecls.push_back(D);
3658 }
3659 }
3660}
3661
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003662IdentifierInfo *ASTReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00003663 if (ID == 0)
3664 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003665
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00003666 if (IdentifiersLoaded.empty()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003667 Error("no identifier table in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00003668 return 0;
3669 }
Mike Stump1eb44332009-09-09 15:08:12 +00003670
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003671 assert(PP && "Forgot to set Preprocessor ?");
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00003672 ID -= 1;
3673 if (!IdentifiersLoaded[ID]) {
3674 unsigned Index = ID;
3675 const char *Str = 0;
3676 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3677 PerFileData *F = Chain[N - I - 1];
3678 if (Index < F->LocalNumIdentifiers) {
3679 uint32_t Offset = F->IdentifierOffsets[Index];
3680 Str = F->IdentifierTableData + Offset;
3681 break;
3682 }
3683 Index -= F->LocalNumIdentifiers;
3684 }
3685 assert(Str && "Broken Chain");
Douglas Gregord6595a42009-04-25 21:04:17 +00003686
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003687 // All of the strings in the AST file are preceded by a 16-bit length.
3688 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenek231bc0b2009-10-23 04:45:31 +00003689 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
3690 // unsigned integers. This is important to avoid integer overflow when
3691 // we cast them to 'unsigned'.
Ted Kremenekff1ea462009-10-23 03:57:22 +00003692 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregor02fc7512009-04-28 20:01:51 +00003693 unsigned StrLen = (((unsigned) StrLenPtr[0])
3694 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00003695 IdentifiersLoaded[ID]
Kovarththanan Rajaratnam811f4262010-03-12 10:32:27 +00003696 = &PP->getIdentifierTable().get(Str, StrLen);
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003697 if (DeserializationListener)
3698 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregorafaf3082009-04-11 00:14:32 +00003699 }
Mike Stump1eb44332009-09-09 15:08:12 +00003700
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00003701 return IdentifiersLoaded[ID];
Douglas Gregor2cf26342009-04-09 22:27:44 +00003702}
3703
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003704void ASTReader::ReadSLocEntry(unsigned ID) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00003705 ReadSLocEntryRecord(ID);
3706}
3707
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003708Selector ASTReader::DecodeSelector(unsigned ID) {
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003709 if (ID == 0)
3710 return Selector();
Mike Stump1eb44332009-09-09 15:08:12 +00003711
Sebastian Redl725cd962010-08-04 20:40:17 +00003712 if (ID > SelectorsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003713 Error("selector ID out of range in AST file");
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003714 return Selector();
3715 }
Douglas Gregor83941df2009-04-25 17:48:32 +00003716
Sebastian Redl725cd962010-08-04 20:40:17 +00003717 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor83941df2009-04-25 17:48:32 +00003718 // Load this selector from the selector table.
Sebastian Redl725cd962010-08-04 20:40:17 +00003719 unsigned Idx = ID - 1;
3720 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3721 PerFileData &F = *Chain[N - I - 1];
3722 if (Idx < F.LocalNumSelectors) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003723 ASTSelectorLookupTrait Trait(*this);
Sebastian Redl725cd962010-08-04 20:40:17 +00003724 SelectorsLoaded[ID - 1] =
3725 Trait.ReadKey(F.SelectorLookupTableData + F.SelectorOffsets[Idx], 0);
3726 if (DeserializationListener)
3727 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
3728 break;
3729 }
3730 Idx -= F.LocalNumSelectors;
3731 }
Douglas Gregor83941df2009-04-25 17:48:32 +00003732 }
3733
Sebastian Redl725cd962010-08-04 20:40:17 +00003734 return SelectorsLoaded[ID - 1];
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003735}
3736
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003737Selector ASTReader::GetExternalSelector(uint32_t ID) {
Douglas Gregor719770d2010-04-06 17:30:22 +00003738 return DecodeSelector(ID);
3739}
3740
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003741uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redl725cd962010-08-04 20:40:17 +00003742 // ID 0 (the null selector) is considered an external selector.
3743 return getTotalNumSelectors() + 1;
Douglas Gregor719770d2010-04-06 17:30:22 +00003744}
3745
Mike Stump1eb44332009-09-09 15:08:12 +00003746DeclarationName
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003747ASTReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003748 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
3749 switch (Kind) {
3750 case DeclarationName::Identifier:
3751 return DeclarationName(GetIdentifierInfo(Record, Idx));
3752
3753 case DeclarationName::ObjCZeroArgSelector:
3754 case DeclarationName::ObjCOneArgSelector:
3755 case DeclarationName::ObjCMultiArgSelector:
Steve Naroffa7503a72009-04-23 15:15:40 +00003756 return DeclarationName(GetSelector(Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00003757
3758 case DeclarationName::CXXConstructorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003759 return Context->DeclarationNames.getCXXConstructorName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00003760 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00003761
3762 case DeclarationName::CXXDestructorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003763 return Context->DeclarationNames.getCXXDestructorName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00003764 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00003765
3766 case DeclarationName::CXXConversionFunctionName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003767 return Context->DeclarationNames.getCXXConversionFunctionName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00003768 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00003769
3770 case DeclarationName::CXXOperatorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003771 return Context->DeclarationNames.getCXXOperatorName(
Douglas Gregor2cf26342009-04-09 22:27:44 +00003772 (OverloadedOperatorKind)Record[Idx++]);
3773
Sean Hunt3e518bd2009-11-29 07:34:05 +00003774 case DeclarationName::CXXLiteralOperatorName:
3775 return Context->DeclarationNames.getCXXLiteralOperatorName(
3776 GetIdentifierInfo(Record, Idx));
3777
Douglas Gregor2cf26342009-04-09 22:27:44 +00003778 case DeclarationName::CXXUsingDirective:
3779 return DeclarationName::getUsingDirectiveName();
3780 }
3781
3782 // Required to silence GCC warning
3783 return DeclarationName();
3784}
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003785
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003786TemplateName
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003787ASTReader::ReadTemplateName(const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003788 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
3789 switch (Kind) {
3790 case TemplateName::Template:
3791 return TemplateName(cast_or_null<TemplateDecl>(GetDecl(Record[Idx++])));
3792
3793 case TemplateName::OverloadedTemplate: {
3794 unsigned size = Record[Idx++];
3795 UnresolvedSet<8> Decls;
3796 while (size--)
3797 Decls.addDecl(cast<NamedDecl>(GetDecl(Record[Idx++])));
3798
3799 return Context->getOverloadedTemplateName(Decls.begin(), Decls.end());
3800 }
3801
3802 case TemplateName::QualifiedTemplate: {
3803 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3804 bool hasTemplKeyword = Record[Idx++];
3805 TemplateDecl *Template = cast<TemplateDecl>(GetDecl(Record[Idx++]));
3806 return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
3807 }
3808
3809 case TemplateName::DependentTemplate: {
3810 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
3811 if (Record[Idx++]) // isIdentifier
3812 return Context->getDependentTemplateName(NNS,
3813 GetIdentifierInfo(Record, Idx));
3814 return Context->getDependentTemplateName(NNS,
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003815 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003816 }
3817 }
3818
3819 assert(0 && "Unhandled template name kind!");
3820 return TemplateName();
3821}
3822
3823TemplateArgument
Sebastian Redlc3632732010-10-05 15:59:54 +00003824ASTReader::ReadTemplateArgument(PerFileData &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00003825 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003826 switch ((TemplateArgument::ArgKind)Record[Idx++]) {
3827 case TemplateArgument::Null:
3828 return TemplateArgument();
3829 case TemplateArgument::Type:
3830 return TemplateArgument(GetType(Record[Idx++]));
3831 case TemplateArgument::Declaration:
3832 return TemplateArgument(GetDecl(Record[Idx++]));
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00003833 case TemplateArgument::Integral: {
3834 llvm::APSInt Value = ReadAPSInt(Record, Idx);
3835 QualType T = GetType(Record[Idx++]);
3836 return TemplateArgument(Value, T);
3837 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003838 case TemplateArgument::Template:
3839 return TemplateArgument(ReadTemplateName(Record, Idx));
3840 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00003841 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003842 case TemplateArgument::Pack: {
3843 unsigned NumArgs = Record[Idx++];
3844 llvm::SmallVector<TemplateArgument, 8> Args;
3845 Args.reserve(NumArgs);
3846 while (NumArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00003847 Args.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00003848 TemplateArgument TemplArg;
3849 TemplArg.setArgumentPack(Args.data(), Args.size(), /*CopyArgs=*/true);
3850 return TemplArg;
3851 }
3852 }
3853
3854 assert(0 && "Unhandled template argument kind!");
3855 return TemplateArgument();
3856}
3857
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003858TemplateParameterList *
Sebastian Redlc3632732010-10-05 15:59:54 +00003859ASTReader::ReadTemplateParameterList(PerFileData &F,
3860 const RecordData &Record, unsigned &Idx) {
3861 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
3862 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
3863 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003864
3865 unsigned NumParams = Record[Idx++];
3866 llvm::SmallVector<NamedDecl *, 16> Params;
3867 Params.reserve(NumParams);
3868 while (NumParams--)
3869 Params.push_back(cast<NamedDecl>(GetDecl(Record[Idx++])));
3870
3871 TemplateParameterList* TemplateParams =
3872 TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc,
3873 Params.data(), Params.size(), RAngleLoc);
3874 return TemplateParams;
3875}
3876
3877void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003878ASTReader::
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003879ReadTemplateArgumentList(llvm::SmallVector<TemplateArgument, 8> &TemplArgs,
Sebastian Redlc3632732010-10-05 15:59:54 +00003880 PerFileData &F, const RecordData &Record,
3881 unsigned &Idx) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003882 unsigned NumTemplateArgs = Record[Idx++];
3883 TemplArgs.reserve(NumTemplateArgs);
3884 while (NumTemplateArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00003885 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003886}
3887
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00003888/// \brief Read a UnresolvedSet structure.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003889void ASTReader::ReadUnresolvedSet(UnresolvedSetImpl &Set,
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00003890 const RecordData &Record, unsigned &Idx) {
3891 unsigned NumDecls = Record[Idx++];
3892 while (NumDecls--) {
3893 NamedDecl *D = cast<NamedDecl>(GetDecl(Record[Idx++]));
3894 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
3895 Set.addDecl(D, AS);
3896 }
3897}
3898
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003899CXXBaseSpecifier
Sebastian Redlc3632732010-10-05 15:59:54 +00003900ASTReader::ReadCXXBaseSpecifier(PerFileData &F,
Nick Lewycky56062202010-07-26 16:56:01 +00003901 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003902 bool isVirtual = static_cast<bool>(Record[Idx++]);
3903 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
3904 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00003905 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
3906 SourceRange Range = ReadSourceRange(F, Record, Idx);
Nick Lewycky56062202010-07-26 16:56:01 +00003907 return CXXBaseSpecifier(Range, isVirtual, isBaseOfClass, AS, TInfo);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003908}
3909
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003910std::pair<CXXBaseOrMemberInitializer **, unsigned>
Sebastian Redlc3632732010-10-05 15:59:54 +00003911ASTReader::ReadCXXBaseOrMemberInitializers(PerFileData &F,
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003912 const RecordData &Record,
3913 unsigned &Idx) {
3914 CXXBaseOrMemberInitializer **BaseOrMemberInitializers = 0;
3915 unsigned NumInitializers = Record[Idx++];
3916 if (NumInitializers) {
3917 ASTContext &C = *getContext();
3918
3919 BaseOrMemberInitializers
3920 = new (C) CXXBaseOrMemberInitializer*[NumInitializers];
3921 for (unsigned i=0; i != NumInitializers; ++i) {
3922 TypeSourceInfo *BaseClassInfo = 0;
3923 bool IsBaseVirtual = false;
3924 FieldDecl *Member = 0;
3925
3926 bool IsBaseInitializer = Record[Idx++];
3927 if (IsBaseInitializer) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003928 BaseClassInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003929 IsBaseVirtual = Record[Idx++];
3930 } else {
3931 Member = cast<FieldDecl>(GetDecl(Record[Idx++]));
3932 }
Sebastian Redlc3632732010-10-05 15:59:54 +00003933 SourceLocation MemberLoc = ReadSourceLocation(F, Record, Idx);
3934 Expr *Init = ReadExpr(F);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003935 FieldDecl *AnonUnionMember
3936 = cast_or_null<FieldDecl>(GetDecl(Record[Idx++]));
Sebastian Redlc3632732010-10-05 15:59:54 +00003937 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
3938 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003939 bool IsWritten = Record[Idx++];
3940 unsigned SourceOrderOrNumArrayIndices;
3941 llvm::SmallVector<VarDecl *, 8> Indices;
3942 if (IsWritten) {
3943 SourceOrderOrNumArrayIndices = Record[Idx++];
3944 } else {
3945 SourceOrderOrNumArrayIndices = Record[Idx++];
3946 Indices.reserve(SourceOrderOrNumArrayIndices);
3947 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
3948 Indices.push_back(cast<VarDecl>(GetDecl(Record[Idx++])));
3949 }
3950
3951 CXXBaseOrMemberInitializer *BOMInit;
3952 if (IsBaseInitializer) {
3953 BOMInit = new (C) CXXBaseOrMemberInitializer(C, BaseClassInfo,
3954 IsBaseVirtual, LParenLoc,
3955 Init, RParenLoc);
3956 } else if (IsWritten) {
3957 BOMInit = new (C) CXXBaseOrMemberInitializer(C, Member, MemberLoc,
3958 LParenLoc, Init, RParenLoc);
3959 } else {
3960 BOMInit = CXXBaseOrMemberInitializer::Create(C, Member, MemberLoc,
3961 LParenLoc, Init, RParenLoc,
3962 Indices.data(),
3963 Indices.size());
3964 }
3965
Argyrios Kyrtzidisf84cde12010-09-06 19:04:27 +00003966 if (IsWritten)
3967 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003968 BOMInit->setAnonUnionMember(AnonUnionMember);
3969 BaseOrMemberInitializers[i] = BOMInit;
3970 }
3971 }
3972
3973 return std::make_pair(BaseOrMemberInitializers, NumInitializers);
3974}
3975
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003976NestedNameSpecifier *
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003977ASTReader::ReadNestedNameSpecifier(const RecordData &Record, unsigned &Idx) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003978 unsigned N = Record[Idx++];
3979 NestedNameSpecifier *NNS = 0, *Prev = 0;
3980 for (unsigned I = 0; I != N; ++I) {
3981 NestedNameSpecifier::SpecifierKind Kind
3982 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
3983 switch (Kind) {
3984 case NestedNameSpecifier::Identifier: {
3985 IdentifierInfo *II = GetIdentifierInfo(Record, Idx);
3986 NNS = NestedNameSpecifier::Create(*Context, Prev, II);
3987 break;
3988 }
3989
3990 case NestedNameSpecifier::Namespace: {
3991 NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++]));
3992 NNS = NestedNameSpecifier::Create(*Context, Prev, NS);
3993 break;
3994 }
3995
3996 case NestedNameSpecifier::TypeSpec:
3997 case NestedNameSpecifier::TypeSpecWithTemplate: {
3998 Type *T = GetType(Record[Idx++]).getTypePtr();
3999 bool Template = Record[Idx++];
4000 NNS = NestedNameSpecifier::Create(*Context, Prev, Template, T);
4001 break;
4002 }
4003
4004 case NestedNameSpecifier::Global: {
4005 NNS = NestedNameSpecifier::GlobalSpecifier(*Context);
4006 // No associated value, and there can't be a prefix.
4007 break;
4008 }
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004009 }
Argyrios Kyrtzidisd2bb2c02010-07-07 15:46:30 +00004010 Prev = NNS;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004011 }
4012 return NNS;
4013}
4014
4015SourceRange
Sebastian Redlc3632732010-10-05 15:59:54 +00004016ASTReader::ReadSourceRange(PerFileData &F, const RecordData &Record,
4017 unsigned &Idx) {
4018 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
4019 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar8ee59392010-06-02 15:47:10 +00004020 return SourceRange(beg, end);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004021}
4022
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004023/// \brief Read an integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004024llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004025 unsigned BitWidth = Record[Idx++];
4026 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
4027 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
4028 Idx += NumWords;
4029 return Result;
4030}
4031
4032/// \brief Read a signed integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004033llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004034 bool isUnsigned = Record[Idx++];
4035 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
4036}
4037
Douglas Gregor17fc2232009-04-14 21:55:33 +00004038/// \brief Read a floating-point value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004039llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00004040 return llvm::APFloat(ReadAPInt(Record, Idx));
4041}
4042
Douglas Gregor68a2eb02009-04-15 21:30:51 +00004043// \brief Read a string
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004044std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00004045 unsigned Len = Record[Idx++];
Jay Foadbeaaccd2009-05-21 09:52:38 +00004046 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00004047 Idx += Len;
4048 return Result;
4049}
4050
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004051CXXTemporary *ASTReader::ReadCXXTemporary(const RecordData &Record,
Chris Lattnerd2598362010-05-10 00:25:06 +00004052 unsigned &Idx) {
4053 CXXDestructorDecl *Decl = cast<CXXDestructorDecl>(GetDecl(Record[Idx++]));
4054 return CXXTemporary::Create(*Context, Decl);
4055}
4056
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004057DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00004058 return Diag(SourceLocation(), DiagID);
4059}
4060
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004061DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00004062 return Diags.Report(FullSourceLoc(Loc, SourceMgr), DiagID);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00004063}
Douglas Gregor025452f2009-04-17 00:04:06 +00004064
Douglas Gregor668c1a42009-04-21 22:25:48 +00004065/// \brief Retrieve the identifier table associated with the
4066/// preprocessor.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004067IdentifierTable &ASTReader::getIdentifierTable() {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00004068 assert(PP && "Forgot to set Preprocessor ?");
4069 return PP->getIdentifierTable();
Douglas Gregor668c1a42009-04-21 22:25:48 +00004070}
4071
Douglas Gregor025452f2009-04-17 00:04:06 +00004072/// \brief Record that the given ID maps to the given switch-case
4073/// statement.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004074void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Douglas Gregor025452f2009-04-17 00:04:06 +00004075 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
4076 SwitchCaseStmts[ID] = SC;
4077}
4078
4079/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004080SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Douglas Gregor025452f2009-04-17 00:04:06 +00004081 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
4082 return SwitchCaseStmts[ID];
4083}
Douglas Gregor1de05fe2009-04-17 18:18:49 +00004084
4085/// \brief Record that the given label statement has been
4086/// deserialized and has the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004087void ASTReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
Mike Stump1eb44332009-09-09 15:08:12 +00004088 assert(LabelStmts.find(ID) == LabelStmts.end() &&
Douglas Gregor1de05fe2009-04-17 18:18:49 +00004089 "Deserialized label twice");
4090 LabelStmts[ID] = S;
4091
4092 // If we've already seen any goto statements that point to this
4093 // label, resolve them now.
4094 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
4095 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
4096 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
4097 Goto->second->setLabel(S);
4098 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00004099
4100 // If we've already seen any address-label statements that point to
4101 // this label, resolve them now.
4102 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
Mike Stump1eb44332009-09-09 15:08:12 +00004103 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00004104 = UnresolvedAddrLabelExprs.equal_range(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00004105 for (AddrLabelIter AddrLabel = AddrLabels.first;
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00004106 AddrLabel != AddrLabels.second; ++AddrLabel)
4107 AddrLabel->second->setLabel(S);
4108 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor1de05fe2009-04-17 18:18:49 +00004109}
4110
4111/// \brief Set the label of the given statement to the label
4112/// identified by ID.
4113///
4114/// Depending on the order in which the label and other statements
4115/// referencing that label occur, this operation may complete
4116/// immediately (updating the statement) or it may queue the
4117/// statement to be back-patched later.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004118void ASTReader::SetLabelOf(GotoStmt *S, unsigned ID) {
Douglas Gregor1de05fe2009-04-17 18:18:49 +00004119 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
4120 if (Label != LabelStmts.end()) {
4121 // We've already seen this label, so set the label of the goto and
4122 // we're done.
4123 S->setLabel(Label->second);
4124 } else {
4125 // We haven't seen this label yet, so add this goto to the set of
4126 // unresolved goto statements.
4127 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
4128 }
4129}
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00004130
4131/// \brief Set the label of the given expression to the label
4132/// identified by ID.
4133///
4134/// Depending on the order in which the label and other statements
4135/// referencing that label occur, this operation may complete
4136/// immediately (updating the statement) or it may queue the
4137/// statement to be back-patched later.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004138void ASTReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00004139 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
4140 if (Label != LabelStmts.end()) {
4141 // We've already seen this label, so set the label of the
4142 // label-address expression and we're done.
4143 S->setLabel(Label->second);
4144 } else {
4145 // We haven't seen this label yet, so add this label-address
4146 // expression to the set of unresolved label-address expressions.
4147 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
4148 }
4149}
Douglas Gregord89275b2009-07-06 18:54:52 +00004150
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004151void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004152 assert(NumCurrentElementsDeserializing &&
4153 "FinishedDeserializing not paired with StartedDeserializing");
4154 if (NumCurrentElementsDeserializing == 1) {
Douglas Gregord89275b2009-07-06 18:54:52 +00004155 // If any identifiers with corresponding top-level declarations have
4156 // been loaded, load those declarations now.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004157 while (!PendingIdentifierInfos.empty()) {
4158 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
4159 PendingIdentifierInfos.front().DeclIDs, true);
4160 PendingIdentifierInfos.pop_front();
Douglas Gregord89275b2009-07-06 18:54:52 +00004161 }
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00004162
4163 // We are not in recursive loading, so it's safe to pass the "interesting"
4164 // decls to the consumer.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004165 if (Consumer)
4166 PassInterestingDeclsToConsumer();
Douglas Gregord89275b2009-07-06 18:54:52 +00004167 }
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004168 --NumCurrentElementsDeserializing;
Douglas Gregord89275b2009-07-06 18:54:52 +00004169}
Douglas Gregor501c1032010-08-19 00:28:17 +00004170
Sebastian Redle1dde812010-08-24 00:50:04 +00004171ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context,
4172 const char *isysroot, bool DisableValidation)
4173 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
4174 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
4175 Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context),
4176 Consumer(0), isysroot(isysroot), DisableValidation(DisableValidation),
4177 NumStatHits(0), NumStatMisses(0), NumSLocEntriesRead(0),
Sebastian Redl8db9fae2010-09-22 20:19:08 +00004178 TotalNumSLocEntries(0), NextSLocOffset(0), NumStatementsRead(0),
4179 TotalNumStatements(0), NumMacrosRead(0), TotalNumMacros(0),
4180 NumSelectorsRead(0), NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
Sebastian Redle1dde812010-08-24 00:50:04 +00004181 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
4182 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
4183 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
4184 RelocatablePCH = false;
4185}
4186
4187ASTReader::ASTReader(SourceManager &SourceMgr, FileManager &FileMgr,
4188 Diagnostic &Diags, const char *isysroot,
4189 bool DisableValidation)
4190 : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr),
4191 Diags(Diags), SemaObj(0), PP(0), Context(0), Consumer(0),
4192 isysroot(isysroot), DisableValidation(DisableValidation), NumStatHits(0),
4193 NumStatMisses(0), NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Sebastian Redl8db9fae2010-09-22 20:19:08 +00004194 NextSLocOffset(0), NumStatementsRead(0), TotalNumStatements(0),
4195 NumMacrosRead(0), TotalNumMacros(0), NumSelectorsRead(0),
4196 NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
4197 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
4198 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
4199 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
Sebastian Redle1dde812010-08-24 00:50:04 +00004200 RelocatablePCH = false;
4201}
4202
4203ASTReader::~ASTReader() {
4204 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
4205 delete Chain[e - i - 1];
4206 // Delete all visible decl lookup tables
4207 for (DeclContextOffsetsMap::iterator I = DeclContextOffsets.begin(),
4208 E = DeclContextOffsets.end();
4209 I != E; ++I) {
4210 for (DeclContextInfos::iterator J = I->second.begin(), F = I->second.end();
4211 J != F; ++J) {
4212 if (J->NameLookupTableData)
4213 delete static_cast<ASTDeclContextNameLookupTable*>(
4214 J->NameLookupTableData);
4215 }
4216 }
4217 for (DeclContextVisibleUpdatesPending::iterator
4218 I = PendingVisibleUpdates.begin(),
4219 E = PendingVisibleUpdates.end();
4220 I != E; ++I) {
4221 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
4222 F = I->second.end();
4223 J != F; ++J)
4224 delete static_cast<ASTDeclContextNameLookupTable*>(*J);
4225 }
4226}
4227
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00004228ASTReader::PerFileData::PerFileData(ASTFileType Ty)
4229 : Type(Ty), SizeInBits(0), LocalNumSLocEntries(0), SLocOffsets(0), LocalSLocSize(0),
Sebastian Redl301c9b02010-09-22 00:42:27 +00004230 LocalNumIdentifiers(0), IdentifierOffsets(0), IdentifierTableData(0),
4231 IdentifierLookupTable(0), LocalNumMacroDefinitions(0),
4232 MacroDefinitionOffsets(0), LocalNumSelectors(0), SelectorOffsets(0),
4233 SelectorLookupTableData(0), SelectorLookupTable(0), LocalNumDecls(0),
4234 DeclOffsets(0), LocalNumTypes(0), TypeOffsets(0), StatCache(0),
Sebastian Redla866e652010-10-01 19:59:12 +00004235 NumPreallocatedPreprocessingEntities(0), NextInSource(0)
Douglas Gregor501c1032010-08-19 00:28:17 +00004236{}
4237
4238ASTReader::PerFileData::~PerFileData() {
4239 delete static_cast<ASTIdentifierLookupTable *>(IdentifierLookupTable);
4240 delete static_cast<ASTSelectorLookupTable *>(SelectorLookupTable);
4241}
4242