blob: 27b7048f375bb31197107ff79db313dcdcccc9c8 [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() ||
Richard Smithe75ee0f2015-08-17 07:13:32 +00002013 (StoredTime && StoredTime != File->getModificationTime() &&
2014 !DisableValidation)
Ben Langmuir198c1682014-03-07 07:27:49 +00002015 )) {
2016 if (Complain) {
2017 // Build a list of the PCH imports that got us here (in reverse).
2018 SmallVector<ModuleFile *, 4> ImportStack(1, &F);
2019 while (ImportStack.back()->ImportedBy.size() > 0)
2020 ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
Ben Langmuire82630d2014-01-17 00:19:09 +00002021
Ben Langmuir198c1682014-03-07 07:27:49 +00002022 // The top-level PCH is stale.
2023 StringRef TopLevelPCHName(ImportStack.back()->FileName);
2024 Error(diag::err_fe_pch_file_modified, Filename, TopLevelPCHName);
Ben Langmuire82630d2014-01-17 00:19:09 +00002025
Ben Langmuir198c1682014-03-07 07:27:49 +00002026 // Print the import stack.
2027 if (ImportStack.size() > 1 && !Diags.isDiagnosticInFlight()) {
2028 Diag(diag::note_pch_required_by)
2029 << Filename << ImportStack[0]->FileName;
2030 for (unsigned I = 1; I < ImportStack.size(); ++I)
Ben Langmuire82630d2014-01-17 00:19:09 +00002031 Diag(diag::note_pch_required_by)
Ben Langmuir198c1682014-03-07 07:27:49 +00002032 << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
Douglas Gregor7029ce12013-03-19 00:28:20 +00002033 }
2034
Ben Langmuir198c1682014-03-07 07:27:49 +00002035 if (!Diags.isDiagnosticInFlight())
2036 Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
Guy Benyei11169dd2012-12-18 14:30:41 +00002037 }
2038
Ben Langmuir198c1682014-03-07 07:27:49 +00002039 IsOutOfDate = true;
Guy Benyei11169dd2012-12-18 14:30:41 +00002040 }
Richard Smitha8cfffa2015-11-26 02:04:16 +00002041 // FIXME: If the file is overridden and we've already opened it,
2042 // issue an error (or split it into a separate FileEntry).
Guy Benyei11169dd2012-12-18 14:30:41 +00002043
Richard Smitha8cfffa2015-11-26 02:04:16 +00002044 InputFile IF = InputFile(File, Overridden || Transient, IsOutOfDate);
Ben Langmuir198c1682014-03-07 07:27:49 +00002045
2046 // Note that we've loaded this input file.
2047 F.InputFilesLoaded[ID-1] = IF;
2048 return IF;
Guy Benyei11169dd2012-12-18 14:30:41 +00002049}
2050
Richard Smith7ed1bc92014-12-05 22:42:13 +00002051/// \brief If we are loading a relocatable PCH or module file, and the filename
2052/// is not an absolute path, add the system or module root to the beginning of
2053/// the file name.
2054void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) {
2055 // Resolve relative to the base directory, if we have one.
2056 if (!M.BaseDirectory.empty())
2057 return ResolveImportedPath(Filename, M.BaseDirectory);
Guy Benyei11169dd2012-12-18 14:30:41 +00002058}
2059
Richard Smith7ed1bc92014-12-05 22:42:13 +00002060void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002061 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
2062 return;
2063
Richard Smith7ed1bc92014-12-05 22:42:13 +00002064 SmallString<128> Buffer;
2065 llvm::sys::path::append(Buffer, Prefix, Filename);
2066 Filename.assign(Buffer.begin(), Buffer.end());
Guy Benyei11169dd2012-12-18 14:30:41 +00002067}
2068
Richard Smith0f99d6a2015-08-09 08:48:41 +00002069static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) {
2070 switch (ARR) {
2071 case ASTReader::Failure: return true;
2072 case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing);
2073 case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate);
2074 case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch);
2075 case ASTReader::ConfigurationMismatch:
2076 return !(Caps & ASTReader::ARR_ConfigurationMismatch);
2077 case ASTReader::HadErrors: return true;
2078 case ASTReader::Success: return false;
2079 }
2080
2081 llvm_unreachable("unknown ASTReadResult");
2082}
2083
Richard Smith0516b182015-09-08 19:40:14 +00002084ASTReader::ASTReadResult ASTReader::ReadOptionsBlock(
2085 BitstreamCursor &Stream, unsigned ClientLoadCapabilities,
2086 bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener,
2087 std::string &SuggestedPredefines) {
2088 if (Stream.EnterSubBlock(OPTIONS_BLOCK_ID))
2089 return Failure;
2090
2091 // Read all of the records in the options block.
2092 RecordData Record;
2093 ASTReadResult Result = Success;
2094 while (1) {
2095 llvm::BitstreamEntry Entry = Stream.advance();
2096
2097 switch (Entry.Kind) {
2098 case llvm::BitstreamEntry::Error:
2099 case llvm::BitstreamEntry::SubBlock:
2100 return Failure;
2101
2102 case llvm::BitstreamEntry::EndBlock:
2103 return Result;
2104
2105 case llvm::BitstreamEntry::Record:
2106 // The interesting case.
2107 break;
2108 }
2109
2110 // Read and process a record.
2111 Record.clear();
2112 switch ((OptionsRecordTypes)Stream.readRecord(Entry.ID, Record)) {
2113 case LANGUAGE_OPTIONS: {
2114 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2115 if (ParseLanguageOptions(Record, Complain, Listener,
2116 AllowCompatibleConfigurationMismatch))
2117 Result = ConfigurationMismatch;
2118 break;
2119 }
2120
2121 case TARGET_OPTIONS: {
2122 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2123 if (ParseTargetOptions(Record, Complain, Listener,
2124 AllowCompatibleConfigurationMismatch))
2125 Result = ConfigurationMismatch;
2126 break;
2127 }
2128
2129 case DIAGNOSTIC_OPTIONS: {
2130 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
2131 if (!AllowCompatibleConfigurationMismatch &&
2132 ParseDiagnosticOptions(Record, Complain, Listener))
2133 return OutOfDate;
2134 break;
2135 }
2136
2137 case FILE_SYSTEM_OPTIONS: {
2138 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2139 if (!AllowCompatibleConfigurationMismatch &&
2140 ParseFileSystemOptions(Record, Complain, Listener))
2141 Result = ConfigurationMismatch;
2142 break;
2143 }
2144
2145 case HEADER_SEARCH_OPTIONS: {
2146 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2147 if (!AllowCompatibleConfigurationMismatch &&
2148 ParseHeaderSearchOptions(Record, Complain, Listener))
2149 Result = ConfigurationMismatch;
2150 break;
2151 }
2152
2153 case PREPROCESSOR_OPTIONS:
2154 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2155 if (!AllowCompatibleConfigurationMismatch &&
2156 ParsePreprocessorOptions(Record, Complain, Listener,
2157 SuggestedPredefines))
2158 Result = ConfigurationMismatch;
2159 break;
2160 }
2161 }
2162}
2163
Guy Benyei11169dd2012-12-18 14:30:41 +00002164ASTReader::ASTReadResult
2165ASTReader::ReadControlBlock(ModuleFile &F,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002166 SmallVectorImpl<ImportedModule> &Loaded,
Ben Langmuirbeee15e2014-04-14 18:00:01 +00002167 const ModuleFile *ImportedBy,
Guy Benyei11169dd2012-12-18 14:30:41 +00002168 unsigned ClientLoadCapabilities) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00002169 BitstreamCursor &Stream = F.Stream;
Richard Smith8a308ec2015-11-05 00:54:55 +00002170 ASTReadResult Result = Success;
Guy Benyei11169dd2012-12-18 14:30:41 +00002171
2172 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
2173 Error("malformed block record in AST file");
2174 return Failure;
2175 }
2176
2177 // Read all of the records and blocks in the control block.
2178 RecordData Record;
Richard Smitha1825302014-10-23 22:18:29 +00002179 unsigned NumInputs = 0;
2180 unsigned NumUserInputs = 0;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002181 while (1) {
2182 llvm::BitstreamEntry Entry = Stream.advance();
2183
2184 switch (Entry.Kind) {
2185 case llvm::BitstreamEntry::Error:
2186 Error("malformed block record in AST file");
2187 return Failure;
Dmitri Gribenkof430da42014-02-12 10:33:14 +00002188 case llvm::BitstreamEntry::EndBlock: {
2189 // Validate input files.
2190 const HeaderSearchOptions &HSOpts =
2191 PP.getHeaderSearchInfo().getHeaderSearchOpts();
Ben Langmuircb69b572014-03-07 06:40:32 +00002192
Richard Smitha1825302014-10-23 22:18:29 +00002193 // All user input files reside at the index range [0, NumUserInputs), and
Richard Smith0f99d6a2015-08-09 08:48:41 +00002194 // system input files reside at [NumUserInputs, NumInputs). For explicitly
2195 // loaded module files, ignore missing inputs.
2196 if (!DisableValidation && F.Kind != MK_ExplicitModule) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002197 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
Ben Langmuircb69b572014-03-07 06:40:32 +00002198
Dmitri Gribenkof430da42014-02-12 10:33:14 +00002199 // If we are reading a module, we will create a verification timestamp,
2200 // so we verify all input files. Otherwise, verify only user input
2201 // files.
Ben Langmuircb69b572014-03-07 06:40:32 +00002202
2203 unsigned N = NumUserInputs;
2204 if (ValidateSystemInputs ||
Richard Smithe842a472014-10-22 02:05:46 +00002205 (HSOpts.ModulesValidateOncePerBuildSession &&
Ben Langmuiracb803e2014-11-10 22:13:10 +00002206 F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp &&
Richard Smithe842a472014-10-22 02:05:46 +00002207 F.Kind == MK_ImplicitModule))
Ben Langmuircb69b572014-03-07 06:40:32 +00002208 N = NumInputs;
2209
Ben Langmuir3d4417c2014-02-07 17:31:11 +00002210 for (unsigned I = 0; I < N; ++I) {
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00002211 InputFile IF = getInputFile(F, I+1, Complain);
2212 if (!IF.getFile() || IF.isOutOfDate())
Guy Benyei11169dd2012-12-18 14:30:41 +00002213 return OutOfDate;
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00002214 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002215 }
Ben Langmuircb69b572014-03-07 06:40:32 +00002216
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +00002217 if (Listener)
Richard Smith216a3bd2015-08-13 17:57:10 +00002218 Listener->visitModuleFile(F.FileName, F.Kind);
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +00002219
Ben Langmuircb69b572014-03-07 06:40:32 +00002220 if (Listener && Listener->needsInputFileVisitation()) {
2221 unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
2222 : NumUserInputs;
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +00002223 for (unsigned I = 0; I < N; ++I) {
2224 bool IsSystem = I >= NumUserInputs;
2225 InputFileInfo FI = readInputFileInfo(F, I+1);
Richard Smith216a3bd2015-08-13 17:57:10 +00002226 Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden,
2227 F.Kind == MK_ExplicitModule);
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +00002228 }
Ben Langmuircb69b572014-03-07 06:40:32 +00002229 }
2230
Richard Smith8a308ec2015-11-05 00:54:55 +00002231 return Result;
Dmitri Gribenkof430da42014-02-12 10:33:14 +00002232 }
2233
Chris Lattnere7b154b2013-01-19 21:39:22 +00002234 case llvm::BitstreamEntry::SubBlock:
2235 switch (Entry.ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002236 case INPUT_FILES_BLOCK_ID:
2237 F.InputFilesCursor = Stream;
2238 if (Stream.SkipBlock() || // Skip with the main cursor
2239 // Read the abbreviations
2240 ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
2241 Error("malformed block record in AST file");
2242 return Failure;
2243 }
2244 continue;
Richard Smith0516b182015-09-08 19:40:14 +00002245
2246 case OPTIONS_BLOCK_ID:
2247 // If we're reading the first module for this group, check its options
2248 // are compatible with ours. For modules it imports, no further checking
2249 // is required, because we checked them when we built it.
2250 if (Listener && !ImportedBy) {
2251 // Should we allow the configuration of the module file to differ from
2252 // the configuration of the current translation unit in a compatible
2253 // way?
2254 //
2255 // FIXME: Allow this for files explicitly specified with -include-pch.
2256 bool AllowCompatibleConfigurationMismatch =
2257 F.Kind == MK_ExplicitModule;
2258
Richard Smith8a308ec2015-11-05 00:54:55 +00002259 Result = ReadOptionsBlock(Stream, ClientLoadCapabilities,
2260 AllowCompatibleConfigurationMismatch,
2261 *Listener, SuggestedPredefines);
Richard Smith0516b182015-09-08 19:40:14 +00002262 if (Result == Failure) {
2263 Error("malformed block record in AST file");
2264 return Result;
2265 }
2266
Richard Smith8a308ec2015-11-05 00:54:55 +00002267 if (DisableValidation ||
2268 (AllowConfigurationMismatch && Result == ConfigurationMismatch))
2269 Result = Success;
2270
Ben Langmuir9b1e442e2016-02-11 18:54:02 +00002271 // If we can't load the module, exit early since we likely
2272 // will rebuild the module anyway. The stream may be in the
2273 // middle of a block.
2274 if (Result != Success)
Richard Smith0516b182015-09-08 19:40:14 +00002275 return Result;
2276 } else if (Stream.SkipBlock()) {
2277 Error("malformed block record in AST file");
2278 return Failure;
2279 }
2280 continue;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002281
Guy Benyei11169dd2012-12-18 14:30:41 +00002282 default:
Chris Lattnere7b154b2013-01-19 21:39:22 +00002283 if (Stream.SkipBlock()) {
2284 Error("malformed block record in AST file");
2285 return Failure;
2286 }
2287 continue;
Guy Benyei11169dd2012-12-18 14:30:41 +00002288 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00002289
2290 case llvm::BitstreamEntry::Record:
2291 // The interesting case.
2292 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002293 }
2294
2295 // Read and process a record.
2296 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00002297 StringRef Blob;
2298 switch ((ControlRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002299 case METADATA: {
2300 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2301 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
Dmitri Gribenko2228cd32014-02-11 15:40:09 +00002302 Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
2303 : diag::err_pch_version_too_new);
Guy Benyei11169dd2012-12-18 14:30:41 +00002304 return VersionMismatch;
2305 }
2306
Richard Smithe75ee0f2015-08-17 07:13:32 +00002307 bool hasErrors = Record[6];
Guy Benyei11169dd2012-12-18 14:30:41 +00002308 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
2309 Diag(diag::err_pch_with_compiler_errors);
2310 return HadErrors;
2311 }
Argyrios Kyrtzidis70ec1c72016-07-13 20:35:26 +00002312 if (hasErrors) {
2313 Diags.ErrorOccurred = true;
2314 Diags.UncompilableErrorOccurred = true;
2315 Diags.UnrecoverableErrorOccurred = true;
2316 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002317
2318 F.RelocatablePCH = Record[4];
Richard Smith7ed1bc92014-12-05 22:42:13 +00002319 // Relative paths in a relocatable PCH are relative to our sysroot.
2320 if (F.RelocatablePCH)
2321 F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
Guy Benyei11169dd2012-12-18 14:30:41 +00002322
Richard Smithe75ee0f2015-08-17 07:13:32 +00002323 F.HasTimestamps = Record[5];
2324
Guy Benyei11169dd2012-12-18 14:30:41 +00002325 const std::string &CurBranch = getClangFullRepositoryVersion();
Chris Lattner0e6c9402013-01-20 02:38:54 +00002326 StringRef ASTBranch = Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00002327 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2328 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
Dmitri Gribenko2228cd32014-02-11 15:40:09 +00002329 Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
Guy Benyei11169dd2012-12-18 14:30:41 +00002330 return VersionMismatch;
2331 }
2332 break;
2333 }
2334
Ben Langmuir487ea142014-10-23 18:05:36 +00002335 case SIGNATURE:
2336 assert((!F.Signature || F.Signature == Record[0]) && "signature changed");
2337 F.Signature = Record[0];
2338 break;
2339
Guy Benyei11169dd2012-12-18 14:30:41 +00002340 case IMPORTS: {
2341 // Load each of the imported PCH files.
2342 unsigned Idx = 0, N = Record.size();
2343 while (Idx < N) {
2344 // Read information about the AST file.
2345 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
2346 // The import location will be the local one for now; we will adjust
2347 // all import locations of module imports after the global source
Richard Smithb22a1d12016-03-27 20:13:24 +00002348 // location info are setup, in ReadAST.
Guy Benyei11169dd2012-12-18 14:30:41 +00002349 SourceLocation ImportLoc =
Richard Smithb22a1d12016-03-27 20:13:24 +00002350 ReadUntranslatedSourceLocation(Record[Idx++]);
Douglas Gregor7029ce12013-03-19 00:28:20 +00002351 off_t StoredSize = (off_t)Record[Idx++];
2352 time_t StoredModTime = (time_t)Record[Idx++];
Ben Langmuir487ea142014-10-23 18:05:36 +00002353 ASTFileSignature StoredSignature = Record[Idx++];
Richard Smith7ed1bc92014-12-05 22:42:13 +00002354 auto ImportedFile = ReadPath(F, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00002355
Richard Smith0f99d6a2015-08-09 08:48:41 +00002356 // If our client can't cope with us being out of date, we can't cope with
2357 // our dependency being missing.
2358 unsigned Capabilities = ClientLoadCapabilities;
2359 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2360 Capabilities &= ~ARR_Missing;
2361
Guy Benyei11169dd2012-12-18 14:30:41 +00002362 // Load the AST file.
Richard Smith0f99d6a2015-08-09 08:48:41 +00002363 auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F,
2364 Loaded, StoredSize, StoredModTime,
2365 StoredSignature, Capabilities);
2366
2367 // If we diagnosed a problem, produce a backtrace.
2368 if (isDiagnosedResult(Result, Capabilities))
2369 Diag(diag::note_module_file_imported_by)
2370 << F.FileName << !F.ModuleName.empty() << F.ModuleName;
2371
2372 switch (Result) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002373 case Failure: return Failure;
2374 // If we have to ignore the dependency, we'll have to ignore this too.
Douglas Gregor2f1806e2013-03-19 00:38:50 +00002375 case Missing:
Guy Benyei11169dd2012-12-18 14:30:41 +00002376 case OutOfDate: return OutOfDate;
2377 case VersionMismatch: return VersionMismatch;
2378 case ConfigurationMismatch: return ConfigurationMismatch;
2379 case HadErrors: return HadErrors;
2380 case Success: break;
2381 }
2382 }
2383 break;
2384 }
2385
Guy Benyei11169dd2012-12-18 14:30:41 +00002386 case ORIGINAL_FILE:
2387 F.OriginalSourceFileID = FileID::get(Record[0]);
Chris Lattner0e6c9402013-01-20 02:38:54 +00002388 F.ActualOriginalSourceFileName = Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00002389 F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
Richard Smith7ed1bc92014-12-05 22:42:13 +00002390 ResolveImportedPath(F, F.OriginalSourceFileName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002391 break;
2392
2393 case ORIGINAL_FILE_ID:
2394 F.OriginalSourceFileID = FileID::get(Record[0]);
2395 break;
2396
2397 case ORIGINAL_PCH_DIR:
Chris Lattner0e6c9402013-01-20 02:38:54 +00002398 F.OriginalDir = Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00002399 break;
2400
Ben Langmuirbeee15e2014-04-14 18:00:01 +00002401 case MODULE_NAME:
2402 F.ModuleName = Blob;
Ben Langmuir4f5212a2014-04-14 22:12:44 +00002403 if (Listener)
2404 Listener->ReadModuleName(F.ModuleName);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00002405 break;
2406
Richard Smith223d3f22014-12-06 03:21:08 +00002407 case MODULE_DIRECTORY: {
2408 assert(!F.ModuleName.empty() &&
2409 "MODULE_DIRECTORY found before MODULE_NAME");
2410 // If we've already loaded a module map file covering this module, we may
2411 // have a better path for it (relative to the current build).
2412 Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
2413 if (M && M->Directory) {
2414 // If we're implicitly loading a module, the base directory can't
2415 // change between the build and use.
2416 if (F.Kind != MK_ExplicitModule) {
2417 const DirectoryEntry *BuildDir =
2418 PP.getFileManager().getDirectory(Blob);
2419 if (!BuildDir || BuildDir != M->Directory) {
2420 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2421 Diag(diag::err_imported_module_relocated)
2422 << F.ModuleName << Blob << M->Directory->getName();
2423 return OutOfDate;
2424 }
2425 }
2426 F.BaseDirectory = M->Directory->getName();
2427 } else {
2428 F.BaseDirectory = Blob;
2429 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00002430 break;
Richard Smith223d3f22014-12-06 03:21:08 +00002431 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00002432
Ben Langmuirbeee15e2014-04-14 18:00:01 +00002433 case MODULE_MAP_FILE:
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00002434 if (ASTReadResult Result =
2435 ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
2436 return Result;
Ben Langmuir264ea152014-11-08 00:06:39 +00002437 break;
2438
Justin Bognerca9c0cc2015-06-21 20:32:36 +00002439 case INPUT_FILE_OFFSETS:
Richard Smitha1825302014-10-23 22:18:29 +00002440 NumInputs = Record[0];
2441 NumUserInputs = Record[1];
Justin Bogner4c183242015-06-21 20:32:40 +00002442 F.InputFileOffsets =
2443 (const llvm::support::unaligned_uint64_t *)Blob.data();
Richard Smitha1825302014-10-23 22:18:29 +00002444 F.InputFilesLoaded.resize(NumInputs);
Guy Benyei11169dd2012-12-18 14:30:41 +00002445 break;
2446 }
2447 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002448}
2449
Ben Langmuir2c9af442014-04-10 17:57:43 +00002450ASTReader::ASTReadResult
2451ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00002452 BitstreamCursor &Stream = F.Stream;
Guy Benyei11169dd2012-12-18 14:30:41 +00002453
2454 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
2455 Error("malformed block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002456 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002457 }
2458
2459 // Read all of the records and blocks for the AST file.
2460 RecordData Record;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002461 while (1) {
2462 llvm::BitstreamEntry Entry = Stream.advance();
2463
2464 switch (Entry.Kind) {
2465 case llvm::BitstreamEntry::Error:
2466 Error("error at end of module block in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002467 return Failure;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002468 case llvm::BitstreamEntry::EndBlock: {
Richard Smithc0fbba72013-04-03 22:49:41 +00002469 // Outside of C++, we do not store a lookup map for the translation unit.
2470 // Instead, mark it as needing a lookup map to be built if this module
2471 // contains any declarations lexically within it (which it always does!).
2472 // This usually has no cost, since we very rarely need the lookup map for
2473 // the translation unit outside C++.
Guy Benyei11169dd2012-12-18 14:30:41 +00002474 DeclContext *DC = Context.getTranslationUnitDecl();
Richard Smithc0fbba72013-04-03 22:49:41 +00002475 if (DC->hasExternalLexicalStorage() &&
2476 !getContext().getLangOpts().CPlusPlus)
Guy Benyei11169dd2012-12-18 14:30:41 +00002477 DC->setMustBuildLookupTable();
Chris Lattnere7b154b2013-01-19 21:39:22 +00002478
Ben Langmuir2c9af442014-04-10 17:57:43 +00002479 return Success;
Guy Benyei11169dd2012-12-18 14:30:41 +00002480 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00002481 case llvm::BitstreamEntry::SubBlock:
2482 switch (Entry.ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002483 case DECLTYPES_BLOCK_ID:
2484 // We lazily load the decls block, but we want to set up the
2485 // DeclsCursor cursor to point into it. Clone our current bitcode
2486 // cursor to it, enter the block and read the abbrevs in that block.
2487 // With the main cursor, we just skip over it.
2488 F.DeclsCursor = Stream;
2489 if (Stream.SkipBlock() || // Skip with the main cursor.
2490 // Read the abbrevs.
2491 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
2492 Error("malformed block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002493 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002494 }
2495 break;
Richard Smithb9eab6d2014-03-20 19:44:17 +00002496
Guy Benyei11169dd2012-12-18 14:30:41 +00002497 case PREPROCESSOR_BLOCK_ID:
2498 F.MacroCursor = Stream;
2499 if (!PP.getExternalSource())
2500 PP.setExternalSource(this);
Chris Lattnere7b154b2013-01-19 21:39:22 +00002501
Guy Benyei11169dd2012-12-18 14:30:41 +00002502 if (Stream.SkipBlock() ||
2503 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
2504 Error("malformed block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002505 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002506 }
2507 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
2508 break;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002509
Guy Benyei11169dd2012-12-18 14:30:41 +00002510 case PREPROCESSOR_DETAIL_BLOCK_ID:
2511 F.PreprocessorDetailCursor = Stream;
2512 if (Stream.SkipBlock() ||
Chris Lattnere7b154b2013-01-19 21:39:22 +00002513 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
Guy Benyei11169dd2012-12-18 14:30:41 +00002514 PREPROCESSOR_DETAIL_BLOCK_ID)) {
Chris Lattnere7b154b2013-01-19 21:39:22 +00002515 Error("malformed preprocessor detail record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002516 return Failure;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002517 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002518 F.PreprocessorDetailStartOffset
Chris Lattnere7b154b2013-01-19 21:39:22 +00002519 = F.PreprocessorDetailCursor.GetCurrentBitNo();
2520
Guy Benyei11169dd2012-12-18 14:30:41 +00002521 if (!PP.getPreprocessingRecord())
2522 PP.createPreprocessingRecord();
2523 if (!PP.getPreprocessingRecord()->getExternalSource())
2524 PP.getPreprocessingRecord()->SetExternalSource(*this);
2525 break;
2526
2527 case SOURCE_MANAGER_BLOCK_ID:
2528 if (ReadSourceManagerBlock(F))
Ben Langmuir2c9af442014-04-10 17:57:43 +00002529 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002530 break;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002531
Guy Benyei11169dd2012-12-18 14:30:41 +00002532 case SUBMODULE_BLOCK_ID:
Ben Langmuir2c9af442014-04-10 17:57:43 +00002533 if (ASTReadResult Result = ReadSubmoduleBlock(F, ClientLoadCapabilities))
2534 return Result;
Guy Benyei11169dd2012-12-18 14:30:41 +00002535 break;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002536
Guy Benyei11169dd2012-12-18 14:30:41 +00002537 case COMMENTS_BLOCK_ID: {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00002538 BitstreamCursor C = Stream;
Guy Benyei11169dd2012-12-18 14:30:41 +00002539 if (Stream.SkipBlock() ||
2540 ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
2541 Error("malformed comments block in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002542 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002543 }
2544 CommentsCursors.push_back(std::make_pair(C, &F));
2545 break;
2546 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00002547
Guy Benyei11169dd2012-12-18 14:30:41 +00002548 default:
Chris Lattnere7b154b2013-01-19 21:39:22 +00002549 if (Stream.SkipBlock()) {
2550 Error("malformed block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002551 return Failure;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002552 }
2553 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002554 }
2555 continue;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002556
2557 case llvm::BitstreamEntry::Record:
2558 // The interesting case.
2559 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002560 }
2561
2562 // Read and process a record.
2563 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00002564 StringRef Blob;
2565 switch ((ASTRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002566 default: // Default behavior: ignore.
2567 break;
2568
2569 case TYPE_OFFSET: {
2570 if (F.LocalNumTypes != 0) {
2571 Error("duplicate TYPE_OFFSET record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002572 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002573 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00002574 F.TypeOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002575 F.LocalNumTypes = Record[0];
2576 unsigned LocalBaseTypeIndex = Record[1];
2577 F.BaseTypeIndex = getTotalNumTypes();
2578
2579 if (F.LocalNumTypes > 0) {
2580 // Introduce the global -> local mapping for types within this module.
2581 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
2582
2583 // Introduce the local -> global mapping for types within this module.
2584 F.TypeRemap.insertOrReplace(
2585 std::make_pair(LocalBaseTypeIndex,
2586 F.BaseTypeIndex - LocalBaseTypeIndex));
Ben Langmuir52ca6782014-10-20 16:27:32 +00002587
2588 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
Guy Benyei11169dd2012-12-18 14:30:41 +00002589 }
2590 break;
2591 }
2592
2593 case DECL_OFFSET: {
2594 if (F.LocalNumDecls != 0) {
2595 Error("duplicate DECL_OFFSET record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002596 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002597 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00002598 F.DeclOffsets = (const DeclOffset *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002599 F.LocalNumDecls = Record[0];
2600 unsigned LocalBaseDeclID = Record[1];
2601 F.BaseDeclID = getTotalNumDecls();
2602
2603 if (F.LocalNumDecls > 0) {
2604 // Introduce the global -> local mapping for declarations within this
2605 // module.
2606 GlobalDeclMap.insert(
2607 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
2608
2609 // Introduce the local -> global mapping for declarations within this
2610 // module.
2611 F.DeclRemap.insertOrReplace(
2612 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
2613
2614 // Introduce the global -> local mapping for declarations within this
2615 // module.
2616 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
Ben Langmuirfe971d92014-08-16 04:54:18 +00002617
Ben Langmuir52ca6782014-10-20 16:27:32 +00002618 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
2619 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002620 break;
2621 }
2622
2623 case TU_UPDATE_LEXICAL: {
2624 DeclContext *TU = Context.getTranslationUnitDecl();
Richard Smith82f8fcd2015-08-06 22:07:25 +00002625 LexicalContents Contents(
2626 reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
2627 Blob.data()),
2628 static_cast<unsigned int>(Blob.size() / 4));
2629 TULexicalDecls.push_back(std::make_pair(&F, Contents));
Guy Benyei11169dd2012-12-18 14:30:41 +00002630 TU->setHasExternalLexicalStorage(true);
2631 break;
2632 }
2633
2634 case UPDATE_VISIBLE: {
2635 unsigned Idx = 0;
2636 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
Richard Smith0f4e2c42015-08-06 04:23:48 +00002637 auto *Data = (const unsigned char*)Blob.data();
Richard Smithd88a7f12015-09-01 20:35:42 +00002638 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data});
Richard Smith0f4e2c42015-08-06 04:23:48 +00002639 // If we've already loaded the decl, perform the updates when we finish
2640 // loading this block.
2641 if (Decl *D = GetExistingDecl(ID))
2642 PendingUpdateRecords.push_back(std::make_pair(ID, D));
Guy Benyei11169dd2012-12-18 14:30:41 +00002643 break;
2644 }
2645
2646 case IDENTIFIER_TABLE:
Chris Lattner0e6c9402013-01-20 02:38:54 +00002647 F.IdentifierTableData = Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002648 if (Record[0]) {
Justin Bognerda4e6502014-04-14 16:34:29 +00002649 F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
2650 (const unsigned char *)F.IdentifierTableData + Record[0],
2651 (const unsigned char *)F.IdentifierTableData + sizeof(uint32_t),
2652 (const unsigned char *)F.IdentifierTableData,
2653 ASTIdentifierLookupTrait(*this, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002654
2655 PP.getIdentifierTable().setExternalIdentifierLookup(this);
2656 }
2657 break;
2658
2659 case IDENTIFIER_OFFSET: {
2660 if (F.LocalNumIdentifiers != 0) {
2661 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002662 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002663 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00002664 F.IdentifierOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002665 F.LocalNumIdentifiers = Record[0];
2666 unsigned LocalBaseIdentifierID = Record[1];
2667 F.BaseIdentifierID = getTotalNumIdentifiers();
2668
2669 if (F.LocalNumIdentifiers > 0) {
2670 // Introduce the global -> local mapping for identifiers within this
2671 // module.
2672 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
2673 &F));
2674
2675 // Introduce the local -> global mapping for identifiers within this
2676 // module.
2677 F.IdentifierRemap.insertOrReplace(
2678 std::make_pair(LocalBaseIdentifierID,
2679 F.BaseIdentifierID - LocalBaseIdentifierID));
Ben Langmuirfe971d92014-08-16 04:54:18 +00002680
Ben Langmuir52ca6782014-10-20 16:27:32 +00002681 IdentifiersLoaded.resize(IdentifiersLoaded.size()
2682 + F.LocalNumIdentifiers);
2683 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002684 break;
2685 }
2686
Richard Smith33e0f7e2015-07-22 02:08:40 +00002687 case INTERESTING_IDENTIFIERS:
2688 F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end());
2689 break;
2690
Ben Langmuir332aafe2014-01-31 01:06:56 +00002691 case EAGERLY_DESERIALIZED_DECLS:
Richard Smith9e2341d2015-03-23 03:25:59 +00002692 // FIXME: Skip reading this record if our ASTConsumer doesn't care
2693 // about "interesting" decls (for instance, if we're building a module).
Guy Benyei11169dd2012-12-18 14:30:41 +00002694 for (unsigned I = 0, N = Record.size(); I != N; ++I)
Ben Langmuir332aafe2014-01-31 01:06:56 +00002695 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
Guy Benyei11169dd2012-12-18 14:30:41 +00002696 break;
2697
2698 case SPECIAL_TYPES:
Douglas Gregor44180f82013-02-01 23:45:03 +00002699 if (SpecialTypes.empty()) {
2700 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2701 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
2702 break;
2703 }
2704
2705 if (SpecialTypes.size() != Record.size()) {
2706 Error("invalid special-types record");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002707 return Failure;
Douglas Gregor44180f82013-02-01 23:45:03 +00002708 }
2709
2710 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2711 serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
2712 if (!SpecialTypes[I])
2713 SpecialTypes[I] = ID;
2714 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
2715 // merge step?
2716 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002717 break;
2718
2719 case STATISTICS:
2720 TotalNumStatements += Record[0];
2721 TotalNumMacros += Record[1];
2722 TotalLexicalDeclContexts += Record[2];
2723 TotalVisibleDeclContexts += Record[3];
2724 break;
2725
2726 case UNUSED_FILESCOPED_DECLS:
2727 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2728 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
2729 break;
2730
2731 case DELEGATING_CTORS:
2732 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2733 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
2734 break;
2735
2736 case WEAK_UNDECLARED_IDENTIFIERS:
2737 if (Record.size() % 4 != 0) {
2738 Error("invalid weak identifiers record");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002739 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002740 }
2741
2742 // FIXME: Ignore weak undeclared identifiers from non-original PCH
2743 // files. This isn't the way to do it :)
2744 WeakUndeclaredIdentifiers.clear();
2745
2746 // Translate the weak, undeclared identifiers into global IDs.
2747 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
2748 WeakUndeclaredIdentifiers.push_back(
2749 getGlobalIdentifierID(F, Record[I++]));
2750 WeakUndeclaredIdentifiers.push_back(
2751 getGlobalIdentifierID(F, Record[I++]));
2752 WeakUndeclaredIdentifiers.push_back(
2753 ReadSourceLocation(F, Record, I).getRawEncoding());
2754 WeakUndeclaredIdentifiers.push_back(Record[I++]);
2755 }
2756 break;
2757
Guy Benyei11169dd2012-12-18 14:30:41 +00002758 case SELECTOR_OFFSETS: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00002759 F.SelectorOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002760 F.LocalNumSelectors = Record[0];
2761 unsigned LocalBaseSelectorID = Record[1];
2762 F.BaseSelectorID = getTotalNumSelectors();
2763
2764 if (F.LocalNumSelectors > 0) {
2765 // Introduce the global -> local mapping for selectors within this
2766 // module.
2767 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
2768
2769 // Introduce the local -> global mapping for selectors within this
2770 // module.
2771 F.SelectorRemap.insertOrReplace(
2772 std::make_pair(LocalBaseSelectorID,
2773 F.BaseSelectorID - LocalBaseSelectorID));
Ben Langmuir52ca6782014-10-20 16:27:32 +00002774
2775 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
Guy Benyei11169dd2012-12-18 14:30:41 +00002776 }
2777 break;
2778 }
2779
2780 case METHOD_POOL:
Chris Lattner0e6c9402013-01-20 02:38:54 +00002781 F.SelectorLookupTableData = (const unsigned char *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002782 if (Record[0])
2783 F.SelectorLookupTable
2784 = ASTSelectorLookupTable::Create(
2785 F.SelectorLookupTableData + Record[0],
2786 F.SelectorLookupTableData,
2787 ASTSelectorLookupTrait(*this, F));
2788 TotalNumMethodPoolEntries += Record[1];
2789 break;
2790
2791 case REFERENCED_SELECTOR_POOL:
2792 if (!Record.empty()) {
2793 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
2794 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
2795 Record[Idx++]));
2796 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
2797 getRawEncoding());
2798 }
2799 }
2800 break;
2801
2802 case PP_COUNTER_VALUE:
2803 if (!Record.empty() && Listener)
2804 Listener->ReadCounter(F, Record[0]);
2805 break;
2806
2807 case FILE_SORTED_DECLS:
Chris Lattner0e6c9402013-01-20 02:38:54 +00002808 F.FileSortedDecls = (const DeclID *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002809 F.NumFileSortedDecls = Record[0];
2810 break;
2811
2812 case SOURCE_LOCATION_OFFSETS: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00002813 F.SLocEntryOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002814 F.LocalNumSLocEntries = Record[0];
2815 unsigned SLocSpaceSize = Record[1];
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00002816 std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
Ben Langmuir52ca6782014-10-20 16:27:32 +00002817 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
Guy Benyei11169dd2012-12-18 14:30:41 +00002818 SLocSpaceSize);
Richard Smith78d81ec2015-08-12 22:25:24 +00002819 if (!F.SLocEntryBaseID) {
2820 Error("ran out of source locations");
2821 break;
2822 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002823 // Make our entry in the range map. BaseID is negative and growing, so
2824 // we invert it. Because we invert it, though, we need the other end of
2825 // the range.
2826 unsigned RangeStart =
2827 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
2828 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
2829 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
2830
2831 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
2832 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
2833 GlobalSLocOffsetMap.insert(
2834 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
2835 - SLocSpaceSize,&F));
2836
2837 // Initialize the remapping table.
2838 // Invalid stays invalid.
Richard Smithb9eab6d2014-03-20 19:44:17 +00002839 F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
Guy Benyei11169dd2012-12-18 14:30:41 +00002840 // This module. Base was 2 when being compiled.
Richard Smithb9eab6d2014-03-20 19:44:17 +00002841 F.SLocRemap.insertOrReplace(std::make_pair(2U,
Guy Benyei11169dd2012-12-18 14:30:41 +00002842 static_cast<int>(F.SLocEntryBaseOffset - 2)));
2843
2844 TotalNumSLocEntries += F.LocalNumSLocEntries;
2845 break;
2846 }
2847
2848 case MODULE_OFFSET_MAP: {
2849 // Additional remapping information.
Chris Lattner0e6c9402013-01-20 02:38:54 +00002850 const unsigned char *Data = (const unsigned char*)Blob.data();
2851 const unsigned char *DataEnd = Data + Blob.size();
Richard Smithb9eab6d2014-03-20 19:44:17 +00002852
2853 // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
2854 if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
2855 F.SLocRemap.insert(std::make_pair(0U, 0));
2856 F.SLocRemap.insert(std::make_pair(2U, 1));
2857 }
2858
Guy Benyei11169dd2012-12-18 14:30:41 +00002859 // Continuous range maps we may be updating in our module.
Ben Langmuir785180e2014-10-20 16:27:30 +00002860 typedef ContinuousRangeMap<uint32_t, int, 2>::Builder
2861 RemapBuilder;
2862 RemapBuilder SLocRemap(F.SLocRemap);
2863 RemapBuilder IdentifierRemap(F.IdentifierRemap);
2864 RemapBuilder MacroRemap(F.MacroRemap);
2865 RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
2866 RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
2867 RemapBuilder SelectorRemap(F.SelectorRemap);
2868 RemapBuilder DeclRemap(F.DeclRemap);
2869 RemapBuilder TypeRemap(F.TypeRemap);
Guy Benyei11169dd2012-12-18 14:30:41 +00002870
Richard Smithd8879c82015-08-24 21:59:32 +00002871 while (Data < DataEnd) {
Justin Bogner57ba0b22014-03-28 22:03:24 +00002872 using namespace llvm::support;
2873 uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data);
Guy Benyei11169dd2012-12-18 14:30:41 +00002874 StringRef Name = StringRef((const char*)Data, Len);
2875 Data += Len;
2876 ModuleFile *OM = ModuleMgr.lookup(Name);
2877 if (!OM) {
2878 Error("SourceLocation remap refers to unknown module");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002879 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002880 }
2881
Justin Bogner57ba0b22014-03-28 22:03:24 +00002882 uint32_t SLocOffset =
2883 endian::readNext<uint32_t, little, unaligned>(Data);
2884 uint32_t IdentifierIDOffset =
2885 endian::readNext<uint32_t, little, unaligned>(Data);
2886 uint32_t MacroIDOffset =
2887 endian::readNext<uint32_t, little, unaligned>(Data);
2888 uint32_t PreprocessedEntityIDOffset =
2889 endian::readNext<uint32_t, little, unaligned>(Data);
2890 uint32_t SubmoduleIDOffset =
2891 endian::readNext<uint32_t, little, unaligned>(Data);
2892 uint32_t SelectorIDOffset =
2893 endian::readNext<uint32_t, little, unaligned>(Data);
2894 uint32_t DeclIDOffset =
2895 endian::readNext<uint32_t, little, unaligned>(Data);
2896 uint32_t TypeIndexOffset =
2897 endian::readNext<uint32_t, little, unaligned>(Data);
2898
Ben Langmuir785180e2014-10-20 16:27:30 +00002899 uint32_t None = std::numeric_limits<uint32_t>::max();
2900
2901 auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
2902 RemapBuilder &Remap) {
2903 if (Offset != None)
2904 Remap.insert(std::make_pair(Offset,
2905 static_cast<int>(BaseOffset - Offset)));
2906 };
2907 mapOffset(SLocOffset, OM->SLocEntryBaseOffset, SLocRemap);
2908 mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap);
2909 mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
2910 mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
2911 PreprocessedEntityRemap);
2912 mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
2913 mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
2914 mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap);
2915 mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap);
Guy Benyei11169dd2012-12-18 14:30:41 +00002916
2917 // Global -> local mappings.
2918 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
2919 }
2920 break;
2921 }
2922
2923 case SOURCE_MANAGER_LINE_TABLE:
2924 if (ParseLineTable(F, Record))
Ben Langmuir2c9af442014-04-10 17:57:43 +00002925 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002926 break;
2927
2928 case SOURCE_LOCATION_PRELOADS: {
2929 // Need to transform from the local view (1-based IDs) to the global view,
2930 // which is based off F.SLocEntryBaseID.
2931 if (!F.PreloadSLocEntries.empty()) {
2932 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002933 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002934 }
2935
2936 F.PreloadSLocEntries.swap(Record);
2937 break;
2938 }
2939
2940 case EXT_VECTOR_DECLS:
2941 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2942 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
2943 break;
2944
2945 case VTABLE_USES:
2946 if (Record.size() % 3 != 0) {
2947 Error("Invalid VTABLE_USES record");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002948 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002949 }
2950
2951 // Later tables overwrite earlier ones.
2952 // FIXME: Modules will have some trouble with this. This is clearly not
2953 // the right way to do this.
2954 VTableUses.clear();
2955
2956 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
2957 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
2958 VTableUses.push_back(
2959 ReadSourceLocation(F, Record, Idx).getRawEncoding());
2960 VTableUses.push_back(Record[Idx++]);
2961 }
2962 break;
2963
Guy Benyei11169dd2012-12-18 14:30:41 +00002964 case PENDING_IMPLICIT_INSTANTIATIONS:
2965 if (PendingInstantiations.size() % 2 != 0) {
2966 Error("Invalid existing PendingInstantiations");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002967 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002968 }
2969
2970 if (Record.size() % 2 != 0) {
2971 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002972 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002973 }
2974
2975 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2976 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
2977 PendingInstantiations.push_back(
2978 ReadSourceLocation(F, Record, I).getRawEncoding());
2979 }
2980 break;
2981
2982 case SEMA_DECL_REFS:
Richard Smith3d8e97e2013-10-18 06:54:39 +00002983 if (Record.size() != 2) {
2984 Error("Invalid SEMA_DECL_REFS block");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002985 return Failure;
Richard Smith3d8e97e2013-10-18 06:54:39 +00002986 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002987 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2988 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
2989 break;
2990
2991 case PPD_ENTITIES_OFFSETS: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00002992 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
2993 assert(Blob.size() % sizeof(PPEntityOffset) == 0);
2994 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
Guy Benyei11169dd2012-12-18 14:30:41 +00002995
2996 unsigned LocalBasePreprocessedEntityID = Record[0];
2997
2998 unsigned StartingID;
2999 if (!PP.getPreprocessingRecord())
3000 PP.createPreprocessingRecord();
3001 if (!PP.getPreprocessingRecord()->getExternalSource())
3002 PP.getPreprocessingRecord()->SetExternalSource(*this);
3003 StartingID
3004 = PP.getPreprocessingRecord()
Ben Langmuir52ca6782014-10-20 16:27:32 +00003005 ->allocateLoadedEntities(F.NumPreprocessedEntities);
Guy Benyei11169dd2012-12-18 14:30:41 +00003006 F.BasePreprocessedEntityID = StartingID;
3007
3008 if (F.NumPreprocessedEntities > 0) {
3009 // Introduce the global -> local mapping for preprocessed entities in
3010 // this module.
3011 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
3012
3013 // Introduce the local -> global mapping for preprocessed entities in
3014 // this module.
3015 F.PreprocessedEntityRemap.insertOrReplace(
3016 std::make_pair(LocalBasePreprocessedEntityID,
3017 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
3018 }
3019
3020 break;
3021 }
3022
3023 case DECL_UPDATE_OFFSETS: {
3024 if (Record.size() % 2 != 0) {
3025 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003026 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003027 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00003028 for (unsigned I = 0, N = Record.size(); I != N; I += 2) {
3029 GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
3030 DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1]));
3031
3032 // If we've already loaded the decl, perform the updates when we finish
3033 // loading this block.
3034 if (Decl *D = GetExistingDecl(ID))
3035 PendingUpdateRecords.push_back(std::make_pair(ID, D));
3036 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003037 break;
3038 }
3039
Guy Benyei11169dd2012-12-18 14:30:41 +00003040 case OBJC_CATEGORIES_MAP: {
3041 if (F.LocalNumObjCCategoriesInMap != 0) {
3042 Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003043 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003044 }
3045
3046 F.LocalNumObjCCategoriesInMap = Record[0];
Chris Lattner0e6c9402013-01-20 02:38:54 +00003047 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003048 break;
3049 }
3050
3051 case OBJC_CATEGORIES:
3052 F.ObjCCategories.swap(Record);
3053 break;
Richard Smithc2bb8182015-03-24 06:36:48 +00003054
Guy Benyei11169dd2012-12-18 14:30:41 +00003055 case DIAG_PRAGMA_MAPPINGS:
3056 if (F.PragmaDiagMappings.empty())
3057 F.PragmaDiagMappings.swap(Record);
3058 else
3059 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
3060 Record.begin(), Record.end());
3061 break;
3062
3063 case CUDA_SPECIAL_DECL_REFS:
3064 // Later tables overwrite earlier ones.
3065 // FIXME: Modules will have trouble with this.
3066 CUDASpecialDeclRefs.clear();
3067 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3068 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3069 break;
3070
3071 case HEADER_SEARCH_TABLE: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00003072 F.HeaderFileInfoTableData = Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003073 F.LocalNumHeaderFileInfos = Record[1];
Guy Benyei11169dd2012-12-18 14:30:41 +00003074 if (Record[0]) {
3075 F.HeaderFileInfoTable
3076 = HeaderFileInfoLookupTable::Create(
3077 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
3078 (const unsigned char *)F.HeaderFileInfoTableData,
3079 HeaderFileInfoTrait(*this, F,
3080 &PP.getHeaderSearchInfo(),
Chris Lattner0e6c9402013-01-20 02:38:54 +00003081 Blob.data() + Record[2]));
Guy Benyei11169dd2012-12-18 14:30:41 +00003082
3083 PP.getHeaderSearchInfo().SetExternalSource(this);
3084 if (!PP.getHeaderSearchInfo().getExternalLookup())
3085 PP.getHeaderSearchInfo().SetExternalLookup(this);
3086 }
3087 break;
3088 }
3089
3090 case FP_PRAGMA_OPTIONS:
3091 // Later tables overwrite earlier ones.
3092 FPPragmaOptions.swap(Record);
3093 break;
3094
3095 case OPENCL_EXTENSIONS:
3096 // Later tables overwrite earlier ones.
3097 OpenCLExtensions.swap(Record);
3098 break;
3099
3100 case TENTATIVE_DEFINITIONS:
3101 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3102 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
3103 break;
3104
3105 case KNOWN_NAMESPACES:
3106 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3107 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
3108 break;
Nick Lewycky8334af82013-01-26 00:35:08 +00003109
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00003110 case UNDEFINED_BUT_USED:
3111 if (UndefinedButUsed.size() % 2 != 0) {
3112 Error("Invalid existing UndefinedButUsed");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003113 return Failure;
Nick Lewycky8334af82013-01-26 00:35:08 +00003114 }
3115
3116 if (Record.size() % 2 != 0) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00003117 Error("invalid undefined-but-used record");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003118 return Failure;
Nick Lewycky8334af82013-01-26 00:35:08 +00003119 }
3120 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00003121 UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
3122 UndefinedButUsed.push_back(
Nick Lewycky8334af82013-01-26 00:35:08 +00003123 ReadSourceLocation(F, Record, I).getRawEncoding());
3124 }
3125 break;
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00003126 case DELETE_EXPRS_TO_ANALYZE:
3127 for (unsigned I = 0, N = Record.size(); I != N;) {
3128 DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++]));
3129 const uint64_t Count = Record[I++];
3130 DelayedDeleteExprs.push_back(Count);
3131 for (uint64_t C = 0; C < Count; ++C) {
3132 DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding());
3133 bool IsArrayForm = Record[I++] == 1;
3134 DelayedDeleteExprs.push_back(IsArrayForm);
3135 }
3136 }
3137 break;
Nick Lewycky8334af82013-01-26 00:35:08 +00003138
Guy Benyei11169dd2012-12-18 14:30:41 +00003139 case IMPORTED_MODULES: {
Richard Smithe842a472014-10-22 02:05:46 +00003140 if (F.Kind != MK_ImplicitModule && F.Kind != MK_ExplicitModule) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003141 // If we aren't loading a module (which has its own exports), make
3142 // all of the imported modules visible.
3143 // FIXME: Deal with macros-only imports.
Richard Smith56be7542014-03-21 00:33:59 +00003144 for (unsigned I = 0, N = Record.size(); I != N; /**/) {
3145 unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
3146 SourceLocation Loc = ReadSourceLocation(F, Record, I);
3147 if (GlobalID)
Aaron Ballman4f45b712014-03-21 15:22:56 +00003148 ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
Guy Benyei11169dd2012-12-18 14:30:41 +00003149 }
3150 }
3151 break;
3152 }
3153
Guy Benyei11169dd2012-12-18 14:30:41 +00003154 case MACRO_OFFSET: {
3155 if (F.LocalNumMacros != 0) {
3156 Error("duplicate MACRO_OFFSET record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003157 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003158 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00003159 F.MacroOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003160 F.LocalNumMacros = Record[0];
3161 unsigned LocalBaseMacroID = Record[1];
3162 F.BaseMacroID = getTotalNumMacros();
3163
3164 if (F.LocalNumMacros > 0) {
3165 // Introduce the global -> local mapping for macros within this module.
3166 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3167
3168 // Introduce the local -> global mapping for macros within this module.
3169 F.MacroRemap.insertOrReplace(
3170 std::make_pair(LocalBaseMacroID,
3171 F.BaseMacroID - LocalBaseMacroID));
Ben Langmuir52ca6782014-10-20 16:27:32 +00003172
3173 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
Guy Benyei11169dd2012-12-18 14:30:41 +00003174 }
3175 break;
3176 }
3177
Richard Smithe40f2ba2013-08-07 21:41:30 +00003178 case LATE_PARSED_TEMPLATE: {
3179 LateParsedTemplates.append(Record.begin(), Record.end());
3180 break;
3181 }
Dario Domizioli13a0a382014-05-23 12:13:25 +00003182
3183 case OPTIMIZE_PRAGMA_OPTIONS:
3184 if (Record.size() != 1) {
3185 Error("invalid pragma optimize record");
3186 return Failure;
3187 }
3188 OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
3189 break;
Nico Weber72889432014-09-06 01:25:55 +00003190
Nico Weber779355f2016-03-02 23:22:00 +00003191 case MSSTRUCT_PRAGMA_OPTIONS:
3192 if (Record.size() != 1) {
3193 Error("invalid pragma ms_struct record");
3194 return Failure;
3195 }
3196 PragmaMSStructState = Record[0];
3197 break;
3198
Nico Weber42932312016-03-03 00:17:35 +00003199 case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS:
3200 if (Record.size() != 2) {
3201 Error("invalid pragma ms_struct record");
3202 return Failure;
3203 }
3204 PragmaMSPointersToMembersState = Record[0];
3205 PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]);
3206 break;
3207
Nico Weber72889432014-09-06 01:25:55 +00003208 case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
3209 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3210 UnusedLocalTypedefNameCandidates.push_back(
3211 getGlobalDeclID(F, Record[I]));
3212 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003213 }
3214 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003215}
3216
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003217ASTReader::ASTReadResult
3218ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
3219 const ModuleFile *ImportedBy,
3220 unsigned ClientLoadCapabilities) {
3221 unsigned Idx = 0;
Richard Smith7ed1bc92014-12-05 22:42:13 +00003222 F.ModuleMapPath = ReadPath(F, Record, Idx);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003223
Richard Smithe842a472014-10-22 02:05:46 +00003224 if (F.Kind == MK_ExplicitModule) {
3225 // For an explicitly-loaded module, we don't care whether the original
3226 // module map file exists or matches.
3227 return Success;
3228 }
3229
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003230 // Try to resolve ModuleName in the current header search context and
3231 // verify that it is found in the same module map file as we saved. If the
3232 // top-level AST file is a main file, skip this check because there is no
3233 // usable header search context.
3234 assert(!F.ModuleName.empty() &&
Richard Smithe842a472014-10-22 02:05:46 +00003235 "MODULE_NAME should come before MODULE_MAP_FILE");
3236 if (F.Kind == MK_ImplicitModule &&
3237 (*ModuleMgr.begin())->Kind != MK_MainFile) {
3238 // An implicitly-loaded module file should have its module listed in some
3239 // module map file that we've already loaded.
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003240 Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
Richard Smithe842a472014-10-22 02:05:46 +00003241 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
3242 const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr;
3243 if (!ModMap) {
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003244 assert(ImportedBy && "top-level import should be verified");
Richard Smith0f99d6a2015-08-09 08:48:41 +00003245 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) {
3246 if (auto *ASTFE = M ? M->getASTFile() : nullptr)
3247 // This module was defined by an imported (explicit) module.
3248 Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName
3249 << ASTFE->getName();
3250 else
3251 // This module was built with a different module map.
3252 Diag(diag::err_imported_module_not_found)
3253 << F.ModuleName << F.FileName << ImportedBy->FileName
3254 << F.ModuleMapPath;
3255 }
3256 return OutOfDate;
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003257 }
3258
Richard Smithe842a472014-10-22 02:05:46 +00003259 assert(M->Name == F.ModuleName && "found module with different name");
3260
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003261 // Check the primary module map file.
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003262 const FileEntry *StoredModMap = FileMgr.getFile(F.ModuleMapPath);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003263 if (StoredModMap == nullptr || StoredModMap != ModMap) {
3264 assert(ModMap && "found module is missing module map file");
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003265 assert(ImportedBy && "top-level import should be verified");
3266 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3267 Diag(diag::err_imported_module_modmap_changed)
3268 << F.ModuleName << ImportedBy->FileName
3269 << ModMap->getName() << F.ModuleMapPath;
3270 return OutOfDate;
3271 }
3272
3273 llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps;
3274 for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
3275 // FIXME: we should use input files rather than storing names.
Richard Smith7ed1bc92014-12-05 22:42:13 +00003276 std::string Filename = ReadPath(F, Record, Idx);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003277 const FileEntry *F =
3278 FileMgr.getFile(Filename, false, false);
3279 if (F == nullptr) {
3280 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3281 Error("could not find file '" + Filename +"' referenced by AST file");
3282 return OutOfDate;
3283 }
3284 AdditionalStoredMaps.insert(F);
3285 }
3286
3287 // Check any additional module map files (e.g. module.private.modulemap)
3288 // that are not in the pcm.
3289 if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
3290 for (const FileEntry *ModMap : *AdditionalModuleMaps) {
3291 // Remove files that match
3292 // Note: SmallPtrSet::erase is really remove
3293 if (!AdditionalStoredMaps.erase(ModMap)) {
3294 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3295 Diag(diag::err_module_different_modmap)
3296 << F.ModuleName << /*new*/0 << ModMap->getName();
3297 return OutOfDate;
3298 }
3299 }
3300 }
3301
3302 // Check any additional module map files that are in the pcm, but not
3303 // found in header search. Cases that match are already removed.
3304 for (const FileEntry *ModMap : AdditionalStoredMaps) {
3305 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3306 Diag(diag::err_module_different_modmap)
3307 << F.ModuleName << /*not new*/1 << ModMap->getName();
3308 return OutOfDate;
3309 }
3310 }
3311
3312 if (Listener)
3313 Listener->ReadModuleMapFile(F.ModuleMapPath);
3314 return Success;
3315}
3316
3317
Douglas Gregorc1489562013-02-12 23:36:21 +00003318/// \brief Move the given method to the back of the global list of methods.
3319static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
3320 // Find the entry for this selector in the method pool.
3321 Sema::GlobalMethodPool::iterator Known
3322 = S.MethodPool.find(Method->getSelector());
3323 if (Known == S.MethodPool.end())
3324 return;
3325
3326 // Retrieve the appropriate method list.
3327 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
3328 : Known->second.second;
3329 bool Found = false;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003330 for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
Douglas Gregorc1489562013-02-12 23:36:21 +00003331 if (!Found) {
Nico Weber2e0c8f72014-12-27 03:58:08 +00003332 if (List->getMethod() == Method) {
Douglas Gregorc1489562013-02-12 23:36:21 +00003333 Found = true;
3334 } else {
3335 // Keep searching.
3336 continue;
3337 }
3338 }
3339
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003340 if (List->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003341 List->setMethod(List->getNext()->getMethod());
Douglas Gregorc1489562013-02-12 23:36:21 +00003342 else
Nico Weber2e0c8f72014-12-27 03:58:08 +00003343 List->setMethod(Method);
Douglas Gregorc1489562013-02-12 23:36:21 +00003344 }
3345}
3346
Richard Smithde711422015-04-23 21:20:19 +00003347void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
Richard Smith10434f32015-05-02 02:08:26 +00003348 assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
Richard Smith20e883e2015-04-29 23:20:19 +00003349 for (Decl *D : Names) {
Richard Smith49f906a2014-03-01 00:08:04 +00003350 bool wasHidden = D->Hidden;
3351 D->Hidden = false;
Guy Benyei11169dd2012-12-18 14:30:41 +00003352
Richard Smith49f906a2014-03-01 00:08:04 +00003353 if (wasHidden && SemaObj) {
3354 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
3355 moveMethodToBackOfGlobalList(*SemaObj, Method);
Douglas Gregorc1489562013-02-12 23:36:21 +00003356 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003357 }
3358 }
3359}
3360
Richard Smith49f906a2014-03-01 00:08:04 +00003361void ASTReader::makeModuleVisible(Module *Mod,
Argyrios Kyrtzidis125df052013-02-01 16:36:12 +00003362 Module::NameVisibilityKind NameVisibility,
Richard Smitha7e2cc62015-05-01 01:53:09 +00003363 SourceLocation ImportLoc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003364 llvm::SmallPtrSet<Module *, 4> Visited;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003365 SmallVector<Module *, 4> Stack;
Robert Wilhelm25284cc2013-08-23 16:11:15 +00003366 Stack.push_back(Mod);
Guy Benyei11169dd2012-12-18 14:30:41 +00003367 while (!Stack.empty()) {
Robert Wilhelm25284cc2013-08-23 16:11:15 +00003368 Mod = Stack.pop_back_val();
Guy Benyei11169dd2012-12-18 14:30:41 +00003369
3370 if (NameVisibility <= Mod->NameVisibility) {
Robert Wilhelm25284cc2013-08-23 16:11:15 +00003371 // This module already has this level of visibility (or greater), so
Guy Benyei11169dd2012-12-18 14:30:41 +00003372 // there is nothing more to do.
3373 continue;
3374 }
Richard Smith49f906a2014-03-01 00:08:04 +00003375
Guy Benyei11169dd2012-12-18 14:30:41 +00003376 if (!Mod->isAvailable()) {
3377 // Modules that aren't available cannot be made visible.
3378 continue;
3379 }
3380
3381 // Update the module's name visibility.
3382 Mod->NameVisibility = NameVisibility;
Richard Smith49f906a2014-03-01 00:08:04 +00003383
Guy Benyei11169dd2012-12-18 14:30:41 +00003384 // If we've already deserialized any names from this module,
3385 // mark them as visible.
3386 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
3387 if (Hidden != HiddenNamesMap.end()) {
Richard Smith57721ac2014-07-21 04:10:40 +00003388 auto HiddenNames = std::move(*Hidden);
Guy Benyei11169dd2012-12-18 14:30:41 +00003389 HiddenNamesMap.erase(Hidden);
Richard Smithde711422015-04-23 21:20:19 +00003390 makeNamesVisible(HiddenNames.second, HiddenNames.first);
Richard Smith57721ac2014-07-21 04:10:40 +00003391 assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() &&
3392 "making names visible added hidden names");
Guy Benyei11169dd2012-12-18 14:30:41 +00003393 }
Dmitri Gribenkoe9bcf5b2013-11-04 21:51:33 +00003394
Guy Benyei11169dd2012-12-18 14:30:41 +00003395 // Push any exported modules onto the stack to be marked as visible.
Argyrios Kyrtzidis8739f7b2013-02-19 19:34:40 +00003396 SmallVector<Module *, 16> Exports;
3397 Mod->getExportedModules(Exports);
3398 for (SmallVectorImpl<Module *>::iterator
3399 I = Exports.begin(), E = Exports.end(); I != E; ++I) {
3400 Module *Exported = *I;
David Blaikie82e95a32014-11-19 07:49:47 +00003401 if (Visited.insert(Exported).second)
Argyrios Kyrtzidis8739f7b2013-02-19 19:34:40 +00003402 Stack.push_back(Exported);
Guy Benyei11169dd2012-12-18 14:30:41 +00003403 }
3404 }
3405}
3406
Douglas Gregore060e572013-01-25 01:03:03 +00003407bool ASTReader::loadGlobalIndex() {
3408 if (GlobalIndex)
3409 return false;
3410
3411 if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
3412 !Context.getLangOpts().Modules)
3413 return true;
3414
3415 // Try to load the global index.
3416 TriedLoadingGlobalIndex = true;
3417 StringRef ModuleCachePath
3418 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
3419 std::pair<GlobalModuleIndex *, GlobalModuleIndex::ErrorCode> Result
Douglas Gregor7029ce12013-03-19 00:28:20 +00003420 = GlobalModuleIndex::readIndex(ModuleCachePath);
Douglas Gregore060e572013-01-25 01:03:03 +00003421 if (!Result.first)
3422 return true;
3423
3424 GlobalIndex.reset(Result.first);
Douglas Gregor7211ac12013-01-25 23:32:03 +00003425 ModuleMgr.setGlobalIndex(GlobalIndex.get());
Douglas Gregore060e572013-01-25 01:03:03 +00003426 return false;
3427}
3428
3429bool ASTReader::isGlobalIndexUnavailable() const {
3430 return Context.getLangOpts().Modules && UseGlobalIndex &&
3431 !hasGlobalIndex() && TriedLoadingGlobalIndex;
3432}
3433
Dmitri Gribenkof430da42014-02-12 10:33:14 +00003434static void updateModuleTimestamp(ModuleFile &MF) {
3435 // Overwrite the timestamp file contents so that file's mtime changes.
3436 std::string TimestampFilename = MF.getTimestampFilename();
Rafael Espindoladae941a2014-08-25 18:17:04 +00003437 std::error_code EC;
3438 llvm::raw_fd_ostream OS(TimestampFilename, EC, llvm::sys::fs::F_Text);
3439 if (EC)
Dmitri Gribenkof430da42014-02-12 10:33:14 +00003440 return;
3441 OS << "Timestamp file\n";
3442}
3443
Douglas Gregor6623e1f2015-11-03 18:33:07 +00003444/// \brief Given a cursor at the start of an AST file, scan ahead and drop the
3445/// cursor into the start of the given block ID, returning false on success and
3446/// true on failure.
3447static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
3448 while (1) {
3449 llvm::BitstreamEntry Entry = Cursor.advance();
3450 switch (Entry.Kind) {
3451 case llvm::BitstreamEntry::Error:
3452 case llvm::BitstreamEntry::EndBlock:
3453 return true;
3454
3455 case llvm::BitstreamEntry::Record:
3456 // Ignore top-level records.
3457 Cursor.skipRecord(Entry.ID);
3458 break;
3459
3460 case llvm::BitstreamEntry::SubBlock:
3461 if (Entry.ID == BlockID) {
3462 if (Cursor.EnterSubBlock(BlockID))
3463 return true;
3464 // Found it!
3465 return false;
3466 }
3467
3468 if (Cursor.SkipBlock())
3469 return true;
3470 }
3471 }
3472}
3473
Benjamin Kramer0772c422016-02-13 13:42:54 +00003474ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName,
Guy Benyei11169dd2012-12-18 14:30:41 +00003475 ModuleKind Type,
3476 SourceLocation ImportLoc,
3477 unsigned ClientLoadCapabilities) {
Argyrios Kyrtzidisdc9fdaf2013-05-24 05:44:08 +00003478 llvm::SaveAndRestore<SourceLocation>
3479 SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
3480
Richard Smithd1c46742014-04-30 02:24:17 +00003481 // Defer any pending actions until we get to the end of reading the AST file.
3482 Deserializing AnASTFile(this);
3483
Guy Benyei11169dd2012-12-18 14:30:41 +00003484 // Bump the generation number.
Richard Smith053f6c62014-05-16 23:01:30 +00003485 unsigned PreviousGeneration = incrementGeneration(Context);
Guy Benyei11169dd2012-12-18 14:30:41 +00003486
3487 unsigned NumModules = ModuleMgr.size();
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003488 SmallVector<ImportedModule, 4> Loaded;
Guy Benyei11169dd2012-12-18 14:30:41 +00003489 switch(ASTReadResult ReadResult = ReadASTCore(FileName, Type, ImportLoc,
Craig Toppera13603a2014-05-22 05:54:18 +00003490 /*ImportedBy=*/nullptr, Loaded,
Ben Langmuir487ea142014-10-23 18:05:36 +00003491 0, 0, 0,
Guy Benyei11169dd2012-12-18 14:30:41 +00003492 ClientLoadCapabilities)) {
3493 case Failure:
Douglas Gregor7029ce12013-03-19 00:28:20 +00003494 case Missing:
Guy Benyei11169dd2012-12-18 14:30:41 +00003495 case OutOfDate:
3496 case VersionMismatch:
3497 case ConfigurationMismatch:
Ben Langmuir9801b252014-06-20 00:24:56 +00003498 case HadErrors: {
3499 llvm::SmallPtrSet<ModuleFile *, 4> LoadedSet;
3500 for (const ImportedModule &IM : Loaded)
3501 LoadedSet.insert(IM.Mod);
3502
Douglas Gregor7029ce12013-03-19 00:28:20 +00003503 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, ModuleMgr.end(),
Ben Langmuir9801b252014-06-20 00:24:56 +00003504 LoadedSet,
Douglas Gregor7029ce12013-03-19 00:28:20 +00003505 Context.getLangOpts().Modules
3506 ? &PP.getHeaderSearchInfo().getModuleMap()
Craig Toppera13603a2014-05-22 05:54:18 +00003507 : nullptr);
Douglas Gregore060e572013-01-25 01:03:03 +00003508
3509 // If we find that any modules are unusable, the global index is going
3510 // to be out-of-date. Just remove it.
3511 GlobalIndex.reset();
Craig Toppera13603a2014-05-22 05:54:18 +00003512 ModuleMgr.setGlobalIndex(nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00003513 return ReadResult;
Ben Langmuir9801b252014-06-20 00:24:56 +00003514 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003515 case Success:
3516 break;
3517 }
3518
3519 // Here comes stuff that we only do once the entire chain is loaded.
3520
3521 // Load the AST blocks of all of the modules that we loaded.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003522 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
3523 MEnd = Loaded.end();
Guy Benyei11169dd2012-12-18 14:30:41 +00003524 M != MEnd; ++M) {
3525 ModuleFile &F = *M->Mod;
3526
3527 // Read the AST block.
Ben Langmuir2c9af442014-04-10 17:57:43 +00003528 if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities))
3529 return Result;
Guy Benyei11169dd2012-12-18 14:30:41 +00003530
Douglas Gregor6623e1f2015-11-03 18:33:07 +00003531 // Read the extension blocks.
3532 while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) {
3533 if (ASTReadResult Result = ReadExtensionBlock(F))
3534 return Result;
3535 }
3536
Guy Benyei11169dd2012-12-18 14:30:41 +00003537 // Once read, set the ModuleFile bit base offset and update the size in
3538 // bits of all files we've seen.
3539 F.GlobalBitOffset = TotalModulesSizeInBits;
3540 TotalModulesSizeInBits += F.SizeInBits;
3541 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
3542
3543 // Preload SLocEntries.
3544 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
3545 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
3546 // Load it through the SourceManager and don't call ReadSLocEntry()
3547 // directly because the entry may have already been loaded in which case
3548 // calling ReadSLocEntry() directly would trigger an assertion in
3549 // SourceManager.
3550 SourceMgr.getLoadedSLocEntryByID(Index);
3551 }
Richard Smith33e0f7e2015-07-22 02:08:40 +00003552
3553 // Preload all the pending interesting identifiers by marking them out of
3554 // date.
3555 for (auto Offset : F.PreloadIdentifierOffsets) {
3556 const unsigned char *Data = reinterpret_cast<const unsigned char *>(
3557 F.IdentifierTableData + Offset);
3558
3559 ASTIdentifierLookupTrait Trait(*this, F);
3560 auto KeyDataLen = Trait.ReadKeyDataLength(Data);
3561 auto Key = Trait.ReadKey(Data, KeyDataLen.first);
Richard Smith79bf9202015-08-24 03:33:22 +00003562 auto &II = PP.getIdentifierTable().getOwn(Key);
3563 II.setOutOfDate(true);
3564
3565 // Mark this identifier as being from an AST file so that we can track
3566 // whether we need to serialize it.
Richard Smitheb4b58f62016-02-05 01:40:54 +00003567 markIdentifierFromAST(*this, II);
Richard Smith79bf9202015-08-24 03:33:22 +00003568
3569 // Associate the ID with the identifier so that the writer can reuse it.
3570 auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first);
3571 SetIdentifierInfo(ID, &II);
Richard Smith33e0f7e2015-07-22 02:08:40 +00003572 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003573 }
3574
Douglas Gregor603cd862013-03-22 18:50:14 +00003575 // Setup the import locations and notify the module manager that we've
3576 // committed to these module files.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003577 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
3578 MEnd = Loaded.end();
Guy Benyei11169dd2012-12-18 14:30:41 +00003579 M != MEnd; ++M) {
3580 ModuleFile &F = *M->Mod;
Douglas Gregor603cd862013-03-22 18:50:14 +00003581
3582 ModuleMgr.moduleFileAccepted(&F);
3583
3584 // Set the import location.
Argyrios Kyrtzidis71c1af82013-02-01 16:36:14 +00003585 F.DirectImportLoc = ImportLoc;
Richard Smithb22a1d12016-03-27 20:13:24 +00003586 // FIXME: We assume that locations from PCH / preamble do not need
3587 // any translation.
Guy Benyei11169dd2012-12-18 14:30:41 +00003588 if (!M->ImportedBy)
3589 F.ImportLoc = M->ImportLoc;
3590 else
Richard Smithb22a1d12016-03-27 20:13:24 +00003591 F.ImportLoc = TranslateSourceLocation(*M->ImportedBy, M->ImportLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003592 }
3593
Richard Smith33e0f7e2015-07-22 02:08:40 +00003594 if (!Context.getLangOpts().CPlusPlus ||
3595 (Type != MK_ImplicitModule && Type != MK_ExplicitModule)) {
3596 // Mark all of the identifiers in the identifier table as being out of date,
3597 // so that various accessors know to check the loaded modules when the
3598 // identifier is used.
3599 //
3600 // For C++ modules, we don't need information on many identifiers (just
3601 // those that provide macros or are poisoned), so we mark all of
3602 // the interesting ones via PreloadIdentifierOffsets.
3603 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
3604 IdEnd = PP.getIdentifierTable().end();
3605 Id != IdEnd; ++Id)
3606 Id->second->setOutOfDate(true);
3607 }
Manman Rena0f31a02016-04-29 19:04:05 +00003608 // Mark selectors as out of date.
3609 for (auto Sel : SelectorGeneration)
3610 SelectorOutOfDate[Sel.first] = true;
Guy Benyei11169dd2012-12-18 14:30:41 +00003611
3612 // Resolve any unresolved module exports.
Douglas Gregorfb912652013-03-20 21:10:35 +00003613 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
3614 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
Guy Benyei11169dd2012-12-18 14:30:41 +00003615 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
3616 Module *ResolvedMod = getSubmodule(GlobalID);
Douglas Gregorfb912652013-03-20 21:10:35 +00003617
3618 switch (Unresolved.Kind) {
3619 case UnresolvedModuleRef::Conflict:
3620 if (ResolvedMod) {
3621 Module::Conflict Conflict;
3622 Conflict.Other = ResolvedMod;
3623 Conflict.Message = Unresolved.String.str();
3624 Unresolved.Mod->Conflicts.push_back(Conflict);
3625 }
3626 continue;
3627
3628 case UnresolvedModuleRef::Import:
Guy Benyei11169dd2012-12-18 14:30:41 +00003629 if (ResolvedMod)
Richard Smith38477db2015-05-02 00:45:56 +00003630 Unresolved.Mod->Imports.insert(ResolvedMod);
Guy Benyei11169dd2012-12-18 14:30:41 +00003631 continue;
Guy Benyei11169dd2012-12-18 14:30:41 +00003632
Douglas Gregorfb912652013-03-20 21:10:35 +00003633 case UnresolvedModuleRef::Export:
3634 if (ResolvedMod || Unresolved.IsWildcard)
3635 Unresolved.Mod->Exports.push_back(
3636 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
3637 continue;
3638 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003639 }
Douglas Gregorfb912652013-03-20 21:10:35 +00003640 UnresolvedModuleRefs.clear();
Daniel Jasperba7f2f72013-09-24 09:14:14 +00003641
3642 // FIXME: How do we load the 'use'd modules? They may not be submodules.
3643 // Might be unnecessary as use declarations are only used to build the
3644 // module itself.
Guy Benyei11169dd2012-12-18 14:30:41 +00003645
3646 InitializeContext();
3647
Richard Smith3d8e97e2013-10-18 06:54:39 +00003648 if (SemaObj)
3649 UpdateSema();
3650
Guy Benyei11169dd2012-12-18 14:30:41 +00003651 if (DeserializationListener)
3652 DeserializationListener->ReaderInitialized(this);
3653
3654 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
Yaron Keren8b563662015-10-03 10:46:20 +00003655 if (PrimaryModule.OriginalSourceFileID.isValid()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003656 PrimaryModule.OriginalSourceFileID
3657 = FileID::get(PrimaryModule.SLocEntryBaseID
3658 + PrimaryModule.OriginalSourceFileID.getOpaqueValue() - 1);
3659
3660 // If this AST file is a precompiled preamble, then set the
3661 // preamble file ID of the source manager to the file source file
3662 // from which the preamble was built.
3663 if (Type == MK_Preamble) {
3664 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
3665 } else if (Type == MK_MainFile) {
3666 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
3667 }
3668 }
3669
3670 // For any Objective-C class definitions we have already loaded, make sure
3671 // that we load any additional categories.
3672 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
3673 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
3674 ObjCClassesLoaded[I],
3675 PreviousGeneration);
3676 }
Douglas Gregore060e572013-01-25 01:03:03 +00003677
Dmitri Gribenkof430da42014-02-12 10:33:14 +00003678 if (PP.getHeaderSearchInfo()
3679 .getHeaderSearchOpts()
3680 .ModulesValidateOncePerBuildSession) {
3681 // Now we are certain that the module and all modules it depends on are
3682 // up to date. Create or update timestamp files for modules that are
3683 // located in the module cache (not for PCH files that could be anywhere
3684 // in the filesystem).
3685 for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
3686 ImportedModule &M = Loaded[I];
Richard Smithe842a472014-10-22 02:05:46 +00003687 if (M.Mod->Kind == MK_ImplicitModule) {
Dmitri Gribenkof430da42014-02-12 10:33:14 +00003688 updateModuleTimestamp(*M.Mod);
3689 }
3690 }
3691 }
3692
Guy Benyei11169dd2012-12-18 14:30:41 +00003693 return Success;
3694}
3695
Ben Langmuir487ea142014-10-23 18:05:36 +00003696static ASTFileSignature readASTFileSignature(llvm::BitstreamReader &StreamFile);
3697
Ben Langmuir70a1b812015-03-24 04:43:52 +00003698/// \brief Whether \p Stream starts with the AST/PCH file magic number 'CPCH'.
3699static bool startsWithASTFileMagic(BitstreamCursor &Stream) {
3700 return Stream.Read(8) == 'C' &&
3701 Stream.Read(8) == 'P' &&
3702 Stream.Read(8) == 'C' &&
3703 Stream.Read(8) == 'H';
3704}
3705
Richard Smith0f99d6a2015-08-09 08:48:41 +00003706static unsigned moduleKindForDiagnostic(ModuleKind Kind) {
3707 switch (Kind) {
3708 case MK_PCH:
3709 return 0; // PCH
3710 case MK_ImplicitModule:
3711 case MK_ExplicitModule:
3712 return 1; // module
3713 case MK_MainFile:
3714 case MK_Preamble:
3715 return 2; // main source file
3716 }
3717 llvm_unreachable("unknown module kind");
3718}
3719
Guy Benyei11169dd2012-12-18 14:30:41 +00003720ASTReader::ASTReadResult
3721ASTReader::ReadASTCore(StringRef FileName,
3722 ModuleKind Type,
3723 SourceLocation ImportLoc,
3724 ModuleFile *ImportedBy,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003725 SmallVectorImpl<ImportedModule> &Loaded,
Douglas Gregor7029ce12013-03-19 00:28:20 +00003726 off_t ExpectedSize, time_t ExpectedModTime,
Ben Langmuir487ea142014-10-23 18:05:36 +00003727 ASTFileSignature ExpectedSignature,
Guy Benyei11169dd2012-12-18 14:30:41 +00003728 unsigned ClientLoadCapabilities) {
3729 ModuleFile *M;
Guy Benyei11169dd2012-12-18 14:30:41 +00003730 std::string ErrorStr;
Douglas Gregor7029ce12013-03-19 00:28:20 +00003731 ModuleManager::AddModuleResult AddResult
3732 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
Richard Smith053f6c62014-05-16 23:01:30 +00003733 getGeneration(), ExpectedSize, ExpectedModTime,
Ben Langmuir487ea142014-10-23 18:05:36 +00003734 ExpectedSignature, readASTFileSignature,
Douglas Gregor7029ce12013-03-19 00:28:20 +00003735 M, ErrorStr);
Guy Benyei11169dd2012-12-18 14:30:41 +00003736
Douglas Gregor7029ce12013-03-19 00:28:20 +00003737 switch (AddResult) {
3738 case ModuleManager::AlreadyLoaded:
3739 return Success;
3740
3741 case ModuleManager::NewlyLoaded:
3742 // Load module file below.
3743 break;
3744
3745 case ModuleManager::Missing:
Richard Smithe842a472014-10-22 02:05:46 +00003746 // The module file was missing; if the client can handle that, return
Douglas Gregor7029ce12013-03-19 00:28:20 +00003747 // it.
3748 if (ClientLoadCapabilities & ARR_Missing)
3749 return Missing;
3750
3751 // Otherwise, return an error.
Richard Smith0f99d6a2015-08-09 08:48:41 +00003752 Diag(diag::err_module_file_not_found) << moduleKindForDiagnostic(Type)
3753 << FileName << ErrorStr.empty()
3754 << ErrorStr;
Douglas Gregor7029ce12013-03-19 00:28:20 +00003755 return Failure;
3756
3757 case ModuleManager::OutOfDate:
3758 // We couldn't load the module file because it is out-of-date. If the
3759 // client can handle out-of-date, return it.
3760 if (ClientLoadCapabilities & ARR_OutOfDate)
3761 return OutOfDate;
3762
3763 // Otherwise, return an error.
Richard Smith0f99d6a2015-08-09 08:48:41 +00003764 Diag(diag::err_module_file_out_of_date) << moduleKindForDiagnostic(Type)
3765 << FileName << ErrorStr.empty()
3766 << ErrorStr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003767 return Failure;
3768 }
3769
Douglas Gregor7029ce12013-03-19 00:28:20 +00003770 assert(M && "Missing module file");
Guy Benyei11169dd2012-12-18 14:30:41 +00003771
3772 // FIXME: This seems rather a hack. Should CurrentDir be part of the
3773 // module?
3774 if (FileName != "-") {
3775 CurrentDir = llvm::sys::path::parent_path(FileName);
3776 if (CurrentDir.empty()) CurrentDir = ".";
3777 }
3778
3779 ModuleFile &F = *M;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003780 BitstreamCursor &Stream = F.Stream;
Adrian Prantlfb2398d2015-07-17 01:19:54 +00003781 PCHContainerRdr.ExtractPCH(F.Buffer->getMemBufferRef(), F.StreamFile);
Rafael Espindolafd832392014-11-12 14:48:44 +00003782 Stream.init(&F.StreamFile);
Adrian Prantlcbc368c2015-02-25 02:44:04 +00003783 F.SizeInBits = F.Buffer->getBufferSize() * 8;
3784
Guy Benyei11169dd2012-12-18 14:30:41 +00003785 // Sniff for the signature.
Ben Langmuir70a1b812015-03-24 04:43:52 +00003786 if (!startsWithASTFileMagic(Stream)) {
Richard Smith0f99d6a2015-08-09 08:48:41 +00003787 Diag(diag::err_module_file_invalid) << moduleKindForDiagnostic(Type)
3788 << FileName;
Guy Benyei11169dd2012-12-18 14:30:41 +00003789 return Failure;
3790 }
3791
3792 // This is used for compatibility with older PCH formats.
3793 bool HaveReadControlBlock = false;
Chris Lattnerefa77172013-01-20 00:00:22 +00003794 while (1) {
3795 llvm::BitstreamEntry Entry = Stream.advance();
3796
3797 switch (Entry.Kind) {
3798 case llvm::BitstreamEntry::Error:
Chris Lattnerefa77172013-01-20 00:00:22 +00003799 case llvm::BitstreamEntry::Record:
Douglas Gregor6623e1f2015-11-03 18:33:07 +00003800 case llvm::BitstreamEntry::EndBlock:
Guy Benyei11169dd2012-12-18 14:30:41 +00003801 Error("invalid record at top-level of AST file");
3802 return Failure;
Chris Lattnerefa77172013-01-20 00:00:22 +00003803
3804 case llvm::BitstreamEntry::SubBlock:
3805 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003806 }
3807
Chris Lattnerefa77172013-01-20 00:00:22 +00003808 switch (Entry.ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003809 case CONTROL_BLOCK_ID:
3810 HaveReadControlBlock = true;
Ben Langmuirbeee15e2014-04-14 18:00:01 +00003811 switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003812 case Success:
Richard Smith0f99d6a2015-08-09 08:48:41 +00003813 // Check that we didn't try to load a non-module AST file as a module.
3814 //
3815 // FIXME: Should we also perform the converse check? Loading a module as
3816 // a PCH file sort of works, but it's a bit wonky.
3817 if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule) &&
3818 F.ModuleName.empty()) {
3819 auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
3820 if (Result != OutOfDate ||
3821 (ClientLoadCapabilities & ARR_OutOfDate) == 0)
3822 Diag(diag::err_module_file_not_module) << FileName;
3823 return Result;
3824 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003825 break;
3826
3827 case Failure: return Failure;
Douglas Gregor7029ce12013-03-19 00:28:20 +00003828 case Missing: return Missing;
Guy Benyei11169dd2012-12-18 14:30:41 +00003829 case OutOfDate: return OutOfDate;
3830 case VersionMismatch: return VersionMismatch;
3831 case ConfigurationMismatch: return ConfigurationMismatch;
3832 case HadErrors: return HadErrors;
3833 }
3834 break;
Richard Smithf8c32552015-09-02 17:45:54 +00003835
Guy Benyei11169dd2012-12-18 14:30:41 +00003836 case AST_BLOCK_ID:
3837 if (!HaveReadControlBlock) {
3838 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
Dmitri Gribenko2228cd32014-02-11 15:40:09 +00003839 Diag(diag::err_pch_version_too_old);
Guy Benyei11169dd2012-12-18 14:30:41 +00003840 return VersionMismatch;
3841 }
3842
3843 // Record that we've loaded this module.
3844 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
3845 return Success;
3846
3847 default:
3848 if (Stream.SkipBlock()) {
3849 Error("malformed block record in AST file");
3850 return Failure;
3851 }
3852 break;
3853 }
3854 }
Douglas Gregor6623e1f2015-11-03 18:33:07 +00003855
3856 return Success;
3857}
3858
3859/// Parse a record and blob containing module file extension metadata.
3860static bool parseModuleFileExtensionMetadata(
3861 const SmallVectorImpl<uint64_t> &Record,
3862 StringRef Blob,
3863 ModuleFileExtensionMetadata &Metadata) {
3864 if (Record.size() < 4) return true;
3865
3866 Metadata.MajorVersion = Record[0];
3867 Metadata.MinorVersion = Record[1];
3868
3869 unsigned BlockNameLen = Record[2];
3870 unsigned UserInfoLen = Record[3];
3871
3872 if (BlockNameLen + UserInfoLen > Blob.size()) return true;
3873
3874 Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
3875 Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
3876 Blob.data() + BlockNameLen + UserInfoLen);
3877 return false;
3878}
3879
3880ASTReader::ASTReadResult ASTReader::ReadExtensionBlock(ModuleFile &F) {
3881 BitstreamCursor &Stream = F.Stream;
3882
3883 RecordData Record;
3884 while (true) {
3885 llvm::BitstreamEntry Entry = Stream.advance();
3886 switch (Entry.Kind) {
3887 case llvm::BitstreamEntry::SubBlock:
3888 if (Stream.SkipBlock())
3889 return Failure;
3890
3891 continue;
3892
3893 case llvm::BitstreamEntry::EndBlock:
3894 return Success;
3895
3896 case llvm::BitstreamEntry::Error:
3897 return HadErrors;
3898
3899 case llvm::BitstreamEntry::Record:
3900 break;
3901 }
3902
3903 Record.clear();
3904 StringRef Blob;
3905 unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob);
3906 switch (RecCode) {
3907 case EXTENSION_METADATA: {
3908 ModuleFileExtensionMetadata Metadata;
3909 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
3910 return Failure;
3911
3912 // Find a module file extension with this block name.
3913 auto Known = ModuleFileExtensions.find(Metadata.BlockName);
3914 if (Known == ModuleFileExtensions.end()) break;
3915
3916 // Form a reader.
3917 if (auto Reader = Known->second->createExtensionReader(Metadata, *this,
3918 F, Stream)) {
3919 F.ExtensionReaders.push_back(std::move(Reader));
3920 }
3921
3922 break;
3923 }
3924 }
3925 }
3926
3927 return Success;
Guy Benyei11169dd2012-12-18 14:30:41 +00003928}
3929
Richard Smitha7e2cc62015-05-01 01:53:09 +00003930void ASTReader::InitializeContext() {
Guy Benyei11169dd2012-12-18 14:30:41 +00003931 // If there's a listener, notify them that we "read" the translation unit.
3932 if (DeserializationListener)
3933 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
3934 Context.getTranslationUnitDecl());
3935
Guy Benyei11169dd2012-12-18 14:30:41 +00003936 // FIXME: Find a better way to deal with collisions between these
3937 // built-in types. Right now, we just ignore the problem.
3938
3939 // Load the special types.
3940 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
3941 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
3942 if (!Context.CFConstantStringTypeDecl)
3943 Context.setCFConstantStringType(GetType(String));
3944 }
3945
3946 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
3947 QualType FileType = GetType(File);
3948 if (FileType.isNull()) {
3949 Error("FILE type is NULL");
3950 return;
3951 }
3952
3953 if (!Context.FILEDecl) {
3954 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
3955 Context.setFILEDecl(Typedef->getDecl());
3956 else {
3957 const TagType *Tag = FileType->getAs<TagType>();
3958 if (!Tag) {
3959 Error("Invalid FILE type in AST file");
3960 return;
3961 }
3962 Context.setFILEDecl(Tag->getDecl());
3963 }
3964 }
3965 }
3966
3967 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
3968 QualType Jmp_bufType = GetType(Jmp_buf);
3969 if (Jmp_bufType.isNull()) {
3970 Error("jmp_buf type is NULL");
3971 return;
3972 }
3973
3974 if (!Context.jmp_bufDecl) {
3975 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
3976 Context.setjmp_bufDecl(Typedef->getDecl());
3977 else {
3978 const TagType *Tag = Jmp_bufType->getAs<TagType>();
3979 if (!Tag) {
3980 Error("Invalid jmp_buf type in AST file");
3981 return;
3982 }
3983 Context.setjmp_bufDecl(Tag->getDecl());
3984 }
3985 }
3986 }
3987
3988 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
3989 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
3990 if (Sigjmp_bufType.isNull()) {
3991 Error("sigjmp_buf type is NULL");
3992 return;
3993 }
3994
3995 if (!Context.sigjmp_bufDecl) {
3996 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
3997 Context.setsigjmp_bufDecl(Typedef->getDecl());
3998 else {
3999 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
4000 assert(Tag && "Invalid sigjmp_buf type in AST file");
4001 Context.setsigjmp_bufDecl(Tag->getDecl());
4002 }
4003 }
4004 }
4005
4006 if (unsigned ObjCIdRedef
4007 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
4008 if (Context.ObjCIdRedefinitionType.isNull())
4009 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
4010 }
4011
4012 if (unsigned ObjCClassRedef
4013 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
4014 if (Context.ObjCClassRedefinitionType.isNull())
4015 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
4016 }
4017
4018 if (unsigned ObjCSelRedef
4019 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
4020 if (Context.ObjCSelRedefinitionType.isNull())
4021 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
4022 }
4023
4024 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
4025 QualType Ucontext_tType = GetType(Ucontext_t);
4026 if (Ucontext_tType.isNull()) {
4027 Error("ucontext_t type is NULL");
4028 return;
4029 }
4030
4031 if (!Context.ucontext_tDecl) {
4032 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
4033 Context.setucontext_tDecl(Typedef->getDecl());
4034 else {
4035 const TagType *Tag = Ucontext_tType->getAs<TagType>();
4036 assert(Tag && "Invalid ucontext_t type in AST file");
4037 Context.setucontext_tDecl(Tag->getDecl());
4038 }
4039 }
4040 }
4041 }
4042
4043 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
4044
4045 // If there were any CUDA special declarations, deserialize them.
4046 if (!CUDASpecialDeclRefs.empty()) {
4047 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
4048 Context.setcudaConfigureCallDecl(
4049 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
4050 }
Richard Smith56be7542014-03-21 00:33:59 +00004051
Guy Benyei11169dd2012-12-18 14:30:41 +00004052 // Re-export any modules that were imported by a non-module AST file.
Richard Smitha7e2cc62015-05-01 01:53:09 +00004053 // FIXME: This does not make macro-only imports visible again.
Richard Smith56be7542014-03-21 00:33:59 +00004054 for (auto &Import : ImportedModules) {
Richard Smitha7e2cc62015-05-01 01:53:09 +00004055 if (Module *Imported = getSubmodule(Import.ID)) {
Argyrios Kyrtzidis125df052013-02-01 16:36:12 +00004056 makeModuleVisible(Imported, Module::AllVisible,
Richard Smitha7e2cc62015-05-01 01:53:09 +00004057 /*ImportLoc=*/Import.ImportLoc);
Ben Langmuir6d25fdc2016-02-11 17:04:42 +00004058 if (Import.ImportLoc.isValid())
4059 PP.makeModuleVisible(Imported, Import.ImportLoc);
4060 // FIXME: should we tell Sema to make the module visible too?
Richard Smitha7e2cc62015-05-01 01:53:09 +00004061 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004062 }
4063 ImportedModules.clear();
4064}
4065
4066void ASTReader::finalizeForWriting() {
Richard Smithde711422015-04-23 21:20:19 +00004067 // Nothing to do for now.
Guy Benyei11169dd2012-12-18 14:30:41 +00004068}
4069
Ben Langmuir70a1b812015-03-24 04:43:52 +00004070/// \brief Reads and return the signature record from \p StreamFile's control
4071/// block, or else returns 0.
Ben Langmuir487ea142014-10-23 18:05:36 +00004072static ASTFileSignature readASTFileSignature(llvm::BitstreamReader &StreamFile){
4073 BitstreamCursor Stream(StreamFile);
Ben Langmuir70a1b812015-03-24 04:43:52 +00004074 if (!startsWithASTFileMagic(Stream))
Ben Langmuir487ea142014-10-23 18:05:36 +00004075 return 0;
Ben Langmuir487ea142014-10-23 18:05:36 +00004076
4077 // Scan for the CONTROL_BLOCK_ID block.
4078 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
4079 return 0;
4080
4081 // Scan for SIGNATURE inside the control block.
4082 ASTReader::RecordData Record;
4083 while (1) {
4084 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
4085 if (Entry.Kind == llvm::BitstreamEntry::EndBlock ||
4086 Entry.Kind != llvm::BitstreamEntry::Record)
4087 return 0;
4088
4089 Record.clear();
4090 StringRef Blob;
4091 if (SIGNATURE == Stream.readRecord(Entry.ID, Record, &Blob))
4092 return Record[0];
4093 }
4094}
4095
Guy Benyei11169dd2012-12-18 14:30:41 +00004096/// \brief Retrieve the name of the original source file name
4097/// directly from the AST file, without actually loading the AST
4098/// file.
Adrian Prantlbb165fb2015-06-20 18:53:08 +00004099std::string ASTReader::getOriginalSourceFile(
4100 const std::string &ASTFileName, FileManager &FileMgr,
Adrian Prantlfb2398d2015-07-17 01:19:54 +00004101 const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004102 // Open the AST file.
Benjamin Kramera8857962014-10-26 22:44:13 +00004103 auto Buffer = FileMgr.getBufferForFile(ASTFileName);
Guy Benyei11169dd2012-12-18 14:30:41 +00004104 if (!Buffer) {
Benjamin Kramera8857962014-10-26 22:44:13 +00004105 Diags.Report(diag::err_fe_unable_to_read_pch_file)
4106 << ASTFileName << Buffer.getError().message();
Guy Benyei11169dd2012-12-18 14:30:41 +00004107 return std::string();
4108 }
4109
4110 // Initialize the stream
4111 llvm::BitstreamReader StreamFile;
Adrian Prantlfb2398d2015-07-17 01:19:54 +00004112 PCHContainerRdr.ExtractPCH((*Buffer)->getMemBufferRef(), StreamFile);
Rafael Espindolaaf0e40a2014-11-12 14:42:25 +00004113 BitstreamCursor Stream(StreamFile);
Guy Benyei11169dd2012-12-18 14:30:41 +00004114
4115 // Sniff for the signature.
Ben Langmuir70a1b812015-03-24 04:43:52 +00004116 if (!startsWithASTFileMagic(Stream)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004117 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
4118 return std::string();
4119 }
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004120
Chris Lattnere7b154b2013-01-19 21:39:22 +00004121 // Scan for the CONTROL_BLOCK_ID block.
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004122 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004123 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
4124 return std::string();
Chris Lattnere7b154b2013-01-19 21:39:22 +00004125 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004126
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004127 // Scan for ORIGINAL_FILE inside the control block.
4128 RecordData Record;
Chris Lattnere7b154b2013-01-19 21:39:22 +00004129 while (1) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004130 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Chris Lattnere7b154b2013-01-19 21:39:22 +00004131 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
4132 return std::string();
4133
4134 if (Entry.Kind != llvm::BitstreamEntry::Record) {
4135 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
4136 return std::string();
Guy Benyei11169dd2012-12-18 14:30:41 +00004137 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00004138
Guy Benyei11169dd2012-12-18 14:30:41 +00004139 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00004140 StringRef Blob;
4141 if (Stream.readRecord(Entry.ID, Record, &Blob) == ORIGINAL_FILE)
4142 return Blob.str();
Guy Benyei11169dd2012-12-18 14:30:41 +00004143 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004144}
4145
4146namespace {
4147 class SimplePCHValidator : public ASTReaderListener {
4148 const LangOptions &ExistingLangOpts;
4149 const TargetOptions &ExistingTargetOpts;
4150 const PreprocessorOptions &ExistingPPOpts;
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00004151 std::string ExistingModuleCachePath;
Guy Benyei11169dd2012-12-18 14:30:41 +00004152 FileManager &FileMgr;
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00004153
Guy Benyei11169dd2012-12-18 14:30:41 +00004154 public:
4155 SimplePCHValidator(const LangOptions &ExistingLangOpts,
4156 const TargetOptions &ExistingTargetOpts,
4157 const PreprocessorOptions &ExistingPPOpts,
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00004158 StringRef ExistingModuleCachePath,
Guy Benyei11169dd2012-12-18 14:30:41 +00004159 FileManager &FileMgr)
4160 : ExistingLangOpts(ExistingLangOpts),
4161 ExistingTargetOpts(ExistingTargetOpts),
4162 ExistingPPOpts(ExistingPPOpts),
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00004163 ExistingModuleCachePath(ExistingModuleCachePath),
Guy Benyei11169dd2012-12-18 14:30:41 +00004164 FileMgr(FileMgr)
4165 {
4166 }
4167
Richard Smith1e2cf0d2014-10-31 02:28:58 +00004168 bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
4169 bool AllowCompatibleDifferences) override {
4170 return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr,
4171 AllowCompatibleDifferences);
Guy Benyei11169dd2012-12-18 14:30:41 +00004172 }
Chandler Carruth0d745bc2015-03-14 04:47:43 +00004173 bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
4174 bool AllowCompatibleDifferences) override {
4175 return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr,
4176 AllowCompatibleDifferences);
Guy Benyei11169dd2012-12-18 14:30:41 +00004177 }
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00004178 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
4179 StringRef SpecificModuleCachePath,
4180 bool Complain) override {
4181 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
4182 ExistingModuleCachePath,
4183 nullptr, ExistingLangOpts);
4184 }
Craig Topper3e89dfe2014-03-13 02:13:41 +00004185 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
4186 bool Complain,
4187 std::string &SuggestedPredefines) override {
Craig Toppera13603a2014-05-22 05:54:18 +00004188 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr,
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00004189 SuggestedPredefines, ExistingLangOpts);
Guy Benyei11169dd2012-12-18 14:30:41 +00004190 }
4191 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +00004192}
Guy Benyei11169dd2012-12-18 14:30:41 +00004193
Adrian Prantlbb165fb2015-06-20 18:53:08 +00004194bool ASTReader::readASTFileControlBlock(
4195 StringRef Filename, FileManager &FileMgr,
Adrian Prantlfb2398d2015-07-17 01:19:54 +00004196 const PCHContainerReader &PCHContainerRdr,
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004197 bool FindModuleFileExtensions,
Adrian Prantlbb165fb2015-06-20 18:53:08 +00004198 ASTReaderListener &Listener) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004199 // Open the AST file.
Richard Smith7f330cd2015-03-18 01:42:29 +00004200 // FIXME: This allows use of the VFS; we do not allow use of the
4201 // VFS when actually loading a module.
Benjamin Kramera8857962014-10-26 22:44:13 +00004202 auto Buffer = FileMgr.getBufferForFile(Filename);
Guy Benyei11169dd2012-12-18 14:30:41 +00004203 if (!Buffer) {
4204 return true;
4205 }
4206
4207 // Initialize the stream
4208 llvm::BitstreamReader StreamFile;
Adrian Prantlfb2398d2015-07-17 01:19:54 +00004209 PCHContainerRdr.ExtractPCH((*Buffer)->getMemBufferRef(), StreamFile);
Rafael Espindolaaf0e40a2014-11-12 14:42:25 +00004210 BitstreamCursor Stream(StreamFile);
Guy Benyei11169dd2012-12-18 14:30:41 +00004211
4212 // Sniff for the signature.
Ben Langmuir70a1b812015-03-24 04:43:52 +00004213 if (!startsWithASTFileMagic(Stream))
Guy Benyei11169dd2012-12-18 14:30:41 +00004214 return true;
Guy Benyei11169dd2012-12-18 14:30:41 +00004215
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004216 // Scan for the CONTROL_BLOCK_ID block.
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004217 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004218 return true;
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004219
4220 bool NeedsInputFiles = Listener.needsInputFileVisitation();
Ben Langmuircb69b572014-03-07 06:40:32 +00004221 bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
Richard Smithd4b230b2014-10-27 23:01:16 +00004222 bool NeedsImports = Listener.needsImportVisitation();
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004223 BitstreamCursor InputFilesCursor;
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004224
Guy Benyei11169dd2012-12-18 14:30:41 +00004225 RecordData Record;
Richard Smith7ed1bc92014-12-05 22:42:13 +00004226 std::string ModuleDir;
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004227 bool DoneWithControlBlock = false;
4228 while (!DoneWithControlBlock) {
Richard Smith0516b182015-09-08 19:40:14 +00004229 llvm::BitstreamEntry Entry = Stream.advance();
4230
4231 switch (Entry.Kind) {
4232 case llvm::BitstreamEntry::SubBlock: {
4233 switch (Entry.ID) {
4234 case OPTIONS_BLOCK_ID: {
4235 std::string IgnoredSuggestedPredefines;
4236 if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate,
4237 /*AllowCompatibleConfigurationMismatch*/ false,
4238 Listener, IgnoredSuggestedPredefines) != Success)
4239 return true;
4240 break;
4241 }
4242
4243 case INPUT_FILES_BLOCK_ID:
4244 InputFilesCursor = Stream;
4245 if (Stream.SkipBlock() ||
4246 (NeedsInputFiles &&
4247 ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID)))
4248 return true;
4249 break;
4250
4251 default:
4252 if (Stream.SkipBlock())
4253 return true;
4254 break;
4255 }
4256
4257 continue;
4258 }
4259
4260 case llvm::BitstreamEntry::EndBlock:
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004261 DoneWithControlBlock = true;
4262 break;
Richard Smith0516b182015-09-08 19:40:14 +00004263
4264 case llvm::BitstreamEntry::Error:
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004265 return true;
Richard Smith0516b182015-09-08 19:40:14 +00004266
4267 case llvm::BitstreamEntry::Record:
4268 break;
4269 }
4270
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004271 if (DoneWithControlBlock) break;
4272
Guy Benyei11169dd2012-12-18 14:30:41 +00004273 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00004274 StringRef Blob;
4275 unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob);
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004276 switch ((ControlRecordTypes)RecCode) {
4277 case METADATA: {
4278 if (Record[0] != VERSION_MAJOR)
4279 return true;
Guy Benyei11169dd2012-12-18 14:30:41 +00004280
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +00004281 if (Listener.ReadFullVersionInformation(Blob))
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004282 return true;
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +00004283
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004284 break;
4285 }
Ben Langmuir4f5212a2014-04-14 22:12:44 +00004286 case MODULE_NAME:
4287 Listener.ReadModuleName(Blob);
4288 break;
Richard Smith7ed1bc92014-12-05 22:42:13 +00004289 case MODULE_DIRECTORY:
4290 ModuleDir = Blob;
4291 break;
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00004292 case MODULE_MAP_FILE: {
4293 unsigned Idx = 0;
Richard Smith7ed1bc92014-12-05 22:42:13 +00004294 auto Path = ReadString(Record, Idx);
4295 ResolveImportedPath(Path, ModuleDir);
4296 Listener.ReadModuleMapFile(Path);
Ben Langmuir4f5212a2014-04-14 22:12:44 +00004297 break;
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00004298 }
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004299 case INPUT_FILE_OFFSETS: {
4300 if (!NeedsInputFiles)
4301 break;
4302
4303 unsigned NumInputFiles = Record[0];
4304 unsigned NumUserFiles = Record[1];
Richard Smithec216502015-02-13 19:48:37 +00004305 const uint64_t *InputFileOffs = (const uint64_t *)Blob.data();
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004306 for (unsigned I = 0; I != NumInputFiles; ++I) {
4307 // Go find this input file.
4308 bool isSystemFile = I >= NumUserFiles;
Ben Langmuircb69b572014-03-07 06:40:32 +00004309
4310 if (isSystemFile && !NeedsSystemInputFiles)
4311 break; // the rest are system input files
4312
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004313 BitstreamCursor &Cursor = InputFilesCursor;
4314 SavedStreamPosition SavedPosition(Cursor);
4315 Cursor.JumpToBit(InputFileOffs[I]);
4316
4317 unsigned Code = Cursor.ReadCode();
4318 RecordData Record;
4319 StringRef Blob;
4320 bool shouldContinue = false;
4321 switch ((InputFileRecordTypes)Cursor.readRecord(Code, Record, &Blob)) {
4322 case INPUT_FILE:
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +00004323 bool Overridden = static_cast<bool>(Record[3]);
Richard Smith7ed1bc92014-12-05 22:42:13 +00004324 std::string Filename = Blob;
4325 ResolveImportedPath(Filename, ModuleDir);
Richard Smith216a3bd2015-08-13 17:57:10 +00004326 shouldContinue = Listener.visitInputFile(
4327 Filename, isSystemFile, Overridden, /*IsExplicitModule*/false);
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004328 break;
4329 }
4330 if (!shouldContinue)
4331 break;
4332 }
4333 break;
4334 }
4335
Richard Smithd4b230b2014-10-27 23:01:16 +00004336 case IMPORTS: {
4337 if (!NeedsImports)
4338 break;
4339
4340 unsigned Idx = 0, N = Record.size();
4341 while (Idx < N) {
4342 // Read information about the AST file.
Richard Smith79c98cc2014-10-27 23:25:15 +00004343 Idx += 5; // ImportLoc, Size, ModTime, Signature
Richard Smith7ed1bc92014-12-05 22:42:13 +00004344 std::string Filename = ReadString(Record, Idx);
4345 ResolveImportedPath(Filename, ModuleDir);
4346 Listener.visitImport(Filename);
Richard Smithd4b230b2014-10-27 23:01:16 +00004347 }
4348 break;
4349 }
4350
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004351 default:
4352 // No other validation to perform.
4353 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004354 }
4355 }
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004356
4357 // Look for module file extension blocks, if requested.
4358 if (FindModuleFileExtensions) {
4359 while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) {
4360 bool DoneWithExtensionBlock = false;
4361 while (!DoneWithExtensionBlock) {
4362 llvm::BitstreamEntry Entry = Stream.advance();
4363
4364 switch (Entry.Kind) {
4365 case llvm::BitstreamEntry::SubBlock:
4366 if (Stream.SkipBlock())
4367 return true;
4368
4369 continue;
4370
4371 case llvm::BitstreamEntry::EndBlock:
4372 DoneWithExtensionBlock = true;
4373 continue;
4374
4375 case llvm::BitstreamEntry::Error:
4376 return true;
4377
4378 case llvm::BitstreamEntry::Record:
4379 break;
4380 }
4381
4382 Record.clear();
4383 StringRef Blob;
4384 unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob);
4385 switch (RecCode) {
4386 case EXTENSION_METADATA: {
4387 ModuleFileExtensionMetadata Metadata;
4388 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
4389 return true;
4390
4391 Listener.readModuleFileExtension(Metadata);
4392 break;
4393 }
4394 }
4395 }
4396 }
4397 }
4398
4399 return false;
Guy Benyei11169dd2012-12-18 14:30:41 +00004400}
4401
Adrian Prantlbb165fb2015-06-20 18:53:08 +00004402bool ASTReader::isAcceptableASTFile(
4403 StringRef Filename, FileManager &FileMgr,
Adrian Prantlfb2398d2015-07-17 01:19:54 +00004404 const PCHContainerReader &PCHContainerRdr, const LangOptions &LangOpts,
Adrian Prantlbb165fb2015-06-20 18:53:08 +00004405 const TargetOptions &TargetOpts, const PreprocessorOptions &PPOpts,
4406 std::string ExistingModuleCachePath) {
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00004407 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts,
4408 ExistingModuleCachePath, FileMgr);
Adrian Prantlfb2398d2015-07-17 01:19:54 +00004409 return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr,
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004410 /*FindModuleFileExtensions=*/false,
Adrian Prantlbb165fb2015-06-20 18:53:08 +00004411 validator);
Guy Benyei11169dd2012-12-18 14:30:41 +00004412}
4413
Ben Langmuir2c9af442014-04-10 17:57:43 +00004414ASTReader::ASTReadResult
4415ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004416 // Enter the submodule block.
4417 if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
4418 Error("malformed submodule block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00004419 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00004420 }
4421
4422 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
4423 bool First = true;
Craig Toppera13603a2014-05-22 05:54:18 +00004424 Module *CurrentModule = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00004425 RecordData Record;
4426 while (true) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004427 llvm::BitstreamEntry Entry = F.Stream.advanceSkippingSubblocks();
4428
4429 switch (Entry.Kind) {
4430 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
4431 case llvm::BitstreamEntry::Error:
4432 Error("malformed block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00004433 return Failure;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004434 case llvm::BitstreamEntry::EndBlock:
Ben Langmuir2c9af442014-04-10 17:57:43 +00004435 return Success;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004436 case llvm::BitstreamEntry::Record:
4437 // The interesting case.
4438 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004439 }
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004440
Guy Benyei11169dd2012-12-18 14:30:41 +00004441 // Read a record.
Chris Lattner0e6c9402013-01-20 02:38:54 +00004442 StringRef Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00004443 Record.clear();
Richard Smith03478d92014-10-23 22:12:14 +00004444 auto Kind = F.Stream.readRecord(Entry.ID, Record, &Blob);
4445
4446 if ((Kind == SUBMODULE_METADATA) != First) {
4447 Error("submodule metadata record should be at beginning of block");
4448 return Failure;
4449 }
4450 First = false;
4451
4452 // Submodule information is only valid if we have a current module.
4453 // FIXME: Should we error on these cases?
4454 if (!CurrentModule && Kind != SUBMODULE_METADATA &&
4455 Kind != SUBMODULE_DEFINITION)
4456 continue;
4457
4458 switch (Kind) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004459 default: // Default behavior: ignore.
4460 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004461
Richard Smith03478d92014-10-23 22:12:14 +00004462 case SUBMODULE_DEFINITION: {
Douglas Gregor8d932422013-03-20 03:59:18 +00004463 if (Record.size() < 8) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004464 Error("malformed module definition");
Ben Langmuir2c9af442014-04-10 17:57:43 +00004465 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00004466 }
Richard Smith03478d92014-10-23 22:12:14 +00004467
Chris Lattner0e6c9402013-01-20 02:38:54 +00004468 StringRef Name = Blob;
Richard Smith9bca2982014-03-08 00:03:56 +00004469 unsigned Idx = 0;
4470 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
4471 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
4472 bool IsFramework = Record[Idx++];
4473 bool IsExplicit = Record[Idx++];
4474 bool IsSystem = Record[Idx++];
4475 bool IsExternC = Record[Idx++];
4476 bool InferSubmodules = Record[Idx++];
4477 bool InferExplicitSubmodules = Record[Idx++];
4478 bool InferExportWildcard = Record[Idx++];
4479 bool ConfigMacrosExhaustive = Record[Idx++];
Douglas Gregor8d932422013-03-20 03:59:18 +00004480
Ben Langmuirbeee15e2014-04-14 18:00:01 +00004481 Module *ParentModule = nullptr;
Ben Langmuir9d6448b2014-08-09 00:57:23 +00004482 if (Parent)
Guy Benyei11169dd2012-12-18 14:30:41 +00004483 ParentModule = getSubmodule(Parent);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00004484
Guy Benyei11169dd2012-12-18 14:30:41 +00004485 // Retrieve this (sub)module from the module map, creating it if
4486 // necessary.
Ben Langmuir9d6448b2014-08-09 00:57:23 +00004487 CurrentModule = ModMap.findOrCreateModule(Name, ParentModule, IsFramework,
Guy Benyei11169dd2012-12-18 14:30:41 +00004488 IsExplicit).first;
Ben Langmuir9d6448b2014-08-09 00:57:23 +00004489
4490 // FIXME: set the definition loc for CurrentModule, or call
4491 // ModMap.setInferredModuleAllowedBy()
4492
Guy Benyei11169dd2012-12-18 14:30:41 +00004493 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
4494 if (GlobalIndex >= SubmodulesLoaded.size() ||
4495 SubmodulesLoaded[GlobalIndex]) {
4496 Error("too many submodules");
Ben Langmuir2c9af442014-04-10 17:57:43 +00004497 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00004498 }
Douglas Gregor8a114ab2013-02-06 22:40:31 +00004499
Douglas Gregor7029ce12013-03-19 00:28:20 +00004500 if (!ParentModule) {
4501 if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
4502 if (CurFile != F.File) {
4503 if (!Diags.isDiagnosticInFlight()) {
4504 Diag(diag::err_module_file_conflict)
4505 << CurrentModule->getTopLevelModuleName()
4506 << CurFile->getName()
4507 << F.File->getName();
4508 }
Ben Langmuir2c9af442014-04-10 17:57:43 +00004509 return Failure;
Douglas Gregor8a114ab2013-02-06 22:40:31 +00004510 }
Douglas Gregor8a114ab2013-02-06 22:40:31 +00004511 }
Douglas Gregor7029ce12013-03-19 00:28:20 +00004512
4513 CurrentModule->setASTFile(F.File);
Douglas Gregor8a114ab2013-02-06 22:40:31 +00004514 }
Ben Langmuirbeee15e2014-04-14 18:00:01 +00004515
Adrian Prantl15bcf702015-06-30 17:39:43 +00004516 CurrentModule->Signature = F.Signature;
Guy Benyei11169dd2012-12-18 14:30:41 +00004517 CurrentModule->IsFromModuleFile = true;
4518 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
Richard Smith9bca2982014-03-08 00:03:56 +00004519 CurrentModule->IsExternC = IsExternC;
Guy Benyei11169dd2012-12-18 14:30:41 +00004520 CurrentModule->InferSubmodules = InferSubmodules;
4521 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
4522 CurrentModule->InferExportWildcard = InferExportWildcard;
Douglas Gregor8d932422013-03-20 03:59:18 +00004523 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
Guy Benyei11169dd2012-12-18 14:30:41 +00004524 if (DeserializationListener)
4525 DeserializationListener->ModuleRead(GlobalID, CurrentModule);
4526
4527 SubmodulesLoaded[GlobalIndex] = CurrentModule;
Douglas Gregor6ddfca92013-01-14 17:21:00 +00004528
Richard Smith8a3e39a2016-03-28 21:31:09 +00004529 // Clear out data that will be replaced by what is in the module file.
Douglas Gregor6ddfca92013-01-14 17:21:00 +00004530 CurrentModule->LinkLibraries.clear();
Douglas Gregor8d932422013-03-20 03:59:18 +00004531 CurrentModule->ConfigMacros.clear();
Douglas Gregorfb912652013-03-20 21:10:35 +00004532 CurrentModule->UnresolvedConflicts.clear();
4533 CurrentModule->Conflicts.clear();
Richard Smith8a3e39a2016-03-28 21:31:09 +00004534
4535 // The module is available unless it's missing a requirement; relevant
4536 // requirements will be (re-)added by SUBMODULE_REQUIRES records.
4537 // Missing headers that were present when the module was built do not
4538 // make it unavailable -- if we got this far, this must be an explicitly
4539 // imported module file.
4540 CurrentModule->Requirements.clear();
4541 CurrentModule->MissingHeaders.clear();
4542 CurrentModule->IsMissingRequirement =
4543 ParentModule && ParentModule->IsMissingRequirement;
4544 CurrentModule->IsAvailable = !CurrentModule->IsMissingRequirement;
Guy Benyei11169dd2012-12-18 14:30:41 +00004545 break;
4546 }
Richard Smith8a3e39a2016-03-28 21:31:09 +00004547
Guy Benyei11169dd2012-12-18 14:30:41 +00004548 case SUBMODULE_UMBRELLA_HEADER: {
Richard Smith2b63d152015-05-16 02:28:53 +00004549 std::string Filename = Blob;
4550 ResolveImportedPath(F, Filename);
4551 if (auto *Umbrella = PP.getFileManager().getFile(Filename)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004552 if (!CurrentModule->getUmbrellaHeader())
Richard Smith2b63d152015-05-16 02:28:53 +00004553 ModMap.setUmbrellaHeader(CurrentModule, Umbrella, Blob);
4554 else if (CurrentModule->getUmbrellaHeader().Entry != Umbrella) {
Ben Langmuirbc35fbe2015-02-20 21:46:39 +00004555 // This can be a spurious difference caused by changing the VFS to
4556 // point to a different copy of the file, and it is too late to
4557 // to rebuild safely.
4558 // FIXME: If we wrote the virtual paths instead of the 'real' paths,
4559 // after input file validation only real problems would remain and we
4560 // could just error. For now, assume it's okay.
4561 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004562 }
4563 }
4564 break;
4565 }
4566
Richard Smith202210b2014-10-24 20:23:01 +00004567 case SUBMODULE_HEADER:
4568 case SUBMODULE_EXCLUDED_HEADER:
4569 case SUBMODULE_PRIVATE_HEADER:
4570 // We lazily associate headers with their modules via the HeaderInfo table.
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00004571 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
4572 // of complete filenames or remove it entirely.
Richard Smith202210b2014-10-24 20:23:01 +00004573 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004574
Richard Smith202210b2014-10-24 20:23:01 +00004575 case SUBMODULE_TEXTUAL_HEADER:
4576 case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
4577 // FIXME: Textual headers are not marked in the HeaderInfo table. Load
4578 // them here.
4579 break;
Lawrence Crowlb53e5482013-06-20 21:14:14 +00004580
Guy Benyei11169dd2012-12-18 14:30:41 +00004581 case SUBMODULE_TOPHEADER: {
Argyrios Kyrtzidis3c5305c2013-03-13 21:13:43 +00004582 CurrentModule->addTopHeaderFilename(Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +00004583 break;
4584 }
4585
4586 case SUBMODULE_UMBRELLA_DIR: {
Richard Smith2b63d152015-05-16 02:28:53 +00004587 std::string Dirname = Blob;
4588 ResolveImportedPath(F, Dirname);
4589 if (auto *Umbrella = PP.getFileManager().getDirectory(Dirname)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004590 if (!CurrentModule->getUmbrellaDir())
Richard Smith2b63d152015-05-16 02:28:53 +00004591 ModMap.setUmbrellaDir(CurrentModule, Umbrella, Blob);
4592 else if (CurrentModule->getUmbrellaDir().Entry != Umbrella) {
Ben Langmuir2c9af442014-04-10 17:57:43 +00004593 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
4594 Error("mismatched umbrella directories in submodule");
4595 return OutOfDate;
Guy Benyei11169dd2012-12-18 14:30:41 +00004596 }
4597 }
4598 break;
4599 }
4600
4601 case SUBMODULE_METADATA: {
Guy Benyei11169dd2012-12-18 14:30:41 +00004602 F.BaseSubmoduleID = getTotalNumSubmodules();
4603 F.LocalNumSubmodules = Record[0];
4604 unsigned LocalBaseSubmoduleID = Record[1];
4605 if (F.LocalNumSubmodules > 0) {
4606 // Introduce the global -> local mapping for submodules within this
4607 // module.
4608 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
4609
4610 // Introduce the local -> global mapping for submodules within this
4611 // module.
4612 F.SubmoduleRemap.insertOrReplace(
4613 std::make_pair(LocalBaseSubmoduleID,
4614 F.BaseSubmoduleID - LocalBaseSubmoduleID));
Ben Langmuirfe971d92014-08-16 04:54:18 +00004615
Ben Langmuir52ca6782014-10-20 16:27:32 +00004616 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
4617 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004618 break;
4619 }
4620
4621 case SUBMODULE_IMPORTS: {
Guy Benyei11169dd2012-12-18 14:30:41 +00004622 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
Douglas Gregorfb912652013-03-20 21:10:35 +00004623 UnresolvedModuleRef Unresolved;
Guy Benyei11169dd2012-12-18 14:30:41 +00004624 Unresolved.File = &F;
4625 Unresolved.Mod = CurrentModule;
4626 Unresolved.ID = Record[Idx];
Douglas Gregorfb912652013-03-20 21:10:35 +00004627 Unresolved.Kind = UnresolvedModuleRef::Import;
Guy Benyei11169dd2012-12-18 14:30:41 +00004628 Unresolved.IsWildcard = false;
Douglas Gregorfb912652013-03-20 21:10:35 +00004629 UnresolvedModuleRefs.push_back(Unresolved);
Guy Benyei11169dd2012-12-18 14:30:41 +00004630 }
4631 break;
4632 }
4633
4634 case SUBMODULE_EXPORTS: {
Guy Benyei11169dd2012-12-18 14:30:41 +00004635 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
Douglas Gregorfb912652013-03-20 21:10:35 +00004636 UnresolvedModuleRef Unresolved;
Guy Benyei11169dd2012-12-18 14:30:41 +00004637 Unresolved.File = &F;
4638 Unresolved.Mod = CurrentModule;
4639 Unresolved.ID = Record[Idx];
Douglas Gregorfb912652013-03-20 21:10:35 +00004640 Unresolved.Kind = UnresolvedModuleRef::Export;
Guy Benyei11169dd2012-12-18 14:30:41 +00004641 Unresolved.IsWildcard = Record[Idx + 1];
Douglas Gregorfb912652013-03-20 21:10:35 +00004642 UnresolvedModuleRefs.push_back(Unresolved);
Guy Benyei11169dd2012-12-18 14:30:41 +00004643 }
4644
4645 // Once we've loaded the set of exports, there's no reason to keep
4646 // the parsed, unresolved exports around.
4647 CurrentModule->UnresolvedExports.clear();
4648 break;
4649 }
4650 case SUBMODULE_REQUIRES: {
Richard Smitha3feee22013-10-28 22:18:19 +00004651 CurrentModule->addRequirement(Blob, Record[0], Context.getLangOpts(),
Guy Benyei11169dd2012-12-18 14:30:41 +00004652 Context.getTargetInfo());
4653 break;
4654 }
Douglas Gregor6ddfca92013-01-14 17:21:00 +00004655
4656 case SUBMODULE_LINK_LIBRARY:
Douglas Gregor6ddfca92013-01-14 17:21:00 +00004657 CurrentModule->LinkLibraries.push_back(
Chris Lattner0e6c9402013-01-20 02:38:54 +00004658 Module::LinkLibrary(Blob, Record[0]));
Douglas Gregor6ddfca92013-01-14 17:21:00 +00004659 break;
Douglas Gregor35b13ec2013-03-20 00:22:05 +00004660
4661 case SUBMODULE_CONFIG_MACRO:
Douglas Gregor35b13ec2013-03-20 00:22:05 +00004662 CurrentModule->ConfigMacros.push_back(Blob.str());
4663 break;
Douglas Gregorfb912652013-03-20 21:10:35 +00004664
4665 case SUBMODULE_CONFLICT: {
Douglas Gregorfb912652013-03-20 21:10:35 +00004666 UnresolvedModuleRef Unresolved;
4667 Unresolved.File = &F;
4668 Unresolved.Mod = CurrentModule;
4669 Unresolved.ID = Record[0];
4670 Unresolved.Kind = UnresolvedModuleRef::Conflict;
4671 Unresolved.IsWildcard = false;
4672 Unresolved.String = Blob;
4673 UnresolvedModuleRefs.push_back(Unresolved);
4674 break;
4675 }
Richard Smithdc1f0422016-07-20 19:10:16 +00004676
4677 case SUBMODULE_INITIALIZERS:
4678 SmallVector<uint32_t, 16> Inits;
4679 for (auto &ID : Record)
4680 Inits.push_back(getGlobalDeclID(F, ID));
4681 Context.addLazyModuleInitializers(CurrentModule, Inits);
4682 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004683 }
4684 }
4685}
4686
4687/// \brief Parse the record that corresponds to a LangOptions data
4688/// structure.
4689///
4690/// This routine parses the language options from the AST file and then gives
4691/// them to the AST listener if one is set.
4692///
4693/// \returns true if the listener deems the file unacceptable, false otherwise.
4694bool ASTReader::ParseLanguageOptions(const RecordData &Record,
4695 bool Complain,
Richard Smith1e2cf0d2014-10-31 02:28:58 +00004696 ASTReaderListener &Listener,
4697 bool AllowCompatibleDifferences) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004698 LangOptions LangOpts;
4699 unsigned Idx = 0;
4700#define LANGOPT(Name, Bits, Default, Description) \
4701 LangOpts.Name = Record[Idx++];
4702#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
4703 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
4704#include "clang/Basic/LangOptions.def"
Alexey Samsonovedf99a92014-11-07 22:29:38 +00004705#define SANITIZER(NAME, ID) \
4706 LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
Will Dietzf54319c2013-01-18 11:30:38 +00004707#include "clang/Basic/Sanitizers.def"
Guy Benyei11169dd2012-12-18 14:30:41 +00004708
Ben Langmuircd98cb72015-06-23 18:20:18 +00004709 for (unsigned N = Record[Idx++]; N; --N)
4710 LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx));
4711
Guy Benyei11169dd2012-12-18 14:30:41 +00004712 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
4713 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
4714 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00004715
Ben Langmuird4a667a2015-06-23 18:20:23 +00004716 LangOpts.CurrentModule = ReadString(Record, Idx);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00004717
4718 // Comment options.
4719 for (unsigned N = Record[Idx++]; N; --N) {
4720 LangOpts.CommentOpts.BlockCommandNames.push_back(
4721 ReadString(Record, Idx));
4722 }
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00004723 LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00004724
Samuel Antaoee8fb302016-01-06 13:42:12 +00004725 // OpenMP offloading options.
4726 for (unsigned N = Record[Idx++]; N; --N) {
4727 LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx)));
4728 }
4729
4730 LangOpts.OMPHostIRFile = ReadString(Record, Idx);
4731
Richard Smith1e2cf0d2014-10-31 02:28:58 +00004732 return Listener.ReadLanguageOptions(LangOpts, Complain,
4733 AllowCompatibleDifferences);
Guy Benyei11169dd2012-12-18 14:30:41 +00004734}
4735
Chandler Carruth0d745bc2015-03-14 04:47:43 +00004736bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain,
4737 ASTReaderListener &Listener,
4738 bool AllowCompatibleDifferences) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004739 unsigned Idx = 0;
4740 TargetOptions TargetOpts;
4741 TargetOpts.Triple = ReadString(Record, Idx);
4742 TargetOpts.CPU = ReadString(Record, Idx);
4743 TargetOpts.ABI = ReadString(Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00004744 for (unsigned N = Record[Idx++]; N; --N) {
4745 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
4746 }
4747 for (unsigned N = Record[Idx++]; N; --N) {
4748 TargetOpts.Features.push_back(ReadString(Record, Idx));
4749 }
4750
Chandler Carruth0d745bc2015-03-14 04:47:43 +00004751 return Listener.ReadTargetOptions(TargetOpts, Complain,
4752 AllowCompatibleDifferences);
Guy Benyei11169dd2012-12-18 14:30:41 +00004753}
4754
4755bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
4756 ASTReaderListener &Listener) {
Ben Langmuirb92de022014-04-29 16:25:26 +00004757 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions);
Guy Benyei11169dd2012-12-18 14:30:41 +00004758 unsigned Idx = 0;
Ben Langmuirb92de022014-04-29 16:25:26 +00004759#define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00004760#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
Ben Langmuirb92de022014-04-29 16:25:26 +00004761 DiagOpts->set##Name(static_cast<Type>(Record[Idx++]));
Guy Benyei11169dd2012-12-18 14:30:41 +00004762#include "clang/Basic/DiagnosticOptions.def"
4763
Richard Smith3be1cb22014-08-07 00:24:21 +00004764 for (unsigned N = Record[Idx++]; N; --N)
Ben Langmuirb92de022014-04-29 16:25:26 +00004765 DiagOpts->Warnings.push_back(ReadString(Record, Idx));
Richard Smith3be1cb22014-08-07 00:24:21 +00004766 for (unsigned N = Record[Idx++]; N; --N)
4767 DiagOpts->Remarks.push_back(ReadString(Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00004768
4769 return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
4770}
4771
4772bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
4773 ASTReaderListener &Listener) {
4774 FileSystemOptions FSOpts;
4775 unsigned Idx = 0;
4776 FSOpts.WorkingDir = ReadString(Record, Idx);
4777 return Listener.ReadFileSystemOptions(FSOpts, Complain);
4778}
4779
4780bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
4781 bool Complain,
4782 ASTReaderListener &Listener) {
4783 HeaderSearchOptions HSOpts;
4784 unsigned Idx = 0;
4785 HSOpts.Sysroot = ReadString(Record, Idx);
4786
4787 // Include entries.
4788 for (unsigned N = Record[Idx++]; N; --N) {
4789 std::string Path = ReadString(Record, Idx);
4790 frontend::IncludeDirGroup Group
4791 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
Guy Benyei11169dd2012-12-18 14:30:41 +00004792 bool IsFramework = Record[Idx++];
4793 bool IgnoreSysRoot = Record[Idx++];
Benjamin Kramer3204b152015-05-29 19:42:19 +00004794 HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework,
4795 IgnoreSysRoot);
Guy Benyei11169dd2012-12-18 14:30:41 +00004796 }
4797
4798 // System header prefixes.
4799 for (unsigned N = Record[Idx++]; N; --N) {
4800 std::string Prefix = ReadString(Record, Idx);
4801 bool IsSystemHeader = Record[Idx++];
Benjamin Kramer3204b152015-05-29 19:42:19 +00004802 HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader);
Guy Benyei11169dd2012-12-18 14:30:41 +00004803 }
4804
4805 HSOpts.ResourceDir = ReadString(Record, Idx);
4806 HSOpts.ModuleCachePath = ReadString(Record, Idx);
Argyrios Kyrtzidis1594c152014-03-03 08:12:05 +00004807 HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00004808 HSOpts.DisableModuleHash = Record[Idx++];
4809 HSOpts.UseBuiltinIncludes = Record[Idx++];
4810 HSOpts.UseStandardSystemIncludes = Record[Idx++];
4811 HSOpts.UseStandardCXXIncludes = Record[Idx++];
4812 HSOpts.UseLibcxx = Record[Idx++];
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00004813 std::string SpecificModuleCachePath = ReadString(Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00004814
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00004815 return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
4816 Complain);
Guy Benyei11169dd2012-12-18 14:30:41 +00004817}
4818
4819bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
4820 bool Complain,
4821 ASTReaderListener &Listener,
4822 std::string &SuggestedPredefines) {
4823 PreprocessorOptions PPOpts;
4824 unsigned Idx = 0;
4825
4826 // Macro definitions/undefs
4827 for (unsigned N = Record[Idx++]; N; --N) {
4828 std::string Macro = ReadString(Record, Idx);
4829 bool IsUndef = Record[Idx++];
4830 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
4831 }
4832
4833 // Includes
4834 for (unsigned N = Record[Idx++]; N; --N) {
4835 PPOpts.Includes.push_back(ReadString(Record, Idx));
4836 }
4837
4838 // Macro Includes
4839 for (unsigned N = Record[Idx++]; N; --N) {
4840 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
4841 }
4842
4843 PPOpts.UsePredefines = Record[Idx++];
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00004844 PPOpts.DetailedRecord = Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00004845 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
4846 PPOpts.ImplicitPTHInclude = ReadString(Record, Idx);
4847 PPOpts.ObjCXXARCStandardLibrary =
4848 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
4849 SuggestedPredefines.clear();
4850 return Listener.ReadPreprocessorOptions(PPOpts, Complain,
4851 SuggestedPredefines);
4852}
4853
4854std::pair<ModuleFile *, unsigned>
4855ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
4856 GlobalPreprocessedEntityMapType::iterator
4857 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
4858 assert(I != GlobalPreprocessedEntityMap.end() &&
4859 "Corrupted global preprocessed entity map");
4860 ModuleFile *M = I->second;
4861 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
4862 return std::make_pair(M, LocalIndex);
4863}
4864
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00004865llvm::iterator_range<PreprocessingRecord::iterator>
Guy Benyei11169dd2012-12-18 14:30:41 +00004866ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
4867 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
4868 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
4869 Mod.NumPreprocessedEntities);
4870
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00004871 return llvm::make_range(PreprocessingRecord::iterator(),
4872 PreprocessingRecord::iterator());
Guy Benyei11169dd2012-12-18 14:30:41 +00004873}
4874
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00004875llvm::iterator_range<ASTReader::ModuleDeclIterator>
Guy Benyei11169dd2012-12-18 14:30:41 +00004876ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00004877 return llvm::make_range(
4878 ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
4879 ModuleDeclIterator(this, &Mod,
4880 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
Guy Benyei11169dd2012-12-18 14:30:41 +00004881}
4882
4883PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
4884 PreprocessedEntityID PPID = Index+1;
4885 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
4886 ModuleFile &M = *PPInfo.first;
4887 unsigned LocalIndex = PPInfo.second;
4888 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
4889
Guy Benyei11169dd2012-12-18 14:30:41 +00004890 if (!PP.getPreprocessingRecord()) {
4891 Error("no preprocessing record");
Craig Toppera13603a2014-05-22 05:54:18 +00004892 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00004893 }
4894
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004895 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
4896 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
4897
4898 llvm::BitstreamEntry Entry =
4899 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
4900 if (Entry.Kind != llvm::BitstreamEntry::Record)
Craig Toppera13603a2014-05-22 05:54:18 +00004901 return nullptr;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004902
Guy Benyei11169dd2012-12-18 14:30:41 +00004903 // Read the record.
Richard Smithcb34bd32016-03-27 07:28:06 +00004904 SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()),
4905 TranslateSourceLocation(M, PPOffs.getEnd()));
Guy Benyei11169dd2012-12-18 14:30:41 +00004906 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
Chris Lattner0e6c9402013-01-20 02:38:54 +00004907 StringRef Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00004908 RecordData Record;
4909 PreprocessorDetailRecordTypes RecType =
Chris Lattner0e6c9402013-01-20 02:38:54 +00004910 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.readRecord(
4911 Entry.ID, Record, &Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +00004912 switch (RecType) {
4913 case PPD_MACRO_EXPANSION: {
4914 bool isBuiltin = Record[0];
Craig Toppera13603a2014-05-22 05:54:18 +00004915 IdentifierInfo *Name = nullptr;
Richard Smith66a81862015-05-04 02:25:31 +00004916 MacroDefinitionRecord *Def = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00004917 if (isBuiltin)
4918 Name = getLocalIdentifier(M, Record[1]);
4919 else {
Richard Smith66a81862015-05-04 02:25:31 +00004920 PreprocessedEntityID GlobalID =
4921 getGlobalPreprocessedEntityID(M, Record[1]);
4922 Def = cast<MacroDefinitionRecord>(
4923 PPRec.getLoadedPreprocessedEntity(GlobalID - 1));
Guy Benyei11169dd2012-12-18 14:30:41 +00004924 }
4925
4926 MacroExpansion *ME;
4927 if (isBuiltin)
4928 ME = new (PPRec) MacroExpansion(Name, Range);
4929 else
4930 ME = new (PPRec) MacroExpansion(Def, Range);
4931
4932 return ME;
4933 }
4934
4935 case PPD_MACRO_DEFINITION: {
4936 // Decode the identifier info and then check again; if the macro is
4937 // still defined and associated with the identifier,
4938 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
Richard Smith66a81862015-05-04 02:25:31 +00004939 MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
Guy Benyei11169dd2012-12-18 14:30:41 +00004940
4941 if (DeserializationListener)
4942 DeserializationListener->MacroDefinitionRead(PPID, MD);
4943
4944 return MD;
4945 }
4946
4947 case PPD_INCLUSION_DIRECTIVE: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00004948 const char *FullFileNameStart = Blob.data() + Record[0];
4949 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
Craig Toppera13603a2014-05-22 05:54:18 +00004950 const FileEntry *File = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00004951 if (!FullFileName.empty())
4952 File = PP.getFileManager().getFile(FullFileName);
4953
4954 // FIXME: Stable encoding
4955 InclusionDirective::InclusionKind Kind
4956 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
4957 InclusionDirective *ID
4958 = new (PPRec) InclusionDirective(PPRec, Kind,
Chris Lattner0e6c9402013-01-20 02:38:54 +00004959 StringRef(Blob.data(), Record[0]),
Guy Benyei11169dd2012-12-18 14:30:41 +00004960 Record[1], Record[3],
4961 File,
4962 Range);
4963 return ID;
4964 }
4965 }
4966
4967 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
4968}
4969
4970/// \brief \arg SLocMapI points at a chunk of a module that contains no
4971/// preprocessed entities or the entities it contains are not the ones we are
4972/// looking for. Find the next module that contains entities and return the ID
4973/// of the first entry.
4974PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
4975 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
4976 ++SLocMapI;
4977 for (GlobalSLocOffsetMapType::const_iterator
4978 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
4979 ModuleFile &M = *SLocMapI->second;
4980 if (M.NumPreprocessedEntities)
4981 return M.BasePreprocessedEntityID;
4982 }
4983
4984 return getTotalNumPreprocessedEntities();
4985}
4986
4987namespace {
4988
Guy Benyei11169dd2012-12-18 14:30:41 +00004989struct PPEntityComp {
4990 const ASTReader &Reader;
4991 ModuleFile &M;
4992
4993 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) { }
4994
4995 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
4996 SourceLocation LHS = getLoc(L);
4997 SourceLocation RHS = getLoc(R);
4998 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4999 }
5000
5001 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
5002 SourceLocation LHS = getLoc(L);
5003 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5004 }
5005
5006 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
5007 SourceLocation RHS = getLoc(R);
5008 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5009 }
5010
5011 SourceLocation getLoc(const PPEntityOffset &PPE) const {
Richard Smithb22a1d12016-03-27 20:13:24 +00005012 return Reader.TranslateSourceLocation(M, PPE.getBegin());
Guy Benyei11169dd2012-12-18 14:30:41 +00005013 }
5014};
5015
Alexander Kornienkoab9db512015-06-22 23:07:51 +00005016}
Guy Benyei11169dd2012-12-18 14:30:41 +00005017
Alp Toker2e9ce4c2014-05-16 18:59:21 +00005018PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
5019 bool EndsAfter) const {
5020 if (SourceMgr.isLocalSourceLocation(Loc))
Guy Benyei11169dd2012-12-18 14:30:41 +00005021 return getTotalNumPreprocessedEntities();
5022
Alp Toker2e9ce4c2014-05-16 18:59:21 +00005023 GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
5024 SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
Guy Benyei11169dd2012-12-18 14:30:41 +00005025 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
5026 "Corrupted global sloc offset map");
5027
5028 if (SLocMapI->second->NumPreprocessedEntities == 0)
5029 return findNextPreprocessedEntity(SLocMapI);
5030
5031 ModuleFile &M = *SLocMapI->second;
5032 typedef const PPEntityOffset *pp_iterator;
5033 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
5034 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
5035
5036 size_t Count = M.NumPreprocessedEntities;
5037 size_t Half;
5038 pp_iterator First = pp_begin;
5039 pp_iterator PPI;
5040
Alp Toker2e9ce4c2014-05-16 18:59:21 +00005041 if (EndsAfter) {
5042 PPI = std::upper_bound(pp_begin, pp_end, Loc,
Richard Smithb22a1d12016-03-27 20:13:24 +00005043 PPEntityComp(*this, M));
Alp Toker2e9ce4c2014-05-16 18:59:21 +00005044 } else {
5045 // Do a binary search manually instead of using std::lower_bound because
5046 // The end locations of entities may be unordered (when a macro expansion
5047 // is inside another macro argument), but for this case it is not important
5048 // whether we get the first macro expansion or its containing macro.
5049 while (Count > 0) {
5050 Half = Count / 2;
5051 PPI = First;
5052 std::advance(PPI, Half);
Richard Smithb22a1d12016-03-27 20:13:24 +00005053 if (SourceMgr.isBeforeInTranslationUnit(
5054 TranslateSourceLocation(M, PPI->getEnd()), Loc)) {
Alp Toker2e9ce4c2014-05-16 18:59:21 +00005055 First = PPI;
5056 ++First;
5057 Count = Count - Half - 1;
5058 } else
5059 Count = Half;
5060 }
Guy Benyei11169dd2012-12-18 14:30:41 +00005061 }
5062
5063 if (PPI == pp_end)
5064 return findNextPreprocessedEntity(SLocMapI);
5065
5066 return M.BasePreprocessedEntityID + (PPI - pp_begin);
5067}
5068
Guy Benyei11169dd2012-12-18 14:30:41 +00005069/// \brief Returns a pair of [Begin, End) indices of preallocated
5070/// preprocessed entities that \arg Range encompasses.
5071std::pair<unsigned, unsigned>
5072 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
5073 if (Range.isInvalid())
5074 return std::make_pair(0,0);
5075 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
5076
Alp Toker2e9ce4c2014-05-16 18:59:21 +00005077 PreprocessedEntityID BeginID =
5078 findPreprocessedEntity(Range.getBegin(), false);
5079 PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true);
Guy Benyei11169dd2012-12-18 14:30:41 +00005080 return std::make_pair(BeginID, EndID);
5081}
5082
5083/// \brief Optionally returns true or false if the preallocated preprocessed
5084/// entity with index \arg Index came from file \arg FID.
David Blaikie05785d12013-02-20 22:23:23 +00005085Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
Guy Benyei11169dd2012-12-18 14:30:41 +00005086 FileID FID) {
5087 if (FID.isInvalid())
5088 return false;
5089
5090 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
5091 ModuleFile &M = *PPInfo.first;
5092 unsigned LocalIndex = PPInfo.second;
5093 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
5094
Richard Smithcb34bd32016-03-27 07:28:06 +00005095 SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin());
Guy Benyei11169dd2012-12-18 14:30:41 +00005096 if (Loc.isInvalid())
5097 return false;
5098
5099 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
5100 return true;
5101 else
5102 return false;
5103}
5104
5105namespace {
5106 /// \brief Visitor used to search for information about a header file.
5107 class HeaderFileInfoVisitor {
Guy Benyei11169dd2012-12-18 14:30:41 +00005108 const FileEntry *FE;
5109
David Blaikie05785d12013-02-20 22:23:23 +00005110 Optional<HeaderFileInfo> HFI;
Guy Benyei11169dd2012-12-18 14:30:41 +00005111
5112 public:
Argyrios Kyrtzidis61a38962013-03-06 18:12:44 +00005113 explicit HeaderFileInfoVisitor(const FileEntry *FE)
5114 : FE(FE) { }
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00005115
5116 bool operator()(ModuleFile &M) {
Guy Benyei11169dd2012-12-18 14:30:41 +00005117 HeaderFileInfoLookupTable *Table
5118 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
5119 if (!Table)
5120 return false;
5121
5122 // Look in the on-disk hash table for an entry for this file name.
Richard Smithbdf2d932015-07-30 03:37:16 +00005123 HeaderFileInfoLookupTable::iterator Pos = Table->find(FE);
Guy Benyei11169dd2012-12-18 14:30:41 +00005124 if (Pos == Table->end())
5125 return false;
5126
Richard Smithbdf2d932015-07-30 03:37:16 +00005127 HFI = *Pos;
Guy Benyei11169dd2012-12-18 14:30:41 +00005128 return true;
5129 }
5130
David Blaikie05785d12013-02-20 22:23:23 +00005131 Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
Guy Benyei11169dd2012-12-18 14:30:41 +00005132 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +00005133}
Guy Benyei11169dd2012-12-18 14:30:41 +00005134
5135HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
Argyrios Kyrtzidis61a38962013-03-06 18:12:44 +00005136 HeaderFileInfoVisitor Visitor(FE);
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00005137 ModuleMgr.visit(Visitor);
Argyrios Kyrtzidis1054bbf2013-05-08 23:46:55 +00005138 if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
Guy Benyei11169dd2012-12-18 14:30:41 +00005139 return *HFI;
Guy Benyei11169dd2012-12-18 14:30:41 +00005140
5141 return HeaderFileInfo();
5142}
5143
5144void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
5145 // FIXME: Make it work properly with modules.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00005146 SmallVector<DiagnosticsEngine::DiagState *, 32> DiagStates;
Guy Benyei11169dd2012-12-18 14:30:41 +00005147 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
5148 ModuleFile &F = *(*I);
5149 unsigned Idx = 0;
5150 DiagStates.clear();
5151 assert(!Diag.DiagStates.empty());
5152 DiagStates.push_back(&Diag.DiagStates.front()); // the command-line one.
5153 while (Idx < F.PragmaDiagMappings.size()) {
5154 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
5155 unsigned DiagStateID = F.PragmaDiagMappings[Idx++];
5156 if (DiagStateID != 0) {
5157 Diag.DiagStatePoints.push_back(
5158 DiagnosticsEngine::DiagStatePoint(DiagStates[DiagStateID-1],
5159 FullSourceLoc(Loc, SourceMgr)));
5160 continue;
5161 }
5162
5163 assert(DiagStateID == 0);
5164 // A new DiagState was created here.
5165 Diag.DiagStates.push_back(*Diag.GetCurDiagState());
5166 DiagnosticsEngine::DiagState *NewState = &Diag.DiagStates.back();
5167 DiagStates.push_back(NewState);
5168 Diag.DiagStatePoints.push_back(
5169 DiagnosticsEngine::DiagStatePoint(NewState,
5170 FullSourceLoc(Loc, SourceMgr)));
5171 while (1) {
5172 assert(Idx < F.PragmaDiagMappings.size() &&
5173 "Invalid data, didn't find '-1' marking end of diag/map pairs");
5174 if (Idx >= F.PragmaDiagMappings.size()) {
5175 break; // Something is messed up but at least avoid infinite loop in
5176 // release build.
5177 }
5178 unsigned DiagID = F.PragmaDiagMappings[Idx++];
5179 if (DiagID == (unsigned)-1) {
5180 break; // no more diag/map pairs for this location.
5181 }
Alp Tokerc726c362014-06-10 09:31:37 +00005182 diag::Severity Map = (diag::Severity)F.PragmaDiagMappings[Idx++];
5183 DiagnosticMapping Mapping = Diag.makeUserMapping(Map, Loc);
5184 Diag.GetCurDiagState()->setMapping(DiagID, Mapping);
Guy Benyei11169dd2012-12-18 14:30:41 +00005185 }
5186 }
5187 }
5188}
5189
5190/// \brief Get the correct cursor and offset for loading a type.
5191ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
5192 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
5193 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
5194 ModuleFile *M = I->second;
5195 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
5196}
5197
5198/// \brief Read and return the type with the given index..
5199///
5200/// The index is the type ID, shifted and minus the number of predefs. This
5201/// routine actually reads the record corresponding to the type at the given
5202/// location. It is a helper routine for GetType, which deals with reading type
5203/// IDs.
5204QualType ASTReader::readTypeRecord(unsigned Index) {
5205 RecordLocation Loc = TypeCursorForIndex(Index);
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005206 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00005207
5208 // Keep track of where we are in the stream, then jump back there
5209 // after reading this type.
5210 SavedStreamPosition SavedPosition(DeclsCursor);
5211
5212 ReadingKindTracker ReadingKind(Read_Type, *this);
5213
5214 // Note that we are loading a type record.
5215 Deserializing AType(this);
5216
5217 unsigned Idx = 0;
5218 DeclsCursor.JumpToBit(Loc.Offset);
5219 RecordData Record;
5220 unsigned Code = DeclsCursor.ReadCode();
Chris Lattner0e6c9402013-01-20 02:38:54 +00005221 switch ((TypeCode)DeclsCursor.readRecord(Code, Record)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00005222 case TYPE_EXT_QUAL: {
5223 if (Record.size() != 2) {
5224 Error("Incorrect encoding of extended qualifier type");
5225 return QualType();
5226 }
5227 QualType Base = readType(*Loc.F, Record, Idx);
5228 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
5229 return Context.getQualifiedType(Base, Quals);
5230 }
5231
5232 case TYPE_COMPLEX: {
5233 if (Record.size() != 1) {
5234 Error("Incorrect encoding of complex type");
5235 return QualType();
5236 }
5237 QualType ElemType = readType(*Loc.F, Record, Idx);
5238 return Context.getComplexType(ElemType);
5239 }
5240
5241 case TYPE_POINTER: {
5242 if (Record.size() != 1) {
5243 Error("Incorrect encoding of pointer type");
5244 return QualType();
5245 }
5246 QualType PointeeType = readType(*Loc.F, Record, Idx);
5247 return Context.getPointerType(PointeeType);
5248 }
5249
Reid Kleckner8a365022013-06-24 17:51:48 +00005250 case TYPE_DECAYED: {
5251 if (Record.size() != 1) {
5252 Error("Incorrect encoding of decayed type");
5253 return QualType();
5254 }
5255 QualType OriginalType = readType(*Loc.F, Record, Idx);
5256 QualType DT = Context.getAdjustedParameterType(OriginalType);
5257 if (!isa<DecayedType>(DT))
5258 Error("Decayed type does not decay");
5259 return DT;
5260 }
5261
Reid Kleckner0503a872013-12-05 01:23:43 +00005262 case TYPE_ADJUSTED: {
5263 if (Record.size() != 2) {
5264 Error("Incorrect encoding of adjusted type");
5265 return QualType();
5266 }
5267 QualType OriginalTy = readType(*Loc.F, Record, Idx);
5268 QualType AdjustedTy = readType(*Loc.F, Record, Idx);
5269 return Context.getAdjustedType(OriginalTy, AdjustedTy);
5270 }
5271
Guy Benyei11169dd2012-12-18 14:30:41 +00005272 case TYPE_BLOCK_POINTER: {
5273 if (Record.size() != 1) {
5274 Error("Incorrect encoding of block pointer type");
5275 return QualType();
5276 }
5277 QualType PointeeType = readType(*Loc.F, Record, Idx);
5278 return Context.getBlockPointerType(PointeeType);
5279 }
5280
5281 case TYPE_LVALUE_REFERENCE: {
5282 if (Record.size() != 2) {
5283 Error("Incorrect encoding of lvalue reference type");
5284 return QualType();
5285 }
5286 QualType PointeeType = readType(*Loc.F, Record, Idx);
5287 return Context.getLValueReferenceType(PointeeType, Record[1]);
5288 }
5289
5290 case TYPE_RVALUE_REFERENCE: {
5291 if (Record.size() != 1) {
5292 Error("Incorrect encoding of rvalue reference type");
5293 return QualType();
5294 }
5295 QualType PointeeType = readType(*Loc.F, Record, Idx);
5296 return Context.getRValueReferenceType(PointeeType);
5297 }
5298
5299 case TYPE_MEMBER_POINTER: {
5300 if (Record.size() != 2) {
5301 Error("Incorrect encoding of member pointer type");
5302 return QualType();
5303 }
5304 QualType PointeeType = readType(*Loc.F, Record, Idx);
5305 QualType ClassType = readType(*Loc.F, Record, Idx);
5306 if (PointeeType.isNull() || ClassType.isNull())
5307 return QualType();
5308
5309 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
5310 }
5311
5312 case TYPE_CONSTANT_ARRAY: {
5313 QualType ElementType = readType(*Loc.F, Record, Idx);
5314 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
5315 unsigned IndexTypeQuals = Record[2];
5316 unsigned Idx = 3;
5317 llvm::APInt Size = ReadAPInt(Record, Idx);
5318 return Context.getConstantArrayType(ElementType, Size,
5319 ASM, IndexTypeQuals);
5320 }
5321
5322 case TYPE_INCOMPLETE_ARRAY: {
5323 QualType ElementType = readType(*Loc.F, Record, Idx);
5324 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
5325 unsigned IndexTypeQuals = Record[2];
5326 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
5327 }
5328
5329 case TYPE_VARIABLE_ARRAY: {
5330 QualType ElementType = readType(*Loc.F, Record, Idx);
5331 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
5332 unsigned IndexTypeQuals = Record[2];
5333 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
5334 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
5335 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
5336 ASM, IndexTypeQuals,
5337 SourceRange(LBLoc, RBLoc));
5338 }
5339
5340 case TYPE_VECTOR: {
5341 if (Record.size() != 3) {
5342 Error("incorrect encoding of vector type in AST file");
5343 return QualType();
5344 }
5345
5346 QualType ElementType = readType(*Loc.F, Record, Idx);
5347 unsigned NumElements = Record[1];
5348 unsigned VecKind = Record[2];
5349 return Context.getVectorType(ElementType, NumElements,
5350 (VectorType::VectorKind)VecKind);
5351 }
5352
5353 case TYPE_EXT_VECTOR: {
5354 if (Record.size() != 3) {
5355 Error("incorrect encoding of extended vector type in AST file");
5356 return QualType();
5357 }
5358
5359 QualType ElementType = readType(*Loc.F, Record, Idx);
5360 unsigned NumElements = Record[1];
5361 return Context.getExtVectorType(ElementType, NumElements);
5362 }
5363
5364 case TYPE_FUNCTION_NO_PROTO: {
5365 if (Record.size() != 6) {
5366 Error("incorrect encoding of no-proto function type");
5367 return QualType();
5368 }
5369 QualType ResultType = readType(*Loc.F, Record, Idx);
5370 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
5371 (CallingConv)Record[4], Record[5]);
5372 return Context.getFunctionNoProtoType(ResultType, Info);
5373 }
5374
5375 case TYPE_FUNCTION_PROTO: {
5376 QualType ResultType = readType(*Loc.F, Record, Idx);
5377
5378 FunctionProtoType::ExtProtoInfo EPI;
5379 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
5380 /*hasregparm*/ Record[2],
5381 /*regparm*/ Record[3],
5382 static_cast<CallingConv>(Record[4]),
5383 /*produces*/ Record[5]);
5384
5385 unsigned Idx = 6;
Guy Benyei11169dd2012-12-18 14:30:41 +00005386
5387 EPI.Variadic = Record[Idx++];
5388 EPI.HasTrailingReturn = Record[Idx++];
5389 EPI.TypeQuals = Record[Idx++];
5390 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
Richard Smith564417a2014-03-20 21:47:22 +00005391 SmallVector<QualType, 8> ExceptionStorage;
Richard Smith8acb4282014-07-31 21:57:55 +00005392 readExceptionSpec(*Loc.F, ExceptionStorage, EPI.ExceptionSpec, Record, Idx);
Richard Smith01b2cb42014-07-26 06:37:51 +00005393
5394 unsigned NumParams = Record[Idx++];
5395 SmallVector<QualType, 16> ParamTypes;
5396 for (unsigned I = 0; I != NumParams; ++I)
5397 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
5398
John McCall18afab72016-03-01 00:49:02 +00005399 SmallVector<FunctionProtoType::ExtParameterInfo, 4> ExtParameterInfos;
5400 if (Idx != Record.size()) {
5401 for (unsigned I = 0; I != NumParams; ++I)
5402 ExtParameterInfos.push_back(
5403 FunctionProtoType::ExtParameterInfo
5404 ::getFromOpaqueValue(Record[Idx++]));
5405 EPI.ExtParameterInfos = ExtParameterInfos.data();
5406 }
5407
5408 assert(Idx == Record.size());
5409
Jordan Rose5c382722013-03-08 21:51:21 +00005410 return Context.getFunctionType(ResultType, ParamTypes, EPI);
Guy Benyei11169dd2012-12-18 14:30:41 +00005411 }
5412
5413 case TYPE_UNRESOLVED_USING: {
5414 unsigned Idx = 0;
5415 return Context.getTypeDeclType(
5416 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
5417 }
5418
5419 case TYPE_TYPEDEF: {
5420 if (Record.size() != 2) {
5421 Error("incorrect encoding of typedef type");
5422 return QualType();
5423 }
5424 unsigned Idx = 0;
5425 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
5426 QualType Canonical = readType(*Loc.F, Record, Idx);
5427 if (!Canonical.isNull())
5428 Canonical = Context.getCanonicalType(Canonical);
5429 return Context.getTypedefType(Decl, Canonical);
5430 }
5431
5432 case TYPE_TYPEOF_EXPR:
5433 return Context.getTypeOfExprType(ReadExpr(*Loc.F));
5434
5435 case TYPE_TYPEOF: {
5436 if (Record.size() != 1) {
5437 Error("incorrect encoding of typeof(type) in AST file");
5438 return QualType();
5439 }
5440 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
5441 return Context.getTypeOfType(UnderlyingType);
5442 }
5443
5444 case TYPE_DECLTYPE: {
5445 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
5446 return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
5447 }
5448
5449 case TYPE_UNARY_TRANSFORM: {
5450 QualType BaseType = readType(*Loc.F, Record, Idx);
5451 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
5452 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
5453 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
5454 }
5455
Richard Smith74aeef52013-04-26 16:15:35 +00005456 case TYPE_AUTO: {
5457 QualType Deduced = readType(*Loc.F, Record, Idx);
Richard Smithe301ba22015-11-11 02:02:15 +00005458 AutoTypeKeyword Keyword = (AutoTypeKeyword)Record[Idx++];
Richard Smith27d807c2013-04-30 13:56:41 +00005459 bool IsDependent = Deduced.isNull() ? Record[Idx++] : false;
Richard Smithe301ba22015-11-11 02:02:15 +00005460 return Context.getAutoType(Deduced, Keyword, IsDependent);
Richard Smith74aeef52013-04-26 16:15:35 +00005461 }
Guy Benyei11169dd2012-12-18 14:30:41 +00005462
5463 case TYPE_RECORD: {
5464 if (Record.size() != 2) {
5465 Error("incorrect encoding of record type");
5466 return QualType();
5467 }
5468 unsigned Idx = 0;
5469 bool IsDependent = Record[Idx++];
5470 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
5471 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
5472 QualType T = Context.getRecordType(RD);
5473 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
5474 return T;
5475 }
5476
5477 case TYPE_ENUM: {
5478 if (Record.size() != 2) {
5479 Error("incorrect encoding of enum type");
5480 return QualType();
5481 }
5482 unsigned Idx = 0;
5483 bool IsDependent = Record[Idx++];
5484 QualType T
5485 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
5486 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
5487 return T;
5488 }
5489
5490 case TYPE_ATTRIBUTED: {
5491 if (Record.size() != 3) {
5492 Error("incorrect encoding of attributed type");
5493 return QualType();
5494 }
5495 QualType modifiedType = readType(*Loc.F, Record, Idx);
5496 QualType equivalentType = readType(*Loc.F, Record, Idx);
5497 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
5498 return Context.getAttributedType(kind, modifiedType, equivalentType);
5499 }
5500
5501 case TYPE_PAREN: {
5502 if (Record.size() != 1) {
5503 Error("incorrect encoding of paren type");
5504 return QualType();
5505 }
5506 QualType InnerType = readType(*Loc.F, Record, Idx);
5507 return Context.getParenType(InnerType);
5508 }
5509
5510 case TYPE_PACK_EXPANSION: {
5511 if (Record.size() != 2) {
5512 Error("incorrect encoding of pack expansion type");
5513 return QualType();
5514 }
5515 QualType Pattern = readType(*Loc.F, Record, Idx);
5516 if (Pattern.isNull())
5517 return QualType();
David Blaikie05785d12013-02-20 22:23:23 +00005518 Optional<unsigned> NumExpansions;
Guy Benyei11169dd2012-12-18 14:30:41 +00005519 if (Record[1])
5520 NumExpansions = Record[1] - 1;
5521 return Context.getPackExpansionType(Pattern, NumExpansions);
5522 }
5523
5524 case TYPE_ELABORATED: {
5525 unsigned Idx = 0;
5526 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
5527 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
5528 QualType NamedType = readType(*Loc.F, Record, Idx);
5529 return Context.getElaboratedType(Keyword, NNS, NamedType);
5530 }
5531
5532 case TYPE_OBJC_INTERFACE: {
5533 unsigned Idx = 0;
5534 ObjCInterfaceDecl *ItfD
5535 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
5536 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
5537 }
5538
5539 case TYPE_OBJC_OBJECT: {
5540 unsigned Idx = 0;
5541 QualType Base = readType(*Loc.F, Record, Idx);
Douglas Gregore9d95f12015-07-07 03:57:35 +00005542 unsigned NumTypeArgs = Record[Idx++];
5543 SmallVector<QualType, 4> TypeArgs;
5544 for (unsigned I = 0; I != NumTypeArgs; ++I)
5545 TypeArgs.push_back(readType(*Loc.F, Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00005546 unsigned NumProtos = Record[Idx++];
5547 SmallVector<ObjCProtocolDecl*, 4> Protos;
5548 for (unsigned I = 0; I != NumProtos; ++I)
5549 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
Douglas Gregorab209d82015-07-07 03:58:42 +00005550 bool IsKindOf = Record[Idx++];
5551 return Context.getObjCObjectType(Base, TypeArgs, Protos, IsKindOf);
Guy Benyei11169dd2012-12-18 14:30:41 +00005552 }
5553
5554 case TYPE_OBJC_OBJECT_POINTER: {
5555 unsigned Idx = 0;
5556 QualType Pointee = readType(*Loc.F, Record, Idx);
5557 return Context.getObjCObjectPointerType(Pointee);
5558 }
5559
5560 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
5561 unsigned Idx = 0;
5562 QualType Parm = readType(*Loc.F, Record, Idx);
5563 QualType Replacement = readType(*Loc.F, Record, Idx);
Stephan Tolksdorfe96f8b32014-03-15 10:23:27 +00005564 return Context.getSubstTemplateTypeParmType(
5565 cast<TemplateTypeParmType>(Parm),
5566 Context.getCanonicalType(Replacement));
Guy Benyei11169dd2012-12-18 14:30:41 +00005567 }
5568
5569 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
5570 unsigned Idx = 0;
5571 QualType Parm = readType(*Loc.F, Record, Idx);
5572 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
5573 return Context.getSubstTemplateTypeParmPackType(
5574 cast<TemplateTypeParmType>(Parm),
5575 ArgPack);
5576 }
5577
5578 case TYPE_INJECTED_CLASS_NAME: {
5579 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
5580 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
5581 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
5582 // for AST reading, too much interdependencies.
Richard Smith6377f8f2014-10-21 21:15:18 +00005583 const Type *T = nullptr;
5584 for (auto *DI = D; DI; DI = DI->getPreviousDecl()) {
5585 if (const Type *Existing = DI->getTypeForDecl()) {
5586 T = Existing;
5587 break;
5588 }
5589 }
5590 if (!T) {
Richard Smithf17fdbd2014-04-24 02:25:27 +00005591 T = new (Context, TypeAlignment) InjectedClassNameType(D, TST);
Richard Smith6377f8f2014-10-21 21:15:18 +00005592 for (auto *DI = D; DI; DI = DI->getPreviousDecl())
5593 DI->setTypeForDecl(T);
5594 }
Richard Smithf17fdbd2014-04-24 02:25:27 +00005595 return QualType(T, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00005596 }
5597
5598 case TYPE_TEMPLATE_TYPE_PARM: {
5599 unsigned Idx = 0;
5600 unsigned Depth = Record[Idx++];
5601 unsigned Index = Record[Idx++];
5602 bool Pack = Record[Idx++];
5603 TemplateTypeParmDecl *D
5604 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
5605 return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
5606 }
5607
5608 case TYPE_DEPENDENT_NAME: {
5609 unsigned Idx = 0;
5610 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
5611 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Richard Smithbdf2d932015-07-30 03:37:16 +00005612 const IdentifierInfo *Name = GetIdentifierInfo(*Loc.F, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00005613 QualType Canon = readType(*Loc.F, Record, Idx);
5614 if (!Canon.isNull())
5615 Canon = Context.getCanonicalType(Canon);
5616 return Context.getDependentNameType(Keyword, NNS, Name, Canon);
5617 }
5618
5619 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
5620 unsigned Idx = 0;
5621 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
5622 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Richard Smithbdf2d932015-07-30 03:37:16 +00005623 const IdentifierInfo *Name = GetIdentifierInfo(*Loc.F, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00005624 unsigned NumArgs = Record[Idx++];
5625 SmallVector<TemplateArgument, 8> Args;
5626 Args.reserve(NumArgs);
5627 while (NumArgs--)
5628 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
5629 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
David Majnemer6fbeee32016-07-07 04:43:07 +00005630 Args);
Guy Benyei11169dd2012-12-18 14:30:41 +00005631 }
5632
5633 case TYPE_DEPENDENT_SIZED_ARRAY: {
5634 unsigned Idx = 0;
5635
5636 // ArrayType
5637 QualType ElementType = readType(*Loc.F, Record, Idx);
5638 ArrayType::ArraySizeModifier ASM
5639 = (ArrayType::ArraySizeModifier)Record[Idx++];
5640 unsigned IndexTypeQuals = Record[Idx++];
5641
5642 // DependentSizedArrayType
5643 Expr *NumElts = ReadExpr(*Loc.F);
5644 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
5645
5646 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
5647 IndexTypeQuals, Brackets);
5648 }
5649
5650 case TYPE_TEMPLATE_SPECIALIZATION: {
5651 unsigned Idx = 0;
5652 bool IsDependent = Record[Idx++];
5653 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
5654 SmallVector<TemplateArgument, 8> Args;
5655 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
5656 QualType Underlying = readType(*Loc.F, Record, Idx);
5657 QualType T;
5658 if (Underlying.isNull())
David Majnemer6fbeee32016-07-07 04:43:07 +00005659 T = Context.getCanonicalTemplateSpecializationType(Name, Args);
Guy Benyei11169dd2012-12-18 14:30:41 +00005660 else
David Majnemer6fbeee32016-07-07 04:43:07 +00005661 T = Context.getTemplateSpecializationType(Name, Args, Underlying);
Guy Benyei11169dd2012-12-18 14:30:41 +00005662 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
5663 return T;
5664 }
5665
5666 case TYPE_ATOMIC: {
5667 if (Record.size() != 1) {
5668 Error("Incorrect encoding of atomic type");
5669 return QualType();
5670 }
5671 QualType ValueType = readType(*Loc.F, Record, Idx);
5672 return Context.getAtomicType(ValueType);
5673 }
Xiuli Pan9c14e282016-01-09 12:53:17 +00005674
5675 case TYPE_PIPE: {
5676 if (Record.size() != 1) {
5677 Error("Incorrect encoding of pipe type");
5678 return QualType();
5679 }
5680
5681 // Reading the pipe element type.
5682 QualType ElementType = readType(*Loc.F, Record, Idx);
5683 return Context.getPipeType(ElementType);
5684 }
Guy Benyei11169dd2012-12-18 14:30:41 +00005685 }
5686 llvm_unreachable("Invalid TypeCode!");
5687}
5688
Richard Smith564417a2014-03-20 21:47:22 +00005689void ASTReader::readExceptionSpec(ModuleFile &ModuleFile,
5690 SmallVectorImpl<QualType> &Exceptions,
Richard Smith8acb4282014-07-31 21:57:55 +00005691 FunctionProtoType::ExceptionSpecInfo &ESI,
Richard Smith564417a2014-03-20 21:47:22 +00005692 const RecordData &Record, unsigned &Idx) {
5693 ExceptionSpecificationType EST =
5694 static_cast<ExceptionSpecificationType>(Record[Idx++]);
Richard Smith8acb4282014-07-31 21:57:55 +00005695 ESI.Type = EST;
Richard Smith564417a2014-03-20 21:47:22 +00005696 if (EST == EST_Dynamic) {
Richard Smith8acb4282014-07-31 21:57:55 +00005697 for (unsigned I = 0, N = Record[Idx++]; I != N; ++I)
Richard Smith564417a2014-03-20 21:47:22 +00005698 Exceptions.push_back(readType(ModuleFile, Record, Idx));
Richard Smith8acb4282014-07-31 21:57:55 +00005699 ESI.Exceptions = Exceptions;
Richard Smith564417a2014-03-20 21:47:22 +00005700 } else if (EST == EST_ComputedNoexcept) {
Richard Smith8acb4282014-07-31 21:57:55 +00005701 ESI.NoexceptExpr = ReadExpr(ModuleFile);
Richard Smith564417a2014-03-20 21:47:22 +00005702 } else if (EST == EST_Uninstantiated) {
Richard Smith8acb4282014-07-31 21:57:55 +00005703 ESI.SourceDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
5704 ESI.SourceTemplate = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
Richard Smith564417a2014-03-20 21:47:22 +00005705 } else if (EST == EST_Unevaluated) {
Richard Smith8acb4282014-07-31 21:57:55 +00005706 ESI.SourceDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
Richard Smith564417a2014-03-20 21:47:22 +00005707 }
5708}
5709
Guy Benyei11169dd2012-12-18 14:30:41 +00005710class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
5711 ASTReader &Reader;
5712 ModuleFile &F;
5713 const ASTReader::RecordData &Record;
5714 unsigned &Idx;
5715
5716 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
5717 unsigned &I) {
5718 return Reader.ReadSourceLocation(F, R, I);
5719 }
5720
5721 template<typename T>
5722 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
5723 return Reader.ReadDeclAs<T>(F, Record, Idx);
5724 }
5725
5726public:
5727 TypeLocReader(ASTReader &Reader, ModuleFile &F,
5728 const ASTReader::RecordData &Record, unsigned &Idx)
5729 : Reader(Reader), F(F), Record(Record), Idx(Idx)
5730 { }
5731
5732 // We want compile-time assurance that we've enumerated all of
5733 // these, so unfortunately we have to declare them first, then
5734 // define them out-of-line.
5735#define ABSTRACT_TYPELOC(CLASS, PARENT)
5736#define TYPELOC(CLASS, PARENT) \
5737 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
5738#include "clang/AST/TypeLocNodes.def"
5739
5740 void VisitFunctionTypeLoc(FunctionTypeLoc);
5741 void VisitArrayTypeLoc(ArrayTypeLoc);
5742};
5743
5744void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
5745 // nothing to do
5746}
5747void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
5748 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
5749 if (TL.needsExtraLocalData()) {
5750 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
5751 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
5752 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
5753 TL.setModeAttr(Record[Idx++]);
5754 }
5755}
5756void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
5757 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5758}
5759void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
5760 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5761}
Reid Kleckner8a365022013-06-24 17:51:48 +00005762void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
5763 // nothing to do
5764}
Reid Kleckner0503a872013-12-05 01:23:43 +00005765void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
5766 // nothing to do
5767}
Guy Benyei11169dd2012-12-18 14:30:41 +00005768void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
5769 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
5770}
5771void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
5772 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
5773}
5774void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
5775 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
5776}
5777void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
5778 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5779 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5780}
5781void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
5782 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
5783 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
5784 if (Record[Idx++])
5785 TL.setSizeExpr(Reader.ReadExpr(F));
5786 else
Craig Toppera13603a2014-05-22 05:54:18 +00005787 TL.setSizeExpr(nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00005788}
5789void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
5790 VisitArrayTypeLoc(TL);
5791}
5792void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
5793 VisitArrayTypeLoc(TL);
5794}
5795void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
5796 VisitArrayTypeLoc(TL);
5797}
5798void TypeLocReader::VisitDependentSizedArrayTypeLoc(
5799 DependentSizedArrayTypeLoc TL) {
5800 VisitArrayTypeLoc(TL);
5801}
5802void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
5803 DependentSizedExtVectorTypeLoc TL) {
5804 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5805}
5806void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
5807 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5808}
5809void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
5810 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5811}
5812void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
5813 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
5814 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5815 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5816 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
Alp Tokerb3fd5cf2014-01-21 00:32:38 +00005817 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
5818 TL.setParam(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00005819 }
5820}
5821void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
5822 VisitFunctionTypeLoc(TL);
5823}
5824void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
5825 VisitFunctionTypeLoc(TL);
5826}
5827void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
5828 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5829}
5830void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
5831 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5832}
5833void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
5834 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
5835 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5836 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5837}
5838void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
5839 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
5840 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5841 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5842 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5843}
5844void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
5845 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5846}
5847void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
5848 TL.setKWLoc(ReadSourceLocation(Record, Idx));
5849 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5850 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5851 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5852}
5853void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
5854 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5855}
5856void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
5857 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5858}
5859void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
5860 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5861}
5862void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
5863 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
5864 if (TL.hasAttrOperand()) {
5865 SourceRange range;
5866 range.setBegin(ReadSourceLocation(Record, Idx));
5867 range.setEnd(ReadSourceLocation(Record, Idx));
5868 TL.setAttrOperandParensRange(range);
5869 }
5870 if (TL.hasAttrExprOperand()) {
5871 if (Record[Idx++])
5872 TL.setAttrExprOperand(Reader.ReadExpr(F));
5873 else
Craig Toppera13603a2014-05-22 05:54:18 +00005874 TL.setAttrExprOperand(nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00005875 } else if (TL.hasAttrEnumOperand())
5876 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
5877}
5878void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
5879 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5880}
5881void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
5882 SubstTemplateTypeParmTypeLoc TL) {
5883 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5884}
5885void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
5886 SubstTemplateTypeParmPackTypeLoc TL) {
5887 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5888}
5889void TypeLocReader::VisitTemplateSpecializationTypeLoc(
5890 TemplateSpecializationTypeLoc TL) {
5891 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
5892 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
5893 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5894 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5895 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
5896 TL.setArgLocInfo(i,
5897 Reader.GetTemplateArgumentLocInfo(F,
5898 TL.getTypePtr()->getArg(i).getKind(),
5899 Record, Idx));
5900}
5901void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
5902 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5903 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5904}
5905void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
5906 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5907 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5908}
5909void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
5910 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5911}
5912void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
5913 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5914 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5915 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5916}
5917void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
5918 DependentTemplateSpecializationTypeLoc TL) {
5919 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5920 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5921 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
5922 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
5923 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5924 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5925 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
5926 TL.setArgLocInfo(I,
5927 Reader.GetTemplateArgumentLocInfo(F,
5928 TL.getTypePtr()->getArg(I).getKind(),
5929 Record, Idx));
5930}
5931void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
5932 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
5933}
5934void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
5935 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5936}
5937void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
5938 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Douglas Gregore9d95f12015-07-07 03:57:35 +00005939 TL.setTypeArgsLAngleLoc(ReadSourceLocation(Record, Idx));
5940 TL.setTypeArgsRAngleLoc(ReadSourceLocation(Record, Idx));
5941 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
5942 TL.setTypeArgTInfo(i, Reader.GetTypeSourceInfo(F, Record, Idx));
5943 TL.setProtocolLAngleLoc(ReadSourceLocation(Record, Idx));
5944 TL.setProtocolRAngleLoc(ReadSourceLocation(Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00005945 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
5946 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
5947}
5948void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
5949 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5950}
5951void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
5952 TL.setKWLoc(ReadSourceLocation(Record, Idx));
5953 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5954 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5955}
Xiuli Pan9c14e282016-01-09 12:53:17 +00005956void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
5957 TL.setKWLoc(ReadSourceLocation(Record, Idx));
5958}
Guy Benyei11169dd2012-12-18 14:30:41 +00005959
5960TypeSourceInfo *ASTReader::GetTypeSourceInfo(ModuleFile &F,
5961 const RecordData &Record,
5962 unsigned &Idx) {
5963 QualType InfoTy = readType(F, Record, Idx);
5964 if (InfoTy.isNull())
Craig Toppera13603a2014-05-22 05:54:18 +00005965 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00005966
5967 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
5968 TypeLocReader TLR(*this, F, Record, Idx);
5969 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
5970 TLR.Visit(TL);
5971 return TInfo;
5972}
5973
5974QualType ASTReader::GetType(TypeID ID) {
5975 unsigned FastQuals = ID & Qualifiers::FastMask;
5976 unsigned Index = ID >> Qualifiers::FastWidth;
5977
5978 if (Index < NUM_PREDEF_TYPE_IDS) {
5979 QualType T;
5980 switch ((PredefinedTypeIDs)Index) {
Alexey Baderbdf7c842015-09-15 12:18:29 +00005981 case PREDEF_TYPE_NULL_ID:
5982 return QualType();
5983 case PREDEF_TYPE_VOID_ID:
5984 T = Context.VoidTy;
5985 break;
5986 case PREDEF_TYPE_BOOL_ID:
5987 T = Context.BoolTy;
5988 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00005989
5990 case PREDEF_TYPE_CHAR_U_ID:
5991 case PREDEF_TYPE_CHAR_S_ID:
5992 // FIXME: Check that the signedness of CharTy is correct!
5993 T = Context.CharTy;
5994 break;
5995
Alexey Baderbdf7c842015-09-15 12:18:29 +00005996 case PREDEF_TYPE_UCHAR_ID:
5997 T = Context.UnsignedCharTy;
5998 break;
5999 case PREDEF_TYPE_USHORT_ID:
6000 T = Context.UnsignedShortTy;
6001 break;
6002 case PREDEF_TYPE_UINT_ID:
6003 T = Context.UnsignedIntTy;
6004 break;
6005 case PREDEF_TYPE_ULONG_ID:
6006 T = Context.UnsignedLongTy;
6007 break;
6008 case PREDEF_TYPE_ULONGLONG_ID:
6009 T = Context.UnsignedLongLongTy;
6010 break;
6011 case PREDEF_TYPE_UINT128_ID:
6012 T = Context.UnsignedInt128Ty;
6013 break;
6014 case PREDEF_TYPE_SCHAR_ID:
6015 T = Context.SignedCharTy;
6016 break;
6017 case PREDEF_TYPE_WCHAR_ID:
6018 T = Context.WCharTy;
6019 break;
6020 case PREDEF_TYPE_SHORT_ID:
6021 T = Context.ShortTy;
6022 break;
6023 case PREDEF_TYPE_INT_ID:
6024 T = Context.IntTy;
6025 break;
6026 case PREDEF_TYPE_LONG_ID:
6027 T = Context.LongTy;
6028 break;
6029 case PREDEF_TYPE_LONGLONG_ID:
6030 T = Context.LongLongTy;
6031 break;
6032 case PREDEF_TYPE_INT128_ID:
6033 T = Context.Int128Ty;
6034 break;
6035 case PREDEF_TYPE_HALF_ID:
6036 T = Context.HalfTy;
6037 break;
6038 case PREDEF_TYPE_FLOAT_ID:
6039 T = Context.FloatTy;
6040 break;
6041 case PREDEF_TYPE_DOUBLE_ID:
6042 T = Context.DoubleTy;
6043 break;
6044 case PREDEF_TYPE_LONGDOUBLE_ID:
6045 T = Context.LongDoubleTy;
6046 break;
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +00006047 case PREDEF_TYPE_FLOAT128_ID:
6048 T = Context.Float128Ty;
6049 break;
Alexey Baderbdf7c842015-09-15 12:18:29 +00006050 case PREDEF_TYPE_OVERLOAD_ID:
6051 T = Context.OverloadTy;
6052 break;
6053 case PREDEF_TYPE_BOUND_MEMBER:
6054 T = Context.BoundMemberTy;
6055 break;
6056 case PREDEF_TYPE_PSEUDO_OBJECT:
6057 T = Context.PseudoObjectTy;
6058 break;
6059 case PREDEF_TYPE_DEPENDENT_ID:
6060 T = Context.DependentTy;
6061 break;
6062 case PREDEF_TYPE_UNKNOWN_ANY:
6063 T = Context.UnknownAnyTy;
6064 break;
6065 case PREDEF_TYPE_NULLPTR_ID:
6066 T = Context.NullPtrTy;
6067 break;
6068 case PREDEF_TYPE_CHAR16_ID:
6069 T = Context.Char16Ty;
6070 break;
6071 case PREDEF_TYPE_CHAR32_ID:
6072 T = Context.Char32Ty;
6073 break;
6074 case PREDEF_TYPE_OBJC_ID:
6075 T = Context.ObjCBuiltinIdTy;
6076 break;
6077 case PREDEF_TYPE_OBJC_CLASS:
6078 T = Context.ObjCBuiltinClassTy;
6079 break;
6080 case PREDEF_TYPE_OBJC_SEL:
6081 T = Context.ObjCBuiltinSelTy;
6082 break;
Alexey Bader954ba212016-04-08 13:40:33 +00006083#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
6084 case PREDEF_TYPE_##Id##_ID: \
6085 T = Context.SingletonId; \
Alexey Baderbdf7c842015-09-15 12:18:29 +00006086 break;
Alexey Baderb62f1442016-04-13 08:33:41 +00006087#include "clang/Basic/OpenCLImageTypes.def"
Alexey Baderbdf7c842015-09-15 12:18:29 +00006088 case PREDEF_TYPE_SAMPLER_ID:
6089 T = Context.OCLSamplerTy;
6090 break;
6091 case PREDEF_TYPE_EVENT_ID:
6092 T = Context.OCLEventTy;
6093 break;
Alexey Bader9c8453f2015-09-15 11:18:52 +00006094 case PREDEF_TYPE_CLK_EVENT_ID:
6095 T = Context.OCLClkEventTy;
6096 break;
6097 case PREDEF_TYPE_QUEUE_ID:
6098 T = Context.OCLQueueTy;
6099 break;
6100 case PREDEF_TYPE_NDRANGE_ID:
6101 T = Context.OCLNDRangeTy;
6102 break;
6103 case PREDEF_TYPE_RESERVE_ID_ID:
6104 T = Context.OCLReserveIDTy;
6105 break;
Alexey Baderbdf7c842015-09-15 12:18:29 +00006106 case PREDEF_TYPE_AUTO_DEDUCT:
6107 T = Context.getAutoDeductType();
6108 break;
6109
6110 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
6111 T = Context.getAutoRRefDeductType();
Guy Benyei11169dd2012-12-18 14:30:41 +00006112 break;
6113
6114 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
6115 T = Context.ARCUnbridgedCastTy;
6116 break;
6117
Guy Benyei11169dd2012-12-18 14:30:41 +00006118 case PREDEF_TYPE_BUILTIN_FN:
6119 T = Context.BuiltinFnTy;
6120 break;
Alexey Bataev1a3320e2015-08-25 14:24:04 +00006121
6122 case PREDEF_TYPE_OMP_ARRAY_SECTION:
6123 T = Context.OMPArraySectionTy;
6124 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00006125 }
6126
6127 assert(!T.isNull() && "Unknown predefined type");
6128 return T.withFastQualifiers(FastQuals);
6129 }
6130
6131 Index -= NUM_PREDEF_TYPE_IDS;
6132 assert(Index < TypesLoaded.size() && "Type index out-of-range");
6133 if (TypesLoaded[Index].isNull()) {
6134 TypesLoaded[Index] = readTypeRecord(Index);
6135 if (TypesLoaded[Index].isNull())
6136 return QualType();
6137
6138 TypesLoaded[Index]->setFromAST();
6139 if (DeserializationListener)
6140 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
6141 TypesLoaded[Index]);
6142 }
6143
6144 return TypesLoaded[Index].withFastQualifiers(FastQuals);
6145}
6146
6147QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
6148 return GetType(getGlobalTypeID(F, LocalID));
6149}
6150
6151serialization::TypeID
6152ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
6153 unsigned FastQuals = LocalID & Qualifiers::FastMask;
6154 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
6155
6156 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
6157 return LocalID;
6158
6159 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6160 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
6161 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
6162
6163 unsigned GlobalIndex = LocalIndex + I->second;
6164 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
6165}
6166
6167TemplateArgumentLocInfo
6168ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
6169 TemplateArgument::ArgKind Kind,
6170 const RecordData &Record,
6171 unsigned &Index) {
6172 switch (Kind) {
6173 case TemplateArgument::Expression:
6174 return ReadExpr(F);
6175 case TemplateArgument::Type:
6176 return GetTypeSourceInfo(F, Record, Index);
6177 case TemplateArgument::Template: {
6178 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
6179 Index);
6180 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
6181 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
6182 SourceLocation());
6183 }
6184 case TemplateArgument::TemplateExpansion: {
6185 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
6186 Index);
6187 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
6188 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
6189 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
6190 EllipsisLoc);
6191 }
6192 case TemplateArgument::Null:
6193 case TemplateArgument::Integral:
6194 case TemplateArgument::Declaration:
6195 case TemplateArgument::NullPtr:
6196 case TemplateArgument::Pack:
6197 // FIXME: Is this right?
6198 return TemplateArgumentLocInfo();
6199 }
6200 llvm_unreachable("unexpected template argument loc");
6201}
6202
6203TemplateArgumentLoc
6204ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
6205 const RecordData &Record, unsigned &Index) {
6206 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
6207
6208 if (Arg.getKind() == TemplateArgument::Expression) {
6209 if (Record[Index++]) // bool InfoHasSameExpr.
6210 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
6211 }
6212 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
6213 Record, Index));
6214}
6215
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00006216const ASTTemplateArgumentListInfo*
6217ASTReader::ReadASTTemplateArgumentListInfo(ModuleFile &F,
6218 const RecordData &Record,
6219 unsigned &Index) {
6220 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Index);
6221 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Index);
6222 unsigned NumArgsAsWritten = Record[Index++];
6223 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
6224 for (unsigned i = 0; i != NumArgsAsWritten; ++i)
6225 TemplArgsInfo.addArgument(ReadTemplateArgumentLoc(F, Record, Index));
6226 return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo);
6227}
6228
Guy Benyei11169dd2012-12-18 14:30:41 +00006229Decl *ASTReader::GetExternalDecl(uint32_t ID) {
6230 return GetDecl(ID);
6231}
6232
Richard Smith50895422015-01-31 03:04:55 +00006233template<typename TemplateSpecializationDecl>
6234static void completeRedeclChainForTemplateSpecialization(Decl *D) {
6235 if (auto *TSD = dyn_cast<TemplateSpecializationDecl>(D))
6236 TSD->getSpecializedTemplate()->LoadLazySpecializations();
6237}
6238
Richard Smith053f6c62014-05-16 23:01:30 +00006239void ASTReader::CompleteRedeclChain(const Decl *D) {
Richard Smith851072e2014-05-19 20:59:20 +00006240 if (NumCurrentElementsDeserializing) {
6241 // We arrange to not care about the complete redeclaration chain while we're
6242 // deserializing. Just remember that the AST has marked this one as complete
6243 // but that it's not actually complete yet, so we know we still need to
6244 // complete it later.
6245 PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D));
6246 return;
6247 }
6248
Richard Smith053f6c62014-05-16 23:01:30 +00006249 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
6250
Richard Smith053f6c62014-05-16 23:01:30 +00006251 // If this is a named declaration, complete it by looking it up
6252 // within its context.
6253 //
Richard Smith01bdb7a2014-08-28 05:44:07 +00006254 // FIXME: Merging a function definition should merge
Richard Smith053f6c62014-05-16 23:01:30 +00006255 // all mergeable entities within it.
6256 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) ||
6257 isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) {
6258 if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) {
Richard Smitha534a312015-07-21 23:54:07 +00006259 if (!getContext().getLangOpts().CPlusPlus &&
6260 isa<TranslationUnitDecl>(DC)) {
Richard Smith053f6c62014-05-16 23:01:30 +00006261 // Outside of C++, we don't have a lookup table for the TU, so update
Richard Smitha534a312015-07-21 23:54:07 +00006262 // the identifier instead. (For C++ modules, we don't store decls
6263 // in the serialized identifier table, so we do the lookup in the TU.)
6264 auto *II = Name.getAsIdentifierInfo();
6265 assert(II && "non-identifier name in C?");
Richard Smith053f6c62014-05-16 23:01:30 +00006266 if (II->isOutOfDate())
6267 updateOutOfDateIdentifier(*II);
6268 } else
6269 DC->lookup(Name);
Richard Smith01bdb7a2014-08-28 05:44:07 +00006270 } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) {
Richard Smith3cb15722015-08-05 22:41:45 +00006271 // Find all declarations of this kind from the relevant context.
6272 for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) {
6273 auto *DC = cast<DeclContext>(DCDecl);
6274 SmallVector<Decl*, 8> Decls;
6275 FindExternalLexicalDecls(
6276 DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls);
6277 }
Richard Smith053f6c62014-05-16 23:01:30 +00006278 }
6279 }
Richard Smith50895422015-01-31 03:04:55 +00006280
6281 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D))
6282 CTSD->getSpecializedTemplate()->LoadLazySpecializations();
6283 if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D))
6284 VTSD->getSpecializedTemplate()->LoadLazySpecializations();
6285 if (auto *FD = dyn_cast<FunctionDecl>(D)) {
6286 if (auto *Template = FD->getPrimaryTemplate())
6287 Template->LoadLazySpecializations();
6288 }
Richard Smith053f6c62014-05-16 23:01:30 +00006289}
6290
Richard Smithc2bb8182015-03-24 06:36:48 +00006291CXXCtorInitializer **
6292ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
6293 RecordLocation Loc = getLocalBitOffset(Offset);
6294 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
6295 SavedStreamPosition SavedPosition(Cursor);
6296 Cursor.JumpToBit(Loc.Offset);
6297 ReadingKindTracker ReadingKind(Read_Decl, *this);
6298
6299 RecordData Record;
6300 unsigned Code = Cursor.ReadCode();
6301 unsigned RecCode = Cursor.readRecord(Code, Record);
6302 if (RecCode != DECL_CXX_CTOR_INITIALIZERS) {
6303 Error("malformed AST file: missing C++ ctor initializers");
6304 return nullptr;
6305 }
6306
6307 unsigned Idx = 0;
6308 return ReadCXXCtorInitializers(*Loc.F, Record, Idx);
6309}
6310
Guy Benyei11169dd2012-12-18 14:30:41 +00006311CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
6312 RecordLocation Loc = getLocalBitOffset(Offset);
Chris Lattner7fb3bef2013-01-20 00:56:42 +00006313 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00006314 SavedStreamPosition SavedPosition(Cursor);
6315 Cursor.JumpToBit(Loc.Offset);
6316 ReadingKindTracker ReadingKind(Read_Decl, *this);
6317 RecordData Record;
6318 unsigned Code = Cursor.ReadCode();
Chris Lattner0e6c9402013-01-20 02:38:54 +00006319 unsigned RecCode = Cursor.readRecord(Code, Record);
Guy Benyei11169dd2012-12-18 14:30:41 +00006320 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00006321 Error("malformed AST file: missing C++ base specifiers");
Craig Toppera13603a2014-05-22 05:54:18 +00006322 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00006323 }
6324
6325 unsigned Idx = 0;
6326 unsigned NumBases = Record[Idx++];
6327 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
6328 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
6329 for (unsigned I = 0; I != NumBases; ++I)
6330 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
6331 return Bases;
6332}
6333
6334serialization::DeclID
6335ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
6336 if (LocalID < NUM_PREDEF_DECL_IDS)
6337 return LocalID;
6338
6339 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6340 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
6341 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
6342
6343 return LocalID + I->second;
6344}
6345
6346bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
6347 ModuleFile &M) const {
Richard Smithfe620d22015-03-05 23:24:12 +00006348 // Predefined decls aren't from any module.
6349 if (ID < NUM_PREDEF_DECL_IDS)
6350 return false;
6351
Richard Smithbcda1a92015-07-12 23:51:20 +00006352 return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID &&
6353 ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls;
Guy Benyei11169dd2012-12-18 14:30:41 +00006354}
6355
Douglas Gregor9f782892013-01-21 15:25:38 +00006356ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
Guy Benyei11169dd2012-12-18 14:30:41 +00006357 if (!D->isFromASTFile())
Craig Toppera13603a2014-05-22 05:54:18 +00006358 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00006359 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
6360 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
6361 return I->second;
6362}
6363
6364SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
6365 if (ID < NUM_PREDEF_DECL_IDS)
6366 return SourceLocation();
Richard Smithcd45dbc2014-04-19 03:48:30 +00006367
Guy Benyei11169dd2012-12-18 14:30:41 +00006368 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
6369
6370 if (Index > DeclsLoaded.size()) {
6371 Error("declaration ID out-of-range for AST file");
6372 return SourceLocation();
6373 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00006374
Guy Benyei11169dd2012-12-18 14:30:41 +00006375 if (Decl *D = DeclsLoaded[Index])
6376 return D->getLocation();
6377
Richard Smithcb34bd32016-03-27 07:28:06 +00006378 SourceLocation Loc;
6379 DeclCursorForID(ID, Loc);
6380 return Loc;
Guy Benyei11169dd2012-12-18 14:30:41 +00006381}
6382
Richard Smithfe620d22015-03-05 23:24:12 +00006383static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) {
6384 switch (ID) {
6385 case PREDEF_DECL_NULL_ID:
6386 return nullptr;
6387
6388 case PREDEF_DECL_TRANSLATION_UNIT_ID:
6389 return Context.getTranslationUnitDecl();
6390
6391 case PREDEF_DECL_OBJC_ID_ID:
6392 return Context.getObjCIdDecl();
6393
6394 case PREDEF_DECL_OBJC_SEL_ID:
6395 return Context.getObjCSelDecl();
6396
6397 case PREDEF_DECL_OBJC_CLASS_ID:
6398 return Context.getObjCClassDecl();
6399
6400 case PREDEF_DECL_OBJC_PROTOCOL_ID:
6401 return Context.getObjCProtocolDecl();
6402
6403 case PREDEF_DECL_INT_128_ID:
6404 return Context.getInt128Decl();
6405
6406 case PREDEF_DECL_UNSIGNED_INT_128_ID:
6407 return Context.getUInt128Decl();
6408
6409 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
6410 return Context.getObjCInstanceTypeDecl();
6411
6412 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
6413 return Context.getBuiltinVaListDecl();
Richard Smithf19e1272015-03-07 00:04:49 +00006414
Richard Smith9b88a4c2015-07-27 05:40:23 +00006415 case PREDEF_DECL_VA_LIST_TAG:
6416 return Context.getVaListTagDecl();
6417
Charles Davisc7d5c942015-09-17 20:55:33 +00006418 case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
6419 return Context.getBuiltinMSVaListDecl();
6420
Richard Smithf19e1272015-03-07 00:04:49 +00006421 case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
6422 return Context.getExternCContextDecl();
David Majnemerd9b1a4f2015-11-04 03:40:30 +00006423
6424 case PREDEF_DECL_MAKE_INTEGER_SEQ_ID:
6425 return Context.getMakeIntegerSeqDecl();
Quentin Colombet043406b2016-02-03 22:41:00 +00006426
6427 case PREDEF_DECL_CF_CONSTANT_STRING_ID:
6428 return Context.getCFConstantStringDecl();
Ben Langmuirf5416742016-02-04 00:55:24 +00006429
6430 case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
6431 return Context.getCFConstantStringTagDecl();
Eric Fiselier6ad68552016-07-01 01:24:09 +00006432
6433 case PREDEF_DECL_TYPE_PACK_ELEMENT_ID:
6434 return Context.getTypePackElementDecl();
Richard Smithfe620d22015-03-05 23:24:12 +00006435 }
Yaron Keren322bdad2015-03-06 07:49:14 +00006436 llvm_unreachable("PredefinedDeclIDs unknown enum value");
Richard Smithfe620d22015-03-05 23:24:12 +00006437}
6438
Richard Smithcd45dbc2014-04-19 03:48:30 +00006439Decl *ASTReader::GetExistingDecl(DeclID ID) {
6440 if (ID < NUM_PREDEF_DECL_IDS) {
Richard Smithfe620d22015-03-05 23:24:12 +00006441 Decl *D = getPredefinedDecl(Context, (PredefinedDeclIDs)ID);
6442 if (D) {
6443 // Track that we have merged the declaration with ID \p ID into the
6444 // pre-existing predefined declaration \p D.
Richard Smith5fc18a92015-07-12 23:43:21 +00006445 auto &Merged = KeyDecls[D->getCanonicalDecl()];
Richard Smithfe620d22015-03-05 23:24:12 +00006446 if (Merged.empty())
6447 Merged.push_back(ID);
Guy Benyei11169dd2012-12-18 14:30:41 +00006448 }
Richard Smithfe620d22015-03-05 23:24:12 +00006449 return D;
Guy Benyei11169dd2012-12-18 14:30:41 +00006450 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00006451
Guy Benyei11169dd2012-12-18 14:30:41 +00006452 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
6453
6454 if (Index >= DeclsLoaded.size()) {
6455 assert(0 && "declaration ID out-of-range for AST file");
6456 Error("declaration ID out-of-range for AST file");
Craig Toppera13603a2014-05-22 05:54:18 +00006457 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00006458 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00006459
6460 return DeclsLoaded[Index];
6461}
6462
6463Decl *ASTReader::GetDecl(DeclID ID) {
6464 if (ID < NUM_PREDEF_DECL_IDS)
6465 return GetExistingDecl(ID);
6466
6467 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
6468
6469 if (Index >= DeclsLoaded.size()) {
6470 assert(0 && "declaration ID out-of-range for AST file");
6471 Error("declaration ID out-of-range for AST file");
Craig Toppera13603a2014-05-22 05:54:18 +00006472 return nullptr;
Richard Smithcd45dbc2014-04-19 03:48:30 +00006473 }
6474
Guy Benyei11169dd2012-12-18 14:30:41 +00006475 if (!DeclsLoaded[Index]) {
6476 ReadDeclRecord(ID);
6477 if (DeserializationListener)
6478 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
6479 }
6480
6481 return DeclsLoaded[Index];
6482}
6483
6484DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
6485 DeclID GlobalID) {
6486 if (GlobalID < NUM_PREDEF_DECL_IDS)
6487 return GlobalID;
6488
6489 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
6490 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
6491 ModuleFile *Owner = I->second;
6492
6493 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
6494 = M.GlobalToLocalDeclIDs.find(Owner);
6495 if (Pos == M.GlobalToLocalDeclIDs.end())
6496 return 0;
6497
6498 return GlobalID - Owner->BaseDeclID + Pos->second;
6499}
6500
6501serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
6502 const RecordData &Record,
6503 unsigned &Idx) {
6504 if (Idx >= Record.size()) {
6505 Error("Corrupted AST file");
6506 return 0;
6507 }
6508
6509 return getGlobalDeclID(F, Record[Idx++]);
6510}
6511
6512/// \brief Resolve the offset of a statement into a statement.
6513///
6514/// This operation will read a new statement from the external
6515/// source each time it is called, and is meant to be used via a
6516/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
6517Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
6518 // Switch case IDs are per Decl.
6519 ClearSwitchCaseIDs();
6520
6521 // Offset here is a global offset across the entire chain.
6522 RecordLocation Loc = getLocalBitOffset(Offset);
6523 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
6524 return ReadStmtFromStream(*Loc.F);
6525}
6526
Richard Smith3cb15722015-08-05 22:41:45 +00006527void ASTReader::FindExternalLexicalDecls(
6528 const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
6529 SmallVectorImpl<Decl *> &Decls) {
Richard Smith82f8fcd2015-08-06 22:07:25 +00006530 bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
6531
Richard Smith9ccdd932015-08-06 22:14:12 +00006532 auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
Richard Smith82f8fcd2015-08-06 22:07:25 +00006533 assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");
6534 for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
6535 auto K = (Decl::Kind)+LexicalDecls[I];
6536 if (!IsKindWeWant(K))
6537 continue;
6538
6539 auto ID = (serialization::DeclID)+LexicalDecls[I + 1];
6540
6541 // Don't add predefined declarations to the lexical context more
6542 // than once.
6543 if (ID < NUM_PREDEF_DECL_IDS) {
6544 if (PredefsVisited[ID])
6545 continue;
6546
6547 PredefsVisited[ID] = true;
6548 }
6549
6550 if (Decl *D = GetLocalDecl(*M, ID)) {
Richard Smith2317a3e2015-08-11 21:21:20 +00006551 assert(D->getKind() == K && "wrong kind for lexical decl");
Richard Smith82f8fcd2015-08-06 22:07:25 +00006552 if (!DC->isDeclInLexicalTraversal(D))
6553 Decls.push_back(D);
6554 }
6555 }
6556 };
6557
6558 if (isa<TranslationUnitDecl>(DC)) {
6559 for (auto Lexical : TULexicalDecls)
6560 Visit(Lexical.first, Lexical.second);
6561 } else {
6562 auto I = LexicalDecls.find(DC);
6563 if (I != LexicalDecls.end())
Richard Smith9c9173d2015-08-11 22:00:24 +00006564 Visit(I->second.first, I->second.second);
Richard Smith82f8fcd2015-08-06 22:07:25 +00006565 }
6566
Guy Benyei11169dd2012-12-18 14:30:41 +00006567 ++NumLexicalDeclContextsRead;
Guy Benyei11169dd2012-12-18 14:30:41 +00006568}
6569
6570namespace {
6571
6572class DeclIDComp {
6573 ASTReader &Reader;
6574 ModuleFile &Mod;
6575
6576public:
6577 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
6578
6579 bool operator()(LocalDeclID L, LocalDeclID R) const {
6580 SourceLocation LHS = getLocation(L);
6581 SourceLocation RHS = getLocation(R);
6582 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6583 }
6584
6585 bool operator()(SourceLocation LHS, LocalDeclID R) const {
6586 SourceLocation RHS = getLocation(R);
6587 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6588 }
6589
6590 bool operator()(LocalDeclID L, SourceLocation RHS) const {
6591 SourceLocation LHS = getLocation(L);
6592 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6593 }
6594
6595 SourceLocation getLocation(LocalDeclID ID) const {
6596 return Reader.getSourceManager().getFileLoc(
6597 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
6598 }
6599};
6600
Alexander Kornienkoab9db512015-06-22 23:07:51 +00006601}
Guy Benyei11169dd2012-12-18 14:30:41 +00006602
6603void ASTReader::FindFileRegionDecls(FileID File,
6604 unsigned Offset, unsigned Length,
6605 SmallVectorImpl<Decl *> &Decls) {
6606 SourceManager &SM = getSourceManager();
6607
6608 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
6609 if (I == FileDeclIDs.end())
6610 return;
6611
6612 FileDeclsInfo &DInfo = I->second;
6613 if (DInfo.Decls.empty())
6614 return;
6615
6616 SourceLocation
6617 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
6618 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
6619
6620 DeclIDComp DIDComp(*this, *DInfo.Mod);
6621 ArrayRef<serialization::LocalDeclID>::iterator
6622 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
6623 BeginLoc, DIDComp);
6624 if (BeginIt != DInfo.Decls.begin())
6625 --BeginIt;
6626
6627 // If we are pointing at a top-level decl inside an objc container, we need
6628 // to backtrack until we find it otherwise we will fail to report that the
6629 // region overlaps with an objc container.
6630 while (BeginIt != DInfo.Decls.begin() &&
6631 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
6632 ->isTopLevelDeclInObjCContainer())
6633 --BeginIt;
6634
6635 ArrayRef<serialization::LocalDeclID>::iterator
6636 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
6637 EndLoc, DIDComp);
6638 if (EndIt != DInfo.Decls.end())
6639 ++EndIt;
6640
6641 for (ArrayRef<serialization::LocalDeclID>::iterator
6642 DIt = BeginIt; DIt != EndIt; ++DIt)
6643 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
6644}
6645
Richard Smith9ce12e32013-02-07 03:30:24 +00006646bool
Guy Benyei11169dd2012-12-18 14:30:41 +00006647ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
6648 DeclarationName Name) {
Richard Smithd88a7f12015-09-01 20:35:42 +00006649 assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
Guy Benyei11169dd2012-12-18 14:30:41 +00006650 "DeclContext has no visible decls in storage");
6651 if (!Name)
Richard Smith9ce12e32013-02-07 03:30:24 +00006652 return false;
Guy Benyei11169dd2012-12-18 14:30:41 +00006653
Richard Smithd88a7f12015-09-01 20:35:42 +00006654 auto It = Lookups.find(DC);
6655 if (It == Lookups.end())
6656 return false;
6657
Richard Smith8c913ec2014-08-14 02:21:01 +00006658 Deserializing LookupResults(this);
6659
Richard Smithd88a7f12015-09-01 20:35:42 +00006660 // Load the list of declarations.
Guy Benyei11169dd2012-12-18 14:30:41 +00006661 SmallVector<NamedDecl *, 64> Decls;
Richard Smithd88a7f12015-09-01 20:35:42 +00006662 for (DeclID ID : It->second.Table.find(Name)) {
6663 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
6664 if (ND->getDeclName() == Name)
6665 Decls.push_back(ND);
6666 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00006667
Guy Benyei11169dd2012-12-18 14:30:41 +00006668 ++NumVisibleDeclContextsRead;
6669 SetExternalVisibleDeclsForName(DC, Name, Decls);
Richard Smith9ce12e32013-02-07 03:30:24 +00006670 return !Decls.empty();
Guy Benyei11169dd2012-12-18 14:30:41 +00006671}
6672
Guy Benyei11169dd2012-12-18 14:30:41 +00006673void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
6674 if (!DC->hasExternalVisibleStorage())
6675 return;
Richard Smithd88a7f12015-09-01 20:35:42 +00006676
6677 auto It = Lookups.find(DC);
6678 assert(It != Lookups.end() &&
6679 "have external visible storage but no lookup tables");
6680
Craig Topper79be4cd2013-07-05 04:33:53 +00006681 DeclsMap Decls;
Guy Benyei11169dd2012-12-18 14:30:41 +00006682
Richard Smithd88a7f12015-09-01 20:35:42 +00006683 for (DeclID ID : It->second.Table.findAll()) {
6684 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
6685 Decls[ND->getDeclName()].push_back(ND);
Guy Benyei11169dd2012-12-18 14:30:41 +00006686 }
6687
Guy Benyei11169dd2012-12-18 14:30:41 +00006688 ++NumVisibleDeclContextsRead;
6689
Craig Topper79be4cd2013-07-05 04:33:53 +00006690 for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
Guy Benyei11169dd2012-12-18 14:30:41 +00006691 SetExternalVisibleDeclsForName(DC, I->first, I->second);
6692 }
6693 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
6694}
6695
Richard Smithd88a7f12015-09-01 20:35:42 +00006696const serialization::reader::DeclContextLookupTable *
6697ASTReader::getLoadedLookupTables(DeclContext *Primary) const {
6698 auto I = Lookups.find(Primary);
6699 return I == Lookups.end() ? nullptr : &I->second;
6700}
6701
Guy Benyei11169dd2012-12-18 14:30:41 +00006702/// \brief Under non-PCH compilation the consumer receives the objc methods
6703/// before receiving the implementation, and codegen depends on this.
6704/// We simulate this by deserializing and passing to consumer the methods of the
6705/// implementation before passing the deserialized implementation decl.
6706static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
6707 ASTConsumer *Consumer) {
6708 assert(ImplD && Consumer);
6709
Aaron Ballmanaff18c02014-03-13 19:03:34 +00006710 for (auto *I : ImplD->methods())
6711 Consumer->HandleInterestingDecl(DeclGroupRef(I));
Guy Benyei11169dd2012-12-18 14:30:41 +00006712
6713 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
6714}
6715
6716void ASTReader::PassInterestingDeclsToConsumer() {
6717 assert(Consumer);
Richard Smith04d05b52014-03-23 00:27:18 +00006718
6719 if (PassingDeclsToConsumer)
6720 return;
6721
6722 // Guard variable to avoid recursively redoing the process of passing
6723 // decls to consumer.
6724 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
6725 true);
6726
Richard Smith9e2341d2015-03-23 03:25:59 +00006727 // Ensure that we've loaded all potentially-interesting declarations
6728 // that need to be eagerly loaded.
6729 for (auto ID : EagerlyDeserializedDecls)
6730 GetDecl(ID);
6731 EagerlyDeserializedDecls.clear();
6732
Guy Benyei11169dd2012-12-18 14:30:41 +00006733 while (!InterestingDecls.empty()) {
6734 Decl *D = InterestingDecls.front();
6735 InterestingDecls.pop_front();
6736
6737 PassInterestingDeclToConsumer(D);
6738 }
6739}
6740
6741void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
6742 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
6743 PassObjCImplDeclToConsumer(ImplD, Consumer);
6744 else
6745 Consumer->HandleInterestingDecl(DeclGroupRef(D));
6746}
6747
6748void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
6749 this->Consumer = Consumer;
6750
Richard Smith9e2341d2015-03-23 03:25:59 +00006751 if (Consumer)
6752 PassInterestingDeclsToConsumer();
Richard Smith7f330cd2015-03-18 01:42:29 +00006753
6754 if (DeserializationListener)
6755 DeserializationListener->ReaderInitialized(this);
Guy Benyei11169dd2012-12-18 14:30:41 +00006756}
6757
6758void ASTReader::PrintStats() {
6759 std::fprintf(stderr, "*** AST File Statistics:\n");
6760
6761 unsigned NumTypesLoaded
6762 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
6763 QualType());
6764 unsigned NumDeclsLoaded
6765 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
Craig Toppera13603a2014-05-22 05:54:18 +00006766 (Decl *)nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00006767 unsigned NumIdentifiersLoaded
6768 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
6769 IdentifiersLoaded.end(),
Craig Toppera13603a2014-05-22 05:54:18 +00006770 (IdentifierInfo *)nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00006771 unsigned NumMacrosLoaded
6772 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
6773 MacrosLoaded.end(),
Craig Toppera13603a2014-05-22 05:54:18 +00006774 (MacroInfo *)nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00006775 unsigned NumSelectorsLoaded
6776 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
6777 SelectorsLoaded.end(),
6778 Selector());
6779
6780 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
6781 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
6782 NumSLocEntriesRead, TotalNumSLocEntries,
6783 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
6784 if (!TypesLoaded.empty())
6785 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
6786 NumTypesLoaded, (unsigned)TypesLoaded.size(),
6787 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
6788 if (!DeclsLoaded.empty())
6789 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
6790 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
6791 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
6792 if (!IdentifiersLoaded.empty())
6793 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
6794 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
6795 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
6796 if (!MacrosLoaded.empty())
6797 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
6798 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
6799 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
6800 if (!SelectorsLoaded.empty())
6801 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
6802 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
6803 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
6804 if (TotalNumStatements)
6805 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
6806 NumStatementsRead, TotalNumStatements,
6807 ((float)NumStatementsRead/TotalNumStatements * 100));
6808 if (TotalNumMacros)
6809 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
6810 NumMacrosRead, TotalNumMacros,
6811 ((float)NumMacrosRead/TotalNumMacros * 100));
6812 if (TotalLexicalDeclContexts)
6813 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
6814 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
6815 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
6816 * 100));
6817 if (TotalVisibleDeclContexts)
6818 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
6819 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
6820 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
6821 * 100));
6822 if (TotalNumMethodPoolEntries) {
6823 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
6824 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
6825 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
6826 * 100));
Guy Benyei11169dd2012-12-18 14:30:41 +00006827 }
Douglas Gregorad2f7a52013-01-28 17:54:36 +00006828 if (NumMethodPoolLookups) {
6829 std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n",
6830 NumMethodPoolHits, NumMethodPoolLookups,
6831 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
6832 }
6833 if (NumMethodPoolTableLookups) {
6834 std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n",
6835 NumMethodPoolTableHits, NumMethodPoolTableLookups,
6836 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
6837 * 100.0));
6838 }
6839
Douglas Gregor00a50f72013-01-25 00:38:33 +00006840 if (NumIdentifierLookupHits) {
6841 std::fprintf(stderr,
6842 " %u / %u identifier table lookups succeeded (%f%%)\n",
6843 NumIdentifierLookupHits, NumIdentifierLookups,
6844 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
6845 }
6846
Douglas Gregore060e572013-01-25 01:03:03 +00006847 if (GlobalIndex) {
6848 std::fprintf(stderr, "\n");
6849 GlobalIndex->printStats();
6850 }
6851
Guy Benyei11169dd2012-12-18 14:30:41 +00006852 std::fprintf(stderr, "\n");
6853 dump();
6854 std::fprintf(stderr, "\n");
6855}
6856
6857template<typename Key, typename ModuleFile, unsigned InitialCapacity>
6858static void
6859dumpModuleIDMap(StringRef Name,
6860 const ContinuousRangeMap<Key, ModuleFile *,
6861 InitialCapacity> &Map) {
6862 if (Map.begin() == Map.end())
6863 return;
6864
6865 typedef ContinuousRangeMap<Key, ModuleFile *, InitialCapacity> MapType;
6866 llvm::errs() << Name << ":\n";
6867 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
6868 I != IEnd; ++I) {
6869 llvm::errs() << " " << I->first << " -> " << I->second->FileName
6870 << "\n";
6871 }
6872}
6873
Yaron Kerencdae9412016-01-29 19:38:18 +00006874LLVM_DUMP_METHOD void ASTReader::dump() {
Guy Benyei11169dd2012-12-18 14:30:41 +00006875 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
6876 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
6877 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
6878 dumpModuleIDMap("Global type map", GlobalTypeMap);
6879 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
6880 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
6881 dumpModuleIDMap("Global macro map", GlobalMacroMap);
6882 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
6883 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
6884 dumpModuleIDMap("Global preprocessed entity map",
6885 GlobalPreprocessedEntityMap);
6886
6887 llvm::errs() << "\n*** PCH/Modules Loaded:";
6888 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
6889 MEnd = ModuleMgr.end();
6890 M != MEnd; ++M)
6891 (*M)->dump();
6892}
6893
6894/// Return the amount of memory used by memory buffers, breaking down
6895/// by heap-backed versus mmap'ed memory.
6896void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
6897 for (ModuleConstIterator I = ModuleMgr.begin(),
6898 E = ModuleMgr.end(); I != E; ++I) {
6899 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
6900 size_t bytes = buf->getBufferSize();
6901 switch (buf->getBufferKind()) {
6902 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
6903 sizes.malloc_bytes += bytes;
6904 break;
6905 case llvm::MemoryBuffer::MemoryBuffer_MMap:
6906 sizes.mmap_bytes += bytes;
6907 break;
6908 }
6909 }
6910 }
6911}
6912
6913void ASTReader::InitializeSema(Sema &S) {
6914 SemaObj = &S;
6915 S.addExternalSource(this);
6916
6917 // Makes sure any declarations that were deserialized "too early"
6918 // still get added to the identifier's declaration chains.
Ben Langmuir5418f402014-09-10 21:29:41 +00006919 for (uint64_t ID : PreloadedDeclIDs) {
6920 NamedDecl *D = cast<NamedDecl>(GetDecl(ID));
6921 pushExternalDeclIntoScope(D, D->getDeclName());
Guy Benyei11169dd2012-12-18 14:30:41 +00006922 }
Ben Langmuir5418f402014-09-10 21:29:41 +00006923 PreloadedDeclIDs.clear();
Guy Benyei11169dd2012-12-18 14:30:41 +00006924
Richard Smith3d8e97e2013-10-18 06:54:39 +00006925 // FIXME: What happens if these are changed by a module import?
Guy Benyei11169dd2012-12-18 14:30:41 +00006926 if (!FPPragmaOptions.empty()) {
6927 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
6928 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
6929 }
6930
Richard Smith3d8e97e2013-10-18 06:54:39 +00006931 // FIXME: What happens if these are changed by a module import?
Guy Benyei11169dd2012-12-18 14:30:41 +00006932 if (!OpenCLExtensions.empty()) {
6933 unsigned I = 0;
6934#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
6935#include "clang/Basic/OpenCLExtensions.def"
6936
6937 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
6938 }
Richard Smith3d8e97e2013-10-18 06:54:39 +00006939
6940 UpdateSema();
6941}
6942
6943void ASTReader::UpdateSema() {
6944 assert(SemaObj && "no Sema to update");
6945
6946 // Load the offsets of the declarations that Sema references.
6947 // They will be lazily deserialized when needed.
6948 if (!SemaDeclRefs.empty()) {
6949 assert(SemaDeclRefs.size() % 2 == 0);
6950 for (unsigned I = 0; I != SemaDeclRefs.size(); I += 2) {
6951 if (!SemaObj->StdNamespace)
6952 SemaObj->StdNamespace = SemaDeclRefs[I];
6953 if (!SemaObj->StdBadAlloc)
6954 SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
6955 }
6956 SemaDeclRefs.clear();
6957 }
Dario Domizioli13a0a382014-05-23 12:13:25 +00006958
Nico Weber779355f2016-03-02 23:22:00 +00006959 // Update the state of pragmas. Use the same API as if we had encountered the
6960 // pragma in the source.
Dario Domizioli13a0a382014-05-23 12:13:25 +00006961 if(OptimizeOffPragmaLocation.isValid())
6962 SemaObj->ActOnPragmaOptimize(/* IsOn = */ false, OptimizeOffPragmaLocation);
Nico Weber779355f2016-03-02 23:22:00 +00006963 if (PragmaMSStructState != -1)
6964 SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState);
Nico Weber42932312016-03-03 00:17:35 +00006965 if (PointersToMembersPragmaLocation.isValid()) {
6966 SemaObj->ActOnPragmaMSPointersToMembers(
6967 (LangOptions::PragmaMSPointersToMembersKind)
6968 PragmaMSPointersToMembersState,
6969 PointersToMembersPragmaLocation);
6970 }
Guy Benyei11169dd2012-12-18 14:30:41 +00006971}
6972
Richard Smitha8d5b6a2015-07-17 19:51:03 +00006973IdentifierInfo *ASTReader::get(StringRef Name) {
Guy Benyei11169dd2012-12-18 14:30:41 +00006974 // Note that we are loading an identifier.
6975 Deserializing AnIdentifier(this);
Douglas Gregore060e572013-01-25 01:03:03 +00006976
Douglas Gregor7211ac12013-01-25 23:32:03 +00006977 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
Douglas Gregor00a50f72013-01-25 00:38:33 +00006978 NumIdentifierLookups,
6979 NumIdentifierLookupHits);
Richard Smith33e0f7e2015-07-22 02:08:40 +00006980
6981 // We don't need to do identifier table lookups in C++ modules (we preload
6982 // all interesting declarations, and don't need to use the scope for name
6983 // lookups). Perform the lookup in PCH files, though, since we don't build
6984 // a complete initial identifier table if we're carrying on from a PCH.
6985 if (Context.getLangOpts().CPlusPlus) {
6986 for (auto F : ModuleMgr.pch_modules())
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00006987 if (Visitor(*F))
Richard Smith33e0f7e2015-07-22 02:08:40 +00006988 break;
6989 } else {
6990 // If there is a global index, look there first to determine which modules
6991 // provably do not have any results for this identifier.
6992 GlobalModuleIndex::HitSet Hits;
6993 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
6994 if (!loadGlobalIndex()) {
6995 if (GlobalIndex->lookupIdentifier(Name, Hits)) {
6996 HitsPtr = &Hits;
6997 }
6998 }
6999
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00007000 ModuleMgr.visit(Visitor, HitsPtr);
Richard Smith33e0f7e2015-07-22 02:08:40 +00007001 }
7002
Guy Benyei11169dd2012-12-18 14:30:41 +00007003 IdentifierInfo *II = Visitor.getIdentifierInfo();
7004 markIdentifierUpToDate(II);
7005 return II;
7006}
7007
7008namespace clang {
7009 /// \brief An identifier-lookup iterator that enumerates all of the
7010 /// identifiers stored within a set of AST files.
7011 class ASTIdentifierIterator : public IdentifierIterator {
7012 /// \brief The AST reader whose identifiers are being enumerated.
7013 const ASTReader &Reader;
7014
7015 /// \brief The current index into the chain of AST files stored in
7016 /// the AST reader.
7017 unsigned Index;
7018
7019 /// \brief The current position within the identifier lookup table
7020 /// of the current AST file.
7021 ASTIdentifierLookupTable::key_iterator Current;
7022
7023 /// \brief The end position within the identifier lookup table of
7024 /// the current AST file.
7025 ASTIdentifierLookupTable::key_iterator End;
7026
Ben Langmuir537c5b52016-05-04 00:53:13 +00007027 /// \brief Whether to skip any modules in the ASTReader.
7028 bool SkipModules;
7029
Guy Benyei11169dd2012-12-18 14:30:41 +00007030 public:
Ben Langmuir537c5b52016-05-04 00:53:13 +00007031 explicit ASTIdentifierIterator(const ASTReader &Reader,
7032 bool SkipModules = false);
Guy Benyei11169dd2012-12-18 14:30:41 +00007033
Craig Topper3e89dfe2014-03-13 02:13:41 +00007034 StringRef Next() override;
Guy Benyei11169dd2012-12-18 14:30:41 +00007035 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +00007036}
Guy Benyei11169dd2012-12-18 14:30:41 +00007037
Ben Langmuir537c5b52016-05-04 00:53:13 +00007038ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader,
7039 bool SkipModules)
7040 : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007041}
7042
7043StringRef ASTIdentifierIterator::Next() {
7044 while (Current == End) {
7045 // If we have exhausted all of our AST files, we're done.
7046 if (Index == 0)
7047 return StringRef();
7048
7049 --Index;
Ben Langmuir537c5b52016-05-04 00:53:13 +00007050 ModuleFile &F = Reader.ModuleMgr[Index];
7051 if (SkipModules && F.isModule())
7052 continue;
7053
7054 ASTIdentifierLookupTable *IdTable =
7055 (ASTIdentifierLookupTable *)F.IdentifierLookupTable;
Guy Benyei11169dd2012-12-18 14:30:41 +00007056 Current = IdTable->key_begin();
7057 End = IdTable->key_end();
7058 }
7059
7060 // We have any identifiers remaining in the current AST file; return
7061 // the next one.
Douglas Gregorbfd73d72013-01-23 18:53:14 +00007062 StringRef Result = *Current;
Guy Benyei11169dd2012-12-18 14:30:41 +00007063 ++Current;
Douglas Gregorbfd73d72013-01-23 18:53:14 +00007064 return Result;
Guy Benyei11169dd2012-12-18 14:30:41 +00007065}
7066
Ben Langmuir537c5b52016-05-04 00:53:13 +00007067namespace {
7068/// A utility for appending two IdentifierIterators.
7069class ChainedIdentifierIterator : public IdentifierIterator {
7070 std::unique_ptr<IdentifierIterator> Current;
7071 std::unique_ptr<IdentifierIterator> Queued;
7072
7073public:
7074 ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First,
7075 std::unique_ptr<IdentifierIterator> Second)
7076 : Current(std::move(First)), Queued(std::move(Second)) {}
7077
7078 StringRef Next() override {
7079 if (!Current)
7080 return StringRef();
7081
7082 StringRef result = Current->Next();
7083 if (!result.empty())
7084 return result;
7085
7086 // Try the queued iterator, which may itself be empty.
7087 Current.reset();
7088 std::swap(Current, Queued);
7089 return Next();
7090 }
7091};
7092} // end anonymous namespace.
7093
Argyrios Kyrtzidis9aca3c62013-04-17 22:10:55 +00007094IdentifierIterator *ASTReader::getIdentifiers() {
Ben Langmuir537c5b52016-05-04 00:53:13 +00007095 if (!loadGlobalIndex()) {
7096 std::unique_ptr<IdentifierIterator> ReaderIter(
7097 new ASTIdentifierIterator(*this, /*SkipModules=*/true));
7098 std::unique_ptr<IdentifierIterator> ModulesIter(
7099 GlobalIndex->createIdentifierIterator());
7100 return new ChainedIdentifierIterator(std::move(ReaderIter),
7101 std::move(ModulesIter));
7102 }
Argyrios Kyrtzidis9aca3c62013-04-17 22:10:55 +00007103
Guy Benyei11169dd2012-12-18 14:30:41 +00007104 return new ASTIdentifierIterator(*this);
7105}
7106
7107namespace clang { namespace serialization {
7108 class ReadMethodPoolVisitor {
7109 ASTReader &Reader;
7110 Selector Sel;
7111 unsigned PriorGeneration;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00007112 unsigned InstanceBits;
7113 unsigned FactoryBits;
Nico Weberff4b35e2014-12-27 22:14:15 +00007114 bool InstanceHasMoreThanOneDecl;
7115 bool FactoryHasMoreThanOneDecl;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00007116 SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
7117 SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
Guy Benyei11169dd2012-12-18 14:30:41 +00007118
7119 public:
Nico Weber2e0c8f72014-12-27 03:58:08 +00007120 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
Guy Benyei11169dd2012-12-18 14:30:41 +00007121 unsigned PriorGeneration)
Nico Weber2e0c8f72014-12-27 03:58:08 +00007122 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration),
Nico Weberff4b35e2014-12-27 22:14:15 +00007123 InstanceBits(0), FactoryBits(0), InstanceHasMoreThanOneDecl(false),
7124 FactoryHasMoreThanOneDecl(false) {}
Nico Weber2e0c8f72014-12-27 03:58:08 +00007125
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00007126 bool operator()(ModuleFile &M) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007127 if (!M.SelectorLookupTable)
7128 return false;
7129
7130 // If we've already searched this module file, skip it now.
Richard Smithbdf2d932015-07-30 03:37:16 +00007131 if (M.Generation <= PriorGeneration)
Guy Benyei11169dd2012-12-18 14:30:41 +00007132 return true;
7133
Richard Smithbdf2d932015-07-30 03:37:16 +00007134 ++Reader.NumMethodPoolTableLookups;
Guy Benyei11169dd2012-12-18 14:30:41 +00007135 ASTSelectorLookupTable *PoolTable
7136 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
Richard Smithbdf2d932015-07-30 03:37:16 +00007137 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
Guy Benyei11169dd2012-12-18 14:30:41 +00007138 if (Pos == PoolTable->end())
7139 return false;
Douglas Gregorad2f7a52013-01-28 17:54:36 +00007140
Richard Smithbdf2d932015-07-30 03:37:16 +00007141 ++Reader.NumMethodPoolTableHits;
7142 ++Reader.NumSelectorsRead;
Guy Benyei11169dd2012-12-18 14:30:41 +00007143 // FIXME: Not quite happy with the statistics here. We probably should
7144 // disable this tracking when called via LoadSelector.
7145 // Also, should entries without methods count as misses?
Richard Smithbdf2d932015-07-30 03:37:16 +00007146 ++Reader.NumMethodPoolEntriesRead;
Guy Benyei11169dd2012-12-18 14:30:41 +00007147 ASTSelectorLookupTrait::data_type Data = *Pos;
Richard Smithbdf2d932015-07-30 03:37:16 +00007148 if (Reader.DeserializationListener)
7149 Reader.DeserializationListener->SelectorRead(Data.ID, Sel);
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00007150
Richard Smithbdf2d932015-07-30 03:37:16 +00007151 InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
7152 FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
7153 InstanceBits = Data.InstanceBits;
7154 FactoryBits = Data.FactoryBits;
7155 InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
7156 FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00007157 return true;
7158 }
7159
7160 /// \brief Retrieve the instance methods found by this visitor.
7161 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
7162 return InstanceMethods;
7163 }
7164
7165 /// \brief Retrieve the instance methods found by this visitor.
7166 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
7167 return FactoryMethods;
7168 }
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00007169
7170 unsigned getInstanceBits() const { return InstanceBits; }
7171 unsigned getFactoryBits() const { return FactoryBits; }
Nico Weberff4b35e2014-12-27 22:14:15 +00007172 bool instanceHasMoreThanOneDecl() const {
7173 return InstanceHasMoreThanOneDecl;
7174 }
7175 bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
Guy Benyei11169dd2012-12-18 14:30:41 +00007176 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +00007177} } // end namespace clang::serialization
Guy Benyei11169dd2012-12-18 14:30:41 +00007178
7179/// \brief Add the given set of methods to the method list.
7180static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
7181 ObjCMethodList &List) {
7182 for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
7183 S.addMethodToGlobalList(&List, Methods[I]);
7184 }
7185}
7186
7187void ASTReader::ReadMethodPool(Selector Sel) {
7188 // Get the selector generation and update it to the current generation.
7189 unsigned &Generation = SelectorGeneration[Sel];
7190 unsigned PriorGeneration = Generation;
Richard Smith053f6c62014-05-16 23:01:30 +00007191 Generation = getGeneration();
Manman Rena0f31a02016-04-29 19:04:05 +00007192 SelectorOutOfDate[Sel] = false;
Guy Benyei11169dd2012-12-18 14:30:41 +00007193
7194 // Search for methods defined with this selector.
Douglas Gregorad2f7a52013-01-28 17:54:36 +00007195 ++NumMethodPoolLookups;
Guy Benyei11169dd2012-12-18 14:30:41 +00007196 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00007197 ModuleMgr.visit(Visitor);
7198
Guy Benyei11169dd2012-12-18 14:30:41 +00007199 if (Visitor.getInstanceMethods().empty() &&
Douglas Gregorad2f7a52013-01-28 17:54:36 +00007200 Visitor.getFactoryMethods().empty())
Guy Benyei11169dd2012-12-18 14:30:41 +00007201 return;
Douglas Gregorad2f7a52013-01-28 17:54:36 +00007202
7203 ++NumMethodPoolHits;
7204
Guy Benyei11169dd2012-12-18 14:30:41 +00007205 if (!getSema())
7206 return;
7207
7208 Sema &S = *getSema();
7209 Sema::GlobalMethodPool::iterator Pos
7210 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
Ben Langmuira0c32e92015-01-12 19:27:00 +00007211
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00007212 Pos->second.first.setBits(Visitor.getInstanceBits());
Nico Weberff4b35e2014-12-27 22:14:15 +00007213 Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00007214 Pos->second.second.setBits(Visitor.getFactoryBits());
Nico Weberff4b35e2014-12-27 22:14:15 +00007215 Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
Ben Langmuira0c32e92015-01-12 19:27:00 +00007216
7217 // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
7218 // when building a module we keep every method individually and may need to
7219 // update hasMoreThanOneDecl as we add the methods.
7220 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
7221 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
Guy Benyei11169dd2012-12-18 14:30:41 +00007222}
7223
Manman Rena0f31a02016-04-29 19:04:05 +00007224void ASTReader::updateOutOfDateSelector(Selector Sel) {
7225 if (SelectorOutOfDate[Sel])
7226 ReadMethodPool(Sel);
7227}
7228
Guy Benyei11169dd2012-12-18 14:30:41 +00007229void ASTReader::ReadKnownNamespaces(
7230 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
7231 Namespaces.clear();
7232
7233 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
7234 if (NamespaceDecl *Namespace
7235 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
7236 Namespaces.push_back(Namespace);
7237 }
7238}
7239
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00007240void ASTReader::ReadUndefinedButUsed(
Richard Smithd6a04d72016-03-25 21:49:43 +00007241 llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00007242 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
7243 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
Nick Lewycky8334af82013-01-26 00:35:08 +00007244 SourceLocation Loc =
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00007245 SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
Nick Lewycky8334af82013-01-26 00:35:08 +00007246 Undefined.insert(std::make_pair(D, Loc));
7247 }
7248}
Nick Lewycky8334af82013-01-26 00:35:08 +00007249
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00007250void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
7251 FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
7252 Exprs) {
7253 for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
7254 FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++]));
7255 uint64_t Count = DelayedDeleteExprs[Idx++];
7256 for (uint64_t C = 0; C < Count; ++C) {
7257 SourceLocation DeleteLoc =
7258 SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]);
7259 const bool IsArrayForm = DelayedDeleteExprs[Idx++];
7260 Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm));
7261 }
7262 }
7263}
7264
Guy Benyei11169dd2012-12-18 14:30:41 +00007265void ASTReader::ReadTentativeDefinitions(
7266 SmallVectorImpl<VarDecl *> &TentativeDefs) {
7267 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
7268 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
7269 if (Var)
7270 TentativeDefs.push_back(Var);
7271 }
7272 TentativeDefinitions.clear();
7273}
7274
7275void ASTReader::ReadUnusedFileScopedDecls(
7276 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
7277 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
7278 DeclaratorDecl *D
7279 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
7280 if (D)
7281 Decls.push_back(D);
7282 }
7283 UnusedFileScopedDecls.clear();
7284}
7285
7286void ASTReader::ReadDelegatingConstructors(
7287 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
7288 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
7289 CXXConstructorDecl *D
7290 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
7291 if (D)
7292 Decls.push_back(D);
7293 }
7294 DelegatingCtorDecls.clear();
7295}
7296
7297void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
7298 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
7299 TypedefNameDecl *D
7300 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
7301 if (D)
7302 Decls.push_back(D);
7303 }
7304 ExtVectorDecls.clear();
7305}
7306
Nico Weber72889432014-09-06 01:25:55 +00007307void ASTReader::ReadUnusedLocalTypedefNameCandidates(
7308 llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
7309 for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
7310 ++I) {
7311 TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
7312 GetDecl(UnusedLocalTypedefNameCandidates[I]));
7313 if (D)
7314 Decls.insert(D);
7315 }
7316 UnusedLocalTypedefNameCandidates.clear();
7317}
7318
Guy Benyei11169dd2012-12-18 14:30:41 +00007319void ASTReader::ReadReferencedSelectors(
7320 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
7321 if (ReferencedSelectorsData.empty())
7322 return;
7323
7324 // If there are @selector references added them to its pool. This is for
7325 // implementation of -Wselector.
7326 unsigned int DataSize = ReferencedSelectorsData.size()-1;
7327 unsigned I = 0;
7328 while (I < DataSize) {
7329 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
7330 SourceLocation SelLoc
7331 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
7332 Sels.push_back(std::make_pair(Sel, SelLoc));
7333 }
7334 ReferencedSelectorsData.clear();
7335}
7336
7337void ASTReader::ReadWeakUndeclaredIdentifiers(
7338 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
7339 if (WeakUndeclaredIdentifiers.empty())
7340 return;
7341
7342 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
7343 IdentifierInfo *WeakId
7344 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
7345 IdentifierInfo *AliasId
7346 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
7347 SourceLocation Loc
7348 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
7349 bool Used = WeakUndeclaredIdentifiers[I++];
7350 WeakInfo WI(AliasId, Loc);
7351 WI.setUsed(Used);
7352 WeakIDs.push_back(std::make_pair(WeakId, WI));
7353 }
7354 WeakUndeclaredIdentifiers.clear();
7355}
7356
7357void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
7358 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
7359 ExternalVTableUse VT;
7360 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
7361 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
7362 VT.DefinitionRequired = VTableUses[Idx++];
7363 VTables.push_back(VT);
7364 }
7365
7366 VTableUses.clear();
7367}
7368
7369void ASTReader::ReadPendingInstantiations(
7370 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
7371 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
7372 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
7373 SourceLocation Loc
7374 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
7375
7376 Pending.push_back(std::make_pair(D, Loc));
7377 }
7378 PendingInstantiations.clear();
7379}
7380
Richard Smithe40f2ba2013-08-07 21:41:30 +00007381void ASTReader::ReadLateParsedTemplates(
Chandler Carruth52cee4d2015-03-26 09:08:15 +00007382 llvm::MapVector<const FunctionDecl *, LateParsedTemplate *> &LPTMap) {
Richard Smithe40f2ba2013-08-07 21:41:30 +00007383 for (unsigned Idx = 0, N = LateParsedTemplates.size(); Idx < N;
7384 /* In loop */) {
7385 FunctionDecl *FD = cast<FunctionDecl>(GetDecl(LateParsedTemplates[Idx++]));
7386
7387 LateParsedTemplate *LT = new LateParsedTemplate;
7388 LT->D = GetDecl(LateParsedTemplates[Idx++]);
7389
7390 ModuleFile *F = getOwningModuleFile(LT->D);
7391 assert(F && "No module");
7392
7393 unsigned TokN = LateParsedTemplates[Idx++];
7394 LT->Toks.reserve(TokN);
7395 for (unsigned T = 0; T < TokN; ++T)
7396 LT->Toks.push_back(ReadToken(*F, LateParsedTemplates, Idx));
7397
Chandler Carruth52cee4d2015-03-26 09:08:15 +00007398 LPTMap.insert(std::make_pair(FD, LT));
Richard Smithe40f2ba2013-08-07 21:41:30 +00007399 }
7400
7401 LateParsedTemplates.clear();
7402}
7403
Guy Benyei11169dd2012-12-18 14:30:41 +00007404void ASTReader::LoadSelector(Selector Sel) {
7405 // It would be complicated to avoid reading the methods anyway. So don't.
7406 ReadMethodPool(Sel);
7407}
7408
7409void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
7410 assert(ID && "Non-zero identifier ID required");
7411 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
7412 IdentifiersLoaded[ID - 1] = II;
7413 if (DeserializationListener)
7414 DeserializationListener->IdentifierRead(ID, II);
7415}
7416
7417/// \brief Set the globally-visible declarations associated with the given
7418/// identifier.
7419///
7420/// If the AST reader is currently in a state where the given declaration IDs
7421/// cannot safely be resolved, they are queued until it is safe to resolve
7422/// them.
7423///
7424/// \param II an IdentifierInfo that refers to one or more globally-visible
7425/// declarations.
7426///
7427/// \param DeclIDs the set of declaration IDs with the name @p II that are
7428/// visible at global scope.
7429///
Douglas Gregor6168bd22013-02-18 15:53:43 +00007430/// \param Decls if non-null, this vector will be populated with the set of
7431/// deserialized declarations. These declarations will not be pushed into
7432/// scope.
Guy Benyei11169dd2012-12-18 14:30:41 +00007433void
7434ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
7435 const SmallVectorImpl<uint32_t> &DeclIDs,
Douglas Gregor6168bd22013-02-18 15:53:43 +00007436 SmallVectorImpl<Decl *> *Decls) {
7437 if (NumCurrentElementsDeserializing && !Decls) {
7438 PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
Guy Benyei11169dd2012-12-18 14:30:41 +00007439 return;
7440 }
7441
7442 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
Ben Langmuir5418f402014-09-10 21:29:41 +00007443 if (!SemaObj) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007444 // Queue this declaration so that it will be added to the
7445 // translation unit scope and identifier's declaration chain
7446 // once a Sema object is known.
Ben Langmuir5418f402014-09-10 21:29:41 +00007447 PreloadedDeclIDs.push_back(DeclIDs[I]);
7448 continue;
Guy Benyei11169dd2012-12-18 14:30:41 +00007449 }
Ben Langmuir5418f402014-09-10 21:29:41 +00007450
7451 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
7452
7453 // If we're simply supposed to record the declarations, do so now.
7454 if (Decls) {
7455 Decls->push_back(D);
7456 continue;
7457 }
7458
7459 // Introduce this declaration into the translation-unit scope
7460 // and add it to the declaration chain for this identifier, so
7461 // that (unqualified) name lookup will find it.
7462 pushExternalDeclIntoScope(D, II);
Guy Benyei11169dd2012-12-18 14:30:41 +00007463 }
7464}
7465
Douglas Gregorc8a992f2013-01-21 16:52:34 +00007466IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007467 if (ID == 0)
Craig Toppera13603a2014-05-22 05:54:18 +00007468 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007469
7470 if (IdentifiersLoaded.empty()) {
7471 Error("no identifier table in AST file");
Craig Toppera13603a2014-05-22 05:54:18 +00007472 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007473 }
7474
7475 ID -= 1;
7476 if (!IdentifiersLoaded[ID]) {
7477 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
7478 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
7479 ModuleFile *M = I->second;
7480 unsigned Index = ID - M->BaseIdentifierID;
7481 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
7482
7483 // All of the strings in the AST file are preceded by a 16-bit length.
7484 // Extract that 16-bit length to avoid having to execute strlen().
7485 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
7486 // unsigned integers. This is important to avoid integer overflow when
7487 // we cast them to 'unsigned'.
7488 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
7489 unsigned StrLen = (((unsigned) StrLenPtr[0])
7490 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Richard Smitheb4b58f62016-02-05 01:40:54 +00007491 auto &II = PP.getIdentifierTable().get(StringRef(Str, StrLen));
7492 IdentifiersLoaded[ID] = &II;
7493 markIdentifierFromAST(*this, II);
Guy Benyei11169dd2012-12-18 14:30:41 +00007494 if (DeserializationListener)
Richard Smitheb4b58f62016-02-05 01:40:54 +00007495 DeserializationListener->IdentifierRead(ID + 1, &II);
Guy Benyei11169dd2012-12-18 14:30:41 +00007496 }
7497
7498 return IdentifiersLoaded[ID];
7499}
7500
Douglas Gregorc8a992f2013-01-21 16:52:34 +00007501IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
7502 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
Guy Benyei11169dd2012-12-18 14:30:41 +00007503}
7504
7505IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
7506 if (LocalID < NUM_PREDEF_IDENT_IDS)
7507 return LocalID;
7508
7509 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7510 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
7511 assert(I != M.IdentifierRemap.end()
7512 && "Invalid index into identifier index remap");
7513
7514 return LocalID + I->second;
7515}
7516
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00007517MacroInfo *ASTReader::getMacro(MacroID ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007518 if (ID == 0)
Craig Toppera13603a2014-05-22 05:54:18 +00007519 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007520
7521 if (MacrosLoaded.empty()) {
7522 Error("no macro table in AST file");
Craig Toppera13603a2014-05-22 05:54:18 +00007523 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007524 }
7525
7526 ID -= NUM_PREDEF_MACRO_IDS;
7527 if (!MacrosLoaded[ID]) {
7528 GlobalMacroMapType::iterator I
7529 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
7530 assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
7531 ModuleFile *M = I->second;
7532 unsigned Index = ID - M->BaseMacroID;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00007533 MacrosLoaded[ID] = ReadMacroRecord(*M, M->MacroOffsets[Index]);
7534
7535 if (DeserializationListener)
7536 DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
7537 MacrosLoaded[ID]);
Guy Benyei11169dd2012-12-18 14:30:41 +00007538 }
7539
7540 return MacrosLoaded[ID];
7541}
7542
7543MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
7544 if (LocalID < NUM_PREDEF_MACRO_IDS)
7545 return LocalID;
7546
7547 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7548 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
7549 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
7550
7551 return LocalID + I->second;
7552}
7553
7554serialization::SubmoduleID
7555ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
7556 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
7557 return LocalID;
7558
7559 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7560 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
7561 assert(I != M.SubmoduleRemap.end()
7562 && "Invalid index into submodule index remap");
7563
7564 return LocalID + I->second;
7565}
7566
7567Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
7568 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
7569 assert(GlobalID == 0 && "Unhandled global submodule ID");
Craig Toppera13603a2014-05-22 05:54:18 +00007570 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007571 }
7572
7573 if (GlobalID > SubmodulesLoaded.size()) {
7574 Error("submodule ID out of range in AST file");
Craig Toppera13603a2014-05-22 05:54:18 +00007575 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007576 }
7577
7578 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
7579}
Douglas Gregorc147b0b2013-01-12 01:29:50 +00007580
7581Module *ASTReader::getModule(unsigned ID) {
7582 return getSubmodule(ID);
7583}
7584
Richard Smithd88a7f12015-09-01 20:35:42 +00007585ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) {
7586 if (ID & 1) {
7587 // It's a module, look it up by submodule ID.
7588 auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1));
7589 return I == GlobalSubmoduleMap.end() ? nullptr : I->second;
7590 } else {
7591 // It's a prefix (preamble, PCH, ...). Look it up by index.
7592 unsigned IndexFromEnd = ID >> 1;
7593 assert(IndexFromEnd && "got reference to unknown module file");
7594 return getModuleManager().pch_modules().end()[-IndexFromEnd];
7595 }
7596}
7597
7598unsigned ASTReader::getModuleFileID(ModuleFile *F) {
7599 if (!F)
7600 return 1;
7601
7602 // For a file representing a module, use the submodule ID of the top-level
7603 // module as the file ID. For any other kind of file, the number of such
7604 // files loaded beforehand will be the same on reload.
7605 // FIXME: Is this true even if we have an explicit module file and a PCH?
7606 if (F->isModule())
7607 return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;
7608
7609 auto PCHModules = getModuleManager().pch_modules();
7610 auto I = std::find(PCHModules.begin(), PCHModules.end(), F);
7611 assert(I != PCHModules.end() && "emitting reference to unknown file");
7612 return (I - PCHModules.end()) << 1;
7613}
7614
Adrian Prantl15bcf702015-06-30 17:39:43 +00007615llvm::Optional<ExternalASTSource::ASTSourceDescriptor>
7616ASTReader::getSourceDescriptor(unsigned ID) {
7617 if (const Module *M = getSubmodule(ID))
Adrian Prantlc6458d62015-09-19 00:10:32 +00007618 return ExternalASTSource::ASTSourceDescriptor(*M);
Adrian Prantl15bcf702015-06-30 17:39:43 +00007619
7620 // If there is only a single PCH, return it instead.
7621 // Chained PCH are not suported.
7622 if (ModuleMgr.size() == 1) {
7623 ModuleFile &MF = ModuleMgr.getPrimaryModule();
Adrian Prantl3a2d4942016-01-22 23:30:56 +00007624 StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName);
Adrian Prantl9bc3c4f2016-04-27 17:06:22 +00007625 StringRef FileName = llvm::sys::path::filename(MF.FileName);
7626 return ASTReader::ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName,
7627 MF.Signature);
Adrian Prantl15bcf702015-06-30 17:39:43 +00007628 }
7629 return None;
7630}
7631
Guy Benyei11169dd2012-12-18 14:30:41 +00007632Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
7633 return DecodeSelector(getGlobalSelectorID(M, LocalID));
7634}
7635
7636Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
7637 if (ID == 0)
7638 return Selector();
7639
7640 if (ID > SelectorsLoaded.size()) {
7641 Error("selector ID out of range in AST file");
7642 return Selector();
7643 }
7644
Craig Toppera13603a2014-05-22 05:54:18 +00007645 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007646 // Load this selector from the selector table.
7647 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
7648 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
7649 ModuleFile &M = *I->second;
7650 ASTSelectorLookupTrait Trait(*this, M);
7651 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
7652 SelectorsLoaded[ID - 1] =
7653 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
7654 if (DeserializationListener)
7655 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
7656 }
7657
7658 return SelectorsLoaded[ID - 1];
7659}
7660
7661Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
7662 return DecodeSelector(ID);
7663}
7664
7665uint32_t ASTReader::GetNumExternalSelectors() {
7666 // ID 0 (the null selector) is considered an external selector.
7667 return getTotalNumSelectors() + 1;
7668}
7669
7670serialization::SelectorID
7671ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
7672 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
7673 return LocalID;
7674
7675 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7676 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
7677 assert(I != M.SelectorRemap.end()
7678 && "Invalid index into selector index remap");
7679
7680 return LocalID + I->second;
7681}
7682
7683DeclarationName
7684ASTReader::ReadDeclarationName(ModuleFile &F,
7685 const RecordData &Record, unsigned &Idx) {
7686 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
7687 switch (Kind) {
7688 case DeclarationName::Identifier:
Douglas Gregorc8a992f2013-01-21 16:52:34 +00007689 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00007690
7691 case DeclarationName::ObjCZeroArgSelector:
7692 case DeclarationName::ObjCOneArgSelector:
7693 case DeclarationName::ObjCMultiArgSelector:
7694 return DeclarationName(ReadSelector(F, Record, Idx));
7695
7696 case DeclarationName::CXXConstructorName:
7697 return Context.DeclarationNames.getCXXConstructorName(
7698 Context.getCanonicalType(readType(F, Record, Idx)));
7699
7700 case DeclarationName::CXXDestructorName:
7701 return Context.DeclarationNames.getCXXDestructorName(
7702 Context.getCanonicalType(readType(F, Record, Idx)));
7703
7704 case DeclarationName::CXXConversionFunctionName:
7705 return Context.DeclarationNames.getCXXConversionFunctionName(
7706 Context.getCanonicalType(readType(F, Record, Idx)));
7707
7708 case DeclarationName::CXXOperatorName:
7709 return Context.DeclarationNames.getCXXOperatorName(
7710 (OverloadedOperatorKind)Record[Idx++]);
7711
7712 case DeclarationName::CXXLiteralOperatorName:
7713 return Context.DeclarationNames.getCXXLiteralOperatorName(
7714 GetIdentifierInfo(F, Record, Idx));
7715
7716 case DeclarationName::CXXUsingDirective:
7717 return DeclarationName::getUsingDirectiveName();
7718 }
7719
7720 llvm_unreachable("Invalid NameKind!");
7721}
7722
7723void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
7724 DeclarationNameLoc &DNLoc,
7725 DeclarationName Name,
7726 const RecordData &Record, unsigned &Idx) {
7727 switch (Name.getNameKind()) {
7728 case DeclarationName::CXXConstructorName:
7729 case DeclarationName::CXXDestructorName:
7730 case DeclarationName::CXXConversionFunctionName:
7731 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
7732 break;
7733
7734 case DeclarationName::CXXOperatorName:
7735 DNLoc.CXXOperatorName.BeginOpNameLoc
7736 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
7737 DNLoc.CXXOperatorName.EndOpNameLoc
7738 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
7739 break;
7740
7741 case DeclarationName::CXXLiteralOperatorName:
7742 DNLoc.CXXLiteralOperatorName.OpNameLoc
7743 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
7744 break;
7745
7746 case DeclarationName::Identifier:
7747 case DeclarationName::ObjCZeroArgSelector:
7748 case DeclarationName::ObjCOneArgSelector:
7749 case DeclarationName::ObjCMultiArgSelector:
7750 case DeclarationName::CXXUsingDirective:
7751 break;
7752 }
7753}
7754
7755void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
7756 DeclarationNameInfo &NameInfo,
7757 const RecordData &Record, unsigned &Idx) {
7758 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
7759 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
7760 DeclarationNameLoc DNLoc;
7761 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
7762 NameInfo.setInfo(DNLoc);
7763}
7764
7765void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
7766 const RecordData &Record, unsigned &Idx) {
7767 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
7768 unsigned NumTPLists = Record[Idx++];
7769 Info.NumTemplParamLists = NumTPLists;
7770 if (NumTPLists) {
7771 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
7772 for (unsigned i=0; i != NumTPLists; ++i)
7773 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
7774 }
7775}
7776
7777TemplateName
7778ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
7779 unsigned &Idx) {
7780 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
7781 switch (Kind) {
7782 case TemplateName::Template:
7783 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
7784
7785 case TemplateName::OverloadedTemplate: {
7786 unsigned size = Record[Idx++];
7787 UnresolvedSet<8> Decls;
7788 while (size--)
7789 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
7790
7791 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
7792 }
7793
7794 case TemplateName::QualifiedTemplate: {
7795 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
7796 bool hasTemplKeyword = Record[Idx++];
7797 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
7798 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
7799 }
7800
7801 case TemplateName::DependentTemplate: {
7802 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
7803 if (Record[Idx++]) // isIdentifier
7804 return Context.getDependentTemplateName(NNS,
7805 GetIdentifierInfo(F, Record,
7806 Idx));
7807 return Context.getDependentTemplateName(NNS,
7808 (OverloadedOperatorKind)Record[Idx++]);
7809 }
7810
7811 case TemplateName::SubstTemplateTemplateParm: {
7812 TemplateTemplateParmDecl *param
7813 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
7814 if (!param) return TemplateName();
7815 TemplateName replacement = ReadTemplateName(F, Record, Idx);
7816 return Context.getSubstTemplateTemplateParm(param, replacement);
7817 }
7818
7819 case TemplateName::SubstTemplateTemplateParmPack: {
7820 TemplateTemplateParmDecl *Param
7821 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
7822 if (!Param)
7823 return TemplateName();
7824
7825 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
7826 if (ArgPack.getKind() != TemplateArgument::Pack)
7827 return TemplateName();
7828
7829 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
7830 }
7831 }
7832
7833 llvm_unreachable("Unhandled template name kind!");
7834}
7835
Richard Smith2bb3c342015-08-09 01:05:31 +00007836TemplateArgument ASTReader::ReadTemplateArgument(ModuleFile &F,
7837 const RecordData &Record,
7838 unsigned &Idx,
7839 bool Canonicalize) {
7840 if (Canonicalize) {
7841 // The caller wants a canonical template argument. Sometimes the AST only
7842 // wants template arguments in canonical form (particularly as the template
7843 // argument lists of template specializations) so ensure we preserve that
7844 // canonical form across serialization.
7845 TemplateArgument Arg = ReadTemplateArgument(F, Record, Idx, false);
7846 return Context.getCanonicalTemplateArgument(Arg);
7847 }
7848
Guy Benyei11169dd2012-12-18 14:30:41 +00007849 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
7850 switch (Kind) {
7851 case TemplateArgument::Null:
7852 return TemplateArgument();
7853 case TemplateArgument::Type:
7854 return TemplateArgument(readType(F, Record, Idx));
7855 case TemplateArgument::Declaration: {
7856 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
David Blaikie0f62c8d2014-10-16 04:21:25 +00007857 return TemplateArgument(D, readType(F, Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00007858 }
7859 case TemplateArgument::NullPtr:
7860 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
7861 case TemplateArgument::Integral: {
7862 llvm::APSInt Value = ReadAPSInt(Record, Idx);
7863 QualType T = readType(F, Record, Idx);
7864 return TemplateArgument(Context, Value, T);
7865 }
7866 case TemplateArgument::Template:
7867 return TemplateArgument(ReadTemplateName(F, Record, Idx));
7868 case TemplateArgument::TemplateExpansion: {
7869 TemplateName Name = ReadTemplateName(F, Record, Idx);
David Blaikie05785d12013-02-20 22:23:23 +00007870 Optional<unsigned> NumTemplateExpansions;
Guy Benyei11169dd2012-12-18 14:30:41 +00007871 if (unsigned NumExpansions = Record[Idx++])
7872 NumTemplateExpansions = NumExpansions - 1;
7873 return TemplateArgument(Name, NumTemplateExpansions);
7874 }
7875 case TemplateArgument::Expression:
7876 return TemplateArgument(ReadExpr(F));
7877 case TemplateArgument::Pack: {
7878 unsigned NumArgs = Record[Idx++];
7879 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
7880 for (unsigned I = 0; I != NumArgs; ++I)
7881 Args[I] = ReadTemplateArgument(F, Record, Idx);
Benjamin Kramercce63472015-08-05 09:40:22 +00007882 return TemplateArgument(llvm::makeArrayRef(Args, NumArgs));
Guy Benyei11169dd2012-12-18 14:30:41 +00007883 }
7884 }
7885
7886 llvm_unreachable("Unhandled template argument kind!");
7887}
7888
7889TemplateParameterList *
7890ASTReader::ReadTemplateParameterList(ModuleFile &F,
7891 const RecordData &Record, unsigned &Idx) {
7892 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
7893 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
7894 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
7895
7896 unsigned NumParams = Record[Idx++];
7897 SmallVector<NamedDecl *, 16> Params;
7898 Params.reserve(NumParams);
7899 while (NumParams--)
7900 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
7901
7902 TemplateParameterList* TemplateParams =
7903 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
Hubert Tong286547a2016-07-20 01:05:31 +00007904 Params, RAngleLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00007905 return TemplateParams;
7906}
7907
7908void
7909ASTReader::
Craig Topper5603df42013-07-05 19:34:19 +00007910ReadTemplateArgumentList(SmallVectorImpl<TemplateArgument> &TemplArgs,
Guy Benyei11169dd2012-12-18 14:30:41 +00007911 ModuleFile &F, const RecordData &Record,
Richard Smith2bb3c342015-08-09 01:05:31 +00007912 unsigned &Idx, bool Canonicalize) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007913 unsigned NumTemplateArgs = Record[Idx++];
7914 TemplArgs.reserve(NumTemplateArgs);
7915 while (NumTemplateArgs--)
Richard Smith2bb3c342015-08-09 01:05:31 +00007916 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx, Canonicalize));
Guy Benyei11169dd2012-12-18 14:30:41 +00007917}
7918
7919/// \brief Read a UnresolvedSet structure.
Richard Smitha4ba74c2013-08-30 04:46:40 +00007920void ASTReader::ReadUnresolvedSet(ModuleFile &F, LazyASTUnresolvedSet &Set,
Guy Benyei11169dd2012-12-18 14:30:41 +00007921 const RecordData &Record, unsigned &Idx) {
7922 unsigned NumDecls = Record[Idx++];
7923 Set.reserve(Context, NumDecls);
7924 while (NumDecls--) {
Richard Smitha4ba74c2013-08-30 04:46:40 +00007925 DeclID ID = ReadDeclID(F, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00007926 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
Richard Smitha4ba74c2013-08-30 04:46:40 +00007927 Set.addLazyDecl(Context, ID, AS);
Guy Benyei11169dd2012-12-18 14:30:41 +00007928 }
7929}
7930
7931CXXBaseSpecifier
7932ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
7933 const RecordData &Record, unsigned &Idx) {
7934 bool isVirtual = static_cast<bool>(Record[Idx++]);
7935 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
7936 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
7937 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
7938 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
7939 SourceRange Range = ReadSourceRange(F, Record, Idx);
7940 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
7941 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
7942 EllipsisLoc);
7943 Result.setInheritConstructors(inheritConstructors);
7944 return Result;
7945}
7946
Richard Smithc2bb8182015-03-24 06:36:48 +00007947CXXCtorInitializer **
Guy Benyei11169dd2012-12-18 14:30:41 +00007948ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
7949 unsigned &Idx) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007950 unsigned NumInitializers = Record[Idx++];
Richard Smithc2bb8182015-03-24 06:36:48 +00007951 assert(NumInitializers && "wrote ctor initializers but have no inits");
7952 auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
7953 for (unsigned i = 0; i != NumInitializers; ++i) {
7954 TypeSourceInfo *TInfo = nullptr;
7955 bool IsBaseVirtual = false;
7956 FieldDecl *Member = nullptr;
7957 IndirectFieldDecl *IndirectMember = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007958
Richard Smithc2bb8182015-03-24 06:36:48 +00007959 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
7960 switch (Type) {
7961 case CTOR_INITIALIZER_BASE:
7962 TInfo = GetTypeSourceInfo(F, Record, Idx);
7963 IsBaseVirtual = Record[Idx++];
7964 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00007965
Richard Smithc2bb8182015-03-24 06:36:48 +00007966 case CTOR_INITIALIZER_DELEGATING:
7967 TInfo = GetTypeSourceInfo(F, Record, Idx);
7968 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00007969
Richard Smithc2bb8182015-03-24 06:36:48 +00007970 case CTOR_INITIALIZER_MEMBER:
7971 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
7972 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00007973
Richard Smithc2bb8182015-03-24 06:36:48 +00007974 case CTOR_INITIALIZER_INDIRECT_MEMBER:
7975 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
7976 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00007977 }
Richard Smithc2bb8182015-03-24 06:36:48 +00007978
7979 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
7980 Expr *Init = ReadExpr(F);
7981 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
7982 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
7983 bool IsWritten = Record[Idx++];
7984 unsigned SourceOrderOrNumArrayIndices;
7985 SmallVector<VarDecl *, 8> Indices;
7986 if (IsWritten) {
7987 SourceOrderOrNumArrayIndices = Record[Idx++];
7988 } else {
7989 SourceOrderOrNumArrayIndices = Record[Idx++];
7990 Indices.reserve(SourceOrderOrNumArrayIndices);
7991 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
7992 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
7993 }
7994
7995 CXXCtorInitializer *BOMInit;
7996 if (Type == CTOR_INITIALIZER_BASE) {
7997 BOMInit = new (Context)
7998 CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
7999 RParenLoc, MemberOrEllipsisLoc);
8000 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
8001 BOMInit = new (Context)
8002 CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
8003 } else if (IsWritten) {
8004 if (Member)
8005 BOMInit = new (Context) CXXCtorInitializer(
8006 Context, Member, MemberOrEllipsisLoc, LParenLoc, Init, RParenLoc);
8007 else
8008 BOMInit = new (Context)
8009 CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
8010 LParenLoc, Init, RParenLoc);
8011 } else {
8012 if (IndirectMember) {
8013 assert(Indices.empty() && "Indirect field improperly initialized");
8014 BOMInit = new (Context)
8015 CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
8016 LParenLoc, Init, RParenLoc);
8017 } else {
8018 BOMInit = CXXCtorInitializer::Create(
8019 Context, Member, MemberOrEllipsisLoc, LParenLoc, Init, RParenLoc,
8020 Indices.data(), Indices.size());
8021 }
8022 }
8023
8024 if (IsWritten)
8025 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
8026 CtorInitializers[i] = BOMInit;
Guy Benyei11169dd2012-12-18 14:30:41 +00008027 }
8028
Richard Smithc2bb8182015-03-24 06:36:48 +00008029 return CtorInitializers;
Guy Benyei11169dd2012-12-18 14:30:41 +00008030}
8031
8032NestedNameSpecifier *
8033ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
8034 const RecordData &Record, unsigned &Idx) {
8035 unsigned N = Record[Idx++];
Craig Toppera13603a2014-05-22 05:54:18 +00008036 NestedNameSpecifier *NNS = nullptr, *Prev = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00008037 for (unsigned I = 0; I != N; ++I) {
8038 NestedNameSpecifier::SpecifierKind Kind
8039 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
8040 switch (Kind) {
8041 case NestedNameSpecifier::Identifier: {
8042 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
8043 NNS = NestedNameSpecifier::Create(Context, Prev, II);
8044 break;
8045 }
8046
8047 case NestedNameSpecifier::Namespace: {
8048 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
8049 NNS = NestedNameSpecifier::Create(Context, Prev, NS);
8050 break;
8051 }
8052
8053 case NestedNameSpecifier::NamespaceAlias: {
8054 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
8055 NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
8056 break;
8057 }
8058
8059 case NestedNameSpecifier::TypeSpec:
8060 case NestedNameSpecifier::TypeSpecWithTemplate: {
8061 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
8062 if (!T)
Craig Toppera13603a2014-05-22 05:54:18 +00008063 return nullptr;
8064
Guy Benyei11169dd2012-12-18 14:30:41 +00008065 bool Template = Record[Idx++];
8066 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
8067 break;
8068 }
8069
8070 case NestedNameSpecifier::Global: {
8071 NNS = NestedNameSpecifier::GlobalSpecifier(Context);
8072 // No associated value, and there can't be a prefix.
8073 break;
8074 }
Nikola Smiljanic67860242014-09-26 00:28:20 +00008075
8076 case NestedNameSpecifier::Super: {
8077 CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(F, Record, Idx);
8078 NNS = NestedNameSpecifier::SuperSpecifier(Context, RD);
8079 break;
8080 }
Guy Benyei11169dd2012-12-18 14:30:41 +00008081 }
8082 Prev = NNS;
8083 }
8084 return NNS;
8085}
8086
8087NestedNameSpecifierLoc
8088ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
8089 unsigned &Idx) {
8090 unsigned N = Record[Idx++];
8091 NestedNameSpecifierLocBuilder Builder;
8092 for (unsigned I = 0; I != N; ++I) {
8093 NestedNameSpecifier::SpecifierKind Kind
8094 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
8095 switch (Kind) {
8096 case NestedNameSpecifier::Identifier: {
8097 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
8098 SourceRange Range = ReadSourceRange(F, Record, Idx);
8099 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
8100 break;
8101 }
8102
8103 case NestedNameSpecifier::Namespace: {
8104 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
8105 SourceRange Range = ReadSourceRange(F, Record, Idx);
8106 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
8107 break;
8108 }
8109
8110 case NestedNameSpecifier::NamespaceAlias: {
8111 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
8112 SourceRange Range = ReadSourceRange(F, Record, Idx);
8113 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
8114 break;
8115 }
8116
8117 case NestedNameSpecifier::TypeSpec:
8118 case NestedNameSpecifier::TypeSpecWithTemplate: {
8119 bool Template = Record[Idx++];
8120 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
8121 if (!T)
8122 return NestedNameSpecifierLoc();
8123 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
8124
8125 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
8126 Builder.Extend(Context,
8127 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
8128 T->getTypeLoc(), ColonColonLoc);
8129 break;
8130 }
8131
8132 case NestedNameSpecifier::Global: {
8133 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
8134 Builder.MakeGlobal(Context, ColonColonLoc);
8135 break;
8136 }
Nikola Smiljanic67860242014-09-26 00:28:20 +00008137
8138 case NestedNameSpecifier::Super: {
8139 CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(F, Record, Idx);
8140 SourceRange Range = ReadSourceRange(F, Record, Idx);
8141 Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd());
8142 break;
8143 }
Guy Benyei11169dd2012-12-18 14:30:41 +00008144 }
8145 }
Nikola Smiljanic67860242014-09-26 00:28:20 +00008146
Guy Benyei11169dd2012-12-18 14:30:41 +00008147 return Builder.getWithLocInContext(Context);
8148}
8149
8150SourceRange
8151ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
8152 unsigned &Idx) {
8153 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
8154 SourceLocation end = ReadSourceLocation(F, Record, Idx);
8155 return SourceRange(beg, end);
8156}
8157
8158/// \brief Read an integral value
8159llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
8160 unsigned BitWidth = Record[Idx++];
8161 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
8162 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
8163 Idx += NumWords;
8164 return Result;
8165}
8166
8167/// \brief Read a signed integral value
8168llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
8169 bool isUnsigned = Record[Idx++];
8170 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
8171}
8172
8173/// \brief Read a floating-point value
Tim Northover178723a2013-01-22 09:46:51 +00008174llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record,
8175 const llvm::fltSemantics &Sem,
8176 unsigned &Idx) {
8177 return llvm::APFloat(Sem, ReadAPInt(Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00008178}
8179
8180// \brief Read a string
8181std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
8182 unsigned Len = Record[Idx++];
8183 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
8184 Idx += Len;
8185 return Result;
8186}
8187
Richard Smith7ed1bc92014-12-05 22:42:13 +00008188std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
8189 unsigned &Idx) {
8190 std::string Filename = ReadString(Record, Idx);
8191 ResolveImportedPath(F, Filename);
8192 return Filename;
8193}
8194
Guy Benyei11169dd2012-12-18 14:30:41 +00008195VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
8196 unsigned &Idx) {
8197 unsigned Major = Record[Idx++];
8198 unsigned Minor = Record[Idx++];
8199 unsigned Subminor = Record[Idx++];
8200 if (Minor == 0)
8201 return VersionTuple(Major);
8202 if (Subminor == 0)
8203 return VersionTuple(Major, Minor - 1);
8204 return VersionTuple(Major, Minor - 1, Subminor - 1);
8205}
8206
8207CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
8208 const RecordData &Record,
8209 unsigned &Idx) {
8210 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
8211 return CXXTemporary::Create(Context, Decl);
8212}
8213
8214DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Argyrios Kyrtzidisdc9fdaf2013-05-24 05:44:08 +00008215 return Diag(CurrentImportLoc, DiagID);
Guy Benyei11169dd2012-12-18 14:30:41 +00008216}
8217
8218DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
8219 return Diags.Report(Loc, DiagID);
8220}
8221
8222/// \brief Retrieve the identifier table associated with the
8223/// preprocessor.
8224IdentifierTable &ASTReader::getIdentifierTable() {
8225 return PP.getIdentifierTable();
8226}
8227
8228/// \brief Record that the given ID maps to the given switch-case
8229/// statement.
8230void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Craig Toppera13603a2014-05-22 05:54:18 +00008231 assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
Guy Benyei11169dd2012-12-18 14:30:41 +00008232 "Already have a SwitchCase with this ID");
8233 (*CurrSwitchCaseStmts)[ID] = SC;
8234}
8235
8236/// \brief Retrieve the switch-case statement with the given ID.
8237SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Craig Toppera13603a2014-05-22 05:54:18 +00008238 assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
Guy Benyei11169dd2012-12-18 14:30:41 +00008239 return (*CurrSwitchCaseStmts)[ID];
8240}
8241
8242void ASTReader::ClearSwitchCaseIDs() {
8243 CurrSwitchCaseStmts->clear();
8244}
8245
8246void ASTReader::ReadComments() {
8247 std::vector<RawComment *> Comments;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00008248 for (SmallVectorImpl<std::pair<BitstreamCursor,
Guy Benyei11169dd2012-12-18 14:30:41 +00008249 serialization::ModuleFile *> >::iterator
8250 I = CommentsCursors.begin(),
8251 E = CommentsCursors.end();
8252 I != E; ++I) {
Dmitri Gribenko9ee0e302014-03-27 15:40:39 +00008253 Comments.clear();
Chris Lattner7fb3bef2013-01-20 00:56:42 +00008254 BitstreamCursor &Cursor = I->first;
Guy Benyei11169dd2012-12-18 14:30:41 +00008255 serialization::ModuleFile &F = *I->second;
8256 SavedStreamPosition SavedPosition(Cursor);
8257
8258 RecordData Record;
8259 while (true) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00008260 llvm::BitstreamEntry Entry =
8261 Cursor.advanceSkippingSubblocks(BitstreamCursor::AF_DontPopBlockAtEnd);
Dmitri Gribenko9ee0e302014-03-27 15:40:39 +00008262
Chris Lattner7fb3bef2013-01-20 00:56:42 +00008263 switch (Entry.Kind) {
8264 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
8265 case llvm::BitstreamEntry::Error:
8266 Error("malformed block record in AST file");
8267 return;
8268 case llvm::BitstreamEntry::EndBlock:
8269 goto NextCursor;
8270 case llvm::BitstreamEntry::Record:
8271 // The interesting case.
Guy Benyei11169dd2012-12-18 14:30:41 +00008272 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00008273 }
8274
8275 // Read a record.
8276 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00008277 switch ((CommentRecordTypes)Cursor.readRecord(Entry.ID, Record)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008278 case COMMENTS_RAW_COMMENT: {
8279 unsigned Idx = 0;
8280 SourceRange SR = ReadSourceRange(F, Record, Idx);
8281 RawComment::CommentKind Kind =
8282 (RawComment::CommentKind) Record[Idx++];
8283 bool IsTrailingComment = Record[Idx++];
8284 bool IsAlmostTrailingComment = Record[Idx++];
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00008285 Comments.push_back(new (Context) RawComment(
8286 SR, Kind, IsTrailingComment, IsAlmostTrailingComment,
8287 Context.getLangOpts().CommentOpts.ParseAllComments));
Guy Benyei11169dd2012-12-18 14:30:41 +00008288 break;
8289 }
8290 }
8291 }
Dmitri Gribenko9ee0e302014-03-27 15:40:39 +00008292 NextCursor:
8293 Context.Comments.addDeserializedComments(Comments);
Guy Benyei11169dd2012-12-18 14:30:41 +00008294 }
Guy Benyei11169dd2012-12-18 14:30:41 +00008295}
8296
Richard Smithcd45dbc2014-04-19 03:48:30 +00008297std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) {
8298 // If we know the owning module, use it.
Richard Smith42413142015-05-15 20:05:43 +00008299 if (Module *M = D->getImportedOwningModule())
Richard Smithcd45dbc2014-04-19 03:48:30 +00008300 return M->getFullModuleName();
8301
8302 // Otherwise, use the name of the top-level module the decl is within.
8303 if (ModuleFile *M = getOwningModuleFile(D))
8304 return M->ModuleName;
8305
8306 // Not from a module.
8307 return "";
8308}
8309
Guy Benyei11169dd2012-12-18 14:30:41 +00008310void ASTReader::finishPendingActions() {
Richard Smith851072e2014-05-19 20:59:20 +00008311 while (!PendingIdentifierInfos.empty() ||
8312 !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() ||
Richard Smith2b9e3e32013-10-18 06:05:18 +00008313 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
Richard Smitha0ce9c42014-07-29 23:23:27 +00008314 !PendingUpdateRecords.empty()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008315 // If any identifiers with corresponding top-level declarations have
8316 // been loaded, load those declarations now.
Craig Topper79be4cd2013-07-05 04:33:53 +00008317 typedef llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2> >
8318 TopLevelDeclsMap;
8319 TopLevelDeclsMap TopLevelDecls;
8320
Guy Benyei11169dd2012-12-18 14:30:41 +00008321 while (!PendingIdentifierInfos.empty()) {
Douglas Gregor6168bd22013-02-18 15:53:43 +00008322 IdentifierInfo *II = PendingIdentifierInfos.back().first;
Richard Smithf0ae3c2d2014-03-28 17:31:23 +00008323 SmallVector<uint32_t, 4> DeclIDs =
8324 std::move(PendingIdentifierInfos.back().second);
Douglas Gregorcb15f082013-02-19 18:26:28 +00008325 PendingIdentifierInfos.pop_back();
Douglas Gregor6168bd22013-02-18 15:53:43 +00008326
8327 SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
Guy Benyei11169dd2012-12-18 14:30:41 +00008328 }
Richard Smithf0ae3c2d2014-03-28 17:31:23 +00008329
Richard Smith851072e2014-05-19 20:59:20 +00008330 // For each decl chain that we wanted to complete while deserializing, mark
8331 // it as "still needs to be completed".
8332 for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
8333 markIncompleteDeclChain(PendingIncompleteDeclChains[I]);
8334 }
8335 PendingIncompleteDeclChains.clear();
8336
Guy Benyei11169dd2012-12-18 14:30:41 +00008337 // Load pending declaration chains.
Richard Smithd8a83712015-08-22 01:47:18 +00008338 for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
Richard Smithd61d4ac2015-08-22 20:13:39 +00008339 loadPendingDeclChain(PendingDeclChains[I].first, PendingDeclChains[I].second);
Guy Benyei11169dd2012-12-18 14:30:41 +00008340 PendingDeclChains.clear();
8341
Douglas Gregor6168bd22013-02-18 15:53:43 +00008342 // Make the most recent of the top-level declarations visible.
Craig Topper79be4cd2013-07-05 04:33:53 +00008343 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
8344 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
Douglas Gregor6168bd22013-02-18 15:53:43 +00008345 IdentifierInfo *II = TLD->first;
8346 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +00008347 pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
Douglas Gregor6168bd22013-02-18 15:53:43 +00008348 }
8349 }
8350
Guy Benyei11169dd2012-12-18 14:30:41 +00008351 // Load any pending macro definitions.
8352 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00008353 IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
8354 SmallVector<PendingMacroInfo, 2> GlobalIDs;
8355 GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
8356 // Initialize the macro history from chained-PCHs ahead of module imports.
Richard Smith49f906a2014-03-01 00:08:04 +00008357 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00008358 ++IDIdx) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00008359 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
Richard Smithe842a472014-10-22 02:05:46 +00008360 if (Info.M->Kind != MK_ImplicitModule &&
8361 Info.M->Kind != MK_ExplicitModule)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00008362 resolvePendingMacro(II, Info);
8363 }
8364 // Handle module imports.
Richard Smith49f906a2014-03-01 00:08:04 +00008365 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00008366 ++IDIdx) {
8367 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
Richard Smithe842a472014-10-22 02:05:46 +00008368 if (Info.M->Kind == MK_ImplicitModule ||
8369 Info.M->Kind == MK_ExplicitModule)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00008370 resolvePendingMacro(II, Info);
Guy Benyei11169dd2012-12-18 14:30:41 +00008371 }
8372 }
8373 PendingMacroIDs.clear();
Argyrios Kyrtzidis83a6e3b2013-02-16 00:48:59 +00008374
8375 // Wire up the DeclContexts for Decls that we delayed setting until
8376 // recursive loading is completed.
8377 while (!PendingDeclContextInfos.empty()) {
8378 PendingDeclContextInfo Info = PendingDeclContextInfos.front();
8379 PendingDeclContextInfos.pop_front();
8380 DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
8381 DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
8382 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
8383 }
Richard Smith2b9e3e32013-10-18 06:05:18 +00008384
Richard Smithd1c46742014-04-30 02:24:17 +00008385 // Perform any pending declaration updates.
Richard Smithd6db68c2014-08-07 20:58:41 +00008386 while (!PendingUpdateRecords.empty()) {
Richard Smithd1c46742014-04-30 02:24:17 +00008387 auto Update = PendingUpdateRecords.pop_back_val();
8388 ReadingKindTracker ReadingKind(Read_Decl, *this);
8389 loadDeclUpdateRecords(Update.first, Update.second);
8390 }
Guy Benyei11169dd2012-12-18 14:30:41 +00008391 }
Richard Smith8a639892015-01-24 01:07:20 +00008392
8393 // At this point, all update records for loaded decls are in place, so any
8394 // fake class definitions should have become real.
8395 assert(PendingFakeDefinitionData.empty() &&
8396 "faked up a class definition but never saw the real one");
8397
Guy Benyei11169dd2012-12-18 14:30:41 +00008398 // If we deserialized any C++ or Objective-C class definitions, any
8399 // Objective-C protocol definitions, or any redeclarable templates, make sure
8400 // that all redeclarations point to the definitions. Note that this can only
8401 // happen now, after the redeclaration chains have been fully wired.
Craig Topperc6914d02014-08-25 04:15:02 +00008402 for (Decl *D : PendingDefinitions) {
8403 if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
Richard Smith5b21db82014-04-23 18:20:42 +00008404 if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008405 // Make sure that the TagType points at the definition.
8406 const_cast<TagType*>(TagT)->decl = TD;
8407 }
Richard Smith8ce51082015-03-11 01:44:51 +00008408
Craig Topperc6914d02014-08-25 04:15:02 +00008409 if (auto RD = dyn_cast<CXXRecordDecl>(D)) {
Richard Smith8ce51082015-03-11 01:44:51 +00008410 for (auto *R = getMostRecentExistingDecl(RD); R;
8411 R = R->getPreviousDecl()) {
8412 assert((R == D) ==
8413 cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
Richard Smith2c381642014-08-27 23:11:59 +00008414 "declaration thinks it's the definition but it isn't");
Aaron Ballman86c93902014-03-06 23:45:36 +00008415 cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
Richard Smith2c381642014-08-27 23:11:59 +00008416 }
Guy Benyei11169dd2012-12-18 14:30:41 +00008417 }
8418
8419 continue;
8420 }
Richard Smith8ce51082015-03-11 01:44:51 +00008421
Craig Topperc6914d02014-08-25 04:15:02 +00008422 if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008423 // Make sure that the ObjCInterfaceType points at the definition.
8424 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
8425 ->Decl = ID;
Richard Smith8ce51082015-03-11 01:44:51 +00008426
8427 for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl())
8428 cast<ObjCInterfaceDecl>(R)->Data = ID->Data;
8429
Guy Benyei11169dd2012-12-18 14:30:41 +00008430 continue;
8431 }
Richard Smith8ce51082015-03-11 01:44:51 +00008432
Craig Topperc6914d02014-08-25 04:15:02 +00008433 if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) {
Richard Smith8ce51082015-03-11 01:44:51 +00008434 for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl())
8435 cast<ObjCProtocolDecl>(R)->Data = PD->Data;
8436
Guy Benyei11169dd2012-12-18 14:30:41 +00008437 continue;
8438 }
Richard Smith8ce51082015-03-11 01:44:51 +00008439
Craig Topperc6914d02014-08-25 04:15:02 +00008440 auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl();
Richard Smith8ce51082015-03-11 01:44:51 +00008441 for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl())
8442 cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common;
Guy Benyei11169dd2012-12-18 14:30:41 +00008443 }
8444 PendingDefinitions.clear();
8445
8446 // Load the bodies of any functions or methods we've encountered. We do
8447 // this now (delayed) so that we can be sure that the declaration chains
Richard Smithb9fa9962015-08-21 03:04:33 +00008448 // have been fully wired up (hasBody relies on this).
8449 // FIXME: We shouldn't require complete redeclaration chains here.
Guy Benyei11169dd2012-12-18 14:30:41 +00008450 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
8451 PBEnd = PendingBodies.end();
8452 PB != PBEnd; ++PB) {
8453 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
8454 // FIXME: Check for =delete/=default?
8455 // FIXME: Complain about ODR violations here?
8456 if (!getContext().getLangOpts().Modules || !FD->hasBody())
8457 FD->setLazyBody(PB->second);
8458 continue;
8459 }
8460
8461 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
8462 if (!getContext().getLangOpts().Modules || !MD->hasBody())
8463 MD->setLazyBody(PB->second);
8464 }
8465 PendingBodies.clear();
Richard Smith42413142015-05-15 20:05:43 +00008466
8467 // Do some cleanup.
8468 for (auto *ND : PendingMergedDefinitionsToDeduplicate)
8469 getContext().deduplicateMergedDefinitonsFor(ND);
8470 PendingMergedDefinitionsToDeduplicate.clear();
Richard Smitha0ce9c42014-07-29 23:23:27 +00008471}
8472
8473void ASTReader::diagnoseOdrViolations() {
Richard Smithbb853c72014-08-13 01:23:33 +00008474 if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty())
8475 return;
8476
Richard Smitha0ce9c42014-07-29 23:23:27 +00008477 // Trigger the import of the full definition of each class that had any
8478 // odr-merging problems, so we can produce better diagnostics for them.
Richard Smithbb853c72014-08-13 01:23:33 +00008479 // These updates may in turn find and diagnose some ODR failures, so take
8480 // ownership of the set first.
8481 auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
8482 PendingOdrMergeFailures.clear();
8483 for (auto &Merge : OdrMergeFailures) {
Richard Smitha0ce9c42014-07-29 23:23:27 +00008484 Merge.first->buildLookup();
8485 Merge.first->decls_begin();
8486 Merge.first->bases_begin();
8487 Merge.first->vbases_begin();
8488 for (auto *RD : Merge.second) {
8489 RD->decls_begin();
8490 RD->bases_begin();
8491 RD->vbases_begin();
8492 }
8493 }
8494
8495 // For each declaration from a merged context, check that the canonical
8496 // definition of that context also contains a declaration of the same
8497 // entity.
8498 //
8499 // Caution: this loop does things that might invalidate iterators into
8500 // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
8501 while (!PendingOdrMergeChecks.empty()) {
8502 NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
8503
8504 // FIXME: Skip over implicit declarations for now. This matters for things
8505 // like implicitly-declared special member functions. This isn't entirely
8506 // correct; we can end up with multiple unmerged declarations of the same
8507 // implicit entity.
8508 if (D->isImplicit())
8509 continue;
8510
8511 DeclContext *CanonDef = D->getDeclContext();
Richard Smitha0ce9c42014-07-29 23:23:27 +00008512
8513 bool Found = false;
8514 const Decl *DCanon = D->getCanonicalDecl();
8515
Richard Smith01bdb7a2014-08-28 05:44:07 +00008516 for (auto RI : D->redecls()) {
8517 if (RI->getLexicalDeclContext() == CanonDef) {
8518 Found = true;
8519 break;
8520 }
8521 }
8522 if (Found)
8523 continue;
8524
Richard Smith0f4e2c42015-08-06 04:23:48 +00008525 // Quick check failed, time to do the slow thing. Note, we can't just
8526 // look up the name of D in CanonDef here, because the member that is
8527 // in CanonDef might not be found by name lookup (it might have been
8528 // replaced by a more recent declaration in the lookup table), and we
8529 // can't necessarily find it in the redeclaration chain because it might
8530 // be merely mergeable, not redeclarable.
Richard Smitha0ce9c42014-07-29 23:23:27 +00008531 llvm::SmallVector<const NamedDecl*, 4> Candidates;
Richard Smith0f4e2c42015-08-06 04:23:48 +00008532 for (auto *CanonMember : CanonDef->decls()) {
8533 if (CanonMember->getCanonicalDecl() == DCanon) {
8534 // This can happen if the declaration is merely mergeable and not
8535 // actually redeclarable (we looked for redeclarations earlier).
8536 //
8537 // FIXME: We should be able to detect this more efficiently, without
8538 // pulling in all of the members of CanonDef.
8539 Found = true;
8540 break;
Richard Smitha0ce9c42014-07-29 23:23:27 +00008541 }
Richard Smith0f4e2c42015-08-06 04:23:48 +00008542 if (auto *ND = dyn_cast<NamedDecl>(CanonMember))
8543 if (ND->getDeclName() == D->getDeclName())
8544 Candidates.push_back(ND);
Richard Smitha0ce9c42014-07-29 23:23:27 +00008545 }
8546
8547 if (!Found) {
Richard Smithd08aeb62014-08-28 01:33:39 +00008548 // The AST doesn't like TagDecls becoming invalid after they've been
8549 // completed. We only really need to mark FieldDecls as invalid here.
8550 if (!isa<TagDecl>(D))
8551 D->setInvalidDecl();
Richard Smith4ab3dbd2015-02-13 22:43:51 +00008552
8553 // Ensure we don't accidentally recursively enter deserialization while
8554 // we're producing our diagnostic.
8555 Deserializing RecursionGuard(this);
Richard Smitha0ce9c42014-07-29 23:23:27 +00008556
8557 std::string CanonDefModule =
8558 getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef));
8559 Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
8560 << D << getOwningModuleNameForDiagnostic(D)
8561 << CanonDef << CanonDefModule.empty() << CanonDefModule;
8562
8563 if (Candidates.empty())
8564 Diag(cast<Decl>(CanonDef)->getLocation(),
8565 diag::note_module_odr_violation_no_possible_decls) << D;
8566 else {
8567 for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
8568 Diag(Candidates[I]->getLocation(),
8569 diag::note_module_odr_violation_possible_decl)
8570 << Candidates[I];
8571 }
8572
8573 DiagnosedOdrMergeFailures.insert(CanonDef);
8574 }
8575 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00008576
Richard Smith4ab3dbd2015-02-13 22:43:51 +00008577 if (OdrMergeFailures.empty())
8578 return;
8579
8580 // Ensure we don't accidentally recursively enter deserialization while
8581 // we're producing our diagnostics.
8582 Deserializing RecursionGuard(this);
8583
Richard Smithcd45dbc2014-04-19 03:48:30 +00008584 // Issue any pending ODR-failure diagnostics.
Richard Smithbb853c72014-08-13 01:23:33 +00008585 for (auto &Merge : OdrMergeFailures) {
Richard Smitha0ce9c42014-07-29 23:23:27 +00008586 // If we've already pointed out a specific problem with this class, don't
8587 // bother issuing a general "something's different" diagnostic.
David Blaikie82e95a32014-11-19 07:49:47 +00008588 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
Richard Smithcd45dbc2014-04-19 03:48:30 +00008589 continue;
8590
8591 bool Diagnosed = false;
8592 for (auto *RD : Merge.second) {
8593 // Multiple different declarations got merged together; tell the user
8594 // where they came from.
8595 if (Merge.first != RD) {
8596 // FIXME: Walk the definition, figure out what's different,
8597 // and diagnose that.
8598 if (!Diagnosed) {
8599 std::string Module = getOwningModuleNameForDiagnostic(Merge.first);
8600 Diag(Merge.first->getLocation(),
8601 diag::err_module_odr_violation_different_definitions)
8602 << Merge.first << Module.empty() << Module;
8603 Diagnosed = true;
8604 }
8605
8606 Diag(RD->getLocation(),
8607 diag::note_module_odr_violation_different_definitions)
8608 << getOwningModuleNameForDiagnostic(RD);
8609 }
8610 }
8611
8612 if (!Diagnosed) {
8613 // All definitions are updates to the same declaration. This happens if a
8614 // module instantiates the declaration of a class template specialization
8615 // and two or more other modules instantiate its definition.
8616 //
8617 // FIXME: Indicate which modules had instantiations of this definition.
8618 // FIXME: How can this even happen?
8619 Diag(Merge.first->getLocation(),
8620 diag::err_module_odr_violation_different_instantiations)
8621 << Merge.first;
8622 }
8623 }
Guy Benyei11169dd2012-12-18 14:30:41 +00008624}
8625
Richard Smithce18a182015-07-14 00:26:00 +00008626void ASTReader::StartedDeserializing() {
8627 if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get())
8628 ReadTimer->startTimer();
8629}
8630
Guy Benyei11169dd2012-12-18 14:30:41 +00008631void ASTReader::FinishedDeserializing() {
8632 assert(NumCurrentElementsDeserializing &&
8633 "FinishedDeserializing not paired with StartedDeserializing");
8634 if (NumCurrentElementsDeserializing == 1) {
8635 // We decrease NumCurrentElementsDeserializing only after pending actions
8636 // are finished, to avoid recursively re-calling finishPendingActions().
8637 finishPendingActions();
8638 }
8639 --NumCurrentElementsDeserializing;
8640
Richard Smitha0ce9c42014-07-29 23:23:27 +00008641 if (NumCurrentElementsDeserializing == 0) {
Richard Smith9e2341d2015-03-23 03:25:59 +00008642 // Propagate exception specification updates along redeclaration chains.
Richard Smith7226f2a2015-03-23 19:54:56 +00008643 while (!PendingExceptionSpecUpdates.empty()) {
8644 auto Updates = std::move(PendingExceptionSpecUpdates);
8645 PendingExceptionSpecUpdates.clear();
8646 for (auto Update : Updates) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00008647 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
Richard Smith7226f2a2015-03-23 19:54:56 +00008648 auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
Richard Smith1d0f1992015-08-19 21:09:32 +00008649 auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
Richard Smithd88a7f12015-09-01 20:35:42 +00008650 if (auto *Listener = Context.getASTMutationListener())
8651 Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second));
Richard Smith1d0f1992015-08-19 21:09:32 +00008652 for (auto *Redecl : Update.second->redecls())
8653 Context.adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI);
Richard Smith7226f2a2015-03-23 19:54:56 +00008654 }
Richard Smith9e2341d2015-03-23 03:25:59 +00008655 }
8656
Richard Smithce18a182015-07-14 00:26:00 +00008657 if (ReadTimer)
8658 ReadTimer->stopTimer();
8659
Richard Smith0f4e2c42015-08-06 04:23:48 +00008660 diagnoseOdrViolations();
8661
Richard Smith04d05b52014-03-23 00:27:18 +00008662 // We are not in recursive loading, so it's safe to pass the "interesting"
8663 // decls to the consumer.
Richard Smitha0ce9c42014-07-29 23:23:27 +00008664 if (Consumer)
8665 PassInterestingDeclsToConsumer();
Guy Benyei11169dd2012-12-18 14:30:41 +00008666 }
8667}
8668
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +00008669void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
Richard Smith9e2341d2015-03-23 03:25:59 +00008670 if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
8671 // Remove any fake results before adding any real ones.
8672 auto It = PendingFakeLookupResults.find(II);
8673 if (It != PendingFakeLookupResults.end()) {
Richard Smitha534a312015-07-21 23:54:07 +00008674 for (auto *ND : It->second)
Richard Smith9e2341d2015-03-23 03:25:59 +00008675 SemaObj->IdResolver.RemoveDecl(ND);
Ben Langmuireb8bd2d2015-04-10 22:25:42 +00008676 // FIXME: this works around module+PCH performance issue.
8677 // Rather than erase the result from the map, which is O(n), just clear
8678 // the vector of NamedDecls.
8679 It->second.clear();
Richard Smith9e2341d2015-03-23 03:25:59 +00008680 }
8681 }
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +00008682
8683 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
8684 SemaObj->TUScope->AddDecl(D);
8685 } else if (SemaObj->TUScope) {
8686 // Adding the decl to IdResolver may have failed because it was already in
8687 // (even though it was not added in scope). If it is already in, make sure
8688 // it gets in the scope as well.
8689 if (std::find(SemaObj->IdResolver.begin(Name),
8690 SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
8691 SemaObj->TUScope->AddDecl(D);
8692 }
8693}
8694
Douglas Gregor6623e1f2015-11-03 18:33:07 +00008695ASTReader::ASTReader(
8696 Preprocessor &PP, ASTContext &Context,
8697 const PCHContainerReader &PCHContainerRdr,
8698 ArrayRef<IntrusiveRefCntPtr<ModuleFileExtension>> Extensions,
8699 StringRef isysroot, bool DisableValidation,
8700 bool AllowASTWithCompilerErrors,
8701 bool AllowConfigurationMismatch, bool ValidateSystemInputs,
8702 bool UseGlobalIndex,
8703 std::unique_ptr<llvm::Timer> ReadTimer)
Craig Toppera13603a2014-05-22 05:54:18 +00008704 : Listener(new PCHValidator(PP, *this)), DeserializationListener(nullptr),
Nico Weber824285e2014-05-08 04:26:47 +00008705 OwnsDeserializationListener(false), SourceMgr(PP.getSourceManager()),
Adrian Prantlfb2398d2015-07-17 01:19:54 +00008706 FileMgr(PP.getFileManager()), PCHContainerRdr(PCHContainerRdr),
Adrian Prantlbb165fb2015-06-20 18:53:08 +00008707 Diags(PP.getDiagnostics()), SemaObj(nullptr), PP(PP), Context(Context),
Adrian Prantlfb2398d2015-07-17 01:19:54 +00008708 Consumer(nullptr), ModuleMgr(PP.getFileManager(), PCHContainerRdr),
Richard Smith10379092016-05-06 23:14:07 +00008709 DummyIdResolver(PP),
Richard Smithce18a182015-07-14 00:26:00 +00008710 ReadTimer(std::move(ReadTimer)),
Nico Weber779355f2016-03-02 23:22:00 +00008711 PragmaMSStructState(-1),
Nico Weber42932312016-03-03 00:17:35 +00008712 PragmaMSPointersToMembersState(-1),
Adrian Prantlbb165fb2015-06-20 18:53:08 +00008713 isysroot(isysroot), DisableValidation(DisableValidation),
Nico Weber824285e2014-05-08 04:26:47 +00008714 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
8715 AllowConfigurationMismatch(AllowConfigurationMismatch),
8716 ValidateSystemInputs(ValidateSystemInputs),
8717 UseGlobalIndex(UseGlobalIndex), TriedLoadingGlobalIndex(false),
Vassil Vassilev19765fb2016-07-22 21:08:24 +00008718 ProcessingUpdateRecords(false),
Adrian Prantlbb165fb2015-06-20 18:53:08 +00008719 CurrSwitchCaseStmts(&SwitchCaseStmts), NumSLocEntriesRead(0),
8720 TotalNumSLocEntries(0), NumStatementsRead(0), TotalNumStatements(0),
8721 NumMacrosRead(0), TotalNumMacros(0), NumIdentifierLookups(0),
8722 NumIdentifierLookupHits(0), NumSelectorsRead(0),
Nico Weber824285e2014-05-08 04:26:47 +00008723 NumMethodPoolEntriesRead(0), NumMethodPoolLookups(0),
8724 NumMethodPoolHits(0), NumMethodPoolTableLookups(0),
8725 NumMethodPoolTableHits(0), TotalNumMethodPoolEntries(0),
8726 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
8727 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
8728 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
Richard Smithc2bb8182015-03-24 06:36:48 +00008729 PassingDeclsToConsumer(false), ReadingKind(Read_None) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008730 SourceMgr.setExternalSLocEntrySource(this);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00008731
8732 for (const auto &Ext : Extensions) {
8733 auto BlockName = Ext->getExtensionMetadata().BlockName;
8734 auto Known = ModuleFileExtensions.find(BlockName);
8735 if (Known != ModuleFileExtensions.end()) {
8736 Diags.Report(diag::warn_duplicate_module_file_extension)
8737 << BlockName;
8738 continue;
8739 }
8740
8741 ModuleFileExtensions.insert({BlockName, Ext});
8742 }
Guy Benyei11169dd2012-12-18 14:30:41 +00008743}
8744
8745ASTReader::~ASTReader() {
Nico Weber824285e2014-05-08 04:26:47 +00008746 if (OwnsDeserializationListener)
8747 delete DeserializationListener;
Guy Benyei11169dd2012-12-18 14:30:41 +00008748}
Richard Smith10379092016-05-06 23:14:07 +00008749
8750IdentifierResolver &ASTReader::getIdResolver() {
8751 return SemaObj ? SemaObj->IdResolver : DummyIdResolver;
8752}