blob: ccadfbd07ad62d46dfacca31e9b72a1a62066b48 [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"
Douglas Gregora71a7d82012-10-24 20:05:57 +000033#include "clang/Lex/PreprocessorOptions.h"
Steve Naroff83d63c72009-04-24 20:03:17 +000034#include "clang/Lex/HeaderSearch.h"
Douglas Gregorbbf38312012-10-24 16:50:34 +000035#include "clang/Lex/HeaderSearchOptions.h"
Douglas Gregor668c1a42009-04-21 22:25:48 +000036#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000037#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000038#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000039#include "clang/Basic/FileManager.h"
Chris Lattner10e286a2010-11-23 19:19:34 +000040#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000041#include "clang/Basic/TargetInfo.h"
Douglas Gregor57016dd2012-10-16 23:40:58 +000042#include "clang/Basic/TargetOptions.h"
Douglas Gregor445e23e2009-10-05 21:07:28 +000043#include "clang/Basic/Version.h"
Douglas Gregor0a0d2b12011-03-23 00:50:03 +000044#include "clang/Basic/VersionTuple.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000045#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000046#include "llvm/Bitcode/BitstreamReader.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000047#include "llvm/Support/MemoryBuffer.h"
John McCall833ca992009-10-29 08:12:44 +000048#include "llvm/Support/ErrorHandling.h"
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000049#include "llvm/Support/FileSystem.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000050#include "llvm/Support/Path.h"
Nick Lewyckyb346d2f2012-04-16 02:51:46 +000051#include "llvm/Support/SaveAndRestore.h"
Michael J. Spencer3a321e22010-12-09 17:36:38 +000052#include "llvm/Support/system_error.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000053#include <algorithm>
Douglas Gregore721f952009-04-28 18:58:38 +000054#include <iterator>
Douglas Gregor2cf26342009-04-09 22:27:44 +000055#include <cstdio>
Douglas Gregor4fed3f42009-04-27 18:38:38 +000056#include <sys/stat.h>
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000057
Douglas Gregor2cf26342009-04-09 22:27:44 +000058using namespace clang;
Sebastian Redl8538e8d2010-08-18 23:57:32 +000059using namespace clang::serialization;
Douglas Gregor98339b92011-08-25 20:47:51 +000060using namespace clang::serialization::reader;
Douglas Gregor2cf26342009-04-09 22:27:44 +000061
62//===----------------------------------------------------------------------===//
Sebastian Redl3c7f4132010-08-18 23:57:06 +000063// PCH validator implementation
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000064//===----------------------------------------------------------------------===//
65
Sebastian Redl571db7f2010-08-18 23:56:56 +000066ASTReaderListener::~ASTReaderListener() {}
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000067
Douglas Gregor27ffa6c2012-10-23 06:18:24 +000068/// \brief Compare the given set of language options against an existing set of
69/// language options.
70///
71/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
72///
73/// \returns true if the languagae options mis-match, false otherwise.
74static bool checkLanguageOptions(const LangOptions &LangOpts,
75 const LangOptions &ExistingLangOpts,
76 DiagnosticsEngine *Diags) {
77#define LANGOPT(Name, Bits, Default, Description) \
78 if (ExistingLangOpts.Name != LangOpts.Name) { \
79 if (Diags) \
80 Diags->Report(diag::err_pch_langopt_mismatch) \
81 << Description << LangOpts.Name << ExistingLangOpts.Name; \
82 return true; \
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000083 }
84
Douglas Gregor27ffa6c2012-10-23 06:18:24 +000085#define VALUE_LANGOPT(Name, Bits, Default, Description) \
86 if (ExistingLangOpts.Name != LangOpts.Name) { \
87 if (Diags) \
88 Diags->Report(diag::err_pch_langopt_value_mismatch) \
89 << Description; \
90 return true; \
Douglas Gregor38295be2012-10-22 23:51:00 +000091 }
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000092
Douglas Gregor27ffa6c2012-10-23 06:18:24 +000093#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
94 if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \
95 if (Diags) \
96 Diags->Report(diag::err_pch_langopt_value_mismatch) \
97 << Description; \
98 return true; \
Douglas Gregor7d5e81b2011-09-13 18:26:39 +000099 }
100
101#define BENIGN_LANGOPT(Name, Bits, Default, Description)
102#define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
103#include "clang/Basic/LangOptions.def"
John McCall260611a2012-06-20 06:18:46 +0000104
Douglas Gregor27ffa6c2012-10-23 06:18:24 +0000105 if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
106 if (Diags)
107 Diags->Report(diag::err_pch_langopt_value_mismatch)
108 << "target Objective-C runtime";
John McCall260611a2012-06-20 06:18:46 +0000109 return true;
110 }
Douglas Gregor27ffa6c2012-10-23 06:18:24 +0000111
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000112 return false;
113}
114
Douglas Gregor27ffa6c2012-10-23 06:18:24 +0000115/// \brief Compare the given set of target options against an existing set of
116/// target options.
117///
118/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
119///
120/// \returns true if the target options mis-match, false otherwise.
121static bool checkTargetOptions(const TargetOptions &TargetOpts,
122 const TargetOptions &ExistingTargetOpts,
123 DiagnosticsEngine *Diags) {
Douglas Gregor38295be2012-10-22 23:51:00 +0000124#define CHECK_TARGET_OPT(Field, Name) \
125 if (TargetOpts.Field != ExistingTargetOpts.Field) { \
Douglas Gregor27ffa6c2012-10-23 06:18:24 +0000126 if (Diags) \
127 Diags->Report(diag::err_pch_targetopt_mismatch) \
Douglas Gregor38295be2012-10-22 23:51:00 +0000128 << Name << TargetOpts.Field << ExistingTargetOpts.Field; \
129 return true; \
Douglas Gregor57016dd2012-10-16 23:40:58 +0000130 }
131
132 CHECK_TARGET_OPT(Triple, "target");
133 CHECK_TARGET_OPT(CPU, "target CPU");
134 CHECK_TARGET_OPT(ABI, "target ABI");
135 CHECK_TARGET_OPT(CXXABI, "target C++ ABI");
136 CHECK_TARGET_OPT(LinkerVersion, "target linker version");
137#undef CHECK_TARGET_OPT
138
139 // Compare feature sets.
140 SmallVector<StringRef, 4> ExistingFeatures(
Douglas Gregor27ffa6c2012-10-23 06:18:24 +0000141 ExistingTargetOpts.FeaturesAsWritten.begin(),
142 ExistingTargetOpts.FeaturesAsWritten.end());
Douglas Gregor57016dd2012-10-16 23:40:58 +0000143 SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
144 TargetOpts.FeaturesAsWritten.end());
145 std::sort(ExistingFeatures.begin(), ExistingFeatures.end());
146 std::sort(ReadFeatures.begin(), ReadFeatures.end());
147
148 unsigned ExistingIdx = 0, ExistingN = ExistingFeatures.size();
149 unsigned ReadIdx = 0, ReadN = ReadFeatures.size();
150 while (ExistingIdx < ExistingN && ReadIdx < ReadN) {
151 if (ExistingFeatures[ExistingIdx] == ReadFeatures[ReadIdx]) {
152 ++ExistingIdx;
153 ++ReadIdx;
154 continue;
155 }
156
157 if (ReadFeatures[ReadIdx] < ExistingFeatures[ExistingIdx]) {
Douglas Gregor27ffa6c2012-10-23 06:18:24 +0000158 if (Diags)
159 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
Douglas Gregor38295be2012-10-22 23:51:00 +0000160 << false << ReadFeatures[ReadIdx];
Douglas Gregor57016dd2012-10-16 23:40:58 +0000161 return true;
162 }
163
Douglas Gregor27ffa6c2012-10-23 06:18:24 +0000164 if (Diags)
165 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
Douglas Gregor38295be2012-10-22 23:51:00 +0000166 << true << ExistingFeatures[ExistingIdx];
Douglas Gregor57016dd2012-10-16 23:40:58 +0000167 return true;
168 }
169
170 if (ExistingIdx < ExistingN) {
Douglas Gregor27ffa6c2012-10-23 06:18:24 +0000171 if (Diags)
172 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
Douglas Gregor38295be2012-10-22 23:51:00 +0000173 << true << ExistingFeatures[ExistingIdx];
Douglas Gregor57016dd2012-10-16 23:40:58 +0000174 return true;
175 }
176
177 if (ReadIdx < ReadN) {
Douglas Gregor27ffa6c2012-10-23 06:18:24 +0000178 if (Diags)
179 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
Douglas Gregor38295be2012-10-22 23:51:00 +0000180 << false << ReadFeatures[ReadIdx];
Douglas Gregor57016dd2012-10-16 23:40:58 +0000181 return true;
182 }
183
184 return false;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000185}
186
Douglas Gregor27ffa6c2012-10-23 06:18:24 +0000187bool
188PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
189 bool Complain) {
190 const LangOptions &PPLangOpts = PP.getLangOpts();
191 return checkLanguageOptions(LangOpts, PPLangOpts,
192 Complain? &Reader.Diags : 0);
193}
194
195bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
196 bool Complain) {
197 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
198 return checkTargetOptions(TargetOpts, ExistingTargetOpts,
199 Complain? &Reader.Diags : 0);
200}
201
Benjamin Kramer54353f42010-11-25 18:29:30 +0000202namespace {
203 struct EmptyStringRef {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000204 bool operator ()(StringRef r) const { return r.empty(); }
Benjamin Kramer54353f42010-11-25 18:29:30 +0000205 };
206 struct EmptyBlock {
207 bool operator ()(const PCHPredefinesBlock &r) const {return r.Data.empty();}
208 };
209}
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000210
Chris Lattner5f9e2722011-07-23 10:55:15 +0000211static bool EqualConcatenations(SmallVector<StringRef, 2> L,
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000212 PCHPredefinesBlocks R) {
213 // First, sum up the lengths.
214 unsigned LL = 0, RL = 0;
215 for (unsigned I = 0, N = L.size(); I != N; ++I) {
216 LL += L[I].size();
217 }
218 for (unsigned I = 0, N = R.size(); I != N; ++I) {
219 RL += R[I].Data.size();
220 }
221 if (LL != RL)
222 return false;
223 if (LL == 0 && RL == 0)
224 return true;
225
226 // Kick out empty parts, they confuse the algorithm below.
227 L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end());
228 R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end());
229
230 // Do it the hard way. At this point, both vectors must be non-empty.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000231 StringRef LR = L[0], RR = R[0].Data;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000232 unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size();
Daniel Dunbarc76c9e02010-07-16 00:00:11 +0000233 (void) RN;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000234 for (;;) {
235 // Compare the current pieces.
236 if (LR.size() == RR.size()) {
237 // If they're the same length, it's pretty easy.
238 if (LR != RR)
239 return false;
240 // Both pieces are done, advance.
241 ++LI;
242 ++RI;
243 // If either string is done, they're both done, since they're the same
244 // length.
245 if (LI == LN) {
246 assert(RI == RN && "Strings not the same length after all?");
247 return true;
248 }
249 LR = L[LI];
250 RR = R[RI].Data;
251 } else if (LR.size() < RR.size()) {
252 // Right piece is longer.
253 if (!RR.startswith(LR))
254 return false;
255 ++LI;
256 assert(LI != LN && "Strings not the same length after all?");
257 RR = RR.substr(LR.size());
258 LR = L[LI];
259 } else {
260 // Left piece is longer.
261 if (!LR.startswith(RR))
262 return false;
263 ++RI;
264 assert(RI != RN && "Strings not the same length after all?");
265 LR = LR.substr(RR.size());
266 RR = R[RI].Data;
267 }
268 }
269}
270
Chris Lattner5f9e2722011-07-23 10:55:15 +0000271static std::pair<FileID, StringRef::size_type>
272FindMacro(const PCHPredefinesBlocks &Buffers, StringRef MacroDef) {
273 std::pair<FileID, StringRef::size_type> Res;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000274 for (unsigned I = 0, N = Buffers.size(); I != N; ++I) {
275 Res.second = Buffers[I].Data.find(MacroDef);
Chris Lattner5f9e2722011-07-23 10:55:15 +0000276 if (Res.second != StringRef::npos) {
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000277 Res.first = Buffers[I].BufferID;
278 break;
279 }
280 }
281 return Res;
282}
283
284bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000285 StringRef OriginalFileName,
Nick Lewycky277a6e72011-02-23 21:16:44 +0000286 std::string &SuggestedPredefines,
Douglas Gregor38295be2012-10-22 23:51:00 +0000287 FileManager &FileMgr,
288 bool Complain) {
Daniel Dunbarc7162932009-11-11 23:58:53 +0000289 // We are in the context of an implicit include, so the predefines buffer will
290 // have a #include entry for the PCH file itself (as normalized by the
291 // preprocessor initialization). Find it and skip over it in the checking
292 // below.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000293 SmallString<256> PCHInclude;
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000294 PCHInclude += "#include \"";
Chandler Carruthcb381ea2011-12-09 01:33:57 +0000295 PCHInclude += HeaderSearch::NormalizeDashIncludePath(OriginalFileName,
296 FileMgr);
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000297 PCHInclude += "\"\n";
Chris Lattner5f9e2722011-07-23 10:55:15 +0000298 std::pair<StringRef,StringRef> Split =
299 StringRef(PP.getPredefines()).split(PCHInclude.str());
300 StringRef Left = Split.first, Right = Split.second;
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000301 if (Left == PP.getPredefines()) {
Douglas Gregor38295be2012-10-22 23:51:00 +0000302 if (Complain)
303 Error("Missing PCH include entry!");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000304 return true;
305 }
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000306
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000307 // If the concatenation of all the PCH buffers is equal to the adjusted
308 // command line, we're done.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000309 SmallVector<StringRef, 2> CommandLine;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000310 CommandLine.push_back(Left);
311 CommandLine.push_back(Right);
312 if (EqualConcatenations(CommandLine, Buffers))
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000313 return false;
314
315 SourceManager &SourceMgr = PP.getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000316
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000317 // The predefines buffers are different. Determine what the differences are,
318 // and whether they require us to reject the PCH file.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000319 SmallVector<StringRef, 8> PCHLines;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000320 for (unsigned I = 0, N = Buffers.size(); I != N; ++I)
321 Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Daniel Dunbare6750492009-11-13 16:46:11 +0000322
Chris Lattner5f9e2722011-07-23 10:55:15 +0000323 SmallVector<StringRef, 8> CmdLineLines;
Daniel Dunbare6750492009-11-13 16:46:11 +0000324 Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000325
326 // Pick out implicit #includes after the PCH and don't consider them for
327 // validation; we will insert them into SuggestedPredefines so that the
328 // preprocessor includes them.
329 std::string IncludesAfterPCH;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000330 SmallVector<StringRef, 8> AfterPCHLines;
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000331 Right.split(AfterPCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
332 for (unsigned i = 0, e = AfterPCHLines.size(); i != e; ++i) {
333 if (AfterPCHLines[i].startswith("#include ")) {
334 IncludesAfterPCH += AfterPCHLines[i];
335 IncludesAfterPCH += '\n';
336 } else {
337 CmdLineLines.push_back(AfterPCHLines[i]);
338 }
339 }
340
341 // Make sure we add the includes last into SuggestedPredefines before we
342 // exit this function.
343 struct AddIncludesRAII {
344 std::string &SuggestedPredefines;
345 std::string &IncludesAfterPCH;
346
347 AddIncludesRAII(std::string &SuggestedPredefines,
348 std::string &IncludesAfterPCH)
349 : SuggestedPredefines(SuggestedPredefines),
350 IncludesAfterPCH(IncludesAfterPCH) { }
351 ~AddIncludesRAII() {
352 SuggestedPredefines += IncludesAfterPCH;
353 }
354 } AddIncludes(SuggestedPredefines, IncludesAfterPCH);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000355
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000356 // Sort both sets of predefined buffer lines, since we allow some extra
357 // definitions and they may appear at any point in the output.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000358 std::sort(CmdLineLines.begin(), CmdLineLines.end());
359 std::sort(PCHLines.begin(), PCHLines.end());
360
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000361 // Determine which predefines that were used to build the PCH file are missing
362 // from the command line.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000363 std::vector<StringRef> MissingPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000364 std::set_difference(PCHLines.begin(), PCHLines.end(),
365 CmdLineLines.begin(), CmdLineLines.end(),
366 std::back_inserter(MissingPredefines));
367
368 bool MissingDefines = false;
369 bool ConflictingDefines = false;
370 for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000371 StringRef Missing = MissingPredefines[I];
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000372 if (Missing.startswith("#include ")) {
373 // An -include was specified when generating the PCH; it is included in
374 // the PCH, just ignore it.
375 continue;
376 }
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000377 if (!Missing.startswith("#define ")) {
Douglas Gregor38295be2012-10-22 23:51:00 +0000378 if (Complain)
379 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000380 return true;
381 }
Mike Stump1eb44332009-09-09 15:08:12 +0000382
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000383 // This is a macro definition. Determine the name of the macro we're
384 // defining.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000385 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000386 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000387 = Missing.find_first_of("( \n\r", StartOfMacroName);
388 assert(EndOfMacroName != std::string::npos &&
389 "Couldn't find the end of the macro name");
Chris Lattner5f9e2722011-07-23 10:55:15 +0000390 StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000391
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000392 // Determine whether this macro was given a different definition on the
393 // command line.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000394 std::string MacroDefStart = "#define " + MacroName.str();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000395 std::string::size_type MacroDefLen = MacroDefStart.size();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000396 SmallVector<StringRef, 8>::iterator ConflictPos
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000397 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
398 MacroDefStart);
399 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000400 if (!ConflictPos->startswith(MacroDefStart)) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000401 // Different macro; we're done.
402 ConflictPos = CmdLineLines.end();
Mike Stump1eb44332009-09-09 15:08:12 +0000403 break;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000404 }
Mike Stump1eb44332009-09-09 15:08:12 +0000405
406 assert(ConflictPos->size() > MacroDefLen &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000407 "Invalid #define in predefines buffer?");
Mike Stump1eb44332009-09-09 15:08:12 +0000408 if ((*ConflictPos)[MacroDefLen] != ' ' &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000409 (*ConflictPos)[MacroDefLen] != '(')
410 continue; // Longer macro name; keep trying.
Mike Stump1eb44332009-09-09 15:08:12 +0000411
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000412 // We found a conflicting macro definition.
413 break;
414 }
Mike Stump1eb44332009-09-09 15:08:12 +0000415
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000416 if (ConflictPos != CmdLineLines.end()) {
Douglas Gregor38295be2012-10-22 23:51:00 +0000417 if (!Complain)
418 return true;
419
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000420 Reader.Diag(diag::warn_cmdline_conflicting_macro_def)
421 << MacroName;
422
423 // Show the definition of this macro within the PCH file.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000424 std::pair<FileID, StringRef::size_type> MacroLoc =
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000425 FindMacro(Buffers, Missing);
Chris Lattner5f9e2722011-07-23 10:55:15 +0000426 assert(MacroLoc.second!=StringRef::npos && "Unable to find macro!");
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000427 SourceLocation PCHMissingLoc =
428 SourceMgr.getLocForStartOfFile(MacroLoc.first)
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +0000429 .getLocWithOffset(MacroLoc.second);
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000430 Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000431
432 ConflictingDefines = true;
433 continue;
434 }
Mike Stump1eb44332009-09-09 15:08:12 +0000435
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000436 // If the macro doesn't conflict, then we'll just pick up the macro
437 // definition from the PCH file. Warn the user that they made a mistake.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000438 if (ConflictingDefines)
439 continue; // Don't complain if there are already conflicting defs
Mike Stump1eb44332009-09-09 15:08:12 +0000440
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000441 if (!MissingDefines) {
Douglas Gregor38295be2012-10-22 23:51:00 +0000442 if (!Complain)
443 return true;
444
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000445 Reader.Diag(diag::warn_cmdline_missing_macro_defs);
446 MissingDefines = true;
447 }
448
Douglas Gregor38295be2012-10-22 23:51:00 +0000449 if (!Complain)
450 return true;
451
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000452 // Show the definition of this macro within the PCH file.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000453 std::pair<FileID, StringRef::size_type> MacroLoc =
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000454 FindMacro(Buffers, Missing);
Chris Lattner5f9e2722011-07-23 10:55:15 +0000455 assert(MacroLoc.second!=StringRef::npos && "Unable to find macro!");
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000456 SourceLocation PCHMissingLoc =
457 SourceMgr.getLocForStartOfFile(MacroLoc.first)
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +0000458 .getLocWithOffset(MacroLoc.second);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000459 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
460 }
Mike Stump1eb44332009-09-09 15:08:12 +0000461
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000462 if (ConflictingDefines)
463 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000464
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000465 // Determine what predefines were introduced based on command-line
466 // parameters that were not present when building the PCH
467 // file. Extra #defines are okay, so long as the identifiers being
468 // defined were not used within the precompiled header.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000469 std::vector<StringRef> ExtraPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000470 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
471 PCHLines.begin(), PCHLines.end(),
Mike Stump1eb44332009-09-09 15:08:12 +0000472 std::back_inserter(ExtraPredefines));
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000473 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000474 StringRef &Extra = ExtraPredefines[I];
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000475 if (!Extra.startswith("#define ")) {
Douglas Gregor38295be2012-10-22 23:51:00 +0000476 if (Complain)
477 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000478 return true;
479 }
480
481 // This is an extra macro definition. Determine the name of the
482 // macro we're defining.
483 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000484 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000485 = Extra.find_first_of("( \n\r", StartOfMacroName);
486 assert(EndOfMacroName != std::string::npos &&
487 "Couldn't find the end of the macro name");
Chris Lattner5f9e2722011-07-23 10:55:15 +0000488 StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000489
490 // Check whether this name was used somewhere in the PCH file. If
491 // so, defining it as a macro could change behavior, so we reject
492 // the PCH file.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000493 if (IdentifierInfo *II = Reader.get(MacroName)) {
Douglas Gregor38295be2012-10-22 23:51:00 +0000494 if (Complain)
495 Reader.Diag(diag::warn_macro_name_used_in_pch) << II;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000496 return true;
497 }
498
499 // Add this definition to the suggested predefines buffer.
500 SuggestedPredefines += Extra;
501 SuggestedPredefines += '\n';
502 }
503
504 // If we get here, it's because the predefines buffer had compatible
505 // contents. Accept the PCH file.
506 return false;
507}
508
Douglas Gregor12fab312010-03-16 16:35:32 +0000509void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
510 unsigned ID) {
511 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
512 ++NumHeaderInfos;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000513}
514
Argyrios Kyrtzidis62288ed2012-10-10 02:12:47 +0000515void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000516 PP.setCounterValue(Value);
517}
518
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000519//===----------------------------------------------------------------------===//
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000520// AST reader implementation
Douglas Gregor668c1a42009-04-21 22:25:48 +0000521//===----------------------------------------------------------------------===//
522
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000523void
Sebastian Redl571db7f2010-08-18 23:56:56 +0000524ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000525 DeserializationListener = Listener;
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000526}
527
Chris Lattner4c6f9522009-04-27 05:14:47 +0000528
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000529
Douglas Gregor98339b92011-08-25 20:47:51 +0000530unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
531 return serialization::ComputeHash(Sel);
532}
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000533
Mike Stump1eb44332009-09-09 15:08:12 +0000534
Douglas Gregor98339b92011-08-25 20:47:51 +0000535std::pair<unsigned, unsigned>
536ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
537 using namespace clang::io;
538 unsigned KeyLen = ReadUnalignedLE16(d);
539 unsigned DataLen = ReadUnalignedLE16(d);
540 return std::make_pair(KeyLen, DataLen);
541}
542
543ASTSelectorLookupTrait::internal_key_type
544ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
545 using namespace clang::io;
Douglas Gregor35942772011-09-09 21:34:22 +0000546 SelectorTable &SelTable = Reader.getContext().Selectors;
Douglas Gregor98339b92011-08-25 20:47:51 +0000547 unsigned N = ReadUnalignedLE16(d);
548 IdentifierInfo *FirstII
549 = Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
550 if (N == 0)
551 return SelTable.getNullarySelector(FirstII);
552 else if (N == 1)
553 return SelTable.getUnarySelector(FirstII);
554
555 SmallVector<IdentifierInfo *, 16> Args;
556 Args.push_back(FirstII);
557 for (unsigned I = 1; I != N; ++I)
558 Args.push_back(Reader.getLocalIdentifier(F, ReadUnalignedLE32(d)));
559
560 return SelTable.getSelector(N, Args.data());
561}
562
563ASTSelectorLookupTrait::data_type
564ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
565 unsigned DataLen) {
566 using namespace clang::io;
567
568 data_type Result;
569
570 Result.ID = Reader.getGlobalSelectorID(F, ReadUnalignedLE32(d));
571 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
572 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
573
574 // Load instance methods
Douglas Gregor98339b92011-08-25 20:47:51 +0000575 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
576 if (ObjCMethodDecl *Method
577 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d)))
578 Result.Instance.push_back(Method);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000579 }
Mike Stump1eb44332009-09-09 15:08:12 +0000580
Douglas Gregor98339b92011-08-25 20:47:51 +0000581 // Load factory methods
Douglas Gregor98339b92011-08-25 20:47:51 +0000582 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
583 if (ObjCMethodDecl *Method
584 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d)))
585 Result.Factory.push_back(Method);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000586 }
Mike Stump1eb44332009-09-09 15:08:12 +0000587
Douglas Gregor98339b92011-08-25 20:47:51 +0000588 return Result;
589}
Mike Stump1eb44332009-09-09 15:08:12 +0000590
Douglas Gregor98339b92011-08-25 20:47:51 +0000591unsigned ASTIdentifierLookupTrait::ComputeHash(const internal_key_type& a) {
592 return llvm::HashString(StringRef(a.first, a.second));
593}
Mike Stump1eb44332009-09-09 15:08:12 +0000594
Douglas Gregor98339b92011-08-25 20:47:51 +0000595std::pair<unsigned, unsigned>
596ASTIdentifierLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
597 using namespace clang::io;
598 unsigned DataLen = ReadUnalignedLE16(d);
599 unsigned KeyLen = ReadUnalignedLE16(d);
600 return std::make_pair(KeyLen, DataLen);
601}
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000602
Douglas Gregor98339b92011-08-25 20:47:51 +0000603std::pair<const char*, unsigned>
604ASTIdentifierLookupTrait::ReadKey(const unsigned char* d, unsigned n) {
605 assert(n >= 2 && d[n-1] == '\0');
606 return std::make_pair((const char*) d, n-1);
607}
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000608
Douglas Gregor98339b92011-08-25 20:47:51 +0000609IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
610 const unsigned char* d,
611 unsigned DataLen) {
612 using namespace clang::io;
613 unsigned RawID = ReadUnalignedLE32(d);
614 bool IsInteresting = RawID & 0x01;
Mike Stump1eb44332009-09-09 15:08:12 +0000615
Douglas Gregor98339b92011-08-25 20:47:51 +0000616 // Wipe out the "is interesting" bit.
617 RawID = RawID >> 1;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000618
Douglas Gregor98339b92011-08-25 20:47:51 +0000619 IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
620 if (!IsInteresting) {
621 // For uninteresting identifiers, just build the IdentifierInfo
622 // and associate it with the persistent ID.
Douglas Gregor668c1a42009-04-21 22:25:48 +0000623 IdentifierInfo *II = KnownII;
Douglas Gregor5d5051f2012-01-24 15:24:38 +0000624 if (!II) {
Douglas Gregor6ec60e02011-08-03 21:49:18 +0000625 II = &Reader.getIdentifierTable().getOwn(StringRef(k.first, k.second));
Douglas Gregor5d5051f2012-01-24 15:24:38 +0000626 KnownII = II;
627 }
Douglas Gregor668c1a42009-04-21 22:25:48 +0000628 Reader.SetIdentifierInfo(ID, II);
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000629 II->setIsFromAST();
Douglas Gregor057df202012-01-18 20:56:22 +0000630 Reader.markIdentifierUpToDate(II);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000631 return II;
632 }
Mike Stump1eb44332009-09-09 15:08:12 +0000633
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +0000634 unsigned ObjCOrBuiltinID = ReadUnalignedLE16(d);
Douglas Gregor98339b92011-08-25 20:47:51 +0000635 unsigned Bits = ReadUnalignedLE16(d);
636 bool CPlusPlusOperatorKeyword = Bits & 0x01;
637 Bits >>= 1;
638 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
639 Bits >>= 1;
640 bool Poisoned = Bits & 0x01;
641 Bits >>= 1;
642 bool ExtensionToken = Bits & 0x01;
643 Bits >>= 1;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +0000644 bool hadMacroDefinition = Bits & 0x01;
645 Bits >>= 1;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000646
Douglas Gregor98339b92011-08-25 20:47:51 +0000647 assert(Bits == 0 && "Extra bits in the identifier?");
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +0000648 DataLen -= 8;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000649
Douglas Gregor98339b92011-08-25 20:47:51 +0000650 // Build the IdentifierInfo itself and link the identifier ID with
651 // the new IdentifierInfo.
652 IdentifierInfo *II = KnownII;
Douglas Gregor5d5051f2012-01-24 15:24:38 +0000653 if (!II) {
Douglas Gregor98339b92011-08-25 20:47:51 +0000654 II = &Reader.getIdentifierTable().getOwn(StringRef(k.first, k.second));
Douglas Gregor5d5051f2012-01-24 15:24:38 +0000655 KnownII = II;
656 }
Douglas Gregor057df202012-01-18 20:56:22 +0000657 Reader.markIdentifierUpToDate(II);
Douglas Gregoreee242f2011-10-27 09:33:13 +0000658 II->setIsFromAST();
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000659
Douglas Gregor98339b92011-08-25 20:47:51 +0000660 // Set or check the various bits in the IdentifierInfo structure.
661 // Token IDs are read-only.
662 if (HasRevertedTokenIDToIdentifier)
663 II->RevertTokenIDToIdentifier();
664 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
665 assert(II->isExtensionToken() == ExtensionToken &&
666 "Incorrect extension token flag");
667 (void)ExtensionToken;
668 if (Poisoned)
669 II->setIsPoisoned(true);
670 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
671 "Incorrect C++ operator keyword flag");
672 (void)CPlusPlusOperatorKeyword;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000673
Douglas Gregor98339b92011-08-25 20:47:51 +0000674 // If this identifier is a macro, deserialize the macro
675 // definition.
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +0000676 if (hadMacroDefinition) {
Douglas Gregor6c6c54a2012-10-11 00:46:49 +0000677 SmallVector<MacroID, 4> MacroIDs;
678 while (uint32_t LocalID = ReadUnalignedLE32(d)) {
679 MacroIDs.push_back(Reader.getGlobalMacroID(F, LocalID));
680 DataLen -= 4;
Douglas Gregor13292642011-12-02 15:45:10 +0000681 }
Douglas Gregor6c6c54a2012-10-11 00:46:49 +0000682 DataLen -= 4;
683 Reader.setIdentifierIsMacro(II, MacroIDs);
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000684 }
685
Douglas Gregoreee242f2011-10-27 09:33:13 +0000686 Reader.SetIdentifierInfo(ID, II);
687
Douglas Gregor98339b92011-08-25 20:47:51 +0000688 // Read all of the declarations visible at global scope with this
689 // name.
Douglas Gregor98339b92011-08-25 20:47:51 +0000690 if (DataLen > 0) {
691 SmallVector<uint32_t, 4> DeclIDs;
692 for (; DataLen > 0; DataLen -= 4)
693 DeclIDs.push_back(Reader.getGlobalDeclID(F, ReadUnalignedLE32(d)));
694 Reader.SetGloballyVisibleDecls(II, DeclIDs);
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000695 }
696
Douglas Gregor98339b92011-08-25 20:47:51 +0000697 return II;
698}
Michael J. Spencer20249a12010-10-21 03:16:25 +0000699
Douglas Gregor98339b92011-08-25 20:47:51 +0000700unsigned
701ASTDeclContextNameLookupTrait::ComputeHash(const DeclNameKey &Key) const {
702 llvm::FoldingSetNodeID ID;
703 ID.AddInteger(Key.Kind);
704
705 switch (Key.Kind) {
706 case DeclarationName::Identifier:
707 case DeclarationName::CXXLiteralOperatorName:
708 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
709 break;
710 case DeclarationName::ObjCZeroArgSelector:
711 case DeclarationName::ObjCOneArgSelector:
712 case DeclarationName::ObjCMultiArgSelector:
713 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
714 break;
715 case DeclarationName::CXXOperatorName:
716 ID.AddInteger((OverloadedOperatorKind)Key.Data);
717 break;
718 case DeclarationName::CXXConstructorName:
719 case DeclarationName::CXXDestructorName:
720 case DeclarationName::CXXConversionFunctionName:
721 case DeclarationName::CXXUsingDirective:
722 break;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000723 }
724
Douglas Gregor98339b92011-08-25 20:47:51 +0000725 return ID.ComputeHash();
726}
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +0000727
Douglas Gregor98339b92011-08-25 20:47:51 +0000728ASTDeclContextNameLookupTrait::internal_key_type
729ASTDeclContextNameLookupTrait::GetInternalKey(
730 const external_key_type& Name) const {
731 DeclNameKey Key;
732 Key.Kind = Name.getNameKind();
733 switch (Name.getNameKind()) {
734 case DeclarationName::Identifier:
735 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
736 break;
737 case DeclarationName::ObjCZeroArgSelector:
738 case DeclarationName::ObjCOneArgSelector:
739 case DeclarationName::ObjCMultiArgSelector:
740 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
741 break;
742 case DeclarationName::CXXOperatorName:
743 Key.Data = Name.getCXXOverloadedOperator();
744 break;
745 case DeclarationName::CXXLiteralOperatorName:
746 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
747 break;
748 case DeclarationName::CXXConstructorName:
749 case DeclarationName::CXXDestructorName:
750 case DeclarationName::CXXConversionFunctionName:
751 case DeclarationName::CXXUsingDirective:
752 Key.Data = 0;
753 break;
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +0000754 }
755
Douglas Gregor98339b92011-08-25 20:47:51 +0000756 return Key;
757}
758
Douglas Gregor98339b92011-08-25 20:47:51 +0000759std::pair<unsigned, unsigned>
760ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
761 using namespace clang::io;
762 unsigned KeyLen = ReadUnalignedLE16(d);
763 unsigned DataLen = ReadUnalignedLE16(d);
764 return std::make_pair(KeyLen, DataLen);
765}
Michael J. Spencer20249a12010-10-21 03:16:25 +0000766
Douglas Gregor98339b92011-08-25 20:47:51 +0000767ASTDeclContextNameLookupTrait::internal_key_type
768ASTDeclContextNameLookupTrait::ReadKey(const unsigned char* d, unsigned) {
769 using namespace clang::io;
770
771 DeclNameKey Key;
772 Key.Kind = (DeclarationName::NameKind)*d++;
773 switch (Key.Kind) {
774 case DeclarationName::Identifier:
775 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
776 break;
777 case DeclarationName::ObjCZeroArgSelector:
778 case DeclarationName::ObjCOneArgSelector:
779 case DeclarationName::ObjCMultiArgSelector:
780 Key.Data =
781 (uint64_t)Reader.getLocalSelector(F, ReadUnalignedLE32(d))
782 .getAsOpaquePtr();
783 break;
784 case DeclarationName::CXXOperatorName:
785 Key.Data = *d++; // OverloadedOperatorKind
786 break;
787 case DeclarationName::CXXLiteralOperatorName:
788 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
789 break;
790 case DeclarationName::CXXConstructorName:
791 case DeclarationName::CXXDestructorName:
792 case DeclarationName::CXXConversionFunctionName:
793 case DeclarationName::CXXUsingDirective:
794 Key.Data = 0;
795 break;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000796 }
797
Douglas Gregor98339b92011-08-25 20:47:51 +0000798 return Key;
799}
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000800
Douglas Gregor98339b92011-08-25 20:47:51 +0000801ASTDeclContextNameLookupTrait::data_type
802ASTDeclContextNameLookupTrait::ReadData(internal_key_type,
803 const unsigned char* d,
Nick Lewyckyb346d2f2012-04-16 02:51:46 +0000804 unsigned DataLen) {
Douglas Gregor98339b92011-08-25 20:47:51 +0000805 using namespace clang::io;
806 unsigned NumDecls = ReadUnalignedLE16(d);
Douglas Gregor9b8b20f2012-01-06 16:09:53 +0000807 LE32DeclID *Start = (LE32DeclID *)d;
Douglas Gregor98339b92011-08-25 20:47:51 +0000808 return std::make_pair(Start, Start + NumDecls);
809}
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000810
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000811bool ASTReader::ReadDeclContextStorage(ModuleFile &M,
Douglas Gregor0d95f772011-08-24 19:03:07 +0000812 llvm::BitstreamCursor &Cursor,
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000813 const std::pair<uint64_t, uint64_t> &Offsets,
814 DeclContextInfo &Info) {
815 SavedStreamPosition SavedPosition(Cursor);
816 // First the lexical decls.
817 if (Offsets.first != 0) {
818 Cursor.JumpToBit(Offsets.first);
819
820 RecordData Record;
821 const char *Blob;
822 unsigned BlobLen;
823 unsigned Code = Cursor.ReadCode();
824 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
825 if (RecCode != DECL_CONTEXT_LEXICAL) {
826 Error("Expected lexical block");
827 return true;
828 }
829
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000830 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob);
831 Info.NumLexicalDecls = BlobLen / sizeof(KindDeclIDPair);
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000832 }
833
834 // Now the lookup table.
835 if (Offsets.second != 0) {
836 Cursor.JumpToBit(Offsets.second);
837
838 RecordData Record;
839 const char *Blob;
840 unsigned BlobLen;
841 unsigned Code = Cursor.ReadCode();
842 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
843 if (RecCode != DECL_CONTEXT_VISIBLE) {
844 Error("Expected visible lookup table block");
845 return true;
846 }
847 Info.NameLookupTableData
848 = ASTDeclContextNameLookupTable::Create(
849 (const unsigned char *)Blob + Record[0],
850 (const unsigned char *)Blob,
Douglas Gregor0d95f772011-08-24 19:03:07 +0000851 ASTDeclContextNameLookupTrait(*this, M));
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000852 }
853
854 return false;
855}
856
Chris Lattner5f9e2722011-07-23 10:55:15 +0000857void ASTReader::Error(StringRef Msg) {
Argyrios Kyrtzidis8d8f2c22011-04-25 22:23:56 +0000858 Error(diag::err_fe_pch_malformed, Msg);
859}
860
861void ASTReader::Error(unsigned DiagID,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000862 StringRef Arg1, StringRef Arg2) {
Argyrios Kyrtzidis8d8f2c22011-04-25 22:23:56 +0000863 if (Diags.isDiagnosticInFlight())
864 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
865 else
866 Diag(DiagID) << Arg1 << Arg2;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000867}
868
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000869/// \brief Tell the AST listener about the predefines buffers in the chain.
Douglas Gregor38295be2012-10-22 23:51:00 +0000870bool ASTReader::CheckPredefinesBuffers(bool Complain) {
Douglas Gregor11407b82012-10-18 21:18:25 +0000871 if (Listener) {
872 // We only care about the primary module.
873 ModuleFile &M = ModuleMgr.getPrimaryModule();
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000874 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
Douglas Gregor11407b82012-10-18 21:18:25 +0000875 M.ActualOriginalSourceFileName,
Nick Lewycky277a6e72011-02-23 21:16:44 +0000876 SuggestedPredefines,
Douglas Gregor38295be2012-10-22 23:51:00 +0000877 FileMgr,
878 Complain);
Douglas Gregor11407b82012-10-18 21:18:25 +0000879 }
Douglas Gregore721f952009-04-28 18:58:38 +0000880 return false;
Douglas Gregore1d918e2009-04-10 23:10:45 +0000881}
882
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000883//===----------------------------------------------------------------------===//
884// Source Manager Deserialization
885//===----------------------------------------------------------------------===//
886
Douglas Gregorbd945002009-04-13 16:31:14 +0000887/// \brief Read the line table in the source manager block.
Sebastian Redlc3632732010-10-05 15:59:54 +0000888/// \returns true if there was an error.
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000889bool ASTReader::ParseLineTable(ModuleFile &F,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000890 SmallVectorImpl<uint64_t> &Record) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000891 unsigned Idx = 0;
892 LineTableInfo &LineTable = SourceMgr.getLineTable();
893
894 // Parse the file names
Douglas Gregorff0a9872009-04-13 17:12:42 +0000895 std::map<int, int> FileIDs;
896 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000897 // Extract the file name
898 unsigned FilenameLen = Record[Idx++];
899 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
900 Idx += FilenameLen;
Douglas Gregorcaed0602012-10-18 21:31:35 +0000901 MaybeAddSystemRootToFilename(F, Filename);
Jay Foad65aa6882011-06-21 15:13:30 +0000902 FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
Douglas Gregorbd945002009-04-13 16:31:14 +0000903 }
904
905 // Parse the line entries
906 std::vector<LineEntry> Entries;
907 while (Idx < Record.size()) {
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000908 int FID = Record[Idx++];
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000909 assert(FID >= 0 && "Serialized line entries for non-local file.");
910 // Remap FileID from 1-based old view.
911 FID += F.SLocEntryBaseID - 1;
Douglas Gregorbd945002009-04-13 16:31:14 +0000912
913 // Extract the line entries
914 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000915 assert(NumEntries && "Numentries is 00000");
Douglas Gregorbd945002009-04-13 16:31:14 +0000916 Entries.clear();
917 Entries.reserve(NumEntries);
918 for (unsigned I = 0; I != NumEntries; ++I) {
919 unsigned FileOffset = Record[Idx++];
920 unsigned LineNo = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000921 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump1eb44332009-09-09 15:08:12 +0000922 SrcMgr::CharacteristicKind FileKind
Douglas Gregorbd945002009-04-13 16:31:14 +0000923 = (SrcMgr::CharacteristicKind)Record[Idx++];
924 unsigned IncludeOffset = Record[Idx++];
925 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
926 FileKind, IncludeOffset));
927 }
Douglas Gregor47d9de62012-06-08 16:40:28 +0000928 LineTable.AddEntry(FileID::get(FID), Entries);
Douglas Gregorbd945002009-04-13 16:31:14 +0000929 }
930
931 return false;
932}
933
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000934namespace {
935
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000936class ASTStatData {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000937public:
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000938 const ino_t ino;
939 const dev_t dev;
940 const mode_t mode;
941 const time_t mtime;
942 const off_t size;
Mike Stump1eb44332009-09-09 15:08:12 +0000943
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000944 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Chris Lattner74e976b2010-11-23 19:28:12 +0000945 : ino(i), dev(d), mode(mo), mtime(m), size(s) {}
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000946};
947
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000948class ASTStatLookupTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000949 public:
950 typedef const char *external_key_type;
951 typedef const char *internal_key_type;
952
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000953 typedef ASTStatData data_type;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000954
955 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000956 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000957 }
958
959 static internal_key_type GetInternalKey(const char *path) { return path; }
960
961 static bool EqualKey(internal_key_type a, internal_key_type b) {
962 return strcmp(a, b) == 0;
963 }
964
965 static std::pair<unsigned, unsigned>
966 ReadKeyDataLength(const unsigned char*& d) {
967 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
968 unsigned DataLen = (unsigned) *d++;
969 return std::make_pair(KeyLen + 1, DataLen);
970 }
971
972 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
973 return (const char *)d;
974 }
975
976 static data_type ReadData(const internal_key_type, const unsigned char *d,
977 unsigned /*DataLen*/) {
978 using namespace clang::io;
979
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000980 ino_t ino = (ino_t) ReadUnalignedLE32(d);
981 dev_t dev = (dev_t) ReadUnalignedLE32(d);
982 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump1eb44332009-09-09 15:08:12 +0000983 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000984 off_t size = (off_t) ReadUnalignedLE64(d);
985 return data_type(ino, dev, mode, mtime, size);
986 }
987};
988
989/// \brief stat() cache for precompiled headers.
990///
991/// This cache is very similar to the stat cache used by pretokenized
992/// headers.
Chris Lattner10e286a2010-11-23 19:19:34 +0000993class ASTStatCache : public FileSystemStatCache {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000994 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000995 CacheTy *Cache;
996
997 unsigned &NumStatHits, &NumStatMisses;
Mike Stump1eb44332009-09-09 15:08:12 +0000998public:
Chris Lattner74e976b2010-11-23 19:28:12 +0000999 ASTStatCache(const unsigned char *Buckets, const unsigned char *Base,
1000 unsigned &NumStatHits, unsigned &NumStatMisses)
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001001 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
1002 Cache = CacheTy::Create(Buckets, Base);
1003 }
1004
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001005 ~ASTStatCache() { delete Cache; }
Mike Stump1eb44332009-09-09 15:08:12 +00001006
Chris Lattner898a0612010-11-23 21:17:56 +00001007 LookupResult getStat(const char *Path, struct stat &StatBuf,
1008 int *FileDescriptor) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001009 // Do the lookup for the file's data in the AST file.
Chris Lattner10e286a2010-11-23 19:19:34 +00001010 CacheTy::iterator I = Cache->find(Path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001011
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001012 // If we don't get a hit in the AST file just forward to 'stat'.
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001013 if (I == Cache->end()) {
1014 ++NumStatMisses;
Chris Lattner898a0612010-11-23 21:17:56 +00001015 return statChained(Path, StatBuf, FileDescriptor);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001016 }
Mike Stump1eb44332009-09-09 15:08:12 +00001017
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001018 ++NumStatHits;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001019 ASTStatData Data = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001020
Chris Lattner10e286a2010-11-23 19:19:34 +00001021 StatBuf.st_ino = Data.ino;
1022 StatBuf.st_dev = Data.dev;
1023 StatBuf.st_mtime = Data.mtime;
1024 StatBuf.st_mode = Data.mode;
1025 StatBuf.st_size = Data.size;
Chris Lattnerd6f61112010-11-23 20:05:15 +00001026 return CacheExists;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001027 }
1028};
1029} // end anonymous namespace
1030
1031
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001032/// \brief Read a source manager block
Douglas Gregor4825fd72012-10-22 22:50:17 +00001033bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001034 using namespace SrcMgr;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001035
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001036 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001037
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001038 // Set the source-location entry cursor to the current position in
1039 // the stream. This cursor will be used to read the contents of the
1040 // source manager block initially, and then lazily read
1041 // source-location entries as needed.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001042 SLocEntryCursor = F.Stream;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001043
1044 // The stream itself is going to skip over the source manager block.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001045 if (F.Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001046 Error("malformed block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001047 return true;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001048 }
1049
1050 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001051 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001052 Error("malformed source manager block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001053 return true;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001054 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001055
Douglas Gregor14f79002009-04-10 03:52:48 +00001056 RecordData Record;
1057 while (true) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001058 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregor14f79002009-04-10 03:52:48 +00001059 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001060 if (SLocEntryCursor.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001061 Error("error at end of Source Manager block in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001062 return true;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001063 }
Douglas Gregor4825fd72012-10-22 22:50:17 +00001064 return false;
Douglas Gregor14f79002009-04-10 03:52:48 +00001065 }
Mike Stump1eb44332009-09-09 15:08:12 +00001066
Douglas Gregor14f79002009-04-10 03:52:48 +00001067 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1068 // No known subblocks, always skip them.
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001069 SLocEntryCursor.ReadSubBlockID();
1070 if (SLocEntryCursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001071 Error("malformed block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001072 return true;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001073 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001074 continue;
1075 }
Mike Stump1eb44332009-09-09 15:08:12 +00001076
Douglas Gregor14f79002009-04-10 03:52:48 +00001077 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001078 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregor14f79002009-04-10 03:52:48 +00001079 continue;
1080 }
Mike Stump1eb44332009-09-09 15:08:12 +00001081
Douglas Gregor14f79002009-04-10 03:52:48 +00001082 // Read a record.
1083 const char *BlobStart;
1084 unsigned BlobLen;
1085 Record.clear();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001086 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001087 default: // Default behavior: ignore.
1088 break;
1089
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001090 case SM_SLOC_FILE_ENTRY:
1091 case SM_SLOC_BUFFER_ENTRY:
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001092 case SM_SLOC_EXPANSION_ENTRY:
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001093 // Once we hit one of the source location entries, we're done.
Douglas Gregor4825fd72012-10-22 22:50:17 +00001094 return false;
Douglas Gregor14f79002009-04-10 03:52:48 +00001095 }
1096 }
1097}
1098
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001099/// \brief If a header file is not found at the path that we expect it to be
1100/// and the PCH file was moved from its original location, try to resolve the
1101/// file by assuming that header+PCH were moved together and the header is in
1102/// the same place relative to the PCH.
1103static std::string
1104resolveFileRelativeToOriginalDir(const std::string &Filename,
1105 const std::string &OriginalDir,
1106 const std::string &CurrDir) {
1107 assert(OriginalDir != CurrDir &&
1108 "No point trying to resolve the file if the PCH dir didn't change");
1109 using namespace llvm::sys;
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001110 SmallString<128> filePath(Filename);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001111 fs::make_absolute(filePath);
1112 assert(path::is_absolute(OriginalDir));
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001113 SmallString<128> currPCHPath(CurrDir);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001114
1115 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1116 fileDirE = path::end(path::parent_path(filePath));
1117 path::const_iterator origDirI = path::begin(OriginalDir),
1118 origDirE = path::end(OriginalDir);
1119 // Skip the common path components from filePath and OriginalDir.
1120 while (fileDirI != fileDirE && origDirI != origDirE &&
1121 *fileDirI == *origDirI) {
1122 ++fileDirI;
1123 ++origDirI;
1124 }
1125 for (; origDirI != origDirE; ++origDirI)
1126 path::append(currPCHPath, "..");
1127 path::append(currPCHPath, fileDirI, fileDirE);
1128 path::append(currPCHPath, path::filename(Filename));
1129 return currPCHPath.str();
1130}
1131
Douglas Gregor8b53d142012-10-22 22:53:10 +00001132bool ASTReader::ReadSLocEntry(int ID) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001133 if (ID == 0)
Douglas Gregor4825fd72012-10-22 22:50:17 +00001134 return false;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001135
Douglas Gregor0cdd7982011-07-21 18:46:38 +00001136 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001137 Error("source location entry ID out-of-range for AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001138 return true;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001139 }
1140
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001141 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001142 F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]);
Sebastian Redlc3632732010-10-05 15:59:54 +00001143 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001144 unsigned BaseOffset = F->SLocEntryBaseOffset;
Sebastian Redl9137a522010-07-16 17:50:48 +00001145
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001146 ++NumSLocEntriesRead;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001147 unsigned Code = SLocEntryCursor.ReadCode();
1148 if (Code == llvm::bitc::END_BLOCK ||
1149 Code == llvm::bitc::ENTER_SUBBLOCK ||
1150 Code == llvm::bitc::DEFINE_ABBREV) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001151 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001152 return true;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001153 }
1154
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001155 RecordData Record;
1156 const char *BlobStart;
1157 unsigned BlobLen;
1158 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1159 default:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001160 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001161 return true;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001162
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001163 case SM_SLOC_FILE_ENTRY: {
Argyrios Kyrtzidisa4c29b62012-02-20 23:58:07 +00001164 // We will detect whether a file changed and return 'Failure' for it, but
1165 // we will also try to fail gracefully by setting up the SLocEntry.
Douglas Gregora930dc92012-10-22 18:42:04 +00001166 unsigned InputID = Record[4];
1167 InputFile IF = getInputFile(*F, InputID);
1168 const FileEntry *File = IF.getPointer();
1169 bool OverriddenBuffer = IF.getInt();
Argyrios Kyrtzidisa4c29b62012-02-20 23:58:07 +00001170
Douglas Gregora930dc92012-10-22 18:42:04 +00001171 if (!IF.getPointer())
Douglas Gregor4825fd72012-10-22 22:50:17 +00001172 return true;
Douglas Gregor2d52be52010-03-21 22:49:54 +00001173
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001174 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
Douglas Gregor72a9ae12011-07-22 16:00:58 +00001175 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001176 // This is the module's main file.
1177 IncludeLoc = getImportLocation(F);
1178 }
Argyrios Kyrtzidisff398962012-07-11 20:59:04 +00001179 SrcMgr::CharacteristicKind
1180 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1181 FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001182 ID, BaseOffset + Record[0]);
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001183 SrcMgr::FileInfo &FileInfo =
1184 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
Douglas Gregora930dc92012-10-22 18:42:04 +00001185 FileInfo.NumCreatedFIDs = Record[5];
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001186 if (Record[3])
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001187 FileInfo.setHasLineDirectives();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001188
Douglas Gregora930dc92012-10-22 18:42:04 +00001189 const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
1190 unsigned NumFileDecls = Record[7];
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001191 if (NumFileDecls) {
1192 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
Argyrios Kyrtzidis9d128d02011-10-31 07:20:08 +00001193 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1194 NumFileDecls));
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001195 }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001196
Douglas Gregor35f9ae62011-11-17 01:44:33 +00001197 const SrcMgr::ContentCache *ContentCache
Argyrios Kyrtzidisff398962012-07-11 20:59:04 +00001198 = SourceMgr.getOrCreateContentCache(File,
1199 /*isSystemFile=*/FileCharacter != SrcMgr::C_User);
Douglas Gregor35f9ae62011-11-17 01:44:33 +00001200 if (OverriddenBuffer && !ContentCache->BufferOverridden &&
1201 ContentCache->ContentsEntry == ContentCache->OrigEntry) {
Douglas Gregora081da52011-11-16 20:05:18 +00001202 unsigned Code = SLocEntryCursor.ReadCode();
1203 Record.clear();
1204 unsigned RecCode
1205 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
1206
1207 if (RecCode != SM_SLOC_BUFFER_BLOB) {
1208 Error("AST record has invalid code");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001209 return true;
Douglas Gregora081da52011-11-16 20:05:18 +00001210 }
1211
1212 llvm::MemoryBuffer *Buffer
1213 = llvm::MemoryBuffer::getMemBuffer(StringRef(BlobStart, BlobLen - 1),
Douglas Gregora930dc92012-10-22 18:42:04 +00001214 File->getName());
Douglas Gregora081da52011-11-16 20:05:18 +00001215 SourceMgr.overrideFileContents(File, Buffer);
1216 }
Argyrios Kyrtzidisa4c29b62012-02-20 23:58:07 +00001217
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001218 break;
1219 }
1220
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001221 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001222 const char *Name = BlobStart;
1223 unsigned Offset = Record[0];
1224 unsigned Code = SLocEntryCursor.ReadCode();
1225 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001226 unsigned RecCode
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001227 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001228
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001229 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001230 Error("AST record has invalid code");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001231 return true;
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001232 }
1233
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001234 llvm::MemoryBuffer *Buffer
Douglas Gregora081da52011-11-16 20:05:18 +00001235 = llvm::MemoryBuffer::getMemBuffer(StringRef(BlobStart, BlobLen - 1),
1236 Name);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001237 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID,
1238 BaseOffset + Offset);
Mike Stump1eb44332009-09-09 15:08:12 +00001239
Douglas Gregor6236a292011-12-02 21:56:05 +00001240 if (strcmp(Name, "<built-in>") == 0 && F->Kind == MK_PCH) {
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +00001241 PCHPredefinesBlock Block = {
1242 BufferID,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001243 StringRef(BlobStart, BlobLen - 1)
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +00001244 };
1245 PCHPredefinesBuffers.push_back(Block);
Douglas Gregor92b059e2009-04-28 20:33:11 +00001246 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001247
1248 break;
1249 }
1250
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001251 case SM_SLOC_EXPANSION_ENTRY: {
Sebastian Redlc3632732010-10-05 15:59:54 +00001252 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
Chandler Carruthbf340e42011-07-26 03:03:05 +00001253 SourceMgr.createExpansionLoc(SpellingLoc,
Sebastian Redlc3632732010-10-05 15:59:54 +00001254 ReadSourceLocation(*F, Record[2]),
1255 ReadSourceLocation(*F, Record[3]),
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001256 Record[4],
1257 ID,
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001258 BaseOffset + Record[0]);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001259 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001260 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001261 }
1262
Douglas Gregor4825fd72012-10-22 22:50:17 +00001263 return false;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001264}
1265
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001266/// \brief Find the location where the module F is imported.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001267SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001268 if (F->ImportLoc.isValid())
1269 return F->ImportLoc;
Jonathan D. Turner2e091632011-07-29 18:09:09 +00001270
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001271 // Otherwise we have a PCH. It's considered to be "imported" at the first
1272 // location of its includer.
Jonathan D. Turner2e091632011-07-29 18:09:09 +00001273 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001274 // Main file is the importer. We assume that it is the first entry in the
1275 // entry table. We can't ask the manager, because at the time of PCH loading
1276 // the main file entry doesn't exist yet.
1277 // The very first entry is the invalid instantiation loc, which takes up
1278 // offsets 0 and 1.
1279 return SourceLocation::getFromRawEncoding(2U);
1280 }
Jonathan D. Turner2e091632011-07-29 18:09:09 +00001281 //return F->Loaders[0]->FirstLoc;
1282 return F->ImportedBy[0]->FirstLoc;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001283}
1284
Chris Lattner6367f6d2009-04-27 01:05:14 +00001285/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1286/// specified cursor. Read the abbreviations that are at the top of the block
1287/// and then leave the cursor pointing into the block.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001288bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattner6367f6d2009-04-27 01:05:14 +00001289 unsigned BlockID) {
1290 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001291 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001292 return Failure;
1293 }
Mike Stump1eb44332009-09-09 15:08:12 +00001294
Chris Lattner6367f6d2009-04-27 01:05:14 +00001295 while (true) {
Douglas Gregorecdcb882010-10-20 22:00:55 +00001296 uint64_t Offset = Cursor.GetCurrentBitNo();
Chris Lattner6367f6d2009-04-27 01:05:14 +00001297 unsigned Code = Cursor.ReadCode();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001298
Chris Lattner6367f6d2009-04-27 01:05:14 +00001299 // We expect all abbrevs to be at the start of the block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001300 if (Code != llvm::bitc::DEFINE_ABBREV) {
1301 Cursor.JumpToBit(Offset);
Chris Lattner6367f6d2009-04-27 01:05:14 +00001302 return false;
Douglas Gregorecdcb882010-10-20 22:00:55 +00001303 }
Chris Lattner6367f6d2009-04-27 01:05:14 +00001304 Cursor.ReadAbbrevRecord();
1305 }
1306}
1307
Douglas Gregor3ab50fe2012-10-11 17:41:54 +00001308void ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset,
1309 MacroInfo *Hint) {
Douglas Gregorecdcb882010-10-20 22:00:55 +00001310 llvm::BitstreamCursor &Stream = F.MacroCursor;
Mike Stump1eb44332009-09-09 15:08:12 +00001311
Douglas Gregor37e26842009-04-21 23:56:24 +00001312 // Keep track of where we are in the stream, then jump back there
1313 // after reading this macro.
1314 SavedStreamPosition SavedPosition(Stream);
1315
1316 Stream.JumpToBit(Offset);
1317 RecordData Record;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001318 SmallVector<IdentifierInfo*, 16> MacroArgs;
Douglas Gregor37e26842009-04-21 23:56:24 +00001319 MacroInfo *Macro = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001320
Douglas Gregore8219a62012-10-11 21:07:39 +00001321 // RAII object to add the loaded macro information once we're done
1322 // adding tokens.
1323 struct AddLoadedMacroInfoRAII {
1324 Preprocessor &PP;
1325 MacroInfo *Hint;
1326 MacroInfo *MI;
1327 IdentifierInfo *II;
1328
1329 AddLoadedMacroInfoRAII(Preprocessor &PP, MacroInfo *Hint)
1330 : PP(PP), Hint(Hint), MI(), II() { }
1331 ~AddLoadedMacroInfoRAII( ) {
1332 if (MI) {
1333 // Finally, install the macro.
1334 PP.addLoadedMacroInfo(II, MI, Hint);
1335 }
1336 }
1337 } AddLoadedMacroInfo(PP, Hint);
1338
Douglas Gregor37e26842009-04-21 23:56:24 +00001339 while (true) {
1340 unsigned Code = Stream.ReadCode();
1341 switch (Code) {
1342 case llvm::bitc::END_BLOCK:
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001343 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001344
1345 case llvm::bitc::ENTER_SUBBLOCK:
1346 // No known subblocks, always skip them.
1347 Stream.ReadSubBlockID();
1348 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001349 Error("malformed block record in AST file");
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001350 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001351 }
1352 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001353
Douglas Gregor37e26842009-04-21 23:56:24 +00001354 case llvm::bitc::DEFINE_ABBREV:
1355 Stream.ReadAbbrevRecord();
1356 continue;
1357 default: break;
1358 }
1359
1360 // Read a record.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001361 const char *BlobStart = 0;
1362 unsigned BlobLen = 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001363 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001364 PreprocessorRecordTypes RecType =
Michael J. Spencer20249a12010-10-21 03:16:25 +00001365 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart,
Douglas Gregorecdcb882010-10-20 22:00:55 +00001366 BlobLen);
Douglas Gregor37e26842009-04-21 23:56:24 +00001367 switch (RecType) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001368 case PP_MACRO_OBJECT_LIKE:
1369 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001370 // If we already have a macro, that means that we've hit the end
1371 // of the definition of the macro we were looking for. We're
1372 // done.
1373 if (Macro)
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001374 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001375
Douglas Gregor95eab172011-07-28 20:55:49 +00001376 IdentifierInfo *II = getLocalIdentifier(F, Record[0]);
Douglas Gregor37e26842009-04-21 23:56:24 +00001377 if (II == 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001378 Error("macro must have a name in AST file");
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001379 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001380 }
Mike Stump1eb44332009-09-09 15:08:12 +00001381
Douglas Gregora8235d62012-10-09 23:05:51 +00001382 unsigned GlobalID = getGlobalMacroID(F, Record[1]);
1383
1384 // If this macro has already been loaded, don't do so again.
1385 if (MacrosLoaded[GlobalID - NUM_PREDEF_MACRO_IDS])
1386 return;
1387
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001388 SubmoduleID GlobalSubmoduleID = getGlobalSubmoduleID(F, Record[2]);
1389 unsigned NextIndex = 3;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001390 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001391 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001392
Douglas Gregora8235d62012-10-09 23:05:51 +00001393 // Record this macro.
1394 MacrosLoaded[GlobalID - NUM_PREDEF_MACRO_IDS] = MI;
1395
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001396 SourceLocation UndefLoc = ReadSourceLocation(F, Record, NextIndex);
1397 if (UndefLoc.isValid())
1398 MI->setUndefLoc(UndefLoc);
1399
1400 MI->setIsUsed(Record[NextIndex++]);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001401 MI->setIsFromAST();
Mike Stump1eb44332009-09-09 15:08:12 +00001402
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001403 bool IsPublic = Record[NextIndex++];
Douglas Gregoraa93a872011-10-17 15:32:29 +00001404 MI->setVisibility(IsPublic, ReadSourceLocation(F, Record, NextIndex));
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001405
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001406 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregor37e26842009-04-21 23:56:24 +00001407 // Decode function-like macro info.
Douglas Gregor7143aab2011-09-01 17:04:32 +00001408 bool isC99VarArgs = Record[NextIndex++];
1409 bool isGNUVarArgs = Record[NextIndex++];
Douglas Gregor37e26842009-04-21 23:56:24 +00001410 MacroArgs.clear();
Douglas Gregor7143aab2011-09-01 17:04:32 +00001411 unsigned NumArgs = Record[NextIndex++];
Douglas Gregor37e26842009-04-21 23:56:24 +00001412 for (unsigned i = 0; i != NumArgs; ++i)
Douglas Gregor7143aab2011-09-01 17:04:32 +00001413 MacroArgs.push_back(getLocalIdentifier(F, Record[NextIndex++]));
Douglas Gregor37e26842009-04-21 23:56:24 +00001414
1415 // Install function-like macro info.
1416 MI->setIsFunctionLike();
1417 if (isC99VarArgs) MI->setIsC99Varargs();
1418 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor75fdb232009-05-22 22:45:36 +00001419 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001420 PP.getPreprocessorAllocator());
Douglas Gregor37e26842009-04-21 23:56:24 +00001421 }
1422
Douglas Gregora8235d62012-10-09 23:05:51 +00001423 if (DeserializationListener)
1424 DeserializationListener->MacroRead(GlobalID, MI);
1425
1426 // If an update record marked this as undefined, do so now.
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001427 // FIXME: Only if the submodule this update came from is visible?
Douglas Gregora8235d62012-10-09 23:05:51 +00001428 MacroUpdatesMap::iterator Update = MacroUpdates.find(GlobalID);
1429 if (Update != MacroUpdates.end()) {
1430 if (MI->getUndefLoc().isInvalid()) {
Douglas Gregor54c8a402012-10-12 00:16:50 +00001431 for (unsigned I = 0, N = Update->second.size(); I != N; ++I) {
1432 bool Hidden = false;
1433 if (unsigned SubmoduleID = Update->second[I].first) {
1434 if (Module *Owner = getSubmodule(SubmoduleID)) {
1435 if (Owner->NameVisibility == Module::Hidden) {
1436 // Note that this #undef is hidden.
1437 Hidden = true;
1438
1439 // Record this hiding for later.
1440 HiddenNamesMap[Owner].push_back(
1441 HiddenName(II, MI, Update->second[I].second.UndefLoc));
1442 }
1443 }
1444 }
1445
1446 if (!Hidden) {
1447 MI->setUndefLoc(Update->second[I].second.UndefLoc);
1448 if (PPMutationListener *Listener = PP.getPPMutationListener())
1449 Listener->UndefinedMacro(MI);
1450 break;
1451 }
1452 }
Douglas Gregora8235d62012-10-09 23:05:51 +00001453 }
1454 MacroUpdates.erase(Update);
1455 }
1456
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001457 // Determine whether this macro definition is visible.
1458 bool Hidden = !MI->isPublic();
1459 if (!Hidden && GlobalSubmoduleID) {
1460 if (Module *Owner = getSubmodule(GlobalSubmoduleID)) {
1461 if (Owner->NameVisibility == Module::Hidden) {
1462 // The owning module is not visible, and this macro definition
1463 // should not be, either.
1464 Hidden = true;
1465
1466 // Note that this macro definition was hidden because its owning
1467 // module is not yet visible.
1468 HiddenNamesMap[Owner].push_back(HiddenName(II, MI));
1469 }
1470 }
1471 }
1472 MI->setHidden(Hidden);
1473
Douglas Gregore8219a62012-10-11 21:07:39 +00001474 // Make sure we install the macro once we're done.
1475 AddLoadedMacroInfo.MI = MI;
1476 AddLoadedMacroInfo.II = II;
Douglas Gregor37e26842009-04-21 23:56:24 +00001477
1478 // Remember that we saw this macro last so that we add the tokens that
1479 // form its body to it.
1480 Macro = MI;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001481
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001482 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1483 Record[NextIndex]) {
1484 // We have a macro definition. Register the association
1485 PreprocessedEntityID
1486 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1487 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1488 PPRec.RegisterMacroDefinition(Macro,
1489 PPRec.getPPEntityID(GlobalID-1, /*isLoaded=*/true));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001490 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001491
Douglas Gregor37e26842009-04-21 23:56:24 +00001492 ++NumMacrosRead;
1493 break;
1494 }
Mike Stump1eb44332009-09-09 15:08:12 +00001495
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001496 case PP_TOKEN: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001497 // If we see a TOKEN before a PP_MACRO_*, then the file is
1498 // erroneous, just pretend we didn't see this.
1499 if (Macro == 0) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001500
Douglas Gregor37e26842009-04-21 23:56:24 +00001501 Token Tok;
1502 Tok.startToken();
Sebastian Redlc3632732010-10-05 15:59:54 +00001503 Tok.setLocation(ReadSourceLocation(F, Record[0]));
Douglas Gregor37e26842009-04-21 23:56:24 +00001504 Tok.setLength(Record[1]);
Douglas Gregor95eab172011-07-28 20:55:49 +00001505 if (IdentifierInfo *II = getLocalIdentifier(F, Record[2]))
Douglas Gregor37e26842009-04-21 23:56:24 +00001506 Tok.setIdentifierInfo(II);
1507 Tok.setKind((tok::TokenKind)Record[3]);
1508 Tok.setFlag((Token::TokenFlags)Record[4]);
1509 Macro->AddTokenToBody(Tok);
1510 break;
1511 }
David Blaikie7530c032012-01-17 06:56:22 +00001512 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001513 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001514}
1515
Douglas Gregor86c67d82011-07-28 22:39:26 +00001516PreprocessedEntityID
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001517ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const {
Argyrios Kyrtzidis1f6d2252011-09-19 20:40:02 +00001518 ContinuousRangeMap<uint32_t, int, 2>::const_iterator
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001519 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1520 assert(I != M.PreprocessedEntityRemap.end()
1521 && "Invalid index into preprocessed entity index remap");
1522
1523 return LocalID + I->second;
Douglas Gregor86c67d82011-07-28 22:39:26 +00001524}
1525
Douglas Gregor98339b92011-08-25 20:47:51 +00001526unsigned HeaderFileInfoTrait::ComputeHash(const char *path) {
1527 return llvm::HashString(llvm::sys::path::filename(path));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001528}
Douglas Gregor98339b92011-08-25 20:47:51 +00001529
1530HeaderFileInfoTrait::internal_key_type
1531HeaderFileInfoTrait::GetInternalKey(const char *path) { return path; }
1532
1533bool HeaderFileInfoTrait::EqualKey(internal_key_type a, internal_key_type b) {
1534 if (strcmp(a, b) == 0)
1535 return true;
1536
1537 if (llvm::sys::path::filename(a) != llvm::sys::path::filename(b))
1538 return false;
Douglas Gregor99a922b2011-12-09 16:22:07 +00001539
1540 // Determine whether the actual files are equivalent.
1541 bool Result = false;
1542 if (llvm::sys::fs::equivalent(a, b, Result))
Douglas Gregor98339b92011-08-25 20:47:51 +00001543 return false;
1544
Douglas Gregor99a922b2011-12-09 16:22:07 +00001545 return Result;
Douglas Gregor98339b92011-08-25 20:47:51 +00001546}
1547
1548std::pair<unsigned, unsigned>
1549HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1550 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1551 unsigned DataLen = (unsigned) *d++;
1552 return std::make_pair(KeyLen + 1, DataLen);
1553}
1554
1555HeaderFileInfoTrait::data_type
1556HeaderFileInfoTrait::ReadData(const internal_key_type, const unsigned char *d,
1557 unsigned DataLen) {
1558 const unsigned char *End = d + DataLen;
1559 using namespace clang::io;
1560 HeaderFileInfo HFI;
1561 unsigned Flags = *d++;
1562 HFI.isImport = (Flags >> 5) & 0x01;
1563 HFI.isPragmaOnce = (Flags >> 4) & 0x01;
1564 HFI.DirInfo = (Flags >> 2) & 0x03;
1565 HFI.Resolved = (Flags >> 1) & 0x01;
1566 HFI.IndexHeaderMapHeader = Flags & 0x01;
1567 HFI.NumIncludes = ReadUnalignedLE16(d);
Douglas Gregor541ba162011-10-17 18:53:12 +00001568 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(M,
1569 ReadUnalignedLE32(d));
Douglas Gregor98339b92011-08-25 20:47:51 +00001570 if (unsigned FrameworkOffset = ReadUnalignedLE32(d)) {
1571 // The framework offset is 1 greater than the actual offset,
1572 // since 0 is used as an indicator for "no framework name".
1573 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1574 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1575 }
1576
1577 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1578 (void)End;
1579
1580 // This HeaderFileInfo was externally loaded.
1581 HFI.External = true;
1582 return HFI;
1583}
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001584
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001585void ASTReader::setIdentifierIsMacro(IdentifierInfo *II, ArrayRef<MacroID> IDs){
1586 II->setHadMacroDefinition(true);
1587 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1588 PendingMacroIDs[II].append(IDs.begin(), IDs.end());
Douglas Gregor295a2a62010-10-30 00:23:06 +00001589}
1590
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001591void ASTReader::ReadDefinedMacros() {
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001592 // Note that we are loading defined macros.
1593 Deserializing Macros(this);
1594
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00001595 for (ModuleReverseIterator I = ModuleMgr.rbegin(),
1596 E = ModuleMgr.rend(); I != E; ++I) {
1597 llvm::BitstreamCursor &MacroCursor = (*I)->MacroCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001598
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001599 // If there was no preprocessor block, skip this file.
1600 if (!MacroCursor.getBitStreamReader())
1601 continue;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001602
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001603 llvm::BitstreamCursor Cursor = MacroCursor;
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00001604 Cursor.JumpToBit((*I)->MacroStartOffset);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001605
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001606 RecordData Record;
1607 while (true) {
1608 unsigned Code = Cursor.ReadCode();
Douglas Gregorecdcb882010-10-20 22:00:55 +00001609 if (Code == llvm::bitc::END_BLOCK)
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001610 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001611
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001612 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1613 // No known subblocks, always skip them.
1614 Cursor.ReadSubBlockID();
1615 if (Cursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001616 Error("malformed block record in AST file");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001617 return;
1618 }
1619 continue;
1620 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001621
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001622 if (Code == llvm::bitc::DEFINE_ABBREV) {
1623 Cursor.ReadAbbrevRecord();
1624 continue;
1625 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001626
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001627 // Read a record.
1628 const char *BlobStart;
1629 unsigned BlobLen;
1630 Record.clear();
1631 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1632 default: // Default behavior: ignore.
1633 break;
Douglas Gregor88a35862010-01-04 19:18:44 +00001634
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001635 case PP_MACRO_OBJECT_LIKE:
1636 case PP_MACRO_FUNCTION_LIKE:
Douglas Gregor95eab172011-07-28 20:55:49 +00001637 getLocalIdentifier(**I, Record[0]);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001638 break;
1639
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001640 case PP_TOKEN:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001641 // Ignore tokens.
1642 break;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001643 }
Douglas Gregor88a35862010-01-04 19:18:44 +00001644 }
1645 }
Douglas Gregor295a2a62010-10-30 00:23:06 +00001646}
1647
Douglas Gregoreee242f2011-10-27 09:33:13 +00001648namespace {
1649 /// \brief Visitor class used to look up identifirs in an AST file.
1650 class IdentifierLookupVisitor {
1651 StringRef Name;
Douglas Gregor057df202012-01-18 20:56:22 +00001652 unsigned PriorGeneration;
Douglas Gregoreee242f2011-10-27 09:33:13 +00001653 IdentifierInfo *Found;
1654 public:
Douglas Gregor057df202012-01-18 20:56:22 +00001655 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration)
1656 : Name(Name), PriorGeneration(PriorGeneration), Found() { }
Douglas Gregoreee242f2011-10-27 09:33:13 +00001657
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001658 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00001659 IdentifierLookupVisitor *This
1660 = static_cast<IdentifierLookupVisitor *>(UserData);
1661
Douglas Gregor057df202012-01-18 20:56:22 +00001662 // If we've already searched this module file, skip it now.
1663 if (M.Generation <= This->PriorGeneration)
1664 return true;
1665
Douglas Gregoreee242f2011-10-27 09:33:13 +00001666 ASTIdentifierLookupTable *IdTable
1667 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
1668 if (!IdTable)
1669 return false;
1670
Douglas Gregor5d5051f2012-01-24 15:24:38 +00001671 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(),
1672 M, This->Found);
1673
Douglas Gregoreee242f2011-10-27 09:33:13 +00001674 std::pair<const char*, unsigned> Key(This->Name.begin(),
1675 This->Name.size());
Douglas Gregor5d5051f2012-01-24 15:24:38 +00001676 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Trait);
Douglas Gregoreee242f2011-10-27 09:33:13 +00001677 if (Pos == IdTable->end())
1678 return false;
1679
1680 // Dereferencing the iterator has the effect of building the
1681 // IdentifierInfo node and populating it with the various
1682 // declarations it needs.
1683 This->Found = *Pos;
1684 return true;
1685 }
1686
1687 // \brief Retrieve the identifier info found within the module
1688 // files.
1689 IdentifierInfo *getIdentifierInfo() const { return Found; }
1690 };
1691}
1692
1693void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001694 // Note that we are loading an identifier.
1695 Deserializing AnIdentifier(this);
1696
Douglas Gregor057df202012-01-18 20:56:22 +00001697 unsigned PriorGeneration = 0;
David Blaikie4e4d0842012-03-11 07:00:24 +00001698 if (getContext().getLangOpts().Modules)
Douglas Gregor057df202012-01-18 20:56:22 +00001699 PriorGeneration = IdentifierGeneration[&II];
1700
1701 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration);
1702 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
1703 markIdentifierUpToDate(&II);
1704}
1705
1706void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
1707 if (!II)
1708 return;
1709
1710 II->setOutOfDate(false);
1711
1712 // Update the generation for this identifier.
David Blaikie4e4d0842012-03-11 07:00:24 +00001713 if (getContext().getLangOpts().Modules)
Douglas Gregor057df202012-01-18 20:56:22 +00001714 IdentifierGeneration[II] = CurrentGeneration;
Douglas Gregoreee242f2011-10-27 09:33:13 +00001715}
1716
Douglas Gregora930dc92012-10-22 18:42:04 +00001717llvm::PointerIntPair<const FileEntry *, 1, bool>
Douglas Gregor38295be2012-10-22 23:51:00 +00001718ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
Douglas Gregora930dc92012-10-22 18:42:04 +00001719 // If this ID is bogus, just return an empty input file.
1720 if (ID == 0 || ID > F.InputFilesLoaded.size())
1721 return InputFile();
1722
1723 // If we've already loaded this input file, return it.
1724 if (F.InputFilesLoaded[ID-1].getPointer())
1725 return F.InputFilesLoaded[ID-1];
1726
1727 // Go find this input file.
1728 llvm::BitstreamCursor &Cursor = F.InputFilesCursor;
1729 SavedStreamPosition SavedPosition(Cursor);
1730 Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
1731
1732 unsigned Code = Cursor.ReadCode();
1733 RecordData Record;
1734 const char *BlobStart = 0;
1735 unsigned BlobLen = 0;
1736 switch ((InputFileRecordTypes)Cursor.ReadRecord(Code, Record,
1737 &BlobStart, &BlobLen)) {
1738 case INPUT_FILE: {
1739 unsigned StoredID = Record[0];
1740 assert(ID == StoredID && "Bogus stored ID or offset");
NAKAMURA Takumi6b8194e2012-10-22 21:50:39 +00001741 (void)StoredID;
Douglas Gregora930dc92012-10-22 18:42:04 +00001742 off_t StoredSize = (off_t)Record[1];
1743 time_t StoredTime = (time_t)Record[2];
1744 bool Overridden = (bool)Record[3];
1745
1746 // Get the file entry for this input file.
1747 StringRef OrigFilename(BlobStart, BlobLen);
1748 std::string Filename = OrigFilename;
1749 MaybeAddSystemRootToFilename(F, Filename);
1750 const FileEntry *File
1751 = Overridden? FileMgr.getVirtualFile(Filename, StoredSize, StoredTime)
1752 : FileMgr.getFile(Filename, /*OpenFile=*/false);
1753
1754 // If we didn't find the file, resolve it relative to the
1755 // original directory from which this AST file was created.
1756 if (File == 0 && !F.OriginalDir.empty() && !CurrentDir.empty() &&
1757 F.OriginalDir != CurrentDir) {
1758 std::string Resolved = resolveFileRelativeToOriginalDir(Filename,
1759 F.OriginalDir,
1760 CurrentDir);
1761 if (!Resolved.empty())
1762 File = FileMgr.getFile(Resolved);
1763 }
1764
1765 // For an overridden file, create a virtual file with the stored
1766 // size/timestamp.
1767 if (Overridden && File == 0) {
1768 File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
1769 }
1770
1771 if (File == 0) {
Douglas Gregor38295be2012-10-22 23:51:00 +00001772 if (Complain) {
1773 std::string ErrorStr = "could not find file '";
1774 ErrorStr += Filename;
1775 ErrorStr += "' referenced by AST file";
1776 Error(ErrorStr.c_str());
1777 }
Douglas Gregora930dc92012-10-22 18:42:04 +00001778 return InputFile();
1779 }
1780
1781 // Note that we've loaded this input file.
1782 F.InputFilesLoaded[ID-1] = InputFile(File, Overridden);
1783
1784 // Check if there was a request to override the contents of the file
1785 // that was part of the precompiled header. Overridding such a file
1786 // can lead to problems when lexing using the source locations from the
1787 // PCH.
1788 SourceManager &SM = getSourceManager();
1789 if (!Overridden && SM.isFileOverridden(File)) {
1790 Error(diag::err_fe_pch_file_overridden, Filename);
1791 // After emitting the diagnostic, recover by disabling the override so
1792 // that the original file will be used.
1793 SM.disableFileContentsOverride(File);
1794 // The FileEntry is a virtual file entry with the size of the contents
1795 // that would override the original contents. Set it to the original's
1796 // size/time.
1797 FileMgr.modifyFileEntry(const_cast<FileEntry*>(File),
1798 StoredSize, StoredTime);
1799 }
1800
1801 // For an overridden file, there is nothing to validate.
1802 if (Overridden)
1803 return InputFile(File, Overridden);
1804
1805 // The stat info from the FileEntry came from the cached stat
1806 // info of the PCH, so we cannot trust it.
1807 struct stat StatBuf;
1808 if (::stat(File->getName(), &StatBuf) != 0) {
1809 StatBuf.st_size = File->getSize();
1810 StatBuf.st_mtime = File->getModificationTime();
1811 }
1812
1813 if ((StoredSize != StatBuf.st_size
1814#if !defined(LLVM_ON_WIN32)
1815 // In our regression testing, the Windows file system seems to
1816 // have inconsistent modification times that sometimes
1817 // erroneously trigger this error-handling path.
1818 || StoredTime != StatBuf.st_mtime
1819#endif
1820 )) {
Douglas Gregor38295be2012-10-22 23:51:00 +00001821 if (Complain)
1822 Error(diag::err_fe_pch_file_modified, Filename);
1823
Douglas Gregora930dc92012-10-22 18:42:04 +00001824 return InputFile();
1825 }
1826
1827 return InputFile(File, Overridden);
1828 }
1829 }
1830
1831 return InputFile();
1832}
1833
Chris Lattner5f9e2722011-07-23 10:55:15 +00001834const FileEntry *ASTReader::getFileEntry(StringRef filenameStrRef) {
Douglas Gregor69e16082012-10-18 21:47:16 +00001835 ModuleFile &M = ModuleMgr.getPrimaryModule();
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00001836 std::string Filename = filenameStrRef;
Douglas Gregor69e16082012-10-18 21:47:16 +00001837 MaybeAddSystemRootToFilename(M, Filename);
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00001838 const FileEntry *File = FileMgr.getFile(Filename);
Douglas Gregor69e16082012-10-18 21:47:16 +00001839 if (File == 0 && !M.OriginalDir.empty() && !CurrentDir.empty() &&
1840 M.OriginalDir != CurrentDir) {
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00001841 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
Douglas Gregor69e16082012-10-18 21:47:16 +00001842 M.OriginalDir,
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00001843 CurrentDir);
1844 if (!resolved.empty())
1845 File = FileMgr.getFile(resolved);
1846 }
1847
1848 return File;
1849}
1850
Douglas Gregore650c8c2009-07-07 00:12:59 +00001851/// \brief If we are loading a relocatable PCH file, and the filename is
1852/// not an absolute path, add the system root to the beginning of the file
1853/// name.
Douglas Gregora930dc92012-10-22 18:42:04 +00001854StringRef ASTReader::MaybeAddSystemRootToFilename(ModuleFile &M,
1855 std::string &Filename) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001856 // If this is not a relocatable PCH file, there's nothing to do.
Douglas Gregorcaed0602012-10-18 21:31:35 +00001857 if (!M.RelocatablePCH)
Douglas Gregora930dc92012-10-22 18:42:04 +00001858 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +00001859
Michael J. Spencer256053b2010-12-17 21:22:22 +00001860 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
Douglas Gregora930dc92012-10-22 18:42:04 +00001861 return Filename;
Douglas Gregore650c8c2009-07-07 00:12:59 +00001862
Douglas Gregor832d6202011-07-22 16:35:34 +00001863 if (isysroot.empty()) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001864 // If no system root was given, default to '/'
1865 Filename.insert(Filename.begin(), '/');
Douglas Gregora930dc92012-10-22 18:42:04 +00001866 return Filename;
Douglas Gregore650c8c2009-07-07 00:12:59 +00001867 }
Mike Stump1eb44332009-09-09 15:08:12 +00001868
Douglas Gregor832d6202011-07-22 16:35:34 +00001869 unsigned Length = isysroot.size();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001870 if (isysroot[Length - 1] != '/')
1871 Filename.insert(Filename.begin(), '/');
Mike Stump1eb44332009-09-09 15:08:12 +00001872
Douglas Gregor832d6202011-07-22 16:35:34 +00001873 Filename.insert(Filename.begin(), isysroot.begin(), isysroot.end());
Douglas Gregora930dc92012-10-22 18:42:04 +00001874 return Filename;
Douglas Gregore650c8c2009-07-07 00:12:59 +00001875}
1876
Douglas Gregor38295be2012-10-22 23:51:00 +00001877ASTReader::ASTReadResult
1878ASTReader::ReadControlBlock(ModuleFile &F,
1879 llvm::SmallVectorImpl<ModuleFile *> &Loaded,
1880 unsigned ClientLoadCapabilities) {
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001881 llvm::BitstreamCursor &Stream = F.Stream;
1882
1883 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
1884 Error("malformed block record in AST file");
1885 return Failure;
1886 }
1887
1888 // Read all of the records and blocks in the control block.
1889 RecordData Record;
1890 while (!Stream.AtEndOfStream()) {
1891 unsigned Code = Stream.ReadCode();
1892 if (Code == llvm::bitc::END_BLOCK) {
1893 if (Stream.ReadBlockEnd()) {
1894 Error("error at end of control block in AST file");
1895 return Failure;
1896 }
1897
Douglas Gregora930dc92012-10-22 18:42:04 +00001898 // Validate all of the input files.
1899 if (!DisableValidation) {
Douglas Gregor38295be2012-10-22 23:51:00 +00001900 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
Douglas Gregora930dc92012-10-22 18:42:04 +00001901 for (unsigned I = 0, N = Record[0]; I < N; ++I)
Douglas Gregor38295be2012-10-22 23:51:00 +00001902 if (!getInputFile(F, I+1, Complain).getPointer())
Douglas Gregor4825fd72012-10-22 22:50:17 +00001903 return OutOfDate;
Douglas Gregora930dc92012-10-22 18:42:04 +00001904 }
1905
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001906 return Success;
1907 }
1908
1909 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
Douglas Gregor745e6f12012-10-19 00:38:02 +00001910 switch (Stream.ReadSubBlockID()) {
1911 case INPUT_FILES_BLOCK_ID:
Douglas Gregora930dc92012-10-22 18:42:04 +00001912 F.InputFilesCursor = Stream;
1913 if (Stream.SkipBlock() || // Skip with the main cursor
1914 // Read the abbreviations
1915 ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
1916 Error("malformed block record in AST file");
Douglas Gregor745e6f12012-10-19 00:38:02 +00001917 return Failure;
Douglas Gregor745e6f12012-10-19 00:38:02 +00001918 }
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001919 continue;
Douglas Gregor745e6f12012-10-19 00:38:02 +00001920
1921 default:
1922 if (!Stream.SkipBlock())
1923 continue;
1924 break;
1925 }
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001926
1927 Error("malformed block record in AST file");
1928 return Failure;
1929 }
1930
1931 if (Code == llvm::bitc::DEFINE_ABBREV) {
1932 Stream.ReadAbbrevRecord();
1933 continue;
1934 }
1935
1936 // Read and process a record.
1937 Record.clear();
1938 const char *BlobStart = 0;
1939 unsigned BlobLen = 0;
1940 switch ((ControlRecordTypes)Stream.ReadRecord(Code, Record,
1941 &BlobStart, &BlobLen)) {
1942 case METADATA: {
1943 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
Douglas Gregor38295be2012-10-22 23:51:00 +00001944 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
1945 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
1946 : diag::warn_pch_version_too_new);
Douglas Gregor4825fd72012-10-22 22:50:17 +00001947 return VersionMismatch;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001948 }
1949
1950 bool hasErrors = Record[5];
1951 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
1952 Diag(diag::err_pch_with_compiler_errors);
Douglas Gregor4825fd72012-10-22 22:50:17 +00001953 return HadErrors;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001954 }
1955
Douglas Gregorcaed0602012-10-18 21:31:35 +00001956 F.RelocatablePCH = Record[4];
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001957
1958 const std::string &CurBranch = getClangFullRepositoryVersion();
1959 StringRef ASTBranch(BlobStart, BlobLen);
1960 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
Douglas Gregor38295be2012-10-22 23:51:00 +00001961 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
1962 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregor4825fd72012-10-22 22:50:17 +00001963 return VersionMismatch;
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001964 }
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001965 break;
1966 }
1967
1968 case IMPORTS: {
1969 // Load each of the imported PCH files.
1970 unsigned Idx = 0, N = Record.size();
1971 while (Idx < N) {
1972 // Read information about the AST file.
1973 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
1974 unsigned Length = Record[Idx++];
1975 SmallString<128> ImportedFile(Record.begin() + Idx,
1976 Record.begin() + Idx + Length);
1977 Idx += Length;
1978
1979 // Load the AST file.
Douglas Gregor38295be2012-10-22 23:51:00 +00001980 switch(ReadASTCore(ImportedFile, ImportedKind, &F, Loaded,
1981 ClientLoadCapabilities)) {
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001982 case Failure: return Failure;
1983 // If we have to ignore the dependency, we'll have to ignore this too.
Douglas Gregor4825fd72012-10-22 22:50:17 +00001984 case OutOfDate: return OutOfDate;
1985 case VersionMismatch: return VersionMismatch;
1986 case ConfigurationMismatch: return ConfigurationMismatch;
1987 case HadErrors: return HadErrors;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001988 case Success: break;
1989 }
1990 }
1991 break;
1992 }
1993
Douglas Gregor38295be2012-10-22 23:51:00 +00001994 case LANGUAGE_OPTIONS: {
1995 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
1996 if (Listener && &F == *ModuleMgr.begin() &&
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00001997 ParseLanguageOptions(Record, Complain, *Listener) &&
1998 !DisableValidation)
Douglas Gregor4825fd72012-10-22 22:50:17 +00001999 return ConfigurationMismatch;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002000 break;
Douglas Gregor38295be2012-10-22 23:51:00 +00002001 }
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002002
Douglas Gregoree097c12012-10-18 17:58:09 +00002003 case TARGET_OPTIONS: {
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00002004 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2005 if (Listener && &F == *ModuleMgr.begin() &&
2006 ParseTargetOptions(Record, Complain, *Listener) &&
2007 !DisableValidation)
2008 return ConfigurationMismatch;
Douglas Gregoree097c12012-10-18 17:58:09 +00002009 break;
2010 }
2011
Douglas Gregor5f3d8222012-10-24 15:17:15 +00002012 case DIAGNOSTIC_OPTIONS: {
2013 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2014 if (Listener && &F == *ModuleMgr.begin() &&
2015 ParseDiagnosticOptions(Record, Complain, *Listener) &&
2016 !DisableValidation)
2017 return ConfigurationMismatch;
2018 break;
2019 }
Douglas Gregor1b2c3c02012-10-24 15:49:58 +00002020
2021 case FILE_SYSTEM_OPTIONS: {
2022 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2023 if (Listener && &F == *ModuleMgr.begin() &&
2024 ParseFileSystemOptions(Record, Complain, *Listener) &&
2025 !DisableValidation)
2026 return ConfigurationMismatch;
2027 break;
2028 }
2029
Douglas Gregorbbf38312012-10-24 16:50:34 +00002030 case HEADER_SEARCH_OPTIONS: {
2031 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2032 if (Listener && &F == *ModuleMgr.begin() &&
2033 ParseHeaderSearchOptions(Record, Complain, *Listener) &&
2034 !DisableValidation)
2035 return ConfigurationMismatch;
2036 break;
2037 }
2038
Douglas Gregora71a7d82012-10-24 20:05:57 +00002039 case PREPROCESSOR_OPTIONS: {
2040 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2041 if (Listener && &F == *ModuleMgr.begin() &&
2042 ParsePreprocessorOptions(Record, Complain, *Listener) &&
2043 !DisableValidation)
2044 return ConfigurationMismatch;
2045 break;
2046 }
2047
Douglas Gregor39c497b2012-10-18 18:36:53 +00002048 case ORIGINAL_FILE:
Douglas Gregor69e16082012-10-18 21:47:16 +00002049 F.OriginalSourceFileID = FileID::get(Record[0]);
2050 F.ActualOriginalSourceFileName.assign(BlobStart, BlobLen);
2051 F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
2052 MaybeAddSystemRootToFilename(F, F.OriginalSourceFileName);
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002053 break;
2054
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002055 case ORIGINAL_PCH_DIR:
Douglas Gregor69e16082012-10-18 21:47:16 +00002056 F.OriginalDir.assign(BlobStart, BlobLen);
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002057 break;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002058
Douglas Gregora930dc92012-10-22 18:42:04 +00002059 case INPUT_FILE_OFFSETS:
2060 F.InputFileOffsets = (const uint32_t *)BlobStart;
2061 F.InputFilesLoaded.resize(Record[0]);
Douglas Gregor745e6f12012-10-19 00:38:02 +00002062 break;
2063 }
Douglas Gregor745e6f12012-10-19 00:38:02 +00002064 }
2065
2066 Error("premature end of bitstream in AST file");
2067 return Failure;
2068}
2069
Douglas Gregor4825fd72012-10-22 22:50:17 +00002070bool ASTReader::ReadASTBlock(ModuleFile &F) {
Sebastian Redl9137a522010-07-16 17:50:48 +00002071 llvm::BitstreamCursor &Stream = F.Stream;
2072
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002073 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002074 Error("malformed block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002075 return true;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002076 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002077
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002078 // Read all of the records and blocks for the AST file.
Douglas Gregor8038d512009-04-10 17:25:41 +00002079 RecordData Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002080 while (!Stream.AtEndOfStream()) {
2081 unsigned Code = Stream.ReadCode();
2082 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002083 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002084 Error("error at end of module block in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002085 return true;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002086 }
Chris Lattner7356a312009-04-11 21:15:38 +00002087
Argyrios Kyrtzidis1f941242012-09-21 01:30:00 +00002088 DeclContext *DC = Context.getTranslationUnitDecl();
2089 if (!DC->hasExternalVisibleStorage() && DC->hasExternalLexicalStorage())
2090 DC->setMustBuildLookupTable();
2091
Douglas Gregor4825fd72012-10-22 22:50:17 +00002092 return false;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002093 }
2094
2095 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2096 switch (Stream.ReadSubBlockID()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002097 case DECLTYPES_BLOCK_ID:
Chris Lattner6367f6d2009-04-27 01:05:14 +00002098 // We lazily load the decls block, but we want to set up the
2099 // DeclsCursor cursor to point into it. Clone our current bitcode
2100 // cursor to it, enter the block and read the abbrevs in that block.
2101 // With the main cursor, we just skip over it.
Sebastian Redl9137a522010-07-16 17:50:48 +00002102 F.DeclsCursor = Stream;
Chris Lattner6367f6d2009-04-27 01:05:14 +00002103 if (Stream.SkipBlock() || // Skip with the main cursor.
2104 // Read the abbrevs.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002105 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002106 Error("malformed block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002107 return true;
Chris Lattner6367f6d2009-04-27 01:05:14 +00002108 }
2109 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002110
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002111 case DECL_UPDATES_BLOCK_ID:
2112 if (Stream.SkipBlock()) {
2113 Error("malformed block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002114 return true;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002115 }
2116 break;
2117
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002118 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl9137a522010-07-16 17:50:48 +00002119 F.MacroCursor = Stream;
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002120 if (!PP.getExternalSource())
2121 PP.setExternalSource(this);
Douglas Gregor88a35862010-01-04 19:18:44 +00002122
Douglas Gregorecdcb882010-10-20 22:00:55 +00002123 if (Stream.SkipBlock() ||
2124 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002125 Error("malformed block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002126 return true;
Chris Lattner7356a312009-04-11 21:15:38 +00002127 }
Douglas Gregorecdcb882010-10-20 22:00:55 +00002128 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
Chris Lattner7356a312009-04-11 21:15:38 +00002129 break;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002130
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002131 case PREPROCESSOR_DETAIL_BLOCK_ID:
2132 F.PreprocessorDetailCursor = Stream;
2133 if (Stream.SkipBlock() ||
2134 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
2135 PREPROCESSOR_DETAIL_BLOCK_ID)) {
2136 Error("malformed preprocessor detail record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002137 return true;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002138 }
2139 F.PreprocessorDetailStartOffset
2140 = F.PreprocessorDetailCursor.GetCurrentBitNo();
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002141
2142 if (!PP.getPreprocessingRecord())
Argyrios Kyrtzidisc6c54522012-03-05 05:48:17 +00002143 PP.createPreprocessingRecord(/*RecordConditionalDirectives=*/false);
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002144 if (!PP.getPreprocessingRecord()->getExternalSource())
2145 PP.getPreprocessingRecord()->SetExternalSource(*this);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002146 break;
2147
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002148 case SOURCE_MANAGER_BLOCK_ID:
Douglas Gregor4825fd72012-10-22 22:50:17 +00002149 if (ReadSourceManagerBlock(F))
2150 return true;
Douglas Gregor14f79002009-04-10 03:52:48 +00002151 break;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002152
2153 case SUBMODULE_BLOCK_ID:
Douglas Gregor4825fd72012-10-22 22:50:17 +00002154 if (ReadSubmoduleBlock(F))
2155 return true;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002156 break;
2157
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002158 case COMMENTS_BLOCK_ID: {
2159 llvm::BitstreamCursor C = Stream;
2160 if (Stream.SkipBlock() ||
2161 ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
2162 Error("malformed comments block in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002163 return true;
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002164 }
2165 CommentsCursors.push_back(std::make_pair(C, &F));
2166 break;
2167 }
2168
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002169 default:
2170 if (!Stream.SkipBlock())
2171 break;
2172 Error("malformed block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002173 return true;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002174 }
Douglas Gregor8038d512009-04-10 17:25:41 +00002175 continue;
2176 }
2177
2178 if (Code == llvm::bitc::DEFINE_ABBREV) {
2179 Stream.ReadAbbrevRecord();
2180 continue;
2181 }
2182
2183 // Read and process a record.
2184 Record.clear();
Douglas Gregor2bec0412009-04-10 21:16:55 +00002185 const char *BlobStart = 0;
2186 unsigned BlobLen = 0;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002187 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00002188 &BlobStart, &BlobLen)) {
Douglas Gregor8038d512009-04-10 17:25:41 +00002189 default: // Default behavior: ignore.
2190 break;
2191
Douglas Gregora119da02011-08-02 16:26:37 +00002192 case TYPE_OFFSET: {
Sebastian Redl12d6da02010-07-19 22:06:55 +00002193 if (F.LocalNumTypes != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002194 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002195 return true;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002196 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00002197 F.TypeOffsets = (const uint32_t *)BlobStart;
2198 F.LocalNumTypes = Record[0];
Douglas Gregore3605012011-08-02 18:32:54 +00002199 unsigned LocalBaseTypeIndex = Record[1];
2200 F.BaseTypeIndex = getTotalNumTypes();
Douglas Gregor1e849b62011-07-29 00:21:44 +00002201
Douglas Gregora119da02011-08-02 16:26:37 +00002202 if (F.LocalNumTypes > 0) {
2203 // Introduce the global -> local mapping for types within this module.
2204 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
2205
2206 // Introduce the local -> global mapping for types within this module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00002207 F.TypeRemap.insertOrReplace(
2208 std::make_pair(LocalBaseTypeIndex,
2209 F.BaseTypeIndex - LocalBaseTypeIndex));
Douglas Gregora119da02011-08-02 16:26:37 +00002210
2211 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
2212 }
Douglas Gregor8038d512009-04-10 17:25:41 +00002213 break;
Douglas Gregora119da02011-08-02 16:26:37 +00002214 }
2215
Douglas Gregor496c7092011-08-03 15:48:04 +00002216 case DECL_OFFSET: {
Sebastian Redl12d6da02010-07-19 22:06:55 +00002217 if (F.LocalNumDecls != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002218 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002219 return true;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002220 }
Argyrios Kyrtzidis9d31fa72011-10-27 18:47:35 +00002221 F.DeclOffsets = (const DeclOffset *)BlobStart;
Sebastian Redl12d6da02010-07-19 22:06:55 +00002222 F.LocalNumDecls = Record[0];
Douglas Gregor496c7092011-08-03 15:48:04 +00002223 unsigned LocalBaseDeclID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00002224 F.BaseDeclID = getTotalNumDecls();
Douglas Gregor96e973f2011-07-20 00:27:43 +00002225
Douglas Gregor496c7092011-08-03 15:48:04 +00002226 if (F.LocalNumDecls > 0) {
2227 // Introduce the global -> local mapping for declarations within this
2228 // module.
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002229 GlobalDeclMap.insert(
2230 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
Douglas Gregor496c7092011-08-03 15:48:04 +00002231
2232 // Introduce the local -> global mapping for declarations within this
2233 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00002234 F.DeclRemap.insertOrReplace(
2235 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
Douglas Gregor496c7092011-08-03 15:48:04 +00002236
Douglas Gregora1be2782011-12-17 23:38:30 +00002237 // Introduce the global -> local mapping for declarations within this
2238 // module.
2239 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
2240
Douglas Gregor496c7092011-08-03 15:48:04 +00002241 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
2242 }
Douglas Gregor8038d512009-04-10 17:25:41 +00002243 break;
Douglas Gregor496c7092011-08-03 15:48:04 +00002244 }
2245
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002246 case TU_UPDATE_LEXICAL: {
Douglas Gregor35942772011-09-09 21:34:22 +00002247 DeclContext *TU = Context.getTranslationUnitDecl();
Douglas Gregor0d95f772011-08-24 19:03:07 +00002248 DeclContextInfo &Info = F.DeclContextInfos[TU];
2249 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(BlobStart);
2250 Info.NumLexicalDecls
2251 = static_cast<unsigned int>(BlobLen / sizeof(KindDeclIDPair));
Douglas Gregor35942772011-09-09 21:34:22 +00002252 TU->setHasExternalLexicalStorage(true);
Sebastian Redld692af72010-07-27 18:24:41 +00002253 break;
2254 }
2255
Sebastian Redle1dde812010-08-24 00:50:04 +00002256 case UPDATE_VISIBLE: {
Douglas Gregor496c7092011-08-03 15:48:04 +00002257 unsigned Idx = 0;
2258 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
Benjamin Kramerb1758c62012-04-15 12:36:49 +00002259 ASTDeclContextNameLookupTable *Table =
2260 ASTDeclContextNameLookupTable::Create(
Douglas Gregor496c7092011-08-03 15:48:04 +00002261 (const unsigned char *)BlobStart + Record[Idx++],
Sebastian Redle1dde812010-08-24 00:50:04 +00002262 (const unsigned char *)BlobStart,
Douglas Gregor393f2492011-07-22 00:38:23 +00002263 ASTDeclContextNameLookupTrait(*this, F));
Douglas Gregor35942772011-09-09 21:34:22 +00002264 if (ID == PREDEF_DECL_TRANSLATION_UNIT_ID) { // Is it the TU?
2265 DeclContext *TU = Context.getTranslationUnitDecl();
Douglas Gregor0d95f772011-08-24 19:03:07 +00002266 F.DeclContextInfos[TU].NameLookupTableData = Table;
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002267 TU->setHasExternalVisibleStorage(true);
Sebastian Redle1dde812010-08-24 00:50:04 +00002268 } else
Douglas Gregor496c7092011-08-03 15:48:04 +00002269 PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F));
Sebastian Redle1dde812010-08-24 00:50:04 +00002270 break;
2271 }
2272
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002273 case IDENTIFIER_TABLE:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002274 F.IdentifierTableData = BlobStart;
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002275 if (Record[0]) {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002276 F.IdentifierLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002277 = ASTIdentifierLookupTable::Create(
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002278 (const unsigned char *)F.IdentifierTableData + Record[0],
2279 (const unsigned char *)F.IdentifierTableData,
Sebastian Redlc3632732010-10-05 15:59:54 +00002280 ASTIdentifierLookupTrait(*this, F));
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002281
2282 PP.getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002283 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002284 break;
2285
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002286 case IDENTIFIER_OFFSET: {
Sebastian Redl2da08f92010-07-19 22:28:42 +00002287 if (F.LocalNumIdentifiers != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002288 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002289 return true;
Douglas Gregorafaf3082009-04-11 00:14:32 +00002290 }
Sebastian Redl2da08f92010-07-19 22:28:42 +00002291 F.IdentifierOffsets = (const uint32_t *)BlobStart;
2292 F.LocalNumIdentifiers = Record[0];
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002293 unsigned LocalBaseIdentifierID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00002294 F.BaseIdentifierID = getTotalNumIdentifiers();
Douglas Gregor67268d02011-07-20 00:59:32 +00002295
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002296 if (F.LocalNumIdentifiers > 0) {
2297 // Introduce the global -> local mapping for identifiers within this
2298 // module.
2299 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
2300 &F));
2301
2302 // Introduce the local -> global mapping for identifiers within this
2303 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00002304 F.IdentifierRemap.insertOrReplace(
2305 std::make_pair(LocalBaseIdentifierID,
2306 F.BaseIdentifierID - LocalBaseIdentifierID));
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002307
2308 IdentifiersLoaded.resize(IdentifiersLoaded.size()
2309 + F.LocalNumIdentifiers);
2310 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002311 break;
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002312 }
Douglas Gregora8235d62012-10-09 23:05:51 +00002313
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002314 case EXTERNAL_DEFINITIONS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002315 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2316 ExternalDefinitions.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregorfdd01722009-04-14 00:24:19 +00002317 break;
Douglas Gregor3e1af842009-04-17 22:13:46 +00002318
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002319 case SPECIAL_TYPES:
Douglas Gregor393f2492011-07-22 00:38:23 +00002320 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2321 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
Douglas Gregorad1de002009-04-18 05:55:16 +00002322 break;
2323
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002324 case STATISTICS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002325 TotalNumStatements += Record[0];
2326 TotalNumMacros += Record[1];
2327 TotalLexicalDeclContexts += Record[2];
2328 TotalVisibleDeclContexts += Record[3];
Douglas Gregor3e1af842009-04-17 22:13:46 +00002329 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002330
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002331 case UNUSED_FILESCOPED_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002332 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2333 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002334 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002335
Sean Huntebcbe1d2011-05-04 23:29:54 +00002336 case DELEGATING_CTORS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002337 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2338 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
Sean Huntebcbe1d2011-05-04 23:29:54 +00002339 break;
2340
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002341 case WEAK_UNDECLARED_IDENTIFIERS:
Douglas Gregor31e37b22011-07-28 18:09:57 +00002342 if (Record.size() % 4 != 0) {
2343 Error("invalid weak identifiers record");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002344 return true;
Douglas Gregor31e37b22011-07-28 18:09:57 +00002345 }
2346
2347 // FIXME: Ignore weak undeclared identifiers from non-original PCH
2348 // files. This isn't the way to do it :)
2349 WeakUndeclaredIdentifiers.clear();
2350
2351 // Translate the weak, undeclared identifiers into global IDs.
2352 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
2353 WeakUndeclaredIdentifiers.push_back(
2354 getGlobalIdentifierID(F, Record[I++]));
2355 WeakUndeclaredIdentifiers.push_back(
2356 getGlobalIdentifierID(F, Record[I++]));
2357 WeakUndeclaredIdentifiers.push_back(
2358 ReadSourceLocation(F, Record, I).getRawEncoding());
2359 WeakUndeclaredIdentifiers.push_back(Record[I++]);
2360 }
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002361 break;
2362
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002363 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002364 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2365 LocallyScopedExternalDecls.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregor14c22f22009-04-22 22:18:58 +00002366 break;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002367
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002368 case SELECTOR_OFFSETS: {
Sebastian Redl059612d2010-08-03 21:58:15 +00002369 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redl725cd962010-08-04 20:40:17 +00002370 F.LocalNumSelectors = Record[0];
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002371 unsigned LocalBaseSelectorID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00002372 F.BaseSelectorID = getTotalNumSelectors();
Douglas Gregor96958cb2011-07-20 01:10:58 +00002373
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002374 if (F.LocalNumSelectors > 0) {
2375 // Introduce the global -> local mapping for selectors within this
2376 // module.
2377 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
2378
2379 // Introduce the local -> global mapping for selectors within this
2380 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00002381 F.SelectorRemap.insertOrReplace(
2382 std::make_pair(LocalBaseSelectorID,
2383 F.BaseSelectorID - LocalBaseSelectorID));
Douglas Gregor83941df2009-04-25 17:48:32 +00002384
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002385 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
2386 }
2387 break;
2388 }
2389
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002390 case METHOD_POOL:
Sebastian Redl725cd962010-08-04 20:40:17 +00002391 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor83941df2009-04-25 17:48:32 +00002392 if (Record[0])
Sebastian Redl725cd962010-08-04 20:40:17 +00002393 F.SelectorLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002394 = ASTSelectorLookupTable::Create(
Sebastian Redl725cd962010-08-04 20:40:17 +00002395 F.SelectorLookupTableData + Record[0],
2396 F.SelectorLookupTableData,
Douglas Gregor409448c2011-07-21 22:35:25 +00002397 ASTSelectorLookupTrait(*this, F));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002398 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002399 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002400
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002401 case REFERENCED_SELECTOR_POOL:
Douglas Gregor8451ec72011-07-28 14:41:43 +00002402 if (!Record.empty()) {
2403 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
2404 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
2405 Record[Idx++]));
2406 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
2407 getRawEncoding());
2408 }
2409 }
Fariborz Jahanian32019832010-07-23 19:11:11 +00002410 break;
2411
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002412 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002413 if (!Record.empty() && Listener)
Argyrios Kyrtzidis62288ed2012-10-10 02:12:47 +00002414 Listener->ReadCounter(F, Record[0]);
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002415 break;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002416
2417 case FILE_SORTED_DECLS:
2418 F.FileSortedDecls = (const DeclID *)BlobStart;
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002419 F.NumFileSortedDecls = Record[0];
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002420 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002421
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002422 case SOURCE_LOCATION_OFFSETS: {
2423 F.SLocEntryOffsets = (const uint32_t *)BlobStart;
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002424 F.LocalNumSLocEntries = Record[0];
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002425 unsigned SLocSpaceSize = Record[1];
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002426 llvm::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002427 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
2428 SLocSpaceSize);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002429 // Make our entry in the range map. BaseID is negative and growing, so
2430 // we invert it. Because we invert it, though, we need the other end of
2431 // the range.
2432 unsigned RangeStart =
2433 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
2434 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
2435 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
2436
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002437 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
2438 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
2439 GlobalSLocOffsetMap.insert(
2440 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
2441 - SLocSpaceSize,&F));
2442
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002443 // Initialize the remapping table.
2444 // Invalid stays invalid.
2445 F.SLocRemap.insert(std::make_pair(0U, 0));
2446 // This module. Base was 2 when being compiled.
2447 F.SLocRemap.insert(std::make_pair(2U,
2448 static_cast<int>(F.SLocEntryBaseOffset - 2)));
Douglas Gregor0cdd7982011-07-21 18:46:38 +00002449
2450 TotalNumSLocEntries += F.LocalNumSLocEntries;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002451 break;
2452 }
2453
Douglas Gregor5d51a1d2011-08-01 16:01:55 +00002454 case MODULE_OFFSET_MAP: {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002455 // Additional remapping information.
2456 const unsigned char *Data = (const unsigned char*)BlobStart;
2457 const unsigned char *DataEnd = Data + BlobLen;
Douglas Gregorf33740e2011-08-02 10:56:51 +00002458
2459 // Continuous range maps we may be updating in our module.
2460 ContinuousRangeMap<uint32_t, int, 2>::Builder SLocRemap(F.SLocRemap);
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002461 ContinuousRangeMap<uint32_t, int, 2>::Builder
2462 IdentifierRemap(F.IdentifierRemap);
Douglas Gregora8235d62012-10-09 23:05:51 +00002463 ContinuousRangeMap<uint32_t, int, 2>::Builder
2464 MacroRemap(F.MacroRemap);
2465 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002466 PreprocessedEntityRemap(F.PreprocessedEntityRemap);
2467 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregor26ced122011-12-01 00:59:36 +00002468 SubmoduleRemap(F.SubmoduleRemap);
2469 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002470 SelectorRemap(F.SelectorRemap);
Douglas Gregor496c7092011-08-03 15:48:04 +00002471 ContinuousRangeMap<uint32_t, int, 2>::Builder DeclRemap(F.DeclRemap);
Douglas Gregora119da02011-08-02 16:26:37 +00002472 ContinuousRangeMap<uint32_t, int, 2>::Builder TypeRemap(F.TypeRemap);
2473
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002474 while(Data < DataEnd) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002475 uint16_t Len = io::ReadUnalignedLE16(Data);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002476 StringRef Name = StringRef((const char*)Data, Len);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002477 Data += Len;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002478 ModuleFile *OM = ModuleMgr.lookup(Name);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002479 if (!OM) {
2480 Error("SourceLocation remap refers to unknown module");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002481 return true;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002482 }
Douglas Gregorf33740e2011-08-02 10:56:51 +00002483
2484 uint32_t SLocOffset = io::ReadUnalignedLE32(Data);
2485 uint32_t IdentifierIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregora8235d62012-10-09 23:05:51 +00002486 uint32_t MacroIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002487 uint32_t PreprocessedEntityIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregor26ced122011-12-01 00:59:36 +00002488 uint32_t SubmoduleIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002489 uint32_t SelectorIDOffset = io::ReadUnalignedLE32(Data);
2490 uint32_t DeclIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregora119da02011-08-02 16:26:37 +00002491 uint32_t TypeIndexOffset = io::ReadUnalignedLE32(Data);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002492
2493 // Source location offset is mapped to OM->SLocEntryBaseOffset.
2494 SLocRemap.insert(std::make_pair(SLocOffset,
2495 static_cast<int>(OM->SLocEntryBaseOffset - SLocOffset)));
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002496 IdentifierRemap.insert(
2497 std::make_pair(IdentifierIDOffset,
2498 OM->BaseIdentifierID - IdentifierIDOffset));
Douglas Gregora8235d62012-10-09 23:05:51 +00002499 MacroRemap.insert(std::make_pair(MacroIDOffset,
2500 OM->BaseMacroID - MacroIDOffset));
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002501 PreprocessedEntityRemap.insert(
2502 std::make_pair(PreprocessedEntityIDOffset,
2503 OM->BasePreprocessedEntityID - PreprocessedEntityIDOffset));
Douglas Gregor26ced122011-12-01 00:59:36 +00002504 SubmoduleRemap.insert(std::make_pair(SubmoduleIDOffset,
2505 OM->BaseSubmoduleID - SubmoduleIDOffset));
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002506 SelectorRemap.insert(std::make_pair(SelectorIDOffset,
2507 OM->BaseSelectorID - SelectorIDOffset));
Douglas Gregor496c7092011-08-03 15:48:04 +00002508 DeclRemap.insert(std::make_pair(DeclIDOffset,
2509 OM->BaseDeclID - DeclIDOffset));
2510
Douglas Gregora119da02011-08-02 16:26:37 +00002511 TypeRemap.insert(std::make_pair(TypeIndexOffset,
Douglas Gregore3605012011-08-02 18:32:54 +00002512 OM->BaseTypeIndex - TypeIndexOffset));
Douglas Gregora1be2782011-12-17 23:38:30 +00002513
2514 // Global -> local mappings.
2515 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002516 }
2517 break;
2518 }
2519
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002520 case SOURCE_MANAGER_LINE_TABLE:
2521 if (ParseLineTable(F, Record))
Douglas Gregor4825fd72012-10-22 22:50:17 +00002522 return true;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002523 break;
2524
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002525 case SOURCE_LOCATION_PRELOADS: {
2526 // Need to transform from the local view (1-based IDs) to the global view,
2527 // which is based off F.SLocEntryBaseID.
Douglas Gregorf249bf32011-08-25 21:09:44 +00002528 if (!F.PreloadSLocEntries.empty()) {
2529 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002530 return true;
Douglas Gregorf249bf32011-08-25 21:09:44 +00002531 }
2532
2533 F.PreloadSLocEntries.swap(Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002534 break;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002535 }
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002536
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002537 case STAT_CACHE: {
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00002538 if (!DisableStatCache) {
2539 ASTStatCache *MyStatCache =
2540 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
2541 (const unsigned char *)BlobStart,
2542 NumStatHits, NumStatMisses);
2543 FileMgr.addStatCache(MyStatCache);
2544 F.StatCache = MyStatCache;
2545 }
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002546 break;
Douglas Gregor52e71082009-10-16 18:18:30 +00002547 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002548
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002549 case EXT_VECTOR_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002550 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2551 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregorb81c1702009-04-27 20:06:05 +00002552 break;
2553
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002554 case VTABLE_USES:
Douglas Gregordfe65432011-07-28 19:11:31 +00002555 if (Record.size() % 3 != 0) {
2556 Error("Invalid VTABLE_USES record");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002557 return true;
Douglas Gregordfe65432011-07-28 19:11:31 +00002558 }
2559
Sebastian Redl40566802010-08-05 18:21:25 +00002560 // Later tables overwrite earlier ones.
Douglas Gregordfe65432011-07-28 19:11:31 +00002561 // FIXME: Modules will have some trouble with this. This is clearly not
2562 // the right way to do this.
Douglas Gregor409448c2011-07-21 22:35:25 +00002563 VTableUses.clear();
Douglas Gregordfe65432011-07-28 19:11:31 +00002564
2565 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
2566 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
2567 VTableUses.push_back(
2568 ReadSourceLocation(F, Record, Idx).getRawEncoding());
2569 VTableUses.push_back(Record[Idx++]);
2570 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002571 break;
2572
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002573 case DYNAMIC_CLASSES:
Douglas Gregor409448c2011-07-21 22:35:25 +00002574 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2575 DynamicClasses.push_back(getGlobalDeclID(F, Record[I]));
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002576 break;
2577
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002578 case PENDING_IMPLICIT_INSTANTIATIONS:
Douglas Gregorf2abb522011-07-28 19:26:52 +00002579 if (PendingInstantiations.size() % 2 != 0) {
Axel Naumann39d26c32012-10-02 09:09:43 +00002580 Error("Invalid existing PendingInstantiations");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002581 return true;
Axel Naumann39d26c32012-10-02 09:09:43 +00002582 }
2583
2584 if (Record.size() % 2 != 0) {
Douglas Gregorf2abb522011-07-28 19:26:52 +00002585 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002586 return true;
Douglas Gregorf2abb522011-07-28 19:26:52 +00002587 }
Axel Naumann39d26c32012-10-02 09:09:43 +00002588
Douglas Gregorf2abb522011-07-28 19:26:52 +00002589 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2590 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
2591 PendingInstantiations.push_back(
2592 ReadSourceLocation(F, Record, I).getRawEncoding());
2593 }
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002594 break;
2595
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002596 case SEMA_DECL_REFS:
Sebastian Redl40566802010-08-05 18:21:25 +00002597 // Later tables overwrite earlier ones.
Douglas Gregor409448c2011-07-21 22:35:25 +00002598 // FIXME: Modules will have some trouble with this.
2599 SemaDeclRefs.clear();
2600 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2601 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002602 break;
2603
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002604 case PPD_ENTITIES_OFFSETS: {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002605 F.PreprocessedEntityOffsets = (const PPEntityOffset *)BlobStart;
2606 assert(BlobLen % sizeof(PPEntityOffset) == 0);
2607 F.NumPreprocessedEntities = BlobLen / sizeof(PPEntityOffset);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002608
2609 unsigned LocalBasePreprocessedEntityID = Record[0];
Douglas Gregorfb2d9e02011-08-04 16:36:56 +00002610
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002611 unsigned StartingID;
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002612 if (!PP.getPreprocessingRecord())
Argyrios Kyrtzidisc6c54522012-03-05 05:48:17 +00002613 PP.createPreprocessingRecord(/*RecordConditionalDirectives=*/false);
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002614 if (!PP.getPreprocessingRecord()->getExternalSource())
2615 PP.getPreprocessingRecord()->SetExternalSource(*this);
2616 StartingID
2617 = PP.getPreprocessingRecord()
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002618 ->allocateLoadedEntities(F.NumPreprocessedEntities);
Douglas Gregor9827a802011-07-29 00:56:45 +00002619 F.BasePreprocessedEntityID = StartingID;
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002620
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002621 if (F.NumPreprocessedEntities > 0) {
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002622 // Introduce the global -> local mapping for preprocessed entities in
2623 // this module.
2624 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
2625
2626 // Introduce the local -> global mapping for preprocessed entities in
2627 // this module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00002628 F.PreprocessedEntityRemap.insertOrReplace(
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002629 std::make_pair(LocalBasePreprocessedEntityID,
2630 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
2631 }
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002632
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002633 break;
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002634 }
2635
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002636 case DECL_UPDATE_OFFSETS: {
2637 if (Record.size() % 2 != 0) {
2638 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002639 return true;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002640 }
2641 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Douglas Gregor496c7092011-08-03 15:48:04 +00002642 DeclUpdateOffsets[getGlobalDeclID(F, Record[I])]
2643 .push_back(std::make_pair(&F, Record[I+1]));
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002644 break;
2645 }
2646
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002647 case DECL_REPLACEMENTS: {
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00002648 if (Record.size() % 3 != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002649 Error("invalid DECL_REPLACEMENTS block in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002650 return true;
Sebastian Redl0b17c612010-08-13 00:28:03 +00002651 }
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00002652 for (unsigned I = 0, N = Record.size(); I != N; I += 3)
Douglas Gregor496c7092011-08-03 15:48:04 +00002653 ReplacedDecls[getGlobalDeclID(F, Record[I])]
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00002654 = ReplacedDeclInfo(&F, Record[I+1], Record[I+2]);
Sebastian Redl0b17c612010-08-13 00:28:03 +00002655 break;
2656 }
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00002657
Douglas Gregorcff9f262012-01-27 01:47:08 +00002658 case OBJC_CATEGORIES_MAP: {
2659 if (F.LocalNumObjCCategoriesInMap != 0) {
2660 Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002661 return true;
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00002662 }
Douglas Gregorcff9f262012-01-27 01:47:08 +00002663
2664 F.LocalNumObjCCategoriesInMap = Record[0];
2665 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)BlobStart;
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00002666 break;
2667 }
Douglas Gregor7c789c12010-10-29 22:39:52 +00002668
Douglas Gregorcff9f262012-01-27 01:47:08 +00002669 case OBJC_CATEGORIES:
2670 F.ObjCCategories.swap(Record);
2671 break;
2672
Douglas Gregor7c789c12010-10-29 22:39:52 +00002673 case CXX_BASE_SPECIFIER_OFFSETS: {
2674 if (F.LocalNumCXXBaseSpecifiers != 0) {
2675 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002676 return true;
Douglas Gregor7c789c12010-10-29 22:39:52 +00002677 }
2678
2679 F.LocalNumCXXBaseSpecifiers = Record[0];
2680 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart;
Jonathan D. Turner1da90142011-07-21 21:15:19 +00002681 NumCXXBaseSpecifiersLoaded += F.LocalNumCXXBaseSpecifiers;
Douglas Gregor7c789c12010-10-29 22:39:52 +00002682 break;
2683 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002684
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002685 case DIAG_PRAGMA_MAPPINGS:
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002686 if (Record.size() % 2 != 0) {
2687 Error("invalid DIAG_USER_MAPPINGS block in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002688 return true;
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002689 }
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002690
2691 if (F.PragmaDiagMappings.empty())
2692 F.PragmaDiagMappings.swap(Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002693 else
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002694 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
2695 Record.begin(), Record.end());
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002696 break;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002697
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002698 case CUDA_SPECIAL_DECL_REFS:
2699 // Later tables overwrite earlier ones.
Douglas Gregor409448c2011-07-21 22:35:25 +00002700 // FIXME: Modules will have trouble with this.
2701 CUDASpecialDeclRefs.clear();
2702 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2703 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002704 break;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002705
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002706 case HEADER_SEARCH_TABLE: {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002707 F.HeaderFileInfoTableData = BlobStart;
2708 F.LocalNumHeaderFileInfos = Record[1];
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002709 F.HeaderFileFrameworkStrings = BlobStart + Record[2];
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002710 if (Record[0]) {
2711 F.HeaderFileInfoTable
2712 = HeaderFileInfoLookupTable::Create(
2713 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002714 (const unsigned char *)F.HeaderFileInfoTableData,
Douglas Gregor95eab172011-07-28 20:55:49 +00002715 HeaderFileInfoTrait(*this, F,
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002716 &PP.getHeaderSearchInfo(),
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002717 BlobStart + Record[2]));
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002718
2719 PP.getHeaderSearchInfo().SetExternalSource(this);
2720 if (!PP.getHeaderSearchInfo().getExternalLookup())
2721 PP.getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002722 }
2723 break;
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002724 }
2725
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002726 case FP_PRAGMA_OPTIONS:
2727 // Later tables overwrite earlier ones.
2728 FPPragmaOptions.swap(Record);
2729 break;
2730
2731 case OPENCL_EXTENSIONS:
2732 // Later tables overwrite earlier ones.
2733 OpenCLExtensions.swap(Record);
2734 break;
Sean Huntebcbe1d2011-05-04 23:29:54 +00002735
2736 case TENTATIVE_DEFINITIONS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002737 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2738 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
Sean Huntebcbe1d2011-05-04 23:29:54 +00002739 break;
Douglas Gregord8bba9c2011-06-28 16:20:02 +00002740
2741 case KNOWN_NAMESPACES:
Douglas Gregor409448c2011-07-21 22:35:25 +00002742 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2743 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregord8bba9c2011-06-28 16:20:02 +00002744 break;
Douglas Gregorf6137e42011-12-03 00:59:55 +00002745
2746 case IMPORTED_MODULES: {
2747 if (F.Kind != MK_Module) {
2748 // If we aren't loading a module (which has its own exports), make
2749 // all of the imported modules visible.
2750 // FIXME: Deal with macros-only imports.
2751 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2752 if (unsigned GlobalID = getGlobalSubmoduleID(F, Record[I]))
2753 ImportedModules.push_back(GlobalID);
2754 }
2755 }
2756 break;
Douglas Gregora1be2782011-12-17 23:38:30 +00002757 }
Douglas Gregor2171bf12012-01-15 16:58:34 +00002758
Douglas Gregora1be2782011-12-17 23:38:30 +00002759 case LOCAL_REDECLARATIONS: {
Douglas Gregor2171bf12012-01-15 16:58:34 +00002760 F.RedeclarationChains.swap(Record);
2761 break;
2762 }
2763
2764 case LOCAL_REDECLARATIONS_MAP: {
2765 if (F.LocalNumRedeclarationsInMap != 0) {
2766 Error("duplicate LOCAL_REDECLARATIONS_MAP record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002767 return true;
Douglas Gregora1be2782011-12-17 23:38:30 +00002768 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00002769
Douglas Gregor2171bf12012-01-15 16:58:34 +00002770 F.LocalNumRedeclarationsInMap = Record[0];
2771 F.RedeclarationsMap = (const LocalRedeclarationsInfo *)BlobStart;
Douglas Gregora1be2782011-12-17 23:38:30 +00002772 break;
Douglas Gregorf6137e42011-12-03 00:59:55 +00002773 }
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00002774
2775 case MERGED_DECLARATIONS: {
2776 for (unsigned Idx = 0; Idx < Record.size(); /* increment in loop */) {
2777 GlobalDeclID CanonID = getGlobalDeclID(F, Record[Idx++]);
2778 SmallVectorImpl<GlobalDeclID> &Decls = StoredMergedDecls[CanonID];
2779 for (unsigned N = Record[Idx++]; N > 0; --N)
2780 Decls.push_back(getGlobalDeclID(F, Record[Idx++]));
2781 }
2782 break;
2783 }
Douglas Gregora8235d62012-10-09 23:05:51 +00002784
2785 case MACRO_OFFSET: {
2786 if (F.LocalNumMacros != 0) {
2787 Error("duplicate MACRO_OFFSET record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002788 return true;
Douglas Gregora8235d62012-10-09 23:05:51 +00002789 }
2790 F.MacroOffsets = (const uint32_t *)BlobStart;
2791 F.LocalNumMacros = Record[0];
2792 unsigned LocalBaseMacroID = Record[1];
2793 F.BaseMacroID = getTotalNumMacros();
2794
2795 if (F.LocalNumMacros > 0) {
2796 // Introduce the global -> local mapping for macros within this module.
2797 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
2798
2799 // Introduce the local -> global mapping for macros within this module.
2800 F.MacroRemap.insertOrReplace(
2801 std::make_pair(LocalBaseMacroID,
2802 F.BaseMacroID - LocalBaseMacroID));
2803
2804 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
2805 }
2806 break;
2807 }
2808
2809 case MACRO_UPDATES: {
2810 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2811 MacroID ID = getGlobalMacroID(F, Record[I++]);
2812 if (I == N)
2813 break;
2814
Douglas Gregor54c8a402012-10-12 00:16:50 +00002815 SourceLocation UndefLoc = ReadSourceLocation(F, Record, I);
2816 SubmoduleID SubmoduleID = getGlobalSubmoduleID(F, Record[I++]);;
2817 MacroUpdate Update;
2818 Update.UndefLoc = UndefLoc;
2819 MacroUpdates[ID].push_back(std::make_pair(SubmoduleID, Update));
Douglas Gregora8235d62012-10-09 23:05:51 +00002820 }
2821 break;
2822 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002823 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002824 }
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002825 Error("premature end of bitstream in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002826 return true;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002827}
2828
Douglas Gregorecc2c092011-12-01 22:20:10 +00002829void ASTReader::makeNamesVisible(const HiddenNames &Names) {
Douglas Gregor13292642011-12-02 15:45:10 +00002830 for (unsigned I = 0, N = Names.size(); I != N; ++I) {
Douglas Gregor54c8a402012-10-12 00:16:50 +00002831 switch (Names[I].getKind()) {
2832 case HiddenName::Declaration:
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00002833 Names[I].getDecl()->Hidden = false;
Douglas Gregor54c8a402012-10-12 00:16:50 +00002834 break;
2835
2836 case HiddenName::MacroVisibility: {
2837 std::pair<IdentifierInfo *, MacroInfo *> Macro = Names[I].getMacro();
2838 Macro.second->setHidden(!Macro.second->isPublic());
2839 if (Macro.second->isDefined()) {
2840 PP.makeLoadedMacroInfoVisible(Macro.first, Macro.second);
2841 }
2842 break;
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00002843 }
2844
Douglas Gregor54c8a402012-10-12 00:16:50 +00002845 case HiddenName::MacroUndef: {
2846 std::pair<IdentifierInfo *, MacroInfo *> Macro = Names[I].getMacro();
2847 if (Macro.second->isDefined()) {
2848 Macro.second->setUndefLoc(Names[I].getMacroUndefLoc());
2849 if (PPMutationListener *Listener = PP.getPPMutationListener())
2850 Listener->UndefinedMacro(Macro.second);
2851 PP.makeLoadedMacroInfoVisible(Macro.first, Macro.second);
2852 }
2853 break;
2854 }
Douglas Gregor1d4c1132011-12-20 22:06:13 +00002855 }
Douglas Gregor13292642011-12-02 15:45:10 +00002856 }
Douglas Gregorecc2c092011-12-01 22:20:10 +00002857}
2858
Douglas Gregor5e356932011-12-01 17:11:21 +00002859void ASTReader::makeModuleVisible(Module *Mod,
2860 Module::NameVisibilityKind NameVisibility) {
2861 llvm::SmallPtrSet<Module *, 4> Visited;
2862 llvm::SmallVector<Module *, 4> Stack;
2863 Stack.push_back(Mod);
2864 while (!Stack.empty()) {
2865 Mod = Stack.back();
2866 Stack.pop_back();
2867
2868 if (NameVisibility <= Mod->NameVisibility) {
2869 // This module already has this level of visibility (or greater), so
2870 // there is nothing more to do.
2871 continue;
2872 }
2873
Douglas Gregor51f564f2011-12-31 04:05:44 +00002874 if (!Mod->isAvailable()) {
2875 // Modules that aren't available cannot be made visible.
2876 continue;
2877 }
2878
Douglas Gregor5e356932011-12-01 17:11:21 +00002879 // Update the module's name visibility.
2880 Mod->NameVisibility = NameVisibility;
2881
Douglas Gregorecc2c092011-12-01 22:20:10 +00002882 // If we've already deserialized any names from this module,
Douglas Gregor5e356932011-12-01 17:11:21 +00002883 // mark them as visible.
Douglas Gregorecc2c092011-12-01 22:20:10 +00002884 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
2885 if (Hidden != HiddenNamesMap.end()) {
2886 makeNamesVisible(Hidden->second);
2887 HiddenNamesMap.erase(Hidden);
2888 }
Douglas Gregor5e356932011-12-01 17:11:21 +00002889
2890 // Push any non-explicit submodules onto the stack to be marked as
2891 // visible.
Douglas Gregorb7a78192012-01-04 23:32:19 +00002892 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2893 SubEnd = Mod->submodule_end();
Douglas Gregor5e356932011-12-01 17:11:21 +00002894 Sub != SubEnd; ++Sub) {
Douglas Gregorb7a78192012-01-04 23:32:19 +00002895 if (!(*Sub)->IsExplicit && Visited.insert(*Sub))
2896 Stack.push_back(*Sub);
Douglas Gregor5e356932011-12-01 17:11:21 +00002897 }
Douglas Gregor07165b92011-12-02 19:11:09 +00002898
2899 // Push any exported modules onto the stack to be marked as visible.
Douglas Gregor0adaa882011-12-05 17:28:06 +00002900 bool AnyWildcard = false;
2901 bool UnrestrictedWildcard = false;
2902 llvm::SmallVector<Module *, 4> WildcardRestrictions;
Douglas Gregor07165b92011-12-02 19:11:09 +00002903 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
2904 Module *Exported = Mod->Exports[I].getPointer();
Douglas Gregor0adaa882011-12-05 17:28:06 +00002905 if (!Mod->Exports[I].getInt()) {
2906 // Export a named module directly; no wildcards involved.
2907 if (Visited.insert(Exported))
Douglas Gregor07165b92011-12-02 19:11:09 +00002908 Stack.push_back(Exported);
Douglas Gregor0adaa882011-12-05 17:28:06 +00002909
2910 continue;
Douglas Gregor07165b92011-12-02 19:11:09 +00002911 }
Douglas Gregor0adaa882011-12-05 17:28:06 +00002912
2913 // Wildcard export: export all of the imported modules that match
2914 // the given pattern.
2915 AnyWildcard = true;
2916 if (UnrestrictedWildcard)
2917 continue;
2918
2919 if (Module *Restriction = Mod->Exports[I].getPointer())
2920 WildcardRestrictions.push_back(Restriction);
2921 else {
2922 WildcardRestrictions.clear();
2923 UnrestrictedWildcard = true;
2924 }
2925 }
2926
2927 // If there were any wildcards, push any imported modules that were
2928 // re-exported by the wildcard restriction.
2929 if (!AnyWildcard)
2930 continue;
2931
2932 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
2933 Module *Imported = Mod->Imports[I];
Benjamin Kramerd48bcb22012-08-22 15:37:55 +00002934 if (!Visited.insert(Imported))
Douglas Gregor0adaa882011-12-05 17:28:06 +00002935 continue;
2936
2937 bool Acceptable = UnrestrictedWildcard;
2938 if (!Acceptable) {
2939 // Check whether this module meets one of the restrictions.
2940 for (unsigned R = 0, NR = WildcardRestrictions.size(); R != NR; ++R) {
2941 Module *Restriction = WildcardRestrictions[R];
2942 if (Imported == Restriction || Imported->isSubModuleOf(Restriction)) {
2943 Acceptable = true;
2944 break;
2945 }
2946 }
2947 }
2948
2949 if (!Acceptable)
2950 continue;
2951
Douglas Gregor0adaa882011-12-05 17:28:06 +00002952 Stack.push_back(Imported);
Douglas Gregor07165b92011-12-02 19:11:09 +00002953 }
Douglas Gregor5e356932011-12-01 17:11:21 +00002954 }
2955}
2956
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002957ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
Douglas Gregor38295be2012-10-22 23:51:00 +00002958 ModuleKind Type,
2959 unsigned ClientLoadCapabilities) {
Douglas Gregor057df202012-01-18 20:56:22 +00002960 // Bump the generation number.
Douglas Gregorcff9f262012-01-27 01:47:08 +00002961 unsigned PreviousGeneration = CurrentGeneration++;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002962
2963 // Load the core of the AST files.
2964 llvm::SmallVector<ModuleFile *, 4> Loaded;
Douglas Gregor38295be2012-10-22 23:51:00 +00002965 switch(ReadASTCore(FileName, Type, /*ImportedBy=*/0, Loaded,
2966 ClientLoadCapabilities)) {
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002967 case Failure: return Failure;
Douglas Gregor4825fd72012-10-22 22:50:17 +00002968 case OutOfDate: return OutOfDate;
2969 case VersionMismatch: return VersionMismatch;
2970 case ConfigurationMismatch: return ConfigurationMismatch;
2971 case HadErrors: return HadErrors;
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002972 case Success: break;
2973 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002974
2975 // Here comes stuff that we only do once the entire chain is loaded.
Douglas Gregor057df202012-01-18 20:56:22 +00002976
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002977 // Load the AST blocks of all of the modules that we loaded.
2978 for (llvm::SmallVectorImpl<ModuleFile *>::iterator M = Loaded.begin(),
2979 MEnd = Loaded.end();
2980 M != MEnd; ++M) {
2981 ModuleFile &F = **M;
2982
2983 // Read the AST block.
Douglas Gregor4825fd72012-10-22 22:50:17 +00002984 if (ReadASTBlock(F))
2985 return Failure;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002986
2987 // Once read, set the ModuleFile bit base offset and update the size in
2988 // bits of all files we've seen.
2989 F.GlobalBitOffset = TotalModulesSizeInBits;
2990 TotalModulesSizeInBits += F.SizeInBits;
2991 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
2992
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002993 // Preload SLocEntries.
2994 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
2995 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
Douglas Gregor8b53d142012-10-22 22:53:10 +00002996 // Load it through the SourceManager and don't call ReadSLocEntry()
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002997 // directly because the entry may have already been loaded in which case
Douglas Gregor8b53d142012-10-22 22:53:10 +00002998 // calling ReadSLocEntry() directly would trigger an assertion in
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002999 // SourceManager.
3000 SourceMgr.getLoadedSLocEntryByID(Index);
3001 }
3002 }
3003
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00003004 // Check the predefines buffers.
Douglas Gregor38295be2012-10-22 23:51:00 +00003005 bool ConfigComplain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
Douglas Gregor6236a292011-12-02 21:56:05 +00003006 if (!DisableValidation && Type == MK_PCH &&
Argyrios Kyrtzidis26d43cd2011-09-12 18:09:38 +00003007 // FIXME: CheckPredefinesBuffers also sets the SuggestedPredefines;
3008 // if DisableValidation is true, defines that were set on command-line
3009 // but not in the PCH file will not be added to SuggestedPredefines.
Douglas Gregor38295be2012-10-22 23:51:00 +00003010 CheckPredefinesBuffers(ConfigComplain))
Douglas Gregor4825fd72012-10-22 22:50:17 +00003011 return ConfigurationMismatch;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00003012
Douglas Gregoreee242f2011-10-27 09:33:13 +00003013 // Mark all of the identifiers in the identifier table as being out of date,
3014 // so that various accessors know to check the loaded modules when the
3015 // identifier is used.
Douglas Gregor712f2fc2011-09-09 22:02:16 +00003016 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
3017 IdEnd = PP.getIdentifierTable().end();
3018 Id != IdEnd; ++Id)
Douglas Gregoreee242f2011-10-27 09:33:13 +00003019 Id->second->setOutOfDate(true);
Douglas Gregor057df202012-01-18 20:56:22 +00003020
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003021 // Resolve any unresolved module exports.
Douglas Gregor55988682011-12-05 16:33:54 +00003022 for (unsigned I = 0, N = UnresolvedModuleImportExports.size(); I != N; ++I) {
3023 UnresolvedModuleImportExport &Unresolved = UnresolvedModuleImportExports[I];
3024 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
Douglas Gregor0adaa882011-12-05 17:28:06 +00003025 Module *ResolvedMod = getSubmodule(GlobalID);
3026
3027 if (Unresolved.IsImport) {
3028 if (ResolvedMod)
Douglas Gregor55988682011-12-05 16:33:54 +00003029 Unresolved.Mod->Imports.push_back(ResolvedMod);
Douglas Gregor0adaa882011-12-05 17:28:06 +00003030 continue;
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003031 }
Douglas Gregor0adaa882011-12-05 17:28:06 +00003032
3033 if (ResolvedMod || Unresolved.IsWildcard)
3034 Unresolved.Mod->Exports.push_back(
3035 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003036 }
Douglas Gregor55988682011-12-05 16:33:54 +00003037 UnresolvedModuleImportExports.clear();
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003038
Douglas Gregor35942772011-09-09 21:34:22 +00003039 InitializeContext();
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00003040
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003041 if (DeserializationListener)
3042 DeserializationListener->ReaderInitialized(this);
3043
Douglas Gregor11407b82012-10-18 21:18:25 +00003044 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
3045 if (!PrimaryModule.OriginalSourceFileID.isInvalid()) {
3046 PrimaryModule.OriginalSourceFileID
3047 = FileID::get(PrimaryModule.SLocEntryBaseID
3048 + PrimaryModule.OriginalSourceFileID.getOpaqueValue() - 1);
Argyrios Kyrtzidisb8c879a2012-01-05 21:36:25 +00003049
Douglas Gregor11407b82012-10-18 21:18:25 +00003050 // If this AST file is a precompiled preamble, then set the
3051 // preamble file ID of the source manager to the file source file
3052 // from which the preamble was built.
Argyrios Kyrtzidisb8c879a2012-01-05 21:36:25 +00003053 if (Type == MK_Preamble) {
Douglas Gregor11407b82012-10-18 21:18:25 +00003054 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
Argyrios Kyrtzidisb8c879a2012-01-05 21:36:25 +00003055 } else if (Type == MK_MainFile) {
Douglas Gregor11407b82012-10-18 21:18:25 +00003056 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00003057 }
Douglas Gregor414cb642010-11-30 05:23:00 +00003058 }
3059
Douglas Gregorcff9f262012-01-27 01:47:08 +00003060 // For any Objective-C class definitions we have already loaded, make sure
3061 // that we load any additional categories.
3062 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
3063 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
3064 ObjCClassesLoaded[I],
3065 PreviousGeneration);
3066 }
3067
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00003068 return Success;
3069}
3070
Douglas Gregor38295be2012-10-22 23:51:00 +00003071ASTReader::ASTReadResult
3072ASTReader::ReadASTCore(StringRef FileName,
3073 ModuleKind Type,
3074 ModuleFile *ImportedBy,
3075 llvm::SmallVectorImpl<ModuleFile *> &Loaded,
3076 unsigned ClientLoadCapabilities) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003077 ModuleFile *M;
Douglas Gregorfac4ece2011-08-19 02:29:29 +00003078 bool NewModule;
3079 std::string ErrorStr;
3080 llvm::tie(M, NewModule) = ModuleMgr.addModule(FileName, Type, ImportedBy,
Douglas Gregor057df202012-01-18 20:56:22 +00003081 CurrentGeneration, ErrorStr);
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00003082
Douglas Gregorfac4ece2011-08-19 02:29:29 +00003083 if (!M) {
3084 // We couldn't load the module.
3085 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
3086 + ErrorStr;
3087 Error(Msg);
3088 return Failure;
3089 }
3090
3091 if (!NewModule) {
3092 // We've already loaded this module.
3093 return Success;
3094 }
3095
3096 // FIXME: This seems rather a hack. Should CurrentDir be part of the
3097 // module?
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00003098 if (FileName != "-") {
3099 CurrentDir = llvm::sys::path::parent_path(FileName);
3100 if (CurrentDir.empty()) CurrentDir = ".";
3101 }
3102
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003103 ModuleFile &F = *M;
Sebastian Redl9137a522010-07-16 17:50:48 +00003104 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00003105 Stream.init(F.StreamFile);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00003106 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Douglas Gregor8f1231b2011-07-22 06:10:01 +00003107
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00003108 // Sniff for the signature.
3109 if (Stream.Read(8) != 'C' ||
3110 Stream.Read(8) != 'P' ||
3111 Stream.Read(8) != 'C' ||
3112 Stream.Read(8) != 'H') {
3113 Diag(diag::err_not_a_pch_file) << FileName;
3114 return Failure;
3115 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003116
Douglas Gregor2cf26342009-04-09 22:27:44 +00003117 while (!Stream.AtEndOfStream()) {
3118 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00003119
Douglas Gregore1d918e2009-04-10 23:10:45 +00003120 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003121 Error("invalid record at top-level of AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00003122 return Failure;
3123 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003124
3125 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregor668c1a42009-04-21 22:25:48 +00003126
Douglas Gregor7ae467f2012-10-18 18:27:37 +00003127 // We only know the control subblock ID.
Douglas Gregor2cf26342009-04-09 22:27:44 +00003128 switch (BlockID) {
3129 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00003130 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003131 Error("malformed BlockInfoBlock in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00003132 return Failure;
3133 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003134 break;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003135 case CONTROL_BLOCK_ID:
Douglas Gregor38295be2012-10-22 23:51:00 +00003136 switch (ReadControlBlock(F, Loaded, ClientLoadCapabilities)) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003137 case Success:
3138 break;
3139
Douglas Gregor4825fd72012-10-22 22:50:17 +00003140 case Failure: return Failure;
3141 case OutOfDate: return OutOfDate;
3142 case VersionMismatch: return VersionMismatch;
3143 case ConfigurationMismatch: return ConfigurationMismatch;
3144 case HadErrors: return HadErrors;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003145 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003146 break;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003147 case AST_BLOCK_ID:
3148 // Record that we've loaded this module.
3149 Loaded.push_back(M);
3150 return Success;
3151
Douglas Gregor2cf26342009-04-09 22:27:44 +00003152 default:
Douglas Gregore1d918e2009-04-10 23:10:45 +00003153 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003154 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00003155 return Failure;
3156 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003157 break;
3158 }
Mike Stump1eb44332009-09-09 15:08:12 +00003159 }
Douglas Gregor8f1231b2011-07-22 06:10:01 +00003160
Sebastian Redlcdf3b832010-07-16 20:41:52 +00003161 return Success;
3162}
3163
Douglas Gregor712f2fc2011-09-09 22:02:16 +00003164void ASTReader::InitializeContext() {
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003165 // If there's a listener, notify them that we "read" the translation unit.
3166 if (DeserializationListener)
Douglas Gregor35942772011-09-09 21:34:22 +00003167 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
3168 Context.getTranslationUnitDecl());
Douglas Gregor3747ee72010-10-01 01:18:02 +00003169
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003170 // Make sure we load the declaration update records for the translation unit,
3171 // if there are any.
Douglas Gregor35942772011-09-09 21:34:22 +00003172 loadDeclUpdateRecords(PREDEF_DECL_TRANSLATION_UNIT_ID,
3173 Context.getTranslationUnitDecl());
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003174
Douglas Gregor5f957282011-08-11 22:18:49 +00003175 // FIXME: Find a better way to deal with collisions between these
3176 // built-in types. Right now, we just ignore the problem.
3177
3178 // Load the special types.
Douglas Gregora6ea10e2012-01-17 18:09:05 +00003179 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
Douglas Gregor02a5e872011-09-10 00:30:18 +00003180 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
3181 if (!Context.CFConstantStringTypeDecl)
3182 Context.setCFConstantStringType(GetType(String));
3183 }
3184
3185 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
3186 QualType FileType = GetType(File);
3187 if (FileType.isNull()) {
3188 Error("FILE type is NULL");
3189 return;
3190 }
3191
3192 if (!Context.FILEDecl) {
3193 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
3194 Context.setFILEDecl(Typedef->getDecl());
3195 else {
3196 const TagType *Tag = FileType->getAs<TagType>();
3197 if (!Tag) {
3198 Error("Invalid FILE type in AST file");
3199 return;
3200 }
3201 Context.setFILEDecl(Tag->getDecl());
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00003202 }
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00003203 }
Douglas Gregorc29f77b2009-07-07 16:35:42 +00003204 }
Douglas Gregor5f957282011-08-11 22:18:49 +00003205
Douglas Gregor72cd7a02011-11-11 19:13:12 +00003206 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
Douglas Gregor02a5e872011-09-10 00:30:18 +00003207 QualType Jmp_bufType = GetType(Jmp_buf);
3208 if (Jmp_bufType.isNull()) {
3209 Error("jmp_buf type is NULL");
3210 return;
3211 }
3212
3213 if (!Context.jmp_bufDecl) {
3214 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
3215 Context.setjmp_bufDecl(Typedef->getDecl());
3216 else {
3217 const TagType *Tag = Jmp_bufType->getAs<TagType>();
3218 if (!Tag) {
3219 Error("Invalid jmp_buf type in AST file");
3220 return;
3221 }
3222 Context.setjmp_bufDecl(Tag->getDecl());
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00003223 }
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00003224 }
Mike Stump782fa302009-07-28 02:25:19 +00003225 }
Douglas Gregor02a5e872011-09-10 00:30:18 +00003226
Douglas Gregor72cd7a02011-11-11 19:13:12 +00003227 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
Douglas Gregor02a5e872011-09-10 00:30:18 +00003228 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
3229 if (Sigjmp_bufType.isNull()) {
3230 Error("sigjmp_buf type is NULL");
3231 return;
3232 }
3233
3234 if (!Context.sigjmp_bufDecl) {
3235 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
3236 Context.setsigjmp_bufDecl(Typedef->getDecl());
3237 else {
3238 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
3239 assert(Tag && "Invalid sigjmp_buf type in AST file");
3240 Context.setsigjmp_bufDecl(Tag->getDecl());
3241 }
3242 }
3243 }
3244
3245 if (unsigned ObjCIdRedef
3246 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
3247 if (Context.ObjCIdRedefinitionType.isNull())
3248 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
3249 }
3250
3251 if (unsigned ObjCClassRedef
3252 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
3253 if (Context.ObjCClassRedefinitionType.isNull())
3254 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
3255 }
3256
3257 if (unsigned ObjCSelRedef
3258 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
3259 if (Context.ObjCSelRedefinitionType.isNull())
3260 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
3261 }
Rafael Espindolae2d4f4e2011-11-13 21:51:09 +00003262
3263 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
3264 QualType Ucontext_tType = GetType(Ucontext_t);
3265 if (Ucontext_tType.isNull()) {
3266 Error("ucontext_t type is NULL");
3267 return;
3268 }
3269
3270 if (!Context.ucontext_tDecl) {
3271 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
3272 Context.setucontext_tDecl(Typedef->getDecl());
3273 else {
3274 const TagType *Tag = Ucontext_tType->getAs<TagType>();
3275 assert(Tag && "Invalid ucontext_t type in AST file");
3276 Context.setucontext_tDecl(Tag->getDecl());
3277 }
3278 }
3279 }
Douglas Gregor5f957282011-08-11 22:18:49 +00003280 }
3281
Douglas Gregor35942772011-09-09 21:34:22 +00003282 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003283
3284 // If there were any CUDA special declarations, deserialize them.
3285 if (!CUDASpecialDeclRefs.empty()) {
3286 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
Douglas Gregor35942772011-09-09 21:34:22 +00003287 Context.setcudaConfigureCallDecl(
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003288 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
3289 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00003290
3291 // Re-export any modules that were imported by a non-module AST file.
3292 for (unsigned I = 0, N = ImportedModules.size(); I != N; ++I) {
3293 if (Module *Imported = getSubmodule(ImportedModules[I]))
3294 makeModuleVisible(Imported, Module::AllVisible);
3295 }
3296 ImportedModules.clear();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003297}
3298
Douglas Gregorecc2c092011-12-01 22:20:10 +00003299void ASTReader::finalizeForWriting() {
3300 for (HiddenNamesMapType::iterator Hidden = HiddenNamesMap.begin(),
3301 HiddenEnd = HiddenNamesMap.end();
3302 Hidden != HiddenEnd; ++Hidden) {
3303 makeNamesVisible(Hidden->second);
3304 }
3305 HiddenNamesMap.clear();
3306}
3307
Douglas Gregorb64c1932009-05-12 01:31:05 +00003308/// \brief Retrieve the name of the original source file name
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003309/// directly from the AST file, without actually loading the AST
Douglas Gregorb64c1932009-05-12 01:31:05 +00003310/// file.
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003311std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +00003312 FileManager &FileMgr,
David Blaikied6471f72011-09-25 23:23:43 +00003313 DiagnosticsEngine &Diags) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003314 // Open the AST file.
Douglas Gregorb64c1932009-05-12 01:31:05 +00003315 std::string ErrStr;
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00003316 OwningPtr<llvm::MemoryBuffer> Buffer;
Chris Lattner39b49bc2010-11-23 08:35:12 +00003317 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
Douglas Gregorb64c1932009-05-12 01:31:05 +00003318 if (!Buffer) {
Kaelyn Uhrainda01f622012-06-20 00:36:03 +00003319 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ASTFileName << ErrStr;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003320 return std::string();
3321 }
3322
3323 // Initialize the stream
3324 llvm::BitstreamReader StreamFile;
3325 llvm::BitstreamCursor Stream;
Mike Stump1eb44332009-09-09 15:08:12 +00003326 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregorb64c1932009-05-12 01:31:05 +00003327 (const unsigned char *)Buffer->getBufferEnd());
3328 Stream.init(StreamFile);
3329
3330 // Sniff for the signature.
3331 if (Stream.Read(8) != 'C' ||
3332 Stream.Read(8) != 'P' ||
3333 Stream.Read(8) != 'C' ||
3334 Stream.Read(8) != 'H') {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003335 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003336 return std::string();
3337 }
3338
3339 RecordData Record;
3340 while (!Stream.AtEndOfStream()) {
3341 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00003342
Douglas Gregorb64c1932009-05-12 01:31:05 +00003343 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
3344 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump1eb44332009-09-09 15:08:12 +00003345
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003346 // We only know the AST subblock ID.
Douglas Gregorb64c1932009-05-12 01:31:05 +00003347 switch (BlockID) {
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003348 case CONTROL_BLOCK_ID:
3349 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003350 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003351 return std::string();
3352 }
3353 break;
Mike Stump1eb44332009-09-09 15:08:12 +00003354
Douglas Gregorb64c1932009-05-12 01:31:05 +00003355 default:
3356 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003357 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003358 return std::string();
3359 }
3360 break;
3361 }
3362 continue;
3363 }
3364
3365 if (Code == llvm::bitc::END_BLOCK) {
3366 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003367 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003368 return std::string();
3369 }
3370 continue;
3371 }
3372
3373 if (Code == llvm::bitc::DEFINE_ABBREV) {
3374 Stream.ReadAbbrevRecord();
3375 continue;
3376 }
3377
3378 Record.clear();
3379 const char *BlobStart = 0;
3380 unsigned BlobLen = 0;
Douglas Gregor39c497b2012-10-18 18:36:53 +00003381 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen) == ORIGINAL_FILE)
Douglas Gregorb64c1932009-05-12 01:31:05 +00003382 return std::string(BlobStart, BlobLen);
Mike Stump1eb44332009-09-09 15:08:12 +00003383 }
Douglas Gregorb64c1932009-05-12 01:31:05 +00003384
3385 return std::string();
3386}
3387
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00003388namespace {
3389 class SimplePCHValidator : public ASTReaderListener {
3390 const LangOptions &ExistingLangOpts;
3391 const TargetOptions &ExistingTargetOpts;
3392
3393 public:
3394 SimplePCHValidator(const LangOptions &ExistingLangOpts,
3395 const TargetOptions &ExistingTargetOpts)
3396 : ExistingLangOpts(ExistingLangOpts),
3397 ExistingTargetOpts(ExistingTargetOpts)
3398 {
3399 }
3400
3401 virtual bool ReadLanguageOptions(const LangOptions &LangOpts,
3402 bool Complain) {
3403 return checkLanguageOptions(ExistingLangOpts, LangOpts, 0);
3404 }
3405 virtual bool ReadTargetOptions(const TargetOptions &TargetOpts,
3406 bool Complain) {
3407 return checkTargetOptions(ExistingTargetOpts, TargetOpts, 0);
3408 }
3409 };
3410}
3411
3412bool ASTReader::isAcceptableASTFile(StringRef Filename,
3413 FileManager &FileMgr,
3414 const LangOptions &LangOpts,
3415 const TargetOptions &TargetOpts) {
3416 // Open the AST file.
3417 std::string ErrStr;
3418 OwningPtr<llvm::MemoryBuffer> Buffer;
3419 Buffer.reset(FileMgr.getBufferForFile(Filename, &ErrStr));
3420 if (!Buffer) {
3421 return false;
3422 }
3423
3424 // Initialize the stream
3425 llvm::BitstreamReader StreamFile;
3426 llvm::BitstreamCursor Stream;
3427 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
3428 (const unsigned char *)Buffer->getBufferEnd());
3429 Stream.init(StreamFile);
3430
3431 // Sniff for the signature.
3432 if (Stream.Read(8) != 'C' ||
3433 Stream.Read(8) != 'P' ||
3434 Stream.Read(8) != 'C' ||
3435 Stream.Read(8) != 'H') {
3436 return false;
3437 }
3438
3439 SimplePCHValidator Validator(LangOpts, TargetOpts);
3440 RecordData Record;
3441 bool InControlBlock = false;
3442 while (!Stream.AtEndOfStream()) {
3443 unsigned Code = Stream.ReadCode();
3444
3445 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
3446 unsigned BlockID = Stream.ReadSubBlockID();
3447
3448 // We only know the control subblock ID.
3449 switch (BlockID) {
3450 case CONTROL_BLOCK_ID:
3451 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
3452 return false;
3453 } else {
3454 InControlBlock = true;
3455 }
3456 break;
3457
3458 default:
3459 if (Stream.SkipBlock())
3460 return false;
3461 break;
3462 }
3463 continue;
3464 }
3465
3466 if (Code == llvm::bitc::END_BLOCK) {
3467 if (Stream.ReadBlockEnd()) {
3468 return false;
3469 }
3470 InControlBlock = false;
3471 continue;
3472 }
3473
3474 if (Code == llvm::bitc::DEFINE_ABBREV) {
3475 Stream.ReadAbbrevRecord();
3476 continue;
3477 }
3478
3479 Record.clear();
3480 const char *BlobStart = 0;
3481 unsigned BlobLen = 0;
3482 unsigned RecCode = Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen);
3483 if (InControlBlock) {
3484 switch ((ControlRecordTypes)RecCode) {
3485 case METADATA: {
3486 if (Record[0] != VERSION_MAJOR) {
3487 return false;
3488 }
3489
3490 const std::string &CurBranch = getClangFullRepositoryVersion();
3491 StringRef ASTBranch(BlobStart, BlobLen);
3492 if (StringRef(CurBranch) != ASTBranch)
3493 return false;
3494
3495 break;
3496 }
3497 case LANGUAGE_OPTIONS:
3498 if (ParseLanguageOptions(Record, false, Validator))
3499 return false;
3500 break;
3501
3502 case TARGET_OPTIONS:
3503 if (ParseTargetOptions(Record, false, Validator))
3504 return false;
3505 break;
3506
Douglas Gregor5f3d8222012-10-24 15:17:15 +00003507 case DIAGNOSTIC_OPTIONS:
3508 if (ParseDiagnosticOptions(Record, false, Validator))
3509 return false;
3510 break;
3511
Douglas Gregorbbf38312012-10-24 16:50:34 +00003512 case FILE_SYSTEM_OPTIONS:
3513 if (ParseFileSystemOptions(Record, false, Validator))
3514 return false;
3515 break;
3516
3517 case HEADER_SEARCH_OPTIONS:
3518 if (ParseHeaderSearchOptions(Record, false, Validator))
3519 return false;
3520 break;
3521
Douglas Gregora71a7d82012-10-24 20:05:57 +00003522 case PREPROCESSOR_OPTIONS:
3523 if (ParsePreprocessorOptions(Record, false, Validator))
3524 return false;
3525 break;
3526
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00003527 default:
3528 // No other validation to perform.
3529 break;
3530 }
3531 }
3532 }
3533
3534 return true;
3535}
3536
Douglas Gregor4825fd72012-10-22 22:50:17 +00003537bool ASTReader::ReadSubmoduleBlock(ModuleFile &F) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003538 // Enter the submodule block.
3539 if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
3540 Error("malformed submodule block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003541 return true;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003542 }
3543
3544 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
Douglas Gregor26ced122011-12-01 00:59:36 +00003545 bool First = true;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003546 Module *CurrentModule = 0;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003547 RecordData Record;
3548 while (true) {
3549 unsigned Code = F.Stream.ReadCode();
3550 if (Code == llvm::bitc::END_BLOCK) {
3551 if (F.Stream.ReadBlockEnd()) {
3552 Error("error at end of submodule block in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003553 return true;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003554 }
Douglas Gregor4825fd72012-10-22 22:50:17 +00003555 return false;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003556 }
3557
3558 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
3559 // No known subblocks, always skip them.
3560 F.Stream.ReadSubBlockID();
3561 if (F.Stream.SkipBlock()) {
3562 Error("malformed block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003563 return true;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003564 }
3565 continue;
3566 }
3567
3568 if (Code == llvm::bitc::DEFINE_ABBREV) {
3569 F.Stream.ReadAbbrevRecord();
3570 continue;
3571 }
3572
3573 // Read a record.
3574 const char *BlobStart;
3575 unsigned BlobLen;
3576 Record.clear();
3577 switch (F.Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
3578 default: // Default behavior: ignore.
3579 break;
3580
3581 case SUBMODULE_DEFINITION: {
Douglas Gregor26ced122011-12-01 00:59:36 +00003582 if (First) {
3583 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003584 return true;
Douglas Gregor26ced122011-12-01 00:59:36 +00003585 }
3586
Douglas Gregore209e502011-12-06 01:10:29 +00003587 if (Record.size() < 7) {
Douglas Gregor1e123682011-12-05 22:27:44 +00003588 Error("malformed module definition");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003589 return true;
Douglas Gregor1e123682011-12-05 22:27:44 +00003590 }
3591
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003592 StringRef Name(BlobStart, BlobLen);
Douglas Gregore209e502011-12-06 01:10:29 +00003593 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[0]);
3594 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[1]);
3595 bool IsFramework = Record[2];
3596 bool IsExplicit = Record[3];
Douglas Gregora1f1fad2012-01-27 19:52:33 +00003597 bool IsSystem = Record[4];
3598 bool InferSubmodules = Record[5];
3599 bool InferExplicitSubmodules = Record[6];
3600 bool InferExportWildcard = Record[7];
Douglas Gregor1e123682011-12-05 22:27:44 +00003601
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003602 Module *ParentModule = 0;
Douglas Gregor26ced122011-12-01 00:59:36 +00003603 if (Parent)
3604 ParentModule = getSubmodule(Parent);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003605
3606 // Retrieve this (sub)module from the module map, creating it if
3607 // necessary.
3608 CurrentModule = ModMap.findOrCreateModule(Name, ParentModule,
3609 IsFramework,
3610 IsExplicit).first;
Douglas Gregore209e502011-12-06 01:10:29 +00003611 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
3612 if (GlobalIndex >= SubmodulesLoaded.size() ||
3613 SubmodulesLoaded[GlobalIndex]) {
Douglas Gregor26ced122011-12-01 00:59:36 +00003614 Error("too many submodules");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003615 return true;
Douglas Gregor26ced122011-12-01 00:59:36 +00003616 }
Douglas Gregora015cab2011-12-02 17:30:13 +00003617
Argyrios Kyrtzidisd64c26f2012-10-03 01:58:42 +00003618 CurrentModule->setASTFile(F.File);
Douglas Gregor305dc3e2011-12-20 00:28:52 +00003619 CurrentModule->IsFromModuleFile = true;
Douglas Gregora1f1fad2012-01-27 19:52:33 +00003620 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
Douglas Gregor1e123682011-12-05 22:27:44 +00003621 CurrentModule->InferSubmodules = InferSubmodules;
3622 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
3623 CurrentModule->InferExportWildcard = InferExportWildcard;
Douglas Gregora015cab2011-12-02 17:30:13 +00003624 if (DeserializationListener)
Douglas Gregore209e502011-12-06 01:10:29 +00003625 DeserializationListener->ModuleRead(GlobalID, CurrentModule);
Douglas Gregora015cab2011-12-02 17:30:13 +00003626
Douglas Gregore209e502011-12-06 01:10:29 +00003627 SubmodulesLoaded[GlobalIndex] = CurrentModule;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003628 break;
3629 }
3630
Douglas Gregor77d029f2011-12-08 19:11:24 +00003631 case SUBMODULE_UMBRELLA_HEADER: {
Douglas Gregor26ced122011-12-01 00:59:36 +00003632 if (First) {
3633 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003634 return true;
Douglas Gregor26ced122011-12-01 00:59:36 +00003635 }
3636
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003637 if (!CurrentModule)
3638 break;
3639
3640 StringRef FileName(BlobStart, BlobLen);
3641 if (const FileEntry *Umbrella = PP.getFileManager().getFile(FileName)) {
Douglas Gregor10694ce2011-12-08 17:39:04 +00003642 if (!CurrentModule->getUmbrellaHeader())
Douglas Gregore209e502011-12-06 01:10:29 +00003643 ModMap.setUmbrellaHeader(CurrentModule, Umbrella);
Douglas Gregor10694ce2011-12-08 17:39:04 +00003644 else if (CurrentModule->getUmbrellaHeader() != Umbrella) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003645 Error("mismatched umbrella headers in submodule");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003646 return true;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003647 }
3648 }
3649 break;
3650 }
3651
3652 case SUBMODULE_HEADER: {
Douglas Gregor26ced122011-12-01 00:59:36 +00003653 if (First) {
3654 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003655 return true;
Douglas Gregor26ced122011-12-01 00:59:36 +00003656 }
3657
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003658 if (!CurrentModule)
3659 break;
3660
3661 // FIXME: Be more lazy about this!
3662 StringRef FileName(BlobStart, BlobLen);
3663 if (const FileEntry *File = PP.getFileManager().getFile(FileName)) {
3664 if (std::find(CurrentModule->Headers.begin(),
3665 CurrentModule->Headers.end(),
3666 File) == CurrentModule->Headers.end())
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00003667 ModMap.addHeader(CurrentModule, File, false);
3668 }
3669 break;
3670 }
3671
3672 case SUBMODULE_EXCLUDED_HEADER: {
3673 if (First) {
3674 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003675 return true;
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00003676 }
3677
3678 if (!CurrentModule)
3679 break;
3680
3681 // FIXME: Be more lazy about this!
3682 StringRef FileName(BlobStart, BlobLen);
3683 if (const FileEntry *File = PP.getFileManager().getFile(FileName)) {
3684 if (std::find(CurrentModule->Headers.begin(),
3685 CurrentModule->Headers.end(),
3686 File) == CurrentModule->Headers.end())
3687 ModMap.addHeader(CurrentModule, File, true);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003688 }
3689 break;
3690 }
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00003691
3692 case SUBMODULE_TOPHEADER: {
3693 if (First) {
3694 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003695 return true;
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00003696 }
3697
3698 if (!CurrentModule)
3699 break;
3700
3701 // FIXME: Be more lazy about this!
3702 StringRef FileName(BlobStart, BlobLen);
3703 if (const FileEntry *File = PP.getFileManager().getFile(FileName))
3704 CurrentModule->TopHeaders.insert(File);
3705 break;
3706 }
3707
Douglas Gregor77d029f2011-12-08 19:11:24 +00003708 case SUBMODULE_UMBRELLA_DIR: {
3709 if (First) {
3710 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003711 return true;
Douglas Gregor77d029f2011-12-08 19:11:24 +00003712 }
3713
3714 if (!CurrentModule)
3715 break;
3716
3717 StringRef DirName(BlobStart, BlobLen);
3718 if (const DirectoryEntry *Umbrella
3719 = PP.getFileManager().getDirectory(DirName)) {
3720 if (!CurrentModule->getUmbrellaDir())
3721 ModMap.setUmbrellaDir(CurrentModule, Umbrella);
3722 else if (CurrentModule->getUmbrellaDir() != Umbrella) {
3723 Error("mismatched umbrella directories in submodule");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003724 return true;
Douglas Gregor77d029f2011-12-08 19:11:24 +00003725 }
3726 }
3727 break;
3728 }
3729
Douglas Gregor26ced122011-12-01 00:59:36 +00003730 case SUBMODULE_METADATA: {
3731 if (!First) {
3732 Error("submodule metadata record not at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003733 return true;
Douglas Gregor26ced122011-12-01 00:59:36 +00003734 }
3735 First = false;
3736
3737 F.BaseSubmoduleID = getTotalNumSubmodules();
Douglas Gregor26ced122011-12-01 00:59:36 +00003738 F.LocalNumSubmodules = Record[0];
3739 unsigned LocalBaseSubmoduleID = Record[1];
3740 if (F.LocalNumSubmodules > 0) {
3741 // Introduce the global -> local mapping for submodules within this
3742 // module.
3743 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
3744
3745 // Introduce the local -> global mapping for submodules within this
3746 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00003747 F.SubmoduleRemap.insertOrReplace(
Douglas Gregor26ced122011-12-01 00:59:36 +00003748 std::make_pair(LocalBaseSubmoduleID,
3749 F.BaseSubmoduleID - LocalBaseSubmoduleID));
3750
3751 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
3752 }
3753 break;
3754 }
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003755
Douglas Gregor55988682011-12-05 16:33:54 +00003756 case SUBMODULE_IMPORTS: {
3757 if (First) {
3758 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003759 return true;
Douglas Gregor55988682011-12-05 16:33:54 +00003760 }
3761
3762 if (!CurrentModule)
3763 break;
3764
3765 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
3766 UnresolvedModuleImportExport Unresolved;
3767 Unresolved.File = &F;
3768 Unresolved.Mod = CurrentModule;
3769 Unresolved.ID = Record[Idx];
3770 Unresolved.IsImport = true;
3771 Unresolved.IsWildcard = false;
3772 UnresolvedModuleImportExports.push_back(Unresolved);
3773 }
3774 break;
3775 }
3776
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003777 case SUBMODULE_EXPORTS: {
3778 if (First) {
3779 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003780 return true;
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003781 }
3782
3783 if (!CurrentModule)
3784 break;
3785
3786 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
Douglas Gregor55988682011-12-05 16:33:54 +00003787 UnresolvedModuleImportExport Unresolved;
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003788 Unresolved.File = &F;
Douglas Gregor55988682011-12-05 16:33:54 +00003789 Unresolved.Mod = CurrentModule;
3790 Unresolved.ID = Record[Idx];
3791 Unresolved.IsImport = false;
3792 Unresolved.IsWildcard = Record[Idx + 1];
3793 UnresolvedModuleImportExports.push_back(Unresolved);
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003794 }
3795
3796 // Once we've loaded the set of exports, there's no reason to keep
3797 // the parsed, unresolved exports around.
3798 CurrentModule->UnresolvedExports.clear();
3799 break;
3800 }
Douglas Gregor51f564f2011-12-31 04:05:44 +00003801 case SUBMODULE_REQUIRES: {
3802 if (First) {
3803 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003804 return true;
Douglas Gregor51f564f2011-12-31 04:05:44 +00003805 }
3806
3807 if (!CurrentModule)
3808 break;
3809
3810 CurrentModule->addRequirement(StringRef(BlobStart, BlobLen),
David Blaikie4e4d0842012-03-11 07:00:24 +00003811 Context.getLangOpts(),
Douglas Gregordc58aa72012-01-30 06:01:29 +00003812 Context.getTargetInfo());
Douglas Gregor51f564f2011-12-31 04:05:44 +00003813 break;
3814 }
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003815 }
3816 }
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003817}
3818
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003819/// \brief Parse the record that corresponds to a LangOptions data
3820/// structure.
3821///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003822/// This routine parses the language options from the AST file and then gives
3823/// them to the AST listener if one is set.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003824///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003825/// \returns true if the listener deems the file unacceptable, false otherwise.
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00003826bool ASTReader::ParseLanguageOptions(const RecordData &Record,
3827 bool Complain,
3828 ASTReaderListener &Listener) {
3829 LangOptions LangOpts;
3830 unsigned Idx = 0;
Douglas Gregor7d5e81b2011-09-13 18:26:39 +00003831#define LANGOPT(Name, Bits, Default, Description) \
3832 LangOpts.Name = Record[Idx++];
3833#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
3834 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
3835#include "clang/Basic/LangOptions.def"
John McCall260611a2012-06-20 06:18:46 +00003836
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00003837 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
3838 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
3839 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
3840
3841 unsigned Length = Record[Idx++];
3842 LangOpts.CurrentModule.assign(Record.begin() + Idx,
3843 Record.begin() + Idx + Length);
3844 return Listener.ReadLanguageOptions(LangOpts, Complain);
3845}
3846
3847bool ASTReader::ParseTargetOptions(const RecordData &Record,
3848 bool Complain,
3849 ASTReaderListener &Listener) {
3850 unsigned Idx = 0;
3851 TargetOptions TargetOpts;
3852 TargetOpts.Triple = ReadString(Record, Idx);
3853 TargetOpts.CPU = ReadString(Record, Idx);
3854 TargetOpts.ABI = ReadString(Record, Idx);
3855 TargetOpts.CXXABI = ReadString(Record, Idx);
3856 TargetOpts.LinkerVersion = ReadString(Record, Idx);
3857 for (unsigned N = Record[Idx++]; N; --N) {
3858 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
3859 }
3860 for (unsigned N = Record[Idx++]; N; --N) {
3861 TargetOpts.Features.push_back(ReadString(Record, Idx));
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003862 }
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003863
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00003864 return Listener.ReadTargetOptions(TargetOpts, Complain);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003865}
3866
Douglas Gregor5f3d8222012-10-24 15:17:15 +00003867bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
3868 ASTReaderListener &Listener) {
3869 DiagnosticOptions DiagOpts;
3870 unsigned Idx = 0;
3871#define DIAGOPT(Name, Bits, Default) DiagOpts.Name = Record[Idx++];
3872#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
3873 DiagOpts.set##Name(static_cast<Type>(Record[Idx++]));
3874#include "clang/Basic/DiagnosticOptions.def"
3875
3876 for (unsigned N = Record[Idx++]; N; --N) {
3877 DiagOpts.Warnings.push_back(ReadString(Record, Idx));
3878 }
3879
3880 return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
3881}
3882
Douglas Gregor1b2c3c02012-10-24 15:49:58 +00003883bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
3884 ASTReaderListener &Listener) {
3885 FileSystemOptions FSOpts;
3886 unsigned Idx = 0;
3887 FSOpts.WorkingDir = ReadString(Record, Idx);
3888 return Listener.ReadFileSystemOptions(FSOpts, Complain);
3889}
3890
Douglas Gregorbbf38312012-10-24 16:50:34 +00003891bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
3892 bool Complain,
3893 ASTReaderListener &Listener) {
3894 HeaderSearchOptions HSOpts;
3895 unsigned Idx = 0;
3896 HSOpts.Sysroot = ReadString(Record, Idx);
3897
3898 // Include entries.
3899 for (unsigned N = Record[Idx++]; N; --N) {
3900 std::string Path = ReadString(Record, Idx);
3901 frontend::IncludeDirGroup Group
3902 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
3903 bool IsUserSupplied = Record[Idx++];
3904 bool IsFramework = Record[Idx++];
3905 bool IgnoreSysRoot = Record[Idx++];
3906 bool IsInternal = Record[Idx++];
3907 bool ImplicitExternC = Record[Idx++];
3908 HSOpts.UserEntries.push_back(
3909 HeaderSearchOptions::Entry(Path, Group, IsUserSupplied, IsFramework,
3910 IgnoreSysRoot, IsInternal, ImplicitExternC));
3911 }
3912
3913 // System header prefixes.
3914 for (unsigned N = Record[Idx++]; N; --N) {
3915 std::string Prefix = ReadString(Record, Idx);
3916 bool IsSystemHeader = Record[Idx++];
3917 HSOpts.SystemHeaderPrefixes.push_back(
3918 HeaderSearchOptions::SystemHeaderPrefix(Prefix, IsSystemHeader));
3919 }
3920
3921 HSOpts.ResourceDir = ReadString(Record, Idx);
3922 HSOpts.ModuleCachePath = ReadString(Record, Idx);
3923 HSOpts.DisableModuleHash = Record[Idx++];
3924 HSOpts.UseBuiltinIncludes = Record[Idx++];
3925 HSOpts.UseStandardSystemIncludes = Record[Idx++];
3926 HSOpts.UseStandardCXXIncludes = Record[Idx++];
3927 HSOpts.UseLibcxx = Record[Idx++];
3928
3929 return Listener.ReadHeaderSearchOptions(HSOpts, Complain);
3930}
3931
Douglas Gregora71a7d82012-10-24 20:05:57 +00003932bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
3933 bool Complain,
3934 ASTReaderListener &Listener) {
3935 PreprocessorOptions PPOpts;
3936 unsigned Idx = 0;
3937
3938 // Macro definitions/undefs
3939 for (unsigned N = Record[Idx++]; N; --N) {
3940 std::string Macro = ReadString(Record, Idx);
3941 bool IsUndef = Record[Idx++];
3942 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
3943 }
3944
3945 // Includes
3946 for (unsigned N = Record[Idx++]; N; --N) {
3947 PPOpts.Includes.push_back(ReadString(Record, Idx));
3948 }
3949
3950 // Macro Includes
3951 for (unsigned N = Record[Idx++]; N; --N) {
3952 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
3953 }
3954
3955 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
3956 PPOpts.ImplicitPTHInclude = ReadString(Record, Idx);
3957 PPOpts.ObjCXXARCStandardLibrary =
3958 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
3959 return Listener.ReadPreprocessorOptions(PPOpts, Complain);
3960}
3961
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003962std::pair<ModuleFile *, unsigned>
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003963ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00003964 GlobalPreprocessedEntityMapType::iterator
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003965 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00003966 assert(I != GlobalPreprocessedEntityMap.end() &&
3967 "Corrupted global preprocessed entity map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003968 ModuleFile *M = I->second;
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003969 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
3970 return std::make_pair(M, LocalIndex);
3971}
3972
Argyrios Kyrtzidis632dcc92012-10-02 16:10:51 +00003973std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
3974ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
3975 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
3976 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
3977 Mod.NumPreprocessedEntities);
3978
3979 return std::make_pair(PreprocessingRecord::iterator(),
3980 PreprocessingRecord::iterator());
3981}
3982
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00003983std::pair<ASTReader::ModuleDeclIterator, ASTReader::ModuleDeclIterator>
3984ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
3985 return std::make_pair(ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
3986 ModuleDeclIterator(this, &Mod,
3987 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
3988}
3989
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003990PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
3991 PreprocessedEntityID PPID = Index+1;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003992 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
3993 ModuleFile &M = *PPInfo.first;
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003994 unsigned LocalIndex = PPInfo.second;
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003995 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
Douglas Gregor4800a5c2011-02-08 21:58:10 +00003996
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00003997 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003998 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003999
4000 unsigned Code = M.PreprocessorDetailCursor.ReadCode();
4001 switch (Code) {
4002 case llvm::bitc::END_BLOCK:
4003 return 0;
4004
4005 case llvm::bitc::ENTER_SUBBLOCK:
4006 Error("unexpected subblock record in preprocessor detail block");
4007 return 0;
4008
4009 case llvm::bitc::DEFINE_ABBREV:
4010 Error("unexpected abbrevation record in preprocessor detail block");
4011 return 0;
4012
4013 default:
4014 break;
4015 }
4016
4017 if (!PP.getPreprocessingRecord()) {
4018 Error("no preprocessing record");
4019 return 0;
4020 }
4021
4022 // Read the record.
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00004023 SourceRange Range(ReadSourceLocation(M, PPOffs.Begin),
4024 ReadSourceLocation(M, PPOffs.End));
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00004025 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
4026 const char *BlobStart = 0;
4027 unsigned BlobLen = 0;
4028 RecordData Record;
4029 PreprocessorDetailRecordTypes RecType =
4030 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.ReadRecord(
4031 Code, Record, BlobStart, BlobLen);
4032 switch (RecType) {
4033 case PPD_MACRO_EXPANSION: {
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00004034 bool isBuiltin = Record[0];
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00004035 IdentifierInfo *Name = 0;
4036 MacroDefinition *Def = 0;
4037 if (isBuiltin)
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00004038 Name = getLocalIdentifier(M, Record[1]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00004039 else {
4040 PreprocessedEntityID
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00004041 GlobalID = getGlobalPreprocessedEntityID(M, Record[1]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00004042 Def =cast<MacroDefinition>(PPRec.getLoadedPreprocessedEntity(GlobalID-1));
4043 }
4044
4045 MacroExpansion *ME;
4046 if (isBuiltin)
4047 ME = new (PPRec) MacroExpansion(Name, Range);
4048 else
4049 ME = new (PPRec) MacroExpansion(Def, Range);
4050
4051 return ME;
4052 }
4053
4054 case PPD_MACRO_DEFINITION: {
4055 // Decode the identifier info and then check again; if the macro is
4056 // still defined and associated with the identifier,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00004057 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00004058 MacroDefinition *MD
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00004059 = new (PPRec) MacroDefinition(II, Range);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00004060
4061 if (DeserializationListener)
4062 DeserializationListener->MacroDefinitionRead(PPID, MD);
4063
4064 return MD;
4065 }
4066
4067 case PPD_INCLUSION_DIRECTIVE: {
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00004068 const char *FullFileNameStart = BlobStart + Record[0];
Argyrios Kyrtzidis29f98b42012-03-08 01:08:28 +00004069 StringRef FullFileName(FullFileNameStart, BlobLen - Record[0]);
4070 const FileEntry *File = 0;
4071 if (!FullFileName.empty())
4072 File = PP.getFileManager().getFile(FullFileName);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00004073
4074 // FIXME: Stable encoding
4075 InclusionDirective::InclusionKind Kind
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00004076 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00004077 InclusionDirective *ID
4078 = new (PPRec) InclusionDirective(PPRec, Kind,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00004079 StringRef(BlobStart, Record[0]),
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00004080 Record[1], Record[3],
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00004081 File,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00004082 Range);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00004083 return ID;
4084 }
4085 }
David Blaikie7530c032012-01-17 06:56:22 +00004086
4087 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00004088}
4089
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00004090/// \brief \arg SLocMapI points at a chunk of a module that contains no
4091/// preprocessed entities or the entities it contains are not the ones we are
4092/// looking for. Find the next module that contains entities and return the ID
4093/// of the first entry.
4094PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
4095 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
4096 ++SLocMapI;
4097 for (GlobalSLocOffsetMapType::const_iterator
4098 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004099 ModuleFile &M = *SLocMapI->second;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00004100 if (M.NumPreprocessedEntities)
4101 return getGlobalPreprocessedEntityID(M, M.BasePreprocessedEntityID);
4102 }
4103
4104 return getTotalNumPreprocessedEntities();
4105}
4106
4107namespace {
4108
4109template <unsigned PPEntityOffset::*PPLoc>
4110struct PPEntityComp {
4111 const ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004112 ModuleFile &M;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00004113
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004114 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) { }
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00004115
Benjamin Kramer88df1252011-09-21 06:42:26 +00004116 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
4117 SourceLocation LHS = getLoc(L);
4118 SourceLocation RHS = getLoc(R);
4119 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4120 }
4121
4122 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00004123 SourceLocation LHS = getLoc(L);
4124 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4125 }
4126
Benjamin Kramer88df1252011-09-21 06:42:26 +00004127 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00004128 SourceLocation RHS = getLoc(R);
4129 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4130 }
4131
4132 SourceLocation getLoc(const PPEntityOffset &PPE) const {
4133 return Reader.ReadSourceLocation(M, PPE.*PPLoc);
4134 }
4135};
4136
4137}
4138
4139/// \brief Returns the first preprocessed entity ID that ends after \arg BLoc.
4140PreprocessedEntityID
4141ASTReader::findBeginPreprocessedEntity(SourceLocation BLoc) const {
4142 if (SourceMgr.isLocalSourceLocation(BLoc))
4143 return getTotalNumPreprocessedEntities();
4144
4145 GlobalSLocOffsetMapType::const_iterator
4146 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
4147 BLoc.getOffset());
4148 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
4149 "Corrupted global sloc offset map");
4150
4151 if (SLocMapI->second->NumPreprocessedEntities == 0)
4152 return findNextPreprocessedEntity(SLocMapI);
4153
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004154 ModuleFile &M = *SLocMapI->second;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00004155 typedef const PPEntityOffset *pp_iterator;
4156 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
4157 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
Argyrios Kyrtzidis4cd06342011-09-22 21:17:02 +00004158
4159 size_t Count = M.NumPreprocessedEntities;
4160 size_t Half;
4161 pp_iterator First = pp_begin;
4162 pp_iterator PPI;
4163
4164 // Do a binary search manually instead of using std::lower_bound because
4165 // The end locations of entities may be unordered (when a macro expansion
4166 // is inside another macro argument), but for this case it is not important
4167 // whether we get the first macro expansion or its containing macro.
4168 while (Count > 0) {
4169 Half = Count/2;
4170 PPI = First;
4171 std::advance(PPI, Half);
4172 if (SourceMgr.isBeforeInTranslationUnit(ReadSourceLocation(M, PPI->End),
4173 BLoc)){
4174 First = PPI;
4175 ++First;
4176 Count = Count - Half - 1;
4177 } else
4178 Count = Half;
4179 }
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00004180
4181 if (PPI == pp_end)
4182 return findNextPreprocessedEntity(SLocMapI);
4183
4184 return getGlobalPreprocessedEntityID(M,
4185 M.BasePreprocessedEntityID + (PPI - pp_begin));
4186}
4187
4188/// \brief Returns the first preprocessed entity ID that begins after \arg ELoc.
4189PreprocessedEntityID
4190ASTReader::findEndPreprocessedEntity(SourceLocation ELoc) const {
4191 if (SourceMgr.isLocalSourceLocation(ELoc))
4192 return getTotalNumPreprocessedEntities();
4193
4194 GlobalSLocOffsetMapType::const_iterator
4195 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
4196 ELoc.getOffset());
4197 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
4198 "Corrupted global sloc offset map");
4199
4200 if (SLocMapI->second->NumPreprocessedEntities == 0)
4201 return findNextPreprocessedEntity(SLocMapI);
4202
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004203 ModuleFile &M = *SLocMapI->second;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00004204 typedef const PPEntityOffset *pp_iterator;
4205 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
4206 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
4207 pp_iterator PPI =
4208 std::upper_bound(pp_begin, pp_end, ELoc,
4209 PPEntityComp<&PPEntityOffset::Begin>(*this, M));
4210
4211 if (PPI == pp_end)
4212 return findNextPreprocessedEntity(SLocMapI);
4213
4214 return getGlobalPreprocessedEntityID(M,
4215 M.BasePreprocessedEntityID + (PPI - pp_begin));
4216}
4217
4218/// \brief Returns a pair of [Begin, End) indices of preallocated
4219/// preprocessed entities that \arg Range encompasses.
4220std::pair<unsigned, unsigned>
4221 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
4222 if (Range.isInvalid())
4223 return std::make_pair(0,0);
4224 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
4225
4226 PreprocessedEntityID BeginID = findBeginPreprocessedEntity(Range.getBegin());
4227 PreprocessedEntityID EndID = findEndPreprocessedEntity(Range.getEnd());
4228 return std::make_pair(BeginID, EndID);
4229}
4230
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00004231/// \brief Optionally returns true or false if the preallocated preprocessed
4232/// entity with index \arg Index came from file \arg FID.
4233llvm::Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
4234 FileID FID) {
4235 if (FID.isInvalid())
4236 return false;
4237
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004238 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
4239 ModuleFile &M = *PPInfo.first;
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00004240 unsigned LocalIndex = PPInfo.second;
4241 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
4242
4243 SourceLocation Loc = ReadSourceLocation(M, PPOffs.Begin);
4244 if (Loc.isInvalid())
4245 return false;
4246
4247 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
4248 return true;
4249 else
4250 return false;
4251}
4252
Douglas Gregord10a3812011-08-25 18:14:34 +00004253namespace {
4254 /// \brief Visitor used to search for information about a header file.
4255 class HeaderFileInfoVisitor {
4256 ASTReader &Reader;
4257 const FileEntry *FE;
4258
4259 llvm::Optional<HeaderFileInfo> HFI;
4260
4261 public:
4262 HeaderFileInfoVisitor(ASTReader &Reader, const FileEntry *FE)
4263 : Reader(Reader), FE(FE) { }
4264
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004265 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregord10a3812011-08-25 18:14:34 +00004266 HeaderFileInfoVisitor *This
4267 = static_cast<HeaderFileInfoVisitor *>(UserData);
4268
4269 HeaderFileInfoTrait Trait(This->Reader, M,
4270 &This->Reader.getPreprocessor().getHeaderSearchInfo(),
4271 M.HeaderFileFrameworkStrings,
4272 This->FE->getName());
4273
4274 HeaderFileInfoLookupTable *Table
4275 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
4276 if (!Table)
4277 return false;
4278
4279 // Look in the on-disk hash table for an entry for this file name.
4280 HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE->getName(),
4281 &Trait);
4282 if (Pos == Table->end())
4283 return false;
4284
4285 This->HFI = *Pos;
4286 return true;
4287 }
4288
4289 llvm::Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
4290 };
4291}
4292
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00004293HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
Douglas Gregord10a3812011-08-25 18:14:34 +00004294 HeaderFileInfoVisitor Visitor(*this, FE);
4295 ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor);
4296 if (llvm::Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00004297 if (Listener)
Douglas Gregord10a3812011-08-25 18:14:34 +00004298 Listener->ReadHeaderFileInfo(*HFI, FE->getUID());
4299 return *HFI;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00004300 }
4301
4302 return HeaderFileInfo();
4303}
4304
David Blaikied6471f72011-09-25 23:23:43 +00004305void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00004306 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004307 ModuleFile &F = *(*I);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00004308 unsigned Idx = 0;
4309 while (Idx < F.PragmaDiagMappings.size()) {
4310 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
Argyrios Kyrtzidis87429a02011-11-09 01:24:17 +00004311 Diag.DiagStates.push_back(*Diag.GetCurDiagState());
4312 Diag.DiagStatePoints.push_back(
4313 DiagnosticsEngine::DiagStatePoint(&Diag.DiagStates.back(),
4314 FullSourceLoc(Loc, SourceMgr)));
Douglas Gregorf62d43d2011-07-19 16:10:42 +00004315 while (1) {
4316 assert(Idx < F.PragmaDiagMappings.size() &&
4317 "Invalid data, didn't find '-1' marking end of diag/map pairs");
4318 if (Idx >= F.PragmaDiagMappings.size()) {
4319 break; // Something is messed up but at least avoid infinite loop in
4320 // release build.
4321 }
4322 unsigned DiagID = F.PragmaDiagMappings[Idx++];
4323 if (DiagID == (unsigned)-1) {
4324 break; // no more diag/map pairs for this location.
4325 }
4326 diag::Mapping Map = (diag::Mapping)F.PragmaDiagMappings[Idx++];
Argyrios Kyrtzidis87429a02011-11-09 01:24:17 +00004327 DiagnosticMappingInfo MappingInfo = Diag.makeMappingInfo(Map, Loc);
4328 Diag.GetCurDiagState()->setMappingInfo(DiagID, MappingInfo);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00004329 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00004330 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00004331 }
4332}
4333
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00004334/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004335ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Douglas Gregora119da02011-08-02 16:26:37 +00004336 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
Jonathan D. Turnere9b76c12011-07-20 21:31:32 +00004337 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004338 ModuleFile *M = I->second;
Douglas Gregore3605012011-08-02 18:32:54 +00004339 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00004340}
4341
4342/// \brief Read and return the type with the given index..
Douglas Gregor2cf26342009-04-09 22:27:44 +00004343///
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00004344/// The index is the type ID, shifted and minus the number of predefs. This
4345/// routine actually reads the record corresponding to the type at the given
4346/// location. It is a helper routine for GetType, which deals with reading type
4347/// IDs.
Douglas Gregor393f2492011-07-22 00:38:23 +00004348QualType ASTReader::readTypeRecord(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00004349 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00004350 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00004351
Douglas Gregor0b748912009-04-14 21:18:50 +00004352 // Keep track of where we are in the stream, then jump back there
4353 // after reading this type.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004354 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregor0b748912009-04-14 21:18:50 +00004355
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00004356 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redl27372b42010-08-11 18:52:41 +00004357
Douglas Gregord89275b2009-07-06 18:54:52 +00004358 // Note that we are loading a type record.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004359 Deserializing AType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00004360
Douglas Gregor393f2492011-07-22 00:38:23 +00004361 unsigned Idx = 0;
Sebastian Redlc3632732010-10-05 15:59:54 +00004362 DeclsCursor.JumpToBit(Loc.Offset);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004363 RecordData Record;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004364 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004365 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
4366 case TYPE_EXT_QUAL: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004367 if (Record.size() != 2) {
4368 Error("Incorrect encoding of extended qualifier type");
4369 return QualType();
4370 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004371 QualType Base = readType(*Loc.F, Record, Idx);
4372 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
Douglas Gregor35942772011-09-09 21:34:22 +00004373 return Context.getQualifiedType(Base, Quals);
Douglas Gregor6d473962009-04-15 22:00:08 +00004374 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004375
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004376 case TYPE_COMPLEX: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004377 if (Record.size() != 1) {
4378 Error("Incorrect encoding of complex type");
4379 return QualType();
4380 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004381 QualType ElemType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004382 return Context.getComplexType(ElemType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004383 }
4384
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004385 case TYPE_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004386 if (Record.size() != 1) {
4387 Error("Incorrect encoding of pointer type");
4388 return QualType();
4389 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004390 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004391 return Context.getPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004392 }
4393
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004394 case TYPE_BLOCK_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004395 if (Record.size() != 1) {
4396 Error("Incorrect encoding of block pointer type");
4397 return QualType();
4398 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004399 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004400 return Context.getBlockPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004401 }
4402
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004403 case TYPE_LVALUE_REFERENCE: {
Richard Smithdf1550f2011-04-12 10:38:03 +00004404 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004405 Error("Incorrect encoding of lvalue reference type");
4406 return QualType();
4407 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004408 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004409 return Context.getLValueReferenceType(PointeeType, Record[1]);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004410 }
4411
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004412 case TYPE_RVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004413 if (Record.size() != 1) {
4414 Error("Incorrect encoding of rvalue reference type");
4415 return QualType();
4416 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004417 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004418 return Context.getRValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004419 }
4420
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004421 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidis240437b2010-07-02 11:55:15 +00004422 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004423 Error("Incorrect encoding of member pointer type");
4424 return QualType();
4425 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004426 QualType PointeeType = readType(*Loc.F, Record, Idx);
4427 QualType ClassType = readType(*Loc.F, Record, Idx);
Douglas Gregor1ab55e92010-12-10 17:03:06 +00004428 if (PointeeType.isNull() || ClassType.isNull())
4429 return QualType();
4430
Douglas Gregor35942772011-09-09 21:34:22 +00004431 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregor2cf26342009-04-09 22:27:44 +00004432 }
4433
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004434 case TYPE_CONSTANT_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00004435 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004436 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4437 unsigned IndexTypeQuals = Record[2];
4438 unsigned Idx = 3;
4439 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004440 return Context.getConstantArrayType(ElementType, Size,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004441 ASM, IndexTypeQuals);
4442 }
4443
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004444 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00004445 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004446 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4447 unsigned IndexTypeQuals = Record[2];
Douglas Gregor35942772011-09-09 21:34:22 +00004448 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004449 }
4450
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004451 case TYPE_VARIABLE_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00004452 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregor0b748912009-04-14 21:18:50 +00004453 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4454 unsigned IndexTypeQuals = Record[2];
Sebastian Redlc3632732010-10-05 15:59:54 +00004455 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
4456 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
Douglas Gregor35942772011-09-09 21:34:22 +00004457 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004458 ASM, IndexTypeQuals,
4459 SourceRange(LBLoc, RBLoc));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004460 }
4461
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004462 case TYPE_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00004463 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004464 Error("incorrect encoding of vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004465 return QualType();
4466 }
4467
Douglas Gregor393f2492011-07-22 00:38:23 +00004468 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004469 unsigned NumElements = Record[1];
Bob Wilsone86d78c2010-11-10 21:56:12 +00004470 unsigned VecKind = Record[2];
Douglas Gregor35942772011-09-09 21:34:22 +00004471 return Context.getVectorType(ElementType, NumElements,
Bob Wilsone86d78c2010-11-10 21:56:12 +00004472 (VectorType::VectorKind)VecKind);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004473 }
4474
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004475 case TYPE_EXT_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00004476 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004477 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004478 return QualType();
4479 }
4480
Douglas Gregor393f2492011-07-22 00:38:23 +00004481 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004482 unsigned NumElements = Record[1];
Douglas Gregor35942772011-09-09 21:34:22 +00004483 return Context.getExtVectorType(ElementType, NumElements);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004484 }
4485
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004486 case TYPE_FUNCTION_NO_PROTO: {
John McCallf85e1932011-06-15 23:02:42 +00004487 if (Record.size() != 6) {
Douglas Gregora02b1472009-04-28 21:53:25 +00004488 Error("incorrect encoding of no-proto function type");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004489 return QualType();
4490 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004491 QualType ResultType = readType(*Loc.F, Record, Idx);
John McCallf85e1932011-06-15 23:02:42 +00004492 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
4493 (CallingConv)Record[4], Record[5]);
Douglas Gregor35942772011-09-09 21:34:22 +00004494 return Context.getFunctionNoProtoType(ResultType, Info);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004495 }
4496
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004497 case TYPE_FUNCTION_PROTO: {
Douglas Gregor393f2492011-07-22 00:38:23 +00004498 QualType ResultType = readType(*Loc.F, Record, Idx);
John McCalle23cf432010-12-14 08:05:40 +00004499
4500 FunctionProtoType::ExtProtoInfo EPI;
4501 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
Eli Friedmana49218e2011-04-09 08:18:08 +00004502 /*hasregparm*/ Record[2],
4503 /*regparm*/ Record[3],
John McCallf85e1932011-06-15 23:02:42 +00004504 static_cast<CallingConv>(Record[4]),
4505 /*produces*/ Record[5]);
John McCalle23cf432010-12-14 08:05:40 +00004506
John McCallf85e1932011-06-15 23:02:42 +00004507 unsigned Idx = 6;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004508 unsigned NumParams = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00004509 SmallVector<QualType, 16> ParamTypes;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004510 for (unsigned I = 0; I != NumParams; ++I)
Douglas Gregor393f2492011-07-22 00:38:23 +00004511 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
John McCalle23cf432010-12-14 08:05:40 +00004512
4513 EPI.Variadic = Record[Idx++];
Richard Smitheefb3d52012-02-10 09:58:53 +00004514 EPI.HasTrailingReturn = Record[Idx++];
John McCalle23cf432010-12-14 08:05:40 +00004515 EPI.TypeQuals = Record[Idx++];
Douglas Gregorc938c162011-01-26 05:01:58 +00004516 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
Sebastian Redl60618fa2011-03-12 11:50:43 +00004517 ExceptionSpecificationType EST =
4518 static_cast<ExceptionSpecificationType>(Record[Idx++]);
4519 EPI.ExceptionSpecType = EST;
Douglas Gregorb0d06e22012-04-04 00:34:49 +00004520 SmallVector<QualType, 2> Exceptions;
Sebastian Redl60618fa2011-03-12 11:50:43 +00004521 if (EST == EST_Dynamic) {
4522 EPI.NumExceptions = Record[Idx++];
Sebastian Redl60618fa2011-03-12 11:50:43 +00004523 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
Douglas Gregor393f2492011-07-22 00:38:23 +00004524 Exceptions.push_back(readType(*Loc.F, Record, Idx));
Sebastian Redl60618fa2011-03-12 11:50:43 +00004525 EPI.Exceptions = Exceptions.data();
4526 } else if (EST == EST_ComputedNoexcept) {
4527 EPI.NoexceptExpr = ReadExpr(*Loc.F);
Richard Smith7bb698a2012-04-21 17:47:47 +00004528 } else if (EST == EST_Uninstantiated) {
4529 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
4530 EPI.ExceptionSpecTemplate = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
Richard Smithb9d0b762012-07-27 04:22:15 +00004531 } else if (EST == EST_Unevaluated) {
4532 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
Sebastian Redl60618fa2011-03-12 11:50:43 +00004533 }
Douglas Gregor35942772011-09-09 21:34:22 +00004534 return Context.getFunctionType(ResultType, ParamTypes.data(), NumParams,
John McCalle23cf432010-12-14 08:05:40 +00004535 EPI);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004536 }
4537
Douglas Gregor409448c2011-07-21 22:35:25 +00004538 case TYPE_UNRESOLVED_USING: {
4539 unsigned Idx = 0;
Douglas Gregor35942772011-09-09 21:34:22 +00004540 return Context.getTypeDeclType(
Douglas Gregor409448c2011-07-21 22:35:25 +00004541 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
4542 }
4543
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004544 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00004545 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004546 Error("incorrect encoding of typedef type");
4547 return QualType();
4548 }
Douglas Gregor409448c2011-07-21 22:35:25 +00004549 unsigned Idx = 0;
4550 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004551 QualType Canonical = readType(*Loc.F, Record, Idx);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00004552 if (!Canonical.isNull())
Douglas Gregor35942772011-09-09 21:34:22 +00004553 Canonical = Context.getCanonicalType(Canonical);
4554 return Context.getTypedefType(Decl, Canonical);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00004555 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004556
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004557 case TYPE_TYPEOF_EXPR:
Douglas Gregor35942772011-09-09 21:34:22 +00004558 return Context.getTypeOfExprType(ReadExpr(*Loc.F));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004559
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004560 case TYPE_TYPEOF: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004561 if (Record.size() != 1) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004562 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004563 return QualType();
4564 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004565 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004566 return Context.getTypeOfType(UnderlyingType);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004567 }
Mike Stump1eb44332009-09-09 15:08:12 +00004568
Douglas Gregorf8af9822012-02-12 18:42:33 +00004569 case TYPE_DECLTYPE: {
4570 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
4571 return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
4572 }
Anders Carlsson395b4752009-06-24 19:06:50 +00004573
Sean Huntca63c202011-05-24 22:41:36 +00004574 case TYPE_UNARY_TRANSFORM: {
Douglas Gregor393f2492011-07-22 00:38:23 +00004575 QualType BaseType = readType(*Loc.F, Record, Idx);
4576 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
Sean Huntca63c202011-05-24 22:41:36 +00004577 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
Douglas Gregor35942772011-09-09 21:34:22 +00004578 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
Sean Huntca63c202011-05-24 22:41:36 +00004579 }
4580
Richard Smith34b41d92011-02-20 03:19:35 +00004581 case TYPE_AUTO:
Douglas Gregor35942772011-09-09 21:34:22 +00004582 return Context.getAutoType(readType(*Loc.F, Record, Idx));
Richard Smith34b41d92011-02-20 03:19:35 +00004583
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004584 case TYPE_RECORD: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004585 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004586 Error("incorrect encoding of record type");
4587 return QualType();
4588 }
Douglas Gregor409448c2011-07-21 22:35:25 +00004589 unsigned Idx = 0;
4590 bool IsDependent = Record[Idx++];
Douglas Gregor56ca8a92012-01-17 19:21:53 +00004591 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
4592 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
4593 QualType T = Context.getRecordType(RD);
John McCallf4c73712011-01-19 06:33:43 +00004594 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004595 return T;
4596 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004597
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004598 case TYPE_ENUM: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004599 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004600 Error("incorrect encoding of enum type");
4601 return QualType();
4602 }
Douglas Gregor409448c2011-07-21 22:35:25 +00004603 unsigned Idx = 0;
4604 bool IsDependent = Record[Idx++];
4605 QualType T
Douglas Gregor35942772011-09-09 21:34:22 +00004606 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
John McCallf4c73712011-01-19 06:33:43 +00004607 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004608 return T;
4609 }
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004610
John McCall9d156a72011-01-06 01:58:22 +00004611 case TYPE_ATTRIBUTED: {
4612 if (Record.size() != 3) {
4613 Error("incorrect encoding of attributed type");
4614 return QualType();
4615 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004616 QualType modifiedType = readType(*Loc.F, Record, Idx);
4617 QualType equivalentType = readType(*Loc.F, Record, Idx);
John McCall9d156a72011-01-06 01:58:22 +00004618 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
Douglas Gregor35942772011-09-09 21:34:22 +00004619 return Context.getAttributedType(kind, modifiedType, equivalentType);
John McCall9d156a72011-01-06 01:58:22 +00004620 }
4621
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004622 case TYPE_PAREN: {
4623 if (Record.size() != 1) {
4624 Error("incorrect encoding of paren type");
4625 return QualType();
4626 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004627 QualType InnerType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004628 return Context.getParenType(InnerType);
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004629 }
4630
Douglas Gregor7536dd52010-12-20 02:24:11 +00004631 case TYPE_PACK_EXPANSION: {
Douglas Gregorf9997a02011-02-01 15:24:58 +00004632 if (Record.size() != 2) {
Douglas Gregor7536dd52010-12-20 02:24:11 +00004633 Error("incorrect encoding of pack expansion type");
4634 return QualType();
4635 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004636 QualType Pattern = readType(*Loc.F, Record, Idx);
Douglas Gregor7536dd52010-12-20 02:24:11 +00004637 if (Pattern.isNull())
4638 return QualType();
Douglas Gregorcded4f62011-01-14 17:04:44 +00004639 llvm::Optional<unsigned> NumExpansions;
4640 if (Record[1])
4641 NumExpansions = Record[1] - 1;
Douglas Gregor35942772011-09-09 21:34:22 +00004642 return Context.getPackExpansionType(Pattern, NumExpansions);
Douglas Gregor7536dd52010-12-20 02:24:11 +00004643 }
4644
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004645 case TYPE_ELABORATED: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004646 unsigned Idx = 0;
4647 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004648 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004649 QualType NamedType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004650 return Context.getElaboratedType(Keyword, NNS, NamedType);
John McCall7da24312009-09-05 00:15:47 +00004651 }
4652
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004653 case TYPE_OBJC_INTERFACE: {
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004654 unsigned Idx = 0;
Douglas Gregor409448c2011-07-21 22:35:25 +00004655 ObjCInterfaceDecl *ItfD
4656 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
Douglas Gregor56ca8a92012-01-17 19:21:53 +00004657 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
John McCallc12c5bb2010-05-15 11:32:37 +00004658 }
4659
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004660 case TYPE_OBJC_OBJECT: {
John McCallc12c5bb2010-05-15 11:32:37 +00004661 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004662 QualType Base = readType(*Loc.F, Record, Idx);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004663 unsigned NumProtos = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00004664 SmallVector<ObjCProtocolDecl*, 4> Protos;
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004665 for (unsigned I = 0; I != NumProtos; ++I)
Douglas Gregor409448c2011-07-21 22:35:25 +00004666 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
Douglas Gregor35942772011-09-09 21:34:22 +00004667 return Context.getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004668 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004669
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004670 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00004671 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004672 QualType Pointee = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004673 return Context.getObjCObjectPointerType(Pointee);
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00004674 }
Argyrios Kyrtzidis24fab412009-09-29 19:42:55 +00004675
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004676 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCall49a832b2009-10-18 09:09:24 +00004677 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004678 QualType Parm = readType(*Loc.F, Record, Idx);
4679 QualType Replacement = readType(*Loc.F, Record, Idx);
John McCall49a832b2009-10-18 09:09:24 +00004680 return
Douglas Gregor35942772011-09-09 21:34:22 +00004681 Context.getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
John McCall49a832b2009-10-18 09:09:24 +00004682 Replacement);
4683 }
John McCall3cb0ebd2010-03-10 03:28:59 +00004684
Douglas Gregorc3069d62011-01-14 02:55:32 +00004685 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
4686 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004687 QualType Parm = readType(*Loc.F, Record, Idx);
Douglas Gregorc3069d62011-01-14 02:55:32 +00004688 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004689 return Context.getSubstTemplateTypeParmPackType(
Douglas Gregorc3069d62011-01-14 02:55:32 +00004690 cast<TemplateTypeParmType>(Parm),
4691 ArgPack);
4692 }
4693
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004694 case TYPE_INJECTED_CLASS_NAME: {
Douglas Gregor409448c2011-07-21 22:35:25 +00004695 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004696 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00004697 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004698 // for AST reading, too much interdependencies.
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00004699 return
Douglas Gregor35942772011-09-09 21:34:22 +00004700 QualType(new (Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCall3cb0ebd2010-03-10 03:28:59 +00004701 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004702
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004703 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004704 unsigned Idx = 0;
4705 unsigned Depth = Record[Idx++];
4706 unsigned Index = Record[Idx++];
4707 bool Pack = Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004708 TemplateTypeParmDecl *D
4709 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004710 return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004711 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004712
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004713 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00004714 unsigned Idx = 0;
4715 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004716 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor95eab172011-07-28 20:55:49 +00004717 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004718 QualType Canon = readType(*Loc.F, Record, Idx);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00004719 if (!Canon.isNull())
Douglas Gregor35942772011-09-09 21:34:22 +00004720 Canon = Context.getCanonicalType(Canon);
4721 return Context.getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00004722 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004723
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004724 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004725 unsigned Idx = 0;
4726 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004727 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor95eab172011-07-28 20:55:49 +00004728 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004729 unsigned NumArgs = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00004730 SmallVector<TemplateArgument, 8> Args;
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004731 Args.reserve(NumArgs);
4732 while (NumArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00004733 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
Douglas Gregor35942772011-09-09 21:34:22 +00004734 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004735 Args.size(), Args.data());
4736 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004737
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004738 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004739 unsigned Idx = 0;
4740
4741 // ArrayType
Douglas Gregor393f2492011-07-22 00:38:23 +00004742 QualType ElementType = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004743 ArrayType::ArraySizeModifier ASM
4744 = (ArrayType::ArraySizeModifier)Record[Idx++];
4745 unsigned IndexTypeQuals = Record[Idx++];
4746
4747 // DependentSizedArrayType
Sebastian Redlc3632732010-10-05 15:59:54 +00004748 Expr *NumElts = ReadExpr(*Loc.F);
4749 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004750
Douglas Gregor35942772011-09-09 21:34:22 +00004751 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004752 IndexTypeQuals, Brackets);
4753 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004754
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004755 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004756 unsigned Idx = 0;
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004757 bool IsDependent = Record[Idx++];
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004758 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
Chris Lattner5f9e2722011-07-23 10:55:15 +00004759 SmallVector<TemplateArgument, 8> Args;
Sebastian Redlc3632732010-10-05 15:59:54 +00004760 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004761 QualType Underlying = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004762 QualType T;
Richard Smith3e4c6c42011-05-05 21:57:07 +00004763 if (Underlying.isNull())
Douglas Gregor35942772011-09-09 21:34:22 +00004764 T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(),
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004765 Args.size());
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00004766 else
Douglas Gregor35942772011-09-09 21:34:22 +00004767 T = Context.getTemplateSpecializationType(Name, Args.data(),
Richard Smith3e4c6c42011-05-05 21:57:07 +00004768 Args.size(), Underlying);
John McCallf4c73712011-01-19 06:33:43 +00004769 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004770 return T;
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004771 }
Eli Friedmanb001de72011-10-06 23:00:33 +00004772
4773 case TYPE_ATOMIC: {
4774 if (Record.size() != 1) {
4775 Error("Incorrect encoding of atomic type");
4776 return QualType();
4777 }
4778 QualType ValueType = readType(*Loc.F, Record, Idx);
4779 return Context.getAtomicType(ValueType);
4780 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00004781 }
David Blaikie7530c032012-01-17 06:56:22 +00004782 llvm_unreachable("Invalid TypeCode!");
Douglas Gregor2cf26342009-04-09 22:27:44 +00004783}
4784
Sebastian Redlc3632732010-10-05 15:59:54 +00004785class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004786 ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004787 ModuleFile &F;
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004788 const ASTReader::RecordData &Record;
John McCalla1ee0c52009-10-16 21:56:05 +00004789 unsigned &Idx;
4790
Sebastian Redlc3632732010-10-05 15:59:54 +00004791 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
4792 unsigned &I) {
4793 return Reader.ReadSourceLocation(F, R, I);
4794 }
4795
Douglas Gregor409448c2011-07-21 22:35:25 +00004796 template<typename T>
4797 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
4798 return Reader.ReadDeclAs<T>(F, Record, Idx);
4799 }
4800
John McCalla1ee0c52009-10-16 21:56:05 +00004801public:
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004802 TypeLocReader(ASTReader &Reader, ModuleFile &F,
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004803 const ASTReader::RecordData &Record, unsigned &Idx)
Benjamin Kramerfacde172012-06-06 17:32:50 +00004804 : Reader(Reader), F(F), Record(Record), Idx(Idx)
Sebastian Redlc3632732010-10-05 15:59:54 +00004805 { }
John McCalla1ee0c52009-10-16 21:56:05 +00004806
John McCall51bd8032009-10-18 01:05:36 +00004807 // We want compile-time assurance that we've enumerated all of
4808 // these, so unfortunately we have to declare them first, then
4809 // define them out-of-line.
4810#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +00004811#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +00004812 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00004813#include "clang/AST/TypeLocNodes.def"
4814
John McCall51bd8032009-10-18 01:05:36 +00004815 void VisitFunctionTypeLoc(FunctionTypeLoc);
4816 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00004817};
4818
John McCall51bd8032009-10-18 01:05:36 +00004819void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCalla1ee0c52009-10-16 21:56:05 +00004820 // nothing to do
4821}
John McCall51bd8032009-10-18 01:05:36 +00004822void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004823 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
Douglas Gregorddf889a2010-01-18 18:04:31 +00004824 if (TL.needsExtraLocalData()) {
4825 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
4826 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
4827 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
4828 TL.setModeAttr(Record[Idx++]);
4829 }
John McCalla1ee0c52009-10-16 21:56:05 +00004830}
John McCall51bd8032009-10-18 01:05:36 +00004831void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004832 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004833}
John McCall51bd8032009-10-18 01:05:36 +00004834void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004835 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004836}
John McCall51bd8032009-10-18 01:05:36 +00004837void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004838 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004839}
John McCall51bd8032009-10-18 01:05:36 +00004840void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004841 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004842}
John McCall51bd8032009-10-18 01:05:36 +00004843void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004844 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004845}
John McCall51bd8032009-10-18 01:05:36 +00004846void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004847 TL.setStarLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +00004848 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004849}
John McCall51bd8032009-10-18 01:05:36 +00004850void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004851 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
4852 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004853 if (Record[Idx++])
Sebastian Redlc3632732010-10-05 15:59:54 +00004854 TL.setSizeExpr(Reader.ReadExpr(F));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004855 else
John McCall51bd8032009-10-18 01:05:36 +00004856 TL.setSizeExpr(0);
4857}
4858void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
4859 VisitArrayTypeLoc(TL);
4860}
4861void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
4862 VisitArrayTypeLoc(TL);
4863}
4864void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
4865 VisitArrayTypeLoc(TL);
4866}
4867void TypeLocReader::VisitDependentSizedArrayTypeLoc(
4868 DependentSizedArrayTypeLoc TL) {
4869 VisitArrayTypeLoc(TL);
4870}
4871void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
4872 DependentSizedExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004873 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004874}
4875void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004876 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004877}
4878void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004879 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004880}
4881void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnara796aa442011-03-12 11:17:06 +00004882 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004883 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4884 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnara796aa442011-03-12 11:17:06 +00004885 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004886 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
Douglas Gregor409448c2011-07-21 22:35:25 +00004887 TL.setArg(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004888 }
4889}
4890void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
4891 VisitFunctionTypeLoc(TL);
4892}
4893void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
4894 VisitFunctionTypeLoc(TL);
4895}
John McCalled976492009-12-04 22:46:56 +00004896void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004897 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalled976492009-12-04 22:46:56 +00004898}
John McCall51bd8032009-10-18 01:05:36 +00004899void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004900 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004901}
4902void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004903 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
4904 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4905 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004906}
4907void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004908 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
4909 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4910 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4911 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004912}
4913void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004914 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004915}
Sean Huntca63c202011-05-24 22:41:36 +00004916void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
4917 TL.setKWLoc(ReadSourceLocation(Record, Idx));
4918 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4919 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4920 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
4921}
Richard Smith34b41d92011-02-20 03:19:35 +00004922void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
4923 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4924}
John McCall51bd8032009-10-18 01:05:36 +00004925void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004926 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004927}
4928void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004929 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004930}
John McCall9d156a72011-01-06 01:58:22 +00004931void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
4932 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
4933 if (TL.hasAttrOperand()) {
4934 SourceRange range;
4935 range.setBegin(ReadSourceLocation(Record, Idx));
4936 range.setEnd(ReadSourceLocation(Record, Idx));
4937 TL.setAttrOperandParensRange(range);
4938 }
4939 if (TL.hasAttrExprOperand()) {
4940 if (Record[Idx++])
4941 TL.setAttrExprOperand(Reader.ReadExpr(F));
4942 else
4943 TL.setAttrExprOperand(0);
4944 } else if (TL.hasAttrEnumOperand())
4945 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
4946}
John McCall51bd8032009-10-18 01:05:36 +00004947void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004948 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004949}
John McCall49a832b2009-10-18 09:09:24 +00004950void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
4951 SubstTemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004952 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall49a832b2009-10-18 09:09:24 +00004953}
Douglas Gregorc3069d62011-01-14 02:55:32 +00004954void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
4955 SubstTemplateTypeParmPackTypeLoc TL) {
4956 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4957}
John McCall51bd8032009-10-18 01:05:36 +00004958void TypeLocReader::VisitTemplateSpecializationTypeLoc(
4959 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004960 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00004961 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
4962 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4963 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall833ca992009-10-29 08:12:44 +00004964 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
4965 TL.setArgLocInfo(i,
Sebastian Redlc3632732010-10-05 15:59:54 +00004966 Reader.GetTemplateArgumentLocInfo(F,
4967 TL.getTypePtr()->getArg(i).getKind(),
4968 Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004969}
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004970void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
4971 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4972 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4973}
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004974void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +00004975 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor9e876872011-03-01 18:12:44 +00004976 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004977}
John McCall3cb0ebd2010-03-10 03:28:59 +00004978void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004979 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall3cb0ebd2010-03-10 03:28:59 +00004980}
Douglas Gregor4714c122010-03-31 17:34:00 +00004981void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +00004982 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor2494dd02011-03-01 01:34:45 +00004983 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00004984 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004985}
John McCall33500952010-06-11 00:33:02 +00004986void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
4987 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004988 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor94fdffa2011-03-01 20:11:18 +00004989 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Abramo Bagnara66581d42012-02-06 22:45:07 +00004990 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004991 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00004992 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4993 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall33500952010-06-11 00:33:02 +00004994 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
4995 TL.setArgLocInfo(I,
Sebastian Redlc3632732010-10-05 15:59:54 +00004996 Reader.GetTemplateArgumentLocInfo(F,
4997 TL.getTypePtr()->getArg(I).getKind(),
4998 Record, Idx));
John McCall33500952010-06-11 00:33:02 +00004999}
Douglas Gregor7536dd52010-12-20 02:24:11 +00005000void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
5001 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
5002}
John McCall51bd8032009-10-18 01:05:36 +00005003void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00005004 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallc12c5bb2010-05-15 11:32:37 +00005005}
5006void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
5007 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00005008 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5009 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00005010 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Sebastian Redlc3632732010-10-05 15:59:54 +00005011 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00005012}
John McCall54e14c42009-10-22 22:37:11 +00005013void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00005014 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall54e14c42009-10-22 22:37:11 +00005015}
Eli Friedmanb001de72011-10-06 23:00:33 +00005016void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
5017 TL.setKWLoc(ReadSourceLocation(Record, Idx));
5018 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5019 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5020}
John McCalla1ee0c52009-10-16 21:56:05 +00005021
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005022TypeSourceInfo *ASTReader::GetTypeSourceInfo(ModuleFile &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00005023 const RecordData &Record,
John McCalla1ee0c52009-10-16 21:56:05 +00005024 unsigned &Idx) {
Douglas Gregor393f2492011-07-22 00:38:23 +00005025 QualType InfoTy = readType(F, Record, Idx);
John McCalla1ee0c52009-10-16 21:56:05 +00005026 if (InfoTy.isNull())
5027 return 0;
5028
Douglas Gregor35942772011-09-09 21:34:22 +00005029 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
Sebastian Redlc3632732010-10-05 15:59:54 +00005030 TypeLocReader TLR(*this, F, Record, Idx);
John McCalla93c9342009-12-07 02:54:59 +00005031 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCalla1ee0c52009-10-16 21:56:05 +00005032 TLR.Visit(TL);
John McCalla93c9342009-12-07 02:54:59 +00005033 return TInfo;
John McCalla1ee0c52009-10-16 21:56:05 +00005034}
Douglas Gregor2cf26342009-04-09 22:27:44 +00005035
Sebastian Redl8538e8d2010-08-18 23:57:32 +00005036QualType ASTReader::GetType(TypeID ID) {
John McCall0953e762009-09-24 19:53:00 +00005037 unsigned FastQuals = ID & Qualifiers::FastMask;
5038 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregor2cf26342009-04-09 22:27:44 +00005039
Sebastian Redl8538e8d2010-08-18 23:57:32 +00005040 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00005041 QualType T;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00005042 switch ((PredefinedTypeIDs)Index) {
5043 case PREDEF_TYPE_NULL_ID: return QualType();
Douglas Gregor35942772011-09-09 21:34:22 +00005044 case PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
5045 case PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00005046
Sebastian Redl8538e8d2010-08-18 23:57:32 +00005047 case PREDEF_TYPE_CHAR_U_ID:
5048 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregor2cf26342009-04-09 22:27:44 +00005049 // FIXME: Check that the signedness of CharTy is correct!
Douglas Gregor35942772011-09-09 21:34:22 +00005050 T = Context.CharTy;
Douglas Gregor2cf26342009-04-09 22:27:44 +00005051 break;
5052
Douglas Gregor35942772011-09-09 21:34:22 +00005053 case PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
5054 case PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
5055 case PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
5056 case PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
5057 case PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
5058 case PREDEF_TYPE_UINT128_ID: T = Context.UnsignedInt128Ty; break;
5059 case PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
5060 case PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
5061 case PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
5062 case PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
5063 case PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
5064 case PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
5065 case PREDEF_TYPE_INT128_ID: T = Context.Int128Ty; break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00005066 case PREDEF_TYPE_HALF_ID: T = Context.HalfTy; break;
Douglas Gregor35942772011-09-09 21:34:22 +00005067 case PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
5068 case PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
5069 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
5070 case PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
5071 case PREDEF_TYPE_BOUND_MEMBER: T = Context.BoundMemberTy; break;
John McCall3c3b7f92011-10-25 17:37:35 +00005072 case PREDEF_TYPE_PSEUDO_OBJECT: T = Context.PseudoObjectTy; break;
Douglas Gregor35942772011-09-09 21:34:22 +00005073 case PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
5074 case PREDEF_TYPE_UNKNOWN_ANY: T = Context.UnknownAnyTy; break;
5075 case PREDEF_TYPE_NULLPTR_ID: T = Context.NullPtrTy; break;
5076 case PREDEF_TYPE_CHAR16_ID: T = Context.Char16Ty; break;
5077 case PREDEF_TYPE_CHAR32_ID: T = Context.Char32Ty; break;
5078 case PREDEF_TYPE_OBJC_ID: T = Context.ObjCBuiltinIdTy; break;
5079 case PREDEF_TYPE_OBJC_CLASS: T = Context.ObjCBuiltinClassTy; break;
5080 case PREDEF_TYPE_OBJC_SEL: T = Context.ObjCBuiltinSelTy; break;
5081 case PREDEF_TYPE_AUTO_DEDUCT: T = Context.getAutoDeductType(); break;
Douglas Gregor3b8043b2011-08-09 15:13:55 +00005082
5083 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
Douglas Gregor35942772011-09-09 21:34:22 +00005084 T = Context.getAutoRRefDeductType();
Douglas Gregor3b8043b2011-08-09 15:13:55 +00005085 break;
John McCall0ddaeb92011-10-17 18:09:15 +00005086
5087 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
5088 T = Context.ARCUnbridgedCastTy;
5089 break;
5090
Meador Ingefb40e3f2012-07-01 15:57:25 +00005091 case PREDEF_TYPE_VA_LIST_TAG:
5092 T = Context.getVaListTagType();
5093 break;
Eli Friedmana6c66ce2012-08-31 00:14:07 +00005094
5095 case PREDEF_TYPE_BUILTIN_FN:
5096 T = Context.BuiltinFnTy;
5097 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00005098 }
5099
5100 assert(!T.isNull() && "Unknown predefined type");
John McCall0953e762009-09-24 19:53:00 +00005101 return T.withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00005102 }
5103
Sebastian Redl8538e8d2010-08-18 23:57:32 +00005104 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00005105 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl07a353c2010-07-14 20:26:45 +00005106 if (TypesLoaded[Index].isNull()) {
Douglas Gregor393f2492011-07-22 00:38:23 +00005107 TypesLoaded[Index] = readTypeRecord(Index);
Douglas Gregor97475832010-10-05 18:37:06 +00005108 if (TypesLoaded[Index].isNull())
5109 return QualType();
5110
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005111 TypesLoaded[Index]->setFromAST();
Sebastian Redl30c514c2010-07-14 23:45:08 +00005112 if (DeserializationListener)
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00005113 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1476ed42010-07-16 16:36:56 +00005114 TypesLoaded[Index]);
Sebastian Redl07a353c2010-07-14 20:26:45 +00005115 }
Mike Stump1eb44332009-09-09 15:08:12 +00005116
John McCall0953e762009-09-24 19:53:00 +00005117 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00005118}
5119
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005120QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
Douglas Gregor393f2492011-07-22 00:38:23 +00005121 return GetType(getGlobalTypeID(F, LocalID));
5122}
5123
5124serialization::TypeID
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005125ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
Douglas Gregora119da02011-08-02 16:26:37 +00005126 unsigned FastQuals = LocalID & Qualifiers::FastMask;
5127 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
5128
5129 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
5130 return LocalID;
5131
5132 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5133 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
5134 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
5135
5136 unsigned GlobalIndex = LocalIndex + I->second;
5137 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
5138}
5139
John McCall833ca992009-10-29 08:12:44 +00005140TemplateArgumentLocInfo
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005141ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
Sebastian Redlc3632732010-10-05 15:59:54 +00005142 TemplateArgument::ArgKind Kind,
John McCall833ca992009-10-29 08:12:44 +00005143 const RecordData &Record,
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00005144 unsigned &Index) {
John McCall833ca992009-10-29 08:12:44 +00005145 switch (Kind) {
5146 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00005147 return ReadExpr(F);
John McCall833ca992009-10-29 08:12:44 +00005148 case TemplateArgument::Type:
Sebastian Redlc3632732010-10-05 15:59:54 +00005149 return GetTypeSourceInfo(F, Record, Index);
Douglas Gregor788cd062009-11-11 01:00:40 +00005150 case TemplateArgument::Template: {
Douglas Gregorb6744ef2011-03-02 17:09:35 +00005151 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
5152 Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00005153 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorb6744ef2011-03-02 17:09:35 +00005154 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregora7fc9012011-01-05 18:58:31 +00005155 SourceLocation());
5156 }
5157 case TemplateArgument::TemplateExpansion: {
Douglas Gregorb6744ef2011-03-02 17:09:35 +00005158 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
5159 Index);
Douglas Gregora7fc9012011-01-05 18:58:31 +00005160 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorba68eca2011-01-05 17:40:24 +00005161 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorb6744ef2011-03-02 17:09:35 +00005162 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregorba68eca2011-01-05 17:40:24 +00005163 EllipsisLoc);
Douglas Gregor788cd062009-11-11 01:00:40 +00005164 }
John McCall833ca992009-10-29 08:12:44 +00005165 case TemplateArgument::Null:
5166 case TemplateArgument::Integral:
5167 case TemplateArgument::Declaration:
Eli Friedmand7a6b162012-09-26 02:36:12 +00005168 case TemplateArgument::NullPtr:
John McCall833ca992009-10-29 08:12:44 +00005169 case TemplateArgument::Pack:
Eli Friedmand7a6b162012-09-26 02:36:12 +00005170 // FIXME: Is this right?
John McCall833ca992009-10-29 08:12:44 +00005171 return TemplateArgumentLocInfo();
5172 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00005173 llvm_unreachable("unexpected template argument loc");
John McCall833ca992009-10-29 08:12:44 +00005174}
5175
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00005176TemplateArgumentLoc
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005177ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00005178 const RecordData &Record, unsigned &Index) {
Sebastian Redlc3632732010-10-05 15:59:54 +00005179 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00005180
5181 if (Arg.getKind() == TemplateArgument::Expression) {
5182 if (Record[Index++]) // bool InfoHasSameExpr.
5183 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
5184 }
Sebastian Redlc3632732010-10-05 15:59:54 +00005185 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00005186 Record, Index));
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00005187}
5188
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005189Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall76bd1f32010-06-01 09:23:16 +00005190 return GetDecl(ID);
5191}
5192
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005193uint64_t ASTReader::readCXXBaseSpecifiers(ModuleFile &M, const RecordData &Record,
Douglas Gregore92b8a12011-08-04 00:01:48 +00005194 unsigned &Idx){
5195 if (Idx >= Record.size())
Douglas Gregor7c789c12010-10-29 22:39:52 +00005196 return 0;
Douglas Gregor7c789c12010-10-29 22:39:52 +00005197
Douglas Gregore92b8a12011-08-04 00:01:48 +00005198 unsigned LocalID = Record[Idx++];
5199 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
Douglas Gregor7c789c12010-10-29 22:39:52 +00005200}
5201
5202CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
Douglas Gregor8f1231b2011-07-22 06:10:01 +00005203 RecordLocation Loc = getLocalBitOffset(Offset);
5204 llvm::BitstreamCursor &Cursor = Loc.F->DeclsCursor;
Douglas Gregor7c789c12010-10-29 22:39:52 +00005205 SavedStreamPosition SavedPosition(Cursor);
Douglas Gregor8f1231b2011-07-22 06:10:01 +00005206 Cursor.JumpToBit(Loc.Offset);
Douglas Gregor7c789c12010-10-29 22:39:52 +00005207 ReadingKindTracker ReadingKind(Read_Decl, *this);
5208 RecordData Record;
5209 unsigned Code = Cursor.ReadCode();
5210 unsigned RecCode = Cursor.ReadRecord(Code, Record);
5211 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
5212 Error("Malformed AST file: missing C++ base specifiers");
5213 return 0;
5214 }
5215
5216 unsigned Idx = 0;
5217 unsigned NumBases = Record[Idx++];
Douglas Gregor35942772011-09-09 21:34:22 +00005218 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00005219 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
5220 for (unsigned I = 0; I != NumBases; ++I)
Douglas Gregor8f1231b2011-07-22 06:10:01 +00005221 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
Douglas Gregor7c789c12010-10-29 22:39:52 +00005222 return Bases;
5223}
5224
Douglas Gregor409448c2011-07-21 22:35:25 +00005225serialization::DeclID
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00005226ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00005227 if (LocalID < NUM_PREDEF_DECL_IDS)
Douglas Gregor496c7092011-08-03 15:48:04 +00005228 return LocalID;
5229
5230 ContinuousRangeMap<uint32_t, int, 2>::iterator I
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00005231 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
Douglas Gregor496c7092011-08-03 15:48:04 +00005232 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
5233
5234 return LocalID + I->second;
Douglas Gregor409448c2011-07-21 22:35:25 +00005235}
5236
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00005237bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005238 ModuleFile &M) const {
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00005239 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(ID);
5240 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5241 return &M == I->second;
5242}
5243
Douglas Gregorcff9f262012-01-27 01:47:08 +00005244ModuleFile *ASTReader::getOwningModuleFile(Decl *D) {
5245 if (!D->isFromASTFile())
5246 return 0;
5247 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
5248 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5249 return I->second;
5250}
5251
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00005252SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
5253 if (ID < NUM_PREDEF_DECL_IDS)
5254 return SourceLocation();
5255
5256 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
5257
5258 if (Index > DeclsLoaded.size()) {
5259 Error("declaration ID out-of-range for AST file");
5260 return SourceLocation();
5261 }
5262
5263 if (Decl *D = DeclsLoaded[Index])
5264 return D->getLocation();
5265
5266 unsigned RawLocation = 0;
5267 RecordLocation Rec = DeclCursorForID(ID, RawLocation);
5268 return ReadSourceLocation(*Rec.F, RawLocation);
5269}
5270
Sebastian Redl8538e8d2010-08-18 23:57:32 +00005271Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00005272 if (ID < NUM_PREDEF_DECL_IDS) {
5273 switch ((PredefinedDeclIDs)ID) {
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00005274 case PREDEF_DECL_NULL_ID:
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00005275 return 0;
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00005276
5277 case PREDEF_DECL_TRANSLATION_UNIT_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00005278 return Context.getTranslationUnitDecl();
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00005279
5280 case PREDEF_DECL_OBJC_ID_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00005281 return Context.getObjCIdDecl();
Douglas Gregor79d67262011-08-12 05:59:41 +00005282
Douglas Gregor7a27ea52011-08-12 06:17:30 +00005283 case PREDEF_DECL_OBJC_SEL_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00005284 return Context.getObjCSelDecl();
Douglas Gregor7a27ea52011-08-12 06:17:30 +00005285
Douglas Gregor79d67262011-08-12 05:59:41 +00005286 case PREDEF_DECL_OBJC_CLASS_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00005287 return Context.getObjCClassDecl();
Douglas Gregor772eeae2011-08-12 06:49:56 +00005288
Douglas Gregora6ea10e2012-01-17 18:09:05 +00005289 case PREDEF_DECL_OBJC_PROTOCOL_ID:
5290 return Context.getObjCProtocolDecl();
5291
Douglas Gregor772eeae2011-08-12 06:49:56 +00005292 case PREDEF_DECL_INT_128_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00005293 return Context.getInt128Decl();
Douglas Gregor772eeae2011-08-12 06:49:56 +00005294
5295 case PREDEF_DECL_UNSIGNED_INT_128_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00005296 return Context.getUInt128Decl();
Douglas Gregore97179c2011-09-08 01:46:34 +00005297
5298 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00005299 return Context.getObjCInstanceTypeDecl();
Meador Ingec5613b22012-06-16 03:34:49 +00005300
5301 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
5302 return Context.getBuiltinVaListDecl();
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00005303 }
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00005304 }
5305
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00005306 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
5307
Richard Smith2fbf3732011-12-20 04:39:57 +00005308 if (Index >= DeclsLoaded.size()) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00005309 assert(0 && "declaration ID out-of-range for AST file");
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005310 Error("declaration ID out-of-range for AST file");
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00005311 return 0;
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005312 }
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00005313
Douglas Gregorfd002a72011-12-16 22:37:11 +00005314 if (!DeclsLoaded[Index]) {
Douglas Gregor496c7092011-08-03 15:48:04 +00005315 ReadDeclRecord(ID);
Sebastian Redl30c514c2010-07-14 23:45:08 +00005316 if (DeserializationListener)
5317 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
5318 }
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005319
5320 return DeclsLoaded[Index];
Douglas Gregor2cf26342009-04-09 22:27:44 +00005321}
5322
Douglas Gregora1be2782011-12-17 23:38:30 +00005323DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
5324 DeclID GlobalID) {
5325 if (GlobalID < NUM_PREDEF_DECL_IDS)
5326 return GlobalID;
5327
5328 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
5329 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5330 ModuleFile *Owner = I->second;
5331
5332 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
5333 = M.GlobalToLocalDeclIDs.find(Owner);
5334 if (Pos == M.GlobalToLocalDeclIDs.end())
5335 return 0;
5336
5337 return GlobalID - Owner->BaseDeclID + Pos->second;
5338}
5339
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005340serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00005341 const RecordData &Record,
5342 unsigned &Idx) {
5343 if (Idx >= Record.size()) {
5344 Error("Corrupted AST file");
5345 return 0;
5346 }
5347
5348 return getGlobalDeclID(F, Record[Idx++]);
5349}
5350
Chris Lattner887e2b32009-04-27 05:46:25 +00005351/// \brief Resolve the offset of a statement into a statement.
5352///
5353/// This operation will read a new statement from the external
5354/// source each time it is called, and is meant to be used via a
5355/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005356Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00005357 // Switch case IDs are per Decl.
5358 ClearSwitchCaseIDs();
5359
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00005360 // Offset here is a global offset across the entire chain.
Douglas Gregor8f1231b2011-07-22 06:10:01 +00005361 RecordLocation Loc = getLocalBitOffset(Offset);
5362 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
5363 return ReadStmtFromStream(*Loc.F);
Douglas Gregor250fc9c2009-04-18 00:07:54 +00005364}
5365
Douglas Gregor851c75a2011-08-24 21:27:34 +00005366namespace {
5367 class FindExternalLexicalDeclsVisitor {
5368 ASTReader &Reader;
5369 const DeclContext *DC;
5370 bool (*isKindWeWant)(Decl::Kind);
Douglas Gregor2ea054f2011-08-26 22:04:51 +00005371
Douglas Gregor851c75a2011-08-24 21:27:34 +00005372 SmallVectorImpl<Decl*> &Decls;
5373 bool PredefsVisited[NUM_PREDEF_DECL_IDS];
5374
5375 public:
5376 FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC,
5377 bool (*isKindWeWant)(Decl::Kind),
5378 SmallVectorImpl<Decl*> &Decls)
5379 : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls)
5380 {
5381 for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I)
5382 PredefsVisited[I] = false;
5383 }
5384
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005385 static bool visit(ModuleFile &M, bool Preorder, void *UserData) {
Douglas Gregor851c75a2011-08-24 21:27:34 +00005386 if (Preorder)
5387 return false;
5388
5389 FindExternalLexicalDeclsVisitor *This
5390 = static_cast<FindExternalLexicalDeclsVisitor *>(UserData);
5391
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005392 ModuleFile::DeclContextInfosMap::iterator Info
Douglas Gregor851c75a2011-08-24 21:27:34 +00005393 = M.DeclContextInfos.find(This->DC);
5394 if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls)
5395 return false;
5396
5397 // Load all of the declaration IDs
5398 for (const KindDeclIDPair *ID = Info->second.LexicalDecls,
5399 *IDE = ID + Info->second.NumLexicalDecls;
5400 ID != IDE; ++ID) {
5401 if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first))
5402 continue;
5403
5404 // Don't add predefined declarations to the lexical context more
5405 // than once.
5406 if (ID->second < NUM_PREDEF_DECL_IDS) {
5407 if (This->PredefsVisited[ID->second])
5408 continue;
5409
5410 This->PredefsVisited[ID->second] = true;
5411 }
5412
Douglas Gregor2ea054f2011-08-26 22:04:51 +00005413 if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) {
5414 if (!This->DC->isDeclInLexicalTraversal(D))
5415 This->Decls.push_back(D);
5416 }
Douglas Gregor851c75a2011-08-24 21:27:34 +00005417 }
5418
5419 return false;
5420 }
5421 };
5422}
5423
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00005424ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00005425 bool (*isKindWeWant)(Decl::Kind),
Chris Lattner5f9e2722011-07-23 10:55:15 +00005426 SmallVectorImpl<Decl*> &Decls) {
Douglas Gregor0d95f772011-08-24 19:03:07 +00005427 // There might be lexical decls in multiple modules, for the TU at
Douglas Gregor851c75a2011-08-24 21:27:34 +00005428 // least. Walk all of the modules in the order they were loaded.
5429 FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls);
5430 ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor);
Douglas Gregor25123082009-04-22 22:34:57 +00005431 ++NumLexicalDeclContextsRead;
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00005432 return ELR_Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00005433}
5434
Douglas Gregor0d95f772011-08-24 19:03:07 +00005435namespace {
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00005436
5437class DeclIDComp {
5438 ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005439 ModuleFile &Mod;
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00005440
5441public:
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005442 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00005443
5444 bool operator()(LocalDeclID L, LocalDeclID R) const {
5445 SourceLocation LHS = getLocation(L);
5446 SourceLocation RHS = getLocation(R);
5447 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5448 }
5449
5450 bool operator()(SourceLocation LHS, LocalDeclID R) const {
5451 SourceLocation RHS = getLocation(R);
5452 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5453 }
5454
5455 bool operator()(LocalDeclID L, SourceLocation RHS) const {
5456 SourceLocation LHS = getLocation(L);
5457 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5458 }
5459
5460 SourceLocation getLocation(LocalDeclID ID) const {
5461 return Reader.getSourceManager().getFileLoc(
5462 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
5463 }
5464};
5465
5466}
5467
5468void ASTReader::FindFileRegionDecls(FileID File,
5469 unsigned Offset, unsigned Length,
5470 SmallVectorImpl<Decl *> &Decls) {
5471 SourceManager &SM = getSourceManager();
5472
5473 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
5474 if (I == FileDeclIDs.end())
5475 return;
5476
5477 FileDeclsInfo &DInfo = I->second;
5478 if (DInfo.Decls.empty())
5479 return;
5480
5481 SourceLocation
5482 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
5483 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
5484
5485 DeclIDComp DIDComp(*this, *DInfo.Mod);
5486 ArrayRef<serialization::LocalDeclID>::iterator
5487 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
5488 BeginLoc, DIDComp);
5489 if (BeginIt != DInfo.Decls.begin())
5490 --BeginIt;
5491
Argyrios Kyrtzidisc14a03d2011-11-23 20:27:36 +00005492 // If we are pointing at a top-level decl inside an objc container, we need
5493 // to backtrack until we find it otherwise we will fail to report that the
5494 // region overlaps with an objc container.
5495 while (BeginIt != DInfo.Decls.begin() &&
5496 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
5497 ->isTopLevelDeclInObjCContainer())
5498 --BeginIt;
5499
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00005500 ArrayRef<serialization::LocalDeclID>::iterator
5501 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
5502 EndLoc, DIDComp);
5503 if (EndIt != DInfo.Decls.end())
5504 ++EndIt;
5505
5506 for (ArrayRef<serialization::LocalDeclID>::iterator
5507 DIt = BeginIt; DIt != EndIt; ++DIt)
5508 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
5509}
5510
5511namespace {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005512 /// \brief ModuleFile visitor used to perform name lookup into a
Douglas Gregor0d95f772011-08-24 19:03:07 +00005513 /// declaration context.
5514 class DeclContextNameLookupVisitor {
5515 ASTReader &Reader;
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005516 llvm::SmallVectorImpl<const DeclContext *> &Contexts;
Douglas Gregor0d95f772011-08-24 19:03:07 +00005517 DeclarationName Name;
5518 SmallVectorImpl<NamedDecl *> &Decls;
5519
5520 public:
5521 DeclContextNameLookupVisitor(ASTReader &Reader,
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005522 SmallVectorImpl<const DeclContext *> &Contexts,
5523 DeclarationName Name,
Douglas Gregor0d95f772011-08-24 19:03:07 +00005524 SmallVectorImpl<NamedDecl *> &Decls)
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005525 : Reader(Reader), Contexts(Contexts), Name(Name), Decls(Decls) { }
Douglas Gregor0d95f772011-08-24 19:03:07 +00005526
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005527 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregor0d95f772011-08-24 19:03:07 +00005528 DeclContextNameLookupVisitor *This
5529 = static_cast<DeclContextNameLookupVisitor *>(UserData);
5530
5531 // Check whether we have any visible declaration information for
5532 // this context in this module.
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005533 ModuleFile::DeclContextInfosMap::iterator Info;
5534 bool FoundInfo = false;
5535 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5536 Info = M.DeclContextInfos.find(This->Contexts[I]);
5537 if (Info != M.DeclContextInfos.end() &&
5538 Info->second.NameLookupTableData) {
5539 FoundInfo = true;
5540 break;
5541 }
5542 }
Douglas Gregor0d95f772011-08-24 19:03:07 +00005543
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005544 if (!FoundInfo)
5545 return false;
5546
Douglas Gregor0d95f772011-08-24 19:03:07 +00005547 // Look for this name within this module.
5548 ASTDeclContextNameLookupTable *LookupTable =
Benjamin Kramerb1758c62012-04-15 12:36:49 +00005549 Info->second.NameLookupTableData;
Douglas Gregor0d95f772011-08-24 19:03:07 +00005550 ASTDeclContextNameLookupTable::iterator Pos
5551 = LookupTable->find(This->Name);
5552 if (Pos == LookupTable->end())
5553 return false;
5554
5555 bool FoundAnything = false;
5556 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
5557 for (; Data.first != Data.second; ++Data.first) {
5558 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first);
5559 if (!ND)
5560 continue;
5561
5562 if (ND->getDeclName() != This->Name) {
Axel Naumann3dd82f72012-10-01 09:51:27 +00005563 // A name might be null because the decl's redeclarable part is
5564 // currently read before reading its name. The lookup is triggered by
5565 // building that decl (likely indirectly), and so it is later in the
5566 // sense of "already existing" and can be ignored here.
Douglas Gregor0d95f772011-08-24 19:03:07 +00005567 continue;
5568 }
5569
5570 // Record this declaration.
5571 FoundAnything = true;
5572 This->Decls.push_back(ND);
5573 }
5574
5575 return FoundAnything;
5576 }
5577 };
5578}
5579
John McCall76bd1f32010-06-01 09:23:16 +00005580DeclContext::lookup_result
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005581ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +00005582 DeclarationName Name) {
Mike Stump1eb44332009-09-09 15:08:12 +00005583 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00005584 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00005585 if (!Name)
5586 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
5587 DeclContext::lookup_iterator(0));
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00005588
Chris Lattner5f9e2722011-07-23 10:55:15 +00005589 SmallVector<NamedDecl *, 64> Decls;
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005590
5591 // Compute the declaration contexts we need to look into. Multiple such
5592 // declaration contexts occur when two declaration contexts from disjoint
5593 // modules get merged, e.g., when two namespaces with the same name are
5594 // independently defined in separate modules.
5595 SmallVector<const DeclContext *, 2> Contexts;
5596 Contexts.push_back(DC);
5597
5598 if (DC->isNamespace()) {
5599 MergedDeclsMap::iterator Merged
5600 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5601 if (Merged != MergedDecls.end()) {
5602 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5603 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5604 }
5605 }
5606
5607 DeclContextNameLookupVisitor Visitor(*this, Contexts, Name, Decls);
Douglas Gregor0d95f772011-08-24 19:03:07 +00005608 ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor);
Douglas Gregor25123082009-04-22 22:34:57 +00005609 ++NumVisibleDeclContextsRead;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00005610 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall76bd1f32010-06-01 09:23:16 +00005611 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregor2cf26342009-04-09 22:27:44 +00005612}
5613
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005614namespace {
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005615 /// \brief ModuleFile visitor used to retrieve all visible names in a
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005616 /// declaration context.
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005617 class DeclContextAllNamesVisitor {
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005618 ASTReader &Reader;
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005619 llvm::SmallVectorImpl<const DeclContext *> &Contexts;
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005620 llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> > &Decls;
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005621
5622 public:
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005623 DeclContextAllNamesVisitor(ASTReader &Reader,
5624 SmallVectorImpl<const DeclContext *> &Contexts,
5625 llvm::DenseMap<DeclarationName,
5626 SmallVector<NamedDecl *, 8> > &Decls)
5627 : Reader(Reader), Contexts(Contexts), Decls(Decls) { }
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005628
5629 static bool visit(ModuleFile &M, void *UserData) {
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005630 DeclContextAllNamesVisitor *This
5631 = static_cast<DeclContextAllNamesVisitor *>(UserData);
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005632
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005633 // Check whether we have any visible declaration information for
5634 // this context in this module.
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005635 ModuleFile::DeclContextInfosMap::iterator Info;
5636 bool FoundInfo = false;
5637 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5638 Info = M.DeclContextInfos.find(This->Contexts[I]);
5639 if (Info != M.DeclContextInfos.end() &&
5640 Info->second.NameLookupTableData) {
5641 FoundInfo = true;
5642 break;
5643 }
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005644 }
5645
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005646 if (!FoundInfo)
5647 return false;
5648
5649 ASTDeclContextNameLookupTable *LookupTable =
5650 Info->second.NameLookupTableData;
5651 bool FoundAnything = false;
5652 for (ASTDeclContextNameLookupTable::data_iterator
5653 I = LookupTable->data_begin(), E = LookupTable->data_end();
5654 I != E; ++I) {
5655 ASTDeclContextNameLookupTrait::data_type Data = *I;
5656 for (; Data.first != Data.second; ++Data.first) {
5657 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M,
5658 *Data.first);
5659 if (!ND)
5660 continue;
5661
5662 // Record this declaration.
5663 FoundAnything = true;
5664 This->Decls[ND->getDeclName()].push_back(ND);
5665 }
5666 }
5667
5668 return FoundAnything;
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005669 }
5670 };
5671}
5672
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005673void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005674 if (!DC->hasExternalVisibleStorage())
5675 return;
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005676 llvm::DenseMap<DeclarationName, llvm::SmallVector<NamedDecl*, 8> > Decls;
5677
5678 // Compute the declaration contexts we need to look into. Multiple such
5679 // declaration contexts occur when two declaration contexts from disjoint
5680 // modules get merged, e.g., when two namespaces with the same name are
5681 // independently defined in separate modules.
5682 SmallVector<const DeclContext *, 2> Contexts;
5683 Contexts.push_back(DC);
5684
5685 if (DC->isNamespace()) {
5686 MergedDeclsMap::iterator Merged
5687 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5688 if (Merged != MergedDecls.end()) {
5689 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5690 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5691 }
5692 }
5693
5694 DeclContextAllNamesVisitor Visitor(*this, Contexts, Decls);
5695 ModuleMgr.visit(&DeclContextAllNamesVisitor::visit, &Visitor);
5696 ++NumVisibleDeclContextsRead;
5697
5698 for (llvm::DenseMap<DeclarationName,
5699 llvm::SmallVector<NamedDecl*, 8> >::iterator
5700 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
5701 SetExternalVisibleDeclsForName(DC, I->first, I->second);
5702 }
Argyrios Kyrtzidis394e5392012-04-26 18:34:14 +00005703 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005704}
5705
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005706/// \brief Under non-PCH compilation the consumer receives the objc methods
5707/// before receiving the implementation, and codegen depends on this.
5708/// We simulate this by deserializing and passing to consumer the methods of the
5709/// implementation before passing the deserialized implementation decl.
5710static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
5711 ASTConsumer *Consumer) {
5712 assert(ImplD && Consumer);
5713
5714 for (ObjCImplDecl::method_iterator
5715 I = ImplD->meth_begin(), E = ImplD->meth_end(); I != E; ++I)
David Blaikie581deb32012-06-06 20:45:41 +00005716 Consumer->HandleInterestingDecl(DeclGroupRef(*I));
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005717
5718 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
5719}
5720
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005721void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005722 assert(Consumer);
5723 while (!InterestingDecls.empty()) {
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005724 Decl *D = InterestingDecls.front();
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005725 InterestingDecls.pop_front();
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005726
Argyrios Kyrtzidis8d39c3d2011-11-30 23:18:26 +00005727 PassInterestingDeclToConsumer(D);
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005728 }
5729}
5730
Argyrios Kyrtzidis8d39c3d2011-11-30 23:18:26 +00005731void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
5732 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
5733 PassObjCImplDeclToConsumer(ImplD, Consumer);
5734 else
5735 Consumer->HandleInterestingDecl(DeclGroupRef(D));
5736}
5737
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005738void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor0af2ca42009-04-22 19:09:20 +00005739 this->Consumer = Consumer;
5740
Douglas Gregorfdd01722009-04-14 00:24:19 +00005741 if (!Consumer)
5742 return;
5743
5744 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005745 // Force deserialization of this decl, which will cause it to be queued for
5746 // passing to the consumer.
Daniel Dunbar04a0b502009-09-17 03:06:44 +00005747 GetDecl(ExternalDefinitions[I]);
Douglas Gregorfdd01722009-04-14 00:24:19 +00005748 }
Douglas Gregor1a995dd2011-09-15 18:47:32 +00005749 ExternalDefinitions.clear();
Douglas Gregorc62a2fe2009-04-25 00:41:30 +00005750
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005751 PassInterestingDeclsToConsumer();
Douglas Gregorfdd01722009-04-14 00:24:19 +00005752}
5753
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005754void ASTReader::PrintStats() {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005755 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregor2cf26342009-04-09 22:27:44 +00005756
Mike Stump1eb44332009-09-09 15:08:12 +00005757 unsigned NumTypesLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005758 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall0953e762009-09-24 19:53:00 +00005759 QualType());
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005760 unsigned NumDeclsLoaded
5761 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
5762 (Decl *)0);
5763 unsigned NumIdentifiersLoaded
5764 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
5765 IdentifiersLoaded.end(),
5766 (IdentifierInfo *)0);
Douglas Gregora8235d62012-10-09 23:05:51 +00005767 unsigned NumMacrosLoaded
5768 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
5769 MacrosLoaded.end(),
5770 (MacroInfo *)0);
Mike Stump1eb44332009-09-09 15:08:12 +00005771 unsigned NumSelectorsLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005772 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
5773 SelectorsLoaded.end(),
5774 Selector());
Douglas Gregor2d41cc12009-04-13 20:50:16 +00005775
Douglas Gregor4fed3f42009-04-27 18:38:38 +00005776 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
5777 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor0cdd7982011-07-21 18:46:38 +00005778 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00005779 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
5780 NumSLocEntriesRead, TotalNumSLocEntries,
5781 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005782 if (!TypesLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005783 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005784 NumTypesLoaded, (unsigned)TypesLoaded.size(),
5785 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
5786 if (!DeclsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005787 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005788 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
5789 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005790 if (!IdentifiersLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005791 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005792 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
5793 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Douglas Gregora8235d62012-10-09 23:05:51 +00005794 if (!MacrosLoaded.empty())
5795 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
5796 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
5797 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
Sebastian Redl725cd962010-08-04 20:40:17 +00005798 if (!SelectorsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005799 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redl725cd962010-08-04 20:40:17 +00005800 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
5801 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor83941df2009-04-25 17:48:32 +00005802 if (TotalNumStatements)
5803 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
5804 NumStatementsRead, TotalNumStatements,
5805 ((float)NumStatementsRead/TotalNumStatements * 100));
5806 if (TotalNumMacros)
5807 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
5808 NumMacrosRead, TotalNumMacros,
5809 ((float)NumMacrosRead/TotalNumMacros * 100));
5810 if (TotalLexicalDeclContexts)
5811 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
5812 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
5813 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
5814 * 100));
5815 if (TotalVisibleDeclContexts)
5816 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
5817 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
5818 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
5819 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005820 if (TotalNumMethodPoolEntries) {
Douglas Gregor83941df2009-04-25 17:48:32 +00005821 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005822 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
5823 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor83941df2009-04-25 17:48:32 +00005824 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005825 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor83941df2009-04-25 17:48:32 +00005826 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00005827 std::fprintf(stderr, "\n");
Douglas Gregor23d7df52011-07-21 19:50:14 +00005828 dump();
5829 std::fprintf(stderr, "\n");
5830}
5831
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005832template<typename Key, typename ModuleFile, unsigned InitialCapacity>
Douglas Gregor23d7df52011-07-21 19:50:14 +00005833static void
Chris Lattner5f9e2722011-07-23 10:55:15 +00005834dumpModuleIDMap(StringRef Name,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005835 const ContinuousRangeMap<Key, ModuleFile *,
Douglas Gregor23d7df52011-07-21 19:50:14 +00005836 InitialCapacity> &Map) {
5837 if (Map.begin() == Map.end())
5838 return;
5839
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005840 typedef ContinuousRangeMap<Key, ModuleFile *, InitialCapacity> MapType;
Douglas Gregor23d7df52011-07-21 19:50:14 +00005841 llvm::errs() << Name << ":\n";
5842 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
5843 I != IEnd; ++I) {
5844 llvm::errs() << " " << I->first << " -> " << I->second->FileName
5845 << "\n";
5846 }
5847}
5848
Douglas Gregor23d7df52011-07-21 19:50:14 +00005849void ASTReader::dump() {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005850 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
Douglas Gregor8f1231b2011-07-22 06:10:01 +00005851 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
Douglas Gregor23d7df52011-07-21 19:50:14 +00005852 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
Douglas Gregor1e849b62011-07-29 00:21:44 +00005853 dumpModuleIDMap("Global type map", GlobalTypeMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00005854 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00005855 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
Douglas Gregora8235d62012-10-09 23:05:51 +00005856 dumpModuleIDMap("Global macro map", GlobalMacroMap);
Douglas Gregor26ced122011-12-01 00:59:36 +00005857 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00005858 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00005859 dumpModuleIDMap("Global preprocessed entity map",
5860 GlobalPreprocessedEntityMap);
Douglas Gregor8df5c9b2011-08-02 11:12:41 +00005861
5862 llvm::errs() << "\n*** PCH/Modules Loaded:";
5863 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
5864 MEnd = ModuleMgr.end();
5865 M != MEnd; ++M)
5866 (*M)->dump();
Douglas Gregor2cf26342009-04-09 22:27:44 +00005867}
5868
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00005869/// Return the amount of memory used by memory buffers, breaking down
5870/// by heap-backed versus mmap'ed memory.
5871void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005872 for (ModuleConstIterator I = ModuleMgr.begin(),
5873 E = ModuleMgr.end(); I != E; ++I) {
5874 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00005875 size_t bytes = buf->getBufferSize();
5876 switch (buf->getBufferKind()) {
5877 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
5878 sizes.malloc_bytes += bytes;
5879 break;
5880 case llvm::MemoryBuffer::MemoryBuffer_MMap:
5881 sizes.mmap_bytes += bytes;
5882 break;
5883 }
5884 }
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005885 }
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00005886}
5887
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005888void ASTReader::InitializeSema(Sema &S) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00005889 SemaObj = &S;
Axel Naumann0ec56b72012-10-18 19:05:02 +00005890 S.addExternalSource(this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00005891
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00005892 // Makes sure any declarations that were deserialized "too early"
5893 // still get added to the identifier's declaration chains.
Douglas Gregor76dc8892010-09-24 23:29:12 +00005894 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00005895 SemaObj->pushExternalDeclIntoScope(PreloadedDecls[I],
5896 PreloadedDecls[I]->getDeclName());
Douglas Gregor668c1a42009-04-21 22:25:48 +00005897 }
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00005898 PreloadedDecls.clear();
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00005899
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00005900 // Load the offsets of the declarations that Sema references.
5901 // They will be lazily deserialized when needed.
5902 if (!SemaDeclRefs.empty()) {
5903 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
Douglas Gregor1e5b6f62011-07-28 00:57:24 +00005904 if (!SemaObj->StdNamespace)
5905 SemaObj->StdNamespace = SemaDeclRefs[0];
5906 if (!SemaObj->StdBadAlloc)
5907 SemaObj->StdBadAlloc = SemaDeclRefs[1];
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00005908 }
5909
Peter Collingbourne84bccea2011-02-15 19:46:30 +00005910 if (!FPPragmaOptions.empty()) {
5911 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
5912 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
5913 }
5914
5915 if (!OpenCLExtensions.empty()) {
5916 unsigned I = 0;
5917#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
5918#include "clang/Basic/OpenCLExtensions.def"
5919
5920 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
5921 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00005922}
5923
Douglas Gregor211f6e82011-08-20 04:39:52 +00005924IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00005925 // Note that we are loading an identifier.
5926 Deserializing AnIdentifier(this);
5927
Douglas Gregor057df202012-01-18 20:56:22 +00005928 IdentifierLookupVisitor Visitor(StringRef(NameStart, NameEnd - NameStart),
5929 /*PriorGeneration=*/0);
Douglas Gregor211f6e82011-08-20 04:39:52 +00005930 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
Douglas Gregoreee242f2011-10-27 09:33:13 +00005931 IdentifierInfo *II = Visitor.getIdentifierInfo();
Douglas Gregor057df202012-01-18 20:56:22 +00005932 markIdentifierUpToDate(II);
Douglas Gregoreee242f2011-10-27 09:33:13 +00005933 return II;
Douglas Gregor668c1a42009-04-21 22:25:48 +00005934}
5935
Douglas Gregor95f42922010-10-14 22:11:03 +00005936namespace clang {
5937 /// \brief An identifier-lookup iterator that enumerates all of the
5938 /// identifiers stored within a set of AST files.
5939 class ASTIdentifierIterator : public IdentifierIterator {
5940 /// \brief The AST reader whose identifiers are being enumerated.
5941 const ASTReader &Reader;
5942
5943 /// \brief The current index into the chain of AST files stored in
5944 /// the AST reader.
5945 unsigned Index;
5946
5947 /// \brief The current position within the identifier lookup table
5948 /// of the current AST file.
5949 ASTIdentifierLookupTable::key_iterator Current;
5950
5951 /// \brief The end position within the identifier lookup table of
5952 /// the current AST file.
5953 ASTIdentifierLookupTable::key_iterator End;
5954
5955 public:
5956 explicit ASTIdentifierIterator(const ASTReader &Reader);
5957
Chris Lattner5f9e2722011-07-23 10:55:15 +00005958 virtual StringRef Next();
Douglas Gregor95f42922010-10-14 22:11:03 +00005959 };
5960}
5961
5962ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005963 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
Douglas Gregor95f42922010-10-14 22:11:03 +00005964 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005965 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
Douglas Gregor95f42922010-10-14 22:11:03 +00005966 Current = IdTable->key_begin();
5967 End = IdTable->key_end();
5968}
5969
Chris Lattner5f9e2722011-07-23 10:55:15 +00005970StringRef ASTIdentifierIterator::Next() {
Douglas Gregor95f42922010-10-14 22:11:03 +00005971 while (Current == End) {
5972 // If we have exhausted all of our AST files, we're done.
5973 if (Index == 0)
Chris Lattner5f9e2722011-07-23 10:55:15 +00005974 return StringRef();
Douglas Gregor95f42922010-10-14 22:11:03 +00005975
5976 --Index;
5977 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005978 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
5979 IdentifierLookupTable;
Douglas Gregor95f42922010-10-14 22:11:03 +00005980 Current = IdTable->key_begin();
5981 End = IdTable->key_end();
5982 }
5983
5984 // We have any identifiers remaining in the current AST file; return
5985 // the next one.
5986 std::pair<const char*, unsigned> Key = *Current;
5987 ++Current;
Chris Lattner5f9e2722011-07-23 10:55:15 +00005988 return StringRef(Key.first, Key.second);
Douglas Gregor95f42922010-10-14 22:11:03 +00005989}
5990
5991IdentifierIterator *ASTReader::getIdentifiers() const {
5992 return new ASTIdentifierIterator(*this);
5993}
5994
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005995namespace clang { namespace serialization {
5996 class ReadMethodPoolVisitor {
5997 ASTReader &Reader;
Douglas Gregor8efca6b2012-01-25 01:14:32 +00005998 Selector Sel;
5999 unsigned PriorGeneration;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00006000 llvm::SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
6001 llvm::SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00006002
Douglas Gregor3d15ab82011-08-25 14:51:20 +00006003 public:
Douglas Gregor8efca6b2012-01-25 01:14:32 +00006004 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
6005 unsigned PriorGeneration)
6006 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) { }
Douglas Gregor3d15ab82011-08-25 14:51:20 +00006007
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006008 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregor3d15ab82011-08-25 14:51:20 +00006009 ReadMethodPoolVisitor *This
6010 = static_cast<ReadMethodPoolVisitor *>(UserData);
6011
6012 if (!M.SelectorLookupTable)
6013 return false;
6014
Douglas Gregor8efca6b2012-01-25 01:14:32 +00006015 // If we've already searched this module file, skip it now.
6016 if (M.Generation <= This->PriorGeneration)
6017 return true;
6018
Douglas Gregor3d15ab82011-08-25 14:51:20 +00006019 ASTSelectorLookupTable *PoolTable
6020 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
6021 ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel);
6022 if (Pos == PoolTable->end())
6023 return false;
6024
6025 ++This->Reader.NumSelectorsRead;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00006026 // FIXME: Not quite happy with the statistics here. We probably should
6027 // disable this tracking when called via LoadSelector.
6028 // Also, should entries without methods count as misses?
Douglas Gregor3d15ab82011-08-25 14:51:20 +00006029 ++This->Reader.NumMethodPoolEntriesRead;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00006030 ASTSelectorLookupTrait::data_type Data = *Pos;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00006031 if (This->Reader.DeserializationListener)
6032 This->Reader.DeserializationListener->SelectorRead(Data.ID,
6033 This->Sel);
6034
6035 This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
6036 This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
6037 return true;
Sebastian Redl725cd962010-08-04 20:40:17 +00006038 }
Douglas Gregor3d15ab82011-08-25 14:51:20 +00006039
6040 /// \brief Retrieve the instance methods found by this visitor.
Douglas Gregor5ac4b692012-01-25 00:49:42 +00006041 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
6042 return InstanceMethods;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00006043 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00006044
Douglas Gregor3d15ab82011-08-25 14:51:20 +00006045 /// \brief Retrieve the instance methods found by this visitor.
Douglas Gregor5ac4b692012-01-25 00:49:42 +00006046 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
6047 return FactoryMethods;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00006048 }
6049 };
6050} } // end namespace clang::serialization
6051
Douglas Gregor5ac4b692012-01-25 00:49:42 +00006052/// \brief Add the given set of methods to the method list.
6053static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
6054 ObjCMethodList &List) {
6055 for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
6056 S.addMethodToGlobalList(&List, Methods[I]);
6057 }
6058}
6059
6060void ASTReader::ReadMethodPool(Selector Sel) {
Douglas Gregor8efca6b2012-01-25 01:14:32 +00006061 // Get the selector generation and update it to the current generation.
6062 unsigned &Generation = SelectorGeneration[Sel];
6063 unsigned PriorGeneration = Generation;
6064 Generation = CurrentGeneration;
6065
6066 // Search for methods defined with this selector.
6067 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
Douglas Gregor3d15ab82011-08-25 14:51:20 +00006068 ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor);
Douglas Gregor3d15ab82011-08-25 14:51:20 +00006069
Douglas Gregor5ac4b692012-01-25 00:49:42 +00006070 if (Visitor.getInstanceMethods().empty() &&
6071 Visitor.getFactoryMethods().empty()) {
Douglas Gregor3d15ab82011-08-25 14:51:20 +00006072 ++NumMethodPoolMisses;
Douglas Gregor5ac4b692012-01-25 00:49:42 +00006073 return;
6074 }
6075
6076 if (!getSema())
6077 return;
6078
6079 Sema &S = *getSema();
6080 Sema::GlobalMethodPool::iterator Pos
6081 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
6082
6083 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
6084 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00006085}
6086
Douglas Gregord8bba9c2011-06-28 16:20:02 +00006087void ASTReader::ReadKnownNamespaces(
Chris Lattner5f9e2722011-07-23 10:55:15 +00006088 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
Douglas Gregord8bba9c2011-06-28 16:20:02 +00006089 Namespaces.clear();
6090
6091 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
6092 if (NamespaceDecl *Namespace
6093 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
6094 Namespaces.push_back(Namespace);
6095 }
6096}
6097
Douglas Gregora8623202011-07-27 20:58:46 +00006098void ASTReader::ReadTentativeDefinitions(
6099 SmallVectorImpl<VarDecl *> &TentativeDefs) {
6100 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
6101 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
6102 if (Var)
6103 TentativeDefs.push_back(Var);
6104 }
6105 TentativeDefinitions.clear();
6106}
6107
Douglas Gregora2ee20a2011-07-27 21:45:57 +00006108void ASTReader::ReadUnusedFileScopedDecls(
6109 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
6110 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
6111 DeclaratorDecl *D
6112 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
6113 if (D)
6114 Decls.push_back(D);
6115 }
6116 UnusedFileScopedDecls.clear();
6117}
6118
Douglas Gregor0129b562011-07-27 21:57:17 +00006119void ASTReader::ReadDelegatingConstructors(
6120 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
6121 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
6122 CXXConstructorDecl *D
6123 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
6124 if (D)
6125 Decls.push_back(D);
6126 }
6127 DelegatingCtorDecls.clear();
6128}
6129
Douglas Gregord58a0a52011-07-28 00:39:29 +00006130void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
6131 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
6132 TypedefNameDecl *D
6133 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
6134 if (D)
6135 Decls.push_back(D);
6136 }
6137 ExtVectorDecls.clear();
6138}
6139
Douglas Gregora126f172011-07-28 00:53:40 +00006140void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) {
6141 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
6142 CXXRecordDecl *D
6143 = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I]));
6144 if (D)
6145 Decls.push_back(D);
6146 }
6147 DynamicClasses.clear();
6148}
6149
Douglas Gregorec12ce22011-07-28 14:20:37 +00006150void
6151ASTReader::ReadLocallyScopedExternalDecls(SmallVectorImpl<NamedDecl *> &Decls) {
6152 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
6153 NamedDecl *D
6154 = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
6155 if (D)
6156 Decls.push_back(D);
6157 }
6158 LocallyScopedExternalDecls.clear();
6159}
6160
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00006161void ASTReader::ReadReferencedSelectors(
6162 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
6163 if (ReferencedSelectorsData.empty())
6164 return;
6165
6166 // If there are @selector references added them to its pool. This is for
6167 // implementation of -Wselector.
6168 unsigned int DataSize = ReferencedSelectorsData.size()-1;
6169 unsigned I = 0;
6170 while (I < DataSize) {
6171 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
6172 SourceLocation SelLoc
6173 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
6174 Sels.push_back(std::make_pair(Sel, SelLoc));
6175 }
6176 ReferencedSelectorsData.clear();
6177}
6178
Douglas Gregor31e37b22011-07-28 18:09:57 +00006179void ASTReader::ReadWeakUndeclaredIdentifiers(
6180 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
6181 if (WeakUndeclaredIdentifiers.empty())
6182 return;
6183
6184 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
6185 IdentifierInfo *WeakId
6186 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
6187 IdentifierInfo *AliasId
6188 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
6189 SourceLocation Loc
6190 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
6191 bool Used = WeakUndeclaredIdentifiers[I++];
6192 WeakInfo WI(AliasId, Loc);
6193 WI.setUsed(Used);
6194 WeakIDs.push_back(std::make_pair(WeakId, WI));
6195 }
6196 WeakUndeclaredIdentifiers.clear();
6197}
6198
Douglas Gregordfe65432011-07-28 19:11:31 +00006199void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
6200 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
6201 ExternalVTableUse VT;
6202 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
6203 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
6204 VT.DefinitionRequired = VTableUses[Idx++];
6205 VTables.push_back(VT);
6206 }
6207
6208 VTableUses.clear();
6209}
6210
Douglas Gregor6e4a3f52011-07-28 19:49:54 +00006211void ASTReader::ReadPendingInstantiations(
6212 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
6213 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
6214 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
6215 SourceLocation Loc
6216 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
Axel Naumann39d26c32012-10-02 09:09:43 +00006217
Douglas Gregore5fa3c22012-10-03 18:34:48 +00006218 Pending.push_back(std::make_pair(D, Loc));
Douglas Gregor6e4a3f52011-07-28 19:49:54 +00006219 }
6220 PendingInstantiations.clear();
6221}
6222
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006223void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redle58aa892010-08-04 18:21:41 +00006224 // It would be complicated to avoid reading the methods anyway. So don't.
6225 ReadMethodPool(Sel);
6226}
6227
Douglas Gregor95eab172011-07-28 20:55:49 +00006228void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00006229 assert(ID && "Non-zero identifier ID required");
Douglas Gregora02b1472009-04-28 21:53:25 +00006230 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00006231 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00006232 if (DeserializationListener)
6233 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregor668c1a42009-04-21 22:25:48 +00006234}
6235
Douglas Gregord89275b2009-07-06 18:54:52 +00006236/// \brief Set the globally-visible declarations associated with the given
6237/// identifier.
6238///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00006239/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump1eb44332009-09-09 15:08:12 +00006240/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregord89275b2009-07-06 18:54:52 +00006241/// them.
6242///
6243/// \param II an IdentifierInfo that refers to one or more globally-visible
6244/// declarations.
6245///
6246/// \param DeclIDs the set of declaration IDs with the name @p II that are
6247/// visible at global scope.
6248///
6249/// \param Nonrecursive should be true to indicate that the caller knows that
6250/// this call is non-recursive, and therefore the globally-visible declarations
6251/// will not be placed onto the pending queue.
Mike Stump1eb44332009-09-09 15:08:12 +00006252void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006253ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Chris Lattner5f9e2722011-07-23 10:55:15 +00006254 const SmallVectorImpl<uint32_t> &DeclIDs,
Douglas Gregord89275b2009-07-06 18:54:52 +00006255 bool Nonrecursive) {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00006256 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregord89275b2009-07-06 18:54:52 +00006257 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
6258 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
6259 PII.II = II;
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00006260 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregord89275b2009-07-06 18:54:52 +00006261 return;
6262 }
Mike Stump1eb44332009-09-09 15:08:12 +00006263
Douglas Gregord89275b2009-07-06 18:54:52 +00006264 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
6265 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
6266 if (SemaObj) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00006267 // Introduce this declaration into the translation-unit scope
6268 // and add it to the declaration chain for this identifier, so
6269 // that (unqualified) name lookup will find it.
6270 SemaObj->pushExternalDeclIntoScope(D, II);
Douglas Gregord89275b2009-07-06 18:54:52 +00006271 } else {
6272 // Queue this declaration so that it will be added to the
6273 // translation unit scope and identifier's declaration chain
6274 // once a Sema object is known.
6275 PreloadedDecls.push_back(D);
6276 }
6277 }
6278}
6279
Douglas Gregor95eab172011-07-28 20:55:49 +00006280IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00006281 if (ID == 0)
6282 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00006283
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00006284 if (IdentifiersLoaded.empty()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00006285 Error("no identifier table in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00006286 return 0;
6287 }
Mike Stump1eb44332009-09-09 15:08:12 +00006288
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00006289 ID -= 1;
6290 if (!IdentifiersLoaded[ID]) {
Douglas Gregor67268d02011-07-20 00:59:32 +00006291 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
6292 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006293 ModuleFile *M = I->second;
Douglas Gregor9827a802011-07-29 00:56:45 +00006294 unsigned Index = ID - M->BaseIdentifierID;
6295 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
Douglas Gregord6595a42009-04-25 21:04:17 +00006296
Sebastian Redl3c7f4132010-08-18 23:57:06 +00006297 // All of the strings in the AST file are preceded by a 16-bit length.
6298 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenek231bc0b2009-10-23 04:45:31 +00006299 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
6300 // unsigned integers. This is important to avoid integer overflow when
6301 // we cast them to 'unsigned'.
Ted Kremenekff1ea462009-10-23 03:57:22 +00006302 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregor02fc7512009-04-28 20:01:51 +00006303 unsigned StrLen = (((unsigned) StrLenPtr[0])
6304 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00006305 IdentifiersLoaded[ID]
Douglas Gregor712f2fc2011-09-09 22:02:16 +00006306 = &PP.getIdentifierTable().get(StringRef(Str, StrLen));
Sebastian Redlf2f0f032010-07-23 23:49:55 +00006307 if (DeserializationListener)
6308 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregorafaf3082009-04-11 00:14:32 +00006309 }
Mike Stump1eb44332009-09-09 15:08:12 +00006310
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00006311 return IdentifiersLoaded[ID];
Douglas Gregor2cf26342009-04-09 22:27:44 +00006312}
6313
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006314IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
Douglas Gregor95eab172011-07-28 20:55:49 +00006315 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
6316}
6317
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006318IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
Douglas Gregor6ec60e02011-08-03 21:49:18 +00006319 if (LocalID < NUM_PREDEF_IDENT_IDS)
6320 return LocalID;
6321
6322 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6323 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
6324 assert(I != M.IdentifierRemap.end()
6325 && "Invalid index into identifier index remap");
6326
6327 return LocalID + I->second;
Douglas Gregor95eab172011-07-28 20:55:49 +00006328}
6329
Douglas Gregor3ab50fe2012-10-11 17:41:54 +00006330MacroInfo *ASTReader::getMacro(MacroID ID, MacroInfo *Hint) {
Douglas Gregora8235d62012-10-09 23:05:51 +00006331 if (ID == 0)
6332 return 0;
6333
6334 if (MacrosLoaded.empty()) {
6335 Error("no macro table in AST file");
6336 return 0;
6337 }
6338
6339 ID -= NUM_PREDEF_MACRO_IDS;
6340 if (!MacrosLoaded[ID]) {
6341 GlobalMacroMapType::iterator I
6342 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
6343 assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
6344 ModuleFile *M = I->second;
6345 unsigned Index = ID - M->BaseMacroID;
Douglas Gregor3ab50fe2012-10-11 17:41:54 +00006346 ReadMacroRecord(*M, M->MacroOffsets[Index], Hint);
Douglas Gregora8235d62012-10-09 23:05:51 +00006347 }
6348
6349 return MacrosLoaded[ID];
6350}
6351
6352MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
6353 if (LocalID < NUM_PREDEF_MACRO_IDS)
6354 return LocalID;
6355
6356 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6357 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
6358 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
6359
6360 return LocalID + I->second;
6361}
6362
Douglas Gregor26ced122011-12-01 00:59:36 +00006363serialization::SubmoduleID
6364ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
6365 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
6366 return LocalID;
6367
6368 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6369 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
6370 assert(I != M.SubmoduleRemap.end()
Douglas Gregora8235d62012-10-09 23:05:51 +00006371 && "Invalid index into submodule index remap");
Douglas Gregor26ced122011-12-01 00:59:36 +00006372
6373 return LocalID + I->second;
6374}
6375
6376Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
6377 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
6378 assert(GlobalID == 0 && "Unhandled global submodule ID");
6379 return 0;
6380 }
6381
6382 if (GlobalID > SubmodulesLoaded.size()) {
6383 Error("submodule ID out of range in AST file");
6384 return 0;
6385 }
6386
6387 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
6388}
6389
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006390Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
Douglas Gregor2d2689a2011-07-28 21:16:51 +00006391 return DecodeSelector(getGlobalSelectorID(M, LocalID));
6392}
6393
6394Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
Steve Naroff90cd1bb2009-04-23 10:39:46 +00006395 if (ID == 0)
6396 return Selector();
Mike Stump1eb44332009-09-09 15:08:12 +00006397
Sebastian Redl725cd962010-08-04 20:40:17 +00006398 if (ID > SelectorsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00006399 Error("selector ID out of range in AST file");
Steve Naroff90cd1bb2009-04-23 10:39:46 +00006400 return Selector();
6401 }
Douglas Gregor83941df2009-04-25 17:48:32 +00006402
Sebastian Redl725cd962010-08-04 20:40:17 +00006403 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor83941df2009-04-25 17:48:32 +00006404 // Load this selector from the selector table.
Douglas Gregor96958cb2011-07-20 01:10:58 +00006405 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
6406 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006407 ModuleFile &M = *I->second;
Douglas Gregor9827a802011-07-29 00:56:45 +00006408 ASTSelectorLookupTrait Trait(*this, M);
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00006409 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
Douglas Gregor96958cb2011-07-20 01:10:58 +00006410 SelectorsLoaded[ID - 1] =
Douglas Gregor9827a802011-07-29 00:56:45 +00006411 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
Douglas Gregor96958cb2011-07-20 01:10:58 +00006412 if (DeserializationListener)
6413 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
Douglas Gregor83941df2009-04-25 17:48:32 +00006414 }
6415
Sebastian Redl725cd962010-08-04 20:40:17 +00006416 return SelectorsLoaded[ID - 1];
Steve Naroff90cd1bb2009-04-23 10:39:46 +00006417}
6418
Douglas Gregor8451ec72011-07-28 14:41:43 +00006419Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
Douglas Gregor719770d2010-04-06 17:30:22 +00006420 return DecodeSelector(ID);
6421}
6422
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006423uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redl725cd962010-08-04 20:40:17 +00006424 // ID 0 (the null selector) is considered an external selector.
6425 return getTotalNumSelectors() + 1;
Douglas Gregor719770d2010-04-06 17:30:22 +00006426}
6427
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00006428serialization::SelectorID
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006429ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00006430 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
6431 return LocalID;
6432
6433 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6434 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
6435 assert(I != M.SelectorRemap.end()
Douglas Gregora8235d62012-10-09 23:05:51 +00006436 && "Invalid index into selector index remap");
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00006437
6438 return LocalID + I->second;
Douglas Gregor8451ec72011-07-28 14:41:43 +00006439}
6440
Mike Stump1eb44332009-09-09 15:08:12 +00006441DeclarationName
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006442ASTReader::ReadDeclarationName(ModuleFile &F,
Douglas Gregor393f2492011-07-22 00:38:23 +00006443 const RecordData &Record, unsigned &Idx) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00006444 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
6445 switch (Kind) {
6446 case DeclarationName::Identifier:
Douglas Gregor95eab172011-07-28 20:55:49 +00006447 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00006448
6449 case DeclarationName::ObjCZeroArgSelector:
6450 case DeclarationName::ObjCOneArgSelector:
6451 case DeclarationName::ObjCMultiArgSelector:
Douglas Gregor2d2689a2011-07-28 21:16:51 +00006452 return DeclarationName(ReadSelector(F, Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00006453
6454 case DeclarationName::CXXConstructorName:
Douglas Gregor35942772011-09-09 21:34:22 +00006455 return Context.DeclarationNames.getCXXConstructorName(
6456 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00006457
6458 case DeclarationName::CXXDestructorName:
Douglas Gregor35942772011-09-09 21:34:22 +00006459 return Context.DeclarationNames.getCXXDestructorName(
6460 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00006461
6462 case DeclarationName::CXXConversionFunctionName:
Douglas Gregor35942772011-09-09 21:34:22 +00006463 return Context.DeclarationNames.getCXXConversionFunctionName(
6464 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00006465
6466 case DeclarationName::CXXOperatorName:
Douglas Gregor35942772011-09-09 21:34:22 +00006467 return Context.DeclarationNames.getCXXOperatorName(
Douglas Gregor2cf26342009-04-09 22:27:44 +00006468 (OverloadedOperatorKind)Record[Idx++]);
6469
Sean Hunt3e518bd2009-11-29 07:34:05 +00006470 case DeclarationName::CXXLiteralOperatorName:
Douglas Gregor35942772011-09-09 21:34:22 +00006471 return Context.DeclarationNames.getCXXLiteralOperatorName(
Douglas Gregor95eab172011-07-28 20:55:49 +00006472 GetIdentifierInfo(F, Record, Idx));
Sean Hunt3e518bd2009-11-29 07:34:05 +00006473
Douglas Gregor2cf26342009-04-09 22:27:44 +00006474 case DeclarationName::CXXUsingDirective:
6475 return DeclarationName::getUsingDirectiveName();
6476 }
6477
David Blaikie7530c032012-01-17 06:56:22 +00006478 llvm_unreachable("Invalid NameKind!");
Douglas Gregor2cf26342009-04-09 22:27:44 +00006479}
Douglas Gregor0a0428e2009-04-10 20:39:37 +00006480
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006481void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006482 DeclarationNameLoc &DNLoc,
6483 DeclarationName Name,
6484 const RecordData &Record, unsigned &Idx) {
6485 switch (Name.getNameKind()) {
6486 case DeclarationName::CXXConstructorName:
6487 case DeclarationName::CXXDestructorName:
6488 case DeclarationName::CXXConversionFunctionName:
6489 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
6490 break;
6491
6492 case DeclarationName::CXXOperatorName:
6493 DNLoc.CXXOperatorName.BeginOpNameLoc
6494 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6495 DNLoc.CXXOperatorName.EndOpNameLoc
6496 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6497 break;
6498
6499 case DeclarationName::CXXLiteralOperatorName:
6500 DNLoc.CXXLiteralOperatorName.OpNameLoc
6501 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6502 break;
6503
6504 case DeclarationName::Identifier:
6505 case DeclarationName::ObjCZeroArgSelector:
6506 case DeclarationName::ObjCOneArgSelector:
6507 case DeclarationName::ObjCMultiArgSelector:
6508 case DeclarationName::CXXUsingDirective:
6509 break;
6510 }
6511}
6512
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006513void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006514 DeclarationNameInfo &NameInfo,
6515 const RecordData &Record, unsigned &Idx) {
Douglas Gregor393f2492011-07-22 00:38:23 +00006516 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006517 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
6518 DeclarationNameLoc DNLoc;
6519 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
6520 NameInfo.setInfo(DNLoc);
6521}
6522
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006523void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006524 const RecordData &Record, unsigned &Idx) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00006525 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006526 unsigned NumTPLists = Record[Idx++];
6527 Info.NumTemplParamLists = NumTPLists;
6528 if (NumTPLists) {
Douglas Gregor35942772011-09-09 21:34:22 +00006529 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006530 for (unsigned i=0; i != NumTPLists; ++i)
6531 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
6532 }
6533}
6534
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006535TemplateName
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006536ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006537 unsigned &Idx) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00006538 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006539 switch (Kind) {
6540 case TemplateName::Template:
Douglas Gregor409448c2011-07-21 22:35:25 +00006541 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006542
6543 case TemplateName::OverloadedTemplate: {
6544 unsigned size = Record[Idx++];
6545 UnresolvedSet<8> Decls;
6546 while (size--)
Douglas Gregor409448c2011-07-21 22:35:25 +00006547 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006548
Douglas Gregor35942772011-09-09 21:34:22 +00006549 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006550 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006551
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006552 case TemplateName::QualifiedTemplate: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006553 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006554 bool hasTemplKeyword = Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00006555 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006556 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006557 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006558
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006559 case TemplateName::DependentTemplate: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006560 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006561 if (Record[Idx++]) // isIdentifier
Douglas Gregor35942772011-09-09 21:34:22 +00006562 return Context.getDependentTemplateName(NNS,
Douglas Gregor95eab172011-07-28 20:55:49 +00006563 GetIdentifierInfo(F, Record,
6564 Idx));
Douglas Gregor35942772011-09-09 21:34:22 +00006565 return Context.getDependentTemplateName(NNS,
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00006566 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006567 }
John McCall14606042011-06-30 08:33:18 +00006568
6569 case TemplateName::SubstTemplateTemplateParm: {
6570 TemplateTemplateParmDecl *param
Douglas Gregor409448c2011-07-21 22:35:25 +00006571 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
John McCall14606042011-06-30 08:33:18 +00006572 if (!param) return TemplateName();
6573 TemplateName replacement = ReadTemplateName(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006574 return Context.getSubstTemplateTemplateParm(param, replacement);
John McCall14606042011-06-30 08:33:18 +00006575 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006576
6577 case TemplateName::SubstTemplateTemplateParmPack: {
6578 TemplateTemplateParmDecl *Param
Douglas Gregor409448c2011-07-21 22:35:25 +00006579 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006580 if (!Param)
6581 return TemplateName();
6582
6583 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
6584 if (ArgPack.getKind() != TemplateArgument::Pack)
6585 return TemplateName();
6586
Douglas Gregor35942772011-09-09 21:34:22 +00006587 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006588 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006589 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006590
David Blaikieb219cfc2011-09-23 05:06:16 +00006591 llvm_unreachable("Unhandled template name kind!");
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006592}
6593
6594TemplateArgument
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006595ASTReader::ReadTemplateArgument(ModuleFile &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00006596 const RecordData &Record, unsigned &Idx) {
Douglas Gregora7fc9012011-01-05 18:58:31 +00006597 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
6598 switch (Kind) {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006599 case TemplateArgument::Null:
6600 return TemplateArgument();
6601 case TemplateArgument::Type:
Douglas Gregor393f2492011-07-22 00:38:23 +00006602 return TemplateArgument(readType(F, Record, Idx));
Eli Friedmand7a6b162012-09-26 02:36:12 +00006603 case TemplateArgument::Declaration: {
6604 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
6605 bool ForReferenceParam = Record[Idx++];
6606 return TemplateArgument(D, ForReferenceParam);
6607 }
6608 case TemplateArgument::NullPtr:
6609 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00006610 case TemplateArgument::Integral: {
6611 llvm::APSInt Value = ReadAPSInt(Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00006612 QualType T = readType(F, Record, Idx);
Benjamin Kramer85524372012-06-07 15:09:51 +00006613 return TemplateArgument(Context, Value, T);
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00006614 }
Douglas Gregora7fc9012011-01-05 18:58:31 +00006615 case TemplateArgument::Template:
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006616 return TemplateArgument(ReadTemplateName(F, Record, Idx));
Douglas Gregora7fc9012011-01-05 18:58:31 +00006617 case TemplateArgument::TemplateExpansion: {
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006618 TemplateName Name = ReadTemplateName(F, Record, Idx);
Douglas Gregor2be29f42011-01-14 23:41:42 +00006619 llvm::Optional<unsigned> NumTemplateExpansions;
6620 if (unsigned NumExpansions = Record[Idx++])
6621 NumTemplateExpansions = NumExpansions - 1;
6622 return TemplateArgument(Name, NumTemplateExpansions);
Douglas Gregorba68eca2011-01-05 17:40:24 +00006623 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006624 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00006625 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006626 case TemplateArgument::Pack: {
6627 unsigned NumArgs = Record[Idx++];
Douglas Gregor35942772011-09-09 21:34:22 +00006628 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
Douglas Gregor910f8002010-11-07 23:05:16 +00006629 for (unsigned I = 0; I != NumArgs; ++I)
6630 Args[I] = ReadTemplateArgument(F, Record, Idx);
6631 return TemplateArgument(Args, NumArgs);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006632 }
6633 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006634
David Blaikieb219cfc2011-09-23 05:06:16 +00006635 llvm_unreachable("Unhandled template argument kind!");
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006636}
6637
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006638TemplateParameterList *
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006639ASTReader::ReadTemplateParameterList(ModuleFile &F,
Sebastian Redlc3632732010-10-05 15:59:54 +00006640 const RecordData &Record, unsigned &Idx) {
6641 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
6642 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
6643 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006644
6645 unsigned NumParams = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00006646 SmallVector<NamedDecl *, 16> Params;
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006647 Params.reserve(NumParams);
6648 while (NumParams--)
Douglas Gregor409448c2011-07-21 22:35:25 +00006649 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
Michael J. Spencer20249a12010-10-21 03:16:25 +00006650
6651 TemplateParameterList* TemplateParams =
Douglas Gregor35942772011-09-09 21:34:22 +00006652 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006653 Params.data(), Params.size(), RAngleLoc);
6654 return TemplateParams;
6655}
6656
6657void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006658ASTReader::
Chris Lattner5f9e2722011-07-23 10:55:15 +00006659ReadTemplateArgumentList(SmallVector<TemplateArgument, 8> &TemplArgs,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006660 ModuleFile &F, const RecordData &Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00006661 unsigned &Idx) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006662 unsigned NumTemplateArgs = Record[Idx++];
6663 TemplArgs.reserve(NumTemplateArgs);
6664 while (NumTemplateArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00006665 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006666}
6667
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00006668/// \brief Read a UnresolvedSet structure.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006669void ASTReader::ReadUnresolvedSet(ModuleFile &F, UnresolvedSetImpl &Set,
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00006670 const RecordData &Record, unsigned &Idx) {
6671 unsigned NumDecls = Record[Idx++];
6672 while (NumDecls--) {
Douglas Gregor409448c2011-07-21 22:35:25 +00006673 NamedDecl *D = ReadDeclAs<NamedDecl>(F, Record, Idx);
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00006674 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
6675 Set.addDecl(D, AS);
6676 }
6677}
6678
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00006679CXXBaseSpecifier
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006680ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
Nick Lewycky56062202010-07-26 16:56:01 +00006681 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00006682 bool isVirtual = static_cast<bool>(Record[Idx++]);
6683 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
6684 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redlf677ea32011-02-05 19:23:19 +00006685 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00006686 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
6687 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00006688 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redlf677ea32011-02-05 19:23:19 +00006689 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
Douglas Gregorf90b27a2011-01-03 22:36:02 +00006690 EllipsisLoc);
Sebastian Redlf677ea32011-02-05 19:23:19 +00006691 Result.setInheritConstructors(inheritConstructors);
6692 return Result;
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00006693}
6694
Sean Huntcbb67482011-01-08 20:30:50 +00006695std::pair<CXXCtorInitializer **, unsigned>
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006696ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
Sean Huntcbb67482011-01-08 20:30:50 +00006697 unsigned &Idx) {
6698 CXXCtorInitializer **CtorInitializers = 0;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006699 unsigned NumInitializers = Record[Idx++];
6700 if (NumInitializers) {
Sean Huntcbb67482011-01-08 20:30:50 +00006701 CtorInitializers
Douglas Gregor35942772011-09-09 21:34:22 +00006702 = new (Context) CXXCtorInitializer*[NumInitializers];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006703 for (unsigned i=0; i != NumInitializers; ++i) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006704 TypeSourceInfo *TInfo = 0;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006705 bool IsBaseVirtual = false;
6706 FieldDecl *Member = 0;
Francois Pichet00eb3f92010-12-04 09:14:42 +00006707 IndirectFieldDecl *IndirectMember = 0;
Michael J. Spencer20249a12010-10-21 03:16:25 +00006708
Sean Hunt156b6402011-05-04 01:19:08 +00006709 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
6710 switch (Type) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006711 case CTOR_INITIALIZER_BASE:
6712 TInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006713 IsBaseVirtual = Record[Idx++];
Sean Hunt156b6402011-05-04 01:19:08 +00006714 break;
Douglas Gregor76852c22011-11-01 01:16:03 +00006715
6716 case CTOR_INITIALIZER_DELEGATING:
6717 TInfo = GetTypeSourceInfo(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00006718 break;
6719
6720 case CTOR_INITIALIZER_MEMBER:
Douglas Gregor409448c2011-07-21 22:35:25 +00006721 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00006722 break;
6723
6724 case CTOR_INITIALIZER_INDIRECT_MEMBER:
Douglas Gregor409448c2011-07-21 22:35:25 +00006725 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00006726 break;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006727 }
Sean Hunt156b6402011-05-04 01:19:08 +00006728
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00006729 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redlc3632732010-10-05 15:59:54 +00006730 Expr *Init = ReadExpr(F);
Sebastian Redlc3632732010-10-05 15:59:54 +00006731 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
6732 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006733 bool IsWritten = Record[Idx++];
6734 unsigned SourceOrderOrNumArrayIndices;
Chris Lattner5f9e2722011-07-23 10:55:15 +00006735 SmallVector<VarDecl *, 8> Indices;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006736 if (IsWritten) {
6737 SourceOrderOrNumArrayIndices = Record[Idx++];
6738 } else {
6739 SourceOrderOrNumArrayIndices = Record[Idx++];
6740 Indices.reserve(SourceOrderOrNumArrayIndices);
6741 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
Douglas Gregor409448c2011-07-21 22:35:25 +00006742 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006743 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006744
Sean Huntcbb67482011-01-08 20:30:50 +00006745 CXXCtorInitializer *BOMInit;
Sean Hunt156b6402011-05-04 01:19:08 +00006746 if (Type == CTOR_INITIALIZER_BASE) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006747 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, IsBaseVirtual,
Sean Huntcbb67482011-01-08 20:30:50 +00006748 LParenLoc, Init, RParenLoc,
6749 MemberOrEllipsisLoc);
Sean Hunt156b6402011-05-04 01:19:08 +00006750 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006751 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, LParenLoc,
6752 Init, RParenLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006753 } else if (IsWritten) {
Francois Pichet00eb3f92010-12-04 09:14:42 +00006754 if (Member)
Douglas Gregor35942772011-09-09 21:34:22 +00006755 BOMInit = new (Context) CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc,
Sean Huntcbb67482011-01-08 20:30:50 +00006756 LParenLoc, Init, RParenLoc);
Francois Pichet00eb3f92010-12-04 09:14:42 +00006757 else
Douglas Gregor35942772011-09-09 21:34:22 +00006758 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
Sean Huntcbb67482011-01-08 20:30:50 +00006759 MemberOrEllipsisLoc, LParenLoc,
6760 Init, RParenLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006761 } else {
Douglas Gregor35942772011-09-09 21:34:22 +00006762 BOMInit = CXXCtorInitializer::Create(Context, Member, MemberOrEllipsisLoc,
Sean Huntcbb67482011-01-08 20:30:50 +00006763 LParenLoc, Init, RParenLoc,
6764 Indices.data(), Indices.size());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006765 }
6766
Argyrios Kyrtzidisf84cde12010-09-06 19:04:27 +00006767 if (IsWritten)
6768 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Sean Huntcbb67482011-01-08 20:30:50 +00006769 CtorInitializers[i] = BOMInit;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006770 }
6771 }
6772
Sean Huntcbb67482011-01-08 20:30:50 +00006773 return std::make_pair(CtorInitializers, NumInitializers);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006774}
6775
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006776NestedNameSpecifier *
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006777ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00006778 const RecordData &Record, unsigned &Idx) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006779 unsigned N = Record[Idx++];
6780 NestedNameSpecifier *NNS = 0, *Prev = 0;
6781 for (unsigned I = 0; I != N; ++I) {
6782 NestedNameSpecifier::SpecifierKind Kind
6783 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
6784 switch (Kind) {
6785 case NestedNameSpecifier::Identifier: {
Douglas Gregor95eab172011-07-28 20:55:49 +00006786 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006787 NNS = NestedNameSpecifier::Create(Context, Prev, II);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006788 break;
6789 }
6790
6791 case NestedNameSpecifier::Namespace: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006792 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006793 NNS = NestedNameSpecifier::Create(Context, Prev, NS);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006794 break;
6795 }
6796
Douglas Gregor14aba762011-02-24 02:36:08 +00006797 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006798 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006799 NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
Douglas Gregor14aba762011-02-24 02:36:08 +00006800 break;
6801 }
6802
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006803 case NestedNameSpecifier::TypeSpec:
6804 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregor393f2492011-07-22 00:38:23 +00006805 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
Douglas Gregor1ab55e92010-12-10 17:03:06 +00006806 if (!T)
6807 return 0;
6808
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006809 bool Template = Record[Idx++];
Douglas Gregor35942772011-09-09 21:34:22 +00006810 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006811 break;
6812 }
6813
6814 case NestedNameSpecifier::Global: {
Douglas Gregor35942772011-09-09 21:34:22 +00006815 NNS = NestedNameSpecifier::GlobalSpecifier(Context);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006816 // No associated value, and there can't be a prefix.
6817 break;
6818 }
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006819 }
Argyrios Kyrtzidisd2bb2c02010-07-07 15:46:30 +00006820 Prev = NNS;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006821 }
6822 return NNS;
6823}
6824
Douglas Gregordc355712011-02-25 00:36:19 +00006825NestedNameSpecifierLoc
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006826ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
Douglas Gregordc355712011-02-25 00:36:19 +00006827 unsigned &Idx) {
6828 unsigned N = Record[Idx++];
Douglas Gregor5f791bb2011-02-28 23:58:31 +00006829 NestedNameSpecifierLocBuilder Builder;
Douglas Gregordc355712011-02-25 00:36:19 +00006830 for (unsigned I = 0; I != N; ++I) {
6831 NestedNameSpecifier::SpecifierKind Kind
6832 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
6833 switch (Kind) {
6834 case NestedNameSpecifier::Identifier: {
Douglas Gregor95eab172011-07-28 20:55:49 +00006835 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00006836 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006837 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00006838 break;
6839 }
6840
6841 case NestedNameSpecifier::Namespace: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006842 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00006843 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006844 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00006845 break;
6846 }
6847
6848 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006849 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00006850 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006851 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00006852 break;
6853 }
6854
6855 case NestedNameSpecifier::TypeSpec:
6856 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregordc355712011-02-25 00:36:19 +00006857 bool Template = Record[Idx++];
6858 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
6859 if (!T)
6860 return NestedNameSpecifierLoc();
Douglas Gregordc355712011-02-25 00:36:19 +00006861 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor5f791bb2011-02-28 23:58:31 +00006862
6863 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
Douglas Gregor35942772011-09-09 21:34:22 +00006864 Builder.Extend(Context,
Douglas Gregor5f791bb2011-02-28 23:58:31 +00006865 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
6866 T->getTypeLoc(), ColonColonLoc);
Douglas Gregordc355712011-02-25 00:36:19 +00006867 break;
6868 }
6869
6870 case NestedNameSpecifier::Global: {
Douglas Gregordc355712011-02-25 00:36:19 +00006871 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006872 Builder.MakeGlobal(Context, ColonColonLoc);
Douglas Gregordc355712011-02-25 00:36:19 +00006873 break;
6874 }
6875 }
Douglas Gregordc355712011-02-25 00:36:19 +00006876 }
6877
Douglas Gregor35942772011-09-09 21:34:22 +00006878 return Builder.getWithLocInContext(Context);
Douglas Gregordc355712011-02-25 00:36:19 +00006879}
6880
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006881SourceRange
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006882ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00006883 unsigned &Idx) {
6884 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
6885 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar8ee59392010-06-02 15:47:10 +00006886 return SourceRange(beg, end);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006887}
6888
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00006889/// \brief Read an integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006890llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00006891 unsigned BitWidth = Record[Idx++];
6892 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
6893 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
6894 Idx += NumWords;
6895 return Result;
6896}
6897
6898/// \brief Read a signed integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006899llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00006900 bool isUnsigned = Record[Idx++];
6901 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
6902}
6903
Douglas Gregor17fc2232009-04-14 21:55:33 +00006904/// \brief Read a floating-point value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006905llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00006906 return llvm::APFloat(ReadAPInt(Record, Idx));
6907}
6908
Douglas Gregor68a2eb02009-04-15 21:30:51 +00006909// \brief Read a string
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006910std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00006911 unsigned Len = Record[Idx++];
Jay Foadbeaaccd2009-05-21 09:52:38 +00006912 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00006913 Idx += Len;
6914 return Result;
6915}
6916
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00006917VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
6918 unsigned &Idx) {
6919 unsigned Major = Record[Idx++];
6920 unsigned Minor = Record[Idx++];
6921 unsigned Subminor = Record[Idx++];
6922 if (Minor == 0)
6923 return VersionTuple(Major);
6924 if (Subminor == 0)
6925 return VersionTuple(Major, Minor - 1);
6926 return VersionTuple(Major, Minor - 1, Subminor - 1);
6927}
6928
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006929CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00006930 const RecordData &Record,
Chris Lattnerd2598362010-05-10 00:25:06 +00006931 unsigned &Idx) {
Douglas Gregor409448c2011-07-21 22:35:25 +00006932 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006933 return CXXTemporary::Create(Context, Decl);
Chris Lattnerd2598362010-05-10 00:25:06 +00006934}
6935
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006936DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00006937 return Diag(SourceLocation(), DiagID);
6938}
6939
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006940DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00006941 return Diags.Report(Loc, DiagID);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00006942}
Douglas Gregor025452f2009-04-17 00:04:06 +00006943
Douglas Gregor668c1a42009-04-21 22:25:48 +00006944/// \brief Retrieve the identifier table associated with the
6945/// preprocessor.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006946IdentifierTable &ASTReader::getIdentifierTable() {
Douglas Gregor712f2fc2011-09-09 22:02:16 +00006947 return PP.getIdentifierTable();
Douglas Gregor668c1a42009-04-21 22:25:48 +00006948}
6949
Douglas Gregor025452f2009-04-17 00:04:06 +00006950/// \brief Record that the given ID maps to the given switch-case
6951/// statement.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006952void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00006953 assert((*CurrSwitchCaseStmts)[ID] == 0 &&
6954 "Already have a SwitchCase with this ID");
6955 (*CurrSwitchCaseStmts)[ID] = SC;
Douglas Gregor025452f2009-04-17 00:04:06 +00006956}
6957
6958/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006959SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00006960 assert((*CurrSwitchCaseStmts)[ID] != 0 && "No SwitchCase with this ID");
6961 return (*CurrSwitchCaseStmts)[ID];
Douglas Gregor025452f2009-04-17 00:04:06 +00006962}
Douglas Gregor1de05fe2009-04-17 18:18:49 +00006963
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00006964void ASTReader::ClearSwitchCaseIDs() {
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00006965 CurrSwitchCaseStmts->clear();
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00006966}
6967
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00006968void ASTReader::ReadComments() {
Dmitri Gribenko811c8202012-07-06 18:19:34 +00006969 std::vector<RawComment *> Comments;
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00006970 for (SmallVectorImpl<std::pair<llvm::BitstreamCursor,
6971 serialization::ModuleFile *> >::iterator
6972 I = CommentsCursors.begin(),
6973 E = CommentsCursors.end();
6974 I != E; ++I) {
6975 llvm::BitstreamCursor &Cursor = I->first;
6976 serialization::ModuleFile &F = *I->second;
6977 SavedStreamPosition SavedPosition(Cursor);
6978
6979 RecordData Record;
6980 while (true) {
6981 unsigned Code = Cursor.ReadCode();
6982 if (Code == llvm::bitc::END_BLOCK)
6983 break;
6984
6985 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
6986 // No known subblocks, always skip them.
6987 Cursor.ReadSubBlockID();
6988 if (Cursor.SkipBlock()) {
6989 Error("malformed block record in AST file");
6990 return;
6991 }
6992 continue;
6993 }
6994
6995 if (Code == llvm::bitc::DEFINE_ABBREV) {
6996 Cursor.ReadAbbrevRecord();
6997 continue;
6998 }
6999
7000 // Read a record.
7001 Record.clear();
7002 switch ((CommentRecordTypes) Cursor.ReadRecord(Code, Record)) {
Chandler Carruth13691bb2012-06-20 06:47:54 +00007003 case COMMENTS_RAW_COMMENT: {
7004 unsigned Idx = 0;
7005 SourceRange SR = ReadSourceRange(F, Record, Idx);
7006 RawComment::CommentKind Kind =
7007 (RawComment::CommentKind) Record[Idx++];
7008 bool IsTrailingComment = Record[Idx++];
7009 bool IsAlmostTrailingComment = Record[Idx++];
Dmitri Gribenko811c8202012-07-06 18:19:34 +00007010 Comments.push_back(new (Context) RawComment(SR, Kind,
7011 IsTrailingComment,
7012 IsAlmostTrailingComment));
Chandler Carruth13691bb2012-06-20 06:47:54 +00007013 break;
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00007014 }
7015 }
7016 }
7017 }
7018 Context.Comments.addCommentsToFront(Comments);
7019}
7020
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00007021void ASTReader::finishPendingActions() {
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00007022 while (!PendingIdentifierInfos.empty() || !PendingDeclChains.empty() ||
7023 !PendingMacroIDs.empty()) {
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00007024 // If any identifiers with corresponding top-level declarations have
7025 // been loaded, load those declarations now.
7026 while (!PendingIdentifierInfos.empty()) {
7027 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
7028 PendingIdentifierInfos.front().DeclIDs, true);
7029 PendingIdentifierInfos.pop_front();
7030 }
7031
Douglas Gregora1be2782011-12-17 23:38:30 +00007032 // Load pending declaration chains.
7033 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) {
7034 loadPendingDeclChain(PendingDeclChains[I]);
Douglas Gregor56ca8a92012-01-17 19:21:53 +00007035 PendingDeclChainsKnown.erase(PendingDeclChains[I]);
Douglas Gregora1be2782011-12-17 23:38:30 +00007036 }
7037 PendingDeclChains.clear();
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00007038
7039 // Load any pending macro definitions.
Douglas Gregore9652bf2012-10-11 17:31:34 +00007040 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
7041 // FIXME: std::move here
7042 SmallVector<MacroID, 2> GlobalIDs = PendingMacroIDs.begin()[I].second;
Douglas Gregor3ab50fe2012-10-11 17:41:54 +00007043 MacroInfo *Hint = 0;
Douglas Gregore9652bf2012-10-11 17:31:34 +00007044 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
7045 ++IDIdx) {
Douglas Gregor3ab50fe2012-10-11 17:41:54 +00007046 Hint = getMacro(GlobalIDs[IDIdx], Hint);
Douglas Gregore9652bf2012-10-11 17:31:34 +00007047 }
7048 }
7049 PendingMacroIDs.clear();
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00007050 }
Douglas Gregorfc529f72011-12-19 19:00:47 +00007051
Douglas Gregor7c99bb5c2012-01-14 15:13:49 +00007052 // If we deserialized any C++ or Objective-C class definitions, any
7053 // Objective-C protocol definitions, or any redeclarable templates, make sure
7054 // that all redeclarations point to the definitions. Note that this can only
7055 // happen now, after the redeclaration chains have been fully wired.
Douglas Gregorfc529f72011-12-19 19:00:47 +00007056 for (llvm::SmallPtrSet<Decl *, 4>::iterator D = PendingDefinitions.begin(),
7057 DEnd = PendingDefinitions.end();
7058 D != DEnd; ++D) {
Douglas Gregor56ca8a92012-01-17 19:21:53 +00007059 if (TagDecl *TD = dyn_cast<TagDecl>(*D)) {
7060 if (const TagType *TagT = dyn_cast<TagType>(TD->TypeForDecl)) {
7061 // Make sure that the TagType points at the definition.
7062 const_cast<TagType*>(TagT)->decl = TD;
7063 }
Douglas Gregorfc529f72011-12-19 19:00:47 +00007064
Douglas Gregor56ca8a92012-01-17 19:21:53 +00007065 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(*D)) {
7066 for (CXXRecordDecl::redecl_iterator R = RD->redecls_begin(),
7067 REnd = RD->redecls_end();
7068 R != REnd; ++R)
7069 cast<CXXRecordDecl>(*R)->DefinitionData = RD->DefinitionData;
7070
7071 }
7072
Douglas Gregorfc529f72011-12-19 19:00:47 +00007073 continue;
7074 }
7075
Douglas Gregor1d784b22012-01-01 19:51:50 +00007076 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(*D)) {
Douglas Gregor56ca8a92012-01-17 19:21:53 +00007077 // Make sure that the ObjCInterfaceType points at the definition.
7078 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
7079 ->Decl = ID;
7080
Douglas Gregor1d784b22012-01-01 19:51:50 +00007081 for (ObjCInterfaceDecl::redecl_iterator R = ID->redecls_begin(),
7082 REnd = ID->redecls_end();
7083 R != REnd; ++R)
7084 R->Data = ID->Data;
7085
7086 continue;
7087 }
7088
Douglas Gregor7c99bb5c2012-01-14 15:13:49 +00007089 if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(*D)) {
7090 for (ObjCProtocolDecl::redecl_iterator R = PD->redecls_begin(),
7091 REnd = PD->redecls_end();
7092 R != REnd; ++R)
7093 R->Data = PD->Data;
7094
7095 continue;
7096 }
7097
7098 RedeclarableTemplateDecl *RTD
7099 = cast<RedeclarableTemplateDecl>(*D)->getCanonicalDecl();
7100 for (RedeclarableTemplateDecl::redecl_iterator R = RTD->redecls_begin(),
7101 REnd = RTD->redecls_end();
Douglas Gregorfc529f72011-12-19 19:00:47 +00007102 R != REnd; ++R)
Douglas Gregor5456b0fe2012-10-09 17:21:28 +00007103 R->Common = RTD->Common;
Douglas Gregorfc529f72011-12-19 19:00:47 +00007104 }
7105 PendingDefinitions.clear();
Douglas Gregor5456b0fe2012-10-09 17:21:28 +00007106
7107 // Load the bodies of any functions or methods we've encountered. We do
7108 // this now (delayed) so that we can be sure that the declaration chains
7109 // have been fully wired up.
Douglas Gregorce12d2f2012-10-09 17:50:23 +00007110 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
7111 PBEnd = PendingBodies.end();
Douglas Gregor5456b0fe2012-10-09 17:21:28 +00007112 PB != PBEnd; ++PB) {
7113 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
7114 // FIXME: Check for =delete/=default?
7115 // FIXME: Complain about ODR violations here?
7116 if (!getContext().getLangOpts().Modules || !FD->hasBody())
7117 FD->setLazyBody(PB->second);
7118 continue;
7119 }
7120
7121 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
7122 if (!getContext().getLangOpts().Modules || !MD->hasBody())
7123 MD->setLazyBody(PB->second);
7124 }
7125 PendingBodies.clear();
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00007126}
7127
Sebastian Redlc43b54c2010-08-18 23:56:43 +00007128void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00007129 assert(NumCurrentElementsDeserializing &&
7130 "FinishedDeserializing not paired with StartedDeserializing");
7131 if (NumCurrentElementsDeserializing == 1) {
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00007132 // We decrease NumCurrentElementsDeserializing only after pending actions
7133 // are finished, to avoid recursively re-calling finishPendingActions().
7134 finishPendingActions();
7135 }
7136 --NumCurrentElementsDeserializing;
Argyrios Kyrtzidis71168332011-12-17 04:13:28 +00007137
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00007138 if (NumCurrentElementsDeserializing == 0 &&
7139 Consumer && !PassingDeclsToConsumer) {
7140 // Guard variable to avoid recursively redoing the process of passing
7141 // decls to consumer.
7142 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
7143 true);
Argyrios Kyrtzidis71168332011-12-17 04:13:28 +00007144
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00007145 while (!InterestingDecls.empty()) {
Argyrios Kyrtzidis8d39c3d2011-11-30 23:18:26 +00007146 // We are not in recursive loading, so it's safe to pass the "interesting"
7147 // decls to the consumer.
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00007148 Decl *D = InterestingDecls.front();
7149 InterestingDecls.pop_front();
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00007150 PassInterestingDeclToConsumer(D);
7151 }
Douglas Gregord89275b2009-07-06 18:54:52 +00007152 }
Douglas Gregord89275b2009-07-06 18:54:52 +00007153}
Douglas Gregor501c1032010-08-19 00:28:17 +00007154
Douglas Gregorf8a1e512011-09-02 00:26:20 +00007155ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context,
Douglas Gregor832d6202011-07-22 16:35:34 +00007156 StringRef isysroot, bool DisableValidation,
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00007157 bool DisableStatCache, bool AllowASTWithCompilerErrors)
Sebastian Redle1dde812010-08-24 00:50:04 +00007158 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
7159 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
Douglas Gregor712f2fc2011-09-09 22:02:16 +00007160 Diags(PP.getDiagnostics()), SemaObj(0), PP(PP), Context(Context),
Argyrios Kyrtzidisd64c26f2012-10-03 01:58:42 +00007161 Consumer(0), ModuleMgr(PP.getFileManager()),
Douglas Gregorcaed0602012-10-18 21:31:35 +00007162 isysroot(isysroot), DisableValidation(DisableValidation),
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00007163 DisableStatCache(DisableStatCache),
7164 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00007165 CurrentGeneration(0), CurrSwitchCaseStmts(&SwitchCaseStmts),
7166 NumStatHits(0), NumStatMisses(0),
Douglas Gregorf62d43d2011-07-19 16:10:42 +00007167 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00007168 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
7169 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
7170 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
7171 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
Jonathan D. Turner1da90142011-07-21 21:15:19 +00007172 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
7173 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00007174 PassingDeclsToConsumer(false),
Jonathan D. Turner1da90142011-07-21 21:15:19 +00007175 NumCXXBaseSpecifiersLoaded(0)
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00007176{
Douglas Gregorf62d43d2011-07-19 16:10:42 +00007177 SourceMgr.setExternalSLocEntrySource(this);
Sebastian Redle1dde812010-08-24 00:50:04 +00007178}
7179
Sebastian Redle1dde812010-08-24 00:50:04 +00007180ASTReader::~ASTReader() {
Sebastian Redle1dde812010-08-24 00:50:04 +00007181 for (DeclContextVisibleUpdatesPending::iterator
7182 I = PendingVisibleUpdates.begin(),
7183 E = PendingVisibleUpdates.end();
7184 I != E; ++I) {
7185 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
7186 F = I->second.end();
7187 J != F; ++J)
Benjamin Kramerb1758c62012-04-15 12:36:49 +00007188 delete J->first;
Sebastian Redle1dde812010-08-24 00:50:04 +00007189 }
7190}