blob: 14d22f6efd91a90c185ec1cf16e9fd2f30f265ea [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]);
Manman Ren11f2a472016-08-18 17:42:15 +00001315 if (IncludeLoc.isInvalid() && F->isModule()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001316 IncludeLoc = getImportLocation(F);
1317 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001318
Richard Smithaada85c2016-02-06 02:06:43 +00001319 auto Buffer = ReadBuffer(SLocEntryCursor, Name);
1320 if (!Buffer)
Guy Benyei11169dd2012-12-18 14:30:41 +00001321 return true;
David Blaikie50a5f972014-08-29 07:59:55 +00001322 SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID,
Rafael Espindolad87f8d72014-08-27 20:03:29 +00001323 BaseOffset + Offset, IncludeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001324 break;
1325 }
1326
1327 case SM_SLOC_EXPANSION_ENTRY: {
1328 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1329 SourceMgr.createExpansionLoc(SpellingLoc,
1330 ReadSourceLocation(*F, Record[2]),
1331 ReadSourceLocation(*F, Record[3]),
1332 Record[4],
1333 ID,
1334 BaseOffset + Record[0]);
1335 break;
1336 }
1337 }
1338
1339 return false;
1340}
1341
1342std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1343 if (ID == 0)
1344 return std::make_pair(SourceLocation(), "");
1345
1346 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1347 Error("source location entry ID out-of-range for AST file");
1348 return std::make_pair(SourceLocation(), "");
1349 }
1350
1351 // Find which module file this entry lands in.
1352 ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
Manman Ren11f2a472016-08-18 17:42:15 +00001353 if (!M->isModule())
Guy Benyei11169dd2012-12-18 14:30:41 +00001354 return std::make_pair(SourceLocation(), "");
1355
1356 // FIXME: Can we map this down to a particular submodule? That would be
1357 // ideal.
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001358 return std::make_pair(M->ImportLoc, StringRef(M->ModuleName));
Guy Benyei11169dd2012-12-18 14:30:41 +00001359}
1360
1361/// \brief Find the location where the module F is imported.
1362SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1363 if (F->ImportLoc.isValid())
1364 return F->ImportLoc;
1365
1366 // Otherwise we have a PCH. It's considered to be "imported" at the first
1367 // location of its includer.
1368 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001369 // Main file is the importer.
Yaron Keren8b563662015-10-03 10:46:20 +00001370 assert(SourceMgr.getMainFileID().isValid() && "missing main file");
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001371 return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
Guy Benyei11169dd2012-12-18 14:30:41 +00001372 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001373 return F->ImportedBy[0]->FirstLoc;
1374}
1375
1376/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1377/// specified cursor. Read the abbreviations that are at the top of the block
1378/// and then leave the cursor pointing into the block.
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001379bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID) {
Richard Smith0516b182015-09-08 19:40:14 +00001380 if (Cursor.EnterSubBlock(BlockID))
1381 return true;
Guy Benyei11169dd2012-12-18 14:30:41 +00001382
1383 while (true) {
1384 uint64_t Offset = Cursor.GetCurrentBitNo();
1385 unsigned Code = Cursor.ReadCode();
1386
1387 // We expect all abbrevs to be at the start of the block.
1388 if (Code != llvm::bitc::DEFINE_ABBREV) {
1389 Cursor.JumpToBit(Offset);
1390 return false;
1391 }
1392 Cursor.ReadAbbrevRecord();
1393 }
1394}
1395
Richard Smithe40f2ba2013-08-07 21:41:30 +00001396Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record,
John McCallf413f5e2013-05-03 00:10:13 +00001397 unsigned &Idx) {
1398 Token Tok;
1399 Tok.startToken();
1400 Tok.setLocation(ReadSourceLocation(F, Record, Idx));
1401 Tok.setLength(Record[Idx++]);
1402 if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
1403 Tok.setIdentifierInfo(II);
1404 Tok.setKind((tok::TokenKind)Record[Idx++]);
1405 Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1406 return Tok;
1407}
1408
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001409MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001410 BitstreamCursor &Stream = F.MacroCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001411
1412 // Keep track of where we are in the stream, then jump back there
1413 // after reading this macro.
1414 SavedStreamPosition SavedPosition(Stream);
1415
1416 Stream.JumpToBit(Offset);
1417 RecordData Record;
1418 SmallVector<IdentifierInfo*, 16> MacroArgs;
Craig Toppera13603a2014-05-22 05:54:18 +00001419 MacroInfo *Macro = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001420
Guy Benyei11169dd2012-12-18 14:30:41 +00001421 while (true) {
Chris Lattnerefa77172013-01-20 00:00:22 +00001422 // Advance to the next record, but if we get to the end of the block, don't
1423 // pop it (removing all the abbreviations from the cursor) since we want to
1424 // be able to reseek within the block and read entries.
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001425 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
Chris Lattnerefa77172013-01-20 00:00:22 +00001426 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks(Flags);
1427
1428 switch (Entry.Kind) {
1429 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1430 case llvm::BitstreamEntry::Error:
1431 Error("malformed block record in AST file");
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001432 return Macro;
Chris Lattnerefa77172013-01-20 00:00:22 +00001433 case llvm::BitstreamEntry::EndBlock:
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001434 return Macro;
Chris Lattnerefa77172013-01-20 00:00:22 +00001435 case llvm::BitstreamEntry::Record:
1436 // The interesting case.
1437 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001438 }
1439
1440 // Read a record.
Guy Benyei11169dd2012-12-18 14:30:41 +00001441 Record.clear();
1442 PreprocessorRecordTypes RecType =
Chris Lattner0e6c9402013-01-20 02:38:54 +00001443 (PreprocessorRecordTypes)Stream.readRecord(Entry.ID, Record);
Guy Benyei11169dd2012-12-18 14:30:41 +00001444 switch (RecType) {
Richard Smithd7329392015-04-21 21:46:32 +00001445 case PP_MODULE_MACRO:
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001446 case PP_MACRO_DIRECTIVE_HISTORY:
1447 return Macro;
1448
Guy Benyei11169dd2012-12-18 14:30:41 +00001449 case PP_MACRO_OBJECT_LIKE:
1450 case PP_MACRO_FUNCTION_LIKE: {
1451 // If we already have a macro, that means that we've hit the end
1452 // of the definition of the macro we were looking for. We're
1453 // done.
1454 if (Macro)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001455 return Macro;
Guy Benyei11169dd2012-12-18 14:30:41 +00001456
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001457 unsigned NextIndex = 1; // Skip identifier ID.
1458 SubmoduleID SubModID = getGlobalSubmoduleID(F, Record[NextIndex++]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001459 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001460 MacroInfo *MI = PP.AllocateDeserializedMacroInfo(Loc, SubModID);
Argyrios Kyrtzidis7572be22013-01-07 19:16:23 +00001461 MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
Guy Benyei11169dd2012-12-18 14:30:41 +00001462 MI->setIsUsed(Record[NextIndex++]);
Argyrios Kyrtzidis9ef53ce2014-04-09 18:21:23 +00001463 MI->setUsedForHeaderGuard(Record[NextIndex++]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001464
Guy Benyei11169dd2012-12-18 14:30:41 +00001465 if (RecType == PP_MACRO_FUNCTION_LIKE) {
1466 // Decode function-like macro info.
1467 bool isC99VarArgs = Record[NextIndex++];
1468 bool isGNUVarArgs = Record[NextIndex++];
1469 bool hasCommaPasting = Record[NextIndex++];
1470 MacroArgs.clear();
1471 unsigned NumArgs = Record[NextIndex++];
1472 for (unsigned i = 0; i != NumArgs; ++i)
1473 MacroArgs.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1474
1475 // Install function-like macro info.
1476 MI->setIsFunctionLike();
1477 if (isC99VarArgs) MI->setIsC99Varargs();
1478 if (isGNUVarArgs) MI->setIsGNUVarargs();
1479 if (hasCommaPasting) MI->setHasCommaPasting();
Craig Topperd96b3f92015-10-22 04:59:52 +00001480 MI->setArgumentList(MacroArgs, PP.getPreprocessorAllocator());
Guy Benyei11169dd2012-12-18 14:30:41 +00001481 }
1482
Guy Benyei11169dd2012-12-18 14:30:41 +00001483 // Remember that we saw this macro last so that we add the tokens that
1484 // form its body to it.
1485 Macro = MI;
1486
1487 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1488 Record[NextIndex]) {
1489 // We have a macro definition. Register the association
1490 PreprocessedEntityID
1491 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1492 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
Richard Smith66a81862015-05-04 02:25:31 +00001493 PreprocessingRecord::PPEntityID PPID =
1494 PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true);
1495 MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
1496 PPRec.getPreprocessedEntity(PPID));
Argyrios Kyrtzidis832de9f2013-02-22 18:35:59 +00001497 if (PPDef)
1498 PPRec.RegisterMacroDefinition(Macro, PPDef);
Guy Benyei11169dd2012-12-18 14:30:41 +00001499 }
1500
1501 ++NumMacrosRead;
1502 break;
1503 }
1504
1505 case PP_TOKEN: {
1506 // If we see a TOKEN before a PP_MACRO_*, then the file is
1507 // erroneous, just pretend we didn't see this.
Craig Toppera13603a2014-05-22 05:54:18 +00001508 if (!Macro) break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001509
John McCallf413f5e2013-05-03 00:10:13 +00001510 unsigned Idx = 0;
1511 Token Tok = ReadToken(F, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00001512 Macro->AddTokenToBody(Tok);
1513 break;
1514 }
1515 }
1516 }
1517}
1518
1519PreprocessedEntityID
1520ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const {
1521 ContinuousRangeMap<uint32_t, int, 2>::const_iterator
1522 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1523 assert(I != M.PreprocessedEntityRemap.end()
1524 && "Invalid index into preprocessed entity index remap");
1525
1526 return LocalID + I->second;
1527}
1528
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001529unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1530 return llvm::hash_combine(ikey.Size, ikey.ModTime);
Guy Benyei11169dd2012-12-18 14:30:41 +00001531}
Richard Smith7ed1bc92014-12-05 22:42:13 +00001532
Guy Benyei11169dd2012-12-18 14:30:41 +00001533HeaderFileInfoTrait::internal_key_type
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001534HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
Richard Smithe75ee0f2015-08-17 07:13:32 +00001535 internal_key_type ikey = {FE->getSize(),
1536 M.HasTimestamps ? FE->getModificationTime() : 0,
1537 FE->getName(), /*Imported*/ false};
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001538 return ikey;
1539}
Guy Benyei11169dd2012-12-18 14:30:41 +00001540
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001541bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
Richard Smithe75ee0f2015-08-17 07:13:32 +00001542 if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime))
Guy Benyei11169dd2012-12-18 14:30:41 +00001543 return false;
1544
Richard Smith7ed1bc92014-12-05 22:42:13 +00001545 if (llvm::sys::path::is_absolute(a.Filename) &&
1546 strcmp(a.Filename, b.Filename) == 0)
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001547 return true;
1548
Guy Benyei11169dd2012-12-18 14:30:41 +00001549 // Determine whether the actual files are equivalent.
Argyrios Kyrtzidis2a513e82013-03-04 20:33:40 +00001550 FileManager &FileMgr = Reader.getFileManager();
Richard Smith7ed1bc92014-12-05 22:42:13 +00001551 auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* {
1552 if (!Key.Imported)
1553 return FileMgr.getFile(Key.Filename);
1554
1555 std::string Resolved = Key.Filename;
1556 Reader.ResolveImportedPath(M, Resolved);
1557 return FileMgr.getFile(Resolved);
1558 };
1559
1560 const FileEntry *FEA = GetFile(a);
1561 const FileEntry *FEB = GetFile(b);
1562 return FEA && FEA == FEB;
Guy Benyei11169dd2012-12-18 14:30:41 +00001563}
1564
1565std::pair<unsigned, unsigned>
1566HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
Justin Bogner57ba0b22014-03-28 22:03:24 +00001567 using namespace llvm::support;
1568 unsigned KeyLen = (unsigned) endian::readNext<uint16_t, little, unaligned>(d);
Guy Benyei11169dd2012-12-18 14:30:41 +00001569 unsigned DataLen = (unsigned) *d++;
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001570 return std::make_pair(KeyLen, DataLen);
Guy Benyei11169dd2012-12-18 14:30:41 +00001571}
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001572
1573HeaderFileInfoTrait::internal_key_type
1574HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
Justin Bogner57ba0b22014-03-28 22:03:24 +00001575 using namespace llvm::support;
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001576 internal_key_type ikey;
Justin Bogner57ba0b22014-03-28 22:03:24 +00001577 ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d));
1578 ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d));
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001579 ikey.Filename = (const char *)d;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001580 ikey.Imported = true;
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001581 return ikey;
1582}
1583
Guy Benyei11169dd2012-12-18 14:30:41 +00001584HeaderFileInfoTrait::data_type
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001585HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
Guy Benyei11169dd2012-12-18 14:30:41 +00001586 unsigned DataLen) {
1587 const unsigned char *End = d + DataLen;
Justin Bogner57ba0b22014-03-28 22:03:24 +00001588 using namespace llvm::support;
Guy Benyei11169dd2012-12-18 14:30:41 +00001589 HeaderFileInfo HFI;
1590 unsigned Flags = *d++;
Richard Smith386bb072015-08-18 23:42:23 +00001591 // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp.
1592 HFI.isImport |= (Flags >> 4) & 0x01;
1593 HFI.isPragmaOnce |= (Flags >> 3) & 0x01;
1594 HFI.DirInfo = (Flags >> 1) & 0x03;
Guy Benyei11169dd2012-12-18 14:30:41 +00001595 HFI.IndexHeaderMapHeader = Flags & 0x01;
Richard Smith386bb072015-08-18 23:42:23 +00001596 // FIXME: Find a better way to handle this. Maybe just store a
1597 // "has been included" flag?
1598 HFI.NumIncludes = std::max(endian::readNext<uint16_t, little, unaligned>(d),
1599 HFI.NumIncludes);
Justin Bogner57ba0b22014-03-28 22:03:24 +00001600 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(
1601 M, endian::readNext<uint32_t, little, unaligned>(d));
1602 if (unsigned FrameworkOffset =
1603 endian::readNext<uint32_t, little, unaligned>(d)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001604 // The framework offset is 1 greater than the actual offset,
1605 // since 0 is used as an indicator for "no framework name".
1606 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1607 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1608 }
Richard Smith386bb072015-08-18 23:42:23 +00001609
1610 assert((End - d) % 4 == 0 &&
1611 "Wrong data length in HeaderFileInfo deserialization");
1612 while (d != End) {
Justin Bogner57ba0b22014-03-28 22:03:24 +00001613 uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d);
Richard Smith386bb072015-08-18 23:42:23 +00001614 auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 3);
1615 LocalSMID >>= 2;
1616
1617 // This header is part of a module. Associate it with the module to enable
1618 // implicit module import.
1619 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1620 Module *Mod = Reader.getSubmodule(GlobalSMID);
1621 FileManager &FileMgr = Reader.getFileManager();
1622 ModuleMap &ModMap =
1623 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
1624
1625 std::string Filename = key.Filename;
1626 if (key.Imported)
1627 Reader.ResolveImportedPath(M, Filename);
1628 // FIXME: This is not always the right filename-as-written, but we're not
1629 // going to use this information to rebuild the module, so it doesn't make
1630 // a lot of difference.
1631 Module::Header H = { key.Filename, FileMgr.getFile(Filename) };
Richard Smithd8879c82015-08-24 21:59:32 +00001632 ModMap.addHeader(Mod, H, HeaderRole, /*Imported*/true);
1633 HFI.isModuleHeader |= !(HeaderRole & ModuleMap::TextualHeader);
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001634 }
1635
Guy Benyei11169dd2012-12-18 14:30:41 +00001636 // This HeaderFileInfo was externally loaded.
1637 HFI.External = true;
Richard Smithd8879c82015-08-24 21:59:32 +00001638 HFI.IsValid = true;
Guy Benyei11169dd2012-12-18 14:30:41 +00001639 return HFI;
1640}
1641
Richard Smithd7329392015-04-21 21:46:32 +00001642void ASTReader::addPendingMacro(IdentifierInfo *II,
1643 ModuleFile *M,
1644 uint64_t MacroDirectivesOffset) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001645 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1646 PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00001647}
1648
1649void ASTReader::ReadDefinedMacros() {
1650 // Note that we are loading defined macros.
1651 Deserializing Macros(this);
1652
Pete Cooper57d3f142015-07-30 17:22:52 +00001653 for (auto &I : llvm::reverse(ModuleMgr)) {
1654 BitstreamCursor &MacroCursor = I->MacroCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001655
1656 // If there was no preprocessor block, skip this file.
1657 if (!MacroCursor.getBitStreamReader())
1658 continue;
1659
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001660 BitstreamCursor Cursor = MacroCursor;
Pete Cooper57d3f142015-07-30 17:22:52 +00001661 Cursor.JumpToBit(I->MacroStartOffset);
Guy Benyei11169dd2012-12-18 14:30:41 +00001662
1663 RecordData Record;
1664 while (true) {
Chris Lattnere7b154b2013-01-19 21:39:22 +00001665 llvm::BitstreamEntry E = Cursor.advanceSkippingSubblocks();
1666
1667 switch (E.Kind) {
1668 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1669 case llvm::BitstreamEntry::Error:
1670 Error("malformed block record in AST file");
1671 return;
1672 case llvm::BitstreamEntry::EndBlock:
1673 goto NextCursor;
1674
1675 case llvm::BitstreamEntry::Record:
Chris Lattnere7b154b2013-01-19 21:39:22 +00001676 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00001677 switch (Cursor.readRecord(E.ID, Record)) {
Chris Lattnere7b154b2013-01-19 21:39:22 +00001678 default: // Default behavior: ignore.
1679 break;
1680
1681 case PP_MACRO_OBJECT_LIKE:
Sean Callananf3682a72016-05-14 06:24:14 +00001682 case PP_MACRO_FUNCTION_LIKE: {
1683 IdentifierInfo *II = getLocalIdentifier(*I, Record[0]);
1684 if (II->isOutOfDate())
1685 updateOutOfDateIdentifier(*II);
Chris Lattnere7b154b2013-01-19 21:39:22 +00001686 break;
Sean Callananf3682a72016-05-14 06:24:14 +00001687 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00001688
1689 case PP_TOKEN:
1690 // Ignore tokens.
1691 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001692 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001693 break;
1694 }
1695 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00001696 NextCursor: ;
Guy Benyei11169dd2012-12-18 14:30:41 +00001697 }
1698}
1699
1700namespace {
1701 /// \brief Visitor class used to look up identifirs in an AST file.
1702 class IdentifierLookupVisitor {
1703 StringRef Name;
Richard Smith3b637412015-07-14 18:42:41 +00001704 unsigned NameHash;
Guy Benyei11169dd2012-12-18 14:30:41 +00001705 unsigned PriorGeneration;
Douglas Gregor00a50f72013-01-25 00:38:33 +00001706 unsigned &NumIdentifierLookups;
1707 unsigned &NumIdentifierLookupHits;
Guy Benyei11169dd2012-12-18 14:30:41 +00001708 IdentifierInfo *Found;
Douglas Gregor00a50f72013-01-25 00:38:33 +00001709
Guy Benyei11169dd2012-12-18 14:30:41 +00001710 public:
Douglas Gregor00a50f72013-01-25 00:38:33 +00001711 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
1712 unsigned &NumIdentifierLookups,
1713 unsigned &NumIdentifierLookupHits)
Richard Smith3b637412015-07-14 18:42:41 +00001714 : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)),
1715 PriorGeneration(PriorGeneration),
Douglas Gregor00a50f72013-01-25 00:38:33 +00001716 NumIdentifierLookups(NumIdentifierLookups),
1717 NumIdentifierLookupHits(NumIdentifierLookupHits),
1718 Found()
1719 {
1720 }
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00001721
1722 bool operator()(ModuleFile &M) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001723 // If we've already searched this module file, skip it now.
Richard Smithbdf2d932015-07-30 03:37:16 +00001724 if (M.Generation <= PriorGeneration)
Guy Benyei11169dd2012-12-18 14:30:41 +00001725 return true;
Douglas Gregore060e572013-01-25 01:03:03 +00001726
Guy Benyei11169dd2012-12-18 14:30:41 +00001727 ASTIdentifierLookupTable *IdTable
1728 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
1729 if (!IdTable)
1730 return false;
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00001731
1732 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M,
Richard Smithbdf2d932015-07-30 03:37:16 +00001733 Found);
1734 ++NumIdentifierLookups;
Richard Smith3b637412015-07-14 18:42:41 +00001735 ASTIdentifierLookupTable::iterator Pos =
Richard Smithbdf2d932015-07-30 03:37:16 +00001736 IdTable->find_hashed(Name, NameHash, &Trait);
Guy Benyei11169dd2012-12-18 14:30:41 +00001737 if (Pos == IdTable->end())
1738 return false;
1739
1740 // Dereferencing the iterator has the effect of building the
1741 // IdentifierInfo node and populating it with the various
1742 // declarations it needs.
Richard Smithbdf2d932015-07-30 03:37:16 +00001743 ++NumIdentifierLookupHits;
1744 Found = *Pos;
Guy Benyei11169dd2012-12-18 14:30:41 +00001745 return true;
1746 }
1747
1748 // \brief Retrieve the identifier info found within the module
1749 // files.
1750 IdentifierInfo *getIdentifierInfo() const { return Found; }
1751 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +00001752}
Guy Benyei11169dd2012-12-18 14:30:41 +00001753
1754void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
1755 // Note that we are loading an identifier.
1756 Deserializing AnIdentifier(this);
1757
1758 unsigned PriorGeneration = 0;
1759 if (getContext().getLangOpts().Modules)
1760 PriorGeneration = IdentifierGeneration[&II];
Douglas Gregore060e572013-01-25 01:03:03 +00001761
1762 // If there is a global index, look there first to determine which modules
1763 // provably do not have any results for this identifier.
Douglas Gregor7211ac12013-01-25 23:32:03 +00001764 GlobalModuleIndex::HitSet Hits;
Craig Toppera13603a2014-05-22 05:54:18 +00001765 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
Douglas Gregore060e572013-01-25 01:03:03 +00001766 if (!loadGlobalIndex()) {
Douglas Gregor7211ac12013-01-25 23:32:03 +00001767 if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
1768 HitsPtr = &Hits;
Douglas Gregore060e572013-01-25 01:03:03 +00001769 }
1770 }
1771
Douglas Gregor7211ac12013-01-25 23:32:03 +00001772 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
Douglas Gregor00a50f72013-01-25 00:38:33 +00001773 NumIdentifierLookups,
1774 NumIdentifierLookupHits);
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00001775 ModuleMgr.visit(Visitor, HitsPtr);
Guy Benyei11169dd2012-12-18 14:30:41 +00001776 markIdentifierUpToDate(&II);
1777}
1778
1779void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
1780 if (!II)
1781 return;
1782
1783 II->setOutOfDate(false);
1784
1785 // Update the generation for this identifier.
1786 if (getContext().getLangOpts().Modules)
Richard Smith053f6c62014-05-16 23:01:30 +00001787 IdentifierGeneration[II] = getGeneration();
Guy Benyei11169dd2012-12-18 14:30:41 +00001788}
1789
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001790void ASTReader::resolvePendingMacro(IdentifierInfo *II,
1791 const PendingMacroInfo &PMInfo) {
Richard Smithd7329392015-04-21 21:46:32 +00001792 ModuleFile &M = *PMInfo.M;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001793
1794 BitstreamCursor &Cursor = M.MacroCursor;
1795 SavedStreamPosition SavedPosition(Cursor);
Richard Smithd7329392015-04-21 21:46:32 +00001796 Cursor.JumpToBit(PMInfo.MacroDirectivesOffset);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001797
Richard Smith713369b2015-04-23 20:40:50 +00001798 struct ModuleMacroRecord {
1799 SubmoduleID SubModID;
1800 MacroInfo *MI;
1801 SmallVector<SubmoduleID, 8> Overrides;
1802 };
1803 llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001804
Richard Smithd7329392015-04-21 21:46:32 +00001805 // We expect to see a sequence of PP_MODULE_MACRO records listing exported
1806 // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
1807 // macro histroy.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001808 RecordData Record;
Richard Smithd7329392015-04-21 21:46:32 +00001809 while (true) {
1810 llvm::BitstreamEntry Entry =
1811 Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
1812 if (Entry.Kind != llvm::BitstreamEntry::Record) {
1813 Error("malformed block record in AST file");
1814 return;
1815 }
1816
1817 Record.clear();
Aaron Ballmanc75a1922015-04-22 15:25:05 +00001818 switch ((PreprocessorRecordTypes)Cursor.readRecord(Entry.ID, Record)) {
Richard Smithd7329392015-04-21 21:46:32 +00001819 case PP_MACRO_DIRECTIVE_HISTORY:
1820 break;
1821
1822 case PP_MODULE_MACRO: {
Richard Smith713369b2015-04-23 20:40:50 +00001823 ModuleMacros.push_back(ModuleMacroRecord());
1824 auto &Info = ModuleMacros.back();
Richard Smithe56c8bc2015-04-22 00:26:11 +00001825 Info.SubModID = getGlobalSubmoduleID(M, Record[0]);
1826 Info.MI = getMacro(getGlobalMacroID(M, Record[1]));
Richard Smith713369b2015-04-23 20:40:50 +00001827 for (int I = 2, N = Record.size(); I != N; ++I)
1828 Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I]));
Richard Smithd7329392015-04-21 21:46:32 +00001829 continue;
1830 }
1831
1832 default:
1833 Error("malformed block record in AST file");
1834 return;
1835 }
1836
1837 // We found the macro directive history; that's the last record
1838 // for this macro.
1839 break;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001840 }
1841
Richard Smithd7329392015-04-21 21:46:32 +00001842 // Module macros are listed in reverse dependency order.
Richard Smithe56c8bc2015-04-22 00:26:11 +00001843 {
1844 std::reverse(ModuleMacros.begin(), ModuleMacros.end());
Richard Smithe56c8bc2015-04-22 00:26:11 +00001845 llvm::SmallVector<ModuleMacro*, 8> Overrides;
Richard Smith713369b2015-04-23 20:40:50 +00001846 for (auto &MMR : ModuleMacros) {
Richard Smithe56c8bc2015-04-22 00:26:11 +00001847 Overrides.clear();
Richard Smith713369b2015-04-23 20:40:50 +00001848 for (unsigned ModID : MMR.Overrides) {
Richard Smithb8b2ed62015-04-23 18:18:26 +00001849 Module *Mod = getSubmodule(ModID);
1850 auto *Macro = PP.getModuleMacro(Mod, II);
Richard Smithe56c8bc2015-04-22 00:26:11 +00001851 assert(Macro && "missing definition for overridden macro");
Richard Smith5dbef922015-04-22 02:09:43 +00001852 Overrides.push_back(Macro);
Richard Smithe56c8bc2015-04-22 00:26:11 +00001853 }
1854
1855 bool Inserted = false;
Richard Smith713369b2015-04-23 20:40:50 +00001856 Module *Owner = getSubmodule(MMR.SubModID);
Richard Smith20e883e2015-04-29 23:20:19 +00001857 PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted);
Richard Smithd7329392015-04-21 21:46:32 +00001858 }
1859 }
1860
1861 // Don't read the directive history for a module; we don't have anywhere
1862 // to put it.
Manman Ren11f2a472016-08-18 17:42:15 +00001863 if (M.isModule())
Richard Smithd7329392015-04-21 21:46:32 +00001864 return;
1865
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001866 // Deserialize the macro directives history in reverse source-order.
Craig Toppera13603a2014-05-22 05:54:18 +00001867 MacroDirective *Latest = nullptr, *Earliest = nullptr;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001868 unsigned Idx = 0, N = Record.size();
1869 while (Idx < N) {
Craig Toppera13603a2014-05-22 05:54:18 +00001870 MacroDirective *MD = nullptr;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001871 SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001872 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
1873 switch (K) {
1874 case MacroDirective::MD_Define: {
Richard Smith713369b2015-04-23 20:40:50 +00001875 MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++]));
Richard Smith3981b172015-04-30 02:16:23 +00001876 MD = PP.AllocateDefMacroDirective(MI, Loc);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001877 break;
1878 }
Richard Smithdaa69e02014-07-25 04:40:03 +00001879 case MacroDirective::MD_Undefine: {
Richard Smith3981b172015-04-30 02:16:23 +00001880 MD = PP.AllocateUndefMacroDirective(Loc);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001881 break;
Richard Smithdaa69e02014-07-25 04:40:03 +00001882 }
1883 case MacroDirective::MD_Visibility:
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001884 bool isPublic = Record[Idx++];
1885 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
1886 break;
1887 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001888
1889 if (!Latest)
1890 Latest = MD;
1891 if (Earliest)
1892 Earliest->setPrevious(MD);
1893 Earliest = MD;
1894 }
1895
Richard Smithd6e8c0d2015-05-04 19:58:00 +00001896 if (Latest)
1897 PP.setLoadedMacroDirective(II, Latest);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001898}
1899
Argyrios Kyrtzidisce9b49e2014-03-14 02:26:27 +00001900ASTReader::InputFileInfo
1901ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) {
Ben Langmuir198c1682014-03-07 07:27:49 +00001902 // Go find this input file.
1903 BitstreamCursor &Cursor = F.InputFilesCursor;
1904 SavedStreamPosition SavedPosition(Cursor);
1905 Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
1906
1907 unsigned Code = Cursor.ReadCode();
1908 RecordData Record;
1909 StringRef Blob;
1910
1911 unsigned Result = Cursor.readRecord(Code, Record, &Blob);
1912 assert(static_cast<InputFileRecordTypes>(Result) == INPUT_FILE &&
1913 "invalid record type for input file");
1914 (void)Result;
1915
1916 assert(Record[0] == ID && "Bogus stored ID or offset");
Richard Smitha8cfffa2015-11-26 02:04:16 +00001917 InputFileInfo R;
1918 R.StoredSize = static_cast<off_t>(Record[1]);
1919 R.StoredTime = static_cast<time_t>(Record[2]);
1920 R.Overridden = static_cast<bool>(Record[3]);
1921 R.Transient = static_cast<bool>(Record[4]);
1922 R.Filename = Blob;
1923 ResolveImportedPath(F, R.Filename);
Hans Wennborg73945142014-03-14 17:45:06 +00001924 return R;
Ben Langmuir198c1682014-03-07 07:27:49 +00001925}
1926
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001927InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001928 // If this ID is bogus, just return an empty input file.
1929 if (ID == 0 || ID > F.InputFilesLoaded.size())
1930 return InputFile();
1931
1932 // If we've already loaded this input file, return it.
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001933 if (F.InputFilesLoaded[ID-1].getFile())
Guy Benyei11169dd2012-12-18 14:30:41 +00001934 return F.InputFilesLoaded[ID-1];
1935
Argyrios Kyrtzidis9308f0a2014-01-08 19:13:34 +00001936 if (F.InputFilesLoaded[ID-1].isNotFound())
1937 return InputFile();
1938
Guy Benyei11169dd2012-12-18 14:30:41 +00001939 // Go find this input file.
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001940 BitstreamCursor &Cursor = F.InputFilesCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001941 SavedStreamPosition SavedPosition(Cursor);
1942 Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
1943
Argyrios Kyrtzidisce9b49e2014-03-14 02:26:27 +00001944 InputFileInfo FI = readInputFileInfo(F, ID);
1945 off_t StoredSize = FI.StoredSize;
1946 time_t StoredTime = FI.StoredTime;
1947 bool Overridden = FI.Overridden;
Richard Smitha8cfffa2015-11-26 02:04:16 +00001948 bool Transient = FI.Transient;
Argyrios Kyrtzidisce9b49e2014-03-14 02:26:27 +00001949 StringRef Filename = FI.Filename;
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001950
Richard Smitha8cfffa2015-11-26 02:04:16 +00001951 const FileEntry *File = FileMgr.getFile(Filename, /*OpenFile=*/false);
Ben Langmuir198c1682014-03-07 07:27:49 +00001952
1953 // If we didn't find the file, resolve it relative to the
1954 // original directory from which this AST file was created.
Craig Toppera13603a2014-05-22 05:54:18 +00001955 if (File == nullptr && !F.OriginalDir.empty() && !CurrentDir.empty() &&
Ben Langmuir198c1682014-03-07 07:27:49 +00001956 F.OriginalDir != CurrentDir) {
1957 std::string Resolved = resolveFileRelativeToOriginalDir(Filename,
1958 F.OriginalDir,
1959 CurrentDir);
1960 if (!Resolved.empty())
1961 File = FileMgr.getFile(Resolved);
1962 }
1963
1964 // For an overridden file, create a virtual file with the stored
1965 // size/timestamp.
Richard Smitha8cfffa2015-11-26 02:04:16 +00001966 if ((Overridden || Transient) && File == nullptr)
Ben Langmuir198c1682014-03-07 07:27:49 +00001967 File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
Ben Langmuir198c1682014-03-07 07:27:49 +00001968
Craig Toppera13603a2014-05-22 05:54:18 +00001969 if (File == nullptr) {
Ben Langmuir198c1682014-03-07 07:27:49 +00001970 if (Complain) {
1971 std::string ErrorStr = "could not find file '";
1972 ErrorStr += Filename;
Richard Smith68142212015-10-13 01:26:26 +00001973 ErrorStr += "' referenced by AST file '";
1974 ErrorStr += F.FileName;
1975 ErrorStr += "'";
Ben Langmuir198c1682014-03-07 07:27:49 +00001976 Error(ErrorStr.c_str());
Guy Benyei11169dd2012-12-18 14:30:41 +00001977 }
Ben Langmuir198c1682014-03-07 07:27:49 +00001978 // Record that we didn't find the file.
1979 F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
1980 return InputFile();
1981 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001982
Ben Langmuir198c1682014-03-07 07:27:49 +00001983 // Check if there was a request to override the contents of the file
1984 // that was part of the precompiled header. Overridding such a file
1985 // can lead to problems when lexing using the source locations from the
1986 // PCH.
1987 SourceManager &SM = getSourceManager();
Richard Smith64daf7b2015-12-01 03:32:49 +00001988 // FIXME: Reject if the overrides are different.
1989 if ((!Overridden && !Transient) && SM.isFileOverridden(File)) {
Ben Langmuir198c1682014-03-07 07:27:49 +00001990 if (Complain)
1991 Error(diag::err_fe_pch_file_overridden, Filename);
1992 // After emitting the diagnostic, recover by disabling the override so
1993 // that the original file will be used.
Richard Smitha8cfffa2015-11-26 02:04:16 +00001994 //
1995 // FIXME: This recovery is just as broken as the original state; there may
1996 // be another precompiled module that's using the overridden contents, or
1997 // we might be half way through parsing it. Instead, we should treat the
1998 // overridden contents as belonging to a separate FileEntry.
Ben Langmuir198c1682014-03-07 07:27:49 +00001999 SM.disableFileContentsOverride(File);
2000 // The FileEntry is a virtual file entry with the size of the contents
2001 // that would override the original contents. Set it to the original's
2002 // size/time.
2003 FileMgr.modifyFileEntry(const_cast<FileEntry*>(File),
2004 StoredSize, StoredTime);
2005 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002006
Ben Langmuir198c1682014-03-07 07:27:49 +00002007 bool IsOutOfDate = false;
2008
2009 // For an overridden file, there is nothing to validate.
Richard Smith96fdab62014-10-28 16:24:08 +00002010 if (!Overridden && //
2011 (StoredSize != File->getSize() ||
Richard Smithe75ee0f2015-08-17 07:13:32 +00002012 (StoredTime && StoredTime != File->getModificationTime() &&
2013 !DisableValidation)
Ben Langmuir198c1682014-03-07 07:27:49 +00002014 )) {
2015 if (Complain) {
2016 // Build a list of the PCH imports that got us here (in reverse).
2017 SmallVector<ModuleFile *, 4> ImportStack(1, &F);
2018 while (ImportStack.back()->ImportedBy.size() > 0)
2019 ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
Ben Langmuire82630d2014-01-17 00:19:09 +00002020
Ben Langmuir198c1682014-03-07 07:27:49 +00002021 // The top-level PCH is stale.
2022 StringRef TopLevelPCHName(ImportStack.back()->FileName);
2023 Error(diag::err_fe_pch_file_modified, Filename, TopLevelPCHName);
Ben Langmuire82630d2014-01-17 00:19:09 +00002024
Ben Langmuir198c1682014-03-07 07:27:49 +00002025 // Print the import stack.
2026 if (ImportStack.size() > 1 && !Diags.isDiagnosticInFlight()) {
2027 Diag(diag::note_pch_required_by)
2028 << Filename << ImportStack[0]->FileName;
2029 for (unsigned I = 1; I < ImportStack.size(); ++I)
Ben Langmuire82630d2014-01-17 00:19:09 +00002030 Diag(diag::note_pch_required_by)
Ben Langmuir198c1682014-03-07 07:27:49 +00002031 << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
Douglas Gregor7029ce12013-03-19 00:28:20 +00002032 }
2033
Ben Langmuir198c1682014-03-07 07:27:49 +00002034 if (!Diags.isDiagnosticInFlight())
2035 Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
Guy Benyei11169dd2012-12-18 14:30:41 +00002036 }
2037
Ben Langmuir198c1682014-03-07 07:27:49 +00002038 IsOutOfDate = true;
Guy Benyei11169dd2012-12-18 14:30:41 +00002039 }
Richard Smitha8cfffa2015-11-26 02:04:16 +00002040 // FIXME: If the file is overridden and we've already opened it,
2041 // issue an error (or split it into a separate FileEntry).
Guy Benyei11169dd2012-12-18 14:30:41 +00002042
Richard Smitha8cfffa2015-11-26 02:04:16 +00002043 InputFile IF = InputFile(File, Overridden || Transient, IsOutOfDate);
Ben Langmuir198c1682014-03-07 07:27:49 +00002044
2045 // Note that we've loaded this input file.
2046 F.InputFilesLoaded[ID-1] = IF;
2047 return IF;
Guy Benyei11169dd2012-12-18 14:30:41 +00002048}
2049
Richard Smith7ed1bc92014-12-05 22:42:13 +00002050/// \brief If we are loading a relocatable PCH or module file, and the filename
2051/// is not an absolute path, add the system or module root to the beginning of
2052/// the file name.
2053void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) {
2054 // Resolve relative to the base directory, if we have one.
2055 if (!M.BaseDirectory.empty())
2056 return ResolveImportedPath(Filename, M.BaseDirectory);
Guy Benyei11169dd2012-12-18 14:30:41 +00002057}
2058
Richard Smith7ed1bc92014-12-05 22:42:13 +00002059void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002060 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
2061 return;
2062
Richard Smith7ed1bc92014-12-05 22:42:13 +00002063 SmallString<128> Buffer;
2064 llvm::sys::path::append(Buffer, Prefix, Filename);
2065 Filename.assign(Buffer.begin(), Buffer.end());
Guy Benyei11169dd2012-12-18 14:30:41 +00002066}
2067
Richard Smith0f99d6a2015-08-09 08:48:41 +00002068static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) {
2069 switch (ARR) {
2070 case ASTReader::Failure: return true;
2071 case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing);
2072 case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate);
2073 case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch);
2074 case ASTReader::ConfigurationMismatch:
2075 return !(Caps & ASTReader::ARR_ConfigurationMismatch);
2076 case ASTReader::HadErrors: return true;
2077 case ASTReader::Success: return false;
2078 }
2079
2080 llvm_unreachable("unknown ASTReadResult");
2081}
2082
Richard Smith0516b182015-09-08 19:40:14 +00002083ASTReader::ASTReadResult ASTReader::ReadOptionsBlock(
2084 BitstreamCursor &Stream, unsigned ClientLoadCapabilities,
2085 bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener,
Manman Ren47a44452016-07-26 17:12:17 +00002086 std::string &SuggestedPredefines, bool ValidateDiagnosticOptions) {
Richard Smith0516b182015-09-08 19:40:14 +00002087 if (Stream.EnterSubBlock(OPTIONS_BLOCK_ID))
2088 return Failure;
2089
2090 // Read all of the records in the options block.
2091 RecordData Record;
2092 ASTReadResult Result = Success;
2093 while (1) {
2094 llvm::BitstreamEntry Entry = Stream.advance();
2095
2096 switch (Entry.Kind) {
2097 case llvm::BitstreamEntry::Error:
2098 case llvm::BitstreamEntry::SubBlock:
2099 return Failure;
2100
2101 case llvm::BitstreamEntry::EndBlock:
2102 return Result;
2103
2104 case llvm::BitstreamEntry::Record:
2105 // The interesting case.
2106 break;
2107 }
2108
2109 // Read and process a record.
2110 Record.clear();
2111 switch ((OptionsRecordTypes)Stream.readRecord(Entry.ID, Record)) {
2112 case LANGUAGE_OPTIONS: {
2113 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2114 if (ParseLanguageOptions(Record, Complain, Listener,
2115 AllowCompatibleConfigurationMismatch))
2116 Result = ConfigurationMismatch;
2117 break;
2118 }
2119
2120 case TARGET_OPTIONS: {
2121 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2122 if (ParseTargetOptions(Record, Complain, Listener,
2123 AllowCompatibleConfigurationMismatch))
2124 Result = ConfigurationMismatch;
2125 break;
2126 }
2127
2128 case DIAGNOSTIC_OPTIONS: {
2129 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
Manman Ren47a44452016-07-26 17:12:17 +00002130 if (ValidateDiagnosticOptions &&
2131 !AllowCompatibleConfigurationMismatch &&
Richard Smith0516b182015-09-08 19:40:14 +00002132 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.
Manman Ren11f2a472016-08-18 17:42:15 +00002196 if (!DisableValidation && F.Kind != MK_ExplicitModule &&
2197 F.Kind != MK_PrebuiltModule) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002198 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
Ben Langmuircb69b572014-03-07 06:40:32 +00002199
Dmitri Gribenkof430da42014-02-12 10:33:14 +00002200 // If we are reading a module, we will create a verification timestamp,
2201 // so we verify all input files. Otherwise, verify only user input
2202 // files.
Ben Langmuircb69b572014-03-07 06:40:32 +00002203
2204 unsigned N = NumUserInputs;
2205 if (ValidateSystemInputs ||
Richard Smithe842a472014-10-22 02:05:46 +00002206 (HSOpts.ModulesValidateOncePerBuildSession &&
Ben Langmuiracb803e2014-11-10 22:13:10 +00002207 F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp &&
Richard Smithe842a472014-10-22 02:05:46 +00002208 F.Kind == MK_ImplicitModule))
Ben Langmuircb69b572014-03-07 06:40:32 +00002209 N = NumInputs;
2210
Ben Langmuir3d4417c2014-02-07 17:31:11 +00002211 for (unsigned I = 0; I < N; ++I) {
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00002212 InputFile IF = getInputFile(F, I+1, Complain);
2213 if (!IF.getFile() || IF.isOutOfDate())
Guy Benyei11169dd2012-12-18 14:30:41 +00002214 return OutOfDate;
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00002215 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002216 }
Ben Langmuircb69b572014-03-07 06:40:32 +00002217
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +00002218 if (Listener)
Richard Smith216a3bd2015-08-13 17:57:10 +00002219 Listener->visitModuleFile(F.FileName, F.Kind);
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +00002220
Ben Langmuircb69b572014-03-07 06:40:32 +00002221 if (Listener && Listener->needsInputFileVisitation()) {
2222 unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
2223 : NumUserInputs;
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +00002224 for (unsigned I = 0; I < N; ++I) {
2225 bool IsSystem = I >= NumUserInputs;
2226 InputFileInfo FI = readInputFileInfo(F, I+1);
Richard Smith216a3bd2015-08-13 17:57:10 +00002227 Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden,
Manman Ren11f2a472016-08-18 17:42:15 +00002228 F.Kind == MK_ExplicitModule ||
2229 F.Kind == MK_PrebuiltModule);
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +00002230 }
Ben Langmuircb69b572014-03-07 06:40:32 +00002231 }
2232
Richard Smith8a308ec2015-11-05 00:54:55 +00002233 return Result;
Dmitri Gribenkof430da42014-02-12 10:33:14 +00002234 }
2235
Chris Lattnere7b154b2013-01-19 21:39:22 +00002236 case llvm::BitstreamEntry::SubBlock:
2237 switch (Entry.ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002238 case INPUT_FILES_BLOCK_ID:
2239 F.InputFilesCursor = Stream;
2240 if (Stream.SkipBlock() || // Skip with the main cursor
2241 // Read the abbreviations
2242 ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
2243 Error("malformed block record in AST file");
2244 return Failure;
2245 }
2246 continue;
Richard Smith0516b182015-09-08 19:40:14 +00002247
2248 case OPTIONS_BLOCK_ID:
2249 // If we're reading the first module for this group, check its options
2250 // are compatible with ours. For modules it imports, no further checking
2251 // is required, because we checked them when we built it.
2252 if (Listener && !ImportedBy) {
2253 // Should we allow the configuration of the module file to differ from
2254 // the configuration of the current translation unit in a compatible
2255 // way?
2256 //
2257 // FIXME: Allow this for files explicitly specified with -include-pch.
2258 bool AllowCompatibleConfigurationMismatch =
Manman Ren11f2a472016-08-18 17:42:15 +00002259 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
Manman Ren47a44452016-07-26 17:12:17 +00002260 const HeaderSearchOptions &HSOpts =
2261 PP.getHeaderSearchInfo().getHeaderSearchOpts();
Richard Smith0516b182015-09-08 19:40:14 +00002262
Richard Smith8a308ec2015-11-05 00:54:55 +00002263 Result = ReadOptionsBlock(Stream, ClientLoadCapabilities,
2264 AllowCompatibleConfigurationMismatch,
Manman Ren47a44452016-07-26 17:12:17 +00002265 *Listener, SuggestedPredefines,
2266 HSOpts.ModulesValidateDiagnosticOptions);
Richard Smith0516b182015-09-08 19:40:14 +00002267 if (Result == Failure) {
2268 Error("malformed block record in AST file");
2269 return Result;
2270 }
2271
Richard Smith8a308ec2015-11-05 00:54:55 +00002272 if (DisableValidation ||
2273 (AllowConfigurationMismatch && Result == ConfigurationMismatch))
2274 Result = Success;
2275
Ben Langmuir9b1e442e2016-02-11 18:54:02 +00002276 // If we can't load the module, exit early since we likely
2277 // will rebuild the module anyway. The stream may be in the
2278 // middle of a block.
2279 if (Result != Success)
Richard Smith0516b182015-09-08 19:40:14 +00002280 return Result;
2281 } else if (Stream.SkipBlock()) {
2282 Error("malformed block record in AST file");
2283 return Failure;
2284 }
2285 continue;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002286
Guy Benyei11169dd2012-12-18 14:30:41 +00002287 default:
Chris Lattnere7b154b2013-01-19 21:39:22 +00002288 if (Stream.SkipBlock()) {
2289 Error("malformed block record in AST file");
2290 return Failure;
2291 }
2292 continue;
Guy Benyei11169dd2012-12-18 14:30:41 +00002293 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00002294
2295 case llvm::BitstreamEntry::Record:
2296 // The interesting case.
2297 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002298 }
2299
2300 // Read and process a record.
2301 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00002302 StringRef Blob;
2303 switch ((ControlRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002304 case METADATA: {
2305 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2306 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
Dmitri Gribenko2228cd32014-02-11 15:40:09 +00002307 Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
2308 : diag::err_pch_version_too_new);
Guy Benyei11169dd2012-12-18 14:30:41 +00002309 return VersionMismatch;
2310 }
2311
Richard Smithe75ee0f2015-08-17 07:13:32 +00002312 bool hasErrors = Record[6];
Guy Benyei11169dd2012-12-18 14:30:41 +00002313 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
2314 Diag(diag::err_pch_with_compiler_errors);
2315 return HadErrors;
2316 }
Argyrios Kyrtzidis70ec1c72016-07-13 20:35:26 +00002317 if (hasErrors) {
2318 Diags.ErrorOccurred = true;
2319 Diags.UncompilableErrorOccurred = true;
2320 Diags.UnrecoverableErrorOccurred = true;
2321 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002322
2323 F.RelocatablePCH = Record[4];
Richard Smith7ed1bc92014-12-05 22:42:13 +00002324 // Relative paths in a relocatable PCH are relative to our sysroot.
2325 if (F.RelocatablePCH)
2326 F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
Guy Benyei11169dd2012-12-18 14:30:41 +00002327
Richard Smithe75ee0f2015-08-17 07:13:32 +00002328 F.HasTimestamps = Record[5];
2329
Guy Benyei11169dd2012-12-18 14:30:41 +00002330 const std::string &CurBranch = getClangFullRepositoryVersion();
Chris Lattner0e6c9402013-01-20 02:38:54 +00002331 StringRef ASTBranch = Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00002332 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2333 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
Dmitri Gribenko2228cd32014-02-11 15:40:09 +00002334 Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
Guy Benyei11169dd2012-12-18 14:30:41 +00002335 return VersionMismatch;
2336 }
2337 break;
2338 }
2339
Ben Langmuir487ea142014-10-23 18:05:36 +00002340 case SIGNATURE:
2341 assert((!F.Signature || F.Signature == Record[0]) && "signature changed");
2342 F.Signature = Record[0];
2343 break;
2344
Guy Benyei11169dd2012-12-18 14:30:41 +00002345 case IMPORTS: {
2346 // Load each of the imported PCH files.
2347 unsigned Idx = 0, N = Record.size();
2348 while (Idx < N) {
2349 // Read information about the AST file.
2350 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
2351 // The import location will be the local one for now; we will adjust
2352 // all import locations of module imports after the global source
Richard Smithb22a1d12016-03-27 20:13:24 +00002353 // location info are setup, in ReadAST.
Guy Benyei11169dd2012-12-18 14:30:41 +00002354 SourceLocation ImportLoc =
Richard Smithb22a1d12016-03-27 20:13:24 +00002355 ReadUntranslatedSourceLocation(Record[Idx++]);
Douglas Gregor7029ce12013-03-19 00:28:20 +00002356 off_t StoredSize = (off_t)Record[Idx++];
2357 time_t StoredModTime = (time_t)Record[Idx++];
Ben Langmuir487ea142014-10-23 18:05:36 +00002358 ASTFileSignature StoredSignature = Record[Idx++];
Richard Smith7ed1bc92014-12-05 22:42:13 +00002359 auto ImportedFile = ReadPath(F, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00002360
Richard Smith0f99d6a2015-08-09 08:48:41 +00002361 // If our client can't cope with us being out of date, we can't cope with
2362 // our dependency being missing.
2363 unsigned Capabilities = ClientLoadCapabilities;
2364 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2365 Capabilities &= ~ARR_Missing;
2366
Guy Benyei11169dd2012-12-18 14:30:41 +00002367 // Load the AST file.
Richard Smith0f99d6a2015-08-09 08:48:41 +00002368 auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F,
2369 Loaded, StoredSize, StoredModTime,
2370 StoredSignature, Capabilities);
2371
2372 // If we diagnosed a problem, produce a backtrace.
2373 if (isDiagnosedResult(Result, Capabilities))
2374 Diag(diag::note_module_file_imported_by)
2375 << F.FileName << !F.ModuleName.empty() << F.ModuleName;
2376
2377 switch (Result) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002378 case Failure: return Failure;
2379 // If we have to ignore the dependency, we'll have to ignore this too.
Douglas Gregor2f1806e2013-03-19 00:38:50 +00002380 case Missing:
Guy Benyei11169dd2012-12-18 14:30:41 +00002381 case OutOfDate: return OutOfDate;
2382 case VersionMismatch: return VersionMismatch;
2383 case ConfigurationMismatch: return ConfigurationMismatch;
2384 case HadErrors: return HadErrors;
2385 case Success: break;
2386 }
2387 }
2388 break;
2389 }
2390
Guy Benyei11169dd2012-12-18 14:30:41 +00002391 case ORIGINAL_FILE:
2392 F.OriginalSourceFileID = FileID::get(Record[0]);
Chris Lattner0e6c9402013-01-20 02:38:54 +00002393 F.ActualOriginalSourceFileName = Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00002394 F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
Richard Smith7ed1bc92014-12-05 22:42:13 +00002395 ResolveImportedPath(F, F.OriginalSourceFileName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002396 break;
2397
2398 case ORIGINAL_FILE_ID:
2399 F.OriginalSourceFileID = FileID::get(Record[0]);
2400 break;
2401
2402 case ORIGINAL_PCH_DIR:
Chris Lattner0e6c9402013-01-20 02:38:54 +00002403 F.OriginalDir = Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00002404 break;
2405
Ben Langmuirbeee15e2014-04-14 18:00:01 +00002406 case MODULE_NAME:
2407 F.ModuleName = Blob;
Ben Langmuir4f5212a2014-04-14 22:12:44 +00002408 if (Listener)
2409 Listener->ReadModuleName(F.ModuleName);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00002410 break;
2411
Richard Smith223d3f22014-12-06 03:21:08 +00002412 case MODULE_DIRECTORY: {
2413 assert(!F.ModuleName.empty() &&
2414 "MODULE_DIRECTORY found before MODULE_NAME");
2415 // If we've already loaded a module map file covering this module, we may
2416 // have a better path for it (relative to the current build).
2417 Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
2418 if (M && M->Directory) {
2419 // If we're implicitly loading a module, the base directory can't
2420 // change between the build and use.
Manman Ren11f2a472016-08-18 17:42:15 +00002421 if (F.Kind != MK_ExplicitModule && F.Kind != MK_PrebuiltModule) {
Richard Smith223d3f22014-12-06 03:21:08 +00002422 const DirectoryEntry *BuildDir =
2423 PP.getFileManager().getDirectory(Blob);
2424 if (!BuildDir || BuildDir != M->Directory) {
2425 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2426 Diag(diag::err_imported_module_relocated)
2427 << F.ModuleName << Blob << M->Directory->getName();
2428 return OutOfDate;
2429 }
2430 }
2431 F.BaseDirectory = M->Directory->getName();
2432 } else {
2433 F.BaseDirectory = Blob;
2434 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00002435 break;
Richard Smith223d3f22014-12-06 03:21:08 +00002436 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00002437
Ben Langmuirbeee15e2014-04-14 18:00:01 +00002438 case MODULE_MAP_FILE:
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00002439 if (ASTReadResult Result =
2440 ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
2441 return Result;
Ben Langmuir264ea152014-11-08 00:06:39 +00002442 break;
2443
Justin Bognerca9c0cc2015-06-21 20:32:36 +00002444 case INPUT_FILE_OFFSETS:
Richard Smitha1825302014-10-23 22:18:29 +00002445 NumInputs = Record[0];
2446 NumUserInputs = Record[1];
Justin Bogner4c183242015-06-21 20:32:40 +00002447 F.InputFileOffsets =
2448 (const llvm::support::unaligned_uint64_t *)Blob.data();
Richard Smitha1825302014-10-23 22:18:29 +00002449 F.InputFilesLoaded.resize(NumInputs);
Guy Benyei11169dd2012-12-18 14:30:41 +00002450 break;
2451 }
2452 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002453}
2454
Ben Langmuir2c9af442014-04-10 17:57:43 +00002455ASTReader::ASTReadResult
2456ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00002457 BitstreamCursor &Stream = F.Stream;
Guy Benyei11169dd2012-12-18 14:30:41 +00002458
2459 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
2460 Error("malformed block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002461 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002462 }
2463
2464 // Read all of the records and blocks for the AST file.
2465 RecordData Record;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002466 while (1) {
2467 llvm::BitstreamEntry Entry = Stream.advance();
2468
2469 switch (Entry.Kind) {
2470 case llvm::BitstreamEntry::Error:
2471 Error("error at end of module block in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002472 return Failure;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002473 case llvm::BitstreamEntry::EndBlock: {
Richard Smithc0fbba72013-04-03 22:49:41 +00002474 // Outside of C++, we do not store a lookup map for the translation unit.
2475 // Instead, mark it as needing a lookup map to be built if this module
2476 // contains any declarations lexically within it (which it always does!).
2477 // This usually has no cost, since we very rarely need the lookup map for
2478 // the translation unit outside C++.
Guy Benyei11169dd2012-12-18 14:30:41 +00002479 DeclContext *DC = Context.getTranslationUnitDecl();
Richard Smithc0fbba72013-04-03 22:49:41 +00002480 if (DC->hasExternalLexicalStorage() &&
2481 !getContext().getLangOpts().CPlusPlus)
Guy Benyei11169dd2012-12-18 14:30:41 +00002482 DC->setMustBuildLookupTable();
Chris Lattnere7b154b2013-01-19 21:39:22 +00002483
Ben Langmuir2c9af442014-04-10 17:57:43 +00002484 return Success;
Guy Benyei11169dd2012-12-18 14:30:41 +00002485 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00002486 case llvm::BitstreamEntry::SubBlock:
2487 switch (Entry.ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002488 case DECLTYPES_BLOCK_ID:
2489 // We lazily load the decls block, but we want to set up the
2490 // DeclsCursor cursor to point into it. Clone our current bitcode
2491 // cursor to it, enter the block and read the abbrevs in that block.
2492 // With the main cursor, we just skip over it.
2493 F.DeclsCursor = Stream;
2494 if (Stream.SkipBlock() || // Skip with the main cursor.
2495 // Read the abbrevs.
2496 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
2497 Error("malformed block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002498 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002499 }
2500 break;
Richard Smithb9eab6d2014-03-20 19:44:17 +00002501
Guy Benyei11169dd2012-12-18 14:30:41 +00002502 case PREPROCESSOR_BLOCK_ID:
2503 F.MacroCursor = Stream;
2504 if (!PP.getExternalSource())
2505 PP.setExternalSource(this);
Chris Lattnere7b154b2013-01-19 21:39:22 +00002506
Guy Benyei11169dd2012-12-18 14:30:41 +00002507 if (Stream.SkipBlock() ||
2508 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
2509 Error("malformed block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002510 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002511 }
2512 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
2513 break;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002514
Guy Benyei11169dd2012-12-18 14:30:41 +00002515 case PREPROCESSOR_DETAIL_BLOCK_ID:
2516 F.PreprocessorDetailCursor = Stream;
2517 if (Stream.SkipBlock() ||
Chris Lattnere7b154b2013-01-19 21:39:22 +00002518 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
Guy Benyei11169dd2012-12-18 14:30:41 +00002519 PREPROCESSOR_DETAIL_BLOCK_ID)) {
Chris Lattnere7b154b2013-01-19 21:39:22 +00002520 Error("malformed preprocessor detail record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002521 return Failure;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002522 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002523 F.PreprocessorDetailStartOffset
Chris Lattnere7b154b2013-01-19 21:39:22 +00002524 = F.PreprocessorDetailCursor.GetCurrentBitNo();
2525
Guy Benyei11169dd2012-12-18 14:30:41 +00002526 if (!PP.getPreprocessingRecord())
2527 PP.createPreprocessingRecord();
2528 if (!PP.getPreprocessingRecord()->getExternalSource())
2529 PP.getPreprocessingRecord()->SetExternalSource(*this);
2530 break;
2531
2532 case SOURCE_MANAGER_BLOCK_ID:
2533 if (ReadSourceManagerBlock(F))
Ben Langmuir2c9af442014-04-10 17:57:43 +00002534 return Failure;
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 SUBMODULE_BLOCK_ID:
Ben Langmuir2c9af442014-04-10 17:57:43 +00002538 if (ASTReadResult Result = ReadSubmoduleBlock(F, ClientLoadCapabilities))
2539 return Result;
Guy Benyei11169dd2012-12-18 14:30:41 +00002540 break;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002541
Guy Benyei11169dd2012-12-18 14:30:41 +00002542 case COMMENTS_BLOCK_ID: {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00002543 BitstreamCursor C = Stream;
Guy Benyei11169dd2012-12-18 14:30:41 +00002544 if (Stream.SkipBlock() ||
2545 ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
2546 Error("malformed comments block in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002547 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002548 }
2549 CommentsCursors.push_back(std::make_pair(C, &F));
2550 break;
2551 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00002552
Guy Benyei11169dd2012-12-18 14:30:41 +00002553 default:
Chris Lattnere7b154b2013-01-19 21:39:22 +00002554 if (Stream.SkipBlock()) {
2555 Error("malformed block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002556 return Failure;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002557 }
2558 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002559 }
2560 continue;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002561
2562 case llvm::BitstreamEntry::Record:
2563 // The interesting case.
2564 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002565 }
2566
2567 // Read and process a record.
2568 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00002569 StringRef Blob;
2570 switch ((ASTRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002571 default: // Default behavior: ignore.
2572 break;
2573
2574 case TYPE_OFFSET: {
2575 if (F.LocalNumTypes != 0) {
2576 Error("duplicate TYPE_OFFSET record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002577 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002578 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00002579 F.TypeOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002580 F.LocalNumTypes = Record[0];
2581 unsigned LocalBaseTypeIndex = Record[1];
2582 F.BaseTypeIndex = getTotalNumTypes();
2583
2584 if (F.LocalNumTypes > 0) {
2585 // Introduce the global -> local mapping for types within this module.
2586 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
2587
2588 // Introduce the local -> global mapping for types within this module.
2589 F.TypeRemap.insertOrReplace(
2590 std::make_pair(LocalBaseTypeIndex,
2591 F.BaseTypeIndex - LocalBaseTypeIndex));
Ben Langmuir52ca6782014-10-20 16:27:32 +00002592
2593 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
Guy Benyei11169dd2012-12-18 14:30:41 +00002594 }
2595 break;
2596 }
2597
2598 case DECL_OFFSET: {
2599 if (F.LocalNumDecls != 0) {
2600 Error("duplicate DECL_OFFSET record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002601 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002602 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00002603 F.DeclOffsets = (const DeclOffset *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002604 F.LocalNumDecls = Record[0];
2605 unsigned LocalBaseDeclID = Record[1];
2606 F.BaseDeclID = getTotalNumDecls();
2607
2608 if (F.LocalNumDecls > 0) {
2609 // Introduce the global -> local mapping for declarations within this
2610 // module.
2611 GlobalDeclMap.insert(
2612 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
2613
2614 // Introduce the local -> global mapping for declarations within this
2615 // module.
2616 F.DeclRemap.insertOrReplace(
2617 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
2618
2619 // Introduce the global -> local mapping for declarations within this
2620 // module.
2621 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
Ben Langmuirfe971d92014-08-16 04:54:18 +00002622
Ben Langmuir52ca6782014-10-20 16:27:32 +00002623 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
2624 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002625 break;
2626 }
2627
2628 case TU_UPDATE_LEXICAL: {
2629 DeclContext *TU = Context.getTranslationUnitDecl();
Richard Smith82f8fcd2015-08-06 22:07:25 +00002630 LexicalContents Contents(
2631 reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
2632 Blob.data()),
2633 static_cast<unsigned int>(Blob.size() / 4));
2634 TULexicalDecls.push_back(std::make_pair(&F, Contents));
Guy Benyei11169dd2012-12-18 14:30:41 +00002635 TU->setHasExternalLexicalStorage(true);
2636 break;
2637 }
2638
2639 case UPDATE_VISIBLE: {
2640 unsigned Idx = 0;
2641 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
Richard Smith0f4e2c42015-08-06 04:23:48 +00002642 auto *Data = (const unsigned char*)Blob.data();
Richard Smithd88a7f12015-09-01 20:35:42 +00002643 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data});
Richard Smith0f4e2c42015-08-06 04:23:48 +00002644 // If we've already loaded the decl, perform the updates when we finish
2645 // loading this block.
2646 if (Decl *D = GetExistingDecl(ID))
2647 PendingUpdateRecords.push_back(std::make_pair(ID, D));
Guy Benyei11169dd2012-12-18 14:30:41 +00002648 break;
2649 }
2650
2651 case IDENTIFIER_TABLE:
Chris Lattner0e6c9402013-01-20 02:38:54 +00002652 F.IdentifierTableData = Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002653 if (Record[0]) {
Justin Bognerda4e6502014-04-14 16:34:29 +00002654 F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
2655 (const unsigned char *)F.IdentifierTableData + Record[0],
2656 (const unsigned char *)F.IdentifierTableData + sizeof(uint32_t),
2657 (const unsigned char *)F.IdentifierTableData,
2658 ASTIdentifierLookupTrait(*this, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002659
2660 PP.getIdentifierTable().setExternalIdentifierLookup(this);
2661 }
2662 break;
2663
2664 case IDENTIFIER_OFFSET: {
2665 if (F.LocalNumIdentifiers != 0) {
2666 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002667 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002668 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00002669 F.IdentifierOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002670 F.LocalNumIdentifiers = Record[0];
2671 unsigned LocalBaseIdentifierID = Record[1];
2672 F.BaseIdentifierID = getTotalNumIdentifiers();
2673
2674 if (F.LocalNumIdentifiers > 0) {
2675 // Introduce the global -> local mapping for identifiers within this
2676 // module.
2677 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
2678 &F));
2679
2680 // Introduce the local -> global mapping for identifiers within this
2681 // module.
2682 F.IdentifierRemap.insertOrReplace(
2683 std::make_pair(LocalBaseIdentifierID,
2684 F.BaseIdentifierID - LocalBaseIdentifierID));
Ben Langmuirfe971d92014-08-16 04:54:18 +00002685
Ben Langmuir52ca6782014-10-20 16:27:32 +00002686 IdentifiersLoaded.resize(IdentifiersLoaded.size()
2687 + F.LocalNumIdentifiers);
2688 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002689 break;
2690 }
2691
Richard Smith33e0f7e2015-07-22 02:08:40 +00002692 case INTERESTING_IDENTIFIERS:
2693 F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end());
2694 break;
2695
Ben Langmuir332aafe2014-01-31 01:06:56 +00002696 case EAGERLY_DESERIALIZED_DECLS:
Richard Smith9e2341d2015-03-23 03:25:59 +00002697 // FIXME: Skip reading this record if our ASTConsumer doesn't care
2698 // about "interesting" decls (for instance, if we're building a module).
Guy Benyei11169dd2012-12-18 14:30:41 +00002699 for (unsigned I = 0, N = Record.size(); I != N; ++I)
Ben Langmuir332aafe2014-01-31 01:06:56 +00002700 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
Guy Benyei11169dd2012-12-18 14:30:41 +00002701 break;
2702
2703 case SPECIAL_TYPES:
Douglas Gregor44180f82013-02-01 23:45:03 +00002704 if (SpecialTypes.empty()) {
2705 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2706 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
2707 break;
2708 }
2709
2710 if (SpecialTypes.size() != Record.size()) {
2711 Error("invalid special-types record");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002712 return Failure;
Douglas Gregor44180f82013-02-01 23:45:03 +00002713 }
2714
2715 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2716 serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
2717 if (!SpecialTypes[I])
2718 SpecialTypes[I] = ID;
2719 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
2720 // merge step?
2721 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002722 break;
2723
2724 case STATISTICS:
2725 TotalNumStatements += Record[0];
2726 TotalNumMacros += Record[1];
2727 TotalLexicalDeclContexts += Record[2];
2728 TotalVisibleDeclContexts += Record[3];
2729 break;
2730
2731 case UNUSED_FILESCOPED_DECLS:
2732 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2733 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
2734 break;
2735
2736 case DELEGATING_CTORS:
2737 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2738 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
2739 break;
2740
2741 case WEAK_UNDECLARED_IDENTIFIERS:
2742 if (Record.size() % 4 != 0) {
2743 Error("invalid weak identifiers record");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002744 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002745 }
2746
2747 // FIXME: Ignore weak undeclared identifiers from non-original PCH
2748 // files. This isn't the way to do it :)
2749 WeakUndeclaredIdentifiers.clear();
2750
2751 // Translate the weak, undeclared identifiers into global IDs.
2752 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
2753 WeakUndeclaredIdentifiers.push_back(
2754 getGlobalIdentifierID(F, Record[I++]));
2755 WeakUndeclaredIdentifiers.push_back(
2756 getGlobalIdentifierID(F, Record[I++]));
2757 WeakUndeclaredIdentifiers.push_back(
2758 ReadSourceLocation(F, Record, I).getRawEncoding());
2759 WeakUndeclaredIdentifiers.push_back(Record[I++]);
2760 }
2761 break;
2762
Guy Benyei11169dd2012-12-18 14:30:41 +00002763 case SELECTOR_OFFSETS: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00002764 F.SelectorOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002765 F.LocalNumSelectors = Record[0];
2766 unsigned LocalBaseSelectorID = Record[1];
2767 F.BaseSelectorID = getTotalNumSelectors();
2768
2769 if (F.LocalNumSelectors > 0) {
2770 // Introduce the global -> local mapping for selectors within this
2771 // module.
2772 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
2773
2774 // Introduce the local -> global mapping for selectors within this
2775 // module.
2776 F.SelectorRemap.insertOrReplace(
2777 std::make_pair(LocalBaseSelectorID,
2778 F.BaseSelectorID - LocalBaseSelectorID));
Ben Langmuir52ca6782014-10-20 16:27:32 +00002779
2780 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
Guy Benyei11169dd2012-12-18 14:30:41 +00002781 }
2782 break;
2783 }
2784
2785 case METHOD_POOL:
Chris Lattner0e6c9402013-01-20 02:38:54 +00002786 F.SelectorLookupTableData = (const unsigned char *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002787 if (Record[0])
2788 F.SelectorLookupTable
2789 = ASTSelectorLookupTable::Create(
2790 F.SelectorLookupTableData + Record[0],
2791 F.SelectorLookupTableData,
2792 ASTSelectorLookupTrait(*this, F));
2793 TotalNumMethodPoolEntries += Record[1];
2794 break;
2795
2796 case REFERENCED_SELECTOR_POOL:
2797 if (!Record.empty()) {
2798 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
2799 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
2800 Record[Idx++]));
2801 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
2802 getRawEncoding());
2803 }
2804 }
2805 break;
2806
2807 case PP_COUNTER_VALUE:
2808 if (!Record.empty() && Listener)
2809 Listener->ReadCounter(F, Record[0]);
2810 break;
2811
2812 case FILE_SORTED_DECLS:
Chris Lattner0e6c9402013-01-20 02:38:54 +00002813 F.FileSortedDecls = (const DeclID *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002814 F.NumFileSortedDecls = Record[0];
2815 break;
2816
2817 case SOURCE_LOCATION_OFFSETS: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00002818 F.SLocEntryOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002819 F.LocalNumSLocEntries = Record[0];
2820 unsigned SLocSpaceSize = Record[1];
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00002821 std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
Ben Langmuir52ca6782014-10-20 16:27:32 +00002822 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
Guy Benyei11169dd2012-12-18 14:30:41 +00002823 SLocSpaceSize);
Richard Smith78d81ec2015-08-12 22:25:24 +00002824 if (!F.SLocEntryBaseID) {
2825 Error("ran out of source locations");
2826 break;
2827 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002828 // Make our entry in the range map. BaseID is negative and growing, so
2829 // we invert it. Because we invert it, though, we need the other end of
2830 // the range.
2831 unsigned RangeStart =
2832 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
2833 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
2834 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
2835
2836 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
2837 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
2838 GlobalSLocOffsetMap.insert(
2839 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
2840 - SLocSpaceSize,&F));
2841
2842 // Initialize the remapping table.
2843 // Invalid stays invalid.
Richard Smithb9eab6d2014-03-20 19:44:17 +00002844 F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
Guy Benyei11169dd2012-12-18 14:30:41 +00002845 // This module. Base was 2 when being compiled.
Richard Smithb9eab6d2014-03-20 19:44:17 +00002846 F.SLocRemap.insertOrReplace(std::make_pair(2U,
Guy Benyei11169dd2012-12-18 14:30:41 +00002847 static_cast<int>(F.SLocEntryBaseOffset - 2)));
2848
2849 TotalNumSLocEntries += F.LocalNumSLocEntries;
2850 break;
2851 }
2852
2853 case MODULE_OFFSET_MAP: {
2854 // Additional remapping information.
Chris Lattner0e6c9402013-01-20 02:38:54 +00002855 const unsigned char *Data = (const unsigned char*)Blob.data();
2856 const unsigned char *DataEnd = Data + Blob.size();
Richard Smithb9eab6d2014-03-20 19:44:17 +00002857
2858 // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
2859 if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
2860 F.SLocRemap.insert(std::make_pair(0U, 0));
2861 F.SLocRemap.insert(std::make_pair(2U, 1));
2862 }
2863
Guy Benyei11169dd2012-12-18 14:30:41 +00002864 // Continuous range maps we may be updating in our module.
Ben Langmuir785180e2014-10-20 16:27:30 +00002865 typedef ContinuousRangeMap<uint32_t, int, 2>::Builder
2866 RemapBuilder;
2867 RemapBuilder SLocRemap(F.SLocRemap);
2868 RemapBuilder IdentifierRemap(F.IdentifierRemap);
2869 RemapBuilder MacroRemap(F.MacroRemap);
2870 RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
2871 RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
2872 RemapBuilder SelectorRemap(F.SelectorRemap);
2873 RemapBuilder DeclRemap(F.DeclRemap);
2874 RemapBuilder TypeRemap(F.TypeRemap);
Guy Benyei11169dd2012-12-18 14:30:41 +00002875
Richard Smithd8879c82015-08-24 21:59:32 +00002876 while (Data < DataEnd) {
Justin Bogner57ba0b22014-03-28 22:03:24 +00002877 using namespace llvm::support;
2878 uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data);
Guy Benyei11169dd2012-12-18 14:30:41 +00002879 StringRef Name = StringRef((const char*)Data, Len);
2880 Data += Len;
2881 ModuleFile *OM = ModuleMgr.lookup(Name);
2882 if (!OM) {
2883 Error("SourceLocation remap refers to unknown module");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002884 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002885 }
2886
Justin Bogner57ba0b22014-03-28 22:03:24 +00002887 uint32_t SLocOffset =
2888 endian::readNext<uint32_t, little, unaligned>(Data);
2889 uint32_t IdentifierIDOffset =
2890 endian::readNext<uint32_t, little, unaligned>(Data);
2891 uint32_t MacroIDOffset =
2892 endian::readNext<uint32_t, little, unaligned>(Data);
2893 uint32_t PreprocessedEntityIDOffset =
2894 endian::readNext<uint32_t, little, unaligned>(Data);
2895 uint32_t SubmoduleIDOffset =
2896 endian::readNext<uint32_t, little, unaligned>(Data);
2897 uint32_t SelectorIDOffset =
2898 endian::readNext<uint32_t, little, unaligned>(Data);
2899 uint32_t DeclIDOffset =
2900 endian::readNext<uint32_t, little, unaligned>(Data);
2901 uint32_t TypeIndexOffset =
2902 endian::readNext<uint32_t, little, unaligned>(Data);
2903
Ben Langmuir785180e2014-10-20 16:27:30 +00002904 uint32_t None = std::numeric_limits<uint32_t>::max();
2905
2906 auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
2907 RemapBuilder &Remap) {
2908 if (Offset != None)
2909 Remap.insert(std::make_pair(Offset,
2910 static_cast<int>(BaseOffset - Offset)));
2911 };
2912 mapOffset(SLocOffset, OM->SLocEntryBaseOffset, SLocRemap);
2913 mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap);
2914 mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
2915 mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
2916 PreprocessedEntityRemap);
2917 mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
2918 mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
2919 mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap);
2920 mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap);
Guy Benyei11169dd2012-12-18 14:30:41 +00002921
2922 // Global -> local mappings.
2923 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
2924 }
2925 break;
2926 }
2927
2928 case SOURCE_MANAGER_LINE_TABLE:
2929 if (ParseLineTable(F, Record))
Ben Langmuir2c9af442014-04-10 17:57:43 +00002930 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002931 break;
2932
2933 case SOURCE_LOCATION_PRELOADS: {
2934 // Need to transform from the local view (1-based IDs) to the global view,
2935 // which is based off F.SLocEntryBaseID.
2936 if (!F.PreloadSLocEntries.empty()) {
2937 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002938 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002939 }
2940
2941 F.PreloadSLocEntries.swap(Record);
2942 break;
2943 }
2944
2945 case EXT_VECTOR_DECLS:
2946 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2947 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
2948 break;
2949
2950 case VTABLE_USES:
2951 if (Record.size() % 3 != 0) {
2952 Error("Invalid VTABLE_USES record");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002953 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002954 }
2955
2956 // Later tables overwrite earlier ones.
2957 // FIXME: Modules will have some trouble with this. This is clearly not
2958 // the right way to do this.
2959 VTableUses.clear();
2960
2961 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
2962 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
2963 VTableUses.push_back(
2964 ReadSourceLocation(F, Record, Idx).getRawEncoding());
2965 VTableUses.push_back(Record[Idx++]);
2966 }
2967 break;
2968
Guy Benyei11169dd2012-12-18 14:30:41 +00002969 case PENDING_IMPLICIT_INSTANTIATIONS:
2970 if (PendingInstantiations.size() % 2 != 0) {
2971 Error("Invalid existing PendingInstantiations");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002972 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002973 }
2974
2975 if (Record.size() % 2 != 0) {
2976 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002977 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002978 }
2979
2980 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2981 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
2982 PendingInstantiations.push_back(
2983 ReadSourceLocation(F, Record, I).getRawEncoding());
2984 }
2985 break;
2986
2987 case SEMA_DECL_REFS:
Richard Smith3d8e97e2013-10-18 06:54:39 +00002988 if (Record.size() != 2) {
2989 Error("Invalid SEMA_DECL_REFS block");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002990 return Failure;
Richard Smith3d8e97e2013-10-18 06:54:39 +00002991 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002992 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2993 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
2994 break;
2995
2996 case PPD_ENTITIES_OFFSETS: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00002997 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
2998 assert(Blob.size() % sizeof(PPEntityOffset) == 0);
2999 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
Guy Benyei11169dd2012-12-18 14:30:41 +00003000
3001 unsigned LocalBasePreprocessedEntityID = Record[0];
3002
3003 unsigned StartingID;
3004 if (!PP.getPreprocessingRecord())
3005 PP.createPreprocessingRecord();
3006 if (!PP.getPreprocessingRecord()->getExternalSource())
3007 PP.getPreprocessingRecord()->SetExternalSource(*this);
3008 StartingID
3009 = PP.getPreprocessingRecord()
Ben Langmuir52ca6782014-10-20 16:27:32 +00003010 ->allocateLoadedEntities(F.NumPreprocessedEntities);
Guy Benyei11169dd2012-12-18 14:30:41 +00003011 F.BasePreprocessedEntityID = StartingID;
3012
3013 if (F.NumPreprocessedEntities > 0) {
3014 // Introduce the global -> local mapping for preprocessed entities in
3015 // this module.
3016 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
3017
3018 // Introduce the local -> global mapping for preprocessed entities in
3019 // this module.
3020 F.PreprocessedEntityRemap.insertOrReplace(
3021 std::make_pair(LocalBasePreprocessedEntityID,
3022 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
3023 }
3024
3025 break;
3026 }
3027
3028 case DECL_UPDATE_OFFSETS: {
3029 if (Record.size() % 2 != 0) {
3030 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003031 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003032 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00003033 for (unsigned I = 0, N = Record.size(); I != N; I += 2) {
3034 GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
3035 DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1]));
3036
3037 // If we've already loaded the decl, perform the updates when we finish
3038 // loading this block.
3039 if (Decl *D = GetExistingDecl(ID))
3040 PendingUpdateRecords.push_back(std::make_pair(ID, D));
3041 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003042 break;
3043 }
3044
Guy Benyei11169dd2012-12-18 14:30:41 +00003045 case OBJC_CATEGORIES_MAP: {
3046 if (F.LocalNumObjCCategoriesInMap != 0) {
3047 Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003048 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003049 }
3050
3051 F.LocalNumObjCCategoriesInMap = Record[0];
Chris Lattner0e6c9402013-01-20 02:38:54 +00003052 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003053 break;
3054 }
3055
3056 case OBJC_CATEGORIES:
3057 F.ObjCCategories.swap(Record);
3058 break;
Richard Smithc2bb8182015-03-24 06:36:48 +00003059
Guy Benyei11169dd2012-12-18 14:30:41 +00003060 case DIAG_PRAGMA_MAPPINGS:
3061 if (F.PragmaDiagMappings.empty())
3062 F.PragmaDiagMappings.swap(Record);
3063 else
3064 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
3065 Record.begin(), Record.end());
3066 break;
3067
3068 case CUDA_SPECIAL_DECL_REFS:
3069 // Later tables overwrite earlier ones.
3070 // FIXME: Modules will have trouble with this.
3071 CUDASpecialDeclRefs.clear();
3072 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3073 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3074 break;
3075
3076 case HEADER_SEARCH_TABLE: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00003077 F.HeaderFileInfoTableData = Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003078 F.LocalNumHeaderFileInfos = Record[1];
Guy Benyei11169dd2012-12-18 14:30:41 +00003079 if (Record[0]) {
3080 F.HeaderFileInfoTable
3081 = HeaderFileInfoLookupTable::Create(
3082 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
3083 (const unsigned char *)F.HeaderFileInfoTableData,
3084 HeaderFileInfoTrait(*this, F,
3085 &PP.getHeaderSearchInfo(),
Chris Lattner0e6c9402013-01-20 02:38:54 +00003086 Blob.data() + Record[2]));
Guy Benyei11169dd2012-12-18 14:30:41 +00003087
3088 PP.getHeaderSearchInfo().SetExternalSource(this);
3089 if (!PP.getHeaderSearchInfo().getExternalLookup())
3090 PP.getHeaderSearchInfo().SetExternalLookup(this);
3091 }
3092 break;
3093 }
3094
3095 case FP_PRAGMA_OPTIONS:
3096 // Later tables overwrite earlier ones.
3097 FPPragmaOptions.swap(Record);
3098 break;
3099
3100 case OPENCL_EXTENSIONS:
3101 // Later tables overwrite earlier ones.
3102 OpenCLExtensions.swap(Record);
3103 break;
3104
3105 case TENTATIVE_DEFINITIONS:
3106 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3107 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
3108 break;
3109
3110 case KNOWN_NAMESPACES:
3111 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3112 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
3113 break;
Nick Lewycky8334af82013-01-26 00:35:08 +00003114
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00003115 case UNDEFINED_BUT_USED:
3116 if (UndefinedButUsed.size() % 2 != 0) {
3117 Error("Invalid existing UndefinedButUsed");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003118 return Failure;
Nick Lewycky8334af82013-01-26 00:35:08 +00003119 }
3120
3121 if (Record.size() % 2 != 0) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00003122 Error("invalid undefined-but-used record");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003123 return Failure;
Nick Lewycky8334af82013-01-26 00:35:08 +00003124 }
3125 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00003126 UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
3127 UndefinedButUsed.push_back(
Nick Lewycky8334af82013-01-26 00:35:08 +00003128 ReadSourceLocation(F, Record, I).getRawEncoding());
3129 }
3130 break;
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00003131 case DELETE_EXPRS_TO_ANALYZE:
3132 for (unsigned I = 0, N = Record.size(); I != N;) {
3133 DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++]));
3134 const uint64_t Count = Record[I++];
3135 DelayedDeleteExprs.push_back(Count);
3136 for (uint64_t C = 0; C < Count; ++C) {
3137 DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding());
3138 bool IsArrayForm = Record[I++] == 1;
3139 DelayedDeleteExprs.push_back(IsArrayForm);
3140 }
3141 }
3142 break;
Nick Lewycky8334af82013-01-26 00:35:08 +00003143
Guy Benyei11169dd2012-12-18 14:30:41 +00003144 case IMPORTED_MODULES: {
Manman Ren11f2a472016-08-18 17:42:15 +00003145 if (!F.isModule()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003146 // If we aren't loading a module (which has its own exports), make
3147 // all of the imported modules visible.
3148 // FIXME: Deal with macros-only imports.
Richard Smith56be7542014-03-21 00:33:59 +00003149 for (unsigned I = 0, N = Record.size(); I != N; /**/) {
3150 unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
3151 SourceLocation Loc = ReadSourceLocation(F, Record, I);
3152 if (GlobalID)
Aaron Ballman4f45b712014-03-21 15:22:56 +00003153 ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
Guy Benyei11169dd2012-12-18 14:30:41 +00003154 }
3155 }
3156 break;
3157 }
3158
Guy Benyei11169dd2012-12-18 14:30:41 +00003159 case MACRO_OFFSET: {
3160 if (F.LocalNumMacros != 0) {
3161 Error("duplicate MACRO_OFFSET record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003162 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003163 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00003164 F.MacroOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003165 F.LocalNumMacros = Record[0];
3166 unsigned LocalBaseMacroID = Record[1];
3167 F.BaseMacroID = getTotalNumMacros();
3168
3169 if (F.LocalNumMacros > 0) {
3170 // Introduce the global -> local mapping for macros within this module.
3171 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3172
3173 // Introduce the local -> global mapping for macros within this module.
3174 F.MacroRemap.insertOrReplace(
3175 std::make_pair(LocalBaseMacroID,
3176 F.BaseMacroID - LocalBaseMacroID));
Ben Langmuir52ca6782014-10-20 16:27:32 +00003177
3178 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
Guy Benyei11169dd2012-12-18 14:30:41 +00003179 }
3180 break;
3181 }
3182
Richard Smithe40f2ba2013-08-07 21:41:30 +00003183 case LATE_PARSED_TEMPLATE: {
3184 LateParsedTemplates.append(Record.begin(), Record.end());
3185 break;
3186 }
Dario Domizioli13a0a382014-05-23 12:13:25 +00003187
3188 case OPTIMIZE_PRAGMA_OPTIONS:
3189 if (Record.size() != 1) {
3190 Error("invalid pragma optimize record");
3191 return Failure;
3192 }
3193 OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
3194 break;
Nico Weber72889432014-09-06 01:25:55 +00003195
Nico Weber779355f2016-03-02 23:22:00 +00003196 case MSSTRUCT_PRAGMA_OPTIONS:
3197 if (Record.size() != 1) {
3198 Error("invalid pragma ms_struct record");
3199 return Failure;
3200 }
3201 PragmaMSStructState = Record[0];
3202 break;
3203
Nico Weber42932312016-03-03 00:17:35 +00003204 case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS:
3205 if (Record.size() != 2) {
3206 Error("invalid pragma ms_struct record");
3207 return Failure;
3208 }
3209 PragmaMSPointersToMembersState = Record[0];
3210 PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]);
3211 break;
3212
Nico Weber72889432014-09-06 01:25:55 +00003213 case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
3214 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3215 UnusedLocalTypedefNameCandidates.push_back(
3216 getGlobalDeclID(F, Record[I]));
3217 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003218 }
3219 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003220}
3221
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003222ASTReader::ASTReadResult
3223ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
3224 const ModuleFile *ImportedBy,
3225 unsigned ClientLoadCapabilities) {
3226 unsigned Idx = 0;
Richard Smith7ed1bc92014-12-05 22:42:13 +00003227 F.ModuleMapPath = ReadPath(F, Record, Idx);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003228
Manman Ren11f2a472016-08-18 17:42:15 +00003229 if (F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule) {
Richard Smithe842a472014-10-22 02:05:46 +00003230 // For an explicitly-loaded module, we don't care whether the original
3231 // module map file exists or matches.
3232 return Success;
3233 }
3234
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003235 // Try to resolve ModuleName in the current header search context and
3236 // verify that it is found in the same module map file as we saved. If the
3237 // top-level AST file is a main file, skip this check because there is no
3238 // usable header search context.
3239 assert(!F.ModuleName.empty() &&
Richard Smithe842a472014-10-22 02:05:46 +00003240 "MODULE_NAME should come before MODULE_MAP_FILE");
3241 if (F.Kind == MK_ImplicitModule &&
3242 (*ModuleMgr.begin())->Kind != MK_MainFile) {
3243 // An implicitly-loaded module file should have its module listed in some
3244 // module map file that we've already loaded.
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003245 Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
Richard Smithe842a472014-10-22 02:05:46 +00003246 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
3247 const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr;
3248 if (!ModMap) {
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003249 assert(ImportedBy && "top-level import should be verified");
Richard Smith0f99d6a2015-08-09 08:48:41 +00003250 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) {
3251 if (auto *ASTFE = M ? M->getASTFile() : nullptr)
3252 // This module was defined by an imported (explicit) module.
3253 Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName
3254 << ASTFE->getName();
3255 else
3256 // This module was built with a different module map.
3257 Diag(diag::err_imported_module_not_found)
3258 << F.ModuleName << F.FileName << ImportedBy->FileName
3259 << F.ModuleMapPath;
3260 }
3261 return OutOfDate;
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003262 }
3263
Richard Smithe842a472014-10-22 02:05:46 +00003264 assert(M->Name == F.ModuleName && "found module with different name");
3265
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003266 // Check the primary module map file.
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003267 const FileEntry *StoredModMap = FileMgr.getFile(F.ModuleMapPath);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003268 if (StoredModMap == nullptr || StoredModMap != ModMap) {
3269 assert(ModMap && "found module is missing module map file");
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003270 assert(ImportedBy && "top-level import should be verified");
3271 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3272 Diag(diag::err_imported_module_modmap_changed)
3273 << F.ModuleName << ImportedBy->FileName
3274 << ModMap->getName() << F.ModuleMapPath;
3275 return OutOfDate;
3276 }
3277
3278 llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps;
3279 for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
3280 // FIXME: we should use input files rather than storing names.
Richard Smith7ed1bc92014-12-05 22:42:13 +00003281 std::string Filename = ReadPath(F, Record, Idx);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003282 const FileEntry *F =
3283 FileMgr.getFile(Filename, false, false);
3284 if (F == nullptr) {
3285 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3286 Error("could not find file '" + Filename +"' referenced by AST file");
3287 return OutOfDate;
3288 }
3289 AdditionalStoredMaps.insert(F);
3290 }
3291
3292 // Check any additional module map files (e.g. module.private.modulemap)
3293 // that are not in the pcm.
3294 if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
3295 for (const FileEntry *ModMap : *AdditionalModuleMaps) {
3296 // Remove files that match
3297 // Note: SmallPtrSet::erase is really remove
3298 if (!AdditionalStoredMaps.erase(ModMap)) {
3299 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3300 Diag(diag::err_module_different_modmap)
3301 << F.ModuleName << /*new*/0 << ModMap->getName();
3302 return OutOfDate;
3303 }
3304 }
3305 }
3306
3307 // Check any additional module map files that are in the pcm, but not
3308 // found in header search. Cases that match are already removed.
3309 for (const FileEntry *ModMap : AdditionalStoredMaps) {
3310 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3311 Diag(diag::err_module_different_modmap)
3312 << F.ModuleName << /*not new*/1 << ModMap->getName();
3313 return OutOfDate;
3314 }
3315 }
3316
3317 if (Listener)
3318 Listener->ReadModuleMapFile(F.ModuleMapPath);
3319 return Success;
3320}
3321
3322
Douglas Gregorc1489562013-02-12 23:36:21 +00003323/// \brief Move the given method to the back of the global list of methods.
3324static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
3325 // Find the entry for this selector in the method pool.
3326 Sema::GlobalMethodPool::iterator Known
3327 = S.MethodPool.find(Method->getSelector());
3328 if (Known == S.MethodPool.end())
3329 return;
3330
3331 // Retrieve the appropriate method list.
3332 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
3333 : Known->second.second;
3334 bool Found = false;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003335 for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
Douglas Gregorc1489562013-02-12 23:36:21 +00003336 if (!Found) {
Nico Weber2e0c8f72014-12-27 03:58:08 +00003337 if (List->getMethod() == Method) {
Douglas Gregorc1489562013-02-12 23:36:21 +00003338 Found = true;
3339 } else {
3340 // Keep searching.
3341 continue;
3342 }
3343 }
3344
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003345 if (List->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003346 List->setMethod(List->getNext()->getMethod());
Douglas Gregorc1489562013-02-12 23:36:21 +00003347 else
Nico Weber2e0c8f72014-12-27 03:58:08 +00003348 List->setMethod(Method);
Douglas Gregorc1489562013-02-12 23:36:21 +00003349 }
3350}
3351
Richard Smithde711422015-04-23 21:20:19 +00003352void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
Richard Smith10434f32015-05-02 02:08:26 +00003353 assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
Richard Smith20e883e2015-04-29 23:20:19 +00003354 for (Decl *D : Names) {
Richard Smith49f906a2014-03-01 00:08:04 +00003355 bool wasHidden = D->Hidden;
3356 D->Hidden = false;
Guy Benyei11169dd2012-12-18 14:30:41 +00003357
Richard Smith49f906a2014-03-01 00:08:04 +00003358 if (wasHidden && SemaObj) {
3359 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
3360 moveMethodToBackOfGlobalList(*SemaObj, Method);
Douglas Gregorc1489562013-02-12 23:36:21 +00003361 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003362 }
3363 }
3364}
3365
Richard Smith49f906a2014-03-01 00:08:04 +00003366void ASTReader::makeModuleVisible(Module *Mod,
Argyrios Kyrtzidis125df052013-02-01 16:36:12 +00003367 Module::NameVisibilityKind NameVisibility,
Richard Smitha7e2cc62015-05-01 01:53:09 +00003368 SourceLocation ImportLoc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003369 llvm::SmallPtrSet<Module *, 4> Visited;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003370 SmallVector<Module *, 4> Stack;
Robert Wilhelm25284cc2013-08-23 16:11:15 +00003371 Stack.push_back(Mod);
Guy Benyei11169dd2012-12-18 14:30:41 +00003372 while (!Stack.empty()) {
Robert Wilhelm25284cc2013-08-23 16:11:15 +00003373 Mod = Stack.pop_back_val();
Guy Benyei11169dd2012-12-18 14:30:41 +00003374
3375 if (NameVisibility <= Mod->NameVisibility) {
Robert Wilhelm25284cc2013-08-23 16:11:15 +00003376 // This module already has this level of visibility (or greater), so
Guy Benyei11169dd2012-12-18 14:30:41 +00003377 // there is nothing more to do.
3378 continue;
3379 }
Richard Smith49f906a2014-03-01 00:08:04 +00003380
Guy Benyei11169dd2012-12-18 14:30:41 +00003381 if (!Mod->isAvailable()) {
3382 // Modules that aren't available cannot be made visible.
3383 continue;
3384 }
3385
3386 // Update the module's name visibility.
3387 Mod->NameVisibility = NameVisibility;
Richard Smith49f906a2014-03-01 00:08:04 +00003388
Guy Benyei11169dd2012-12-18 14:30:41 +00003389 // If we've already deserialized any names from this module,
3390 // mark them as visible.
3391 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
3392 if (Hidden != HiddenNamesMap.end()) {
Richard Smith57721ac2014-07-21 04:10:40 +00003393 auto HiddenNames = std::move(*Hidden);
Guy Benyei11169dd2012-12-18 14:30:41 +00003394 HiddenNamesMap.erase(Hidden);
Richard Smithde711422015-04-23 21:20:19 +00003395 makeNamesVisible(HiddenNames.second, HiddenNames.first);
Richard Smith57721ac2014-07-21 04:10:40 +00003396 assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() &&
3397 "making names visible added hidden names");
Guy Benyei11169dd2012-12-18 14:30:41 +00003398 }
Dmitri Gribenkoe9bcf5b2013-11-04 21:51:33 +00003399
Guy Benyei11169dd2012-12-18 14:30:41 +00003400 // Push any exported modules onto the stack to be marked as visible.
Argyrios Kyrtzidis8739f7b2013-02-19 19:34:40 +00003401 SmallVector<Module *, 16> Exports;
3402 Mod->getExportedModules(Exports);
3403 for (SmallVectorImpl<Module *>::iterator
3404 I = Exports.begin(), E = Exports.end(); I != E; ++I) {
3405 Module *Exported = *I;
David Blaikie82e95a32014-11-19 07:49:47 +00003406 if (Visited.insert(Exported).second)
Argyrios Kyrtzidis8739f7b2013-02-19 19:34:40 +00003407 Stack.push_back(Exported);
Guy Benyei11169dd2012-12-18 14:30:41 +00003408 }
3409 }
3410}
3411
Douglas Gregore060e572013-01-25 01:03:03 +00003412bool ASTReader::loadGlobalIndex() {
3413 if (GlobalIndex)
3414 return false;
3415
3416 if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
3417 !Context.getLangOpts().Modules)
3418 return true;
3419
3420 // Try to load the global index.
3421 TriedLoadingGlobalIndex = true;
3422 StringRef ModuleCachePath
3423 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
3424 std::pair<GlobalModuleIndex *, GlobalModuleIndex::ErrorCode> Result
Douglas Gregor7029ce12013-03-19 00:28:20 +00003425 = GlobalModuleIndex::readIndex(ModuleCachePath);
Douglas Gregore060e572013-01-25 01:03:03 +00003426 if (!Result.first)
3427 return true;
3428
3429 GlobalIndex.reset(Result.first);
Douglas Gregor7211ac12013-01-25 23:32:03 +00003430 ModuleMgr.setGlobalIndex(GlobalIndex.get());
Douglas Gregore060e572013-01-25 01:03:03 +00003431 return false;
3432}
3433
3434bool ASTReader::isGlobalIndexUnavailable() const {
3435 return Context.getLangOpts().Modules && UseGlobalIndex &&
3436 !hasGlobalIndex() && TriedLoadingGlobalIndex;
3437}
3438
Dmitri Gribenkof430da42014-02-12 10:33:14 +00003439static void updateModuleTimestamp(ModuleFile &MF) {
3440 // Overwrite the timestamp file contents so that file's mtime changes.
3441 std::string TimestampFilename = MF.getTimestampFilename();
Rafael Espindoladae941a2014-08-25 18:17:04 +00003442 std::error_code EC;
3443 llvm::raw_fd_ostream OS(TimestampFilename, EC, llvm::sys::fs::F_Text);
3444 if (EC)
Dmitri Gribenkof430da42014-02-12 10:33:14 +00003445 return;
3446 OS << "Timestamp file\n";
3447}
3448
Douglas Gregor6623e1f2015-11-03 18:33:07 +00003449/// \brief Given a cursor at the start of an AST file, scan ahead and drop the
3450/// cursor into the start of the given block ID, returning false on success and
3451/// true on failure.
3452static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
3453 while (1) {
3454 llvm::BitstreamEntry Entry = Cursor.advance();
3455 switch (Entry.Kind) {
3456 case llvm::BitstreamEntry::Error:
3457 case llvm::BitstreamEntry::EndBlock:
3458 return true;
3459
3460 case llvm::BitstreamEntry::Record:
3461 // Ignore top-level records.
3462 Cursor.skipRecord(Entry.ID);
3463 break;
3464
3465 case llvm::BitstreamEntry::SubBlock:
3466 if (Entry.ID == BlockID) {
3467 if (Cursor.EnterSubBlock(BlockID))
3468 return true;
3469 // Found it!
3470 return false;
3471 }
3472
3473 if (Cursor.SkipBlock())
3474 return true;
3475 }
3476 }
3477}
3478
Benjamin Kramer0772c422016-02-13 13:42:54 +00003479ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName,
Guy Benyei11169dd2012-12-18 14:30:41 +00003480 ModuleKind Type,
3481 SourceLocation ImportLoc,
3482 unsigned ClientLoadCapabilities) {
Argyrios Kyrtzidisdc9fdaf2013-05-24 05:44:08 +00003483 llvm::SaveAndRestore<SourceLocation>
3484 SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
3485
Richard Smithd1c46742014-04-30 02:24:17 +00003486 // Defer any pending actions until we get to the end of reading the AST file.
3487 Deserializing AnASTFile(this);
3488
Guy Benyei11169dd2012-12-18 14:30:41 +00003489 // Bump the generation number.
Richard Smith053f6c62014-05-16 23:01:30 +00003490 unsigned PreviousGeneration = incrementGeneration(Context);
Guy Benyei11169dd2012-12-18 14:30:41 +00003491
3492 unsigned NumModules = ModuleMgr.size();
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003493 SmallVector<ImportedModule, 4> Loaded;
Guy Benyei11169dd2012-12-18 14:30:41 +00003494 switch(ASTReadResult ReadResult = ReadASTCore(FileName, Type, ImportLoc,
Craig Toppera13603a2014-05-22 05:54:18 +00003495 /*ImportedBy=*/nullptr, Loaded,
Ben Langmuir487ea142014-10-23 18:05:36 +00003496 0, 0, 0,
Guy Benyei11169dd2012-12-18 14:30:41 +00003497 ClientLoadCapabilities)) {
3498 case Failure:
Douglas Gregor7029ce12013-03-19 00:28:20 +00003499 case Missing:
Guy Benyei11169dd2012-12-18 14:30:41 +00003500 case OutOfDate:
3501 case VersionMismatch:
3502 case ConfigurationMismatch:
Ben Langmuir9801b252014-06-20 00:24:56 +00003503 case HadErrors: {
3504 llvm::SmallPtrSet<ModuleFile *, 4> LoadedSet;
3505 for (const ImportedModule &IM : Loaded)
3506 LoadedSet.insert(IM.Mod);
3507
Douglas Gregor7029ce12013-03-19 00:28:20 +00003508 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, ModuleMgr.end(),
Ben Langmuir9801b252014-06-20 00:24:56 +00003509 LoadedSet,
Douglas Gregor7029ce12013-03-19 00:28:20 +00003510 Context.getLangOpts().Modules
3511 ? &PP.getHeaderSearchInfo().getModuleMap()
Craig Toppera13603a2014-05-22 05:54:18 +00003512 : nullptr);
Douglas Gregore060e572013-01-25 01:03:03 +00003513
3514 // If we find that any modules are unusable, the global index is going
3515 // to be out-of-date. Just remove it.
3516 GlobalIndex.reset();
Craig Toppera13603a2014-05-22 05:54:18 +00003517 ModuleMgr.setGlobalIndex(nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00003518 return ReadResult;
Ben Langmuir9801b252014-06-20 00:24:56 +00003519 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003520 case Success:
3521 break;
3522 }
3523
3524 // Here comes stuff that we only do once the entire chain is loaded.
3525
3526 // Load the AST blocks of all of the modules that we loaded.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003527 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
3528 MEnd = Loaded.end();
Guy Benyei11169dd2012-12-18 14:30:41 +00003529 M != MEnd; ++M) {
3530 ModuleFile &F = *M->Mod;
3531
3532 // Read the AST block.
Ben Langmuir2c9af442014-04-10 17:57:43 +00003533 if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities))
3534 return Result;
Guy Benyei11169dd2012-12-18 14:30:41 +00003535
Douglas Gregor6623e1f2015-11-03 18:33:07 +00003536 // Read the extension blocks.
3537 while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) {
3538 if (ASTReadResult Result = ReadExtensionBlock(F))
3539 return Result;
3540 }
3541
Guy Benyei11169dd2012-12-18 14:30:41 +00003542 // Once read, set the ModuleFile bit base offset and update the size in
3543 // bits of all files we've seen.
3544 F.GlobalBitOffset = TotalModulesSizeInBits;
3545 TotalModulesSizeInBits += F.SizeInBits;
3546 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
3547
3548 // Preload SLocEntries.
3549 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
3550 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
3551 // Load it through the SourceManager and don't call ReadSLocEntry()
3552 // directly because the entry may have already been loaded in which case
3553 // calling ReadSLocEntry() directly would trigger an assertion in
3554 // SourceManager.
3555 SourceMgr.getLoadedSLocEntryByID(Index);
3556 }
Richard Smith33e0f7e2015-07-22 02:08:40 +00003557
3558 // Preload all the pending interesting identifiers by marking them out of
3559 // date.
3560 for (auto Offset : F.PreloadIdentifierOffsets) {
3561 const unsigned char *Data = reinterpret_cast<const unsigned char *>(
3562 F.IdentifierTableData + Offset);
3563
3564 ASTIdentifierLookupTrait Trait(*this, F);
3565 auto KeyDataLen = Trait.ReadKeyDataLength(Data);
3566 auto Key = Trait.ReadKey(Data, KeyDataLen.first);
Richard Smith79bf9202015-08-24 03:33:22 +00003567 auto &II = PP.getIdentifierTable().getOwn(Key);
3568 II.setOutOfDate(true);
3569
3570 // Mark this identifier as being from an AST file so that we can track
3571 // whether we need to serialize it.
Richard Smitheb4b58f62016-02-05 01:40:54 +00003572 markIdentifierFromAST(*this, II);
Richard Smith79bf9202015-08-24 03:33:22 +00003573
3574 // Associate the ID with the identifier so that the writer can reuse it.
3575 auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first);
3576 SetIdentifierInfo(ID, &II);
Richard Smith33e0f7e2015-07-22 02:08:40 +00003577 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003578 }
3579
Douglas Gregor603cd862013-03-22 18:50:14 +00003580 // Setup the import locations and notify the module manager that we've
3581 // committed to these module files.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003582 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
3583 MEnd = Loaded.end();
Guy Benyei11169dd2012-12-18 14:30:41 +00003584 M != MEnd; ++M) {
3585 ModuleFile &F = *M->Mod;
Douglas Gregor603cd862013-03-22 18:50:14 +00003586
3587 ModuleMgr.moduleFileAccepted(&F);
3588
3589 // Set the import location.
Argyrios Kyrtzidis71c1af82013-02-01 16:36:14 +00003590 F.DirectImportLoc = ImportLoc;
Richard Smithb22a1d12016-03-27 20:13:24 +00003591 // FIXME: We assume that locations from PCH / preamble do not need
3592 // any translation.
Guy Benyei11169dd2012-12-18 14:30:41 +00003593 if (!M->ImportedBy)
3594 F.ImportLoc = M->ImportLoc;
3595 else
Richard Smithb22a1d12016-03-27 20:13:24 +00003596 F.ImportLoc = TranslateSourceLocation(*M->ImportedBy, M->ImportLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00003597 }
3598
Richard Smith33e0f7e2015-07-22 02:08:40 +00003599 if (!Context.getLangOpts().CPlusPlus ||
Manman Ren11f2a472016-08-18 17:42:15 +00003600 (Type != MK_ImplicitModule && Type != MK_ExplicitModule &&
3601 Type != MK_PrebuiltModule)) {
Richard Smith33e0f7e2015-07-22 02:08:40 +00003602 // Mark all of the identifiers in the identifier table as being out of date,
3603 // so that various accessors know to check the loaded modules when the
3604 // identifier is used.
3605 //
3606 // For C++ modules, we don't need information on many identifiers (just
3607 // those that provide macros or are poisoned), so we mark all of
3608 // the interesting ones via PreloadIdentifierOffsets.
3609 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
3610 IdEnd = PP.getIdentifierTable().end();
3611 Id != IdEnd; ++Id)
3612 Id->second->setOutOfDate(true);
3613 }
Manman Rena0f31a02016-04-29 19:04:05 +00003614 // Mark selectors as out of date.
3615 for (auto Sel : SelectorGeneration)
3616 SelectorOutOfDate[Sel.first] = true;
Guy Benyei11169dd2012-12-18 14:30:41 +00003617
3618 // Resolve any unresolved module exports.
Douglas Gregorfb912652013-03-20 21:10:35 +00003619 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
3620 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
Guy Benyei11169dd2012-12-18 14:30:41 +00003621 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
3622 Module *ResolvedMod = getSubmodule(GlobalID);
Douglas Gregorfb912652013-03-20 21:10:35 +00003623
3624 switch (Unresolved.Kind) {
3625 case UnresolvedModuleRef::Conflict:
3626 if (ResolvedMod) {
3627 Module::Conflict Conflict;
3628 Conflict.Other = ResolvedMod;
3629 Conflict.Message = Unresolved.String.str();
3630 Unresolved.Mod->Conflicts.push_back(Conflict);
3631 }
3632 continue;
3633
3634 case UnresolvedModuleRef::Import:
Guy Benyei11169dd2012-12-18 14:30:41 +00003635 if (ResolvedMod)
Richard Smith38477db2015-05-02 00:45:56 +00003636 Unresolved.Mod->Imports.insert(ResolvedMod);
Guy Benyei11169dd2012-12-18 14:30:41 +00003637 continue;
Guy Benyei11169dd2012-12-18 14:30:41 +00003638
Douglas Gregorfb912652013-03-20 21:10:35 +00003639 case UnresolvedModuleRef::Export:
3640 if (ResolvedMod || Unresolved.IsWildcard)
3641 Unresolved.Mod->Exports.push_back(
3642 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
3643 continue;
3644 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003645 }
Douglas Gregorfb912652013-03-20 21:10:35 +00003646 UnresolvedModuleRefs.clear();
Daniel Jasperba7f2f72013-09-24 09:14:14 +00003647
3648 // FIXME: How do we load the 'use'd modules? They may not be submodules.
3649 // Might be unnecessary as use declarations are only used to build the
3650 // module itself.
Guy Benyei11169dd2012-12-18 14:30:41 +00003651
3652 InitializeContext();
3653
Richard Smith3d8e97e2013-10-18 06:54:39 +00003654 if (SemaObj)
3655 UpdateSema();
3656
Guy Benyei11169dd2012-12-18 14:30:41 +00003657 if (DeserializationListener)
3658 DeserializationListener->ReaderInitialized(this);
3659
3660 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
Yaron Keren8b563662015-10-03 10:46:20 +00003661 if (PrimaryModule.OriginalSourceFileID.isValid()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003662 PrimaryModule.OriginalSourceFileID
3663 = FileID::get(PrimaryModule.SLocEntryBaseID
3664 + PrimaryModule.OriginalSourceFileID.getOpaqueValue() - 1);
3665
3666 // If this AST file is a precompiled preamble, then set the
3667 // preamble file ID of the source manager to the file source file
3668 // from which the preamble was built.
3669 if (Type == MK_Preamble) {
3670 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
3671 } else if (Type == MK_MainFile) {
3672 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
3673 }
3674 }
3675
3676 // For any Objective-C class definitions we have already loaded, make sure
3677 // that we load any additional categories.
3678 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
3679 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
3680 ObjCClassesLoaded[I],
3681 PreviousGeneration);
3682 }
Douglas Gregore060e572013-01-25 01:03:03 +00003683
Dmitri Gribenkof430da42014-02-12 10:33:14 +00003684 if (PP.getHeaderSearchInfo()
3685 .getHeaderSearchOpts()
3686 .ModulesValidateOncePerBuildSession) {
3687 // Now we are certain that the module and all modules it depends on are
3688 // up to date. Create or update timestamp files for modules that are
3689 // located in the module cache (not for PCH files that could be anywhere
3690 // in the filesystem).
3691 for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
3692 ImportedModule &M = Loaded[I];
Richard Smithe842a472014-10-22 02:05:46 +00003693 if (M.Mod->Kind == MK_ImplicitModule) {
Dmitri Gribenkof430da42014-02-12 10:33:14 +00003694 updateModuleTimestamp(*M.Mod);
3695 }
3696 }
3697 }
3698
Guy Benyei11169dd2012-12-18 14:30:41 +00003699 return Success;
3700}
3701
Ben Langmuir487ea142014-10-23 18:05:36 +00003702static ASTFileSignature readASTFileSignature(llvm::BitstreamReader &StreamFile);
3703
Ben Langmuir70a1b812015-03-24 04:43:52 +00003704/// \brief Whether \p Stream starts with the AST/PCH file magic number 'CPCH'.
3705static bool startsWithASTFileMagic(BitstreamCursor &Stream) {
3706 return Stream.Read(8) == 'C' &&
3707 Stream.Read(8) == 'P' &&
3708 Stream.Read(8) == 'C' &&
3709 Stream.Read(8) == 'H';
3710}
3711
Richard Smith0f99d6a2015-08-09 08:48:41 +00003712static unsigned moduleKindForDiagnostic(ModuleKind Kind) {
3713 switch (Kind) {
3714 case MK_PCH:
3715 return 0; // PCH
3716 case MK_ImplicitModule:
3717 case MK_ExplicitModule:
Manman Ren11f2a472016-08-18 17:42:15 +00003718 case MK_PrebuiltModule:
Richard Smith0f99d6a2015-08-09 08:48:41 +00003719 return 1; // module
3720 case MK_MainFile:
3721 case MK_Preamble:
3722 return 2; // main source file
3723 }
3724 llvm_unreachable("unknown module kind");
3725}
3726
Guy Benyei11169dd2012-12-18 14:30:41 +00003727ASTReader::ASTReadResult
3728ASTReader::ReadASTCore(StringRef FileName,
3729 ModuleKind Type,
3730 SourceLocation ImportLoc,
3731 ModuleFile *ImportedBy,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003732 SmallVectorImpl<ImportedModule> &Loaded,
Douglas Gregor7029ce12013-03-19 00:28:20 +00003733 off_t ExpectedSize, time_t ExpectedModTime,
Ben Langmuir487ea142014-10-23 18:05:36 +00003734 ASTFileSignature ExpectedSignature,
Guy Benyei11169dd2012-12-18 14:30:41 +00003735 unsigned ClientLoadCapabilities) {
3736 ModuleFile *M;
Guy Benyei11169dd2012-12-18 14:30:41 +00003737 std::string ErrorStr;
Douglas Gregor7029ce12013-03-19 00:28:20 +00003738 ModuleManager::AddModuleResult AddResult
3739 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
Richard Smith053f6c62014-05-16 23:01:30 +00003740 getGeneration(), ExpectedSize, ExpectedModTime,
Ben Langmuir487ea142014-10-23 18:05:36 +00003741 ExpectedSignature, readASTFileSignature,
Douglas Gregor7029ce12013-03-19 00:28:20 +00003742 M, ErrorStr);
Guy Benyei11169dd2012-12-18 14:30:41 +00003743
Douglas Gregor7029ce12013-03-19 00:28:20 +00003744 switch (AddResult) {
3745 case ModuleManager::AlreadyLoaded:
3746 return Success;
3747
3748 case ModuleManager::NewlyLoaded:
3749 // Load module file below.
3750 break;
3751
3752 case ModuleManager::Missing:
Richard Smithe842a472014-10-22 02:05:46 +00003753 // The module file was missing; if the client can handle that, return
Douglas Gregor7029ce12013-03-19 00:28:20 +00003754 // it.
3755 if (ClientLoadCapabilities & ARR_Missing)
3756 return Missing;
3757
3758 // Otherwise, return an error.
Richard Smith0f99d6a2015-08-09 08:48:41 +00003759 Diag(diag::err_module_file_not_found) << moduleKindForDiagnostic(Type)
3760 << FileName << ErrorStr.empty()
3761 << ErrorStr;
Douglas Gregor7029ce12013-03-19 00:28:20 +00003762 return Failure;
3763
3764 case ModuleManager::OutOfDate:
3765 // We couldn't load the module file because it is out-of-date. If the
3766 // client can handle out-of-date, return it.
3767 if (ClientLoadCapabilities & ARR_OutOfDate)
3768 return OutOfDate;
3769
3770 // Otherwise, return an error.
Richard Smith0f99d6a2015-08-09 08:48:41 +00003771 Diag(diag::err_module_file_out_of_date) << moduleKindForDiagnostic(Type)
3772 << FileName << ErrorStr.empty()
3773 << ErrorStr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003774 return Failure;
3775 }
3776
Douglas Gregor7029ce12013-03-19 00:28:20 +00003777 assert(M && "Missing module file");
Guy Benyei11169dd2012-12-18 14:30:41 +00003778
3779 // FIXME: This seems rather a hack. Should CurrentDir be part of the
3780 // module?
3781 if (FileName != "-") {
3782 CurrentDir = llvm::sys::path::parent_path(FileName);
3783 if (CurrentDir.empty()) CurrentDir = ".";
3784 }
3785
3786 ModuleFile &F = *M;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003787 BitstreamCursor &Stream = F.Stream;
Adrian Prantlfb2398d2015-07-17 01:19:54 +00003788 PCHContainerRdr.ExtractPCH(F.Buffer->getMemBufferRef(), F.StreamFile);
Rafael Espindolafd832392014-11-12 14:48:44 +00003789 Stream.init(&F.StreamFile);
Adrian Prantlcbc368c2015-02-25 02:44:04 +00003790 F.SizeInBits = F.Buffer->getBufferSize() * 8;
3791
Guy Benyei11169dd2012-12-18 14:30:41 +00003792 // Sniff for the signature.
Ben Langmuir70a1b812015-03-24 04:43:52 +00003793 if (!startsWithASTFileMagic(Stream)) {
Richard Smith0f99d6a2015-08-09 08:48:41 +00003794 Diag(diag::err_module_file_invalid) << moduleKindForDiagnostic(Type)
3795 << FileName;
Guy Benyei11169dd2012-12-18 14:30:41 +00003796 return Failure;
3797 }
3798
3799 // This is used for compatibility with older PCH formats.
3800 bool HaveReadControlBlock = false;
Chris Lattnerefa77172013-01-20 00:00:22 +00003801 while (1) {
3802 llvm::BitstreamEntry Entry = Stream.advance();
3803
3804 switch (Entry.Kind) {
3805 case llvm::BitstreamEntry::Error:
Chris Lattnerefa77172013-01-20 00:00:22 +00003806 case llvm::BitstreamEntry::Record:
Douglas Gregor6623e1f2015-11-03 18:33:07 +00003807 case llvm::BitstreamEntry::EndBlock:
Guy Benyei11169dd2012-12-18 14:30:41 +00003808 Error("invalid record at top-level of AST file");
3809 return Failure;
Chris Lattnerefa77172013-01-20 00:00:22 +00003810
3811 case llvm::BitstreamEntry::SubBlock:
3812 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003813 }
3814
Chris Lattnerefa77172013-01-20 00:00:22 +00003815 switch (Entry.ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003816 case CONTROL_BLOCK_ID:
3817 HaveReadControlBlock = true;
Ben Langmuirbeee15e2014-04-14 18:00:01 +00003818 switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003819 case Success:
Richard Smith0f99d6a2015-08-09 08:48:41 +00003820 // Check that we didn't try to load a non-module AST file as a module.
3821 //
3822 // FIXME: Should we also perform the converse check? Loading a module as
3823 // a PCH file sort of works, but it's a bit wonky.
Manman Ren11f2a472016-08-18 17:42:15 +00003824 if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule ||
3825 Type == MK_PrebuiltModule) &&
Richard Smith0f99d6a2015-08-09 08:48:41 +00003826 F.ModuleName.empty()) {
3827 auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
3828 if (Result != OutOfDate ||
3829 (ClientLoadCapabilities & ARR_OutOfDate) == 0)
3830 Diag(diag::err_module_file_not_module) << FileName;
3831 return Result;
3832 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003833 break;
3834
3835 case Failure: return Failure;
Douglas Gregor7029ce12013-03-19 00:28:20 +00003836 case Missing: return Missing;
Guy Benyei11169dd2012-12-18 14:30:41 +00003837 case OutOfDate: return OutOfDate;
3838 case VersionMismatch: return VersionMismatch;
3839 case ConfigurationMismatch: return ConfigurationMismatch;
3840 case HadErrors: return HadErrors;
3841 }
3842 break;
Richard Smithf8c32552015-09-02 17:45:54 +00003843
Guy Benyei11169dd2012-12-18 14:30:41 +00003844 case AST_BLOCK_ID:
3845 if (!HaveReadControlBlock) {
3846 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
Dmitri Gribenko2228cd32014-02-11 15:40:09 +00003847 Diag(diag::err_pch_version_too_old);
Guy Benyei11169dd2012-12-18 14:30:41 +00003848 return VersionMismatch;
3849 }
3850
3851 // Record that we've loaded this module.
3852 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
3853 return Success;
3854
3855 default:
3856 if (Stream.SkipBlock()) {
3857 Error("malformed block record in AST file");
3858 return Failure;
3859 }
3860 break;
3861 }
3862 }
Douglas Gregor6623e1f2015-11-03 18:33:07 +00003863
3864 return Success;
3865}
3866
3867/// Parse a record and blob containing module file extension metadata.
3868static bool parseModuleFileExtensionMetadata(
3869 const SmallVectorImpl<uint64_t> &Record,
3870 StringRef Blob,
3871 ModuleFileExtensionMetadata &Metadata) {
3872 if (Record.size() < 4) return true;
3873
3874 Metadata.MajorVersion = Record[0];
3875 Metadata.MinorVersion = Record[1];
3876
3877 unsigned BlockNameLen = Record[2];
3878 unsigned UserInfoLen = Record[3];
3879
3880 if (BlockNameLen + UserInfoLen > Blob.size()) return true;
3881
3882 Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
3883 Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
3884 Blob.data() + BlockNameLen + UserInfoLen);
3885 return false;
3886}
3887
3888ASTReader::ASTReadResult ASTReader::ReadExtensionBlock(ModuleFile &F) {
3889 BitstreamCursor &Stream = F.Stream;
3890
3891 RecordData Record;
3892 while (true) {
3893 llvm::BitstreamEntry Entry = Stream.advance();
3894 switch (Entry.Kind) {
3895 case llvm::BitstreamEntry::SubBlock:
3896 if (Stream.SkipBlock())
3897 return Failure;
3898
3899 continue;
3900
3901 case llvm::BitstreamEntry::EndBlock:
3902 return Success;
3903
3904 case llvm::BitstreamEntry::Error:
3905 return HadErrors;
3906
3907 case llvm::BitstreamEntry::Record:
3908 break;
3909 }
3910
3911 Record.clear();
3912 StringRef Blob;
3913 unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob);
3914 switch (RecCode) {
3915 case EXTENSION_METADATA: {
3916 ModuleFileExtensionMetadata Metadata;
3917 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
3918 return Failure;
3919
3920 // Find a module file extension with this block name.
3921 auto Known = ModuleFileExtensions.find(Metadata.BlockName);
3922 if (Known == ModuleFileExtensions.end()) break;
3923
3924 // Form a reader.
3925 if (auto Reader = Known->second->createExtensionReader(Metadata, *this,
3926 F, Stream)) {
3927 F.ExtensionReaders.push_back(std::move(Reader));
3928 }
3929
3930 break;
3931 }
3932 }
3933 }
3934
3935 return Success;
Guy Benyei11169dd2012-12-18 14:30:41 +00003936}
3937
Richard Smitha7e2cc62015-05-01 01:53:09 +00003938void ASTReader::InitializeContext() {
Guy Benyei11169dd2012-12-18 14:30:41 +00003939 // If there's a listener, notify them that we "read" the translation unit.
3940 if (DeserializationListener)
3941 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
3942 Context.getTranslationUnitDecl());
3943
Guy Benyei11169dd2012-12-18 14:30:41 +00003944 // FIXME: Find a better way to deal with collisions between these
3945 // built-in types. Right now, we just ignore the problem.
3946
3947 // Load the special types.
3948 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
3949 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
3950 if (!Context.CFConstantStringTypeDecl)
3951 Context.setCFConstantStringType(GetType(String));
3952 }
3953
3954 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
3955 QualType FileType = GetType(File);
3956 if (FileType.isNull()) {
3957 Error("FILE type is NULL");
3958 return;
3959 }
3960
3961 if (!Context.FILEDecl) {
3962 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
3963 Context.setFILEDecl(Typedef->getDecl());
3964 else {
3965 const TagType *Tag = FileType->getAs<TagType>();
3966 if (!Tag) {
3967 Error("Invalid FILE type in AST file");
3968 return;
3969 }
3970 Context.setFILEDecl(Tag->getDecl());
3971 }
3972 }
3973 }
3974
3975 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
3976 QualType Jmp_bufType = GetType(Jmp_buf);
3977 if (Jmp_bufType.isNull()) {
3978 Error("jmp_buf type is NULL");
3979 return;
3980 }
3981
3982 if (!Context.jmp_bufDecl) {
3983 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
3984 Context.setjmp_bufDecl(Typedef->getDecl());
3985 else {
3986 const TagType *Tag = Jmp_bufType->getAs<TagType>();
3987 if (!Tag) {
3988 Error("Invalid jmp_buf type in AST file");
3989 return;
3990 }
3991 Context.setjmp_bufDecl(Tag->getDecl());
3992 }
3993 }
3994 }
3995
3996 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
3997 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
3998 if (Sigjmp_bufType.isNull()) {
3999 Error("sigjmp_buf type is NULL");
4000 return;
4001 }
4002
4003 if (!Context.sigjmp_bufDecl) {
4004 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
4005 Context.setsigjmp_bufDecl(Typedef->getDecl());
4006 else {
4007 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
4008 assert(Tag && "Invalid sigjmp_buf type in AST file");
4009 Context.setsigjmp_bufDecl(Tag->getDecl());
4010 }
4011 }
4012 }
4013
4014 if (unsigned ObjCIdRedef
4015 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
4016 if (Context.ObjCIdRedefinitionType.isNull())
4017 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
4018 }
4019
4020 if (unsigned ObjCClassRedef
4021 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
4022 if (Context.ObjCClassRedefinitionType.isNull())
4023 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
4024 }
4025
4026 if (unsigned ObjCSelRedef
4027 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
4028 if (Context.ObjCSelRedefinitionType.isNull())
4029 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
4030 }
4031
4032 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
4033 QualType Ucontext_tType = GetType(Ucontext_t);
4034 if (Ucontext_tType.isNull()) {
4035 Error("ucontext_t type is NULL");
4036 return;
4037 }
4038
4039 if (!Context.ucontext_tDecl) {
4040 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
4041 Context.setucontext_tDecl(Typedef->getDecl());
4042 else {
4043 const TagType *Tag = Ucontext_tType->getAs<TagType>();
4044 assert(Tag && "Invalid ucontext_t type in AST file");
4045 Context.setucontext_tDecl(Tag->getDecl());
4046 }
4047 }
4048 }
4049 }
4050
4051 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
4052
4053 // If there were any CUDA special declarations, deserialize them.
4054 if (!CUDASpecialDeclRefs.empty()) {
4055 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
4056 Context.setcudaConfigureCallDecl(
4057 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
4058 }
Richard Smith56be7542014-03-21 00:33:59 +00004059
Guy Benyei11169dd2012-12-18 14:30:41 +00004060 // Re-export any modules that were imported by a non-module AST file.
Richard Smitha7e2cc62015-05-01 01:53:09 +00004061 // FIXME: This does not make macro-only imports visible again.
Richard Smith56be7542014-03-21 00:33:59 +00004062 for (auto &Import : ImportedModules) {
Richard Smitha7e2cc62015-05-01 01:53:09 +00004063 if (Module *Imported = getSubmodule(Import.ID)) {
Argyrios Kyrtzidis125df052013-02-01 16:36:12 +00004064 makeModuleVisible(Imported, Module::AllVisible,
Richard Smitha7e2cc62015-05-01 01:53:09 +00004065 /*ImportLoc=*/Import.ImportLoc);
Ben Langmuir6d25fdc2016-02-11 17:04:42 +00004066 if (Import.ImportLoc.isValid())
4067 PP.makeModuleVisible(Imported, Import.ImportLoc);
4068 // FIXME: should we tell Sema to make the module visible too?
Richard Smitha7e2cc62015-05-01 01:53:09 +00004069 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004070 }
4071 ImportedModules.clear();
4072}
4073
4074void ASTReader::finalizeForWriting() {
Richard Smithde711422015-04-23 21:20:19 +00004075 // Nothing to do for now.
Guy Benyei11169dd2012-12-18 14:30:41 +00004076}
4077
Ben Langmuir70a1b812015-03-24 04:43:52 +00004078/// \brief Reads and return the signature record from \p StreamFile's control
4079/// block, or else returns 0.
Ben Langmuir487ea142014-10-23 18:05:36 +00004080static ASTFileSignature readASTFileSignature(llvm::BitstreamReader &StreamFile){
4081 BitstreamCursor Stream(StreamFile);
Ben Langmuir70a1b812015-03-24 04:43:52 +00004082 if (!startsWithASTFileMagic(Stream))
Ben Langmuir487ea142014-10-23 18:05:36 +00004083 return 0;
Ben Langmuir487ea142014-10-23 18:05:36 +00004084
4085 // Scan for the CONTROL_BLOCK_ID block.
4086 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
4087 return 0;
4088
4089 // Scan for SIGNATURE inside the control block.
4090 ASTReader::RecordData Record;
4091 while (1) {
4092 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
4093 if (Entry.Kind == llvm::BitstreamEntry::EndBlock ||
4094 Entry.Kind != llvm::BitstreamEntry::Record)
4095 return 0;
4096
4097 Record.clear();
4098 StringRef Blob;
4099 if (SIGNATURE == Stream.readRecord(Entry.ID, Record, &Blob))
4100 return Record[0];
4101 }
4102}
4103
Guy Benyei11169dd2012-12-18 14:30:41 +00004104/// \brief Retrieve the name of the original source file name
4105/// directly from the AST file, without actually loading the AST
4106/// file.
Adrian Prantlbb165fb2015-06-20 18:53:08 +00004107std::string ASTReader::getOriginalSourceFile(
4108 const std::string &ASTFileName, FileManager &FileMgr,
Adrian Prantlfb2398d2015-07-17 01:19:54 +00004109 const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004110 // Open the AST file.
Benjamin Kramera8857962014-10-26 22:44:13 +00004111 auto Buffer = FileMgr.getBufferForFile(ASTFileName);
Guy Benyei11169dd2012-12-18 14:30:41 +00004112 if (!Buffer) {
Benjamin Kramera8857962014-10-26 22:44:13 +00004113 Diags.Report(diag::err_fe_unable_to_read_pch_file)
4114 << ASTFileName << Buffer.getError().message();
Guy Benyei11169dd2012-12-18 14:30:41 +00004115 return std::string();
4116 }
4117
4118 // Initialize the stream
4119 llvm::BitstreamReader StreamFile;
Adrian Prantlfb2398d2015-07-17 01:19:54 +00004120 PCHContainerRdr.ExtractPCH((*Buffer)->getMemBufferRef(), StreamFile);
Rafael Espindolaaf0e40a2014-11-12 14:42:25 +00004121 BitstreamCursor Stream(StreamFile);
Guy Benyei11169dd2012-12-18 14:30:41 +00004122
4123 // Sniff for the signature.
Ben Langmuir70a1b812015-03-24 04:43:52 +00004124 if (!startsWithASTFileMagic(Stream)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004125 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
4126 return std::string();
4127 }
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004128
Chris Lattnere7b154b2013-01-19 21:39:22 +00004129 // Scan for the CONTROL_BLOCK_ID block.
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004130 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004131 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
4132 return std::string();
Chris Lattnere7b154b2013-01-19 21:39:22 +00004133 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004134
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004135 // Scan for ORIGINAL_FILE inside the control block.
4136 RecordData Record;
Chris Lattnere7b154b2013-01-19 21:39:22 +00004137 while (1) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004138 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Chris Lattnere7b154b2013-01-19 21:39:22 +00004139 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
4140 return std::string();
4141
4142 if (Entry.Kind != llvm::BitstreamEntry::Record) {
4143 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
4144 return std::string();
Guy Benyei11169dd2012-12-18 14:30:41 +00004145 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00004146
Guy Benyei11169dd2012-12-18 14:30:41 +00004147 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00004148 StringRef Blob;
4149 if (Stream.readRecord(Entry.ID, Record, &Blob) == ORIGINAL_FILE)
4150 return Blob.str();
Guy Benyei11169dd2012-12-18 14:30:41 +00004151 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004152}
4153
4154namespace {
4155 class SimplePCHValidator : public ASTReaderListener {
4156 const LangOptions &ExistingLangOpts;
4157 const TargetOptions &ExistingTargetOpts;
4158 const PreprocessorOptions &ExistingPPOpts;
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00004159 std::string ExistingModuleCachePath;
Guy Benyei11169dd2012-12-18 14:30:41 +00004160 FileManager &FileMgr;
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00004161
Guy Benyei11169dd2012-12-18 14:30:41 +00004162 public:
4163 SimplePCHValidator(const LangOptions &ExistingLangOpts,
4164 const TargetOptions &ExistingTargetOpts,
4165 const PreprocessorOptions &ExistingPPOpts,
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00004166 StringRef ExistingModuleCachePath,
Guy Benyei11169dd2012-12-18 14:30:41 +00004167 FileManager &FileMgr)
4168 : ExistingLangOpts(ExistingLangOpts),
4169 ExistingTargetOpts(ExistingTargetOpts),
4170 ExistingPPOpts(ExistingPPOpts),
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00004171 ExistingModuleCachePath(ExistingModuleCachePath),
Guy Benyei11169dd2012-12-18 14:30:41 +00004172 FileMgr(FileMgr)
4173 {
4174 }
4175
Richard Smith1e2cf0d2014-10-31 02:28:58 +00004176 bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
4177 bool AllowCompatibleDifferences) override {
4178 return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr,
4179 AllowCompatibleDifferences);
Guy Benyei11169dd2012-12-18 14:30:41 +00004180 }
Chandler Carruth0d745bc2015-03-14 04:47:43 +00004181 bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
4182 bool AllowCompatibleDifferences) override {
4183 return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr,
4184 AllowCompatibleDifferences);
Guy Benyei11169dd2012-12-18 14:30:41 +00004185 }
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00004186 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
4187 StringRef SpecificModuleCachePath,
4188 bool Complain) override {
4189 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
4190 ExistingModuleCachePath,
4191 nullptr, ExistingLangOpts);
4192 }
Craig Topper3e89dfe2014-03-13 02:13:41 +00004193 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
4194 bool Complain,
4195 std::string &SuggestedPredefines) override {
Craig Toppera13603a2014-05-22 05:54:18 +00004196 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr,
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00004197 SuggestedPredefines, ExistingLangOpts);
Guy Benyei11169dd2012-12-18 14:30:41 +00004198 }
4199 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +00004200}
Guy Benyei11169dd2012-12-18 14:30:41 +00004201
Adrian Prantlbb165fb2015-06-20 18:53:08 +00004202bool ASTReader::readASTFileControlBlock(
4203 StringRef Filename, FileManager &FileMgr,
Adrian Prantlfb2398d2015-07-17 01:19:54 +00004204 const PCHContainerReader &PCHContainerRdr,
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004205 bool FindModuleFileExtensions,
Manman Ren47a44452016-07-26 17:12:17 +00004206 ASTReaderListener &Listener, bool ValidateDiagnosticOptions) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004207 // Open the AST file.
Richard Smith7f330cd2015-03-18 01:42:29 +00004208 // FIXME: This allows use of the VFS; we do not allow use of the
4209 // VFS when actually loading a module.
Benjamin Kramera8857962014-10-26 22:44:13 +00004210 auto Buffer = FileMgr.getBufferForFile(Filename);
Guy Benyei11169dd2012-12-18 14:30:41 +00004211 if (!Buffer) {
4212 return true;
4213 }
4214
4215 // Initialize the stream
4216 llvm::BitstreamReader StreamFile;
Adrian Prantlfb2398d2015-07-17 01:19:54 +00004217 PCHContainerRdr.ExtractPCH((*Buffer)->getMemBufferRef(), StreamFile);
Rafael Espindolaaf0e40a2014-11-12 14:42:25 +00004218 BitstreamCursor Stream(StreamFile);
Guy Benyei11169dd2012-12-18 14:30:41 +00004219
4220 // Sniff for the signature.
Ben Langmuir70a1b812015-03-24 04:43:52 +00004221 if (!startsWithASTFileMagic(Stream))
Guy Benyei11169dd2012-12-18 14:30:41 +00004222 return true;
Guy Benyei11169dd2012-12-18 14:30:41 +00004223
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004224 // Scan for the CONTROL_BLOCK_ID block.
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004225 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004226 return true;
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004227
4228 bool NeedsInputFiles = Listener.needsInputFileVisitation();
Ben Langmuircb69b572014-03-07 06:40:32 +00004229 bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
Richard Smithd4b230b2014-10-27 23:01:16 +00004230 bool NeedsImports = Listener.needsImportVisitation();
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004231 BitstreamCursor InputFilesCursor;
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004232
Guy Benyei11169dd2012-12-18 14:30:41 +00004233 RecordData Record;
Richard Smith7ed1bc92014-12-05 22:42:13 +00004234 std::string ModuleDir;
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004235 bool DoneWithControlBlock = false;
4236 while (!DoneWithControlBlock) {
Richard Smith0516b182015-09-08 19:40:14 +00004237 llvm::BitstreamEntry Entry = Stream.advance();
4238
4239 switch (Entry.Kind) {
4240 case llvm::BitstreamEntry::SubBlock: {
4241 switch (Entry.ID) {
4242 case OPTIONS_BLOCK_ID: {
4243 std::string IgnoredSuggestedPredefines;
4244 if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate,
4245 /*AllowCompatibleConfigurationMismatch*/ false,
Manman Ren47a44452016-07-26 17:12:17 +00004246 Listener, IgnoredSuggestedPredefines,
4247 ValidateDiagnosticOptions) != Success)
Richard Smith0516b182015-09-08 19:40:14 +00004248 return true;
4249 break;
4250 }
4251
4252 case INPUT_FILES_BLOCK_ID:
4253 InputFilesCursor = Stream;
4254 if (Stream.SkipBlock() ||
4255 (NeedsInputFiles &&
4256 ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID)))
4257 return true;
4258 break;
4259
4260 default:
4261 if (Stream.SkipBlock())
4262 return true;
4263 break;
4264 }
4265
4266 continue;
4267 }
4268
4269 case llvm::BitstreamEntry::EndBlock:
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004270 DoneWithControlBlock = true;
4271 break;
Richard Smith0516b182015-09-08 19:40:14 +00004272
4273 case llvm::BitstreamEntry::Error:
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004274 return true;
Richard Smith0516b182015-09-08 19:40:14 +00004275
4276 case llvm::BitstreamEntry::Record:
4277 break;
4278 }
4279
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004280 if (DoneWithControlBlock) break;
4281
Guy Benyei11169dd2012-12-18 14:30:41 +00004282 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00004283 StringRef Blob;
4284 unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob);
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004285 switch ((ControlRecordTypes)RecCode) {
4286 case METADATA: {
4287 if (Record[0] != VERSION_MAJOR)
4288 return true;
Guy Benyei11169dd2012-12-18 14:30:41 +00004289
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +00004290 if (Listener.ReadFullVersionInformation(Blob))
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004291 return true;
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +00004292
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004293 break;
4294 }
Ben Langmuir4f5212a2014-04-14 22:12:44 +00004295 case MODULE_NAME:
4296 Listener.ReadModuleName(Blob);
4297 break;
Richard Smith7ed1bc92014-12-05 22:42:13 +00004298 case MODULE_DIRECTORY:
4299 ModuleDir = Blob;
4300 break;
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00004301 case MODULE_MAP_FILE: {
4302 unsigned Idx = 0;
Richard Smith7ed1bc92014-12-05 22:42:13 +00004303 auto Path = ReadString(Record, Idx);
4304 ResolveImportedPath(Path, ModuleDir);
4305 Listener.ReadModuleMapFile(Path);
Ben Langmuir4f5212a2014-04-14 22:12:44 +00004306 break;
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00004307 }
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004308 case INPUT_FILE_OFFSETS: {
4309 if (!NeedsInputFiles)
4310 break;
4311
4312 unsigned NumInputFiles = Record[0];
4313 unsigned NumUserFiles = Record[1];
Richard Smithec216502015-02-13 19:48:37 +00004314 const uint64_t *InputFileOffs = (const uint64_t *)Blob.data();
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004315 for (unsigned I = 0; I != NumInputFiles; ++I) {
4316 // Go find this input file.
4317 bool isSystemFile = I >= NumUserFiles;
Ben Langmuircb69b572014-03-07 06:40:32 +00004318
4319 if (isSystemFile && !NeedsSystemInputFiles)
4320 break; // the rest are system input files
4321
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004322 BitstreamCursor &Cursor = InputFilesCursor;
4323 SavedStreamPosition SavedPosition(Cursor);
4324 Cursor.JumpToBit(InputFileOffs[I]);
4325
4326 unsigned Code = Cursor.ReadCode();
4327 RecordData Record;
4328 StringRef Blob;
4329 bool shouldContinue = false;
4330 switch ((InputFileRecordTypes)Cursor.readRecord(Code, Record, &Blob)) {
4331 case INPUT_FILE:
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +00004332 bool Overridden = static_cast<bool>(Record[3]);
Richard Smith7ed1bc92014-12-05 22:42:13 +00004333 std::string Filename = Blob;
4334 ResolveImportedPath(Filename, ModuleDir);
Richard Smith216a3bd2015-08-13 17:57:10 +00004335 shouldContinue = Listener.visitInputFile(
4336 Filename, isSystemFile, Overridden, /*IsExplicitModule*/false);
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004337 break;
4338 }
4339 if (!shouldContinue)
4340 break;
4341 }
4342 break;
4343 }
4344
Richard Smithd4b230b2014-10-27 23:01:16 +00004345 case IMPORTS: {
4346 if (!NeedsImports)
4347 break;
4348
4349 unsigned Idx = 0, N = Record.size();
4350 while (Idx < N) {
4351 // Read information about the AST file.
Richard Smith79c98cc2014-10-27 23:25:15 +00004352 Idx += 5; // ImportLoc, Size, ModTime, Signature
Richard Smith7ed1bc92014-12-05 22:42:13 +00004353 std::string Filename = ReadString(Record, Idx);
4354 ResolveImportedPath(Filename, ModuleDir);
4355 Listener.visitImport(Filename);
Richard Smithd4b230b2014-10-27 23:01:16 +00004356 }
4357 break;
4358 }
4359
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004360 default:
4361 // No other validation to perform.
4362 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004363 }
4364 }
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004365
4366 // Look for module file extension blocks, if requested.
4367 if (FindModuleFileExtensions) {
4368 while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) {
4369 bool DoneWithExtensionBlock = false;
4370 while (!DoneWithExtensionBlock) {
4371 llvm::BitstreamEntry Entry = Stream.advance();
4372
4373 switch (Entry.Kind) {
4374 case llvm::BitstreamEntry::SubBlock:
4375 if (Stream.SkipBlock())
4376 return true;
4377
4378 continue;
4379
4380 case llvm::BitstreamEntry::EndBlock:
4381 DoneWithExtensionBlock = true;
4382 continue;
4383
4384 case llvm::BitstreamEntry::Error:
4385 return true;
4386
4387 case llvm::BitstreamEntry::Record:
4388 break;
4389 }
4390
4391 Record.clear();
4392 StringRef Blob;
4393 unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob);
4394 switch (RecCode) {
4395 case EXTENSION_METADATA: {
4396 ModuleFileExtensionMetadata Metadata;
4397 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
4398 return true;
4399
4400 Listener.readModuleFileExtension(Metadata);
4401 break;
4402 }
4403 }
4404 }
4405 }
4406 }
4407
4408 return false;
Guy Benyei11169dd2012-12-18 14:30:41 +00004409}
4410
Adrian Prantlbb165fb2015-06-20 18:53:08 +00004411bool ASTReader::isAcceptableASTFile(
4412 StringRef Filename, FileManager &FileMgr,
Adrian Prantlfb2398d2015-07-17 01:19:54 +00004413 const PCHContainerReader &PCHContainerRdr, const LangOptions &LangOpts,
Adrian Prantlbb165fb2015-06-20 18:53:08 +00004414 const TargetOptions &TargetOpts, const PreprocessorOptions &PPOpts,
4415 std::string ExistingModuleCachePath) {
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00004416 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts,
4417 ExistingModuleCachePath, FileMgr);
Adrian Prantlfb2398d2015-07-17 01:19:54 +00004418 return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr,
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004419 /*FindModuleFileExtensions=*/false,
Manman Ren47a44452016-07-26 17:12:17 +00004420 validator,
4421 /*ValidateDiagnosticOptions=*/true);
Guy Benyei11169dd2012-12-18 14:30:41 +00004422}
4423
Ben Langmuir2c9af442014-04-10 17:57:43 +00004424ASTReader::ASTReadResult
4425ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004426 // Enter the submodule block.
4427 if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
4428 Error("malformed submodule block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00004429 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00004430 }
4431
4432 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
4433 bool First = true;
Craig Toppera13603a2014-05-22 05:54:18 +00004434 Module *CurrentModule = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00004435 RecordData Record;
4436 while (true) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004437 llvm::BitstreamEntry Entry = F.Stream.advanceSkippingSubblocks();
4438
4439 switch (Entry.Kind) {
4440 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
4441 case llvm::BitstreamEntry::Error:
4442 Error("malformed block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00004443 return Failure;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004444 case llvm::BitstreamEntry::EndBlock:
Ben Langmuir2c9af442014-04-10 17:57:43 +00004445 return Success;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004446 case llvm::BitstreamEntry::Record:
4447 // The interesting case.
4448 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004449 }
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004450
Guy Benyei11169dd2012-12-18 14:30:41 +00004451 // Read a record.
Chris Lattner0e6c9402013-01-20 02:38:54 +00004452 StringRef Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00004453 Record.clear();
Richard Smith03478d92014-10-23 22:12:14 +00004454 auto Kind = F.Stream.readRecord(Entry.ID, Record, &Blob);
4455
4456 if ((Kind == SUBMODULE_METADATA) != First) {
4457 Error("submodule metadata record should be at beginning of block");
4458 return Failure;
4459 }
4460 First = false;
4461
4462 // Submodule information is only valid if we have a current module.
4463 // FIXME: Should we error on these cases?
4464 if (!CurrentModule && Kind != SUBMODULE_METADATA &&
4465 Kind != SUBMODULE_DEFINITION)
4466 continue;
4467
4468 switch (Kind) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004469 default: // Default behavior: ignore.
4470 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004471
Richard Smith03478d92014-10-23 22:12:14 +00004472 case SUBMODULE_DEFINITION: {
Douglas Gregor8d932422013-03-20 03:59:18 +00004473 if (Record.size() < 8) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004474 Error("malformed module definition");
Ben Langmuir2c9af442014-04-10 17:57:43 +00004475 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00004476 }
Richard Smith03478d92014-10-23 22:12:14 +00004477
Chris Lattner0e6c9402013-01-20 02:38:54 +00004478 StringRef Name = Blob;
Richard Smith9bca2982014-03-08 00:03:56 +00004479 unsigned Idx = 0;
4480 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
4481 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
4482 bool IsFramework = Record[Idx++];
4483 bool IsExplicit = Record[Idx++];
4484 bool IsSystem = Record[Idx++];
4485 bool IsExternC = Record[Idx++];
4486 bool InferSubmodules = Record[Idx++];
4487 bool InferExplicitSubmodules = Record[Idx++];
4488 bool InferExportWildcard = Record[Idx++];
4489 bool ConfigMacrosExhaustive = Record[Idx++];
Douglas Gregor8d932422013-03-20 03:59:18 +00004490
Ben Langmuirbeee15e2014-04-14 18:00:01 +00004491 Module *ParentModule = nullptr;
Ben Langmuir9d6448b2014-08-09 00:57:23 +00004492 if (Parent)
Guy Benyei11169dd2012-12-18 14:30:41 +00004493 ParentModule = getSubmodule(Parent);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00004494
Guy Benyei11169dd2012-12-18 14:30:41 +00004495 // Retrieve this (sub)module from the module map, creating it if
4496 // necessary.
Ben Langmuir9d6448b2014-08-09 00:57:23 +00004497 CurrentModule = ModMap.findOrCreateModule(Name, ParentModule, IsFramework,
Guy Benyei11169dd2012-12-18 14:30:41 +00004498 IsExplicit).first;
Ben Langmuir9d6448b2014-08-09 00:57:23 +00004499
4500 // FIXME: set the definition loc for CurrentModule, or call
4501 // ModMap.setInferredModuleAllowedBy()
4502
Guy Benyei11169dd2012-12-18 14:30:41 +00004503 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
4504 if (GlobalIndex >= SubmodulesLoaded.size() ||
4505 SubmodulesLoaded[GlobalIndex]) {
4506 Error("too many submodules");
Ben Langmuir2c9af442014-04-10 17:57:43 +00004507 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00004508 }
Douglas Gregor8a114ab2013-02-06 22:40:31 +00004509
Douglas Gregor7029ce12013-03-19 00:28:20 +00004510 if (!ParentModule) {
4511 if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
4512 if (CurFile != F.File) {
4513 if (!Diags.isDiagnosticInFlight()) {
4514 Diag(diag::err_module_file_conflict)
4515 << CurrentModule->getTopLevelModuleName()
4516 << CurFile->getName()
4517 << F.File->getName();
4518 }
Ben Langmuir2c9af442014-04-10 17:57:43 +00004519 return Failure;
Douglas Gregor8a114ab2013-02-06 22:40:31 +00004520 }
Douglas Gregor8a114ab2013-02-06 22:40:31 +00004521 }
Douglas Gregor7029ce12013-03-19 00:28:20 +00004522
4523 CurrentModule->setASTFile(F.File);
Douglas Gregor8a114ab2013-02-06 22:40:31 +00004524 }
Ben Langmuirbeee15e2014-04-14 18:00:01 +00004525
Adrian Prantl15bcf702015-06-30 17:39:43 +00004526 CurrentModule->Signature = F.Signature;
Guy Benyei11169dd2012-12-18 14:30:41 +00004527 CurrentModule->IsFromModuleFile = true;
4528 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
Richard Smith9bca2982014-03-08 00:03:56 +00004529 CurrentModule->IsExternC = IsExternC;
Guy Benyei11169dd2012-12-18 14:30:41 +00004530 CurrentModule->InferSubmodules = InferSubmodules;
4531 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
4532 CurrentModule->InferExportWildcard = InferExportWildcard;
Douglas Gregor8d932422013-03-20 03:59:18 +00004533 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
Guy Benyei11169dd2012-12-18 14:30:41 +00004534 if (DeserializationListener)
4535 DeserializationListener->ModuleRead(GlobalID, CurrentModule);
4536
4537 SubmodulesLoaded[GlobalIndex] = CurrentModule;
Douglas Gregor6ddfca92013-01-14 17:21:00 +00004538
Richard Smith8a3e39a2016-03-28 21:31:09 +00004539 // Clear out data that will be replaced by what is in the module file.
Douglas Gregor6ddfca92013-01-14 17:21:00 +00004540 CurrentModule->LinkLibraries.clear();
Douglas Gregor8d932422013-03-20 03:59:18 +00004541 CurrentModule->ConfigMacros.clear();
Douglas Gregorfb912652013-03-20 21:10:35 +00004542 CurrentModule->UnresolvedConflicts.clear();
4543 CurrentModule->Conflicts.clear();
Richard Smith8a3e39a2016-03-28 21:31:09 +00004544
4545 // The module is available unless it's missing a requirement; relevant
4546 // requirements will be (re-)added by SUBMODULE_REQUIRES records.
4547 // Missing headers that were present when the module was built do not
4548 // make it unavailable -- if we got this far, this must be an explicitly
4549 // imported module file.
4550 CurrentModule->Requirements.clear();
4551 CurrentModule->MissingHeaders.clear();
4552 CurrentModule->IsMissingRequirement =
4553 ParentModule && ParentModule->IsMissingRequirement;
4554 CurrentModule->IsAvailable = !CurrentModule->IsMissingRequirement;
Guy Benyei11169dd2012-12-18 14:30:41 +00004555 break;
4556 }
Richard Smith8a3e39a2016-03-28 21:31:09 +00004557
Guy Benyei11169dd2012-12-18 14:30:41 +00004558 case SUBMODULE_UMBRELLA_HEADER: {
Richard Smith2b63d152015-05-16 02:28:53 +00004559 std::string Filename = Blob;
4560 ResolveImportedPath(F, Filename);
4561 if (auto *Umbrella = PP.getFileManager().getFile(Filename)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004562 if (!CurrentModule->getUmbrellaHeader())
Richard Smith2b63d152015-05-16 02:28:53 +00004563 ModMap.setUmbrellaHeader(CurrentModule, Umbrella, Blob);
4564 else if (CurrentModule->getUmbrellaHeader().Entry != Umbrella) {
Ben Langmuirbc35fbe2015-02-20 21:46:39 +00004565 // This can be a spurious difference caused by changing the VFS to
4566 // point to a different copy of the file, and it is too late to
4567 // to rebuild safely.
4568 // FIXME: If we wrote the virtual paths instead of the 'real' paths,
4569 // after input file validation only real problems would remain and we
4570 // could just error. For now, assume it's okay.
4571 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004572 }
4573 }
4574 break;
4575 }
4576
Richard Smith202210b2014-10-24 20:23:01 +00004577 case SUBMODULE_HEADER:
4578 case SUBMODULE_EXCLUDED_HEADER:
4579 case SUBMODULE_PRIVATE_HEADER:
4580 // We lazily associate headers with their modules via the HeaderInfo table.
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00004581 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
4582 // of complete filenames or remove it entirely.
Richard Smith202210b2014-10-24 20:23:01 +00004583 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004584
Richard Smith202210b2014-10-24 20:23:01 +00004585 case SUBMODULE_TEXTUAL_HEADER:
4586 case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
4587 // FIXME: Textual headers are not marked in the HeaderInfo table. Load
4588 // them here.
4589 break;
Lawrence Crowlb53e5482013-06-20 21:14:14 +00004590
Guy Benyei11169dd2012-12-18 14:30:41 +00004591 case SUBMODULE_TOPHEADER: {
Argyrios Kyrtzidis3c5305c2013-03-13 21:13:43 +00004592 CurrentModule->addTopHeaderFilename(Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +00004593 break;
4594 }
4595
4596 case SUBMODULE_UMBRELLA_DIR: {
Richard Smith2b63d152015-05-16 02:28:53 +00004597 std::string Dirname = Blob;
4598 ResolveImportedPath(F, Dirname);
4599 if (auto *Umbrella = PP.getFileManager().getDirectory(Dirname)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004600 if (!CurrentModule->getUmbrellaDir())
Richard Smith2b63d152015-05-16 02:28:53 +00004601 ModMap.setUmbrellaDir(CurrentModule, Umbrella, Blob);
4602 else if (CurrentModule->getUmbrellaDir().Entry != Umbrella) {
Ben Langmuir2c9af442014-04-10 17:57:43 +00004603 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
4604 Error("mismatched umbrella directories in submodule");
4605 return OutOfDate;
Guy Benyei11169dd2012-12-18 14:30:41 +00004606 }
4607 }
4608 break;
4609 }
4610
4611 case SUBMODULE_METADATA: {
Guy Benyei11169dd2012-12-18 14:30:41 +00004612 F.BaseSubmoduleID = getTotalNumSubmodules();
4613 F.LocalNumSubmodules = Record[0];
4614 unsigned LocalBaseSubmoduleID = Record[1];
4615 if (F.LocalNumSubmodules > 0) {
4616 // Introduce the global -> local mapping for submodules within this
4617 // module.
4618 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
4619
4620 // Introduce the local -> global mapping for submodules within this
4621 // module.
4622 F.SubmoduleRemap.insertOrReplace(
4623 std::make_pair(LocalBaseSubmoduleID,
4624 F.BaseSubmoduleID - LocalBaseSubmoduleID));
Ben Langmuirfe971d92014-08-16 04:54:18 +00004625
Ben Langmuir52ca6782014-10-20 16:27:32 +00004626 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
4627 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004628 break;
4629 }
4630
4631 case SUBMODULE_IMPORTS: {
Guy Benyei11169dd2012-12-18 14:30:41 +00004632 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
Douglas Gregorfb912652013-03-20 21:10:35 +00004633 UnresolvedModuleRef Unresolved;
Guy Benyei11169dd2012-12-18 14:30:41 +00004634 Unresolved.File = &F;
4635 Unresolved.Mod = CurrentModule;
4636 Unresolved.ID = Record[Idx];
Douglas Gregorfb912652013-03-20 21:10:35 +00004637 Unresolved.Kind = UnresolvedModuleRef::Import;
Guy Benyei11169dd2012-12-18 14:30:41 +00004638 Unresolved.IsWildcard = false;
Douglas Gregorfb912652013-03-20 21:10:35 +00004639 UnresolvedModuleRefs.push_back(Unresolved);
Guy Benyei11169dd2012-12-18 14:30:41 +00004640 }
4641 break;
4642 }
4643
4644 case SUBMODULE_EXPORTS: {
Guy Benyei11169dd2012-12-18 14:30:41 +00004645 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
Douglas Gregorfb912652013-03-20 21:10:35 +00004646 UnresolvedModuleRef Unresolved;
Guy Benyei11169dd2012-12-18 14:30:41 +00004647 Unresolved.File = &F;
4648 Unresolved.Mod = CurrentModule;
4649 Unresolved.ID = Record[Idx];
Douglas Gregorfb912652013-03-20 21:10:35 +00004650 Unresolved.Kind = UnresolvedModuleRef::Export;
Guy Benyei11169dd2012-12-18 14:30:41 +00004651 Unresolved.IsWildcard = Record[Idx + 1];
Douglas Gregorfb912652013-03-20 21:10:35 +00004652 UnresolvedModuleRefs.push_back(Unresolved);
Guy Benyei11169dd2012-12-18 14:30:41 +00004653 }
4654
4655 // Once we've loaded the set of exports, there's no reason to keep
4656 // the parsed, unresolved exports around.
4657 CurrentModule->UnresolvedExports.clear();
4658 break;
4659 }
4660 case SUBMODULE_REQUIRES: {
Richard Smitha3feee22013-10-28 22:18:19 +00004661 CurrentModule->addRequirement(Blob, Record[0], Context.getLangOpts(),
Guy Benyei11169dd2012-12-18 14:30:41 +00004662 Context.getTargetInfo());
4663 break;
4664 }
Douglas Gregor6ddfca92013-01-14 17:21:00 +00004665
4666 case SUBMODULE_LINK_LIBRARY:
Douglas Gregor6ddfca92013-01-14 17:21:00 +00004667 CurrentModule->LinkLibraries.push_back(
Chris Lattner0e6c9402013-01-20 02:38:54 +00004668 Module::LinkLibrary(Blob, Record[0]));
Douglas Gregor6ddfca92013-01-14 17:21:00 +00004669 break;
Douglas Gregor35b13ec2013-03-20 00:22:05 +00004670
4671 case SUBMODULE_CONFIG_MACRO:
Douglas Gregor35b13ec2013-03-20 00:22:05 +00004672 CurrentModule->ConfigMacros.push_back(Blob.str());
4673 break;
Douglas Gregorfb912652013-03-20 21:10:35 +00004674
4675 case SUBMODULE_CONFLICT: {
Douglas Gregorfb912652013-03-20 21:10:35 +00004676 UnresolvedModuleRef Unresolved;
4677 Unresolved.File = &F;
4678 Unresolved.Mod = CurrentModule;
4679 Unresolved.ID = Record[0];
4680 Unresolved.Kind = UnresolvedModuleRef::Conflict;
4681 Unresolved.IsWildcard = false;
4682 Unresolved.String = Blob;
4683 UnresolvedModuleRefs.push_back(Unresolved);
4684 break;
4685 }
Richard Smithdc1f0422016-07-20 19:10:16 +00004686
4687 case SUBMODULE_INITIALIZERS:
4688 SmallVector<uint32_t, 16> Inits;
4689 for (auto &ID : Record)
4690 Inits.push_back(getGlobalDeclID(F, ID));
4691 Context.addLazyModuleInitializers(CurrentModule, Inits);
4692 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004693 }
4694 }
4695}
4696
4697/// \brief Parse the record that corresponds to a LangOptions data
4698/// structure.
4699///
4700/// This routine parses the language options from the AST file and then gives
4701/// them to the AST listener if one is set.
4702///
4703/// \returns true if the listener deems the file unacceptable, false otherwise.
4704bool ASTReader::ParseLanguageOptions(const RecordData &Record,
4705 bool Complain,
Richard Smith1e2cf0d2014-10-31 02:28:58 +00004706 ASTReaderListener &Listener,
4707 bool AllowCompatibleDifferences) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004708 LangOptions LangOpts;
4709 unsigned Idx = 0;
4710#define LANGOPT(Name, Bits, Default, Description) \
4711 LangOpts.Name = Record[Idx++];
4712#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
4713 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
4714#include "clang/Basic/LangOptions.def"
Alexey Samsonovedf99a92014-11-07 22:29:38 +00004715#define SANITIZER(NAME, ID) \
4716 LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
Will Dietzf54319c2013-01-18 11:30:38 +00004717#include "clang/Basic/Sanitizers.def"
Guy Benyei11169dd2012-12-18 14:30:41 +00004718
Ben Langmuircd98cb72015-06-23 18:20:18 +00004719 for (unsigned N = Record[Idx++]; N; --N)
4720 LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx));
4721
Guy Benyei11169dd2012-12-18 14:30:41 +00004722 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
4723 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
4724 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00004725
Ben Langmuird4a667a2015-06-23 18:20:23 +00004726 LangOpts.CurrentModule = ReadString(Record, Idx);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00004727
4728 // Comment options.
4729 for (unsigned N = Record[Idx++]; N; --N) {
4730 LangOpts.CommentOpts.BlockCommandNames.push_back(
4731 ReadString(Record, Idx));
4732 }
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00004733 LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00004734
Samuel Antaoee8fb302016-01-06 13:42:12 +00004735 // OpenMP offloading options.
4736 for (unsigned N = Record[Idx++]; N; --N) {
4737 LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx)));
4738 }
4739
4740 LangOpts.OMPHostIRFile = ReadString(Record, Idx);
4741
Richard Smith1e2cf0d2014-10-31 02:28:58 +00004742 return Listener.ReadLanguageOptions(LangOpts, Complain,
4743 AllowCompatibleDifferences);
Guy Benyei11169dd2012-12-18 14:30:41 +00004744}
4745
Chandler Carruth0d745bc2015-03-14 04:47:43 +00004746bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain,
4747 ASTReaderListener &Listener,
4748 bool AllowCompatibleDifferences) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004749 unsigned Idx = 0;
4750 TargetOptions TargetOpts;
4751 TargetOpts.Triple = ReadString(Record, Idx);
4752 TargetOpts.CPU = ReadString(Record, Idx);
4753 TargetOpts.ABI = ReadString(Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00004754 for (unsigned N = Record[Idx++]; N; --N) {
4755 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
4756 }
4757 for (unsigned N = Record[Idx++]; N; --N) {
4758 TargetOpts.Features.push_back(ReadString(Record, Idx));
4759 }
4760
Chandler Carruth0d745bc2015-03-14 04:47:43 +00004761 return Listener.ReadTargetOptions(TargetOpts, Complain,
4762 AllowCompatibleDifferences);
Guy Benyei11169dd2012-12-18 14:30:41 +00004763}
4764
4765bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
4766 ASTReaderListener &Listener) {
Ben Langmuirb92de022014-04-29 16:25:26 +00004767 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions);
Guy Benyei11169dd2012-12-18 14:30:41 +00004768 unsigned Idx = 0;
Ben Langmuirb92de022014-04-29 16:25:26 +00004769#define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00004770#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
Ben Langmuirb92de022014-04-29 16:25:26 +00004771 DiagOpts->set##Name(static_cast<Type>(Record[Idx++]));
Guy Benyei11169dd2012-12-18 14:30:41 +00004772#include "clang/Basic/DiagnosticOptions.def"
4773
Richard Smith3be1cb22014-08-07 00:24:21 +00004774 for (unsigned N = Record[Idx++]; N; --N)
Ben Langmuirb92de022014-04-29 16:25:26 +00004775 DiagOpts->Warnings.push_back(ReadString(Record, Idx));
Richard Smith3be1cb22014-08-07 00:24:21 +00004776 for (unsigned N = Record[Idx++]; N; --N)
4777 DiagOpts->Remarks.push_back(ReadString(Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00004778
4779 return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
4780}
4781
4782bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
4783 ASTReaderListener &Listener) {
4784 FileSystemOptions FSOpts;
4785 unsigned Idx = 0;
4786 FSOpts.WorkingDir = ReadString(Record, Idx);
4787 return Listener.ReadFileSystemOptions(FSOpts, Complain);
4788}
4789
4790bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
4791 bool Complain,
4792 ASTReaderListener &Listener) {
4793 HeaderSearchOptions HSOpts;
4794 unsigned Idx = 0;
4795 HSOpts.Sysroot = ReadString(Record, Idx);
4796
4797 // Include entries.
4798 for (unsigned N = Record[Idx++]; N; --N) {
4799 std::string Path = ReadString(Record, Idx);
4800 frontend::IncludeDirGroup Group
4801 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
Guy Benyei11169dd2012-12-18 14:30:41 +00004802 bool IsFramework = Record[Idx++];
4803 bool IgnoreSysRoot = Record[Idx++];
Benjamin Kramer3204b152015-05-29 19:42:19 +00004804 HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework,
4805 IgnoreSysRoot);
Guy Benyei11169dd2012-12-18 14:30:41 +00004806 }
4807
4808 // System header prefixes.
4809 for (unsigned N = Record[Idx++]; N; --N) {
4810 std::string Prefix = ReadString(Record, Idx);
4811 bool IsSystemHeader = Record[Idx++];
Benjamin Kramer3204b152015-05-29 19:42:19 +00004812 HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader);
Guy Benyei11169dd2012-12-18 14:30:41 +00004813 }
4814
4815 HSOpts.ResourceDir = ReadString(Record, Idx);
4816 HSOpts.ModuleCachePath = ReadString(Record, Idx);
Argyrios Kyrtzidis1594c152014-03-03 08:12:05 +00004817 HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00004818 HSOpts.DisableModuleHash = Record[Idx++];
4819 HSOpts.UseBuiltinIncludes = Record[Idx++];
4820 HSOpts.UseStandardSystemIncludes = Record[Idx++];
4821 HSOpts.UseStandardCXXIncludes = Record[Idx++];
4822 HSOpts.UseLibcxx = Record[Idx++];
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00004823 std::string SpecificModuleCachePath = ReadString(Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00004824
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00004825 return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
4826 Complain);
Guy Benyei11169dd2012-12-18 14:30:41 +00004827}
4828
4829bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
4830 bool Complain,
4831 ASTReaderListener &Listener,
4832 std::string &SuggestedPredefines) {
4833 PreprocessorOptions PPOpts;
4834 unsigned Idx = 0;
4835
4836 // Macro definitions/undefs
4837 for (unsigned N = Record[Idx++]; N; --N) {
4838 std::string Macro = ReadString(Record, Idx);
4839 bool IsUndef = Record[Idx++];
4840 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
4841 }
4842
4843 // Includes
4844 for (unsigned N = Record[Idx++]; N; --N) {
4845 PPOpts.Includes.push_back(ReadString(Record, Idx));
4846 }
4847
4848 // Macro Includes
4849 for (unsigned N = Record[Idx++]; N; --N) {
4850 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
4851 }
4852
4853 PPOpts.UsePredefines = Record[Idx++];
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00004854 PPOpts.DetailedRecord = Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00004855 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
4856 PPOpts.ImplicitPTHInclude = ReadString(Record, Idx);
4857 PPOpts.ObjCXXARCStandardLibrary =
4858 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
4859 SuggestedPredefines.clear();
4860 return Listener.ReadPreprocessorOptions(PPOpts, Complain,
4861 SuggestedPredefines);
4862}
4863
4864std::pair<ModuleFile *, unsigned>
4865ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
4866 GlobalPreprocessedEntityMapType::iterator
4867 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
4868 assert(I != GlobalPreprocessedEntityMap.end() &&
4869 "Corrupted global preprocessed entity map");
4870 ModuleFile *M = I->second;
4871 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
4872 return std::make_pair(M, LocalIndex);
4873}
4874
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00004875llvm::iterator_range<PreprocessingRecord::iterator>
Guy Benyei11169dd2012-12-18 14:30:41 +00004876ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
4877 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
4878 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
4879 Mod.NumPreprocessedEntities);
4880
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00004881 return llvm::make_range(PreprocessingRecord::iterator(),
4882 PreprocessingRecord::iterator());
Guy Benyei11169dd2012-12-18 14:30:41 +00004883}
4884
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00004885llvm::iterator_range<ASTReader::ModuleDeclIterator>
Guy Benyei11169dd2012-12-18 14:30:41 +00004886ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00004887 return llvm::make_range(
4888 ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
4889 ModuleDeclIterator(this, &Mod,
4890 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
Guy Benyei11169dd2012-12-18 14:30:41 +00004891}
4892
4893PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
4894 PreprocessedEntityID PPID = Index+1;
4895 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
4896 ModuleFile &M = *PPInfo.first;
4897 unsigned LocalIndex = PPInfo.second;
4898 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
4899
Guy Benyei11169dd2012-12-18 14:30:41 +00004900 if (!PP.getPreprocessingRecord()) {
4901 Error("no preprocessing record");
Craig Toppera13603a2014-05-22 05:54:18 +00004902 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00004903 }
4904
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004905 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
4906 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
4907
4908 llvm::BitstreamEntry Entry =
4909 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
4910 if (Entry.Kind != llvm::BitstreamEntry::Record)
Craig Toppera13603a2014-05-22 05:54:18 +00004911 return nullptr;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004912
Guy Benyei11169dd2012-12-18 14:30:41 +00004913 // Read the record.
Richard Smithcb34bd32016-03-27 07:28:06 +00004914 SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()),
4915 TranslateSourceLocation(M, PPOffs.getEnd()));
Guy Benyei11169dd2012-12-18 14:30:41 +00004916 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
Chris Lattner0e6c9402013-01-20 02:38:54 +00004917 StringRef Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00004918 RecordData Record;
4919 PreprocessorDetailRecordTypes RecType =
Chris Lattner0e6c9402013-01-20 02:38:54 +00004920 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.readRecord(
4921 Entry.ID, Record, &Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +00004922 switch (RecType) {
4923 case PPD_MACRO_EXPANSION: {
4924 bool isBuiltin = Record[0];
Craig Toppera13603a2014-05-22 05:54:18 +00004925 IdentifierInfo *Name = nullptr;
Richard Smith66a81862015-05-04 02:25:31 +00004926 MacroDefinitionRecord *Def = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00004927 if (isBuiltin)
4928 Name = getLocalIdentifier(M, Record[1]);
4929 else {
Richard Smith66a81862015-05-04 02:25:31 +00004930 PreprocessedEntityID GlobalID =
4931 getGlobalPreprocessedEntityID(M, Record[1]);
4932 Def = cast<MacroDefinitionRecord>(
4933 PPRec.getLoadedPreprocessedEntity(GlobalID - 1));
Guy Benyei11169dd2012-12-18 14:30:41 +00004934 }
4935
4936 MacroExpansion *ME;
4937 if (isBuiltin)
4938 ME = new (PPRec) MacroExpansion(Name, Range);
4939 else
4940 ME = new (PPRec) MacroExpansion(Def, Range);
4941
4942 return ME;
4943 }
4944
4945 case PPD_MACRO_DEFINITION: {
4946 // Decode the identifier info and then check again; if the macro is
4947 // still defined and associated with the identifier,
4948 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
Richard Smith66a81862015-05-04 02:25:31 +00004949 MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
Guy Benyei11169dd2012-12-18 14:30:41 +00004950
4951 if (DeserializationListener)
4952 DeserializationListener->MacroDefinitionRead(PPID, MD);
4953
4954 return MD;
4955 }
4956
4957 case PPD_INCLUSION_DIRECTIVE: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00004958 const char *FullFileNameStart = Blob.data() + Record[0];
4959 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
Craig Toppera13603a2014-05-22 05:54:18 +00004960 const FileEntry *File = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00004961 if (!FullFileName.empty())
4962 File = PP.getFileManager().getFile(FullFileName);
4963
4964 // FIXME: Stable encoding
4965 InclusionDirective::InclusionKind Kind
4966 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
4967 InclusionDirective *ID
4968 = new (PPRec) InclusionDirective(PPRec, Kind,
Chris Lattner0e6c9402013-01-20 02:38:54 +00004969 StringRef(Blob.data(), Record[0]),
Guy Benyei11169dd2012-12-18 14:30:41 +00004970 Record[1], Record[3],
4971 File,
4972 Range);
4973 return ID;
4974 }
4975 }
4976
4977 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
4978}
4979
4980/// \brief \arg SLocMapI points at a chunk of a module that contains no
4981/// preprocessed entities or the entities it contains are not the ones we are
4982/// looking for. Find the next module that contains entities and return the ID
4983/// of the first entry.
4984PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
4985 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
4986 ++SLocMapI;
4987 for (GlobalSLocOffsetMapType::const_iterator
4988 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
4989 ModuleFile &M = *SLocMapI->second;
4990 if (M.NumPreprocessedEntities)
4991 return M.BasePreprocessedEntityID;
4992 }
4993
4994 return getTotalNumPreprocessedEntities();
4995}
4996
4997namespace {
4998
Guy Benyei11169dd2012-12-18 14:30:41 +00004999struct PPEntityComp {
5000 const ASTReader &Reader;
5001 ModuleFile &M;
5002
5003 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) { }
5004
5005 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
5006 SourceLocation LHS = getLoc(L);
5007 SourceLocation RHS = getLoc(R);
5008 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5009 }
5010
5011 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
5012 SourceLocation LHS = getLoc(L);
5013 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5014 }
5015
5016 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
5017 SourceLocation RHS = getLoc(R);
5018 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5019 }
5020
5021 SourceLocation getLoc(const PPEntityOffset &PPE) const {
Richard Smithb22a1d12016-03-27 20:13:24 +00005022 return Reader.TranslateSourceLocation(M, PPE.getBegin());
Guy Benyei11169dd2012-12-18 14:30:41 +00005023 }
5024};
5025
Alexander Kornienkoab9db512015-06-22 23:07:51 +00005026}
Guy Benyei11169dd2012-12-18 14:30:41 +00005027
Alp Toker2e9ce4c2014-05-16 18:59:21 +00005028PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
5029 bool EndsAfter) const {
5030 if (SourceMgr.isLocalSourceLocation(Loc))
Guy Benyei11169dd2012-12-18 14:30:41 +00005031 return getTotalNumPreprocessedEntities();
5032
Alp Toker2e9ce4c2014-05-16 18:59:21 +00005033 GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
5034 SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
Guy Benyei11169dd2012-12-18 14:30:41 +00005035 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
5036 "Corrupted global sloc offset map");
5037
5038 if (SLocMapI->second->NumPreprocessedEntities == 0)
5039 return findNextPreprocessedEntity(SLocMapI);
5040
5041 ModuleFile &M = *SLocMapI->second;
5042 typedef const PPEntityOffset *pp_iterator;
5043 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
5044 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
5045
5046 size_t Count = M.NumPreprocessedEntities;
5047 size_t Half;
5048 pp_iterator First = pp_begin;
5049 pp_iterator PPI;
5050
Alp Toker2e9ce4c2014-05-16 18:59:21 +00005051 if (EndsAfter) {
5052 PPI = std::upper_bound(pp_begin, pp_end, Loc,
Richard Smithb22a1d12016-03-27 20:13:24 +00005053 PPEntityComp(*this, M));
Alp Toker2e9ce4c2014-05-16 18:59:21 +00005054 } else {
5055 // Do a binary search manually instead of using std::lower_bound because
5056 // The end locations of entities may be unordered (when a macro expansion
5057 // is inside another macro argument), but for this case it is not important
5058 // whether we get the first macro expansion or its containing macro.
5059 while (Count > 0) {
5060 Half = Count / 2;
5061 PPI = First;
5062 std::advance(PPI, Half);
Richard Smithb22a1d12016-03-27 20:13:24 +00005063 if (SourceMgr.isBeforeInTranslationUnit(
5064 TranslateSourceLocation(M, PPI->getEnd()), Loc)) {
Alp Toker2e9ce4c2014-05-16 18:59:21 +00005065 First = PPI;
5066 ++First;
5067 Count = Count - Half - 1;
5068 } else
5069 Count = Half;
5070 }
Guy Benyei11169dd2012-12-18 14:30:41 +00005071 }
5072
5073 if (PPI == pp_end)
5074 return findNextPreprocessedEntity(SLocMapI);
5075
5076 return M.BasePreprocessedEntityID + (PPI - pp_begin);
5077}
5078
Guy Benyei11169dd2012-12-18 14:30:41 +00005079/// \brief Returns a pair of [Begin, End) indices of preallocated
5080/// preprocessed entities that \arg Range encompasses.
5081std::pair<unsigned, unsigned>
5082 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
5083 if (Range.isInvalid())
5084 return std::make_pair(0,0);
5085 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
5086
Alp Toker2e9ce4c2014-05-16 18:59:21 +00005087 PreprocessedEntityID BeginID =
5088 findPreprocessedEntity(Range.getBegin(), false);
5089 PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true);
Guy Benyei11169dd2012-12-18 14:30:41 +00005090 return std::make_pair(BeginID, EndID);
5091}
5092
5093/// \brief Optionally returns true or false if the preallocated preprocessed
5094/// entity with index \arg Index came from file \arg FID.
David Blaikie05785d12013-02-20 22:23:23 +00005095Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
Guy Benyei11169dd2012-12-18 14:30:41 +00005096 FileID FID) {
5097 if (FID.isInvalid())
5098 return false;
5099
5100 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
5101 ModuleFile &M = *PPInfo.first;
5102 unsigned LocalIndex = PPInfo.second;
5103 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
5104
Richard Smithcb34bd32016-03-27 07:28:06 +00005105 SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin());
Guy Benyei11169dd2012-12-18 14:30:41 +00005106 if (Loc.isInvalid())
5107 return false;
5108
5109 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
5110 return true;
5111 else
5112 return false;
5113}
5114
5115namespace {
5116 /// \brief Visitor used to search for information about a header file.
5117 class HeaderFileInfoVisitor {
Guy Benyei11169dd2012-12-18 14:30:41 +00005118 const FileEntry *FE;
5119
David Blaikie05785d12013-02-20 22:23:23 +00005120 Optional<HeaderFileInfo> HFI;
Guy Benyei11169dd2012-12-18 14:30:41 +00005121
5122 public:
Argyrios Kyrtzidis61a38962013-03-06 18:12:44 +00005123 explicit HeaderFileInfoVisitor(const FileEntry *FE)
5124 : FE(FE) { }
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00005125
5126 bool operator()(ModuleFile &M) {
Guy Benyei11169dd2012-12-18 14:30:41 +00005127 HeaderFileInfoLookupTable *Table
5128 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
5129 if (!Table)
5130 return false;
5131
5132 // Look in the on-disk hash table for an entry for this file name.
Richard Smithbdf2d932015-07-30 03:37:16 +00005133 HeaderFileInfoLookupTable::iterator Pos = Table->find(FE);
Guy Benyei11169dd2012-12-18 14:30:41 +00005134 if (Pos == Table->end())
5135 return false;
5136
Richard Smithbdf2d932015-07-30 03:37:16 +00005137 HFI = *Pos;
Guy Benyei11169dd2012-12-18 14:30:41 +00005138 return true;
5139 }
5140
David Blaikie05785d12013-02-20 22:23:23 +00005141 Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
Guy Benyei11169dd2012-12-18 14:30:41 +00005142 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +00005143}
Guy Benyei11169dd2012-12-18 14:30:41 +00005144
5145HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
Argyrios Kyrtzidis61a38962013-03-06 18:12:44 +00005146 HeaderFileInfoVisitor Visitor(FE);
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00005147 ModuleMgr.visit(Visitor);
Argyrios Kyrtzidis1054bbf2013-05-08 23:46:55 +00005148 if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
Guy Benyei11169dd2012-12-18 14:30:41 +00005149 return *HFI;
Guy Benyei11169dd2012-12-18 14:30:41 +00005150
5151 return HeaderFileInfo();
5152}
5153
5154void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
5155 // FIXME: Make it work properly with modules.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00005156 SmallVector<DiagnosticsEngine::DiagState *, 32> DiagStates;
Guy Benyei11169dd2012-12-18 14:30:41 +00005157 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
5158 ModuleFile &F = *(*I);
5159 unsigned Idx = 0;
5160 DiagStates.clear();
5161 assert(!Diag.DiagStates.empty());
5162 DiagStates.push_back(&Diag.DiagStates.front()); // the command-line one.
5163 while (Idx < F.PragmaDiagMappings.size()) {
5164 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
5165 unsigned DiagStateID = F.PragmaDiagMappings[Idx++];
5166 if (DiagStateID != 0) {
5167 Diag.DiagStatePoints.push_back(
5168 DiagnosticsEngine::DiagStatePoint(DiagStates[DiagStateID-1],
5169 FullSourceLoc(Loc, SourceMgr)));
5170 continue;
5171 }
5172
5173 assert(DiagStateID == 0);
5174 // A new DiagState was created here.
5175 Diag.DiagStates.push_back(*Diag.GetCurDiagState());
5176 DiagnosticsEngine::DiagState *NewState = &Diag.DiagStates.back();
5177 DiagStates.push_back(NewState);
5178 Diag.DiagStatePoints.push_back(
5179 DiagnosticsEngine::DiagStatePoint(NewState,
5180 FullSourceLoc(Loc, SourceMgr)));
5181 while (1) {
5182 assert(Idx < F.PragmaDiagMappings.size() &&
5183 "Invalid data, didn't find '-1' marking end of diag/map pairs");
5184 if (Idx >= F.PragmaDiagMappings.size()) {
5185 break; // Something is messed up but at least avoid infinite loop in
5186 // release build.
5187 }
5188 unsigned DiagID = F.PragmaDiagMappings[Idx++];
5189 if (DiagID == (unsigned)-1) {
5190 break; // no more diag/map pairs for this location.
5191 }
Alp Tokerc726c362014-06-10 09:31:37 +00005192 diag::Severity Map = (diag::Severity)F.PragmaDiagMappings[Idx++];
5193 DiagnosticMapping Mapping = Diag.makeUserMapping(Map, Loc);
5194 Diag.GetCurDiagState()->setMapping(DiagID, Mapping);
Guy Benyei11169dd2012-12-18 14:30:41 +00005195 }
5196 }
5197 }
5198}
5199
5200/// \brief Get the correct cursor and offset for loading a type.
5201ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
5202 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
5203 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
5204 ModuleFile *M = I->second;
5205 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
5206}
5207
5208/// \brief Read and return the type with the given index..
5209///
5210/// The index is the type ID, shifted and minus the number of predefs. This
5211/// routine actually reads the record corresponding to the type at the given
5212/// location. It is a helper routine for GetType, which deals with reading type
5213/// IDs.
5214QualType ASTReader::readTypeRecord(unsigned Index) {
5215 RecordLocation Loc = TypeCursorForIndex(Index);
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005216 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00005217
5218 // Keep track of where we are in the stream, then jump back there
5219 // after reading this type.
5220 SavedStreamPosition SavedPosition(DeclsCursor);
5221
5222 ReadingKindTracker ReadingKind(Read_Type, *this);
5223
5224 // Note that we are loading a type record.
5225 Deserializing AType(this);
5226
5227 unsigned Idx = 0;
5228 DeclsCursor.JumpToBit(Loc.Offset);
5229 RecordData Record;
5230 unsigned Code = DeclsCursor.ReadCode();
Chris Lattner0e6c9402013-01-20 02:38:54 +00005231 switch ((TypeCode)DeclsCursor.readRecord(Code, Record)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00005232 case TYPE_EXT_QUAL: {
5233 if (Record.size() != 2) {
5234 Error("Incorrect encoding of extended qualifier type");
5235 return QualType();
5236 }
5237 QualType Base = readType(*Loc.F, Record, Idx);
5238 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
5239 return Context.getQualifiedType(Base, Quals);
5240 }
5241
5242 case TYPE_COMPLEX: {
5243 if (Record.size() != 1) {
5244 Error("Incorrect encoding of complex type");
5245 return QualType();
5246 }
5247 QualType ElemType = readType(*Loc.F, Record, Idx);
5248 return Context.getComplexType(ElemType);
5249 }
5250
5251 case TYPE_POINTER: {
5252 if (Record.size() != 1) {
5253 Error("Incorrect encoding of pointer type");
5254 return QualType();
5255 }
5256 QualType PointeeType = readType(*Loc.F, Record, Idx);
5257 return Context.getPointerType(PointeeType);
5258 }
5259
Reid Kleckner8a365022013-06-24 17:51:48 +00005260 case TYPE_DECAYED: {
5261 if (Record.size() != 1) {
5262 Error("Incorrect encoding of decayed type");
5263 return QualType();
5264 }
5265 QualType OriginalType = readType(*Loc.F, Record, Idx);
5266 QualType DT = Context.getAdjustedParameterType(OriginalType);
5267 if (!isa<DecayedType>(DT))
5268 Error("Decayed type does not decay");
5269 return DT;
5270 }
5271
Reid Kleckner0503a872013-12-05 01:23:43 +00005272 case TYPE_ADJUSTED: {
5273 if (Record.size() != 2) {
5274 Error("Incorrect encoding of adjusted type");
5275 return QualType();
5276 }
5277 QualType OriginalTy = readType(*Loc.F, Record, Idx);
5278 QualType AdjustedTy = readType(*Loc.F, Record, Idx);
5279 return Context.getAdjustedType(OriginalTy, AdjustedTy);
5280 }
5281
Guy Benyei11169dd2012-12-18 14:30:41 +00005282 case TYPE_BLOCK_POINTER: {
5283 if (Record.size() != 1) {
5284 Error("Incorrect encoding of block pointer type");
5285 return QualType();
5286 }
5287 QualType PointeeType = readType(*Loc.F, Record, Idx);
5288 return Context.getBlockPointerType(PointeeType);
5289 }
5290
5291 case TYPE_LVALUE_REFERENCE: {
5292 if (Record.size() != 2) {
5293 Error("Incorrect encoding of lvalue reference type");
5294 return QualType();
5295 }
5296 QualType PointeeType = readType(*Loc.F, Record, Idx);
5297 return Context.getLValueReferenceType(PointeeType, Record[1]);
5298 }
5299
5300 case TYPE_RVALUE_REFERENCE: {
5301 if (Record.size() != 1) {
5302 Error("Incorrect encoding of rvalue reference type");
5303 return QualType();
5304 }
5305 QualType PointeeType = readType(*Loc.F, Record, Idx);
5306 return Context.getRValueReferenceType(PointeeType);
5307 }
5308
5309 case TYPE_MEMBER_POINTER: {
5310 if (Record.size() != 2) {
5311 Error("Incorrect encoding of member pointer type");
5312 return QualType();
5313 }
5314 QualType PointeeType = readType(*Loc.F, Record, Idx);
5315 QualType ClassType = readType(*Loc.F, Record, Idx);
5316 if (PointeeType.isNull() || ClassType.isNull())
5317 return QualType();
5318
5319 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
5320 }
5321
5322 case TYPE_CONSTANT_ARRAY: {
5323 QualType ElementType = readType(*Loc.F, Record, Idx);
5324 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
5325 unsigned IndexTypeQuals = Record[2];
5326 unsigned Idx = 3;
5327 llvm::APInt Size = ReadAPInt(Record, Idx);
5328 return Context.getConstantArrayType(ElementType, Size,
5329 ASM, IndexTypeQuals);
5330 }
5331
5332 case TYPE_INCOMPLETE_ARRAY: {
5333 QualType ElementType = readType(*Loc.F, Record, Idx);
5334 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
5335 unsigned IndexTypeQuals = Record[2];
5336 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
5337 }
5338
5339 case TYPE_VARIABLE_ARRAY: {
5340 QualType ElementType = readType(*Loc.F, Record, Idx);
5341 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
5342 unsigned IndexTypeQuals = Record[2];
5343 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
5344 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
5345 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
5346 ASM, IndexTypeQuals,
5347 SourceRange(LBLoc, RBLoc));
5348 }
5349
5350 case TYPE_VECTOR: {
5351 if (Record.size() != 3) {
5352 Error("incorrect encoding of vector type in AST file");
5353 return QualType();
5354 }
5355
5356 QualType ElementType = readType(*Loc.F, Record, Idx);
5357 unsigned NumElements = Record[1];
5358 unsigned VecKind = Record[2];
5359 return Context.getVectorType(ElementType, NumElements,
5360 (VectorType::VectorKind)VecKind);
5361 }
5362
5363 case TYPE_EXT_VECTOR: {
5364 if (Record.size() != 3) {
5365 Error("incorrect encoding of extended vector type in AST file");
5366 return QualType();
5367 }
5368
5369 QualType ElementType = readType(*Loc.F, Record, Idx);
5370 unsigned NumElements = Record[1];
5371 return Context.getExtVectorType(ElementType, NumElements);
5372 }
5373
5374 case TYPE_FUNCTION_NO_PROTO: {
5375 if (Record.size() != 6) {
5376 Error("incorrect encoding of no-proto function type");
5377 return QualType();
5378 }
5379 QualType ResultType = readType(*Loc.F, Record, Idx);
5380 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
5381 (CallingConv)Record[4], Record[5]);
5382 return Context.getFunctionNoProtoType(ResultType, Info);
5383 }
5384
5385 case TYPE_FUNCTION_PROTO: {
5386 QualType ResultType = readType(*Loc.F, Record, Idx);
5387
5388 FunctionProtoType::ExtProtoInfo EPI;
5389 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
5390 /*hasregparm*/ Record[2],
5391 /*regparm*/ Record[3],
5392 static_cast<CallingConv>(Record[4]),
5393 /*produces*/ Record[5]);
5394
5395 unsigned Idx = 6;
Guy Benyei11169dd2012-12-18 14:30:41 +00005396
5397 EPI.Variadic = Record[Idx++];
5398 EPI.HasTrailingReturn = Record[Idx++];
5399 EPI.TypeQuals = Record[Idx++];
5400 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
Richard Smith564417a2014-03-20 21:47:22 +00005401 SmallVector<QualType, 8> ExceptionStorage;
Richard Smith8acb4282014-07-31 21:57:55 +00005402 readExceptionSpec(*Loc.F, ExceptionStorage, EPI.ExceptionSpec, Record, Idx);
Richard Smith01b2cb42014-07-26 06:37:51 +00005403
5404 unsigned NumParams = Record[Idx++];
5405 SmallVector<QualType, 16> ParamTypes;
5406 for (unsigned I = 0; I != NumParams; ++I)
5407 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
5408
John McCall18afab72016-03-01 00:49:02 +00005409 SmallVector<FunctionProtoType::ExtParameterInfo, 4> ExtParameterInfos;
5410 if (Idx != Record.size()) {
5411 for (unsigned I = 0; I != NumParams; ++I)
5412 ExtParameterInfos.push_back(
5413 FunctionProtoType::ExtParameterInfo
5414 ::getFromOpaqueValue(Record[Idx++]));
5415 EPI.ExtParameterInfos = ExtParameterInfos.data();
5416 }
5417
5418 assert(Idx == Record.size());
5419
Jordan Rose5c382722013-03-08 21:51:21 +00005420 return Context.getFunctionType(ResultType, ParamTypes, EPI);
Guy Benyei11169dd2012-12-18 14:30:41 +00005421 }
5422
5423 case TYPE_UNRESOLVED_USING: {
5424 unsigned Idx = 0;
5425 return Context.getTypeDeclType(
5426 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
5427 }
5428
5429 case TYPE_TYPEDEF: {
5430 if (Record.size() != 2) {
5431 Error("incorrect encoding of typedef type");
5432 return QualType();
5433 }
5434 unsigned Idx = 0;
5435 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
5436 QualType Canonical = readType(*Loc.F, Record, Idx);
5437 if (!Canonical.isNull())
5438 Canonical = Context.getCanonicalType(Canonical);
5439 return Context.getTypedefType(Decl, Canonical);
5440 }
5441
5442 case TYPE_TYPEOF_EXPR:
5443 return Context.getTypeOfExprType(ReadExpr(*Loc.F));
5444
5445 case TYPE_TYPEOF: {
5446 if (Record.size() != 1) {
5447 Error("incorrect encoding of typeof(type) in AST file");
5448 return QualType();
5449 }
5450 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
5451 return Context.getTypeOfType(UnderlyingType);
5452 }
5453
5454 case TYPE_DECLTYPE: {
5455 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
5456 return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
5457 }
5458
5459 case TYPE_UNARY_TRANSFORM: {
5460 QualType BaseType = readType(*Loc.F, Record, Idx);
5461 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
5462 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
5463 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
5464 }
5465
Richard Smith74aeef52013-04-26 16:15:35 +00005466 case TYPE_AUTO: {
5467 QualType Deduced = readType(*Loc.F, Record, Idx);
Richard Smithe301ba22015-11-11 02:02:15 +00005468 AutoTypeKeyword Keyword = (AutoTypeKeyword)Record[Idx++];
Richard Smith27d807c2013-04-30 13:56:41 +00005469 bool IsDependent = Deduced.isNull() ? Record[Idx++] : false;
Richard Smithe301ba22015-11-11 02:02:15 +00005470 return Context.getAutoType(Deduced, Keyword, IsDependent);
Richard Smith74aeef52013-04-26 16:15:35 +00005471 }
Guy Benyei11169dd2012-12-18 14:30:41 +00005472
5473 case TYPE_RECORD: {
5474 if (Record.size() != 2) {
5475 Error("incorrect encoding of record type");
5476 return QualType();
5477 }
5478 unsigned Idx = 0;
5479 bool IsDependent = Record[Idx++];
5480 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
5481 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
5482 QualType T = Context.getRecordType(RD);
5483 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
5484 return T;
5485 }
5486
5487 case TYPE_ENUM: {
5488 if (Record.size() != 2) {
5489 Error("incorrect encoding of enum type");
5490 return QualType();
5491 }
5492 unsigned Idx = 0;
5493 bool IsDependent = Record[Idx++];
5494 QualType T
5495 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
5496 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
5497 return T;
5498 }
5499
5500 case TYPE_ATTRIBUTED: {
5501 if (Record.size() != 3) {
5502 Error("incorrect encoding of attributed type");
5503 return QualType();
5504 }
5505 QualType modifiedType = readType(*Loc.F, Record, Idx);
5506 QualType equivalentType = readType(*Loc.F, Record, Idx);
5507 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
5508 return Context.getAttributedType(kind, modifiedType, equivalentType);
5509 }
5510
5511 case TYPE_PAREN: {
5512 if (Record.size() != 1) {
5513 Error("incorrect encoding of paren type");
5514 return QualType();
5515 }
5516 QualType InnerType = readType(*Loc.F, Record, Idx);
5517 return Context.getParenType(InnerType);
5518 }
5519
5520 case TYPE_PACK_EXPANSION: {
5521 if (Record.size() != 2) {
5522 Error("incorrect encoding of pack expansion type");
5523 return QualType();
5524 }
5525 QualType Pattern = readType(*Loc.F, Record, Idx);
5526 if (Pattern.isNull())
5527 return QualType();
David Blaikie05785d12013-02-20 22:23:23 +00005528 Optional<unsigned> NumExpansions;
Guy Benyei11169dd2012-12-18 14:30:41 +00005529 if (Record[1])
5530 NumExpansions = Record[1] - 1;
5531 return Context.getPackExpansionType(Pattern, NumExpansions);
5532 }
5533
5534 case TYPE_ELABORATED: {
5535 unsigned Idx = 0;
5536 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
5537 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
5538 QualType NamedType = readType(*Loc.F, Record, Idx);
5539 return Context.getElaboratedType(Keyword, NNS, NamedType);
5540 }
5541
5542 case TYPE_OBJC_INTERFACE: {
5543 unsigned Idx = 0;
5544 ObjCInterfaceDecl *ItfD
5545 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
5546 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
5547 }
5548
5549 case TYPE_OBJC_OBJECT: {
5550 unsigned Idx = 0;
5551 QualType Base = readType(*Loc.F, Record, Idx);
Douglas Gregore9d95f12015-07-07 03:57:35 +00005552 unsigned NumTypeArgs = Record[Idx++];
5553 SmallVector<QualType, 4> TypeArgs;
5554 for (unsigned I = 0; I != NumTypeArgs; ++I)
5555 TypeArgs.push_back(readType(*Loc.F, Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00005556 unsigned NumProtos = Record[Idx++];
5557 SmallVector<ObjCProtocolDecl*, 4> Protos;
5558 for (unsigned I = 0; I != NumProtos; ++I)
5559 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
Douglas Gregorab209d82015-07-07 03:58:42 +00005560 bool IsKindOf = Record[Idx++];
5561 return Context.getObjCObjectType(Base, TypeArgs, Protos, IsKindOf);
Guy Benyei11169dd2012-12-18 14:30:41 +00005562 }
5563
5564 case TYPE_OBJC_OBJECT_POINTER: {
5565 unsigned Idx = 0;
5566 QualType Pointee = readType(*Loc.F, Record, Idx);
5567 return Context.getObjCObjectPointerType(Pointee);
5568 }
5569
5570 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
5571 unsigned Idx = 0;
5572 QualType Parm = readType(*Loc.F, Record, Idx);
5573 QualType Replacement = readType(*Loc.F, Record, Idx);
Stephan Tolksdorfe96f8b32014-03-15 10:23:27 +00005574 return Context.getSubstTemplateTypeParmType(
5575 cast<TemplateTypeParmType>(Parm),
5576 Context.getCanonicalType(Replacement));
Guy Benyei11169dd2012-12-18 14:30:41 +00005577 }
5578
5579 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
5580 unsigned Idx = 0;
5581 QualType Parm = readType(*Loc.F, Record, Idx);
5582 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
5583 return Context.getSubstTemplateTypeParmPackType(
5584 cast<TemplateTypeParmType>(Parm),
5585 ArgPack);
5586 }
5587
5588 case TYPE_INJECTED_CLASS_NAME: {
5589 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
5590 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
5591 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
5592 // for AST reading, too much interdependencies.
Richard Smith6377f8f2014-10-21 21:15:18 +00005593 const Type *T = nullptr;
5594 for (auto *DI = D; DI; DI = DI->getPreviousDecl()) {
5595 if (const Type *Existing = DI->getTypeForDecl()) {
5596 T = Existing;
5597 break;
5598 }
5599 }
5600 if (!T) {
Richard Smithf17fdbd2014-04-24 02:25:27 +00005601 T = new (Context, TypeAlignment) InjectedClassNameType(D, TST);
Richard Smith6377f8f2014-10-21 21:15:18 +00005602 for (auto *DI = D; DI; DI = DI->getPreviousDecl())
5603 DI->setTypeForDecl(T);
5604 }
Richard Smithf17fdbd2014-04-24 02:25:27 +00005605 return QualType(T, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00005606 }
5607
5608 case TYPE_TEMPLATE_TYPE_PARM: {
5609 unsigned Idx = 0;
5610 unsigned Depth = Record[Idx++];
5611 unsigned Index = Record[Idx++];
5612 bool Pack = Record[Idx++];
5613 TemplateTypeParmDecl *D
5614 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
5615 return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
5616 }
5617
5618 case TYPE_DEPENDENT_NAME: {
5619 unsigned Idx = 0;
5620 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
5621 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Richard Smithbdf2d932015-07-30 03:37:16 +00005622 const IdentifierInfo *Name = GetIdentifierInfo(*Loc.F, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00005623 QualType Canon = readType(*Loc.F, Record, Idx);
5624 if (!Canon.isNull())
5625 Canon = Context.getCanonicalType(Canon);
5626 return Context.getDependentNameType(Keyword, NNS, Name, Canon);
5627 }
5628
5629 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
5630 unsigned Idx = 0;
5631 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
5632 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Richard Smithbdf2d932015-07-30 03:37:16 +00005633 const IdentifierInfo *Name = GetIdentifierInfo(*Loc.F, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00005634 unsigned NumArgs = Record[Idx++];
5635 SmallVector<TemplateArgument, 8> Args;
5636 Args.reserve(NumArgs);
5637 while (NumArgs--)
5638 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
5639 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
David Majnemer6fbeee32016-07-07 04:43:07 +00005640 Args);
Guy Benyei11169dd2012-12-18 14:30:41 +00005641 }
5642
5643 case TYPE_DEPENDENT_SIZED_ARRAY: {
5644 unsigned Idx = 0;
5645
5646 // ArrayType
5647 QualType ElementType = readType(*Loc.F, Record, Idx);
5648 ArrayType::ArraySizeModifier ASM
5649 = (ArrayType::ArraySizeModifier)Record[Idx++];
5650 unsigned IndexTypeQuals = Record[Idx++];
5651
5652 // DependentSizedArrayType
5653 Expr *NumElts = ReadExpr(*Loc.F);
5654 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
5655
5656 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
5657 IndexTypeQuals, Brackets);
5658 }
5659
5660 case TYPE_TEMPLATE_SPECIALIZATION: {
5661 unsigned Idx = 0;
5662 bool IsDependent = Record[Idx++];
5663 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
5664 SmallVector<TemplateArgument, 8> Args;
5665 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
5666 QualType Underlying = readType(*Loc.F, Record, Idx);
5667 QualType T;
5668 if (Underlying.isNull())
David Majnemer6fbeee32016-07-07 04:43:07 +00005669 T = Context.getCanonicalTemplateSpecializationType(Name, Args);
Guy Benyei11169dd2012-12-18 14:30:41 +00005670 else
David Majnemer6fbeee32016-07-07 04:43:07 +00005671 T = Context.getTemplateSpecializationType(Name, Args, Underlying);
Guy Benyei11169dd2012-12-18 14:30:41 +00005672 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
5673 return T;
5674 }
5675
5676 case TYPE_ATOMIC: {
5677 if (Record.size() != 1) {
5678 Error("Incorrect encoding of atomic type");
5679 return QualType();
5680 }
5681 QualType ValueType = readType(*Loc.F, Record, Idx);
5682 return Context.getAtomicType(ValueType);
5683 }
Xiuli Pan9c14e282016-01-09 12:53:17 +00005684
5685 case TYPE_PIPE: {
5686 if (Record.size() != 1) {
5687 Error("Incorrect encoding of pipe type");
5688 return QualType();
5689 }
5690
5691 // Reading the pipe element type.
5692 QualType ElementType = readType(*Loc.F, Record, Idx);
5693 return Context.getPipeType(ElementType);
5694 }
Guy Benyei11169dd2012-12-18 14:30:41 +00005695 }
5696 llvm_unreachable("Invalid TypeCode!");
5697}
5698
Richard Smith564417a2014-03-20 21:47:22 +00005699void ASTReader::readExceptionSpec(ModuleFile &ModuleFile,
5700 SmallVectorImpl<QualType> &Exceptions,
Richard Smith8acb4282014-07-31 21:57:55 +00005701 FunctionProtoType::ExceptionSpecInfo &ESI,
Richard Smith564417a2014-03-20 21:47:22 +00005702 const RecordData &Record, unsigned &Idx) {
5703 ExceptionSpecificationType EST =
5704 static_cast<ExceptionSpecificationType>(Record[Idx++]);
Richard Smith8acb4282014-07-31 21:57:55 +00005705 ESI.Type = EST;
Richard Smith564417a2014-03-20 21:47:22 +00005706 if (EST == EST_Dynamic) {
Richard Smith8acb4282014-07-31 21:57:55 +00005707 for (unsigned I = 0, N = Record[Idx++]; I != N; ++I)
Richard Smith564417a2014-03-20 21:47:22 +00005708 Exceptions.push_back(readType(ModuleFile, Record, Idx));
Richard Smith8acb4282014-07-31 21:57:55 +00005709 ESI.Exceptions = Exceptions;
Richard Smith564417a2014-03-20 21:47:22 +00005710 } else if (EST == EST_ComputedNoexcept) {
Richard Smith8acb4282014-07-31 21:57:55 +00005711 ESI.NoexceptExpr = ReadExpr(ModuleFile);
Richard Smith564417a2014-03-20 21:47:22 +00005712 } else if (EST == EST_Uninstantiated) {
Richard Smith8acb4282014-07-31 21:57:55 +00005713 ESI.SourceDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
5714 ESI.SourceTemplate = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
Richard Smith564417a2014-03-20 21:47:22 +00005715 } else if (EST == EST_Unevaluated) {
Richard Smith8acb4282014-07-31 21:57:55 +00005716 ESI.SourceDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
Richard Smith564417a2014-03-20 21:47:22 +00005717 }
5718}
5719
Guy Benyei11169dd2012-12-18 14:30:41 +00005720class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
5721 ASTReader &Reader;
5722 ModuleFile &F;
5723 const ASTReader::RecordData &Record;
5724 unsigned &Idx;
5725
5726 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
5727 unsigned &I) {
5728 return Reader.ReadSourceLocation(F, R, I);
5729 }
5730
5731 template<typename T>
5732 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
5733 return Reader.ReadDeclAs<T>(F, Record, Idx);
5734 }
5735
5736public:
5737 TypeLocReader(ASTReader &Reader, ModuleFile &F,
5738 const ASTReader::RecordData &Record, unsigned &Idx)
5739 : Reader(Reader), F(F), Record(Record), Idx(Idx)
5740 { }
5741
5742 // We want compile-time assurance that we've enumerated all of
5743 // these, so unfortunately we have to declare them first, then
5744 // define them out-of-line.
5745#define ABSTRACT_TYPELOC(CLASS, PARENT)
5746#define TYPELOC(CLASS, PARENT) \
5747 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
5748#include "clang/AST/TypeLocNodes.def"
5749
5750 void VisitFunctionTypeLoc(FunctionTypeLoc);
5751 void VisitArrayTypeLoc(ArrayTypeLoc);
5752};
5753
5754void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
5755 // nothing to do
5756}
5757void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
5758 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
5759 if (TL.needsExtraLocalData()) {
5760 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
5761 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
5762 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
5763 TL.setModeAttr(Record[Idx++]);
5764 }
5765}
5766void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
5767 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5768}
5769void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
5770 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5771}
Reid Kleckner8a365022013-06-24 17:51:48 +00005772void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
5773 // nothing to do
5774}
Reid Kleckner0503a872013-12-05 01:23:43 +00005775void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
5776 // nothing to do
5777}
Guy Benyei11169dd2012-12-18 14:30:41 +00005778void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
5779 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
5780}
5781void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
5782 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
5783}
5784void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
5785 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
5786}
5787void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
5788 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5789 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5790}
5791void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
5792 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
5793 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
5794 if (Record[Idx++])
5795 TL.setSizeExpr(Reader.ReadExpr(F));
5796 else
Craig Toppera13603a2014-05-22 05:54:18 +00005797 TL.setSizeExpr(nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00005798}
5799void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
5800 VisitArrayTypeLoc(TL);
5801}
5802void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
5803 VisitArrayTypeLoc(TL);
5804}
5805void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
5806 VisitArrayTypeLoc(TL);
5807}
5808void TypeLocReader::VisitDependentSizedArrayTypeLoc(
5809 DependentSizedArrayTypeLoc TL) {
5810 VisitArrayTypeLoc(TL);
5811}
5812void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
5813 DependentSizedExtVectorTypeLoc TL) {
5814 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5815}
5816void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
5817 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5818}
5819void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
5820 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5821}
5822void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
5823 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
5824 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5825 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5826 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
Alp Tokerb3fd5cf2014-01-21 00:32:38 +00005827 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
5828 TL.setParam(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00005829 }
5830}
5831void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
5832 VisitFunctionTypeLoc(TL);
5833}
5834void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
5835 VisitFunctionTypeLoc(TL);
5836}
5837void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
5838 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5839}
5840void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
5841 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5842}
5843void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
5844 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
5845 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5846 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5847}
5848void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
5849 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
5850 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5851 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5852 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5853}
5854void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
5855 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5856}
5857void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
5858 TL.setKWLoc(ReadSourceLocation(Record, Idx));
5859 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5860 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5861 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5862}
5863void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
5864 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5865}
5866void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
5867 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5868}
5869void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
5870 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5871}
5872void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
5873 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
5874 if (TL.hasAttrOperand()) {
5875 SourceRange range;
5876 range.setBegin(ReadSourceLocation(Record, Idx));
5877 range.setEnd(ReadSourceLocation(Record, Idx));
5878 TL.setAttrOperandParensRange(range);
5879 }
5880 if (TL.hasAttrExprOperand()) {
5881 if (Record[Idx++])
5882 TL.setAttrExprOperand(Reader.ReadExpr(F));
5883 else
Craig Toppera13603a2014-05-22 05:54:18 +00005884 TL.setAttrExprOperand(nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00005885 } else if (TL.hasAttrEnumOperand())
5886 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
5887}
5888void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
5889 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5890}
5891void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
5892 SubstTemplateTypeParmTypeLoc TL) {
5893 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5894}
5895void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
5896 SubstTemplateTypeParmPackTypeLoc TL) {
5897 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5898}
5899void TypeLocReader::VisitTemplateSpecializationTypeLoc(
5900 TemplateSpecializationTypeLoc TL) {
5901 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
5902 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
5903 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5904 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5905 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
5906 TL.setArgLocInfo(i,
5907 Reader.GetTemplateArgumentLocInfo(F,
5908 TL.getTypePtr()->getArg(i).getKind(),
5909 Record, Idx));
5910}
5911void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
5912 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5913 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5914}
5915void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
5916 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5917 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5918}
5919void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
5920 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5921}
5922void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
5923 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5924 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5925 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5926}
5927void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
5928 DependentTemplateSpecializationTypeLoc TL) {
5929 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5930 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5931 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
5932 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
5933 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5934 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5935 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
5936 TL.setArgLocInfo(I,
5937 Reader.GetTemplateArgumentLocInfo(F,
5938 TL.getTypePtr()->getArg(I).getKind(),
5939 Record, Idx));
5940}
5941void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
5942 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
5943}
5944void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
5945 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5946}
5947void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
5948 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Douglas Gregore9d95f12015-07-07 03:57:35 +00005949 TL.setTypeArgsLAngleLoc(ReadSourceLocation(Record, Idx));
5950 TL.setTypeArgsRAngleLoc(ReadSourceLocation(Record, Idx));
5951 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
5952 TL.setTypeArgTInfo(i, Reader.GetTypeSourceInfo(F, Record, Idx));
5953 TL.setProtocolLAngleLoc(ReadSourceLocation(Record, Idx));
5954 TL.setProtocolRAngleLoc(ReadSourceLocation(Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00005955 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
5956 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
5957}
5958void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
5959 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5960}
5961void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
5962 TL.setKWLoc(ReadSourceLocation(Record, Idx));
5963 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5964 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5965}
Xiuli Pan9c14e282016-01-09 12:53:17 +00005966void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
5967 TL.setKWLoc(ReadSourceLocation(Record, Idx));
5968}
Guy Benyei11169dd2012-12-18 14:30:41 +00005969
5970TypeSourceInfo *ASTReader::GetTypeSourceInfo(ModuleFile &F,
5971 const RecordData &Record,
5972 unsigned &Idx) {
5973 QualType InfoTy = readType(F, Record, Idx);
5974 if (InfoTy.isNull())
Craig Toppera13603a2014-05-22 05:54:18 +00005975 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00005976
5977 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
5978 TypeLocReader TLR(*this, F, Record, Idx);
5979 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
5980 TLR.Visit(TL);
5981 return TInfo;
5982}
5983
5984QualType ASTReader::GetType(TypeID ID) {
5985 unsigned FastQuals = ID & Qualifiers::FastMask;
5986 unsigned Index = ID >> Qualifiers::FastWidth;
5987
5988 if (Index < NUM_PREDEF_TYPE_IDS) {
5989 QualType T;
5990 switch ((PredefinedTypeIDs)Index) {
Alexey Baderbdf7c842015-09-15 12:18:29 +00005991 case PREDEF_TYPE_NULL_ID:
5992 return QualType();
5993 case PREDEF_TYPE_VOID_ID:
5994 T = Context.VoidTy;
5995 break;
5996 case PREDEF_TYPE_BOOL_ID:
5997 T = Context.BoolTy;
5998 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00005999
6000 case PREDEF_TYPE_CHAR_U_ID:
6001 case PREDEF_TYPE_CHAR_S_ID:
6002 // FIXME: Check that the signedness of CharTy is correct!
6003 T = Context.CharTy;
6004 break;
6005
Alexey Baderbdf7c842015-09-15 12:18:29 +00006006 case PREDEF_TYPE_UCHAR_ID:
6007 T = Context.UnsignedCharTy;
6008 break;
6009 case PREDEF_TYPE_USHORT_ID:
6010 T = Context.UnsignedShortTy;
6011 break;
6012 case PREDEF_TYPE_UINT_ID:
6013 T = Context.UnsignedIntTy;
6014 break;
6015 case PREDEF_TYPE_ULONG_ID:
6016 T = Context.UnsignedLongTy;
6017 break;
6018 case PREDEF_TYPE_ULONGLONG_ID:
6019 T = Context.UnsignedLongLongTy;
6020 break;
6021 case PREDEF_TYPE_UINT128_ID:
6022 T = Context.UnsignedInt128Ty;
6023 break;
6024 case PREDEF_TYPE_SCHAR_ID:
6025 T = Context.SignedCharTy;
6026 break;
6027 case PREDEF_TYPE_WCHAR_ID:
6028 T = Context.WCharTy;
6029 break;
6030 case PREDEF_TYPE_SHORT_ID:
6031 T = Context.ShortTy;
6032 break;
6033 case PREDEF_TYPE_INT_ID:
6034 T = Context.IntTy;
6035 break;
6036 case PREDEF_TYPE_LONG_ID:
6037 T = Context.LongTy;
6038 break;
6039 case PREDEF_TYPE_LONGLONG_ID:
6040 T = Context.LongLongTy;
6041 break;
6042 case PREDEF_TYPE_INT128_ID:
6043 T = Context.Int128Ty;
6044 break;
6045 case PREDEF_TYPE_HALF_ID:
6046 T = Context.HalfTy;
6047 break;
6048 case PREDEF_TYPE_FLOAT_ID:
6049 T = Context.FloatTy;
6050 break;
6051 case PREDEF_TYPE_DOUBLE_ID:
6052 T = Context.DoubleTy;
6053 break;
6054 case PREDEF_TYPE_LONGDOUBLE_ID:
6055 T = Context.LongDoubleTy;
6056 break;
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +00006057 case PREDEF_TYPE_FLOAT128_ID:
6058 T = Context.Float128Ty;
6059 break;
Alexey Baderbdf7c842015-09-15 12:18:29 +00006060 case PREDEF_TYPE_OVERLOAD_ID:
6061 T = Context.OverloadTy;
6062 break;
6063 case PREDEF_TYPE_BOUND_MEMBER:
6064 T = Context.BoundMemberTy;
6065 break;
6066 case PREDEF_TYPE_PSEUDO_OBJECT:
6067 T = Context.PseudoObjectTy;
6068 break;
6069 case PREDEF_TYPE_DEPENDENT_ID:
6070 T = Context.DependentTy;
6071 break;
6072 case PREDEF_TYPE_UNKNOWN_ANY:
6073 T = Context.UnknownAnyTy;
6074 break;
6075 case PREDEF_TYPE_NULLPTR_ID:
6076 T = Context.NullPtrTy;
6077 break;
6078 case PREDEF_TYPE_CHAR16_ID:
6079 T = Context.Char16Ty;
6080 break;
6081 case PREDEF_TYPE_CHAR32_ID:
6082 T = Context.Char32Ty;
6083 break;
6084 case PREDEF_TYPE_OBJC_ID:
6085 T = Context.ObjCBuiltinIdTy;
6086 break;
6087 case PREDEF_TYPE_OBJC_CLASS:
6088 T = Context.ObjCBuiltinClassTy;
6089 break;
6090 case PREDEF_TYPE_OBJC_SEL:
6091 T = Context.ObjCBuiltinSelTy;
6092 break;
Alexey Bader954ba212016-04-08 13:40:33 +00006093#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
6094 case PREDEF_TYPE_##Id##_ID: \
6095 T = Context.SingletonId; \
Alexey Baderbdf7c842015-09-15 12:18:29 +00006096 break;
Alexey Baderb62f1442016-04-13 08:33:41 +00006097#include "clang/Basic/OpenCLImageTypes.def"
Alexey Baderbdf7c842015-09-15 12:18:29 +00006098 case PREDEF_TYPE_SAMPLER_ID:
6099 T = Context.OCLSamplerTy;
6100 break;
6101 case PREDEF_TYPE_EVENT_ID:
6102 T = Context.OCLEventTy;
6103 break;
Alexey Bader9c8453f2015-09-15 11:18:52 +00006104 case PREDEF_TYPE_CLK_EVENT_ID:
6105 T = Context.OCLClkEventTy;
6106 break;
6107 case PREDEF_TYPE_QUEUE_ID:
6108 T = Context.OCLQueueTy;
6109 break;
6110 case PREDEF_TYPE_NDRANGE_ID:
6111 T = Context.OCLNDRangeTy;
6112 break;
6113 case PREDEF_TYPE_RESERVE_ID_ID:
6114 T = Context.OCLReserveIDTy;
6115 break;
Alexey Baderbdf7c842015-09-15 12:18:29 +00006116 case PREDEF_TYPE_AUTO_DEDUCT:
6117 T = Context.getAutoDeductType();
6118 break;
6119
6120 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
6121 T = Context.getAutoRRefDeductType();
Guy Benyei11169dd2012-12-18 14:30:41 +00006122 break;
6123
6124 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
6125 T = Context.ARCUnbridgedCastTy;
6126 break;
6127
Guy Benyei11169dd2012-12-18 14:30:41 +00006128 case PREDEF_TYPE_BUILTIN_FN:
6129 T = Context.BuiltinFnTy;
6130 break;
Alexey Bataev1a3320e2015-08-25 14:24:04 +00006131
6132 case PREDEF_TYPE_OMP_ARRAY_SECTION:
6133 T = Context.OMPArraySectionTy;
6134 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00006135 }
6136
6137 assert(!T.isNull() && "Unknown predefined type");
6138 return T.withFastQualifiers(FastQuals);
6139 }
6140
6141 Index -= NUM_PREDEF_TYPE_IDS;
6142 assert(Index < TypesLoaded.size() && "Type index out-of-range");
6143 if (TypesLoaded[Index].isNull()) {
6144 TypesLoaded[Index] = readTypeRecord(Index);
6145 if (TypesLoaded[Index].isNull())
6146 return QualType();
6147
6148 TypesLoaded[Index]->setFromAST();
6149 if (DeserializationListener)
6150 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
6151 TypesLoaded[Index]);
6152 }
6153
6154 return TypesLoaded[Index].withFastQualifiers(FastQuals);
6155}
6156
6157QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
6158 return GetType(getGlobalTypeID(F, LocalID));
6159}
6160
6161serialization::TypeID
6162ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
6163 unsigned FastQuals = LocalID & Qualifiers::FastMask;
6164 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
6165
6166 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
6167 return LocalID;
6168
6169 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6170 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
6171 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
6172
6173 unsigned GlobalIndex = LocalIndex + I->second;
6174 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
6175}
6176
6177TemplateArgumentLocInfo
6178ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
6179 TemplateArgument::ArgKind Kind,
6180 const RecordData &Record,
6181 unsigned &Index) {
6182 switch (Kind) {
6183 case TemplateArgument::Expression:
6184 return ReadExpr(F);
6185 case TemplateArgument::Type:
6186 return GetTypeSourceInfo(F, Record, Index);
6187 case TemplateArgument::Template: {
6188 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
6189 Index);
6190 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
6191 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
6192 SourceLocation());
6193 }
6194 case TemplateArgument::TemplateExpansion: {
6195 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
6196 Index);
6197 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
6198 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
6199 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
6200 EllipsisLoc);
6201 }
6202 case TemplateArgument::Null:
6203 case TemplateArgument::Integral:
6204 case TemplateArgument::Declaration:
6205 case TemplateArgument::NullPtr:
6206 case TemplateArgument::Pack:
6207 // FIXME: Is this right?
6208 return TemplateArgumentLocInfo();
6209 }
6210 llvm_unreachable("unexpected template argument loc");
6211}
6212
6213TemplateArgumentLoc
6214ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
6215 const RecordData &Record, unsigned &Index) {
6216 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
6217
6218 if (Arg.getKind() == TemplateArgument::Expression) {
6219 if (Record[Index++]) // bool InfoHasSameExpr.
6220 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
6221 }
6222 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
6223 Record, Index));
6224}
6225
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00006226const ASTTemplateArgumentListInfo*
6227ASTReader::ReadASTTemplateArgumentListInfo(ModuleFile &F,
6228 const RecordData &Record,
6229 unsigned &Index) {
6230 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Index);
6231 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Index);
6232 unsigned NumArgsAsWritten = Record[Index++];
6233 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
6234 for (unsigned i = 0; i != NumArgsAsWritten; ++i)
6235 TemplArgsInfo.addArgument(ReadTemplateArgumentLoc(F, Record, Index));
6236 return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo);
6237}
6238
Guy Benyei11169dd2012-12-18 14:30:41 +00006239Decl *ASTReader::GetExternalDecl(uint32_t ID) {
6240 return GetDecl(ID);
6241}
6242
Richard Smith50895422015-01-31 03:04:55 +00006243template<typename TemplateSpecializationDecl>
6244static void completeRedeclChainForTemplateSpecialization(Decl *D) {
6245 if (auto *TSD = dyn_cast<TemplateSpecializationDecl>(D))
6246 TSD->getSpecializedTemplate()->LoadLazySpecializations();
6247}
6248
Richard Smith053f6c62014-05-16 23:01:30 +00006249void ASTReader::CompleteRedeclChain(const Decl *D) {
Richard Smith851072e2014-05-19 20:59:20 +00006250 if (NumCurrentElementsDeserializing) {
6251 // We arrange to not care about the complete redeclaration chain while we're
6252 // deserializing. Just remember that the AST has marked this one as complete
6253 // but that it's not actually complete yet, so we know we still need to
6254 // complete it later.
6255 PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D));
6256 return;
6257 }
6258
Richard Smith053f6c62014-05-16 23:01:30 +00006259 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
6260
Richard Smith053f6c62014-05-16 23:01:30 +00006261 // If this is a named declaration, complete it by looking it up
6262 // within its context.
6263 //
Richard Smith01bdb7a2014-08-28 05:44:07 +00006264 // FIXME: Merging a function definition should merge
Richard Smith053f6c62014-05-16 23:01:30 +00006265 // all mergeable entities within it.
6266 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) ||
6267 isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) {
6268 if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) {
Richard Smitha534a312015-07-21 23:54:07 +00006269 if (!getContext().getLangOpts().CPlusPlus &&
6270 isa<TranslationUnitDecl>(DC)) {
Richard Smith053f6c62014-05-16 23:01:30 +00006271 // Outside of C++, we don't have a lookup table for the TU, so update
Richard Smitha534a312015-07-21 23:54:07 +00006272 // the identifier instead. (For C++ modules, we don't store decls
6273 // in the serialized identifier table, so we do the lookup in the TU.)
6274 auto *II = Name.getAsIdentifierInfo();
6275 assert(II && "non-identifier name in C?");
Richard Smith053f6c62014-05-16 23:01:30 +00006276 if (II->isOutOfDate())
6277 updateOutOfDateIdentifier(*II);
6278 } else
6279 DC->lookup(Name);
Richard Smith01bdb7a2014-08-28 05:44:07 +00006280 } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) {
Richard Smith3cb15722015-08-05 22:41:45 +00006281 // Find all declarations of this kind from the relevant context.
6282 for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) {
6283 auto *DC = cast<DeclContext>(DCDecl);
6284 SmallVector<Decl*, 8> Decls;
6285 FindExternalLexicalDecls(
6286 DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls);
6287 }
Richard Smith053f6c62014-05-16 23:01:30 +00006288 }
6289 }
Richard Smith50895422015-01-31 03:04:55 +00006290
6291 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D))
6292 CTSD->getSpecializedTemplate()->LoadLazySpecializations();
6293 if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D))
6294 VTSD->getSpecializedTemplate()->LoadLazySpecializations();
6295 if (auto *FD = dyn_cast<FunctionDecl>(D)) {
6296 if (auto *Template = FD->getPrimaryTemplate())
6297 Template->LoadLazySpecializations();
6298 }
Richard Smith053f6c62014-05-16 23:01:30 +00006299}
6300
Richard Smithc2bb8182015-03-24 06:36:48 +00006301CXXCtorInitializer **
6302ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
6303 RecordLocation Loc = getLocalBitOffset(Offset);
6304 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
6305 SavedStreamPosition SavedPosition(Cursor);
6306 Cursor.JumpToBit(Loc.Offset);
6307 ReadingKindTracker ReadingKind(Read_Decl, *this);
6308
6309 RecordData Record;
6310 unsigned Code = Cursor.ReadCode();
6311 unsigned RecCode = Cursor.readRecord(Code, Record);
6312 if (RecCode != DECL_CXX_CTOR_INITIALIZERS) {
6313 Error("malformed AST file: missing C++ ctor initializers");
6314 return nullptr;
6315 }
6316
6317 unsigned Idx = 0;
6318 return ReadCXXCtorInitializers(*Loc.F, Record, Idx);
6319}
6320
Guy Benyei11169dd2012-12-18 14:30:41 +00006321CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
6322 RecordLocation Loc = getLocalBitOffset(Offset);
Chris Lattner7fb3bef2013-01-20 00:56:42 +00006323 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00006324 SavedStreamPosition SavedPosition(Cursor);
6325 Cursor.JumpToBit(Loc.Offset);
6326 ReadingKindTracker ReadingKind(Read_Decl, *this);
6327 RecordData Record;
6328 unsigned Code = Cursor.ReadCode();
Chris Lattner0e6c9402013-01-20 02:38:54 +00006329 unsigned RecCode = Cursor.readRecord(Code, Record);
Guy Benyei11169dd2012-12-18 14:30:41 +00006330 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00006331 Error("malformed AST file: missing C++ base specifiers");
Craig Toppera13603a2014-05-22 05:54:18 +00006332 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00006333 }
6334
6335 unsigned Idx = 0;
6336 unsigned NumBases = Record[Idx++];
6337 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
6338 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
6339 for (unsigned I = 0; I != NumBases; ++I)
6340 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
6341 return Bases;
6342}
6343
6344serialization::DeclID
6345ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
6346 if (LocalID < NUM_PREDEF_DECL_IDS)
6347 return LocalID;
6348
6349 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6350 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
6351 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
6352
6353 return LocalID + I->second;
6354}
6355
6356bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
6357 ModuleFile &M) const {
Richard Smithfe620d22015-03-05 23:24:12 +00006358 // Predefined decls aren't from any module.
6359 if (ID < NUM_PREDEF_DECL_IDS)
6360 return false;
6361
Richard Smithbcda1a92015-07-12 23:51:20 +00006362 return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID &&
6363 ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls;
Guy Benyei11169dd2012-12-18 14:30:41 +00006364}
6365
Douglas Gregor9f782892013-01-21 15:25:38 +00006366ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
Guy Benyei11169dd2012-12-18 14:30:41 +00006367 if (!D->isFromASTFile())
Craig Toppera13603a2014-05-22 05:54:18 +00006368 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00006369 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
6370 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
6371 return I->second;
6372}
6373
6374SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
6375 if (ID < NUM_PREDEF_DECL_IDS)
6376 return SourceLocation();
Richard Smithcd45dbc2014-04-19 03:48:30 +00006377
Guy Benyei11169dd2012-12-18 14:30:41 +00006378 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
6379
6380 if (Index > DeclsLoaded.size()) {
6381 Error("declaration ID out-of-range for AST file");
6382 return SourceLocation();
6383 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00006384
Guy Benyei11169dd2012-12-18 14:30:41 +00006385 if (Decl *D = DeclsLoaded[Index])
6386 return D->getLocation();
6387
Richard Smithcb34bd32016-03-27 07:28:06 +00006388 SourceLocation Loc;
6389 DeclCursorForID(ID, Loc);
6390 return Loc;
Guy Benyei11169dd2012-12-18 14:30:41 +00006391}
6392
Richard Smithfe620d22015-03-05 23:24:12 +00006393static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) {
6394 switch (ID) {
6395 case PREDEF_DECL_NULL_ID:
6396 return nullptr;
6397
6398 case PREDEF_DECL_TRANSLATION_UNIT_ID:
6399 return Context.getTranslationUnitDecl();
6400
6401 case PREDEF_DECL_OBJC_ID_ID:
6402 return Context.getObjCIdDecl();
6403
6404 case PREDEF_DECL_OBJC_SEL_ID:
6405 return Context.getObjCSelDecl();
6406
6407 case PREDEF_DECL_OBJC_CLASS_ID:
6408 return Context.getObjCClassDecl();
6409
6410 case PREDEF_DECL_OBJC_PROTOCOL_ID:
6411 return Context.getObjCProtocolDecl();
6412
6413 case PREDEF_DECL_INT_128_ID:
6414 return Context.getInt128Decl();
6415
6416 case PREDEF_DECL_UNSIGNED_INT_128_ID:
6417 return Context.getUInt128Decl();
6418
6419 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
6420 return Context.getObjCInstanceTypeDecl();
6421
6422 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
6423 return Context.getBuiltinVaListDecl();
Richard Smithf19e1272015-03-07 00:04:49 +00006424
Richard Smith9b88a4c2015-07-27 05:40:23 +00006425 case PREDEF_DECL_VA_LIST_TAG:
6426 return Context.getVaListTagDecl();
6427
Charles Davisc7d5c942015-09-17 20:55:33 +00006428 case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
6429 return Context.getBuiltinMSVaListDecl();
6430
Richard Smithf19e1272015-03-07 00:04:49 +00006431 case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
6432 return Context.getExternCContextDecl();
David Majnemerd9b1a4f2015-11-04 03:40:30 +00006433
6434 case PREDEF_DECL_MAKE_INTEGER_SEQ_ID:
6435 return Context.getMakeIntegerSeqDecl();
Quentin Colombet043406b2016-02-03 22:41:00 +00006436
6437 case PREDEF_DECL_CF_CONSTANT_STRING_ID:
6438 return Context.getCFConstantStringDecl();
Ben Langmuirf5416742016-02-04 00:55:24 +00006439
6440 case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
6441 return Context.getCFConstantStringTagDecl();
Eric Fiselier6ad68552016-07-01 01:24:09 +00006442
6443 case PREDEF_DECL_TYPE_PACK_ELEMENT_ID:
6444 return Context.getTypePackElementDecl();
Richard Smithfe620d22015-03-05 23:24:12 +00006445 }
Yaron Keren322bdad2015-03-06 07:49:14 +00006446 llvm_unreachable("PredefinedDeclIDs unknown enum value");
Richard Smithfe620d22015-03-05 23:24:12 +00006447}
6448
Richard Smithcd45dbc2014-04-19 03:48:30 +00006449Decl *ASTReader::GetExistingDecl(DeclID ID) {
6450 if (ID < NUM_PREDEF_DECL_IDS) {
Richard Smithfe620d22015-03-05 23:24:12 +00006451 Decl *D = getPredefinedDecl(Context, (PredefinedDeclIDs)ID);
6452 if (D) {
6453 // Track that we have merged the declaration with ID \p ID into the
6454 // pre-existing predefined declaration \p D.
Richard Smith5fc18a92015-07-12 23:43:21 +00006455 auto &Merged = KeyDecls[D->getCanonicalDecl()];
Richard Smithfe620d22015-03-05 23:24:12 +00006456 if (Merged.empty())
6457 Merged.push_back(ID);
Guy Benyei11169dd2012-12-18 14:30:41 +00006458 }
Richard Smithfe620d22015-03-05 23:24:12 +00006459 return D;
Guy Benyei11169dd2012-12-18 14:30:41 +00006460 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00006461
Guy Benyei11169dd2012-12-18 14:30:41 +00006462 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
6463
6464 if (Index >= DeclsLoaded.size()) {
6465 assert(0 && "declaration ID out-of-range for AST file");
6466 Error("declaration ID out-of-range for AST file");
Craig Toppera13603a2014-05-22 05:54:18 +00006467 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00006468 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00006469
6470 return DeclsLoaded[Index];
6471}
6472
6473Decl *ASTReader::GetDecl(DeclID ID) {
6474 if (ID < NUM_PREDEF_DECL_IDS)
6475 return GetExistingDecl(ID);
6476
6477 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
6478
6479 if (Index >= DeclsLoaded.size()) {
6480 assert(0 && "declaration ID out-of-range for AST file");
6481 Error("declaration ID out-of-range for AST file");
Craig Toppera13603a2014-05-22 05:54:18 +00006482 return nullptr;
Richard Smithcd45dbc2014-04-19 03:48:30 +00006483 }
6484
Guy Benyei11169dd2012-12-18 14:30:41 +00006485 if (!DeclsLoaded[Index]) {
6486 ReadDeclRecord(ID);
6487 if (DeserializationListener)
6488 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
6489 }
6490
6491 return DeclsLoaded[Index];
6492}
6493
6494DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
6495 DeclID GlobalID) {
6496 if (GlobalID < NUM_PREDEF_DECL_IDS)
6497 return GlobalID;
6498
6499 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
6500 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
6501 ModuleFile *Owner = I->second;
6502
6503 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
6504 = M.GlobalToLocalDeclIDs.find(Owner);
6505 if (Pos == M.GlobalToLocalDeclIDs.end())
6506 return 0;
6507
6508 return GlobalID - Owner->BaseDeclID + Pos->second;
6509}
6510
6511serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
6512 const RecordData &Record,
6513 unsigned &Idx) {
6514 if (Idx >= Record.size()) {
6515 Error("Corrupted AST file");
6516 return 0;
6517 }
6518
6519 return getGlobalDeclID(F, Record[Idx++]);
6520}
6521
6522/// \brief Resolve the offset of a statement into a statement.
6523///
6524/// This operation will read a new statement from the external
6525/// source each time it is called, and is meant to be used via a
6526/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
6527Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
6528 // Switch case IDs are per Decl.
6529 ClearSwitchCaseIDs();
6530
6531 // Offset here is a global offset across the entire chain.
6532 RecordLocation Loc = getLocalBitOffset(Offset);
6533 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
6534 return ReadStmtFromStream(*Loc.F);
6535}
6536
Richard Smith3cb15722015-08-05 22:41:45 +00006537void ASTReader::FindExternalLexicalDecls(
6538 const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
6539 SmallVectorImpl<Decl *> &Decls) {
Richard Smith82f8fcd2015-08-06 22:07:25 +00006540 bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
6541
Richard Smith9ccdd932015-08-06 22:14:12 +00006542 auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
Richard Smith82f8fcd2015-08-06 22:07:25 +00006543 assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");
6544 for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
6545 auto K = (Decl::Kind)+LexicalDecls[I];
6546 if (!IsKindWeWant(K))
6547 continue;
6548
6549 auto ID = (serialization::DeclID)+LexicalDecls[I + 1];
6550
6551 // Don't add predefined declarations to the lexical context more
6552 // than once.
6553 if (ID < NUM_PREDEF_DECL_IDS) {
6554 if (PredefsVisited[ID])
6555 continue;
6556
6557 PredefsVisited[ID] = true;
6558 }
6559
6560 if (Decl *D = GetLocalDecl(*M, ID)) {
Richard Smith2317a3e2015-08-11 21:21:20 +00006561 assert(D->getKind() == K && "wrong kind for lexical decl");
Richard Smith82f8fcd2015-08-06 22:07:25 +00006562 if (!DC->isDeclInLexicalTraversal(D))
6563 Decls.push_back(D);
6564 }
6565 }
6566 };
6567
6568 if (isa<TranslationUnitDecl>(DC)) {
6569 for (auto Lexical : TULexicalDecls)
6570 Visit(Lexical.first, Lexical.second);
6571 } else {
6572 auto I = LexicalDecls.find(DC);
6573 if (I != LexicalDecls.end())
Richard Smith9c9173d2015-08-11 22:00:24 +00006574 Visit(I->second.first, I->second.second);
Richard Smith82f8fcd2015-08-06 22:07:25 +00006575 }
6576
Guy Benyei11169dd2012-12-18 14:30:41 +00006577 ++NumLexicalDeclContextsRead;
Guy Benyei11169dd2012-12-18 14:30:41 +00006578}
6579
6580namespace {
6581
6582class DeclIDComp {
6583 ASTReader &Reader;
6584 ModuleFile &Mod;
6585
6586public:
6587 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
6588
6589 bool operator()(LocalDeclID L, LocalDeclID R) const {
6590 SourceLocation LHS = getLocation(L);
6591 SourceLocation RHS = getLocation(R);
6592 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6593 }
6594
6595 bool operator()(SourceLocation LHS, LocalDeclID R) const {
6596 SourceLocation RHS = getLocation(R);
6597 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6598 }
6599
6600 bool operator()(LocalDeclID L, SourceLocation RHS) const {
6601 SourceLocation LHS = getLocation(L);
6602 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6603 }
6604
6605 SourceLocation getLocation(LocalDeclID ID) const {
6606 return Reader.getSourceManager().getFileLoc(
6607 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
6608 }
6609};
6610
Alexander Kornienkoab9db512015-06-22 23:07:51 +00006611}
Guy Benyei11169dd2012-12-18 14:30:41 +00006612
6613void ASTReader::FindFileRegionDecls(FileID File,
6614 unsigned Offset, unsigned Length,
6615 SmallVectorImpl<Decl *> &Decls) {
6616 SourceManager &SM = getSourceManager();
6617
6618 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
6619 if (I == FileDeclIDs.end())
6620 return;
6621
6622 FileDeclsInfo &DInfo = I->second;
6623 if (DInfo.Decls.empty())
6624 return;
6625
6626 SourceLocation
6627 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
6628 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
6629
6630 DeclIDComp DIDComp(*this, *DInfo.Mod);
6631 ArrayRef<serialization::LocalDeclID>::iterator
6632 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
6633 BeginLoc, DIDComp);
6634 if (BeginIt != DInfo.Decls.begin())
6635 --BeginIt;
6636
6637 // If we are pointing at a top-level decl inside an objc container, we need
6638 // to backtrack until we find it otherwise we will fail to report that the
6639 // region overlaps with an objc container.
6640 while (BeginIt != DInfo.Decls.begin() &&
6641 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
6642 ->isTopLevelDeclInObjCContainer())
6643 --BeginIt;
6644
6645 ArrayRef<serialization::LocalDeclID>::iterator
6646 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
6647 EndLoc, DIDComp);
6648 if (EndIt != DInfo.Decls.end())
6649 ++EndIt;
6650
6651 for (ArrayRef<serialization::LocalDeclID>::iterator
6652 DIt = BeginIt; DIt != EndIt; ++DIt)
6653 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
6654}
6655
Richard Smith9ce12e32013-02-07 03:30:24 +00006656bool
Guy Benyei11169dd2012-12-18 14:30:41 +00006657ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
6658 DeclarationName Name) {
Richard Smithd88a7f12015-09-01 20:35:42 +00006659 assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
Guy Benyei11169dd2012-12-18 14:30:41 +00006660 "DeclContext has no visible decls in storage");
6661 if (!Name)
Richard Smith9ce12e32013-02-07 03:30:24 +00006662 return false;
Guy Benyei11169dd2012-12-18 14:30:41 +00006663
Richard Smithd88a7f12015-09-01 20:35:42 +00006664 auto It = Lookups.find(DC);
6665 if (It == Lookups.end())
6666 return false;
6667
Richard Smith8c913ec2014-08-14 02:21:01 +00006668 Deserializing LookupResults(this);
6669
Richard Smithd88a7f12015-09-01 20:35:42 +00006670 // Load the list of declarations.
Guy Benyei11169dd2012-12-18 14:30:41 +00006671 SmallVector<NamedDecl *, 64> Decls;
Richard Smithd88a7f12015-09-01 20:35:42 +00006672 for (DeclID ID : It->second.Table.find(Name)) {
6673 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
6674 if (ND->getDeclName() == Name)
6675 Decls.push_back(ND);
6676 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00006677
Guy Benyei11169dd2012-12-18 14:30:41 +00006678 ++NumVisibleDeclContextsRead;
6679 SetExternalVisibleDeclsForName(DC, Name, Decls);
Richard Smith9ce12e32013-02-07 03:30:24 +00006680 return !Decls.empty();
Guy Benyei11169dd2012-12-18 14:30:41 +00006681}
6682
Guy Benyei11169dd2012-12-18 14:30:41 +00006683void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
6684 if (!DC->hasExternalVisibleStorage())
6685 return;
Richard Smithd88a7f12015-09-01 20:35:42 +00006686
6687 auto It = Lookups.find(DC);
6688 assert(It != Lookups.end() &&
6689 "have external visible storage but no lookup tables");
6690
Craig Topper79be4cd2013-07-05 04:33:53 +00006691 DeclsMap Decls;
Guy Benyei11169dd2012-12-18 14:30:41 +00006692
Richard Smithd88a7f12015-09-01 20:35:42 +00006693 for (DeclID ID : It->second.Table.findAll()) {
6694 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
6695 Decls[ND->getDeclName()].push_back(ND);
Guy Benyei11169dd2012-12-18 14:30:41 +00006696 }
6697
Guy Benyei11169dd2012-12-18 14:30:41 +00006698 ++NumVisibleDeclContextsRead;
6699
Craig Topper79be4cd2013-07-05 04:33:53 +00006700 for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
Guy Benyei11169dd2012-12-18 14:30:41 +00006701 SetExternalVisibleDeclsForName(DC, I->first, I->second);
6702 }
6703 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
6704}
6705
Richard Smithd88a7f12015-09-01 20:35:42 +00006706const serialization::reader::DeclContextLookupTable *
6707ASTReader::getLoadedLookupTables(DeclContext *Primary) const {
6708 auto I = Lookups.find(Primary);
6709 return I == Lookups.end() ? nullptr : &I->second;
6710}
6711
Guy Benyei11169dd2012-12-18 14:30:41 +00006712/// \brief Under non-PCH compilation the consumer receives the objc methods
6713/// before receiving the implementation, and codegen depends on this.
6714/// We simulate this by deserializing and passing to consumer the methods of the
6715/// implementation before passing the deserialized implementation decl.
6716static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
6717 ASTConsumer *Consumer) {
6718 assert(ImplD && Consumer);
6719
Aaron Ballmanaff18c02014-03-13 19:03:34 +00006720 for (auto *I : ImplD->methods())
6721 Consumer->HandleInterestingDecl(DeclGroupRef(I));
Guy Benyei11169dd2012-12-18 14:30:41 +00006722
6723 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
6724}
6725
6726void ASTReader::PassInterestingDeclsToConsumer() {
6727 assert(Consumer);
Richard Smith04d05b52014-03-23 00:27:18 +00006728
6729 if (PassingDeclsToConsumer)
6730 return;
6731
6732 // Guard variable to avoid recursively redoing the process of passing
6733 // decls to consumer.
6734 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
6735 true);
6736
Richard Smith9e2341d2015-03-23 03:25:59 +00006737 // Ensure that we've loaded all potentially-interesting declarations
6738 // that need to be eagerly loaded.
6739 for (auto ID : EagerlyDeserializedDecls)
6740 GetDecl(ID);
6741 EagerlyDeserializedDecls.clear();
6742
Guy Benyei11169dd2012-12-18 14:30:41 +00006743 while (!InterestingDecls.empty()) {
6744 Decl *D = InterestingDecls.front();
6745 InterestingDecls.pop_front();
6746
6747 PassInterestingDeclToConsumer(D);
6748 }
6749}
6750
6751void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
6752 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
6753 PassObjCImplDeclToConsumer(ImplD, Consumer);
6754 else
6755 Consumer->HandleInterestingDecl(DeclGroupRef(D));
6756}
6757
6758void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
6759 this->Consumer = Consumer;
6760
Richard Smith9e2341d2015-03-23 03:25:59 +00006761 if (Consumer)
6762 PassInterestingDeclsToConsumer();
Richard Smith7f330cd2015-03-18 01:42:29 +00006763
6764 if (DeserializationListener)
6765 DeserializationListener->ReaderInitialized(this);
Guy Benyei11169dd2012-12-18 14:30:41 +00006766}
6767
6768void ASTReader::PrintStats() {
6769 std::fprintf(stderr, "*** AST File Statistics:\n");
6770
6771 unsigned NumTypesLoaded
6772 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
6773 QualType());
6774 unsigned NumDeclsLoaded
6775 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
Craig Toppera13603a2014-05-22 05:54:18 +00006776 (Decl *)nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00006777 unsigned NumIdentifiersLoaded
6778 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
6779 IdentifiersLoaded.end(),
Craig Toppera13603a2014-05-22 05:54:18 +00006780 (IdentifierInfo *)nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00006781 unsigned NumMacrosLoaded
6782 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
6783 MacrosLoaded.end(),
Craig Toppera13603a2014-05-22 05:54:18 +00006784 (MacroInfo *)nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00006785 unsigned NumSelectorsLoaded
6786 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
6787 SelectorsLoaded.end(),
6788 Selector());
6789
6790 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
6791 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
6792 NumSLocEntriesRead, TotalNumSLocEntries,
6793 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
6794 if (!TypesLoaded.empty())
6795 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
6796 NumTypesLoaded, (unsigned)TypesLoaded.size(),
6797 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
6798 if (!DeclsLoaded.empty())
6799 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
6800 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
6801 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
6802 if (!IdentifiersLoaded.empty())
6803 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
6804 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
6805 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
6806 if (!MacrosLoaded.empty())
6807 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
6808 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
6809 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
6810 if (!SelectorsLoaded.empty())
6811 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
6812 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
6813 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
6814 if (TotalNumStatements)
6815 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
6816 NumStatementsRead, TotalNumStatements,
6817 ((float)NumStatementsRead/TotalNumStatements * 100));
6818 if (TotalNumMacros)
6819 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
6820 NumMacrosRead, TotalNumMacros,
6821 ((float)NumMacrosRead/TotalNumMacros * 100));
6822 if (TotalLexicalDeclContexts)
6823 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
6824 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
6825 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
6826 * 100));
6827 if (TotalVisibleDeclContexts)
6828 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
6829 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
6830 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
6831 * 100));
6832 if (TotalNumMethodPoolEntries) {
6833 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
6834 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
6835 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
6836 * 100));
Guy Benyei11169dd2012-12-18 14:30:41 +00006837 }
Douglas Gregorad2f7a52013-01-28 17:54:36 +00006838 if (NumMethodPoolLookups) {
6839 std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n",
6840 NumMethodPoolHits, NumMethodPoolLookups,
6841 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
6842 }
6843 if (NumMethodPoolTableLookups) {
6844 std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n",
6845 NumMethodPoolTableHits, NumMethodPoolTableLookups,
6846 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
6847 * 100.0));
6848 }
6849
Douglas Gregor00a50f72013-01-25 00:38:33 +00006850 if (NumIdentifierLookupHits) {
6851 std::fprintf(stderr,
6852 " %u / %u identifier table lookups succeeded (%f%%)\n",
6853 NumIdentifierLookupHits, NumIdentifierLookups,
6854 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
6855 }
6856
Douglas Gregore060e572013-01-25 01:03:03 +00006857 if (GlobalIndex) {
6858 std::fprintf(stderr, "\n");
6859 GlobalIndex->printStats();
6860 }
6861
Guy Benyei11169dd2012-12-18 14:30:41 +00006862 std::fprintf(stderr, "\n");
6863 dump();
6864 std::fprintf(stderr, "\n");
6865}
6866
6867template<typename Key, typename ModuleFile, unsigned InitialCapacity>
6868static void
6869dumpModuleIDMap(StringRef Name,
6870 const ContinuousRangeMap<Key, ModuleFile *,
6871 InitialCapacity> &Map) {
6872 if (Map.begin() == Map.end())
6873 return;
6874
6875 typedef ContinuousRangeMap<Key, ModuleFile *, InitialCapacity> MapType;
6876 llvm::errs() << Name << ":\n";
6877 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
6878 I != IEnd; ++I) {
6879 llvm::errs() << " " << I->first << " -> " << I->second->FileName
6880 << "\n";
6881 }
6882}
6883
Yaron Kerencdae9412016-01-29 19:38:18 +00006884LLVM_DUMP_METHOD void ASTReader::dump() {
Guy Benyei11169dd2012-12-18 14:30:41 +00006885 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
6886 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
6887 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
6888 dumpModuleIDMap("Global type map", GlobalTypeMap);
6889 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
6890 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
6891 dumpModuleIDMap("Global macro map", GlobalMacroMap);
6892 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
6893 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
6894 dumpModuleIDMap("Global preprocessed entity map",
6895 GlobalPreprocessedEntityMap);
6896
6897 llvm::errs() << "\n*** PCH/Modules Loaded:";
6898 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
6899 MEnd = ModuleMgr.end();
6900 M != MEnd; ++M)
6901 (*M)->dump();
6902}
6903
6904/// Return the amount of memory used by memory buffers, breaking down
6905/// by heap-backed versus mmap'ed memory.
6906void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
6907 for (ModuleConstIterator I = ModuleMgr.begin(),
6908 E = ModuleMgr.end(); I != E; ++I) {
6909 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
6910 size_t bytes = buf->getBufferSize();
6911 switch (buf->getBufferKind()) {
6912 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
6913 sizes.malloc_bytes += bytes;
6914 break;
6915 case llvm::MemoryBuffer::MemoryBuffer_MMap:
6916 sizes.mmap_bytes += bytes;
6917 break;
6918 }
6919 }
6920 }
6921}
6922
6923void ASTReader::InitializeSema(Sema &S) {
6924 SemaObj = &S;
6925 S.addExternalSource(this);
6926
6927 // Makes sure any declarations that were deserialized "too early"
6928 // still get added to the identifier's declaration chains.
Ben Langmuir5418f402014-09-10 21:29:41 +00006929 for (uint64_t ID : PreloadedDeclIDs) {
6930 NamedDecl *D = cast<NamedDecl>(GetDecl(ID));
6931 pushExternalDeclIntoScope(D, D->getDeclName());
Guy Benyei11169dd2012-12-18 14:30:41 +00006932 }
Ben Langmuir5418f402014-09-10 21:29:41 +00006933 PreloadedDeclIDs.clear();
Guy Benyei11169dd2012-12-18 14:30:41 +00006934
Richard Smith3d8e97e2013-10-18 06:54:39 +00006935 // FIXME: What happens if these are changed by a module import?
Guy Benyei11169dd2012-12-18 14:30:41 +00006936 if (!FPPragmaOptions.empty()) {
6937 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
6938 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
6939 }
6940
Richard Smith3d8e97e2013-10-18 06:54:39 +00006941 // FIXME: What happens if these are changed by a module import?
Guy Benyei11169dd2012-12-18 14:30:41 +00006942 if (!OpenCLExtensions.empty()) {
6943 unsigned I = 0;
6944#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
6945#include "clang/Basic/OpenCLExtensions.def"
6946
6947 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
6948 }
Richard Smith3d8e97e2013-10-18 06:54:39 +00006949
6950 UpdateSema();
6951}
6952
6953void ASTReader::UpdateSema() {
6954 assert(SemaObj && "no Sema to update");
6955
6956 // Load the offsets of the declarations that Sema references.
6957 // They will be lazily deserialized when needed.
6958 if (!SemaDeclRefs.empty()) {
6959 assert(SemaDeclRefs.size() % 2 == 0);
6960 for (unsigned I = 0; I != SemaDeclRefs.size(); I += 2) {
6961 if (!SemaObj->StdNamespace)
6962 SemaObj->StdNamespace = SemaDeclRefs[I];
6963 if (!SemaObj->StdBadAlloc)
6964 SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
6965 }
6966 SemaDeclRefs.clear();
6967 }
Dario Domizioli13a0a382014-05-23 12:13:25 +00006968
Nico Weber779355f2016-03-02 23:22:00 +00006969 // Update the state of pragmas. Use the same API as if we had encountered the
6970 // pragma in the source.
Dario Domizioli13a0a382014-05-23 12:13:25 +00006971 if(OptimizeOffPragmaLocation.isValid())
6972 SemaObj->ActOnPragmaOptimize(/* IsOn = */ false, OptimizeOffPragmaLocation);
Nico Weber779355f2016-03-02 23:22:00 +00006973 if (PragmaMSStructState != -1)
6974 SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState);
Nico Weber42932312016-03-03 00:17:35 +00006975 if (PointersToMembersPragmaLocation.isValid()) {
6976 SemaObj->ActOnPragmaMSPointersToMembers(
6977 (LangOptions::PragmaMSPointersToMembersKind)
6978 PragmaMSPointersToMembersState,
6979 PointersToMembersPragmaLocation);
6980 }
Guy Benyei11169dd2012-12-18 14:30:41 +00006981}
6982
Richard Smitha8d5b6a2015-07-17 19:51:03 +00006983IdentifierInfo *ASTReader::get(StringRef Name) {
Guy Benyei11169dd2012-12-18 14:30:41 +00006984 // Note that we are loading an identifier.
6985 Deserializing AnIdentifier(this);
Douglas Gregore060e572013-01-25 01:03:03 +00006986
Douglas Gregor7211ac12013-01-25 23:32:03 +00006987 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
Douglas Gregor00a50f72013-01-25 00:38:33 +00006988 NumIdentifierLookups,
6989 NumIdentifierLookupHits);
Richard Smith33e0f7e2015-07-22 02:08:40 +00006990
6991 // We don't need to do identifier table lookups in C++ modules (we preload
6992 // all interesting declarations, and don't need to use the scope for name
6993 // lookups). Perform the lookup in PCH files, though, since we don't build
6994 // a complete initial identifier table if we're carrying on from a PCH.
6995 if (Context.getLangOpts().CPlusPlus) {
6996 for (auto F : ModuleMgr.pch_modules())
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00006997 if (Visitor(*F))
Richard Smith33e0f7e2015-07-22 02:08:40 +00006998 break;
6999 } else {
7000 // If there is a global index, look there first to determine which modules
7001 // provably do not have any results for this identifier.
7002 GlobalModuleIndex::HitSet Hits;
7003 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
7004 if (!loadGlobalIndex()) {
7005 if (GlobalIndex->lookupIdentifier(Name, Hits)) {
7006 HitsPtr = &Hits;
7007 }
7008 }
7009
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00007010 ModuleMgr.visit(Visitor, HitsPtr);
Richard Smith33e0f7e2015-07-22 02:08:40 +00007011 }
7012
Guy Benyei11169dd2012-12-18 14:30:41 +00007013 IdentifierInfo *II = Visitor.getIdentifierInfo();
7014 markIdentifierUpToDate(II);
7015 return II;
7016}
7017
7018namespace clang {
7019 /// \brief An identifier-lookup iterator that enumerates all of the
7020 /// identifiers stored within a set of AST files.
7021 class ASTIdentifierIterator : public IdentifierIterator {
7022 /// \brief The AST reader whose identifiers are being enumerated.
7023 const ASTReader &Reader;
7024
7025 /// \brief The current index into the chain of AST files stored in
7026 /// the AST reader.
7027 unsigned Index;
7028
7029 /// \brief The current position within the identifier lookup table
7030 /// of the current AST file.
7031 ASTIdentifierLookupTable::key_iterator Current;
7032
7033 /// \brief The end position within the identifier lookup table of
7034 /// the current AST file.
7035 ASTIdentifierLookupTable::key_iterator End;
7036
Ben Langmuir537c5b52016-05-04 00:53:13 +00007037 /// \brief Whether to skip any modules in the ASTReader.
7038 bool SkipModules;
7039
Guy Benyei11169dd2012-12-18 14:30:41 +00007040 public:
Ben Langmuir537c5b52016-05-04 00:53:13 +00007041 explicit ASTIdentifierIterator(const ASTReader &Reader,
7042 bool SkipModules = false);
Guy Benyei11169dd2012-12-18 14:30:41 +00007043
Craig Topper3e89dfe2014-03-13 02:13:41 +00007044 StringRef Next() override;
Guy Benyei11169dd2012-12-18 14:30:41 +00007045 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +00007046}
Guy Benyei11169dd2012-12-18 14:30:41 +00007047
Ben Langmuir537c5b52016-05-04 00:53:13 +00007048ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader,
7049 bool SkipModules)
7050 : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007051}
7052
7053StringRef ASTIdentifierIterator::Next() {
7054 while (Current == End) {
7055 // If we have exhausted all of our AST files, we're done.
7056 if (Index == 0)
7057 return StringRef();
7058
7059 --Index;
Ben Langmuir537c5b52016-05-04 00:53:13 +00007060 ModuleFile &F = Reader.ModuleMgr[Index];
7061 if (SkipModules && F.isModule())
7062 continue;
7063
7064 ASTIdentifierLookupTable *IdTable =
7065 (ASTIdentifierLookupTable *)F.IdentifierLookupTable;
Guy Benyei11169dd2012-12-18 14:30:41 +00007066 Current = IdTable->key_begin();
7067 End = IdTable->key_end();
7068 }
7069
7070 // We have any identifiers remaining in the current AST file; return
7071 // the next one.
Douglas Gregorbfd73d72013-01-23 18:53:14 +00007072 StringRef Result = *Current;
Guy Benyei11169dd2012-12-18 14:30:41 +00007073 ++Current;
Douglas Gregorbfd73d72013-01-23 18:53:14 +00007074 return Result;
Guy Benyei11169dd2012-12-18 14:30:41 +00007075}
7076
Ben Langmuir537c5b52016-05-04 00:53:13 +00007077namespace {
7078/// A utility for appending two IdentifierIterators.
7079class ChainedIdentifierIterator : public IdentifierIterator {
7080 std::unique_ptr<IdentifierIterator> Current;
7081 std::unique_ptr<IdentifierIterator> Queued;
7082
7083public:
7084 ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First,
7085 std::unique_ptr<IdentifierIterator> Second)
7086 : Current(std::move(First)), Queued(std::move(Second)) {}
7087
7088 StringRef Next() override {
7089 if (!Current)
7090 return StringRef();
7091
7092 StringRef result = Current->Next();
7093 if (!result.empty())
7094 return result;
7095
7096 // Try the queued iterator, which may itself be empty.
7097 Current.reset();
7098 std::swap(Current, Queued);
7099 return Next();
7100 }
7101};
7102} // end anonymous namespace.
7103
Argyrios Kyrtzidis9aca3c62013-04-17 22:10:55 +00007104IdentifierIterator *ASTReader::getIdentifiers() {
Ben Langmuir537c5b52016-05-04 00:53:13 +00007105 if (!loadGlobalIndex()) {
7106 std::unique_ptr<IdentifierIterator> ReaderIter(
7107 new ASTIdentifierIterator(*this, /*SkipModules=*/true));
7108 std::unique_ptr<IdentifierIterator> ModulesIter(
7109 GlobalIndex->createIdentifierIterator());
7110 return new ChainedIdentifierIterator(std::move(ReaderIter),
7111 std::move(ModulesIter));
7112 }
Argyrios Kyrtzidis9aca3c62013-04-17 22:10:55 +00007113
Guy Benyei11169dd2012-12-18 14:30:41 +00007114 return new ASTIdentifierIterator(*this);
7115}
7116
7117namespace clang { namespace serialization {
7118 class ReadMethodPoolVisitor {
7119 ASTReader &Reader;
7120 Selector Sel;
7121 unsigned PriorGeneration;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00007122 unsigned InstanceBits;
7123 unsigned FactoryBits;
Nico Weberff4b35e2014-12-27 22:14:15 +00007124 bool InstanceHasMoreThanOneDecl;
7125 bool FactoryHasMoreThanOneDecl;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00007126 SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
7127 SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
Guy Benyei11169dd2012-12-18 14:30:41 +00007128
7129 public:
Nico Weber2e0c8f72014-12-27 03:58:08 +00007130 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
Guy Benyei11169dd2012-12-18 14:30:41 +00007131 unsigned PriorGeneration)
Nico Weber2e0c8f72014-12-27 03:58:08 +00007132 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration),
Nico Weberff4b35e2014-12-27 22:14:15 +00007133 InstanceBits(0), FactoryBits(0), InstanceHasMoreThanOneDecl(false),
7134 FactoryHasMoreThanOneDecl(false) {}
Nico Weber2e0c8f72014-12-27 03:58:08 +00007135
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00007136 bool operator()(ModuleFile &M) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007137 if (!M.SelectorLookupTable)
7138 return false;
7139
7140 // If we've already searched this module file, skip it now.
Richard Smithbdf2d932015-07-30 03:37:16 +00007141 if (M.Generation <= PriorGeneration)
Guy Benyei11169dd2012-12-18 14:30:41 +00007142 return true;
7143
Richard Smithbdf2d932015-07-30 03:37:16 +00007144 ++Reader.NumMethodPoolTableLookups;
Guy Benyei11169dd2012-12-18 14:30:41 +00007145 ASTSelectorLookupTable *PoolTable
7146 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
Richard Smithbdf2d932015-07-30 03:37:16 +00007147 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
Guy Benyei11169dd2012-12-18 14:30:41 +00007148 if (Pos == PoolTable->end())
7149 return false;
Douglas Gregorad2f7a52013-01-28 17:54:36 +00007150
Richard Smithbdf2d932015-07-30 03:37:16 +00007151 ++Reader.NumMethodPoolTableHits;
7152 ++Reader.NumSelectorsRead;
Guy Benyei11169dd2012-12-18 14:30:41 +00007153 // FIXME: Not quite happy with the statistics here. We probably should
7154 // disable this tracking when called via LoadSelector.
7155 // Also, should entries without methods count as misses?
Richard Smithbdf2d932015-07-30 03:37:16 +00007156 ++Reader.NumMethodPoolEntriesRead;
Guy Benyei11169dd2012-12-18 14:30:41 +00007157 ASTSelectorLookupTrait::data_type Data = *Pos;
Richard Smithbdf2d932015-07-30 03:37:16 +00007158 if (Reader.DeserializationListener)
7159 Reader.DeserializationListener->SelectorRead(Data.ID, Sel);
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00007160
Richard Smithbdf2d932015-07-30 03:37:16 +00007161 InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
7162 FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
7163 InstanceBits = Data.InstanceBits;
7164 FactoryBits = Data.FactoryBits;
7165 InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
7166 FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00007167 return true;
7168 }
7169
7170 /// \brief Retrieve the instance methods found by this visitor.
7171 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
7172 return InstanceMethods;
7173 }
7174
7175 /// \brief Retrieve the instance methods found by this visitor.
7176 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
7177 return FactoryMethods;
7178 }
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00007179
7180 unsigned getInstanceBits() const { return InstanceBits; }
7181 unsigned getFactoryBits() const { return FactoryBits; }
Nico Weberff4b35e2014-12-27 22:14:15 +00007182 bool instanceHasMoreThanOneDecl() const {
7183 return InstanceHasMoreThanOneDecl;
7184 }
7185 bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
Guy Benyei11169dd2012-12-18 14:30:41 +00007186 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +00007187} } // end namespace clang::serialization
Guy Benyei11169dd2012-12-18 14:30:41 +00007188
7189/// \brief Add the given set of methods to the method list.
7190static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
7191 ObjCMethodList &List) {
7192 for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
7193 S.addMethodToGlobalList(&List, Methods[I]);
7194 }
7195}
7196
7197void ASTReader::ReadMethodPool(Selector Sel) {
7198 // Get the selector generation and update it to the current generation.
7199 unsigned &Generation = SelectorGeneration[Sel];
7200 unsigned PriorGeneration = Generation;
Richard Smith053f6c62014-05-16 23:01:30 +00007201 Generation = getGeneration();
Manman Rena0f31a02016-04-29 19:04:05 +00007202 SelectorOutOfDate[Sel] = false;
Guy Benyei11169dd2012-12-18 14:30:41 +00007203
7204 // Search for methods defined with this selector.
Douglas Gregorad2f7a52013-01-28 17:54:36 +00007205 ++NumMethodPoolLookups;
Guy Benyei11169dd2012-12-18 14:30:41 +00007206 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00007207 ModuleMgr.visit(Visitor);
7208
Guy Benyei11169dd2012-12-18 14:30:41 +00007209 if (Visitor.getInstanceMethods().empty() &&
Douglas Gregorad2f7a52013-01-28 17:54:36 +00007210 Visitor.getFactoryMethods().empty())
Guy Benyei11169dd2012-12-18 14:30:41 +00007211 return;
Douglas Gregorad2f7a52013-01-28 17:54:36 +00007212
7213 ++NumMethodPoolHits;
7214
Guy Benyei11169dd2012-12-18 14:30:41 +00007215 if (!getSema())
7216 return;
7217
7218 Sema &S = *getSema();
7219 Sema::GlobalMethodPool::iterator Pos
7220 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
Ben Langmuira0c32e92015-01-12 19:27:00 +00007221
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00007222 Pos->second.first.setBits(Visitor.getInstanceBits());
Nico Weberff4b35e2014-12-27 22:14:15 +00007223 Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00007224 Pos->second.second.setBits(Visitor.getFactoryBits());
Nico Weberff4b35e2014-12-27 22:14:15 +00007225 Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
Ben Langmuira0c32e92015-01-12 19:27:00 +00007226
7227 // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
7228 // when building a module we keep every method individually and may need to
7229 // update hasMoreThanOneDecl as we add the methods.
7230 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
7231 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
Guy Benyei11169dd2012-12-18 14:30:41 +00007232}
7233
Manman Rena0f31a02016-04-29 19:04:05 +00007234void ASTReader::updateOutOfDateSelector(Selector Sel) {
7235 if (SelectorOutOfDate[Sel])
7236 ReadMethodPool(Sel);
7237}
7238
Guy Benyei11169dd2012-12-18 14:30:41 +00007239void ASTReader::ReadKnownNamespaces(
7240 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
7241 Namespaces.clear();
7242
7243 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
7244 if (NamespaceDecl *Namespace
7245 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
7246 Namespaces.push_back(Namespace);
7247 }
7248}
7249
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00007250void ASTReader::ReadUndefinedButUsed(
Richard Smithd6a04d72016-03-25 21:49:43 +00007251 llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00007252 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
7253 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
Nick Lewycky8334af82013-01-26 00:35:08 +00007254 SourceLocation Loc =
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00007255 SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
Nick Lewycky8334af82013-01-26 00:35:08 +00007256 Undefined.insert(std::make_pair(D, Loc));
7257 }
7258}
Nick Lewycky8334af82013-01-26 00:35:08 +00007259
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00007260void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
7261 FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
7262 Exprs) {
7263 for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
7264 FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++]));
7265 uint64_t Count = DelayedDeleteExprs[Idx++];
7266 for (uint64_t C = 0; C < Count; ++C) {
7267 SourceLocation DeleteLoc =
7268 SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]);
7269 const bool IsArrayForm = DelayedDeleteExprs[Idx++];
7270 Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm));
7271 }
7272 }
7273}
7274
Guy Benyei11169dd2012-12-18 14:30:41 +00007275void ASTReader::ReadTentativeDefinitions(
7276 SmallVectorImpl<VarDecl *> &TentativeDefs) {
7277 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
7278 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
7279 if (Var)
7280 TentativeDefs.push_back(Var);
7281 }
7282 TentativeDefinitions.clear();
7283}
7284
7285void ASTReader::ReadUnusedFileScopedDecls(
7286 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
7287 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
7288 DeclaratorDecl *D
7289 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
7290 if (D)
7291 Decls.push_back(D);
7292 }
7293 UnusedFileScopedDecls.clear();
7294}
7295
7296void ASTReader::ReadDelegatingConstructors(
7297 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
7298 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
7299 CXXConstructorDecl *D
7300 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
7301 if (D)
7302 Decls.push_back(D);
7303 }
7304 DelegatingCtorDecls.clear();
7305}
7306
7307void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
7308 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
7309 TypedefNameDecl *D
7310 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
7311 if (D)
7312 Decls.push_back(D);
7313 }
7314 ExtVectorDecls.clear();
7315}
7316
Nico Weber72889432014-09-06 01:25:55 +00007317void ASTReader::ReadUnusedLocalTypedefNameCandidates(
7318 llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
7319 for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
7320 ++I) {
7321 TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
7322 GetDecl(UnusedLocalTypedefNameCandidates[I]));
7323 if (D)
7324 Decls.insert(D);
7325 }
7326 UnusedLocalTypedefNameCandidates.clear();
7327}
7328
Guy Benyei11169dd2012-12-18 14:30:41 +00007329void ASTReader::ReadReferencedSelectors(
7330 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
7331 if (ReferencedSelectorsData.empty())
7332 return;
7333
7334 // If there are @selector references added them to its pool. This is for
7335 // implementation of -Wselector.
7336 unsigned int DataSize = ReferencedSelectorsData.size()-1;
7337 unsigned I = 0;
7338 while (I < DataSize) {
7339 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
7340 SourceLocation SelLoc
7341 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
7342 Sels.push_back(std::make_pair(Sel, SelLoc));
7343 }
7344 ReferencedSelectorsData.clear();
7345}
7346
7347void ASTReader::ReadWeakUndeclaredIdentifiers(
7348 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
7349 if (WeakUndeclaredIdentifiers.empty())
7350 return;
7351
7352 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
7353 IdentifierInfo *WeakId
7354 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
7355 IdentifierInfo *AliasId
7356 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
7357 SourceLocation Loc
7358 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
7359 bool Used = WeakUndeclaredIdentifiers[I++];
7360 WeakInfo WI(AliasId, Loc);
7361 WI.setUsed(Used);
7362 WeakIDs.push_back(std::make_pair(WeakId, WI));
7363 }
7364 WeakUndeclaredIdentifiers.clear();
7365}
7366
7367void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
7368 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
7369 ExternalVTableUse VT;
7370 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
7371 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
7372 VT.DefinitionRequired = VTableUses[Idx++];
7373 VTables.push_back(VT);
7374 }
7375
7376 VTableUses.clear();
7377}
7378
7379void ASTReader::ReadPendingInstantiations(
7380 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
7381 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
7382 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
7383 SourceLocation Loc
7384 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
7385
7386 Pending.push_back(std::make_pair(D, Loc));
7387 }
7388 PendingInstantiations.clear();
7389}
7390
Richard Smithe40f2ba2013-08-07 21:41:30 +00007391void ASTReader::ReadLateParsedTemplates(
Chandler Carruth52cee4d2015-03-26 09:08:15 +00007392 llvm::MapVector<const FunctionDecl *, LateParsedTemplate *> &LPTMap) {
Richard Smithe40f2ba2013-08-07 21:41:30 +00007393 for (unsigned Idx = 0, N = LateParsedTemplates.size(); Idx < N;
7394 /* In loop */) {
7395 FunctionDecl *FD = cast<FunctionDecl>(GetDecl(LateParsedTemplates[Idx++]));
7396
7397 LateParsedTemplate *LT = new LateParsedTemplate;
7398 LT->D = GetDecl(LateParsedTemplates[Idx++]);
7399
7400 ModuleFile *F = getOwningModuleFile(LT->D);
7401 assert(F && "No module");
7402
7403 unsigned TokN = LateParsedTemplates[Idx++];
7404 LT->Toks.reserve(TokN);
7405 for (unsigned T = 0; T < TokN; ++T)
7406 LT->Toks.push_back(ReadToken(*F, LateParsedTemplates, Idx));
7407
Chandler Carruth52cee4d2015-03-26 09:08:15 +00007408 LPTMap.insert(std::make_pair(FD, LT));
Richard Smithe40f2ba2013-08-07 21:41:30 +00007409 }
7410
7411 LateParsedTemplates.clear();
7412}
7413
Guy Benyei11169dd2012-12-18 14:30:41 +00007414void ASTReader::LoadSelector(Selector Sel) {
7415 // It would be complicated to avoid reading the methods anyway. So don't.
7416 ReadMethodPool(Sel);
7417}
7418
7419void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
7420 assert(ID && "Non-zero identifier ID required");
7421 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
7422 IdentifiersLoaded[ID - 1] = II;
7423 if (DeserializationListener)
7424 DeserializationListener->IdentifierRead(ID, II);
7425}
7426
7427/// \brief Set the globally-visible declarations associated with the given
7428/// identifier.
7429///
7430/// If the AST reader is currently in a state where the given declaration IDs
7431/// cannot safely be resolved, they are queued until it is safe to resolve
7432/// them.
7433///
7434/// \param II an IdentifierInfo that refers to one or more globally-visible
7435/// declarations.
7436///
7437/// \param DeclIDs the set of declaration IDs with the name @p II that are
7438/// visible at global scope.
7439///
Douglas Gregor6168bd22013-02-18 15:53:43 +00007440/// \param Decls if non-null, this vector will be populated with the set of
7441/// deserialized declarations. These declarations will not be pushed into
7442/// scope.
Guy Benyei11169dd2012-12-18 14:30:41 +00007443void
7444ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
7445 const SmallVectorImpl<uint32_t> &DeclIDs,
Douglas Gregor6168bd22013-02-18 15:53:43 +00007446 SmallVectorImpl<Decl *> *Decls) {
7447 if (NumCurrentElementsDeserializing && !Decls) {
7448 PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
Guy Benyei11169dd2012-12-18 14:30:41 +00007449 return;
7450 }
7451
7452 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
Ben Langmuir5418f402014-09-10 21:29:41 +00007453 if (!SemaObj) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007454 // Queue this declaration so that it will be added to the
7455 // translation unit scope and identifier's declaration chain
7456 // once a Sema object is known.
Ben Langmuir5418f402014-09-10 21:29:41 +00007457 PreloadedDeclIDs.push_back(DeclIDs[I]);
7458 continue;
Guy Benyei11169dd2012-12-18 14:30:41 +00007459 }
Ben Langmuir5418f402014-09-10 21:29:41 +00007460
7461 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
7462
7463 // If we're simply supposed to record the declarations, do so now.
7464 if (Decls) {
7465 Decls->push_back(D);
7466 continue;
7467 }
7468
7469 // Introduce this declaration into the translation-unit scope
7470 // and add it to the declaration chain for this identifier, so
7471 // that (unqualified) name lookup will find it.
7472 pushExternalDeclIntoScope(D, II);
Guy Benyei11169dd2012-12-18 14:30:41 +00007473 }
7474}
7475
Douglas Gregorc8a992f2013-01-21 16:52:34 +00007476IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007477 if (ID == 0)
Craig Toppera13603a2014-05-22 05:54:18 +00007478 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007479
7480 if (IdentifiersLoaded.empty()) {
7481 Error("no identifier table in AST file");
Craig Toppera13603a2014-05-22 05:54:18 +00007482 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007483 }
7484
7485 ID -= 1;
7486 if (!IdentifiersLoaded[ID]) {
7487 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
7488 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
7489 ModuleFile *M = I->second;
7490 unsigned Index = ID - M->BaseIdentifierID;
7491 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
7492
7493 // All of the strings in the AST file are preceded by a 16-bit length.
7494 // Extract that 16-bit length to avoid having to execute strlen().
7495 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
7496 // unsigned integers. This is important to avoid integer overflow when
7497 // we cast them to 'unsigned'.
7498 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
7499 unsigned StrLen = (((unsigned) StrLenPtr[0])
7500 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Richard Smitheb4b58f62016-02-05 01:40:54 +00007501 auto &II = PP.getIdentifierTable().get(StringRef(Str, StrLen));
7502 IdentifiersLoaded[ID] = &II;
7503 markIdentifierFromAST(*this, II);
Guy Benyei11169dd2012-12-18 14:30:41 +00007504 if (DeserializationListener)
Richard Smitheb4b58f62016-02-05 01:40:54 +00007505 DeserializationListener->IdentifierRead(ID + 1, &II);
Guy Benyei11169dd2012-12-18 14:30:41 +00007506 }
7507
7508 return IdentifiersLoaded[ID];
7509}
7510
Douglas Gregorc8a992f2013-01-21 16:52:34 +00007511IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
7512 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
Guy Benyei11169dd2012-12-18 14:30:41 +00007513}
7514
7515IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
7516 if (LocalID < NUM_PREDEF_IDENT_IDS)
7517 return LocalID;
7518
7519 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7520 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
7521 assert(I != M.IdentifierRemap.end()
7522 && "Invalid index into identifier index remap");
7523
7524 return LocalID + I->second;
7525}
7526
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00007527MacroInfo *ASTReader::getMacro(MacroID ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007528 if (ID == 0)
Craig Toppera13603a2014-05-22 05:54:18 +00007529 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007530
7531 if (MacrosLoaded.empty()) {
7532 Error("no macro table in AST file");
Craig Toppera13603a2014-05-22 05:54:18 +00007533 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007534 }
7535
7536 ID -= NUM_PREDEF_MACRO_IDS;
7537 if (!MacrosLoaded[ID]) {
7538 GlobalMacroMapType::iterator I
7539 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
7540 assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
7541 ModuleFile *M = I->second;
7542 unsigned Index = ID - M->BaseMacroID;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00007543 MacrosLoaded[ID] = ReadMacroRecord(*M, M->MacroOffsets[Index]);
7544
7545 if (DeserializationListener)
7546 DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
7547 MacrosLoaded[ID]);
Guy Benyei11169dd2012-12-18 14:30:41 +00007548 }
7549
7550 return MacrosLoaded[ID];
7551}
7552
7553MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
7554 if (LocalID < NUM_PREDEF_MACRO_IDS)
7555 return LocalID;
7556
7557 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7558 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
7559 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
7560
7561 return LocalID + I->second;
7562}
7563
7564serialization::SubmoduleID
7565ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
7566 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
7567 return LocalID;
7568
7569 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7570 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
7571 assert(I != M.SubmoduleRemap.end()
7572 && "Invalid index into submodule index remap");
7573
7574 return LocalID + I->second;
7575}
7576
7577Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
7578 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
7579 assert(GlobalID == 0 && "Unhandled global submodule ID");
Craig Toppera13603a2014-05-22 05:54:18 +00007580 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007581 }
7582
7583 if (GlobalID > SubmodulesLoaded.size()) {
7584 Error("submodule ID out of range in AST file");
Craig Toppera13603a2014-05-22 05:54:18 +00007585 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007586 }
7587
7588 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
7589}
Douglas Gregorc147b0b2013-01-12 01:29:50 +00007590
7591Module *ASTReader::getModule(unsigned ID) {
7592 return getSubmodule(ID);
7593}
7594
Richard Smithd88a7f12015-09-01 20:35:42 +00007595ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) {
7596 if (ID & 1) {
7597 // It's a module, look it up by submodule ID.
7598 auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1));
7599 return I == GlobalSubmoduleMap.end() ? nullptr : I->second;
7600 } else {
7601 // It's a prefix (preamble, PCH, ...). Look it up by index.
7602 unsigned IndexFromEnd = ID >> 1;
7603 assert(IndexFromEnd && "got reference to unknown module file");
7604 return getModuleManager().pch_modules().end()[-IndexFromEnd];
7605 }
7606}
7607
7608unsigned ASTReader::getModuleFileID(ModuleFile *F) {
7609 if (!F)
7610 return 1;
7611
7612 // For a file representing a module, use the submodule ID of the top-level
7613 // module as the file ID. For any other kind of file, the number of such
7614 // files loaded beforehand will be the same on reload.
7615 // FIXME: Is this true even if we have an explicit module file and a PCH?
7616 if (F->isModule())
7617 return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;
7618
7619 auto PCHModules = getModuleManager().pch_modules();
7620 auto I = std::find(PCHModules.begin(), PCHModules.end(), F);
7621 assert(I != PCHModules.end() && "emitting reference to unknown file");
7622 return (I - PCHModules.end()) << 1;
7623}
7624
Adrian Prantl15bcf702015-06-30 17:39:43 +00007625llvm::Optional<ExternalASTSource::ASTSourceDescriptor>
7626ASTReader::getSourceDescriptor(unsigned ID) {
7627 if (const Module *M = getSubmodule(ID))
Adrian Prantlc6458d62015-09-19 00:10:32 +00007628 return ExternalASTSource::ASTSourceDescriptor(*M);
Adrian Prantl15bcf702015-06-30 17:39:43 +00007629
7630 // If there is only a single PCH, return it instead.
7631 // Chained PCH are not suported.
7632 if (ModuleMgr.size() == 1) {
7633 ModuleFile &MF = ModuleMgr.getPrimaryModule();
Adrian Prantl3a2d4942016-01-22 23:30:56 +00007634 StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName);
Adrian Prantl9bc3c4f2016-04-27 17:06:22 +00007635 StringRef FileName = llvm::sys::path::filename(MF.FileName);
7636 return ASTReader::ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName,
7637 MF.Signature);
Adrian Prantl15bcf702015-06-30 17:39:43 +00007638 }
7639 return None;
7640}
7641
Guy Benyei11169dd2012-12-18 14:30:41 +00007642Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
7643 return DecodeSelector(getGlobalSelectorID(M, LocalID));
7644}
7645
7646Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
7647 if (ID == 0)
7648 return Selector();
7649
7650 if (ID > SelectorsLoaded.size()) {
7651 Error("selector ID out of range in AST file");
7652 return Selector();
7653 }
7654
Craig Toppera13603a2014-05-22 05:54:18 +00007655 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007656 // Load this selector from the selector table.
7657 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
7658 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
7659 ModuleFile &M = *I->second;
7660 ASTSelectorLookupTrait Trait(*this, M);
7661 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
7662 SelectorsLoaded[ID - 1] =
7663 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
7664 if (DeserializationListener)
7665 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
7666 }
7667
7668 return SelectorsLoaded[ID - 1];
7669}
7670
7671Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
7672 return DecodeSelector(ID);
7673}
7674
7675uint32_t ASTReader::GetNumExternalSelectors() {
7676 // ID 0 (the null selector) is considered an external selector.
7677 return getTotalNumSelectors() + 1;
7678}
7679
7680serialization::SelectorID
7681ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
7682 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
7683 return LocalID;
7684
7685 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7686 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
7687 assert(I != M.SelectorRemap.end()
7688 && "Invalid index into selector index remap");
7689
7690 return LocalID + I->second;
7691}
7692
7693DeclarationName
7694ASTReader::ReadDeclarationName(ModuleFile &F,
7695 const RecordData &Record, unsigned &Idx) {
7696 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
7697 switch (Kind) {
7698 case DeclarationName::Identifier:
Douglas Gregorc8a992f2013-01-21 16:52:34 +00007699 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00007700
7701 case DeclarationName::ObjCZeroArgSelector:
7702 case DeclarationName::ObjCOneArgSelector:
7703 case DeclarationName::ObjCMultiArgSelector:
7704 return DeclarationName(ReadSelector(F, Record, Idx));
7705
7706 case DeclarationName::CXXConstructorName:
7707 return Context.DeclarationNames.getCXXConstructorName(
7708 Context.getCanonicalType(readType(F, Record, Idx)));
7709
7710 case DeclarationName::CXXDestructorName:
7711 return Context.DeclarationNames.getCXXDestructorName(
7712 Context.getCanonicalType(readType(F, Record, Idx)));
7713
7714 case DeclarationName::CXXConversionFunctionName:
7715 return Context.DeclarationNames.getCXXConversionFunctionName(
7716 Context.getCanonicalType(readType(F, Record, Idx)));
7717
7718 case DeclarationName::CXXOperatorName:
7719 return Context.DeclarationNames.getCXXOperatorName(
7720 (OverloadedOperatorKind)Record[Idx++]);
7721
7722 case DeclarationName::CXXLiteralOperatorName:
7723 return Context.DeclarationNames.getCXXLiteralOperatorName(
7724 GetIdentifierInfo(F, Record, Idx));
7725
7726 case DeclarationName::CXXUsingDirective:
7727 return DeclarationName::getUsingDirectiveName();
7728 }
7729
7730 llvm_unreachable("Invalid NameKind!");
7731}
7732
7733void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
7734 DeclarationNameLoc &DNLoc,
7735 DeclarationName Name,
7736 const RecordData &Record, unsigned &Idx) {
7737 switch (Name.getNameKind()) {
7738 case DeclarationName::CXXConstructorName:
7739 case DeclarationName::CXXDestructorName:
7740 case DeclarationName::CXXConversionFunctionName:
7741 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
7742 break;
7743
7744 case DeclarationName::CXXOperatorName:
7745 DNLoc.CXXOperatorName.BeginOpNameLoc
7746 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
7747 DNLoc.CXXOperatorName.EndOpNameLoc
7748 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
7749 break;
7750
7751 case DeclarationName::CXXLiteralOperatorName:
7752 DNLoc.CXXLiteralOperatorName.OpNameLoc
7753 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
7754 break;
7755
7756 case DeclarationName::Identifier:
7757 case DeclarationName::ObjCZeroArgSelector:
7758 case DeclarationName::ObjCOneArgSelector:
7759 case DeclarationName::ObjCMultiArgSelector:
7760 case DeclarationName::CXXUsingDirective:
7761 break;
7762 }
7763}
7764
7765void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
7766 DeclarationNameInfo &NameInfo,
7767 const RecordData &Record, unsigned &Idx) {
7768 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
7769 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
7770 DeclarationNameLoc DNLoc;
7771 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
7772 NameInfo.setInfo(DNLoc);
7773}
7774
7775void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
7776 const RecordData &Record, unsigned &Idx) {
7777 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
7778 unsigned NumTPLists = Record[Idx++];
7779 Info.NumTemplParamLists = NumTPLists;
7780 if (NumTPLists) {
7781 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
7782 for (unsigned i=0; i != NumTPLists; ++i)
7783 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
7784 }
7785}
7786
7787TemplateName
7788ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
7789 unsigned &Idx) {
7790 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
7791 switch (Kind) {
7792 case TemplateName::Template:
7793 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
7794
7795 case TemplateName::OverloadedTemplate: {
7796 unsigned size = Record[Idx++];
7797 UnresolvedSet<8> Decls;
7798 while (size--)
7799 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
7800
7801 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
7802 }
7803
7804 case TemplateName::QualifiedTemplate: {
7805 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
7806 bool hasTemplKeyword = Record[Idx++];
7807 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
7808 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
7809 }
7810
7811 case TemplateName::DependentTemplate: {
7812 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
7813 if (Record[Idx++]) // isIdentifier
7814 return Context.getDependentTemplateName(NNS,
7815 GetIdentifierInfo(F, Record,
7816 Idx));
7817 return Context.getDependentTemplateName(NNS,
7818 (OverloadedOperatorKind)Record[Idx++]);
7819 }
7820
7821 case TemplateName::SubstTemplateTemplateParm: {
7822 TemplateTemplateParmDecl *param
7823 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
7824 if (!param) return TemplateName();
7825 TemplateName replacement = ReadTemplateName(F, Record, Idx);
7826 return Context.getSubstTemplateTemplateParm(param, replacement);
7827 }
7828
7829 case TemplateName::SubstTemplateTemplateParmPack: {
7830 TemplateTemplateParmDecl *Param
7831 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
7832 if (!Param)
7833 return TemplateName();
7834
7835 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
7836 if (ArgPack.getKind() != TemplateArgument::Pack)
7837 return TemplateName();
7838
7839 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
7840 }
7841 }
7842
7843 llvm_unreachable("Unhandled template name kind!");
7844}
7845
Richard Smith2bb3c342015-08-09 01:05:31 +00007846TemplateArgument ASTReader::ReadTemplateArgument(ModuleFile &F,
7847 const RecordData &Record,
7848 unsigned &Idx,
7849 bool Canonicalize) {
7850 if (Canonicalize) {
7851 // The caller wants a canonical template argument. Sometimes the AST only
7852 // wants template arguments in canonical form (particularly as the template
7853 // argument lists of template specializations) so ensure we preserve that
7854 // canonical form across serialization.
7855 TemplateArgument Arg = ReadTemplateArgument(F, Record, Idx, false);
7856 return Context.getCanonicalTemplateArgument(Arg);
7857 }
7858
Guy Benyei11169dd2012-12-18 14:30:41 +00007859 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
7860 switch (Kind) {
7861 case TemplateArgument::Null:
7862 return TemplateArgument();
7863 case TemplateArgument::Type:
7864 return TemplateArgument(readType(F, Record, Idx));
7865 case TemplateArgument::Declaration: {
7866 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
David Blaikie0f62c8d2014-10-16 04:21:25 +00007867 return TemplateArgument(D, readType(F, Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00007868 }
7869 case TemplateArgument::NullPtr:
7870 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
7871 case TemplateArgument::Integral: {
7872 llvm::APSInt Value = ReadAPSInt(Record, Idx);
7873 QualType T = readType(F, Record, Idx);
7874 return TemplateArgument(Context, Value, T);
7875 }
7876 case TemplateArgument::Template:
7877 return TemplateArgument(ReadTemplateName(F, Record, Idx));
7878 case TemplateArgument::TemplateExpansion: {
7879 TemplateName Name = ReadTemplateName(F, Record, Idx);
David Blaikie05785d12013-02-20 22:23:23 +00007880 Optional<unsigned> NumTemplateExpansions;
Guy Benyei11169dd2012-12-18 14:30:41 +00007881 if (unsigned NumExpansions = Record[Idx++])
7882 NumTemplateExpansions = NumExpansions - 1;
7883 return TemplateArgument(Name, NumTemplateExpansions);
7884 }
7885 case TemplateArgument::Expression:
7886 return TemplateArgument(ReadExpr(F));
7887 case TemplateArgument::Pack: {
7888 unsigned NumArgs = Record[Idx++];
7889 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
7890 for (unsigned I = 0; I != NumArgs; ++I)
7891 Args[I] = ReadTemplateArgument(F, Record, Idx);
Benjamin Kramercce63472015-08-05 09:40:22 +00007892 return TemplateArgument(llvm::makeArrayRef(Args, NumArgs));
Guy Benyei11169dd2012-12-18 14:30:41 +00007893 }
7894 }
7895
7896 llvm_unreachable("Unhandled template argument kind!");
7897}
7898
7899TemplateParameterList *
7900ASTReader::ReadTemplateParameterList(ModuleFile &F,
7901 const RecordData &Record, unsigned &Idx) {
7902 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
7903 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
7904 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
7905
7906 unsigned NumParams = Record[Idx++];
7907 SmallVector<NamedDecl *, 16> Params;
7908 Params.reserve(NumParams);
7909 while (NumParams--)
7910 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
7911
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00007912 // TODO: Concepts
Guy Benyei11169dd2012-12-18 14:30:41 +00007913 TemplateParameterList* TemplateParams =
7914 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00007915 Params, RAngleLoc, nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00007916 return TemplateParams;
7917}
7918
7919void
7920ASTReader::
Craig Topper5603df42013-07-05 19:34:19 +00007921ReadTemplateArgumentList(SmallVectorImpl<TemplateArgument> &TemplArgs,
Guy Benyei11169dd2012-12-18 14:30:41 +00007922 ModuleFile &F, const RecordData &Record,
Richard Smith2bb3c342015-08-09 01:05:31 +00007923 unsigned &Idx, bool Canonicalize) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007924 unsigned NumTemplateArgs = Record[Idx++];
7925 TemplArgs.reserve(NumTemplateArgs);
7926 while (NumTemplateArgs--)
Richard Smith2bb3c342015-08-09 01:05:31 +00007927 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx, Canonicalize));
Guy Benyei11169dd2012-12-18 14:30:41 +00007928}
7929
7930/// \brief Read a UnresolvedSet structure.
Richard Smitha4ba74c2013-08-30 04:46:40 +00007931void ASTReader::ReadUnresolvedSet(ModuleFile &F, LazyASTUnresolvedSet &Set,
Guy Benyei11169dd2012-12-18 14:30:41 +00007932 const RecordData &Record, unsigned &Idx) {
7933 unsigned NumDecls = Record[Idx++];
7934 Set.reserve(Context, NumDecls);
7935 while (NumDecls--) {
Richard Smitha4ba74c2013-08-30 04:46:40 +00007936 DeclID ID = ReadDeclID(F, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00007937 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
Richard Smitha4ba74c2013-08-30 04:46:40 +00007938 Set.addLazyDecl(Context, ID, AS);
Guy Benyei11169dd2012-12-18 14:30:41 +00007939 }
7940}
7941
7942CXXBaseSpecifier
7943ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
7944 const RecordData &Record, unsigned &Idx) {
7945 bool isVirtual = static_cast<bool>(Record[Idx++]);
7946 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
7947 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
7948 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
7949 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
7950 SourceRange Range = ReadSourceRange(F, Record, Idx);
7951 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
7952 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
7953 EllipsisLoc);
7954 Result.setInheritConstructors(inheritConstructors);
7955 return Result;
7956}
7957
Richard Smithc2bb8182015-03-24 06:36:48 +00007958CXXCtorInitializer **
Guy Benyei11169dd2012-12-18 14:30:41 +00007959ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
7960 unsigned &Idx) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007961 unsigned NumInitializers = Record[Idx++];
Richard Smithc2bb8182015-03-24 06:36:48 +00007962 assert(NumInitializers && "wrote ctor initializers but have no inits");
7963 auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
7964 for (unsigned i = 0; i != NumInitializers; ++i) {
7965 TypeSourceInfo *TInfo = nullptr;
7966 bool IsBaseVirtual = false;
7967 FieldDecl *Member = nullptr;
7968 IndirectFieldDecl *IndirectMember = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007969
Richard Smithc2bb8182015-03-24 06:36:48 +00007970 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
7971 switch (Type) {
7972 case CTOR_INITIALIZER_BASE:
7973 TInfo = GetTypeSourceInfo(F, Record, Idx);
7974 IsBaseVirtual = Record[Idx++];
7975 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00007976
Richard Smithc2bb8182015-03-24 06:36:48 +00007977 case CTOR_INITIALIZER_DELEGATING:
7978 TInfo = GetTypeSourceInfo(F, Record, Idx);
7979 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00007980
Richard Smithc2bb8182015-03-24 06:36:48 +00007981 case CTOR_INITIALIZER_MEMBER:
7982 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
7983 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00007984
Richard Smithc2bb8182015-03-24 06:36:48 +00007985 case CTOR_INITIALIZER_INDIRECT_MEMBER:
7986 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
7987 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00007988 }
Richard Smithc2bb8182015-03-24 06:36:48 +00007989
7990 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
7991 Expr *Init = ReadExpr(F);
7992 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
7993 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
7994 bool IsWritten = Record[Idx++];
7995 unsigned SourceOrderOrNumArrayIndices;
7996 SmallVector<VarDecl *, 8> Indices;
7997 if (IsWritten) {
7998 SourceOrderOrNumArrayIndices = Record[Idx++];
7999 } else {
8000 SourceOrderOrNumArrayIndices = Record[Idx++];
8001 Indices.reserve(SourceOrderOrNumArrayIndices);
8002 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
8003 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
8004 }
8005
8006 CXXCtorInitializer *BOMInit;
8007 if (Type == CTOR_INITIALIZER_BASE) {
8008 BOMInit = new (Context)
8009 CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
8010 RParenLoc, MemberOrEllipsisLoc);
8011 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
8012 BOMInit = new (Context)
8013 CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
8014 } else if (IsWritten) {
8015 if (Member)
8016 BOMInit = new (Context) CXXCtorInitializer(
8017 Context, Member, MemberOrEllipsisLoc, LParenLoc, Init, RParenLoc);
8018 else
8019 BOMInit = new (Context)
8020 CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
8021 LParenLoc, Init, RParenLoc);
8022 } else {
8023 if (IndirectMember) {
8024 assert(Indices.empty() && "Indirect field improperly initialized");
8025 BOMInit = new (Context)
8026 CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
8027 LParenLoc, Init, RParenLoc);
8028 } else {
8029 BOMInit = CXXCtorInitializer::Create(
8030 Context, Member, MemberOrEllipsisLoc, LParenLoc, Init, RParenLoc,
8031 Indices.data(), Indices.size());
8032 }
8033 }
8034
8035 if (IsWritten)
8036 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
8037 CtorInitializers[i] = BOMInit;
Guy Benyei11169dd2012-12-18 14:30:41 +00008038 }
8039
Richard Smithc2bb8182015-03-24 06:36:48 +00008040 return CtorInitializers;
Guy Benyei11169dd2012-12-18 14:30:41 +00008041}
8042
8043NestedNameSpecifier *
8044ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
8045 const RecordData &Record, unsigned &Idx) {
8046 unsigned N = Record[Idx++];
Craig Toppera13603a2014-05-22 05:54:18 +00008047 NestedNameSpecifier *NNS = nullptr, *Prev = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00008048 for (unsigned I = 0; I != N; ++I) {
8049 NestedNameSpecifier::SpecifierKind Kind
8050 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
8051 switch (Kind) {
8052 case NestedNameSpecifier::Identifier: {
8053 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
8054 NNS = NestedNameSpecifier::Create(Context, Prev, II);
8055 break;
8056 }
8057
8058 case NestedNameSpecifier::Namespace: {
8059 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
8060 NNS = NestedNameSpecifier::Create(Context, Prev, NS);
8061 break;
8062 }
8063
8064 case NestedNameSpecifier::NamespaceAlias: {
8065 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
8066 NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
8067 break;
8068 }
8069
8070 case NestedNameSpecifier::TypeSpec:
8071 case NestedNameSpecifier::TypeSpecWithTemplate: {
8072 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
8073 if (!T)
Craig Toppera13603a2014-05-22 05:54:18 +00008074 return nullptr;
8075
Guy Benyei11169dd2012-12-18 14:30:41 +00008076 bool Template = Record[Idx++];
8077 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
8078 break;
8079 }
8080
8081 case NestedNameSpecifier::Global: {
8082 NNS = NestedNameSpecifier::GlobalSpecifier(Context);
8083 // No associated value, and there can't be a prefix.
8084 break;
8085 }
Nikola Smiljanic67860242014-09-26 00:28:20 +00008086
8087 case NestedNameSpecifier::Super: {
8088 CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(F, Record, Idx);
8089 NNS = NestedNameSpecifier::SuperSpecifier(Context, RD);
8090 break;
8091 }
Guy Benyei11169dd2012-12-18 14:30:41 +00008092 }
8093 Prev = NNS;
8094 }
8095 return NNS;
8096}
8097
8098NestedNameSpecifierLoc
8099ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
8100 unsigned &Idx) {
8101 unsigned N = Record[Idx++];
8102 NestedNameSpecifierLocBuilder Builder;
8103 for (unsigned I = 0; I != N; ++I) {
8104 NestedNameSpecifier::SpecifierKind Kind
8105 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
8106 switch (Kind) {
8107 case NestedNameSpecifier::Identifier: {
8108 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
8109 SourceRange Range = ReadSourceRange(F, Record, Idx);
8110 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
8111 break;
8112 }
8113
8114 case NestedNameSpecifier::Namespace: {
8115 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
8116 SourceRange Range = ReadSourceRange(F, Record, Idx);
8117 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
8118 break;
8119 }
8120
8121 case NestedNameSpecifier::NamespaceAlias: {
8122 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
8123 SourceRange Range = ReadSourceRange(F, Record, Idx);
8124 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
8125 break;
8126 }
8127
8128 case NestedNameSpecifier::TypeSpec:
8129 case NestedNameSpecifier::TypeSpecWithTemplate: {
8130 bool Template = Record[Idx++];
8131 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
8132 if (!T)
8133 return NestedNameSpecifierLoc();
8134 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
8135
8136 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
8137 Builder.Extend(Context,
8138 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
8139 T->getTypeLoc(), ColonColonLoc);
8140 break;
8141 }
8142
8143 case NestedNameSpecifier::Global: {
8144 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
8145 Builder.MakeGlobal(Context, ColonColonLoc);
8146 break;
8147 }
Nikola Smiljanic67860242014-09-26 00:28:20 +00008148
8149 case NestedNameSpecifier::Super: {
8150 CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(F, Record, Idx);
8151 SourceRange Range = ReadSourceRange(F, Record, Idx);
8152 Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd());
8153 break;
8154 }
Guy Benyei11169dd2012-12-18 14:30:41 +00008155 }
8156 }
Nikola Smiljanic67860242014-09-26 00:28:20 +00008157
Guy Benyei11169dd2012-12-18 14:30:41 +00008158 return Builder.getWithLocInContext(Context);
8159}
8160
8161SourceRange
8162ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
8163 unsigned &Idx) {
8164 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
8165 SourceLocation end = ReadSourceLocation(F, Record, Idx);
8166 return SourceRange(beg, end);
8167}
8168
8169/// \brief Read an integral value
8170llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
8171 unsigned BitWidth = Record[Idx++];
8172 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
8173 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
8174 Idx += NumWords;
8175 return Result;
8176}
8177
8178/// \brief Read a signed integral value
8179llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
8180 bool isUnsigned = Record[Idx++];
8181 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
8182}
8183
8184/// \brief Read a floating-point value
Tim Northover178723a2013-01-22 09:46:51 +00008185llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record,
8186 const llvm::fltSemantics &Sem,
8187 unsigned &Idx) {
8188 return llvm::APFloat(Sem, ReadAPInt(Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00008189}
8190
8191// \brief Read a string
8192std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
8193 unsigned Len = Record[Idx++];
8194 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
8195 Idx += Len;
8196 return Result;
8197}
8198
Richard Smith7ed1bc92014-12-05 22:42:13 +00008199std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
8200 unsigned &Idx) {
8201 std::string Filename = ReadString(Record, Idx);
8202 ResolveImportedPath(F, Filename);
8203 return Filename;
8204}
8205
Guy Benyei11169dd2012-12-18 14:30:41 +00008206VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
8207 unsigned &Idx) {
8208 unsigned Major = Record[Idx++];
8209 unsigned Minor = Record[Idx++];
8210 unsigned Subminor = Record[Idx++];
8211 if (Minor == 0)
8212 return VersionTuple(Major);
8213 if (Subminor == 0)
8214 return VersionTuple(Major, Minor - 1);
8215 return VersionTuple(Major, Minor - 1, Subminor - 1);
8216}
8217
8218CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
8219 const RecordData &Record,
8220 unsigned &Idx) {
8221 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
8222 return CXXTemporary::Create(Context, Decl);
8223}
8224
8225DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Argyrios Kyrtzidisdc9fdaf2013-05-24 05:44:08 +00008226 return Diag(CurrentImportLoc, DiagID);
Guy Benyei11169dd2012-12-18 14:30:41 +00008227}
8228
8229DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
8230 return Diags.Report(Loc, DiagID);
8231}
8232
8233/// \brief Retrieve the identifier table associated with the
8234/// preprocessor.
8235IdentifierTable &ASTReader::getIdentifierTable() {
8236 return PP.getIdentifierTable();
8237}
8238
8239/// \brief Record that the given ID maps to the given switch-case
8240/// statement.
8241void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Craig Toppera13603a2014-05-22 05:54:18 +00008242 assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
Guy Benyei11169dd2012-12-18 14:30:41 +00008243 "Already have a SwitchCase with this ID");
8244 (*CurrSwitchCaseStmts)[ID] = SC;
8245}
8246
8247/// \brief Retrieve the switch-case statement with the given ID.
8248SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Craig Toppera13603a2014-05-22 05:54:18 +00008249 assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
Guy Benyei11169dd2012-12-18 14:30:41 +00008250 return (*CurrSwitchCaseStmts)[ID];
8251}
8252
8253void ASTReader::ClearSwitchCaseIDs() {
8254 CurrSwitchCaseStmts->clear();
8255}
8256
8257void ASTReader::ReadComments() {
8258 std::vector<RawComment *> Comments;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00008259 for (SmallVectorImpl<std::pair<BitstreamCursor,
Guy Benyei11169dd2012-12-18 14:30:41 +00008260 serialization::ModuleFile *> >::iterator
8261 I = CommentsCursors.begin(),
8262 E = CommentsCursors.end();
8263 I != E; ++I) {
Dmitri Gribenko9ee0e302014-03-27 15:40:39 +00008264 Comments.clear();
Chris Lattner7fb3bef2013-01-20 00:56:42 +00008265 BitstreamCursor &Cursor = I->first;
Guy Benyei11169dd2012-12-18 14:30:41 +00008266 serialization::ModuleFile &F = *I->second;
8267 SavedStreamPosition SavedPosition(Cursor);
8268
8269 RecordData Record;
8270 while (true) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00008271 llvm::BitstreamEntry Entry =
8272 Cursor.advanceSkippingSubblocks(BitstreamCursor::AF_DontPopBlockAtEnd);
Dmitri Gribenko9ee0e302014-03-27 15:40:39 +00008273
Chris Lattner7fb3bef2013-01-20 00:56:42 +00008274 switch (Entry.Kind) {
8275 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
8276 case llvm::BitstreamEntry::Error:
8277 Error("malformed block record in AST file");
8278 return;
8279 case llvm::BitstreamEntry::EndBlock:
8280 goto NextCursor;
8281 case llvm::BitstreamEntry::Record:
8282 // The interesting case.
Guy Benyei11169dd2012-12-18 14:30:41 +00008283 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00008284 }
8285
8286 // Read a record.
8287 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00008288 switch ((CommentRecordTypes)Cursor.readRecord(Entry.ID, Record)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008289 case COMMENTS_RAW_COMMENT: {
8290 unsigned Idx = 0;
8291 SourceRange SR = ReadSourceRange(F, Record, Idx);
8292 RawComment::CommentKind Kind =
8293 (RawComment::CommentKind) Record[Idx++];
8294 bool IsTrailingComment = Record[Idx++];
8295 bool IsAlmostTrailingComment = Record[Idx++];
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00008296 Comments.push_back(new (Context) RawComment(
8297 SR, Kind, IsTrailingComment, IsAlmostTrailingComment,
8298 Context.getLangOpts().CommentOpts.ParseAllComments));
Guy Benyei11169dd2012-12-18 14:30:41 +00008299 break;
8300 }
8301 }
8302 }
Dmitri Gribenko9ee0e302014-03-27 15:40:39 +00008303 NextCursor:
8304 Context.Comments.addDeserializedComments(Comments);
Guy Benyei11169dd2012-12-18 14:30:41 +00008305 }
Guy Benyei11169dd2012-12-18 14:30:41 +00008306}
8307
Richard Smithcd45dbc2014-04-19 03:48:30 +00008308std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) {
8309 // If we know the owning module, use it.
Richard Smith42413142015-05-15 20:05:43 +00008310 if (Module *M = D->getImportedOwningModule())
Richard Smithcd45dbc2014-04-19 03:48:30 +00008311 return M->getFullModuleName();
8312
8313 // Otherwise, use the name of the top-level module the decl is within.
8314 if (ModuleFile *M = getOwningModuleFile(D))
8315 return M->ModuleName;
8316
8317 // Not from a module.
8318 return "";
8319}
8320
Guy Benyei11169dd2012-12-18 14:30:41 +00008321void ASTReader::finishPendingActions() {
Richard Smith851072e2014-05-19 20:59:20 +00008322 while (!PendingIdentifierInfos.empty() ||
8323 !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() ||
Richard Smith2b9e3e32013-10-18 06:05:18 +00008324 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
Richard Smitha0ce9c42014-07-29 23:23:27 +00008325 !PendingUpdateRecords.empty()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008326 // If any identifiers with corresponding top-level declarations have
8327 // been loaded, load those declarations now.
Craig Topper79be4cd2013-07-05 04:33:53 +00008328 typedef llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2> >
8329 TopLevelDeclsMap;
8330 TopLevelDeclsMap TopLevelDecls;
8331
Guy Benyei11169dd2012-12-18 14:30:41 +00008332 while (!PendingIdentifierInfos.empty()) {
Douglas Gregor6168bd22013-02-18 15:53:43 +00008333 IdentifierInfo *II = PendingIdentifierInfos.back().first;
Richard Smithf0ae3c2d2014-03-28 17:31:23 +00008334 SmallVector<uint32_t, 4> DeclIDs =
8335 std::move(PendingIdentifierInfos.back().second);
Douglas Gregorcb15f082013-02-19 18:26:28 +00008336 PendingIdentifierInfos.pop_back();
Douglas Gregor6168bd22013-02-18 15:53:43 +00008337
8338 SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
Guy Benyei11169dd2012-12-18 14:30:41 +00008339 }
Richard Smithf0ae3c2d2014-03-28 17:31:23 +00008340
Richard Smith851072e2014-05-19 20:59:20 +00008341 // For each decl chain that we wanted to complete while deserializing, mark
8342 // it as "still needs to be completed".
8343 for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
8344 markIncompleteDeclChain(PendingIncompleteDeclChains[I]);
8345 }
8346 PendingIncompleteDeclChains.clear();
8347
Guy Benyei11169dd2012-12-18 14:30:41 +00008348 // Load pending declaration chains.
Richard Smithd8a83712015-08-22 01:47:18 +00008349 for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
Richard Smithd61d4ac2015-08-22 20:13:39 +00008350 loadPendingDeclChain(PendingDeclChains[I].first, PendingDeclChains[I].second);
Guy Benyei11169dd2012-12-18 14:30:41 +00008351 PendingDeclChains.clear();
8352
Douglas Gregor6168bd22013-02-18 15:53:43 +00008353 // Make the most recent of the top-level declarations visible.
Craig Topper79be4cd2013-07-05 04:33:53 +00008354 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
8355 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
Douglas Gregor6168bd22013-02-18 15:53:43 +00008356 IdentifierInfo *II = TLD->first;
8357 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +00008358 pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
Douglas Gregor6168bd22013-02-18 15:53:43 +00008359 }
8360 }
8361
Guy Benyei11169dd2012-12-18 14:30:41 +00008362 // Load any pending macro definitions.
8363 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00008364 IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
8365 SmallVector<PendingMacroInfo, 2> GlobalIDs;
8366 GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
8367 // Initialize the macro history from chained-PCHs ahead of module imports.
Richard Smith49f906a2014-03-01 00:08:04 +00008368 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00008369 ++IDIdx) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00008370 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
Manman Ren11f2a472016-08-18 17:42:15 +00008371 if (!Info.M->isModule())
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00008372 resolvePendingMacro(II, Info);
8373 }
8374 // Handle module imports.
Richard Smith49f906a2014-03-01 00:08:04 +00008375 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00008376 ++IDIdx) {
8377 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
Manman Ren11f2a472016-08-18 17:42:15 +00008378 if (Info.M->isModule())
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00008379 resolvePendingMacro(II, Info);
Guy Benyei11169dd2012-12-18 14:30:41 +00008380 }
8381 }
8382 PendingMacroIDs.clear();
Argyrios Kyrtzidis83a6e3b2013-02-16 00:48:59 +00008383
8384 // Wire up the DeclContexts for Decls that we delayed setting until
8385 // recursive loading is completed.
8386 while (!PendingDeclContextInfos.empty()) {
8387 PendingDeclContextInfo Info = PendingDeclContextInfos.front();
8388 PendingDeclContextInfos.pop_front();
8389 DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
8390 DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
8391 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
8392 }
Richard Smith2b9e3e32013-10-18 06:05:18 +00008393
Richard Smithd1c46742014-04-30 02:24:17 +00008394 // Perform any pending declaration updates.
Richard Smithd6db68c2014-08-07 20:58:41 +00008395 while (!PendingUpdateRecords.empty()) {
Richard Smithd1c46742014-04-30 02:24:17 +00008396 auto Update = PendingUpdateRecords.pop_back_val();
8397 ReadingKindTracker ReadingKind(Read_Decl, *this);
8398 loadDeclUpdateRecords(Update.first, Update.second);
8399 }
Guy Benyei11169dd2012-12-18 14:30:41 +00008400 }
Richard Smith8a639892015-01-24 01:07:20 +00008401
8402 // At this point, all update records for loaded decls are in place, so any
8403 // fake class definitions should have become real.
8404 assert(PendingFakeDefinitionData.empty() &&
8405 "faked up a class definition but never saw the real one");
8406
Guy Benyei11169dd2012-12-18 14:30:41 +00008407 // If we deserialized any C++ or Objective-C class definitions, any
8408 // Objective-C protocol definitions, or any redeclarable templates, make sure
8409 // that all redeclarations point to the definitions. Note that this can only
8410 // happen now, after the redeclaration chains have been fully wired.
Craig Topperc6914d02014-08-25 04:15:02 +00008411 for (Decl *D : PendingDefinitions) {
8412 if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
Richard Smith5b21db82014-04-23 18:20:42 +00008413 if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008414 // Make sure that the TagType points at the definition.
8415 const_cast<TagType*>(TagT)->decl = TD;
8416 }
Richard Smith8ce51082015-03-11 01:44:51 +00008417
Craig Topperc6914d02014-08-25 04:15:02 +00008418 if (auto RD = dyn_cast<CXXRecordDecl>(D)) {
Richard Smith8ce51082015-03-11 01:44:51 +00008419 for (auto *R = getMostRecentExistingDecl(RD); R;
8420 R = R->getPreviousDecl()) {
8421 assert((R == D) ==
8422 cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
Richard Smith2c381642014-08-27 23:11:59 +00008423 "declaration thinks it's the definition but it isn't");
Aaron Ballman86c93902014-03-06 23:45:36 +00008424 cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
Richard Smith2c381642014-08-27 23:11:59 +00008425 }
Guy Benyei11169dd2012-12-18 14:30:41 +00008426 }
8427
8428 continue;
8429 }
Richard Smith8ce51082015-03-11 01:44:51 +00008430
Craig Topperc6914d02014-08-25 04:15:02 +00008431 if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008432 // Make sure that the ObjCInterfaceType points at the definition.
8433 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
8434 ->Decl = ID;
Richard Smith8ce51082015-03-11 01:44:51 +00008435
8436 for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl())
8437 cast<ObjCInterfaceDecl>(R)->Data = ID->Data;
8438
Guy Benyei11169dd2012-12-18 14:30:41 +00008439 continue;
8440 }
Richard Smith8ce51082015-03-11 01:44:51 +00008441
Craig Topperc6914d02014-08-25 04:15:02 +00008442 if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) {
Richard Smith8ce51082015-03-11 01:44:51 +00008443 for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl())
8444 cast<ObjCProtocolDecl>(R)->Data = PD->Data;
8445
Guy Benyei11169dd2012-12-18 14:30:41 +00008446 continue;
8447 }
Richard Smith8ce51082015-03-11 01:44:51 +00008448
Craig Topperc6914d02014-08-25 04:15:02 +00008449 auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl();
Richard Smith8ce51082015-03-11 01:44:51 +00008450 for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl())
8451 cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common;
Guy Benyei11169dd2012-12-18 14:30:41 +00008452 }
8453 PendingDefinitions.clear();
8454
8455 // Load the bodies of any functions or methods we've encountered. We do
8456 // this now (delayed) so that we can be sure that the declaration chains
Richard Smithb9fa9962015-08-21 03:04:33 +00008457 // have been fully wired up (hasBody relies on this).
8458 // FIXME: We shouldn't require complete redeclaration chains here.
Guy Benyei11169dd2012-12-18 14:30:41 +00008459 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
8460 PBEnd = PendingBodies.end();
8461 PB != PBEnd; ++PB) {
8462 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
8463 // FIXME: Check for =delete/=default?
8464 // FIXME: Complain about ODR violations here?
8465 if (!getContext().getLangOpts().Modules || !FD->hasBody())
8466 FD->setLazyBody(PB->second);
8467 continue;
8468 }
8469
8470 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
8471 if (!getContext().getLangOpts().Modules || !MD->hasBody())
8472 MD->setLazyBody(PB->second);
8473 }
8474 PendingBodies.clear();
Richard Smith42413142015-05-15 20:05:43 +00008475
8476 // Do some cleanup.
8477 for (auto *ND : PendingMergedDefinitionsToDeduplicate)
8478 getContext().deduplicateMergedDefinitonsFor(ND);
8479 PendingMergedDefinitionsToDeduplicate.clear();
Richard Smitha0ce9c42014-07-29 23:23:27 +00008480}
8481
8482void ASTReader::diagnoseOdrViolations() {
Richard Smithbb853c72014-08-13 01:23:33 +00008483 if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty())
8484 return;
8485
Richard Smitha0ce9c42014-07-29 23:23:27 +00008486 // Trigger the import of the full definition of each class that had any
8487 // odr-merging problems, so we can produce better diagnostics for them.
Richard Smithbb853c72014-08-13 01:23:33 +00008488 // These updates may in turn find and diagnose some ODR failures, so take
8489 // ownership of the set first.
8490 auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
8491 PendingOdrMergeFailures.clear();
8492 for (auto &Merge : OdrMergeFailures) {
Richard Smitha0ce9c42014-07-29 23:23:27 +00008493 Merge.first->buildLookup();
8494 Merge.first->decls_begin();
8495 Merge.first->bases_begin();
8496 Merge.first->vbases_begin();
8497 for (auto *RD : Merge.second) {
8498 RD->decls_begin();
8499 RD->bases_begin();
8500 RD->vbases_begin();
8501 }
8502 }
8503
8504 // For each declaration from a merged context, check that the canonical
8505 // definition of that context also contains a declaration of the same
8506 // entity.
8507 //
8508 // Caution: this loop does things that might invalidate iterators into
8509 // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
8510 while (!PendingOdrMergeChecks.empty()) {
8511 NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
8512
8513 // FIXME: Skip over implicit declarations for now. This matters for things
8514 // like implicitly-declared special member functions. This isn't entirely
8515 // correct; we can end up with multiple unmerged declarations of the same
8516 // implicit entity.
8517 if (D->isImplicit())
8518 continue;
8519
8520 DeclContext *CanonDef = D->getDeclContext();
Richard Smitha0ce9c42014-07-29 23:23:27 +00008521
8522 bool Found = false;
8523 const Decl *DCanon = D->getCanonicalDecl();
8524
Richard Smith01bdb7a2014-08-28 05:44:07 +00008525 for (auto RI : D->redecls()) {
8526 if (RI->getLexicalDeclContext() == CanonDef) {
8527 Found = true;
8528 break;
8529 }
8530 }
8531 if (Found)
8532 continue;
8533
Richard Smith0f4e2c42015-08-06 04:23:48 +00008534 // Quick check failed, time to do the slow thing. Note, we can't just
8535 // look up the name of D in CanonDef here, because the member that is
8536 // in CanonDef might not be found by name lookup (it might have been
8537 // replaced by a more recent declaration in the lookup table), and we
8538 // can't necessarily find it in the redeclaration chain because it might
8539 // be merely mergeable, not redeclarable.
Richard Smitha0ce9c42014-07-29 23:23:27 +00008540 llvm::SmallVector<const NamedDecl*, 4> Candidates;
Richard Smith0f4e2c42015-08-06 04:23:48 +00008541 for (auto *CanonMember : CanonDef->decls()) {
8542 if (CanonMember->getCanonicalDecl() == DCanon) {
8543 // This can happen if the declaration is merely mergeable and not
8544 // actually redeclarable (we looked for redeclarations earlier).
8545 //
8546 // FIXME: We should be able to detect this more efficiently, without
8547 // pulling in all of the members of CanonDef.
8548 Found = true;
8549 break;
Richard Smitha0ce9c42014-07-29 23:23:27 +00008550 }
Richard Smith0f4e2c42015-08-06 04:23:48 +00008551 if (auto *ND = dyn_cast<NamedDecl>(CanonMember))
8552 if (ND->getDeclName() == D->getDeclName())
8553 Candidates.push_back(ND);
Richard Smitha0ce9c42014-07-29 23:23:27 +00008554 }
8555
8556 if (!Found) {
Richard Smithd08aeb62014-08-28 01:33:39 +00008557 // The AST doesn't like TagDecls becoming invalid after they've been
8558 // completed. We only really need to mark FieldDecls as invalid here.
8559 if (!isa<TagDecl>(D))
8560 D->setInvalidDecl();
Richard Smith4ab3dbd2015-02-13 22:43:51 +00008561
8562 // Ensure we don't accidentally recursively enter deserialization while
8563 // we're producing our diagnostic.
8564 Deserializing RecursionGuard(this);
Richard Smitha0ce9c42014-07-29 23:23:27 +00008565
8566 std::string CanonDefModule =
8567 getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef));
8568 Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
8569 << D << getOwningModuleNameForDiagnostic(D)
8570 << CanonDef << CanonDefModule.empty() << CanonDefModule;
8571
8572 if (Candidates.empty())
8573 Diag(cast<Decl>(CanonDef)->getLocation(),
8574 diag::note_module_odr_violation_no_possible_decls) << D;
8575 else {
8576 for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
8577 Diag(Candidates[I]->getLocation(),
8578 diag::note_module_odr_violation_possible_decl)
8579 << Candidates[I];
8580 }
8581
8582 DiagnosedOdrMergeFailures.insert(CanonDef);
8583 }
8584 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00008585
Richard Smith4ab3dbd2015-02-13 22:43:51 +00008586 if (OdrMergeFailures.empty())
8587 return;
8588
8589 // Ensure we don't accidentally recursively enter deserialization while
8590 // we're producing our diagnostics.
8591 Deserializing RecursionGuard(this);
8592
Richard Smithcd45dbc2014-04-19 03:48:30 +00008593 // Issue any pending ODR-failure diagnostics.
Richard Smithbb853c72014-08-13 01:23:33 +00008594 for (auto &Merge : OdrMergeFailures) {
Richard Smitha0ce9c42014-07-29 23:23:27 +00008595 // If we've already pointed out a specific problem with this class, don't
8596 // bother issuing a general "something's different" diagnostic.
David Blaikie82e95a32014-11-19 07:49:47 +00008597 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
Richard Smithcd45dbc2014-04-19 03:48:30 +00008598 continue;
8599
8600 bool Diagnosed = false;
8601 for (auto *RD : Merge.second) {
8602 // Multiple different declarations got merged together; tell the user
8603 // where they came from.
8604 if (Merge.first != RD) {
8605 // FIXME: Walk the definition, figure out what's different,
8606 // and diagnose that.
8607 if (!Diagnosed) {
8608 std::string Module = getOwningModuleNameForDiagnostic(Merge.first);
8609 Diag(Merge.first->getLocation(),
8610 diag::err_module_odr_violation_different_definitions)
8611 << Merge.first << Module.empty() << Module;
8612 Diagnosed = true;
8613 }
8614
8615 Diag(RD->getLocation(),
8616 diag::note_module_odr_violation_different_definitions)
8617 << getOwningModuleNameForDiagnostic(RD);
8618 }
8619 }
8620
8621 if (!Diagnosed) {
8622 // All definitions are updates to the same declaration. This happens if a
8623 // module instantiates the declaration of a class template specialization
8624 // and two or more other modules instantiate its definition.
8625 //
8626 // FIXME: Indicate which modules had instantiations of this definition.
8627 // FIXME: How can this even happen?
8628 Diag(Merge.first->getLocation(),
8629 diag::err_module_odr_violation_different_instantiations)
8630 << Merge.first;
8631 }
8632 }
Guy Benyei11169dd2012-12-18 14:30:41 +00008633}
8634
Richard Smithce18a182015-07-14 00:26:00 +00008635void ASTReader::StartedDeserializing() {
8636 if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get())
8637 ReadTimer->startTimer();
8638}
8639
Guy Benyei11169dd2012-12-18 14:30:41 +00008640void ASTReader::FinishedDeserializing() {
8641 assert(NumCurrentElementsDeserializing &&
8642 "FinishedDeserializing not paired with StartedDeserializing");
8643 if (NumCurrentElementsDeserializing == 1) {
8644 // We decrease NumCurrentElementsDeserializing only after pending actions
8645 // are finished, to avoid recursively re-calling finishPendingActions().
8646 finishPendingActions();
8647 }
8648 --NumCurrentElementsDeserializing;
8649
Richard Smitha0ce9c42014-07-29 23:23:27 +00008650 if (NumCurrentElementsDeserializing == 0) {
Richard Smith9e2341d2015-03-23 03:25:59 +00008651 // Propagate exception specification updates along redeclaration chains.
Richard Smith7226f2a2015-03-23 19:54:56 +00008652 while (!PendingExceptionSpecUpdates.empty()) {
8653 auto Updates = std::move(PendingExceptionSpecUpdates);
8654 PendingExceptionSpecUpdates.clear();
8655 for (auto Update : Updates) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00008656 ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
Richard Smith7226f2a2015-03-23 19:54:56 +00008657 auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
Richard Smith1d0f1992015-08-19 21:09:32 +00008658 auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
Richard Smithd88a7f12015-09-01 20:35:42 +00008659 if (auto *Listener = Context.getASTMutationListener())
8660 Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second));
Richard Smith1d0f1992015-08-19 21:09:32 +00008661 for (auto *Redecl : Update.second->redecls())
8662 Context.adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI);
Richard Smith7226f2a2015-03-23 19:54:56 +00008663 }
Richard Smith9e2341d2015-03-23 03:25:59 +00008664 }
8665
Richard Smithce18a182015-07-14 00:26:00 +00008666 if (ReadTimer)
8667 ReadTimer->stopTimer();
8668
Richard Smith0f4e2c42015-08-06 04:23:48 +00008669 diagnoseOdrViolations();
8670
Richard Smith04d05b52014-03-23 00:27:18 +00008671 // We are not in recursive loading, so it's safe to pass the "interesting"
8672 // decls to the consumer.
Richard Smitha0ce9c42014-07-29 23:23:27 +00008673 if (Consumer)
8674 PassInterestingDeclsToConsumer();
Guy Benyei11169dd2012-12-18 14:30:41 +00008675 }
8676}
8677
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +00008678void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
Richard Smith9e2341d2015-03-23 03:25:59 +00008679 if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
8680 // Remove any fake results before adding any real ones.
8681 auto It = PendingFakeLookupResults.find(II);
8682 if (It != PendingFakeLookupResults.end()) {
Richard Smitha534a312015-07-21 23:54:07 +00008683 for (auto *ND : It->second)
Richard Smith9e2341d2015-03-23 03:25:59 +00008684 SemaObj->IdResolver.RemoveDecl(ND);
Ben Langmuireb8bd2d2015-04-10 22:25:42 +00008685 // FIXME: this works around module+PCH performance issue.
8686 // Rather than erase the result from the map, which is O(n), just clear
8687 // the vector of NamedDecls.
8688 It->second.clear();
Richard Smith9e2341d2015-03-23 03:25:59 +00008689 }
8690 }
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +00008691
8692 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
8693 SemaObj->TUScope->AddDecl(D);
8694 } else if (SemaObj->TUScope) {
8695 // Adding the decl to IdResolver may have failed because it was already in
8696 // (even though it was not added in scope). If it is already in, make sure
8697 // it gets in the scope as well.
8698 if (std::find(SemaObj->IdResolver.begin(Name),
8699 SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
8700 SemaObj->TUScope->AddDecl(D);
8701 }
8702}
8703
Douglas Gregor6623e1f2015-11-03 18:33:07 +00008704ASTReader::ASTReader(
8705 Preprocessor &PP, ASTContext &Context,
8706 const PCHContainerReader &PCHContainerRdr,
8707 ArrayRef<IntrusiveRefCntPtr<ModuleFileExtension>> Extensions,
8708 StringRef isysroot, bool DisableValidation,
8709 bool AllowASTWithCompilerErrors,
8710 bool AllowConfigurationMismatch, bool ValidateSystemInputs,
8711 bool UseGlobalIndex,
8712 std::unique_ptr<llvm::Timer> ReadTimer)
Craig Toppera13603a2014-05-22 05:54:18 +00008713 : Listener(new PCHValidator(PP, *this)), DeserializationListener(nullptr),
Nico Weber824285e2014-05-08 04:26:47 +00008714 OwnsDeserializationListener(false), SourceMgr(PP.getSourceManager()),
Adrian Prantlfb2398d2015-07-17 01:19:54 +00008715 FileMgr(PP.getFileManager()), PCHContainerRdr(PCHContainerRdr),
Adrian Prantlbb165fb2015-06-20 18:53:08 +00008716 Diags(PP.getDiagnostics()), SemaObj(nullptr), PP(PP), Context(Context),
Adrian Prantlfb2398d2015-07-17 01:19:54 +00008717 Consumer(nullptr), ModuleMgr(PP.getFileManager(), PCHContainerRdr),
Richard Smith10379092016-05-06 23:14:07 +00008718 DummyIdResolver(PP),
Richard Smithce18a182015-07-14 00:26:00 +00008719 ReadTimer(std::move(ReadTimer)),
Nico Weber779355f2016-03-02 23:22:00 +00008720 PragmaMSStructState(-1),
Nico Weber42932312016-03-03 00:17:35 +00008721 PragmaMSPointersToMembersState(-1),
Adrian Prantlbb165fb2015-06-20 18:53:08 +00008722 isysroot(isysroot), DisableValidation(DisableValidation),
Nico Weber824285e2014-05-08 04:26:47 +00008723 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
8724 AllowConfigurationMismatch(AllowConfigurationMismatch),
8725 ValidateSystemInputs(ValidateSystemInputs),
8726 UseGlobalIndex(UseGlobalIndex), TriedLoadingGlobalIndex(false),
Vassil Vassilev19765fb2016-07-22 21:08:24 +00008727 ProcessingUpdateRecords(false),
Adrian Prantlbb165fb2015-06-20 18:53:08 +00008728 CurrSwitchCaseStmts(&SwitchCaseStmts), NumSLocEntriesRead(0),
8729 TotalNumSLocEntries(0), NumStatementsRead(0), TotalNumStatements(0),
8730 NumMacrosRead(0), TotalNumMacros(0), NumIdentifierLookups(0),
8731 NumIdentifierLookupHits(0), NumSelectorsRead(0),
Nico Weber824285e2014-05-08 04:26:47 +00008732 NumMethodPoolEntriesRead(0), NumMethodPoolLookups(0),
8733 NumMethodPoolHits(0), NumMethodPoolTableLookups(0),
8734 NumMethodPoolTableHits(0), TotalNumMethodPoolEntries(0),
8735 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
8736 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
8737 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
Richard Smithc2bb8182015-03-24 06:36:48 +00008738 PassingDeclsToConsumer(false), ReadingKind(Read_None) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008739 SourceMgr.setExternalSLocEntrySource(this);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00008740
8741 for (const auto &Ext : Extensions) {
8742 auto BlockName = Ext->getExtensionMetadata().BlockName;
8743 auto Known = ModuleFileExtensions.find(BlockName);
8744 if (Known != ModuleFileExtensions.end()) {
8745 Diags.Report(diag::warn_duplicate_module_file_extension)
8746 << BlockName;
8747 continue;
8748 }
8749
8750 ModuleFileExtensions.insert({BlockName, Ext});
8751 }
Guy Benyei11169dd2012-12-18 14:30:41 +00008752}
8753
8754ASTReader::~ASTReader() {
Nico Weber824285e2014-05-08 04:26:47 +00008755 if (OwnsDeserializationListener)
8756 delete DeserializationListener;
Guy Benyei11169dd2012-12-18 14:30:41 +00008757}
Richard Smith10379092016-05-06 23:14:07 +00008758
8759IdentifierResolver &ASTReader::getIdResolver() {
8760 return SemaObj ? SemaObj->IdResolver : DummyIdResolver;
8761}