blob: b90203b477b0dae2d12704fd6b03548a4cd0717c [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);
Michael J. Spencerdae4ac42010-10-21 05:21:48 +000077 PARSE_LANGOPT_BENIGN(MSCVersion);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000078 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
79 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
80 PARSE_LANGOPT_BENIGN(CXXOperatorName);
81 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
82 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
83 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
Fariborz Jahanian412e7982010-02-09 19:31:38 +000084 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI2, diag::warn_pch_nonfragile_abi2);
Michael J. Spencer20249a12010-10-21 03:16:25 +000085 PARSE_LANGOPT_IMPORTANT(NoConstantCFStrings,
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +000086 diag::warn_pch_no_constant_cfstrings);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000087 PARSE_LANGOPT_BENIGN(PascalStrings);
88 PARSE_LANGOPT_BENIGN(WritableStrings);
Mike Stump1eb44332009-09-09 15:08:12 +000089 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000090 diag::warn_pch_lax_vector_conversions);
Nate Begeman69cfb9b2009-06-25 22:57:40 +000091 PARSE_LANGOPT_IMPORTANT(AltiVec, diag::warn_pch_altivec);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000092 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
Daniel Dunbar73482882010-02-10 18:48:44 +000093 PARSE_LANGOPT_IMPORTANT(SjLjExceptions, diag::warn_pch_sjlj_exceptions);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000094 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
95 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
96 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
Mike Stump1eb44332009-09-09 15:08:12 +000097 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000098 diag::warn_pch_thread_safe_statics);
Daniel Dunbar5345c392009-09-03 04:54:28 +000099 PARSE_LANGOPT_IMPORTANT(POSIXThreads, diag::warn_pch_posix_threads);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000100 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
101 PARSE_LANGOPT_BENIGN(EmitAllDecls);
102 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
Chris Lattnera4d71452010-06-26 21:25:03 +0000103 PARSE_LANGOPT_BENIGN(getSignedOverflowBehavior());
Mike Stump1eb44332009-09-09 15:08:12 +0000104 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000105 diag::warn_pch_heinous_extensions);
106 // FIXME: Most of the options below are benign if the macro wasn't
107 // used. Unfortunately, this means that a PCH compiled without
108 // optimization can't be used with optimization turned on, even
109 // though the only thing that changes is whether __OPTIMIZE__ was
110 // defined... but if __OPTIMIZE__ never showed up in the header, it
111 // doesn't matter. We could consider making this some special kind
112 // of check.
113 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
114 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
115 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
116 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
117 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
118 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
119 PARSE_LANGOPT_IMPORTANT(AccessControl, diag::warn_pch_access_control);
120 PARSE_LANGOPT_IMPORTANT(CharIsSigned, diag::warn_pch_char_signed);
John Thompsona6fda122009-11-05 20:14:16 +0000121 PARSE_LANGOPT_IMPORTANT(ShortWChar, diag::warn_pch_short_wchar);
Argyrios Kyrtzidis9a2b9d72010-10-08 00:25:19 +0000122 PARSE_LANGOPT_IMPORTANT(ShortEnums, diag::warn_pch_short_enums);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000123 if ((PPLangOpts.getGCMode() != 0) != (LangOpts.getGCMode() != 0)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000124 Reader.Diag(diag::warn_pch_gc_mode)
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000125 << LangOpts.getGCMode() << PPLangOpts.getGCMode();
126 return true;
127 }
128 PARSE_LANGOPT_BENIGN(getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000129 PARSE_LANGOPT_IMPORTANT(getStackProtectorMode(),
130 diag::warn_pch_stack_protector);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000131 PARSE_LANGOPT_BENIGN(InstantiationDepth);
Nate Begeman69cfb9b2009-06-25 22:57:40 +0000132 PARSE_LANGOPT_IMPORTANT(OpenCL, diag::warn_pch_opencl);
Mike Stump9c276ae2009-12-12 01:27:46 +0000133 PARSE_LANGOPT_BENIGN(CatchUndefined);
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000134 PARSE_LANGOPT_IMPORTANT(ElideConstructors, diag::warn_pch_elide_constructors);
Douglas Gregora0068fc2010-07-09 17:35:33 +0000135 PARSE_LANGOPT_BENIGN(SpellChecking);
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +0000136#undef PARSE_LANGOPT_IMPORTANT
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000137#undef PARSE_LANGOPT_BENIGN
138
139 return false;
140}
141
Daniel Dunbardc3c0d22009-11-11 00:52:11 +0000142bool PCHValidator::ReadTargetTriple(llvm::StringRef Triple) {
143 if (Triple == PP.getTargetInfo().getTriple().str())
144 return false;
145
146 Reader.Diag(diag::warn_pch_target_triple)
147 << Triple << PP.getTargetInfo().getTriple().str();
148 return true;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000149}
150
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000151struct EmptyStringRef {
Benjamin Kramerec1b1cc2010-07-14 23:19:41 +0000152 bool operator ()(llvm::StringRef r) const { return r.empty(); }
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000153};
154struct EmptyBlock {
155 bool operator ()(const PCHPredefinesBlock &r) const { return r.Data.empty(); }
156};
157
158static bool EqualConcatenations(llvm::SmallVector<llvm::StringRef, 2> L,
159 PCHPredefinesBlocks R) {
160 // First, sum up the lengths.
161 unsigned LL = 0, RL = 0;
162 for (unsigned I = 0, N = L.size(); I != N; ++I) {
163 LL += L[I].size();
164 }
165 for (unsigned I = 0, N = R.size(); I != N; ++I) {
166 RL += R[I].Data.size();
167 }
168 if (LL != RL)
169 return false;
170 if (LL == 0 && RL == 0)
171 return true;
172
173 // Kick out empty parts, they confuse the algorithm below.
174 L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end());
175 R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end());
176
177 // Do it the hard way. At this point, both vectors must be non-empty.
178 llvm::StringRef LR = L[0], RR = R[0].Data;
179 unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size();
Daniel Dunbarc76c9e02010-07-16 00:00:11 +0000180 (void) RN;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000181 for (;;) {
182 // Compare the current pieces.
183 if (LR.size() == RR.size()) {
184 // If they're the same length, it's pretty easy.
185 if (LR != RR)
186 return false;
187 // Both pieces are done, advance.
188 ++LI;
189 ++RI;
190 // If either string is done, they're both done, since they're the same
191 // length.
192 if (LI == LN) {
193 assert(RI == RN && "Strings not the same length after all?");
194 return true;
195 }
196 LR = L[LI];
197 RR = R[RI].Data;
198 } else if (LR.size() < RR.size()) {
199 // Right piece is longer.
200 if (!RR.startswith(LR))
201 return false;
202 ++LI;
203 assert(LI != LN && "Strings not the same length after all?");
204 RR = RR.substr(LR.size());
205 LR = L[LI];
206 } else {
207 // Left piece is longer.
208 if (!LR.startswith(RR))
209 return false;
210 ++RI;
211 assert(RI != RN && "Strings not the same length after all?");
212 LR = LR.substr(RR.size());
213 RR = R[RI].Data;
214 }
215 }
216}
217
218static std::pair<FileID, llvm::StringRef::size_type>
219FindMacro(const PCHPredefinesBlocks &Buffers, llvm::StringRef MacroDef) {
220 std::pair<FileID, llvm::StringRef::size_type> Res;
221 for (unsigned I = 0, N = Buffers.size(); I != N; ++I) {
222 Res.second = Buffers[I].Data.find(MacroDef);
223 if (Res.second != llvm::StringRef::npos) {
224 Res.first = Buffers[I].BufferID;
225 break;
226 }
227 }
228 return Res;
229}
230
231bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000232 llvm::StringRef OriginalFileName,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000233 std::string &SuggestedPredefines) {
Daniel Dunbarc7162932009-11-11 23:58:53 +0000234 // We are in the context of an implicit include, so the predefines buffer will
235 // have a #include entry for the PCH file itself (as normalized by the
236 // preprocessor initialization). Find it and skip over it in the checking
237 // below.
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000238 llvm::SmallString<256> PCHInclude;
239 PCHInclude += "#include \"";
Daniel Dunbarc7162932009-11-11 23:58:53 +0000240 PCHInclude += NormalizeDashIncludePath(OriginalFileName);
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000241 PCHInclude += "\"\n";
242 std::pair<llvm::StringRef,llvm::StringRef> Split =
243 llvm::StringRef(PP.getPredefines()).split(PCHInclude.str());
244 llvm::StringRef Left = Split.first, Right = Split.second;
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000245 if (Left == PP.getPredefines()) {
246 Error("Missing PCH include entry!");
247 return true;
248 }
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000249
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000250 // If the concatenation of all the PCH buffers is equal to the adjusted
251 // command line, we're done.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000252 llvm::SmallVector<llvm::StringRef, 2> CommandLine;
253 CommandLine.push_back(Left);
254 CommandLine.push_back(Right);
255 if (EqualConcatenations(CommandLine, Buffers))
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000256 return false;
257
258 SourceManager &SourceMgr = PP.getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000259
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000260 // The predefines buffers are different. Determine what the differences are,
261 // and whether they require us to reject the PCH file.
Daniel Dunbare6750492009-11-13 16:46:11 +0000262 llvm::SmallVector<llvm::StringRef, 8> PCHLines;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000263 for (unsigned I = 0, N = Buffers.size(); I != N; ++I)
264 Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Daniel Dunbare6750492009-11-13 16:46:11 +0000265
266 llvm::SmallVector<llvm::StringRef, 8> CmdLineLines;
267 Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000268
269 // Pick out implicit #includes after the PCH and don't consider them for
270 // validation; we will insert them into SuggestedPredefines so that the
271 // preprocessor includes them.
272 std::string IncludesAfterPCH;
273 llvm::SmallVector<llvm::StringRef, 8> AfterPCHLines;
274 Right.split(AfterPCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
275 for (unsigned i = 0, e = AfterPCHLines.size(); i != e; ++i) {
276 if (AfterPCHLines[i].startswith("#include ")) {
277 IncludesAfterPCH += AfterPCHLines[i];
278 IncludesAfterPCH += '\n';
279 } else {
280 CmdLineLines.push_back(AfterPCHLines[i]);
281 }
282 }
283
284 // Make sure we add the includes last into SuggestedPredefines before we
285 // exit this function.
286 struct AddIncludesRAII {
287 std::string &SuggestedPredefines;
288 std::string &IncludesAfterPCH;
289
290 AddIncludesRAII(std::string &SuggestedPredefines,
291 std::string &IncludesAfterPCH)
292 : SuggestedPredefines(SuggestedPredefines),
293 IncludesAfterPCH(IncludesAfterPCH) { }
294 ~AddIncludesRAII() {
295 SuggestedPredefines += IncludesAfterPCH;
296 }
297 } AddIncludes(SuggestedPredefines, IncludesAfterPCH);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000298
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000299 // Sort both sets of predefined buffer lines, since we allow some extra
300 // definitions and they may appear at any point in the output.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000301 std::sort(CmdLineLines.begin(), CmdLineLines.end());
302 std::sort(PCHLines.begin(), PCHLines.end());
303
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000304 // Determine which predefines that were used to build the PCH file are missing
305 // from the command line.
306 std::vector<llvm::StringRef> MissingPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000307 std::set_difference(PCHLines.begin(), PCHLines.end(),
308 CmdLineLines.begin(), CmdLineLines.end(),
309 std::back_inserter(MissingPredefines));
310
311 bool MissingDefines = false;
312 bool ConflictingDefines = false;
313 for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) {
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000314 llvm::StringRef Missing = MissingPredefines[I];
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000315 if (Missing.startswith("#include ")) {
316 // An -include was specified when generating the PCH; it is included in
317 // the PCH, just ignore it.
318 continue;
319 }
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000320 if (!Missing.startswith("#define ")) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000321 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
322 return true;
323 }
Mike Stump1eb44332009-09-09 15:08:12 +0000324
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000325 // This is a macro definition. Determine the name of the macro we're
326 // defining.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000327 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000328 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000329 = Missing.find_first_of("( \n\r", StartOfMacroName);
330 assert(EndOfMacroName != std::string::npos &&
331 "Couldn't find the end of the macro name");
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000332 llvm::StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000333
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000334 // Determine whether this macro was given a different definition on the
335 // command line.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000336 std::string MacroDefStart = "#define " + MacroName.str();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000337 std::string::size_type MacroDefLen = MacroDefStart.size();
Daniel Dunbare6750492009-11-13 16:46:11 +0000338 llvm::SmallVector<llvm::StringRef, 8>::iterator ConflictPos
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000339 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
340 MacroDefStart);
341 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000342 if (!ConflictPos->startswith(MacroDefStart)) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000343 // Different macro; we're done.
344 ConflictPos = CmdLineLines.end();
Mike Stump1eb44332009-09-09 15:08:12 +0000345 break;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000346 }
Mike Stump1eb44332009-09-09 15:08:12 +0000347
348 assert(ConflictPos->size() > MacroDefLen &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000349 "Invalid #define in predefines buffer?");
Mike Stump1eb44332009-09-09 15:08:12 +0000350 if ((*ConflictPos)[MacroDefLen] != ' ' &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000351 (*ConflictPos)[MacroDefLen] != '(')
352 continue; // Longer macro name; keep trying.
Mike Stump1eb44332009-09-09 15:08:12 +0000353
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000354 // We found a conflicting macro definition.
355 break;
356 }
Mike Stump1eb44332009-09-09 15:08:12 +0000357
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000358 if (ConflictPos != CmdLineLines.end()) {
359 Reader.Diag(diag::warn_cmdline_conflicting_macro_def)
360 << MacroName;
361
362 // Show the definition of this macro within the PCH file.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000363 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
364 FindMacro(Buffers, Missing);
365 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
366 SourceLocation PCHMissingLoc =
367 SourceMgr.getLocForStartOfFile(MacroLoc.first)
368 .getFileLocWithOffset(MacroLoc.second);
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000369 Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000370
371 ConflictingDefines = true;
372 continue;
373 }
Mike Stump1eb44332009-09-09 15:08:12 +0000374
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000375 // If the macro doesn't conflict, then we'll just pick up the macro
376 // definition from the PCH file. Warn the user that they made a mistake.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000377 if (ConflictingDefines)
378 continue; // Don't complain if there are already conflicting defs
Mike Stump1eb44332009-09-09 15:08:12 +0000379
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000380 if (!MissingDefines) {
381 Reader.Diag(diag::warn_cmdline_missing_macro_defs);
382 MissingDefines = true;
383 }
384
385 // Show the definition of this macro within the PCH file.
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000386 std::pair<FileID, llvm::StringRef::size_type> MacroLoc =
387 FindMacro(Buffers, Missing);
388 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!");
389 SourceLocation PCHMissingLoc =
390 SourceMgr.getLocForStartOfFile(MacroLoc.first)
391 .getFileLocWithOffset(MacroLoc.second);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000392 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
393 }
Mike Stump1eb44332009-09-09 15:08:12 +0000394
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000395 if (ConflictingDefines)
396 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000397
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000398 // Determine what predefines were introduced based on command-line
399 // parameters that were not present when building the PCH
400 // file. Extra #defines are okay, so long as the identifiers being
401 // defined were not used within the precompiled header.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000402 std::vector<llvm::StringRef> ExtraPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000403 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
404 PCHLines.begin(), PCHLines.end(),
Mike Stump1eb44332009-09-09 15:08:12 +0000405 std::back_inserter(ExtraPredefines));
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000406 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000407 llvm::StringRef &Extra = ExtraPredefines[I];
408 if (!Extra.startswith("#define ")) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000409 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
410 return true;
411 }
412
413 // This is an extra macro definition. Determine the name of the
414 // macro we're defining.
415 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000416 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000417 = Extra.find_first_of("( \n\r", StartOfMacroName);
418 assert(EndOfMacroName != std::string::npos &&
419 "Couldn't find the end of the macro name");
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000420 llvm::StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000421
422 // Check whether this name was used somewhere in the PCH file. If
423 // so, defining it as a macro could change behavior, so we reject
424 // the PCH file.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000425 if (IdentifierInfo *II = Reader.get(MacroName)) {
Daniel Dunbar4fda42e2009-11-11 00:52:00 +0000426 Reader.Diag(diag::warn_macro_name_used_in_pch) << II;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000427 return true;
428 }
429
430 // Add this definition to the suggested predefines buffer.
431 SuggestedPredefines += Extra;
432 SuggestedPredefines += '\n';
433 }
434
435 // If we get here, it's because the predefines buffer had compatible
436 // contents. Accept the PCH file.
437 return false;
438}
439
Douglas Gregor12fab312010-03-16 16:35:32 +0000440void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
441 unsigned ID) {
442 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
443 ++NumHeaderInfos;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000444}
445
446void PCHValidator::ReadCounter(unsigned Value) {
447 PP.setCounterValue(Value);
448}
449
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000450//===----------------------------------------------------------------------===//
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000451// AST reader implementation
Douglas Gregor668c1a42009-04-21 22:25:48 +0000452//===----------------------------------------------------------------------===//
453
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000454void
Sebastian Redl571db7f2010-08-18 23:56:56 +0000455ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000456 DeserializationListener = Listener;
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000457}
458
Chris Lattner4c6f9522009-04-27 05:14:47 +0000459
Douglas Gregor668c1a42009-04-21 22:25:48 +0000460namespace {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000461class ASTSelectorLookupTrait {
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000462 ASTReader &Reader;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000463
464public:
Sebastian Redl5d050072010-08-04 17:20:04 +0000465 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000466 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +0000467 ObjCMethodList Instance, Factory;
468 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000469
470 typedef Selector external_key_type;
471 typedef external_key_type internal_key_type;
472
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000473 explicit ASTSelectorLookupTrait(ASTReader &Reader) : Reader(Reader) { }
Mike Stump1eb44332009-09-09 15:08:12 +0000474
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000475 static bool EqualKey(const internal_key_type& a,
476 const internal_key_type& b) {
477 return a == b;
478 }
Mike Stump1eb44332009-09-09 15:08:12 +0000479
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000480 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +0000481 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000482 }
Mike Stump1eb44332009-09-09 15:08:12 +0000483
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000484 // This hopefully will just get inlined and removed by the optimizer.
485 static const internal_key_type&
486 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump1eb44332009-09-09 15:08:12 +0000487
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000488 static std::pair<unsigned, unsigned>
489 ReadKeyDataLength(const unsigned char*& d) {
490 using namespace clang::io;
491 unsigned KeyLen = ReadUnalignedLE16(d);
492 unsigned DataLen = ReadUnalignedLE16(d);
493 return std::make_pair(KeyLen, DataLen);
494 }
Mike Stump1eb44332009-09-09 15:08:12 +0000495
Douglas Gregor83941df2009-04-25 17:48:32 +0000496 internal_key_type ReadKey(const unsigned char* d, unsigned) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000497 using namespace clang::io;
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000498 SelectorTable &SelTable = Reader.getContext()->Selectors;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000499 unsigned N = ReadUnalignedLE16(d);
Mike Stump1eb44332009-09-09 15:08:12 +0000500 IdentifierInfo *FirstII
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000501 = Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
502 if (N == 0)
503 return SelTable.getNullarySelector(FirstII);
504 else if (N == 1)
505 return SelTable.getUnarySelector(FirstII);
506
507 llvm::SmallVector<IdentifierInfo *, 16> Args;
508 Args.push_back(FirstII);
509 for (unsigned I = 1; I != N; ++I)
510 Args.push_back(Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)));
511
Douglas Gregor75fdb232009-05-22 22:45:36 +0000512 return SelTable.getSelector(N, Args.data());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000513 }
Mike Stump1eb44332009-09-09 15:08:12 +0000514
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000515 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen) {
516 using namespace clang::io;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000517
518 data_type Result;
519
Sebastian Redl5d050072010-08-04 17:20:04 +0000520 Result.ID = ReadUnalignedLE32(d);
521 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
522 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
523
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000524 // Load instance methods
525 ObjCMethodList *Prev = 0;
526 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000527 ObjCMethodDecl *Method
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000528 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl5d050072010-08-04 17:20:04 +0000529 if (!Result.Instance.Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000530 // This is the first method, which is the easy case.
Sebastian Redl5d050072010-08-04 17:20:04 +0000531 Result.Instance.Method = Method;
532 Prev = &Result.Instance;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000533 continue;
534 }
535
Ted Kremenek298ed872010-02-11 00:53:01 +0000536 ObjCMethodList *Mem =
537 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
538 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000539 Prev = Prev->Next;
540 }
541
542 // Load factory methods
543 Prev = 0;
544 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000545 ObjCMethodDecl *Method
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000546 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Sebastian Redl5d050072010-08-04 17:20:04 +0000547 if (!Result.Factory.Method) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000548 // This is the first method, which is the easy case.
Sebastian Redl5d050072010-08-04 17:20:04 +0000549 Result.Factory.Method = Method;
550 Prev = &Result.Factory;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000551 continue;
552 }
553
Ted Kremenek298ed872010-02-11 00:53:01 +0000554 ObjCMethodList *Mem =
555 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>();
556 Prev->Next = new (Mem) ObjCMethodList(Method, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000557 Prev = Prev->Next;
558 }
559
560 return Result;
561 }
562};
Mike Stump1eb44332009-09-09 15:08:12 +0000563
564} // end anonymous namespace
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000565
566/// \brief The on-disk hash table used for the global method pool.
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000567typedef OnDiskChainedHashTable<ASTSelectorLookupTrait>
568 ASTSelectorLookupTable;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000569
Sebastian Redlc3632732010-10-05 15:59:54 +0000570namespace clang {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000571class ASTIdentifierLookupTrait {
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000572 ASTReader &Reader;
Sebastian Redlc3632732010-10-05 15:59:54 +0000573 ASTReader::PerFileData &F;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000574
575 // If we know the IdentifierInfo in advance, it is here and we will
576 // not build a new one. Used when deserializing information about an
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000577 // identifier that was constructed before the AST file was read.
Douglas Gregor668c1a42009-04-21 22:25:48 +0000578 IdentifierInfo *KnownII;
579
580public:
581 typedef IdentifierInfo * data_type;
582
583 typedef const std::pair<const char*, unsigned> external_key_type;
584
585 typedef external_key_type internal_key_type;
586
Sebastian Redlc3632732010-10-05 15:59:54 +0000587 ASTIdentifierLookupTrait(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redld27d3fc2010-07-21 22:31:37 +0000588 IdentifierInfo *II = 0)
Sebastian Redlc3632732010-10-05 15:59:54 +0000589 : Reader(Reader), F(F), KnownII(II) { }
Mike Stump1eb44332009-09-09 15:08:12 +0000590
Douglas Gregor668c1a42009-04-21 22:25:48 +0000591 static bool EqualKey(const internal_key_type& a,
592 const internal_key_type& b) {
593 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
594 : false;
595 }
Mike Stump1eb44332009-09-09 15:08:12 +0000596
Douglas Gregor668c1a42009-04-21 22:25:48 +0000597 static unsigned ComputeHash(const internal_key_type& a) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000598 return llvm::HashString(llvm::StringRef(a.first, a.second));
Douglas Gregor668c1a42009-04-21 22:25:48 +0000599 }
Mike Stump1eb44332009-09-09 15:08:12 +0000600
Douglas Gregor668c1a42009-04-21 22:25:48 +0000601 // This hopefully will just get inlined and removed by the optimizer.
602 static const internal_key_type&
603 GetInternalKey(const external_key_type& x) { return x; }
Mike Stump1eb44332009-09-09 15:08:12 +0000604
Douglas Gregor95f42922010-10-14 22:11:03 +0000605 // This hopefully will just get inlined and removed by the optimizer.
606 static const external_key_type&
607 GetExternalKey(const internal_key_type& x) { return x; }
608
Douglas Gregor668c1a42009-04-21 22:25:48 +0000609 static std::pair<unsigned, unsigned>
610 ReadKeyDataLength(const unsigned char*& d) {
611 using namespace clang::io;
Douglas Gregor5f8e3302009-04-25 20:26:24 +0000612 unsigned DataLen = ReadUnalignedLE16(d);
Douglas Gregord6595a42009-04-25 21:04:17 +0000613 unsigned KeyLen = ReadUnalignedLE16(d);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000614 return std::make_pair(KeyLen, DataLen);
615 }
Mike Stump1eb44332009-09-09 15:08:12 +0000616
Douglas Gregor668c1a42009-04-21 22:25:48 +0000617 static std::pair<const char*, unsigned>
618 ReadKey(const unsigned char* d, unsigned n) {
619 assert(n >= 2 && d[n-1] == '\0');
620 return std::make_pair((const char*) d, n-1);
621 }
Mike Stump1eb44332009-09-09 15:08:12 +0000622
623 IdentifierInfo *ReadData(const internal_key_type& k,
Douglas Gregor668c1a42009-04-21 22:25:48 +0000624 const unsigned char* d,
625 unsigned DataLen) {
626 using namespace clang::io;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000627 IdentID ID = ReadUnalignedLE32(d);
Douglas Gregora92193e2009-04-28 21:18:29 +0000628 bool IsInteresting = ID & 0x01;
629
630 // Wipe out the "is interesting" bit.
631 ID = ID >> 1;
632
633 if (!IsInteresting) {
Sebastian Redl083abdf2010-07-27 23:01:28 +0000634 // For uninteresting identifiers, just build the IdentifierInfo
Douglas Gregora92193e2009-04-28 21:18:29 +0000635 // and associate it with the persistent ID.
636 IdentifierInfo *II = KnownII;
637 if (!II)
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000638 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregora92193e2009-04-28 21:18:29 +0000639 Reader.SetIdentifierInfo(ID, II);
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000640 II->setIsFromAST();
Douglas Gregora92193e2009-04-28 21:18:29 +0000641 return II;
642 }
643
Douglas Gregor5998da52009-04-28 21:32:13 +0000644 unsigned Bits = ReadUnalignedLE16(d);
Douglas Gregor2deaea32009-04-22 18:49:13 +0000645 bool CPlusPlusOperatorKeyword = Bits & 0x01;
646 Bits >>= 1;
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000647 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
648 Bits >>= 1;
Douglas Gregor2deaea32009-04-22 18:49:13 +0000649 bool Poisoned = Bits & 0x01;
650 Bits >>= 1;
651 bool ExtensionToken = Bits & 0x01;
652 Bits >>= 1;
653 bool hasMacroDefinition = Bits & 0x01;
654 Bits >>= 1;
655 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
656 Bits >>= 10;
Mike Stump1eb44332009-09-09 15:08:12 +0000657
Douglas Gregor2deaea32009-04-22 18:49:13 +0000658 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregor5998da52009-04-28 21:32:13 +0000659 DataLen -= 6;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000660
661 // Build the IdentifierInfo itself and link the identifier ID with
662 // the new IdentifierInfo.
663 IdentifierInfo *II = KnownII;
664 if (!II)
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000665 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000666 Reader.SetIdentifierInfo(ID, II);
667
Douglas Gregor2deaea32009-04-22 18:49:13 +0000668 // Set or check the various bits in the IdentifierInfo structure.
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +0000669 // Token IDs are read-only.
670 if (HasRevertedTokenIDToIdentifier)
671 II->RevertTokenIDToIdentifier();
Douglas Gregor2deaea32009-04-22 18:49:13 +0000672 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
Mike Stump1eb44332009-09-09 15:08:12 +0000673 assert(II->isExtensionToken() == ExtensionToken &&
Douglas Gregor2deaea32009-04-22 18:49:13 +0000674 "Incorrect extension token flag");
675 (void)ExtensionToken;
676 II->setIsPoisoned(Poisoned);
677 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
678 "Incorrect C++ operator keyword flag");
679 (void)CPlusPlusOperatorKeyword;
680
Douglas Gregor37e26842009-04-21 23:56:24 +0000681 // If this identifier is a macro, deserialize the macro
682 // definition.
683 if (hasMacroDefinition) {
Douglas Gregor5998da52009-04-28 21:32:13 +0000684 uint32_t Offset = ReadUnalignedLE32(d);
Douglas Gregor295a2a62010-10-30 00:23:06 +0000685 Reader.SetIdentifierIsMacro(II, F, Offset);
Douglas Gregor5998da52009-04-28 21:32:13 +0000686 DataLen -= 4;
Douglas Gregor37e26842009-04-21 23:56:24 +0000687 }
Douglas Gregor668c1a42009-04-21 22:25:48 +0000688
689 // Read all of the declarations visible at global scope with this
690 // name.
Chris Lattner6bf690f2009-04-27 22:17:41 +0000691 if (Reader.getContext() == 0) return II;
Douglas Gregord89275b2009-07-06 18:54:52 +0000692 if (DataLen > 0) {
693 llvm::SmallVector<uint32_t, 4> DeclIDs;
694 for (; DataLen > 0; DataLen -= 4)
695 DeclIDs.push_back(ReadUnalignedLE32(d));
696 Reader.SetGloballyVisibleDecls(II, DeclIDs);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000697 }
Mike Stump1eb44332009-09-09 15:08:12 +0000698
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000699 II->setIsFromAST();
Douglas Gregor668c1a42009-04-21 22:25:48 +0000700 return II;
701 }
702};
Mike Stump1eb44332009-09-09 15:08:12 +0000703
704} // end anonymous namespace
Douglas Gregor668c1a42009-04-21 22:25:48 +0000705
706/// \brief The on-disk hash table used to contain information about
707/// all of the identifiers in the program.
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000708typedef OnDiskChainedHashTable<ASTIdentifierLookupTrait>
709 ASTIdentifierLookupTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000710
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000711namespace {
712class ASTDeclContextNameLookupTrait {
713 ASTReader &Reader;
714
715public:
716 /// \brief Pair of begin/end iterators for DeclIDs.
717 typedef std::pair<DeclID *, DeclID *> data_type;
718
719 /// \brief Special internal key for declaration names.
720 /// The hash table creates keys for comparison; we do not create
721 /// a DeclarationName for the internal key to avoid deserializing types.
722 struct DeclNameKey {
723 DeclarationName::NameKind Kind;
724 uint64_t Data;
725 DeclNameKey() : Kind((DeclarationName::NameKind)0), Data(0) { }
726 };
727
728 typedef DeclarationName external_key_type;
729 typedef DeclNameKey internal_key_type;
730
731 explicit ASTDeclContextNameLookupTrait(ASTReader &Reader) : Reader(Reader) { }
732
733 static bool EqualKey(const internal_key_type& a,
734 const internal_key_type& b) {
735 return a.Kind == b.Kind && a.Data == b.Data;
736 }
737
738 unsigned ComputeHash(const DeclNameKey &Key) const {
739 llvm::FoldingSetNodeID ID;
740 ID.AddInteger(Key.Kind);
741
742 switch (Key.Kind) {
743 case DeclarationName::Identifier:
744 case DeclarationName::CXXLiteralOperatorName:
745 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
746 break;
747 case DeclarationName::ObjCZeroArgSelector:
748 case DeclarationName::ObjCOneArgSelector:
749 case DeclarationName::ObjCMultiArgSelector:
750 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
751 break;
752 case DeclarationName::CXXConstructorName:
753 case DeclarationName::CXXDestructorName:
754 case DeclarationName::CXXConversionFunctionName:
755 ID.AddInteger((TypeID)Key.Data);
756 break;
757 case DeclarationName::CXXOperatorName:
758 ID.AddInteger((OverloadedOperatorKind)Key.Data);
759 break;
760 case DeclarationName::CXXUsingDirective:
761 break;
762 }
763
764 return ID.ComputeHash();
765 }
766
767 internal_key_type GetInternalKey(const external_key_type& Name) const {
768 DeclNameKey Key;
769 Key.Kind = Name.getNameKind();
770 switch (Name.getNameKind()) {
771 case DeclarationName::Identifier:
772 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
773 break;
774 case DeclarationName::ObjCZeroArgSelector:
775 case DeclarationName::ObjCOneArgSelector:
776 case DeclarationName::ObjCMultiArgSelector:
777 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
778 break;
779 case DeclarationName::CXXConstructorName:
780 case DeclarationName::CXXDestructorName:
781 case DeclarationName::CXXConversionFunctionName:
782 Key.Data = Reader.GetTypeID(Name.getCXXNameType());
783 break;
784 case DeclarationName::CXXOperatorName:
785 Key.Data = Name.getCXXOverloadedOperator();
786 break;
787 case DeclarationName::CXXLiteralOperatorName:
788 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
789 break;
790 case DeclarationName::CXXUsingDirective:
791 break;
792 }
Michael J. Spencer20249a12010-10-21 03:16:25 +0000793
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000794 return Key;
795 }
796
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +0000797 external_key_type GetExternalKey(const internal_key_type& Key) const {
798 ASTContext *Context = Reader.getContext();
799 switch (Key.Kind) {
800 case DeclarationName::Identifier:
801 return DeclarationName((IdentifierInfo*)Key.Data);
802
803 case DeclarationName::ObjCZeroArgSelector:
804 case DeclarationName::ObjCOneArgSelector:
805 case DeclarationName::ObjCMultiArgSelector:
806 return DeclarationName(Selector(Key.Data));
807
808 case DeclarationName::CXXConstructorName:
809 return Context->DeclarationNames.getCXXConstructorName(
810 Context->getCanonicalType(Reader.GetType(Key.Data)));
811
812 case DeclarationName::CXXDestructorName:
813 return Context->DeclarationNames.getCXXDestructorName(
814 Context->getCanonicalType(Reader.GetType(Key.Data)));
815
816 case DeclarationName::CXXConversionFunctionName:
817 return Context->DeclarationNames.getCXXConversionFunctionName(
818 Context->getCanonicalType(Reader.GetType(Key.Data)));
819
820 case DeclarationName::CXXOperatorName:
821 return Context->DeclarationNames.getCXXOperatorName(
822 (OverloadedOperatorKind)Key.Data);
823
824 case DeclarationName::CXXLiteralOperatorName:
825 return Context->DeclarationNames.getCXXLiteralOperatorName(
826 (IdentifierInfo*)Key.Data);
827
828 case DeclarationName::CXXUsingDirective:
829 return DeclarationName::getUsingDirectiveName();
830 }
831
832 llvm_unreachable("Invalid Name Kind ?");
833 }
834
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000835 static std::pair<unsigned, unsigned>
836 ReadKeyDataLength(const unsigned char*& d) {
837 using namespace clang::io;
838 unsigned KeyLen = ReadUnalignedLE16(d);
839 unsigned DataLen = ReadUnalignedLE16(d);
840 return std::make_pair(KeyLen, DataLen);
841 }
842
843 internal_key_type ReadKey(const unsigned char* d, unsigned) {
844 using namespace clang::io;
845
846 DeclNameKey Key;
847 Key.Kind = (DeclarationName::NameKind)*d++;
848 switch (Key.Kind) {
849 case DeclarationName::Identifier:
850 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
851 break;
852 case DeclarationName::ObjCZeroArgSelector:
853 case DeclarationName::ObjCOneArgSelector:
854 case DeclarationName::ObjCMultiArgSelector:
Michael J. Spencer20249a12010-10-21 03:16:25 +0000855 Key.Data =
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000856 (uint64_t)Reader.DecodeSelector(ReadUnalignedLE32(d)).getAsOpaquePtr();
857 break;
858 case DeclarationName::CXXConstructorName:
859 case DeclarationName::CXXDestructorName:
860 case DeclarationName::CXXConversionFunctionName:
861 Key.Data = ReadUnalignedLE32(d); // TypeID
862 break;
863 case DeclarationName::CXXOperatorName:
864 Key.Data = *d++; // OverloadedOperatorKind
865 break;
866 case DeclarationName::CXXLiteralOperatorName:
867 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
868 break;
869 case DeclarationName::CXXUsingDirective:
870 break;
871 }
Michael J. Spencer20249a12010-10-21 03:16:25 +0000872
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000873 return Key;
874 }
875
876 data_type ReadData(internal_key_type, const unsigned char* d,
877 unsigned DataLen) {
878 using namespace clang::io;
879 unsigned NumDecls = ReadUnalignedLE16(d);
880 DeclID *Start = (DeclID *)d;
881 return std::make_pair(Start, Start + NumDecls);
882 }
883};
884
885} // end anonymous namespace
886
887/// \brief The on-disk hash table used for the DeclContext's Name lookup table.
888typedef OnDiskChainedHashTable<ASTDeclContextNameLookupTrait>
889 ASTDeclContextNameLookupTable;
890
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000891bool ASTReader::ReadDeclContextStorage(llvm::BitstreamCursor &Cursor,
892 const std::pair<uint64_t, uint64_t> &Offsets,
893 DeclContextInfo &Info) {
894 SavedStreamPosition SavedPosition(Cursor);
895 // First the lexical decls.
896 if (Offsets.first != 0) {
897 Cursor.JumpToBit(Offsets.first);
898
899 RecordData Record;
900 const char *Blob;
901 unsigned BlobLen;
902 unsigned Code = Cursor.ReadCode();
903 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
904 if (RecCode != DECL_CONTEXT_LEXICAL) {
905 Error("Expected lexical block");
906 return true;
907 }
908
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000909 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob);
910 Info.NumLexicalDecls = BlobLen / sizeof(KindDeclIDPair);
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000911 } else {
912 Info.LexicalDecls = 0;
913 Info.NumLexicalDecls = 0;
914 }
915
916 // Now the lookup table.
917 if (Offsets.second != 0) {
918 Cursor.JumpToBit(Offsets.second);
919
920 RecordData Record;
921 const char *Blob;
922 unsigned BlobLen;
923 unsigned Code = Cursor.ReadCode();
924 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
925 if (RecCode != DECL_CONTEXT_VISIBLE) {
926 Error("Expected visible lookup table block");
927 return true;
928 }
929 Info.NameLookupTableData
930 = ASTDeclContextNameLookupTable::Create(
931 (const unsigned char *)Blob + Record[0],
932 (const unsigned char *)Blob,
933 ASTDeclContextNameLookupTrait(*this));
Sebastian Redl0ea8f7f2010-08-24 00:50:00 +0000934 } else {
935 Info.NameLookupTableData = 0;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000936 }
937
938 return false;
939}
940
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000941void ASTReader::Error(const char *Msg) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000942 Diag(diag::err_fe_pch_malformed) << Msg;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000943}
944
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000945/// \brief Tell the AST listener about the predefines buffers in the chain.
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000946bool ASTReader::CheckPredefinesBuffers() {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000947 if (Listener)
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000948 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000949 ActualOriginalFileName,
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000950 SuggestedPredefines);
Douglas Gregore721f952009-04-28 18:58:38 +0000951 return false;
Douglas Gregore1d918e2009-04-10 23:10:45 +0000952}
953
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000954//===----------------------------------------------------------------------===//
955// Source Manager Deserialization
956//===----------------------------------------------------------------------===//
957
Douglas Gregorbd945002009-04-13 16:31:14 +0000958/// \brief Read the line table in the source manager block.
Sebastian Redlc3632732010-10-05 15:59:54 +0000959/// \returns true if there was an error.
960bool ASTReader::ParseLineTable(PerFileData &F,
961 llvm::SmallVectorImpl<uint64_t> &Record) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000962 unsigned Idx = 0;
963 LineTableInfo &LineTable = SourceMgr.getLineTable();
964
965 // Parse the file names
Douglas Gregorff0a9872009-04-13 17:12:42 +0000966 std::map<int, int> FileIDs;
967 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000968 // Extract the file name
969 unsigned FilenameLen = Record[Idx++];
970 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
971 Idx += FilenameLen;
Douglas Gregore650c8c2009-07-07 00:12:59 +0000972 MaybeAddSystemRootToFilename(Filename);
Mike Stump1eb44332009-09-09 15:08:12 +0000973 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
Douglas Gregorff0a9872009-04-13 17:12:42 +0000974 Filename.size());
Douglas Gregorbd945002009-04-13 16:31:14 +0000975 }
976
977 // Parse the line entries
978 std::vector<LineEntry> Entries;
979 while (Idx < Record.size()) {
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000980 int FID = Record[Idx++];
Douglas Gregorbd945002009-04-13 16:31:14 +0000981
982 // Extract the line entries
983 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000984 assert(NumEntries && "Numentries is 00000");
Douglas Gregorbd945002009-04-13 16:31:14 +0000985 Entries.clear();
986 Entries.reserve(NumEntries);
987 for (unsigned I = 0; I != NumEntries; ++I) {
988 unsigned FileOffset = Record[Idx++];
989 unsigned LineNo = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000990 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump1eb44332009-09-09 15:08:12 +0000991 SrcMgr::CharacteristicKind FileKind
Douglas Gregorbd945002009-04-13 16:31:14 +0000992 = (SrcMgr::CharacteristicKind)Record[Idx++];
993 unsigned IncludeOffset = Record[Idx++];
994 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
995 FileKind, IncludeOffset));
996 }
997 LineTable.AddEntry(FID, Entries);
998 }
999
1000 return false;
1001}
1002
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001003namespace {
1004
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001005class ASTStatData {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001006public:
1007 const bool hasStat;
1008 const ino_t ino;
1009 const dev_t dev;
1010 const mode_t mode;
1011 const time_t mtime;
1012 const off_t size;
Mike Stump1eb44332009-09-09 15:08:12 +00001013
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001014 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Mike Stump1eb44332009-09-09 15:08:12 +00001015 : hasStat(true), ino(i), dev(d), mode(mo), mtime(m), size(s) {}
1016
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001017 ASTStatData()
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001018 : hasStat(false), ino(0), dev(0), mode(0), mtime(0), size(0) {}
1019};
1020
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001021class ASTStatLookupTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001022 public:
1023 typedef const char *external_key_type;
1024 typedef const char *internal_key_type;
1025
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001026 typedef ASTStatData data_type;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001027
1028 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001029 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001030 }
1031
1032 static internal_key_type GetInternalKey(const char *path) { return path; }
1033
1034 static bool EqualKey(internal_key_type a, internal_key_type b) {
1035 return strcmp(a, b) == 0;
1036 }
1037
1038 static std::pair<unsigned, unsigned>
1039 ReadKeyDataLength(const unsigned char*& d) {
1040 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1041 unsigned DataLen = (unsigned) *d++;
1042 return std::make_pair(KeyLen + 1, DataLen);
1043 }
1044
1045 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
1046 return (const char *)d;
1047 }
1048
1049 static data_type ReadData(const internal_key_type, const unsigned char *d,
1050 unsigned /*DataLen*/) {
1051 using namespace clang::io;
1052
1053 if (*d++ == 1)
1054 return data_type();
1055
1056 ino_t ino = (ino_t) ReadUnalignedLE32(d);
1057 dev_t dev = (dev_t) ReadUnalignedLE32(d);
1058 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump1eb44332009-09-09 15:08:12 +00001059 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001060 off_t size = (off_t) ReadUnalignedLE64(d);
1061 return data_type(ino, dev, mode, mtime, size);
1062 }
1063};
1064
1065/// \brief stat() cache for precompiled headers.
1066///
1067/// This cache is very similar to the stat cache used by pretokenized
1068/// headers.
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001069class ASTStatCache : public StatSysCallCache {
1070 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001071 CacheTy *Cache;
1072
1073 unsigned &NumStatHits, &NumStatMisses;
Mike Stump1eb44332009-09-09 15:08:12 +00001074public:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001075 ASTStatCache(const unsigned char *Buckets,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001076 const unsigned char *Base,
1077 unsigned &NumStatHits,
Mike Stump1eb44332009-09-09 15:08:12 +00001078 unsigned &NumStatMisses)
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001079 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
1080 Cache = CacheTy::Create(Buckets, Base);
1081 }
1082
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001083 ~ASTStatCache() { delete Cache; }
Mike Stump1eb44332009-09-09 15:08:12 +00001084
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001085 int stat(const char *path, struct stat *buf) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001086 // Do the lookup for the file's data in the AST file.
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001087 CacheTy::iterator I = Cache->find(path);
1088
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001089 // If we don't get a hit in the AST file just forward to 'stat'.
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001090 if (I == Cache->end()) {
1091 ++NumStatMisses;
Douglas Gregor52e71082009-10-16 18:18:30 +00001092 return StatSysCallCache::stat(path, buf);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001093 }
Mike Stump1eb44332009-09-09 15:08:12 +00001094
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001095 ++NumStatHits;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001096 ASTStatData Data = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001097
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001098 if (!Data.hasStat)
1099 return 1;
1100
1101 buf->st_ino = Data.ino;
1102 buf->st_dev = Data.dev;
1103 buf->st_mtime = Data.mtime;
1104 buf->st_mode = Data.mode;
1105 buf->st_size = Data.size;
1106 return 0;
1107 }
1108};
1109} // end anonymous namespace
1110
1111
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001112/// \brief Read a source manager block
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001113ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(PerFileData &F) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001114 using namespace SrcMgr;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001115
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001116 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001117
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001118 // Set the source-location entry cursor to the current position in
1119 // the stream. This cursor will be used to read the contents of the
1120 // source manager block initially, and then lazily read
1121 // source-location entries as needed.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001122 SLocEntryCursor = F.Stream;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001123
1124 // The stream itself is going to skip over the source manager block.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001125 if (F.Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001126 Error("malformed block record in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001127 return Failure;
1128 }
1129
1130 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001131 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001132 Error("malformed source manager block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001133 return Failure;
1134 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001135
Douglas Gregor14f79002009-04-10 03:52:48 +00001136 RecordData Record;
1137 while (true) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001138 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregor14f79002009-04-10 03:52:48 +00001139 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001140 if (SLocEntryCursor.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001141 Error("error at end of Source Manager block in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001142 return Failure;
1143 }
Douglas Gregore1d918e2009-04-10 23:10:45 +00001144 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +00001145 }
Mike Stump1eb44332009-09-09 15:08:12 +00001146
Douglas Gregor14f79002009-04-10 03:52:48 +00001147 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1148 // No known subblocks, always skip them.
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001149 SLocEntryCursor.ReadSubBlockID();
1150 if (SLocEntryCursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001151 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00001152 return Failure;
1153 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001154 continue;
1155 }
Mike Stump1eb44332009-09-09 15:08:12 +00001156
Douglas Gregor14f79002009-04-10 03:52:48 +00001157 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001158 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregor14f79002009-04-10 03:52:48 +00001159 continue;
1160 }
Mike Stump1eb44332009-09-09 15:08:12 +00001161
Douglas Gregor14f79002009-04-10 03:52:48 +00001162 // Read a record.
1163 const char *BlobStart;
1164 unsigned BlobLen;
1165 Record.clear();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001166 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001167 default: // Default behavior: ignore.
1168 break;
1169
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001170 case SM_LINE_TABLE:
Sebastian Redlc3632732010-10-05 15:59:54 +00001171 if (ParseLineTable(F, Record))
Douglas Gregorbd945002009-04-13 16:31:14 +00001172 return Failure;
Chris Lattner2c78b872009-04-14 23:22:57 +00001173 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001174
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001175 case SM_SLOC_FILE_ENTRY:
1176 case SM_SLOC_BUFFER_ENTRY:
1177 case SM_SLOC_INSTANTIATION_ENTRY:
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001178 // Once we hit one of the source location entries, we're done.
1179 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +00001180 }
1181 }
1182}
1183
Sebastian Redl190faf72010-07-20 21:50:20 +00001184/// \brief Get a cursor that's correctly positioned for reading the source
1185/// location entry with the given ID.
Sebastian Redlc3632732010-10-05 15:59:54 +00001186ASTReader::PerFileData *ASTReader::SLocCursorForID(unsigned ID) {
Sebastian Redl190faf72010-07-20 21:50:20 +00001187 assert(ID != 0 && ID <= TotalNumSLocEntries &&
1188 "SLocCursorForID should only be called for real IDs.");
1189
1190 ID -= 1;
1191 PerFileData *F = 0;
1192 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1193 F = Chain[N - I - 1];
1194 if (ID < F->LocalNumSLocEntries)
1195 break;
1196 ID -= F->LocalNumSLocEntries;
1197 }
1198 assert(F && F->LocalNumSLocEntries > ID && "Chain corrupted");
1199
1200 F->SLocEntryCursor.JumpToBit(F->SLocOffsets[ID]);
Sebastian Redlc3632732010-10-05 15:59:54 +00001201 return F;
Sebastian Redl190faf72010-07-20 21:50:20 +00001202}
1203
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001204/// \brief Read in the source location entry with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001205ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(unsigned ID) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001206 if (ID == 0)
1207 return Success;
1208
1209 if (ID > TotalNumSLocEntries) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001210 Error("source location entry ID out-of-range for AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001211 return Failure;
1212 }
1213
Sebastian Redlc3632732010-10-05 15:59:54 +00001214 PerFileData *F = SLocCursorForID(ID);
1215 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001216
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001217 ++NumSLocEntriesRead;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001218 unsigned Code = SLocEntryCursor.ReadCode();
1219 if (Code == llvm::bitc::END_BLOCK ||
1220 Code == llvm::bitc::ENTER_SUBBLOCK ||
1221 Code == llvm::bitc::DEFINE_ABBREV) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001222 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001223 return Failure;
1224 }
1225
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001226 RecordData Record;
1227 const char *BlobStart;
1228 unsigned BlobLen;
1229 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1230 default:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001231 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001232 return Failure;
1233
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001234 case SM_SLOC_FILE_ENTRY: {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001235 std::string Filename(BlobStart, BlobStart + BlobLen);
1236 MaybeAddSystemRootToFilename(Filename);
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +00001237 const FileEntry *File = FileMgr.getFile(Filename, FileSystemOpts);
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001238 if (File == 0) {
1239 std::string ErrorStr = "could not find file '";
Douglas Gregore650c8c2009-07-07 00:12:59 +00001240 ErrorStr += Filename;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001241 ErrorStr += "' referenced by AST file";
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001242 Error(ErrorStr.c_str());
1243 return Failure;
1244 }
Mike Stump1eb44332009-09-09 15:08:12 +00001245
Douglas Gregor2d52be52010-03-21 22:49:54 +00001246 if (Record.size() < 10) {
Ted Kremenek1857f622010-03-18 21:23:05 +00001247 Error("source location entry is incorrect");
1248 return Failure;
1249 }
1250
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001251 if (!DisableValidation &&
1252 ((off_t)Record[4] != File->getSize()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001253#if !defined(LLVM_ON_WIN32)
1254 // In our regression testing, the Windows file system seems to
1255 // have inconsistent modification times that sometimes
1256 // erroneously trigger this error-handling path.
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001257 || (time_t)Record[5] != File->getModificationTime()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001258#endif
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001259 )) {
Douglas Gregor2d52be52010-03-21 22:49:54 +00001260 Diag(diag::err_fe_pch_file_modified)
1261 << Filename;
1262 return Failure;
1263 }
1264
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001265 FileID FID = SourceMgr.createFileID(File,
Sebastian Redlc3632732010-10-05 15:59:54 +00001266 ReadSourceLocation(*F, Record[1]),
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001267 (SrcMgr::CharacteristicKind)Record[2],
1268 ID, Record[0]);
1269 if (Record[3])
1270 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile())
1271 .setHasLineDirectives();
1272
Douglas Gregor12fab312010-03-16 16:35:32 +00001273 // Reconstruct header-search information for this file.
1274 HeaderFileInfo HFI;
Douglas Gregor2d52be52010-03-21 22:49:54 +00001275 HFI.isImport = Record[6];
1276 HFI.DirInfo = Record[7];
1277 HFI.NumIncludes = Record[8];
1278 HFI.ControllingMacroID = Record[9];
Douglas Gregor12fab312010-03-16 16:35:32 +00001279 if (Listener)
1280 Listener->ReadHeaderFileInfo(HFI, File->getUID());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001281 break;
1282 }
1283
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001284 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001285 const char *Name = BlobStart;
1286 unsigned Offset = Record[0];
1287 unsigned Code = SLocEntryCursor.ReadCode();
1288 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001289 unsigned RecCode
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001290 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001291
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001292 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001293 Error("AST record has invalid code");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001294 return Failure;
1295 }
1296
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001297 llvm::MemoryBuffer *Buffer
Chris Lattnera0a270c2010-04-05 22:42:27 +00001298 = llvm::MemoryBuffer::getMemBuffer(llvm::StringRef(BlobStart, BlobLen - 1),
1299 Name);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001300 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID, Offset);
Mike Stump1eb44332009-09-09 15:08:12 +00001301
Douglas Gregor92b059e2009-04-28 20:33:11 +00001302 if (strcmp(Name, "<built-in>") == 0) {
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +00001303 PCHPredefinesBlock Block = {
1304 BufferID,
1305 llvm::StringRef(BlobStart, BlobLen - 1)
1306 };
1307 PCHPredefinesBuffers.push_back(Block);
Douglas Gregor92b059e2009-04-28 20:33:11 +00001308 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001309
1310 break;
1311 }
1312
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001313 case SM_SLOC_INSTANTIATION_ENTRY: {
Sebastian Redlc3632732010-10-05 15:59:54 +00001314 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001315 SourceMgr.createInstantiationLoc(SpellingLoc,
Sebastian Redlc3632732010-10-05 15:59:54 +00001316 ReadSourceLocation(*F, Record[2]),
1317 ReadSourceLocation(*F, Record[3]),
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001318 Record[4],
1319 ID,
1320 Record[0]);
1321 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001322 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001323 }
1324
1325 return Success;
1326}
1327
Chris Lattner6367f6d2009-04-27 01:05:14 +00001328/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1329/// specified cursor. Read the abbreviations that are at the top of the block
1330/// and then leave the cursor pointing into the block.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001331bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattner6367f6d2009-04-27 01:05:14 +00001332 unsigned BlockID) {
1333 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001334 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001335 return Failure;
1336 }
Mike Stump1eb44332009-09-09 15:08:12 +00001337
Chris Lattner6367f6d2009-04-27 01:05:14 +00001338 while (true) {
Douglas Gregorecdcb882010-10-20 22:00:55 +00001339 uint64_t Offset = Cursor.GetCurrentBitNo();
Chris Lattner6367f6d2009-04-27 01:05:14 +00001340 unsigned Code = Cursor.ReadCode();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001341
Chris Lattner6367f6d2009-04-27 01:05:14 +00001342 // We expect all abbrevs to be at the start of the block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001343 if (Code != llvm::bitc::DEFINE_ABBREV) {
1344 Cursor.JumpToBit(Offset);
Chris Lattner6367f6d2009-04-27 01:05:14 +00001345 return false;
Douglas Gregorecdcb882010-10-20 22:00:55 +00001346 }
Chris Lattner6367f6d2009-04-27 01:05:14 +00001347 Cursor.ReadAbbrevRecord();
1348 }
1349}
1350
Sebastian Redlc3632732010-10-05 15:59:54 +00001351void ASTReader::ReadMacroRecord(PerFileData &F, uint64_t Offset) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001352 assert(PP && "Forgot to set Preprocessor ?");
Douglas Gregorecdcb882010-10-20 22:00:55 +00001353 llvm::BitstreamCursor &Stream = F.MacroCursor;
Mike Stump1eb44332009-09-09 15:08:12 +00001354
Douglas Gregor37e26842009-04-21 23:56:24 +00001355 // Keep track of where we are in the stream, then jump back there
1356 // after reading this macro.
1357 SavedStreamPosition SavedPosition(Stream);
1358
1359 Stream.JumpToBit(Offset);
1360 RecordData Record;
1361 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1362 MacroInfo *Macro = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001363
Douglas Gregor37e26842009-04-21 23:56:24 +00001364 while (true) {
1365 unsigned Code = Stream.ReadCode();
1366 switch (Code) {
1367 case llvm::bitc::END_BLOCK:
1368 return;
1369
1370 case llvm::bitc::ENTER_SUBBLOCK:
1371 // No known subblocks, always skip them.
1372 Stream.ReadSubBlockID();
1373 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001374 Error("malformed block record in AST file");
Douglas Gregor37e26842009-04-21 23:56:24 +00001375 return;
1376 }
1377 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001378
Douglas Gregor37e26842009-04-21 23:56:24 +00001379 case llvm::bitc::DEFINE_ABBREV:
1380 Stream.ReadAbbrevRecord();
1381 continue;
1382 default: break;
1383 }
1384
1385 // Read a record.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001386 const char *BlobStart = 0;
1387 unsigned BlobLen = 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001388 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001389 PreprocessorRecordTypes RecType =
Michael J. Spencer20249a12010-10-21 03:16:25 +00001390 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart,
Douglas Gregorecdcb882010-10-20 22:00:55 +00001391 BlobLen);
Douglas Gregor37e26842009-04-21 23:56:24 +00001392 switch (RecType) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001393 case PP_MACRO_OBJECT_LIKE:
1394 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001395 // If we already have a macro, that means that we've hit the end
1396 // of the definition of the macro we were looking for. We're
1397 // done.
1398 if (Macro)
1399 return;
1400
1401 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1402 if (II == 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001403 Error("macro must have a name in AST file");
Douglas Gregor37e26842009-04-21 23:56:24 +00001404 return;
1405 }
Sebastian Redlc3632732010-10-05 15:59:54 +00001406 SourceLocation Loc = ReadSourceLocation(F, Record[1]);
Douglas Gregor37e26842009-04-21 23:56:24 +00001407 bool isUsed = Record[2];
Mike Stump1eb44332009-09-09 15:08:12 +00001408
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001409 MacroInfo *MI = PP->AllocateMacroInfo(Loc);
Douglas Gregor37e26842009-04-21 23:56:24 +00001410 MI->setIsUsed(isUsed);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001411 MI->setIsFromAST();
Mike Stump1eb44332009-09-09 15:08:12 +00001412
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001413 unsigned NextIndex = 3;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001414 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregor37e26842009-04-21 23:56:24 +00001415 // Decode function-like macro info.
1416 bool isC99VarArgs = Record[3];
1417 bool isGNUVarArgs = Record[4];
1418 MacroArgs.clear();
1419 unsigned NumArgs = Record[5];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001420 NextIndex = 6 + NumArgs;
Douglas Gregor37e26842009-04-21 23:56:24 +00001421 for (unsigned i = 0; i != NumArgs; ++i)
1422 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1423
1424 // Install function-like macro info.
1425 MI->setIsFunctionLike();
1426 if (isC99VarArgs) MI->setIsC99Varargs();
1427 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor75fdb232009-05-22 22:45:36 +00001428 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001429 PP->getPreprocessorAllocator());
Douglas Gregor37e26842009-04-21 23:56:24 +00001430 }
1431
1432 // Finally, install the macro.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001433 PP->setMacroInfo(II, MI);
Douglas Gregor37e26842009-04-21 23:56:24 +00001434
1435 // Remember that we saw this macro last so that we add the tokens that
1436 // form its body to it.
1437 Macro = MI;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001438
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001439 if (NextIndex + 1 == Record.size() && PP->getPreprocessingRecord()) {
1440 // We have a macro definition. Load it now.
1441 PP->getPreprocessingRecord()->RegisterMacroDefinition(Macro,
1442 getMacroDefinition(Record[NextIndex]));
1443 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001444
Douglas Gregor37e26842009-04-21 23:56:24 +00001445 ++NumMacrosRead;
1446 break;
1447 }
Mike Stump1eb44332009-09-09 15:08:12 +00001448
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001449 case PP_TOKEN: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001450 // If we see a TOKEN before a PP_MACRO_*, then the file is
1451 // erroneous, just pretend we didn't see this.
1452 if (Macro == 0) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001453
Douglas Gregor37e26842009-04-21 23:56:24 +00001454 Token Tok;
1455 Tok.startToken();
Sebastian Redlc3632732010-10-05 15:59:54 +00001456 Tok.setLocation(ReadSourceLocation(F, Record[0]));
Douglas Gregor37e26842009-04-21 23:56:24 +00001457 Tok.setLength(Record[1]);
1458 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1459 Tok.setIdentifierInfo(II);
1460 Tok.setKind((tok::TokenKind)Record[3]);
1461 Tok.setFlag((Token::TokenFlags)Record[4]);
1462 Macro->AddTokenToBody(Tok);
1463 break;
1464 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001465
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001466 case PP_MACRO_INSTANTIATION: {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001467 // If we already have a macro, that means that we've hit the end
1468 // of the definition of the macro we were looking for. We're
1469 // done.
1470 if (Macro)
1471 return;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001472
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001473 if (!PP->getPreprocessingRecord()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001474 Error("missing preprocessing record in AST file");
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001475 return;
1476 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001477
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001478 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1479 if (PPRec.getPreprocessedEntity(Record[0]))
1480 return;
1481
1482 MacroInstantiation *MI
1483 = new (PPRec) MacroInstantiation(DecodeIdentifierInfo(Record[3]),
Sebastian Redlc3632732010-10-05 15:59:54 +00001484 SourceRange(ReadSourceLocation(F, Record[1]),
1485 ReadSourceLocation(F, Record[2])),
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001486 getMacroDefinition(Record[4]));
1487 PPRec.SetPreallocatedEntity(Record[0], MI);
1488 return;
1489 }
1490
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001491 case PP_MACRO_DEFINITION: {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001492 // If we already have a macro, that means that we've hit the end
1493 // of the definition of the macro we were looking for. We're
1494 // done.
1495 if (Macro)
1496 return;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001497
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001498 if (!PP->getPreprocessingRecord()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001499 Error("missing preprocessing record in AST file");
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001500 return;
1501 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001502
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001503 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1504 if (PPRec.getPreprocessedEntity(Record[0]))
1505 return;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001506
Douglas Gregor77424bc2010-10-02 19:29:26 +00001507 if (Record[1] > MacroDefinitionsLoaded.size()) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001508 Error("out-of-bounds macro definition record");
1509 return;
1510 }
1511
Douglas Gregor77424bc2010-10-02 19:29:26 +00001512 // Decode the identifier info and then check again; if the macro is
Michael J. Spencer20249a12010-10-21 03:16:25 +00001513 // still defined and associated with the identifier,
Douglas Gregor77424bc2010-10-02 19:29:26 +00001514 IdentifierInfo *II = DecodeIdentifierInfo(Record[4]);
1515 if (!MacroDefinitionsLoaded[Record[1] - 1]) {
1516 MacroDefinition *MD
1517 = new (PPRec) MacroDefinition(II,
Sebastian Redlc3632732010-10-05 15:59:54 +00001518 ReadSourceLocation(F, Record[5]),
Douglas Gregorb1a7d9a2010-10-01 20:33:34 +00001519 SourceRange(
Sebastian Redlc3632732010-10-05 15:59:54 +00001520 ReadSourceLocation(F, Record[2]),
1521 ReadSourceLocation(F, Record[3])));
Michael J. Spencer20249a12010-10-21 03:16:25 +00001522
Douglas Gregor77424bc2010-10-02 19:29:26 +00001523 PPRec.SetPreallocatedEntity(Record[0], MD);
1524 MacroDefinitionsLoaded[Record[1] - 1] = MD;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001525
Douglas Gregor77424bc2010-10-02 19:29:26 +00001526 if (DeserializationListener)
1527 DeserializationListener->MacroDefinitionRead(Record[1], MD);
1528 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001529
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001530 return;
1531 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001532
Douglas Gregorecdcb882010-10-20 22:00:55 +00001533 case PP_INCLUSION_DIRECTIVE: {
1534 // If we already have a macro, that means that we've hit the end
1535 // of the definition of the macro we were looking for. We're
1536 // done.
1537 if (Macro)
1538 return;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001539
Douglas Gregorecdcb882010-10-20 22:00:55 +00001540 if (!PP->getPreprocessingRecord()) {
1541 Error("missing preprocessing record in AST file");
1542 return;
1543 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001544
Douglas Gregorecdcb882010-10-20 22:00:55 +00001545 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord();
1546 if (PPRec.getPreprocessedEntity(Record[0]))
1547 return;
1548
1549 const char *FullFileNameStart = BlobStart + Record[3];
Michael J. Spencer20249a12010-10-21 03:16:25 +00001550 const FileEntry *File
Douglas Gregorecdcb882010-10-20 22:00:55 +00001551 = PP->getFileManager().getFile(FullFileNameStart,
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +00001552 FullFileNameStart + (BlobLen - Record[3]),
1553 FileSystemOpts);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001554
Douglas Gregorecdcb882010-10-20 22:00:55 +00001555 // FIXME: Stable encoding
1556 InclusionDirective::InclusionKind Kind
1557 = static_cast<InclusionDirective::InclusionKind>(Record[5]);
1558 InclusionDirective *ID
Douglas Gregor4ab829c2010-11-01 15:03:47 +00001559 = new (PPRec) InclusionDirective(PPRec, Kind,
Douglas Gregorecdcb882010-10-20 22:00:55 +00001560 llvm::StringRef(BlobStart, Record[3]),
1561 Record[4],
1562 File,
1563 SourceRange(ReadSourceLocation(F, Record[1]),
1564 ReadSourceLocation(F, Record[2])));
1565 PPRec.SetPreallocatedEntity(Record[0], ID);
1566 return;
1567 }
Sebastian Redlb57a6242010-09-27 22:18:47 +00001568 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001569 }
1570}
1571
Douglas Gregor295a2a62010-10-30 00:23:06 +00001572void ASTReader::SetIdentifierIsMacro(IdentifierInfo *II, PerFileData &F,
1573 uint64_t Offset) {
1574 // Note that this identifier has a macro definition.
1575 II->setHasMacroDefinition(true);
1576
1577 // Adjust the offset based on our position in the chain.
1578 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1579 if (Chain[I] == &F)
1580 break;
1581
1582 Offset += Chain[I]->SizeInBits;
1583 }
1584
1585 UnreadMacroRecordOffsets[II] = Offset;
1586}
1587
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001588void ASTReader::ReadDefinedMacros() {
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001589 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redlc3632732010-10-05 15:59:54 +00001590 PerFileData &F = *Chain[N - I - 1];
1591 llvm::BitstreamCursor &MacroCursor = F.MacroCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001592
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001593 // If there was no preprocessor block, skip this file.
1594 if (!MacroCursor.getBitStreamReader())
1595 continue;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001596
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001597 llvm::BitstreamCursor Cursor = MacroCursor;
Douglas Gregorecdcb882010-10-20 22:00:55 +00001598 Cursor.JumpToBit(F.MacroStartOffset);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001599
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001600 RecordData Record;
1601 while (true) {
Sebastian Redledadecc2010-09-28 02:55:49 +00001602 uint64_t Offset = Cursor.GetCurrentBitNo();
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001603 unsigned Code = Cursor.ReadCode();
Douglas Gregorecdcb882010-10-20 22:00:55 +00001604 if (Code == llvm::bitc::END_BLOCK)
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001605 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001606
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001607 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1608 // No known subblocks, always skip them.
1609 Cursor.ReadSubBlockID();
1610 if (Cursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001611 Error("malformed block record in AST file");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001612 return;
1613 }
1614 continue;
1615 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001616
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001617 if (Code == llvm::bitc::DEFINE_ABBREV) {
1618 Cursor.ReadAbbrevRecord();
1619 continue;
1620 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001621
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001622 // Read a record.
1623 const char *BlobStart;
1624 unsigned BlobLen;
1625 Record.clear();
1626 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1627 default: // Default behavior: ignore.
1628 break;
Douglas Gregor88a35862010-01-04 19:18:44 +00001629
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001630 case PP_MACRO_OBJECT_LIKE:
1631 case PP_MACRO_FUNCTION_LIKE:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001632 DecodeIdentifierInfo(Record[0]);
1633 break;
1634
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001635 case PP_TOKEN:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001636 // Ignore tokens.
1637 break;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001638
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001639 case PP_MACRO_INSTANTIATION:
1640 case PP_MACRO_DEFINITION:
Douglas Gregorecdcb882010-10-20 22:00:55 +00001641 case PP_INCLUSION_DIRECTIVE:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001642 // Read the macro record.
Sebastian Redledadecc2010-09-28 02:55:49 +00001643 // FIXME: That's a stupid way to do this. We should reuse this cursor.
Sebastian Redlc3632732010-10-05 15:59:54 +00001644 ReadMacroRecord(F, Offset);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001645 break;
1646 }
Douglas Gregor88a35862010-01-04 19:18:44 +00001647 }
1648 }
Douglas Gregor295a2a62010-10-30 00:23:06 +00001649
1650 // Drain the unread macro-record offsets map.
1651 while (!UnreadMacroRecordOffsets.empty())
1652 LoadMacroDefinition(UnreadMacroRecordOffsets.begin());
1653}
1654
1655void ASTReader::LoadMacroDefinition(
1656 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos) {
1657 assert(Pos != UnreadMacroRecordOffsets.end() && "Unknown macro definition");
1658 PerFileData *F = 0;
1659 uint64_t Offset = Pos->second;
1660 UnreadMacroRecordOffsets.erase(Pos);
1661
1662 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1663 if (Offset < Chain[I]->SizeInBits) {
1664 F = Chain[I];
1665 break;
1666 }
1667
1668 Offset -= Chain[I]->SizeInBits;
1669 }
1670 if (!F) {
1671 Error("Malformed macro record offset");
1672 return;
1673 }
1674
1675 ReadMacroRecord(*F, Offset);
1676}
1677
1678void ASTReader::LoadMacroDefinition(IdentifierInfo *II) {
1679 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos
1680 = UnreadMacroRecordOffsets.find(II);
1681 LoadMacroDefinition(Pos);
Douglas Gregor88a35862010-01-04 19:18:44 +00001682}
1683
Sebastian Redlf73c93f2010-09-15 19:54:06 +00001684MacroDefinition *ASTReader::getMacroDefinition(MacroID ID) {
Douglas Gregor77424bc2010-10-02 19:29:26 +00001685 if (ID == 0 || ID > MacroDefinitionsLoaded.size())
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001686 return 0;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001687
Douglas Gregor77424bc2010-10-02 19:29:26 +00001688 if (!MacroDefinitionsLoaded[ID - 1]) {
1689 unsigned Index = ID - 1;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001690 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1691 PerFileData &F = *Chain[N - I - 1];
1692 if (Index < F.LocalNumMacroDefinitions) {
Sebastian Redlc3632732010-10-05 15:59:54 +00001693 ReadMacroRecord(F, F.MacroDefinitionOffsets[Index]);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001694 break;
1695 }
1696 Index -= F.LocalNumMacroDefinitions;
1697 }
Douglas Gregor77424bc2010-10-02 19:29:26 +00001698 assert(MacroDefinitionsLoaded[ID - 1] && "Broken chain");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001699 }
1700
Douglas Gregor77424bc2010-10-02 19:29:26 +00001701 return MacroDefinitionsLoaded[ID - 1];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001702}
1703
Douglas Gregore650c8c2009-07-07 00:12:59 +00001704/// \brief If we are loading a relocatable PCH file, and the filename is
1705/// not an absolute path, add the system root to the beginning of the file
1706/// name.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001707void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001708 // If this is not a relocatable PCH file, there's nothing to do.
1709 if (!RelocatablePCH)
1710 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001711
Daniel Dunbard5b21972009-11-18 19:50:41 +00001712 if (Filename.empty() || llvm::sys::Path(Filename).isAbsolute())
Douglas Gregore650c8c2009-07-07 00:12:59 +00001713 return;
1714
Douglas Gregore650c8c2009-07-07 00:12:59 +00001715 if (isysroot == 0) {
1716 // If no system root was given, default to '/'
1717 Filename.insert(Filename.begin(), '/');
1718 return;
1719 }
Mike Stump1eb44332009-09-09 15:08:12 +00001720
Douglas Gregore650c8c2009-07-07 00:12:59 +00001721 unsigned Length = strlen(isysroot);
1722 if (isysroot[Length - 1] != '/')
1723 Filename.insert(Filename.begin(), '/');
Mike Stump1eb44332009-09-09 15:08:12 +00001724
Douglas Gregore650c8c2009-07-07 00:12:59 +00001725 Filename.insert(Filename.begin(), isysroot, isysroot + Length);
1726}
1727
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001728ASTReader::ASTReadResult
Sebastian Redl571db7f2010-08-18 23:56:56 +00001729ASTReader::ReadASTBlock(PerFileData &F) {
Sebastian Redl9137a522010-07-16 17:50:48 +00001730 llvm::BitstreamCursor &Stream = F.Stream;
1731
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001732 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001733 Error("malformed block record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001734 return Failure;
1735 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001736
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001737 // Read all of the records and blocks for the ASt file.
Douglas Gregor8038d512009-04-10 17:25:41 +00001738 RecordData Record;
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001739 bool First = true;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001740 while (!Stream.AtEndOfStream()) {
1741 unsigned Code = Stream.ReadCode();
1742 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001743 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001744 Error("error at end of module block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001745 return Failure;
1746 }
Chris Lattner7356a312009-04-11 21:15:38 +00001747
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001748 return Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001749 }
1750
1751 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1752 switch (Stream.ReadSubBlockID()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001753 case DECLTYPES_BLOCK_ID:
Chris Lattner6367f6d2009-04-27 01:05:14 +00001754 // We lazily load the decls block, but we want to set up the
1755 // DeclsCursor cursor to point into it. Clone our current bitcode
1756 // cursor to it, enter the block and read the abbrevs in that block.
1757 // With the main cursor, we just skip over it.
Sebastian Redl9137a522010-07-16 17:50:48 +00001758 F.DeclsCursor = Stream;
Chris Lattner6367f6d2009-04-27 01:05:14 +00001759 if (Stream.SkipBlock() || // Skip with the main cursor.
1760 // Read the abbrevs.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001761 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001762 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001763 return Failure;
1764 }
1765 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001766
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00001767 case DECL_UPDATES_BLOCK_ID:
1768 if (Stream.SkipBlock()) {
1769 Error("malformed block record in AST file");
1770 return Failure;
1771 }
1772 break;
1773
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001774 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl9137a522010-07-16 17:50:48 +00001775 F.MacroCursor = Stream;
Douglas Gregor88a35862010-01-04 19:18:44 +00001776 if (PP)
1777 PP->setExternalSource(this);
1778
Douglas Gregorecdcb882010-10-20 22:00:55 +00001779 if (Stream.SkipBlock() ||
1780 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001781 Error("malformed block record in AST file");
Chris Lattner7356a312009-04-11 21:15:38 +00001782 return Failure;
1783 }
Douglas Gregorecdcb882010-10-20 22:00:55 +00001784 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
Chris Lattner7356a312009-04-11 21:15:38 +00001785 break;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00001786
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001787 case SOURCE_MANAGER_BLOCK_ID:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001788 switch (ReadSourceManagerBlock(F)) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00001789 case Success:
1790 break;
1791
1792 case Failure:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001793 Error("malformed source manager block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001794 return Failure;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001795
1796 case IgnorePCH:
1797 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001798 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001799 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001800 }
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001801 First = false;
Douglas Gregor8038d512009-04-10 17:25:41 +00001802 continue;
1803 }
1804
1805 if (Code == llvm::bitc::DEFINE_ABBREV) {
1806 Stream.ReadAbbrevRecord();
1807 continue;
1808 }
1809
1810 // Read and process a record.
1811 Record.clear();
Douglas Gregor2bec0412009-04-10 21:16:55 +00001812 const char *BlobStart = 0;
1813 unsigned BlobLen = 0;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001814 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00001815 &BlobStart, &BlobLen)) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001816 default: // Default behavior: ignore.
1817 break;
1818
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001819 case METADATA: {
1820 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1821 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001822 : diag::warn_pch_version_too_new);
1823 return IgnorePCH;
1824 }
1825
1826 RelocatablePCH = Record[4];
1827 if (Listener) {
1828 std::string TargetTriple(BlobStart, BlobLen);
1829 if (Listener->ReadTargetTriple(TargetTriple))
1830 return IgnorePCH;
1831 }
1832 break;
1833 }
1834
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001835 case CHAINED_METADATA: {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001836 if (!First) {
1837 Error("CHAINED_METADATA is not first record in block");
1838 return Failure;
1839 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001840 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1841 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001842 : diag::warn_pch_version_too_new);
1843 return IgnorePCH;
1844 }
1845
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00001846 // Load the chained file, which is always a PCH file.
1847 switch(ReadASTCore(llvm::StringRef(BlobStart, BlobLen), PCH)) {
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001848 case Failure: return Failure;
1849 // If we have to ignore the dependency, we'll have to ignore this too.
1850 case IgnorePCH: return IgnorePCH;
1851 case Success: break;
1852 }
1853 break;
1854 }
1855
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001856 case TYPE_OFFSET:
Sebastian Redl12d6da02010-07-19 22:06:55 +00001857 if (F.LocalNumTypes != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001858 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001859 return Failure;
1860 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00001861 F.TypeOffsets = (const uint32_t *)BlobStart;
1862 F.LocalNumTypes = Record[0];
Douglas Gregor8038d512009-04-10 17:25:41 +00001863 break;
1864
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001865 case DECL_OFFSET:
Sebastian Redl12d6da02010-07-19 22:06:55 +00001866 if (F.LocalNumDecls != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001867 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001868 return Failure;
1869 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00001870 F.DeclOffsets = (const uint32_t *)BlobStart;
1871 F.LocalNumDecls = Record[0];
Douglas Gregor8038d512009-04-10 17:25:41 +00001872 break;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001873
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001874 case TU_UPDATE_LEXICAL: {
Sebastian Redld692af72010-07-27 18:24:41 +00001875 DeclContextInfo Info = {
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001876 /* No visible information */ 0,
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001877 reinterpret_cast<const KindDeclIDPair *>(BlobStart),
1878 BlobLen / sizeof(KindDeclIDPair)
Sebastian Redld692af72010-07-27 18:24:41 +00001879 };
Douglas Gregor3747ee72010-10-01 01:18:02 +00001880 DeclContextOffsets[Context ? Context->getTranslationUnitDecl() : 0]
1881 .push_back(Info);
Sebastian Redld692af72010-07-27 18:24:41 +00001882 break;
1883 }
1884
Sebastian Redle1dde812010-08-24 00:50:04 +00001885 case UPDATE_VISIBLE: {
1886 serialization::DeclID ID = Record[0];
1887 void *Table = ASTDeclContextNameLookupTable::Create(
1888 (const unsigned char *)BlobStart + Record[1],
1889 (const unsigned char *)BlobStart,
1890 ASTDeclContextNameLookupTrait(*this));
Douglas Gregor3747ee72010-10-01 01:18:02 +00001891 if (ID == 1 && Context) { // Is it the TU?
Sebastian Redle1dde812010-08-24 00:50:04 +00001892 DeclContextInfo Info = {
1893 Table, /* No lexical inforamtion */ 0, 0
1894 };
1895 DeclContextOffsets[Context->getTranslationUnitDecl()].push_back(Info);
1896 } else
1897 PendingVisibleUpdates[ID].push_back(Table);
1898 break;
1899 }
1900
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001901 case REDECLS_UPDATE_LATEST: {
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00001902 assert(Record.size() % 2 == 0 && "Expected pairs of DeclIDs");
1903 for (unsigned i = 0, e = Record.size(); i < e; i += 2) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001904 DeclID First = Record[i], Latest = Record[i+1];
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00001905 assert((FirstLatestDeclIDs.find(First) == FirstLatestDeclIDs.end() ||
1906 Latest > FirstLatestDeclIDs[First]) &&
1907 "The new latest is supposed to come after the previous latest");
1908 FirstLatestDeclIDs[First] = Latest;
1909 }
1910 break;
1911 }
1912
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001913 case LANGUAGE_OPTIONS:
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001914 if (ParseLanguageOptions(Record) && !DisableValidation)
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001915 return IgnorePCH;
1916 break;
Douglas Gregor2bec0412009-04-10 21:16:55 +00001917
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001918 case IDENTIFIER_TABLE:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001919 F.IdentifierTableData = BlobStart;
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001920 if (Record[0]) {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001921 F.IdentifierLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001922 = ASTIdentifierLookupTable::Create(
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001923 (const unsigned char *)F.IdentifierTableData + Record[0],
1924 (const unsigned char *)F.IdentifierTableData,
Sebastian Redlc3632732010-10-05 15:59:54 +00001925 ASTIdentifierLookupTrait(*this, F));
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00001926 if (PP)
1927 PP->getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001928 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001929 break;
1930
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001931 case IDENTIFIER_OFFSET:
Sebastian Redl2da08f92010-07-19 22:28:42 +00001932 if (F.LocalNumIdentifiers != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001933 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00001934 return Failure;
1935 }
Sebastian Redl2da08f92010-07-19 22:28:42 +00001936 F.IdentifierOffsets = (const uint32_t *)BlobStart;
1937 F.LocalNumIdentifiers = Record[0];
Douglas Gregorafaf3082009-04-11 00:14:32 +00001938 break;
Douglas Gregorfdd01722009-04-14 00:24:19 +00001939
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001940 case EXTERNAL_DEFINITIONS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001941 // Optimization for the first block.
1942 if (ExternalDefinitions.empty())
1943 ExternalDefinitions.swap(Record);
1944 else
1945 ExternalDefinitions.insert(ExternalDefinitions.end(),
1946 Record.begin(), Record.end());
Douglas Gregorfdd01722009-04-14 00:24:19 +00001947 break;
Douglas Gregor3e1af842009-04-17 22:13:46 +00001948
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001949 case SPECIAL_TYPES:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001950 // Optimization for the first block
1951 if (SpecialTypes.empty())
1952 SpecialTypes.swap(Record);
1953 else
1954 SpecialTypes.insert(SpecialTypes.end(), Record.begin(), Record.end());
Douglas Gregorad1de002009-04-18 05:55:16 +00001955 break;
1956
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001957 case STATISTICS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001958 TotalNumStatements += Record[0];
1959 TotalNumMacros += Record[1];
1960 TotalLexicalDeclContexts += Record[2];
1961 TotalVisibleDeclContexts += Record[3];
Douglas Gregor3e1af842009-04-17 22:13:46 +00001962 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001963
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001964 case TENTATIVE_DEFINITIONS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001965 // Optimization for the first block.
1966 if (TentativeDefinitions.empty())
1967 TentativeDefinitions.swap(Record);
1968 else
1969 TentativeDefinitions.insert(TentativeDefinitions.end(),
1970 Record.begin(), Record.end());
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00001971 break;
Douglas Gregor14c22f22009-04-22 22:18:58 +00001972
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001973 case UNUSED_FILESCOPED_DECLS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001974 // Optimization for the first block.
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00001975 if (UnusedFileScopedDecls.empty())
1976 UnusedFileScopedDecls.swap(Record);
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001977 else
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00001978 UnusedFileScopedDecls.insert(UnusedFileScopedDecls.end(),
1979 Record.begin(), Record.end());
Tanya Lattnere6bbc012010-02-12 00:07:30 +00001980 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001981
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001982 case WEAK_UNDECLARED_IDENTIFIERS:
Sebastian Redl40566802010-08-05 18:21:25 +00001983 // Later blocks overwrite earlier ones.
1984 WeakUndeclaredIdentifiers.swap(Record);
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00001985 break;
1986
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001987 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001988 // Optimization for the first block.
1989 if (LocallyScopedExternalDecls.empty())
1990 LocallyScopedExternalDecls.swap(Record);
1991 else
1992 LocallyScopedExternalDecls.insert(LocallyScopedExternalDecls.end(),
1993 Record.begin(), Record.end());
Douglas Gregor14c22f22009-04-22 22:18:58 +00001994 break;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001995
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001996 case SELECTOR_OFFSETS:
Sebastian Redl059612d2010-08-03 21:58:15 +00001997 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redl725cd962010-08-04 20:40:17 +00001998 F.LocalNumSelectors = Record[0];
Douglas Gregor83941df2009-04-25 17:48:32 +00001999 break;
2000
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002001 case METHOD_POOL:
Sebastian Redl725cd962010-08-04 20:40:17 +00002002 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor83941df2009-04-25 17:48:32 +00002003 if (Record[0])
Sebastian Redl725cd962010-08-04 20:40:17 +00002004 F.SelectorLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002005 = ASTSelectorLookupTable::Create(
Sebastian Redl725cd962010-08-04 20:40:17 +00002006 F.SelectorLookupTableData + Record[0],
2007 F.SelectorLookupTableData,
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002008 ASTSelectorLookupTrait(*this));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002009 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002010 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002011
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002012 case REFERENCED_SELECTOR_POOL:
Sebastian Redlc3632732010-10-05 15:59:54 +00002013 F.ReferencedSelectorsData.swap(Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002014 break;
2015
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002016 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002017 if (!Record.empty() && Listener)
2018 Listener->ReadCounter(Record[0]);
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002019 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002020
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002021 case SOURCE_LOCATION_OFFSETS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002022 F.SLocOffsets = (const uint32_t *)BlobStart;
2023 F.LocalNumSLocEntries = Record[0];
Sebastian Redl8db9fae2010-09-22 20:19:08 +00002024 F.LocalSLocSize = Record[1];
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002025 break;
2026
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002027 case SOURCE_LOCATION_PRELOADS:
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002028 if (PreloadSLocEntries.empty())
2029 PreloadSLocEntries.swap(Record);
2030 else
2031 PreloadSLocEntries.insert(PreloadSLocEntries.end(),
2032 Record.begin(), Record.end());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002033 break;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002034
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002035 case STAT_CACHE: {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002036 ASTStatCache *MyStatCache =
2037 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
Douglas Gregor52e71082009-10-16 18:18:30 +00002038 (const unsigned char *)BlobStart,
2039 NumStatHits, NumStatMisses);
2040 FileMgr.addStatCache(MyStatCache);
Sebastian Redl9137a522010-07-16 17:50:48 +00002041 F.StatCache = MyStatCache;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002042 break;
Douglas Gregor52e71082009-10-16 18:18:30 +00002043 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002044
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002045 case EXT_VECTOR_DECLS:
Sebastian Redla9f23682010-07-28 21:38:49 +00002046 // Optimization for the first block.
2047 if (ExtVectorDecls.empty())
2048 ExtVectorDecls.swap(Record);
2049 else
2050 ExtVectorDecls.insert(ExtVectorDecls.end(),
2051 Record.begin(), Record.end());
Douglas Gregorb81c1702009-04-27 20:06:05 +00002052 break;
2053
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002054 case VTABLE_USES:
Sebastian Redl40566802010-08-05 18:21:25 +00002055 // Later tables overwrite earlier ones.
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002056 VTableUses.swap(Record);
2057 break;
2058
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002059 case DYNAMIC_CLASSES:
Sebastian Redl40566802010-08-05 18:21:25 +00002060 // Optimization for the first block.
2061 if (DynamicClasses.empty())
2062 DynamicClasses.swap(Record);
2063 else
2064 DynamicClasses.insert(DynamicClasses.end(),
2065 Record.begin(), Record.end());
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002066 break;
2067
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002068 case PENDING_IMPLICIT_INSTANTIATIONS:
Sebastian Redlc3632732010-10-05 15:59:54 +00002069 F.PendingInstantiations.swap(Record);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002070 break;
2071
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002072 case SEMA_DECL_REFS:
Sebastian Redl40566802010-08-05 18:21:25 +00002073 // Later tables overwrite earlier ones.
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002074 SemaDeclRefs.swap(Record);
2075 break;
2076
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002077 case ORIGINAL_FILE_NAME:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002078 // The primary AST will be the last to get here, so it will be the one
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002079 // that's used.
Daniel Dunbar7b5a1212009-11-11 05:29:04 +00002080 ActualOriginalFileName.assign(BlobStart, BlobLen);
2081 OriginalFileName = ActualOriginalFileName;
Douglas Gregore650c8c2009-07-07 00:12:59 +00002082 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregorb64c1932009-05-12 01:31:05 +00002083 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002084
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002085 case VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek974be4d2010-02-12 23:31:14 +00002086 const std::string &CurBranch = getClangFullRepositoryVersion();
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002087 llvm::StringRef ASTBranch(BlobStart, BlobLen);
2088 if (llvm::StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2089 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregor445e23e2009-10-05 21:07:28 +00002090 return IgnorePCH;
2091 }
2092 break;
2093 }
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002094
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002095 case MACRO_DEFINITION_OFFSETS:
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002096 F.MacroDefinitionOffsets = (const uint32_t *)BlobStart;
2097 F.NumPreallocatedPreprocessingEntities = Record[0];
2098 F.LocalNumMacroDefinitions = Record[1];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002099 break;
Sebastian Redl0b17c612010-08-13 00:28:03 +00002100
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002101 case DECL_UPDATE_OFFSETS: {
2102 if (Record.size() % 2 != 0) {
2103 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2104 return Failure;
2105 }
2106 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
2107 DeclUpdateOffsets[static_cast<DeclID>(Record[I])]
2108 .push_back(std::make_pair(&F, Record[I+1]));
2109 break;
2110 }
2111
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002112 case DECL_REPLACEMENTS: {
Sebastian Redl0b17c612010-08-13 00:28:03 +00002113 if (Record.size() % 2 != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002114 Error("invalid DECL_REPLACEMENTS block in AST file");
Sebastian Redl0b17c612010-08-13 00:28:03 +00002115 return Failure;
2116 }
2117 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002118 ReplacedDecls[static_cast<DeclID>(Record[I])] =
Sebastian Redl0b17c612010-08-13 00:28:03 +00002119 std::make_pair(&F, Record[I+1]);
2120 break;
2121 }
Douglas Gregor7c789c12010-10-29 22:39:52 +00002122
2123 case CXX_BASE_SPECIFIER_OFFSETS: {
2124 if (F.LocalNumCXXBaseSpecifiers != 0) {
2125 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2126 return Failure;
2127 }
2128
2129 F.LocalNumCXXBaseSpecifiers = Record[0];
2130 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart;
2131 break;
2132 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002133 }
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002134 First = false;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002135 }
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002136 Error("premature end of bitstream in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002137 return Failure;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002138}
2139
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002140ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
2141 ASTFileType Type) {
2142 switch(ReadASTCore(FileName, Type)) {
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002143 case Failure: return Failure;
2144 case IgnorePCH: return IgnorePCH;
2145 case Success: break;
2146 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002147
2148 // Here comes stuff that we only do once the entire chain is loaded.
2149
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002150 // Allocate space for loaded slocentries, identifiers, decls and types.
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002151 unsigned TotalNumIdentifiers = 0, TotalNumTypes = 0, TotalNumDecls = 0,
Sebastian Redl725cd962010-08-04 20:40:17 +00002152 TotalNumPreallocatedPreprocessingEntities = 0, TotalNumMacroDefs = 0,
2153 TotalNumSelectors = 0;
Sebastian Redl12d6da02010-07-19 22:06:55 +00002154 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002155 TotalNumSLocEntries += Chain[I]->LocalNumSLocEntries;
Sebastian Redl8db9fae2010-09-22 20:19:08 +00002156 NextSLocOffset += Chain[I]->LocalSLocSize;
Sebastian Redl2da08f92010-07-19 22:28:42 +00002157 TotalNumIdentifiers += Chain[I]->LocalNumIdentifiers;
Sebastian Redl12d6da02010-07-19 22:06:55 +00002158 TotalNumTypes += Chain[I]->LocalNumTypes;
2159 TotalNumDecls += Chain[I]->LocalNumDecls;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002160 TotalNumPreallocatedPreprocessingEntities +=
2161 Chain[I]->NumPreallocatedPreprocessingEntities;
2162 TotalNumMacroDefs += Chain[I]->LocalNumMacroDefinitions;
Sebastian Redl725cd962010-08-04 20:40:17 +00002163 TotalNumSelectors += Chain[I]->LocalNumSelectors;
Sebastian Redl12d6da02010-07-19 22:06:55 +00002164 }
Sebastian Redl8db9fae2010-09-22 20:19:08 +00002165 SourceMgr.PreallocateSLocEntries(this, TotalNumSLocEntries, NextSLocOffset);
Sebastian Redl2da08f92010-07-19 22:28:42 +00002166 IdentifiersLoaded.resize(TotalNumIdentifiers);
Sebastian Redl12d6da02010-07-19 22:06:55 +00002167 TypesLoaded.resize(TotalNumTypes);
2168 DeclsLoaded.resize(TotalNumDecls);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002169 MacroDefinitionsLoaded.resize(TotalNumMacroDefs);
2170 if (PP) {
2171 if (TotalNumIdentifiers > 0)
2172 PP->getHeaderSearchInfo().SetExternalLookup(this);
2173 if (TotalNumPreallocatedPreprocessingEntities > 0) {
2174 if (!PP->getPreprocessingRecord())
2175 PP->createPreprocessingRecord();
2176 PP->getPreprocessingRecord()->SetExternalSource(*this,
2177 TotalNumPreallocatedPreprocessingEntities);
2178 }
2179 }
Sebastian Redl725cd962010-08-04 20:40:17 +00002180 SelectorsLoaded.resize(TotalNumSelectors);
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002181 // Preload SLocEntries.
2182 for (unsigned I = 0, N = PreloadSLocEntries.size(); I != N; ++I) {
2183 ASTReadResult Result = ReadSLocEntryRecord(PreloadSLocEntries[I]);
2184 if (Result != Success)
2185 return Result;
2186 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00002187
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002188 // Check the predefines buffers.
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00002189 if (!DisableValidation && CheckPredefinesBuffers())
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002190 return IgnorePCH;
2191
2192 if (PP) {
2193 // Initialization of keywords and pragmas occurs before the
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002194 // AST file is read, so there may be some identifiers that were
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002195 // loaded into the IdentifierTable before we intercepted the
2196 // creation of identifiers. Iterate through the list of known
2197 // identifiers and determine whether we have to establish
2198 // preprocessor definitions or top-level identifier declaration
2199 // chains for those identifiers.
2200 //
2201 // We copy the IdentifierInfo pointers to a small vector first,
2202 // since de-serializing declarations or macro definitions can add
2203 // new entries into the identifier table, invalidating the
2204 // iterators.
2205 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
2206 for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(),
2207 IdEnd = PP->getIdentifierTable().end();
2208 Id != IdEnd; ++Id)
2209 Identifiers.push_back(Id->second);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002210 // We need to search the tables in all files.
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002211 for (unsigned J = 0, M = Chain.size(); J != M; ++J) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002212 ASTIdentifierLookupTable *IdTable
2213 = (ASTIdentifierLookupTable *)Chain[J]->IdentifierLookupTable;
2214 // Not all AST files necessarily have identifier tables, only the useful
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00002215 // ones.
2216 if (!IdTable)
2217 continue;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002218 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
2219 IdentifierInfo *II = Identifiers[I];
2220 // Look in the on-disk hash tables for an entry for this identifier
Sebastian Redlc3632732010-10-05 15:59:54 +00002221 ASTIdentifierLookupTrait Info(*this, *Chain[J], II);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002222 std::pair<const char*,unsigned> Key(II->getNameStart(),II->getLength());
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002223 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002224 if (Pos == IdTable->end())
2225 continue;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002226
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002227 // Dereferencing the iterator has the effect of populating the
2228 // IdentifierInfo node with the various declarations it needs.
2229 (void)*Pos;
2230 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002231 }
2232 }
2233
2234 if (Context)
2235 InitializeContext(*Context);
2236
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002237 if (DeserializationListener)
2238 DeserializationListener->ReaderInitialized(this);
2239
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002240 return Success;
2241}
2242
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002243ASTReader::ASTReadResult ASTReader::ReadASTCore(llvm::StringRef FileName,
2244 ASTFileType Type) {
Sebastian Redla866e652010-10-01 19:59:12 +00002245 PerFileData *Prev = Chain.empty() ? 0 : Chain.back();
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002246 Chain.push_back(new PerFileData(Type));
Sebastian Redl9137a522010-07-16 17:50:48 +00002247 PerFileData &F = *Chain.back();
Sebastian Redla866e652010-10-01 19:59:12 +00002248 if (Prev)
2249 Prev->NextInSource = &F;
2250 else
2251 FirstInSource = &F;
2252 F.Loaders.push_back(Prev);
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002253
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002254 // Set the AST file name.
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002255 F.FileName = FileName;
2256
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002257 // Open the AST file.
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002258 //
2259 // FIXME: This shouldn't be here, we should just take a raw_ostream.
2260 std::string ErrStr;
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +00002261 if (FileName == "-")
2262 F.Buffer.reset(llvm::MemoryBuffer::getSTDIN(&ErrStr));
2263 else
2264 F.Buffer.reset(FileMgr.getBufferForFile(FileName, FileSystemOpts, &ErrStr));
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002265 if (!F.Buffer) {
2266 Error(ErrStr.c_str());
2267 return IgnorePCH;
2268 }
2269
2270 // Initialize the stream
2271 F.StreamFile.init((const unsigned char *)F.Buffer->getBufferStart(),
2272 (const unsigned char *)F.Buffer->getBufferEnd());
Sebastian Redl9137a522010-07-16 17:50:48 +00002273 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002274 Stream.init(F.StreamFile);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002275 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002276
2277 // Sniff for the signature.
2278 if (Stream.Read(8) != 'C' ||
2279 Stream.Read(8) != 'P' ||
2280 Stream.Read(8) != 'C' ||
2281 Stream.Read(8) != 'H') {
2282 Diag(diag::err_not_a_pch_file) << FileName;
2283 return Failure;
2284 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002285
Douglas Gregor2cf26342009-04-09 22:27:44 +00002286 while (!Stream.AtEndOfStream()) {
2287 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00002288
Douglas Gregore1d918e2009-04-10 23:10:45 +00002289 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002290 Error("invalid record at top-level of AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002291 return Failure;
2292 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002293
2294 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregor668c1a42009-04-21 22:25:48 +00002295
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002296 // We only know the AST subblock ID.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002297 switch (BlockID) {
2298 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002299 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002300 Error("malformed BlockInfoBlock in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002301 return Failure;
2302 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002303 break;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002304 case AST_BLOCK_ID:
Sebastian Redl571db7f2010-08-18 23:56:56 +00002305 switch (ReadASTBlock(F)) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002306 case Success:
2307 break;
2308
2309 case Failure:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002310 return Failure;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002311
2312 case IgnorePCH:
Douglas Gregor2bec0412009-04-10 21:16:55 +00002313 // FIXME: We could consider reading through to the end of this
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002314 // AST block, skipping subblocks, to see if there are other
2315 // AST blocks elsewhere.
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002316
2317 // Clear out any preallocated source location entries, so that
2318 // the source manager does not try to resolve them later.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002319 SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002320
2321 // Remove the stat cache.
Sebastian Redl9137a522010-07-16 17:50:48 +00002322 if (F.StatCache)
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002323 FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002324
Douglas Gregore1d918e2009-04-10 23:10:45 +00002325 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002326 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002327 break;
2328 default:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002329 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002330 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002331 return Failure;
2332 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002333 break;
2334 }
Mike Stump1eb44332009-09-09 15:08:12 +00002335 }
2336
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002337 return Success;
2338}
2339
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002340void ASTReader::setPreprocessor(Preprocessor &pp) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002341 PP = &pp;
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002342
2343 unsigned TotalNum = 0;
2344 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
2345 TotalNum += Chain[I]->NumPreallocatedPreprocessingEntities;
2346 if (TotalNum) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002347 if (!PP->getPreprocessingRecord())
2348 PP->createPreprocessingRecord();
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002349 PP->getPreprocessingRecord()->SetExternalSource(*this, TotalNum);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002350 }
2351}
2352
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002353void ASTReader::InitializeContext(ASTContext &Ctx) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002354 Context = &Ctx;
2355 assert(Context && "Passed null context!");
2356
2357 assert(PP && "Forgot to set Preprocessor ?");
2358 PP->getIdentifierTable().setExternalIdentifierLookup(this);
2359 PP->getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregor88a35862010-01-04 19:18:44 +00002360 PP->setExternalSource(this);
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002361
Douglas Gregor3747ee72010-10-01 01:18:02 +00002362 // If we have an update block for the TU waiting, we have to add it before
2363 // deserializing the decl.
2364 DeclContextOffsetsMap::iterator DCU = DeclContextOffsets.find(0);
2365 if (DCU != DeclContextOffsets.end()) {
2366 // Insertion could invalidate map, so grab vector.
2367 DeclContextInfos T;
2368 T.swap(DCU->second);
2369 DeclContextOffsets.erase(DCU);
2370 DeclContextOffsets[Ctx.getTranslationUnitDecl()].swap(T);
2371 }
2372
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002373 // Load the translation unit declaration
Argyrios Kyrtzidis8871a442010-07-08 17:13:02 +00002374 GetTranslationUnitDecl();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002375
2376 // Load the special types.
2377 Context->setBuiltinVaListType(
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002378 GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST]));
2379 if (unsigned Id = SpecialTypes[SPECIAL_TYPE_OBJC_ID])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002380 Context->setObjCIdType(GetType(Id));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002381 if (unsigned Sel = SpecialTypes[SPECIAL_TYPE_OBJC_SELECTOR])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002382 Context->setObjCSelType(GetType(Sel));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002383 if (unsigned Proto = SpecialTypes[SPECIAL_TYPE_OBJC_PROTOCOL])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002384 Context->setObjCProtoType(GetType(Proto));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002385 if (unsigned Class = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002386 Context->setObjCClassType(GetType(Class));
Steve Naroff14108da2009-07-10 23:34:53 +00002387
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002388 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002389 Context->setCFConstantStringType(GetType(String));
Mike Stump1eb44332009-09-09 15:08:12 +00002390 if (unsigned FastEnum
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002391 = SpecialTypes[SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002392 Context->setObjCFastEnumerationStateType(GetType(FastEnum));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002393 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002394 QualType FileType = GetType(File);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002395 if (FileType.isNull()) {
2396 Error("FILE type is NULL");
2397 return;
2398 }
John McCall183700f2009-09-21 23:43:11 +00002399 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002400 Context->setFILEDecl(Typedef->getDecl());
2401 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002402 const TagType *Tag = FileType->getAs<TagType>();
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002403 if (!Tag) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002404 Error("Invalid FILE type in AST file");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002405 return;
2406 }
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002407 Context->setFILEDecl(Tag->getDecl());
2408 }
2409 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002410 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_jmp_buf]) {
Mike Stump782fa302009-07-28 02:25:19 +00002411 QualType Jmp_bufType = GetType(Jmp_buf);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002412 if (Jmp_bufType.isNull()) {
2413 Error("jmp_bug type is NULL");
2414 return;
2415 }
John McCall183700f2009-09-21 23:43:11 +00002416 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
Mike Stump782fa302009-07-28 02:25:19 +00002417 Context->setjmp_bufDecl(Typedef->getDecl());
2418 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002419 const TagType *Tag = Jmp_bufType->getAs<TagType>();
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002420 if (!Tag) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002421 Error("Invalid jmp_buf type in AST file");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002422 return;
2423 }
Mike Stump782fa302009-07-28 02:25:19 +00002424 Context->setjmp_bufDecl(Tag->getDecl());
2425 }
2426 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002427 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_sigjmp_buf]) {
Mike Stump782fa302009-07-28 02:25:19 +00002428 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002429 if (Sigjmp_bufType.isNull()) {
2430 Error("sigjmp_buf type is NULL");
2431 return;
2432 }
John McCall183700f2009-09-21 23:43:11 +00002433 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
Mike Stump782fa302009-07-28 02:25:19 +00002434 Context->setsigjmp_bufDecl(Typedef->getDecl());
2435 else {
Ted Kremenek6217b802009-07-29 21:53:49 +00002436 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002437 assert(Tag && "Invalid sigjmp_buf type in AST file");
Mike Stump782fa302009-07-28 02:25:19 +00002438 Context->setsigjmp_bufDecl(Tag->getDecl());
2439 }
2440 }
Mike Stump1eb44332009-09-09 15:08:12 +00002441 if (unsigned ObjCIdRedef
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002442 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION])
Douglas Gregord1571ac2009-08-21 00:27:50 +00002443 Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef);
Mike Stump1eb44332009-09-09 15:08:12 +00002444 if (unsigned ObjCClassRedef
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002445 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION])
Douglas Gregord1571ac2009-08-21 00:27:50 +00002446 Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002447 if (unsigned String = SpecialTypes[SPECIAL_TYPE_BLOCK_DESCRIPTOR])
Mike Stumpadaaad32009-10-20 02:12:22 +00002448 Context->setBlockDescriptorType(GetType(String));
Mike Stump083c25e2009-10-22 00:49:09 +00002449 if (unsigned String
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002450 = SpecialTypes[SPECIAL_TYPE_BLOCK_EXTENDED_DESCRIPTOR])
Mike Stump083c25e2009-10-22 00:49:09 +00002451 Context->setBlockDescriptorExtendedType(GetType(String));
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002452 if (unsigned ObjCSelRedef
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002453 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION])
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002454 Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002455 if (unsigned String = SpecialTypes[SPECIAL_TYPE_NS_CONSTANT_STRING])
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002456 Context->setNSConstantStringType(GetType(String));
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +00002457
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002458 if (SpecialTypes[SPECIAL_TYPE_INT128_INSTALLED])
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +00002459 Context->setInt128Installed();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002460}
2461
Douglas Gregorb64c1932009-05-12 01:31:05 +00002462/// \brief Retrieve the name of the original source file name
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002463/// directly from the AST file, without actually loading the AST
Douglas Gregorb64c1932009-05-12 01:31:05 +00002464/// file.
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002465std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +00002466 FileManager &FileMgr,
2467 const FileSystemOptions &FSOpts,
Daniel Dunbar93ebb1b2009-12-03 09:13:06 +00002468 Diagnostic &Diags) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002469 // Open the AST file.
Douglas Gregorb64c1932009-05-12 01:31:05 +00002470 std::string ErrStr;
2471 llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +00002472 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, FSOpts, &ErrStr));
Douglas Gregorb64c1932009-05-12 01:31:05 +00002473 if (!Buffer) {
Daniel Dunbar93ebb1b2009-12-03 09:13:06 +00002474 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002475 return std::string();
2476 }
2477
2478 // Initialize the stream
2479 llvm::BitstreamReader StreamFile;
2480 llvm::BitstreamCursor Stream;
Mike Stump1eb44332009-09-09 15:08:12 +00002481 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregorb64c1932009-05-12 01:31:05 +00002482 (const unsigned char *)Buffer->getBufferEnd());
2483 Stream.init(StreamFile);
2484
2485 // Sniff for the signature.
2486 if (Stream.Read(8) != 'C' ||
2487 Stream.Read(8) != 'P' ||
2488 Stream.Read(8) != 'C' ||
2489 Stream.Read(8) != 'H') {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002490 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002491 return std::string();
2492 }
2493
2494 RecordData Record;
2495 while (!Stream.AtEndOfStream()) {
2496 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00002497
Douglas Gregorb64c1932009-05-12 01:31:05 +00002498 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2499 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump1eb44332009-09-09 15:08:12 +00002500
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002501 // We only know the AST subblock ID.
Douglas Gregorb64c1932009-05-12 01:31:05 +00002502 switch (BlockID) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002503 case AST_BLOCK_ID:
2504 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002505 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002506 return std::string();
2507 }
2508 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002509
Douglas Gregorb64c1932009-05-12 01:31:05 +00002510 default:
2511 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002512 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002513 return std::string();
2514 }
2515 break;
2516 }
2517 continue;
2518 }
2519
2520 if (Code == llvm::bitc::END_BLOCK) {
2521 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002522 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00002523 return std::string();
2524 }
2525 continue;
2526 }
2527
2528 if (Code == llvm::bitc::DEFINE_ABBREV) {
2529 Stream.ReadAbbrevRecord();
2530 continue;
2531 }
2532
2533 Record.clear();
2534 const char *BlobStart = 0;
2535 unsigned BlobLen = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002536 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002537 == ORIGINAL_FILE_NAME)
Douglas Gregorb64c1932009-05-12 01:31:05 +00002538 return std::string(BlobStart, BlobLen);
Mike Stump1eb44332009-09-09 15:08:12 +00002539 }
Douglas Gregorb64c1932009-05-12 01:31:05 +00002540
2541 return std::string();
2542}
2543
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002544/// \brief Parse the record that corresponds to a LangOptions data
2545/// structure.
2546///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002547/// This routine parses the language options from the AST file and then gives
2548/// them to the AST listener if one is set.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002549///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002550/// \returns true if the listener deems the file unacceptable, false otherwise.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002551bool ASTReader::ParseLanguageOptions(
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002552 const llvm::SmallVectorImpl<uint64_t> &Record) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002553 if (Listener) {
2554 LangOptions LangOpts;
Mike Stump1eb44332009-09-09 15:08:12 +00002555
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002556 #define PARSE_LANGOPT(Option) \
2557 LangOpts.Option = Record[Idx]; \
2558 ++Idx
Mike Stump1eb44332009-09-09 15:08:12 +00002559
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002560 unsigned Idx = 0;
2561 PARSE_LANGOPT(Trigraphs);
2562 PARSE_LANGOPT(BCPLComment);
2563 PARSE_LANGOPT(DollarIdents);
2564 PARSE_LANGOPT(AsmPreprocessor);
2565 PARSE_LANGOPT(GNUMode);
Chandler Carrutheb5d7b72010-04-17 20:17:31 +00002566 PARSE_LANGOPT(GNUKeywords);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002567 PARSE_LANGOPT(ImplicitInt);
2568 PARSE_LANGOPT(Digraphs);
2569 PARSE_LANGOPT(HexFloats);
2570 PARSE_LANGOPT(C99);
2571 PARSE_LANGOPT(Microsoft);
2572 PARSE_LANGOPT(CPlusPlus);
2573 PARSE_LANGOPT(CPlusPlus0x);
2574 PARSE_LANGOPT(CXXOperatorNames);
2575 PARSE_LANGOPT(ObjC1);
2576 PARSE_LANGOPT(ObjC2);
2577 PARSE_LANGOPT(ObjCNonFragileABI);
Fariborz Jahanian412e7982010-02-09 19:31:38 +00002578 PARSE_LANGOPT(ObjCNonFragileABI2);
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +00002579 PARSE_LANGOPT(NoConstantCFStrings);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002580 PARSE_LANGOPT(PascalStrings);
2581 PARSE_LANGOPT(WritableStrings);
2582 PARSE_LANGOPT(LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +00002583 PARSE_LANGOPT(AltiVec);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002584 PARSE_LANGOPT(Exceptions);
Daniel Dunbar73482882010-02-10 18:48:44 +00002585 PARSE_LANGOPT(SjLjExceptions);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002586 PARSE_LANGOPT(NeXTRuntime);
2587 PARSE_LANGOPT(Freestanding);
2588 PARSE_LANGOPT(NoBuiltin);
2589 PARSE_LANGOPT(ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +00002590 PARSE_LANGOPT(POSIXThreads);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002591 PARSE_LANGOPT(Blocks);
2592 PARSE_LANGOPT(EmitAllDecls);
2593 PARSE_LANGOPT(MathErrno);
Chris Lattnera4d71452010-06-26 21:25:03 +00002594 LangOpts.setSignedOverflowBehavior((LangOptions::SignedOverflowBehaviorTy)
2595 Record[Idx++]);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002596 PARSE_LANGOPT(HeinousExtensions);
2597 PARSE_LANGOPT(Optimize);
2598 PARSE_LANGOPT(OptimizeSize);
2599 PARSE_LANGOPT(Static);
2600 PARSE_LANGOPT(PICLevel);
2601 PARSE_LANGOPT(GNUInline);
2602 PARSE_LANGOPT(NoInline);
2603 PARSE_LANGOPT(AccessControl);
2604 PARSE_LANGOPT(CharIsSigned);
John Thompsona6fda122009-11-05 20:14:16 +00002605 PARSE_LANGOPT(ShortWChar);
Chris Lattnera4d71452010-06-26 21:25:03 +00002606 LangOpts.setGCMode((LangOptions::GCMode)Record[Idx++]);
John McCall1fb0caa2010-10-22 21:05:15 +00002607 LangOpts.setVisibilityMode((Visibility)Record[Idx++]);
Daniel Dunbarab8e2812009-09-21 04:16:19 +00002608 LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode)
Chris Lattnera4d71452010-06-26 21:25:03 +00002609 Record[Idx++]);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002610 PARSE_LANGOPT(InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +00002611 PARSE_LANGOPT(OpenCL);
Mike Stump9c276ae2009-12-12 01:27:46 +00002612 PARSE_LANGOPT(CatchUndefined);
2613 // FIXME: Missing ElideConstructors?!
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002614 #undef PARSE_LANGOPT
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002615
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002616 return Listener->ReadLanguageOptions(LangOpts);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002617 }
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002618
2619 return false;
2620}
2621
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002622void ASTReader::ReadPreprocessedEntities() {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002623 ReadDefinedMacros();
2624}
2625
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002626/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002627ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002628 PerFileData *F = 0;
2629 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
2630 F = Chain[N - I - 1];
2631 if (Index < F->LocalNumTypes)
2632 break;
2633 Index -= F->LocalNumTypes;
2634 }
2635 assert(F && F->LocalNumTypes > Index && "Broken chain");
Sebastian Redlc3632732010-10-05 15:59:54 +00002636 return RecordLocation(F, F->TypeOffsets[Index]);
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002637}
2638
2639/// \brief Read and return the type with the given index..
Douglas Gregor2cf26342009-04-09 22:27:44 +00002640///
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002641/// The index is the type ID, shifted and minus the number of predefs. This
2642/// routine actually reads the record corresponding to the type at the given
2643/// location. It is a helper routine for GetType, which deals with reading type
2644/// IDs.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002645QualType ASTReader::ReadTypeRecord(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00002646 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00002647 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00002648
Douglas Gregor0b748912009-04-14 21:18:50 +00002649 // Keep track of where we are in the stream, then jump back there
2650 // after reading this type.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002651 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregor0b748912009-04-14 21:18:50 +00002652
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00002653 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redl27372b42010-08-11 18:52:41 +00002654
Douglas Gregord89275b2009-07-06 18:54:52 +00002655 // Note that we are loading a type record.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00002656 Deserializing AType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00002657
Sebastian Redlc3632732010-10-05 15:59:54 +00002658 DeclsCursor.JumpToBit(Loc.Offset);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002659 RecordData Record;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002660 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002661 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
2662 case TYPE_EXT_QUAL: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002663 if (Record.size() != 2) {
2664 Error("Incorrect encoding of extended qualifier type");
2665 return QualType();
2666 }
Douglas Gregor6d473962009-04-15 22:00:08 +00002667 QualType Base = GetType(Record[0]);
John McCall0953e762009-09-24 19:53:00 +00002668 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[1]);
2669 return Context->getQualifiedType(Base, Quals);
Douglas Gregor6d473962009-04-15 22:00:08 +00002670 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002671
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002672 case TYPE_COMPLEX: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002673 if (Record.size() != 1) {
2674 Error("Incorrect encoding of complex type");
2675 return QualType();
2676 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002677 QualType ElemType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002678 return Context->getComplexType(ElemType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002679 }
2680
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002681 case TYPE_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002682 if (Record.size() != 1) {
2683 Error("Incorrect encoding of pointer type");
2684 return QualType();
2685 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002686 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002687 return Context->getPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002688 }
2689
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002690 case TYPE_BLOCK_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002691 if (Record.size() != 1) {
2692 Error("Incorrect encoding of block pointer type");
2693 return QualType();
2694 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002695 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002696 return Context->getBlockPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002697 }
2698
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002699 case TYPE_LVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002700 if (Record.size() != 1) {
2701 Error("Incorrect encoding of lvalue reference type");
2702 return QualType();
2703 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002704 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002705 return Context->getLValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002706 }
2707
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002708 case TYPE_RVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002709 if (Record.size() != 1) {
2710 Error("Incorrect encoding of rvalue reference type");
2711 return QualType();
2712 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002713 QualType PointeeType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002714 return Context->getRValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002715 }
2716
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002717 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidis240437b2010-07-02 11:55:15 +00002718 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002719 Error("Incorrect encoding of member pointer type");
2720 return QualType();
2721 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002722 QualType PointeeType = GetType(Record[0]);
2723 QualType ClassType = GetType(Record[1]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002724 return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002725 }
2726
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002727 case TYPE_CONSTANT_ARRAY: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002728 QualType ElementType = GetType(Record[0]);
2729 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2730 unsigned IndexTypeQuals = Record[2];
2731 unsigned Idx = 3;
2732 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002733 return Context->getConstantArrayType(ElementType, Size,
2734 ASM, IndexTypeQuals);
2735 }
2736
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002737 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002738 QualType ElementType = GetType(Record[0]);
2739 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2740 unsigned IndexTypeQuals = Record[2];
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002741 return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002742 }
2743
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002744 case TYPE_VARIABLE_ARRAY: {
Douglas Gregor0b748912009-04-14 21:18:50 +00002745 QualType ElementType = GetType(Record[0]);
2746 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2747 unsigned IndexTypeQuals = Record[2];
Sebastian Redlc3632732010-10-05 15:59:54 +00002748 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
2749 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
2750 return Context->getVariableArrayType(ElementType, ReadExpr(*Loc.F),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002751 ASM, IndexTypeQuals,
2752 SourceRange(LBLoc, RBLoc));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002753 }
2754
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002755 case TYPE_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00002756 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002757 Error("incorrect encoding of vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002758 return QualType();
2759 }
2760
2761 QualType ElementType = GetType(Record[0]);
2762 unsigned NumElements = Record[1];
Chris Lattner788b0fd2010-06-23 06:00:24 +00002763 unsigned AltiVecSpec = Record[2];
2764 return Context->getVectorType(ElementType, NumElements,
2765 (VectorType::AltiVecSpecific)AltiVecSpec);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002766 }
2767
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002768 case TYPE_EXT_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00002769 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002770 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002771 return QualType();
2772 }
2773
2774 QualType ElementType = GetType(Record[0]);
2775 unsigned NumElements = Record[1];
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002776 return Context->getExtVectorType(ElementType, NumElements);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002777 }
2778
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002779 case TYPE_FUNCTION_NO_PROTO: {
Rafael Espindola425ef722010-03-30 22:15:11 +00002780 if (Record.size() != 4) {
Douglas Gregora02b1472009-04-28 21:53:25 +00002781 Error("incorrect encoding of no-proto function type");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002782 return QualType();
2783 }
2784 QualType ResultType = GetType(Record[0]);
Rafael Espindola425ef722010-03-30 22:15:11 +00002785 FunctionType::ExtInfo Info(Record[1], Record[2], (CallingConv)Record[3]);
Rafael Espindola264ba482010-03-30 20:24:48 +00002786 return Context->getFunctionNoProtoType(ResultType, Info);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002787 }
2788
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002789 case TYPE_FUNCTION_PROTO: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002790 QualType ResultType = GetType(Record[0]);
Douglas Gregor91236662009-12-22 18:11:50 +00002791 bool NoReturn = Record[1];
Rafael Espindola425ef722010-03-30 22:15:11 +00002792 unsigned RegParm = Record[2];
2793 CallingConv CallConv = (CallingConv)Record[3];
2794 unsigned Idx = 4;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002795 unsigned NumParams = Record[Idx++];
2796 llvm::SmallVector<QualType, 16> ParamTypes;
2797 for (unsigned I = 0; I != NumParams; ++I)
2798 ParamTypes.push_back(GetType(Record[Idx++]));
2799 bool isVariadic = Record[Idx++];
2800 unsigned Quals = Record[Idx++];
Sebastian Redl465226e2009-05-27 22:11:52 +00002801 bool hasExceptionSpec = Record[Idx++];
2802 bool hasAnyExceptionSpec = Record[Idx++];
2803 unsigned NumExceptions = Record[Idx++];
2804 llvm::SmallVector<QualType, 2> Exceptions;
2805 for (unsigned I = 0; I != NumExceptions; ++I)
2806 Exceptions.push_back(GetType(Record[Idx++]));
Jay Foadbeaaccd2009-05-21 09:52:38 +00002807 return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams,
Sebastian Redl465226e2009-05-27 22:11:52 +00002808 isVariadic, Quals, hasExceptionSpec,
2809 hasAnyExceptionSpec, NumExceptions,
Rafael Espindola264ba482010-03-30 20:24:48 +00002810 Exceptions.data(),
Rafael Espindola425ef722010-03-30 22:15:11 +00002811 FunctionType::ExtInfo(NoReturn, RegParm,
2812 CallConv));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002813 }
2814
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002815 case TYPE_UNRESOLVED_USING:
John McCalled976492009-12-04 22:46:56 +00002816 return Context->getTypeDeclType(
2817 cast<UnresolvedUsingTypenameDecl>(GetDecl(Record[0])));
2818
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002819 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002820 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002821 Error("incorrect encoding of typedef type");
2822 return QualType();
2823 }
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002824 TypedefDecl *Decl = cast<TypedefDecl>(GetDecl(Record[0]));
2825 QualType Canonical = GetType(Record[1]);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00002826 if (!Canonical.isNull())
2827 Canonical = Context->getCanonicalType(Canonical);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002828 return Context->getTypedefType(Decl, Canonical);
2829 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002830
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002831 case TYPE_TYPEOF_EXPR:
Sebastian Redlc3632732010-10-05 15:59:54 +00002832 return Context->getTypeOfExprType(ReadExpr(*Loc.F));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002833
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002834 case TYPE_TYPEOF: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002835 if (Record.size() != 1) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002836 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002837 return QualType();
2838 }
2839 QualType UnderlyingType = GetType(Record[0]);
Chris Lattnerd1d64a02009-04-27 21:45:14 +00002840 return Context->getTypeOfType(UnderlyingType);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002841 }
Mike Stump1eb44332009-09-09 15:08:12 +00002842
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002843 case TYPE_DECLTYPE:
Sebastian Redlc3632732010-10-05 15:59:54 +00002844 return Context->getDecltypeType(ReadExpr(*Loc.F));
Anders Carlsson395b4752009-06-24 19:06:50 +00002845
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002846 case TYPE_RECORD: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002847 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002848 Error("incorrect encoding of record type");
2849 return QualType();
2850 }
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002851 bool IsDependent = Record[0];
2852 QualType T = Context->getRecordType(cast<RecordDecl>(GetDecl(Record[1])));
John McCallb870b882010-10-14 21:48:26 +00002853 T->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002854 return T;
2855 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002856
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002857 case TYPE_ENUM: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002858 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00002859 Error("incorrect encoding of enum type");
2860 return QualType();
2861 }
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002862 bool IsDependent = Record[0];
2863 QualType T = Context->getEnumType(cast<EnumDecl>(GetDecl(Record[1])));
John McCallb870b882010-10-14 21:48:26 +00002864 T->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002865 return T;
2866 }
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002867
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002868 case TYPE_ELABORATED: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00002869 unsigned Idx = 0;
2870 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2871 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2872 QualType NamedType = GetType(Record[Idx++]);
2873 return Context->getElaboratedType(Keyword, NNS, NamedType);
John McCall7da24312009-09-05 00:15:47 +00002874 }
2875
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002876 case TYPE_OBJC_INTERFACE: {
Chris Lattnerc6fa4452009-04-22 06:45:28 +00002877 unsigned Idx = 0;
2878 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
John McCallc12c5bb2010-05-15 11:32:37 +00002879 return Context->getObjCInterfaceType(ItfD);
2880 }
2881
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002882 case TYPE_OBJC_OBJECT: {
John McCallc12c5bb2010-05-15 11:32:37 +00002883 unsigned Idx = 0;
2884 QualType Base = GetType(Record[Idx++]);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00002885 unsigned NumProtos = Record[Idx++];
2886 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2887 for (unsigned I = 0; I != NumProtos; ++I)
2888 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
Michael J. Spencer20249a12010-10-21 03:16:25 +00002889 return Context->getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00002890 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002891
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002892 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00002893 unsigned Idx = 0;
John McCallc12c5bb2010-05-15 11:32:37 +00002894 QualType Pointee = GetType(Record[Idx++]);
2895 return Context->getObjCObjectPointerType(Pointee);
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00002896 }
Argyrios Kyrtzidis24fab412009-09-29 19:42:55 +00002897
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002898 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCall49a832b2009-10-18 09:09:24 +00002899 unsigned Idx = 0;
2900 QualType Parm = GetType(Record[Idx++]);
2901 QualType Replacement = GetType(Record[Idx++]);
2902 return
2903 Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
2904 Replacement);
2905 }
John McCall3cb0ebd2010-03-10 03:28:59 +00002906
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002907 case TYPE_INJECTED_CLASS_NAME: {
John McCall3cb0ebd2010-03-10 03:28:59 +00002908 CXXRecordDecl *D = cast<CXXRecordDecl>(GetDecl(Record[0]));
2909 QualType TST = GetType(Record[1]); // probably derivable
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00002910 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002911 // for AST reading, too much interdependencies.
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00002912 return
2913 QualType(new (*Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCall3cb0ebd2010-03-10 03:28:59 +00002914 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00002915
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002916 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00002917 unsigned Idx = 0;
2918 unsigned Depth = Record[Idx++];
2919 unsigned Index = Record[Idx++];
2920 bool Pack = Record[Idx++];
2921 IdentifierInfo *Name = GetIdentifierInfo(Record, Idx);
2922 return Context->getTemplateTypeParmType(Depth, Index, Pack, Name);
2923 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00002924
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002925 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00002926 unsigned Idx = 0;
2927 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2928 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2929 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +00002930 QualType Canon = GetType(Record[Idx++]);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00002931 if (!Canon.isNull())
2932 Canon = Context->getCanonicalType(Canon);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +00002933 return Context->getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00002934 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00002935
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002936 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00002937 unsigned Idx = 0;
2938 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
2939 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
2940 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx);
2941 unsigned NumArgs = Record[Idx++];
2942 llvm::SmallVector<TemplateArgument, 8> Args;
2943 Args.reserve(NumArgs);
2944 while (NumArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00002945 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00002946 return Context->getDependentTemplateSpecializationType(Keyword, NNS, Name,
2947 Args.size(), Args.data());
2948 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00002949
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002950 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00002951 unsigned Idx = 0;
2952
2953 // ArrayType
2954 QualType ElementType = GetType(Record[Idx++]);
2955 ArrayType::ArraySizeModifier ASM
2956 = (ArrayType::ArraySizeModifier)Record[Idx++];
2957 unsigned IndexTypeQuals = Record[Idx++];
2958
2959 // DependentSizedArrayType
Sebastian Redlc3632732010-10-05 15:59:54 +00002960 Expr *NumElts = ReadExpr(*Loc.F);
2961 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00002962
2963 return Context->getDependentSizedArrayType(ElementType, NumElts, ASM,
2964 IndexTypeQuals, Brackets);
2965 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00002966
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002967 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00002968 unsigned Idx = 0;
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002969 bool IsDependent = Record[Idx++];
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00002970 TemplateName Name = ReadTemplateName(Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00002971 llvm::SmallVector<TemplateArgument, 8> Args;
Sebastian Redlc3632732010-10-05 15:59:54 +00002972 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00002973 QualType Canon = GetType(Record[Idx++]);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002974 QualType T;
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002975 if (Canon.isNull())
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002976 T = Context->getCanonicalTemplateSpecializationType(Name, Args.data(),
2977 Args.size());
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002978 else
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002979 T = Context->getTemplateSpecializationType(Name, Args.data(),
2980 Args.size(), Canon);
John McCallb870b882010-10-14 21:48:26 +00002981 T->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00002982 return T;
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00002983 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002984 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002985 // Suppress a GCC warning
2986 return QualType();
2987}
2988
Sebastian Redlc3632732010-10-05 15:59:54 +00002989class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002990 ASTReader &Reader;
Sebastian Redlc3632732010-10-05 15:59:54 +00002991 ASTReader::PerFileData &F;
Sebastian Redl577d4792010-07-22 22:43:28 +00002992 llvm::BitstreamCursor &DeclsCursor;
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002993 const ASTReader::RecordData &Record;
John McCalla1ee0c52009-10-16 21:56:05 +00002994 unsigned &Idx;
2995
Sebastian Redlc3632732010-10-05 15:59:54 +00002996 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
2997 unsigned &I) {
2998 return Reader.ReadSourceLocation(F, R, I);
2999 }
3000
John McCalla1ee0c52009-10-16 21:56:05 +00003001public:
Sebastian Redlc3632732010-10-05 15:59:54 +00003002 TypeLocReader(ASTReader &Reader, ASTReader::PerFileData &F,
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003003 const ASTReader::RecordData &Record, unsigned &Idx)
Sebastian Redlc3632732010-10-05 15:59:54 +00003004 : Reader(Reader), F(F), DeclsCursor(F.DeclsCursor), Record(Record), Idx(Idx)
3005 { }
John McCalla1ee0c52009-10-16 21:56:05 +00003006
John McCall51bd8032009-10-18 01:05:36 +00003007 // We want compile-time assurance that we've enumerated all of
3008 // these, so unfortunately we have to declare them first, then
3009 // define them out-of-line.
3010#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +00003011#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +00003012 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00003013#include "clang/AST/TypeLocNodes.def"
3014
John McCall51bd8032009-10-18 01:05:36 +00003015 void VisitFunctionTypeLoc(FunctionTypeLoc);
3016 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00003017};
3018
John McCall51bd8032009-10-18 01:05:36 +00003019void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCalla1ee0c52009-10-16 21:56:05 +00003020 // nothing to do
3021}
John McCall51bd8032009-10-18 01:05:36 +00003022void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003023 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
Douglas Gregorddf889a2010-01-18 18:04:31 +00003024 if (TL.needsExtraLocalData()) {
3025 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
3026 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
3027 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
3028 TL.setModeAttr(Record[Idx++]);
3029 }
John McCalla1ee0c52009-10-16 21:56:05 +00003030}
John McCall51bd8032009-10-18 01:05:36 +00003031void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003032 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003033}
John McCall51bd8032009-10-18 01:05:36 +00003034void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003035 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003036}
John McCall51bd8032009-10-18 01:05:36 +00003037void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003038 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003039}
John McCall51bd8032009-10-18 01:05:36 +00003040void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003041 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003042}
John McCall51bd8032009-10-18 01:05:36 +00003043void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003044 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003045}
John McCall51bd8032009-10-18 01:05:36 +00003046void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003047 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003048}
John McCall51bd8032009-10-18 01:05:36 +00003049void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003050 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
3051 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003052 if (Record[Idx++])
Sebastian Redlc3632732010-10-05 15:59:54 +00003053 TL.setSizeExpr(Reader.ReadExpr(F));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003054 else
John McCall51bd8032009-10-18 01:05:36 +00003055 TL.setSizeExpr(0);
3056}
3057void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
3058 VisitArrayTypeLoc(TL);
3059}
3060void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
3061 VisitArrayTypeLoc(TL);
3062}
3063void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
3064 VisitArrayTypeLoc(TL);
3065}
3066void TypeLocReader::VisitDependentSizedArrayTypeLoc(
3067 DependentSizedArrayTypeLoc TL) {
3068 VisitArrayTypeLoc(TL);
3069}
3070void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
3071 DependentSizedExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003072 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003073}
3074void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003075 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003076}
3077void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003078 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003079}
3080void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003081 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3082 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
Douglas Gregordab60ad2010-10-01 18:44:50 +00003083 TL.setTrailingReturn(Record[Idx++]);
John McCall51bd8032009-10-18 01:05:36 +00003084 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
John McCall86acc2a2009-10-23 01:28:53 +00003085 TL.setArg(i, cast_or_null<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
John McCall51bd8032009-10-18 01:05:36 +00003086 }
3087}
3088void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
3089 VisitFunctionTypeLoc(TL);
3090}
3091void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
3092 VisitFunctionTypeLoc(TL);
3093}
John McCalled976492009-12-04 22:46:56 +00003094void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003095 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalled976492009-12-04 22:46:56 +00003096}
John McCall51bd8032009-10-18 01:05:36 +00003097void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003098 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003099}
3100void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003101 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3102 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3103 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003104}
3105void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003106 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
3107 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
3108 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
3109 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003110}
3111void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003112 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003113}
3114void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003115 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003116}
3117void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003118 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003119}
John McCall51bd8032009-10-18 01:05:36 +00003120void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003121 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003122}
John McCall49a832b2009-10-18 09:09:24 +00003123void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
3124 SubstTemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003125 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall49a832b2009-10-18 09:09:24 +00003126}
John McCall51bd8032009-10-18 01:05:36 +00003127void TypeLocReader::VisitTemplateSpecializationTypeLoc(
3128 TemplateSpecializationTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003129 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
3130 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3131 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall833ca992009-10-29 08:12:44 +00003132 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
3133 TL.setArgLocInfo(i,
Sebastian Redlc3632732010-10-05 15:59:54 +00003134 Reader.GetTemplateArgumentLocInfo(F,
3135 TL.getTypePtr()->getArg(i).getKind(),
3136 Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003137}
Abramo Bagnara465d41b2010-05-11 21:36:43 +00003138void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003139 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3140 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003141}
John McCall3cb0ebd2010-03-10 03:28:59 +00003142void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003143 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall3cb0ebd2010-03-10 03:28:59 +00003144}
Douglas Gregor4714c122010-03-31 17:34:00 +00003145void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003146 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3147 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
3148 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003149}
John McCall33500952010-06-11 00:33:02 +00003150void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
3151 DependentTemplateSpecializationTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003152 TL.setKeywordLoc(ReadSourceLocation(Record, Idx));
3153 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx));
3154 TL.setNameLoc(ReadSourceLocation(Record, Idx));
3155 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3156 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall33500952010-06-11 00:33:02 +00003157 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
3158 TL.setArgLocInfo(I,
Sebastian Redlc3632732010-10-05 15:59:54 +00003159 Reader.GetTemplateArgumentLocInfo(F,
3160 TL.getTypePtr()->getArg(I).getKind(),
3161 Record, Idx));
John McCall33500952010-06-11 00:33:02 +00003162}
John McCall51bd8032009-10-18 01:05:36 +00003163void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003164 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallc12c5bb2010-05-15 11:32:37 +00003165}
3166void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
3167 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00003168 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
3169 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00003170 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Sebastian Redlc3632732010-10-05 15:59:54 +00003171 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00003172}
John McCall54e14c42009-10-22 22:37:11 +00003173void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003174 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall54e14c42009-10-22 22:37:11 +00003175}
John McCalla1ee0c52009-10-16 21:56:05 +00003176
Sebastian Redlc3632732010-10-05 15:59:54 +00003177TypeSourceInfo *ASTReader::GetTypeSourceInfo(PerFileData &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00003178 const RecordData &Record,
John McCalla1ee0c52009-10-16 21:56:05 +00003179 unsigned &Idx) {
3180 QualType InfoTy = GetType(Record[Idx++]);
3181 if (InfoTy.isNull())
3182 return 0;
3183
John McCalla93c9342009-12-07 02:54:59 +00003184 TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy);
Sebastian Redlc3632732010-10-05 15:59:54 +00003185 TypeLocReader TLR(*this, F, Record, Idx);
John McCalla93c9342009-12-07 02:54:59 +00003186 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCalla1ee0c52009-10-16 21:56:05 +00003187 TLR.Visit(TL);
John McCalla93c9342009-12-07 02:54:59 +00003188 return TInfo;
John McCalla1ee0c52009-10-16 21:56:05 +00003189}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003190
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003191QualType ASTReader::GetType(TypeID ID) {
John McCall0953e762009-09-24 19:53:00 +00003192 unsigned FastQuals = ID & Qualifiers::FastMask;
3193 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003194
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003195 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003196 QualType T;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003197 switch ((PredefinedTypeIDs)Index) {
3198 case PREDEF_TYPE_NULL_ID: return QualType();
3199 case PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break;
3200 case PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003201
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003202 case PREDEF_TYPE_CHAR_U_ID:
3203 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregor2cf26342009-04-09 22:27:44 +00003204 // FIXME: Check that the signedness of CharTy is correct!
Chris Lattnerd1d64a02009-04-27 21:45:14 +00003205 T = Context->CharTy;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003206 break;
3207
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003208 case PREDEF_TYPE_UCHAR_ID: T = Context->UnsignedCharTy; break;
3209 case PREDEF_TYPE_USHORT_ID: T = Context->UnsignedShortTy; break;
3210 case PREDEF_TYPE_UINT_ID: T = Context->UnsignedIntTy; break;
3211 case PREDEF_TYPE_ULONG_ID: T = Context->UnsignedLongTy; break;
3212 case PREDEF_TYPE_ULONGLONG_ID: T = Context->UnsignedLongLongTy; break;
3213 case PREDEF_TYPE_UINT128_ID: T = Context->UnsignedInt128Ty; break;
3214 case PREDEF_TYPE_SCHAR_ID: T = Context->SignedCharTy; break;
3215 case PREDEF_TYPE_WCHAR_ID: T = Context->WCharTy; break;
3216 case PREDEF_TYPE_SHORT_ID: T = Context->ShortTy; break;
3217 case PREDEF_TYPE_INT_ID: T = Context->IntTy; break;
3218 case PREDEF_TYPE_LONG_ID: T = Context->LongTy; break;
3219 case PREDEF_TYPE_LONGLONG_ID: T = Context->LongLongTy; break;
3220 case PREDEF_TYPE_INT128_ID: T = Context->Int128Ty; break;
3221 case PREDEF_TYPE_FLOAT_ID: T = Context->FloatTy; break;
3222 case PREDEF_TYPE_DOUBLE_ID: T = Context->DoubleTy; break;
3223 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy; break;
3224 case PREDEF_TYPE_OVERLOAD_ID: T = Context->OverloadTy; break;
3225 case PREDEF_TYPE_DEPENDENT_ID: T = Context->DependentTy; break;
3226 case PREDEF_TYPE_NULLPTR_ID: T = Context->NullPtrTy; break;
3227 case PREDEF_TYPE_CHAR16_ID: T = Context->Char16Ty; break;
3228 case PREDEF_TYPE_CHAR32_ID: T = Context->Char32Ty; break;
3229 case PREDEF_TYPE_OBJC_ID: T = Context->ObjCBuiltinIdTy; break;
3230 case PREDEF_TYPE_OBJC_CLASS: T = Context->ObjCBuiltinClassTy; break;
3231 case PREDEF_TYPE_OBJC_SEL: T = Context->ObjCBuiltinSelTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003232 }
3233
3234 assert(!T.isNull() && "Unknown predefined type");
John McCall0953e762009-09-24 19:53:00 +00003235 return T.withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003236 }
3237
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003238 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003239 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl07a353c2010-07-14 20:26:45 +00003240 if (TypesLoaded[Index].isNull()) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003241 TypesLoaded[Index] = ReadTypeRecord(Index);
Douglas Gregor97475832010-10-05 18:37:06 +00003242 if (TypesLoaded[Index].isNull())
3243 return QualType();
3244
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003245 TypesLoaded[Index]->setFromAST();
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003246 TypeIdxs[TypesLoaded[Index]] = TypeIdx::fromTypeID(ID);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003247 if (DeserializationListener)
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003248 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1476ed42010-07-16 16:36:56 +00003249 TypesLoaded[Index]);
Sebastian Redl07a353c2010-07-14 20:26:45 +00003250 }
Mike Stump1eb44332009-09-09 15:08:12 +00003251
John McCall0953e762009-09-24 19:53:00 +00003252 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003253}
3254
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003255TypeID ASTReader::GetTypeID(QualType T) const {
3256 return MakeTypeID(T,
3257 std::bind1st(std::mem_fun(&ASTReader::GetTypeIdx), this));
3258}
3259
3260TypeIdx ASTReader::GetTypeIdx(QualType T) const {
3261 if (T.isNull())
3262 return TypeIdx();
3263 assert(!T.getLocalFastQualifiers());
3264
3265 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3266 // GetTypeIdx is mostly used for computing the hash of DeclarationNames and
3267 // comparing keys of ASTDeclContextNameLookupTable.
3268 // If the type didn't come from the AST file use a specially marked index
3269 // so that any hash/key comparison fail since no such index is stored
3270 // in a AST file.
3271 if (I == TypeIdxs.end())
3272 return TypeIdx(-1);
3273 return I->second;
3274}
3275
Douglas Gregor7c789c12010-10-29 22:39:52 +00003276unsigned ASTReader::getTotalNumCXXBaseSpecifiers() const {
3277 unsigned Result = 0;
3278 for (unsigned I = 0, N = Chain.size(); I != N; ++I)
3279 Result += Chain[I]->LocalNumCXXBaseSpecifiers;
3280
3281 return Result;
3282}
3283
John McCall833ca992009-10-29 08:12:44 +00003284TemplateArgumentLocInfo
Sebastian Redlc3632732010-10-05 15:59:54 +00003285ASTReader::GetTemplateArgumentLocInfo(PerFileData &F,
3286 TemplateArgument::ArgKind Kind,
John McCall833ca992009-10-29 08:12:44 +00003287 const RecordData &Record,
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00003288 unsigned &Index) {
John McCall833ca992009-10-29 08:12:44 +00003289 switch (Kind) {
3290 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00003291 return ReadExpr(F);
John McCall833ca992009-10-29 08:12:44 +00003292 case TemplateArgument::Type:
Sebastian Redlc3632732010-10-05 15:59:54 +00003293 return GetTypeSourceInfo(F, Record, Index);
Douglas Gregor788cd062009-11-11 01:00:40 +00003294 case TemplateArgument::Template: {
Sebastian Redlc3632732010-10-05 15:59:54 +00003295 SourceRange QualifierRange = ReadSourceRange(F, Record, Index);
3296 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003297 return TemplateArgumentLocInfo(QualifierRange, TemplateNameLoc);
Douglas Gregor788cd062009-11-11 01:00:40 +00003298 }
John McCall833ca992009-10-29 08:12:44 +00003299 case TemplateArgument::Null:
3300 case TemplateArgument::Integral:
3301 case TemplateArgument::Declaration:
3302 case TemplateArgument::Pack:
3303 return TemplateArgumentLocInfo();
3304 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00003305 llvm_unreachable("unexpected template argument loc");
John McCall833ca992009-10-29 08:12:44 +00003306 return TemplateArgumentLocInfo();
3307}
3308
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003309TemplateArgumentLoc
Sebastian Redlc3632732010-10-05 15:59:54 +00003310ASTReader::ReadTemplateArgumentLoc(PerFileData &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00003311 const RecordData &Record, unsigned &Index) {
Sebastian Redlc3632732010-10-05 15:59:54 +00003312 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003313
3314 if (Arg.getKind() == TemplateArgument::Expression) {
3315 if (Record[Index++]) // bool InfoHasSameExpr.
3316 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
3317 }
Sebastian Redlc3632732010-10-05 15:59:54 +00003318 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00003319 Record, Index));
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003320}
3321
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003322Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall76bd1f32010-06-01 09:23:16 +00003323 return GetDecl(ID);
3324}
3325
Douglas Gregor7c789c12010-10-29 22:39:52 +00003326uint64_t
3327ASTReader::GetCXXBaseSpecifiersOffset(serialization::CXXBaseSpecifiersID ID) {
3328 if (ID == 0)
3329 return 0;
3330
3331 --ID;
3332 uint64_t Offset = 0;
3333 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3334 if (ID < Chain[I]->LocalNumCXXBaseSpecifiers)
3335 return Offset + Chain[I]->CXXBaseSpecifiersOffsets[ID];
3336
3337 ID -= Chain[I]->LocalNumCXXBaseSpecifiers;
3338 Offset += Chain[I]->SizeInBits;
3339 }
3340
3341 assert(false && "CXXBaseSpecifiers not found");
3342 return 0;
3343}
3344
3345CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
3346 // Figure out which AST file contains this offset.
3347 PerFileData *F = 0;
3348 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3349 if (Offset < Chain[I]->SizeInBits) {
3350 F = Chain[I];
3351 break;
3352 }
3353
3354 Offset -= Chain[I]->SizeInBits;
3355 }
3356
3357 if (!F) {
3358 Error("Malformed AST file: C++ base specifiers at impossible offset");
3359 return 0;
3360 }
3361
3362 llvm::BitstreamCursor &Cursor = F->DeclsCursor;
3363 SavedStreamPosition SavedPosition(Cursor);
3364 Cursor.JumpToBit(Offset);
3365 ReadingKindTracker ReadingKind(Read_Decl, *this);
3366 RecordData Record;
3367 unsigned Code = Cursor.ReadCode();
3368 unsigned RecCode = Cursor.ReadRecord(Code, Record);
3369 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
3370 Error("Malformed AST file: missing C++ base specifiers");
3371 return 0;
3372 }
3373
3374 unsigned Idx = 0;
3375 unsigned NumBases = Record[Idx++];
3376 void *Mem = Context->Allocate(sizeof(CXXBaseSpecifier) * NumBases);
3377 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
3378 for (unsigned I = 0; I != NumBases; ++I)
3379 Bases[I] = ReadCXXBaseSpecifier(*F, Record, Idx);
3380 return Bases;
3381}
3382
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003383TranslationUnitDecl *ASTReader::GetTranslationUnitDecl() {
Sebastian Redl30c514c2010-07-14 23:45:08 +00003384 if (!DeclsLoaded[0]) {
Sebastian Redle1dde812010-08-24 00:50:04 +00003385 ReadDeclRecord(0, 1);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003386 if (DeserializationListener)
Sebastian Redl1476ed42010-07-16 16:36:56 +00003387 DeserializationListener->DeclRead(1, DeclsLoaded[0]);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003388 }
Argyrios Kyrtzidis8871a442010-07-08 17:13:02 +00003389
3390 return cast<TranslationUnitDecl>(DeclsLoaded[0]);
3391}
3392
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003393Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003394 if (ID == 0)
3395 return 0;
3396
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003397 if (ID > DeclsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003398 Error("declaration ID out-of-range for AST file");
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003399 return 0;
3400 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003401
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003402 unsigned Index = ID - 1;
Sebastian Redl30c514c2010-07-14 23:45:08 +00003403 if (!DeclsLoaded[Index]) {
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00003404 ReadDeclRecord(Index, ID);
Sebastian Redl30c514c2010-07-14 23:45:08 +00003405 if (DeserializationListener)
3406 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
3407 }
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003408
3409 return DeclsLoaded[Index];
Douglas Gregor2cf26342009-04-09 22:27:44 +00003410}
3411
Chris Lattner887e2b32009-04-27 05:46:25 +00003412/// \brief Resolve the offset of a statement into a statement.
3413///
3414/// This operation will read a new statement from the external
3415/// source each time it is called, and is meant to be used via a
3416/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003417Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00003418 // Switch case IDs are per Decl.
3419 ClearSwitchCaseIDs();
3420
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003421 // Offset here is a global offset across the entire chain.
3422 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3423 PerFileData &F = *Chain[N - I - 1];
3424 if (Offset < F.SizeInBits) {
3425 // Since we know that this statement is part of a decl, make sure to use
3426 // the decl cursor to read it.
3427 F.DeclsCursor.JumpToBit(Offset);
Sebastian Redlc3632732010-10-05 15:59:54 +00003428 return ReadStmtFromStream(F);
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003429 }
3430 Offset -= F.SizeInBits;
3431 }
3432 llvm_unreachable("Broken chain");
Douglas Gregor250fc9c2009-04-18 00:07:54 +00003433}
3434
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003435bool ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00003436 bool (*isKindWeWant)(Decl::Kind),
John McCall76bd1f32010-06-01 09:23:16 +00003437 llvm::SmallVectorImpl<Decl*> &Decls) {
Mike Stump1eb44332009-09-09 15:08:12 +00003438 assert(DC->hasExternalLexicalStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00003439 "DeclContext has no lexical decls in storage");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003440
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003441 // There might be lexical decls in multiple parts of the chain, for the TU
3442 // at least.
Sebastian Redl4a9eb262010-09-28 02:24:44 +00003443 // DeclContextOffsets might reallocate as we load additional decls below,
3444 // so make a copy of the vector.
3445 DeclContextInfos Infos = DeclContextOffsets[DC];
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003446 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3447 I != E; ++I) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003448 // IDs can be 0 if this context doesn't contain declarations.
3449 if (!I->LexicalDecls)
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003450 continue;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003451
3452 // Load all of the declaration IDs
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00003453 for (const KindDeclIDPair *ID = I->LexicalDecls,
3454 *IDE = ID + I->NumLexicalDecls; ID != IDE; ++ID) {
3455 if (isKindWeWant && !isKindWeWant((Decl::Kind)ID->first))
3456 continue;
3457
3458 Decl *D = GetDecl(ID->second);
Sebastian Redl4a9eb262010-09-28 02:24:44 +00003459 assert(D && "Null decl in lexical decls");
3460 Decls.push_back(D);
3461 }
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003462 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003463
Douglas Gregor25123082009-04-22 22:34:57 +00003464 ++NumLexicalDeclContextsRead;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003465 return false;
3466}
3467
John McCall76bd1f32010-06-01 09:23:16 +00003468DeclContext::lookup_result
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003469ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +00003470 DeclarationName Name) {
Mike Stump1eb44332009-09-09 15:08:12 +00003471 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00003472 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003473 if (!Name)
3474 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
3475 DeclContext::lookup_iterator(0));
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003476
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003477 llvm::SmallVector<NamedDecl *, 64> Decls;
Sebastian Redl8b122732010-08-24 00:49:55 +00003478 // There might be visible decls in multiple parts of the chain, for the TU
Sebastian Redl5967d622010-08-24 00:50:16 +00003479 // and namespaces. For any given name, the last available results replace
3480 // all earlier ones. For this reason, we walk in reverse.
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003481 DeclContextInfos &Infos = DeclContextOffsets[DC];
Sebastian Redl5967d622010-08-24 00:50:16 +00003482 for (DeclContextInfos::reverse_iterator I = Infos.rbegin(), E = Infos.rend();
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003483 I != E; ++I) {
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003484 if (!I->NameLookupTableData)
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003485 continue;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003486
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003487 ASTDeclContextNameLookupTable *LookupTable =
3488 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3489 ASTDeclContextNameLookupTable::iterator Pos = LookupTable->find(Name);
3490 if (Pos == LookupTable->end())
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003491 continue;
3492
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003493 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
3494 for (; Data.first != Data.second; ++Data.first)
3495 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
Sebastian Redl5967d622010-08-24 00:50:16 +00003496 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003497 }
3498
Douglas Gregor25123082009-04-22 22:34:57 +00003499 ++NumVisibleDeclContextsRead;
John McCall76bd1f32010-06-01 09:23:16 +00003500
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003501 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall76bd1f32010-06-01 09:23:16 +00003502 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003503}
3504
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +00003505void ASTReader::MaterializeVisibleDecls(const DeclContext *DC) {
3506 assert(DC->hasExternalVisibleStorage() &&
3507 "DeclContext has no visible decls in storage");
3508
3509 llvm::SmallVector<NamedDecl *, 64> Decls;
3510 // There might be visible decls in multiple parts of the chain, for the TU
3511 // and namespaces.
3512 DeclContextInfos &Infos = DeclContextOffsets[DC];
3513 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end();
3514 I != E; ++I) {
3515 if (!I->NameLookupTableData)
3516 continue;
3517
3518 ASTDeclContextNameLookupTable *LookupTable =
3519 (ASTDeclContextNameLookupTable*)I->NameLookupTableData;
3520 for (ASTDeclContextNameLookupTable::item_iterator
3521 ItemI = LookupTable->item_begin(),
3522 ItemEnd = LookupTable->item_end() ; ItemI != ItemEnd; ++ItemI) {
3523 ASTDeclContextNameLookupTable::item_iterator::value_type Val
3524 = *ItemI;
3525 ASTDeclContextNameLookupTrait::data_type Data = Val.second;
3526 Decls.clear();
3527 for (; Data.first != Data.second; ++Data.first)
3528 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first)));
3529 MaterializeVisibleDeclsForName(DC, Val.first, Decls);
3530 }
3531 }
3532}
3533
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003534void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003535 assert(Consumer);
3536 while (!InterestingDecls.empty()) {
3537 DeclGroupRef DG(InterestingDecls.front());
3538 InterestingDecls.pop_front();
Sebastian Redl27372b42010-08-11 18:52:41 +00003539 Consumer->HandleInterestingDecl(DG);
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003540 }
3541}
3542
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003543void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor0af2ca42009-04-22 19:09:20 +00003544 this->Consumer = Consumer;
3545
Douglas Gregorfdd01722009-04-14 00:24:19 +00003546 if (!Consumer)
3547 return;
3548
3549 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003550 // Force deserialization of this decl, which will cause it to be queued for
3551 // passing to the consumer.
Daniel Dunbar04a0b502009-09-17 03:06:44 +00003552 GetDecl(ExternalDefinitions[I]);
Douglas Gregorfdd01722009-04-14 00:24:19 +00003553 }
Douglas Gregorc62a2fe2009-04-25 00:41:30 +00003554
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00003555 PassInterestingDeclsToConsumer();
Douglas Gregorfdd01722009-04-14 00:24:19 +00003556}
3557
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003558void ASTReader::PrintStats() {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003559 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregor2cf26342009-04-09 22:27:44 +00003560
Mike Stump1eb44332009-09-09 15:08:12 +00003561 unsigned NumTypesLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003562 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall0953e762009-09-24 19:53:00 +00003563 QualType());
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003564 unsigned NumDeclsLoaded
3565 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
3566 (Decl *)0);
3567 unsigned NumIdentifiersLoaded
3568 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
3569 IdentifiersLoaded.end(),
3570 (IdentifierInfo *)0);
Mike Stump1eb44332009-09-09 15:08:12 +00003571 unsigned NumSelectorsLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003572 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
3573 SelectorsLoaded.end(),
3574 Selector());
Douglas Gregor2d41cc12009-04-13 20:50:16 +00003575
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003576 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
3577 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00003578 if (TotalNumSLocEntries)
3579 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
3580 NumSLocEntriesRead, TotalNumSLocEntries,
3581 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003582 if (!TypesLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003583 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003584 NumTypesLoaded, (unsigned)TypesLoaded.size(),
3585 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
3586 if (!DeclsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003587 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003588 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
3589 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003590 if (!IdentifiersLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003591 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003592 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
3593 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Sebastian Redl725cd962010-08-04 20:40:17 +00003594 if (!SelectorsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00003595 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redl725cd962010-08-04 20:40:17 +00003596 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
3597 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor83941df2009-04-25 17:48:32 +00003598 if (TotalNumStatements)
3599 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
3600 NumStatementsRead, TotalNumStatements,
3601 ((float)NumStatementsRead/TotalNumStatements * 100));
3602 if (TotalNumMacros)
3603 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
3604 NumMacrosRead, TotalNumMacros,
3605 ((float)NumMacrosRead/TotalNumMacros * 100));
3606 if (TotalLexicalDeclContexts)
3607 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
3608 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
3609 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
3610 * 100));
3611 if (TotalVisibleDeclContexts)
3612 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
3613 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
3614 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
3615 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003616 if (TotalNumMethodPoolEntries) {
Douglas Gregor83941df2009-04-25 17:48:32 +00003617 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003618 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
3619 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor83941df2009-04-25 17:48:32 +00003620 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003621 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor83941df2009-04-25 17:48:32 +00003622 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003623 std::fprintf(stderr, "\n");
3624}
3625
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003626void ASTReader::InitializeSema(Sema &S) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00003627 SemaObj = &S;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003628 S.ExternalSource = this;
3629
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00003630 // Makes sure any declarations that were deserialized "too early"
3631 // still get added to the identifier's declaration chains.
Douglas Gregor76dc8892010-09-24 23:29:12 +00003632 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
3633 if (SemaObj->TUScope)
John McCalld226f652010-08-21 09:40:31 +00003634 SemaObj->TUScope->AddDecl(PreloadedDecls[I]);
Douglas Gregor76dc8892010-09-24 23:29:12 +00003635
3636 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregor668c1a42009-04-21 22:25:48 +00003637 }
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00003638 PreloadedDecls.clear();
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003639
3640 // If there were any tentative definitions, deserialize them and add
Sebastian Redle9d12b62010-01-31 22:27:38 +00003641 // them to Sema's list of tentative definitions.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003642 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
3643 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
Sebastian Redle9d12b62010-01-31 22:27:38 +00003644 SemaObj->TentativeDefinitions.push_back(Var);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003645 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00003646
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003647 // If there were any unused file scoped decls, deserialize them and add to
3648 // Sema's list of unused file scoped decls.
3649 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
3650 DeclaratorDecl *D = cast<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
3651 SemaObj->UnusedFileScopedDecls.push_back(D);
Tanya Lattnere6bbc012010-02-12 00:07:30 +00003652 }
Douglas Gregor14c22f22009-04-22 22:18:58 +00003653
3654 // If there were any locally-scoped external declarations,
3655 // deserialize them and add them to Sema's table of locally-scoped
3656 // external declarations.
3657 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
3658 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
3659 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
3660 }
Douglas Gregorb81c1702009-04-27 20:06:05 +00003661
3662 // If there were any ext_vector type declarations, deserialize them
3663 // and add them to Sema's vector of such declarations.
3664 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I)
3665 SemaObj->ExtVectorDecls.push_back(
3666 cast<TypedefDecl>(GetDecl(ExtVectorDecls[I])));
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003667
3668 // FIXME: Do VTable uses and dynamic classes deserialize too much ?
3669 // Can we cut them down before writing them ?
3670
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003671 // If there were any dynamic classes declarations, deserialize them
3672 // and add them to Sema's vector of such declarations.
3673 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I)
3674 SemaObj->DynamicClasses.push_back(
3675 cast<CXXRecordDecl>(GetDecl(DynamicClasses[I])));
Fariborz Jahanian32019832010-07-23 19:11:11 +00003676
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003677 // Load the offsets of the declarations that Sema references.
3678 // They will be lazily deserialized when needed.
3679 if (!SemaDeclRefs.empty()) {
3680 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
3681 SemaObj->StdNamespace = SemaDeclRefs[0];
3682 SemaObj->StdBadAlloc = SemaDeclRefs[1];
3683 }
3684
Sebastian Redlc3632732010-10-05 15:59:54 +00003685 for (PerFileData *F = FirstInSource; F; F = F->NextInSource) {
3686
3687 // If there are @selector references added them to its pool. This is for
3688 // implementation of -Wselector.
3689 if (!F->ReferencedSelectorsData.empty()) {
3690 unsigned int DataSize = F->ReferencedSelectorsData.size()-1;
3691 unsigned I = 0;
3692 while (I < DataSize) {
3693 Selector Sel = DecodeSelector(F->ReferencedSelectorsData[I++]);
3694 SourceLocation SelLoc = ReadSourceLocation(
3695 *F, F->ReferencedSelectorsData, I);
3696 SemaObj->ReferencedSelectors.insert(std::make_pair(Sel, SelLoc));
3697 }
3698 }
3699
3700 // If there were any pending implicit instantiations, deserialize them
3701 // and add them to Sema's queue of such instantiations.
3702 assert(F->PendingInstantiations.size() % 2 == 0 &&
3703 "Expected pairs of entries");
3704 for (unsigned Idx = 0, N = F->PendingInstantiations.size(); Idx < N;) {
3705 ValueDecl *D=cast<ValueDecl>(GetDecl(F->PendingInstantiations[Idx++]));
3706 SourceLocation Loc = ReadSourceLocation(*F, F->PendingInstantiations,Idx);
3707 SemaObj->PendingInstantiations.push_back(std::make_pair(D, Loc));
3708 }
3709 }
3710
3711 // The two special data sets below always come from the most recent PCH,
3712 // which is at the front of the chain.
3713 PerFileData &F = *Chain.front();
3714
3715 // If there were any weak undeclared identifiers, deserialize them and add to
3716 // Sema's list of weak undeclared identifiers.
3717 if (!WeakUndeclaredIdentifiers.empty()) {
3718 unsigned Idx = 0;
3719 for (unsigned I = 0, N = WeakUndeclaredIdentifiers[Idx++]; I != N; ++I) {
3720 IdentifierInfo *WeakId = GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
3721 IdentifierInfo *AliasId= GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx);
3722 SourceLocation Loc = ReadSourceLocation(F, WeakUndeclaredIdentifiers,Idx);
3723 bool Used = WeakUndeclaredIdentifiers[Idx++];
3724 Sema::WeakInfo WI(AliasId, Loc);
3725 WI.setUsed(Used);
3726 SemaObj->WeakUndeclaredIdentifiers.insert(std::make_pair(WeakId, WI));
3727 }
3728 }
3729
3730 // If there were any VTable uses, deserialize the information and add it
3731 // to Sema's vector and map of VTable uses.
3732 if (!VTableUses.empty()) {
3733 unsigned Idx = 0;
3734 for (unsigned I = 0, N = VTableUses[Idx++]; I != N; ++I) {
3735 CXXRecordDecl *Class = cast<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
3736 SourceLocation Loc = ReadSourceLocation(F, VTableUses, Idx);
3737 bool DefinitionRequired = VTableUses[Idx++];
3738 SemaObj->VTableUses.push_back(std::make_pair(Class, Loc));
3739 SemaObj->VTablesUsed[Class] = DefinitionRequired;
Fariborz Jahanian32019832010-07-23 19:11:11 +00003740 }
3741 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00003742}
3743
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003744IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Sebastian Redld8c5abb2010-08-02 18:30:12 +00003745 // Try to find this name within our on-disk hash tables. We start with the
3746 // most recent one, since that one contains the most up-to-date info.
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003747 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003748 ASTIdentifierLookupTable *IdTable
3749 = (ASTIdentifierLookupTable *)Chain[I]->IdentifierLookupTable;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00003750 if (!IdTable)
3751 continue;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003752 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003753 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003754 if (Pos == IdTable->end())
3755 continue;
Douglas Gregor668c1a42009-04-21 22:25:48 +00003756
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003757 // Dereferencing the iterator has the effect of building the
3758 // IdentifierInfo node and populating it with the various
3759 // declarations it needs.
Sebastian Redld8c5abb2010-08-02 18:30:12 +00003760 return *Pos;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00003761 }
Sebastian Redld8c5abb2010-08-02 18:30:12 +00003762 return 0;
Douglas Gregor668c1a42009-04-21 22:25:48 +00003763}
3764
Douglas Gregor95f42922010-10-14 22:11:03 +00003765namespace clang {
3766 /// \brief An identifier-lookup iterator that enumerates all of the
3767 /// identifiers stored within a set of AST files.
3768 class ASTIdentifierIterator : public IdentifierIterator {
3769 /// \brief The AST reader whose identifiers are being enumerated.
3770 const ASTReader &Reader;
3771
3772 /// \brief The current index into the chain of AST files stored in
3773 /// the AST reader.
3774 unsigned Index;
3775
3776 /// \brief The current position within the identifier lookup table
3777 /// of the current AST file.
3778 ASTIdentifierLookupTable::key_iterator Current;
3779
3780 /// \brief The end position within the identifier lookup table of
3781 /// the current AST file.
3782 ASTIdentifierLookupTable::key_iterator End;
3783
3784 public:
3785 explicit ASTIdentifierIterator(const ASTReader &Reader);
3786
3787 virtual llvm::StringRef Next();
3788 };
3789}
3790
3791ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
3792 : Reader(Reader), Index(Reader.Chain.size() - 1) {
3793 ASTIdentifierLookupTable *IdTable
3794 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
3795 Current = IdTable->key_begin();
3796 End = IdTable->key_end();
3797}
3798
3799llvm::StringRef ASTIdentifierIterator::Next() {
3800 while (Current == End) {
3801 // If we have exhausted all of our AST files, we're done.
3802 if (Index == 0)
3803 return llvm::StringRef();
3804
3805 --Index;
3806 ASTIdentifierLookupTable *IdTable
3807 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable;
3808 Current = IdTable->key_begin();
3809 End = IdTable->key_end();
3810 }
3811
3812 // We have any identifiers remaining in the current AST file; return
3813 // the next one.
3814 std::pair<const char*, unsigned> Key = *Current;
3815 ++Current;
3816 return llvm::StringRef(Key.first, Key.second);
3817}
3818
3819IdentifierIterator *ASTReader::getIdentifiers() const {
3820 return new ASTIdentifierIterator(*this);
3821}
3822
Mike Stump1eb44332009-09-09 15:08:12 +00003823std::pair<ObjCMethodList, ObjCMethodList>
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003824ASTReader::ReadMethodPool(Selector Sel) {
Sebastian Redl725cd962010-08-04 20:40:17 +00003825 // Find this selector in a hash table. We want to find the most recent entry.
3826 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3827 PerFileData &F = *Chain[I];
3828 if (!F.SelectorLookupTable)
3829 continue;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003830
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003831 ASTSelectorLookupTable *PoolTable
3832 = (ASTSelectorLookupTable*)F.SelectorLookupTable;
3833 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
Sebastian Redl725cd962010-08-04 20:40:17 +00003834 if (Pos != PoolTable->end()) {
3835 ++NumSelectorsRead;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003836 // FIXME: Not quite happy with the statistics here. We probably should
3837 // disable this tracking when called via LoadSelector.
3838 // Also, should entries without methods count as misses?
3839 ++NumMethodPoolEntriesRead;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003840 ASTSelectorLookupTrait::data_type Data = *Pos;
Sebastian Redl725cd962010-08-04 20:40:17 +00003841 if (DeserializationListener)
3842 DeserializationListener->SelectorRead(Data.ID, Sel);
3843 return std::make_pair(Data.Instance, Data.Factory);
3844 }
Douglas Gregor83941df2009-04-25 17:48:32 +00003845 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003846
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003847 ++NumMethodPoolMisses;
Sebastian Redl725cd962010-08-04 20:40:17 +00003848 return std::pair<ObjCMethodList, ObjCMethodList>();
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003849}
3850
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003851void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redle58aa892010-08-04 18:21:41 +00003852 // It would be complicated to avoid reading the methods anyway. So don't.
3853 ReadMethodPool(Sel);
3854}
3855
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003856void ASTReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00003857 assert(ID && "Non-zero identifier ID required");
Douglas Gregora02b1472009-04-28 21:53:25 +00003858 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003859 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003860 if (DeserializationListener)
3861 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregor668c1a42009-04-21 22:25:48 +00003862}
3863
Douglas Gregord89275b2009-07-06 18:54:52 +00003864/// \brief Set the globally-visible declarations associated with the given
3865/// identifier.
3866///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003867/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump1eb44332009-09-09 15:08:12 +00003868/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregord89275b2009-07-06 18:54:52 +00003869/// them.
3870///
3871/// \param II an IdentifierInfo that refers to one or more globally-visible
3872/// declarations.
3873///
3874/// \param DeclIDs the set of declaration IDs with the name @p II that are
3875/// visible at global scope.
3876///
3877/// \param Nonrecursive should be true to indicate that the caller knows that
3878/// this call is non-recursive, and therefore the globally-visible declarations
3879/// will not be placed onto the pending queue.
Mike Stump1eb44332009-09-09 15:08:12 +00003880void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003881ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Douglas Gregord89275b2009-07-06 18:54:52 +00003882 const llvm::SmallVectorImpl<uint32_t> &DeclIDs,
3883 bool Nonrecursive) {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00003884 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregord89275b2009-07-06 18:54:52 +00003885 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
3886 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
3887 PII.II = II;
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00003888 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregord89275b2009-07-06 18:54:52 +00003889 return;
3890 }
Mike Stump1eb44332009-09-09 15:08:12 +00003891
Douglas Gregord89275b2009-07-06 18:54:52 +00003892 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
3893 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
3894 if (SemaObj) {
Douglas Gregor914ed9d2010-08-13 03:15:25 +00003895 if (SemaObj->TUScope) {
3896 // Introduce this declaration into the translation-unit scope
3897 // and add it to the declaration chain for this identifier, so
3898 // that (unqualified) name lookup will find it.
John McCalld226f652010-08-21 09:40:31 +00003899 SemaObj->TUScope->AddDecl(D);
Douglas Gregor914ed9d2010-08-13 03:15:25 +00003900 }
Douglas Gregor76dc8892010-09-24 23:29:12 +00003901 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
Douglas Gregord89275b2009-07-06 18:54:52 +00003902 } else {
3903 // Queue this declaration so that it will be added to the
3904 // translation unit scope and identifier's declaration chain
3905 // once a Sema object is known.
3906 PreloadedDecls.push_back(D);
3907 }
3908 }
3909}
3910
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003911IdentifierInfo *ASTReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00003912 if (ID == 0)
3913 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003914
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00003915 if (IdentifiersLoaded.empty()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003916 Error("no identifier table in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00003917 return 0;
3918 }
Mike Stump1eb44332009-09-09 15:08:12 +00003919
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003920 assert(PP && "Forgot to set Preprocessor ?");
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00003921 ID -= 1;
3922 if (!IdentifiersLoaded[ID]) {
3923 unsigned Index = ID;
3924 const char *Str = 0;
3925 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3926 PerFileData *F = Chain[N - I - 1];
3927 if (Index < F->LocalNumIdentifiers) {
3928 uint32_t Offset = F->IdentifierOffsets[Index];
3929 Str = F->IdentifierTableData + Offset;
3930 break;
3931 }
3932 Index -= F->LocalNumIdentifiers;
3933 }
3934 assert(Str && "Broken Chain");
Douglas Gregord6595a42009-04-25 21:04:17 +00003935
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003936 // All of the strings in the AST file are preceded by a 16-bit length.
3937 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenek231bc0b2009-10-23 04:45:31 +00003938 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
3939 // unsigned integers. This is important to avoid integer overflow when
3940 // we cast them to 'unsigned'.
Ted Kremenekff1ea462009-10-23 03:57:22 +00003941 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregor02fc7512009-04-28 20:01:51 +00003942 unsigned StrLen = (((unsigned) StrLenPtr[0])
3943 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00003944 IdentifiersLoaded[ID]
Kovarththanan Rajaratnam811f4262010-03-12 10:32:27 +00003945 = &PP->getIdentifierTable().get(Str, StrLen);
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003946 if (DeserializationListener)
3947 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregorafaf3082009-04-11 00:14:32 +00003948 }
Mike Stump1eb44332009-09-09 15:08:12 +00003949
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00003950 return IdentifiersLoaded[ID];
Douglas Gregor2cf26342009-04-09 22:27:44 +00003951}
3952
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003953void ASTReader::ReadSLocEntry(unsigned ID) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00003954 ReadSLocEntryRecord(ID);
3955}
3956
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003957Selector ASTReader::DecodeSelector(unsigned ID) {
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003958 if (ID == 0)
3959 return Selector();
Mike Stump1eb44332009-09-09 15:08:12 +00003960
Sebastian Redl725cd962010-08-04 20:40:17 +00003961 if (ID > SelectorsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003962 Error("selector ID out of range in AST file");
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003963 return Selector();
3964 }
Douglas Gregor83941df2009-04-25 17:48:32 +00003965
Sebastian Redl725cd962010-08-04 20:40:17 +00003966 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor83941df2009-04-25 17:48:32 +00003967 // Load this selector from the selector table.
Sebastian Redl725cd962010-08-04 20:40:17 +00003968 unsigned Idx = ID - 1;
3969 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
3970 PerFileData &F = *Chain[N - I - 1];
3971 if (Idx < F.LocalNumSelectors) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003972 ASTSelectorLookupTrait Trait(*this);
Sebastian Redl725cd962010-08-04 20:40:17 +00003973 SelectorsLoaded[ID - 1] =
3974 Trait.ReadKey(F.SelectorLookupTableData + F.SelectorOffsets[Idx], 0);
3975 if (DeserializationListener)
3976 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
3977 break;
3978 }
3979 Idx -= F.LocalNumSelectors;
3980 }
Douglas Gregor83941df2009-04-25 17:48:32 +00003981 }
3982
Sebastian Redl725cd962010-08-04 20:40:17 +00003983 return SelectorsLoaded[ID - 1];
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003984}
3985
Michael J. Spencer20249a12010-10-21 03:16:25 +00003986Selector ASTReader::GetExternalSelector(uint32_t ID) {
Douglas Gregor719770d2010-04-06 17:30:22 +00003987 return DecodeSelector(ID);
3988}
3989
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003990uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redl725cd962010-08-04 20:40:17 +00003991 // ID 0 (the null selector) is considered an external selector.
3992 return getTotalNumSelectors() + 1;
Douglas Gregor719770d2010-04-06 17:30:22 +00003993}
3994
Mike Stump1eb44332009-09-09 15:08:12 +00003995DeclarationName
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003996ASTReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003997 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
3998 switch (Kind) {
3999 case DeclarationName::Identifier:
4000 return DeclarationName(GetIdentifierInfo(Record, Idx));
4001
4002 case DeclarationName::ObjCZeroArgSelector:
4003 case DeclarationName::ObjCOneArgSelector:
4004 case DeclarationName::ObjCMultiArgSelector:
Steve Naroffa7503a72009-04-23 15:15:40 +00004005 return DeclarationName(GetSelector(Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004006
4007 case DeclarationName::CXXConstructorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00004008 return Context->DeclarationNames.getCXXConstructorName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00004009 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004010
4011 case DeclarationName::CXXDestructorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00004012 return Context->DeclarationNames.getCXXDestructorName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00004013 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004014
4015 case DeclarationName::CXXConversionFunctionName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00004016 return Context->DeclarationNames.getCXXConversionFunctionName(
Douglas Gregor50d62d12009-08-05 05:36:45 +00004017 Context->getCanonicalType(GetType(Record[Idx++])));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004018
4019 case DeclarationName::CXXOperatorName:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00004020 return Context->DeclarationNames.getCXXOperatorName(
Douglas Gregor2cf26342009-04-09 22:27:44 +00004021 (OverloadedOperatorKind)Record[Idx++]);
4022
Sean Hunt3e518bd2009-11-29 07:34:05 +00004023 case DeclarationName::CXXLiteralOperatorName:
4024 return Context->DeclarationNames.getCXXLiteralOperatorName(
4025 GetIdentifierInfo(Record, Idx));
4026
Douglas Gregor2cf26342009-04-09 22:27:44 +00004027 case DeclarationName::CXXUsingDirective:
4028 return DeclarationName::getUsingDirectiveName();
4029 }
4030
4031 // Required to silence GCC warning
4032 return DeclarationName();
4033}
Douglas Gregor0a0428e2009-04-10 20:39:37 +00004034
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004035void ASTReader::ReadDeclarationNameLoc(PerFileData &F,
4036 DeclarationNameLoc &DNLoc,
4037 DeclarationName Name,
4038 const RecordData &Record, unsigned &Idx) {
4039 switch (Name.getNameKind()) {
4040 case DeclarationName::CXXConstructorName:
4041 case DeclarationName::CXXDestructorName:
4042 case DeclarationName::CXXConversionFunctionName:
4043 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
4044 break;
4045
4046 case DeclarationName::CXXOperatorName:
4047 DNLoc.CXXOperatorName.BeginOpNameLoc
4048 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4049 DNLoc.CXXOperatorName.EndOpNameLoc
4050 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4051 break;
4052
4053 case DeclarationName::CXXLiteralOperatorName:
4054 DNLoc.CXXLiteralOperatorName.OpNameLoc
4055 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
4056 break;
4057
4058 case DeclarationName::Identifier:
4059 case DeclarationName::ObjCZeroArgSelector:
4060 case DeclarationName::ObjCOneArgSelector:
4061 case DeclarationName::ObjCMultiArgSelector:
4062 case DeclarationName::CXXUsingDirective:
4063 break;
4064 }
4065}
4066
4067void ASTReader::ReadDeclarationNameInfo(PerFileData &F,
4068 DeclarationNameInfo &NameInfo,
4069 const RecordData &Record, unsigned &Idx) {
4070 NameInfo.setName(ReadDeclarationName(Record, Idx));
4071 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
4072 DeclarationNameLoc DNLoc;
4073 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
4074 NameInfo.setInfo(DNLoc);
4075}
4076
4077void ASTReader::ReadQualifierInfo(PerFileData &F, QualifierInfo &Info,
4078 const RecordData &Record, unsigned &Idx) {
4079 Info.NNS = ReadNestedNameSpecifier(Record, Idx);
4080 Info.NNSRange = ReadSourceRange(F, Record, Idx);
4081 unsigned NumTPLists = Record[Idx++];
4082 Info.NumTemplParamLists = NumTPLists;
4083 if (NumTPLists) {
4084 Info.TemplParamLists = new (*Context) TemplateParameterList*[NumTPLists];
4085 for (unsigned i=0; i != NumTPLists; ++i)
4086 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
4087 }
4088}
4089
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004090TemplateName
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004091ASTReader::ReadTemplateName(const RecordData &Record, unsigned &Idx) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00004092 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004093 switch (Kind) {
4094 case TemplateName::Template:
4095 return TemplateName(cast_or_null<TemplateDecl>(GetDecl(Record[Idx++])));
4096
4097 case TemplateName::OverloadedTemplate: {
4098 unsigned size = Record[Idx++];
4099 UnresolvedSet<8> Decls;
4100 while (size--)
4101 Decls.addDecl(cast<NamedDecl>(GetDecl(Record[Idx++])));
4102
4103 return Context->getOverloadedTemplateName(Decls.begin(), Decls.end());
4104 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004105
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004106 case TemplateName::QualifiedTemplate: {
4107 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
4108 bool hasTemplKeyword = Record[Idx++];
4109 TemplateDecl *Template = cast<TemplateDecl>(GetDecl(Record[Idx++]));
4110 return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
4111 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004112
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004113 case TemplateName::DependentTemplate: {
4114 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx);
4115 if (Record[Idx++]) // isIdentifier
4116 return Context->getDependentTemplateName(NNS,
4117 GetIdentifierInfo(Record, Idx));
4118 return Context->getDependentTemplateName(NNS,
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004119 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004120 }
4121 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004122
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004123 assert(0 && "Unhandled template name kind!");
4124 return TemplateName();
4125}
4126
4127TemplateArgument
Sebastian Redlc3632732010-10-05 15:59:54 +00004128ASTReader::ReadTemplateArgument(PerFileData &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00004129 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004130 switch ((TemplateArgument::ArgKind)Record[Idx++]) {
4131 case TemplateArgument::Null:
4132 return TemplateArgument();
4133 case TemplateArgument::Type:
4134 return TemplateArgument(GetType(Record[Idx++]));
4135 case TemplateArgument::Declaration:
4136 return TemplateArgument(GetDecl(Record[Idx++]));
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00004137 case TemplateArgument::Integral: {
4138 llvm::APSInt Value = ReadAPSInt(Record, Idx);
4139 QualType T = GetType(Record[Idx++]);
4140 return TemplateArgument(Value, T);
4141 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004142 case TemplateArgument::Template:
4143 return TemplateArgument(ReadTemplateName(Record, Idx));
4144 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00004145 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004146 case TemplateArgument::Pack: {
4147 unsigned NumArgs = Record[Idx++];
4148 llvm::SmallVector<TemplateArgument, 8> Args;
4149 Args.reserve(NumArgs);
4150 while (NumArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00004151 Args.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004152 TemplateArgument TemplArg;
4153 TemplArg.setArgumentPack(Args.data(), Args.size(), /*CopyArgs=*/true);
4154 return TemplArg;
4155 }
4156 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004157
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004158 assert(0 && "Unhandled template argument kind!");
4159 return TemplateArgument();
4160}
4161
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004162TemplateParameterList *
Sebastian Redlc3632732010-10-05 15:59:54 +00004163ASTReader::ReadTemplateParameterList(PerFileData &F,
4164 const RecordData &Record, unsigned &Idx) {
4165 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
4166 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
4167 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004168
4169 unsigned NumParams = Record[Idx++];
4170 llvm::SmallVector<NamedDecl *, 16> Params;
4171 Params.reserve(NumParams);
4172 while (NumParams--)
4173 Params.push_back(cast<NamedDecl>(GetDecl(Record[Idx++])));
Michael J. Spencer20249a12010-10-21 03:16:25 +00004174
4175 TemplateParameterList* TemplateParams =
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004176 TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc,
4177 Params.data(), Params.size(), RAngleLoc);
4178 return TemplateParams;
4179}
4180
4181void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004182ASTReader::
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004183ReadTemplateArgumentList(llvm::SmallVector<TemplateArgument, 8> &TemplArgs,
Sebastian Redlc3632732010-10-05 15:59:54 +00004184 PerFileData &F, const RecordData &Record,
4185 unsigned &Idx) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004186 unsigned NumTemplateArgs = Record[Idx++];
4187 TemplArgs.reserve(NumTemplateArgs);
4188 while (NumTemplateArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00004189 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004190}
4191
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004192/// \brief Read a UnresolvedSet structure.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004193void ASTReader::ReadUnresolvedSet(UnresolvedSetImpl &Set,
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004194 const RecordData &Record, unsigned &Idx) {
4195 unsigned NumDecls = Record[Idx++];
4196 while (NumDecls--) {
4197 NamedDecl *D = cast<NamedDecl>(GetDecl(Record[Idx++]));
4198 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
4199 Set.addDecl(D, AS);
4200 }
4201}
4202
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004203CXXBaseSpecifier
Sebastian Redlc3632732010-10-05 15:59:54 +00004204ASTReader::ReadCXXBaseSpecifier(PerFileData &F,
Nick Lewycky56062202010-07-26 16:56:01 +00004205 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004206 bool isVirtual = static_cast<bool>(Record[Idx++]);
4207 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
4208 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00004209 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
4210 SourceRange Range = ReadSourceRange(F, Record, Idx);
Nick Lewycky56062202010-07-26 16:56:01 +00004211 return CXXBaseSpecifier(Range, isVirtual, isBaseOfClass, AS, TInfo);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004212}
4213
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004214std::pair<CXXBaseOrMemberInitializer **, unsigned>
Sebastian Redlc3632732010-10-05 15:59:54 +00004215ASTReader::ReadCXXBaseOrMemberInitializers(PerFileData &F,
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004216 const RecordData &Record,
4217 unsigned &Idx) {
4218 CXXBaseOrMemberInitializer **BaseOrMemberInitializers = 0;
4219 unsigned NumInitializers = Record[Idx++];
4220 if (NumInitializers) {
4221 ASTContext &C = *getContext();
4222
4223 BaseOrMemberInitializers
4224 = new (C) CXXBaseOrMemberInitializer*[NumInitializers];
4225 for (unsigned i=0; i != NumInitializers; ++i) {
4226 TypeSourceInfo *BaseClassInfo = 0;
4227 bool IsBaseVirtual = false;
4228 FieldDecl *Member = 0;
Michael J. Spencer20249a12010-10-21 03:16:25 +00004229
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004230 bool IsBaseInitializer = Record[Idx++];
4231 if (IsBaseInitializer) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004232 BaseClassInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004233 IsBaseVirtual = Record[Idx++];
4234 } else {
4235 Member = cast<FieldDecl>(GetDecl(Record[Idx++]));
4236 }
Sebastian Redlc3632732010-10-05 15:59:54 +00004237 SourceLocation MemberLoc = ReadSourceLocation(F, Record, Idx);
4238 Expr *Init = ReadExpr(F);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004239 FieldDecl *AnonUnionMember
4240 = cast_or_null<FieldDecl>(GetDecl(Record[Idx++]));
Sebastian Redlc3632732010-10-05 15:59:54 +00004241 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
4242 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004243 bool IsWritten = Record[Idx++];
4244 unsigned SourceOrderOrNumArrayIndices;
4245 llvm::SmallVector<VarDecl *, 8> Indices;
4246 if (IsWritten) {
4247 SourceOrderOrNumArrayIndices = Record[Idx++];
4248 } else {
4249 SourceOrderOrNumArrayIndices = Record[Idx++];
4250 Indices.reserve(SourceOrderOrNumArrayIndices);
4251 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
4252 Indices.push_back(cast<VarDecl>(GetDecl(Record[Idx++])));
4253 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004254
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004255 CXXBaseOrMemberInitializer *BOMInit;
4256 if (IsBaseInitializer) {
4257 BOMInit = new (C) CXXBaseOrMemberInitializer(C, BaseClassInfo,
4258 IsBaseVirtual, LParenLoc,
4259 Init, RParenLoc);
4260 } else if (IsWritten) {
4261 BOMInit = new (C) CXXBaseOrMemberInitializer(C, Member, MemberLoc,
4262 LParenLoc, Init, RParenLoc);
4263 } else {
4264 BOMInit = CXXBaseOrMemberInitializer::Create(C, Member, MemberLoc,
4265 LParenLoc, Init, RParenLoc,
4266 Indices.data(),
4267 Indices.size());
4268 }
4269
Argyrios Kyrtzidisf84cde12010-09-06 19:04:27 +00004270 if (IsWritten)
4271 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004272 BOMInit->setAnonUnionMember(AnonUnionMember);
4273 BaseOrMemberInitializers[i] = BOMInit;
4274 }
4275 }
4276
4277 return std::make_pair(BaseOrMemberInitializers, NumInitializers);
4278}
4279
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004280NestedNameSpecifier *
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004281ASTReader::ReadNestedNameSpecifier(const RecordData &Record, unsigned &Idx) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004282 unsigned N = Record[Idx++];
4283 NestedNameSpecifier *NNS = 0, *Prev = 0;
4284 for (unsigned I = 0; I != N; ++I) {
4285 NestedNameSpecifier::SpecifierKind Kind
4286 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
4287 switch (Kind) {
4288 case NestedNameSpecifier::Identifier: {
4289 IdentifierInfo *II = GetIdentifierInfo(Record, Idx);
4290 NNS = NestedNameSpecifier::Create(*Context, Prev, II);
4291 break;
4292 }
4293
4294 case NestedNameSpecifier::Namespace: {
4295 NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++]));
4296 NNS = NestedNameSpecifier::Create(*Context, Prev, NS);
4297 break;
4298 }
4299
4300 case NestedNameSpecifier::TypeSpec:
4301 case NestedNameSpecifier::TypeSpecWithTemplate: {
4302 Type *T = GetType(Record[Idx++]).getTypePtr();
4303 bool Template = Record[Idx++];
4304 NNS = NestedNameSpecifier::Create(*Context, Prev, Template, T);
4305 break;
4306 }
4307
4308 case NestedNameSpecifier::Global: {
4309 NNS = NestedNameSpecifier::GlobalSpecifier(*Context);
4310 // No associated value, and there can't be a prefix.
4311 break;
4312 }
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004313 }
Argyrios Kyrtzidisd2bb2c02010-07-07 15:46:30 +00004314 Prev = NNS;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004315 }
4316 return NNS;
4317}
4318
4319SourceRange
Sebastian Redlc3632732010-10-05 15:59:54 +00004320ASTReader::ReadSourceRange(PerFileData &F, const RecordData &Record,
4321 unsigned &Idx) {
4322 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
4323 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar8ee59392010-06-02 15:47:10 +00004324 return SourceRange(beg, end);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004325}
4326
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004327/// \brief Read an integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004328llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004329 unsigned BitWidth = Record[Idx++];
4330 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
4331 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
4332 Idx += NumWords;
4333 return Result;
4334}
4335
4336/// \brief Read a signed integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004337llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004338 bool isUnsigned = Record[Idx++];
4339 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
4340}
4341
Douglas Gregor17fc2232009-04-14 21:55:33 +00004342/// \brief Read a floating-point value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004343llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00004344 return llvm::APFloat(ReadAPInt(Record, Idx));
4345}
4346
Douglas Gregor68a2eb02009-04-15 21:30:51 +00004347// \brief Read a string
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004348std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00004349 unsigned Len = Record[Idx++];
Jay Foadbeaaccd2009-05-21 09:52:38 +00004350 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00004351 Idx += Len;
4352 return Result;
4353}
4354
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004355CXXTemporary *ASTReader::ReadCXXTemporary(const RecordData &Record,
Chris Lattnerd2598362010-05-10 00:25:06 +00004356 unsigned &Idx) {
4357 CXXDestructorDecl *Decl = cast<CXXDestructorDecl>(GetDecl(Record[Idx++]));
4358 return CXXTemporary::Create(*Context, Decl);
4359}
4360
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004361DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00004362 return Diag(SourceLocation(), DiagID);
4363}
4364
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004365DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00004366 return Diags.Report(FullSourceLoc(Loc, SourceMgr), DiagID);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00004367}
Douglas Gregor025452f2009-04-17 00:04:06 +00004368
Douglas Gregor668c1a42009-04-21 22:25:48 +00004369/// \brief Retrieve the identifier table associated with the
4370/// preprocessor.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004371IdentifierTable &ASTReader::getIdentifierTable() {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00004372 assert(PP && "Forgot to set Preprocessor ?");
4373 return PP->getIdentifierTable();
Douglas Gregor668c1a42009-04-21 22:25:48 +00004374}
4375
Douglas Gregor025452f2009-04-17 00:04:06 +00004376/// \brief Record that the given ID maps to the given switch-case
4377/// statement.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004378void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Douglas Gregor025452f2009-04-17 00:04:06 +00004379 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
4380 SwitchCaseStmts[ID] = SC;
4381}
4382
4383/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004384SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Douglas Gregor025452f2009-04-17 00:04:06 +00004385 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
4386 return SwitchCaseStmts[ID];
4387}
Douglas Gregor1de05fe2009-04-17 18:18:49 +00004388
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00004389void ASTReader::ClearSwitchCaseIDs() {
4390 SwitchCaseStmts.clear();
4391}
4392
Douglas Gregor1de05fe2009-04-17 18:18:49 +00004393/// \brief Record that the given label statement has been
4394/// deserialized and has the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004395void ASTReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
Mike Stump1eb44332009-09-09 15:08:12 +00004396 assert(LabelStmts.find(ID) == LabelStmts.end() &&
Douglas Gregor1de05fe2009-04-17 18:18:49 +00004397 "Deserialized label twice");
4398 LabelStmts[ID] = S;
4399
4400 // If we've already seen any goto statements that point to this
4401 // label, resolve them now.
4402 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
4403 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
4404 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
4405 Goto->second->setLabel(S);
4406 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00004407
4408 // If we've already seen any address-label statements that point to
4409 // this label, resolve them now.
4410 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
Mike Stump1eb44332009-09-09 15:08:12 +00004411 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00004412 = UnresolvedAddrLabelExprs.equal_range(ID);
Mike Stump1eb44332009-09-09 15:08:12 +00004413 for (AddrLabelIter AddrLabel = AddrLabels.first;
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00004414 AddrLabel != AddrLabels.second; ++AddrLabel)
4415 AddrLabel->second->setLabel(S);
4416 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor1de05fe2009-04-17 18:18:49 +00004417}
4418
4419/// \brief Set the label of the given statement to the label
4420/// identified by ID.
4421///
4422/// Depending on the order in which the label and other statements
4423/// referencing that label occur, this operation may complete
4424/// immediately (updating the statement) or it may queue the
4425/// statement to be back-patched later.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004426void ASTReader::SetLabelOf(GotoStmt *S, unsigned ID) {
Douglas Gregor1de05fe2009-04-17 18:18:49 +00004427 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
4428 if (Label != LabelStmts.end()) {
4429 // We've already seen this label, so set the label of the goto and
4430 // we're done.
4431 S->setLabel(Label->second);
4432 } else {
4433 // We haven't seen this label yet, so add this goto to the set of
4434 // unresolved goto statements.
4435 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
4436 }
4437}
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00004438
4439/// \brief Set the label of the given expression to the label
4440/// identified by ID.
4441///
4442/// Depending on the order in which the label and other statements
4443/// referencing that label occur, this operation may complete
4444/// immediately (updating the statement) or it may queue the
4445/// statement to be back-patched later.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004446void ASTReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00004447 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
4448 if (Label != LabelStmts.end()) {
4449 // We've already seen this label, so set the label of the
4450 // label-address expression and we're done.
4451 S->setLabel(Label->second);
4452 } else {
4453 // We haven't seen this label yet, so add this label-address
4454 // expression to the set of unresolved label-address expressions.
4455 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
4456 }
4457}
Douglas Gregord89275b2009-07-06 18:54:52 +00004458
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004459void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004460 assert(NumCurrentElementsDeserializing &&
4461 "FinishedDeserializing not paired with StartedDeserializing");
4462 if (NumCurrentElementsDeserializing == 1) {
Douglas Gregord89275b2009-07-06 18:54:52 +00004463 // If any identifiers with corresponding top-level declarations have
4464 // been loaded, load those declarations now.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004465 while (!PendingIdentifierInfos.empty()) {
4466 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
4467 PendingIdentifierInfos.front().DeclIDs, true);
4468 PendingIdentifierInfos.pop_front();
Douglas Gregord89275b2009-07-06 18:54:52 +00004469 }
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00004470
4471 // We are not in recursive loading, so it's safe to pass the "interesting"
4472 // decls to the consumer.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004473 if (Consumer)
4474 PassInterestingDeclsToConsumer();
Argyrios Kyrtzidis134db1f2010-10-24 17:26:31 +00004475
4476 assert(PendingForwardRefs.size() == 0 &&
4477 "Some forward refs did not get linked to the definition!");
Douglas Gregord89275b2009-07-06 18:54:52 +00004478 }
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004479 --NumCurrentElementsDeserializing;
Douglas Gregord89275b2009-07-06 18:54:52 +00004480}
Douglas Gregor501c1032010-08-19 00:28:17 +00004481
Sebastian Redle1dde812010-08-24 00:50:04 +00004482ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context,
4483 const char *isysroot, bool DisableValidation)
4484 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
4485 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +00004486 FileSystemOpts(PP.getFileSystemOpts()),
Sebastian Redle1dde812010-08-24 00:50:04 +00004487 Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context),
4488 Consumer(0), isysroot(isysroot), DisableValidation(DisableValidation),
4489 NumStatHits(0), NumStatMisses(0), NumSLocEntriesRead(0),
Sebastian Redl8db9fae2010-09-22 20:19:08 +00004490 TotalNumSLocEntries(0), NextSLocOffset(0), NumStatementsRead(0),
4491 TotalNumStatements(0), NumMacrosRead(0), TotalNumMacros(0),
4492 NumSelectorsRead(0), NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
Sebastian Redle1dde812010-08-24 00:50:04 +00004493 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
4494 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
4495 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
4496 RelocatablePCH = false;
4497}
4498
4499ASTReader::ASTReader(SourceManager &SourceMgr, FileManager &FileMgr,
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +00004500 const FileSystemOptions &FileSystemOpts,
Sebastian Redle1dde812010-08-24 00:50:04 +00004501 Diagnostic &Diags, const char *isysroot,
4502 bool DisableValidation)
4503 : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr),
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +00004504 FileSystemOpts(FileSystemOpts),
Sebastian Redle1dde812010-08-24 00:50:04 +00004505 Diags(Diags), SemaObj(0), PP(0), Context(0), Consumer(0),
4506 isysroot(isysroot), DisableValidation(DisableValidation), NumStatHits(0),
4507 NumStatMisses(0), NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Sebastian Redl8db9fae2010-09-22 20:19:08 +00004508 NextSLocOffset(0), NumStatementsRead(0), TotalNumStatements(0),
4509 NumMacrosRead(0), TotalNumMacros(0), NumSelectorsRead(0),
4510 NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0),
4511 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0),
4512 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0),
4513 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) {
Sebastian Redle1dde812010-08-24 00:50:04 +00004514 RelocatablePCH = false;
4515}
4516
4517ASTReader::~ASTReader() {
4518 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
4519 delete Chain[e - i - 1];
4520 // Delete all visible decl lookup tables
4521 for (DeclContextOffsetsMap::iterator I = DeclContextOffsets.begin(),
4522 E = DeclContextOffsets.end();
4523 I != E; ++I) {
4524 for (DeclContextInfos::iterator J = I->second.begin(), F = I->second.end();
4525 J != F; ++J) {
4526 if (J->NameLookupTableData)
4527 delete static_cast<ASTDeclContextNameLookupTable*>(
4528 J->NameLookupTableData);
4529 }
4530 }
4531 for (DeclContextVisibleUpdatesPending::iterator
4532 I = PendingVisibleUpdates.begin(),
4533 E = PendingVisibleUpdates.end();
4534 I != E; ++I) {
4535 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
4536 F = I->second.end();
4537 J != F; ++J)
4538 delete static_cast<ASTDeclContextNameLookupTable*>(*J);
4539 }
4540}
4541
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00004542ASTReader::PerFileData::PerFileData(ASTFileType Ty)
4543 : Type(Ty), SizeInBits(0), LocalNumSLocEntries(0), SLocOffsets(0), LocalSLocSize(0),
Sebastian Redl301c9b02010-09-22 00:42:27 +00004544 LocalNumIdentifiers(0), IdentifierOffsets(0), IdentifierTableData(0),
4545 IdentifierLookupTable(0), LocalNumMacroDefinitions(0),
4546 MacroDefinitionOffsets(0), LocalNumSelectors(0), SelectorOffsets(0),
4547 SelectorLookupTableData(0), SelectorLookupTable(0), LocalNumDecls(0),
Douglas Gregor7c789c12010-10-29 22:39:52 +00004548 DeclOffsets(0), LocalNumCXXBaseSpecifiers(0), CXXBaseSpecifiersOffsets(0),
4549 LocalNumTypes(0), TypeOffsets(0), StatCache(0),
Sebastian Redla866e652010-10-01 19:59:12 +00004550 NumPreallocatedPreprocessingEntities(0), NextInSource(0)
Douglas Gregor501c1032010-08-19 00:28:17 +00004551{}
4552
4553ASTReader::PerFileData::~PerFileData() {
4554 delete static_cast<ASTIdentifierLookupTable *>(IdentifierLookupTable);
4555 delete static_cast<ASTSelectorLookupTable *>(SelectorLookupTable);
4556}
4557