blob: 1462aa19646806fea36923474f2e572d2c05e3d6 [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"
Douglas Gregor98339b92011-08-25 20:47:51 +000016#include "clang/Serialization/ModuleManager.h"
Chandler Carrutha2398d72011-12-09 00:02:23 +000017#include "clang/Serialization/SerializationDiagnostic.h"
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +000018#include "ASTCommon.h"
Douglas Gregor98339b92011-08-25 20:47:51 +000019#include "ASTReaderInternals.h"
Douglas Gregore737f502010-08-12 20:07:10 +000020#include "clang/Sema/Sema.h"
John McCall5f1e0942010-08-24 08:50:51 +000021#include "clang/Sema/Scope.h"
Douglas Gregorfdd01722009-04-14 00:24:19 +000022#include "clang/AST/ASTConsumer.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000023#include "clang/AST/ASTContext.h"
John McCall2a7fb272010-08-25 05:32:35 +000024#include "clang/AST/DeclTemplate.h"
Douglas Gregor0b748912009-04-14 21:18:50 +000025#include "clang/AST/Expr.h"
John McCall7a1fad32010-08-24 07:32:53 +000026#include "clang/AST/ExprCXX.h"
Douglas Gregor5f791bb2011-02-28 23:58:31 +000027#include "clang/AST/NestedNameSpecifier.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000028#include "clang/AST/Type.h"
John McCalla1ee0c52009-10-16 21:56:05 +000029#include "clang/AST/TypeLocVisitor.h"
Chris Lattner42d42b52009-04-10 21:41:48 +000030#include "clang/Lex/MacroInfo.h"
Douglas Gregor6a5a23f2010-03-19 21:51:54 +000031#include "clang/Lex/PreprocessingRecord.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000032#include "clang/Lex/Preprocessor.h"
Steve Naroff83d63c72009-04-24 20:03:17 +000033#include "clang/Lex/HeaderSearch.h"
Douglas Gregorbbf38312012-10-24 16:50:34 +000034#include "clang/Lex/HeaderSearchOptions.h"
Douglas Gregor668c1a42009-04-21 22:25:48 +000035#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000036#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000037#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000038#include "clang/Basic/FileManager.h"
Chris Lattner10e286a2010-11-23 19:19:34 +000039#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000040#include "clang/Basic/TargetInfo.h"
Douglas Gregor57016dd2012-10-16 23:40:58 +000041#include "clang/Basic/TargetOptions.h"
Douglas Gregor445e23e2009-10-05 21:07:28 +000042#include "clang/Basic/Version.h"
Douglas Gregor0a0d2b12011-03-23 00:50:03 +000043#include "clang/Basic/VersionTuple.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000044#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000045#include "llvm/Bitcode/BitstreamReader.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000046#include "llvm/Support/MemoryBuffer.h"
John McCall833ca992009-10-29 08:12:44 +000047#include "llvm/Support/ErrorHandling.h"
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000048#include "llvm/Support/FileSystem.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000049#include "llvm/Support/Path.h"
Nick Lewyckyb346d2f2012-04-16 02:51:46 +000050#include "llvm/Support/SaveAndRestore.h"
Michael J. Spencer3a321e22010-12-09 17:36:38 +000051#include "llvm/Support/system_error.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000052#include <algorithm>
Douglas Gregore721f952009-04-28 18:58:38 +000053#include <iterator>
Douglas Gregor2cf26342009-04-09 22:27:44 +000054#include <cstdio>
Douglas Gregor4fed3f42009-04-27 18:38:38 +000055#include <sys/stat.h>
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000056
Douglas Gregor2cf26342009-04-09 22:27:44 +000057using namespace clang;
Sebastian Redl8538e8d2010-08-18 23:57:32 +000058using namespace clang::serialization;
Douglas Gregor98339b92011-08-25 20:47:51 +000059using namespace clang::serialization::reader;
Douglas Gregor2cf26342009-04-09 22:27:44 +000060
61//===----------------------------------------------------------------------===//
Sebastian Redl3c7f4132010-08-18 23:57:06 +000062// PCH validator implementation
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000063//===----------------------------------------------------------------------===//
64
Sebastian Redl571db7f2010-08-18 23:56:56 +000065ASTReaderListener::~ASTReaderListener() {}
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000066
Douglas Gregor27ffa6c2012-10-23 06:18:24 +000067/// \brief Compare the given set of language options against an existing set of
68/// language options.
69///
70/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
71///
72/// \returns true if the languagae options mis-match, false otherwise.
73static bool checkLanguageOptions(const LangOptions &LangOpts,
74 const LangOptions &ExistingLangOpts,
75 DiagnosticsEngine *Diags) {
76#define LANGOPT(Name, Bits, Default, Description) \
77 if (ExistingLangOpts.Name != LangOpts.Name) { \
78 if (Diags) \
79 Diags->Report(diag::err_pch_langopt_mismatch) \
80 << Description << LangOpts.Name << ExistingLangOpts.Name; \
81 return true; \
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000082 }
83
Douglas Gregor27ffa6c2012-10-23 06:18:24 +000084#define VALUE_LANGOPT(Name, Bits, Default, Description) \
85 if (ExistingLangOpts.Name != LangOpts.Name) { \
86 if (Diags) \
87 Diags->Report(diag::err_pch_langopt_value_mismatch) \
88 << Description; \
89 return true; \
Douglas Gregor38295be2012-10-22 23:51:00 +000090 }
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000091
Douglas Gregor27ffa6c2012-10-23 06:18:24 +000092#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
93 if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \
94 if (Diags) \
95 Diags->Report(diag::err_pch_langopt_value_mismatch) \
96 << Description; \
97 return true; \
Douglas Gregor7d5e81b2011-09-13 18:26:39 +000098 }
99
100#define BENIGN_LANGOPT(Name, Bits, Default, Description)
101#define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
102#include "clang/Basic/LangOptions.def"
John McCall260611a2012-06-20 06:18:46 +0000103
Douglas Gregor27ffa6c2012-10-23 06:18:24 +0000104 if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
105 if (Diags)
106 Diags->Report(diag::err_pch_langopt_value_mismatch)
107 << "target Objective-C runtime";
John McCall260611a2012-06-20 06:18:46 +0000108 return true;
109 }
Douglas Gregor27ffa6c2012-10-23 06:18:24 +0000110
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000111 return false;
112}
113
Douglas Gregor27ffa6c2012-10-23 06:18:24 +0000114/// \brief Compare the given set of target options against an existing set of
115/// target options.
116///
117/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
118///
119/// \returns true if the target options mis-match, false otherwise.
120static bool checkTargetOptions(const TargetOptions &TargetOpts,
121 const TargetOptions &ExistingTargetOpts,
122 DiagnosticsEngine *Diags) {
Douglas Gregor38295be2012-10-22 23:51:00 +0000123#define CHECK_TARGET_OPT(Field, Name) \
124 if (TargetOpts.Field != ExistingTargetOpts.Field) { \
Douglas Gregor27ffa6c2012-10-23 06:18:24 +0000125 if (Diags) \
126 Diags->Report(diag::err_pch_targetopt_mismatch) \
Douglas Gregor38295be2012-10-22 23:51:00 +0000127 << Name << TargetOpts.Field << ExistingTargetOpts.Field; \
128 return true; \
Douglas Gregor57016dd2012-10-16 23:40:58 +0000129 }
130
131 CHECK_TARGET_OPT(Triple, "target");
132 CHECK_TARGET_OPT(CPU, "target CPU");
133 CHECK_TARGET_OPT(ABI, "target ABI");
134 CHECK_TARGET_OPT(CXXABI, "target C++ ABI");
135 CHECK_TARGET_OPT(LinkerVersion, "target linker version");
136#undef CHECK_TARGET_OPT
137
138 // Compare feature sets.
139 SmallVector<StringRef, 4> ExistingFeatures(
Douglas Gregor27ffa6c2012-10-23 06:18:24 +0000140 ExistingTargetOpts.FeaturesAsWritten.begin(),
141 ExistingTargetOpts.FeaturesAsWritten.end());
Douglas Gregor57016dd2012-10-16 23:40:58 +0000142 SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
143 TargetOpts.FeaturesAsWritten.end());
144 std::sort(ExistingFeatures.begin(), ExistingFeatures.end());
145 std::sort(ReadFeatures.begin(), ReadFeatures.end());
146
147 unsigned ExistingIdx = 0, ExistingN = ExistingFeatures.size();
148 unsigned ReadIdx = 0, ReadN = ReadFeatures.size();
149 while (ExistingIdx < ExistingN && ReadIdx < ReadN) {
150 if (ExistingFeatures[ExistingIdx] == ReadFeatures[ReadIdx]) {
151 ++ExistingIdx;
152 ++ReadIdx;
153 continue;
154 }
155
156 if (ReadFeatures[ReadIdx] < ExistingFeatures[ExistingIdx]) {
Douglas Gregor27ffa6c2012-10-23 06:18:24 +0000157 if (Diags)
158 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
Douglas Gregor38295be2012-10-22 23:51:00 +0000159 << false << ReadFeatures[ReadIdx];
Douglas Gregor57016dd2012-10-16 23:40:58 +0000160 return true;
161 }
162
Douglas Gregor27ffa6c2012-10-23 06:18:24 +0000163 if (Diags)
164 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
Douglas Gregor38295be2012-10-22 23:51:00 +0000165 << true << ExistingFeatures[ExistingIdx];
Douglas Gregor57016dd2012-10-16 23:40:58 +0000166 return true;
167 }
168
169 if (ExistingIdx < ExistingN) {
Douglas Gregor27ffa6c2012-10-23 06:18:24 +0000170 if (Diags)
171 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
Douglas Gregor38295be2012-10-22 23:51:00 +0000172 << true << ExistingFeatures[ExistingIdx];
Douglas Gregor57016dd2012-10-16 23:40:58 +0000173 return true;
174 }
175
176 if (ReadIdx < ReadN) {
Douglas Gregor27ffa6c2012-10-23 06:18:24 +0000177 if (Diags)
178 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
Douglas Gregor38295be2012-10-22 23:51:00 +0000179 << false << ReadFeatures[ReadIdx];
Douglas Gregor57016dd2012-10-16 23:40:58 +0000180 return true;
181 }
182
183 return false;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000184}
185
Douglas Gregor27ffa6c2012-10-23 06:18:24 +0000186bool
187PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
188 bool Complain) {
189 const LangOptions &PPLangOpts = PP.getLangOpts();
190 return checkLanguageOptions(LangOpts, PPLangOpts,
191 Complain? &Reader.Diags : 0);
192}
193
194bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
195 bool Complain) {
196 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
197 return checkTargetOptions(TargetOpts, ExistingTargetOpts,
198 Complain? &Reader.Diags : 0);
199}
200
Benjamin Kramer54353f42010-11-25 18:29:30 +0000201namespace {
202 struct EmptyStringRef {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000203 bool operator ()(StringRef r) const { return r.empty(); }
Benjamin Kramer54353f42010-11-25 18:29:30 +0000204 };
205 struct EmptyBlock {
206 bool operator ()(const PCHPredefinesBlock &r) const {return r.Data.empty();}
207 };
208}
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000209
Chris Lattner5f9e2722011-07-23 10:55:15 +0000210static bool EqualConcatenations(SmallVector<StringRef, 2> L,
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000211 PCHPredefinesBlocks R) {
212 // First, sum up the lengths.
213 unsigned LL = 0, RL = 0;
214 for (unsigned I = 0, N = L.size(); I != N; ++I) {
215 LL += L[I].size();
216 }
217 for (unsigned I = 0, N = R.size(); I != N; ++I) {
218 RL += R[I].Data.size();
219 }
220 if (LL != RL)
221 return false;
222 if (LL == 0 && RL == 0)
223 return true;
224
225 // Kick out empty parts, they confuse the algorithm below.
226 L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end());
227 R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end());
228
229 // Do it the hard way. At this point, both vectors must be non-empty.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000230 StringRef LR = L[0], RR = R[0].Data;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000231 unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size();
Daniel Dunbarc76c9e02010-07-16 00:00:11 +0000232 (void) RN;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000233 for (;;) {
234 // Compare the current pieces.
235 if (LR.size() == RR.size()) {
236 // If they're the same length, it's pretty easy.
237 if (LR != RR)
238 return false;
239 // Both pieces are done, advance.
240 ++LI;
241 ++RI;
242 // If either string is done, they're both done, since they're the same
243 // length.
244 if (LI == LN) {
245 assert(RI == RN && "Strings not the same length after all?");
246 return true;
247 }
248 LR = L[LI];
249 RR = R[RI].Data;
250 } else if (LR.size() < RR.size()) {
251 // Right piece is longer.
252 if (!RR.startswith(LR))
253 return false;
254 ++LI;
255 assert(LI != LN && "Strings not the same length after all?");
256 RR = RR.substr(LR.size());
257 LR = L[LI];
258 } else {
259 // Left piece is longer.
260 if (!LR.startswith(RR))
261 return false;
262 ++RI;
263 assert(RI != RN && "Strings not the same length after all?");
264 LR = LR.substr(RR.size());
265 RR = R[RI].Data;
266 }
267 }
268}
269
Chris Lattner5f9e2722011-07-23 10:55:15 +0000270static std::pair<FileID, StringRef::size_type>
271FindMacro(const PCHPredefinesBlocks &Buffers, StringRef MacroDef) {
272 std::pair<FileID, StringRef::size_type> Res;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000273 for (unsigned I = 0, N = Buffers.size(); I != N; ++I) {
274 Res.second = Buffers[I].Data.find(MacroDef);
Chris Lattner5f9e2722011-07-23 10:55:15 +0000275 if (Res.second != StringRef::npos) {
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000276 Res.first = Buffers[I].BufferID;
277 break;
278 }
279 }
280 return Res;
281}
282
283bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000284 StringRef OriginalFileName,
Nick Lewycky277a6e72011-02-23 21:16:44 +0000285 std::string &SuggestedPredefines,
Douglas Gregor38295be2012-10-22 23:51:00 +0000286 FileManager &FileMgr,
287 bool Complain) {
Daniel Dunbarc7162932009-11-11 23:58:53 +0000288 // We are in the context of an implicit include, so the predefines buffer will
289 // have a #include entry for the PCH file itself (as normalized by the
290 // preprocessor initialization). Find it and skip over it in the checking
291 // below.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000292 SmallString<256> PCHInclude;
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000293 PCHInclude += "#include \"";
Chandler Carruthcb381ea2011-12-09 01:33:57 +0000294 PCHInclude += HeaderSearch::NormalizeDashIncludePath(OriginalFileName,
295 FileMgr);
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000296 PCHInclude += "\"\n";
Chris Lattner5f9e2722011-07-23 10:55:15 +0000297 std::pair<StringRef,StringRef> Split =
298 StringRef(PP.getPredefines()).split(PCHInclude.str());
299 StringRef Left = Split.first, Right = Split.second;
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000300 if (Left == PP.getPredefines()) {
Douglas Gregor38295be2012-10-22 23:51:00 +0000301 if (Complain)
302 Error("Missing PCH include entry!");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000303 return true;
304 }
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000305
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000306 // If the concatenation of all the PCH buffers is equal to the adjusted
307 // command line, we're done.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000308 SmallVector<StringRef, 2> CommandLine;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000309 CommandLine.push_back(Left);
310 CommandLine.push_back(Right);
311 if (EqualConcatenations(CommandLine, Buffers))
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000312 return false;
313
314 SourceManager &SourceMgr = PP.getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000315
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000316 // The predefines buffers are different. Determine what the differences are,
317 // and whether they require us to reject the PCH file.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000318 SmallVector<StringRef, 8> PCHLines;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000319 for (unsigned I = 0, N = Buffers.size(); I != N; ++I)
320 Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Daniel Dunbare6750492009-11-13 16:46:11 +0000321
Chris Lattner5f9e2722011-07-23 10:55:15 +0000322 SmallVector<StringRef, 8> CmdLineLines;
Daniel Dunbare6750492009-11-13 16:46:11 +0000323 Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000324
325 // Pick out implicit #includes after the PCH and don't consider them for
326 // validation; we will insert them into SuggestedPredefines so that the
327 // preprocessor includes them.
328 std::string IncludesAfterPCH;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000329 SmallVector<StringRef, 8> AfterPCHLines;
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000330 Right.split(AfterPCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
331 for (unsigned i = 0, e = AfterPCHLines.size(); i != e; ++i) {
332 if (AfterPCHLines[i].startswith("#include ")) {
333 IncludesAfterPCH += AfterPCHLines[i];
334 IncludesAfterPCH += '\n';
335 } else {
336 CmdLineLines.push_back(AfterPCHLines[i]);
337 }
338 }
339
340 // Make sure we add the includes last into SuggestedPredefines before we
341 // exit this function.
342 struct AddIncludesRAII {
343 std::string &SuggestedPredefines;
344 std::string &IncludesAfterPCH;
345
346 AddIncludesRAII(std::string &SuggestedPredefines,
347 std::string &IncludesAfterPCH)
348 : SuggestedPredefines(SuggestedPredefines),
349 IncludesAfterPCH(IncludesAfterPCH) { }
350 ~AddIncludesRAII() {
351 SuggestedPredefines += IncludesAfterPCH;
352 }
353 } AddIncludes(SuggestedPredefines, IncludesAfterPCH);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000354
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000355 // Sort both sets of predefined buffer lines, since we allow some extra
356 // definitions and they may appear at any point in the output.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000357 std::sort(CmdLineLines.begin(), CmdLineLines.end());
358 std::sort(PCHLines.begin(), PCHLines.end());
359
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000360 // Determine which predefines that were used to build the PCH file are missing
361 // from the command line.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000362 std::vector<StringRef> MissingPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000363 std::set_difference(PCHLines.begin(), PCHLines.end(),
364 CmdLineLines.begin(), CmdLineLines.end(),
365 std::back_inserter(MissingPredefines));
366
367 bool MissingDefines = false;
368 bool ConflictingDefines = false;
369 for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000370 StringRef Missing = MissingPredefines[I];
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000371 if (Missing.startswith("#include ")) {
372 // An -include was specified when generating the PCH; it is included in
373 // the PCH, just ignore it.
374 continue;
375 }
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000376 if (!Missing.startswith("#define ")) {
Douglas Gregor38295be2012-10-22 23:51:00 +0000377 if (Complain)
378 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000379 return true;
380 }
Mike Stump1eb44332009-09-09 15:08:12 +0000381
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000382 // This is a macro definition. Determine the name of the macro we're
383 // defining.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000384 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000385 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000386 = Missing.find_first_of("( \n\r", StartOfMacroName);
387 assert(EndOfMacroName != std::string::npos &&
388 "Couldn't find the end of the macro name");
Chris Lattner5f9e2722011-07-23 10:55:15 +0000389 StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000390
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000391 // Determine whether this macro was given a different definition on the
392 // command line.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000393 std::string MacroDefStart = "#define " + MacroName.str();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000394 std::string::size_type MacroDefLen = MacroDefStart.size();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000395 SmallVector<StringRef, 8>::iterator ConflictPos
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000396 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
397 MacroDefStart);
398 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000399 if (!ConflictPos->startswith(MacroDefStart)) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000400 // Different macro; we're done.
401 ConflictPos = CmdLineLines.end();
Mike Stump1eb44332009-09-09 15:08:12 +0000402 break;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000403 }
Mike Stump1eb44332009-09-09 15:08:12 +0000404
405 assert(ConflictPos->size() > MacroDefLen &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000406 "Invalid #define in predefines buffer?");
Mike Stump1eb44332009-09-09 15:08:12 +0000407 if ((*ConflictPos)[MacroDefLen] != ' ' &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000408 (*ConflictPos)[MacroDefLen] != '(')
409 continue; // Longer macro name; keep trying.
Mike Stump1eb44332009-09-09 15:08:12 +0000410
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000411 // We found a conflicting macro definition.
412 break;
413 }
Mike Stump1eb44332009-09-09 15:08:12 +0000414
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000415 if (ConflictPos != CmdLineLines.end()) {
Douglas Gregor38295be2012-10-22 23:51:00 +0000416 if (!Complain)
417 return true;
418
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000419 Reader.Diag(diag::warn_cmdline_conflicting_macro_def)
420 << MacroName;
421
422 // Show the definition of this macro within the PCH file.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000423 std::pair<FileID, StringRef::size_type> MacroLoc =
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000424 FindMacro(Buffers, Missing);
Chris Lattner5f9e2722011-07-23 10:55:15 +0000425 assert(MacroLoc.second!=StringRef::npos && "Unable to find macro!");
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000426 SourceLocation PCHMissingLoc =
427 SourceMgr.getLocForStartOfFile(MacroLoc.first)
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +0000428 .getLocWithOffset(MacroLoc.second);
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000429 Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000430
431 ConflictingDefines = true;
432 continue;
433 }
Mike Stump1eb44332009-09-09 15:08:12 +0000434
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000435 // If the macro doesn't conflict, then we'll just pick up the macro
436 // definition from the PCH file. Warn the user that they made a mistake.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000437 if (ConflictingDefines)
438 continue; // Don't complain if there are already conflicting defs
Mike Stump1eb44332009-09-09 15:08:12 +0000439
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000440 if (!MissingDefines) {
Douglas Gregor38295be2012-10-22 23:51:00 +0000441 if (!Complain)
442 return true;
443
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000444 Reader.Diag(diag::warn_cmdline_missing_macro_defs);
445 MissingDefines = true;
446 }
447
Douglas Gregor38295be2012-10-22 23:51:00 +0000448 if (!Complain)
449 return true;
450
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000451 // Show the definition of this macro within the PCH file.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000452 std::pair<FileID, StringRef::size_type> MacroLoc =
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000453 FindMacro(Buffers, Missing);
Chris Lattner5f9e2722011-07-23 10:55:15 +0000454 assert(MacroLoc.second!=StringRef::npos && "Unable to find macro!");
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000455 SourceLocation PCHMissingLoc =
456 SourceMgr.getLocForStartOfFile(MacroLoc.first)
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +0000457 .getLocWithOffset(MacroLoc.second);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000458 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
459 }
Mike Stump1eb44332009-09-09 15:08:12 +0000460
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000461 if (ConflictingDefines)
462 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000463
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000464 // Determine what predefines were introduced based on command-line
465 // parameters that were not present when building the PCH
466 // file. Extra #defines are okay, so long as the identifiers being
467 // defined were not used within the precompiled header.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000468 std::vector<StringRef> ExtraPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000469 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
470 PCHLines.begin(), PCHLines.end(),
Mike Stump1eb44332009-09-09 15:08:12 +0000471 std::back_inserter(ExtraPredefines));
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000472 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000473 StringRef &Extra = ExtraPredefines[I];
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000474 if (!Extra.startswith("#define ")) {
Douglas Gregor38295be2012-10-22 23:51:00 +0000475 if (Complain)
476 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000477 return true;
478 }
479
480 // This is an extra macro definition. Determine the name of the
481 // macro we're defining.
482 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000483 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000484 = Extra.find_first_of("( \n\r", StartOfMacroName);
485 assert(EndOfMacroName != std::string::npos &&
486 "Couldn't find the end of the macro name");
Chris Lattner5f9e2722011-07-23 10:55:15 +0000487 StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000488
489 // Check whether this name was used somewhere in the PCH file. If
490 // so, defining it as a macro could change behavior, so we reject
491 // the PCH file.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000492 if (IdentifierInfo *II = Reader.get(MacroName)) {
Douglas Gregor38295be2012-10-22 23:51:00 +0000493 if (Complain)
494 Reader.Diag(diag::warn_macro_name_used_in_pch) << II;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000495 return true;
496 }
497
498 // Add this definition to the suggested predefines buffer.
499 SuggestedPredefines += Extra;
500 SuggestedPredefines += '\n';
501 }
502
503 // If we get here, it's because the predefines buffer had compatible
504 // contents. Accept the PCH file.
505 return false;
506}
507
Douglas Gregor12fab312010-03-16 16:35:32 +0000508void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
509 unsigned ID) {
510 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
511 ++NumHeaderInfos;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000512}
513
Argyrios Kyrtzidis62288ed2012-10-10 02:12:47 +0000514void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000515 PP.setCounterValue(Value);
516}
517
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000518//===----------------------------------------------------------------------===//
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000519// AST reader implementation
Douglas Gregor668c1a42009-04-21 22:25:48 +0000520//===----------------------------------------------------------------------===//
521
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000522void
Sebastian Redl571db7f2010-08-18 23:56:56 +0000523ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000524 DeserializationListener = Listener;
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000525}
526
Chris Lattner4c6f9522009-04-27 05:14:47 +0000527
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000528
Douglas Gregor98339b92011-08-25 20:47:51 +0000529unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
530 return serialization::ComputeHash(Sel);
531}
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000532
Mike Stump1eb44332009-09-09 15:08:12 +0000533
Douglas Gregor98339b92011-08-25 20:47:51 +0000534std::pair<unsigned, unsigned>
535ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
536 using namespace clang::io;
537 unsigned KeyLen = ReadUnalignedLE16(d);
538 unsigned DataLen = ReadUnalignedLE16(d);
539 return std::make_pair(KeyLen, DataLen);
540}
541
542ASTSelectorLookupTrait::internal_key_type
543ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
544 using namespace clang::io;
Douglas Gregor35942772011-09-09 21:34:22 +0000545 SelectorTable &SelTable = Reader.getContext().Selectors;
Douglas Gregor98339b92011-08-25 20:47:51 +0000546 unsigned N = ReadUnalignedLE16(d);
547 IdentifierInfo *FirstII
548 = Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
549 if (N == 0)
550 return SelTable.getNullarySelector(FirstII);
551 else if (N == 1)
552 return SelTable.getUnarySelector(FirstII);
553
554 SmallVector<IdentifierInfo *, 16> Args;
555 Args.push_back(FirstII);
556 for (unsigned I = 1; I != N; ++I)
557 Args.push_back(Reader.getLocalIdentifier(F, ReadUnalignedLE32(d)));
558
559 return SelTable.getSelector(N, Args.data());
560}
561
562ASTSelectorLookupTrait::data_type
563ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
564 unsigned DataLen) {
565 using namespace clang::io;
566
567 data_type Result;
568
569 Result.ID = Reader.getGlobalSelectorID(F, ReadUnalignedLE32(d));
570 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
571 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
572
573 // Load instance methods
Douglas Gregor98339b92011-08-25 20:47:51 +0000574 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
575 if (ObjCMethodDecl *Method
576 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d)))
577 Result.Instance.push_back(Method);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000578 }
Mike Stump1eb44332009-09-09 15:08:12 +0000579
Douglas Gregor98339b92011-08-25 20:47:51 +0000580 // Load factory methods
Douglas Gregor98339b92011-08-25 20:47:51 +0000581 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
582 if (ObjCMethodDecl *Method
583 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d)))
584 Result.Factory.push_back(Method);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000585 }
Mike Stump1eb44332009-09-09 15:08:12 +0000586
Douglas Gregor98339b92011-08-25 20:47:51 +0000587 return Result;
588}
Mike Stump1eb44332009-09-09 15:08:12 +0000589
Douglas Gregor98339b92011-08-25 20:47:51 +0000590unsigned ASTIdentifierLookupTrait::ComputeHash(const internal_key_type& a) {
591 return llvm::HashString(StringRef(a.first, a.second));
592}
Mike Stump1eb44332009-09-09 15:08:12 +0000593
Douglas Gregor98339b92011-08-25 20:47:51 +0000594std::pair<unsigned, unsigned>
595ASTIdentifierLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
596 using namespace clang::io;
597 unsigned DataLen = ReadUnalignedLE16(d);
598 unsigned KeyLen = ReadUnalignedLE16(d);
599 return std::make_pair(KeyLen, DataLen);
600}
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000601
Douglas Gregor98339b92011-08-25 20:47:51 +0000602std::pair<const char*, unsigned>
603ASTIdentifierLookupTrait::ReadKey(const unsigned char* d, unsigned n) {
604 assert(n >= 2 && d[n-1] == '\0');
605 return std::make_pair((const char*) d, n-1);
606}
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000607
Douglas Gregor98339b92011-08-25 20:47:51 +0000608IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
609 const unsigned char* d,
610 unsigned DataLen) {
611 using namespace clang::io;
612 unsigned RawID = ReadUnalignedLE32(d);
613 bool IsInteresting = RawID & 0x01;
Mike Stump1eb44332009-09-09 15:08:12 +0000614
Douglas Gregor98339b92011-08-25 20:47:51 +0000615 // Wipe out the "is interesting" bit.
616 RawID = RawID >> 1;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000617
Douglas Gregor98339b92011-08-25 20:47:51 +0000618 IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
619 if (!IsInteresting) {
620 // For uninteresting identifiers, just build the IdentifierInfo
621 // and associate it with the persistent ID.
Douglas Gregor668c1a42009-04-21 22:25:48 +0000622 IdentifierInfo *II = KnownII;
Douglas Gregor5d5051f2012-01-24 15:24:38 +0000623 if (!II) {
Douglas Gregor6ec60e02011-08-03 21:49:18 +0000624 II = &Reader.getIdentifierTable().getOwn(StringRef(k.first, k.second));
Douglas Gregor5d5051f2012-01-24 15:24:38 +0000625 KnownII = II;
626 }
Douglas Gregor668c1a42009-04-21 22:25:48 +0000627 Reader.SetIdentifierInfo(ID, II);
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000628 II->setIsFromAST();
Douglas Gregor057df202012-01-18 20:56:22 +0000629 Reader.markIdentifierUpToDate(II);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000630 return II;
631 }
Mike Stump1eb44332009-09-09 15:08:12 +0000632
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +0000633 unsigned ObjCOrBuiltinID = ReadUnalignedLE16(d);
Douglas Gregor98339b92011-08-25 20:47:51 +0000634 unsigned Bits = ReadUnalignedLE16(d);
635 bool CPlusPlusOperatorKeyword = Bits & 0x01;
636 Bits >>= 1;
637 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
638 Bits >>= 1;
639 bool Poisoned = Bits & 0x01;
640 Bits >>= 1;
641 bool ExtensionToken = Bits & 0x01;
642 Bits >>= 1;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +0000643 bool hadMacroDefinition = Bits & 0x01;
644 Bits >>= 1;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000645
Douglas Gregor98339b92011-08-25 20:47:51 +0000646 assert(Bits == 0 && "Extra bits in the identifier?");
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +0000647 DataLen -= 8;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000648
Douglas Gregor98339b92011-08-25 20:47:51 +0000649 // Build the IdentifierInfo itself and link the identifier ID with
650 // the new IdentifierInfo.
651 IdentifierInfo *II = KnownII;
Douglas Gregor5d5051f2012-01-24 15:24:38 +0000652 if (!II) {
Douglas Gregor98339b92011-08-25 20:47:51 +0000653 II = &Reader.getIdentifierTable().getOwn(StringRef(k.first, k.second));
Douglas Gregor5d5051f2012-01-24 15:24:38 +0000654 KnownII = II;
655 }
Douglas Gregor057df202012-01-18 20:56:22 +0000656 Reader.markIdentifierUpToDate(II);
Douglas Gregoreee242f2011-10-27 09:33:13 +0000657 II->setIsFromAST();
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000658
Douglas Gregor98339b92011-08-25 20:47:51 +0000659 // Set or check the various bits in the IdentifierInfo structure.
660 // Token IDs are read-only.
661 if (HasRevertedTokenIDToIdentifier)
662 II->RevertTokenIDToIdentifier();
663 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
664 assert(II->isExtensionToken() == ExtensionToken &&
665 "Incorrect extension token flag");
666 (void)ExtensionToken;
667 if (Poisoned)
668 II->setIsPoisoned(true);
669 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
670 "Incorrect C++ operator keyword flag");
671 (void)CPlusPlusOperatorKeyword;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000672
Douglas Gregor98339b92011-08-25 20:47:51 +0000673 // If this identifier is a macro, deserialize the macro
674 // definition.
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +0000675 if (hadMacroDefinition) {
Douglas Gregor6c6c54a2012-10-11 00:46:49 +0000676 SmallVector<MacroID, 4> MacroIDs;
677 while (uint32_t LocalID = ReadUnalignedLE32(d)) {
678 MacroIDs.push_back(Reader.getGlobalMacroID(F, LocalID));
679 DataLen -= 4;
Douglas Gregor13292642011-12-02 15:45:10 +0000680 }
Douglas Gregor6c6c54a2012-10-11 00:46:49 +0000681 DataLen -= 4;
682 Reader.setIdentifierIsMacro(II, MacroIDs);
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000683 }
684
Douglas Gregoreee242f2011-10-27 09:33:13 +0000685 Reader.SetIdentifierInfo(ID, II);
686
Douglas Gregor98339b92011-08-25 20:47:51 +0000687 // Read all of the declarations visible at global scope with this
688 // name.
Douglas Gregor98339b92011-08-25 20:47:51 +0000689 if (DataLen > 0) {
690 SmallVector<uint32_t, 4> DeclIDs;
691 for (; DataLen > 0; DataLen -= 4)
692 DeclIDs.push_back(Reader.getGlobalDeclID(F, ReadUnalignedLE32(d)));
693 Reader.SetGloballyVisibleDecls(II, DeclIDs);
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000694 }
695
Douglas Gregor98339b92011-08-25 20:47:51 +0000696 return II;
697}
Michael J. Spencer20249a12010-10-21 03:16:25 +0000698
Douglas Gregor98339b92011-08-25 20:47:51 +0000699unsigned
700ASTDeclContextNameLookupTrait::ComputeHash(const DeclNameKey &Key) const {
701 llvm::FoldingSetNodeID ID;
702 ID.AddInteger(Key.Kind);
703
704 switch (Key.Kind) {
705 case DeclarationName::Identifier:
706 case DeclarationName::CXXLiteralOperatorName:
707 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
708 break;
709 case DeclarationName::ObjCZeroArgSelector:
710 case DeclarationName::ObjCOneArgSelector:
711 case DeclarationName::ObjCMultiArgSelector:
712 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
713 break;
714 case DeclarationName::CXXOperatorName:
715 ID.AddInteger((OverloadedOperatorKind)Key.Data);
716 break;
717 case DeclarationName::CXXConstructorName:
718 case DeclarationName::CXXDestructorName:
719 case DeclarationName::CXXConversionFunctionName:
720 case DeclarationName::CXXUsingDirective:
721 break;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000722 }
723
Douglas Gregor98339b92011-08-25 20:47:51 +0000724 return ID.ComputeHash();
725}
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +0000726
Douglas Gregor98339b92011-08-25 20:47:51 +0000727ASTDeclContextNameLookupTrait::internal_key_type
728ASTDeclContextNameLookupTrait::GetInternalKey(
729 const external_key_type& Name) const {
730 DeclNameKey Key;
731 Key.Kind = Name.getNameKind();
732 switch (Name.getNameKind()) {
733 case DeclarationName::Identifier:
734 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
735 break;
736 case DeclarationName::ObjCZeroArgSelector:
737 case DeclarationName::ObjCOneArgSelector:
738 case DeclarationName::ObjCMultiArgSelector:
739 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
740 break;
741 case DeclarationName::CXXOperatorName:
742 Key.Data = Name.getCXXOverloadedOperator();
743 break;
744 case DeclarationName::CXXLiteralOperatorName:
745 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
746 break;
747 case DeclarationName::CXXConstructorName:
748 case DeclarationName::CXXDestructorName:
749 case DeclarationName::CXXConversionFunctionName:
750 case DeclarationName::CXXUsingDirective:
751 Key.Data = 0;
752 break;
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +0000753 }
754
Douglas Gregor98339b92011-08-25 20:47:51 +0000755 return Key;
756}
757
Douglas Gregor98339b92011-08-25 20:47:51 +0000758std::pair<unsigned, unsigned>
759ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
760 using namespace clang::io;
761 unsigned KeyLen = ReadUnalignedLE16(d);
762 unsigned DataLen = ReadUnalignedLE16(d);
763 return std::make_pair(KeyLen, DataLen);
764}
Michael J. Spencer20249a12010-10-21 03:16:25 +0000765
Douglas Gregor98339b92011-08-25 20:47:51 +0000766ASTDeclContextNameLookupTrait::internal_key_type
767ASTDeclContextNameLookupTrait::ReadKey(const unsigned char* d, unsigned) {
768 using namespace clang::io;
769
770 DeclNameKey Key;
771 Key.Kind = (DeclarationName::NameKind)*d++;
772 switch (Key.Kind) {
773 case DeclarationName::Identifier:
774 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
775 break;
776 case DeclarationName::ObjCZeroArgSelector:
777 case DeclarationName::ObjCOneArgSelector:
778 case DeclarationName::ObjCMultiArgSelector:
779 Key.Data =
780 (uint64_t)Reader.getLocalSelector(F, ReadUnalignedLE32(d))
781 .getAsOpaquePtr();
782 break;
783 case DeclarationName::CXXOperatorName:
784 Key.Data = *d++; // OverloadedOperatorKind
785 break;
786 case DeclarationName::CXXLiteralOperatorName:
787 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
788 break;
789 case DeclarationName::CXXConstructorName:
790 case DeclarationName::CXXDestructorName:
791 case DeclarationName::CXXConversionFunctionName:
792 case DeclarationName::CXXUsingDirective:
793 Key.Data = 0;
794 break;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000795 }
796
Douglas Gregor98339b92011-08-25 20:47:51 +0000797 return Key;
798}
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000799
Douglas Gregor98339b92011-08-25 20:47:51 +0000800ASTDeclContextNameLookupTrait::data_type
801ASTDeclContextNameLookupTrait::ReadData(internal_key_type,
802 const unsigned char* d,
Nick Lewyckyb346d2f2012-04-16 02:51:46 +0000803 unsigned DataLen) {
Douglas Gregor98339b92011-08-25 20:47:51 +0000804 using namespace clang::io;
805 unsigned NumDecls = ReadUnalignedLE16(d);
Douglas Gregor9b8b20f2012-01-06 16:09:53 +0000806 LE32DeclID *Start = (LE32DeclID *)d;
Douglas Gregor98339b92011-08-25 20:47:51 +0000807 return std::make_pair(Start, Start + NumDecls);
808}
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000809
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000810bool ASTReader::ReadDeclContextStorage(ModuleFile &M,
Douglas Gregor0d95f772011-08-24 19:03:07 +0000811 llvm::BitstreamCursor &Cursor,
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000812 const std::pair<uint64_t, uint64_t> &Offsets,
813 DeclContextInfo &Info) {
814 SavedStreamPosition SavedPosition(Cursor);
815 // First the lexical decls.
816 if (Offsets.first != 0) {
817 Cursor.JumpToBit(Offsets.first);
818
819 RecordData Record;
820 const char *Blob;
821 unsigned BlobLen;
822 unsigned Code = Cursor.ReadCode();
823 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
824 if (RecCode != DECL_CONTEXT_LEXICAL) {
825 Error("Expected lexical block");
826 return true;
827 }
828
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000829 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob);
830 Info.NumLexicalDecls = BlobLen / sizeof(KindDeclIDPair);
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000831 }
832
833 // Now the lookup table.
834 if (Offsets.second != 0) {
835 Cursor.JumpToBit(Offsets.second);
836
837 RecordData Record;
838 const char *Blob;
839 unsigned BlobLen;
840 unsigned Code = Cursor.ReadCode();
841 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
842 if (RecCode != DECL_CONTEXT_VISIBLE) {
843 Error("Expected visible lookup table block");
844 return true;
845 }
846 Info.NameLookupTableData
847 = ASTDeclContextNameLookupTable::Create(
848 (const unsigned char *)Blob + Record[0],
849 (const unsigned char *)Blob,
Douglas Gregor0d95f772011-08-24 19:03:07 +0000850 ASTDeclContextNameLookupTrait(*this, M));
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000851 }
852
853 return false;
854}
855
Chris Lattner5f9e2722011-07-23 10:55:15 +0000856void ASTReader::Error(StringRef Msg) {
Argyrios Kyrtzidis8d8f2c22011-04-25 22:23:56 +0000857 Error(diag::err_fe_pch_malformed, Msg);
858}
859
860void ASTReader::Error(unsigned DiagID,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000861 StringRef Arg1, StringRef Arg2) {
Argyrios Kyrtzidis8d8f2c22011-04-25 22:23:56 +0000862 if (Diags.isDiagnosticInFlight())
863 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
864 else
865 Diag(DiagID) << Arg1 << Arg2;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000866}
867
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000868/// \brief Tell the AST listener about the predefines buffers in the chain.
Douglas Gregor38295be2012-10-22 23:51:00 +0000869bool ASTReader::CheckPredefinesBuffers(bool Complain) {
Douglas Gregor11407b82012-10-18 21:18:25 +0000870 if (Listener) {
871 // We only care about the primary module.
872 ModuleFile &M = ModuleMgr.getPrimaryModule();
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000873 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
Douglas Gregor11407b82012-10-18 21:18:25 +0000874 M.ActualOriginalSourceFileName,
Nick Lewycky277a6e72011-02-23 21:16:44 +0000875 SuggestedPredefines,
Douglas Gregor38295be2012-10-22 23:51:00 +0000876 FileMgr,
877 Complain);
Douglas Gregor11407b82012-10-18 21:18:25 +0000878 }
Douglas Gregore721f952009-04-28 18:58:38 +0000879 return false;
Douglas Gregore1d918e2009-04-10 23:10:45 +0000880}
881
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000882//===----------------------------------------------------------------------===//
883// Source Manager Deserialization
884//===----------------------------------------------------------------------===//
885
Douglas Gregorbd945002009-04-13 16:31:14 +0000886/// \brief Read the line table in the source manager block.
Sebastian Redlc3632732010-10-05 15:59:54 +0000887/// \returns true if there was an error.
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000888bool ASTReader::ParseLineTable(ModuleFile &F,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000889 SmallVectorImpl<uint64_t> &Record) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000890 unsigned Idx = 0;
891 LineTableInfo &LineTable = SourceMgr.getLineTable();
892
893 // Parse the file names
Douglas Gregorff0a9872009-04-13 17:12:42 +0000894 std::map<int, int> FileIDs;
895 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000896 // Extract the file name
897 unsigned FilenameLen = Record[Idx++];
898 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
899 Idx += FilenameLen;
Douglas Gregorcaed0602012-10-18 21:31:35 +0000900 MaybeAddSystemRootToFilename(F, Filename);
Jay Foad65aa6882011-06-21 15:13:30 +0000901 FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
Douglas Gregorbd945002009-04-13 16:31:14 +0000902 }
903
904 // Parse the line entries
905 std::vector<LineEntry> Entries;
906 while (Idx < Record.size()) {
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000907 int FID = Record[Idx++];
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000908 assert(FID >= 0 && "Serialized line entries for non-local file.");
909 // Remap FileID from 1-based old view.
910 FID += F.SLocEntryBaseID - 1;
Douglas Gregorbd945002009-04-13 16:31:14 +0000911
912 // Extract the line entries
913 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000914 assert(NumEntries && "Numentries is 00000");
Douglas Gregorbd945002009-04-13 16:31:14 +0000915 Entries.clear();
916 Entries.reserve(NumEntries);
917 for (unsigned I = 0; I != NumEntries; ++I) {
918 unsigned FileOffset = Record[Idx++];
919 unsigned LineNo = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000920 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump1eb44332009-09-09 15:08:12 +0000921 SrcMgr::CharacteristicKind FileKind
Douglas Gregorbd945002009-04-13 16:31:14 +0000922 = (SrcMgr::CharacteristicKind)Record[Idx++];
923 unsigned IncludeOffset = Record[Idx++];
924 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
925 FileKind, IncludeOffset));
926 }
Douglas Gregor47d9de62012-06-08 16:40:28 +0000927 LineTable.AddEntry(FileID::get(FID), Entries);
Douglas Gregorbd945002009-04-13 16:31:14 +0000928 }
929
930 return false;
931}
932
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000933namespace {
934
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000935class ASTStatData {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000936public:
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000937 const ino_t ino;
938 const dev_t dev;
939 const mode_t mode;
940 const time_t mtime;
941 const off_t size;
Mike Stump1eb44332009-09-09 15:08:12 +0000942
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000943 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Chris Lattner74e976b2010-11-23 19:28:12 +0000944 : ino(i), dev(d), mode(mo), mtime(m), size(s) {}
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000945};
946
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000947class ASTStatLookupTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000948 public:
949 typedef const char *external_key_type;
950 typedef const char *internal_key_type;
951
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000952 typedef ASTStatData data_type;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000953
954 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000955 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000956 }
957
958 static internal_key_type GetInternalKey(const char *path) { return path; }
959
960 static bool EqualKey(internal_key_type a, internal_key_type b) {
961 return strcmp(a, b) == 0;
962 }
963
964 static std::pair<unsigned, unsigned>
965 ReadKeyDataLength(const unsigned char*& d) {
966 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
967 unsigned DataLen = (unsigned) *d++;
968 return std::make_pair(KeyLen + 1, DataLen);
969 }
970
971 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
972 return (const char *)d;
973 }
974
975 static data_type ReadData(const internal_key_type, const unsigned char *d,
976 unsigned /*DataLen*/) {
977 using namespace clang::io;
978
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000979 ino_t ino = (ino_t) ReadUnalignedLE32(d);
980 dev_t dev = (dev_t) ReadUnalignedLE32(d);
981 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump1eb44332009-09-09 15:08:12 +0000982 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000983 off_t size = (off_t) ReadUnalignedLE64(d);
984 return data_type(ino, dev, mode, mtime, size);
985 }
986};
987
988/// \brief stat() cache for precompiled headers.
989///
990/// This cache is very similar to the stat cache used by pretokenized
991/// headers.
Chris Lattner10e286a2010-11-23 19:19:34 +0000992class ASTStatCache : public FileSystemStatCache {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000993 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000994 CacheTy *Cache;
995
996 unsigned &NumStatHits, &NumStatMisses;
Mike Stump1eb44332009-09-09 15:08:12 +0000997public:
Chris Lattner74e976b2010-11-23 19:28:12 +0000998 ASTStatCache(const unsigned char *Buckets, const unsigned char *Base,
999 unsigned &NumStatHits, unsigned &NumStatMisses)
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001000 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
1001 Cache = CacheTy::Create(Buckets, Base);
1002 }
1003
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001004 ~ASTStatCache() { delete Cache; }
Mike Stump1eb44332009-09-09 15:08:12 +00001005
Chris Lattner898a0612010-11-23 21:17:56 +00001006 LookupResult getStat(const char *Path, struct stat &StatBuf,
1007 int *FileDescriptor) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001008 // Do the lookup for the file's data in the AST file.
Chris Lattner10e286a2010-11-23 19:19:34 +00001009 CacheTy::iterator I = Cache->find(Path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001010
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001011 // If we don't get a hit in the AST file just forward to 'stat'.
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001012 if (I == Cache->end()) {
1013 ++NumStatMisses;
Chris Lattner898a0612010-11-23 21:17:56 +00001014 return statChained(Path, StatBuf, FileDescriptor);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001015 }
Mike Stump1eb44332009-09-09 15:08:12 +00001016
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001017 ++NumStatHits;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001018 ASTStatData Data = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001019
Chris Lattner10e286a2010-11-23 19:19:34 +00001020 StatBuf.st_ino = Data.ino;
1021 StatBuf.st_dev = Data.dev;
1022 StatBuf.st_mtime = Data.mtime;
1023 StatBuf.st_mode = Data.mode;
1024 StatBuf.st_size = Data.size;
Chris Lattnerd6f61112010-11-23 20:05:15 +00001025 return CacheExists;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001026 }
1027};
1028} // end anonymous namespace
1029
1030
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001031/// \brief Read a source manager block
Douglas Gregor4825fd72012-10-22 22:50:17 +00001032bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001033 using namespace SrcMgr;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001034
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001035 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001036
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001037 // Set the source-location entry cursor to the current position in
1038 // the stream. This cursor will be used to read the contents of the
1039 // source manager block initially, and then lazily read
1040 // source-location entries as needed.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001041 SLocEntryCursor = F.Stream;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001042
1043 // The stream itself is going to skip over the source manager block.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001044 if (F.Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001045 Error("malformed block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001046 return true;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001047 }
1048
1049 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001050 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001051 Error("malformed source manager block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001052 return true;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001053 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001054
Douglas Gregor14f79002009-04-10 03:52:48 +00001055 RecordData Record;
1056 while (true) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001057 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregor14f79002009-04-10 03:52:48 +00001058 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001059 if (SLocEntryCursor.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001060 Error("error at end of Source Manager block in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001061 return true;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001062 }
Douglas Gregor4825fd72012-10-22 22:50:17 +00001063 return false;
Douglas Gregor14f79002009-04-10 03:52:48 +00001064 }
Mike Stump1eb44332009-09-09 15:08:12 +00001065
Douglas Gregor14f79002009-04-10 03:52:48 +00001066 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1067 // No known subblocks, always skip them.
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001068 SLocEntryCursor.ReadSubBlockID();
1069 if (SLocEntryCursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001070 Error("malformed block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001071 return true;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001072 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001073 continue;
1074 }
Mike Stump1eb44332009-09-09 15:08:12 +00001075
Douglas Gregor14f79002009-04-10 03:52:48 +00001076 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001077 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregor14f79002009-04-10 03:52:48 +00001078 continue;
1079 }
Mike Stump1eb44332009-09-09 15:08:12 +00001080
Douglas Gregor14f79002009-04-10 03:52:48 +00001081 // Read a record.
1082 const char *BlobStart;
1083 unsigned BlobLen;
1084 Record.clear();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001085 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001086 default: // Default behavior: ignore.
1087 break;
1088
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001089 case SM_SLOC_FILE_ENTRY:
1090 case SM_SLOC_BUFFER_ENTRY:
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001091 case SM_SLOC_EXPANSION_ENTRY:
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001092 // Once we hit one of the source location entries, we're done.
Douglas Gregor4825fd72012-10-22 22:50:17 +00001093 return false;
Douglas Gregor14f79002009-04-10 03:52:48 +00001094 }
1095 }
1096}
1097
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001098/// \brief If a header file is not found at the path that we expect it to be
1099/// and the PCH file was moved from its original location, try to resolve the
1100/// file by assuming that header+PCH were moved together and the header is in
1101/// the same place relative to the PCH.
1102static std::string
1103resolveFileRelativeToOriginalDir(const std::string &Filename,
1104 const std::string &OriginalDir,
1105 const std::string &CurrDir) {
1106 assert(OriginalDir != CurrDir &&
1107 "No point trying to resolve the file if the PCH dir didn't change");
1108 using namespace llvm::sys;
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001109 SmallString<128> filePath(Filename);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001110 fs::make_absolute(filePath);
1111 assert(path::is_absolute(OriginalDir));
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001112 SmallString<128> currPCHPath(CurrDir);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001113
1114 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1115 fileDirE = path::end(path::parent_path(filePath));
1116 path::const_iterator origDirI = path::begin(OriginalDir),
1117 origDirE = path::end(OriginalDir);
1118 // Skip the common path components from filePath and OriginalDir.
1119 while (fileDirI != fileDirE && origDirI != origDirE &&
1120 *fileDirI == *origDirI) {
1121 ++fileDirI;
1122 ++origDirI;
1123 }
1124 for (; origDirI != origDirE; ++origDirI)
1125 path::append(currPCHPath, "..");
1126 path::append(currPCHPath, fileDirI, fileDirE);
1127 path::append(currPCHPath, path::filename(Filename));
1128 return currPCHPath.str();
1129}
1130
Douglas Gregor8b53d142012-10-22 22:53:10 +00001131bool ASTReader::ReadSLocEntry(int ID) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001132 if (ID == 0)
Douglas Gregor4825fd72012-10-22 22:50:17 +00001133 return false;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001134
Douglas Gregor0cdd7982011-07-21 18:46:38 +00001135 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001136 Error("source location entry ID out-of-range for AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001137 return true;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001138 }
1139
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001140 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001141 F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]);
Sebastian Redlc3632732010-10-05 15:59:54 +00001142 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001143 unsigned BaseOffset = F->SLocEntryBaseOffset;
Sebastian Redl9137a522010-07-16 17:50:48 +00001144
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001145 ++NumSLocEntriesRead;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001146 unsigned Code = SLocEntryCursor.ReadCode();
1147 if (Code == llvm::bitc::END_BLOCK ||
1148 Code == llvm::bitc::ENTER_SUBBLOCK ||
1149 Code == llvm::bitc::DEFINE_ABBREV) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001150 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001151 return true;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001152 }
1153
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001154 RecordData Record;
1155 const char *BlobStart;
1156 unsigned BlobLen;
1157 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1158 default:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001159 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001160 return true;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001161
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001162 case SM_SLOC_FILE_ENTRY: {
Argyrios Kyrtzidisa4c29b62012-02-20 23:58:07 +00001163 // We will detect whether a file changed and return 'Failure' for it, but
1164 // we will also try to fail gracefully by setting up the SLocEntry.
Douglas Gregora930dc92012-10-22 18:42:04 +00001165 unsigned InputID = Record[4];
1166 InputFile IF = getInputFile(*F, InputID);
1167 const FileEntry *File = IF.getPointer();
1168 bool OverriddenBuffer = IF.getInt();
Argyrios Kyrtzidisa4c29b62012-02-20 23:58:07 +00001169
Douglas Gregora930dc92012-10-22 18:42:04 +00001170 if (!IF.getPointer())
Douglas Gregor4825fd72012-10-22 22:50:17 +00001171 return true;
Douglas Gregor2d52be52010-03-21 22:49:54 +00001172
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001173 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
Douglas Gregor72a9ae12011-07-22 16:00:58 +00001174 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001175 // This is the module's main file.
1176 IncludeLoc = getImportLocation(F);
1177 }
Argyrios Kyrtzidisff398962012-07-11 20:59:04 +00001178 SrcMgr::CharacteristicKind
1179 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1180 FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001181 ID, BaseOffset + Record[0]);
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001182 SrcMgr::FileInfo &FileInfo =
1183 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
Douglas Gregora930dc92012-10-22 18:42:04 +00001184 FileInfo.NumCreatedFIDs = Record[5];
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001185 if (Record[3])
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001186 FileInfo.setHasLineDirectives();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001187
Douglas Gregora930dc92012-10-22 18:42:04 +00001188 const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
1189 unsigned NumFileDecls = Record[7];
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001190 if (NumFileDecls) {
1191 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
Argyrios Kyrtzidis9d128d02011-10-31 07:20:08 +00001192 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1193 NumFileDecls));
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001194 }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001195
Douglas Gregor35f9ae62011-11-17 01:44:33 +00001196 const SrcMgr::ContentCache *ContentCache
Argyrios Kyrtzidisff398962012-07-11 20:59:04 +00001197 = SourceMgr.getOrCreateContentCache(File,
1198 /*isSystemFile=*/FileCharacter != SrcMgr::C_User);
Douglas Gregor35f9ae62011-11-17 01:44:33 +00001199 if (OverriddenBuffer && !ContentCache->BufferOverridden &&
1200 ContentCache->ContentsEntry == ContentCache->OrigEntry) {
Douglas Gregora081da52011-11-16 20:05:18 +00001201 unsigned Code = SLocEntryCursor.ReadCode();
1202 Record.clear();
1203 unsigned RecCode
1204 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
1205
1206 if (RecCode != SM_SLOC_BUFFER_BLOB) {
1207 Error("AST record has invalid code");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001208 return true;
Douglas Gregora081da52011-11-16 20:05:18 +00001209 }
1210
1211 llvm::MemoryBuffer *Buffer
1212 = llvm::MemoryBuffer::getMemBuffer(StringRef(BlobStart, BlobLen - 1),
Douglas Gregora930dc92012-10-22 18:42:04 +00001213 File->getName());
Douglas Gregora081da52011-11-16 20:05:18 +00001214 SourceMgr.overrideFileContents(File, Buffer);
1215 }
Argyrios Kyrtzidisa4c29b62012-02-20 23:58:07 +00001216
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001217 break;
1218 }
1219
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001220 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001221 const char *Name = BlobStart;
1222 unsigned Offset = Record[0];
1223 unsigned Code = SLocEntryCursor.ReadCode();
1224 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001225 unsigned RecCode
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001226 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001227
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001228 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001229 Error("AST record has invalid code");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001230 return true;
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001231 }
1232
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001233 llvm::MemoryBuffer *Buffer
Douglas Gregora081da52011-11-16 20:05:18 +00001234 = llvm::MemoryBuffer::getMemBuffer(StringRef(BlobStart, BlobLen - 1),
1235 Name);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001236 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID,
1237 BaseOffset + Offset);
Mike Stump1eb44332009-09-09 15:08:12 +00001238
Douglas Gregor6236a292011-12-02 21:56:05 +00001239 if (strcmp(Name, "<built-in>") == 0 && F->Kind == MK_PCH) {
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +00001240 PCHPredefinesBlock Block = {
1241 BufferID,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001242 StringRef(BlobStart, BlobLen - 1)
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +00001243 };
1244 PCHPredefinesBuffers.push_back(Block);
Douglas Gregor92b059e2009-04-28 20:33:11 +00001245 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001246
1247 break;
1248 }
1249
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001250 case SM_SLOC_EXPANSION_ENTRY: {
Sebastian Redlc3632732010-10-05 15:59:54 +00001251 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
Chandler Carruthbf340e42011-07-26 03:03:05 +00001252 SourceMgr.createExpansionLoc(SpellingLoc,
Sebastian Redlc3632732010-10-05 15:59:54 +00001253 ReadSourceLocation(*F, Record[2]),
1254 ReadSourceLocation(*F, Record[3]),
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001255 Record[4],
1256 ID,
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001257 BaseOffset + Record[0]);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001258 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001259 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001260 }
1261
Douglas Gregor4825fd72012-10-22 22:50:17 +00001262 return false;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001263}
1264
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001265/// \brief Find the location where the module F is imported.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001266SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001267 if (F->ImportLoc.isValid())
1268 return F->ImportLoc;
Jonathan D. Turner2e091632011-07-29 18:09:09 +00001269
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001270 // Otherwise we have a PCH. It's considered to be "imported" at the first
1271 // location of its includer.
Jonathan D. Turner2e091632011-07-29 18:09:09 +00001272 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001273 // Main file is the importer. We assume that it is the first entry in the
1274 // entry table. We can't ask the manager, because at the time of PCH loading
1275 // the main file entry doesn't exist yet.
1276 // The very first entry is the invalid instantiation loc, which takes up
1277 // offsets 0 and 1.
1278 return SourceLocation::getFromRawEncoding(2U);
1279 }
Jonathan D. Turner2e091632011-07-29 18:09:09 +00001280 //return F->Loaders[0]->FirstLoc;
1281 return F->ImportedBy[0]->FirstLoc;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001282}
1283
Chris Lattner6367f6d2009-04-27 01:05:14 +00001284/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1285/// specified cursor. Read the abbreviations that are at the top of the block
1286/// and then leave the cursor pointing into the block.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001287bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattner6367f6d2009-04-27 01:05:14 +00001288 unsigned BlockID) {
1289 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001290 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001291 return Failure;
1292 }
Mike Stump1eb44332009-09-09 15:08:12 +00001293
Chris Lattner6367f6d2009-04-27 01:05:14 +00001294 while (true) {
Douglas Gregorecdcb882010-10-20 22:00:55 +00001295 uint64_t Offset = Cursor.GetCurrentBitNo();
Chris Lattner6367f6d2009-04-27 01:05:14 +00001296 unsigned Code = Cursor.ReadCode();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001297
Chris Lattner6367f6d2009-04-27 01:05:14 +00001298 // We expect all abbrevs to be at the start of the block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001299 if (Code != llvm::bitc::DEFINE_ABBREV) {
1300 Cursor.JumpToBit(Offset);
Chris Lattner6367f6d2009-04-27 01:05:14 +00001301 return false;
Douglas Gregorecdcb882010-10-20 22:00:55 +00001302 }
Chris Lattner6367f6d2009-04-27 01:05:14 +00001303 Cursor.ReadAbbrevRecord();
1304 }
1305}
1306
Douglas Gregor3ab50fe2012-10-11 17:41:54 +00001307void ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset,
1308 MacroInfo *Hint) {
Douglas Gregorecdcb882010-10-20 22:00:55 +00001309 llvm::BitstreamCursor &Stream = F.MacroCursor;
Mike Stump1eb44332009-09-09 15:08:12 +00001310
Douglas Gregor37e26842009-04-21 23:56:24 +00001311 // Keep track of where we are in the stream, then jump back there
1312 // after reading this macro.
1313 SavedStreamPosition SavedPosition(Stream);
1314
1315 Stream.JumpToBit(Offset);
1316 RecordData Record;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001317 SmallVector<IdentifierInfo*, 16> MacroArgs;
Douglas Gregor37e26842009-04-21 23:56:24 +00001318 MacroInfo *Macro = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001319
Douglas Gregore8219a62012-10-11 21:07:39 +00001320 // RAII object to add the loaded macro information once we're done
1321 // adding tokens.
1322 struct AddLoadedMacroInfoRAII {
1323 Preprocessor &PP;
1324 MacroInfo *Hint;
1325 MacroInfo *MI;
1326 IdentifierInfo *II;
1327
1328 AddLoadedMacroInfoRAII(Preprocessor &PP, MacroInfo *Hint)
1329 : PP(PP), Hint(Hint), MI(), II() { }
1330 ~AddLoadedMacroInfoRAII( ) {
1331 if (MI) {
1332 // Finally, install the macro.
1333 PP.addLoadedMacroInfo(II, MI, Hint);
1334 }
1335 }
1336 } AddLoadedMacroInfo(PP, Hint);
1337
Douglas Gregor37e26842009-04-21 23:56:24 +00001338 while (true) {
1339 unsigned Code = Stream.ReadCode();
1340 switch (Code) {
1341 case llvm::bitc::END_BLOCK:
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001342 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001343
1344 case llvm::bitc::ENTER_SUBBLOCK:
1345 // No known subblocks, always skip them.
1346 Stream.ReadSubBlockID();
1347 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001348 Error("malformed block record in AST file");
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001349 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001350 }
1351 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001352
Douglas Gregor37e26842009-04-21 23:56:24 +00001353 case llvm::bitc::DEFINE_ABBREV:
1354 Stream.ReadAbbrevRecord();
1355 continue;
1356 default: break;
1357 }
1358
1359 // Read a record.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001360 const char *BlobStart = 0;
1361 unsigned BlobLen = 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001362 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001363 PreprocessorRecordTypes RecType =
Michael J. Spencer20249a12010-10-21 03:16:25 +00001364 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart,
Douglas Gregorecdcb882010-10-20 22:00:55 +00001365 BlobLen);
Douglas Gregor37e26842009-04-21 23:56:24 +00001366 switch (RecType) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001367 case PP_MACRO_OBJECT_LIKE:
1368 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001369 // If we already have a macro, that means that we've hit the end
1370 // of the definition of the macro we were looking for. We're
1371 // done.
1372 if (Macro)
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001373 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001374
Douglas Gregor95eab172011-07-28 20:55:49 +00001375 IdentifierInfo *II = getLocalIdentifier(F, Record[0]);
Douglas Gregor37e26842009-04-21 23:56:24 +00001376 if (II == 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001377 Error("macro must have a name in AST file");
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001378 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001379 }
Mike Stump1eb44332009-09-09 15:08:12 +00001380
Douglas Gregora8235d62012-10-09 23:05:51 +00001381 unsigned GlobalID = getGlobalMacroID(F, Record[1]);
1382
1383 // If this macro has already been loaded, don't do so again.
1384 if (MacrosLoaded[GlobalID - NUM_PREDEF_MACRO_IDS])
1385 return;
1386
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001387 SubmoduleID GlobalSubmoduleID = getGlobalSubmoduleID(F, Record[2]);
1388 unsigned NextIndex = 3;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001389 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001390 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001391
Douglas Gregora8235d62012-10-09 23:05:51 +00001392 // Record this macro.
1393 MacrosLoaded[GlobalID - NUM_PREDEF_MACRO_IDS] = MI;
1394
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001395 SourceLocation UndefLoc = ReadSourceLocation(F, Record, NextIndex);
1396 if (UndefLoc.isValid())
1397 MI->setUndefLoc(UndefLoc);
1398
1399 MI->setIsUsed(Record[NextIndex++]);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001400 MI->setIsFromAST();
Mike Stump1eb44332009-09-09 15:08:12 +00001401
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001402 bool IsPublic = Record[NextIndex++];
Douglas Gregoraa93a872011-10-17 15:32:29 +00001403 MI->setVisibility(IsPublic, ReadSourceLocation(F, Record, NextIndex));
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001404
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001405 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregor37e26842009-04-21 23:56:24 +00001406 // Decode function-like macro info.
Douglas Gregor7143aab2011-09-01 17:04:32 +00001407 bool isC99VarArgs = Record[NextIndex++];
1408 bool isGNUVarArgs = Record[NextIndex++];
Douglas Gregor37e26842009-04-21 23:56:24 +00001409 MacroArgs.clear();
Douglas Gregor7143aab2011-09-01 17:04:32 +00001410 unsigned NumArgs = Record[NextIndex++];
Douglas Gregor37e26842009-04-21 23:56:24 +00001411 for (unsigned i = 0; i != NumArgs; ++i)
Douglas Gregor7143aab2011-09-01 17:04:32 +00001412 MacroArgs.push_back(getLocalIdentifier(F, Record[NextIndex++]));
Douglas Gregor37e26842009-04-21 23:56:24 +00001413
1414 // Install function-like macro info.
1415 MI->setIsFunctionLike();
1416 if (isC99VarArgs) MI->setIsC99Varargs();
1417 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor75fdb232009-05-22 22:45:36 +00001418 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001419 PP.getPreprocessorAllocator());
Douglas Gregor37e26842009-04-21 23:56:24 +00001420 }
1421
Douglas Gregora8235d62012-10-09 23:05:51 +00001422 if (DeserializationListener)
1423 DeserializationListener->MacroRead(GlobalID, MI);
1424
1425 // If an update record marked this as undefined, do so now.
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001426 // FIXME: Only if the submodule this update came from is visible?
Douglas Gregora8235d62012-10-09 23:05:51 +00001427 MacroUpdatesMap::iterator Update = MacroUpdates.find(GlobalID);
1428 if (Update != MacroUpdates.end()) {
1429 if (MI->getUndefLoc().isInvalid()) {
Douglas Gregor54c8a402012-10-12 00:16:50 +00001430 for (unsigned I = 0, N = Update->second.size(); I != N; ++I) {
1431 bool Hidden = false;
1432 if (unsigned SubmoduleID = Update->second[I].first) {
1433 if (Module *Owner = getSubmodule(SubmoduleID)) {
1434 if (Owner->NameVisibility == Module::Hidden) {
1435 // Note that this #undef is hidden.
1436 Hidden = true;
1437
1438 // Record this hiding for later.
1439 HiddenNamesMap[Owner].push_back(
1440 HiddenName(II, MI, Update->second[I].second.UndefLoc));
1441 }
1442 }
1443 }
1444
1445 if (!Hidden) {
1446 MI->setUndefLoc(Update->second[I].second.UndefLoc);
1447 if (PPMutationListener *Listener = PP.getPPMutationListener())
1448 Listener->UndefinedMacro(MI);
1449 break;
1450 }
1451 }
Douglas Gregora8235d62012-10-09 23:05:51 +00001452 }
1453 MacroUpdates.erase(Update);
1454 }
1455
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001456 // Determine whether this macro definition is visible.
1457 bool Hidden = !MI->isPublic();
1458 if (!Hidden && GlobalSubmoduleID) {
1459 if (Module *Owner = getSubmodule(GlobalSubmoduleID)) {
1460 if (Owner->NameVisibility == Module::Hidden) {
1461 // The owning module is not visible, and this macro definition
1462 // should not be, either.
1463 Hidden = true;
1464
1465 // Note that this macro definition was hidden because its owning
1466 // module is not yet visible.
1467 HiddenNamesMap[Owner].push_back(HiddenName(II, MI));
1468 }
1469 }
1470 }
1471 MI->setHidden(Hidden);
1472
Douglas Gregore8219a62012-10-11 21:07:39 +00001473 // Make sure we install the macro once we're done.
1474 AddLoadedMacroInfo.MI = MI;
1475 AddLoadedMacroInfo.II = II;
Douglas Gregor37e26842009-04-21 23:56:24 +00001476
1477 // Remember that we saw this macro last so that we add the tokens that
1478 // form its body to it.
1479 Macro = MI;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001480
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001481 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1482 Record[NextIndex]) {
1483 // We have a macro definition. Register the association
1484 PreprocessedEntityID
1485 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1486 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1487 PPRec.RegisterMacroDefinition(Macro,
1488 PPRec.getPPEntityID(GlobalID-1, /*isLoaded=*/true));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001489 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001490
Douglas Gregor37e26842009-04-21 23:56:24 +00001491 ++NumMacrosRead;
1492 break;
1493 }
Mike Stump1eb44332009-09-09 15:08:12 +00001494
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001495 case PP_TOKEN: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001496 // If we see a TOKEN before a PP_MACRO_*, then the file is
1497 // erroneous, just pretend we didn't see this.
1498 if (Macro == 0) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001499
Douglas Gregor37e26842009-04-21 23:56:24 +00001500 Token Tok;
1501 Tok.startToken();
Sebastian Redlc3632732010-10-05 15:59:54 +00001502 Tok.setLocation(ReadSourceLocation(F, Record[0]));
Douglas Gregor37e26842009-04-21 23:56:24 +00001503 Tok.setLength(Record[1]);
Douglas Gregor95eab172011-07-28 20:55:49 +00001504 if (IdentifierInfo *II = getLocalIdentifier(F, Record[2]))
Douglas Gregor37e26842009-04-21 23:56:24 +00001505 Tok.setIdentifierInfo(II);
1506 Tok.setKind((tok::TokenKind)Record[3]);
1507 Tok.setFlag((Token::TokenFlags)Record[4]);
1508 Macro->AddTokenToBody(Tok);
1509 break;
1510 }
David Blaikie7530c032012-01-17 06:56:22 +00001511 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001512 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001513}
1514
Douglas Gregor86c67d82011-07-28 22:39:26 +00001515PreprocessedEntityID
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001516ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const {
Argyrios Kyrtzidis1f6d2252011-09-19 20:40:02 +00001517 ContinuousRangeMap<uint32_t, int, 2>::const_iterator
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001518 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1519 assert(I != M.PreprocessedEntityRemap.end()
1520 && "Invalid index into preprocessed entity index remap");
1521
1522 return LocalID + I->second;
Douglas Gregor86c67d82011-07-28 22:39:26 +00001523}
1524
Douglas Gregor98339b92011-08-25 20:47:51 +00001525unsigned HeaderFileInfoTrait::ComputeHash(const char *path) {
1526 return llvm::HashString(llvm::sys::path::filename(path));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001527}
Douglas Gregor98339b92011-08-25 20:47:51 +00001528
1529HeaderFileInfoTrait::internal_key_type
1530HeaderFileInfoTrait::GetInternalKey(const char *path) { return path; }
1531
1532bool HeaderFileInfoTrait::EqualKey(internal_key_type a, internal_key_type b) {
1533 if (strcmp(a, b) == 0)
1534 return true;
1535
1536 if (llvm::sys::path::filename(a) != llvm::sys::path::filename(b))
1537 return false;
Douglas Gregor99a922b2011-12-09 16:22:07 +00001538
1539 // Determine whether the actual files are equivalent.
1540 bool Result = false;
1541 if (llvm::sys::fs::equivalent(a, b, Result))
Douglas Gregor98339b92011-08-25 20:47:51 +00001542 return false;
1543
Douglas Gregor99a922b2011-12-09 16:22:07 +00001544 return Result;
Douglas Gregor98339b92011-08-25 20:47:51 +00001545}
1546
1547std::pair<unsigned, unsigned>
1548HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1549 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1550 unsigned DataLen = (unsigned) *d++;
1551 return std::make_pair(KeyLen + 1, DataLen);
1552}
1553
1554HeaderFileInfoTrait::data_type
1555HeaderFileInfoTrait::ReadData(const internal_key_type, const unsigned char *d,
1556 unsigned DataLen) {
1557 const unsigned char *End = d + DataLen;
1558 using namespace clang::io;
1559 HeaderFileInfo HFI;
1560 unsigned Flags = *d++;
1561 HFI.isImport = (Flags >> 5) & 0x01;
1562 HFI.isPragmaOnce = (Flags >> 4) & 0x01;
1563 HFI.DirInfo = (Flags >> 2) & 0x03;
1564 HFI.Resolved = (Flags >> 1) & 0x01;
1565 HFI.IndexHeaderMapHeader = Flags & 0x01;
1566 HFI.NumIncludes = ReadUnalignedLE16(d);
Douglas Gregor541ba162011-10-17 18:53:12 +00001567 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(M,
1568 ReadUnalignedLE32(d));
Douglas Gregor98339b92011-08-25 20:47:51 +00001569 if (unsigned FrameworkOffset = ReadUnalignedLE32(d)) {
1570 // The framework offset is 1 greater than the actual offset,
1571 // since 0 is used as an indicator for "no framework name".
1572 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1573 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1574 }
1575
1576 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1577 (void)End;
1578
1579 // This HeaderFileInfo was externally loaded.
1580 HFI.External = true;
1581 return HFI;
1582}
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001583
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001584void ASTReader::setIdentifierIsMacro(IdentifierInfo *II, ArrayRef<MacroID> IDs){
1585 II->setHadMacroDefinition(true);
1586 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1587 PendingMacroIDs[II].append(IDs.begin(), IDs.end());
Douglas Gregor295a2a62010-10-30 00:23:06 +00001588}
1589
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001590void ASTReader::ReadDefinedMacros() {
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001591 // Note that we are loading defined macros.
1592 Deserializing Macros(this);
1593
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00001594 for (ModuleReverseIterator I = ModuleMgr.rbegin(),
1595 E = ModuleMgr.rend(); I != E; ++I) {
1596 llvm::BitstreamCursor &MacroCursor = (*I)->MacroCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001597
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001598 // If there was no preprocessor block, skip this file.
1599 if (!MacroCursor.getBitStreamReader())
1600 continue;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001601
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001602 llvm::BitstreamCursor Cursor = MacroCursor;
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00001603 Cursor.JumpToBit((*I)->MacroStartOffset);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001604
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001605 RecordData Record;
1606 while (true) {
1607 unsigned Code = Cursor.ReadCode();
Douglas Gregorecdcb882010-10-20 22:00:55 +00001608 if (Code == llvm::bitc::END_BLOCK)
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001609 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001610
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001611 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1612 // No known subblocks, always skip them.
1613 Cursor.ReadSubBlockID();
1614 if (Cursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001615 Error("malformed block record in AST file");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001616 return;
1617 }
1618 continue;
1619 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001620
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001621 if (Code == llvm::bitc::DEFINE_ABBREV) {
1622 Cursor.ReadAbbrevRecord();
1623 continue;
1624 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001625
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001626 // Read a record.
1627 const char *BlobStart;
1628 unsigned BlobLen;
1629 Record.clear();
1630 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1631 default: // Default behavior: ignore.
1632 break;
Douglas Gregor88a35862010-01-04 19:18:44 +00001633
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001634 case PP_MACRO_OBJECT_LIKE:
1635 case PP_MACRO_FUNCTION_LIKE:
Douglas Gregor95eab172011-07-28 20:55:49 +00001636 getLocalIdentifier(**I, Record[0]);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001637 break;
1638
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001639 case PP_TOKEN:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001640 // Ignore tokens.
1641 break;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001642 }
Douglas Gregor88a35862010-01-04 19:18:44 +00001643 }
1644 }
Douglas Gregor295a2a62010-10-30 00:23:06 +00001645}
1646
Douglas Gregoreee242f2011-10-27 09:33:13 +00001647namespace {
1648 /// \brief Visitor class used to look up identifirs in an AST file.
1649 class IdentifierLookupVisitor {
1650 StringRef Name;
Douglas Gregor057df202012-01-18 20:56:22 +00001651 unsigned PriorGeneration;
Douglas Gregoreee242f2011-10-27 09:33:13 +00001652 IdentifierInfo *Found;
1653 public:
Douglas Gregor057df202012-01-18 20:56:22 +00001654 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration)
1655 : Name(Name), PriorGeneration(PriorGeneration), Found() { }
Douglas Gregoreee242f2011-10-27 09:33:13 +00001656
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001657 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00001658 IdentifierLookupVisitor *This
1659 = static_cast<IdentifierLookupVisitor *>(UserData);
1660
Douglas Gregor057df202012-01-18 20:56:22 +00001661 // If we've already searched this module file, skip it now.
1662 if (M.Generation <= This->PriorGeneration)
1663 return true;
1664
Douglas Gregoreee242f2011-10-27 09:33:13 +00001665 ASTIdentifierLookupTable *IdTable
1666 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
1667 if (!IdTable)
1668 return false;
1669
Douglas Gregor5d5051f2012-01-24 15:24:38 +00001670 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(),
1671 M, This->Found);
1672
Douglas Gregoreee242f2011-10-27 09:33:13 +00001673 std::pair<const char*, unsigned> Key(This->Name.begin(),
1674 This->Name.size());
Douglas Gregor5d5051f2012-01-24 15:24:38 +00001675 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Trait);
Douglas Gregoreee242f2011-10-27 09:33:13 +00001676 if (Pos == IdTable->end())
1677 return false;
1678
1679 // Dereferencing the iterator has the effect of building the
1680 // IdentifierInfo node and populating it with the various
1681 // declarations it needs.
1682 This->Found = *Pos;
1683 return true;
1684 }
1685
1686 // \brief Retrieve the identifier info found within the module
1687 // files.
1688 IdentifierInfo *getIdentifierInfo() const { return Found; }
1689 };
1690}
1691
1692void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001693 // Note that we are loading an identifier.
1694 Deserializing AnIdentifier(this);
1695
Douglas Gregor057df202012-01-18 20:56:22 +00001696 unsigned PriorGeneration = 0;
David Blaikie4e4d0842012-03-11 07:00:24 +00001697 if (getContext().getLangOpts().Modules)
Douglas Gregor057df202012-01-18 20:56:22 +00001698 PriorGeneration = IdentifierGeneration[&II];
1699
1700 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration);
1701 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
1702 markIdentifierUpToDate(&II);
1703}
1704
1705void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
1706 if (!II)
1707 return;
1708
1709 II->setOutOfDate(false);
1710
1711 // Update the generation for this identifier.
David Blaikie4e4d0842012-03-11 07:00:24 +00001712 if (getContext().getLangOpts().Modules)
Douglas Gregor057df202012-01-18 20:56:22 +00001713 IdentifierGeneration[II] = CurrentGeneration;
Douglas Gregoreee242f2011-10-27 09:33:13 +00001714}
1715
Douglas Gregora930dc92012-10-22 18:42:04 +00001716llvm::PointerIntPair<const FileEntry *, 1, bool>
Douglas Gregor38295be2012-10-22 23:51:00 +00001717ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
Douglas Gregora930dc92012-10-22 18:42:04 +00001718 // If this ID is bogus, just return an empty input file.
1719 if (ID == 0 || ID > F.InputFilesLoaded.size())
1720 return InputFile();
1721
1722 // If we've already loaded this input file, return it.
1723 if (F.InputFilesLoaded[ID-1].getPointer())
1724 return F.InputFilesLoaded[ID-1];
1725
1726 // Go find this input file.
1727 llvm::BitstreamCursor &Cursor = F.InputFilesCursor;
1728 SavedStreamPosition SavedPosition(Cursor);
1729 Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
1730
1731 unsigned Code = Cursor.ReadCode();
1732 RecordData Record;
1733 const char *BlobStart = 0;
1734 unsigned BlobLen = 0;
1735 switch ((InputFileRecordTypes)Cursor.ReadRecord(Code, Record,
1736 &BlobStart, &BlobLen)) {
1737 case INPUT_FILE: {
1738 unsigned StoredID = Record[0];
1739 assert(ID == StoredID && "Bogus stored ID or offset");
NAKAMURA Takumi6b8194e2012-10-22 21:50:39 +00001740 (void)StoredID;
Douglas Gregora930dc92012-10-22 18:42:04 +00001741 off_t StoredSize = (off_t)Record[1];
1742 time_t StoredTime = (time_t)Record[2];
1743 bool Overridden = (bool)Record[3];
1744
1745 // Get the file entry for this input file.
1746 StringRef OrigFilename(BlobStart, BlobLen);
1747 std::string Filename = OrigFilename;
1748 MaybeAddSystemRootToFilename(F, Filename);
1749 const FileEntry *File
1750 = Overridden? FileMgr.getVirtualFile(Filename, StoredSize, StoredTime)
1751 : FileMgr.getFile(Filename, /*OpenFile=*/false);
1752
1753 // If we didn't find the file, resolve it relative to the
1754 // original directory from which this AST file was created.
1755 if (File == 0 && !F.OriginalDir.empty() && !CurrentDir.empty() &&
1756 F.OriginalDir != CurrentDir) {
1757 std::string Resolved = resolveFileRelativeToOriginalDir(Filename,
1758 F.OriginalDir,
1759 CurrentDir);
1760 if (!Resolved.empty())
1761 File = FileMgr.getFile(Resolved);
1762 }
1763
1764 // For an overridden file, create a virtual file with the stored
1765 // size/timestamp.
1766 if (Overridden && File == 0) {
1767 File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
1768 }
1769
1770 if (File == 0) {
Douglas Gregor38295be2012-10-22 23:51:00 +00001771 if (Complain) {
1772 std::string ErrorStr = "could not find file '";
1773 ErrorStr += Filename;
1774 ErrorStr += "' referenced by AST file";
1775 Error(ErrorStr.c_str());
1776 }
Douglas Gregora930dc92012-10-22 18:42:04 +00001777 return InputFile();
1778 }
1779
1780 // Note that we've loaded this input file.
1781 F.InputFilesLoaded[ID-1] = InputFile(File, Overridden);
1782
1783 // Check if there was a request to override the contents of the file
1784 // that was part of the precompiled header. Overridding such a file
1785 // can lead to problems when lexing using the source locations from the
1786 // PCH.
1787 SourceManager &SM = getSourceManager();
1788 if (!Overridden && SM.isFileOverridden(File)) {
1789 Error(diag::err_fe_pch_file_overridden, Filename);
1790 // After emitting the diagnostic, recover by disabling the override so
1791 // that the original file will be used.
1792 SM.disableFileContentsOverride(File);
1793 // The FileEntry is a virtual file entry with the size of the contents
1794 // that would override the original contents. Set it to the original's
1795 // size/time.
1796 FileMgr.modifyFileEntry(const_cast<FileEntry*>(File),
1797 StoredSize, StoredTime);
1798 }
1799
1800 // For an overridden file, there is nothing to validate.
1801 if (Overridden)
1802 return InputFile(File, Overridden);
1803
1804 // The stat info from the FileEntry came from the cached stat
1805 // info of the PCH, so we cannot trust it.
1806 struct stat StatBuf;
1807 if (::stat(File->getName(), &StatBuf) != 0) {
1808 StatBuf.st_size = File->getSize();
1809 StatBuf.st_mtime = File->getModificationTime();
1810 }
1811
1812 if ((StoredSize != StatBuf.st_size
1813#if !defined(LLVM_ON_WIN32)
1814 // In our regression testing, the Windows file system seems to
1815 // have inconsistent modification times that sometimes
1816 // erroneously trigger this error-handling path.
1817 || StoredTime != StatBuf.st_mtime
1818#endif
1819 )) {
Douglas Gregor38295be2012-10-22 23:51:00 +00001820 if (Complain)
1821 Error(diag::err_fe_pch_file_modified, Filename);
1822
Douglas Gregora930dc92012-10-22 18:42:04 +00001823 return InputFile();
1824 }
1825
1826 return InputFile(File, Overridden);
1827 }
1828 }
1829
1830 return InputFile();
1831}
1832
Chris Lattner5f9e2722011-07-23 10:55:15 +00001833const FileEntry *ASTReader::getFileEntry(StringRef filenameStrRef) {
Douglas Gregor69e16082012-10-18 21:47:16 +00001834 ModuleFile &M = ModuleMgr.getPrimaryModule();
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00001835 std::string Filename = filenameStrRef;
Douglas Gregor69e16082012-10-18 21:47:16 +00001836 MaybeAddSystemRootToFilename(M, Filename);
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00001837 const FileEntry *File = FileMgr.getFile(Filename);
Douglas Gregor69e16082012-10-18 21:47:16 +00001838 if (File == 0 && !M.OriginalDir.empty() && !CurrentDir.empty() &&
1839 M.OriginalDir != CurrentDir) {
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00001840 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
Douglas Gregor69e16082012-10-18 21:47:16 +00001841 M.OriginalDir,
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00001842 CurrentDir);
1843 if (!resolved.empty())
1844 File = FileMgr.getFile(resolved);
1845 }
1846
1847 return File;
1848}
1849
Douglas Gregore650c8c2009-07-07 00:12:59 +00001850/// \brief If we are loading a relocatable PCH file, and the filename is
1851/// not an absolute path, add the system root to the beginning of the file
1852/// name.
Douglas Gregora930dc92012-10-22 18:42:04 +00001853StringRef ASTReader::MaybeAddSystemRootToFilename(ModuleFile &M,
1854 std::string &Filename) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001855 // If this is not a relocatable PCH file, there's nothing to do.
Douglas Gregorcaed0602012-10-18 21:31:35 +00001856 if (!M.RelocatablePCH)
Douglas Gregora930dc92012-10-22 18:42:04 +00001857 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +00001858
Michael J. Spencer256053b2010-12-17 21:22:22 +00001859 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
Douglas Gregora930dc92012-10-22 18:42:04 +00001860 return Filename;
Douglas Gregore650c8c2009-07-07 00:12:59 +00001861
Douglas Gregor832d6202011-07-22 16:35:34 +00001862 if (isysroot.empty()) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001863 // If no system root was given, default to '/'
1864 Filename.insert(Filename.begin(), '/');
Douglas Gregora930dc92012-10-22 18:42:04 +00001865 return Filename;
Douglas Gregore650c8c2009-07-07 00:12:59 +00001866 }
Mike Stump1eb44332009-09-09 15:08:12 +00001867
Douglas Gregor832d6202011-07-22 16:35:34 +00001868 unsigned Length = isysroot.size();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001869 if (isysroot[Length - 1] != '/')
1870 Filename.insert(Filename.begin(), '/');
Mike Stump1eb44332009-09-09 15:08:12 +00001871
Douglas Gregor832d6202011-07-22 16:35:34 +00001872 Filename.insert(Filename.begin(), isysroot.begin(), isysroot.end());
Douglas Gregora930dc92012-10-22 18:42:04 +00001873 return Filename;
Douglas Gregore650c8c2009-07-07 00:12:59 +00001874}
1875
Douglas Gregor38295be2012-10-22 23:51:00 +00001876ASTReader::ASTReadResult
1877ASTReader::ReadControlBlock(ModuleFile &F,
1878 llvm::SmallVectorImpl<ModuleFile *> &Loaded,
1879 unsigned ClientLoadCapabilities) {
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001880 llvm::BitstreamCursor &Stream = F.Stream;
1881
1882 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
1883 Error("malformed block record in AST file");
1884 return Failure;
1885 }
1886
1887 // Read all of the records and blocks in the control block.
1888 RecordData Record;
1889 while (!Stream.AtEndOfStream()) {
1890 unsigned Code = Stream.ReadCode();
1891 if (Code == llvm::bitc::END_BLOCK) {
1892 if (Stream.ReadBlockEnd()) {
1893 Error("error at end of control block in AST file");
1894 return Failure;
1895 }
1896
Douglas Gregora930dc92012-10-22 18:42:04 +00001897 // Validate all of the input files.
1898 if (!DisableValidation) {
Douglas Gregor38295be2012-10-22 23:51:00 +00001899 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
Douglas Gregora930dc92012-10-22 18:42:04 +00001900 for (unsigned I = 0, N = Record[0]; I < N; ++I)
Douglas Gregor38295be2012-10-22 23:51:00 +00001901 if (!getInputFile(F, I+1, Complain).getPointer())
Douglas Gregor4825fd72012-10-22 22:50:17 +00001902 return OutOfDate;
Douglas Gregora930dc92012-10-22 18:42:04 +00001903 }
1904
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001905 return Success;
1906 }
1907
1908 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
Douglas Gregor745e6f12012-10-19 00:38:02 +00001909 switch (Stream.ReadSubBlockID()) {
1910 case INPUT_FILES_BLOCK_ID:
Douglas Gregora930dc92012-10-22 18:42:04 +00001911 F.InputFilesCursor = Stream;
1912 if (Stream.SkipBlock() || // Skip with the main cursor
1913 // Read the abbreviations
1914 ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
1915 Error("malformed block record in AST file");
Douglas Gregor745e6f12012-10-19 00:38:02 +00001916 return Failure;
Douglas Gregor745e6f12012-10-19 00:38:02 +00001917 }
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001918 continue;
Douglas Gregor745e6f12012-10-19 00:38:02 +00001919
1920 default:
1921 if (!Stream.SkipBlock())
1922 continue;
1923 break;
1924 }
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001925
1926 Error("malformed block record in AST file");
1927 return Failure;
1928 }
1929
1930 if (Code == llvm::bitc::DEFINE_ABBREV) {
1931 Stream.ReadAbbrevRecord();
1932 continue;
1933 }
1934
1935 // Read and process a record.
1936 Record.clear();
1937 const char *BlobStart = 0;
1938 unsigned BlobLen = 0;
1939 switch ((ControlRecordTypes)Stream.ReadRecord(Code, Record,
1940 &BlobStart, &BlobLen)) {
1941 case METADATA: {
1942 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
Douglas Gregor38295be2012-10-22 23:51:00 +00001943 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
1944 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
1945 : diag::warn_pch_version_too_new);
Douglas Gregor4825fd72012-10-22 22:50:17 +00001946 return VersionMismatch;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001947 }
1948
1949 bool hasErrors = Record[5];
1950 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
1951 Diag(diag::err_pch_with_compiler_errors);
Douglas Gregor4825fd72012-10-22 22:50:17 +00001952 return HadErrors;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001953 }
1954
Douglas Gregorcaed0602012-10-18 21:31:35 +00001955 F.RelocatablePCH = Record[4];
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001956
1957 const std::string &CurBranch = getClangFullRepositoryVersion();
1958 StringRef ASTBranch(BlobStart, BlobLen);
1959 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
Douglas Gregor38295be2012-10-22 23:51:00 +00001960 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
1961 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregor4825fd72012-10-22 22:50:17 +00001962 return VersionMismatch;
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001963 }
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001964 break;
1965 }
1966
1967 case IMPORTS: {
1968 // Load each of the imported PCH files.
1969 unsigned Idx = 0, N = Record.size();
1970 while (Idx < N) {
1971 // Read information about the AST file.
1972 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
1973 unsigned Length = Record[Idx++];
1974 SmallString<128> ImportedFile(Record.begin() + Idx,
1975 Record.begin() + Idx + Length);
1976 Idx += Length;
1977
1978 // Load the AST file.
Douglas Gregor38295be2012-10-22 23:51:00 +00001979 switch(ReadASTCore(ImportedFile, ImportedKind, &F, Loaded,
1980 ClientLoadCapabilities)) {
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001981 case Failure: return Failure;
1982 // If we have to ignore the dependency, we'll have to ignore this too.
Douglas Gregor4825fd72012-10-22 22:50:17 +00001983 case OutOfDate: return OutOfDate;
1984 case VersionMismatch: return VersionMismatch;
1985 case ConfigurationMismatch: return ConfigurationMismatch;
1986 case HadErrors: return HadErrors;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001987 case Success: break;
1988 }
1989 }
1990 break;
1991 }
1992
Douglas Gregor38295be2012-10-22 23:51:00 +00001993 case LANGUAGE_OPTIONS: {
1994 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
1995 if (Listener && &F == *ModuleMgr.begin() &&
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00001996 ParseLanguageOptions(Record, Complain, *Listener) &&
1997 !DisableValidation)
Douglas Gregor4825fd72012-10-22 22:50:17 +00001998 return ConfigurationMismatch;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001999 break;
Douglas Gregor38295be2012-10-22 23:51:00 +00002000 }
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002001
Douglas Gregoree097c12012-10-18 17:58:09 +00002002 case TARGET_OPTIONS: {
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00002003 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2004 if (Listener && &F == *ModuleMgr.begin() &&
2005 ParseTargetOptions(Record, Complain, *Listener) &&
2006 !DisableValidation)
2007 return ConfigurationMismatch;
Douglas Gregoree097c12012-10-18 17:58:09 +00002008 break;
2009 }
2010
Douglas Gregor5f3d8222012-10-24 15:17:15 +00002011 case DIAGNOSTIC_OPTIONS: {
2012 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2013 if (Listener && &F == *ModuleMgr.begin() &&
2014 ParseDiagnosticOptions(Record, Complain, *Listener) &&
2015 !DisableValidation)
2016 return ConfigurationMismatch;
2017 break;
2018 }
Douglas Gregor1b2c3c02012-10-24 15:49:58 +00002019
2020 case FILE_SYSTEM_OPTIONS: {
2021 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2022 if (Listener && &F == *ModuleMgr.begin() &&
2023 ParseFileSystemOptions(Record, Complain, *Listener) &&
2024 !DisableValidation)
2025 return ConfigurationMismatch;
2026 break;
2027 }
2028
Douglas Gregorbbf38312012-10-24 16:50:34 +00002029 case HEADER_SEARCH_OPTIONS: {
2030 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2031 if (Listener && &F == *ModuleMgr.begin() &&
2032 ParseHeaderSearchOptions(Record, Complain, *Listener) &&
2033 !DisableValidation)
2034 return ConfigurationMismatch;
2035 break;
2036 }
2037
Douglas Gregor39c497b2012-10-18 18:36:53 +00002038 case ORIGINAL_FILE:
Douglas Gregor69e16082012-10-18 21:47:16 +00002039 F.OriginalSourceFileID = FileID::get(Record[0]);
2040 F.ActualOriginalSourceFileName.assign(BlobStart, BlobLen);
2041 F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
2042 MaybeAddSystemRootToFilename(F, F.OriginalSourceFileName);
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002043 break;
2044
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002045 case ORIGINAL_PCH_DIR:
Douglas Gregor69e16082012-10-18 21:47:16 +00002046 F.OriginalDir.assign(BlobStart, BlobLen);
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002047 break;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002048
Douglas Gregora930dc92012-10-22 18:42:04 +00002049 case INPUT_FILE_OFFSETS:
2050 F.InputFileOffsets = (const uint32_t *)BlobStart;
2051 F.InputFilesLoaded.resize(Record[0]);
Douglas Gregor745e6f12012-10-19 00:38:02 +00002052 break;
2053 }
Douglas Gregor745e6f12012-10-19 00:38:02 +00002054 }
2055
2056 Error("premature end of bitstream in AST file");
2057 return Failure;
2058}
2059
Douglas Gregor4825fd72012-10-22 22:50:17 +00002060bool ASTReader::ReadASTBlock(ModuleFile &F) {
Sebastian Redl9137a522010-07-16 17:50:48 +00002061 llvm::BitstreamCursor &Stream = F.Stream;
2062
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002063 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002064 Error("malformed block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002065 return true;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002066 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002067
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002068 // Read all of the records and blocks for the AST file.
Douglas Gregor8038d512009-04-10 17:25:41 +00002069 RecordData Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002070 while (!Stream.AtEndOfStream()) {
2071 unsigned Code = Stream.ReadCode();
2072 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002073 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002074 Error("error at end of module block in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002075 return true;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002076 }
Chris Lattner7356a312009-04-11 21:15:38 +00002077
Argyrios Kyrtzidis1f941242012-09-21 01:30:00 +00002078 DeclContext *DC = Context.getTranslationUnitDecl();
2079 if (!DC->hasExternalVisibleStorage() && DC->hasExternalLexicalStorage())
2080 DC->setMustBuildLookupTable();
2081
Douglas Gregor4825fd72012-10-22 22:50:17 +00002082 return false;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002083 }
2084
2085 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2086 switch (Stream.ReadSubBlockID()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002087 case DECLTYPES_BLOCK_ID:
Chris Lattner6367f6d2009-04-27 01:05:14 +00002088 // We lazily load the decls block, but we want to set up the
2089 // DeclsCursor cursor to point into it. Clone our current bitcode
2090 // cursor to it, enter the block and read the abbrevs in that block.
2091 // With the main cursor, we just skip over it.
Sebastian Redl9137a522010-07-16 17:50:48 +00002092 F.DeclsCursor = Stream;
Chris Lattner6367f6d2009-04-27 01:05:14 +00002093 if (Stream.SkipBlock() || // Skip with the main cursor.
2094 // Read the abbrevs.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002095 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002096 Error("malformed block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002097 return true;
Chris Lattner6367f6d2009-04-27 01:05:14 +00002098 }
2099 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002100
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002101 case DECL_UPDATES_BLOCK_ID:
2102 if (Stream.SkipBlock()) {
2103 Error("malformed block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002104 return true;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002105 }
2106 break;
2107
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002108 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl9137a522010-07-16 17:50:48 +00002109 F.MacroCursor = Stream;
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002110 if (!PP.getExternalSource())
2111 PP.setExternalSource(this);
Douglas Gregor88a35862010-01-04 19:18:44 +00002112
Douglas Gregorecdcb882010-10-20 22:00:55 +00002113 if (Stream.SkipBlock() ||
2114 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002115 Error("malformed block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002116 return true;
Chris Lattner7356a312009-04-11 21:15:38 +00002117 }
Douglas Gregorecdcb882010-10-20 22:00:55 +00002118 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
Chris Lattner7356a312009-04-11 21:15:38 +00002119 break;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002120
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002121 case PREPROCESSOR_DETAIL_BLOCK_ID:
2122 F.PreprocessorDetailCursor = Stream;
2123 if (Stream.SkipBlock() ||
2124 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
2125 PREPROCESSOR_DETAIL_BLOCK_ID)) {
2126 Error("malformed preprocessor detail record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002127 return true;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002128 }
2129 F.PreprocessorDetailStartOffset
2130 = F.PreprocessorDetailCursor.GetCurrentBitNo();
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002131
2132 if (!PP.getPreprocessingRecord())
Argyrios Kyrtzidisc6c54522012-03-05 05:48:17 +00002133 PP.createPreprocessingRecord(/*RecordConditionalDirectives=*/false);
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002134 if (!PP.getPreprocessingRecord()->getExternalSource())
2135 PP.getPreprocessingRecord()->SetExternalSource(*this);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002136 break;
2137
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002138 case SOURCE_MANAGER_BLOCK_ID:
Douglas Gregor4825fd72012-10-22 22:50:17 +00002139 if (ReadSourceManagerBlock(F))
2140 return true;
Douglas Gregor14f79002009-04-10 03:52:48 +00002141 break;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002142
2143 case SUBMODULE_BLOCK_ID:
Douglas Gregor4825fd72012-10-22 22:50:17 +00002144 if (ReadSubmoduleBlock(F))
2145 return true;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002146 break;
2147
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002148 case COMMENTS_BLOCK_ID: {
2149 llvm::BitstreamCursor C = Stream;
2150 if (Stream.SkipBlock() ||
2151 ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
2152 Error("malformed comments block in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002153 return true;
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002154 }
2155 CommentsCursors.push_back(std::make_pair(C, &F));
2156 break;
2157 }
2158
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002159 default:
2160 if (!Stream.SkipBlock())
2161 break;
2162 Error("malformed block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002163 return true;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002164 }
Douglas Gregor8038d512009-04-10 17:25:41 +00002165 continue;
2166 }
2167
2168 if (Code == llvm::bitc::DEFINE_ABBREV) {
2169 Stream.ReadAbbrevRecord();
2170 continue;
2171 }
2172
2173 // Read and process a record.
2174 Record.clear();
Douglas Gregor2bec0412009-04-10 21:16:55 +00002175 const char *BlobStart = 0;
2176 unsigned BlobLen = 0;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002177 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00002178 &BlobStart, &BlobLen)) {
Douglas Gregor8038d512009-04-10 17:25:41 +00002179 default: // Default behavior: ignore.
2180 break;
2181
Douglas Gregora119da02011-08-02 16:26:37 +00002182 case TYPE_OFFSET: {
Sebastian Redl12d6da02010-07-19 22:06:55 +00002183 if (F.LocalNumTypes != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002184 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002185 return true;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002186 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00002187 F.TypeOffsets = (const uint32_t *)BlobStart;
2188 F.LocalNumTypes = Record[0];
Douglas Gregore3605012011-08-02 18:32:54 +00002189 unsigned LocalBaseTypeIndex = Record[1];
2190 F.BaseTypeIndex = getTotalNumTypes();
Douglas Gregor1e849b62011-07-29 00:21:44 +00002191
Douglas Gregora119da02011-08-02 16:26:37 +00002192 if (F.LocalNumTypes > 0) {
2193 // Introduce the global -> local mapping for types within this module.
2194 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
2195
2196 // Introduce the local -> global mapping for types within this module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00002197 F.TypeRemap.insertOrReplace(
2198 std::make_pair(LocalBaseTypeIndex,
2199 F.BaseTypeIndex - LocalBaseTypeIndex));
Douglas Gregora119da02011-08-02 16:26:37 +00002200
2201 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
2202 }
Douglas Gregor8038d512009-04-10 17:25:41 +00002203 break;
Douglas Gregora119da02011-08-02 16:26:37 +00002204 }
2205
Douglas Gregor496c7092011-08-03 15:48:04 +00002206 case DECL_OFFSET: {
Sebastian Redl12d6da02010-07-19 22:06:55 +00002207 if (F.LocalNumDecls != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002208 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002209 return true;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002210 }
Argyrios Kyrtzidis9d31fa72011-10-27 18:47:35 +00002211 F.DeclOffsets = (const DeclOffset *)BlobStart;
Sebastian Redl12d6da02010-07-19 22:06:55 +00002212 F.LocalNumDecls = Record[0];
Douglas Gregor496c7092011-08-03 15:48:04 +00002213 unsigned LocalBaseDeclID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00002214 F.BaseDeclID = getTotalNumDecls();
Douglas Gregor96e973f2011-07-20 00:27:43 +00002215
Douglas Gregor496c7092011-08-03 15:48:04 +00002216 if (F.LocalNumDecls > 0) {
2217 // Introduce the global -> local mapping for declarations within this
2218 // module.
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002219 GlobalDeclMap.insert(
2220 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
Douglas Gregor496c7092011-08-03 15:48:04 +00002221
2222 // Introduce the local -> global mapping for declarations within this
2223 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00002224 F.DeclRemap.insertOrReplace(
2225 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
Douglas Gregor496c7092011-08-03 15:48:04 +00002226
Douglas Gregora1be2782011-12-17 23:38:30 +00002227 // Introduce the global -> local mapping for declarations within this
2228 // module.
2229 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
2230
Douglas Gregor496c7092011-08-03 15:48:04 +00002231 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
2232 }
Douglas Gregor8038d512009-04-10 17:25:41 +00002233 break;
Douglas Gregor496c7092011-08-03 15:48:04 +00002234 }
2235
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002236 case TU_UPDATE_LEXICAL: {
Douglas Gregor35942772011-09-09 21:34:22 +00002237 DeclContext *TU = Context.getTranslationUnitDecl();
Douglas Gregor0d95f772011-08-24 19:03:07 +00002238 DeclContextInfo &Info = F.DeclContextInfos[TU];
2239 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(BlobStart);
2240 Info.NumLexicalDecls
2241 = static_cast<unsigned int>(BlobLen / sizeof(KindDeclIDPair));
Douglas Gregor35942772011-09-09 21:34:22 +00002242 TU->setHasExternalLexicalStorage(true);
Sebastian Redld692af72010-07-27 18:24:41 +00002243 break;
2244 }
2245
Sebastian Redle1dde812010-08-24 00:50:04 +00002246 case UPDATE_VISIBLE: {
Douglas Gregor496c7092011-08-03 15:48:04 +00002247 unsigned Idx = 0;
2248 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
Benjamin Kramerb1758c62012-04-15 12:36:49 +00002249 ASTDeclContextNameLookupTable *Table =
2250 ASTDeclContextNameLookupTable::Create(
Douglas Gregor496c7092011-08-03 15:48:04 +00002251 (const unsigned char *)BlobStart + Record[Idx++],
Sebastian Redle1dde812010-08-24 00:50:04 +00002252 (const unsigned char *)BlobStart,
Douglas Gregor393f2492011-07-22 00:38:23 +00002253 ASTDeclContextNameLookupTrait(*this, F));
Douglas Gregor35942772011-09-09 21:34:22 +00002254 if (ID == PREDEF_DECL_TRANSLATION_UNIT_ID) { // Is it the TU?
2255 DeclContext *TU = Context.getTranslationUnitDecl();
Douglas Gregor0d95f772011-08-24 19:03:07 +00002256 F.DeclContextInfos[TU].NameLookupTableData = Table;
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002257 TU->setHasExternalVisibleStorage(true);
Sebastian Redle1dde812010-08-24 00:50:04 +00002258 } else
Douglas Gregor496c7092011-08-03 15:48:04 +00002259 PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F));
Sebastian Redle1dde812010-08-24 00:50:04 +00002260 break;
2261 }
2262
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002263 case IDENTIFIER_TABLE:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002264 F.IdentifierTableData = BlobStart;
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002265 if (Record[0]) {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002266 F.IdentifierLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002267 = ASTIdentifierLookupTable::Create(
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002268 (const unsigned char *)F.IdentifierTableData + Record[0],
2269 (const unsigned char *)F.IdentifierTableData,
Sebastian Redlc3632732010-10-05 15:59:54 +00002270 ASTIdentifierLookupTrait(*this, F));
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002271
2272 PP.getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002273 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002274 break;
2275
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002276 case IDENTIFIER_OFFSET: {
Sebastian Redl2da08f92010-07-19 22:28:42 +00002277 if (F.LocalNumIdentifiers != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002278 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002279 return true;
Douglas Gregorafaf3082009-04-11 00:14:32 +00002280 }
Sebastian Redl2da08f92010-07-19 22:28:42 +00002281 F.IdentifierOffsets = (const uint32_t *)BlobStart;
2282 F.LocalNumIdentifiers = Record[0];
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002283 unsigned LocalBaseIdentifierID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00002284 F.BaseIdentifierID = getTotalNumIdentifiers();
Douglas Gregor67268d02011-07-20 00:59:32 +00002285
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002286 if (F.LocalNumIdentifiers > 0) {
2287 // Introduce the global -> local mapping for identifiers within this
2288 // module.
2289 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
2290 &F));
2291
2292 // Introduce the local -> global mapping for identifiers within this
2293 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00002294 F.IdentifierRemap.insertOrReplace(
2295 std::make_pair(LocalBaseIdentifierID,
2296 F.BaseIdentifierID - LocalBaseIdentifierID));
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002297
2298 IdentifiersLoaded.resize(IdentifiersLoaded.size()
2299 + F.LocalNumIdentifiers);
2300 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002301 break;
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002302 }
Douglas Gregora8235d62012-10-09 23:05:51 +00002303
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002304 case EXTERNAL_DEFINITIONS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002305 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2306 ExternalDefinitions.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregorfdd01722009-04-14 00:24:19 +00002307 break;
Douglas Gregor3e1af842009-04-17 22:13:46 +00002308
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002309 case SPECIAL_TYPES:
Douglas Gregor393f2492011-07-22 00:38:23 +00002310 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2311 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
Douglas Gregorad1de002009-04-18 05:55:16 +00002312 break;
2313
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002314 case STATISTICS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002315 TotalNumStatements += Record[0];
2316 TotalNumMacros += Record[1];
2317 TotalLexicalDeclContexts += Record[2];
2318 TotalVisibleDeclContexts += Record[3];
Douglas Gregor3e1af842009-04-17 22:13:46 +00002319 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002320
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002321 case UNUSED_FILESCOPED_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002322 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2323 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002324 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002325
Sean Huntebcbe1d2011-05-04 23:29:54 +00002326 case DELEGATING_CTORS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002327 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2328 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
Sean Huntebcbe1d2011-05-04 23:29:54 +00002329 break;
2330
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002331 case WEAK_UNDECLARED_IDENTIFIERS:
Douglas Gregor31e37b22011-07-28 18:09:57 +00002332 if (Record.size() % 4 != 0) {
2333 Error("invalid weak identifiers record");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002334 return true;
Douglas Gregor31e37b22011-07-28 18:09:57 +00002335 }
2336
2337 // FIXME: Ignore weak undeclared identifiers from non-original PCH
2338 // files. This isn't the way to do it :)
2339 WeakUndeclaredIdentifiers.clear();
2340
2341 // Translate the weak, undeclared identifiers into global IDs.
2342 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
2343 WeakUndeclaredIdentifiers.push_back(
2344 getGlobalIdentifierID(F, Record[I++]));
2345 WeakUndeclaredIdentifiers.push_back(
2346 getGlobalIdentifierID(F, Record[I++]));
2347 WeakUndeclaredIdentifiers.push_back(
2348 ReadSourceLocation(F, Record, I).getRawEncoding());
2349 WeakUndeclaredIdentifiers.push_back(Record[I++]);
2350 }
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002351 break;
2352
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002353 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002354 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2355 LocallyScopedExternalDecls.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregor14c22f22009-04-22 22:18:58 +00002356 break;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002357
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002358 case SELECTOR_OFFSETS: {
Sebastian Redl059612d2010-08-03 21:58:15 +00002359 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redl725cd962010-08-04 20:40:17 +00002360 F.LocalNumSelectors = Record[0];
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002361 unsigned LocalBaseSelectorID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00002362 F.BaseSelectorID = getTotalNumSelectors();
Douglas Gregor96958cb2011-07-20 01:10:58 +00002363
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002364 if (F.LocalNumSelectors > 0) {
2365 // Introduce the global -> local mapping for selectors within this
2366 // module.
2367 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
2368
2369 // Introduce the local -> global mapping for selectors within this
2370 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00002371 F.SelectorRemap.insertOrReplace(
2372 std::make_pair(LocalBaseSelectorID,
2373 F.BaseSelectorID - LocalBaseSelectorID));
Douglas Gregor83941df2009-04-25 17:48:32 +00002374
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002375 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
2376 }
2377 break;
2378 }
2379
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002380 case METHOD_POOL:
Sebastian Redl725cd962010-08-04 20:40:17 +00002381 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor83941df2009-04-25 17:48:32 +00002382 if (Record[0])
Sebastian Redl725cd962010-08-04 20:40:17 +00002383 F.SelectorLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002384 = ASTSelectorLookupTable::Create(
Sebastian Redl725cd962010-08-04 20:40:17 +00002385 F.SelectorLookupTableData + Record[0],
2386 F.SelectorLookupTableData,
Douglas Gregor409448c2011-07-21 22:35:25 +00002387 ASTSelectorLookupTrait(*this, F));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002388 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002389 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002390
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002391 case REFERENCED_SELECTOR_POOL:
Douglas Gregor8451ec72011-07-28 14:41:43 +00002392 if (!Record.empty()) {
2393 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
2394 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
2395 Record[Idx++]));
2396 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
2397 getRawEncoding());
2398 }
2399 }
Fariborz Jahanian32019832010-07-23 19:11:11 +00002400 break;
2401
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002402 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002403 if (!Record.empty() && Listener)
Argyrios Kyrtzidis62288ed2012-10-10 02:12:47 +00002404 Listener->ReadCounter(F, Record[0]);
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002405 break;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002406
2407 case FILE_SORTED_DECLS:
2408 F.FileSortedDecls = (const DeclID *)BlobStart;
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002409 F.NumFileSortedDecls = Record[0];
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002410 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002411
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002412 case SOURCE_LOCATION_OFFSETS: {
2413 F.SLocEntryOffsets = (const uint32_t *)BlobStart;
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002414 F.LocalNumSLocEntries = Record[0];
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002415 unsigned SLocSpaceSize = Record[1];
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002416 llvm::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002417 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
2418 SLocSpaceSize);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002419 // Make our entry in the range map. BaseID is negative and growing, so
2420 // we invert it. Because we invert it, though, we need the other end of
2421 // the range.
2422 unsigned RangeStart =
2423 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
2424 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
2425 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
2426
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002427 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
2428 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
2429 GlobalSLocOffsetMap.insert(
2430 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
2431 - SLocSpaceSize,&F));
2432
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002433 // Initialize the remapping table.
2434 // Invalid stays invalid.
2435 F.SLocRemap.insert(std::make_pair(0U, 0));
2436 // This module. Base was 2 when being compiled.
2437 F.SLocRemap.insert(std::make_pair(2U,
2438 static_cast<int>(F.SLocEntryBaseOffset - 2)));
Douglas Gregor0cdd7982011-07-21 18:46:38 +00002439
2440 TotalNumSLocEntries += F.LocalNumSLocEntries;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002441 break;
2442 }
2443
Douglas Gregor5d51a1d2011-08-01 16:01:55 +00002444 case MODULE_OFFSET_MAP: {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002445 // Additional remapping information.
2446 const unsigned char *Data = (const unsigned char*)BlobStart;
2447 const unsigned char *DataEnd = Data + BlobLen;
Douglas Gregorf33740e2011-08-02 10:56:51 +00002448
2449 // Continuous range maps we may be updating in our module.
2450 ContinuousRangeMap<uint32_t, int, 2>::Builder SLocRemap(F.SLocRemap);
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002451 ContinuousRangeMap<uint32_t, int, 2>::Builder
2452 IdentifierRemap(F.IdentifierRemap);
Douglas Gregora8235d62012-10-09 23:05:51 +00002453 ContinuousRangeMap<uint32_t, int, 2>::Builder
2454 MacroRemap(F.MacroRemap);
2455 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002456 PreprocessedEntityRemap(F.PreprocessedEntityRemap);
2457 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregor26ced122011-12-01 00:59:36 +00002458 SubmoduleRemap(F.SubmoduleRemap);
2459 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002460 SelectorRemap(F.SelectorRemap);
Douglas Gregor496c7092011-08-03 15:48:04 +00002461 ContinuousRangeMap<uint32_t, int, 2>::Builder DeclRemap(F.DeclRemap);
Douglas Gregora119da02011-08-02 16:26:37 +00002462 ContinuousRangeMap<uint32_t, int, 2>::Builder TypeRemap(F.TypeRemap);
2463
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002464 while(Data < DataEnd) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002465 uint16_t Len = io::ReadUnalignedLE16(Data);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002466 StringRef Name = StringRef((const char*)Data, Len);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002467 Data += Len;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002468 ModuleFile *OM = ModuleMgr.lookup(Name);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002469 if (!OM) {
2470 Error("SourceLocation remap refers to unknown module");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002471 return true;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002472 }
Douglas Gregorf33740e2011-08-02 10:56:51 +00002473
2474 uint32_t SLocOffset = io::ReadUnalignedLE32(Data);
2475 uint32_t IdentifierIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregora8235d62012-10-09 23:05:51 +00002476 uint32_t MacroIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002477 uint32_t PreprocessedEntityIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregor26ced122011-12-01 00:59:36 +00002478 uint32_t SubmoduleIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002479 uint32_t SelectorIDOffset = io::ReadUnalignedLE32(Data);
2480 uint32_t DeclIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregora119da02011-08-02 16:26:37 +00002481 uint32_t TypeIndexOffset = io::ReadUnalignedLE32(Data);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002482
2483 // Source location offset is mapped to OM->SLocEntryBaseOffset.
2484 SLocRemap.insert(std::make_pair(SLocOffset,
2485 static_cast<int>(OM->SLocEntryBaseOffset - SLocOffset)));
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002486 IdentifierRemap.insert(
2487 std::make_pair(IdentifierIDOffset,
2488 OM->BaseIdentifierID - IdentifierIDOffset));
Douglas Gregora8235d62012-10-09 23:05:51 +00002489 MacroRemap.insert(std::make_pair(MacroIDOffset,
2490 OM->BaseMacroID - MacroIDOffset));
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002491 PreprocessedEntityRemap.insert(
2492 std::make_pair(PreprocessedEntityIDOffset,
2493 OM->BasePreprocessedEntityID - PreprocessedEntityIDOffset));
Douglas Gregor26ced122011-12-01 00:59:36 +00002494 SubmoduleRemap.insert(std::make_pair(SubmoduleIDOffset,
2495 OM->BaseSubmoduleID - SubmoduleIDOffset));
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002496 SelectorRemap.insert(std::make_pair(SelectorIDOffset,
2497 OM->BaseSelectorID - SelectorIDOffset));
Douglas Gregor496c7092011-08-03 15:48:04 +00002498 DeclRemap.insert(std::make_pair(DeclIDOffset,
2499 OM->BaseDeclID - DeclIDOffset));
2500
Douglas Gregora119da02011-08-02 16:26:37 +00002501 TypeRemap.insert(std::make_pair(TypeIndexOffset,
Douglas Gregore3605012011-08-02 18:32:54 +00002502 OM->BaseTypeIndex - TypeIndexOffset));
Douglas Gregora1be2782011-12-17 23:38:30 +00002503
2504 // Global -> local mappings.
2505 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002506 }
2507 break;
2508 }
2509
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002510 case SOURCE_MANAGER_LINE_TABLE:
2511 if (ParseLineTable(F, Record))
Douglas Gregor4825fd72012-10-22 22:50:17 +00002512 return true;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002513 break;
2514
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002515 case SOURCE_LOCATION_PRELOADS: {
2516 // Need to transform from the local view (1-based IDs) to the global view,
2517 // which is based off F.SLocEntryBaseID.
Douglas Gregorf249bf32011-08-25 21:09:44 +00002518 if (!F.PreloadSLocEntries.empty()) {
2519 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002520 return true;
Douglas Gregorf249bf32011-08-25 21:09:44 +00002521 }
2522
2523 F.PreloadSLocEntries.swap(Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002524 break;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002525 }
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002526
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002527 case STAT_CACHE: {
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00002528 if (!DisableStatCache) {
2529 ASTStatCache *MyStatCache =
2530 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
2531 (const unsigned char *)BlobStart,
2532 NumStatHits, NumStatMisses);
2533 FileMgr.addStatCache(MyStatCache);
2534 F.StatCache = MyStatCache;
2535 }
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002536 break;
Douglas Gregor52e71082009-10-16 18:18:30 +00002537 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002538
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002539 case EXT_VECTOR_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002540 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2541 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregorb81c1702009-04-27 20:06:05 +00002542 break;
2543
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002544 case VTABLE_USES:
Douglas Gregordfe65432011-07-28 19:11:31 +00002545 if (Record.size() % 3 != 0) {
2546 Error("Invalid VTABLE_USES record");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002547 return true;
Douglas Gregordfe65432011-07-28 19:11:31 +00002548 }
2549
Sebastian Redl40566802010-08-05 18:21:25 +00002550 // Later tables overwrite earlier ones.
Douglas Gregordfe65432011-07-28 19:11:31 +00002551 // FIXME: Modules will have some trouble with this. This is clearly not
2552 // the right way to do this.
Douglas Gregor409448c2011-07-21 22:35:25 +00002553 VTableUses.clear();
Douglas Gregordfe65432011-07-28 19:11:31 +00002554
2555 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
2556 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
2557 VTableUses.push_back(
2558 ReadSourceLocation(F, Record, Idx).getRawEncoding());
2559 VTableUses.push_back(Record[Idx++]);
2560 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002561 break;
2562
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002563 case DYNAMIC_CLASSES:
Douglas Gregor409448c2011-07-21 22:35:25 +00002564 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2565 DynamicClasses.push_back(getGlobalDeclID(F, Record[I]));
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002566 break;
2567
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002568 case PENDING_IMPLICIT_INSTANTIATIONS:
Douglas Gregorf2abb522011-07-28 19:26:52 +00002569 if (PendingInstantiations.size() % 2 != 0) {
Axel Naumann39d26c32012-10-02 09:09:43 +00002570 Error("Invalid existing PendingInstantiations");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002571 return true;
Axel Naumann39d26c32012-10-02 09:09:43 +00002572 }
2573
2574 if (Record.size() % 2 != 0) {
Douglas Gregorf2abb522011-07-28 19:26:52 +00002575 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002576 return true;
Douglas Gregorf2abb522011-07-28 19:26:52 +00002577 }
Axel Naumann39d26c32012-10-02 09:09:43 +00002578
Douglas Gregorf2abb522011-07-28 19:26:52 +00002579 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2580 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
2581 PendingInstantiations.push_back(
2582 ReadSourceLocation(F, Record, I).getRawEncoding());
2583 }
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002584 break;
2585
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002586 case SEMA_DECL_REFS:
Sebastian Redl40566802010-08-05 18:21:25 +00002587 // Later tables overwrite earlier ones.
Douglas Gregor409448c2011-07-21 22:35:25 +00002588 // FIXME: Modules will have some trouble with this.
2589 SemaDeclRefs.clear();
2590 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2591 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002592 break;
2593
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002594 case PPD_ENTITIES_OFFSETS: {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002595 F.PreprocessedEntityOffsets = (const PPEntityOffset *)BlobStart;
2596 assert(BlobLen % sizeof(PPEntityOffset) == 0);
2597 F.NumPreprocessedEntities = BlobLen / sizeof(PPEntityOffset);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002598
2599 unsigned LocalBasePreprocessedEntityID = Record[0];
Douglas Gregorfb2d9e02011-08-04 16:36:56 +00002600
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002601 unsigned StartingID;
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002602 if (!PP.getPreprocessingRecord())
Argyrios Kyrtzidisc6c54522012-03-05 05:48:17 +00002603 PP.createPreprocessingRecord(/*RecordConditionalDirectives=*/false);
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002604 if (!PP.getPreprocessingRecord()->getExternalSource())
2605 PP.getPreprocessingRecord()->SetExternalSource(*this);
2606 StartingID
2607 = PP.getPreprocessingRecord()
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002608 ->allocateLoadedEntities(F.NumPreprocessedEntities);
Douglas Gregor9827a802011-07-29 00:56:45 +00002609 F.BasePreprocessedEntityID = StartingID;
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002610
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002611 if (F.NumPreprocessedEntities > 0) {
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002612 // Introduce the global -> local mapping for preprocessed entities in
2613 // this module.
2614 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
2615
2616 // Introduce the local -> global mapping for preprocessed entities in
2617 // this module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00002618 F.PreprocessedEntityRemap.insertOrReplace(
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002619 std::make_pair(LocalBasePreprocessedEntityID,
2620 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
2621 }
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002622
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002623 break;
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002624 }
2625
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002626 case DECL_UPDATE_OFFSETS: {
2627 if (Record.size() % 2 != 0) {
2628 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002629 return true;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002630 }
2631 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Douglas Gregor496c7092011-08-03 15:48:04 +00002632 DeclUpdateOffsets[getGlobalDeclID(F, Record[I])]
2633 .push_back(std::make_pair(&F, Record[I+1]));
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002634 break;
2635 }
2636
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002637 case DECL_REPLACEMENTS: {
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00002638 if (Record.size() % 3 != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002639 Error("invalid DECL_REPLACEMENTS block in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002640 return true;
Sebastian Redl0b17c612010-08-13 00:28:03 +00002641 }
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00002642 for (unsigned I = 0, N = Record.size(); I != N; I += 3)
Douglas Gregor496c7092011-08-03 15:48:04 +00002643 ReplacedDecls[getGlobalDeclID(F, Record[I])]
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00002644 = ReplacedDeclInfo(&F, Record[I+1], Record[I+2]);
Sebastian Redl0b17c612010-08-13 00:28:03 +00002645 break;
2646 }
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00002647
Douglas Gregorcff9f262012-01-27 01:47:08 +00002648 case OBJC_CATEGORIES_MAP: {
2649 if (F.LocalNumObjCCategoriesInMap != 0) {
2650 Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002651 return true;
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00002652 }
Douglas Gregorcff9f262012-01-27 01:47:08 +00002653
2654 F.LocalNumObjCCategoriesInMap = Record[0];
2655 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)BlobStart;
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00002656 break;
2657 }
Douglas Gregor7c789c12010-10-29 22:39:52 +00002658
Douglas Gregorcff9f262012-01-27 01:47:08 +00002659 case OBJC_CATEGORIES:
2660 F.ObjCCategories.swap(Record);
2661 break;
2662
Douglas Gregor7c789c12010-10-29 22:39:52 +00002663 case CXX_BASE_SPECIFIER_OFFSETS: {
2664 if (F.LocalNumCXXBaseSpecifiers != 0) {
2665 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002666 return true;
Douglas Gregor7c789c12010-10-29 22:39:52 +00002667 }
2668
2669 F.LocalNumCXXBaseSpecifiers = Record[0];
2670 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart;
Jonathan D. Turner1da90142011-07-21 21:15:19 +00002671 NumCXXBaseSpecifiersLoaded += F.LocalNumCXXBaseSpecifiers;
Douglas Gregor7c789c12010-10-29 22:39:52 +00002672 break;
2673 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002674
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002675 case DIAG_PRAGMA_MAPPINGS:
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002676 if (Record.size() % 2 != 0) {
2677 Error("invalid DIAG_USER_MAPPINGS block in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002678 return true;
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002679 }
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002680
2681 if (F.PragmaDiagMappings.empty())
2682 F.PragmaDiagMappings.swap(Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002683 else
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002684 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
2685 Record.begin(), Record.end());
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002686 break;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002687
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002688 case CUDA_SPECIAL_DECL_REFS:
2689 // Later tables overwrite earlier ones.
Douglas Gregor409448c2011-07-21 22:35:25 +00002690 // FIXME: Modules will have trouble with this.
2691 CUDASpecialDeclRefs.clear();
2692 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2693 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002694 break;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002695
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002696 case HEADER_SEARCH_TABLE: {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002697 F.HeaderFileInfoTableData = BlobStart;
2698 F.LocalNumHeaderFileInfos = Record[1];
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002699 F.HeaderFileFrameworkStrings = BlobStart + Record[2];
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002700 if (Record[0]) {
2701 F.HeaderFileInfoTable
2702 = HeaderFileInfoLookupTable::Create(
2703 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002704 (const unsigned char *)F.HeaderFileInfoTableData,
Douglas Gregor95eab172011-07-28 20:55:49 +00002705 HeaderFileInfoTrait(*this, F,
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002706 &PP.getHeaderSearchInfo(),
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002707 BlobStart + Record[2]));
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002708
2709 PP.getHeaderSearchInfo().SetExternalSource(this);
2710 if (!PP.getHeaderSearchInfo().getExternalLookup())
2711 PP.getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002712 }
2713 break;
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002714 }
2715
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002716 case FP_PRAGMA_OPTIONS:
2717 // Later tables overwrite earlier ones.
2718 FPPragmaOptions.swap(Record);
2719 break;
2720
2721 case OPENCL_EXTENSIONS:
2722 // Later tables overwrite earlier ones.
2723 OpenCLExtensions.swap(Record);
2724 break;
Sean Huntebcbe1d2011-05-04 23:29:54 +00002725
2726 case TENTATIVE_DEFINITIONS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002727 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2728 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
Sean Huntebcbe1d2011-05-04 23:29:54 +00002729 break;
Douglas Gregord8bba9c2011-06-28 16:20:02 +00002730
2731 case KNOWN_NAMESPACES:
Douglas Gregor409448c2011-07-21 22:35:25 +00002732 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2733 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregord8bba9c2011-06-28 16:20:02 +00002734 break;
Douglas Gregorf6137e42011-12-03 00:59:55 +00002735
2736 case IMPORTED_MODULES: {
2737 if (F.Kind != MK_Module) {
2738 // If we aren't loading a module (which has its own exports), make
2739 // all of the imported modules visible.
2740 // FIXME: Deal with macros-only imports.
2741 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2742 if (unsigned GlobalID = getGlobalSubmoduleID(F, Record[I]))
2743 ImportedModules.push_back(GlobalID);
2744 }
2745 }
2746 break;
Douglas Gregora1be2782011-12-17 23:38:30 +00002747 }
Douglas Gregor2171bf12012-01-15 16:58:34 +00002748
Douglas Gregora1be2782011-12-17 23:38:30 +00002749 case LOCAL_REDECLARATIONS: {
Douglas Gregor2171bf12012-01-15 16:58:34 +00002750 F.RedeclarationChains.swap(Record);
2751 break;
2752 }
2753
2754 case LOCAL_REDECLARATIONS_MAP: {
2755 if (F.LocalNumRedeclarationsInMap != 0) {
2756 Error("duplicate LOCAL_REDECLARATIONS_MAP record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002757 return true;
Douglas Gregora1be2782011-12-17 23:38:30 +00002758 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00002759
Douglas Gregor2171bf12012-01-15 16:58:34 +00002760 F.LocalNumRedeclarationsInMap = Record[0];
2761 F.RedeclarationsMap = (const LocalRedeclarationsInfo *)BlobStart;
Douglas Gregora1be2782011-12-17 23:38:30 +00002762 break;
Douglas Gregorf6137e42011-12-03 00:59:55 +00002763 }
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00002764
2765 case MERGED_DECLARATIONS: {
2766 for (unsigned Idx = 0; Idx < Record.size(); /* increment in loop */) {
2767 GlobalDeclID CanonID = getGlobalDeclID(F, Record[Idx++]);
2768 SmallVectorImpl<GlobalDeclID> &Decls = StoredMergedDecls[CanonID];
2769 for (unsigned N = Record[Idx++]; N > 0; --N)
2770 Decls.push_back(getGlobalDeclID(F, Record[Idx++]));
2771 }
2772 break;
2773 }
Douglas Gregora8235d62012-10-09 23:05:51 +00002774
2775 case MACRO_OFFSET: {
2776 if (F.LocalNumMacros != 0) {
2777 Error("duplicate MACRO_OFFSET record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002778 return true;
Douglas Gregora8235d62012-10-09 23:05:51 +00002779 }
2780 F.MacroOffsets = (const uint32_t *)BlobStart;
2781 F.LocalNumMacros = Record[0];
2782 unsigned LocalBaseMacroID = Record[1];
2783 F.BaseMacroID = getTotalNumMacros();
2784
2785 if (F.LocalNumMacros > 0) {
2786 // Introduce the global -> local mapping for macros within this module.
2787 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
2788
2789 // Introduce the local -> global mapping for macros within this module.
2790 F.MacroRemap.insertOrReplace(
2791 std::make_pair(LocalBaseMacroID,
2792 F.BaseMacroID - LocalBaseMacroID));
2793
2794 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
2795 }
2796 break;
2797 }
2798
2799 case MACRO_UPDATES: {
2800 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2801 MacroID ID = getGlobalMacroID(F, Record[I++]);
2802 if (I == N)
2803 break;
2804
Douglas Gregor54c8a402012-10-12 00:16:50 +00002805 SourceLocation UndefLoc = ReadSourceLocation(F, Record, I);
2806 SubmoduleID SubmoduleID = getGlobalSubmoduleID(F, Record[I++]);;
2807 MacroUpdate Update;
2808 Update.UndefLoc = UndefLoc;
2809 MacroUpdates[ID].push_back(std::make_pair(SubmoduleID, Update));
Douglas Gregora8235d62012-10-09 23:05:51 +00002810 }
2811 break;
2812 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002813 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002814 }
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002815 Error("premature end of bitstream in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002816 return true;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002817}
2818
Douglas Gregorecc2c092011-12-01 22:20:10 +00002819void ASTReader::makeNamesVisible(const HiddenNames &Names) {
Douglas Gregor13292642011-12-02 15:45:10 +00002820 for (unsigned I = 0, N = Names.size(); I != N; ++I) {
Douglas Gregor54c8a402012-10-12 00:16:50 +00002821 switch (Names[I].getKind()) {
2822 case HiddenName::Declaration:
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00002823 Names[I].getDecl()->Hidden = false;
Douglas Gregor54c8a402012-10-12 00:16:50 +00002824 break;
2825
2826 case HiddenName::MacroVisibility: {
2827 std::pair<IdentifierInfo *, MacroInfo *> Macro = Names[I].getMacro();
2828 Macro.second->setHidden(!Macro.second->isPublic());
2829 if (Macro.second->isDefined()) {
2830 PP.makeLoadedMacroInfoVisible(Macro.first, Macro.second);
2831 }
2832 break;
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00002833 }
2834
Douglas Gregor54c8a402012-10-12 00:16:50 +00002835 case HiddenName::MacroUndef: {
2836 std::pair<IdentifierInfo *, MacroInfo *> Macro = Names[I].getMacro();
2837 if (Macro.second->isDefined()) {
2838 Macro.second->setUndefLoc(Names[I].getMacroUndefLoc());
2839 if (PPMutationListener *Listener = PP.getPPMutationListener())
2840 Listener->UndefinedMacro(Macro.second);
2841 PP.makeLoadedMacroInfoVisible(Macro.first, Macro.second);
2842 }
2843 break;
2844 }
Douglas Gregor1d4c1132011-12-20 22:06:13 +00002845 }
Douglas Gregor13292642011-12-02 15:45:10 +00002846 }
Douglas Gregorecc2c092011-12-01 22:20:10 +00002847}
2848
Douglas Gregor5e356932011-12-01 17:11:21 +00002849void ASTReader::makeModuleVisible(Module *Mod,
2850 Module::NameVisibilityKind NameVisibility) {
2851 llvm::SmallPtrSet<Module *, 4> Visited;
2852 llvm::SmallVector<Module *, 4> Stack;
2853 Stack.push_back(Mod);
2854 while (!Stack.empty()) {
2855 Mod = Stack.back();
2856 Stack.pop_back();
2857
2858 if (NameVisibility <= Mod->NameVisibility) {
2859 // This module already has this level of visibility (or greater), so
2860 // there is nothing more to do.
2861 continue;
2862 }
2863
Douglas Gregor51f564f2011-12-31 04:05:44 +00002864 if (!Mod->isAvailable()) {
2865 // Modules that aren't available cannot be made visible.
2866 continue;
2867 }
2868
Douglas Gregor5e356932011-12-01 17:11:21 +00002869 // Update the module's name visibility.
2870 Mod->NameVisibility = NameVisibility;
2871
Douglas Gregorecc2c092011-12-01 22:20:10 +00002872 // If we've already deserialized any names from this module,
Douglas Gregor5e356932011-12-01 17:11:21 +00002873 // mark them as visible.
Douglas Gregorecc2c092011-12-01 22:20:10 +00002874 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
2875 if (Hidden != HiddenNamesMap.end()) {
2876 makeNamesVisible(Hidden->second);
2877 HiddenNamesMap.erase(Hidden);
2878 }
Douglas Gregor5e356932011-12-01 17:11:21 +00002879
2880 // Push any non-explicit submodules onto the stack to be marked as
2881 // visible.
Douglas Gregorb7a78192012-01-04 23:32:19 +00002882 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2883 SubEnd = Mod->submodule_end();
Douglas Gregor5e356932011-12-01 17:11:21 +00002884 Sub != SubEnd; ++Sub) {
Douglas Gregorb7a78192012-01-04 23:32:19 +00002885 if (!(*Sub)->IsExplicit && Visited.insert(*Sub))
2886 Stack.push_back(*Sub);
Douglas Gregor5e356932011-12-01 17:11:21 +00002887 }
Douglas Gregor07165b92011-12-02 19:11:09 +00002888
2889 // Push any exported modules onto the stack to be marked as visible.
Douglas Gregor0adaa882011-12-05 17:28:06 +00002890 bool AnyWildcard = false;
2891 bool UnrestrictedWildcard = false;
2892 llvm::SmallVector<Module *, 4> WildcardRestrictions;
Douglas Gregor07165b92011-12-02 19:11:09 +00002893 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
2894 Module *Exported = Mod->Exports[I].getPointer();
Douglas Gregor0adaa882011-12-05 17:28:06 +00002895 if (!Mod->Exports[I].getInt()) {
2896 // Export a named module directly; no wildcards involved.
2897 if (Visited.insert(Exported))
Douglas Gregor07165b92011-12-02 19:11:09 +00002898 Stack.push_back(Exported);
Douglas Gregor0adaa882011-12-05 17:28:06 +00002899
2900 continue;
Douglas Gregor07165b92011-12-02 19:11:09 +00002901 }
Douglas Gregor0adaa882011-12-05 17:28:06 +00002902
2903 // Wildcard export: export all of the imported modules that match
2904 // the given pattern.
2905 AnyWildcard = true;
2906 if (UnrestrictedWildcard)
2907 continue;
2908
2909 if (Module *Restriction = Mod->Exports[I].getPointer())
2910 WildcardRestrictions.push_back(Restriction);
2911 else {
2912 WildcardRestrictions.clear();
2913 UnrestrictedWildcard = true;
2914 }
2915 }
2916
2917 // If there were any wildcards, push any imported modules that were
2918 // re-exported by the wildcard restriction.
2919 if (!AnyWildcard)
2920 continue;
2921
2922 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
2923 Module *Imported = Mod->Imports[I];
Benjamin Kramerd48bcb22012-08-22 15:37:55 +00002924 if (!Visited.insert(Imported))
Douglas Gregor0adaa882011-12-05 17:28:06 +00002925 continue;
2926
2927 bool Acceptable = UnrestrictedWildcard;
2928 if (!Acceptable) {
2929 // Check whether this module meets one of the restrictions.
2930 for (unsigned R = 0, NR = WildcardRestrictions.size(); R != NR; ++R) {
2931 Module *Restriction = WildcardRestrictions[R];
2932 if (Imported == Restriction || Imported->isSubModuleOf(Restriction)) {
2933 Acceptable = true;
2934 break;
2935 }
2936 }
2937 }
2938
2939 if (!Acceptable)
2940 continue;
2941
Douglas Gregor0adaa882011-12-05 17:28:06 +00002942 Stack.push_back(Imported);
Douglas Gregor07165b92011-12-02 19:11:09 +00002943 }
Douglas Gregor5e356932011-12-01 17:11:21 +00002944 }
2945}
2946
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002947ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
Douglas Gregor38295be2012-10-22 23:51:00 +00002948 ModuleKind Type,
2949 unsigned ClientLoadCapabilities) {
Douglas Gregor057df202012-01-18 20:56:22 +00002950 // Bump the generation number.
Douglas Gregorcff9f262012-01-27 01:47:08 +00002951 unsigned PreviousGeneration = CurrentGeneration++;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002952
2953 // Load the core of the AST files.
2954 llvm::SmallVector<ModuleFile *, 4> Loaded;
Douglas Gregor38295be2012-10-22 23:51:00 +00002955 switch(ReadASTCore(FileName, Type, /*ImportedBy=*/0, Loaded,
2956 ClientLoadCapabilities)) {
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002957 case Failure: return Failure;
Douglas Gregor4825fd72012-10-22 22:50:17 +00002958 case OutOfDate: return OutOfDate;
2959 case VersionMismatch: return VersionMismatch;
2960 case ConfigurationMismatch: return ConfigurationMismatch;
2961 case HadErrors: return HadErrors;
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002962 case Success: break;
2963 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002964
2965 // Here comes stuff that we only do once the entire chain is loaded.
Douglas Gregor057df202012-01-18 20:56:22 +00002966
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002967 // Load the AST blocks of all of the modules that we loaded.
2968 for (llvm::SmallVectorImpl<ModuleFile *>::iterator M = Loaded.begin(),
2969 MEnd = Loaded.end();
2970 M != MEnd; ++M) {
2971 ModuleFile &F = **M;
2972
2973 // Read the AST block.
Douglas Gregor4825fd72012-10-22 22:50:17 +00002974 if (ReadASTBlock(F))
2975 return Failure;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002976
2977 // Once read, set the ModuleFile bit base offset and update the size in
2978 // bits of all files we've seen.
2979 F.GlobalBitOffset = TotalModulesSizeInBits;
2980 TotalModulesSizeInBits += F.SizeInBits;
2981 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
2982
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002983 // Preload SLocEntries.
2984 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
2985 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
Douglas Gregor8b53d142012-10-22 22:53:10 +00002986 // Load it through the SourceManager and don't call ReadSLocEntry()
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002987 // directly because the entry may have already been loaded in which case
Douglas Gregor8b53d142012-10-22 22:53:10 +00002988 // calling ReadSLocEntry() directly would trigger an assertion in
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002989 // SourceManager.
2990 SourceMgr.getLoadedSLocEntryByID(Index);
2991 }
2992 }
2993
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002994 // Check the predefines buffers.
Douglas Gregor38295be2012-10-22 23:51:00 +00002995 bool ConfigComplain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
Douglas Gregor6236a292011-12-02 21:56:05 +00002996 if (!DisableValidation && Type == MK_PCH &&
Argyrios Kyrtzidis26d43cd2011-09-12 18:09:38 +00002997 // FIXME: CheckPredefinesBuffers also sets the SuggestedPredefines;
2998 // if DisableValidation is true, defines that were set on command-line
2999 // but not in the PCH file will not be added to SuggestedPredefines.
Douglas Gregor38295be2012-10-22 23:51:00 +00003000 CheckPredefinesBuffers(ConfigComplain))
Douglas Gregor4825fd72012-10-22 22:50:17 +00003001 return ConfigurationMismatch;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00003002
Douglas Gregoreee242f2011-10-27 09:33:13 +00003003 // Mark all of the identifiers in the identifier table as being out of date,
3004 // so that various accessors know to check the loaded modules when the
3005 // identifier is used.
Douglas Gregor712f2fc2011-09-09 22:02:16 +00003006 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
3007 IdEnd = PP.getIdentifierTable().end();
3008 Id != IdEnd; ++Id)
Douglas Gregoreee242f2011-10-27 09:33:13 +00003009 Id->second->setOutOfDate(true);
Douglas Gregor057df202012-01-18 20:56:22 +00003010
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003011 // Resolve any unresolved module exports.
Douglas Gregor55988682011-12-05 16:33:54 +00003012 for (unsigned I = 0, N = UnresolvedModuleImportExports.size(); I != N; ++I) {
3013 UnresolvedModuleImportExport &Unresolved = UnresolvedModuleImportExports[I];
3014 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
Douglas Gregor0adaa882011-12-05 17:28:06 +00003015 Module *ResolvedMod = getSubmodule(GlobalID);
3016
3017 if (Unresolved.IsImport) {
3018 if (ResolvedMod)
Douglas Gregor55988682011-12-05 16:33:54 +00003019 Unresolved.Mod->Imports.push_back(ResolvedMod);
Douglas Gregor0adaa882011-12-05 17:28:06 +00003020 continue;
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003021 }
Douglas Gregor0adaa882011-12-05 17:28:06 +00003022
3023 if (ResolvedMod || Unresolved.IsWildcard)
3024 Unresolved.Mod->Exports.push_back(
3025 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003026 }
Douglas Gregor55988682011-12-05 16:33:54 +00003027 UnresolvedModuleImportExports.clear();
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003028
Douglas Gregor35942772011-09-09 21:34:22 +00003029 InitializeContext();
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00003030
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003031 if (DeserializationListener)
3032 DeserializationListener->ReaderInitialized(this);
3033
Douglas Gregor11407b82012-10-18 21:18:25 +00003034 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
3035 if (!PrimaryModule.OriginalSourceFileID.isInvalid()) {
3036 PrimaryModule.OriginalSourceFileID
3037 = FileID::get(PrimaryModule.SLocEntryBaseID
3038 + PrimaryModule.OriginalSourceFileID.getOpaqueValue() - 1);
Argyrios Kyrtzidisb8c879a2012-01-05 21:36:25 +00003039
Douglas Gregor11407b82012-10-18 21:18:25 +00003040 // If this AST file is a precompiled preamble, then set the
3041 // preamble file ID of the source manager to the file source file
3042 // from which the preamble was built.
Argyrios Kyrtzidisb8c879a2012-01-05 21:36:25 +00003043 if (Type == MK_Preamble) {
Douglas Gregor11407b82012-10-18 21:18:25 +00003044 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
Argyrios Kyrtzidisb8c879a2012-01-05 21:36:25 +00003045 } else if (Type == MK_MainFile) {
Douglas Gregor11407b82012-10-18 21:18:25 +00003046 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00003047 }
Douglas Gregor414cb642010-11-30 05:23:00 +00003048 }
3049
Douglas Gregorcff9f262012-01-27 01:47:08 +00003050 // For any Objective-C class definitions we have already loaded, make sure
3051 // that we load any additional categories.
3052 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
3053 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
3054 ObjCClassesLoaded[I],
3055 PreviousGeneration);
3056 }
3057
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00003058 return Success;
3059}
3060
Douglas Gregor38295be2012-10-22 23:51:00 +00003061ASTReader::ASTReadResult
3062ASTReader::ReadASTCore(StringRef FileName,
3063 ModuleKind Type,
3064 ModuleFile *ImportedBy,
3065 llvm::SmallVectorImpl<ModuleFile *> &Loaded,
3066 unsigned ClientLoadCapabilities) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003067 ModuleFile *M;
Douglas Gregorfac4ece2011-08-19 02:29:29 +00003068 bool NewModule;
3069 std::string ErrorStr;
3070 llvm::tie(M, NewModule) = ModuleMgr.addModule(FileName, Type, ImportedBy,
Douglas Gregor057df202012-01-18 20:56:22 +00003071 CurrentGeneration, ErrorStr);
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00003072
Douglas Gregorfac4ece2011-08-19 02:29:29 +00003073 if (!M) {
3074 // We couldn't load the module.
3075 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
3076 + ErrorStr;
3077 Error(Msg);
3078 return Failure;
3079 }
3080
3081 if (!NewModule) {
3082 // We've already loaded this module.
3083 return Success;
3084 }
3085
3086 // FIXME: This seems rather a hack. Should CurrentDir be part of the
3087 // module?
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00003088 if (FileName != "-") {
3089 CurrentDir = llvm::sys::path::parent_path(FileName);
3090 if (CurrentDir.empty()) CurrentDir = ".";
3091 }
3092
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003093 ModuleFile &F = *M;
Sebastian Redl9137a522010-07-16 17:50:48 +00003094 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00003095 Stream.init(F.StreamFile);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00003096 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Douglas Gregor8f1231b2011-07-22 06:10:01 +00003097
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00003098 // Sniff for the signature.
3099 if (Stream.Read(8) != 'C' ||
3100 Stream.Read(8) != 'P' ||
3101 Stream.Read(8) != 'C' ||
3102 Stream.Read(8) != 'H') {
3103 Diag(diag::err_not_a_pch_file) << FileName;
3104 return Failure;
3105 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003106
Douglas Gregor2cf26342009-04-09 22:27:44 +00003107 while (!Stream.AtEndOfStream()) {
3108 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00003109
Douglas Gregore1d918e2009-04-10 23:10:45 +00003110 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003111 Error("invalid record at top-level of AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00003112 return Failure;
3113 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003114
3115 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregor668c1a42009-04-21 22:25:48 +00003116
Douglas Gregor7ae467f2012-10-18 18:27:37 +00003117 // We only know the control subblock ID.
Douglas Gregor2cf26342009-04-09 22:27:44 +00003118 switch (BlockID) {
3119 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00003120 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003121 Error("malformed BlockInfoBlock in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00003122 return Failure;
3123 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003124 break;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003125 case CONTROL_BLOCK_ID:
Douglas Gregor38295be2012-10-22 23:51:00 +00003126 switch (ReadControlBlock(F, Loaded, ClientLoadCapabilities)) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003127 case Success:
3128 break;
3129
Douglas Gregor4825fd72012-10-22 22:50:17 +00003130 case Failure: return Failure;
3131 case OutOfDate: return OutOfDate;
3132 case VersionMismatch: return VersionMismatch;
3133 case ConfigurationMismatch: return ConfigurationMismatch;
3134 case HadErrors: return HadErrors;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003135 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003136 break;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003137 case AST_BLOCK_ID:
3138 // Record that we've loaded this module.
3139 Loaded.push_back(M);
3140 return Success;
3141
Douglas Gregor2cf26342009-04-09 22:27:44 +00003142 default:
Douglas Gregore1d918e2009-04-10 23:10:45 +00003143 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003144 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00003145 return Failure;
3146 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003147 break;
3148 }
Mike Stump1eb44332009-09-09 15:08:12 +00003149 }
Douglas Gregor8f1231b2011-07-22 06:10:01 +00003150
Sebastian Redlcdf3b832010-07-16 20:41:52 +00003151 return Success;
3152}
3153
Douglas Gregor712f2fc2011-09-09 22:02:16 +00003154void ASTReader::InitializeContext() {
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003155 // If there's a listener, notify them that we "read" the translation unit.
3156 if (DeserializationListener)
Douglas Gregor35942772011-09-09 21:34:22 +00003157 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
3158 Context.getTranslationUnitDecl());
Douglas Gregor3747ee72010-10-01 01:18:02 +00003159
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003160 // Make sure we load the declaration update records for the translation unit,
3161 // if there are any.
Douglas Gregor35942772011-09-09 21:34:22 +00003162 loadDeclUpdateRecords(PREDEF_DECL_TRANSLATION_UNIT_ID,
3163 Context.getTranslationUnitDecl());
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003164
Douglas Gregor5f957282011-08-11 22:18:49 +00003165 // FIXME: Find a better way to deal with collisions between these
3166 // built-in types. Right now, we just ignore the problem.
3167
3168 // Load the special types.
Douglas Gregora6ea10e2012-01-17 18:09:05 +00003169 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
Douglas Gregor02a5e872011-09-10 00:30:18 +00003170 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
3171 if (!Context.CFConstantStringTypeDecl)
3172 Context.setCFConstantStringType(GetType(String));
3173 }
3174
3175 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
3176 QualType FileType = GetType(File);
3177 if (FileType.isNull()) {
3178 Error("FILE type is NULL");
3179 return;
3180 }
3181
3182 if (!Context.FILEDecl) {
3183 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
3184 Context.setFILEDecl(Typedef->getDecl());
3185 else {
3186 const TagType *Tag = FileType->getAs<TagType>();
3187 if (!Tag) {
3188 Error("Invalid FILE type in AST file");
3189 return;
3190 }
3191 Context.setFILEDecl(Tag->getDecl());
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00003192 }
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00003193 }
Douglas Gregorc29f77b2009-07-07 16:35:42 +00003194 }
Douglas Gregor5f957282011-08-11 22:18:49 +00003195
Douglas Gregor72cd7a02011-11-11 19:13:12 +00003196 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
Douglas Gregor02a5e872011-09-10 00:30:18 +00003197 QualType Jmp_bufType = GetType(Jmp_buf);
3198 if (Jmp_bufType.isNull()) {
3199 Error("jmp_buf type is NULL");
3200 return;
3201 }
3202
3203 if (!Context.jmp_bufDecl) {
3204 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
3205 Context.setjmp_bufDecl(Typedef->getDecl());
3206 else {
3207 const TagType *Tag = Jmp_bufType->getAs<TagType>();
3208 if (!Tag) {
3209 Error("Invalid jmp_buf type in AST file");
3210 return;
3211 }
3212 Context.setjmp_bufDecl(Tag->getDecl());
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00003213 }
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00003214 }
Mike Stump782fa302009-07-28 02:25:19 +00003215 }
Douglas Gregor02a5e872011-09-10 00:30:18 +00003216
Douglas Gregor72cd7a02011-11-11 19:13:12 +00003217 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
Douglas Gregor02a5e872011-09-10 00:30:18 +00003218 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
3219 if (Sigjmp_bufType.isNull()) {
3220 Error("sigjmp_buf type is NULL");
3221 return;
3222 }
3223
3224 if (!Context.sigjmp_bufDecl) {
3225 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
3226 Context.setsigjmp_bufDecl(Typedef->getDecl());
3227 else {
3228 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
3229 assert(Tag && "Invalid sigjmp_buf type in AST file");
3230 Context.setsigjmp_bufDecl(Tag->getDecl());
3231 }
3232 }
3233 }
3234
3235 if (unsigned ObjCIdRedef
3236 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
3237 if (Context.ObjCIdRedefinitionType.isNull())
3238 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
3239 }
3240
3241 if (unsigned ObjCClassRedef
3242 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
3243 if (Context.ObjCClassRedefinitionType.isNull())
3244 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
3245 }
3246
3247 if (unsigned ObjCSelRedef
3248 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
3249 if (Context.ObjCSelRedefinitionType.isNull())
3250 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
3251 }
Rafael Espindolae2d4f4e2011-11-13 21:51:09 +00003252
3253 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
3254 QualType Ucontext_tType = GetType(Ucontext_t);
3255 if (Ucontext_tType.isNull()) {
3256 Error("ucontext_t type is NULL");
3257 return;
3258 }
3259
3260 if (!Context.ucontext_tDecl) {
3261 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
3262 Context.setucontext_tDecl(Typedef->getDecl());
3263 else {
3264 const TagType *Tag = Ucontext_tType->getAs<TagType>();
3265 assert(Tag && "Invalid ucontext_t type in AST file");
3266 Context.setucontext_tDecl(Tag->getDecl());
3267 }
3268 }
3269 }
Douglas Gregor5f957282011-08-11 22:18:49 +00003270 }
3271
Douglas Gregor35942772011-09-09 21:34:22 +00003272 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003273
3274 // If there were any CUDA special declarations, deserialize them.
3275 if (!CUDASpecialDeclRefs.empty()) {
3276 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
Douglas Gregor35942772011-09-09 21:34:22 +00003277 Context.setcudaConfigureCallDecl(
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003278 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
3279 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00003280
3281 // Re-export any modules that were imported by a non-module AST file.
3282 for (unsigned I = 0, N = ImportedModules.size(); I != N; ++I) {
3283 if (Module *Imported = getSubmodule(ImportedModules[I]))
3284 makeModuleVisible(Imported, Module::AllVisible);
3285 }
3286 ImportedModules.clear();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003287}
3288
Douglas Gregorecc2c092011-12-01 22:20:10 +00003289void ASTReader::finalizeForWriting() {
3290 for (HiddenNamesMapType::iterator Hidden = HiddenNamesMap.begin(),
3291 HiddenEnd = HiddenNamesMap.end();
3292 Hidden != HiddenEnd; ++Hidden) {
3293 makeNamesVisible(Hidden->second);
3294 }
3295 HiddenNamesMap.clear();
3296}
3297
Douglas Gregorb64c1932009-05-12 01:31:05 +00003298/// \brief Retrieve the name of the original source file name
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003299/// directly from the AST file, without actually loading the AST
Douglas Gregorb64c1932009-05-12 01:31:05 +00003300/// file.
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003301std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +00003302 FileManager &FileMgr,
David Blaikied6471f72011-09-25 23:23:43 +00003303 DiagnosticsEngine &Diags) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003304 // Open the AST file.
Douglas Gregorb64c1932009-05-12 01:31:05 +00003305 std::string ErrStr;
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00003306 OwningPtr<llvm::MemoryBuffer> Buffer;
Chris Lattner39b49bc2010-11-23 08:35:12 +00003307 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
Douglas Gregorb64c1932009-05-12 01:31:05 +00003308 if (!Buffer) {
Kaelyn Uhrainda01f622012-06-20 00:36:03 +00003309 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ASTFileName << ErrStr;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003310 return std::string();
3311 }
3312
3313 // Initialize the stream
3314 llvm::BitstreamReader StreamFile;
3315 llvm::BitstreamCursor Stream;
Mike Stump1eb44332009-09-09 15:08:12 +00003316 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregorb64c1932009-05-12 01:31:05 +00003317 (const unsigned char *)Buffer->getBufferEnd());
3318 Stream.init(StreamFile);
3319
3320 // Sniff for the signature.
3321 if (Stream.Read(8) != 'C' ||
3322 Stream.Read(8) != 'P' ||
3323 Stream.Read(8) != 'C' ||
3324 Stream.Read(8) != 'H') {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003325 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003326 return std::string();
3327 }
3328
3329 RecordData Record;
3330 while (!Stream.AtEndOfStream()) {
3331 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00003332
Douglas Gregorb64c1932009-05-12 01:31:05 +00003333 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
3334 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump1eb44332009-09-09 15:08:12 +00003335
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003336 // We only know the AST subblock ID.
Douglas Gregorb64c1932009-05-12 01:31:05 +00003337 switch (BlockID) {
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003338 case CONTROL_BLOCK_ID:
3339 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003340 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003341 return std::string();
3342 }
3343 break;
Mike Stump1eb44332009-09-09 15:08:12 +00003344
Douglas Gregorb64c1932009-05-12 01:31:05 +00003345 default:
3346 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003347 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003348 return std::string();
3349 }
3350 break;
3351 }
3352 continue;
3353 }
3354
3355 if (Code == llvm::bitc::END_BLOCK) {
3356 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003357 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003358 return std::string();
3359 }
3360 continue;
3361 }
3362
3363 if (Code == llvm::bitc::DEFINE_ABBREV) {
3364 Stream.ReadAbbrevRecord();
3365 continue;
3366 }
3367
3368 Record.clear();
3369 const char *BlobStart = 0;
3370 unsigned BlobLen = 0;
Douglas Gregor39c497b2012-10-18 18:36:53 +00003371 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen) == ORIGINAL_FILE)
Douglas Gregorb64c1932009-05-12 01:31:05 +00003372 return std::string(BlobStart, BlobLen);
Mike Stump1eb44332009-09-09 15:08:12 +00003373 }
Douglas Gregorb64c1932009-05-12 01:31:05 +00003374
3375 return std::string();
3376}
3377
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00003378namespace {
3379 class SimplePCHValidator : public ASTReaderListener {
3380 const LangOptions &ExistingLangOpts;
3381 const TargetOptions &ExistingTargetOpts;
3382
3383 public:
3384 SimplePCHValidator(const LangOptions &ExistingLangOpts,
3385 const TargetOptions &ExistingTargetOpts)
3386 : ExistingLangOpts(ExistingLangOpts),
3387 ExistingTargetOpts(ExistingTargetOpts)
3388 {
3389 }
3390
3391 virtual bool ReadLanguageOptions(const LangOptions &LangOpts,
3392 bool Complain) {
3393 return checkLanguageOptions(ExistingLangOpts, LangOpts, 0);
3394 }
3395 virtual bool ReadTargetOptions(const TargetOptions &TargetOpts,
3396 bool Complain) {
3397 return checkTargetOptions(ExistingTargetOpts, TargetOpts, 0);
3398 }
3399 };
3400}
3401
3402bool ASTReader::isAcceptableASTFile(StringRef Filename,
3403 FileManager &FileMgr,
3404 const LangOptions &LangOpts,
3405 const TargetOptions &TargetOpts) {
3406 // Open the AST file.
3407 std::string ErrStr;
3408 OwningPtr<llvm::MemoryBuffer> Buffer;
3409 Buffer.reset(FileMgr.getBufferForFile(Filename, &ErrStr));
3410 if (!Buffer) {
3411 return false;
3412 }
3413
3414 // Initialize the stream
3415 llvm::BitstreamReader StreamFile;
3416 llvm::BitstreamCursor Stream;
3417 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
3418 (const unsigned char *)Buffer->getBufferEnd());
3419 Stream.init(StreamFile);
3420
3421 // Sniff for the signature.
3422 if (Stream.Read(8) != 'C' ||
3423 Stream.Read(8) != 'P' ||
3424 Stream.Read(8) != 'C' ||
3425 Stream.Read(8) != 'H') {
3426 return false;
3427 }
3428
3429 SimplePCHValidator Validator(LangOpts, TargetOpts);
3430 RecordData Record;
3431 bool InControlBlock = false;
3432 while (!Stream.AtEndOfStream()) {
3433 unsigned Code = Stream.ReadCode();
3434
3435 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
3436 unsigned BlockID = Stream.ReadSubBlockID();
3437
3438 // We only know the control subblock ID.
3439 switch (BlockID) {
3440 case CONTROL_BLOCK_ID:
3441 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
3442 return false;
3443 } else {
3444 InControlBlock = true;
3445 }
3446 break;
3447
3448 default:
3449 if (Stream.SkipBlock())
3450 return false;
3451 break;
3452 }
3453 continue;
3454 }
3455
3456 if (Code == llvm::bitc::END_BLOCK) {
3457 if (Stream.ReadBlockEnd()) {
3458 return false;
3459 }
3460 InControlBlock = false;
3461 continue;
3462 }
3463
3464 if (Code == llvm::bitc::DEFINE_ABBREV) {
3465 Stream.ReadAbbrevRecord();
3466 continue;
3467 }
3468
3469 Record.clear();
3470 const char *BlobStart = 0;
3471 unsigned BlobLen = 0;
3472 unsigned RecCode = Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen);
3473 if (InControlBlock) {
3474 switch ((ControlRecordTypes)RecCode) {
3475 case METADATA: {
3476 if (Record[0] != VERSION_MAJOR) {
3477 return false;
3478 }
3479
3480 const std::string &CurBranch = getClangFullRepositoryVersion();
3481 StringRef ASTBranch(BlobStart, BlobLen);
3482 if (StringRef(CurBranch) != ASTBranch)
3483 return false;
3484
3485 break;
3486 }
3487 case LANGUAGE_OPTIONS:
3488 if (ParseLanguageOptions(Record, false, Validator))
3489 return false;
3490 break;
3491
3492 case TARGET_OPTIONS:
3493 if (ParseTargetOptions(Record, false, Validator))
3494 return false;
3495 break;
3496
Douglas Gregor5f3d8222012-10-24 15:17:15 +00003497 case DIAGNOSTIC_OPTIONS:
3498 if (ParseDiagnosticOptions(Record, false, Validator))
3499 return false;
3500 break;
3501
Douglas Gregorbbf38312012-10-24 16:50:34 +00003502 case FILE_SYSTEM_OPTIONS:
3503 if (ParseFileSystemOptions(Record, false, Validator))
3504 return false;
3505 break;
3506
3507 case HEADER_SEARCH_OPTIONS:
3508 if (ParseHeaderSearchOptions(Record, false, Validator))
3509 return false;
3510 break;
3511
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00003512 default:
3513 // No other validation to perform.
3514 break;
3515 }
3516 }
3517 }
3518
3519 return true;
3520}
3521
Douglas Gregor4825fd72012-10-22 22:50:17 +00003522bool ASTReader::ReadSubmoduleBlock(ModuleFile &F) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003523 // Enter the submodule block.
3524 if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
3525 Error("malformed submodule block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003526 return true;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003527 }
3528
3529 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
Douglas Gregor26ced122011-12-01 00:59:36 +00003530 bool First = true;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003531 Module *CurrentModule = 0;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003532 RecordData Record;
3533 while (true) {
3534 unsigned Code = F.Stream.ReadCode();
3535 if (Code == llvm::bitc::END_BLOCK) {
3536 if (F.Stream.ReadBlockEnd()) {
3537 Error("error at end of submodule block in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003538 return true;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003539 }
Douglas Gregor4825fd72012-10-22 22:50:17 +00003540 return false;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003541 }
3542
3543 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
3544 // No known subblocks, always skip them.
3545 F.Stream.ReadSubBlockID();
3546 if (F.Stream.SkipBlock()) {
3547 Error("malformed block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003548 return true;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003549 }
3550 continue;
3551 }
3552
3553 if (Code == llvm::bitc::DEFINE_ABBREV) {
3554 F.Stream.ReadAbbrevRecord();
3555 continue;
3556 }
3557
3558 // Read a record.
3559 const char *BlobStart;
3560 unsigned BlobLen;
3561 Record.clear();
3562 switch (F.Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
3563 default: // Default behavior: ignore.
3564 break;
3565
3566 case SUBMODULE_DEFINITION: {
Douglas Gregor26ced122011-12-01 00:59:36 +00003567 if (First) {
3568 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003569 return true;
Douglas Gregor26ced122011-12-01 00:59:36 +00003570 }
3571
Douglas Gregore209e502011-12-06 01:10:29 +00003572 if (Record.size() < 7) {
Douglas Gregor1e123682011-12-05 22:27:44 +00003573 Error("malformed module definition");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003574 return true;
Douglas Gregor1e123682011-12-05 22:27:44 +00003575 }
3576
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003577 StringRef Name(BlobStart, BlobLen);
Douglas Gregore209e502011-12-06 01:10:29 +00003578 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[0]);
3579 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[1]);
3580 bool IsFramework = Record[2];
3581 bool IsExplicit = Record[3];
Douglas Gregora1f1fad2012-01-27 19:52:33 +00003582 bool IsSystem = Record[4];
3583 bool InferSubmodules = Record[5];
3584 bool InferExplicitSubmodules = Record[6];
3585 bool InferExportWildcard = Record[7];
Douglas Gregor1e123682011-12-05 22:27:44 +00003586
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003587 Module *ParentModule = 0;
Douglas Gregor26ced122011-12-01 00:59:36 +00003588 if (Parent)
3589 ParentModule = getSubmodule(Parent);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003590
3591 // Retrieve this (sub)module from the module map, creating it if
3592 // necessary.
3593 CurrentModule = ModMap.findOrCreateModule(Name, ParentModule,
3594 IsFramework,
3595 IsExplicit).first;
Douglas Gregore209e502011-12-06 01:10:29 +00003596 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
3597 if (GlobalIndex >= SubmodulesLoaded.size() ||
3598 SubmodulesLoaded[GlobalIndex]) {
Douglas Gregor26ced122011-12-01 00:59:36 +00003599 Error("too many submodules");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003600 return true;
Douglas Gregor26ced122011-12-01 00:59:36 +00003601 }
Douglas Gregora015cab2011-12-02 17:30:13 +00003602
Argyrios Kyrtzidisd64c26f2012-10-03 01:58:42 +00003603 CurrentModule->setASTFile(F.File);
Douglas Gregor305dc3e2011-12-20 00:28:52 +00003604 CurrentModule->IsFromModuleFile = true;
Douglas Gregora1f1fad2012-01-27 19:52:33 +00003605 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
Douglas Gregor1e123682011-12-05 22:27:44 +00003606 CurrentModule->InferSubmodules = InferSubmodules;
3607 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
3608 CurrentModule->InferExportWildcard = InferExportWildcard;
Douglas Gregora015cab2011-12-02 17:30:13 +00003609 if (DeserializationListener)
Douglas Gregore209e502011-12-06 01:10:29 +00003610 DeserializationListener->ModuleRead(GlobalID, CurrentModule);
Douglas Gregora015cab2011-12-02 17:30:13 +00003611
Douglas Gregore209e502011-12-06 01:10:29 +00003612 SubmodulesLoaded[GlobalIndex] = CurrentModule;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003613 break;
3614 }
3615
Douglas Gregor77d029f2011-12-08 19:11:24 +00003616 case SUBMODULE_UMBRELLA_HEADER: {
Douglas Gregor26ced122011-12-01 00:59:36 +00003617 if (First) {
3618 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003619 return true;
Douglas Gregor26ced122011-12-01 00:59:36 +00003620 }
3621
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003622 if (!CurrentModule)
3623 break;
3624
3625 StringRef FileName(BlobStart, BlobLen);
3626 if (const FileEntry *Umbrella = PP.getFileManager().getFile(FileName)) {
Douglas Gregor10694ce2011-12-08 17:39:04 +00003627 if (!CurrentModule->getUmbrellaHeader())
Douglas Gregore209e502011-12-06 01:10:29 +00003628 ModMap.setUmbrellaHeader(CurrentModule, Umbrella);
Douglas Gregor10694ce2011-12-08 17:39:04 +00003629 else if (CurrentModule->getUmbrellaHeader() != Umbrella) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003630 Error("mismatched umbrella headers in submodule");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003631 return true;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003632 }
3633 }
3634 break;
3635 }
3636
3637 case SUBMODULE_HEADER: {
Douglas Gregor26ced122011-12-01 00:59:36 +00003638 if (First) {
3639 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003640 return true;
Douglas Gregor26ced122011-12-01 00:59:36 +00003641 }
3642
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003643 if (!CurrentModule)
3644 break;
3645
3646 // FIXME: Be more lazy about this!
3647 StringRef FileName(BlobStart, BlobLen);
3648 if (const FileEntry *File = PP.getFileManager().getFile(FileName)) {
3649 if (std::find(CurrentModule->Headers.begin(),
3650 CurrentModule->Headers.end(),
3651 File) == CurrentModule->Headers.end())
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00003652 ModMap.addHeader(CurrentModule, File, false);
3653 }
3654 break;
3655 }
3656
3657 case SUBMODULE_EXCLUDED_HEADER: {
3658 if (First) {
3659 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003660 return true;
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00003661 }
3662
3663 if (!CurrentModule)
3664 break;
3665
3666 // FIXME: Be more lazy about this!
3667 StringRef FileName(BlobStart, BlobLen);
3668 if (const FileEntry *File = PP.getFileManager().getFile(FileName)) {
3669 if (std::find(CurrentModule->Headers.begin(),
3670 CurrentModule->Headers.end(),
3671 File) == CurrentModule->Headers.end())
3672 ModMap.addHeader(CurrentModule, File, true);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003673 }
3674 break;
3675 }
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00003676
3677 case SUBMODULE_TOPHEADER: {
3678 if (First) {
3679 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003680 return true;
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00003681 }
3682
3683 if (!CurrentModule)
3684 break;
3685
3686 // FIXME: Be more lazy about this!
3687 StringRef FileName(BlobStart, BlobLen);
3688 if (const FileEntry *File = PP.getFileManager().getFile(FileName))
3689 CurrentModule->TopHeaders.insert(File);
3690 break;
3691 }
3692
Douglas Gregor77d029f2011-12-08 19:11:24 +00003693 case SUBMODULE_UMBRELLA_DIR: {
3694 if (First) {
3695 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003696 return true;
Douglas Gregor77d029f2011-12-08 19:11:24 +00003697 }
3698
3699 if (!CurrentModule)
3700 break;
3701
3702 StringRef DirName(BlobStart, BlobLen);
3703 if (const DirectoryEntry *Umbrella
3704 = PP.getFileManager().getDirectory(DirName)) {
3705 if (!CurrentModule->getUmbrellaDir())
3706 ModMap.setUmbrellaDir(CurrentModule, Umbrella);
3707 else if (CurrentModule->getUmbrellaDir() != Umbrella) {
3708 Error("mismatched umbrella directories in submodule");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003709 return true;
Douglas Gregor77d029f2011-12-08 19:11:24 +00003710 }
3711 }
3712 break;
3713 }
3714
Douglas Gregor26ced122011-12-01 00:59:36 +00003715 case SUBMODULE_METADATA: {
3716 if (!First) {
3717 Error("submodule metadata record not at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003718 return true;
Douglas Gregor26ced122011-12-01 00:59:36 +00003719 }
3720 First = false;
3721
3722 F.BaseSubmoduleID = getTotalNumSubmodules();
Douglas Gregor26ced122011-12-01 00:59:36 +00003723 F.LocalNumSubmodules = Record[0];
3724 unsigned LocalBaseSubmoduleID = Record[1];
3725 if (F.LocalNumSubmodules > 0) {
3726 // Introduce the global -> local mapping for submodules within this
3727 // module.
3728 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
3729
3730 // Introduce the local -> global mapping for submodules within this
3731 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00003732 F.SubmoduleRemap.insertOrReplace(
Douglas Gregor26ced122011-12-01 00:59:36 +00003733 std::make_pair(LocalBaseSubmoduleID,
3734 F.BaseSubmoduleID - LocalBaseSubmoduleID));
3735
3736 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
3737 }
3738 break;
3739 }
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003740
Douglas Gregor55988682011-12-05 16:33:54 +00003741 case SUBMODULE_IMPORTS: {
3742 if (First) {
3743 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003744 return true;
Douglas Gregor55988682011-12-05 16:33:54 +00003745 }
3746
3747 if (!CurrentModule)
3748 break;
3749
3750 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
3751 UnresolvedModuleImportExport Unresolved;
3752 Unresolved.File = &F;
3753 Unresolved.Mod = CurrentModule;
3754 Unresolved.ID = Record[Idx];
3755 Unresolved.IsImport = true;
3756 Unresolved.IsWildcard = false;
3757 UnresolvedModuleImportExports.push_back(Unresolved);
3758 }
3759 break;
3760 }
3761
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003762 case SUBMODULE_EXPORTS: {
3763 if (First) {
3764 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003765 return true;
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003766 }
3767
3768 if (!CurrentModule)
3769 break;
3770
3771 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
Douglas Gregor55988682011-12-05 16:33:54 +00003772 UnresolvedModuleImportExport Unresolved;
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003773 Unresolved.File = &F;
Douglas Gregor55988682011-12-05 16:33:54 +00003774 Unresolved.Mod = CurrentModule;
3775 Unresolved.ID = Record[Idx];
3776 Unresolved.IsImport = false;
3777 Unresolved.IsWildcard = Record[Idx + 1];
3778 UnresolvedModuleImportExports.push_back(Unresolved);
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003779 }
3780
3781 // Once we've loaded the set of exports, there's no reason to keep
3782 // the parsed, unresolved exports around.
3783 CurrentModule->UnresolvedExports.clear();
3784 break;
3785 }
Douglas Gregor51f564f2011-12-31 04:05:44 +00003786 case SUBMODULE_REQUIRES: {
3787 if (First) {
3788 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003789 return true;
Douglas Gregor51f564f2011-12-31 04:05:44 +00003790 }
3791
3792 if (!CurrentModule)
3793 break;
3794
3795 CurrentModule->addRequirement(StringRef(BlobStart, BlobLen),
David Blaikie4e4d0842012-03-11 07:00:24 +00003796 Context.getLangOpts(),
Douglas Gregordc58aa72012-01-30 06:01:29 +00003797 Context.getTargetInfo());
Douglas Gregor51f564f2011-12-31 04:05:44 +00003798 break;
3799 }
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003800 }
3801 }
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003802}
3803
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003804/// \brief Parse the record that corresponds to a LangOptions data
3805/// structure.
3806///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003807/// This routine parses the language options from the AST file and then gives
3808/// them to the AST listener if one is set.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003809///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003810/// \returns true if the listener deems the file unacceptable, false otherwise.
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00003811bool ASTReader::ParseLanguageOptions(const RecordData &Record,
3812 bool Complain,
3813 ASTReaderListener &Listener) {
3814 LangOptions LangOpts;
3815 unsigned Idx = 0;
Douglas Gregor7d5e81b2011-09-13 18:26:39 +00003816#define LANGOPT(Name, Bits, Default, Description) \
3817 LangOpts.Name = Record[Idx++];
3818#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
3819 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
3820#include "clang/Basic/LangOptions.def"
John McCall260611a2012-06-20 06:18:46 +00003821
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00003822 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
3823 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
3824 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
3825
3826 unsigned Length = Record[Idx++];
3827 LangOpts.CurrentModule.assign(Record.begin() + Idx,
3828 Record.begin() + Idx + Length);
3829 return Listener.ReadLanguageOptions(LangOpts, Complain);
3830}
3831
3832bool ASTReader::ParseTargetOptions(const RecordData &Record,
3833 bool Complain,
3834 ASTReaderListener &Listener) {
3835 unsigned Idx = 0;
3836 TargetOptions TargetOpts;
3837 TargetOpts.Triple = ReadString(Record, Idx);
3838 TargetOpts.CPU = ReadString(Record, Idx);
3839 TargetOpts.ABI = ReadString(Record, Idx);
3840 TargetOpts.CXXABI = ReadString(Record, Idx);
3841 TargetOpts.LinkerVersion = ReadString(Record, Idx);
3842 for (unsigned N = Record[Idx++]; N; --N) {
3843 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
3844 }
3845 for (unsigned N = Record[Idx++]; N; --N) {
3846 TargetOpts.Features.push_back(ReadString(Record, Idx));
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003847 }
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003848
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00003849 return Listener.ReadTargetOptions(TargetOpts, Complain);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003850}
3851
Douglas Gregor5f3d8222012-10-24 15:17:15 +00003852bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
3853 ASTReaderListener &Listener) {
3854 DiagnosticOptions DiagOpts;
3855 unsigned Idx = 0;
3856#define DIAGOPT(Name, Bits, Default) DiagOpts.Name = Record[Idx++];
3857#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
3858 DiagOpts.set##Name(static_cast<Type>(Record[Idx++]));
3859#include "clang/Basic/DiagnosticOptions.def"
3860
3861 for (unsigned N = Record[Idx++]; N; --N) {
3862 DiagOpts.Warnings.push_back(ReadString(Record, Idx));
3863 }
3864
3865 return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
3866}
3867
Douglas Gregor1b2c3c02012-10-24 15:49:58 +00003868bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
3869 ASTReaderListener &Listener) {
3870 FileSystemOptions FSOpts;
3871 unsigned Idx = 0;
3872 FSOpts.WorkingDir = ReadString(Record, Idx);
3873 return Listener.ReadFileSystemOptions(FSOpts, Complain);
3874}
3875
Douglas Gregorbbf38312012-10-24 16:50:34 +00003876bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
3877 bool Complain,
3878 ASTReaderListener &Listener) {
3879 HeaderSearchOptions HSOpts;
3880 unsigned Idx = 0;
3881 HSOpts.Sysroot = ReadString(Record, Idx);
3882
3883 // Include entries.
3884 for (unsigned N = Record[Idx++]; N; --N) {
3885 std::string Path = ReadString(Record, Idx);
3886 frontend::IncludeDirGroup Group
3887 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
3888 bool IsUserSupplied = Record[Idx++];
3889 bool IsFramework = Record[Idx++];
3890 bool IgnoreSysRoot = Record[Idx++];
3891 bool IsInternal = Record[Idx++];
3892 bool ImplicitExternC = Record[Idx++];
3893 HSOpts.UserEntries.push_back(
3894 HeaderSearchOptions::Entry(Path, Group, IsUserSupplied, IsFramework,
3895 IgnoreSysRoot, IsInternal, ImplicitExternC));
3896 }
3897
3898 // System header prefixes.
3899 for (unsigned N = Record[Idx++]; N; --N) {
3900 std::string Prefix = ReadString(Record, Idx);
3901 bool IsSystemHeader = Record[Idx++];
3902 HSOpts.SystemHeaderPrefixes.push_back(
3903 HeaderSearchOptions::SystemHeaderPrefix(Prefix, IsSystemHeader));
3904 }
3905
3906 HSOpts.ResourceDir = ReadString(Record, Idx);
3907 HSOpts.ModuleCachePath = ReadString(Record, Idx);
3908 HSOpts.DisableModuleHash = Record[Idx++];
3909 HSOpts.UseBuiltinIncludes = Record[Idx++];
3910 HSOpts.UseStandardSystemIncludes = Record[Idx++];
3911 HSOpts.UseStandardCXXIncludes = Record[Idx++];
3912 HSOpts.UseLibcxx = Record[Idx++];
3913
3914 return Listener.ReadHeaderSearchOptions(HSOpts, Complain);
3915}
3916
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003917std::pair<ModuleFile *, unsigned>
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003918ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00003919 GlobalPreprocessedEntityMapType::iterator
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003920 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00003921 assert(I != GlobalPreprocessedEntityMap.end() &&
3922 "Corrupted global preprocessed entity map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003923 ModuleFile *M = I->second;
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003924 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
3925 return std::make_pair(M, LocalIndex);
3926}
3927
Argyrios Kyrtzidis632dcc92012-10-02 16:10:51 +00003928std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
3929ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
3930 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
3931 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
3932 Mod.NumPreprocessedEntities);
3933
3934 return std::make_pair(PreprocessingRecord::iterator(),
3935 PreprocessingRecord::iterator());
3936}
3937
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00003938std::pair<ASTReader::ModuleDeclIterator, ASTReader::ModuleDeclIterator>
3939ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
3940 return std::make_pair(ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
3941 ModuleDeclIterator(this, &Mod,
3942 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
3943}
3944
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003945PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
3946 PreprocessedEntityID PPID = Index+1;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003947 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
3948 ModuleFile &M = *PPInfo.first;
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003949 unsigned LocalIndex = PPInfo.second;
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003950 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
Douglas Gregor4800a5c2011-02-08 21:58:10 +00003951
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00003952 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003953 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003954
3955 unsigned Code = M.PreprocessorDetailCursor.ReadCode();
3956 switch (Code) {
3957 case llvm::bitc::END_BLOCK:
3958 return 0;
3959
3960 case llvm::bitc::ENTER_SUBBLOCK:
3961 Error("unexpected subblock record in preprocessor detail block");
3962 return 0;
3963
3964 case llvm::bitc::DEFINE_ABBREV:
3965 Error("unexpected abbrevation record in preprocessor detail block");
3966 return 0;
3967
3968 default:
3969 break;
3970 }
3971
3972 if (!PP.getPreprocessingRecord()) {
3973 Error("no preprocessing record");
3974 return 0;
3975 }
3976
3977 // Read the record.
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003978 SourceRange Range(ReadSourceLocation(M, PPOffs.Begin),
3979 ReadSourceLocation(M, PPOffs.End));
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003980 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
3981 const char *BlobStart = 0;
3982 unsigned BlobLen = 0;
3983 RecordData Record;
3984 PreprocessorDetailRecordTypes RecType =
3985 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.ReadRecord(
3986 Code, Record, BlobStart, BlobLen);
3987 switch (RecType) {
3988 case PPD_MACRO_EXPANSION: {
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003989 bool isBuiltin = Record[0];
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003990 IdentifierInfo *Name = 0;
3991 MacroDefinition *Def = 0;
3992 if (isBuiltin)
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003993 Name = getLocalIdentifier(M, Record[1]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003994 else {
3995 PreprocessedEntityID
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003996 GlobalID = getGlobalPreprocessedEntityID(M, Record[1]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003997 Def =cast<MacroDefinition>(PPRec.getLoadedPreprocessedEntity(GlobalID-1));
3998 }
3999
4000 MacroExpansion *ME;
4001 if (isBuiltin)
4002 ME = new (PPRec) MacroExpansion(Name, Range);
4003 else
4004 ME = new (PPRec) MacroExpansion(Def, Range);
4005
4006 return ME;
4007 }
4008
4009 case PPD_MACRO_DEFINITION: {
4010 // Decode the identifier info and then check again; if the macro is
4011 // still defined and associated with the identifier,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00004012 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00004013 MacroDefinition *MD
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00004014 = new (PPRec) MacroDefinition(II, Range);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00004015
4016 if (DeserializationListener)
4017 DeserializationListener->MacroDefinitionRead(PPID, MD);
4018
4019 return MD;
4020 }
4021
4022 case PPD_INCLUSION_DIRECTIVE: {
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00004023 const char *FullFileNameStart = BlobStart + Record[0];
Argyrios Kyrtzidis29f98b42012-03-08 01:08:28 +00004024 StringRef FullFileName(FullFileNameStart, BlobLen - Record[0]);
4025 const FileEntry *File = 0;
4026 if (!FullFileName.empty())
4027 File = PP.getFileManager().getFile(FullFileName);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00004028
4029 // FIXME: Stable encoding
4030 InclusionDirective::InclusionKind Kind
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00004031 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00004032 InclusionDirective *ID
4033 = new (PPRec) InclusionDirective(PPRec, Kind,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00004034 StringRef(BlobStart, Record[0]),
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00004035 Record[1], Record[3],
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00004036 File,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00004037 Range);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00004038 return ID;
4039 }
4040 }
David Blaikie7530c032012-01-17 06:56:22 +00004041
4042 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00004043}
4044
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00004045/// \brief \arg SLocMapI points at a chunk of a module that contains no
4046/// preprocessed entities or the entities it contains are not the ones we are
4047/// looking for. Find the next module that contains entities and return the ID
4048/// of the first entry.
4049PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
4050 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
4051 ++SLocMapI;
4052 for (GlobalSLocOffsetMapType::const_iterator
4053 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004054 ModuleFile &M = *SLocMapI->second;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00004055 if (M.NumPreprocessedEntities)
4056 return getGlobalPreprocessedEntityID(M, M.BasePreprocessedEntityID);
4057 }
4058
4059 return getTotalNumPreprocessedEntities();
4060}
4061
4062namespace {
4063
4064template <unsigned PPEntityOffset::*PPLoc>
4065struct PPEntityComp {
4066 const ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004067 ModuleFile &M;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00004068
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004069 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) { }
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00004070
Benjamin Kramer88df1252011-09-21 06:42:26 +00004071 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
4072 SourceLocation LHS = getLoc(L);
4073 SourceLocation RHS = getLoc(R);
4074 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4075 }
4076
4077 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00004078 SourceLocation LHS = getLoc(L);
4079 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4080 }
4081
Benjamin Kramer88df1252011-09-21 06:42:26 +00004082 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00004083 SourceLocation RHS = getLoc(R);
4084 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4085 }
4086
4087 SourceLocation getLoc(const PPEntityOffset &PPE) const {
4088 return Reader.ReadSourceLocation(M, PPE.*PPLoc);
4089 }
4090};
4091
4092}
4093
4094/// \brief Returns the first preprocessed entity ID that ends after \arg BLoc.
4095PreprocessedEntityID
4096ASTReader::findBeginPreprocessedEntity(SourceLocation BLoc) const {
4097 if (SourceMgr.isLocalSourceLocation(BLoc))
4098 return getTotalNumPreprocessedEntities();
4099
4100 GlobalSLocOffsetMapType::const_iterator
4101 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
4102 BLoc.getOffset());
4103 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
4104 "Corrupted global sloc offset map");
4105
4106 if (SLocMapI->second->NumPreprocessedEntities == 0)
4107 return findNextPreprocessedEntity(SLocMapI);
4108
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004109 ModuleFile &M = *SLocMapI->second;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00004110 typedef const PPEntityOffset *pp_iterator;
4111 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
4112 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
Argyrios Kyrtzidis4cd06342011-09-22 21:17:02 +00004113
4114 size_t Count = M.NumPreprocessedEntities;
4115 size_t Half;
4116 pp_iterator First = pp_begin;
4117 pp_iterator PPI;
4118
4119 // Do a binary search manually instead of using std::lower_bound because
4120 // The end locations of entities may be unordered (when a macro expansion
4121 // is inside another macro argument), but for this case it is not important
4122 // whether we get the first macro expansion or its containing macro.
4123 while (Count > 0) {
4124 Half = Count/2;
4125 PPI = First;
4126 std::advance(PPI, Half);
4127 if (SourceMgr.isBeforeInTranslationUnit(ReadSourceLocation(M, PPI->End),
4128 BLoc)){
4129 First = PPI;
4130 ++First;
4131 Count = Count - Half - 1;
4132 } else
4133 Count = Half;
4134 }
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00004135
4136 if (PPI == pp_end)
4137 return findNextPreprocessedEntity(SLocMapI);
4138
4139 return getGlobalPreprocessedEntityID(M,
4140 M.BasePreprocessedEntityID + (PPI - pp_begin));
4141}
4142
4143/// \brief Returns the first preprocessed entity ID that begins after \arg ELoc.
4144PreprocessedEntityID
4145ASTReader::findEndPreprocessedEntity(SourceLocation ELoc) const {
4146 if (SourceMgr.isLocalSourceLocation(ELoc))
4147 return getTotalNumPreprocessedEntities();
4148
4149 GlobalSLocOffsetMapType::const_iterator
4150 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
4151 ELoc.getOffset());
4152 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
4153 "Corrupted global sloc offset map");
4154
4155 if (SLocMapI->second->NumPreprocessedEntities == 0)
4156 return findNextPreprocessedEntity(SLocMapI);
4157
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004158 ModuleFile &M = *SLocMapI->second;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00004159 typedef const PPEntityOffset *pp_iterator;
4160 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
4161 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
4162 pp_iterator PPI =
4163 std::upper_bound(pp_begin, pp_end, ELoc,
4164 PPEntityComp<&PPEntityOffset::Begin>(*this, M));
4165
4166 if (PPI == pp_end)
4167 return findNextPreprocessedEntity(SLocMapI);
4168
4169 return getGlobalPreprocessedEntityID(M,
4170 M.BasePreprocessedEntityID + (PPI - pp_begin));
4171}
4172
4173/// \brief Returns a pair of [Begin, End) indices of preallocated
4174/// preprocessed entities that \arg Range encompasses.
4175std::pair<unsigned, unsigned>
4176 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
4177 if (Range.isInvalid())
4178 return std::make_pair(0,0);
4179 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
4180
4181 PreprocessedEntityID BeginID = findBeginPreprocessedEntity(Range.getBegin());
4182 PreprocessedEntityID EndID = findEndPreprocessedEntity(Range.getEnd());
4183 return std::make_pair(BeginID, EndID);
4184}
4185
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00004186/// \brief Optionally returns true or false if the preallocated preprocessed
4187/// entity with index \arg Index came from file \arg FID.
4188llvm::Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
4189 FileID FID) {
4190 if (FID.isInvalid())
4191 return false;
4192
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004193 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
4194 ModuleFile &M = *PPInfo.first;
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00004195 unsigned LocalIndex = PPInfo.second;
4196 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
4197
4198 SourceLocation Loc = ReadSourceLocation(M, PPOffs.Begin);
4199 if (Loc.isInvalid())
4200 return false;
4201
4202 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
4203 return true;
4204 else
4205 return false;
4206}
4207
Douglas Gregord10a3812011-08-25 18:14:34 +00004208namespace {
4209 /// \brief Visitor used to search for information about a header file.
4210 class HeaderFileInfoVisitor {
4211 ASTReader &Reader;
4212 const FileEntry *FE;
4213
4214 llvm::Optional<HeaderFileInfo> HFI;
4215
4216 public:
4217 HeaderFileInfoVisitor(ASTReader &Reader, const FileEntry *FE)
4218 : Reader(Reader), FE(FE) { }
4219
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004220 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregord10a3812011-08-25 18:14:34 +00004221 HeaderFileInfoVisitor *This
4222 = static_cast<HeaderFileInfoVisitor *>(UserData);
4223
4224 HeaderFileInfoTrait Trait(This->Reader, M,
4225 &This->Reader.getPreprocessor().getHeaderSearchInfo(),
4226 M.HeaderFileFrameworkStrings,
4227 This->FE->getName());
4228
4229 HeaderFileInfoLookupTable *Table
4230 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
4231 if (!Table)
4232 return false;
4233
4234 // Look in the on-disk hash table for an entry for this file name.
4235 HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE->getName(),
4236 &Trait);
4237 if (Pos == Table->end())
4238 return false;
4239
4240 This->HFI = *Pos;
4241 return true;
4242 }
4243
4244 llvm::Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
4245 };
4246}
4247
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00004248HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
Douglas Gregord10a3812011-08-25 18:14:34 +00004249 HeaderFileInfoVisitor Visitor(*this, FE);
4250 ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor);
4251 if (llvm::Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00004252 if (Listener)
Douglas Gregord10a3812011-08-25 18:14:34 +00004253 Listener->ReadHeaderFileInfo(*HFI, FE->getUID());
4254 return *HFI;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00004255 }
4256
4257 return HeaderFileInfo();
4258}
4259
David Blaikied6471f72011-09-25 23:23:43 +00004260void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00004261 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004262 ModuleFile &F = *(*I);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00004263 unsigned Idx = 0;
4264 while (Idx < F.PragmaDiagMappings.size()) {
4265 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
Argyrios Kyrtzidis87429a02011-11-09 01:24:17 +00004266 Diag.DiagStates.push_back(*Diag.GetCurDiagState());
4267 Diag.DiagStatePoints.push_back(
4268 DiagnosticsEngine::DiagStatePoint(&Diag.DiagStates.back(),
4269 FullSourceLoc(Loc, SourceMgr)));
Douglas Gregorf62d43d2011-07-19 16:10:42 +00004270 while (1) {
4271 assert(Idx < F.PragmaDiagMappings.size() &&
4272 "Invalid data, didn't find '-1' marking end of diag/map pairs");
4273 if (Idx >= F.PragmaDiagMappings.size()) {
4274 break; // Something is messed up but at least avoid infinite loop in
4275 // release build.
4276 }
4277 unsigned DiagID = F.PragmaDiagMappings[Idx++];
4278 if (DiagID == (unsigned)-1) {
4279 break; // no more diag/map pairs for this location.
4280 }
4281 diag::Mapping Map = (diag::Mapping)F.PragmaDiagMappings[Idx++];
Argyrios Kyrtzidis87429a02011-11-09 01:24:17 +00004282 DiagnosticMappingInfo MappingInfo = Diag.makeMappingInfo(Map, Loc);
4283 Diag.GetCurDiagState()->setMappingInfo(DiagID, MappingInfo);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00004284 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00004285 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00004286 }
4287}
4288
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00004289/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004290ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Douglas Gregora119da02011-08-02 16:26:37 +00004291 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
Jonathan D. Turnere9b76c12011-07-20 21:31:32 +00004292 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004293 ModuleFile *M = I->second;
Douglas Gregore3605012011-08-02 18:32:54 +00004294 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00004295}
4296
4297/// \brief Read and return the type with the given index..
Douglas Gregor2cf26342009-04-09 22:27:44 +00004298///
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00004299/// The index is the type ID, shifted and minus the number of predefs. This
4300/// routine actually reads the record corresponding to the type at the given
4301/// location. It is a helper routine for GetType, which deals with reading type
4302/// IDs.
Douglas Gregor393f2492011-07-22 00:38:23 +00004303QualType ASTReader::readTypeRecord(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00004304 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00004305 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00004306
Douglas Gregor0b748912009-04-14 21:18:50 +00004307 // Keep track of where we are in the stream, then jump back there
4308 // after reading this type.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004309 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregor0b748912009-04-14 21:18:50 +00004310
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00004311 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redl27372b42010-08-11 18:52:41 +00004312
Douglas Gregord89275b2009-07-06 18:54:52 +00004313 // Note that we are loading a type record.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004314 Deserializing AType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00004315
Douglas Gregor393f2492011-07-22 00:38:23 +00004316 unsigned Idx = 0;
Sebastian Redlc3632732010-10-05 15:59:54 +00004317 DeclsCursor.JumpToBit(Loc.Offset);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004318 RecordData Record;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004319 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004320 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
4321 case TYPE_EXT_QUAL: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004322 if (Record.size() != 2) {
4323 Error("Incorrect encoding of extended qualifier type");
4324 return QualType();
4325 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004326 QualType Base = readType(*Loc.F, Record, Idx);
4327 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
Douglas Gregor35942772011-09-09 21:34:22 +00004328 return Context.getQualifiedType(Base, Quals);
Douglas Gregor6d473962009-04-15 22:00:08 +00004329 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004330
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004331 case TYPE_COMPLEX: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004332 if (Record.size() != 1) {
4333 Error("Incorrect encoding of complex type");
4334 return QualType();
4335 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004336 QualType ElemType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004337 return Context.getComplexType(ElemType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004338 }
4339
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004340 case TYPE_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004341 if (Record.size() != 1) {
4342 Error("Incorrect encoding of pointer type");
4343 return QualType();
4344 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004345 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004346 return Context.getPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004347 }
4348
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004349 case TYPE_BLOCK_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004350 if (Record.size() != 1) {
4351 Error("Incorrect encoding of block pointer type");
4352 return QualType();
4353 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004354 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004355 return Context.getBlockPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004356 }
4357
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004358 case TYPE_LVALUE_REFERENCE: {
Richard Smithdf1550f2011-04-12 10:38:03 +00004359 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004360 Error("Incorrect encoding of lvalue reference type");
4361 return QualType();
4362 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004363 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004364 return Context.getLValueReferenceType(PointeeType, Record[1]);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004365 }
4366
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004367 case TYPE_RVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004368 if (Record.size() != 1) {
4369 Error("Incorrect encoding of rvalue reference type");
4370 return QualType();
4371 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004372 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004373 return Context.getRValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004374 }
4375
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004376 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidis240437b2010-07-02 11:55:15 +00004377 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004378 Error("Incorrect encoding of member pointer type");
4379 return QualType();
4380 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004381 QualType PointeeType = readType(*Loc.F, Record, Idx);
4382 QualType ClassType = readType(*Loc.F, Record, Idx);
Douglas Gregor1ab55e92010-12-10 17:03:06 +00004383 if (PointeeType.isNull() || ClassType.isNull())
4384 return QualType();
4385
Douglas Gregor35942772011-09-09 21:34:22 +00004386 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregor2cf26342009-04-09 22:27:44 +00004387 }
4388
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004389 case TYPE_CONSTANT_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00004390 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004391 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4392 unsigned IndexTypeQuals = Record[2];
4393 unsigned Idx = 3;
4394 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004395 return Context.getConstantArrayType(ElementType, Size,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004396 ASM, IndexTypeQuals);
4397 }
4398
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004399 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00004400 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004401 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4402 unsigned IndexTypeQuals = Record[2];
Douglas Gregor35942772011-09-09 21:34:22 +00004403 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004404 }
4405
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004406 case TYPE_VARIABLE_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00004407 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregor0b748912009-04-14 21:18:50 +00004408 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4409 unsigned IndexTypeQuals = Record[2];
Sebastian Redlc3632732010-10-05 15:59:54 +00004410 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
4411 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
Douglas Gregor35942772011-09-09 21:34:22 +00004412 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004413 ASM, IndexTypeQuals,
4414 SourceRange(LBLoc, RBLoc));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004415 }
4416
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004417 case TYPE_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00004418 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004419 Error("incorrect encoding of vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004420 return QualType();
4421 }
4422
Douglas Gregor393f2492011-07-22 00:38:23 +00004423 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004424 unsigned NumElements = Record[1];
Bob Wilsone86d78c2010-11-10 21:56:12 +00004425 unsigned VecKind = Record[2];
Douglas Gregor35942772011-09-09 21:34:22 +00004426 return Context.getVectorType(ElementType, NumElements,
Bob Wilsone86d78c2010-11-10 21:56:12 +00004427 (VectorType::VectorKind)VecKind);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004428 }
4429
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004430 case TYPE_EXT_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00004431 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004432 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004433 return QualType();
4434 }
4435
Douglas Gregor393f2492011-07-22 00:38:23 +00004436 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004437 unsigned NumElements = Record[1];
Douglas Gregor35942772011-09-09 21:34:22 +00004438 return Context.getExtVectorType(ElementType, NumElements);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004439 }
4440
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004441 case TYPE_FUNCTION_NO_PROTO: {
John McCallf85e1932011-06-15 23:02:42 +00004442 if (Record.size() != 6) {
Douglas Gregora02b1472009-04-28 21:53:25 +00004443 Error("incorrect encoding of no-proto function type");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004444 return QualType();
4445 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004446 QualType ResultType = readType(*Loc.F, Record, Idx);
John McCallf85e1932011-06-15 23:02:42 +00004447 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
4448 (CallingConv)Record[4], Record[5]);
Douglas Gregor35942772011-09-09 21:34:22 +00004449 return Context.getFunctionNoProtoType(ResultType, Info);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004450 }
4451
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004452 case TYPE_FUNCTION_PROTO: {
Douglas Gregor393f2492011-07-22 00:38:23 +00004453 QualType ResultType = readType(*Loc.F, Record, Idx);
John McCalle23cf432010-12-14 08:05:40 +00004454
4455 FunctionProtoType::ExtProtoInfo EPI;
4456 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
Eli Friedmana49218e2011-04-09 08:18:08 +00004457 /*hasregparm*/ Record[2],
4458 /*regparm*/ Record[3],
John McCallf85e1932011-06-15 23:02:42 +00004459 static_cast<CallingConv>(Record[4]),
4460 /*produces*/ Record[5]);
John McCalle23cf432010-12-14 08:05:40 +00004461
John McCallf85e1932011-06-15 23:02:42 +00004462 unsigned Idx = 6;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004463 unsigned NumParams = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00004464 SmallVector<QualType, 16> ParamTypes;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004465 for (unsigned I = 0; I != NumParams; ++I)
Douglas Gregor393f2492011-07-22 00:38:23 +00004466 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
John McCalle23cf432010-12-14 08:05:40 +00004467
4468 EPI.Variadic = Record[Idx++];
Richard Smitheefb3d52012-02-10 09:58:53 +00004469 EPI.HasTrailingReturn = Record[Idx++];
John McCalle23cf432010-12-14 08:05:40 +00004470 EPI.TypeQuals = Record[Idx++];
Douglas Gregorc938c162011-01-26 05:01:58 +00004471 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
Sebastian Redl60618fa2011-03-12 11:50:43 +00004472 ExceptionSpecificationType EST =
4473 static_cast<ExceptionSpecificationType>(Record[Idx++]);
4474 EPI.ExceptionSpecType = EST;
Douglas Gregorb0d06e22012-04-04 00:34:49 +00004475 SmallVector<QualType, 2> Exceptions;
Sebastian Redl60618fa2011-03-12 11:50:43 +00004476 if (EST == EST_Dynamic) {
4477 EPI.NumExceptions = Record[Idx++];
Sebastian Redl60618fa2011-03-12 11:50:43 +00004478 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
Douglas Gregor393f2492011-07-22 00:38:23 +00004479 Exceptions.push_back(readType(*Loc.F, Record, Idx));
Sebastian Redl60618fa2011-03-12 11:50:43 +00004480 EPI.Exceptions = Exceptions.data();
4481 } else if (EST == EST_ComputedNoexcept) {
4482 EPI.NoexceptExpr = ReadExpr(*Loc.F);
Richard Smith7bb698a2012-04-21 17:47:47 +00004483 } else if (EST == EST_Uninstantiated) {
4484 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
4485 EPI.ExceptionSpecTemplate = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
Richard Smithb9d0b762012-07-27 04:22:15 +00004486 } else if (EST == EST_Unevaluated) {
4487 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
Sebastian Redl60618fa2011-03-12 11:50:43 +00004488 }
Douglas Gregor35942772011-09-09 21:34:22 +00004489 return Context.getFunctionType(ResultType, ParamTypes.data(), NumParams,
John McCalle23cf432010-12-14 08:05:40 +00004490 EPI);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004491 }
4492
Douglas Gregor409448c2011-07-21 22:35:25 +00004493 case TYPE_UNRESOLVED_USING: {
4494 unsigned Idx = 0;
Douglas Gregor35942772011-09-09 21:34:22 +00004495 return Context.getTypeDeclType(
Douglas Gregor409448c2011-07-21 22:35:25 +00004496 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
4497 }
4498
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004499 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00004500 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004501 Error("incorrect encoding of typedef type");
4502 return QualType();
4503 }
Douglas Gregor409448c2011-07-21 22:35:25 +00004504 unsigned Idx = 0;
4505 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004506 QualType Canonical = readType(*Loc.F, Record, Idx);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00004507 if (!Canonical.isNull())
Douglas Gregor35942772011-09-09 21:34:22 +00004508 Canonical = Context.getCanonicalType(Canonical);
4509 return Context.getTypedefType(Decl, Canonical);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00004510 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004511
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004512 case TYPE_TYPEOF_EXPR:
Douglas Gregor35942772011-09-09 21:34:22 +00004513 return Context.getTypeOfExprType(ReadExpr(*Loc.F));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004514
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004515 case TYPE_TYPEOF: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004516 if (Record.size() != 1) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004517 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004518 return QualType();
4519 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004520 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004521 return Context.getTypeOfType(UnderlyingType);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004522 }
Mike Stump1eb44332009-09-09 15:08:12 +00004523
Douglas Gregorf8af9822012-02-12 18:42:33 +00004524 case TYPE_DECLTYPE: {
4525 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
4526 return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
4527 }
Anders Carlsson395b4752009-06-24 19:06:50 +00004528
Sean Huntca63c202011-05-24 22:41:36 +00004529 case TYPE_UNARY_TRANSFORM: {
Douglas Gregor393f2492011-07-22 00:38:23 +00004530 QualType BaseType = readType(*Loc.F, Record, Idx);
4531 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
Sean Huntca63c202011-05-24 22:41:36 +00004532 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
Douglas Gregor35942772011-09-09 21:34:22 +00004533 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
Sean Huntca63c202011-05-24 22:41:36 +00004534 }
4535
Richard Smith34b41d92011-02-20 03:19:35 +00004536 case TYPE_AUTO:
Douglas Gregor35942772011-09-09 21:34:22 +00004537 return Context.getAutoType(readType(*Loc.F, Record, Idx));
Richard Smith34b41d92011-02-20 03:19:35 +00004538
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004539 case TYPE_RECORD: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004540 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004541 Error("incorrect encoding of record type");
4542 return QualType();
4543 }
Douglas Gregor409448c2011-07-21 22:35:25 +00004544 unsigned Idx = 0;
4545 bool IsDependent = Record[Idx++];
Douglas Gregor56ca8a92012-01-17 19:21:53 +00004546 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
4547 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
4548 QualType T = Context.getRecordType(RD);
John McCallf4c73712011-01-19 06:33:43 +00004549 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004550 return T;
4551 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004552
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004553 case TYPE_ENUM: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004554 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004555 Error("incorrect encoding of enum type");
4556 return QualType();
4557 }
Douglas Gregor409448c2011-07-21 22:35:25 +00004558 unsigned Idx = 0;
4559 bool IsDependent = Record[Idx++];
4560 QualType T
Douglas Gregor35942772011-09-09 21:34:22 +00004561 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
John McCallf4c73712011-01-19 06:33:43 +00004562 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004563 return T;
4564 }
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004565
John McCall9d156a72011-01-06 01:58:22 +00004566 case TYPE_ATTRIBUTED: {
4567 if (Record.size() != 3) {
4568 Error("incorrect encoding of attributed type");
4569 return QualType();
4570 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004571 QualType modifiedType = readType(*Loc.F, Record, Idx);
4572 QualType equivalentType = readType(*Loc.F, Record, Idx);
John McCall9d156a72011-01-06 01:58:22 +00004573 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
Douglas Gregor35942772011-09-09 21:34:22 +00004574 return Context.getAttributedType(kind, modifiedType, equivalentType);
John McCall9d156a72011-01-06 01:58:22 +00004575 }
4576
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004577 case TYPE_PAREN: {
4578 if (Record.size() != 1) {
4579 Error("incorrect encoding of paren type");
4580 return QualType();
4581 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004582 QualType InnerType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004583 return Context.getParenType(InnerType);
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004584 }
4585
Douglas Gregor7536dd52010-12-20 02:24:11 +00004586 case TYPE_PACK_EXPANSION: {
Douglas Gregorf9997a02011-02-01 15:24:58 +00004587 if (Record.size() != 2) {
Douglas Gregor7536dd52010-12-20 02:24:11 +00004588 Error("incorrect encoding of pack expansion type");
4589 return QualType();
4590 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004591 QualType Pattern = readType(*Loc.F, Record, Idx);
Douglas Gregor7536dd52010-12-20 02:24:11 +00004592 if (Pattern.isNull())
4593 return QualType();
Douglas Gregorcded4f62011-01-14 17:04:44 +00004594 llvm::Optional<unsigned> NumExpansions;
4595 if (Record[1])
4596 NumExpansions = Record[1] - 1;
Douglas Gregor35942772011-09-09 21:34:22 +00004597 return Context.getPackExpansionType(Pattern, NumExpansions);
Douglas Gregor7536dd52010-12-20 02:24:11 +00004598 }
4599
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004600 case TYPE_ELABORATED: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004601 unsigned Idx = 0;
4602 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004603 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004604 QualType NamedType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004605 return Context.getElaboratedType(Keyword, NNS, NamedType);
John McCall7da24312009-09-05 00:15:47 +00004606 }
4607
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004608 case TYPE_OBJC_INTERFACE: {
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004609 unsigned Idx = 0;
Douglas Gregor409448c2011-07-21 22:35:25 +00004610 ObjCInterfaceDecl *ItfD
4611 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
Douglas Gregor56ca8a92012-01-17 19:21:53 +00004612 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
John McCallc12c5bb2010-05-15 11:32:37 +00004613 }
4614
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004615 case TYPE_OBJC_OBJECT: {
John McCallc12c5bb2010-05-15 11:32:37 +00004616 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004617 QualType Base = readType(*Loc.F, Record, Idx);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004618 unsigned NumProtos = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00004619 SmallVector<ObjCProtocolDecl*, 4> Protos;
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004620 for (unsigned I = 0; I != NumProtos; ++I)
Douglas Gregor409448c2011-07-21 22:35:25 +00004621 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
Douglas Gregor35942772011-09-09 21:34:22 +00004622 return Context.getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004623 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004624
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004625 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00004626 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004627 QualType Pointee = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004628 return Context.getObjCObjectPointerType(Pointee);
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00004629 }
Argyrios Kyrtzidis24fab412009-09-29 19:42:55 +00004630
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004631 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCall49a832b2009-10-18 09:09:24 +00004632 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004633 QualType Parm = readType(*Loc.F, Record, Idx);
4634 QualType Replacement = readType(*Loc.F, Record, Idx);
John McCall49a832b2009-10-18 09:09:24 +00004635 return
Douglas Gregor35942772011-09-09 21:34:22 +00004636 Context.getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
John McCall49a832b2009-10-18 09:09:24 +00004637 Replacement);
4638 }
John McCall3cb0ebd2010-03-10 03:28:59 +00004639
Douglas Gregorc3069d62011-01-14 02:55:32 +00004640 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
4641 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004642 QualType Parm = readType(*Loc.F, Record, Idx);
Douglas Gregorc3069d62011-01-14 02:55:32 +00004643 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004644 return Context.getSubstTemplateTypeParmPackType(
Douglas Gregorc3069d62011-01-14 02:55:32 +00004645 cast<TemplateTypeParmType>(Parm),
4646 ArgPack);
4647 }
4648
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004649 case TYPE_INJECTED_CLASS_NAME: {
Douglas Gregor409448c2011-07-21 22:35:25 +00004650 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004651 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00004652 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004653 // for AST reading, too much interdependencies.
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00004654 return
Douglas Gregor35942772011-09-09 21:34:22 +00004655 QualType(new (Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCall3cb0ebd2010-03-10 03:28:59 +00004656 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004657
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004658 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004659 unsigned Idx = 0;
4660 unsigned Depth = Record[Idx++];
4661 unsigned Index = Record[Idx++];
4662 bool Pack = Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004663 TemplateTypeParmDecl *D
4664 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004665 return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004666 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004667
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004668 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00004669 unsigned Idx = 0;
4670 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004671 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor95eab172011-07-28 20:55:49 +00004672 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004673 QualType Canon = readType(*Loc.F, Record, Idx);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00004674 if (!Canon.isNull())
Douglas Gregor35942772011-09-09 21:34:22 +00004675 Canon = Context.getCanonicalType(Canon);
4676 return Context.getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00004677 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004678
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004679 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004680 unsigned Idx = 0;
4681 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004682 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor95eab172011-07-28 20:55:49 +00004683 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004684 unsigned NumArgs = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00004685 SmallVector<TemplateArgument, 8> Args;
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004686 Args.reserve(NumArgs);
4687 while (NumArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00004688 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
Douglas Gregor35942772011-09-09 21:34:22 +00004689 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004690 Args.size(), Args.data());
4691 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004692
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004693 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004694 unsigned Idx = 0;
4695
4696 // ArrayType
Douglas Gregor393f2492011-07-22 00:38:23 +00004697 QualType ElementType = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004698 ArrayType::ArraySizeModifier ASM
4699 = (ArrayType::ArraySizeModifier)Record[Idx++];
4700 unsigned IndexTypeQuals = Record[Idx++];
4701
4702 // DependentSizedArrayType
Sebastian Redlc3632732010-10-05 15:59:54 +00004703 Expr *NumElts = ReadExpr(*Loc.F);
4704 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004705
Douglas Gregor35942772011-09-09 21:34:22 +00004706 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004707 IndexTypeQuals, Brackets);
4708 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004709
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004710 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004711 unsigned Idx = 0;
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004712 bool IsDependent = Record[Idx++];
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004713 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
Chris Lattner5f9e2722011-07-23 10:55:15 +00004714 SmallVector<TemplateArgument, 8> Args;
Sebastian Redlc3632732010-10-05 15:59:54 +00004715 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004716 QualType Underlying = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004717 QualType T;
Richard Smith3e4c6c42011-05-05 21:57:07 +00004718 if (Underlying.isNull())
Douglas Gregor35942772011-09-09 21:34:22 +00004719 T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(),
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004720 Args.size());
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00004721 else
Douglas Gregor35942772011-09-09 21:34:22 +00004722 T = Context.getTemplateSpecializationType(Name, Args.data(),
Richard Smith3e4c6c42011-05-05 21:57:07 +00004723 Args.size(), Underlying);
John McCallf4c73712011-01-19 06:33:43 +00004724 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004725 return T;
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004726 }
Eli Friedmanb001de72011-10-06 23:00:33 +00004727
4728 case TYPE_ATOMIC: {
4729 if (Record.size() != 1) {
4730 Error("Incorrect encoding of atomic type");
4731 return QualType();
4732 }
4733 QualType ValueType = readType(*Loc.F, Record, Idx);
4734 return Context.getAtomicType(ValueType);
4735 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00004736 }
David Blaikie7530c032012-01-17 06:56:22 +00004737 llvm_unreachable("Invalid TypeCode!");
Douglas Gregor2cf26342009-04-09 22:27:44 +00004738}
4739
Sebastian Redlc3632732010-10-05 15:59:54 +00004740class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004741 ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004742 ModuleFile &F;
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004743 const ASTReader::RecordData &Record;
John McCalla1ee0c52009-10-16 21:56:05 +00004744 unsigned &Idx;
4745
Sebastian Redlc3632732010-10-05 15:59:54 +00004746 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
4747 unsigned &I) {
4748 return Reader.ReadSourceLocation(F, R, I);
4749 }
4750
Douglas Gregor409448c2011-07-21 22:35:25 +00004751 template<typename T>
4752 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
4753 return Reader.ReadDeclAs<T>(F, Record, Idx);
4754 }
4755
John McCalla1ee0c52009-10-16 21:56:05 +00004756public:
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004757 TypeLocReader(ASTReader &Reader, ModuleFile &F,
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004758 const ASTReader::RecordData &Record, unsigned &Idx)
Benjamin Kramerfacde172012-06-06 17:32:50 +00004759 : Reader(Reader), F(F), Record(Record), Idx(Idx)
Sebastian Redlc3632732010-10-05 15:59:54 +00004760 { }
John McCalla1ee0c52009-10-16 21:56:05 +00004761
John McCall51bd8032009-10-18 01:05:36 +00004762 // We want compile-time assurance that we've enumerated all of
4763 // these, so unfortunately we have to declare them first, then
4764 // define them out-of-line.
4765#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +00004766#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +00004767 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00004768#include "clang/AST/TypeLocNodes.def"
4769
John McCall51bd8032009-10-18 01:05:36 +00004770 void VisitFunctionTypeLoc(FunctionTypeLoc);
4771 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00004772};
4773
John McCall51bd8032009-10-18 01:05:36 +00004774void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCalla1ee0c52009-10-16 21:56:05 +00004775 // nothing to do
4776}
John McCall51bd8032009-10-18 01:05:36 +00004777void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004778 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
Douglas Gregorddf889a2010-01-18 18:04:31 +00004779 if (TL.needsExtraLocalData()) {
4780 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
4781 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
4782 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
4783 TL.setModeAttr(Record[Idx++]);
4784 }
John McCalla1ee0c52009-10-16 21:56:05 +00004785}
John McCall51bd8032009-10-18 01:05:36 +00004786void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004787 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004788}
John McCall51bd8032009-10-18 01:05:36 +00004789void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004790 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004791}
John McCall51bd8032009-10-18 01:05:36 +00004792void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004793 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004794}
John McCall51bd8032009-10-18 01:05:36 +00004795void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004796 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004797}
John McCall51bd8032009-10-18 01:05:36 +00004798void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004799 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004800}
John McCall51bd8032009-10-18 01:05:36 +00004801void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004802 TL.setStarLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +00004803 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004804}
John McCall51bd8032009-10-18 01:05:36 +00004805void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004806 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
4807 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004808 if (Record[Idx++])
Sebastian Redlc3632732010-10-05 15:59:54 +00004809 TL.setSizeExpr(Reader.ReadExpr(F));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004810 else
John McCall51bd8032009-10-18 01:05:36 +00004811 TL.setSizeExpr(0);
4812}
4813void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
4814 VisitArrayTypeLoc(TL);
4815}
4816void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
4817 VisitArrayTypeLoc(TL);
4818}
4819void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
4820 VisitArrayTypeLoc(TL);
4821}
4822void TypeLocReader::VisitDependentSizedArrayTypeLoc(
4823 DependentSizedArrayTypeLoc TL) {
4824 VisitArrayTypeLoc(TL);
4825}
4826void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
4827 DependentSizedExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004828 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004829}
4830void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004831 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004832}
4833void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004834 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004835}
4836void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnara796aa442011-03-12 11:17:06 +00004837 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004838 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4839 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnara796aa442011-03-12 11:17:06 +00004840 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004841 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
Douglas Gregor409448c2011-07-21 22:35:25 +00004842 TL.setArg(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004843 }
4844}
4845void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
4846 VisitFunctionTypeLoc(TL);
4847}
4848void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
4849 VisitFunctionTypeLoc(TL);
4850}
John McCalled976492009-12-04 22:46:56 +00004851void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004852 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalled976492009-12-04 22:46:56 +00004853}
John McCall51bd8032009-10-18 01:05:36 +00004854void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004855 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004856}
4857void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004858 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
4859 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4860 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004861}
4862void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004863 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
4864 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4865 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4866 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004867}
4868void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004869 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004870}
Sean Huntca63c202011-05-24 22:41:36 +00004871void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
4872 TL.setKWLoc(ReadSourceLocation(Record, Idx));
4873 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4874 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4875 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
4876}
Richard Smith34b41d92011-02-20 03:19:35 +00004877void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
4878 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4879}
John McCall51bd8032009-10-18 01:05:36 +00004880void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004881 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004882}
4883void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004884 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004885}
John McCall9d156a72011-01-06 01:58:22 +00004886void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
4887 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
4888 if (TL.hasAttrOperand()) {
4889 SourceRange range;
4890 range.setBegin(ReadSourceLocation(Record, Idx));
4891 range.setEnd(ReadSourceLocation(Record, Idx));
4892 TL.setAttrOperandParensRange(range);
4893 }
4894 if (TL.hasAttrExprOperand()) {
4895 if (Record[Idx++])
4896 TL.setAttrExprOperand(Reader.ReadExpr(F));
4897 else
4898 TL.setAttrExprOperand(0);
4899 } else if (TL.hasAttrEnumOperand())
4900 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
4901}
John McCall51bd8032009-10-18 01:05:36 +00004902void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004903 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004904}
John McCall49a832b2009-10-18 09:09:24 +00004905void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
4906 SubstTemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004907 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall49a832b2009-10-18 09:09:24 +00004908}
Douglas Gregorc3069d62011-01-14 02:55:32 +00004909void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
4910 SubstTemplateTypeParmPackTypeLoc TL) {
4911 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4912}
John McCall51bd8032009-10-18 01:05:36 +00004913void TypeLocReader::VisitTemplateSpecializationTypeLoc(
4914 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004915 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00004916 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
4917 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4918 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall833ca992009-10-29 08:12:44 +00004919 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
4920 TL.setArgLocInfo(i,
Sebastian Redlc3632732010-10-05 15:59:54 +00004921 Reader.GetTemplateArgumentLocInfo(F,
4922 TL.getTypePtr()->getArg(i).getKind(),
4923 Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004924}
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004925void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
4926 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4927 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4928}
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004929void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +00004930 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor9e876872011-03-01 18:12:44 +00004931 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004932}
John McCall3cb0ebd2010-03-10 03:28:59 +00004933void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004934 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall3cb0ebd2010-03-10 03:28:59 +00004935}
Douglas Gregor4714c122010-03-31 17:34:00 +00004936void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +00004937 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor2494dd02011-03-01 01:34:45 +00004938 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00004939 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004940}
John McCall33500952010-06-11 00:33:02 +00004941void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
4942 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004943 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor94fdffa2011-03-01 20:11:18 +00004944 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Abramo Bagnara66581d42012-02-06 22:45:07 +00004945 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004946 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00004947 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4948 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall33500952010-06-11 00:33:02 +00004949 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
4950 TL.setArgLocInfo(I,
Sebastian Redlc3632732010-10-05 15:59:54 +00004951 Reader.GetTemplateArgumentLocInfo(F,
4952 TL.getTypePtr()->getArg(I).getKind(),
4953 Record, Idx));
John McCall33500952010-06-11 00:33:02 +00004954}
Douglas Gregor7536dd52010-12-20 02:24:11 +00004955void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
4956 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
4957}
John McCall51bd8032009-10-18 01:05:36 +00004958void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004959 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallc12c5bb2010-05-15 11:32:37 +00004960}
4961void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
4962 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00004963 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4964 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004965 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Sebastian Redlc3632732010-10-05 15:59:54 +00004966 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004967}
John McCall54e14c42009-10-22 22:37:11 +00004968void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004969 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall54e14c42009-10-22 22:37:11 +00004970}
Eli Friedmanb001de72011-10-06 23:00:33 +00004971void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
4972 TL.setKWLoc(ReadSourceLocation(Record, Idx));
4973 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4974 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4975}
John McCalla1ee0c52009-10-16 21:56:05 +00004976
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004977TypeSourceInfo *ASTReader::GetTypeSourceInfo(ModuleFile &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00004978 const RecordData &Record,
John McCalla1ee0c52009-10-16 21:56:05 +00004979 unsigned &Idx) {
Douglas Gregor393f2492011-07-22 00:38:23 +00004980 QualType InfoTy = readType(F, Record, Idx);
John McCalla1ee0c52009-10-16 21:56:05 +00004981 if (InfoTy.isNull())
4982 return 0;
4983
Douglas Gregor35942772011-09-09 21:34:22 +00004984 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
Sebastian Redlc3632732010-10-05 15:59:54 +00004985 TypeLocReader TLR(*this, F, Record, Idx);
John McCalla93c9342009-12-07 02:54:59 +00004986 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCalla1ee0c52009-10-16 21:56:05 +00004987 TLR.Visit(TL);
John McCalla93c9342009-12-07 02:54:59 +00004988 return TInfo;
John McCalla1ee0c52009-10-16 21:56:05 +00004989}
Douglas Gregor2cf26342009-04-09 22:27:44 +00004990
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004991QualType ASTReader::GetType(TypeID ID) {
John McCall0953e762009-09-24 19:53:00 +00004992 unsigned FastQuals = ID & Qualifiers::FastMask;
4993 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004994
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004995 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00004996 QualType T;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004997 switch ((PredefinedTypeIDs)Index) {
4998 case PREDEF_TYPE_NULL_ID: return QualType();
Douglas Gregor35942772011-09-09 21:34:22 +00004999 case PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
5000 case PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00005001
Sebastian Redl8538e8d2010-08-18 23:57:32 +00005002 case PREDEF_TYPE_CHAR_U_ID:
5003 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregor2cf26342009-04-09 22:27:44 +00005004 // FIXME: Check that the signedness of CharTy is correct!
Douglas Gregor35942772011-09-09 21:34:22 +00005005 T = Context.CharTy;
Douglas Gregor2cf26342009-04-09 22:27:44 +00005006 break;
5007
Douglas Gregor35942772011-09-09 21:34:22 +00005008 case PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
5009 case PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
5010 case PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
5011 case PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
5012 case PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
5013 case PREDEF_TYPE_UINT128_ID: T = Context.UnsignedInt128Ty; break;
5014 case PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
5015 case PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
5016 case PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
5017 case PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
5018 case PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
5019 case PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
5020 case PREDEF_TYPE_INT128_ID: T = Context.Int128Ty; break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00005021 case PREDEF_TYPE_HALF_ID: T = Context.HalfTy; break;
Douglas Gregor35942772011-09-09 21:34:22 +00005022 case PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
5023 case PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
5024 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
5025 case PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
5026 case PREDEF_TYPE_BOUND_MEMBER: T = Context.BoundMemberTy; break;
John McCall3c3b7f92011-10-25 17:37:35 +00005027 case PREDEF_TYPE_PSEUDO_OBJECT: T = Context.PseudoObjectTy; break;
Douglas Gregor35942772011-09-09 21:34:22 +00005028 case PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
5029 case PREDEF_TYPE_UNKNOWN_ANY: T = Context.UnknownAnyTy; break;
5030 case PREDEF_TYPE_NULLPTR_ID: T = Context.NullPtrTy; break;
5031 case PREDEF_TYPE_CHAR16_ID: T = Context.Char16Ty; break;
5032 case PREDEF_TYPE_CHAR32_ID: T = Context.Char32Ty; break;
5033 case PREDEF_TYPE_OBJC_ID: T = Context.ObjCBuiltinIdTy; break;
5034 case PREDEF_TYPE_OBJC_CLASS: T = Context.ObjCBuiltinClassTy; break;
5035 case PREDEF_TYPE_OBJC_SEL: T = Context.ObjCBuiltinSelTy; break;
5036 case PREDEF_TYPE_AUTO_DEDUCT: T = Context.getAutoDeductType(); break;
Douglas Gregor3b8043b2011-08-09 15:13:55 +00005037
5038 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
Douglas Gregor35942772011-09-09 21:34:22 +00005039 T = Context.getAutoRRefDeductType();
Douglas Gregor3b8043b2011-08-09 15:13:55 +00005040 break;
John McCall0ddaeb92011-10-17 18:09:15 +00005041
5042 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
5043 T = Context.ARCUnbridgedCastTy;
5044 break;
5045
Meador Ingefb40e3f2012-07-01 15:57:25 +00005046 case PREDEF_TYPE_VA_LIST_TAG:
5047 T = Context.getVaListTagType();
5048 break;
Eli Friedmana6c66ce2012-08-31 00:14:07 +00005049
5050 case PREDEF_TYPE_BUILTIN_FN:
5051 T = Context.BuiltinFnTy;
5052 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00005053 }
5054
5055 assert(!T.isNull() && "Unknown predefined type");
John McCall0953e762009-09-24 19:53:00 +00005056 return T.withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00005057 }
5058
Sebastian Redl8538e8d2010-08-18 23:57:32 +00005059 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00005060 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl07a353c2010-07-14 20:26:45 +00005061 if (TypesLoaded[Index].isNull()) {
Douglas Gregor393f2492011-07-22 00:38:23 +00005062 TypesLoaded[Index] = readTypeRecord(Index);
Douglas Gregor97475832010-10-05 18:37:06 +00005063 if (TypesLoaded[Index].isNull())
5064 return QualType();
5065
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005066 TypesLoaded[Index]->setFromAST();
Sebastian Redl30c514c2010-07-14 23:45:08 +00005067 if (DeserializationListener)
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00005068 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1476ed42010-07-16 16:36:56 +00005069 TypesLoaded[Index]);
Sebastian Redl07a353c2010-07-14 20:26:45 +00005070 }
Mike Stump1eb44332009-09-09 15:08:12 +00005071
John McCall0953e762009-09-24 19:53:00 +00005072 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00005073}
5074
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005075QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
Douglas Gregor393f2492011-07-22 00:38:23 +00005076 return GetType(getGlobalTypeID(F, LocalID));
5077}
5078
5079serialization::TypeID
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005080ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
Douglas Gregora119da02011-08-02 16:26:37 +00005081 unsigned FastQuals = LocalID & Qualifiers::FastMask;
5082 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
5083
5084 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
5085 return LocalID;
5086
5087 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5088 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
5089 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
5090
5091 unsigned GlobalIndex = LocalIndex + I->second;
5092 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
5093}
5094
John McCall833ca992009-10-29 08:12:44 +00005095TemplateArgumentLocInfo
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005096ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
Sebastian Redlc3632732010-10-05 15:59:54 +00005097 TemplateArgument::ArgKind Kind,
John McCall833ca992009-10-29 08:12:44 +00005098 const RecordData &Record,
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00005099 unsigned &Index) {
John McCall833ca992009-10-29 08:12:44 +00005100 switch (Kind) {
5101 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00005102 return ReadExpr(F);
John McCall833ca992009-10-29 08:12:44 +00005103 case TemplateArgument::Type:
Sebastian Redlc3632732010-10-05 15:59:54 +00005104 return GetTypeSourceInfo(F, Record, Index);
Douglas Gregor788cd062009-11-11 01:00:40 +00005105 case TemplateArgument::Template: {
Douglas Gregorb6744ef2011-03-02 17:09:35 +00005106 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
5107 Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00005108 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorb6744ef2011-03-02 17:09:35 +00005109 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregora7fc9012011-01-05 18:58:31 +00005110 SourceLocation());
5111 }
5112 case TemplateArgument::TemplateExpansion: {
Douglas Gregorb6744ef2011-03-02 17:09:35 +00005113 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
5114 Index);
Douglas Gregora7fc9012011-01-05 18:58:31 +00005115 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorba68eca2011-01-05 17:40:24 +00005116 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorb6744ef2011-03-02 17:09:35 +00005117 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregorba68eca2011-01-05 17:40:24 +00005118 EllipsisLoc);
Douglas Gregor788cd062009-11-11 01:00:40 +00005119 }
John McCall833ca992009-10-29 08:12:44 +00005120 case TemplateArgument::Null:
5121 case TemplateArgument::Integral:
5122 case TemplateArgument::Declaration:
Eli Friedmand7a6b162012-09-26 02:36:12 +00005123 case TemplateArgument::NullPtr:
John McCall833ca992009-10-29 08:12:44 +00005124 case TemplateArgument::Pack:
Eli Friedmand7a6b162012-09-26 02:36:12 +00005125 // FIXME: Is this right?
John McCall833ca992009-10-29 08:12:44 +00005126 return TemplateArgumentLocInfo();
5127 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00005128 llvm_unreachable("unexpected template argument loc");
John McCall833ca992009-10-29 08:12:44 +00005129}
5130
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00005131TemplateArgumentLoc
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005132ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00005133 const RecordData &Record, unsigned &Index) {
Sebastian Redlc3632732010-10-05 15:59:54 +00005134 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00005135
5136 if (Arg.getKind() == TemplateArgument::Expression) {
5137 if (Record[Index++]) // bool InfoHasSameExpr.
5138 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
5139 }
Sebastian Redlc3632732010-10-05 15:59:54 +00005140 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00005141 Record, Index));
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00005142}
5143
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005144Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall76bd1f32010-06-01 09:23:16 +00005145 return GetDecl(ID);
5146}
5147
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005148uint64_t ASTReader::readCXXBaseSpecifiers(ModuleFile &M, const RecordData &Record,
Douglas Gregore92b8a12011-08-04 00:01:48 +00005149 unsigned &Idx){
5150 if (Idx >= Record.size())
Douglas Gregor7c789c12010-10-29 22:39:52 +00005151 return 0;
Douglas Gregor7c789c12010-10-29 22:39:52 +00005152
Douglas Gregore92b8a12011-08-04 00:01:48 +00005153 unsigned LocalID = Record[Idx++];
5154 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
Douglas Gregor7c789c12010-10-29 22:39:52 +00005155}
5156
5157CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
Douglas Gregor8f1231b2011-07-22 06:10:01 +00005158 RecordLocation Loc = getLocalBitOffset(Offset);
5159 llvm::BitstreamCursor &Cursor = Loc.F->DeclsCursor;
Douglas Gregor7c789c12010-10-29 22:39:52 +00005160 SavedStreamPosition SavedPosition(Cursor);
Douglas Gregor8f1231b2011-07-22 06:10:01 +00005161 Cursor.JumpToBit(Loc.Offset);
Douglas Gregor7c789c12010-10-29 22:39:52 +00005162 ReadingKindTracker ReadingKind(Read_Decl, *this);
5163 RecordData Record;
5164 unsigned Code = Cursor.ReadCode();
5165 unsigned RecCode = Cursor.ReadRecord(Code, Record);
5166 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
5167 Error("Malformed AST file: missing C++ base specifiers");
5168 return 0;
5169 }
5170
5171 unsigned Idx = 0;
5172 unsigned NumBases = Record[Idx++];
Douglas Gregor35942772011-09-09 21:34:22 +00005173 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00005174 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
5175 for (unsigned I = 0; I != NumBases; ++I)
Douglas Gregor8f1231b2011-07-22 06:10:01 +00005176 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
Douglas Gregor7c789c12010-10-29 22:39:52 +00005177 return Bases;
5178}
5179
Douglas Gregor409448c2011-07-21 22:35:25 +00005180serialization::DeclID
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00005181ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00005182 if (LocalID < NUM_PREDEF_DECL_IDS)
Douglas Gregor496c7092011-08-03 15:48:04 +00005183 return LocalID;
5184
5185 ContinuousRangeMap<uint32_t, int, 2>::iterator I
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00005186 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
Douglas Gregor496c7092011-08-03 15:48:04 +00005187 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
5188
5189 return LocalID + I->second;
Douglas Gregor409448c2011-07-21 22:35:25 +00005190}
5191
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00005192bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005193 ModuleFile &M) const {
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00005194 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(ID);
5195 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5196 return &M == I->second;
5197}
5198
Douglas Gregorcff9f262012-01-27 01:47:08 +00005199ModuleFile *ASTReader::getOwningModuleFile(Decl *D) {
5200 if (!D->isFromASTFile())
5201 return 0;
5202 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
5203 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5204 return I->second;
5205}
5206
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00005207SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
5208 if (ID < NUM_PREDEF_DECL_IDS)
5209 return SourceLocation();
5210
5211 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
5212
5213 if (Index > DeclsLoaded.size()) {
5214 Error("declaration ID out-of-range for AST file");
5215 return SourceLocation();
5216 }
5217
5218 if (Decl *D = DeclsLoaded[Index])
5219 return D->getLocation();
5220
5221 unsigned RawLocation = 0;
5222 RecordLocation Rec = DeclCursorForID(ID, RawLocation);
5223 return ReadSourceLocation(*Rec.F, RawLocation);
5224}
5225
Sebastian Redl8538e8d2010-08-18 23:57:32 +00005226Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00005227 if (ID < NUM_PREDEF_DECL_IDS) {
5228 switch ((PredefinedDeclIDs)ID) {
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00005229 case PREDEF_DECL_NULL_ID:
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00005230 return 0;
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00005231
5232 case PREDEF_DECL_TRANSLATION_UNIT_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00005233 return Context.getTranslationUnitDecl();
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00005234
5235 case PREDEF_DECL_OBJC_ID_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00005236 return Context.getObjCIdDecl();
Douglas Gregor79d67262011-08-12 05:59:41 +00005237
Douglas Gregor7a27ea52011-08-12 06:17:30 +00005238 case PREDEF_DECL_OBJC_SEL_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00005239 return Context.getObjCSelDecl();
Douglas Gregor7a27ea52011-08-12 06:17:30 +00005240
Douglas Gregor79d67262011-08-12 05:59:41 +00005241 case PREDEF_DECL_OBJC_CLASS_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00005242 return Context.getObjCClassDecl();
Douglas Gregor772eeae2011-08-12 06:49:56 +00005243
Douglas Gregora6ea10e2012-01-17 18:09:05 +00005244 case PREDEF_DECL_OBJC_PROTOCOL_ID:
5245 return Context.getObjCProtocolDecl();
5246
Douglas Gregor772eeae2011-08-12 06:49:56 +00005247 case PREDEF_DECL_INT_128_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00005248 return Context.getInt128Decl();
Douglas Gregor772eeae2011-08-12 06:49:56 +00005249
5250 case PREDEF_DECL_UNSIGNED_INT_128_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00005251 return Context.getUInt128Decl();
Douglas Gregore97179c2011-09-08 01:46:34 +00005252
5253 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00005254 return Context.getObjCInstanceTypeDecl();
Meador Ingec5613b22012-06-16 03:34:49 +00005255
5256 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
5257 return Context.getBuiltinVaListDecl();
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00005258 }
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00005259 }
5260
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00005261 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
5262
Richard Smith2fbf3732011-12-20 04:39:57 +00005263 if (Index >= DeclsLoaded.size()) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00005264 assert(0 && "declaration ID out-of-range for AST file");
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005265 Error("declaration ID out-of-range for AST file");
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00005266 return 0;
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005267 }
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00005268
Douglas Gregorfd002a72011-12-16 22:37:11 +00005269 if (!DeclsLoaded[Index]) {
Douglas Gregor496c7092011-08-03 15:48:04 +00005270 ReadDeclRecord(ID);
Sebastian Redl30c514c2010-07-14 23:45:08 +00005271 if (DeserializationListener)
5272 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
5273 }
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005274
5275 return DeclsLoaded[Index];
Douglas Gregor2cf26342009-04-09 22:27:44 +00005276}
5277
Douglas Gregora1be2782011-12-17 23:38:30 +00005278DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
5279 DeclID GlobalID) {
5280 if (GlobalID < NUM_PREDEF_DECL_IDS)
5281 return GlobalID;
5282
5283 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
5284 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5285 ModuleFile *Owner = I->second;
5286
5287 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
5288 = M.GlobalToLocalDeclIDs.find(Owner);
5289 if (Pos == M.GlobalToLocalDeclIDs.end())
5290 return 0;
5291
5292 return GlobalID - Owner->BaseDeclID + Pos->second;
5293}
5294
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005295serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00005296 const RecordData &Record,
5297 unsigned &Idx) {
5298 if (Idx >= Record.size()) {
5299 Error("Corrupted AST file");
5300 return 0;
5301 }
5302
5303 return getGlobalDeclID(F, Record[Idx++]);
5304}
5305
Chris Lattner887e2b32009-04-27 05:46:25 +00005306/// \brief Resolve the offset of a statement into a statement.
5307///
5308/// This operation will read a new statement from the external
5309/// source each time it is called, and is meant to be used via a
5310/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005311Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00005312 // Switch case IDs are per Decl.
5313 ClearSwitchCaseIDs();
5314
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00005315 // Offset here is a global offset across the entire chain.
Douglas Gregor8f1231b2011-07-22 06:10:01 +00005316 RecordLocation Loc = getLocalBitOffset(Offset);
5317 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
5318 return ReadStmtFromStream(*Loc.F);
Douglas Gregor250fc9c2009-04-18 00:07:54 +00005319}
5320
Douglas Gregor851c75a2011-08-24 21:27:34 +00005321namespace {
5322 class FindExternalLexicalDeclsVisitor {
5323 ASTReader &Reader;
5324 const DeclContext *DC;
5325 bool (*isKindWeWant)(Decl::Kind);
Douglas Gregor2ea054f2011-08-26 22:04:51 +00005326
Douglas Gregor851c75a2011-08-24 21:27:34 +00005327 SmallVectorImpl<Decl*> &Decls;
5328 bool PredefsVisited[NUM_PREDEF_DECL_IDS];
5329
5330 public:
5331 FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC,
5332 bool (*isKindWeWant)(Decl::Kind),
5333 SmallVectorImpl<Decl*> &Decls)
5334 : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls)
5335 {
5336 for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I)
5337 PredefsVisited[I] = false;
5338 }
5339
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005340 static bool visit(ModuleFile &M, bool Preorder, void *UserData) {
Douglas Gregor851c75a2011-08-24 21:27:34 +00005341 if (Preorder)
5342 return false;
5343
5344 FindExternalLexicalDeclsVisitor *This
5345 = static_cast<FindExternalLexicalDeclsVisitor *>(UserData);
5346
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005347 ModuleFile::DeclContextInfosMap::iterator Info
Douglas Gregor851c75a2011-08-24 21:27:34 +00005348 = M.DeclContextInfos.find(This->DC);
5349 if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls)
5350 return false;
5351
5352 // Load all of the declaration IDs
5353 for (const KindDeclIDPair *ID = Info->second.LexicalDecls,
5354 *IDE = ID + Info->second.NumLexicalDecls;
5355 ID != IDE; ++ID) {
5356 if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first))
5357 continue;
5358
5359 // Don't add predefined declarations to the lexical context more
5360 // than once.
5361 if (ID->second < NUM_PREDEF_DECL_IDS) {
5362 if (This->PredefsVisited[ID->second])
5363 continue;
5364
5365 This->PredefsVisited[ID->second] = true;
5366 }
5367
Douglas Gregor2ea054f2011-08-26 22:04:51 +00005368 if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) {
5369 if (!This->DC->isDeclInLexicalTraversal(D))
5370 This->Decls.push_back(D);
5371 }
Douglas Gregor851c75a2011-08-24 21:27:34 +00005372 }
5373
5374 return false;
5375 }
5376 };
5377}
5378
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00005379ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00005380 bool (*isKindWeWant)(Decl::Kind),
Chris Lattner5f9e2722011-07-23 10:55:15 +00005381 SmallVectorImpl<Decl*> &Decls) {
Douglas Gregor0d95f772011-08-24 19:03:07 +00005382 // There might be lexical decls in multiple modules, for the TU at
Douglas Gregor851c75a2011-08-24 21:27:34 +00005383 // least. Walk all of the modules in the order they were loaded.
5384 FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls);
5385 ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor);
Douglas Gregor25123082009-04-22 22:34:57 +00005386 ++NumLexicalDeclContextsRead;
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00005387 return ELR_Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00005388}
5389
Douglas Gregor0d95f772011-08-24 19:03:07 +00005390namespace {
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00005391
5392class DeclIDComp {
5393 ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005394 ModuleFile &Mod;
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00005395
5396public:
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005397 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00005398
5399 bool operator()(LocalDeclID L, LocalDeclID R) const {
5400 SourceLocation LHS = getLocation(L);
5401 SourceLocation RHS = getLocation(R);
5402 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5403 }
5404
5405 bool operator()(SourceLocation LHS, LocalDeclID R) const {
5406 SourceLocation RHS = getLocation(R);
5407 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5408 }
5409
5410 bool operator()(LocalDeclID L, SourceLocation RHS) const {
5411 SourceLocation LHS = getLocation(L);
5412 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5413 }
5414
5415 SourceLocation getLocation(LocalDeclID ID) const {
5416 return Reader.getSourceManager().getFileLoc(
5417 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
5418 }
5419};
5420
5421}
5422
5423void ASTReader::FindFileRegionDecls(FileID File,
5424 unsigned Offset, unsigned Length,
5425 SmallVectorImpl<Decl *> &Decls) {
5426 SourceManager &SM = getSourceManager();
5427
5428 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
5429 if (I == FileDeclIDs.end())
5430 return;
5431
5432 FileDeclsInfo &DInfo = I->second;
5433 if (DInfo.Decls.empty())
5434 return;
5435
5436 SourceLocation
5437 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
5438 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
5439
5440 DeclIDComp DIDComp(*this, *DInfo.Mod);
5441 ArrayRef<serialization::LocalDeclID>::iterator
5442 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
5443 BeginLoc, DIDComp);
5444 if (BeginIt != DInfo.Decls.begin())
5445 --BeginIt;
5446
Argyrios Kyrtzidisc14a03d2011-11-23 20:27:36 +00005447 // If we are pointing at a top-level decl inside an objc container, we need
5448 // to backtrack until we find it otherwise we will fail to report that the
5449 // region overlaps with an objc container.
5450 while (BeginIt != DInfo.Decls.begin() &&
5451 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
5452 ->isTopLevelDeclInObjCContainer())
5453 --BeginIt;
5454
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00005455 ArrayRef<serialization::LocalDeclID>::iterator
5456 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
5457 EndLoc, DIDComp);
5458 if (EndIt != DInfo.Decls.end())
5459 ++EndIt;
5460
5461 for (ArrayRef<serialization::LocalDeclID>::iterator
5462 DIt = BeginIt; DIt != EndIt; ++DIt)
5463 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
5464}
5465
5466namespace {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005467 /// \brief ModuleFile visitor used to perform name lookup into a
Douglas Gregor0d95f772011-08-24 19:03:07 +00005468 /// declaration context.
5469 class DeclContextNameLookupVisitor {
5470 ASTReader &Reader;
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005471 llvm::SmallVectorImpl<const DeclContext *> &Contexts;
Douglas Gregor0d95f772011-08-24 19:03:07 +00005472 DeclarationName Name;
5473 SmallVectorImpl<NamedDecl *> &Decls;
5474
5475 public:
5476 DeclContextNameLookupVisitor(ASTReader &Reader,
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005477 SmallVectorImpl<const DeclContext *> &Contexts,
5478 DeclarationName Name,
Douglas Gregor0d95f772011-08-24 19:03:07 +00005479 SmallVectorImpl<NamedDecl *> &Decls)
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005480 : Reader(Reader), Contexts(Contexts), Name(Name), Decls(Decls) { }
Douglas Gregor0d95f772011-08-24 19:03:07 +00005481
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005482 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregor0d95f772011-08-24 19:03:07 +00005483 DeclContextNameLookupVisitor *This
5484 = static_cast<DeclContextNameLookupVisitor *>(UserData);
5485
5486 // Check whether we have any visible declaration information for
5487 // this context in this module.
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005488 ModuleFile::DeclContextInfosMap::iterator Info;
5489 bool FoundInfo = false;
5490 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5491 Info = M.DeclContextInfos.find(This->Contexts[I]);
5492 if (Info != M.DeclContextInfos.end() &&
5493 Info->second.NameLookupTableData) {
5494 FoundInfo = true;
5495 break;
5496 }
5497 }
Douglas Gregor0d95f772011-08-24 19:03:07 +00005498
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005499 if (!FoundInfo)
5500 return false;
5501
Douglas Gregor0d95f772011-08-24 19:03:07 +00005502 // Look for this name within this module.
5503 ASTDeclContextNameLookupTable *LookupTable =
Benjamin Kramerb1758c62012-04-15 12:36:49 +00005504 Info->second.NameLookupTableData;
Douglas Gregor0d95f772011-08-24 19:03:07 +00005505 ASTDeclContextNameLookupTable::iterator Pos
5506 = LookupTable->find(This->Name);
5507 if (Pos == LookupTable->end())
5508 return false;
5509
5510 bool FoundAnything = false;
5511 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
5512 for (; Data.first != Data.second; ++Data.first) {
5513 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first);
5514 if (!ND)
5515 continue;
5516
5517 if (ND->getDeclName() != This->Name) {
Axel Naumann3dd82f72012-10-01 09:51:27 +00005518 // A name might be null because the decl's redeclarable part is
5519 // currently read before reading its name. The lookup is triggered by
5520 // building that decl (likely indirectly), and so it is later in the
5521 // sense of "already existing" and can be ignored here.
Douglas Gregor0d95f772011-08-24 19:03:07 +00005522 continue;
5523 }
5524
5525 // Record this declaration.
5526 FoundAnything = true;
5527 This->Decls.push_back(ND);
5528 }
5529
5530 return FoundAnything;
5531 }
5532 };
5533}
5534
John McCall76bd1f32010-06-01 09:23:16 +00005535DeclContext::lookup_result
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005536ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +00005537 DeclarationName Name) {
Mike Stump1eb44332009-09-09 15:08:12 +00005538 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00005539 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00005540 if (!Name)
5541 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
5542 DeclContext::lookup_iterator(0));
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00005543
Chris Lattner5f9e2722011-07-23 10:55:15 +00005544 SmallVector<NamedDecl *, 64> Decls;
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005545
5546 // Compute the declaration contexts we need to look into. Multiple such
5547 // declaration contexts occur when two declaration contexts from disjoint
5548 // modules get merged, e.g., when two namespaces with the same name are
5549 // independently defined in separate modules.
5550 SmallVector<const DeclContext *, 2> Contexts;
5551 Contexts.push_back(DC);
5552
5553 if (DC->isNamespace()) {
5554 MergedDeclsMap::iterator Merged
5555 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5556 if (Merged != MergedDecls.end()) {
5557 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5558 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5559 }
5560 }
5561
5562 DeclContextNameLookupVisitor Visitor(*this, Contexts, Name, Decls);
Douglas Gregor0d95f772011-08-24 19:03:07 +00005563 ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor);
Douglas Gregor25123082009-04-22 22:34:57 +00005564 ++NumVisibleDeclContextsRead;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00005565 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall76bd1f32010-06-01 09:23:16 +00005566 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregor2cf26342009-04-09 22:27:44 +00005567}
5568
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005569namespace {
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005570 /// \brief ModuleFile visitor used to retrieve all visible names in a
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005571 /// declaration context.
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005572 class DeclContextAllNamesVisitor {
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005573 ASTReader &Reader;
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005574 llvm::SmallVectorImpl<const DeclContext *> &Contexts;
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005575 llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> > &Decls;
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005576
5577 public:
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005578 DeclContextAllNamesVisitor(ASTReader &Reader,
5579 SmallVectorImpl<const DeclContext *> &Contexts,
5580 llvm::DenseMap<DeclarationName,
5581 SmallVector<NamedDecl *, 8> > &Decls)
5582 : Reader(Reader), Contexts(Contexts), Decls(Decls) { }
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005583
5584 static bool visit(ModuleFile &M, void *UserData) {
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005585 DeclContextAllNamesVisitor *This
5586 = static_cast<DeclContextAllNamesVisitor *>(UserData);
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005587
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005588 // Check whether we have any visible declaration information for
5589 // this context in this module.
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005590 ModuleFile::DeclContextInfosMap::iterator Info;
5591 bool FoundInfo = false;
5592 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5593 Info = M.DeclContextInfos.find(This->Contexts[I]);
5594 if (Info != M.DeclContextInfos.end() &&
5595 Info->second.NameLookupTableData) {
5596 FoundInfo = true;
5597 break;
5598 }
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005599 }
5600
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005601 if (!FoundInfo)
5602 return false;
5603
5604 ASTDeclContextNameLookupTable *LookupTable =
5605 Info->second.NameLookupTableData;
5606 bool FoundAnything = false;
5607 for (ASTDeclContextNameLookupTable::data_iterator
5608 I = LookupTable->data_begin(), E = LookupTable->data_end();
5609 I != E; ++I) {
5610 ASTDeclContextNameLookupTrait::data_type Data = *I;
5611 for (; Data.first != Data.second; ++Data.first) {
5612 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M,
5613 *Data.first);
5614 if (!ND)
5615 continue;
5616
5617 // Record this declaration.
5618 FoundAnything = true;
5619 This->Decls[ND->getDeclName()].push_back(ND);
5620 }
5621 }
5622
5623 return FoundAnything;
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005624 }
5625 };
5626}
5627
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005628void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005629 if (!DC->hasExternalVisibleStorage())
5630 return;
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005631 llvm::DenseMap<DeclarationName, llvm::SmallVector<NamedDecl*, 8> > Decls;
5632
5633 // Compute the declaration contexts we need to look into. Multiple such
5634 // declaration contexts occur when two declaration contexts from disjoint
5635 // modules get merged, e.g., when two namespaces with the same name are
5636 // independently defined in separate modules.
5637 SmallVector<const DeclContext *, 2> Contexts;
5638 Contexts.push_back(DC);
5639
5640 if (DC->isNamespace()) {
5641 MergedDeclsMap::iterator Merged
5642 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5643 if (Merged != MergedDecls.end()) {
5644 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5645 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5646 }
5647 }
5648
5649 DeclContextAllNamesVisitor Visitor(*this, Contexts, Decls);
5650 ModuleMgr.visit(&DeclContextAllNamesVisitor::visit, &Visitor);
5651 ++NumVisibleDeclContextsRead;
5652
5653 for (llvm::DenseMap<DeclarationName,
5654 llvm::SmallVector<NamedDecl*, 8> >::iterator
5655 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
5656 SetExternalVisibleDeclsForName(DC, I->first, I->second);
5657 }
Argyrios Kyrtzidis394e5392012-04-26 18:34:14 +00005658 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005659}
5660
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005661/// \brief Under non-PCH compilation the consumer receives the objc methods
5662/// before receiving the implementation, and codegen depends on this.
5663/// We simulate this by deserializing and passing to consumer the methods of the
5664/// implementation before passing the deserialized implementation decl.
5665static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
5666 ASTConsumer *Consumer) {
5667 assert(ImplD && Consumer);
5668
5669 for (ObjCImplDecl::method_iterator
5670 I = ImplD->meth_begin(), E = ImplD->meth_end(); I != E; ++I)
David Blaikie581deb32012-06-06 20:45:41 +00005671 Consumer->HandleInterestingDecl(DeclGroupRef(*I));
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005672
5673 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
5674}
5675
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005676void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005677 assert(Consumer);
5678 while (!InterestingDecls.empty()) {
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005679 Decl *D = InterestingDecls.front();
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005680 InterestingDecls.pop_front();
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005681
Argyrios Kyrtzidis8d39c3d2011-11-30 23:18:26 +00005682 PassInterestingDeclToConsumer(D);
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005683 }
5684}
5685
Argyrios Kyrtzidis8d39c3d2011-11-30 23:18:26 +00005686void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
5687 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
5688 PassObjCImplDeclToConsumer(ImplD, Consumer);
5689 else
5690 Consumer->HandleInterestingDecl(DeclGroupRef(D));
5691}
5692
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005693void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor0af2ca42009-04-22 19:09:20 +00005694 this->Consumer = Consumer;
5695
Douglas Gregorfdd01722009-04-14 00:24:19 +00005696 if (!Consumer)
5697 return;
5698
5699 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005700 // Force deserialization of this decl, which will cause it to be queued for
5701 // passing to the consumer.
Daniel Dunbar04a0b502009-09-17 03:06:44 +00005702 GetDecl(ExternalDefinitions[I]);
Douglas Gregorfdd01722009-04-14 00:24:19 +00005703 }
Douglas Gregor1a995dd2011-09-15 18:47:32 +00005704 ExternalDefinitions.clear();
Douglas Gregorc62a2fe2009-04-25 00:41:30 +00005705
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005706 PassInterestingDeclsToConsumer();
Douglas Gregorfdd01722009-04-14 00:24:19 +00005707}
5708
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005709void ASTReader::PrintStats() {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005710 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregor2cf26342009-04-09 22:27:44 +00005711
Mike Stump1eb44332009-09-09 15:08:12 +00005712 unsigned NumTypesLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005713 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall0953e762009-09-24 19:53:00 +00005714 QualType());
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005715 unsigned NumDeclsLoaded
5716 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
5717 (Decl *)0);
5718 unsigned NumIdentifiersLoaded
5719 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
5720 IdentifiersLoaded.end(),
5721 (IdentifierInfo *)0);
Douglas Gregora8235d62012-10-09 23:05:51 +00005722 unsigned NumMacrosLoaded
5723 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
5724 MacrosLoaded.end(),
5725 (MacroInfo *)0);
Mike Stump1eb44332009-09-09 15:08:12 +00005726 unsigned NumSelectorsLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005727 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
5728 SelectorsLoaded.end(),
5729 Selector());
Douglas Gregor2d41cc12009-04-13 20:50:16 +00005730
Douglas Gregor4fed3f42009-04-27 18:38:38 +00005731 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
5732 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor0cdd7982011-07-21 18:46:38 +00005733 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00005734 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
5735 NumSLocEntriesRead, TotalNumSLocEntries,
5736 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005737 if (!TypesLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005738 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005739 NumTypesLoaded, (unsigned)TypesLoaded.size(),
5740 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
5741 if (!DeclsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005742 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005743 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
5744 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005745 if (!IdentifiersLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005746 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005747 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
5748 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Douglas Gregora8235d62012-10-09 23:05:51 +00005749 if (!MacrosLoaded.empty())
5750 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
5751 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
5752 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
Sebastian Redl725cd962010-08-04 20:40:17 +00005753 if (!SelectorsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005754 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redl725cd962010-08-04 20:40:17 +00005755 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
5756 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor83941df2009-04-25 17:48:32 +00005757 if (TotalNumStatements)
5758 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
5759 NumStatementsRead, TotalNumStatements,
5760 ((float)NumStatementsRead/TotalNumStatements * 100));
5761 if (TotalNumMacros)
5762 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
5763 NumMacrosRead, TotalNumMacros,
5764 ((float)NumMacrosRead/TotalNumMacros * 100));
5765 if (TotalLexicalDeclContexts)
5766 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
5767 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
5768 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
5769 * 100));
5770 if (TotalVisibleDeclContexts)
5771 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
5772 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
5773 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
5774 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005775 if (TotalNumMethodPoolEntries) {
Douglas Gregor83941df2009-04-25 17:48:32 +00005776 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005777 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
5778 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor83941df2009-04-25 17:48:32 +00005779 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005780 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor83941df2009-04-25 17:48:32 +00005781 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00005782 std::fprintf(stderr, "\n");
Douglas Gregor23d7df52011-07-21 19:50:14 +00005783 dump();
5784 std::fprintf(stderr, "\n");
5785}
5786
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005787template<typename Key, typename ModuleFile, unsigned InitialCapacity>
Douglas Gregor23d7df52011-07-21 19:50:14 +00005788static void
Chris Lattner5f9e2722011-07-23 10:55:15 +00005789dumpModuleIDMap(StringRef Name,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005790 const ContinuousRangeMap<Key, ModuleFile *,
Douglas Gregor23d7df52011-07-21 19:50:14 +00005791 InitialCapacity> &Map) {
5792 if (Map.begin() == Map.end())
5793 return;
5794
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005795 typedef ContinuousRangeMap<Key, ModuleFile *, InitialCapacity> MapType;
Douglas Gregor23d7df52011-07-21 19:50:14 +00005796 llvm::errs() << Name << ":\n";
5797 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
5798 I != IEnd; ++I) {
5799 llvm::errs() << " " << I->first << " -> " << I->second->FileName
5800 << "\n";
5801 }
5802}
5803
Douglas Gregor23d7df52011-07-21 19:50:14 +00005804void ASTReader::dump() {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005805 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
Douglas Gregor8f1231b2011-07-22 06:10:01 +00005806 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
Douglas Gregor23d7df52011-07-21 19:50:14 +00005807 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
Douglas Gregor1e849b62011-07-29 00:21:44 +00005808 dumpModuleIDMap("Global type map", GlobalTypeMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00005809 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00005810 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
Douglas Gregora8235d62012-10-09 23:05:51 +00005811 dumpModuleIDMap("Global macro map", GlobalMacroMap);
Douglas Gregor26ced122011-12-01 00:59:36 +00005812 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00005813 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00005814 dumpModuleIDMap("Global preprocessed entity map",
5815 GlobalPreprocessedEntityMap);
Douglas Gregor8df5c9b2011-08-02 11:12:41 +00005816
5817 llvm::errs() << "\n*** PCH/Modules Loaded:";
5818 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
5819 MEnd = ModuleMgr.end();
5820 M != MEnd; ++M)
5821 (*M)->dump();
Douglas Gregor2cf26342009-04-09 22:27:44 +00005822}
5823
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00005824/// Return the amount of memory used by memory buffers, breaking down
5825/// by heap-backed versus mmap'ed memory.
5826void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005827 for (ModuleConstIterator I = ModuleMgr.begin(),
5828 E = ModuleMgr.end(); I != E; ++I) {
5829 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00005830 size_t bytes = buf->getBufferSize();
5831 switch (buf->getBufferKind()) {
5832 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
5833 sizes.malloc_bytes += bytes;
5834 break;
5835 case llvm::MemoryBuffer::MemoryBuffer_MMap:
5836 sizes.mmap_bytes += bytes;
5837 break;
5838 }
5839 }
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005840 }
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00005841}
5842
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005843void ASTReader::InitializeSema(Sema &S) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00005844 SemaObj = &S;
Axel Naumann0ec56b72012-10-18 19:05:02 +00005845 S.addExternalSource(this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00005846
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00005847 // Makes sure any declarations that were deserialized "too early"
5848 // still get added to the identifier's declaration chains.
Douglas Gregor76dc8892010-09-24 23:29:12 +00005849 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00005850 SemaObj->pushExternalDeclIntoScope(PreloadedDecls[I],
5851 PreloadedDecls[I]->getDeclName());
Douglas Gregor668c1a42009-04-21 22:25:48 +00005852 }
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00005853 PreloadedDecls.clear();
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00005854
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00005855 // Load the offsets of the declarations that Sema references.
5856 // They will be lazily deserialized when needed.
5857 if (!SemaDeclRefs.empty()) {
5858 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
Douglas Gregor1e5b6f62011-07-28 00:57:24 +00005859 if (!SemaObj->StdNamespace)
5860 SemaObj->StdNamespace = SemaDeclRefs[0];
5861 if (!SemaObj->StdBadAlloc)
5862 SemaObj->StdBadAlloc = SemaDeclRefs[1];
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00005863 }
5864
Peter Collingbourne84bccea2011-02-15 19:46:30 +00005865 if (!FPPragmaOptions.empty()) {
5866 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
5867 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
5868 }
5869
5870 if (!OpenCLExtensions.empty()) {
5871 unsigned I = 0;
5872#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
5873#include "clang/Basic/OpenCLExtensions.def"
5874
5875 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
5876 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00005877}
5878
Douglas Gregor211f6e82011-08-20 04:39:52 +00005879IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00005880 // Note that we are loading an identifier.
5881 Deserializing AnIdentifier(this);
5882
Douglas Gregor057df202012-01-18 20:56:22 +00005883 IdentifierLookupVisitor Visitor(StringRef(NameStart, NameEnd - NameStart),
5884 /*PriorGeneration=*/0);
Douglas Gregor211f6e82011-08-20 04:39:52 +00005885 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
Douglas Gregoreee242f2011-10-27 09:33:13 +00005886 IdentifierInfo *II = Visitor.getIdentifierInfo();
Douglas Gregor057df202012-01-18 20:56:22 +00005887 markIdentifierUpToDate(II);
Douglas Gregoreee242f2011-10-27 09:33:13 +00005888 return II;
Douglas Gregor668c1a42009-04-21 22:25:48 +00005889}
5890
Douglas Gregor95f42922010-10-14 22:11:03 +00005891namespace clang {
5892 /// \brief An identifier-lookup iterator that enumerates all of the
5893 /// identifiers stored within a set of AST files.
5894 class ASTIdentifierIterator : public IdentifierIterator {
5895 /// \brief The AST reader whose identifiers are being enumerated.
5896 const ASTReader &Reader;
5897
5898 /// \brief The current index into the chain of AST files stored in
5899 /// the AST reader.
5900 unsigned Index;
5901
5902 /// \brief The current position within the identifier lookup table
5903 /// of the current AST file.
5904 ASTIdentifierLookupTable::key_iterator Current;
5905
5906 /// \brief The end position within the identifier lookup table of
5907 /// the current AST file.
5908 ASTIdentifierLookupTable::key_iterator End;
5909
5910 public:
5911 explicit ASTIdentifierIterator(const ASTReader &Reader);
5912
Chris Lattner5f9e2722011-07-23 10:55:15 +00005913 virtual StringRef Next();
Douglas Gregor95f42922010-10-14 22:11:03 +00005914 };
5915}
5916
5917ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005918 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
Douglas Gregor95f42922010-10-14 22:11:03 +00005919 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005920 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
Douglas Gregor95f42922010-10-14 22:11:03 +00005921 Current = IdTable->key_begin();
5922 End = IdTable->key_end();
5923}
5924
Chris Lattner5f9e2722011-07-23 10:55:15 +00005925StringRef ASTIdentifierIterator::Next() {
Douglas Gregor95f42922010-10-14 22:11:03 +00005926 while (Current == End) {
5927 // If we have exhausted all of our AST files, we're done.
5928 if (Index == 0)
Chris Lattner5f9e2722011-07-23 10:55:15 +00005929 return StringRef();
Douglas Gregor95f42922010-10-14 22:11:03 +00005930
5931 --Index;
5932 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005933 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
5934 IdentifierLookupTable;
Douglas Gregor95f42922010-10-14 22:11:03 +00005935 Current = IdTable->key_begin();
5936 End = IdTable->key_end();
5937 }
5938
5939 // We have any identifiers remaining in the current AST file; return
5940 // the next one.
5941 std::pair<const char*, unsigned> Key = *Current;
5942 ++Current;
Chris Lattner5f9e2722011-07-23 10:55:15 +00005943 return StringRef(Key.first, Key.second);
Douglas Gregor95f42922010-10-14 22:11:03 +00005944}
5945
5946IdentifierIterator *ASTReader::getIdentifiers() const {
5947 return new ASTIdentifierIterator(*this);
5948}
5949
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005950namespace clang { namespace serialization {
5951 class ReadMethodPoolVisitor {
5952 ASTReader &Reader;
Douglas Gregor8efca6b2012-01-25 01:14:32 +00005953 Selector Sel;
5954 unsigned PriorGeneration;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005955 llvm::SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
5956 llvm::SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00005957
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005958 public:
Douglas Gregor8efca6b2012-01-25 01:14:32 +00005959 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
5960 unsigned PriorGeneration)
5961 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) { }
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005962
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005963 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005964 ReadMethodPoolVisitor *This
5965 = static_cast<ReadMethodPoolVisitor *>(UserData);
5966
5967 if (!M.SelectorLookupTable)
5968 return false;
5969
Douglas Gregor8efca6b2012-01-25 01:14:32 +00005970 // If we've already searched this module file, skip it now.
5971 if (M.Generation <= This->PriorGeneration)
5972 return true;
5973
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005974 ASTSelectorLookupTable *PoolTable
5975 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
5976 ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel);
5977 if (Pos == PoolTable->end())
5978 return false;
5979
5980 ++This->Reader.NumSelectorsRead;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005981 // FIXME: Not quite happy with the statistics here. We probably should
5982 // disable this tracking when called via LoadSelector.
5983 // Also, should entries without methods count as misses?
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005984 ++This->Reader.NumMethodPoolEntriesRead;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005985 ASTSelectorLookupTrait::data_type Data = *Pos;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005986 if (This->Reader.DeserializationListener)
5987 This->Reader.DeserializationListener->SelectorRead(Data.ID,
5988 This->Sel);
5989
5990 This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
5991 This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
5992 return true;
Sebastian Redl725cd962010-08-04 20:40:17 +00005993 }
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005994
5995 /// \brief Retrieve the instance methods found by this visitor.
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005996 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
5997 return InstanceMethods;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005998 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00005999
Douglas Gregor3d15ab82011-08-25 14:51:20 +00006000 /// \brief Retrieve the instance methods found by this visitor.
Douglas Gregor5ac4b692012-01-25 00:49:42 +00006001 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
6002 return FactoryMethods;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00006003 }
6004 };
6005} } // end namespace clang::serialization
6006
Douglas Gregor5ac4b692012-01-25 00:49:42 +00006007/// \brief Add the given set of methods to the method list.
6008static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
6009 ObjCMethodList &List) {
6010 for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
6011 S.addMethodToGlobalList(&List, Methods[I]);
6012 }
6013}
6014
6015void ASTReader::ReadMethodPool(Selector Sel) {
Douglas Gregor8efca6b2012-01-25 01:14:32 +00006016 // Get the selector generation and update it to the current generation.
6017 unsigned &Generation = SelectorGeneration[Sel];
6018 unsigned PriorGeneration = Generation;
6019 Generation = CurrentGeneration;
6020
6021 // Search for methods defined with this selector.
6022 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
Douglas Gregor3d15ab82011-08-25 14:51:20 +00006023 ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor);
Douglas Gregor3d15ab82011-08-25 14:51:20 +00006024
Douglas Gregor5ac4b692012-01-25 00:49:42 +00006025 if (Visitor.getInstanceMethods().empty() &&
6026 Visitor.getFactoryMethods().empty()) {
Douglas Gregor3d15ab82011-08-25 14:51:20 +00006027 ++NumMethodPoolMisses;
Douglas Gregor5ac4b692012-01-25 00:49:42 +00006028 return;
6029 }
6030
6031 if (!getSema())
6032 return;
6033
6034 Sema &S = *getSema();
6035 Sema::GlobalMethodPool::iterator Pos
6036 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
6037
6038 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
6039 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00006040}
6041
Douglas Gregord8bba9c2011-06-28 16:20:02 +00006042void ASTReader::ReadKnownNamespaces(
Chris Lattner5f9e2722011-07-23 10:55:15 +00006043 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
Douglas Gregord8bba9c2011-06-28 16:20:02 +00006044 Namespaces.clear();
6045
6046 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
6047 if (NamespaceDecl *Namespace
6048 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
6049 Namespaces.push_back(Namespace);
6050 }
6051}
6052
Douglas Gregora8623202011-07-27 20:58:46 +00006053void ASTReader::ReadTentativeDefinitions(
6054 SmallVectorImpl<VarDecl *> &TentativeDefs) {
6055 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
6056 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
6057 if (Var)
6058 TentativeDefs.push_back(Var);
6059 }
6060 TentativeDefinitions.clear();
6061}
6062
Douglas Gregora2ee20a2011-07-27 21:45:57 +00006063void ASTReader::ReadUnusedFileScopedDecls(
6064 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
6065 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
6066 DeclaratorDecl *D
6067 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
6068 if (D)
6069 Decls.push_back(D);
6070 }
6071 UnusedFileScopedDecls.clear();
6072}
6073
Douglas Gregor0129b562011-07-27 21:57:17 +00006074void ASTReader::ReadDelegatingConstructors(
6075 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
6076 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
6077 CXXConstructorDecl *D
6078 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
6079 if (D)
6080 Decls.push_back(D);
6081 }
6082 DelegatingCtorDecls.clear();
6083}
6084
Douglas Gregord58a0a52011-07-28 00:39:29 +00006085void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
6086 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
6087 TypedefNameDecl *D
6088 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
6089 if (D)
6090 Decls.push_back(D);
6091 }
6092 ExtVectorDecls.clear();
6093}
6094
Douglas Gregora126f172011-07-28 00:53:40 +00006095void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) {
6096 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
6097 CXXRecordDecl *D
6098 = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I]));
6099 if (D)
6100 Decls.push_back(D);
6101 }
6102 DynamicClasses.clear();
6103}
6104
Douglas Gregorec12ce22011-07-28 14:20:37 +00006105void
6106ASTReader::ReadLocallyScopedExternalDecls(SmallVectorImpl<NamedDecl *> &Decls) {
6107 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
6108 NamedDecl *D
6109 = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
6110 if (D)
6111 Decls.push_back(D);
6112 }
6113 LocallyScopedExternalDecls.clear();
6114}
6115
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00006116void ASTReader::ReadReferencedSelectors(
6117 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
6118 if (ReferencedSelectorsData.empty())
6119 return;
6120
6121 // If there are @selector references added them to its pool. This is for
6122 // implementation of -Wselector.
6123 unsigned int DataSize = ReferencedSelectorsData.size()-1;
6124 unsigned I = 0;
6125 while (I < DataSize) {
6126 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
6127 SourceLocation SelLoc
6128 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
6129 Sels.push_back(std::make_pair(Sel, SelLoc));
6130 }
6131 ReferencedSelectorsData.clear();
6132}
6133
Douglas Gregor31e37b22011-07-28 18:09:57 +00006134void ASTReader::ReadWeakUndeclaredIdentifiers(
6135 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
6136 if (WeakUndeclaredIdentifiers.empty())
6137 return;
6138
6139 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
6140 IdentifierInfo *WeakId
6141 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
6142 IdentifierInfo *AliasId
6143 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
6144 SourceLocation Loc
6145 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
6146 bool Used = WeakUndeclaredIdentifiers[I++];
6147 WeakInfo WI(AliasId, Loc);
6148 WI.setUsed(Used);
6149 WeakIDs.push_back(std::make_pair(WeakId, WI));
6150 }
6151 WeakUndeclaredIdentifiers.clear();
6152}
6153
Douglas Gregordfe65432011-07-28 19:11:31 +00006154void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
6155 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
6156 ExternalVTableUse VT;
6157 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
6158 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
6159 VT.DefinitionRequired = VTableUses[Idx++];
6160 VTables.push_back(VT);
6161 }
6162
6163 VTableUses.clear();
6164}
6165
Douglas Gregor6e4a3f52011-07-28 19:49:54 +00006166void ASTReader::ReadPendingInstantiations(
6167 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
6168 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
6169 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
6170 SourceLocation Loc
6171 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
Axel Naumann39d26c32012-10-02 09:09:43 +00006172
Douglas Gregore5fa3c22012-10-03 18:34:48 +00006173 Pending.push_back(std::make_pair(D, Loc));
Douglas Gregor6e4a3f52011-07-28 19:49:54 +00006174 }
6175 PendingInstantiations.clear();
6176}
6177
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006178void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redle58aa892010-08-04 18:21:41 +00006179 // It would be complicated to avoid reading the methods anyway. So don't.
6180 ReadMethodPool(Sel);
6181}
6182
Douglas Gregor95eab172011-07-28 20:55:49 +00006183void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00006184 assert(ID && "Non-zero identifier ID required");
Douglas Gregora02b1472009-04-28 21:53:25 +00006185 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00006186 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00006187 if (DeserializationListener)
6188 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregor668c1a42009-04-21 22:25:48 +00006189}
6190
Douglas Gregord89275b2009-07-06 18:54:52 +00006191/// \brief Set the globally-visible declarations associated with the given
6192/// identifier.
6193///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00006194/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump1eb44332009-09-09 15:08:12 +00006195/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregord89275b2009-07-06 18:54:52 +00006196/// them.
6197///
6198/// \param II an IdentifierInfo that refers to one or more globally-visible
6199/// declarations.
6200///
6201/// \param DeclIDs the set of declaration IDs with the name @p II that are
6202/// visible at global scope.
6203///
6204/// \param Nonrecursive should be true to indicate that the caller knows that
6205/// this call is non-recursive, and therefore the globally-visible declarations
6206/// will not be placed onto the pending queue.
Mike Stump1eb44332009-09-09 15:08:12 +00006207void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006208ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Chris Lattner5f9e2722011-07-23 10:55:15 +00006209 const SmallVectorImpl<uint32_t> &DeclIDs,
Douglas Gregord89275b2009-07-06 18:54:52 +00006210 bool Nonrecursive) {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00006211 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregord89275b2009-07-06 18:54:52 +00006212 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
6213 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
6214 PII.II = II;
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00006215 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregord89275b2009-07-06 18:54:52 +00006216 return;
6217 }
Mike Stump1eb44332009-09-09 15:08:12 +00006218
Douglas Gregord89275b2009-07-06 18:54:52 +00006219 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
6220 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
6221 if (SemaObj) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00006222 // Introduce this declaration into the translation-unit scope
6223 // and add it to the declaration chain for this identifier, so
6224 // that (unqualified) name lookup will find it.
6225 SemaObj->pushExternalDeclIntoScope(D, II);
Douglas Gregord89275b2009-07-06 18:54:52 +00006226 } else {
6227 // Queue this declaration so that it will be added to the
6228 // translation unit scope and identifier's declaration chain
6229 // once a Sema object is known.
6230 PreloadedDecls.push_back(D);
6231 }
6232 }
6233}
6234
Douglas Gregor95eab172011-07-28 20:55:49 +00006235IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00006236 if (ID == 0)
6237 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00006238
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00006239 if (IdentifiersLoaded.empty()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00006240 Error("no identifier table in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00006241 return 0;
6242 }
Mike Stump1eb44332009-09-09 15:08:12 +00006243
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00006244 ID -= 1;
6245 if (!IdentifiersLoaded[ID]) {
Douglas Gregor67268d02011-07-20 00:59:32 +00006246 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
6247 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006248 ModuleFile *M = I->second;
Douglas Gregor9827a802011-07-29 00:56:45 +00006249 unsigned Index = ID - M->BaseIdentifierID;
6250 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
Douglas Gregord6595a42009-04-25 21:04:17 +00006251
Sebastian Redl3c7f4132010-08-18 23:57:06 +00006252 // All of the strings in the AST file are preceded by a 16-bit length.
6253 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenek231bc0b2009-10-23 04:45:31 +00006254 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
6255 // unsigned integers. This is important to avoid integer overflow when
6256 // we cast them to 'unsigned'.
Ted Kremenekff1ea462009-10-23 03:57:22 +00006257 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregor02fc7512009-04-28 20:01:51 +00006258 unsigned StrLen = (((unsigned) StrLenPtr[0])
6259 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00006260 IdentifiersLoaded[ID]
Douglas Gregor712f2fc2011-09-09 22:02:16 +00006261 = &PP.getIdentifierTable().get(StringRef(Str, StrLen));
Sebastian Redlf2f0f032010-07-23 23:49:55 +00006262 if (DeserializationListener)
6263 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregorafaf3082009-04-11 00:14:32 +00006264 }
Mike Stump1eb44332009-09-09 15:08:12 +00006265
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00006266 return IdentifiersLoaded[ID];
Douglas Gregor2cf26342009-04-09 22:27:44 +00006267}
6268
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006269IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
Douglas Gregor95eab172011-07-28 20:55:49 +00006270 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
6271}
6272
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006273IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
Douglas Gregor6ec60e02011-08-03 21:49:18 +00006274 if (LocalID < NUM_PREDEF_IDENT_IDS)
6275 return LocalID;
6276
6277 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6278 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
6279 assert(I != M.IdentifierRemap.end()
6280 && "Invalid index into identifier index remap");
6281
6282 return LocalID + I->second;
Douglas Gregor95eab172011-07-28 20:55:49 +00006283}
6284
Douglas Gregor3ab50fe2012-10-11 17:41:54 +00006285MacroInfo *ASTReader::getMacro(MacroID ID, MacroInfo *Hint) {
Douglas Gregora8235d62012-10-09 23:05:51 +00006286 if (ID == 0)
6287 return 0;
6288
6289 if (MacrosLoaded.empty()) {
6290 Error("no macro table in AST file");
6291 return 0;
6292 }
6293
6294 ID -= NUM_PREDEF_MACRO_IDS;
6295 if (!MacrosLoaded[ID]) {
6296 GlobalMacroMapType::iterator I
6297 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
6298 assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
6299 ModuleFile *M = I->second;
6300 unsigned Index = ID - M->BaseMacroID;
Douglas Gregor3ab50fe2012-10-11 17:41:54 +00006301 ReadMacroRecord(*M, M->MacroOffsets[Index], Hint);
Douglas Gregora8235d62012-10-09 23:05:51 +00006302 }
6303
6304 return MacrosLoaded[ID];
6305}
6306
6307MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
6308 if (LocalID < NUM_PREDEF_MACRO_IDS)
6309 return LocalID;
6310
6311 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6312 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
6313 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
6314
6315 return LocalID + I->second;
6316}
6317
Douglas Gregor26ced122011-12-01 00:59:36 +00006318serialization::SubmoduleID
6319ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
6320 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
6321 return LocalID;
6322
6323 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6324 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
6325 assert(I != M.SubmoduleRemap.end()
Douglas Gregora8235d62012-10-09 23:05:51 +00006326 && "Invalid index into submodule index remap");
Douglas Gregor26ced122011-12-01 00:59:36 +00006327
6328 return LocalID + I->second;
6329}
6330
6331Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
6332 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
6333 assert(GlobalID == 0 && "Unhandled global submodule ID");
6334 return 0;
6335 }
6336
6337 if (GlobalID > SubmodulesLoaded.size()) {
6338 Error("submodule ID out of range in AST file");
6339 return 0;
6340 }
6341
6342 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
6343}
6344
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006345Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
Douglas Gregor2d2689a2011-07-28 21:16:51 +00006346 return DecodeSelector(getGlobalSelectorID(M, LocalID));
6347}
6348
6349Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
Steve Naroff90cd1bb2009-04-23 10:39:46 +00006350 if (ID == 0)
6351 return Selector();
Mike Stump1eb44332009-09-09 15:08:12 +00006352
Sebastian Redl725cd962010-08-04 20:40:17 +00006353 if (ID > SelectorsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00006354 Error("selector ID out of range in AST file");
Steve Naroff90cd1bb2009-04-23 10:39:46 +00006355 return Selector();
6356 }
Douglas Gregor83941df2009-04-25 17:48:32 +00006357
Sebastian Redl725cd962010-08-04 20:40:17 +00006358 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor83941df2009-04-25 17:48:32 +00006359 // Load this selector from the selector table.
Douglas Gregor96958cb2011-07-20 01:10:58 +00006360 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
6361 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006362 ModuleFile &M = *I->second;
Douglas Gregor9827a802011-07-29 00:56:45 +00006363 ASTSelectorLookupTrait Trait(*this, M);
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00006364 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
Douglas Gregor96958cb2011-07-20 01:10:58 +00006365 SelectorsLoaded[ID - 1] =
Douglas Gregor9827a802011-07-29 00:56:45 +00006366 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
Douglas Gregor96958cb2011-07-20 01:10:58 +00006367 if (DeserializationListener)
6368 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
Douglas Gregor83941df2009-04-25 17:48:32 +00006369 }
6370
Sebastian Redl725cd962010-08-04 20:40:17 +00006371 return SelectorsLoaded[ID - 1];
Steve Naroff90cd1bb2009-04-23 10:39:46 +00006372}
6373
Douglas Gregor8451ec72011-07-28 14:41:43 +00006374Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
Douglas Gregor719770d2010-04-06 17:30:22 +00006375 return DecodeSelector(ID);
6376}
6377
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006378uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redl725cd962010-08-04 20:40:17 +00006379 // ID 0 (the null selector) is considered an external selector.
6380 return getTotalNumSelectors() + 1;
Douglas Gregor719770d2010-04-06 17:30:22 +00006381}
6382
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00006383serialization::SelectorID
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006384ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00006385 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
6386 return LocalID;
6387
6388 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6389 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
6390 assert(I != M.SelectorRemap.end()
Douglas Gregora8235d62012-10-09 23:05:51 +00006391 && "Invalid index into selector index remap");
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00006392
6393 return LocalID + I->second;
Douglas Gregor8451ec72011-07-28 14:41:43 +00006394}
6395
Mike Stump1eb44332009-09-09 15:08:12 +00006396DeclarationName
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006397ASTReader::ReadDeclarationName(ModuleFile &F,
Douglas Gregor393f2492011-07-22 00:38:23 +00006398 const RecordData &Record, unsigned &Idx) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00006399 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
6400 switch (Kind) {
6401 case DeclarationName::Identifier:
Douglas Gregor95eab172011-07-28 20:55:49 +00006402 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00006403
6404 case DeclarationName::ObjCZeroArgSelector:
6405 case DeclarationName::ObjCOneArgSelector:
6406 case DeclarationName::ObjCMultiArgSelector:
Douglas Gregor2d2689a2011-07-28 21:16:51 +00006407 return DeclarationName(ReadSelector(F, Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00006408
6409 case DeclarationName::CXXConstructorName:
Douglas Gregor35942772011-09-09 21:34:22 +00006410 return Context.DeclarationNames.getCXXConstructorName(
6411 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00006412
6413 case DeclarationName::CXXDestructorName:
Douglas Gregor35942772011-09-09 21:34:22 +00006414 return Context.DeclarationNames.getCXXDestructorName(
6415 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00006416
6417 case DeclarationName::CXXConversionFunctionName:
Douglas Gregor35942772011-09-09 21:34:22 +00006418 return Context.DeclarationNames.getCXXConversionFunctionName(
6419 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00006420
6421 case DeclarationName::CXXOperatorName:
Douglas Gregor35942772011-09-09 21:34:22 +00006422 return Context.DeclarationNames.getCXXOperatorName(
Douglas Gregor2cf26342009-04-09 22:27:44 +00006423 (OverloadedOperatorKind)Record[Idx++]);
6424
Sean Hunt3e518bd2009-11-29 07:34:05 +00006425 case DeclarationName::CXXLiteralOperatorName:
Douglas Gregor35942772011-09-09 21:34:22 +00006426 return Context.DeclarationNames.getCXXLiteralOperatorName(
Douglas Gregor95eab172011-07-28 20:55:49 +00006427 GetIdentifierInfo(F, Record, Idx));
Sean Hunt3e518bd2009-11-29 07:34:05 +00006428
Douglas Gregor2cf26342009-04-09 22:27:44 +00006429 case DeclarationName::CXXUsingDirective:
6430 return DeclarationName::getUsingDirectiveName();
6431 }
6432
David Blaikie7530c032012-01-17 06:56:22 +00006433 llvm_unreachable("Invalid NameKind!");
Douglas Gregor2cf26342009-04-09 22:27:44 +00006434}
Douglas Gregor0a0428e2009-04-10 20:39:37 +00006435
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006436void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006437 DeclarationNameLoc &DNLoc,
6438 DeclarationName Name,
6439 const RecordData &Record, unsigned &Idx) {
6440 switch (Name.getNameKind()) {
6441 case DeclarationName::CXXConstructorName:
6442 case DeclarationName::CXXDestructorName:
6443 case DeclarationName::CXXConversionFunctionName:
6444 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
6445 break;
6446
6447 case DeclarationName::CXXOperatorName:
6448 DNLoc.CXXOperatorName.BeginOpNameLoc
6449 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6450 DNLoc.CXXOperatorName.EndOpNameLoc
6451 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6452 break;
6453
6454 case DeclarationName::CXXLiteralOperatorName:
6455 DNLoc.CXXLiteralOperatorName.OpNameLoc
6456 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6457 break;
6458
6459 case DeclarationName::Identifier:
6460 case DeclarationName::ObjCZeroArgSelector:
6461 case DeclarationName::ObjCOneArgSelector:
6462 case DeclarationName::ObjCMultiArgSelector:
6463 case DeclarationName::CXXUsingDirective:
6464 break;
6465 }
6466}
6467
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006468void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006469 DeclarationNameInfo &NameInfo,
6470 const RecordData &Record, unsigned &Idx) {
Douglas Gregor393f2492011-07-22 00:38:23 +00006471 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006472 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
6473 DeclarationNameLoc DNLoc;
6474 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
6475 NameInfo.setInfo(DNLoc);
6476}
6477
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006478void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006479 const RecordData &Record, unsigned &Idx) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00006480 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006481 unsigned NumTPLists = Record[Idx++];
6482 Info.NumTemplParamLists = NumTPLists;
6483 if (NumTPLists) {
Douglas Gregor35942772011-09-09 21:34:22 +00006484 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006485 for (unsigned i=0; i != NumTPLists; ++i)
6486 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
6487 }
6488}
6489
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006490TemplateName
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006491ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006492 unsigned &Idx) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00006493 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006494 switch (Kind) {
6495 case TemplateName::Template:
Douglas Gregor409448c2011-07-21 22:35:25 +00006496 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006497
6498 case TemplateName::OverloadedTemplate: {
6499 unsigned size = Record[Idx++];
6500 UnresolvedSet<8> Decls;
6501 while (size--)
Douglas Gregor409448c2011-07-21 22:35:25 +00006502 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006503
Douglas Gregor35942772011-09-09 21:34:22 +00006504 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006505 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006506
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006507 case TemplateName::QualifiedTemplate: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006508 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006509 bool hasTemplKeyword = Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00006510 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006511 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006512 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006513
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006514 case TemplateName::DependentTemplate: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006515 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006516 if (Record[Idx++]) // isIdentifier
Douglas Gregor35942772011-09-09 21:34:22 +00006517 return Context.getDependentTemplateName(NNS,
Douglas Gregor95eab172011-07-28 20:55:49 +00006518 GetIdentifierInfo(F, Record,
6519 Idx));
Douglas Gregor35942772011-09-09 21:34:22 +00006520 return Context.getDependentTemplateName(NNS,
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00006521 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006522 }
John McCall14606042011-06-30 08:33:18 +00006523
6524 case TemplateName::SubstTemplateTemplateParm: {
6525 TemplateTemplateParmDecl *param
Douglas Gregor409448c2011-07-21 22:35:25 +00006526 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
John McCall14606042011-06-30 08:33:18 +00006527 if (!param) return TemplateName();
6528 TemplateName replacement = ReadTemplateName(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006529 return Context.getSubstTemplateTemplateParm(param, replacement);
John McCall14606042011-06-30 08:33:18 +00006530 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006531
6532 case TemplateName::SubstTemplateTemplateParmPack: {
6533 TemplateTemplateParmDecl *Param
Douglas Gregor409448c2011-07-21 22:35:25 +00006534 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006535 if (!Param)
6536 return TemplateName();
6537
6538 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
6539 if (ArgPack.getKind() != TemplateArgument::Pack)
6540 return TemplateName();
6541
Douglas Gregor35942772011-09-09 21:34:22 +00006542 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006543 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006544 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006545
David Blaikieb219cfc2011-09-23 05:06:16 +00006546 llvm_unreachable("Unhandled template name kind!");
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006547}
6548
6549TemplateArgument
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006550ASTReader::ReadTemplateArgument(ModuleFile &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00006551 const RecordData &Record, unsigned &Idx) {
Douglas Gregora7fc9012011-01-05 18:58:31 +00006552 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
6553 switch (Kind) {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006554 case TemplateArgument::Null:
6555 return TemplateArgument();
6556 case TemplateArgument::Type:
Douglas Gregor393f2492011-07-22 00:38:23 +00006557 return TemplateArgument(readType(F, Record, Idx));
Eli Friedmand7a6b162012-09-26 02:36:12 +00006558 case TemplateArgument::Declaration: {
6559 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
6560 bool ForReferenceParam = Record[Idx++];
6561 return TemplateArgument(D, ForReferenceParam);
6562 }
6563 case TemplateArgument::NullPtr:
6564 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00006565 case TemplateArgument::Integral: {
6566 llvm::APSInt Value = ReadAPSInt(Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00006567 QualType T = readType(F, Record, Idx);
Benjamin Kramer85524372012-06-07 15:09:51 +00006568 return TemplateArgument(Context, Value, T);
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00006569 }
Douglas Gregora7fc9012011-01-05 18:58:31 +00006570 case TemplateArgument::Template:
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006571 return TemplateArgument(ReadTemplateName(F, Record, Idx));
Douglas Gregora7fc9012011-01-05 18:58:31 +00006572 case TemplateArgument::TemplateExpansion: {
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006573 TemplateName Name = ReadTemplateName(F, Record, Idx);
Douglas Gregor2be29f42011-01-14 23:41:42 +00006574 llvm::Optional<unsigned> NumTemplateExpansions;
6575 if (unsigned NumExpansions = Record[Idx++])
6576 NumTemplateExpansions = NumExpansions - 1;
6577 return TemplateArgument(Name, NumTemplateExpansions);
Douglas Gregorba68eca2011-01-05 17:40:24 +00006578 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006579 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00006580 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006581 case TemplateArgument::Pack: {
6582 unsigned NumArgs = Record[Idx++];
Douglas Gregor35942772011-09-09 21:34:22 +00006583 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
Douglas Gregor910f8002010-11-07 23:05:16 +00006584 for (unsigned I = 0; I != NumArgs; ++I)
6585 Args[I] = ReadTemplateArgument(F, Record, Idx);
6586 return TemplateArgument(Args, NumArgs);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006587 }
6588 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006589
David Blaikieb219cfc2011-09-23 05:06:16 +00006590 llvm_unreachable("Unhandled template argument kind!");
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006591}
6592
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006593TemplateParameterList *
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006594ASTReader::ReadTemplateParameterList(ModuleFile &F,
Sebastian Redlc3632732010-10-05 15:59:54 +00006595 const RecordData &Record, unsigned &Idx) {
6596 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
6597 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
6598 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006599
6600 unsigned NumParams = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00006601 SmallVector<NamedDecl *, 16> Params;
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006602 Params.reserve(NumParams);
6603 while (NumParams--)
Douglas Gregor409448c2011-07-21 22:35:25 +00006604 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
Michael J. Spencer20249a12010-10-21 03:16:25 +00006605
6606 TemplateParameterList* TemplateParams =
Douglas Gregor35942772011-09-09 21:34:22 +00006607 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006608 Params.data(), Params.size(), RAngleLoc);
6609 return TemplateParams;
6610}
6611
6612void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006613ASTReader::
Chris Lattner5f9e2722011-07-23 10:55:15 +00006614ReadTemplateArgumentList(SmallVector<TemplateArgument, 8> &TemplArgs,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006615 ModuleFile &F, const RecordData &Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00006616 unsigned &Idx) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006617 unsigned NumTemplateArgs = Record[Idx++];
6618 TemplArgs.reserve(NumTemplateArgs);
6619 while (NumTemplateArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00006620 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006621}
6622
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00006623/// \brief Read a UnresolvedSet structure.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006624void ASTReader::ReadUnresolvedSet(ModuleFile &F, UnresolvedSetImpl &Set,
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00006625 const RecordData &Record, unsigned &Idx) {
6626 unsigned NumDecls = Record[Idx++];
6627 while (NumDecls--) {
Douglas Gregor409448c2011-07-21 22:35:25 +00006628 NamedDecl *D = ReadDeclAs<NamedDecl>(F, Record, Idx);
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00006629 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
6630 Set.addDecl(D, AS);
6631 }
6632}
6633
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00006634CXXBaseSpecifier
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006635ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
Nick Lewycky56062202010-07-26 16:56:01 +00006636 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00006637 bool isVirtual = static_cast<bool>(Record[Idx++]);
6638 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
6639 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redlf677ea32011-02-05 19:23:19 +00006640 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00006641 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
6642 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00006643 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redlf677ea32011-02-05 19:23:19 +00006644 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
Douglas Gregorf90b27a2011-01-03 22:36:02 +00006645 EllipsisLoc);
Sebastian Redlf677ea32011-02-05 19:23:19 +00006646 Result.setInheritConstructors(inheritConstructors);
6647 return Result;
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00006648}
6649
Sean Huntcbb67482011-01-08 20:30:50 +00006650std::pair<CXXCtorInitializer **, unsigned>
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006651ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
Sean Huntcbb67482011-01-08 20:30:50 +00006652 unsigned &Idx) {
6653 CXXCtorInitializer **CtorInitializers = 0;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006654 unsigned NumInitializers = Record[Idx++];
6655 if (NumInitializers) {
Sean Huntcbb67482011-01-08 20:30:50 +00006656 CtorInitializers
Douglas Gregor35942772011-09-09 21:34:22 +00006657 = new (Context) CXXCtorInitializer*[NumInitializers];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006658 for (unsigned i=0; i != NumInitializers; ++i) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006659 TypeSourceInfo *TInfo = 0;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006660 bool IsBaseVirtual = false;
6661 FieldDecl *Member = 0;
Francois Pichet00eb3f92010-12-04 09:14:42 +00006662 IndirectFieldDecl *IndirectMember = 0;
Michael J. Spencer20249a12010-10-21 03:16:25 +00006663
Sean Hunt156b6402011-05-04 01:19:08 +00006664 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
6665 switch (Type) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006666 case CTOR_INITIALIZER_BASE:
6667 TInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006668 IsBaseVirtual = Record[Idx++];
Sean Hunt156b6402011-05-04 01:19:08 +00006669 break;
Douglas Gregor76852c22011-11-01 01:16:03 +00006670
6671 case CTOR_INITIALIZER_DELEGATING:
6672 TInfo = GetTypeSourceInfo(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00006673 break;
6674
6675 case CTOR_INITIALIZER_MEMBER:
Douglas Gregor409448c2011-07-21 22:35:25 +00006676 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00006677 break;
6678
6679 case CTOR_INITIALIZER_INDIRECT_MEMBER:
Douglas Gregor409448c2011-07-21 22:35:25 +00006680 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00006681 break;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006682 }
Sean Hunt156b6402011-05-04 01:19:08 +00006683
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00006684 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redlc3632732010-10-05 15:59:54 +00006685 Expr *Init = ReadExpr(F);
Sebastian Redlc3632732010-10-05 15:59:54 +00006686 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
6687 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006688 bool IsWritten = Record[Idx++];
6689 unsigned SourceOrderOrNumArrayIndices;
Chris Lattner5f9e2722011-07-23 10:55:15 +00006690 SmallVector<VarDecl *, 8> Indices;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006691 if (IsWritten) {
6692 SourceOrderOrNumArrayIndices = Record[Idx++];
6693 } else {
6694 SourceOrderOrNumArrayIndices = Record[Idx++];
6695 Indices.reserve(SourceOrderOrNumArrayIndices);
6696 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
Douglas Gregor409448c2011-07-21 22:35:25 +00006697 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006698 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006699
Sean Huntcbb67482011-01-08 20:30:50 +00006700 CXXCtorInitializer *BOMInit;
Sean Hunt156b6402011-05-04 01:19:08 +00006701 if (Type == CTOR_INITIALIZER_BASE) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006702 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, IsBaseVirtual,
Sean Huntcbb67482011-01-08 20:30:50 +00006703 LParenLoc, Init, RParenLoc,
6704 MemberOrEllipsisLoc);
Sean Hunt156b6402011-05-04 01:19:08 +00006705 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006706 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, LParenLoc,
6707 Init, RParenLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006708 } else if (IsWritten) {
Francois Pichet00eb3f92010-12-04 09:14:42 +00006709 if (Member)
Douglas Gregor35942772011-09-09 21:34:22 +00006710 BOMInit = new (Context) CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc,
Sean Huntcbb67482011-01-08 20:30:50 +00006711 LParenLoc, Init, RParenLoc);
Francois Pichet00eb3f92010-12-04 09:14:42 +00006712 else
Douglas Gregor35942772011-09-09 21:34:22 +00006713 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
Sean Huntcbb67482011-01-08 20:30:50 +00006714 MemberOrEllipsisLoc, LParenLoc,
6715 Init, RParenLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006716 } else {
Douglas Gregor35942772011-09-09 21:34:22 +00006717 BOMInit = CXXCtorInitializer::Create(Context, Member, MemberOrEllipsisLoc,
Sean Huntcbb67482011-01-08 20:30:50 +00006718 LParenLoc, Init, RParenLoc,
6719 Indices.data(), Indices.size());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006720 }
6721
Argyrios Kyrtzidisf84cde12010-09-06 19:04:27 +00006722 if (IsWritten)
6723 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Sean Huntcbb67482011-01-08 20:30:50 +00006724 CtorInitializers[i] = BOMInit;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006725 }
6726 }
6727
Sean Huntcbb67482011-01-08 20:30:50 +00006728 return std::make_pair(CtorInitializers, NumInitializers);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006729}
6730
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006731NestedNameSpecifier *
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006732ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00006733 const RecordData &Record, unsigned &Idx) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006734 unsigned N = Record[Idx++];
6735 NestedNameSpecifier *NNS = 0, *Prev = 0;
6736 for (unsigned I = 0; I != N; ++I) {
6737 NestedNameSpecifier::SpecifierKind Kind
6738 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
6739 switch (Kind) {
6740 case NestedNameSpecifier::Identifier: {
Douglas Gregor95eab172011-07-28 20:55:49 +00006741 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006742 NNS = NestedNameSpecifier::Create(Context, Prev, II);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006743 break;
6744 }
6745
6746 case NestedNameSpecifier::Namespace: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006747 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006748 NNS = NestedNameSpecifier::Create(Context, Prev, NS);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006749 break;
6750 }
6751
Douglas Gregor14aba762011-02-24 02:36:08 +00006752 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006753 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006754 NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
Douglas Gregor14aba762011-02-24 02:36:08 +00006755 break;
6756 }
6757
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006758 case NestedNameSpecifier::TypeSpec:
6759 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregor393f2492011-07-22 00:38:23 +00006760 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
Douglas Gregor1ab55e92010-12-10 17:03:06 +00006761 if (!T)
6762 return 0;
6763
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006764 bool Template = Record[Idx++];
Douglas Gregor35942772011-09-09 21:34:22 +00006765 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006766 break;
6767 }
6768
6769 case NestedNameSpecifier::Global: {
Douglas Gregor35942772011-09-09 21:34:22 +00006770 NNS = NestedNameSpecifier::GlobalSpecifier(Context);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006771 // No associated value, and there can't be a prefix.
6772 break;
6773 }
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006774 }
Argyrios Kyrtzidisd2bb2c02010-07-07 15:46:30 +00006775 Prev = NNS;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006776 }
6777 return NNS;
6778}
6779
Douglas Gregordc355712011-02-25 00:36:19 +00006780NestedNameSpecifierLoc
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006781ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
Douglas Gregordc355712011-02-25 00:36:19 +00006782 unsigned &Idx) {
6783 unsigned N = Record[Idx++];
Douglas Gregor5f791bb2011-02-28 23:58:31 +00006784 NestedNameSpecifierLocBuilder Builder;
Douglas Gregordc355712011-02-25 00:36:19 +00006785 for (unsigned I = 0; I != N; ++I) {
6786 NestedNameSpecifier::SpecifierKind Kind
6787 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
6788 switch (Kind) {
6789 case NestedNameSpecifier::Identifier: {
Douglas Gregor95eab172011-07-28 20:55:49 +00006790 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00006791 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006792 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00006793 break;
6794 }
6795
6796 case NestedNameSpecifier::Namespace: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006797 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00006798 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006799 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00006800 break;
6801 }
6802
6803 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006804 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00006805 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006806 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00006807 break;
6808 }
6809
6810 case NestedNameSpecifier::TypeSpec:
6811 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregordc355712011-02-25 00:36:19 +00006812 bool Template = Record[Idx++];
6813 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
6814 if (!T)
6815 return NestedNameSpecifierLoc();
Douglas Gregordc355712011-02-25 00:36:19 +00006816 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor5f791bb2011-02-28 23:58:31 +00006817
6818 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
Douglas Gregor35942772011-09-09 21:34:22 +00006819 Builder.Extend(Context,
Douglas Gregor5f791bb2011-02-28 23:58:31 +00006820 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
6821 T->getTypeLoc(), ColonColonLoc);
Douglas Gregordc355712011-02-25 00:36:19 +00006822 break;
6823 }
6824
6825 case NestedNameSpecifier::Global: {
Douglas Gregordc355712011-02-25 00:36:19 +00006826 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006827 Builder.MakeGlobal(Context, ColonColonLoc);
Douglas Gregordc355712011-02-25 00:36:19 +00006828 break;
6829 }
6830 }
Douglas Gregordc355712011-02-25 00:36:19 +00006831 }
6832
Douglas Gregor35942772011-09-09 21:34:22 +00006833 return Builder.getWithLocInContext(Context);
Douglas Gregordc355712011-02-25 00:36:19 +00006834}
6835
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006836SourceRange
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006837ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00006838 unsigned &Idx) {
6839 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
6840 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar8ee59392010-06-02 15:47:10 +00006841 return SourceRange(beg, end);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006842}
6843
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00006844/// \brief Read an integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006845llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00006846 unsigned BitWidth = Record[Idx++];
6847 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
6848 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
6849 Idx += NumWords;
6850 return Result;
6851}
6852
6853/// \brief Read a signed integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006854llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00006855 bool isUnsigned = Record[Idx++];
6856 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
6857}
6858
Douglas Gregor17fc2232009-04-14 21:55:33 +00006859/// \brief Read a floating-point value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006860llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00006861 return llvm::APFloat(ReadAPInt(Record, Idx));
6862}
6863
Douglas Gregor68a2eb02009-04-15 21:30:51 +00006864// \brief Read a string
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006865std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00006866 unsigned Len = Record[Idx++];
Jay Foadbeaaccd2009-05-21 09:52:38 +00006867 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00006868 Idx += Len;
6869 return Result;
6870}
6871
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00006872VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
6873 unsigned &Idx) {
6874 unsigned Major = Record[Idx++];
6875 unsigned Minor = Record[Idx++];
6876 unsigned Subminor = Record[Idx++];
6877 if (Minor == 0)
6878 return VersionTuple(Major);
6879 if (Subminor == 0)
6880 return VersionTuple(Major, Minor - 1);
6881 return VersionTuple(Major, Minor - 1, Subminor - 1);
6882}
6883
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006884CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00006885 const RecordData &Record,
Chris Lattnerd2598362010-05-10 00:25:06 +00006886 unsigned &Idx) {
Douglas Gregor409448c2011-07-21 22:35:25 +00006887 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006888 return CXXTemporary::Create(Context, Decl);
Chris Lattnerd2598362010-05-10 00:25:06 +00006889}
6890
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006891DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00006892 return Diag(SourceLocation(), DiagID);
6893}
6894
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006895DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00006896 return Diags.Report(Loc, DiagID);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00006897}
Douglas Gregor025452f2009-04-17 00:04:06 +00006898
Douglas Gregor668c1a42009-04-21 22:25:48 +00006899/// \brief Retrieve the identifier table associated with the
6900/// preprocessor.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006901IdentifierTable &ASTReader::getIdentifierTable() {
Douglas Gregor712f2fc2011-09-09 22:02:16 +00006902 return PP.getIdentifierTable();
Douglas Gregor668c1a42009-04-21 22:25:48 +00006903}
6904
Douglas Gregor025452f2009-04-17 00:04:06 +00006905/// \brief Record that the given ID maps to the given switch-case
6906/// statement.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006907void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00006908 assert((*CurrSwitchCaseStmts)[ID] == 0 &&
6909 "Already have a SwitchCase with this ID");
6910 (*CurrSwitchCaseStmts)[ID] = SC;
Douglas Gregor025452f2009-04-17 00:04:06 +00006911}
6912
6913/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006914SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00006915 assert((*CurrSwitchCaseStmts)[ID] != 0 && "No SwitchCase with this ID");
6916 return (*CurrSwitchCaseStmts)[ID];
Douglas Gregor025452f2009-04-17 00:04:06 +00006917}
Douglas Gregor1de05fe2009-04-17 18:18:49 +00006918
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00006919void ASTReader::ClearSwitchCaseIDs() {
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00006920 CurrSwitchCaseStmts->clear();
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00006921}
6922
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00006923void ASTReader::ReadComments() {
Dmitri Gribenko811c8202012-07-06 18:19:34 +00006924 std::vector<RawComment *> Comments;
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00006925 for (SmallVectorImpl<std::pair<llvm::BitstreamCursor,
6926 serialization::ModuleFile *> >::iterator
6927 I = CommentsCursors.begin(),
6928 E = CommentsCursors.end();
6929 I != E; ++I) {
6930 llvm::BitstreamCursor &Cursor = I->first;
6931 serialization::ModuleFile &F = *I->second;
6932 SavedStreamPosition SavedPosition(Cursor);
6933
6934 RecordData Record;
6935 while (true) {
6936 unsigned Code = Cursor.ReadCode();
6937 if (Code == llvm::bitc::END_BLOCK)
6938 break;
6939
6940 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
6941 // No known subblocks, always skip them.
6942 Cursor.ReadSubBlockID();
6943 if (Cursor.SkipBlock()) {
6944 Error("malformed block record in AST file");
6945 return;
6946 }
6947 continue;
6948 }
6949
6950 if (Code == llvm::bitc::DEFINE_ABBREV) {
6951 Cursor.ReadAbbrevRecord();
6952 continue;
6953 }
6954
6955 // Read a record.
6956 Record.clear();
6957 switch ((CommentRecordTypes) Cursor.ReadRecord(Code, Record)) {
Chandler Carruth13691bb2012-06-20 06:47:54 +00006958 case COMMENTS_RAW_COMMENT: {
6959 unsigned Idx = 0;
6960 SourceRange SR = ReadSourceRange(F, Record, Idx);
6961 RawComment::CommentKind Kind =
6962 (RawComment::CommentKind) Record[Idx++];
6963 bool IsTrailingComment = Record[Idx++];
6964 bool IsAlmostTrailingComment = Record[Idx++];
Dmitri Gribenko811c8202012-07-06 18:19:34 +00006965 Comments.push_back(new (Context) RawComment(SR, Kind,
6966 IsTrailingComment,
6967 IsAlmostTrailingComment));
Chandler Carruth13691bb2012-06-20 06:47:54 +00006968 break;
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00006969 }
6970 }
6971 }
6972 }
6973 Context.Comments.addCommentsToFront(Comments);
6974}
6975
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006976void ASTReader::finishPendingActions() {
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00006977 while (!PendingIdentifierInfos.empty() || !PendingDeclChains.empty() ||
6978 !PendingMacroIDs.empty()) {
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006979 // If any identifiers with corresponding top-level declarations have
6980 // been loaded, load those declarations now.
6981 while (!PendingIdentifierInfos.empty()) {
6982 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
6983 PendingIdentifierInfos.front().DeclIDs, true);
6984 PendingIdentifierInfos.pop_front();
6985 }
6986
Douglas Gregora1be2782011-12-17 23:38:30 +00006987 // Load pending declaration chains.
6988 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) {
6989 loadPendingDeclChain(PendingDeclChains[I]);
Douglas Gregor56ca8a92012-01-17 19:21:53 +00006990 PendingDeclChainsKnown.erase(PendingDeclChains[I]);
Douglas Gregora1be2782011-12-17 23:38:30 +00006991 }
6992 PendingDeclChains.clear();
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00006993
6994 // Load any pending macro definitions.
Douglas Gregore9652bf2012-10-11 17:31:34 +00006995 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
6996 // FIXME: std::move here
6997 SmallVector<MacroID, 2> GlobalIDs = PendingMacroIDs.begin()[I].second;
Douglas Gregor3ab50fe2012-10-11 17:41:54 +00006998 MacroInfo *Hint = 0;
Douglas Gregore9652bf2012-10-11 17:31:34 +00006999 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
7000 ++IDIdx) {
Douglas Gregor3ab50fe2012-10-11 17:41:54 +00007001 Hint = getMacro(GlobalIDs[IDIdx], Hint);
Douglas Gregore9652bf2012-10-11 17:31:34 +00007002 }
7003 }
7004 PendingMacroIDs.clear();
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00007005 }
Douglas Gregorfc529f72011-12-19 19:00:47 +00007006
Douglas Gregor7c99bb5c2012-01-14 15:13:49 +00007007 // If we deserialized any C++ or Objective-C class definitions, any
7008 // Objective-C protocol definitions, or any redeclarable templates, make sure
7009 // that all redeclarations point to the definitions. Note that this can only
7010 // happen now, after the redeclaration chains have been fully wired.
Douglas Gregorfc529f72011-12-19 19:00:47 +00007011 for (llvm::SmallPtrSet<Decl *, 4>::iterator D = PendingDefinitions.begin(),
7012 DEnd = PendingDefinitions.end();
7013 D != DEnd; ++D) {
Douglas Gregor56ca8a92012-01-17 19:21:53 +00007014 if (TagDecl *TD = dyn_cast<TagDecl>(*D)) {
7015 if (const TagType *TagT = dyn_cast<TagType>(TD->TypeForDecl)) {
7016 // Make sure that the TagType points at the definition.
7017 const_cast<TagType*>(TagT)->decl = TD;
7018 }
Douglas Gregorfc529f72011-12-19 19:00:47 +00007019
Douglas Gregor56ca8a92012-01-17 19:21:53 +00007020 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(*D)) {
7021 for (CXXRecordDecl::redecl_iterator R = RD->redecls_begin(),
7022 REnd = RD->redecls_end();
7023 R != REnd; ++R)
7024 cast<CXXRecordDecl>(*R)->DefinitionData = RD->DefinitionData;
7025
7026 }
7027
Douglas Gregorfc529f72011-12-19 19:00:47 +00007028 continue;
7029 }
7030
Douglas Gregor1d784b22012-01-01 19:51:50 +00007031 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(*D)) {
Douglas Gregor56ca8a92012-01-17 19:21:53 +00007032 // Make sure that the ObjCInterfaceType points at the definition.
7033 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
7034 ->Decl = ID;
7035
Douglas Gregor1d784b22012-01-01 19:51:50 +00007036 for (ObjCInterfaceDecl::redecl_iterator R = ID->redecls_begin(),
7037 REnd = ID->redecls_end();
7038 R != REnd; ++R)
7039 R->Data = ID->Data;
7040
7041 continue;
7042 }
7043
Douglas Gregor7c99bb5c2012-01-14 15:13:49 +00007044 if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(*D)) {
7045 for (ObjCProtocolDecl::redecl_iterator R = PD->redecls_begin(),
7046 REnd = PD->redecls_end();
7047 R != REnd; ++R)
7048 R->Data = PD->Data;
7049
7050 continue;
7051 }
7052
7053 RedeclarableTemplateDecl *RTD
7054 = cast<RedeclarableTemplateDecl>(*D)->getCanonicalDecl();
7055 for (RedeclarableTemplateDecl::redecl_iterator R = RTD->redecls_begin(),
7056 REnd = RTD->redecls_end();
Douglas Gregorfc529f72011-12-19 19:00:47 +00007057 R != REnd; ++R)
Douglas Gregor5456b0fe2012-10-09 17:21:28 +00007058 R->Common = RTD->Common;
Douglas Gregorfc529f72011-12-19 19:00:47 +00007059 }
7060 PendingDefinitions.clear();
Douglas Gregor5456b0fe2012-10-09 17:21:28 +00007061
7062 // Load the bodies of any functions or methods we've encountered. We do
7063 // this now (delayed) so that we can be sure that the declaration chains
7064 // have been fully wired up.
Douglas Gregorce12d2f2012-10-09 17:50:23 +00007065 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
7066 PBEnd = PendingBodies.end();
Douglas Gregor5456b0fe2012-10-09 17:21:28 +00007067 PB != PBEnd; ++PB) {
7068 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
7069 // FIXME: Check for =delete/=default?
7070 // FIXME: Complain about ODR violations here?
7071 if (!getContext().getLangOpts().Modules || !FD->hasBody())
7072 FD->setLazyBody(PB->second);
7073 continue;
7074 }
7075
7076 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
7077 if (!getContext().getLangOpts().Modules || !MD->hasBody())
7078 MD->setLazyBody(PB->second);
7079 }
7080 PendingBodies.clear();
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00007081}
7082
Sebastian Redlc43b54c2010-08-18 23:56:43 +00007083void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00007084 assert(NumCurrentElementsDeserializing &&
7085 "FinishedDeserializing not paired with StartedDeserializing");
7086 if (NumCurrentElementsDeserializing == 1) {
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00007087 // We decrease NumCurrentElementsDeserializing only after pending actions
7088 // are finished, to avoid recursively re-calling finishPendingActions().
7089 finishPendingActions();
7090 }
7091 --NumCurrentElementsDeserializing;
Argyrios Kyrtzidis71168332011-12-17 04:13:28 +00007092
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00007093 if (NumCurrentElementsDeserializing == 0 &&
7094 Consumer && !PassingDeclsToConsumer) {
7095 // Guard variable to avoid recursively redoing the process of passing
7096 // decls to consumer.
7097 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
7098 true);
Argyrios Kyrtzidis71168332011-12-17 04:13:28 +00007099
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00007100 while (!InterestingDecls.empty()) {
Argyrios Kyrtzidis8d39c3d2011-11-30 23:18:26 +00007101 // We are not in recursive loading, so it's safe to pass the "interesting"
7102 // decls to the consumer.
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00007103 Decl *D = InterestingDecls.front();
7104 InterestingDecls.pop_front();
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00007105 PassInterestingDeclToConsumer(D);
7106 }
Douglas Gregord89275b2009-07-06 18:54:52 +00007107 }
Douglas Gregord89275b2009-07-06 18:54:52 +00007108}
Douglas Gregor501c1032010-08-19 00:28:17 +00007109
Douglas Gregorf8a1e512011-09-02 00:26:20 +00007110ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context,
Douglas Gregor832d6202011-07-22 16:35:34 +00007111 StringRef isysroot, bool DisableValidation,
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00007112 bool DisableStatCache, bool AllowASTWithCompilerErrors)
Sebastian Redle1dde812010-08-24 00:50:04 +00007113 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
7114 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
Douglas Gregor712f2fc2011-09-09 22:02:16 +00007115 Diags(PP.getDiagnostics()), SemaObj(0), PP(PP), Context(Context),
Argyrios Kyrtzidisd64c26f2012-10-03 01:58:42 +00007116 Consumer(0), ModuleMgr(PP.getFileManager()),
Douglas Gregorcaed0602012-10-18 21:31:35 +00007117 isysroot(isysroot), DisableValidation(DisableValidation),
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00007118 DisableStatCache(DisableStatCache),
7119 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00007120 CurrentGeneration(0), CurrSwitchCaseStmts(&SwitchCaseStmts),
7121 NumStatHits(0), NumStatMisses(0),
Douglas Gregorf62d43d2011-07-19 16:10:42 +00007122 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00007123 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
7124 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
7125 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
7126 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
Jonathan D. Turner1da90142011-07-21 21:15:19 +00007127 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
7128 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00007129 PassingDeclsToConsumer(false),
Jonathan D. Turner1da90142011-07-21 21:15:19 +00007130 NumCXXBaseSpecifiersLoaded(0)
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00007131{
Douglas Gregorf62d43d2011-07-19 16:10:42 +00007132 SourceMgr.setExternalSLocEntrySource(this);
Sebastian Redle1dde812010-08-24 00:50:04 +00007133}
7134
Sebastian Redle1dde812010-08-24 00:50:04 +00007135ASTReader::~ASTReader() {
Sebastian Redle1dde812010-08-24 00:50:04 +00007136 for (DeclContextVisibleUpdatesPending::iterator
7137 I = PendingVisibleUpdates.begin(),
7138 E = PendingVisibleUpdates.end();
7139 I != E; ++I) {
7140 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
7141 F = I->second.end();
7142 J != F; ++J)
Benjamin Kramerb1758c62012-04-15 12:36:49 +00007143 delete J->first;
Sebastian Redle1dde812010-08-24 00:50:04 +00007144 }
7145}