blob: 62fefed4107f4d1e092c01ce0eb2138a7782fd66 [file] [log] [blame]
Richard Smith9e2341d2015-03-23 03:25:59 +00001//===-- ASTReader.cpp - AST File Reader ----------------------------------===//
Guy Benyei11169dd2012-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"
Benjamin Kramerf3ca26982014-05-10 16:31:55 +000025#include "clang/Basic/DiagnosticOptions.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000026#include "clang/Basic/FileManager.h"
Guy Benyei11169dd2012-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"
Ben Langmuirb92de022014-04-29 16:25:26 +000033#include "clang/Frontend/Utils.h"
Guy Benyei11169dd2012-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 Gregore060e572013-01-25 01:03:03 +000043#include "clang/Serialization/GlobalModuleIndex.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000044#include "clang/Serialization/ModuleManager.h"
45#include "clang/Serialization/SerializationDiagnostic.h"
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +000046#include "llvm/ADT/Hashing.h"
Guy Benyei11169dd2012-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"
Dmitri Gribenkof430da42014-02-12 10:33:14 +000054#include "llvm/Support/raw_ostream.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000055#include <algorithm>
Chris Lattner91f373e2013-01-20 00:57:52 +000056#include <cstdio>
Guy Benyei11169dd2012-12-18 14:30:41 +000057#include <iterator>
Rafael Espindola8a8e5542014-06-12 17:19:42 +000058#include <system_error>
Guy Benyei11169dd2012-12-18 14:30:41 +000059
60using namespace clang;
61using namespace clang::serialization;
62using namespace clang::serialization::reader;
Chris Lattner7fb3bef2013-01-20 00:56:42 +000063using llvm::BitstreamCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +000064
Ben Langmuircb69b572014-03-07 06:40:32 +000065
66//===----------------------------------------------------------------------===//
67// ChainedASTReaderListener implementation
68//===----------------------------------------------------------------------===//
69
70bool
71ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) {
72 return First->ReadFullVersionInformation(FullVersion) ||
73 Second->ReadFullVersionInformation(FullVersion);
74}
Ben Langmuir4f5212a2014-04-14 22:12:44 +000075void 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}
Richard Smith1e2cf0d2014-10-31 02:28:58 +000083bool
84ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts,
85 bool Complain,
86 bool AllowCompatibleDifferences) {
87 return First->ReadLanguageOptions(LangOpts, Complain,
88 AllowCompatibleDifferences) ||
89 Second->ReadLanguageOptions(LangOpts, Complain,
90 AllowCompatibleDifferences);
Ben Langmuircb69b572014-03-07 06:40:32 +000091}
Chandler Carruth0d745bc2015-03-14 04:47:43 +000092bool ChainedASTReaderListener::ReadTargetOptions(
93 const TargetOptions &TargetOpts, bool Complain,
94 bool AllowCompatibleDifferences) {
95 return First->ReadTargetOptions(TargetOpts, Complain,
96 AllowCompatibleDifferences) ||
97 Second->ReadTargetOptions(TargetOpts, Complain,
98 AllowCompatibleDifferences);
Ben Langmuircb69b572014-03-07 06:40:32 +000099}
100bool ChainedASTReaderListener::ReadDiagnosticOptions(
Ben Langmuirb92de022014-04-29 16:25:26 +0000101 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
Ben Langmuircb69b572014-03-07 06:40:32 +0000102 return First->ReadDiagnosticOptions(DiagOpts, Complain) ||
103 Second->ReadDiagnosticOptions(DiagOpts, Complain);
104}
105bool
106ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts,
107 bool Complain) {
108 return First->ReadFileSystemOptions(FSOpts, Complain) ||
109 Second->ReadFileSystemOptions(FSOpts, Complain);
110}
111
112bool ChainedASTReaderListener::ReadHeaderSearchOptions(
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +0000113 const HeaderSearchOptions &HSOpts, StringRef SpecificModuleCachePath,
114 bool Complain) {
115 return First->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
116 Complain) ||
117 Second->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
118 Complain);
Ben Langmuircb69b572014-03-07 06:40:32 +0000119}
120bool ChainedASTReaderListener::ReadPreprocessorOptions(
121 const PreprocessorOptions &PPOpts, bool Complain,
122 std::string &SuggestedPredefines) {
123 return First->ReadPreprocessorOptions(PPOpts, Complain,
124 SuggestedPredefines) ||
125 Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines);
126}
127void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M,
128 unsigned Value) {
129 First->ReadCounter(M, Value);
130 Second->ReadCounter(M, Value);
131}
132bool ChainedASTReaderListener::needsInputFileVisitation() {
133 return First->needsInputFileVisitation() ||
134 Second->needsInputFileVisitation();
135}
136bool ChainedASTReaderListener::needsSystemInputFileVisitation() {
137 return First->needsSystemInputFileVisitation() ||
138 Second->needsSystemInputFileVisitation();
139}
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +0000140void ChainedASTReaderListener::visitModuleFile(StringRef Filename) {
141 First->visitModuleFile(Filename);
142 Second->visitModuleFile(Filename);
143}
Ben Langmuircb69b572014-03-07 06:40:32 +0000144bool ChainedASTReaderListener::visitInputFile(StringRef Filename,
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +0000145 bool isSystem,
146 bool isOverridden) {
Justin Bognerc65a66d2014-05-22 06:04:59 +0000147 bool Continue = false;
148 if (First->needsInputFileVisitation() &&
149 (!isSystem || First->needsSystemInputFileVisitation()))
150 Continue |= First->visitInputFile(Filename, isSystem, isOverridden);
151 if (Second->needsInputFileVisitation() &&
152 (!isSystem || Second->needsSystemInputFileVisitation()))
153 Continue |= Second->visitInputFile(Filename, isSystem, isOverridden);
154 return Continue;
Ben Langmuircb69b572014-03-07 06:40:32 +0000155}
156
Guy Benyei11169dd2012-12-18 14:30:41 +0000157//===----------------------------------------------------------------------===//
158// PCH validator implementation
159//===----------------------------------------------------------------------===//
160
161ASTReaderListener::~ASTReaderListener() {}
162
163/// \brief Compare the given set of language options against an existing set of
164/// language options.
165///
166/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
Richard Smith1e2cf0d2014-10-31 02:28:58 +0000167/// \param AllowCompatibleDifferences If true, differences between compatible
168/// language options will be permitted.
Guy Benyei11169dd2012-12-18 14:30:41 +0000169///
170/// \returns true if the languagae options mis-match, false otherwise.
171static bool checkLanguageOptions(const LangOptions &LangOpts,
172 const LangOptions &ExistingLangOpts,
Richard Smith1e2cf0d2014-10-31 02:28:58 +0000173 DiagnosticsEngine *Diags,
174 bool AllowCompatibleDifferences = true) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000175#define LANGOPT(Name, Bits, Default, Description) \
176 if (ExistingLangOpts.Name != LangOpts.Name) { \
177 if (Diags) \
178 Diags->Report(diag::err_pch_langopt_mismatch) \
179 << Description << LangOpts.Name << ExistingLangOpts.Name; \
180 return true; \
181 }
182
183#define VALUE_LANGOPT(Name, Bits, Default, Description) \
184 if (ExistingLangOpts.Name != LangOpts.Name) { \
185 if (Diags) \
186 Diags->Report(diag::err_pch_langopt_value_mismatch) \
187 << Description; \
188 return true; \
189 }
190
191#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
192 if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \
193 if (Diags) \
194 Diags->Report(diag::err_pch_langopt_value_mismatch) \
195 << Description; \
196 return true; \
197 }
198
Richard Smith1e2cf0d2014-10-31 02:28:58 +0000199#define COMPATIBLE_LANGOPT(Name, Bits, Default, Description) \
200 if (!AllowCompatibleDifferences) \
201 LANGOPT(Name, Bits, Default, Description)
202
203#define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description) \
204 if (!AllowCompatibleDifferences) \
205 ENUM_LANGOPT(Name, Bits, Default, Description)
206
Guy Benyei11169dd2012-12-18 14:30:41 +0000207#define BENIGN_LANGOPT(Name, Bits, Default, Description)
208#define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
209#include "clang/Basic/LangOptions.def"
210
211 if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
212 if (Diags)
213 Diags->Report(diag::err_pch_langopt_value_mismatch)
214 << "target Objective-C runtime";
215 return true;
216 }
217
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +0000218 if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
219 LangOpts.CommentOpts.BlockCommandNames) {
220 if (Diags)
221 Diags->Report(diag::err_pch_langopt_value_mismatch)
222 << "block command names";
223 return true;
224 }
225
Guy Benyei11169dd2012-12-18 14:30:41 +0000226 return false;
227}
228
229/// \brief Compare the given set of target options against an existing set of
230/// target options.
231///
232/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
233///
234/// \returns true if the target options mis-match, false otherwise.
235static bool checkTargetOptions(const TargetOptions &TargetOpts,
236 const TargetOptions &ExistingTargetOpts,
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000237 DiagnosticsEngine *Diags,
238 bool AllowCompatibleDifferences = true) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000239#define CHECK_TARGET_OPT(Field, Name) \
240 if (TargetOpts.Field != ExistingTargetOpts.Field) { \
241 if (Diags) \
242 Diags->Report(diag::err_pch_targetopt_mismatch) \
243 << Name << TargetOpts.Field << ExistingTargetOpts.Field; \
244 return true; \
245 }
246
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000247 // The triple and ABI must match exactly.
Guy Benyei11169dd2012-12-18 14:30:41 +0000248 CHECK_TARGET_OPT(Triple, "target");
Guy Benyei11169dd2012-12-18 14:30:41 +0000249 CHECK_TARGET_OPT(ABI, "target ABI");
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000250
251 // We can tolerate different CPUs in many cases, notably when one CPU
252 // supports a strict superset of another. When allowing compatible
253 // differences skip this check.
254 if (!AllowCompatibleDifferences)
255 CHECK_TARGET_OPT(CPU, "target CPU");
256
Guy Benyei11169dd2012-12-18 14:30:41 +0000257#undef CHECK_TARGET_OPT
258
259 // Compare feature sets.
260 SmallVector<StringRef, 4> ExistingFeatures(
261 ExistingTargetOpts.FeaturesAsWritten.begin(),
262 ExistingTargetOpts.FeaturesAsWritten.end());
263 SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
264 TargetOpts.FeaturesAsWritten.end());
265 std::sort(ExistingFeatures.begin(), ExistingFeatures.end());
266 std::sort(ReadFeatures.begin(), ReadFeatures.end());
267
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000268 // We compute the set difference in both directions explicitly so that we can
269 // diagnose the differences differently.
270 SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures;
271 std::set_difference(
272 ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(),
273 ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures));
274 std::set_difference(ReadFeatures.begin(), ReadFeatures.end(),
275 ExistingFeatures.begin(), ExistingFeatures.end(),
276 std::back_inserter(UnmatchedReadFeatures));
Guy Benyei11169dd2012-12-18 14:30:41 +0000277
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000278 // If we are allowing compatible differences and the read feature set is
279 // a strict subset of the existing feature set, there is nothing to diagnose.
280 if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty())
281 return false;
Guy Benyei11169dd2012-12-18 14:30:41 +0000282
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000283 if (Diags) {
284 for (StringRef Feature : UnmatchedReadFeatures)
Guy Benyei11169dd2012-12-18 14:30:41 +0000285 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000286 << /* is-existing-feature */ false << Feature;
287 for (StringRef Feature : UnmatchedExistingFeatures)
288 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
289 << /* is-existing-feature */ true << Feature;
Guy Benyei11169dd2012-12-18 14:30:41 +0000290 }
291
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000292 return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty();
Guy Benyei11169dd2012-12-18 14:30:41 +0000293}
294
295bool
296PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
Richard Smith1e2cf0d2014-10-31 02:28:58 +0000297 bool Complain,
298 bool AllowCompatibleDifferences) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000299 const LangOptions &ExistingLangOpts = PP.getLangOpts();
300 return checkLanguageOptions(LangOpts, ExistingLangOpts,
Richard Smith1e2cf0d2014-10-31 02:28:58 +0000301 Complain ? &Reader.Diags : nullptr,
302 AllowCompatibleDifferences);
Guy Benyei11169dd2012-12-18 14:30:41 +0000303}
304
305bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000306 bool Complain,
307 bool AllowCompatibleDifferences) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000308 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
309 return checkTargetOptions(TargetOpts, ExistingTargetOpts,
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000310 Complain ? &Reader.Diags : nullptr,
311 AllowCompatibleDifferences);
Guy Benyei11169dd2012-12-18 14:30:41 +0000312}
313
314namespace {
315 typedef llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> >
316 MacroDefinitionsMap;
Craig Topper3598eb72013-07-05 04:43:31 +0000317 typedef llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> >
318 DeclsMap;
Guy Benyei11169dd2012-12-18 14:30:41 +0000319}
320
Ben Langmuirb92de022014-04-29 16:25:26 +0000321static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags,
322 DiagnosticsEngine &Diags,
323 bool Complain) {
324 typedef DiagnosticsEngine::Level Level;
325
326 // Check current mappings for new -Werror mappings, and the stored mappings
327 // for cases that were explicitly mapped to *not* be errors that are now
328 // errors because of options like -Werror.
329 DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags };
330
331 for (DiagnosticsEngine *MappingSource : MappingSources) {
332 for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {
333 diag::kind DiagID = DiagIDMappingPair.first;
334 Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation());
335 if (CurLevel < DiagnosticsEngine::Error)
336 continue; // not significant
337 Level StoredLevel =
338 StoredDiags.getDiagnosticLevel(DiagID, SourceLocation());
339 if (StoredLevel < DiagnosticsEngine::Error) {
340 if (Complain)
341 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" +
342 Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str();
343 return true;
344 }
345 }
346 }
347
348 return false;
349}
350
Alp Tokerac4e8e52014-06-22 21:58:33 +0000351static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) {
352 diag::Severity Ext = Diags.getExtensionHandlingBehavior();
353 if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors())
354 return true;
355 return Ext >= diag::Severity::Error;
Ben Langmuirb92de022014-04-29 16:25:26 +0000356}
357
358static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags,
359 DiagnosticsEngine &Diags,
360 bool IsSystem, bool Complain) {
361 // Top-level options
362 if (IsSystem) {
363 if (Diags.getSuppressSystemWarnings())
364 return false;
365 // If -Wsystem-headers was not enabled before, be conservative
366 if (StoredDiags.getSuppressSystemWarnings()) {
367 if (Complain)
368 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers";
369 return true;
370 }
371 }
372
373 if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) {
374 if (Complain)
375 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror";
376 return true;
377 }
378
379 if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() &&
380 !StoredDiags.getEnableAllWarnings()) {
381 if (Complain)
382 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror";
383 return true;
384 }
385
386 if (isExtHandlingFromDiagsError(Diags) &&
387 !isExtHandlingFromDiagsError(StoredDiags)) {
388 if (Complain)
389 Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors";
390 return true;
391 }
392
393 return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain);
394}
395
396bool PCHValidator::ReadDiagnosticOptions(
397 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
398 DiagnosticsEngine &ExistingDiags = PP.getDiagnostics();
399 IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs());
400 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
Alp Tokerf994cef2014-07-05 03:08:06 +0000401 new DiagnosticsEngine(DiagIDs, DiagOpts.get()));
Ben Langmuirb92de022014-04-29 16:25:26 +0000402 // This should never fail, because we would have processed these options
403 // before writing them to an ASTFile.
404 ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false);
405
406 ModuleManager &ModuleMgr = Reader.getModuleManager();
407 assert(ModuleMgr.size() >= 1 && "what ASTFile is this then");
408
409 // If the original import came from a file explicitly generated by the user,
410 // don't check the diagnostic mappings.
411 // FIXME: currently this is approximated by checking whether this is not a
Richard Smithe842a472014-10-22 02:05:46 +0000412 // module import of an implicitly-loaded module file.
Ben Langmuirb92de022014-04-29 16:25:26 +0000413 // Note: ModuleMgr.rbegin() may not be the current module, but it must be in
414 // the transitive closure of its imports, since unrelated modules cannot be
415 // imported until after this module finishes validation.
416 ModuleFile *TopImport = *ModuleMgr.rbegin();
417 while (!TopImport->ImportedBy.empty())
418 TopImport = TopImport->ImportedBy[0];
Richard Smithe842a472014-10-22 02:05:46 +0000419 if (TopImport->Kind != MK_ImplicitModule)
Ben Langmuirb92de022014-04-29 16:25:26 +0000420 return false;
421
422 StringRef ModuleName = TopImport->ModuleName;
423 assert(!ModuleName.empty() && "diagnostic options read before module name");
424
425 Module *M = PP.getHeaderSearchInfo().lookupModule(ModuleName);
426 assert(M && "missing module");
427
428 // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that
429 // contains the union of their flags.
430 return checkDiagnosticMappings(*Diags, ExistingDiags, M->IsSystem, Complain);
431}
432
Guy Benyei11169dd2012-12-18 14:30:41 +0000433/// \brief Collect the macro definitions provided by the given preprocessor
434/// options.
Craig Toppera13603a2014-05-22 05:54:18 +0000435static void
436collectMacroDefinitions(const PreprocessorOptions &PPOpts,
437 MacroDefinitionsMap &Macros,
438 SmallVectorImpl<StringRef> *MacroNames = nullptr) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000439 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
440 StringRef Macro = PPOpts.Macros[I].first;
441 bool IsUndef = PPOpts.Macros[I].second;
442
443 std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
444 StringRef MacroName = MacroPair.first;
445 StringRef MacroBody = MacroPair.second;
446
447 // For an #undef'd macro, we only care about the name.
448 if (IsUndef) {
449 if (MacroNames && !Macros.count(MacroName))
450 MacroNames->push_back(MacroName);
451
452 Macros[MacroName] = std::make_pair("", true);
453 continue;
454 }
455
456 // For a #define'd macro, figure out the actual definition.
457 if (MacroName.size() == Macro.size())
458 MacroBody = "1";
459 else {
460 // Note: GCC drops anything following an end-of-line character.
461 StringRef::size_type End = MacroBody.find_first_of("\n\r");
462 MacroBody = MacroBody.substr(0, End);
463 }
464
465 if (MacroNames && !Macros.count(MacroName))
466 MacroNames->push_back(MacroName);
467 Macros[MacroName] = std::make_pair(MacroBody, false);
468 }
469}
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +0000470
Guy Benyei11169dd2012-12-18 14:30:41 +0000471/// \brief Check the preprocessor options deserialized from the control block
472/// against the preprocessor options in an existing preprocessor.
473///
474/// \param Diags If non-null, produce diagnostics for any mismatches incurred.
475static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts,
476 const PreprocessorOptions &ExistingPPOpts,
477 DiagnosticsEngine *Diags,
478 FileManager &FileMgr,
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +0000479 std::string &SuggestedPredefines,
480 const LangOptions &LangOpts) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000481 // Check macro definitions.
482 MacroDefinitionsMap ASTFileMacros;
483 collectMacroDefinitions(PPOpts, ASTFileMacros);
484 MacroDefinitionsMap ExistingMacros;
485 SmallVector<StringRef, 4> ExistingMacroNames;
486 collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames);
487
488 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
489 // Dig out the macro definition in the existing preprocessor options.
490 StringRef MacroName = ExistingMacroNames[I];
491 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
492
493 // Check whether we know anything about this macro name or not.
494 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> >::iterator Known
495 = ASTFileMacros.find(MacroName);
496 if (Known == ASTFileMacros.end()) {
497 // FIXME: Check whether this identifier was referenced anywhere in the
498 // AST file. If so, we should reject the AST file. Unfortunately, this
499 // information isn't in the control block. What shall we do about it?
500
501 if (Existing.second) {
502 SuggestedPredefines += "#undef ";
503 SuggestedPredefines += MacroName.str();
504 SuggestedPredefines += '\n';
505 } else {
506 SuggestedPredefines += "#define ";
507 SuggestedPredefines += MacroName.str();
508 SuggestedPredefines += ' ';
509 SuggestedPredefines += Existing.first.str();
510 SuggestedPredefines += '\n';
511 }
512 continue;
513 }
514
515 // If the macro was defined in one but undef'd in the other, we have a
516 // conflict.
517 if (Existing.second != Known->second.second) {
518 if (Diags) {
519 Diags->Report(diag::err_pch_macro_def_undef)
520 << MacroName << Known->second.second;
521 }
522 return true;
523 }
524
525 // If the macro was #undef'd in both, or if the macro bodies are identical,
526 // it's fine.
527 if (Existing.second || Existing.first == Known->second.first)
528 continue;
529
530 // The macro bodies differ; complain.
531 if (Diags) {
532 Diags->Report(diag::err_pch_macro_def_conflict)
533 << MacroName << Known->second.first << Existing.first;
534 }
535 return true;
536 }
537
538 // Check whether we're using predefines.
539 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines) {
540 if (Diags) {
541 Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
542 }
543 return true;
544 }
545
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +0000546 // Detailed record is important since it is used for the module cache hash.
547 if (LangOpts.Modules &&
548 PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord) {
549 if (Diags) {
550 Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord;
551 }
552 return true;
553 }
554
Guy Benyei11169dd2012-12-18 14:30:41 +0000555 // Compute the #include and #include_macros lines we need.
556 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
557 StringRef File = ExistingPPOpts.Includes[I];
558 if (File == ExistingPPOpts.ImplicitPCHInclude)
559 continue;
560
561 if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File)
562 != PPOpts.Includes.end())
563 continue;
564
565 SuggestedPredefines += "#include \"";
Manuel Klimek9af34ae2014-08-12 08:25:57 +0000566 SuggestedPredefines += File;
Guy Benyei11169dd2012-12-18 14:30:41 +0000567 SuggestedPredefines += "\"\n";
568 }
569
570 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
571 StringRef File = ExistingPPOpts.MacroIncludes[I];
572 if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(),
573 File)
574 != PPOpts.MacroIncludes.end())
575 continue;
576
577 SuggestedPredefines += "#__include_macros \"";
Manuel Klimek9af34ae2014-08-12 08:25:57 +0000578 SuggestedPredefines += File;
Guy Benyei11169dd2012-12-18 14:30:41 +0000579 SuggestedPredefines += "\"\n##\n";
580 }
581
582 return false;
583}
584
585bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
586 bool Complain,
587 std::string &SuggestedPredefines) {
588 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
589
590 return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
Craig Toppera13603a2014-05-22 05:54:18 +0000591 Complain? &Reader.Diags : nullptr,
Guy Benyei11169dd2012-12-18 14:30:41 +0000592 PP.getFileManager(),
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +0000593 SuggestedPredefines,
594 PP.getLangOpts());
Guy Benyei11169dd2012-12-18 14:30:41 +0000595}
596
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +0000597/// Check the header search options deserialized from the control block
598/// against the header search options in an existing preprocessor.
599///
600/// \param Diags If non-null, produce diagnostics for any mismatches incurred.
601static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
602 StringRef SpecificModuleCachePath,
603 StringRef ExistingModuleCachePath,
604 DiagnosticsEngine *Diags,
605 const LangOptions &LangOpts) {
606 if (LangOpts.Modules) {
607 if (SpecificModuleCachePath != ExistingModuleCachePath) {
608 if (Diags)
609 Diags->Report(diag::err_pch_modulecache_mismatch)
610 << SpecificModuleCachePath << ExistingModuleCachePath;
611 return true;
612 }
613 }
614
615 return false;
616}
617
618bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
619 StringRef SpecificModuleCachePath,
620 bool Complain) {
621 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
622 PP.getHeaderSearchInfo().getModuleCachePath(),
623 Complain ? &Reader.Diags : nullptr,
624 PP.getLangOpts());
625}
626
Guy Benyei11169dd2012-12-18 14:30:41 +0000627void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
628 PP.setCounterValue(Value);
629}
630
631//===----------------------------------------------------------------------===//
632// AST reader implementation
633//===----------------------------------------------------------------------===//
634
Nico Weber824285e2014-05-08 04:26:47 +0000635void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,
636 bool TakeOwnership) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000637 DeserializationListener = Listener;
Nico Weber824285e2014-05-08 04:26:47 +0000638 OwnsDeserializationListener = TakeOwnership;
Guy Benyei11169dd2012-12-18 14:30:41 +0000639}
640
641
642
643unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
644 return serialization::ComputeHash(Sel);
645}
646
647
648std::pair<unsigned, unsigned>
649ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000650 using namespace llvm::support;
651 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
652 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
Guy Benyei11169dd2012-12-18 14:30:41 +0000653 return std::make_pair(KeyLen, DataLen);
654}
655
656ASTSelectorLookupTrait::internal_key_type
657ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000658 using namespace llvm::support;
Guy Benyei11169dd2012-12-18 14:30:41 +0000659 SelectorTable &SelTable = Reader.getContext().Selectors;
Justin Bogner57ba0b22014-03-28 22:03:24 +0000660 unsigned N = endian::readNext<uint16_t, little, unaligned>(d);
661 IdentifierInfo *FirstII = Reader.getLocalIdentifier(
662 F, endian::readNext<uint32_t, little, unaligned>(d));
Guy Benyei11169dd2012-12-18 14:30:41 +0000663 if (N == 0)
664 return SelTable.getNullarySelector(FirstII);
665 else if (N == 1)
666 return SelTable.getUnarySelector(FirstII);
667
668 SmallVector<IdentifierInfo *, 16> Args;
669 Args.push_back(FirstII);
670 for (unsigned I = 1; I != N; ++I)
Justin Bogner57ba0b22014-03-28 22:03:24 +0000671 Args.push_back(Reader.getLocalIdentifier(
672 F, endian::readNext<uint32_t, little, unaligned>(d)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000673
674 return SelTable.getSelector(N, Args.data());
675}
676
677ASTSelectorLookupTrait::data_type
678ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
679 unsigned DataLen) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000680 using namespace llvm::support;
Guy Benyei11169dd2012-12-18 14:30:41 +0000681
682 data_type Result;
683
Justin Bogner57ba0b22014-03-28 22:03:24 +0000684 Result.ID = Reader.getGlobalSelectorID(
685 F, endian::readNext<uint32_t, little, unaligned>(d));
Nico Weberff4b35e2014-12-27 22:14:15 +0000686 unsigned FullInstanceBits = endian::readNext<uint16_t, little, unaligned>(d);
687 unsigned FullFactoryBits = endian::readNext<uint16_t, little, unaligned>(d);
688 Result.InstanceBits = FullInstanceBits & 0x3;
689 Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1;
690 Result.FactoryBits = FullFactoryBits & 0x3;
691 Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1;
692 unsigned NumInstanceMethods = FullInstanceBits >> 3;
693 unsigned NumFactoryMethods = FullFactoryBits >> 3;
Guy Benyei11169dd2012-12-18 14:30:41 +0000694
695 // Load instance methods
696 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000697 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
698 F, endian::readNext<uint32_t, little, unaligned>(d)))
Guy Benyei11169dd2012-12-18 14:30:41 +0000699 Result.Instance.push_back(Method);
700 }
701
702 // Load factory methods
703 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000704 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
705 F, endian::readNext<uint32_t, little, unaligned>(d)))
Guy Benyei11169dd2012-12-18 14:30:41 +0000706 Result.Factory.push_back(Method);
707 }
708
709 return Result;
710}
711
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000712unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
713 return llvm::HashString(a);
Guy Benyei11169dd2012-12-18 14:30:41 +0000714}
715
716std::pair<unsigned, unsigned>
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000717ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000718 using namespace llvm::support;
719 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
720 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
Guy Benyei11169dd2012-12-18 14:30:41 +0000721 return std::make_pair(KeyLen, DataLen);
722}
723
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000724ASTIdentifierLookupTraitBase::internal_key_type
725ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000726 assert(n >= 2 && d[n-1] == '\0');
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000727 return StringRef((const char*) d, n-1);
Guy Benyei11169dd2012-12-18 14:30:41 +0000728}
729
Douglas Gregordcf25082013-02-11 18:16:18 +0000730/// \brief Whether the given identifier is "interesting".
731static bool isInterestingIdentifier(IdentifierInfo &II) {
732 return II.isPoisoned() ||
733 II.isExtensionToken() ||
734 II.getObjCOrBuiltinID() ||
735 II.hasRevertedTokenIDToIdentifier() ||
736 II.hadMacroDefinition() ||
737 II.getFETokenInfo<void>();
738}
739
Guy Benyei11169dd2012-12-18 14:30:41 +0000740IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
741 const unsigned char* d,
742 unsigned DataLen) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000743 using namespace llvm::support;
744 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
Guy Benyei11169dd2012-12-18 14:30:41 +0000745 bool IsInteresting = RawID & 0x01;
746
747 // Wipe out the "is interesting" bit.
748 RawID = RawID >> 1;
749
750 IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
751 if (!IsInteresting) {
752 // For uninteresting identifiers, just build the IdentifierInfo
753 // and associate it with the persistent ID.
754 IdentifierInfo *II = KnownII;
755 if (!II) {
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000756 II = &Reader.getIdentifierTable().getOwn(k);
Guy Benyei11169dd2012-12-18 14:30:41 +0000757 KnownII = II;
758 }
759 Reader.SetIdentifierInfo(ID, II);
Douglas Gregordcf25082013-02-11 18:16:18 +0000760 if (!II->isFromAST()) {
761 bool WasInteresting = isInterestingIdentifier(*II);
762 II->setIsFromAST();
763 if (WasInteresting)
764 II->setChangedSinceDeserialization();
765 }
766 Reader.markIdentifierUpToDate(II);
Guy Benyei11169dd2012-12-18 14:30:41 +0000767 return II;
768 }
769
Justin Bogner57ba0b22014-03-28 22:03:24 +0000770 unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d);
771 unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d);
Guy Benyei11169dd2012-12-18 14:30:41 +0000772 bool CPlusPlusOperatorKeyword = Bits & 0x01;
773 Bits >>= 1;
774 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
775 Bits >>= 1;
776 bool Poisoned = Bits & 0x01;
777 Bits >>= 1;
778 bool ExtensionToken = Bits & 0x01;
779 Bits >>= 1;
780 bool hadMacroDefinition = Bits & 0x01;
781 Bits >>= 1;
782
783 assert(Bits == 0 && "Extra bits in the identifier?");
784 DataLen -= 8;
785
786 // Build the IdentifierInfo itself and link the identifier ID with
787 // the new IdentifierInfo.
788 IdentifierInfo *II = KnownII;
789 if (!II) {
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000790 II = &Reader.getIdentifierTable().getOwn(StringRef(k));
Guy Benyei11169dd2012-12-18 14:30:41 +0000791 KnownII = II;
792 }
793 Reader.markIdentifierUpToDate(II);
Douglas Gregordcf25082013-02-11 18:16:18 +0000794 if (!II->isFromAST()) {
795 bool WasInteresting = isInterestingIdentifier(*II);
796 II->setIsFromAST();
797 if (WasInteresting)
798 II->setChangedSinceDeserialization();
799 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000800
801 // Set or check the various bits in the IdentifierInfo structure.
802 // Token IDs are read-only.
Argyrios Kyrtzidisddee8c92013-02-27 01:13:51 +0000803 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
Guy Benyei11169dd2012-12-18 14:30:41 +0000804 II->RevertTokenIDToIdentifier();
805 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
806 assert(II->isExtensionToken() == ExtensionToken &&
807 "Incorrect extension token flag");
808 (void)ExtensionToken;
809 if (Poisoned)
810 II->setIsPoisoned(true);
811 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
812 "Incorrect C++ operator keyword flag");
813 (void)CPlusPlusOperatorKeyword;
814
815 // If this identifier is a macro, deserialize the macro
816 // definition.
817 if (hadMacroDefinition) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000818 uint32_t MacroDirectivesOffset =
819 endian::readNext<uint32_t, little, unaligned>(d);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +0000820 DataLen -= 4;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +0000821
Richard Smithd7329392015-04-21 21:46:32 +0000822 Reader.addPendingMacro(II, &F, MacroDirectivesOffset);
Guy Benyei11169dd2012-12-18 14:30:41 +0000823 }
824
825 Reader.SetIdentifierInfo(ID, II);
826
827 // Read all of the declarations visible at global scope with this
828 // name.
829 if (DataLen > 0) {
830 SmallVector<uint32_t, 4> DeclIDs;
831 for (; DataLen > 0; DataLen -= 4)
Justin Bogner57ba0b22014-03-28 22:03:24 +0000832 DeclIDs.push_back(Reader.getGlobalDeclID(
833 F, endian::readNext<uint32_t, little, unaligned>(d)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000834 Reader.SetGloballyVisibleDecls(II, DeclIDs);
835 }
836
837 return II;
838}
839
840unsigned
841ASTDeclContextNameLookupTrait::ComputeHash(const DeclNameKey &Key) const {
842 llvm::FoldingSetNodeID ID;
843 ID.AddInteger(Key.Kind);
844
845 switch (Key.Kind) {
846 case DeclarationName::Identifier:
847 case DeclarationName::CXXLiteralOperatorName:
848 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
849 break;
850 case DeclarationName::ObjCZeroArgSelector:
851 case DeclarationName::ObjCOneArgSelector:
852 case DeclarationName::ObjCMultiArgSelector:
853 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
854 break;
855 case DeclarationName::CXXOperatorName:
856 ID.AddInteger((OverloadedOperatorKind)Key.Data);
857 break;
858 case DeclarationName::CXXConstructorName:
859 case DeclarationName::CXXDestructorName:
860 case DeclarationName::CXXConversionFunctionName:
861 case DeclarationName::CXXUsingDirective:
862 break;
863 }
864
865 return ID.ComputeHash();
866}
867
868ASTDeclContextNameLookupTrait::internal_key_type
869ASTDeclContextNameLookupTrait::GetInternalKey(
870 const external_key_type& Name) const {
871 DeclNameKey Key;
872 Key.Kind = Name.getNameKind();
873 switch (Name.getNameKind()) {
874 case DeclarationName::Identifier:
875 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
876 break;
877 case DeclarationName::ObjCZeroArgSelector:
878 case DeclarationName::ObjCOneArgSelector:
879 case DeclarationName::ObjCMultiArgSelector:
880 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
881 break;
882 case DeclarationName::CXXOperatorName:
883 Key.Data = Name.getCXXOverloadedOperator();
884 break;
885 case DeclarationName::CXXLiteralOperatorName:
886 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
887 break;
888 case DeclarationName::CXXConstructorName:
889 case DeclarationName::CXXDestructorName:
890 case DeclarationName::CXXConversionFunctionName:
891 case DeclarationName::CXXUsingDirective:
892 Key.Data = 0;
893 break;
894 }
895
896 return Key;
897}
898
899std::pair<unsigned, unsigned>
900ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000901 using namespace llvm::support;
902 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
903 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
Guy Benyei11169dd2012-12-18 14:30:41 +0000904 return std::make_pair(KeyLen, DataLen);
905}
906
907ASTDeclContextNameLookupTrait::internal_key_type
908ASTDeclContextNameLookupTrait::ReadKey(const unsigned char* d, unsigned) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000909 using namespace llvm::support;
Guy Benyei11169dd2012-12-18 14:30:41 +0000910
911 DeclNameKey Key;
912 Key.Kind = (DeclarationName::NameKind)*d++;
913 switch (Key.Kind) {
914 case DeclarationName::Identifier:
Justin Bogner57ba0b22014-03-28 22:03:24 +0000915 Key.Data = (uint64_t)Reader.getLocalIdentifier(
916 F, endian::readNext<uint32_t, little, unaligned>(d));
Guy Benyei11169dd2012-12-18 14:30:41 +0000917 break;
918 case DeclarationName::ObjCZeroArgSelector:
919 case DeclarationName::ObjCOneArgSelector:
920 case DeclarationName::ObjCMultiArgSelector:
921 Key.Data =
Justin Bogner57ba0b22014-03-28 22:03:24 +0000922 (uint64_t)Reader.getLocalSelector(
923 F, endian::readNext<uint32_t, little, unaligned>(
924 d)).getAsOpaquePtr();
Guy Benyei11169dd2012-12-18 14:30:41 +0000925 break;
926 case DeclarationName::CXXOperatorName:
927 Key.Data = *d++; // OverloadedOperatorKind
928 break;
929 case DeclarationName::CXXLiteralOperatorName:
Justin Bogner57ba0b22014-03-28 22:03:24 +0000930 Key.Data = (uint64_t)Reader.getLocalIdentifier(
931 F, endian::readNext<uint32_t, little, unaligned>(d));
Guy Benyei11169dd2012-12-18 14:30:41 +0000932 break;
933 case DeclarationName::CXXConstructorName:
934 case DeclarationName::CXXDestructorName:
935 case DeclarationName::CXXConversionFunctionName:
936 case DeclarationName::CXXUsingDirective:
937 Key.Data = 0;
938 break;
939 }
940
941 return Key;
942}
943
944ASTDeclContextNameLookupTrait::data_type
945ASTDeclContextNameLookupTrait::ReadData(internal_key_type,
946 const unsigned char* d,
947 unsigned DataLen) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000948 using namespace llvm::support;
949 unsigned NumDecls = endian::readNext<uint16_t, little, unaligned>(d);
Argyrios Kyrtzidisc57e5032013-01-11 22:29:49 +0000950 LE32DeclID *Start = reinterpret_cast<LE32DeclID *>(
951 const_cast<unsigned char *>(d));
Guy Benyei11169dd2012-12-18 14:30:41 +0000952 return std::make_pair(Start, Start + NumDecls);
953}
954
955bool ASTReader::ReadDeclContextStorage(ModuleFile &M,
Chris Lattner7fb3bef2013-01-20 00:56:42 +0000956 BitstreamCursor &Cursor,
Guy Benyei11169dd2012-12-18 14:30:41 +0000957 const std::pair<uint64_t, uint64_t> &Offsets,
958 DeclContextInfo &Info) {
959 SavedStreamPosition SavedPosition(Cursor);
960 // First the lexical decls.
961 if (Offsets.first != 0) {
962 Cursor.JumpToBit(Offsets.first);
963
964 RecordData Record;
Chris Lattner0e6c9402013-01-20 02:38:54 +0000965 StringRef Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +0000966 unsigned Code = Cursor.ReadCode();
Chris Lattner0e6c9402013-01-20 02:38:54 +0000967 unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +0000968 if (RecCode != DECL_CONTEXT_LEXICAL) {
969 Error("Expected lexical block");
970 return true;
971 }
972
Chris Lattner0e6c9402013-01-20 02:38:54 +0000973 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob.data());
974 Info.NumLexicalDecls = Blob.size() / sizeof(KindDeclIDPair);
Guy Benyei11169dd2012-12-18 14:30:41 +0000975 }
976
977 // Now the lookup table.
978 if (Offsets.second != 0) {
979 Cursor.JumpToBit(Offsets.second);
980
981 RecordData Record;
Chris Lattner0e6c9402013-01-20 02:38:54 +0000982 StringRef Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +0000983 unsigned Code = Cursor.ReadCode();
Chris Lattner0e6c9402013-01-20 02:38:54 +0000984 unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +0000985 if (RecCode != DECL_CONTEXT_VISIBLE) {
986 Error("Expected visible lookup table block");
987 return true;
988 }
Justin Bognerda4e6502014-04-14 16:34:29 +0000989 Info.NameLookupTableData = ASTDeclContextNameLookupTable::Create(
990 (const unsigned char *)Blob.data() + Record[0],
991 (const unsigned char *)Blob.data() + sizeof(uint32_t),
992 (const unsigned char *)Blob.data(),
993 ASTDeclContextNameLookupTrait(*this, M));
Guy Benyei11169dd2012-12-18 14:30:41 +0000994 }
995
996 return false;
997}
998
999void ASTReader::Error(StringRef Msg) {
1000 Error(diag::err_fe_pch_malformed, Msg);
Douglas Gregor940e8052013-05-10 22:15:13 +00001001 if (Context.getLangOpts().Modules && !Diags.isDiagnosticInFlight()) {
1002 Diag(diag::note_module_cache_path)
1003 << PP.getHeaderSearchInfo().getModuleCachePath();
1004 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001005}
1006
1007void ASTReader::Error(unsigned DiagID,
1008 StringRef Arg1, StringRef Arg2) {
1009 if (Diags.isDiagnosticInFlight())
1010 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
1011 else
1012 Diag(DiagID) << Arg1 << Arg2;
1013}
1014
1015//===----------------------------------------------------------------------===//
1016// Source Manager Deserialization
1017//===----------------------------------------------------------------------===//
1018
1019/// \brief Read the line table in the source manager block.
1020/// \returns true if there was an error.
1021bool ASTReader::ParseLineTable(ModuleFile &F,
Richard Smith7ed1bc92014-12-05 22:42:13 +00001022 const RecordData &Record) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001023 unsigned Idx = 0;
1024 LineTableInfo &LineTable = SourceMgr.getLineTable();
1025
1026 // Parse the file names
1027 std::map<int, int> FileIDs;
1028 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
1029 // Extract the file name
Richard Smith7ed1bc92014-12-05 22:42:13 +00001030 auto Filename = ReadPath(F, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00001031 FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
1032 }
1033
1034 // Parse the line entries
1035 std::vector<LineEntry> Entries;
1036 while (Idx < Record.size()) {
1037 int FID = Record[Idx++];
1038 assert(FID >= 0 && "Serialized line entries for non-local file.");
1039 // Remap FileID from 1-based old view.
1040 FID += F.SLocEntryBaseID - 1;
1041
1042 // Extract the line entries
1043 unsigned NumEntries = Record[Idx++];
1044 assert(NumEntries && "Numentries is 00000");
1045 Entries.clear();
1046 Entries.reserve(NumEntries);
1047 for (unsigned I = 0; I != NumEntries; ++I) {
1048 unsigned FileOffset = Record[Idx++];
1049 unsigned LineNo = Record[Idx++];
1050 int FilenameID = FileIDs[Record[Idx++]];
1051 SrcMgr::CharacteristicKind FileKind
1052 = (SrcMgr::CharacteristicKind)Record[Idx++];
1053 unsigned IncludeOffset = Record[Idx++];
1054 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1055 FileKind, IncludeOffset));
1056 }
1057 LineTable.AddEntry(FileID::get(FID), Entries);
1058 }
1059
1060 return false;
1061}
1062
1063/// \brief Read a source manager block
1064bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1065 using namespace SrcMgr;
1066
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001067 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001068
1069 // Set the source-location entry cursor to the current position in
1070 // the stream. This cursor will be used to read the contents of the
1071 // source manager block initially, and then lazily read
1072 // source-location entries as needed.
1073 SLocEntryCursor = F.Stream;
1074
1075 // The stream itself is going to skip over the source manager block.
1076 if (F.Stream.SkipBlock()) {
1077 Error("malformed block record in AST file");
1078 return true;
1079 }
1080
1081 // Enter the source manager block.
1082 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
1083 Error("malformed source manager block record in AST file");
1084 return true;
1085 }
1086
1087 RecordData Record;
1088 while (true) {
Chris Lattnere7b154b2013-01-19 21:39:22 +00001089 llvm::BitstreamEntry E = SLocEntryCursor.advanceSkippingSubblocks();
1090
1091 switch (E.Kind) {
1092 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1093 case llvm::BitstreamEntry::Error:
1094 Error("malformed block record in AST file");
1095 return true;
1096 case llvm::BitstreamEntry::EndBlock:
Guy Benyei11169dd2012-12-18 14:30:41 +00001097 return false;
Chris Lattnere7b154b2013-01-19 21:39:22 +00001098 case llvm::BitstreamEntry::Record:
1099 // The interesting case.
1100 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001101 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00001102
Guy Benyei11169dd2012-12-18 14:30:41 +00001103 // Read a record.
Guy Benyei11169dd2012-12-18 14:30:41 +00001104 Record.clear();
Chris Lattner15c3e7d2013-01-21 18:28:26 +00001105 StringRef Blob;
1106 switch (SLocEntryCursor.readRecord(E.ID, Record, &Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001107 default: // Default behavior: ignore.
1108 break;
1109
1110 case SM_SLOC_FILE_ENTRY:
1111 case SM_SLOC_BUFFER_ENTRY:
1112 case SM_SLOC_EXPANSION_ENTRY:
1113 // Once we hit one of the source location entries, we're done.
1114 return false;
1115 }
1116 }
1117}
1118
1119/// \brief If a header file is not found at the path that we expect it to be
1120/// and the PCH file was moved from its original location, try to resolve the
1121/// file by assuming that header+PCH were moved together and the header is in
1122/// the same place relative to the PCH.
1123static std::string
1124resolveFileRelativeToOriginalDir(const std::string &Filename,
1125 const std::string &OriginalDir,
1126 const std::string &CurrDir) {
1127 assert(OriginalDir != CurrDir &&
1128 "No point trying to resolve the file if the PCH dir didn't change");
1129 using namespace llvm::sys;
1130 SmallString<128> filePath(Filename);
1131 fs::make_absolute(filePath);
1132 assert(path::is_absolute(OriginalDir));
1133 SmallString<128> currPCHPath(CurrDir);
1134
1135 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1136 fileDirE = path::end(path::parent_path(filePath));
1137 path::const_iterator origDirI = path::begin(OriginalDir),
1138 origDirE = path::end(OriginalDir);
1139 // Skip the common path components from filePath and OriginalDir.
1140 while (fileDirI != fileDirE && origDirI != origDirE &&
1141 *fileDirI == *origDirI) {
1142 ++fileDirI;
1143 ++origDirI;
1144 }
1145 for (; origDirI != origDirE; ++origDirI)
1146 path::append(currPCHPath, "..");
1147 path::append(currPCHPath, fileDirI, fileDirE);
1148 path::append(currPCHPath, path::filename(Filename));
1149 return currPCHPath.str();
1150}
1151
1152bool ASTReader::ReadSLocEntry(int ID) {
1153 if (ID == 0)
1154 return false;
1155
1156 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1157 Error("source location entry ID out-of-range for AST file");
1158 return true;
1159 }
1160
1161 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
1162 F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]);
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001163 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001164 unsigned BaseOffset = F->SLocEntryBaseOffset;
1165
1166 ++NumSLocEntriesRead;
Chris Lattnere7b154b2013-01-19 21:39:22 +00001167 llvm::BitstreamEntry Entry = SLocEntryCursor.advance();
1168 if (Entry.Kind != llvm::BitstreamEntry::Record) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001169 Error("incorrectly-formatted source location entry in AST file");
1170 return true;
1171 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00001172
Guy Benyei11169dd2012-12-18 14:30:41 +00001173 RecordData Record;
Chris Lattner0e6c9402013-01-20 02:38:54 +00001174 StringRef Blob;
1175 switch (SLocEntryCursor.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001176 default:
1177 Error("incorrectly-formatted source location entry in AST file");
1178 return true;
1179
1180 case SM_SLOC_FILE_ENTRY: {
1181 // We will detect whether a file changed and return 'Failure' for it, but
1182 // we will also try to fail gracefully by setting up the SLocEntry.
1183 unsigned InputID = Record[4];
1184 InputFile IF = getInputFile(*F, InputID);
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001185 const FileEntry *File = IF.getFile();
1186 bool OverriddenBuffer = IF.isOverridden();
Guy Benyei11169dd2012-12-18 14:30:41 +00001187
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001188 // Note that we only check if a File was returned. If it was out-of-date
1189 // we have complained but we will continue creating a FileID to recover
1190 // gracefully.
1191 if (!File)
Guy Benyei11169dd2012-12-18 14:30:41 +00001192 return true;
1193
1194 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1195 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1196 // This is the module's main file.
1197 IncludeLoc = getImportLocation(F);
1198 }
1199 SrcMgr::CharacteristicKind
1200 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1201 FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
1202 ID, BaseOffset + Record[0]);
1203 SrcMgr::FileInfo &FileInfo =
1204 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1205 FileInfo.NumCreatedFIDs = Record[5];
1206 if (Record[3])
1207 FileInfo.setHasLineDirectives();
1208
1209 const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
1210 unsigned NumFileDecls = Record[7];
1211 if (NumFileDecls) {
1212 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1213 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1214 NumFileDecls));
1215 }
1216
1217 const SrcMgr::ContentCache *ContentCache
1218 = SourceMgr.getOrCreateContentCache(File,
1219 /*isSystemFile=*/FileCharacter != SrcMgr::C_User);
1220 if (OverriddenBuffer && !ContentCache->BufferOverridden &&
1221 ContentCache->ContentsEntry == ContentCache->OrigEntry) {
1222 unsigned Code = SLocEntryCursor.ReadCode();
1223 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00001224 unsigned RecCode = SLocEntryCursor.readRecord(Code, Record, &Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +00001225
1226 if (RecCode != SM_SLOC_BUFFER_BLOB) {
1227 Error("AST record has invalid code");
1228 return true;
1229 }
1230
Rafael Espindolad87f8d72014-08-27 20:03:29 +00001231 std::unique_ptr<llvm::MemoryBuffer> Buffer
Chris Lattner0e6c9402013-01-20 02:38:54 +00001232 = llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), File->getName());
David Blaikie49cc3182014-08-27 20:54:45 +00001233 SourceMgr.overrideFileContents(File, std::move(Buffer));
Guy Benyei11169dd2012-12-18 14:30:41 +00001234 }
1235
1236 break;
1237 }
1238
1239 case SM_SLOC_BUFFER_ENTRY: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00001240 const char *Name = Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00001241 unsigned Offset = Record[0];
1242 SrcMgr::CharacteristicKind
1243 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1244 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
Richard Smithe842a472014-10-22 02:05:46 +00001245 if (IncludeLoc.isInvalid() &&
1246 (F->Kind == MK_ImplicitModule || F->Kind == MK_ExplicitModule)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001247 IncludeLoc = getImportLocation(F);
1248 }
1249 unsigned Code = SLocEntryCursor.ReadCode();
1250 Record.clear();
1251 unsigned RecCode
Chris Lattner0e6c9402013-01-20 02:38:54 +00001252 = SLocEntryCursor.readRecord(Code, Record, &Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +00001253
1254 if (RecCode != SM_SLOC_BUFFER_BLOB) {
1255 Error("AST record has invalid code");
1256 return true;
1257 }
1258
Rafael Espindolad87f8d72014-08-27 20:03:29 +00001259 std::unique_ptr<llvm::MemoryBuffer> Buffer =
1260 llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name);
David Blaikie50a5f972014-08-29 07:59:55 +00001261 SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID,
Rafael Espindolad87f8d72014-08-27 20:03:29 +00001262 BaseOffset + Offset, IncludeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001263 break;
1264 }
1265
1266 case SM_SLOC_EXPANSION_ENTRY: {
1267 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1268 SourceMgr.createExpansionLoc(SpellingLoc,
1269 ReadSourceLocation(*F, Record[2]),
1270 ReadSourceLocation(*F, Record[3]),
1271 Record[4],
1272 ID,
1273 BaseOffset + Record[0]);
1274 break;
1275 }
1276 }
1277
1278 return false;
1279}
1280
1281std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1282 if (ID == 0)
1283 return std::make_pair(SourceLocation(), "");
1284
1285 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1286 Error("source location entry ID out-of-range for AST file");
1287 return std::make_pair(SourceLocation(), "");
1288 }
1289
1290 // Find which module file this entry lands in.
1291 ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
Richard Smithe842a472014-10-22 02:05:46 +00001292 if (M->Kind != MK_ImplicitModule && M->Kind != MK_ExplicitModule)
Guy Benyei11169dd2012-12-18 14:30:41 +00001293 return std::make_pair(SourceLocation(), "");
1294
1295 // FIXME: Can we map this down to a particular submodule? That would be
1296 // ideal.
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001297 return std::make_pair(M->ImportLoc, StringRef(M->ModuleName));
Guy Benyei11169dd2012-12-18 14:30:41 +00001298}
1299
1300/// \brief Find the location where the module F is imported.
1301SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1302 if (F->ImportLoc.isValid())
1303 return F->ImportLoc;
1304
1305 // Otherwise we have a PCH. It's considered to be "imported" at the first
1306 // location of its includer.
1307 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001308 // Main file is the importer.
1309 assert(!SourceMgr.getMainFileID().isInvalid() && "missing main file");
1310 return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
Guy Benyei11169dd2012-12-18 14:30:41 +00001311 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001312 return F->ImportedBy[0]->FirstLoc;
1313}
1314
1315/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1316/// specified cursor. Read the abbreviations that are at the top of the block
1317/// and then leave the cursor pointing into the block.
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001318bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001319 if (Cursor.EnterSubBlock(BlockID)) {
1320 Error("malformed block record in AST file");
1321 return Failure;
1322 }
1323
1324 while (true) {
1325 uint64_t Offset = Cursor.GetCurrentBitNo();
1326 unsigned Code = Cursor.ReadCode();
1327
1328 // We expect all abbrevs to be at the start of the block.
1329 if (Code != llvm::bitc::DEFINE_ABBREV) {
1330 Cursor.JumpToBit(Offset);
1331 return false;
1332 }
1333 Cursor.ReadAbbrevRecord();
1334 }
1335}
1336
Richard Smithe40f2ba2013-08-07 21:41:30 +00001337Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record,
John McCallf413f5e2013-05-03 00:10:13 +00001338 unsigned &Idx) {
1339 Token Tok;
1340 Tok.startToken();
1341 Tok.setLocation(ReadSourceLocation(F, Record, Idx));
1342 Tok.setLength(Record[Idx++]);
1343 if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
1344 Tok.setIdentifierInfo(II);
1345 Tok.setKind((tok::TokenKind)Record[Idx++]);
1346 Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1347 return Tok;
1348}
1349
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001350MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001351 BitstreamCursor &Stream = F.MacroCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001352
1353 // Keep track of where we are in the stream, then jump back there
1354 // after reading this macro.
1355 SavedStreamPosition SavedPosition(Stream);
1356
1357 Stream.JumpToBit(Offset);
1358 RecordData Record;
1359 SmallVector<IdentifierInfo*, 16> MacroArgs;
Craig Toppera13603a2014-05-22 05:54:18 +00001360 MacroInfo *Macro = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001361
Guy Benyei11169dd2012-12-18 14:30:41 +00001362 while (true) {
Chris Lattnerefa77172013-01-20 00:00:22 +00001363 // Advance to the next record, but if we get to the end of the block, don't
1364 // pop it (removing all the abbreviations from the cursor) since we want to
1365 // be able to reseek within the block and read entries.
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001366 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
Chris Lattnerefa77172013-01-20 00:00:22 +00001367 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks(Flags);
1368
1369 switch (Entry.Kind) {
1370 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1371 case llvm::BitstreamEntry::Error:
1372 Error("malformed block record in AST file");
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001373 return Macro;
Chris Lattnerefa77172013-01-20 00:00:22 +00001374 case llvm::BitstreamEntry::EndBlock:
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001375 return Macro;
Chris Lattnerefa77172013-01-20 00:00:22 +00001376 case llvm::BitstreamEntry::Record:
1377 // The interesting case.
1378 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001379 }
1380
1381 // Read a record.
Guy Benyei11169dd2012-12-18 14:30:41 +00001382 Record.clear();
1383 PreprocessorRecordTypes RecType =
Chris Lattner0e6c9402013-01-20 02:38:54 +00001384 (PreprocessorRecordTypes)Stream.readRecord(Entry.ID, Record);
Guy Benyei11169dd2012-12-18 14:30:41 +00001385 switch (RecType) {
Richard Smithd7329392015-04-21 21:46:32 +00001386 case PP_MODULE_MACRO:
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001387 case PP_MACRO_DIRECTIVE_HISTORY:
1388 return Macro;
1389
Guy Benyei11169dd2012-12-18 14:30:41 +00001390 case PP_MACRO_OBJECT_LIKE:
1391 case PP_MACRO_FUNCTION_LIKE: {
1392 // If we already have a macro, that means that we've hit the end
1393 // of the definition of the macro we were looking for. We're
1394 // done.
1395 if (Macro)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001396 return Macro;
Guy Benyei11169dd2012-12-18 14:30:41 +00001397
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001398 unsigned NextIndex = 1; // Skip identifier ID.
1399 SubmoduleID SubModID = getGlobalSubmoduleID(F, Record[NextIndex++]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001400 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001401 MacroInfo *MI = PP.AllocateDeserializedMacroInfo(Loc, SubModID);
Argyrios Kyrtzidis7572be22013-01-07 19:16:23 +00001402 MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
Guy Benyei11169dd2012-12-18 14:30:41 +00001403 MI->setIsUsed(Record[NextIndex++]);
Argyrios Kyrtzidis9ef53ce2014-04-09 18:21:23 +00001404 MI->setUsedForHeaderGuard(Record[NextIndex++]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001405
Guy Benyei11169dd2012-12-18 14:30:41 +00001406 if (RecType == PP_MACRO_FUNCTION_LIKE) {
1407 // Decode function-like macro info.
1408 bool isC99VarArgs = Record[NextIndex++];
1409 bool isGNUVarArgs = Record[NextIndex++];
1410 bool hasCommaPasting = Record[NextIndex++];
1411 MacroArgs.clear();
1412 unsigned NumArgs = Record[NextIndex++];
1413 for (unsigned i = 0; i != NumArgs; ++i)
1414 MacroArgs.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1415
1416 // Install function-like macro info.
1417 MI->setIsFunctionLike();
1418 if (isC99VarArgs) MI->setIsC99Varargs();
1419 if (isGNUVarArgs) MI->setIsGNUVarargs();
1420 if (hasCommaPasting) MI->setHasCommaPasting();
1421 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
1422 PP.getPreprocessorAllocator());
1423 }
1424
Guy Benyei11169dd2012-12-18 14:30:41 +00001425 // Remember that we saw this macro last so that we add the tokens that
1426 // form its body to it.
1427 Macro = MI;
1428
1429 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1430 Record[NextIndex]) {
1431 // We have a macro definition. Register the association
1432 PreprocessedEntityID
1433 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1434 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
Argyrios Kyrtzidis832de9f2013-02-22 18:35:59 +00001435 PreprocessingRecord::PPEntityID
1436 PPID = PPRec.getPPEntityID(GlobalID-1, /*isLoaded=*/true);
1437 MacroDefinition *PPDef =
1438 cast_or_null<MacroDefinition>(PPRec.getPreprocessedEntity(PPID));
1439 if (PPDef)
1440 PPRec.RegisterMacroDefinition(Macro, PPDef);
Guy Benyei11169dd2012-12-18 14:30:41 +00001441 }
1442
1443 ++NumMacrosRead;
1444 break;
1445 }
1446
1447 case PP_TOKEN: {
1448 // If we see a TOKEN before a PP_MACRO_*, then the file is
1449 // erroneous, just pretend we didn't see this.
Craig Toppera13603a2014-05-22 05:54:18 +00001450 if (!Macro) break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001451
John McCallf413f5e2013-05-03 00:10:13 +00001452 unsigned Idx = 0;
1453 Token Tok = ReadToken(F, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00001454 Macro->AddTokenToBody(Tok);
1455 break;
1456 }
1457 }
1458 }
1459}
1460
1461PreprocessedEntityID
1462ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const {
1463 ContinuousRangeMap<uint32_t, int, 2>::const_iterator
1464 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1465 assert(I != M.PreprocessedEntityRemap.end()
1466 && "Invalid index into preprocessed entity index remap");
1467
1468 return LocalID + I->second;
1469}
1470
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001471unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1472 return llvm::hash_combine(ikey.Size, ikey.ModTime);
Guy Benyei11169dd2012-12-18 14:30:41 +00001473}
Richard Smith7ed1bc92014-12-05 22:42:13 +00001474
Guy Benyei11169dd2012-12-18 14:30:41 +00001475HeaderFileInfoTrait::internal_key_type
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001476HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
1477 internal_key_type ikey = { FE->getSize(), FE->getModificationTime(),
Richard Smith7ed1bc92014-12-05 22:42:13 +00001478 FE->getName(), /*Imported*/false };
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001479 return ikey;
1480}
Guy Benyei11169dd2012-12-18 14:30:41 +00001481
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001482bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
1483 if (a.Size != b.Size || a.ModTime != b.ModTime)
Guy Benyei11169dd2012-12-18 14:30:41 +00001484 return false;
1485
Richard Smith7ed1bc92014-12-05 22:42:13 +00001486 if (llvm::sys::path::is_absolute(a.Filename) &&
1487 strcmp(a.Filename, b.Filename) == 0)
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001488 return true;
1489
Guy Benyei11169dd2012-12-18 14:30:41 +00001490 // Determine whether the actual files are equivalent.
Argyrios Kyrtzidis2a513e82013-03-04 20:33:40 +00001491 FileManager &FileMgr = Reader.getFileManager();
Richard Smith7ed1bc92014-12-05 22:42:13 +00001492 auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* {
1493 if (!Key.Imported)
1494 return FileMgr.getFile(Key.Filename);
1495
1496 std::string Resolved = Key.Filename;
1497 Reader.ResolveImportedPath(M, Resolved);
1498 return FileMgr.getFile(Resolved);
1499 };
1500
1501 const FileEntry *FEA = GetFile(a);
1502 const FileEntry *FEB = GetFile(b);
1503 return FEA && FEA == FEB;
Guy Benyei11169dd2012-12-18 14:30:41 +00001504}
1505
1506std::pair<unsigned, unsigned>
1507HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
Justin Bogner57ba0b22014-03-28 22:03:24 +00001508 using namespace llvm::support;
1509 unsigned KeyLen = (unsigned) endian::readNext<uint16_t, little, unaligned>(d);
Guy Benyei11169dd2012-12-18 14:30:41 +00001510 unsigned DataLen = (unsigned) *d++;
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001511 return std::make_pair(KeyLen, DataLen);
Guy Benyei11169dd2012-12-18 14:30:41 +00001512}
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001513
1514HeaderFileInfoTrait::internal_key_type
1515HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
Justin Bogner57ba0b22014-03-28 22:03:24 +00001516 using namespace llvm::support;
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001517 internal_key_type ikey;
Justin Bogner57ba0b22014-03-28 22:03:24 +00001518 ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d));
1519 ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d));
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001520 ikey.Filename = (const char *)d;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001521 ikey.Imported = true;
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001522 return ikey;
1523}
1524
Guy Benyei11169dd2012-12-18 14:30:41 +00001525HeaderFileInfoTrait::data_type
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001526HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
Guy Benyei11169dd2012-12-18 14:30:41 +00001527 unsigned DataLen) {
1528 const unsigned char *End = d + DataLen;
Justin Bogner57ba0b22014-03-28 22:03:24 +00001529 using namespace llvm::support;
Guy Benyei11169dd2012-12-18 14:30:41 +00001530 HeaderFileInfo HFI;
1531 unsigned Flags = *d++;
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001532 HFI.HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>
1533 ((Flags >> 6) & 0x03);
Guy Benyei11169dd2012-12-18 14:30:41 +00001534 HFI.isImport = (Flags >> 5) & 0x01;
1535 HFI.isPragmaOnce = (Flags >> 4) & 0x01;
1536 HFI.DirInfo = (Flags >> 2) & 0x03;
1537 HFI.Resolved = (Flags >> 1) & 0x01;
1538 HFI.IndexHeaderMapHeader = Flags & 0x01;
Justin Bogner57ba0b22014-03-28 22:03:24 +00001539 HFI.NumIncludes = endian::readNext<uint16_t, little, unaligned>(d);
1540 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(
1541 M, endian::readNext<uint32_t, little, unaligned>(d));
1542 if (unsigned FrameworkOffset =
1543 endian::readNext<uint32_t, little, unaligned>(d)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001544 // The framework offset is 1 greater than the actual offset,
1545 // since 0 is used as an indicator for "no framework name".
1546 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1547 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1548 }
1549
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001550 if (d != End) {
Justin Bogner57ba0b22014-03-28 22:03:24 +00001551 uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d);
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001552 if (LocalSMID) {
1553 // This header is part of a module. Associate it with the module to enable
1554 // implicit module import.
1555 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1556 Module *Mod = Reader.getSubmodule(GlobalSMID);
1557 HFI.isModuleHeader = true;
1558 FileManager &FileMgr = Reader.getFileManager();
1559 ModuleMap &ModMap =
1560 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
Richard Smith7ed1bc92014-12-05 22:42:13 +00001561 // FIXME: This information should be propagated through the
1562 // SUBMODULE_HEADER etc records rather than from here.
Richard Smith3c1a41a2014-12-02 00:08:08 +00001563 // FIXME: We don't ever mark excluded headers.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001564 std::string Filename = key.Filename;
1565 if (key.Imported)
1566 Reader.ResolveImportedPath(M, Filename);
1567 Module::Header H = { key.Filename, FileMgr.getFile(Filename) };
Hans Wennborg0101b542014-12-02 02:13:09 +00001568 ModMap.addHeader(Mod, H, HFI.getHeaderRole());
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001569 }
1570 }
1571
Guy Benyei11169dd2012-12-18 14:30:41 +00001572 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1573 (void)End;
1574
1575 // This HeaderFileInfo was externally loaded.
1576 HFI.External = true;
1577 return HFI;
1578}
1579
Richard Smithd7329392015-04-21 21:46:32 +00001580void ASTReader::addPendingMacro(IdentifierInfo *II,
1581 ModuleFile *M,
1582 uint64_t MacroDirectivesOffset) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001583 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1584 PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00001585}
1586
1587void ASTReader::ReadDefinedMacros() {
1588 // Note that we are loading defined macros.
1589 Deserializing Macros(this);
1590
1591 for (ModuleReverseIterator I = ModuleMgr.rbegin(),
1592 E = ModuleMgr.rend(); I != E; ++I) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001593 BitstreamCursor &MacroCursor = (*I)->MacroCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001594
1595 // If there was no preprocessor block, skip this file.
1596 if (!MacroCursor.getBitStreamReader())
1597 continue;
1598
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001599 BitstreamCursor Cursor = MacroCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001600 Cursor.JumpToBit((*I)->MacroStartOffset);
1601
1602 RecordData Record;
1603 while (true) {
Chris Lattnere7b154b2013-01-19 21:39:22 +00001604 llvm::BitstreamEntry E = Cursor.advanceSkippingSubblocks();
1605
1606 switch (E.Kind) {
1607 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1608 case llvm::BitstreamEntry::Error:
1609 Error("malformed block record in AST file");
1610 return;
1611 case llvm::BitstreamEntry::EndBlock:
1612 goto NextCursor;
1613
1614 case llvm::BitstreamEntry::Record:
Chris Lattnere7b154b2013-01-19 21:39:22 +00001615 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00001616 switch (Cursor.readRecord(E.ID, Record)) {
Chris Lattnere7b154b2013-01-19 21:39:22 +00001617 default: // Default behavior: ignore.
1618 break;
1619
1620 case PP_MACRO_OBJECT_LIKE:
1621 case PP_MACRO_FUNCTION_LIKE:
1622 getLocalIdentifier(**I, Record[0]);
1623 break;
1624
1625 case PP_TOKEN:
1626 // Ignore tokens.
1627 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001628 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001629 break;
1630 }
1631 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00001632 NextCursor: ;
Guy Benyei11169dd2012-12-18 14:30:41 +00001633 }
1634}
1635
1636namespace {
1637 /// \brief Visitor class used to look up identifirs in an AST file.
1638 class IdentifierLookupVisitor {
1639 StringRef Name;
1640 unsigned PriorGeneration;
Douglas Gregor00a50f72013-01-25 00:38:33 +00001641 unsigned &NumIdentifierLookups;
1642 unsigned &NumIdentifierLookupHits;
Guy Benyei11169dd2012-12-18 14:30:41 +00001643 IdentifierInfo *Found;
Douglas Gregor00a50f72013-01-25 00:38:33 +00001644
Guy Benyei11169dd2012-12-18 14:30:41 +00001645 public:
Douglas Gregor00a50f72013-01-25 00:38:33 +00001646 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
1647 unsigned &NumIdentifierLookups,
1648 unsigned &NumIdentifierLookupHits)
Douglas Gregor7211ac12013-01-25 23:32:03 +00001649 : Name(Name), PriorGeneration(PriorGeneration),
Douglas Gregor00a50f72013-01-25 00:38:33 +00001650 NumIdentifierLookups(NumIdentifierLookups),
1651 NumIdentifierLookupHits(NumIdentifierLookupHits),
1652 Found()
1653 {
1654 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001655
1656 static bool visit(ModuleFile &M, void *UserData) {
1657 IdentifierLookupVisitor *This
1658 = static_cast<IdentifierLookupVisitor *>(UserData);
1659
1660 // If we've already searched this module file, skip it now.
1661 if (M.Generation <= This->PriorGeneration)
1662 return true;
Douglas Gregore060e572013-01-25 01:03:03 +00001663
Guy Benyei11169dd2012-12-18 14:30:41 +00001664 ASTIdentifierLookupTable *IdTable
1665 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
1666 if (!IdTable)
1667 return false;
1668
1669 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(),
1670 M, This->Found);
Douglas Gregor00a50f72013-01-25 00:38:33 +00001671 ++This->NumIdentifierLookups;
1672 ASTIdentifierLookupTable::iterator Pos = IdTable->find(This->Name,&Trait);
Guy Benyei11169dd2012-12-18 14:30:41 +00001673 if (Pos == IdTable->end())
1674 return false;
1675
1676 // Dereferencing the iterator has the effect of building the
1677 // IdentifierInfo node and populating it with the various
1678 // declarations it needs.
Douglas Gregor00a50f72013-01-25 00:38:33 +00001679 ++This->NumIdentifierLookupHits;
Guy Benyei11169dd2012-12-18 14:30:41 +00001680 This->Found = *Pos;
1681 return true;
1682 }
1683
1684 // \brief Retrieve the identifier info found within the module
1685 // files.
1686 IdentifierInfo *getIdentifierInfo() const { return Found; }
1687 };
1688}
1689
1690void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
1691 // Note that we are loading an identifier.
1692 Deserializing AnIdentifier(this);
1693
1694 unsigned PriorGeneration = 0;
1695 if (getContext().getLangOpts().Modules)
1696 PriorGeneration = IdentifierGeneration[&II];
Douglas Gregore060e572013-01-25 01:03:03 +00001697
1698 // If there is a global index, look there first to determine which modules
1699 // provably do not have any results for this identifier.
Douglas Gregor7211ac12013-01-25 23:32:03 +00001700 GlobalModuleIndex::HitSet Hits;
Craig Toppera13603a2014-05-22 05:54:18 +00001701 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
Douglas Gregore060e572013-01-25 01:03:03 +00001702 if (!loadGlobalIndex()) {
Douglas Gregor7211ac12013-01-25 23:32:03 +00001703 if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
1704 HitsPtr = &Hits;
Douglas Gregore060e572013-01-25 01:03:03 +00001705 }
1706 }
1707
Douglas Gregor7211ac12013-01-25 23:32:03 +00001708 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
Douglas Gregor00a50f72013-01-25 00:38:33 +00001709 NumIdentifierLookups,
1710 NumIdentifierLookupHits);
Douglas Gregor7211ac12013-01-25 23:32:03 +00001711 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor, HitsPtr);
Guy Benyei11169dd2012-12-18 14:30:41 +00001712 markIdentifierUpToDate(&II);
1713}
1714
1715void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
1716 if (!II)
1717 return;
1718
1719 II->setOutOfDate(false);
1720
1721 // Update the generation for this identifier.
1722 if (getContext().getLangOpts().Modules)
Richard Smith053f6c62014-05-16 23:01:30 +00001723 IdentifierGeneration[II] = getGeneration();
Guy Benyei11169dd2012-12-18 14:30:41 +00001724}
1725
Richard Smith49f906a2014-03-01 00:08:04 +00001726struct ASTReader::ModuleMacroInfo {
Richard Smith713369b2015-04-23 20:40:50 +00001727 ModuleMacro *MM;
Richard Smith49f906a2014-03-01 00:08:04 +00001728 // FIXME: Remove this.
1729 ModuleFile *F;
1730
Richard Smith713369b2015-04-23 20:40:50 +00001731 bool isDefine() const { return MM->getMacroInfo(); }
Richard Smith49f906a2014-03-01 00:08:04 +00001732
Richard Smith713369b2015-04-23 20:40:50 +00001733 ArrayRef<ModuleMacro *> getOverriddenMacros() const {
1734 return MM->overrides();
1735 }
Richard Smith49f906a2014-03-01 00:08:04 +00001736
Richard Smithdaa69e02014-07-25 04:40:03 +00001737 MacroDirective *import(Preprocessor &PP, SourceLocation ImportLoc) const {
Richard Smith713369b2015-04-23 20:40:50 +00001738 if (auto *MI = MM->getMacroInfo())
1739 return PP.AllocateDefMacroDirective(MI, ImportLoc, MM);
1740 return PP.AllocateUndefMacroDirective(ImportLoc, MM);
Richard Smith49f906a2014-03-01 00:08:04 +00001741 }
1742};
1743
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001744void ASTReader::resolvePendingMacro(IdentifierInfo *II,
1745 const PendingMacroInfo &PMInfo) {
Richard Smithd7329392015-04-21 21:46:32 +00001746 ModuleFile &M = *PMInfo.M;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001747
1748 BitstreamCursor &Cursor = M.MacroCursor;
1749 SavedStreamPosition SavedPosition(Cursor);
Richard Smithd7329392015-04-21 21:46:32 +00001750 Cursor.JumpToBit(PMInfo.MacroDirectivesOffset);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001751
Richard Smith713369b2015-04-23 20:40:50 +00001752 struct ModuleMacroRecord {
1753 SubmoduleID SubModID;
1754 MacroInfo *MI;
1755 SmallVector<SubmoduleID, 8> Overrides;
1756 };
1757 llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001758
Richard Smithd7329392015-04-21 21:46:32 +00001759 // We expect to see a sequence of PP_MODULE_MACRO records listing exported
1760 // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
1761 // macro histroy.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001762 RecordData Record;
Richard Smithd7329392015-04-21 21:46:32 +00001763 while (true) {
1764 llvm::BitstreamEntry Entry =
1765 Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
1766 if (Entry.Kind != llvm::BitstreamEntry::Record) {
1767 Error("malformed block record in AST file");
1768 return;
1769 }
1770
1771 Record.clear();
Aaron Ballmanc75a1922015-04-22 15:25:05 +00001772 switch ((PreprocessorRecordTypes)Cursor.readRecord(Entry.ID, Record)) {
Richard Smithd7329392015-04-21 21:46:32 +00001773 case PP_MACRO_DIRECTIVE_HISTORY:
1774 break;
1775
1776 case PP_MODULE_MACRO: {
Richard Smith713369b2015-04-23 20:40:50 +00001777 ModuleMacros.push_back(ModuleMacroRecord());
1778 auto &Info = ModuleMacros.back();
Richard Smithe56c8bc2015-04-22 00:26:11 +00001779 Info.SubModID = getGlobalSubmoduleID(M, Record[0]);
1780 Info.MI = getMacro(getGlobalMacroID(M, Record[1]));
Richard Smith713369b2015-04-23 20:40:50 +00001781 for (int I = 2, N = Record.size(); I != N; ++I)
1782 Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I]));
Richard Smithd7329392015-04-21 21:46:32 +00001783 continue;
1784 }
1785
1786 default:
1787 Error("malformed block record in AST file");
1788 return;
1789 }
1790
1791 // We found the macro directive history; that's the last record
1792 // for this macro.
1793 break;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001794 }
1795
Richard Smithd7329392015-04-21 21:46:32 +00001796 // Module macros are listed in reverse dependency order.
Richard Smithe56c8bc2015-04-22 00:26:11 +00001797 {
1798 std::reverse(ModuleMacros.begin(), ModuleMacros.end());
Richard Smithe56c8bc2015-04-22 00:26:11 +00001799 llvm::SmallVector<ModuleMacro*, 8> Overrides;
Richard Smith713369b2015-04-23 20:40:50 +00001800 for (auto &MMR : ModuleMacros) {
Richard Smithe56c8bc2015-04-22 00:26:11 +00001801 Overrides.clear();
Richard Smith713369b2015-04-23 20:40:50 +00001802 for (unsigned ModID : MMR.Overrides) {
Richard Smithb8b2ed62015-04-23 18:18:26 +00001803 Module *Mod = getSubmodule(ModID);
1804 auto *Macro = PP.getModuleMacro(Mod, II);
Richard Smithe56c8bc2015-04-22 00:26:11 +00001805 assert(Macro && "missing definition for overridden macro");
Richard Smith5dbef922015-04-22 02:09:43 +00001806 Overrides.push_back(Macro);
Richard Smithe56c8bc2015-04-22 00:26:11 +00001807 }
1808
1809 bool Inserted = false;
Richard Smith713369b2015-04-23 20:40:50 +00001810 Module *Owner = getSubmodule(MMR.SubModID);
1811 auto *MM = PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted);
Richard Smithe56c8bc2015-04-22 00:26:11 +00001812 if (!Inserted)
1813 continue;
1814
Richard Smith713369b2015-04-23 20:40:50 +00001815 ModuleMacroInfo MMI = { MM, &M };
Richard Smithe56c8bc2015-04-22 00:26:11 +00001816 if (Owner->NameVisibility == Module::Hidden) {
1817 // Macros in the owning module are hidden. Just remember this macro to
1818 // install if we make this module visible.
1819 HiddenNamesMap[Owner].HiddenMacros.insert(
Richard Smith5dbef922015-04-22 02:09:43 +00001820 std::make_pair(II, new (Context) ModuleMacroInfo(MMI)));
Richard Smithe56c8bc2015-04-22 00:26:11 +00001821 } else {
1822 installImportedMacro(II, MMI, Owner);
1823 }
Richard Smithd7329392015-04-21 21:46:32 +00001824 }
1825 }
1826
1827 // Don't read the directive history for a module; we don't have anywhere
1828 // to put it.
1829 if (M.Kind == MK_ImplicitModule || M.Kind == MK_ExplicitModule)
1830 return;
1831
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001832 // Deserialize the macro directives history in reverse source-order.
Craig Toppera13603a2014-05-22 05:54:18 +00001833 MacroDirective *Latest = nullptr, *Earliest = nullptr;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001834 unsigned Idx = 0, N = Record.size();
1835 while (Idx < N) {
Craig Toppera13603a2014-05-22 05:54:18 +00001836 MacroDirective *MD = nullptr;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001837 SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001838 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
1839 switch (K) {
1840 case MacroDirective::MD_Define: {
Richard Smith713369b2015-04-23 20:40:50 +00001841 MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++]));
Richard Smithdaa69e02014-07-25 04:40:03 +00001842 bool IsAmbiguous = Record[Idx++];
Richard Smith713369b2015-04-23 20:40:50 +00001843 ModuleMacro *MM = nullptr;
1844 if (SubmoduleID ImportedFrom = getGlobalSubmoduleID(M, Record[Idx++]))
1845 MM = PP.getModuleMacro(getSubmodule(ImportedFrom), II);
1846 DefMacroDirective *DefMD = PP.AllocateDefMacroDirective(MI, Loc, MM);
Richard Smithdaa69e02014-07-25 04:40:03 +00001847 DefMD->setAmbiguous(IsAmbiguous);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001848 MD = DefMD;
1849 break;
1850 }
Richard Smithdaa69e02014-07-25 04:40:03 +00001851 case MacroDirective::MD_Undefine: {
Richard Smith713369b2015-04-23 20:40:50 +00001852 ModuleMacro *MM = nullptr;
1853 if (SubmoduleID ImportedFrom = getGlobalSubmoduleID(M, Record[Idx++]))
1854 MM = PP.getModuleMacro(getSubmodule(ImportedFrom), II);
1855 MD = PP.AllocateUndefMacroDirective(Loc, MM);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001856 break;
Richard Smithdaa69e02014-07-25 04:40:03 +00001857 }
1858 case MacroDirective::MD_Visibility:
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001859 bool isPublic = Record[Idx++];
1860 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
1861 break;
1862 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001863
1864 if (!Latest)
1865 Latest = MD;
1866 if (Earliest)
1867 Earliest->setPrevious(MD);
1868 Earliest = MD;
1869 }
1870
1871 PP.setLoadedMacroDirective(II, Latest);
1872}
1873
Argyrios Kyrtzidis3a9c42c2013-03-27 01:25:34 +00001874/// \brief For the given macro definitions, check if they are both in system
Douglas Gregor0b202052013-04-12 21:00:54 +00001875/// modules.
1876static bool areDefinedInSystemModules(MacroInfo *PrevMI, MacroInfo *NewMI,
Douglas Gregor5e461192013-06-07 22:56:11 +00001877 Module *NewOwner, ASTReader &Reader) {
Argyrios Kyrtzidis3a9c42c2013-03-27 01:25:34 +00001878 assert(PrevMI && NewMI);
Craig Toppera13603a2014-05-22 05:54:18 +00001879 Module *PrevOwner = nullptr;
Argyrios Kyrtzidis3a9c42c2013-03-27 01:25:34 +00001880 if (SubmoduleID PrevModID = PrevMI->getOwningModuleID())
1881 PrevOwner = Reader.getSubmodule(PrevModID);
Douglas Gregor5e461192013-06-07 22:56:11 +00001882 if (PrevOwner && PrevOwner == NewOwner)
Argyrios Kyrtzidis3a9c42c2013-03-27 01:25:34 +00001883 return false;
Chandler Carruthc2132d82015-03-13 08:29:54 +00001884 SourceManager &SrcMgr = Reader.getSourceManager();
1885 bool PrevInSystem = (PrevOwner && PrevOwner->IsSystem) ||
1886 SrcMgr.isInSystemHeader(PrevMI->getDefinitionLoc());
1887 bool NewInSystem = (NewOwner && NewOwner->IsSystem) ||
1888 SrcMgr.isInSystemHeader(NewMI->getDefinitionLoc());
Douglas Gregor5e461192013-06-07 22:56:11 +00001889 return PrevInSystem && NewInSystem;
Argyrios Kyrtzidis3a9c42c2013-03-27 01:25:34 +00001890}
1891
Richard Smith49f906a2014-03-01 00:08:04 +00001892void ASTReader::removeOverriddenMacros(IdentifierInfo *II,
Richard Smithdaa69e02014-07-25 04:40:03 +00001893 SourceLocation ImportLoc,
Richard Smith49f906a2014-03-01 00:08:04 +00001894 AmbiguousMacros &Ambig,
Richard Smith713369b2015-04-23 20:40:50 +00001895 ArrayRef<ModuleMacro *> Overrides) {
1896 for (ModuleMacro *Overridden : Overrides) {
1897 Module *Owner = Overridden->getOwningModule();
Richard Smith49f906a2014-03-01 00:08:04 +00001898 // If this macro is not yet visible, remove it from the hidden names list.
Richard Smithbb853c72014-08-13 01:23:33 +00001899 // It won't be there if we're in the middle of making the owner visible.
Richard Smithbb853c72014-08-13 01:23:33 +00001900 auto HiddenIt = HiddenNamesMap.find(Owner);
1901 if (HiddenIt != HiddenNamesMap.end()) {
1902 HiddenNames &Hidden = HiddenIt->second;
1903 HiddenMacrosMap::iterator HI = Hidden.HiddenMacros.find(II);
1904 if (HI != Hidden.HiddenMacros.end()) {
1905 // Register the macro now so we don't lose it when we re-export.
1906 PP.appendMacroDirective(II, HI->second->import(PP, ImportLoc));
Richard Smithdaa69e02014-07-25 04:40:03 +00001907
Richard Smith713369b2015-04-23 20:40:50 +00001908 auto SubOverrides = HI->second->getOverriddenMacros();
Richard Smithbb853c72014-08-13 01:23:33 +00001909 Hidden.HiddenMacros.erase(HI);
1910 removeOverriddenMacros(II, ImportLoc, Ambig, SubOverrides);
1911 }
Richard Smith49f906a2014-03-01 00:08:04 +00001912 }
1913
1914 // If this macro is already in our list of conflicts, remove it from there.
Richard Smithbb29e512014-03-06 00:33:23 +00001915 Ambig.erase(
1916 std::remove_if(Ambig.begin(), Ambig.end(), [&](DefMacroDirective *MD) {
Richard Smith713369b2015-04-23 20:40:50 +00001917 return getSubmodule(MD->getInfo()->getOwningModuleID()) == Owner;
Richard Smithbb29e512014-03-06 00:33:23 +00001918 }),
1919 Ambig.end());
Richard Smith49f906a2014-03-01 00:08:04 +00001920 }
1921}
1922
1923ASTReader::AmbiguousMacros *
1924ASTReader::removeOverriddenMacros(IdentifierInfo *II,
Richard Smithdaa69e02014-07-25 04:40:03 +00001925 SourceLocation ImportLoc,
Richard Smith713369b2015-04-23 20:40:50 +00001926 ArrayRef<ModuleMacro *> Overrides) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001927 MacroDirective *Prev = PP.getMacroDirective(II);
Richard Smith49f906a2014-03-01 00:08:04 +00001928 if (!Prev && Overrides.empty())
Craig Toppera13603a2014-05-22 05:54:18 +00001929 return nullptr;
Richard Smith49f906a2014-03-01 00:08:04 +00001930
Craig Toppera13603a2014-05-22 05:54:18 +00001931 DefMacroDirective *PrevDef = Prev ? Prev->getDefinition().getDirective()
1932 : nullptr;
Richard Smith49f906a2014-03-01 00:08:04 +00001933 if (PrevDef && PrevDef->isAmbiguous()) {
1934 // We had a prior ambiguity. Check whether we resolve it (or make it worse).
1935 AmbiguousMacros &Ambig = AmbiguousMacroDefs[II];
1936 Ambig.push_back(PrevDef);
1937
Richard Smithdaa69e02014-07-25 04:40:03 +00001938 removeOverriddenMacros(II, ImportLoc, Ambig, Overrides);
Richard Smith49f906a2014-03-01 00:08:04 +00001939
1940 if (!Ambig.empty())
1941 return &Ambig;
1942
1943 AmbiguousMacroDefs.erase(II);
1944 } else {
1945 // There's no ambiguity yet. Maybe we're introducing one.
Benjamin Kramer834652a2014-05-03 18:44:26 +00001946 AmbiguousMacros Ambig;
Richard Smith49f906a2014-03-01 00:08:04 +00001947 if (PrevDef)
1948 Ambig.push_back(PrevDef);
1949
Richard Smithdaa69e02014-07-25 04:40:03 +00001950 removeOverriddenMacros(II, ImportLoc, Ambig, Overrides);
Richard Smith49f906a2014-03-01 00:08:04 +00001951
1952 if (!Ambig.empty()) {
1953 AmbiguousMacros &Result = AmbiguousMacroDefs[II];
Benjamin Kramer834652a2014-05-03 18:44:26 +00001954 std::swap(Result, Ambig);
Richard Smith49f906a2014-03-01 00:08:04 +00001955 return &Result;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001956 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001957 }
Richard Smith49f906a2014-03-01 00:08:04 +00001958
1959 // We ended up with no ambiguity.
Craig Toppera13603a2014-05-22 05:54:18 +00001960 return nullptr;
Richard Smith49f906a2014-03-01 00:08:04 +00001961}
1962
Richard Smithe56c8bc2015-04-22 00:26:11 +00001963void ASTReader::installImportedMacro(IdentifierInfo *II, ModuleMacroInfo &MMI,
Richard Smithdaa69e02014-07-25 04:40:03 +00001964 Module *Owner) {
Richard Smith49f906a2014-03-01 00:08:04 +00001965 assert(II && Owner);
1966
1967 SourceLocation ImportLoc = Owner->MacroVisibilityLoc;
Richard Smithdaa69e02014-07-25 04:40:03 +00001968 if (ImportLoc.isInvalid()) {
Richard Smith49f906a2014-03-01 00:08:04 +00001969 // FIXME: If we made macros from this module visible but didn't provide a
1970 // source location for the import, we don't have a location for the macro.
1971 // Use the location at which the containing module file was first imported
1972 // for now.
Richard Smithe56c8bc2015-04-22 00:26:11 +00001973 ImportLoc = MMI.F->DirectImportLoc;
Richard Smith56be7542014-03-21 00:33:59 +00001974 assert(ImportLoc.isValid() && "no import location for a visible macro?");
Richard Smith49f906a2014-03-01 00:08:04 +00001975 }
1976
Benjamin Kramer834652a2014-05-03 18:44:26 +00001977 AmbiguousMacros *Prev =
Richard Smith713369b2015-04-23 20:40:50 +00001978 removeOverriddenMacros(II, ImportLoc, MMI.getOverriddenMacros());
Richard Smith49f906a2014-03-01 00:08:04 +00001979
Richard Smith49f906a2014-03-01 00:08:04 +00001980 // Create a synthetic macro definition corresponding to the import (or null
1981 // if this was an undefinition of the macro).
Richard Smithe56c8bc2015-04-22 00:26:11 +00001982 MacroDirective *Imported = MMI.import(PP, ImportLoc);
Richard Smithdaa69e02014-07-25 04:40:03 +00001983 DefMacroDirective *MD = dyn_cast<DefMacroDirective>(Imported);
Richard Smith49f906a2014-03-01 00:08:04 +00001984
1985 // If there's no ambiguity, just install the macro.
1986 if (!Prev) {
Richard Smithdaa69e02014-07-25 04:40:03 +00001987 PP.appendMacroDirective(II, Imported);
Richard Smith49f906a2014-03-01 00:08:04 +00001988 return;
1989 }
1990 assert(!Prev->empty());
1991
1992 if (!MD) {
1993 // We imported a #undef that didn't remove all prior definitions. The most
1994 // recent prior definition remains, and we install it in the place of the
Richard Smithdaa69e02014-07-25 04:40:03 +00001995 // imported directive, as if by a local #pragma pop_macro.
Richard Smith49f906a2014-03-01 00:08:04 +00001996 MacroInfo *NewMI = Prev->back()->getInfo();
1997 Prev->pop_back();
Richard Smithdaa69e02014-07-25 04:40:03 +00001998 MD = PP.AllocateDefMacroDirective(NewMI, ImportLoc);
1999
2000 // Install our #undef first so that we don't lose track of it. We'll replace
2001 // this with whichever macro definition ends up winning.
2002 PP.appendMacroDirective(II, Imported);
Richard Smith49f906a2014-03-01 00:08:04 +00002003 }
2004
2005 // We're introducing a macro definition that creates or adds to an ambiguity.
2006 // We can resolve that ambiguity if this macro is token-for-token identical to
2007 // all of the existing definitions.
2008 MacroInfo *NewMI = MD->getInfo();
2009 assert(NewMI && "macro definition with no MacroInfo?");
2010 while (!Prev->empty()) {
2011 MacroInfo *PrevMI = Prev->back()->getInfo();
2012 assert(PrevMI && "macro definition with no MacroInfo?");
2013
2014 // Before marking the macros as ambiguous, check if this is a case where
2015 // both macros are in system headers. If so, we trust that the system
2016 // did not get it wrong. This also handles cases where Clang's own
2017 // headers have a different spelling of certain system macros:
2018 // #define LONG_MAX __LONG_MAX__ (clang's limits.h)
2019 // #define LONG_MAX 0x7fffffffffffffffL (system's limits.h)
2020 //
2021 // FIXME: Remove the defined-in-system-headers check. clang's limits.h
2022 // overrides the system limits.h's macros, so there's no conflict here.
2023 if (NewMI != PrevMI &&
2024 !PrevMI->isIdenticalTo(*NewMI, PP, /*Syntactically=*/true) &&
2025 !areDefinedInSystemModules(PrevMI, NewMI, Owner, *this))
2026 break;
2027
2028 // The previous definition is the same as this one (or both are defined in
2029 // system modules so we can assume they're equivalent); we don't need to
2030 // track it any more.
2031 Prev->pop_back();
2032 }
2033
2034 if (!Prev->empty())
2035 MD->setAmbiguous(true);
2036
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002037 PP.appendMacroDirective(II, MD);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002038}
2039
Argyrios Kyrtzidisce9b49e2014-03-14 02:26:27 +00002040ASTReader::InputFileInfo
2041ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) {
Ben Langmuir198c1682014-03-07 07:27:49 +00002042 // Go find this input file.
2043 BitstreamCursor &Cursor = F.InputFilesCursor;
2044 SavedStreamPosition SavedPosition(Cursor);
2045 Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
2046
2047 unsigned Code = Cursor.ReadCode();
2048 RecordData Record;
2049 StringRef Blob;
2050
2051 unsigned Result = Cursor.readRecord(Code, Record, &Blob);
2052 assert(static_cast<InputFileRecordTypes>(Result) == INPUT_FILE &&
2053 "invalid record type for input file");
2054 (void)Result;
2055
Argyrios Kyrtzidisce9b49e2014-03-14 02:26:27 +00002056 std::string Filename;
2057 off_t StoredSize;
2058 time_t StoredTime;
2059 bool Overridden;
Richard Smith7ed1bc92014-12-05 22:42:13 +00002060
Ben Langmuir198c1682014-03-07 07:27:49 +00002061 assert(Record[0] == ID && "Bogus stored ID or offset");
2062 StoredSize = static_cast<off_t>(Record[1]);
2063 StoredTime = static_cast<time_t>(Record[2]);
2064 Overridden = static_cast<bool>(Record[3]);
2065 Filename = Blob;
Richard Smith7ed1bc92014-12-05 22:42:13 +00002066 ResolveImportedPath(F, Filename);
2067
Hans Wennborg73945142014-03-14 17:45:06 +00002068 InputFileInfo R = { std::move(Filename), StoredSize, StoredTime, Overridden };
2069 return R;
Ben Langmuir198c1682014-03-07 07:27:49 +00002070}
2071
2072std::string ASTReader::getInputFileName(ModuleFile &F, unsigned int ID) {
Argyrios Kyrtzidisce9b49e2014-03-14 02:26:27 +00002073 return readInputFileInfo(F, ID).Filename;
Ben Langmuir198c1682014-03-07 07:27:49 +00002074}
2075
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00002076InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002077 // If this ID is bogus, just return an empty input file.
2078 if (ID == 0 || ID > F.InputFilesLoaded.size())
2079 return InputFile();
2080
2081 // If we've already loaded this input file, return it.
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00002082 if (F.InputFilesLoaded[ID-1].getFile())
Guy Benyei11169dd2012-12-18 14:30:41 +00002083 return F.InputFilesLoaded[ID-1];
2084
Argyrios Kyrtzidis9308f0a2014-01-08 19:13:34 +00002085 if (F.InputFilesLoaded[ID-1].isNotFound())
2086 return InputFile();
2087
Guy Benyei11169dd2012-12-18 14:30:41 +00002088 // Go find this input file.
Chris Lattner7fb3bef2013-01-20 00:56:42 +00002089 BitstreamCursor &Cursor = F.InputFilesCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00002090 SavedStreamPosition SavedPosition(Cursor);
2091 Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
2092
Argyrios Kyrtzidisce9b49e2014-03-14 02:26:27 +00002093 InputFileInfo FI = readInputFileInfo(F, ID);
2094 off_t StoredSize = FI.StoredSize;
2095 time_t StoredTime = FI.StoredTime;
2096 bool Overridden = FI.Overridden;
2097 StringRef Filename = FI.Filename;
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00002098
Ben Langmuir198c1682014-03-07 07:27:49 +00002099 const FileEntry *File
2100 = Overridden? FileMgr.getVirtualFile(Filename, StoredSize, StoredTime)
2101 : FileMgr.getFile(Filename, /*OpenFile=*/false);
2102
2103 // If we didn't find the file, resolve it relative to the
2104 // original directory from which this AST file was created.
Craig Toppera13603a2014-05-22 05:54:18 +00002105 if (File == nullptr && !F.OriginalDir.empty() && !CurrentDir.empty() &&
Ben Langmuir198c1682014-03-07 07:27:49 +00002106 F.OriginalDir != CurrentDir) {
2107 std::string Resolved = resolveFileRelativeToOriginalDir(Filename,
2108 F.OriginalDir,
2109 CurrentDir);
2110 if (!Resolved.empty())
2111 File = FileMgr.getFile(Resolved);
2112 }
2113
2114 // For an overridden file, create a virtual file with the stored
2115 // size/timestamp.
Craig Toppera13603a2014-05-22 05:54:18 +00002116 if (Overridden && File == nullptr) {
Ben Langmuir198c1682014-03-07 07:27:49 +00002117 File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
2118 }
2119
Craig Toppera13603a2014-05-22 05:54:18 +00002120 if (File == nullptr) {
Ben Langmuir198c1682014-03-07 07:27:49 +00002121 if (Complain) {
2122 std::string ErrorStr = "could not find file '";
2123 ErrorStr += Filename;
2124 ErrorStr += "' referenced by AST file";
2125 Error(ErrorStr.c_str());
Guy Benyei11169dd2012-12-18 14:30:41 +00002126 }
Ben Langmuir198c1682014-03-07 07:27:49 +00002127 // Record that we didn't find the file.
2128 F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
2129 return InputFile();
2130 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002131
Ben Langmuir198c1682014-03-07 07:27:49 +00002132 // Check if there was a request to override the contents of the file
2133 // that was part of the precompiled header. Overridding such a file
2134 // can lead to problems when lexing using the source locations from the
2135 // PCH.
2136 SourceManager &SM = getSourceManager();
2137 if (!Overridden && SM.isFileOverridden(File)) {
2138 if (Complain)
2139 Error(diag::err_fe_pch_file_overridden, Filename);
2140 // After emitting the diagnostic, recover by disabling the override so
2141 // that the original file will be used.
2142 SM.disableFileContentsOverride(File);
2143 // The FileEntry is a virtual file entry with the size of the contents
2144 // that would override the original contents. Set it to the original's
2145 // size/time.
2146 FileMgr.modifyFileEntry(const_cast<FileEntry*>(File),
2147 StoredSize, StoredTime);
2148 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002149
Ben Langmuir198c1682014-03-07 07:27:49 +00002150 bool IsOutOfDate = false;
2151
2152 // For an overridden file, there is nothing to validate.
Richard Smith96fdab62014-10-28 16:24:08 +00002153 if (!Overridden && //
2154 (StoredSize != File->getSize() ||
2155#if defined(LLVM_ON_WIN32)
2156 false
2157#else
Ben Langmuir198c1682014-03-07 07:27:49 +00002158 // In our regression testing, the Windows file system seems to
2159 // have inconsistent modification times that sometimes
2160 // erroneously trigger this error-handling path.
Richard Smith96fdab62014-10-28 16:24:08 +00002161 //
2162 // This also happens in networked file systems, so disable this
2163 // check if validation is disabled or if we have an explicitly
2164 // built PCM file.
2165 //
2166 // FIXME: Should we also do this for PCH files? They could also
2167 // reasonably get shared across a network during a distributed build.
2168 (StoredTime != File->getModificationTime() && !DisableValidation &&
2169 F.Kind != MK_ExplicitModule)
Guy Benyei11169dd2012-12-18 14:30:41 +00002170#endif
Ben Langmuir198c1682014-03-07 07:27:49 +00002171 )) {
2172 if (Complain) {
2173 // Build a list of the PCH imports that got us here (in reverse).
2174 SmallVector<ModuleFile *, 4> ImportStack(1, &F);
2175 while (ImportStack.back()->ImportedBy.size() > 0)
2176 ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
Ben Langmuire82630d2014-01-17 00:19:09 +00002177
Ben Langmuir198c1682014-03-07 07:27:49 +00002178 // The top-level PCH is stale.
2179 StringRef TopLevelPCHName(ImportStack.back()->FileName);
2180 Error(diag::err_fe_pch_file_modified, Filename, TopLevelPCHName);
Ben Langmuire82630d2014-01-17 00:19:09 +00002181
Ben Langmuir198c1682014-03-07 07:27:49 +00002182 // Print the import stack.
2183 if (ImportStack.size() > 1 && !Diags.isDiagnosticInFlight()) {
2184 Diag(diag::note_pch_required_by)
2185 << Filename << ImportStack[0]->FileName;
2186 for (unsigned I = 1; I < ImportStack.size(); ++I)
Ben Langmuire82630d2014-01-17 00:19:09 +00002187 Diag(diag::note_pch_required_by)
Ben Langmuir198c1682014-03-07 07:27:49 +00002188 << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
Douglas Gregor7029ce12013-03-19 00:28:20 +00002189 }
2190
Ben Langmuir198c1682014-03-07 07:27:49 +00002191 if (!Diags.isDiagnosticInFlight())
2192 Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
Guy Benyei11169dd2012-12-18 14:30:41 +00002193 }
2194
Ben Langmuir198c1682014-03-07 07:27:49 +00002195 IsOutOfDate = true;
Guy Benyei11169dd2012-12-18 14:30:41 +00002196 }
2197
Ben Langmuir198c1682014-03-07 07:27:49 +00002198 InputFile IF = InputFile(File, Overridden, IsOutOfDate);
2199
2200 // Note that we've loaded this input file.
2201 F.InputFilesLoaded[ID-1] = IF;
2202 return IF;
Guy Benyei11169dd2012-12-18 14:30:41 +00002203}
2204
Richard Smith7ed1bc92014-12-05 22:42:13 +00002205/// \brief If we are loading a relocatable PCH or module file, and the filename
2206/// is not an absolute path, add the system or module root to the beginning of
2207/// the file name.
2208void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) {
2209 // Resolve relative to the base directory, if we have one.
2210 if (!M.BaseDirectory.empty())
2211 return ResolveImportedPath(Filename, M.BaseDirectory);
Guy Benyei11169dd2012-12-18 14:30:41 +00002212}
2213
Richard Smith7ed1bc92014-12-05 22:42:13 +00002214void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002215 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
2216 return;
2217
Richard Smith7ed1bc92014-12-05 22:42:13 +00002218 SmallString<128> Buffer;
2219 llvm::sys::path::append(Buffer, Prefix, Filename);
2220 Filename.assign(Buffer.begin(), Buffer.end());
Guy Benyei11169dd2012-12-18 14:30:41 +00002221}
2222
2223ASTReader::ASTReadResult
2224ASTReader::ReadControlBlock(ModuleFile &F,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002225 SmallVectorImpl<ImportedModule> &Loaded,
Ben Langmuirbeee15e2014-04-14 18:00:01 +00002226 const ModuleFile *ImportedBy,
Guy Benyei11169dd2012-12-18 14:30:41 +00002227 unsigned ClientLoadCapabilities) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00002228 BitstreamCursor &Stream = F.Stream;
Guy Benyei11169dd2012-12-18 14:30:41 +00002229
2230 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
2231 Error("malformed block record in AST file");
2232 return Failure;
2233 }
2234
Richard Smith1e2cf0d2014-10-31 02:28:58 +00002235 // Should we allow the configuration of the module file to differ from the
2236 // configuration of the current translation unit in a compatible way?
2237 //
2238 // FIXME: Allow this for files explicitly specified with -include-pch too.
2239 bool AllowCompatibleConfigurationMismatch = F.Kind == MK_ExplicitModule;
2240
Guy Benyei11169dd2012-12-18 14:30:41 +00002241 // Read all of the records and blocks in the control block.
2242 RecordData Record;
Richard Smitha1825302014-10-23 22:18:29 +00002243 unsigned NumInputs = 0;
2244 unsigned NumUserInputs = 0;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002245 while (1) {
2246 llvm::BitstreamEntry Entry = Stream.advance();
2247
2248 switch (Entry.Kind) {
2249 case llvm::BitstreamEntry::Error:
2250 Error("malformed block record in AST file");
2251 return Failure;
Dmitri Gribenkof430da42014-02-12 10:33:14 +00002252 case llvm::BitstreamEntry::EndBlock: {
2253 // Validate input files.
2254 const HeaderSearchOptions &HSOpts =
2255 PP.getHeaderSearchInfo().getHeaderSearchOpts();
Ben Langmuircb69b572014-03-07 06:40:32 +00002256
Richard Smitha1825302014-10-23 22:18:29 +00002257 // All user input files reside at the index range [0, NumUserInputs), and
2258 // system input files reside at [NumUserInputs, NumInputs).
Ben Langmuiracb803e2014-11-10 22:13:10 +00002259 if (!DisableValidation) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002260 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
Ben Langmuircb69b572014-03-07 06:40:32 +00002261
Dmitri Gribenkof430da42014-02-12 10:33:14 +00002262 // If we are reading a module, we will create a verification timestamp,
2263 // so we verify all input files. Otherwise, verify only user input
2264 // files.
Ben Langmuircb69b572014-03-07 06:40:32 +00002265
2266 unsigned N = NumUserInputs;
2267 if (ValidateSystemInputs ||
Richard Smithe842a472014-10-22 02:05:46 +00002268 (HSOpts.ModulesValidateOncePerBuildSession &&
Ben Langmuiracb803e2014-11-10 22:13:10 +00002269 F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp &&
Richard Smithe842a472014-10-22 02:05:46 +00002270 F.Kind == MK_ImplicitModule))
Ben Langmuircb69b572014-03-07 06:40:32 +00002271 N = NumInputs;
2272
Ben Langmuir3d4417c2014-02-07 17:31:11 +00002273 for (unsigned I = 0; I < N; ++I) {
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00002274 InputFile IF = getInputFile(F, I+1, Complain);
2275 if (!IF.getFile() || IF.isOutOfDate())
Guy Benyei11169dd2012-12-18 14:30:41 +00002276 return OutOfDate;
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00002277 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002278 }
Ben Langmuircb69b572014-03-07 06:40:32 +00002279
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +00002280 if (Listener)
2281 Listener->visitModuleFile(F.FileName);
2282
Ben Langmuircb69b572014-03-07 06:40:32 +00002283 if (Listener && Listener->needsInputFileVisitation()) {
2284 unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
2285 : NumUserInputs;
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +00002286 for (unsigned I = 0; I < N; ++I) {
2287 bool IsSystem = I >= NumUserInputs;
2288 InputFileInfo FI = readInputFileInfo(F, I+1);
2289 Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden);
2290 }
Ben Langmuircb69b572014-03-07 06:40:32 +00002291 }
2292
Guy Benyei11169dd2012-12-18 14:30:41 +00002293 return Success;
Dmitri Gribenkof430da42014-02-12 10:33:14 +00002294 }
2295
Chris Lattnere7b154b2013-01-19 21:39:22 +00002296 case llvm::BitstreamEntry::SubBlock:
2297 switch (Entry.ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002298 case INPUT_FILES_BLOCK_ID:
2299 F.InputFilesCursor = Stream;
2300 if (Stream.SkipBlock() || // Skip with the main cursor
2301 // Read the abbreviations
2302 ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
2303 Error("malformed block record in AST file");
2304 return Failure;
2305 }
2306 continue;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002307
Guy Benyei11169dd2012-12-18 14:30:41 +00002308 default:
Chris Lattnere7b154b2013-01-19 21:39:22 +00002309 if (Stream.SkipBlock()) {
2310 Error("malformed block record in AST file");
2311 return Failure;
2312 }
2313 continue;
Guy Benyei11169dd2012-12-18 14:30:41 +00002314 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00002315
2316 case llvm::BitstreamEntry::Record:
2317 // The interesting case.
2318 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002319 }
2320
2321 // Read and process a record.
2322 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00002323 StringRef Blob;
2324 switch ((ControlRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002325 case METADATA: {
2326 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2327 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
Dmitri Gribenko2228cd32014-02-11 15:40:09 +00002328 Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
2329 : diag::err_pch_version_too_new);
Guy Benyei11169dd2012-12-18 14:30:41 +00002330 return VersionMismatch;
2331 }
2332
2333 bool hasErrors = Record[5];
2334 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
2335 Diag(diag::err_pch_with_compiler_errors);
2336 return HadErrors;
2337 }
2338
2339 F.RelocatablePCH = Record[4];
Richard Smith7ed1bc92014-12-05 22:42:13 +00002340 // Relative paths in a relocatable PCH are relative to our sysroot.
2341 if (F.RelocatablePCH)
2342 F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
Guy Benyei11169dd2012-12-18 14:30:41 +00002343
2344 const std::string &CurBranch = getClangFullRepositoryVersion();
Chris Lattner0e6c9402013-01-20 02:38:54 +00002345 StringRef ASTBranch = Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00002346 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2347 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
Dmitri Gribenko2228cd32014-02-11 15:40:09 +00002348 Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
Guy Benyei11169dd2012-12-18 14:30:41 +00002349 return VersionMismatch;
2350 }
2351 break;
2352 }
2353
Ben Langmuir487ea142014-10-23 18:05:36 +00002354 case SIGNATURE:
2355 assert((!F.Signature || F.Signature == Record[0]) && "signature changed");
2356 F.Signature = Record[0];
2357 break;
2358
Guy Benyei11169dd2012-12-18 14:30:41 +00002359 case IMPORTS: {
2360 // Load each of the imported PCH files.
2361 unsigned Idx = 0, N = Record.size();
2362 while (Idx < N) {
2363 // Read information about the AST file.
2364 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
2365 // The import location will be the local one for now; we will adjust
2366 // all import locations of module imports after the global source
2367 // location info are setup.
2368 SourceLocation ImportLoc =
2369 SourceLocation::getFromRawEncoding(Record[Idx++]);
Douglas Gregor7029ce12013-03-19 00:28:20 +00002370 off_t StoredSize = (off_t)Record[Idx++];
2371 time_t StoredModTime = (time_t)Record[Idx++];
Ben Langmuir487ea142014-10-23 18:05:36 +00002372 ASTFileSignature StoredSignature = Record[Idx++];
Richard Smith7ed1bc92014-12-05 22:42:13 +00002373 auto ImportedFile = ReadPath(F, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00002374
2375 // Load the AST file.
2376 switch(ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F, Loaded,
Ben Langmuir487ea142014-10-23 18:05:36 +00002377 StoredSize, StoredModTime, StoredSignature,
Guy Benyei11169dd2012-12-18 14:30:41 +00002378 ClientLoadCapabilities)) {
2379 case Failure: return Failure;
2380 // If we have to ignore the dependency, we'll have to ignore this too.
Douglas Gregor2f1806e2013-03-19 00:38:50 +00002381 case Missing:
Guy Benyei11169dd2012-12-18 14:30:41 +00002382 case OutOfDate: return OutOfDate;
2383 case VersionMismatch: return VersionMismatch;
2384 case ConfigurationMismatch: return ConfigurationMismatch;
2385 case HadErrors: return HadErrors;
2386 case Success: break;
2387 }
2388 }
2389 break;
2390 }
2391
Richard Smith7f330cd2015-03-18 01:42:29 +00002392 case KNOWN_MODULE_FILES:
2393 break;
2394
Guy Benyei11169dd2012-12-18 14:30:41 +00002395 case LANGUAGE_OPTIONS: {
2396 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
Richard Smith1e2cf0d2014-10-31 02:28:58 +00002397 // FIXME: The &F == *ModuleMgr.begin() check is wrong for modules.
Guy Benyei11169dd2012-12-18 14:30:41 +00002398 if (Listener && &F == *ModuleMgr.begin() &&
Richard Smith1e2cf0d2014-10-31 02:28:58 +00002399 ParseLanguageOptions(Record, Complain, *Listener,
2400 AllowCompatibleConfigurationMismatch) &&
Ben Langmuir2cb4a782014-02-05 22:21:15 +00002401 !DisableValidation && !AllowConfigurationMismatch)
Guy Benyei11169dd2012-12-18 14:30:41 +00002402 return ConfigurationMismatch;
2403 break;
2404 }
2405
2406 case TARGET_OPTIONS: {
2407 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2408 if (Listener && &F == *ModuleMgr.begin() &&
Chandler Carruth0d745bc2015-03-14 04:47:43 +00002409 ParseTargetOptions(Record, Complain, *Listener,
2410 AllowCompatibleConfigurationMismatch) &&
Ben Langmuir2cb4a782014-02-05 22:21:15 +00002411 !DisableValidation && !AllowConfigurationMismatch)
Guy Benyei11169dd2012-12-18 14:30:41 +00002412 return ConfigurationMismatch;
2413 break;
2414 }
2415
2416 case DIAGNOSTIC_OPTIONS: {
Ben Langmuirb92de022014-04-29 16:25:26 +00002417 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate)==0;
Guy Benyei11169dd2012-12-18 14:30:41 +00002418 if (Listener && &F == *ModuleMgr.begin() &&
Richard Smith1e2cf0d2014-10-31 02:28:58 +00002419 !AllowCompatibleConfigurationMismatch &&
Guy Benyei11169dd2012-12-18 14:30:41 +00002420 ParseDiagnosticOptions(Record, Complain, *Listener) &&
Ben Langmuirb92de022014-04-29 16:25:26 +00002421 !DisableValidation)
2422 return OutOfDate;
Guy Benyei11169dd2012-12-18 14:30:41 +00002423 break;
2424 }
2425
2426 case FILE_SYSTEM_OPTIONS: {
2427 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2428 if (Listener && &F == *ModuleMgr.begin() &&
Richard Smith1e2cf0d2014-10-31 02:28:58 +00002429 !AllowCompatibleConfigurationMismatch &&
Guy Benyei11169dd2012-12-18 14:30:41 +00002430 ParseFileSystemOptions(Record, Complain, *Listener) &&
Ben Langmuir2cb4a782014-02-05 22:21:15 +00002431 !DisableValidation && !AllowConfigurationMismatch)
Guy Benyei11169dd2012-12-18 14:30:41 +00002432 return ConfigurationMismatch;
2433 break;
2434 }
2435
2436 case HEADER_SEARCH_OPTIONS: {
2437 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2438 if (Listener && &F == *ModuleMgr.begin() &&
Richard Smith1e2cf0d2014-10-31 02:28:58 +00002439 !AllowCompatibleConfigurationMismatch &&
Guy Benyei11169dd2012-12-18 14:30:41 +00002440 ParseHeaderSearchOptions(Record, Complain, *Listener) &&
Ben Langmuir2cb4a782014-02-05 22:21:15 +00002441 !DisableValidation && !AllowConfigurationMismatch)
Guy Benyei11169dd2012-12-18 14:30:41 +00002442 return ConfigurationMismatch;
2443 break;
2444 }
2445
2446 case PREPROCESSOR_OPTIONS: {
2447 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2448 if (Listener && &F == *ModuleMgr.begin() &&
Richard Smith1e2cf0d2014-10-31 02:28:58 +00002449 !AllowCompatibleConfigurationMismatch &&
Guy Benyei11169dd2012-12-18 14:30:41 +00002450 ParsePreprocessorOptions(Record, Complain, *Listener,
2451 SuggestedPredefines) &&
Ben Langmuir2cb4a782014-02-05 22:21:15 +00002452 !DisableValidation && !AllowConfigurationMismatch)
Guy Benyei11169dd2012-12-18 14:30:41 +00002453 return ConfigurationMismatch;
2454 break;
2455 }
2456
2457 case ORIGINAL_FILE:
2458 F.OriginalSourceFileID = FileID::get(Record[0]);
Chris Lattner0e6c9402013-01-20 02:38:54 +00002459 F.ActualOriginalSourceFileName = Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00002460 F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
Richard Smith7ed1bc92014-12-05 22:42:13 +00002461 ResolveImportedPath(F, F.OriginalSourceFileName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002462 break;
2463
2464 case ORIGINAL_FILE_ID:
2465 F.OriginalSourceFileID = FileID::get(Record[0]);
2466 break;
2467
2468 case ORIGINAL_PCH_DIR:
Chris Lattner0e6c9402013-01-20 02:38:54 +00002469 F.OriginalDir = Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00002470 break;
2471
Ben Langmuirbeee15e2014-04-14 18:00:01 +00002472 case MODULE_NAME:
2473 F.ModuleName = Blob;
Ben Langmuir4f5212a2014-04-14 22:12:44 +00002474 if (Listener)
2475 Listener->ReadModuleName(F.ModuleName);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00002476 break;
2477
Richard Smith223d3f22014-12-06 03:21:08 +00002478 case MODULE_DIRECTORY: {
2479 assert(!F.ModuleName.empty() &&
2480 "MODULE_DIRECTORY found before MODULE_NAME");
2481 // If we've already loaded a module map file covering this module, we may
2482 // have a better path for it (relative to the current build).
2483 Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
2484 if (M && M->Directory) {
2485 // If we're implicitly loading a module, the base directory can't
2486 // change between the build and use.
2487 if (F.Kind != MK_ExplicitModule) {
2488 const DirectoryEntry *BuildDir =
2489 PP.getFileManager().getDirectory(Blob);
2490 if (!BuildDir || BuildDir != M->Directory) {
2491 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2492 Diag(diag::err_imported_module_relocated)
2493 << F.ModuleName << Blob << M->Directory->getName();
2494 return OutOfDate;
2495 }
2496 }
2497 F.BaseDirectory = M->Directory->getName();
2498 } else {
2499 F.BaseDirectory = Blob;
2500 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00002501 break;
Richard Smith223d3f22014-12-06 03:21:08 +00002502 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00002503
Ben Langmuirbeee15e2014-04-14 18:00:01 +00002504 case MODULE_MAP_FILE:
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00002505 if (ASTReadResult Result =
2506 ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
2507 return Result;
Ben Langmuir264ea152014-11-08 00:06:39 +00002508 break;
2509
Guy Benyei11169dd2012-12-18 14:30:41 +00002510 case INPUT_FILE_OFFSETS:
Richard Smitha1825302014-10-23 22:18:29 +00002511 NumInputs = Record[0];
2512 NumUserInputs = Record[1];
Richard Smithec216502015-02-13 19:48:37 +00002513 F.InputFileOffsets = (const uint64_t *)Blob.data();
Richard Smitha1825302014-10-23 22:18:29 +00002514 F.InputFilesLoaded.resize(NumInputs);
Guy Benyei11169dd2012-12-18 14:30:41 +00002515 break;
2516 }
2517 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002518}
2519
Ben Langmuir2c9af442014-04-10 17:57:43 +00002520ASTReader::ASTReadResult
2521ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00002522 BitstreamCursor &Stream = F.Stream;
Guy Benyei11169dd2012-12-18 14:30:41 +00002523
2524 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
2525 Error("malformed block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002526 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002527 }
2528
2529 // Read all of the records and blocks for the AST file.
2530 RecordData Record;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002531 while (1) {
2532 llvm::BitstreamEntry Entry = Stream.advance();
2533
2534 switch (Entry.Kind) {
2535 case llvm::BitstreamEntry::Error:
2536 Error("error at end of module block in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002537 return Failure;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002538 case llvm::BitstreamEntry::EndBlock: {
Richard Smithc0fbba72013-04-03 22:49:41 +00002539 // Outside of C++, we do not store a lookup map for the translation unit.
2540 // Instead, mark it as needing a lookup map to be built if this module
2541 // contains any declarations lexically within it (which it always does!).
2542 // This usually has no cost, since we very rarely need the lookup map for
2543 // the translation unit outside C++.
Guy Benyei11169dd2012-12-18 14:30:41 +00002544 DeclContext *DC = Context.getTranslationUnitDecl();
Richard Smithc0fbba72013-04-03 22:49:41 +00002545 if (DC->hasExternalLexicalStorage() &&
2546 !getContext().getLangOpts().CPlusPlus)
Guy Benyei11169dd2012-12-18 14:30:41 +00002547 DC->setMustBuildLookupTable();
Chris Lattnere7b154b2013-01-19 21:39:22 +00002548
Ben Langmuir2c9af442014-04-10 17:57:43 +00002549 return Success;
Guy Benyei11169dd2012-12-18 14:30:41 +00002550 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00002551 case llvm::BitstreamEntry::SubBlock:
2552 switch (Entry.ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002553 case DECLTYPES_BLOCK_ID:
2554 // We lazily load the decls block, but we want to set up the
2555 // DeclsCursor cursor to point into it. Clone our current bitcode
2556 // cursor to it, enter the block and read the abbrevs in that block.
2557 // With the main cursor, we just skip over it.
2558 F.DeclsCursor = Stream;
2559 if (Stream.SkipBlock() || // Skip with the main cursor.
2560 // Read the abbrevs.
2561 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
2562 Error("malformed block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002563 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002564 }
2565 break;
Richard Smithb9eab6d2014-03-20 19:44:17 +00002566
Guy Benyei11169dd2012-12-18 14:30:41 +00002567 case PREPROCESSOR_BLOCK_ID:
2568 F.MacroCursor = Stream;
2569 if (!PP.getExternalSource())
2570 PP.setExternalSource(this);
Chris Lattnere7b154b2013-01-19 21:39:22 +00002571
Guy Benyei11169dd2012-12-18 14:30:41 +00002572 if (Stream.SkipBlock() ||
2573 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
2574 Error("malformed block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002575 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002576 }
2577 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
2578 break;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002579
Guy Benyei11169dd2012-12-18 14:30:41 +00002580 case PREPROCESSOR_DETAIL_BLOCK_ID:
2581 F.PreprocessorDetailCursor = Stream;
2582 if (Stream.SkipBlock() ||
Chris Lattnere7b154b2013-01-19 21:39:22 +00002583 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
Guy Benyei11169dd2012-12-18 14:30:41 +00002584 PREPROCESSOR_DETAIL_BLOCK_ID)) {
Chris Lattnere7b154b2013-01-19 21:39:22 +00002585 Error("malformed preprocessor detail record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002586 return Failure;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002587 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002588 F.PreprocessorDetailStartOffset
Chris Lattnere7b154b2013-01-19 21:39:22 +00002589 = F.PreprocessorDetailCursor.GetCurrentBitNo();
2590
Guy Benyei11169dd2012-12-18 14:30:41 +00002591 if (!PP.getPreprocessingRecord())
2592 PP.createPreprocessingRecord();
2593 if (!PP.getPreprocessingRecord()->getExternalSource())
2594 PP.getPreprocessingRecord()->SetExternalSource(*this);
2595 break;
2596
2597 case SOURCE_MANAGER_BLOCK_ID:
2598 if (ReadSourceManagerBlock(F))
Ben Langmuir2c9af442014-04-10 17:57:43 +00002599 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002600 break;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002601
Guy Benyei11169dd2012-12-18 14:30:41 +00002602 case SUBMODULE_BLOCK_ID:
Ben Langmuir2c9af442014-04-10 17:57:43 +00002603 if (ASTReadResult Result = ReadSubmoduleBlock(F, ClientLoadCapabilities))
2604 return Result;
Guy Benyei11169dd2012-12-18 14:30:41 +00002605 break;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002606
Guy Benyei11169dd2012-12-18 14:30:41 +00002607 case COMMENTS_BLOCK_ID: {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00002608 BitstreamCursor C = Stream;
Guy Benyei11169dd2012-12-18 14:30:41 +00002609 if (Stream.SkipBlock() ||
2610 ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
2611 Error("malformed comments block in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002612 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002613 }
2614 CommentsCursors.push_back(std::make_pair(C, &F));
2615 break;
2616 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00002617
Guy Benyei11169dd2012-12-18 14:30:41 +00002618 default:
Chris Lattnere7b154b2013-01-19 21:39:22 +00002619 if (Stream.SkipBlock()) {
2620 Error("malformed block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002621 return Failure;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002622 }
2623 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002624 }
2625 continue;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002626
2627 case llvm::BitstreamEntry::Record:
2628 // The interesting case.
2629 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002630 }
2631
2632 // Read and process a record.
2633 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00002634 StringRef Blob;
2635 switch ((ASTRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002636 default: // Default behavior: ignore.
2637 break;
2638
2639 case TYPE_OFFSET: {
2640 if (F.LocalNumTypes != 0) {
2641 Error("duplicate TYPE_OFFSET record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002642 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002643 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00002644 F.TypeOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002645 F.LocalNumTypes = Record[0];
2646 unsigned LocalBaseTypeIndex = Record[1];
2647 F.BaseTypeIndex = getTotalNumTypes();
2648
2649 if (F.LocalNumTypes > 0) {
2650 // Introduce the global -> local mapping for types within this module.
2651 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
2652
2653 // Introduce the local -> global mapping for types within this module.
2654 F.TypeRemap.insertOrReplace(
2655 std::make_pair(LocalBaseTypeIndex,
2656 F.BaseTypeIndex - LocalBaseTypeIndex));
Ben Langmuir52ca6782014-10-20 16:27:32 +00002657
2658 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
Guy Benyei11169dd2012-12-18 14:30:41 +00002659 }
2660 break;
2661 }
2662
2663 case DECL_OFFSET: {
2664 if (F.LocalNumDecls != 0) {
2665 Error("duplicate DECL_OFFSET record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002666 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002667 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00002668 F.DeclOffsets = (const DeclOffset *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002669 F.LocalNumDecls = Record[0];
2670 unsigned LocalBaseDeclID = Record[1];
2671 F.BaseDeclID = getTotalNumDecls();
2672
2673 if (F.LocalNumDecls > 0) {
2674 // Introduce the global -> local mapping for declarations within this
2675 // module.
2676 GlobalDeclMap.insert(
2677 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
2678
2679 // Introduce the local -> global mapping for declarations within this
2680 // module.
2681 F.DeclRemap.insertOrReplace(
2682 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
2683
2684 // Introduce the global -> local mapping for declarations within this
2685 // module.
2686 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
Ben Langmuirfe971d92014-08-16 04:54:18 +00002687
Ben Langmuir52ca6782014-10-20 16:27:32 +00002688 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
2689 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002690 break;
2691 }
2692
2693 case TU_UPDATE_LEXICAL: {
2694 DeclContext *TU = Context.getTranslationUnitDecl();
2695 DeclContextInfo &Info = F.DeclContextInfos[TU];
Chris Lattner0e6c9402013-01-20 02:38:54 +00002696 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(Blob.data());
Guy Benyei11169dd2012-12-18 14:30:41 +00002697 Info.NumLexicalDecls
Chris Lattner0e6c9402013-01-20 02:38:54 +00002698 = static_cast<unsigned int>(Blob.size() / sizeof(KindDeclIDPair));
Guy Benyei11169dd2012-12-18 14:30:41 +00002699 TU->setHasExternalLexicalStorage(true);
2700 break;
2701 }
2702
2703 case UPDATE_VISIBLE: {
2704 unsigned Idx = 0;
2705 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
2706 ASTDeclContextNameLookupTable *Table =
Justin Bognerda4e6502014-04-14 16:34:29 +00002707 ASTDeclContextNameLookupTable::Create(
2708 (const unsigned char *)Blob.data() + Record[Idx++],
2709 (const unsigned char *)Blob.data() + sizeof(uint32_t),
2710 (const unsigned char *)Blob.data(),
2711 ASTDeclContextNameLookupTrait(*this, F));
Richard Smithcd45dbc2014-04-19 03:48:30 +00002712 if (Decl *D = GetExistingDecl(ID)) {
Richard Smithd9174792014-03-11 03:10:46 +00002713 auto *DC = cast<DeclContext>(D);
2714 DC->getPrimaryContext()->setHasExternalVisibleStorage(true);
Richard Smith52e3fba2014-03-11 07:17:35 +00002715 auto *&LookupTable = F.DeclContextInfos[DC].NameLookupTableData;
2716 delete LookupTable;
2717 LookupTable = Table;
Guy Benyei11169dd2012-12-18 14:30:41 +00002718 } else
2719 PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F));
2720 break;
2721 }
2722
2723 case IDENTIFIER_TABLE:
Chris Lattner0e6c9402013-01-20 02:38:54 +00002724 F.IdentifierTableData = Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002725 if (Record[0]) {
Justin Bognerda4e6502014-04-14 16:34:29 +00002726 F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
2727 (const unsigned char *)F.IdentifierTableData + Record[0],
2728 (const unsigned char *)F.IdentifierTableData + sizeof(uint32_t),
2729 (const unsigned char *)F.IdentifierTableData,
2730 ASTIdentifierLookupTrait(*this, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002731
2732 PP.getIdentifierTable().setExternalIdentifierLookup(this);
2733 }
2734 break;
2735
2736 case IDENTIFIER_OFFSET: {
2737 if (F.LocalNumIdentifiers != 0) {
2738 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002739 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002740 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00002741 F.IdentifierOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002742 F.LocalNumIdentifiers = Record[0];
2743 unsigned LocalBaseIdentifierID = Record[1];
2744 F.BaseIdentifierID = getTotalNumIdentifiers();
2745
2746 if (F.LocalNumIdentifiers > 0) {
2747 // Introduce the global -> local mapping for identifiers within this
2748 // module.
2749 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
2750 &F));
2751
2752 // Introduce the local -> global mapping for identifiers within this
2753 // module.
2754 F.IdentifierRemap.insertOrReplace(
2755 std::make_pair(LocalBaseIdentifierID,
2756 F.BaseIdentifierID - LocalBaseIdentifierID));
Ben Langmuirfe971d92014-08-16 04:54:18 +00002757
Ben Langmuir52ca6782014-10-20 16:27:32 +00002758 IdentifiersLoaded.resize(IdentifiersLoaded.size()
2759 + F.LocalNumIdentifiers);
2760 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002761 break;
2762 }
2763
Ben Langmuir332aafe2014-01-31 01:06:56 +00002764 case EAGERLY_DESERIALIZED_DECLS:
Richard Smith9e2341d2015-03-23 03:25:59 +00002765 // FIXME: Skip reading this record if our ASTConsumer doesn't care
2766 // about "interesting" decls (for instance, if we're building a module).
Guy Benyei11169dd2012-12-18 14:30:41 +00002767 for (unsigned I = 0, N = Record.size(); I != N; ++I)
Ben Langmuir332aafe2014-01-31 01:06:56 +00002768 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
Guy Benyei11169dd2012-12-18 14:30:41 +00002769 break;
2770
2771 case SPECIAL_TYPES:
Douglas Gregor44180f82013-02-01 23:45:03 +00002772 if (SpecialTypes.empty()) {
2773 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2774 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
2775 break;
2776 }
2777
2778 if (SpecialTypes.size() != Record.size()) {
2779 Error("invalid special-types record");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002780 return Failure;
Douglas Gregor44180f82013-02-01 23:45:03 +00002781 }
2782
2783 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2784 serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
2785 if (!SpecialTypes[I])
2786 SpecialTypes[I] = ID;
2787 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
2788 // merge step?
2789 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002790 break;
2791
2792 case STATISTICS:
2793 TotalNumStatements += Record[0];
2794 TotalNumMacros += Record[1];
2795 TotalLexicalDeclContexts += Record[2];
2796 TotalVisibleDeclContexts += Record[3];
2797 break;
2798
2799 case UNUSED_FILESCOPED_DECLS:
2800 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2801 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
2802 break;
2803
2804 case DELEGATING_CTORS:
2805 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2806 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
2807 break;
2808
2809 case WEAK_UNDECLARED_IDENTIFIERS:
2810 if (Record.size() % 4 != 0) {
2811 Error("invalid weak identifiers record");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002812 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002813 }
2814
2815 // FIXME: Ignore weak undeclared identifiers from non-original PCH
2816 // files. This isn't the way to do it :)
2817 WeakUndeclaredIdentifiers.clear();
2818
2819 // Translate the weak, undeclared identifiers into global IDs.
2820 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
2821 WeakUndeclaredIdentifiers.push_back(
2822 getGlobalIdentifierID(F, Record[I++]));
2823 WeakUndeclaredIdentifiers.push_back(
2824 getGlobalIdentifierID(F, Record[I++]));
2825 WeakUndeclaredIdentifiers.push_back(
2826 ReadSourceLocation(F, Record, I).getRawEncoding());
2827 WeakUndeclaredIdentifiers.push_back(Record[I++]);
2828 }
2829 break;
2830
Guy Benyei11169dd2012-12-18 14:30:41 +00002831 case SELECTOR_OFFSETS: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00002832 F.SelectorOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002833 F.LocalNumSelectors = Record[0];
2834 unsigned LocalBaseSelectorID = Record[1];
2835 F.BaseSelectorID = getTotalNumSelectors();
2836
2837 if (F.LocalNumSelectors > 0) {
2838 // Introduce the global -> local mapping for selectors within this
2839 // module.
2840 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
2841
2842 // Introduce the local -> global mapping for selectors within this
2843 // module.
2844 F.SelectorRemap.insertOrReplace(
2845 std::make_pair(LocalBaseSelectorID,
2846 F.BaseSelectorID - LocalBaseSelectorID));
Ben Langmuir52ca6782014-10-20 16:27:32 +00002847
2848 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
Guy Benyei11169dd2012-12-18 14:30:41 +00002849 }
2850 break;
2851 }
2852
2853 case METHOD_POOL:
Chris Lattner0e6c9402013-01-20 02:38:54 +00002854 F.SelectorLookupTableData = (const unsigned char *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002855 if (Record[0])
2856 F.SelectorLookupTable
2857 = ASTSelectorLookupTable::Create(
2858 F.SelectorLookupTableData + Record[0],
2859 F.SelectorLookupTableData,
2860 ASTSelectorLookupTrait(*this, F));
2861 TotalNumMethodPoolEntries += Record[1];
2862 break;
2863
2864 case REFERENCED_SELECTOR_POOL:
2865 if (!Record.empty()) {
2866 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
2867 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
2868 Record[Idx++]));
2869 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
2870 getRawEncoding());
2871 }
2872 }
2873 break;
2874
2875 case PP_COUNTER_VALUE:
2876 if (!Record.empty() && Listener)
2877 Listener->ReadCounter(F, Record[0]);
2878 break;
2879
2880 case FILE_SORTED_DECLS:
Chris Lattner0e6c9402013-01-20 02:38:54 +00002881 F.FileSortedDecls = (const DeclID *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002882 F.NumFileSortedDecls = Record[0];
2883 break;
2884
2885 case SOURCE_LOCATION_OFFSETS: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00002886 F.SLocEntryOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002887 F.LocalNumSLocEntries = Record[0];
2888 unsigned SLocSpaceSize = Record[1];
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00002889 std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
Ben Langmuir52ca6782014-10-20 16:27:32 +00002890 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
Guy Benyei11169dd2012-12-18 14:30:41 +00002891 SLocSpaceSize);
2892 // Make our entry in the range map. BaseID is negative and growing, so
2893 // we invert it. Because we invert it, though, we need the other end of
2894 // the range.
2895 unsigned RangeStart =
2896 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
2897 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
2898 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
2899
2900 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
2901 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
2902 GlobalSLocOffsetMap.insert(
2903 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
2904 - SLocSpaceSize,&F));
2905
2906 // Initialize the remapping table.
2907 // Invalid stays invalid.
Richard Smithb9eab6d2014-03-20 19:44:17 +00002908 F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
Guy Benyei11169dd2012-12-18 14:30:41 +00002909 // This module. Base was 2 when being compiled.
Richard Smithb9eab6d2014-03-20 19:44:17 +00002910 F.SLocRemap.insertOrReplace(std::make_pair(2U,
Guy Benyei11169dd2012-12-18 14:30:41 +00002911 static_cast<int>(F.SLocEntryBaseOffset - 2)));
2912
2913 TotalNumSLocEntries += F.LocalNumSLocEntries;
2914 break;
2915 }
2916
2917 case MODULE_OFFSET_MAP: {
2918 // Additional remapping information.
Chris Lattner0e6c9402013-01-20 02:38:54 +00002919 const unsigned char *Data = (const unsigned char*)Blob.data();
2920 const unsigned char *DataEnd = Data + Blob.size();
Richard Smithb9eab6d2014-03-20 19:44:17 +00002921
2922 // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
2923 if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
2924 F.SLocRemap.insert(std::make_pair(0U, 0));
2925 F.SLocRemap.insert(std::make_pair(2U, 1));
2926 }
2927
Guy Benyei11169dd2012-12-18 14:30:41 +00002928 // Continuous range maps we may be updating in our module.
Ben Langmuir785180e2014-10-20 16:27:30 +00002929 typedef ContinuousRangeMap<uint32_t, int, 2>::Builder
2930 RemapBuilder;
2931 RemapBuilder SLocRemap(F.SLocRemap);
2932 RemapBuilder IdentifierRemap(F.IdentifierRemap);
2933 RemapBuilder MacroRemap(F.MacroRemap);
2934 RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
2935 RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
2936 RemapBuilder SelectorRemap(F.SelectorRemap);
2937 RemapBuilder DeclRemap(F.DeclRemap);
2938 RemapBuilder TypeRemap(F.TypeRemap);
Guy Benyei11169dd2012-12-18 14:30:41 +00002939
2940 while(Data < DataEnd) {
Justin Bogner57ba0b22014-03-28 22:03:24 +00002941 using namespace llvm::support;
2942 uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data);
Guy Benyei11169dd2012-12-18 14:30:41 +00002943 StringRef Name = StringRef((const char*)Data, Len);
2944 Data += Len;
2945 ModuleFile *OM = ModuleMgr.lookup(Name);
2946 if (!OM) {
2947 Error("SourceLocation remap refers to unknown module");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002948 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002949 }
2950
Justin Bogner57ba0b22014-03-28 22:03:24 +00002951 uint32_t SLocOffset =
2952 endian::readNext<uint32_t, little, unaligned>(Data);
2953 uint32_t IdentifierIDOffset =
2954 endian::readNext<uint32_t, little, unaligned>(Data);
2955 uint32_t MacroIDOffset =
2956 endian::readNext<uint32_t, little, unaligned>(Data);
2957 uint32_t PreprocessedEntityIDOffset =
2958 endian::readNext<uint32_t, little, unaligned>(Data);
2959 uint32_t SubmoduleIDOffset =
2960 endian::readNext<uint32_t, little, unaligned>(Data);
2961 uint32_t SelectorIDOffset =
2962 endian::readNext<uint32_t, little, unaligned>(Data);
2963 uint32_t DeclIDOffset =
2964 endian::readNext<uint32_t, little, unaligned>(Data);
2965 uint32_t TypeIndexOffset =
2966 endian::readNext<uint32_t, little, unaligned>(Data);
2967
Ben Langmuir785180e2014-10-20 16:27:30 +00002968 uint32_t None = std::numeric_limits<uint32_t>::max();
2969
2970 auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
2971 RemapBuilder &Remap) {
2972 if (Offset != None)
2973 Remap.insert(std::make_pair(Offset,
2974 static_cast<int>(BaseOffset - Offset)));
2975 };
2976 mapOffset(SLocOffset, OM->SLocEntryBaseOffset, SLocRemap);
2977 mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap);
2978 mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
2979 mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
2980 PreprocessedEntityRemap);
2981 mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
2982 mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
2983 mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap);
2984 mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap);
Guy Benyei11169dd2012-12-18 14:30:41 +00002985
2986 // Global -> local mappings.
2987 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
2988 }
2989 break;
2990 }
2991
2992 case SOURCE_MANAGER_LINE_TABLE:
2993 if (ParseLineTable(F, Record))
Ben Langmuir2c9af442014-04-10 17:57:43 +00002994 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002995 break;
2996
2997 case SOURCE_LOCATION_PRELOADS: {
2998 // Need to transform from the local view (1-based IDs) to the global view,
2999 // which is based off F.SLocEntryBaseID.
3000 if (!F.PreloadSLocEntries.empty()) {
3001 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003002 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003003 }
3004
3005 F.PreloadSLocEntries.swap(Record);
3006 break;
3007 }
3008
3009 case EXT_VECTOR_DECLS:
3010 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3011 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
3012 break;
3013
3014 case VTABLE_USES:
3015 if (Record.size() % 3 != 0) {
3016 Error("Invalid VTABLE_USES record");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003017 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003018 }
3019
3020 // Later tables overwrite earlier ones.
3021 // FIXME: Modules will have some trouble with this. This is clearly not
3022 // the right way to do this.
3023 VTableUses.clear();
3024
3025 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
3026 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
3027 VTableUses.push_back(
3028 ReadSourceLocation(F, Record, Idx).getRawEncoding());
3029 VTableUses.push_back(Record[Idx++]);
3030 }
3031 break;
3032
Guy Benyei11169dd2012-12-18 14:30:41 +00003033 case PENDING_IMPLICIT_INSTANTIATIONS:
3034 if (PendingInstantiations.size() % 2 != 0) {
3035 Error("Invalid existing PendingInstantiations");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003036 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003037 }
3038
3039 if (Record.size() % 2 != 0) {
3040 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003041 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003042 }
3043
3044 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3045 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
3046 PendingInstantiations.push_back(
3047 ReadSourceLocation(F, Record, I).getRawEncoding());
3048 }
3049 break;
3050
3051 case SEMA_DECL_REFS:
Richard Smith3d8e97e2013-10-18 06:54:39 +00003052 if (Record.size() != 2) {
3053 Error("Invalid SEMA_DECL_REFS block");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003054 return Failure;
Richard Smith3d8e97e2013-10-18 06:54:39 +00003055 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003056 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3057 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3058 break;
3059
3060 case PPD_ENTITIES_OFFSETS: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00003061 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
3062 assert(Blob.size() % sizeof(PPEntityOffset) == 0);
3063 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
Guy Benyei11169dd2012-12-18 14:30:41 +00003064
3065 unsigned LocalBasePreprocessedEntityID = Record[0];
3066
3067 unsigned StartingID;
3068 if (!PP.getPreprocessingRecord())
3069 PP.createPreprocessingRecord();
3070 if (!PP.getPreprocessingRecord()->getExternalSource())
3071 PP.getPreprocessingRecord()->SetExternalSource(*this);
3072 StartingID
3073 = PP.getPreprocessingRecord()
Ben Langmuir52ca6782014-10-20 16:27:32 +00003074 ->allocateLoadedEntities(F.NumPreprocessedEntities);
Guy Benyei11169dd2012-12-18 14:30:41 +00003075 F.BasePreprocessedEntityID = StartingID;
3076
3077 if (F.NumPreprocessedEntities > 0) {
3078 // Introduce the global -> local mapping for preprocessed entities in
3079 // this module.
3080 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
3081
3082 // Introduce the local -> global mapping for preprocessed entities in
3083 // this module.
3084 F.PreprocessedEntityRemap.insertOrReplace(
3085 std::make_pair(LocalBasePreprocessedEntityID,
3086 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
3087 }
3088
3089 break;
3090 }
3091
3092 case DECL_UPDATE_OFFSETS: {
3093 if (Record.size() % 2 != 0) {
3094 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003095 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003096 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00003097 for (unsigned I = 0, N = Record.size(); I != N; I += 2) {
3098 GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
3099 DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1]));
3100
3101 // If we've already loaded the decl, perform the updates when we finish
3102 // loading this block.
3103 if (Decl *D = GetExistingDecl(ID))
3104 PendingUpdateRecords.push_back(std::make_pair(ID, D));
3105 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003106 break;
3107 }
3108
3109 case DECL_REPLACEMENTS: {
3110 if (Record.size() % 3 != 0) {
3111 Error("invalid DECL_REPLACEMENTS block in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003112 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003113 }
3114 for (unsigned I = 0, N = Record.size(); I != N; I += 3)
3115 ReplacedDecls[getGlobalDeclID(F, Record[I])]
3116 = ReplacedDeclInfo(&F, Record[I+1], Record[I+2]);
3117 break;
3118 }
3119
3120 case OBJC_CATEGORIES_MAP: {
3121 if (F.LocalNumObjCCategoriesInMap != 0) {
3122 Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003123 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003124 }
3125
3126 F.LocalNumObjCCategoriesInMap = Record[0];
Chris Lattner0e6c9402013-01-20 02:38:54 +00003127 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003128 break;
3129 }
3130
3131 case OBJC_CATEGORIES:
3132 F.ObjCCategories.swap(Record);
3133 break;
Richard Smithc2bb8182015-03-24 06:36:48 +00003134
Guy Benyei11169dd2012-12-18 14:30:41 +00003135 case CXX_BASE_SPECIFIER_OFFSETS: {
3136 if (F.LocalNumCXXBaseSpecifiers != 0) {
3137 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003138 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003139 }
Richard Smithc2bb8182015-03-24 06:36:48 +00003140
Guy Benyei11169dd2012-12-18 14:30:41 +00003141 F.LocalNumCXXBaseSpecifiers = Record[0];
Chris Lattner0e6c9402013-01-20 02:38:54 +00003142 F.CXXBaseSpecifiersOffsets = (const uint32_t *)Blob.data();
Richard Smithc2bb8182015-03-24 06:36:48 +00003143 break;
3144 }
3145
3146 case CXX_CTOR_INITIALIZERS_OFFSETS: {
3147 if (F.LocalNumCXXCtorInitializers != 0) {
3148 Error("duplicate CXX_CTOR_INITIALIZERS_OFFSETS record in AST file");
3149 return Failure;
3150 }
3151
3152 F.LocalNumCXXCtorInitializers = Record[0];
3153 F.CXXCtorInitializersOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003154 break;
3155 }
3156
3157 case DIAG_PRAGMA_MAPPINGS:
3158 if (F.PragmaDiagMappings.empty())
3159 F.PragmaDiagMappings.swap(Record);
3160 else
3161 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
3162 Record.begin(), Record.end());
3163 break;
3164
3165 case CUDA_SPECIAL_DECL_REFS:
3166 // Later tables overwrite earlier ones.
3167 // FIXME: Modules will have trouble with this.
3168 CUDASpecialDeclRefs.clear();
3169 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3170 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3171 break;
3172
3173 case HEADER_SEARCH_TABLE: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00003174 F.HeaderFileInfoTableData = Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003175 F.LocalNumHeaderFileInfos = Record[1];
Guy Benyei11169dd2012-12-18 14:30:41 +00003176 if (Record[0]) {
3177 F.HeaderFileInfoTable
3178 = HeaderFileInfoLookupTable::Create(
3179 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
3180 (const unsigned char *)F.HeaderFileInfoTableData,
3181 HeaderFileInfoTrait(*this, F,
3182 &PP.getHeaderSearchInfo(),
Chris Lattner0e6c9402013-01-20 02:38:54 +00003183 Blob.data() + Record[2]));
Guy Benyei11169dd2012-12-18 14:30:41 +00003184
3185 PP.getHeaderSearchInfo().SetExternalSource(this);
3186 if (!PP.getHeaderSearchInfo().getExternalLookup())
3187 PP.getHeaderSearchInfo().SetExternalLookup(this);
3188 }
3189 break;
3190 }
3191
3192 case FP_PRAGMA_OPTIONS:
3193 // Later tables overwrite earlier ones.
3194 FPPragmaOptions.swap(Record);
3195 break;
3196
3197 case OPENCL_EXTENSIONS:
3198 // Later tables overwrite earlier ones.
3199 OpenCLExtensions.swap(Record);
3200 break;
3201
3202 case TENTATIVE_DEFINITIONS:
3203 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3204 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
3205 break;
3206
3207 case KNOWN_NAMESPACES:
3208 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3209 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
3210 break;
Nick Lewycky8334af82013-01-26 00:35:08 +00003211
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00003212 case UNDEFINED_BUT_USED:
3213 if (UndefinedButUsed.size() % 2 != 0) {
3214 Error("Invalid existing UndefinedButUsed");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003215 return Failure;
Nick Lewycky8334af82013-01-26 00:35:08 +00003216 }
3217
3218 if (Record.size() % 2 != 0) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00003219 Error("invalid undefined-but-used record");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003220 return Failure;
Nick Lewycky8334af82013-01-26 00:35:08 +00003221 }
3222 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00003223 UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
3224 UndefinedButUsed.push_back(
Nick Lewycky8334af82013-01-26 00:35:08 +00003225 ReadSourceLocation(F, Record, I).getRawEncoding());
3226 }
3227 break;
3228
Guy Benyei11169dd2012-12-18 14:30:41 +00003229 case IMPORTED_MODULES: {
Richard Smithe842a472014-10-22 02:05:46 +00003230 if (F.Kind != MK_ImplicitModule && F.Kind != MK_ExplicitModule) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003231 // If we aren't loading a module (which has its own exports), make
3232 // all of the imported modules visible.
3233 // FIXME: Deal with macros-only imports.
Richard Smith56be7542014-03-21 00:33:59 +00003234 for (unsigned I = 0, N = Record.size(); I != N; /**/) {
3235 unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
3236 SourceLocation Loc = ReadSourceLocation(F, Record, I);
3237 if (GlobalID)
Aaron Ballman4f45b712014-03-21 15:22:56 +00003238 ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
Guy Benyei11169dd2012-12-18 14:30:41 +00003239 }
3240 }
3241 break;
3242 }
3243
3244 case LOCAL_REDECLARATIONS: {
3245 F.RedeclarationChains.swap(Record);
3246 break;
3247 }
3248
3249 case LOCAL_REDECLARATIONS_MAP: {
3250 if (F.LocalNumRedeclarationsInMap != 0) {
3251 Error("duplicate LOCAL_REDECLARATIONS_MAP record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003252 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003253 }
3254
3255 F.LocalNumRedeclarationsInMap = Record[0];
Chris Lattner0e6c9402013-01-20 02:38:54 +00003256 F.RedeclarationsMap = (const LocalRedeclarationsInfo *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003257 break;
3258 }
3259
Guy Benyei11169dd2012-12-18 14:30:41 +00003260 case MACRO_OFFSET: {
3261 if (F.LocalNumMacros != 0) {
3262 Error("duplicate MACRO_OFFSET record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003263 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003264 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00003265 F.MacroOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003266 F.LocalNumMacros = Record[0];
3267 unsigned LocalBaseMacroID = Record[1];
3268 F.BaseMacroID = getTotalNumMacros();
3269
3270 if (F.LocalNumMacros > 0) {
3271 // Introduce the global -> local mapping for macros within this module.
3272 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3273
3274 // Introduce the local -> global mapping for macros within this module.
3275 F.MacroRemap.insertOrReplace(
3276 std::make_pair(LocalBaseMacroID,
3277 F.BaseMacroID - LocalBaseMacroID));
Ben Langmuir52ca6782014-10-20 16:27:32 +00003278
3279 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
Guy Benyei11169dd2012-12-18 14:30:41 +00003280 }
3281 break;
3282 }
3283
Richard Smithe40f2ba2013-08-07 21:41:30 +00003284 case LATE_PARSED_TEMPLATE: {
3285 LateParsedTemplates.append(Record.begin(), Record.end());
3286 break;
3287 }
Dario Domizioli13a0a382014-05-23 12:13:25 +00003288
3289 case OPTIMIZE_PRAGMA_OPTIONS:
3290 if (Record.size() != 1) {
3291 Error("invalid pragma optimize record");
3292 return Failure;
3293 }
3294 OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
3295 break;
Nico Weber72889432014-09-06 01:25:55 +00003296
3297 case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
3298 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3299 UnusedLocalTypedefNameCandidates.push_back(
3300 getGlobalDeclID(F, Record[I]));
3301 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003302 }
3303 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003304}
3305
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003306ASTReader::ASTReadResult
3307ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
3308 const ModuleFile *ImportedBy,
3309 unsigned ClientLoadCapabilities) {
3310 unsigned Idx = 0;
Richard Smith7ed1bc92014-12-05 22:42:13 +00003311 F.ModuleMapPath = ReadPath(F, Record, Idx);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003312
Richard Smithe842a472014-10-22 02:05:46 +00003313 if (F.Kind == MK_ExplicitModule) {
3314 // For an explicitly-loaded module, we don't care whether the original
3315 // module map file exists or matches.
3316 return Success;
3317 }
3318
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003319 // Try to resolve ModuleName in the current header search context and
3320 // verify that it is found in the same module map file as we saved. If the
3321 // top-level AST file is a main file, skip this check because there is no
3322 // usable header search context.
3323 assert(!F.ModuleName.empty() &&
Richard Smithe842a472014-10-22 02:05:46 +00003324 "MODULE_NAME should come before MODULE_MAP_FILE");
3325 if (F.Kind == MK_ImplicitModule &&
3326 (*ModuleMgr.begin())->Kind != MK_MainFile) {
3327 // An implicitly-loaded module file should have its module listed in some
3328 // module map file that we've already loaded.
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003329 Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
Richard Smithe842a472014-10-22 02:05:46 +00003330 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
3331 const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr;
3332 if (!ModMap) {
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003333 assert(ImportedBy && "top-level import should be verified");
3334 if ((ClientLoadCapabilities & ARR_Missing) == 0)
Richard Smithe842a472014-10-22 02:05:46 +00003335 Diag(diag::err_imported_module_not_found) << F.ModuleName << F.FileName
3336 << ImportedBy->FileName
3337 << F.ModuleMapPath;
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003338 return Missing;
3339 }
3340
Richard Smithe842a472014-10-22 02:05:46 +00003341 assert(M->Name == F.ModuleName && "found module with different name");
3342
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003343 // Check the primary module map file.
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003344 const FileEntry *StoredModMap = FileMgr.getFile(F.ModuleMapPath);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003345 if (StoredModMap == nullptr || StoredModMap != ModMap) {
3346 assert(ModMap && "found module is missing module map file");
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003347 assert(ImportedBy && "top-level import should be verified");
3348 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3349 Diag(diag::err_imported_module_modmap_changed)
3350 << F.ModuleName << ImportedBy->FileName
3351 << ModMap->getName() << F.ModuleMapPath;
3352 return OutOfDate;
3353 }
3354
3355 llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps;
3356 for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
3357 // FIXME: we should use input files rather than storing names.
Richard Smith7ed1bc92014-12-05 22:42:13 +00003358 std::string Filename = ReadPath(F, Record, Idx);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003359 const FileEntry *F =
3360 FileMgr.getFile(Filename, false, false);
3361 if (F == nullptr) {
3362 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3363 Error("could not find file '" + Filename +"' referenced by AST file");
3364 return OutOfDate;
3365 }
3366 AdditionalStoredMaps.insert(F);
3367 }
3368
3369 // Check any additional module map files (e.g. module.private.modulemap)
3370 // that are not in the pcm.
3371 if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
3372 for (const FileEntry *ModMap : *AdditionalModuleMaps) {
3373 // Remove files that match
3374 // Note: SmallPtrSet::erase is really remove
3375 if (!AdditionalStoredMaps.erase(ModMap)) {
3376 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3377 Diag(diag::err_module_different_modmap)
3378 << F.ModuleName << /*new*/0 << ModMap->getName();
3379 return OutOfDate;
3380 }
3381 }
3382 }
3383
3384 // Check any additional module map files that are in the pcm, but not
3385 // found in header search. Cases that match are already removed.
3386 for (const FileEntry *ModMap : AdditionalStoredMaps) {
3387 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3388 Diag(diag::err_module_different_modmap)
3389 << F.ModuleName << /*not new*/1 << ModMap->getName();
3390 return OutOfDate;
3391 }
3392 }
3393
3394 if (Listener)
3395 Listener->ReadModuleMapFile(F.ModuleMapPath);
3396 return Success;
3397}
3398
3399
Douglas Gregorc1489562013-02-12 23:36:21 +00003400/// \brief Move the given method to the back of the global list of methods.
3401static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
3402 // Find the entry for this selector in the method pool.
3403 Sema::GlobalMethodPool::iterator Known
3404 = S.MethodPool.find(Method->getSelector());
3405 if (Known == S.MethodPool.end())
3406 return;
3407
3408 // Retrieve the appropriate method list.
3409 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
3410 : Known->second.second;
3411 bool Found = false;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003412 for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
Douglas Gregorc1489562013-02-12 23:36:21 +00003413 if (!Found) {
Nico Weber2e0c8f72014-12-27 03:58:08 +00003414 if (List->getMethod() == Method) {
Douglas Gregorc1489562013-02-12 23:36:21 +00003415 Found = true;
3416 } else {
3417 // Keep searching.
3418 continue;
3419 }
3420 }
3421
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003422 if (List->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003423 List->setMethod(List->getNext()->getMethod());
Douglas Gregorc1489562013-02-12 23:36:21 +00003424 else
Nico Weber2e0c8f72014-12-27 03:58:08 +00003425 List->setMethod(Method);
Douglas Gregorc1489562013-02-12 23:36:21 +00003426 }
3427}
3428
Richard Smithe657bbd2014-07-18 22:13:40 +00003429void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner,
3430 bool FromFinalization) {
3431 // FIXME: Only do this if Owner->NameVisibility == AllVisible.
Richard Smith57721ac2014-07-21 04:10:40 +00003432 for (Decl *D : Names.HiddenDecls) {
Richard Smith49f906a2014-03-01 00:08:04 +00003433 bool wasHidden = D->Hidden;
3434 D->Hidden = false;
Guy Benyei11169dd2012-12-18 14:30:41 +00003435
Richard Smith49f906a2014-03-01 00:08:04 +00003436 if (wasHidden && SemaObj) {
3437 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
3438 moveMethodToBackOfGlobalList(*SemaObj, Method);
Douglas Gregorc1489562013-02-12 23:36:21 +00003439 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003440 }
3441 }
Richard Smith49f906a2014-03-01 00:08:04 +00003442
Richard Smithe657bbd2014-07-18 22:13:40 +00003443 assert((FromFinalization || Owner->NameVisibility >= Module::MacrosVisible) &&
3444 "nothing to make visible?");
Richard Smithdaa69e02014-07-25 04:40:03 +00003445 for (const auto &Macro : Names.HiddenMacros) {
3446 if (FromFinalization)
3447 PP.appendMacroDirective(Macro.first,
3448 Macro.second->import(PP, SourceLocation()));
3449 else
Richard Smithe56c8bc2015-04-22 00:26:11 +00003450 installImportedMacro(Macro.first, *Macro.second, Owner);
Richard Smithdaa69e02014-07-25 04:40:03 +00003451 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003452}
3453
Richard Smith49f906a2014-03-01 00:08:04 +00003454void ASTReader::makeModuleVisible(Module *Mod,
Argyrios Kyrtzidis125df052013-02-01 16:36:12 +00003455 Module::NameVisibilityKind NameVisibility,
Douglas Gregorfb912652013-03-20 21:10:35 +00003456 SourceLocation ImportLoc,
3457 bool Complain) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003458 llvm::SmallPtrSet<Module *, 4> Visited;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003459 SmallVector<Module *, 4> Stack;
Robert Wilhelm25284cc2013-08-23 16:11:15 +00003460 Stack.push_back(Mod);
Guy Benyei11169dd2012-12-18 14:30:41 +00003461 while (!Stack.empty()) {
Robert Wilhelm25284cc2013-08-23 16:11:15 +00003462 Mod = Stack.pop_back_val();
Guy Benyei11169dd2012-12-18 14:30:41 +00003463
3464 if (NameVisibility <= Mod->NameVisibility) {
Robert Wilhelm25284cc2013-08-23 16:11:15 +00003465 // This module already has this level of visibility (or greater), so
Guy Benyei11169dd2012-12-18 14:30:41 +00003466 // there is nothing more to do.
3467 continue;
3468 }
Richard Smith49f906a2014-03-01 00:08:04 +00003469
Guy Benyei11169dd2012-12-18 14:30:41 +00003470 if (!Mod->isAvailable()) {
3471 // Modules that aren't available cannot be made visible.
3472 continue;
3473 }
3474
3475 // Update the module's name visibility.
Richard Smith49f906a2014-03-01 00:08:04 +00003476 if (NameVisibility >= Module::MacrosVisible &&
3477 Mod->NameVisibility < Module::MacrosVisible)
3478 Mod->MacroVisibilityLoc = ImportLoc;
Guy Benyei11169dd2012-12-18 14:30:41 +00003479 Mod->NameVisibility = NameVisibility;
Richard Smith49f906a2014-03-01 00:08:04 +00003480
Guy Benyei11169dd2012-12-18 14:30:41 +00003481 // If we've already deserialized any names from this module,
3482 // mark them as visible.
3483 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
3484 if (Hidden != HiddenNamesMap.end()) {
Richard Smith57721ac2014-07-21 04:10:40 +00003485 auto HiddenNames = std::move(*Hidden);
Guy Benyei11169dd2012-12-18 14:30:41 +00003486 HiddenNamesMap.erase(Hidden);
Richard Smith57721ac2014-07-21 04:10:40 +00003487 makeNamesVisible(HiddenNames.second, HiddenNames.first,
3488 /*FromFinalization*/false);
3489 assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() &&
3490 "making names visible added hidden names");
Guy Benyei11169dd2012-12-18 14:30:41 +00003491 }
Dmitri Gribenkoe9bcf5b2013-11-04 21:51:33 +00003492
Guy Benyei11169dd2012-12-18 14:30:41 +00003493 // Push any exported modules onto the stack to be marked as visible.
Argyrios Kyrtzidis8739f7b2013-02-19 19:34:40 +00003494 SmallVector<Module *, 16> Exports;
3495 Mod->getExportedModules(Exports);
3496 for (SmallVectorImpl<Module *>::iterator
3497 I = Exports.begin(), E = Exports.end(); I != E; ++I) {
3498 Module *Exported = *I;
David Blaikie82e95a32014-11-19 07:49:47 +00003499 if (Visited.insert(Exported).second)
Argyrios Kyrtzidis8739f7b2013-02-19 19:34:40 +00003500 Stack.push_back(Exported);
Guy Benyei11169dd2012-12-18 14:30:41 +00003501 }
Douglas Gregorfb912652013-03-20 21:10:35 +00003502
3503 // Detect any conflicts.
3504 if (Complain) {
3505 assert(ImportLoc.isValid() && "Missing import location");
3506 for (unsigned I = 0, N = Mod->Conflicts.size(); I != N; ++I) {
3507 if (Mod->Conflicts[I].Other->NameVisibility >= NameVisibility) {
3508 Diag(ImportLoc, diag::warn_module_conflict)
3509 << Mod->getFullModuleName()
3510 << Mod->Conflicts[I].Other->getFullModuleName()
3511 << Mod->Conflicts[I].Message;
3512 // FIXME: Need note where the other module was imported.
3513 }
3514 }
3515 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003516 }
3517}
3518
Douglas Gregore060e572013-01-25 01:03:03 +00003519bool ASTReader::loadGlobalIndex() {
3520 if (GlobalIndex)
3521 return false;
3522
3523 if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
3524 !Context.getLangOpts().Modules)
3525 return true;
3526
3527 // Try to load the global index.
3528 TriedLoadingGlobalIndex = true;
3529 StringRef ModuleCachePath
3530 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
3531 std::pair<GlobalModuleIndex *, GlobalModuleIndex::ErrorCode> Result
Douglas Gregor7029ce12013-03-19 00:28:20 +00003532 = GlobalModuleIndex::readIndex(ModuleCachePath);
Douglas Gregore060e572013-01-25 01:03:03 +00003533 if (!Result.first)
3534 return true;
3535
3536 GlobalIndex.reset(Result.first);
Douglas Gregor7211ac12013-01-25 23:32:03 +00003537 ModuleMgr.setGlobalIndex(GlobalIndex.get());
Douglas Gregore060e572013-01-25 01:03:03 +00003538 return false;
3539}
3540
3541bool ASTReader::isGlobalIndexUnavailable() const {
3542 return Context.getLangOpts().Modules && UseGlobalIndex &&
3543 !hasGlobalIndex() && TriedLoadingGlobalIndex;
3544}
3545
Dmitri Gribenkof430da42014-02-12 10:33:14 +00003546static void updateModuleTimestamp(ModuleFile &MF) {
3547 // Overwrite the timestamp file contents so that file's mtime changes.
3548 std::string TimestampFilename = MF.getTimestampFilename();
Rafael Espindoladae941a2014-08-25 18:17:04 +00003549 std::error_code EC;
3550 llvm::raw_fd_ostream OS(TimestampFilename, EC, llvm::sys::fs::F_Text);
3551 if (EC)
Dmitri Gribenkof430da42014-02-12 10:33:14 +00003552 return;
3553 OS << "Timestamp file\n";
3554}
3555
Guy Benyei11169dd2012-12-18 14:30:41 +00003556ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
3557 ModuleKind Type,
3558 SourceLocation ImportLoc,
3559 unsigned ClientLoadCapabilities) {
Argyrios Kyrtzidisdc9fdaf2013-05-24 05:44:08 +00003560 llvm::SaveAndRestore<SourceLocation>
3561 SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
3562
Richard Smithd1c46742014-04-30 02:24:17 +00003563 // Defer any pending actions until we get to the end of reading the AST file.
3564 Deserializing AnASTFile(this);
3565
Guy Benyei11169dd2012-12-18 14:30:41 +00003566 // Bump the generation number.
Richard Smith053f6c62014-05-16 23:01:30 +00003567 unsigned PreviousGeneration = incrementGeneration(Context);
Guy Benyei11169dd2012-12-18 14:30:41 +00003568
3569 unsigned NumModules = ModuleMgr.size();
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003570 SmallVector<ImportedModule, 4> Loaded;
Guy Benyei11169dd2012-12-18 14:30:41 +00003571 switch(ASTReadResult ReadResult = ReadASTCore(FileName, Type, ImportLoc,
Craig Toppera13603a2014-05-22 05:54:18 +00003572 /*ImportedBy=*/nullptr, Loaded,
Ben Langmuir487ea142014-10-23 18:05:36 +00003573 0, 0, 0,
Guy Benyei11169dd2012-12-18 14:30:41 +00003574 ClientLoadCapabilities)) {
3575 case Failure:
Douglas Gregor7029ce12013-03-19 00:28:20 +00003576 case Missing:
Guy Benyei11169dd2012-12-18 14:30:41 +00003577 case OutOfDate:
3578 case VersionMismatch:
3579 case ConfigurationMismatch:
Ben Langmuir9801b252014-06-20 00:24:56 +00003580 case HadErrors: {
3581 llvm::SmallPtrSet<ModuleFile *, 4> LoadedSet;
3582 for (const ImportedModule &IM : Loaded)
3583 LoadedSet.insert(IM.Mod);
3584
Douglas Gregor7029ce12013-03-19 00:28:20 +00003585 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, ModuleMgr.end(),
Ben Langmuir9801b252014-06-20 00:24:56 +00003586 LoadedSet,
Douglas Gregor7029ce12013-03-19 00:28:20 +00003587 Context.getLangOpts().Modules
3588 ? &PP.getHeaderSearchInfo().getModuleMap()
Craig Toppera13603a2014-05-22 05:54:18 +00003589 : nullptr);
Douglas Gregore060e572013-01-25 01:03:03 +00003590
3591 // If we find that any modules are unusable, the global index is going
3592 // to be out-of-date. Just remove it.
3593 GlobalIndex.reset();
Craig Toppera13603a2014-05-22 05:54:18 +00003594 ModuleMgr.setGlobalIndex(nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00003595 return ReadResult;
Ben Langmuir9801b252014-06-20 00:24:56 +00003596 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003597 case Success:
3598 break;
3599 }
3600
3601 // Here comes stuff that we only do once the entire chain is loaded.
3602
3603 // Load the AST blocks of all of the modules that we loaded.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003604 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
3605 MEnd = Loaded.end();
Guy Benyei11169dd2012-12-18 14:30:41 +00003606 M != MEnd; ++M) {
3607 ModuleFile &F = *M->Mod;
3608
3609 // Read the AST block.
Ben Langmuir2c9af442014-04-10 17:57:43 +00003610 if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities))
3611 return Result;
Guy Benyei11169dd2012-12-18 14:30:41 +00003612
3613 // Once read, set the ModuleFile bit base offset and update the size in
3614 // bits of all files we've seen.
3615 F.GlobalBitOffset = TotalModulesSizeInBits;
3616 TotalModulesSizeInBits += F.SizeInBits;
3617 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
3618
3619 // Preload SLocEntries.
3620 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
3621 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
3622 // Load it through the SourceManager and don't call ReadSLocEntry()
3623 // directly because the entry may have already been loaded in which case
3624 // calling ReadSLocEntry() directly would trigger an assertion in
3625 // SourceManager.
3626 SourceMgr.getLoadedSLocEntryByID(Index);
3627 }
3628 }
3629
Douglas Gregor603cd862013-03-22 18:50:14 +00003630 // Setup the import locations and notify the module manager that we've
3631 // committed to these module files.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003632 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
3633 MEnd = Loaded.end();
Guy Benyei11169dd2012-12-18 14:30:41 +00003634 M != MEnd; ++M) {
3635 ModuleFile &F = *M->Mod;
Douglas Gregor603cd862013-03-22 18:50:14 +00003636
3637 ModuleMgr.moduleFileAccepted(&F);
3638
3639 // Set the import location.
Argyrios Kyrtzidis71c1af82013-02-01 16:36:14 +00003640 F.DirectImportLoc = ImportLoc;
Guy Benyei11169dd2012-12-18 14:30:41 +00003641 if (!M->ImportedBy)
3642 F.ImportLoc = M->ImportLoc;
3643 else
3644 F.ImportLoc = ReadSourceLocation(*M->ImportedBy,
3645 M->ImportLoc.getRawEncoding());
3646 }
3647
3648 // Mark all of the identifiers in the identifier table as being out of date,
3649 // so that various accessors know to check the loaded modules when the
3650 // identifier is used.
3651 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
3652 IdEnd = PP.getIdentifierTable().end();
3653 Id != IdEnd; ++Id)
3654 Id->second->setOutOfDate(true);
3655
3656 // Resolve any unresolved module exports.
Douglas Gregorfb912652013-03-20 21:10:35 +00003657 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
3658 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
Guy Benyei11169dd2012-12-18 14:30:41 +00003659 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
3660 Module *ResolvedMod = getSubmodule(GlobalID);
Douglas Gregorfb912652013-03-20 21:10:35 +00003661
3662 switch (Unresolved.Kind) {
3663 case UnresolvedModuleRef::Conflict:
3664 if (ResolvedMod) {
3665 Module::Conflict Conflict;
3666 Conflict.Other = ResolvedMod;
3667 Conflict.Message = Unresolved.String.str();
3668 Unresolved.Mod->Conflicts.push_back(Conflict);
3669 }
3670 continue;
3671
3672 case UnresolvedModuleRef::Import:
Guy Benyei11169dd2012-12-18 14:30:41 +00003673 if (ResolvedMod)
3674 Unresolved.Mod->Imports.push_back(ResolvedMod);
3675 continue;
Guy Benyei11169dd2012-12-18 14:30:41 +00003676
Douglas Gregorfb912652013-03-20 21:10:35 +00003677 case UnresolvedModuleRef::Export:
3678 if (ResolvedMod || Unresolved.IsWildcard)
3679 Unresolved.Mod->Exports.push_back(
3680 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
3681 continue;
3682 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003683 }
Douglas Gregorfb912652013-03-20 21:10:35 +00003684 UnresolvedModuleRefs.clear();
Daniel Jasperba7f2f72013-09-24 09:14:14 +00003685
3686 // FIXME: How do we load the 'use'd modules? They may not be submodules.
3687 // Might be unnecessary as use declarations are only used to build the
3688 // module itself.
Guy Benyei11169dd2012-12-18 14:30:41 +00003689
3690 InitializeContext();
3691
Richard Smith3d8e97e2013-10-18 06:54:39 +00003692 if (SemaObj)
3693 UpdateSema();
3694
Guy Benyei11169dd2012-12-18 14:30:41 +00003695 if (DeserializationListener)
3696 DeserializationListener->ReaderInitialized(this);
3697
3698 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
3699 if (!PrimaryModule.OriginalSourceFileID.isInvalid()) {
3700 PrimaryModule.OriginalSourceFileID
3701 = FileID::get(PrimaryModule.SLocEntryBaseID
3702 + PrimaryModule.OriginalSourceFileID.getOpaqueValue() - 1);
3703
3704 // If this AST file is a precompiled preamble, then set the
3705 // preamble file ID of the source manager to the file source file
3706 // from which the preamble was built.
3707 if (Type == MK_Preamble) {
3708 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
3709 } else if (Type == MK_MainFile) {
3710 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
3711 }
3712 }
3713
3714 // For any Objective-C class definitions we have already loaded, make sure
3715 // that we load any additional categories.
3716 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
3717 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
3718 ObjCClassesLoaded[I],
3719 PreviousGeneration);
3720 }
Douglas Gregore060e572013-01-25 01:03:03 +00003721
Dmitri Gribenkof430da42014-02-12 10:33:14 +00003722 if (PP.getHeaderSearchInfo()
3723 .getHeaderSearchOpts()
3724 .ModulesValidateOncePerBuildSession) {
3725 // Now we are certain that the module and all modules it depends on are
3726 // up to date. Create or update timestamp files for modules that are
3727 // located in the module cache (not for PCH files that could be anywhere
3728 // in the filesystem).
3729 for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
3730 ImportedModule &M = Loaded[I];
Richard Smithe842a472014-10-22 02:05:46 +00003731 if (M.Mod->Kind == MK_ImplicitModule) {
Dmitri Gribenkof430da42014-02-12 10:33:14 +00003732 updateModuleTimestamp(*M.Mod);
3733 }
3734 }
3735 }
3736
Guy Benyei11169dd2012-12-18 14:30:41 +00003737 return Success;
3738}
3739
Ben Langmuir487ea142014-10-23 18:05:36 +00003740static ASTFileSignature readASTFileSignature(llvm::BitstreamReader &StreamFile);
3741
Ben Langmuir70a1b812015-03-24 04:43:52 +00003742/// \brief Whether \p Stream starts with the AST/PCH file magic number 'CPCH'.
3743static bool startsWithASTFileMagic(BitstreamCursor &Stream) {
3744 return Stream.Read(8) == 'C' &&
3745 Stream.Read(8) == 'P' &&
3746 Stream.Read(8) == 'C' &&
3747 Stream.Read(8) == 'H';
3748}
3749
Guy Benyei11169dd2012-12-18 14:30:41 +00003750ASTReader::ASTReadResult
3751ASTReader::ReadASTCore(StringRef FileName,
3752 ModuleKind Type,
3753 SourceLocation ImportLoc,
3754 ModuleFile *ImportedBy,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003755 SmallVectorImpl<ImportedModule> &Loaded,
Douglas Gregor7029ce12013-03-19 00:28:20 +00003756 off_t ExpectedSize, time_t ExpectedModTime,
Ben Langmuir487ea142014-10-23 18:05:36 +00003757 ASTFileSignature ExpectedSignature,
Guy Benyei11169dd2012-12-18 14:30:41 +00003758 unsigned ClientLoadCapabilities) {
3759 ModuleFile *M;
Guy Benyei11169dd2012-12-18 14:30:41 +00003760 std::string ErrorStr;
Douglas Gregor7029ce12013-03-19 00:28:20 +00003761 ModuleManager::AddModuleResult AddResult
3762 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
Richard Smith053f6c62014-05-16 23:01:30 +00003763 getGeneration(), ExpectedSize, ExpectedModTime,
Ben Langmuir487ea142014-10-23 18:05:36 +00003764 ExpectedSignature, readASTFileSignature,
Douglas Gregor7029ce12013-03-19 00:28:20 +00003765 M, ErrorStr);
Guy Benyei11169dd2012-12-18 14:30:41 +00003766
Douglas Gregor7029ce12013-03-19 00:28:20 +00003767 switch (AddResult) {
3768 case ModuleManager::AlreadyLoaded:
3769 return Success;
3770
3771 case ModuleManager::NewlyLoaded:
3772 // Load module file below.
3773 break;
3774
3775 case ModuleManager::Missing:
Richard Smithe842a472014-10-22 02:05:46 +00003776 // The module file was missing; if the client can handle that, return
Douglas Gregor7029ce12013-03-19 00:28:20 +00003777 // it.
3778 if (ClientLoadCapabilities & ARR_Missing)
3779 return Missing;
3780
3781 // Otherwise, return an error.
3782 {
3783 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
3784 + ErrorStr;
3785 Error(Msg);
3786 }
3787 return Failure;
3788
3789 case ModuleManager::OutOfDate:
3790 // We couldn't load the module file because it is out-of-date. If the
3791 // client can handle out-of-date, return it.
3792 if (ClientLoadCapabilities & ARR_OutOfDate)
3793 return OutOfDate;
3794
3795 // Otherwise, return an error.
3796 {
3797 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
3798 + ErrorStr;
3799 Error(Msg);
3800 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003801 return Failure;
3802 }
3803
Douglas Gregor7029ce12013-03-19 00:28:20 +00003804 assert(M && "Missing module file");
Guy Benyei11169dd2012-12-18 14:30:41 +00003805
3806 // FIXME: This seems rather a hack. Should CurrentDir be part of the
3807 // module?
3808 if (FileName != "-") {
3809 CurrentDir = llvm::sys::path::parent_path(FileName);
3810 if (CurrentDir.empty()) CurrentDir = ".";
3811 }
3812
3813 ModuleFile &F = *M;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003814 BitstreamCursor &Stream = F.Stream;
Rafael Espindolafd832392014-11-12 14:48:44 +00003815 Stream.init(&F.StreamFile);
Adrian Prantlcbc368c2015-02-25 02:44:04 +00003816 F.SizeInBits = F.Buffer->getBufferSize() * 8;
3817
Guy Benyei11169dd2012-12-18 14:30:41 +00003818 // Sniff for the signature.
Ben Langmuir70a1b812015-03-24 04:43:52 +00003819 if (!startsWithASTFileMagic(Stream)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003820 Diag(diag::err_not_a_pch_file) << FileName;
3821 return Failure;
3822 }
3823
3824 // This is used for compatibility with older PCH formats.
3825 bool HaveReadControlBlock = false;
3826
Chris Lattnerefa77172013-01-20 00:00:22 +00003827 while (1) {
3828 llvm::BitstreamEntry Entry = Stream.advance();
3829
3830 switch (Entry.Kind) {
3831 case llvm::BitstreamEntry::Error:
3832 case llvm::BitstreamEntry::EndBlock:
3833 case llvm::BitstreamEntry::Record:
Guy Benyei11169dd2012-12-18 14:30:41 +00003834 Error("invalid record at top-level of AST file");
3835 return Failure;
Chris Lattnerefa77172013-01-20 00:00:22 +00003836
3837 case llvm::BitstreamEntry::SubBlock:
3838 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003839 }
3840
Guy Benyei11169dd2012-12-18 14:30:41 +00003841 // We only know the control subblock ID.
Chris Lattnerefa77172013-01-20 00:00:22 +00003842 switch (Entry.ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003843 case llvm::bitc::BLOCKINFO_BLOCK_ID:
3844 if (Stream.ReadBlockInfoBlock()) {
3845 Error("malformed BlockInfoBlock in AST file");
3846 return Failure;
3847 }
3848 break;
3849 case CONTROL_BLOCK_ID:
3850 HaveReadControlBlock = true;
Ben Langmuirbeee15e2014-04-14 18:00:01 +00003851 switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003852 case Success:
3853 break;
3854
3855 case Failure: return Failure;
Douglas Gregor7029ce12013-03-19 00:28:20 +00003856 case Missing: return Missing;
Guy Benyei11169dd2012-12-18 14:30:41 +00003857 case OutOfDate: return OutOfDate;
3858 case VersionMismatch: return VersionMismatch;
3859 case ConfigurationMismatch: return ConfigurationMismatch;
3860 case HadErrors: return HadErrors;
3861 }
3862 break;
3863 case AST_BLOCK_ID:
3864 if (!HaveReadControlBlock) {
3865 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
Dmitri Gribenko2228cd32014-02-11 15:40:09 +00003866 Diag(diag::err_pch_version_too_old);
Guy Benyei11169dd2012-12-18 14:30:41 +00003867 return VersionMismatch;
3868 }
3869
3870 // Record that we've loaded this module.
3871 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
3872 return Success;
3873
3874 default:
3875 if (Stream.SkipBlock()) {
3876 Error("malformed block record in AST file");
3877 return Failure;
3878 }
3879 break;
3880 }
3881 }
3882
3883 return Success;
3884}
3885
3886void ASTReader::InitializeContext() {
3887 // If there's a listener, notify them that we "read" the translation unit.
3888 if (DeserializationListener)
3889 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
3890 Context.getTranslationUnitDecl());
3891
Guy Benyei11169dd2012-12-18 14:30:41 +00003892 // FIXME: Find a better way to deal with collisions between these
3893 // built-in types. Right now, we just ignore the problem.
3894
3895 // Load the special types.
3896 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
3897 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
3898 if (!Context.CFConstantStringTypeDecl)
3899 Context.setCFConstantStringType(GetType(String));
3900 }
3901
3902 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
3903 QualType FileType = GetType(File);
3904 if (FileType.isNull()) {
3905 Error("FILE type is NULL");
3906 return;
3907 }
3908
3909 if (!Context.FILEDecl) {
3910 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
3911 Context.setFILEDecl(Typedef->getDecl());
3912 else {
3913 const TagType *Tag = FileType->getAs<TagType>();
3914 if (!Tag) {
3915 Error("Invalid FILE type in AST file");
3916 return;
3917 }
3918 Context.setFILEDecl(Tag->getDecl());
3919 }
3920 }
3921 }
3922
3923 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
3924 QualType Jmp_bufType = GetType(Jmp_buf);
3925 if (Jmp_bufType.isNull()) {
3926 Error("jmp_buf type is NULL");
3927 return;
3928 }
3929
3930 if (!Context.jmp_bufDecl) {
3931 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
3932 Context.setjmp_bufDecl(Typedef->getDecl());
3933 else {
3934 const TagType *Tag = Jmp_bufType->getAs<TagType>();
3935 if (!Tag) {
3936 Error("Invalid jmp_buf type in AST file");
3937 return;
3938 }
3939 Context.setjmp_bufDecl(Tag->getDecl());
3940 }
3941 }
3942 }
3943
3944 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
3945 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
3946 if (Sigjmp_bufType.isNull()) {
3947 Error("sigjmp_buf type is NULL");
3948 return;
3949 }
3950
3951 if (!Context.sigjmp_bufDecl) {
3952 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
3953 Context.setsigjmp_bufDecl(Typedef->getDecl());
3954 else {
3955 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
3956 assert(Tag && "Invalid sigjmp_buf type in AST file");
3957 Context.setsigjmp_bufDecl(Tag->getDecl());
3958 }
3959 }
3960 }
3961
3962 if (unsigned ObjCIdRedef
3963 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
3964 if (Context.ObjCIdRedefinitionType.isNull())
3965 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
3966 }
3967
3968 if (unsigned ObjCClassRedef
3969 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
3970 if (Context.ObjCClassRedefinitionType.isNull())
3971 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
3972 }
3973
3974 if (unsigned ObjCSelRedef
3975 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
3976 if (Context.ObjCSelRedefinitionType.isNull())
3977 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
3978 }
3979
3980 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
3981 QualType Ucontext_tType = GetType(Ucontext_t);
3982 if (Ucontext_tType.isNull()) {
3983 Error("ucontext_t type is NULL");
3984 return;
3985 }
3986
3987 if (!Context.ucontext_tDecl) {
3988 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
3989 Context.setucontext_tDecl(Typedef->getDecl());
3990 else {
3991 const TagType *Tag = Ucontext_tType->getAs<TagType>();
3992 assert(Tag && "Invalid ucontext_t type in AST file");
3993 Context.setucontext_tDecl(Tag->getDecl());
3994 }
3995 }
3996 }
3997 }
3998
3999 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
4000
4001 // If there were any CUDA special declarations, deserialize them.
4002 if (!CUDASpecialDeclRefs.empty()) {
4003 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
4004 Context.setcudaConfigureCallDecl(
4005 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
4006 }
Richard Smith56be7542014-03-21 00:33:59 +00004007
Guy Benyei11169dd2012-12-18 14:30:41 +00004008 // Re-export any modules that were imported by a non-module AST file.
Richard Smith56be7542014-03-21 00:33:59 +00004009 // FIXME: This does not make macro-only imports visible again. It also doesn't
4010 // make #includes mapped to module imports visible.
4011 for (auto &Import : ImportedModules) {
4012 if (Module *Imported = getSubmodule(Import.ID))
Argyrios Kyrtzidis125df052013-02-01 16:36:12 +00004013 makeModuleVisible(Imported, Module::AllVisible,
Richard Smith56be7542014-03-21 00:33:59 +00004014 /*ImportLoc=*/Import.ImportLoc,
Douglas Gregorfb912652013-03-20 21:10:35 +00004015 /*Complain=*/false);
Guy Benyei11169dd2012-12-18 14:30:41 +00004016 }
4017 ImportedModules.clear();
4018}
4019
4020void ASTReader::finalizeForWriting() {
Richard Smith57721ac2014-07-21 04:10:40 +00004021 while (!HiddenNamesMap.empty()) {
4022 auto HiddenNames = std::move(*HiddenNamesMap.begin());
4023 HiddenNamesMap.erase(HiddenNamesMap.begin());
4024 makeNamesVisible(HiddenNames.second, HiddenNames.first,
4025 /*FromFinalization*/true);
Guy Benyei11169dd2012-12-18 14:30:41 +00004026 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004027}
4028
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004029/// \brief Given a cursor at the start of an AST file, scan ahead and drop the
4030/// cursor into the start of the given block ID, returning false on success and
4031/// true on failure.
4032static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004033 while (1) {
4034 llvm::BitstreamEntry Entry = Cursor.advance();
4035 switch (Entry.Kind) {
4036 case llvm::BitstreamEntry::Error:
4037 case llvm::BitstreamEntry::EndBlock:
4038 return true;
4039
4040 case llvm::BitstreamEntry::Record:
4041 // Ignore top-level records.
4042 Cursor.skipRecord(Entry.ID);
4043 break;
4044
4045 case llvm::BitstreamEntry::SubBlock:
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004046 if (Entry.ID == BlockID) {
4047 if (Cursor.EnterSubBlock(BlockID))
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004048 return true;
4049 // Found it!
4050 return false;
4051 }
4052
4053 if (Cursor.SkipBlock())
4054 return true;
4055 }
4056 }
4057}
4058
Ben Langmuir70a1b812015-03-24 04:43:52 +00004059/// \brief Reads and return the signature record from \p StreamFile's control
4060/// block, or else returns 0.
Ben Langmuir487ea142014-10-23 18:05:36 +00004061static ASTFileSignature readASTFileSignature(llvm::BitstreamReader &StreamFile){
4062 BitstreamCursor Stream(StreamFile);
Ben Langmuir70a1b812015-03-24 04:43:52 +00004063 if (!startsWithASTFileMagic(Stream))
Ben Langmuir487ea142014-10-23 18:05:36 +00004064 return 0;
Ben Langmuir487ea142014-10-23 18:05:36 +00004065
4066 // Scan for the CONTROL_BLOCK_ID block.
4067 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
4068 return 0;
4069
4070 // Scan for SIGNATURE inside the control block.
4071 ASTReader::RecordData Record;
4072 while (1) {
4073 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
4074 if (Entry.Kind == llvm::BitstreamEntry::EndBlock ||
4075 Entry.Kind != llvm::BitstreamEntry::Record)
4076 return 0;
4077
4078 Record.clear();
4079 StringRef Blob;
4080 if (SIGNATURE == Stream.readRecord(Entry.ID, Record, &Blob))
4081 return Record[0];
4082 }
4083}
4084
Guy Benyei11169dd2012-12-18 14:30:41 +00004085/// \brief Retrieve the name of the original source file name
4086/// directly from the AST file, without actually loading the AST
4087/// file.
4088std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
4089 FileManager &FileMgr,
4090 DiagnosticsEngine &Diags) {
4091 // Open the AST file.
Benjamin Kramera8857962014-10-26 22:44:13 +00004092 auto Buffer = FileMgr.getBufferForFile(ASTFileName);
Guy Benyei11169dd2012-12-18 14:30:41 +00004093 if (!Buffer) {
Benjamin Kramera8857962014-10-26 22:44:13 +00004094 Diags.Report(diag::err_fe_unable_to_read_pch_file)
4095 << ASTFileName << Buffer.getError().message();
Guy Benyei11169dd2012-12-18 14:30:41 +00004096 return std::string();
4097 }
4098
4099 // Initialize the stream
4100 llvm::BitstreamReader StreamFile;
Adrian Prantlcbc368c2015-02-25 02:44:04 +00004101 StreamFile.init((const unsigned char *)(*Buffer)->getBufferStart(),
4102 (const unsigned char *)(*Buffer)->getBufferEnd());
Rafael Espindolaaf0e40a2014-11-12 14:42:25 +00004103 BitstreamCursor Stream(StreamFile);
Guy Benyei11169dd2012-12-18 14:30:41 +00004104
4105 // Sniff for the signature.
Ben Langmuir70a1b812015-03-24 04:43:52 +00004106 if (!startsWithASTFileMagic(Stream)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004107 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
4108 return std::string();
4109 }
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004110
Chris Lattnere7b154b2013-01-19 21:39:22 +00004111 // Scan for the CONTROL_BLOCK_ID block.
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004112 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004113 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
4114 return std::string();
Chris Lattnere7b154b2013-01-19 21:39:22 +00004115 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004116
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004117 // Scan for ORIGINAL_FILE inside the control block.
4118 RecordData Record;
Chris Lattnere7b154b2013-01-19 21:39:22 +00004119 while (1) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004120 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Chris Lattnere7b154b2013-01-19 21:39:22 +00004121 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
4122 return std::string();
4123
4124 if (Entry.Kind != llvm::BitstreamEntry::Record) {
4125 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
4126 return std::string();
Guy Benyei11169dd2012-12-18 14:30:41 +00004127 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00004128
Guy Benyei11169dd2012-12-18 14:30:41 +00004129 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00004130 StringRef Blob;
4131 if (Stream.readRecord(Entry.ID, Record, &Blob) == ORIGINAL_FILE)
4132 return Blob.str();
Guy Benyei11169dd2012-12-18 14:30:41 +00004133 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004134}
4135
4136namespace {
4137 class SimplePCHValidator : public ASTReaderListener {
4138 const LangOptions &ExistingLangOpts;
4139 const TargetOptions &ExistingTargetOpts;
4140 const PreprocessorOptions &ExistingPPOpts;
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00004141 std::string ExistingModuleCachePath;
Guy Benyei11169dd2012-12-18 14:30:41 +00004142 FileManager &FileMgr;
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00004143
Guy Benyei11169dd2012-12-18 14:30:41 +00004144 public:
4145 SimplePCHValidator(const LangOptions &ExistingLangOpts,
4146 const TargetOptions &ExistingTargetOpts,
4147 const PreprocessorOptions &ExistingPPOpts,
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00004148 StringRef ExistingModuleCachePath,
Guy Benyei11169dd2012-12-18 14:30:41 +00004149 FileManager &FileMgr)
4150 : ExistingLangOpts(ExistingLangOpts),
4151 ExistingTargetOpts(ExistingTargetOpts),
4152 ExistingPPOpts(ExistingPPOpts),
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00004153 ExistingModuleCachePath(ExistingModuleCachePath),
Guy Benyei11169dd2012-12-18 14:30:41 +00004154 FileMgr(FileMgr)
4155 {
4156 }
4157
Richard Smith1e2cf0d2014-10-31 02:28:58 +00004158 bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
4159 bool AllowCompatibleDifferences) override {
4160 return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr,
4161 AllowCompatibleDifferences);
Guy Benyei11169dd2012-12-18 14:30:41 +00004162 }
Chandler Carruth0d745bc2015-03-14 04:47:43 +00004163 bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
4164 bool AllowCompatibleDifferences) override {
4165 return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr,
4166 AllowCompatibleDifferences);
Guy Benyei11169dd2012-12-18 14:30:41 +00004167 }
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00004168 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
4169 StringRef SpecificModuleCachePath,
4170 bool Complain) override {
4171 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
4172 ExistingModuleCachePath,
4173 nullptr, ExistingLangOpts);
4174 }
Craig Topper3e89dfe2014-03-13 02:13:41 +00004175 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
4176 bool Complain,
4177 std::string &SuggestedPredefines) override {
Craig Toppera13603a2014-05-22 05:54:18 +00004178 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr,
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00004179 SuggestedPredefines, ExistingLangOpts);
Guy Benyei11169dd2012-12-18 14:30:41 +00004180 }
4181 };
4182}
4183
4184bool ASTReader::readASTFileControlBlock(StringRef Filename,
4185 FileManager &FileMgr,
4186 ASTReaderListener &Listener) {
4187 // Open the AST file.
Richard Smith7f330cd2015-03-18 01:42:29 +00004188 // FIXME: This allows use of the VFS; we do not allow use of the
4189 // VFS when actually loading a module.
Benjamin Kramera8857962014-10-26 22:44:13 +00004190 auto Buffer = FileMgr.getBufferForFile(Filename);
Guy Benyei11169dd2012-12-18 14:30:41 +00004191 if (!Buffer) {
4192 return true;
4193 }
4194
4195 // Initialize the stream
4196 llvm::BitstreamReader StreamFile;
Adrian Prantlcbc368c2015-02-25 02:44:04 +00004197 StreamFile.init((const unsigned char *)(*Buffer)->getBufferStart(),
4198 (const unsigned char *)(*Buffer)->getBufferEnd());
Rafael Espindolaaf0e40a2014-11-12 14:42:25 +00004199 BitstreamCursor Stream(StreamFile);
Guy Benyei11169dd2012-12-18 14:30:41 +00004200
4201 // Sniff for the signature.
Ben Langmuir70a1b812015-03-24 04:43:52 +00004202 if (!startsWithASTFileMagic(Stream))
Guy Benyei11169dd2012-12-18 14:30:41 +00004203 return true;
Guy Benyei11169dd2012-12-18 14:30:41 +00004204
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004205 // Scan for the CONTROL_BLOCK_ID block.
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004206 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004207 return true;
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004208
4209 bool NeedsInputFiles = Listener.needsInputFileVisitation();
Ben Langmuircb69b572014-03-07 06:40:32 +00004210 bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
Richard Smithd4b230b2014-10-27 23:01:16 +00004211 bool NeedsImports = Listener.needsImportVisitation();
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004212 BitstreamCursor InputFilesCursor;
4213 if (NeedsInputFiles) {
4214 InputFilesCursor = Stream;
4215 if (SkipCursorToBlock(InputFilesCursor, INPUT_FILES_BLOCK_ID))
4216 return true;
4217
4218 // Read the abbreviations
4219 while (true) {
4220 uint64_t Offset = InputFilesCursor.GetCurrentBitNo();
4221 unsigned Code = InputFilesCursor.ReadCode();
4222
4223 // We expect all abbrevs to be at the start of the block.
4224 if (Code != llvm::bitc::DEFINE_ABBREV) {
4225 InputFilesCursor.JumpToBit(Offset);
4226 break;
4227 }
4228 InputFilesCursor.ReadAbbrevRecord();
4229 }
4230 }
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004231
4232 // Scan for ORIGINAL_FILE inside the control block.
Guy Benyei11169dd2012-12-18 14:30:41 +00004233 RecordData Record;
Richard Smith7ed1bc92014-12-05 22:42:13 +00004234 std::string ModuleDir;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004235 while (1) {
4236 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
4237 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
4238 return false;
4239
4240 if (Entry.Kind != llvm::BitstreamEntry::Record)
4241 return true;
4242
Guy Benyei11169dd2012-12-18 14:30:41 +00004243 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00004244 StringRef Blob;
4245 unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob);
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004246 switch ((ControlRecordTypes)RecCode) {
4247 case METADATA: {
4248 if (Record[0] != VERSION_MAJOR)
4249 return true;
Guy Benyei11169dd2012-12-18 14:30:41 +00004250
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +00004251 if (Listener.ReadFullVersionInformation(Blob))
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004252 return true;
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +00004253
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004254 break;
4255 }
Ben Langmuir4f5212a2014-04-14 22:12:44 +00004256 case MODULE_NAME:
4257 Listener.ReadModuleName(Blob);
4258 break;
Richard Smith7ed1bc92014-12-05 22:42:13 +00004259 case MODULE_DIRECTORY:
4260 ModuleDir = Blob;
4261 break;
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00004262 case MODULE_MAP_FILE: {
4263 unsigned Idx = 0;
Richard Smith7ed1bc92014-12-05 22:42:13 +00004264 auto Path = ReadString(Record, Idx);
4265 ResolveImportedPath(Path, ModuleDir);
4266 Listener.ReadModuleMapFile(Path);
Ben Langmuir4f5212a2014-04-14 22:12:44 +00004267 break;
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00004268 }
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004269 case LANGUAGE_OPTIONS:
Richard Smith1e2cf0d2014-10-31 02:28:58 +00004270 if (ParseLanguageOptions(Record, false, Listener,
4271 /*AllowCompatibleConfigurationMismatch*/false))
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004272 return true;
4273 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004274
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004275 case TARGET_OPTIONS:
Chandler Carruth0d745bc2015-03-14 04:47:43 +00004276 if (ParseTargetOptions(Record, false, Listener,
4277 /*AllowCompatibleConfigurationMismatch*/ false))
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004278 return true;
4279 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004280
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004281 case DIAGNOSTIC_OPTIONS:
4282 if (ParseDiagnosticOptions(Record, false, Listener))
4283 return true;
4284 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004285
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004286 case FILE_SYSTEM_OPTIONS:
4287 if (ParseFileSystemOptions(Record, false, Listener))
4288 return true;
4289 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004290
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004291 case HEADER_SEARCH_OPTIONS:
4292 if (ParseHeaderSearchOptions(Record, false, Listener))
4293 return true;
4294 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004295
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004296 case PREPROCESSOR_OPTIONS: {
4297 std::string IgnoredSuggestedPredefines;
4298 if (ParsePreprocessorOptions(Record, false, Listener,
4299 IgnoredSuggestedPredefines))
4300 return true;
4301 break;
4302 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004303
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004304 case INPUT_FILE_OFFSETS: {
4305 if (!NeedsInputFiles)
4306 break;
4307
4308 unsigned NumInputFiles = Record[0];
4309 unsigned NumUserFiles = Record[1];
Richard Smithec216502015-02-13 19:48:37 +00004310 const uint64_t *InputFileOffs = (const uint64_t *)Blob.data();
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004311 for (unsigned I = 0; I != NumInputFiles; ++I) {
4312 // Go find this input file.
4313 bool isSystemFile = I >= NumUserFiles;
Ben Langmuircb69b572014-03-07 06:40:32 +00004314
4315 if (isSystemFile && !NeedsSystemInputFiles)
4316 break; // the rest are system input files
4317
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004318 BitstreamCursor &Cursor = InputFilesCursor;
4319 SavedStreamPosition SavedPosition(Cursor);
4320 Cursor.JumpToBit(InputFileOffs[I]);
4321
4322 unsigned Code = Cursor.ReadCode();
4323 RecordData Record;
4324 StringRef Blob;
4325 bool shouldContinue = false;
4326 switch ((InputFileRecordTypes)Cursor.readRecord(Code, Record, &Blob)) {
4327 case INPUT_FILE:
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +00004328 bool Overridden = static_cast<bool>(Record[3]);
Richard Smith7ed1bc92014-12-05 22:42:13 +00004329 std::string Filename = Blob;
4330 ResolveImportedPath(Filename, ModuleDir);
4331 shouldContinue =
4332 Listener.visitInputFile(Filename, isSystemFile, Overridden);
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004333 break;
4334 }
4335 if (!shouldContinue)
4336 break;
4337 }
4338 break;
4339 }
4340
Richard Smithd4b230b2014-10-27 23:01:16 +00004341 case IMPORTS: {
4342 if (!NeedsImports)
4343 break;
4344
4345 unsigned Idx = 0, N = Record.size();
4346 while (Idx < N) {
4347 // Read information about the AST file.
Richard Smith79c98cc2014-10-27 23:25:15 +00004348 Idx += 5; // ImportLoc, Size, ModTime, Signature
Richard Smith7ed1bc92014-12-05 22:42:13 +00004349 std::string Filename = ReadString(Record, Idx);
4350 ResolveImportedPath(Filename, ModuleDir);
4351 Listener.visitImport(Filename);
Richard Smithd4b230b2014-10-27 23:01:16 +00004352 }
4353 break;
4354 }
4355
Richard Smith7f330cd2015-03-18 01:42:29 +00004356 case KNOWN_MODULE_FILES: {
4357 // Known-but-not-technically-used module files are treated as imports.
4358 if (!NeedsImports)
4359 break;
4360
4361 unsigned Idx = 0, N = Record.size();
4362 while (Idx < N) {
4363 std::string Filename = ReadString(Record, Idx);
4364 ResolveImportedPath(Filename, ModuleDir);
4365 Listener.visitImport(Filename);
4366 }
4367 break;
4368 }
4369
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004370 default:
4371 // No other validation to perform.
4372 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004373 }
4374 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004375}
4376
4377
4378bool ASTReader::isAcceptableASTFile(StringRef Filename,
4379 FileManager &FileMgr,
4380 const LangOptions &LangOpts,
4381 const TargetOptions &TargetOpts,
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00004382 const PreprocessorOptions &PPOpts,
4383 std::string ExistingModuleCachePath) {
4384 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts,
4385 ExistingModuleCachePath, FileMgr);
Guy Benyei11169dd2012-12-18 14:30:41 +00004386 return !readASTFileControlBlock(Filename, FileMgr, validator);
4387}
4388
Ben Langmuir2c9af442014-04-10 17:57:43 +00004389ASTReader::ASTReadResult
4390ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004391 // Enter the submodule block.
4392 if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
4393 Error("malformed submodule block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00004394 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00004395 }
4396
4397 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
4398 bool First = true;
Craig Toppera13603a2014-05-22 05:54:18 +00004399 Module *CurrentModule = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00004400 RecordData Record;
4401 while (true) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004402 llvm::BitstreamEntry Entry = F.Stream.advanceSkippingSubblocks();
4403
4404 switch (Entry.Kind) {
4405 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
4406 case llvm::BitstreamEntry::Error:
4407 Error("malformed block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00004408 return Failure;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004409 case llvm::BitstreamEntry::EndBlock:
Ben Langmuir2c9af442014-04-10 17:57:43 +00004410 return Success;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004411 case llvm::BitstreamEntry::Record:
4412 // The interesting case.
4413 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004414 }
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004415
Guy Benyei11169dd2012-12-18 14:30:41 +00004416 // Read a record.
Chris Lattner0e6c9402013-01-20 02:38:54 +00004417 StringRef Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00004418 Record.clear();
Richard Smith03478d92014-10-23 22:12:14 +00004419 auto Kind = F.Stream.readRecord(Entry.ID, Record, &Blob);
4420
4421 if ((Kind == SUBMODULE_METADATA) != First) {
4422 Error("submodule metadata record should be at beginning of block");
4423 return Failure;
4424 }
4425 First = false;
4426
4427 // Submodule information is only valid if we have a current module.
4428 // FIXME: Should we error on these cases?
4429 if (!CurrentModule && Kind != SUBMODULE_METADATA &&
4430 Kind != SUBMODULE_DEFINITION)
4431 continue;
4432
4433 switch (Kind) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004434 default: // Default behavior: ignore.
4435 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004436
Richard Smith03478d92014-10-23 22:12:14 +00004437 case SUBMODULE_DEFINITION: {
Douglas Gregor8d932422013-03-20 03:59:18 +00004438 if (Record.size() < 8) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004439 Error("malformed module definition");
Ben Langmuir2c9af442014-04-10 17:57:43 +00004440 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00004441 }
Richard Smith03478d92014-10-23 22:12:14 +00004442
Chris Lattner0e6c9402013-01-20 02:38:54 +00004443 StringRef Name = Blob;
Richard Smith9bca2982014-03-08 00:03:56 +00004444 unsigned Idx = 0;
4445 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
4446 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
4447 bool IsFramework = Record[Idx++];
4448 bool IsExplicit = Record[Idx++];
4449 bool IsSystem = Record[Idx++];
4450 bool IsExternC = Record[Idx++];
4451 bool InferSubmodules = Record[Idx++];
4452 bool InferExplicitSubmodules = Record[Idx++];
4453 bool InferExportWildcard = Record[Idx++];
4454 bool ConfigMacrosExhaustive = Record[Idx++];
Douglas Gregor8d932422013-03-20 03:59:18 +00004455
Ben Langmuirbeee15e2014-04-14 18:00:01 +00004456 Module *ParentModule = nullptr;
Ben Langmuir9d6448b2014-08-09 00:57:23 +00004457 if (Parent)
Guy Benyei11169dd2012-12-18 14:30:41 +00004458 ParentModule = getSubmodule(Parent);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00004459
Guy Benyei11169dd2012-12-18 14:30:41 +00004460 // Retrieve this (sub)module from the module map, creating it if
4461 // necessary.
Ben Langmuir9d6448b2014-08-09 00:57:23 +00004462 CurrentModule = ModMap.findOrCreateModule(Name, ParentModule, IsFramework,
Guy Benyei11169dd2012-12-18 14:30:41 +00004463 IsExplicit).first;
Ben Langmuir9d6448b2014-08-09 00:57:23 +00004464
4465 // FIXME: set the definition loc for CurrentModule, or call
4466 // ModMap.setInferredModuleAllowedBy()
4467
Guy Benyei11169dd2012-12-18 14:30:41 +00004468 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
4469 if (GlobalIndex >= SubmodulesLoaded.size() ||
4470 SubmodulesLoaded[GlobalIndex]) {
4471 Error("too many submodules");
Ben Langmuir2c9af442014-04-10 17:57:43 +00004472 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00004473 }
Douglas Gregor8a114ab2013-02-06 22:40:31 +00004474
Douglas Gregor7029ce12013-03-19 00:28:20 +00004475 if (!ParentModule) {
4476 if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
4477 if (CurFile != F.File) {
4478 if (!Diags.isDiagnosticInFlight()) {
4479 Diag(diag::err_module_file_conflict)
4480 << CurrentModule->getTopLevelModuleName()
4481 << CurFile->getName()
4482 << F.File->getName();
4483 }
Ben Langmuir2c9af442014-04-10 17:57:43 +00004484 return Failure;
Douglas Gregor8a114ab2013-02-06 22:40:31 +00004485 }
Douglas Gregor8a114ab2013-02-06 22:40:31 +00004486 }
Douglas Gregor7029ce12013-03-19 00:28:20 +00004487
4488 CurrentModule->setASTFile(F.File);
Douglas Gregor8a114ab2013-02-06 22:40:31 +00004489 }
Ben Langmuirbeee15e2014-04-14 18:00:01 +00004490
Guy Benyei11169dd2012-12-18 14:30:41 +00004491 CurrentModule->IsFromModuleFile = true;
4492 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
Richard Smith9bca2982014-03-08 00:03:56 +00004493 CurrentModule->IsExternC = IsExternC;
Guy Benyei11169dd2012-12-18 14:30:41 +00004494 CurrentModule->InferSubmodules = InferSubmodules;
4495 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
4496 CurrentModule->InferExportWildcard = InferExportWildcard;
Douglas Gregor8d932422013-03-20 03:59:18 +00004497 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
Guy Benyei11169dd2012-12-18 14:30:41 +00004498 if (DeserializationListener)
4499 DeserializationListener->ModuleRead(GlobalID, CurrentModule);
4500
4501 SubmodulesLoaded[GlobalIndex] = CurrentModule;
Douglas Gregor6ddfca92013-01-14 17:21:00 +00004502
Douglas Gregorfb912652013-03-20 21:10:35 +00004503 // Clear out data that will be replaced by what is the module file.
Douglas Gregor6ddfca92013-01-14 17:21:00 +00004504 CurrentModule->LinkLibraries.clear();
Douglas Gregor8d932422013-03-20 03:59:18 +00004505 CurrentModule->ConfigMacros.clear();
Douglas Gregorfb912652013-03-20 21:10:35 +00004506 CurrentModule->UnresolvedConflicts.clear();
4507 CurrentModule->Conflicts.clear();
Guy Benyei11169dd2012-12-18 14:30:41 +00004508 break;
4509 }
4510
4511 case SUBMODULE_UMBRELLA_HEADER: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00004512 if (const FileEntry *Umbrella = PP.getFileManager().getFile(Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004513 if (!CurrentModule->getUmbrellaHeader())
4514 ModMap.setUmbrellaHeader(CurrentModule, Umbrella);
4515 else if (CurrentModule->getUmbrellaHeader() != Umbrella) {
Ben Langmuirbc35fbe2015-02-20 21:46:39 +00004516 // This can be a spurious difference caused by changing the VFS to
4517 // point to a different copy of the file, and it is too late to
4518 // to rebuild safely.
4519 // FIXME: If we wrote the virtual paths instead of the 'real' paths,
4520 // after input file validation only real problems would remain and we
4521 // could just error. For now, assume it's okay.
4522 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004523 }
4524 }
4525 break;
4526 }
4527
Richard Smith202210b2014-10-24 20:23:01 +00004528 case SUBMODULE_HEADER:
4529 case SUBMODULE_EXCLUDED_HEADER:
4530 case SUBMODULE_PRIVATE_HEADER:
4531 // We lazily associate headers with their modules via the HeaderInfo table.
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00004532 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
4533 // of complete filenames or remove it entirely.
Richard Smith202210b2014-10-24 20:23:01 +00004534 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004535
Richard Smith202210b2014-10-24 20:23:01 +00004536 case SUBMODULE_TEXTUAL_HEADER:
4537 case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
4538 // FIXME: Textual headers are not marked in the HeaderInfo table. Load
4539 // them here.
4540 break;
Lawrence Crowlb53e5482013-06-20 21:14:14 +00004541
Guy Benyei11169dd2012-12-18 14:30:41 +00004542 case SUBMODULE_TOPHEADER: {
Argyrios Kyrtzidis3c5305c2013-03-13 21:13:43 +00004543 CurrentModule->addTopHeaderFilename(Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +00004544 break;
4545 }
4546
4547 case SUBMODULE_UMBRELLA_DIR: {
Guy Benyei11169dd2012-12-18 14:30:41 +00004548 if (const DirectoryEntry *Umbrella
Chris Lattner0e6c9402013-01-20 02:38:54 +00004549 = PP.getFileManager().getDirectory(Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004550 if (!CurrentModule->getUmbrellaDir())
4551 ModMap.setUmbrellaDir(CurrentModule, Umbrella);
4552 else if (CurrentModule->getUmbrellaDir() != Umbrella) {
Ben Langmuir2c9af442014-04-10 17:57:43 +00004553 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
4554 Error("mismatched umbrella directories in submodule");
4555 return OutOfDate;
Guy Benyei11169dd2012-12-18 14:30:41 +00004556 }
4557 }
4558 break;
4559 }
4560
4561 case SUBMODULE_METADATA: {
Guy Benyei11169dd2012-12-18 14:30:41 +00004562 F.BaseSubmoduleID = getTotalNumSubmodules();
4563 F.LocalNumSubmodules = Record[0];
4564 unsigned LocalBaseSubmoduleID = Record[1];
4565 if (F.LocalNumSubmodules > 0) {
4566 // Introduce the global -> local mapping for submodules within this
4567 // module.
4568 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
4569
4570 // Introduce the local -> global mapping for submodules within this
4571 // module.
4572 F.SubmoduleRemap.insertOrReplace(
4573 std::make_pair(LocalBaseSubmoduleID,
4574 F.BaseSubmoduleID - LocalBaseSubmoduleID));
Ben Langmuirfe971d92014-08-16 04:54:18 +00004575
Ben Langmuir52ca6782014-10-20 16:27:32 +00004576 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
4577 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004578 break;
4579 }
4580
4581 case SUBMODULE_IMPORTS: {
Guy Benyei11169dd2012-12-18 14:30:41 +00004582 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
Douglas Gregorfb912652013-03-20 21:10:35 +00004583 UnresolvedModuleRef Unresolved;
Guy Benyei11169dd2012-12-18 14:30:41 +00004584 Unresolved.File = &F;
4585 Unresolved.Mod = CurrentModule;
4586 Unresolved.ID = Record[Idx];
Douglas Gregorfb912652013-03-20 21:10:35 +00004587 Unresolved.Kind = UnresolvedModuleRef::Import;
Guy Benyei11169dd2012-12-18 14:30:41 +00004588 Unresolved.IsWildcard = false;
Douglas Gregorfb912652013-03-20 21:10:35 +00004589 UnresolvedModuleRefs.push_back(Unresolved);
Guy Benyei11169dd2012-12-18 14:30:41 +00004590 }
4591 break;
4592 }
4593
4594 case SUBMODULE_EXPORTS: {
Guy Benyei11169dd2012-12-18 14:30:41 +00004595 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
Douglas Gregorfb912652013-03-20 21:10:35 +00004596 UnresolvedModuleRef Unresolved;
Guy Benyei11169dd2012-12-18 14:30:41 +00004597 Unresolved.File = &F;
4598 Unresolved.Mod = CurrentModule;
4599 Unresolved.ID = Record[Idx];
Douglas Gregorfb912652013-03-20 21:10:35 +00004600 Unresolved.Kind = UnresolvedModuleRef::Export;
Guy Benyei11169dd2012-12-18 14:30:41 +00004601 Unresolved.IsWildcard = Record[Idx + 1];
Douglas Gregorfb912652013-03-20 21:10:35 +00004602 UnresolvedModuleRefs.push_back(Unresolved);
Guy Benyei11169dd2012-12-18 14:30:41 +00004603 }
4604
4605 // Once we've loaded the set of exports, there's no reason to keep
4606 // the parsed, unresolved exports around.
4607 CurrentModule->UnresolvedExports.clear();
4608 break;
4609 }
4610 case SUBMODULE_REQUIRES: {
Richard Smitha3feee22013-10-28 22:18:19 +00004611 CurrentModule->addRequirement(Blob, Record[0], Context.getLangOpts(),
Guy Benyei11169dd2012-12-18 14:30:41 +00004612 Context.getTargetInfo());
4613 break;
4614 }
Douglas Gregor6ddfca92013-01-14 17:21:00 +00004615
4616 case SUBMODULE_LINK_LIBRARY:
Douglas Gregor6ddfca92013-01-14 17:21:00 +00004617 CurrentModule->LinkLibraries.push_back(
Chris Lattner0e6c9402013-01-20 02:38:54 +00004618 Module::LinkLibrary(Blob, Record[0]));
Douglas Gregor6ddfca92013-01-14 17:21:00 +00004619 break;
Douglas Gregor35b13ec2013-03-20 00:22:05 +00004620
4621 case SUBMODULE_CONFIG_MACRO:
Douglas Gregor35b13ec2013-03-20 00:22:05 +00004622 CurrentModule->ConfigMacros.push_back(Blob.str());
4623 break;
Douglas Gregorfb912652013-03-20 21:10:35 +00004624
4625 case SUBMODULE_CONFLICT: {
Douglas Gregorfb912652013-03-20 21:10:35 +00004626 UnresolvedModuleRef Unresolved;
4627 Unresolved.File = &F;
4628 Unresolved.Mod = CurrentModule;
4629 Unresolved.ID = Record[0];
4630 Unresolved.Kind = UnresolvedModuleRef::Conflict;
4631 Unresolved.IsWildcard = false;
4632 Unresolved.String = Blob;
4633 UnresolvedModuleRefs.push_back(Unresolved);
4634 break;
4635 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004636 }
4637 }
4638}
4639
4640/// \brief Parse the record that corresponds to a LangOptions data
4641/// structure.
4642///
4643/// This routine parses the language options from the AST file and then gives
4644/// them to the AST listener if one is set.
4645///
4646/// \returns true if the listener deems the file unacceptable, false otherwise.
4647bool ASTReader::ParseLanguageOptions(const RecordData &Record,
4648 bool Complain,
Richard Smith1e2cf0d2014-10-31 02:28:58 +00004649 ASTReaderListener &Listener,
4650 bool AllowCompatibleDifferences) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004651 LangOptions LangOpts;
4652 unsigned Idx = 0;
4653#define LANGOPT(Name, Bits, Default, Description) \
4654 LangOpts.Name = Record[Idx++];
4655#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
4656 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
4657#include "clang/Basic/LangOptions.def"
Alexey Samsonovedf99a92014-11-07 22:29:38 +00004658#define SANITIZER(NAME, ID) \
4659 LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
Will Dietzf54319c2013-01-18 11:30:38 +00004660#include "clang/Basic/Sanitizers.def"
Guy Benyei11169dd2012-12-18 14:30:41 +00004661
4662 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
4663 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
4664 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
4665
4666 unsigned Length = Record[Idx++];
4667 LangOpts.CurrentModule.assign(Record.begin() + Idx,
4668 Record.begin() + Idx + Length);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00004669
4670 Idx += Length;
4671
4672 // Comment options.
4673 for (unsigned N = Record[Idx++]; N; --N) {
4674 LangOpts.CommentOpts.BlockCommandNames.push_back(
4675 ReadString(Record, Idx));
4676 }
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00004677 LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00004678
Richard Smith1e2cf0d2014-10-31 02:28:58 +00004679 return Listener.ReadLanguageOptions(LangOpts, Complain,
4680 AllowCompatibleDifferences);
Guy Benyei11169dd2012-12-18 14:30:41 +00004681}
4682
Chandler Carruth0d745bc2015-03-14 04:47:43 +00004683bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain,
4684 ASTReaderListener &Listener,
4685 bool AllowCompatibleDifferences) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004686 unsigned Idx = 0;
4687 TargetOptions TargetOpts;
4688 TargetOpts.Triple = ReadString(Record, Idx);
4689 TargetOpts.CPU = ReadString(Record, Idx);
4690 TargetOpts.ABI = ReadString(Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00004691 for (unsigned N = Record[Idx++]; N; --N) {
4692 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
4693 }
4694 for (unsigned N = Record[Idx++]; N; --N) {
4695 TargetOpts.Features.push_back(ReadString(Record, Idx));
4696 }
4697
Chandler Carruth0d745bc2015-03-14 04:47:43 +00004698 return Listener.ReadTargetOptions(TargetOpts, Complain,
4699 AllowCompatibleDifferences);
Guy Benyei11169dd2012-12-18 14:30:41 +00004700}
4701
4702bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
4703 ASTReaderListener &Listener) {
Ben Langmuirb92de022014-04-29 16:25:26 +00004704 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions);
Guy Benyei11169dd2012-12-18 14:30:41 +00004705 unsigned Idx = 0;
Ben Langmuirb92de022014-04-29 16:25:26 +00004706#define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00004707#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
Ben Langmuirb92de022014-04-29 16:25:26 +00004708 DiagOpts->set##Name(static_cast<Type>(Record[Idx++]));
Guy Benyei11169dd2012-12-18 14:30:41 +00004709#include "clang/Basic/DiagnosticOptions.def"
4710
Richard Smith3be1cb22014-08-07 00:24:21 +00004711 for (unsigned N = Record[Idx++]; N; --N)
Ben Langmuirb92de022014-04-29 16:25:26 +00004712 DiagOpts->Warnings.push_back(ReadString(Record, Idx));
Richard Smith3be1cb22014-08-07 00:24:21 +00004713 for (unsigned N = Record[Idx++]; N; --N)
4714 DiagOpts->Remarks.push_back(ReadString(Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00004715
4716 return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
4717}
4718
4719bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
4720 ASTReaderListener &Listener) {
4721 FileSystemOptions FSOpts;
4722 unsigned Idx = 0;
4723 FSOpts.WorkingDir = ReadString(Record, Idx);
4724 return Listener.ReadFileSystemOptions(FSOpts, Complain);
4725}
4726
4727bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
4728 bool Complain,
4729 ASTReaderListener &Listener) {
4730 HeaderSearchOptions HSOpts;
4731 unsigned Idx = 0;
4732 HSOpts.Sysroot = ReadString(Record, Idx);
4733
4734 // Include entries.
4735 for (unsigned N = Record[Idx++]; N; --N) {
4736 std::string Path = ReadString(Record, Idx);
4737 frontend::IncludeDirGroup Group
4738 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
Guy Benyei11169dd2012-12-18 14:30:41 +00004739 bool IsFramework = Record[Idx++];
4740 bool IgnoreSysRoot = Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00004741 HSOpts.UserEntries.push_back(
Daniel Dunbar53681732013-01-30 00:34:26 +00004742 HeaderSearchOptions::Entry(Path, Group, IsFramework, IgnoreSysRoot));
Guy Benyei11169dd2012-12-18 14:30:41 +00004743 }
4744
4745 // System header prefixes.
4746 for (unsigned N = Record[Idx++]; N; --N) {
4747 std::string Prefix = ReadString(Record, Idx);
4748 bool IsSystemHeader = Record[Idx++];
4749 HSOpts.SystemHeaderPrefixes.push_back(
4750 HeaderSearchOptions::SystemHeaderPrefix(Prefix, IsSystemHeader));
4751 }
4752
4753 HSOpts.ResourceDir = ReadString(Record, Idx);
4754 HSOpts.ModuleCachePath = ReadString(Record, Idx);
Argyrios Kyrtzidis1594c152014-03-03 08:12:05 +00004755 HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00004756 HSOpts.DisableModuleHash = Record[Idx++];
4757 HSOpts.UseBuiltinIncludes = Record[Idx++];
4758 HSOpts.UseStandardSystemIncludes = Record[Idx++];
4759 HSOpts.UseStandardCXXIncludes = Record[Idx++];
4760 HSOpts.UseLibcxx = Record[Idx++];
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00004761 std::string SpecificModuleCachePath = ReadString(Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00004762
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00004763 return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
4764 Complain);
Guy Benyei11169dd2012-12-18 14:30:41 +00004765}
4766
4767bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
4768 bool Complain,
4769 ASTReaderListener &Listener,
4770 std::string &SuggestedPredefines) {
4771 PreprocessorOptions PPOpts;
4772 unsigned Idx = 0;
4773
4774 // Macro definitions/undefs
4775 for (unsigned N = Record[Idx++]; N; --N) {
4776 std::string Macro = ReadString(Record, Idx);
4777 bool IsUndef = Record[Idx++];
4778 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
4779 }
4780
4781 // Includes
4782 for (unsigned N = Record[Idx++]; N; --N) {
4783 PPOpts.Includes.push_back(ReadString(Record, Idx));
4784 }
4785
4786 // Macro Includes
4787 for (unsigned N = Record[Idx++]; N; --N) {
4788 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
4789 }
4790
4791 PPOpts.UsePredefines = Record[Idx++];
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00004792 PPOpts.DetailedRecord = Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00004793 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
4794 PPOpts.ImplicitPTHInclude = ReadString(Record, Idx);
4795 PPOpts.ObjCXXARCStandardLibrary =
4796 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
4797 SuggestedPredefines.clear();
4798 return Listener.ReadPreprocessorOptions(PPOpts, Complain,
4799 SuggestedPredefines);
4800}
4801
4802std::pair<ModuleFile *, unsigned>
4803ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
4804 GlobalPreprocessedEntityMapType::iterator
4805 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
4806 assert(I != GlobalPreprocessedEntityMap.end() &&
4807 "Corrupted global preprocessed entity map");
4808 ModuleFile *M = I->second;
4809 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
4810 return std::make_pair(M, LocalIndex);
4811}
4812
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00004813llvm::iterator_range<PreprocessingRecord::iterator>
Guy Benyei11169dd2012-12-18 14:30:41 +00004814ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
4815 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
4816 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
4817 Mod.NumPreprocessedEntities);
4818
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00004819 return llvm::make_range(PreprocessingRecord::iterator(),
4820 PreprocessingRecord::iterator());
Guy Benyei11169dd2012-12-18 14:30:41 +00004821}
4822
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00004823llvm::iterator_range<ASTReader::ModuleDeclIterator>
Guy Benyei11169dd2012-12-18 14:30:41 +00004824ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00004825 return llvm::make_range(
4826 ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
4827 ModuleDeclIterator(this, &Mod,
4828 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
Guy Benyei11169dd2012-12-18 14:30:41 +00004829}
4830
4831PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
4832 PreprocessedEntityID PPID = Index+1;
4833 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
4834 ModuleFile &M = *PPInfo.first;
4835 unsigned LocalIndex = PPInfo.second;
4836 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
4837
Guy Benyei11169dd2012-12-18 14:30:41 +00004838 if (!PP.getPreprocessingRecord()) {
4839 Error("no preprocessing record");
Craig Toppera13603a2014-05-22 05:54:18 +00004840 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00004841 }
4842
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004843 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
4844 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
4845
4846 llvm::BitstreamEntry Entry =
4847 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
4848 if (Entry.Kind != llvm::BitstreamEntry::Record)
Craig Toppera13603a2014-05-22 05:54:18 +00004849 return nullptr;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004850
Guy Benyei11169dd2012-12-18 14:30:41 +00004851 // Read the record.
4852 SourceRange Range(ReadSourceLocation(M, PPOffs.Begin),
4853 ReadSourceLocation(M, PPOffs.End));
4854 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
Chris Lattner0e6c9402013-01-20 02:38:54 +00004855 StringRef Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00004856 RecordData Record;
4857 PreprocessorDetailRecordTypes RecType =
Chris Lattner0e6c9402013-01-20 02:38:54 +00004858 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.readRecord(
4859 Entry.ID, Record, &Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +00004860 switch (RecType) {
4861 case PPD_MACRO_EXPANSION: {
4862 bool isBuiltin = Record[0];
Craig Toppera13603a2014-05-22 05:54:18 +00004863 IdentifierInfo *Name = nullptr;
4864 MacroDefinition *Def = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00004865 if (isBuiltin)
4866 Name = getLocalIdentifier(M, Record[1]);
4867 else {
4868 PreprocessedEntityID
4869 GlobalID = getGlobalPreprocessedEntityID(M, Record[1]);
4870 Def =cast<MacroDefinition>(PPRec.getLoadedPreprocessedEntity(GlobalID-1));
4871 }
4872
4873 MacroExpansion *ME;
4874 if (isBuiltin)
4875 ME = new (PPRec) MacroExpansion(Name, Range);
4876 else
4877 ME = new (PPRec) MacroExpansion(Def, Range);
4878
4879 return ME;
4880 }
4881
4882 case PPD_MACRO_DEFINITION: {
4883 // Decode the identifier info and then check again; if the macro is
4884 // still defined and associated with the identifier,
4885 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
4886 MacroDefinition *MD
4887 = new (PPRec) MacroDefinition(II, Range);
4888
4889 if (DeserializationListener)
4890 DeserializationListener->MacroDefinitionRead(PPID, MD);
4891
4892 return MD;
4893 }
4894
4895 case PPD_INCLUSION_DIRECTIVE: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00004896 const char *FullFileNameStart = Blob.data() + Record[0];
4897 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
Craig Toppera13603a2014-05-22 05:54:18 +00004898 const FileEntry *File = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00004899 if (!FullFileName.empty())
4900 File = PP.getFileManager().getFile(FullFileName);
4901
4902 // FIXME: Stable encoding
4903 InclusionDirective::InclusionKind Kind
4904 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
4905 InclusionDirective *ID
4906 = new (PPRec) InclusionDirective(PPRec, Kind,
Chris Lattner0e6c9402013-01-20 02:38:54 +00004907 StringRef(Blob.data(), Record[0]),
Guy Benyei11169dd2012-12-18 14:30:41 +00004908 Record[1], Record[3],
4909 File,
4910 Range);
4911 return ID;
4912 }
4913 }
4914
4915 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
4916}
4917
4918/// \brief \arg SLocMapI points at a chunk of a module that contains no
4919/// preprocessed entities or the entities it contains are not the ones we are
4920/// looking for. Find the next module that contains entities and return the ID
4921/// of the first entry.
4922PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
4923 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
4924 ++SLocMapI;
4925 for (GlobalSLocOffsetMapType::const_iterator
4926 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
4927 ModuleFile &M = *SLocMapI->second;
4928 if (M.NumPreprocessedEntities)
4929 return M.BasePreprocessedEntityID;
4930 }
4931
4932 return getTotalNumPreprocessedEntities();
4933}
4934
4935namespace {
4936
4937template <unsigned PPEntityOffset::*PPLoc>
4938struct PPEntityComp {
4939 const ASTReader &Reader;
4940 ModuleFile &M;
4941
4942 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) { }
4943
4944 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
4945 SourceLocation LHS = getLoc(L);
4946 SourceLocation RHS = getLoc(R);
4947 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4948 }
4949
4950 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
4951 SourceLocation LHS = getLoc(L);
4952 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4953 }
4954
4955 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
4956 SourceLocation RHS = getLoc(R);
4957 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4958 }
4959
4960 SourceLocation getLoc(const PPEntityOffset &PPE) const {
4961 return Reader.ReadSourceLocation(M, PPE.*PPLoc);
4962 }
4963};
4964
4965}
4966
Alp Toker2e9ce4c2014-05-16 18:59:21 +00004967PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
4968 bool EndsAfter) const {
4969 if (SourceMgr.isLocalSourceLocation(Loc))
Guy Benyei11169dd2012-12-18 14:30:41 +00004970 return getTotalNumPreprocessedEntities();
4971
Alp Toker2e9ce4c2014-05-16 18:59:21 +00004972 GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
4973 SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
Guy Benyei11169dd2012-12-18 14:30:41 +00004974 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
4975 "Corrupted global sloc offset map");
4976
4977 if (SLocMapI->second->NumPreprocessedEntities == 0)
4978 return findNextPreprocessedEntity(SLocMapI);
4979
4980 ModuleFile &M = *SLocMapI->second;
4981 typedef const PPEntityOffset *pp_iterator;
4982 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
4983 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
4984
4985 size_t Count = M.NumPreprocessedEntities;
4986 size_t Half;
4987 pp_iterator First = pp_begin;
4988 pp_iterator PPI;
4989
Alp Toker2e9ce4c2014-05-16 18:59:21 +00004990 if (EndsAfter) {
4991 PPI = std::upper_bound(pp_begin, pp_end, Loc,
4992 PPEntityComp<&PPEntityOffset::Begin>(*this, M));
4993 } else {
4994 // Do a binary search manually instead of using std::lower_bound because
4995 // The end locations of entities may be unordered (when a macro expansion
4996 // is inside another macro argument), but for this case it is not important
4997 // whether we get the first macro expansion or its containing macro.
4998 while (Count > 0) {
4999 Half = Count / 2;
5000 PPI = First;
5001 std::advance(PPI, Half);
5002 if (SourceMgr.isBeforeInTranslationUnit(ReadSourceLocation(M, PPI->End),
5003 Loc)) {
5004 First = PPI;
5005 ++First;
5006 Count = Count - Half - 1;
5007 } else
5008 Count = Half;
5009 }
Guy Benyei11169dd2012-12-18 14:30:41 +00005010 }
5011
5012 if (PPI == pp_end)
5013 return findNextPreprocessedEntity(SLocMapI);
5014
5015 return M.BasePreprocessedEntityID + (PPI - pp_begin);
5016}
5017
Guy Benyei11169dd2012-12-18 14:30:41 +00005018/// \brief Returns a pair of [Begin, End) indices of preallocated
5019/// preprocessed entities that \arg Range encompasses.
5020std::pair<unsigned, unsigned>
5021 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
5022 if (Range.isInvalid())
5023 return std::make_pair(0,0);
5024 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
5025
Alp Toker2e9ce4c2014-05-16 18:59:21 +00005026 PreprocessedEntityID BeginID =
5027 findPreprocessedEntity(Range.getBegin(), false);
5028 PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true);
Guy Benyei11169dd2012-12-18 14:30:41 +00005029 return std::make_pair(BeginID, EndID);
5030}
5031
5032/// \brief Optionally returns true or false if the preallocated preprocessed
5033/// entity with index \arg Index came from file \arg FID.
David Blaikie05785d12013-02-20 22:23:23 +00005034Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
Guy Benyei11169dd2012-12-18 14:30:41 +00005035 FileID FID) {
5036 if (FID.isInvalid())
5037 return false;
5038
5039 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
5040 ModuleFile &M = *PPInfo.first;
5041 unsigned LocalIndex = PPInfo.second;
5042 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
5043
5044 SourceLocation Loc = ReadSourceLocation(M, PPOffs.Begin);
5045 if (Loc.isInvalid())
5046 return false;
5047
5048 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
5049 return true;
5050 else
5051 return false;
5052}
5053
5054namespace {
5055 /// \brief Visitor used to search for information about a header file.
5056 class HeaderFileInfoVisitor {
Guy Benyei11169dd2012-12-18 14:30:41 +00005057 const FileEntry *FE;
5058
David Blaikie05785d12013-02-20 22:23:23 +00005059 Optional<HeaderFileInfo> HFI;
Guy Benyei11169dd2012-12-18 14:30:41 +00005060
5061 public:
Argyrios Kyrtzidis61a38962013-03-06 18:12:44 +00005062 explicit HeaderFileInfoVisitor(const FileEntry *FE)
5063 : FE(FE) { }
Guy Benyei11169dd2012-12-18 14:30:41 +00005064
5065 static bool visit(ModuleFile &M, void *UserData) {
5066 HeaderFileInfoVisitor *This
5067 = static_cast<HeaderFileInfoVisitor *>(UserData);
5068
Guy Benyei11169dd2012-12-18 14:30:41 +00005069 HeaderFileInfoLookupTable *Table
5070 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
5071 if (!Table)
5072 return false;
5073
5074 // Look in the on-disk hash table for an entry for this file name.
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00005075 HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE);
Guy Benyei11169dd2012-12-18 14:30:41 +00005076 if (Pos == Table->end())
5077 return false;
5078
5079 This->HFI = *Pos;
5080 return true;
5081 }
5082
David Blaikie05785d12013-02-20 22:23:23 +00005083 Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
Guy Benyei11169dd2012-12-18 14:30:41 +00005084 };
5085}
5086
5087HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
Argyrios Kyrtzidis61a38962013-03-06 18:12:44 +00005088 HeaderFileInfoVisitor Visitor(FE);
Guy Benyei11169dd2012-12-18 14:30:41 +00005089 ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor);
Argyrios Kyrtzidis1054bbf2013-05-08 23:46:55 +00005090 if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
Guy Benyei11169dd2012-12-18 14:30:41 +00005091 return *HFI;
Guy Benyei11169dd2012-12-18 14:30:41 +00005092
5093 return HeaderFileInfo();
5094}
5095
5096void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
5097 // FIXME: Make it work properly with modules.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00005098 SmallVector<DiagnosticsEngine::DiagState *, 32> DiagStates;
Guy Benyei11169dd2012-12-18 14:30:41 +00005099 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
5100 ModuleFile &F = *(*I);
5101 unsigned Idx = 0;
5102 DiagStates.clear();
5103 assert(!Diag.DiagStates.empty());
5104 DiagStates.push_back(&Diag.DiagStates.front()); // the command-line one.
5105 while (Idx < F.PragmaDiagMappings.size()) {
5106 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
5107 unsigned DiagStateID = F.PragmaDiagMappings[Idx++];
5108 if (DiagStateID != 0) {
5109 Diag.DiagStatePoints.push_back(
5110 DiagnosticsEngine::DiagStatePoint(DiagStates[DiagStateID-1],
5111 FullSourceLoc(Loc, SourceMgr)));
5112 continue;
5113 }
5114
5115 assert(DiagStateID == 0);
5116 // A new DiagState was created here.
5117 Diag.DiagStates.push_back(*Diag.GetCurDiagState());
5118 DiagnosticsEngine::DiagState *NewState = &Diag.DiagStates.back();
5119 DiagStates.push_back(NewState);
5120 Diag.DiagStatePoints.push_back(
5121 DiagnosticsEngine::DiagStatePoint(NewState,
5122 FullSourceLoc(Loc, SourceMgr)));
5123 while (1) {
5124 assert(Idx < F.PragmaDiagMappings.size() &&
5125 "Invalid data, didn't find '-1' marking end of diag/map pairs");
5126 if (Idx >= F.PragmaDiagMappings.size()) {
5127 break; // Something is messed up but at least avoid infinite loop in
5128 // release build.
5129 }
5130 unsigned DiagID = F.PragmaDiagMappings[Idx++];
5131 if (DiagID == (unsigned)-1) {
5132 break; // no more diag/map pairs for this location.
5133 }
Alp Tokerc726c362014-06-10 09:31:37 +00005134 diag::Severity Map = (diag::Severity)F.PragmaDiagMappings[Idx++];
5135 DiagnosticMapping Mapping = Diag.makeUserMapping(Map, Loc);
5136 Diag.GetCurDiagState()->setMapping(DiagID, Mapping);
Guy Benyei11169dd2012-12-18 14:30:41 +00005137 }
5138 }
5139 }
5140}
5141
5142/// \brief Get the correct cursor and offset for loading a type.
5143ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
5144 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
5145 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
5146 ModuleFile *M = I->second;
5147 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
5148}
5149
5150/// \brief Read and return the type with the given index..
5151///
5152/// The index is the type ID, shifted and minus the number of predefs. This
5153/// routine actually reads the record corresponding to the type at the given
5154/// location. It is a helper routine for GetType, which deals with reading type
5155/// IDs.
5156QualType ASTReader::readTypeRecord(unsigned Index) {
5157 RecordLocation Loc = TypeCursorForIndex(Index);
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005158 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00005159
5160 // Keep track of where we are in the stream, then jump back there
5161 // after reading this type.
5162 SavedStreamPosition SavedPosition(DeclsCursor);
5163
5164 ReadingKindTracker ReadingKind(Read_Type, *this);
5165
5166 // Note that we are loading a type record.
5167 Deserializing AType(this);
5168
5169 unsigned Idx = 0;
5170 DeclsCursor.JumpToBit(Loc.Offset);
5171 RecordData Record;
5172 unsigned Code = DeclsCursor.ReadCode();
Chris Lattner0e6c9402013-01-20 02:38:54 +00005173 switch ((TypeCode)DeclsCursor.readRecord(Code, Record)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00005174 case TYPE_EXT_QUAL: {
5175 if (Record.size() != 2) {
5176 Error("Incorrect encoding of extended qualifier type");
5177 return QualType();
5178 }
5179 QualType Base = readType(*Loc.F, Record, Idx);
5180 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
5181 return Context.getQualifiedType(Base, Quals);
5182 }
5183
5184 case TYPE_COMPLEX: {
5185 if (Record.size() != 1) {
5186 Error("Incorrect encoding of complex type");
5187 return QualType();
5188 }
5189 QualType ElemType = readType(*Loc.F, Record, Idx);
5190 return Context.getComplexType(ElemType);
5191 }
5192
5193 case TYPE_POINTER: {
5194 if (Record.size() != 1) {
5195 Error("Incorrect encoding of pointer type");
5196 return QualType();
5197 }
5198 QualType PointeeType = readType(*Loc.F, Record, Idx);
5199 return Context.getPointerType(PointeeType);
5200 }
5201
Reid Kleckner8a365022013-06-24 17:51:48 +00005202 case TYPE_DECAYED: {
5203 if (Record.size() != 1) {
5204 Error("Incorrect encoding of decayed type");
5205 return QualType();
5206 }
5207 QualType OriginalType = readType(*Loc.F, Record, Idx);
5208 QualType DT = Context.getAdjustedParameterType(OriginalType);
5209 if (!isa<DecayedType>(DT))
5210 Error("Decayed type does not decay");
5211 return DT;
5212 }
5213
Reid Kleckner0503a872013-12-05 01:23:43 +00005214 case TYPE_ADJUSTED: {
5215 if (Record.size() != 2) {
5216 Error("Incorrect encoding of adjusted type");
5217 return QualType();
5218 }
5219 QualType OriginalTy = readType(*Loc.F, Record, Idx);
5220 QualType AdjustedTy = readType(*Loc.F, Record, Idx);
5221 return Context.getAdjustedType(OriginalTy, AdjustedTy);
5222 }
5223
Guy Benyei11169dd2012-12-18 14:30:41 +00005224 case TYPE_BLOCK_POINTER: {
5225 if (Record.size() != 1) {
5226 Error("Incorrect encoding of block pointer type");
5227 return QualType();
5228 }
5229 QualType PointeeType = readType(*Loc.F, Record, Idx);
5230 return Context.getBlockPointerType(PointeeType);
5231 }
5232
5233 case TYPE_LVALUE_REFERENCE: {
5234 if (Record.size() != 2) {
5235 Error("Incorrect encoding of lvalue reference type");
5236 return QualType();
5237 }
5238 QualType PointeeType = readType(*Loc.F, Record, Idx);
5239 return Context.getLValueReferenceType(PointeeType, Record[1]);
5240 }
5241
5242 case TYPE_RVALUE_REFERENCE: {
5243 if (Record.size() != 1) {
5244 Error("Incorrect encoding of rvalue reference type");
5245 return QualType();
5246 }
5247 QualType PointeeType = readType(*Loc.F, Record, Idx);
5248 return Context.getRValueReferenceType(PointeeType);
5249 }
5250
5251 case TYPE_MEMBER_POINTER: {
5252 if (Record.size() != 2) {
5253 Error("Incorrect encoding of member pointer type");
5254 return QualType();
5255 }
5256 QualType PointeeType = readType(*Loc.F, Record, Idx);
5257 QualType ClassType = readType(*Loc.F, Record, Idx);
5258 if (PointeeType.isNull() || ClassType.isNull())
5259 return QualType();
5260
5261 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
5262 }
5263
5264 case TYPE_CONSTANT_ARRAY: {
5265 QualType ElementType = readType(*Loc.F, Record, Idx);
5266 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
5267 unsigned IndexTypeQuals = Record[2];
5268 unsigned Idx = 3;
5269 llvm::APInt Size = ReadAPInt(Record, Idx);
5270 return Context.getConstantArrayType(ElementType, Size,
5271 ASM, IndexTypeQuals);
5272 }
5273
5274 case TYPE_INCOMPLETE_ARRAY: {
5275 QualType ElementType = readType(*Loc.F, Record, Idx);
5276 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
5277 unsigned IndexTypeQuals = Record[2];
5278 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
5279 }
5280
5281 case TYPE_VARIABLE_ARRAY: {
5282 QualType ElementType = readType(*Loc.F, Record, Idx);
5283 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
5284 unsigned IndexTypeQuals = Record[2];
5285 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
5286 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
5287 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
5288 ASM, IndexTypeQuals,
5289 SourceRange(LBLoc, RBLoc));
5290 }
5291
5292 case TYPE_VECTOR: {
5293 if (Record.size() != 3) {
5294 Error("incorrect encoding of vector type in AST file");
5295 return QualType();
5296 }
5297
5298 QualType ElementType = readType(*Loc.F, Record, Idx);
5299 unsigned NumElements = Record[1];
5300 unsigned VecKind = Record[2];
5301 return Context.getVectorType(ElementType, NumElements,
5302 (VectorType::VectorKind)VecKind);
5303 }
5304
5305 case TYPE_EXT_VECTOR: {
5306 if (Record.size() != 3) {
5307 Error("incorrect encoding of extended vector type in AST file");
5308 return QualType();
5309 }
5310
5311 QualType ElementType = readType(*Loc.F, Record, Idx);
5312 unsigned NumElements = Record[1];
5313 return Context.getExtVectorType(ElementType, NumElements);
5314 }
5315
5316 case TYPE_FUNCTION_NO_PROTO: {
5317 if (Record.size() != 6) {
5318 Error("incorrect encoding of no-proto function type");
5319 return QualType();
5320 }
5321 QualType ResultType = readType(*Loc.F, Record, Idx);
5322 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
5323 (CallingConv)Record[4], Record[5]);
5324 return Context.getFunctionNoProtoType(ResultType, Info);
5325 }
5326
5327 case TYPE_FUNCTION_PROTO: {
5328 QualType ResultType = readType(*Loc.F, Record, Idx);
5329
5330 FunctionProtoType::ExtProtoInfo EPI;
5331 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
5332 /*hasregparm*/ Record[2],
5333 /*regparm*/ Record[3],
5334 static_cast<CallingConv>(Record[4]),
5335 /*produces*/ Record[5]);
5336
5337 unsigned Idx = 6;
Guy Benyei11169dd2012-12-18 14:30:41 +00005338
5339 EPI.Variadic = Record[Idx++];
5340 EPI.HasTrailingReturn = Record[Idx++];
5341 EPI.TypeQuals = Record[Idx++];
5342 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
Richard Smith564417a2014-03-20 21:47:22 +00005343 SmallVector<QualType, 8> ExceptionStorage;
Richard Smith8acb4282014-07-31 21:57:55 +00005344 readExceptionSpec(*Loc.F, ExceptionStorage, EPI.ExceptionSpec, Record, Idx);
Richard Smith01b2cb42014-07-26 06:37:51 +00005345
5346 unsigned NumParams = Record[Idx++];
5347 SmallVector<QualType, 16> ParamTypes;
5348 for (unsigned I = 0; I != NumParams; ++I)
5349 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
5350
Jordan Rose5c382722013-03-08 21:51:21 +00005351 return Context.getFunctionType(ResultType, ParamTypes, EPI);
Guy Benyei11169dd2012-12-18 14:30:41 +00005352 }
5353
5354 case TYPE_UNRESOLVED_USING: {
5355 unsigned Idx = 0;
5356 return Context.getTypeDeclType(
5357 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
5358 }
5359
5360 case TYPE_TYPEDEF: {
5361 if (Record.size() != 2) {
5362 Error("incorrect encoding of typedef type");
5363 return QualType();
5364 }
5365 unsigned Idx = 0;
5366 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
5367 QualType Canonical = readType(*Loc.F, Record, Idx);
5368 if (!Canonical.isNull())
5369 Canonical = Context.getCanonicalType(Canonical);
5370 return Context.getTypedefType(Decl, Canonical);
5371 }
5372
5373 case TYPE_TYPEOF_EXPR:
5374 return Context.getTypeOfExprType(ReadExpr(*Loc.F));
5375
5376 case TYPE_TYPEOF: {
5377 if (Record.size() != 1) {
5378 Error("incorrect encoding of typeof(type) in AST file");
5379 return QualType();
5380 }
5381 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
5382 return Context.getTypeOfType(UnderlyingType);
5383 }
5384
5385 case TYPE_DECLTYPE: {
5386 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
5387 return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
5388 }
5389
5390 case TYPE_UNARY_TRANSFORM: {
5391 QualType BaseType = readType(*Loc.F, Record, Idx);
5392 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
5393 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
5394 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
5395 }
5396
Richard Smith74aeef52013-04-26 16:15:35 +00005397 case TYPE_AUTO: {
5398 QualType Deduced = readType(*Loc.F, Record, Idx);
5399 bool IsDecltypeAuto = Record[Idx++];
Richard Smith27d807c2013-04-30 13:56:41 +00005400 bool IsDependent = Deduced.isNull() ? Record[Idx++] : false;
Manuel Klimek2fdbea22013-08-22 12:12:24 +00005401 return Context.getAutoType(Deduced, IsDecltypeAuto, IsDependent);
Richard Smith74aeef52013-04-26 16:15:35 +00005402 }
Guy Benyei11169dd2012-12-18 14:30:41 +00005403
5404 case TYPE_RECORD: {
5405 if (Record.size() != 2) {
5406 Error("incorrect encoding of record type");
5407 return QualType();
5408 }
5409 unsigned Idx = 0;
5410 bool IsDependent = Record[Idx++];
5411 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
5412 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
5413 QualType T = Context.getRecordType(RD);
5414 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
5415 return T;
5416 }
5417
5418 case TYPE_ENUM: {
5419 if (Record.size() != 2) {
5420 Error("incorrect encoding of enum type");
5421 return QualType();
5422 }
5423 unsigned Idx = 0;
5424 bool IsDependent = Record[Idx++];
5425 QualType T
5426 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
5427 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
5428 return T;
5429 }
5430
5431 case TYPE_ATTRIBUTED: {
5432 if (Record.size() != 3) {
5433 Error("incorrect encoding of attributed type");
5434 return QualType();
5435 }
5436 QualType modifiedType = readType(*Loc.F, Record, Idx);
5437 QualType equivalentType = readType(*Loc.F, Record, Idx);
5438 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
5439 return Context.getAttributedType(kind, modifiedType, equivalentType);
5440 }
5441
5442 case TYPE_PAREN: {
5443 if (Record.size() != 1) {
5444 Error("incorrect encoding of paren type");
5445 return QualType();
5446 }
5447 QualType InnerType = readType(*Loc.F, Record, Idx);
5448 return Context.getParenType(InnerType);
5449 }
5450
5451 case TYPE_PACK_EXPANSION: {
5452 if (Record.size() != 2) {
5453 Error("incorrect encoding of pack expansion type");
5454 return QualType();
5455 }
5456 QualType Pattern = readType(*Loc.F, Record, Idx);
5457 if (Pattern.isNull())
5458 return QualType();
David Blaikie05785d12013-02-20 22:23:23 +00005459 Optional<unsigned> NumExpansions;
Guy Benyei11169dd2012-12-18 14:30:41 +00005460 if (Record[1])
5461 NumExpansions = Record[1] - 1;
5462 return Context.getPackExpansionType(Pattern, NumExpansions);
5463 }
5464
5465 case TYPE_ELABORATED: {
5466 unsigned Idx = 0;
5467 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
5468 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
5469 QualType NamedType = readType(*Loc.F, Record, Idx);
5470 return Context.getElaboratedType(Keyword, NNS, NamedType);
5471 }
5472
5473 case TYPE_OBJC_INTERFACE: {
5474 unsigned Idx = 0;
5475 ObjCInterfaceDecl *ItfD
5476 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
5477 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
5478 }
5479
5480 case TYPE_OBJC_OBJECT: {
5481 unsigned Idx = 0;
5482 QualType Base = readType(*Loc.F, Record, Idx);
5483 unsigned NumProtos = Record[Idx++];
5484 SmallVector<ObjCProtocolDecl*, 4> Protos;
5485 for (unsigned I = 0; I != NumProtos; ++I)
5486 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
5487 return Context.getObjCObjectType(Base, Protos.data(), NumProtos);
5488 }
5489
5490 case TYPE_OBJC_OBJECT_POINTER: {
5491 unsigned Idx = 0;
5492 QualType Pointee = readType(*Loc.F, Record, Idx);
5493 return Context.getObjCObjectPointerType(Pointee);
5494 }
5495
5496 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
5497 unsigned Idx = 0;
5498 QualType Parm = readType(*Loc.F, Record, Idx);
5499 QualType Replacement = readType(*Loc.F, Record, Idx);
Stephan Tolksdorfe96f8b32014-03-15 10:23:27 +00005500 return Context.getSubstTemplateTypeParmType(
5501 cast<TemplateTypeParmType>(Parm),
5502 Context.getCanonicalType(Replacement));
Guy Benyei11169dd2012-12-18 14:30:41 +00005503 }
5504
5505 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
5506 unsigned Idx = 0;
5507 QualType Parm = readType(*Loc.F, Record, Idx);
5508 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
5509 return Context.getSubstTemplateTypeParmPackType(
5510 cast<TemplateTypeParmType>(Parm),
5511 ArgPack);
5512 }
5513
5514 case TYPE_INJECTED_CLASS_NAME: {
5515 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
5516 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
5517 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
5518 // for AST reading, too much interdependencies.
Richard Smith6377f8f2014-10-21 21:15:18 +00005519 const Type *T = nullptr;
5520 for (auto *DI = D; DI; DI = DI->getPreviousDecl()) {
5521 if (const Type *Existing = DI->getTypeForDecl()) {
5522 T = Existing;
5523 break;
5524 }
5525 }
5526 if (!T) {
Richard Smithf17fdbd2014-04-24 02:25:27 +00005527 T = new (Context, TypeAlignment) InjectedClassNameType(D, TST);
Richard Smith6377f8f2014-10-21 21:15:18 +00005528 for (auto *DI = D; DI; DI = DI->getPreviousDecl())
5529 DI->setTypeForDecl(T);
5530 }
Richard Smithf17fdbd2014-04-24 02:25:27 +00005531 return QualType(T, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00005532 }
5533
5534 case TYPE_TEMPLATE_TYPE_PARM: {
5535 unsigned Idx = 0;
5536 unsigned Depth = Record[Idx++];
5537 unsigned Index = Record[Idx++];
5538 bool Pack = Record[Idx++];
5539 TemplateTypeParmDecl *D
5540 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
5541 return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
5542 }
5543
5544 case TYPE_DEPENDENT_NAME: {
5545 unsigned Idx = 0;
5546 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
5547 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
5548 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
5549 QualType Canon = readType(*Loc.F, Record, Idx);
5550 if (!Canon.isNull())
5551 Canon = Context.getCanonicalType(Canon);
5552 return Context.getDependentNameType(Keyword, NNS, Name, Canon);
5553 }
5554
5555 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
5556 unsigned Idx = 0;
5557 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
5558 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
5559 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
5560 unsigned NumArgs = Record[Idx++];
5561 SmallVector<TemplateArgument, 8> Args;
5562 Args.reserve(NumArgs);
5563 while (NumArgs--)
5564 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
5565 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
5566 Args.size(), Args.data());
5567 }
5568
5569 case TYPE_DEPENDENT_SIZED_ARRAY: {
5570 unsigned Idx = 0;
5571
5572 // ArrayType
5573 QualType ElementType = readType(*Loc.F, Record, Idx);
5574 ArrayType::ArraySizeModifier ASM
5575 = (ArrayType::ArraySizeModifier)Record[Idx++];
5576 unsigned IndexTypeQuals = Record[Idx++];
5577
5578 // DependentSizedArrayType
5579 Expr *NumElts = ReadExpr(*Loc.F);
5580 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
5581
5582 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
5583 IndexTypeQuals, Brackets);
5584 }
5585
5586 case TYPE_TEMPLATE_SPECIALIZATION: {
5587 unsigned Idx = 0;
5588 bool IsDependent = Record[Idx++];
5589 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
5590 SmallVector<TemplateArgument, 8> Args;
5591 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
5592 QualType Underlying = readType(*Loc.F, Record, Idx);
5593 QualType T;
5594 if (Underlying.isNull())
5595 T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(),
5596 Args.size());
5597 else
5598 T = Context.getTemplateSpecializationType(Name, Args.data(),
5599 Args.size(), Underlying);
5600 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
5601 return T;
5602 }
5603
5604 case TYPE_ATOMIC: {
5605 if (Record.size() != 1) {
5606 Error("Incorrect encoding of atomic type");
5607 return QualType();
5608 }
5609 QualType ValueType = readType(*Loc.F, Record, Idx);
5610 return Context.getAtomicType(ValueType);
5611 }
5612 }
5613 llvm_unreachable("Invalid TypeCode!");
5614}
5615
Richard Smith564417a2014-03-20 21:47:22 +00005616void ASTReader::readExceptionSpec(ModuleFile &ModuleFile,
5617 SmallVectorImpl<QualType> &Exceptions,
Richard Smith8acb4282014-07-31 21:57:55 +00005618 FunctionProtoType::ExceptionSpecInfo &ESI,
Richard Smith564417a2014-03-20 21:47:22 +00005619 const RecordData &Record, unsigned &Idx) {
5620 ExceptionSpecificationType EST =
5621 static_cast<ExceptionSpecificationType>(Record[Idx++]);
Richard Smith8acb4282014-07-31 21:57:55 +00005622 ESI.Type = EST;
Richard Smith564417a2014-03-20 21:47:22 +00005623 if (EST == EST_Dynamic) {
Richard Smith8acb4282014-07-31 21:57:55 +00005624 for (unsigned I = 0, N = Record[Idx++]; I != N; ++I)
Richard Smith564417a2014-03-20 21:47:22 +00005625 Exceptions.push_back(readType(ModuleFile, Record, Idx));
Richard Smith8acb4282014-07-31 21:57:55 +00005626 ESI.Exceptions = Exceptions;
Richard Smith564417a2014-03-20 21:47:22 +00005627 } else if (EST == EST_ComputedNoexcept) {
Richard Smith8acb4282014-07-31 21:57:55 +00005628 ESI.NoexceptExpr = ReadExpr(ModuleFile);
Richard Smith564417a2014-03-20 21:47:22 +00005629 } else if (EST == EST_Uninstantiated) {
Richard Smith8acb4282014-07-31 21:57:55 +00005630 ESI.SourceDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
5631 ESI.SourceTemplate = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
Richard Smith564417a2014-03-20 21:47:22 +00005632 } else if (EST == EST_Unevaluated) {
Richard Smith8acb4282014-07-31 21:57:55 +00005633 ESI.SourceDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
Richard Smith564417a2014-03-20 21:47:22 +00005634 }
5635}
5636
Guy Benyei11169dd2012-12-18 14:30:41 +00005637class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
5638 ASTReader &Reader;
5639 ModuleFile &F;
5640 const ASTReader::RecordData &Record;
5641 unsigned &Idx;
5642
5643 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
5644 unsigned &I) {
5645 return Reader.ReadSourceLocation(F, R, I);
5646 }
5647
5648 template<typename T>
5649 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
5650 return Reader.ReadDeclAs<T>(F, Record, Idx);
5651 }
5652
5653public:
5654 TypeLocReader(ASTReader &Reader, ModuleFile &F,
5655 const ASTReader::RecordData &Record, unsigned &Idx)
5656 : Reader(Reader), F(F), Record(Record), Idx(Idx)
5657 { }
5658
5659 // We want compile-time assurance that we've enumerated all of
5660 // these, so unfortunately we have to declare them first, then
5661 // define them out-of-line.
5662#define ABSTRACT_TYPELOC(CLASS, PARENT)
5663#define TYPELOC(CLASS, PARENT) \
5664 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
5665#include "clang/AST/TypeLocNodes.def"
5666
5667 void VisitFunctionTypeLoc(FunctionTypeLoc);
5668 void VisitArrayTypeLoc(ArrayTypeLoc);
5669};
5670
5671void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
5672 // nothing to do
5673}
5674void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
5675 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
5676 if (TL.needsExtraLocalData()) {
5677 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
5678 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
5679 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
5680 TL.setModeAttr(Record[Idx++]);
5681 }
5682}
5683void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
5684 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5685}
5686void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
5687 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5688}
Reid Kleckner8a365022013-06-24 17:51:48 +00005689void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
5690 // nothing to do
5691}
Reid Kleckner0503a872013-12-05 01:23:43 +00005692void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
5693 // nothing to do
5694}
Guy Benyei11169dd2012-12-18 14:30:41 +00005695void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
5696 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
5697}
5698void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
5699 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
5700}
5701void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
5702 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
5703}
5704void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
5705 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5706 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5707}
5708void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
5709 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
5710 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
5711 if (Record[Idx++])
5712 TL.setSizeExpr(Reader.ReadExpr(F));
5713 else
Craig Toppera13603a2014-05-22 05:54:18 +00005714 TL.setSizeExpr(nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00005715}
5716void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
5717 VisitArrayTypeLoc(TL);
5718}
5719void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
5720 VisitArrayTypeLoc(TL);
5721}
5722void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
5723 VisitArrayTypeLoc(TL);
5724}
5725void TypeLocReader::VisitDependentSizedArrayTypeLoc(
5726 DependentSizedArrayTypeLoc TL) {
5727 VisitArrayTypeLoc(TL);
5728}
5729void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
5730 DependentSizedExtVectorTypeLoc TL) {
5731 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5732}
5733void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
5734 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5735}
5736void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
5737 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5738}
5739void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
5740 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
5741 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5742 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5743 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
Alp Tokerb3fd5cf2014-01-21 00:32:38 +00005744 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
5745 TL.setParam(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00005746 }
5747}
5748void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
5749 VisitFunctionTypeLoc(TL);
5750}
5751void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
5752 VisitFunctionTypeLoc(TL);
5753}
5754void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
5755 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5756}
5757void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
5758 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5759}
5760void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
5761 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
5762 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5763 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5764}
5765void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
5766 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
5767 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5768 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5769 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5770}
5771void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
5772 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5773}
5774void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
5775 TL.setKWLoc(ReadSourceLocation(Record, Idx));
5776 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5777 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5778 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5779}
5780void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
5781 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5782}
5783void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
5784 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5785}
5786void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
5787 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5788}
5789void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
5790 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
5791 if (TL.hasAttrOperand()) {
5792 SourceRange range;
5793 range.setBegin(ReadSourceLocation(Record, Idx));
5794 range.setEnd(ReadSourceLocation(Record, Idx));
5795 TL.setAttrOperandParensRange(range);
5796 }
5797 if (TL.hasAttrExprOperand()) {
5798 if (Record[Idx++])
5799 TL.setAttrExprOperand(Reader.ReadExpr(F));
5800 else
Craig Toppera13603a2014-05-22 05:54:18 +00005801 TL.setAttrExprOperand(nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00005802 } else if (TL.hasAttrEnumOperand())
5803 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
5804}
5805void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
5806 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5807}
5808void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
5809 SubstTemplateTypeParmTypeLoc TL) {
5810 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5811}
5812void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
5813 SubstTemplateTypeParmPackTypeLoc TL) {
5814 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5815}
5816void TypeLocReader::VisitTemplateSpecializationTypeLoc(
5817 TemplateSpecializationTypeLoc TL) {
5818 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
5819 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
5820 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5821 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5822 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
5823 TL.setArgLocInfo(i,
5824 Reader.GetTemplateArgumentLocInfo(F,
5825 TL.getTypePtr()->getArg(i).getKind(),
5826 Record, Idx));
5827}
5828void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
5829 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5830 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5831}
5832void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
5833 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5834 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5835}
5836void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
5837 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5838}
5839void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
5840 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5841 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5842 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5843}
5844void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
5845 DependentTemplateSpecializationTypeLoc TL) {
5846 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5847 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5848 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
5849 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
5850 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5851 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5852 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
5853 TL.setArgLocInfo(I,
5854 Reader.GetTemplateArgumentLocInfo(F,
5855 TL.getTypePtr()->getArg(I).getKind(),
5856 Record, Idx));
5857}
5858void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
5859 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
5860}
5861void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
5862 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5863}
5864void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
5865 TL.setHasBaseTypeAsWritten(Record[Idx++]);
5866 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5867 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5868 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
5869 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
5870}
5871void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
5872 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5873}
5874void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
5875 TL.setKWLoc(ReadSourceLocation(Record, Idx));
5876 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5877 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5878}
5879
5880TypeSourceInfo *ASTReader::GetTypeSourceInfo(ModuleFile &F,
5881 const RecordData &Record,
5882 unsigned &Idx) {
5883 QualType InfoTy = readType(F, Record, Idx);
5884 if (InfoTy.isNull())
Craig Toppera13603a2014-05-22 05:54:18 +00005885 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00005886
5887 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
5888 TypeLocReader TLR(*this, F, Record, Idx);
5889 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
5890 TLR.Visit(TL);
5891 return TInfo;
5892}
5893
5894QualType ASTReader::GetType(TypeID ID) {
5895 unsigned FastQuals = ID & Qualifiers::FastMask;
5896 unsigned Index = ID >> Qualifiers::FastWidth;
5897
5898 if (Index < NUM_PREDEF_TYPE_IDS) {
5899 QualType T;
5900 switch ((PredefinedTypeIDs)Index) {
5901 case PREDEF_TYPE_NULL_ID: return QualType();
5902 case PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
5903 case PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
5904
5905 case PREDEF_TYPE_CHAR_U_ID:
5906 case PREDEF_TYPE_CHAR_S_ID:
5907 // FIXME: Check that the signedness of CharTy is correct!
5908 T = Context.CharTy;
5909 break;
5910
5911 case PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
5912 case PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
5913 case PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
5914 case PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
5915 case PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
5916 case PREDEF_TYPE_UINT128_ID: T = Context.UnsignedInt128Ty; break;
5917 case PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
5918 case PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
5919 case PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
5920 case PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
5921 case PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
5922 case PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
5923 case PREDEF_TYPE_INT128_ID: T = Context.Int128Ty; break;
5924 case PREDEF_TYPE_HALF_ID: T = Context.HalfTy; break;
5925 case PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
5926 case PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
5927 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
5928 case PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
5929 case PREDEF_TYPE_BOUND_MEMBER: T = Context.BoundMemberTy; break;
5930 case PREDEF_TYPE_PSEUDO_OBJECT: T = Context.PseudoObjectTy; break;
5931 case PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
5932 case PREDEF_TYPE_UNKNOWN_ANY: T = Context.UnknownAnyTy; break;
5933 case PREDEF_TYPE_NULLPTR_ID: T = Context.NullPtrTy; break;
5934 case PREDEF_TYPE_CHAR16_ID: T = Context.Char16Ty; break;
5935 case PREDEF_TYPE_CHAR32_ID: T = Context.Char32Ty; break;
5936 case PREDEF_TYPE_OBJC_ID: T = Context.ObjCBuiltinIdTy; break;
5937 case PREDEF_TYPE_OBJC_CLASS: T = Context.ObjCBuiltinClassTy; break;
5938 case PREDEF_TYPE_OBJC_SEL: T = Context.ObjCBuiltinSelTy; break;
Guy Benyeid8a08ea2012-12-18 14:38:23 +00005939 case PREDEF_TYPE_IMAGE1D_ID: T = Context.OCLImage1dTy; break;
5940 case PREDEF_TYPE_IMAGE1D_ARR_ID: T = Context.OCLImage1dArrayTy; break;
5941 case PREDEF_TYPE_IMAGE1D_BUFF_ID: T = Context.OCLImage1dBufferTy; break;
5942 case PREDEF_TYPE_IMAGE2D_ID: T = Context.OCLImage2dTy; break;
5943 case PREDEF_TYPE_IMAGE2D_ARR_ID: T = Context.OCLImage2dArrayTy; break;
5944 case PREDEF_TYPE_IMAGE3D_ID: T = Context.OCLImage3dTy; break;
Guy Benyei61054192013-02-07 10:55:47 +00005945 case PREDEF_TYPE_SAMPLER_ID: T = Context.OCLSamplerTy; break;
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00005946 case PREDEF_TYPE_EVENT_ID: T = Context.OCLEventTy; break;
Guy Benyei11169dd2012-12-18 14:30:41 +00005947 case PREDEF_TYPE_AUTO_DEDUCT: T = Context.getAutoDeductType(); break;
5948
5949 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
5950 T = Context.getAutoRRefDeductType();
5951 break;
5952
5953 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
5954 T = Context.ARCUnbridgedCastTy;
5955 break;
5956
5957 case PREDEF_TYPE_VA_LIST_TAG:
5958 T = Context.getVaListTagType();
5959 break;
5960
5961 case PREDEF_TYPE_BUILTIN_FN:
5962 T = Context.BuiltinFnTy;
5963 break;
5964 }
5965
5966 assert(!T.isNull() && "Unknown predefined type");
5967 return T.withFastQualifiers(FastQuals);
5968 }
5969
5970 Index -= NUM_PREDEF_TYPE_IDS;
5971 assert(Index < TypesLoaded.size() && "Type index out-of-range");
5972 if (TypesLoaded[Index].isNull()) {
5973 TypesLoaded[Index] = readTypeRecord(Index);
5974 if (TypesLoaded[Index].isNull())
5975 return QualType();
5976
5977 TypesLoaded[Index]->setFromAST();
5978 if (DeserializationListener)
5979 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
5980 TypesLoaded[Index]);
5981 }
5982
5983 return TypesLoaded[Index].withFastQualifiers(FastQuals);
5984}
5985
5986QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
5987 return GetType(getGlobalTypeID(F, LocalID));
5988}
5989
5990serialization::TypeID
5991ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
5992 unsigned FastQuals = LocalID & Qualifiers::FastMask;
5993 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
5994
5995 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
5996 return LocalID;
5997
5998 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5999 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
6000 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
6001
6002 unsigned GlobalIndex = LocalIndex + I->second;
6003 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
6004}
6005
6006TemplateArgumentLocInfo
6007ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
6008 TemplateArgument::ArgKind Kind,
6009 const RecordData &Record,
6010 unsigned &Index) {
6011 switch (Kind) {
6012 case TemplateArgument::Expression:
6013 return ReadExpr(F);
6014 case TemplateArgument::Type:
6015 return GetTypeSourceInfo(F, Record, Index);
6016 case TemplateArgument::Template: {
6017 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
6018 Index);
6019 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
6020 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
6021 SourceLocation());
6022 }
6023 case TemplateArgument::TemplateExpansion: {
6024 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
6025 Index);
6026 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
6027 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
6028 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
6029 EllipsisLoc);
6030 }
6031 case TemplateArgument::Null:
6032 case TemplateArgument::Integral:
6033 case TemplateArgument::Declaration:
6034 case TemplateArgument::NullPtr:
6035 case TemplateArgument::Pack:
6036 // FIXME: Is this right?
6037 return TemplateArgumentLocInfo();
6038 }
6039 llvm_unreachable("unexpected template argument loc");
6040}
6041
6042TemplateArgumentLoc
6043ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
6044 const RecordData &Record, unsigned &Index) {
6045 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
6046
6047 if (Arg.getKind() == TemplateArgument::Expression) {
6048 if (Record[Index++]) // bool InfoHasSameExpr.
6049 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
6050 }
6051 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
6052 Record, Index));
6053}
6054
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00006055const ASTTemplateArgumentListInfo*
6056ASTReader::ReadASTTemplateArgumentListInfo(ModuleFile &F,
6057 const RecordData &Record,
6058 unsigned &Index) {
6059 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Index);
6060 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Index);
6061 unsigned NumArgsAsWritten = Record[Index++];
6062 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
6063 for (unsigned i = 0; i != NumArgsAsWritten; ++i)
6064 TemplArgsInfo.addArgument(ReadTemplateArgumentLoc(F, Record, Index));
6065 return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo);
6066}
6067
Guy Benyei11169dd2012-12-18 14:30:41 +00006068Decl *ASTReader::GetExternalDecl(uint32_t ID) {
6069 return GetDecl(ID);
6070}
6071
Richard Smith50895422015-01-31 03:04:55 +00006072template<typename TemplateSpecializationDecl>
6073static void completeRedeclChainForTemplateSpecialization(Decl *D) {
6074 if (auto *TSD = dyn_cast<TemplateSpecializationDecl>(D))
6075 TSD->getSpecializedTemplate()->LoadLazySpecializations();
6076}
6077
Richard Smith053f6c62014-05-16 23:01:30 +00006078void ASTReader::CompleteRedeclChain(const Decl *D) {
Richard Smith851072e2014-05-19 20:59:20 +00006079 if (NumCurrentElementsDeserializing) {
6080 // We arrange to not care about the complete redeclaration chain while we're
6081 // deserializing. Just remember that the AST has marked this one as complete
6082 // but that it's not actually complete yet, so we know we still need to
6083 // complete it later.
6084 PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D));
6085 return;
6086 }
6087
Richard Smith053f6c62014-05-16 23:01:30 +00006088 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
6089
Richard Smith053f6c62014-05-16 23:01:30 +00006090 // If this is a named declaration, complete it by looking it up
6091 // within its context.
6092 //
Richard Smith01bdb7a2014-08-28 05:44:07 +00006093 // FIXME: Merging a function definition should merge
Richard Smith053f6c62014-05-16 23:01:30 +00006094 // all mergeable entities within it.
6095 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) ||
6096 isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) {
6097 if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) {
6098 auto *II = Name.getAsIdentifierInfo();
6099 if (isa<TranslationUnitDecl>(DC) && II) {
6100 // Outside of C++, we don't have a lookup table for the TU, so update
6101 // the identifier instead. In C++, either way should work fine.
6102 if (II->isOutOfDate())
6103 updateOutOfDateIdentifier(*II);
6104 } else
6105 DC->lookup(Name);
Richard Smith01bdb7a2014-08-28 05:44:07 +00006106 } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) {
6107 // FIXME: It'd be nice to do something a bit more targeted here.
6108 D->getDeclContext()->decls_begin();
Richard Smith053f6c62014-05-16 23:01:30 +00006109 }
6110 }
Richard Smith50895422015-01-31 03:04:55 +00006111
6112 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D))
6113 CTSD->getSpecializedTemplate()->LoadLazySpecializations();
6114 if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D))
6115 VTSD->getSpecializedTemplate()->LoadLazySpecializations();
6116 if (auto *FD = dyn_cast<FunctionDecl>(D)) {
6117 if (auto *Template = FD->getPrimaryTemplate())
6118 Template->LoadLazySpecializations();
6119 }
Richard Smith053f6c62014-05-16 23:01:30 +00006120}
6121
Richard Smithc2bb8182015-03-24 06:36:48 +00006122uint64_t ASTReader::ReadCXXCtorInitializersRef(ModuleFile &M,
6123 const RecordData &Record,
6124 unsigned &Idx) {
6125 if (Idx >= Record.size() || Record[Idx] > M.LocalNumCXXCtorInitializers) {
6126 Error("malformed AST file: missing C++ ctor initializers");
6127 return 0;
6128 }
6129
6130 unsigned LocalID = Record[Idx++];
6131 return getGlobalBitOffset(M, M.CXXCtorInitializersOffsets[LocalID - 1]);
6132}
6133
6134CXXCtorInitializer **
6135ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
6136 RecordLocation Loc = getLocalBitOffset(Offset);
6137 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
6138 SavedStreamPosition SavedPosition(Cursor);
6139 Cursor.JumpToBit(Loc.Offset);
6140 ReadingKindTracker ReadingKind(Read_Decl, *this);
6141
6142 RecordData Record;
6143 unsigned Code = Cursor.ReadCode();
6144 unsigned RecCode = Cursor.readRecord(Code, Record);
6145 if (RecCode != DECL_CXX_CTOR_INITIALIZERS) {
6146 Error("malformed AST file: missing C++ ctor initializers");
6147 return nullptr;
6148 }
6149
6150 unsigned Idx = 0;
6151 return ReadCXXCtorInitializers(*Loc.F, Record, Idx);
6152}
6153
Richard Smithcd45dbc2014-04-19 03:48:30 +00006154uint64_t ASTReader::readCXXBaseSpecifiers(ModuleFile &M,
6155 const RecordData &Record,
6156 unsigned &Idx) {
6157 if (Idx >= Record.size() || Record[Idx] > M.LocalNumCXXBaseSpecifiers) {
6158 Error("malformed AST file: missing C++ base specifier");
Guy Benyei11169dd2012-12-18 14:30:41 +00006159 return 0;
Richard Smithcd45dbc2014-04-19 03:48:30 +00006160 }
6161
Guy Benyei11169dd2012-12-18 14:30:41 +00006162 unsigned LocalID = Record[Idx++];
6163 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
6164}
6165
6166CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
6167 RecordLocation Loc = getLocalBitOffset(Offset);
Chris Lattner7fb3bef2013-01-20 00:56:42 +00006168 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00006169 SavedStreamPosition SavedPosition(Cursor);
6170 Cursor.JumpToBit(Loc.Offset);
6171 ReadingKindTracker ReadingKind(Read_Decl, *this);
6172 RecordData Record;
6173 unsigned Code = Cursor.ReadCode();
Chris Lattner0e6c9402013-01-20 02:38:54 +00006174 unsigned RecCode = Cursor.readRecord(Code, Record);
Guy Benyei11169dd2012-12-18 14:30:41 +00006175 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00006176 Error("malformed AST file: missing C++ base specifiers");
Craig Toppera13603a2014-05-22 05:54:18 +00006177 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00006178 }
6179
6180 unsigned Idx = 0;
6181 unsigned NumBases = Record[Idx++];
6182 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
6183 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
6184 for (unsigned I = 0; I != NumBases; ++I)
6185 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
6186 return Bases;
6187}
6188
6189serialization::DeclID
6190ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
6191 if (LocalID < NUM_PREDEF_DECL_IDS)
6192 return LocalID;
6193
6194 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6195 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
6196 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
6197
6198 return LocalID + I->second;
6199}
6200
6201bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
6202 ModuleFile &M) const {
Richard Smithfe620d22015-03-05 23:24:12 +00006203 // Predefined decls aren't from any module.
6204 if (ID < NUM_PREDEF_DECL_IDS)
6205 return false;
6206
Guy Benyei11169dd2012-12-18 14:30:41 +00006207 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(ID);
6208 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
6209 return &M == I->second;
6210}
6211
Douglas Gregor9f782892013-01-21 15:25:38 +00006212ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
Guy Benyei11169dd2012-12-18 14:30:41 +00006213 if (!D->isFromASTFile())
Craig Toppera13603a2014-05-22 05:54:18 +00006214 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00006215 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
6216 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
6217 return I->second;
6218}
6219
6220SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
6221 if (ID < NUM_PREDEF_DECL_IDS)
6222 return SourceLocation();
Richard Smithcd45dbc2014-04-19 03:48:30 +00006223
Guy Benyei11169dd2012-12-18 14:30:41 +00006224 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
6225
6226 if (Index > DeclsLoaded.size()) {
6227 Error("declaration ID out-of-range for AST file");
6228 return SourceLocation();
6229 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00006230
Guy Benyei11169dd2012-12-18 14:30:41 +00006231 if (Decl *D = DeclsLoaded[Index])
6232 return D->getLocation();
6233
6234 unsigned RawLocation = 0;
6235 RecordLocation Rec = DeclCursorForID(ID, RawLocation);
6236 return ReadSourceLocation(*Rec.F, RawLocation);
6237}
6238
Richard Smithfe620d22015-03-05 23:24:12 +00006239static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) {
6240 switch (ID) {
6241 case PREDEF_DECL_NULL_ID:
6242 return nullptr;
6243
6244 case PREDEF_DECL_TRANSLATION_UNIT_ID:
6245 return Context.getTranslationUnitDecl();
6246
6247 case PREDEF_DECL_OBJC_ID_ID:
6248 return Context.getObjCIdDecl();
6249
6250 case PREDEF_DECL_OBJC_SEL_ID:
6251 return Context.getObjCSelDecl();
6252
6253 case PREDEF_DECL_OBJC_CLASS_ID:
6254 return Context.getObjCClassDecl();
6255
6256 case PREDEF_DECL_OBJC_PROTOCOL_ID:
6257 return Context.getObjCProtocolDecl();
6258
6259 case PREDEF_DECL_INT_128_ID:
6260 return Context.getInt128Decl();
6261
6262 case PREDEF_DECL_UNSIGNED_INT_128_ID:
6263 return Context.getUInt128Decl();
6264
6265 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
6266 return Context.getObjCInstanceTypeDecl();
6267
6268 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
6269 return Context.getBuiltinVaListDecl();
Richard Smithf19e1272015-03-07 00:04:49 +00006270
6271 case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
6272 return Context.getExternCContextDecl();
Richard Smithfe620d22015-03-05 23:24:12 +00006273 }
Yaron Keren322bdad2015-03-06 07:49:14 +00006274 llvm_unreachable("PredefinedDeclIDs unknown enum value");
Richard Smithfe620d22015-03-05 23:24:12 +00006275}
6276
Richard Smithcd45dbc2014-04-19 03:48:30 +00006277Decl *ASTReader::GetExistingDecl(DeclID ID) {
6278 if (ID < NUM_PREDEF_DECL_IDS) {
Richard Smithfe620d22015-03-05 23:24:12 +00006279 Decl *D = getPredefinedDecl(Context, (PredefinedDeclIDs)ID);
6280 if (D) {
6281 // Track that we have merged the declaration with ID \p ID into the
6282 // pre-existing predefined declaration \p D.
6283 auto &Merged = MergedDecls[D->getCanonicalDecl()];
6284 if (Merged.empty())
6285 Merged.push_back(ID);
Guy Benyei11169dd2012-12-18 14:30:41 +00006286 }
Richard Smithfe620d22015-03-05 23:24:12 +00006287 return D;
Guy Benyei11169dd2012-12-18 14:30:41 +00006288 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00006289
Guy Benyei11169dd2012-12-18 14:30:41 +00006290 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
6291
6292 if (Index >= DeclsLoaded.size()) {
6293 assert(0 && "declaration ID out-of-range for AST file");
6294 Error("declaration ID out-of-range for AST file");
Craig Toppera13603a2014-05-22 05:54:18 +00006295 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00006296 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00006297
6298 return DeclsLoaded[Index];
6299}
6300
6301Decl *ASTReader::GetDecl(DeclID ID) {
6302 if (ID < NUM_PREDEF_DECL_IDS)
6303 return GetExistingDecl(ID);
6304
6305 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
6306
6307 if (Index >= DeclsLoaded.size()) {
6308 assert(0 && "declaration ID out-of-range for AST file");
6309 Error("declaration ID out-of-range for AST file");
Craig Toppera13603a2014-05-22 05:54:18 +00006310 return nullptr;
Richard Smithcd45dbc2014-04-19 03:48:30 +00006311 }
6312
Guy Benyei11169dd2012-12-18 14:30:41 +00006313 if (!DeclsLoaded[Index]) {
6314 ReadDeclRecord(ID);
6315 if (DeserializationListener)
6316 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
6317 }
6318
6319 return DeclsLoaded[Index];
6320}
6321
6322DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
6323 DeclID GlobalID) {
6324 if (GlobalID < NUM_PREDEF_DECL_IDS)
6325 return GlobalID;
6326
6327 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
6328 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
6329 ModuleFile *Owner = I->second;
6330
6331 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
6332 = M.GlobalToLocalDeclIDs.find(Owner);
6333 if (Pos == M.GlobalToLocalDeclIDs.end())
6334 return 0;
6335
6336 return GlobalID - Owner->BaseDeclID + Pos->second;
6337}
6338
6339serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
6340 const RecordData &Record,
6341 unsigned &Idx) {
6342 if (Idx >= Record.size()) {
6343 Error("Corrupted AST file");
6344 return 0;
6345 }
6346
6347 return getGlobalDeclID(F, Record[Idx++]);
6348}
6349
6350/// \brief Resolve the offset of a statement into a statement.
6351///
6352/// This operation will read a new statement from the external
6353/// source each time it is called, and is meant to be used via a
6354/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
6355Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
6356 // Switch case IDs are per Decl.
6357 ClearSwitchCaseIDs();
6358
6359 // Offset here is a global offset across the entire chain.
6360 RecordLocation Loc = getLocalBitOffset(Offset);
6361 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
6362 return ReadStmtFromStream(*Loc.F);
6363}
6364
6365namespace {
6366 class FindExternalLexicalDeclsVisitor {
6367 ASTReader &Reader;
6368 const DeclContext *DC;
6369 bool (*isKindWeWant)(Decl::Kind);
6370
6371 SmallVectorImpl<Decl*> &Decls;
6372 bool PredefsVisited[NUM_PREDEF_DECL_IDS];
6373
6374 public:
6375 FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC,
6376 bool (*isKindWeWant)(Decl::Kind),
6377 SmallVectorImpl<Decl*> &Decls)
6378 : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls)
6379 {
6380 for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I)
6381 PredefsVisited[I] = false;
6382 }
6383
6384 static bool visit(ModuleFile &M, bool Preorder, void *UserData) {
6385 if (Preorder)
6386 return false;
6387
6388 FindExternalLexicalDeclsVisitor *This
6389 = static_cast<FindExternalLexicalDeclsVisitor *>(UserData);
6390
6391 ModuleFile::DeclContextInfosMap::iterator Info
6392 = M.DeclContextInfos.find(This->DC);
6393 if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls)
6394 return false;
6395
6396 // Load all of the declaration IDs
6397 for (const KindDeclIDPair *ID = Info->second.LexicalDecls,
6398 *IDE = ID + Info->second.NumLexicalDecls;
6399 ID != IDE; ++ID) {
6400 if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first))
6401 continue;
6402
6403 // Don't add predefined declarations to the lexical context more
6404 // than once.
6405 if (ID->second < NUM_PREDEF_DECL_IDS) {
6406 if (This->PredefsVisited[ID->second])
6407 continue;
6408
6409 This->PredefsVisited[ID->second] = true;
6410 }
6411
6412 if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) {
6413 if (!This->DC->isDeclInLexicalTraversal(D))
6414 This->Decls.push_back(D);
6415 }
6416 }
6417
6418 return false;
6419 }
6420 };
6421}
6422
6423ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
6424 bool (*isKindWeWant)(Decl::Kind),
6425 SmallVectorImpl<Decl*> &Decls) {
6426 // There might be lexical decls in multiple modules, for the TU at
6427 // least. Walk all of the modules in the order they were loaded.
6428 FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls);
6429 ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor);
6430 ++NumLexicalDeclContextsRead;
6431 return ELR_Success;
6432}
6433
6434namespace {
6435
6436class DeclIDComp {
6437 ASTReader &Reader;
6438 ModuleFile &Mod;
6439
6440public:
6441 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
6442
6443 bool operator()(LocalDeclID L, LocalDeclID R) const {
6444 SourceLocation LHS = getLocation(L);
6445 SourceLocation RHS = getLocation(R);
6446 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6447 }
6448
6449 bool operator()(SourceLocation LHS, LocalDeclID R) const {
6450 SourceLocation RHS = getLocation(R);
6451 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6452 }
6453
6454 bool operator()(LocalDeclID L, SourceLocation RHS) const {
6455 SourceLocation LHS = getLocation(L);
6456 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6457 }
6458
6459 SourceLocation getLocation(LocalDeclID ID) const {
6460 return Reader.getSourceManager().getFileLoc(
6461 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
6462 }
6463};
6464
6465}
6466
6467void ASTReader::FindFileRegionDecls(FileID File,
6468 unsigned Offset, unsigned Length,
6469 SmallVectorImpl<Decl *> &Decls) {
6470 SourceManager &SM = getSourceManager();
6471
6472 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
6473 if (I == FileDeclIDs.end())
6474 return;
6475
6476 FileDeclsInfo &DInfo = I->second;
6477 if (DInfo.Decls.empty())
6478 return;
6479
6480 SourceLocation
6481 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
6482 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
6483
6484 DeclIDComp DIDComp(*this, *DInfo.Mod);
6485 ArrayRef<serialization::LocalDeclID>::iterator
6486 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
6487 BeginLoc, DIDComp);
6488 if (BeginIt != DInfo.Decls.begin())
6489 --BeginIt;
6490
6491 // If we are pointing at a top-level decl inside an objc container, we need
6492 // to backtrack until we find it otherwise we will fail to report that the
6493 // region overlaps with an objc container.
6494 while (BeginIt != DInfo.Decls.begin() &&
6495 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
6496 ->isTopLevelDeclInObjCContainer())
6497 --BeginIt;
6498
6499 ArrayRef<serialization::LocalDeclID>::iterator
6500 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
6501 EndLoc, DIDComp);
6502 if (EndIt != DInfo.Decls.end())
6503 ++EndIt;
6504
6505 for (ArrayRef<serialization::LocalDeclID>::iterator
6506 DIt = BeginIt; DIt != EndIt; ++DIt)
6507 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
6508}
6509
6510namespace {
6511 /// \brief ModuleFile visitor used to perform name lookup into a
6512 /// declaration context.
6513 class DeclContextNameLookupVisitor {
6514 ASTReader &Reader;
Richard Smith8c913ec2014-08-14 02:21:01 +00006515 ArrayRef<const DeclContext *> Contexts;
Guy Benyei11169dd2012-12-18 14:30:41 +00006516 DeclarationName Name;
6517 SmallVectorImpl<NamedDecl *> &Decls;
Richard Smith52874ec2015-02-13 20:17:14 +00006518 llvm::SmallPtrSetImpl<NamedDecl *> &DeclSet;
Guy Benyei11169dd2012-12-18 14:30:41 +00006519
6520 public:
Richard Smith8c913ec2014-08-14 02:21:01 +00006521 DeclContextNameLookupVisitor(ASTReader &Reader,
6522 ArrayRef<const DeclContext *> Contexts,
Guy Benyei11169dd2012-12-18 14:30:41 +00006523 DeclarationName Name,
Richard Smith52874ec2015-02-13 20:17:14 +00006524 SmallVectorImpl<NamedDecl *> &Decls,
6525 llvm::SmallPtrSetImpl<NamedDecl *> &DeclSet)
6526 : Reader(Reader), Contexts(Contexts), Name(Name), Decls(Decls),
6527 DeclSet(DeclSet) { }
Guy Benyei11169dd2012-12-18 14:30:41 +00006528
6529 static bool visit(ModuleFile &M, void *UserData) {
6530 DeclContextNameLookupVisitor *This
6531 = static_cast<DeclContextNameLookupVisitor *>(UserData);
6532
6533 // Check whether we have any visible declaration information for
6534 // this context in this module.
6535 ModuleFile::DeclContextInfosMap::iterator Info;
6536 bool FoundInfo = false;
Richard Smith8c913ec2014-08-14 02:21:01 +00006537 for (auto *DC : This->Contexts) {
6538 Info = M.DeclContextInfos.find(DC);
6539 if (Info != M.DeclContextInfos.end() &&
Guy Benyei11169dd2012-12-18 14:30:41 +00006540 Info->second.NameLookupTableData) {
6541 FoundInfo = true;
6542 break;
6543 }
6544 }
6545
6546 if (!FoundInfo)
6547 return false;
Richard Smith8c913ec2014-08-14 02:21:01 +00006548
Guy Benyei11169dd2012-12-18 14:30:41 +00006549 // Look for this name within this module.
Richard Smith52e3fba2014-03-11 07:17:35 +00006550 ASTDeclContextNameLookupTable *LookupTable =
Guy Benyei11169dd2012-12-18 14:30:41 +00006551 Info->second.NameLookupTableData;
6552 ASTDeclContextNameLookupTable::iterator Pos
6553 = LookupTable->find(This->Name);
6554 if (Pos == LookupTable->end())
6555 return false;
6556
6557 bool FoundAnything = false;
6558 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
6559 for (; Data.first != Data.second; ++Data.first) {
6560 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first);
6561 if (!ND)
6562 continue;
6563
6564 if (ND->getDeclName() != This->Name) {
6565 // A name might be null because the decl's redeclarable part is
6566 // currently read before reading its name. The lookup is triggered by
6567 // building that decl (likely indirectly), and so it is later in the
6568 // sense of "already existing" and can be ignored here.
Richard Smith8c913ec2014-08-14 02:21:01 +00006569 // FIXME: This should not happen; deserializing declarations should
6570 // not perform lookups since that can lead to deserialization cycles.
Guy Benyei11169dd2012-12-18 14:30:41 +00006571 continue;
6572 }
Richard Smith8c913ec2014-08-14 02:21:01 +00006573
Guy Benyei11169dd2012-12-18 14:30:41 +00006574 // Record this declaration.
6575 FoundAnything = true;
Richard Smith52874ec2015-02-13 20:17:14 +00006576 if (This->DeclSet.insert(ND).second)
6577 This->Decls.push_back(ND);
Guy Benyei11169dd2012-12-18 14:30:41 +00006578 }
6579
6580 return FoundAnything;
6581 }
6582 };
6583}
6584
Douglas Gregor9f782892013-01-21 15:25:38 +00006585/// \brief Retrieve the "definitive" module file for the definition of the
6586/// given declaration context, if there is one.
6587///
6588/// The "definitive" module file is the only place where we need to look to
6589/// find information about the declarations within the given declaration
6590/// context. For example, C++ and Objective-C classes, C structs/unions, and
6591/// Objective-C protocols, categories, and extensions are all defined in a
6592/// single place in the source code, so they have definitive module files
6593/// associated with them. C++ namespaces, on the other hand, can have
6594/// definitions in multiple different module files.
6595///
6596/// Note: this needs to be kept in sync with ASTWriter::AddedVisibleDecl's
6597/// NDEBUG checking.
6598static ModuleFile *getDefinitiveModuleFileFor(const DeclContext *DC,
6599 ASTReader &Reader) {
Douglas Gregor7a6e2002013-01-22 17:08:30 +00006600 if (const DeclContext *DefDC = getDefinitiveDeclContext(DC))
6601 return Reader.getOwningModuleFile(cast<Decl>(DefDC));
Douglas Gregor9f782892013-01-21 15:25:38 +00006602
Craig Toppera13603a2014-05-22 05:54:18 +00006603 return nullptr;
Douglas Gregor9f782892013-01-21 15:25:38 +00006604}
6605
Richard Smith9ce12e32013-02-07 03:30:24 +00006606bool
Guy Benyei11169dd2012-12-18 14:30:41 +00006607ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
6608 DeclarationName Name) {
6609 assert(DC->hasExternalVisibleStorage() &&
6610 "DeclContext has no visible decls in storage");
6611 if (!Name)
Richard Smith9ce12e32013-02-07 03:30:24 +00006612 return false;
Guy Benyei11169dd2012-12-18 14:30:41 +00006613
Richard Smith8c913ec2014-08-14 02:21:01 +00006614 Deserializing LookupResults(this);
6615
Guy Benyei11169dd2012-12-18 14:30:41 +00006616 SmallVector<NamedDecl *, 64> Decls;
Richard Smith52874ec2015-02-13 20:17:14 +00006617 llvm::SmallPtrSet<NamedDecl*, 64> DeclSet;
Richard Smith8c913ec2014-08-14 02:21:01 +00006618
Guy Benyei11169dd2012-12-18 14:30:41 +00006619 // Compute the declaration contexts we need to look into. Multiple such
6620 // declaration contexts occur when two declaration contexts from disjoint
6621 // modules get merged, e.g., when two namespaces with the same name are
6622 // independently defined in separate modules.
6623 SmallVector<const DeclContext *, 2> Contexts;
6624 Contexts.push_back(DC);
Richard Smith8c913ec2014-08-14 02:21:01 +00006625
Guy Benyei11169dd2012-12-18 14:30:41 +00006626 if (DC->isNamespace()) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00006627 auto Merged = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
Guy Benyei11169dd2012-12-18 14:30:41 +00006628 if (Merged != MergedDecls.end()) {
6629 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
6630 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
6631 }
6632 }
Richard Smith8c913ec2014-08-14 02:21:01 +00006633
6634 auto LookUpInContexts = [&](ArrayRef<const DeclContext*> Contexts) {
Richard Smith52874ec2015-02-13 20:17:14 +00006635 DeclContextNameLookupVisitor Visitor(*this, Contexts, Name, Decls, DeclSet);
Richard Smith8c913ec2014-08-14 02:21:01 +00006636
6637 // If we can definitively determine which module file to look into,
6638 // only look there. Otherwise, look in all module files.
6639 ModuleFile *Definitive;
6640 if (Contexts.size() == 1 &&
6641 (Definitive = getDefinitiveModuleFileFor(Contexts[0], *this))) {
6642 DeclContextNameLookupVisitor::visit(*Definitive, &Visitor);
6643 } else {
6644 ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor);
6645 }
6646 };
6647
6648 LookUpInContexts(Contexts);
6649
6650 // If this might be an implicit special member function, then also search
6651 // all merged definitions of the surrounding class. We need to search them
6652 // individually, because finding an entity in one of them doesn't imply that
6653 // we can't find a different entity in another one.
Richard Smithcd45dbc2014-04-19 03:48:30 +00006654 if (isa<CXXRecordDecl>(DC)) {
Richard Smith02793752015-03-27 21:16:39 +00006655 auto Merged = MergedLookups.find(DC);
6656 if (Merged != MergedLookups.end()) {
6657 for (unsigned I = 0; I != Merged->second.size(); ++I) {
6658 const DeclContext *Context = Merged->second[I];
6659 LookUpInContexts(Context);
6660 // We might have just added some more merged lookups. If so, our
6661 // iterator is now invalid, so grab a fresh one before continuing.
6662 Merged = MergedLookups.find(DC);
Richard Smithe0612472014-11-21 05:16:13 +00006663 }
Richard Smith8c913ec2014-08-14 02:21:01 +00006664 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00006665 }
6666
Guy Benyei11169dd2012-12-18 14:30:41 +00006667 ++NumVisibleDeclContextsRead;
6668 SetExternalVisibleDeclsForName(DC, Name, Decls);
Richard Smith9ce12e32013-02-07 03:30:24 +00006669 return !Decls.empty();
Guy Benyei11169dd2012-12-18 14:30:41 +00006670}
6671
6672namespace {
6673 /// \brief ModuleFile visitor used to retrieve all visible names in a
6674 /// declaration context.
6675 class DeclContextAllNamesVisitor {
6676 ASTReader &Reader;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00006677 SmallVectorImpl<const DeclContext *> &Contexts;
Craig Topper3598eb72013-07-05 04:43:31 +00006678 DeclsMap &Decls;
Richard Smith52874ec2015-02-13 20:17:14 +00006679 llvm::SmallPtrSet<NamedDecl *, 256> DeclSet;
Argyrios Kyrtzidis2810e9d2012-12-19 22:21:18 +00006680 bool VisitAll;
Guy Benyei11169dd2012-12-18 14:30:41 +00006681
6682 public:
6683 DeclContextAllNamesVisitor(ASTReader &Reader,
6684 SmallVectorImpl<const DeclContext *> &Contexts,
Craig Topper3598eb72013-07-05 04:43:31 +00006685 DeclsMap &Decls, bool VisitAll)
Argyrios Kyrtzidis2810e9d2012-12-19 22:21:18 +00006686 : Reader(Reader), Contexts(Contexts), Decls(Decls), VisitAll(VisitAll) { }
Guy Benyei11169dd2012-12-18 14:30:41 +00006687
6688 static bool visit(ModuleFile &M, void *UserData) {
6689 DeclContextAllNamesVisitor *This
6690 = static_cast<DeclContextAllNamesVisitor *>(UserData);
6691
6692 // Check whether we have any visible declaration information for
6693 // this context in this module.
6694 ModuleFile::DeclContextInfosMap::iterator Info;
6695 bool FoundInfo = false;
6696 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
6697 Info = M.DeclContextInfos.find(This->Contexts[I]);
6698 if (Info != M.DeclContextInfos.end() &&
6699 Info->second.NameLookupTableData) {
6700 FoundInfo = true;
6701 break;
6702 }
6703 }
6704
6705 if (!FoundInfo)
6706 return false;
6707
Richard Smith52e3fba2014-03-11 07:17:35 +00006708 ASTDeclContextNameLookupTable *LookupTable =
Guy Benyei11169dd2012-12-18 14:30:41 +00006709 Info->second.NameLookupTableData;
6710 bool FoundAnything = false;
6711 for (ASTDeclContextNameLookupTable::data_iterator
Douglas Gregor5e306b12013-01-23 22:38:11 +00006712 I = LookupTable->data_begin(), E = LookupTable->data_end();
6713 I != E;
6714 ++I) {
Guy Benyei11169dd2012-12-18 14:30:41 +00006715 ASTDeclContextNameLookupTrait::data_type Data = *I;
6716 for (; Data.first != Data.second; ++Data.first) {
6717 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M,
6718 *Data.first);
6719 if (!ND)
6720 continue;
6721
6722 // Record this declaration.
6723 FoundAnything = true;
Richard Smith52874ec2015-02-13 20:17:14 +00006724 if (This->DeclSet.insert(ND).second)
6725 This->Decls[ND->getDeclName()].push_back(ND);
Guy Benyei11169dd2012-12-18 14:30:41 +00006726 }
6727 }
6728
Argyrios Kyrtzidis2810e9d2012-12-19 22:21:18 +00006729 return FoundAnything && !This->VisitAll;
Guy Benyei11169dd2012-12-18 14:30:41 +00006730 }
6731 };
6732}
6733
6734void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
6735 if (!DC->hasExternalVisibleStorage())
6736 return;
Craig Topper79be4cd2013-07-05 04:33:53 +00006737 DeclsMap Decls;
Guy Benyei11169dd2012-12-18 14:30:41 +00006738
6739 // Compute the declaration contexts we need to look into. Multiple such
6740 // declaration contexts occur when two declaration contexts from disjoint
6741 // modules get merged, e.g., when two namespaces with the same name are
6742 // independently defined in separate modules.
6743 SmallVector<const DeclContext *, 2> Contexts;
6744 Contexts.push_back(DC);
6745
6746 if (DC->isNamespace()) {
6747 MergedDeclsMap::iterator Merged
6748 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
6749 if (Merged != MergedDecls.end()) {
6750 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
6751 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
6752 }
6753 }
6754
Argyrios Kyrtzidis2810e9d2012-12-19 22:21:18 +00006755 DeclContextAllNamesVisitor Visitor(*this, Contexts, Decls,
6756 /*VisitAll=*/DC->isFileContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00006757 ModuleMgr.visit(&DeclContextAllNamesVisitor::visit, &Visitor);
6758 ++NumVisibleDeclContextsRead;
6759
Craig Topper79be4cd2013-07-05 04:33:53 +00006760 for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
Guy Benyei11169dd2012-12-18 14:30:41 +00006761 SetExternalVisibleDeclsForName(DC, I->first, I->second);
6762 }
6763 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
6764}
6765
6766/// \brief Under non-PCH compilation the consumer receives the objc methods
6767/// before receiving the implementation, and codegen depends on this.
6768/// We simulate this by deserializing and passing to consumer the methods of the
6769/// implementation before passing the deserialized implementation decl.
6770static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
6771 ASTConsumer *Consumer) {
6772 assert(ImplD && Consumer);
6773
Aaron Ballmanaff18c02014-03-13 19:03:34 +00006774 for (auto *I : ImplD->methods())
6775 Consumer->HandleInterestingDecl(DeclGroupRef(I));
Guy Benyei11169dd2012-12-18 14:30:41 +00006776
6777 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
6778}
6779
6780void ASTReader::PassInterestingDeclsToConsumer() {
6781 assert(Consumer);
Richard Smith04d05b52014-03-23 00:27:18 +00006782
6783 if (PassingDeclsToConsumer)
6784 return;
6785
6786 // Guard variable to avoid recursively redoing the process of passing
6787 // decls to consumer.
6788 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
6789 true);
6790
Richard Smith9e2341d2015-03-23 03:25:59 +00006791 // Ensure that we've loaded all potentially-interesting declarations
6792 // that need to be eagerly loaded.
6793 for (auto ID : EagerlyDeserializedDecls)
6794 GetDecl(ID);
6795 EagerlyDeserializedDecls.clear();
6796
Guy Benyei11169dd2012-12-18 14:30:41 +00006797 while (!InterestingDecls.empty()) {
6798 Decl *D = InterestingDecls.front();
6799 InterestingDecls.pop_front();
6800
6801 PassInterestingDeclToConsumer(D);
6802 }
6803}
6804
6805void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
6806 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
6807 PassObjCImplDeclToConsumer(ImplD, Consumer);
6808 else
6809 Consumer->HandleInterestingDecl(DeclGroupRef(D));
6810}
6811
6812void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
6813 this->Consumer = Consumer;
6814
Richard Smith9e2341d2015-03-23 03:25:59 +00006815 if (Consumer)
6816 PassInterestingDeclsToConsumer();
Richard Smith7f330cd2015-03-18 01:42:29 +00006817
6818 if (DeserializationListener)
6819 DeserializationListener->ReaderInitialized(this);
Guy Benyei11169dd2012-12-18 14:30:41 +00006820}
6821
6822void ASTReader::PrintStats() {
6823 std::fprintf(stderr, "*** AST File Statistics:\n");
6824
6825 unsigned NumTypesLoaded
6826 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
6827 QualType());
6828 unsigned NumDeclsLoaded
6829 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
Craig Toppera13603a2014-05-22 05:54:18 +00006830 (Decl *)nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00006831 unsigned NumIdentifiersLoaded
6832 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
6833 IdentifiersLoaded.end(),
Craig Toppera13603a2014-05-22 05:54:18 +00006834 (IdentifierInfo *)nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00006835 unsigned NumMacrosLoaded
6836 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
6837 MacrosLoaded.end(),
Craig Toppera13603a2014-05-22 05:54:18 +00006838 (MacroInfo *)nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00006839 unsigned NumSelectorsLoaded
6840 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
6841 SelectorsLoaded.end(),
6842 Selector());
6843
6844 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
6845 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
6846 NumSLocEntriesRead, TotalNumSLocEntries,
6847 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
6848 if (!TypesLoaded.empty())
6849 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
6850 NumTypesLoaded, (unsigned)TypesLoaded.size(),
6851 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
6852 if (!DeclsLoaded.empty())
6853 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
6854 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
6855 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
6856 if (!IdentifiersLoaded.empty())
6857 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
6858 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
6859 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
6860 if (!MacrosLoaded.empty())
6861 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
6862 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
6863 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
6864 if (!SelectorsLoaded.empty())
6865 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
6866 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
6867 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
6868 if (TotalNumStatements)
6869 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
6870 NumStatementsRead, TotalNumStatements,
6871 ((float)NumStatementsRead/TotalNumStatements * 100));
6872 if (TotalNumMacros)
6873 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
6874 NumMacrosRead, TotalNumMacros,
6875 ((float)NumMacrosRead/TotalNumMacros * 100));
6876 if (TotalLexicalDeclContexts)
6877 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
6878 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
6879 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
6880 * 100));
6881 if (TotalVisibleDeclContexts)
6882 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
6883 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
6884 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
6885 * 100));
6886 if (TotalNumMethodPoolEntries) {
6887 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
6888 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
6889 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
6890 * 100));
Guy Benyei11169dd2012-12-18 14:30:41 +00006891 }
Douglas Gregorad2f7a52013-01-28 17:54:36 +00006892 if (NumMethodPoolLookups) {
6893 std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n",
6894 NumMethodPoolHits, NumMethodPoolLookups,
6895 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
6896 }
6897 if (NumMethodPoolTableLookups) {
6898 std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n",
6899 NumMethodPoolTableHits, NumMethodPoolTableLookups,
6900 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
6901 * 100.0));
6902 }
6903
Douglas Gregor00a50f72013-01-25 00:38:33 +00006904 if (NumIdentifierLookupHits) {
6905 std::fprintf(stderr,
6906 " %u / %u identifier table lookups succeeded (%f%%)\n",
6907 NumIdentifierLookupHits, NumIdentifierLookups,
6908 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
6909 }
6910
Douglas Gregore060e572013-01-25 01:03:03 +00006911 if (GlobalIndex) {
6912 std::fprintf(stderr, "\n");
6913 GlobalIndex->printStats();
6914 }
6915
Guy Benyei11169dd2012-12-18 14:30:41 +00006916 std::fprintf(stderr, "\n");
6917 dump();
6918 std::fprintf(stderr, "\n");
6919}
6920
6921template<typename Key, typename ModuleFile, unsigned InitialCapacity>
6922static void
6923dumpModuleIDMap(StringRef Name,
6924 const ContinuousRangeMap<Key, ModuleFile *,
6925 InitialCapacity> &Map) {
6926 if (Map.begin() == Map.end())
6927 return;
6928
6929 typedef ContinuousRangeMap<Key, ModuleFile *, InitialCapacity> MapType;
6930 llvm::errs() << Name << ":\n";
6931 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
6932 I != IEnd; ++I) {
6933 llvm::errs() << " " << I->first << " -> " << I->second->FileName
6934 << "\n";
6935 }
6936}
6937
6938void ASTReader::dump() {
6939 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
6940 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
6941 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
6942 dumpModuleIDMap("Global type map", GlobalTypeMap);
6943 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
6944 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
6945 dumpModuleIDMap("Global macro map", GlobalMacroMap);
6946 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
6947 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
6948 dumpModuleIDMap("Global preprocessed entity map",
6949 GlobalPreprocessedEntityMap);
6950
6951 llvm::errs() << "\n*** PCH/Modules Loaded:";
6952 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
6953 MEnd = ModuleMgr.end();
6954 M != MEnd; ++M)
6955 (*M)->dump();
6956}
6957
6958/// Return the amount of memory used by memory buffers, breaking down
6959/// by heap-backed versus mmap'ed memory.
6960void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
6961 for (ModuleConstIterator I = ModuleMgr.begin(),
6962 E = ModuleMgr.end(); I != E; ++I) {
6963 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
6964 size_t bytes = buf->getBufferSize();
6965 switch (buf->getBufferKind()) {
6966 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
6967 sizes.malloc_bytes += bytes;
6968 break;
6969 case llvm::MemoryBuffer::MemoryBuffer_MMap:
6970 sizes.mmap_bytes += bytes;
6971 break;
6972 }
6973 }
6974 }
6975}
6976
6977void ASTReader::InitializeSema(Sema &S) {
6978 SemaObj = &S;
6979 S.addExternalSource(this);
6980
6981 // Makes sure any declarations that were deserialized "too early"
6982 // still get added to the identifier's declaration chains.
Ben Langmuir5418f402014-09-10 21:29:41 +00006983 for (uint64_t ID : PreloadedDeclIDs) {
6984 NamedDecl *D = cast<NamedDecl>(GetDecl(ID));
6985 pushExternalDeclIntoScope(D, D->getDeclName());
Guy Benyei11169dd2012-12-18 14:30:41 +00006986 }
Ben Langmuir5418f402014-09-10 21:29:41 +00006987 PreloadedDeclIDs.clear();
Guy Benyei11169dd2012-12-18 14:30:41 +00006988
Richard Smith3d8e97e2013-10-18 06:54:39 +00006989 // FIXME: What happens if these are changed by a module import?
Guy Benyei11169dd2012-12-18 14:30:41 +00006990 if (!FPPragmaOptions.empty()) {
6991 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
6992 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
6993 }
6994
Richard Smith3d8e97e2013-10-18 06:54:39 +00006995 // FIXME: What happens if these are changed by a module import?
Guy Benyei11169dd2012-12-18 14:30:41 +00006996 if (!OpenCLExtensions.empty()) {
6997 unsigned I = 0;
6998#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
6999#include "clang/Basic/OpenCLExtensions.def"
7000
7001 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
7002 }
Richard Smith3d8e97e2013-10-18 06:54:39 +00007003
7004 UpdateSema();
7005}
7006
7007void ASTReader::UpdateSema() {
7008 assert(SemaObj && "no Sema to update");
7009
7010 // Load the offsets of the declarations that Sema references.
7011 // They will be lazily deserialized when needed.
7012 if (!SemaDeclRefs.empty()) {
7013 assert(SemaDeclRefs.size() % 2 == 0);
7014 for (unsigned I = 0; I != SemaDeclRefs.size(); I += 2) {
7015 if (!SemaObj->StdNamespace)
7016 SemaObj->StdNamespace = SemaDeclRefs[I];
7017 if (!SemaObj->StdBadAlloc)
7018 SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
7019 }
7020 SemaDeclRefs.clear();
7021 }
Dario Domizioli13a0a382014-05-23 12:13:25 +00007022
7023 // Update the state of 'pragma clang optimize'. Use the same API as if we had
7024 // encountered the pragma in the source.
7025 if(OptimizeOffPragmaLocation.isValid())
7026 SemaObj->ActOnPragmaOptimize(/* IsOn = */ false, OptimizeOffPragmaLocation);
Guy Benyei11169dd2012-12-18 14:30:41 +00007027}
7028
7029IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
7030 // Note that we are loading an identifier.
7031 Deserializing AnIdentifier(this);
Douglas Gregore060e572013-01-25 01:03:03 +00007032 StringRef Name(NameStart, NameEnd - NameStart);
7033
7034 // If there is a global index, look there first to determine which modules
7035 // provably do not have any results for this identifier.
Douglas Gregor7211ac12013-01-25 23:32:03 +00007036 GlobalModuleIndex::HitSet Hits;
Craig Toppera13603a2014-05-22 05:54:18 +00007037 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
Douglas Gregore060e572013-01-25 01:03:03 +00007038 if (!loadGlobalIndex()) {
Douglas Gregor7211ac12013-01-25 23:32:03 +00007039 if (GlobalIndex->lookupIdentifier(Name, Hits)) {
7040 HitsPtr = &Hits;
Douglas Gregore060e572013-01-25 01:03:03 +00007041 }
7042 }
Douglas Gregor7211ac12013-01-25 23:32:03 +00007043 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
Douglas Gregor00a50f72013-01-25 00:38:33 +00007044 NumIdentifierLookups,
7045 NumIdentifierLookupHits);
Douglas Gregor7211ac12013-01-25 23:32:03 +00007046 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor, HitsPtr);
Guy Benyei11169dd2012-12-18 14:30:41 +00007047 IdentifierInfo *II = Visitor.getIdentifierInfo();
7048 markIdentifierUpToDate(II);
7049 return II;
7050}
7051
7052namespace clang {
7053 /// \brief An identifier-lookup iterator that enumerates all of the
7054 /// identifiers stored within a set of AST files.
7055 class ASTIdentifierIterator : public IdentifierIterator {
7056 /// \brief The AST reader whose identifiers are being enumerated.
7057 const ASTReader &Reader;
7058
7059 /// \brief The current index into the chain of AST files stored in
7060 /// the AST reader.
7061 unsigned Index;
7062
7063 /// \brief The current position within the identifier lookup table
7064 /// of the current AST file.
7065 ASTIdentifierLookupTable::key_iterator Current;
7066
7067 /// \brief The end position within the identifier lookup table of
7068 /// the current AST file.
7069 ASTIdentifierLookupTable::key_iterator End;
7070
7071 public:
7072 explicit ASTIdentifierIterator(const ASTReader &Reader);
7073
Craig Topper3e89dfe2014-03-13 02:13:41 +00007074 StringRef Next() override;
Guy Benyei11169dd2012-12-18 14:30:41 +00007075 };
7076}
7077
7078ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
7079 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
7080 ASTIdentifierLookupTable *IdTable
7081 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
7082 Current = IdTable->key_begin();
7083 End = IdTable->key_end();
7084}
7085
7086StringRef ASTIdentifierIterator::Next() {
7087 while (Current == End) {
7088 // If we have exhausted all of our AST files, we're done.
7089 if (Index == 0)
7090 return StringRef();
7091
7092 --Index;
7093 ASTIdentifierLookupTable *IdTable
7094 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
7095 IdentifierLookupTable;
7096 Current = IdTable->key_begin();
7097 End = IdTable->key_end();
7098 }
7099
7100 // We have any identifiers remaining in the current AST file; return
7101 // the next one.
Douglas Gregorbfd73d72013-01-23 18:53:14 +00007102 StringRef Result = *Current;
Guy Benyei11169dd2012-12-18 14:30:41 +00007103 ++Current;
Douglas Gregorbfd73d72013-01-23 18:53:14 +00007104 return Result;
Guy Benyei11169dd2012-12-18 14:30:41 +00007105}
7106
Argyrios Kyrtzidis9aca3c62013-04-17 22:10:55 +00007107IdentifierIterator *ASTReader::getIdentifiers() {
7108 if (!loadGlobalIndex())
7109 return GlobalIndex->createIdentifierIterator();
7110
Guy Benyei11169dd2012-12-18 14:30:41 +00007111 return new ASTIdentifierIterator(*this);
7112}
7113
7114namespace clang { namespace serialization {
7115 class ReadMethodPoolVisitor {
7116 ASTReader &Reader;
7117 Selector Sel;
7118 unsigned PriorGeneration;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00007119 unsigned InstanceBits;
7120 unsigned FactoryBits;
Nico Weberff4b35e2014-12-27 22:14:15 +00007121 bool InstanceHasMoreThanOneDecl;
7122 bool FactoryHasMoreThanOneDecl;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00007123 SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
7124 SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
Guy Benyei11169dd2012-12-18 14:30:41 +00007125
7126 public:
Nico Weber2e0c8f72014-12-27 03:58:08 +00007127 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
Guy Benyei11169dd2012-12-18 14:30:41 +00007128 unsigned PriorGeneration)
Nico Weber2e0c8f72014-12-27 03:58:08 +00007129 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration),
Nico Weberff4b35e2014-12-27 22:14:15 +00007130 InstanceBits(0), FactoryBits(0), InstanceHasMoreThanOneDecl(false),
7131 FactoryHasMoreThanOneDecl(false) {}
Nico Weber2e0c8f72014-12-27 03:58:08 +00007132
Guy Benyei11169dd2012-12-18 14:30:41 +00007133 static bool visit(ModuleFile &M, void *UserData) {
7134 ReadMethodPoolVisitor *This
7135 = static_cast<ReadMethodPoolVisitor *>(UserData);
7136
7137 if (!M.SelectorLookupTable)
7138 return false;
7139
7140 // If we've already searched this module file, skip it now.
7141 if (M.Generation <= This->PriorGeneration)
7142 return true;
7143
Douglas Gregorad2f7a52013-01-28 17:54:36 +00007144 ++This->Reader.NumMethodPoolTableLookups;
Guy Benyei11169dd2012-12-18 14:30:41 +00007145 ASTSelectorLookupTable *PoolTable
7146 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
7147 ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel);
7148 if (Pos == PoolTable->end())
7149 return false;
Douglas Gregorad2f7a52013-01-28 17:54:36 +00007150
7151 ++This->Reader.NumMethodPoolTableHits;
Guy Benyei11169dd2012-12-18 14:30:41 +00007152 ++This->Reader.NumSelectorsRead;
7153 // FIXME: Not quite happy with the statistics here. We probably should
7154 // disable this tracking when called via LoadSelector.
7155 // Also, should entries without methods count as misses?
7156 ++This->Reader.NumMethodPoolEntriesRead;
7157 ASTSelectorLookupTrait::data_type Data = *Pos;
7158 if (This->Reader.DeserializationListener)
7159 This->Reader.DeserializationListener->SelectorRead(Data.ID,
7160 This->Sel);
7161
7162 This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
7163 This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00007164 This->InstanceBits = Data.InstanceBits;
7165 This->FactoryBits = Data.FactoryBits;
Nico Weberff4b35e2014-12-27 22:14:15 +00007166 This->InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
7167 This->FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00007168 return true;
7169 }
7170
7171 /// \brief Retrieve the instance methods found by this visitor.
7172 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
7173 return InstanceMethods;
7174 }
7175
7176 /// \brief Retrieve the instance methods found by this visitor.
7177 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
7178 return FactoryMethods;
7179 }
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00007180
7181 unsigned getInstanceBits() const { return InstanceBits; }
7182 unsigned getFactoryBits() const { return FactoryBits; }
Nico Weberff4b35e2014-12-27 22:14:15 +00007183 bool instanceHasMoreThanOneDecl() const {
7184 return InstanceHasMoreThanOneDecl;
7185 }
7186 bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
Guy Benyei11169dd2012-12-18 14:30:41 +00007187 };
7188} } // end namespace clang::serialization
7189
7190/// \brief Add the given set of methods to the method list.
7191static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
7192 ObjCMethodList &List) {
7193 for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
7194 S.addMethodToGlobalList(&List, Methods[I]);
7195 }
7196}
7197
7198void ASTReader::ReadMethodPool(Selector Sel) {
7199 // Get the selector generation and update it to the current generation.
7200 unsigned &Generation = SelectorGeneration[Sel];
7201 unsigned PriorGeneration = Generation;
Richard Smith053f6c62014-05-16 23:01:30 +00007202 Generation = getGeneration();
Guy Benyei11169dd2012-12-18 14:30:41 +00007203
7204 // Search for methods defined with this selector.
Douglas Gregorad2f7a52013-01-28 17:54:36 +00007205 ++NumMethodPoolLookups;
Guy Benyei11169dd2012-12-18 14:30:41 +00007206 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
7207 ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor);
7208
7209 if (Visitor.getInstanceMethods().empty() &&
Douglas Gregorad2f7a52013-01-28 17:54:36 +00007210 Visitor.getFactoryMethods().empty())
Guy Benyei11169dd2012-12-18 14:30:41 +00007211 return;
Douglas Gregorad2f7a52013-01-28 17:54:36 +00007212
7213 ++NumMethodPoolHits;
7214
Guy Benyei11169dd2012-12-18 14:30:41 +00007215 if (!getSema())
7216 return;
7217
7218 Sema &S = *getSema();
7219 Sema::GlobalMethodPool::iterator Pos
7220 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
Ben Langmuira0c32e92015-01-12 19:27:00 +00007221
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00007222 Pos->second.first.setBits(Visitor.getInstanceBits());
Nico Weberff4b35e2014-12-27 22:14:15 +00007223 Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00007224 Pos->second.second.setBits(Visitor.getFactoryBits());
Nico Weberff4b35e2014-12-27 22:14:15 +00007225 Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
Ben Langmuira0c32e92015-01-12 19:27:00 +00007226
7227 // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
7228 // when building a module we keep every method individually and may need to
7229 // update hasMoreThanOneDecl as we add the methods.
7230 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
7231 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
Guy Benyei11169dd2012-12-18 14:30:41 +00007232}
7233
7234void ASTReader::ReadKnownNamespaces(
7235 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
7236 Namespaces.clear();
7237
7238 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
7239 if (NamespaceDecl *Namespace
7240 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
7241 Namespaces.push_back(Namespace);
7242 }
7243}
7244
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00007245void ASTReader::ReadUndefinedButUsed(
Nick Lewyckyf0f56162013-01-31 03:23:57 +00007246 llvm::DenseMap<NamedDecl*, SourceLocation> &Undefined) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00007247 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
7248 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
Nick Lewycky8334af82013-01-26 00:35:08 +00007249 SourceLocation Loc =
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00007250 SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
Nick Lewycky8334af82013-01-26 00:35:08 +00007251 Undefined.insert(std::make_pair(D, Loc));
7252 }
7253}
Nick Lewycky8334af82013-01-26 00:35:08 +00007254
Guy Benyei11169dd2012-12-18 14:30:41 +00007255void ASTReader::ReadTentativeDefinitions(
7256 SmallVectorImpl<VarDecl *> &TentativeDefs) {
7257 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
7258 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
7259 if (Var)
7260 TentativeDefs.push_back(Var);
7261 }
7262 TentativeDefinitions.clear();
7263}
7264
7265void ASTReader::ReadUnusedFileScopedDecls(
7266 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
7267 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
7268 DeclaratorDecl *D
7269 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
7270 if (D)
7271 Decls.push_back(D);
7272 }
7273 UnusedFileScopedDecls.clear();
7274}
7275
7276void ASTReader::ReadDelegatingConstructors(
7277 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
7278 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
7279 CXXConstructorDecl *D
7280 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
7281 if (D)
7282 Decls.push_back(D);
7283 }
7284 DelegatingCtorDecls.clear();
7285}
7286
7287void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
7288 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
7289 TypedefNameDecl *D
7290 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
7291 if (D)
7292 Decls.push_back(D);
7293 }
7294 ExtVectorDecls.clear();
7295}
7296
Nico Weber72889432014-09-06 01:25:55 +00007297void ASTReader::ReadUnusedLocalTypedefNameCandidates(
7298 llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
7299 for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
7300 ++I) {
7301 TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
7302 GetDecl(UnusedLocalTypedefNameCandidates[I]));
7303 if (D)
7304 Decls.insert(D);
7305 }
7306 UnusedLocalTypedefNameCandidates.clear();
7307}
7308
Guy Benyei11169dd2012-12-18 14:30:41 +00007309void ASTReader::ReadReferencedSelectors(
7310 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
7311 if (ReferencedSelectorsData.empty())
7312 return;
7313
7314 // If there are @selector references added them to its pool. This is for
7315 // implementation of -Wselector.
7316 unsigned int DataSize = ReferencedSelectorsData.size()-1;
7317 unsigned I = 0;
7318 while (I < DataSize) {
7319 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
7320 SourceLocation SelLoc
7321 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
7322 Sels.push_back(std::make_pair(Sel, SelLoc));
7323 }
7324 ReferencedSelectorsData.clear();
7325}
7326
7327void ASTReader::ReadWeakUndeclaredIdentifiers(
7328 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
7329 if (WeakUndeclaredIdentifiers.empty())
7330 return;
7331
7332 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
7333 IdentifierInfo *WeakId
7334 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
7335 IdentifierInfo *AliasId
7336 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
7337 SourceLocation Loc
7338 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
7339 bool Used = WeakUndeclaredIdentifiers[I++];
7340 WeakInfo WI(AliasId, Loc);
7341 WI.setUsed(Used);
7342 WeakIDs.push_back(std::make_pair(WeakId, WI));
7343 }
7344 WeakUndeclaredIdentifiers.clear();
7345}
7346
7347void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
7348 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
7349 ExternalVTableUse VT;
7350 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
7351 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
7352 VT.DefinitionRequired = VTableUses[Idx++];
7353 VTables.push_back(VT);
7354 }
7355
7356 VTableUses.clear();
7357}
7358
7359void ASTReader::ReadPendingInstantiations(
7360 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
7361 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
7362 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
7363 SourceLocation Loc
7364 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
7365
7366 Pending.push_back(std::make_pair(D, Loc));
7367 }
7368 PendingInstantiations.clear();
7369}
7370
Richard Smithe40f2ba2013-08-07 21:41:30 +00007371void ASTReader::ReadLateParsedTemplates(
Chandler Carruth52cee4d2015-03-26 09:08:15 +00007372 llvm::MapVector<const FunctionDecl *, LateParsedTemplate *> &LPTMap) {
Richard Smithe40f2ba2013-08-07 21:41:30 +00007373 for (unsigned Idx = 0, N = LateParsedTemplates.size(); Idx < N;
7374 /* In loop */) {
7375 FunctionDecl *FD = cast<FunctionDecl>(GetDecl(LateParsedTemplates[Idx++]));
7376
7377 LateParsedTemplate *LT = new LateParsedTemplate;
7378 LT->D = GetDecl(LateParsedTemplates[Idx++]);
7379
7380 ModuleFile *F = getOwningModuleFile(LT->D);
7381 assert(F && "No module");
7382
7383 unsigned TokN = LateParsedTemplates[Idx++];
7384 LT->Toks.reserve(TokN);
7385 for (unsigned T = 0; T < TokN; ++T)
7386 LT->Toks.push_back(ReadToken(*F, LateParsedTemplates, Idx));
7387
Chandler Carruth52cee4d2015-03-26 09:08:15 +00007388 LPTMap.insert(std::make_pair(FD, LT));
Richard Smithe40f2ba2013-08-07 21:41:30 +00007389 }
7390
7391 LateParsedTemplates.clear();
7392}
7393
Guy Benyei11169dd2012-12-18 14:30:41 +00007394void ASTReader::LoadSelector(Selector Sel) {
7395 // It would be complicated to avoid reading the methods anyway. So don't.
7396 ReadMethodPool(Sel);
7397}
7398
7399void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
7400 assert(ID && "Non-zero identifier ID required");
7401 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
7402 IdentifiersLoaded[ID - 1] = II;
7403 if (DeserializationListener)
7404 DeserializationListener->IdentifierRead(ID, II);
7405}
7406
7407/// \brief Set the globally-visible declarations associated with the given
7408/// identifier.
7409///
7410/// If the AST reader is currently in a state where the given declaration IDs
7411/// cannot safely be resolved, they are queued until it is safe to resolve
7412/// them.
7413///
7414/// \param II an IdentifierInfo that refers to one or more globally-visible
7415/// declarations.
7416///
7417/// \param DeclIDs the set of declaration IDs with the name @p II that are
7418/// visible at global scope.
7419///
Douglas Gregor6168bd22013-02-18 15:53:43 +00007420/// \param Decls if non-null, this vector will be populated with the set of
7421/// deserialized declarations. These declarations will not be pushed into
7422/// scope.
Guy Benyei11169dd2012-12-18 14:30:41 +00007423void
7424ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
7425 const SmallVectorImpl<uint32_t> &DeclIDs,
Douglas Gregor6168bd22013-02-18 15:53:43 +00007426 SmallVectorImpl<Decl *> *Decls) {
7427 if (NumCurrentElementsDeserializing && !Decls) {
7428 PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
Guy Benyei11169dd2012-12-18 14:30:41 +00007429 return;
7430 }
7431
7432 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
Ben Langmuir5418f402014-09-10 21:29:41 +00007433 if (!SemaObj) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007434 // Queue this declaration so that it will be added to the
7435 // translation unit scope and identifier's declaration chain
7436 // once a Sema object is known.
Ben Langmuir5418f402014-09-10 21:29:41 +00007437 PreloadedDeclIDs.push_back(DeclIDs[I]);
7438 continue;
Guy Benyei11169dd2012-12-18 14:30:41 +00007439 }
Ben Langmuir5418f402014-09-10 21:29:41 +00007440
7441 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
7442
7443 // If we're simply supposed to record the declarations, do so now.
7444 if (Decls) {
7445 Decls->push_back(D);
7446 continue;
7447 }
7448
7449 // Introduce this declaration into the translation-unit scope
7450 // and add it to the declaration chain for this identifier, so
7451 // that (unqualified) name lookup will find it.
7452 pushExternalDeclIntoScope(D, II);
Guy Benyei11169dd2012-12-18 14:30:41 +00007453 }
7454}
7455
Douglas Gregorc8a992f2013-01-21 16:52:34 +00007456IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007457 if (ID == 0)
Craig Toppera13603a2014-05-22 05:54:18 +00007458 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007459
7460 if (IdentifiersLoaded.empty()) {
7461 Error("no identifier table in AST file");
Craig Toppera13603a2014-05-22 05:54:18 +00007462 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007463 }
7464
7465 ID -= 1;
7466 if (!IdentifiersLoaded[ID]) {
7467 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
7468 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
7469 ModuleFile *M = I->second;
7470 unsigned Index = ID - M->BaseIdentifierID;
7471 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
7472
7473 // All of the strings in the AST file are preceded by a 16-bit length.
7474 // Extract that 16-bit length to avoid having to execute strlen().
7475 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
7476 // unsigned integers. This is important to avoid integer overflow when
7477 // we cast them to 'unsigned'.
7478 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
7479 unsigned StrLen = (((unsigned) StrLenPtr[0])
7480 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Douglas Gregorc8a992f2013-01-21 16:52:34 +00007481 IdentifiersLoaded[ID]
7482 = &PP.getIdentifierTable().get(StringRef(Str, StrLen));
Guy Benyei11169dd2012-12-18 14:30:41 +00007483 if (DeserializationListener)
7484 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
7485 }
7486
7487 return IdentifiersLoaded[ID];
7488}
7489
Douglas Gregorc8a992f2013-01-21 16:52:34 +00007490IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
7491 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
Guy Benyei11169dd2012-12-18 14:30:41 +00007492}
7493
7494IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
7495 if (LocalID < NUM_PREDEF_IDENT_IDS)
7496 return LocalID;
7497
7498 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7499 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
7500 assert(I != M.IdentifierRemap.end()
7501 && "Invalid index into identifier index remap");
7502
7503 return LocalID + I->second;
7504}
7505
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00007506MacroInfo *ASTReader::getMacro(MacroID ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007507 if (ID == 0)
Craig Toppera13603a2014-05-22 05:54:18 +00007508 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007509
7510 if (MacrosLoaded.empty()) {
7511 Error("no macro table in AST file");
Craig Toppera13603a2014-05-22 05:54:18 +00007512 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007513 }
7514
7515 ID -= NUM_PREDEF_MACRO_IDS;
7516 if (!MacrosLoaded[ID]) {
7517 GlobalMacroMapType::iterator I
7518 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
7519 assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
7520 ModuleFile *M = I->second;
7521 unsigned Index = ID - M->BaseMacroID;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00007522 MacrosLoaded[ID] = ReadMacroRecord(*M, M->MacroOffsets[Index]);
7523
7524 if (DeserializationListener)
7525 DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
7526 MacrosLoaded[ID]);
Guy Benyei11169dd2012-12-18 14:30:41 +00007527 }
7528
7529 return MacrosLoaded[ID];
7530}
7531
7532MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
7533 if (LocalID < NUM_PREDEF_MACRO_IDS)
7534 return LocalID;
7535
7536 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7537 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
7538 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
7539
7540 return LocalID + I->second;
7541}
7542
7543serialization::SubmoduleID
7544ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
7545 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
7546 return LocalID;
7547
7548 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7549 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
7550 assert(I != M.SubmoduleRemap.end()
7551 && "Invalid index into submodule index remap");
7552
7553 return LocalID + I->second;
7554}
7555
7556Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
7557 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
7558 assert(GlobalID == 0 && "Unhandled global submodule ID");
Craig Toppera13603a2014-05-22 05:54:18 +00007559 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007560 }
7561
7562 if (GlobalID > SubmodulesLoaded.size()) {
7563 Error("submodule ID out of range in AST file");
Craig Toppera13603a2014-05-22 05:54:18 +00007564 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007565 }
7566
7567 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
7568}
Douglas Gregorc147b0b2013-01-12 01:29:50 +00007569
7570Module *ASTReader::getModule(unsigned ID) {
7571 return getSubmodule(ID);
7572}
7573
Guy Benyei11169dd2012-12-18 14:30:41 +00007574Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
7575 return DecodeSelector(getGlobalSelectorID(M, LocalID));
7576}
7577
7578Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
7579 if (ID == 0)
7580 return Selector();
7581
7582 if (ID > SelectorsLoaded.size()) {
7583 Error("selector ID out of range in AST file");
7584 return Selector();
7585 }
7586
Craig Toppera13603a2014-05-22 05:54:18 +00007587 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007588 // Load this selector from the selector table.
7589 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
7590 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
7591 ModuleFile &M = *I->second;
7592 ASTSelectorLookupTrait Trait(*this, M);
7593 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
7594 SelectorsLoaded[ID - 1] =
7595 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
7596 if (DeserializationListener)
7597 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
7598 }
7599
7600 return SelectorsLoaded[ID - 1];
7601}
7602
7603Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
7604 return DecodeSelector(ID);
7605}
7606
7607uint32_t ASTReader::GetNumExternalSelectors() {
7608 // ID 0 (the null selector) is considered an external selector.
7609 return getTotalNumSelectors() + 1;
7610}
7611
7612serialization::SelectorID
7613ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
7614 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
7615 return LocalID;
7616
7617 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7618 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
7619 assert(I != M.SelectorRemap.end()
7620 && "Invalid index into selector index remap");
7621
7622 return LocalID + I->second;
7623}
7624
7625DeclarationName
7626ASTReader::ReadDeclarationName(ModuleFile &F,
7627 const RecordData &Record, unsigned &Idx) {
7628 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
7629 switch (Kind) {
7630 case DeclarationName::Identifier:
Douglas Gregorc8a992f2013-01-21 16:52:34 +00007631 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00007632
7633 case DeclarationName::ObjCZeroArgSelector:
7634 case DeclarationName::ObjCOneArgSelector:
7635 case DeclarationName::ObjCMultiArgSelector:
7636 return DeclarationName(ReadSelector(F, Record, Idx));
7637
7638 case DeclarationName::CXXConstructorName:
7639 return Context.DeclarationNames.getCXXConstructorName(
7640 Context.getCanonicalType(readType(F, Record, Idx)));
7641
7642 case DeclarationName::CXXDestructorName:
7643 return Context.DeclarationNames.getCXXDestructorName(
7644 Context.getCanonicalType(readType(F, Record, Idx)));
7645
7646 case DeclarationName::CXXConversionFunctionName:
7647 return Context.DeclarationNames.getCXXConversionFunctionName(
7648 Context.getCanonicalType(readType(F, Record, Idx)));
7649
7650 case DeclarationName::CXXOperatorName:
7651 return Context.DeclarationNames.getCXXOperatorName(
7652 (OverloadedOperatorKind)Record[Idx++]);
7653
7654 case DeclarationName::CXXLiteralOperatorName:
7655 return Context.DeclarationNames.getCXXLiteralOperatorName(
7656 GetIdentifierInfo(F, Record, Idx));
7657
7658 case DeclarationName::CXXUsingDirective:
7659 return DeclarationName::getUsingDirectiveName();
7660 }
7661
7662 llvm_unreachable("Invalid NameKind!");
7663}
7664
7665void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
7666 DeclarationNameLoc &DNLoc,
7667 DeclarationName Name,
7668 const RecordData &Record, unsigned &Idx) {
7669 switch (Name.getNameKind()) {
7670 case DeclarationName::CXXConstructorName:
7671 case DeclarationName::CXXDestructorName:
7672 case DeclarationName::CXXConversionFunctionName:
7673 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
7674 break;
7675
7676 case DeclarationName::CXXOperatorName:
7677 DNLoc.CXXOperatorName.BeginOpNameLoc
7678 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
7679 DNLoc.CXXOperatorName.EndOpNameLoc
7680 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
7681 break;
7682
7683 case DeclarationName::CXXLiteralOperatorName:
7684 DNLoc.CXXLiteralOperatorName.OpNameLoc
7685 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
7686 break;
7687
7688 case DeclarationName::Identifier:
7689 case DeclarationName::ObjCZeroArgSelector:
7690 case DeclarationName::ObjCOneArgSelector:
7691 case DeclarationName::ObjCMultiArgSelector:
7692 case DeclarationName::CXXUsingDirective:
7693 break;
7694 }
7695}
7696
7697void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
7698 DeclarationNameInfo &NameInfo,
7699 const RecordData &Record, unsigned &Idx) {
7700 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
7701 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
7702 DeclarationNameLoc DNLoc;
7703 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
7704 NameInfo.setInfo(DNLoc);
7705}
7706
7707void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
7708 const RecordData &Record, unsigned &Idx) {
7709 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
7710 unsigned NumTPLists = Record[Idx++];
7711 Info.NumTemplParamLists = NumTPLists;
7712 if (NumTPLists) {
7713 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
7714 for (unsigned i=0; i != NumTPLists; ++i)
7715 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
7716 }
7717}
7718
7719TemplateName
7720ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
7721 unsigned &Idx) {
7722 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
7723 switch (Kind) {
7724 case TemplateName::Template:
7725 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
7726
7727 case TemplateName::OverloadedTemplate: {
7728 unsigned size = Record[Idx++];
7729 UnresolvedSet<8> Decls;
7730 while (size--)
7731 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
7732
7733 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
7734 }
7735
7736 case TemplateName::QualifiedTemplate: {
7737 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
7738 bool hasTemplKeyword = Record[Idx++];
7739 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
7740 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
7741 }
7742
7743 case TemplateName::DependentTemplate: {
7744 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
7745 if (Record[Idx++]) // isIdentifier
7746 return Context.getDependentTemplateName(NNS,
7747 GetIdentifierInfo(F, Record,
7748 Idx));
7749 return Context.getDependentTemplateName(NNS,
7750 (OverloadedOperatorKind)Record[Idx++]);
7751 }
7752
7753 case TemplateName::SubstTemplateTemplateParm: {
7754 TemplateTemplateParmDecl *param
7755 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
7756 if (!param) return TemplateName();
7757 TemplateName replacement = ReadTemplateName(F, Record, Idx);
7758 return Context.getSubstTemplateTemplateParm(param, replacement);
7759 }
7760
7761 case TemplateName::SubstTemplateTemplateParmPack: {
7762 TemplateTemplateParmDecl *Param
7763 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
7764 if (!Param)
7765 return TemplateName();
7766
7767 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
7768 if (ArgPack.getKind() != TemplateArgument::Pack)
7769 return TemplateName();
7770
7771 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
7772 }
7773 }
7774
7775 llvm_unreachable("Unhandled template name kind!");
7776}
7777
7778TemplateArgument
7779ASTReader::ReadTemplateArgument(ModuleFile &F,
7780 const RecordData &Record, unsigned &Idx) {
7781 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
7782 switch (Kind) {
7783 case TemplateArgument::Null:
7784 return TemplateArgument();
7785 case TemplateArgument::Type:
7786 return TemplateArgument(readType(F, Record, Idx));
7787 case TemplateArgument::Declaration: {
7788 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
David Blaikie0f62c8d2014-10-16 04:21:25 +00007789 return TemplateArgument(D, readType(F, Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00007790 }
7791 case TemplateArgument::NullPtr:
7792 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
7793 case TemplateArgument::Integral: {
7794 llvm::APSInt Value = ReadAPSInt(Record, Idx);
7795 QualType T = readType(F, Record, Idx);
7796 return TemplateArgument(Context, Value, T);
7797 }
7798 case TemplateArgument::Template:
7799 return TemplateArgument(ReadTemplateName(F, Record, Idx));
7800 case TemplateArgument::TemplateExpansion: {
7801 TemplateName Name = ReadTemplateName(F, Record, Idx);
David Blaikie05785d12013-02-20 22:23:23 +00007802 Optional<unsigned> NumTemplateExpansions;
Guy Benyei11169dd2012-12-18 14:30:41 +00007803 if (unsigned NumExpansions = Record[Idx++])
7804 NumTemplateExpansions = NumExpansions - 1;
7805 return TemplateArgument(Name, NumTemplateExpansions);
7806 }
7807 case TemplateArgument::Expression:
7808 return TemplateArgument(ReadExpr(F));
7809 case TemplateArgument::Pack: {
7810 unsigned NumArgs = Record[Idx++];
7811 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
7812 for (unsigned I = 0; I != NumArgs; ++I)
7813 Args[I] = ReadTemplateArgument(F, Record, Idx);
7814 return TemplateArgument(Args, NumArgs);
7815 }
7816 }
7817
7818 llvm_unreachable("Unhandled template argument kind!");
7819}
7820
7821TemplateParameterList *
7822ASTReader::ReadTemplateParameterList(ModuleFile &F,
7823 const RecordData &Record, unsigned &Idx) {
7824 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
7825 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
7826 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
7827
7828 unsigned NumParams = Record[Idx++];
7829 SmallVector<NamedDecl *, 16> Params;
7830 Params.reserve(NumParams);
7831 while (NumParams--)
7832 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
7833
7834 TemplateParameterList* TemplateParams =
7835 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
7836 Params.data(), Params.size(), RAngleLoc);
7837 return TemplateParams;
7838}
7839
7840void
7841ASTReader::
Craig Topper5603df42013-07-05 19:34:19 +00007842ReadTemplateArgumentList(SmallVectorImpl<TemplateArgument> &TemplArgs,
Guy Benyei11169dd2012-12-18 14:30:41 +00007843 ModuleFile &F, const RecordData &Record,
7844 unsigned &Idx) {
7845 unsigned NumTemplateArgs = Record[Idx++];
7846 TemplArgs.reserve(NumTemplateArgs);
7847 while (NumTemplateArgs--)
7848 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
7849}
7850
7851/// \brief Read a UnresolvedSet structure.
Richard Smitha4ba74c2013-08-30 04:46:40 +00007852void ASTReader::ReadUnresolvedSet(ModuleFile &F, LazyASTUnresolvedSet &Set,
Guy Benyei11169dd2012-12-18 14:30:41 +00007853 const RecordData &Record, unsigned &Idx) {
7854 unsigned NumDecls = Record[Idx++];
7855 Set.reserve(Context, NumDecls);
7856 while (NumDecls--) {
Richard Smitha4ba74c2013-08-30 04:46:40 +00007857 DeclID ID = ReadDeclID(F, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00007858 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
Richard Smitha4ba74c2013-08-30 04:46:40 +00007859 Set.addLazyDecl(Context, ID, AS);
Guy Benyei11169dd2012-12-18 14:30:41 +00007860 }
7861}
7862
7863CXXBaseSpecifier
7864ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
7865 const RecordData &Record, unsigned &Idx) {
7866 bool isVirtual = static_cast<bool>(Record[Idx++]);
7867 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
7868 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
7869 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
7870 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
7871 SourceRange Range = ReadSourceRange(F, Record, Idx);
7872 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
7873 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
7874 EllipsisLoc);
7875 Result.setInheritConstructors(inheritConstructors);
7876 return Result;
7877}
7878
Richard Smithc2bb8182015-03-24 06:36:48 +00007879CXXCtorInitializer **
Guy Benyei11169dd2012-12-18 14:30:41 +00007880ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
7881 unsigned &Idx) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007882 unsigned NumInitializers = Record[Idx++];
Richard Smithc2bb8182015-03-24 06:36:48 +00007883 assert(NumInitializers && "wrote ctor initializers but have no inits");
7884 auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
7885 for (unsigned i = 0; i != NumInitializers; ++i) {
7886 TypeSourceInfo *TInfo = nullptr;
7887 bool IsBaseVirtual = false;
7888 FieldDecl *Member = nullptr;
7889 IndirectFieldDecl *IndirectMember = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007890
Richard Smithc2bb8182015-03-24 06:36:48 +00007891 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
7892 switch (Type) {
7893 case CTOR_INITIALIZER_BASE:
7894 TInfo = GetTypeSourceInfo(F, Record, Idx);
7895 IsBaseVirtual = Record[Idx++];
7896 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00007897
Richard Smithc2bb8182015-03-24 06:36:48 +00007898 case CTOR_INITIALIZER_DELEGATING:
7899 TInfo = GetTypeSourceInfo(F, Record, Idx);
7900 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00007901
Richard Smithc2bb8182015-03-24 06:36:48 +00007902 case CTOR_INITIALIZER_MEMBER:
7903 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
7904 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00007905
Richard Smithc2bb8182015-03-24 06:36:48 +00007906 case CTOR_INITIALIZER_INDIRECT_MEMBER:
7907 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
7908 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00007909 }
Richard Smithc2bb8182015-03-24 06:36:48 +00007910
7911 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
7912 Expr *Init = ReadExpr(F);
7913 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
7914 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
7915 bool IsWritten = Record[Idx++];
7916 unsigned SourceOrderOrNumArrayIndices;
7917 SmallVector<VarDecl *, 8> Indices;
7918 if (IsWritten) {
7919 SourceOrderOrNumArrayIndices = Record[Idx++];
7920 } else {
7921 SourceOrderOrNumArrayIndices = Record[Idx++];
7922 Indices.reserve(SourceOrderOrNumArrayIndices);
7923 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
7924 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
7925 }
7926
7927 CXXCtorInitializer *BOMInit;
7928 if (Type == CTOR_INITIALIZER_BASE) {
7929 BOMInit = new (Context)
7930 CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
7931 RParenLoc, MemberOrEllipsisLoc);
7932 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
7933 BOMInit = new (Context)
7934 CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
7935 } else if (IsWritten) {
7936 if (Member)
7937 BOMInit = new (Context) CXXCtorInitializer(
7938 Context, Member, MemberOrEllipsisLoc, LParenLoc, Init, RParenLoc);
7939 else
7940 BOMInit = new (Context)
7941 CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
7942 LParenLoc, Init, RParenLoc);
7943 } else {
7944 if (IndirectMember) {
7945 assert(Indices.empty() && "Indirect field improperly initialized");
7946 BOMInit = new (Context)
7947 CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
7948 LParenLoc, Init, RParenLoc);
7949 } else {
7950 BOMInit = CXXCtorInitializer::Create(
7951 Context, Member, MemberOrEllipsisLoc, LParenLoc, Init, RParenLoc,
7952 Indices.data(), Indices.size());
7953 }
7954 }
7955
7956 if (IsWritten)
7957 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
7958 CtorInitializers[i] = BOMInit;
Guy Benyei11169dd2012-12-18 14:30:41 +00007959 }
7960
Richard Smithc2bb8182015-03-24 06:36:48 +00007961 return CtorInitializers;
Guy Benyei11169dd2012-12-18 14:30:41 +00007962}
7963
7964NestedNameSpecifier *
7965ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
7966 const RecordData &Record, unsigned &Idx) {
7967 unsigned N = Record[Idx++];
Craig Toppera13603a2014-05-22 05:54:18 +00007968 NestedNameSpecifier *NNS = nullptr, *Prev = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007969 for (unsigned I = 0; I != N; ++I) {
7970 NestedNameSpecifier::SpecifierKind Kind
7971 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
7972 switch (Kind) {
7973 case NestedNameSpecifier::Identifier: {
7974 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
7975 NNS = NestedNameSpecifier::Create(Context, Prev, II);
7976 break;
7977 }
7978
7979 case NestedNameSpecifier::Namespace: {
7980 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
7981 NNS = NestedNameSpecifier::Create(Context, Prev, NS);
7982 break;
7983 }
7984
7985 case NestedNameSpecifier::NamespaceAlias: {
7986 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
7987 NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
7988 break;
7989 }
7990
7991 case NestedNameSpecifier::TypeSpec:
7992 case NestedNameSpecifier::TypeSpecWithTemplate: {
7993 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
7994 if (!T)
Craig Toppera13603a2014-05-22 05:54:18 +00007995 return nullptr;
7996
Guy Benyei11169dd2012-12-18 14:30:41 +00007997 bool Template = Record[Idx++];
7998 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
7999 break;
8000 }
8001
8002 case NestedNameSpecifier::Global: {
8003 NNS = NestedNameSpecifier::GlobalSpecifier(Context);
8004 // No associated value, and there can't be a prefix.
8005 break;
8006 }
Nikola Smiljanic67860242014-09-26 00:28:20 +00008007
8008 case NestedNameSpecifier::Super: {
8009 CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(F, Record, Idx);
8010 NNS = NestedNameSpecifier::SuperSpecifier(Context, RD);
8011 break;
8012 }
Guy Benyei11169dd2012-12-18 14:30:41 +00008013 }
8014 Prev = NNS;
8015 }
8016 return NNS;
8017}
8018
8019NestedNameSpecifierLoc
8020ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
8021 unsigned &Idx) {
8022 unsigned N = Record[Idx++];
8023 NestedNameSpecifierLocBuilder Builder;
8024 for (unsigned I = 0; I != N; ++I) {
8025 NestedNameSpecifier::SpecifierKind Kind
8026 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
8027 switch (Kind) {
8028 case NestedNameSpecifier::Identifier: {
8029 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
8030 SourceRange Range = ReadSourceRange(F, Record, Idx);
8031 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
8032 break;
8033 }
8034
8035 case NestedNameSpecifier::Namespace: {
8036 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
8037 SourceRange Range = ReadSourceRange(F, Record, Idx);
8038 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
8039 break;
8040 }
8041
8042 case NestedNameSpecifier::NamespaceAlias: {
8043 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
8044 SourceRange Range = ReadSourceRange(F, Record, Idx);
8045 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
8046 break;
8047 }
8048
8049 case NestedNameSpecifier::TypeSpec:
8050 case NestedNameSpecifier::TypeSpecWithTemplate: {
8051 bool Template = Record[Idx++];
8052 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
8053 if (!T)
8054 return NestedNameSpecifierLoc();
8055 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
8056
8057 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
8058 Builder.Extend(Context,
8059 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
8060 T->getTypeLoc(), ColonColonLoc);
8061 break;
8062 }
8063
8064 case NestedNameSpecifier::Global: {
8065 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
8066 Builder.MakeGlobal(Context, ColonColonLoc);
8067 break;
8068 }
Nikola Smiljanic67860242014-09-26 00:28:20 +00008069
8070 case NestedNameSpecifier::Super: {
8071 CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(F, Record, Idx);
8072 SourceRange Range = ReadSourceRange(F, Record, Idx);
8073 Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd());
8074 break;
8075 }
Guy Benyei11169dd2012-12-18 14:30:41 +00008076 }
8077 }
Nikola Smiljanic67860242014-09-26 00:28:20 +00008078
Guy Benyei11169dd2012-12-18 14:30:41 +00008079 return Builder.getWithLocInContext(Context);
8080}
8081
8082SourceRange
8083ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
8084 unsigned &Idx) {
8085 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
8086 SourceLocation end = ReadSourceLocation(F, Record, Idx);
8087 return SourceRange(beg, end);
8088}
8089
8090/// \brief Read an integral value
8091llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
8092 unsigned BitWidth = Record[Idx++];
8093 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
8094 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
8095 Idx += NumWords;
8096 return Result;
8097}
8098
8099/// \brief Read a signed integral value
8100llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
8101 bool isUnsigned = Record[Idx++];
8102 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
8103}
8104
8105/// \brief Read a floating-point value
Tim Northover178723a2013-01-22 09:46:51 +00008106llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record,
8107 const llvm::fltSemantics &Sem,
8108 unsigned &Idx) {
8109 return llvm::APFloat(Sem, ReadAPInt(Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00008110}
8111
8112// \brief Read a string
8113std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
8114 unsigned Len = Record[Idx++];
8115 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
8116 Idx += Len;
8117 return Result;
8118}
8119
Richard Smith7ed1bc92014-12-05 22:42:13 +00008120std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
8121 unsigned &Idx) {
8122 std::string Filename = ReadString(Record, Idx);
8123 ResolveImportedPath(F, Filename);
8124 return Filename;
8125}
8126
Guy Benyei11169dd2012-12-18 14:30:41 +00008127VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
8128 unsigned &Idx) {
8129 unsigned Major = Record[Idx++];
8130 unsigned Minor = Record[Idx++];
8131 unsigned Subminor = Record[Idx++];
8132 if (Minor == 0)
8133 return VersionTuple(Major);
8134 if (Subminor == 0)
8135 return VersionTuple(Major, Minor - 1);
8136 return VersionTuple(Major, Minor - 1, Subminor - 1);
8137}
8138
8139CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
8140 const RecordData &Record,
8141 unsigned &Idx) {
8142 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
8143 return CXXTemporary::Create(Context, Decl);
8144}
8145
8146DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Argyrios Kyrtzidisdc9fdaf2013-05-24 05:44:08 +00008147 return Diag(CurrentImportLoc, DiagID);
Guy Benyei11169dd2012-12-18 14:30:41 +00008148}
8149
8150DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
8151 return Diags.Report(Loc, DiagID);
8152}
8153
8154/// \brief Retrieve the identifier table associated with the
8155/// preprocessor.
8156IdentifierTable &ASTReader::getIdentifierTable() {
8157 return PP.getIdentifierTable();
8158}
8159
8160/// \brief Record that the given ID maps to the given switch-case
8161/// statement.
8162void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Craig Toppera13603a2014-05-22 05:54:18 +00008163 assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
Guy Benyei11169dd2012-12-18 14:30:41 +00008164 "Already have a SwitchCase with this ID");
8165 (*CurrSwitchCaseStmts)[ID] = SC;
8166}
8167
8168/// \brief Retrieve the switch-case statement with the given ID.
8169SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Craig Toppera13603a2014-05-22 05:54:18 +00008170 assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
Guy Benyei11169dd2012-12-18 14:30:41 +00008171 return (*CurrSwitchCaseStmts)[ID];
8172}
8173
8174void ASTReader::ClearSwitchCaseIDs() {
8175 CurrSwitchCaseStmts->clear();
8176}
8177
8178void ASTReader::ReadComments() {
8179 std::vector<RawComment *> Comments;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00008180 for (SmallVectorImpl<std::pair<BitstreamCursor,
Guy Benyei11169dd2012-12-18 14:30:41 +00008181 serialization::ModuleFile *> >::iterator
8182 I = CommentsCursors.begin(),
8183 E = CommentsCursors.end();
8184 I != E; ++I) {
Dmitri Gribenko9ee0e302014-03-27 15:40:39 +00008185 Comments.clear();
Chris Lattner7fb3bef2013-01-20 00:56:42 +00008186 BitstreamCursor &Cursor = I->first;
Guy Benyei11169dd2012-12-18 14:30:41 +00008187 serialization::ModuleFile &F = *I->second;
8188 SavedStreamPosition SavedPosition(Cursor);
8189
8190 RecordData Record;
8191 while (true) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00008192 llvm::BitstreamEntry Entry =
8193 Cursor.advanceSkippingSubblocks(BitstreamCursor::AF_DontPopBlockAtEnd);
Dmitri Gribenko9ee0e302014-03-27 15:40:39 +00008194
Chris Lattner7fb3bef2013-01-20 00:56:42 +00008195 switch (Entry.Kind) {
8196 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
8197 case llvm::BitstreamEntry::Error:
8198 Error("malformed block record in AST file");
8199 return;
8200 case llvm::BitstreamEntry::EndBlock:
8201 goto NextCursor;
8202 case llvm::BitstreamEntry::Record:
8203 // The interesting case.
Guy Benyei11169dd2012-12-18 14:30:41 +00008204 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00008205 }
8206
8207 // Read a record.
8208 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00008209 switch ((CommentRecordTypes)Cursor.readRecord(Entry.ID, Record)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008210 case COMMENTS_RAW_COMMENT: {
8211 unsigned Idx = 0;
8212 SourceRange SR = ReadSourceRange(F, Record, Idx);
8213 RawComment::CommentKind Kind =
8214 (RawComment::CommentKind) Record[Idx++];
8215 bool IsTrailingComment = Record[Idx++];
8216 bool IsAlmostTrailingComment = Record[Idx++];
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00008217 Comments.push_back(new (Context) RawComment(
8218 SR, Kind, IsTrailingComment, IsAlmostTrailingComment,
8219 Context.getLangOpts().CommentOpts.ParseAllComments));
Guy Benyei11169dd2012-12-18 14:30:41 +00008220 break;
8221 }
8222 }
8223 }
Dmitri Gribenko9ee0e302014-03-27 15:40:39 +00008224 NextCursor:
8225 Context.Comments.addDeserializedComments(Comments);
Guy Benyei11169dd2012-12-18 14:30:41 +00008226 }
Guy Benyei11169dd2012-12-18 14:30:41 +00008227}
8228
Argyrios Kyrtzidis1bde1172014-11-18 05:24:18 +00008229void ASTReader::getInputFiles(ModuleFile &F,
8230 SmallVectorImpl<serialization::InputFile> &Files) {
8231 for (unsigned I = 0, E = F.InputFilesLoaded.size(); I != E; ++I) {
8232 unsigned ID = I+1;
8233 Files.push_back(getInputFile(F, ID));
8234 }
8235}
8236
Richard Smithcd45dbc2014-04-19 03:48:30 +00008237std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) {
8238 // If we know the owning module, use it.
8239 if (Module *M = D->getOwningModule())
8240 return M->getFullModuleName();
8241
8242 // Otherwise, use the name of the top-level module the decl is within.
8243 if (ModuleFile *M = getOwningModuleFile(D))
8244 return M->ModuleName;
8245
8246 // Not from a module.
8247 return "";
8248}
8249
Guy Benyei11169dd2012-12-18 14:30:41 +00008250void ASTReader::finishPendingActions() {
Richard Smith851072e2014-05-19 20:59:20 +00008251 while (!PendingIdentifierInfos.empty() ||
8252 !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() ||
Richard Smith2b9e3e32013-10-18 06:05:18 +00008253 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
Richard Smitha0ce9c42014-07-29 23:23:27 +00008254 !PendingUpdateRecords.empty()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008255 // If any identifiers with corresponding top-level declarations have
8256 // been loaded, load those declarations now.
Craig Topper79be4cd2013-07-05 04:33:53 +00008257 typedef llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2> >
8258 TopLevelDeclsMap;
8259 TopLevelDeclsMap TopLevelDecls;
8260
Guy Benyei11169dd2012-12-18 14:30:41 +00008261 while (!PendingIdentifierInfos.empty()) {
Douglas Gregor6168bd22013-02-18 15:53:43 +00008262 IdentifierInfo *II = PendingIdentifierInfos.back().first;
Richard Smithf0ae3c2d2014-03-28 17:31:23 +00008263 SmallVector<uint32_t, 4> DeclIDs =
8264 std::move(PendingIdentifierInfos.back().second);
Douglas Gregorcb15f082013-02-19 18:26:28 +00008265 PendingIdentifierInfos.pop_back();
Douglas Gregor6168bd22013-02-18 15:53:43 +00008266
8267 SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
Guy Benyei11169dd2012-12-18 14:30:41 +00008268 }
Richard Smithf0ae3c2d2014-03-28 17:31:23 +00008269
Richard Smith851072e2014-05-19 20:59:20 +00008270 // For each decl chain that we wanted to complete while deserializing, mark
8271 // it as "still needs to be completed".
8272 for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
8273 markIncompleteDeclChain(PendingIncompleteDeclChains[I]);
8274 }
8275 PendingIncompleteDeclChains.clear();
8276
Guy Benyei11169dd2012-12-18 14:30:41 +00008277 // Load pending declaration chains.
Richard Smithfe620d22015-03-05 23:24:12 +00008278 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) {
Richard Smithfe620d22015-03-05 23:24:12 +00008279 PendingDeclChainsKnown.erase(PendingDeclChains[I]);
Richard Smithe687bf82015-03-16 20:54:07 +00008280 loadPendingDeclChain(PendingDeclChains[I]);
Richard Smithfe620d22015-03-05 23:24:12 +00008281 }
8282 assert(PendingDeclChainsKnown.empty());
Guy Benyei11169dd2012-12-18 14:30:41 +00008283 PendingDeclChains.clear();
8284
Douglas Gregor6168bd22013-02-18 15:53:43 +00008285 // Make the most recent of the top-level declarations visible.
Craig Topper79be4cd2013-07-05 04:33:53 +00008286 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
8287 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
Douglas Gregor6168bd22013-02-18 15:53:43 +00008288 IdentifierInfo *II = TLD->first;
8289 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +00008290 pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
Douglas Gregor6168bd22013-02-18 15:53:43 +00008291 }
8292 }
8293
Guy Benyei11169dd2012-12-18 14:30:41 +00008294 // Load any pending macro definitions.
8295 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00008296 IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
8297 SmallVector<PendingMacroInfo, 2> GlobalIDs;
8298 GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
8299 // Initialize the macro history from chained-PCHs ahead of module imports.
Richard Smith49f906a2014-03-01 00:08:04 +00008300 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00008301 ++IDIdx) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00008302 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
Richard Smithe842a472014-10-22 02:05:46 +00008303 if (Info.M->Kind != MK_ImplicitModule &&
8304 Info.M->Kind != MK_ExplicitModule)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00008305 resolvePendingMacro(II, Info);
8306 }
8307 // Handle module imports.
Richard Smith49f906a2014-03-01 00:08:04 +00008308 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00008309 ++IDIdx) {
8310 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
Richard Smithe842a472014-10-22 02:05:46 +00008311 if (Info.M->Kind == MK_ImplicitModule ||
8312 Info.M->Kind == MK_ExplicitModule)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00008313 resolvePendingMacro(II, Info);
Guy Benyei11169dd2012-12-18 14:30:41 +00008314 }
8315 }
8316 PendingMacroIDs.clear();
Argyrios Kyrtzidis83a6e3b2013-02-16 00:48:59 +00008317
8318 // Wire up the DeclContexts for Decls that we delayed setting until
8319 // recursive loading is completed.
8320 while (!PendingDeclContextInfos.empty()) {
8321 PendingDeclContextInfo Info = PendingDeclContextInfos.front();
8322 PendingDeclContextInfos.pop_front();
8323 DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
8324 DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
8325 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
8326 }
Richard Smith2b9e3e32013-10-18 06:05:18 +00008327
Richard Smithd1c46742014-04-30 02:24:17 +00008328 // Perform any pending declaration updates.
Richard Smithd6db68c2014-08-07 20:58:41 +00008329 while (!PendingUpdateRecords.empty()) {
Richard Smithd1c46742014-04-30 02:24:17 +00008330 auto Update = PendingUpdateRecords.pop_back_val();
8331 ReadingKindTracker ReadingKind(Read_Decl, *this);
8332 loadDeclUpdateRecords(Update.first, Update.second);
8333 }
Guy Benyei11169dd2012-12-18 14:30:41 +00008334 }
Richard Smith8a639892015-01-24 01:07:20 +00008335
8336 // At this point, all update records for loaded decls are in place, so any
8337 // fake class definitions should have become real.
8338 assert(PendingFakeDefinitionData.empty() &&
8339 "faked up a class definition but never saw the real one");
8340
Guy Benyei11169dd2012-12-18 14:30:41 +00008341 // If we deserialized any C++ or Objective-C class definitions, any
8342 // Objective-C protocol definitions, or any redeclarable templates, make sure
8343 // that all redeclarations point to the definitions. Note that this can only
8344 // happen now, after the redeclaration chains have been fully wired.
Craig Topperc6914d02014-08-25 04:15:02 +00008345 for (Decl *D : PendingDefinitions) {
8346 if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
Richard Smith5b21db82014-04-23 18:20:42 +00008347 if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008348 // Make sure that the TagType points at the definition.
8349 const_cast<TagType*>(TagT)->decl = TD;
8350 }
Richard Smith8ce51082015-03-11 01:44:51 +00008351
Craig Topperc6914d02014-08-25 04:15:02 +00008352 if (auto RD = dyn_cast<CXXRecordDecl>(D)) {
Richard Smith8ce51082015-03-11 01:44:51 +00008353 for (auto *R = getMostRecentExistingDecl(RD); R;
8354 R = R->getPreviousDecl()) {
8355 assert((R == D) ==
8356 cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
Richard Smith2c381642014-08-27 23:11:59 +00008357 "declaration thinks it's the definition but it isn't");
Aaron Ballman86c93902014-03-06 23:45:36 +00008358 cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
Richard Smith2c381642014-08-27 23:11:59 +00008359 }
Guy Benyei11169dd2012-12-18 14:30:41 +00008360 }
8361
8362 continue;
8363 }
Richard Smith8ce51082015-03-11 01:44:51 +00008364
Craig Topperc6914d02014-08-25 04:15:02 +00008365 if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008366 // Make sure that the ObjCInterfaceType points at the definition.
8367 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
8368 ->Decl = ID;
Richard Smith8ce51082015-03-11 01:44:51 +00008369
8370 for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl())
8371 cast<ObjCInterfaceDecl>(R)->Data = ID->Data;
8372
Guy Benyei11169dd2012-12-18 14:30:41 +00008373 continue;
8374 }
Richard Smith8ce51082015-03-11 01:44:51 +00008375
Craig Topperc6914d02014-08-25 04:15:02 +00008376 if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) {
Richard Smith8ce51082015-03-11 01:44:51 +00008377 for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl())
8378 cast<ObjCProtocolDecl>(R)->Data = PD->Data;
8379
Guy Benyei11169dd2012-12-18 14:30:41 +00008380 continue;
8381 }
Richard Smith8ce51082015-03-11 01:44:51 +00008382
Craig Topperc6914d02014-08-25 04:15:02 +00008383 auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl();
Richard Smith8ce51082015-03-11 01:44:51 +00008384 for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl())
8385 cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common;
Guy Benyei11169dd2012-12-18 14:30:41 +00008386 }
8387 PendingDefinitions.clear();
8388
8389 // Load the bodies of any functions or methods we've encountered. We do
8390 // this now (delayed) so that we can be sure that the declaration chains
8391 // have been fully wired up.
Richard Smith8ce51082015-03-11 01:44:51 +00008392 // FIXME: There seems to be no point in delaying this, it does not depend
8393 // on the redecl chains having been wired up.
Guy Benyei11169dd2012-12-18 14:30:41 +00008394 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
8395 PBEnd = PendingBodies.end();
8396 PB != PBEnd; ++PB) {
8397 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
8398 // FIXME: Check for =delete/=default?
8399 // FIXME: Complain about ODR violations here?
8400 if (!getContext().getLangOpts().Modules || !FD->hasBody())
8401 FD->setLazyBody(PB->second);
8402 continue;
8403 }
8404
8405 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
8406 if (!getContext().getLangOpts().Modules || !MD->hasBody())
8407 MD->setLazyBody(PB->second);
8408 }
8409 PendingBodies.clear();
Richard Smitha0ce9c42014-07-29 23:23:27 +00008410}
8411
8412void ASTReader::diagnoseOdrViolations() {
Richard Smithbb853c72014-08-13 01:23:33 +00008413 if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty())
8414 return;
8415
Richard Smitha0ce9c42014-07-29 23:23:27 +00008416 // Trigger the import of the full definition of each class that had any
8417 // odr-merging problems, so we can produce better diagnostics for them.
Richard Smithbb853c72014-08-13 01:23:33 +00008418 // These updates may in turn find and diagnose some ODR failures, so take
8419 // ownership of the set first.
8420 auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
8421 PendingOdrMergeFailures.clear();
8422 for (auto &Merge : OdrMergeFailures) {
Richard Smitha0ce9c42014-07-29 23:23:27 +00008423 Merge.first->buildLookup();
8424 Merge.first->decls_begin();
8425 Merge.first->bases_begin();
8426 Merge.first->vbases_begin();
8427 for (auto *RD : Merge.second) {
8428 RD->decls_begin();
8429 RD->bases_begin();
8430 RD->vbases_begin();
8431 }
8432 }
8433
8434 // For each declaration from a merged context, check that the canonical
8435 // definition of that context also contains a declaration of the same
8436 // entity.
8437 //
8438 // Caution: this loop does things that might invalidate iterators into
8439 // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
8440 while (!PendingOdrMergeChecks.empty()) {
8441 NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
8442
8443 // FIXME: Skip over implicit declarations for now. This matters for things
8444 // like implicitly-declared special member functions. This isn't entirely
8445 // correct; we can end up with multiple unmerged declarations of the same
8446 // implicit entity.
8447 if (D->isImplicit())
8448 continue;
8449
8450 DeclContext *CanonDef = D->getDeclContext();
Richard Smitha0ce9c42014-07-29 23:23:27 +00008451
8452 bool Found = false;
8453 const Decl *DCanon = D->getCanonicalDecl();
8454
Richard Smith01bdb7a2014-08-28 05:44:07 +00008455 for (auto RI : D->redecls()) {
8456 if (RI->getLexicalDeclContext() == CanonDef) {
8457 Found = true;
8458 break;
8459 }
8460 }
8461 if (Found)
8462 continue;
8463
Richard Smitha0ce9c42014-07-29 23:23:27 +00008464 llvm::SmallVector<const NamedDecl*, 4> Candidates;
Richard Smith01bdb7a2014-08-28 05:44:07 +00008465 DeclContext::lookup_result R = CanonDef->lookup(D->getDeclName());
Richard Smitha0ce9c42014-07-29 23:23:27 +00008466 for (DeclContext::lookup_iterator I = R.begin(), E = R.end();
8467 !Found && I != E; ++I) {
8468 for (auto RI : (*I)->redecls()) {
8469 if (RI->getLexicalDeclContext() == CanonDef) {
8470 // This declaration is present in the canonical definition. If it's
8471 // in the same redecl chain, it's the one we're looking for.
8472 if (RI->getCanonicalDecl() == DCanon)
8473 Found = true;
8474 else
8475 Candidates.push_back(cast<NamedDecl>(RI));
8476 break;
8477 }
8478 }
8479 }
8480
8481 if (!Found) {
Richard Smithd08aeb62014-08-28 01:33:39 +00008482 // The AST doesn't like TagDecls becoming invalid after they've been
8483 // completed. We only really need to mark FieldDecls as invalid here.
8484 if (!isa<TagDecl>(D))
8485 D->setInvalidDecl();
Richard Smith4ab3dbd2015-02-13 22:43:51 +00008486
8487 // Ensure we don't accidentally recursively enter deserialization while
8488 // we're producing our diagnostic.
8489 Deserializing RecursionGuard(this);
Richard Smitha0ce9c42014-07-29 23:23:27 +00008490
8491 std::string CanonDefModule =
8492 getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef));
8493 Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
8494 << D << getOwningModuleNameForDiagnostic(D)
8495 << CanonDef << CanonDefModule.empty() << CanonDefModule;
8496
8497 if (Candidates.empty())
8498 Diag(cast<Decl>(CanonDef)->getLocation(),
8499 diag::note_module_odr_violation_no_possible_decls) << D;
8500 else {
8501 for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
8502 Diag(Candidates[I]->getLocation(),
8503 diag::note_module_odr_violation_possible_decl)
8504 << Candidates[I];
8505 }
8506
8507 DiagnosedOdrMergeFailures.insert(CanonDef);
8508 }
8509 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00008510
Richard Smith4ab3dbd2015-02-13 22:43:51 +00008511 if (OdrMergeFailures.empty())
8512 return;
8513
8514 // Ensure we don't accidentally recursively enter deserialization while
8515 // we're producing our diagnostics.
8516 Deserializing RecursionGuard(this);
8517
Richard Smithcd45dbc2014-04-19 03:48:30 +00008518 // Issue any pending ODR-failure diagnostics.
Richard Smithbb853c72014-08-13 01:23:33 +00008519 for (auto &Merge : OdrMergeFailures) {
Richard Smitha0ce9c42014-07-29 23:23:27 +00008520 // If we've already pointed out a specific problem with this class, don't
8521 // bother issuing a general "something's different" diagnostic.
David Blaikie82e95a32014-11-19 07:49:47 +00008522 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
Richard Smithcd45dbc2014-04-19 03:48:30 +00008523 continue;
8524
8525 bool Diagnosed = false;
8526 for (auto *RD : Merge.second) {
8527 // Multiple different declarations got merged together; tell the user
8528 // where they came from.
8529 if (Merge.first != RD) {
8530 // FIXME: Walk the definition, figure out what's different,
8531 // and diagnose that.
8532 if (!Diagnosed) {
8533 std::string Module = getOwningModuleNameForDiagnostic(Merge.first);
8534 Diag(Merge.first->getLocation(),
8535 diag::err_module_odr_violation_different_definitions)
8536 << Merge.first << Module.empty() << Module;
8537 Diagnosed = true;
8538 }
8539
8540 Diag(RD->getLocation(),
8541 diag::note_module_odr_violation_different_definitions)
8542 << getOwningModuleNameForDiagnostic(RD);
8543 }
8544 }
8545
8546 if (!Diagnosed) {
8547 // All definitions are updates to the same declaration. This happens if a
8548 // module instantiates the declaration of a class template specialization
8549 // and two or more other modules instantiate its definition.
8550 //
8551 // FIXME: Indicate which modules had instantiations of this definition.
8552 // FIXME: How can this even happen?
8553 Diag(Merge.first->getLocation(),
8554 diag::err_module_odr_violation_different_instantiations)
8555 << Merge.first;
8556 }
8557 }
Guy Benyei11169dd2012-12-18 14:30:41 +00008558}
8559
8560void ASTReader::FinishedDeserializing() {
8561 assert(NumCurrentElementsDeserializing &&
8562 "FinishedDeserializing not paired with StartedDeserializing");
8563 if (NumCurrentElementsDeserializing == 1) {
8564 // We decrease NumCurrentElementsDeserializing only after pending actions
8565 // are finished, to avoid recursively re-calling finishPendingActions().
8566 finishPendingActions();
8567 }
8568 --NumCurrentElementsDeserializing;
8569
Richard Smitha0ce9c42014-07-29 23:23:27 +00008570 if (NumCurrentElementsDeserializing == 0) {
Richard Smith9e2341d2015-03-23 03:25:59 +00008571 // Propagate exception specification updates along redeclaration chains.
Richard Smith7226f2a2015-03-23 19:54:56 +00008572 while (!PendingExceptionSpecUpdates.empty()) {
8573 auto Updates = std::move(PendingExceptionSpecUpdates);
8574 PendingExceptionSpecUpdates.clear();
8575 for (auto Update : Updates) {
8576 auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
8577 SemaObj->UpdateExceptionSpec(Update.second,
8578 FPT->getExtProtoInfo().ExceptionSpec);
8579 }
Richard Smith9e2341d2015-03-23 03:25:59 +00008580 }
8581
Richard Smitha0ce9c42014-07-29 23:23:27 +00008582 diagnoseOdrViolations();
8583
Richard Smith04d05b52014-03-23 00:27:18 +00008584 // We are not in recursive loading, so it's safe to pass the "interesting"
8585 // decls to the consumer.
Richard Smitha0ce9c42014-07-29 23:23:27 +00008586 if (Consumer)
8587 PassInterestingDeclsToConsumer();
Guy Benyei11169dd2012-12-18 14:30:41 +00008588 }
8589}
8590
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +00008591void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
Richard Smith9e2341d2015-03-23 03:25:59 +00008592 if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
8593 // Remove any fake results before adding any real ones.
8594 auto It = PendingFakeLookupResults.find(II);
8595 if (It != PendingFakeLookupResults.end()) {
8596 for (auto *ND : PendingFakeLookupResults[II])
8597 SemaObj->IdResolver.RemoveDecl(ND);
Ben Langmuireb8bd2d2015-04-10 22:25:42 +00008598 // FIXME: this works around module+PCH performance issue.
8599 // Rather than erase the result from the map, which is O(n), just clear
8600 // the vector of NamedDecls.
8601 It->second.clear();
Richard Smith9e2341d2015-03-23 03:25:59 +00008602 }
8603 }
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +00008604
8605 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
8606 SemaObj->TUScope->AddDecl(D);
8607 } else if (SemaObj->TUScope) {
8608 // Adding the decl to IdResolver may have failed because it was already in
8609 // (even though it was not added in scope). If it is already in, make sure
8610 // it gets in the scope as well.
8611 if (std::find(SemaObj->IdResolver.begin(Name),
8612 SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
8613 SemaObj->TUScope->AddDecl(D);
8614 }
8615}
8616
Nico Weber824285e2014-05-08 04:26:47 +00008617ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context, StringRef isysroot,
8618 bool DisableValidation, bool AllowASTWithCompilerErrors,
8619 bool AllowConfigurationMismatch, bool ValidateSystemInputs,
Ben Langmuir2cb4a782014-02-05 22:21:15 +00008620 bool UseGlobalIndex)
Craig Toppera13603a2014-05-22 05:54:18 +00008621 : Listener(new PCHValidator(PP, *this)), DeserializationListener(nullptr),
Nico Weber824285e2014-05-08 04:26:47 +00008622 OwnsDeserializationListener(false), SourceMgr(PP.getSourceManager()),
Craig Toppera13603a2014-05-22 05:54:18 +00008623 FileMgr(PP.getFileManager()), Diags(PP.getDiagnostics()),
8624 SemaObj(nullptr), PP(PP), Context(Context), Consumer(nullptr),
8625 ModuleMgr(PP.getFileManager()), isysroot(isysroot),
8626 DisableValidation(DisableValidation),
Nico Weber824285e2014-05-08 04:26:47 +00008627 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
8628 AllowConfigurationMismatch(AllowConfigurationMismatch),
8629 ValidateSystemInputs(ValidateSystemInputs),
8630 UseGlobalIndex(UseGlobalIndex), TriedLoadingGlobalIndex(false),
Richard Smith053f6c62014-05-16 23:01:30 +00008631 CurrSwitchCaseStmts(&SwitchCaseStmts),
Nico Weber824285e2014-05-08 04:26:47 +00008632 NumSLocEntriesRead(0), TotalNumSLocEntries(0), NumStatementsRead(0),
8633 TotalNumStatements(0), NumMacrosRead(0), TotalNumMacros(0),
8634 NumIdentifierLookups(0), NumIdentifierLookupHits(0), NumSelectorsRead(0),
8635 NumMethodPoolEntriesRead(0), NumMethodPoolLookups(0),
8636 NumMethodPoolHits(0), NumMethodPoolTableLookups(0),
8637 NumMethodPoolTableHits(0), TotalNumMethodPoolEntries(0),
8638 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
8639 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
8640 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
Richard Smithc2bb8182015-03-24 06:36:48 +00008641 PassingDeclsToConsumer(false), ReadingKind(Read_None) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008642 SourceMgr.setExternalSLocEntrySource(this);
8643}
8644
8645ASTReader::~ASTReader() {
Nico Weber824285e2014-05-08 04:26:47 +00008646 if (OwnsDeserializationListener)
8647 delete DeserializationListener;
8648
Guy Benyei11169dd2012-12-18 14:30:41 +00008649 for (DeclContextVisibleUpdatesPending::iterator
8650 I = PendingVisibleUpdates.begin(),
8651 E = PendingVisibleUpdates.end();
8652 I != E; ++I) {
8653 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
8654 F = I->second.end();
8655 J != F; ++J)
8656 delete J->first;
8657 }
8658}