blob: d246569a4cd981a9c68c904acdba7942f1be42ed [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) {
Douglas Gregor4c0c7e82012-10-24 23:41:50 +0000190 const LangOptions &ExistingLangOpts = PP.getLangOpts();
191 return checkLanguageOptions(LangOpts, ExistingLangOpts,
Douglas Gregor27ffa6c2012-10-23 06:18:24 +0000192 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 {
Douglas Gregor4c0c7e82012-10-24 23:41:50 +0000203 typedef llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> >
204 MacroDefinitionsMap;
205}
206
207/// \brief Collect the macro definitions provided by the given preprocessor
208/// options.
209static void collectMacroDefinitions(const PreprocessorOptions &PPOpts,
210 MacroDefinitionsMap &Macros,
211 SmallVectorImpl<StringRef> *MacroNames = 0){
212 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
213 StringRef Macro = PPOpts.Macros[I].first;
214 bool IsUndef = PPOpts.Macros[I].second;
215
216 std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
217 StringRef MacroName = MacroPair.first;
218 StringRef MacroBody = MacroPair.second;
219
220 // For an #undef'd macro, we only care about the name.
221 if (IsUndef) {
222 if (MacroNames && !Macros.count(MacroName))
223 MacroNames->push_back(MacroName);
224
225 Macros[MacroName] = std::make_pair("", true);
226 continue;
227 }
228
229 // For a #define'd macro, figure out the actual definition.
230 if (MacroName.size() == Macro.size())
231 MacroBody = "1";
232 else {
233 // Note: GCC drops anything following an end-of-line character.
234 StringRef::size_type End = MacroBody.find_first_of("\n\r");
235 MacroBody = MacroBody.substr(0, End);
236 }
237
238 if (MacroNames && !Macros.count(MacroName))
239 MacroNames->push_back(MacroName);
240 Macros[MacroName] = std::make_pair(MacroBody, false);
241 }
242}
243
244/// \brief Check the preprocessor options deserialized from the control block
245/// against the preprocessor options in an existing preprocessor.
246///
247/// \param Diags If non-null, produce diagnostics for any mismatches incurred.
248static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts,
249 const PreprocessorOptions &ExistingPPOpts,
Douglas Gregor87699242012-10-25 00:07:54 +0000250 DiagnosticsEngine *Diags,
251 FileManager &FileMgr,
252 std::string &SuggestedPredefines) {
Douglas Gregor4c0c7e82012-10-24 23:41:50 +0000253 // Check macro definitions.
254 MacroDefinitionsMap ASTFileMacros;
255 collectMacroDefinitions(PPOpts, ASTFileMacros);
256 MacroDefinitionsMap ExistingMacros;
257 SmallVector<StringRef, 4> ExistingMacroNames;
258 collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames);
259
260 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
261 // Dig out the macro definition in the existing preprocessor options.
262 StringRef MacroName = ExistingMacroNames[I];
263 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
264
265 // Check whether we know anything about this macro name or not.
266 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> >::iterator Known
267 = ASTFileMacros.find(MacroName);
268 if (Known == ASTFileMacros.end()) {
269 // FIXME: Check whether this identifier was referenced anywhere in the
270 // AST file. If so, we should reject the AST file. Unfortunately, this
271 // information isn't in the control block. What shall we do about it?
Douglas Gregor87699242012-10-25 00:07:54 +0000272
273 if (Existing.second) {
274 SuggestedPredefines += "#undef ";
275 SuggestedPredefines += MacroName.str();
276 SuggestedPredefines += '\n';
277 } else {
278 SuggestedPredefines += "#define ";
279 SuggestedPredefines += MacroName.str();
Douglas Gregora9b8da42012-10-25 00:25:27 +0000280 SuggestedPredefines += ' ';
Douglas Gregor87699242012-10-25 00:07:54 +0000281 SuggestedPredefines += Existing.first.str();
282 SuggestedPredefines += '\n';
283 }
Douglas Gregor4c0c7e82012-10-24 23:41:50 +0000284 continue;
285 }
286
287 // If the macro was defined in one but undef'd in the other, we have a
288 // conflict.
289 if (Existing.second != Known->second.second) {
290 if (Diags) {
291 Diags->Report(diag::err_pch_macro_def_undef)
292 << MacroName << Known->second.second;
293 }
294 return true;
295 }
296
297 // If the macro was #undef'd in both, or if the macro bodies are identical,
298 // it's fine.
299 if (Existing.second || Existing.first == Known->second.first)
300 continue;
301
302 // The macro bodies differ; complain.
303 if (Diags) {
304 Diags->Report(diag::err_pch_macro_def_conflict)
305 << MacroName << Known->second.first << Existing.first;
306 }
307 return true;
308 }
309
310 // Check whether we're using predefines.
311 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines) {
312 if (Diags) {
313 Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
314 }
315 return true;
316 }
317
Douglas Gregor87699242012-10-25 00:07:54 +0000318 // Compute the #include and #include_macros lines we need.
319 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
320 StringRef File = ExistingPPOpts.Includes[I];
321 if (File == ExistingPPOpts.ImplicitPCHInclude)
322 continue;
323
324 if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File)
325 != PPOpts.Includes.end())
326 continue;
327
328 SuggestedPredefines += "#include \"";
329 SuggestedPredefines +=
330 HeaderSearch::NormalizeDashIncludePath(File, FileMgr);
331 SuggestedPredefines += "\"\n";
332 }
333
334 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
335 StringRef File = ExistingPPOpts.MacroIncludes[I];
336 if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(),
337 File)
338 != PPOpts.MacroIncludes.end())
339 continue;
340
341 SuggestedPredefines += "#__include_macros \"";
342 SuggestedPredefines +=
343 HeaderSearch::NormalizeDashIncludePath(File, FileMgr);
344 SuggestedPredefines += "\"\n##\n";
345 }
346
Douglas Gregor4c0c7e82012-10-24 23:41:50 +0000347 return false;
348}
349
350bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
Douglas Gregor87699242012-10-25 00:07:54 +0000351 bool Complain,
352 std::string &SuggestedPredefines) {
Douglas Gregor4c0c7e82012-10-24 23:41:50 +0000353 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
354
355 return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
Douglas Gregor87699242012-10-25 00:07:54 +0000356 Complain? &Reader.Diags : 0,
357 PP.getFileManager(),
358 SuggestedPredefines);
Douglas Gregor4c0c7e82012-10-24 23:41:50 +0000359}
360
361namespace {
Benjamin Kramer54353f42010-11-25 18:29:30 +0000362 struct EmptyStringRef {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000363 bool operator ()(StringRef r) const { return r.empty(); }
Benjamin Kramer54353f42010-11-25 18:29:30 +0000364 };
365 struct EmptyBlock {
366 bool operator ()(const PCHPredefinesBlock &r) const {return r.Data.empty();}
367 };
368}
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000369
Chris Lattner5f9e2722011-07-23 10:55:15 +0000370static bool EqualConcatenations(SmallVector<StringRef, 2> L,
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000371 PCHPredefinesBlocks R) {
372 // First, sum up the lengths.
373 unsigned LL = 0, RL = 0;
374 for (unsigned I = 0, N = L.size(); I != N; ++I) {
375 LL += L[I].size();
376 }
377 for (unsigned I = 0, N = R.size(); I != N; ++I) {
378 RL += R[I].Data.size();
379 }
380 if (LL != RL)
381 return false;
382 if (LL == 0 && RL == 0)
383 return true;
384
385 // Kick out empty parts, they confuse the algorithm below.
386 L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end());
387 R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end());
388
389 // Do it the hard way. At this point, both vectors must be non-empty.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000390 StringRef LR = L[0], RR = R[0].Data;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000391 unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size();
Daniel Dunbarc76c9e02010-07-16 00:00:11 +0000392 (void) RN;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000393 for (;;) {
394 // Compare the current pieces.
395 if (LR.size() == RR.size()) {
396 // If they're the same length, it's pretty easy.
397 if (LR != RR)
398 return false;
399 // Both pieces are done, advance.
400 ++LI;
401 ++RI;
402 // If either string is done, they're both done, since they're the same
403 // length.
404 if (LI == LN) {
405 assert(RI == RN && "Strings not the same length after all?");
406 return true;
407 }
408 LR = L[LI];
409 RR = R[RI].Data;
410 } else if (LR.size() < RR.size()) {
411 // Right piece is longer.
412 if (!RR.startswith(LR))
413 return false;
414 ++LI;
415 assert(LI != LN && "Strings not the same length after all?");
416 RR = RR.substr(LR.size());
417 LR = L[LI];
418 } else {
419 // Left piece is longer.
420 if (!LR.startswith(RR))
421 return false;
422 ++RI;
423 assert(RI != RN && "Strings not the same length after all?");
424 LR = LR.substr(RR.size());
425 RR = R[RI].Data;
426 }
427 }
428}
429
Chris Lattner5f9e2722011-07-23 10:55:15 +0000430static std::pair<FileID, StringRef::size_type>
431FindMacro(const PCHPredefinesBlocks &Buffers, StringRef MacroDef) {
432 std::pair<FileID, StringRef::size_type> Res;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000433 for (unsigned I = 0, N = Buffers.size(); I != N; ++I) {
434 Res.second = Buffers[I].Data.find(MacroDef);
Chris Lattner5f9e2722011-07-23 10:55:15 +0000435 if (Res.second != StringRef::npos) {
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000436 Res.first = Buffers[I].BufferID;
437 break;
438 }
439 }
440 return Res;
441}
442
443bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000444 StringRef OriginalFileName,
Nick Lewycky277a6e72011-02-23 21:16:44 +0000445 std::string &SuggestedPredefines,
Douglas Gregor38295be2012-10-22 23:51:00 +0000446 FileManager &FileMgr,
447 bool Complain) {
Daniel Dunbarc7162932009-11-11 23:58:53 +0000448 // We are in the context of an implicit include, so the predefines buffer will
449 // have a #include entry for the PCH file itself (as normalized by the
450 // preprocessor initialization). Find it and skip over it in the checking
451 // below.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000452 SmallString<256> PCHInclude;
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000453 PCHInclude += "#include \"";
Chandler Carruthcb381ea2011-12-09 01:33:57 +0000454 PCHInclude += HeaderSearch::NormalizeDashIncludePath(OriginalFileName,
455 FileMgr);
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000456 PCHInclude += "\"\n";
Chris Lattner5f9e2722011-07-23 10:55:15 +0000457 std::pair<StringRef,StringRef> Split =
458 StringRef(PP.getPredefines()).split(PCHInclude.str());
459 StringRef Left = Split.first, Right = Split.second;
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000460 if (Left == PP.getPredefines()) {
Douglas Gregor38295be2012-10-22 23:51:00 +0000461 if (Complain)
462 Error("Missing PCH include entry!");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000463 return true;
464 }
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000465
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000466 // If the concatenation of all the PCH buffers is equal to the adjusted
467 // command line, we're done.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000468 SmallVector<StringRef, 2> CommandLine;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000469 CommandLine.push_back(Left);
470 CommandLine.push_back(Right);
471 if (EqualConcatenations(CommandLine, Buffers))
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000472 return false;
473
474 SourceManager &SourceMgr = PP.getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000475
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000476 // The predefines buffers are different. Determine what the differences are,
477 // and whether they require us to reject the PCH file.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000478 SmallVector<StringRef, 8> PCHLines;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000479 for (unsigned I = 0, N = Buffers.size(); I != N; ++I)
480 Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Daniel Dunbare6750492009-11-13 16:46:11 +0000481
Chris Lattner5f9e2722011-07-23 10:55:15 +0000482 SmallVector<StringRef, 8> CmdLineLines;
Daniel Dunbare6750492009-11-13 16:46:11 +0000483 Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000484
485 // Pick out implicit #includes after the PCH and don't consider them for
486 // validation; we will insert them into SuggestedPredefines so that the
487 // preprocessor includes them.
488 std::string IncludesAfterPCH;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000489 SmallVector<StringRef, 8> AfterPCHLines;
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000490 Right.split(AfterPCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
491 for (unsigned i = 0, e = AfterPCHLines.size(); i != e; ++i) {
492 if (AfterPCHLines[i].startswith("#include ")) {
493 IncludesAfterPCH += AfterPCHLines[i];
494 IncludesAfterPCH += '\n';
495 } else {
496 CmdLineLines.push_back(AfterPCHLines[i]);
497 }
498 }
499
500 // Make sure we add the includes last into SuggestedPredefines before we
501 // exit this function.
502 struct AddIncludesRAII {
503 std::string &SuggestedPredefines;
504 std::string &IncludesAfterPCH;
505
506 AddIncludesRAII(std::string &SuggestedPredefines,
507 std::string &IncludesAfterPCH)
508 : SuggestedPredefines(SuggestedPredefines),
509 IncludesAfterPCH(IncludesAfterPCH) { }
510 ~AddIncludesRAII() {
511 SuggestedPredefines += IncludesAfterPCH;
512 }
513 } AddIncludes(SuggestedPredefines, IncludesAfterPCH);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000514
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000515 // Sort both sets of predefined buffer lines, since we allow some extra
516 // definitions and they may appear at any point in the output.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000517 std::sort(CmdLineLines.begin(), CmdLineLines.end());
518 std::sort(PCHLines.begin(), PCHLines.end());
519
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000520 // Determine which predefines that were used to build the PCH file are missing
521 // from the command line.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000522 std::vector<StringRef> MissingPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000523 std::set_difference(PCHLines.begin(), PCHLines.end(),
524 CmdLineLines.begin(), CmdLineLines.end(),
525 std::back_inserter(MissingPredefines));
526
527 bool MissingDefines = false;
528 bool ConflictingDefines = false;
529 for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000530 StringRef Missing = MissingPredefines[I];
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000531 if (Missing.startswith("#include ")) {
532 // An -include was specified when generating the PCH; it is included in
533 // the PCH, just ignore it.
534 continue;
535 }
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000536 if (!Missing.startswith("#define ")) {
Douglas Gregor38295be2012-10-22 23:51:00 +0000537 if (Complain)
538 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000539 return true;
540 }
Mike Stump1eb44332009-09-09 15:08:12 +0000541
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000542 // This is a macro definition. Determine the name of the macro we're
543 // defining.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000544 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000545 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000546 = Missing.find_first_of("( \n\r", StartOfMacroName);
547 assert(EndOfMacroName != std::string::npos &&
548 "Couldn't find the end of the macro name");
Chris Lattner5f9e2722011-07-23 10:55:15 +0000549 StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000550
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000551 // Determine whether this macro was given a different definition on the
552 // command line.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000553 std::string MacroDefStart = "#define " + MacroName.str();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000554 std::string::size_type MacroDefLen = MacroDefStart.size();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000555 SmallVector<StringRef, 8>::iterator ConflictPos
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000556 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
557 MacroDefStart);
558 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000559 if (!ConflictPos->startswith(MacroDefStart)) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000560 // Different macro; we're done.
561 ConflictPos = CmdLineLines.end();
Mike Stump1eb44332009-09-09 15:08:12 +0000562 break;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000563 }
Mike Stump1eb44332009-09-09 15:08:12 +0000564
565 assert(ConflictPos->size() > MacroDefLen &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000566 "Invalid #define in predefines buffer?");
Mike Stump1eb44332009-09-09 15:08:12 +0000567 if ((*ConflictPos)[MacroDefLen] != ' ' &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000568 (*ConflictPos)[MacroDefLen] != '(')
569 continue; // Longer macro name; keep trying.
Mike Stump1eb44332009-09-09 15:08:12 +0000570
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000571 // We found a conflicting macro definition.
572 break;
573 }
Mike Stump1eb44332009-09-09 15:08:12 +0000574
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000575 if (ConflictPos != CmdLineLines.end()) {
Douglas Gregor38295be2012-10-22 23:51:00 +0000576 if (!Complain)
577 return true;
578
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000579 Reader.Diag(diag::warn_cmdline_conflicting_macro_def)
580 << MacroName;
581
582 // Show the definition of this macro within the PCH file.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000583 std::pair<FileID, StringRef::size_type> MacroLoc =
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000584 FindMacro(Buffers, Missing);
Chris Lattner5f9e2722011-07-23 10:55:15 +0000585 assert(MacroLoc.second!=StringRef::npos && "Unable to find macro!");
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000586 SourceLocation PCHMissingLoc =
587 SourceMgr.getLocForStartOfFile(MacroLoc.first)
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +0000588 .getLocWithOffset(MacroLoc.second);
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000589 Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000590
591 ConflictingDefines = true;
592 continue;
593 }
Mike Stump1eb44332009-09-09 15:08:12 +0000594
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000595 // If the macro doesn't conflict, then we'll just pick up the macro
596 // definition from the PCH file. Warn the user that they made a mistake.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000597 if (ConflictingDefines)
598 continue; // Don't complain if there are already conflicting defs
Mike Stump1eb44332009-09-09 15:08:12 +0000599
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000600 if (!MissingDefines) {
Douglas Gregor38295be2012-10-22 23:51:00 +0000601 if (!Complain)
602 return true;
603
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000604 Reader.Diag(diag::warn_cmdline_missing_macro_defs);
605 MissingDefines = true;
606 }
607
Douglas Gregor38295be2012-10-22 23:51:00 +0000608 if (!Complain)
609 return true;
610
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000611 // Show the definition of this macro within the PCH file.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000612 std::pair<FileID, StringRef::size_type> MacroLoc =
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000613 FindMacro(Buffers, Missing);
Chris Lattner5f9e2722011-07-23 10:55:15 +0000614 assert(MacroLoc.second!=StringRef::npos && "Unable to find macro!");
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000615 SourceLocation PCHMissingLoc =
616 SourceMgr.getLocForStartOfFile(MacroLoc.first)
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +0000617 .getLocWithOffset(MacroLoc.second);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000618 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
619 }
Mike Stump1eb44332009-09-09 15:08:12 +0000620
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000621 if (ConflictingDefines)
622 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000623
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000624 // Determine what predefines were introduced based on command-line
625 // parameters that were not present when building the PCH
626 // file. Extra #defines are okay, so long as the identifiers being
627 // defined were not used within the precompiled header.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000628 std::vector<StringRef> ExtraPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000629 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
630 PCHLines.begin(), PCHLines.end(),
Mike Stump1eb44332009-09-09 15:08:12 +0000631 std::back_inserter(ExtraPredefines));
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000632 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000633 StringRef &Extra = ExtraPredefines[I];
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000634 if (!Extra.startswith("#define ")) {
Douglas Gregor38295be2012-10-22 23:51:00 +0000635 if (Complain)
636 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000637 return true;
638 }
639
640 // This is an extra macro definition. Determine the name of the
641 // macro we're defining.
642 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000643 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000644 = Extra.find_first_of("( \n\r", StartOfMacroName);
645 assert(EndOfMacroName != std::string::npos &&
646 "Couldn't find the end of the macro name");
Chris Lattner5f9e2722011-07-23 10:55:15 +0000647 StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000648
649 // Check whether this name was used somewhere in the PCH file. If
650 // so, defining it as a macro could change behavior, so we reject
651 // the PCH file.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000652 if (IdentifierInfo *II = Reader.get(MacroName)) {
Douglas Gregor38295be2012-10-22 23:51:00 +0000653 if (Complain)
654 Reader.Diag(diag::warn_macro_name_used_in_pch) << II;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000655 return true;
656 }
657
658 // Add this definition to the suggested predefines buffer.
659 SuggestedPredefines += Extra;
660 SuggestedPredefines += '\n';
661 }
662
663 // If we get here, it's because the predefines buffer had compatible
664 // contents. Accept the PCH file.
665 return false;
666}
667
Douglas Gregor12fab312010-03-16 16:35:32 +0000668void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
669 unsigned ID) {
670 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
671 ++NumHeaderInfos;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000672}
673
Argyrios Kyrtzidis62288ed2012-10-10 02:12:47 +0000674void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000675 PP.setCounterValue(Value);
676}
677
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000678//===----------------------------------------------------------------------===//
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000679// AST reader implementation
Douglas Gregor668c1a42009-04-21 22:25:48 +0000680//===----------------------------------------------------------------------===//
681
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000682void
Sebastian Redl571db7f2010-08-18 23:56:56 +0000683ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000684 DeserializationListener = Listener;
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000685}
686
Chris Lattner4c6f9522009-04-27 05:14:47 +0000687
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000688
Douglas Gregor98339b92011-08-25 20:47:51 +0000689unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
690 return serialization::ComputeHash(Sel);
691}
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000692
Mike Stump1eb44332009-09-09 15:08:12 +0000693
Douglas Gregor98339b92011-08-25 20:47:51 +0000694std::pair<unsigned, unsigned>
695ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
696 using namespace clang::io;
697 unsigned KeyLen = ReadUnalignedLE16(d);
698 unsigned DataLen = ReadUnalignedLE16(d);
699 return std::make_pair(KeyLen, DataLen);
700}
701
702ASTSelectorLookupTrait::internal_key_type
703ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
704 using namespace clang::io;
Douglas Gregor35942772011-09-09 21:34:22 +0000705 SelectorTable &SelTable = Reader.getContext().Selectors;
Douglas Gregor98339b92011-08-25 20:47:51 +0000706 unsigned N = ReadUnalignedLE16(d);
707 IdentifierInfo *FirstII
708 = Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
709 if (N == 0)
710 return SelTable.getNullarySelector(FirstII);
711 else if (N == 1)
712 return SelTable.getUnarySelector(FirstII);
713
714 SmallVector<IdentifierInfo *, 16> Args;
715 Args.push_back(FirstII);
716 for (unsigned I = 1; I != N; ++I)
717 Args.push_back(Reader.getLocalIdentifier(F, ReadUnalignedLE32(d)));
718
719 return SelTable.getSelector(N, Args.data());
720}
721
722ASTSelectorLookupTrait::data_type
723ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
724 unsigned DataLen) {
725 using namespace clang::io;
726
727 data_type Result;
728
729 Result.ID = Reader.getGlobalSelectorID(F, ReadUnalignedLE32(d));
730 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
731 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
732
733 // Load instance methods
Douglas Gregor98339b92011-08-25 20:47:51 +0000734 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
735 if (ObjCMethodDecl *Method
736 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d)))
737 Result.Instance.push_back(Method);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000738 }
Mike Stump1eb44332009-09-09 15:08:12 +0000739
Douglas Gregor98339b92011-08-25 20:47:51 +0000740 // Load factory methods
Douglas Gregor98339b92011-08-25 20:47:51 +0000741 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
742 if (ObjCMethodDecl *Method
743 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d)))
744 Result.Factory.push_back(Method);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000745 }
Mike Stump1eb44332009-09-09 15:08:12 +0000746
Douglas Gregor98339b92011-08-25 20:47:51 +0000747 return Result;
748}
Mike Stump1eb44332009-09-09 15:08:12 +0000749
Douglas Gregor98339b92011-08-25 20:47:51 +0000750unsigned ASTIdentifierLookupTrait::ComputeHash(const internal_key_type& a) {
751 return llvm::HashString(StringRef(a.first, a.second));
752}
Mike Stump1eb44332009-09-09 15:08:12 +0000753
Douglas Gregor98339b92011-08-25 20:47:51 +0000754std::pair<unsigned, unsigned>
755ASTIdentifierLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
756 using namespace clang::io;
757 unsigned DataLen = ReadUnalignedLE16(d);
758 unsigned KeyLen = ReadUnalignedLE16(d);
759 return std::make_pair(KeyLen, DataLen);
760}
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000761
Douglas Gregor98339b92011-08-25 20:47:51 +0000762std::pair<const char*, unsigned>
763ASTIdentifierLookupTrait::ReadKey(const unsigned char* d, unsigned n) {
764 assert(n >= 2 && d[n-1] == '\0');
765 return std::make_pair((const char*) d, n-1);
766}
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000767
Douglas Gregor98339b92011-08-25 20:47:51 +0000768IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
769 const unsigned char* d,
770 unsigned DataLen) {
771 using namespace clang::io;
772 unsigned RawID = ReadUnalignedLE32(d);
773 bool IsInteresting = RawID & 0x01;
Mike Stump1eb44332009-09-09 15:08:12 +0000774
Douglas Gregor98339b92011-08-25 20:47:51 +0000775 // Wipe out the "is interesting" bit.
776 RawID = RawID >> 1;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000777
Douglas Gregor98339b92011-08-25 20:47:51 +0000778 IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
779 if (!IsInteresting) {
780 // For uninteresting identifiers, just build the IdentifierInfo
781 // and associate it with the persistent ID.
Douglas Gregor668c1a42009-04-21 22:25:48 +0000782 IdentifierInfo *II = KnownII;
Douglas Gregor5d5051f2012-01-24 15:24:38 +0000783 if (!II) {
Douglas Gregor6ec60e02011-08-03 21:49:18 +0000784 II = &Reader.getIdentifierTable().getOwn(StringRef(k.first, k.second));
Douglas Gregor5d5051f2012-01-24 15:24:38 +0000785 KnownII = II;
786 }
Douglas Gregor668c1a42009-04-21 22:25:48 +0000787 Reader.SetIdentifierInfo(ID, II);
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000788 II->setIsFromAST();
Douglas Gregor057df202012-01-18 20:56:22 +0000789 Reader.markIdentifierUpToDate(II);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000790 return II;
791 }
Mike Stump1eb44332009-09-09 15:08:12 +0000792
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +0000793 unsigned ObjCOrBuiltinID = ReadUnalignedLE16(d);
Douglas Gregor98339b92011-08-25 20:47:51 +0000794 unsigned Bits = ReadUnalignedLE16(d);
795 bool CPlusPlusOperatorKeyword = Bits & 0x01;
796 Bits >>= 1;
797 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
798 Bits >>= 1;
799 bool Poisoned = Bits & 0x01;
800 Bits >>= 1;
801 bool ExtensionToken = Bits & 0x01;
802 Bits >>= 1;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +0000803 bool hadMacroDefinition = Bits & 0x01;
804 Bits >>= 1;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000805
Douglas Gregor98339b92011-08-25 20:47:51 +0000806 assert(Bits == 0 && "Extra bits in the identifier?");
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +0000807 DataLen -= 8;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000808
Douglas Gregor98339b92011-08-25 20:47:51 +0000809 // Build the IdentifierInfo itself and link the identifier ID with
810 // the new IdentifierInfo.
811 IdentifierInfo *II = KnownII;
Douglas Gregor5d5051f2012-01-24 15:24:38 +0000812 if (!II) {
Douglas Gregor98339b92011-08-25 20:47:51 +0000813 II = &Reader.getIdentifierTable().getOwn(StringRef(k.first, k.second));
Douglas Gregor5d5051f2012-01-24 15:24:38 +0000814 KnownII = II;
815 }
Douglas Gregor057df202012-01-18 20:56:22 +0000816 Reader.markIdentifierUpToDate(II);
Douglas Gregoreee242f2011-10-27 09:33:13 +0000817 II->setIsFromAST();
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000818
Douglas Gregor98339b92011-08-25 20:47:51 +0000819 // Set or check the various bits in the IdentifierInfo structure.
820 // Token IDs are read-only.
821 if (HasRevertedTokenIDToIdentifier)
822 II->RevertTokenIDToIdentifier();
823 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
824 assert(II->isExtensionToken() == ExtensionToken &&
825 "Incorrect extension token flag");
826 (void)ExtensionToken;
827 if (Poisoned)
828 II->setIsPoisoned(true);
829 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
830 "Incorrect C++ operator keyword flag");
831 (void)CPlusPlusOperatorKeyword;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000832
Douglas Gregor98339b92011-08-25 20:47:51 +0000833 // If this identifier is a macro, deserialize the macro
834 // definition.
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +0000835 if (hadMacroDefinition) {
Douglas Gregor6c6c54a2012-10-11 00:46:49 +0000836 SmallVector<MacroID, 4> MacroIDs;
837 while (uint32_t LocalID = ReadUnalignedLE32(d)) {
838 MacroIDs.push_back(Reader.getGlobalMacroID(F, LocalID));
839 DataLen -= 4;
Douglas Gregor13292642011-12-02 15:45:10 +0000840 }
Douglas Gregor6c6c54a2012-10-11 00:46:49 +0000841 DataLen -= 4;
842 Reader.setIdentifierIsMacro(II, MacroIDs);
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000843 }
844
Douglas Gregoreee242f2011-10-27 09:33:13 +0000845 Reader.SetIdentifierInfo(ID, II);
846
Douglas Gregor98339b92011-08-25 20:47:51 +0000847 // Read all of the declarations visible at global scope with this
848 // name.
Douglas Gregor98339b92011-08-25 20:47:51 +0000849 if (DataLen > 0) {
850 SmallVector<uint32_t, 4> DeclIDs;
851 for (; DataLen > 0; DataLen -= 4)
852 DeclIDs.push_back(Reader.getGlobalDeclID(F, ReadUnalignedLE32(d)));
853 Reader.SetGloballyVisibleDecls(II, DeclIDs);
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000854 }
855
Douglas Gregor98339b92011-08-25 20:47:51 +0000856 return II;
857}
Michael J. Spencer20249a12010-10-21 03:16:25 +0000858
Douglas Gregor98339b92011-08-25 20:47:51 +0000859unsigned
860ASTDeclContextNameLookupTrait::ComputeHash(const DeclNameKey &Key) const {
861 llvm::FoldingSetNodeID ID;
862 ID.AddInteger(Key.Kind);
863
864 switch (Key.Kind) {
865 case DeclarationName::Identifier:
866 case DeclarationName::CXXLiteralOperatorName:
867 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
868 break;
869 case DeclarationName::ObjCZeroArgSelector:
870 case DeclarationName::ObjCOneArgSelector:
871 case DeclarationName::ObjCMultiArgSelector:
872 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
873 break;
874 case DeclarationName::CXXOperatorName:
875 ID.AddInteger((OverloadedOperatorKind)Key.Data);
876 break;
877 case DeclarationName::CXXConstructorName:
878 case DeclarationName::CXXDestructorName:
879 case DeclarationName::CXXConversionFunctionName:
880 case DeclarationName::CXXUsingDirective:
881 break;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000882 }
883
Douglas Gregor98339b92011-08-25 20:47:51 +0000884 return ID.ComputeHash();
885}
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +0000886
Douglas Gregor98339b92011-08-25 20:47:51 +0000887ASTDeclContextNameLookupTrait::internal_key_type
888ASTDeclContextNameLookupTrait::GetInternalKey(
889 const external_key_type& Name) const {
890 DeclNameKey Key;
891 Key.Kind = Name.getNameKind();
892 switch (Name.getNameKind()) {
893 case DeclarationName::Identifier:
894 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
895 break;
896 case DeclarationName::ObjCZeroArgSelector:
897 case DeclarationName::ObjCOneArgSelector:
898 case DeclarationName::ObjCMultiArgSelector:
899 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
900 break;
901 case DeclarationName::CXXOperatorName:
902 Key.Data = Name.getCXXOverloadedOperator();
903 break;
904 case DeclarationName::CXXLiteralOperatorName:
905 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
906 break;
907 case DeclarationName::CXXConstructorName:
908 case DeclarationName::CXXDestructorName:
909 case DeclarationName::CXXConversionFunctionName:
910 case DeclarationName::CXXUsingDirective:
911 Key.Data = 0;
912 break;
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +0000913 }
914
Douglas Gregor98339b92011-08-25 20:47:51 +0000915 return Key;
916}
917
Douglas Gregor98339b92011-08-25 20:47:51 +0000918std::pair<unsigned, unsigned>
919ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
920 using namespace clang::io;
921 unsigned KeyLen = ReadUnalignedLE16(d);
922 unsigned DataLen = ReadUnalignedLE16(d);
923 return std::make_pair(KeyLen, DataLen);
924}
Michael J. Spencer20249a12010-10-21 03:16:25 +0000925
Douglas Gregor98339b92011-08-25 20:47:51 +0000926ASTDeclContextNameLookupTrait::internal_key_type
927ASTDeclContextNameLookupTrait::ReadKey(const unsigned char* d, unsigned) {
928 using namespace clang::io;
929
930 DeclNameKey Key;
931 Key.Kind = (DeclarationName::NameKind)*d++;
932 switch (Key.Kind) {
933 case DeclarationName::Identifier:
934 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
935 break;
936 case DeclarationName::ObjCZeroArgSelector:
937 case DeclarationName::ObjCOneArgSelector:
938 case DeclarationName::ObjCMultiArgSelector:
939 Key.Data =
940 (uint64_t)Reader.getLocalSelector(F, ReadUnalignedLE32(d))
941 .getAsOpaquePtr();
942 break;
943 case DeclarationName::CXXOperatorName:
944 Key.Data = *d++; // OverloadedOperatorKind
945 break;
946 case DeclarationName::CXXLiteralOperatorName:
947 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
948 break;
949 case DeclarationName::CXXConstructorName:
950 case DeclarationName::CXXDestructorName:
951 case DeclarationName::CXXConversionFunctionName:
952 case DeclarationName::CXXUsingDirective:
953 Key.Data = 0;
954 break;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000955 }
956
Douglas Gregor98339b92011-08-25 20:47:51 +0000957 return Key;
958}
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000959
Douglas Gregor98339b92011-08-25 20:47:51 +0000960ASTDeclContextNameLookupTrait::data_type
961ASTDeclContextNameLookupTrait::ReadData(internal_key_type,
962 const unsigned char* d,
Nick Lewyckyb346d2f2012-04-16 02:51:46 +0000963 unsigned DataLen) {
Douglas Gregor98339b92011-08-25 20:47:51 +0000964 using namespace clang::io;
965 unsigned NumDecls = ReadUnalignedLE16(d);
Douglas Gregor9b8b20f2012-01-06 16:09:53 +0000966 LE32DeclID *Start = (LE32DeclID *)d;
Douglas Gregor98339b92011-08-25 20:47:51 +0000967 return std::make_pair(Start, Start + NumDecls);
968}
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000969
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000970bool ASTReader::ReadDeclContextStorage(ModuleFile &M,
Douglas Gregor0d95f772011-08-24 19:03:07 +0000971 llvm::BitstreamCursor &Cursor,
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000972 const std::pair<uint64_t, uint64_t> &Offsets,
973 DeclContextInfo &Info) {
974 SavedStreamPosition SavedPosition(Cursor);
975 // First the lexical decls.
976 if (Offsets.first != 0) {
977 Cursor.JumpToBit(Offsets.first);
978
979 RecordData Record;
980 const char *Blob;
981 unsigned BlobLen;
982 unsigned Code = Cursor.ReadCode();
983 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
984 if (RecCode != DECL_CONTEXT_LEXICAL) {
985 Error("Expected lexical block");
986 return true;
987 }
988
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000989 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob);
990 Info.NumLexicalDecls = BlobLen / sizeof(KindDeclIDPair);
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000991 }
992
993 // Now the lookup table.
994 if (Offsets.second != 0) {
995 Cursor.JumpToBit(Offsets.second);
996
997 RecordData Record;
998 const char *Blob;
999 unsigned BlobLen;
1000 unsigned Code = Cursor.ReadCode();
1001 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
1002 if (RecCode != DECL_CONTEXT_VISIBLE) {
1003 Error("Expected visible lookup table block");
1004 return true;
1005 }
1006 Info.NameLookupTableData
1007 = ASTDeclContextNameLookupTable::Create(
1008 (const unsigned char *)Blob + Record[0],
1009 (const unsigned char *)Blob,
Douglas Gregor0d95f772011-08-24 19:03:07 +00001010 ASTDeclContextNameLookupTrait(*this, M));
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001011 }
1012
1013 return false;
1014}
1015
Chris Lattner5f9e2722011-07-23 10:55:15 +00001016void ASTReader::Error(StringRef Msg) {
Argyrios Kyrtzidis8d8f2c22011-04-25 22:23:56 +00001017 Error(diag::err_fe_pch_malformed, Msg);
1018}
1019
1020void ASTReader::Error(unsigned DiagID,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001021 StringRef Arg1, StringRef Arg2) {
Argyrios Kyrtzidis8d8f2c22011-04-25 22:23:56 +00001022 if (Diags.isDiagnosticInFlight())
1023 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
1024 else
1025 Diag(DiagID) << Arg1 << Arg2;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001026}
1027
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001028/// \brief Tell the AST listener about the predefines buffers in the chain.
Douglas Gregor38295be2012-10-22 23:51:00 +00001029bool ASTReader::CheckPredefinesBuffers(bool Complain) {
Douglas Gregore721f952009-04-28 18:58:38 +00001030 return false;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001031}
1032
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001033//===----------------------------------------------------------------------===//
1034// Source Manager Deserialization
1035//===----------------------------------------------------------------------===//
1036
Douglas Gregorbd945002009-04-13 16:31:14 +00001037/// \brief Read the line table in the source manager block.
Sebastian Redlc3632732010-10-05 15:59:54 +00001038/// \returns true if there was an error.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001039bool ASTReader::ParseLineTable(ModuleFile &F,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001040 SmallVectorImpl<uint64_t> &Record) {
Douglas Gregorbd945002009-04-13 16:31:14 +00001041 unsigned Idx = 0;
1042 LineTableInfo &LineTable = SourceMgr.getLineTable();
1043
1044 // Parse the file names
Douglas Gregorff0a9872009-04-13 17:12:42 +00001045 std::map<int, int> FileIDs;
1046 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregorbd945002009-04-13 16:31:14 +00001047 // Extract the file name
1048 unsigned FilenameLen = Record[Idx++];
1049 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
1050 Idx += FilenameLen;
Douglas Gregorcaed0602012-10-18 21:31:35 +00001051 MaybeAddSystemRootToFilename(F, Filename);
Jay Foad65aa6882011-06-21 15:13:30 +00001052 FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
Douglas Gregorbd945002009-04-13 16:31:14 +00001053 }
1054
1055 // Parse the line entries
1056 std::vector<LineEntry> Entries;
1057 while (Idx < Record.size()) {
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +00001058 int FID = Record[Idx++];
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001059 assert(FID >= 0 && "Serialized line entries for non-local file.");
1060 // Remap FileID from 1-based old view.
1061 FID += F.SLocEntryBaseID - 1;
Douglas Gregorbd945002009-04-13 16:31:14 +00001062
1063 // Extract the line entries
1064 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +00001065 assert(NumEntries && "Numentries is 00000");
Douglas Gregorbd945002009-04-13 16:31:14 +00001066 Entries.clear();
1067 Entries.reserve(NumEntries);
1068 for (unsigned I = 0; I != NumEntries; ++I) {
1069 unsigned FileOffset = Record[Idx++];
1070 unsigned LineNo = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +00001071 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump1eb44332009-09-09 15:08:12 +00001072 SrcMgr::CharacteristicKind FileKind
Douglas Gregorbd945002009-04-13 16:31:14 +00001073 = (SrcMgr::CharacteristicKind)Record[Idx++];
1074 unsigned IncludeOffset = Record[Idx++];
1075 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1076 FileKind, IncludeOffset));
1077 }
Douglas Gregor47d9de62012-06-08 16:40:28 +00001078 LineTable.AddEntry(FileID::get(FID), Entries);
Douglas Gregorbd945002009-04-13 16:31:14 +00001079 }
1080
1081 return false;
1082}
1083
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001084namespace {
1085
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001086class ASTStatData {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001087public:
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001088 const ino_t ino;
1089 const dev_t dev;
1090 const mode_t mode;
1091 const time_t mtime;
1092 const off_t size;
Mike Stump1eb44332009-09-09 15:08:12 +00001093
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001094 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Chris Lattner74e976b2010-11-23 19:28:12 +00001095 : ino(i), dev(d), mode(mo), mtime(m), size(s) {}
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001096};
1097
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001098class ASTStatLookupTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001099 public:
1100 typedef const char *external_key_type;
1101 typedef const char *internal_key_type;
1102
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001103 typedef ASTStatData data_type;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001104
1105 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001106 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001107 }
1108
1109 static internal_key_type GetInternalKey(const char *path) { return path; }
1110
1111 static bool EqualKey(internal_key_type a, internal_key_type b) {
1112 return strcmp(a, b) == 0;
1113 }
1114
1115 static std::pair<unsigned, unsigned>
1116 ReadKeyDataLength(const unsigned char*& d) {
1117 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1118 unsigned DataLen = (unsigned) *d++;
1119 return std::make_pair(KeyLen + 1, DataLen);
1120 }
1121
1122 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
1123 return (const char *)d;
1124 }
1125
1126 static data_type ReadData(const internal_key_type, const unsigned char *d,
1127 unsigned /*DataLen*/) {
1128 using namespace clang::io;
1129
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001130 ino_t ino = (ino_t) ReadUnalignedLE32(d);
1131 dev_t dev = (dev_t) ReadUnalignedLE32(d);
1132 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump1eb44332009-09-09 15:08:12 +00001133 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001134 off_t size = (off_t) ReadUnalignedLE64(d);
1135 return data_type(ino, dev, mode, mtime, size);
1136 }
1137};
1138
1139/// \brief stat() cache for precompiled headers.
1140///
1141/// This cache is very similar to the stat cache used by pretokenized
1142/// headers.
Chris Lattner10e286a2010-11-23 19:19:34 +00001143class ASTStatCache : public FileSystemStatCache {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001144 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001145 CacheTy *Cache;
1146
1147 unsigned &NumStatHits, &NumStatMisses;
Mike Stump1eb44332009-09-09 15:08:12 +00001148public:
Chris Lattner74e976b2010-11-23 19:28:12 +00001149 ASTStatCache(const unsigned char *Buckets, const unsigned char *Base,
1150 unsigned &NumStatHits, unsigned &NumStatMisses)
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001151 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
1152 Cache = CacheTy::Create(Buckets, Base);
1153 }
1154
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001155 ~ASTStatCache() { delete Cache; }
Mike Stump1eb44332009-09-09 15:08:12 +00001156
Chris Lattner898a0612010-11-23 21:17:56 +00001157 LookupResult getStat(const char *Path, struct stat &StatBuf,
1158 int *FileDescriptor) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001159 // Do the lookup for the file's data in the AST file.
Chris Lattner10e286a2010-11-23 19:19:34 +00001160 CacheTy::iterator I = Cache->find(Path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001161
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001162 // If we don't get a hit in the AST file just forward to 'stat'.
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001163 if (I == Cache->end()) {
1164 ++NumStatMisses;
Chris Lattner898a0612010-11-23 21:17:56 +00001165 return statChained(Path, StatBuf, FileDescriptor);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001166 }
Mike Stump1eb44332009-09-09 15:08:12 +00001167
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001168 ++NumStatHits;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001169 ASTStatData Data = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001170
Chris Lattner10e286a2010-11-23 19:19:34 +00001171 StatBuf.st_ino = Data.ino;
1172 StatBuf.st_dev = Data.dev;
1173 StatBuf.st_mtime = Data.mtime;
1174 StatBuf.st_mode = Data.mode;
1175 StatBuf.st_size = Data.size;
Chris Lattnerd6f61112010-11-23 20:05:15 +00001176 return CacheExists;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001177 }
1178};
1179} // end anonymous namespace
1180
1181
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001182/// \brief Read a source manager block
Douglas Gregor4825fd72012-10-22 22:50:17 +00001183bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001184 using namespace SrcMgr;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001185
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001186 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001187
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001188 // Set the source-location entry cursor to the current position in
1189 // the stream. This cursor will be used to read the contents of the
1190 // source manager block initially, and then lazily read
1191 // source-location entries as needed.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001192 SLocEntryCursor = F.Stream;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001193
1194 // The stream itself is going to skip over the source manager block.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001195 if (F.Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001196 Error("malformed block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001197 return true;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001198 }
1199
1200 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001201 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001202 Error("malformed source manager block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001203 return true;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001204 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001205
Douglas Gregor14f79002009-04-10 03:52:48 +00001206 RecordData Record;
1207 while (true) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001208 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregor14f79002009-04-10 03:52:48 +00001209 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001210 if (SLocEntryCursor.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001211 Error("error at end of Source Manager block in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001212 return true;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001213 }
Douglas Gregor4825fd72012-10-22 22:50:17 +00001214 return false;
Douglas Gregor14f79002009-04-10 03:52:48 +00001215 }
Mike Stump1eb44332009-09-09 15:08:12 +00001216
Douglas Gregor14f79002009-04-10 03:52:48 +00001217 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1218 // No known subblocks, always skip them.
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001219 SLocEntryCursor.ReadSubBlockID();
1220 if (SLocEntryCursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001221 Error("malformed block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001222 return true;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001223 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001224 continue;
1225 }
Mike Stump1eb44332009-09-09 15:08:12 +00001226
Douglas Gregor14f79002009-04-10 03:52:48 +00001227 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001228 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregor14f79002009-04-10 03:52:48 +00001229 continue;
1230 }
Mike Stump1eb44332009-09-09 15:08:12 +00001231
Douglas Gregor14f79002009-04-10 03:52:48 +00001232 // Read a record.
1233 const char *BlobStart;
1234 unsigned BlobLen;
1235 Record.clear();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001236 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001237 default: // Default behavior: ignore.
1238 break;
1239
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001240 case SM_SLOC_FILE_ENTRY:
1241 case SM_SLOC_BUFFER_ENTRY:
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001242 case SM_SLOC_EXPANSION_ENTRY:
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001243 // Once we hit one of the source location entries, we're done.
Douglas Gregor4825fd72012-10-22 22:50:17 +00001244 return false;
Douglas Gregor14f79002009-04-10 03:52:48 +00001245 }
1246 }
1247}
1248
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001249/// \brief If a header file is not found at the path that we expect it to be
1250/// and the PCH file was moved from its original location, try to resolve the
1251/// file by assuming that header+PCH were moved together and the header is in
1252/// the same place relative to the PCH.
1253static std::string
1254resolveFileRelativeToOriginalDir(const std::string &Filename,
1255 const std::string &OriginalDir,
1256 const std::string &CurrDir) {
1257 assert(OriginalDir != CurrDir &&
1258 "No point trying to resolve the file if the PCH dir didn't change");
1259 using namespace llvm::sys;
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001260 SmallString<128> filePath(Filename);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001261 fs::make_absolute(filePath);
1262 assert(path::is_absolute(OriginalDir));
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001263 SmallString<128> currPCHPath(CurrDir);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001264
1265 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1266 fileDirE = path::end(path::parent_path(filePath));
1267 path::const_iterator origDirI = path::begin(OriginalDir),
1268 origDirE = path::end(OriginalDir);
1269 // Skip the common path components from filePath and OriginalDir.
1270 while (fileDirI != fileDirE && origDirI != origDirE &&
1271 *fileDirI == *origDirI) {
1272 ++fileDirI;
1273 ++origDirI;
1274 }
1275 for (; origDirI != origDirE; ++origDirI)
1276 path::append(currPCHPath, "..");
1277 path::append(currPCHPath, fileDirI, fileDirE);
1278 path::append(currPCHPath, path::filename(Filename));
1279 return currPCHPath.str();
1280}
1281
Douglas Gregor8b53d142012-10-22 22:53:10 +00001282bool ASTReader::ReadSLocEntry(int ID) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001283 if (ID == 0)
Douglas Gregor4825fd72012-10-22 22:50:17 +00001284 return false;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001285
Douglas Gregor0cdd7982011-07-21 18:46:38 +00001286 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001287 Error("source location entry ID out-of-range for AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001288 return true;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001289 }
1290
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001291 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001292 F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]);
Sebastian Redlc3632732010-10-05 15:59:54 +00001293 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001294 unsigned BaseOffset = F->SLocEntryBaseOffset;
Sebastian Redl9137a522010-07-16 17:50:48 +00001295
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001296 ++NumSLocEntriesRead;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001297 unsigned Code = SLocEntryCursor.ReadCode();
1298 if (Code == llvm::bitc::END_BLOCK ||
1299 Code == llvm::bitc::ENTER_SUBBLOCK ||
1300 Code == llvm::bitc::DEFINE_ABBREV) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001301 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001302 return true;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001303 }
1304
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001305 RecordData Record;
1306 const char *BlobStart;
1307 unsigned BlobLen;
1308 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1309 default:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001310 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001311 return true;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001312
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001313 case SM_SLOC_FILE_ENTRY: {
Argyrios Kyrtzidisa4c29b62012-02-20 23:58:07 +00001314 // We will detect whether a file changed and return 'Failure' for it, but
1315 // we will also try to fail gracefully by setting up the SLocEntry.
Douglas Gregora930dc92012-10-22 18:42:04 +00001316 unsigned InputID = Record[4];
1317 InputFile IF = getInputFile(*F, InputID);
1318 const FileEntry *File = IF.getPointer();
1319 bool OverriddenBuffer = IF.getInt();
Argyrios Kyrtzidisa4c29b62012-02-20 23:58:07 +00001320
Douglas Gregora930dc92012-10-22 18:42:04 +00001321 if (!IF.getPointer())
Douglas Gregor4825fd72012-10-22 22:50:17 +00001322 return true;
Douglas Gregor2d52be52010-03-21 22:49:54 +00001323
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001324 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
Douglas Gregor72a9ae12011-07-22 16:00:58 +00001325 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001326 // This is the module's main file.
1327 IncludeLoc = getImportLocation(F);
1328 }
Argyrios Kyrtzidisff398962012-07-11 20:59:04 +00001329 SrcMgr::CharacteristicKind
1330 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1331 FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001332 ID, BaseOffset + Record[0]);
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001333 SrcMgr::FileInfo &FileInfo =
1334 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
Douglas Gregora930dc92012-10-22 18:42:04 +00001335 FileInfo.NumCreatedFIDs = Record[5];
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001336 if (Record[3])
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001337 FileInfo.setHasLineDirectives();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001338
Douglas Gregora930dc92012-10-22 18:42:04 +00001339 const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
1340 unsigned NumFileDecls = Record[7];
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001341 if (NumFileDecls) {
1342 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
Argyrios Kyrtzidis9d128d02011-10-31 07:20:08 +00001343 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1344 NumFileDecls));
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001345 }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001346
Douglas Gregor35f9ae62011-11-17 01:44:33 +00001347 const SrcMgr::ContentCache *ContentCache
Argyrios Kyrtzidisff398962012-07-11 20:59:04 +00001348 = SourceMgr.getOrCreateContentCache(File,
1349 /*isSystemFile=*/FileCharacter != SrcMgr::C_User);
Douglas Gregor35f9ae62011-11-17 01:44:33 +00001350 if (OverriddenBuffer && !ContentCache->BufferOverridden &&
1351 ContentCache->ContentsEntry == ContentCache->OrigEntry) {
Douglas Gregora081da52011-11-16 20:05:18 +00001352 unsigned Code = SLocEntryCursor.ReadCode();
1353 Record.clear();
1354 unsigned RecCode
1355 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
1356
1357 if (RecCode != SM_SLOC_BUFFER_BLOB) {
1358 Error("AST record has invalid code");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001359 return true;
Douglas Gregora081da52011-11-16 20:05:18 +00001360 }
1361
1362 llvm::MemoryBuffer *Buffer
1363 = llvm::MemoryBuffer::getMemBuffer(StringRef(BlobStart, BlobLen - 1),
Douglas Gregora930dc92012-10-22 18:42:04 +00001364 File->getName());
Douglas Gregora081da52011-11-16 20:05:18 +00001365 SourceMgr.overrideFileContents(File, Buffer);
1366 }
Argyrios Kyrtzidisa4c29b62012-02-20 23:58:07 +00001367
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001368 break;
1369 }
1370
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001371 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001372 const char *Name = BlobStart;
1373 unsigned Offset = Record[0];
1374 unsigned Code = SLocEntryCursor.ReadCode();
1375 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001376 unsigned RecCode
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001377 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001378
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001379 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001380 Error("AST record has invalid code");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001381 return true;
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001382 }
1383
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001384 llvm::MemoryBuffer *Buffer
Douglas Gregora081da52011-11-16 20:05:18 +00001385 = llvm::MemoryBuffer::getMemBuffer(StringRef(BlobStart, BlobLen - 1),
1386 Name);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001387 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID,
1388 BaseOffset + Offset);
Mike Stump1eb44332009-09-09 15:08:12 +00001389
Douglas Gregor6236a292011-12-02 21:56:05 +00001390 if (strcmp(Name, "<built-in>") == 0 && F->Kind == MK_PCH) {
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +00001391 PCHPredefinesBlock Block = {
1392 BufferID,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001393 StringRef(BlobStart, BlobLen - 1)
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +00001394 };
1395 PCHPredefinesBuffers.push_back(Block);
Douglas Gregor92b059e2009-04-28 20:33:11 +00001396 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001397
1398 break;
1399 }
1400
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001401 case SM_SLOC_EXPANSION_ENTRY: {
Sebastian Redlc3632732010-10-05 15:59:54 +00001402 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
Chandler Carruthbf340e42011-07-26 03:03:05 +00001403 SourceMgr.createExpansionLoc(SpellingLoc,
Sebastian Redlc3632732010-10-05 15:59:54 +00001404 ReadSourceLocation(*F, Record[2]),
1405 ReadSourceLocation(*F, Record[3]),
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001406 Record[4],
1407 ID,
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001408 BaseOffset + Record[0]);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001409 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001410 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001411 }
1412
Douglas Gregor4825fd72012-10-22 22:50:17 +00001413 return false;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001414}
1415
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001416/// \brief Find the location where the module F is imported.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001417SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001418 if (F->ImportLoc.isValid())
1419 return F->ImportLoc;
Jonathan D. Turner2e091632011-07-29 18:09:09 +00001420
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001421 // Otherwise we have a PCH. It's considered to be "imported" at the first
1422 // location of its includer.
Jonathan D. Turner2e091632011-07-29 18:09:09 +00001423 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001424 // Main file is the importer. We assume that it is the first entry in the
1425 // entry table. We can't ask the manager, because at the time of PCH loading
1426 // the main file entry doesn't exist yet.
1427 // The very first entry is the invalid instantiation loc, which takes up
1428 // offsets 0 and 1.
1429 return SourceLocation::getFromRawEncoding(2U);
1430 }
Jonathan D. Turner2e091632011-07-29 18:09:09 +00001431 //return F->Loaders[0]->FirstLoc;
1432 return F->ImportedBy[0]->FirstLoc;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001433}
1434
Chris Lattner6367f6d2009-04-27 01:05:14 +00001435/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1436/// specified cursor. Read the abbreviations that are at the top of the block
1437/// and then leave the cursor pointing into the block.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001438bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattner6367f6d2009-04-27 01:05:14 +00001439 unsigned BlockID) {
1440 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001441 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001442 return Failure;
1443 }
Mike Stump1eb44332009-09-09 15:08:12 +00001444
Chris Lattner6367f6d2009-04-27 01:05:14 +00001445 while (true) {
Douglas Gregorecdcb882010-10-20 22:00:55 +00001446 uint64_t Offset = Cursor.GetCurrentBitNo();
Chris Lattner6367f6d2009-04-27 01:05:14 +00001447 unsigned Code = Cursor.ReadCode();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001448
Chris Lattner6367f6d2009-04-27 01:05:14 +00001449 // We expect all abbrevs to be at the start of the block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001450 if (Code != llvm::bitc::DEFINE_ABBREV) {
1451 Cursor.JumpToBit(Offset);
Chris Lattner6367f6d2009-04-27 01:05:14 +00001452 return false;
Douglas Gregorecdcb882010-10-20 22:00:55 +00001453 }
Chris Lattner6367f6d2009-04-27 01:05:14 +00001454 Cursor.ReadAbbrevRecord();
1455 }
1456}
1457
Douglas Gregor3ab50fe2012-10-11 17:41:54 +00001458void ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset,
1459 MacroInfo *Hint) {
Douglas Gregorecdcb882010-10-20 22:00:55 +00001460 llvm::BitstreamCursor &Stream = F.MacroCursor;
Mike Stump1eb44332009-09-09 15:08:12 +00001461
Douglas Gregor37e26842009-04-21 23:56:24 +00001462 // Keep track of where we are in the stream, then jump back there
1463 // after reading this macro.
1464 SavedStreamPosition SavedPosition(Stream);
1465
1466 Stream.JumpToBit(Offset);
1467 RecordData Record;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001468 SmallVector<IdentifierInfo*, 16> MacroArgs;
Douglas Gregor37e26842009-04-21 23:56:24 +00001469 MacroInfo *Macro = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001470
Douglas Gregore8219a62012-10-11 21:07:39 +00001471 // RAII object to add the loaded macro information once we're done
1472 // adding tokens.
1473 struct AddLoadedMacroInfoRAII {
1474 Preprocessor &PP;
1475 MacroInfo *Hint;
1476 MacroInfo *MI;
1477 IdentifierInfo *II;
1478
1479 AddLoadedMacroInfoRAII(Preprocessor &PP, MacroInfo *Hint)
1480 : PP(PP), Hint(Hint), MI(), II() { }
1481 ~AddLoadedMacroInfoRAII( ) {
1482 if (MI) {
1483 // Finally, install the macro.
1484 PP.addLoadedMacroInfo(II, MI, Hint);
1485 }
1486 }
1487 } AddLoadedMacroInfo(PP, Hint);
1488
Douglas Gregor37e26842009-04-21 23:56:24 +00001489 while (true) {
1490 unsigned Code = Stream.ReadCode();
1491 switch (Code) {
1492 case llvm::bitc::END_BLOCK:
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001493 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001494
1495 case llvm::bitc::ENTER_SUBBLOCK:
1496 // No known subblocks, always skip them.
1497 Stream.ReadSubBlockID();
1498 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001499 Error("malformed block record in AST file");
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001500 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001501 }
1502 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001503
Douglas Gregor37e26842009-04-21 23:56:24 +00001504 case llvm::bitc::DEFINE_ABBREV:
1505 Stream.ReadAbbrevRecord();
1506 continue;
1507 default: break;
1508 }
1509
1510 // Read a record.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001511 const char *BlobStart = 0;
1512 unsigned BlobLen = 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001513 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001514 PreprocessorRecordTypes RecType =
Michael J. Spencer20249a12010-10-21 03:16:25 +00001515 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart,
Douglas Gregorecdcb882010-10-20 22:00:55 +00001516 BlobLen);
Douglas Gregor37e26842009-04-21 23:56:24 +00001517 switch (RecType) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001518 case PP_MACRO_OBJECT_LIKE:
1519 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001520 // If we already have a macro, that means that we've hit the end
1521 // of the definition of the macro we were looking for. We're
1522 // done.
1523 if (Macro)
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001524 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001525
Douglas Gregor95eab172011-07-28 20:55:49 +00001526 IdentifierInfo *II = getLocalIdentifier(F, Record[0]);
Douglas Gregor37e26842009-04-21 23:56:24 +00001527 if (II == 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001528 Error("macro must have a name in AST file");
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001529 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001530 }
Mike Stump1eb44332009-09-09 15:08:12 +00001531
Douglas Gregora8235d62012-10-09 23:05:51 +00001532 unsigned GlobalID = getGlobalMacroID(F, Record[1]);
1533
1534 // If this macro has already been loaded, don't do so again.
1535 if (MacrosLoaded[GlobalID - NUM_PREDEF_MACRO_IDS])
1536 return;
1537
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001538 SubmoduleID GlobalSubmoduleID = getGlobalSubmoduleID(F, Record[2]);
1539 unsigned NextIndex = 3;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001540 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001541 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001542
Douglas Gregora8235d62012-10-09 23:05:51 +00001543 // Record this macro.
1544 MacrosLoaded[GlobalID - NUM_PREDEF_MACRO_IDS] = MI;
1545
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001546 SourceLocation UndefLoc = ReadSourceLocation(F, Record, NextIndex);
1547 if (UndefLoc.isValid())
1548 MI->setUndefLoc(UndefLoc);
1549
1550 MI->setIsUsed(Record[NextIndex++]);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001551 MI->setIsFromAST();
Mike Stump1eb44332009-09-09 15:08:12 +00001552
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001553 bool IsPublic = Record[NextIndex++];
Douglas Gregoraa93a872011-10-17 15:32:29 +00001554 MI->setVisibility(IsPublic, ReadSourceLocation(F, Record, NextIndex));
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001555
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001556 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregor37e26842009-04-21 23:56:24 +00001557 // Decode function-like macro info.
Douglas Gregor7143aab2011-09-01 17:04:32 +00001558 bool isC99VarArgs = Record[NextIndex++];
1559 bool isGNUVarArgs = Record[NextIndex++];
Douglas Gregor37e26842009-04-21 23:56:24 +00001560 MacroArgs.clear();
Douglas Gregor7143aab2011-09-01 17:04:32 +00001561 unsigned NumArgs = Record[NextIndex++];
Douglas Gregor37e26842009-04-21 23:56:24 +00001562 for (unsigned i = 0; i != NumArgs; ++i)
Douglas Gregor7143aab2011-09-01 17:04:32 +00001563 MacroArgs.push_back(getLocalIdentifier(F, Record[NextIndex++]));
Douglas Gregor37e26842009-04-21 23:56:24 +00001564
1565 // Install function-like macro info.
1566 MI->setIsFunctionLike();
1567 if (isC99VarArgs) MI->setIsC99Varargs();
1568 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor75fdb232009-05-22 22:45:36 +00001569 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001570 PP.getPreprocessorAllocator());
Douglas Gregor37e26842009-04-21 23:56:24 +00001571 }
1572
Douglas Gregora8235d62012-10-09 23:05:51 +00001573 if (DeserializationListener)
1574 DeserializationListener->MacroRead(GlobalID, MI);
1575
1576 // If an update record marked this as undefined, do so now.
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001577 // FIXME: Only if the submodule this update came from is visible?
Douglas Gregora8235d62012-10-09 23:05:51 +00001578 MacroUpdatesMap::iterator Update = MacroUpdates.find(GlobalID);
1579 if (Update != MacroUpdates.end()) {
1580 if (MI->getUndefLoc().isInvalid()) {
Douglas Gregor54c8a402012-10-12 00:16:50 +00001581 for (unsigned I = 0, N = Update->second.size(); I != N; ++I) {
1582 bool Hidden = false;
1583 if (unsigned SubmoduleID = Update->second[I].first) {
1584 if (Module *Owner = getSubmodule(SubmoduleID)) {
1585 if (Owner->NameVisibility == Module::Hidden) {
1586 // Note that this #undef is hidden.
1587 Hidden = true;
1588
1589 // Record this hiding for later.
1590 HiddenNamesMap[Owner].push_back(
1591 HiddenName(II, MI, Update->second[I].second.UndefLoc));
1592 }
1593 }
1594 }
1595
1596 if (!Hidden) {
1597 MI->setUndefLoc(Update->second[I].second.UndefLoc);
1598 if (PPMutationListener *Listener = PP.getPPMutationListener())
1599 Listener->UndefinedMacro(MI);
1600 break;
1601 }
1602 }
Douglas Gregora8235d62012-10-09 23:05:51 +00001603 }
1604 MacroUpdates.erase(Update);
1605 }
1606
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001607 // Determine whether this macro definition is visible.
1608 bool Hidden = !MI->isPublic();
1609 if (!Hidden && GlobalSubmoduleID) {
1610 if (Module *Owner = getSubmodule(GlobalSubmoduleID)) {
1611 if (Owner->NameVisibility == Module::Hidden) {
1612 // The owning module is not visible, and this macro definition
1613 // should not be, either.
1614 Hidden = true;
1615
1616 // Note that this macro definition was hidden because its owning
1617 // module is not yet visible.
1618 HiddenNamesMap[Owner].push_back(HiddenName(II, MI));
1619 }
1620 }
1621 }
1622 MI->setHidden(Hidden);
1623
Douglas Gregore8219a62012-10-11 21:07:39 +00001624 // Make sure we install the macro once we're done.
1625 AddLoadedMacroInfo.MI = MI;
1626 AddLoadedMacroInfo.II = II;
Douglas Gregor37e26842009-04-21 23:56:24 +00001627
1628 // Remember that we saw this macro last so that we add the tokens that
1629 // form its body to it.
1630 Macro = MI;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001631
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001632 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1633 Record[NextIndex]) {
1634 // We have a macro definition. Register the association
1635 PreprocessedEntityID
1636 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1637 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1638 PPRec.RegisterMacroDefinition(Macro,
1639 PPRec.getPPEntityID(GlobalID-1, /*isLoaded=*/true));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001640 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001641
Douglas Gregor37e26842009-04-21 23:56:24 +00001642 ++NumMacrosRead;
1643 break;
1644 }
Mike Stump1eb44332009-09-09 15:08:12 +00001645
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001646 case PP_TOKEN: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001647 // If we see a TOKEN before a PP_MACRO_*, then the file is
1648 // erroneous, just pretend we didn't see this.
1649 if (Macro == 0) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001650
Douglas Gregor37e26842009-04-21 23:56:24 +00001651 Token Tok;
1652 Tok.startToken();
Sebastian Redlc3632732010-10-05 15:59:54 +00001653 Tok.setLocation(ReadSourceLocation(F, Record[0]));
Douglas Gregor37e26842009-04-21 23:56:24 +00001654 Tok.setLength(Record[1]);
Douglas Gregor95eab172011-07-28 20:55:49 +00001655 if (IdentifierInfo *II = getLocalIdentifier(F, Record[2]))
Douglas Gregor37e26842009-04-21 23:56:24 +00001656 Tok.setIdentifierInfo(II);
1657 Tok.setKind((tok::TokenKind)Record[3]);
1658 Tok.setFlag((Token::TokenFlags)Record[4]);
1659 Macro->AddTokenToBody(Tok);
1660 break;
1661 }
David Blaikie7530c032012-01-17 06:56:22 +00001662 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001663 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001664}
1665
Douglas Gregor86c67d82011-07-28 22:39:26 +00001666PreprocessedEntityID
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001667ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const {
Argyrios Kyrtzidis1f6d2252011-09-19 20:40:02 +00001668 ContinuousRangeMap<uint32_t, int, 2>::const_iterator
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001669 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1670 assert(I != M.PreprocessedEntityRemap.end()
1671 && "Invalid index into preprocessed entity index remap");
1672
1673 return LocalID + I->second;
Douglas Gregor86c67d82011-07-28 22:39:26 +00001674}
1675
Douglas Gregor98339b92011-08-25 20:47:51 +00001676unsigned HeaderFileInfoTrait::ComputeHash(const char *path) {
1677 return llvm::HashString(llvm::sys::path::filename(path));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001678}
Douglas Gregor98339b92011-08-25 20:47:51 +00001679
1680HeaderFileInfoTrait::internal_key_type
1681HeaderFileInfoTrait::GetInternalKey(const char *path) { return path; }
1682
1683bool HeaderFileInfoTrait::EqualKey(internal_key_type a, internal_key_type b) {
1684 if (strcmp(a, b) == 0)
1685 return true;
1686
1687 if (llvm::sys::path::filename(a) != llvm::sys::path::filename(b))
1688 return false;
Douglas Gregor99a922b2011-12-09 16:22:07 +00001689
1690 // Determine whether the actual files are equivalent.
1691 bool Result = false;
1692 if (llvm::sys::fs::equivalent(a, b, Result))
Douglas Gregor98339b92011-08-25 20:47:51 +00001693 return false;
1694
Douglas Gregor99a922b2011-12-09 16:22:07 +00001695 return Result;
Douglas Gregor98339b92011-08-25 20:47:51 +00001696}
1697
1698std::pair<unsigned, unsigned>
1699HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1700 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1701 unsigned DataLen = (unsigned) *d++;
1702 return std::make_pair(KeyLen + 1, DataLen);
1703}
1704
1705HeaderFileInfoTrait::data_type
1706HeaderFileInfoTrait::ReadData(const internal_key_type, const unsigned char *d,
1707 unsigned DataLen) {
1708 const unsigned char *End = d + DataLen;
1709 using namespace clang::io;
1710 HeaderFileInfo HFI;
1711 unsigned Flags = *d++;
1712 HFI.isImport = (Flags >> 5) & 0x01;
1713 HFI.isPragmaOnce = (Flags >> 4) & 0x01;
1714 HFI.DirInfo = (Flags >> 2) & 0x03;
1715 HFI.Resolved = (Flags >> 1) & 0x01;
1716 HFI.IndexHeaderMapHeader = Flags & 0x01;
1717 HFI.NumIncludes = ReadUnalignedLE16(d);
Douglas Gregor541ba162011-10-17 18:53:12 +00001718 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(M,
1719 ReadUnalignedLE32(d));
Douglas Gregor98339b92011-08-25 20:47:51 +00001720 if (unsigned FrameworkOffset = ReadUnalignedLE32(d)) {
1721 // The framework offset is 1 greater than the actual offset,
1722 // since 0 is used as an indicator for "no framework name".
1723 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1724 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1725 }
1726
1727 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1728 (void)End;
1729
1730 // This HeaderFileInfo was externally loaded.
1731 HFI.External = true;
1732 return HFI;
1733}
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001734
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001735void ASTReader::setIdentifierIsMacro(IdentifierInfo *II, ArrayRef<MacroID> IDs){
1736 II->setHadMacroDefinition(true);
1737 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1738 PendingMacroIDs[II].append(IDs.begin(), IDs.end());
Douglas Gregor295a2a62010-10-30 00:23:06 +00001739}
1740
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001741void ASTReader::ReadDefinedMacros() {
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001742 // Note that we are loading defined macros.
1743 Deserializing Macros(this);
1744
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00001745 for (ModuleReverseIterator I = ModuleMgr.rbegin(),
1746 E = ModuleMgr.rend(); I != E; ++I) {
1747 llvm::BitstreamCursor &MacroCursor = (*I)->MacroCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001748
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001749 // If there was no preprocessor block, skip this file.
1750 if (!MacroCursor.getBitStreamReader())
1751 continue;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001752
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001753 llvm::BitstreamCursor Cursor = MacroCursor;
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00001754 Cursor.JumpToBit((*I)->MacroStartOffset);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001755
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001756 RecordData Record;
1757 while (true) {
1758 unsigned Code = Cursor.ReadCode();
Douglas Gregorecdcb882010-10-20 22:00:55 +00001759 if (Code == llvm::bitc::END_BLOCK)
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001760 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001761
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001762 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1763 // No known subblocks, always skip them.
1764 Cursor.ReadSubBlockID();
1765 if (Cursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001766 Error("malformed block record in AST file");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001767 return;
1768 }
1769 continue;
1770 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001771
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001772 if (Code == llvm::bitc::DEFINE_ABBREV) {
1773 Cursor.ReadAbbrevRecord();
1774 continue;
1775 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001776
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001777 // Read a record.
1778 const char *BlobStart;
1779 unsigned BlobLen;
1780 Record.clear();
1781 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1782 default: // Default behavior: ignore.
1783 break;
Douglas Gregor88a35862010-01-04 19:18:44 +00001784
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001785 case PP_MACRO_OBJECT_LIKE:
1786 case PP_MACRO_FUNCTION_LIKE:
Douglas Gregor95eab172011-07-28 20:55:49 +00001787 getLocalIdentifier(**I, Record[0]);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001788 break;
1789
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001790 case PP_TOKEN:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001791 // Ignore tokens.
1792 break;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001793 }
Douglas Gregor88a35862010-01-04 19:18:44 +00001794 }
1795 }
Douglas Gregor295a2a62010-10-30 00:23:06 +00001796}
1797
Douglas Gregoreee242f2011-10-27 09:33:13 +00001798namespace {
1799 /// \brief Visitor class used to look up identifirs in an AST file.
1800 class IdentifierLookupVisitor {
1801 StringRef Name;
Douglas Gregor057df202012-01-18 20:56:22 +00001802 unsigned PriorGeneration;
Douglas Gregoreee242f2011-10-27 09:33:13 +00001803 IdentifierInfo *Found;
1804 public:
Douglas Gregor057df202012-01-18 20:56:22 +00001805 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration)
1806 : Name(Name), PriorGeneration(PriorGeneration), Found() { }
Douglas Gregoreee242f2011-10-27 09:33:13 +00001807
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001808 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00001809 IdentifierLookupVisitor *This
1810 = static_cast<IdentifierLookupVisitor *>(UserData);
1811
Douglas Gregor057df202012-01-18 20:56:22 +00001812 // If we've already searched this module file, skip it now.
1813 if (M.Generation <= This->PriorGeneration)
1814 return true;
1815
Douglas Gregoreee242f2011-10-27 09:33:13 +00001816 ASTIdentifierLookupTable *IdTable
1817 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
1818 if (!IdTable)
1819 return false;
1820
Douglas Gregor5d5051f2012-01-24 15:24:38 +00001821 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(),
1822 M, This->Found);
1823
Douglas Gregoreee242f2011-10-27 09:33:13 +00001824 std::pair<const char*, unsigned> Key(This->Name.begin(),
1825 This->Name.size());
Douglas Gregor5d5051f2012-01-24 15:24:38 +00001826 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Trait);
Douglas Gregoreee242f2011-10-27 09:33:13 +00001827 if (Pos == IdTable->end())
1828 return false;
1829
1830 // Dereferencing the iterator has the effect of building the
1831 // IdentifierInfo node and populating it with the various
1832 // declarations it needs.
1833 This->Found = *Pos;
1834 return true;
1835 }
1836
1837 // \brief Retrieve the identifier info found within the module
1838 // files.
1839 IdentifierInfo *getIdentifierInfo() const { return Found; }
1840 };
1841}
1842
1843void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001844 // Note that we are loading an identifier.
1845 Deserializing AnIdentifier(this);
1846
Douglas Gregor057df202012-01-18 20:56:22 +00001847 unsigned PriorGeneration = 0;
David Blaikie4e4d0842012-03-11 07:00:24 +00001848 if (getContext().getLangOpts().Modules)
Douglas Gregor057df202012-01-18 20:56:22 +00001849 PriorGeneration = IdentifierGeneration[&II];
1850
1851 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration);
1852 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
1853 markIdentifierUpToDate(&II);
1854}
1855
1856void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
1857 if (!II)
1858 return;
1859
1860 II->setOutOfDate(false);
1861
1862 // Update the generation for this identifier.
David Blaikie4e4d0842012-03-11 07:00:24 +00001863 if (getContext().getLangOpts().Modules)
Douglas Gregor057df202012-01-18 20:56:22 +00001864 IdentifierGeneration[II] = CurrentGeneration;
Douglas Gregoreee242f2011-10-27 09:33:13 +00001865}
1866
Douglas Gregora930dc92012-10-22 18:42:04 +00001867llvm::PointerIntPair<const FileEntry *, 1, bool>
Douglas Gregor38295be2012-10-22 23:51:00 +00001868ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
Douglas Gregora930dc92012-10-22 18:42:04 +00001869 // If this ID is bogus, just return an empty input file.
1870 if (ID == 0 || ID > F.InputFilesLoaded.size())
1871 return InputFile();
1872
1873 // If we've already loaded this input file, return it.
1874 if (F.InputFilesLoaded[ID-1].getPointer())
1875 return F.InputFilesLoaded[ID-1];
1876
1877 // Go find this input file.
1878 llvm::BitstreamCursor &Cursor = F.InputFilesCursor;
1879 SavedStreamPosition SavedPosition(Cursor);
1880 Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
1881
1882 unsigned Code = Cursor.ReadCode();
1883 RecordData Record;
1884 const char *BlobStart = 0;
1885 unsigned BlobLen = 0;
1886 switch ((InputFileRecordTypes)Cursor.ReadRecord(Code, Record,
1887 &BlobStart, &BlobLen)) {
1888 case INPUT_FILE: {
1889 unsigned StoredID = Record[0];
1890 assert(ID == StoredID && "Bogus stored ID or offset");
NAKAMURA Takumi6b8194e2012-10-22 21:50:39 +00001891 (void)StoredID;
Douglas Gregora930dc92012-10-22 18:42:04 +00001892 off_t StoredSize = (off_t)Record[1];
1893 time_t StoredTime = (time_t)Record[2];
1894 bool Overridden = (bool)Record[3];
1895
1896 // Get the file entry for this input file.
1897 StringRef OrigFilename(BlobStart, BlobLen);
1898 std::string Filename = OrigFilename;
1899 MaybeAddSystemRootToFilename(F, Filename);
1900 const FileEntry *File
1901 = Overridden? FileMgr.getVirtualFile(Filename, StoredSize, StoredTime)
1902 : FileMgr.getFile(Filename, /*OpenFile=*/false);
1903
1904 // If we didn't find the file, resolve it relative to the
1905 // original directory from which this AST file was created.
1906 if (File == 0 && !F.OriginalDir.empty() && !CurrentDir.empty() &&
1907 F.OriginalDir != CurrentDir) {
1908 std::string Resolved = resolveFileRelativeToOriginalDir(Filename,
1909 F.OriginalDir,
1910 CurrentDir);
1911 if (!Resolved.empty())
1912 File = FileMgr.getFile(Resolved);
1913 }
1914
1915 // For an overridden file, create a virtual file with the stored
1916 // size/timestamp.
1917 if (Overridden && File == 0) {
1918 File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
1919 }
1920
1921 if (File == 0) {
Douglas Gregor38295be2012-10-22 23:51:00 +00001922 if (Complain) {
1923 std::string ErrorStr = "could not find file '";
1924 ErrorStr += Filename;
1925 ErrorStr += "' referenced by AST file";
1926 Error(ErrorStr.c_str());
1927 }
Douglas Gregora930dc92012-10-22 18:42:04 +00001928 return InputFile();
1929 }
1930
1931 // Note that we've loaded this input file.
1932 F.InputFilesLoaded[ID-1] = InputFile(File, Overridden);
1933
1934 // Check if there was a request to override the contents of the file
1935 // that was part of the precompiled header. Overridding such a file
1936 // can lead to problems when lexing using the source locations from the
1937 // PCH.
1938 SourceManager &SM = getSourceManager();
1939 if (!Overridden && SM.isFileOverridden(File)) {
1940 Error(diag::err_fe_pch_file_overridden, Filename);
1941 // After emitting the diagnostic, recover by disabling the override so
1942 // that the original file will be used.
1943 SM.disableFileContentsOverride(File);
1944 // The FileEntry is a virtual file entry with the size of the contents
1945 // that would override the original contents. Set it to the original's
1946 // size/time.
1947 FileMgr.modifyFileEntry(const_cast<FileEntry*>(File),
1948 StoredSize, StoredTime);
1949 }
1950
1951 // For an overridden file, there is nothing to validate.
1952 if (Overridden)
1953 return InputFile(File, Overridden);
1954
1955 // The stat info from the FileEntry came from the cached stat
1956 // info of the PCH, so we cannot trust it.
1957 struct stat StatBuf;
1958 if (::stat(File->getName(), &StatBuf) != 0) {
1959 StatBuf.st_size = File->getSize();
1960 StatBuf.st_mtime = File->getModificationTime();
1961 }
1962
1963 if ((StoredSize != StatBuf.st_size
1964#if !defined(LLVM_ON_WIN32)
1965 // In our regression testing, the Windows file system seems to
1966 // have inconsistent modification times that sometimes
1967 // erroneously trigger this error-handling path.
1968 || StoredTime != StatBuf.st_mtime
1969#endif
1970 )) {
Douglas Gregor38295be2012-10-22 23:51:00 +00001971 if (Complain)
1972 Error(diag::err_fe_pch_file_modified, Filename);
1973
Douglas Gregora930dc92012-10-22 18:42:04 +00001974 return InputFile();
1975 }
1976
1977 return InputFile(File, Overridden);
1978 }
1979 }
1980
1981 return InputFile();
1982}
1983
Chris Lattner5f9e2722011-07-23 10:55:15 +00001984const FileEntry *ASTReader::getFileEntry(StringRef filenameStrRef) {
Douglas Gregor69e16082012-10-18 21:47:16 +00001985 ModuleFile &M = ModuleMgr.getPrimaryModule();
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00001986 std::string Filename = filenameStrRef;
Douglas Gregor69e16082012-10-18 21:47:16 +00001987 MaybeAddSystemRootToFilename(M, Filename);
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00001988 const FileEntry *File = FileMgr.getFile(Filename);
Douglas Gregor69e16082012-10-18 21:47:16 +00001989 if (File == 0 && !M.OriginalDir.empty() && !CurrentDir.empty() &&
1990 M.OriginalDir != CurrentDir) {
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00001991 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
Douglas Gregor69e16082012-10-18 21:47:16 +00001992 M.OriginalDir,
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00001993 CurrentDir);
1994 if (!resolved.empty())
1995 File = FileMgr.getFile(resolved);
1996 }
1997
1998 return File;
1999}
2000
Douglas Gregore650c8c2009-07-07 00:12:59 +00002001/// \brief If we are loading a relocatable PCH file, and the filename is
2002/// not an absolute path, add the system root to the beginning of the file
2003/// name.
Douglas Gregora930dc92012-10-22 18:42:04 +00002004StringRef ASTReader::MaybeAddSystemRootToFilename(ModuleFile &M,
2005 std::string &Filename) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00002006 // If this is not a relocatable PCH file, there's nothing to do.
Douglas Gregorcaed0602012-10-18 21:31:35 +00002007 if (!M.RelocatablePCH)
Douglas Gregora930dc92012-10-22 18:42:04 +00002008 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +00002009
Michael J. Spencer256053b2010-12-17 21:22:22 +00002010 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
Douglas Gregora930dc92012-10-22 18:42:04 +00002011 return Filename;
Douglas Gregore650c8c2009-07-07 00:12:59 +00002012
Douglas Gregor832d6202011-07-22 16:35:34 +00002013 if (isysroot.empty()) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00002014 // If no system root was given, default to '/'
2015 Filename.insert(Filename.begin(), '/');
Douglas Gregora930dc92012-10-22 18:42:04 +00002016 return Filename;
Douglas Gregore650c8c2009-07-07 00:12:59 +00002017 }
Mike Stump1eb44332009-09-09 15:08:12 +00002018
Douglas Gregor832d6202011-07-22 16:35:34 +00002019 unsigned Length = isysroot.size();
Douglas Gregore650c8c2009-07-07 00:12:59 +00002020 if (isysroot[Length - 1] != '/')
2021 Filename.insert(Filename.begin(), '/');
Mike Stump1eb44332009-09-09 15:08:12 +00002022
Douglas Gregor832d6202011-07-22 16:35:34 +00002023 Filename.insert(Filename.begin(), isysroot.begin(), isysroot.end());
Douglas Gregora930dc92012-10-22 18:42:04 +00002024 return Filename;
Douglas Gregore650c8c2009-07-07 00:12:59 +00002025}
2026
Douglas Gregor38295be2012-10-22 23:51:00 +00002027ASTReader::ASTReadResult
2028ASTReader::ReadControlBlock(ModuleFile &F,
2029 llvm::SmallVectorImpl<ModuleFile *> &Loaded,
2030 unsigned ClientLoadCapabilities) {
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002031 llvm::BitstreamCursor &Stream = F.Stream;
2032
2033 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
2034 Error("malformed block record in AST file");
2035 return Failure;
2036 }
2037
2038 // Read all of the records and blocks in the control block.
2039 RecordData Record;
2040 while (!Stream.AtEndOfStream()) {
2041 unsigned Code = Stream.ReadCode();
2042 if (Code == llvm::bitc::END_BLOCK) {
2043 if (Stream.ReadBlockEnd()) {
2044 Error("error at end of control block in AST file");
2045 return Failure;
2046 }
2047
Douglas Gregora930dc92012-10-22 18:42:04 +00002048 // Validate all of the input files.
2049 if (!DisableValidation) {
Douglas Gregor38295be2012-10-22 23:51:00 +00002050 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
Douglas Gregora930dc92012-10-22 18:42:04 +00002051 for (unsigned I = 0, N = Record[0]; I < N; ++I)
Douglas Gregor38295be2012-10-22 23:51:00 +00002052 if (!getInputFile(F, I+1, Complain).getPointer())
Douglas Gregor4825fd72012-10-22 22:50:17 +00002053 return OutOfDate;
Douglas Gregora930dc92012-10-22 18:42:04 +00002054 }
2055
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002056 return Success;
2057 }
2058
2059 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
Douglas Gregor745e6f12012-10-19 00:38:02 +00002060 switch (Stream.ReadSubBlockID()) {
2061 case INPUT_FILES_BLOCK_ID:
Douglas Gregora930dc92012-10-22 18:42:04 +00002062 F.InputFilesCursor = Stream;
2063 if (Stream.SkipBlock() || // Skip with the main cursor
2064 // Read the abbreviations
2065 ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
2066 Error("malformed block record in AST file");
Douglas Gregor745e6f12012-10-19 00:38:02 +00002067 return Failure;
Douglas Gregor745e6f12012-10-19 00:38:02 +00002068 }
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002069 continue;
Douglas Gregor745e6f12012-10-19 00:38:02 +00002070
2071 default:
2072 if (!Stream.SkipBlock())
2073 continue;
2074 break;
2075 }
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002076
2077 Error("malformed block record in AST file");
2078 return Failure;
2079 }
2080
2081 if (Code == llvm::bitc::DEFINE_ABBREV) {
2082 Stream.ReadAbbrevRecord();
2083 continue;
2084 }
2085
2086 // Read and process a record.
2087 Record.clear();
2088 const char *BlobStart = 0;
2089 unsigned BlobLen = 0;
2090 switch ((ControlRecordTypes)Stream.ReadRecord(Code, Record,
2091 &BlobStart, &BlobLen)) {
2092 case METADATA: {
2093 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
Douglas Gregor38295be2012-10-22 23:51:00 +00002094 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2095 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
2096 : diag::warn_pch_version_too_new);
Douglas Gregor4825fd72012-10-22 22:50:17 +00002097 return VersionMismatch;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002098 }
2099
2100 bool hasErrors = Record[5];
2101 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
2102 Diag(diag::err_pch_with_compiler_errors);
Douglas Gregor4825fd72012-10-22 22:50:17 +00002103 return HadErrors;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002104 }
2105
Douglas Gregorcaed0602012-10-18 21:31:35 +00002106 F.RelocatablePCH = Record[4];
Douglas Gregor7ae467f2012-10-18 18:27:37 +00002107
2108 const std::string &CurBranch = getClangFullRepositoryVersion();
2109 StringRef ASTBranch(BlobStart, BlobLen);
2110 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
Douglas Gregor38295be2012-10-22 23:51:00 +00002111 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2112 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregor4825fd72012-10-22 22:50:17 +00002113 return VersionMismatch;
Douglas Gregor7ae467f2012-10-18 18:27:37 +00002114 }
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002115 break;
2116 }
2117
2118 case IMPORTS: {
2119 // Load each of the imported PCH files.
2120 unsigned Idx = 0, N = Record.size();
2121 while (Idx < N) {
2122 // Read information about the AST file.
2123 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
2124 unsigned Length = Record[Idx++];
2125 SmallString<128> ImportedFile(Record.begin() + Idx,
2126 Record.begin() + Idx + Length);
2127 Idx += Length;
2128
2129 // Load the AST file.
Douglas Gregor38295be2012-10-22 23:51:00 +00002130 switch(ReadASTCore(ImportedFile, ImportedKind, &F, Loaded,
2131 ClientLoadCapabilities)) {
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002132 case Failure: return Failure;
2133 // If we have to ignore the dependency, we'll have to ignore this too.
Douglas Gregor4825fd72012-10-22 22:50:17 +00002134 case OutOfDate: return OutOfDate;
2135 case VersionMismatch: return VersionMismatch;
2136 case ConfigurationMismatch: return ConfigurationMismatch;
2137 case HadErrors: return HadErrors;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002138 case Success: break;
2139 }
2140 }
2141 break;
2142 }
2143
Douglas Gregor38295be2012-10-22 23:51:00 +00002144 case LANGUAGE_OPTIONS: {
2145 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2146 if (Listener && &F == *ModuleMgr.begin() &&
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00002147 ParseLanguageOptions(Record, Complain, *Listener) &&
2148 !DisableValidation)
Douglas Gregor4825fd72012-10-22 22:50:17 +00002149 return ConfigurationMismatch;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002150 break;
Douglas Gregor38295be2012-10-22 23:51:00 +00002151 }
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002152
Douglas Gregoree097c12012-10-18 17:58:09 +00002153 case TARGET_OPTIONS: {
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00002154 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2155 if (Listener && &F == *ModuleMgr.begin() &&
2156 ParseTargetOptions(Record, Complain, *Listener) &&
2157 !DisableValidation)
2158 return ConfigurationMismatch;
Douglas Gregoree097c12012-10-18 17:58:09 +00002159 break;
2160 }
2161
Douglas Gregor5f3d8222012-10-24 15:17:15 +00002162 case DIAGNOSTIC_OPTIONS: {
2163 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2164 if (Listener && &F == *ModuleMgr.begin() &&
2165 ParseDiagnosticOptions(Record, Complain, *Listener) &&
2166 !DisableValidation)
2167 return ConfigurationMismatch;
2168 break;
2169 }
Douglas Gregor1b2c3c02012-10-24 15:49:58 +00002170
2171 case FILE_SYSTEM_OPTIONS: {
2172 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2173 if (Listener && &F == *ModuleMgr.begin() &&
2174 ParseFileSystemOptions(Record, Complain, *Listener) &&
2175 !DisableValidation)
2176 return ConfigurationMismatch;
2177 break;
2178 }
2179
Douglas Gregorbbf38312012-10-24 16:50:34 +00002180 case HEADER_SEARCH_OPTIONS: {
2181 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2182 if (Listener && &F == *ModuleMgr.begin() &&
2183 ParseHeaderSearchOptions(Record, Complain, *Listener) &&
2184 !DisableValidation)
2185 return ConfigurationMismatch;
2186 break;
2187 }
2188
Douglas Gregora71a7d82012-10-24 20:05:57 +00002189 case PREPROCESSOR_OPTIONS: {
2190 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2191 if (Listener && &F == *ModuleMgr.begin() &&
Douglas Gregor87699242012-10-25 00:07:54 +00002192 ParsePreprocessorOptions(Record, Complain, *Listener,
2193 SuggestedPredefines) &&
Douglas Gregora71a7d82012-10-24 20:05:57 +00002194 !DisableValidation)
2195 return ConfigurationMismatch;
2196 break;
2197 }
2198
Douglas Gregor39c497b2012-10-18 18:36:53 +00002199 case ORIGINAL_FILE:
Douglas Gregor69e16082012-10-18 21:47:16 +00002200 F.OriginalSourceFileID = FileID::get(Record[0]);
2201 F.ActualOriginalSourceFileName.assign(BlobStart, BlobLen);
2202 F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
2203 MaybeAddSystemRootToFilename(F, F.OriginalSourceFileName);
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002204 break;
2205
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002206 case ORIGINAL_PCH_DIR:
Douglas Gregor69e16082012-10-18 21:47:16 +00002207 F.OriginalDir.assign(BlobStart, BlobLen);
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002208 break;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002209
Douglas Gregora930dc92012-10-22 18:42:04 +00002210 case INPUT_FILE_OFFSETS:
2211 F.InputFileOffsets = (const uint32_t *)BlobStart;
2212 F.InputFilesLoaded.resize(Record[0]);
Douglas Gregor745e6f12012-10-19 00:38:02 +00002213 break;
2214 }
Douglas Gregor745e6f12012-10-19 00:38:02 +00002215 }
2216
2217 Error("premature end of bitstream in AST file");
2218 return Failure;
2219}
2220
Douglas Gregor4825fd72012-10-22 22:50:17 +00002221bool ASTReader::ReadASTBlock(ModuleFile &F) {
Sebastian Redl9137a522010-07-16 17:50:48 +00002222 llvm::BitstreamCursor &Stream = F.Stream;
2223
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002224 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002225 Error("malformed block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002226 return true;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002227 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002228
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002229 // Read all of the records and blocks for the AST file.
Douglas Gregor8038d512009-04-10 17:25:41 +00002230 RecordData Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002231 while (!Stream.AtEndOfStream()) {
2232 unsigned Code = Stream.ReadCode();
2233 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002234 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002235 Error("error at end of module block in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002236 return true;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002237 }
Chris Lattner7356a312009-04-11 21:15:38 +00002238
Argyrios Kyrtzidis1f941242012-09-21 01:30:00 +00002239 DeclContext *DC = Context.getTranslationUnitDecl();
2240 if (!DC->hasExternalVisibleStorage() && DC->hasExternalLexicalStorage())
2241 DC->setMustBuildLookupTable();
2242
Douglas Gregor4825fd72012-10-22 22:50:17 +00002243 return false;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002244 }
2245
2246 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2247 switch (Stream.ReadSubBlockID()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002248 case DECLTYPES_BLOCK_ID:
Chris Lattner6367f6d2009-04-27 01:05:14 +00002249 // We lazily load the decls block, but we want to set up the
2250 // DeclsCursor cursor to point into it. Clone our current bitcode
2251 // cursor to it, enter the block and read the abbrevs in that block.
2252 // With the main cursor, we just skip over it.
Sebastian Redl9137a522010-07-16 17:50:48 +00002253 F.DeclsCursor = Stream;
Chris Lattner6367f6d2009-04-27 01:05:14 +00002254 if (Stream.SkipBlock() || // Skip with the main cursor.
2255 // Read the abbrevs.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002256 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002257 Error("malformed block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002258 return true;
Chris Lattner6367f6d2009-04-27 01:05:14 +00002259 }
2260 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002261
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002262 case DECL_UPDATES_BLOCK_ID:
2263 if (Stream.SkipBlock()) {
2264 Error("malformed block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002265 return true;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002266 }
2267 break;
2268
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002269 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl9137a522010-07-16 17:50:48 +00002270 F.MacroCursor = Stream;
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002271 if (!PP.getExternalSource())
2272 PP.setExternalSource(this);
Douglas Gregor88a35862010-01-04 19:18:44 +00002273
Douglas Gregorecdcb882010-10-20 22:00:55 +00002274 if (Stream.SkipBlock() ||
2275 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002276 Error("malformed block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002277 return true;
Chris Lattner7356a312009-04-11 21:15:38 +00002278 }
Douglas Gregorecdcb882010-10-20 22:00:55 +00002279 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
Chris Lattner7356a312009-04-11 21:15:38 +00002280 break;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002281
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002282 case PREPROCESSOR_DETAIL_BLOCK_ID:
2283 F.PreprocessorDetailCursor = Stream;
2284 if (Stream.SkipBlock() ||
2285 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
2286 PREPROCESSOR_DETAIL_BLOCK_ID)) {
2287 Error("malformed preprocessor detail record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002288 return true;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002289 }
2290 F.PreprocessorDetailStartOffset
2291 = F.PreprocessorDetailCursor.GetCurrentBitNo();
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002292
2293 if (!PP.getPreprocessingRecord())
Argyrios Kyrtzidisc6c54522012-03-05 05:48:17 +00002294 PP.createPreprocessingRecord(/*RecordConditionalDirectives=*/false);
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002295 if (!PP.getPreprocessingRecord()->getExternalSource())
2296 PP.getPreprocessingRecord()->SetExternalSource(*this);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002297 break;
2298
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002299 case SOURCE_MANAGER_BLOCK_ID:
Douglas Gregor4825fd72012-10-22 22:50:17 +00002300 if (ReadSourceManagerBlock(F))
2301 return true;
Douglas Gregor14f79002009-04-10 03:52:48 +00002302 break;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002303
2304 case SUBMODULE_BLOCK_ID:
Douglas Gregor4825fd72012-10-22 22:50:17 +00002305 if (ReadSubmoduleBlock(F))
2306 return true;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002307 break;
2308
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002309 case COMMENTS_BLOCK_ID: {
2310 llvm::BitstreamCursor C = Stream;
2311 if (Stream.SkipBlock() ||
2312 ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
2313 Error("malformed comments block in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002314 return true;
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002315 }
2316 CommentsCursors.push_back(std::make_pair(C, &F));
2317 break;
2318 }
2319
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002320 default:
2321 if (!Stream.SkipBlock())
2322 break;
2323 Error("malformed block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002324 return true;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002325 }
Douglas Gregor8038d512009-04-10 17:25:41 +00002326 continue;
2327 }
2328
2329 if (Code == llvm::bitc::DEFINE_ABBREV) {
2330 Stream.ReadAbbrevRecord();
2331 continue;
2332 }
2333
2334 // Read and process a record.
2335 Record.clear();
Douglas Gregor2bec0412009-04-10 21:16:55 +00002336 const char *BlobStart = 0;
2337 unsigned BlobLen = 0;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002338 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00002339 &BlobStart, &BlobLen)) {
Douglas Gregor8038d512009-04-10 17:25:41 +00002340 default: // Default behavior: ignore.
2341 break;
2342
Douglas Gregora119da02011-08-02 16:26:37 +00002343 case TYPE_OFFSET: {
Sebastian Redl12d6da02010-07-19 22:06:55 +00002344 if (F.LocalNumTypes != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002345 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002346 return true;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002347 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00002348 F.TypeOffsets = (const uint32_t *)BlobStart;
2349 F.LocalNumTypes = Record[0];
Douglas Gregore3605012011-08-02 18:32:54 +00002350 unsigned LocalBaseTypeIndex = Record[1];
2351 F.BaseTypeIndex = getTotalNumTypes();
Douglas Gregor1e849b62011-07-29 00:21:44 +00002352
Douglas Gregora119da02011-08-02 16:26:37 +00002353 if (F.LocalNumTypes > 0) {
2354 // Introduce the global -> local mapping for types within this module.
2355 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
2356
2357 // Introduce the local -> global mapping for types within this module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00002358 F.TypeRemap.insertOrReplace(
2359 std::make_pair(LocalBaseTypeIndex,
2360 F.BaseTypeIndex - LocalBaseTypeIndex));
Douglas Gregora119da02011-08-02 16:26:37 +00002361
2362 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
2363 }
Douglas Gregor8038d512009-04-10 17:25:41 +00002364 break;
Douglas Gregora119da02011-08-02 16:26:37 +00002365 }
2366
Douglas Gregor496c7092011-08-03 15:48:04 +00002367 case DECL_OFFSET: {
Sebastian Redl12d6da02010-07-19 22:06:55 +00002368 if (F.LocalNumDecls != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002369 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002370 return true;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002371 }
Argyrios Kyrtzidis9d31fa72011-10-27 18:47:35 +00002372 F.DeclOffsets = (const DeclOffset *)BlobStart;
Sebastian Redl12d6da02010-07-19 22:06:55 +00002373 F.LocalNumDecls = Record[0];
Douglas Gregor496c7092011-08-03 15:48:04 +00002374 unsigned LocalBaseDeclID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00002375 F.BaseDeclID = getTotalNumDecls();
Douglas Gregor96e973f2011-07-20 00:27:43 +00002376
Douglas Gregor496c7092011-08-03 15:48:04 +00002377 if (F.LocalNumDecls > 0) {
2378 // Introduce the global -> local mapping for declarations within this
2379 // module.
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002380 GlobalDeclMap.insert(
2381 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
Douglas Gregor496c7092011-08-03 15:48:04 +00002382
2383 // Introduce the local -> global mapping for declarations within this
2384 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00002385 F.DeclRemap.insertOrReplace(
2386 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
Douglas Gregor496c7092011-08-03 15:48:04 +00002387
Douglas Gregora1be2782011-12-17 23:38:30 +00002388 // Introduce the global -> local mapping for declarations within this
2389 // module.
2390 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
2391
Douglas Gregor496c7092011-08-03 15:48:04 +00002392 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
2393 }
Douglas Gregor8038d512009-04-10 17:25:41 +00002394 break;
Douglas Gregor496c7092011-08-03 15:48:04 +00002395 }
2396
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002397 case TU_UPDATE_LEXICAL: {
Douglas Gregor35942772011-09-09 21:34:22 +00002398 DeclContext *TU = Context.getTranslationUnitDecl();
Douglas Gregor0d95f772011-08-24 19:03:07 +00002399 DeclContextInfo &Info = F.DeclContextInfos[TU];
2400 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(BlobStart);
2401 Info.NumLexicalDecls
2402 = static_cast<unsigned int>(BlobLen / sizeof(KindDeclIDPair));
Douglas Gregor35942772011-09-09 21:34:22 +00002403 TU->setHasExternalLexicalStorage(true);
Sebastian Redld692af72010-07-27 18:24:41 +00002404 break;
2405 }
2406
Sebastian Redle1dde812010-08-24 00:50:04 +00002407 case UPDATE_VISIBLE: {
Douglas Gregor496c7092011-08-03 15:48:04 +00002408 unsigned Idx = 0;
2409 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
Benjamin Kramerb1758c62012-04-15 12:36:49 +00002410 ASTDeclContextNameLookupTable *Table =
2411 ASTDeclContextNameLookupTable::Create(
Douglas Gregor496c7092011-08-03 15:48:04 +00002412 (const unsigned char *)BlobStart + Record[Idx++],
Sebastian Redle1dde812010-08-24 00:50:04 +00002413 (const unsigned char *)BlobStart,
Douglas Gregor393f2492011-07-22 00:38:23 +00002414 ASTDeclContextNameLookupTrait(*this, F));
Douglas Gregor35942772011-09-09 21:34:22 +00002415 if (ID == PREDEF_DECL_TRANSLATION_UNIT_ID) { // Is it the TU?
2416 DeclContext *TU = Context.getTranslationUnitDecl();
Douglas Gregor0d95f772011-08-24 19:03:07 +00002417 F.DeclContextInfos[TU].NameLookupTableData = Table;
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002418 TU->setHasExternalVisibleStorage(true);
Sebastian Redle1dde812010-08-24 00:50:04 +00002419 } else
Douglas Gregor496c7092011-08-03 15:48:04 +00002420 PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F));
Sebastian Redle1dde812010-08-24 00:50:04 +00002421 break;
2422 }
2423
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002424 case IDENTIFIER_TABLE:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002425 F.IdentifierTableData = BlobStart;
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002426 if (Record[0]) {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002427 F.IdentifierLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002428 = ASTIdentifierLookupTable::Create(
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002429 (const unsigned char *)F.IdentifierTableData + Record[0],
2430 (const unsigned char *)F.IdentifierTableData,
Sebastian Redlc3632732010-10-05 15:59:54 +00002431 ASTIdentifierLookupTrait(*this, F));
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002432
2433 PP.getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002434 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002435 break;
2436
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002437 case IDENTIFIER_OFFSET: {
Sebastian Redl2da08f92010-07-19 22:28:42 +00002438 if (F.LocalNumIdentifiers != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002439 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002440 return true;
Douglas Gregorafaf3082009-04-11 00:14:32 +00002441 }
Sebastian Redl2da08f92010-07-19 22:28:42 +00002442 F.IdentifierOffsets = (const uint32_t *)BlobStart;
2443 F.LocalNumIdentifiers = Record[0];
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002444 unsigned LocalBaseIdentifierID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00002445 F.BaseIdentifierID = getTotalNumIdentifiers();
Douglas Gregor67268d02011-07-20 00:59:32 +00002446
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002447 if (F.LocalNumIdentifiers > 0) {
2448 // Introduce the global -> local mapping for identifiers within this
2449 // module.
2450 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
2451 &F));
2452
2453 // Introduce the local -> global mapping for identifiers within this
2454 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00002455 F.IdentifierRemap.insertOrReplace(
2456 std::make_pair(LocalBaseIdentifierID,
2457 F.BaseIdentifierID - LocalBaseIdentifierID));
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002458
2459 IdentifiersLoaded.resize(IdentifiersLoaded.size()
2460 + F.LocalNumIdentifiers);
2461 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002462 break;
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002463 }
Douglas Gregora8235d62012-10-09 23:05:51 +00002464
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002465 case EXTERNAL_DEFINITIONS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002466 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2467 ExternalDefinitions.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregorfdd01722009-04-14 00:24:19 +00002468 break;
Douglas Gregor3e1af842009-04-17 22:13:46 +00002469
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002470 case SPECIAL_TYPES:
Douglas Gregor393f2492011-07-22 00:38:23 +00002471 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2472 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
Douglas Gregorad1de002009-04-18 05:55:16 +00002473 break;
2474
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002475 case STATISTICS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002476 TotalNumStatements += Record[0];
2477 TotalNumMacros += Record[1];
2478 TotalLexicalDeclContexts += Record[2];
2479 TotalVisibleDeclContexts += Record[3];
Douglas Gregor3e1af842009-04-17 22:13:46 +00002480 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002481
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002482 case UNUSED_FILESCOPED_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002483 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2484 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002485 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002486
Sean Huntebcbe1d2011-05-04 23:29:54 +00002487 case DELEGATING_CTORS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002488 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2489 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
Sean Huntebcbe1d2011-05-04 23:29:54 +00002490 break;
2491
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002492 case WEAK_UNDECLARED_IDENTIFIERS:
Douglas Gregor31e37b22011-07-28 18:09:57 +00002493 if (Record.size() % 4 != 0) {
2494 Error("invalid weak identifiers record");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002495 return true;
Douglas Gregor31e37b22011-07-28 18:09:57 +00002496 }
2497
2498 // FIXME: Ignore weak undeclared identifiers from non-original PCH
2499 // files. This isn't the way to do it :)
2500 WeakUndeclaredIdentifiers.clear();
2501
2502 // Translate the weak, undeclared identifiers into global IDs.
2503 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
2504 WeakUndeclaredIdentifiers.push_back(
2505 getGlobalIdentifierID(F, Record[I++]));
2506 WeakUndeclaredIdentifiers.push_back(
2507 getGlobalIdentifierID(F, Record[I++]));
2508 WeakUndeclaredIdentifiers.push_back(
2509 ReadSourceLocation(F, Record, I).getRawEncoding());
2510 WeakUndeclaredIdentifiers.push_back(Record[I++]);
2511 }
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002512 break;
2513
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002514 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002515 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2516 LocallyScopedExternalDecls.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregor14c22f22009-04-22 22:18:58 +00002517 break;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002518
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002519 case SELECTOR_OFFSETS: {
Sebastian Redl059612d2010-08-03 21:58:15 +00002520 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redl725cd962010-08-04 20:40:17 +00002521 F.LocalNumSelectors = Record[0];
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002522 unsigned LocalBaseSelectorID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00002523 F.BaseSelectorID = getTotalNumSelectors();
Douglas Gregor96958cb2011-07-20 01:10:58 +00002524
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002525 if (F.LocalNumSelectors > 0) {
2526 // Introduce the global -> local mapping for selectors within this
2527 // module.
2528 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
2529
2530 // Introduce the local -> global mapping for selectors within this
2531 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00002532 F.SelectorRemap.insertOrReplace(
2533 std::make_pair(LocalBaseSelectorID,
2534 F.BaseSelectorID - LocalBaseSelectorID));
Douglas Gregor83941df2009-04-25 17:48:32 +00002535
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002536 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
2537 }
2538 break;
2539 }
2540
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002541 case METHOD_POOL:
Sebastian Redl725cd962010-08-04 20:40:17 +00002542 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor83941df2009-04-25 17:48:32 +00002543 if (Record[0])
Sebastian Redl725cd962010-08-04 20:40:17 +00002544 F.SelectorLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002545 = ASTSelectorLookupTable::Create(
Sebastian Redl725cd962010-08-04 20:40:17 +00002546 F.SelectorLookupTableData + Record[0],
2547 F.SelectorLookupTableData,
Douglas Gregor409448c2011-07-21 22:35:25 +00002548 ASTSelectorLookupTrait(*this, F));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002549 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002550 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002551
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002552 case REFERENCED_SELECTOR_POOL:
Douglas Gregor8451ec72011-07-28 14:41:43 +00002553 if (!Record.empty()) {
2554 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
2555 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
2556 Record[Idx++]));
2557 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
2558 getRawEncoding());
2559 }
2560 }
Fariborz Jahanian32019832010-07-23 19:11:11 +00002561 break;
2562
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002563 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002564 if (!Record.empty() && Listener)
Argyrios Kyrtzidis62288ed2012-10-10 02:12:47 +00002565 Listener->ReadCounter(F, Record[0]);
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002566 break;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002567
2568 case FILE_SORTED_DECLS:
2569 F.FileSortedDecls = (const DeclID *)BlobStart;
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002570 F.NumFileSortedDecls = Record[0];
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002571 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002572
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002573 case SOURCE_LOCATION_OFFSETS: {
2574 F.SLocEntryOffsets = (const uint32_t *)BlobStart;
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002575 F.LocalNumSLocEntries = Record[0];
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002576 unsigned SLocSpaceSize = Record[1];
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002577 llvm::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002578 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
2579 SLocSpaceSize);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002580 // Make our entry in the range map. BaseID is negative and growing, so
2581 // we invert it. Because we invert it, though, we need the other end of
2582 // the range.
2583 unsigned RangeStart =
2584 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
2585 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
2586 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
2587
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002588 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
2589 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
2590 GlobalSLocOffsetMap.insert(
2591 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
2592 - SLocSpaceSize,&F));
2593
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002594 // Initialize the remapping table.
2595 // Invalid stays invalid.
2596 F.SLocRemap.insert(std::make_pair(0U, 0));
2597 // This module. Base was 2 when being compiled.
2598 F.SLocRemap.insert(std::make_pair(2U,
2599 static_cast<int>(F.SLocEntryBaseOffset - 2)));
Douglas Gregor0cdd7982011-07-21 18:46:38 +00002600
2601 TotalNumSLocEntries += F.LocalNumSLocEntries;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002602 break;
2603 }
2604
Douglas Gregor5d51a1d2011-08-01 16:01:55 +00002605 case MODULE_OFFSET_MAP: {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002606 // Additional remapping information.
2607 const unsigned char *Data = (const unsigned char*)BlobStart;
2608 const unsigned char *DataEnd = Data + BlobLen;
Douglas Gregorf33740e2011-08-02 10:56:51 +00002609
2610 // Continuous range maps we may be updating in our module.
2611 ContinuousRangeMap<uint32_t, int, 2>::Builder SLocRemap(F.SLocRemap);
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002612 ContinuousRangeMap<uint32_t, int, 2>::Builder
2613 IdentifierRemap(F.IdentifierRemap);
Douglas Gregora8235d62012-10-09 23:05:51 +00002614 ContinuousRangeMap<uint32_t, int, 2>::Builder
2615 MacroRemap(F.MacroRemap);
2616 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002617 PreprocessedEntityRemap(F.PreprocessedEntityRemap);
2618 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregor26ced122011-12-01 00:59:36 +00002619 SubmoduleRemap(F.SubmoduleRemap);
2620 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002621 SelectorRemap(F.SelectorRemap);
Douglas Gregor496c7092011-08-03 15:48:04 +00002622 ContinuousRangeMap<uint32_t, int, 2>::Builder DeclRemap(F.DeclRemap);
Douglas Gregora119da02011-08-02 16:26:37 +00002623 ContinuousRangeMap<uint32_t, int, 2>::Builder TypeRemap(F.TypeRemap);
2624
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002625 while(Data < DataEnd) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002626 uint16_t Len = io::ReadUnalignedLE16(Data);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002627 StringRef Name = StringRef((const char*)Data, Len);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002628 Data += Len;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002629 ModuleFile *OM = ModuleMgr.lookup(Name);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002630 if (!OM) {
2631 Error("SourceLocation remap refers to unknown module");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002632 return true;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002633 }
Douglas Gregorf33740e2011-08-02 10:56:51 +00002634
2635 uint32_t SLocOffset = io::ReadUnalignedLE32(Data);
2636 uint32_t IdentifierIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregora8235d62012-10-09 23:05:51 +00002637 uint32_t MacroIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002638 uint32_t PreprocessedEntityIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregor26ced122011-12-01 00:59:36 +00002639 uint32_t SubmoduleIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002640 uint32_t SelectorIDOffset = io::ReadUnalignedLE32(Data);
2641 uint32_t DeclIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregora119da02011-08-02 16:26:37 +00002642 uint32_t TypeIndexOffset = io::ReadUnalignedLE32(Data);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002643
2644 // Source location offset is mapped to OM->SLocEntryBaseOffset.
2645 SLocRemap.insert(std::make_pair(SLocOffset,
2646 static_cast<int>(OM->SLocEntryBaseOffset - SLocOffset)));
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002647 IdentifierRemap.insert(
2648 std::make_pair(IdentifierIDOffset,
2649 OM->BaseIdentifierID - IdentifierIDOffset));
Douglas Gregora8235d62012-10-09 23:05:51 +00002650 MacroRemap.insert(std::make_pair(MacroIDOffset,
2651 OM->BaseMacroID - MacroIDOffset));
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002652 PreprocessedEntityRemap.insert(
2653 std::make_pair(PreprocessedEntityIDOffset,
2654 OM->BasePreprocessedEntityID - PreprocessedEntityIDOffset));
Douglas Gregor26ced122011-12-01 00:59:36 +00002655 SubmoduleRemap.insert(std::make_pair(SubmoduleIDOffset,
2656 OM->BaseSubmoduleID - SubmoduleIDOffset));
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002657 SelectorRemap.insert(std::make_pair(SelectorIDOffset,
2658 OM->BaseSelectorID - SelectorIDOffset));
Douglas Gregor496c7092011-08-03 15:48:04 +00002659 DeclRemap.insert(std::make_pair(DeclIDOffset,
2660 OM->BaseDeclID - DeclIDOffset));
2661
Douglas Gregora119da02011-08-02 16:26:37 +00002662 TypeRemap.insert(std::make_pair(TypeIndexOffset,
Douglas Gregore3605012011-08-02 18:32:54 +00002663 OM->BaseTypeIndex - TypeIndexOffset));
Douglas Gregora1be2782011-12-17 23:38:30 +00002664
2665 // Global -> local mappings.
2666 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002667 }
2668 break;
2669 }
2670
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002671 case SOURCE_MANAGER_LINE_TABLE:
2672 if (ParseLineTable(F, Record))
Douglas Gregor4825fd72012-10-22 22:50:17 +00002673 return true;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002674 break;
2675
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002676 case SOURCE_LOCATION_PRELOADS: {
2677 // Need to transform from the local view (1-based IDs) to the global view,
2678 // which is based off F.SLocEntryBaseID.
Douglas Gregorf249bf32011-08-25 21:09:44 +00002679 if (!F.PreloadSLocEntries.empty()) {
2680 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002681 return true;
Douglas Gregorf249bf32011-08-25 21:09:44 +00002682 }
2683
2684 F.PreloadSLocEntries.swap(Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002685 break;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002686 }
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002687
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002688 case STAT_CACHE: {
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00002689 if (!DisableStatCache) {
2690 ASTStatCache *MyStatCache =
2691 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
2692 (const unsigned char *)BlobStart,
2693 NumStatHits, NumStatMisses);
2694 FileMgr.addStatCache(MyStatCache);
2695 F.StatCache = MyStatCache;
2696 }
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002697 break;
Douglas Gregor52e71082009-10-16 18:18:30 +00002698 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002699
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002700 case EXT_VECTOR_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002701 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2702 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregorb81c1702009-04-27 20:06:05 +00002703 break;
2704
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002705 case VTABLE_USES:
Douglas Gregordfe65432011-07-28 19:11:31 +00002706 if (Record.size() % 3 != 0) {
2707 Error("Invalid VTABLE_USES record");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002708 return true;
Douglas Gregordfe65432011-07-28 19:11:31 +00002709 }
2710
Sebastian Redl40566802010-08-05 18:21:25 +00002711 // Later tables overwrite earlier ones.
Douglas Gregordfe65432011-07-28 19:11:31 +00002712 // FIXME: Modules will have some trouble with this. This is clearly not
2713 // the right way to do this.
Douglas Gregor409448c2011-07-21 22:35:25 +00002714 VTableUses.clear();
Douglas Gregordfe65432011-07-28 19:11:31 +00002715
2716 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
2717 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
2718 VTableUses.push_back(
2719 ReadSourceLocation(F, Record, Idx).getRawEncoding());
2720 VTableUses.push_back(Record[Idx++]);
2721 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002722 break;
2723
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002724 case DYNAMIC_CLASSES:
Douglas Gregor409448c2011-07-21 22:35:25 +00002725 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2726 DynamicClasses.push_back(getGlobalDeclID(F, Record[I]));
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002727 break;
2728
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002729 case PENDING_IMPLICIT_INSTANTIATIONS:
Douglas Gregorf2abb522011-07-28 19:26:52 +00002730 if (PendingInstantiations.size() % 2 != 0) {
Axel Naumann39d26c32012-10-02 09:09:43 +00002731 Error("Invalid existing PendingInstantiations");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002732 return true;
Axel Naumann39d26c32012-10-02 09:09:43 +00002733 }
2734
2735 if (Record.size() % 2 != 0) {
Douglas Gregorf2abb522011-07-28 19:26:52 +00002736 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002737 return true;
Douglas Gregorf2abb522011-07-28 19:26:52 +00002738 }
Axel Naumann39d26c32012-10-02 09:09:43 +00002739
Douglas Gregorf2abb522011-07-28 19:26:52 +00002740 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2741 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
2742 PendingInstantiations.push_back(
2743 ReadSourceLocation(F, Record, I).getRawEncoding());
2744 }
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002745 break;
2746
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002747 case SEMA_DECL_REFS:
Sebastian Redl40566802010-08-05 18:21:25 +00002748 // Later tables overwrite earlier ones.
Douglas Gregor409448c2011-07-21 22:35:25 +00002749 // FIXME: Modules will have some trouble with this.
2750 SemaDeclRefs.clear();
2751 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2752 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002753 break;
2754
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002755 case PPD_ENTITIES_OFFSETS: {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002756 F.PreprocessedEntityOffsets = (const PPEntityOffset *)BlobStart;
2757 assert(BlobLen % sizeof(PPEntityOffset) == 0);
2758 F.NumPreprocessedEntities = BlobLen / sizeof(PPEntityOffset);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002759
2760 unsigned LocalBasePreprocessedEntityID = Record[0];
Douglas Gregorfb2d9e02011-08-04 16:36:56 +00002761
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002762 unsigned StartingID;
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002763 if (!PP.getPreprocessingRecord())
Argyrios Kyrtzidisc6c54522012-03-05 05:48:17 +00002764 PP.createPreprocessingRecord(/*RecordConditionalDirectives=*/false);
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002765 if (!PP.getPreprocessingRecord()->getExternalSource())
2766 PP.getPreprocessingRecord()->SetExternalSource(*this);
2767 StartingID
2768 = PP.getPreprocessingRecord()
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002769 ->allocateLoadedEntities(F.NumPreprocessedEntities);
Douglas Gregor9827a802011-07-29 00:56:45 +00002770 F.BasePreprocessedEntityID = StartingID;
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002771
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002772 if (F.NumPreprocessedEntities > 0) {
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002773 // Introduce the global -> local mapping for preprocessed entities in
2774 // this module.
2775 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
2776
2777 // Introduce the local -> global mapping for preprocessed entities in
2778 // this module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00002779 F.PreprocessedEntityRemap.insertOrReplace(
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002780 std::make_pair(LocalBasePreprocessedEntityID,
2781 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
2782 }
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002783
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002784 break;
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002785 }
2786
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002787 case DECL_UPDATE_OFFSETS: {
2788 if (Record.size() % 2 != 0) {
2789 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002790 return true;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002791 }
2792 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Douglas Gregor496c7092011-08-03 15:48:04 +00002793 DeclUpdateOffsets[getGlobalDeclID(F, Record[I])]
2794 .push_back(std::make_pair(&F, Record[I+1]));
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002795 break;
2796 }
2797
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002798 case DECL_REPLACEMENTS: {
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00002799 if (Record.size() % 3 != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002800 Error("invalid DECL_REPLACEMENTS block in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002801 return true;
Sebastian Redl0b17c612010-08-13 00:28:03 +00002802 }
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00002803 for (unsigned I = 0, N = Record.size(); I != N; I += 3)
Douglas Gregor496c7092011-08-03 15:48:04 +00002804 ReplacedDecls[getGlobalDeclID(F, Record[I])]
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00002805 = ReplacedDeclInfo(&F, Record[I+1], Record[I+2]);
Sebastian Redl0b17c612010-08-13 00:28:03 +00002806 break;
2807 }
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00002808
Douglas Gregorcff9f262012-01-27 01:47:08 +00002809 case OBJC_CATEGORIES_MAP: {
2810 if (F.LocalNumObjCCategoriesInMap != 0) {
2811 Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002812 return true;
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00002813 }
Douglas Gregorcff9f262012-01-27 01:47:08 +00002814
2815 F.LocalNumObjCCategoriesInMap = Record[0];
2816 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)BlobStart;
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00002817 break;
2818 }
Douglas Gregor7c789c12010-10-29 22:39:52 +00002819
Douglas Gregorcff9f262012-01-27 01:47:08 +00002820 case OBJC_CATEGORIES:
2821 F.ObjCCategories.swap(Record);
2822 break;
2823
Douglas Gregor7c789c12010-10-29 22:39:52 +00002824 case CXX_BASE_SPECIFIER_OFFSETS: {
2825 if (F.LocalNumCXXBaseSpecifiers != 0) {
2826 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002827 return true;
Douglas Gregor7c789c12010-10-29 22:39:52 +00002828 }
2829
2830 F.LocalNumCXXBaseSpecifiers = Record[0];
2831 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart;
Jonathan D. Turner1da90142011-07-21 21:15:19 +00002832 NumCXXBaseSpecifiersLoaded += F.LocalNumCXXBaseSpecifiers;
Douglas Gregor7c789c12010-10-29 22:39:52 +00002833 break;
2834 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002835
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002836 case DIAG_PRAGMA_MAPPINGS:
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002837 if (Record.size() % 2 != 0) {
2838 Error("invalid DIAG_USER_MAPPINGS block in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002839 return true;
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002840 }
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002841
2842 if (F.PragmaDiagMappings.empty())
2843 F.PragmaDiagMappings.swap(Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002844 else
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002845 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
2846 Record.begin(), Record.end());
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002847 break;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002848
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002849 case CUDA_SPECIAL_DECL_REFS:
2850 // Later tables overwrite earlier ones.
Douglas Gregor409448c2011-07-21 22:35:25 +00002851 // FIXME: Modules will have trouble with this.
2852 CUDASpecialDeclRefs.clear();
2853 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2854 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002855 break;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002856
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002857 case HEADER_SEARCH_TABLE: {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002858 F.HeaderFileInfoTableData = BlobStart;
2859 F.LocalNumHeaderFileInfos = Record[1];
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002860 F.HeaderFileFrameworkStrings = BlobStart + Record[2];
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002861 if (Record[0]) {
2862 F.HeaderFileInfoTable
2863 = HeaderFileInfoLookupTable::Create(
2864 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002865 (const unsigned char *)F.HeaderFileInfoTableData,
Douglas Gregor95eab172011-07-28 20:55:49 +00002866 HeaderFileInfoTrait(*this, F,
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002867 &PP.getHeaderSearchInfo(),
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002868 BlobStart + Record[2]));
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002869
2870 PP.getHeaderSearchInfo().SetExternalSource(this);
2871 if (!PP.getHeaderSearchInfo().getExternalLookup())
2872 PP.getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002873 }
2874 break;
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002875 }
2876
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002877 case FP_PRAGMA_OPTIONS:
2878 // Later tables overwrite earlier ones.
2879 FPPragmaOptions.swap(Record);
2880 break;
2881
2882 case OPENCL_EXTENSIONS:
2883 // Later tables overwrite earlier ones.
2884 OpenCLExtensions.swap(Record);
2885 break;
Sean Huntebcbe1d2011-05-04 23:29:54 +00002886
2887 case TENTATIVE_DEFINITIONS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002888 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2889 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
Sean Huntebcbe1d2011-05-04 23:29:54 +00002890 break;
Douglas Gregord8bba9c2011-06-28 16:20:02 +00002891
2892 case KNOWN_NAMESPACES:
Douglas Gregor409448c2011-07-21 22:35:25 +00002893 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2894 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregord8bba9c2011-06-28 16:20:02 +00002895 break;
Douglas Gregorf6137e42011-12-03 00:59:55 +00002896
2897 case IMPORTED_MODULES: {
2898 if (F.Kind != MK_Module) {
2899 // If we aren't loading a module (which has its own exports), make
2900 // all of the imported modules visible.
2901 // FIXME: Deal with macros-only imports.
2902 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2903 if (unsigned GlobalID = getGlobalSubmoduleID(F, Record[I]))
2904 ImportedModules.push_back(GlobalID);
2905 }
2906 }
2907 break;
Douglas Gregora1be2782011-12-17 23:38:30 +00002908 }
Douglas Gregor2171bf12012-01-15 16:58:34 +00002909
Douglas Gregora1be2782011-12-17 23:38:30 +00002910 case LOCAL_REDECLARATIONS: {
Douglas Gregor2171bf12012-01-15 16:58:34 +00002911 F.RedeclarationChains.swap(Record);
2912 break;
2913 }
2914
2915 case LOCAL_REDECLARATIONS_MAP: {
2916 if (F.LocalNumRedeclarationsInMap != 0) {
2917 Error("duplicate LOCAL_REDECLARATIONS_MAP record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002918 return true;
Douglas Gregora1be2782011-12-17 23:38:30 +00002919 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00002920
Douglas Gregor2171bf12012-01-15 16:58:34 +00002921 F.LocalNumRedeclarationsInMap = Record[0];
2922 F.RedeclarationsMap = (const LocalRedeclarationsInfo *)BlobStart;
Douglas Gregora1be2782011-12-17 23:38:30 +00002923 break;
Douglas Gregorf6137e42011-12-03 00:59:55 +00002924 }
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00002925
2926 case MERGED_DECLARATIONS: {
2927 for (unsigned Idx = 0; Idx < Record.size(); /* increment in loop */) {
2928 GlobalDeclID CanonID = getGlobalDeclID(F, Record[Idx++]);
2929 SmallVectorImpl<GlobalDeclID> &Decls = StoredMergedDecls[CanonID];
2930 for (unsigned N = Record[Idx++]; N > 0; --N)
2931 Decls.push_back(getGlobalDeclID(F, Record[Idx++]));
2932 }
2933 break;
2934 }
Douglas Gregora8235d62012-10-09 23:05:51 +00002935
2936 case MACRO_OFFSET: {
2937 if (F.LocalNumMacros != 0) {
2938 Error("duplicate MACRO_OFFSET record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002939 return true;
Douglas Gregora8235d62012-10-09 23:05:51 +00002940 }
2941 F.MacroOffsets = (const uint32_t *)BlobStart;
2942 F.LocalNumMacros = Record[0];
2943 unsigned LocalBaseMacroID = Record[1];
2944 F.BaseMacroID = getTotalNumMacros();
2945
2946 if (F.LocalNumMacros > 0) {
2947 // Introduce the global -> local mapping for macros within this module.
2948 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
2949
2950 // Introduce the local -> global mapping for macros within this module.
2951 F.MacroRemap.insertOrReplace(
2952 std::make_pair(LocalBaseMacroID,
2953 F.BaseMacroID - LocalBaseMacroID));
2954
2955 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
2956 }
2957 break;
2958 }
2959
2960 case MACRO_UPDATES: {
2961 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2962 MacroID ID = getGlobalMacroID(F, Record[I++]);
2963 if (I == N)
2964 break;
2965
Douglas Gregor54c8a402012-10-12 00:16:50 +00002966 SourceLocation UndefLoc = ReadSourceLocation(F, Record, I);
2967 SubmoduleID SubmoduleID = getGlobalSubmoduleID(F, Record[I++]);;
2968 MacroUpdate Update;
2969 Update.UndefLoc = UndefLoc;
2970 MacroUpdates[ID].push_back(std::make_pair(SubmoduleID, Update));
Douglas Gregora8235d62012-10-09 23:05:51 +00002971 }
2972 break;
2973 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002974 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002975 }
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002976 Error("premature end of bitstream in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002977 return true;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002978}
2979
Douglas Gregorecc2c092011-12-01 22:20:10 +00002980void ASTReader::makeNamesVisible(const HiddenNames &Names) {
Douglas Gregor13292642011-12-02 15:45:10 +00002981 for (unsigned I = 0, N = Names.size(); I != N; ++I) {
Douglas Gregor54c8a402012-10-12 00:16:50 +00002982 switch (Names[I].getKind()) {
2983 case HiddenName::Declaration:
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00002984 Names[I].getDecl()->Hidden = false;
Douglas Gregor54c8a402012-10-12 00:16:50 +00002985 break;
2986
2987 case HiddenName::MacroVisibility: {
2988 std::pair<IdentifierInfo *, MacroInfo *> Macro = Names[I].getMacro();
2989 Macro.second->setHidden(!Macro.second->isPublic());
2990 if (Macro.second->isDefined()) {
2991 PP.makeLoadedMacroInfoVisible(Macro.first, Macro.second);
2992 }
2993 break;
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00002994 }
2995
Douglas Gregor54c8a402012-10-12 00:16:50 +00002996 case HiddenName::MacroUndef: {
2997 std::pair<IdentifierInfo *, MacroInfo *> Macro = Names[I].getMacro();
2998 if (Macro.second->isDefined()) {
2999 Macro.second->setUndefLoc(Names[I].getMacroUndefLoc());
3000 if (PPMutationListener *Listener = PP.getPPMutationListener())
3001 Listener->UndefinedMacro(Macro.second);
3002 PP.makeLoadedMacroInfoVisible(Macro.first, Macro.second);
3003 }
3004 break;
3005 }
Douglas Gregor1d4c1132011-12-20 22:06:13 +00003006 }
Douglas Gregor13292642011-12-02 15:45:10 +00003007 }
Douglas Gregorecc2c092011-12-01 22:20:10 +00003008}
3009
Douglas Gregor5e356932011-12-01 17:11:21 +00003010void ASTReader::makeModuleVisible(Module *Mod,
3011 Module::NameVisibilityKind NameVisibility) {
3012 llvm::SmallPtrSet<Module *, 4> Visited;
3013 llvm::SmallVector<Module *, 4> Stack;
3014 Stack.push_back(Mod);
3015 while (!Stack.empty()) {
3016 Mod = Stack.back();
3017 Stack.pop_back();
3018
3019 if (NameVisibility <= Mod->NameVisibility) {
3020 // This module already has this level of visibility (or greater), so
3021 // there is nothing more to do.
3022 continue;
3023 }
3024
Douglas Gregor51f564f2011-12-31 04:05:44 +00003025 if (!Mod->isAvailable()) {
3026 // Modules that aren't available cannot be made visible.
3027 continue;
3028 }
3029
Douglas Gregor5e356932011-12-01 17:11:21 +00003030 // Update the module's name visibility.
3031 Mod->NameVisibility = NameVisibility;
3032
Douglas Gregorecc2c092011-12-01 22:20:10 +00003033 // If we've already deserialized any names from this module,
Douglas Gregor5e356932011-12-01 17:11:21 +00003034 // mark them as visible.
Douglas Gregorecc2c092011-12-01 22:20:10 +00003035 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
3036 if (Hidden != HiddenNamesMap.end()) {
3037 makeNamesVisible(Hidden->second);
3038 HiddenNamesMap.erase(Hidden);
3039 }
Douglas Gregor5e356932011-12-01 17:11:21 +00003040
3041 // Push any non-explicit submodules onto the stack to be marked as
3042 // visible.
Douglas Gregorb7a78192012-01-04 23:32:19 +00003043 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
3044 SubEnd = Mod->submodule_end();
Douglas Gregor5e356932011-12-01 17:11:21 +00003045 Sub != SubEnd; ++Sub) {
Douglas Gregorb7a78192012-01-04 23:32:19 +00003046 if (!(*Sub)->IsExplicit && Visited.insert(*Sub))
3047 Stack.push_back(*Sub);
Douglas Gregor5e356932011-12-01 17:11:21 +00003048 }
Douglas Gregor07165b92011-12-02 19:11:09 +00003049
3050 // Push any exported modules onto the stack to be marked as visible.
Douglas Gregor0adaa882011-12-05 17:28:06 +00003051 bool AnyWildcard = false;
3052 bool UnrestrictedWildcard = false;
3053 llvm::SmallVector<Module *, 4> WildcardRestrictions;
Douglas Gregor07165b92011-12-02 19:11:09 +00003054 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
3055 Module *Exported = Mod->Exports[I].getPointer();
Douglas Gregor0adaa882011-12-05 17:28:06 +00003056 if (!Mod->Exports[I].getInt()) {
3057 // Export a named module directly; no wildcards involved.
3058 if (Visited.insert(Exported))
Douglas Gregor07165b92011-12-02 19:11:09 +00003059 Stack.push_back(Exported);
Douglas Gregor0adaa882011-12-05 17:28:06 +00003060
3061 continue;
Douglas Gregor07165b92011-12-02 19:11:09 +00003062 }
Douglas Gregor0adaa882011-12-05 17:28:06 +00003063
3064 // Wildcard export: export all of the imported modules that match
3065 // the given pattern.
3066 AnyWildcard = true;
3067 if (UnrestrictedWildcard)
3068 continue;
3069
3070 if (Module *Restriction = Mod->Exports[I].getPointer())
3071 WildcardRestrictions.push_back(Restriction);
3072 else {
3073 WildcardRestrictions.clear();
3074 UnrestrictedWildcard = true;
3075 }
3076 }
3077
3078 // If there were any wildcards, push any imported modules that were
3079 // re-exported by the wildcard restriction.
3080 if (!AnyWildcard)
3081 continue;
3082
3083 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
3084 Module *Imported = Mod->Imports[I];
Benjamin Kramerd48bcb22012-08-22 15:37:55 +00003085 if (!Visited.insert(Imported))
Douglas Gregor0adaa882011-12-05 17:28:06 +00003086 continue;
3087
3088 bool Acceptable = UnrestrictedWildcard;
3089 if (!Acceptable) {
3090 // Check whether this module meets one of the restrictions.
3091 for (unsigned R = 0, NR = WildcardRestrictions.size(); R != NR; ++R) {
3092 Module *Restriction = WildcardRestrictions[R];
3093 if (Imported == Restriction || Imported->isSubModuleOf(Restriction)) {
3094 Acceptable = true;
3095 break;
3096 }
3097 }
3098 }
3099
3100 if (!Acceptable)
3101 continue;
3102
Douglas Gregor0adaa882011-12-05 17:28:06 +00003103 Stack.push_back(Imported);
Douglas Gregor07165b92011-12-02 19:11:09 +00003104 }
Douglas Gregor5e356932011-12-01 17:11:21 +00003105 }
3106}
3107
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00003108ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
Douglas Gregor38295be2012-10-22 23:51:00 +00003109 ModuleKind Type,
3110 unsigned ClientLoadCapabilities) {
Douglas Gregor057df202012-01-18 20:56:22 +00003111 // Bump the generation number.
Douglas Gregorcff9f262012-01-27 01:47:08 +00003112 unsigned PreviousGeneration = CurrentGeneration++;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003113
3114 // Load the core of the AST files.
3115 llvm::SmallVector<ModuleFile *, 4> Loaded;
Douglas Gregor38295be2012-10-22 23:51:00 +00003116 switch(ReadASTCore(FileName, Type, /*ImportedBy=*/0, Loaded,
3117 ClientLoadCapabilities)) {
Sebastian Redlcdf3b832010-07-16 20:41:52 +00003118 case Failure: return Failure;
Douglas Gregor4825fd72012-10-22 22:50:17 +00003119 case OutOfDate: return OutOfDate;
3120 case VersionMismatch: return VersionMismatch;
3121 case ConfigurationMismatch: return ConfigurationMismatch;
3122 case HadErrors: return HadErrors;
Sebastian Redlcdf3b832010-07-16 20:41:52 +00003123 case Success: break;
3124 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00003125
3126 // Here comes stuff that we only do once the entire chain is loaded.
Douglas Gregor057df202012-01-18 20:56:22 +00003127
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003128 // Load the AST blocks of all of the modules that we loaded.
3129 for (llvm::SmallVectorImpl<ModuleFile *>::iterator M = Loaded.begin(),
3130 MEnd = Loaded.end();
3131 M != MEnd; ++M) {
3132 ModuleFile &F = **M;
3133
3134 // Read the AST block.
Douglas Gregor4825fd72012-10-22 22:50:17 +00003135 if (ReadASTBlock(F))
3136 return Failure;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003137
3138 // Once read, set the ModuleFile bit base offset and update the size in
3139 // bits of all files we've seen.
3140 F.GlobalBitOffset = TotalModulesSizeInBits;
3141 TotalModulesSizeInBits += F.SizeInBits;
3142 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
3143
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003144 // Preload SLocEntries.
3145 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
3146 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
Douglas Gregor8b53d142012-10-22 22:53:10 +00003147 // Load it through the SourceManager and don't call ReadSLocEntry()
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003148 // directly because the entry may have already been loaded in which case
Douglas Gregor8b53d142012-10-22 22:53:10 +00003149 // calling ReadSLocEntry() directly would trigger an assertion in
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003150 // SourceManager.
3151 SourceMgr.getLoadedSLocEntryByID(Index);
3152 }
3153 }
3154
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00003155 // Check the predefines buffers.
Douglas Gregor38295be2012-10-22 23:51:00 +00003156 bool ConfigComplain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
Douglas Gregor6236a292011-12-02 21:56:05 +00003157 if (!DisableValidation && Type == MK_PCH &&
Argyrios Kyrtzidis26d43cd2011-09-12 18:09:38 +00003158 // FIXME: CheckPredefinesBuffers also sets the SuggestedPredefines;
3159 // if DisableValidation is true, defines that were set on command-line
3160 // but not in the PCH file will not be added to SuggestedPredefines.
Douglas Gregor38295be2012-10-22 23:51:00 +00003161 CheckPredefinesBuffers(ConfigComplain))
Douglas Gregor4825fd72012-10-22 22:50:17 +00003162 return ConfigurationMismatch;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00003163
Douglas Gregoreee242f2011-10-27 09:33:13 +00003164 // Mark all of the identifiers in the identifier table as being out of date,
3165 // so that various accessors know to check the loaded modules when the
3166 // identifier is used.
Douglas Gregor712f2fc2011-09-09 22:02:16 +00003167 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
3168 IdEnd = PP.getIdentifierTable().end();
3169 Id != IdEnd; ++Id)
Douglas Gregoreee242f2011-10-27 09:33:13 +00003170 Id->second->setOutOfDate(true);
Douglas Gregor057df202012-01-18 20:56:22 +00003171
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003172 // Resolve any unresolved module exports.
Douglas Gregor55988682011-12-05 16:33:54 +00003173 for (unsigned I = 0, N = UnresolvedModuleImportExports.size(); I != N; ++I) {
3174 UnresolvedModuleImportExport &Unresolved = UnresolvedModuleImportExports[I];
3175 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
Douglas Gregor0adaa882011-12-05 17:28:06 +00003176 Module *ResolvedMod = getSubmodule(GlobalID);
3177
3178 if (Unresolved.IsImport) {
3179 if (ResolvedMod)
Douglas Gregor55988682011-12-05 16:33:54 +00003180 Unresolved.Mod->Imports.push_back(ResolvedMod);
Douglas Gregor0adaa882011-12-05 17:28:06 +00003181 continue;
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003182 }
Douglas Gregor0adaa882011-12-05 17:28:06 +00003183
3184 if (ResolvedMod || Unresolved.IsWildcard)
3185 Unresolved.Mod->Exports.push_back(
3186 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003187 }
Douglas Gregor55988682011-12-05 16:33:54 +00003188 UnresolvedModuleImportExports.clear();
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003189
Douglas Gregor35942772011-09-09 21:34:22 +00003190 InitializeContext();
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00003191
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003192 if (DeserializationListener)
3193 DeserializationListener->ReaderInitialized(this);
3194
Douglas Gregor11407b82012-10-18 21:18:25 +00003195 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
3196 if (!PrimaryModule.OriginalSourceFileID.isInvalid()) {
3197 PrimaryModule.OriginalSourceFileID
3198 = FileID::get(PrimaryModule.SLocEntryBaseID
3199 + PrimaryModule.OriginalSourceFileID.getOpaqueValue() - 1);
Argyrios Kyrtzidisb8c879a2012-01-05 21:36:25 +00003200
Douglas Gregor11407b82012-10-18 21:18:25 +00003201 // If this AST file is a precompiled preamble, then set the
3202 // preamble file ID of the source manager to the file source file
3203 // from which the preamble was built.
Argyrios Kyrtzidisb8c879a2012-01-05 21:36:25 +00003204 if (Type == MK_Preamble) {
Douglas Gregor11407b82012-10-18 21:18:25 +00003205 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
Argyrios Kyrtzidisb8c879a2012-01-05 21:36:25 +00003206 } else if (Type == MK_MainFile) {
Douglas Gregor11407b82012-10-18 21:18:25 +00003207 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00003208 }
Douglas Gregor414cb642010-11-30 05:23:00 +00003209 }
3210
Douglas Gregorcff9f262012-01-27 01:47:08 +00003211 // For any Objective-C class definitions we have already loaded, make sure
3212 // that we load any additional categories.
3213 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
3214 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
3215 ObjCClassesLoaded[I],
3216 PreviousGeneration);
3217 }
3218
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00003219 return Success;
3220}
3221
Douglas Gregor38295be2012-10-22 23:51:00 +00003222ASTReader::ASTReadResult
3223ASTReader::ReadASTCore(StringRef FileName,
3224 ModuleKind Type,
3225 ModuleFile *ImportedBy,
3226 llvm::SmallVectorImpl<ModuleFile *> &Loaded,
3227 unsigned ClientLoadCapabilities) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003228 ModuleFile *M;
Douglas Gregorfac4ece2011-08-19 02:29:29 +00003229 bool NewModule;
3230 std::string ErrorStr;
3231 llvm::tie(M, NewModule) = ModuleMgr.addModule(FileName, Type, ImportedBy,
Douglas Gregor057df202012-01-18 20:56:22 +00003232 CurrentGeneration, ErrorStr);
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00003233
Douglas Gregorfac4ece2011-08-19 02:29:29 +00003234 if (!M) {
3235 // We couldn't load the module.
3236 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
3237 + ErrorStr;
3238 Error(Msg);
3239 return Failure;
3240 }
3241
3242 if (!NewModule) {
3243 // We've already loaded this module.
3244 return Success;
3245 }
3246
3247 // FIXME: This seems rather a hack. Should CurrentDir be part of the
3248 // module?
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00003249 if (FileName != "-") {
3250 CurrentDir = llvm::sys::path::parent_path(FileName);
3251 if (CurrentDir.empty()) CurrentDir = ".";
3252 }
3253
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003254 ModuleFile &F = *M;
Sebastian Redl9137a522010-07-16 17:50:48 +00003255 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00003256 Stream.init(F.StreamFile);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00003257 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Douglas Gregor8f1231b2011-07-22 06:10:01 +00003258
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00003259 // Sniff for the signature.
3260 if (Stream.Read(8) != 'C' ||
3261 Stream.Read(8) != 'P' ||
3262 Stream.Read(8) != 'C' ||
3263 Stream.Read(8) != 'H') {
3264 Diag(diag::err_not_a_pch_file) << FileName;
3265 return Failure;
3266 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003267
Douglas Gregor2cf26342009-04-09 22:27:44 +00003268 while (!Stream.AtEndOfStream()) {
3269 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00003270
Douglas Gregore1d918e2009-04-10 23:10:45 +00003271 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003272 Error("invalid record at top-level of AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00003273 return Failure;
3274 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003275
3276 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregor668c1a42009-04-21 22:25:48 +00003277
Douglas Gregor7ae467f2012-10-18 18:27:37 +00003278 // We only know the control subblock ID.
Douglas Gregor2cf26342009-04-09 22:27:44 +00003279 switch (BlockID) {
3280 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00003281 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003282 Error("malformed BlockInfoBlock in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00003283 return Failure;
3284 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003285 break;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003286 case CONTROL_BLOCK_ID:
Douglas Gregor38295be2012-10-22 23:51:00 +00003287 switch (ReadControlBlock(F, Loaded, ClientLoadCapabilities)) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003288 case Success:
3289 break;
3290
Douglas Gregor4825fd72012-10-22 22:50:17 +00003291 case Failure: return Failure;
3292 case OutOfDate: return OutOfDate;
3293 case VersionMismatch: return VersionMismatch;
3294 case ConfigurationMismatch: return ConfigurationMismatch;
3295 case HadErrors: return HadErrors;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003296 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003297 break;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003298 case AST_BLOCK_ID:
3299 // Record that we've loaded this module.
3300 Loaded.push_back(M);
3301 return Success;
3302
Douglas Gregor2cf26342009-04-09 22:27:44 +00003303 default:
Douglas Gregore1d918e2009-04-10 23:10:45 +00003304 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003305 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00003306 return Failure;
3307 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003308 break;
3309 }
Mike Stump1eb44332009-09-09 15:08:12 +00003310 }
Douglas Gregor8f1231b2011-07-22 06:10:01 +00003311
Sebastian Redlcdf3b832010-07-16 20:41:52 +00003312 return Success;
3313}
3314
Douglas Gregor712f2fc2011-09-09 22:02:16 +00003315void ASTReader::InitializeContext() {
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003316 // If there's a listener, notify them that we "read" the translation unit.
3317 if (DeserializationListener)
Douglas Gregor35942772011-09-09 21:34:22 +00003318 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
3319 Context.getTranslationUnitDecl());
Douglas Gregor3747ee72010-10-01 01:18:02 +00003320
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003321 // Make sure we load the declaration update records for the translation unit,
3322 // if there are any.
Douglas Gregor35942772011-09-09 21:34:22 +00003323 loadDeclUpdateRecords(PREDEF_DECL_TRANSLATION_UNIT_ID,
3324 Context.getTranslationUnitDecl());
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003325
Douglas Gregor5f957282011-08-11 22:18:49 +00003326 // FIXME: Find a better way to deal with collisions between these
3327 // built-in types. Right now, we just ignore the problem.
3328
3329 // Load the special types.
Douglas Gregora6ea10e2012-01-17 18:09:05 +00003330 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
Douglas Gregor02a5e872011-09-10 00:30:18 +00003331 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
3332 if (!Context.CFConstantStringTypeDecl)
3333 Context.setCFConstantStringType(GetType(String));
3334 }
3335
3336 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
3337 QualType FileType = GetType(File);
3338 if (FileType.isNull()) {
3339 Error("FILE type is NULL");
3340 return;
3341 }
3342
3343 if (!Context.FILEDecl) {
3344 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
3345 Context.setFILEDecl(Typedef->getDecl());
3346 else {
3347 const TagType *Tag = FileType->getAs<TagType>();
3348 if (!Tag) {
3349 Error("Invalid FILE type in AST file");
3350 return;
3351 }
3352 Context.setFILEDecl(Tag->getDecl());
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00003353 }
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00003354 }
Douglas Gregorc29f77b2009-07-07 16:35:42 +00003355 }
Douglas Gregor5f957282011-08-11 22:18:49 +00003356
Douglas Gregor72cd7a02011-11-11 19:13:12 +00003357 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
Douglas Gregor02a5e872011-09-10 00:30:18 +00003358 QualType Jmp_bufType = GetType(Jmp_buf);
3359 if (Jmp_bufType.isNull()) {
3360 Error("jmp_buf type is NULL");
3361 return;
3362 }
3363
3364 if (!Context.jmp_bufDecl) {
3365 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
3366 Context.setjmp_bufDecl(Typedef->getDecl());
3367 else {
3368 const TagType *Tag = Jmp_bufType->getAs<TagType>();
3369 if (!Tag) {
3370 Error("Invalid jmp_buf type in AST file");
3371 return;
3372 }
3373 Context.setjmp_bufDecl(Tag->getDecl());
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00003374 }
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00003375 }
Mike Stump782fa302009-07-28 02:25:19 +00003376 }
Douglas Gregor02a5e872011-09-10 00:30:18 +00003377
Douglas Gregor72cd7a02011-11-11 19:13:12 +00003378 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
Douglas Gregor02a5e872011-09-10 00:30:18 +00003379 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
3380 if (Sigjmp_bufType.isNull()) {
3381 Error("sigjmp_buf type is NULL");
3382 return;
3383 }
3384
3385 if (!Context.sigjmp_bufDecl) {
3386 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
3387 Context.setsigjmp_bufDecl(Typedef->getDecl());
3388 else {
3389 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
3390 assert(Tag && "Invalid sigjmp_buf type in AST file");
3391 Context.setsigjmp_bufDecl(Tag->getDecl());
3392 }
3393 }
3394 }
3395
3396 if (unsigned ObjCIdRedef
3397 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
3398 if (Context.ObjCIdRedefinitionType.isNull())
3399 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
3400 }
3401
3402 if (unsigned ObjCClassRedef
3403 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
3404 if (Context.ObjCClassRedefinitionType.isNull())
3405 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
3406 }
3407
3408 if (unsigned ObjCSelRedef
3409 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
3410 if (Context.ObjCSelRedefinitionType.isNull())
3411 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
3412 }
Rafael Espindolae2d4f4e2011-11-13 21:51:09 +00003413
3414 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
3415 QualType Ucontext_tType = GetType(Ucontext_t);
3416 if (Ucontext_tType.isNull()) {
3417 Error("ucontext_t type is NULL");
3418 return;
3419 }
3420
3421 if (!Context.ucontext_tDecl) {
3422 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
3423 Context.setucontext_tDecl(Typedef->getDecl());
3424 else {
3425 const TagType *Tag = Ucontext_tType->getAs<TagType>();
3426 assert(Tag && "Invalid ucontext_t type in AST file");
3427 Context.setucontext_tDecl(Tag->getDecl());
3428 }
3429 }
3430 }
Douglas Gregor5f957282011-08-11 22:18:49 +00003431 }
3432
Douglas Gregor35942772011-09-09 21:34:22 +00003433 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003434
3435 // If there were any CUDA special declarations, deserialize them.
3436 if (!CUDASpecialDeclRefs.empty()) {
3437 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
Douglas Gregor35942772011-09-09 21:34:22 +00003438 Context.setcudaConfigureCallDecl(
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003439 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
3440 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00003441
3442 // Re-export any modules that were imported by a non-module AST file.
3443 for (unsigned I = 0, N = ImportedModules.size(); I != N; ++I) {
3444 if (Module *Imported = getSubmodule(ImportedModules[I]))
3445 makeModuleVisible(Imported, Module::AllVisible);
3446 }
3447 ImportedModules.clear();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003448}
3449
Douglas Gregorecc2c092011-12-01 22:20:10 +00003450void ASTReader::finalizeForWriting() {
3451 for (HiddenNamesMapType::iterator Hidden = HiddenNamesMap.begin(),
3452 HiddenEnd = HiddenNamesMap.end();
3453 Hidden != HiddenEnd; ++Hidden) {
3454 makeNamesVisible(Hidden->second);
3455 }
3456 HiddenNamesMap.clear();
3457}
3458
Douglas Gregorb64c1932009-05-12 01:31:05 +00003459/// \brief Retrieve the name of the original source file name
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003460/// directly from the AST file, without actually loading the AST
Douglas Gregorb64c1932009-05-12 01:31:05 +00003461/// file.
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003462std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +00003463 FileManager &FileMgr,
David Blaikied6471f72011-09-25 23:23:43 +00003464 DiagnosticsEngine &Diags) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003465 // Open the AST file.
Douglas Gregorb64c1932009-05-12 01:31:05 +00003466 std::string ErrStr;
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00003467 OwningPtr<llvm::MemoryBuffer> Buffer;
Chris Lattner39b49bc2010-11-23 08:35:12 +00003468 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
Douglas Gregorb64c1932009-05-12 01:31:05 +00003469 if (!Buffer) {
Kaelyn Uhrainda01f622012-06-20 00:36:03 +00003470 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ASTFileName << ErrStr;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003471 return std::string();
3472 }
3473
3474 // Initialize the stream
3475 llvm::BitstreamReader StreamFile;
3476 llvm::BitstreamCursor Stream;
Mike Stump1eb44332009-09-09 15:08:12 +00003477 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregorb64c1932009-05-12 01:31:05 +00003478 (const unsigned char *)Buffer->getBufferEnd());
3479 Stream.init(StreamFile);
3480
3481 // Sniff for the signature.
3482 if (Stream.Read(8) != 'C' ||
3483 Stream.Read(8) != 'P' ||
3484 Stream.Read(8) != 'C' ||
3485 Stream.Read(8) != 'H') {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003486 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003487 return std::string();
3488 }
3489
3490 RecordData Record;
3491 while (!Stream.AtEndOfStream()) {
3492 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00003493
Douglas Gregorb64c1932009-05-12 01:31:05 +00003494 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
3495 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump1eb44332009-09-09 15:08:12 +00003496
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003497 // We only know the AST subblock ID.
Douglas Gregorb64c1932009-05-12 01:31:05 +00003498 switch (BlockID) {
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003499 case CONTROL_BLOCK_ID:
3500 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003501 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003502 return std::string();
3503 }
3504 break;
Mike Stump1eb44332009-09-09 15:08:12 +00003505
Douglas Gregorb64c1932009-05-12 01:31:05 +00003506 default:
3507 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003508 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003509 return std::string();
3510 }
3511 break;
3512 }
3513 continue;
3514 }
3515
3516 if (Code == llvm::bitc::END_BLOCK) {
3517 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003518 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003519 return std::string();
3520 }
3521 continue;
3522 }
3523
3524 if (Code == llvm::bitc::DEFINE_ABBREV) {
3525 Stream.ReadAbbrevRecord();
3526 continue;
3527 }
3528
3529 Record.clear();
3530 const char *BlobStart = 0;
3531 unsigned BlobLen = 0;
Douglas Gregor39c497b2012-10-18 18:36:53 +00003532 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen) == ORIGINAL_FILE)
Douglas Gregorb64c1932009-05-12 01:31:05 +00003533 return std::string(BlobStart, BlobLen);
Mike Stump1eb44332009-09-09 15:08:12 +00003534 }
Douglas Gregorb64c1932009-05-12 01:31:05 +00003535
3536 return std::string();
3537}
3538
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00003539namespace {
3540 class SimplePCHValidator : public ASTReaderListener {
3541 const LangOptions &ExistingLangOpts;
3542 const TargetOptions &ExistingTargetOpts;
Douglas Gregor4c0c7e82012-10-24 23:41:50 +00003543 const PreprocessorOptions &ExistingPPOpts;
Douglas Gregor87699242012-10-25 00:07:54 +00003544 FileManager &FileMgr;
3545
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00003546 public:
3547 SimplePCHValidator(const LangOptions &ExistingLangOpts,
Douglas Gregor4c0c7e82012-10-24 23:41:50 +00003548 const TargetOptions &ExistingTargetOpts,
Douglas Gregor87699242012-10-25 00:07:54 +00003549 const PreprocessorOptions &ExistingPPOpts,
3550 FileManager &FileMgr)
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00003551 : ExistingLangOpts(ExistingLangOpts),
Douglas Gregor4c0c7e82012-10-24 23:41:50 +00003552 ExistingTargetOpts(ExistingTargetOpts),
Douglas Gregor87699242012-10-25 00:07:54 +00003553 ExistingPPOpts(ExistingPPOpts),
3554 FileMgr(FileMgr)
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00003555 {
3556 }
3557
3558 virtual bool ReadLanguageOptions(const LangOptions &LangOpts,
3559 bool Complain) {
3560 return checkLanguageOptions(ExistingLangOpts, LangOpts, 0);
3561 }
3562 virtual bool ReadTargetOptions(const TargetOptions &TargetOpts,
3563 bool Complain) {
3564 return checkTargetOptions(ExistingTargetOpts, TargetOpts, 0);
3565 }
Douglas Gregor4c0c7e82012-10-24 23:41:50 +00003566 virtual bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
Douglas Gregor87699242012-10-25 00:07:54 +00003567 bool Complain,
3568 std::string &SuggestedPredefines) {
3569 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, 0, FileMgr,
3570 SuggestedPredefines);
Douglas Gregor4c0c7e82012-10-24 23:41:50 +00003571 }
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00003572 };
3573}
3574
3575bool ASTReader::isAcceptableASTFile(StringRef Filename,
3576 FileManager &FileMgr,
3577 const LangOptions &LangOpts,
Douglas Gregor4c0c7e82012-10-24 23:41:50 +00003578 const TargetOptions &TargetOpts,
3579 const PreprocessorOptions &PPOpts) {
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00003580 // Open the AST file.
3581 std::string ErrStr;
3582 OwningPtr<llvm::MemoryBuffer> Buffer;
3583 Buffer.reset(FileMgr.getBufferForFile(Filename, &ErrStr));
3584 if (!Buffer) {
3585 return false;
3586 }
3587
3588 // Initialize the stream
3589 llvm::BitstreamReader StreamFile;
3590 llvm::BitstreamCursor Stream;
3591 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
3592 (const unsigned char *)Buffer->getBufferEnd());
3593 Stream.init(StreamFile);
3594
3595 // Sniff for the signature.
3596 if (Stream.Read(8) != 'C' ||
3597 Stream.Read(8) != 'P' ||
3598 Stream.Read(8) != 'C' ||
3599 Stream.Read(8) != 'H') {
3600 return false;
3601 }
3602
Douglas Gregor87699242012-10-25 00:07:54 +00003603 SimplePCHValidator Validator(LangOpts, TargetOpts, PPOpts, FileMgr);
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00003604 RecordData Record;
3605 bool InControlBlock = false;
3606 while (!Stream.AtEndOfStream()) {
3607 unsigned Code = Stream.ReadCode();
3608
3609 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
3610 unsigned BlockID = Stream.ReadSubBlockID();
3611
3612 // We only know the control subblock ID.
3613 switch (BlockID) {
3614 case CONTROL_BLOCK_ID:
3615 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
3616 return false;
3617 } else {
3618 InControlBlock = true;
3619 }
3620 break;
3621
3622 default:
3623 if (Stream.SkipBlock())
3624 return false;
3625 break;
3626 }
3627 continue;
3628 }
3629
3630 if (Code == llvm::bitc::END_BLOCK) {
3631 if (Stream.ReadBlockEnd()) {
3632 return false;
3633 }
3634 InControlBlock = false;
3635 continue;
3636 }
3637
3638 if (Code == llvm::bitc::DEFINE_ABBREV) {
3639 Stream.ReadAbbrevRecord();
3640 continue;
3641 }
3642
3643 Record.clear();
3644 const char *BlobStart = 0;
3645 unsigned BlobLen = 0;
3646 unsigned RecCode = Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen);
3647 if (InControlBlock) {
3648 switch ((ControlRecordTypes)RecCode) {
3649 case METADATA: {
3650 if (Record[0] != VERSION_MAJOR) {
3651 return false;
3652 }
3653
3654 const std::string &CurBranch = getClangFullRepositoryVersion();
3655 StringRef ASTBranch(BlobStart, BlobLen);
3656 if (StringRef(CurBranch) != ASTBranch)
3657 return false;
3658
3659 break;
3660 }
3661 case LANGUAGE_OPTIONS:
3662 if (ParseLanguageOptions(Record, false, Validator))
3663 return false;
3664 break;
3665
3666 case TARGET_OPTIONS:
3667 if (ParseTargetOptions(Record, false, Validator))
3668 return false;
3669 break;
3670
Douglas Gregor5f3d8222012-10-24 15:17:15 +00003671 case DIAGNOSTIC_OPTIONS:
3672 if (ParseDiagnosticOptions(Record, false, Validator))
3673 return false;
3674 break;
3675
Douglas Gregorbbf38312012-10-24 16:50:34 +00003676 case FILE_SYSTEM_OPTIONS:
3677 if (ParseFileSystemOptions(Record, false, Validator))
3678 return false;
3679 break;
3680
3681 case HEADER_SEARCH_OPTIONS:
3682 if (ParseHeaderSearchOptions(Record, false, Validator))
3683 return false;
3684 break;
3685
Douglas Gregor87699242012-10-25 00:07:54 +00003686 case PREPROCESSOR_OPTIONS: {
3687 std::string IgnoredSuggestedPredefines;
3688 if (ParsePreprocessorOptions(Record, false, Validator,
3689 IgnoredSuggestedPredefines))
Douglas Gregora71a7d82012-10-24 20:05:57 +00003690 return false;
3691 break;
Douglas Gregor87699242012-10-25 00:07:54 +00003692 }
Douglas Gregora71a7d82012-10-24 20:05:57 +00003693
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00003694 default:
3695 // No other validation to perform.
3696 break;
3697 }
3698 }
3699 }
3700
3701 return true;
3702}
3703
Douglas Gregor4825fd72012-10-22 22:50:17 +00003704bool ASTReader::ReadSubmoduleBlock(ModuleFile &F) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003705 // Enter the submodule block.
3706 if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
3707 Error("malformed submodule block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003708 return true;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003709 }
3710
3711 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
Douglas Gregor26ced122011-12-01 00:59:36 +00003712 bool First = true;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003713 Module *CurrentModule = 0;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003714 RecordData Record;
3715 while (true) {
3716 unsigned Code = F.Stream.ReadCode();
3717 if (Code == llvm::bitc::END_BLOCK) {
3718 if (F.Stream.ReadBlockEnd()) {
3719 Error("error at end of submodule block in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003720 return true;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003721 }
Douglas Gregor4825fd72012-10-22 22:50:17 +00003722 return false;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003723 }
3724
3725 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
3726 // No known subblocks, always skip them.
3727 F.Stream.ReadSubBlockID();
3728 if (F.Stream.SkipBlock()) {
3729 Error("malformed block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003730 return true;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003731 }
3732 continue;
3733 }
3734
3735 if (Code == llvm::bitc::DEFINE_ABBREV) {
3736 F.Stream.ReadAbbrevRecord();
3737 continue;
3738 }
3739
3740 // Read a record.
3741 const char *BlobStart;
3742 unsigned BlobLen;
3743 Record.clear();
3744 switch (F.Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
3745 default: // Default behavior: ignore.
3746 break;
3747
3748 case SUBMODULE_DEFINITION: {
Douglas Gregor26ced122011-12-01 00:59:36 +00003749 if (First) {
3750 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003751 return true;
Douglas Gregor26ced122011-12-01 00:59:36 +00003752 }
3753
Douglas Gregore209e502011-12-06 01:10:29 +00003754 if (Record.size() < 7) {
Douglas Gregor1e123682011-12-05 22:27:44 +00003755 Error("malformed module definition");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003756 return true;
Douglas Gregor1e123682011-12-05 22:27:44 +00003757 }
3758
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003759 StringRef Name(BlobStart, BlobLen);
Douglas Gregore209e502011-12-06 01:10:29 +00003760 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[0]);
3761 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[1]);
3762 bool IsFramework = Record[2];
3763 bool IsExplicit = Record[3];
Douglas Gregora1f1fad2012-01-27 19:52:33 +00003764 bool IsSystem = Record[4];
3765 bool InferSubmodules = Record[5];
3766 bool InferExplicitSubmodules = Record[6];
3767 bool InferExportWildcard = Record[7];
Douglas Gregor1e123682011-12-05 22:27:44 +00003768
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003769 Module *ParentModule = 0;
Douglas Gregor26ced122011-12-01 00:59:36 +00003770 if (Parent)
3771 ParentModule = getSubmodule(Parent);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003772
3773 // Retrieve this (sub)module from the module map, creating it if
3774 // necessary.
3775 CurrentModule = ModMap.findOrCreateModule(Name, ParentModule,
3776 IsFramework,
3777 IsExplicit).first;
Douglas Gregore209e502011-12-06 01:10:29 +00003778 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
3779 if (GlobalIndex >= SubmodulesLoaded.size() ||
3780 SubmodulesLoaded[GlobalIndex]) {
Douglas Gregor26ced122011-12-01 00:59:36 +00003781 Error("too many submodules");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003782 return true;
Douglas Gregor26ced122011-12-01 00:59:36 +00003783 }
Douglas Gregora015cab2011-12-02 17:30:13 +00003784
Argyrios Kyrtzidisd64c26f2012-10-03 01:58:42 +00003785 CurrentModule->setASTFile(F.File);
Douglas Gregor305dc3e2011-12-20 00:28:52 +00003786 CurrentModule->IsFromModuleFile = true;
Douglas Gregora1f1fad2012-01-27 19:52:33 +00003787 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
Douglas Gregor1e123682011-12-05 22:27:44 +00003788 CurrentModule->InferSubmodules = InferSubmodules;
3789 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
3790 CurrentModule->InferExportWildcard = InferExportWildcard;
Douglas Gregora015cab2011-12-02 17:30:13 +00003791 if (DeserializationListener)
Douglas Gregore209e502011-12-06 01:10:29 +00003792 DeserializationListener->ModuleRead(GlobalID, CurrentModule);
Douglas Gregora015cab2011-12-02 17:30:13 +00003793
Douglas Gregore209e502011-12-06 01:10:29 +00003794 SubmodulesLoaded[GlobalIndex] = CurrentModule;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003795 break;
3796 }
3797
Douglas Gregor77d029f2011-12-08 19:11:24 +00003798 case SUBMODULE_UMBRELLA_HEADER: {
Douglas Gregor26ced122011-12-01 00:59:36 +00003799 if (First) {
3800 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003801 return true;
Douglas Gregor26ced122011-12-01 00:59:36 +00003802 }
3803
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003804 if (!CurrentModule)
3805 break;
3806
3807 StringRef FileName(BlobStart, BlobLen);
3808 if (const FileEntry *Umbrella = PP.getFileManager().getFile(FileName)) {
Douglas Gregor10694ce2011-12-08 17:39:04 +00003809 if (!CurrentModule->getUmbrellaHeader())
Douglas Gregore209e502011-12-06 01:10:29 +00003810 ModMap.setUmbrellaHeader(CurrentModule, Umbrella);
Douglas Gregor10694ce2011-12-08 17:39:04 +00003811 else if (CurrentModule->getUmbrellaHeader() != Umbrella) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003812 Error("mismatched umbrella headers in submodule");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003813 return true;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003814 }
3815 }
3816 break;
3817 }
3818
3819 case SUBMODULE_HEADER: {
Douglas Gregor26ced122011-12-01 00:59:36 +00003820 if (First) {
3821 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003822 return true;
Douglas Gregor26ced122011-12-01 00:59:36 +00003823 }
3824
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003825 if (!CurrentModule)
3826 break;
3827
3828 // FIXME: Be more lazy about this!
3829 StringRef FileName(BlobStart, BlobLen);
3830 if (const FileEntry *File = PP.getFileManager().getFile(FileName)) {
3831 if (std::find(CurrentModule->Headers.begin(),
3832 CurrentModule->Headers.end(),
3833 File) == CurrentModule->Headers.end())
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00003834 ModMap.addHeader(CurrentModule, File, false);
3835 }
3836 break;
3837 }
3838
3839 case SUBMODULE_EXCLUDED_HEADER: {
3840 if (First) {
3841 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003842 return true;
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00003843 }
3844
3845 if (!CurrentModule)
3846 break;
3847
3848 // FIXME: Be more lazy about this!
3849 StringRef FileName(BlobStart, BlobLen);
3850 if (const FileEntry *File = PP.getFileManager().getFile(FileName)) {
3851 if (std::find(CurrentModule->Headers.begin(),
3852 CurrentModule->Headers.end(),
3853 File) == CurrentModule->Headers.end())
3854 ModMap.addHeader(CurrentModule, File, true);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003855 }
3856 break;
3857 }
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00003858
3859 case SUBMODULE_TOPHEADER: {
3860 if (First) {
3861 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003862 return true;
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00003863 }
3864
3865 if (!CurrentModule)
3866 break;
3867
3868 // FIXME: Be more lazy about this!
3869 StringRef FileName(BlobStart, BlobLen);
3870 if (const FileEntry *File = PP.getFileManager().getFile(FileName))
3871 CurrentModule->TopHeaders.insert(File);
3872 break;
3873 }
3874
Douglas Gregor77d029f2011-12-08 19:11:24 +00003875 case SUBMODULE_UMBRELLA_DIR: {
3876 if (First) {
3877 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003878 return true;
Douglas Gregor77d029f2011-12-08 19:11:24 +00003879 }
3880
3881 if (!CurrentModule)
3882 break;
3883
3884 StringRef DirName(BlobStart, BlobLen);
3885 if (const DirectoryEntry *Umbrella
3886 = PP.getFileManager().getDirectory(DirName)) {
3887 if (!CurrentModule->getUmbrellaDir())
3888 ModMap.setUmbrellaDir(CurrentModule, Umbrella);
3889 else if (CurrentModule->getUmbrellaDir() != Umbrella) {
3890 Error("mismatched umbrella directories in submodule");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003891 return true;
Douglas Gregor77d029f2011-12-08 19:11:24 +00003892 }
3893 }
3894 break;
3895 }
3896
Douglas Gregor26ced122011-12-01 00:59:36 +00003897 case SUBMODULE_METADATA: {
3898 if (!First) {
3899 Error("submodule metadata record not at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003900 return true;
Douglas Gregor26ced122011-12-01 00:59:36 +00003901 }
3902 First = false;
3903
3904 F.BaseSubmoduleID = getTotalNumSubmodules();
Douglas Gregor26ced122011-12-01 00:59:36 +00003905 F.LocalNumSubmodules = Record[0];
3906 unsigned LocalBaseSubmoduleID = Record[1];
3907 if (F.LocalNumSubmodules > 0) {
3908 // Introduce the global -> local mapping for submodules within this
3909 // module.
3910 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
3911
3912 // Introduce the local -> global mapping for submodules within this
3913 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00003914 F.SubmoduleRemap.insertOrReplace(
Douglas Gregor26ced122011-12-01 00:59:36 +00003915 std::make_pair(LocalBaseSubmoduleID,
3916 F.BaseSubmoduleID - LocalBaseSubmoduleID));
3917
3918 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
3919 }
3920 break;
3921 }
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003922
Douglas Gregor55988682011-12-05 16:33:54 +00003923 case SUBMODULE_IMPORTS: {
3924 if (First) {
3925 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003926 return true;
Douglas Gregor55988682011-12-05 16:33:54 +00003927 }
3928
3929 if (!CurrentModule)
3930 break;
3931
3932 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
3933 UnresolvedModuleImportExport Unresolved;
3934 Unresolved.File = &F;
3935 Unresolved.Mod = CurrentModule;
3936 Unresolved.ID = Record[Idx];
3937 Unresolved.IsImport = true;
3938 Unresolved.IsWildcard = false;
3939 UnresolvedModuleImportExports.push_back(Unresolved);
3940 }
3941 break;
3942 }
3943
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003944 case SUBMODULE_EXPORTS: {
3945 if (First) {
3946 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003947 return true;
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003948 }
3949
3950 if (!CurrentModule)
3951 break;
3952
3953 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
Douglas Gregor55988682011-12-05 16:33:54 +00003954 UnresolvedModuleImportExport Unresolved;
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003955 Unresolved.File = &F;
Douglas Gregor55988682011-12-05 16:33:54 +00003956 Unresolved.Mod = CurrentModule;
3957 Unresolved.ID = Record[Idx];
3958 Unresolved.IsImport = false;
3959 Unresolved.IsWildcard = Record[Idx + 1];
3960 UnresolvedModuleImportExports.push_back(Unresolved);
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003961 }
3962
3963 // Once we've loaded the set of exports, there's no reason to keep
3964 // the parsed, unresolved exports around.
3965 CurrentModule->UnresolvedExports.clear();
3966 break;
3967 }
Douglas Gregor51f564f2011-12-31 04:05:44 +00003968 case SUBMODULE_REQUIRES: {
3969 if (First) {
3970 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003971 return true;
Douglas Gregor51f564f2011-12-31 04:05:44 +00003972 }
3973
3974 if (!CurrentModule)
3975 break;
3976
3977 CurrentModule->addRequirement(StringRef(BlobStart, BlobLen),
David Blaikie4e4d0842012-03-11 07:00:24 +00003978 Context.getLangOpts(),
Douglas Gregordc58aa72012-01-30 06:01:29 +00003979 Context.getTargetInfo());
Douglas Gregor51f564f2011-12-31 04:05:44 +00003980 break;
3981 }
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003982 }
3983 }
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003984}
3985
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003986/// \brief Parse the record that corresponds to a LangOptions data
3987/// structure.
3988///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003989/// This routine parses the language options from the AST file and then gives
3990/// them to the AST listener if one is set.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003991///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003992/// \returns true if the listener deems the file unacceptable, false otherwise.
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00003993bool ASTReader::ParseLanguageOptions(const RecordData &Record,
3994 bool Complain,
3995 ASTReaderListener &Listener) {
3996 LangOptions LangOpts;
3997 unsigned Idx = 0;
Douglas Gregor7d5e81b2011-09-13 18:26:39 +00003998#define LANGOPT(Name, Bits, Default, Description) \
3999 LangOpts.Name = Record[Idx++];
4000#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
4001 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
4002#include "clang/Basic/LangOptions.def"
John McCall260611a2012-06-20 06:18:46 +00004003
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00004004 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
4005 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
4006 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
4007
4008 unsigned Length = Record[Idx++];
4009 LangOpts.CurrentModule.assign(Record.begin() + Idx,
4010 Record.begin() + Idx + Length);
4011 return Listener.ReadLanguageOptions(LangOpts, Complain);
4012}
4013
4014bool ASTReader::ParseTargetOptions(const RecordData &Record,
4015 bool Complain,
4016 ASTReaderListener &Listener) {
4017 unsigned Idx = 0;
4018 TargetOptions TargetOpts;
4019 TargetOpts.Triple = ReadString(Record, Idx);
4020 TargetOpts.CPU = ReadString(Record, Idx);
4021 TargetOpts.ABI = ReadString(Record, Idx);
4022 TargetOpts.CXXABI = ReadString(Record, Idx);
4023 TargetOpts.LinkerVersion = ReadString(Record, Idx);
4024 for (unsigned N = Record[Idx++]; N; --N) {
4025 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
4026 }
4027 for (unsigned N = Record[Idx++]; N; --N) {
4028 TargetOpts.Features.push_back(ReadString(Record, Idx));
Douglas Gregor0a0428e2009-04-10 20:39:37 +00004029 }
Douglas Gregor0a0428e2009-04-10 20:39:37 +00004030
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00004031 return Listener.ReadTargetOptions(TargetOpts, Complain);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00004032}
4033
Douglas Gregor5f3d8222012-10-24 15:17:15 +00004034bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
4035 ASTReaderListener &Listener) {
4036 DiagnosticOptions DiagOpts;
4037 unsigned Idx = 0;
4038#define DIAGOPT(Name, Bits, Default) DiagOpts.Name = Record[Idx++];
4039#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
4040 DiagOpts.set##Name(static_cast<Type>(Record[Idx++]));
4041#include "clang/Basic/DiagnosticOptions.def"
4042
4043 for (unsigned N = Record[Idx++]; N; --N) {
4044 DiagOpts.Warnings.push_back(ReadString(Record, Idx));
4045 }
4046
4047 return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
4048}
4049
Douglas Gregor1b2c3c02012-10-24 15:49:58 +00004050bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
4051 ASTReaderListener &Listener) {
4052 FileSystemOptions FSOpts;
4053 unsigned Idx = 0;
4054 FSOpts.WorkingDir = ReadString(Record, Idx);
4055 return Listener.ReadFileSystemOptions(FSOpts, Complain);
4056}
4057
Douglas Gregorbbf38312012-10-24 16:50:34 +00004058bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
4059 bool Complain,
4060 ASTReaderListener &Listener) {
4061 HeaderSearchOptions HSOpts;
4062 unsigned Idx = 0;
4063 HSOpts.Sysroot = ReadString(Record, Idx);
4064
4065 // Include entries.
4066 for (unsigned N = Record[Idx++]; N; --N) {
4067 std::string Path = ReadString(Record, Idx);
4068 frontend::IncludeDirGroup Group
4069 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
4070 bool IsUserSupplied = Record[Idx++];
4071 bool IsFramework = Record[Idx++];
4072 bool IgnoreSysRoot = Record[Idx++];
4073 bool IsInternal = Record[Idx++];
4074 bool ImplicitExternC = Record[Idx++];
4075 HSOpts.UserEntries.push_back(
4076 HeaderSearchOptions::Entry(Path, Group, IsUserSupplied, IsFramework,
4077 IgnoreSysRoot, IsInternal, ImplicitExternC));
4078 }
4079
4080 // System header prefixes.
4081 for (unsigned N = Record[Idx++]; N; --N) {
4082 std::string Prefix = ReadString(Record, Idx);
4083 bool IsSystemHeader = Record[Idx++];
4084 HSOpts.SystemHeaderPrefixes.push_back(
4085 HeaderSearchOptions::SystemHeaderPrefix(Prefix, IsSystemHeader));
4086 }
4087
4088 HSOpts.ResourceDir = ReadString(Record, Idx);
4089 HSOpts.ModuleCachePath = ReadString(Record, Idx);
4090 HSOpts.DisableModuleHash = Record[Idx++];
4091 HSOpts.UseBuiltinIncludes = Record[Idx++];
4092 HSOpts.UseStandardSystemIncludes = Record[Idx++];
4093 HSOpts.UseStandardCXXIncludes = Record[Idx++];
4094 HSOpts.UseLibcxx = Record[Idx++];
4095
4096 return Listener.ReadHeaderSearchOptions(HSOpts, Complain);
4097}
4098
Douglas Gregora71a7d82012-10-24 20:05:57 +00004099bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
4100 bool Complain,
Douglas Gregor87699242012-10-25 00:07:54 +00004101 ASTReaderListener &Listener,
4102 std::string &SuggestedPredefines) {
Douglas Gregora71a7d82012-10-24 20:05:57 +00004103 PreprocessorOptions PPOpts;
4104 unsigned Idx = 0;
4105
4106 // Macro definitions/undefs
4107 for (unsigned N = Record[Idx++]; N; --N) {
4108 std::string Macro = ReadString(Record, Idx);
4109 bool IsUndef = Record[Idx++];
4110 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
4111 }
4112
4113 // Includes
4114 for (unsigned N = Record[Idx++]; N; --N) {
4115 PPOpts.Includes.push_back(ReadString(Record, Idx));
4116 }
4117
4118 // Macro Includes
4119 for (unsigned N = Record[Idx++]; N; --N) {
4120 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
4121 }
4122
Douglas Gregor4c0c7e82012-10-24 23:41:50 +00004123 PPOpts.UsePredefines = Record[Idx++];
Douglas Gregora71a7d82012-10-24 20:05:57 +00004124 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
4125 PPOpts.ImplicitPTHInclude = ReadString(Record, Idx);
4126 PPOpts.ObjCXXARCStandardLibrary =
4127 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
Douglas Gregor87699242012-10-25 00:07:54 +00004128 SuggestedPredefines.clear();
4129 return Listener.ReadPreprocessorOptions(PPOpts, Complain,
4130 SuggestedPredefines);
Douglas Gregora71a7d82012-10-24 20:05:57 +00004131}
4132
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004133std::pair<ModuleFile *, unsigned>
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00004134ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00004135 GlobalPreprocessedEntityMapType::iterator
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00004136 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00004137 assert(I != GlobalPreprocessedEntityMap.end() &&
4138 "Corrupted global preprocessed entity map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004139 ModuleFile *M = I->second;
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00004140 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
4141 return std::make_pair(M, LocalIndex);
4142}
4143
Argyrios Kyrtzidis632dcc92012-10-02 16:10:51 +00004144std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
4145ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
4146 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
4147 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
4148 Mod.NumPreprocessedEntities);
4149
4150 return std::make_pair(PreprocessingRecord::iterator(),
4151 PreprocessingRecord::iterator());
4152}
4153
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00004154std::pair<ASTReader::ModuleDeclIterator, ASTReader::ModuleDeclIterator>
4155ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
4156 return std::make_pair(ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
4157 ModuleDeclIterator(this, &Mod,
4158 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
4159}
4160
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00004161PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
4162 PreprocessedEntityID PPID = Index+1;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004163 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
4164 ModuleFile &M = *PPInfo.first;
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00004165 unsigned LocalIndex = PPInfo.second;
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00004166 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
Douglas Gregor4800a5c2011-02-08 21:58:10 +00004167
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00004168 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00004169 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00004170
4171 unsigned Code = M.PreprocessorDetailCursor.ReadCode();
4172 switch (Code) {
4173 case llvm::bitc::END_BLOCK:
4174 return 0;
4175
4176 case llvm::bitc::ENTER_SUBBLOCK:
4177 Error("unexpected subblock record in preprocessor detail block");
4178 return 0;
4179
4180 case llvm::bitc::DEFINE_ABBREV:
4181 Error("unexpected abbrevation record in preprocessor detail block");
4182 return 0;
4183
4184 default:
4185 break;
4186 }
4187
4188 if (!PP.getPreprocessingRecord()) {
4189 Error("no preprocessing record");
4190 return 0;
4191 }
4192
4193 // Read the record.
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00004194 SourceRange Range(ReadSourceLocation(M, PPOffs.Begin),
4195 ReadSourceLocation(M, PPOffs.End));
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00004196 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
4197 const char *BlobStart = 0;
4198 unsigned BlobLen = 0;
4199 RecordData Record;
4200 PreprocessorDetailRecordTypes RecType =
4201 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.ReadRecord(
4202 Code, Record, BlobStart, BlobLen);
4203 switch (RecType) {
4204 case PPD_MACRO_EXPANSION: {
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00004205 bool isBuiltin = Record[0];
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00004206 IdentifierInfo *Name = 0;
4207 MacroDefinition *Def = 0;
4208 if (isBuiltin)
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00004209 Name = getLocalIdentifier(M, Record[1]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00004210 else {
4211 PreprocessedEntityID
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00004212 GlobalID = getGlobalPreprocessedEntityID(M, Record[1]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00004213 Def =cast<MacroDefinition>(PPRec.getLoadedPreprocessedEntity(GlobalID-1));
4214 }
4215
4216 MacroExpansion *ME;
4217 if (isBuiltin)
4218 ME = new (PPRec) MacroExpansion(Name, Range);
4219 else
4220 ME = new (PPRec) MacroExpansion(Def, Range);
4221
4222 return ME;
4223 }
4224
4225 case PPD_MACRO_DEFINITION: {
4226 // Decode the identifier info and then check again; if the macro is
4227 // still defined and associated with the identifier,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00004228 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00004229 MacroDefinition *MD
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00004230 = new (PPRec) MacroDefinition(II, Range);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00004231
4232 if (DeserializationListener)
4233 DeserializationListener->MacroDefinitionRead(PPID, MD);
4234
4235 return MD;
4236 }
4237
4238 case PPD_INCLUSION_DIRECTIVE: {
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00004239 const char *FullFileNameStart = BlobStart + Record[0];
Argyrios Kyrtzidis29f98b42012-03-08 01:08:28 +00004240 StringRef FullFileName(FullFileNameStart, BlobLen - Record[0]);
4241 const FileEntry *File = 0;
4242 if (!FullFileName.empty())
4243 File = PP.getFileManager().getFile(FullFileName);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00004244
4245 // FIXME: Stable encoding
4246 InclusionDirective::InclusionKind Kind
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00004247 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00004248 InclusionDirective *ID
4249 = new (PPRec) InclusionDirective(PPRec, Kind,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00004250 StringRef(BlobStart, Record[0]),
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00004251 Record[1], Record[3],
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00004252 File,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00004253 Range);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00004254 return ID;
4255 }
4256 }
David Blaikie7530c032012-01-17 06:56:22 +00004257
4258 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00004259}
4260
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00004261/// \brief \arg SLocMapI points at a chunk of a module that contains no
4262/// preprocessed entities or the entities it contains are not the ones we are
4263/// looking for. Find the next module that contains entities and return the ID
4264/// of the first entry.
4265PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
4266 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
4267 ++SLocMapI;
4268 for (GlobalSLocOffsetMapType::const_iterator
4269 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004270 ModuleFile &M = *SLocMapI->second;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00004271 if (M.NumPreprocessedEntities)
4272 return getGlobalPreprocessedEntityID(M, M.BasePreprocessedEntityID);
4273 }
4274
4275 return getTotalNumPreprocessedEntities();
4276}
4277
4278namespace {
4279
4280template <unsigned PPEntityOffset::*PPLoc>
4281struct PPEntityComp {
4282 const ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004283 ModuleFile &M;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00004284
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004285 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) { }
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00004286
Benjamin Kramer88df1252011-09-21 06:42:26 +00004287 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
4288 SourceLocation LHS = getLoc(L);
4289 SourceLocation RHS = getLoc(R);
4290 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4291 }
4292
4293 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00004294 SourceLocation LHS = getLoc(L);
4295 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4296 }
4297
Benjamin Kramer88df1252011-09-21 06:42:26 +00004298 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00004299 SourceLocation RHS = getLoc(R);
4300 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4301 }
4302
4303 SourceLocation getLoc(const PPEntityOffset &PPE) const {
4304 return Reader.ReadSourceLocation(M, PPE.*PPLoc);
4305 }
4306};
4307
4308}
4309
4310/// \brief Returns the first preprocessed entity ID that ends after \arg BLoc.
4311PreprocessedEntityID
4312ASTReader::findBeginPreprocessedEntity(SourceLocation BLoc) const {
4313 if (SourceMgr.isLocalSourceLocation(BLoc))
4314 return getTotalNumPreprocessedEntities();
4315
4316 GlobalSLocOffsetMapType::const_iterator
4317 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
4318 BLoc.getOffset());
4319 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
4320 "Corrupted global sloc offset map");
4321
4322 if (SLocMapI->second->NumPreprocessedEntities == 0)
4323 return findNextPreprocessedEntity(SLocMapI);
4324
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004325 ModuleFile &M = *SLocMapI->second;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00004326 typedef const PPEntityOffset *pp_iterator;
4327 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
4328 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
Argyrios Kyrtzidis4cd06342011-09-22 21:17:02 +00004329
4330 size_t Count = M.NumPreprocessedEntities;
4331 size_t Half;
4332 pp_iterator First = pp_begin;
4333 pp_iterator PPI;
4334
4335 // Do a binary search manually instead of using std::lower_bound because
4336 // The end locations of entities may be unordered (when a macro expansion
4337 // is inside another macro argument), but for this case it is not important
4338 // whether we get the first macro expansion or its containing macro.
4339 while (Count > 0) {
4340 Half = Count/2;
4341 PPI = First;
4342 std::advance(PPI, Half);
4343 if (SourceMgr.isBeforeInTranslationUnit(ReadSourceLocation(M, PPI->End),
4344 BLoc)){
4345 First = PPI;
4346 ++First;
4347 Count = Count - Half - 1;
4348 } else
4349 Count = Half;
4350 }
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00004351
4352 if (PPI == pp_end)
4353 return findNextPreprocessedEntity(SLocMapI);
4354
4355 return getGlobalPreprocessedEntityID(M,
4356 M.BasePreprocessedEntityID + (PPI - pp_begin));
4357}
4358
4359/// \brief Returns the first preprocessed entity ID that begins after \arg ELoc.
4360PreprocessedEntityID
4361ASTReader::findEndPreprocessedEntity(SourceLocation ELoc) const {
4362 if (SourceMgr.isLocalSourceLocation(ELoc))
4363 return getTotalNumPreprocessedEntities();
4364
4365 GlobalSLocOffsetMapType::const_iterator
4366 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
4367 ELoc.getOffset());
4368 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
4369 "Corrupted global sloc offset map");
4370
4371 if (SLocMapI->second->NumPreprocessedEntities == 0)
4372 return findNextPreprocessedEntity(SLocMapI);
4373
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004374 ModuleFile &M = *SLocMapI->second;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00004375 typedef const PPEntityOffset *pp_iterator;
4376 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
4377 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
4378 pp_iterator PPI =
4379 std::upper_bound(pp_begin, pp_end, ELoc,
4380 PPEntityComp<&PPEntityOffset::Begin>(*this, M));
4381
4382 if (PPI == pp_end)
4383 return findNextPreprocessedEntity(SLocMapI);
4384
4385 return getGlobalPreprocessedEntityID(M,
4386 M.BasePreprocessedEntityID + (PPI - pp_begin));
4387}
4388
4389/// \brief Returns a pair of [Begin, End) indices of preallocated
4390/// preprocessed entities that \arg Range encompasses.
4391std::pair<unsigned, unsigned>
4392 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
4393 if (Range.isInvalid())
4394 return std::make_pair(0,0);
4395 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
4396
4397 PreprocessedEntityID BeginID = findBeginPreprocessedEntity(Range.getBegin());
4398 PreprocessedEntityID EndID = findEndPreprocessedEntity(Range.getEnd());
4399 return std::make_pair(BeginID, EndID);
4400}
4401
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00004402/// \brief Optionally returns true or false if the preallocated preprocessed
4403/// entity with index \arg Index came from file \arg FID.
4404llvm::Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
4405 FileID FID) {
4406 if (FID.isInvalid())
4407 return false;
4408
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004409 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
4410 ModuleFile &M = *PPInfo.first;
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00004411 unsigned LocalIndex = PPInfo.second;
4412 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
4413
4414 SourceLocation Loc = ReadSourceLocation(M, PPOffs.Begin);
4415 if (Loc.isInvalid())
4416 return false;
4417
4418 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
4419 return true;
4420 else
4421 return false;
4422}
4423
Douglas Gregord10a3812011-08-25 18:14:34 +00004424namespace {
4425 /// \brief Visitor used to search for information about a header file.
4426 class HeaderFileInfoVisitor {
4427 ASTReader &Reader;
4428 const FileEntry *FE;
4429
4430 llvm::Optional<HeaderFileInfo> HFI;
4431
4432 public:
4433 HeaderFileInfoVisitor(ASTReader &Reader, const FileEntry *FE)
4434 : Reader(Reader), FE(FE) { }
4435
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004436 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregord10a3812011-08-25 18:14:34 +00004437 HeaderFileInfoVisitor *This
4438 = static_cast<HeaderFileInfoVisitor *>(UserData);
4439
4440 HeaderFileInfoTrait Trait(This->Reader, M,
4441 &This->Reader.getPreprocessor().getHeaderSearchInfo(),
4442 M.HeaderFileFrameworkStrings,
4443 This->FE->getName());
4444
4445 HeaderFileInfoLookupTable *Table
4446 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
4447 if (!Table)
4448 return false;
4449
4450 // Look in the on-disk hash table for an entry for this file name.
4451 HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE->getName(),
4452 &Trait);
4453 if (Pos == Table->end())
4454 return false;
4455
4456 This->HFI = *Pos;
4457 return true;
4458 }
4459
4460 llvm::Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
4461 };
4462}
4463
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00004464HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
Douglas Gregord10a3812011-08-25 18:14:34 +00004465 HeaderFileInfoVisitor Visitor(*this, FE);
4466 ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor);
4467 if (llvm::Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00004468 if (Listener)
Douglas Gregord10a3812011-08-25 18:14:34 +00004469 Listener->ReadHeaderFileInfo(*HFI, FE->getUID());
4470 return *HFI;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00004471 }
4472
4473 return HeaderFileInfo();
4474}
4475
David Blaikied6471f72011-09-25 23:23:43 +00004476void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00004477 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004478 ModuleFile &F = *(*I);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00004479 unsigned Idx = 0;
4480 while (Idx < F.PragmaDiagMappings.size()) {
4481 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
Argyrios Kyrtzidis87429a02011-11-09 01:24:17 +00004482 Diag.DiagStates.push_back(*Diag.GetCurDiagState());
4483 Diag.DiagStatePoints.push_back(
4484 DiagnosticsEngine::DiagStatePoint(&Diag.DiagStates.back(),
4485 FullSourceLoc(Loc, SourceMgr)));
Douglas Gregorf62d43d2011-07-19 16:10:42 +00004486 while (1) {
4487 assert(Idx < F.PragmaDiagMappings.size() &&
4488 "Invalid data, didn't find '-1' marking end of diag/map pairs");
4489 if (Idx >= F.PragmaDiagMappings.size()) {
4490 break; // Something is messed up but at least avoid infinite loop in
4491 // release build.
4492 }
4493 unsigned DiagID = F.PragmaDiagMappings[Idx++];
4494 if (DiagID == (unsigned)-1) {
4495 break; // no more diag/map pairs for this location.
4496 }
4497 diag::Mapping Map = (diag::Mapping)F.PragmaDiagMappings[Idx++];
Argyrios Kyrtzidis87429a02011-11-09 01:24:17 +00004498 DiagnosticMappingInfo MappingInfo = Diag.makeMappingInfo(Map, Loc);
4499 Diag.GetCurDiagState()->setMappingInfo(DiagID, MappingInfo);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00004500 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00004501 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00004502 }
4503}
4504
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00004505/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004506ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Douglas Gregora119da02011-08-02 16:26:37 +00004507 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
Jonathan D. Turnere9b76c12011-07-20 21:31:32 +00004508 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004509 ModuleFile *M = I->second;
Douglas Gregore3605012011-08-02 18:32:54 +00004510 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00004511}
4512
4513/// \brief Read and return the type with the given index..
Douglas Gregor2cf26342009-04-09 22:27:44 +00004514///
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00004515/// The index is the type ID, shifted and minus the number of predefs. This
4516/// routine actually reads the record corresponding to the type at the given
4517/// location. It is a helper routine for GetType, which deals with reading type
4518/// IDs.
Douglas Gregor393f2492011-07-22 00:38:23 +00004519QualType ASTReader::readTypeRecord(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00004520 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00004521 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00004522
Douglas Gregor0b748912009-04-14 21:18:50 +00004523 // Keep track of where we are in the stream, then jump back there
4524 // after reading this type.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004525 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregor0b748912009-04-14 21:18:50 +00004526
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00004527 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redl27372b42010-08-11 18:52:41 +00004528
Douglas Gregord89275b2009-07-06 18:54:52 +00004529 // Note that we are loading a type record.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004530 Deserializing AType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00004531
Douglas Gregor393f2492011-07-22 00:38:23 +00004532 unsigned Idx = 0;
Sebastian Redlc3632732010-10-05 15:59:54 +00004533 DeclsCursor.JumpToBit(Loc.Offset);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004534 RecordData Record;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004535 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004536 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
4537 case TYPE_EXT_QUAL: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004538 if (Record.size() != 2) {
4539 Error("Incorrect encoding of extended qualifier type");
4540 return QualType();
4541 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004542 QualType Base = readType(*Loc.F, Record, Idx);
4543 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
Douglas Gregor35942772011-09-09 21:34:22 +00004544 return Context.getQualifiedType(Base, Quals);
Douglas Gregor6d473962009-04-15 22:00:08 +00004545 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004546
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004547 case TYPE_COMPLEX: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004548 if (Record.size() != 1) {
4549 Error("Incorrect encoding of complex type");
4550 return QualType();
4551 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004552 QualType ElemType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004553 return Context.getComplexType(ElemType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004554 }
4555
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004556 case TYPE_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004557 if (Record.size() != 1) {
4558 Error("Incorrect encoding of pointer type");
4559 return QualType();
4560 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004561 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004562 return Context.getPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004563 }
4564
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004565 case TYPE_BLOCK_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004566 if (Record.size() != 1) {
4567 Error("Incorrect encoding of block pointer type");
4568 return QualType();
4569 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004570 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004571 return Context.getBlockPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004572 }
4573
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004574 case TYPE_LVALUE_REFERENCE: {
Richard Smithdf1550f2011-04-12 10:38:03 +00004575 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004576 Error("Incorrect encoding of lvalue reference type");
4577 return QualType();
4578 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004579 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004580 return Context.getLValueReferenceType(PointeeType, Record[1]);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004581 }
4582
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004583 case TYPE_RVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004584 if (Record.size() != 1) {
4585 Error("Incorrect encoding of rvalue reference type");
4586 return QualType();
4587 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004588 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004589 return Context.getRValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004590 }
4591
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004592 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidis240437b2010-07-02 11:55:15 +00004593 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004594 Error("Incorrect encoding of member pointer type");
4595 return QualType();
4596 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004597 QualType PointeeType = readType(*Loc.F, Record, Idx);
4598 QualType ClassType = readType(*Loc.F, Record, Idx);
Douglas Gregor1ab55e92010-12-10 17:03:06 +00004599 if (PointeeType.isNull() || ClassType.isNull())
4600 return QualType();
4601
Douglas Gregor35942772011-09-09 21:34:22 +00004602 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregor2cf26342009-04-09 22:27:44 +00004603 }
4604
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004605 case TYPE_CONSTANT_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00004606 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004607 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4608 unsigned IndexTypeQuals = Record[2];
4609 unsigned Idx = 3;
4610 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004611 return Context.getConstantArrayType(ElementType, Size,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004612 ASM, IndexTypeQuals);
4613 }
4614
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004615 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00004616 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004617 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4618 unsigned IndexTypeQuals = Record[2];
Douglas Gregor35942772011-09-09 21:34:22 +00004619 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004620 }
4621
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004622 case TYPE_VARIABLE_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00004623 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregor0b748912009-04-14 21:18:50 +00004624 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4625 unsigned IndexTypeQuals = Record[2];
Sebastian Redlc3632732010-10-05 15:59:54 +00004626 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
4627 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
Douglas Gregor35942772011-09-09 21:34:22 +00004628 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004629 ASM, IndexTypeQuals,
4630 SourceRange(LBLoc, RBLoc));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004631 }
4632
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004633 case TYPE_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00004634 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004635 Error("incorrect encoding of vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004636 return QualType();
4637 }
4638
Douglas Gregor393f2492011-07-22 00:38:23 +00004639 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004640 unsigned NumElements = Record[1];
Bob Wilsone86d78c2010-11-10 21:56:12 +00004641 unsigned VecKind = Record[2];
Douglas Gregor35942772011-09-09 21:34:22 +00004642 return Context.getVectorType(ElementType, NumElements,
Bob Wilsone86d78c2010-11-10 21:56:12 +00004643 (VectorType::VectorKind)VecKind);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004644 }
4645
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004646 case TYPE_EXT_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00004647 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004648 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004649 return QualType();
4650 }
4651
Douglas Gregor393f2492011-07-22 00:38:23 +00004652 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004653 unsigned NumElements = Record[1];
Douglas Gregor35942772011-09-09 21:34:22 +00004654 return Context.getExtVectorType(ElementType, NumElements);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004655 }
4656
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004657 case TYPE_FUNCTION_NO_PROTO: {
John McCallf85e1932011-06-15 23:02:42 +00004658 if (Record.size() != 6) {
Douglas Gregora02b1472009-04-28 21:53:25 +00004659 Error("incorrect encoding of no-proto function type");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004660 return QualType();
4661 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004662 QualType ResultType = readType(*Loc.F, Record, Idx);
John McCallf85e1932011-06-15 23:02:42 +00004663 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
4664 (CallingConv)Record[4], Record[5]);
Douglas Gregor35942772011-09-09 21:34:22 +00004665 return Context.getFunctionNoProtoType(ResultType, Info);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004666 }
4667
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004668 case TYPE_FUNCTION_PROTO: {
Douglas Gregor393f2492011-07-22 00:38:23 +00004669 QualType ResultType = readType(*Loc.F, Record, Idx);
John McCalle23cf432010-12-14 08:05:40 +00004670
4671 FunctionProtoType::ExtProtoInfo EPI;
4672 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
Eli Friedmana49218e2011-04-09 08:18:08 +00004673 /*hasregparm*/ Record[2],
4674 /*regparm*/ Record[3],
John McCallf85e1932011-06-15 23:02:42 +00004675 static_cast<CallingConv>(Record[4]),
4676 /*produces*/ Record[5]);
John McCalle23cf432010-12-14 08:05:40 +00004677
John McCallf85e1932011-06-15 23:02:42 +00004678 unsigned Idx = 6;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004679 unsigned NumParams = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00004680 SmallVector<QualType, 16> ParamTypes;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004681 for (unsigned I = 0; I != NumParams; ++I)
Douglas Gregor393f2492011-07-22 00:38:23 +00004682 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
John McCalle23cf432010-12-14 08:05:40 +00004683
4684 EPI.Variadic = Record[Idx++];
Richard Smitheefb3d52012-02-10 09:58:53 +00004685 EPI.HasTrailingReturn = Record[Idx++];
John McCalle23cf432010-12-14 08:05:40 +00004686 EPI.TypeQuals = Record[Idx++];
Douglas Gregorc938c162011-01-26 05:01:58 +00004687 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
Sebastian Redl60618fa2011-03-12 11:50:43 +00004688 ExceptionSpecificationType EST =
4689 static_cast<ExceptionSpecificationType>(Record[Idx++]);
4690 EPI.ExceptionSpecType = EST;
Douglas Gregorb0d06e22012-04-04 00:34:49 +00004691 SmallVector<QualType, 2> Exceptions;
Sebastian Redl60618fa2011-03-12 11:50:43 +00004692 if (EST == EST_Dynamic) {
4693 EPI.NumExceptions = Record[Idx++];
Sebastian Redl60618fa2011-03-12 11:50:43 +00004694 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
Douglas Gregor393f2492011-07-22 00:38:23 +00004695 Exceptions.push_back(readType(*Loc.F, Record, Idx));
Sebastian Redl60618fa2011-03-12 11:50:43 +00004696 EPI.Exceptions = Exceptions.data();
4697 } else if (EST == EST_ComputedNoexcept) {
4698 EPI.NoexceptExpr = ReadExpr(*Loc.F);
Richard Smith7bb698a2012-04-21 17:47:47 +00004699 } else if (EST == EST_Uninstantiated) {
4700 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
4701 EPI.ExceptionSpecTemplate = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
Richard Smithb9d0b762012-07-27 04:22:15 +00004702 } else if (EST == EST_Unevaluated) {
4703 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
Sebastian Redl60618fa2011-03-12 11:50:43 +00004704 }
Douglas Gregor35942772011-09-09 21:34:22 +00004705 return Context.getFunctionType(ResultType, ParamTypes.data(), NumParams,
John McCalle23cf432010-12-14 08:05:40 +00004706 EPI);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004707 }
4708
Douglas Gregor409448c2011-07-21 22:35:25 +00004709 case TYPE_UNRESOLVED_USING: {
4710 unsigned Idx = 0;
Douglas Gregor35942772011-09-09 21:34:22 +00004711 return Context.getTypeDeclType(
Douglas Gregor409448c2011-07-21 22:35:25 +00004712 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
4713 }
4714
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004715 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00004716 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004717 Error("incorrect encoding of typedef type");
4718 return QualType();
4719 }
Douglas Gregor409448c2011-07-21 22:35:25 +00004720 unsigned Idx = 0;
4721 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004722 QualType Canonical = readType(*Loc.F, Record, Idx);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00004723 if (!Canonical.isNull())
Douglas Gregor35942772011-09-09 21:34:22 +00004724 Canonical = Context.getCanonicalType(Canonical);
4725 return Context.getTypedefType(Decl, Canonical);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00004726 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004727
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004728 case TYPE_TYPEOF_EXPR:
Douglas Gregor35942772011-09-09 21:34:22 +00004729 return Context.getTypeOfExprType(ReadExpr(*Loc.F));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004730
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004731 case TYPE_TYPEOF: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004732 if (Record.size() != 1) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004733 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004734 return QualType();
4735 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004736 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004737 return Context.getTypeOfType(UnderlyingType);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004738 }
Mike Stump1eb44332009-09-09 15:08:12 +00004739
Douglas Gregorf8af9822012-02-12 18:42:33 +00004740 case TYPE_DECLTYPE: {
4741 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
4742 return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
4743 }
Anders Carlsson395b4752009-06-24 19:06:50 +00004744
Sean Huntca63c202011-05-24 22:41:36 +00004745 case TYPE_UNARY_TRANSFORM: {
Douglas Gregor393f2492011-07-22 00:38:23 +00004746 QualType BaseType = readType(*Loc.F, Record, Idx);
4747 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
Sean Huntca63c202011-05-24 22:41:36 +00004748 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
Douglas Gregor35942772011-09-09 21:34:22 +00004749 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
Sean Huntca63c202011-05-24 22:41:36 +00004750 }
4751
Richard Smith34b41d92011-02-20 03:19:35 +00004752 case TYPE_AUTO:
Douglas Gregor35942772011-09-09 21:34:22 +00004753 return Context.getAutoType(readType(*Loc.F, Record, Idx));
Richard Smith34b41d92011-02-20 03:19:35 +00004754
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004755 case TYPE_RECORD: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004756 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004757 Error("incorrect encoding of record type");
4758 return QualType();
4759 }
Douglas Gregor409448c2011-07-21 22:35:25 +00004760 unsigned Idx = 0;
4761 bool IsDependent = Record[Idx++];
Douglas Gregor56ca8a92012-01-17 19:21:53 +00004762 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
4763 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
4764 QualType T = Context.getRecordType(RD);
John McCallf4c73712011-01-19 06:33:43 +00004765 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004766 return T;
4767 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004768
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004769 case TYPE_ENUM: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004770 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004771 Error("incorrect encoding of enum type");
4772 return QualType();
4773 }
Douglas Gregor409448c2011-07-21 22:35:25 +00004774 unsigned Idx = 0;
4775 bool IsDependent = Record[Idx++];
4776 QualType T
Douglas Gregor35942772011-09-09 21:34:22 +00004777 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
John McCallf4c73712011-01-19 06:33:43 +00004778 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004779 return T;
4780 }
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004781
John McCall9d156a72011-01-06 01:58:22 +00004782 case TYPE_ATTRIBUTED: {
4783 if (Record.size() != 3) {
4784 Error("incorrect encoding of attributed type");
4785 return QualType();
4786 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004787 QualType modifiedType = readType(*Loc.F, Record, Idx);
4788 QualType equivalentType = readType(*Loc.F, Record, Idx);
John McCall9d156a72011-01-06 01:58:22 +00004789 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
Douglas Gregor35942772011-09-09 21:34:22 +00004790 return Context.getAttributedType(kind, modifiedType, equivalentType);
John McCall9d156a72011-01-06 01:58:22 +00004791 }
4792
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004793 case TYPE_PAREN: {
4794 if (Record.size() != 1) {
4795 Error("incorrect encoding of paren type");
4796 return QualType();
4797 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004798 QualType InnerType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004799 return Context.getParenType(InnerType);
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004800 }
4801
Douglas Gregor7536dd52010-12-20 02:24:11 +00004802 case TYPE_PACK_EXPANSION: {
Douglas Gregorf9997a02011-02-01 15:24:58 +00004803 if (Record.size() != 2) {
Douglas Gregor7536dd52010-12-20 02:24:11 +00004804 Error("incorrect encoding of pack expansion type");
4805 return QualType();
4806 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004807 QualType Pattern = readType(*Loc.F, Record, Idx);
Douglas Gregor7536dd52010-12-20 02:24:11 +00004808 if (Pattern.isNull())
4809 return QualType();
Douglas Gregorcded4f62011-01-14 17:04:44 +00004810 llvm::Optional<unsigned> NumExpansions;
4811 if (Record[1])
4812 NumExpansions = Record[1] - 1;
Douglas Gregor35942772011-09-09 21:34:22 +00004813 return Context.getPackExpansionType(Pattern, NumExpansions);
Douglas Gregor7536dd52010-12-20 02:24:11 +00004814 }
4815
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004816 case TYPE_ELABORATED: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004817 unsigned Idx = 0;
4818 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004819 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004820 QualType NamedType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004821 return Context.getElaboratedType(Keyword, NNS, NamedType);
John McCall7da24312009-09-05 00:15:47 +00004822 }
4823
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004824 case TYPE_OBJC_INTERFACE: {
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004825 unsigned Idx = 0;
Douglas Gregor409448c2011-07-21 22:35:25 +00004826 ObjCInterfaceDecl *ItfD
4827 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
Douglas Gregor56ca8a92012-01-17 19:21:53 +00004828 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
John McCallc12c5bb2010-05-15 11:32:37 +00004829 }
4830
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004831 case TYPE_OBJC_OBJECT: {
John McCallc12c5bb2010-05-15 11:32:37 +00004832 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004833 QualType Base = readType(*Loc.F, Record, Idx);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004834 unsigned NumProtos = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00004835 SmallVector<ObjCProtocolDecl*, 4> Protos;
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004836 for (unsigned I = 0; I != NumProtos; ++I)
Douglas Gregor409448c2011-07-21 22:35:25 +00004837 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
Douglas Gregor35942772011-09-09 21:34:22 +00004838 return Context.getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004839 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004840
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004841 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00004842 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004843 QualType Pointee = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004844 return Context.getObjCObjectPointerType(Pointee);
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00004845 }
Argyrios Kyrtzidis24fab412009-09-29 19:42:55 +00004846
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004847 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCall49a832b2009-10-18 09:09:24 +00004848 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004849 QualType Parm = readType(*Loc.F, Record, Idx);
4850 QualType Replacement = readType(*Loc.F, Record, Idx);
John McCall49a832b2009-10-18 09:09:24 +00004851 return
Douglas Gregor35942772011-09-09 21:34:22 +00004852 Context.getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
John McCall49a832b2009-10-18 09:09:24 +00004853 Replacement);
4854 }
John McCall3cb0ebd2010-03-10 03:28:59 +00004855
Douglas Gregorc3069d62011-01-14 02:55:32 +00004856 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
4857 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004858 QualType Parm = readType(*Loc.F, Record, Idx);
Douglas Gregorc3069d62011-01-14 02:55:32 +00004859 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004860 return Context.getSubstTemplateTypeParmPackType(
Douglas Gregorc3069d62011-01-14 02:55:32 +00004861 cast<TemplateTypeParmType>(Parm),
4862 ArgPack);
4863 }
4864
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004865 case TYPE_INJECTED_CLASS_NAME: {
Douglas Gregor409448c2011-07-21 22:35:25 +00004866 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004867 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00004868 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004869 // for AST reading, too much interdependencies.
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00004870 return
Douglas Gregor35942772011-09-09 21:34:22 +00004871 QualType(new (Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCall3cb0ebd2010-03-10 03:28:59 +00004872 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004873
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004874 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004875 unsigned Idx = 0;
4876 unsigned Depth = Record[Idx++];
4877 unsigned Index = Record[Idx++];
4878 bool Pack = Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004879 TemplateTypeParmDecl *D
4880 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004881 return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004882 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004883
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004884 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00004885 unsigned Idx = 0;
4886 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004887 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor95eab172011-07-28 20:55:49 +00004888 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004889 QualType Canon = readType(*Loc.F, Record, Idx);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00004890 if (!Canon.isNull())
Douglas Gregor35942772011-09-09 21:34:22 +00004891 Canon = Context.getCanonicalType(Canon);
4892 return Context.getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00004893 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004894
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004895 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004896 unsigned Idx = 0;
4897 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004898 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor95eab172011-07-28 20:55:49 +00004899 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004900 unsigned NumArgs = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00004901 SmallVector<TemplateArgument, 8> Args;
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004902 Args.reserve(NumArgs);
4903 while (NumArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00004904 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
Douglas Gregor35942772011-09-09 21:34:22 +00004905 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004906 Args.size(), Args.data());
4907 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004908
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004909 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004910 unsigned Idx = 0;
4911
4912 // ArrayType
Douglas Gregor393f2492011-07-22 00:38:23 +00004913 QualType ElementType = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004914 ArrayType::ArraySizeModifier ASM
4915 = (ArrayType::ArraySizeModifier)Record[Idx++];
4916 unsigned IndexTypeQuals = Record[Idx++];
4917
4918 // DependentSizedArrayType
Sebastian Redlc3632732010-10-05 15:59:54 +00004919 Expr *NumElts = ReadExpr(*Loc.F);
4920 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004921
Douglas Gregor35942772011-09-09 21:34:22 +00004922 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004923 IndexTypeQuals, Brackets);
4924 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004925
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004926 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004927 unsigned Idx = 0;
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004928 bool IsDependent = Record[Idx++];
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004929 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
Chris Lattner5f9e2722011-07-23 10:55:15 +00004930 SmallVector<TemplateArgument, 8> Args;
Sebastian Redlc3632732010-10-05 15:59:54 +00004931 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004932 QualType Underlying = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004933 QualType T;
Richard Smith3e4c6c42011-05-05 21:57:07 +00004934 if (Underlying.isNull())
Douglas Gregor35942772011-09-09 21:34:22 +00004935 T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(),
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004936 Args.size());
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00004937 else
Douglas Gregor35942772011-09-09 21:34:22 +00004938 T = Context.getTemplateSpecializationType(Name, Args.data(),
Richard Smith3e4c6c42011-05-05 21:57:07 +00004939 Args.size(), Underlying);
John McCallf4c73712011-01-19 06:33:43 +00004940 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004941 return T;
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004942 }
Eli Friedmanb001de72011-10-06 23:00:33 +00004943
4944 case TYPE_ATOMIC: {
4945 if (Record.size() != 1) {
4946 Error("Incorrect encoding of atomic type");
4947 return QualType();
4948 }
4949 QualType ValueType = readType(*Loc.F, Record, Idx);
4950 return Context.getAtomicType(ValueType);
4951 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00004952 }
David Blaikie7530c032012-01-17 06:56:22 +00004953 llvm_unreachable("Invalid TypeCode!");
Douglas Gregor2cf26342009-04-09 22:27:44 +00004954}
4955
Sebastian Redlc3632732010-10-05 15:59:54 +00004956class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004957 ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004958 ModuleFile &F;
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004959 const ASTReader::RecordData &Record;
John McCalla1ee0c52009-10-16 21:56:05 +00004960 unsigned &Idx;
4961
Sebastian Redlc3632732010-10-05 15:59:54 +00004962 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
4963 unsigned &I) {
4964 return Reader.ReadSourceLocation(F, R, I);
4965 }
4966
Douglas Gregor409448c2011-07-21 22:35:25 +00004967 template<typename T>
4968 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
4969 return Reader.ReadDeclAs<T>(F, Record, Idx);
4970 }
4971
John McCalla1ee0c52009-10-16 21:56:05 +00004972public:
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004973 TypeLocReader(ASTReader &Reader, ModuleFile &F,
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004974 const ASTReader::RecordData &Record, unsigned &Idx)
Benjamin Kramerfacde172012-06-06 17:32:50 +00004975 : Reader(Reader), F(F), Record(Record), Idx(Idx)
Sebastian Redlc3632732010-10-05 15:59:54 +00004976 { }
John McCalla1ee0c52009-10-16 21:56:05 +00004977
John McCall51bd8032009-10-18 01:05:36 +00004978 // We want compile-time assurance that we've enumerated all of
4979 // these, so unfortunately we have to declare them first, then
4980 // define them out-of-line.
4981#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +00004982#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +00004983 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00004984#include "clang/AST/TypeLocNodes.def"
4985
John McCall51bd8032009-10-18 01:05:36 +00004986 void VisitFunctionTypeLoc(FunctionTypeLoc);
4987 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00004988};
4989
John McCall51bd8032009-10-18 01:05:36 +00004990void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCalla1ee0c52009-10-16 21:56:05 +00004991 // nothing to do
4992}
John McCall51bd8032009-10-18 01:05:36 +00004993void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004994 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
Douglas Gregorddf889a2010-01-18 18:04:31 +00004995 if (TL.needsExtraLocalData()) {
4996 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
4997 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
4998 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
4999 TL.setModeAttr(Record[Idx++]);
5000 }
John McCalla1ee0c52009-10-16 21:56:05 +00005001}
John McCall51bd8032009-10-18 01:05:36 +00005002void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00005003 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00005004}
John McCall51bd8032009-10-18 01:05:36 +00005005void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00005006 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00005007}
John McCall51bd8032009-10-18 01:05:36 +00005008void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00005009 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00005010}
John McCall51bd8032009-10-18 01:05:36 +00005011void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00005012 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00005013}
John McCall51bd8032009-10-18 01:05:36 +00005014void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00005015 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00005016}
John McCall51bd8032009-10-18 01:05:36 +00005017void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00005018 TL.setStarLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +00005019 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00005020}
John McCall51bd8032009-10-18 01:05:36 +00005021void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00005022 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
5023 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00005024 if (Record[Idx++])
Sebastian Redlc3632732010-10-05 15:59:54 +00005025 TL.setSizeExpr(Reader.ReadExpr(F));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00005026 else
John McCall51bd8032009-10-18 01:05:36 +00005027 TL.setSizeExpr(0);
5028}
5029void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
5030 VisitArrayTypeLoc(TL);
5031}
5032void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
5033 VisitArrayTypeLoc(TL);
5034}
5035void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
5036 VisitArrayTypeLoc(TL);
5037}
5038void TypeLocReader::VisitDependentSizedArrayTypeLoc(
5039 DependentSizedArrayTypeLoc TL) {
5040 VisitArrayTypeLoc(TL);
5041}
5042void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
5043 DependentSizedExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00005044 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00005045}
5046void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00005047 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00005048}
5049void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00005050 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00005051}
5052void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnara796aa442011-03-12 11:17:06 +00005053 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
Abramo Bagnara59c0a812012-10-04 21:42:10 +00005054 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5055 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnara796aa442011-03-12 11:17:06 +00005056 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00005057 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
Douglas Gregor409448c2011-07-21 22:35:25 +00005058 TL.setArg(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00005059 }
5060}
5061void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
5062 VisitFunctionTypeLoc(TL);
5063}
5064void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
5065 VisitFunctionTypeLoc(TL);
5066}
John McCalled976492009-12-04 22:46:56 +00005067void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00005068 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalled976492009-12-04 22:46:56 +00005069}
John McCall51bd8032009-10-18 01:05:36 +00005070void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00005071 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00005072}
5073void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00005074 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
5075 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5076 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00005077}
5078void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00005079 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
5080 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5081 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5082 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00005083}
5084void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00005085 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00005086}
Sean Huntca63c202011-05-24 22:41:36 +00005087void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
5088 TL.setKWLoc(ReadSourceLocation(Record, Idx));
5089 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5090 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5091 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5092}
Richard Smith34b41d92011-02-20 03:19:35 +00005093void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
5094 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5095}
John McCall51bd8032009-10-18 01:05:36 +00005096void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00005097 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00005098}
5099void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00005100 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00005101}
John McCall9d156a72011-01-06 01:58:22 +00005102void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
5103 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
5104 if (TL.hasAttrOperand()) {
5105 SourceRange range;
5106 range.setBegin(ReadSourceLocation(Record, Idx));
5107 range.setEnd(ReadSourceLocation(Record, Idx));
5108 TL.setAttrOperandParensRange(range);
5109 }
5110 if (TL.hasAttrExprOperand()) {
5111 if (Record[Idx++])
5112 TL.setAttrExprOperand(Reader.ReadExpr(F));
5113 else
5114 TL.setAttrExprOperand(0);
5115 } else if (TL.hasAttrEnumOperand())
5116 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
5117}
John McCall51bd8032009-10-18 01:05:36 +00005118void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00005119 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00005120}
John McCall49a832b2009-10-18 09:09:24 +00005121void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
5122 SubstTemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00005123 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall49a832b2009-10-18 09:09:24 +00005124}
Douglas Gregorc3069d62011-01-14 02:55:32 +00005125void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
5126 SubstTemplateTypeParmPackTypeLoc TL) {
5127 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5128}
John McCall51bd8032009-10-18 01:05:36 +00005129void TypeLocReader::VisitTemplateSpecializationTypeLoc(
5130 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +00005131 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00005132 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
5133 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5134 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall833ca992009-10-29 08:12:44 +00005135 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
5136 TL.setArgLocInfo(i,
Sebastian Redlc3632732010-10-05 15:59:54 +00005137 Reader.GetTemplateArgumentLocInfo(F,
5138 TL.getTypePtr()->getArg(i).getKind(),
5139 Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00005140}
Abramo Bagnara075f8f12010-12-10 16:29:40 +00005141void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
5142 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5143 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5144}
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005145void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +00005146 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor9e876872011-03-01 18:12:44 +00005147 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00005148}
John McCall3cb0ebd2010-03-10 03:28:59 +00005149void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00005150 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall3cb0ebd2010-03-10 03:28:59 +00005151}
Douglas Gregor4714c122010-03-31 17:34:00 +00005152void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +00005153 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor2494dd02011-03-01 01:34:45 +00005154 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00005155 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00005156}
John McCall33500952010-06-11 00:33:02 +00005157void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
5158 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +00005159 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor94fdffa2011-03-01 20:11:18 +00005160 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Abramo Bagnara66581d42012-02-06 22:45:07 +00005161 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnara55d23c92012-02-06 14:41:24 +00005162 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00005163 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5164 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall33500952010-06-11 00:33:02 +00005165 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
5166 TL.setArgLocInfo(I,
Sebastian Redlc3632732010-10-05 15:59:54 +00005167 Reader.GetTemplateArgumentLocInfo(F,
5168 TL.getTypePtr()->getArg(I).getKind(),
5169 Record, Idx));
John McCall33500952010-06-11 00:33:02 +00005170}
Douglas Gregor7536dd52010-12-20 02:24:11 +00005171void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
5172 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
5173}
John McCall51bd8032009-10-18 01:05:36 +00005174void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00005175 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallc12c5bb2010-05-15 11:32:37 +00005176}
5177void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
5178 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00005179 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5180 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00005181 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Sebastian Redlc3632732010-10-05 15:59:54 +00005182 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00005183}
John McCall54e14c42009-10-22 22:37:11 +00005184void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00005185 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall54e14c42009-10-22 22:37:11 +00005186}
Eli Friedmanb001de72011-10-06 23:00:33 +00005187void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
5188 TL.setKWLoc(ReadSourceLocation(Record, Idx));
5189 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5190 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5191}
John McCalla1ee0c52009-10-16 21:56:05 +00005192
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005193TypeSourceInfo *ASTReader::GetTypeSourceInfo(ModuleFile &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00005194 const RecordData &Record,
John McCalla1ee0c52009-10-16 21:56:05 +00005195 unsigned &Idx) {
Douglas Gregor393f2492011-07-22 00:38:23 +00005196 QualType InfoTy = readType(F, Record, Idx);
John McCalla1ee0c52009-10-16 21:56:05 +00005197 if (InfoTy.isNull())
5198 return 0;
5199
Douglas Gregor35942772011-09-09 21:34:22 +00005200 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
Sebastian Redlc3632732010-10-05 15:59:54 +00005201 TypeLocReader TLR(*this, F, Record, Idx);
John McCalla93c9342009-12-07 02:54:59 +00005202 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCalla1ee0c52009-10-16 21:56:05 +00005203 TLR.Visit(TL);
John McCalla93c9342009-12-07 02:54:59 +00005204 return TInfo;
John McCalla1ee0c52009-10-16 21:56:05 +00005205}
Douglas Gregor2cf26342009-04-09 22:27:44 +00005206
Sebastian Redl8538e8d2010-08-18 23:57:32 +00005207QualType ASTReader::GetType(TypeID ID) {
John McCall0953e762009-09-24 19:53:00 +00005208 unsigned FastQuals = ID & Qualifiers::FastMask;
5209 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregor2cf26342009-04-09 22:27:44 +00005210
Sebastian Redl8538e8d2010-08-18 23:57:32 +00005211 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00005212 QualType T;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00005213 switch ((PredefinedTypeIDs)Index) {
5214 case PREDEF_TYPE_NULL_ID: return QualType();
Douglas Gregor35942772011-09-09 21:34:22 +00005215 case PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
5216 case PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00005217
Sebastian Redl8538e8d2010-08-18 23:57:32 +00005218 case PREDEF_TYPE_CHAR_U_ID:
5219 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregor2cf26342009-04-09 22:27:44 +00005220 // FIXME: Check that the signedness of CharTy is correct!
Douglas Gregor35942772011-09-09 21:34:22 +00005221 T = Context.CharTy;
Douglas Gregor2cf26342009-04-09 22:27:44 +00005222 break;
5223
Douglas Gregor35942772011-09-09 21:34:22 +00005224 case PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
5225 case PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
5226 case PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
5227 case PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
5228 case PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
5229 case PREDEF_TYPE_UINT128_ID: T = Context.UnsignedInt128Ty; break;
5230 case PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
5231 case PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
5232 case PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
5233 case PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
5234 case PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
5235 case PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
5236 case PREDEF_TYPE_INT128_ID: T = Context.Int128Ty; break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00005237 case PREDEF_TYPE_HALF_ID: T = Context.HalfTy; break;
Douglas Gregor35942772011-09-09 21:34:22 +00005238 case PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
5239 case PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
5240 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
5241 case PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
5242 case PREDEF_TYPE_BOUND_MEMBER: T = Context.BoundMemberTy; break;
John McCall3c3b7f92011-10-25 17:37:35 +00005243 case PREDEF_TYPE_PSEUDO_OBJECT: T = Context.PseudoObjectTy; break;
Douglas Gregor35942772011-09-09 21:34:22 +00005244 case PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
5245 case PREDEF_TYPE_UNKNOWN_ANY: T = Context.UnknownAnyTy; break;
5246 case PREDEF_TYPE_NULLPTR_ID: T = Context.NullPtrTy; break;
5247 case PREDEF_TYPE_CHAR16_ID: T = Context.Char16Ty; break;
5248 case PREDEF_TYPE_CHAR32_ID: T = Context.Char32Ty; break;
5249 case PREDEF_TYPE_OBJC_ID: T = Context.ObjCBuiltinIdTy; break;
5250 case PREDEF_TYPE_OBJC_CLASS: T = Context.ObjCBuiltinClassTy; break;
5251 case PREDEF_TYPE_OBJC_SEL: T = Context.ObjCBuiltinSelTy; break;
5252 case PREDEF_TYPE_AUTO_DEDUCT: T = Context.getAutoDeductType(); break;
Douglas Gregor3b8043b2011-08-09 15:13:55 +00005253
5254 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
Douglas Gregor35942772011-09-09 21:34:22 +00005255 T = Context.getAutoRRefDeductType();
Douglas Gregor3b8043b2011-08-09 15:13:55 +00005256 break;
John McCall0ddaeb92011-10-17 18:09:15 +00005257
5258 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
5259 T = Context.ARCUnbridgedCastTy;
5260 break;
5261
Meador Ingefb40e3f2012-07-01 15:57:25 +00005262 case PREDEF_TYPE_VA_LIST_TAG:
5263 T = Context.getVaListTagType();
5264 break;
Eli Friedmana6c66ce2012-08-31 00:14:07 +00005265
5266 case PREDEF_TYPE_BUILTIN_FN:
5267 T = Context.BuiltinFnTy;
5268 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00005269 }
5270
5271 assert(!T.isNull() && "Unknown predefined type");
John McCall0953e762009-09-24 19:53:00 +00005272 return T.withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00005273 }
5274
Sebastian Redl8538e8d2010-08-18 23:57:32 +00005275 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00005276 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl07a353c2010-07-14 20:26:45 +00005277 if (TypesLoaded[Index].isNull()) {
Douglas Gregor393f2492011-07-22 00:38:23 +00005278 TypesLoaded[Index] = readTypeRecord(Index);
Douglas Gregor97475832010-10-05 18:37:06 +00005279 if (TypesLoaded[Index].isNull())
5280 return QualType();
5281
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005282 TypesLoaded[Index]->setFromAST();
Sebastian Redl30c514c2010-07-14 23:45:08 +00005283 if (DeserializationListener)
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00005284 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1476ed42010-07-16 16:36:56 +00005285 TypesLoaded[Index]);
Sebastian Redl07a353c2010-07-14 20:26:45 +00005286 }
Mike Stump1eb44332009-09-09 15:08:12 +00005287
John McCall0953e762009-09-24 19:53:00 +00005288 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00005289}
5290
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005291QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
Douglas Gregor393f2492011-07-22 00:38:23 +00005292 return GetType(getGlobalTypeID(F, LocalID));
5293}
5294
5295serialization::TypeID
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005296ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
Douglas Gregora119da02011-08-02 16:26:37 +00005297 unsigned FastQuals = LocalID & Qualifiers::FastMask;
5298 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
5299
5300 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
5301 return LocalID;
5302
5303 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5304 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
5305 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
5306
5307 unsigned GlobalIndex = LocalIndex + I->second;
5308 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
5309}
5310
John McCall833ca992009-10-29 08:12:44 +00005311TemplateArgumentLocInfo
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005312ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
Sebastian Redlc3632732010-10-05 15:59:54 +00005313 TemplateArgument::ArgKind Kind,
John McCall833ca992009-10-29 08:12:44 +00005314 const RecordData &Record,
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00005315 unsigned &Index) {
John McCall833ca992009-10-29 08:12:44 +00005316 switch (Kind) {
5317 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00005318 return ReadExpr(F);
John McCall833ca992009-10-29 08:12:44 +00005319 case TemplateArgument::Type:
Sebastian Redlc3632732010-10-05 15:59:54 +00005320 return GetTypeSourceInfo(F, Record, Index);
Douglas Gregor788cd062009-11-11 01:00:40 +00005321 case TemplateArgument::Template: {
Douglas Gregorb6744ef2011-03-02 17:09:35 +00005322 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
5323 Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00005324 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorb6744ef2011-03-02 17:09:35 +00005325 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregora7fc9012011-01-05 18:58:31 +00005326 SourceLocation());
5327 }
5328 case TemplateArgument::TemplateExpansion: {
Douglas Gregorb6744ef2011-03-02 17:09:35 +00005329 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
5330 Index);
Douglas Gregora7fc9012011-01-05 18:58:31 +00005331 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorba68eca2011-01-05 17:40:24 +00005332 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorb6744ef2011-03-02 17:09:35 +00005333 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregorba68eca2011-01-05 17:40:24 +00005334 EllipsisLoc);
Douglas Gregor788cd062009-11-11 01:00:40 +00005335 }
John McCall833ca992009-10-29 08:12:44 +00005336 case TemplateArgument::Null:
5337 case TemplateArgument::Integral:
5338 case TemplateArgument::Declaration:
Eli Friedmand7a6b162012-09-26 02:36:12 +00005339 case TemplateArgument::NullPtr:
John McCall833ca992009-10-29 08:12:44 +00005340 case TemplateArgument::Pack:
Eli Friedmand7a6b162012-09-26 02:36:12 +00005341 // FIXME: Is this right?
John McCall833ca992009-10-29 08:12:44 +00005342 return TemplateArgumentLocInfo();
5343 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00005344 llvm_unreachable("unexpected template argument loc");
John McCall833ca992009-10-29 08:12:44 +00005345}
5346
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00005347TemplateArgumentLoc
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005348ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00005349 const RecordData &Record, unsigned &Index) {
Sebastian Redlc3632732010-10-05 15:59:54 +00005350 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00005351
5352 if (Arg.getKind() == TemplateArgument::Expression) {
5353 if (Record[Index++]) // bool InfoHasSameExpr.
5354 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
5355 }
Sebastian Redlc3632732010-10-05 15:59:54 +00005356 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00005357 Record, Index));
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00005358}
5359
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005360Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall76bd1f32010-06-01 09:23:16 +00005361 return GetDecl(ID);
5362}
5363
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005364uint64_t ASTReader::readCXXBaseSpecifiers(ModuleFile &M, const RecordData &Record,
Douglas Gregore92b8a12011-08-04 00:01:48 +00005365 unsigned &Idx){
5366 if (Idx >= Record.size())
Douglas Gregor7c789c12010-10-29 22:39:52 +00005367 return 0;
Douglas Gregor7c789c12010-10-29 22:39:52 +00005368
Douglas Gregore92b8a12011-08-04 00:01:48 +00005369 unsigned LocalID = Record[Idx++];
5370 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
Douglas Gregor7c789c12010-10-29 22:39:52 +00005371}
5372
5373CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
Douglas Gregor8f1231b2011-07-22 06:10:01 +00005374 RecordLocation Loc = getLocalBitOffset(Offset);
5375 llvm::BitstreamCursor &Cursor = Loc.F->DeclsCursor;
Douglas Gregor7c789c12010-10-29 22:39:52 +00005376 SavedStreamPosition SavedPosition(Cursor);
Douglas Gregor8f1231b2011-07-22 06:10:01 +00005377 Cursor.JumpToBit(Loc.Offset);
Douglas Gregor7c789c12010-10-29 22:39:52 +00005378 ReadingKindTracker ReadingKind(Read_Decl, *this);
5379 RecordData Record;
5380 unsigned Code = Cursor.ReadCode();
5381 unsigned RecCode = Cursor.ReadRecord(Code, Record);
5382 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
5383 Error("Malformed AST file: missing C++ base specifiers");
5384 return 0;
5385 }
5386
5387 unsigned Idx = 0;
5388 unsigned NumBases = Record[Idx++];
Douglas Gregor35942772011-09-09 21:34:22 +00005389 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00005390 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
5391 for (unsigned I = 0; I != NumBases; ++I)
Douglas Gregor8f1231b2011-07-22 06:10:01 +00005392 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
Douglas Gregor7c789c12010-10-29 22:39:52 +00005393 return Bases;
5394}
5395
Douglas Gregor409448c2011-07-21 22:35:25 +00005396serialization::DeclID
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00005397ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00005398 if (LocalID < NUM_PREDEF_DECL_IDS)
Douglas Gregor496c7092011-08-03 15:48:04 +00005399 return LocalID;
5400
5401 ContinuousRangeMap<uint32_t, int, 2>::iterator I
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00005402 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
Douglas Gregor496c7092011-08-03 15:48:04 +00005403 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
5404
5405 return LocalID + I->second;
Douglas Gregor409448c2011-07-21 22:35:25 +00005406}
5407
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00005408bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005409 ModuleFile &M) const {
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00005410 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(ID);
5411 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5412 return &M == I->second;
5413}
5414
Douglas Gregorcff9f262012-01-27 01:47:08 +00005415ModuleFile *ASTReader::getOwningModuleFile(Decl *D) {
5416 if (!D->isFromASTFile())
5417 return 0;
5418 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
5419 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5420 return I->second;
5421}
5422
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00005423SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
5424 if (ID < NUM_PREDEF_DECL_IDS)
5425 return SourceLocation();
5426
5427 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
5428
5429 if (Index > DeclsLoaded.size()) {
5430 Error("declaration ID out-of-range for AST file");
5431 return SourceLocation();
5432 }
5433
5434 if (Decl *D = DeclsLoaded[Index])
5435 return D->getLocation();
5436
5437 unsigned RawLocation = 0;
5438 RecordLocation Rec = DeclCursorForID(ID, RawLocation);
5439 return ReadSourceLocation(*Rec.F, RawLocation);
5440}
5441
Sebastian Redl8538e8d2010-08-18 23:57:32 +00005442Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00005443 if (ID < NUM_PREDEF_DECL_IDS) {
5444 switch ((PredefinedDeclIDs)ID) {
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00005445 case PREDEF_DECL_NULL_ID:
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00005446 return 0;
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00005447
5448 case PREDEF_DECL_TRANSLATION_UNIT_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00005449 return Context.getTranslationUnitDecl();
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00005450
5451 case PREDEF_DECL_OBJC_ID_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00005452 return Context.getObjCIdDecl();
Douglas Gregor79d67262011-08-12 05:59:41 +00005453
Douglas Gregor7a27ea52011-08-12 06:17:30 +00005454 case PREDEF_DECL_OBJC_SEL_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00005455 return Context.getObjCSelDecl();
Douglas Gregor7a27ea52011-08-12 06:17:30 +00005456
Douglas Gregor79d67262011-08-12 05:59:41 +00005457 case PREDEF_DECL_OBJC_CLASS_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00005458 return Context.getObjCClassDecl();
Douglas Gregor772eeae2011-08-12 06:49:56 +00005459
Douglas Gregora6ea10e2012-01-17 18:09:05 +00005460 case PREDEF_DECL_OBJC_PROTOCOL_ID:
5461 return Context.getObjCProtocolDecl();
5462
Douglas Gregor772eeae2011-08-12 06:49:56 +00005463 case PREDEF_DECL_INT_128_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00005464 return Context.getInt128Decl();
Douglas Gregor772eeae2011-08-12 06:49:56 +00005465
5466 case PREDEF_DECL_UNSIGNED_INT_128_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00005467 return Context.getUInt128Decl();
Douglas Gregore97179c2011-09-08 01:46:34 +00005468
5469 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00005470 return Context.getObjCInstanceTypeDecl();
Meador Ingec5613b22012-06-16 03:34:49 +00005471
5472 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
5473 return Context.getBuiltinVaListDecl();
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00005474 }
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00005475 }
5476
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00005477 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
5478
Richard Smith2fbf3732011-12-20 04:39:57 +00005479 if (Index >= DeclsLoaded.size()) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00005480 assert(0 && "declaration ID out-of-range for AST file");
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005481 Error("declaration ID out-of-range for AST file");
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00005482 return 0;
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005483 }
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00005484
Douglas Gregorfd002a72011-12-16 22:37:11 +00005485 if (!DeclsLoaded[Index]) {
Douglas Gregor496c7092011-08-03 15:48:04 +00005486 ReadDeclRecord(ID);
Sebastian Redl30c514c2010-07-14 23:45:08 +00005487 if (DeserializationListener)
5488 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
5489 }
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005490
5491 return DeclsLoaded[Index];
Douglas Gregor2cf26342009-04-09 22:27:44 +00005492}
5493
Douglas Gregora1be2782011-12-17 23:38:30 +00005494DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
5495 DeclID GlobalID) {
5496 if (GlobalID < NUM_PREDEF_DECL_IDS)
5497 return GlobalID;
5498
5499 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
5500 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5501 ModuleFile *Owner = I->second;
5502
5503 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
5504 = M.GlobalToLocalDeclIDs.find(Owner);
5505 if (Pos == M.GlobalToLocalDeclIDs.end())
5506 return 0;
5507
5508 return GlobalID - Owner->BaseDeclID + Pos->second;
5509}
5510
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005511serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00005512 const RecordData &Record,
5513 unsigned &Idx) {
5514 if (Idx >= Record.size()) {
5515 Error("Corrupted AST file");
5516 return 0;
5517 }
5518
5519 return getGlobalDeclID(F, Record[Idx++]);
5520}
5521
Chris Lattner887e2b32009-04-27 05:46:25 +00005522/// \brief Resolve the offset of a statement into a statement.
5523///
5524/// This operation will read a new statement from the external
5525/// source each time it is called, and is meant to be used via a
5526/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005527Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00005528 // Switch case IDs are per Decl.
5529 ClearSwitchCaseIDs();
5530
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00005531 // Offset here is a global offset across the entire chain.
Douglas Gregor8f1231b2011-07-22 06:10:01 +00005532 RecordLocation Loc = getLocalBitOffset(Offset);
5533 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
5534 return ReadStmtFromStream(*Loc.F);
Douglas Gregor250fc9c2009-04-18 00:07:54 +00005535}
5536
Douglas Gregor851c75a2011-08-24 21:27:34 +00005537namespace {
5538 class FindExternalLexicalDeclsVisitor {
5539 ASTReader &Reader;
5540 const DeclContext *DC;
5541 bool (*isKindWeWant)(Decl::Kind);
Douglas Gregor2ea054f2011-08-26 22:04:51 +00005542
Douglas Gregor851c75a2011-08-24 21:27:34 +00005543 SmallVectorImpl<Decl*> &Decls;
5544 bool PredefsVisited[NUM_PREDEF_DECL_IDS];
5545
5546 public:
5547 FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC,
5548 bool (*isKindWeWant)(Decl::Kind),
5549 SmallVectorImpl<Decl*> &Decls)
5550 : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls)
5551 {
5552 for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I)
5553 PredefsVisited[I] = false;
5554 }
5555
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005556 static bool visit(ModuleFile &M, bool Preorder, void *UserData) {
Douglas Gregor851c75a2011-08-24 21:27:34 +00005557 if (Preorder)
5558 return false;
5559
5560 FindExternalLexicalDeclsVisitor *This
5561 = static_cast<FindExternalLexicalDeclsVisitor *>(UserData);
5562
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005563 ModuleFile::DeclContextInfosMap::iterator Info
Douglas Gregor851c75a2011-08-24 21:27:34 +00005564 = M.DeclContextInfos.find(This->DC);
5565 if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls)
5566 return false;
5567
5568 // Load all of the declaration IDs
5569 for (const KindDeclIDPair *ID = Info->second.LexicalDecls,
5570 *IDE = ID + Info->second.NumLexicalDecls;
5571 ID != IDE; ++ID) {
5572 if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first))
5573 continue;
5574
5575 // Don't add predefined declarations to the lexical context more
5576 // than once.
5577 if (ID->second < NUM_PREDEF_DECL_IDS) {
5578 if (This->PredefsVisited[ID->second])
5579 continue;
5580
5581 This->PredefsVisited[ID->second] = true;
5582 }
5583
Douglas Gregor2ea054f2011-08-26 22:04:51 +00005584 if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) {
5585 if (!This->DC->isDeclInLexicalTraversal(D))
5586 This->Decls.push_back(D);
5587 }
Douglas Gregor851c75a2011-08-24 21:27:34 +00005588 }
5589
5590 return false;
5591 }
5592 };
5593}
5594
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00005595ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00005596 bool (*isKindWeWant)(Decl::Kind),
Chris Lattner5f9e2722011-07-23 10:55:15 +00005597 SmallVectorImpl<Decl*> &Decls) {
Douglas Gregor0d95f772011-08-24 19:03:07 +00005598 // There might be lexical decls in multiple modules, for the TU at
Douglas Gregor851c75a2011-08-24 21:27:34 +00005599 // least. Walk all of the modules in the order they were loaded.
5600 FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls);
5601 ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor);
Douglas Gregor25123082009-04-22 22:34:57 +00005602 ++NumLexicalDeclContextsRead;
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00005603 return ELR_Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00005604}
5605
Douglas Gregor0d95f772011-08-24 19:03:07 +00005606namespace {
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00005607
5608class DeclIDComp {
5609 ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005610 ModuleFile &Mod;
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00005611
5612public:
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005613 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00005614
5615 bool operator()(LocalDeclID L, LocalDeclID R) const {
5616 SourceLocation LHS = getLocation(L);
5617 SourceLocation RHS = getLocation(R);
5618 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5619 }
5620
5621 bool operator()(SourceLocation LHS, LocalDeclID R) const {
5622 SourceLocation RHS = getLocation(R);
5623 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5624 }
5625
5626 bool operator()(LocalDeclID L, SourceLocation RHS) const {
5627 SourceLocation LHS = getLocation(L);
5628 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5629 }
5630
5631 SourceLocation getLocation(LocalDeclID ID) const {
5632 return Reader.getSourceManager().getFileLoc(
5633 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
5634 }
5635};
5636
5637}
5638
5639void ASTReader::FindFileRegionDecls(FileID File,
5640 unsigned Offset, unsigned Length,
5641 SmallVectorImpl<Decl *> &Decls) {
5642 SourceManager &SM = getSourceManager();
5643
5644 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
5645 if (I == FileDeclIDs.end())
5646 return;
5647
5648 FileDeclsInfo &DInfo = I->second;
5649 if (DInfo.Decls.empty())
5650 return;
5651
5652 SourceLocation
5653 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
5654 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
5655
5656 DeclIDComp DIDComp(*this, *DInfo.Mod);
5657 ArrayRef<serialization::LocalDeclID>::iterator
5658 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
5659 BeginLoc, DIDComp);
5660 if (BeginIt != DInfo.Decls.begin())
5661 --BeginIt;
5662
Argyrios Kyrtzidisc14a03d2011-11-23 20:27:36 +00005663 // If we are pointing at a top-level decl inside an objc container, we need
5664 // to backtrack until we find it otherwise we will fail to report that the
5665 // region overlaps with an objc container.
5666 while (BeginIt != DInfo.Decls.begin() &&
5667 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
5668 ->isTopLevelDeclInObjCContainer())
5669 --BeginIt;
5670
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00005671 ArrayRef<serialization::LocalDeclID>::iterator
5672 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
5673 EndLoc, DIDComp);
5674 if (EndIt != DInfo.Decls.end())
5675 ++EndIt;
5676
5677 for (ArrayRef<serialization::LocalDeclID>::iterator
5678 DIt = BeginIt; DIt != EndIt; ++DIt)
5679 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
5680}
5681
5682namespace {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005683 /// \brief ModuleFile visitor used to perform name lookup into a
Douglas Gregor0d95f772011-08-24 19:03:07 +00005684 /// declaration context.
5685 class DeclContextNameLookupVisitor {
5686 ASTReader &Reader;
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005687 llvm::SmallVectorImpl<const DeclContext *> &Contexts;
Douglas Gregor0d95f772011-08-24 19:03:07 +00005688 DeclarationName Name;
5689 SmallVectorImpl<NamedDecl *> &Decls;
5690
5691 public:
5692 DeclContextNameLookupVisitor(ASTReader &Reader,
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005693 SmallVectorImpl<const DeclContext *> &Contexts,
5694 DeclarationName Name,
Douglas Gregor0d95f772011-08-24 19:03:07 +00005695 SmallVectorImpl<NamedDecl *> &Decls)
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005696 : Reader(Reader), Contexts(Contexts), Name(Name), Decls(Decls) { }
Douglas Gregor0d95f772011-08-24 19:03:07 +00005697
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005698 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregor0d95f772011-08-24 19:03:07 +00005699 DeclContextNameLookupVisitor *This
5700 = static_cast<DeclContextNameLookupVisitor *>(UserData);
5701
5702 // Check whether we have any visible declaration information for
5703 // this context in this module.
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005704 ModuleFile::DeclContextInfosMap::iterator Info;
5705 bool FoundInfo = false;
5706 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5707 Info = M.DeclContextInfos.find(This->Contexts[I]);
5708 if (Info != M.DeclContextInfos.end() &&
5709 Info->second.NameLookupTableData) {
5710 FoundInfo = true;
5711 break;
5712 }
5713 }
Douglas Gregor0d95f772011-08-24 19:03:07 +00005714
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005715 if (!FoundInfo)
5716 return false;
5717
Douglas Gregor0d95f772011-08-24 19:03:07 +00005718 // Look for this name within this module.
5719 ASTDeclContextNameLookupTable *LookupTable =
Benjamin Kramerb1758c62012-04-15 12:36:49 +00005720 Info->second.NameLookupTableData;
Douglas Gregor0d95f772011-08-24 19:03:07 +00005721 ASTDeclContextNameLookupTable::iterator Pos
5722 = LookupTable->find(This->Name);
5723 if (Pos == LookupTable->end())
5724 return false;
5725
5726 bool FoundAnything = false;
5727 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
5728 for (; Data.first != Data.second; ++Data.first) {
5729 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first);
5730 if (!ND)
5731 continue;
5732
5733 if (ND->getDeclName() != This->Name) {
Axel Naumann3dd82f72012-10-01 09:51:27 +00005734 // A name might be null because the decl's redeclarable part is
5735 // currently read before reading its name. The lookup is triggered by
5736 // building that decl (likely indirectly), and so it is later in the
5737 // sense of "already existing" and can be ignored here.
Douglas Gregor0d95f772011-08-24 19:03:07 +00005738 continue;
5739 }
5740
5741 // Record this declaration.
5742 FoundAnything = true;
5743 This->Decls.push_back(ND);
5744 }
5745
5746 return FoundAnything;
5747 }
5748 };
5749}
5750
John McCall76bd1f32010-06-01 09:23:16 +00005751DeclContext::lookup_result
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005752ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +00005753 DeclarationName Name) {
Mike Stump1eb44332009-09-09 15:08:12 +00005754 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00005755 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00005756 if (!Name)
5757 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
5758 DeclContext::lookup_iterator(0));
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00005759
Chris Lattner5f9e2722011-07-23 10:55:15 +00005760 SmallVector<NamedDecl *, 64> Decls;
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005761
5762 // Compute the declaration contexts we need to look into. Multiple such
5763 // declaration contexts occur when two declaration contexts from disjoint
5764 // modules get merged, e.g., when two namespaces with the same name are
5765 // independently defined in separate modules.
5766 SmallVector<const DeclContext *, 2> Contexts;
5767 Contexts.push_back(DC);
5768
5769 if (DC->isNamespace()) {
5770 MergedDeclsMap::iterator Merged
5771 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5772 if (Merged != MergedDecls.end()) {
5773 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5774 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5775 }
5776 }
5777
5778 DeclContextNameLookupVisitor Visitor(*this, Contexts, Name, Decls);
Douglas Gregor0d95f772011-08-24 19:03:07 +00005779 ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor);
Douglas Gregor25123082009-04-22 22:34:57 +00005780 ++NumVisibleDeclContextsRead;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00005781 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall76bd1f32010-06-01 09:23:16 +00005782 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregor2cf26342009-04-09 22:27:44 +00005783}
5784
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005785namespace {
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005786 /// \brief ModuleFile visitor used to retrieve all visible names in a
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005787 /// declaration context.
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005788 class DeclContextAllNamesVisitor {
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005789 ASTReader &Reader;
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005790 llvm::SmallVectorImpl<const DeclContext *> &Contexts;
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005791 llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> > &Decls;
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005792
5793 public:
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005794 DeclContextAllNamesVisitor(ASTReader &Reader,
5795 SmallVectorImpl<const DeclContext *> &Contexts,
5796 llvm::DenseMap<DeclarationName,
5797 SmallVector<NamedDecl *, 8> > &Decls)
5798 : Reader(Reader), Contexts(Contexts), Decls(Decls) { }
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005799
5800 static bool visit(ModuleFile &M, void *UserData) {
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005801 DeclContextAllNamesVisitor *This
5802 = static_cast<DeclContextAllNamesVisitor *>(UserData);
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005803
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005804 // Check whether we have any visible declaration information for
5805 // this context in this module.
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005806 ModuleFile::DeclContextInfosMap::iterator Info;
5807 bool FoundInfo = false;
5808 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5809 Info = M.DeclContextInfos.find(This->Contexts[I]);
5810 if (Info != M.DeclContextInfos.end() &&
5811 Info->second.NameLookupTableData) {
5812 FoundInfo = true;
5813 break;
5814 }
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005815 }
5816
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005817 if (!FoundInfo)
5818 return false;
5819
5820 ASTDeclContextNameLookupTable *LookupTable =
5821 Info->second.NameLookupTableData;
5822 bool FoundAnything = false;
5823 for (ASTDeclContextNameLookupTable::data_iterator
5824 I = LookupTable->data_begin(), E = LookupTable->data_end();
5825 I != E; ++I) {
5826 ASTDeclContextNameLookupTrait::data_type Data = *I;
5827 for (; Data.first != Data.second; ++Data.first) {
5828 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M,
5829 *Data.first);
5830 if (!ND)
5831 continue;
5832
5833 // Record this declaration.
5834 FoundAnything = true;
5835 This->Decls[ND->getDeclName()].push_back(ND);
5836 }
5837 }
5838
5839 return FoundAnything;
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005840 }
5841 };
5842}
5843
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005844void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005845 if (!DC->hasExternalVisibleStorage())
5846 return;
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005847 llvm::DenseMap<DeclarationName, llvm::SmallVector<NamedDecl*, 8> > Decls;
5848
5849 // Compute the declaration contexts we need to look into. Multiple such
5850 // declaration contexts occur when two declaration contexts from disjoint
5851 // modules get merged, e.g., when two namespaces with the same name are
5852 // independently defined in separate modules.
5853 SmallVector<const DeclContext *, 2> Contexts;
5854 Contexts.push_back(DC);
5855
5856 if (DC->isNamespace()) {
5857 MergedDeclsMap::iterator Merged
5858 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5859 if (Merged != MergedDecls.end()) {
5860 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5861 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5862 }
5863 }
5864
5865 DeclContextAllNamesVisitor Visitor(*this, Contexts, Decls);
5866 ModuleMgr.visit(&DeclContextAllNamesVisitor::visit, &Visitor);
5867 ++NumVisibleDeclContextsRead;
5868
5869 for (llvm::DenseMap<DeclarationName,
5870 llvm::SmallVector<NamedDecl*, 8> >::iterator
5871 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
5872 SetExternalVisibleDeclsForName(DC, I->first, I->second);
5873 }
Argyrios Kyrtzidis394e5392012-04-26 18:34:14 +00005874 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005875}
5876
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005877/// \brief Under non-PCH compilation the consumer receives the objc methods
5878/// before receiving the implementation, and codegen depends on this.
5879/// We simulate this by deserializing and passing to consumer the methods of the
5880/// implementation before passing the deserialized implementation decl.
5881static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
5882 ASTConsumer *Consumer) {
5883 assert(ImplD && Consumer);
5884
5885 for (ObjCImplDecl::method_iterator
5886 I = ImplD->meth_begin(), E = ImplD->meth_end(); I != E; ++I)
David Blaikie581deb32012-06-06 20:45:41 +00005887 Consumer->HandleInterestingDecl(DeclGroupRef(*I));
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005888
5889 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
5890}
5891
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005892void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005893 assert(Consumer);
5894 while (!InterestingDecls.empty()) {
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005895 Decl *D = InterestingDecls.front();
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005896 InterestingDecls.pop_front();
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005897
Argyrios Kyrtzidis8d39c3d2011-11-30 23:18:26 +00005898 PassInterestingDeclToConsumer(D);
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005899 }
5900}
5901
Argyrios Kyrtzidis8d39c3d2011-11-30 23:18:26 +00005902void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
5903 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
5904 PassObjCImplDeclToConsumer(ImplD, Consumer);
5905 else
5906 Consumer->HandleInterestingDecl(DeclGroupRef(D));
5907}
5908
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005909void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor0af2ca42009-04-22 19:09:20 +00005910 this->Consumer = Consumer;
5911
Douglas Gregorfdd01722009-04-14 00:24:19 +00005912 if (!Consumer)
5913 return;
5914
5915 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005916 // Force deserialization of this decl, which will cause it to be queued for
5917 // passing to the consumer.
Daniel Dunbar04a0b502009-09-17 03:06:44 +00005918 GetDecl(ExternalDefinitions[I]);
Douglas Gregorfdd01722009-04-14 00:24:19 +00005919 }
Douglas Gregor1a995dd2011-09-15 18:47:32 +00005920 ExternalDefinitions.clear();
Douglas Gregorc62a2fe2009-04-25 00:41:30 +00005921
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005922 PassInterestingDeclsToConsumer();
Douglas Gregorfdd01722009-04-14 00:24:19 +00005923}
5924
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005925void ASTReader::PrintStats() {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005926 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregor2cf26342009-04-09 22:27:44 +00005927
Mike Stump1eb44332009-09-09 15:08:12 +00005928 unsigned NumTypesLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005929 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall0953e762009-09-24 19:53:00 +00005930 QualType());
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005931 unsigned NumDeclsLoaded
5932 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
5933 (Decl *)0);
5934 unsigned NumIdentifiersLoaded
5935 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
5936 IdentifiersLoaded.end(),
5937 (IdentifierInfo *)0);
Douglas Gregora8235d62012-10-09 23:05:51 +00005938 unsigned NumMacrosLoaded
5939 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
5940 MacrosLoaded.end(),
5941 (MacroInfo *)0);
Mike Stump1eb44332009-09-09 15:08:12 +00005942 unsigned NumSelectorsLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005943 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
5944 SelectorsLoaded.end(),
5945 Selector());
Douglas Gregor2d41cc12009-04-13 20:50:16 +00005946
Douglas Gregor4fed3f42009-04-27 18:38:38 +00005947 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
5948 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor0cdd7982011-07-21 18:46:38 +00005949 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00005950 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
5951 NumSLocEntriesRead, TotalNumSLocEntries,
5952 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005953 if (!TypesLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005954 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005955 NumTypesLoaded, (unsigned)TypesLoaded.size(),
5956 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
5957 if (!DeclsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005958 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005959 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
5960 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005961 if (!IdentifiersLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005962 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005963 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
5964 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Douglas Gregora8235d62012-10-09 23:05:51 +00005965 if (!MacrosLoaded.empty())
5966 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
5967 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
5968 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
Sebastian Redl725cd962010-08-04 20:40:17 +00005969 if (!SelectorsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005970 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redl725cd962010-08-04 20:40:17 +00005971 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
5972 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor83941df2009-04-25 17:48:32 +00005973 if (TotalNumStatements)
5974 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
5975 NumStatementsRead, TotalNumStatements,
5976 ((float)NumStatementsRead/TotalNumStatements * 100));
5977 if (TotalNumMacros)
5978 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
5979 NumMacrosRead, TotalNumMacros,
5980 ((float)NumMacrosRead/TotalNumMacros * 100));
5981 if (TotalLexicalDeclContexts)
5982 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
5983 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
5984 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
5985 * 100));
5986 if (TotalVisibleDeclContexts)
5987 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
5988 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
5989 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
5990 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005991 if (TotalNumMethodPoolEntries) {
Douglas Gregor83941df2009-04-25 17:48:32 +00005992 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005993 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
5994 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor83941df2009-04-25 17:48:32 +00005995 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005996 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor83941df2009-04-25 17:48:32 +00005997 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00005998 std::fprintf(stderr, "\n");
Douglas Gregor23d7df52011-07-21 19:50:14 +00005999 dump();
6000 std::fprintf(stderr, "\n");
6001}
6002
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006003template<typename Key, typename ModuleFile, unsigned InitialCapacity>
Douglas Gregor23d7df52011-07-21 19:50:14 +00006004static void
Chris Lattner5f9e2722011-07-23 10:55:15 +00006005dumpModuleIDMap(StringRef Name,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006006 const ContinuousRangeMap<Key, ModuleFile *,
Douglas Gregor23d7df52011-07-21 19:50:14 +00006007 InitialCapacity> &Map) {
6008 if (Map.begin() == Map.end())
6009 return;
6010
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006011 typedef ContinuousRangeMap<Key, ModuleFile *, InitialCapacity> MapType;
Douglas Gregor23d7df52011-07-21 19:50:14 +00006012 llvm::errs() << Name << ":\n";
6013 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
6014 I != IEnd; ++I) {
6015 llvm::errs() << " " << I->first << " -> " << I->second->FileName
6016 << "\n";
6017 }
6018}
6019
Douglas Gregor23d7df52011-07-21 19:50:14 +00006020void ASTReader::dump() {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006021 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
Douglas Gregor8f1231b2011-07-22 06:10:01 +00006022 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
Douglas Gregor23d7df52011-07-21 19:50:14 +00006023 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
Douglas Gregor1e849b62011-07-29 00:21:44 +00006024 dumpModuleIDMap("Global type map", GlobalTypeMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00006025 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00006026 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
Douglas Gregora8235d62012-10-09 23:05:51 +00006027 dumpModuleIDMap("Global macro map", GlobalMacroMap);
Douglas Gregor26ced122011-12-01 00:59:36 +00006028 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00006029 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00006030 dumpModuleIDMap("Global preprocessed entity map",
6031 GlobalPreprocessedEntityMap);
Douglas Gregor8df5c9b2011-08-02 11:12:41 +00006032
6033 llvm::errs() << "\n*** PCH/Modules Loaded:";
6034 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
6035 MEnd = ModuleMgr.end();
6036 M != MEnd; ++M)
6037 (*M)->dump();
Douglas Gregor2cf26342009-04-09 22:27:44 +00006038}
6039
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00006040/// Return the amount of memory used by memory buffers, breaking down
6041/// by heap-backed versus mmap'ed memory.
6042void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00006043 for (ModuleConstIterator I = ModuleMgr.begin(),
6044 E = ModuleMgr.end(); I != E; ++I) {
6045 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00006046 size_t bytes = buf->getBufferSize();
6047 switch (buf->getBufferKind()) {
6048 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
6049 sizes.malloc_bytes += bytes;
6050 break;
6051 case llvm::MemoryBuffer::MemoryBuffer_MMap:
6052 sizes.mmap_bytes += bytes;
6053 break;
6054 }
6055 }
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00006056 }
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00006057}
6058
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006059void ASTReader::InitializeSema(Sema &S) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00006060 SemaObj = &S;
Axel Naumann0ec56b72012-10-18 19:05:02 +00006061 S.addExternalSource(this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00006062
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00006063 // Makes sure any declarations that were deserialized "too early"
6064 // still get added to the identifier's declaration chains.
Douglas Gregor76dc8892010-09-24 23:29:12 +00006065 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00006066 SemaObj->pushExternalDeclIntoScope(PreloadedDecls[I],
6067 PreloadedDecls[I]->getDeclName());
Douglas Gregor668c1a42009-04-21 22:25:48 +00006068 }
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00006069 PreloadedDecls.clear();
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00006070
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00006071 // Load the offsets of the declarations that Sema references.
6072 // They will be lazily deserialized when needed.
6073 if (!SemaDeclRefs.empty()) {
6074 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
Douglas Gregor1e5b6f62011-07-28 00:57:24 +00006075 if (!SemaObj->StdNamespace)
6076 SemaObj->StdNamespace = SemaDeclRefs[0];
6077 if (!SemaObj->StdBadAlloc)
6078 SemaObj->StdBadAlloc = SemaDeclRefs[1];
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00006079 }
6080
Peter Collingbourne84bccea2011-02-15 19:46:30 +00006081 if (!FPPragmaOptions.empty()) {
6082 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
6083 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
6084 }
6085
6086 if (!OpenCLExtensions.empty()) {
6087 unsigned I = 0;
6088#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
6089#include "clang/Basic/OpenCLExtensions.def"
6090
6091 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
6092 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00006093}
6094
Douglas Gregor211f6e82011-08-20 04:39:52 +00006095IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00006096 // Note that we are loading an identifier.
6097 Deserializing AnIdentifier(this);
6098
Douglas Gregor057df202012-01-18 20:56:22 +00006099 IdentifierLookupVisitor Visitor(StringRef(NameStart, NameEnd - NameStart),
6100 /*PriorGeneration=*/0);
Douglas Gregor211f6e82011-08-20 04:39:52 +00006101 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
Douglas Gregoreee242f2011-10-27 09:33:13 +00006102 IdentifierInfo *II = Visitor.getIdentifierInfo();
Douglas Gregor057df202012-01-18 20:56:22 +00006103 markIdentifierUpToDate(II);
Douglas Gregoreee242f2011-10-27 09:33:13 +00006104 return II;
Douglas Gregor668c1a42009-04-21 22:25:48 +00006105}
6106
Douglas Gregor95f42922010-10-14 22:11:03 +00006107namespace clang {
6108 /// \brief An identifier-lookup iterator that enumerates all of the
6109 /// identifiers stored within a set of AST files.
6110 class ASTIdentifierIterator : public IdentifierIterator {
6111 /// \brief The AST reader whose identifiers are being enumerated.
6112 const ASTReader &Reader;
6113
6114 /// \brief The current index into the chain of AST files stored in
6115 /// the AST reader.
6116 unsigned Index;
6117
6118 /// \brief The current position within the identifier lookup table
6119 /// of the current AST file.
6120 ASTIdentifierLookupTable::key_iterator Current;
6121
6122 /// \brief The end position within the identifier lookup table of
6123 /// the current AST file.
6124 ASTIdentifierLookupTable::key_iterator End;
6125
6126 public:
6127 explicit ASTIdentifierIterator(const ASTReader &Reader);
6128
Chris Lattner5f9e2722011-07-23 10:55:15 +00006129 virtual StringRef Next();
Douglas Gregor95f42922010-10-14 22:11:03 +00006130 };
6131}
6132
6133ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00006134 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
Douglas Gregor95f42922010-10-14 22:11:03 +00006135 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00006136 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
Douglas Gregor95f42922010-10-14 22:11:03 +00006137 Current = IdTable->key_begin();
6138 End = IdTable->key_end();
6139}
6140
Chris Lattner5f9e2722011-07-23 10:55:15 +00006141StringRef ASTIdentifierIterator::Next() {
Douglas Gregor95f42922010-10-14 22:11:03 +00006142 while (Current == End) {
6143 // If we have exhausted all of our AST files, we're done.
6144 if (Index == 0)
Chris Lattner5f9e2722011-07-23 10:55:15 +00006145 return StringRef();
Douglas Gregor95f42922010-10-14 22:11:03 +00006146
6147 --Index;
6148 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00006149 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
6150 IdentifierLookupTable;
Douglas Gregor95f42922010-10-14 22:11:03 +00006151 Current = IdTable->key_begin();
6152 End = IdTable->key_end();
6153 }
6154
6155 // We have any identifiers remaining in the current AST file; return
6156 // the next one.
6157 std::pair<const char*, unsigned> Key = *Current;
6158 ++Current;
Chris Lattner5f9e2722011-07-23 10:55:15 +00006159 return StringRef(Key.first, Key.second);
Douglas Gregor95f42922010-10-14 22:11:03 +00006160}
6161
6162IdentifierIterator *ASTReader::getIdentifiers() const {
6163 return new ASTIdentifierIterator(*this);
6164}
6165
Douglas Gregor3d15ab82011-08-25 14:51:20 +00006166namespace clang { namespace serialization {
6167 class ReadMethodPoolVisitor {
6168 ASTReader &Reader;
Douglas Gregor8efca6b2012-01-25 01:14:32 +00006169 Selector Sel;
6170 unsigned PriorGeneration;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00006171 llvm::SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
6172 llvm::SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00006173
Douglas Gregor3d15ab82011-08-25 14:51:20 +00006174 public:
Douglas Gregor8efca6b2012-01-25 01:14:32 +00006175 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
6176 unsigned PriorGeneration)
6177 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) { }
Douglas Gregor3d15ab82011-08-25 14:51:20 +00006178
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006179 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregor3d15ab82011-08-25 14:51:20 +00006180 ReadMethodPoolVisitor *This
6181 = static_cast<ReadMethodPoolVisitor *>(UserData);
6182
6183 if (!M.SelectorLookupTable)
6184 return false;
6185
Douglas Gregor8efca6b2012-01-25 01:14:32 +00006186 // If we've already searched this module file, skip it now.
6187 if (M.Generation <= This->PriorGeneration)
6188 return true;
6189
Douglas Gregor3d15ab82011-08-25 14:51:20 +00006190 ASTSelectorLookupTable *PoolTable
6191 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
6192 ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel);
6193 if (Pos == PoolTable->end())
6194 return false;
6195
6196 ++This->Reader.NumSelectorsRead;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00006197 // FIXME: Not quite happy with the statistics here. We probably should
6198 // disable this tracking when called via LoadSelector.
6199 // Also, should entries without methods count as misses?
Douglas Gregor3d15ab82011-08-25 14:51:20 +00006200 ++This->Reader.NumMethodPoolEntriesRead;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00006201 ASTSelectorLookupTrait::data_type Data = *Pos;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00006202 if (This->Reader.DeserializationListener)
6203 This->Reader.DeserializationListener->SelectorRead(Data.ID,
6204 This->Sel);
6205
6206 This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
6207 This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
6208 return true;
Sebastian Redl725cd962010-08-04 20:40:17 +00006209 }
Douglas Gregor3d15ab82011-08-25 14:51:20 +00006210
6211 /// \brief Retrieve the instance methods found by this visitor.
Douglas Gregor5ac4b692012-01-25 00:49:42 +00006212 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
6213 return InstanceMethods;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00006214 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00006215
Douglas Gregor3d15ab82011-08-25 14:51:20 +00006216 /// \brief Retrieve the instance methods found by this visitor.
Douglas Gregor5ac4b692012-01-25 00:49:42 +00006217 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
6218 return FactoryMethods;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00006219 }
6220 };
6221} } // end namespace clang::serialization
6222
Douglas Gregor5ac4b692012-01-25 00:49:42 +00006223/// \brief Add the given set of methods to the method list.
6224static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
6225 ObjCMethodList &List) {
6226 for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
6227 S.addMethodToGlobalList(&List, Methods[I]);
6228 }
6229}
6230
6231void ASTReader::ReadMethodPool(Selector Sel) {
Douglas Gregor8efca6b2012-01-25 01:14:32 +00006232 // Get the selector generation and update it to the current generation.
6233 unsigned &Generation = SelectorGeneration[Sel];
6234 unsigned PriorGeneration = Generation;
6235 Generation = CurrentGeneration;
6236
6237 // Search for methods defined with this selector.
6238 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
Douglas Gregor3d15ab82011-08-25 14:51:20 +00006239 ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor);
Douglas Gregor3d15ab82011-08-25 14:51:20 +00006240
Douglas Gregor5ac4b692012-01-25 00:49:42 +00006241 if (Visitor.getInstanceMethods().empty() &&
6242 Visitor.getFactoryMethods().empty()) {
Douglas Gregor3d15ab82011-08-25 14:51:20 +00006243 ++NumMethodPoolMisses;
Douglas Gregor5ac4b692012-01-25 00:49:42 +00006244 return;
6245 }
6246
6247 if (!getSema())
6248 return;
6249
6250 Sema &S = *getSema();
6251 Sema::GlobalMethodPool::iterator Pos
6252 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
6253
6254 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
6255 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00006256}
6257
Douglas Gregord8bba9c2011-06-28 16:20:02 +00006258void ASTReader::ReadKnownNamespaces(
Chris Lattner5f9e2722011-07-23 10:55:15 +00006259 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
Douglas Gregord8bba9c2011-06-28 16:20:02 +00006260 Namespaces.clear();
6261
6262 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
6263 if (NamespaceDecl *Namespace
6264 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
6265 Namespaces.push_back(Namespace);
6266 }
6267}
6268
Douglas Gregora8623202011-07-27 20:58:46 +00006269void ASTReader::ReadTentativeDefinitions(
6270 SmallVectorImpl<VarDecl *> &TentativeDefs) {
6271 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
6272 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
6273 if (Var)
6274 TentativeDefs.push_back(Var);
6275 }
6276 TentativeDefinitions.clear();
6277}
6278
Douglas Gregora2ee20a2011-07-27 21:45:57 +00006279void ASTReader::ReadUnusedFileScopedDecls(
6280 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
6281 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
6282 DeclaratorDecl *D
6283 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
6284 if (D)
6285 Decls.push_back(D);
6286 }
6287 UnusedFileScopedDecls.clear();
6288}
6289
Douglas Gregor0129b562011-07-27 21:57:17 +00006290void ASTReader::ReadDelegatingConstructors(
6291 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
6292 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
6293 CXXConstructorDecl *D
6294 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
6295 if (D)
6296 Decls.push_back(D);
6297 }
6298 DelegatingCtorDecls.clear();
6299}
6300
Douglas Gregord58a0a52011-07-28 00:39:29 +00006301void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
6302 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
6303 TypedefNameDecl *D
6304 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
6305 if (D)
6306 Decls.push_back(D);
6307 }
6308 ExtVectorDecls.clear();
6309}
6310
Douglas Gregora126f172011-07-28 00:53:40 +00006311void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) {
6312 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
6313 CXXRecordDecl *D
6314 = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I]));
6315 if (D)
6316 Decls.push_back(D);
6317 }
6318 DynamicClasses.clear();
6319}
6320
Douglas Gregorec12ce22011-07-28 14:20:37 +00006321void
6322ASTReader::ReadLocallyScopedExternalDecls(SmallVectorImpl<NamedDecl *> &Decls) {
6323 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
6324 NamedDecl *D
6325 = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
6326 if (D)
6327 Decls.push_back(D);
6328 }
6329 LocallyScopedExternalDecls.clear();
6330}
6331
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00006332void ASTReader::ReadReferencedSelectors(
6333 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
6334 if (ReferencedSelectorsData.empty())
6335 return;
6336
6337 // If there are @selector references added them to its pool. This is for
6338 // implementation of -Wselector.
6339 unsigned int DataSize = ReferencedSelectorsData.size()-1;
6340 unsigned I = 0;
6341 while (I < DataSize) {
6342 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
6343 SourceLocation SelLoc
6344 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
6345 Sels.push_back(std::make_pair(Sel, SelLoc));
6346 }
6347 ReferencedSelectorsData.clear();
6348}
6349
Douglas Gregor31e37b22011-07-28 18:09:57 +00006350void ASTReader::ReadWeakUndeclaredIdentifiers(
6351 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
6352 if (WeakUndeclaredIdentifiers.empty())
6353 return;
6354
6355 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
6356 IdentifierInfo *WeakId
6357 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
6358 IdentifierInfo *AliasId
6359 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
6360 SourceLocation Loc
6361 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
6362 bool Used = WeakUndeclaredIdentifiers[I++];
6363 WeakInfo WI(AliasId, Loc);
6364 WI.setUsed(Used);
6365 WeakIDs.push_back(std::make_pair(WeakId, WI));
6366 }
6367 WeakUndeclaredIdentifiers.clear();
6368}
6369
Douglas Gregordfe65432011-07-28 19:11:31 +00006370void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
6371 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
6372 ExternalVTableUse VT;
6373 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
6374 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
6375 VT.DefinitionRequired = VTableUses[Idx++];
6376 VTables.push_back(VT);
6377 }
6378
6379 VTableUses.clear();
6380}
6381
Douglas Gregor6e4a3f52011-07-28 19:49:54 +00006382void ASTReader::ReadPendingInstantiations(
6383 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
6384 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
6385 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
6386 SourceLocation Loc
6387 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
Axel Naumann39d26c32012-10-02 09:09:43 +00006388
Douglas Gregore5fa3c22012-10-03 18:34:48 +00006389 Pending.push_back(std::make_pair(D, Loc));
Douglas Gregor6e4a3f52011-07-28 19:49:54 +00006390 }
6391 PendingInstantiations.clear();
6392}
6393
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006394void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redle58aa892010-08-04 18:21:41 +00006395 // It would be complicated to avoid reading the methods anyway. So don't.
6396 ReadMethodPool(Sel);
6397}
6398
Douglas Gregor95eab172011-07-28 20:55:49 +00006399void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00006400 assert(ID && "Non-zero identifier ID required");
Douglas Gregora02b1472009-04-28 21:53:25 +00006401 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00006402 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00006403 if (DeserializationListener)
6404 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregor668c1a42009-04-21 22:25:48 +00006405}
6406
Douglas Gregord89275b2009-07-06 18:54:52 +00006407/// \brief Set the globally-visible declarations associated with the given
6408/// identifier.
6409///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00006410/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump1eb44332009-09-09 15:08:12 +00006411/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregord89275b2009-07-06 18:54:52 +00006412/// them.
6413///
6414/// \param II an IdentifierInfo that refers to one or more globally-visible
6415/// declarations.
6416///
6417/// \param DeclIDs the set of declaration IDs with the name @p II that are
6418/// visible at global scope.
6419///
6420/// \param Nonrecursive should be true to indicate that the caller knows that
6421/// this call is non-recursive, and therefore the globally-visible declarations
6422/// will not be placed onto the pending queue.
Mike Stump1eb44332009-09-09 15:08:12 +00006423void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006424ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Chris Lattner5f9e2722011-07-23 10:55:15 +00006425 const SmallVectorImpl<uint32_t> &DeclIDs,
Douglas Gregord89275b2009-07-06 18:54:52 +00006426 bool Nonrecursive) {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00006427 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregord89275b2009-07-06 18:54:52 +00006428 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
6429 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
6430 PII.II = II;
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00006431 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregord89275b2009-07-06 18:54:52 +00006432 return;
6433 }
Mike Stump1eb44332009-09-09 15:08:12 +00006434
Douglas Gregord89275b2009-07-06 18:54:52 +00006435 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
6436 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
6437 if (SemaObj) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00006438 // Introduce this declaration into the translation-unit scope
6439 // and add it to the declaration chain for this identifier, so
6440 // that (unqualified) name lookup will find it.
6441 SemaObj->pushExternalDeclIntoScope(D, II);
Douglas Gregord89275b2009-07-06 18:54:52 +00006442 } else {
6443 // Queue this declaration so that it will be added to the
6444 // translation unit scope and identifier's declaration chain
6445 // once a Sema object is known.
6446 PreloadedDecls.push_back(D);
6447 }
6448 }
6449}
6450
Douglas Gregor95eab172011-07-28 20:55:49 +00006451IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00006452 if (ID == 0)
6453 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00006454
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00006455 if (IdentifiersLoaded.empty()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00006456 Error("no identifier table in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00006457 return 0;
6458 }
Mike Stump1eb44332009-09-09 15:08:12 +00006459
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00006460 ID -= 1;
6461 if (!IdentifiersLoaded[ID]) {
Douglas Gregor67268d02011-07-20 00:59:32 +00006462 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
6463 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006464 ModuleFile *M = I->second;
Douglas Gregor9827a802011-07-29 00:56:45 +00006465 unsigned Index = ID - M->BaseIdentifierID;
6466 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
Douglas Gregord6595a42009-04-25 21:04:17 +00006467
Sebastian Redl3c7f4132010-08-18 23:57:06 +00006468 // All of the strings in the AST file are preceded by a 16-bit length.
6469 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenek231bc0b2009-10-23 04:45:31 +00006470 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
6471 // unsigned integers. This is important to avoid integer overflow when
6472 // we cast them to 'unsigned'.
Ted Kremenekff1ea462009-10-23 03:57:22 +00006473 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregor02fc7512009-04-28 20:01:51 +00006474 unsigned StrLen = (((unsigned) StrLenPtr[0])
6475 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00006476 IdentifiersLoaded[ID]
Douglas Gregor712f2fc2011-09-09 22:02:16 +00006477 = &PP.getIdentifierTable().get(StringRef(Str, StrLen));
Sebastian Redlf2f0f032010-07-23 23:49:55 +00006478 if (DeserializationListener)
6479 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregorafaf3082009-04-11 00:14:32 +00006480 }
Mike Stump1eb44332009-09-09 15:08:12 +00006481
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00006482 return IdentifiersLoaded[ID];
Douglas Gregor2cf26342009-04-09 22:27:44 +00006483}
6484
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006485IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
Douglas Gregor95eab172011-07-28 20:55:49 +00006486 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
6487}
6488
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006489IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
Douglas Gregor6ec60e02011-08-03 21:49:18 +00006490 if (LocalID < NUM_PREDEF_IDENT_IDS)
6491 return LocalID;
6492
6493 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6494 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
6495 assert(I != M.IdentifierRemap.end()
6496 && "Invalid index into identifier index remap");
6497
6498 return LocalID + I->second;
Douglas Gregor95eab172011-07-28 20:55:49 +00006499}
6500
Douglas Gregor3ab50fe2012-10-11 17:41:54 +00006501MacroInfo *ASTReader::getMacro(MacroID ID, MacroInfo *Hint) {
Douglas Gregora8235d62012-10-09 23:05:51 +00006502 if (ID == 0)
6503 return 0;
6504
6505 if (MacrosLoaded.empty()) {
6506 Error("no macro table in AST file");
6507 return 0;
6508 }
6509
6510 ID -= NUM_PREDEF_MACRO_IDS;
6511 if (!MacrosLoaded[ID]) {
6512 GlobalMacroMapType::iterator I
6513 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
6514 assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
6515 ModuleFile *M = I->second;
6516 unsigned Index = ID - M->BaseMacroID;
Douglas Gregor3ab50fe2012-10-11 17:41:54 +00006517 ReadMacroRecord(*M, M->MacroOffsets[Index], Hint);
Douglas Gregora8235d62012-10-09 23:05:51 +00006518 }
6519
6520 return MacrosLoaded[ID];
6521}
6522
6523MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
6524 if (LocalID < NUM_PREDEF_MACRO_IDS)
6525 return LocalID;
6526
6527 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6528 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
6529 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
6530
6531 return LocalID + I->second;
6532}
6533
Douglas Gregor26ced122011-12-01 00:59:36 +00006534serialization::SubmoduleID
6535ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
6536 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
6537 return LocalID;
6538
6539 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6540 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
6541 assert(I != M.SubmoduleRemap.end()
Douglas Gregora8235d62012-10-09 23:05:51 +00006542 && "Invalid index into submodule index remap");
Douglas Gregor26ced122011-12-01 00:59:36 +00006543
6544 return LocalID + I->second;
6545}
6546
6547Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
6548 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
6549 assert(GlobalID == 0 && "Unhandled global submodule ID");
6550 return 0;
6551 }
6552
6553 if (GlobalID > SubmodulesLoaded.size()) {
6554 Error("submodule ID out of range in AST file");
6555 return 0;
6556 }
6557
6558 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
6559}
6560
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006561Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
Douglas Gregor2d2689a2011-07-28 21:16:51 +00006562 return DecodeSelector(getGlobalSelectorID(M, LocalID));
6563}
6564
6565Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
Steve Naroff90cd1bb2009-04-23 10:39:46 +00006566 if (ID == 0)
6567 return Selector();
Mike Stump1eb44332009-09-09 15:08:12 +00006568
Sebastian Redl725cd962010-08-04 20:40:17 +00006569 if (ID > SelectorsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00006570 Error("selector ID out of range in AST file");
Steve Naroff90cd1bb2009-04-23 10:39:46 +00006571 return Selector();
6572 }
Douglas Gregor83941df2009-04-25 17:48:32 +00006573
Sebastian Redl725cd962010-08-04 20:40:17 +00006574 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor83941df2009-04-25 17:48:32 +00006575 // Load this selector from the selector table.
Douglas Gregor96958cb2011-07-20 01:10:58 +00006576 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
6577 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006578 ModuleFile &M = *I->second;
Douglas Gregor9827a802011-07-29 00:56:45 +00006579 ASTSelectorLookupTrait Trait(*this, M);
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00006580 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
Douglas Gregor96958cb2011-07-20 01:10:58 +00006581 SelectorsLoaded[ID - 1] =
Douglas Gregor9827a802011-07-29 00:56:45 +00006582 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
Douglas Gregor96958cb2011-07-20 01:10:58 +00006583 if (DeserializationListener)
6584 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
Douglas Gregor83941df2009-04-25 17:48:32 +00006585 }
6586
Sebastian Redl725cd962010-08-04 20:40:17 +00006587 return SelectorsLoaded[ID - 1];
Steve Naroff90cd1bb2009-04-23 10:39:46 +00006588}
6589
Douglas Gregor8451ec72011-07-28 14:41:43 +00006590Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
Douglas Gregor719770d2010-04-06 17:30:22 +00006591 return DecodeSelector(ID);
6592}
6593
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006594uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redl725cd962010-08-04 20:40:17 +00006595 // ID 0 (the null selector) is considered an external selector.
6596 return getTotalNumSelectors() + 1;
Douglas Gregor719770d2010-04-06 17:30:22 +00006597}
6598
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00006599serialization::SelectorID
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006600ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00006601 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
6602 return LocalID;
6603
6604 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6605 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
6606 assert(I != M.SelectorRemap.end()
Douglas Gregora8235d62012-10-09 23:05:51 +00006607 && "Invalid index into selector index remap");
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00006608
6609 return LocalID + I->second;
Douglas Gregor8451ec72011-07-28 14:41:43 +00006610}
6611
Mike Stump1eb44332009-09-09 15:08:12 +00006612DeclarationName
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006613ASTReader::ReadDeclarationName(ModuleFile &F,
Douglas Gregor393f2492011-07-22 00:38:23 +00006614 const RecordData &Record, unsigned &Idx) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00006615 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
6616 switch (Kind) {
6617 case DeclarationName::Identifier:
Douglas Gregor95eab172011-07-28 20:55:49 +00006618 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00006619
6620 case DeclarationName::ObjCZeroArgSelector:
6621 case DeclarationName::ObjCOneArgSelector:
6622 case DeclarationName::ObjCMultiArgSelector:
Douglas Gregor2d2689a2011-07-28 21:16:51 +00006623 return DeclarationName(ReadSelector(F, Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00006624
6625 case DeclarationName::CXXConstructorName:
Douglas Gregor35942772011-09-09 21:34:22 +00006626 return Context.DeclarationNames.getCXXConstructorName(
6627 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00006628
6629 case DeclarationName::CXXDestructorName:
Douglas Gregor35942772011-09-09 21:34:22 +00006630 return Context.DeclarationNames.getCXXDestructorName(
6631 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00006632
6633 case DeclarationName::CXXConversionFunctionName:
Douglas Gregor35942772011-09-09 21:34:22 +00006634 return Context.DeclarationNames.getCXXConversionFunctionName(
6635 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00006636
6637 case DeclarationName::CXXOperatorName:
Douglas Gregor35942772011-09-09 21:34:22 +00006638 return Context.DeclarationNames.getCXXOperatorName(
Douglas Gregor2cf26342009-04-09 22:27:44 +00006639 (OverloadedOperatorKind)Record[Idx++]);
6640
Sean Hunt3e518bd2009-11-29 07:34:05 +00006641 case DeclarationName::CXXLiteralOperatorName:
Douglas Gregor35942772011-09-09 21:34:22 +00006642 return Context.DeclarationNames.getCXXLiteralOperatorName(
Douglas Gregor95eab172011-07-28 20:55:49 +00006643 GetIdentifierInfo(F, Record, Idx));
Sean Hunt3e518bd2009-11-29 07:34:05 +00006644
Douglas Gregor2cf26342009-04-09 22:27:44 +00006645 case DeclarationName::CXXUsingDirective:
6646 return DeclarationName::getUsingDirectiveName();
6647 }
6648
David Blaikie7530c032012-01-17 06:56:22 +00006649 llvm_unreachable("Invalid NameKind!");
Douglas Gregor2cf26342009-04-09 22:27:44 +00006650}
Douglas Gregor0a0428e2009-04-10 20:39:37 +00006651
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006652void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006653 DeclarationNameLoc &DNLoc,
6654 DeclarationName Name,
6655 const RecordData &Record, unsigned &Idx) {
6656 switch (Name.getNameKind()) {
6657 case DeclarationName::CXXConstructorName:
6658 case DeclarationName::CXXDestructorName:
6659 case DeclarationName::CXXConversionFunctionName:
6660 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
6661 break;
6662
6663 case DeclarationName::CXXOperatorName:
6664 DNLoc.CXXOperatorName.BeginOpNameLoc
6665 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6666 DNLoc.CXXOperatorName.EndOpNameLoc
6667 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6668 break;
6669
6670 case DeclarationName::CXXLiteralOperatorName:
6671 DNLoc.CXXLiteralOperatorName.OpNameLoc
6672 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6673 break;
6674
6675 case DeclarationName::Identifier:
6676 case DeclarationName::ObjCZeroArgSelector:
6677 case DeclarationName::ObjCOneArgSelector:
6678 case DeclarationName::ObjCMultiArgSelector:
6679 case DeclarationName::CXXUsingDirective:
6680 break;
6681 }
6682}
6683
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006684void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006685 DeclarationNameInfo &NameInfo,
6686 const RecordData &Record, unsigned &Idx) {
Douglas Gregor393f2492011-07-22 00:38:23 +00006687 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006688 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
6689 DeclarationNameLoc DNLoc;
6690 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
6691 NameInfo.setInfo(DNLoc);
6692}
6693
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006694void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006695 const RecordData &Record, unsigned &Idx) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00006696 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006697 unsigned NumTPLists = Record[Idx++];
6698 Info.NumTemplParamLists = NumTPLists;
6699 if (NumTPLists) {
Douglas Gregor35942772011-09-09 21:34:22 +00006700 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006701 for (unsigned i=0; i != NumTPLists; ++i)
6702 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
6703 }
6704}
6705
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006706TemplateName
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006707ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006708 unsigned &Idx) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00006709 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006710 switch (Kind) {
6711 case TemplateName::Template:
Douglas Gregor409448c2011-07-21 22:35:25 +00006712 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006713
6714 case TemplateName::OverloadedTemplate: {
6715 unsigned size = Record[Idx++];
6716 UnresolvedSet<8> Decls;
6717 while (size--)
Douglas Gregor409448c2011-07-21 22:35:25 +00006718 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006719
Douglas Gregor35942772011-09-09 21:34:22 +00006720 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006721 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006722
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006723 case TemplateName::QualifiedTemplate: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006724 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006725 bool hasTemplKeyword = Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00006726 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006727 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006728 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006729
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006730 case TemplateName::DependentTemplate: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006731 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006732 if (Record[Idx++]) // isIdentifier
Douglas Gregor35942772011-09-09 21:34:22 +00006733 return Context.getDependentTemplateName(NNS,
Douglas Gregor95eab172011-07-28 20:55:49 +00006734 GetIdentifierInfo(F, Record,
6735 Idx));
Douglas Gregor35942772011-09-09 21:34:22 +00006736 return Context.getDependentTemplateName(NNS,
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00006737 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006738 }
John McCall14606042011-06-30 08:33:18 +00006739
6740 case TemplateName::SubstTemplateTemplateParm: {
6741 TemplateTemplateParmDecl *param
Douglas Gregor409448c2011-07-21 22:35:25 +00006742 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
John McCall14606042011-06-30 08:33:18 +00006743 if (!param) return TemplateName();
6744 TemplateName replacement = ReadTemplateName(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006745 return Context.getSubstTemplateTemplateParm(param, replacement);
John McCall14606042011-06-30 08:33:18 +00006746 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006747
6748 case TemplateName::SubstTemplateTemplateParmPack: {
6749 TemplateTemplateParmDecl *Param
Douglas Gregor409448c2011-07-21 22:35:25 +00006750 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006751 if (!Param)
6752 return TemplateName();
6753
6754 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
6755 if (ArgPack.getKind() != TemplateArgument::Pack)
6756 return TemplateName();
6757
Douglas Gregor35942772011-09-09 21:34:22 +00006758 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006759 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006760 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006761
David Blaikieb219cfc2011-09-23 05:06:16 +00006762 llvm_unreachable("Unhandled template name kind!");
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006763}
6764
6765TemplateArgument
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006766ASTReader::ReadTemplateArgument(ModuleFile &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00006767 const RecordData &Record, unsigned &Idx) {
Douglas Gregora7fc9012011-01-05 18:58:31 +00006768 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
6769 switch (Kind) {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006770 case TemplateArgument::Null:
6771 return TemplateArgument();
6772 case TemplateArgument::Type:
Douglas Gregor393f2492011-07-22 00:38:23 +00006773 return TemplateArgument(readType(F, Record, Idx));
Eli Friedmand7a6b162012-09-26 02:36:12 +00006774 case TemplateArgument::Declaration: {
6775 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
6776 bool ForReferenceParam = Record[Idx++];
6777 return TemplateArgument(D, ForReferenceParam);
6778 }
6779 case TemplateArgument::NullPtr:
6780 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00006781 case TemplateArgument::Integral: {
6782 llvm::APSInt Value = ReadAPSInt(Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00006783 QualType T = readType(F, Record, Idx);
Benjamin Kramer85524372012-06-07 15:09:51 +00006784 return TemplateArgument(Context, Value, T);
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00006785 }
Douglas Gregora7fc9012011-01-05 18:58:31 +00006786 case TemplateArgument::Template:
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006787 return TemplateArgument(ReadTemplateName(F, Record, Idx));
Douglas Gregora7fc9012011-01-05 18:58:31 +00006788 case TemplateArgument::TemplateExpansion: {
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006789 TemplateName Name = ReadTemplateName(F, Record, Idx);
Douglas Gregor2be29f42011-01-14 23:41:42 +00006790 llvm::Optional<unsigned> NumTemplateExpansions;
6791 if (unsigned NumExpansions = Record[Idx++])
6792 NumTemplateExpansions = NumExpansions - 1;
6793 return TemplateArgument(Name, NumTemplateExpansions);
Douglas Gregorba68eca2011-01-05 17:40:24 +00006794 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006795 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00006796 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006797 case TemplateArgument::Pack: {
6798 unsigned NumArgs = Record[Idx++];
Douglas Gregor35942772011-09-09 21:34:22 +00006799 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
Douglas Gregor910f8002010-11-07 23:05:16 +00006800 for (unsigned I = 0; I != NumArgs; ++I)
6801 Args[I] = ReadTemplateArgument(F, Record, Idx);
6802 return TemplateArgument(Args, NumArgs);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006803 }
6804 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006805
David Blaikieb219cfc2011-09-23 05:06:16 +00006806 llvm_unreachable("Unhandled template argument kind!");
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006807}
6808
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006809TemplateParameterList *
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006810ASTReader::ReadTemplateParameterList(ModuleFile &F,
Sebastian Redlc3632732010-10-05 15:59:54 +00006811 const RecordData &Record, unsigned &Idx) {
6812 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
6813 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
6814 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006815
6816 unsigned NumParams = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00006817 SmallVector<NamedDecl *, 16> Params;
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006818 Params.reserve(NumParams);
6819 while (NumParams--)
Douglas Gregor409448c2011-07-21 22:35:25 +00006820 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
Michael J. Spencer20249a12010-10-21 03:16:25 +00006821
6822 TemplateParameterList* TemplateParams =
Douglas Gregor35942772011-09-09 21:34:22 +00006823 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006824 Params.data(), Params.size(), RAngleLoc);
6825 return TemplateParams;
6826}
6827
6828void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006829ASTReader::
Chris Lattner5f9e2722011-07-23 10:55:15 +00006830ReadTemplateArgumentList(SmallVector<TemplateArgument, 8> &TemplArgs,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006831 ModuleFile &F, const RecordData &Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00006832 unsigned &Idx) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006833 unsigned NumTemplateArgs = Record[Idx++];
6834 TemplArgs.reserve(NumTemplateArgs);
6835 while (NumTemplateArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00006836 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006837}
6838
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00006839/// \brief Read a UnresolvedSet structure.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006840void ASTReader::ReadUnresolvedSet(ModuleFile &F, UnresolvedSetImpl &Set,
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00006841 const RecordData &Record, unsigned &Idx) {
6842 unsigned NumDecls = Record[Idx++];
6843 while (NumDecls--) {
Douglas Gregor409448c2011-07-21 22:35:25 +00006844 NamedDecl *D = ReadDeclAs<NamedDecl>(F, Record, Idx);
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00006845 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
6846 Set.addDecl(D, AS);
6847 }
6848}
6849
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00006850CXXBaseSpecifier
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006851ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
Nick Lewycky56062202010-07-26 16:56:01 +00006852 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00006853 bool isVirtual = static_cast<bool>(Record[Idx++]);
6854 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
6855 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redlf677ea32011-02-05 19:23:19 +00006856 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00006857 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
6858 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00006859 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redlf677ea32011-02-05 19:23:19 +00006860 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
Douglas Gregorf90b27a2011-01-03 22:36:02 +00006861 EllipsisLoc);
Sebastian Redlf677ea32011-02-05 19:23:19 +00006862 Result.setInheritConstructors(inheritConstructors);
6863 return Result;
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00006864}
6865
Sean Huntcbb67482011-01-08 20:30:50 +00006866std::pair<CXXCtorInitializer **, unsigned>
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006867ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
Sean Huntcbb67482011-01-08 20:30:50 +00006868 unsigned &Idx) {
6869 CXXCtorInitializer **CtorInitializers = 0;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006870 unsigned NumInitializers = Record[Idx++];
6871 if (NumInitializers) {
Sean Huntcbb67482011-01-08 20:30:50 +00006872 CtorInitializers
Douglas Gregor35942772011-09-09 21:34:22 +00006873 = new (Context) CXXCtorInitializer*[NumInitializers];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006874 for (unsigned i=0; i != NumInitializers; ++i) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006875 TypeSourceInfo *TInfo = 0;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006876 bool IsBaseVirtual = false;
6877 FieldDecl *Member = 0;
Francois Pichet00eb3f92010-12-04 09:14:42 +00006878 IndirectFieldDecl *IndirectMember = 0;
Michael J. Spencer20249a12010-10-21 03:16:25 +00006879
Sean Hunt156b6402011-05-04 01:19:08 +00006880 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
6881 switch (Type) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006882 case CTOR_INITIALIZER_BASE:
6883 TInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006884 IsBaseVirtual = Record[Idx++];
Sean Hunt156b6402011-05-04 01:19:08 +00006885 break;
Douglas Gregor76852c22011-11-01 01:16:03 +00006886
6887 case CTOR_INITIALIZER_DELEGATING:
6888 TInfo = GetTypeSourceInfo(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00006889 break;
6890
6891 case CTOR_INITIALIZER_MEMBER:
Douglas Gregor409448c2011-07-21 22:35:25 +00006892 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00006893 break;
6894
6895 case CTOR_INITIALIZER_INDIRECT_MEMBER:
Douglas Gregor409448c2011-07-21 22:35:25 +00006896 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00006897 break;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006898 }
Sean Hunt156b6402011-05-04 01:19:08 +00006899
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00006900 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redlc3632732010-10-05 15:59:54 +00006901 Expr *Init = ReadExpr(F);
Sebastian Redlc3632732010-10-05 15:59:54 +00006902 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
6903 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006904 bool IsWritten = Record[Idx++];
6905 unsigned SourceOrderOrNumArrayIndices;
Chris Lattner5f9e2722011-07-23 10:55:15 +00006906 SmallVector<VarDecl *, 8> Indices;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006907 if (IsWritten) {
6908 SourceOrderOrNumArrayIndices = Record[Idx++];
6909 } else {
6910 SourceOrderOrNumArrayIndices = Record[Idx++];
6911 Indices.reserve(SourceOrderOrNumArrayIndices);
6912 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
Douglas Gregor409448c2011-07-21 22:35:25 +00006913 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006914 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006915
Sean Huntcbb67482011-01-08 20:30:50 +00006916 CXXCtorInitializer *BOMInit;
Sean Hunt156b6402011-05-04 01:19:08 +00006917 if (Type == CTOR_INITIALIZER_BASE) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006918 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, IsBaseVirtual,
Sean Huntcbb67482011-01-08 20:30:50 +00006919 LParenLoc, Init, RParenLoc,
6920 MemberOrEllipsisLoc);
Sean Hunt156b6402011-05-04 01:19:08 +00006921 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006922 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, LParenLoc,
6923 Init, RParenLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006924 } else if (IsWritten) {
Francois Pichet00eb3f92010-12-04 09:14:42 +00006925 if (Member)
Douglas Gregor35942772011-09-09 21:34:22 +00006926 BOMInit = new (Context) CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc,
Sean Huntcbb67482011-01-08 20:30:50 +00006927 LParenLoc, Init, RParenLoc);
Francois Pichet00eb3f92010-12-04 09:14:42 +00006928 else
Douglas Gregor35942772011-09-09 21:34:22 +00006929 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
Sean Huntcbb67482011-01-08 20:30:50 +00006930 MemberOrEllipsisLoc, LParenLoc,
6931 Init, RParenLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006932 } else {
Douglas Gregor35942772011-09-09 21:34:22 +00006933 BOMInit = CXXCtorInitializer::Create(Context, Member, MemberOrEllipsisLoc,
Sean Huntcbb67482011-01-08 20:30:50 +00006934 LParenLoc, Init, RParenLoc,
6935 Indices.data(), Indices.size());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006936 }
6937
Argyrios Kyrtzidisf84cde12010-09-06 19:04:27 +00006938 if (IsWritten)
6939 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Sean Huntcbb67482011-01-08 20:30:50 +00006940 CtorInitializers[i] = BOMInit;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006941 }
6942 }
6943
Sean Huntcbb67482011-01-08 20:30:50 +00006944 return std::make_pair(CtorInitializers, NumInitializers);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006945}
6946
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006947NestedNameSpecifier *
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006948ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00006949 const RecordData &Record, unsigned &Idx) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006950 unsigned N = Record[Idx++];
6951 NestedNameSpecifier *NNS = 0, *Prev = 0;
6952 for (unsigned I = 0; I != N; ++I) {
6953 NestedNameSpecifier::SpecifierKind Kind
6954 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
6955 switch (Kind) {
6956 case NestedNameSpecifier::Identifier: {
Douglas Gregor95eab172011-07-28 20:55:49 +00006957 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006958 NNS = NestedNameSpecifier::Create(Context, Prev, II);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006959 break;
6960 }
6961
6962 case NestedNameSpecifier::Namespace: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006963 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006964 NNS = NestedNameSpecifier::Create(Context, Prev, NS);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006965 break;
6966 }
6967
Douglas Gregor14aba762011-02-24 02:36:08 +00006968 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006969 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006970 NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
Douglas Gregor14aba762011-02-24 02:36:08 +00006971 break;
6972 }
6973
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006974 case NestedNameSpecifier::TypeSpec:
6975 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregor393f2492011-07-22 00:38:23 +00006976 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
Douglas Gregor1ab55e92010-12-10 17:03:06 +00006977 if (!T)
6978 return 0;
6979
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006980 bool Template = Record[Idx++];
Douglas Gregor35942772011-09-09 21:34:22 +00006981 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006982 break;
6983 }
6984
6985 case NestedNameSpecifier::Global: {
Douglas Gregor35942772011-09-09 21:34:22 +00006986 NNS = NestedNameSpecifier::GlobalSpecifier(Context);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006987 // No associated value, and there can't be a prefix.
6988 break;
6989 }
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006990 }
Argyrios Kyrtzidisd2bb2c02010-07-07 15:46:30 +00006991 Prev = NNS;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006992 }
6993 return NNS;
6994}
6995
Douglas Gregordc355712011-02-25 00:36:19 +00006996NestedNameSpecifierLoc
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006997ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
Douglas Gregordc355712011-02-25 00:36:19 +00006998 unsigned &Idx) {
6999 unsigned N = Record[Idx++];
Douglas Gregor5f791bb2011-02-28 23:58:31 +00007000 NestedNameSpecifierLocBuilder Builder;
Douglas Gregordc355712011-02-25 00:36:19 +00007001 for (unsigned I = 0; I != N; ++I) {
7002 NestedNameSpecifier::SpecifierKind Kind
7003 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
7004 switch (Kind) {
7005 case NestedNameSpecifier::Identifier: {
Douglas Gregor95eab172011-07-28 20:55:49 +00007006 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00007007 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00007008 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00007009 break;
7010 }
7011
7012 case NestedNameSpecifier::Namespace: {
Douglas Gregor409448c2011-07-21 22:35:25 +00007013 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00007014 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00007015 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00007016 break;
7017 }
7018
7019 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregor409448c2011-07-21 22:35:25 +00007020 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00007021 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00007022 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00007023 break;
7024 }
7025
7026 case NestedNameSpecifier::TypeSpec:
7027 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregordc355712011-02-25 00:36:19 +00007028 bool Template = Record[Idx++];
7029 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
7030 if (!T)
7031 return NestedNameSpecifierLoc();
Douglas Gregordc355712011-02-25 00:36:19 +00007032 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor5f791bb2011-02-28 23:58:31 +00007033
7034 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
Douglas Gregor35942772011-09-09 21:34:22 +00007035 Builder.Extend(Context,
Douglas Gregor5f791bb2011-02-28 23:58:31 +00007036 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
7037 T->getTypeLoc(), ColonColonLoc);
Douglas Gregordc355712011-02-25 00:36:19 +00007038 break;
7039 }
7040
7041 case NestedNameSpecifier::Global: {
Douglas Gregordc355712011-02-25 00:36:19 +00007042 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00007043 Builder.MakeGlobal(Context, ColonColonLoc);
Douglas Gregordc355712011-02-25 00:36:19 +00007044 break;
7045 }
7046 }
Douglas Gregordc355712011-02-25 00:36:19 +00007047 }
7048
Douglas Gregor35942772011-09-09 21:34:22 +00007049 return Builder.getWithLocInContext(Context);
Douglas Gregordc355712011-02-25 00:36:19 +00007050}
7051
Chris Lattner6ad9ac02010-05-07 21:43:38 +00007052SourceRange
Douglas Gregor1a4761e2011-11-30 23:21:26 +00007053ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00007054 unsigned &Idx) {
7055 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
7056 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar8ee59392010-06-02 15:47:10 +00007057 return SourceRange(beg, end);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00007058}
7059
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00007060/// \brief Read an integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00007061llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00007062 unsigned BitWidth = Record[Idx++];
7063 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
7064 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
7065 Idx += NumWords;
7066 return Result;
7067}
7068
7069/// \brief Read a signed integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00007070llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00007071 bool isUnsigned = Record[Idx++];
7072 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
7073}
7074
Douglas Gregor17fc2232009-04-14 21:55:33 +00007075/// \brief Read a floating-point value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00007076llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00007077 return llvm::APFloat(ReadAPInt(Record, Idx));
7078}
7079
Douglas Gregor68a2eb02009-04-15 21:30:51 +00007080// \brief Read a string
Sebastian Redlc43b54c2010-08-18 23:56:43 +00007081std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00007082 unsigned Len = Record[Idx++];
Jay Foadbeaaccd2009-05-21 09:52:38 +00007083 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00007084 Idx += Len;
7085 return Result;
7086}
7087
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00007088VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
7089 unsigned &Idx) {
7090 unsigned Major = Record[Idx++];
7091 unsigned Minor = Record[Idx++];
7092 unsigned Subminor = Record[Idx++];
7093 if (Minor == 0)
7094 return VersionTuple(Major);
7095 if (Subminor == 0)
7096 return VersionTuple(Major, Minor - 1);
7097 return VersionTuple(Major, Minor - 1, Subminor - 1);
7098}
7099
Douglas Gregor1a4761e2011-11-30 23:21:26 +00007100CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00007101 const RecordData &Record,
Chris Lattnerd2598362010-05-10 00:25:06 +00007102 unsigned &Idx) {
Douglas Gregor409448c2011-07-21 22:35:25 +00007103 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00007104 return CXXTemporary::Create(Context, Decl);
Chris Lattnerd2598362010-05-10 00:25:06 +00007105}
7106
Sebastian Redlc43b54c2010-08-18 23:56:43 +00007107DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00007108 return Diag(SourceLocation(), DiagID);
7109}
7110
Sebastian Redlc43b54c2010-08-18 23:56:43 +00007111DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00007112 return Diags.Report(Loc, DiagID);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00007113}
Douglas Gregor025452f2009-04-17 00:04:06 +00007114
Douglas Gregor668c1a42009-04-21 22:25:48 +00007115/// \brief Retrieve the identifier table associated with the
7116/// preprocessor.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00007117IdentifierTable &ASTReader::getIdentifierTable() {
Douglas Gregor712f2fc2011-09-09 22:02:16 +00007118 return PP.getIdentifierTable();
Douglas Gregor668c1a42009-04-21 22:25:48 +00007119}
7120
Douglas Gregor025452f2009-04-17 00:04:06 +00007121/// \brief Record that the given ID maps to the given switch-case
7122/// statement.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00007123void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00007124 assert((*CurrSwitchCaseStmts)[ID] == 0 &&
7125 "Already have a SwitchCase with this ID");
7126 (*CurrSwitchCaseStmts)[ID] = SC;
Douglas Gregor025452f2009-04-17 00:04:06 +00007127}
7128
7129/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00007130SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00007131 assert((*CurrSwitchCaseStmts)[ID] != 0 && "No SwitchCase with this ID");
7132 return (*CurrSwitchCaseStmts)[ID];
Douglas Gregor025452f2009-04-17 00:04:06 +00007133}
Douglas Gregor1de05fe2009-04-17 18:18:49 +00007134
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00007135void ASTReader::ClearSwitchCaseIDs() {
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00007136 CurrSwitchCaseStmts->clear();
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00007137}
7138
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00007139void ASTReader::ReadComments() {
Dmitri Gribenko811c8202012-07-06 18:19:34 +00007140 std::vector<RawComment *> Comments;
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00007141 for (SmallVectorImpl<std::pair<llvm::BitstreamCursor,
7142 serialization::ModuleFile *> >::iterator
7143 I = CommentsCursors.begin(),
7144 E = CommentsCursors.end();
7145 I != E; ++I) {
7146 llvm::BitstreamCursor &Cursor = I->first;
7147 serialization::ModuleFile &F = *I->second;
7148 SavedStreamPosition SavedPosition(Cursor);
7149
7150 RecordData Record;
7151 while (true) {
7152 unsigned Code = Cursor.ReadCode();
7153 if (Code == llvm::bitc::END_BLOCK)
7154 break;
7155
7156 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
7157 // No known subblocks, always skip them.
7158 Cursor.ReadSubBlockID();
7159 if (Cursor.SkipBlock()) {
7160 Error("malformed block record in AST file");
7161 return;
7162 }
7163 continue;
7164 }
7165
7166 if (Code == llvm::bitc::DEFINE_ABBREV) {
7167 Cursor.ReadAbbrevRecord();
7168 continue;
7169 }
7170
7171 // Read a record.
7172 Record.clear();
7173 switch ((CommentRecordTypes) Cursor.ReadRecord(Code, Record)) {
Chandler Carruth13691bb2012-06-20 06:47:54 +00007174 case COMMENTS_RAW_COMMENT: {
7175 unsigned Idx = 0;
7176 SourceRange SR = ReadSourceRange(F, Record, Idx);
7177 RawComment::CommentKind Kind =
7178 (RawComment::CommentKind) Record[Idx++];
7179 bool IsTrailingComment = Record[Idx++];
7180 bool IsAlmostTrailingComment = Record[Idx++];
Dmitri Gribenko811c8202012-07-06 18:19:34 +00007181 Comments.push_back(new (Context) RawComment(SR, Kind,
7182 IsTrailingComment,
7183 IsAlmostTrailingComment));
Chandler Carruth13691bb2012-06-20 06:47:54 +00007184 break;
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00007185 }
7186 }
7187 }
7188 }
7189 Context.Comments.addCommentsToFront(Comments);
7190}
7191
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00007192void ASTReader::finishPendingActions() {
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00007193 while (!PendingIdentifierInfos.empty() || !PendingDeclChains.empty() ||
7194 !PendingMacroIDs.empty()) {
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00007195 // If any identifiers with corresponding top-level declarations have
7196 // been loaded, load those declarations now.
7197 while (!PendingIdentifierInfos.empty()) {
7198 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
7199 PendingIdentifierInfos.front().DeclIDs, true);
7200 PendingIdentifierInfos.pop_front();
7201 }
7202
Douglas Gregora1be2782011-12-17 23:38:30 +00007203 // Load pending declaration chains.
7204 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) {
7205 loadPendingDeclChain(PendingDeclChains[I]);
Douglas Gregor56ca8a92012-01-17 19:21:53 +00007206 PendingDeclChainsKnown.erase(PendingDeclChains[I]);
Douglas Gregora1be2782011-12-17 23:38:30 +00007207 }
7208 PendingDeclChains.clear();
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00007209
7210 // Load any pending macro definitions.
Douglas Gregore9652bf2012-10-11 17:31:34 +00007211 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
7212 // FIXME: std::move here
7213 SmallVector<MacroID, 2> GlobalIDs = PendingMacroIDs.begin()[I].second;
Douglas Gregor3ab50fe2012-10-11 17:41:54 +00007214 MacroInfo *Hint = 0;
Douglas Gregore9652bf2012-10-11 17:31:34 +00007215 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
7216 ++IDIdx) {
Douglas Gregor3ab50fe2012-10-11 17:41:54 +00007217 Hint = getMacro(GlobalIDs[IDIdx], Hint);
Douglas Gregore9652bf2012-10-11 17:31:34 +00007218 }
7219 }
7220 PendingMacroIDs.clear();
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00007221 }
Douglas Gregorfc529f72011-12-19 19:00:47 +00007222
Douglas Gregor7c99bb5c2012-01-14 15:13:49 +00007223 // If we deserialized any C++ or Objective-C class definitions, any
7224 // Objective-C protocol definitions, or any redeclarable templates, make sure
7225 // that all redeclarations point to the definitions. Note that this can only
7226 // happen now, after the redeclaration chains have been fully wired.
Douglas Gregorfc529f72011-12-19 19:00:47 +00007227 for (llvm::SmallPtrSet<Decl *, 4>::iterator D = PendingDefinitions.begin(),
7228 DEnd = PendingDefinitions.end();
7229 D != DEnd; ++D) {
Douglas Gregor56ca8a92012-01-17 19:21:53 +00007230 if (TagDecl *TD = dyn_cast<TagDecl>(*D)) {
7231 if (const TagType *TagT = dyn_cast<TagType>(TD->TypeForDecl)) {
7232 // Make sure that the TagType points at the definition.
7233 const_cast<TagType*>(TagT)->decl = TD;
7234 }
Douglas Gregorfc529f72011-12-19 19:00:47 +00007235
Douglas Gregor56ca8a92012-01-17 19:21:53 +00007236 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(*D)) {
7237 for (CXXRecordDecl::redecl_iterator R = RD->redecls_begin(),
7238 REnd = RD->redecls_end();
7239 R != REnd; ++R)
7240 cast<CXXRecordDecl>(*R)->DefinitionData = RD->DefinitionData;
7241
7242 }
7243
Douglas Gregorfc529f72011-12-19 19:00:47 +00007244 continue;
7245 }
7246
Douglas Gregor1d784b22012-01-01 19:51:50 +00007247 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(*D)) {
Douglas Gregor56ca8a92012-01-17 19:21:53 +00007248 // Make sure that the ObjCInterfaceType points at the definition.
7249 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
7250 ->Decl = ID;
7251
Douglas Gregor1d784b22012-01-01 19:51:50 +00007252 for (ObjCInterfaceDecl::redecl_iterator R = ID->redecls_begin(),
7253 REnd = ID->redecls_end();
7254 R != REnd; ++R)
7255 R->Data = ID->Data;
7256
7257 continue;
7258 }
7259
Douglas Gregor7c99bb5c2012-01-14 15:13:49 +00007260 if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(*D)) {
7261 for (ObjCProtocolDecl::redecl_iterator R = PD->redecls_begin(),
7262 REnd = PD->redecls_end();
7263 R != REnd; ++R)
7264 R->Data = PD->Data;
7265
7266 continue;
7267 }
7268
7269 RedeclarableTemplateDecl *RTD
7270 = cast<RedeclarableTemplateDecl>(*D)->getCanonicalDecl();
7271 for (RedeclarableTemplateDecl::redecl_iterator R = RTD->redecls_begin(),
7272 REnd = RTD->redecls_end();
Douglas Gregorfc529f72011-12-19 19:00:47 +00007273 R != REnd; ++R)
Douglas Gregor5456b0fe2012-10-09 17:21:28 +00007274 R->Common = RTD->Common;
Douglas Gregorfc529f72011-12-19 19:00:47 +00007275 }
7276 PendingDefinitions.clear();
Douglas Gregor5456b0fe2012-10-09 17:21:28 +00007277
7278 // Load the bodies of any functions or methods we've encountered. We do
7279 // this now (delayed) so that we can be sure that the declaration chains
7280 // have been fully wired up.
Douglas Gregorce12d2f2012-10-09 17:50:23 +00007281 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
7282 PBEnd = PendingBodies.end();
Douglas Gregor5456b0fe2012-10-09 17:21:28 +00007283 PB != PBEnd; ++PB) {
7284 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
7285 // FIXME: Check for =delete/=default?
7286 // FIXME: Complain about ODR violations here?
7287 if (!getContext().getLangOpts().Modules || !FD->hasBody())
7288 FD->setLazyBody(PB->second);
7289 continue;
7290 }
7291
7292 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
7293 if (!getContext().getLangOpts().Modules || !MD->hasBody())
7294 MD->setLazyBody(PB->second);
7295 }
7296 PendingBodies.clear();
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00007297}
7298
Sebastian Redlc43b54c2010-08-18 23:56:43 +00007299void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00007300 assert(NumCurrentElementsDeserializing &&
7301 "FinishedDeserializing not paired with StartedDeserializing");
7302 if (NumCurrentElementsDeserializing == 1) {
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00007303 // We decrease NumCurrentElementsDeserializing only after pending actions
7304 // are finished, to avoid recursively re-calling finishPendingActions().
7305 finishPendingActions();
7306 }
7307 --NumCurrentElementsDeserializing;
Argyrios Kyrtzidis71168332011-12-17 04:13:28 +00007308
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00007309 if (NumCurrentElementsDeserializing == 0 &&
7310 Consumer && !PassingDeclsToConsumer) {
7311 // Guard variable to avoid recursively redoing the process of passing
7312 // decls to consumer.
7313 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
7314 true);
Argyrios Kyrtzidis71168332011-12-17 04:13:28 +00007315
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00007316 while (!InterestingDecls.empty()) {
Argyrios Kyrtzidis8d39c3d2011-11-30 23:18:26 +00007317 // We are not in recursive loading, so it's safe to pass the "interesting"
7318 // decls to the consumer.
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00007319 Decl *D = InterestingDecls.front();
7320 InterestingDecls.pop_front();
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00007321 PassInterestingDeclToConsumer(D);
7322 }
Douglas Gregord89275b2009-07-06 18:54:52 +00007323 }
Douglas Gregord89275b2009-07-06 18:54:52 +00007324}
Douglas Gregor501c1032010-08-19 00:28:17 +00007325
Douglas Gregorf8a1e512011-09-02 00:26:20 +00007326ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context,
Douglas Gregor832d6202011-07-22 16:35:34 +00007327 StringRef isysroot, bool DisableValidation,
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00007328 bool DisableStatCache, bool AllowASTWithCompilerErrors)
Sebastian Redle1dde812010-08-24 00:50:04 +00007329 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
7330 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
Douglas Gregor712f2fc2011-09-09 22:02:16 +00007331 Diags(PP.getDiagnostics()), SemaObj(0), PP(PP), Context(Context),
Argyrios Kyrtzidisd64c26f2012-10-03 01:58:42 +00007332 Consumer(0), ModuleMgr(PP.getFileManager()),
Douglas Gregorcaed0602012-10-18 21:31:35 +00007333 isysroot(isysroot), DisableValidation(DisableValidation),
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00007334 DisableStatCache(DisableStatCache),
7335 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00007336 CurrentGeneration(0), CurrSwitchCaseStmts(&SwitchCaseStmts),
7337 NumStatHits(0), NumStatMisses(0),
Douglas Gregorf62d43d2011-07-19 16:10:42 +00007338 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00007339 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
7340 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
7341 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
7342 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
Jonathan D. Turner1da90142011-07-21 21:15:19 +00007343 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
7344 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00007345 PassingDeclsToConsumer(false),
Jonathan D. Turner1da90142011-07-21 21:15:19 +00007346 NumCXXBaseSpecifiersLoaded(0)
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00007347{
Douglas Gregorf62d43d2011-07-19 16:10:42 +00007348 SourceMgr.setExternalSLocEntrySource(this);
Sebastian Redle1dde812010-08-24 00:50:04 +00007349}
7350
Sebastian Redle1dde812010-08-24 00:50:04 +00007351ASTReader::~ASTReader() {
Sebastian Redle1dde812010-08-24 00:50:04 +00007352 for (DeclContextVisibleUpdatesPending::iterator
7353 I = PendingVisibleUpdates.begin(),
7354 E = PendingVisibleUpdates.end();
7355 I != E; ++I) {
7356 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
7357 F = I->second.end();
7358 J != F; ++J)
Benjamin Kramerb1758c62012-04-15 12:36:49 +00007359 delete J->first;
Sebastian Redle1dde812010-08-24 00:50:04 +00007360 }
7361}