blob: 4ea95d0712048acfe664b85df2b78114d9bba028 [file] [log] [blame]
Nick Lewycky995e26b2013-01-31 03:23:57 +00001//===--- ASTReader.cpp - AST File Reader ----------------------------------===//
Guy Benyei7f92f2d2012-12-18 14:30:41 +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//
10// This file defines the ASTReader class, which reads AST files.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Serialization/ASTReader.h"
15#include "ASTCommon.h"
16#include "ASTReaderInternals.h"
17#include "clang/AST/ASTConsumer.h"
18#include "clang/AST/ASTContext.h"
19#include "clang/AST/DeclTemplate.h"
20#include "clang/AST/Expr.h"
21#include "clang/AST/ExprCXX.h"
22#include "clang/AST/NestedNameSpecifier.h"
23#include "clang/AST/Type.h"
24#include "clang/AST/TypeLocVisitor.h"
Stephen Hines6bcf27b2014-05-29 04:14:42 -070025#include "clang/Basic/DiagnosticOptions.h"
Guy Benyei7f92f2d2012-12-18 14:30:41 +000026#include "clang/Basic/FileManager.h"
Guy Benyei7f92f2d2012-12-18 14:30:41 +000027#include "clang/Basic/SourceManager.h"
28#include "clang/Basic/SourceManagerInternals.h"
29#include "clang/Basic/TargetInfo.h"
30#include "clang/Basic/TargetOptions.h"
31#include "clang/Basic/Version.h"
32#include "clang/Basic/VersionTuple.h"
Stephen Hines6bcf27b2014-05-29 04:14:42 -070033#include "clang/Frontend/Utils.h"
Guy Benyei7f92f2d2012-12-18 14:30:41 +000034#include "clang/Lex/HeaderSearch.h"
35#include "clang/Lex/HeaderSearchOptions.h"
36#include "clang/Lex/MacroInfo.h"
37#include "clang/Lex/PreprocessingRecord.h"
38#include "clang/Lex/Preprocessor.h"
39#include "clang/Lex/PreprocessorOptions.h"
40#include "clang/Sema/Scope.h"
41#include "clang/Sema/Sema.h"
42#include "clang/Serialization/ASTDeserializationListener.h"
Douglas Gregor1a49d972013-01-25 01:03:03 +000043#include "clang/Serialization/GlobalModuleIndex.h"
Guy Benyei7f92f2d2012-12-18 14:30:41 +000044#include "clang/Serialization/ModuleManager.h"
45#include "clang/Serialization/SerializationDiagnostic.h"
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +000046#include "llvm/ADT/Hashing.h"
Guy Benyei7f92f2d2012-12-18 14:30:41 +000047#include "llvm/ADT/StringExtras.h"
48#include "llvm/Bitcode/BitstreamReader.h"
49#include "llvm/Support/ErrorHandling.h"
50#include "llvm/Support/FileSystem.h"
51#include "llvm/Support/MemoryBuffer.h"
52#include "llvm/Support/Path.h"
53#include "llvm/Support/SaveAndRestore.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070054#include "llvm/Support/raw_ostream.h"
Guy Benyei7f92f2d2012-12-18 14:30:41 +000055#include "llvm/Support/system_error.h"
56#include <algorithm>
Chris Lattnere4e4a882013-01-20 00:57:52 +000057#include <cstdio>
Guy Benyei7f92f2d2012-12-18 14:30:41 +000058#include <iterator>
59
60using namespace clang;
61using namespace clang::serialization;
62using namespace clang::serialization::reader;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +000063using llvm::BitstreamCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +000064
Stephen Hines651f13c2014-04-23 16:59:28 -070065
66//===----------------------------------------------------------------------===//
67// ChainedASTReaderListener implementation
68//===----------------------------------------------------------------------===//
69
70bool
71ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) {
72 return First->ReadFullVersionInformation(FullVersion) ||
73 Second->ReadFullVersionInformation(FullVersion);
74}
Stephen Hines6bcf27b2014-05-29 04:14:42 -070075void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) {
76 First->ReadModuleName(ModuleName);
77 Second->ReadModuleName(ModuleName);
78}
79void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) {
80 First->ReadModuleMapFile(ModuleMapPath);
81 Second->ReadModuleMapFile(ModuleMapPath);
82}
Stephen Hines651f13c2014-04-23 16:59:28 -070083bool ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts,
84 bool Complain) {
85 return First->ReadLanguageOptions(LangOpts, Complain) ||
86 Second->ReadLanguageOptions(LangOpts, Complain);
87}
88bool
89ChainedASTReaderListener::ReadTargetOptions(const TargetOptions &TargetOpts,
90 bool Complain) {
91 return First->ReadTargetOptions(TargetOpts, Complain) ||
92 Second->ReadTargetOptions(TargetOpts, Complain);
93}
94bool ChainedASTReaderListener::ReadDiagnosticOptions(
Stephen Hines6bcf27b2014-05-29 04:14:42 -070095 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
Stephen Hines651f13c2014-04-23 16:59:28 -070096 return First->ReadDiagnosticOptions(DiagOpts, Complain) ||
97 Second->ReadDiagnosticOptions(DiagOpts, Complain);
98}
99bool
100ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts,
101 bool Complain) {
102 return First->ReadFileSystemOptions(FSOpts, Complain) ||
103 Second->ReadFileSystemOptions(FSOpts, Complain);
104}
105
106bool ChainedASTReaderListener::ReadHeaderSearchOptions(
107 const HeaderSearchOptions &HSOpts, bool Complain) {
108 return First->ReadHeaderSearchOptions(HSOpts, Complain) ||
109 Second->ReadHeaderSearchOptions(HSOpts, Complain);
110}
111bool ChainedASTReaderListener::ReadPreprocessorOptions(
112 const PreprocessorOptions &PPOpts, bool Complain,
113 std::string &SuggestedPredefines) {
114 return First->ReadPreprocessorOptions(PPOpts, Complain,
115 SuggestedPredefines) ||
116 Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines);
117}
118void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M,
119 unsigned Value) {
120 First->ReadCounter(M, Value);
121 Second->ReadCounter(M, Value);
122}
123bool ChainedASTReaderListener::needsInputFileVisitation() {
124 return First->needsInputFileVisitation() ||
125 Second->needsInputFileVisitation();
126}
127bool ChainedASTReaderListener::needsSystemInputFileVisitation() {
128 return First->needsSystemInputFileVisitation() ||
129 Second->needsSystemInputFileVisitation();
130}
131void ChainedASTReaderListener::visitModuleFile(StringRef Filename) {
132 First->visitModuleFile(Filename);
133 Second->visitModuleFile(Filename);
134}
135bool ChainedASTReaderListener::visitInputFile(StringRef Filename,
136 bool isSystem,
137 bool isOverridden) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700138 bool Continue = false;
139 if (First->needsInputFileVisitation() &&
140 (!isSystem || First->needsSystemInputFileVisitation()))
141 Continue |= First->visitInputFile(Filename, isSystem, isOverridden);
142 if (Second->needsInputFileVisitation() &&
143 (!isSystem || Second->needsSystemInputFileVisitation()))
144 Continue |= Second->visitInputFile(Filename, isSystem, isOverridden);
145 return Continue;
Stephen Hines651f13c2014-04-23 16:59:28 -0700146}
147
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000148//===----------------------------------------------------------------------===//
149// PCH validator implementation
150//===----------------------------------------------------------------------===//
151
152ASTReaderListener::~ASTReaderListener() {}
153
154/// \brief Compare the given set of language options against an existing set of
155/// language options.
156///
157/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
158///
159/// \returns true if the languagae options mis-match, false otherwise.
160static bool checkLanguageOptions(const LangOptions &LangOpts,
161 const LangOptions &ExistingLangOpts,
162 DiagnosticsEngine *Diags) {
163#define LANGOPT(Name, Bits, Default, Description) \
164 if (ExistingLangOpts.Name != LangOpts.Name) { \
165 if (Diags) \
166 Diags->Report(diag::err_pch_langopt_mismatch) \
167 << Description << LangOpts.Name << ExistingLangOpts.Name; \
168 return true; \
169 }
170
171#define VALUE_LANGOPT(Name, Bits, Default, Description) \
172 if (ExistingLangOpts.Name != LangOpts.Name) { \
173 if (Diags) \
174 Diags->Report(diag::err_pch_langopt_value_mismatch) \
175 << Description; \
176 return true; \
177 }
178
179#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
180 if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \
181 if (Diags) \
182 Diags->Report(diag::err_pch_langopt_value_mismatch) \
183 << Description; \
184 return true; \
185 }
186
187#define BENIGN_LANGOPT(Name, Bits, Default, Description)
188#define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
189#include "clang/Basic/LangOptions.def"
190
191 if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
192 if (Diags)
193 Diags->Report(diag::err_pch_langopt_value_mismatch)
194 << "target Objective-C runtime";
195 return true;
196 }
197
Dmitri Gribenko6ebf0912013-02-22 14:21:27 +0000198 if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
199 LangOpts.CommentOpts.BlockCommandNames) {
200 if (Diags)
201 Diags->Report(diag::err_pch_langopt_value_mismatch)
202 << "block command names";
203 return true;
204 }
205
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000206 return false;
207}
208
209/// \brief Compare the given set of target options against an existing set of
210/// target options.
211///
212/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
213///
214/// \returns true if the target options mis-match, false otherwise.
215static bool checkTargetOptions(const TargetOptions &TargetOpts,
216 const TargetOptions &ExistingTargetOpts,
217 DiagnosticsEngine *Diags) {
218#define CHECK_TARGET_OPT(Field, Name) \
219 if (TargetOpts.Field != ExistingTargetOpts.Field) { \
220 if (Diags) \
221 Diags->Report(diag::err_pch_targetopt_mismatch) \
222 << Name << TargetOpts.Field << ExistingTargetOpts.Field; \
223 return true; \
224 }
225
226 CHECK_TARGET_OPT(Triple, "target");
227 CHECK_TARGET_OPT(CPU, "target CPU");
228 CHECK_TARGET_OPT(ABI, "target ABI");
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000229#undef CHECK_TARGET_OPT
230
231 // Compare feature sets.
232 SmallVector<StringRef, 4> ExistingFeatures(
233 ExistingTargetOpts.FeaturesAsWritten.begin(),
234 ExistingTargetOpts.FeaturesAsWritten.end());
235 SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
236 TargetOpts.FeaturesAsWritten.end());
237 std::sort(ExistingFeatures.begin(), ExistingFeatures.end());
238 std::sort(ReadFeatures.begin(), ReadFeatures.end());
239
240 unsigned ExistingIdx = 0, ExistingN = ExistingFeatures.size();
241 unsigned ReadIdx = 0, ReadN = ReadFeatures.size();
242 while (ExistingIdx < ExistingN && ReadIdx < ReadN) {
243 if (ExistingFeatures[ExistingIdx] == ReadFeatures[ReadIdx]) {
244 ++ExistingIdx;
245 ++ReadIdx;
246 continue;
247 }
248
249 if (ReadFeatures[ReadIdx] < ExistingFeatures[ExistingIdx]) {
250 if (Diags)
251 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
252 << false << ReadFeatures[ReadIdx];
253 return true;
254 }
255
256 if (Diags)
257 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
258 << true << ExistingFeatures[ExistingIdx];
259 return true;
260 }
261
262 if (ExistingIdx < ExistingN) {
263 if (Diags)
264 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
265 << true << ExistingFeatures[ExistingIdx];
266 return true;
267 }
268
269 if (ReadIdx < ReadN) {
270 if (Diags)
271 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
272 << false << ReadFeatures[ReadIdx];
273 return true;
274 }
275
276 return false;
277}
278
279bool
280PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
281 bool Complain) {
282 const LangOptions &ExistingLangOpts = PP.getLangOpts();
283 return checkLanguageOptions(LangOpts, ExistingLangOpts,
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700284 Complain? &Reader.Diags : nullptr);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000285}
286
287bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
288 bool Complain) {
289 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
290 return checkTargetOptions(TargetOpts, ExistingTargetOpts,
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700291 Complain? &Reader.Diags : nullptr);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000292}
293
294namespace {
295 typedef llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> >
296 MacroDefinitionsMap;
Craig Topper8ae63872013-07-05 04:43:31 +0000297 typedef llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> >
298 DeclsMap;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000299}
300
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700301static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags,
302 DiagnosticsEngine &Diags,
303 bool Complain) {
304 typedef DiagnosticsEngine::Level Level;
305
306 // Check current mappings for new -Werror mappings, and the stored mappings
307 // for cases that were explicitly mapped to *not* be errors that are now
308 // errors because of options like -Werror.
309 DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags };
310
311 for (DiagnosticsEngine *MappingSource : MappingSources) {
312 for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {
313 diag::kind DiagID = DiagIDMappingPair.first;
314 Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation());
315 if (CurLevel < DiagnosticsEngine::Error)
316 continue; // not significant
317 Level StoredLevel =
318 StoredDiags.getDiagnosticLevel(DiagID, SourceLocation());
319 if (StoredLevel < DiagnosticsEngine::Error) {
320 if (Complain)
321 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" +
322 Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str();
323 return true;
324 }
325 }
326 }
327
328 return false;
329}
330
331static DiagnosticsEngine::ExtensionHandling
332isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) {
333 DiagnosticsEngine::ExtensionHandling Ext =
334 Diags.getExtensionHandlingBehavior();
335 if (Ext == DiagnosticsEngine::Ext_Warn && Diags.getWarningsAsErrors())
336 Ext = DiagnosticsEngine::Ext_Error;
337 return Ext;
338}
339
340static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags,
341 DiagnosticsEngine &Diags,
342 bool IsSystem, bool Complain) {
343 // Top-level options
344 if (IsSystem) {
345 if (Diags.getSuppressSystemWarnings())
346 return false;
347 // If -Wsystem-headers was not enabled before, be conservative
348 if (StoredDiags.getSuppressSystemWarnings()) {
349 if (Complain)
350 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers";
351 return true;
352 }
353 }
354
355 if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) {
356 if (Complain)
357 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror";
358 return true;
359 }
360
361 if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() &&
362 !StoredDiags.getEnableAllWarnings()) {
363 if (Complain)
364 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror";
365 return true;
366 }
367
368 if (isExtHandlingFromDiagsError(Diags) &&
369 !isExtHandlingFromDiagsError(StoredDiags)) {
370 if (Complain)
371 Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors";
372 return true;
373 }
374
375 return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain);
376}
377
378bool PCHValidator::ReadDiagnosticOptions(
379 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
380 DiagnosticsEngine &ExistingDiags = PP.getDiagnostics();
381 IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs());
382 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
383 new DiagnosticsEngine(DiagIDs, DiagOpts.getPtr()));
384 // This should never fail, because we would have processed these options
385 // before writing them to an ASTFile.
386 ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false);
387
388 ModuleManager &ModuleMgr = Reader.getModuleManager();
389 assert(ModuleMgr.size() >= 1 && "what ASTFile is this then");
390
391 // If the original import came from a file explicitly generated by the user,
392 // don't check the diagnostic mappings.
393 // FIXME: currently this is approximated by checking whether this is not a
394 // module import.
395 // Note: ModuleMgr.rbegin() may not be the current module, but it must be in
396 // the transitive closure of its imports, since unrelated modules cannot be
397 // imported until after this module finishes validation.
398 ModuleFile *TopImport = *ModuleMgr.rbegin();
399 while (!TopImport->ImportedBy.empty())
400 TopImport = TopImport->ImportedBy[0];
401 if (TopImport->Kind != MK_Module)
402 return false;
403
404 StringRef ModuleName = TopImport->ModuleName;
405 assert(!ModuleName.empty() && "diagnostic options read before module name");
406
407 Module *M = PP.getHeaderSearchInfo().lookupModule(ModuleName);
408 assert(M && "missing module");
409
410 // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that
411 // contains the union of their flags.
412 return checkDiagnosticMappings(*Diags, ExistingDiags, M->IsSystem, Complain);
413}
414
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000415/// \brief Collect the macro definitions provided by the given preprocessor
416/// options.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700417static void
418collectMacroDefinitions(const PreprocessorOptions &PPOpts,
419 MacroDefinitionsMap &Macros,
420 SmallVectorImpl<StringRef> *MacroNames = nullptr) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000421 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
422 StringRef Macro = PPOpts.Macros[I].first;
423 bool IsUndef = PPOpts.Macros[I].second;
424
425 std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
426 StringRef MacroName = MacroPair.first;
427 StringRef MacroBody = MacroPair.second;
428
429 // For an #undef'd macro, we only care about the name.
430 if (IsUndef) {
431 if (MacroNames && !Macros.count(MacroName))
432 MacroNames->push_back(MacroName);
433
434 Macros[MacroName] = std::make_pair("", true);
435 continue;
436 }
437
438 // For a #define'd macro, figure out the actual definition.
439 if (MacroName.size() == Macro.size())
440 MacroBody = "1";
441 else {
442 // Note: GCC drops anything following an end-of-line character.
443 StringRef::size_type End = MacroBody.find_first_of("\n\r");
444 MacroBody = MacroBody.substr(0, End);
445 }
446
447 if (MacroNames && !Macros.count(MacroName))
448 MacroNames->push_back(MacroName);
449 Macros[MacroName] = std::make_pair(MacroBody, false);
450 }
451}
452
453/// \brief Check the preprocessor options deserialized from the control block
454/// against the preprocessor options in an existing preprocessor.
455///
456/// \param Diags If non-null, produce diagnostics for any mismatches incurred.
457static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts,
458 const PreprocessorOptions &ExistingPPOpts,
459 DiagnosticsEngine *Diags,
460 FileManager &FileMgr,
Argyrios Kyrtzidis65110ca2013-04-26 21:33:40 +0000461 std::string &SuggestedPredefines,
462 const LangOptions &LangOpts) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000463 // Check macro definitions.
464 MacroDefinitionsMap ASTFileMacros;
465 collectMacroDefinitions(PPOpts, ASTFileMacros);
466 MacroDefinitionsMap ExistingMacros;
467 SmallVector<StringRef, 4> ExistingMacroNames;
468 collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames);
469
470 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
471 // Dig out the macro definition in the existing preprocessor options.
472 StringRef MacroName = ExistingMacroNames[I];
473 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
474
475 // Check whether we know anything about this macro name or not.
476 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> >::iterator Known
477 = ASTFileMacros.find(MacroName);
478 if (Known == ASTFileMacros.end()) {
479 // FIXME: Check whether this identifier was referenced anywhere in the
480 // AST file. If so, we should reject the AST file. Unfortunately, this
481 // information isn't in the control block. What shall we do about it?
482
483 if (Existing.second) {
484 SuggestedPredefines += "#undef ";
485 SuggestedPredefines += MacroName.str();
486 SuggestedPredefines += '\n';
487 } else {
488 SuggestedPredefines += "#define ";
489 SuggestedPredefines += MacroName.str();
490 SuggestedPredefines += ' ';
491 SuggestedPredefines += Existing.first.str();
492 SuggestedPredefines += '\n';
493 }
494 continue;
495 }
496
497 // If the macro was defined in one but undef'd in the other, we have a
498 // conflict.
499 if (Existing.second != Known->second.second) {
500 if (Diags) {
501 Diags->Report(diag::err_pch_macro_def_undef)
502 << MacroName << Known->second.second;
503 }
504 return true;
505 }
506
507 // If the macro was #undef'd in both, or if the macro bodies are identical,
508 // it's fine.
509 if (Existing.second || Existing.first == Known->second.first)
510 continue;
511
512 // The macro bodies differ; complain.
513 if (Diags) {
514 Diags->Report(diag::err_pch_macro_def_conflict)
515 << MacroName << Known->second.first << Existing.first;
516 }
517 return true;
518 }
519
520 // Check whether we're using predefines.
521 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines) {
522 if (Diags) {
523 Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
524 }
525 return true;
526 }
527
Argyrios Kyrtzidis65110ca2013-04-26 21:33:40 +0000528 // Detailed record is important since it is used for the module cache hash.
529 if (LangOpts.Modules &&
530 PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord) {
531 if (Diags) {
532 Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord;
533 }
534 return true;
535 }
536
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000537 // Compute the #include and #include_macros lines we need.
538 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
539 StringRef File = ExistingPPOpts.Includes[I];
540 if (File == ExistingPPOpts.ImplicitPCHInclude)
541 continue;
542
543 if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File)
544 != PPOpts.Includes.end())
545 continue;
546
547 SuggestedPredefines += "#include \"";
548 SuggestedPredefines +=
549 HeaderSearch::NormalizeDashIncludePath(File, FileMgr);
550 SuggestedPredefines += "\"\n";
551 }
552
553 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
554 StringRef File = ExistingPPOpts.MacroIncludes[I];
555 if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(),
556 File)
557 != PPOpts.MacroIncludes.end())
558 continue;
559
560 SuggestedPredefines += "#__include_macros \"";
561 SuggestedPredefines +=
562 HeaderSearch::NormalizeDashIncludePath(File, FileMgr);
563 SuggestedPredefines += "\"\n##\n";
564 }
565
566 return false;
567}
568
569bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
570 bool Complain,
571 std::string &SuggestedPredefines) {
572 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
573
574 return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700575 Complain? &Reader.Diags : nullptr,
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000576 PP.getFileManager(),
Argyrios Kyrtzidis65110ca2013-04-26 21:33:40 +0000577 SuggestedPredefines,
578 PP.getLangOpts());
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000579}
580
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000581void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
582 PP.setCounterValue(Value);
583}
584
585//===----------------------------------------------------------------------===//
586// AST reader implementation
587//===----------------------------------------------------------------------===//
588
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700589void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,
590 bool TakeOwnership) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000591 DeserializationListener = Listener;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700592 OwnsDeserializationListener = TakeOwnership;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000593}
594
595
596
597unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
598 return serialization::ComputeHash(Sel);
599}
600
601
602std::pair<unsigned, unsigned>
603ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700604 using namespace llvm::support;
605 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
606 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000607 return std::make_pair(KeyLen, DataLen);
608}
609
610ASTSelectorLookupTrait::internal_key_type
611ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700612 using namespace llvm::support;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000613 SelectorTable &SelTable = Reader.getContext().Selectors;
Stephen Hines651f13c2014-04-23 16:59:28 -0700614 unsigned N = endian::readNext<uint16_t, little, unaligned>(d);
615 IdentifierInfo *FirstII = Reader.getLocalIdentifier(
616 F, endian::readNext<uint32_t, little, unaligned>(d));
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000617 if (N == 0)
618 return SelTable.getNullarySelector(FirstII);
619 else if (N == 1)
620 return SelTable.getUnarySelector(FirstII);
621
622 SmallVector<IdentifierInfo *, 16> Args;
623 Args.push_back(FirstII);
624 for (unsigned I = 1; I != N; ++I)
Stephen Hines651f13c2014-04-23 16:59:28 -0700625 Args.push_back(Reader.getLocalIdentifier(
626 F, endian::readNext<uint32_t, little, unaligned>(d)));
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000627
628 return SelTable.getSelector(N, Args.data());
629}
630
631ASTSelectorLookupTrait::data_type
632ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
633 unsigned DataLen) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700634 using namespace llvm::support;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000635
636 data_type Result;
637
Stephen Hines651f13c2014-04-23 16:59:28 -0700638 Result.ID = Reader.getGlobalSelectorID(
639 F, endian::readNext<uint32_t, little, unaligned>(d));
640 unsigned NumInstanceMethodsAndBits =
641 endian::readNext<uint16_t, little, unaligned>(d);
642 unsigned NumFactoryMethodsAndBits =
643 endian::readNext<uint16_t, little, unaligned>(d);
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +0000644 Result.InstanceBits = NumInstanceMethodsAndBits & 0x3;
645 Result.FactoryBits = NumFactoryMethodsAndBits & 0x3;
646 unsigned NumInstanceMethods = NumInstanceMethodsAndBits >> 2;
647 unsigned NumFactoryMethods = NumFactoryMethodsAndBits >> 2;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000648
649 // Load instance methods
650 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700651 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
652 F, endian::readNext<uint32_t, little, unaligned>(d)))
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000653 Result.Instance.push_back(Method);
654 }
655
656 // Load factory methods
657 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700658 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
659 F, endian::readNext<uint32_t, little, unaligned>(d)))
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000660 Result.Factory.push_back(Method);
661 }
662
663 return Result;
664}
665
Douglas Gregor479633c2013-01-23 18:53:14 +0000666unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
667 return llvm::HashString(a);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000668}
669
670std::pair<unsigned, unsigned>
Douglas Gregor479633c2013-01-23 18:53:14 +0000671ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700672 using namespace llvm::support;
673 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
674 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000675 return std::make_pair(KeyLen, DataLen);
676}
677
Douglas Gregor479633c2013-01-23 18:53:14 +0000678ASTIdentifierLookupTraitBase::internal_key_type
679ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000680 assert(n >= 2 && d[n-1] == '\0');
Douglas Gregor479633c2013-01-23 18:53:14 +0000681 return StringRef((const char*) d, n-1);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000682}
683
Douglas Gregorf4e955b2013-02-11 18:16:18 +0000684/// \brief Whether the given identifier is "interesting".
685static bool isInterestingIdentifier(IdentifierInfo &II) {
686 return II.isPoisoned() ||
687 II.isExtensionToken() ||
688 II.getObjCOrBuiltinID() ||
689 II.hasRevertedTokenIDToIdentifier() ||
690 II.hadMacroDefinition() ||
691 II.getFETokenInfo<void>();
692}
693
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000694IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
695 const unsigned char* d,
696 unsigned DataLen) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700697 using namespace llvm::support;
698 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000699 bool IsInteresting = RawID & 0x01;
700
701 // Wipe out the "is interesting" bit.
702 RawID = RawID >> 1;
703
704 IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
705 if (!IsInteresting) {
706 // For uninteresting identifiers, just build the IdentifierInfo
707 // and associate it with the persistent ID.
708 IdentifierInfo *II = KnownII;
709 if (!II) {
Douglas Gregor479633c2013-01-23 18:53:14 +0000710 II = &Reader.getIdentifierTable().getOwn(k);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000711 KnownII = II;
712 }
713 Reader.SetIdentifierInfo(ID, II);
Douglas Gregorf4e955b2013-02-11 18:16:18 +0000714 if (!II->isFromAST()) {
715 bool WasInteresting = isInterestingIdentifier(*II);
716 II->setIsFromAST();
717 if (WasInteresting)
718 II->setChangedSinceDeserialization();
719 }
720 Reader.markIdentifierUpToDate(II);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000721 return II;
722 }
723
Stephen Hines651f13c2014-04-23 16:59:28 -0700724 unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d);
725 unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000726 bool CPlusPlusOperatorKeyword = Bits & 0x01;
727 Bits >>= 1;
728 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
729 Bits >>= 1;
730 bool Poisoned = Bits & 0x01;
731 Bits >>= 1;
732 bool ExtensionToken = Bits & 0x01;
733 Bits >>= 1;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +0000734 bool hasSubmoduleMacros = Bits & 0x01;
735 Bits >>= 1;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000736 bool hadMacroDefinition = Bits & 0x01;
737 Bits >>= 1;
738
739 assert(Bits == 0 && "Extra bits in the identifier?");
740 DataLen -= 8;
741
742 // Build the IdentifierInfo itself and link the identifier ID with
743 // the new IdentifierInfo.
744 IdentifierInfo *II = KnownII;
745 if (!II) {
Douglas Gregor479633c2013-01-23 18:53:14 +0000746 II = &Reader.getIdentifierTable().getOwn(StringRef(k));
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000747 KnownII = II;
748 }
749 Reader.markIdentifierUpToDate(II);
Douglas Gregorf4e955b2013-02-11 18:16:18 +0000750 if (!II->isFromAST()) {
751 bool WasInteresting = isInterestingIdentifier(*II);
752 II->setIsFromAST();
753 if (WasInteresting)
754 II->setChangedSinceDeserialization();
755 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000756
757 // Set or check the various bits in the IdentifierInfo structure.
758 // Token IDs are read-only.
Argyrios Kyrtzidis1ebefc72013-02-27 01:13:51 +0000759 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000760 II->RevertTokenIDToIdentifier();
761 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
762 assert(II->isExtensionToken() == ExtensionToken &&
763 "Incorrect extension token flag");
764 (void)ExtensionToken;
765 if (Poisoned)
766 II->setIsPoisoned(true);
767 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
768 "Incorrect C++ operator keyword flag");
769 (void)CPlusPlusOperatorKeyword;
770
771 // If this identifier is a macro, deserialize the macro
772 // definition.
773 if (hadMacroDefinition) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700774 uint32_t MacroDirectivesOffset =
775 endian::readNext<uint32_t, little, unaligned>(d);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +0000776 DataLen -= 4;
777 SmallVector<uint32_t, 8> LocalMacroIDs;
778 if (hasSubmoduleMacros) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700779 while (uint32_t LocalMacroID =
780 endian::readNext<uint32_t, little, unaligned>(d)) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +0000781 DataLen -= 4;
782 LocalMacroIDs.push_back(LocalMacroID);
783 }
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +0000784 DataLen -= 4;
785 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +0000786
787 if (F.Kind == MK_Module) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700788 // Macro definitions are stored from newest to oldest, so reverse them
789 // before registering them.
790 llvm::SmallVector<unsigned, 8> MacroSizes;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +0000791 for (SmallVectorImpl<uint32_t>::iterator
Stephen Hines651f13c2014-04-23 16:59:28 -0700792 I = LocalMacroIDs.begin(), E = LocalMacroIDs.end(); I != E; /**/) {
793 unsigned Size = 1;
794
795 static const uint32_t HasOverridesFlag = 0x80000000U;
796 if (I + 1 != E && (I[1] & HasOverridesFlag))
797 Size += 1 + (I[1] & ~HasOverridesFlag);
798
799 MacroSizes.push_back(Size);
800 I += Size;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +0000801 }
Stephen Hines651f13c2014-04-23 16:59:28 -0700802
803 SmallVectorImpl<uint32_t>::iterator I = LocalMacroIDs.end();
804 for (SmallVectorImpl<unsigned>::reverse_iterator SI = MacroSizes.rbegin(),
805 SE = MacroSizes.rend();
806 SI != SE; ++SI) {
807 I -= *SI;
808
809 uint32_t LocalMacroID = *I;
810 llvm::ArrayRef<uint32_t> Overrides;
811 if (*SI != 1)
812 Overrides = llvm::makeArrayRef(&I[2], *SI - 2);
813 Reader.addPendingMacroFromModule(II, &F, LocalMacroID, Overrides);
814 }
815 assert(I == LocalMacroIDs.begin());
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +0000816 } else {
817 Reader.addPendingMacroFromPCH(II, &F, MacroDirectivesOffset);
818 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000819 }
820
821 Reader.SetIdentifierInfo(ID, II);
822
823 // Read all of the declarations visible at global scope with this
824 // name.
825 if (DataLen > 0) {
826 SmallVector<uint32_t, 4> DeclIDs;
827 for (; DataLen > 0; DataLen -= 4)
Stephen Hines651f13c2014-04-23 16:59:28 -0700828 DeclIDs.push_back(Reader.getGlobalDeclID(
829 F, endian::readNext<uint32_t, little, unaligned>(d)));
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000830 Reader.SetGloballyVisibleDecls(II, DeclIDs);
831 }
832
833 return II;
834}
835
836unsigned
837ASTDeclContextNameLookupTrait::ComputeHash(const DeclNameKey &Key) const {
838 llvm::FoldingSetNodeID ID;
839 ID.AddInteger(Key.Kind);
840
841 switch (Key.Kind) {
842 case DeclarationName::Identifier:
843 case DeclarationName::CXXLiteralOperatorName:
844 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
845 break;
846 case DeclarationName::ObjCZeroArgSelector:
847 case DeclarationName::ObjCOneArgSelector:
848 case DeclarationName::ObjCMultiArgSelector:
849 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
850 break;
851 case DeclarationName::CXXOperatorName:
852 ID.AddInteger((OverloadedOperatorKind)Key.Data);
853 break;
854 case DeclarationName::CXXConstructorName:
855 case DeclarationName::CXXDestructorName:
856 case DeclarationName::CXXConversionFunctionName:
857 case DeclarationName::CXXUsingDirective:
858 break;
859 }
860
861 return ID.ComputeHash();
862}
863
864ASTDeclContextNameLookupTrait::internal_key_type
865ASTDeclContextNameLookupTrait::GetInternalKey(
866 const external_key_type& Name) const {
867 DeclNameKey Key;
868 Key.Kind = Name.getNameKind();
869 switch (Name.getNameKind()) {
870 case DeclarationName::Identifier:
871 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
872 break;
873 case DeclarationName::ObjCZeroArgSelector:
874 case DeclarationName::ObjCOneArgSelector:
875 case DeclarationName::ObjCMultiArgSelector:
876 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
877 break;
878 case DeclarationName::CXXOperatorName:
879 Key.Data = Name.getCXXOverloadedOperator();
880 break;
881 case DeclarationName::CXXLiteralOperatorName:
882 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
883 break;
884 case DeclarationName::CXXConstructorName:
885 case DeclarationName::CXXDestructorName:
886 case DeclarationName::CXXConversionFunctionName:
887 case DeclarationName::CXXUsingDirective:
888 Key.Data = 0;
889 break;
890 }
891
892 return Key;
893}
894
895std::pair<unsigned, unsigned>
896ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700897 using namespace llvm::support;
898 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
899 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000900 return std::make_pair(KeyLen, DataLen);
901}
902
903ASTDeclContextNameLookupTrait::internal_key_type
904ASTDeclContextNameLookupTrait::ReadKey(const unsigned char* d, unsigned) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700905 using namespace llvm::support;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000906
907 DeclNameKey Key;
908 Key.Kind = (DeclarationName::NameKind)*d++;
909 switch (Key.Kind) {
910 case DeclarationName::Identifier:
Stephen Hines651f13c2014-04-23 16:59:28 -0700911 Key.Data = (uint64_t)Reader.getLocalIdentifier(
912 F, endian::readNext<uint32_t, little, unaligned>(d));
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000913 break;
914 case DeclarationName::ObjCZeroArgSelector:
915 case DeclarationName::ObjCOneArgSelector:
916 case DeclarationName::ObjCMultiArgSelector:
917 Key.Data =
Stephen Hines651f13c2014-04-23 16:59:28 -0700918 (uint64_t)Reader.getLocalSelector(
919 F, endian::readNext<uint32_t, little, unaligned>(
920 d)).getAsOpaquePtr();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000921 break;
922 case DeclarationName::CXXOperatorName:
923 Key.Data = *d++; // OverloadedOperatorKind
924 break;
925 case DeclarationName::CXXLiteralOperatorName:
Stephen Hines651f13c2014-04-23 16:59:28 -0700926 Key.Data = (uint64_t)Reader.getLocalIdentifier(
927 F, endian::readNext<uint32_t, little, unaligned>(d));
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000928 break;
929 case DeclarationName::CXXConstructorName:
930 case DeclarationName::CXXDestructorName:
931 case DeclarationName::CXXConversionFunctionName:
932 case DeclarationName::CXXUsingDirective:
933 Key.Data = 0;
934 break;
935 }
936
937 return Key;
938}
939
940ASTDeclContextNameLookupTrait::data_type
941ASTDeclContextNameLookupTrait::ReadData(internal_key_type,
942 const unsigned char* d,
943 unsigned DataLen) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700944 using namespace llvm::support;
945 unsigned NumDecls = endian::readNext<uint16_t, little, unaligned>(d);
Argyrios Kyrtzidise8b61cf2013-01-11 22:29:49 +0000946 LE32DeclID *Start = reinterpret_cast<LE32DeclID *>(
947 const_cast<unsigned char *>(d));
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000948 return std::make_pair(Start, Start + NumDecls);
949}
950
951bool ASTReader::ReadDeclContextStorage(ModuleFile &M,
Chris Lattner8f9a1eb2013-01-20 00:56:42 +0000952 BitstreamCursor &Cursor,
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000953 const std::pair<uint64_t, uint64_t> &Offsets,
954 DeclContextInfo &Info) {
955 SavedStreamPosition SavedPosition(Cursor);
956 // First the lexical decls.
957 if (Offsets.first != 0) {
958 Cursor.JumpToBit(Offsets.first);
959
960 RecordData Record;
Chris Lattnerb3ce3572013-01-20 02:38:54 +0000961 StringRef Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000962 unsigned Code = Cursor.ReadCode();
Chris Lattnerb3ce3572013-01-20 02:38:54 +0000963 unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000964 if (RecCode != DECL_CONTEXT_LEXICAL) {
965 Error("Expected lexical block");
966 return true;
967 }
968
Chris Lattnerb3ce3572013-01-20 02:38:54 +0000969 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob.data());
970 Info.NumLexicalDecls = Blob.size() / sizeof(KindDeclIDPair);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000971 }
972
973 // Now the lookup table.
974 if (Offsets.second != 0) {
975 Cursor.JumpToBit(Offsets.second);
976
977 RecordData Record;
Chris Lattnerb3ce3572013-01-20 02:38:54 +0000978 StringRef Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000979 unsigned Code = Cursor.ReadCode();
Chris Lattnerb3ce3572013-01-20 02:38:54 +0000980 unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000981 if (RecCode != DECL_CONTEXT_VISIBLE) {
982 Error("Expected visible lookup table block");
983 return true;
984 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700985 Info.NameLookupTableData = ASTDeclContextNameLookupTable::Create(
986 (const unsigned char *)Blob.data() + Record[0],
987 (const unsigned char *)Blob.data() + sizeof(uint32_t),
988 (const unsigned char *)Blob.data(),
989 ASTDeclContextNameLookupTrait(*this, M));
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000990 }
991
992 return false;
993}
994
995void ASTReader::Error(StringRef Msg) {
996 Error(diag::err_fe_pch_malformed, Msg);
Douglas Gregorc1478612013-05-10 22:15:13 +0000997 if (Context.getLangOpts().Modules && !Diags.isDiagnosticInFlight()) {
998 Diag(diag::note_module_cache_path)
999 << PP.getHeaderSearchInfo().getModuleCachePath();
1000 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001001}
1002
1003void ASTReader::Error(unsigned DiagID,
1004 StringRef Arg1, StringRef Arg2) {
1005 if (Diags.isDiagnosticInFlight())
1006 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
1007 else
1008 Diag(DiagID) << Arg1 << Arg2;
1009}
1010
1011//===----------------------------------------------------------------------===//
1012// Source Manager Deserialization
1013//===----------------------------------------------------------------------===//
1014
1015/// \brief Read the line table in the source manager block.
1016/// \returns true if there was an error.
1017bool ASTReader::ParseLineTable(ModuleFile &F,
1018 SmallVectorImpl<uint64_t> &Record) {
1019 unsigned Idx = 0;
1020 LineTableInfo &LineTable = SourceMgr.getLineTable();
1021
1022 // Parse the file names
1023 std::map<int, int> FileIDs;
1024 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
1025 // Extract the file name
1026 unsigned FilenameLen = Record[Idx++];
1027 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
1028 Idx += FilenameLen;
1029 MaybeAddSystemRootToFilename(F, Filename);
1030 FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
1031 }
1032
1033 // Parse the line entries
1034 std::vector<LineEntry> Entries;
1035 while (Idx < Record.size()) {
1036 int FID = Record[Idx++];
1037 assert(FID >= 0 && "Serialized line entries for non-local file.");
1038 // Remap FileID from 1-based old view.
1039 FID += F.SLocEntryBaseID - 1;
1040
1041 // Extract the line entries
1042 unsigned NumEntries = Record[Idx++];
1043 assert(NumEntries && "Numentries is 00000");
1044 Entries.clear();
1045 Entries.reserve(NumEntries);
1046 for (unsigned I = 0; I != NumEntries; ++I) {
1047 unsigned FileOffset = Record[Idx++];
1048 unsigned LineNo = Record[Idx++];
1049 int FilenameID = FileIDs[Record[Idx++]];
1050 SrcMgr::CharacteristicKind FileKind
1051 = (SrcMgr::CharacteristicKind)Record[Idx++];
1052 unsigned IncludeOffset = Record[Idx++];
1053 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1054 FileKind, IncludeOffset));
1055 }
1056 LineTable.AddEntry(FileID::get(FID), Entries);
1057 }
1058
1059 return false;
1060}
1061
1062/// \brief Read a source manager block
1063bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1064 using namespace SrcMgr;
1065
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001066 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001067
1068 // Set the source-location entry cursor to the current position in
1069 // the stream. This cursor will be used to read the contents of the
1070 // source manager block initially, and then lazily read
1071 // source-location entries as needed.
1072 SLocEntryCursor = F.Stream;
1073
1074 // The stream itself is going to skip over the source manager block.
1075 if (F.Stream.SkipBlock()) {
1076 Error("malformed block record in AST file");
1077 return true;
1078 }
1079
1080 // Enter the source manager block.
1081 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
1082 Error("malformed source manager block record in AST file");
1083 return true;
1084 }
1085
1086 RecordData Record;
1087 while (true) {
Chris Lattner88bde502013-01-19 21:39:22 +00001088 llvm::BitstreamEntry E = SLocEntryCursor.advanceSkippingSubblocks();
1089
1090 switch (E.Kind) {
1091 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1092 case llvm::BitstreamEntry::Error:
1093 Error("malformed block record in AST file");
1094 return true;
1095 case llvm::BitstreamEntry::EndBlock:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001096 return false;
Chris Lattner88bde502013-01-19 21:39:22 +00001097 case llvm::BitstreamEntry::Record:
1098 // The interesting case.
1099 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001100 }
Chris Lattner88bde502013-01-19 21:39:22 +00001101
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001102 // Read a record.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001103 Record.clear();
Chris Lattner125eb3e2013-01-21 18:28:26 +00001104 StringRef Blob;
1105 switch (SLocEntryCursor.readRecord(E.ID, Record, &Blob)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001106 default: // Default behavior: ignore.
1107 break;
1108
1109 case SM_SLOC_FILE_ENTRY:
1110 case SM_SLOC_BUFFER_ENTRY:
1111 case SM_SLOC_EXPANSION_ENTRY:
1112 // Once we hit one of the source location entries, we're done.
1113 return false;
1114 }
1115 }
1116}
1117
1118/// \brief If a header file is not found at the path that we expect it to be
1119/// and the PCH file was moved from its original location, try to resolve the
1120/// file by assuming that header+PCH were moved together and the header is in
1121/// the same place relative to the PCH.
1122static std::string
1123resolveFileRelativeToOriginalDir(const std::string &Filename,
1124 const std::string &OriginalDir,
1125 const std::string &CurrDir) {
1126 assert(OriginalDir != CurrDir &&
1127 "No point trying to resolve the file if the PCH dir didn't change");
1128 using namespace llvm::sys;
1129 SmallString<128> filePath(Filename);
1130 fs::make_absolute(filePath);
1131 assert(path::is_absolute(OriginalDir));
1132 SmallString<128> currPCHPath(CurrDir);
1133
1134 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1135 fileDirE = path::end(path::parent_path(filePath));
1136 path::const_iterator origDirI = path::begin(OriginalDir),
1137 origDirE = path::end(OriginalDir);
1138 // Skip the common path components from filePath and OriginalDir.
1139 while (fileDirI != fileDirE && origDirI != origDirE &&
1140 *fileDirI == *origDirI) {
1141 ++fileDirI;
1142 ++origDirI;
1143 }
1144 for (; origDirI != origDirE; ++origDirI)
1145 path::append(currPCHPath, "..");
1146 path::append(currPCHPath, fileDirI, fileDirE);
1147 path::append(currPCHPath, path::filename(Filename));
1148 return currPCHPath.str();
1149}
1150
1151bool ASTReader::ReadSLocEntry(int ID) {
1152 if (ID == 0)
1153 return false;
1154
1155 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1156 Error("source location entry ID out-of-range for AST file");
1157 return true;
1158 }
1159
1160 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
1161 F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]);
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001162 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001163 unsigned BaseOffset = F->SLocEntryBaseOffset;
1164
1165 ++NumSLocEntriesRead;
Chris Lattner88bde502013-01-19 21:39:22 +00001166 llvm::BitstreamEntry Entry = SLocEntryCursor.advance();
1167 if (Entry.Kind != llvm::BitstreamEntry::Record) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001168 Error("incorrectly-formatted source location entry in AST file");
1169 return true;
1170 }
Chris Lattner88bde502013-01-19 21:39:22 +00001171
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001172 RecordData Record;
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001173 StringRef Blob;
1174 switch (SLocEntryCursor.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001175 default:
1176 Error("incorrectly-formatted source location entry in AST file");
1177 return true;
1178
1179 case SM_SLOC_FILE_ENTRY: {
1180 // We will detect whether a file changed and return 'Failure' for it, but
1181 // we will also try to fail gracefully by setting up the SLocEntry.
1182 unsigned InputID = Record[4];
1183 InputFile IF = getInputFile(*F, InputID);
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00001184 const FileEntry *File = IF.getFile();
1185 bool OverriddenBuffer = IF.isOverridden();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001186
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00001187 // Note that we only check if a File was returned. If it was out-of-date
1188 // we have complained but we will continue creating a FileID to recover
1189 // gracefully.
1190 if (!File)
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001191 return true;
1192
1193 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1194 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1195 // This is the module's main file.
1196 IncludeLoc = getImportLocation(F);
1197 }
1198 SrcMgr::CharacteristicKind
1199 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1200 FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
1201 ID, BaseOffset + Record[0]);
1202 SrcMgr::FileInfo &FileInfo =
1203 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1204 FileInfo.NumCreatedFIDs = Record[5];
1205 if (Record[3])
1206 FileInfo.setHasLineDirectives();
1207
1208 const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
1209 unsigned NumFileDecls = Record[7];
1210 if (NumFileDecls) {
1211 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1212 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1213 NumFileDecls));
1214 }
1215
1216 const SrcMgr::ContentCache *ContentCache
1217 = SourceMgr.getOrCreateContentCache(File,
1218 /*isSystemFile=*/FileCharacter != SrcMgr::C_User);
1219 if (OverriddenBuffer && !ContentCache->BufferOverridden &&
1220 ContentCache->ContentsEntry == ContentCache->OrigEntry) {
1221 unsigned Code = SLocEntryCursor.ReadCode();
1222 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001223 unsigned RecCode = SLocEntryCursor.readRecord(Code, Record, &Blob);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001224
1225 if (RecCode != SM_SLOC_BUFFER_BLOB) {
1226 Error("AST record has invalid code");
1227 return true;
1228 }
1229
1230 llvm::MemoryBuffer *Buffer
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001231 = llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), File->getName());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001232 SourceMgr.overrideFileContents(File, Buffer);
1233 }
1234
1235 break;
1236 }
1237
1238 case SM_SLOC_BUFFER_ENTRY: {
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001239 const char *Name = Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001240 unsigned Offset = Record[0];
1241 SrcMgr::CharacteristicKind
1242 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1243 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1244 if (IncludeLoc.isInvalid() && F->Kind == MK_Module) {
1245 IncludeLoc = getImportLocation(F);
1246 }
1247 unsigned Code = SLocEntryCursor.ReadCode();
1248 Record.clear();
1249 unsigned RecCode
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001250 = SLocEntryCursor.readRecord(Code, Record, &Blob);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001251
1252 if (RecCode != SM_SLOC_BUFFER_BLOB) {
1253 Error("AST record has invalid code");
1254 return true;
1255 }
1256
1257 llvm::MemoryBuffer *Buffer
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001258 = llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001259 SourceMgr.createFileID(Buffer, FileCharacter, ID, BaseOffset + Offset,
1260 IncludeLoc);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001261 break;
1262 }
1263
1264 case SM_SLOC_EXPANSION_ENTRY: {
1265 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1266 SourceMgr.createExpansionLoc(SpellingLoc,
1267 ReadSourceLocation(*F, Record[2]),
1268 ReadSourceLocation(*F, Record[3]),
1269 Record[4],
1270 ID,
1271 BaseOffset + Record[0]);
1272 break;
1273 }
1274 }
1275
1276 return false;
1277}
1278
1279std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1280 if (ID == 0)
1281 return std::make_pair(SourceLocation(), "");
1282
1283 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1284 Error("source location entry ID out-of-range for AST file");
1285 return std::make_pair(SourceLocation(), "");
1286 }
1287
1288 // Find which module file this entry lands in.
1289 ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
1290 if (M->Kind != MK_Module)
1291 return std::make_pair(SourceLocation(), "");
1292
1293 // FIXME: Can we map this down to a particular submodule? That would be
1294 // ideal.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001295 return std::make_pair(M->ImportLoc, StringRef(M->ModuleName));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001296}
1297
1298/// \brief Find the location where the module F is imported.
1299SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1300 if (F->ImportLoc.isValid())
1301 return F->ImportLoc;
1302
1303 // Otherwise we have a PCH. It's considered to be "imported" at the first
1304 // location of its includer.
1305 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001306 // Main file is the importer.
1307 assert(!SourceMgr.getMainFileID().isInvalid() && "missing main file");
1308 return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001309 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001310 return F->ImportedBy[0]->FirstLoc;
1311}
1312
1313/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1314/// specified cursor. Read the abbreviations that are at the top of the block
1315/// and then leave the cursor pointing into the block.
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001316bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001317 if (Cursor.EnterSubBlock(BlockID)) {
1318 Error("malformed block record in AST file");
1319 return Failure;
1320 }
1321
1322 while (true) {
1323 uint64_t Offset = Cursor.GetCurrentBitNo();
1324 unsigned Code = Cursor.ReadCode();
1325
1326 // We expect all abbrevs to be at the start of the block.
1327 if (Code != llvm::bitc::DEFINE_ABBREV) {
1328 Cursor.JumpToBit(Offset);
1329 return false;
1330 }
1331 Cursor.ReadAbbrevRecord();
1332 }
1333}
1334
Richard Smithac32d902013-08-07 21:41:30 +00001335Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record,
John McCallaeeacf72013-05-03 00:10:13 +00001336 unsigned &Idx) {
1337 Token Tok;
1338 Tok.startToken();
1339 Tok.setLocation(ReadSourceLocation(F, Record, Idx));
1340 Tok.setLength(Record[Idx++]);
1341 if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
1342 Tok.setIdentifierInfo(II);
1343 Tok.setKind((tok::TokenKind)Record[Idx++]);
1344 Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1345 return Tok;
1346}
1347
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001348MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001349 BitstreamCursor &Stream = F.MacroCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001350
1351 // Keep track of where we are in the stream, then jump back there
1352 // after reading this macro.
1353 SavedStreamPosition SavedPosition(Stream);
1354
1355 Stream.JumpToBit(Offset);
1356 RecordData Record;
1357 SmallVector<IdentifierInfo*, 16> MacroArgs;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001358 MacroInfo *Macro = nullptr;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001359
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001360 while (true) {
Chris Lattner99a5af02013-01-20 00:00:22 +00001361 // Advance to the next record, but if we get to the end of the block, don't
1362 // pop it (removing all the abbreviations from the cursor) since we want to
1363 // be able to reseek within the block and read entries.
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001364 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
Chris Lattner99a5af02013-01-20 00:00:22 +00001365 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks(Flags);
1366
1367 switch (Entry.Kind) {
1368 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1369 case llvm::BitstreamEntry::Error:
1370 Error("malformed block record in AST file");
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001371 return Macro;
Chris Lattner99a5af02013-01-20 00:00:22 +00001372 case llvm::BitstreamEntry::EndBlock:
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001373 return Macro;
Chris Lattner99a5af02013-01-20 00:00:22 +00001374 case llvm::BitstreamEntry::Record:
1375 // The interesting case.
1376 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001377 }
1378
1379 // Read a record.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001380 Record.clear();
1381 PreprocessorRecordTypes RecType =
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001382 (PreprocessorRecordTypes)Stream.readRecord(Entry.ID, Record);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001383 switch (RecType) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001384 case PP_MACRO_DIRECTIVE_HISTORY:
1385 return Macro;
1386
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001387 case PP_MACRO_OBJECT_LIKE:
1388 case PP_MACRO_FUNCTION_LIKE: {
1389 // If we already have a macro, that means that we've hit the end
1390 // of the definition of the macro we were looking for. We're
1391 // done.
1392 if (Macro)
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001393 return Macro;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001394
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001395 unsigned NextIndex = 1; // Skip identifier ID.
1396 SubmoduleID SubModID = getGlobalSubmoduleID(F, Record[NextIndex++]);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001397 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001398 MacroInfo *MI = PP.AllocateDeserializedMacroInfo(Loc, SubModID);
Argyrios Kyrtzidis8169b672013-01-07 19:16:23 +00001399 MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001400 MI->setIsUsed(Record[NextIndex++]);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001401 MI->setUsedForHeaderGuard(Record[NextIndex++]);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001402
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001403 if (RecType == PP_MACRO_FUNCTION_LIKE) {
1404 // Decode function-like macro info.
1405 bool isC99VarArgs = Record[NextIndex++];
1406 bool isGNUVarArgs = Record[NextIndex++];
1407 bool hasCommaPasting = Record[NextIndex++];
1408 MacroArgs.clear();
1409 unsigned NumArgs = Record[NextIndex++];
1410 for (unsigned i = 0; i != NumArgs; ++i)
1411 MacroArgs.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1412
1413 // Install function-like macro info.
1414 MI->setIsFunctionLike();
1415 if (isC99VarArgs) MI->setIsC99Varargs();
1416 if (isGNUVarArgs) MI->setIsGNUVarargs();
1417 if (hasCommaPasting) MI->setHasCommaPasting();
1418 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
1419 PP.getPreprocessorAllocator());
1420 }
1421
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001422 // Remember that we saw this macro last so that we add the tokens that
1423 // form its body to it.
1424 Macro = MI;
1425
1426 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1427 Record[NextIndex]) {
1428 // We have a macro definition. Register the association
1429 PreprocessedEntityID
1430 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1431 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
Argyrios Kyrtzidis0b849d32013-02-22 18:35:59 +00001432 PreprocessingRecord::PPEntityID
1433 PPID = PPRec.getPPEntityID(GlobalID-1, /*isLoaded=*/true);
1434 MacroDefinition *PPDef =
1435 cast_or_null<MacroDefinition>(PPRec.getPreprocessedEntity(PPID));
1436 if (PPDef)
1437 PPRec.RegisterMacroDefinition(Macro, PPDef);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001438 }
1439
1440 ++NumMacrosRead;
1441 break;
1442 }
1443
1444 case PP_TOKEN: {
1445 // If we see a TOKEN before a PP_MACRO_*, then the file is
1446 // erroneous, just pretend we didn't see this.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001447 if (!Macro) break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001448
John McCallaeeacf72013-05-03 00:10:13 +00001449 unsigned Idx = 0;
1450 Token Tok = ReadToken(F, Record, Idx);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001451 Macro->AddTokenToBody(Tok);
1452 break;
1453 }
1454 }
1455 }
1456}
1457
1458PreprocessedEntityID
1459ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const {
1460 ContinuousRangeMap<uint32_t, int, 2>::const_iterator
1461 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1462 assert(I != M.PreprocessedEntityRemap.end()
1463 && "Invalid index into preprocessed entity index remap");
1464
1465 return LocalID + I->second;
1466}
1467
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001468unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1469 return llvm::hash_combine(ikey.Size, ikey.ModTime);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001470}
1471
1472HeaderFileInfoTrait::internal_key_type
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001473HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
1474 internal_key_type ikey = { FE->getSize(), FE->getModificationTime(),
1475 FE->getName() };
1476 return ikey;
1477}
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001478
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001479bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
1480 if (a.Size != b.Size || a.ModTime != b.ModTime)
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001481 return false;
1482
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001483 if (strcmp(a.Filename, b.Filename) == 0)
1484 return true;
1485
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001486 // Determine whether the actual files are equivalent.
Argyrios Kyrtzidis1c1508b2013-03-04 20:33:40 +00001487 FileManager &FileMgr = Reader.getFileManager();
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001488 const FileEntry *FEA = FileMgr.getFile(a.Filename);
1489 const FileEntry *FEB = FileMgr.getFile(b.Filename);
Argyrios Kyrtzidis1c1508b2013-03-04 20:33:40 +00001490 return (FEA && FEA == FEB);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001491}
1492
1493std::pair<unsigned, unsigned>
1494HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001495 using namespace llvm::support;
1496 unsigned KeyLen = (unsigned) endian::readNext<uint16_t, little, unaligned>(d);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001497 unsigned DataLen = (unsigned) *d++;
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001498 return std::make_pair(KeyLen, DataLen);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001499}
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001500
1501HeaderFileInfoTrait::internal_key_type
1502HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001503 using namespace llvm::support;
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001504 internal_key_type ikey;
Stephen Hines651f13c2014-04-23 16:59:28 -07001505 ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d));
1506 ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d));
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001507 ikey.Filename = (const char *)d;
1508 return ikey;
1509}
1510
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001511HeaderFileInfoTrait::data_type
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001512HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001513 unsigned DataLen) {
1514 const unsigned char *End = d + DataLen;
Stephen Hines651f13c2014-04-23 16:59:28 -07001515 using namespace llvm::support;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001516 HeaderFileInfo HFI;
1517 unsigned Flags = *d++;
Lawrence Crowlbc3f6282013-06-20 21:14:14 +00001518 HFI.HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>
1519 ((Flags >> 6) & 0x03);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001520 HFI.isImport = (Flags >> 5) & 0x01;
1521 HFI.isPragmaOnce = (Flags >> 4) & 0x01;
1522 HFI.DirInfo = (Flags >> 2) & 0x03;
1523 HFI.Resolved = (Flags >> 1) & 0x01;
1524 HFI.IndexHeaderMapHeader = Flags & 0x01;
Stephen Hines651f13c2014-04-23 16:59:28 -07001525 HFI.NumIncludes = endian::readNext<uint16_t, little, unaligned>(d);
1526 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(
1527 M, endian::readNext<uint32_t, little, unaligned>(d));
1528 if (unsigned FrameworkOffset =
1529 endian::readNext<uint32_t, little, unaligned>(d)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001530 // The framework offset is 1 greater than the actual offset,
1531 // since 0 is used as an indicator for "no framework name".
1532 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1533 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1534 }
1535
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001536 if (d != End) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001537 uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d);
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001538 if (LocalSMID) {
1539 // This header is part of a module. Associate it with the module to enable
1540 // implicit module import.
1541 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1542 Module *Mod = Reader.getSubmodule(GlobalSMID);
1543 HFI.isModuleHeader = true;
1544 FileManager &FileMgr = Reader.getFileManager();
1545 ModuleMap &ModMap =
1546 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
Lawrence Crowlbc3f6282013-06-20 21:14:14 +00001547 ModMap.addHeader(Mod, FileMgr.getFile(key.Filename), HFI.getHeaderRole());
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001548 }
1549 }
1550
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001551 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1552 (void)End;
1553
1554 // This HeaderFileInfo was externally loaded.
1555 HFI.External = true;
1556 return HFI;
1557}
1558
Stephen Hines651f13c2014-04-23 16:59:28 -07001559void
1560ASTReader::addPendingMacroFromModule(IdentifierInfo *II, ModuleFile *M,
1561 GlobalMacroID GMacID,
1562 llvm::ArrayRef<SubmoduleID> Overrides) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001563 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001564 SubmoduleID *OverrideData = nullptr;
Stephen Hines651f13c2014-04-23 16:59:28 -07001565 if (!Overrides.empty()) {
1566 OverrideData = new (Context) SubmoduleID[Overrides.size() + 1];
1567 OverrideData[0] = Overrides.size();
1568 for (unsigned I = 0; I != Overrides.size(); ++I)
1569 OverrideData[I + 1] = getGlobalSubmoduleID(*M, Overrides[I]);
1570 }
1571 PendingMacroIDs[II].push_back(PendingMacroInfo(M, GMacID, OverrideData));
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001572}
1573
1574void ASTReader::addPendingMacroFromPCH(IdentifierInfo *II,
1575 ModuleFile *M,
1576 uint64_t MacroDirectivesOffset) {
1577 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1578 PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001579}
1580
1581void ASTReader::ReadDefinedMacros() {
1582 // Note that we are loading defined macros.
1583 Deserializing Macros(this);
1584
1585 for (ModuleReverseIterator I = ModuleMgr.rbegin(),
1586 E = ModuleMgr.rend(); I != E; ++I) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001587 BitstreamCursor &MacroCursor = (*I)->MacroCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001588
1589 // If there was no preprocessor block, skip this file.
1590 if (!MacroCursor.getBitStreamReader())
1591 continue;
1592
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001593 BitstreamCursor Cursor = MacroCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001594 Cursor.JumpToBit((*I)->MacroStartOffset);
1595
1596 RecordData Record;
1597 while (true) {
Chris Lattner88bde502013-01-19 21:39:22 +00001598 llvm::BitstreamEntry E = Cursor.advanceSkippingSubblocks();
1599
1600 switch (E.Kind) {
1601 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1602 case llvm::BitstreamEntry::Error:
1603 Error("malformed block record in AST file");
1604 return;
1605 case llvm::BitstreamEntry::EndBlock:
1606 goto NextCursor;
1607
1608 case llvm::BitstreamEntry::Record:
Chris Lattner88bde502013-01-19 21:39:22 +00001609 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001610 switch (Cursor.readRecord(E.ID, Record)) {
Chris Lattner88bde502013-01-19 21:39:22 +00001611 default: // Default behavior: ignore.
1612 break;
1613
1614 case PP_MACRO_OBJECT_LIKE:
1615 case PP_MACRO_FUNCTION_LIKE:
1616 getLocalIdentifier(**I, Record[0]);
1617 break;
1618
1619 case PP_TOKEN:
1620 // Ignore tokens.
1621 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001622 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001623 break;
1624 }
1625 }
Chris Lattner88bde502013-01-19 21:39:22 +00001626 NextCursor: ;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001627 }
1628}
1629
1630namespace {
1631 /// \brief Visitor class used to look up identifirs in an AST file.
1632 class IdentifierLookupVisitor {
1633 StringRef Name;
1634 unsigned PriorGeneration;
Douglas Gregore1698072013-01-25 00:38:33 +00001635 unsigned &NumIdentifierLookups;
1636 unsigned &NumIdentifierLookupHits;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001637 IdentifierInfo *Found;
Douglas Gregore1698072013-01-25 00:38:33 +00001638
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001639 public:
Douglas Gregore1698072013-01-25 00:38:33 +00001640 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
1641 unsigned &NumIdentifierLookups,
1642 unsigned &NumIdentifierLookupHits)
Douglas Gregor188bdcd2013-01-25 23:32:03 +00001643 : Name(Name), PriorGeneration(PriorGeneration),
Douglas Gregore1698072013-01-25 00:38:33 +00001644 NumIdentifierLookups(NumIdentifierLookups),
1645 NumIdentifierLookupHits(NumIdentifierLookupHits),
1646 Found()
1647 {
1648 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001649
1650 static bool visit(ModuleFile &M, void *UserData) {
1651 IdentifierLookupVisitor *This
1652 = static_cast<IdentifierLookupVisitor *>(UserData);
1653
1654 // If we've already searched this module file, skip it now.
1655 if (M.Generation <= This->PriorGeneration)
1656 return true;
Douglas Gregor1a49d972013-01-25 01:03:03 +00001657
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001658 ASTIdentifierLookupTable *IdTable
1659 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
1660 if (!IdTable)
1661 return false;
1662
1663 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(),
1664 M, This->Found);
Douglas Gregore1698072013-01-25 00:38:33 +00001665 ++This->NumIdentifierLookups;
1666 ASTIdentifierLookupTable::iterator Pos = IdTable->find(This->Name,&Trait);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001667 if (Pos == IdTable->end())
1668 return false;
1669
1670 // Dereferencing the iterator has the effect of building the
1671 // IdentifierInfo node and populating it with the various
1672 // declarations it needs.
Douglas Gregore1698072013-01-25 00:38:33 +00001673 ++This->NumIdentifierLookupHits;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001674 This->Found = *Pos;
1675 return true;
1676 }
1677
1678 // \brief Retrieve the identifier info found within the module
1679 // files.
1680 IdentifierInfo *getIdentifierInfo() const { return Found; }
1681 };
1682}
1683
1684void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
1685 // Note that we are loading an identifier.
1686 Deserializing AnIdentifier(this);
1687
1688 unsigned PriorGeneration = 0;
1689 if (getContext().getLangOpts().Modules)
1690 PriorGeneration = IdentifierGeneration[&II];
Douglas Gregor1a49d972013-01-25 01:03:03 +00001691
1692 // If there is a global index, look there first to determine which modules
1693 // provably do not have any results for this identifier.
Douglas Gregor188bdcd2013-01-25 23:32:03 +00001694 GlobalModuleIndex::HitSet Hits;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001695 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
Douglas Gregor1a49d972013-01-25 01:03:03 +00001696 if (!loadGlobalIndex()) {
Douglas Gregor188bdcd2013-01-25 23:32:03 +00001697 if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
1698 HitsPtr = &Hits;
Douglas Gregor1a49d972013-01-25 01:03:03 +00001699 }
1700 }
1701
Douglas Gregor188bdcd2013-01-25 23:32:03 +00001702 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
Douglas Gregore1698072013-01-25 00:38:33 +00001703 NumIdentifierLookups,
1704 NumIdentifierLookupHits);
Douglas Gregor188bdcd2013-01-25 23:32:03 +00001705 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor, HitsPtr);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001706 markIdentifierUpToDate(&II);
1707}
1708
1709void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
1710 if (!II)
1711 return;
1712
1713 II->setOutOfDate(false);
1714
1715 // Update the generation for this identifier.
1716 if (getContext().getLangOpts().Modules)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001717 IdentifierGeneration[II] = getGeneration();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001718}
1719
Stephen Hines651f13c2014-04-23 16:59:28 -07001720struct ASTReader::ModuleMacroInfo {
1721 SubmoduleID SubModID;
1722 MacroInfo *MI;
1723 SubmoduleID *Overrides;
1724 // FIXME: Remove this.
1725 ModuleFile *F;
1726
1727 bool isDefine() const { return MI; }
1728
1729 SubmoduleID getSubmoduleID() const { return SubModID; }
1730
1731 llvm::ArrayRef<SubmoduleID> getOverriddenSubmodules() const {
1732 if (!Overrides)
1733 return llvm::ArrayRef<SubmoduleID>();
1734 return llvm::makeArrayRef(Overrides + 1, *Overrides);
1735 }
1736
1737 DefMacroDirective *import(Preprocessor &PP, SourceLocation ImportLoc) const {
1738 if (!MI)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001739 return nullptr;
Stephen Hines651f13c2014-04-23 16:59:28 -07001740 return PP.AllocateDefMacroDirective(MI, ImportLoc, /*isImported=*/true);
1741 }
1742};
1743
1744ASTReader::ModuleMacroInfo *
1745ASTReader::getModuleMacro(const PendingMacroInfo &PMInfo) {
1746 ModuleMacroInfo Info;
1747
1748 uint32_t ID = PMInfo.ModuleMacroData.MacID;
1749 if (ID & 1) {
1750 // Macro undefinition.
1751 Info.SubModID = getGlobalSubmoduleID(*PMInfo.M, ID >> 1);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001752 Info.MI = nullptr;
Stephen Hines651f13c2014-04-23 16:59:28 -07001753 } else {
1754 // Macro definition.
1755 GlobalMacroID GMacID = getGlobalMacroID(*PMInfo.M, ID >> 1);
1756 assert(GMacID);
1757
1758 // If this macro has already been loaded, don't do so again.
1759 // FIXME: This is highly dubious. Multiple macro definitions can have the
1760 // same MacroInfo (and hence the same GMacID) due to #pragma push_macro etc.
1761 if (MacrosLoaded[GMacID - NUM_PREDEF_MACRO_IDS])
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001762 return nullptr;
Stephen Hines651f13c2014-04-23 16:59:28 -07001763
1764 Info.MI = getMacro(GMacID);
1765 Info.SubModID = Info.MI->getOwningModuleID();
1766 }
1767 Info.Overrides = PMInfo.ModuleMacroData.Overrides;
1768 Info.F = PMInfo.M;
1769
1770 return new (Context) ModuleMacroInfo(Info);
1771}
1772
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001773void ASTReader::resolvePendingMacro(IdentifierInfo *II,
1774 const PendingMacroInfo &PMInfo) {
1775 assert(II);
1776
1777 if (PMInfo.M->Kind != MK_Module) {
1778 installPCHMacroDirectives(II, *PMInfo.M,
1779 PMInfo.PCHMacroData.MacroDirectivesOffset);
1780 return;
1781 }
Stephen Hines651f13c2014-04-23 16:59:28 -07001782
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001783 // Module Macro.
1784
Stephen Hines651f13c2014-04-23 16:59:28 -07001785 ModuleMacroInfo *MMI = getModuleMacro(PMInfo);
1786 if (!MMI)
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001787 return;
1788
Stephen Hines651f13c2014-04-23 16:59:28 -07001789 Module *Owner = getSubmodule(MMI->getSubmoduleID());
1790 if (Owner && Owner->NameVisibility == Module::Hidden) {
1791 // Macros in the owning module are hidden. Just remember this macro to
1792 // install if we make this module visible.
1793 HiddenNamesMap[Owner].HiddenMacros.insert(std::make_pair(II, MMI));
1794 } else {
1795 installImportedMacro(II, MMI, Owner);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001796 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001797}
1798
1799void ASTReader::installPCHMacroDirectives(IdentifierInfo *II,
1800 ModuleFile &M, uint64_t Offset) {
1801 assert(M.Kind != MK_Module);
1802
1803 BitstreamCursor &Cursor = M.MacroCursor;
1804 SavedStreamPosition SavedPosition(Cursor);
1805 Cursor.JumpToBit(Offset);
1806
1807 llvm::BitstreamEntry Entry =
1808 Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
1809 if (Entry.Kind != llvm::BitstreamEntry::Record) {
1810 Error("malformed block record in AST file");
1811 return;
1812 }
1813
1814 RecordData Record;
1815 PreprocessorRecordTypes RecType =
1816 (PreprocessorRecordTypes)Cursor.readRecord(Entry.ID, Record);
1817 if (RecType != PP_MACRO_DIRECTIVE_HISTORY) {
1818 Error("malformed block record in AST file");
1819 return;
1820 }
1821
1822 // Deserialize the macro directives history in reverse source-order.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001823 MacroDirective *Latest = nullptr, *Earliest = nullptr;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001824 unsigned Idx = 0, N = Record.size();
1825 while (Idx < N) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001826 MacroDirective *MD = nullptr;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001827 SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001828 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
1829 switch (K) {
1830 case MacroDirective::MD_Define: {
1831 GlobalMacroID GMacID = getGlobalMacroID(M, Record[Idx++]);
1832 MacroInfo *MI = getMacro(GMacID);
1833 bool isImported = Record[Idx++];
1834 bool isAmbiguous = Record[Idx++];
1835 DefMacroDirective *DefMD =
1836 PP.AllocateDefMacroDirective(MI, Loc, isImported);
1837 DefMD->setAmbiguous(isAmbiguous);
1838 MD = DefMD;
1839 break;
1840 }
1841 case MacroDirective::MD_Undefine:
1842 MD = PP.AllocateUndefMacroDirective(Loc);
1843 break;
1844 case MacroDirective::MD_Visibility: {
1845 bool isPublic = Record[Idx++];
1846 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
1847 break;
1848 }
1849 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001850
1851 if (!Latest)
1852 Latest = MD;
1853 if (Earliest)
1854 Earliest->setPrevious(MD);
1855 Earliest = MD;
1856 }
1857
1858 PP.setLoadedMacroDirective(II, Latest);
1859}
1860
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00001861/// \brief For the given macro definitions, check if they are both in system
Douglas Gregor7332ae42013-04-12 21:00:54 +00001862/// modules.
1863static bool areDefinedInSystemModules(MacroInfo *PrevMI, MacroInfo *NewMI,
Douglas Gregorf9dbae72013-06-07 22:56:11 +00001864 Module *NewOwner, ASTReader &Reader) {
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00001865 assert(PrevMI && NewMI);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001866 Module *PrevOwner = nullptr;
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00001867 if (SubmoduleID PrevModID = PrevMI->getOwningModuleID())
1868 PrevOwner = Reader.getSubmodule(PrevModID);
Douglas Gregorf9dbae72013-06-07 22:56:11 +00001869 SourceManager &SrcMgr = Reader.getSourceManager();
1870 bool PrevInSystem
1871 = PrevOwner? PrevOwner->IsSystem
1872 : SrcMgr.isInSystemHeader(PrevMI->getDefinitionLoc());
1873 bool NewInSystem
1874 = NewOwner? NewOwner->IsSystem
1875 : SrcMgr.isInSystemHeader(NewMI->getDefinitionLoc());
1876 if (PrevOwner && PrevOwner == NewOwner)
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00001877 return false;
Douglas Gregorf9dbae72013-06-07 22:56:11 +00001878 return PrevInSystem && NewInSystem;
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00001879}
1880
Stephen Hines651f13c2014-04-23 16:59:28 -07001881void ASTReader::removeOverriddenMacros(IdentifierInfo *II,
1882 AmbiguousMacros &Ambig,
1883 llvm::ArrayRef<SubmoduleID> Overrides) {
1884 for (unsigned OI = 0, ON = Overrides.size(); OI != ON; ++OI) {
1885 SubmoduleID OwnerID = Overrides[OI];
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001886
Stephen Hines651f13c2014-04-23 16:59:28 -07001887 // If this macro is not yet visible, remove it from the hidden names list.
1888 Module *Owner = getSubmodule(OwnerID);
1889 HiddenNames &Hidden = HiddenNamesMap[Owner];
1890 HiddenMacrosMap::iterator HI = Hidden.HiddenMacros.find(II);
1891 if (HI != Hidden.HiddenMacros.end()) {
1892 auto SubOverrides = HI->second->getOverriddenSubmodules();
1893 Hidden.HiddenMacros.erase(HI);
1894 removeOverriddenMacros(II, Ambig, SubOverrides);
1895 }
1896
1897 // If this macro is already in our list of conflicts, remove it from there.
1898 Ambig.erase(
1899 std::remove_if(Ambig.begin(), Ambig.end(), [&](DefMacroDirective *MD) {
1900 return MD->getInfo()->getOwningModuleID() == OwnerID;
1901 }),
1902 Ambig.end());
1903 }
1904}
1905
1906ASTReader::AmbiguousMacros *
1907ASTReader::removeOverriddenMacros(IdentifierInfo *II,
1908 llvm::ArrayRef<SubmoduleID> Overrides) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001909 MacroDirective *Prev = PP.getMacroDirective(II);
Stephen Hines651f13c2014-04-23 16:59:28 -07001910 if (!Prev && Overrides.empty())
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001911 return nullptr;
Stephen Hines651f13c2014-04-23 16:59:28 -07001912
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001913 DefMacroDirective *PrevDef = Prev ? Prev->getDefinition().getDirective()
1914 : nullptr;
Stephen Hines651f13c2014-04-23 16:59:28 -07001915 if (PrevDef && PrevDef->isAmbiguous()) {
1916 // We had a prior ambiguity. Check whether we resolve it (or make it worse).
1917 AmbiguousMacros &Ambig = AmbiguousMacroDefs[II];
1918 Ambig.push_back(PrevDef);
1919
1920 removeOverriddenMacros(II, Ambig, Overrides);
1921
1922 if (!Ambig.empty())
1923 return &Ambig;
1924
1925 AmbiguousMacroDefs.erase(II);
1926 } else {
1927 // There's no ambiguity yet. Maybe we're introducing one.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001928 AmbiguousMacros Ambig;
Stephen Hines651f13c2014-04-23 16:59:28 -07001929 if (PrevDef)
1930 Ambig.push_back(PrevDef);
1931
1932 removeOverriddenMacros(II, Ambig, Overrides);
1933
1934 if (!Ambig.empty()) {
1935 AmbiguousMacros &Result = AmbiguousMacroDefs[II];
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001936 std::swap(Result, Ambig);
Stephen Hines651f13c2014-04-23 16:59:28 -07001937 return &Result;
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001938 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001939 }
Stephen Hines651f13c2014-04-23 16:59:28 -07001940
1941 // We ended up with no ambiguity.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001942 return nullptr;
Stephen Hines651f13c2014-04-23 16:59:28 -07001943}
1944
1945void ASTReader::installImportedMacro(IdentifierInfo *II, ModuleMacroInfo *MMI,
1946 Module *Owner) {
1947 assert(II && Owner);
1948
1949 SourceLocation ImportLoc = Owner->MacroVisibilityLoc;
1950 if (ImportLoc.isInvalid()) {
1951 // FIXME: If we made macros from this module visible but didn't provide a
1952 // source location for the import, we don't have a location for the macro.
1953 // Use the location at which the containing module file was first imported
1954 // for now.
1955 ImportLoc = MMI->F->DirectImportLoc;
1956 assert(ImportLoc.isValid() && "no import location for a visible macro?");
1957 }
1958
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001959 AmbiguousMacros *Prev =
Stephen Hines651f13c2014-04-23 16:59:28 -07001960 removeOverriddenMacros(II, MMI->getOverriddenSubmodules());
1961
Stephen Hines651f13c2014-04-23 16:59:28 -07001962 // Create a synthetic macro definition corresponding to the import (or null
1963 // if this was an undefinition of the macro).
1964 DefMacroDirective *MD = MMI->import(PP, ImportLoc);
1965
1966 // If there's no ambiguity, just install the macro.
1967 if (!Prev) {
1968 if (MD)
1969 PP.appendMacroDirective(II, MD);
1970 else
1971 PP.appendMacroDirective(II, PP.AllocateUndefMacroDirective(ImportLoc));
1972 return;
1973 }
1974 assert(!Prev->empty());
1975
1976 if (!MD) {
1977 // We imported a #undef that didn't remove all prior definitions. The most
1978 // recent prior definition remains, and we install it in the place of the
1979 // imported directive.
1980 MacroInfo *NewMI = Prev->back()->getInfo();
1981 Prev->pop_back();
1982 MD = PP.AllocateDefMacroDirective(NewMI, ImportLoc, /*Imported*/true);
1983 }
1984
1985 // We're introducing a macro definition that creates or adds to an ambiguity.
1986 // We can resolve that ambiguity if this macro is token-for-token identical to
1987 // all of the existing definitions.
1988 MacroInfo *NewMI = MD->getInfo();
1989 assert(NewMI && "macro definition with no MacroInfo?");
1990 while (!Prev->empty()) {
1991 MacroInfo *PrevMI = Prev->back()->getInfo();
1992 assert(PrevMI && "macro definition with no MacroInfo?");
1993
1994 // Before marking the macros as ambiguous, check if this is a case where
1995 // both macros are in system headers. If so, we trust that the system
1996 // did not get it wrong. This also handles cases where Clang's own
1997 // headers have a different spelling of certain system macros:
1998 // #define LONG_MAX __LONG_MAX__ (clang's limits.h)
1999 // #define LONG_MAX 0x7fffffffffffffffL (system's limits.h)
2000 //
2001 // FIXME: Remove the defined-in-system-headers check. clang's limits.h
2002 // overrides the system limits.h's macros, so there's no conflict here.
2003 if (NewMI != PrevMI &&
2004 !PrevMI->isIdenticalTo(*NewMI, PP, /*Syntactically=*/true) &&
2005 !areDefinedInSystemModules(PrevMI, NewMI, Owner, *this))
2006 break;
2007
2008 // The previous definition is the same as this one (or both are defined in
2009 // system modules so we can assume they're equivalent); we don't need to
2010 // track it any more.
2011 Prev->pop_back();
2012 }
2013
2014 if (!Prev->empty())
2015 MD->setAmbiguous(true);
2016
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002017 PP.appendMacroDirective(II, MD);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002018}
2019
Stephen Hines651f13c2014-04-23 16:59:28 -07002020ASTReader::InputFileInfo
2021ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) {
2022 // Go find this input file.
2023 BitstreamCursor &Cursor = F.InputFilesCursor;
2024 SavedStreamPosition SavedPosition(Cursor);
2025 Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
2026
2027 unsigned Code = Cursor.ReadCode();
2028 RecordData Record;
2029 StringRef Blob;
2030
2031 unsigned Result = Cursor.readRecord(Code, Record, &Blob);
2032 assert(static_cast<InputFileRecordTypes>(Result) == INPUT_FILE &&
2033 "invalid record type for input file");
2034 (void)Result;
2035
2036 std::string Filename;
2037 off_t StoredSize;
2038 time_t StoredTime;
2039 bool Overridden;
2040
2041 assert(Record[0] == ID && "Bogus stored ID or offset");
2042 StoredSize = static_cast<off_t>(Record[1]);
2043 StoredTime = static_cast<time_t>(Record[2]);
2044 Overridden = static_cast<bool>(Record[3]);
2045 Filename = Blob;
2046 MaybeAddSystemRootToFilename(F, Filename);
2047
2048 InputFileInfo R = { std::move(Filename), StoredSize, StoredTime, Overridden };
2049 return R;
2050}
2051
2052std::string ASTReader::getInputFileName(ModuleFile &F, unsigned int ID) {
2053 return readInputFileInfo(F, ID).Filename;
2054}
2055
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00002056InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002057 // If this ID is bogus, just return an empty input file.
2058 if (ID == 0 || ID > F.InputFilesLoaded.size())
2059 return InputFile();
2060
2061 // If we've already loaded this input file, return it.
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00002062 if (F.InputFilesLoaded[ID-1].getFile())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002063 return F.InputFilesLoaded[ID-1];
2064
Stephen Hines651f13c2014-04-23 16:59:28 -07002065 if (F.InputFilesLoaded[ID-1].isNotFound())
2066 return InputFile();
2067
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002068 // Go find this input file.
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00002069 BitstreamCursor &Cursor = F.InputFilesCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002070 SavedStreamPosition SavedPosition(Cursor);
2071 Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
2072
Stephen Hines651f13c2014-04-23 16:59:28 -07002073 InputFileInfo FI = readInputFileInfo(F, ID);
2074 off_t StoredSize = FI.StoredSize;
2075 time_t StoredTime = FI.StoredTime;
2076 bool Overridden = FI.Overridden;
2077 StringRef Filename = FI.Filename;
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00002078
Stephen Hines651f13c2014-04-23 16:59:28 -07002079 const FileEntry *File
2080 = Overridden? FileMgr.getVirtualFile(Filename, StoredSize, StoredTime)
2081 : FileMgr.getFile(Filename, /*OpenFile=*/false);
2082
2083 // If we didn't find the file, resolve it relative to the
2084 // original directory from which this AST file was created.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002085 if (File == nullptr && !F.OriginalDir.empty() && !CurrentDir.empty() &&
Stephen Hines651f13c2014-04-23 16:59:28 -07002086 F.OriginalDir != CurrentDir) {
2087 std::string Resolved = resolveFileRelativeToOriginalDir(Filename,
2088 F.OriginalDir,
2089 CurrentDir);
2090 if (!Resolved.empty())
2091 File = FileMgr.getFile(Resolved);
2092 }
2093
2094 // For an overridden file, create a virtual file with the stored
2095 // size/timestamp.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002096 if (Overridden && File == nullptr) {
Stephen Hines651f13c2014-04-23 16:59:28 -07002097 File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
2098 }
2099
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002100 if (File == nullptr) {
Stephen Hines651f13c2014-04-23 16:59:28 -07002101 if (Complain) {
2102 std::string ErrorStr = "could not find file '";
2103 ErrorStr += Filename;
2104 ErrorStr += "' referenced by AST file";
2105 Error(ErrorStr.c_str());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002106 }
Stephen Hines651f13c2014-04-23 16:59:28 -07002107 // Record that we didn't find the file.
2108 F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
2109 return InputFile();
2110 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002111
Stephen Hines651f13c2014-04-23 16:59:28 -07002112 // Check if there was a request to override the contents of the file
2113 // that was part of the precompiled header. Overridding such a file
2114 // can lead to problems when lexing using the source locations from the
2115 // PCH.
2116 SourceManager &SM = getSourceManager();
2117 if (!Overridden && SM.isFileOverridden(File)) {
2118 if (Complain)
2119 Error(diag::err_fe_pch_file_overridden, Filename);
2120 // After emitting the diagnostic, recover by disabling the override so
2121 // that the original file will be used.
2122 SM.disableFileContentsOverride(File);
2123 // The FileEntry is a virtual file entry with the size of the contents
2124 // that would override the original contents. Set it to the original's
2125 // size/time.
2126 FileMgr.modifyFileEntry(const_cast<FileEntry*>(File),
2127 StoredSize, StoredTime);
2128 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002129
Stephen Hines651f13c2014-04-23 16:59:28 -07002130 bool IsOutOfDate = false;
2131
2132 // For an overridden file, there is nothing to validate.
2133 if (!Overridden && (StoredSize != File->getSize()
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002134#if !defined(LLVM_ON_WIN32)
Stephen Hines651f13c2014-04-23 16:59:28 -07002135 // In our regression testing, the Windows file system seems to
2136 // have inconsistent modification times that sometimes
2137 // erroneously trigger this error-handling path.
2138 || StoredTime != File->getModificationTime()
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002139#endif
Stephen Hines651f13c2014-04-23 16:59:28 -07002140 )) {
2141 if (Complain) {
2142 // Build a list of the PCH imports that got us here (in reverse).
2143 SmallVector<ModuleFile *, 4> ImportStack(1, &F);
2144 while (ImportStack.back()->ImportedBy.size() > 0)
2145 ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
2146
2147 // The top-level PCH is stale.
2148 StringRef TopLevelPCHName(ImportStack.back()->FileName);
2149 Error(diag::err_fe_pch_file_modified, Filename, TopLevelPCHName);
2150
2151 // Print the import stack.
2152 if (ImportStack.size() > 1 && !Diags.isDiagnosticInFlight()) {
2153 Diag(diag::note_pch_required_by)
2154 << Filename << ImportStack[0]->FileName;
2155 for (unsigned I = 1; I < ImportStack.size(); ++I)
2156 Diag(diag::note_pch_required_by)
2157 << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
Douglas Gregor677e15f2013-03-19 00:28:20 +00002158 }
2159
Stephen Hines651f13c2014-04-23 16:59:28 -07002160 if (!Diags.isDiagnosticInFlight())
2161 Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002162 }
2163
Stephen Hines651f13c2014-04-23 16:59:28 -07002164 IsOutOfDate = true;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002165 }
2166
Stephen Hines651f13c2014-04-23 16:59:28 -07002167 InputFile IF = InputFile(File, Overridden, IsOutOfDate);
2168
2169 // Note that we've loaded this input file.
2170 F.InputFilesLoaded[ID-1] = IF;
2171 return IF;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002172}
2173
2174const FileEntry *ASTReader::getFileEntry(StringRef filenameStrRef) {
2175 ModuleFile &M = ModuleMgr.getPrimaryModule();
2176 std::string Filename = filenameStrRef;
2177 MaybeAddSystemRootToFilename(M, Filename);
2178 const FileEntry *File = FileMgr.getFile(Filename);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002179 if (File == nullptr && !M.OriginalDir.empty() && !CurrentDir.empty() &&
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002180 M.OriginalDir != CurrentDir) {
2181 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
2182 M.OriginalDir,
2183 CurrentDir);
2184 if (!resolved.empty())
2185 File = FileMgr.getFile(resolved);
2186 }
2187
2188 return File;
2189}
2190
2191/// \brief If we are loading a relocatable PCH file, and the filename is
2192/// not an absolute path, add the system root to the beginning of the file
2193/// name.
2194void ASTReader::MaybeAddSystemRootToFilename(ModuleFile &M,
2195 std::string &Filename) {
2196 // If this is not a relocatable PCH file, there's nothing to do.
2197 if (!M.RelocatablePCH)
2198 return;
2199
2200 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
2201 return;
2202
2203 if (isysroot.empty()) {
2204 // If no system root was given, default to '/'
2205 Filename.insert(Filename.begin(), '/');
2206 return;
2207 }
2208
2209 unsigned Length = isysroot.size();
2210 if (isysroot[Length - 1] != '/')
2211 Filename.insert(Filename.begin(), '/');
2212
2213 Filename.insert(Filename.begin(), isysroot.begin(), isysroot.end());
2214}
2215
2216ASTReader::ASTReadResult
2217ASTReader::ReadControlBlock(ModuleFile &F,
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002218 SmallVectorImpl<ImportedModule> &Loaded,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002219 const ModuleFile *ImportedBy,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002220 unsigned ClientLoadCapabilities) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00002221 BitstreamCursor &Stream = F.Stream;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002222
2223 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
2224 Error("malformed block record in AST file");
2225 return Failure;
2226 }
2227
2228 // Read all of the records and blocks in the control block.
2229 RecordData Record;
Chris Lattner88bde502013-01-19 21:39:22 +00002230 while (1) {
2231 llvm::BitstreamEntry Entry = Stream.advance();
2232
2233 switch (Entry.Kind) {
2234 case llvm::BitstreamEntry::Error:
2235 Error("malformed block record in AST file");
2236 return Failure;
Stephen Hines651f13c2014-04-23 16:59:28 -07002237 case llvm::BitstreamEntry::EndBlock: {
2238 // Validate input files.
2239 const HeaderSearchOptions &HSOpts =
2240 PP.getHeaderSearchInfo().getHeaderSearchOpts();
2241
2242 // All user input files reside at the index range [0, Record[1]), and
2243 // system input files reside at [Record[1], Record[0]).
2244 // Record is the one from INPUT_FILE_OFFSETS.
2245 unsigned NumInputs = Record[0];
2246 unsigned NumUserInputs = Record[1];
2247
2248 if (!DisableValidation &&
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002249 (ValidateSystemInputs || !HSOpts.ModulesValidateOncePerBuildSession ||
Stephen Hines651f13c2014-04-23 16:59:28 -07002250 F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002251 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
Stephen Hines651f13c2014-04-23 16:59:28 -07002252
2253 // If we are reading a module, we will create a verification timestamp,
2254 // so we verify all input files. Otherwise, verify only user input
2255 // files.
2256
2257 unsigned N = NumUserInputs;
2258 if (ValidateSystemInputs ||
2259 (HSOpts.ModulesValidateOncePerBuildSession && F.Kind == MK_Module))
2260 N = NumInputs;
2261
2262 for (unsigned I = 0; I < N; ++I) {
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00002263 InputFile IF = getInputFile(F, I+1, Complain);
2264 if (!IF.getFile() || IF.isOutOfDate())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002265 return OutOfDate;
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00002266 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002267 }
Stephen Hines651f13c2014-04-23 16:59:28 -07002268
2269 if (Listener)
2270 Listener->visitModuleFile(F.FileName);
2271
2272 if (Listener && Listener->needsInputFileVisitation()) {
2273 unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
2274 : NumUserInputs;
2275 for (unsigned I = 0; I < N; ++I) {
2276 bool IsSystem = I >= NumUserInputs;
2277 InputFileInfo FI = readInputFileInfo(F, I+1);
2278 Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden);
2279 }
2280 }
2281
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002282 return Success;
Stephen Hines651f13c2014-04-23 16:59:28 -07002283 }
2284
Chris Lattner88bde502013-01-19 21:39:22 +00002285 case llvm::BitstreamEntry::SubBlock:
2286 switch (Entry.ID) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002287 case INPUT_FILES_BLOCK_ID:
2288 F.InputFilesCursor = Stream;
2289 if (Stream.SkipBlock() || // Skip with the main cursor
2290 // Read the abbreviations
2291 ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
2292 Error("malformed block record in AST file");
2293 return Failure;
2294 }
2295 continue;
Chris Lattner88bde502013-01-19 21:39:22 +00002296
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002297 default:
Chris Lattner88bde502013-01-19 21:39:22 +00002298 if (Stream.SkipBlock()) {
2299 Error("malformed block record in AST file");
2300 return Failure;
2301 }
2302 continue;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002303 }
Chris Lattner88bde502013-01-19 21:39:22 +00002304
2305 case llvm::BitstreamEntry::Record:
2306 // The interesting case.
2307 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002308 }
2309
2310 // Read and process a record.
2311 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002312 StringRef Blob;
2313 switch ((ControlRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002314 case METADATA: {
2315 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2316 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
Stephen Hines651f13c2014-04-23 16:59:28 -07002317 Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
2318 : diag::err_pch_version_too_new);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002319 return VersionMismatch;
2320 }
2321
2322 bool hasErrors = Record[5];
2323 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
2324 Diag(diag::err_pch_with_compiler_errors);
2325 return HadErrors;
2326 }
2327
2328 F.RelocatablePCH = Record[4];
2329
2330 const std::string &CurBranch = getClangFullRepositoryVersion();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002331 StringRef ASTBranch = Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002332 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2333 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
Stephen Hines651f13c2014-04-23 16:59:28 -07002334 Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002335 return VersionMismatch;
2336 }
2337 break;
2338 }
2339
2340 case IMPORTS: {
2341 // Load each of the imported PCH files.
2342 unsigned Idx = 0, N = Record.size();
2343 while (Idx < N) {
2344 // Read information about the AST file.
2345 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
2346 // The import location will be the local one for now; we will adjust
2347 // all import locations of module imports after the global source
2348 // location info are setup.
2349 SourceLocation ImportLoc =
2350 SourceLocation::getFromRawEncoding(Record[Idx++]);
Douglas Gregor677e15f2013-03-19 00:28:20 +00002351 off_t StoredSize = (off_t)Record[Idx++];
2352 time_t StoredModTime = (time_t)Record[Idx++];
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002353 unsigned Length = Record[Idx++];
2354 SmallString<128> ImportedFile(Record.begin() + Idx,
2355 Record.begin() + Idx + Length);
2356 Idx += Length;
2357
2358 // Load the AST file.
2359 switch(ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F, Loaded,
Douglas Gregor677e15f2013-03-19 00:28:20 +00002360 StoredSize, StoredModTime,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002361 ClientLoadCapabilities)) {
2362 case Failure: return Failure;
2363 // If we have to ignore the dependency, we'll have to ignore this too.
Douglas Gregorac39f132013-03-19 00:38:50 +00002364 case Missing:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002365 case OutOfDate: return OutOfDate;
2366 case VersionMismatch: return VersionMismatch;
2367 case ConfigurationMismatch: return ConfigurationMismatch;
2368 case HadErrors: return HadErrors;
2369 case Success: break;
2370 }
2371 }
2372 break;
2373 }
2374
2375 case LANGUAGE_OPTIONS: {
2376 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2377 if (Listener && &F == *ModuleMgr.begin() &&
2378 ParseLanguageOptions(Record, Complain, *Listener) &&
Stephen Hines651f13c2014-04-23 16:59:28 -07002379 !DisableValidation && !AllowConfigurationMismatch)
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002380 return ConfigurationMismatch;
2381 break;
2382 }
2383
2384 case TARGET_OPTIONS: {
2385 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2386 if (Listener && &F == *ModuleMgr.begin() &&
2387 ParseTargetOptions(Record, Complain, *Listener) &&
Stephen Hines651f13c2014-04-23 16:59:28 -07002388 !DisableValidation && !AllowConfigurationMismatch)
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002389 return ConfigurationMismatch;
2390 break;
2391 }
2392
2393 case DIAGNOSTIC_OPTIONS: {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002394 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate)==0;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002395 if (Listener && &F == *ModuleMgr.begin() &&
2396 ParseDiagnosticOptions(Record, Complain, *Listener) &&
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002397 !DisableValidation)
2398 return OutOfDate;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002399 break;
2400 }
2401
2402 case FILE_SYSTEM_OPTIONS: {
2403 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2404 if (Listener && &F == *ModuleMgr.begin() &&
2405 ParseFileSystemOptions(Record, Complain, *Listener) &&
Stephen Hines651f13c2014-04-23 16:59:28 -07002406 !DisableValidation && !AllowConfigurationMismatch)
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002407 return ConfigurationMismatch;
2408 break;
2409 }
2410
2411 case HEADER_SEARCH_OPTIONS: {
2412 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2413 if (Listener && &F == *ModuleMgr.begin() &&
2414 ParseHeaderSearchOptions(Record, Complain, *Listener) &&
Stephen Hines651f13c2014-04-23 16:59:28 -07002415 !DisableValidation && !AllowConfigurationMismatch)
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002416 return ConfigurationMismatch;
2417 break;
2418 }
2419
2420 case PREPROCESSOR_OPTIONS: {
2421 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2422 if (Listener && &F == *ModuleMgr.begin() &&
2423 ParsePreprocessorOptions(Record, Complain, *Listener,
2424 SuggestedPredefines) &&
Stephen Hines651f13c2014-04-23 16:59:28 -07002425 !DisableValidation && !AllowConfigurationMismatch)
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002426 return ConfigurationMismatch;
2427 break;
2428 }
2429
2430 case ORIGINAL_FILE:
2431 F.OriginalSourceFileID = FileID::get(Record[0]);
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002432 F.ActualOriginalSourceFileName = Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002433 F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
2434 MaybeAddSystemRootToFilename(F, F.OriginalSourceFileName);
2435 break;
2436
2437 case ORIGINAL_FILE_ID:
2438 F.OriginalSourceFileID = FileID::get(Record[0]);
2439 break;
2440
2441 case ORIGINAL_PCH_DIR:
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002442 F.OriginalDir = Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002443 break;
2444
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002445 case MODULE_NAME:
2446 F.ModuleName = Blob;
2447 if (Listener)
2448 Listener->ReadModuleName(F.ModuleName);
2449 break;
2450
2451 case MODULE_MAP_FILE:
2452 F.ModuleMapPath = Blob;
2453
2454 // Try to resolve ModuleName in the current header search context and
2455 // verify that it is found in the same module map file as we saved. If the
2456 // top-level AST file is a main file, skip this check because there is no
2457 // usable header search context.
2458 assert(!F.ModuleName.empty() &&
2459 "MODULE_NAME should come before MOUDLE_MAP_FILE");
2460 if (F.Kind == MK_Module &&
2461 (*ModuleMgr.begin())->Kind != MK_MainFile) {
2462 Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
2463 if (!M) {
2464 assert(ImportedBy && "top-level import should be verified");
2465 if ((ClientLoadCapabilities & ARR_Missing) == 0)
2466 Diag(diag::err_imported_module_not_found)
2467 << F.ModuleName << ImportedBy->FileName;
2468 return Missing;
2469 }
2470
2471 const FileEntry *StoredModMap = FileMgr.getFile(F.ModuleMapPath);
2472 if (StoredModMap == nullptr || StoredModMap != M->ModuleMap) {
2473 assert(M->ModuleMap && "found module is missing module map file");
2474 assert(M->Name == F.ModuleName && "found module with different name");
2475 assert(ImportedBy && "top-level import should be verified");
2476 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2477 Diag(diag::err_imported_module_modmap_changed)
2478 << F.ModuleName << ImportedBy->FileName
2479 << M->ModuleMap->getName() << F.ModuleMapPath;
2480 return OutOfDate;
2481 }
2482 }
2483
2484 if (Listener)
2485 Listener->ReadModuleMapFile(F.ModuleMapPath);
2486 break;
2487
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002488 case INPUT_FILE_OFFSETS:
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002489 F.InputFileOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002490 F.InputFilesLoaded.resize(Record[0]);
2491 break;
2492 }
2493 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002494}
2495
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002496ASTReader::ASTReadResult
2497ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00002498 BitstreamCursor &Stream = F.Stream;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002499
2500 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
2501 Error("malformed block record in AST file");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002502 return Failure;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002503 }
2504
2505 // Read all of the records and blocks for the AST file.
2506 RecordData Record;
Chris Lattner88bde502013-01-19 21:39:22 +00002507 while (1) {
2508 llvm::BitstreamEntry Entry = Stream.advance();
2509
2510 switch (Entry.Kind) {
2511 case llvm::BitstreamEntry::Error:
2512 Error("error at end of module block in AST file");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002513 return Failure;
Chris Lattner88bde502013-01-19 21:39:22 +00002514 case llvm::BitstreamEntry::EndBlock: {
Richard Smith43828672013-04-03 22:49:41 +00002515 // Outside of C++, we do not store a lookup map for the translation unit.
2516 // Instead, mark it as needing a lookup map to be built if this module
2517 // contains any declarations lexically within it (which it always does!).
2518 // This usually has no cost, since we very rarely need the lookup map for
2519 // the translation unit outside C++.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002520 DeclContext *DC = Context.getTranslationUnitDecl();
Richard Smith43828672013-04-03 22:49:41 +00002521 if (DC->hasExternalLexicalStorage() &&
2522 !getContext().getLangOpts().CPlusPlus)
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002523 DC->setMustBuildLookupTable();
Chris Lattner88bde502013-01-19 21:39:22 +00002524
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002525 return Success;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002526 }
Chris Lattner88bde502013-01-19 21:39:22 +00002527 case llvm::BitstreamEntry::SubBlock:
2528 switch (Entry.ID) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002529 case DECLTYPES_BLOCK_ID:
2530 // We lazily load the decls block, but we want to set up the
2531 // DeclsCursor cursor to point into it. Clone our current bitcode
2532 // cursor to it, enter the block and read the abbrevs in that block.
2533 // With the main cursor, we just skip over it.
2534 F.DeclsCursor = Stream;
2535 if (Stream.SkipBlock() || // Skip with the main cursor.
2536 // Read the abbrevs.
2537 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
2538 Error("malformed block record in AST file");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002539 return Failure;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002540 }
2541 break;
Stephen Hines651f13c2014-04-23 16:59:28 -07002542
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002543 case PREPROCESSOR_BLOCK_ID:
2544 F.MacroCursor = Stream;
2545 if (!PP.getExternalSource())
2546 PP.setExternalSource(this);
Chris Lattner88bde502013-01-19 21:39:22 +00002547
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002548 if (Stream.SkipBlock() ||
2549 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
2550 Error("malformed block record in AST file");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002551 return Failure;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002552 }
2553 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
2554 break;
Chris Lattner88bde502013-01-19 21:39:22 +00002555
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002556 case PREPROCESSOR_DETAIL_BLOCK_ID:
2557 F.PreprocessorDetailCursor = Stream;
2558 if (Stream.SkipBlock() ||
Chris Lattner88bde502013-01-19 21:39:22 +00002559 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002560 PREPROCESSOR_DETAIL_BLOCK_ID)) {
Chris Lattner88bde502013-01-19 21:39:22 +00002561 Error("malformed preprocessor detail record in AST file");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002562 return Failure;
Chris Lattner88bde502013-01-19 21:39:22 +00002563 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002564 F.PreprocessorDetailStartOffset
Chris Lattner88bde502013-01-19 21:39:22 +00002565 = F.PreprocessorDetailCursor.GetCurrentBitNo();
2566
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002567 if (!PP.getPreprocessingRecord())
2568 PP.createPreprocessingRecord();
2569 if (!PP.getPreprocessingRecord()->getExternalSource())
2570 PP.getPreprocessingRecord()->SetExternalSource(*this);
2571 break;
2572
2573 case SOURCE_MANAGER_BLOCK_ID:
2574 if (ReadSourceManagerBlock(F))
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002575 return Failure;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002576 break;
Chris Lattner88bde502013-01-19 21:39:22 +00002577
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002578 case SUBMODULE_BLOCK_ID:
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002579 if (ASTReadResult Result = ReadSubmoduleBlock(F, ClientLoadCapabilities))
2580 return Result;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002581 break;
Chris Lattner88bde502013-01-19 21:39:22 +00002582
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002583 case COMMENTS_BLOCK_ID: {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00002584 BitstreamCursor C = Stream;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002585 if (Stream.SkipBlock() ||
2586 ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
2587 Error("malformed comments block in AST file");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002588 return Failure;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002589 }
2590 CommentsCursors.push_back(std::make_pair(C, &F));
2591 break;
2592 }
Chris Lattner88bde502013-01-19 21:39:22 +00002593
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002594 default:
Chris Lattner88bde502013-01-19 21:39:22 +00002595 if (Stream.SkipBlock()) {
2596 Error("malformed block record in AST file");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002597 return Failure;
Chris Lattner88bde502013-01-19 21:39:22 +00002598 }
2599 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002600 }
2601 continue;
Chris Lattner88bde502013-01-19 21:39:22 +00002602
2603 case llvm::BitstreamEntry::Record:
2604 // The interesting case.
2605 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002606 }
2607
2608 // Read and process a record.
2609 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002610 StringRef Blob;
2611 switch ((ASTRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002612 default: // Default behavior: ignore.
2613 break;
2614
2615 case TYPE_OFFSET: {
2616 if (F.LocalNumTypes != 0) {
2617 Error("duplicate TYPE_OFFSET record in AST file");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002618 return Failure;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002619 }
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002620 F.TypeOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002621 F.LocalNumTypes = Record[0];
2622 unsigned LocalBaseTypeIndex = Record[1];
2623 F.BaseTypeIndex = getTotalNumTypes();
2624
2625 if (F.LocalNumTypes > 0) {
2626 // Introduce the global -> local mapping for types within this module.
2627 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
2628
2629 // Introduce the local -> global mapping for types within this module.
2630 F.TypeRemap.insertOrReplace(
2631 std::make_pair(LocalBaseTypeIndex,
2632 F.BaseTypeIndex - LocalBaseTypeIndex));
2633
2634 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
2635 }
2636 break;
2637 }
2638
2639 case DECL_OFFSET: {
2640 if (F.LocalNumDecls != 0) {
2641 Error("duplicate DECL_OFFSET record in AST file");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002642 return Failure;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002643 }
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002644 F.DeclOffsets = (const DeclOffset *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002645 F.LocalNumDecls = Record[0];
2646 unsigned LocalBaseDeclID = Record[1];
2647 F.BaseDeclID = getTotalNumDecls();
2648
2649 if (F.LocalNumDecls > 0) {
2650 // Introduce the global -> local mapping for declarations within this
2651 // module.
2652 GlobalDeclMap.insert(
2653 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
2654
2655 // Introduce the local -> global mapping for declarations within this
2656 // module.
2657 F.DeclRemap.insertOrReplace(
2658 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
2659
2660 // Introduce the global -> local mapping for declarations within this
2661 // module.
2662 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
2663
2664 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
2665 }
2666 break;
2667 }
2668
2669 case TU_UPDATE_LEXICAL: {
2670 DeclContext *TU = Context.getTranslationUnitDecl();
2671 DeclContextInfo &Info = F.DeclContextInfos[TU];
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002672 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(Blob.data());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002673 Info.NumLexicalDecls
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002674 = static_cast<unsigned int>(Blob.size() / sizeof(KindDeclIDPair));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002675 TU->setHasExternalLexicalStorage(true);
2676 break;
2677 }
2678
2679 case UPDATE_VISIBLE: {
2680 unsigned Idx = 0;
2681 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
2682 ASTDeclContextNameLookupTable *Table =
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002683 ASTDeclContextNameLookupTable::Create(
2684 (const unsigned char *)Blob.data() + Record[Idx++],
2685 (const unsigned char *)Blob.data() + sizeof(uint32_t),
2686 (const unsigned char *)Blob.data(),
2687 ASTDeclContextNameLookupTrait(*this, F));
2688 if (Decl *D = GetExistingDecl(ID)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07002689 auto *DC = cast<DeclContext>(D);
2690 DC->getPrimaryContext()->setHasExternalVisibleStorage(true);
2691 auto *&LookupTable = F.DeclContextInfos[DC].NameLookupTableData;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002692 // FIXME: There should never be an existing lookup table.
Stephen Hines651f13c2014-04-23 16:59:28 -07002693 delete LookupTable;
2694 LookupTable = Table;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002695 } else
2696 PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F));
2697 break;
2698 }
2699
2700 case IDENTIFIER_TABLE:
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002701 F.IdentifierTableData = Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002702 if (Record[0]) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002703 F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
2704 (const unsigned char *)F.IdentifierTableData + Record[0],
2705 (const unsigned char *)F.IdentifierTableData + sizeof(uint32_t),
2706 (const unsigned char *)F.IdentifierTableData,
2707 ASTIdentifierLookupTrait(*this, F));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002708
2709 PP.getIdentifierTable().setExternalIdentifierLookup(this);
2710 }
2711 break;
2712
2713 case IDENTIFIER_OFFSET: {
2714 if (F.LocalNumIdentifiers != 0) {
2715 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002716 return Failure;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002717 }
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002718 F.IdentifierOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002719 F.LocalNumIdentifiers = Record[0];
2720 unsigned LocalBaseIdentifierID = Record[1];
2721 F.BaseIdentifierID = getTotalNumIdentifiers();
2722
2723 if (F.LocalNumIdentifiers > 0) {
2724 // Introduce the global -> local mapping for identifiers within this
2725 // module.
2726 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
2727 &F));
2728
2729 // Introduce the local -> global mapping for identifiers within this
2730 // module.
2731 F.IdentifierRemap.insertOrReplace(
2732 std::make_pair(LocalBaseIdentifierID,
2733 F.BaseIdentifierID - LocalBaseIdentifierID));
2734
2735 IdentifiersLoaded.resize(IdentifiersLoaded.size()
2736 + F.LocalNumIdentifiers);
2737 }
2738 break;
2739 }
2740
Stephen Hines651f13c2014-04-23 16:59:28 -07002741 case EAGERLY_DESERIALIZED_DECLS:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002742 for (unsigned I = 0, N = Record.size(); I != N; ++I)
Stephen Hines651f13c2014-04-23 16:59:28 -07002743 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002744 break;
2745
2746 case SPECIAL_TYPES:
Douglas Gregorf5cfc892013-02-01 23:45:03 +00002747 if (SpecialTypes.empty()) {
2748 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2749 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
2750 break;
2751 }
2752
2753 if (SpecialTypes.size() != Record.size()) {
2754 Error("invalid special-types record");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002755 return Failure;
Douglas Gregorf5cfc892013-02-01 23:45:03 +00002756 }
2757
2758 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2759 serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
2760 if (!SpecialTypes[I])
2761 SpecialTypes[I] = ID;
2762 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
2763 // merge step?
2764 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002765 break;
2766
2767 case STATISTICS:
2768 TotalNumStatements += Record[0];
2769 TotalNumMacros += Record[1];
2770 TotalLexicalDeclContexts += Record[2];
2771 TotalVisibleDeclContexts += Record[3];
2772 break;
2773
2774 case UNUSED_FILESCOPED_DECLS:
2775 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2776 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
2777 break;
2778
2779 case DELEGATING_CTORS:
2780 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2781 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
2782 break;
2783
2784 case WEAK_UNDECLARED_IDENTIFIERS:
2785 if (Record.size() % 4 != 0) {
2786 Error("invalid weak identifiers record");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002787 return Failure;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002788 }
2789
2790 // FIXME: Ignore weak undeclared identifiers from non-original PCH
2791 // files. This isn't the way to do it :)
2792 WeakUndeclaredIdentifiers.clear();
2793
2794 // Translate the weak, undeclared identifiers into global IDs.
2795 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
2796 WeakUndeclaredIdentifiers.push_back(
2797 getGlobalIdentifierID(F, Record[I++]));
2798 WeakUndeclaredIdentifiers.push_back(
2799 getGlobalIdentifierID(F, Record[I++]));
2800 WeakUndeclaredIdentifiers.push_back(
2801 ReadSourceLocation(F, Record, I).getRawEncoding());
2802 WeakUndeclaredIdentifiers.push_back(Record[I++]);
2803 }
2804 break;
2805
Richard Smith5ea6ef42013-01-10 23:43:47 +00002806 case LOCALLY_SCOPED_EXTERN_C_DECLS:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002807 for (unsigned I = 0, N = Record.size(); I != N; ++I)
Richard Smith5ea6ef42013-01-10 23:43:47 +00002808 LocallyScopedExternCDecls.push_back(getGlobalDeclID(F, Record[I]));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002809 break;
2810
2811 case SELECTOR_OFFSETS: {
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002812 F.SelectorOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002813 F.LocalNumSelectors = Record[0];
2814 unsigned LocalBaseSelectorID = Record[1];
2815 F.BaseSelectorID = getTotalNumSelectors();
2816
2817 if (F.LocalNumSelectors > 0) {
2818 // Introduce the global -> local mapping for selectors within this
2819 // module.
2820 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
2821
2822 // Introduce the local -> global mapping for selectors within this
2823 // module.
2824 F.SelectorRemap.insertOrReplace(
2825 std::make_pair(LocalBaseSelectorID,
2826 F.BaseSelectorID - LocalBaseSelectorID));
2827
2828 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
2829 }
2830 break;
2831 }
2832
2833 case METHOD_POOL:
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002834 F.SelectorLookupTableData = (const unsigned char *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002835 if (Record[0])
2836 F.SelectorLookupTable
2837 = ASTSelectorLookupTable::Create(
2838 F.SelectorLookupTableData + Record[0],
2839 F.SelectorLookupTableData,
2840 ASTSelectorLookupTrait(*this, F));
2841 TotalNumMethodPoolEntries += Record[1];
2842 break;
2843
2844 case REFERENCED_SELECTOR_POOL:
2845 if (!Record.empty()) {
2846 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
2847 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
2848 Record[Idx++]));
2849 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
2850 getRawEncoding());
2851 }
2852 }
2853 break;
2854
2855 case PP_COUNTER_VALUE:
2856 if (!Record.empty() && Listener)
2857 Listener->ReadCounter(F, Record[0]);
2858 break;
2859
2860 case FILE_SORTED_DECLS:
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002861 F.FileSortedDecls = (const DeclID *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002862 F.NumFileSortedDecls = Record[0];
2863 break;
2864
2865 case SOURCE_LOCATION_OFFSETS: {
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002866 F.SLocEntryOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002867 F.LocalNumSLocEntries = Record[0];
2868 unsigned SLocSpaceSize = Record[1];
Stephen Hines651f13c2014-04-23 16:59:28 -07002869 std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002870 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
2871 SLocSpaceSize);
2872 // Make our entry in the range map. BaseID is negative and growing, so
2873 // we invert it. Because we invert it, though, we need the other end of
2874 // the range.
2875 unsigned RangeStart =
2876 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
2877 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
2878 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
2879
2880 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
2881 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
2882 GlobalSLocOffsetMap.insert(
2883 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
2884 - SLocSpaceSize,&F));
2885
2886 // Initialize the remapping table.
2887 // Invalid stays invalid.
Stephen Hines651f13c2014-04-23 16:59:28 -07002888 F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002889 // This module. Base was 2 when being compiled.
Stephen Hines651f13c2014-04-23 16:59:28 -07002890 F.SLocRemap.insertOrReplace(std::make_pair(2U,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002891 static_cast<int>(F.SLocEntryBaseOffset - 2)));
2892
2893 TotalNumSLocEntries += F.LocalNumSLocEntries;
2894 break;
2895 }
2896
2897 case MODULE_OFFSET_MAP: {
2898 // Additional remapping information.
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002899 const unsigned char *Data = (const unsigned char*)Blob.data();
2900 const unsigned char *DataEnd = Data + Blob.size();
Stephen Hines651f13c2014-04-23 16:59:28 -07002901
2902 // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
2903 if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
2904 F.SLocRemap.insert(std::make_pair(0U, 0));
2905 F.SLocRemap.insert(std::make_pair(2U, 1));
2906 }
2907
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002908 // Continuous range maps we may be updating in our module.
2909 ContinuousRangeMap<uint32_t, int, 2>::Builder SLocRemap(F.SLocRemap);
2910 ContinuousRangeMap<uint32_t, int, 2>::Builder
2911 IdentifierRemap(F.IdentifierRemap);
2912 ContinuousRangeMap<uint32_t, int, 2>::Builder
2913 MacroRemap(F.MacroRemap);
2914 ContinuousRangeMap<uint32_t, int, 2>::Builder
2915 PreprocessedEntityRemap(F.PreprocessedEntityRemap);
2916 ContinuousRangeMap<uint32_t, int, 2>::Builder
2917 SubmoduleRemap(F.SubmoduleRemap);
2918 ContinuousRangeMap<uint32_t, int, 2>::Builder
2919 SelectorRemap(F.SelectorRemap);
2920 ContinuousRangeMap<uint32_t, int, 2>::Builder DeclRemap(F.DeclRemap);
2921 ContinuousRangeMap<uint32_t, int, 2>::Builder TypeRemap(F.TypeRemap);
2922
2923 while(Data < DataEnd) {
Stephen Hines651f13c2014-04-23 16:59:28 -07002924 using namespace llvm::support;
2925 uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002926 StringRef Name = StringRef((const char*)Data, Len);
2927 Data += Len;
2928 ModuleFile *OM = ModuleMgr.lookup(Name);
2929 if (!OM) {
2930 Error("SourceLocation remap refers to unknown module");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002931 return Failure;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002932 }
2933
Stephen Hines651f13c2014-04-23 16:59:28 -07002934 uint32_t SLocOffset =
2935 endian::readNext<uint32_t, little, unaligned>(Data);
2936 uint32_t IdentifierIDOffset =
2937 endian::readNext<uint32_t, little, unaligned>(Data);
2938 uint32_t MacroIDOffset =
2939 endian::readNext<uint32_t, little, unaligned>(Data);
2940 uint32_t PreprocessedEntityIDOffset =
2941 endian::readNext<uint32_t, little, unaligned>(Data);
2942 uint32_t SubmoduleIDOffset =
2943 endian::readNext<uint32_t, little, unaligned>(Data);
2944 uint32_t SelectorIDOffset =
2945 endian::readNext<uint32_t, little, unaligned>(Data);
2946 uint32_t DeclIDOffset =
2947 endian::readNext<uint32_t, little, unaligned>(Data);
2948 uint32_t TypeIndexOffset =
2949 endian::readNext<uint32_t, little, unaligned>(Data);
2950
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002951 // Source location offset is mapped to OM->SLocEntryBaseOffset.
2952 SLocRemap.insert(std::make_pair(SLocOffset,
2953 static_cast<int>(OM->SLocEntryBaseOffset - SLocOffset)));
2954 IdentifierRemap.insert(
2955 std::make_pair(IdentifierIDOffset,
2956 OM->BaseIdentifierID - IdentifierIDOffset));
2957 MacroRemap.insert(std::make_pair(MacroIDOffset,
2958 OM->BaseMacroID - MacroIDOffset));
2959 PreprocessedEntityRemap.insert(
2960 std::make_pair(PreprocessedEntityIDOffset,
2961 OM->BasePreprocessedEntityID - PreprocessedEntityIDOffset));
2962 SubmoduleRemap.insert(std::make_pair(SubmoduleIDOffset,
2963 OM->BaseSubmoduleID - SubmoduleIDOffset));
2964 SelectorRemap.insert(std::make_pair(SelectorIDOffset,
2965 OM->BaseSelectorID - SelectorIDOffset));
2966 DeclRemap.insert(std::make_pair(DeclIDOffset,
2967 OM->BaseDeclID - DeclIDOffset));
2968
2969 TypeRemap.insert(std::make_pair(TypeIndexOffset,
2970 OM->BaseTypeIndex - TypeIndexOffset));
2971
2972 // Global -> local mappings.
2973 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
2974 }
2975 break;
2976 }
2977
2978 case SOURCE_MANAGER_LINE_TABLE:
2979 if (ParseLineTable(F, Record))
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002980 return Failure;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002981 break;
2982
2983 case SOURCE_LOCATION_PRELOADS: {
2984 // Need to transform from the local view (1-based IDs) to the global view,
2985 // which is based off F.SLocEntryBaseID.
2986 if (!F.PreloadSLocEntries.empty()) {
2987 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002988 return Failure;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002989 }
2990
2991 F.PreloadSLocEntries.swap(Record);
2992 break;
2993 }
2994
2995 case EXT_VECTOR_DECLS:
2996 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2997 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
2998 break;
2999
3000 case VTABLE_USES:
3001 if (Record.size() % 3 != 0) {
3002 Error("Invalid VTABLE_USES record");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003003 return Failure;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003004 }
3005
3006 // Later tables overwrite earlier ones.
3007 // FIXME: Modules will have some trouble with this. This is clearly not
3008 // the right way to do this.
3009 VTableUses.clear();
3010
3011 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
3012 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
3013 VTableUses.push_back(
3014 ReadSourceLocation(F, Record, Idx).getRawEncoding());
3015 VTableUses.push_back(Record[Idx++]);
3016 }
3017 break;
3018
3019 case DYNAMIC_CLASSES:
3020 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3021 DynamicClasses.push_back(getGlobalDeclID(F, Record[I]));
3022 break;
3023
3024 case PENDING_IMPLICIT_INSTANTIATIONS:
3025 if (PendingInstantiations.size() % 2 != 0) {
3026 Error("Invalid existing PendingInstantiations");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003027 return Failure;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003028 }
3029
3030 if (Record.size() % 2 != 0) {
3031 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003032 return Failure;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003033 }
3034
3035 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3036 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
3037 PendingInstantiations.push_back(
3038 ReadSourceLocation(F, Record, I).getRawEncoding());
3039 }
3040 break;
3041
3042 case SEMA_DECL_REFS:
Richard Smith9b671182013-10-18 06:54:39 +00003043 if (Record.size() != 2) {
3044 Error("Invalid SEMA_DECL_REFS block");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003045 return Failure;
Richard Smith9b671182013-10-18 06:54:39 +00003046 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003047 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3048 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3049 break;
3050
3051 case PPD_ENTITIES_OFFSETS: {
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003052 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
3053 assert(Blob.size() % sizeof(PPEntityOffset) == 0);
3054 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003055
3056 unsigned LocalBasePreprocessedEntityID = Record[0];
3057
3058 unsigned StartingID;
3059 if (!PP.getPreprocessingRecord())
3060 PP.createPreprocessingRecord();
3061 if (!PP.getPreprocessingRecord()->getExternalSource())
3062 PP.getPreprocessingRecord()->SetExternalSource(*this);
3063 StartingID
3064 = PP.getPreprocessingRecord()
3065 ->allocateLoadedEntities(F.NumPreprocessedEntities);
3066 F.BasePreprocessedEntityID = StartingID;
3067
3068 if (F.NumPreprocessedEntities > 0) {
3069 // Introduce the global -> local mapping for preprocessed entities in
3070 // this module.
3071 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
3072
3073 // Introduce the local -> global mapping for preprocessed entities in
3074 // this module.
3075 F.PreprocessedEntityRemap.insertOrReplace(
3076 std::make_pair(LocalBasePreprocessedEntityID,
3077 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
3078 }
3079
3080 break;
3081 }
3082
3083 case DECL_UPDATE_OFFSETS: {
3084 if (Record.size() % 2 != 0) {
3085 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003086 return Failure;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003087 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003088 for (unsigned I = 0, N = Record.size(); I != N; I += 2) {
3089 GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
3090 DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1]));
3091
3092 // If we've already loaded the decl, perform the updates when we finish
3093 // loading this block.
3094 if (Decl *D = GetExistingDecl(ID))
3095 PendingUpdateRecords.push_back(std::make_pair(ID, D));
3096 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003097 break;
3098 }
3099
3100 case DECL_REPLACEMENTS: {
3101 if (Record.size() % 3 != 0) {
3102 Error("invalid DECL_REPLACEMENTS block in AST file");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003103 return Failure;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003104 }
3105 for (unsigned I = 0, N = Record.size(); I != N; I += 3)
3106 ReplacedDecls[getGlobalDeclID(F, Record[I])]
3107 = ReplacedDeclInfo(&F, Record[I+1], Record[I+2]);
3108 break;
3109 }
3110
3111 case OBJC_CATEGORIES_MAP: {
3112 if (F.LocalNumObjCCategoriesInMap != 0) {
3113 Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003114 return Failure;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003115 }
3116
3117 F.LocalNumObjCCategoriesInMap = Record[0];
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003118 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003119 break;
3120 }
3121
3122 case OBJC_CATEGORIES:
3123 F.ObjCCategories.swap(Record);
3124 break;
3125
3126 case CXX_BASE_SPECIFIER_OFFSETS: {
3127 if (F.LocalNumCXXBaseSpecifiers != 0) {
3128 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003129 return Failure;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003130 }
3131
3132 F.LocalNumCXXBaseSpecifiers = Record[0];
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003133 F.CXXBaseSpecifiersOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003134 NumCXXBaseSpecifiersLoaded += F.LocalNumCXXBaseSpecifiers;
3135 break;
3136 }
3137
3138 case DIAG_PRAGMA_MAPPINGS:
3139 if (F.PragmaDiagMappings.empty())
3140 F.PragmaDiagMappings.swap(Record);
3141 else
3142 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
3143 Record.begin(), Record.end());
3144 break;
3145
3146 case CUDA_SPECIAL_DECL_REFS:
3147 // Later tables overwrite earlier ones.
3148 // FIXME: Modules will have trouble with this.
3149 CUDASpecialDeclRefs.clear();
3150 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3151 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3152 break;
3153
3154 case HEADER_SEARCH_TABLE: {
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003155 F.HeaderFileInfoTableData = Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003156 F.LocalNumHeaderFileInfos = Record[1];
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003157 if (Record[0]) {
3158 F.HeaderFileInfoTable
3159 = HeaderFileInfoLookupTable::Create(
3160 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
3161 (const unsigned char *)F.HeaderFileInfoTableData,
3162 HeaderFileInfoTrait(*this, F,
3163 &PP.getHeaderSearchInfo(),
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003164 Blob.data() + Record[2]));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003165
3166 PP.getHeaderSearchInfo().SetExternalSource(this);
3167 if (!PP.getHeaderSearchInfo().getExternalLookup())
3168 PP.getHeaderSearchInfo().SetExternalLookup(this);
3169 }
3170 break;
3171 }
3172
3173 case FP_PRAGMA_OPTIONS:
3174 // Later tables overwrite earlier ones.
3175 FPPragmaOptions.swap(Record);
3176 break;
3177
3178 case OPENCL_EXTENSIONS:
3179 // Later tables overwrite earlier ones.
3180 OpenCLExtensions.swap(Record);
3181 break;
3182
3183 case TENTATIVE_DEFINITIONS:
3184 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3185 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
3186 break;
3187
3188 case KNOWN_NAMESPACES:
3189 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3190 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
3191 break;
Nick Lewycky01a41142013-01-26 00:35:08 +00003192
Nick Lewyckycd0655b2013-02-01 08:13:20 +00003193 case UNDEFINED_BUT_USED:
3194 if (UndefinedButUsed.size() % 2 != 0) {
3195 Error("Invalid existing UndefinedButUsed");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003196 return Failure;
Nick Lewycky01a41142013-01-26 00:35:08 +00003197 }
3198
3199 if (Record.size() % 2 != 0) {
Nick Lewyckycd0655b2013-02-01 08:13:20 +00003200 Error("invalid undefined-but-used record");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003201 return Failure;
Nick Lewycky01a41142013-01-26 00:35:08 +00003202 }
3203 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
Nick Lewyckycd0655b2013-02-01 08:13:20 +00003204 UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
3205 UndefinedButUsed.push_back(
Nick Lewycky01a41142013-01-26 00:35:08 +00003206 ReadSourceLocation(F, Record, I).getRawEncoding());
3207 }
3208 break;
3209
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003210 case IMPORTED_MODULES: {
3211 if (F.Kind != MK_Module) {
3212 // If we aren't loading a module (which has its own exports), make
3213 // all of the imported modules visible.
3214 // FIXME: Deal with macros-only imports.
Stephen Hines651f13c2014-04-23 16:59:28 -07003215 for (unsigned I = 0, N = Record.size(); I != N; /**/) {
3216 unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
3217 SourceLocation Loc = ReadSourceLocation(F, Record, I);
3218 if (GlobalID)
3219 ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003220 }
3221 }
3222 break;
3223 }
3224
3225 case LOCAL_REDECLARATIONS: {
3226 F.RedeclarationChains.swap(Record);
3227 break;
3228 }
3229
3230 case LOCAL_REDECLARATIONS_MAP: {
3231 if (F.LocalNumRedeclarationsInMap != 0) {
3232 Error("duplicate LOCAL_REDECLARATIONS_MAP record in AST file");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003233 return Failure;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003234 }
3235
3236 F.LocalNumRedeclarationsInMap = Record[0];
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003237 F.RedeclarationsMap = (const LocalRedeclarationsInfo *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003238 break;
3239 }
3240
3241 case MERGED_DECLARATIONS: {
3242 for (unsigned Idx = 0; Idx < Record.size(); /* increment in loop */) {
3243 GlobalDeclID CanonID = getGlobalDeclID(F, Record[Idx++]);
3244 SmallVectorImpl<GlobalDeclID> &Decls = StoredMergedDecls[CanonID];
3245 for (unsigned N = Record[Idx++]; N > 0; --N)
3246 Decls.push_back(getGlobalDeclID(F, Record[Idx++]));
3247 }
3248 break;
3249 }
3250
3251 case MACRO_OFFSET: {
3252 if (F.LocalNumMacros != 0) {
3253 Error("duplicate MACRO_OFFSET record in AST file");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003254 return Failure;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003255 }
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003256 F.MacroOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003257 F.LocalNumMacros = Record[0];
3258 unsigned LocalBaseMacroID = Record[1];
3259 F.BaseMacroID = getTotalNumMacros();
3260
3261 if (F.LocalNumMacros > 0) {
3262 // Introduce the global -> local mapping for macros within this module.
3263 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3264
3265 // Introduce the local -> global mapping for macros within this module.
3266 F.MacroRemap.insertOrReplace(
3267 std::make_pair(LocalBaseMacroID,
3268 F.BaseMacroID - LocalBaseMacroID));
3269
3270 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
3271 }
3272 break;
3273 }
3274
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003275 case MACRO_TABLE: {
3276 // FIXME: Not used yet.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003277 break;
3278 }
Richard Smithac32d902013-08-07 21:41:30 +00003279
3280 case LATE_PARSED_TEMPLATE: {
3281 LateParsedTemplates.append(Record.begin(), Record.end());
3282 break;
3283 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003284
3285 case OPTIMIZE_PRAGMA_OPTIONS:
3286 if (Record.size() != 1) {
3287 Error("invalid pragma optimize record");
3288 return Failure;
3289 }
3290 OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
3291 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003292 }
3293 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003294}
3295
Douglas Gregor2cbd4272013-02-12 23:36:21 +00003296/// \brief Move the given method to the back of the global list of methods.
3297static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
3298 // Find the entry for this selector in the method pool.
3299 Sema::GlobalMethodPool::iterator Known
3300 = S.MethodPool.find(Method->getSelector());
3301 if (Known == S.MethodPool.end())
3302 return;
3303
3304 // Retrieve the appropriate method list.
3305 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
3306 : Known->second.second;
3307 bool Found = false;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00003308 for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
Douglas Gregor2cbd4272013-02-12 23:36:21 +00003309 if (!Found) {
3310 if (List->Method == Method) {
3311 Found = true;
3312 } else {
3313 // Keep searching.
3314 continue;
3315 }
3316 }
3317
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00003318 if (List->getNext())
3319 List->Method = List->getNext()->Method;
Douglas Gregor2cbd4272013-02-12 23:36:21 +00003320 else
3321 List->Method = Method;
3322 }
3323}
3324
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00003325void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003326 for (unsigned I = 0, N = Names.HiddenDecls.size(); I != N; ++I) {
3327 Decl *D = Names.HiddenDecls[I];
3328 bool wasHidden = D->Hidden;
3329 D->Hidden = false;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003330
Stephen Hines651f13c2014-04-23 16:59:28 -07003331 if (wasHidden && SemaObj) {
3332 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
3333 moveMethodToBackOfGlobalList(*SemaObj, Method);
Douglas Gregor2cbd4272013-02-12 23:36:21 +00003334 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003335 }
3336 }
Stephen Hines651f13c2014-04-23 16:59:28 -07003337
3338 for (HiddenMacrosMap::const_iterator I = Names.HiddenMacros.begin(),
3339 E = Names.HiddenMacros.end();
3340 I != E; ++I)
3341 installImportedMacro(I->first, I->second, Owner);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003342}
3343
Stephen Hines651f13c2014-04-23 16:59:28 -07003344void ASTReader::makeModuleVisible(Module *Mod,
Argyrios Kyrtzidis5ebcb202013-02-01 16:36:12 +00003345 Module::NameVisibilityKind NameVisibility,
Douglas Gregor906d66a2013-03-20 21:10:35 +00003346 SourceLocation ImportLoc,
3347 bool Complain) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003348 llvm::SmallPtrSet<Module *, 4> Visited;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003349 SmallVector<Module *, 4> Stack;
Robert Wilhelm344472e2013-08-23 16:11:15 +00003350 Stack.push_back(Mod);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003351 while (!Stack.empty()) {
Robert Wilhelm344472e2013-08-23 16:11:15 +00003352 Mod = Stack.pop_back_val();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003353
3354 if (NameVisibility <= Mod->NameVisibility) {
Robert Wilhelm344472e2013-08-23 16:11:15 +00003355 // This module already has this level of visibility (or greater), so
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003356 // there is nothing more to do.
3357 continue;
3358 }
Stephen Hines651f13c2014-04-23 16:59:28 -07003359
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003360 if (!Mod->isAvailable()) {
3361 // Modules that aren't available cannot be made visible.
3362 continue;
3363 }
3364
3365 // Update the module's name visibility.
Stephen Hines651f13c2014-04-23 16:59:28 -07003366 if (NameVisibility >= Module::MacrosVisible &&
3367 Mod->NameVisibility < Module::MacrosVisible)
3368 Mod->MacroVisibilityLoc = ImportLoc;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003369 Mod->NameVisibility = NameVisibility;
Stephen Hines651f13c2014-04-23 16:59:28 -07003370
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003371 // If we've already deserialized any names from this module,
3372 // mark them as visible.
3373 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
3374 if (Hidden != HiddenNamesMap.end()) {
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00003375 makeNamesVisible(Hidden->second, Hidden->first);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003376 HiddenNamesMap.erase(Hidden);
3377 }
Dmitri Gribenko86250892013-11-04 21:51:33 +00003378
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003379 // Push any exported modules onto the stack to be marked as visible.
Argyrios Kyrtzidis21a00042013-02-19 19:34:40 +00003380 SmallVector<Module *, 16> Exports;
3381 Mod->getExportedModules(Exports);
3382 for (SmallVectorImpl<Module *>::iterator
3383 I = Exports.begin(), E = Exports.end(); I != E; ++I) {
3384 Module *Exported = *I;
3385 if (Visited.insert(Exported))
3386 Stack.push_back(Exported);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003387 }
Douglas Gregor906d66a2013-03-20 21:10:35 +00003388
3389 // Detect any conflicts.
3390 if (Complain) {
3391 assert(ImportLoc.isValid() && "Missing import location");
3392 for (unsigned I = 0, N = Mod->Conflicts.size(); I != N; ++I) {
3393 if (Mod->Conflicts[I].Other->NameVisibility >= NameVisibility) {
3394 Diag(ImportLoc, diag::warn_module_conflict)
3395 << Mod->getFullModuleName()
3396 << Mod->Conflicts[I].Other->getFullModuleName()
3397 << Mod->Conflicts[I].Message;
3398 // FIXME: Need note where the other module was imported.
3399 }
3400 }
3401 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003402 }
3403}
3404
Douglas Gregor1a49d972013-01-25 01:03:03 +00003405bool ASTReader::loadGlobalIndex() {
3406 if (GlobalIndex)
3407 return false;
3408
3409 if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
3410 !Context.getLangOpts().Modules)
3411 return true;
3412
3413 // Try to load the global index.
3414 TriedLoadingGlobalIndex = true;
3415 StringRef ModuleCachePath
3416 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
3417 std::pair<GlobalModuleIndex *, GlobalModuleIndex::ErrorCode> Result
Douglas Gregor677e15f2013-03-19 00:28:20 +00003418 = GlobalModuleIndex::readIndex(ModuleCachePath);
Douglas Gregor1a49d972013-01-25 01:03:03 +00003419 if (!Result.first)
3420 return true;
3421
3422 GlobalIndex.reset(Result.first);
Douglas Gregor188bdcd2013-01-25 23:32:03 +00003423 ModuleMgr.setGlobalIndex(GlobalIndex.get());
Douglas Gregor1a49d972013-01-25 01:03:03 +00003424 return false;
3425}
3426
3427bool ASTReader::isGlobalIndexUnavailable() const {
3428 return Context.getLangOpts().Modules && UseGlobalIndex &&
3429 !hasGlobalIndex() && TriedLoadingGlobalIndex;
3430}
3431
Stephen Hines651f13c2014-04-23 16:59:28 -07003432static void updateModuleTimestamp(ModuleFile &MF) {
3433 // Overwrite the timestamp file contents so that file's mtime changes.
3434 std::string TimestampFilename = MF.getTimestampFilename();
3435 std::string ErrorInfo;
3436 llvm::raw_fd_ostream OS(TimestampFilename.c_str(), ErrorInfo,
3437 llvm::sys::fs::F_Text);
3438 if (!ErrorInfo.empty())
3439 return;
3440 OS << "Timestamp file\n";
3441}
3442
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003443ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
3444 ModuleKind Type,
3445 SourceLocation ImportLoc,
3446 unsigned ClientLoadCapabilities) {
Argyrios Kyrtzidis3b7deda2013-05-24 05:44:08 +00003447 llvm::SaveAndRestore<SourceLocation>
3448 SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
3449
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003450 // Defer any pending actions until we get to the end of reading the AST file.
3451 Deserializing AnASTFile(this);
3452
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003453 // Bump the generation number.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003454 unsigned PreviousGeneration = incrementGeneration(Context);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003455
3456 unsigned NumModules = ModuleMgr.size();
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003457 SmallVector<ImportedModule, 4> Loaded;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003458 switch(ASTReadResult ReadResult = ReadASTCore(FileName, Type, ImportLoc,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003459 /*ImportedBy=*/nullptr, Loaded,
Douglas Gregor677e15f2013-03-19 00:28:20 +00003460 0, 0,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003461 ClientLoadCapabilities)) {
3462 case Failure:
Douglas Gregor677e15f2013-03-19 00:28:20 +00003463 case Missing:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003464 case OutOfDate:
3465 case VersionMismatch:
3466 case ConfigurationMismatch:
3467 case HadErrors:
Douglas Gregor677e15f2013-03-19 00:28:20 +00003468 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, ModuleMgr.end(),
3469 Context.getLangOpts().Modules
3470 ? &PP.getHeaderSearchInfo().getModuleMap()
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003471 : nullptr);
Douglas Gregor1a49d972013-01-25 01:03:03 +00003472
3473 // If we find that any modules are unusable, the global index is going
3474 // to be out-of-date. Just remove it.
3475 GlobalIndex.reset();
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003476 ModuleMgr.setGlobalIndex(nullptr);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003477 return ReadResult;
3478
3479 case Success:
3480 break;
3481 }
3482
3483 // Here comes stuff that we only do once the entire chain is loaded.
3484
3485 // Load the AST blocks of all of the modules that we loaded.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003486 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
3487 MEnd = Loaded.end();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003488 M != MEnd; ++M) {
3489 ModuleFile &F = *M->Mod;
3490
3491 // Read the AST block.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003492 if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities))
3493 return Result;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003494
3495 // Once read, set the ModuleFile bit base offset and update the size in
3496 // bits of all files we've seen.
3497 F.GlobalBitOffset = TotalModulesSizeInBits;
3498 TotalModulesSizeInBits += F.SizeInBits;
3499 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
3500
3501 // Preload SLocEntries.
3502 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
3503 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
3504 // Load it through the SourceManager and don't call ReadSLocEntry()
3505 // directly because the entry may have already been loaded in which case
3506 // calling ReadSLocEntry() directly would trigger an assertion in
3507 // SourceManager.
3508 SourceMgr.getLoadedSLocEntryByID(Index);
3509 }
3510 }
3511
Douglas Gregorfa69fc12013-03-22 18:50:14 +00003512 // Setup the import locations and notify the module manager that we've
3513 // committed to these module files.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003514 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
3515 MEnd = Loaded.end();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003516 M != MEnd; ++M) {
3517 ModuleFile &F = *M->Mod;
Douglas Gregorfa69fc12013-03-22 18:50:14 +00003518
3519 ModuleMgr.moduleFileAccepted(&F);
3520
3521 // Set the import location.
Argyrios Kyrtzidis8b136d82013-02-01 16:36:14 +00003522 F.DirectImportLoc = ImportLoc;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003523 if (!M->ImportedBy)
3524 F.ImportLoc = M->ImportLoc;
3525 else
3526 F.ImportLoc = ReadSourceLocation(*M->ImportedBy,
3527 M->ImportLoc.getRawEncoding());
3528 }
3529
3530 // Mark all of the identifiers in the identifier table as being out of date,
3531 // so that various accessors know to check the loaded modules when the
3532 // identifier is used.
3533 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
3534 IdEnd = PP.getIdentifierTable().end();
3535 Id != IdEnd; ++Id)
3536 Id->second->setOutOfDate(true);
3537
3538 // Resolve any unresolved module exports.
Douglas Gregor906d66a2013-03-20 21:10:35 +00003539 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
3540 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003541 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
3542 Module *ResolvedMod = getSubmodule(GlobalID);
Douglas Gregor906d66a2013-03-20 21:10:35 +00003543
3544 switch (Unresolved.Kind) {
3545 case UnresolvedModuleRef::Conflict:
3546 if (ResolvedMod) {
3547 Module::Conflict Conflict;
3548 Conflict.Other = ResolvedMod;
3549 Conflict.Message = Unresolved.String.str();
3550 Unresolved.Mod->Conflicts.push_back(Conflict);
3551 }
3552 continue;
3553
3554 case UnresolvedModuleRef::Import:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003555 if (ResolvedMod)
3556 Unresolved.Mod->Imports.push_back(ResolvedMod);
3557 continue;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003558
Douglas Gregor906d66a2013-03-20 21:10:35 +00003559 case UnresolvedModuleRef::Export:
3560 if (ResolvedMod || Unresolved.IsWildcard)
3561 Unresolved.Mod->Exports.push_back(
3562 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
3563 continue;
3564 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003565 }
Douglas Gregor906d66a2013-03-20 21:10:35 +00003566 UnresolvedModuleRefs.clear();
Daniel Jasperddd2dfc2013-09-24 09:14:14 +00003567
3568 // FIXME: How do we load the 'use'd modules? They may not be submodules.
3569 // Might be unnecessary as use declarations are only used to build the
3570 // module itself.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003571
3572 InitializeContext();
3573
Richard Smith9b671182013-10-18 06:54:39 +00003574 if (SemaObj)
3575 UpdateSema();
3576
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003577 if (DeserializationListener)
3578 DeserializationListener->ReaderInitialized(this);
3579
3580 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
3581 if (!PrimaryModule.OriginalSourceFileID.isInvalid()) {
3582 PrimaryModule.OriginalSourceFileID
3583 = FileID::get(PrimaryModule.SLocEntryBaseID
3584 + PrimaryModule.OriginalSourceFileID.getOpaqueValue() - 1);
3585
3586 // If this AST file is a precompiled preamble, then set the
3587 // preamble file ID of the source manager to the file source file
3588 // from which the preamble was built.
3589 if (Type == MK_Preamble) {
3590 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
3591 } else if (Type == MK_MainFile) {
3592 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
3593 }
3594 }
3595
3596 // For any Objective-C class definitions we have already loaded, make sure
3597 // that we load any additional categories.
3598 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
3599 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
3600 ObjCClassesLoaded[I],
3601 PreviousGeneration);
3602 }
Douglas Gregor1a49d972013-01-25 01:03:03 +00003603
Stephen Hines651f13c2014-04-23 16:59:28 -07003604 if (PP.getHeaderSearchInfo()
3605 .getHeaderSearchOpts()
3606 .ModulesValidateOncePerBuildSession) {
3607 // Now we are certain that the module and all modules it depends on are
3608 // up to date. Create or update timestamp files for modules that are
3609 // located in the module cache (not for PCH files that could be anywhere
3610 // in the filesystem).
3611 for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
3612 ImportedModule &M = Loaded[I];
3613 if (M.Mod->Kind == MK_Module) {
3614 updateModuleTimestamp(*M.Mod);
3615 }
3616 }
3617 }
3618
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003619 return Success;
3620}
3621
3622ASTReader::ASTReadResult
3623ASTReader::ReadASTCore(StringRef FileName,
3624 ModuleKind Type,
3625 SourceLocation ImportLoc,
3626 ModuleFile *ImportedBy,
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003627 SmallVectorImpl<ImportedModule> &Loaded,
Douglas Gregor677e15f2013-03-19 00:28:20 +00003628 off_t ExpectedSize, time_t ExpectedModTime,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003629 unsigned ClientLoadCapabilities) {
3630 ModuleFile *M;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003631 std::string ErrorStr;
Douglas Gregor677e15f2013-03-19 00:28:20 +00003632 ModuleManager::AddModuleResult AddResult
3633 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003634 getGeneration(), ExpectedSize, ExpectedModTime,
Douglas Gregor677e15f2013-03-19 00:28:20 +00003635 M, ErrorStr);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003636
Douglas Gregor677e15f2013-03-19 00:28:20 +00003637 switch (AddResult) {
3638 case ModuleManager::AlreadyLoaded:
3639 return Success;
3640
3641 case ModuleManager::NewlyLoaded:
3642 // Load module file below.
3643 break;
3644
3645 case ModuleManager::Missing:
3646 // The module file was missing; if the client handle handle, that, return
3647 // it.
3648 if (ClientLoadCapabilities & ARR_Missing)
3649 return Missing;
3650
3651 // Otherwise, return an error.
3652 {
3653 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
3654 + ErrorStr;
3655 Error(Msg);
3656 }
3657 return Failure;
3658
3659 case ModuleManager::OutOfDate:
3660 // We couldn't load the module file because it is out-of-date. If the
3661 // client can handle out-of-date, return it.
3662 if (ClientLoadCapabilities & ARR_OutOfDate)
3663 return OutOfDate;
3664
3665 // Otherwise, return an error.
3666 {
3667 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
3668 + ErrorStr;
3669 Error(Msg);
3670 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003671 return Failure;
3672 }
3673
Douglas Gregor677e15f2013-03-19 00:28:20 +00003674 assert(M && "Missing module file");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003675
3676 // FIXME: This seems rather a hack. Should CurrentDir be part of the
3677 // module?
3678 if (FileName != "-") {
3679 CurrentDir = llvm::sys::path::parent_path(FileName);
3680 if (CurrentDir.empty()) CurrentDir = ".";
3681 }
3682
3683 ModuleFile &F = *M;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003684 BitstreamCursor &Stream = F.Stream;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003685 Stream.init(F.StreamFile);
3686 F.SizeInBits = F.Buffer->getBufferSize() * 8;
3687
3688 // Sniff for the signature.
3689 if (Stream.Read(8) != 'C' ||
3690 Stream.Read(8) != 'P' ||
3691 Stream.Read(8) != 'C' ||
3692 Stream.Read(8) != 'H') {
3693 Diag(diag::err_not_a_pch_file) << FileName;
3694 return Failure;
3695 }
3696
3697 // This is used for compatibility with older PCH formats.
3698 bool HaveReadControlBlock = false;
3699
Chris Lattner99a5af02013-01-20 00:00:22 +00003700 while (1) {
3701 llvm::BitstreamEntry Entry = Stream.advance();
3702
3703 switch (Entry.Kind) {
3704 case llvm::BitstreamEntry::Error:
3705 case llvm::BitstreamEntry::EndBlock:
3706 case llvm::BitstreamEntry::Record:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003707 Error("invalid record at top-level of AST file");
3708 return Failure;
Chris Lattner99a5af02013-01-20 00:00:22 +00003709
3710 case llvm::BitstreamEntry::SubBlock:
3711 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003712 }
3713
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003714 // We only know the control subblock ID.
Chris Lattner99a5af02013-01-20 00:00:22 +00003715 switch (Entry.ID) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003716 case llvm::bitc::BLOCKINFO_BLOCK_ID:
3717 if (Stream.ReadBlockInfoBlock()) {
3718 Error("malformed BlockInfoBlock in AST file");
3719 return Failure;
3720 }
3721 break;
3722 case CONTROL_BLOCK_ID:
3723 HaveReadControlBlock = true;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003724 switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003725 case Success:
3726 break;
3727
3728 case Failure: return Failure;
Douglas Gregor677e15f2013-03-19 00:28:20 +00003729 case Missing: return Missing;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003730 case OutOfDate: return OutOfDate;
3731 case VersionMismatch: return VersionMismatch;
3732 case ConfigurationMismatch: return ConfigurationMismatch;
3733 case HadErrors: return HadErrors;
3734 }
3735 break;
3736 case AST_BLOCK_ID:
3737 if (!HaveReadControlBlock) {
3738 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
Stephen Hines651f13c2014-04-23 16:59:28 -07003739 Diag(diag::err_pch_version_too_old);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003740 return VersionMismatch;
3741 }
3742
3743 // Record that we've loaded this module.
3744 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
3745 return Success;
3746
3747 default:
3748 if (Stream.SkipBlock()) {
3749 Error("malformed block record in AST file");
3750 return Failure;
3751 }
3752 break;
3753 }
3754 }
3755
3756 return Success;
3757}
3758
3759void ASTReader::InitializeContext() {
3760 // If there's a listener, notify them that we "read" the translation unit.
3761 if (DeserializationListener)
3762 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
3763 Context.getTranslationUnitDecl());
3764
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003765 // FIXME: Find a better way to deal with collisions between these
3766 // built-in types. Right now, we just ignore the problem.
3767
3768 // Load the special types.
3769 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
3770 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
3771 if (!Context.CFConstantStringTypeDecl)
3772 Context.setCFConstantStringType(GetType(String));
3773 }
3774
3775 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
3776 QualType FileType = GetType(File);
3777 if (FileType.isNull()) {
3778 Error("FILE type is NULL");
3779 return;
3780 }
3781
3782 if (!Context.FILEDecl) {
3783 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
3784 Context.setFILEDecl(Typedef->getDecl());
3785 else {
3786 const TagType *Tag = FileType->getAs<TagType>();
3787 if (!Tag) {
3788 Error("Invalid FILE type in AST file");
3789 return;
3790 }
3791 Context.setFILEDecl(Tag->getDecl());
3792 }
3793 }
3794 }
3795
3796 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
3797 QualType Jmp_bufType = GetType(Jmp_buf);
3798 if (Jmp_bufType.isNull()) {
3799 Error("jmp_buf type is NULL");
3800 return;
3801 }
3802
3803 if (!Context.jmp_bufDecl) {
3804 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
3805 Context.setjmp_bufDecl(Typedef->getDecl());
3806 else {
3807 const TagType *Tag = Jmp_bufType->getAs<TagType>();
3808 if (!Tag) {
3809 Error("Invalid jmp_buf type in AST file");
3810 return;
3811 }
3812 Context.setjmp_bufDecl(Tag->getDecl());
3813 }
3814 }
3815 }
3816
3817 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
3818 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
3819 if (Sigjmp_bufType.isNull()) {
3820 Error("sigjmp_buf type is NULL");
3821 return;
3822 }
3823
3824 if (!Context.sigjmp_bufDecl) {
3825 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
3826 Context.setsigjmp_bufDecl(Typedef->getDecl());
3827 else {
3828 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
3829 assert(Tag && "Invalid sigjmp_buf type in AST file");
3830 Context.setsigjmp_bufDecl(Tag->getDecl());
3831 }
3832 }
3833 }
3834
3835 if (unsigned ObjCIdRedef
3836 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
3837 if (Context.ObjCIdRedefinitionType.isNull())
3838 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
3839 }
3840
3841 if (unsigned ObjCClassRedef
3842 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
3843 if (Context.ObjCClassRedefinitionType.isNull())
3844 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
3845 }
3846
3847 if (unsigned ObjCSelRedef
3848 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
3849 if (Context.ObjCSelRedefinitionType.isNull())
3850 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
3851 }
3852
3853 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
3854 QualType Ucontext_tType = GetType(Ucontext_t);
3855 if (Ucontext_tType.isNull()) {
3856 Error("ucontext_t type is NULL");
3857 return;
3858 }
3859
3860 if (!Context.ucontext_tDecl) {
3861 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
3862 Context.setucontext_tDecl(Typedef->getDecl());
3863 else {
3864 const TagType *Tag = Ucontext_tType->getAs<TagType>();
3865 assert(Tag && "Invalid ucontext_t type in AST file");
3866 Context.setucontext_tDecl(Tag->getDecl());
3867 }
3868 }
3869 }
3870 }
3871
3872 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
3873
3874 // If there were any CUDA special declarations, deserialize them.
3875 if (!CUDASpecialDeclRefs.empty()) {
3876 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
3877 Context.setcudaConfigureCallDecl(
3878 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
3879 }
Stephen Hines651f13c2014-04-23 16:59:28 -07003880
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003881 // Re-export any modules that were imported by a non-module AST file.
Stephen Hines651f13c2014-04-23 16:59:28 -07003882 // FIXME: This does not make macro-only imports visible again. It also doesn't
3883 // make #includes mapped to module imports visible.
3884 for (auto &Import : ImportedModules) {
3885 if (Module *Imported = getSubmodule(Import.ID))
Argyrios Kyrtzidis5ebcb202013-02-01 16:36:12 +00003886 makeModuleVisible(Imported, Module::AllVisible,
Stephen Hines651f13c2014-04-23 16:59:28 -07003887 /*ImportLoc=*/Import.ImportLoc,
Douglas Gregor906d66a2013-03-20 21:10:35 +00003888 /*Complain=*/false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003889 }
3890 ImportedModules.clear();
3891}
3892
3893void ASTReader::finalizeForWriting() {
3894 for (HiddenNamesMapType::iterator Hidden = HiddenNamesMap.begin(),
3895 HiddenEnd = HiddenNamesMap.end();
3896 Hidden != HiddenEnd; ++Hidden) {
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00003897 makeNamesVisible(Hidden->second, Hidden->first);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003898 }
3899 HiddenNamesMap.clear();
3900}
3901
Argyrios Kyrtzidisbdfdb1d2013-05-06 19:23:40 +00003902/// \brief Given a cursor at the start of an AST file, scan ahead and drop the
3903/// cursor into the start of the given block ID, returning false on success and
3904/// true on failure.
3905static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003906 while (1) {
3907 llvm::BitstreamEntry Entry = Cursor.advance();
3908 switch (Entry.Kind) {
3909 case llvm::BitstreamEntry::Error:
3910 case llvm::BitstreamEntry::EndBlock:
3911 return true;
3912
3913 case llvm::BitstreamEntry::Record:
3914 // Ignore top-level records.
3915 Cursor.skipRecord(Entry.ID);
3916 break;
3917
3918 case llvm::BitstreamEntry::SubBlock:
Argyrios Kyrtzidisbdfdb1d2013-05-06 19:23:40 +00003919 if (Entry.ID == BlockID) {
3920 if (Cursor.EnterSubBlock(BlockID))
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003921 return true;
3922 // Found it!
3923 return false;
3924 }
3925
3926 if (Cursor.SkipBlock())
3927 return true;
3928 }
3929 }
3930}
3931
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003932/// \brief Retrieve the name of the original source file name
3933/// directly from the AST file, without actually loading the AST
3934/// file.
3935std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
3936 FileManager &FileMgr,
3937 DiagnosticsEngine &Diags) {
3938 // Open the AST file.
3939 std::string ErrStr;
Stephen Hines651f13c2014-04-23 16:59:28 -07003940 std::unique_ptr<llvm::MemoryBuffer> Buffer;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003941 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
3942 if (!Buffer) {
3943 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ASTFileName << ErrStr;
3944 return std::string();
3945 }
3946
3947 // Initialize the stream
3948 llvm::BitstreamReader StreamFile;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003949 BitstreamCursor Stream;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003950 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
3951 (const unsigned char *)Buffer->getBufferEnd());
3952 Stream.init(StreamFile);
3953
3954 // Sniff for the signature.
3955 if (Stream.Read(8) != 'C' ||
3956 Stream.Read(8) != 'P' ||
3957 Stream.Read(8) != 'C' ||
3958 Stream.Read(8) != 'H') {
3959 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
3960 return std::string();
3961 }
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003962
Chris Lattner88bde502013-01-19 21:39:22 +00003963 // Scan for the CONTROL_BLOCK_ID block.
Argyrios Kyrtzidisbdfdb1d2013-05-06 19:23:40 +00003964 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003965 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
3966 return std::string();
Chris Lattner88bde502013-01-19 21:39:22 +00003967 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003968
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003969 // Scan for ORIGINAL_FILE inside the control block.
3970 RecordData Record;
Chris Lattner88bde502013-01-19 21:39:22 +00003971 while (1) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003972 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Chris Lattner88bde502013-01-19 21:39:22 +00003973 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
3974 return std::string();
3975
3976 if (Entry.Kind != llvm::BitstreamEntry::Record) {
3977 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
3978 return std::string();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003979 }
Chris Lattner88bde502013-01-19 21:39:22 +00003980
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003981 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003982 StringRef Blob;
3983 if (Stream.readRecord(Entry.ID, Record, &Blob) == ORIGINAL_FILE)
3984 return Blob.str();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003985 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003986}
3987
3988namespace {
3989 class SimplePCHValidator : public ASTReaderListener {
3990 const LangOptions &ExistingLangOpts;
3991 const TargetOptions &ExistingTargetOpts;
3992 const PreprocessorOptions &ExistingPPOpts;
3993 FileManager &FileMgr;
3994
3995 public:
3996 SimplePCHValidator(const LangOptions &ExistingLangOpts,
3997 const TargetOptions &ExistingTargetOpts,
3998 const PreprocessorOptions &ExistingPPOpts,
3999 FileManager &FileMgr)
4000 : ExistingLangOpts(ExistingLangOpts),
4001 ExistingTargetOpts(ExistingTargetOpts),
4002 ExistingPPOpts(ExistingPPOpts),
4003 FileMgr(FileMgr)
4004 {
4005 }
4006
Stephen Hines651f13c2014-04-23 16:59:28 -07004007 bool ReadLanguageOptions(const LangOptions &LangOpts,
4008 bool Complain) override {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004009 return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004010 }
Stephen Hines651f13c2014-04-23 16:59:28 -07004011 bool ReadTargetOptions(const TargetOptions &TargetOpts,
4012 bool Complain) override {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004013 return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004014 }
Stephen Hines651f13c2014-04-23 16:59:28 -07004015 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
4016 bool Complain,
4017 std::string &SuggestedPredefines) override {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004018 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr,
Argyrios Kyrtzidis65110ca2013-04-26 21:33:40 +00004019 SuggestedPredefines, ExistingLangOpts);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004020 }
4021 };
4022}
4023
4024bool ASTReader::readASTFileControlBlock(StringRef Filename,
4025 FileManager &FileMgr,
4026 ASTReaderListener &Listener) {
4027 // Open the AST file.
4028 std::string ErrStr;
Stephen Hines651f13c2014-04-23 16:59:28 -07004029 std::unique_ptr<llvm::MemoryBuffer> Buffer;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004030 Buffer.reset(FileMgr.getBufferForFile(Filename, &ErrStr));
4031 if (!Buffer) {
4032 return true;
4033 }
4034
4035 // Initialize the stream
4036 llvm::BitstreamReader StreamFile;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004037 BitstreamCursor Stream;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004038 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
4039 (const unsigned char *)Buffer->getBufferEnd());
4040 Stream.init(StreamFile);
4041
4042 // Sniff for the signature.
4043 if (Stream.Read(8) != 'C' ||
4044 Stream.Read(8) != 'P' ||
4045 Stream.Read(8) != 'C' ||
4046 Stream.Read(8) != 'H') {
4047 return true;
4048 }
4049
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004050 // Scan for the CONTROL_BLOCK_ID block.
Argyrios Kyrtzidisbdfdb1d2013-05-06 19:23:40 +00004051 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004052 return true;
Argyrios Kyrtzidisbdfdb1d2013-05-06 19:23:40 +00004053
4054 bool NeedsInputFiles = Listener.needsInputFileVisitation();
Stephen Hines651f13c2014-04-23 16:59:28 -07004055 bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
Argyrios Kyrtzidisbdfdb1d2013-05-06 19:23:40 +00004056 BitstreamCursor InputFilesCursor;
4057 if (NeedsInputFiles) {
4058 InputFilesCursor = Stream;
4059 if (SkipCursorToBlock(InputFilesCursor, INPUT_FILES_BLOCK_ID))
4060 return true;
4061
4062 // Read the abbreviations
4063 while (true) {
4064 uint64_t Offset = InputFilesCursor.GetCurrentBitNo();
4065 unsigned Code = InputFilesCursor.ReadCode();
4066
4067 // We expect all abbrevs to be at the start of the block.
4068 if (Code != llvm::bitc::DEFINE_ABBREV) {
4069 InputFilesCursor.JumpToBit(Offset);
4070 break;
4071 }
4072 InputFilesCursor.ReadAbbrevRecord();
4073 }
4074 }
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004075
4076 // Scan for ORIGINAL_FILE inside the control block.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004077 RecordData Record;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004078 while (1) {
4079 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
4080 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
4081 return false;
4082
4083 if (Entry.Kind != llvm::BitstreamEntry::Record)
4084 return true;
4085
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004086 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00004087 StringRef Blob;
4088 unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob);
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004089 switch ((ControlRecordTypes)RecCode) {
4090 case METADATA: {
4091 if (Record[0] != VERSION_MAJOR)
4092 return true;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004093
Douglas Gregorc544ba02013-03-27 16:47:18 +00004094 if (Listener.ReadFullVersionInformation(Blob))
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004095 return true;
Douglas Gregorc544ba02013-03-27 16:47:18 +00004096
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004097 break;
4098 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004099 case MODULE_NAME:
4100 Listener.ReadModuleName(Blob);
4101 break;
4102 case MODULE_MAP_FILE:
4103 Listener.ReadModuleMapFile(Blob);
4104 break;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004105 case LANGUAGE_OPTIONS:
4106 if (ParseLanguageOptions(Record, false, Listener))
4107 return true;
4108 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004109
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004110 case TARGET_OPTIONS:
4111 if (ParseTargetOptions(Record, false, Listener))
4112 return true;
4113 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004114
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004115 case DIAGNOSTIC_OPTIONS:
4116 if (ParseDiagnosticOptions(Record, false, Listener))
4117 return true;
4118 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004119
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004120 case FILE_SYSTEM_OPTIONS:
4121 if (ParseFileSystemOptions(Record, false, Listener))
4122 return true;
4123 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004124
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004125 case HEADER_SEARCH_OPTIONS:
4126 if (ParseHeaderSearchOptions(Record, false, Listener))
4127 return true;
4128 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004129
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004130 case PREPROCESSOR_OPTIONS: {
4131 std::string IgnoredSuggestedPredefines;
4132 if (ParsePreprocessorOptions(Record, false, Listener,
4133 IgnoredSuggestedPredefines))
4134 return true;
4135 break;
4136 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004137
Argyrios Kyrtzidisbdfdb1d2013-05-06 19:23:40 +00004138 case INPUT_FILE_OFFSETS: {
4139 if (!NeedsInputFiles)
4140 break;
4141
4142 unsigned NumInputFiles = Record[0];
4143 unsigned NumUserFiles = Record[1];
4144 const uint32_t *InputFileOffs = (const uint32_t *)Blob.data();
4145 for (unsigned I = 0; I != NumInputFiles; ++I) {
4146 // Go find this input file.
4147 bool isSystemFile = I >= NumUserFiles;
Stephen Hines651f13c2014-04-23 16:59:28 -07004148
4149 if (isSystemFile && !NeedsSystemInputFiles)
4150 break; // the rest are system input files
4151
Argyrios Kyrtzidisbdfdb1d2013-05-06 19:23:40 +00004152 BitstreamCursor &Cursor = InputFilesCursor;
4153 SavedStreamPosition SavedPosition(Cursor);
4154 Cursor.JumpToBit(InputFileOffs[I]);
4155
4156 unsigned Code = Cursor.ReadCode();
4157 RecordData Record;
4158 StringRef Blob;
4159 bool shouldContinue = false;
4160 switch ((InputFileRecordTypes)Cursor.readRecord(Code, Record, &Blob)) {
4161 case INPUT_FILE:
Stephen Hines651f13c2014-04-23 16:59:28 -07004162 bool Overridden = static_cast<bool>(Record[3]);
4163 shouldContinue = Listener.visitInputFile(Blob, isSystemFile, Overridden);
Argyrios Kyrtzidisbdfdb1d2013-05-06 19:23:40 +00004164 break;
4165 }
4166 if (!shouldContinue)
4167 break;
4168 }
4169 break;
4170 }
4171
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004172 default:
4173 // No other validation to perform.
4174 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004175 }
4176 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004177}
4178
4179
4180bool ASTReader::isAcceptableASTFile(StringRef Filename,
4181 FileManager &FileMgr,
4182 const LangOptions &LangOpts,
4183 const TargetOptions &TargetOpts,
4184 const PreprocessorOptions &PPOpts) {
4185 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts, FileMgr);
4186 return !readASTFileControlBlock(Filename, FileMgr, validator);
4187}
4188
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004189ASTReader::ASTReadResult
4190ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004191 // Enter the submodule block.
4192 if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
4193 Error("malformed submodule block record in AST file");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004194 return Failure;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004195 }
4196
4197 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
4198 bool First = true;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004199 Module *CurrentModule = nullptr;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004200 RecordData Record;
4201 while (true) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004202 llvm::BitstreamEntry Entry = F.Stream.advanceSkippingSubblocks();
4203
4204 switch (Entry.Kind) {
4205 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
4206 case llvm::BitstreamEntry::Error:
4207 Error("malformed block record in AST file");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004208 return Failure;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004209 case llvm::BitstreamEntry::EndBlock:
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004210 return Success;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004211 case llvm::BitstreamEntry::Record:
4212 // The interesting case.
4213 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004214 }
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004215
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004216 // Read a record.
Chris Lattnerb3ce3572013-01-20 02:38:54 +00004217 StringRef Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004218 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00004219 switch (F.Stream.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004220 default: // Default behavior: ignore.
4221 break;
4222
4223 case SUBMODULE_DEFINITION: {
4224 if (First) {
4225 Error("missing submodule metadata record at beginning of block");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004226 return Failure;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004227 }
4228
Douglas Gregor970e4412013-03-20 03:59:18 +00004229 if (Record.size() < 8) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004230 Error("malformed module definition");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004231 return Failure;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004232 }
4233
Chris Lattnerb3ce3572013-01-20 02:38:54 +00004234 StringRef Name = Blob;
Stephen Hines651f13c2014-04-23 16:59:28 -07004235 unsigned Idx = 0;
4236 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
4237 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
4238 bool IsFramework = Record[Idx++];
4239 bool IsExplicit = Record[Idx++];
4240 bool IsSystem = Record[Idx++];
4241 bool IsExternC = Record[Idx++];
4242 bool InferSubmodules = Record[Idx++];
4243 bool InferExplicitSubmodules = Record[Idx++];
4244 bool InferExportWildcard = Record[Idx++];
4245 bool ConfigMacrosExhaustive = Record[Idx++];
Douglas Gregor970e4412013-03-20 03:59:18 +00004246
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004247 Module *ParentModule = nullptr;
4248 const FileEntry *ModuleMap = nullptr;
4249 if (Parent) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004250 ParentModule = getSubmodule(Parent);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004251 ModuleMap = ParentModule->ModuleMap;
4252 }
4253
4254 if (!F.ModuleMapPath.empty())
4255 ModuleMap = FileMgr.getFile(F.ModuleMapPath);
4256
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004257 // Retrieve this (sub)module from the module map, creating it if
4258 // necessary.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004259 CurrentModule = ModMap.findOrCreateModule(Name, ParentModule, ModuleMap,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004260 IsFramework,
4261 IsExplicit).first;
4262 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
4263 if (GlobalIndex >= SubmodulesLoaded.size() ||
4264 SubmodulesLoaded[GlobalIndex]) {
4265 Error("too many submodules");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004266 return Failure;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004267 }
Douglas Gregor8bf778e2013-02-06 22:40:31 +00004268
Douglas Gregor677e15f2013-03-19 00:28:20 +00004269 if (!ParentModule) {
4270 if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
4271 if (CurFile != F.File) {
4272 if (!Diags.isDiagnosticInFlight()) {
4273 Diag(diag::err_module_file_conflict)
4274 << CurrentModule->getTopLevelModuleName()
4275 << CurFile->getName()
4276 << F.File->getName();
4277 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004278 return Failure;
Douglas Gregor8bf778e2013-02-06 22:40:31 +00004279 }
Douglas Gregor8bf778e2013-02-06 22:40:31 +00004280 }
Douglas Gregor677e15f2013-03-19 00:28:20 +00004281
4282 CurrentModule->setASTFile(F.File);
Douglas Gregor8bf778e2013-02-06 22:40:31 +00004283 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004284
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004285 CurrentModule->IsFromModuleFile = true;
4286 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
Stephen Hines651f13c2014-04-23 16:59:28 -07004287 CurrentModule->IsExternC = IsExternC;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004288 CurrentModule->InferSubmodules = InferSubmodules;
4289 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
4290 CurrentModule->InferExportWildcard = InferExportWildcard;
Douglas Gregor970e4412013-03-20 03:59:18 +00004291 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004292 if (DeserializationListener)
4293 DeserializationListener->ModuleRead(GlobalID, CurrentModule);
4294
4295 SubmodulesLoaded[GlobalIndex] = CurrentModule;
Douglas Gregorb6cbe512013-01-14 17:21:00 +00004296
Douglas Gregor906d66a2013-03-20 21:10:35 +00004297 // Clear out data that will be replaced by what is the module file.
Douglas Gregorb6cbe512013-01-14 17:21:00 +00004298 CurrentModule->LinkLibraries.clear();
Douglas Gregor970e4412013-03-20 03:59:18 +00004299 CurrentModule->ConfigMacros.clear();
Douglas Gregor906d66a2013-03-20 21:10:35 +00004300 CurrentModule->UnresolvedConflicts.clear();
4301 CurrentModule->Conflicts.clear();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004302 break;
4303 }
4304
4305 case SUBMODULE_UMBRELLA_HEADER: {
4306 if (First) {
4307 Error("missing submodule metadata record at beginning of block");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004308 return Failure;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004309 }
4310
4311 if (!CurrentModule)
4312 break;
4313
Chris Lattnerb3ce3572013-01-20 02:38:54 +00004314 if (const FileEntry *Umbrella = PP.getFileManager().getFile(Blob)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004315 if (!CurrentModule->getUmbrellaHeader())
4316 ModMap.setUmbrellaHeader(CurrentModule, Umbrella);
4317 else if (CurrentModule->getUmbrellaHeader() != Umbrella) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004318 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
4319 Error("mismatched umbrella headers in submodule");
4320 return OutOfDate;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004321 }
4322 }
4323 break;
4324 }
4325
4326 case SUBMODULE_HEADER: {
4327 if (First) {
4328 Error("missing submodule metadata record at beginning of block");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004329 return Failure;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004330 }
4331
4332 if (!CurrentModule)
4333 break;
4334
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00004335 // We lazily associate headers with their modules via the HeaderInfoTable.
4336 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
4337 // of complete filenames or remove it entirely.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004338 break;
4339 }
4340
4341 case SUBMODULE_EXCLUDED_HEADER: {
4342 if (First) {
4343 Error("missing submodule metadata record at beginning of block");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004344 return Failure;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004345 }
4346
4347 if (!CurrentModule)
4348 break;
4349
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00004350 // We lazily associate headers with their modules via the HeaderInfoTable.
4351 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
4352 // of complete filenames or remove it entirely.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004353 break;
4354 }
4355
Lawrence Crowlbc3f6282013-06-20 21:14:14 +00004356 case SUBMODULE_PRIVATE_HEADER: {
4357 if (First) {
4358 Error("missing submodule metadata record at beginning of block");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004359 return Failure;
Lawrence Crowlbc3f6282013-06-20 21:14:14 +00004360 }
4361
4362 if (!CurrentModule)
4363 break;
4364
4365 // We lazily associate headers with their modules via the HeaderInfoTable.
4366 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
4367 // of complete filenames or remove it entirely.
4368 break;
4369 }
4370
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004371 case SUBMODULE_TOPHEADER: {
4372 if (First) {
4373 Error("missing submodule metadata record at beginning of block");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004374 return Failure;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004375 }
4376
4377 if (!CurrentModule)
4378 break;
4379
Argyrios Kyrtzidisc1d22392013-03-13 21:13:43 +00004380 CurrentModule->addTopHeaderFilename(Blob);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004381 break;
4382 }
4383
4384 case SUBMODULE_UMBRELLA_DIR: {
4385 if (First) {
4386 Error("missing submodule metadata record at beginning of block");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004387 return Failure;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004388 }
4389
4390 if (!CurrentModule)
4391 break;
4392
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004393 if (const DirectoryEntry *Umbrella
Chris Lattnerb3ce3572013-01-20 02:38:54 +00004394 = PP.getFileManager().getDirectory(Blob)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004395 if (!CurrentModule->getUmbrellaDir())
4396 ModMap.setUmbrellaDir(CurrentModule, Umbrella);
4397 else if (CurrentModule->getUmbrellaDir() != Umbrella) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004398 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
4399 Error("mismatched umbrella directories in submodule");
4400 return OutOfDate;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004401 }
4402 }
4403 break;
4404 }
4405
4406 case SUBMODULE_METADATA: {
4407 if (!First) {
4408 Error("submodule metadata record not at beginning of block");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004409 return Failure;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004410 }
4411 First = false;
4412
4413 F.BaseSubmoduleID = getTotalNumSubmodules();
4414 F.LocalNumSubmodules = Record[0];
4415 unsigned LocalBaseSubmoduleID = Record[1];
4416 if (F.LocalNumSubmodules > 0) {
4417 // Introduce the global -> local mapping for submodules within this
4418 // module.
4419 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
4420
4421 // Introduce the local -> global mapping for submodules within this
4422 // module.
4423 F.SubmoduleRemap.insertOrReplace(
4424 std::make_pair(LocalBaseSubmoduleID,
4425 F.BaseSubmoduleID - LocalBaseSubmoduleID));
4426
4427 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
4428 }
4429 break;
4430 }
4431
4432 case SUBMODULE_IMPORTS: {
4433 if (First) {
4434 Error("missing submodule metadata record at beginning of block");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004435 return Failure;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004436 }
4437
4438 if (!CurrentModule)
4439 break;
4440
4441 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
Douglas Gregor906d66a2013-03-20 21:10:35 +00004442 UnresolvedModuleRef Unresolved;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004443 Unresolved.File = &F;
4444 Unresolved.Mod = CurrentModule;
4445 Unresolved.ID = Record[Idx];
Douglas Gregor906d66a2013-03-20 21:10:35 +00004446 Unresolved.Kind = UnresolvedModuleRef::Import;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004447 Unresolved.IsWildcard = false;
Douglas Gregor906d66a2013-03-20 21:10:35 +00004448 UnresolvedModuleRefs.push_back(Unresolved);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004449 }
4450 break;
4451 }
4452
4453 case SUBMODULE_EXPORTS: {
4454 if (First) {
4455 Error("missing submodule metadata record at beginning of block");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004456 return Failure;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004457 }
4458
4459 if (!CurrentModule)
4460 break;
4461
4462 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
Douglas Gregor906d66a2013-03-20 21:10:35 +00004463 UnresolvedModuleRef Unresolved;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004464 Unresolved.File = &F;
4465 Unresolved.Mod = CurrentModule;
4466 Unresolved.ID = Record[Idx];
Douglas Gregor906d66a2013-03-20 21:10:35 +00004467 Unresolved.Kind = UnresolvedModuleRef::Export;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004468 Unresolved.IsWildcard = Record[Idx + 1];
Douglas Gregor906d66a2013-03-20 21:10:35 +00004469 UnresolvedModuleRefs.push_back(Unresolved);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004470 }
4471
4472 // Once we've loaded the set of exports, there's no reason to keep
4473 // the parsed, unresolved exports around.
4474 CurrentModule->UnresolvedExports.clear();
4475 break;
4476 }
4477 case SUBMODULE_REQUIRES: {
4478 if (First) {
4479 Error("missing submodule metadata record at beginning of block");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004480 return Failure;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004481 }
4482
4483 if (!CurrentModule)
4484 break;
4485
Richard Smith5794b532013-10-28 22:18:19 +00004486 CurrentModule->addRequirement(Blob, Record[0], Context.getLangOpts(),
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004487 Context.getTargetInfo());
4488 break;
4489 }
Douglas Gregorb6cbe512013-01-14 17:21:00 +00004490
4491 case SUBMODULE_LINK_LIBRARY:
4492 if (First) {
4493 Error("missing submodule metadata record at beginning of block");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004494 return Failure;
Douglas Gregorb6cbe512013-01-14 17:21:00 +00004495 }
4496
4497 if (!CurrentModule)
4498 break;
4499
4500 CurrentModule->LinkLibraries.push_back(
Chris Lattnerb3ce3572013-01-20 02:38:54 +00004501 Module::LinkLibrary(Blob, Record[0]));
Douglas Gregorb6cbe512013-01-14 17:21:00 +00004502 break;
Douglas Gregor63a72682013-03-20 00:22:05 +00004503
4504 case SUBMODULE_CONFIG_MACRO:
4505 if (First) {
4506 Error("missing submodule metadata record at beginning of block");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004507 return Failure;
Douglas Gregor63a72682013-03-20 00:22:05 +00004508 }
4509
4510 if (!CurrentModule)
4511 break;
4512
4513 CurrentModule->ConfigMacros.push_back(Blob.str());
4514 break;
Douglas Gregor906d66a2013-03-20 21:10:35 +00004515
4516 case SUBMODULE_CONFLICT: {
4517 if (First) {
4518 Error("missing submodule metadata record at beginning of block");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004519 return Failure;
Douglas Gregor906d66a2013-03-20 21:10:35 +00004520 }
4521
4522 if (!CurrentModule)
4523 break;
4524
4525 UnresolvedModuleRef Unresolved;
4526 Unresolved.File = &F;
4527 Unresolved.Mod = CurrentModule;
4528 Unresolved.ID = Record[0];
4529 Unresolved.Kind = UnresolvedModuleRef::Conflict;
4530 Unresolved.IsWildcard = false;
4531 Unresolved.String = Blob;
4532 UnresolvedModuleRefs.push_back(Unresolved);
4533 break;
4534 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004535 }
4536 }
4537}
4538
4539/// \brief Parse the record that corresponds to a LangOptions data
4540/// structure.
4541///
4542/// This routine parses the language options from the AST file and then gives
4543/// them to the AST listener if one is set.
4544///
4545/// \returns true if the listener deems the file unacceptable, false otherwise.
4546bool ASTReader::ParseLanguageOptions(const RecordData &Record,
4547 bool Complain,
4548 ASTReaderListener &Listener) {
4549 LangOptions LangOpts;
4550 unsigned Idx = 0;
4551#define LANGOPT(Name, Bits, Default, Description) \
4552 LangOpts.Name = Record[Idx++];
4553#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
4554 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
4555#include "clang/Basic/LangOptions.def"
Will Dietz4f45bc02013-01-18 11:30:38 +00004556#define SANITIZER(NAME, ID) LangOpts.Sanitize.ID = Record[Idx++];
4557#include "clang/Basic/Sanitizers.def"
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004558
4559 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
4560 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
4561 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
4562
4563 unsigned Length = Record[Idx++];
4564 LangOpts.CurrentModule.assign(Record.begin() + Idx,
4565 Record.begin() + Idx + Length);
Dmitri Gribenko6ebf0912013-02-22 14:21:27 +00004566
4567 Idx += Length;
4568
4569 // Comment options.
4570 for (unsigned N = Record[Idx++]; N; --N) {
4571 LangOpts.CommentOpts.BlockCommandNames.push_back(
4572 ReadString(Record, Idx));
4573 }
Dmitri Gribenko6fd7d302013-04-10 15:35:17 +00004574 LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
Dmitri Gribenko6ebf0912013-02-22 14:21:27 +00004575
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004576 return Listener.ReadLanguageOptions(LangOpts, Complain);
4577}
4578
4579bool ASTReader::ParseTargetOptions(const RecordData &Record,
4580 bool Complain,
4581 ASTReaderListener &Listener) {
4582 unsigned Idx = 0;
4583 TargetOptions TargetOpts;
4584 TargetOpts.Triple = ReadString(Record, Idx);
4585 TargetOpts.CPU = ReadString(Record, Idx);
4586 TargetOpts.ABI = ReadString(Record, Idx);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004587 for (unsigned N = Record[Idx++]; N; --N) {
4588 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
4589 }
4590 for (unsigned N = Record[Idx++]; N; --N) {
4591 TargetOpts.Features.push_back(ReadString(Record, Idx));
4592 }
4593
4594 return Listener.ReadTargetOptions(TargetOpts, Complain);
4595}
4596
4597bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
4598 ASTReaderListener &Listener) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004599 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004600 unsigned Idx = 0;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004601#define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++];
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004602#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004603 DiagOpts->set##Name(static_cast<Type>(Record[Idx++]));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004604#include "clang/Basic/DiagnosticOptions.def"
4605
4606 for (unsigned N = Record[Idx++]; N; --N) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004607 DiagOpts->Warnings.push_back(ReadString(Record, Idx));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004608 }
4609
4610 return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
4611}
4612
4613bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
4614 ASTReaderListener &Listener) {
4615 FileSystemOptions FSOpts;
4616 unsigned Idx = 0;
4617 FSOpts.WorkingDir = ReadString(Record, Idx);
4618 return Listener.ReadFileSystemOptions(FSOpts, Complain);
4619}
4620
4621bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
4622 bool Complain,
4623 ASTReaderListener &Listener) {
4624 HeaderSearchOptions HSOpts;
4625 unsigned Idx = 0;
4626 HSOpts.Sysroot = ReadString(Record, Idx);
4627
4628 // Include entries.
4629 for (unsigned N = Record[Idx++]; N; --N) {
4630 std::string Path = ReadString(Record, Idx);
4631 frontend::IncludeDirGroup Group
4632 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004633 bool IsFramework = Record[Idx++];
4634 bool IgnoreSysRoot = Record[Idx++];
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004635 HSOpts.UserEntries.push_back(
Daniel Dunbar59fd6352013-01-30 00:34:26 +00004636 HeaderSearchOptions::Entry(Path, Group, IsFramework, IgnoreSysRoot));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004637 }
4638
4639 // System header prefixes.
4640 for (unsigned N = Record[Idx++]; N; --N) {
4641 std::string Prefix = ReadString(Record, Idx);
4642 bool IsSystemHeader = Record[Idx++];
4643 HSOpts.SystemHeaderPrefixes.push_back(
4644 HeaderSearchOptions::SystemHeaderPrefix(Prefix, IsSystemHeader));
4645 }
4646
4647 HSOpts.ResourceDir = ReadString(Record, Idx);
4648 HSOpts.ModuleCachePath = ReadString(Record, Idx);
Stephen Hines651f13c2014-04-23 16:59:28 -07004649 HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004650 HSOpts.DisableModuleHash = Record[Idx++];
4651 HSOpts.UseBuiltinIncludes = Record[Idx++];
4652 HSOpts.UseStandardSystemIncludes = Record[Idx++];
4653 HSOpts.UseStandardCXXIncludes = Record[Idx++];
4654 HSOpts.UseLibcxx = Record[Idx++];
4655
4656 return Listener.ReadHeaderSearchOptions(HSOpts, Complain);
4657}
4658
4659bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
4660 bool Complain,
4661 ASTReaderListener &Listener,
4662 std::string &SuggestedPredefines) {
4663 PreprocessorOptions PPOpts;
4664 unsigned Idx = 0;
4665
4666 // Macro definitions/undefs
4667 for (unsigned N = Record[Idx++]; N; --N) {
4668 std::string Macro = ReadString(Record, Idx);
4669 bool IsUndef = Record[Idx++];
4670 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
4671 }
4672
4673 // Includes
4674 for (unsigned N = Record[Idx++]; N; --N) {
4675 PPOpts.Includes.push_back(ReadString(Record, Idx));
4676 }
4677
4678 // Macro Includes
4679 for (unsigned N = Record[Idx++]; N; --N) {
4680 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
4681 }
4682
4683 PPOpts.UsePredefines = Record[Idx++];
Argyrios Kyrtzidis65110ca2013-04-26 21:33:40 +00004684 PPOpts.DetailedRecord = Record[Idx++];
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004685 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
4686 PPOpts.ImplicitPTHInclude = ReadString(Record, Idx);
4687 PPOpts.ObjCXXARCStandardLibrary =
4688 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
4689 SuggestedPredefines.clear();
4690 return Listener.ReadPreprocessorOptions(PPOpts, Complain,
4691 SuggestedPredefines);
4692}
4693
4694std::pair<ModuleFile *, unsigned>
4695ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
4696 GlobalPreprocessedEntityMapType::iterator
4697 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
4698 assert(I != GlobalPreprocessedEntityMap.end() &&
4699 "Corrupted global preprocessed entity map");
4700 ModuleFile *M = I->second;
4701 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
4702 return std::make_pair(M, LocalIndex);
4703}
4704
4705std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
4706ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
4707 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
4708 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
4709 Mod.NumPreprocessedEntities);
4710
4711 return std::make_pair(PreprocessingRecord::iterator(),
4712 PreprocessingRecord::iterator());
4713}
4714
4715std::pair<ASTReader::ModuleDeclIterator, ASTReader::ModuleDeclIterator>
4716ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
4717 return std::make_pair(ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
4718 ModuleDeclIterator(this, &Mod,
4719 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
4720}
4721
4722PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
4723 PreprocessedEntityID PPID = Index+1;
4724 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
4725 ModuleFile &M = *PPInfo.first;
4726 unsigned LocalIndex = PPInfo.second;
4727 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
4728
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004729 if (!PP.getPreprocessingRecord()) {
4730 Error("no preprocessing record");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004731 return nullptr;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004732 }
4733
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004734 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
4735 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
4736
4737 llvm::BitstreamEntry Entry =
4738 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
4739 if (Entry.Kind != llvm::BitstreamEntry::Record)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004740 return nullptr;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004741
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004742 // Read the record.
4743 SourceRange Range(ReadSourceLocation(M, PPOffs.Begin),
4744 ReadSourceLocation(M, PPOffs.End));
4745 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00004746 StringRef Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004747 RecordData Record;
4748 PreprocessorDetailRecordTypes RecType =
Chris Lattnerb3ce3572013-01-20 02:38:54 +00004749 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.readRecord(
4750 Entry.ID, Record, &Blob);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004751 switch (RecType) {
4752 case PPD_MACRO_EXPANSION: {
4753 bool isBuiltin = Record[0];
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004754 IdentifierInfo *Name = nullptr;
4755 MacroDefinition *Def = nullptr;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004756 if (isBuiltin)
4757 Name = getLocalIdentifier(M, Record[1]);
4758 else {
4759 PreprocessedEntityID
4760 GlobalID = getGlobalPreprocessedEntityID(M, Record[1]);
4761 Def =cast<MacroDefinition>(PPRec.getLoadedPreprocessedEntity(GlobalID-1));
4762 }
4763
4764 MacroExpansion *ME;
4765 if (isBuiltin)
4766 ME = new (PPRec) MacroExpansion(Name, Range);
4767 else
4768 ME = new (PPRec) MacroExpansion(Def, Range);
4769
4770 return ME;
4771 }
4772
4773 case PPD_MACRO_DEFINITION: {
4774 // Decode the identifier info and then check again; if the macro is
4775 // still defined and associated with the identifier,
4776 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
4777 MacroDefinition *MD
4778 = new (PPRec) MacroDefinition(II, Range);
4779
4780 if (DeserializationListener)
4781 DeserializationListener->MacroDefinitionRead(PPID, MD);
4782
4783 return MD;
4784 }
4785
4786 case PPD_INCLUSION_DIRECTIVE: {
Chris Lattnerb3ce3572013-01-20 02:38:54 +00004787 const char *FullFileNameStart = Blob.data() + Record[0];
4788 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004789 const FileEntry *File = nullptr;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004790 if (!FullFileName.empty())
4791 File = PP.getFileManager().getFile(FullFileName);
4792
4793 // FIXME: Stable encoding
4794 InclusionDirective::InclusionKind Kind
4795 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
4796 InclusionDirective *ID
4797 = new (PPRec) InclusionDirective(PPRec, Kind,
Chris Lattnerb3ce3572013-01-20 02:38:54 +00004798 StringRef(Blob.data(), Record[0]),
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004799 Record[1], Record[3],
4800 File,
4801 Range);
4802 return ID;
4803 }
4804 }
4805
4806 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
4807}
4808
4809/// \brief \arg SLocMapI points at a chunk of a module that contains no
4810/// preprocessed entities or the entities it contains are not the ones we are
4811/// looking for. Find the next module that contains entities and return the ID
4812/// of the first entry.
4813PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
4814 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
4815 ++SLocMapI;
4816 for (GlobalSLocOffsetMapType::const_iterator
4817 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
4818 ModuleFile &M = *SLocMapI->second;
4819 if (M.NumPreprocessedEntities)
4820 return M.BasePreprocessedEntityID;
4821 }
4822
4823 return getTotalNumPreprocessedEntities();
4824}
4825
4826namespace {
4827
4828template <unsigned PPEntityOffset::*PPLoc>
4829struct PPEntityComp {
4830 const ASTReader &Reader;
4831 ModuleFile &M;
4832
4833 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) { }
4834
4835 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
4836 SourceLocation LHS = getLoc(L);
4837 SourceLocation RHS = getLoc(R);
4838 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4839 }
4840
4841 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
4842 SourceLocation LHS = getLoc(L);
4843 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4844 }
4845
4846 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
4847 SourceLocation RHS = getLoc(R);
4848 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4849 }
4850
4851 SourceLocation getLoc(const PPEntityOffset &PPE) const {
4852 return Reader.ReadSourceLocation(M, PPE.*PPLoc);
4853 }
4854};
4855
4856}
4857
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004858PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
4859 bool EndsAfter) const {
4860 if (SourceMgr.isLocalSourceLocation(Loc))
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004861 return getTotalNumPreprocessedEntities();
4862
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004863 GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
4864 SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004865 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
4866 "Corrupted global sloc offset map");
4867
4868 if (SLocMapI->second->NumPreprocessedEntities == 0)
4869 return findNextPreprocessedEntity(SLocMapI);
4870
4871 ModuleFile &M = *SLocMapI->second;
4872 typedef const PPEntityOffset *pp_iterator;
4873 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
4874 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
4875
4876 size_t Count = M.NumPreprocessedEntities;
4877 size_t Half;
4878 pp_iterator First = pp_begin;
4879 pp_iterator PPI;
4880
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004881 if (EndsAfter) {
4882 PPI = std::upper_bound(pp_begin, pp_end, Loc,
4883 PPEntityComp<&PPEntityOffset::Begin>(*this, M));
4884 } else {
4885 // Do a binary search manually instead of using std::lower_bound because
4886 // The end locations of entities may be unordered (when a macro expansion
4887 // is inside another macro argument), but for this case it is not important
4888 // whether we get the first macro expansion or its containing macro.
4889 while (Count > 0) {
4890 Half = Count / 2;
4891 PPI = First;
4892 std::advance(PPI, Half);
4893 if (SourceMgr.isBeforeInTranslationUnit(ReadSourceLocation(M, PPI->End),
4894 Loc)) {
4895 First = PPI;
4896 ++First;
4897 Count = Count - Half - 1;
4898 } else
4899 Count = Half;
4900 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004901 }
4902
4903 if (PPI == pp_end)
4904 return findNextPreprocessedEntity(SLocMapI);
4905
4906 return M.BasePreprocessedEntityID + (PPI - pp_begin);
4907}
4908
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004909/// \brief Returns a pair of [Begin, End) indices of preallocated
4910/// preprocessed entities that \arg Range encompasses.
4911std::pair<unsigned, unsigned>
4912 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
4913 if (Range.isInvalid())
4914 return std::make_pair(0,0);
4915 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
4916
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004917 PreprocessedEntityID BeginID =
4918 findPreprocessedEntity(Range.getBegin(), false);
4919 PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004920 return std::make_pair(BeginID, EndID);
4921}
4922
4923/// \brief Optionally returns true or false if the preallocated preprocessed
4924/// entity with index \arg Index came from file \arg FID.
David Blaikiedc84cd52013-02-20 22:23:23 +00004925Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004926 FileID FID) {
4927 if (FID.isInvalid())
4928 return false;
4929
4930 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
4931 ModuleFile &M = *PPInfo.first;
4932 unsigned LocalIndex = PPInfo.second;
4933 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
4934
4935 SourceLocation Loc = ReadSourceLocation(M, PPOffs.Begin);
4936 if (Loc.isInvalid())
4937 return false;
4938
4939 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
4940 return true;
4941 else
4942 return false;
4943}
4944
4945namespace {
4946 /// \brief Visitor used to search for information about a header file.
4947 class HeaderFileInfoVisitor {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004948 const FileEntry *FE;
4949
David Blaikiedc84cd52013-02-20 22:23:23 +00004950 Optional<HeaderFileInfo> HFI;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004951
4952 public:
Argyrios Kyrtzidis36592b12013-03-06 18:12:44 +00004953 explicit HeaderFileInfoVisitor(const FileEntry *FE)
4954 : FE(FE) { }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004955
4956 static bool visit(ModuleFile &M, void *UserData) {
4957 HeaderFileInfoVisitor *This
4958 = static_cast<HeaderFileInfoVisitor *>(UserData);
4959
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004960 HeaderFileInfoLookupTable *Table
4961 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
4962 if (!Table)
4963 return false;
4964
4965 // Look in the on-disk hash table for an entry for this file name.
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00004966 HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004967 if (Pos == Table->end())
4968 return false;
4969
4970 This->HFI = *Pos;
4971 return true;
4972 }
4973
David Blaikiedc84cd52013-02-20 22:23:23 +00004974 Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004975 };
4976}
4977
4978HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
Argyrios Kyrtzidis36592b12013-03-06 18:12:44 +00004979 HeaderFileInfoVisitor Visitor(FE);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004980 ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor);
Argyrios Kyrtzidisf9ba8512013-05-08 23:46:55 +00004981 if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004982 return *HFI;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004983
4984 return HeaderFileInfo();
4985}
4986
4987void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
4988 // FIXME: Make it work properly with modules.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00004989 SmallVector<DiagnosticsEngine::DiagState *, 32> DiagStates;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004990 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
4991 ModuleFile &F = *(*I);
4992 unsigned Idx = 0;
4993 DiagStates.clear();
4994 assert(!Diag.DiagStates.empty());
4995 DiagStates.push_back(&Diag.DiagStates.front()); // the command-line one.
4996 while (Idx < F.PragmaDiagMappings.size()) {
4997 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
4998 unsigned DiagStateID = F.PragmaDiagMappings[Idx++];
4999 if (DiagStateID != 0) {
5000 Diag.DiagStatePoints.push_back(
5001 DiagnosticsEngine::DiagStatePoint(DiagStates[DiagStateID-1],
5002 FullSourceLoc(Loc, SourceMgr)));
5003 continue;
5004 }
5005
5006 assert(DiagStateID == 0);
5007 // A new DiagState was created here.
5008 Diag.DiagStates.push_back(*Diag.GetCurDiagState());
5009 DiagnosticsEngine::DiagState *NewState = &Diag.DiagStates.back();
5010 DiagStates.push_back(NewState);
5011 Diag.DiagStatePoints.push_back(
5012 DiagnosticsEngine::DiagStatePoint(NewState,
5013 FullSourceLoc(Loc, SourceMgr)));
5014 while (1) {
5015 assert(Idx < F.PragmaDiagMappings.size() &&
5016 "Invalid data, didn't find '-1' marking end of diag/map pairs");
5017 if (Idx >= F.PragmaDiagMappings.size()) {
5018 break; // Something is messed up but at least avoid infinite loop in
5019 // release build.
5020 }
5021 unsigned DiagID = F.PragmaDiagMappings[Idx++];
5022 if (DiagID == (unsigned)-1) {
5023 break; // no more diag/map pairs for this location.
5024 }
5025 diag::Mapping Map = (diag::Mapping)F.PragmaDiagMappings[Idx++];
5026 DiagnosticMappingInfo MappingInfo = Diag.makeMappingInfo(Map, Loc);
5027 Diag.GetCurDiagState()->setMappingInfo(DiagID, MappingInfo);
5028 }
5029 }
5030 }
5031}
5032
5033/// \brief Get the correct cursor and offset for loading a type.
5034ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
5035 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
5036 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
5037 ModuleFile *M = I->second;
5038 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
5039}
5040
5041/// \brief Read and return the type with the given index..
5042///
5043/// The index is the type ID, shifted and minus the number of predefs. This
5044/// routine actually reads the record corresponding to the type at the given
5045/// location. It is a helper routine for GetType, which deals with reading type
5046/// IDs.
5047QualType ASTReader::readTypeRecord(unsigned Index) {
5048 RecordLocation Loc = TypeCursorForIndex(Index);
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00005049 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005050
5051 // Keep track of where we are in the stream, then jump back there
5052 // after reading this type.
5053 SavedStreamPosition SavedPosition(DeclsCursor);
5054
5055 ReadingKindTracker ReadingKind(Read_Type, *this);
5056
5057 // Note that we are loading a type record.
5058 Deserializing AType(this);
5059
5060 unsigned Idx = 0;
5061 DeclsCursor.JumpToBit(Loc.Offset);
5062 RecordData Record;
5063 unsigned Code = DeclsCursor.ReadCode();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00005064 switch ((TypeCode)DeclsCursor.readRecord(Code, Record)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005065 case TYPE_EXT_QUAL: {
5066 if (Record.size() != 2) {
5067 Error("Incorrect encoding of extended qualifier type");
5068 return QualType();
5069 }
5070 QualType Base = readType(*Loc.F, Record, Idx);
5071 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
5072 return Context.getQualifiedType(Base, Quals);
5073 }
5074
5075 case TYPE_COMPLEX: {
5076 if (Record.size() != 1) {
5077 Error("Incorrect encoding of complex type");
5078 return QualType();
5079 }
5080 QualType ElemType = readType(*Loc.F, Record, Idx);
5081 return Context.getComplexType(ElemType);
5082 }
5083
5084 case TYPE_POINTER: {
5085 if (Record.size() != 1) {
5086 Error("Incorrect encoding of pointer type");
5087 return QualType();
5088 }
5089 QualType PointeeType = readType(*Loc.F, Record, Idx);
5090 return Context.getPointerType(PointeeType);
5091 }
5092
Reid Kleckner12df2462013-06-24 17:51:48 +00005093 case TYPE_DECAYED: {
5094 if (Record.size() != 1) {
5095 Error("Incorrect encoding of decayed type");
5096 return QualType();
5097 }
5098 QualType OriginalType = readType(*Loc.F, Record, Idx);
5099 QualType DT = Context.getAdjustedParameterType(OriginalType);
5100 if (!isa<DecayedType>(DT))
5101 Error("Decayed type does not decay");
5102 return DT;
5103 }
5104
Stephen Hines651f13c2014-04-23 16:59:28 -07005105 case TYPE_ADJUSTED: {
5106 if (Record.size() != 2) {
5107 Error("Incorrect encoding of adjusted type");
5108 return QualType();
5109 }
5110 QualType OriginalTy = readType(*Loc.F, Record, Idx);
5111 QualType AdjustedTy = readType(*Loc.F, Record, Idx);
5112 return Context.getAdjustedType(OriginalTy, AdjustedTy);
5113 }
5114
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005115 case TYPE_BLOCK_POINTER: {
5116 if (Record.size() != 1) {
5117 Error("Incorrect encoding of block pointer type");
5118 return QualType();
5119 }
5120 QualType PointeeType = readType(*Loc.F, Record, Idx);
5121 return Context.getBlockPointerType(PointeeType);
5122 }
5123
5124 case TYPE_LVALUE_REFERENCE: {
5125 if (Record.size() != 2) {
5126 Error("Incorrect encoding of lvalue reference type");
5127 return QualType();
5128 }
5129 QualType PointeeType = readType(*Loc.F, Record, Idx);
5130 return Context.getLValueReferenceType(PointeeType, Record[1]);
5131 }
5132
5133 case TYPE_RVALUE_REFERENCE: {
5134 if (Record.size() != 1) {
5135 Error("Incorrect encoding of rvalue reference type");
5136 return QualType();
5137 }
5138 QualType PointeeType = readType(*Loc.F, Record, Idx);
5139 return Context.getRValueReferenceType(PointeeType);
5140 }
5141
5142 case TYPE_MEMBER_POINTER: {
5143 if (Record.size() != 2) {
5144 Error("Incorrect encoding of member pointer type");
5145 return QualType();
5146 }
5147 QualType PointeeType = readType(*Loc.F, Record, Idx);
5148 QualType ClassType = readType(*Loc.F, Record, Idx);
5149 if (PointeeType.isNull() || ClassType.isNull())
5150 return QualType();
5151
5152 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
5153 }
5154
5155 case TYPE_CONSTANT_ARRAY: {
5156 QualType ElementType = readType(*Loc.F, Record, Idx);
5157 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
5158 unsigned IndexTypeQuals = Record[2];
5159 unsigned Idx = 3;
5160 llvm::APInt Size = ReadAPInt(Record, Idx);
5161 return Context.getConstantArrayType(ElementType, Size,
5162 ASM, IndexTypeQuals);
5163 }
5164
5165 case TYPE_INCOMPLETE_ARRAY: {
5166 QualType ElementType = readType(*Loc.F, Record, Idx);
5167 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
5168 unsigned IndexTypeQuals = Record[2];
5169 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
5170 }
5171
5172 case TYPE_VARIABLE_ARRAY: {
5173 QualType ElementType = readType(*Loc.F, Record, Idx);
5174 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
5175 unsigned IndexTypeQuals = Record[2];
5176 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
5177 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
5178 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
5179 ASM, IndexTypeQuals,
5180 SourceRange(LBLoc, RBLoc));
5181 }
5182
5183 case TYPE_VECTOR: {
5184 if (Record.size() != 3) {
5185 Error("incorrect encoding of vector type in AST file");
5186 return QualType();
5187 }
5188
5189 QualType ElementType = readType(*Loc.F, Record, Idx);
5190 unsigned NumElements = Record[1];
5191 unsigned VecKind = Record[2];
5192 return Context.getVectorType(ElementType, NumElements,
5193 (VectorType::VectorKind)VecKind);
5194 }
5195
5196 case TYPE_EXT_VECTOR: {
5197 if (Record.size() != 3) {
5198 Error("incorrect encoding of extended vector type in AST file");
5199 return QualType();
5200 }
5201
5202 QualType ElementType = readType(*Loc.F, Record, Idx);
5203 unsigned NumElements = Record[1];
5204 return Context.getExtVectorType(ElementType, NumElements);
5205 }
5206
5207 case TYPE_FUNCTION_NO_PROTO: {
5208 if (Record.size() != 6) {
5209 Error("incorrect encoding of no-proto function type");
5210 return QualType();
5211 }
5212 QualType ResultType = readType(*Loc.F, Record, Idx);
5213 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
5214 (CallingConv)Record[4], Record[5]);
5215 return Context.getFunctionNoProtoType(ResultType, Info);
5216 }
5217
5218 case TYPE_FUNCTION_PROTO: {
5219 QualType ResultType = readType(*Loc.F, Record, Idx);
5220
5221 FunctionProtoType::ExtProtoInfo EPI;
5222 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
5223 /*hasregparm*/ Record[2],
5224 /*regparm*/ Record[3],
5225 static_cast<CallingConv>(Record[4]),
5226 /*produces*/ Record[5]);
5227
5228 unsigned Idx = 6;
5229 unsigned NumParams = Record[Idx++];
5230 SmallVector<QualType, 16> ParamTypes;
5231 for (unsigned I = 0; I != NumParams; ++I)
5232 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
5233
5234 EPI.Variadic = Record[Idx++];
5235 EPI.HasTrailingReturn = Record[Idx++];
5236 EPI.TypeQuals = Record[Idx++];
5237 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
Stephen Hines651f13c2014-04-23 16:59:28 -07005238 SmallVector<QualType, 8> ExceptionStorage;
5239 readExceptionSpec(*Loc.F, ExceptionStorage, EPI, Record, Idx);
Jordan Rosebea522f2013-03-08 21:51:21 +00005240 return Context.getFunctionType(ResultType, ParamTypes, EPI);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005241 }
5242
5243 case TYPE_UNRESOLVED_USING: {
5244 unsigned Idx = 0;
5245 return Context.getTypeDeclType(
5246 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
5247 }
5248
5249 case TYPE_TYPEDEF: {
5250 if (Record.size() != 2) {
5251 Error("incorrect encoding of typedef type");
5252 return QualType();
5253 }
5254 unsigned Idx = 0;
5255 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
5256 QualType Canonical = readType(*Loc.F, Record, Idx);
5257 if (!Canonical.isNull())
5258 Canonical = Context.getCanonicalType(Canonical);
5259 return Context.getTypedefType(Decl, Canonical);
5260 }
5261
5262 case TYPE_TYPEOF_EXPR:
5263 return Context.getTypeOfExprType(ReadExpr(*Loc.F));
5264
5265 case TYPE_TYPEOF: {
5266 if (Record.size() != 1) {
5267 Error("incorrect encoding of typeof(type) in AST file");
5268 return QualType();
5269 }
5270 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
5271 return Context.getTypeOfType(UnderlyingType);
5272 }
5273
5274 case TYPE_DECLTYPE: {
5275 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
5276 return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
5277 }
5278
5279 case TYPE_UNARY_TRANSFORM: {
5280 QualType BaseType = readType(*Loc.F, Record, Idx);
5281 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
5282 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
5283 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
5284 }
5285
Richard Smitha2c36462013-04-26 16:15:35 +00005286 case TYPE_AUTO: {
5287 QualType Deduced = readType(*Loc.F, Record, Idx);
5288 bool IsDecltypeAuto = Record[Idx++];
Richard Smithdc7a4f52013-04-30 13:56:41 +00005289 bool IsDependent = Deduced.isNull() ? Record[Idx++] : false;
Manuel Klimek152b4e42013-08-22 12:12:24 +00005290 return Context.getAutoType(Deduced, IsDecltypeAuto, IsDependent);
Richard Smitha2c36462013-04-26 16:15:35 +00005291 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005292
5293 case TYPE_RECORD: {
5294 if (Record.size() != 2) {
5295 Error("incorrect encoding of record type");
5296 return QualType();
5297 }
5298 unsigned Idx = 0;
5299 bool IsDependent = Record[Idx++];
5300 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
5301 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
5302 QualType T = Context.getRecordType(RD);
5303 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
5304 return T;
5305 }
5306
5307 case TYPE_ENUM: {
5308 if (Record.size() != 2) {
5309 Error("incorrect encoding of enum type");
5310 return QualType();
5311 }
5312 unsigned Idx = 0;
5313 bool IsDependent = Record[Idx++];
5314 QualType T
5315 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
5316 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
5317 return T;
5318 }
5319
5320 case TYPE_ATTRIBUTED: {
5321 if (Record.size() != 3) {
5322 Error("incorrect encoding of attributed type");
5323 return QualType();
5324 }
5325 QualType modifiedType = readType(*Loc.F, Record, Idx);
5326 QualType equivalentType = readType(*Loc.F, Record, Idx);
5327 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
5328 return Context.getAttributedType(kind, modifiedType, equivalentType);
5329 }
5330
5331 case TYPE_PAREN: {
5332 if (Record.size() != 1) {
5333 Error("incorrect encoding of paren type");
5334 return QualType();
5335 }
5336 QualType InnerType = readType(*Loc.F, Record, Idx);
5337 return Context.getParenType(InnerType);
5338 }
5339
5340 case TYPE_PACK_EXPANSION: {
5341 if (Record.size() != 2) {
5342 Error("incorrect encoding of pack expansion type");
5343 return QualType();
5344 }
5345 QualType Pattern = readType(*Loc.F, Record, Idx);
5346 if (Pattern.isNull())
5347 return QualType();
David Blaikiedc84cd52013-02-20 22:23:23 +00005348 Optional<unsigned> NumExpansions;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005349 if (Record[1])
5350 NumExpansions = Record[1] - 1;
5351 return Context.getPackExpansionType(Pattern, NumExpansions);
5352 }
5353
5354 case TYPE_ELABORATED: {
5355 unsigned Idx = 0;
5356 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
5357 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
5358 QualType NamedType = readType(*Loc.F, Record, Idx);
5359 return Context.getElaboratedType(Keyword, NNS, NamedType);
5360 }
5361
5362 case TYPE_OBJC_INTERFACE: {
5363 unsigned Idx = 0;
5364 ObjCInterfaceDecl *ItfD
5365 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
5366 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
5367 }
5368
5369 case TYPE_OBJC_OBJECT: {
5370 unsigned Idx = 0;
5371 QualType Base = readType(*Loc.F, Record, Idx);
5372 unsigned NumProtos = Record[Idx++];
5373 SmallVector<ObjCProtocolDecl*, 4> Protos;
5374 for (unsigned I = 0; I != NumProtos; ++I)
5375 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
5376 return Context.getObjCObjectType(Base, Protos.data(), NumProtos);
5377 }
5378
5379 case TYPE_OBJC_OBJECT_POINTER: {
5380 unsigned Idx = 0;
5381 QualType Pointee = readType(*Loc.F, Record, Idx);
5382 return Context.getObjCObjectPointerType(Pointee);
5383 }
5384
5385 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
5386 unsigned Idx = 0;
5387 QualType Parm = readType(*Loc.F, Record, Idx);
5388 QualType Replacement = readType(*Loc.F, Record, Idx);
Stephen Hines651f13c2014-04-23 16:59:28 -07005389 return Context.getSubstTemplateTypeParmType(
5390 cast<TemplateTypeParmType>(Parm),
5391 Context.getCanonicalType(Replacement));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005392 }
5393
5394 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
5395 unsigned Idx = 0;
5396 QualType Parm = readType(*Loc.F, Record, Idx);
5397 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
5398 return Context.getSubstTemplateTypeParmPackType(
5399 cast<TemplateTypeParmType>(Parm),
5400 ArgPack);
5401 }
5402
5403 case TYPE_INJECTED_CLASS_NAME: {
5404 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
5405 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
5406 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
5407 // for AST reading, too much interdependencies.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005408 const Type *T;
5409 if (const Type *Existing = D->getTypeForDecl())
5410 T = Existing;
5411 else if (auto *Prev = D->getPreviousDecl())
5412 T = Prev->getTypeForDecl();
5413 else
5414 T = new (Context, TypeAlignment) InjectedClassNameType(D, TST);
5415 return QualType(T, 0);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005416 }
5417
5418 case TYPE_TEMPLATE_TYPE_PARM: {
5419 unsigned Idx = 0;
5420 unsigned Depth = Record[Idx++];
5421 unsigned Index = Record[Idx++];
5422 bool Pack = Record[Idx++];
5423 TemplateTypeParmDecl *D
5424 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
5425 return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
5426 }
5427
5428 case TYPE_DEPENDENT_NAME: {
5429 unsigned Idx = 0;
5430 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
5431 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
5432 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
5433 QualType Canon = readType(*Loc.F, Record, Idx);
5434 if (!Canon.isNull())
5435 Canon = Context.getCanonicalType(Canon);
5436 return Context.getDependentNameType(Keyword, NNS, Name, Canon);
5437 }
5438
5439 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
5440 unsigned Idx = 0;
5441 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
5442 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
5443 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
5444 unsigned NumArgs = Record[Idx++];
5445 SmallVector<TemplateArgument, 8> Args;
5446 Args.reserve(NumArgs);
5447 while (NumArgs--)
5448 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
5449 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
5450 Args.size(), Args.data());
5451 }
5452
5453 case TYPE_DEPENDENT_SIZED_ARRAY: {
5454 unsigned Idx = 0;
5455
5456 // ArrayType
5457 QualType ElementType = readType(*Loc.F, Record, Idx);
5458 ArrayType::ArraySizeModifier ASM
5459 = (ArrayType::ArraySizeModifier)Record[Idx++];
5460 unsigned IndexTypeQuals = Record[Idx++];
5461
5462 // DependentSizedArrayType
5463 Expr *NumElts = ReadExpr(*Loc.F);
5464 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
5465
5466 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
5467 IndexTypeQuals, Brackets);
5468 }
5469
5470 case TYPE_TEMPLATE_SPECIALIZATION: {
5471 unsigned Idx = 0;
5472 bool IsDependent = Record[Idx++];
5473 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
5474 SmallVector<TemplateArgument, 8> Args;
5475 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
5476 QualType Underlying = readType(*Loc.F, Record, Idx);
5477 QualType T;
5478 if (Underlying.isNull())
5479 T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(),
5480 Args.size());
5481 else
5482 T = Context.getTemplateSpecializationType(Name, Args.data(),
5483 Args.size(), Underlying);
5484 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
5485 return T;
5486 }
5487
5488 case TYPE_ATOMIC: {
5489 if (Record.size() != 1) {
5490 Error("Incorrect encoding of atomic type");
5491 return QualType();
5492 }
5493 QualType ValueType = readType(*Loc.F, Record, Idx);
5494 return Context.getAtomicType(ValueType);
5495 }
5496 }
5497 llvm_unreachable("Invalid TypeCode!");
5498}
5499
Stephen Hines651f13c2014-04-23 16:59:28 -07005500void ASTReader::readExceptionSpec(ModuleFile &ModuleFile,
5501 SmallVectorImpl<QualType> &Exceptions,
5502 FunctionProtoType::ExtProtoInfo &EPI,
5503 const RecordData &Record, unsigned &Idx) {
5504 ExceptionSpecificationType EST =
5505 static_cast<ExceptionSpecificationType>(Record[Idx++]);
5506 EPI.ExceptionSpecType = EST;
5507 if (EST == EST_Dynamic) {
5508 EPI.NumExceptions = Record[Idx++];
5509 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
5510 Exceptions.push_back(readType(ModuleFile, Record, Idx));
5511 EPI.Exceptions = Exceptions.data();
5512 } else if (EST == EST_ComputedNoexcept) {
5513 EPI.NoexceptExpr = ReadExpr(ModuleFile);
5514 } else if (EST == EST_Uninstantiated) {
5515 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
5516 EPI.ExceptionSpecTemplate =
5517 ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
5518 } else if (EST == EST_Unevaluated) {
5519 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
5520 }
5521}
5522
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005523class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
5524 ASTReader &Reader;
5525 ModuleFile &F;
5526 const ASTReader::RecordData &Record;
5527 unsigned &Idx;
5528
5529 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
5530 unsigned &I) {
5531 return Reader.ReadSourceLocation(F, R, I);
5532 }
5533
5534 template<typename T>
5535 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
5536 return Reader.ReadDeclAs<T>(F, Record, Idx);
5537 }
5538
5539public:
5540 TypeLocReader(ASTReader &Reader, ModuleFile &F,
5541 const ASTReader::RecordData &Record, unsigned &Idx)
5542 : Reader(Reader), F(F), Record(Record), Idx(Idx)
5543 { }
5544
5545 // We want compile-time assurance that we've enumerated all of
5546 // these, so unfortunately we have to declare them first, then
5547 // define them out-of-line.
5548#define ABSTRACT_TYPELOC(CLASS, PARENT)
5549#define TYPELOC(CLASS, PARENT) \
5550 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
5551#include "clang/AST/TypeLocNodes.def"
5552
5553 void VisitFunctionTypeLoc(FunctionTypeLoc);
5554 void VisitArrayTypeLoc(ArrayTypeLoc);
5555};
5556
5557void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
5558 // nothing to do
5559}
5560void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
5561 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
5562 if (TL.needsExtraLocalData()) {
5563 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
5564 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
5565 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
5566 TL.setModeAttr(Record[Idx++]);
5567 }
5568}
5569void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
5570 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5571}
5572void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
5573 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5574}
Reid Kleckner12df2462013-06-24 17:51:48 +00005575void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
5576 // nothing to do
5577}
Stephen Hines651f13c2014-04-23 16:59:28 -07005578void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
5579 // nothing to do
5580}
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005581void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
5582 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
5583}
5584void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
5585 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
5586}
5587void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
5588 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
5589}
5590void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
5591 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5592 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5593}
5594void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
5595 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
5596 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
5597 if (Record[Idx++])
5598 TL.setSizeExpr(Reader.ReadExpr(F));
5599 else
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005600 TL.setSizeExpr(nullptr);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005601}
5602void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
5603 VisitArrayTypeLoc(TL);
5604}
5605void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
5606 VisitArrayTypeLoc(TL);
5607}
5608void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
5609 VisitArrayTypeLoc(TL);
5610}
5611void TypeLocReader::VisitDependentSizedArrayTypeLoc(
5612 DependentSizedArrayTypeLoc TL) {
5613 VisitArrayTypeLoc(TL);
5614}
5615void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
5616 DependentSizedExtVectorTypeLoc TL) {
5617 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5618}
5619void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
5620 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5621}
5622void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
5623 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5624}
5625void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
5626 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
5627 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5628 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5629 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
Stephen Hines651f13c2014-04-23 16:59:28 -07005630 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
5631 TL.setParam(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005632 }
5633}
5634void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
5635 VisitFunctionTypeLoc(TL);
5636}
5637void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
5638 VisitFunctionTypeLoc(TL);
5639}
5640void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
5641 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5642}
5643void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
5644 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5645}
5646void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
5647 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
5648 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5649 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5650}
5651void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
5652 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
5653 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5654 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5655 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5656}
5657void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
5658 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5659}
5660void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
5661 TL.setKWLoc(ReadSourceLocation(Record, Idx));
5662 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5663 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5664 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5665}
5666void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
5667 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5668}
5669void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
5670 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5671}
5672void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
5673 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5674}
5675void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
5676 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
5677 if (TL.hasAttrOperand()) {
5678 SourceRange range;
5679 range.setBegin(ReadSourceLocation(Record, Idx));
5680 range.setEnd(ReadSourceLocation(Record, Idx));
5681 TL.setAttrOperandParensRange(range);
5682 }
5683 if (TL.hasAttrExprOperand()) {
5684 if (Record[Idx++])
5685 TL.setAttrExprOperand(Reader.ReadExpr(F));
5686 else
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005687 TL.setAttrExprOperand(nullptr);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005688 } else if (TL.hasAttrEnumOperand())
5689 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
5690}
5691void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
5692 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5693}
5694void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
5695 SubstTemplateTypeParmTypeLoc TL) {
5696 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5697}
5698void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
5699 SubstTemplateTypeParmPackTypeLoc TL) {
5700 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5701}
5702void TypeLocReader::VisitTemplateSpecializationTypeLoc(
5703 TemplateSpecializationTypeLoc TL) {
5704 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
5705 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
5706 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5707 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5708 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
5709 TL.setArgLocInfo(i,
5710 Reader.GetTemplateArgumentLocInfo(F,
5711 TL.getTypePtr()->getArg(i).getKind(),
5712 Record, Idx));
5713}
5714void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
5715 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5716 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5717}
5718void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
5719 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5720 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5721}
5722void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
5723 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5724}
5725void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
5726 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5727 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5728 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5729}
5730void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
5731 DependentTemplateSpecializationTypeLoc TL) {
5732 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5733 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5734 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
5735 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
5736 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5737 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5738 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
5739 TL.setArgLocInfo(I,
5740 Reader.GetTemplateArgumentLocInfo(F,
5741 TL.getTypePtr()->getArg(I).getKind(),
5742 Record, Idx));
5743}
5744void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
5745 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
5746}
5747void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
5748 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5749}
5750void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
5751 TL.setHasBaseTypeAsWritten(Record[Idx++]);
5752 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5753 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5754 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
5755 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
5756}
5757void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
5758 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5759}
5760void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
5761 TL.setKWLoc(ReadSourceLocation(Record, Idx));
5762 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5763 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5764}
5765
5766TypeSourceInfo *ASTReader::GetTypeSourceInfo(ModuleFile &F,
5767 const RecordData &Record,
5768 unsigned &Idx) {
5769 QualType InfoTy = readType(F, Record, Idx);
5770 if (InfoTy.isNull())
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005771 return nullptr;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005772
5773 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
5774 TypeLocReader TLR(*this, F, Record, Idx);
5775 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
5776 TLR.Visit(TL);
5777 return TInfo;
5778}
5779
5780QualType ASTReader::GetType(TypeID ID) {
5781 unsigned FastQuals = ID & Qualifiers::FastMask;
5782 unsigned Index = ID >> Qualifiers::FastWidth;
5783
5784 if (Index < NUM_PREDEF_TYPE_IDS) {
5785 QualType T;
5786 switch ((PredefinedTypeIDs)Index) {
5787 case PREDEF_TYPE_NULL_ID: return QualType();
5788 case PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
5789 case PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
5790
5791 case PREDEF_TYPE_CHAR_U_ID:
5792 case PREDEF_TYPE_CHAR_S_ID:
5793 // FIXME: Check that the signedness of CharTy is correct!
5794 T = Context.CharTy;
5795 break;
5796
5797 case PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
5798 case PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
5799 case PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
5800 case PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
5801 case PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
5802 case PREDEF_TYPE_UINT128_ID: T = Context.UnsignedInt128Ty; break;
5803 case PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
5804 case PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
5805 case PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
5806 case PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
5807 case PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
5808 case PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
5809 case PREDEF_TYPE_INT128_ID: T = Context.Int128Ty; break;
5810 case PREDEF_TYPE_HALF_ID: T = Context.HalfTy; break;
5811 case PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
5812 case PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
5813 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
5814 case PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
5815 case PREDEF_TYPE_BOUND_MEMBER: T = Context.BoundMemberTy; break;
5816 case PREDEF_TYPE_PSEUDO_OBJECT: T = Context.PseudoObjectTy; break;
5817 case PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
5818 case PREDEF_TYPE_UNKNOWN_ANY: T = Context.UnknownAnyTy; break;
5819 case PREDEF_TYPE_NULLPTR_ID: T = Context.NullPtrTy; break;
5820 case PREDEF_TYPE_CHAR16_ID: T = Context.Char16Ty; break;
5821 case PREDEF_TYPE_CHAR32_ID: T = Context.Char32Ty; break;
5822 case PREDEF_TYPE_OBJC_ID: T = Context.ObjCBuiltinIdTy; break;
5823 case PREDEF_TYPE_OBJC_CLASS: T = Context.ObjCBuiltinClassTy; break;
5824 case PREDEF_TYPE_OBJC_SEL: T = Context.ObjCBuiltinSelTy; break;
Guy Benyeib13621d2012-12-18 14:38:23 +00005825 case PREDEF_TYPE_IMAGE1D_ID: T = Context.OCLImage1dTy; break;
5826 case PREDEF_TYPE_IMAGE1D_ARR_ID: T = Context.OCLImage1dArrayTy; break;
5827 case PREDEF_TYPE_IMAGE1D_BUFF_ID: T = Context.OCLImage1dBufferTy; break;
5828 case PREDEF_TYPE_IMAGE2D_ID: T = Context.OCLImage2dTy; break;
5829 case PREDEF_TYPE_IMAGE2D_ARR_ID: T = Context.OCLImage2dArrayTy; break;
5830 case PREDEF_TYPE_IMAGE3D_ID: T = Context.OCLImage3dTy; break;
Guy Benyei21f18c42013-02-07 10:55:47 +00005831 case PREDEF_TYPE_SAMPLER_ID: T = Context.OCLSamplerTy; break;
Guy Benyeie6b9d802013-01-20 12:31:11 +00005832 case PREDEF_TYPE_EVENT_ID: T = Context.OCLEventTy; break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005833 case PREDEF_TYPE_AUTO_DEDUCT: T = Context.getAutoDeductType(); break;
5834
5835 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
5836 T = Context.getAutoRRefDeductType();
5837 break;
5838
5839 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
5840 T = Context.ARCUnbridgedCastTy;
5841 break;
5842
5843 case PREDEF_TYPE_VA_LIST_TAG:
5844 T = Context.getVaListTagType();
5845 break;
5846
5847 case PREDEF_TYPE_BUILTIN_FN:
5848 T = Context.BuiltinFnTy;
5849 break;
5850 }
5851
5852 assert(!T.isNull() && "Unknown predefined type");
5853 return T.withFastQualifiers(FastQuals);
5854 }
5855
5856 Index -= NUM_PREDEF_TYPE_IDS;
5857 assert(Index < TypesLoaded.size() && "Type index out-of-range");
5858 if (TypesLoaded[Index].isNull()) {
5859 TypesLoaded[Index] = readTypeRecord(Index);
5860 if (TypesLoaded[Index].isNull())
5861 return QualType();
5862
5863 TypesLoaded[Index]->setFromAST();
5864 if (DeserializationListener)
5865 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
5866 TypesLoaded[Index]);
5867 }
5868
5869 return TypesLoaded[Index].withFastQualifiers(FastQuals);
5870}
5871
5872QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
5873 return GetType(getGlobalTypeID(F, LocalID));
5874}
5875
5876serialization::TypeID
5877ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
5878 unsigned FastQuals = LocalID & Qualifiers::FastMask;
5879 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
5880
5881 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
5882 return LocalID;
5883
5884 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5885 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
5886 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
5887
5888 unsigned GlobalIndex = LocalIndex + I->second;
5889 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
5890}
5891
5892TemplateArgumentLocInfo
5893ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
5894 TemplateArgument::ArgKind Kind,
5895 const RecordData &Record,
5896 unsigned &Index) {
5897 switch (Kind) {
5898 case TemplateArgument::Expression:
5899 return ReadExpr(F);
5900 case TemplateArgument::Type:
5901 return GetTypeSourceInfo(F, Record, Index);
5902 case TemplateArgument::Template: {
5903 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
5904 Index);
5905 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
5906 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
5907 SourceLocation());
5908 }
5909 case TemplateArgument::TemplateExpansion: {
5910 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
5911 Index);
5912 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
5913 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
5914 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
5915 EllipsisLoc);
5916 }
5917 case TemplateArgument::Null:
5918 case TemplateArgument::Integral:
5919 case TemplateArgument::Declaration:
5920 case TemplateArgument::NullPtr:
5921 case TemplateArgument::Pack:
5922 // FIXME: Is this right?
5923 return TemplateArgumentLocInfo();
5924 }
5925 llvm_unreachable("unexpected template argument loc");
5926}
5927
5928TemplateArgumentLoc
5929ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
5930 const RecordData &Record, unsigned &Index) {
5931 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
5932
5933 if (Arg.getKind() == TemplateArgument::Expression) {
5934 if (Record[Index++]) // bool InfoHasSameExpr.
5935 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
5936 }
5937 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
5938 Record, Index));
5939}
5940
Enea Zaffanellac1cef082013-08-10 07:24:53 +00005941const ASTTemplateArgumentListInfo*
5942ASTReader::ReadASTTemplateArgumentListInfo(ModuleFile &F,
5943 const RecordData &Record,
5944 unsigned &Index) {
5945 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Index);
5946 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Index);
5947 unsigned NumArgsAsWritten = Record[Index++];
5948 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
5949 for (unsigned i = 0; i != NumArgsAsWritten; ++i)
5950 TemplArgsInfo.addArgument(ReadTemplateArgumentLoc(F, Record, Index));
5951 return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo);
5952}
5953
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005954Decl *ASTReader::GetExternalDecl(uint32_t ID) {
5955 return GetDecl(ID);
5956}
5957
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005958void ASTReader::CompleteRedeclChain(const Decl *D) {
5959 if (NumCurrentElementsDeserializing) {
5960 // We arrange to not care about the complete redeclaration chain while we're
5961 // deserializing. Just remember that the AST has marked this one as complete
5962 // but that it's not actually complete yet, so we know we still need to
5963 // complete it later.
5964 PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D));
5965 return;
5966 }
5967
5968 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
5969
5970 // Recursively ensure that the decl context itself is complete
5971 // (in particular, this matters if the decl context is a namespace).
5972 //
5973 // FIXME: This should be performed by lookup instead of here.
5974 cast<Decl>(DC)->getMostRecentDecl();
5975
5976 // If this is a named declaration, complete it by looking it up
5977 // within its context.
5978 //
5979 // FIXME: We don't currently handle the cases where we can't do this;
5980 // merging a class definition that contains unnamed entities should merge
5981 // those entities. Likewise, merging a function definition should merge
5982 // all mergeable entities within it.
5983 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) ||
5984 isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) {
5985 if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) {
5986 auto *II = Name.getAsIdentifierInfo();
5987 if (isa<TranslationUnitDecl>(DC) && II) {
5988 // Outside of C++, we don't have a lookup table for the TU, so update
5989 // the identifier instead. In C++, either way should work fine.
5990 if (II->isOutOfDate())
5991 updateOutOfDateIdentifier(*II);
5992 } else
5993 DC->lookup(Name);
5994 }
5995 }
5996}
5997
5998uint64_t ASTReader::readCXXBaseSpecifiers(ModuleFile &M,
5999 const RecordData &Record,
6000 unsigned &Idx) {
6001 if (Idx >= Record.size() || Record[Idx] > M.LocalNumCXXBaseSpecifiers) {
6002 Error("malformed AST file: missing C++ base specifier");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006003 return 0;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006004 }
6005
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006006 unsigned LocalID = Record[Idx++];
6007 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
6008}
6009
6010CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
6011 RecordLocation Loc = getLocalBitOffset(Offset);
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00006012 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006013 SavedStreamPosition SavedPosition(Cursor);
6014 Cursor.JumpToBit(Loc.Offset);
6015 ReadingKindTracker ReadingKind(Read_Decl, *this);
6016 RecordData Record;
6017 unsigned Code = Cursor.ReadCode();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00006018 unsigned RecCode = Cursor.readRecord(Code, Record);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006019 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006020 Error("malformed AST file: missing C++ base specifiers");
6021 return nullptr;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006022 }
6023
6024 unsigned Idx = 0;
6025 unsigned NumBases = Record[Idx++];
6026 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
6027 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
6028 for (unsigned I = 0; I != NumBases; ++I)
6029 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
6030 return Bases;
6031}
6032
6033serialization::DeclID
6034ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
6035 if (LocalID < NUM_PREDEF_DECL_IDS)
6036 return LocalID;
6037
6038 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6039 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
6040 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
6041
6042 return LocalID + I->second;
6043}
6044
6045bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
6046 ModuleFile &M) const {
6047 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(ID);
6048 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
6049 return &M == I->second;
6050}
6051
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00006052ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006053 if (!D->isFromASTFile())
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006054 return nullptr;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006055 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
6056 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
6057 return I->second;
6058}
6059
6060SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
6061 if (ID < NUM_PREDEF_DECL_IDS)
6062 return SourceLocation();
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006063
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006064 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
6065
6066 if (Index > DeclsLoaded.size()) {
6067 Error("declaration ID out-of-range for AST file");
6068 return SourceLocation();
6069 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006070
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006071 if (Decl *D = DeclsLoaded[Index])
6072 return D->getLocation();
6073
6074 unsigned RawLocation = 0;
6075 RecordLocation Rec = DeclCursorForID(ID, RawLocation);
6076 return ReadSourceLocation(*Rec.F, RawLocation);
6077}
6078
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006079Decl *ASTReader::GetExistingDecl(DeclID ID) {
6080 if (ID < NUM_PREDEF_DECL_IDS) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006081 switch ((PredefinedDeclIDs)ID) {
6082 case PREDEF_DECL_NULL_ID:
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006083 return nullptr;
6084
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006085 case PREDEF_DECL_TRANSLATION_UNIT_ID:
6086 return Context.getTranslationUnitDecl();
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006087
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006088 case PREDEF_DECL_OBJC_ID_ID:
6089 return Context.getObjCIdDecl();
6090
6091 case PREDEF_DECL_OBJC_SEL_ID:
6092 return Context.getObjCSelDecl();
6093
6094 case PREDEF_DECL_OBJC_CLASS_ID:
6095 return Context.getObjCClassDecl();
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006096
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006097 case PREDEF_DECL_OBJC_PROTOCOL_ID:
6098 return Context.getObjCProtocolDecl();
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006099
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006100 case PREDEF_DECL_INT_128_ID:
6101 return Context.getInt128Decl();
6102
6103 case PREDEF_DECL_UNSIGNED_INT_128_ID:
6104 return Context.getUInt128Decl();
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006105
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006106 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
6107 return Context.getObjCInstanceTypeDecl();
6108
6109 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
6110 return Context.getBuiltinVaListDecl();
6111 }
6112 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006113
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006114 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
6115
6116 if (Index >= DeclsLoaded.size()) {
6117 assert(0 && "declaration ID out-of-range for AST file");
6118 Error("declaration ID out-of-range for AST file");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006119 return nullptr;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006120 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006121
6122 return DeclsLoaded[Index];
6123}
6124
6125Decl *ASTReader::GetDecl(DeclID ID) {
6126 if (ID < NUM_PREDEF_DECL_IDS)
6127 return GetExistingDecl(ID);
6128
6129 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
6130
6131 if (Index >= DeclsLoaded.size()) {
6132 assert(0 && "declaration ID out-of-range for AST file");
6133 Error("declaration ID out-of-range for AST file");
6134 return nullptr;
6135 }
6136
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006137 if (!DeclsLoaded[Index]) {
6138 ReadDeclRecord(ID);
6139 if (DeserializationListener)
6140 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
6141 }
6142
6143 return DeclsLoaded[Index];
6144}
6145
6146DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
6147 DeclID GlobalID) {
6148 if (GlobalID < NUM_PREDEF_DECL_IDS)
6149 return GlobalID;
6150
6151 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
6152 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
6153 ModuleFile *Owner = I->second;
6154
6155 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
6156 = M.GlobalToLocalDeclIDs.find(Owner);
6157 if (Pos == M.GlobalToLocalDeclIDs.end())
6158 return 0;
6159
6160 return GlobalID - Owner->BaseDeclID + Pos->second;
6161}
6162
6163serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
6164 const RecordData &Record,
6165 unsigned &Idx) {
6166 if (Idx >= Record.size()) {
6167 Error("Corrupted AST file");
6168 return 0;
6169 }
6170
6171 return getGlobalDeclID(F, Record[Idx++]);
6172}
6173
6174/// \brief Resolve the offset of a statement into a statement.
6175///
6176/// This operation will read a new statement from the external
6177/// source each time it is called, and is meant to be used via a
6178/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
6179Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
6180 // Switch case IDs are per Decl.
6181 ClearSwitchCaseIDs();
6182
6183 // Offset here is a global offset across the entire chain.
6184 RecordLocation Loc = getLocalBitOffset(Offset);
6185 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
6186 return ReadStmtFromStream(*Loc.F);
6187}
6188
6189namespace {
6190 class FindExternalLexicalDeclsVisitor {
6191 ASTReader &Reader;
6192 const DeclContext *DC;
6193 bool (*isKindWeWant)(Decl::Kind);
6194
6195 SmallVectorImpl<Decl*> &Decls;
6196 bool PredefsVisited[NUM_PREDEF_DECL_IDS];
6197
6198 public:
6199 FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC,
6200 bool (*isKindWeWant)(Decl::Kind),
6201 SmallVectorImpl<Decl*> &Decls)
6202 : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls)
6203 {
6204 for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I)
6205 PredefsVisited[I] = false;
6206 }
6207
6208 static bool visit(ModuleFile &M, bool Preorder, void *UserData) {
6209 if (Preorder)
6210 return false;
6211
6212 FindExternalLexicalDeclsVisitor *This
6213 = static_cast<FindExternalLexicalDeclsVisitor *>(UserData);
6214
6215 ModuleFile::DeclContextInfosMap::iterator Info
6216 = M.DeclContextInfos.find(This->DC);
6217 if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls)
6218 return false;
6219
6220 // Load all of the declaration IDs
6221 for (const KindDeclIDPair *ID = Info->second.LexicalDecls,
6222 *IDE = ID + Info->second.NumLexicalDecls;
6223 ID != IDE; ++ID) {
6224 if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first))
6225 continue;
6226
6227 // Don't add predefined declarations to the lexical context more
6228 // than once.
6229 if (ID->second < NUM_PREDEF_DECL_IDS) {
6230 if (This->PredefsVisited[ID->second])
6231 continue;
6232
6233 This->PredefsVisited[ID->second] = true;
6234 }
6235
6236 if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) {
6237 if (!This->DC->isDeclInLexicalTraversal(D))
6238 This->Decls.push_back(D);
6239 }
6240 }
6241
6242 return false;
6243 }
6244 };
6245}
6246
6247ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
6248 bool (*isKindWeWant)(Decl::Kind),
6249 SmallVectorImpl<Decl*> &Decls) {
6250 // There might be lexical decls in multiple modules, for the TU at
6251 // least. Walk all of the modules in the order they were loaded.
6252 FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls);
6253 ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor);
6254 ++NumLexicalDeclContextsRead;
6255 return ELR_Success;
6256}
6257
6258namespace {
6259
6260class DeclIDComp {
6261 ASTReader &Reader;
6262 ModuleFile &Mod;
6263
6264public:
6265 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
6266
6267 bool operator()(LocalDeclID L, LocalDeclID R) const {
6268 SourceLocation LHS = getLocation(L);
6269 SourceLocation RHS = getLocation(R);
6270 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6271 }
6272
6273 bool operator()(SourceLocation LHS, LocalDeclID R) const {
6274 SourceLocation RHS = getLocation(R);
6275 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6276 }
6277
6278 bool operator()(LocalDeclID L, SourceLocation RHS) const {
6279 SourceLocation LHS = getLocation(L);
6280 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6281 }
6282
6283 SourceLocation getLocation(LocalDeclID ID) const {
6284 return Reader.getSourceManager().getFileLoc(
6285 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
6286 }
6287};
6288
6289}
6290
6291void ASTReader::FindFileRegionDecls(FileID File,
6292 unsigned Offset, unsigned Length,
6293 SmallVectorImpl<Decl *> &Decls) {
6294 SourceManager &SM = getSourceManager();
6295
6296 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
6297 if (I == FileDeclIDs.end())
6298 return;
6299
6300 FileDeclsInfo &DInfo = I->second;
6301 if (DInfo.Decls.empty())
6302 return;
6303
6304 SourceLocation
6305 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
6306 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
6307
6308 DeclIDComp DIDComp(*this, *DInfo.Mod);
6309 ArrayRef<serialization::LocalDeclID>::iterator
6310 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
6311 BeginLoc, DIDComp);
6312 if (BeginIt != DInfo.Decls.begin())
6313 --BeginIt;
6314
6315 // If we are pointing at a top-level decl inside an objc container, we need
6316 // to backtrack until we find it otherwise we will fail to report that the
6317 // region overlaps with an objc container.
6318 while (BeginIt != DInfo.Decls.begin() &&
6319 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
6320 ->isTopLevelDeclInObjCContainer())
6321 --BeginIt;
6322
6323 ArrayRef<serialization::LocalDeclID>::iterator
6324 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
6325 EndLoc, DIDComp);
6326 if (EndIt != DInfo.Decls.end())
6327 ++EndIt;
6328
6329 for (ArrayRef<serialization::LocalDeclID>::iterator
6330 DIt = BeginIt; DIt != EndIt; ++DIt)
6331 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
6332}
6333
6334namespace {
6335 /// \brief ModuleFile visitor used to perform name lookup into a
6336 /// declaration context.
6337 class DeclContextNameLookupVisitor {
6338 ASTReader &Reader;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00006339 SmallVectorImpl<const DeclContext *> &Contexts;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006340 DeclarationName Name;
6341 SmallVectorImpl<NamedDecl *> &Decls;
6342
6343 public:
6344 DeclContextNameLookupVisitor(ASTReader &Reader,
6345 SmallVectorImpl<const DeclContext *> &Contexts,
6346 DeclarationName Name,
6347 SmallVectorImpl<NamedDecl *> &Decls)
6348 : Reader(Reader), Contexts(Contexts), Name(Name), Decls(Decls) { }
6349
6350 static bool visit(ModuleFile &M, void *UserData) {
6351 DeclContextNameLookupVisitor *This
6352 = static_cast<DeclContextNameLookupVisitor *>(UserData);
6353
6354 // Check whether we have any visible declaration information for
6355 // this context in this module.
6356 ModuleFile::DeclContextInfosMap::iterator Info;
6357 bool FoundInfo = false;
6358 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
6359 Info = M.DeclContextInfos.find(This->Contexts[I]);
6360 if (Info != M.DeclContextInfos.end() &&
6361 Info->second.NameLookupTableData) {
6362 FoundInfo = true;
6363 break;
6364 }
6365 }
6366
6367 if (!FoundInfo)
6368 return false;
6369
6370 // Look for this name within this module.
6371 ASTDeclContextNameLookupTable *LookupTable =
6372 Info->second.NameLookupTableData;
6373 ASTDeclContextNameLookupTable::iterator Pos
6374 = LookupTable->find(This->Name);
6375 if (Pos == LookupTable->end())
6376 return false;
6377
6378 bool FoundAnything = false;
6379 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
6380 for (; Data.first != Data.second; ++Data.first) {
6381 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first);
6382 if (!ND)
6383 continue;
6384
6385 if (ND->getDeclName() != This->Name) {
6386 // A name might be null because the decl's redeclarable part is
6387 // currently read before reading its name. The lookup is triggered by
6388 // building that decl (likely indirectly), and so it is later in the
6389 // sense of "already existing" and can be ignored here.
6390 continue;
6391 }
6392
6393 // Record this declaration.
6394 FoundAnything = true;
6395 This->Decls.push_back(ND);
6396 }
6397
6398 return FoundAnything;
6399 }
6400 };
6401}
6402
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00006403/// \brief Retrieve the "definitive" module file for the definition of the
6404/// given declaration context, if there is one.
6405///
6406/// The "definitive" module file is the only place where we need to look to
6407/// find information about the declarations within the given declaration
6408/// context. For example, C++ and Objective-C classes, C structs/unions, and
6409/// Objective-C protocols, categories, and extensions are all defined in a
6410/// single place in the source code, so they have definitive module files
6411/// associated with them. C++ namespaces, on the other hand, can have
6412/// definitions in multiple different module files.
6413///
6414/// Note: this needs to be kept in sync with ASTWriter::AddedVisibleDecl's
6415/// NDEBUG checking.
6416static ModuleFile *getDefinitiveModuleFileFor(const DeclContext *DC,
6417 ASTReader &Reader) {
Douglas Gregore0d20662013-01-22 17:08:30 +00006418 if (const DeclContext *DefDC = getDefinitiveDeclContext(DC))
6419 return Reader.getOwningModuleFile(cast<Decl>(DefDC));
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00006420
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006421 return nullptr;
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00006422}
6423
Richard Smith3646c682013-02-07 03:30:24 +00006424bool
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006425ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
6426 DeclarationName Name) {
6427 assert(DC->hasExternalVisibleStorage() &&
6428 "DeclContext has no visible decls in storage");
6429 if (!Name)
Richard Smith3646c682013-02-07 03:30:24 +00006430 return false;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006431
6432 SmallVector<NamedDecl *, 64> Decls;
6433
6434 // Compute the declaration contexts we need to look into. Multiple such
6435 // declaration contexts occur when two declaration contexts from disjoint
6436 // modules get merged, e.g., when two namespaces with the same name are
6437 // independently defined in separate modules.
6438 SmallVector<const DeclContext *, 2> Contexts;
6439 Contexts.push_back(DC);
6440
6441 if (DC->isNamespace()) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006442 auto Merged = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006443 if (Merged != MergedDecls.end()) {
6444 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
6445 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
6446 }
6447 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006448 if (isa<CXXRecordDecl>(DC)) {
6449 auto Merged = MergedLookups.find(DC);
6450 if (Merged != MergedLookups.end())
6451 Contexts.insert(Contexts.end(), Merged->second.begin(),
6452 Merged->second.end());
6453 }
6454
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006455 DeclContextNameLookupVisitor Visitor(*this, Contexts, Name, Decls);
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00006456
6457 // If we can definitively determine which module file to look into,
6458 // only look there. Otherwise, look in all module files.
6459 ModuleFile *Definitive;
6460 if (Contexts.size() == 1 &&
6461 (Definitive = getDefinitiveModuleFileFor(DC, *this))) {
6462 DeclContextNameLookupVisitor::visit(*Definitive, &Visitor);
6463 } else {
6464 ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor);
6465 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006466 ++NumVisibleDeclContextsRead;
6467 SetExternalVisibleDeclsForName(DC, Name, Decls);
Richard Smith3646c682013-02-07 03:30:24 +00006468 return !Decls.empty();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006469}
6470
6471namespace {
6472 /// \brief ModuleFile visitor used to retrieve all visible names in a
6473 /// declaration context.
6474 class DeclContextAllNamesVisitor {
6475 ASTReader &Reader;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00006476 SmallVectorImpl<const DeclContext *> &Contexts;
Craig Topper8ae63872013-07-05 04:43:31 +00006477 DeclsMap &Decls;
Argyrios Kyrtzidisca40f302012-12-19 22:21:18 +00006478 bool VisitAll;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006479
6480 public:
6481 DeclContextAllNamesVisitor(ASTReader &Reader,
6482 SmallVectorImpl<const DeclContext *> &Contexts,
Craig Topper8ae63872013-07-05 04:43:31 +00006483 DeclsMap &Decls, bool VisitAll)
Argyrios Kyrtzidisca40f302012-12-19 22:21:18 +00006484 : Reader(Reader), Contexts(Contexts), Decls(Decls), VisitAll(VisitAll) { }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006485
6486 static bool visit(ModuleFile &M, void *UserData) {
6487 DeclContextAllNamesVisitor *This
6488 = static_cast<DeclContextAllNamesVisitor *>(UserData);
6489
6490 // Check whether we have any visible declaration information for
6491 // this context in this module.
6492 ModuleFile::DeclContextInfosMap::iterator Info;
6493 bool FoundInfo = false;
6494 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
6495 Info = M.DeclContextInfos.find(This->Contexts[I]);
6496 if (Info != M.DeclContextInfos.end() &&
6497 Info->second.NameLookupTableData) {
6498 FoundInfo = true;
6499 break;
6500 }
6501 }
6502
6503 if (!FoundInfo)
6504 return false;
6505
6506 ASTDeclContextNameLookupTable *LookupTable =
6507 Info->second.NameLookupTableData;
6508 bool FoundAnything = false;
6509 for (ASTDeclContextNameLookupTable::data_iterator
Douglas Gregora6b00fc2013-01-23 22:38:11 +00006510 I = LookupTable->data_begin(), E = LookupTable->data_end();
6511 I != E;
6512 ++I) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006513 ASTDeclContextNameLookupTrait::data_type Data = *I;
6514 for (; Data.first != Data.second; ++Data.first) {
6515 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M,
6516 *Data.first);
6517 if (!ND)
6518 continue;
6519
6520 // Record this declaration.
6521 FoundAnything = true;
6522 This->Decls[ND->getDeclName()].push_back(ND);
6523 }
6524 }
6525
Argyrios Kyrtzidisca40f302012-12-19 22:21:18 +00006526 return FoundAnything && !This->VisitAll;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006527 }
6528 };
6529}
6530
6531void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
6532 if (!DC->hasExternalVisibleStorage())
6533 return;
Craig Topperee0a4792013-07-05 04:33:53 +00006534 DeclsMap Decls;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006535
6536 // Compute the declaration contexts we need to look into. Multiple such
6537 // declaration contexts occur when two declaration contexts from disjoint
6538 // modules get merged, e.g., when two namespaces with the same name are
6539 // independently defined in separate modules.
6540 SmallVector<const DeclContext *, 2> Contexts;
6541 Contexts.push_back(DC);
6542
6543 if (DC->isNamespace()) {
6544 MergedDeclsMap::iterator Merged
6545 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
6546 if (Merged != MergedDecls.end()) {
6547 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
6548 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
6549 }
6550 }
6551
Argyrios Kyrtzidisca40f302012-12-19 22:21:18 +00006552 DeclContextAllNamesVisitor Visitor(*this, Contexts, Decls,
6553 /*VisitAll=*/DC->isFileContext());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006554 ModuleMgr.visit(&DeclContextAllNamesVisitor::visit, &Visitor);
6555 ++NumVisibleDeclContextsRead;
6556
Craig Topperee0a4792013-07-05 04:33:53 +00006557 for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006558 SetExternalVisibleDeclsForName(DC, I->first, I->second);
6559 }
6560 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
6561}
6562
6563/// \brief Under non-PCH compilation the consumer receives the objc methods
6564/// before receiving the implementation, and codegen depends on this.
6565/// We simulate this by deserializing and passing to consumer the methods of the
6566/// implementation before passing the deserialized implementation decl.
6567static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
6568 ASTConsumer *Consumer) {
6569 assert(ImplD && Consumer);
6570
Stephen Hines651f13c2014-04-23 16:59:28 -07006571 for (auto *I : ImplD->methods())
6572 Consumer->HandleInterestingDecl(DeclGroupRef(I));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006573
6574 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
6575}
6576
6577void ASTReader::PassInterestingDeclsToConsumer() {
6578 assert(Consumer);
Stephen Hines651f13c2014-04-23 16:59:28 -07006579
6580 if (PassingDeclsToConsumer)
6581 return;
6582
6583 // Guard variable to avoid recursively redoing the process of passing
6584 // decls to consumer.
6585 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
6586 true);
6587
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006588 while (!InterestingDecls.empty()) {
6589 Decl *D = InterestingDecls.front();
6590 InterestingDecls.pop_front();
6591
6592 PassInterestingDeclToConsumer(D);
6593 }
6594}
6595
6596void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
6597 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
6598 PassObjCImplDeclToConsumer(ImplD, Consumer);
6599 else
6600 Consumer->HandleInterestingDecl(DeclGroupRef(D));
6601}
6602
6603void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
6604 this->Consumer = Consumer;
6605
6606 if (!Consumer)
6607 return;
6608
Stephen Hines651f13c2014-04-23 16:59:28 -07006609 for (unsigned I = 0, N = EagerlyDeserializedDecls.size(); I != N; ++I) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006610 // Force deserialization of this decl, which will cause it to be queued for
6611 // passing to the consumer.
Stephen Hines651f13c2014-04-23 16:59:28 -07006612 GetDecl(EagerlyDeserializedDecls[I]);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006613 }
Stephen Hines651f13c2014-04-23 16:59:28 -07006614 EagerlyDeserializedDecls.clear();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006615
6616 PassInterestingDeclsToConsumer();
6617}
6618
6619void ASTReader::PrintStats() {
6620 std::fprintf(stderr, "*** AST File Statistics:\n");
6621
6622 unsigned NumTypesLoaded
6623 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
6624 QualType());
6625 unsigned NumDeclsLoaded
6626 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006627 (Decl *)nullptr);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006628 unsigned NumIdentifiersLoaded
6629 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
6630 IdentifiersLoaded.end(),
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006631 (IdentifierInfo *)nullptr);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006632 unsigned NumMacrosLoaded
6633 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
6634 MacrosLoaded.end(),
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006635 (MacroInfo *)nullptr);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006636 unsigned NumSelectorsLoaded
6637 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
6638 SelectorsLoaded.end(),
6639 Selector());
6640
6641 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
6642 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
6643 NumSLocEntriesRead, TotalNumSLocEntries,
6644 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
6645 if (!TypesLoaded.empty())
6646 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
6647 NumTypesLoaded, (unsigned)TypesLoaded.size(),
6648 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
6649 if (!DeclsLoaded.empty())
6650 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
6651 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
6652 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
6653 if (!IdentifiersLoaded.empty())
6654 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
6655 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
6656 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
6657 if (!MacrosLoaded.empty())
6658 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
6659 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
6660 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
6661 if (!SelectorsLoaded.empty())
6662 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
6663 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
6664 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
6665 if (TotalNumStatements)
6666 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
6667 NumStatementsRead, TotalNumStatements,
6668 ((float)NumStatementsRead/TotalNumStatements * 100));
6669 if (TotalNumMacros)
6670 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
6671 NumMacrosRead, TotalNumMacros,
6672 ((float)NumMacrosRead/TotalNumMacros * 100));
6673 if (TotalLexicalDeclContexts)
6674 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
6675 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
6676 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
6677 * 100));
6678 if (TotalVisibleDeclContexts)
6679 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
6680 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
6681 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
6682 * 100));
6683 if (TotalNumMethodPoolEntries) {
6684 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
6685 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
6686 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
6687 * 100));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006688 }
Douglas Gregor95fb36e2013-01-28 17:54:36 +00006689 if (NumMethodPoolLookups) {
6690 std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n",
6691 NumMethodPoolHits, NumMethodPoolLookups,
6692 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
6693 }
6694 if (NumMethodPoolTableLookups) {
6695 std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n",
6696 NumMethodPoolTableHits, NumMethodPoolTableLookups,
6697 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
6698 * 100.0));
6699 }
6700
Douglas Gregore1698072013-01-25 00:38:33 +00006701 if (NumIdentifierLookupHits) {
6702 std::fprintf(stderr,
6703 " %u / %u identifier table lookups succeeded (%f%%)\n",
6704 NumIdentifierLookupHits, NumIdentifierLookups,
6705 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
6706 }
6707
Douglas Gregor1a49d972013-01-25 01:03:03 +00006708 if (GlobalIndex) {
6709 std::fprintf(stderr, "\n");
6710 GlobalIndex->printStats();
6711 }
6712
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006713 std::fprintf(stderr, "\n");
6714 dump();
6715 std::fprintf(stderr, "\n");
6716}
6717
6718template<typename Key, typename ModuleFile, unsigned InitialCapacity>
6719static void
6720dumpModuleIDMap(StringRef Name,
6721 const ContinuousRangeMap<Key, ModuleFile *,
6722 InitialCapacity> &Map) {
6723 if (Map.begin() == Map.end())
6724 return;
6725
6726 typedef ContinuousRangeMap<Key, ModuleFile *, InitialCapacity> MapType;
6727 llvm::errs() << Name << ":\n";
6728 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
6729 I != IEnd; ++I) {
6730 llvm::errs() << " " << I->first << " -> " << I->second->FileName
6731 << "\n";
6732 }
6733}
6734
6735void ASTReader::dump() {
6736 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
6737 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
6738 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
6739 dumpModuleIDMap("Global type map", GlobalTypeMap);
6740 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
6741 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
6742 dumpModuleIDMap("Global macro map", GlobalMacroMap);
6743 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
6744 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
6745 dumpModuleIDMap("Global preprocessed entity map",
6746 GlobalPreprocessedEntityMap);
6747
6748 llvm::errs() << "\n*** PCH/Modules Loaded:";
6749 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
6750 MEnd = ModuleMgr.end();
6751 M != MEnd; ++M)
6752 (*M)->dump();
6753}
6754
6755/// Return the amount of memory used by memory buffers, breaking down
6756/// by heap-backed versus mmap'ed memory.
6757void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
6758 for (ModuleConstIterator I = ModuleMgr.begin(),
6759 E = ModuleMgr.end(); I != E; ++I) {
6760 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
6761 size_t bytes = buf->getBufferSize();
6762 switch (buf->getBufferKind()) {
6763 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
6764 sizes.malloc_bytes += bytes;
6765 break;
6766 case llvm::MemoryBuffer::MemoryBuffer_MMap:
6767 sizes.mmap_bytes += bytes;
6768 break;
6769 }
6770 }
6771 }
6772}
6773
6774void ASTReader::InitializeSema(Sema &S) {
6775 SemaObj = &S;
6776 S.addExternalSource(this);
6777
6778 // Makes sure any declarations that were deserialized "too early"
6779 // still get added to the identifier's declaration chains.
6780 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
Argyrios Kyrtzidis0532df02013-04-26 21:33:35 +00006781 pushExternalDeclIntoScope(PreloadedDecls[I],
6782 PreloadedDecls[I]->getDeclName());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006783 }
6784 PreloadedDecls.clear();
6785
Richard Smith9b671182013-10-18 06:54:39 +00006786 // FIXME: What happens if these are changed by a module import?
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006787 if (!FPPragmaOptions.empty()) {
6788 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
6789 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
6790 }
6791
Richard Smith9b671182013-10-18 06:54:39 +00006792 // FIXME: What happens if these are changed by a module import?
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006793 if (!OpenCLExtensions.empty()) {
6794 unsigned I = 0;
6795#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
6796#include "clang/Basic/OpenCLExtensions.def"
6797
6798 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
6799 }
Richard Smith9b671182013-10-18 06:54:39 +00006800
6801 UpdateSema();
6802}
6803
6804void ASTReader::UpdateSema() {
6805 assert(SemaObj && "no Sema to update");
6806
6807 // Load the offsets of the declarations that Sema references.
6808 // They will be lazily deserialized when needed.
6809 if (!SemaDeclRefs.empty()) {
6810 assert(SemaDeclRefs.size() % 2 == 0);
6811 for (unsigned I = 0; I != SemaDeclRefs.size(); I += 2) {
6812 if (!SemaObj->StdNamespace)
6813 SemaObj->StdNamespace = SemaDeclRefs[I];
6814 if (!SemaObj->StdBadAlloc)
6815 SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
6816 }
6817 SemaDeclRefs.clear();
6818 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006819
6820 // Update the state of 'pragma clang optimize'. Use the same API as if we had
6821 // encountered the pragma in the source.
6822 if(OptimizeOffPragmaLocation.isValid())
6823 SemaObj->ActOnPragmaOptimize(/* IsOn = */ false, OptimizeOffPragmaLocation);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006824}
6825
6826IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
6827 // Note that we are loading an identifier.
6828 Deserializing AnIdentifier(this);
Douglas Gregor1a49d972013-01-25 01:03:03 +00006829 StringRef Name(NameStart, NameEnd - NameStart);
6830
6831 // If there is a global index, look there first to determine which modules
6832 // provably do not have any results for this identifier.
Douglas Gregor188bdcd2013-01-25 23:32:03 +00006833 GlobalModuleIndex::HitSet Hits;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006834 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
Douglas Gregor1a49d972013-01-25 01:03:03 +00006835 if (!loadGlobalIndex()) {
Douglas Gregor188bdcd2013-01-25 23:32:03 +00006836 if (GlobalIndex->lookupIdentifier(Name, Hits)) {
6837 HitsPtr = &Hits;
Douglas Gregor1a49d972013-01-25 01:03:03 +00006838 }
6839 }
Douglas Gregor188bdcd2013-01-25 23:32:03 +00006840 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
Douglas Gregore1698072013-01-25 00:38:33 +00006841 NumIdentifierLookups,
6842 NumIdentifierLookupHits);
Douglas Gregor188bdcd2013-01-25 23:32:03 +00006843 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor, HitsPtr);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006844 IdentifierInfo *II = Visitor.getIdentifierInfo();
6845 markIdentifierUpToDate(II);
6846 return II;
6847}
6848
6849namespace clang {
6850 /// \brief An identifier-lookup iterator that enumerates all of the
6851 /// identifiers stored within a set of AST files.
6852 class ASTIdentifierIterator : public IdentifierIterator {
6853 /// \brief The AST reader whose identifiers are being enumerated.
6854 const ASTReader &Reader;
6855
6856 /// \brief The current index into the chain of AST files stored in
6857 /// the AST reader.
6858 unsigned Index;
6859
6860 /// \brief The current position within the identifier lookup table
6861 /// of the current AST file.
6862 ASTIdentifierLookupTable::key_iterator Current;
6863
6864 /// \brief The end position within the identifier lookup table of
6865 /// the current AST file.
6866 ASTIdentifierLookupTable::key_iterator End;
6867
6868 public:
6869 explicit ASTIdentifierIterator(const ASTReader &Reader);
6870
Stephen Hines651f13c2014-04-23 16:59:28 -07006871 StringRef Next() override;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006872 };
6873}
6874
6875ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
6876 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
6877 ASTIdentifierLookupTable *IdTable
6878 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
6879 Current = IdTable->key_begin();
6880 End = IdTable->key_end();
6881}
6882
6883StringRef ASTIdentifierIterator::Next() {
6884 while (Current == End) {
6885 // If we have exhausted all of our AST files, we're done.
6886 if (Index == 0)
6887 return StringRef();
6888
6889 --Index;
6890 ASTIdentifierLookupTable *IdTable
6891 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
6892 IdentifierLookupTable;
6893 Current = IdTable->key_begin();
6894 End = IdTable->key_end();
6895 }
6896
6897 // We have any identifiers remaining in the current AST file; return
6898 // the next one.
Douglas Gregor479633c2013-01-23 18:53:14 +00006899 StringRef Result = *Current;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006900 ++Current;
Douglas Gregor479633c2013-01-23 18:53:14 +00006901 return Result;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006902}
6903
Argyrios Kyrtzidis87f9d812013-04-17 22:10:55 +00006904IdentifierIterator *ASTReader::getIdentifiers() {
6905 if (!loadGlobalIndex())
6906 return GlobalIndex->createIdentifierIterator();
6907
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006908 return new ASTIdentifierIterator(*this);
6909}
6910
6911namespace clang { namespace serialization {
6912 class ReadMethodPoolVisitor {
6913 ASTReader &Reader;
6914 Selector Sel;
6915 unsigned PriorGeneration;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00006916 unsigned InstanceBits;
6917 unsigned FactoryBits;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00006918 SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
6919 SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006920
6921 public:
6922 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
6923 unsigned PriorGeneration)
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00006924 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration),
6925 InstanceBits(0), FactoryBits(0) { }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006926
6927 static bool visit(ModuleFile &M, void *UserData) {
6928 ReadMethodPoolVisitor *This
6929 = static_cast<ReadMethodPoolVisitor *>(UserData);
6930
6931 if (!M.SelectorLookupTable)
6932 return false;
6933
6934 // If we've already searched this module file, skip it now.
6935 if (M.Generation <= This->PriorGeneration)
6936 return true;
6937
Douglas Gregor95fb36e2013-01-28 17:54:36 +00006938 ++This->Reader.NumMethodPoolTableLookups;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006939 ASTSelectorLookupTable *PoolTable
6940 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
6941 ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel);
6942 if (Pos == PoolTable->end())
6943 return false;
Douglas Gregor95fb36e2013-01-28 17:54:36 +00006944
6945 ++This->Reader.NumMethodPoolTableHits;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006946 ++This->Reader.NumSelectorsRead;
6947 // FIXME: Not quite happy with the statistics here. We probably should
6948 // disable this tracking when called via LoadSelector.
6949 // Also, should entries without methods count as misses?
6950 ++This->Reader.NumMethodPoolEntriesRead;
6951 ASTSelectorLookupTrait::data_type Data = *Pos;
6952 if (This->Reader.DeserializationListener)
6953 This->Reader.DeserializationListener->SelectorRead(Data.ID,
6954 This->Sel);
6955
6956 This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
6957 This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00006958 This->InstanceBits = Data.InstanceBits;
6959 This->FactoryBits = Data.FactoryBits;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006960 return true;
6961 }
6962
6963 /// \brief Retrieve the instance methods found by this visitor.
6964 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
6965 return InstanceMethods;
6966 }
6967
6968 /// \brief Retrieve the instance methods found by this visitor.
6969 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
6970 return FactoryMethods;
6971 }
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00006972
6973 unsigned getInstanceBits() const { return InstanceBits; }
6974 unsigned getFactoryBits() const { return FactoryBits; }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006975 };
6976} } // end namespace clang::serialization
6977
6978/// \brief Add the given set of methods to the method list.
6979static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
6980 ObjCMethodList &List) {
6981 for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
6982 S.addMethodToGlobalList(&List, Methods[I]);
6983 }
6984}
6985
6986void ASTReader::ReadMethodPool(Selector Sel) {
6987 // Get the selector generation and update it to the current generation.
6988 unsigned &Generation = SelectorGeneration[Sel];
6989 unsigned PriorGeneration = Generation;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07006990 Generation = getGeneration();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006991
6992 // Search for methods defined with this selector.
Douglas Gregor95fb36e2013-01-28 17:54:36 +00006993 ++NumMethodPoolLookups;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006994 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
6995 ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor);
6996
6997 if (Visitor.getInstanceMethods().empty() &&
Douglas Gregor95fb36e2013-01-28 17:54:36 +00006998 Visitor.getFactoryMethods().empty())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006999 return;
Douglas Gregor95fb36e2013-01-28 17:54:36 +00007000
7001 ++NumMethodPoolHits;
7002
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007003 if (!getSema())
7004 return;
7005
7006 Sema &S = *getSema();
7007 Sema::GlobalMethodPool::iterator Pos
7008 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
7009
7010 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
7011 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00007012 Pos->second.first.setBits(Visitor.getInstanceBits());
7013 Pos->second.second.setBits(Visitor.getFactoryBits());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007014}
7015
7016void ASTReader::ReadKnownNamespaces(
7017 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
7018 Namespaces.clear();
7019
7020 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
7021 if (NamespaceDecl *Namespace
7022 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
7023 Namespaces.push_back(Namespace);
7024 }
7025}
7026
Nick Lewyckycd0655b2013-02-01 08:13:20 +00007027void ASTReader::ReadUndefinedButUsed(
Nick Lewycky995e26b2013-01-31 03:23:57 +00007028 llvm::DenseMap<NamedDecl*, SourceLocation> &Undefined) {
Nick Lewyckycd0655b2013-02-01 08:13:20 +00007029 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
7030 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
Nick Lewycky01a41142013-01-26 00:35:08 +00007031 SourceLocation Loc =
Nick Lewyckycd0655b2013-02-01 08:13:20 +00007032 SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
Nick Lewycky01a41142013-01-26 00:35:08 +00007033 Undefined.insert(std::make_pair(D, Loc));
7034 }
7035}
Nick Lewycky01a41142013-01-26 00:35:08 +00007036
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007037void ASTReader::ReadTentativeDefinitions(
7038 SmallVectorImpl<VarDecl *> &TentativeDefs) {
7039 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
7040 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
7041 if (Var)
7042 TentativeDefs.push_back(Var);
7043 }
7044 TentativeDefinitions.clear();
7045}
7046
7047void ASTReader::ReadUnusedFileScopedDecls(
7048 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
7049 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
7050 DeclaratorDecl *D
7051 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
7052 if (D)
7053 Decls.push_back(D);
7054 }
7055 UnusedFileScopedDecls.clear();
7056}
7057
7058void ASTReader::ReadDelegatingConstructors(
7059 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
7060 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
7061 CXXConstructorDecl *D
7062 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
7063 if (D)
7064 Decls.push_back(D);
7065 }
7066 DelegatingCtorDecls.clear();
7067}
7068
7069void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
7070 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
7071 TypedefNameDecl *D
7072 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
7073 if (D)
7074 Decls.push_back(D);
7075 }
7076 ExtVectorDecls.clear();
7077}
7078
7079void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) {
7080 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
7081 CXXRecordDecl *D
7082 = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I]));
7083 if (D)
7084 Decls.push_back(D);
7085 }
7086 DynamicClasses.clear();
7087}
7088
7089void
Richard Smith5ea6ef42013-01-10 23:43:47 +00007090ASTReader::ReadLocallyScopedExternCDecls(SmallVectorImpl<NamedDecl *> &Decls) {
7091 for (unsigned I = 0, N = LocallyScopedExternCDecls.size(); I != N; ++I) {
7092 NamedDecl *D
7093 = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternCDecls[I]));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007094 if (D)
7095 Decls.push_back(D);
7096 }
Richard Smith5ea6ef42013-01-10 23:43:47 +00007097 LocallyScopedExternCDecls.clear();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007098}
7099
7100void ASTReader::ReadReferencedSelectors(
7101 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
7102 if (ReferencedSelectorsData.empty())
7103 return;
7104
7105 // If there are @selector references added them to its pool. This is for
7106 // implementation of -Wselector.
7107 unsigned int DataSize = ReferencedSelectorsData.size()-1;
7108 unsigned I = 0;
7109 while (I < DataSize) {
7110 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
7111 SourceLocation SelLoc
7112 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
7113 Sels.push_back(std::make_pair(Sel, SelLoc));
7114 }
7115 ReferencedSelectorsData.clear();
7116}
7117
7118void ASTReader::ReadWeakUndeclaredIdentifiers(
7119 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
7120 if (WeakUndeclaredIdentifiers.empty())
7121 return;
7122
7123 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
7124 IdentifierInfo *WeakId
7125 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
7126 IdentifierInfo *AliasId
7127 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
7128 SourceLocation Loc
7129 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
7130 bool Used = WeakUndeclaredIdentifiers[I++];
7131 WeakInfo WI(AliasId, Loc);
7132 WI.setUsed(Used);
7133 WeakIDs.push_back(std::make_pair(WeakId, WI));
7134 }
7135 WeakUndeclaredIdentifiers.clear();
7136}
7137
7138void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
7139 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
7140 ExternalVTableUse VT;
7141 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
7142 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
7143 VT.DefinitionRequired = VTableUses[Idx++];
7144 VTables.push_back(VT);
7145 }
7146
7147 VTableUses.clear();
7148}
7149
7150void ASTReader::ReadPendingInstantiations(
7151 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
7152 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
7153 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
7154 SourceLocation Loc
7155 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
7156
7157 Pending.push_back(std::make_pair(D, Loc));
7158 }
7159 PendingInstantiations.clear();
7160}
7161
Richard Smithac32d902013-08-07 21:41:30 +00007162void ASTReader::ReadLateParsedTemplates(
7163 llvm::DenseMap<const FunctionDecl *, LateParsedTemplate *> &LPTMap) {
7164 for (unsigned Idx = 0, N = LateParsedTemplates.size(); Idx < N;
7165 /* In loop */) {
7166 FunctionDecl *FD = cast<FunctionDecl>(GetDecl(LateParsedTemplates[Idx++]));
7167
7168 LateParsedTemplate *LT = new LateParsedTemplate;
7169 LT->D = GetDecl(LateParsedTemplates[Idx++]);
7170
7171 ModuleFile *F = getOwningModuleFile(LT->D);
7172 assert(F && "No module");
7173
7174 unsigned TokN = LateParsedTemplates[Idx++];
7175 LT->Toks.reserve(TokN);
7176 for (unsigned T = 0; T < TokN; ++T)
7177 LT->Toks.push_back(ReadToken(*F, LateParsedTemplates, Idx));
7178
7179 LPTMap[FD] = LT;
7180 }
7181
7182 LateParsedTemplates.clear();
7183}
7184
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007185void ASTReader::LoadSelector(Selector Sel) {
7186 // It would be complicated to avoid reading the methods anyway. So don't.
7187 ReadMethodPool(Sel);
7188}
7189
7190void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
7191 assert(ID && "Non-zero identifier ID required");
7192 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
7193 IdentifiersLoaded[ID - 1] = II;
7194 if (DeserializationListener)
7195 DeserializationListener->IdentifierRead(ID, II);
7196}
7197
7198/// \brief Set the globally-visible declarations associated with the given
7199/// identifier.
7200///
7201/// If the AST reader is currently in a state where the given declaration IDs
7202/// cannot safely be resolved, they are queued until it is safe to resolve
7203/// them.
7204///
7205/// \param II an IdentifierInfo that refers to one or more globally-visible
7206/// declarations.
7207///
7208/// \param DeclIDs the set of declaration IDs with the name @p II that are
7209/// visible at global scope.
7210///
Douglas Gregoraa945902013-02-18 15:53:43 +00007211/// \param Decls if non-null, this vector will be populated with the set of
7212/// deserialized declarations. These declarations will not be pushed into
7213/// scope.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007214void
7215ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
7216 const SmallVectorImpl<uint32_t> &DeclIDs,
Douglas Gregoraa945902013-02-18 15:53:43 +00007217 SmallVectorImpl<Decl *> *Decls) {
7218 if (NumCurrentElementsDeserializing && !Decls) {
7219 PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007220 return;
7221 }
7222
7223 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
7224 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
7225 if (SemaObj) {
Douglas Gregoraa945902013-02-18 15:53:43 +00007226 // If we're simply supposed to record the declarations, do so now.
7227 if (Decls) {
7228 Decls->push_back(D);
7229 continue;
7230 }
7231
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007232 // Introduce this declaration into the translation-unit scope
7233 // and add it to the declaration chain for this identifier, so
7234 // that (unqualified) name lookup will find it.
Argyrios Kyrtzidis0532df02013-04-26 21:33:35 +00007235 pushExternalDeclIntoScope(D, II);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007236 } else {
7237 // Queue this declaration so that it will be added to the
7238 // translation unit scope and identifier's declaration chain
7239 // once a Sema object is known.
7240 PreloadedDecls.push_back(D);
7241 }
7242 }
7243}
7244
Douglas Gregor8222b892013-01-21 16:52:34 +00007245IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007246 if (ID == 0)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007247 return nullptr;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007248
7249 if (IdentifiersLoaded.empty()) {
7250 Error("no identifier table in AST file");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007251 return nullptr;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007252 }
7253
7254 ID -= 1;
7255 if (!IdentifiersLoaded[ID]) {
7256 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
7257 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
7258 ModuleFile *M = I->second;
7259 unsigned Index = ID - M->BaseIdentifierID;
7260 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
7261
7262 // All of the strings in the AST file are preceded by a 16-bit length.
7263 // Extract that 16-bit length to avoid having to execute strlen().
7264 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
7265 // unsigned integers. This is important to avoid integer overflow when
7266 // we cast them to 'unsigned'.
7267 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
7268 unsigned StrLen = (((unsigned) StrLenPtr[0])
7269 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Douglas Gregor8222b892013-01-21 16:52:34 +00007270 IdentifiersLoaded[ID]
7271 = &PP.getIdentifierTable().get(StringRef(Str, StrLen));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007272 if (DeserializationListener)
7273 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
7274 }
7275
7276 return IdentifiersLoaded[ID];
7277}
7278
Douglas Gregor8222b892013-01-21 16:52:34 +00007279IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
7280 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007281}
7282
7283IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
7284 if (LocalID < NUM_PREDEF_IDENT_IDS)
7285 return LocalID;
7286
7287 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7288 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
7289 assert(I != M.IdentifierRemap.end()
7290 && "Invalid index into identifier index remap");
7291
7292 return LocalID + I->second;
7293}
7294
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00007295MacroInfo *ASTReader::getMacro(MacroID ID) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007296 if (ID == 0)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007297 return nullptr;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007298
7299 if (MacrosLoaded.empty()) {
7300 Error("no macro table in AST file");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007301 return nullptr;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007302 }
7303
7304 ID -= NUM_PREDEF_MACRO_IDS;
7305 if (!MacrosLoaded[ID]) {
7306 GlobalMacroMapType::iterator I
7307 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
7308 assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
7309 ModuleFile *M = I->second;
7310 unsigned Index = ID - M->BaseMacroID;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00007311 MacrosLoaded[ID] = ReadMacroRecord(*M, M->MacroOffsets[Index]);
7312
7313 if (DeserializationListener)
7314 DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
7315 MacrosLoaded[ID]);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007316 }
7317
7318 return MacrosLoaded[ID];
7319}
7320
7321MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
7322 if (LocalID < NUM_PREDEF_MACRO_IDS)
7323 return LocalID;
7324
7325 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7326 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
7327 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
7328
7329 return LocalID + I->second;
7330}
7331
7332serialization::SubmoduleID
7333ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
7334 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
7335 return LocalID;
7336
7337 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7338 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
7339 assert(I != M.SubmoduleRemap.end()
7340 && "Invalid index into submodule index remap");
7341
7342 return LocalID + I->second;
7343}
7344
7345Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
7346 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
7347 assert(GlobalID == 0 && "Unhandled global submodule ID");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007348 return nullptr;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007349 }
7350
7351 if (GlobalID > SubmodulesLoaded.size()) {
7352 Error("submodule ID out of range in AST file");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007353 return nullptr;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007354 }
7355
7356 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
7357}
Douglas Gregorca2ab452013-01-12 01:29:50 +00007358
7359Module *ASTReader::getModule(unsigned ID) {
7360 return getSubmodule(ID);
7361}
7362
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007363Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
7364 return DecodeSelector(getGlobalSelectorID(M, LocalID));
7365}
7366
7367Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
7368 if (ID == 0)
7369 return Selector();
7370
7371 if (ID > SelectorsLoaded.size()) {
7372 Error("selector ID out of range in AST file");
7373 return Selector();
7374 }
7375
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007376 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007377 // Load this selector from the selector table.
7378 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
7379 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
7380 ModuleFile &M = *I->second;
7381 ASTSelectorLookupTrait Trait(*this, M);
7382 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
7383 SelectorsLoaded[ID - 1] =
7384 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
7385 if (DeserializationListener)
7386 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
7387 }
7388
7389 return SelectorsLoaded[ID - 1];
7390}
7391
7392Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
7393 return DecodeSelector(ID);
7394}
7395
7396uint32_t ASTReader::GetNumExternalSelectors() {
7397 // ID 0 (the null selector) is considered an external selector.
7398 return getTotalNumSelectors() + 1;
7399}
7400
7401serialization::SelectorID
7402ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
7403 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
7404 return LocalID;
7405
7406 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7407 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
7408 assert(I != M.SelectorRemap.end()
7409 && "Invalid index into selector index remap");
7410
7411 return LocalID + I->second;
7412}
7413
7414DeclarationName
7415ASTReader::ReadDeclarationName(ModuleFile &F,
7416 const RecordData &Record, unsigned &Idx) {
7417 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
7418 switch (Kind) {
7419 case DeclarationName::Identifier:
Douglas Gregor8222b892013-01-21 16:52:34 +00007420 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007421
7422 case DeclarationName::ObjCZeroArgSelector:
7423 case DeclarationName::ObjCOneArgSelector:
7424 case DeclarationName::ObjCMultiArgSelector:
7425 return DeclarationName(ReadSelector(F, Record, Idx));
7426
7427 case DeclarationName::CXXConstructorName:
7428 return Context.DeclarationNames.getCXXConstructorName(
7429 Context.getCanonicalType(readType(F, Record, Idx)));
7430
7431 case DeclarationName::CXXDestructorName:
7432 return Context.DeclarationNames.getCXXDestructorName(
7433 Context.getCanonicalType(readType(F, Record, Idx)));
7434
7435 case DeclarationName::CXXConversionFunctionName:
7436 return Context.DeclarationNames.getCXXConversionFunctionName(
7437 Context.getCanonicalType(readType(F, Record, Idx)));
7438
7439 case DeclarationName::CXXOperatorName:
7440 return Context.DeclarationNames.getCXXOperatorName(
7441 (OverloadedOperatorKind)Record[Idx++]);
7442
7443 case DeclarationName::CXXLiteralOperatorName:
7444 return Context.DeclarationNames.getCXXLiteralOperatorName(
7445 GetIdentifierInfo(F, Record, Idx));
7446
7447 case DeclarationName::CXXUsingDirective:
7448 return DeclarationName::getUsingDirectiveName();
7449 }
7450
7451 llvm_unreachable("Invalid NameKind!");
7452}
7453
7454void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
7455 DeclarationNameLoc &DNLoc,
7456 DeclarationName Name,
7457 const RecordData &Record, unsigned &Idx) {
7458 switch (Name.getNameKind()) {
7459 case DeclarationName::CXXConstructorName:
7460 case DeclarationName::CXXDestructorName:
7461 case DeclarationName::CXXConversionFunctionName:
7462 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
7463 break;
7464
7465 case DeclarationName::CXXOperatorName:
7466 DNLoc.CXXOperatorName.BeginOpNameLoc
7467 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
7468 DNLoc.CXXOperatorName.EndOpNameLoc
7469 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
7470 break;
7471
7472 case DeclarationName::CXXLiteralOperatorName:
7473 DNLoc.CXXLiteralOperatorName.OpNameLoc
7474 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
7475 break;
7476
7477 case DeclarationName::Identifier:
7478 case DeclarationName::ObjCZeroArgSelector:
7479 case DeclarationName::ObjCOneArgSelector:
7480 case DeclarationName::ObjCMultiArgSelector:
7481 case DeclarationName::CXXUsingDirective:
7482 break;
7483 }
7484}
7485
7486void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
7487 DeclarationNameInfo &NameInfo,
7488 const RecordData &Record, unsigned &Idx) {
7489 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
7490 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
7491 DeclarationNameLoc DNLoc;
7492 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
7493 NameInfo.setInfo(DNLoc);
7494}
7495
7496void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
7497 const RecordData &Record, unsigned &Idx) {
7498 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
7499 unsigned NumTPLists = Record[Idx++];
7500 Info.NumTemplParamLists = NumTPLists;
7501 if (NumTPLists) {
7502 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
7503 for (unsigned i=0; i != NumTPLists; ++i)
7504 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
7505 }
7506}
7507
7508TemplateName
7509ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
7510 unsigned &Idx) {
7511 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
7512 switch (Kind) {
7513 case TemplateName::Template:
7514 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
7515
7516 case TemplateName::OverloadedTemplate: {
7517 unsigned size = Record[Idx++];
7518 UnresolvedSet<8> Decls;
7519 while (size--)
7520 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
7521
7522 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
7523 }
7524
7525 case TemplateName::QualifiedTemplate: {
7526 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
7527 bool hasTemplKeyword = Record[Idx++];
7528 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
7529 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
7530 }
7531
7532 case TemplateName::DependentTemplate: {
7533 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
7534 if (Record[Idx++]) // isIdentifier
7535 return Context.getDependentTemplateName(NNS,
7536 GetIdentifierInfo(F, Record,
7537 Idx));
7538 return Context.getDependentTemplateName(NNS,
7539 (OverloadedOperatorKind)Record[Idx++]);
7540 }
7541
7542 case TemplateName::SubstTemplateTemplateParm: {
7543 TemplateTemplateParmDecl *param
7544 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
7545 if (!param) return TemplateName();
7546 TemplateName replacement = ReadTemplateName(F, Record, Idx);
7547 return Context.getSubstTemplateTemplateParm(param, replacement);
7548 }
7549
7550 case TemplateName::SubstTemplateTemplateParmPack: {
7551 TemplateTemplateParmDecl *Param
7552 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
7553 if (!Param)
7554 return TemplateName();
7555
7556 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
7557 if (ArgPack.getKind() != TemplateArgument::Pack)
7558 return TemplateName();
7559
7560 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
7561 }
7562 }
7563
7564 llvm_unreachable("Unhandled template name kind!");
7565}
7566
7567TemplateArgument
7568ASTReader::ReadTemplateArgument(ModuleFile &F,
7569 const RecordData &Record, unsigned &Idx) {
7570 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
7571 switch (Kind) {
7572 case TemplateArgument::Null:
7573 return TemplateArgument();
7574 case TemplateArgument::Type:
7575 return TemplateArgument(readType(F, Record, Idx));
7576 case TemplateArgument::Declaration: {
7577 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
7578 bool ForReferenceParam = Record[Idx++];
7579 return TemplateArgument(D, ForReferenceParam);
7580 }
7581 case TemplateArgument::NullPtr:
7582 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
7583 case TemplateArgument::Integral: {
7584 llvm::APSInt Value = ReadAPSInt(Record, Idx);
7585 QualType T = readType(F, Record, Idx);
7586 return TemplateArgument(Context, Value, T);
7587 }
7588 case TemplateArgument::Template:
7589 return TemplateArgument(ReadTemplateName(F, Record, Idx));
7590 case TemplateArgument::TemplateExpansion: {
7591 TemplateName Name = ReadTemplateName(F, Record, Idx);
David Blaikiedc84cd52013-02-20 22:23:23 +00007592 Optional<unsigned> NumTemplateExpansions;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007593 if (unsigned NumExpansions = Record[Idx++])
7594 NumTemplateExpansions = NumExpansions - 1;
7595 return TemplateArgument(Name, NumTemplateExpansions);
7596 }
7597 case TemplateArgument::Expression:
7598 return TemplateArgument(ReadExpr(F));
7599 case TemplateArgument::Pack: {
7600 unsigned NumArgs = Record[Idx++];
7601 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
7602 for (unsigned I = 0; I != NumArgs; ++I)
7603 Args[I] = ReadTemplateArgument(F, Record, Idx);
7604 return TemplateArgument(Args, NumArgs);
7605 }
7606 }
7607
7608 llvm_unreachable("Unhandled template argument kind!");
7609}
7610
7611TemplateParameterList *
7612ASTReader::ReadTemplateParameterList(ModuleFile &F,
7613 const RecordData &Record, unsigned &Idx) {
7614 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
7615 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
7616 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
7617
7618 unsigned NumParams = Record[Idx++];
7619 SmallVector<NamedDecl *, 16> Params;
7620 Params.reserve(NumParams);
7621 while (NumParams--)
7622 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
7623
7624 TemplateParameterList* TemplateParams =
7625 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
7626 Params.data(), Params.size(), RAngleLoc);
7627 return TemplateParams;
7628}
7629
7630void
7631ASTReader::
Craig Topper6b9240e2013-07-05 19:34:19 +00007632ReadTemplateArgumentList(SmallVectorImpl<TemplateArgument> &TemplArgs,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007633 ModuleFile &F, const RecordData &Record,
7634 unsigned &Idx) {
7635 unsigned NumTemplateArgs = Record[Idx++];
7636 TemplArgs.reserve(NumTemplateArgs);
7637 while (NumTemplateArgs--)
7638 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
7639}
7640
7641/// \brief Read a UnresolvedSet structure.
Richard Smithc2d77572013-08-30 04:46:40 +00007642void ASTReader::ReadUnresolvedSet(ModuleFile &F, LazyASTUnresolvedSet &Set,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007643 const RecordData &Record, unsigned &Idx) {
7644 unsigned NumDecls = Record[Idx++];
7645 Set.reserve(Context, NumDecls);
7646 while (NumDecls--) {
Richard Smithc2d77572013-08-30 04:46:40 +00007647 DeclID ID = ReadDeclID(F, Record, Idx);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007648 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
Richard Smithc2d77572013-08-30 04:46:40 +00007649 Set.addLazyDecl(Context, ID, AS);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007650 }
7651}
7652
7653CXXBaseSpecifier
7654ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
7655 const RecordData &Record, unsigned &Idx) {
7656 bool isVirtual = static_cast<bool>(Record[Idx++]);
7657 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
7658 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
7659 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
7660 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
7661 SourceRange Range = ReadSourceRange(F, Record, Idx);
7662 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
7663 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
7664 EllipsisLoc);
7665 Result.setInheritConstructors(inheritConstructors);
7666 return Result;
7667}
7668
7669std::pair<CXXCtorInitializer **, unsigned>
7670ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
7671 unsigned &Idx) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007672 CXXCtorInitializer **CtorInitializers = nullptr;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007673 unsigned NumInitializers = Record[Idx++];
7674 if (NumInitializers) {
7675 CtorInitializers
7676 = new (Context) CXXCtorInitializer*[NumInitializers];
7677 for (unsigned i=0; i != NumInitializers; ++i) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007678 TypeSourceInfo *TInfo = nullptr;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007679 bool IsBaseVirtual = false;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007680 FieldDecl *Member = nullptr;
7681 IndirectFieldDecl *IndirectMember = nullptr;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007682
7683 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
7684 switch (Type) {
7685 case CTOR_INITIALIZER_BASE:
7686 TInfo = GetTypeSourceInfo(F, Record, Idx);
7687 IsBaseVirtual = Record[Idx++];
7688 break;
7689
7690 case CTOR_INITIALIZER_DELEGATING:
7691 TInfo = GetTypeSourceInfo(F, Record, Idx);
7692 break;
7693
7694 case CTOR_INITIALIZER_MEMBER:
7695 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
7696 break;
7697
7698 case CTOR_INITIALIZER_INDIRECT_MEMBER:
7699 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
7700 break;
7701 }
7702
7703 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
7704 Expr *Init = ReadExpr(F);
7705 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
7706 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
7707 bool IsWritten = Record[Idx++];
7708 unsigned SourceOrderOrNumArrayIndices;
7709 SmallVector<VarDecl *, 8> Indices;
7710 if (IsWritten) {
7711 SourceOrderOrNumArrayIndices = Record[Idx++];
7712 } else {
7713 SourceOrderOrNumArrayIndices = Record[Idx++];
7714 Indices.reserve(SourceOrderOrNumArrayIndices);
7715 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
7716 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
7717 }
7718
7719 CXXCtorInitializer *BOMInit;
7720 if (Type == CTOR_INITIALIZER_BASE) {
7721 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, IsBaseVirtual,
7722 LParenLoc, Init, RParenLoc,
7723 MemberOrEllipsisLoc);
7724 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
7725 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, LParenLoc,
7726 Init, RParenLoc);
7727 } else if (IsWritten) {
7728 if (Member)
7729 BOMInit = new (Context) CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc,
7730 LParenLoc, Init, RParenLoc);
7731 else
7732 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
7733 MemberOrEllipsisLoc, LParenLoc,
7734 Init, RParenLoc);
7735 } else {
Argyrios Kyrtzidisf8f480f2013-05-30 23:59:46 +00007736 if (IndirectMember) {
7737 assert(Indices.empty() && "Indirect field improperly initialized");
7738 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
7739 MemberOrEllipsisLoc, LParenLoc,
7740 Init, RParenLoc);
7741 } else {
7742 BOMInit = CXXCtorInitializer::Create(Context, Member, MemberOrEllipsisLoc,
7743 LParenLoc, Init, RParenLoc,
7744 Indices.data(), Indices.size());
7745 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007746 }
7747
7748 if (IsWritten)
7749 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
7750 CtorInitializers[i] = BOMInit;
7751 }
7752 }
7753
7754 return std::make_pair(CtorInitializers, NumInitializers);
7755}
7756
7757NestedNameSpecifier *
7758ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
7759 const RecordData &Record, unsigned &Idx) {
7760 unsigned N = Record[Idx++];
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007761 NestedNameSpecifier *NNS = nullptr, *Prev = nullptr;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007762 for (unsigned I = 0; I != N; ++I) {
7763 NestedNameSpecifier::SpecifierKind Kind
7764 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
7765 switch (Kind) {
7766 case NestedNameSpecifier::Identifier: {
7767 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
7768 NNS = NestedNameSpecifier::Create(Context, Prev, II);
7769 break;
7770 }
7771
7772 case NestedNameSpecifier::Namespace: {
7773 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
7774 NNS = NestedNameSpecifier::Create(Context, Prev, NS);
7775 break;
7776 }
7777
7778 case NestedNameSpecifier::NamespaceAlias: {
7779 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
7780 NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
7781 break;
7782 }
7783
7784 case NestedNameSpecifier::TypeSpec:
7785 case NestedNameSpecifier::TypeSpecWithTemplate: {
7786 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
7787 if (!T)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007788 return nullptr;
7789
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007790 bool Template = Record[Idx++];
7791 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
7792 break;
7793 }
7794
7795 case NestedNameSpecifier::Global: {
7796 NNS = NestedNameSpecifier::GlobalSpecifier(Context);
7797 // No associated value, and there can't be a prefix.
7798 break;
7799 }
7800 }
7801 Prev = NNS;
7802 }
7803 return NNS;
7804}
7805
7806NestedNameSpecifierLoc
7807ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
7808 unsigned &Idx) {
7809 unsigned N = Record[Idx++];
7810 NestedNameSpecifierLocBuilder Builder;
7811 for (unsigned I = 0; I != N; ++I) {
7812 NestedNameSpecifier::SpecifierKind Kind
7813 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
7814 switch (Kind) {
7815 case NestedNameSpecifier::Identifier: {
7816 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
7817 SourceRange Range = ReadSourceRange(F, Record, Idx);
7818 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
7819 break;
7820 }
7821
7822 case NestedNameSpecifier::Namespace: {
7823 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
7824 SourceRange Range = ReadSourceRange(F, Record, Idx);
7825 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
7826 break;
7827 }
7828
7829 case NestedNameSpecifier::NamespaceAlias: {
7830 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
7831 SourceRange Range = ReadSourceRange(F, Record, Idx);
7832 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
7833 break;
7834 }
7835
7836 case NestedNameSpecifier::TypeSpec:
7837 case NestedNameSpecifier::TypeSpecWithTemplate: {
7838 bool Template = Record[Idx++];
7839 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
7840 if (!T)
7841 return NestedNameSpecifierLoc();
7842 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
7843
7844 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
7845 Builder.Extend(Context,
7846 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
7847 T->getTypeLoc(), ColonColonLoc);
7848 break;
7849 }
7850
7851 case NestedNameSpecifier::Global: {
7852 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
7853 Builder.MakeGlobal(Context, ColonColonLoc);
7854 break;
7855 }
7856 }
7857 }
7858
7859 return Builder.getWithLocInContext(Context);
7860}
7861
7862SourceRange
7863ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
7864 unsigned &Idx) {
7865 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
7866 SourceLocation end = ReadSourceLocation(F, Record, Idx);
7867 return SourceRange(beg, end);
7868}
7869
7870/// \brief Read an integral value
7871llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
7872 unsigned BitWidth = Record[Idx++];
7873 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
7874 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
7875 Idx += NumWords;
7876 return Result;
7877}
7878
7879/// \brief Read a signed integral value
7880llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
7881 bool isUnsigned = Record[Idx++];
7882 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
7883}
7884
7885/// \brief Read a floating-point value
Tim Northover9ec55f22013-01-22 09:46:51 +00007886llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record,
7887 const llvm::fltSemantics &Sem,
7888 unsigned &Idx) {
7889 return llvm::APFloat(Sem, ReadAPInt(Record, Idx));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007890}
7891
7892// \brief Read a string
7893std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
7894 unsigned Len = Record[Idx++];
7895 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
7896 Idx += Len;
7897 return Result;
7898}
7899
7900VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
7901 unsigned &Idx) {
7902 unsigned Major = Record[Idx++];
7903 unsigned Minor = Record[Idx++];
7904 unsigned Subminor = Record[Idx++];
7905 if (Minor == 0)
7906 return VersionTuple(Major);
7907 if (Subminor == 0)
7908 return VersionTuple(Major, Minor - 1);
7909 return VersionTuple(Major, Minor - 1, Subminor - 1);
7910}
7911
7912CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
7913 const RecordData &Record,
7914 unsigned &Idx) {
7915 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
7916 return CXXTemporary::Create(Context, Decl);
7917}
7918
7919DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Argyrios Kyrtzidis3b7deda2013-05-24 05:44:08 +00007920 return Diag(CurrentImportLoc, DiagID);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007921}
7922
7923DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
7924 return Diags.Report(Loc, DiagID);
7925}
7926
7927/// \brief Retrieve the identifier table associated with the
7928/// preprocessor.
7929IdentifierTable &ASTReader::getIdentifierTable() {
7930 return PP.getIdentifierTable();
7931}
7932
7933/// \brief Record that the given ID maps to the given switch-case
7934/// statement.
7935void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007936 assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007937 "Already have a SwitchCase with this ID");
7938 (*CurrSwitchCaseStmts)[ID] = SC;
7939}
7940
7941/// \brief Retrieve the switch-case statement with the given ID.
7942SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07007943 assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007944 return (*CurrSwitchCaseStmts)[ID];
7945}
7946
7947void ASTReader::ClearSwitchCaseIDs() {
7948 CurrSwitchCaseStmts->clear();
7949}
7950
7951void ASTReader::ReadComments() {
7952 std::vector<RawComment *> Comments;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00007953 for (SmallVectorImpl<std::pair<BitstreamCursor,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007954 serialization::ModuleFile *> >::iterator
7955 I = CommentsCursors.begin(),
7956 E = CommentsCursors.end();
7957 I != E; ++I) {
Stephen Hines651f13c2014-04-23 16:59:28 -07007958 Comments.clear();
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00007959 BitstreamCursor &Cursor = I->first;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007960 serialization::ModuleFile &F = *I->second;
7961 SavedStreamPosition SavedPosition(Cursor);
7962
7963 RecordData Record;
7964 while (true) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00007965 llvm::BitstreamEntry Entry =
7966 Cursor.advanceSkippingSubblocks(BitstreamCursor::AF_DontPopBlockAtEnd);
Stephen Hines651f13c2014-04-23 16:59:28 -07007967
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00007968 switch (Entry.Kind) {
7969 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
7970 case llvm::BitstreamEntry::Error:
7971 Error("malformed block record in AST file");
7972 return;
7973 case llvm::BitstreamEntry::EndBlock:
7974 goto NextCursor;
7975 case llvm::BitstreamEntry::Record:
7976 // The interesting case.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007977 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007978 }
7979
7980 // Read a record.
7981 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00007982 switch ((CommentRecordTypes)Cursor.readRecord(Entry.ID, Record)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007983 case COMMENTS_RAW_COMMENT: {
7984 unsigned Idx = 0;
7985 SourceRange SR = ReadSourceRange(F, Record, Idx);
7986 RawComment::CommentKind Kind =
7987 (RawComment::CommentKind) Record[Idx++];
7988 bool IsTrailingComment = Record[Idx++];
7989 bool IsAlmostTrailingComment = Record[Idx++];
Dmitri Gribenko6fd7d302013-04-10 15:35:17 +00007990 Comments.push_back(new (Context) RawComment(
7991 SR, Kind, IsTrailingComment, IsAlmostTrailingComment,
7992 Context.getLangOpts().CommentOpts.ParseAllComments));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007993 break;
7994 }
7995 }
7996 }
Stephen Hines651f13c2014-04-23 16:59:28 -07007997 NextCursor:
7998 Context.Comments.addDeserializedComments(Comments);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007999 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00008000}
8001
Stephen Hines6bcf27b2014-05-29 04:14:42 -07008002std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) {
8003 // If we know the owning module, use it.
8004 if (Module *M = D->getOwningModule())
8005 return M->getFullModuleName();
8006
8007 // Otherwise, use the name of the top-level module the decl is within.
8008 if (ModuleFile *M = getOwningModuleFile(D))
8009 return M->ModuleName;
8010
8011 // Not from a module.
8012 return "";
8013}
8014
Guy Benyei7f92f2d2012-12-18 14:30:41 +00008015void ASTReader::finishPendingActions() {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07008016 while (!PendingIdentifierInfos.empty() ||
8017 !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() ||
Richard Smith3c40a282013-10-18 06:05:18 +00008018 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
Stephen Hines6bcf27b2014-05-29 04:14:42 -07008019 !PendingUpdateRecords.empty() || !PendingOdrMergeChecks.empty()) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00008020 // If any identifiers with corresponding top-level declarations have
8021 // been loaded, load those declarations now.
Craig Topperee0a4792013-07-05 04:33:53 +00008022 typedef llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2> >
8023 TopLevelDeclsMap;
8024 TopLevelDeclsMap TopLevelDecls;
8025
Guy Benyei7f92f2d2012-12-18 14:30:41 +00008026 while (!PendingIdentifierInfos.empty()) {
Douglas Gregoraa945902013-02-18 15:53:43 +00008027 IdentifierInfo *II = PendingIdentifierInfos.back().first;
Stephen Hines651f13c2014-04-23 16:59:28 -07008028 SmallVector<uint32_t, 4> DeclIDs =
8029 std::move(PendingIdentifierInfos.back().second);
Douglas Gregorcc9bdcb2013-02-19 18:26:28 +00008030 PendingIdentifierInfos.pop_back();
Douglas Gregoraa945902013-02-18 15:53:43 +00008031
8032 SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00008033 }
Stephen Hines651f13c2014-04-23 16:59:28 -07008034
Stephen Hines6bcf27b2014-05-29 04:14:42 -07008035 // For each decl chain that we wanted to complete while deserializing, mark
8036 // it as "still needs to be completed".
8037 for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
8038 markIncompleteDeclChain(PendingIncompleteDeclChains[I]);
8039 }
8040 PendingIncompleteDeclChains.clear();
8041
Guy Benyei7f92f2d2012-12-18 14:30:41 +00008042 // Load pending declaration chains.
8043 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) {
8044 loadPendingDeclChain(PendingDeclChains[I]);
8045 PendingDeclChainsKnown.erase(PendingDeclChains[I]);
8046 }
8047 PendingDeclChains.clear();
8048
Douglas Gregoraa945902013-02-18 15:53:43 +00008049 // Make the most recent of the top-level declarations visible.
Craig Topperee0a4792013-07-05 04:33:53 +00008050 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
8051 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
Douglas Gregoraa945902013-02-18 15:53:43 +00008052 IdentifierInfo *II = TLD->first;
8053 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
Argyrios Kyrtzidis0532df02013-04-26 21:33:35 +00008054 pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
Douglas Gregoraa945902013-02-18 15:53:43 +00008055 }
8056 }
8057
Guy Benyei7f92f2d2012-12-18 14:30:41 +00008058 // Load any pending macro definitions.
8059 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00008060 IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
8061 SmallVector<PendingMacroInfo, 2> GlobalIDs;
8062 GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
8063 // Initialize the macro history from chained-PCHs ahead of module imports.
Stephen Hines651f13c2014-04-23 16:59:28 -07008064 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00008065 ++IDIdx) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00008066 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
8067 if (Info.M->Kind != MK_Module)
8068 resolvePendingMacro(II, Info);
8069 }
8070 // Handle module imports.
Stephen Hines651f13c2014-04-23 16:59:28 -07008071 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00008072 ++IDIdx) {
8073 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
8074 if (Info.M->Kind == MK_Module)
8075 resolvePendingMacro(II, Info);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00008076 }
8077 }
8078 PendingMacroIDs.clear();
Argyrios Kyrtzidis7640b022013-02-16 00:48:59 +00008079
8080 // Wire up the DeclContexts for Decls that we delayed setting until
8081 // recursive loading is completed.
8082 while (!PendingDeclContextInfos.empty()) {
8083 PendingDeclContextInfo Info = PendingDeclContextInfos.front();
8084 PendingDeclContextInfos.pop_front();
8085 DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
8086 DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
8087 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
8088 }
Richard Smith3c40a282013-10-18 06:05:18 +00008089
Stephen Hines6bcf27b2014-05-29 04:14:42 -07008090 // Perform any pending declaration updates.
8091 while (!PendingUpdateRecords.empty()) {
8092 auto Update = PendingUpdateRecords.pop_back_val();
8093 ReadingKindTracker ReadingKind(Read_Decl, *this);
8094 loadDeclUpdateRecords(Update.first, Update.second);
8095 }
8096
8097 // Trigger the import of the full definition of each class that had any
8098 // odr-merging problems, so we can produce better diagnostics for them.
8099 for (auto &Merge : PendingOdrMergeFailures) {
8100 Merge.first->buildLookup();
8101 Merge.first->decls_begin();
8102 Merge.first->bases_begin();
8103 Merge.first->vbases_begin();
8104 for (auto *RD : Merge.second) {
8105 RD->decls_begin();
8106 RD->bases_begin();
8107 RD->vbases_begin();
8108 }
8109 }
8110
Richard Smith3c40a282013-10-18 06:05:18 +00008111 // For each declaration from a merged context, check that the canonical
8112 // definition of that context also contains a declaration of the same
8113 // entity.
8114 while (!PendingOdrMergeChecks.empty()) {
8115 NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
8116
8117 // FIXME: Skip over implicit declarations for now. This matters for things
8118 // like implicitly-declared special member functions. This isn't entirely
8119 // correct; we can end up with multiple unmerged declarations of the same
8120 // implicit entity.
8121 if (D->isImplicit())
8122 continue;
8123
8124 DeclContext *CanonDef = D->getDeclContext();
8125 DeclContext::lookup_result R = CanonDef->lookup(D->getDeclName());
8126
8127 bool Found = false;
8128 const Decl *DCanon = D->getCanonicalDecl();
8129
8130 llvm::SmallVector<const NamedDecl*, 4> Candidates;
8131 for (DeclContext::lookup_iterator I = R.begin(), E = R.end();
8132 !Found && I != E; ++I) {
Stephen Hines651f13c2014-04-23 16:59:28 -07008133 for (auto RI : (*I)->redecls()) {
8134 if (RI->getLexicalDeclContext() == CanonDef) {
Richard Smith3c40a282013-10-18 06:05:18 +00008135 // This declaration is present in the canonical definition. If it's
8136 // in the same redecl chain, it's the one we're looking for.
Stephen Hines651f13c2014-04-23 16:59:28 -07008137 if (RI->getCanonicalDecl() == DCanon)
Richard Smith3c40a282013-10-18 06:05:18 +00008138 Found = true;
8139 else
Stephen Hines651f13c2014-04-23 16:59:28 -07008140 Candidates.push_back(cast<NamedDecl>(RI));
Richard Smith3c40a282013-10-18 06:05:18 +00008141 break;
8142 }
8143 }
8144 }
8145
8146 if (!Found) {
8147 D->setInvalidDecl();
8148
Stephen Hines6bcf27b2014-05-29 04:14:42 -07008149 std::string CanonDefModule =
8150 getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef));
Richard Smith3c40a282013-10-18 06:05:18 +00008151 Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07008152 << D << getOwningModuleNameForDiagnostic(D)
8153 << CanonDef << CanonDefModule.empty() << CanonDefModule;
Richard Smith3c40a282013-10-18 06:05:18 +00008154
8155 if (Candidates.empty())
8156 Diag(cast<Decl>(CanonDef)->getLocation(),
8157 diag::note_module_odr_violation_no_possible_decls) << D;
8158 else {
8159 for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
8160 Diag(Candidates[I]->getLocation(),
8161 diag::note_module_odr_violation_possible_decl)
8162 << Candidates[I];
8163 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07008164
8165 DiagnosedOdrMergeFailures.insert(CanonDef);
Richard Smith3c40a282013-10-18 06:05:18 +00008166 }
8167 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00008168 }
8169
8170 // If we deserialized any C++ or Objective-C class definitions, any
8171 // Objective-C protocol definitions, or any redeclarable templates, make sure
8172 // that all redeclarations point to the definitions. Note that this can only
8173 // happen now, after the redeclaration chains have been fully wired.
8174 for (llvm::SmallPtrSet<Decl *, 4>::iterator D = PendingDefinitions.begin(),
8175 DEnd = PendingDefinitions.end();
8176 D != DEnd; ++D) {
8177 if (TagDecl *TD = dyn_cast<TagDecl>(*D)) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07008178 if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00008179 // Make sure that the TagType points at the definition.
8180 const_cast<TagType*>(TagT)->decl = TD;
8181 }
8182
Stephen Hines651f13c2014-04-23 16:59:28 -07008183 if (auto RD = dyn_cast<CXXRecordDecl>(*D)) {
8184 for (auto R : RD->redecls())
8185 cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00008186 }
8187
8188 continue;
8189 }
8190
Stephen Hines651f13c2014-04-23 16:59:28 -07008191 if (auto ID = dyn_cast<ObjCInterfaceDecl>(*D)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00008192 // Make sure that the ObjCInterfaceType points at the definition.
8193 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
8194 ->Decl = ID;
8195
Stephen Hines651f13c2014-04-23 16:59:28 -07008196 for (auto R : ID->redecls())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00008197 R->Data = ID->Data;
8198
8199 continue;
8200 }
8201
Stephen Hines651f13c2014-04-23 16:59:28 -07008202 if (auto PD = dyn_cast<ObjCProtocolDecl>(*D)) {
8203 for (auto R : PD->redecls())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00008204 R->Data = PD->Data;
8205
8206 continue;
8207 }
8208
Stephen Hines651f13c2014-04-23 16:59:28 -07008209 auto RTD = cast<RedeclarableTemplateDecl>(*D)->getCanonicalDecl();
8210 for (auto R : RTD->redecls())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00008211 R->Common = RTD->Common;
8212 }
8213 PendingDefinitions.clear();
8214
8215 // Load the bodies of any functions or methods we've encountered. We do
8216 // this now (delayed) so that we can be sure that the declaration chains
8217 // have been fully wired up.
8218 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
8219 PBEnd = PendingBodies.end();
8220 PB != PBEnd; ++PB) {
8221 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
8222 // FIXME: Check for =delete/=default?
8223 // FIXME: Complain about ODR violations here?
8224 if (!getContext().getLangOpts().Modules || !FD->hasBody())
8225 FD->setLazyBody(PB->second);
8226 continue;
8227 }
8228
8229 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
8230 if (!getContext().getLangOpts().Modules || !MD->hasBody())
8231 MD->setLazyBody(PB->second);
8232 }
8233 PendingBodies.clear();
Stephen Hines6bcf27b2014-05-29 04:14:42 -07008234
8235 // Issue any pending ODR-failure diagnostics.
8236 for (auto &Merge : PendingOdrMergeFailures) {
8237 if (!DiagnosedOdrMergeFailures.insert(Merge.first))
8238 continue;
8239
8240 bool Diagnosed = false;
8241 for (auto *RD : Merge.second) {
8242 // Multiple different declarations got merged together; tell the user
8243 // where they came from.
8244 if (Merge.first != RD) {
8245 // FIXME: Walk the definition, figure out what's different,
8246 // and diagnose that.
8247 if (!Diagnosed) {
8248 std::string Module = getOwningModuleNameForDiagnostic(Merge.first);
8249 Diag(Merge.first->getLocation(),
8250 diag::err_module_odr_violation_different_definitions)
8251 << Merge.first << Module.empty() << Module;
8252 Diagnosed = true;
8253 }
8254
8255 Diag(RD->getLocation(),
8256 diag::note_module_odr_violation_different_definitions)
8257 << getOwningModuleNameForDiagnostic(RD);
8258 }
8259 }
8260
8261 if (!Diagnosed) {
8262 // All definitions are updates to the same declaration. This happens if a
8263 // module instantiates the declaration of a class template specialization
8264 // and two or more other modules instantiate its definition.
8265 //
8266 // FIXME: Indicate which modules had instantiations of this definition.
8267 // FIXME: How can this even happen?
8268 Diag(Merge.first->getLocation(),
8269 diag::err_module_odr_violation_different_instantiations)
8270 << Merge.first;
8271 }
8272 }
8273 PendingOdrMergeFailures.clear();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00008274}
8275
8276void ASTReader::FinishedDeserializing() {
8277 assert(NumCurrentElementsDeserializing &&
8278 "FinishedDeserializing not paired with StartedDeserializing");
8279 if (NumCurrentElementsDeserializing == 1) {
8280 // We decrease NumCurrentElementsDeserializing only after pending actions
8281 // are finished, to avoid recursively re-calling finishPendingActions().
8282 finishPendingActions();
8283 }
8284 --NumCurrentElementsDeserializing;
8285
Stephen Hines651f13c2014-04-23 16:59:28 -07008286 if (NumCurrentElementsDeserializing == 0 && Consumer) {
8287 // We are not in recursive loading, so it's safe to pass the "interesting"
8288 // decls to the consumer.
8289 PassInterestingDeclsToConsumer();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00008290 }
8291}
8292
Argyrios Kyrtzidis0532df02013-04-26 21:33:35 +00008293void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
Rafael Espindola87bcee82013-10-19 16:55:03 +00008294 D = D->getMostRecentDecl();
Argyrios Kyrtzidis0532df02013-04-26 21:33:35 +00008295
8296 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
8297 SemaObj->TUScope->AddDecl(D);
8298 } else if (SemaObj->TUScope) {
8299 // Adding the decl to IdResolver may have failed because it was already in
8300 // (even though it was not added in scope). If it is already in, make sure
8301 // it gets in the scope as well.
8302 if (std::find(SemaObj->IdResolver.begin(Name),
8303 SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
8304 SemaObj->TUScope->AddDecl(D);
8305 }
8306}
8307
Stephen Hines6bcf27b2014-05-29 04:14:42 -07008308ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context, StringRef isysroot,
8309 bool DisableValidation, bool AllowASTWithCompilerErrors,
8310 bool AllowConfigurationMismatch, bool ValidateSystemInputs,
Stephen Hines651f13c2014-04-23 16:59:28 -07008311 bool UseGlobalIndex)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07008312 : Listener(new PCHValidator(PP, *this)), DeserializationListener(nullptr),
8313 OwnsDeserializationListener(false), SourceMgr(PP.getSourceManager()),
8314 FileMgr(PP.getFileManager()), Diags(PP.getDiagnostics()),
8315 SemaObj(nullptr), PP(PP), Context(Context), Consumer(nullptr),
8316 ModuleMgr(PP.getFileManager()), isysroot(isysroot),
8317 DisableValidation(DisableValidation),
8318 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
8319 AllowConfigurationMismatch(AllowConfigurationMismatch),
8320 ValidateSystemInputs(ValidateSystemInputs),
8321 UseGlobalIndex(UseGlobalIndex), TriedLoadingGlobalIndex(false),
8322 CurrSwitchCaseStmts(&SwitchCaseStmts),
8323 NumSLocEntriesRead(0), TotalNumSLocEntries(0), NumStatementsRead(0),
8324 TotalNumStatements(0), NumMacrosRead(0), TotalNumMacros(0),
8325 NumIdentifierLookups(0), NumIdentifierLookupHits(0), NumSelectorsRead(0),
8326 NumMethodPoolEntriesRead(0), NumMethodPoolLookups(0),
8327 NumMethodPoolHits(0), NumMethodPoolTableLookups(0),
8328 NumMethodPoolTableHits(0), TotalNumMethodPoolEntries(0),
8329 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
8330 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
8331 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
8332 PassingDeclsToConsumer(false), NumCXXBaseSpecifiersLoaded(0),
8333 ReadingKind(Read_None) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00008334 SourceMgr.setExternalSLocEntrySource(this);
8335}
8336
8337ASTReader::~ASTReader() {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07008338 if (OwnsDeserializationListener)
8339 delete DeserializationListener;
8340
Guy Benyei7f92f2d2012-12-18 14:30:41 +00008341 for (DeclContextVisibleUpdatesPending::iterator
8342 I = PendingVisibleUpdates.begin(),
8343 E = PendingVisibleUpdates.end();
8344 I != E; ++I) {
8345 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
8346 F = I->second.end();
8347 J != F; ++J)
8348 delete J->first;
8349 }
8350}