blob: 8650ce757698210e4773589324fa60417704cd69 [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"
Adrian Prantlbb165fb2015-06-20 18:53:08 +000022#include "clang/Frontend/PCHContainerOperations.h"
Richard Smithd88a7f12015-09-01 20:35:42 +000023#include "clang/AST/ASTMutationListener.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000024#include "clang/AST/NestedNameSpecifier.h"
25#include "clang/AST/Type.h"
26#include "clang/AST/TypeLocVisitor.h"
Benjamin Kramerf3ca26982014-05-10 16:31:55 +000027#include "clang/Basic/DiagnosticOptions.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000028#include "clang/Basic/FileManager.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000029#include "clang/Basic/SourceManager.h"
30#include "clang/Basic/SourceManagerInternals.h"
31#include "clang/Basic/TargetInfo.h"
32#include "clang/Basic/TargetOptions.h"
33#include "clang/Basic/Version.h"
34#include "clang/Basic/VersionTuple.h"
Ben Langmuirb92de022014-04-29 16:25:26 +000035#include "clang/Frontend/Utils.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000036#include "clang/Lex/HeaderSearch.h"
37#include "clang/Lex/HeaderSearchOptions.h"
38#include "clang/Lex/MacroInfo.h"
39#include "clang/Lex/PreprocessingRecord.h"
40#include "clang/Lex/Preprocessor.h"
41#include "clang/Lex/PreprocessorOptions.h"
42#include "clang/Sema/Scope.h"
43#include "clang/Sema/Sema.h"
44#include "clang/Serialization/ASTDeserializationListener.h"
Douglas Gregore060e572013-01-25 01:03:03 +000045#include "clang/Serialization/GlobalModuleIndex.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000046#include "clang/Serialization/ModuleManager.h"
47#include "clang/Serialization/SerializationDiagnostic.h"
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +000048#include "llvm/ADT/Hashing.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000049#include "llvm/ADT/StringExtras.h"
50#include "llvm/Bitcode/BitstreamReader.h"
Richard Smithaada85c2016-02-06 02:06:43 +000051#include "llvm/Support/Compression.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000052#include "llvm/Support/ErrorHandling.h"
53#include "llvm/Support/FileSystem.h"
54#include "llvm/Support/MemoryBuffer.h"
55#include "llvm/Support/Path.h"
56#include "llvm/Support/SaveAndRestore.h"
Dmitri Gribenkof430da42014-02-12 10:33:14 +000057#include "llvm/Support/raw_ostream.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000058#include <algorithm>
Chris Lattner91f373e2013-01-20 00:57:52 +000059#include <cstdio>
Guy Benyei11169dd2012-12-18 14:30:41 +000060#include <iterator>
Rafael Espindola8a8e5542014-06-12 17:19:42 +000061#include <system_error>
Guy Benyei11169dd2012-12-18 14:30:41 +000062
63using namespace clang;
64using namespace clang::serialization;
65using namespace clang::serialization::reader;
Chris Lattner7fb3bef2013-01-20 00:56:42 +000066using llvm::BitstreamCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +000067
Ben Langmuircb69b572014-03-07 06:40:32 +000068
69//===----------------------------------------------------------------------===//
70// ChainedASTReaderListener implementation
71//===----------------------------------------------------------------------===//
72
73bool
74ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) {
75 return First->ReadFullVersionInformation(FullVersion) ||
76 Second->ReadFullVersionInformation(FullVersion);
77}
Ben Langmuir4f5212a2014-04-14 22:12:44 +000078void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) {
79 First->ReadModuleName(ModuleName);
80 Second->ReadModuleName(ModuleName);
81}
82void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) {
83 First->ReadModuleMapFile(ModuleMapPath);
84 Second->ReadModuleMapFile(ModuleMapPath);
85}
Richard Smith1e2cf0d2014-10-31 02:28:58 +000086bool
87ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts,
88 bool Complain,
89 bool AllowCompatibleDifferences) {
90 return First->ReadLanguageOptions(LangOpts, Complain,
91 AllowCompatibleDifferences) ||
92 Second->ReadLanguageOptions(LangOpts, Complain,
93 AllowCompatibleDifferences);
Ben Langmuircb69b572014-03-07 06:40:32 +000094}
Chandler Carruth0d745bc2015-03-14 04:47:43 +000095bool ChainedASTReaderListener::ReadTargetOptions(
96 const TargetOptions &TargetOpts, bool Complain,
97 bool AllowCompatibleDifferences) {
98 return First->ReadTargetOptions(TargetOpts, Complain,
99 AllowCompatibleDifferences) ||
100 Second->ReadTargetOptions(TargetOpts, Complain,
101 AllowCompatibleDifferences);
Ben Langmuircb69b572014-03-07 06:40:32 +0000102}
103bool ChainedASTReaderListener::ReadDiagnosticOptions(
Ben Langmuirb92de022014-04-29 16:25:26 +0000104 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
Ben Langmuircb69b572014-03-07 06:40:32 +0000105 return First->ReadDiagnosticOptions(DiagOpts, Complain) ||
106 Second->ReadDiagnosticOptions(DiagOpts, Complain);
107}
108bool
109ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts,
110 bool Complain) {
111 return First->ReadFileSystemOptions(FSOpts, Complain) ||
112 Second->ReadFileSystemOptions(FSOpts, Complain);
113}
114
115bool ChainedASTReaderListener::ReadHeaderSearchOptions(
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +0000116 const HeaderSearchOptions &HSOpts, StringRef SpecificModuleCachePath,
117 bool Complain) {
118 return First->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
119 Complain) ||
120 Second->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
121 Complain);
Ben Langmuircb69b572014-03-07 06:40:32 +0000122}
123bool ChainedASTReaderListener::ReadPreprocessorOptions(
124 const PreprocessorOptions &PPOpts, bool Complain,
125 std::string &SuggestedPredefines) {
126 return First->ReadPreprocessorOptions(PPOpts, Complain,
127 SuggestedPredefines) ||
128 Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines);
129}
130void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M,
131 unsigned Value) {
132 First->ReadCounter(M, Value);
133 Second->ReadCounter(M, Value);
134}
135bool ChainedASTReaderListener::needsInputFileVisitation() {
136 return First->needsInputFileVisitation() ||
137 Second->needsInputFileVisitation();
138}
139bool ChainedASTReaderListener::needsSystemInputFileVisitation() {
140 return First->needsSystemInputFileVisitation() ||
141 Second->needsSystemInputFileVisitation();
142}
Richard Smith216a3bd2015-08-13 17:57:10 +0000143void ChainedASTReaderListener::visitModuleFile(StringRef Filename,
144 ModuleKind Kind) {
145 First->visitModuleFile(Filename, Kind);
146 Second->visitModuleFile(Filename, Kind);
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +0000147}
Ben Langmuircb69b572014-03-07 06:40:32 +0000148bool ChainedASTReaderListener::visitInputFile(StringRef Filename,
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +0000149 bool isSystem,
Richard Smith216a3bd2015-08-13 17:57:10 +0000150 bool isOverridden,
151 bool isExplicitModule) {
Justin Bognerc65a66d2014-05-22 06:04:59 +0000152 bool Continue = false;
153 if (First->needsInputFileVisitation() &&
154 (!isSystem || First->needsSystemInputFileVisitation()))
Richard Smith216a3bd2015-08-13 17:57:10 +0000155 Continue |= First->visitInputFile(Filename, isSystem, isOverridden,
156 isExplicitModule);
Justin Bognerc65a66d2014-05-22 06:04:59 +0000157 if (Second->needsInputFileVisitation() &&
158 (!isSystem || Second->needsSystemInputFileVisitation()))
Richard Smith216a3bd2015-08-13 17:57:10 +0000159 Continue |= Second->visitInputFile(Filename, isSystem, isOverridden,
160 isExplicitModule);
Justin Bognerc65a66d2014-05-22 06:04:59 +0000161 return Continue;
Ben Langmuircb69b572014-03-07 06:40:32 +0000162}
163
Douglas Gregor6623e1f2015-11-03 18:33:07 +0000164void ChainedASTReaderListener::readModuleFileExtension(
165 const ModuleFileExtensionMetadata &Metadata) {
166 First->readModuleFileExtension(Metadata);
167 Second->readModuleFileExtension(Metadata);
168}
169
Guy Benyei11169dd2012-12-18 14:30:41 +0000170//===----------------------------------------------------------------------===//
171// PCH validator implementation
172//===----------------------------------------------------------------------===//
173
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000174ASTReaderListener::~ASTReaderListener() {}
Guy Benyei11169dd2012-12-18 14:30:41 +0000175
176/// \brief Compare the given set of language options against an existing set of
177/// language options.
178///
179/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
Richard Smith1e2cf0d2014-10-31 02:28:58 +0000180/// \param AllowCompatibleDifferences If true, differences between compatible
181/// language options will be permitted.
Guy Benyei11169dd2012-12-18 14:30:41 +0000182///
183/// \returns true if the languagae options mis-match, false otherwise.
184static bool checkLanguageOptions(const LangOptions &LangOpts,
185 const LangOptions &ExistingLangOpts,
Richard Smith1e2cf0d2014-10-31 02:28:58 +0000186 DiagnosticsEngine *Diags,
187 bool AllowCompatibleDifferences = true) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000188#define LANGOPT(Name, Bits, Default, Description) \
189 if (ExistingLangOpts.Name != LangOpts.Name) { \
190 if (Diags) \
191 Diags->Report(diag::err_pch_langopt_mismatch) \
192 << Description << LangOpts.Name << ExistingLangOpts.Name; \
193 return true; \
194 }
195
196#define VALUE_LANGOPT(Name, Bits, Default, Description) \
197 if (ExistingLangOpts.Name != LangOpts.Name) { \
198 if (Diags) \
199 Diags->Report(diag::err_pch_langopt_value_mismatch) \
200 << Description; \
201 return true; \
202 }
203
204#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
205 if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \
206 if (Diags) \
207 Diags->Report(diag::err_pch_langopt_value_mismatch) \
208 << Description; \
209 return true; \
210 }
211
Richard Smith1e2cf0d2014-10-31 02:28:58 +0000212#define COMPATIBLE_LANGOPT(Name, Bits, Default, Description) \
213 if (!AllowCompatibleDifferences) \
214 LANGOPT(Name, Bits, Default, Description)
215
216#define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description) \
217 if (!AllowCompatibleDifferences) \
218 ENUM_LANGOPT(Name, Bits, Default, Description)
219
Richard Smitha1ddf5e2016-04-07 20:47:37 +0000220#define COMPATIBLE_VALUE_LANGOPT(Name, Bits, Default, Description) \
221 if (!AllowCompatibleDifferences) \
222 VALUE_LANGOPT(Name, Bits, Default, Description)
223
Guy Benyei11169dd2012-12-18 14:30:41 +0000224#define BENIGN_LANGOPT(Name, Bits, Default, Description)
225#define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
Richard Smitha1ddf5e2016-04-07 20:47:37 +0000226#define BENIGN_VALUE_LANGOPT(Name, Type, Bits, Default, Description)
Guy Benyei11169dd2012-12-18 14:30:41 +0000227#include "clang/Basic/LangOptions.def"
228
Ben Langmuircd98cb72015-06-23 18:20:18 +0000229 if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) {
230 if (Diags)
231 Diags->Report(diag::err_pch_langopt_value_mismatch) << "module features";
232 return true;
233 }
234
Guy Benyei11169dd2012-12-18 14:30:41 +0000235 if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
236 if (Diags)
237 Diags->Report(diag::err_pch_langopt_value_mismatch)
238 << "target Objective-C runtime";
239 return true;
240 }
241
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +0000242 if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
243 LangOpts.CommentOpts.BlockCommandNames) {
244 if (Diags)
245 Diags->Report(diag::err_pch_langopt_value_mismatch)
246 << "block command names";
247 return true;
248 }
249
Guy Benyei11169dd2012-12-18 14:30:41 +0000250 return false;
251}
252
253/// \brief Compare the given set of target options against an existing set of
254/// target options.
255///
256/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
257///
258/// \returns true if the target options mis-match, false otherwise.
259static bool checkTargetOptions(const TargetOptions &TargetOpts,
260 const TargetOptions &ExistingTargetOpts,
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000261 DiagnosticsEngine *Diags,
262 bool AllowCompatibleDifferences = true) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000263#define CHECK_TARGET_OPT(Field, Name) \
264 if (TargetOpts.Field != ExistingTargetOpts.Field) { \
265 if (Diags) \
266 Diags->Report(diag::err_pch_targetopt_mismatch) \
267 << Name << TargetOpts.Field << ExistingTargetOpts.Field; \
268 return true; \
269 }
270
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000271 // The triple and ABI must match exactly.
Guy Benyei11169dd2012-12-18 14:30:41 +0000272 CHECK_TARGET_OPT(Triple, "target");
Guy Benyei11169dd2012-12-18 14:30:41 +0000273 CHECK_TARGET_OPT(ABI, "target ABI");
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000274
275 // We can tolerate different CPUs in many cases, notably when one CPU
276 // supports a strict superset of another. When allowing compatible
277 // differences skip this check.
278 if (!AllowCompatibleDifferences)
279 CHECK_TARGET_OPT(CPU, "target CPU");
280
Guy Benyei11169dd2012-12-18 14:30:41 +0000281#undef CHECK_TARGET_OPT
282
283 // Compare feature sets.
284 SmallVector<StringRef, 4> ExistingFeatures(
285 ExistingTargetOpts.FeaturesAsWritten.begin(),
286 ExistingTargetOpts.FeaturesAsWritten.end());
287 SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
288 TargetOpts.FeaturesAsWritten.end());
289 std::sort(ExistingFeatures.begin(), ExistingFeatures.end());
290 std::sort(ReadFeatures.begin(), ReadFeatures.end());
291
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000292 // We compute the set difference in both directions explicitly so that we can
293 // diagnose the differences differently.
294 SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures;
295 std::set_difference(
296 ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(),
297 ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures));
298 std::set_difference(ReadFeatures.begin(), ReadFeatures.end(),
299 ExistingFeatures.begin(), ExistingFeatures.end(),
300 std::back_inserter(UnmatchedReadFeatures));
Guy Benyei11169dd2012-12-18 14:30:41 +0000301
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000302 // If we are allowing compatible differences and the read feature set is
303 // a strict subset of the existing feature set, there is nothing to diagnose.
304 if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty())
305 return false;
Guy Benyei11169dd2012-12-18 14:30:41 +0000306
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000307 if (Diags) {
308 for (StringRef Feature : UnmatchedReadFeatures)
Guy Benyei11169dd2012-12-18 14:30:41 +0000309 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000310 << /* is-existing-feature */ false << Feature;
311 for (StringRef Feature : UnmatchedExistingFeatures)
312 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
313 << /* is-existing-feature */ true << Feature;
Guy Benyei11169dd2012-12-18 14:30:41 +0000314 }
315
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000316 return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty();
Guy Benyei11169dd2012-12-18 14:30:41 +0000317}
318
319bool
320PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
Richard Smith1e2cf0d2014-10-31 02:28:58 +0000321 bool Complain,
322 bool AllowCompatibleDifferences) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000323 const LangOptions &ExistingLangOpts = PP.getLangOpts();
324 return checkLanguageOptions(LangOpts, ExistingLangOpts,
Richard Smith1e2cf0d2014-10-31 02:28:58 +0000325 Complain ? &Reader.Diags : nullptr,
326 AllowCompatibleDifferences);
Guy Benyei11169dd2012-12-18 14:30:41 +0000327}
328
329bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000330 bool Complain,
331 bool AllowCompatibleDifferences) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000332 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
333 return checkTargetOptions(TargetOpts, ExistingTargetOpts,
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000334 Complain ? &Reader.Diags : nullptr,
335 AllowCompatibleDifferences);
Guy Benyei11169dd2012-12-18 14:30:41 +0000336}
337
338namespace {
339 typedef llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> >
340 MacroDefinitionsMap;
Craig Topper3598eb72013-07-05 04:43:31 +0000341 typedef llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> >
342 DeclsMap;
Guy Benyei11169dd2012-12-18 14:30:41 +0000343}
344
Ben Langmuirb92de022014-04-29 16:25:26 +0000345static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags,
346 DiagnosticsEngine &Diags,
347 bool Complain) {
348 typedef DiagnosticsEngine::Level Level;
349
350 // Check current mappings for new -Werror mappings, and the stored mappings
351 // for cases that were explicitly mapped to *not* be errors that are now
352 // errors because of options like -Werror.
353 DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags };
354
355 for (DiagnosticsEngine *MappingSource : MappingSources) {
356 for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {
357 diag::kind DiagID = DiagIDMappingPair.first;
358 Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation());
359 if (CurLevel < DiagnosticsEngine::Error)
360 continue; // not significant
361 Level StoredLevel =
362 StoredDiags.getDiagnosticLevel(DiagID, SourceLocation());
363 if (StoredLevel < DiagnosticsEngine::Error) {
364 if (Complain)
365 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" +
366 Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str();
367 return true;
368 }
369 }
370 }
371
372 return false;
373}
374
Alp Tokerac4e8e52014-06-22 21:58:33 +0000375static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) {
376 diag::Severity Ext = Diags.getExtensionHandlingBehavior();
377 if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors())
378 return true;
379 return Ext >= diag::Severity::Error;
Ben Langmuirb92de022014-04-29 16:25:26 +0000380}
381
382static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags,
383 DiagnosticsEngine &Diags,
384 bool IsSystem, bool Complain) {
385 // Top-level options
386 if (IsSystem) {
387 if (Diags.getSuppressSystemWarnings())
388 return false;
389 // If -Wsystem-headers was not enabled before, be conservative
390 if (StoredDiags.getSuppressSystemWarnings()) {
391 if (Complain)
392 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers";
393 return true;
394 }
395 }
396
397 if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) {
398 if (Complain)
399 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror";
400 return true;
401 }
402
403 if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() &&
404 !StoredDiags.getEnableAllWarnings()) {
405 if (Complain)
406 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror";
407 return true;
408 }
409
410 if (isExtHandlingFromDiagsError(Diags) &&
411 !isExtHandlingFromDiagsError(StoredDiags)) {
412 if (Complain)
413 Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors";
414 return true;
415 }
416
417 return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain);
418}
419
420bool PCHValidator::ReadDiagnosticOptions(
421 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
422 DiagnosticsEngine &ExistingDiags = PP.getDiagnostics();
423 IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs());
424 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
Alp Tokerf994cef2014-07-05 03:08:06 +0000425 new DiagnosticsEngine(DiagIDs, DiagOpts.get()));
Ben Langmuirb92de022014-04-29 16:25:26 +0000426 // This should never fail, because we would have processed these options
427 // before writing them to an ASTFile.
428 ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false);
429
430 ModuleManager &ModuleMgr = Reader.getModuleManager();
431 assert(ModuleMgr.size() >= 1 && "what ASTFile is this then");
432
433 // If the original import came from a file explicitly generated by the user,
434 // don't check the diagnostic mappings.
435 // FIXME: currently this is approximated by checking whether this is not a
Richard Smithe842a472014-10-22 02:05:46 +0000436 // module import of an implicitly-loaded module file.
Ben Langmuirb92de022014-04-29 16:25:26 +0000437 // Note: ModuleMgr.rbegin() may not be the current module, but it must be in
438 // the transitive closure of its imports, since unrelated modules cannot be
439 // imported until after this module finishes validation.
440 ModuleFile *TopImport = *ModuleMgr.rbegin();
441 while (!TopImport->ImportedBy.empty())
442 TopImport = TopImport->ImportedBy[0];
Richard Smithe842a472014-10-22 02:05:46 +0000443 if (TopImport->Kind != MK_ImplicitModule)
Ben Langmuirb92de022014-04-29 16:25:26 +0000444 return false;
445
446 StringRef ModuleName = TopImport->ModuleName;
447 assert(!ModuleName.empty() && "diagnostic options read before module name");
448
449 Module *M = PP.getHeaderSearchInfo().lookupModule(ModuleName);
450 assert(M && "missing module");
451
452 // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that
453 // contains the union of their flags.
454 return checkDiagnosticMappings(*Diags, ExistingDiags, M->IsSystem, Complain);
455}
456
Guy Benyei11169dd2012-12-18 14:30:41 +0000457/// \brief Collect the macro definitions provided by the given preprocessor
458/// options.
Craig Toppera13603a2014-05-22 05:54:18 +0000459static void
460collectMacroDefinitions(const PreprocessorOptions &PPOpts,
461 MacroDefinitionsMap &Macros,
462 SmallVectorImpl<StringRef> *MacroNames = nullptr) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000463 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
464 StringRef Macro = PPOpts.Macros[I].first;
465 bool IsUndef = PPOpts.Macros[I].second;
466
467 std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
468 StringRef MacroName = MacroPair.first;
469 StringRef MacroBody = MacroPair.second;
470
471 // For an #undef'd macro, we only care about the name.
472 if (IsUndef) {
473 if (MacroNames && !Macros.count(MacroName))
474 MacroNames->push_back(MacroName);
475
476 Macros[MacroName] = std::make_pair("", true);
477 continue;
478 }
479
480 // For a #define'd macro, figure out the actual definition.
481 if (MacroName.size() == Macro.size())
482 MacroBody = "1";
483 else {
484 // Note: GCC drops anything following an end-of-line character.
485 StringRef::size_type End = MacroBody.find_first_of("\n\r");
486 MacroBody = MacroBody.substr(0, End);
487 }
488
489 if (MacroNames && !Macros.count(MacroName))
490 MacroNames->push_back(MacroName);
491 Macros[MacroName] = std::make_pair(MacroBody, false);
492 }
493}
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +0000494
Guy Benyei11169dd2012-12-18 14:30:41 +0000495/// \brief Check the preprocessor options deserialized from the control block
496/// against the preprocessor options in an existing preprocessor.
497///
498/// \param Diags If non-null, produce diagnostics for any mismatches incurred.
499static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts,
500 const PreprocessorOptions &ExistingPPOpts,
501 DiagnosticsEngine *Diags,
502 FileManager &FileMgr,
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +0000503 std::string &SuggestedPredefines,
504 const LangOptions &LangOpts) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000505 // Check macro definitions.
506 MacroDefinitionsMap ASTFileMacros;
507 collectMacroDefinitions(PPOpts, ASTFileMacros);
508 MacroDefinitionsMap ExistingMacros;
509 SmallVector<StringRef, 4> ExistingMacroNames;
510 collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames);
511
512 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
513 // Dig out the macro definition in the existing preprocessor options.
514 StringRef MacroName = ExistingMacroNames[I];
515 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
516
517 // Check whether we know anything about this macro name or not.
518 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> >::iterator Known
519 = ASTFileMacros.find(MacroName);
520 if (Known == ASTFileMacros.end()) {
521 // FIXME: Check whether this identifier was referenced anywhere in the
522 // AST file. If so, we should reject the AST file. Unfortunately, this
523 // information isn't in the control block. What shall we do about it?
524
525 if (Existing.second) {
526 SuggestedPredefines += "#undef ";
527 SuggestedPredefines += MacroName.str();
528 SuggestedPredefines += '\n';
529 } else {
530 SuggestedPredefines += "#define ";
531 SuggestedPredefines += MacroName.str();
532 SuggestedPredefines += ' ';
533 SuggestedPredefines += Existing.first.str();
534 SuggestedPredefines += '\n';
535 }
536 continue;
537 }
538
539 // If the macro was defined in one but undef'd in the other, we have a
540 // conflict.
541 if (Existing.second != Known->second.second) {
542 if (Diags) {
543 Diags->Report(diag::err_pch_macro_def_undef)
544 << MacroName << Known->second.second;
545 }
546 return true;
547 }
548
549 // If the macro was #undef'd in both, or if the macro bodies are identical,
550 // it's fine.
551 if (Existing.second || Existing.first == Known->second.first)
552 continue;
553
554 // The macro bodies differ; complain.
555 if (Diags) {
556 Diags->Report(diag::err_pch_macro_def_conflict)
557 << MacroName << Known->second.first << Existing.first;
558 }
559 return true;
560 }
561
562 // Check whether we're using predefines.
563 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines) {
564 if (Diags) {
565 Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
566 }
567 return true;
568 }
569
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +0000570 // Detailed record is important since it is used for the module cache hash.
571 if (LangOpts.Modules &&
572 PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord) {
573 if (Diags) {
574 Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord;
575 }
576 return true;
577 }
578
Guy Benyei11169dd2012-12-18 14:30:41 +0000579 // Compute the #include and #include_macros lines we need.
580 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
581 StringRef File = ExistingPPOpts.Includes[I];
582 if (File == ExistingPPOpts.ImplicitPCHInclude)
583 continue;
584
585 if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File)
586 != PPOpts.Includes.end())
587 continue;
588
589 SuggestedPredefines += "#include \"";
Manuel Klimek9af34ae2014-08-12 08:25:57 +0000590 SuggestedPredefines += File;
Guy Benyei11169dd2012-12-18 14:30:41 +0000591 SuggestedPredefines += "\"\n";
592 }
593
594 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
595 StringRef File = ExistingPPOpts.MacroIncludes[I];
596 if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(),
597 File)
598 != PPOpts.MacroIncludes.end())
599 continue;
600
601 SuggestedPredefines += "#__include_macros \"";
Manuel Klimek9af34ae2014-08-12 08:25:57 +0000602 SuggestedPredefines += File;
Guy Benyei11169dd2012-12-18 14:30:41 +0000603 SuggestedPredefines += "\"\n##\n";
604 }
605
606 return false;
607}
608
609bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
610 bool Complain,
611 std::string &SuggestedPredefines) {
612 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
613
614 return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
Craig Toppera13603a2014-05-22 05:54:18 +0000615 Complain? &Reader.Diags : nullptr,
Guy Benyei11169dd2012-12-18 14:30:41 +0000616 PP.getFileManager(),
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +0000617 SuggestedPredefines,
618 PP.getLangOpts());
Guy Benyei11169dd2012-12-18 14:30:41 +0000619}
620
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +0000621/// Check the header search options deserialized from the control block
622/// against the header search options in an existing preprocessor.
623///
624/// \param Diags If non-null, produce diagnostics for any mismatches incurred.
625static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
626 StringRef SpecificModuleCachePath,
627 StringRef ExistingModuleCachePath,
628 DiagnosticsEngine *Diags,
629 const LangOptions &LangOpts) {
630 if (LangOpts.Modules) {
631 if (SpecificModuleCachePath != ExistingModuleCachePath) {
632 if (Diags)
633 Diags->Report(diag::err_pch_modulecache_mismatch)
634 << SpecificModuleCachePath << ExistingModuleCachePath;
635 return true;
636 }
637 }
638
639 return false;
640}
641
642bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
643 StringRef SpecificModuleCachePath,
644 bool Complain) {
645 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
646 PP.getHeaderSearchInfo().getModuleCachePath(),
647 Complain ? &Reader.Diags : nullptr,
648 PP.getLangOpts());
649}
650
Guy Benyei11169dd2012-12-18 14:30:41 +0000651void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
652 PP.setCounterValue(Value);
653}
654
655//===----------------------------------------------------------------------===//
656// AST reader implementation
657//===----------------------------------------------------------------------===//
658
Nico Weber824285e2014-05-08 04:26:47 +0000659void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,
660 bool TakeOwnership) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000661 DeserializationListener = Listener;
Nico Weber824285e2014-05-08 04:26:47 +0000662 OwnsDeserializationListener = TakeOwnership;
Guy Benyei11169dd2012-12-18 14:30:41 +0000663}
664
665
666
667unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
668 return serialization::ComputeHash(Sel);
669}
670
671
672std::pair<unsigned, unsigned>
673ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000674 using namespace llvm::support;
675 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
676 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
Guy Benyei11169dd2012-12-18 14:30:41 +0000677 return std::make_pair(KeyLen, DataLen);
678}
679
680ASTSelectorLookupTrait::internal_key_type
681ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000682 using namespace llvm::support;
Guy Benyei11169dd2012-12-18 14:30:41 +0000683 SelectorTable &SelTable = Reader.getContext().Selectors;
Justin Bogner57ba0b22014-03-28 22:03:24 +0000684 unsigned N = endian::readNext<uint16_t, little, unaligned>(d);
685 IdentifierInfo *FirstII = Reader.getLocalIdentifier(
686 F, endian::readNext<uint32_t, little, unaligned>(d));
Guy Benyei11169dd2012-12-18 14:30:41 +0000687 if (N == 0)
688 return SelTable.getNullarySelector(FirstII);
689 else if (N == 1)
690 return SelTable.getUnarySelector(FirstII);
691
692 SmallVector<IdentifierInfo *, 16> Args;
693 Args.push_back(FirstII);
694 for (unsigned I = 1; I != N; ++I)
Justin Bogner57ba0b22014-03-28 22:03:24 +0000695 Args.push_back(Reader.getLocalIdentifier(
696 F, endian::readNext<uint32_t, little, unaligned>(d)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000697
698 return SelTable.getSelector(N, Args.data());
699}
700
701ASTSelectorLookupTrait::data_type
702ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
703 unsigned DataLen) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000704 using namespace llvm::support;
Guy Benyei11169dd2012-12-18 14:30:41 +0000705
706 data_type Result;
707
Justin Bogner57ba0b22014-03-28 22:03:24 +0000708 Result.ID = Reader.getGlobalSelectorID(
709 F, endian::readNext<uint32_t, little, unaligned>(d));
Nico Weberff4b35e2014-12-27 22:14:15 +0000710 unsigned FullInstanceBits = endian::readNext<uint16_t, little, unaligned>(d);
711 unsigned FullFactoryBits = endian::readNext<uint16_t, little, unaligned>(d);
712 Result.InstanceBits = FullInstanceBits & 0x3;
713 Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1;
714 Result.FactoryBits = FullFactoryBits & 0x3;
715 Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1;
716 unsigned NumInstanceMethods = FullInstanceBits >> 3;
717 unsigned NumFactoryMethods = FullFactoryBits >> 3;
Guy Benyei11169dd2012-12-18 14:30:41 +0000718
719 // Load instance methods
720 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000721 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
722 F, endian::readNext<uint32_t, little, unaligned>(d)))
Guy Benyei11169dd2012-12-18 14:30:41 +0000723 Result.Instance.push_back(Method);
724 }
725
726 // Load factory methods
727 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000728 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
729 F, endian::readNext<uint32_t, little, unaligned>(d)))
Guy Benyei11169dd2012-12-18 14:30:41 +0000730 Result.Factory.push_back(Method);
731 }
732
733 return Result;
734}
735
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000736unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
737 return llvm::HashString(a);
Guy Benyei11169dd2012-12-18 14:30:41 +0000738}
739
740std::pair<unsigned, unsigned>
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000741ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000742 using namespace llvm::support;
743 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
744 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
Guy Benyei11169dd2012-12-18 14:30:41 +0000745 return std::make_pair(KeyLen, DataLen);
746}
747
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000748ASTIdentifierLookupTraitBase::internal_key_type
749ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000750 assert(n >= 2 && d[n-1] == '\0');
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000751 return StringRef((const char*) d, n-1);
Guy Benyei11169dd2012-12-18 14:30:41 +0000752}
753
Douglas Gregordcf25082013-02-11 18:16:18 +0000754/// \brief Whether the given identifier is "interesting".
Richard Smitha534a312015-07-21 23:54:07 +0000755static bool isInterestingIdentifier(ASTReader &Reader, IdentifierInfo &II,
756 bool IsModule) {
Richard Smithcab89802015-07-17 20:19:56 +0000757 return II.hadMacroDefinition() ||
758 II.isPoisoned() ||
Richard Smith9c254182015-07-19 21:41:12 +0000759 (IsModule ? II.hasRevertedBuiltin() : II.getObjCOrBuiltinID()) ||
Douglas Gregordcf25082013-02-11 18:16:18 +0000760 II.hasRevertedTokenIDToIdentifier() ||
Richard Smitha534a312015-07-21 23:54:07 +0000761 (!(IsModule && Reader.getContext().getLangOpts().CPlusPlus) &&
762 II.getFETokenInfo<void>());
Douglas Gregordcf25082013-02-11 18:16:18 +0000763}
764
Richard Smith76c2f2c2015-07-17 20:09:43 +0000765static bool readBit(unsigned &Bits) {
766 bool Value = Bits & 0x1;
767 Bits >>= 1;
768 return Value;
769}
770
Richard Smith79bf9202015-08-24 03:33:22 +0000771IdentID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) {
772 using namespace llvm::support;
773 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
774 return Reader.getGlobalIdentifierID(F, RawID >> 1);
775}
776
Richard Smitheb4b58f62016-02-05 01:40:54 +0000777static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II) {
778 if (!II.isFromAST()) {
779 II.setIsFromAST();
780 bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr;
781 if (isInterestingIdentifier(Reader, II, IsModule))
782 II.setChangedSinceDeserialization();
783 }
784}
785
Guy Benyei11169dd2012-12-18 14:30:41 +0000786IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
787 const unsigned char* d,
788 unsigned DataLen) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000789 using namespace llvm::support;
790 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
Guy Benyei11169dd2012-12-18 14:30:41 +0000791 bool IsInteresting = RawID & 0x01;
792
793 // Wipe out the "is interesting" bit.
794 RawID = RawID >> 1;
795
Richard Smith76c2f2c2015-07-17 20:09:43 +0000796 // Build the IdentifierInfo and link the identifier ID with it.
797 IdentifierInfo *II = KnownII;
798 if (!II) {
799 II = &Reader.getIdentifierTable().getOwn(k);
800 KnownII = II;
801 }
Richard Smitheb4b58f62016-02-05 01:40:54 +0000802 markIdentifierFromAST(Reader, *II);
Richard Smith76c2f2c2015-07-17 20:09:43 +0000803 Reader.markIdentifierUpToDate(II);
804
Guy Benyei11169dd2012-12-18 14:30:41 +0000805 IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
806 if (!IsInteresting) {
Richard Smith76c2f2c2015-07-17 20:09:43 +0000807 // For uninteresting identifiers, there's nothing else to do. Just notify
808 // the reader that we've finished loading this identifier.
Guy Benyei11169dd2012-12-18 14:30:41 +0000809 Reader.SetIdentifierInfo(ID, II);
Guy Benyei11169dd2012-12-18 14:30:41 +0000810 return II;
811 }
812
Justin Bogner57ba0b22014-03-28 22:03:24 +0000813 unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d);
814 unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d);
Richard Smith76c2f2c2015-07-17 20:09:43 +0000815 bool CPlusPlusOperatorKeyword = readBit(Bits);
816 bool HasRevertedTokenIDToIdentifier = readBit(Bits);
Richard Smith9c254182015-07-19 21:41:12 +0000817 bool HasRevertedBuiltin = readBit(Bits);
Richard Smith76c2f2c2015-07-17 20:09:43 +0000818 bool Poisoned = readBit(Bits);
819 bool ExtensionToken = readBit(Bits);
820 bool HadMacroDefinition = readBit(Bits);
Guy Benyei11169dd2012-12-18 14:30:41 +0000821
822 assert(Bits == 0 && "Extra bits in the identifier?");
823 DataLen -= 8;
824
Guy Benyei11169dd2012-12-18 14:30:41 +0000825 // Set or check the various bits in the IdentifierInfo structure.
826 // Token IDs are read-only.
Argyrios Kyrtzidisddee8c92013-02-27 01:13:51 +0000827 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
Richard Smith9c254182015-07-19 21:41:12 +0000828 II->revertTokenIDToIdentifier();
829 if (!F.isModule())
830 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
831 else if (HasRevertedBuiltin && II->getBuiltinID()) {
832 II->revertBuiltin();
833 assert((II->hasRevertedBuiltin() ||
834 II->getObjCOrBuiltinID() == ObjCOrBuiltinID) &&
835 "Incorrect ObjC keyword or builtin ID");
836 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000837 assert(II->isExtensionToken() == ExtensionToken &&
838 "Incorrect extension token flag");
839 (void)ExtensionToken;
840 if (Poisoned)
841 II->setIsPoisoned(true);
842 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
843 "Incorrect C++ operator keyword flag");
844 (void)CPlusPlusOperatorKeyword;
845
846 // If this identifier is a macro, deserialize the macro
847 // definition.
Richard Smith76c2f2c2015-07-17 20:09:43 +0000848 if (HadMacroDefinition) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000849 uint32_t MacroDirectivesOffset =
850 endian::readNext<uint32_t, little, unaligned>(d);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +0000851 DataLen -= 4;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +0000852
Richard Smithd7329392015-04-21 21:46:32 +0000853 Reader.addPendingMacro(II, &F, MacroDirectivesOffset);
Guy Benyei11169dd2012-12-18 14:30:41 +0000854 }
855
856 Reader.SetIdentifierInfo(ID, II);
857
858 // Read all of the declarations visible at global scope with this
859 // name.
860 if (DataLen > 0) {
861 SmallVector<uint32_t, 4> DeclIDs;
862 for (; DataLen > 0; DataLen -= 4)
Justin Bogner57ba0b22014-03-28 22:03:24 +0000863 DeclIDs.push_back(Reader.getGlobalDeclID(
864 F, endian::readNext<uint32_t, little, unaligned>(d)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000865 Reader.SetGloballyVisibleDecls(II, DeclIDs);
866 }
867
868 return II;
869}
870
Richard Smitha06c7e62015-08-26 23:55:49 +0000871DeclarationNameKey::DeclarationNameKey(DeclarationName Name)
872 : Kind(Name.getNameKind()) {
873 switch (Kind) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000874 case DeclarationName::Identifier:
Richard Smitha06c7e62015-08-26 23:55:49 +0000875 Data = (uint64_t)Name.getAsIdentifierInfo();
Guy Benyei11169dd2012-12-18 14:30:41 +0000876 break;
877 case DeclarationName::ObjCZeroArgSelector:
878 case DeclarationName::ObjCOneArgSelector:
879 case DeclarationName::ObjCMultiArgSelector:
Richard Smitha06c7e62015-08-26 23:55:49 +0000880 Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
Guy Benyei11169dd2012-12-18 14:30:41 +0000881 break;
882 case DeclarationName::CXXOperatorName:
Richard Smitha06c7e62015-08-26 23:55:49 +0000883 Data = Name.getCXXOverloadedOperator();
884 break;
885 case DeclarationName::CXXLiteralOperatorName:
886 Data = (uint64_t)Name.getCXXLiteralIdentifier();
887 break;
888 case DeclarationName::CXXConstructorName:
889 case DeclarationName::CXXDestructorName:
890 case DeclarationName::CXXConversionFunctionName:
891 case DeclarationName::CXXUsingDirective:
892 Data = 0;
893 break;
894 }
895}
896
897unsigned DeclarationNameKey::getHash() const {
898 llvm::FoldingSetNodeID ID;
899 ID.AddInteger(Kind);
900
901 switch (Kind) {
902 case DeclarationName::Identifier:
903 case DeclarationName::CXXLiteralOperatorName:
904 ID.AddString(((IdentifierInfo*)Data)->getName());
905 break;
906 case DeclarationName::ObjCZeroArgSelector:
907 case DeclarationName::ObjCOneArgSelector:
908 case DeclarationName::ObjCMultiArgSelector:
909 ID.AddInteger(serialization::ComputeHash(Selector(Data)));
910 break;
911 case DeclarationName::CXXOperatorName:
912 ID.AddInteger((OverloadedOperatorKind)Data);
Guy Benyei11169dd2012-12-18 14:30:41 +0000913 break;
914 case DeclarationName::CXXConstructorName:
915 case DeclarationName::CXXDestructorName:
916 case DeclarationName::CXXConversionFunctionName:
917 case DeclarationName::CXXUsingDirective:
918 break;
919 }
920
921 return ID.ComputeHash();
922}
923
Richard Smithd88a7f12015-09-01 20:35:42 +0000924ModuleFile *
925ASTDeclContextNameLookupTrait::ReadFileRef(const unsigned char *&d) {
926 using namespace llvm::support;
927 uint32_t ModuleFileID = endian::readNext<uint32_t, little, unaligned>(d);
928 return Reader.getLocalModuleFile(F, ModuleFileID);
929}
930
Guy Benyei11169dd2012-12-18 14:30:41 +0000931std::pair<unsigned, unsigned>
Richard Smitha06c7e62015-08-26 23:55:49 +0000932ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char *&d) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000933 using namespace llvm::support;
934 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
935 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
Guy Benyei11169dd2012-12-18 14:30:41 +0000936 return std::make_pair(KeyLen, DataLen);
937}
938
Richard Smitha06c7e62015-08-26 23:55:49 +0000939ASTDeclContextNameLookupTrait::internal_key_type
940ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000941 using namespace llvm::support;
Guy Benyei11169dd2012-12-18 14:30:41 +0000942
Richard Smitha06c7e62015-08-26 23:55:49 +0000943 auto Kind = (DeclarationName::NameKind)*d++;
944 uint64_t Data;
945 switch (Kind) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000946 case DeclarationName::Identifier:
Richard Smitha06c7e62015-08-26 23:55:49 +0000947 Data = (uint64_t)Reader.getLocalIdentifier(
Justin Bogner57ba0b22014-03-28 22:03:24 +0000948 F, endian::readNext<uint32_t, little, unaligned>(d));
Guy Benyei11169dd2012-12-18 14:30:41 +0000949 break;
950 case DeclarationName::ObjCZeroArgSelector:
951 case DeclarationName::ObjCOneArgSelector:
952 case DeclarationName::ObjCMultiArgSelector:
Richard Smitha06c7e62015-08-26 23:55:49 +0000953 Data =
Justin Bogner57ba0b22014-03-28 22:03:24 +0000954 (uint64_t)Reader.getLocalSelector(
955 F, endian::readNext<uint32_t, little, unaligned>(
956 d)).getAsOpaquePtr();
Guy Benyei11169dd2012-12-18 14:30:41 +0000957 break;
958 case DeclarationName::CXXOperatorName:
Richard Smitha06c7e62015-08-26 23:55:49 +0000959 Data = *d++; // OverloadedOperatorKind
Guy Benyei11169dd2012-12-18 14:30:41 +0000960 break;
961 case DeclarationName::CXXLiteralOperatorName:
Richard Smitha06c7e62015-08-26 23:55:49 +0000962 Data = (uint64_t)Reader.getLocalIdentifier(
Justin Bogner57ba0b22014-03-28 22:03:24 +0000963 F, endian::readNext<uint32_t, little, unaligned>(d));
Guy Benyei11169dd2012-12-18 14:30:41 +0000964 break;
965 case DeclarationName::CXXConstructorName:
966 case DeclarationName::CXXDestructorName:
967 case DeclarationName::CXXConversionFunctionName:
968 case DeclarationName::CXXUsingDirective:
Richard Smitha06c7e62015-08-26 23:55:49 +0000969 Data = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000970 break;
971 }
972
Richard Smitha06c7e62015-08-26 23:55:49 +0000973 return DeclarationNameKey(Kind, Data);
Guy Benyei11169dd2012-12-18 14:30:41 +0000974}
975
Richard Smithd88a7f12015-09-01 20:35:42 +0000976void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type,
977 const unsigned char *d,
978 unsigned DataLen,
979 data_type_builder &Val) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000980 using namespace llvm::support;
Richard Smithd88a7f12015-09-01 20:35:42 +0000981 for (unsigned NumDecls = DataLen / 4; NumDecls; --NumDecls) {
982 uint32_t LocalID = endian::readNext<uint32_t, little, unaligned>(d);
983 Val.insert(Reader.getGlobalDeclID(F, LocalID));
984 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000985}
986
Richard Smith0f4e2c42015-08-06 04:23:48 +0000987bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M,
988 BitstreamCursor &Cursor,
989 uint64_t Offset,
990 DeclContext *DC) {
991 assert(Offset != 0);
992
Guy Benyei11169dd2012-12-18 14:30:41 +0000993 SavedStreamPosition SavedPosition(Cursor);
Richard Smith0f4e2c42015-08-06 04:23:48 +0000994 Cursor.JumpToBit(Offset);
Guy Benyei11169dd2012-12-18 14:30:41 +0000995
Richard Smith0f4e2c42015-08-06 04:23:48 +0000996 RecordData Record;
997 StringRef Blob;
998 unsigned Code = Cursor.ReadCode();
999 unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
1000 if (RecCode != DECL_CONTEXT_LEXICAL) {
1001 Error("Expected lexical block");
1002 return true;
Guy Benyei11169dd2012-12-18 14:30:41 +00001003 }
1004
Richard Smith82f8fcd2015-08-06 22:07:25 +00001005 assert(!isa<TranslationUnitDecl>(DC) &&
1006 "expected a TU_UPDATE_LEXICAL record for TU");
Richard Smith9c9173d2015-08-11 22:00:24 +00001007 // If we are handling a C++ class template instantiation, we can see multiple
1008 // lexical updates for the same record. It's important that we select only one
1009 // of them, so that field numbering works properly. Just pick the first one we
1010 // see.
1011 auto &Lex = LexicalDecls[DC];
1012 if (!Lex.first) {
1013 Lex = std::make_pair(
1014 &M, llvm::makeArrayRef(
1015 reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
1016 Blob.data()),
1017 Blob.size() / 4));
1018 }
Richard Smith0f4e2c42015-08-06 04:23:48 +00001019 DC->setHasExternalLexicalStorage(true);
1020 return false;
1021}
Guy Benyei11169dd2012-12-18 14:30:41 +00001022
Richard Smith0f4e2c42015-08-06 04:23:48 +00001023bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M,
1024 BitstreamCursor &Cursor,
1025 uint64_t Offset,
1026 DeclID ID) {
1027 assert(Offset != 0);
1028
1029 SavedStreamPosition SavedPosition(Cursor);
1030 Cursor.JumpToBit(Offset);
1031
1032 RecordData Record;
1033 StringRef Blob;
1034 unsigned Code = Cursor.ReadCode();
1035 unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
1036 if (RecCode != DECL_CONTEXT_VISIBLE) {
1037 Error("Expected visible lookup table block");
1038 return true;
Guy Benyei11169dd2012-12-18 14:30:41 +00001039 }
1040
Richard Smith0f4e2c42015-08-06 04:23:48 +00001041 // We can't safely determine the primary context yet, so delay attaching the
1042 // lookup table until we're done with recursive deserialization.
Richard Smithd88a7f12015-09-01 20:35:42 +00001043 auto *Data = (const unsigned char*)Blob.data();
1044 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data});
Guy Benyei11169dd2012-12-18 14:30:41 +00001045 return false;
1046}
1047
1048void ASTReader::Error(StringRef Msg) {
1049 Error(diag::err_fe_pch_malformed, Msg);
Richard Smithfb1e7f72015-08-14 05:02:58 +00001050 if (Context.getLangOpts().Modules && !Diags.isDiagnosticInFlight() &&
1051 !PP.getHeaderSearchInfo().getModuleCachePath().empty()) {
Douglas Gregor940e8052013-05-10 22:15:13 +00001052 Diag(diag::note_module_cache_path)
1053 << PP.getHeaderSearchInfo().getModuleCachePath();
1054 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001055}
1056
1057void ASTReader::Error(unsigned DiagID,
1058 StringRef Arg1, StringRef Arg2) {
1059 if (Diags.isDiagnosticInFlight())
1060 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
1061 else
1062 Diag(DiagID) << Arg1 << Arg2;
1063}
1064
1065//===----------------------------------------------------------------------===//
1066// Source Manager Deserialization
1067//===----------------------------------------------------------------------===//
1068
1069/// \brief Read the line table in the source manager block.
1070/// \returns true if there was an error.
1071bool ASTReader::ParseLineTable(ModuleFile &F,
Richard Smith7ed1bc92014-12-05 22:42:13 +00001072 const RecordData &Record) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001073 unsigned Idx = 0;
1074 LineTableInfo &LineTable = SourceMgr.getLineTable();
1075
1076 // Parse the file names
1077 std::map<int, int> FileIDs;
Richard Smith63078492015-09-01 07:41:55 +00001078 for (unsigned I = 0; Record[Idx]; ++I) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001079 // Extract the file name
Richard Smith7ed1bc92014-12-05 22:42:13 +00001080 auto Filename = ReadPath(F, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00001081 FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
1082 }
Richard Smith63078492015-09-01 07:41:55 +00001083 ++Idx;
Guy Benyei11169dd2012-12-18 14:30:41 +00001084
1085 // Parse the line entries
1086 std::vector<LineEntry> Entries;
1087 while (Idx < Record.size()) {
1088 int FID = Record[Idx++];
1089 assert(FID >= 0 && "Serialized line entries for non-local file.");
1090 // Remap FileID from 1-based old view.
1091 FID += F.SLocEntryBaseID - 1;
1092
1093 // Extract the line entries
1094 unsigned NumEntries = Record[Idx++];
Richard Smith63078492015-09-01 07:41:55 +00001095 assert(NumEntries && "no line entries for file ID");
Guy Benyei11169dd2012-12-18 14:30:41 +00001096 Entries.clear();
1097 Entries.reserve(NumEntries);
1098 for (unsigned I = 0; I != NumEntries; ++I) {
1099 unsigned FileOffset = Record[Idx++];
1100 unsigned LineNo = Record[Idx++];
1101 int FilenameID = FileIDs[Record[Idx++]];
1102 SrcMgr::CharacteristicKind FileKind
1103 = (SrcMgr::CharacteristicKind)Record[Idx++];
1104 unsigned IncludeOffset = Record[Idx++];
1105 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1106 FileKind, IncludeOffset));
1107 }
1108 LineTable.AddEntry(FileID::get(FID), Entries);
1109 }
1110
1111 return false;
1112}
1113
1114/// \brief Read a source manager block
1115bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1116 using namespace SrcMgr;
1117
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001118 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001119
1120 // Set the source-location entry cursor to the current position in
1121 // the stream. This cursor will be used to read the contents of the
1122 // source manager block initially, and then lazily read
1123 // source-location entries as needed.
1124 SLocEntryCursor = F.Stream;
1125
1126 // The stream itself is going to skip over the source manager block.
1127 if (F.Stream.SkipBlock()) {
1128 Error("malformed block record in AST file");
1129 return true;
1130 }
1131
1132 // Enter the source manager block.
1133 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
1134 Error("malformed source manager block record in AST file");
1135 return true;
1136 }
1137
1138 RecordData Record;
1139 while (true) {
Chris Lattnere7b154b2013-01-19 21:39:22 +00001140 llvm::BitstreamEntry E = SLocEntryCursor.advanceSkippingSubblocks();
1141
1142 switch (E.Kind) {
1143 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1144 case llvm::BitstreamEntry::Error:
1145 Error("malformed block record in AST file");
1146 return true;
1147 case llvm::BitstreamEntry::EndBlock:
Guy Benyei11169dd2012-12-18 14:30:41 +00001148 return false;
Chris Lattnere7b154b2013-01-19 21:39:22 +00001149 case llvm::BitstreamEntry::Record:
1150 // The interesting case.
1151 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001152 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00001153
Guy Benyei11169dd2012-12-18 14:30:41 +00001154 // Read a record.
Guy Benyei11169dd2012-12-18 14:30:41 +00001155 Record.clear();
Chris Lattner15c3e7d2013-01-21 18:28:26 +00001156 StringRef Blob;
1157 switch (SLocEntryCursor.readRecord(E.ID, Record, &Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001158 default: // Default behavior: ignore.
1159 break;
1160
1161 case SM_SLOC_FILE_ENTRY:
1162 case SM_SLOC_BUFFER_ENTRY:
1163 case SM_SLOC_EXPANSION_ENTRY:
1164 // Once we hit one of the source location entries, we're done.
1165 return false;
1166 }
1167 }
1168}
1169
1170/// \brief If a header file is not found at the path that we expect it to be
1171/// and the PCH file was moved from its original location, try to resolve the
1172/// file by assuming that header+PCH were moved together and the header is in
1173/// the same place relative to the PCH.
1174static std::string
1175resolveFileRelativeToOriginalDir(const std::string &Filename,
1176 const std::string &OriginalDir,
1177 const std::string &CurrDir) {
1178 assert(OriginalDir != CurrDir &&
1179 "No point trying to resolve the file if the PCH dir didn't change");
1180 using namespace llvm::sys;
1181 SmallString<128> filePath(Filename);
1182 fs::make_absolute(filePath);
1183 assert(path::is_absolute(OriginalDir));
1184 SmallString<128> currPCHPath(CurrDir);
1185
1186 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1187 fileDirE = path::end(path::parent_path(filePath));
1188 path::const_iterator origDirI = path::begin(OriginalDir),
1189 origDirE = path::end(OriginalDir);
1190 // Skip the common path components from filePath and OriginalDir.
1191 while (fileDirI != fileDirE && origDirI != origDirE &&
1192 *fileDirI == *origDirI) {
1193 ++fileDirI;
1194 ++origDirI;
1195 }
1196 for (; origDirI != origDirE; ++origDirI)
1197 path::append(currPCHPath, "..");
1198 path::append(currPCHPath, fileDirI, fileDirE);
1199 path::append(currPCHPath, path::filename(Filename));
1200 return currPCHPath.str();
1201}
1202
1203bool ASTReader::ReadSLocEntry(int ID) {
1204 if (ID == 0)
1205 return false;
1206
1207 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1208 Error("source location entry ID out-of-range for AST file");
1209 return true;
1210 }
1211
Richard Smithaada85c2016-02-06 02:06:43 +00001212 // Local helper to read the (possibly-compressed) buffer data following the
1213 // entry record.
1214 auto ReadBuffer = [this](
1215 BitstreamCursor &SLocEntryCursor,
1216 StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> {
1217 RecordData Record;
1218 StringRef Blob;
1219 unsigned Code = SLocEntryCursor.ReadCode();
1220 unsigned RecCode = SLocEntryCursor.readRecord(Code, Record, &Blob);
1221
1222 if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) {
1223 SmallString<0> Uncompressed;
1224 if (llvm::zlib::uncompress(Blob, Uncompressed, Record[0]) !=
1225 llvm::zlib::StatusOK) {
1226 Error("could not decompress embedded file contents");
1227 return nullptr;
1228 }
1229 return llvm::MemoryBuffer::getMemBufferCopy(Uncompressed, Name);
1230 } else if (RecCode == SM_SLOC_BUFFER_BLOB) {
1231 return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true);
1232 } else {
1233 Error("AST record has invalid code");
1234 return nullptr;
1235 }
1236 };
1237
Guy Benyei11169dd2012-12-18 14:30:41 +00001238 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
1239 F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]);
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001240 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001241 unsigned BaseOffset = F->SLocEntryBaseOffset;
1242
1243 ++NumSLocEntriesRead;
Chris Lattnere7b154b2013-01-19 21:39:22 +00001244 llvm::BitstreamEntry Entry = SLocEntryCursor.advance();
1245 if (Entry.Kind != llvm::BitstreamEntry::Record) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001246 Error("incorrectly-formatted source location entry in AST file");
1247 return true;
1248 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00001249
Guy Benyei11169dd2012-12-18 14:30:41 +00001250 RecordData Record;
Chris Lattner0e6c9402013-01-20 02:38:54 +00001251 StringRef Blob;
1252 switch (SLocEntryCursor.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001253 default:
1254 Error("incorrectly-formatted source location entry in AST file");
1255 return true;
1256
1257 case SM_SLOC_FILE_ENTRY: {
1258 // We will detect whether a file changed and return 'Failure' for it, but
1259 // we will also try to fail gracefully by setting up the SLocEntry.
1260 unsigned InputID = Record[4];
1261 InputFile IF = getInputFile(*F, InputID);
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001262 const FileEntry *File = IF.getFile();
1263 bool OverriddenBuffer = IF.isOverridden();
Guy Benyei11169dd2012-12-18 14:30:41 +00001264
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001265 // Note that we only check if a File was returned. If it was out-of-date
1266 // we have complained but we will continue creating a FileID to recover
1267 // gracefully.
1268 if (!File)
Guy Benyei11169dd2012-12-18 14:30:41 +00001269 return true;
1270
1271 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1272 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1273 // This is the module's main file.
1274 IncludeLoc = getImportLocation(F);
1275 }
1276 SrcMgr::CharacteristicKind
1277 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1278 FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
1279 ID, BaseOffset + Record[0]);
1280 SrcMgr::FileInfo &FileInfo =
1281 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1282 FileInfo.NumCreatedFIDs = Record[5];
1283 if (Record[3])
1284 FileInfo.setHasLineDirectives();
1285
1286 const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
1287 unsigned NumFileDecls = Record[7];
1288 if (NumFileDecls) {
1289 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1290 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1291 NumFileDecls));
1292 }
Richard Smithaada85c2016-02-06 02:06:43 +00001293
Guy Benyei11169dd2012-12-18 14:30:41 +00001294 const SrcMgr::ContentCache *ContentCache
1295 = SourceMgr.getOrCreateContentCache(File,
1296 /*isSystemFile=*/FileCharacter != SrcMgr::C_User);
1297 if (OverriddenBuffer && !ContentCache->BufferOverridden &&
Richard Smitha8cfffa2015-11-26 02:04:16 +00001298 ContentCache->ContentsEntry == ContentCache->OrigEntry &&
1299 !ContentCache->getRawBuffer()) {
Richard Smithaada85c2016-02-06 02:06:43 +00001300 auto Buffer = ReadBuffer(SLocEntryCursor, File->getName());
1301 if (!Buffer)
Guy Benyei11169dd2012-12-18 14:30:41 +00001302 return true;
David Blaikie49cc3182014-08-27 20:54:45 +00001303 SourceMgr.overrideFileContents(File, std::move(Buffer));
Guy Benyei11169dd2012-12-18 14:30:41 +00001304 }
1305
1306 break;
1307 }
1308
1309 case SM_SLOC_BUFFER_ENTRY: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00001310 const char *Name = Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00001311 unsigned Offset = Record[0];
1312 SrcMgr::CharacteristicKind
1313 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1314 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
Richard Smithe842a472014-10-22 02:05:46 +00001315 if (IncludeLoc.isInvalid() &&
1316 (F->Kind == MK_ImplicitModule || F->Kind == MK_ExplicitModule)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001317 IncludeLoc = getImportLocation(F);
1318 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001319
Richard Smithaada85c2016-02-06 02:06:43 +00001320 auto Buffer = ReadBuffer(SLocEntryCursor, Name);
1321 if (!Buffer)
Guy Benyei11169dd2012-12-18 14:30:41 +00001322 return true;
David Blaikie50a5f972014-08-29 07:59:55 +00001323 SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID,
Rafael Espindolad87f8d72014-08-27 20:03:29 +00001324 BaseOffset + Offset, IncludeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001325 break;
1326 }
1327
1328 case SM_SLOC_EXPANSION_ENTRY: {
1329 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1330 SourceMgr.createExpansionLoc(SpellingLoc,
1331 ReadSourceLocation(*F, Record[2]),
1332 ReadSourceLocation(*F, Record[3]),
1333 Record[4],
1334 ID,
1335 BaseOffset + Record[0]);
1336 break;
1337 }
1338 }
1339
1340 return false;
1341}
1342
1343std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1344 if (ID == 0)
1345 return std::make_pair(SourceLocation(), "");
1346
1347 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1348 Error("source location entry ID out-of-range for AST file");
1349 return std::make_pair(SourceLocation(), "");
1350 }
1351
1352 // Find which module file this entry lands in.
1353 ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
Richard Smithe842a472014-10-22 02:05:46 +00001354 if (M->Kind != MK_ImplicitModule && M->Kind != MK_ExplicitModule)
Guy Benyei11169dd2012-12-18 14:30:41 +00001355 return std::make_pair(SourceLocation(), "");
1356
1357 // FIXME: Can we map this down to a particular submodule? That would be
1358 // ideal.
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001359 return std::make_pair(M->ImportLoc, StringRef(M->ModuleName));
Guy Benyei11169dd2012-12-18 14:30:41 +00001360}
1361
1362/// \brief Find the location where the module F is imported.
1363SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1364 if (F->ImportLoc.isValid())
1365 return F->ImportLoc;
1366
1367 // Otherwise we have a PCH. It's considered to be "imported" at the first
1368 // location of its includer.
1369 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001370 // Main file is the importer.
Yaron Keren8b563662015-10-03 10:46:20 +00001371 assert(SourceMgr.getMainFileID().isValid() && "missing main file");
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001372 return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
Guy Benyei11169dd2012-12-18 14:30:41 +00001373 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001374 return F->ImportedBy[0]->FirstLoc;
1375}
1376
1377/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1378/// specified cursor. Read the abbreviations that are at the top of the block
1379/// and then leave the cursor pointing into the block.
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001380bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID) {
Richard Smith0516b182015-09-08 19:40:14 +00001381 if (Cursor.EnterSubBlock(BlockID))
1382 return true;
Guy Benyei11169dd2012-12-18 14:30:41 +00001383
1384 while (true) {
1385 uint64_t Offset = Cursor.GetCurrentBitNo();
1386 unsigned Code = Cursor.ReadCode();
1387
1388 // We expect all abbrevs to be at the start of the block.
1389 if (Code != llvm::bitc::DEFINE_ABBREV) {
1390 Cursor.JumpToBit(Offset);
1391 return false;
1392 }
1393 Cursor.ReadAbbrevRecord();
1394 }
1395}
1396
Richard Smithe40f2ba2013-08-07 21:41:30 +00001397Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record,
John McCallf413f5e2013-05-03 00:10:13 +00001398 unsigned &Idx) {
1399 Token Tok;
1400 Tok.startToken();
1401 Tok.setLocation(ReadSourceLocation(F, Record, Idx));
1402 Tok.setLength(Record[Idx++]);
1403 if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
1404 Tok.setIdentifierInfo(II);
1405 Tok.setKind((tok::TokenKind)Record[Idx++]);
1406 Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1407 return Tok;
1408}
1409
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001410MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001411 BitstreamCursor &Stream = F.MacroCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001412
1413 // Keep track of where we are in the stream, then jump back there
1414 // after reading this macro.
1415 SavedStreamPosition SavedPosition(Stream);
1416
1417 Stream.JumpToBit(Offset);
1418 RecordData Record;
1419 SmallVector<IdentifierInfo*, 16> MacroArgs;
Craig Toppera13603a2014-05-22 05:54:18 +00001420 MacroInfo *Macro = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001421
Guy Benyei11169dd2012-12-18 14:30:41 +00001422 while (true) {
Chris Lattnerefa77172013-01-20 00:00:22 +00001423 // Advance to the next record, but if we get to the end of the block, don't
1424 // pop it (removing all the abbreviations from the cursor) since we want to
1425 // be able to reseek within the block and read entries.
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001426 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
Chris Lattnerefa77172013-01-20 00:00:22 +00001427 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks(Flags);
1428
1429 switch (Entry.Kind) {
1430 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1431 case llvm::BitstreamEntry::Error:
1432 Error("malformed block record in AST file");
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001433 return Macro;
Chris Lattnerefa77172013-01-20 00:00:22 +00001434 case llvm::BitstreamEntry::EndBlock:
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001435 return Macro;
Chris Lattnerefa77172013-01-20 00:00:22 +00001436 case llvm::BitstreamEntry::Record:
1437 // The interesting case.
1438 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001439 }
1440
1441 // Read a record.
Guy Benyei11169dd2012-12-18 14:30:41 +00001442 Record.clear();
1443 PreprocessorRecordTypes RecType =
Chris Lattner0e6c9402013-01-20 02:38:54 +00001444 (PreprocessorRecordTypes)Stream.readRecord(Entry.ID, Record);
Guy Benyei11169dd2012-12-18 14:30:41 +00001445 switch (RecType) {
Richard Smithd7329392015-04-21 21:46:32 +00001446 case PP_MODULE_MACRO:
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001447 case PP_MACRO_DIRECTIVE_HISTORY:
1448 return Macro;
1449
Guy Benyei11169dd2012-12-18 14:30:41 +00001450 case PP_MACRO_OBJECT_LIKE:
1451 case PP_MACRO_FUNCTION_LIKE: {
1452 // If we already have a macro, that means that we've hit the end
1453 // of the definition of the macro we were looking for. We're
1454 // done.
1455 if (Macro)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001456 return Macro;
Guy Benyei11169dd2012-12-18 14:30:41 +00001457
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001458 unsigned NextIndex = 1; // Skip identifier ID.
1459 SubmoduleID SubModID = getGlobalSubmoduleID(F, Record[NextIndex++]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001460 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001461 MacroInfo *MI = PP.AllocateDeserializedMacroInfo(Loc, SubModID);
Argyrios Kyrtzidis7572be22013-01-07 19:16:23 +00001462 MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
Guy Benyei11169dd2012-12-18 14:30:41 +00001463 MI->setIsUsed(Record[NextIndex++]);
Argyrios Kyrtzidis9ef53ce2014-04-09 18:21:23 +00001464 MI->setUsedForHeaderGuard(Record[NextIndex++]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001465
Guy Benyei11169dd2012-12-18 14:30:41 +00001466 if (RecType == PP_MACRO_FUNCTION_LIKE) {
1467 // Decode function-like macro info.
1468 bool isC99VarArgs = Record[NextIndex++];
1469 bool isGNUVarArgs = Record[NextIndex++];
1470 bool hasCommaPasting = Record[NextIndex++];
1471 MacroArgs.clear();
1472 unsigned NumArgs = Record[NextIndex++];
1473 for (unsigned i = 0; i != NumArgs; ++i)
1474 MacroArgs.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1475
1476 // Install function-like macro info.
1477 MI->setIsFunctionLike();
1478 if (isC99VarArgs) MI->setIsC99Varargs();
1479 if (isGNUVarArgs) MI->setIsGNUVarargs();
1480 if (hasCommaPasting) MI->setHasCommaPasting();
Craig Topperd96b3f92015-10-22 04:59:52 +00001481 MI->setArgumentList(MacroArgs, PP.getPreprocessorAllocator());
Guy Benyei11169dd2012-12-18 14:30:41 +00001482 }
1483
Guy Benyei11169dd2012-12-18 14:30:41 +00001484 // Remember that we saw this macro last so that we add the tokens that
1485 // form its body to it.
1486 Macro = MI;
1487
1488 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1489 Record[NextIndex]) {
1490 // We have a macro definition. Register the association
1491 PreprocessedEntityID
1492 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1493 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
Richard Smith66a81862015-05-04 02:25:31 +00001494 PreprocessingRecord::PPEntityID PPID =
1495 PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true);
1496 MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
1497 PPRec.getPreprocessedEntity(PPID));
Argyrios Kyrtzidis832de9f2013-02-22 18:35:59 +00001498 if (PPDef)
1499 PPRec.RegisterMacroDefinition(Macro, PPDef);
Guy Benyei11169dd2012-12-18 14:30:41 +00001500 }
1501
1502 ++NumMacrosRead;
1503 break;
1504 }
1505
1506 case PP_TOKEN: {
1507 // If we see a TOKEN before a PP_MACRO_*, then the file is
1508 // erroneous, just pretend we didn't see this.
Craig Toppera13603a2014-05-22 05:54:18 +00001509 if (!Macro) break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001510
John McCallf413f5e2013-05-03 00:10:13 +00001511 unsigned Idx = 0;
1512 Token Tok = ReadToken(F, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00001513 Macro->AddTokenToBody(Tok);
1514 break;
1515 }
1516 }
1517 }
1518}
1519
1520PreprocessedEntityID
1521ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const {
1522 ContinuousRangeMap<uint32_t, int, 2>::const_iterator
1523 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1524 assert(I != M.PreprocessedEntityRemap.end()
1525 && "Invalid index into preprocessed entity index remap");
1526
1527 return LocalID + I->second;
1528}
1529
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001530unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1531 return llvm::hash_combine(ikey.Size, ikey.ModTime);
Guy Benyei11169dd2012-12-18 14:30:41 +00001532}
Richard Smith7ed1bc92014-12-05 22:42:13 +00001533
Guy Benyei11169dd2012-12-18 14:30:41 +00001534HeaderFileInfoTrait::internal_key_type
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001535HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
Richard Smithe75ee0f2015-08-17 07:13:32 +00001536 internal_key_type ikey = {FE->getSize(),
1537 M.HasTimestamps ? FE->getModificationTime() : 0,
1538 FE->getName(), /*Imported*/ false};
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001539 return ikey;
1540}
Guy Benyei11169dd2012-12-18 14:30:41 +00001541
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001542bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
Richard Smithe75ee0f2015-08-17 07:13:32 +00001543 if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime))
Guy Benyei11169dd2012-12-18 14:30:41 +00001544 return false;
1545
Richard Smith7ed1bc92014-12-05 22:42:13 +00001546 if (llvm::sys::path::is_absolute(a.Filename) &&
1547 strcmp(a.Filename, b.Filename) == 0)
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001548 return true;
1549
Guy Benyei11169dd2012-12-18 14:30:41 +00001550 // Determine whether the actual files are equivalent.
Argyrios Kyrtzidis2a513e82013-03-04 20:33:40 +00001551 FileManager &FileMgr = Reader.getFileManager();
Richard Smith7ed1bc92014-12-05 22:42:13 +00001552 auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* {
1553 if (!Key.Imported)
1554 return FileMgr.getFile(Key.Filename);
1555
1556 std::string Resolved = Key.Filename;
1557 Reader.ResolveImportedPath(M, Resolved);
1558 return FileMgr.getFile(Resolved);
1559 };
1560
1561 const FileEntry *FEA = GetFile(a);
1562 const FileEntry *FEB = GetFile(b);
1563 return FEA && FEA == FEB;
Guy Benyei11169dd2012-12-18 14:30:41 +00001564}
1565
1566std::pair<unsigned, unsigned>
1567HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
Justin Bogner57ba0b22014-03-28 22:03:24 +00001568 using namespace llvm::support;
1569 unsigned KeyLen = (unsigned) endian::readNext<uint16_t, little, unaligned>(d);
Guy Benyei11169dd2012-12-18 14:30:41 +00001570 unsigned DataLen = (unsigned) *d++;
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001571 return std::make_pair(KeyLen, DataLen);
Guy Benyei11169dd2012-12-18 14:30:41 +00001572}
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001573
1574HeaderFileInfoTrait::internal_key_type
1575HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
Justin Bogner57ba0b22014-03-28 22:03:24 +00001576 using namespace llvm::support;
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001577 internal_key_type ikey;
Justin Bogner57ba0b22014-03-28 22:03:24 +00001578 ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d));
1579 ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d));
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001580 ikey.Filename = (const char *)d;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001581 ikey.Imported = true;
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001582 return ikey;
1583}
1584
Guy Benyei11169dd2012-12-18 14:30:41 +00001585HeaderFileInfoTrait::data_type
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001586HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
Guy Benyei11169dd2012-12-18 14:30:41 +00001587 unsigned DataLen) {
1588 const unsigned char *End = d + DataLen;
Justin Bogner57ba0b22014-03-28 22:03:24 +00001589 using namespace llvm::support;
Guy Benyei11169dd2012-12-18 14:30:41 +00001590 HeaderFileInfo HFI;
1591 unsigned Flags = *d++;
Richard Smith386bb072015-08-18 23:42:23 +00001592 // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp.
1593 HFI.isImport |= (Flags >> 4) & 0x01;
1594 HFI.isPragmaOnce |= (Flags >> 3) & 0x01;
1595 HFI.DirInfo = (Flags >> 1) & 0x03;
Guy Benyei11169dd2012-12-18 14:30:41 +00001596 HFI.IndexHeaderMapHeader = Flags & 0x01;
Richard Smith386bb072015-08-18 23:42:23 +00001597 // FIXME: Find a better way to handle this. Maybe just store a
1598 // "has been included" flag?
1599 HFI.NumIncludes = std::max(endian::readNext<uint16_t, little, unaligned>(d),
1600 HFI.NumIncludes);
Justin Bogner57ba0b22014-03-28 22:03:24 +00001601 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(
1602 M, endian::readNext<uint32_t, little, unaligned>(d));
1603 if (unsigned FrameworkOffset =
1604 endian::readNext<uint32_t, little, unaligned>(d)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001605 // The framework offset is 1 greater than the actual offset,
1606 // since 0 is used as an indicator for "no framework name".
1607 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1608 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1609 }
Richard Smith386bb072015-08-18 23:42:23 +00001610
1611 assert((End - d) % 4 == 0 &&
1612 "Wrong data length in HeaderFileInfo deserialization");
1613 while (d != End) {
Justin Bogner57ba0b22014-03-28 22:03:24 +00001614 uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d);
Richard Smith386bb072015-08-18 23:42:23 +00001615 auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 3);
1616 LocalSMID >>= 2;
1617
1618 // This header is part of a module. Associate it with the module to enable
1619 // implicit module import.
1620 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1621 Module *Mod = Reader.getSubmodule(GlobalSMID);
1622 FileManager &FileMgr = Reader.getFileManager();
1623 ModuleMap &ModMap =
1624 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
1625
1626 std::string Filename = key.Filename;
1627 if (key.Imported)
1628 Reader.ResolveImportedPath(M, Filename);
1629 // FIXME: This is not always the right filename-as-written, but we're not
1630 // going to use this information to rebuild the module, so it doesn't make
1631 // a lot of difference.
1632 Module::Header H = { key.Filename, FileMgr.getFile(Filename) };
Richard Smithd8879c82015-08-24 21:59:32 +00001633 ModMap.addHeader(Mod, H, HeaderRole, /*Imported*/true);
1634 HFI.isModuleHeader |= !(HeaderRole & ModuleMap::TextualHeader);
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001635 }
1636
Guy Benyei11169dd2012-12-18 14:30:41 +00001637 // This HeaderFileInfo was externally loaded.
1638 HFI.External = true;
Richard Smithd8879c82015-08-24 21:59:32 +00001639 HFI.IsValid = true;
Guy Benyei11169dd2012-12-18 14:30:41 +00001640 return HFI;
1641}
1642
Richard Smithd7329392015-04-21 21:46:32 +00001643void ASTReader::addPendingMacro(IdentifierInfo *II,
1644 ModuleFile *M,
1645 uint64_t MacroDirectivesOffset) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001646 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1647 PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00001648}
1649
1650void ASTReader::ReadDefinedMacros() {
1651 // Note that we are loading defined macros.
1652 Deserializing Macros(this);
1653
Pete Cooper57d3f142015-07-30 17:22:52 +00001654 for (auto &I : llvm::reverse(ModuleMgr)) {
1655 BitstreamCursor &MacroCursor = I->MacroCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001656
1657 // If there was no preprocessor block, skip this file.
1658 if (!MacroCursor.getBitStreamReader())
1659 continue;
1660
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001661 BitstreamCursor Cursor = MacroCursor;
Pete Cooper57d3f142015-07-30 17:22:52 +00001662 Cursor.JumpToBit(I->MacroStartOffset);
Guy Benyei11169dd2012-12-18 14:30:41 +00001663
1664 RecordData Record;
1665 while (true) {
Chris Lattnere7b154b2013-01-19 21:39:22 +00001666 llvm::BitstreamEntry E = Cursor.advanceSkippingSubblocks();
1667
1668 switch (E.Kind) {
1669 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1670 case llvm::BitstreamEntry::Error:
1671 Error("malformed block record in AST file");
1672 return;
1673 case llvm::BitstreamEntry::EndBlock:
1674 goto NextCursor;
1675
1676 case llvm::BitstreamEntry::Record:
Chris Lattnere7b154b2013-01-19 21:39:22 +00001677 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00001678 switch (Cursor.readRecord(E.ID, Record)) {
Chris Lattnere7b154b2013-01-19 21:39:22 +00001679 default: // Default behavior: ignore.
1680 break;
1681
1682 case PP_MACRO_OBJECT_LIKE:
Sean Callananf3682a72016-05-14 06:24:14 +00001683 case PP_MACRO_FUNCTION_LIKE: {
1684 IdentifierInfo *II = getLocalIdentifier(*I, Record[0]);
1685 if (II->isOutOfDate())
1686 updateOutOfDateIdentifier(*II);
Chris Lattnere7b154b2013-01-19 21:39:22 +00001687 break;
Sean Callananf3682a72016-05-14 06:24:14 +00001688 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00001689
1690 case PP_TOKEN:
1691 // Ignore tokens.
1692 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001693 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001694 break;
1695 }
1696 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00001697 NextCursor: ;
Guy Benyei11169dd2012-12-18 14:30:41 +00001698 }
1699}
1700
1701namespace {
1702 /// \brief Visitor class used to look up identifirs in an AST file.
1703 class IdentifierLookupVisitor {
1704 StringRef Name;
Richard Smith3b637412015-07-14 18:42:41 +00001705 unsigned NameHash;
Guy Benyei11169dd2012-12-18 14:30:41 +00001706 unsigned PriorGeneration;
Douglas Gregor00a50f72013-01-25 00:38:33 +00001707 unsigned &NumIdentifierLookups;
1708 unsigned &NumIdentifierLookupHits;
Guy Benyei11169dd2012-12-18 14:30:41 +00001709 IdentifierInfo *Found;
Douglas Gregor00a50f72013-01-25 00:38:33 +00001710
Guy Benyei11169dd2012-12-18 14:30:41 +00001711 public:
Douglas Gregor00a50f72013-01-25 00:38:33 +00001712 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
1713 unsigned &NumIdentifierLookups,
1714 unsigned &NumIdentifierLookupHits)
Richard Smith3b637412015-07-14 18:42:41 +00001715 : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)),
1716 PriorGeneration(PriorGeneration),
Douglas Gregor00a50f72013-01-25 00:38:33 +00001717 NumIdentifierLookups(NumIdentifierLookups),
1718 NumIdentifierLookupHits(NumIdentifierLookupHits),
1719 Found()
1720 {
1721 }
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00001722
1723 bool operator()(ModuleFile &M) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001724 // If we've already searched this module file, skip it now.
Richard Smithbdf2d932015-07-30 03:37:16 +00001725 if (M.Generation <= PriorGeneration)
Guy Benyei11169dd2012-12-18 14:30:41 +00001726 return true;
Douglas Gregore060e572013-01-25 01:03:03 +00001727
Guy Benyei11169dd2012-12-18 14:30:41 +00001728 ASTIdentifierLookupTable *IdTable
1729 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
1730 if (!IdTable)
1731 return false;
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00001732
1733 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M,
Richard Smithbdf2d932015-07-30 03:37:16 +00001734 Found);
1735 ++NumIdentifierLookups;
Richard Smith3b637412015-07-14 18:42:41 +00001736 ASTIdentifierLookupTable::iterator Pos =
Richard Smithbdf2d932015-07-30 03:37:16 +00001737 IdTable->find_hashed(Name, NameHash, &Trait);
Guy Benyei11169dd2012-12-18 14:30:41 +00001738 if (Pos == IdTable->end())
1739 return false;
1740
1741 // Dereferencing the iterator has the effect of building the
1742 // IdentifierInfo node and populating it with the various
1743 // declarations it needs.
Richard Smithbdf2d932015-07-30 03:37:16 +00001744 ++NumIdentifierLookupHits;
1745 Found = *Pos;
Guy Benyei11169dd2012-12-18 14:30:41 +00001746 return true;
1747 }
1748
1749 // \brief Retrieve the identifier info found within the module
1750 // files.
1751 IdentifierInfo *getIdentifierInfo() const { return Found; }
1752 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +00001753}
Guy Benyei11169dd2012-12-18 14:30:41 +00001754
1755void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
1756 // Note that we are loading an identifier.
1757 Deserializing AnIdentifier(this);
1758
1759 unsigned PriorGeneration = 0;
1760 if (getContext().getLangOpts().Modules)
1761 PriorGeneration = IdentifierGeneration[&II];
Douglas Gregore060e572013-01-25 01:03:03 +00001762
1763 // If there is a global index, look there first to determine which modules
1764 // provably do not have any results for this identifier.
Douglas Gregor7211ac12013-01-25 23:32:03 +00001765 GlobalModuleIndex::HitSet Hits;
Craig Toppera13603a2014-05-22 05:54:18 +00001766 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
Douglas Gregore060e572013-01-25 01:03:03 +00001767 if (!loadGlobalIndex()) {
Douglas Gregor7211ac12013-01-25 23:32:03 +00001768 if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
1769 HitsPtr = &Hits;
Douglas Gregore060e572013-01-25 01:03:03 +00001770 }
1771 }
1772
Douglas Gregor7211ac12013-01-25 23:32:03 +00001773 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
Douglas Gregor00a50f72013-01-25 00:38:33 +00001774 NumIdentifierLookups,
1775 NumIdentifierLookupHits);
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00001776 ModuleMgr.visit(Visitor, HitsPtr);
Guy Benyei11169dd2012-12-18 14:30:41 +00001777 markIdentifierUpToDate(&II);
1778}
1779
1780void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
1781 if (!II)
1782 return;
1783
1784 II->setOutOfDate(false);
1785
1786 // Update the generation for this identifier.
1787 if (getContext().getLangOpts().Modules)
Richard Smith053f6c62014-05-16 23:01:30 +00001788 IdentifierGeneration[II] = getGeneration();
Guy Benyei11169dd2012-12-18 14:30:41 +00001789}
1790
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001791void ASTReader::resolvePendingMacro(IdentifierInfo *II,
1792 const PendingMacroInfo &PMInfo) {
Richard Smithd7329392015-04-21 21:46:32 +00001793 ModuleFile &M = *PMInfo.M;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001794
1795 BitstreamCursor &Cursor = M.MacroCursor;
1796 SavedStreamPosition SavedPosition(Cursor);
Richard Smithd7329392015-04-21 21:46:32 +00001797 Cursor.JumpToBit(PMInfo.MacroDirectivesOffset);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001798
Richard Smith713369b2015-04-23 20:40:50 +00001799 struct ModuleMacroRecord {
1800 SubmoduleID SubModID;
1801 MacroInfo *MI;
1802 SmallVector<SubmoduleID, 8> Overrides;
1803 };
1804 llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001805
Richard Smithd7329392015-04-21 21:46:32 +00001806 // We expect to see a sequence of PP_MODULE_MACRO records listing exported
1807 // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
1808 // macro histroy.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001809 RecordData Record;
Richard Smithd7329392015-04-21 21:46:32 +00001810 while (true) {
1811 llvm::BitstreamEntry Entry =
1812 Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
1813 if (Entry.Kind != llvm::BitstreamEntry::Record) {
1814 Error("malformed block record in AST file");
1815 return;
1816 }
1817
1818 Record.clear();
Aaron Ballmanc75a1922015-04-22 15:25:05 +00001819 switch ((PreprocessorRecordTypes)Cursor.readRecord(Entry.ID, Record)) {
Richard Smithd7329392015-04-21 21:46:32 +00001820 case PP_MACRO_DIRECTIVE_HISTORY:
1821 break;
1822
1823 case PP_MODULE_MACRO: {
Richard Smith713369b2015-04-23 20:40:50 +00001824 ModuleMacros.push_back(ModuleMacroRecord());
1825 auto &Info = ModuleMacros.back();
Richard Smithe56c8bc2015-04-22 00:26:11 +00001826 Info.SubModID = getGlobalSubmoduleID(M, Record[0]);
1827 Info.MI = getMacro(getGlobalMacroID(M, Record[1]));
Richard Smith713369b2015-04-23 20:40:50 +00001828 for (int I = 2, N = Record.size(); I != N; ++I)
1829 Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I]));
Richard Smithd7329392015-04-21 21:46:32 +00001830 continue;
1831 }
1832
1833 default:
1834 Error("malformed block record in AST file");
1835 return;
1836 }
1837
1838 // We found the macro directive history; that's the last record
1839 // for this macro.
1840 break;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001841 }
1842
Richard Smithd7329392015-04-21 21:46:32 +00001843 // Module macros are listed in reverse dependency order.
Richard Smithe56c8bc2015-04-22 00:26:11 +00001844 {
1845 std::reverse(ModuleMacros.begin(), ModuleMacros.end());
Richard Smithe56c8bc2015-04-22 00:26:11 +00001846 llvm::SmallVector<ModuleMacro*, 8> Overrides;
Richard Smith713369b2015-04-23 20:40:50 +00001847 for (auto &MMR : ModuleMacros) {
Richard Smithe56c8bc2015-04-22 00:26:11 +00001848 Overrides.clear();
Richard Smith713369b2015-04-23 20:40:50 +00001849 for (unsigned ModID : MMR.Overrides) {
Richard Smithb8b2ed62015-04-23 18:18:26 +00001850 Module *Mod = getSubmodule(ModID);
1851 auto *Macro = PP.getModuleMacro(Mod, II);
Richard Smithe56c8bc2015-04-22 00:26:11 +00001852 assert(Macro && "missing definition for overridden macro");
Richard Smith5dbef922015-04-22 02:09:43 +00001853 Overrides.push_back(Macro);
Richard Smithe56c8bc2015-04-22 00:26:11 +00001854 }
1855
1856 bool Inserted = false;
Richard Smith713369b2015-04-23 20:40:50 +00001857 Module *Owner = getSubmodule(MMR.SubModID);
Richard Smith20e883e2015-04-29 23:20:19 +00001858 PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted);
Richard Smithd7329392015-04-21 21:46:32 +00001859 }
1860 }
1861
1862 // Don't read the directive history for a module; we don't have anywhere
1863 // to put it.
1864 if (M.Kind == MK_ImplicitModule || M.Kind == MK_ExplicitModule)
1865 return;
1866
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001867 // Deserialize the macro directives history in reverse source-order.
Craig Toppera13603a2014-05-22 05:54:18 +00001868 MacroDirective *Latest = nullptr, *Earliest = nullptr;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001869 unsigned Idx = 0, N = Record.size();
1870 while (Idx < N) {
Craig Toppera13603a2014-05-22 05:54:18 +00001871 MacroDirective *MD = nullptr;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001872 SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001873 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
1874 switch (K) {
1875 case MacroDirective::MD_Define: {
Richard Smith713369b2015-04-23 20:40:50 +00001876 MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++]));
Richard Smith3981b172015-04-30 02:16:23 +00001877 MD = PP.AllocateDefMacroDirective(MI, Loc);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001878 break;
1879 }
Richard Smithdaa69e02014-07-25 04:40:03 +00001880 case MacroDirective::MD_Undefine: {
Richard Smith3981b172015-04-30 02:16:23 +00001881 MD = PP.AllocateUndefMacroDirective(Loc);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001882 break;
Richard Smithdaa69e02014-07-25 04:40:03 +00001883 }
1884 case MacroDirective::MD_Visibility:
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001885 bool isPublic = Record[Idx++];
1886 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
1887 break;
1888 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001889
1890 if (!Latest)
1891 Latest = MD;
1892 if (Earliest)
1893 Earliest->setPrevious(MD);
1894 Earliest = MD;
1895 }
1896
Richard Smithd6e8c0d2015-05-04 19:58:00 +00001897 if (Latest)
1898 PP.setLoadedMacroDirective(II, Latest);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001899}
1900
Argyrios Kyrtzidisce9b49e2014-03-14 02:26:27 +00001901ASTReader::InputFileInfo
1902ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) {
Ben Langmuir198c1682014-03-07 07:27:49 +00001903 // Go find this input file.
1904 BitstreamCursor &Cursor = F.InputFilesCursor;
1905 SavedStreamPosition SavedPosition(Cursor);
1906 Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
1907
1908 unsigned Code = Cursor.ReadCode();
1909 RecordData Record;
1910 StringRef Blob;
1911
1912 unsigned Result = Cursor.readRecord(Code, Record, &Blob);
1913 assert(static_cast<InputFileRecordTypes>(Result) == INPUT_FILE &&
1914 "invalid record type for input file");
1915 (void)Result;
1916
1917 assert(Record[0] == ID && "Bogus stored ID or offset");
Richard Smitha8cfffa2015-11-26 02:04:16 +00001918 InputFileInfo R;
1919 R.StoredSize = static_cast<off_t>(Record[1]);
1920 R.StoredTime = static_cast<time_t>(Record[2]);
1921 R.Overridden = static_cast<bool>(Record[3]);
1922 R.Transient = static_cast<bool>(Record[4]);
1923 R.Filename = Blob;
1924 ResolveImportedPath(F, R.Filename);
Hans Wennborg73945142014-03-14 17:45:06 +00001925 return R;
Ben Langmuir198c1682014-03-07 07:27:49 +00001926}
1927
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001928InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001929 // If this ID is bogus, just return an empty input file.
1930 if (ID == 0 || ID > F.InputFilesLoaded.size())
1931 return InputFile();
1932
1933 // If we've already loaded this input file, return it.
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001934 if (F.InputFilesLoaded[ID-1].getFile())
Guy Benyei11169dd2012-12-18 14:30:41 +00001935 return F.InputFilesLoaded[ID-1];
1936
Argyrios Kyrtzidis9308f0a2014-01-08 19:13:34 +00001937 if (F.InputFilesLoaded[ID-1].isNotFound())
1938 return InputFile();
1939
Guy Benyei11169dd2012-12-18 14:30:41 +00001940 // Go find this input file.
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001941 BitstreamCursor &Cursor = F.InputFilesCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001942 SavedStreamPosition SavedPosition(Cursor);
1943 Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
1944
Argyrios Kyrtzidisce9b49e2014-03-14 02:26:27 +00001945 InputFileInfo FI = readInputFileInfo(F, ID);
1946 off_t StoredSize = FI.StoredSize;
1947 time_t StoredTime = FI.StoredTime;
1948 bool Overridden = FI.Overridden;
Richard Smitha8cfffa2015-11-26 02:04:16 +00001949 bool Transient = FI.Transient;
Argyrios Kyrtzidisce9b49e2014-03-14 02:26:27 +00001950 StringRef Filename = FI.Filename;
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001951
Richard Smitha8cfffa2015-11-26 02:04:16 +00001952 const FileEntry *File = FileMgr.getFile(Filename, /*OpenFile=*/false);
Ben Langmuir198c1682014-03-07 07:27:49 +00001953
1954 // If we didn't find the file, resolve it relative to the
1955 // original directory from which this AST file was created.
Craig Toppera13603a2014-05-22 05:54:18 +00001956 if (File == nullptr && !F.OriginalDir.empty() && !CurrentDir.empty() &&
Ben Langmuir198c1682014-03-07 07:27:49 +00001957 F.OriginalDir != CurrentDir) {
1958 std::string Resolved = resolveFileRelativeToOriginalDir(Filename,
1959 F.OriginalDir,
1960 CurrentDir);
1961 if (!Resolved.empty())
1962 File = FileMgr.getFile(Resolved);
1963 }
1964
1965 // For an overridden file, create a virtual file with the stored
1966 // size/timestamp.
Richard Smitha8cfffa2015-11-26 02:04:16 +00001967 if ((Overridden || Transient) && File == nullptr)
Ben Langmuir198c1682014-03-07 07:27:49 +00001968 File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
Ben Langmuir198c1682014-03-07 07:27:49 +00001969
Craig Toppera13603a2014-05-22 05:54:18 +00001970 if (File == nullptr) {
Ben Langmuir198c1682014-03-07 07:27:49 +00001971 if (Complain) {
1972 std::string ErrorStr = "could not find file '";
1973 ErrorStr += Filename;
Richard Smith68142212015-10-13 01:26:26 +00001974 ErrorStr += "' referenced by AST file '";
1975 ErrorStr += F.FileName;
1976 ErrorStr += "'";
Ben Langmuir198c1682014-03-07 07:27:49 +00001977 Error(ErrorStr.c_str());
Guy Benyei11169dd2012-12-18 14:30:41 +00001978 }
Ben Langmuir198c1682014-03-07 07:27:49 +00001979 // Record that we didn't find the file.
1980 F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
1981 return InputFile();
1982 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001983
Ben Langmuir198c1682014-03-07 07:27:49 +00001984 // Check if there was a request to override the contents of the file
1985 // that was part of the precompiled header. Overridding such a file
1986 // can lead to problems when lexing using the source locations from the
1987 // PCH.
1988 SourceManager &SM = getSourceManager();
Richard Smith64daf7b2015-12-01 03:32:49 +00001989 // FIXME: Reject if the overrides are different.
1990 if ((!Overridden && !Transient) && SM.isFileOverridden(File)) {
Ben Langmuir198c1682014-03-07 07:27:49 +00001991 if (Complain)
1992 Error(diag::err_fe_pch_file_overridden, Filename);
1993 // After emitting the diagnostic, recover by disabling the override so
1994 // that the original file will be used.
Richard Smitha8cfffa2015-11-26 02:04:16 +00001995 //
1996 // FIXME: This recovery is just as broken as the original state; there may
1997 // be another precompiled module that's using the overridden contents, or
1998 // we might be half way through parsing it. Instead, we should treat the
1999 // overridden contents as belonging to a separate FileEntry.
Ben Langmuir198c1682014-03-07 07:27:49 +00002000 SM.disableFileContentsOverride(File);
2001 // The FileEntry is a virtual file entry with the size of the contents
2002 // that would override the original contents. Set it to the original's
2003 // size/time.
2004 FileMgr.modifyFileEntry(const_cast<FileEntry*>(File),
2005 StoredSize, StoredTime);
2006 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002007
Ben Langmuir198c1682014-03-07 07:27:49 +00002008 bool IsOutOfDate = false;
2009
2010 // For an overridden file, there is nothing to validate.
Richard Smith96fdab62014-10-28 16:24:08 +00002011 if (!Overridden && //
2012 (StoredSize != File->getSize() ||
2013#if defined(LLVM_ON_WIN32)
2014 false
2015#else
Ben Langmuir198c1682014-03-07 07:27:49 +00002016 // In our regression testing, the Windows file system seems to
2017 // have inconsistent modification times that sometimes
2018 // erroneously trigger this error-handling path.
Richard Smith96fdab62014-10-28 16:24:08 +00002019 //
Richard Smithe75ee0f2015-08-17 07:13:32 +00002020 // FIXME: This probably also breaks HeaderFileInfo lookups on Windows.
2021 (StoredTime && StoredTime != File->getModificationTime() &&
2022 !DisableValidation)
Guy Benyei11169dd2012-12-18 14:30:41 +00002023#endif
Ben Langmuir198c1682014-03-07 07:27:49 +00002024 )) {
2025 if (Complain) {
2026 // Build a list of the PCH imports that got us here (in reverse).
2027 SmallVector<ModuleFile *, 4> ImportStack(1, &F);
2028 while (ImportStack.back()->ImportedBy.size() > 0)
2029 ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
Ben Langmuire82630d2014-01-17 00:19:09 +00002030
Ben Langmuir198c1682014-03-07 07:27:49 +00002031 // The top-level PCH is stale.
2032 StringRef TopLevelPCHName(ImportStack.back()->FileName);
2033 Error(diag::err_fe_pch_file_modified, Filename, TopLevelPCHName);
Ben Langmuire82630d2014-01-17 00:19:09 +00002034
Ben Langmuir198c1682014-03-07 07:27:49 +00002035 // Print the import stack.
2036 if (ImportStack.size() > 1 && !Diags.isDiagnosticInFlight()) {
2037 Diag(diag::note_pch_required_by)
2038 << Filename << ImportStack[0]->FileName;
2039 for (unsigned I = 1; I < ImportStack.size(); ++I)
Ben Langmuire82630d2014-01-17 00:19:09 +00002040 Diag(diag::note_pch_required_by)
Ben Langmuir198c1682014-03-07 07:27:49 +00002041 << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
Douglas Gregor7029ce12013-03-19 00:28:20 +00002042 }
2043
Ben Langmuir198c1682014-03-07 07:27:49 +00002044 if (!Diags.isDiagnosticInFlight())
2045 Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
Guy Benyei11169dd2012-12-18 14:30:41 +00002046 }
2047
Ben Langmuir198c1682014-03-07 07:27:49 +00002048 IsOutOfDate = true;
Guy Benyei11169dd2012-12-18 14:30:41 +00002049 }
Richard Smitha8cfffa2015-11-26 02:04:16 +00002050 // FIXME: If the file is overridden and we've already opened it,
2051 // issue an error (or split it into a separate FileEntry).
Guy Benyei11169dd2012-12-18 14:30:41 +00002052
Richard Smitha8cfffa2015-11-26 02:04:16 +00002053 InputFile IF = InputFile(File, Overridden || Transient, IsOutOfDate);
Ben Langmuir198c1682014-03-07 07:27:49 +00002054
2055 // Note that we've loaded this input file.
2056 F.InputFilesLoaded[ID-1] = IF;
2057 return IF;
Guy Benyei11169dd2012-12-18 14:30:41 +00002058}
2059
Richard Smith7ed1bc92014-12-05 22:42:13 +00002060/// \brief If we are loading a relocatable PCH or module file, and the filename
2061/// is not an absolute path, add the system or module root to the beginning of
2062/// the file name.
2063void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) {
2064 // Resolve relative to the base directory, if we have one.
2065 if (!M.BaseDirectory.empty())
2066 return ResolveImportedPath(Filename, M.BaseDirectory);
Guy Benyei11169dd2012-12-18 14:30:41 +00002067}
2068
Richard Smith7ed1bc92014-12-05 22:42:13 +00002069void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002070 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
2071 return;
2072
Richard Smith7ed1bc92014-12-05 22:42:13 +00002073 SmallString<128> Buffer;
2074 llvm::sys::path::append(Buffer, Prefix, Filename);
2075 Filename.assign(Buffer.begin(), Buffer.end());
Guy Benyei11169dd2012-12-18 14:30:41 +00002076}
2077
Richard Smith0f99d6a2015-08-09 08:48:41 +00002078static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) {
2079 switch (ARR) {
2080 case ASTReader::Failure: return true;
2081 case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing);
2082 case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate);
2083 case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch);
2084 case ASTReader::ConfigurationMismatch:
2085 return !(Caps & ASTReader::ARR_ConfigurationMismatch);
2086 case ASTReader::HadErrors: return true;
2087 case ASTReader::Success: return false;
2088 }
2089
2090 llvm_unreachable("unknown ASTReadResult");
2091}
2092
Richard Smith0516b182015-09-08 19:40:14 +00002093ASTReader::ASTReadResult ASTReader::ReadOptionsBlock(
2094 BitstreamCursor &Stream, unsigned ClientLoadCapabilities,
2095 bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener,
2096 std::string &SuggestedPredefines) {
2097 if (Stream.EnterSubBlock(OPTIONS_BLOCK_ID))
2098 return Failure;
2099
2100 // Read all of the records in the options block.
2101 RecordData Record;
2102 ASTReadResult Result = Success;
2103 while (1) {
2104 llvm::BitstreamEntry Entry = Stream.advance();
2105
2106 switch (Entry.Kind) {
2107 case llvm::BitstreamEntry::Error:
2108 case llvm::BitstreamEntry::SubBlock:
2109 return Failure;
2110
2111 case llvm::BitstreamEntry::EndBlock:
2112 return Result;
2113
2114 case llvm::BitstreamEntry::Record:
2115 // The interesting case.
2116 break;
2117 }
2118
2119 // Read and process a record.
2120 Record.clear();
2121 switch ((OptionsRecordTypes)Stream.readRecord(Entry.ID, Record)) {
2122 case LANGUAGE_OPTIONS: {
2123 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2124 if (ParseLanguageOptions(Record, Complain, Listener,
2125 AllowCompatibleConfigurationMismatch))
2126 Result = ConfigurationMismatch;
2127 break;
2128 }
2129
2130 case TARGET_OPTIONS: {
2131 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2132 if (ParseTargetOptions(Record, Complain, Listener,
2133 AllowCompatibleConfigurationMismatch))
2134 Result = ConfigurationMismatch;
2135 break;
2136 }
2137
2138 case DIAGNOSTIC_OPTIONS: {
2139 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
2140 if (!AllowCompatibleConfigurationMismatch &&
2141 ParseDiagnosticOptions(Record, Complain, Listener))
2142 return OutOfDate;
2143 break;
2144 }
2145
2146 case FILE_SYSTEM_OPTIONS: {
2147 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2148 if (!AllowCompatibleConfigurationMismatch &&
2149 ParseFileSystemOptions(Record, Complain, Listener))
2150 Result = ConfigurationMismatch;
2151 break;
2152 }
2153
2154 case HEADER_SEARCH_OPTIONS: {
2155 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2156 if (!AllowCompatibleConfigurationMismatch &&
2157 ParseHeaderSearchOptions(Record, Complain, Listener))
2158 Result = ConfigurationMismatch;
2159 break;
2160 }
2161
2162 case PREPROCESSOR_OPTIONS:
2163 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2164 if (!AllowCompatibleConfigurationMismatch &&
2165 ParsePreprocessorOptions(Record, Complain, Listener,
2166 SuggestedPredefines))
2167 Result = ConfigurationMismatch;
2168 break;
2169 }
2170 }
2171}
2172
Guy Benyei11169dd2012-12-18 14:30:41 +00002173ASTReader::ASTReadResult
2174ASTReader::ReadControlBlock(ModuleFile &F,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002175 SmallVectorImpl<ImportedModule> &Loaded,
Ben Langmuirbeee15e2014-04-14 18:00:01 +00002176 const ModuleFile *ImportedBy,
Guy Benyei11169dd2012-12-18 14:30:41 +00002177 unsigned ClientLoadCapabilities) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00002178 BitstreamCursor &Stream = F.Stream;
Richard Smith8a308ec2015-11-05 00:54:55 +00002179 ASTReadResult Result = Success;
Guy Benyei11169dd2012-12-18 14:30:41 +00002180
2181 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
2182 Error("malformed block record in AST file");
2183 return Failure;
2184 }
2185
2186 // Read all of the records and blocks in the control block.
2187 RecordData Record;
Richard Smitha1825302014-10-23 22:18:29 +00002188 unsigned NumInputs = 0;
2189 unsigned NumUserInputs = 0;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002190 while (1) {
2191 llvm::BitstreamEntry Entry = Stream.advance();
2192
2193 switch (Entry.Kind) {
2194 case llvm::BitstreamEntry::Error:
2195 Error("malformed block record in AST file");
2196 return Failure;
Dmitri Gribenkof430da42014-02-12 10:33:14 +00002197 case llvm::BitstreamEntry::EndBlock: {
2198 // Validate input files.
2199 const HeaderSearchOptions &HSOpts =
2200 PP.getHeaderSearchInfo().getHeaderSearchOpts();
Ben Langmuircb69b572014-03-07 06:40:32 +00002201
Richard Smitha1825302014-10-23 22:18:29 +00002202 // All user input files reside at the index range [0, NumUserInputs), and
Richard Smith0f99d6a2015-08-09 08:48:41 +00002203 // system input files reside at [NumUserInputs, NumInputs). For explicitly
2204 // loaded module files, ignore missing inputs.
2205 if (!DisableValidation && F.Kind != MK_ExplicitModule) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002206 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
Ben Langmuircb69b572014-03-07 06:40:32 +00002207
Dmitri Gribenkof430da42014-02-12 10:33:14 +00002208 // If we are reading a module, we will create a verification timestamp,
2209 // so we verify all input files. Otherwise, verify only user input
2210 // files.
Ben Langmuircb69b572014-03-07 06:40:32 +00002211
2212 unsigned N = NumUserInputs;
2213 if (ValidateSystemInputs ||
Richard Smithe842a472014-10-22 02:05:46 +00002214 (HSOpts.ModulesValidateOncePerBuildSession &&
Ben Langmuiracb803e2014-11-10 22:13:10 +00002215 F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp &&
Richard Smithe842a472014-10-22 02:05:46 +00002216 F.Kind == MK_ImplicitModule))
Ben Langmuircb69b572014-03-07 06:40:32 +00002217 N = NumInputs;
2218
Ben Langmuir3d4417c2014-02-07 17:31:11 +00002219 for (unsigned I = 0; I < N; ++I) {
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00002220 InputFile IF = getInputFile(F, I+1, Complain);
2221 if (!IF.getFile() || IF.isOutOfDate())
Guy Benyei11169dd2012-12-18 14:30:41 +00002222 return OutOfDate;
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00002223 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002224 }
Ben Langmuircb69b572014-03-07 06:40:32 +00002225
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +00002226 if (Listener)
Richard Smith216a3bd2015-08-13 17:57:10 +00002227 Listener->visitModuleFile(F.FileName, F.Kind);
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +00002228
Ben Langmuircb69b572014-03-07 06:40:32 +00002229 if (Listener && Listener->needsInputFileVisitation()) {
2230 unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
2231 : NumUserInputs;
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +00002232 for (unsigned I = 0; I < N; ++I) {
2233 bool IsSystem = I >= NumUserInputs;
2234 InputFileInfo FI = readInputFileInfo(F, I+1);
Richard Smith216a3bd2015-08-13 17:57:10 +00002235 Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden,
2236 F.Kind == MK_ExplicitModule);
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +00002237 }
Ben Langmuircb69b572014-03-07 06:40:32 +00002238 }
2239
Richard Smith8a308ec2015-11-05 00:54:55 +00002240 return Result;
Dmitri Gribenkof430da42014-02-12 10:33:14 +00002241 }
2242
Chris Lattnere7b154b2013-01-19 21:39:22 +00002243 case llvm::BitstreamEntry::SubBlock:
2244 switch (Entry.ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002245 case INPUT_FILES_BLOCK_ID:
2246 F.InputFilesCursor = Stream;
2247 if (Stream.SkipBlock() || // Skip with the main cursor
2248 // Read the abbreviations
2249 ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
2250 Error("malformed block record in AST file");
2251 return Failure;
2252 }
2253 continue;
Richard Smith0516b182015-09-08 19:40:14 +00002254
2255 case OPTIONS_BLOCK_ID:
2256 // If we're reading the first module for this group, check its options
2257 // are compatible with ours. For modules it imports, no further checking
2258 // is required, because we checked them when we built it.
2259 if (Listener && !ImportedBy) {
2260 // Should we allow the configuration of the module file to differ from
2261 // the configuration of the current translation unit in a compatible
2262 // way?
2263 //
2264 // FIXME: Allow this for files explicitly specified with -include-pch.
2265 bool AllowCompatibleConfigurationMismatch =
2266 F.Kind == MK_ExplicitModule;
2267
Richard Smith8a308ec2015-11-05 00:54:55 +00002268 Result = ReadOptionsBlock(Stream, ClientLoadCapabilities,
2269 AllowCompatibleConfigurationMismatch,
2270 *Listener, SuggestedPredefines);
Richard Smith0516b182015-09-08 19:40:14 +00002271 if (Result == Failure) {
2272 Error("malformed block record in AST file");
2273 return Result;
2274 }
2275
Richard Smith8a308ec2015-11-05 00:54:55 +00002276 if (DisableValidation ||
2277 (AllowConfigurationMismatch && Result == ConfigurationMismatch))
2278 Result = Success;
2279
Ben Langmuir9b1e442e2016-02-11 18:54:02 +00002280 // If we can't load the module, exit early since we likely
2281 // will rebuild the module anyway. The stream may be in the
2282 // middle of a block.
2283 if (Result != Success)
Richard Smith0516b182015-09-08 19:40:14 +00002284 return Result;
2285 } else if (Stream.SkipBlock()) {
2286 Error("malformed block record in AST file");
2287 return Failure;
2288 }
2289 continue;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002290
Guy Benyei11169dd2012-12-18 14:30:41 +00002291 default:
Chris Lattnere7b154b2013-01-19 21:39:22 +00002292 if (Stream.SkipBlock()) {
2293 Error("malformed block record in AST file");
2294 return Failure;
2295 }
2296 continue;
Guy Benyei11169dd2012-12-18 14:30:41 +00002297 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00002298
2299 case llvm::BitstreamEntry::Record:
2300 // The interesting case.
2301 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002302 }
2303
2304 // Read and process a record.
2305 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00002306 StringRef Blob;
2307 switch ((ControlRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002308 case METADATA: {
2309 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2310 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
Dmitri Gribenko2228cd32014-02-11 15:40:09 +00002311 Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
2312 : diag::err_pch_version_too_new);
Guy Benyei11169dd2012-12-18 14:30:41 +00002313 return VersionMismatch;
2314 }
2315
Richard Smithe75ee0f2015-08-17 07:13:32 +00002316 bool hasErrors = Record[6];
Guy Benyei11169dd2012-12-18 14:30:41 +00002317 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
2318 Diag(diag::err_pch_with_compiler_errors);
2319 return HadErrors;
2320 }
2321
2322 F.RelocatablePCH = Record[4];
Richard Smith7ed1bc92014-12-05 22:42:13 +00002323 // Relative paths in a relocatable PCH are relative to our sysroot.
2324 if (F.RelocatablePCH)
2325 F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
Guy Benyei11169dd2012-12-18 14:30:41 +00002326
Richard Smithe75ee0f2015-08-17 07:13:32 +00002327 F.HasTimestamps = Record[5];
2328
Guy Benyei11169dd2012-12-18 14:30:41 +00002329 const std::string &CurBranch = getClangFullRepositoryVersion();
Chris Lattner0e6c9402013-01-20 02:38:54 +00002330 StringRef ASTBranch = Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00002331 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2332 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
Dmitri Gribenko2228cd32014-02-11 15:40:09 +00002333 Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
Guy Benyei11169dd2012-12-18 14:30:41 +00002334 return VersionMismatch;
2335 }
2336 break;
2337 }
2338
Ben Langmuir487ea142014-10-23 18:05:36 +00002339 case SIGNATURE:
2340 assert((!F.Signature || F.Signature == Record[0]) && "signature changed");
2341 F.Signature = Record[0];
2342 break;
2343
Guy Benyei11169dd2012-12-18 14:30:41 +00002344 case IMPORTS: {
2345 // Load each of the imported PCH files.
2346 unsigned Idx = 0, N = Record.size();
2347 while (Idx < N) {
2348 // Read information about the AST file.
2349 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
2350 // The import location will be the local one for now; we will adjust
2351 // all import locations of module imports after the global source
Richard Smithb22a1d12016-03-27 20:13:24 +00002352 // location info are setup, in ReadAST.
Guy Benyei11169dd2012-12-18 14:30:41 +00002353 SourceLocation ImportLoc =
Richard Smithb22a1d12016-03-27 20:13:24 +00002354 ReadUntranslatedSourceLocation(Record[Idx++]);
Douglas Gregor7029ce12013-03-19 00:28:20 +00002355 off_t StoredSize = (off_t)Record[Idx++];
2356 time_t StoredModTime = (time_t)Record[Idx++];
Ben Langmuir487ea142014-10-23 18:05:36 +00002357 ASTFileSignature StoredSignature = Record[Idx++];
Richard Smith7ed1bc92014-12-05 22:42:13 +00002358 auto ImportedFile = ReadPath(F, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00002359
Richard Smith0f99d6a2015-08-09 08:48:41 +00002360 // If our client can't cope with us being out of date, we can't cope with
2361 // our dependency being missing.
2362 unsigned Capabilities = ClientLoadCapabilities;
2363 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2364 Capabilities &= ~ARR_Missing;
2365
Guy Benyei11169dd2012-12-18 14:30:41 +00002366 // Load the AST file.
Richard Smith0f99d6a2015-08-09 08:48:41 +00002367 auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F,
2368 Loaded, StoredSize, StoredModTime,
2369 StoredSignature, Capabilities);
2370
2371 // If we diagnosed a problem, produce a backtrace.
2372 if (isDiagnosedResult(Result, Capabilities))
2373 Diag(diag::note_module_file_imported_by)
2374 << F.FileName << !F.ModuleName.empty() << F.ModuleName;
2375
2376 switch (Result) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002377 case Failure: return Failure;
2378 // If we have to ignore the dependency, we'll have to ignore this too.
Douglas Gregor2f1806e2013-03-19 00:38:50 +00002379 case Missing:
Guy Benyei11169dd2012-12-18 14:30:41 +00002380 case OutOfDate: return OutOfDate;
2381 case VersionMismatch: return VersionMismatch;
2382 case ConfigurationMismatch: return ConfigurationMismatch;
2383 case HadErrors: return HadErrors;
2384 case Success: break;
2385 }
2386 }
2387 break;
2388 }
2389
Guy Benyei11169dd2012-12-18 14:30:41 +00002390 case ORIGINAL_FILE:
2391 F.OriginalSourceFileID = FileID::get(Record[0]);
Chris Lattner0e6c9402013-01-20 02:38:54 +00002392 F.ActualOriginalSourceFileName = Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00002393 F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
Richard Smith7ed1bc92014-12-05 22:42:13 +00002394 ResolveImportedPath(F, F.OriginalSourceFileName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002395 break;
2396
2397 case ORIGINAL_FILE_ID:
2398 F.OriginalSourceFileID = FileID::get(Record[0]);
2399 break;
2400
2401 case ORIGINAL_PCH_DIR:
Chris Lattner0e6c9402013-01-20 02:38:54 +00002402 F.OriginalDir = Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00002403 break;
2404
Ben Langmuirbeee15e2014-04-14 18:00:01 +00002405 case MODULE_NAME:
2406 F.ModuleName = Blob;
Ben Langmuir4f5212a2014-04-14 22:12:44 +00002407 if (Listener)
2408 Listener->ReadModuleName(F.ModuleName);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00002409 break;
2410
Richard Smith223d3f22014-12-06 03:21:08 +00002411 case MODULE_DIRECTORY: {
2412 assert(!F.ModuleName.empty() &&
2413 "MODULE_DIRECTORY found before MODULE_NAME");
2414 // If we've already loaded a module map file covering this module, we may
2415 // have a better path for it (relative to the current build).
2416 Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
2417 if (M && M->Directory) {
2418 // If we're implicitly loading a module, the base directory can't
2419 // change between the build and use.
2420 if (F.Kind != MK_ExplicitModule) {
2421 const DirectoryEntry *BuildDir =
2422 PP.getFileManager().getDirectory(Blob);
2423 if (!BuildDir || BuildDir != M->Directory) {
2424 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2425 Diag(diag::err_imported_module_relocated)
2426 << F.ModuleName << Blob << M->Directory->getName();
2427 return OutOfDate;
2428 }
2429 }
2430 F.BaseDirectory = M->Directory->getName();
2431 } else {
2432 F.BaseDirectory = Blob;
2433 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00002434 break;
Richard Smith223d3f22014-12-06 03:21:08 +00002435 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00002436
Ben Langmuirbeee15e2014-04-14 18:00:01 +00002437 case MODULE_MAP_FILE:
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00002438 if (ASTReadResult Result =
2439 ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
2440 return Result;
Ben Langmuir264ea152014-11-08 00:06:39 +00002441 break;
2442
Justin Bognerca9c0cc2015-06-21 20:32:36 +00002443 case INPUT_FILE_OFFSETS:
Richard Smitha1825302014-10-23 22:18:29 +00002444 NumInputs = Record[0];
2445 NumUserInputs = Record[1];
Justin Bogner4c183242015-06-21 20:32:40 +00002446 F.InputFileOffsets =
2447 (const llvm::support::unaligned_uint64_t *)Blob.data();
Richard Smitha1825302014-10-23 22:18:29 +00002448 F.InputFilesLoaded.resize(NumInputs);
Guy Benyei11169dd2012-12-18 14:30:41 +00002449 break;
2450 }
2451 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002452}
2453
Ben Langmuir2c9af442014-04-10 17:57:43 +00002454ASTReader::ASTReadResult
2455ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00002456 BitstreamCursor &Stream = F.Stream;
Guy Benyei11169dd2012-12-18 14:30:41 +00002457
2458 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
2459 Error("malformed block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002460 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002461 }
2462
2463 // Read all of the records and blocks for the AST file.
2464 RecordData Record;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002465 while (1) {
2466 llvm::BitstreamEntry Entry = Stream.advance();
2467
2468 switch (Entry.Kind) {
2469 case llvm::BitstreamEntry::Error:
2470 Error("error at end of module block in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002471 return Failure;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002472 case llvm::BitstreamEntry::EndBlock: {
Richard Smithc0fbba72013-04-03 22:49:41 +00002473 // Outside of C++, we do not store a lookup map for the translation unit.
2474 // Instead, mark it as needing a lookup map to be built if this module
2475 // contains any declarations lexically within it (which it always does!).
2476 // This usually has no cost, since we very rarely need the lookup map for
2477 // the translation unit outside C++.
Guy Benyei11169dd2012-12-18 14:30:41 +00002478 DeclContext *DC = Context.getTranslationUnitDecl();
Richard Smithc0fbba72013-04-03 22:49:41 +00002479 if (DC->hasExternalLexicalStorage() &&
2480 !getContext().getLangOpts().CPlusPlus)
Guy Benyei11169dd2012-12-18 14:30:41 +00002481 DC->setMustBuildLookupTable();
Chris Lattnere7b154b2013-01-19 21:39:22 +00002482
Ben Langmuir2c9af442014-04-10 17:57:43 +00002483 return Success;
Guy Benyei11169dd2012-12-18 14:30:41 +00002484 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00002485 case llvm::BitstreamEntry::SubBlock:
2486 switch (Entry.ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002487 case DECLTYPES_BLOCK_ID:
2488 // We lazily load the decls block, but we want to set up the
2489 // DeclsCursor cursor to point into it. Clone our current bitcode
2490 // cursor to it, enter the block and read the abbrevs in that block.
2491 // With the main cursor, we just skip over it.
2492 F.DeclsCursor = Stream;
2493 if (Stream.SkipBlock() || // Skip with the main cursor.
2494 // Read the abbrevs.
2495 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
2496 Error("malformed block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002497 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002498 }
2499 break;
Richard Smithb9eab6d2014-03-20 19:44:17 +00002500
Guy Benyei11169dd2012-12-18 14:30:41 +00002501 case PREPROCESSOR_BLOCK_ID:
2502 F.MacroCursor = Stream;
2503 if (!PP.getExternalSource())
2504 PP.setExternalSource(this);
Chris Lattnere7b154b2013-01-19 21:39:22 +00002505
Guy Benyei11169dd2012-12-18 14:30:41 +00002506 if (Stream.SkipBlock() ||
2507 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
2508 Error("malformed block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002509 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002510 }
2511 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
2512 break;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002513
Guy Benyei11169dd2012-12-18 14:30:41 +00002514 case PREPROCESSOR_DETAIL_BLOCK_ID:
2515 F.PreprocessorDetailCursor = Stream;
2516 if (Stream.SkipBlock() ||
Chris Lattnere7b154b2013-01-19 21:39:22 +00002517 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
Guy Benyei11169dd2012-12-18 14:30:41 +00002518 PREPROCESSOR_DETAIL_BLOCK_ID)) {
Chris Lattnere7b154b2013-01-19 21:39:22 +00002519 Error("malformed preprocessor detail record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002520 return Failure;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002521 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002522 F.PreprocessorDetailStartOffset
Chris Lattnere7b154b2013-01-19 21:39:22 +00002523 = F.PreprocessorDetailCursor.GetCurrentBitNo();
2524
Guy Benyei11169dd2012-12-18 14:30:41 +00002525 if (!PP.getPreprocessingRecord())
2526 PP.createPreprocessingRecord();
2527 if (!PP.getPreprocessingRecord()->getExternalSource())
2528 PP.getPreprocessingRecord()->SetExternalSource(*this);
2529 break;
2530
2531 case SOURCE_MANAGER_BLOCK_ID:
2532 if (ReadSourceManagerBlock(F))
Ben Langmuir2c9af442014-04-10 17:57:43 +00002533 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002534 break;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002535
Guy Benyei11169dd2012-12-18 14:30:41 +00002536 case SUBMODULE_BLOCK_ID:
Ben Langmuir2c9af442014-04-10 17:57:43 +00002537 if (ASTReadResult Result = ReadSubmoduleBlock(F, ClientLoadCapabilities))
2538 return Result;
Guy Benyei11169dd2012-12-18 14:30:41 +00002539 break;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002540
Guy Benyei11169dd2012-12-18 14:30:41 +00002541 case COMMENTS_BLOCK_ID: {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00002542 BitstreamCursor C = Stream;
Guy Benyei11169dd2012-12-18 14:30:41 +00002543 if (Stream.SkipBlock() ||
2544 ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
2545 Error("malformed comments block in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002546 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002547 }
2548 CommentsCursors.push_back(std::make_pair(C, &F));
2549 break;
2550 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00002551
Guy Benyei11169dd2012-12-18 14:30:41 +00002552 default:
Chris Lattnere7b154b2013-01-19 21:39:22 +00002553 if (Stream.SkipBlock()) {
2554 Error("malformed block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002555 return Failure;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002556 }
2557 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002558 }
2559 continue;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002560
2561 case llvm::BitstreamEntry::Record:
2562 // The interesting case.
2563 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002564 }
2565
2566 // Read and process a record.
2567 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00002568 StringRef Blob;
2569 switch ((ASTRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002570 default: // Default behavior: ignore.
2571 break;
2572
2573 case TYPE_OFFSET: {
2574 if (F.LocalNumTypes != 0) {
2575 Error("duplicate TYPE_OFFSET record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002576 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002577 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00002578 F.TypeOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002579 F.LocalNumTypes = Record[0];
2580 unsigned LocalBaseTypeIndex = Record[1];
2581 F.BaseTypeIndex = getTotalNumTypes();
2582
2583 if (F.LocalNumTypes > 0) {
2584 // Introduce the global -> local mapping for types within this module.
2585 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
2586
2587 // Introduce the local -> global mapping for types within this module.
2588 F.TypeRemap.insertOrReplace(
2589 std::make_pair(LocalBaseTypeIndex,
2590 F.BaseTypeIndex - LocalBaseTypeIndex));
Ben Langmuir52ca6782014-10-20 16:27:32 +00002591
2592 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
Guy Benyei11169dd2012-12-18 14:30:41 +00002593 }
2594 break;
2595 }
2596
2597 case DECL_OFFSET: {
2598 if (F.LocalNumDecls != 0) {
2599 Error("duplicate DECL_OFFSET record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002600 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002601 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00002602 F.DeclOffsets = (const DeclOffset *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002603 F.LocalNumDecls = Record[0];
2604 unsigned LocalBaseDeclID = Record[1];
2605 F.BaseDeclID = getTotalNumDecls();
2606
2607 if (F.LocalNumDecls > 0) {
2608 // Introduce the global -> local mapping for declarations within this
2609 // module.
2610 GlobalDeclMap.insert(
2611 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
2612
2613 // Introduce the local -> global mapping for declarations within this
2614 // module.
2615 F.DeclRemap.insertOrReplace(
2616 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
2617
2618 // Introduce the global -> local mapping for declarations within this
2619 // module.
2620 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
Ben Langmuirfe971d92014-08-16 04:54:18 +00002621
Ben Langmuir52ca6782014-10-20 16:27:32 +00002622 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
2623 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002624 break;
2625 }
2626
2627 case TU_UPDATE_LEXICAL: {
2628 DeclContext *TU = Context.getTranslationUnitDecl();
Richard Smith82f8fcd2015-08-06 22:07:25 +00002629 LexicalContents Contents(
2630 reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
2631 Blob.data()),
2632 static_cast<unsigned int>(Blob.size() / 4));
2633 TULexicalDecls.push_back(std::make_pair(&F, Contents));
Guy Benyei11169dd2012-12-18 14:30:41 +00002634 TU->setHasExternalLexicalStorage(true);
2635 break;
2636 }
2637
2638 case UPDATE_VISIBLE: {
2639 unsigned Idx = 0;
2640 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
Richard Smith0f4e2c42015-08-06 04:23:48 +00002641 auto *Data = (const unsigned char*)Blob.data();
Richard Smithd88a7f12015-09-01 20:35:42 +00002642 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data});
Richard Smith0f4e2c42015-08-06 04:23:48 +00002643 // If we've already loaded the decl, perform the updates when we finish
2644 // loading this block.
2645 if (Decl *D = GetExistingDecl(ID))
2646 PendingUpdateRecords.push_back(std::make_pair(ID, D));
Guy Benyei11169dd2012-12-18 14:30:41 +00002647 break;
2648 }
2649
2650 case IDENTIFIER_TABLE:
Chris Lattner0e6c9402013-01-20 02:38:54 +00002651 F.IdentifierTableData = Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002652 if (Record[0]) {
Justin Bognerda4e6502014-04-14 16:34:29 +00002653 F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
2654 (const unsigned char *)F.IdentifierTableData + Record[0],
2655 (const unsigned char *)F.IdentifierTableData + sizeof(uint32_t),
2656 (const unsigned char *)F.IdentifierTableData,
2657 ASTIdentifierLookupTrait(*this, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002658
2659 PP.getIdentifierTable().setExternalIdentifierLookup(this);
2660 }
2661 break;
2662
2663 case IDENTIFIER_OFFSET: {
2664 if (F.LocalNumIdentifiers != 0) {
2665 Error("duplicate IDENTIFIER_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.IdentifierOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002669 F.LocalNumIdentifiers = Record[0];
2670 unsigned LocalBaseIdentifierID = Record[1];
2671 F.BaseIdentifierID = getTotalNumIdentifiers();
2672
2673 if (F.LocalNumIdentifiers > 0) {
2674 // Introduce the global -> local mapping for identifiers within this
2675 // module.
2676 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
2677 &F));
2678
2679 // Introduce the local -> global mapping for identifiers within this
2680 // module.
2681 F.IdentifierRemap.insertOrReplace(
2682 std::make_pair(LocalBaseIdentifierID,
2683 F.BaseIdentifierID - LocalBaseIdentifierID));
Ben Langmuirfe971d92014-08-16 04:54:18 +00002684
Ben Langmuir52ca6782014-10-20 16:27:32 +00002685 IdentifiersLoaded.resize(IdentifiersLoaded.size()
2686 + F.LocalNumIdentifiers);
2687 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002688 break;
2689 }
2690
Richard Smith33e0f7e2015-07-22 02:08:40 +00002691 case INTERESTING_IDENTIFIERS:
2692 F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end());
2693 break;
2694
Ben Langmuir332aafe2014-01-31 01:06:56 +00002695 case EAGERLY_DESERIALIZED_DECLS:
Richard Smith9e2341d2015-03-23 03:25:59 +00002696 // FIXME: Skip reading this record if our ASTConsumer doesn't care
2697 // about "interesting" decls (for instance, if we're building a module).
Guy Benyei11169dd2012-12-18 14:30:41 +00002698 for (unsigned I = 0, N = Record.size(); I != N; ++I)
Ben Langmuir332aafe2014-01-31 01:06:56 +00002699 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
Guy Benyei11169dd2012-12-18 14:30:41 +00002700 break;
2701
2702 case SPECIAL_TYPES:
Douglas Gregor44180f82013-02-01 23:45:03 +00002703 if (SpecialTypes.empty()) {
2704 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2705 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
2706 break;
2707 }
2708
2709 if (SpecialTypes.size() != Record.size()) {
2710 Error("invalid special-types record");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002711 return Failure;
Douglas Gregor44180f82013-02-01 23:45:03 +00002712 }
2713
2714 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2715 serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
2716 if (!SpecialTypes[I])
2717 SpecialTypes[I] = ID;
2718 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
2719 // merge step?
2720 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002721 break;
2722
2723 case STATISTICS:
2724 TotalNumStatements += Record[0];
2725 TotalNumMacros += Record[1];
2726 TotalLexicalDeclContexts += Record[2];
2727 TotalVisibleDeclContexts += Record[3];
2728 break;
2729
2730 case UNUSED_FILESCOPED_DECLS:
2731 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2732 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
2733 break;
2734
2735 case DELEGATING_CTORS:
2736 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2737 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
2738 break;
2739
2740 case WEAK_UNDECLARED_IDENTIFIERS:
2741 if (Record.size() % 4 != 0) {
2742 Error("invalid weak identifiers record");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002743 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002744 }
2745
2746 // FIXME: Ignore weak undeclared identifiers from non-original PCH
2747 // files. This isn't the way to do it :)
2748 WeakUndeclaredIdentifiers.clear();
2749
2750 // Translate the weak, undeclared identifiers into global IDs.
2751 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
2752 WeakUndeclaredIdentifiers.push_back(
2753 getGlobalIdentifierID(F, Record[I++]));
2754 WeakUndeclaredIdentifiers.push_back(
2755 getGlobalIdentifierID(F, Record[I++]));
2756 WeakUndeclaredIdentifiers.push_back(
2757 ReadSourceLocation(F, Record, I).getRawEncoding());
2758 WeakUndeclaredIdentifiers.push_back(Record[I++]);
2759 }
2760 break;
2761
Guy Benyei11169dd2012-12-18 14:30:41 +00002762 case SELECTOR_OFFSETS: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00002763 F.SelectorOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002764 F.LocalNumSelectors = Record[0];
2765 unsigned LocalBaseSelectorID = Record[1];
2766 F.BaseSelectorID = getTotalNumSelectors();
2767
2768 if (F.LocalNumSelectors > 0) {
2769 // Introduce the global -> local mapping for selectors within this
2770 // module.
2771 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
2772
2773 // Introduce the local -> global mapping for selectors within this
2774 // module.
2775 F.SelectorRemap.insertOrReplace(
2776 std::make_pair(LocalBaseSelectorID,
2777 F.BaseSelectorID - LocalBaseSelectorID));
Ben Langmuir52ca6782014-10-20 16:27:32 +00002778
2779 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
Guy Benyei11169dd2012-12-18 14:30:41 +00002780 }
2781 break;
2782 }
2783
2784 case METHOD_POOL:
Chris Lattner0e6c9402013-01-20 02:38:54 +00002785 F.SelectorLookupTableData = (const unsigned char *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002786 if (Record[0])
2787 F.SelectorLookupTable
2788 = ASTSelectorLookupTable::Create(
2789 F.SelectorLookupTableData + Record[0],
2790 F.SelectorLookupTableData,
2791 ASTSelectorLookupTrait(*this, F));
2792 TotalNumMethodPoolEntries += Record[1];
2793 break;
2794
2795 case REFERENCED_SELECTOR_POOL:
2796 if (!Record.empty()) {
2797 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
2798 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
2799 Record[Idx++]));
2800 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
2801 getRawEncoding());
2802 }
2803 }
2804 break;
2805
2806 case PP_COUNTER_VALUE:
2807 if (!Record.empty() && Listener)
2808 Listener->ReadCounter(F, Record[0]);
2809 break;
2810
2811 case FILE_SORTED_DECLS:
Chris Lattner0e6c9402013-01-20 02:38:54 +00002812 F.FileSortedDecls = (const DeclID *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002813 F.NumFileSortedDecls = Record[0];
2814 break;
2815
2816 case SOURCE_LOCATION_OFFSETS: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00002817 F.SLocEntryOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002818 F.LocalNumSLocEntries = Record[0];
2819 unsigned SLocSpaceSize = Record[1];
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00002820 std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
Ben Langmuir52ca6782014-10-20 16:27:32 +00002821 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
Guy Benyei11169dd2012-12-18 14:30:41 +00002822 SLocSpaceSize);
Richard Smith78d81ec2015-08-12 22:25:24 +00002823 if (!F.SLocEntryBaseID) {
2824 Error("ran out of source locations");
2825 break;
2826 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002827 // Make our entry in the range map. BaseID is negative and growing, so
2828 // we invert it. Because we invert it, though, we need the other end of
2829 // the range.
2830 unsigned RangeStart =
2831 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
2832 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
2833 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
2834
2835 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
2836 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
2837 GlobalSLocOffsetMap.insert(
2838 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
2839 - SLocSpaceSize,&F));
2840
2841 // Initialize the remapping table.
2842 // Invalid stays invalid.
Richard Smithb9eab6d2014-03-20 19:44:17 +00002843 F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
Guy Benyei11169dd2012-12-18 14:30:41 +00002844 // This module. Base was 2 when being compiled.
Richard Smithb9eab6d2014-03-20 19:44:17 +00002845 F.SLocRemap.insertOrReplace(std::make_pair(2U,
Guy Benyei11169dd2012-12-18 14:30:41 +00002846 static_cast<int>(F.SLocEntryBaseOffset - 2)));
2847
2848 TotalNumSLocEntries += F.LocalNumSLocEntries;
2849 break;
2850 }
2851
2852 case MODULE_OFFSET_MAP: {
2853 // Additional remapping information.
Chris Lattner0e6c9402013-01-20 02:38:54 +00002854 const unsigned char *Data = (const unsigned char*)Blob.data();
2855 const unsigned char *DataEnd = Data + Blob.size();
Richard Smithb9eab6d2014-03-20 19:44:17 +00002856
2857 // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
2858 if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
2859 F.SLocRemap.insert(std::make_pair(0U, 0));
2860 F.SLocRemap.insert(std::make_pair(2U, 1));
2861 }
2862
Guy Benyei11169dd2012-12-18 14:30:41 +00002863 // Continuous range maps we may be updating in our module.
Ben Langmuir785180e2014-10-20 16:27:30 +00002864 typedef ContinuousRangeMap<uint32_t, int, 2>::Builder
2865 RemapBuilder;
2866 RemapBuilder SLocRemap(F.SLocRemap);
2867 RemapBuilder IdentifierRemap(F.IdentifierRemap);
2868 RemapBuilder MacroRemap(F.MacroRemap);
2869 RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
2870 RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
2871 RemapBuilder SelectorRemap(F.SelectorRemap);
2872 RemapBuilder DeclRemap(F.DeclRemap);
2873 RemapBuilder TypeRemap(F.TypeRemap);
Guy Benyei11169dd2012-12-18 14:30:41 +00002874
Richard Smithd8879c82015-08-24 21:59:32 +00002875 while (Data < DataEnd) {
Justin Bogner57ba0b22014-03-28 22:03:24 +00002876 using namespace llvm::support;
2877 uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data);
Guy Benyei11169dd2012-12-18 14:30:41 +00002878 StringRef Name = StringRef((const char*)Data, Len);
2879 Data += Len;
2880 ModuleFile *OM = ModuleMgr.lookup(Name);
2881 if (!OM) {
2882 Error("SourceLocation remap refers to unknown module");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002883 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002884 }
2885
Justin Bogner57ba0b22014-03-28 22:03:24 +00002886 uint32_t SLocOffset =
2887 endian::readNext<uint32_t, little, unaligned>(Data);
2888 uint32_t IdentifierIDOffset =
2889 endian::readNext<uint32_t, little, unaligned>(Data);
2890 uint32_t MacroIDOffset =
2891 endian::readNext<uint32_t, little, unaligned>(Data);
2892 uint32_t PreprocessedEntityIDOffset =
2893 endian::readNext<uint32_t, little, unaligned>(Data);
2894 uint32_t SubmoduleIDOffset =
2895 endian::readNext<uint32_t, little, unaligned>(Data);
2896 uint32_t SelectorIDOffset =
2897 endian::readNext<uint32_t, little, unaligned>(Data);
2898 uint32_t DeclIDOffset =
2899 endian::readNext<uint32_t, little, unaligned>(Data);
2900 uint32_t TypeIndexOffset =
2901 endian::readNext<uint32_t, little, unaligned>(Data);
2902
Ben Langmuir785180e2014-10-20 16:27:30 +00002903 uint32_t None = std::numeric_limits<uint32_t>::max();
2904
2905 auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
2906 RemapBuilder &Remap) {
2907 if (Offset != None)
2908 Remap.insert(std::make_pair(Offset,
2909 static_cast<int>(BaseOffset - Offset)));
2910 };
2911 mapOffset(SLocOffset, OM->SLocEntryBaseOffset, SLocRemap);
2912 mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap);
2913 mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
2914 mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
2915 PreprocessedEntityRemap);
2916 mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
2917 mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
2918 mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap);
2919 mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap);
Guy Benyei11169dd2012-12-18 14:30:41 +00002920
2921 // Global -> local mappings.
2922 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
2923 }
2924 break;
2925 }
2926
2927 case SOURCE_MANAGER_LINE_TABLE:
2928 if (ParseLineTable(F, Record))
Ben Langmuir2c9af442014-04-10 17:57:43 +00002929 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002930 break;
2931
2932 case SOURCE_LOCATION_PRELOADS: {
2933 // Need to transform from the local view (1-based IDs) to the global view,
2934 // which is based off F.SLocEntryBaseID.
2935 if (!F.PreloadSLocEntries.empty()) {
2936 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002937 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002938 }
2939
2940 F.PreloadSLocEntries.swap(Record);
2941 break;
2942 }
2943
2944 case EXT_VECTOR_DECLS:
2945 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2946 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
2947 break;
2948
2949 case VTABLE_USES:
2950 if (Record.size() % 3 != 0) {
2951 Error("Invalid VTABLE_USES record");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002952 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002953 }
2954
2955 // Later tables overwrite earlier ones.
2956 // FIXME: Modules will have some trouble with this. This is clearly not
2957 // the right way to do this.
2958 VTableUses.clear();
2959
2960 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
2961 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
2962 VTableUses.push_back(
2963 ReadSourceLocation(F, Record, Idx).getRawEncoding());
2964 VTableUses.push_back(Record[Idx++]);
2965 }
2966 break;
2967
Guy Benyei11169dd2012-12-18 14:30:41 +00002968 case PENDING_IMPLICIT_INSTANTIATIONS:
2969 if (PendingInstantiations.size() % 2 != 0) {
2970 Error("Invalid existing PendingInstantiations");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002971 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002972 }
2973
2974 if (Record.size() % 2 != 0) {
2975 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002976 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002977 }
2978
2979 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2980 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
2981 PendingInstantiations.push_back(
2982 ReadSourceLocation(F, Record, I).getRawEncoding());
2983 }
2984 break;
2985
2986 case SEMA_DECL_REFS:
Richard Smith3d8e97e2013-10-18 06:54:39 +00002987 if (Record.size() != 2) {
2988 Error("Invalid SEMA_DECL_REFS block");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002989 return Failure;
Richard Smith3d8e97e2013-10-18 06:54:39 +00002990 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002991 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2992 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
2993 break;
2994
2995 case PPD_ENTITIES_OFFSETS: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00002996 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
2997 assert(Blob.size() % sizeof(PPEntityOffset) == 0);
2998 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
Guy Benyei11169dd2012-12-18 14:30:41 +00002999
3000 unsigned LocalBasePreprocessedEntityID = Record[0];
3001
3002 unsigned StartingID;
3003 if (!PP.getPreprocessingRecord())
3004 PP.createPreprocessingRecord();
3005 if (!PP.getPreprocessingRecord()->getExternalSource())
3006 PP.getPreprocessingRecord()->SetExternalSource(*this);
3007 StartingID
3008 = PP.getPreprocessingRecord()
Ben Langmuir52ca6782014-10-20 16:27:32 +00003009 ->allocateLoadedEntities(F.NumPreprocessedEntities);
Guy Benyei11169dd2012-12-18 14:30:41 +00003010 F.BasePreprocessedEntityID = StartingID;
3011
3012 if (F.NumPreprocessedEntities > 0) {
3013 // Introduce the global -> local mapping for preprocessed entities in
3014 // this module.
3015 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
3016
3017 // Introduce the local -> global mapping for preprocessed entities in
3018 // this module.
3019 F.PreprocessedEntityRemap.insertOrReplace(
3020 std::make_pair(LocalBasePreprocessedEntityID,
3021 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
3022 }
3023
3024 break;
3025 }
3026
3027 case DECL_UPDATE_OFFSETS: {
3028 if (Record.size() % 2 != 0) {
3029 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003030 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003031 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00003032 for (unsigned I = 0, N = Record.size(); I != N; I += 2) {
3033 GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
3034 DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1]));
3035
3036 // If we've already loaded the decl, perform the updates when we finish
3037 // loading this block.
3038 if (Decl *D = GetExistingDecl(ID))
3039 PendingUpdateRecords.push_back(std::make_pair(ID, D));
3040 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003041 break;
3042 }
3043
Guy Benyei11169dd2012-12-18 14:30:41 +00003044 case OBJC_CATEGORIES_MAP: {
3045 if (F.LocalNumObjCCategoriesInMap != 0) {
3046 Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003047 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003048 }
3049
3050 F.LocalNumObjCCategoriesInMap = Record[0];
Chris Lattner0e6c9402013-01-20 02:38:54 +00003051 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003052 break;
3053 }
3054
3055 case OBJC_CATEGORIES:
3056 F.ObjCCategories.swap(Record);
3057 break;
Richard Smithc2bb8182015-03-24 06:36:48 +00003058
Guy Benyei11169dd2012-12-18 14:30:41 +00003059 case DIAG_PRAGMA_MAPPINGS:
3060 if (F.PragmaDiagMappings.empty())
3061 F.PragmaDiagMappings.swap(Record);
3062 else
3063 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
3064 Record.begin(), Record.end());
3065 break;
3066
3067 case CUDA_SPECIAL_DECL_REFS:
3068 // Later tables overwrite earlier ones.
3069 // FIXME: Modules will have trouble with this.
3070 CUDASpecialDeclRefs.clear();
3071 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3072 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3073 break;
3074
3075 case HEADER_SEARCH_TABLE: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00003076 F.HeaderFileInfoTableData = Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003077 F.LocalNumHeaderFileInfos = Record[1];
Guy Benyei11169dd2012-12-18 14:30:41 +00003078 if (Record[0]) {
3079 F.HeaderFileInfoTable
3080 = HeaderFileInfoLookupTable::Create(
3081 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
3082 (const unsigned char *)F.HeaderFileInfoTableData,
3083 HeaderFileInfoTrait(*this, F,
3084 &PP.getHeaderSearchInfo(),
Chris Lattner0e6c9402013-01-20 02:38:54 +00003085 Blob.data() + Record[2]));
Guy Benyei11169dd2012-12-18 14:30:41 +00003086
3087 PP.getHeaderSearchInfo().SetExternalSource(this);
3088 if (!PP.getHeaderSearchInfo().getExternalLookup())
3089 PP.getHeaderSearchInfo().SetExternalLookup(this);
3090 }
3091 break;
3092 }
3093
3094 case FP_PRAGMA_OPTIONS:
3095 // Later tables overwrite earlier ones.
3096 FPPragmaOptions.swap(Record);
3097 break;
3098
3099 case OPENCL_EXTENSIONS:
3100 // Later tables overwrite earlier ones.
3101 OpenCLExtensions.swap(Record);
3102 break;
3103
3104 case TENTATIVE_DEFINITIONS:
3105 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3106 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
3107 break;
3108
3109 case KNOWN_NAMESPACES:
3110 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3111 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
3112 break;
Nick Lewycky8334af82013-01-26 00:35:08 +00003113
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00003114 case UNDEFINED_BUT_USED:
3115 if (UndefinedButUsed.size() % 2 != 0) {
3116 Error("Invalid existing UndefinedButUsed");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003117 return Failure;
Nick Lewycky8334af82013-01-26 00:35:08 +00003118 }
3119
3120 if (Record.size() % 2 != 0) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00003121 Error("invalid undefined-but-used record");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003122 return Failure;
Nick Lewycky8334af82013-01-26 00:35:08 +00003123 }
3124 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00003125 UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
3126 UndefinedButUsed.push_back(
Nick Lewycky8334af82013-01-26 00:35:08 +00003127 ReadSourceLocation(F, Record, I).getRawEncoding());
3128 }
3129 break;
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00003130 case DELETE_EXPRS_TO_ANALYZE:
3131 for (unsigned I = 0, N = Record.size(); I != N;) {
3132 DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++]));
3133 const uint64_t Count = Record[I++];
3134 DelayedDeleteExprs.push_back(Count);
3135 for (uint64_t C = 0; C < Count; ++C) {
3136 DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding());
3137 bool IsArrayForm = Record[I++] == 1;
3138 DelayedDeleteExprs.push_back(IsArrayForm);
3139 }
3140 }
3141 break;
Nick Lewycky8334af82013-01-26 00:35:08 +00003142
Guy Benyei11169dd2012-12-18 14:30:41 +00003143 case IMPORTED_MODULES: {
Richard Smithe842a472014-10-22 02:05:46 +00003144 if (F.Kind != MK_ImplicitModule && F.Kind != MK_ExplicitModule) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003145 // If we aren't loading a module (which has its own exports), make
3146 // all of the imported modules visible.
3147 // FIXME: Deal with macros-only imports.
Richard Smith56be7542014-03-21 00:33:59 +00003148 for (unsigned I = 0, N = Record.size(); I != N; /**/) {
3149 unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
3150 SourceLocation Loc = ReadSourceLocation(F, Record, I);
3151 if (GlobalID)
Aaron Ballman4f45b712014-03-21 15:22:56 +00003152 ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
Guy Benyei11169dd2012-12-18 14:30:41 +00003153 }
3154 }
3155 break;
3156 }
3157
Guy Benyei11169dd2012-12-18 14:30:41 +00003158 case MACRO_OFFSET: {
3159 if (F.LocalNumMacros != 0) {
3160 Error("duplicate MACRO_OFFSET record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003161 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003162 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00003163 F.MacroOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003164 F.LocalNumMacros = Record[0];
3165 unsigned LocalBaseMacroID = Record[1];
3166 F.BaseMacroID = getTotalNumMacros();
3167
3168 if (F.LocalNumMacros > 0) {
3169 // Introduce the global -> local mapping for macros within this module.
3170 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3171
3172 // Introduce the local -> global mapping for macros within this module.
3173 F.MacroRemap.insertOrReplace(
3174 std::make_pair(LocalBaseMacroID,
3175 F.BaseMacroID - LocalBaseMacroID));
Ben Langmuir52ca6782014-10-20 16:27:32 +00003176
3177 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
Guy Benyei11169dd2012-12-18 14:30:41 +00003178 }
3179 break;
3180 }
3181
Richard Smithe40f2ba2013-08-07 21:41:30 +00003182 case LATE_PARSED_TEMPLATE: {
3183 LateParsedTemplates.append(Record.begin(), Record.end());
3184 break;
3185 }
Dario Domizioli13a0a382014-05-23 12:13:25 +00003186
3187 case OPTIMIZE_PRAGMA_OPTIONS:
3188 if (Record.size() != 1) {
3189 Error("invalid pragma optimize record");
3190 return Failure;
3191 }
3192 OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
3193 break;
Nico Weber72889432014-09-06 01:25:55 +00003194
Nico Weber779355f2016-03-02 23:22:00 +00003195 case MSSTRUCT_PRAGMA_OPTIONS:
3196 if (Record.size() != 1) {
3197 Error("invalid pragma ms_struct record");
3198 return Failure;
3199 }
3200 PragmaMSStructState = Record[0];
3201 break;
3202
Nico Weber42932312016-03-03 00:17:35 +00003203 case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS:
3204 if (Record.size() != 2) {
3205 Error("invalid pragma ms_struct record");
3206 return Failure;
3207 }
3208 PragmaMSPointersToMembersState = Record[0];
3209 PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]);
3210 break;
3211
Nico Weber72889432014-09-06 01:25:55 +00003212 case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
3213 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3214 UnusedLocalTypedefNameCandidates.push_back(
3215 getGlobalDeclID(F, Record[I]));
3216 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003217 }
3218 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003219}
3220
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003221ASTReader::ASTReadResult
3222ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
3223 const ModuleFile *ImportedBy,
3224 unsigned ClientLoadCapabilities) {
3225 unsigned Idx = 0;
Richard Smith7ed1bc92014-12-05 22:42:13 +00003226 F.ModuleMapPath = ReadPath(F, Record, Idx);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003227
Richard Smithe842a472014-10-22 02:05:46 +00003228 if (F.Kind == MK_ExplicitModule) {
3229 // For an explicitly-loaded module, we don't care whether the original
3230 // module map file exists or matches.
3231 return Success;
3232 }
3233
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003234 // Try to resolve ModuleName in the current header search context and
3235 // verify that it is found in the same module map file as we saved. If the
3236 // top-level AST file is a main file, skip this check because there is no
3237 // usable header search context.
3238 assert(!F.ModuleName.empty() &&
Richard Smithe842a472014-10-22 02:05:46 +00003239 "MODULE_NAME should come before MODULE_MAP_FILE");
3240 if (F.Kind == MK_ImplicitModule &&
3241 (*ModuleMgr.begin())->Kind != MK_MainFile) {
3242 // An implicitly-loaded module file should have its module listed in some
3243 // module map file that we've already loaded.
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003244 Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
Richard Smithe842a472014-10-22 02:05:46 +00003245 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
3246 const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr;
3247 if (!ModMap) {
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003248 assert(ImportedBy && "top-level import should be verified");
Richard Smith0f99d6a2015-08-09 08:48:41 +00003249 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) {
3250 if (auto *ASTFE = M ? M->getASTFile() : nullptr)
3251 // This module was defined by an imported (explicit) module.
3252 Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName
3253 << ASTFE->getName();
3254 else
3255 // This module was built with a different module map.
3256 Diag(diag::err_imported_module_not_found)
3257 << F.ModuleName << F.FileName << ImportedBy->FileName
3258 << F.ModuleMapPath;
3259 }
3260 return OutOfDate;
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003261 }
3262
Richard Smithe842a472014-10-22 02:05:46 +00003263 assert(M->Name == F.ModuleName && "found module with different name");
3264
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003265 // Check the primary module map file.
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003266 const FileEntry *StoredModMap = FileMgr.getFile(F.ModuleMapPath);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003267 if (StoredModMap == nullptr || StoredModMap != ModMap) {
3268 assert(ModMap && "found module is missing module map file");
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003269 assert(ImportedBy && "top-level import should be verified");
3270 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3271 Diag(diag::err_imported_module_modmap_changed)
3272 << F.ModuleName << ImportedBy->FileName
3273 << ModMap->getName() << F.ModuleMapPath;
3274 return OutOfDate;
3275 }
3276
3277 llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps;
3278 for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
3279 // FIXME: we should use input files rather than storing names.
Richard Smith7ed1bc92014-12-05 22:42:13 +00003280 std::string Filename = ReadPath(F, Record, Idx);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003281 const FileEntry *F =
3282 FileMgr.getFile(Filename, false, false);
3283 if (F == nullptr) {
3284 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3285 Error("could not find file '" + Filename +"' referenced by AST file");
3286 return OutOfDate;
3287 }
3288 AdditionalStoredMaps.insert(F);
3289 }
3290
3291 // Check any additional module map files (e.g. module.private.modulemap)
3292 // that are not in the pcm.
3293 if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
3294 for (const FileEntry *ModMap : *AdditionalModuleMaps) {
3295 // Remove files that match
3296 // Note: SmallPtrSet::erase is really remove
3297 if (!AdditionalStoredMaps.erase(ModMap)) {
3298 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3299 Diag(diag::err_module_different_modmap)
3300 << F.ModuleName << /*new*/0 << ModMap->getName();
3301 return OutOfDate;
3302 }
3303 }
3304 }
3305
3306 // Check any additional module map files that are in the pcm, but not
3307 // found in header search. Cases that match are already removed.
3308 for (const FileEntry *ModMap : AdditionalStoredMaps) {
3309 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3310 Diag(diag::err_module_different_modmap)
3311 << F.ModuleName << /*not new*/1 << ModMap->getName();
3312 return OutOfDate;
3313 }
3314 }
3315
3316 if (Listener)
3317 Listener->ReadModuleMapFile(F.ModuleMapPath);
3318 return Success;
3319}
3320
3321
Douglas Gregorc1489562013-02-12 23:36:21 +00003322/// \brief Move the given method to the back of the global list of methods.
3323static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
3324 // Find the entry for this selector in the method pool.
3325 Sema::GlobalMethodPool::iterator Known
3326 = S.MethodPool.find(Method->getSelector());
3327 if (Known == S.MethodPool.end())
3328 return;
3329
3330 // Retrieve the appropriate method list.
3331 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
3332 : Known->second.second;
3333 bool Found = false;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003334 for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
Douglas Gregorc1489562013-02-12 23:36:21 +00003335 if (!Found) {
Nico Weber2e0c8f72014-12-27 03:58:08 +00003336 if (List->getMethod() == Method) {
Douglas Gregorc1489562013-02-12 23:36:21 +00003337 Found = true;
3338 } else {
3339 // Keep searching.
3340 continue;
3341 }
3342 }
3343
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003344 if (List->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003345 List->setMethod(List->getNext()->getMethod());
Douglas Gregorc1489562013-02-12 23:36:21 +00003346 else
Nico Weber2e0c8f72014-12-27 03:58:08 +00003347 List->setMethod(Method);
Douglas Gregorc1489562013-02-12 23:36:21 +00003348 }
3349}
3350
Richard Smithde711422015-04-23 21:20:19 +00003351void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
Richard Smith10434f32015-05-02 02:08:26 +00003352 assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
Richard Smith20e883e2015-04-29 23:20:19 +00003353 for (Decl *D : Names) {
Richard Smith49f906a2014-03-01 00:08:04 +00003354 bool wasHidden = D->Hidden;
3355 D->Hidden = false;
Guy Benyei11169dd2012-12-18 14:30:41 +00003356
Richard Smith49f906a2014-03-01 00:08:04 +00003357 if (wasHidden && SemaObj) {
3358 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
3359 moveMethodToBackOfGlobalList(*SemaObj, Method);
Douglas Gregorc1489562013-02-12 23:36:21 +00003360 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003361 }
3362 }
3363}
3364
Richard Smith49f906a2014-03-01 00:08:04 +00003365void ASTReader::makeModuleVisible(Module *Mod,
Argyrios Kyrtzidis125df052013-02-01 16:36:12 +00003366 Module::NameVisibilityKind NameVisibility,
Richard Smitha7e2cc62015-05-01 01:53:09 +00003367 SourceLocation ImportLoc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003368 llvm::SmallPtrSet<Module *, 4> Visited;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003369 SmallVector<Module *, 4> Stack;
Robert Wilhelm25284cc2013-08-23 16:11:15 +00003370 Stack.push_back(Mod);
Guy Benyei11169dd2012-12-18 14:30:41 +00003371 while (!Stack.empty()) {
Robert Wilhelm25284cc2013-08-23 16:11:15 +00003372 Mod = Stack.pop_back_val();
Guy Benyei11169dd2012-12-18 14:30:41 +00003373
3374 if (NameVisibility <= Mod->NameVisibility) {
Robert Wilhelm25284cc2013-08-23 16:11:15 +00003375 // This module already has this level of visibility (or greater), so
Guy Benyei11169dd2012-12-18 14:30:41 +00003376 // there is nothing more to do.
3377 continue;
3378 }
Richard Smith49f906a2014-03-01 00:08:04 +00003379
Guy Benyei11169dd2012-12-18 14:30:41 +00003380 if (!Mod->isAvailable()) {
3381 // Modules that aren't available cannot be made visible.
3382 continue;
3383 }
3384
3385 // Update the module's name visibility.
3386 Mod->NameVisibility = NameVisibility;
Richard Smith49f906a2014-03-01 00:08:04 +00003387
Guy Benyei11169dd2012-12-18 14:30:41 +00003388 // If we've already deserialized any names from this module,
3389 // mark them as visible.
3390 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
3391 if (Hidden != HiddenNamesMap.end()) {
Richard Smith57721ac2014-07-21 04:10:40 +00003392 auto HiddenNames = std::move(*Hidden);
Guy Benyei11169dd2012-12-18 14:30:41 +00003393 HiddenNamesMap.erase(Hidden);
Richard Smithde711422015-04-23 21:20:19 +00003394 makeNamesVisible(HiddenNames.second, HiddenNames.first);
Richard Smith57721ac2014-07-21 04:10:40 +00003395 assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() &&
3396 "making names visible added hidden names");
Guy Benyei11169dd2012-12-18 14:30:41 +00003397 }
Dmitri Gribenkoe9bcf5b2013-11-04 21:51:33 +00003398
Guy Benyei11169dd2012-12-18 14:30:41 +00003399 // Push any exported modules onto the stack to be marked as visible.
Argyrios Kyrtzidis8739f7b2013-02-19 19:34:40 +00003400 SmallVector<Module *, 16> Exports;
3401 Mod->getExportedModules(Exports);
3402 for (SmallVectorImpl<Module *>::iterator
3403 I = Exports.begin(), E = Exports.end(); I != E; ++I) {
3404 Module *Exported = *I;
David Blaikie82e95a32014-11-19 07:49:47 +00003405 if (Visited.insert(Exported).second)
Argyrios Kyrtzidis8739f7b2013-02-19 19:34:40 +00003406 Stack.push_back(Exported);
Guy Benyei11169dd2012-12-18 14:30:41 +00003407 }
3408 }
3409}
3410
Douglas Gregore060e572013-01-25 01:03:03 +00003411bool ASTReader::loadGlobalIndex() {
3412 if (GlobalIndex)
3413 return false;
3414
3415 if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
3416 !Context.getLangOpts().Modules)
3417 return true;
3418
3419 // Try to load the global index.
3420 TriedLoadingGlobalIndex = true;
3421 StringRef ModuleCachePath
3422 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
3423 std::pair<GlobalModuleIndex *, GlobalModuleIndex::ErrorCode> Result
Douglas Gregor7029ce12013-03-19 00:28:20 +00003424 = GlobalModuleIndex::readIndex(ModuleCachePath);
Douglas Gregore060e572013-01-25 01:03:03 +00003425 if (!Result.first)
3426 return true;
3427
3428 GlobalIndex.reset(Result.first);
Douglas Gregor7211ac12013-01-25 23:32:03 +00003429 ModuleMgr.setGlobalIndex(GlobalIndex.get());
Douglas Gregore060e572013-01-25 01:03:03 +00003430 return false;
3431}
3432
3433bool ASTReader::isGlobalIndexUnavailable() const {
3434 return Context.getLangOpts().Modules && UseGlobalIndex &&
3435 !hasGlobalIndex() && TriedLoadingGlobalIndex;
3436}
3437
Dmitri Gribenkof430da42014-02-12 10:33:14 +00003438static void updateModuleTimestamp(ModuleFile &MF) {
3439 // Overwrite the timestamp file contents so that file's mtime changes.
3440 std::string TimestampFilename = MF.getTimestampFilename();
Rafael Espindoladae941a2014-08-25 18:17:04 +00003441 std::error_code EC;
3442 llvm::raw_fd_ostream OS(TimestampFilename, EC, llvm::sys::fs::F_Text);
3443 if (EC)
Dmitri Gribenkof430da42014-02-12 10:33:14 +00003444 return;
3445 OS << "Timestamp file\n";
3446}
3447
Douglas Gregor6623e1f2015-11-03 18:33:07 +00003448/// \brief Given a cursor at the start of an AST file, scan ahead and drop the
3449/// cursor into the start of the given block ID, returning false on success and
3450/// true on failure.
3451static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
3452 while (1) {
3453 llvm::BitstreamEntry Entry = Cursor.advance();
3454 switch (Entry.Kind) {
3455 case llvm::BitstreamEntry::Error:
3456 case llvm::BitstreamEntry::EndBlock:
3457 return true;
3458
3459 case llvm::BitstreamEntry::Record:
3460 // Ignore top-level records.
3461 Cursor.skipRecord(Entry.ID);
3462 break;
3463
3464 case llvm::BitstreamEntry::SubBlock:
3465 if (Entry.ID == BlockID) {
3466 if (Cursor.EnterSubBlock(BlockID))
3467 return true;
3468 // Found it!
3469 return false;
3470 }
3471
3472 if (Cursor.SkipBlock())
3473 return true;
3474 }
3475 }
3476}
3477
Benjamin Kramer0772c422016-02-13 13:42:54 +00003478ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName,
Guy Benyei11169dd2012-12-18 14:30:41 +00003479 ModuleKind Type,
3480 SourceLocation ImportLoc,
3481 unsigned ClientLoadCapabilities) {
Argyrios Kyrtzidisdc9fdaf2013-05-24 05:44:08 +00003482 llvm::SaveAndRestore<SourceLocation>
3483 SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
3484
Richard Smithd1c46742014-04-30 02:24:17 +00003485 // Defer any pending actions until we get to the end of reading the AST file.
3486 Deserializing AnASTFile(this);
3487
Guy Benyei11169dd2012-12-18 14:30:41 +00003488 // Bump the generation number.
Richard Smith053f6c62014-05-16 23:01:30 +00003489 unsigned PreviousGeneration = incrementGeneration(Context);
Guy Benyei11169dd2012-12-18 14:30:41 +00003490
3491 unsigned NumModules = ModuleMgr.size();
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003492 SmallVector<ImportedModule, 4> Loaded;
Guy Benyei11169dd2012-12-18 14:30:41 +00003493 switch(ASTReadResult ReadResult = ReadASTCore(FileName, Type, ImportLoc,
Craig Toppera13603a2014-05-22 05:54:18 +00003494 /*ImportedBy=*/nullptr, Loaded,
Ben Langmuir487ea142014-10-23 18:05:36 +00003495 0, 0, 0,
Guy Benyei11169dd2012-12-18 14:30:41 +00003496 ClientLoadCapabilities)) {
3497 case Failure:
Douglas Gregor7029ce12013-03-19 00:28:20 +00003498 case Missing:
Guy Benyei11169dd2012-12-18 14:30:41 +00003499 case OutOfDate:
3500 case VersionMismatch:
3501 case ConfigurationMismatch:
Ben Langmuir9801b252014-06-20 00:24:56 +00003502 case HadErrors: {
3503 llvm::SmallPtrSet<ModuleFile *, 4> LoadedSet;
3504 for (const ImportedModule &IM : Loaded)
3505 LoadedSet.insert(IM.Mod);
3506
Douglas Gregor7029ce12013-03-19 00:28:20 +00003507 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, ModuleMgr.end(),
Ben Langmuir9801b252014-06-20 00:24:56 +00003508 LoadedSet,
Douglas Gregor7029ce12013-03-19 00:28:20 +00003509 Context.getLangOpts().Modules
3510 ? &PP.getHeaderSearchInfo().getModuleMap()
Craig Toppera13603a2014-05-22 05:54:18 +00003511 : nullptr);
Douglas Gregore060e572013-01-25 01:03:03 +00003512
3513 // If we find that any modules are unusable, the global index is going
3514 // to be out-of-date. Just remove it.
3515 GlobalIndex.reset();
Craig Toppera13603a2014-05-22 05:54:18 +00003516 ModuleMgr.setGlobalIndex(nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00003517 return ReadResult;
Ben Langmuir9801b252014-06-20 00:24:56 +00003518 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003519 case Success:
3520 break;
3521 }
3522
3523 // Here comes stuff that we only do once the entire chain is loaded.
3524
3525 // Load the AST blocks of all of the modules that we loaded.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003526 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
3527 MEnd = Loaded.end();
Guy Benyei11169dd2012-12-18 14:30:41 +00003528 M != MEnd; ++M) {
3529 ModuleFile &F = *M->Mod;
3530
3531 // Read the AST block.
Ben Langmuir2c9af442014-04-10 17:57:43 +00003532 if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities))
3533 return Result;
Guy Benyei11169dd2012-12-18 14:30:41 +00003534
Douglas Gregor6623e1f2015-11-03 18:33:07 +00003535 // Read the extension blocks.
3536 while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) {
3537 if (ASTReadResult Result = ReadExtensionBlock(F))
3538 return Result;
3539 }
3540
Guy Benyei11169dd2012-12-18 14:30:41 +00003541 // Once read, set the ModuleFile bit base offset and update the size in
3542 // bits of all files we've seen.
3543 F.GlobalBitOffset = TotalModulesSizeInBits;
3544 TotalModulesSizeInBits += F.SizeInBits;
3545 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
3546
3547 // Preload SLocEntries.
3548 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
3549 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
3550 // Load it through the SourceManager and don't call ReadSLocEntry()
3551 // directly because the entry may have already been loaded in which case
3552 // calling ReadSLocEntry() directly would trigger an assertion in
3553 // SourceManager.
3554 SourceMgr.getLoadedSLocEntryByID(Index);
3555 }
Richard Smith33e0f7e2015-07-22 02:08:40 +00003556
3557 // Preload all the pending interesting identifiers by marking them out of
3558 // date.
3559 for (auto Offset : F.PreloadIdentifierOffsets) {
3560 const unsigned char *Data = reinterpret_cast<const unsigned char *>(
3561 F.IdentifierTableData + Offset);
3562
3563 ASTIdentifierLookupTrait Trait(*this, F);
3564 auto KeyDataLen = Trait.ReadKeyDataLength(Data);
3565 auto Key = Trait.ReadKey(Data, KeyDataLen.first);
Richard Smith79bf9202015-08-24 03:33:22 +00003566 auto &II = PP.getIdentifierTable().getOwn(Key);
3567 II.setOutOfDate(true);
3568
3569 // Mark this identifier as being from an AST file so that we can track
3570 // whether we need to serialize it.
Richard Smitheb4b58f62016-02-05 01:40:54 +00003571 markIdentifierFromAST(*this, II);
Richard Smith79bf9202015-08-24 03:33:22 +00003572
3573 // Associate the ID with the identifier so that the writer can reuse it.
3574 auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first);
3575 SetIdentifierInfo(ID, &II);
Richard Smith33e0f7e2015-07-22 02:08:40 +00003576 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003577 }
3578
Douglas Gregor603cd862013-03-22 18:50:14 +00003579 // Setup the import locations and notify the module manager that we've
3580 // committed to these module files.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003581 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
3582 MEnd = Loaded.end();
Guy Benyei11169dd2012-12-18 14:30:41 +00003583 M != MEnd; ++M) {
3584 ModuleFile &F = *M->Mod;
Douglas Gregor603cd862013-03-22 18:50:14 +00003585
3586 ModuleMgr.moduleFileAccepted(&F);
3587
3588 // Set the import location.
Argyrios Kyrtzidis71c1af82013-02-01 16:36:14 +00003589 F.DirectImportLoc = ImportLoc;
Richard Smithb22a1d12016-03-27 20:13:24 +00003590 // FIXME: We assume that locations from PCH / preamble do not need
3591 // any translation.
Guy Benyei11169dd2012-12-18 14:30:41 +00003592 if (!M->ImportedBy)
3593 F.ImportLoc = M->ImportLoc;
3594 else
Richard Smithb22a1d12016-03-27 20:13:24 +00003595 F.ImportLoc = TranslateSourceLocation(*M->ImportedBy, M->ImportLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003596 }
3597
Richard Smith33e0f7e2015-07-22 02:08:40 +00003598 if (!Context.getLangOpts().CPlusPlus ||
3599 (Type != MK_ImplicitModule && Type != MK_ExplicitModule)) {
3600 // Mark all of the identifiers in the identifier table as being out of date,
3601 // so that various accessors know to check the loaded modules when the
3602 // identifier is used.
3603 //
3604 // For C++ modules, we don't need information on many identifiers (just
3605 // those that provide macros or are poisoned), so we mark all of
3606 // the interesting ones via PreloadIdentifierOffsets.
3607 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
3608 IdEnd = PP.getIdentifierTable().end();
3609 Id != IdEnd; ++Id)
3610 Id->second->setOutOfDate(true);
3611 }
Manman Rena0f31a02016-04-29 19:04:05 +00003612 // Mark selectors as out of date.
3613 for (auto Sel : SelectorGeneration)
3614 SelectorOutOfDate[Sel.first] = true;
Guy Benyei11169dd2012-12-18 14:30:41 +00003615
3616 // Resolve any unresolved module exports.
Douglas Gregorfb912652013-03-20 21:10:35 +00003617 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
3618 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
Guy Benyei11169dd2012-12-18 14:30:41 +00003619 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
3620 Module *ResolvedMod = getSubmodule(GlobalID);
Douglas Gregorfb912652013-03-20 21:10:35 +00003621
3622 switch (Unresolved.Kind) {
3623 case UnresolvedModuleRef::Conflict:
3624 if (ResolvedMod) {
3625 Module::Conflict Conflict;
3626 Conflict.Other = ResolvedMod;
3627 Conflict.Message = Unresolved.String.str();
3628 Unresolved.Mod->Conflicts.push_back(Conflict);
3629 }
3630 continue;
3631
3632 case UnresolvedModuleRef::Import:
Guy Benyei11169dd2012-12-18 14:30:41 +00003633 if (ResolvedMod)
Richard Smith38477db2015-05-02 00:45:56 +00003634 Unresolved.Mod->Imports.insert(ResolvedMod);
Guy Benyei11169dd2012-12-18 14:30:41 +00003635 continue;
Guy Benyei11169dd2012-12-18 14:30:41 +00003636
Douglas Gregorfb912652013-03-20 21:10:35 +00003637 case UnresolvedModuleRef::Export:
3638 if (ResolvedMod || Unresolved.IsWildcard)
3639 Unresolved.Mod->Exports.push_back(
3640 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
3641 continue;
3642 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003643 }
Douglas Gregorfb912652013-03-20 21:10:35 +00003644 UnresolvedModuleRefs.clear();
Daniel Jasperba7f2f72013-09-24 09:14:14 +00003645
3646 // FIXME: How do we load the 'use'd modules? They may not be submodules.
3647 // Might be unnecessary as use declarations are only used to build the
3648 // module itself.
Guy Benyei11169dd2012-12-18 14:30:41 +00003649
3650 InitializeContext();
3651
Richard Smith3d8e97e2013-10-18 06:54:39 +00003652 if (SemaObj)
3653 UpdateSema();
3654
Guy Benyei11169dd2012-12-18 14:30:41 +00003655 if (DeserializationListener)
3656 DeserializationListener->ReaderInitialized(this);
3657
3658 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
Yaron Keren8b563662015-10-03 10:46:20 +00003659 if (PrimaryModule.OriginalSourceFileID.isValid()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003660 PrimaryModule.OriginalSourceFileID
3661 = FileID::get(PrimaryModule.SLocEntryBaseID
3662 + PrimaryModule.OriginalSourceFileID.getOpaqueValue() - 1);
3663
3664 // If this AST file is a precompiled preamble, then set the
3665 // preamble file ID of the source manager to the file source file
3666 // from which the preamble was built.
3667 if (Type == MK_Preamble) {
3668 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
3669 } else if (Type == MK_MainFile) {
3670 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
3671 }
3672 }
3673
3674 // For any Objective-C class definitions we have already loaded, make sure
3675 // that we load any additional categories.
3676 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
3677 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
3678 ObjCClassesLoaded[I],
3679 PreviousGeneration);
3680 }
Douglas Gregore060e572013-01-25 01:03:03 +00003681
Dmitri Gribenkof430da42014-02-12 10:33:14 +00003682 if (PP.getHeaderSearchInfo()
3683 .getHeaderSearchOpts()
3684 .ModulesValidateOncePerBuildSession) {
3685 // Now we are certain that the module and all modules it depends on are
3686 // up to date. Create or update timestamp files for modules that are
3687 // located in the module cache (not for PCH files that could be anywhere
3688 // in the filesystem).
3689 for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
3690 ImportedModule &M = Loaded[I];
Richard Smithe842a472014-10-22 02:05:46 +00003691 if (M.Mod->Kind == MK_ImplicitModule) {
Dmitri Gribenkof430da42014-02-12 10:33:14 +00003692 updateModuleTimestamp(*M.Mod);
3693 }
3694 }
3695 }
3696
Guy Benyei11169dd2012-12-18 14:30:41 +00003697 return Success;
3698}
3699
Ben Langmuir487ea142014-10-23 18:05:36 +00003700static ASTFileSignature readASTFileSignature(llvm::BitstreamReader &StreamFile);
3701
Ben Langmuir70a1b812015-03-24 04:43:52 +00003702/// \brief Whether \p Stream starts with the AST/PCH file magic number 'CPCH'.
3703static bool startsWithASTFileMagic(BitstreamCursor &Stream) {
3704 return Stream.Read(8) == 'C' &&
3705 Stream.Read(8) == 'P' &&
3706 Stream.Read(8) == 'C' &&
3707 Stream.Read(8) == 'H';
3708}
3709
Richard Smith0f99d6a2015-08-09 08:48:41 +00003710static unsigned moduleKindForDiagnostic(ModuleKind Kind) {
3711 switch (Kind) {
3712 case MK_PCH:
3713 return 0; // PCH
3714 case MK_ImplicitModule:
3715 case MK_ExplicitModule:
3716 return 1; // module
3717 case MK_MainFile:
3718 case MK_Preamble:
3719 return 2; // main source file
3720 }
3721 llvm_unreachable("unknown module kind");
3722}
3723
Guy Benyei11169dd2012-12-18 14:30:41 +00003724ASTReader::ASTReadResult
3725ASTReader::ReadASTCore(StringRef FileName,
3726 ModuleKind Type,
3727 SourceLocation ImportLoc,
3728 ModuleFile *ImportedBy,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003729 SmallVectorImpl<ImportedModule> &Loaded,
Douglas Gregor7029ce12013-03-19 00:28:20 +00003730 off_t ExpectedSize, time_t ExpectedModTime,
Ben Langmuir487ea142014-10-23 18:05:36 +00003731 ASTFileSignature ExpectedSignature,
Guy Benyei11169dd2012-12-18 14:30:41 +00003732 unsigned ClientLoadCapabilities) {
3733 ModuleFile *M;
Guy Benyei11169dd2012-12-18 14:30:41 +00003734 std::string ErrorStr;
Douglas Gregor7029ce12013-03-19 00:28:20 +00003735 ModuleManager::AddModuleResult AddResult
3736 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
Richard Smith053f6c62014-05-16 23:01:30 +00003737 getGeneration(), ExpectedSize, ExpectedModTime,
Ben Langmuir487ea142014-10-23 18:05:36 +00003738 ExpectedSignature, readASTFileSignature,
Douglas Gregor7029ce12013-03-19 00:28:20 +00003739 M, ErrorStr);
Guy Benyei11169dd2012-12-18 14:30:41 +00003740
Douglas Gregor7029ce12013-03-19 00:28:20 +00003741 switch (AddResult) {
3742 case ModuleManager::AlreadyLoaded:
3743 return Success;
3744
3745 case ModuleManager::NewlyLoaded:
3746 // Load module file below.
3747 break;
3748
3749 case ModuleManager::Missing:
Richard Smithe842a472014-10-22 02:05:46 +00003750 // The module file was missing; if the client can handle that, return
Douglas Gregor7029ce12013-03-19 00:28:20 +00003751 // it.
3752 if (ClientLoadCapabilities & ARR_Missing)
3753 return Missing;
3754
3755 // Otherwise, return an error.
Richard Smith0f99d6a2015-08-09 08:48:41 +00003756 Diag(diag::err_module_file_not_found) << moduleKindForDiagnostic(Type)
3757 << FileName << ErrorStr.empty()
3758 << ErrorStr;
Douglas Gregor7029ce12013-03-19 00:28:20 +00003759 return Failure;
3760
3761 case ModuleManager::OutOfDate:
3762 // We couldn't load the module file because it is out-of-date. If the
3763 // client can handle out-of-date, return it.
3764 if (ClientLoadCapabilities & ARR_OutOfDate)
3765 return OutOfDate;
3766
3767 // Otherwise, return an error.
Richard Smith0f99d6a2015-08-09 08:48:41 +00003768 Diag(diag::err_module_file_out_of_date) << moduleKindForDiagnostic(Type)
3769 << FileName << ErrorStr.empty()
3770 << ErrorStr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003771 return Failure;
3772 }
3773
Douglas Gregor7029ce12013-03-19 00:28:20 +00003774 assert(M && "Missing module file");
Guy Benyei11169dd2012-12-18 14:30:41 +00003775
3776 // FIXME: This seems rather a hack. Should CurrentDir be part of the
3777 // module?
3778 if (FileName != "-") {
3779 CurrentDir = llvm::sys::path::parent_path(FileName);
3780 if (CurrentDir.empty()) CurrentDir = ".";
3781 }
3782
3783 ModuleFile &F = *M;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003784 BitstreamCursor &Stream = F.Stream;
Adrian Prantlfb2398d2015-07-17 01:19:54 +00003785 PCHContainerRdr.ExtractPCH(F.Buffer->getMemBufferRef(), F.StreamFile);
Rafael Espindolafd832392014-11-12 14:48:44 +00003786 Stream.init(&F.StreamFile);
Adrian Prantlcbc368c2015-02-25 02:44:04 +00003787 F.SizeInBits = F.Buffer->getBufferSize() * 8;
3788
Guy Benyei11169dd2012-12-18 14:30:41 +00003789 // Sniff for the signature.
Ben Langmuir70a1b812015-03-24 04:43:52 +00003790 if (!startsWithASTFileMagic(Stream)) {
Richard Smith0f99d6a2015-08-09 08:48:41 +00003791 Diag(diag::err_module_file_invalid) << moduleKindForDiagnostic(Type)
3792 << FileName;
Guy Benyei11169dd2012-12-18 14:30:41 +00003793 return Failure;
3794 }
3795
3796 // This is used for compatibility with older PCH formats.
3797 bool HaveReadControlBlock = false;
Chris Lattnerefa77172013-01-20 00:00:22 +00003798 while (1) {
3799 llvm::BitstreamEntry Entry = Stream.advance();
3800
3801 switch (Entry.Kind) {
3802 case llvm::BitstreamEntry::Error:
Chris Lattnerefa77172013-01-20 00:00:22 +00003803 case llvm::BitstreamEntry::Record:
Douglas Gregor6623e1f2015-11-03 18:33:07 +00003804 case llvm::BitstreamEntry::EndBlock:
Guy Benyei11169dd2012-12-18 14:30:41 +00003805 Error("invalid record at top-level of AST file");
3806 return Failure;
Chris Lattnerefa77172013-01-20 00:00:22 +00003807
3808 case llvm::BitstreamEntry::SubBlock:
3809 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003810 }
3811
Chris Lattnerefa77172013-01-20 00:00:22 +00003812 switch (Entry.ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003813 case CONTROL_BLOCK_ID:
3814 HaveReadControlBlock = true;
Ben Langmuirbeee15e2014-04-14 18:00:01 +00003815 switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003816 case Success:
Richard Smith0f99d6a2015-08-09 08:48:41 +00003817 // Check that we didn't try to load a non-module AST file as a module.
3818 //
3819 // FIXME: Should we also perform the converse check? Loading a module as
3820 // a PCH file sort of works, but it's a bit wonky.
3821 if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule) &&
3822 F.ModuleName.empty()) {
3823 auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
3824 if (Result != OutOfDate ||
3825 (ClientLoadCapabilities & ARR_OutOfDate) == 0)
3826 Diag(diag::err_module_file_not_module) << FileName;
3827 return Result;
3828 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003829 break;
3830
3831 case Failure: return Failure;
Douglas Gregor7029ce12013-03-19 00:28:20 +00003832 case Missing: return Missing;
Guy Benyei11169dd2012-12-18 14:30:41 +00003833 case OutOfDate: return OutOfDate;
3834 case VersionMismatch: return VersionMismatch;
3835 case ConfigurationMismatch: return ConfigurationMismatch;
3836 case HadErrors: return HadErrors;
3837 }
3838 break;
Richard Smithf8c32552015-09-02 17:45:54 +00003839
Guy Benyei11169dd2012-12-18 14:30:41 +00003840 case AST_BLOCK_ID:
3841 if (!HaveReadControlBlock) {
3842 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
Dmitri Gribenko2228cd32014-02-11 15:40:09 +00003843 Diag(diag::err_pch_version_too_old);
Guy Benyei11169dd2012-12-18 14:30:41 +00003844 return VersionMismatch;
3845 }
3846
3847 // Record that we've loaded this module.
3848 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
3849 return Success;
3850
3851 default:
3852 if (Stream.SkipBlock()) {
3853 Error("malformed block record in AST file");
3854 return Failure;
3855 }
3856 break;
3857 }
3858 }
Douglas Gregor6623e1f2015-11-03 18:33:07 +00003859
3860 return Success;
3861}
3862
3863/// Parse a record and blob containing module file extension metadata.
3864static bool parseModuleFileExtensionMetadata(
3865 const SmallVectorImpl<uint64_t> &Record,
3866 StringRef Blob,
3867 ModuleFileExtensionMetadata &Metadata) {
3868 if (Record.size() < 4) return true;
3869
3870 Metadata.MajorVersion = Record[0];
3871 Metadata.MinorVersion = Record[1];
3872
3873 unsigned BlockNameLen = Record[2];
3874 unsigned UserInfoLen = Record[3];
3875
3876 if (BlockNameLen + UserInfoLen > Blob.size()) return true;
3877
3878 Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
3879 Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
3880 Blob.data() + BlockNameLen + UserInfoLen);
3881 return false;
3882}
3883
3884ASTReader::ASTReadResult ASTReader::ReadExtensionBlock(ModuleFile &F) {
3885 BitstreamCursor &Stream = F.Stream;
3886
3887 RecordData Record;
3888 while (true) {
3889 llvm::BitstreamEntry Entry = Stream.advance();
3890 switch (Entry.Kind) {
3891 case llvm::BitstreamEntry::SubBlock:
3892 if (Stream.SkipBlock())
3893 return Failure;
3894
3895 continue;
3896
3897 case llvm::BitstreamEntry::EndBlock:
3898 return Success;
3899
3900 case llvm::BitstreamEntry::Error:
3901 return HadErrors;
3902
3903 case llvm::BitstreamEntry::Record:
3904 break;
3905 }
3906
3907 Record.clear();
3908 StringRef Blob;
3909 unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob);
3910 switch (RecCode) {
3911 case EXTENSION_METADATA: {
3912 ModuleFileExtensionMetadata Metadata;
3913 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
3914 return Failure;
3915
3916 // Find a module file extension with this block name.
3917 auto Known = ModuleFileExtensions.find(Metadata.BlockName);
3918 if (Known == ModuleFileExtensions.end()) break;
3919
3920 // Form a reader.
3921 if (auto Reader = Known->second->createExtensionReader(Metadata, *this,
3922 F, Stream)) {
3923 F.ExtensionReaders.push_back(std::move(Reader));
3924 }
3925
3926 break;
3927 }
3928 }
3929 }
3930
3931 return Success;
Guy Benyei11169dd2012-12-18 14:30:41 +00003932}
3933
Richard Smitha7e2cc62015-05-01 01:53:09 +00003934void ASTReader::InitializeContext() {
Guy Benyei11169dd2012-12-18 14:30:41 +00003935 // If there's a listener, notify them that we "read" the translation unit.
3936 if (DeserializationListener)
3937 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
3938 Context.getTranslationUnitDecl());
3939
Guy Benyei11169dd2012-12-18 14:30:41 +00003940 // FIXME: Find a better way to deal with collisions between these
3941 // built-in types. Right now, we just ignore the problem.
3942
3943 // Load the special types.
3944 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
3945 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
3946 if (!Context.CFConstantStringTypeDecl)
3947 Context.setCFConstantStringType(GetType(String));
3948 }
3949
3950 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
3951 QualType FileType = GetType(File);
3952 if (FileType.isNull()) {
3953 Error("FILE type is NULL");
3954 return;
3955 }
3956
3957 if (!Context.FILEDecl) {
3958 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
3959 Context.setFILEDecl(Typedef->getDecl());
3960 else {
3961 const TagType *Tag = FileType->getAs<TagType>();
3962 if (!Tag) {
3963 Error("Invalid FILE type in AST file");
3964 return;
3965 }
3966 Context.setFILEDecl(Tag->getDecl());
3967 }
3968 }
3969 }
3970
3971 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
3972 QualType Jmp_bufType = GetType(Jmp_buf);
3973 if (Jmp_bufType.isNull()) {
3974 Error("jmp_buf type is NULL");
3975 return;
3976 }
3977
3978 if (!Context.jmp_bufDecl) {
3979 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
3980 Context.setjmp_bufDecl(Typedef->getDecl());
3981 else {
3982 const TagType *Tag = Jmp_bufType->getAs<TagType>();
3983 if (!Tag) {
3984 Error("Invalid jmp_buf type in AST file");
3985 return;
3986 }
3987 Context.setjmp_bufDecl(Tag->getDecl());
3988 }
3989 }
3990 }
3991
3992 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
3993 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
3994 if (Sigjmp_bufType.isNull()) {
3995 Error("sigjmp_buf type is NULL");
3996 return;
3997 }
3998
3999 if (!Context.sigjmp_bufDecl) {
4000 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
4001 Context.setsigjmp_bufDecl(Typedef->getDecl());
4002 else {
4003 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
4004 assert(Tag && "Invalid sigjmp_buf type in AST file");
4005 Context.setsigjmp_bufDecl(Tag->getDecl());
4006 }
4007 }
4008 }
4009
4010 if (unsigned ObjCIdRedef
4011 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
4012 if (Context.ObjCIdRedefinitionType.isNull())
4013 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
4014 }
4015
4016 if (unsigned ObjCClassRedef
4017 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
4018 if (Context.ObjCClassRedefinitionType.isNull())
4019 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
4020 }
4021
4022 if (unsigned ObjCSelRedef
4023 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
4024 if (Context.ObjCSelRedefinitionType.isNull())
4025 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
4026 }
4027
4028 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
4029 QualType Ucontext_tType = GetType(Ucontext_t);
4030 if (Ucontext_tType.isNull()) {
4031 Error("ucontext_t type is NULL");
4032 return;
4033 }
4034
4035 if (!Context.ucontext_tDecl) {
4036 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
4037 Context.setucontext_tDecl(Typedef->getDecl());
4038 else {
4039 const TagType *Tag = Ucontext_tType->getAs<TagType>();
4040 assert(Tag && "Invalid ucontext_t type in AST file");
4041 Context.setucontext_tDecl(Tag->getDecl());
4042 }
4043 }
4044 }
4045 }
4046
4047 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
4048
4049 // If there were any CUDA special declarations, deserialize them.
4050 if (!CUDASpecialDeclRefs.empty()) {
4051 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
4052 Context.setcudaConfigureCallDecl(
4053 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
4054 }
Richard Smith56be7542014-03-21 00:33:59 +00004055
Guy Benyei11169dd2012-12-18 14:30:41 +00004056 // Re-export any modules that were imported by a non-module AST file.
Richard Smitha7e2cc62015-05-01 01:53:09 +00004057 // FIXME: This does not make macro-only imports visible again.
Richard Smith56be7542014-03-21 00:33:59 +00004058 for (auto &Import : ImportedModules) {
Richard Smitha7e2cc62015-05-01 01:53:09 +00004059 if (Module *Imported = getSubmodule(Import.ID)) {
Argyrios Kyrtzidis125df052013-02-01 16:36:12 +00004060 makeModuleVisible(Imported, Module::AllVisible,
Richard Smitha7e2cc62015-05-01 01:53:09 +00004061 /*ImportLoc=*/Import.ImportLoc);
Ben Langmuir6d25fdc2016-02-11 17:04:42 +00004062 if (Import.ImportLoc.isValid())
4063 PP.makeModuleVisible(Imported, Import.ImportLoc);
4064 // FIXME: should we tell Sema to make the module visible too?
Richard Smitha7e2cc62015-05-01 01:53:09 +00004065 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004066 }
4067 ImportedModules.clear();
4068}
4069
4070void ASTReader::finalizeForWriting() {
Richard Smithde711422015-04-23 21:20:19 +00004071 // Nothing to do for now.
Guy Benyei11169dd2012-12-18 14:30:41 +00004072}
4073
Ben Langmuir70a1b812015-03-24 04:43:52 +00004074/// \brief Reads and return the signature record from \p StreamFile's control
4075/// block, or else returns 0.
Ben Langmuir487ea142014-10-23 18:05:36 +00004076static ASTFileSignature readASTFileSignature(llvm::BitstreamReader &StreamFile){
4077 BitstreamCursor Stream(StreamFile);
Ben Langmuir70a1b812015-03-24 04:43:52 +00004078 if (!startsWithASTFileMagic(Stream))
Ben Langmuir487ea142014-10-23 18:05:36 +00004079 return 0;
Ben Langmuir487ea142014-10-23 18:05:36 +00004080
4081 // Scan for the CONTROL_BLOCK_ID block.
4082 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
4083 return 0;
4084
4085 // Scan for SIGNATURE inside the control block.
4086 ASTReader::RecordData Record;
4087 while (1) {
4088 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
4089 if (Entry.Kind == llvm::BitstreamEntry::EndBlock ||
4090 Entry.Kind != llvm::BitstreamEntry::Record)
4091 return 0;
4092
4093 Record.clear();
4094 StringRef Blob;
4095 if (SIGNATURE == Stream.readRecord(Entry.ID, Record, &Blob))
4096 return Record[0];
4097 }
4098}
4099
Guy Benyei11169dd2012-12-18 14:30:41 +00004100/// \brief Retrieve the name of the original source file name
4101/// directly from the AST file, without actually loading the AST
4102/// file.
Adrian Prantlbb165fb2015-06-20 18:53:08 +00004103std::string ASTReader::getOriginalSourceFile(
4104 const std::string &ASTFileName, FileManager &FileMgr,
Adrian Prantlfb2398d2015-07-17 01:19:54 +00004105 const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004106 // Open the AST file.
Benjamin Kramera8857962014-10-26 22:44:13 +00004107 auto Buffer = FileMgr.getBufferForFile(ASTFileName);
Guy Benyei11169dd2012-12-18 14:30:41 +00004108 if (!Buffer) {
Benjamin Kramera8857962014-10-26 22:44:13 +00004109 Diags.Report(diag::err_fe_unable_to_read_pch_file)
4110 << ASTFileName << Buffer.getError().message();
Guy Benyei11169dd2012-12-18 14:30:41 +00004111 return std::string();
4112 }
4113
4114 // Initialize the stream
4115 llvm::BitstreamReader StreamFile;
Adrian Prantlfb2398d2015-07-17 01:19:54 +00004116 PCHContainerRdr.ExtractPCH((*Buffer)->getMemBufferRef(), StreamFile);
Rafael Espindolaaf0e40a2014-11-12 14:42:25 +00004117 BitstreamCursor Stream(StreamFile);
Guy Benyei11169dd2012-12-18 14:30:41 +00004118
4119 // Sniff for the signature.
Ben Langmuir70a1b812015-03-24 04:43:52 +00004120 if (!startsWithASTFileMagic(Stream)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004121 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
4122 return std::string();
4123 }
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004124
Chris Lattnere7b154b2013-01-19 21:39:22 +00004125 // Scan for the CONTROL_BLOCK_ID block.
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004126 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004127 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
4128 return std::string();
Chris Lattnere7b154b2013-01-19 21:39:22 +00004129 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004130
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004131 // Scan for ORIGINAL_FILE inside the control block.
4132 RecordData Record;
Chris Lattnere7b154b2013-01-19 21:39:22 +00004133 while (1) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004134 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Chris Lattnere7b154b2013-01-19 21:39:22 +00004135 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
4136 return std::string();
4137
4138 if (Entry.Kind != llvm::BitstreamEntry::Record) {
4139 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
4140 return std::string();
Guy Benyei11169dd2012-12-18 14:30:41 +00004141 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00004142
Guy Benyei11169dd2012-12-18 14:30:41 +00004143 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00004144 StringRef Blob;
4145 if (Stream.readRecord(Entry.ID, Record, &Blob) == ORIGINAL_FILE)
4146 return Blob.str();
Guy Benyei11169dd2012-12-18 14:30:41 +00004147 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004148}
4149
4150namespace {
4151 class SimplePCHValidator : public ASTReaderListener {
4152 const LangOptions &ExistingLangOpts;
4153 const TargetOptions &ExistingTargetOpts;
4154 const PreprocessorOptions &ExistingPPOpts;
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00004155 std::string ExistingModuleCachePath;
Guy Benyei11169dd2012-12-18 14:30:41 +00004156 FileManager &FileMgr;
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00004157
Guy Benyei11169dd2012-12-18 14:30:41 +00004158 public:
4159 SimplePCHValidator(const LangOptions &ExistingLangOpts,
4160 const TargetOptions &ExistingTargetOpts,
4161 const PreprocessorOptions &ExistingPPOpts,
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00004162 StringRef ExistingModuleCachePath,
Guy Benyei11169dd2012-12-18 14:30:41 +00004163 FileManager &FileMgr)
4164 : ExistingLangOpts(ExistingLangOpts),
4165 ExistingTargetOpts(ExistingTargetOpts),
4166 ExistingPPOpts(ExistingPPOpts),
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00004167 ExistingModuleCachePath(ExistingModuleCachePath),
Guy Benyei11169dd2012-12-18 14:30:41 +00004168 FileMgr(FileMgr)
4169 {
4170 }
4171
Richard Smith1e2cf0d2014-10-31 02:28:58 +00004172 bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
4173 bool AllowCompatibleDifferences) override {
4174 return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr,
4175 AllowCompatibleDifferences);
Guy Benyei11169dd2012-12-18 14:30:41 +00004176 }
Chandler Carruth0d745bc2015-03-14 04:47:43 +00004177 bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
4178 bool AllowCompatibleDifferences) override {
4179 return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr,
4180 AllowCompatibleDifferences);
Guy Benyei11169dd2012-12-18 14:30:41 +00004181 }
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00004182 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
4183 StringRef SpecificModuleCachePath,
4184 bool Complain) override {
4185 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
4186 ExistingModuleCachePath,
4187 nullptr, ExistingLangOpts);
4188 }
Craig Topper3e89dfe2014-03-13 02:13:41 +00004189 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
4190 bool Complain,
4191 std::string &SuggestedPredefines) override {
Craig Toppera13603a2014-05-22 05:54:18 +00004192 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr,
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00004193 SuggestedPredefines, ExistingLangOpts);
Guy Benyei11169dd2012-12-18 14:30:41 +00004194 }
4195 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +00004196}
Guy Benyei11169dd2012-12-18 14:30:41 +00004197
Adrian Prantlbb165fb2015-06-20 18:53:08 +00004198bool ASTReader::readASTFileControlBlock(
4199 StringRef Filename, FileManager &FileMgr,
Adrian Prantlfb2398d2015-07-17 01:19:54 +00004200 const PCHContainerReader &PCHContainerRdr,
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004201 bool FindModuleFileExtensions,
Adrian Prantlbb165fb2015-06-20 18:53:08 +00004202 ASTReaderListener &Listener) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004203 // Open the AST file.
Richard Smith7f330cd2015-03-18 01:42:29 +00004204 // FIXME: This allows use of the VFS; we do not allow use of the
4205 // VFS when actually loading a module.
Benjamin Kramera8857962014-10-26 22:44:13 +00004206 auto Buffer = FileMgr.getBufferForFile(Filename);
Guy Benyei11169dd2012-12-18 14:30:41 +00004207 if (!Buffer) {
4208 return true;
4209 }
4210
4211 // Initialize the stream
4212 llvm::BitstreamReader StreamFile;
Adrian Prantlfb2398d2015-07-17 01:19:54 +00004213 PCHContainerRdr.ExtractPCH((*Buffer)->getMemBufferRef(), StreamFile);
Rafael Espindolaaf0e40a2014-11-12 14:42:25 +00004214 BitstreamCursor Stream(StreamFile);
Guy Benyei11169dd2012-12-18 14:30:41 +00004215
4216 // Sniff for the signature.
Ben Langmuir70a1b812015-03-24 04:43:52 +00004217 if (!startsWithASTFileMagic(Stream))
Guy Benyei11169dd2012-12-18 14:30:41 +00004218 return true;
Guy Benyei11169dd2012-12-18 14:30:41 +00004219
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004220 // Scan for the CONTROL_BLOCK_ID block.
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004221 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004222 return true;
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004223
4224 bool NeedsInputFiles = Listener.needsInputFileVisitation();
Ben Langmuircb69b572014-03-07 06:40:32 +00004225 bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
Richard Smithd4b230b2014-10-27 23:01:16 +00004226 bool NeedsImports = Listener.needsImportVisitation();
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004227 BitstreamCursor InputFilesCursor;
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004228
Guy Benyei11169dd2012-12-18 14:30:41 +00004229 RecordData Record;
Richard Smith7ed1bc92014-12-05 22:42:13 +00004230 std::string ModuleDir;
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004231 bool DoneWithControlBlock = false;
4232 while (!DoneWithControlBlock) {
Richard Smith0516b182015-09-08 19:40:14 +00004233 llvm::BitstreamEntry Entry = Stream.advance();
4234
4235 switch (Entry.Kind) {
4236 case llvm::BitstreamEntry::SubBlock: {
4237 switch (Entry.ID) {
4238 case OPTIONS_BLOCK_ID: {
4239 std::string IgnoredSuggestedPredefines;
4240 if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate,
4241 /*AllowCompatibleConfigurationMismatch*/ false,
4242 Listener, IgnoredSuggestedPredefines) != Success)
4243 return true;
4244 break;
4245 }
4246
4247 case INPUT_FILES_BLOCK_ID:
4248 InputFilesCursor = Stream;
4249 if (Stream.SkipBlock() ||
4250 (NeedsInputFiles &&
4251 ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID)))
4252 return true;
4253 break;
4254
4255 default:
4256 if (Stream.SkipBlock())
4257 return true;
4258 break;
4259 }
4260
4261 continue;
4262 }
4263
4264 case llvm::BitstreamEntry::EndBlock:
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004265 DoneWithControlBlock = true;
4266 break;
Richard Smith0516b182015-09-08 19:40:14 +00004267
4268 case llvm::BitstreamEntry::Error:
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004269 return true;
Richard Smith0516b182015-09-08 19:40:14 +00004270
4271 case llvm::BitstreamEntry::Record:
4272 break;
4273 }
4274
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004275 if (DoneWithControlBlock) break;
4276
Guy Benyei11169dd2012-12-18 14:30:41 +00004277 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00004278 StringRef Blob;
4279 unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob);
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004280 switch ((ControlRecordTypes)RecCode) {
4281 case METADATA: {
4282 if (Record[0] != VERSION_MAJOR)
4283 return true;
Guy Benyei11169dd2012-12-18 14:30:41 +00004284
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +00004285 if (Listener.ReadFullVersionInformation(Blob))
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004286 return true;
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +00004287
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004288 break;
4289 }
Ben Langmuir4f5212a2014-04-14 22:12:44 +00004290 case MODULE_NAME:
4291 Listener.ReadModuleName(Blob);
4292 break;
Richard Smith7ed1bc92014-12-05 22:42:13 +00004293 case MODULE_DIRECTORY:
4294 ModuleDir = Blob;
4295 break;
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00004296 case MODULE_MAP_FILE: {
4297 unsigned Idx = 0;
Richard Smith7ed1bc92014-12-05 22:42:13 +00004298 auto Path = ReadString(Record, Idx);
4299 ResolveImportedPath(Path, ModuleDir);
4300 Listener.ReadModuleMapFile(Path);
Ben Langmuir4f5212a2014-04-14 22:12:44 +00004301 break;
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00004302 }
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004303 case INPUT_FILE_OFFSETS: {
4304 if (!NeedsInputFiles)
4305 break;
4306
4307 unsigned NumInputFiles = Record[0];
4308 unsigned NumUserFiles = Record[1];
Richard Smithec216502015-02-13 19:48:37 +00004309 const uint64_t *InputFileOffs = (const uint64_t *)Blob.data();
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004310 for (unsigned I = 0; I != NumInputFiles; ++I) {
4311 // Go find this input file.
4312 bool isSystemFile = I >= NumUserFiles;
Ben Langmuircb69b572014-03-07 06:40:32 +00004313
4314 if (isSystemFile && !NeedsSystemInputFiles)
4315 break; // the rest are system input files
4316
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004317 BitstreamCursor &Cursor = InputFilesCursor;
4318 SavedStreamPosition SavedPosition(Cursor);
4319 Cursor.JumpToBit(InputFileOffs[I]);
4320
4321 unsigned Code = Cursor.ReadCode();
4322 RecordData Record;
4323 StringRef Blob;
4324 bool shouldContinue = false;
4325 switch ((InputFileRecordTypes)Cursor.readRecord(Code, Record, &Blob)) {
4326 case INPUT_FILE:
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +00004327 bool Overridden = static_cast<bool>(Record[3]);
Richard Smith7ed1bc92014-12-05 22:42:13 +00004328 std::string Filename = Blob;
4329 ResolveImportedPath(Filename, ModuleDir);
Richard Smith216a3bd2015-08-13 17:57:10 +00004330 shouldContinue = Listener.visitInputFile(
4331 Filename, isSystemFile, Overridden, /*IsExplicitModule*/false);
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004332 break;
4333 }
4334 if (!shouldContinue)
4335 break;
4336 }
4337 break;
4338 }
4339
Richard Smithd4b230b2014-10-27 23:01:16 +00004340 case IMPORTS: {
4341 if (!NeedsImports)
4342 break;
4343
4344 unsigned Idx = 0, N = Record.size();
4345 while (Idx < N) {
4346 // Read information about the AST file.
Richard Smith79c98cc2014-10-27 23:25:15 +00004347 Idx += 5; // ImportLoc, Size, ModTime, Signature
Richard Smith7ed1bc92014-12-05 22:42:13 +00004348 std::string Filename = ReadString(Record, Idx);
4349 ResolveImportedPath(Filename, ModuleDir);
4350 Listener.visitImport(Filename);
Richard Smithd4b230b2014-10-27 23:01:16 +00004351 }
4352 break;
4353 }
4354
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004355 default:
4356 // No other validation to perform.
4357 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004358 }
4359 }
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004360
4361 // Look for module file extension blocks, if requested.
4362 if (FindModuleFileExtensions) {
4363 while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) {
4364 bool DoneWithExtensionBlock = false;
4365 while (!DoneWithExtensionBlock) {
4366 llvm::BitstreamEntry Entry = Stream.advance();
4367
4368 switch (Entry.Kind) {
4369 case llvm::BitstreamEntry::SubBlock:
4370 if (Stream.SkipBlock())
4371 return true;
4372
4373 continue;
4374
4375 case llvm::BitstreamEntry::EndBlock:
4376 DoneWithExtensionBlock = true;
4377 continue;
4378
4379 case llvm::BitstreamEntry::Error:
4380 return true;
4381
4382 case llvm::BitstreamEntry::Record:
4383 break;
4384 }
4385
4386 Record.clear();
4387 StringRef Blob;
4388 unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob);
4389 switch (RecCode) {
4390 case EXTENSION_METADATA: {
4391 ModuleFileExtensionMetadata Metadata;
4392 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
4393 return true;
4394
4395 Listener.readModuleFileExtension(Metadata);
4396 break;
4397 }
4398 }
4399 }
4400 }
4401 }
4402
4403 return false;
Guy Benyei11169dd2012-12-18 14:30:41 +00004404}
4405
Adrian Prantlbb165fb2015-06-20 18:53:08 +00004406bool ASTReader::isAcceptableASTFile(
4407 StringRef Filename, FileManager &FileMgr,
Adrian Prantlfb2398d2015-07-17 01:19:54 +00004408 const PCHContainerReader &PCHContainerRdr, const LangOptions &LangOpts,
Adrian Prantlbb165fb2015-06-20 18:53:08 +00004409 const TargetOptions &TargetOpts, const PreprocessorOptions &PPOpts,
4410 std::string ExistingModuleCachePath) {
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00004411 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts,
4412 ExistingModuleCachePath, FileMgr);
Adrian Prantlfb2398d2015-07-17 01:19:54 +00004413 return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr,
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004414 /*FindModuleFileExtensions=*/false,
Adrian Prantlbb165fb2015-06-20 18:53:08 +00004415 validator);
Guy Benyei11169dd2012-12-18 14:30:41 +00004416}
4417
Ben Langmuir2c9af442014-04-10 17:57:43 +00004418ASTReader::ASTReadResult
4419ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004420 // Enter the submodule block.
4421 if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
4422 Error("malformed submodule block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00004423 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00004424 }
4425
4426 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
4427 bool First = true;
Craig Toppera13603a2014-05-22 05:54:18 +00004428 Module *CurrentModule = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00004429 RecordData Record;
4430 while (true) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004431 llvm::BitstreamEntry Entry = F.Stream.advanceSkippingSubblocks();
4432
4433 switch (Entry.Kind) {
4434 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
4435 case llvm::BitstreamEntry::Error:
4436 Error("malformed block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00004437 return Failure;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004438 case llvm::BitstreamEntry::EndBlock:
Ben Langmuir2c9af442014-04-10 17:57:43 +00004439 return Success;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004440 case llvm::BitstreamEntry::Record:
4441 // The interesting case.
4442 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004443 }
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004444
Guy Benyei11169dd2012-12-18 14:30:41 +00004445 // Read a record.
Chris Lattner0e6c9402013-01-20 02:38:54 +00004446 StringRef Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00004447 Record.clear();
Richard Smith03478d92014-10-23 22:12:14 +00004448 auto Kind = F.Stream.readRecord(Entry.ID, Record, &Blob);
4449
4450 if ((Kind == SUBMODULE_METADATA) != First) {
4451 Error("submodule metadata record should be at beginning of block");
4452 return Failure;
4453 }
4454 First = false;
4455
4456 // Submodule information is only valid if we have a current module.
4457 // FIXME: Should we error on these cases?
4458 if (!CurrentModule && Kind != SUBMODULE_METADATA &&
4459 Kind != SUBMODULE_DEFINITION)
4460 continue;
4461
4462 switch (Kind) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004463 default: // Default behavior: ignore.
4464 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004465
Richard Smith03478d92014-10-23 22:12:14 +00004466 case SUBMODULE_DEFINITION: {
Douglas Gregor8d932422013-03-20 03:59:18 +00004467 if (Record.size() < 8) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004468 Error("malformed module definition");
Ben Langmuir2c9af442014-04-10 17:57:43 +00004469 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00004470 }
Richard Smith03478d92014-10-23 22:12:14 +00004471
Chris Lattner0e6c9402013-01-20 02:38:54 +00004472 StringRef Name = Blob;
Richard Smith9bca2982014-03-08 00:03:56 +00004473 unsigned Idx = 0;
4474 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
4475 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
4476 bool IsFramework = Record[Idx++];
4477 bool IsExplicit = Record[Idx++];
4478 bool IsSystem = Record[Idx++];
4479 bool IsExternC = Record[Idx++];
4480 bool InferSubmodules = Record[Idx++];
4481 bool InferExplicitSubmodules = Record[Idx++];
4482 bool InferExportWildcard = Record[Idx++];
4483 bool ConfigMacrosExhaustive = Record[Idx++];
Douglas Gregor8d932422013-03-20 03:59:18 +00004484
Ben Langmuirbeee15e2014-04-14 18:00:01 +00004485 Module *ParentModule = nullptr;
Ben Langmuir9d6448b2014-08-09 00:57:23 +00004486 if (Parent)
Guy Benyei11169dd2012-12-18 14:30:41 +00004487 ParentModule = getSubmodule(Parent);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00004488
Guy Benyei11169dd2012-12-18 14:30:41 +00004489 // Retrieve this (sub)module from the module map, creating it if
4490 // necessary.
Ben Langmuir9d6448b2014-08-09 00:57:23 +00004491 CurrentModule = ModMap.findOrCreateModule(Name, ParentModule, IsFramework,
Guy Benyei11169dd2012-12-18 14:30:41 +00004492 IsExplicit).first;
Ben Langmuir9d6448b2014-08-09 00:57:23 +00004493
4494 // FIXME: set the definition loc for CurrentModule, or call
4495 // ModMap.setInferredModuleAllowedBy()
4496
Guy Benyei11169dd2012-12-18 14:30:41 +00004497 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
4498 if (GlobalIndex >= SubmodulesLoaded.size() ||
4499 SubmodulesLoaded[GlobalIndex]) {
4500 Error("too many submodules");
Ben Langmuir2c9af442014-04-10 17:57:43 +00004501 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00004502 }
Douglas Gregor8a114ab2013-02-06 22:40:31 +00004503
Douglas Gregor7029ce12013-03-19 00:28:20 +00004504 if (!ParentModule) {
4505 if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
4506 if (CurFile != F.File) {
4507 if (!Diags.isDiagnosticInFlight()) {
4508 Diag(diag::err_module_file_conflict)
4509 << CurrentModule->getTopLevelModuleName()
4510 << CurFile->getName()
4511 << F.File->getName();
4512 }
Ben Langmuir2c9af442014-04-10 17:57:43 +00004513 return Failure;
Douglas Gregor8a114ab2013-02-06 22:40:31 +00004514 }
Douglas Gregor8a114ab2013-02-06 22:40:31 +00004515 }
Douglas Gregor7029ce12013-03-19 00:28:20 +00004516
4517 CurrentModule->setASTFile(F.File);
Douglas Gregor8a114ab2013-02-06 22:40:31 +00004518 }
Ben Langmuirbeee15e2014-04-14 18:00:01 +00004519
Adrian Prantl15bcf702015-06-30 17:39:43 +00004520 CurrentModule->Signature = F.Signature;
Guy Benyei11169dd2012-12-18 14:30:41 +00004521 CurrentModule->IsFromModuleFile = true;
4522 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
Richard Smith9bca2982014-03-08 00:03:56 +00004523 CurrentModule->IsExternC = IsExternC;
Guy Benyei11169dd2012-12-18 14:30:41 +00004524 CurrentModule->InferSubmodules = InferSubmodules;
4525 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
4526 CurrentModule->InferExportWildcard = InferExportWildcard;
Douglas Gregor8d932422013-03-20 03:59:18 +00004527 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
Guy Benyei11169dd2012-12-18 14:30:41 +00004528 if (DeserializationListener)
4529 DeserializationListener->ModuleRead(GlobalID, CurrentModule);
4530
4531 SubmodulesLoaded[GlobalIndex] = CurrentModule;
Douglas Gregor6ddfca92013-01-14 17:21:00 +00004532
Richard Smith8a3e39a2016-03-28 21:31:09 +00004533 // Clear out data that will be replaced by what is in the module file.
Douglas Gregor6ddfca92013-01-14 17:21:00 +00004534 CurrentModule->LinkLibraries.clear();
Douglas Gregor8d932422013-03-20 03:59:18 +00004535 CurrentModule->ConfigMacros.clear();
Douglas Gregorfb912652013-03-20 21:10:35 +00004536 CurrentModule->UnresolvedConflicts.clear();
4537 CurrentModule->Conflicts.clear();
Richard Smith8a3e39a2016-03-28 21:31:09 +00004538
4539 // The module is available unless it's missing a requirement; relevant
4540 // requirements will be (re-)added by SUBMODULE_REQUIRES records.
4541 // Missing headers that were present when the module was built do not
4542 // make it unavailable -- if we got this far, this must be an explicitly
4543 // imported module file.
4544 CurrentModule->Requirements.clear();
4545 CurrentModule->MissingHeaders.clear();
4546 CurrentModule->IsMissingRequirement =
4547 ParentModule && ParentModule->IsMissingRequirement;
4548 CurrentModule->IsAvailable = !CurrentModule->IsMissingRequirement;
Guy Benyei11169dd2012-12-18 14:30:41 +00004549 break;
4550 }
Richard Smith8a3e39a2016-03-28 21:31:09 +00004551
Guy Benyei11169dd2012-12-18 14:30:41 +00004552 case SUBMODULE_UMBRELLA_HEADER: {
Richard Smith2b63d152015-05-16 02:28:53 +00004553 std::string Filename = Blob;
4554 ResolveImportedPath(F, Filename);
4555 if (auto *Umbrella = PP.getFileManager().getFile(Filename)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004556 if (!CurrentModule->getUmbrellaHeader())
Richard Smith2b63d152015-05-16 02:28:53 +00004557 ModMap.setUmbrellaHeader(CurrentModule, Umbrella, Blob);
4558 else if (CurrentModule->getUmbrellaHeader().Entry != Umbrella) {
Ben Langmuirbc35fbe2015-02-20 21:46:39 +00004559 // This can be a spurious difference caused by changing the VFS to
4560 // point to a different copy of the file, and it is too late to
4561 // to rebuild safely.
4562 // FIXME: If we wrote the virtual paths instead of the 'real' paths,
4563 // after input file validation only real problems would remain and we
4564 // could just error. For now, assume it's okay.
4565 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004566 }
4567 }
4568 break;
4569 }
4570
Richard Smith202210b2014-10-24 20:23:01 +00004571 case SUBMODULE_HEADER:
4572 case SUBMODULE_EXCLUDED_HEADER:
4573 case SUBMODULE_PRIVATE_HEADER:
4574 // We lazily associate headers with their modules via the HeaderInfo table.
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00004575 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
4576 // of complete filenames or remove it entirely.
Richard Smith202210b2014-10-24 20:23:01 +00004577 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004578
Richard Smith202210b2014-10-24 20:23:01 +00004579 case SUBMODULE_TEXTUAL_HEADER:
4580 case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
4581 // FIXME: Textual headers are not marked in the HeaderInfo table. Load
4582 // them here.
4583 break;
Lawrence Crowlb53e5482013-06-20 21:14:14 +00004584
Guy Benyei11169dd2012-12-18 14:30:41 +00004585 case SUBMODULE_TOPHEADER: {
Argyrios Kyrtzidis3c5305c2013-03-13 21:13:43 +00004586 CurrentModule->addTopHeaderFilename(Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +00004587 break;
4588 }
4589
4590 case SUBMODULE_UMBRELLA_DIR: {
Richard Smith2b63d152015-05-16 02:28:53 +00004591 std::string Dirname = Blob;
4592 ResolveImportedPath(F, Dirname);
4593 if (auto *Umbrella = PP.getFileManager().getDirectory(Dirname)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004594 if (!CurrentModule->getUmbrellaDir())
Richard Smith2b63d152015-05-16 02:28:53 +00004595 ModMap.setUmbrellaDir(CurrentModule, Umbrella, Blob);
4596 else if (CurrentModule->getUmbrellaDir().Entry != Umbrella) {
Ben Langmuir2c9af442014-04-10 17:57:43 +00004597 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
4598 Error("mismatched umbrella directories in submodule");
4599 return OutOfDate;
Guy Benyei11169dd2012-12-18 14:30:41 +00004600 }
4601 }
4602 break;
4603 }
4604
4605 case SUBMODULE_METADATA: {
Guy Benyei11169dd2012-12-18 14:30:41 +00004606 F.BaseSubmoduleID = getTotalNumSubmodules();
4607 F.LocalNumSubmodules = Record[0];
4608 unsigned LocalBaseSubmoduleID = Record[1];
4609 if (F.LocalNumSubmodules > 0) {
4610 // Introduce the global -> local mapping for submodules within this
4611 // module.
4612 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
4613
4614 // Introduce the local -> global mapping for submodules within this
4615 // module.
4616 F.SubmoduleRemap.insertOrReplace(
4617 std::make_pair(LocalBaseSubmoduleID,
4618 F.BaseSubmoduleID - LocalBaseSubmoduleID));
Ben Langmuirfe971d92014-08-16 04:54:18 +00004619
Ben Langmuir52ca6782014-10-20 16:27:32 +00004620 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
4621 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004622 break;
4623 }
4624
4625 case SUBMODULE_IMPORTS: {
Guy Benyei11169dd2012-12-18 14:30:41 +00004626 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
Douglas Gregorfb912652013-03-20 21:10:35 +00004627 UnresolvedModuleRef Unresolved;
Guy Benyei11169dd2012-12-18 14:30:41 +00004628 Unresolved.File = &F;
4629 Unresolved.Mod = CurrentModule;
4630 Unresolved.ID = Record[Idx];
Douglas Gregorfb912652013-03-20 21:10:35 +00004631 Unresolved.Kind = UnresolvedModuleRef::Import;
Guy Benyei11169dd2012-12-18 14:30:41 +00004632 Unresolved.IsWildcard = false;
Douglas Gregorfb912652013-03-20 21:10:35 +00004633 UnresolvedModuleRefs.push_back(Unresolved);
Guy Benyei11169dd2012-12-18 14:30:41 +00004634 }
4635 break;
4636 }
4637
4638 case SUBMODULE_EXPORTS: {
Guy Benyei11169dd2012-12-18 14:30:41 +00004639 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
Douglas Gregorfb912652013-03-20 21:10:35 +00004640 UnresolvedModuleRef Unresolved;
Guy Benyei11169dd2012-12-18 14:30:41 +00004641 Unresolved.File = &F;
4642 Unresolved.Mod = CurrentModule;
4643 Unresolved.ID = Record[Idx];
Douglas Gregorfb912652013-03-20 21:10:35 +00004644 Unresolved.Kind = UnresolvedModuleRef::Export;
Guy Benyei11169dd2012-12-18 14:30:41 +00004645 Unresolved.IsWildcard = Record[Idx + 1];
Douglas Gregorfb912652013-03-20 21:10:35 +00004646 UnresolvedModuleRefs.push_back(Unresolved);
Guy Benyei11169dd2012-12-18 14:30:41 +00004647 }
4648
4649 // Once we've loaded the set of exports, there's no reason to keep
4650 // the parsed, unresolved exports around.
4651 CurrentModule->UnresolvedExports.clear();
4652 break;
4653 }
4654 case SUBMODULE_REQUIRES: {
Richard Smitha3feee22013-10-28 22:18:19 +00004655 CurrentModule->addRequirement(Blob, Record[0], Context.getLangOpts(),
Guy Benyei11169dd2012-12-18 14:30:41 +00004656 Context.getTargetInfo());
4657 break;
4658 }
Douglas Gregor6ddfca92013-01-14 17:21:00 +00004659
4660 case SUBMODULE_LINK_LIBRARY:
Douglas Gregor6ddfca92013-01-14 17:21:00 +00004661 CurrentModule->LinkLibraries.push_back(
Chris Lattner0e6c9402013-01-20 02:38:54 +00004662 Module::LinkLibrary(Blob, Record[0]));
Douglas Gregor6ddfca92013-01-14 17:21:00 +00004663 break;
Douglas Gregor35b13ec2013-03-20 00:22:05 +00004664
4665 case SUBMODULE_CONFIG_MACRO:
Douglas Gregor35b13ec2013-03-20 00:22:05 +00004666 CurrentModule->ConfigMacros.push_back(Blob.str());
4667 break;
Douglas Gregorfb912652013-03-20 21:10:35 +00004668
4669 case SUBMODULE_CONFLICT: {
Douglas Gregorfb912652013-03-20 21:10:35 +00004670 UnresolvedModuleRef Unresolved;
4671 Unresolved.File = &F;
4672 Unresolved.Mod = CurrentModule;
4673 Unresolved.ID = Record[0];
4674 Unresolved.Kind = UnresolvedModuleRef::Conflict;
4675 Unresolved.IsWildcard = false;
4676 Unresolved.String = Blob;
4677 UnresolvedModuleRefs.push_back(Unresolved);
4678 break;
4679 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004680 }
4681 }
4682}
4683
4684/// \brief Parse the record that corresponds to a LangOptions data
4685/// structure.
4686///
4687/// This routine parses the language options from the AST file and then gives
4688/// them to the AST listener if one is set.
4689///
4690/// \returns true if the listener deems the file unacceptable, false otherwise.
4691bool ASTReader::ParseLanguageOptions(const RecordData &Record,
4692 bool Complain,
Richard Smith1e2cf0d2014-10-31 02:28:58 +00004693 ASTReaderListener &Listener,
4694 bool AllowCompatibleDifferences) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004695 LangOptions LangOpts;
4696 unsigned Idx = 0;
4697#define LANGOPT(Name, Bits, Default, Description) \
4698 LangOpts.Name = Record[Idx++];
4699#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
4700 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
4701#include "clang/Basic/LangOptions.def"
Alexey Samsonovedf99a92014-11-07 22:29:38 +00004702#define SANITIZER(NAME, ID) \
4703 LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
Will Dietzf54319c2013-01-18 11:30:38 +00004704#include "clang/Basic/Sanitizers.def"
Guy Benyei11169dd2012-12-18 14:30:41 +00004705
Ben Langmuircd98cb72015-06-23 18:20:18 +00004706 for (unsigned N = Record[Idx++]; N; --N)
4707 LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx));
4708
Guy Benyei11169dd2012-12-18 14:30:41 +00004709 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
4710 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
4711 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00004712
Ben Langmuird4a667a2015-06-23 18:20:23 +00004713 LangOpts.CurrentModule = ReadString(Record, Idx);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00004714
4715 // Comment options.
4716 for (unsigned N = Record[Idx++]; N; --N) {
4717 LangOpts.CommentOpts.BlockCommandNames.push_back(
4718 ReadString(Record, Idx));
4719 }
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00004720 LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00004721
Samuel Antaoee8fb302016-01-06 13:42:12 +00004722 // OpenMP offloading options.
4723 for (unsigned N = Record[Idx++]; N; --N) {
4724 LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx)));
4725 }
4726
4727 LangOpts.OMPHostIRFile = ReadString(Record, Idx);
4728
Richard Smith1e2cf0d2014-10-31 02:28:58 +00004729 return Listener.ReadLanguageOptions(LangOpts, Complain,
4730 AllowCompatibleDifferences);
Guy Benyei11169dd2012-12-18 14:30:41 +00004731}
4732
Chandler Carruth0d745bc2015-03-14 04:47:43 +00004733bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain,
4734 ASTReaderListener &Listener,
4735 bool AllowCompatibleDifferences) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004736 unsigned Idx = 0;
4737 TargetOptions TargetOpts;
4738 TargetOpts.Triple = ReadString(Record, Idx);
4739 TargetOpts.CPU = ReadString(Record, Idx);
4740 TargetOpts.ABI = ReadString(Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00004741 for (unsigned N = Record[Idx++]; N; --N) {
4742 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
4743 }
4744 for (unsigned N = Record[Idx++]; N; --N) {
4745 TargetOpts.Features.push_back(ReadString(Record, Idx));
4746 }
4747
Chandler Carruth0d745bc2015-03-14 04:47:43 +00004748 return Listener.ReadTargetOptions(TargetOpts, Complain,
4749 AllowCompatibleDifferences);
Guy Benyei11169dd2012-12-18 14:30:41 +00004750}
4751
4752bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
4753 ASTReaderListener &Listener) {
Ben Langmuirb92de022014-04-29 16:25:26 +00004754 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions);
Guy Benyei11169dd2012-12-18 14:30:41 +00004755 unsigned Idx = 0;
Ben Langmuirb92de022014-04-29 16:25:26 +00004756#define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00004757#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
Ben Langmuirb92de022014-04-29 16:25:26 +00004758 DiagOpts->set##Name(static_cast<Type>(Record[Idx++]));
Guy Benyei11169dd2012-12-18 14:30:41 +00004759#include "clang/Basic/DiagnosticOptions.def"
4760
Richard Smith3be1cb22014-08-07 00:24:21 +00004761 for (unsigned N = Record[Idx++]; N; --N)
Ben Langmuirb92de022014-04-29 16:25:26 +00004762 DiagOpts->Warnings.push_back(ReadString(Record, Idx));
Richard Smith3be1cb22014-08-07 00:24:21 +00004763 for (unsigned N = Record[Idx++]; N; --N)
4764 DiagOpts->Remarks.push_back(ReadString(Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00004765
4766 return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
4767}
4768
4769bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
4770 ASTReaderListener &Listener) {
4771 FileSystemOptions FSOpts;
4772 unsigned Idx = 0;
4773 FSOpts.WorkingDir = ReadString(Record, Idx);
4774 return Listener.ReadFileSystemOptions(FSOpts, Complain);
4775}
4776
4777bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
4778 bool Complain,
4779 ASTReaderListener &Listener) {
4780 HeaderSearchOptions HSOpts;
4781 unsigned Idx = 0;
4782 HSOpts.Sysroot = ReadString(Record, Idx);
4783
4784 // Include entries.
4785 for (unsigned N = Record[Idx++]; N; --N) {
4786 std::string Path = ReadString(Record, Idx);
4787 frontend::IncludeDirGroup Group
4788 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
Guy Benyei11169dd2012-12-18 14:30:41 +00004789 bool IsFramework = Record[Idx++];
4790 bool IgnoreSysRoot = Record[Idx++];
Benjamin Kramer3204b152015-05-29 19:42:19 +00004791 HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework,
4792 IgnoreSysRoot);
Guy Benyei11169dd2012-12-18 14:30:41 +00004793 }
4794
4795 // System header prefixes.
4796 for (unsigned N = Record[Idx++]; N; --N) {
4797 std::string Prefix = ReadString(Record, Idx);
4798 bool IsSystemHeader = Record[Idx++];
Benjamin Kramer3204b152015-05-29 19:42:19 +00004799 HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader);
Guy Benyei11169dd2012-12-18 14:30:41 +00004800 }
4801
4802 HSOpts.ResourceDir = ReadString(Record, Idx);
4803 HSOpts.ModuleCachePath = ReadString(Record, Idx);
Argyrios Kyrtzidis1594c152014-03-03 08:12:05 +00004804 HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00004805 HSOpts.DisableModuleHash = Record[Idx++];
4806 HSOpts.UseBuiltinIncludes = Record[Idx++];
4807 HSOpts.UseStandardSystemIncludes = Record[Idx++];
4808 HSOpts.UseStandardCXXIncludes = Record[Idx++];
4809 HSOpts.UseLibcxx = Record[Idx++];
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00004810 std::string SpecificModuleCachePath = ReadString(Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00004811
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00004812 return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
4813 Complain);
Guy Benyei11169dd2012-12-18 14:30:41 +00004814}
4815
4816bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
4817 bool Complain,
4818 ASTReaderListener &Listener,
4819 std::string &SuggestedPredefines) {
4820 PreprocessorOptions PPOpts;
4821 unsigned Idx = 0;
4822
4823 // Macro definitions/undefs
4824 for (unsigned N = Record[Idx++]; N; --N) {
4825 std::string Macro = ReadString(Record, Idx);
4826 bool IsUndef = Record[Idx++];
4827 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
4828 }
4829
4830 // Includes
4831 for (unsigned N = Record[Idx++]; N; --N) {
4832 PPOpts.Includes.push_back(ReadString(Record, Idx));
4833 }
4834
4835 // Macro Includes
4836 for (unsigned N = Record[Idx++]; N; --N) {
4837 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
4838 }
4839
4840 PPOpts.UsePredefines = Record[Idx++];
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00004841 PPOpts.DetailedRecord = Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00004842 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
4843 PPOpts.ImplicitPTHInclude = ReadString(Record, Idx);
4844 PPOpts.ObjCXXARCStandardLibrary =
4845 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
4846 SuggestedPredefines.clear();
4847 return Listener.ReadPreprocessorOptions(PPOpts, Complain,
4848 SuggestedPredefines);
4849}
4850
4851std::pair<ModuleFile *, unsigned>
4852ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
4853 GlobalPreprocessedEntityMapType::iterator
4854 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
4855 assert(I != GlobalPreprocessedEntityMap.end() &&
4856 "Corrupted global preprocessed entity map");
4857 ModuleFile *M = I->second;
4858 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
4859 return std::make_pair(M, LocalIndex);
4860}
4861
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00004862llvm::iterator_range<PreprocessingRecord::iterator>
Guy Benyei11169dd2012-12-18 14:30:41 +00004863ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
4864 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
4865 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
4866 Mod.NumPreprocessedEntities);
4867
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00004868 return llvm::make_range(PreprocessingRecord::iterator(),
4869 PreprocessingRecord::iterator());
Guy Benyei11169dd2012-12-18 14:30:41 +00004870}
4871
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00004872llvm::iterator_range<ASTReader::ModuleDeclIterator>
Guy Benyei11169dd2012-12-18 14:30:41 +00004873ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00004874 return llvm::make_range(
4875 ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
4876 ModuleDeclIterator(this, &Mod,
4877 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
Guy Benyei11169dd2012-12-18 14:30:41 +00004878}
4879
4880PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
4881 PreprocessedEntityID PPID = Index+1;
4882 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
4883 ModuleFile &M = *PPInfo.first;
4884 unsigned LocalIndex = PPInfo.second;
4885 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
4886
Guy Benyei11169dd2012-12-18 14:30:41 +00004887 if (!PP.getPreprocessingRecord()) {
4888 Error("no preprocessing record");
Craig Toppera13603a2014-05-22 05:54:18 +00004889 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00004890 }
4891
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004892 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
4893 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
4894
4895 llvm::BitstreamEntry Entry =
4896 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
4897 if (Entry.Kind != llvm::BitstreamEntry::Record)
Craig Toppera13603a2014-05-22 05:54:18 +00004898 return nullptr;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004899
Guy Benyei11169dd2012-12-18 14:30:41 +00004900 // Read the record.
Richard Smithcb34bd32016-03-27 07:28:06 +00004901 SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()),
4902 TranslateSourceLocation(M, PPOffs.getEnd()));
Guy Benyei11169dd2012-12-18 14:30:41 +00004903 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
Chris Lattner0e6c9402013-01-20 02:38:54 +00004904 StringRef Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00004905 RecordData Record;
4906 PreprocessorDetailRecordTypes RecType =
Chris Lattner0e6c9402013-01-20 02:38:54 +00004907 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.readRecord(
4908 Entry.ID, Record, &Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +00004909 switch (RecType) {
4910 case PPD_MACRO_EXPANSION: {
4911 bool isBuiltin = Record[0];
Craig Toppera13603a2014-05-22 05:54:18 +00004912 IdentifierInfo *Name = nullptr;
Richard Smith66a81862015-05-04 02:25:31 +00004913 MacroDefinitionRecord *Def = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00004914 if (isBuiltin)
4915 Name = getLocalIdentifier(M, Record[1]);
4916 else {
Richard Smith66a81862015-05-04 02:25:31 +00004917 PreprocessedEntityID GlobalID =
4918 getGlobalPreprocessedEntityID(M, Record[1]);
4919 Def = cast<MacroDefinitionRecord>(
4920 PPRec.getLoadedPreprocessedEntity(GlobalID - 1));
Guy Benyei11169dd2012-12-18 14:30:41 +00004921 }
4922
4923 MacroExpansion *ME;
4924 if (isBuiltin)
4925 ME = new (PPRec) MacroExpansion(Name, Range);
4926 else
4927 ME = new (PPRec) MacroExpansion(Def, Range);
4928
4929 return ME;
4930 }
4931
4932 case PPD_MACRO_DEFINITION: {
4933 // Decode the identifier info and then check again; if the macro is
4934 // still defined and associated with the identifier,
4935 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
Richard Smith66a81862015-05-04 02:25:31 +00004936 MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
Guy Benyei11169dd2012-12-18 14:30:41 +00004937
4938 if (DeserializationListener)
4939 DeserializationListener->MacroDefinitionRead(PPID, MD);
4940
4941 return MD;
4942 }
4943
4944 case PPD_INCLUSION_DIRECTIVE: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00004945 const char *FullFileNameStart = Blob.data() + Record[0];
4946 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
Craig Toppera13603a2014-05-22 05:54:18 +00004947 const FileEntry *File = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00004948 if (!FullFileName.empty())
4949 File = PP.getFileManager().getFile(FullFileName);
4950
4951 // FIXME: Stable encoding
4952 InclusionDirective::InclusionKind Kind
4953 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
4954 InclusionDirective *ID
4955 = new (PPRec) InclusionDirective(PPRec, Kind,
Chris Lattner0e6c9402013-01-20 02:38:54 +00004956 StringRef(Blob.data(), Record[0]),
Guy Benyei11169dd2012-12-18 14:30:41 +00004957 Record[1], Record[3],
4958 File,
4959 Range);
4960 return ID;
4961 }
4962 }
4963
4964 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
4965}
4966
4967/// \brief \arg SLocMapI points at a chunk of a module that contains no
4968/// preprocessed entities or the entities it contains are not the ones we are
4969/// looking for. Find the next module that contains entities and return the ID
4970/// of the first entry.
4971PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
4972 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
4973 ++SLocMapI;
4974 for (GlobalSLocOffsetMapType::const_iterator
4975 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
4976 ModuleFile &M = *SLocMapI->second;
4977 if (M.NumPreprocessedEntities)
4978 return M.BasePreprocessedEntityID;
4979 }
4980
4981 return getTotalNumPreprocessedEntities();
4982}
4983
4984namespace {
4985
Guy Benyei11169dd2012-12-18 14:30:41 +00004986struct PPEntityComp {
4987 const ASTReader &Reader;
4988 ModuleFile &M;
4989
4990 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) { }
4991
4992 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
4993 SourceLocation LHS = getLoc(L);
4994 SourceLocation RHS = getLoc(R);
4995 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4996 }
4997
4998 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
4999 SourceLocation LHS = getLoc(L);
5000 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5001 }
5002
5003 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
5004 SourceLocation RHS = getLoc(R);
5005 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5006 }
5007
5008 SourceLocation getLoc(const PPEntityOffset &PPE) const {
Richard Smithb22a1d12016-03-27 20:13:24 +00005009 return Reader.TranslateSourceLocation(M, PPE.getBegin());
Guy Benyei11169dd2012-12-18 14:30:41 +00005010 }
5011};
5012
Alexander Kornienkoab9db512015-06-22 23:07:51 +00005013}
Guy Benyei11169dd2012-12-18 14:30:41 +00005014
Alp Toker2e9ce4c2014-05-16 18:59:21 +00005015PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
5016 bool EndsAfter) const {
5017 if (SourceMgr.isLocalSourceLocation(Loc))
Guy Benyei11169dd2012-12-18 14:30:41 +00005018 return getTotalNumPreprocessedEntities();
5019
Alp Toker2e9ce4c2014-05-16 18:59:21 +00005020 GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
5021 SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
Guy Benyei11169dd2012-12-18 14:30:41 +00005022 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
5023 "Corrupted global sloc offset map");
5024
5025 if (SLocMapI->second->NumPreprocessedEntities == 0)
5026 return findNextPreprocessedEntity(SLocMapI);
5027
5028 ModuleFile &M = *SLocMapI->second;
5029 typedef const PPEntityOffset *pp_iterator;
5030 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
5031 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
5032
5033 size_t Count = M.NumPreprocessedEntities;
5034 size_t Half;
5035 pp_iterator First = pp_begin;
5036 pp_iterator PPI;
5037
Alp Toker2e9ce4c2014-05-16 18:59:21 +00005038 if (EndsAfter) {
5039 PPI = std::upper_bound(pp_begin, pp_end, Loc,
Richard Smithb22a1d12016-03-27 20:13:24 +00005040 PPEntityComp(*this, M));
Alp Toker2e9ce4c2014-05-16 18:59:21 +00005041 } else {
5042 // Do a binary search manually instead of using std::lower_bound because
5043 // The end locations of entities may be unordered (when a macro expansion
5044 // is inside another macro argument), but for this case it is not important
5045 // whether we get the first macro expansion or its containing macro.
5046 while (Count > 0) {
5047 Half = Count / 2;
5048 PPI = First;
5049 std::advance(PPI, Half);
Richard Smithb22a1d12016-03-27 20:13:24 +00005050 if (SourceMgr.isBeforeInTranslationUnit(
5051 TranslateSourceLocation(M, PPI->getEnd()), Loc)) {
Alp Toker2e9ce4c2014-05-16 18:59:21 +00005052 First = PPI;
5053 ++First;
5054 Count = Count - Half - 1;
5055 } else
5056 Count = Half;
5057 }
Guy Benyei11169dd2012-12-18 14:30:41 +00005058 }
5059
5060 if (PPI == pp_end)
5061 return findNextPreprocessedEntity(SLocMapI);
5062
5063 return M.BasePreprocessedEntityID + (PPI - pp_begin);
5064}
5065
Guy Benyei11169dd2012-12-18 14:30:41 +00005066/// \brief Returns a pair of [Begin, End) indices of preallocated
5067/// preprocessed entities that \arg Range encompasses.
5068std::pair<unsigned, unsigned>
5069 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
5070 if (Range.isInvalid())
5071 return std::make_pair(0,0);
5072 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
5073
Alp Toker2e9ce4c2014-05-16 18:59:21 +00005074 PreprocessedEntityID BeginID =
5075 findPreprocessedEntity(Range.getBegin(), false);
5076 PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true);
Guy Benyei11169dd2012-12-18 14:30:41 +00005077 return std::make_pair(BeginID, EndID);
5078}
5079
5080/// \brief Optionally returns true or false if the preallocated preprocessed
5081/// entity with index \arg Index came from file \arg FID.
David Blaikie05785d12013-02-20 22:23:23 +00005082Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
Guy Benyei11169dd2012-12-18 14:30:41 +00005083 FileID FID) {
5084 if (FID.isInvalid())
5085 return false;
5086
5087 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
5088 ModuleFile &M = *PPInfo.first;
5089 unsigned LocalIndex = PPInfo.second;
5090 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
5091
Richard Smithcb34bd32016-03-27 07:28:06 +00005092 SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin());
Guy Benyei11169dd2012-12-18 14:30:41 +00005093 if (Loc.isInvalid())
5094 return false;
5095
5096 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
5097 return true;
5098 else
5099 return false;
5100}
5101
5102namespace {
5103 /// \brief Visitor used to search for information about a header file.
5104 class HeaderFileInfoVisitor {
Guy Benyei11169dd2012-12-18 14:30:41 +00005105 const FileEntry *FE;
5106
David Blaikie05785d12013-02-20 22:23:23 +00005107 Optional<HeaderFileInfo> HFI;
Guy Benyei11169dd2012-12-18 14:30:41 +00005108
5109 public:
Argyrios Kyrtzidis61a38962013-03-06 18:12:44 +00005110 explicit HeaderFileInfoVisitor(const FileEntry *FE)
5111 : FE(FE) { }
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00005112
5113 bool operator()(ModuleFile &M) {
Guy Benyei11169dd2012-12-18 14:30:41 +00005114 HeaderFileInfoLookupTable *Table
5115 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
5116 if (!Table)
5117 return false;
5118
5119 // Look in the on-disk hash table for an entry for this file name.
Richard Smithbdf2d932015-07-30 03:37:16 +00005120 HeaderFileInfoLookupTable::iterator Pos = Table->find(FE);
Guy Benyei11169dd2012-12-18 14:30:41 +00005121 if (Pos == Table->end())
5122 return false;
5123
Richard Smithbdf2d932015-07-30 03:37:16 +00005124 HFI = *Pos;
Guy Benyei11169dd2012-12-18 14:30:41 +00005125 return true;
5126 }
5127
David Blaikie05785d12013-02-20 22:23:23 +00005128 Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
Guy Benyei11169dd2012-12-18 14:30:41 +00005129 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +00005130}
Guy Benyei11169dd2012-12-18 14:30:41 +00005131
5132HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
Argyrios Kyrtzidis61a38962013-03-06 18:12:44 +00005133 HeaderFileInfoVisitor Visitor(FE);
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00005134 ModuleMgr.visit(Visitor);
Argyrios Kyrtzidis1054bbf2013-05-08 23:46:55 +00005135 if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
Guy Benyei11169dd2012-12-18 14:30:41 +00005136 return *HFI;
Guy Benyei11169dd2012-12-18 14:30:41 +00005137
5138 return HeaderFileInfo();
5139}
5140
5141void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
5142 // FIXME: Make it work properly with modules.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00005143 SmallVector<DiagnosticsEngine::DiagState *, 32> DiagStates;
Guy Benyei11169dd2012-12-18 14:30:41 +00005144 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
5145 ModuleFile &F = *(*I);
5146 unsigned Idx = 0;
5147 DiagStates.clear();
5148 assert(!Diag.DiagStates.empty());
5149 DiagStates.push_back(&Diag.DiagStates.front()); // the command-line one.
5150 while (Idx < F.PragmaDiagMappings.size()) {
5151 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
5152 unsigned DiagStateID = F.PragmaDiagMappings[Idx++];
5153 if (DiagStateID != 0) {
5154 Diag.DiagStatePoints.push_back(
5155 DiagnosticsEngine::DiagStatePoint(DiagStates[DiagStateID-1],
5156 FullSourceLoc(Loc, SourceMgr)));
5157 continue;
5158 }
5159
5160 assert(DiagStateID == 0);
5161 // A new DiagState was created here.
5162 Diag.DiagStates.push_back(*Diag.GetCurDiagState());
5163 DiagnosticsEngine::DiagState *NewState = &Diag.DiagStates.back();
5164 DiagStates.push_back(NewState);
5165 Diag.DiagStatePoints.push_back(
5166 DiagnosticsEngine::DiagStatePoint(NewState,
5167 FullSourceLoc(Loc, SourceMgr)));
5168 while (1) {
5169 assert(Idx < F.PragmaDiagMappings.size() &&
5170 "Invalid data, didn't find '-1' marking end of diag/map pairs");
5171 if (Idx >= F.PragmaDiagMappings.size()) {
5172 break; // Something is messed up but at least avoid infinite loop in
5173 // release build.
5174 }
5175 unsigned DiagID = F.PragmaDiagMappings[Idx++];
5176 if (DiagID == (unsigned)-1) {
5177 break; // no more diag/map pairs for this location.
5178 }
Alp Tokerc726c362014-06-10 09:31:37 +00005179 diag::Severity Map = (diag::Severity)F.PragmaDiagMappings[Idx++];
5180 DiagnosticMapping Mapping = Diag.makeUserMapping(Map, Loc);
5181 Diag.GetCurDiagState()->setMapping(DiagID, Mapping);
Guy Benyei11169dd2012-12-18 14:30:41 +00005182 }
5183 }
5184 }
5185}
5186
5187/// \brief Get the correct cursor and offset for loading a type.
5188ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
5189 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
5190 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
5191 ModuleFile *M = I->second;
5192 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
5193}
5194
5195/// \brief Read and return the type with the given index..
5196///
5197/// The index is the type ID, shifted and minus the number of predefs. This
5198/// routine actually reads the record corresponding to the type at the given
5199/// location. It is a helper routine for GetType, which deals with reading type
5200/// IDs.
5201QualType ASTReader::readTypeRecord(unsigned Index) {
5202 RecordLocation Loc = TypeCursorForIndex(Index);
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005203 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00005204
5205 // Keep track of where we are in the stream, then jump back there
5206 // after reading this type.
5207 SavedStreamPosition SavedPosition(DeclsCursor);
5208
5209 ReadingKindTracker ReadingKind(Read_Type, *this);
5210
5211 // Note that we are loading a type record.
5212 Deserializing AType(this);
5213
5214 unsigned Idx = 0;
5215 DeclsCursor.JumpToBit(Loc.Offset);
5216 RecordData Record;
5217 unsigned Code = DeclsCursor.ReadCode();
Chris Lattner0e6c9402013-01-20 02:38:54 +00005218 switch ((TypeCode)DeclsCursor.readRecord(Code, Record)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00005219 case TYPE_EXT_QUAL: {
5220 if (Record.size() != 2) {
5221 Error("Incorrect encoding of extended qualifier type");
5222 return QualType();
5223 }
5224 QualType Base = readType(*Loc.F, Record, Idx);
5225 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
5226 return Context.getQualifiedType(Base, Quals);
5227 }
5228
5229 case TYPE_COMPLEX: {
5230 if (Record.size() != 1) {
5231 Error("Incorrect encoding of complex type");
5232 return QualType();
5233 }
5234 QualType ElemType = readType(*Loc.F, Record, Idx);
5235 return Context.getComplexType(ElemType);
5236 }
5237
5238 case TYPE_POINTER: {
5239 if (Record.size() != 1) {
5240 Error("Incorrect encoding of pointer type");
5241 return QualType();
5242 }
5243 QualType PointeeType = readType(*Loc.F, Record, Idx);
5244 return Context.getPointerType(PointeeType);
5245 }
5246
Reid Kleckner8a365022013-06-24 17:51:48 +00005247 case TYPE_DECAYED: {
5248 if (Record.size() != 1) {
5249 Error("Incorrect encoding of decayed type");
5250 return QualType();
5251 }
5252 QualType OriginalType = readType(*Loc.F, Record, Idx);
5253 QualType DT = Context.getAdjustedParameterType(OriginalType);
5254 if (!isa<DecayedType>(DT))
5255 Error("Decayed type does not decay");
5256 return DT;
5257 }
5258
Reid Kleckner0503a872013-12-05 01:23:43 +00005259 case TYPE_ADJUSTED: {
5260 if (Record.size() != 2) {
5261 Error("Incorrect encoding of adjusted type");
5262 return QualType();
5263 }
5264 QualType OriginalTy = readType(*Loc.F, Record, Idx);
5265 QualType AdjustedTy = readType(*Loc.F, Record, Idx);
5266 return Context.getAdjustedType(OriginalTy, AdjustedTy);
5267 }
5268
Guy Benyei11169dd2012-12-18 14:30:41 +00005269 case TYPE_BLOCK_POINTER: {
5270 if (Record.size() != 1) {
5271 Error("Incorrect encoding of block pointer type");
5272 return QualType();
5273 }
5274 QualType PointeeType = readType(*Loc.F, Record, Idx);
5275 return Context.getBlockPointerType(PointeeType);
5276 }
5277
5278 case TYPE_LVALUE_REFERENCE: {
5279 if (Record.size() != 2) {
5280 Error("Incorrect encoding of lvalue reference type");
5281 return QualType();
5282 }
5283 QualType PointeeType = readType(*Loc.F, Record, Idx);
5284 return Context.getLValueReferenceType(PointeeType, Record[1]);
5285 }
5286
5287 case TYPE_RVALUE_REFERENCE: {
5288 if (Record.size() != 1) {
5289 Error("Incorrect encoding of rvalue reference type");
5290 return QualType();
5291 }
5292 QualType PointeeType = readType(*Loc.F, Record, Idx);
5293 return Context.getRValueReferenceType(PointeeType);
5294 }
5295
5296 case TYPE_MEMBER_POINTER: {
5297 if (Record.size() != 2) {
5298 Error("Incorrect encoding of member pointer type");
5299 return QualType();
5300 }
5301 QualType PointeeType = readType(*Loc.F, Record, Idx);
5302 QualType ClassType = readType(*Loc.F, Record, Idx);
5303 if (PointeeType.isNull() || ClassType.isNull())
5304 return QualType();
5305
5306 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
5307 }
5308
5309 case TYPE_CONSTANT_ARRAY: {
5310 QualType ElementType = readType(*Loc.F, Record, Idx);
5311 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
5312 unsigned IndexTypeQuals = Record[2];
5313 unsigned Idx = 3;
5314 llvm::APInt Size = ReadAPInt(Record, Idx);
5315 return Context.getConstantArrayType(ElementType, Size,
5316 ASM, IndexTypeQuals);
5317 }
5318
5319 case TYPE_INCOMPLETE_ARRAY: {
5320 QualType ElementType = readType(*Loc.F, Record, Idx);
5321 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
5322 unsigned IndexTypeQuals = Record[2];
5323 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
5324 }
5325
5326 case TYPE_VARIABLE_ARRAY: {
5327 QualType ElementType = readType(*Loc.F, Record, Idx);
5328 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
5329 unsigned IndexTypeQuals = Record[2];
5330 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
5331 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
5332 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
5333 ASM, IndexTypeQuals,
5334 SourceRange(LBLoc, RBLoc));
5335 }
5336
5337 case TYPE_VECTOR: {
5338 if (Record.size() != 3) {
5339 Error("incorrect encoding of vector type in AST file");
5340 return QualType();
5341 }
5342
5343 QualType ElementType = readType(*Loc.F, Record, Idx);
5344 unsigned NumElements = Record[1];
5345 unsigned VecKind = Record[2];
5346 return Context.getVectorType(ElementType, NumElements,
5347 (VectorType::VectorKind)VecKind);
5348 }
5349
5350 case TYPE_EXT_VECTOR: {
5351 if (Record.size() != 3) {
5352 Error("incorrect encoding of extended vector type in AST file");
5353 return QualType();
5354 }
5355
5356 QualType ElementType = readType(*Loc.F, Record, Idx);
5357 unsigned NumElements = Record[1];
5358 return Context.getExtVectorType(ElementType, NumElements);
5359 }
5360
5361 case TYPE_FUNCTION_NO_PROTO: {
5362 if (Record.size() != 6) {
5363 Error("incorrect encoding of no-proto function type");
5364 return QualType();
5365 }
5366 QualType ResultType = readType(*Loc.F, Record, Idx);
5367 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
5368 (CallingConv)Record[4], Record[5]);
5369 return Context.getFunctionNoProtoType(ResultType, Info);
5370 }
5371
5372 case TYPE_FUNCTION_PROTO: {
5373 QualType ResultType = readType(*Loc.F, Record, Idx);
5374
5375 FunctionProtoType::ExtProtoInfo EPI;
5376 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
5377 /*hasregparm*/ Record[2],
5378 /*regparm*/ Record[3],
5379 static_cast<CallingConv>(Record[4]),
5380 /*produces*/ Record[5]);
5381
5382 unsigned Idx = 6;
Guy Benyei11169dd2012-12-18 14:30:41 +00005383
5384 EPI.Variadic = Record[Idx++];
5385 EPI.HasTrailingReturn = Record[Idx++];
5386 EPI.TypeQuals = Record[Idx++];
5387 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
Richard Smith564417a2014-03-20 21:47:22 +00005388 SmallVector<QualType, 8> ExceptionStorage;
Richard Smith8acb4282014-07-31 21:57:55 +00005389 readExceptionSpec(*Loc.F, ExceptionStorage, EPI.ExceptionSpec, Record, Idx);
Richard Smith01b2cb42014-07-26 06:37:51 +00005390
5391 unsigned NumParams = Record[Idx++];
5392 SmallVector<QualType, 16> ParamTypes;
5393 for (unsigned I = 0; I != NumParams; ++I)
5394 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
5395
John McCall18afab72016-03-01 00:49:02 +00005396 SmallVector<FunctionProtoType::ExtParameterInfo, 4> ExtParameterInfos;
5397 if (Idx != Record.size()) {
5398 for (unsigned I = 0; I != NumParams; ++I)
5399 ExtParameterInfos.push_back(
5400 FunctionProtoType::ExtParameterInfo
5401 ::getFromOpaqueValue(Record[Idx++]));
5402 EPI.ExtParameterInfos = ExtParameterInfos.data();
5403 }
5404
5405 assert(Idx == Record.size());
5406
Jordan Rose5c382722013-03-08 21:51:21 +00005407 return Context.getFunctionType(ResultType, ParamTypes, EPI);
Guy Benyei11169dd2012-12-18 14:30:41 +00005408 }
5409
5410 case TYPE_UNRESOLVED_USING: {
5411 unsigned Idx = 0;
5412 return Context.getTypeDeclType(
5413 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
5414 }
5415
5416 case TYPE_TYPEDEF: {
5417 if (Record.size() != 2) {
5418 Error("incorrect encoding of typedef type");
5419 return QualType();
5420 }
5421 unsigned Idx = 0;
5422 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
5423 QualType Canonical = readType(*Loc.F, Record, Idx);
5424 if (!Canonical.isNull())
5425 Canonical = Context.getCanonicalType(Canonical);
5426 return Context.getTypedefType(Decl, Canonical);
5427 }
5428
5429 case TYPE_TYPEOF_EXPR:
5430 return Context.getTypeOfExprType(ReadExpr(*Loc.F));
5431
5432 case TYPE_TYPEOF: {
5433 if (Record.size() != 1) {
5434 Error("incorrect encoding of typeof(type) in AST file");
5435 return QualType();
5436 }
5437 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
5438 return Context.getTypeOfType(UnderlyingType);
5439 }
5440
5441 case TYPE_DECLTYPE: {
5442 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
5443 return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
5444 }
5445
5446 case TYPE_UNARY_TRANSFORM: {
5447 QualType BaseType = readType(*Loc.F, Record, Idx);
5448 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
5449 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
5450 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
5451 }
5452
Richard Smith74aeef52013-04-26 16:15:35 +00005453 case TYPE_AUTO: {
5454 QualType Deduced = readType(*Loc.F, Record, Idx);
Richard Smithe301ba22015-11-11 02:02:15 +00005455 AutoTypeKeyword Keyword = (AutoTypeKeyword)Record[Idx++];
Richard Smith27d807c2013-04-30 13:56:41 +00005456 bool IsDependent = Deduced.isNull() ? Record[Idx++] : false;
Richard Smithe301ba22015-11-11 02:02:15 +00005457 return Context.getAutoType(Deduced, Keyword, IsDependent);
Richard Smith74aeef52013-04-26 16:15:35 +00005458 }
Guy Benyei11169dd2012-12-18 14:30:41 +00005459
5460 case TYPE_RECORD: {
5461 if (Record.size() != 2) {
5462 Error("incorrect encoding of record type");
5463 return QualType();
5464 }
5465 unsigned Idx = 0;
5466 bool IsDependent = Record[Idx++];
5467 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
5468 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
5469 QualType T = Context.getRecordType(RD);
5470 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
5471 return T;
5472 }
5473
5474 case TYPE_ENUM: {
5475 if (Record.size() != 2) {
5476 Error("incorrect encoding of enum type");
5477 return QualType();
5478 }
5479 unsigned Idx = 0;
5480 bool IsDependent = Record[Idx++];
5481 QualType T
5482 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
5483 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
5484 return T;
5485 }
5486
5487 case TYPE_ATTRIBUTED: {
5488 if (Record.size() != 3) {
5489 Error("incorrect encoding of attributed type");
5490 return QualType();
5491 }
5492 QualType modifiedType = readType(*Loc.F, Record, Idx);
5493 QualType equivalentType = readType(*Loc.F, Record, Idx);
5494 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
5495 return Context.getAttributedType(kind, modifiedType, equivalentType);
5496 }
5497
5498 case TYPE_PAREN: {
5499 if (Record.size() != 1) {
5500 Error("incorrect encoding of paren type");
5501 return QualType();
5502 }
5503 QualType InnerType = readType(*Loc.F, Record, Idx);
5504 return Context.getParenType(InnerType);
5505 }
5506
5507 case TYPE_PACK_EXPANSION: {
5508 if (Record.size() != 2) {
5509 Error("incorrect encoding of pack expansion type");
5510 return QualType();
5511 }
5512 QualType Pattern = readType(*Loc.F, Record, Idx);
5513 if (Pattern.isNull())
5514 return QualType();
David Blaikie05785d12013-02-20 22:23:23 +00005515 Optional<unsigned> NumExpansions;
Guy Benyei11169dd2012-12-18 14:30:41 +00005516 if (Record[1])
5517 NumExpansions = Record[1] - 1;
5518 return Context.getPackExpansionType(Pattern, NumExpansions);
5519 }
5520
5521 case TYPE_ELABORATED: {
5522 unsigned Idx = 0;
5523 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
5524 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
5525 QualType NamedType = readType(*Loc.F, Record, Idx);
5526 return Context.getElaboratedType(Keyword, NNS, NamedType);
5527 }
5528
5529 case TYPE_OBJC_INTERFACE: {
5530 unsigned Idx = 0;
5531 ObjCInterfaceDecl *ItfD
5532 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
5533 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
5534 }
5535
5536 case TYPE_OBJC_OBJECT: {
5537 unsigned Idx = 0;
5538 QualType Base = readType(*Loc.F, Record, Idx);
Douglas Gregore9d95f12015-07-07 03:57:35 +00005539 unsigned NumTypeArgs = Record[Idx++];
5540 SmallVector<QualType, 4> TypeArgs;
5541 for (unsigned I = 0; I != NumTypeArgs; ++I)
5542 TypeArgs.push_back(readType(*Loc.F, Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00005543 unsigned NumProtos = Record[Idx++];
5544 SmallVector<ObjCProtocolDecl*, 4> Protos;
5545 for (unsigned I = 0; I != NumProtos; ++I)
5546 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
Douglas Gregorab209d82015-07-07 03:58:42 +00005547 bool IsKindOf = Record[Idx++];
5548 return Context.getObjCObjectType(Base, TypeArgs, Protos, IsKindOf);
Guy Benyei11169dd2012-12-18 14:30:41 +00005549 }
5550
5551 case TYPE_OBJC_OBJECT_POINTER: {
5552 unsigned Idx = 0;
5553 QualType Pointee = readType(*Loc.F, Record, Idx);
5554 return Context.getObjCObjectPointerType(Pointee);
5555 }
5556
5557 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
5558 unsigned Idx = 0;
5559 QualType Parm = readType(*Loc.F, Record, Idx);
5560 QualType Replacement = readType(*Loc.F, Record, Idx);
Stephan Tolksdorfe96f8b32014-03-15 10:23:27 +00005561 return Context.getSubstTemplateTypeParmType(
5562 cast<TemplateTypeParmType>(Parm),
5563 Context.getCanonicalType(Replacement));
Guy Benyei11169dd2012-12-18 14:30:41 +00005564 }
5565
5566 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
5567 unsigned Idx = 0;
5568 QualType Parm = readType(*Loc.F, Record, Idx);
5569 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
5570 return Context.getSubstTemplateTypeParmPackType(
5571 cast<TemplateTypeParmType>(Parm),
5572 ArgPack);
5573 }
5574
5575 case TYPE_INJECTED_CLASS_NAME: {
5576 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
5577 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
5578 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
5579 // for AST reading, too much interdependencies.
Richard Smith6377f8f2014-10-21 21:15:18 +00005580 const Type *T = nullptr;
5581 for (auto *DI = D; DI; DI = DI->getPreviousDecl()) {
5582 if (const Type *Existing = DI->getTypeForDecl()) {
5583 T = Existing;
5584 break;
5585 }
5586 }
5587 if (!T) {
Richard Smithf17fdbd2014-04-24 02:25:27 +00005588 T = new (Context, TypeAlignment) InjectedClassNameType(D, TST);
Richard Smith6377f8f2014-10-21 21:15:18 +00005589 for (auto *DI = D; DI; DI = DI->getPreviousDecl())
5590 DI->setTypeForDecl(T);
5591 }
Richard Smithf17fdbd2014-04-24 02:25:27 +00005592 return QualType(T, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00005593 }
5594
5595 case TYPE_TEMPLATE_TYPE_PARM: {
5596 unsigned Idx = 0;
5597 unsigned Depth = Record[Idx++];
5598 unsigned Index = Record[Idx++];
5599 bool Pack = Record[Idx++];
5600 TemplateTypeParmDecl *D
5601 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
5602 return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
5603 }
5604
5605 case TYPE_DEPENDENT_NAME: {
5606 unsigned Idx = 0;
5607 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
5608 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Richard Smithbdf2d932015-07-30 03:37:16 +00005609 const IdentifierInfo *Name = GetIdentifierInfo(*Loc.F, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00005610 QualType Canon = readType(*Loc.F, Record, Idx);
5611 if (!Canon.isNull())
5612 Canon = Context.getCanonicalType(Canon);
5613 return Context.getDependentNameType(Keyword, NNS, Name, Canon);
5614 }
5615
5616 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
5617 unsigned Idx = 0;
5618 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
5619 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Richard Smithbdf2d932015-07-30 03:37:16 +00005620 const IdentifierInfo *Name = GetIdentifierInfo(*Loc.F, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00005621 unsigned NumArgs = Record[Idx++];
5622 SmallVector<TemplateArgument, 8> Args;
5623 Args.reserve(NumArgs);
5624 while (NumArgs--)
5625 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
5626 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
5627 Args.size(), Args.data());
5628 }
5629
5630 case TYPE_DEPENDENT_SIZED_ARRAY: {
5631 unsigned Idx = 0;
5632
5633 // ArrayType
5634 QualType ElementType = readType(*Loc.F, Record, Idx);
5635 ArrayType::ArraySizeModifier ASM
5636 = (ArrayType::ArraySizeModifier)Record[Idx++];
5637 unsigned IndexTypeQuals = Record[Idx++];
5638
5639 // DependentSizedArrayType
5640 Expr *NumElts = ReadExpr(*Loc.F);
5641 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
5642
5643 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
5644 IndexTypeQuals, Brackets);
5645 }
5646
5647 case TYPE_TEMPLATE_SPECIALIZATION: {
5648 unsigned Idx = 0;
5649 bool IsDependent = Record[Idx++];
5650 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
5651 SmallVector<TemplateArgument, 8> Args;
5652 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
5653 QualType Underlying = readType(*Loc.F, Record, Idx);
5654 QualType T;
5655 if (Underlying.isNull())
5656 T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(),
5657 Args.size());
5658 else
5659 T = Context.getTemplateSpecializationType(Name, Args.data(),
5660 Args.size(), Underlying);
5661 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
5662 return T;
5663 }
5664
5665 case TYPE_ATOMIC: {
5666 if (Record.size() != 1) {
5667 Error("Incorrect encoding of atomic type");
5668 return QualType();
5669 }
5670 QualType ValueType = readType(*Loc.F, Record, Idx);
5671 return Context.getAtomicType(ValueType);
5672 }
Xiuli Pan9c14e282016-01-09 12:53:17 +00005673
5674 case TYPE_PIPE: {
5675 if (Record.size() != 1) {
5676 Error("Incorrect encoding of pipe type");
5677 return QualType();
5678 }
5679
5680 // Reading the pipe element type.
5681 QualType ElementType = readType(*Loc.F, Record, Idx);
5682 return Context.getPipeType(ElementType);
5683 }
Guy Benyei11169dd2012-12-18 14:30:41 +00005684 }
5685 llvm_unreachable("Invalid TypeCode!");
5686}
5687
Richard Smith564417a2014-03-20 21:47:22 +00005688void ASTReader::readExceptionSpec(ModuleFile &ModuleFile,
5689 SmallVectorImpl<QualType> &Exceptions,
Richard Smith8acb4282014-07-31 21:57:55 +00005690 FunctionProtoType::ExceptionSpecInfo &ESI,
Richard Smith564417a2014-03-20 21:47:22 +00005691 const RecordData &Record, unsigned &Idx) {
5692 ExceptionSpecificationType EST =
5693 static_cast<ExceptionSpecificationType>(Record[Idx++]);
Richard Smith8acb4282014-07-31 21:57:55 +00005694 ESI.Type = EST;
Richard Smith564417a2014-03-20 21:47:22 +00005695 if (EST == EST_Dynamic) {
Richard Smith8acb4282014-07-31 21:57:55 +00005696 for (unsigned I = 0, N = Record[Idx++]; I != N; ++I)
Richard Smith564417a2014-03-20 21:47:22 +00005697 Exceptions.push_back(readType(ModuleFile, Record, Idx));
Richard Smith8acb4282014-07-31 21:57:55 +00005698 ESI.Exceptions = Exceptions;
Richard Smith564417a2014-03-20 21:47:22 +00005699 } else if (EST == EST_ComputedNoexcept) {
Richard Smith8acb4282014-07-31 21:57:55 +00005700 ESI.NoexceptExpr = ReadExpr(ModuleFile);
Richard Smith564417a2014-03-20 21:47:22 +00005701 } else if (EST == EST_Uninstantiated) {
Richard Smith8acb4282014-07-31 21:57:55 +00005702 ESI.SourceDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
5703 ESI.SourceTemplate = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
Richard Smith564417a2014-03-20 21:47:22 +00005704 } else if (EST == EST_Unevaluated) {
Richard Smith8acb4282014-07-31 21:57:55 +00005705 ESI.SourceDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
Richard Smith564417a2014-03-20 21:47:22 +00005706 }
5707}
5708
Guy Benyei11169dd2012-12-18 14:30:41 +00005709class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
5710 ASTReader &Reader;
5711 ModuleFile &F;
5712 const ASTReader::RecordData &Record;
5713 unsigned &Idx;
5714
5715 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
5716 unsigned &I) {
5717 return Reader.ReadSourceLocation(F, R, I);
5718 }
5719
5720 template<typename T>
5721 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
5722 return Reader.ReadDeclAs<T>(F, Record, Idx);
5723 }
5724
5725public:
5726 TypeLocReader(ASTReader &Reader, ModuleFile &F,
5727 const ASTReader::RecordData &Record, unsigned &Idx)
5728 : Reader(Reader), F(F), Record(Record), Idx(Idx)
5729 { }
5730
5731 // We want compile-time assurance that we've enumerated all of
5732 // these, so unfortunately we have to declare them first, then
5733 // define them out-of-line.
5734#define ABSTRACT_TYPELOC(CLASS, PARENT)
5735#define TYPELOC(CLASS, PARENT) \
5736 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
5737#include "clang/AST/TypeLocNodes.def"
5738
5739 void VisitFunctionTypeLoc(FunctionTypeLoc);
5740 void VisitArrayTypeLoc(ArrayTypeLoc);
5741};
5742
5743void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
5744 // nothing to do
5745}
5746void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
5747 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
5748 if (TL.needsExtraLocalData()) {
5749 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
5750 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
5751 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
5752 TL.setModeAttr(Record[Idx++]);
5753 }
5754}
5755void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
5756 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5757}
5758void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
5759 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5760}
Reid Kleckner8a365022013-06-24 17:51:48 +00005761void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
5762 // nothing to do
5763}
Reid Kleckner0503a872013-12-05 01:23:43 +00005764void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
5765 // nothing to do
5766}
Guy Benyei11169dd2012-12-18 14:30:41 +00005767void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
5768 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
5769}
5770void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
5771 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
5772}
5773void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
5774 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
5775}
5776void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
5777 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5778 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5779}
5780void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
5781 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
5782 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
5783 if (Record[Idx++])
5784 TL.setSizeExpr(Reader.ReadExpr(F));
5785 else
Craig Toppera13603a2014-05-22 05:54:18 +00005786 TL.setSizeExpr(nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00005787}
5788void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
5789 VisitArrayTypeLoc(TL);
5790}
5791void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
5792 VisitArrayTypeLoc(TL);
5793}
5794void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
5795 VisitArrayTypeLoc(TL);
5796}
5797void TypeLocReader::VisitDependentSizedArrayTypeLoc(
5798 DependentSizedArrayTypeLoc TL) {
5799 VisitArrayTypeLoc(TL);
5800}
5801void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
5802 DependentSizedExtVectorTypeLoc TL) {
5803 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5804}
5805void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
5806 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5807}
5808void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
5809 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5810}
5811void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
5812 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
5813 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5814 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5815 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
Alp Tokerb3fd5cf2014-01-21 00:32:38 +00005816 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
5817 TL.setParam(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00005818 }
5819}
5820void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
5821 VisitFunctionTypeLoc(TL);
5822}
5823void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
5824 VisitFunctionTypeLoc(TL);
5825}
5826void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
5827 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5828}
5829void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
5830 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5831}
5832void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
5833 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
5834 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5835 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5836}
5837void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
5838 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
5839 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5840 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5841 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5842}
5843void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
5844 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5845}
5846void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
5847 TL.setKWLoc(ReadSourceLocation(Record, Idx));
5848 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5849 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5850 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5851}
5852void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
5853 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5854}
5855void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
5856 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5857}
5858void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
5859 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5860}
5861void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
5862 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
5863 if (TL.hasAttrOperand()) {
5864 SourceRange range;
5865 range.setBegin(ReadSourceLocation(Record, Idx));
5866 range.setEnd(ReadSourceLocation(Record, Idx));
5867 TL.setAttrOperandParensRange(range);
5868 }
5869 if (TL.hasAttrExprOperand()) {
5870 if (Record[Idx++])
5871 TL.setAttrExprOperand(Reader.ReadExpr(F));
5872 else
Craig Toppera13603a2014-05-22 05:54:18 +00005873 TL.setAttrExprOperand(nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00005874 } else if (TL.hasAttrEnumOperand())
5875 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
5876}
5877void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
5878 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5879}
5880void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
5881 SubstTemplateTypeParmTypeLoc TL) {
5882 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5883}
5884void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
5885 SubstTemplateTypeParmPackTypeLoc TL) {
5886 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5887}
5888void TypeLocReader::VisitTemplateSpecializationTypeLoc(
5889 TemplateSpecializationTypeLoc TL) {
5890 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
5891 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
5892 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5893 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5894 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
5895 TL.setArgLocInfo(i,
5896 Reader.GetTemplateArgumentLocInfo(F,
5897 TL.getTypePtr()->getArg(i).getKind(),
5898 Record, Idx));
5899}
5900void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
5901 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5902 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5903}
5904void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
5905 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5906 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5907}
5908void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
5909 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5910}
5911void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
5912 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5913 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5914 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5915}
5916void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
5917 DependentTemplateSpecializationTypeLoc TL) {
5918 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5919 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5920 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
5921 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
5922 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5923 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5924 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
5925 TL.setArgLocInfo(I,
5926 Reader.GetTemplateArgumentLocInfo(F,
5927 TL.getTypePtr()->getArg(I).getKind(),
5928 Record, Idx));
5929}
5930void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
5931 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
5932}
5933void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
5934 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5935}
5936void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
5937 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Douglas Gregore9d95f12015-07-07 03:57:35 +00005938 TL.setTypeArgsLAngleLoc(ReadSourceLocation(Record, Idx));
5939 TL.setTypeArgsRAngleLoc(ReadSourceLocation(Record, Idx));
5940 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
5941 TL.setTypeArgTInfo(i, Reader.GetTypeSourceInfo(F, Record, Idx));
5942 TL.setProtocolLAngleLoc(ReadSourceLocation(Record, Idx));
5943 TL.setProtocolRAngleLoc(ReadSourceLocation(Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00005944 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
5945 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
5946}
5947void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
5948 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5949}
5950void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
5951 TL.setKWLoc(ReadSourceLocation(Record, Idx));
5952 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5953 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5954}
Xiuli Pan9c14e282016-01-09 12:53:17 +00005955void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
5956 TL.setKWLoc(ReadSourceLocation(Record, Idx));
5957}
Guy Benyei11169dd2012-12-18 14:30:41 +00005958
5959TypeSourceInfo *ASTReader::GetTypeSourceInfo(ModuleFile &F,
5960 const RecordData &Record,
5961 unsigned &Idx) {
5962 QualType InfoTy = readType(F, Record, Idx);
5963 if (InfoTy.isNull())
Craig Toppera13603a2014-05-22 05:54:18 +00005964 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00005965
5966 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
5967 TypeLocReader TLR(*this, F, Record, Idx);
5968 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
5969 TLR.Visit(TL);
5970 return TInfo;
5971}
5972
5973QualType ASTReader::GetType(TypeID ID) {
5974 unsigned FastQuals = ID & Qualifiers::FastMask;
5975 unsigned Index = ID >> Qualifiers::FastWidth;
5976
5977 if (Index < NUM_PREDEF_TYPE_IDS) {
5978 QualType T;
5979 switch ((PredefinedTypeIDs)Index) {
Alexey Baderbdf7c842015-09-15 12:18:29 +00005980 case PREDEF_TYPE_NULL_ID:
5981 return QualType();
5982 case PREDEF_TYPE_VOID_ID:
5983 T = Context.VoidTy;
5984 break;
5985 case PREDEF_TYPE_BOOL_ID:
5986 T = Context.BoolTy;
5987 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00005988
5989 case PREDEF_TYPE_CHAR_U_ID:
5990 case PREDEF_TYPE_CHAR_S_ID:
5991 // FIXME: Check that the signedness of CharTy is correct!
5992 T = Context.CharTy;
5993 break;
5994
Alexey Baderbdf7c842015-09-15 12:18:29 +00005995 case PREDEF_TYPE_UCHAR_ID:
5996 T = Context.UnsignedCharTy;
5997 break;
5998 case PREDEF_TYPE_USHORT_ID:
5999 T = Context.UnsignedShortTy;
6000 break;
6001 case PREDEF_TYPE_UINT_ID:
6002 T = Context.UnsignedIntTy;
6003 break;
6004 case PREDEF_TYPE_ULONG_ID:
6005 T = Context.UnsignedLongTy;
6006 break;
6007 case PREDEF_TYPE_ULONGLONG_ID:
6008 T = Context.UnsignedLongLongTy;
6009 break;
6010 case PREDEF_TYPE_UINT128_ID:
6011 T = Context.UnsignedInt128Ty;
6012 break;
6013 case PREDEF_TYPE_SCHAR_ID:
6014 T = Context.SignedCharTy;
6015 break;
6016 case PREDEF_TYPE_WCHAR_ID:
6017 T = Context.WCharTy;
6018 break;
6019 case PREDEF_TYPE_SHORT_ID:
6020 T = Context.ShortTy;
6021 break;
6022 case PREDEF_TYPE_INT_ID:
6023 T = Context.IntTy;
6024 break;
6025 case PREDEF_TYPE_LONG_ID:
6026 T = Context.LongTy;
6027 break;
6028 case PREDEF_TYPE_LONGLONG_ID:
6029 T = Context.LongLongTy;
6030 break;
6031 case PREDEF_TYPE_INT128_ID:
6032 T = Context.Int128Ty;
6033 break;
6034 case PREDEF_TYPE_HALF_ID:
6035 T = Context.HalfTy;
6036 break;
6037 case PREDEF_TYPE_FLOAT_ID:
6038 T = Context.FloatTy;
6039 break;
6040 case PREDEF_TYPE_DOUBLE_ID:
6041 T = Context.DoubleTy;
6042 break;
6043 case PREDEF_TYPE_LONGDOUBLE_ID:
6044 T = Context.LongDoubleTy;
6045 break;
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +00006046 case PREDEF_TYPE_FLOAT128_ID:
6047 T = Context.Float128Ty;
6048 break;
Alexey Baderbdf7c842015-09-15 12:18:29 +00006049 case PREDEF_TYPE_OVERLOAD_ID:
6050 T = Context.OverloadTy;
6051 break;
6052 case PREDEF_TYPE_BOUND_MEMBER:
6053 T = Context.BoundMemberTy;
6054 break;
6055 case PREDEF_TYPE_PSEUDO_OBJECT:
6056 T = Context.PseudoObjectTy;
6057 break;
6058 case PREDEF_TYPE_DEPENDENT_ID:
6059 T = Context.DependentTy;
6060 break;
6061 case PREDEF_TYPE_UNKNOWN_ANY:
6062 T = Context.UnknownAnyTy;
6063 break;
6064 case PREDEF_TYPE_NULLPTR_ID:
6065 T = Context.NullPtrTy;
6066 break;
6067 case PREDEF_TYPE_CHAR16_ID:
6068 T = Context.Char16Ty;
6069 break;
6070 case PREDEF_TYPE_CHAR32_ID:
6071 T = Context.Char32Ty;
6072 break;
6073 case PREDEF_TYPE_OBJC_ID:
6074 T = Context.ObjCBuiltinIdTy;
6075 break;
6076 case PREDEF_TYPE_OBJC_CLASS:
6077 T = Context.ObjCBuiltinClassTy;
6078 break;
6079 case PREDEF_TYPE_OBJC_SEL:
6080 T = Context.ObjCBuiltinSelTy;
6081 break;
Alexey Bader954ba212016-04-08 13:40:33 +00006082#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
6083 case PREDEF_TYPE_##Id##_ID: \
6084 T = Context.SingletonId; \
Alexey Baderbdf7c842015-09-15 12:18:29 +00006085 break;
Alexey Baderb62f1442016-04-13 08:33:41 +00006086#include "clang/Basic/OpenCLImageTypes.def"
Alexey Baderbdf7c842015-09-15 12:18:29 +00006087 case PREDEF_TYPE_SAMPLER_ID:
6088 T = Context.OCLSamplerTy;
6089 break;
6090 case PREDEF_TYPE_EVENT_ID:
6091 T = Context.OCLEventTy;
6092 break;
Alexey Bader9c8453f2015-09-15 11:18:52 +00006093 case PREDEF_TYPE_CLK_EVENT_ID:
6094 T = Context.OCLClkEventTy;
6095 break;
6096 case PREDEF_TYPE_QUEUE_ID:
6097 T = Context.OCLQueueTy;
6098 break;
6099 case PREDEF_TYPE_NDRANGE_ID:
6100 T = Context.OCLNDRangeTy;
6101 break;
6102 case PREDEF_TYPE_RESERVE_ID_ID:
6103 T = Context.OCLReserveIDTy;
6104 break;
Alexey Baderbdf7c842015-09-15 12:18:29 +00006105 case PREDEF_TYPE_AUTO_DEDUCT:
6106 T = Context.getAutoDeductType();
6107 break;
6108
6109 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
6110 T = Context.getAutoRRefDeductType();
Guy Benyei11169dd2012-12-18 14:30:41 +00006111 break;
6112
6113 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
6114 T = Context.ARCUnbridgedCastTy;
6115 break;
6116
Guy Benyei11169dd2012-12-18 14:30:41 +00006117 case PREDEF_TYPE_BUILTIN_FN:
6118 T = Context.BuiltinFnTy;
6119 break;
Alexey Bataev1a3320e2015-08-25 14:24:04 +00006120
6121 case PREDEF_TYPE_OMP_ARRAY_SECTION:
6122 T = Context.OMPArraySectionTy;
6123 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00006124 }
6125
6126 assert(!T.isNull() && "Unknown predefined type");
6127 return T.withFastQualifiers(FastQuals);
6128 }
6129
6130 Index -= NUM_PREDEF_TYPE_IDS;
6131 assert(Index < TypesLoaded.size() && "Type index out-of-range");
6132 if (TypesLoaded[Index].isNull()) {
6133 TypesLoaded[Index] = readTypeRecord(Index);
6134 if (TypesLoaded[Index].isNull())
6135 return QualType();
6136
6137 TypesLoaded[Index]->setFromAST();
6138 if (DeserializationListener)
6139 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
6140 TypesLoaded[Index]);
6141 }
6142
6143 return TypesLoaded[Index].withFastQualifiers(FastQuals);
6144}
6145
6146QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
6147 return GetType(getGlobalTypeID(F, LocalID));
6148}
6149
6150serialization::TypeID
6151ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
6152 unsigned FastQuals = LocalID & Qualifiers::FastMask;
6153 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
6154
6155 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
6156 return LocalID;
6157
6158 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6159 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
6160 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
6161
6162 unsigned GlobalIndex = LocalIndex + I->second;
6163 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
6164}
6165
6166TemplateArgumentLocInfo
6167ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
6168 TemplateArgument::ArgKind Kind,
6169 const RecordData &Record,
6170 unsigned &Index) {
6171 switch (Kind) {
6172 case TemplateArgument::Expression:
6173 return ReadExpr(F);
6174 case TemplateArgument::Type:
6175 return GetTypeSourceInfo(F, Record, Index);
6176 case TemplateArgument::Template: {
6177 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
6178 Index);
6179 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
6180 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
6181 SourceLocation());
6182 }
6183 case TemplateArgument::TemplateExpansion: {
6184 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
6185 Index);
6186 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
6187 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
6188 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
6189 EllipsisLoc);
6190 }
6191 case TemplateArgument::Null:
6192 case TemplateArgument::Integral:
6193 case TemplateArgument::Declaration:
6194 case TemplateArgument::NullPtr:
6195 case TemplateArgument::Pack:
6196 // FIXME: Is this right?
6197 return TemplateArgumentLocInfo();
6198 }
6199 llvm_unreachable("unexpected template argument loc");
6200}
6201
6202TemplateArgumentLoc
6203ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
6204 const RecordData &Record, unsigned &Index) {
6205 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
6206
6207 if (Arg.getKind() == TemplateArgument::Expression) {
6208 if (Record[Index++]) // bool InfoHasSameExpr.
6209 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
6210 }
6211 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
6212 Record, Index));
6213}
6214
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00006215const ASTTemplateArgumentListInfo*
6216ASTReader::ReadASTTemplateArgumentListInfo(ModuleFile &F,
6217 const RecordData &Record,
6218 unsigned &Index) {
6219 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Index);
6220 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Index);
6221 unsigned NumArgsAsWritten = Record[Index++];
6222 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
6223 for (unsigned i = 0; i != NumArgsAsWritten; ++i)
6224 TemplArgsInfo.addArgument(ReadTemplateArgumentLoc(F, Record, Index));
6225 return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo);
6226}
6227
Guy Benyei11169dd2012-12-18 14:30:41 +00006228Decl *ASTReader::GetExternalDecl(uint32_t ID) {
6229 return GetDecl(ID);
6230}
6231
Richard Smith50895422015-01-31 03:04:55 +00006232template<typename TemplateSpecializationDecl>
6233static void completeRedeclChainForTemplateSpecialization(Decl *D) {
6234 if (auto *TSD = dyn_cast<TemplateSpecializationDecl>(D))
6235 TSD->getSpecializedTemplate()->LoadLazySpecializations();
6236}
6237
Richard Smith053f6c62014-05-16 23:01:30 +00006238void ASTReader::CompleteRedeclChain(const Decl *D) {
Richard Smith851072e2014-05-19 20:59:20 +00006239 if (NumCurrentElementsDeserializing) {
6240 // We arrange to not care about the complete redeclaration chain while we're
6241 // deserializing. Just remember that the AST has marked this one as complete
6242 // but that it's not actually complete yet, so we know we still need to
6243 // complete it later.
6244 PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D));
6245 return;
6246 }
6247
Richard Smith053f6c62014-05-16 23:01:30 +00006248 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
6249
Richard Smith053f6c62014-05-16 23:01:30 +00006250 // If this is a named declaration, complete it by looking it up
6251 // within its context.
6252 //
Richard Smith01bdb7a2014-08-28 05:44:07 +00006253 // FIXME: Merging a function definition should merge
Richard Smith053f6c62014-05-16 23:01:30 +00006254 // all mergeable entities within it.
6255 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) ||
6256 isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) {
6257 if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) {
Richard Smitha534a312015-07-21 23:54:07 +00006258 if (!getContext().getLangOpts().CPlusPlus &&
6259 isa<TranslationUnitDecl>(DC)) {
Richard Smith053f6c62014-05-16 23:01:30 +00006260 // Outside of C++, we don't have a lookup table for the TU, so update
Richard Smitha534a312015-07-21 23:54:07 +00006261 // the identifier instead. (For C++ modules, we don't store decls
6262 // in the serialized identifier table, so we do the lookup in the TU.)
6263 auto *II = Name.getAsIdentifierInfo();
6264 assert(II && "non-identifier name in C?");
Richard Smith053f6c62014-05-16 23:01:30 +00006265 if (II->isOutOfDate())
6266 updateOutOfDateIdentifier(*II);
6267 } else
6268 DC->lookup(Name);
Richard Smith01bdb7a2014-08-28 05:44:07 +00006269 } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) {
Richard Smith3cb15722015-08-05 22:41:45 +00006270 // Find all declarations of this kind from the relevant context.
6271 for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) {
6272 auto *DC = cast<DeclContext>(DCDecl);
6273 SmallVector<Decl*, 8> Decls;
6274 FindExternalLexicalDecls(
6275 DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls);
6276 }
Richard Smith053f6c62014-05-16 23:01:30 +00006277 }
6278 }
Richard Smith50895422015-01-31 03:04:55 +00006279
6280 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D))
6281 CTSD->getSpecializedTemplate()->LoadLazySpecializations();
6282 if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D))
6283 VTSD->getSpecializedTemplate()->LoadLazySpecializations();
6284 if (auto *FD = dyn_cast<FunctionDecl>(D)) {
6285 if (auto *Template = FD->getPrimaryTemplate())
6286 Template->LoadLazySpecializations();
6287 }
Richard Smith053f6c62014-05-16 23:01:30 +00006288}
6289
Richard Smithc2bb8182015-03-24 06:36:48 +00006290CXXCtorInitializer **
6291ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
6292 RecordLocation Loc = getLocalBitOffset(Offset);
6293 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
6294 SavedStreamPosition SavedPosition(Cursor);
6295 Cursor.JumpToBit(Loc.Offset);
6296 ReadingKindTracker ReadingKind(Read_Decl, *this);
6297
6298 RecordData Record;
6299 unsigned Code = Cursor.ReadCode();
6300 unsigned RecCode = Cursor.readRecord(Code, Record);
6301 if (RecCode != DECL_CXX_CTOR_INITIALIZERS) {
6302 Error("malformed AST file: missing C++ ctor initializers");
6303 return nullptr;
6304 }
6305
6306 unsigned Idx = 0;
6307 return ReadCXXCtorInitializers(*Loc.F, Record, Idx);
6308}
6309
Guy Benyei11169dd2012-12-18 14:30:41 +00006310CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
6311 RecordLocation Loc = getLocalBitOffset(Offset);
Chris Lattner7fb3bef2013-01-20 00:56:42 +00006312 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00006313 SavedStreamPosition SavedPosition(Cursor);
6314 Cursor.JumpToBit(Loc.Offset);
6315 ReadingKindTracker ReadingKind(Read_Decl, *this);
6316 RecordData Record;
6317 unsigned Code = Cursor.ReadCode();
Chris Lattner0e6c9402013-01-20 02:38:54 +00006318 unsigned RecCode = Cursor.readRecord(Code, Record);
Guy Benyei11169dd2012-12-18 14:30:41 +00006319 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00006320 Error("malformed AST file: missing C++ base specifiers");
Craig Toppera13603a2014-05-22 05:54:18 +00006321 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00006322 }
6323
6324 unsigned Idx = 0;
6325 unsigned NumBases = Record[Idx++];
6326 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
6327 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
6328 for (unsigned I = 0; I != NumBases; ++I)
6329 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
6330 return Bases;
6331}
6332
6333serialization::DeclID
6334ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
6335 if (LocalID < NUM_PREDEF_DECL_IDS)
6336 return LocalID;
6337
6338 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6339 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
6340 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
6341
6342 return LocalID + I->second;
6343}
6344
6345bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
6346 ModuleFile &M) const {
Richard Smithfe620d22015-03-05 23:24:12 +00006347 // Predefined decls aren't from any module.
6348 if (ID < NUM_PREDEF_DECL_IDS)
6349 return false;
6350
Richard Smithbcda1a92015-07-12 23:51:20 +00006351 return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID &&
6352 ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls;
Guy Benyei11169dd2012-12-18 14:30:41 +00006353}
6354
Douglas Gregor9f782892013-01-21 15:25:38 +00006355ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
Guy Benyei11169dd2012-12-18 14:30:41 +00006356 if (!D->isFromASTFile())
Craig Toppera13603a2014-05-22 05:54:18 +00006357 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00006358 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
6359 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
6360 return I->second;
6361}
6362
6363SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
6364 if (ID < NUM_PREDEF_DECL_IDS)
6365 return SourceLocation();
Richard Smithcd45dbc2014-04-19 03:48:30 +00006366
Guy Benyei11169dd2012-12-18 14:30:41 +00006367 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
6368
6369 if (Index > DeclsLoaded.size()) {
6370 Error("declaration ID out-of-range for AST file");
6371 return SourceLocation();
6372 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00006373
Guy Benyei11169dd2012-12-18 14:30:41 +00006374 if (Decl *D = DeclsLoaded[Index])
6375 return D->getLocation();
6376
Richard Smithcb34bd32016-03-27 07:28:06 +00006377 SourceLocation Loc;
6378 DeclCursorForID(ID, Loc);
6379 return Loc;
Guy Benyei11169dd2012-12-18 14:30:41 +00006380}
6381
Richard Smithfe620d22015-03-05 23:24:12 +00006382static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) {
6383 switch (ID) {
6384 case PREDEF_DECL_NULL_ID:
6385 return nullptr;
6386
6387 case PREDEF_DECL_TRANSLATION_UNIT_ID:
6388 return Context.getTranslationUnitDecl();
6389
6390 case PREDEF_DECL_OBJC_ID_ID:
6391 return Context.getObjCIdDecl();
6392
6393 case PREDEF_DECL_OBJC_SEL_ID:
6394 return Context.getObjCSelDecl();
6395
6396 case PREDEF_DECL_OBJC_CLASS_ID:
6397 return Context.getObjCClassDecl();
6398
6399 case PREDEF_DECL_OBJC_PROTOCOL_ID:
6400 return Context.getObjCProtocolDecl();
6401
6402 case PREDEF_DECL_INT_128_ID:
6403 return Context.getInt128Decl();
6404
6405 case PREDEF_DECL_UNSIGNED_INT_128_ID:
6406 return Context.getUInt128Decl();
6407
6408 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
6409 return Context.getObjCInstanceTypeDecl();
6410
6411 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
6412 return Context.getBuiltinVaListDecl();
Richard Smithf19e1272015-03-07 00:04:49 +00006413
Richard Smith9b88a4c2015-07-27 05:40:23 +00006414 case PREDEF_DECL_VA_LIST_TAG:
6415 return Context.getVaListTagDecl();
6416
Charles Davisc7d5c942015-09-17 20:55:33 +00006417 case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
6418 return Context.getBuiltinMSVaListDecl();
6419
Richard Smithf19e1272015-03-07 00:04:49 +00006420 case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
6421 return Context.getExternCContextDecl();
David Majnemerd9b1a4f2015-11-04 03:40:30 +00006422
6423 case PREDEF_DECL_MAKE_INTEGER_SEQ_ID:
6424 return Context.getMakeIntegerSeqDecl();
Quentin Colombet043406b2016-02-03 22:41:00 +00006425
6426 case PREDEF_DECL_CF_CONSTANT_STRING_ID:
6427 return Context.getCFConstantStringDecl();
Ben Langmuirf5416742016-02-04 00:55:24 +00006428
6429 case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
6430 return Context.getCFConstantStringTagDecl();
Richard Smithfe620d22015-03-05 23:24:12 +00006431 }
Yaron Keren322bdad2015-03-06 07:49:14 +00006432 llvm_unreachable("PredefinedDeclIDs unknown enum value");
Richard Smithfe620d22015-03-05 23:24:12 +00006433}
6434
Richard Smithcd45dbc2014-04-19 03:48:30 +00006435Decl *ASTReader::GetExistingDecl(DeclID ID) {
6436 if (ID < NUM_PREDEF_DECL_IDS) {
Richard Smithfe620d22015-03-05 23:24:12 +00006437 Decl *D = getPredefinedDecl(Context, (PredefinedDeclIDs)ID);
6438 if (D) {
6439 // Track that we have merged the declaration with ID \p ID into the
6440 // pre-existing predefined declaration \p D.
Richard Smith5fc18a92015-07-12 23:43:21 +00006441 auto &Merged = KeyDecls[D->getCanonicalDecl()];
Richard Smithfe620d22015-03-05 23:24:12 +00006442 if (Merged.empty())
6443 Merged.push_back(ID);
Guy Benyei11169dd2012-12-18 14:30:41 +00006444 }
Richard Smithfe620d22015-03-05 23:24:12 +00006445 return D;
Guy Benyei11169dd2012-12-18 14:30:41 +00006446 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00006447
Guy Benyei11169dd2012-12-18 14:30:41 +00006448 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
6449
6450 if (Index >= DeclsLoaded.size()) {
6451 assert(0 && "declaration ID out-of-range for AST file");
6452 Error("declaration ID out-of-range for AST file");
Craig Toppera13603a2014-05-22 05:54:18 +00006453 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00006454 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00006455
6456 return DeclsLoaded[Index];
6457}
6458
6459Decl *ASTReader::GetDecl(DeclID ID) {
6460 if (ID < NUM_PREDEF_DECL_IDS)
6461 return GetExistingDecl(ID);
6462
6463 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
6464
6465 if (Index >= DeclsLoaded.size()) {
6466 assert(0 && "declaration ID out-of-range for AST file");
6467 Error("declaration ID out-of-range for AST file");
Craig Toppera13603a2014-05-22 05:54:18 +00006468 return nullptr;
Richard Smithcd45dbc2014-04-19 03:48:30 +00006469 }
6470
Guy Benyei11169dd2012-12-18 14:30:41 +00006471 if (!DeclsLoaded[Index]) {
6472 ReadDeclRecord(ID);
6473 if (DeserializationListener)
6474 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
6475 }
6476
6477 return DeclsLoaded[Index];
6478}
6479
6480DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
6481 DeclID GlobalID) {
6482 if (GlobalID < NUM_PREDEF_DECL_IDS)
6483 return GlobalID;
6484
6485 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
6486 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
6487 ModuleFile *Owner = I->second;
6488
6489 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
6490 = M.GlobalToLocalDeclIDs.find(Owner);
6491 if (Pos == M.GlobalToLocalDeclIDs.end())
6492 return 0;
6493
6494 return GlobalID - Owner->BaseDeclID + Pos->second;
6495}
6496
6497serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
6498 const RecordData &Record,
6499 unsigned &Idx) {
6500 if (Idx >= Record.size()) {
6501 Error("Corrupted AST file");
6502 return 0;
6503 }
6504
6505 return getGlobalDeclID(F, Record[Idx++]);
6506}
6507
6508/// \brief Resolve the offset of a statement into a statement.
6509///
6510/// This operation will read a new statement from the external
6511/// source each time it is called, and is meant to be used via a
6512/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
6513Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
6514 // Switch case IDs are per Decl.
6515 ClearSwitchCaseIDs();
6516
6517 // Offset here is a global offset across the entire chain.
6518 RecordLocation Loc = getLocalBitOffset(Offset);
6519 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
6520 return ReadStmtFromStream(*Loc.F);
6521}
6522
Richard Smith3cb15722015-08-05 22:41:45 +00006523void ASTReader::FindExternalLexicalDecls(
6524 const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
6525 SmallVectorImpl<Decl *> &Decls) {
Richard Smith82f8fcd2015-08-06 22:07:25 +00006526 bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
6527
Richard Smith9ccdd932015-08-06 22:14:12 +00006528 auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
Richard Smith82f8fcd2015-08-06 22:07:25 +00006529 assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");
6530 for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
6531 auto K = (Decl::Kind)+LexicalDecls[I];
6532 if (!IsKindWeWant(K))
6533 continue;
6534
6535 auto ID = (serialization::DeclID)+LexicalDecls[I + 1];
6536
6537 // Don't add predefined declarations to the lexical context more
6538 // than once.
6539 if (ID < NUM_PREDEF_DECL_IDS) {
6540 if (PredefsVisited[ID])
6541 continue;
6542
6543 PredefsVisited[ID] = true;
6544 }
6545
6546 if (Decl *D = GetLocalDecl(*M, ID)) {
Richard Smith2317a3e2015-08-11 21:21:20 +00006547 assert(D->getKind() == K && "wrong kind for lexical decl");
Richard Smith82f8fcd2015-08-06 22:07:25 +00006548 if (!DC->isDeclInLexicalTraversal(D))
6549 Decls.push_back(D);
6550 }
6551 }
6552 };
6553
6554 if (isa<TranslationUnitDecl>(DC)) {
6555 for (auto Lexical : TULexicalDecls)
6556 Visit(Lexical.first, Lexical.second);
6557 } else {
6558 auto I = LexicalDecls.find(DC);
6559 if (I != LexicalDecls.end())
Richard Smith9c9173d2015-08-11 22:00:24 +00006560 Visit(I->second.first, I->second.second);
Richard Smith82f8fcd2015-08-06 22:07:25 +00006561 }
6562
Guy Benyei11169dd2012-12-18 14:30:41 +00006563 ++NumLexicalDeclContextsRead;
Guy Benyei11169dd2012-12-18 14:30:41 +00006564}
6565
6566namespace {
6567
6568class DeclIDComp {
6569 ASTReader &Reader;
6570 ModuleFile &Mod;
6571
6572public:
6573 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
6574
6575 bool operator()(LocalDeclID L, LocalDeclID R) const {
6576 SourceLocation LHS = getLocation(L);
6577 SourceLocation RHS = getLocation(R);
6578 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6579 }
6580
6581 bool operator()(SourceLocation LHS, LocalDeclID R) const {
6582 SourceLocation RHS = getLocation(R);
6583 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6584 }
6585
6586 bool operator()(LocalDeclID L, SourceLocation RHS) const {
6587 SourceLocation LHS = getLocation(L);
6588 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6589 }
6590
6591 SourceLocation getLocation(LocalDeclID ID) const {
6592 return Reader.getSourceManager().getFileLoc(
6593 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
6594 }
6595};
6596
Alexander Kornienkoab9db512015-06-22 23:07:51 +00006597}
Guy Benyei11169dd2012-12-18 14:30:41 +00006598
6599void ASTReader::FindFileRegionDecls(FileID File,
6600 unsigned Offset, unsigned Length,
6601 SmallVectorImpl<Decl *> &Decls) {
6602 SourceManager &SM = getSourceManager();
6603
6604 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
6605 if (I == FileDeclIDs.end())
6606 return;
6607
6608 FileDeclsInfo &DInfo = I->second;
6609 if (DInfo.Decls.empty())
6610 return;
6611
6612 SourceLocation
6613 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
6614 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
6615
6616 DeclIDComp DIDComp(*this, *DInfo.Mod);
6617 ArrayRef<serialization::LocalDeclID>::iterator
6618 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
6619 BeginLoc, DIDComp);
6620 if (BeginIt != DInfo.Decls.begin())
6621 --BeginIt;
6622
6623 // If we are pointing at a top-level decl inside an objc container, we need
6624 // to backtrack until we find it otherwise we will fail to report that the
6625 // region overlaps with an objc container.
6626 while (BeginIt != DInfo.Decls.begin() &&
6627 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
6628 ->isTopLevelDeclInObjCContainer())
6629 --BeginIt;
6630
6631 ArrayRef<serialization::LocalDeclID>::iterator
6632 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
6633 EndLoc, DIDComp);
6634 if (EndIt != DInfo.Decls.end())
6635 ++EndIt;
6636
6637 for (ArrayRef<serialization::LocalDeclID>::iterator
6638 DIt = BeginIt; DIt != EndIt; ++DIt)
6639 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
6640}
6641
Richard Smith9ce12e32013-02-07 03:30:24 +00006642bool
Guy Benyei11169dd2012-12-18 14:30:41 +00006643ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
6644 DeclarationName Name) {
Richard Smithd88a7f12015-09-01 20:35:42 +00006645 assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
Guy Benyei11169dd2012-12-18 14:30:41 +00006646 "DeclContext has no visible decls in storage");
6647 if (!Name)
Richard Smith9ce12e32013-02-07 03:30:24 +00006648 return false;
Guy Benyei11169dd2012-12-18 14:30:41 +00006649
Richard Smithd88a7f12015-09-01 20:35:42 +00006650 auto It = Lookups.find(DC);
6651 if (It == Lookups.end())
6652 return false;
6653
Richard Smith8c913ec2014-08-14 02:21:01 +00006654 Deserializing LookupResults(this);
6655
Richard Smithd88a7f12015-09-01 20:35:42 +00006656 // Load the list of declarations.
Guy Benyei11169dd2012-12-18 14:30:41 +00006657 SmallVector<NamedDecl *, 64> Decls;
Richard Smithd88a7f12015-09-01 20:35:42 +00006658 for (DeclID ID : It->second.Table.find(Name)) {
6659 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
6660 if (ND->getDeclName() == Name)
6661 Decls.push_back(ND);
6662 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00006663
Guy Benyei11169dd2012-12-18 14:30:41 +00006664 ++NumVisibleDeclContextsRead;
6665 SetExternalVisibleDeclsForName(DC, Name, Decls);
Richard Smith9ce12e32013-02-07 03:30:24 +00006666 return !Decls.empty();
Guy Benyei11169dd2012-12-18 14:30:41 +00006667}
6668
Guy Benyei11169dd2012-12-18 14:30:41 +00006669void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
6670 if (!DC->hasExternalVisibleStorage())
6671 return;
Richard Smithd88a7f12015-09-01 20:35:42 +00006672
6673 auto It = Lookups.find(DC);
6674 assert(It != Lookups.end() &&
6675 "have external visible storage but no lookup tables");
6676
Craig Topper79be4cd2013-07-05 04:33:53 +00006677 DeclsMap Decls;
Guy Benyei11169dd2012-12-18 14:30:41 +00006678
Richard Smithd88a7f12015-09-01 20:35:42 +00006679 for (DeclID ID : It->second.Table.findAll()) {
6680 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
6681 Decls[ND->getDeclName()].push_back(ND);
Guy Benyei11169dd2012-12-18 14:30:41 +00006682 }
6683
Guy Benyei11169dd2012-12-18 14:30:41 +00006684 ++NumVisibleDeclContextsRead;
6685
Craig Topper79be4cd2013-07-05 04:33:53 +00006686 for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
Guy Benyei11169dd2012-12-18 14:30:41 +00006687 SetExternalVisibleDeclsForName(DC, I->first, I->second);
6688 }
6689 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
6690}
6691
Richard Smithd88a7f12015-09-01 20:35:42 +00006692const serialization::reader::DeclContextLookupTable *
6693ASTReader::getLoadedLookupTables(DeclContext *Primary) const {
6694 auto I = Lookups.find(Primary);
6695 return I == Lookups.end() ? nullptr : &I->second;
6696}
6697
Guy Benyei11169dd2012-12-18 14:30:41 +00006698/// \brief Under non-PCH compilation the consumer receives the objc methods
6699/// before receiving the implementation, and codegen depends on this.
6700/// We simulate this by deserializing and passing to consumer the methods of the
6701/// implementation before passing the deserialized implementation decl.
6702static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
6703 ASTConsumer *Consumer) {
6704 assert(ImplD && Consumer);
6705
Aaron Ballmanaff18c02014-03-13 19:03:34 +00006706 for (auto *I : ImplD->methods())
6707 Consumer->HandleInterestingDecl(DeclGroupRef(I));
Guy Benyei11169dd2012-12-18 14:30:41 +00006708
6709 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
6710}
6711
6712void ASTReader::PassInterestingDeclsToConsumer() {
6713 assert(Consumer);
Richard Smith04d05b52014-03-23 00:27:18 +00006714
6715 if (PassingDeclsToConsumer)
6716 return;
6717
6718 // Guard variable to avoid recursively redoing the process of passing
6719 // decls to consumer.
6720 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
6721 true);
6722
Richard Smith9e2341d2015-03-23 03:25:59 +00006723 // Ensure that we've loaded all potentially-interesting declarations
6724 // that need to be eagerly loaded.
6725 for (auto ID : EagerlyDeserializedDecls)
6726 GetDecl(ID);
6727 EagerlyDeserializedDecls.clear();
6728
Guy Benyei11169dd2012-12-18 14:30:41 +00006729 while (!InterestingDecls.empty()) {
6730 Decl *D = InterestingDecls.front();
6731 InterestingDecls.pop_front();
6732
6733 PassInterestingDeclToConsumer(D);
6734 }
6735}
6736
6737void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
6738 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
6739 PassObjCImplDeclToConsumer(ImplD, Consumer);
6740 else
6741 Consumer->HandleInterestingDecl(DeclGroupRef(D));
6742}
6743
6744void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
6745 this->Consumer = Consumer;
6746
Richard Smith9e2341d2015-03-23 03:25:59 +00006747 if (Consumer)
6748 PassInterestingDeclsToConsumer();
Richard Smith7f330cd2015-03-18 01:42:29 +00006749
6750 if (DeserializationListener)
6751 DeserializationListener->ReaderInitialized(this);
Guy Benyei11169dd2012-12-18 14:30:41 +00006752}
6753
6754void ASTReader::PrintStats() {
6755 std::fprintf(stderr, "*** AST File Statistics:\n");
6756
6757 unsigned NumTypesLoaded
6758 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
6759 QualType());
6760 unsigned NumDeclsLoaded
6761 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
Craig Toppera13603a2014-05-22 05:54:18 +00006762 (Decl *)nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00006763 unsigned NumIdentifiersLoaded
6764 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
6765 IdentifiersLoaded.end(),
Craig Toppera13603a2014-05-22 05:54:18 +00006766 (IdentifierInfo *)nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00006767 unsigned NumMacrosLoaded
6768 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
6769 MacrosLoaded.end(),
Craig Toppera13603a2014-05-22 05:54:18 +00006770 (MacroInfo *)nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00006771 unsigned NumSelectorsLoaded
6772 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
6773 SelectorsLoaded.end(),
6774 Selector());
6775
6776 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
6777 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
6778 NumSLocEntriesRead, TotalNumSLocEntries,
6779 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
6780 if (!TypesLoaded.empty())
6781 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
6782 NumTypesLoaded, (unsigned)TypesLoaded.size(),
6783 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
6784 if (!DeclsLoaded.empty())
6785 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
6786 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
6787 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
6788 if (!IdentifiersLoaded.empty())
6789 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
6790 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
6791 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
6792 if (!MacrosLoaded.empty())
6793 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
6794 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
6795 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
6796 if (!SelectorsLoaded.empty())
6797 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
6798 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
6799 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
6800 if (TotalNumStatements)
6801 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
6802 NumStatementsRead, TotalNumStatements,
6803 ((float)NumStatementsRead/TotalNumStatements * 100));
6804 if (TotalNumMacros)
6805 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
6806 NumMacrosRead, TotalNumMacros,
6807 ((float)NumMacrosRead/TotalNumMacros * 100));
6808 if (TotalLexicalDeclContexts)
6809 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
6810 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
6811 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
6812 * 100));
6813 if (TotalVisibleDeclContexts)
6814 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
6815 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
6816 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
6817 * 100));
6818 if (TotalNumMethodPoolEntries) {
6819 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
6820 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
6821 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
6822 * 100));
Guy Benyei11169dd2012-12-18 14:30:41 +00006823 }
Douglas Gregorad2f7a52013-01-28 17:54:36 +00006824 if (NumMethodPoolLookups) {
6825 std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n",
6826 NumMethodPoolHits, NumMethodPoolLookups,
6827 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
6828 }
6829 if (NumMethodPoolTableLookups) {
6830 std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n",
6831 NumMethodPoolTableHits, NumMethodPoolTableLookups,
6832 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
6833 * 100.0));
6834 }
6835
Douglas Gregor00a50f72013-01-25 00:38:33 +00006836 if (NumIdentifierLookupHits) {
6837 std::fprintf(stderr,
6838 " %u / %u identifier table lookups succeeded (%f%%)\n",
6839 NumIdentifierLookupHits, NumIdentifierLookups,
6840 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
6841 }
6842
Douglas Gregore060e572013-01-25 01:03:03 +00006843 if (GlobalIndex) {
6844 std::fprintf(stderr, "\n");
6845 GlobalIndex->printStats();
6846 }
6847
Guy Benyei11169dd2012-12-18 14:30:41 +00006848 std::fprintf(stderr, "\n");
6849 dump();
6850 std::fprintf(stderr, "\n");
6851}
6852
6853template<typename Key, typename ModuleFile, unsigned InitialCapacity>
6854static void
6855dumpModuleIDMap(StringRef Name,
6856 const ContinuousRangeMap<Key, ModuleFile *,
6857 InitialCapacity> &Map) {
6858 if (Map.begin() == Map.end())
6859 return;
6860
6861 typedef ContinuousRangeMap<Key, ModuleFile *, InitialCapacity> MapType;
6862 llvm::errs() << Name << ":\n";
6863 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
6864 I != IEnd; ++I) {
6865 llvm::errs() << " " << I->first << " -> " << I->second->FileName
6866 << "\n";
6867 }
6868}
6869
Yaron Kerencdae9412016-01-29 19:38:18 +00006870LLVM_DUMP_METHOD void ASTReader::dump() {
Guy Benyei11169dd2012-12-18 14:30:41 +00006871 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
6872 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
6873 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
6874 dumpModuleIDMap("Global type map", GlobalTypeMap);
6875 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
6876 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
6877 dumpModuleIDMap("Global macro map", GlobalMacroMap);
6878 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
6879 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
6880 dumpModuleIDMap("Global preprocessed entity map",
6881 GlobalPreprocessedEntityMap);
6882
6883 llvm::errs() << "\n*** PCH/Modules Loaded:";
6884 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
6885 MEnd = ModuleMgr.end();
6886 M != MEnd; ++M)
6887 (*M)->dump();
6888}
6889
6890/// Return the amount of memory used by memory buffers, breaking down
6891/// by heap-backed versus mmap'ed memory.
6892void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
6893 for (ModuleConstIterator I = ModuleMgr.begin(),
6894 E = ModuleMgr.end(); I != E; ++I) {
6895 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
6896 size_t bytes = buf->getBufferSize();
6897 switch (buf->getBufferKind()) {
6898 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
6899 sizes.malloc_bytes += bytes;
6900 break;
6901 case llvm::MemoryBuffer::MemoryBuffer_MMap:
6902 sizes.mmap_bytes += bytes;
6903 break;
6904 }
6905 }
6906 }
6907}
6908
6909void ASTReader::InitializeSema(Sema &S) {
6910 SemaObj = &S;
6911 S.addExternalSource(this);
6912
6913 // Makes sure any declarations that were deserialized "too early"
6914 // still get added to the identifier's declaration chains.
Ben Langmuir5418f402014-09-10 21:29:41 +00006915 for (uint64_t ID : PreloadedDeclIDs) {
6916 NamedDecl *D = cast<NamedDecl>(GetDecl(ID));
6917 pushExternalDeclIntoScope(D, D->getDeclName());
Guy Benyei11169dd2012-12-18 14:30:41 +00006918 }
Ben Langmuir5418f402014-09-10 21:29:41 +00006919 PreloadedDeclIDs.clear();
Guy Benyei11169dd2012-12-18 14:30:41 +00006920
Richard Smith3d8e97e2013-10-18 06:54:39 +00006921 // FIXME: What happens if these are changed by a module import?
Guy Benyei11169dd2012-12-18 14:30:41 +00006922 if (!FPPragmaOptions.empty()) {
6923 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
6924 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
6925 }
6926
Richard Smith3d8e97e2013-10-18 06:54:39 +00006927 // FIXME: What happens if these are changed by a module import?
Guy Benyei11169dd2012-12-18 14:30:41 +00006928 if (!OpenCLExtensions.empty()) {
6929 unsigned I = 0;
6930#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
6931#include "clang/Basic/OpenCLExtensions.def"
6932
6933 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
6934 }
Richard Smith3d8e97e2013-10-18 06:54:39 +00006935
6936 UpdateSema();
6937}
6938
6939void ASTReader::UpdateSema() {
6940 assert(SemaObj && "no Sema to update");
6941
6942 // Load the offsets of the declarations that Sema references.
6943 // They will be lazily deserialized when needed.
6944 if (!SemaDeclRefs.empty()) {
6945 assert(SemaDeclRefs.size() % 2 == 0);
6946 for (unsigned I = 0; I != SemaDeclRefs.size(); I += 2) {
6947 if (!SemaObj->StdNamespace)
6948 SemaObj->StdNamespace = SemaDeclRefs[I];
6949 if (!SemaObj->StdBadAlloc)
6950 SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
6951 }
6952 SemaDeclRefs.clear();
6953 }
Dario Domizioli13a0a382014-05-23 12:13:25 +00006954
Nico Weber779355f2016-03-02 23:22:00 +00006955 // Update the state of pragmas. Use the same API as if we had encountered the
6956 // pragma in the source.
Dario Domizioli13a0a382014-05-23 12:13:25 +00006957 if(OptimizeOffPragmaLocation.isValid())
6958 SemaObj->ActOnPragmaOptimize(/* IsOn = */ false, OptimizeOffPragmaLocation);
Nico Weber779355f2016-03-02 23:22:00 +00006959 if (PragmaMSStructState != -1)
6960 SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState);
Nico Weber42932312016-03-03 00:17:35 +00006961 if (PointersToMembersPragmaLocation.isValid()) {
6962 SemaObj->ActOnPragmaMSPointersToMembers(
6963 (LangOptions::PragmaMSPointersToMembersKind)
6964 PragmaMSPointersToMembersState,
6965 PointersToMembersPragmaLocation);
6966 }
Guy Benyei11169dd2012-12-18 14:30:41 +00006967}
6968
Richard Smitha8d5b6a2015-07-17 19:51:03 +00006969IdentifierInfo *ASTReader::get(StringRef Name) {
Guy Benyei11169dd2012-12-18 14:30:41 +00006970 // Note that we are loading an identifier.
6971 Deserializing AnIdentifier(this);
Douglas Gregore060e572013-01-25 01:03:03 +00006972
Douglas Gregor7211ac12013-01-25 23:32:03 +00006973 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
Douglas Gregor00a50f72013-01-25 00:38:33 +00006974 NumIdentifierLookups,
6975 NumIdentifierLookupHits);
Richard Smith33e0f7e2015-07-22 02:08:40 +00006976
6977 // We don't need to do identifier table lookups in C++ modules (we preload
6978 // all interesting declarations, and don't need to use the scope for name
6979 // lookups). Perform the lookup in PCH files, though, since we don't build
6980 // a complete initial identifier table if we're carrying on from a PCH.
6981 if (Context.getLangOpts().CPlusPlus) {
6982 for (auto F : ModuleMgr.pch_modules())
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00006983 if (Visitor(*F))
Richard Smith33e0f7e2015-07-22 02:08:40 +00006984 break;
6985 } else {
6986 // If there is a global index, look there first to determine which modules
6987 // provably do not have any results for this identifier.
6988 GlobalModuleIndex::HitSet Hits;
6989 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
6990 if (!loadGlobalIndex()) {
6991 if (GlobalIndex->lookupIdentifier(Name, Hits)) {
6992 HitsPtr = &Hits;
6993 }
6994 }
6995
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00006996 ModuleMgr.visit(Visitor, HitsPtr);
Richard Smith33e0f7e2015-07-22 02:08:40 +00006997 }
6998
Guy Benyei11169dd2012-12-18 14:30:41 +00006999 IdentifierInfo *II = Visitor.getIdentifierInfo();
7000 markIdentifierUpToDate(II);
7001 return II;
7002}
7003
7004namespace clang {
7005 /// \brief An identifier-lookup iterator that enumerates all of the
7006 /// identifiers stored within a set of AST files.
7007 class ASTIdentifierIterator : public IdentifierIterator {
7008 /// \brief The AST reader whose identifiers are being enumerated.
7009 const ASTReader &Reader;
7010
7011 /// \brief The current index into the chain of AST files stored in
7012 /// the AST reader.
7013 unsigned Index;
7014
7015 /// \brief The current position within the identifier lookup table
7016 /// of the current AST file.
7017 ASTIdentifierLookupTable::key_iterator Current;
7018
7019 /// \brief The end position within the identifier lookup table of
7020 /// the current AST file.
7021 ASTIdentifierLookupTable::key_iterator End;
7022
Ben Langmuir537c5b52016-05-04 00:53:13 +00007023 /// \brief Whether to skip any modules in the ASTReader.
7024 bool SkipModules;
7025
Guy Benyei11169dd2012-12-18 14:30:41 +00007026 public:
Ben Langmuir537c5b52016-05-04 00:53:13 +00007027 explicit ASTIdentifierIterator(const ASTReader &Reader,
7028 bool SkipModules = false);
Guy Benyei11169dd2012-12-18 14:30:41 +00007029
Craig Topper3e89dfe2014-03-13 02:13:41 +00007030 StringRef Next() override;
Guy Benyei11169dd2012-12-18 14:30:41 +00007031 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +00007032}
Guy Benyei11169dd2012-12-18 14:30:41 +00007033
Ben Langmuir537c5b52016-05-04 00:53:13 +00007034ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader,
7035 bool SkipModules)
7036 : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007037}
7038
7039StringRef ASTIdentifierIterator::Next() {
7040 while (Current == End) {
7041 // If we have exhausted all of our AST files, we're done.
7042 if (Index == 0)
7043 return StringRef();
7044
7045 --Index;
Ben Langmuir537c5b52016-05-04 00:53:13 +00007046 ModuleFile &F = Reader.ModuleMgr[Index];
7047 if (SkipModules && F.isModule())
7048 continue;
7049
7050 ASTIdentifierLookupTable *IdTable =
7051 (ASTIdentifierLookupTable *)F.IdentifierLookupTable;
Guy Benyei11169dd2012-12-18 14:30:41 +00007052 Current = IdTable->key_begin();
7053 End = IdTable->key_end();
7054 }
7055
7056 // We have any identifiers remaining in the current AST file; return
7057 // the next one.
Douglas Gregorbfd73d72013-01-23 18:53:14 +00007058 StringRef Result = *Current;
Guy Benyei11169dd2012-12-18 14:30:41 +00007059 ++Current;
Douglas Gregorbfd73d72013-01-23 18:53:14 +00007060 return Result;
Guy Benyei11169dd2012-12-18 14:30:41 +00007061}
7062
Ben Langmuir537c5b52016-05-04 00:53:13 +00007063namespace {
7064/// A utility for appending two IdentifierIterators.
7065class ChainedIdentifierIterator : public IdentifierIterator {
7066 std::unique_ptr<IdentifierIterator> Current;
7067 std::unique_ptr<IdentifierIterator> Queued;
7068
7069public:
7070 ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First,
7071 std::unique_ptr<IdentifierIterator> Second)
7072 : Current(std::move(First)), Queued(std::move(Second)) {}
7073
7074 StringRef Next() override {
7075 if (!Current)
7076 return StringRef();
7077
7078 StringRef result = Current->Next();
7079 if (!result.empty())
7080 return result;
7081
7082 // Try the queued iterator, which may itself be empty.
7083 Current.reset();
7084 std::swap(Current, Queued);
7085 return Next();
7086 }
7087};
7088} // end anonymous namespace.
7089
Argyrios Kyrtzidis9aca3c62013-04-17 22:10:55 +00007090IdentifierIterator *ASTReader::getIdentifiers() {
Ben Langmuir537c5b52016-05-04 00:53:13 +00007091 if (!loadGlobalIndex()) {
7092 std::unique_ptr<IdentifierIterator> ReaderIter(
7093 new ASTIdentifierIterator(*this, /*SkipModules=*/true));
7094 std::unique_ptr<IdentifierIterator> ModulesIter(
7095 GlobalIndex->createIdentifierIterator());
7096 return new ChainedIdentifierIterator(std::move(ReaderIter),
7097 std::move(ModulesIter));
7098 }
Argyrios Kyrtzidis9aca3c62013-04-17 22:10:55 +00007099
Guy Benyei11169dd2012-12-18 14:30:41 +00007100 return new ASTIdentifierIterator(*this);
7101}
7102
7103namespace clang { namespace serialization {
7104 class ReadMethodPoolVisitor {
7105 ASTReader &Reader;
7106 Selector Sel;
7107 unsigned PriorGeneration;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00007108 unsigned InstanceBits;
7109 unsigned FactoryBits;
Nico Weberff4b35e2014-12-27 22:14:15 +00007110 bool InstanceHasMoreThanOneDecl;
7111 bool FactoryHasMoreThanOneDecl;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00007112 SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
7113 SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
Guy Benyei11169dd2012-12-18 14:30:41 +00007114
7115 public:
Nico Weber2e0c8f72014-12-27 03:58:08 +00007116 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
Guy Benyei11169dd2012-12-18 14:30:41 +00007117 unsigned PriorGeneration)
Nico Weber2e0c8f72014-12-27 03:58:08 +00007118 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration),
Nico Weberff4b35e2014-12-27 22:14:15 +00007119 InstanceBits(0), FactoryBits(0), InstanceHasMoreThanOneDecl(false),
7120 FactoryHasMoreThanOneDecl(false) {}
Nico Weber2e0c8f72014-12-27 03:58:08 +00007121
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00007122 bool operator()(ModuleFile &M) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007123 if (!M.SelectorLookupTable)
7124 return false;
7125
7126 // If we've already searched this module file, skip it now.
Richard Smithbdf2d932015-07-30 03:37:16 +00007127 if (M.Generation <= PriorGeneration)
Guy Benyei11169dd2012-12-18 14:30:41 +00007128 return true;
7129
Richard Smithbdf2d932015-07-30 03:37:16 +00007130 ++Reader.NumMethodPoolTableLookups;
Guy Benyei11169dd2012-12-18 14:30:41 +00007131 ASTSelectorLookupTable *PoolTable
7132 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
Richard Smithbdf2d932015-07-30 03:37:16 +00007133 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
Guy Benyei11169dd2012-12-18 14:30:41 +00007134 if (Pos == PoolTable->end())
7135 return false;
Douglas Gregorad2f7a52013-01-28 17:54:36 +00007136
Richard Smithbdf2d932015-07-30 03:37:16 +00007137 ++Reader.NumMethodPoolTableHits;
7138 ++Reader.NumSelectorsRead;
Guy Benyei11169dd2012-12-18 14:30:41 +00007139 // FIXME: Not quite happy with the statistics here. We probably should
7140 // disable this tracking when called via LoadSelector.
7141 // Also, should entries without methods count as misses?
Richard Smithbdf2d932015-07-30 03:37:16 +00007142 ++Reader.NumMethodPoolEntriesRead;
Guy Benyei11169dd2012-12-18 14:30:41 +00007143 ASTSelectorLookupTrait::data_type Data = *Pos;
Richard Smithbdf2d932015-07-30 03:37:16 +00007144 if (Reader.DeserializationListener)
7145 Reader.DeserializationListener->SelectorRead(Data.ID, Sel);
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00007146
Richard Smithbdf2d932015-07-30 03:37:16 +00007147 InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
7148 FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
7149 InstanceBits = Data.InstanceBits;
7150 FactoryBits = Data.FactoryBits;
7151 InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
7152 FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00007153 return true;
7154 }
7155
7156 /// \brief Retrieve the instance methods found by this visitor.
7157 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
7158 return InstanceMethods;
7159 }
7160
7161 /// \brief Retrieve the instance methods found by this visitor.
7162 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
7163 return FactoryMethods;
7164 }
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00007165
7166 unsigned getInstanceBits() const { return InstanceBits; }
7167 unsigned getFactoryBits() const { return FactoryBits; }
Nico Weberff4b35e2014-12-27 22:14:15 +00007168 bool instanceHasMoreThanOneDecl() const {
7169 return InstanceHasMoreThanOneDecl;
7170 }
7171 bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
Guy Benyei11169dd2012-12-18 14:30:41 +00007172 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +00007173} } // end namespace clang::serialization
Guy Benyei11169dd2012-12-18 14:30:41 +00007174
7175/// \brief Add the given set of methods to the method list.
7176static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
7177 ObjCMethodList &List) {
7178 for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
7179 S.addMethodToGlobalList(&List, Methods[I]);
7180 }
7181}
7182
7183void ASTReader::ReadMethodPool(Selector Sel) {
7184 // Get the selector generation and update it to the current generation.
7185 unsigned &Generation = SelectorGeneration[Sel];
7186 unsigned PriorGeneration = Generation;
Richard Smith053f6c62014-05-16 23:01:30 +00007187 Generation = getGeneration();
Manman Rena0f31a02016-04-29 19:04:05 +00007188 SelectorOutOfDate[Sel] = false;
Guy Benyei11169dd2012-12-18 14:30:41 +00007189
7190 // Search for methods defined with this selector.
Douglas Gregorad2f7a52013-01-28 17:54:36 +00007191 ++NumMethodPoolLookups;
Guy Benyei11169dd2012-12-18 14:30:41 +00007192 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00007193 ModuleMgr.visit(Visitor);
7194
Guy Benyei11169dd2012-12-18 14:30:41 +00007195 if (Visitor.getInstanceMethods().empty() &&
Douglas Gregorad2f7a52013-01-28 17:54:36 +00007196 Visitor.getFactoryMethods().empty())
Guy Benyei11169dd2012-12-18 14:30:41 +00007197 return;
Douglas Gregorad2f7a52013-01-28 17:54:36 +00007198
7199 ++NumMethodPoolHits;
7200
Guy Benyei11169dd2012-12-18 14:30:41 +00007201 if (!getSema())
7202 return;
7203
7204 Sema &S = *getSema();
7205 Sema::GlobalMethodPool::iterator Pos
7206 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
Ben Langmuira0c32e92015-01-12 19:27:00 +00007207
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00007208 Pos->second.first.setBits(Visitor.getInstanceBits());
Nico Weberff4b35e2014-12-27 22:14:15 +00007209 Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00007210 Pos->second.second.setBits(Visitor.getFactoryBits());
Nico Weberff4b35e2014-12-27 22:14:15 +00007211 Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
Ben Langmuira0c32e92015-01-12 19:27:00 +00007212
7213 // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
7214 // when building a module we keep every method individually and may need to
7215 // update hasMoreThanOneDecl as we add the methods.
7216 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
7217 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
Guy Benyei11169dd2012-12-18 14:30:41 +00007218}
7219
Manman Rena0f31a02016-04-29 19:04:05 +00007220void ASTReader::updateOutOfDateSelector(Selector Sel) {
7221 if (SelectorOutOfDate[Sel])
7222 ReadMethodPool(Sel);
7223}
7224
Guy Benyei11169dd2012-12-18 14:30:41 +00007225void ASTReader::ReadKnownNamespaces(
7226 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
7227 Namespaces.clear();
7228
7229 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
7230 if (NamespaceDecl *Namespace
7231 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
7232 Namespaces.push_back(Namespace);
7233 }
7234}
7235
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00007236void ASTReader::ReadUndefinedButUsed(
Richard Smithd6a04d72016-03-25 21:49:43 +00007237 llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00007238 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
7239 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
Nick Lewycky8334af82013-01-26 00:35:08 +00007240 SourceLocation Loc =
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00007241 SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
Nick Lewycky8334af82013-01-26 00:35:08 +00007242 Undefined.insert(std::make_pair(D, Loc));
7243 }
7244}
Nick Lewycky8334af82013-01-26 00:35:08 +00007245
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00007246void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
7247 FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
7248 Exprs) {
7249 for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
7250 FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++]));
7251 uint64_t Count = DelayedDeleteExprs[Idx++];
7252 for (uint64_t C = 0; C < Count; ++C) {
7253 SourceLocation DeleteLoc =
7254 SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]);
7255 const bool IsArrayForm = DelayedDeleteExprs[Idx++];
7256 Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm));
7257 }
7258 }
7259}
7260
Guy Benyei11169dd2012-12-18 14:30:41 +00007261void ASTReader::ReadTentativeDefinitions(
7262 SmallVectorImpl<VarDecl *> &TentativeDefs) {
7263 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
7264 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
7265 if (Var)
7266 TentativeDefs.push_back(Var);
7267 }
7268 TentativeDefinitions.clear();
7269}
7270
7271void ASTReader::ReadUnusedFileScopedDecls(
7272 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
7273 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
7274 DeclaratorDecl *D
7275 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
7276 if (D)
7277 Decls.push_back(D);
7278 }
7279 UnusedFileScopedDecls.clear();
7280}
7281
7282void ASTReader::ReadDelegatingConstructors(
7283 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
7284 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
7285 CXXConstructorDecl *D
7286 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
7287 if (D)
7288 Decls.push_back(D);
7289 }
7290 DelegatingCtorDecls.clear();
7291}
7292
7293void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
7294 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
7295 TypedefNameDecl *D
7296 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
7297 if (D)
7298 Decls.push_back(D);
7299 }
7300 ExtVectorDecls.clear();
7301}
7302
Nico Weber72889432014-09-06 01:25:55 +00007303void ASTReader::ReadUnusedLocalTypedefNameCandidates(
7304 llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
7305 for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
7306 ++I) {
7307 TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
7308 GetDecl(UnusedLocalTypedefNameCandidates[I]));
7309 if (D)
7310 Decls.insert(D);
7311 }
7312 UnusedLocalTypedefNameCandidates.clear();
7313}
7314
Guy Benyei11169dd2012-12-18 14:30:41 +00007315void ASTReader::ReadReferencedSelectors(
7316 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
7317 if (ReferencedSelectorsData.empty())
7318 return;
7319
7320 // If there are @selector references added them to its pool. This is for
7321 // implementation of -Wselector.
7322 unsigned int DataSize = ReferencedSelectorsData.size()-1;
7323 unsigned I = 0;
7324 while (I < DataSize) {
7325 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
7326 SourceLocation SelLoc
7327 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
7328 Sels.push_back(std::make_pair(Sel, SelLoc));
7329 }
7330 ReferencedSelectorsData.clear();
7331}
7332
7333void ASTReader::ReadWeakUndeclaredIdentifiers(
7334 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
7335 if (WeakUndeclaredIdentifiers.empty())
7336 return;
7337
7338 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
7339 IdentifierInfo *WeakId
7340 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
7341 IdentifierInfo *AliasId
7342 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
7343 SourceLocation Loc
7344 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
7345 bool Used = WeakUndeclaredIdentifiers[I++];
7346 WeakInfo WI(AliasId, Loc);
7347 WI.setUsed(Used);
7348 WeakIDs.push_back(std::make_pair(WeakId, WI));
7349 }
7350 WeakUndeclaredIdentifiers.clear();
7351}
7352
7353void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
7354 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
7355 ExternalVTableUse VT;
7356 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
7357 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
7358 VT.DefinitionRequired = VTableUses[Idx++];
7359 VTables.push_back(VT);
7360 }
7361
7362 VTableUses.clear();
7363}
7364
7365void ASTReader::ReadPendingInstantiations(
7366 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
7367 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
7368 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
7369 SourceLocation Loc
7370 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
7371
7372 Pending.push_back(std::make_pair(D, Loc));
7373 }
7374 PendingInstantiations.clear();
7375}
7376
Richard Smithe40f2ba2013-08-07 21:41:30 +00007377void ASTReader::ReadLateParsedTemplates(
Chandler Carruth52cee4d2015-03-26 09:08:15 +00007378 llvm::MapVector<const FunctionDecl *, LateParsedTemplate *> &LPTMap) {
Richard Smithe40f2ba2013-08-07 21:41:30 +00007379 for (unsigned Idx = 0, N = LateParsedTemplates.size(); Idx < N;
7380 /* In loop */) {
7381 FunctionDecl *FD = cast<FunctionDecl>(GetDecl(LateParsedTemplates[Idx++]));
7382
7383 LateParsedTemplate *LT = new LateParsedTemplate;
7384 LT->D = GetDecl(LateParsedTemplates[Idx++]);
7385
7386 ModuleFile *F = getOwningModuleFile(LT->D);
7387 assert(F && "No module");
7388
7389 unsigned TokN = LateParsedTemplates[Idx++];
7390 LT->Toks.reserve(TokN);
7391 for (unsigned T = 0; T < TokN; ++T)
7392 LT->Toks.push_back(ReadToken(*F, LateParsedTemplates, Idx));
7393
Chandler Carruth52cee4d2015-03-26 09:08:15 +00007394 LPTMap.insert(std::make_pair(FD, LT));
Richard Smithe40f2ba2013-08-07 21:41:30 +00007395 }
7396
7397 LateParsedTemplates.clear();
7398}
7399
Guy Benyei11169dd2012-12-18 14:30:41 +00007400void ASTReader::LoadSelector(Selector Sel) {
7401 // It would be complicated to avoid reading the methods anyway. So don't.
7402 ReadMethodPool(Sel);
7403}
7404
7405void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
7406 assert(ID && "Non-zero identifier ID required");
7407 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
7408 IdentifiersLoaded[ID - 1] = II;
7409 if (DeserializationListener)
7410 DeserializationListener->IdentifierRead(ID, II);
7411}
7412
7413/// \brief Set the globally-visible declarations associated with the given
7414/// identifier.
7415///
7416/// If the AST reader is currently in a state where the given declaration IDs
7417/// cannot safely be resolved, they are queued until it is safe to resolve
7418/// them.
7419///
7420/// \param II an IdentifierInfo that refers to one or more globally-visible
7421/// declarations.
7422///
7423/// \param DeclIDs the set of declaration IDs with the name @p II that are
7424/// visible at global scope.
7425///
Douglas Gregor6168bd22013-02-18 15:53:43 +00007426/// \param Decls if non-null, this vector will be populated with the set of
7427/// deserialized declarations. These declarations will not be pushed into
7428/// scope.
Guy Benyei11169dd2012-12-18 14:30:41 +00007429void
7430ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
7431 const SmallVectorImpl<uint32_t> &DeclIDs,
Douglas Gregor6168bd22013-02-18 15:53:43 +00007432 SmallVectorImpl<Decl *> *Decls) {
7433 if (NumCurrentElementsDeserializing && !Decls) {
7434 PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
Guy Benyei11169dd2012-12-18 14:30:41 +00007435 return;
7436 }
7437
7438 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
Ben Langmuir5418f402014-09-10 21:29:41 +00007439 if (!SemaObj) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007440 // Queue this declaration so that it will be added to the
7441 // translation unit scope and identifier's declaration chain
7442 // once a Sema object is known.
Ben Langmuir5418f402014-09-10 21:29:41 +00007443 PreloadedDeclIDs.push_back(DeclIDs[I]);
7444 continue;
Guy Benyei11169dd2012-12-18 14:30:41 +00007445 }
Ben Langmuir5418f402014-09-10 21:29:41 +00007446
7447 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
7448
7449 // If we're simply supposed to record the declarations, do so now.
7450 if (Decls) {
7451 Decls->push_back(D);
7452 continue;
7453 }
7454
7455 // Introduce this declaration into the translation-unit scope
7456 // and add it to the declaration chain for this identifier, so
7457 // that (unqualified) name lookup will find it.
7458 pushExternalDeclIntoScope(D, II);
Guy Benyei11169dd2012-12-18 14:30:41 +00007459 }
7460}
7461
Douglas Gregorc8a992f2013-01-21 16:52:34 +00007462IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007463 if (ID == 0)
Craig Toppera13603a2014-05-22 05:54:18 +00007464 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007465
7466 if (IdentifiersLoaded.empty()) {
7467 Error("no identifier table in AST file");
Craig Toppera13603a2014-05-22 05:54:18 +00007468 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007469 }
7470
7471 ID -= 1;
7472 if (!IdentifiersLoaded[ID]) {
7473 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
7474 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
7475 ModuleFile *M = I->second;
7476 unsigned Index = ID - M->BaseIdentifierID;
7477 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
7478
7479 // All of the strings in the AST file are preceded by a 16-bit length.
7480 // Extract that 16-bit length to avoid having to execute strlen().
7481 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
7482 // unsigned integers. This is important to avoid integer overflow when
7483 // we cast them to 'unsigned'.
7484 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
7485 unsigned StrLen = (((unsigned) StrLenPtr[0])
7486 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Richard Smitheb4b58f62016-02-05 01:40:54 +00007487 auto &II = PP.getIdentifierTable().get(StringRef(Str, StrLen));
7488 IdentifiersLoaded[ID] = &II;
7489 markIdentifierFromAST(*this, II);
Guy Benyei11169dd2012-12-18 14:30:41 +00007490 if (DeserializationListener)
Richard Smitheb4b58f62016-02-05 01:40:54 +00007491 DeserializationListener->IdentifierRead(ID + 1, &II);
Guy Benyei11169dd2012-12-18 14:30:41 +00007492 }
7493
7494 return IdentifiersLoaded[ID];
7495}
7496
Douglas Gregorc8a992f2013-01-21 16:52:34 +00007497IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
7498 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
Guy Benyei11169dd2012-12-18 14:30:41 +00007499}
7500
7501IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
7502 if (LocalID < NUM_PREDEF_IDENT_IDS)
7503 return LocalID;
7504
7505 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7506 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
7507 assert(I != M.IdentifierRemap.end()
7508 && "Invalid index into identifier index remap");
7509
7510 return LocalID + I->second;
7511}
7512
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00007513MacroInfo *ASTReader::getMacro(MacroID ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007514 if (ID == 0)
Craig Toppera13603a2014-05-22 05:54:18 +00007515 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007516
7517 if (MacrosLoaded.empty()) {
7518 Error("no macro table in AST file");
Craig Toppera13603a2014-05-22 05:54:18 +00007519 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007520 }
7521
7522 ID -= NUM_PREDEF_MACRO_IDS;
7523 if (!MacrosLoaded[ID]) {
7524 GlobalMacroMapType::iterator I
7525 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
7526 assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
7527 ModuleFile *M = I->second;
7528 unsigned Index = ID - M->BaseMacroID;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00007529 MacrosLoaded[ID] = ReadMacroRecord(*M, M->MacroOffsets[Index]);
7530
7531 if (DeserializationListener)
7532 DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
7533 MacrosLoaded[ID]);
Guy Benyei11169dd2012-12-18 14:30:41 +00007534 }
7535
7536 return MacrosLoaded[ID];
7537}
7538
7539MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
7540 if (LocalID < NUM_PREDEF_MACRO_IDS)
7541 return LocalID;
7542
7543 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7544 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
7545 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
7546
7547 return LocalID + I->second;
7548}
7549
7550serialization::SubmoduleID
7551ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
7552 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
7553 return LocalID;
7554
7555 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7556 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
7557 assert(I != M.SubmoduleRemap.end()
7558 && "Invalid index into submodule index remap");
7559
7560 return LocalID + I->second;
7561}
7562
7563Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
7564 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
7565 assert(GlobalID == 0 && "Unhandled global submodule ID");
Craig Toppera13603a2014-05-22 05:54:18 +00007566 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007567 }
7568
7569 if (GlobalID > SubmodulesLoaded.size()) {
7570 Error("submodule ID out of range in AST file");
Craig Toppera13603a2014-05-22 05:54:18 +00007571 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007572 }
7573
7574 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
7575}
Douglas Gregorc147b0b2013-01-12 01:29:50 +00007576
7577Module *ASTReader::getModule(unsigned ID) {
7578 return getSubmodule(ID);
7579}
7580
Richard Smithd88a7f12015-09-01 20:35:42 +00007581ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) {
7582 if (ID & 1) {
7583 // It's a module, look it up by submodule ID.
7584 auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1));
7585 return I == GlobalSubmoduleMap.end() ? nullptr : I->second;
7586 } else {
7587 // It's a prefix (preamble, PCH, ...). Look it up by index.
7588 unsigned IndexFromEnd = ID >> 1;
7589 assert(IndexFromEnd && "got reference to unknown module file");
7590 return getModuleManager().pch_modules().end()[-IndexFromEnd];
7591 }
7592}
7593
7594unsigned ASTReader::getModuleFileID(ModuleFile *F) {
7595 if (!F)
7596 return 1;
7597
7598 // For a file representing a module, use the submodule ID of the top-level
7599 // module as the file ID. For any other kind of file, the number of such
7600 // files loaded beforehand will be the same on reload.
7601 // FIXME: Is this true even if we have an explicit module file and a PCH?
7602 if (F->isModule())
7603 return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;
7604
7605 auto PCHModules = getModuleManager().pch_modules();
7606 auto I = std::find(PCHModules.begin(), PCHModules.end(), F);
7607 assert(I != PCHModules.end() && "emitting reference to unknown file");
7608 return (I - PCHModules.end()) << 1;
7609}
7610
Adrian Prantl15bcf702015-06-30 17:39:43 +00007611llvm::Optional<ExternalASTSource::ASTSourceDescriptor>
7612ASTReader::getSourceDescriptor(unsigned ID) {
7613 if (const Module *M = getSubmodule(ID))
Adrian Prantlc6458d62015-09-19 00:10:32 +00007614 return ExternalASTSource::ASTSourceDescriptor(*M);
Adrian Prantl15bcf702015-06-30 17:39:43 +00007615
7616 // If there is only a single PCH, return it instead.
7617 // Chained PCH are not suported.
7618 if (ModuleMgr.size() == 1) {
7619 ModuleFile &MF = ModuleMgr.getPrimaryModule();
Adrian Prantl3a2d4942016-01-22 23:30:56 +00007620 StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName);
Adrian Prantl9bc3c4f2016-04-27 17:06:22 +00007621 StringRef FileName = llvm::sys::path::filename(MF.FileName);
7622 return ASTReader::ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName,
7623 MF.Signature);
Adrian Prantl15bcf702015-06-30 17:39:43 +00007624 }
7625 return None;
7626}
7627
Guy Benyei11169dd2012-12-18 14:30:41 +00007628Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
7629 return DecodeSelector(getGlobalSelectorID(M, LocalID));
7630}
7631
7632Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
7633 if (ID == 0)
7634 return Selector();
7635
7636 if (ID > SelectorsLoaded.size()) {
7637 Error("selector ID out of range in AST file");
7638 return Selector();
7639 }
7640
Craig Toppera13603a2014-05-22 05:54:18 +00007641 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007642 // Load this selector from the selector table.
7643 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
7644 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
7645 ModuleFile &M = *I->second;
7646 ASTSelectorLookupTrait Trait(*this, M);
7647 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
7648 SelectorsLoaded[ID - 1] =
7649 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
7650 if (DeserializationListener)
7651 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
7652 }
7653
7654 return SelectorsLoaded[ID - 1];
7655}
7656
7657Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
7658 return DecodeSelector(ID);
7659}
7660
7661uint32_t ASTReader::GetNumExternalSelectors() {
7662 // ID 0 (the null selector) is considered an external selector.
7663 return getTotalNumSelectors() + 1;
7664}
7665
7666serialization::SelectorID
7667ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
7668 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
7669 return LocalID;
7670
7671 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7672 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
7673 assert(I != M.SelectorRemap.end()
7674 && "Invalid index into selector index remap");
7675
7676 return LocalID + I->second;
7677}
7678
7679DeclarationName
7680ASTReader::ReadDeclarationName(ModuleFile &F,
7681 const RecordData &Record, unsigned &Idx) {
7682 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
7683 switch (Kind) {
7684 case DeclarationName::Identifier:
Douglas Gregorc8a992f2013-01-21 16:52:34 +00007685 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00007686
7687 case DeclarationName::ObjCZeroArgSelector:
7688 case DeclarationName::ObjCOneArgSelector:
7689 case DeclarationName::ObjCMultiArgSelector:
7690 return DeclarationName(ReadSelector(F, Record, Idx));
7691
7692 case DeclarationName::CXXConstructorName:
7693 return Context.DeclarationNames.getCXXConstructorName(
7694 Context.getCanonicalType(readType(F, Record, Idx)));
7695
7696 case DeclarationName::CXXDestructorName:
7697 return Context.DeclarationNames.getCXXDestructorName(
7698 Context.getCanonicalType(readType(F, Record, Idx)));
7699
7700 case DeclarationName::CXXConversionFunctionName:
7701 return Context.DeclarationNames.getCXXConversionFunctionName(
7702 Context.getCanonicalType(readType(F, Record, Idx)));
7703
7704 case DeclarationName::CXXOperatorName:
7705 return Context.DeclarationNames.getCXXOperatorName(
7706 (OverloadedOperatorKind)Record[Idx++]);
7707
7708 case DeclarationName::CXXLiteralOperatorName:
7709 return Context.DeclarationNames.getCXXLiteralOperatorName(
7710 GetIdentifierInfo(F, Record, Idx));
7711
7712 case DeclarationName::CXXUsingDirective:
7713 return DeclarationName::getUsingDirectiveName();
7714 }
7715
7716 llvm_unreachable("Invalid NameKind!");
7717}
7718
7719void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
7720 DeclarationNameLoc &DNLoc,
7721 DeclarationName Name,
7722 const RecordData &Record, unsigned &Idx) {
7723 switch (Name.getNameKind()) {
7724 case DeclarationName::CXXConstructorName:
7725 case DeclarationName::CXXDestructorName:
7726 case DeclarationName::CXXConversionFunctionName:
7727 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
7728 break;
7729
7730 case DeclarationName::CXXOperatorName:
7731 DNLoc.CXXOperatorName.BeginOpNameLoc
7732 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
7733 DNLoc.CXXOperatorName.EndOpNameLoc
7734 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
7735 break;
7736
7737 case DeclarationName::CXXLiteralOperatorName:
7738 DNLoc.CXXLiteralOperatorName.OpNameLoc
7739 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
7740 break;
7741
7742 case DeclarationName::Identifier:
7743 case DeclarationName::ObjCZeroArgSelector:
7744 case DeclarationName::ObjCOneArgSelector:
7745 case DeclarationName::ObjCMultiArgSelector:
7746 case DeclarationName::CXXUsingDirective:
7747 break;
7748 }
7749}
7750
7751void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
7752 DeclarationNameInfo &NameInfo,
7753 const RecordData &Record, unsigned &Idx) {
7754 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
7755 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
7756 DeclarationNameLoc DNLoc;
7757 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
7758 NameInfo.setInfo(DNLoc);
7759}
7760
7761void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
7762 const RecordData &Record, unsigned &Idx) {
7763 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
7764 unsigned NumTPLists = Record[Idx++];
7765 Info.NumTemplParamLists = NumTPLists;
7766 if (NumTPLists) {
7767 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
7768 for (unsigned i=0; i != NumTPLists; ++i)
7769 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
7770 }
7771}
7772
7773TemplateName
7774ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
7775 unsigned &Idx) {
7776 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
7777 switch (Kind) {
7778 case TemplateName::Template:
7779 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
7780
7781 case TemplateName::OverloadedTemplate: {
7782 unsigned size = Record[Idx++];
7783 UnresolvedSet<8> Decls;
7784 while (size--)
7785 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
7786
7787 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
7788 }
7789
7790 case TemplateName::QualifiedTemplate: {
7791 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
7792 bool hasTemplKeyword = Record[Idx++];
7793 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
7794 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
7795 }
7796
7797 case TemplateName::DependentTemplate: {
7798 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
7799 if (Record[Idx++]) // isIdentifier
7800 return Context.getDependentTemplateName(NNS,
7801 GetIdentifierInfo(F, Record,
7802 Idx));
7803 return Context.getDependentTemplateName(NNS,
7804 (OverloadedOperatorKind)Record[Idx++]);
7805 }
7806
7807 case TemplateName::SubstTemplateTemplateParm: {
7808 TemplateTemplateParmDecl *param
7809 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
7810 if (!param) return TemplateName();
7811 TemplateName replacement = ReadTemplateName(F, Record, Idx);
7812 return Context.getSubstTemplateTemplateParm(param, replacement);
7813 }
7814
7815 case TemplateName::SubstTemplateTemplateParmPack: {
7816 TemplateTemplateParmDecl *Param
7817 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
7818 if (!Param)
7819 return TemplateName();
7820
7821 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
7822 if (ArgPack.getKind() != TemplateArgument::Pack)
7823 return TemplateName();
7824
7825 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
7826 }
7827 }
7828
7829 llvm_unreachable("Unhandled template name kind!");
7830}
7831
Richard Smith2bb3c342015-08-09 01:05:31 +00007832TemplateArgument ASTReader::ReadTemplateArgument(ModuleFile &F,
7833 const RecordData &Record,
7834 unsigned &Idx,
7835 bool Canonicalize) {
7836 if (Canonicalize) {
7837 // The caller wants a canonical template argument. Sometimes the AST only
7838 // wants template arguments in canonical form (particularly as the template
7839 // argument lists of template specializations) so ensure we preserve that
7840 // canonical form across serialization.
7841 TemplateArgument Arg = ReadTemplateArgument(F, Record, Idx, false);
7842 return Context.getCanonicalTemplateArgument(Arg);
7843 }
7844
Guy Benyei11169dd2012-12-18 14:30:41 +00007845 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
7846 switch (Kind) {
7847 case TemplateArgument::Null:
7848 return TemplateArgument();
7849 case TemplateArgument::Type:
7850 return TemplateArgument(readType(F, Record, Idx));
7851 case TemplateArgument::Declaration: {
7852 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
David Blaikie0f62c8d2014-10-16 04:21:25 +00007853 return TemplateArgument(D, readType(F, Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00007854 }
7855 case TemplateArgument::NullPtr:
7856 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
7857 case TemplateArgument::Integral: {
7858 llvm::APSInt Value = ReadAPSInt(Record, Idx);
7859 QualType T = readType(F, Record, Idx);
7860 return TemplateArgument(Context, Value, T);
7861 }
7862 case TemplateArgument::Template:
7863 return TemplateArgument(ReadTemplateName(F, Record, Idx));
7864 case TemplateArgument::TemplateExpansion: {
7865 TemplateName Name = ReadTemplateName(F, Record, Idx);
David Blaikie05785d12013-02-20 22:23:23 +00007866 Optional<unsigned> NumTemplateExpansions;
Guy Benyei11169dd2012-12-18 14:30:41 +00007867 if (unsigned NumExpansions = Record[Idx++])
7868 NumTemplateExpansions = NumExpansions - 1;
7869 return TemplateArgument(Name, NumTemplateExpansions);
7870 }
7871 case TemplateArgument::Expression:
7872 return TemplateArgument(ReadExpr(F));
7873 case TemplateArgument::Pack: {
7874 unsigned NumArgs = Record[Idx++];
7875 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
7876 for (unsigned I = 0; I != NumArgs; ++I)
7877 Args[I] = ReadTemplateArgument(F, Record, Idx);
Benjamin Kramercce63472015-08-05 09:40:22 +00007878 return TemplateArgument(llvm::makeArrayRef(Args, NumArgs));
Guy Benyei11169dd2012-12-18 14:30:41 +00007879 }
7880 }
7881
7882 llvm_unreachable("Unhandled template argument kind!");
7883}
7884
7885TemplateParameterList *
7886ASTReader::ReadTemplateParameterList(ModuleFile &F,
7887 const RecordData &Record, unsigned &Idx) {
7888 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
7889 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
7890 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
7891
7892 unsigned NumParams = Record[Idx++];
7893 SmallVector<NamedDecl *, 16> Params;
7894 Params.reserve(NumParams);
7895 while (NumParams--)
7896 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
7897
7898 TemplateParameterList* TemplateParams =
7899 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
David Majnemer902f8c62015-12-27 07:16:27 +00007900 Params, RAngleLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00007901 return TemplateParams;
7902}
7903
7904void
7905ASTReader::
Craig Topper5603df42013-07-05 19:34:19 +00007906ReadTemplateArgumentList(SmallVectorImpl<TemplateArgument> &TemplArgs,
Guy Benyei11169dd2012-12-18 14:30:41 +00007907 ModuleFile &F, const RecordData &Record,
Richard Smith2bb3c342015-08-09 01:05:31 +00007908 unsigned &Idx, bool Canonicalize) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007909 unsigned NumTemplateArgs = Record[Idx++];
7910 TemplArgs.reserve(NumTemplateArgs);
7911 while (NumTemplateArgs--)
Richard Smith2bb3c342015-08-09 01:05:31 +00007912 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx, Canonicalize));
Guy Benyei11169dd2012-12-18 14:30:41 +00007913}
7914
7915/// \brief Read a UnresolvedSet structure.
Richard Smitha4ba74c2013-08-30 04:46:40 +00007916void ASTReader::ReadUnresolvedSet(ModuleFile &F, LazyASTUnresolvedSet &Set,
Guy Benyei11169dd2012-12-18 14:30:41 +00007917 const RecordData &Record, unsigned &Idx) {
7918 unsigned NumDecls = Record[Idx++];
7919 Set.reserve(Context, NumDecls);
7920 while (NumDecls--) {
Richard Smitha4ba74c2013-08-30 04:46:40 +00007921 DeclID ID = ReadDeclID(F, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00007922 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
Richard Smitha4ba74c2013-08-30 04:46:40 +00007923 Set.addLazyDecl(Context, ID, AS);
Guy Benyei11169dd2012-12-18 14:30:41 +00007924 }
7925}
7926
7927CXXBaseSpecifier
7928ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
7929 const RecordData &Record, unsigned &Idx) {
7930 bool isVirtual = static_cast<bool>(Record[Idx++]);
7931 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
7932 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
7933 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
7934 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
7935 SourceRange Range = ReadSourceRange(F, Record, Idx);
7936 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
7937 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
7938 EllipsisLoc);
7939 Result.setInheritConstructors(inheritConstructors);
7940 return Result;
7941}
7942
Richard Smithc2bb8182015-03-24 06:36:48 +00007943CXXCtorInitializer **
Guy Benyei11169dd2012-12-18 14:30:41 +00007944ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
7945 unsigned &Idx) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007946 unsigned NumInitializers = Record[Idx++];
Richard Smithc2bb8182015-03-24 06:36:48 +00007947 assert(NumInitializers && "wrote ctor initializers but have no inits");
7948 auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
7949 for (unsigned i = 0; i != NumInitializers; ++i) {
7950 TypeSourceInfo *TInfo = nullptr;
7951 bool IsBaseVirtual = false;
7952 FieldDecl *Member = nullptr;
7953 IndirectFieldDecl *IndirectMember = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007954
Richard Smithc2bb8182015-03-24 06:36:48 +00007955 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
7956 switch (Type) {
7957 case CTOR_INITIALIZER_BASE:
7958 TInfo = GetTypeSourceInfo(F, Record, Idx);
7959 IsBaseVirtual = Record[Idx++];
7960 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00007961
Richard Smithc2bb8182015-03-24 06:36:48 +00007962 case CTOR_INITIALIZER_DELEGATING:
7963 TInfo = GetTypeSourceInfo(F, Record, Idx);
7964 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00007965
Richard Smithc2bb8182015-03-24 06:36:48 +00007966 case CTOR_INITIALIZER_MEMBER:
7967 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
7968 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00007969
Richard Smithc2bb8182015-03-24 06:36:48 +00007970 case CTOR_INITIALIZER_INDIRECT_MEMBER:
7971 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
7972 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00007973 }
Richard Smithc2bb8182015-03-24 06:36:48 +00007974
7975 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
7976 Expr *Init = ReadExpr(F);
7977 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
7978 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
7979 bool IsWritten = Record[Idx++];
7980 unsigned SourceOrderOrNumArrayIndices;
7981 SmallVector<VarDecl *, 8> Indices;
7982 if (IsWritten) {
7983 SourceOrderOrNumArrayIndices = Record[Idx++];
7984 } else {
7985 SourceOrderOrNumArrayIndices = Record[Idx++];
7986 Indices.reserve(SourceOrderOrNumArrayIndices);
7987 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
7988 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
7989 }
7990
7991 CXXCtorInitializer *BOMInit;
7992 if (Type == CTOR_INITIALIZER_BASE) {
7993 BOMInit = new (Context)
7994 CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
7995 RParenLoc, MemberOrEllipsisLoc);
7996 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
7997 BOMInit = new (Context)
7998 CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
7999 } else if (IsWritten) {
8000 if (Member)
8001 BOMInit = new (Context) CXXCtorInitializer(
8002 Context, Member, MemberOrEllipsisLoc, LParenLoc, Init, RParenLoc);
8003 else
8004 BOMInit = new (Context)
8005 CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
8006 LParenLoc, Init, RParenLoc);
8007 } else {
8008 if (IndirectMember) {
8009 assert(Indices.empty() && "Indirect field improperly initialized");
8010 BOMInit = new (Context)
8011 CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
8012 LParenLoc, Init, RParenLoc);
8013 } else {
8014 BOMInit = CXXCtorInitializer::Create(
8015 Context, Member, MemberOrEllipsisLoc, LParenLoc, Init, RParenLoc,
8016 Indices.data(), Indices.size());
8017 }
8018 }
8019
8020 if (IsWritten)
8021 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
8022 CtorInitializers[i] = BOMInit;
Guy Benyei11169dd2012-12-18 14:30:41 +00008023 }
8024
Richard Smithc2bb8182015-03-24 06:36:48 +00008025 return CtorInitializers;
Guy Benyei11169dd2012-12-18 14:30:41 +00008026}
8027
8028NestedNameSpecifier *
8029ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
8030 const RecordData &Record, unsigned &Idx) {
8031 unsigned N = Record[Idx++];
Craig Toppera13603a2014-05-22 05:54:18 +00008032 NestedNameSpecifier *NNS = nullptr, *Prev = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00008033 for (unsigned I = 0; I != N; ++I) {
8034 NestedNameSpecifier::SpecifierKind Kind
8035 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
8036 switch (Kind) {
8037 case NestedNameSpecifier::Identifier: {
8038 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
8039 NNS = NestedNameSpecifier::Create(Context, Prev, II);
8040 break;
8041 }
8042
8043 case NestedNameSpecifier::Namespace: {
8044 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
8045 NNS = NestedNameSpecifier::Create(Context, Prev, NS);
8046 break;
8047 }
8048
8049 case NestedNameSpecifier::NamespaceAlias: {
8050 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
8051 NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
8052 break;
8053 }
8054
8055 case NestedNameSpecifier::TypeSpec:
8056 case NestedNameSpecifier::TypeSpecWithTemplate: {
8057 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
8058 if (!T)
Craig Toppera13603a2014-05-22 05:54:18 +00008059 return nullptr;
8060
Guy Benyei11169dd2012-12-18 14:30:41 +00008061 bool Template = Record[Idx++];
8062 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
8063 break;
8064 }
8065
8066 case NestedNameSpecifier::Global: {
8067 NNS = NestedNameSpecifier::GlobalSpecifier(Context);
8068 // No associated value, and there can't be a prefix.
8069 break;
8070 }
Nikola Smiljanic67860242014-09-26 00:28:20 +00008071
8072 case NestedNameSpecifier::Super: {
8073 CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(F, Record, Idx);
8074 NNS = NestedNameSpecifier::SuperSpecifier(Context, RD);
8075 break;
8076 }
Guy Benyei11169dd2012-12-18 14:30:41 +00008077 }
8078 Prev = NNS;
8079 }
8080 return NNS;
8081}
8082
8083NestedNameSpecifierLoc
8084ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
8085 unsigned &Idx) {
8086 unsigned N = Record[Idx++];
8087 NestedNameSpecifierLocBuilder Builder;
8088 for (unsigned I = 0; I != N; ++I) {
8089 NestedNameSpecifier::SpecifierKind Kind
8090 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
8091 switch (Kind) {
8092 case NestedNameSpecifier::Identifier: {
8093 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
8094 SourceRange Range = ReadSourceRange(F, Record, Idx);
8095 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
8096 break;
8097 }
8098
8099 case NestedNameSpecifier::Namespace: {
8100 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
8101 SourceRange Range = ReadSourceRange(F, Record, Idx);
8102 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
8103 break;
8104 }
8105
8106 case NestedNameSpecifier::NamespaceAlias: {
8107 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
8108 SourceRange Range = ReadSourceRange(F, Record, Idx);
8109 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
8110 break;
8111 }
8112
8113 case NestedNameSpecifier::TypeSpec:
8114 case NestedNameSpecifier::TypeSpecWithTemplate: {
8115 bool Template = Record[Idx++];
8116 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
8117 if (!T)
8118 return NestedNameSpecifierLoc();
8119 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
8120
8121 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
8122 Builder.Extend(Context,
8123 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
8124 T->getTypeLoc(), ColonColonLoc);
8125 break;
8126 }
8127
8128 case NestedNameSpecifier::Global: {
8129 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
8130 Builder.MakeGlobal(Context, ColonColonLoc);
8131 break;
8132 }
Nikola Smiljanic67860242014-09-26 00:28:20 +00008133
8134 case NestedNameSpecifier::Super: {
8135 CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(F, Record, Idx);
8136 SourceRange Range = ReadSourceRange(F, Record, Idx);
8137 Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd());
8138 break;
8139 }
Guy Benyei11169dd2012-12-18 14:30:41 +00008140 }
8141 }
Nikola Smiljanic67860242014-09-26 00:28:20 +00008142
Guy Benyei11169dd2012-12-18 14:30:41 +00008143 return Builder.getWithLocInContext(Context);
8144}
8145
8146SourceRange
8147ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
8148 unsigned &Idx) {
8149 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
8150 SourceLocation end = ReadSourceLocation(F, Record, Idx);
8151 return SourceRange(beg, end);
8152}
8153
8154/// \brief Read an integral value
8155llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
8156 unsigned BitWidth = Record[Idx++];
8157 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
8158 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
8159 Idx += NumWords;
8160 return Result;
8161}
8162
8163/// \brief Read a signed integral value
8164llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
8165 bool isUnsigned = Record[Idx++];
8166 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
8167}
8168
8169/// \brief Read a floating-point value
Tim Northover178723a2013-01-22 09:46:51 +00008170llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record,
8171 const llvm::fltSemantics &Sem,
8172 unsigned &Idx) {
8173 return llvm::APFloat(Sem, ReadAPInt(Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00008174}
8175
8176// \brief Read a string
8177std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
8178 unsigned Len = Record[Idx++];
8179 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
8180 Idx += Len;
8181 return Result;
8182}
8183
Richard Smith7ed1bc92014-12-05 22:42:13 +00008184std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
8185 unsigned &Idx) {
8186 std::string Filename = ReadString(Record, Idx);
8187 ResolveImportedPath(F, Filename);
8188 return Filename;
8189}
8190
Guy Benyei11169dd2012-12-18 14:30:41 +00008191VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
8192 unsigned &Idx) {
8193 unsigned Major = Record[Idx++];
8194 unsigned Minor = Record[Idx++];
8195 unsigned Subminor = Record[Idx++];
8196 if (Minor == 0)
8197 return VersionTuple(Major);
8198 if (Subminor == 0)
8199 return VersionTuple(Major, Minor - 1);
8200 return VersionTuple(Major, Minor - 1, Subminor - 1);
8201}
8202
8203CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
8204 const RecordData &Record,
8205 unsigned &Idx) {
8206 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
8207 return CXXTemporary::Create(Context, Decl);
8208}
8209
8210DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Argyrios Kyrtzidisdc9fdaf2013-05-24 05:44:08 +00008211 return Diag(CurrentImportLoc, DiagID);
Guy Benyei11169dd2012-12-18 14:30:41 +00008212}
8213
8214DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
8215 return Diags.Report(Loc, DiagID);
8216}
8217
8218/// \brief Retrieve the identifier table associated with the
8219/// preprocessor.
8220IdentifierTable &ASTReader::getIdentifierTable() {
8221 return PP.getIdentifierTable();
8222}
8223
8224/// \brief Record that the given ID maps to the given switch-case
8225/// statement.
8226void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Craig Toppera13603a2014-05-22 05:54:18 +00008227 assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
Guy Benyei11169dd2012-12-18 14:30:41 +00008228 "Already have a SwitchCase with this ID");
8229 (*CurrSwitchCaseStmts)[ID] = SC;
8230}
8231
8232/// \brief Retrieve the switch-case statement with the given ID.
8233SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Craig Toppera13603a2014-05-22 05:54:18 +00008234 assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
Guy Benyei11169dd2012-12-18 14:30:41 +00008235 return (*CurrSwitchCaseStmts)[ID];
8236}
8237
8238void ASTReader::ClearSwitchCaseIDs() {
8239 CurrSwitchCaseStmts->clear();
8240}
8241
8242void ASTReader::ReadComments() {
8243 std::vector<RawComment *> Comments;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00008244 for (SmallVectorImpl<std::pair<BitstreamCursor,
Guy Benyei11169dd2012-12-18 14:30:41 +00008245 serialization::ModuleFile *> >::iterator
8246 I = CommentsCursors.begin(),
8247 E = CommentsCursors.end();
8248 I != E; ++I) {
Dmitri Gribenko9ee0e302014-03-27 15:40:39 +00008249 Comments.clear();
Chris Lattner7fb3bef2013-01-20 00:56:42 +00008250 BitstreamCursor &Cursor = I->first;
Guy Benyei11169dd2012-12-18 14:30:41 +00008251 serialization::ModuleFile &F = *I->second;
8252 SavedStreamPosition SavedPosition(Cursor);
8253
8254 RecordData Record;
8255 while (true) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00008256 llvm::BitstreamEntry Entry =
8257 Cursor.advanceSkippingSubblocks(BitstreamCursor::AF_DontPopBlockAtEnd);
Dmitri Gribenko9ee0e302014-03-27 15:40:39 +00008258
Chris Lattner7fb3bef2013-01-20 00:56:42 +00008259 switch (Entry.Kind) {
8260 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
8261 case llvm::BitstreamEntry::Error:
8262 Error("malformed block record in AST file");
8263 return;
8264 case llvm::BitstreamEntry::EndBlock:
8265 goto NextCursor;
8266 case llvm::BitstreamEntry::Record:
8267 // The interesting case.
Guy Benyei11169dd2012-12-18 14:30:41 +00008268 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00008269 }
8270
8271 // Read a record.
8272 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00008273 switch ((CommentRecordTypes)Cursor.readRecord(Entry.ID, Record)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008274 case COMMENTS_RAW_COMMENT: {
8275 unsigned Idx = 0;
8276 SourceRange SR = ReadSourceRange(F, Record, Idx);
8277 RawComment::CommentKind Kind =
8278 (RawComment::CommentKind) Record[Idx++];
8279 bool IsTrailingComment = Record[Idx++];
8280 bool IsAlmostTrailingComment = Record[Idx++];
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00008281 Comments.push_back(new (Context) RawComment(
8282 SR, Kind, IsTrailingComment, IsAlmostTrailingComment,
8283 Context.getLangOpts().CommentOpts.ParseAllComments));
Guy Benyei11169dd2012-12-18 14:30:41 +00008284 break;
8285 }
8286 }
8287 }
Dmitri Gribenko9ee0e302014-03-27 15:40:39 +00008288 NextCursor:
8289 Context.Comments.addDeserializedComments(Comments);
Guy Benyei11169dd2012-12-18 14:30:41 +00008290 }
Guy Benyei11169dd2012-12-18 14:30:41 +00008291}
8292
Richard Smithcd45dbc2014-04-19 03:48:30 +00008293std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) {
8294 // If we know the owning module, use it.
Richard Smith42413142015-05-15 20:05:43 +00008295 if (Module *M = D->getImportedOwningModule())
Richard Smithcd45dbc2014-04-19 03:48:30 +00008296 return M->getFullModuleName();
8297
8298 // Otherwise, use the name of the top-level module the decl is within.
8299 if (ModuleFile *M = getOwningModuleFile(D))
8300 return M->ModuleName;
8301
8302 // Not from a module.
8303 return "";
8304}
8305
Guy Benyei11169dd2012-12-18 14:30:41 +00008306void ASTReader::finishPendingActions() {
Richard Smith851072e2014-05-19 20:59:20 +00008307 while (!PendingIdentifierInfos.empty() ||
8308 !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() ||
Richard Smith2b9e3e32013-10-18 06:05:18 +00008309 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
Richard Smitha0ce9c42014-07-29 23:23:27 +00008310 !PendingUpdateRecords.empty()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008311 // If any identifiers with corresponding top-level declarations have
8312 // been loaded, load those declarations now.
Craig Topper79be4cd2013-07-05 04:33:53 +00008313 typedef llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2> >
8314 TopLevelDeclsMap;
8315 TopLevelDeclsMap TopLevelDecls;
8316
Guy Benyei11169dd2012-12-18 14:30:41 +00008317 while (!PendingIdentifierInfos.empty()) {
Douglas Gregor6168bd22013-02-18 15:53:43 +00008318 IdentifierInfo *II = PendingIdentifierInfos.back().first;
Richard Smithf0ae3c2d2014-03-28 17:31:23 +00008319 SmallVector<uint32_t, 4> DeclIDs =
8320 std::move(PendingIdentifierInfos.back().second);
Douglas Gregorcb15f082013-02-19 18:26:28 +00008321 PendingIdentifierInfos.pop_back();
Douglas Gregor6168bd22013-02-18 15:53:43 +00008322
8323 SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
Guy Benyei11169dd2012-12-18 14:30:41 +00008324 }
Richard Smithf0ae3c2d2014-03-28 17:31:23 +00008325
Richard Smith851072e2014-05-19 20:59:20 +00008326 // For each decl chain that we wanted to complete while deserializing, mark
8327 // it as "still needs to be completed".
8328 for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
8329 markIncompleteDeclChain(PendingIncompleteDeclChains[I]);
8330 }
8331 PendingIncompleteDeclChains.clear();
8332
Guy Benyei11169dd2012-12-18 14:30:41 +00008333 // Load pending declaration chains.
Richard Smithd8a83712015-08-22 01:47:18 +00008334 for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
Richard Smithd61d4ac2015-08-22 20:13:39 +00008335 loadPendingDeclChain(PendingDeclChains[I].first, PendingDeclChains[I].second);
Guy Benyei11169dd2012-12-18 14:30:41 +00008336 PendingDeclChains.clear();
8337
Douglas Gregor6168bd22013-02-18 15:53:43 +00008338 // Make the most recent of the top-level declarations visible.
Craig Topper79be4cd2013-07-05 04:33:53 +00008339 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
8340 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
Douglas Gregor6168bd22013-02-18 15:53:43 +00008341 IdentifierInfo *II = TLD->first;
8342 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +00008343 pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
Douglas Gregor6168bd22013-02-18 15:53:43 +00008344 }
8345 }
8346
Guy Benyei11169dd2012-12-18 14:30:41 +00008347 // Load any pending macro definitions.
8348 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00008349 IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
8350 SmallVector<PendingMacroInfo, 2> GlobalIDs;
8351 GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
8352 // Initialize the macro history from chained-PCHs ahead of module imports.
Richard Smith49f906a2014-03-01 00:08:04 +00008353 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00008354 ++IDIdx) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00008355 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
Richard Smithe842a472014-10-22 02:05:46 +00008356 if (Info.M->Kind != MK_ImplicitModule &&
8357 Info.M->Kind != MK_ExplicitModule)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00008358 resolvePendingMacro(II, Info);
8359 }
8360 // Handle module imports.
Richard Smith49f906a2014-03-01 00:08:04 +00008361 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00008362 ++IDIdx) {
8363 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
Richard Smithe842a472014-10-22 02:05:46 +00008364 if (Info.M->Kind == MK_ImplicitModule ||
8365 Info.M->Kind == MK_ExplicitModule)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00008366 resolvePendingMacro(II, Info);
Guy Benyei11169dd2012-12-18 14:30:41 +00008367 }
8368 }
8369 PendingMacroIDs.clear();
Argyrios Kyrtzidis83a6e3b2013-02-16 00:48:59 +00008370
8371 // Wire up the DeclContexts for Decls that we delayed setting until
8372 // recursive loading is completed.
8373 while (!PendingDeclContextInfos.empty()) {
8374 PendingDeclContextInfo Info = PendingDeclContextInfos.front();
8375 PendingDeclContextInfos.pop_front();
8376 DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
8377 DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
8378 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
8379 }
Richard Smith2b9e3e32013-10-18 06:05:18 +00008380
Richard Smithd1c46742014-04-30 02:24:17 +00008381 // Perform any pending declaration updates.
Richard Smithd6db68c2014-08-07 20:58:41 +00008382 while (!PendingUpdateRecords.empty()) {
Richard Smithd1c46742014-04-30 02:24:17 +00008383 auto Update = PendingUpdateRecords.pop_back_val();
8384 ReadingKindTracker ReadingKind(Read_Decl, *this);
8385 loadDeclUpdateRecords(Update.first, Update.second);
8386 }
Guy Benyei11169dd2012-12-18 14:30:41 +00008387 }
Richard Smith8a639892015-01-24 01:07:20 +00008388
8389 // At this point, all update records for loaded decls are in place, so any
8390 // fake class definitions should have become real.
8391 assert(PendingFakeDefinitionData.empty() &&
8392 "faked up a class definition but never saw the real one");
8393
Guy Benyei11169dd2012-12-18 14:30:41 +00008394 // If we deserialized any C++ or Objective-C class definitions, any
8395 // Objective-C protocol definitions, or any redeclarable templates, make sure
8396 // that all redeclarations point to the definitions. Note that this can only
8397 // happen now, after the redeclaration chains have been fully wired.
Craig Topperc6914d02014-08-25 04:15:02 +00008398 for (Decl *D : PendingDefinitions) {
8399 if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
Richard Smith5b21db82014-04-23 18:20:42 +00008400 if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008401 // Make sure that the TagType points at the definition.
8402 const_cast<TagType*>(TagT)->decl = TD;
8403 }
Richard Smith8ce51082015-03-11 01:44:51 +00008404
Craig Topperc6914d02014-08-25 04:15:02 +00008405 if (auto RD = dyn_cast<CXXRecordDecl>(D)) {
Richard Smith8ce51082015-03-11 01:44:51 +00008406 for (auto *R = getMostRecentExistingDecl(RD); R;
8407 R = R->getPreviousDecl()) {
8408 assert((R == D) ==
8409 cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
Richard Smith2c381642014-08-27 23:11:59 +00008410 "declaration thinks it's the definition but it isn't");
Aaron Ballman86c93902014-03-06 23:45:36 +00008411 cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
Richard Smith2c381642014-08-27 23:11:59 +00008412 }
Guy Benyei11169dd2012-12-18 14:30:41 +00008413 }
8414
8415 continue;
8416 }
Richard Smith8ce51082015-03-11 01:44:51 +00008417
Craig Topperc6914d02014-08-25 04:15:02 +00008418 if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008419 // Make sure that the ObjCInterfaceType points at the definition.
8420 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
8421 ->Decl = ID;
Richard Smith8ce51082015-03-11 01:44:51 +00008422
8423 for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl())
8424 cast<ObjCInterfaceDecl>(R)->Data = ID->Data;
8425
Guy Benyei11169dd2012-12-18 14:30:41 +00008426 continue;
8427 }
Richard Smith8ce51082015-03-11 01:44:51 +00008428
Craig Topperc6914d02014-08-25 04:15:02 +00008429 if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) {
Richard Smith8ce51082015-03-11 01:44:51 +00008430 for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl())
8431 cast<ObjCProtocolDecl>(R)->Data = PD->Data;
8432
Guy Benyei11169dd2012-12-18 14:30:41 +00008433 continue;
8434 }
Richard Smith8ce51082015-03-11 01:44:51 +00008435
Craig Topperc6914d02014-08-25 04:15:02 +00008436 auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl();
Richard Smith8ce51082015-03-11 01:44:51 +00008437 for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl())
8438 cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common;
Guy Benyei11169dd2012-12-18 14:30:41 +00008439 }
8440 PendingDefinitions.clear();
8441
8442 // Load the bodies of any functions or methods we've encountered. We do
8443 // this now (delayed) so that we can be sure that the declaration chains
Richard Smithb9fa9962015-08-21 03:04:33 +00008444 // have been fully wired up (hasBody relies on this).
8445 // FIXME: We shouldn't require complete redeclaration chains here.
Guy Benyei11169dd2012-12-18 14:30:41 +00008446 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
8447 PBEnd = PendingBodies.end();
8448 PB != PBEnd; ++PB) {
8449 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
8450 // FIXME: Check for =delete/=default?
8451 // FIXME: Complain about ODR violations here?
8452 if (!getContext().getLangOpts().Modules || !FD->hasBody())
8453 FD->setLazyBody(PB->second);
8454 continue;
8455 }
8456
8457 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
8458 if (!getContext().getLangOpts().Modules || !MD->hasBody())
8459 MD->setLazyBody(PB->second);
8460 }
8461 PendingBodies.clear();
Richard Smith42413142015-05-15 20:05:43 +00008462
8463 // Do some cleanup.
8464 for (auto *ND : PendingMergedDefinitionsToDeduplicate)
8465 getContext().deduplicateMergedDefinitonsFor(ND);
8466 PendingMergedDefinitionsToDeduplicate.clear();
Richard Smitha0ce9c42014-07-29 23:23:27 +00008467}
8468
8469void ASTReader::diagnoseOdrViolations() {
Richard Smithbb853c72014-08-13 01:23:33 +00008470 if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty())
8471 return;
8472
Richard Smitha0ce9c42014-07-29 23:23:27 +00008473 // Trigger the import of the full definition of each class that had any
8474 // odr-merging problems, so we can produce better diagnostics for them.
Richard Smithbb853c72014-08-13 01:23:33 +00008475 // These updates may in turn find and diagnose some ODR failures, so take
8476 // ownership of the set first.
8477 auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
8478 PendingOdrMergeFailures.clear();
8479 for (auto &Merge : OdrMergeFailures) {
Richard Smitha0ce9c42014-07-29 23:23:27 +00008480 Merge.first->buildLookup();
8481 Merge.first->decls_begin();
8482 Merge.first->bases_begin();
8483 Merge.first->vbases_begin();
8484 for (auto *RD : Merge.second) {
8485 RD->decls_begin();
8486 RD->bases_begin();
8487 RD->vbases_begin();
8488 }
8489 }
8490
8491 // For each declaration from a merged context, check that the canonical
8492 // definition of that context also contains a declaration of the same
8493 // entity.
8494 //
8495 // Caution: this loop does things that might invalidate iterators into
8496 // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
8497 while (!PendingOdrMergeChecks.empty()) {
8498 NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
8499
8500 // FIXME: Skip over implicit declarations for now. This matters for things
8501 // like implicitly-declared special member functions. This isn't entirely
8502 // correct; we can end up with multiple unmerged declarations of the same
8503 // implicit entity.
8504 if (D->isImplicit())
8505 continue;
8506
8507 DeclContext *CanonDef = D->getDeclContext();
Richard Smitha0ce9c42014-07-29 23:23:27 +00008508
8509 bool Found = false;
8510 const Decl *DCanon = D->getCanonicalDecl();
8511
Richard Smith01bdb7a2014-08-28 05:44:07 +00008512 for (auto RI : D->redecls()) {
8513 if (RI->getLexicalDeclContext() == CanonDef) {
8514 Found = true;
8515 break;
8516 }
8517 }
8518 if (Found)
8519 continue;
8520
Richard Smith0f4e2c42015-08-06 04:23:48 +00008521 // Quick check failed, time to do the slow thing. Note, we can't just
8522 // look up the name of D in CanonDef here, because the member that is
8523 // in CanonDef might not be found by name lookup (it might have been
8524 // replaced by a more recent declaration in the lookup table), and we
8525 // can't necessarily find it in the redeclaration chain because it might
8526 // be merely mergeable, not redeclarable.
Richard Smitha0ce9c42014-07-29 23:23:27 +00008527 llvm::SmallVector<const NamedDecl*, 4> Candidates;
Richard Smith0f4e2c42015-08-06 04:23:48 +00008528 for (auto *CanonMember : CanonDef->decls()) {
8529 if (CanonMember->getCanonicalDecl() == DCanon) {
8530 // This can happen if the declaration is merely mergeable and not
8531 // actually redeclarable (we looked for redeclarations earlier).
8532 //
8533 // FIXME: We should be able to detect this more efficiently, without
8534 // pulling in all of the members of CanonDef.
8535 Found = true;
8536 break;
Richard Smitha0ce9c42014-07-29 23:23:27 +00008537 }
Richard Smith0f4e2c42015-08-06 04:23:48 +00008538 if (auto *ND = dyn_cast<NamedDecl>(CanonMember))
8539 if (ND->getDeclName() == D->getDeclName())
8540 Candidates.push_back(ND);
Richard Smitha0ce9c42014-07-29 23:23:27 +00008541 }
8542
8543 if (!Found) {
Richard Smithd08aeb62014-08-28 01:33:39 +00008544 // The AST doesn't like TagDecls becoming invalid after they've been
8545 // completed. We only really need to mark FieldDecls as invalid here.
8546 if (!isa<TagDecl>(D))
8547 D->setInvalidDecl();
Richard Smith4ab3dbd2015-02-13 22:43:51 +00008548
8549 // Ensure we don't accidentally recursively enter deserialization while
8550 // we're producing our diagnostic.
8551 Deserializing RecursionGuard(this);
Richard Smitha0ce9c42014-07-29 23:23:27 +00008552
8553 std::string CanonDefModule =
8554 getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef));
8555 Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
8556 << D << getOwningModuleNameForDiagnostic(D)
8557 << CanonDef << CanonDefModule.empty() << CanonDefModule;
8558
8559 if (Candidates.empty())
8560 Diag(cast<Decl>(CanonDef)->getLocation(),
8561 diag::note_module_odr_violation_no_possible_decls) << D;
8562 else {
8563 for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
8564 Diag(Candidates[I]->getLocation(),
8565 diag::note_module_odr_violation_possible_decl)
8566 << Candidates[I];
8567 }
8568
8569 DiagnosedOdrMergeFailures.insert(CanonDef);
8570 }
8571 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00008572
Richard Smith4ab3dbd2015-02-13 22:43:51 +00008573 if (OdrMergeFailures.empty())
8574 return;
8575
8576 // Ensure we don't accidentally recursively enter deserialization while
8577 // we're producing our diagnostics.
8578 Deserializing RecursionGuard(this);
8579
Richard Smithcd45dbc2014-04-19 03:48:30 +00008580 // Issue any pending ODR-failure diagnostics.
Richard Smithbb853c72014-08-13 01:23:33 +00008581 for (auto &Merge : OdrMergeFailures) {
Richard Smitha0ce9c42014-07-29 23:23:27 +00008582 // If we've already pointed out a specific problem with this class, don't
8583 // bother issuing a general "something's different" diagnostic.
David Blaikie82e95a32014-11-19 07:49:47 +00008584 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
Richard Smithcd45dbc2014-04-19 03:48:30 +00008585 continue;
8586
8587 bool Diagnosed = false;
8588 for (auto *RD : Merge.second) {
8589 // Multiple different declarations got merged together; tell the user
8590 // where they came from.
8591 if (Merge.first != RD) {
8592 // FIXME: Walk the definition, figure out what's different,
8593 // and diagnose that.
8594 if (!Diagnosed) {
8595 std::string Module = getOwningModuleNameForDiagnostic(Merge.first);
8596 Diag(Merge.first->getLocation(),
8597 diag::err_module_odr_violation_different_definitions)
8598 << Merge.first << Module.empty() << Module;
8599 Diagnosed = true;
8600 }
8601
8602 Diag(RD->getLocation(),
8603 diag::note_module_odr_violation_different_definitions)
8604 << getOwningModuleNameForDiagnostic(RD);
8605 }
8606 }
8607
8608 if (!Diagnosed) {
8609 // All definitions are updates to the same declaration. This happens if a
8610 // module instantiates the declaration of a class template specialization
8611 // and two or more other modules instantiate its definition.
8612 //
8613 // FIXME: Indicate which modules had instantiations of this definition.
8614 // FIXME: How can this even happen?
8615 Diag(Merge.first->getLocation(),
8616 diag::err_module_odr_violation_different_instantiations)
8617 << Merge.first;
8618 }
8619 }
Guy Benyei11169dd2012-12-18 14:30:41 +00008620}
8621
Richard Smithce18a182015-07-14 00:26:00 +00008622void ASTReader::StartedDeserializing() {
8623 if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get())
8624 ReadTimer->startTimer();
8625}
8626
Guy Benyei11169dd2012-12-18 14:30:41 +00008627void ASTReader::FinishedDeserializing() {
8628 assert(NumCurrentElementsDeserializing &&
8629 "FinishedDeserializing not paired with StartedDeserializing");
8630 if (NumCurrentElementsDeserializing == 1) {
8631 // We decrease NumCurrentElementsDeserializing only after pending actions
8632 // are finished, to avoid recursively re-calling finishPendingActions().
8633 finishPendingActions();
8634 }
8635 --NumCurrentElementsDeserializing;
8636
Richard Smitha0ce9c42014-07-29 23:23:27 +00008637 if (NumCurrentElementsDeserializing == 0) {
Richard Smith9e2341d2015-03-23 03:25:59 +00008638 // Propagate exception specification updates along redeclaration chains.
Richard Smith7226f2a2015-03-23 19:54:56 +00008639 while (!PendingExceptionSpecUpdates.empty()) {
8640 auto Updates = std::move(PendingExceptionSpecUpdates);
8641 PendingExceptionSpecUpdates.clear();
8642 for (auto Update : Updates) {
8643 auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
Richard Smith1d0f1992015-08-19 21:09:32 +00008644 auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
Richard Smithd88a7f12015-09-01 20:35:42 +00008645 if (auto *Listener = Context.getASTMutationListener())
8646 Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second));
Richard Smith1d0f1992015-08-19 21:09:32 +00008647 for (auto *Redecl : Update.second->redecls())
8648 Context.adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI);
Richard Smith7226f2a2015-03-23 19:54:56 +00008649 }
Richard Smith9e2341d2015-03-23 03:25:59 +00008650 }
8651
Richard Smithce18a182015-07-14 00:26:00 +00008652 if (ReadTimer)
8653 ReadTimer->stopTimer();
8654
Richard Smith0f4e2c42015-08-06 04:23:48 +00008655 diagnoseOdrViolations();
8656
Richard Smith04d05b52014-03-23 00:27:18 +00008657 // We are not in recursive loading, so it's safe to pass the "interesting"
8658 // decls to the consumer.
Richard Smitha0ce9c42014-07-29 23:23:27 +00008659 if (Consumer)
8660 PassInterestingDeclsToConsumer();
Guy Benyei11169dd2012-12-18 14:30:41 +00008661 }
8662}
8663
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +00008664void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
Richard Smith9e2341d2015-03-23 03:25:59 +00008665 if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
8666 // Remove any fake results before adding any real ones.
8667 auto It = PendingFakeLookupResults.find(II);
8668 if (It != PendingFakeLookupResults.end()) {
Richard Smitha534a312015-07-21 23:54:07 +00008669 for (auto *ND : It->second)
Richard Smith9e2341d2015-03-23 03:25:59 +00008670 SemaObj->IdResolver.RemoveDecl(ND);
Ben Langmuireb8bd2d2015-04-10 22:25:42 +00008671 // FIXME: this works around module+PCH performance issue.
8672 // Rather than erase the result from the map, which is O(n), just clear
8673 // the vector of NamedDecls.
8674 It->second.clear();
Richard Smith9e2341d2015-03-23 03:25:59 +00008675 }
8676 }
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +00008677
8678 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
8679 SemaObj->TUScope->AddDecl(D);
8680 } else if (SemaObj->TUScope) {
8681 // Adding the decl to IdResolver may have failed because it was already in
8682 // (even though it was not added in scope). If it is already in, make sure
8683 // it gets in the scope as well.
8684 if (std::find(SemaObj->IdResolver.begin(Name),
8685 SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
8686 SemaObj->TUScope->AddDecl(D);
8687 }
8688}
8689
Douglas Gregor6623e1f2015-11-03 18:33:07 +00008690ASTReader::ASTReader(
8691 Preprocessor &PP, ASTContext &Context,
8692 const PCHContainerReader &PCHContainerRdr,
8693 ArrayRef<IntrusiveRefCntPtr<ModuleFileExtension>> Extensions,
8694 StringRef isysroot, bool DisableValidation,
8695 bool AllowASTWithCompilerErrors,
8696 bool AllowConfigurationMismatch, bool ValidateSystemInputs,
8697 bool UseGlobalIndex,
8698 std::unique_ptr<llvm::Timer> ReadTimer)
Craig Toppera13603a2014-05-22 05:54:18 +00008699 : Listener(new PCHValidator(PP, *this)), DeserializationListener(nullptr),
Nico Weber824285e2014-05-08 04:26:47 +00008700 OwnsDeserializationListener(false), SourceMgr(PP.getSourceManager()),
Adrian Prantlfb2398d2015-07-17 01:19:54 +00008701 FileMgr(PP.getFileManager()), PCHContainerRdr(PCHContainerRdr),
Adrian Prantlbb165fb2015-06-20 18:53:08 +00008702 Diags(PP.getDiagnostics()), SemaObj(nullptr), PP(PP), Context(Context),
Adrian Prantlfb2398d2015-07-17 01:19:54 +00008703 Consumer(nullptr), ModuleMgr(PP.getFileManager(), PCHContainerRdr),
Richard Smith10379092016-05-06 23:14:07 +00008704 DummyIdResolver(PP),
Richard Smithce18a182015-07-14 00:26:00 +00008705 ReadTimer(std::move(ReadTimer)),
Nico Weber779355f2016-03-02 23:22:00 +00008706 PragmaMSStructState(-1),
Nico Weber42932312016-03-03 00:17:35 +00008707 PragmaMSPointersToMembersState(-1),
Adrian Prantlbb165fb2015-06-20 18:53:08 +00008708 isysroot(isysroot), DisableValidation(DisableValidation),
Nico Weber824285e2014-05-08 04:26:47 +00008709 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
8710 AllowConfigurationMismatch(AllowConfigurationMismatch),
8711 ValidateSystemInputs(ValidateSystemInputs),
8712 UseGlobalIndex(UseGlobalIndex), TriedLoadingGlobalIndex(false),
Adrian Prantlbb165fb2015-06-20 18:53:08 +00008713 CurrSwitchCaseStmts(&SwitchCaseStmts), NumSLocEntriesRead(0),
8714 TotalNumSLocEntries(0), NumStatementsRead(0), TotalNumStatements(0),
8715 NumMacrosRead(0), TotalNumMacros(0), NumIdentifierLookups(0),
8716 NumIdentifierLookupHits(0), NumSelectorsRead(0),
Nico Weber824285e2014-05-08 04:26:47 +00008717 NumMethodPoolEntriesRead(0), NumMethodPoolLookups(0),
8718 NumMethodPoolHits(0), NumMethodPoolTableLookups(0),
8719 NumMethodPoolTableHits(0), TotalNumMethodPoolEntries(0),
8720 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
8721 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
8722 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
Richard Smithc2bb8182015-03-24 06:36:48 +00008723 PassingDeclsToConsumer(false), ReadingKind(Read_None) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008724 SourceMgr.setExternalSLocEntrySource(this);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00008725
8726 for (const auto &Ext : Extensions) {
8727 auto BlockName = Ext->getExtensionMetadata().BlockName;
8728 auto Known = ModuleFileExtensions.find(BlockName);
8729 if (Known != ModuleFileExtensions.end()) {
8730 Diags.Report(diag::warn_duplicate_module_file_extension)
8731 << BlockName;
8732 continue;
8733 }
8734
8735 ModuleFileExtensions.insert({BlockName, Ext});
8736 }
Guy Benyei11169dd2012-12-18 14:30:41 +00008737}
8738
8739ASTReader::~ASTReader() {
Nico Weber824285e2014-05-08 04:26:47 +00008740 if (OwnsDeserializationListener)
8741 delete DeserializationListener;
Guy Benyei11169dd2012-12-18 14:30:41 +00008742}
Richard Smith10379092016-05-06 23:14:07 +00008743
8744IdentifierResolver &ASTReader::getIdResolver() {
8745 return SemaObj ? SemaObj->IdResolver : DummyIdResolver;
8746}