blob: ac78da26140503108df358157728090d0d5aa9d6 [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
Guy Benyei11169dd2012-12-18 14:30:41 +0000220#define BENIGN_LANGOPT(Name, Bits, Default, Description)
221#define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
222#include "clang/Basic/LangOptions.def"
223
Ben Langmuircd98cb72015-06-23 18:20:18 +0000224 if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) {
225 if (Diags)
226 Diags->Report(diag::err_pch_langopt_value_mismatch) << "module features";
227 return true;
228 }
229
Guy Benyei11169dd2012-12-18 14:30:41 +0000230 if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
231 if (Diags)
232 Diags->Report(diag::err_pch_langopt_value_mismatch)
233 << "target Objective-C runtime";
234 return true;
235 }
236
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +0000237 if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
238 LangOpts.CommentOpts.BlockCommandNames) {
239 if (Diags)
240 Diags->Report(diag::err_pch_langopt_value_mismatch)
241 << "block command names";
242 return true;
243 }
244
Guy Benyei11169dd2012-12-18 14:30:41 +0000245 return false;
246}
247
248/// \brief Compare the given set of target options against an existing set of
249/// target options.
250///
251/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
252///
253/// \returns true if the target options mis-match, false otherwise.
254static bool checkTargetOptions(const TargetOptions &TargetOpts,
255 const TargetOptions &ExistingTargetOpts,
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000256 DiagnosticsEngine *Diags,
257 bool AllowCompatibleDifferences = true) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000258#define CHECK_TARGET_OPT(Field, Name) \
259 if (TargetOpts.Field != ExistingTargetOpts.Field) { \
260 if (Diags) \
261 Diags->Report(diag::err_pch_targetopt_mismatch) \
262 << Name << TargetOpts.Field << ExistingTargetOpts.Field; \
263 return true; \
264 }
265
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000266 // The triple and ABI must match exactly.
Guy Benyei11169dd2012-12-18 14:30:41 +0000267 CHECK_TARGET_OPT(Triple, "target");
Guy Benyei11169dd2012-12-18 14:30:41 +0000268 CHECK_TARGET_OPT(ABI, "target ABI");
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000269
270 // We can tolerate different CPUs in many cases, notably when one CPU
271 // supports a strict superset of another. When allowing compatible
272 // differences skip this check.
273 if (!AllowCompatibleDifferences)
274 CHECK_TARGET_OPT(CPU, "target CPU");
275
Guy Benyei11169dd2012-12-18 14:30:41 +0000276#undef CHECK_TARGET_OPT
277
278 // Compare feature sets.
279 SmallVector<StringRef, 4> ExistingFeatures(
280 ExistingTargetOpts.FeaturesAsWritten.begin(),
281 ExistingTargetOpts.FeaturesAsWritten.end());
282 SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
283 TargetOpts.FeaturesAsWritten.end());
284 std::sort(ExistingFeatures.begin(), ExistingFeatures.end());
285 std::sort(ReadFeatures.begin(), ReadFeatures.end());
286
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000287 // We compute the set difference in both directions explicitly so that we can
288 // diagnose the differences differently.
289 SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures;
290 std::set_difference(
291 ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(),
292 ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures));
293 std::set_difference(ReadFeatures.begin(), ReadFeatures.end(),
294 ExistingFeatures.begin(), ExistingFeatures.end(),
295 std::back_inserter(UnmatchedReadFeatures));
Guy Benyei11169dd2012-12-18 14:30:41 +0000296
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000297 // If we are allowing compatible differences and the read feature set is
298 // a strict subset of the existing feature set, there is nothing to diagnose.
299 if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty())
300 return false;
Guy Benyei11169dd2012-12-18 14:30:41 +0000301
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000302 if (Diags) {
303 for (StringRef Feature : UnmatchedReadFeatures)
Guy Benyei11169dd2012-12-18 14:30:41 +0000304 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000305 << /* is-existing-feature */ false << Feature;
306 for (StringRef Feature : UnmatchedExistingFeatures)
307 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
308 << /* is-existing-feature */ true << Feature;
Guy Benyei11169dd2012-12-18 14:30:41 +0000309 }
310
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000311 return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty();
Guy Benyei11169dd2012-12-18 14:30:41 +0000312}
313
314bool
315PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
Richard Smith1e2cf0d2014-10-31 02:28:58 +0000316 bool Complain,
317 bool AllowCompatibleDifferences) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000318 const LangOptions &ExistingLangOpts = PP.getLangOpts();
319 return checkLanguageOptions(LangOpts, ExistingLangOpts,
Richard Smith1e2cf0d2014-10-31 02:28:58 +0000320 Complain ? &Reader.Diags : nullptr,
321 AllowCompatibleDifferences);
Guy Benyei11169dd2012-12-18 14:30:41 +0000322}
323
324bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000325 bool Complain,
326 bool AllowCompatibleDifferences) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000327 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
328 return checkTargetOptions(TargetOpts, ExistingTargetOpts,
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000329 Complain ? &Reader.Diags : nullptr,
330 AllowCompatibleDifferences);
Guy Benyei11169dd2012-12-18 14:30:41 +0000331}
332
333namespace {
334 typedef llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> >
335 MacroDefinitionsMap;
Craig Topper3598eb72013-07-05 04:43:31 +0000336 typedef llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> >
337 DeclsMap;
Guy Benyei11169dd2012-12-18 14:30:41 +0000338}
339
Ben Langmuirb92de022014-04-29 16:25:26 +0000340static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags,
341 DiagnosticsEngine &Diags,
342 bool Complain) {
343 typedef DiagnosticsEngine::Level Level;
344
345 // Check current mappings for new -Werror mappings, and the stored mappings
346 // for cases that were explicitly mapped to *not* be errors that are now
347 // errors because of options like -Werror.
348 DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags };
349
350 for (DiagnosticsEngine *MappingSource : MappingSources) {
351 for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {
352 diag::kind DiagID = DiagIDMappingPair.first;
353 Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation());
354 if (CurLevel < DiagnosticsEngine::Error)
355 continue; // not significant
356 Level StoredLevel =
357 StoredDiags.getDiagnosticLevel(DiagID, SourceLocation());
358 if (StoredLevel < DiagnosticsEngine::Error) {
359 if (Complain)
360 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" +
361 Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str();
362 return true;
363 }
364 }
365 }
366
367 return false;
368}
369
Alp Tokerac4e8e52014-06-22 21:58:33 +0000370static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) {
371 diag::Severity Ext = Diags.getExtensionHandlingBehavior();
372 if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors())
373 return true;
374 return Ext >= diag::Severity::Error;
Ben Langmuirb92de022014-04-29 16:25:26 +0000375}
376
377static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags,
378 DiagnosticsEngine &Diags,
379 bool IsSystem, bool Complain) {
380 // Top-level options
381 if (IsSystem) {
382 if (Diags.getSuppressSystemWarnings())
383 return false;
384 // If -Wsystem-headers was not enabled before, be conservative
385 if (StoredDiags.getSuppressSystemWarnings()) {
386 if (Complain)
387 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers";
388 return true;
389 }
390 }
391
392 if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) {
393 if (Complain)
394 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror";
395 return true;
396 }
397
398 if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() &&
399 !StoredDiags.getEnableAllWarnings()) {
400 if (Complain)
401 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror";
402 return true;
403 }
404
405 if (isExtHandlingFromDiagsError(Diags) &&
406 !isExtHandlingFromDiagsError(StoredDiags)) {
407 if (Complain)
408 Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors";
409 return true;
410 }
411
412 return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain);
413}
414
415bool PCHValidator::ReadDiagnosticOptions(
416 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
417 DiagnosticsEngine &ExistingDiags = PP.getDiagnostics();
418 IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs());
419 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
Alp Tokerf994cef2014-07-05 03:08:06 +0000420 new DiagnosticsEngine(DiagIDs, DiagOpts.get()));
Ben Langmuirb92de022014-04-29 16:25:26 +0000421 // This should never fail, because we would have processed these options
422 // before writing them to an ASTFile.
423 ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false);
424
425 ModuleManager &ModuleMgr = Reader.getModuleManager();
426 assert(ModuleMgr.size() >= 1 && "what ASTFile is this then");
427
428 // If the original import came from a file explicitly generated by the user,
429 // don't check the diagnostic mappings.
430 // FIXME: currently this is approximated by checking whether this is not a
Richard Smithe842a472014-10-22 02:05:46 +0000431 // module import of an implicitly-loaded module file.
Ben Langmuirb92de022014-04-29 16:25:26 +0000432 // Note: ModuleMgr.rbegin() may not be the current module, but it must be in
433 // the transitive closure of its imports, since unrelated modules cannot be
434 // imported until after this module finishes validation.
435 ModuleFile *TopImport = *ModuleMgr.rbegin();
436 while (!TopImport->ImportedBy.empty())
437 TopImport = TopImport->ImportedBy[0];
Richard Smithe842a472014-10-22 02:05:46 +0000438 if (TopImport->Kind != MK_ImplicitModule)
Ben Langmuirb92de022014-04-29 16:25:26 +0000439 return false;
440
441 StringRef ModuleName = TopImport->ModuleName;
442 assert(!ModuleName.empty() && "diagnostic options read before module name");
443
444 Module *M = PP.getHeaderSearchInfo().lookupModule(ModuleName);
445 assert(M && "missing module");
446
447 // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that
448 // contains the union of their flags.
449 return checkDiagnosticMappings(*Diags, ExistingDiags, M->IsSystem, Complain);
450}
451
Guy Benyei11169dd2012-12-18 14:30:41 +0000452/// \brief Collect the macro definitions provided by the given preprocessor
453/// options.
Craig Toppera13603a2014-05-22 05:54:18 +0000454static void
455collectMacroDefinitions(const PreprocessorOptions &PPOpts,
456 MacroDefinitionsMap &Macros,
457 SmallVectorImpl<StringRef> *MacroNames = nullptr) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000458 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
459 StringRef Macro = PPOpts.Macros[I].first;
460 bool IsUndef = PPOpts.Macros[I].second;
461
462 std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
463 StringRef MacroName = MacroPair.first;
464 StringRef MacroBody = MacroPair.second;
465
466 // For an #undef'd macro, we only care about the name.
467 if (IsUndef) {
468 if (MacroNames && !Macros.count(MacroName))
469 MacroNames->push_back(MacroName);
470
471 Macros[MacroName] = std::make_pair("", true);
472 continue;
473 }
474
475 // For a #define'd macro, figure out the actual definition.
476 if (MacroName.size() == Macro.size())
477 MacroBody = "1";
478 else {
479 // Note: GCC drops anything following an end-of-line character.
480 StringRef::size_type End = MacroBody.find_first_of("\n\r");
481 MacroBody = MacroBody.substr(0, End);
482 }
483
484 if (MacroNames && !Macros.count(MacroName))
485 MacroNames->push_back(MacroName);
486 Macros[MacroName] = std::make_pair(MacroBody, false);
487 }
488}
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +0000489
Guy Benyei11169dd2012-12-18 14:30:41 +0000490/// \brief Check the preprocessor options deserialized from the control block
491/// against the preprocessor options in an existing preprocessor.
492///
493/// \param Diags If non-null, produce diagnostics for any mismatches incurred.
494static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts,
495 const PreprocessorOptions &ExistingPPOpts,
496 DiagnosticsEngine *Diags,
497 FileManager &FileMgr,
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +0000498 std::string &SuggestedPredefines,
499 const LangOptions &LangOpts) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000500 // Check macro definitions.
501 MacroDefinitionsMap ASTFileMacros;
502 collectMacroDefinitions(PPOpts, ASTFileMacros);
503 MacroDefinitionsMap ExistingMacros;
504 SmallVector<StringRef, 4> ExistingMacroNames;
505 collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames);
506
507 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
508 // Dig out the macro definition in the existing preprocessor options.
509 StringRef MacroName = ExistingMacroNames[I];
510 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
511
512 // Check whether we know anything about this macro name or not.
513 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> >::iterator Known
514 = ASTFileMacros.find(MacroName);
515 if (Known == ASTFileMacros.end()) {
516 // FIXME: Check whether this identifier was referenced anywhere in the
517 // AST file. If so, we should reject the AST file. Unfortunately, this
518 // information isn't in the control block. What shall we do about it?
519
520 if (Existing.second) {
521 SuggestedPredefines += "#undef ";
522 SuggestedPredefines += MacroName.str();
523 SuggestedPredefines += '\n';
524 } else {
525 SuggestedPredefines += "#define ";
526 SuggestedPredefines += MacroName.str();
527 SuggestedPredefines += ' ';
528 SuggestedPredefines += Existing.first.str();
529 SuggestedPredefines += '\n';
530 }
531 continue;
532 }
533
534 // If the macro was defined in one but undef'd in the other, we have a
535 // conflict.
536 if (Existing.second != Known->second.second) {
537 if (Diags) {
538 Diags->Report(diag::err_pch_macro_def_undef)
539 << MacroName << Known->second.second;
540 }
541 return true;
542 }
543
544 // If the macro was #undef'd in both, or if the macro bodies are identical,
545 // it's fine.
546 if (Existing.second || Existing.first == Known->second.first)
547 continue;
548
549 // The macro bodies differ; complain.
550 if (Diags) {
551 Diags->Report(diag::err_pch_macro_def_conflict)
552 << MacroName << Known->second.first << Existing.first;
553 }
554 return true;
555 }
556
557 // Check whether we're using predefines.
558 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines) {
559 if (Diags) {
560 Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
561 }
562 return true;
563 }
564
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +0000565 // Detailed record is important since it is used for the module cache hash.
566 if (LangOpts.Modules &&
567 PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord) {
568 if (Diags) {
569 Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord;
570 }
571 return true;
572 }
573
Guy Benyei11169dd2012-12-18 14:30:41 +0000574 // Compute the #include and #include_macros lines we need.
575 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
576 StringRef File = ExistingPPOpts.Includes[I];
577 if (File == ExistingPPOpts.ImplicitPCHInclude)
578 continue;
579
580 if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File)
581 != PPOpts.Includes.end())
582 continue;
583
584 SuggestedPredefines += "#include \"";
Manuel Klimek9af34ae2014-08-12 08:25:57 +0000585 SuggestedPredefines += File;
Guy Benyei11169dd2012-12-18 14:30:41 +0000586 SuggestedPredefines += "\"\n";
587 }
588
589 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
590 StringRef File = ExistingPPOpts.MacroIncludes[I];
591 if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(),
592 File)
593 != PPOpts.MacroIncludes.end())
594 continue;
595
596 SuggestedPredefines += "#__include_macros \"";
Manuel Klimek9af34ae2014-08-12 08:25:57 +0000597 SuggestedPredefines += File;
Guy Benyei11169dd2012-12-18 14:30:41 +0000598 SuggestedPredefines += "\"\n##\n";
599 }
600
601 return false;
602}
603
604bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
605 bool Complain,
606 std::string &SuggestedPredefines) {
607 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
608
609 return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
Craig Toppera13603a2014-05-22 05:54:18 +0000610 Complain? &Reader.Diags : nullptr,
Guy Benyei11169dd2012-12-18 14:30:41 +0000611 PP.getFileManager(),
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +0000612 SuggestedPredefines,
613 PP.getLangOpts());
Guy Benyei11169dd2012-12-18 14:30:41 +0000614}
615
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +0000616/// Check the header search options deserialized from the control block
617/// against the header search options in an existing preprocessor.
618///
619/// \param Diags If non-null, produce diagnostics for any mismatches incurred.
620static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
621 StringRef SpecificModuleCachePath,
622 StringRef ExistingModuleCachePath,
623 DiagnosticsEngine *Diags,
624 const LangOptions &LangOpts) {
625 if (LangOpts.Modules) {
626 if (SpecificModuleCachePath != ExistingModuleCachePath) {
627 if (Diags)
628 Diags->Report(diag::err_pch_modulecache_mismatch)
629 << SpecificModuleCachePath << ExistingModuleCachePath;
630 return true;
631 }
632 }
633
634 return false;
635}
636
637bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
638 StringRef SpecificModuleCachePath,
639 bool Complain) {
640 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
641 PP.getHeaderSearchInfo().getModuleCachePath(),
642 Complain ? &Reader.Diags : nullptr,
643 PP.getLangOpts());
644}
645
Guy Benyei11169dd2012-12-18 14:30:41 +0000646void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
647 PP.setCounterValue(Value);
648}
649
650//===----------------------------------------------------------------------===//
651// AST reader implementation
652//===----------------------------------------------------------------------===//
653
Nico Weber824285e2014-05-08 04:26:47 +0000654void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,
655 bool TakeOwnership) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000656 DeserializationListener = Listener;
Nico Weber824285e2014-05-08 04:26:47 +0000657 OwnsDeserializationListener = TakeOwnership;
Guy Benyei11169dd2012-12-18 14:30:41 +0000658}
659
660
661
662unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
663 return serialization::ComputeHash(Sel);
664}
665
666
667std::pair<unsigned, unsigned>
668ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000669 using namespace llvm::support;
670 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
671 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
Guy Benyei11169dd2012-12-18 14:30:41 +0000672 return std::make_pair(KeyLen, DataLen);
673}
674
675ASTSelectorLookupTrait::internal_key_type
676ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000677 using namespace llvm::support;
Guy Benyei11169dd2012-12-18 14:30:41 +0000678 SelectorTable &SelTable = Reader.getContext().Selectors;
Justin Bogner57ba0b22014-03-28 22:03:24 +0000679 unsigned N = endian::readNext<uint16_t, little, unaligned>(d);
680 IdentifierInfo *FirstII = Reader.getLocalIdentifier(
681 F, endian::readNext<uint32_t, little, unaligned>(d));
Guy Benyei11169dd2012-12-18 14:30:41 +0000682 if (N == 0)
683 return SelTable.getNullarySelector(FirstII);
684 else if (N == 1)
685 return SelTable.getUnarySelector(FirstII);
686
687 SmallVector<IdentifierInfo *, 16> Args;
688 Args.push_back(FirstII);
689 for (unsigned I = 1; I != N; ++I)
Justin Bogner57ba0b22014-03-28 22:03:24 +0000690 Args.push_back(Reader.getLocalIdentifier(
691 F, endian::readNext<uint32_t, little, unaligned>(d)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000692
693 return SelTable.getSelector(N, Args.data());
694}
695
696ASTSelectorLookupTrait::data_type
697ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
698 unsigned DataLen) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000699 using namespace llvm::support;
Guy Benyei11169dd2012-12-18 14:30:41 +0000700
701 data_type Result;
702
Justin Bogner57ba0b22014-03-28 22:03:24 +0000703 Result.ID = Reader.getGlobalSelectorID(
704 F, endian::readNext<uint32_t, little, unaligned>(d));
Nico Weberff4b35e2014-12-27 22:14:15 +0000705 unsigned FullInstanceBits = endian::readNext<uint16_t, little, unaligned>(d);
706 unsigned FullFactoryBits = endian::readNext<uint16_t, little, unaligned>(d);
707 Result.InstanceBits = FullInstanceBits & 0x3;
708 Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1;
709 Result.FactoryBits = FullFactoryBits & 0x3;
710 Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1;
711 unsigned NumInstanceMethods = FullInstanceBits >> 3;
712 unsigned NumFactoryMethods = FullFactoryBits >> 3;
Guy Benyei11169dd2012-12-18 14:30:41 +0000713
714 // Load instance methods
715 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000716 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
717 F, endian::readNext<uint32_t, little, unaligned>(d)))
Guy Benyei11169dd2012-12-18 14:30:41 +0000718 Result.Instance.push_back(Method);
719 }
720
721 // Load factory methods
722 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000723 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
724 F, endian::readNext<uint32_t, little, unaligned>(d)))
Guy Benyei11169dd2012-12-18 14:30:41 +0000725 Result.Factory.push_back(Method);
726 }
727
728 return Result;
729}
730
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000731unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
732 return llvm::HashString(a);
Guy Benyei11169dd2012-12-18 14:30:41 +0000733}
734
735std::pair<unsigned, unsigned>
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000736ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000737 using namespace llvm::support;
738 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
739 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
Guy Benyei11169dd2012-12-18 14:30:41 +0000740 return std::make_pair(KeyLen, DataLen);
741}
742
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000743ASTIdentifierLookupTraitBase::internal_key_type
744ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000745 assert(n >= 2 && d[n-1] == '\0');
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000746 return StringRef((const char*) d, n-1);
Guy Benyei11169dd2012-12-18 14:30:41 +0000747}
748
Douglas Gregordcf25082013-02-11 18:16:18 +0000749/// \brief Whether the given identifier is "interesting".
Richard Smitha534a312015-07-21 23:54:07 +0000750static bool isInterestingIdentifier(ASTReader &Reader, IdentifierInfo &II,
751 bool IsModule) {
Richard Smithcab89802015-07-17 20:19:56 +0000752 return II.hadMacroDefinition() ||
753 II.isPoisoned() ||
Richard Smith9c254182015-07-19 21:41:12 +0000754 (IsModule ? II.hasRevertedBuiltin() : II.getObjCOrBuiltinID()) ||
Douglas Gregordcf25082013-02-11 18:16:18 +0000755 II.hasRevertedTokenIDToIdentifier() ||
Richard Smitha534a312015-07-21 23:54:07 +0000756 (!(IsModule && Reader.getContext().getLangOpts().CPlusPlus) &&
757 II.getFETokenInfo<void>());
Douglas Gregordcf25082013-02-11 18:16:18 +0000758}
759
Richard Smith76c2f2c2015-07-17 20:09:43 +0000760static bool readBit(unsigned &Bits) {
761 bool Value = Bits & 0x1;
762 Bits >>= 1;
763 return Value;
764}
765
Richard Smith79bf9202015-08-24 03:33:22 +0000766IdentID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) {
767 using namespace llvm::support;
768 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
769 return Reader.getGlobalIdentifierID(F, RawID >> 1);
770}
771
Richard Smitheb4b58f62016-02-05 01:40:54 +0000772static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II) {
773 if (!II.isFromAST()) {
774 II.setIsFromAST();
775 bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr;
776 if (isInterestingIdentifier(Reader, II, IsModule))
777 II.setChangedSinceDeserialization();
778 }
779}
780
Guy Benyei11169dd2012-12-18 14:30:41 +0000781IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
782 const unsigned char* d,
783 unsigned DataLen) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000784 using namespace llvm::support;
785 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
Guy Benyei11169dd2012-12-18 14:30:41 +0000786 bool IsInteresting = RawID & 0x01;
787
788 // Wipe out the "is interesting" bit.
789 RawID = RawID >> 1;
790
Richard Smith76c2f2c2015-07-17 20:09:43 +0000791 // Build the IdentifierInfo and link the identifier ID with it.
792 IdentifierInfo *II = KnownII;
793 if (!II) {
794 II = &Reader.getIdentifierTable().getOwn(k);
795 KnownII = II;
796 }
Richard Smitheb4b58f62016-02-05 01:40:54 +0000797 markIdentifierFromAST(Reader, *II);
Richard Smith76c2f2c2015-07-17 20:09:43 +0000798 Reader.markIdentifierUpToDate(II);
799
Guy Benyei11169dd2012-12-18 14:30:41 +0000800 IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
801 if (!IsInteresting) {
Richard Smith76c2f2c2015-07-17 20:09:43 +0000802 // For uninteresting identifiers, there's nothing else to do. Just notify
803 // the reader that we've finished loading this identifier.
Guy Benyei11169dd2012-12-18 14:30:41 +0000804 Reader.SetIdentifierInfo(ID, II);
Guy Benyei11169dd2012-12-18 14:30:41 +0000805 return II;
806 }
807
Justin Bogner57ba0b22014-03-28 22:03:24 +0000808 unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d);
809 unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d);
Richard Smith76c2f2c2015-07-17 20:09:43 +0000810 bool CPlusPlusOperatorKeyword = readBit(Bits);
811 bool HasRevertedTokenIDToIdentifier = readBit(Bits);
Richard Smith9c254182015-07-19 21:41:12 +0000812 bool HasRevertedBuiltin = readBit(Bits);
Richard Smith76c2f2c2015-07-17 20:09:43 +0000813 bool Poisoned = readBit(Bits);
814 bool ExtensionToken = readBit(Bits);
815 bool HadMacroDefinition = readBit(Bits);
Guy Benyei11169dd2012-12-18 14:30:41 +0000816
817 assert(Bits == 0 && "Extra bits in the identifier?");
818 DataLen -= 8;
819
Guy Benyei11169dd2012-12-18 14:30:41 +0000820 // Set or check the various bits in the IdentifierInfo structure.
821 // Token IDs are read-only.
Argyrios Kyrtzidisddee8c92013-02-27 01:13:51 +0000822 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
Richard Smith9c254182015-07-19 21:41:12 +0000823 II->revertTokenIDToIdentifier();
824 if (!F.isModule())
825 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
826 else if (HasRevertedBuiltin && II->getBuiltinID()) {
827 II->revertBuiltin();
828 assert((II->hasRevertedBuiltin() ||
829 II->getObjCOrBuiltinID() == ObjCOrBuiltinID) &&
830 "Incorrect ObjC keyword or builtin ID");
831 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000832 assert(II->isExtensionToken() == ExtensionToken &&
833 "Incorrect extension token flag");
834 (void)ExtensionToken;
835 if (Poisoned)
836 II->setIsPoisoned(true);
837 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
838 "Incorrect C++ operator keyword flag");
839 (void)CPlusPlusOperatorKeyword;
840
841 // If this identifier is a macro, deserialize the macro
842 // definition.
Richard Smith76c2f2c2015-07-17 20:09:43 +0000843 if (HadMacroDefinition) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000844 uint32_t MacroDirectivesOffset =
845 endian::readNext<uint32_t, little, unaligned>(d);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +0000846 DataLen -= 4;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +0000847
Richard Smithd7329392015-04-21 21:46:32 +0000848 Reader.addPendingMacro(II, &F, MacroDirectivesOffset);
Guy Benyei11169dd2012-12-18 14:30:41 +0000849 }
850
851 Reader.SetIdentifierInfo(ID, II);
852
853 // Read all of the declarations visible at global scope with this
854 // name.
855 if (DataLen > 0) {
856 SmallVector<uint32_t, 4> DeclIDs;
857 for (; DataLen > 0; DataLen -= 4)
Justin Bogner57ba0b22014-03-28 22:03:24 +0000858 DeclIDs.push_back(Reader.getGlobalDeclID(
859 F, endian::readNext<uint32_t, little, unaligned>(d)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000860 Reader.SetGloballyVisibleDecls(II, DeclIDs);
861 }
862
863 return II;
864}
865
Richard Smitha06c7e62015-08-26 23:55:49 +0000866DeclarationNameKey::DeclarationNameKey(DeclarationName Name)
867 : Kind(Name.getNameKind()) {
868 switch (Kind) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000869 case DeclarationName::Identifier:
Richard Smitha06c7e62015-08-26 23:55:49 +0000870 Data = (uint64_t)Name.getAsIdentifierInfo();
Guy Benyei11169dd2012-12-18 14:30:41 +0000871 break;
872 case DeclarationName::ObjCZeroArgSelector:
873 case DeclarationName::ObjCOneArgSelector:
874 case DeclarationName::ObjCMultiArgSelector:
Richard Smitha06c7e62015-08-26 23:55:49 +0000875 Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
Guy Benyei11169dd2012-12-18 14:30:41 +0000876 break;
877 case DeclarationName::CXXOperatorName:
Richard Smitha06c7e62015-08-26 23:55:49 +0000878 Data = Name.getCXXOverloadedOperator();
879 break;
880 case DeclarationName::CXXLiteralOperatorName:
881 Data = (uint64_t)Name.getCXXLiteralIdentifier();
882 break;
883 case DeclarationName::CXXConstructorName:
884 case DeclarationName::CXXDestructorName:
885 case DeclarationName::CXXConversionFunctionName:
886 case DeclarationName::CXXUsingDirective:
887 Data = 0;
888 break;
889 }
890}
891
892unsigned DeclarationNameKey::getHash() const {
893 llvm::FoldingSetNodeID ID;
894 ID.AddInteger(Kind);
895
896 switch (Kind) {
897 case DeclarationName::Identifier:
898 case DeclarationName::CXXLiteralOperatorName:
899 ID.AddString(((IdentifierInfo*)Data)->getName());
900 break;
901 case DeclarationName::ObjCZeroArgSelector:
902 case DeclarationName::ObjCOneArgSelector:
903 case DeclarationName::ObjCMultiArgSelector:
904 ID.AddInteger(serialization::ComputeHash(Selector(Data)));
905 break;
906 case DeclarationName::CXXOperatorName:
907 ID.AddInteger((OverloadedOperatorKind)Data);
Guy Benyei11169dd2012-12-18 14:30:41 +0000908 break;
909 case DeclarationName::CXXConstructorName:
910 case DeclarationName::CXXDestructorName:
911 case DeclarationName::CXXConversionFunctionName:
912 case DeclarationName::CXXUsingDirective:
913 break;
914 }
915
916 return ID.ComputeHash();
917}
918
Richard Smithd88a7f12015-09-01 20:35:42 +0000919ModuleFile *
920ASTDeclContextNameLookupTrait::ReadFileRef(const unsigned char *&d) {
921 using namespace llvm::support;
922 uint32_t ModuleFileID = endian::readNext<uint32_t, little, unaligned>(d);
923 return Reader.getLocalModuleFile(F, ModuleFileID);
924}
925
Guy Benyei11169dd2012-12-18 14:30:41 +0000926std::pair<unsigned, unsigned>
Richard Smitha06c7e62015-08-26 23:55:49 +0000927ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char *&d) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000928 using namespace llvm::support;
929 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
930 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
Guy Benyei11169dd2012-12-18 14:30:41 +0000931 return std::make_pair(KeyLen, DataLen);
932}
933
Richard Smitha06c7e62015-08-26 23:55:49 +0000934ASTDeclContextNameLookupTrait::internal_key_type
935ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000936 using namespace llvm::support;
Guy Benyei11169dd2012-12-18 14:30:41 +0000937
Richard Smitha06c7e62015-08-26 23:55:49 +0000938 auto Kind = (DeclarationName::NameKind)*d++;
939 uint64_t Data;
940 switch (Kind) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000941 case DeclarationName::Identifier:
Richard Smitha06c7e62015-08-26 23:55:49 +0000942 Data = (uint64_t)Reader.getLocalIdentifier(
Justin Bogner57ba0b22014-03-28 22:03:24 +0000943 F, endian::readNext<uint32_t, little, unaligned>(d));
Guy Benyei11169dd2012-12-18 14:30:41 +0000944 break;
945 case DeclarationName::ObjCZeroArgSelector:
946 case DeclarationName::ObjCOneArgSelector:
947 case DeclarationName::ObjCMultiArgSelector:
Richard Smitha06c7e62015-08-26 23:55:49 +0000948 Data =
Justin Bogner57ba0b22014-03-28 22:03:24 +0000949 (uint64_t)Reader.getLocalSelector(
950 F, endian::readNext<uint32_t, little, unaligned>(
951 d)).getAsOpaquePtr();
Guy Benyei11169dd2012-12-18 14:30:41 +0000952 break;
953 case DeclarationName::CXXOperatorName:
Richard Smitha06c7e62015-08-26 23:55:49 +0000954 Data = *d++; // OverloadedOperatorKind
Guy Benyei11169dd2012-12-18 14:30:41 +0000955 break;
956 case DeclarationName::CXXLiteralOperatorName:
Richard Smitha06c7e62015-08-26 23:55:49 +0000957 Data = (uint64_t)Reader.getLocalIdentifier(
Justin Bogner57ba0b22014-03-28 22:03:24 +0000958 F, endian::readNext<uint32_t, little, unaligned>(d));
Guy Benyei11169dd2012-12-18 14:30:41 +0000959 break;
960 case DeclarationName::CXXConstructorName:
961 case DeclarationName::CXXDestructorName:
962 case DeclarationName::CXXConversionFunctionName:
963 case DeclarationName::CXXUsingDirective:
Richard Smitha06c7e62015-08-26 23:55:49 +0000964 Data = 0;
Guy Benyei11169dd2012-12-18 14:30:41 +0000965 break;
966 }
967
Richard Smitha06c7e62015-08-26 23:55:49 +0000968 return DeclarationNameKey(Kind, Data);
Guy Benyei11169dd2012-12-18 14:30:41 +0000969}
970
Richard Smithd88a7f12015-09-01 20:35:42 +0000971void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type,
972 const unsigned char *d,
973 unsigned DataLen,
974 data_type_builder &Val) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000975 using namespace llvm::support;
Richard Smithd88a7f12015-09-01 20:35:42 +0000976 for (unsigned NumDecls = DataLen / 4; NumDecls; --NumDecls) {
977 uint32_t LocalID = endian::readNext<uint32_t, little, unaligned>(d);
978 Val.insert(Reader.getGlobalDeclID(F, LocalID));
979 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000980}
981
Richard Smith0f4e2c42015-08-06 04:23:48 +0000982bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M,
983 BitstreamCursor &Cursor,
984 uint64_t Offset,
985 DeclContext *DC) {
986 assert(Offset != 0);
987
Guy Benyei11169dd2012-12-18 14:30:41 +0000988 SavedStreamPosition SavedPosition(Cursor);
Richard Smith0f4e2c42015-08-06 04:23:48 +0000989 Cursor.JumpToBit(Offset);
Guy Benyei11169dd2012-12-18 14:30:41 +0000990
Richard Smith0f4e2c42015-08-06 04:23:48 +0000991 RecordData Record;
992 StringRef Blob;
993 unsigned Code = Cursor.ReadCode();
994 unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
995 if (RecCode != DECL_CONTEXT_LEXICAL) {
996 Error("Expected lexical block");
997 return true;
Guy Benyei11169dd2012-12-18 14:30:41 +0000998 }
999
Richard Smith82f8fcd2015-08-06 22:07:25 +00001000 assert(!isa<TranslationUnitDecl>(DC) &&
1001 "expected a TU_UPDATE_LEXICAL record for TU");
Richard Smith9c9173d2015-08-11 22:00:24 +00001002 // If we are handling a C++ class template instantiation, we can see multiple
1003 // lexical updates for the same record. It's important that we select only one
1004 // of them, so that field numbering works properly. Just pick the first one we
1005 // see.
1006 auto &Lex = LexicalDecls[DC];
1007 if (!Lex.first) {
1008 Lex = std::make_pair(
1009 &M, llvm::makeArrayRef(
1010 reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
1011 Blob.data()),
1012 Blob.size() / 4));
1013 }
Richard Smith0f4e2c42015-08-06 04:23:48 +00001014 DC->setHasExternalLexicalStorage(true);
1015 return false;
1016}
Guy Benyei11169dd2012-12-18 14:30:41 +00001017
Richard Smith0f4e2c42015-08-06 04:23:48 +00001018bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M,
1019 BitstreamCursor &Cursor,
1020 uint64_t Offset,
1021 DeclID ID) {
1022 assert(Offset != 0);
1023
1024 SavedStreamPosition SavedPosition(Cursor);
1025 Cursor.JumpToBit(Offset);
1026
1027 RecordData Record;
1028 StringRef Blob;
1029 unsigned Code = Cursor.ReadCode();
1030 unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
1031 if (RecCode != DECL_CONTEXT_VISIBLE) {
1032 Error("Expected visible lookup table block");
1033 return true;
Guy Benyei11169dd2012-12-18 14:30:41 +00001034 }
1035
Richard Smith0f4e2c42015-08-06 04:23:48 +00001036 // We can't safely determine the primary context yet, so delay attaching the
1037 // lookup table until we're done with recursive deserialization.
Richard Smithd88a7f12015-09-01 20:35:42 +00001038 auto *Data = (const unsigned char*)Blob.data();
1039 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data});
Guy Benyei11169dd2012-12-18 14:30:41 +00001040 return false;
1041}
1042
1043void ASTReader::Error(StringRef Msg) {
1044 Error(diag::err_fe_pch_malformed, Msg);
Richard Smithfb1e7f72015-08-14 05:02:58 +00001045 if (Context.getLangOpts().Modules && !Diags.isDiagnosticInFlight() &&
1046 !PP.getHeaderSearchInfo().getModuleCachePath().empty()) {
Douglas Gregor940e8052013-05-10 22:15:13 +00001047 Diag(diag::note_module_cache_path)
1048 << PP.getHeaderSearchInfo().getModuleCachePath();
1049 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001050}
1051
1052void ASTReader::Error(unsigned DiagID,
1053 StringRef Arg1, StringRef Arg2) {
1054 if (Diags.isDiagnosticInFlight())
1055 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
1056 else
1057 Diag(DiagID) << Arg1 << Arg2;
1058}
1059
1060//===----------------------------------------------------------------------===//
1061// Source Manager Deserialization
1062//===----------------------------------------------------------------------===//
1063
1064/// \brief Read the line table in the source manager block.
1065/// \returns true if there was an error.
1066bool ASTReader::ParseLineTable(ModuleFile &F,
Richard Smith7ed1bc92014-12-05 22:42:13 +00001067 const RecordData &Record) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001068 unsigned Idx = 0;
1069 LineTableInfo &LineTable = SourceMgr.getLineTable();
1070
1071 // Parse the file names
1072 std::map<int, int> FileIDs;
Richard Smith63078492015-09-01 07:41:55 +00001073 for (unsigned I = 0; Record[Idx]; ++I) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001074 // Extract the file name
Richard Smith7ed1bc92014-12-05 22:42:13 +00001075 auto Filename = ReadPath(F, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00001076 FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
1077 }
Richard Smith63078492015-09-01 07:41:55 +00001078 ++Idx;
Guy Benyei11169dd2012-12-18 14:30:41 +00001079
1080 // Parse the line entries
1081 std::vector<LineEntry> Entries;
1082 while (Idx < Record.size()) {
1083 int FID = Record[Idx++];
1084 assert(FID >= 0 && "Serialized line entries for non-local file.");
1085 // Remap FileID from 1-based old view.
1086 FID += F.SLocEntryBaseID - 1;
1087
1088 // Extract the line entries
1089 unsigned NumEntries = Record[Idx++];
Richard Smith63078492015-09-01 07:41:55 +00001090 assert(NumEntries && "no line entries for file ID");
Guy Benyei11169dd2012-12-18 14:30:41 +00001091 Entries.clear();
1092 Entries.reserve(NumEntries);
1093 for (unsigned I = 0; I != NumEntries; ++I) {
1094 unsigned FileOffset = Record[Idx++];
1095 unsigned LineNo = Record[Idx++];
1096 int FilenameID = FileIDs[Record[Idx++]];
1097 SrcMgr::CharacteristicKind FileKind
1098 = (SrcMgr::CharacteristicKind)Record[Idx++];
1099 unsigned IncludeOffset = Record[Idx++];
1100 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1101 FileKind, IncludeOffset));
1102 }
1103 LineTable.AddEntry(FileID::get(FID), Entries);
1104 }
1105
1106 return false;
1107}
1108
1109/// \brief Read a source manager block
1110bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1111 using namespace SrcMgr;
1112
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001113 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001114
1115 // Set the source-location entry cursor to the current position in
1116 // the stream. This cursor will be used to read the contents of the
1117 // source manager block initially, and then lazily read
1118 // source-location entries as needed.
1119 SLocEntryCursor = F.Stream;
1120
1121 // The stream itself is going to skip over the source manager block.
1122 if (F.Stream.SkipBlock()) {
1123 Error("malformed block record in AST file");
1124 return true;
1125 }
1126
1127 // Enter the source manager block.
1128 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
1129 Error("malformed source manager block record in AST file");
1130 return true;
1131 }
1132
1133 RecordData Record;
1134 while (true) {
Chris Lattnere7b154b2013-01-19 21:39:22 +00001135 llvm::BitstreamEntry E = SLocEntryCursor.advanceSkippingSubblocks();
1136
1137 switch (E.Kind) {
1138 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1139 case llvm::BitstreamEntry::Error:
1140 Error("malformed block record in AST file");
1141 return true;
1142 case llvm::BitstreamEntry::EndBlock:
Guy Benyei11169dd2012-12-18 14:30:41 +00001143 return false;
Chris Lattnere7b154b2013-01-19 21:39:22 +00001144 case llvm::BitstreamEntry::Record:
1145 // The interesting case.
1146 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001147 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00001148
Guy Benyei11169dd2012-12-18 14:30:41 +00001149 // Read a record.
Guy Benyei11169dd2012-12-18 14:30:41 +00001150 Record.clear();
Chris Lattner15c3e7d2013-01-21 18:28:26 +00001151 StringRef Blob;
1152 switch (SLocEntryCursor.readRecord(E.ID, Record, &Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001153 default: // Default behavior: ignore.
1154 break;
1155
1156 case SM_SLOC_FILE_ENTRY:
1157 case SM_SLOC_BUFFER_ENTRY:
1158 case SM_SLOC_EXPANSION_ENTRY:
1159 // Once we hit one of the source location entries, we're done.
1160 return false;
1161 }
1162 }
1163}
1164
1165/// \brief If a header file is not found at the path that we expect it to be
1166/// and the PCH file was moved from its original location, try to resolve the
1167/// file by assuming that header+PCH were moved together and the header is in
1168/// the same place relative to the PCH.
1169static std::string
1170resolveFileRelativeToOriginalDir(const std::string &Filename,
1171 const std::string &OriginalDir,
1172 const std::string &CurrDir) {
1173 assert(OriginalDir != CurrDir &&
1174 "No point trying to resolve the file if the PCH dir didn't change");
1175 using namespace llvm::sys;
1176 SmallString<128> filePath(Filename);
1177 fs::make_absolute(filePath);
1178 assert(path::is_absolute(OriginalDir));
1179 SmallString<128> currPCHPath(CurrDir);
1180
1181 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1182 fileDirE = path::end(path::parent_path(filePath));
1183 path::const_iterator origDirI = path::begin(OriginalDir),
1184 origDirE = path::end(OriginalDir);
1185 // Skip the common path components from filePath and OriginalDir.
1186 while (fileDirI != fileDirE && origDirI != origDirE &&
1187 *fileDirI == *origDirI) {
1188 ++fileDirI;
1189 ++origDirI;
1190 }
1191 for (; origDirI != origDirE; ++origDirI)
1192 path::append(currPCHPath, "..");
1193 path::append(currPCHPath, fileDirI, fileDirE);
1194 path::append(currPCHPath, path::filename(Filename));
1195 return currPCHPath.str();
1196}
1197
1198bool ASTReader::ReadSLocEntry(int ID) {
1199 if (ID == 0)
1200 return false;
1201
1202 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1203 Error("source location entry ID out-of-range for AST file");
1204 return true;
1205 }
1206
Richard Smithaada85c2016-02-06 02:06:43 +00001207 // Local helper to read the (possibly-compressed) buffer data following the
1208 // entry record.
1209 auto ReadBuffer = [this](
1210 BitstreamCursor &SLocEntryCursor,
1211 StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> {
1212 RecordData Record;
1213 StringRef Blob;
1214 unsigned Code = SLocEntryCursor.ReadCode();
1215 unsigned RecCode = SLocEntryCursor.readRecord(Code, Record, &Blob);
1216
1217 if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) {
1218 SmallString<0> Uncompressed;
1219 if (llvm::zlib::uncompress(Blob, Uncompressed, Record[0]) !=
1220 llvm::zlib::StatusOK) {
1221 Error("could not decompress embedded file contents");
1222 return nullptr;
1223 }
1224 return llvm::MemoryBuffer::getMemBufferCopy(Uncompressed, Name);
1225 } else if (RecCode == SM_SLOC_BUFFER_BLOB) {
1226 return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true);
1227 } else {
1228 Error("AST record has invalid code");
1229 return nullptr;
1230 }
1231 };
1232
Guy Benyei11169dd2012-12-18 14:30:41 +00001233 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
1234 F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]);
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001235 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001236 unsigned BaseOffset = F->SLocEntryBaseOffset;
1237
1238 ++NumSLocEntriesRead;
Chris Lattnere7b154b2013-01-19 21:39:22 +00001239 llvm::BitstreamEntry Entry = SLocEntryCursor.advance();
1240 if (Entry.Kind != llvm::BitstreamEntry::Record) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001241 Error("incorrectly-formatted source location entry in AST file");
1242 return true;
1243 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00001244
Guy Benyei11169dd2012-12-18 14:30:41 +00001245 RecordData Record;
Chris Lattner0e6c9402013-01-20 02:38:54 +00001246 StringRef Blob;
1247 switch (SLocEntryCursor.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001248 default:
1249 Error("incorrectly-formatted source location entry in AST file");
1250 return true;
1251
1252 case SM_SLOC_FILE_ENTRY: {
1253 // We will detect whether a file changed and return 'Failure' for it, but
1254 // we will also try to fail gracefully by setting up the SLocEntry.
1255 unsigned InputID = Record[4];
1256 InputFile IF = getInputFile(*F, InputID);
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001257 const FileEntry *File = IF.getFile();
1258 bool OverriddenBuffer = IF.isOverridden();
Guy Benyei11169dd2012-12-18 14:30:41 +00001259
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001260 // Note that we only check if a File was returned. If it was out-of-date
1261 // we have complained but we will continue creating a FileID to recover
1262 // gracefully.
1263 if (!File)
Guy Benyei11169dd2012-12-18 14:30:41 +00001264 return true;
1265
1266 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1267 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1268 // This is the module's main file.
1269 IncludeLoc = getImportLocation(F);
1270 }
1271 SrcMgr::CharacteristicKind
1272 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1273 FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
1274 ID, BaseOffset + Record[0]);
1275 SrcMgr::FileInfo &FileInfo =
1276 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1277 FileInfo.NumCreatedFIDs = Record[5];
1278 if (Record[3])
1279 FileInfo.setHasLineDirectives();
1280
1281 const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
1282 unsigned NumFileDecls = Record[7];
1283 if (NumFileDecls) {
1284 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1285 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1286 NumFileDecls));
1287 }
Richard Smithaada85c2016-02-06 02:06:43 +00001288
Guy Benyei11169dd2012-12-18 14:30:41 +00001289 const SrcMgr::ContentCache *ContentCache
1290 = SourceMgr.getOrCreateContentCache(File,
1291 /*isSystemFile=*/FileCharacter != SrcMgr::C_User);
1292 if (OverriddenBuffer && !ContentCache->BufferOverridden &&
Richard Smitha8cfffa2015-11-26 02:04:16 +00001293 ContentCache->ContentsEntry == ContentCache->OrigEntry &&
1294 !ContentCache->getRawBuffer()) {
Richard Smithaada85c2016-02-06 02:06:43 +00001295 auto Buffer = ReadBuffer(SLocEntryCursor, File->getName());
1296 if (!Buffer)
Guy Benyei11169dd2012-12-18 14:30:41 +00001297 return true;
David Blaikie49cc3182014-08-27 20:54:45 +00001298 SourceMgr.overrideFileContents(File, std::move(Buffer));
Guy Benyei11169dd2012-12-18 14:30:41 +00001299 }
1300
1301 break;
1302 }
1303
1304 case SM_SLOC_BUFFER_ENTRY: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00001305 const char *Name = Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00001306 unsigned Offset = Record[0];
1307 SrcMgr::CharacteristicKind
1308 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1309 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
Richard Smithe842a472014-10-22 02:05:46 +00001310 if (IncludeLoc.isInvalid() &&
1311 (F->Kind == MK_ImplicitModule || F->Kind == MK_ExplicitModule)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001312 IncludeLoc = getImportLocation(F);
1313 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001314
Richard Smithaada85c2016-02-06 02:06:43 +00001315 auto Buffer = ReadBuffer(SLocEntryCursor, Name);
1316 if (!Buffer)
Guy Benyei11169dd2012-12-18 14:30:41 +00001317 return true;
David Blaikie50a5f972014-08-29 07:59:55 +00001318 SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID,
Rafael Espindolad87f8d72014-08-27 20:03:29 +00001319 BaseOffset + Offset, IncludeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001320 break;
1321 }
1322
1323 case SM_SLOC_EXPANSION_ENTRY: {
1324 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1325 SourceMgr.createExpansionLoc(SpellingLoc,
1326 ReadSourceLocation(*F, Record[2]),
1327 ReadSourceLocation(*F, Record[3]),
1328 Record[4],
1329 ID,
1330 BaseOffset + Record[0]);
1331 break;
1332 }
1333 }
1334
1335 return false;
1336}
1337
1338std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1339 if (ID == 0)
1340 return std::make_pair(SourceLocation(), "");
1341
1342 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1343 Error("source location entry ID out-of-range for AST file");
1344 return std::make_pair(SourceLocation(), "");
1345 }
1346
1347 // Find which module file this entry lands in.
1348 ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
Richard Smithe842a472014-10-22 02:05:46 +00001349 if (M->Kind != MK_ImplicitModule && M->Kind != MK_ExplicitModule)
Guy Benyei11169dd2012-12-18 14:30:41 +00001350 return std::make_pair(SourceLocation(), "");
1351
1352 // FIXME: Can we map this down to a particular submodule? That would be
1353 // ideal.
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001354 return std::make_pair(M->ImportLoc, StringRef(M->ModuleName));
Guy Benyei11169dd2012-12-18 14:30:41 +00001355}
1356
1357/// \brief Find the location where the module F is imported.
1358SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1359 if (F->ImportLoc.isValid())
1360 return F->ImportLoc;
1361
1362 // Otherwise we have a PCH. It's considered to be "imported" at the first
1363 // location of its includer.
1364 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001365 // Main file is the importer.
Yaron Keren8b563662015-10-03 10:46:20 +00001366 assert(SourceMgr.getMainFileID().isValid() && "missing main file");
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001367 return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
Guy Benyei11169dd2012-12-18 14:30:41 +00001368 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001369 return F->ImportedBy[0]->FirstLoc;
1370}
1371
1372/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1373/// specified cursor. Read the abbreviations that are at the top of the block
1374/// and then leave the cursor pointing into the block.
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001375bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID) {
Richard Smith0516b182015-09-08 19:40:14 +00001376 if (Cursor.EnterSubBlock(BlockID))
1377 return true;
Guy Benyei11169dd2012-12-18 14:30:41 +00001378
1379 while (true) {
1380 uint64_t Offset = Cursor.GetCurrentBitNo();
1381 unsigned Code = Cursor.ReadCode();
1382
1383 // We expect all abbrevs to be at the start of the block.
1384 if (Code != llvm::bitc::DEFINE_ABBREV) {
1385 Cursor.JumpToBit(Offset);
1386 return false;
1387 }
1388 Cursor.ReadAbbrevRecord();
1389 }
1390}
1391
Richard Smithe40f2ba2013-08-07 21:41:30 +00001392Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record,
John McCallf413f5e2013-05-03 00:10:13 +00001393 unsigned &Idx) {
1394 Token Tok;
1395 Tok.startToken();
1396 Tok.setLocation(ReadSourceLocation(F, Record, Idx));
1397 Tok.setLength(Record[Idx++]);
1398 if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
1399 Tok.setIdentifierInfo(II);
1400 Tok.setKind((tok::TokenKind)Record[Idx++]);
1401 Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1402 return Tok;
1403}
1404
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001405MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001406 BitstreamCursor &Stream = F.MacroCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001407
1408 // Keep track of where we are in the stream, then jump back there
1409 // after reading this macro.
1410 SavedStreamPosition SavedPosition(Stream);
1411
1412 Stream.JumpToBit(Offset);
1413 RecordData Record;
1414 SmallVector<IdentifierInfo*, 16> MacroArgs;
Craig Toppera13603a2014-05-22 05:54:18 +00001415 MacroInfo *Macro = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001416
Guy Benyei11169dd2012-12-18 14:30:41 +00001417 while (true) {
Chris Lattnerefa77172013-01-20 00:00:22 +00001418 // Advance to the next record, but if we get to the end of the block, don't
1419 // pop it (removing all the abbreviations from the cursor) since we want to
1420 // be able to reseek within the block and read entries.
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001421 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
Chris Lattnerefa77172013-01-20 00:00:22 +00001422 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks(Flags);
1423
1424 switch (Entry.Kind) {
1425 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1426 case llvm::BitstreamEntry::Error:
1427 Error("malformed block record in AST file");
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001428 return Macro;
Chris Lattnerefa77172013-01-20 00:00:22 +00001429 case llvm::BitstreamEntry::EndBlock:
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001430 return Macro;
Chris Lattnerefa77172013-01-20 00:00:22 +00001431 case llvm::BitstreamEntry::Record:
1432 // The interesting case.
1433 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001434 }
1435
1436 // Read a record.
Guy Benyei11169dd2012-12-18 14:30:41 +00001437 Record.clear();
1438 PreprocessorRecordTypes RecType =
Chris Lattner0e6c9402013-01-20 02:38:54 +00001439 (PreprocessorRecordTypes)Stream.readRecord(Entry.ID, Record);
Guy Benyei11169dd2012-12-18 14:30:41 +00001440 switch (RecType) {
Richard Smithd7329392015-04-21 21:46:32 +00001441 case PP_MODULE_MACRO:
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001442 case PP_MACRO_DIRECTIVE_HISTORY:
1443 return Macro;
1444
Guy Benyei11169dd2012-12-18 14:30:41 +00001445 case PP_MACRO_OBJECT_LIKE:
1446 case PP_MACRO_FUNCTION_LIKE: {
1447 // If we already have a macro, that means that we've hit the end
1448 // of the definition of the macro we were looking for. We're
1449 // done.
1450 if (Macro)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001451 return Macro;
Guy Benyei11169dd2012-12-18 14:30:41 +00001452
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001453 unsigned NextIndex = 1; // Skip identifier ID.
1454 SubmoduleID SubModID = getGlobalSubmoduleID(F, Record[NextIndex++]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001455 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001456 MacroInfo *MI = PP.AllocateDeserializedMacroInfo(Loc, SubModID);
Argyrios Kyrtzidis7572be22013-01-07 19:16:23 +00001457 MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
Guy Benyei11169dd2012-12-18 14:30:41 +00001458 MI->setIsUsed(Record[NextIndex++]);
Argyrios Kyrtzidis9ef53ce2014-04-09 18:21:23 +00001459 MI->setUsedForHeaderGuard(Record[NextIndex++]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001460
Guy Benyei11169dd2012-12-18 14:30:41 +00001461 if (RecType == PP_MACRO_FUNCTION_LIKE) {
1462 // Decode function-like macro info.
1463 bool isC99VarArgs = Record[NextIndex++];
1464 bool isGNUVarArgs = Record[NextIndex++];
1465 bool hasCommaPasting = Record[NextIndex++];
1466 MacroArgs.clear();
1467 unsigned NumArgs = Record[NextIndex++];
1468 for (unsigned i = 0; i != NumArgs; ++i)
1469 MacroArgs.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1470
1471 // Install function-like macro info.
1472 MI->setIsFunctionLike();
1473 if (isC99VarArgs) MI->setIsC99Varargs();
1474 if (isGNUVarArgs) MI->setIsGNUVarargs();
1475 if (hasCommaPasting) MI->setHasCommaPasting();
Craig Topperd96b3f92015-10-22 04:59:52 +00001476 MI->setArgumentList(MacroArgs, PP.getPreprocessorAllocator());
Guy Benyei11169dd2012-12-18 14:30:41 +00001477 }
1478
Guy Benyei11169dd2012-12-18 14:30:41 +00001479 // Remember that we saw this macro last so that we add the tokens that
1480 // form its body to it.
1481 Macro = MI;
1482
1483 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1484 Record[NextIndex]) {
1485 // We have a macro definition. Register the association
1486 PreprocessedEntityID
1487 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1488 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
Richard Smith66a81862015-05-04 02:25:31 +00001489 PreprocessingRecord::PPEntityID PPID =
1490 PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true);
1491 MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
1492 PPRec.getPreprocessedEntity(PPID));
Argyrios Kyrtzidis832de9f2013-02-22 18:35:59 +00001493 if (PPDef)
1494 PPRec.RegisterMacroDefinition(Macro, PPDef);
Guy Benyei11169dd2012-12-18 14:30:41 +00001495 }
1496
1497 ++NumMacrosRead;
1498 break;
1499 }
1500
1501 case PP_TOKEN: {
1502 // If we see a TOKEN before a PP_MACRO_*, then the file is
1503 // erroneous, just pretend we didn't see this.
Craig Toppera13603a2014-05-22 05:54:18 +00001504 if (!Macro) break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001505
John McCallf413f5e2013-05-03 00:10:13 +00001506 unsigned Idx = 0;
1507 Token Tok = ReadToken(F, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00001508 Macro->AddTokenToBody(Tok);
1509 break;
1510 }
1511 }
1512 }
1513}
1514
1515PreprocessedEntityID
1516ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const {
1517 ContinuousRangeMap<uint32_t, int, 2>::const_iterator
1518 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1519 assert(I != M.PreprocessedEntityRemap.end()
1520 && "Invalid index into preprocessed entity index remap");
1521
1522 return LocalID + I->second;
1523}
1524
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001525unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1526 return llvm::hash_combine(ikey.Size, ikey.ModTime);
Guy Benyei11169dd2012-12-18 14:30:41 +00001527}
Richard Smith7ed1bc92014-12-05 22:42:13 +00001528
Guy Benyei11169dd2012-12-18 14:30:41 +00001529HeaderFileInfoTrait::internal_key_type
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001530HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
Richard Smithe75ee0f2015-08-17 07:13:32 +00001531 internal_key_type ikey = {FE->getSize(),
1532 M.HasTimestamps ? FE->getModificationTime() : 0,
1533 FE->getName(), /*Imported*/ false};
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001534 return ikey;
1535}
Guy Benyei11169dd2012-12-18 14:30:41 +00001536
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001537bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
Richard Smithe75ee0f2015-08-17 07:13:32 +00001538 if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime))
Guy Benyei11169dd2012-12-18 14:30:41 +00001539 return false;
1540
Richard Smith7ed1bc92014-12-05 22:42:13 +00001541 if (llvm::sys::path::is_absolute(a.Filename) &&
1542 strcmp(a.Filename, b.Filename) == 0)
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001543 return true;
1544
Guy Benyei11169dd2012-12-18 14:30:41 +00001545 // Determine whether the actual files are equivalent.
Argyrios Kyrtzidis2a513e82013-03-04 20:33:40 +00001546 FileManager &FileMgr = Reader.getFileManager();
Richard Smith7ed1bc92014-12-05 22:42:13 +00001547 auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* {
1548 if (!Key.Imported)
1549 return FileMgr.getFile(Key.Filename);
1550
1551 std::string Resolved = Key.Filename;
1552 Reader.ResolveImportedPath(M, Resolved);
1553 return FileMgr.getFile(Resolved);
1554 };
1555
1556 const FileEntry *FEA = GetFile(a);
1557 const FileEntry *FEB = GetFile(b);
1558 return FEA && FEA == FEB;
Guy Benyei11169dd2012-12-18 14:30:41 +00001559}
1560
1561std::pair<unsigned, unsigned>
1562HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
Justin Bogner57ba0b22014-03-28 22:03:24 +00001563 using namespace llvm::support;
1564 unsigned KeyLen = (unsigned) endian::readNext<uint16_t, little, unaligned>(d);
Guy Benyei11169dd2012-12-18 14:30:41 +00001565 unsigned DataLen = (unsigned) *d++;
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001566 return std::make_pair(KeyLen, DataLen);
Guy Benyei11169dd2012-12-18 14:30:41 +00001567}
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001568
1569HeaderFileInfoTrait::internal_key_type
1570HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
Justin Bogner57ba0b22014-03-28 22:03:24 +00001571 using namespace llvm::support;
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001572 internal_key_type ikey;
Justin Bogner57ba0b22014-03-28 22:03:24 +00001573 ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d));
1574 ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d));
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001575 ikey.Filename = (const char *)d;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001576 ikey.Imported = true;
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001577 return ikey;
1578}
1579
Guy Benyei11169dd2012-12-18 14:30:41 +00001580HeaderFileInfoTrait::data_type
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001581HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
Guy Benyei11169dd2012-12-18 14:30:41 +00001582 unsigned DataLen) {
1583 const unsigned char *End = d + DataLen;
Justin Bogner57ba0b22014-03-28 22:03:24 +00001584 using namespace llvm::support;
Guy Benyei11169dd2012-12-18 14:30:41 +00001585 HeaderFileInfo HFI;
1586 unsigned Flags = *d++;
Richard Smith386bb072015-08-18 23:42:23 +00001587 // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp.
1588 HFI.isImport |= (Flags >> 4) & 0x01;
1589 HFI.isPragmaOnce |= (Flags >> 3) & 0x01;
1590 HFI.DirInfo = (Flags >> 1) & 0x03;
Guy Benyei11169dd2012-12-18 14:30:41 +00001591 HFI.IndexHeaderMapHeader = Flags & 0x01;
Richard Smith386bb072015-08-18 23:42:23 +00001592 // FIXME: Find a better way to handle this. Maybe just store a
1593 // "has been included" flag?
1594 HFI.NumIncludes = std::max(endian::readNext<uint16_t, little, unaligned>(d),
1595 HFI.NumIncludes);
Justin Bogner57ba0b22014-03-28 22:03:24 +00001596 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(
1597 M, endian::readNext<uint32_t, little, unaligned>(d));
1598 if (unsigned FrameworkOffset =
1599 endian::readNext<uint32_t, little, unaligned>(d)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001600 // The framework offset is 1 greater than the actual offset,
1601 // since 0 is used as an indicator for "no framework name".
1602 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1603 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1604 }
Richard Smith386bb072015-08-18 23:42:23 +00001605
1606 assert((End - d) % 4 == 0 &&
1607 "Wrong data length in HeaderFileInfo deserialization");
1608 while (d != End) {
Justin Bogner57ba0b22014-03-28 22:03:24 +00001609 uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d);
Richard Smith386bb072015-08-18 23:42:23 +00001610 auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 3);
1611 LocalSMID >>= 2;
1612
1613 // This header is part of a module. Associate it with the module to enable
1614 // implicit module import.
1615 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1616 Module *Mod = Reader.getSubmodule(GlobalSMID);
1617 FileManager &FileMgr = Reader.getFileManager();
1618 ModuleMap &ModMap =
1619 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
1620
1621 std::string Filename = key.Filename;
1622 if (key.Imported)
1623 Reader.ResolveImportedPath(M, Filename);
1624 // FIXME: This is not always the right filename-as-written, but we're not
1625 // going to use this information to rebuild the module, so it doesn't make
1626 // a lot of difference.
1627 Module::Header H = { key.Filename, FileMgr.getFile(Filename) };
Richard Smithd8879c82015-08-24 21:59:32 +00001628 ModMap.addHeader(Mod, H, HeaderRole, /*Imported*/true);
1629 HFI.isModuleHeader |= !(HeaderRole & ModuleMap::TextualHeader);
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001630 }
1631
Guy Benyei11169dd2012-12-18 14:30:41 +00001632 // This HeaderFileInfo was externally loaded.
1633 HFI.External = true;
Richard Smithd8879c82015-08-24 21:59:32 +00001634 HFI.IsValid = true;
Guy Benyei11169dd2012-12-18 14:30:41 +00001635 return HFI;
1636}
1637
Richard Smithd7329392015-04-21 21:46:32 +00001638void ASTReader::addPendingMacro(IdentifierInfo *II,
1639 ModuleFile *M,
1640 uint64_t MacroDirectivesOffset) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001641 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1642 PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00001643}
1644
1645void ASTReader::ReadDefinedMacros() {
1646 // Note that we are loading defined macros.
1647 Deserializing Macros(this);
1648
Pete Cooper57d3f142015-07-30 17:22:52 +00001649 for (auto &I : llvm::reverse(ModuleMgr)) {
1650 BitstreamCursor &MacroCursor = I->MacroCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001651
1652 // If there was no preprocessor block, skip this file.
1653 if (!MacroCursor.getBitStreamReader())
1654 continue;
1655
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001656 BitstreamCursor Cursor = MacroCursor;
Pete Cooper57d3f142015-07-30 17:22:52 +00001657 Cursor.JumpToBit(I->MacroStartOffset);
Guy Benyei11169dd2012-12-18 14:30:41 +00001658
1659 RecordData Record;
1660 while (true) {
Chris Lattnere7b154b2013-01-19 21:39:22 +00001661 llvm::BitstreamEntry E = Cursor.advanceSkippingSubblocks();
1662
1663 switch (E.Kind) {
1664 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1665 case llvm::BitstreamEntry::Error:
1666 Error("malformed block record in AST file");
1667 return;
1668 case llvm::BitstreamEntry::EndBlock:
1669 goto NextCursor;
1670
1671 case llvm::BitstreamEntry::Record:
Chris Lattnere7b154b2013-01-19 21:39:22 +00001672 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00001673 switch (Cursor.readRecord(E.ID, Record)) {
Chris Lattnere7b154b2013-01-19 21:39:22 +00001674 default: // Default behavior: ignore.
1675 break;
1676
1677 case PP_MACRO_OBJECT_LIKE:
1678 case PP_MACRO_FUNCTION_LIKE:
Pete Cooper57d3f142015-07-30 17:22:52 +00001679 getLocalIdentifier(*I, Record[0]);
Chris Lattnere7b154b2013-01-19 21:39:22 +00001680 break;
1681
1682 case PP_TOKEN:
1683 // Ignore tokens.
1684 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001685 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001686 break;
1687 }
1688 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00001689 NextCursor: ;
Guy Benyei11169dd2012-12-18 14:30:41 +00001690 }
1691}
1692
1693namespace {
1694 /// \brief Visitor class used to look up identifirs in an AST file.
1695 class IdentifierLookupVisitor {
1696 StringRef Name;
Richard Smith3b637412015-07-14 18:42:41 +00001697 unsigned NameHash;
Guy Benyei11169dd2012-12-18 14:30:41 +00001698 unsigned PriorGeneration;
Douglas Gregor00a50f72013-01-25 00:38:33 +00001699 unsigned &NumIdentifierLookups;
1700 unsigned &NumIdentifierLookupHits;
Guy Benyei11169dd2012-12-18 14:30:41 +00001701 IdentifierInfo *Found;
Douglas Gregor00a50f72013-01-25 00:38:33 +00001702
Guy Benyei11169dd2012-12-18 14:30:41 +00001703 public:
Douglas Gregor00a50f72013-01-25 00:38:33 +00001704 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
1705 unsigned &NumIdentifierLookups,
1706 unsigned &NumIdentifierLookupHits)
Richard Smith3b637412015-07-14 18:42:41 +00001707 : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)),
1708 PriorGeneration(PriorGeneration),
Douglas Gregor00a50f72013-01-25 00:38:33 +00001709 NumIdentifierLookups(NumIdentifierLookups),
1710 NumIdentifierLookupHits(NumIdentifierLookupHits),
1711 Found()
1712 {
1713 }
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00001714
1715 bool operator()(ModuleFile &M) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001716 // If we've already searched this module file, skip it now.
Richard Smithbdf2d932015-07-30 03:37:16 +00001717 if (M.Generation <= PriorGeneration)
Guy Benyei11169dd2012-12-18 14:30:41 +00001718 return true;
Douglas Gregore060e572013-01-25 01:03:03 +00001719
Guy Benyei11169dd2012-12-18 14:30:41 +00001720 ASTIdentifierLookupTable *IdTable
1721 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
1722 if (!IdTable)
1723 return false;
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00001724
1725 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M,
Richard Smithbdf2d932015-07-30 03:37:16 +00001726 Found);
1727 ++NumIdentifierLookups;
Richard Smith3b637412015-07-14 18:42:41 +00001728 ASTIdentifierLookupTable::iterator Pos =
Richard Smithbdf2d932015-07-30 03:37:16 +00001729 IdTable->find_hashed(Name, NameHash, &Trait);
Guy Benyei11169dd2012-12-18 14:30:41 +00001730 if (Pos == IdTable->end())
1731 return false;
1732
1733 // Dereferencing the iterator has the effect of building the
1734 // IdentifierInfo node and populating it with the various
1735 // declarations it needs.
Richard Smithbdf2d932015-07-30 03:37:16 +00001736 ++NumIdentifierLookupHits;
1737 Found = *Pos;
Guy Benyei11169dd2012-12-18 14:30:41 +00001738 return true;
1739 }
1740
1741 // \brief Retrieve the identifier info found within the module
1742 // files.
1743 IdentifierInfo *getIdentifierInfo() const { return Found; }
1744 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +00001745}
Guy Benyei11169dd2012-12-18 14:30:41 +00001746
1747void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
1748 // Note that we are loading an identifier.
1749 Deserializing AnIdentifier(this);
1750
1751 unsigned PriorGeneration = 0;
1752 if (getContext().getLangOpts().Modules)
1753 PriorGeneration = IdentifierGeneration[&II];
Douglas Gregore060e572013-01-25 01:03:03 +00001754
1755 // If there is a global index, look there first to determine which modules
1756 // provably do not have any results for this identifier.
Douglas Gregor7211ac12013-01-25 23:32:03 +00001757 GlobalModuleIndex::HitSet Hits;
Craig Toppera13603a2014-05-22 05:54:18 +00001758 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
Douglas Gregore060e572013-01-25 01:03:03 +00001759 if (!loadGlobalIndex()) {
Douglas Gregor7211ac12013-01-25 23:32:03 +00001760 if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
1761 HitsPtr = &Hits;
Douglas Gregore060e572013-01-25 01:03:03 +00001762 }
1763 }
1764
Douglas Gregor7211ac12013-01-25 23:32:03 +00001765 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
Douglas Gregor00a50f72013-01-25 00:38:33 +00001766 NumIdentifierLookups,
1767 NumIdentifierLookupHits);
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00001768 ModuleMgr.visit(Visitor, HitsPtr);
Guy Benyei11169dd2012-12-18 14:30:41 +00001769 markIdentifierUpToDate(&II);
1770}
1771
1772void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
1773 if (!II)
1774 return;
1775
1776 II->setOutOfDate(false);
1777
1778 // Update the generation for this identifier.
1779 if (getContext().getLangOpts().Modules)
Richard Smith053f6c62014-05-16 23:01:30 +00001780 IdentifierGeneration[II] = getGeneration();
Guy Benyei11169dd2012-12-18 14:30:41 +00001781}
1782
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001783void ASTReader::resolvePendingMacro(IdentifierInfo *II,
1784 const PendingMacroInfo &PMInfo) {
Richard Smithd7329392015-04-21 21:46:32 +00001785 ModuleFile &M = *PMInfo.M;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001786
1787 BitstreamCursor &Cursor = M.MacroCursor;
1788 SavedStreamPosition SavedPosition(Cursor);
Richard Smithd7329392015-04-21 21:46:32 +00001789 Cursor.JumpToBit(PMInfo.MacroDirectivesOffset);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001790
Richard Smith713369b2015-04-23 20:40:50 +00001791 struct ModuleMacroRecord {
1792 SubmoduleID SubModID;
1793 MacroInfo *MI;
1794 SmallVector<SubmoduleID, 8> Overrides;
1795 };
1796 llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001797
Richard Smithd7329392015-04-21 21:46:32 +00001798 // We expect to see a sequence of PP_MODULE_MACRO records listing exported
1799 // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
1800 // macro histroy.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001801 RecordData Record;
Richard Smithd7329392015-04-21 21:46:32 +00001802 while (true) {
1803 llvm::BitstreamEntry Entry =
1804 Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
1805 if (Entry.Kind != llvm::BitstreamEntry::Record) {
1806 Error("malformed block record in AST file");
1807 return;
1808 }
1809
1810 Record.clear();
Aaron Ballmanc75a1922015-04-22 15:25:05 +00001811 switch ((PreprocessorRecordTypes)Cursor.readRecord(Entry.ID, Record)) {
Richard Smithd7329392015-04-21 21:46:32 +00001812 case PP_MACRO_DIRECTIVE_HISTORY:
1813 break;
1814
1815 case PP_MODULE_MACRO: {
Richard Smith713369b2015-04-23 20:40:50 +00001816 ModuleMacros.push_back(ModuleMacroRecord());
1817 auto &Info = ModuleMacros.back();
Richard Smithe56c8bc2015-04-22 00:26:11 +00001818 Info.SubModID = getGlobalSubmoduleID(M, Record[0]);
1819 Info.MI = getMacro(getGlobalMacroID(M, Record[1]));
Richard Smith713369b2015-04-23 20:40:50 +00001820 for (int I = 2, N = Record.size(); I != N; ++I)
1821 Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I]));
Richard Smithd7329392015-04-21 21:46:32 +00001822 continue;
1823 }
1824
1825 default:
1826 Error("malformed block record in AST file");
1827 return;
1828 }
1829
1830 // We found the macro directive history; that's the last record
1831 // for this macro.
1832 break;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001833 }
1834
Richard Smithd7329392015-04-21 21:46:32 +00001835 // Module macros are listed in reverse dependency order.
Richard Smithe56c8bc2015-04-22 00:26:11 +00001836 {
1837 std::reverse(ModuleMacros.begin(), ModuleMacros.end());
Richard Smithe56c8bc2015-04-22 00:26:11 +00001838 llvm::SmallVector<ModuleMacro*, 8> Overrides;
Richard Smith713369b2015-04-23 20:40:50 +00001839 for (auto &MMR : ModuleMacros) {
Richard Smithe56c8bc2015-04-22 00:26:11 +00001840 Overrides.clear();
Richard Smith713369b2015-04-23 20:40:50 +00001841 for (unsigned ModID : MMR.Overrides) {
Richard Smithb8b2ed62015-04-23 18:18:26 +00001842 Module *Mod = getSubmodule(ModID);
1843 auto *Macro = PP.getModuleMacro(Mod, II);
Richard Smithe56c8bc2015-04-22 00:26:11 +00001844 assert(Macro && "missing definition for overridden macro");
Richard Smith5dbef922015-04-22 02:09:43 +00001845 Overrides.push_back(Macro);
Richard Smithe56c8bc2015-04-22 00:26:11 +00001846 }
1847
1848 bool Inserted = false;
Richard Smith713369b2015-04-23 20:40:50 +00001849 Module *Owner = getSubmodule(MMR.SubModID);
Richard Smith20e883e2015-04-29 23:20:19 +00001850 PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted);
Richard Smithd7329392015-04-21 21:46:32 +00001851 }
1852 }
1853
1854 // Don't read the directive history for a module; we don't have anywhere
1855 // to put it.
1856 if (M.Kind == MK_ImplicitModule || M.Kind == MK_ExplicitModule)
1857 return;
1858
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001859 // Deserialize the macro directives history in reverse source-order.
Craig Toppera13603a2014-05-22 05:54:18 +00001860 MacroDirective *Latest = nullptr, *Earliest = nullptr;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001861 unsigned Idx = 0, N = Record.size();
1862 while (Idx < N) {
Craig Toppera13603a2014-05-22 05:54:18 +00001863 MacroDirective *MD = nullptr;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001864 SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001865 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
1866 switch (K) {
1867 case MacroDirective::MD_Define: {
Richard Smith713369b2015-04-23 20:40:50 +00001868 MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++]));
Richard Smith3981b172015-04-30 02:16:23 +00001869 MD = PP.AllocateDefMacroDirective(MI, Loc);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001870 break;
1871 }
Richard Smithdaa69e02014-07-25 04:40:03 +00001872 case MacroDirective::MD_Undefine: {
Richard Smith3981b172015-04-30 02:16:23 +00001873 MD = PP.AllocateUndefMacroDirective(Loc);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001874 break;
Richard Smithdaa69e02014-07-25 04:40:03 +00001875 }
1876 case MacroDirective::MD_Visibility:
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001877 bool isPublic = Record[Idx++];
1878 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
1879 break;
1880 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001881
1882 if (!Latest)
1883 Latest = MD;
1884 if (Earliest)
1885 Earliest->setPrevious(MD);
1886 Earliest = MD;
1887 }
1888
Richard Smithd6e8c0d2015-05-04 19:58:00 +00001889 if (Latest)
1890 PP.setLoadedMacroDirective(II, Latest);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001891}
1892
Argyrios Kyrtzidisce9b49e2014-03-14 02:26:27 +00001893ASTReader::InputFileInfo
1894ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) {
Ben Langmuir198c1682014-03-07 07:27:49 +00001895 // Go find this input file.
1896 BitstreamCursor &Cursor = F.InputFilesCursor;
1897 SavedStreamPosition SavedPosition(Cursor);
1898 Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
1899
1900 unsigned Code = Cursor.ReadCode();
1901 RecordData Record;
1902 StringRef Blob;
1903
1904 unsigned Result = Cursor.readRecord(Code, Record, &Blob);
1905 assert(static_cast<InputFileRecordTypes>(Result) == INPUT_FILE &&
1906 "invalid record type for input file");
1907 (void)Result;
1908
1909 assert(Record[0] == ID && "Bogus stored ID or offset");
Richard Smitha8cfffa2015-11-26 02:04:16 +00001910 InputFileInfo R;
1911 R.StoredSize = static_cast<off_t>(Record[1]);
1912 R.StoredTime = static_cast<time_t>(Record[2]);
1913 R.Overridden = static_cast<bool>(Record[3]);
1914 R.Transient = static_cast<bool>(Record[4]);
1915 R.Filename = Blob;
1916 ResolveImportedPath(F, R.Filename);
Hans Wennborg73945142014-03-14 17:45:06 +00001917 return R;
Ben Langmuir198c1682014-03-07 07:27:49 +00001918}
1919
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001920InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001921 // If this ID is bogus, just return an empty input file.
1922 if (ID == 0 || ID > F.InputFilesLoaded.size())
1923 return InputFile();
1924
1925 // If we've already loaded this input file, return it.
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001926 if (F.InputFilesLoaded[ID-1].getFile())
Guy Benyei11169dd2012-12-18 14:30:41 +00001927 return F.InputFilesLoaded[ID-1];
1928
Argyrios Kyrtzidis9308f0a2014-01-08 19:13:34 +00001929 if (F.InputFilesLoaded[ID-1].isNotFound())
1930 return InputFile();
1931
Guy Benyei11169dd2012-12-18 14:30:41 +00001932 // Go find this input file.
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001933 BitstreamCursor &Cursor = F.InputFilesCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001934 SavedStreamPosition SavedPosition(Cursor);
1935 Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
1936
Argyrios Kyrtzidisce9b49e2014-03-14 02:26:27 +00001937 InputFileInfo FI = readInputFileInfo(F, ID);
1938 off_t StoredSize = FI.StoredSize;
1939 time_t StoredTime = FI.StoredTime;
1940 bool Overridden = FI.Overridden;
Richard Smitha8cfffa2015-11-26 02:04:16 +00001941 bool Transient = FI.Transient;
Argyrios Kyrtzidisce9b49e2014-03-14 02:26:27 +00001942 StringRef Filename = FI.Filename;
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001943
Richard Smitha8cfffa2015-11-26 02:04:16 +00001944 const FileEntry *File = FileMgr.getFile(Filename, /*OpenFile=*/false);
Ben Langmuir198c1682014-03-07 07:27:49 +00001945
1946 // If we didn't find the file, resolve it relative to the
1947 // original directory from which this AST file was created.
Craig Toppera13603a2014-05-22 05:54:18 +00001948 if (File == nullptr && !F.OriginalDir.empty() && !CurrentDir.empty() &&
Ben Langmuir198c1682014-03-07 07:27:49 +00001949 F.OriginalDir != CurrentDir) {
1950 std::string Resolved = resolveFileRelativeToOriginalDir(Filename,
1951 F.OriginalDir,
1952 CurrentDir);
1953 if (!Resolved.empty())
1954 File = FileMgr.getFile(Resolved);
1955 }
1956
1957 // For an overridden file, create a virtual file with the stored
1958 // size/timestamp.
Richard Smitha8cfffa2015-11-26 02:04:16 +00001959 if ((Overridden || Transient) && File == nullptr)
Ben Langmuir198c1682014-03-07 07:27:49 +00001960 File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
Ben Langmuir198c1682014-03-07 07:27:49 +00001961
Craig Toppera13603a2014-05-22 05:54:18 +00001962 if (File == nullptr) {
Ben Langmuir198c1682014-03-07 07:27:49 +00001963 if (Complain) {
1964 std::string ErrorStr = "could not find file '";
1965 ErrorStr += Filename;
Richard Smith68142212015-10-13 01:26:26 +00001966 ErrorStr += "' referenced by AST file '";
1967 ErrorStr += F.FileName;
1968 ErrorStr += "'";
Ben Langmuir198c1682014-03-07 07:27:49 +00001969 Error(ErrorStr.c_str());
Guy Benyei11169dd2012-12-18 14:30:41 +00001970 }
Ben Langmuir198c1682014-03-07 07:27:49 +00001971 // Record that we didn't find the file.
1972 F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
1973 return InputFile();
1974 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001975
Ben Langmuir198c1682014-03-07 07:27:49 +00001976 // Check if there was a request to override the contents of the file
1977 // that was part of the precompiled header. Overridding such a file
1978 // can lead to problems when lexing using the source locations from the
1979 // PCH.
1980 SourceManager &SM = getSourceManager();
Richard Smith64daf7b2015-12-01 03:32:49 +00001981 // FIXME: Reject if the overrides are different.
1982 if ((!Overridden && !Transient) && SM.isFileOverridden(File)) {
Ben Langmuir198c1682014-03-07 07:27:49 +00001983 if (Complain)
1984 Error(diag::err_fe_pch_file_overridden, Filename);
1985 // After emitting the diagnostic, recover by disabling the override so
1986 // that the original file will be used.
Richard Smitha8cfffa2015-11-26 02:04:16 +00001987 //
1988 // FIXME: This recovery is just as broken as the original state; there may
1989 // be another precompiled module that's using the overridden contents, or
1990 // we might be half way through parsing it. Instead, we should treat the
1991 // overridden contents as belonging to a separate FileEntry.
Ben Langmuir198c1682014-03-07 07:27:49 +00001992 SM.disableFileContentsOverride(File);
1993 // The FileEntry is a virtual file entry with the size of the contents
1994 // that would override the original contents. Set it to the original's
1995 // size/time.
1996 FileMgr.modifyFileEntry(const_cast<FileEntry*>(File),
1997 StoredSize, StoredTime);
1998 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001999
Ben Langmuir198c1682014-03-07 07:27:49 +00002000 bool IsOutOfDate = false;
2001
2002 // For an overridden file, there is nothing to validate.
Richard Smith96fdab62014-10-28 16:24:08 +00002003 if (!Overridden && //
2004 (StoredSize != File->getSize() ||
2005#if defined(LLVM_ON_WIN32)
2006 false
2007#else
Ben Langmuir198c1682014-03-07 07:27:49 +00002008 // In our regression testing, the Windows file system seems to
2009 // have inconsistent modification times that sometimes
2010 // erroneously trigger this error-handling path.
Richard Smith96fdab62014-10-28 16:24:08 +00002011 //
Richard Smithe75ee0f2015-08-17 07:13:32 +00002012 // FIXME: This probably also breaks HeaderFileInfo lookups on Windows.
2013 (StoredTime && StoredTime != File->getModificationTime() &&
2014 !DisableValidation)
Guy Benyei11169dd2012-12-18 14:30:41 +00002015#endif
Ben Langmuir198c1682014-03-07 07:27:49 +00002016 )) {
2017 if (Complain) {
2018 // Build a list of the PCH imports that got us here (in reverse).
2019 SmallVector<ModuleFile *, 4> ImportStack(1, &F);
2020 while (ImportStack.back()->ImportedBy.size() > 0)
2021 ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
Ben Langmuire82630d2014-01-17 00:19:09 +00002022
Ben Langmuir198c1682014-03-07 07:27:49 +00002023 // The top-level PCH is stale.
2024 StringRef TopLevelPCHName(ImportStack.back()->FileName);
2025 Error(diag::err_fe_pch_file_modified, Filename, TopLevelPCHName);
Ben Langmuire82630d2014-01-17 00:19:09 +00002026
Ben Langmuir198c1682014-03-07 07:27:49 +00002027 // Print the import stack.
2028 if (ImportStack.size() > 1 && !Diags.isDiagnosticInFlight()) {
2029 Diag(diag::note_pch_required_by)
2030 << Filename << ImportStack[0]->FileName;
2031 for (unsigned I = 1; I < ImportStack.size(); ++I)
Ben Langmuire82630d2014-01-17 00:19:09 +00002032 Diag(diag::note_pch_required_by)
Ben Langmuir198c1682014-03-07 07:27:49 +00002033 << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
Douglas Gregor7029ce12013-03-19 00:28:20 +00002034 }
2035
Ben Langmuir198c1682014-03-07 07:27:49 +00002036 if (!Diags.isDiagnosticInFlight())
2037 Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
Guy Benyei11169dd2012-12-18 14:30:41 +00002038 }
2039
Ben Langmuir198c1682014-03-07 07:27:49 +00002040 IsOutOfDate = true;
Guy Benyei11169dd2012-12-18 14:30:41 +00002041 }
Richard Smitha8cfffa2015-11-26 02:04:16 +00002042 // FIXME: If the file is overridden and we've already opened it,
2043 // issue an error (or split it into a separate FileEntry).
Guy Benyei11169dd2012-12-18 14:30:41 +00002044
Richard Smitha8cfffa2015-11-26 02:04:16 +00002045 InputFile IF = InputFile(File, Overridden || Transient, IsOutOfDate);
Ben Langmuir198c1682014-03-07 07:27:49 +00002046
2047 // Note that we've loaded this input file.
2048 F.InputFilesLoaded[ID-1] = IF;
2049 return IF;
Guy Benyei11169dd2012-12-18 14:30:41 +00002050}
2051
Richard Smith7ed1bc92014-12-05 22:42:13 +00002052/// \brief If we are loading a relocatable PCH or module file, and the filename
2053/// is not an absolute path, add the system or module root to the beginning of
2054/// the file name.
2055void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) {
2056 // Resolve relative to the base directory, if we have one.
2057 if (!M.BaseDirectory.empty())
2058 return ResolveImportedPath(Filename, M.BaseDirectory);
Guy Benyei11169dd2012-12-18 14:30:41 +00002059}
2060
Richard Smith7ed1bc92014-12-05 22:42:13 +00002061void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002062 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
2063 return;
2064
Richard Smith7ed1bc92014-12-05 22:42:13 +00002065 SmallString<128> Buffer;
2066 llvm::sys::path::append(Buffer, Prefix, Filename);
2067 Filename.assign(Buffer.begin(), Buffer.end());
Guy Benyei11169dd2012-12-18 14:30:41 +00002068}
2069
Richard Smith0f99d6a2015-08-09 08:48:41 +00002070static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) {
2071 switch (ARR) {
2072 case ASTReader::Failure: return true;
2073 case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing);
2074 case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate);
2075 case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch);
2076 case ASTReader::ConfigurationMismatch:
2077 return !(Caps & ASTReader::ARR_ConfigurationMismatch);
2078 case ASTReader::HadErrors: return true;
2079 case ASTReader::Success: return false;
2080 }
2081
2082 llvm_unreachable("unknown ASTReadResult");
2083}
2084
Richard Smith0516b182015-09-08 19:40:14 +00002085ASTReader::ASTReadResult ASTReader::ReadOptionsBlock(
2086 BitstreamCursor &Stream, unsigned ClientLoadCapabilities,
2087 bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener,
2088 std::string &SuggestedPredefines) {
2089 if (Stream.EnterSubBlock(OPTIONS_BLOCK_ID))
2090 return Failure;
2091
2092 // Read all of the records in the options block.
2093 RecordData Record;
2094 ASTReadResult Result = Success;
2095 while (1) {
2096 llvm::BitstreamEntry Entry = Stream.advance();
2097
2098 switch (Entry.Kind) {
2099 case llvm::BitstreamEntry::Error:
2100 case llvm::BitstreamEntry::SubBlock:
2101 return Failure;
2102
2103 case llvm::BitstreamEntry::EndBlock:
2104 return Result;
2105
2106 case llvm::BitstreamEntry::Record:
2107 // The interesting case.
2108 break;
2109 }
2110
2111 // Read and process a record.
2112 Record.clear();
2113 switch ((OptionsRecordTypes)Stream.readRecord(Entry.ID, Record)) {
2114 case LANGUAGE_OPTIONS: {
2115 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2116 if (ParseLanguageOptions(Record, Complain, Listener,
2117 AllowCompatibleConfigurationMismatch))
2118 Result = ConfigurationMismatch;
2119 break;
2120 }
2121
2122 case TARGET_OPTIONS: {
2123 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2124 if (ParseTargetOptions(Record, Complain, Listener,
2125 AllowCompatibleConfigurationMismatch))
2126 Result = ConfigurationMismatch;
2127 break;
2128 }
2129
2130 case DIAGNOSTIC_OPTIONS: {
2131 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
2132 if (!AllowCompatibleConfigurationMismatch &&
2133 ParseDiagnosticOptions(Record, Complain, Listener))
2134 return OutOfDate;
2135 break;
2136 }
2137
2138 case FILE_SYSTEM_OPTIONS: {
2139 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2140 if (!AllowCompatibleConfigurationMismatch &&
2141 ParseFileSystemOptions(Record, Complain, Listener))
2142 Result = ConfigurationMismatch;
2143 break;
2144 }
2145
2146 case HEADER_SEARCH_OPTIONS: {
2147 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2148 if (!AllowCompatibleConfigurationMismatch &&
2149 ParseHeaderSearchOptions(Record, Complain, Listener))
2150 Result = ConfigurationMismatch;
2151 break;
2152 }
2153
2154 case PREPROCESSOR_OPTIONS:
2155 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2156 if (!AllowCompatibleConfigurationMismatch &&
2157 ParsePreprocessorOptions(Record, Complain, Listener,
2158 SuggestedPredefines))
2159 Result = ConfigurationMismatch;
2160 break;
2161 }
2162 }
2163}
2164
Guy Benyei11169dd2012-12-18 14:30:41 +00002165ASTReader::ASTReadResult
2166ASTReader::ReadControlBlock(ModuleFile &F,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002167 SmallVectorImpl<ImportedModule> &Loaded,
Ben Langmuirbeee15e2014-04-14 18:00:01 +00002168 const ModuleFile *ImportedBy,
Guy Benyei11169dd2012-12-18 14:30:41 +00002169 unsigned ClientLoadCapabilities) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00002170 BitstreamCursor &Stream = F.Stream;
Richard Smith8a308ec2015-11-05 00:54:55 +00002171 ASTReadResult Result = Success;
Guy Benyei11169dd2012-12-18 14:30:41 +00002172
2173 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
2174 Error("malformed block record in AST file");
2175 return Failure;
2176 }
2177
2178 // Read all of the records and blocks in the control block.
2179 RecordData Record;
Richard Smitha1825302014-10-23 22:18:29 +00002180 unsigned NumInputs = 0;
2181 unsigned NumUserInputs = 0;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002182 while (1) {
2183 llvm::BitstreamEntry Entry = Stream.advance();
2184
2185 switch (Entry.Kind) {
2186 case llvm::BitstreamEntry::Error:
2187 Error("malformed block record in AST file");
2188 return Failure;
Dmitri Gribenkof430da42014-02-12 10:33:14 +00002189 case llvm::BitstreamEntry::EndBlock: {
2190 // Validate input files.
2191 const HeaderSearchOptions &HSOpts =
2192 PP.getHeaderSearchInfo().getHeaderSearchOpts();
Ben Langmuircb69b572014-03-07 06:40:32 +00002193
Richard Smitha1825302014-10-23 22:18:29 +00002194 // All user input files reside at the index range [0, NumUserInputs), and
Richard Smith0f99d6a2015-08-09 08:48:41 +00002195 // system input files reside at [NumUserInputs, NumInputs). For explicitly
2196 // loaded module files, ignore missing inputs.
2197 if (!DisableValidation && F.Kind != MK_ExplicitModule) {
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,
2228 F.Kind == MK_ExplicitModule);
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +00002229 }
Ben Langmuircb69b572014-03-07 06:40:32 +00002230 }
2231
Richard Smith8a308ec2015-11-05 00:54:55 +00002232 return Result;
Dmitri Gribenkof430da42014-02-12 10:33:14 +00002233 }
2234
Chris Lattnere7b154b2013-01-19 21:39:22 +00002235 case llvm::BitstreamEntry::SubBlock:
2236 switch (Entry.ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002237 case INPUT_FILES_BLOCK_ID:
2238 F.InputFilesCursor = Stream;
2239 if (Stream.SkipBlock() || // Skip with the main cursor
2240 // Read the abbreviations
2241 ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
2242 Error("malformed block record in AST file");
2243 return Failure;
2244 }
2245 continue;
Richard Smith0516b182015-09-08 19:40:14 +00002246
2247 case OPTIONS_BLOCK_ID:
2248 // If we're reading the first module for this group, check its options
2249 // are compatible with ours. For modules it imports, no further checking
2250 // is required, because we checked them when we built it.
2251 if (Listener && !ImportedBy) {
2252 // Should we allow the configuration of the module file to differ from
2253 // the configuration of the current translation unit in a compatible
2254 // way?
2255 //
2256 // FIXME: Allow this for files explicitly specified with -include-pch.
2257 bool AllowCompatibleConfigurationMismatch =
2258 F.Kind == MK_ExplicitModule;
2259
Richard Smith8a308ec2015-11-05 00:54:55 +00002260 Result = ReadOptionsBlock(Stream, ClientLoadCapabilities,
2261 AllowCompatibleConfigurationMismatch,
2262 *Listener, SuggestedPredefines);
Richard Smith0516b182015-09-08 19:40:14 +00002263 if (Result == Failure) {
2264 Error("malformed block record in AST file");
2265 return Result;
2266 }
2267
Richard Smith8a308ec2015-11-05 00:54:55 +00002268 if (DisableValidation ||
2269 (AllowConfigurationMismatch && Result == ConfigurationMismatch))
2270 Result = Success;
2271
Ben Langmuir9b1e442e2016-02-11 18:54:02 +00002272 // If we can't load the module, exit early since we likely
2273 // will rebuild the module anyway. The stream may be in the
2274 // middle of a block.
2275 if (Result != Success)
Richard Smith0516b182015-09-08 19:40:14 +00002276 return Result;
2277 } else if (Stream.SkipBlock()) {
2278 Error("malformed block record in AST file");
2279 return Failure;
2280 }
2281 continue;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002282
Guy Benyei11169dd2012-12-18 14:30:41 +00002283 default:
Chris Lattnere7b154b2013-01-19 21:39:22 +00002284 if (Stream.SkipBlock()) {
2285 Error("malformed block record in AST file");
2286 return Failure;
2287 }
2288 continue;
Guy Benyei11169dd2012-12-18 14:30:41 +00002289 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00002290
2291 case llvm::BitstreamEntry::Record:
2292 // The interesting case.
2293 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002294 }
2295
2296 // Read and process a record.
2297 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00002298 StringRef Blob;
2299 switch ((ControlRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002300 case METADATA: {
2301 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2302 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
Dmitri Gribenko2228cd32014-02-11 15:40:09 +00002303 Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
2304 : diag::err_pch_version_too_new);
Guy Benyei11169dd2012-12-18 14:30:41 +00002305 return VersionMismatch;
2306 }
2307
Richard Smithe75ee0f2015-08-17 07:13:32 +00002308 bool hasErrors = Record[6];
Guy Benyei11169dd2012-12-18 14:30:41 +00002309 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
2310 Diag(diag::err_pch_with_compiler_errors);
2311 return HadErrors;
2312 }
2313
2314 F.RelocatablePCH = Record[4];
Richard Smith7ed1bc92014-12-05 22:42:13 +00002315 // Relative paths in a relocatable PCH are relative to our sysroot.
2316 if (F.RelocatablePCH)
2317 F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
Guy Benyei11169dd2012-12-18 14:30:41 +00002318
Richard Smithe75ee0f2015-08-17 07:13:32 +00002319 F.HasTimestamps = Record[5];
2320
Guy Benyei11169dd2012-12-18 14:30:41 +00002321 const std::string &CurBranch = getClangFullRepositoryVersion();
Chris Lattner0e6c9402013-01-20 02:38:54 +00002322 StringRef ASTBranch = Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00002323 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2324 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
Dmitri Gribenko2228cd32014-02-11 15:40:09 +00002325 Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
Guy Benyei11169dd2012-12-18 14:30:41 +00002326 return VersionMismatch;
2327 }
2328 break;
2329 }
2330
Ben Langmuir487ea142014-10-23 18:05:36 +00002331 case SIGNATURE:
2332 assert((!F.Signature || F.Signature == Record[0]) && "signature changed");
2333 F.Signature = Record[0];
2334 break;
2335
Guy Benyei11169dd2012-12-18 14:30:41 +00002336 case IMPORTS: {
2337 // Load each of the imported PCH files.
2338 unsigned Idx = 0, N = Record.size();
2339 while (Idx < N) {
2340 // Read information about the AST file.
2341 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
2342 // The import location will be the local one for now; we will adjust
2343 // all import locations of module imports after the global source
2344 // location info are setup.
2345 SourceLocation ImportLoc =
2346 SourceLocation::getFromRawEncoding(Record[Idx++]);
Douglas Gregor7029ce12013-03-19 00:28:20 +00002347 off_t StoredSize = (off_t)Record[Idx++];
2348 time_t StoredModTime = (time_t)Record[Idx++];
Ben Langmuir487ea142014-10-23 18:05:36 +00002349 ASTFileSignature StoredSignature = Record[Idx++];
Richard Smith7ed1bc92014-12-05 22:42:13 +00002350 auto ImportedFile = ReadPath(F, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00002351
Richard Smith0f99d6a2015-08-09 08:48:41 +00002352 // If our client can't cope with us being out of date, we can't cope with
2353 // our dependency being missing.
2354 unsigned Capabilities = ClientLoadCapabilities;
2355 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2356 Capabilities &= ~ARR_Missing;
2357
Guy Benyei11169dd2012-12-18 14:30:41 +00002358 // Load the AST file.
Richard Smith0f99d6a2015-08-09 08:48:41 +00002359 auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F,
2360 Loaded, StoredSize, StoredModTime,
2361 StoredSignature, Capabilities);
2362
2363 // If we diagnosed a problem, produce a backtrace.
2364 if (isDiagnosedResult(Result, Capabilities))
2365 Diag(diag::note_module_file_imported_by)
2366 << F.FileName << !F.ModuleName.empty() << F.ModuleName;
2367
2368 switch (Result) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002369 case Failure: return Failure;
2370 // If we have to ignore the dependency, we'll have to ignore this too.
Douglas Gregor2f1806e2013-03-19 00:38:50 +00002371 case Missing:
Guy Benyei11169dd2012-12-18 14:30:41 +00002372 case OutOfDate: return OutOfDate;
2373 case VersionMismatch: return VersionMismatch;
2374 case ConfigurationMismatch: return ConfigurationMismatch;
2375 case HadErrors: return HadErrors;
2376 case Success: break;
2377 }
2378 }
2379 break;
2380 }
2381
Guy Benyei11169dd2012-12-18 14:30:41 +00002382 case ORIGINAL_FILE:
2383 F.OriginalSourceFileID = FileID::get(Record[0]);
Chris Lattner0e6c9402013-01-20 02:38:54 +00002384 F.ActualOriginalSourceFileName = Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00002385 F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
Richard Smith7ed1bc92014-12-05 22:42:13 +00002386 ResolveImportedPath(F, F.OriginalSourceFileName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002387 break;
2388
2389 case ORIGINAL_FILE_ID:
2390 F.OriginalSourceFileID = FileID::get(Record[0]);
2391 break;
2392
2393 case ORIGINAL_PCH_DIR:
Chris Lattner0e6c9402013-01-20 02:38:54 +00002394 F.OriginalDir = Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00002395 break;
2396
Ben Langmuirbeee15e2014-04-14 18:00:01 +00002397 case MODULE_NAME:
2398 F.ModuleName = Blob;
Ben Langmuir4f5212a2014-04-14 22:12:44 +00002399 if (Listener)
2400 Listener->ReadModuleName(F.ModuleName);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00002401 break;
2402
Richard Smith223d3f22014-12-06 03:21:08 +00002403 case MODULE_DIRECTORY: {
2404 assert(!F.ModuleName.empty() &&
2405 "MODULE_DIRECTORY found before MODULE_NAME");
2406 // If we've already loaded a module map file covering this module, we may
2407 // have a better path for it (relative to the current build).
2408 Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
2409 if (M && M->Directory) {
2410 // If we're implicitly loading a module, the base directory can't
2411 // change between the build and use.
2412 if (F.Kind != MK_ExplicitModule) {
2413 const DirectoryEntry *BuildDir =
2414 PP.getFileManager().getDirectory(Blob);
2415 if (!BuildDir || BuildDir != M->Directory) {
2416 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2417 Diag(diag::err_imported_module_relocated)
2418 << F.ModuleName << Blob << M->Directory->getName();
2419 return OutOfDate;
2420 }
2421 }
2422 F.BaseDirectory = M->Directory->getName();
2423 } else {
2424 F.BaseDirectory = Blob;
2425 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00002426 break;
Richard Smith223d3f22014-12-06 03:21:08 +00002427 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00002428
Ben Langmuirbeee15e2014-04-14 18:00:01 +00002429 case MODULE_MAP_FILE:
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00002430 if (ASTReadResult Result =
2431 ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
2432 return Result;
Ben Langmuir264ea152014-11-08 00:06:39 +00002433 break;
2434
Justin Bognerca9c0cc2015-06-21 20:32:36 +00002435 case INPUT_FILE_OFFSETS:
Richard Smitha1825302014-10-23 22:18:29 +00002436 NumInputs = Record[0];
2437 NumUserInputs = Record[1];
Justin Bogner4c183242015-06-21 20:32:40 +00002438 F.InputFileOffsets =
2439 (const llvm::support::unaligned_uint64_t *)Blob.data();
Richard Smitha1825302014-10-23 22:18:29 +00002440 F.InputFilesLoaded.resize(NumInputs);
Guy Benyei11169dd2012-12-18 14:30:41 +00002441 break;
2442 }
2443 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002444}
2445
Ben Langmuir2c9af442014-04-10 17:57:43 +00002446ASTReader::ASTReadResult
2447ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00002448 BitstreamCursor &Stream = F.Stream;
Guy Benyei11169dd2012-12-18 14:30:41 +00002449
2450 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
2451 Error("malformed block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002452 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002453 }
2454
2455 // Read all of the records and blocks for the AST file.
2456 RecordData Record;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002457 while (1) {
2458 llvm::BitstreamEntry Entry = Stream.advance();
2459
2460 switch (Entry.Kind) {
2461 case llvm::BitstreamEntry::Error:
2462 Error("error at end of module block in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002463 return Failure;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002464 case llvm::BitstreamEntry::EndBlock: {
Richard Smithc0fbba72013-04-03 22:49:41 +00002465 // Outside of C++, we do not store a lookup map for the translation unit.
2466 // Instead, mark it as needing a lookup map to be built if this module
2467 // contains any declarations lexically within it (which it always does!).
2468 // This usually has no cost, since we very rarely need the lookup map for
2469 // the translation unit outside C++.
Guy Benyei11169dd2012-12-18 14:30:41 +00002470 DeclContext *DC = Context.getTranslationUnitDecl();
Richard Smithc0fbba72013-04-03 22:49:41 +00002471 if (DC->hasExternalLexicalStorage() &&
2472 !getContext().getLangOpts().CPlusPlus)
Guy Benyei11169dd2012-12-18 14:30:41 +00002473 DC->setMustBuildLookupTable();
Chris Lattnere7b154b2013-01-19 21:39:22 +00002474
Ben Langmuir2c9af442014-04-10 17:57:43 +00002475 return Success;
Guy Benyei11169dd2012-12-18 14:30:41 +00002476 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00002477 case llvm::BitstreamEntry::SubBlock:
2478 switch (Entry.ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002479 case DECLTYPES_BLOCK_ID:
2480 // We lazily load the decls block, but we want to set up the
2481 // DeclsCursor cursor to point into it. Clone our current bitcode
2482 // cursor to it, enter the block and read the abbrevs in that block.
2483 // With the main cursor, we just skip over it.
2484 F.DeclsCursor = Stream;
2485 if (Stream.SkipBlock() || // Skip with the main cursor.
2486 // Read the abbrevs.
2487 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
2488 Error("malformed block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002489 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002490 }
2491 break;
Richard Smithb9eab6d2014-03-20 19:44:17 +00002492
Guy Benyei11169dd2012-12-18 14:30:41 +00002493 case PREPROCESSOR_BLOCK_ID:
2494 F.MacroCursor = Stream;
2495 if (!PP.getExternalSource())
2496 PP.setExternalSource(this);
Chris Lattnere7b154b2013-01-19 21:39:22 +00002497
Guy Benyei11169dd2012-12-18 14:30:41 +00002498 if (Stream.SkipBlock() ||
2499 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
2500 Error("malformed block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002501 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002502 }
2503 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
2504 break;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002505
Guy Benyei11169dd2012-12-18 14:30:41 +00002506 case PREPROCESSOR_DETAIL_BLOCK_ID:
2507 F.PreprocessorDetailCursor = Stream;
2508 if (Stream.SkipBlock() ||
Chris Lattnere7b154b2013-01-19 21:39:22 +00002509 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
Guy Benyei11169dd2012-12-18 14:30:41 +00002510 PREPROCESSOR_DETAIL_BLOCK_ID)) {
Chris Lattnere7b154b2013-01-19 21:39:22 +00002511 Error("malformed preprocessor detail record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002512 return Failure;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002513 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002514 F.PreprocessorDetailStartOffset
Chris Lattnere7b154b2013-01-19 21:39:22 +00002515 = F.PreprocessorDetailCursor.GetCurrentBitNo();
2516
Guy Benyei11169dd2012-12-18 14:30:41 +00002517 if (!PP.getPreprocessingRecord())
2518 PP.createPreprocessingRecord();
2519 if (!PP.getPreprocessingRecord()->getExternalSource())
2520 PP.getPreprocessingRecord()->SetExternalSource(*this);
2521 break;
2522
2523 case SOURCE_MANAGER_BLOCK_ID:
2524 if (ReadSourceManagerBlock(F))
Ben Langmuir2c9af442014-04-10 17:57:43 +00002525 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002526 break;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002527
Guy Benyei11169dd2012-12-18 14:30:41 +00002528 case SUBMODULE_BLOCK_ID:
Ben Langmuir2c9af442014-04-10 17:57:43 +00002529 if (ASTReadResult Result = ReadSubmoduleBlock(F, ClientLoadCapabilities))
2530 return Result;
Guy Benyei11169dd2012-12-18 14:30:41 +00002531 break;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002532
Guy Benyei11169dd2012-12-18 14:30:41 +00002533 case COMMENTS_BLOCK_ID: {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00002534 BitstreamCursor C = Stream;
Guy Benyei11169dd2012-12-18 14:30:41 +00002535 if (Stream.SkipBlock() ||
2536 ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
2537 Error("malformed comments block in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002538 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002539 }
2540 CommentsCursors.push_back(std::make_pair(C, &F));
2541 break;
2542 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00002543
Guy Benyei11169dd2012-12-18 14:30:41 +00002544 default:
Chris Lattnere7b154b2013-01-19 21:39:22 +00002545 if (Stream.SkipBlock()) {
2546 Error("malformed block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002547 return Failure;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002548 }
2549 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002550 }
2551 continue;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002552
2553 case llvm::BitstreamEntry::Record:
2554 // The interesting case.
2555 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002556 }
2557
2558 // Read and process a record.
2559 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00002560 StringRef Blob;
2561 switch ((ASTRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002562 default: // Default behavior: ignore.
2563 break;
2564
2565 case TYPE_OFFSET: {
2566 if (F.LocalNumTypes != 0) {
2567 Error("duplicate TYPE_OFFSET record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002568 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002569 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00002570 F.TypeOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002571 F.LocalNumTypes = Record[0];
2572 unsigned LocalBaseTypeIndex = Record[1];
2573 F.BaseTypeIndex = getTotalNumTypes();
2574
2575 if (F.LocalNumTypes > 0) {
2576 // Introduce the global -> local mapping for types within this module.
2577 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
2578
2579 // Introduce the local -> global mapping for types within this module.
2580 F.TypeRemap.insertOrReplace(
2581 std::make_pair(LocalBaseTypeIndex,
2582 F.BaseTypeIndex - LocalBaseTypeIndex));
Ben Langmuir52ca6782014-10-20 16:27:32 +00002583
2584 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
Guy Benyei11169dd2012-12-18 14:30:41 +00002585 }
2586 break;
2587 }
2588
2589 case DECL_OFFSET: {
2590 if (F.LocalNumDecls != 0) {
2591 Error("duplicate DECL_OFFSET record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002592 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002593 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00002594 F.DeclOffsets = (const DeclOffset *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002595 F.LocalNumDecls = Record[0];
2596 unsigned LocalBaseDeclID = Record[1];
2597 F.BaseDeclID = getTotalNumDecls();
2598
2599 if (F.LocalNumDecls > 0) {
2600 // Introduce the global -> local mapping for declarations within this
2601 // module.
2602 GlobalDeclMap.insert(
2603 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
2604
2605 // Introduce the local -> global mapping for declarations within this
2606 // module.
2607 F.DeclRemap.insertOrReplace(
2608 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
2609
2610 // Introduce the global -> local mapping for declarations within this
2611 // module.
2612 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
Ben Langmuirfe971d92014-08-16 04:54:18 +00002613
Ben Langmuir52ca6782014-10-20 16:27:32 +00002614 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
2615 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002616 break;
2617 }
2618
2619 case TU_UPDATE_LEXICAL: {
2620 DeclContext *TU = Context.getTranslationUnitDecl();
Richard Smith82f8fcd2015-08-06 22:07:25 +00002621 LexicalContents Contents(
2622 reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
2623 Blob.data()),
2624 static_cast<unsigned int>(Blob.size() / 4));
2625 TULexicalDecls.push_back(std::make_pair(&F, Contents));
Guy Benyei11169dd2012-12-18 14:30:41 +00002626 TU->setHasExternalLexicalStorage(true);
2627 break;
2628 }
2629
2630 case UPDATE_VISIBLE: {
2631 unsigned Idx = 0;
2632 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
Richard Smith0f4e2c42015-08-06 04:23:48 +00002633 auto *Data = (const unsigned char*)Blob.data();
Richard Smithd88a7f12015-09-01 20:35:42 +00002634 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data});
Richard Smith0f4e2c42015-08-06 04:23:48 +00002635 // If we've already loaded the decl, perform the updates when we finish
2636 // loading this block.
2637 if (Decl *D = GetExistingDecl(ID))
2638 PendingUpdateRecords.push_back(std::make_pair(ID, D));
Guy Benyei11169dd2012-12-18 14:30:41 +00002639 break;
2640 }
2641
2642 case IDENTIFIER_TABLE:
Chris Lattner0e6c9402013-01-20 02:38:54 +00002643 F.IdentifierTableData = Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002644 if (Record[0]) {
Justin Bognerda4e6502014-04-14 16:34:29 +00002645 F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
2646 (const unsigned char *)F.IdentifierTableData + Record[0],
2647 (const unsigned char *)F.IdentifierTableData + sizeof(uint32_t),
2648 (const unsigned char *)F.IdentifierTableData,
2649 ASTIdentifierLookupTrait(*this, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002650
2651 PP.getIdentifierTable().setExternalIdentifierLookup(this);
2652 }
2653 break;
2654
2655 case IDENTIFIER_OFFSET: {
2656 if (F.LocalNumIdentifiers != 0) {
2657 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002658 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002659 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00002660 F.IdentifierOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002661 F.LocalNumIdentifiers = Record[0];
2662 unsigned LocalBaseIdentifierID = Record[1];
2663 F.BaseIdentifierID = getTotalNumIdentifiers();
2664
2665 if (F.LocalNumIdentifiers > 0) {
2666 // Introduce the global -> local mapping for identifiers within this
2667 // module.
2668 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
2669 &F));
2670
2671 // Introduce the local -> global mapping for identifiers within this
2672 // module.
2673 F.IdentifierRemap.insertOrReplace(
2674 std::make_pair(LocalBaseIdentifierID,
2675 F.BaseIdentifierID - LocalBaseIdentifierID));
Ben Langmuirfe971d92014-08-16 04:54:18 +00002676
Ben Langmuir52ca6782014-10-20 16:27:32 +00002677 IdentifiersLoaded.resize(IdentifiersLoaded.size()
2678 + F.LocalNumIdentifiers);
2679 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002680 break;
2681 }
2682
Richard Smith33e0f7e2015-07-22 02:08:40 +00002683 case INTERESTING_IDENTIFIERS:
2684 F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end());
2685 break;
2686
Ben Langmuir332aafe2014-01-31 01:06:56 +00002687 case EAGERLY_DESERIALIZED_DECLS:
Richard Smith9e2341d2015-03-23 03:25:59 +00002688 // FIXME: Skip reading this record if our ASTConsumer doesn't care
2689 // about "interesting" decls (for instance, if we're building a module).
Guy Benyei11169dd2012-12-18 14:30:41 +00002690 for (unsigned I = 0, N = Record.size(); I != N; ++I)
Ben Langmuir332aafe2014-01-31 01:06:56 +00002691 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
Guy Benyei11169dd2012-12-18 14:30:41 +00002692 break;
2693
2694 case SPECIAL_TYPES:
Douglas Gregor44180f82013-02-01 23:45:03 +00002695 if (SpecialTypes.empty()) {
2696 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2697 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
2698 break;
2699 }
2700
2701 if (SpecialTypes.size() != Record.size()) {
2702 Error("invalid special-types record");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002703 return Failure;
Douglas Gregor44180f82013-02-01 23:45:03 +00002704 }
2705
2706 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2707 serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
2708 if (!SpecialTypes[I])
2709 SpecialTypes[I] = ID;
2710 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
2711 // merge step?
2712 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002713 break;
2714
2715 case STATISTICS:
2716 TotalNumStatements += Record[0];
2717 TotalNumMacros += Record[1];
2718 TotalLexicalDeclContexts += Record[2];
2719 TotalVisibleDeclContexts += Record[3];
2720 break;
2721
2722 case UNUSED_FILESCOPED_DECLS:
2723 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2724 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
2725 break;
2726
2727 case DELEGATING_CTORS:
2728 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2729 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
2730 break;
2731
2732 case WEAK_UNDECLARED_IDENTIFIERS:
2733 if (Record.size() % 4 != 0) {
2734 Error("invalid weak identifiers record");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002735 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002736 }
2737
2738 // FIXME: Ignore weak undeclared identifiers from non-original PCH
2739 // files. This isn't the way to do it :)
2740 WeakUndeclaredIdentifiers.clear();
2741
2742 // Translate the weak, undeclared identifiers into global IDs.
2743 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
2744 WeakUndeclaredIdentifiers.push_back(
2745 getGlobalIdentifierID(F, Record[I++]));
2746 WeakUndeclaredIdentifiers.push_back(
2747 getGlobalIdentifierID(F, Record[I++]));
2748 WeakUndeclaredIdentifiers.push_back(
2749 ReadSourceLocation(F, Record, I).getRawEncoding());
2750 WeakUndeclaredIdentifiers.push_back(Record[I++]);
2751 }
2752 break;
2753
Guy Benyei11169dd2012-12-18 14:30:41 +00002754 case SELECTOR_OFFSETS: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00002755 F.SelectorOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002756 F.LocalNumSelectors = Record[0];
2757 unsigned LocalBaseSelectorID = Record[1];
2758 F.BaseSelectorID = getTotalNumSelectors();
2759
2760 if (F.LocalNumSelectors > 0) {
2761 // Introduce the global -> local mapping for selectors within this
2762 // module.
2763 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
2764
2765 // Introduce the local -> global mapping for selectors within this
2766 // module.
2767 F.SelectorRemap.insertOrReplace(
2768 std::make_pair(LocalBaseSelectorID,
2769 F.BaseSelectorID - LocalBaseSelectorID));
Ben Langmuir52ca6782014-10-20 16:27:32 +00002770
2771 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
Guy Benyei11169dd2012-12-18 14:30:41 +00002772 }
2773 break;
2774 }
2775
2776 case METHOD_POOL:
Chris Lattner0e6c9402013-01-20 02:38:54 +00002777 F.SelectorLookupTableData = (const unsigned char *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002778 if (Record[0])
2779 F.SelectorLookupTable
2780 = ASTSelectorLookupTable::Create(
2781 F.SelectorLookupTableData + Record[0],
2782 F.SelectorLookupTableData,
2783 ASTSelectorLookupTrait(*this, F));
2784 TotalNumMethodPoolEntries += Record[1];
2785 break;
2786
2787 case REFERENCED_SELECTOR_POOL:
2788 if (!Record.empty()) {
2789 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
2790 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
2791 Record[Idx++]));
2792 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
2793 getRawEncoding());
2794 }
2795 }
2796 break;
2797
2798 case PP_COUNTER_VALUE:
2799 if (!Record.empty() && Listener)
2800 Listener->ReadCounter(F, Record[0]);
2801 break;
2802
2803 case FILE_SORTED_DECLS:
Chris Lattner0e6c9402013-01-20 02:38:54 +00002804 F.FileSortedDecls = (const DeclID *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002805 F.NumFileSortedDecls = Record[0];
2806 break;
2807
2808 case SOURCE_LOCATION_OFFSETS: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00002809 F.SLocEntryOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002810 F.LocalNumSLocEntries = Record[0];
2811 unsigned SLocSpaceSize = Record[1];
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00002812 std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
Ben Langmuir52ca6782014-10-20 16:27:32 +00002813 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
Guy Benyei11169dd2012-12-18 14:30:41 +00002814 SLocSpaceSize);
Richard Smith78d81ec2015-08-12 22:25:24 +00002815 if (!F.SLocEntryBaseID) {
2816 Error("ran out of source locations");
2817 break;
2818 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002819 // Make our entry in the range map. BaseID is negative and growing, so
2820 // we invert it. Because we invert it, though, we need the other end of
2821 // the range.
2822 unsigned RangeStart =
2823 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
2824 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
2825 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
2826
2827 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
2828 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
2829 GlobalSLocOffsetMap.insert(
2830 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
2831 - SLocSpaceSize,&F));
2832
2833 // Initialize the remapping table.
2834 // Invalid stays invalid.
Richard Smithb9eab6d2014-03-20 19:44:17 +00002835 F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
Guy Benyei11169dd2012-12-18 14:30:41 +00002836 // This module. Base was 2 when being compiled.
Richard Smithb9eab6d2014-03-20 19:44:17 +00002837 F.SLocRemap.insertOrReplace(std::make_pair(2U,
Guy Benyei11169dd2012-12-18 14:30:41 +00002838 static_cast<int>(F.SLocEntryBaseOffset - 2)));
2839
2840 TotalNumSLocEntries += F.LocalNumSLocEntries;
2841 break;
2842 }
2843
2844 case MODULE_OFFSET_MAP: {
2845 // Additional remapping information.
Chris Lattner0e6c9402013-01-20 02:38:54 +00002846 const unsigned char *Data = (const unsigned char*)Blob.data();
2847 const unsigned char *DataEnd = Data + Blob.size();
Richard Smithb9eab6d2014-03-20 19:44:17 +00002848
2849 // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
2850 if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
2851 F.SLocRemap.insert(std::make_pair(0U, 0));
2852 F.SLocRemap.insert(std::make_pair(2U, 1));
2853 }
2854
Guy Benyei11169dd2012-12-18 14:30:41 +00002855 // Continuous range maps we may be updating in our module.
Ben Langmuir785180e2014-10-20 16:27:30 +00002856 typedef ContinuousRangeMap<uint32_t, int, 2>::Builder
2857 RemapBuilder;
2858 RemapBuilder SLocRemap(F.SLocRemap);
2859 RemapBuilder IdentifierRemap(F.IdentifierRemap);
2860 RemapBuilder MacroRemap(F.MacroRemap);
2861 RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
2862 RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
2863 RemapBuilder SelectorRemap(F.SelectorRemap);
2864 RemapBuilder DeclRemap(F.DeclRemap);
2865 RemapBuilder TypeRemap(F.TypeRemap);
Guy Benyei11169dd2012-12-18 14:30:41 +00002866
Richard Smithd8879c82015-08-24 21:59:32 +00002867 while (Data < DataEnd) {
Justin Bogner57ba0b22014-03-28 22:03:24 +00002868 using namespace llvm::support;
2869 uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data);
Guy Benyei11169dd2012-12-18 14:30:41 +00002870 StringRef Name = StringRef((const char*)Data, Len);
2871 Data += Len;
2872 ModuleFile *OM = ModuleMgr.lookup(Name);
2873 if (!OM) {
2874 Error("SourceLocation remap refers to unknown module");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002875 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002876 }
2877
Justin Bogner57ba0b22014-03-28 22:03:24 +00002878 uint32_t SLocOffset =
2879 endian::readNext<uint32_t, little, unaligned>(Data);
2880 uint32_t IdentifierIDOffset =
2881 endian::readNext<uint32_t, little, unaligned>(Data);
2882 uint32_t MacroIDOffset =
2883 endian::readNext<uint32_t, little, unaligned>(Data);
2884 uint32_t PreprocessedEntityIDOffset =
2885 endian::readNext<uint32_t, little, unaligned>(Data);
2886 uint32_t SubmoduleIDOffset =
2887 endian::readNext<uint32_t, little, unaligned>(Data);
2888 uint32_t SelectorIDOffset =
2889 endian::readNext<uint32_t, little, unaligned>(Data);
2890 uint32_t DeclIDOffset =
2891 endian::readNext<uint32_t, little, unaligned>(Data);
2892 uint32_t TypeIndexOffset =
2893 endian::readNext<uint32_t, little, unaligned>(Data);
2894
Ben Langmuir785180e2014-10-20 16:27:30 +00002895 uint32_t None = std::numeric_limits<uint32_t>::max();
2896
2897 auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
2898 RemapBuilder &Remap) {
2899 if (Offset != None)
2900 Remap.insert(std::make_pair(Offset,
2901 static_cast<int>(BaseOffset - Offset)));
2902 };
2903 mapOffset(SLocOffset, OM->SLocEntryBaseOffset, SLocRemap);
2904 mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap);
2905 mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
2906 mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
2907 PreprocessedEntityRemap);
2908 mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
2909 mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
2910 mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap);
2911 mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap);
Guy Benyei11169dd2012-12-18 14:30:41 +00002912
2913 // Global -> local mappings.
2914 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
2915 }
2916 break;
2917 }
2918
2919 case SOURCE_MANAGER_LINE_TABLE:
2920 if (ParseLineTable(F, Record))
Ben Langmuir2c9af442014-04-10 17:57:43 +00002921 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002922 break;
2923
2924 case SOURCE_LOCATION_PRELOADS: {
2925 // Need to transform from the local view (1-based IDs) to the global view,
2926 // which is based off F.SLocEntryBaseID.
2927 if (!F.PreloadSLocEntries.empty()) {
2928 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002929 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002930 }
2931
2932 F.PreloadSLocEntries.swap(Record);
2933 break;
2934 }
2935
2936 case EXT_VECTOR_DECLS:
2937 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2938 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
2939 break;
2940
2941 case VTABLE_USES:
2942 if (Record.size() % 3 != 0) {
2943 Error("Invalid VTABLE_USES record");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002944 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002945 }
2946
2947 // Later tables overwrite earlier ones.
2948 // FIXME: Modules will have some trouble with this. This is clearly not
2949 // the right way to do this.
2950 VTableUses.clear();
2951
2952 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
2953 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
2954 VTableUses.push_back(
2955 ReadSourceLocation(F, Record, Idx).getRawEncoding());
2956 VTableUses.push_back(Record[Idx++]);
2957 }
2958 break;
2959
Guy Benyei11169dd2012-12-18 14:30:41 +00002960 case PENDING_IMPLICIT_INSTANTIATIONS:
2961 if (PendingInstantiations.size() % 2 != 0) {
2962 Error("Invalid existing PendingInstantiations");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002963 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002964 }
2965
2966 if (Record.size() % 2 != 0) {
2967 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002968 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002969 }
2970
2971 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2972 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
2973 PendingInstantiations.push_back(
2974 ReadSourceLocation(F, Record, I).getRawEncoding());
2975 }
2976 break;
2977
2978 case SEMA_DECL_REFS:
Richard Smith3d8e97e2013-10-18 06:54:39 +00002979 if (Record.size() != 2) {
2980 Error("Invalid SEMA_DECL_REFS block");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002981 return Failure;
Richard Smith3d8e97e2013-10-18 06:54:39 +00002982 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002983 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2984 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
2985 break;
2986
2987 case PPD_ENTITIES_OFFSETS: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00002988 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
2989 assert(Blob.size() % sizeof(PPEntityOffset) == 0);
2990 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
Guy Benyei11169dd2012-12-18 14:30:41 +00002991
2992 unsigned LocalBasePreprocessedEntityID = Record[0];
2993
2994 unsigned StartingID;
2995 if (!PP.getPreprocessingRecord())
2996 PP.createPreprocessingRecord();
2997 if (!PP.getPreprocessingRecord()->getExternalSource())
2998 PP.getPreprocessingRecord()->SetExternalSource(*this);
2999 StartingID
3000 = PP.getPreprocessingRecord()
Ben Langmuir52ca6782014-10-20 16:27:32 +00003001 ->allocateLoadedEntities(F.NumPreprocessedEntities);
Guy Benyei11169dd2012-12-18 14:30:41 +00003002 F.BasePreprocessedEntityID = StartingID;
3003
3004 if (F.NumPreprocessedEntities > 0) {
3005 // Introduce the global -> local mapping for preprocessed entities in
3006 // this module.
3007 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
3008
3009 // Introduce the local -> global mapping for preprocessed entities in
3010 // this module.
3011 F.PreprocessedEntityRemap.insertOrReplace(
3012 std::make_pair(LocalBasePreprocessedEntityID,
3013 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
3014 }
3015
3016 break;
3017 }
3018
3019 case DECL_UPDATE_OFFSETS: {
3020 if (Record.size() % 2 != 0) {
3021 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003022 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003023 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00003024 for (unsigned I = 0, N = Record.size(); I != N; I += 2) {
3025 GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
3026 DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1]));
3027
3028 // If we've already loaded the decl, perform the updates when we finish
3029 // loading this block.
3030 if (Decl *D = GetExistingDecl(ID))
3031 PendingUpdateRecords.push_back(std::make_pair(ID, D));
3032 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003033 break;
3034 }
3035
3036 case DECL_REPLACEMENTS: {
3037 if (Record.size() % 3 != 0) {
3038 Error("invalid DECL_REPLACEMENTS block in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003039 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003040 }
3041 for (unsigned I = 0, N = Record.size(); I != N; I += 3)
3042 ReplacedDecls[getGlobalDeclID(F, Record[I])]
3043 = ReplacedDeclInfo(&F, Record[I+1], Record[I+2]);
3044 break;
3045 }
3046
3047 case OBJC_CATEGORIES_MAP: {
3048 if (F.LocalNumObjCCategoriesInMap != 0) {
3049 Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003050 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003051 }
3052
3053 F.LocalNumObjCCategoriesInMap = Record[0];
Chris Lattner0e6c9402013-01-20 02:38:54 +00003054 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003055 break;
3056 }
3057
3058 case OBJC_CATEGORIES:
3059 F.ObjCCategories.swap(Record);
3060 break;
Richard Smithc2bb8182015-03-24 06:36:48 +00003061
Guy Benyei11169dd2012-12-18 14:30:41 +00003062 case CXX_BASE_SPECIFIER_OFFSETS: {
3063 if (F.LocalNumCXXBaseSpecifiers != 0) {
3064 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003065 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003066 }
Richard Smithc2bb8182015-03-24 06:36:48 +00003067
Guy Benyei11169dd2012-12-18 14:30:41 +00003068 F.LocalNumCXXBaseSpecifiers = Record[0];
Chris Lattner0e6c9402013-01-20 02:38:54 +00003069 F.CXXBaseSpecifiersOffsets = (const uint32_t *)Blob.data();
Richard Smithc2bb8182015-03-24 06:36:48 +00003070 break;
3071 }
3072
3073 case CXX_CTOR_INITIALIZERS_OFFSETS: {
3074 if (F.LocalNumCXXCtorInitializers != 0) {
3075 Error("duplicate CXX_CTOR_INITIALIZERS_OFFSETS record in AST file");
3076 return Failure;
3077 }
3078
3079 F.LocalNumCXXCtorInitializers = Record[0];
3080 F.CXXCtorInitializersOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003081 break;
3082 }
3083
3084 case DIAG_PRAGMA_MAPPINGS:
3085 if (F.PragmaDiagMappings.empty())
3086 F.PragmaDiagMappings.swap(Record);
3087 else
3088 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
3089 Record.begin(), Record.end());
3090 break;
3091
3092 case CUDA_SPECIAL_DECL_REFS:
3093 // Later tables overwrite earlier ones.
3094 // FIXME: Modules will have trouble with this.
3095 CUDASpecialDeclRefs.clear();
3096 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3097 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3098 break;
3099
3100 case HEADER_SEARCH_TABLE: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00003101 F.HeaderFileInfoTableData = Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003102 F.LocalNumHeaderFileInfos = Record[1];
Guy Benyei11169dd2012-12-18 14:30:41 +00003103 if (Record[0]) {
3104 F.HeaderFileInfoTable
3105 = HeaderFileInfoLookupTable::Create(
3106 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
3107 (const unsigned char *)F.HeaderFileInfoTableData,
3108 HeaderFileInfoTrait(*this, F,
3109 &PP.getHeaderSearchInfo(),
Chris Lattner0e6c9402013-01-20 02:38:54 +00003110 Blob.data() + Record[2]));
Guy Benyei11169dd2012-12-18 14:30:41 +00003111
3112 PP.getHeaderSearchInfo().SetExternalSource(this);
3113 if (!PP.getHeaderSearchInfo().getExternalLookup())
3114 PP.getHeaderSearchInfo().SetExternalLookup(this);
3115 }
3116 break;
3117 }
3118
3119 case FP_PRAGMA_OPTIONS:
3120 // Later tables overwrite earlier ones.
3121 FPPragmaOptions.swap(Record);
3122 break;
3123
3124 case OPENCL_EXTENSIONS:
3125 // Later tables overwrite earlier ones.
3126 OpenCLExtensions.swap(Record);
3127 break;
3128
3129 case TENTATIVE_DEFINITIONS:
3130 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3131 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
3132 break;
3133
3134 case KNOWN_NAMESPACES:
3135 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3136 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
3137 break;
Nick Lewycky8334af82013-01-26 00:35:08 +00003138
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00003139 case UNDEFINED_BUT_USED:
3140 if (UndefinedButUsed.size() % 2 != 0) {
3141 Error("Invalid existing UndefinedButUsed");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003142 return Failure;
Nick Lewycky8334af82013-01-26 00:35:08 +00003143 }
3144
3145 if (Record.size() % 2 != 0) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00003146 Error("invalid undefined-but-used record");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003147 return Failure;
Nick Lewycky8334af82013-01-26 00:35:08 +00003148 }
3149 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00003150 UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
3151 UndefinedButUsed.push_back(
Nick Lewycky8334af82013-01-26 00:35:08 +00003152 ReadSourceLocation(F, Record, I).getRawEncoding());
3153 }
3154 break;
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00003155 case DELETE_EXPRS_TO_ANALYZE:
3156 for (unsigned I = 0, N = Record.size(); I != N;) {
3157 DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++]));
3158 const uint64_t Count = Record[I++];
3159 DelayedDeleteExprs.push_back(Count);
3160 for (uint64_t C = 0; C < Count; ++C) {
3161 DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding());
3162 bool IsArrayForm = Record[I++] == 1;
3163 DelayedDeleteExprs.push_back(IsArrayForm);
3164 }
3165 }
3166 break;
Nick Lewycky8334af82013-01-26 00:35:08 +00003167
Guy Benyei11169dd2012-12-18 14:30:41 +00003168 case IMPORTED_MODULES: {
Richard Smithe842a472014-10-22 02:05:46 +00003169 if (F.Kind != MK_ImplicitModule && F.Kind != MK_ExplicitModule) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003170 // If we aren't loading a module (which has its own exports), make
3171 // all of the imported modules visible.
3172 // FIXME: Deal with macros-only imports.
Richard Smith56be7542014-03-21 00:33:59 +00003173 for (unsigned I = 0, N = Record.size(); I != N; /**/) {
3174 unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
3175 SourceLocation Loc = ReadSourceLocation(F, Record, I);
3176 if (GlobalID)
Aaron Ballman4f45b712014-03-21 15:22:56 +00003177 ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
Guy Benyei11169dd2012-12-18 14:30:41 +00003178 }
3179 }
3180 break;
3181 }
3182
Guy Benyei11169dd2012-12-18 14:30:41 +00003183 case MACRO_OFFSET: {
3184 if (F.LocalNumMacros != 0) {
3185 Error("duplicate MACRO_OFFSET record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003186 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003187 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00003188 F.MacroOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003189 F.LocalNumMacros = Record[0];
3190 unsigned LocalBaseMacroID = Record[1];
3191 F.BaseMacroID = getTotalNumMacros();
3192
3193 if (F.LocalNumMacros > 0) {
3194 // Introduce the global -> local mapping for macros within this module.
3195 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3196
3197 // Introduce the local -> global mapping for macros within this module.
3198 F.MacroRemap.insertOrReplace(
3199 std::make_pair(LocalBaseMacroID,
3200 F.BaseMacroID - LocalBaseMacroID));
Ben Langmuir52ca6782014-10-20 16:27:32 +00003201
3202 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
Guy Benyei11169dd2012-12-18 14:30:41 +00003203 }
3204 break;
3205 }
3206
Richard Smithe40f2ba2013-08-07 21:41:30 +00003207 case LATE_PARSED_TEMPLATE: {
3208 LateParsedTemplates.append(Record.begin(), Record.end());
3209 break;
3210 }
Dario Domizioli13a0a382014-05-23 12:13:25 +00003211
3212 case OPTIMIZE_PRAGMA_OPTIONS:
3213 if (Record.size() != 1) {
3214 Error("invalid pragma optimize record");
3215 return Failure;
3216 }
3217 OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
3218 break;
Nico Weber72889432014-09-06 01:25:55 +00003219
3220 case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
3221 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3222 UnusedLocalTypedefNameCandidates.push_back(
3223 getGlobalDeclID(F, Record[I]));
3224 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003225 }
3226 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003227}
3228
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003229ASTReader::ASTReadResult
3230ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
3231 const ModuleFile *ImportedBy,
3232 unsigned ClientLoadCapabilities) {
3233 unsigned Idx = 0;
Richard Smith7ed1bc92014-12-05 22:42:13 +00003234 F.ModuleMapPath = ReadPath(F, Record, Idx);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003235
Richard Smithe842a472014-10-22 02:05:46 +00003236 if (F.Kind == MK_ExplicitModule) {
3237 // For an explicitly-loaded module, we don't care whether the original
3238 // module map file exists or matches.
3239 return Success;
3240 }
3241
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003242 // Try to resolve ModuleName in the current header search context and
3243 // verify that it is found in the same module map file as we saved. If the
3244 // top-level AST file is a main file, skip this check because there is no
3245 // usable header search context.
3246 assert(!F.ModuleName.empty() &&
Richard Smithe842a472014-10-22 02:05:46 +00003247 "MODULE_NAME should come before MODULE_MAP_FILE");
3248 if (F.Kind == MK_ImplicitModule &&
3249 (*ModuleMgr.begin())->Kind != MK_MainFile) {
3250 // An implicitly-loaded module file should have its module listed in some
3251 // module map file that we've already loaded.
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003252 Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
Richard Smithe842a472014-10-22 02:05:46 +00003253 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
3254 const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr;
3255 if (!ModMap) {
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003256 assert(ImportedBy && "top-level import should be verified");
Richard Smith0f99d6a2015-08-09 08:48:41 +00003257 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) {
3258 if (auto *ASTFE = M ? M->getASTFile() : nullptr)
3259 // This module was defined by an imported (explicit) module.
3260 Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName
3261 << ASTFE->getName();
3262 else
3263 // This module was built with a different module map.
3264 Diag(diag::err_imported_module_not_found)
3265 << F.ModuleName << F.FileName << ImportedBy->FileName
3266 << F.ModuleMapPath;
3267 }
3268 return OutOfDate;
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003269 }
3270
Richard Smithe842a472014-10-22 02:05:46 +00003271 assert(M->Name == F.ModuleName && "found module with different name");
3272
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003273 // Check the primary module map file.
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003274 const FileEntry *StoredModMap = FileMgr.getFile(F.ModuleMapPath);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003275 if (StoredModMap == nullptr || StoredModMap != ModMap) {
3276 assert(ModMap && "found module is missing module map file");
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003277 assert(ImportedBy && "top-level import should be verified");
3278 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3279 Diag(diag::err_imported_module_modmap_changed)
3280 << F.ModuleName << ImportedBy->FileName
3281 << ModMap->getName() << F.ModuleMapPath;
3282 return OutOfDate;
3283 }
3284
3285 llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps;
3286 for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
3287 // FIXME: we should use input files rather than storing names.
Richard Smith7ed1bc92014-12-05 22:42:13 +00003288 std::string Filename = ReadPath(F, Record, Idx);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003289 const FileEntry *F =
3290 FileMgr.getFile(Filename, false, false);
3291 if (F == nullptr) {
3292 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3293 Error("could not find file '" + Filename +"' referenced by AST file");
3294 return OutOfDate;
3295 }
3296 AdditionalStoredMaps.insert(F);
3297 }
3298
3299 // Check any additional module map files (e.g. module.private.modulemap)
3300 // that are not in the pcm.
3301 if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
3302 for (const FileEntry *ModMap : *AdditionalModuleMaps) {
3303 // Remove files that match
3304 // Note: SmallPtrSet::erase is really remove
3305 if (!AdditionalStoredMaps.erase(ModMap)) {
3306 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3307 Diag(diag::err_module_different_modmap)
3308 << F.ModuleName << /*new*/0 << ModMap->getName();
3309 return OutOfDate;
3310 }
3311 }
3312 }
3313
3314 // Check any additional module map files that are in the pcm, but not
3315 // found in header search. Cases that match are already removed.
3316 for (const FileEntry *ModMap : AdditionalStoredMaps) {
3317 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3318 Diag(diag::err_module_different_modmap)
3319 << F.ModuleName << /*not new*/1 << ModMap->getName();
3320 return OutOfDate;
3321 }
3322 }
3323
3324 if (Listener)
3325 Listener->ReadModuleMapFile(F.ModuleMapPath);
3326 return Success;
3327}
3328
3329
Douglas Gregorc1489562013-02-12 23:36:21 +00003330/// \brief Move the given method to the back of the global list of methods.
3331static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
3332 // Find the entry for this selector in the method pool.
3333 Sema::GlobalMethodPool::iterator Known
3334 = S.MethodPool.find(Method->getSelector());
3335 if (Known == S.MethodPool.end())
3336 return;
3337
3338 // Retrieve the appropriate method list.
3339 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
3340 : Known->second.second;
3341 bool Found = false;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003342 for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
Douglas Gregorc1489562013-02-12 23:36:21 +00003343 if (!Found) {
Nico Weber2e0c8f72014-12-27 03:58:08 +00003344 if (List->getMethod() == Method) {
Douglas Gregorc1489562013-02-12 23:36:21 +00003345 Found = true;
3346 } else {
3347 // Keep searching.
3348 continue;
3349 }
3350 }
3351
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003352 if (List->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003353 List->setMethod(List->getNext()->getMethod());
Douglas Gregorc1489562013-02-12 23:36:21 +00003354 else
Nico Weber2e0c8f72014-12-27 03:58:08 +00003355 List->setMethod(Method);
Douglas Gregorc1489562013-02-12 23:36:21 +00003356 }
3357}
3358
Richard Smithde711422015-04-23 21:20:19 +00003359void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
Richard Smith10434f32015-05-02 02:08:26 +00003360 assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
Richard Smith20e883e2015-04-29 23:20:19 +00003361 for (Decl *D : Names) {
Richard Smith49f906a2014-03-01 00:08:04 +00003362 bool wasHidden = D->Hidden;
3363 D->Hidden = false;
Guy Benyei11169dd2012-12-18 14:30:41 +00003364
Richard Smith49f906a2014-03-01 00:08:04 +00003365 if (wasHidden && SemaObj) {
3366 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
3367 moveMethodToBackOfGlobalList(*SemaObj, Method);
Douglas Gregorc1489562013-02-12 23:36:21 +00003368 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003369 }
3370 }
3371}
3372
Richard Smith49f906a2014-03-01 00:08:04 +00003373void ASTReader::makeModuleVisible(Module *Mod,
Argyrios Kyrtzidis125df052013-02-01 16:36:12 +00003374 Module::NameVisibilityKind NameVisibility,
Richard Smitha7e2cc62015-05-01 01:53:09 +00003375 SourceLocation ImportLoc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003376 llvm::SmallPtrSet<Module *, 4> Visited;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003377 SmallVector<Module *, 4> Stack;
Robert Wilhelm25284cc2013-08-23 16:11:15 +00003378 Stack.push_back(Mod);
Guy Benyei11169dd2012-12-18 14:30:41 +00003379 while (!Stack.empty()) {
Robert Wilhelm25284cc2013-08-23 16:11:15 +00003380 Mod = Stack.pop_back_val();
Guy Benyei11169dd2012-12-18 14:30:41 +00003381
3382 if (NameVisibility <= Mod->NameVisibility) {
Robert Wilhelm25284cc2013-08-23 16:11:15 +00003383 // This module already has this level of visibility (or greater), so
Guy Benyei11169dd2012-12-18 14:30:41 +00003384 // there is nothing more to do.
3385 continue;
3386 }
Richard Smith49f906a2014-03-01 00:08:04 +00003387
Guy Benyei11169dd2012-12-18 14:30:41 +00003388 if (!Mod->isAvailable()) {
3389 // Modules that aren't available cannot be made visible.
3390 continue;
3391 }
3392
3393 // Update the module's name visibility.
3394 Mod->NameVisibility = NameVisibility;
Richard Smith49f906a2014-03-01 00:08:04 +00003395
Guy Benyei11169dd2012-12-18 14:30:41 +00003396 // If we've already deserialized any names from this module,
3397 // mark them as visible.
3398 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
3399 if (Hidden != HiddenNamesMap.end()) {
Richard Smith57721ac2014-07-21 04:10:40 +00003400 auto HiddenNames = std::move(*Hidden);
Guy Benyei11169dd2012-12-18 14:30:41 +00003401 HiddenNamesMap.erase(Hidden);
Richard Smithde711422015-04-23 21:20:19 +00003402 makeNamesVisible(HiddenNames.second, HiddenNames.first);
Richard Smith57721ac2014-07-21 04:10:40 +00003403 assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() &&
3404 "making names visible added hidden names");
Guy Benyei11169dd2012-12-18 14:30:41 +00003405 }
Dmitri Gribenkoe9bcf5b2013-11-04 21:51:33 +00003406
Guy Benyei11169dd2012-12-18 14:30:41 +00003407 // Push any exported modules onto the stack to be marked as visible.
Argyrios Kyrtzidis8739f7b2013-02-19 19:34:40 +00003408 SmallVector<Module *, 16> Exports;
3409 Mod->getExportedModules(Exports);
3410 for (SmallVectorImpl<Module *>::iterator
3411 I = Exports.begin(), E = Exports.end(); I != E; ++I) {
3412 Module *Exported = *I;
David Blaikie82e95a32014-11-19 07:49:47 +00003413 if (Visited.insert(Exported).second)
Argyrios Kyrtzidis8739f7b2013-02-19 19:34:40 +00003414 Stack.push_back(Exported);
Guy Benyei11169dd2012-12-18 14:30:41 +00003415 }
3416 }
3417}
3418
Douglas Gregore060e572013-01-25 01:03:03 +00003419bool ASTReader::loadGlobalIndex() {
3420 if (GlobalIndex)
3421 return false;
3422
3423 if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
3424 !Context.getLangOpts().Modules)
3425 return true;
3426
3427 // Try to load the global index.
3428 TriedLoadingGlobalIndex = true;
3429 StringRef ModuleCachePath
3430 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
3431 std::pair<GlobalModuleIndex *, GlobalModuleIndex::ErrorCode> Result
Douglas Gregor7029ce12013-03-19 00:28:20 +00003432 = GlobalModuleIndex::readIndex(ModuleCachePath);
Douglas Gregore060e572013-01-25 01:03:03 +00003433 if (!Result.first)
3434 return true;
3435
3436 GlobalIndex.reset(Result.first);
Douglas Gregor7211ac12013-01-25 23:32:03 +00003437 ModuleMgr.setGlobalIndex(GlobalIndex.get());
Douglas Gregore060e572013-01-25 01:03:03 +00003438 return false;
3439}
3440
3441bool ASTReader::isGlobalIndexUnavailable() const {
3442 return Context.getLangOpts().Modules && UseGlobalIndex &&
3443 !hasGlobalIndex() && TriedLoadingGlobalIndex;
3444}
3445
Dmitri Gribenkof430da42014-02-12 10:33:14 +00003446static void updateModuleTimestamp(ModuleFile &MF) {
3447 // Overwrite the timestamp file contents so that file's mtime changes.
3448 std::string TimestampFilename = MF.getTimestampFilename();
Rafael Espindoladae941a2014-08-25 18:17:04 +00003449 std::error_code EC;
3450 llvm::raw_fd_ostream OS(TimestampFilename, EC, llvm::sys::fs::F_Text);
3451 if (EC)
Dmitri Gribenkof430da42014-02-12 10:33:14 +00003452 return;
3453 OS << "Timestamp file\n";
3454}
3455
Douglas Gregor6623e1f2015-11-03 18:33:07 +00003456/// \brief Given a cursor at the start of an AST file, scan ahead and drop the
3457/// cursor into the start of the given block ID, returning false on success and
3458/// true on failure.
3459static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
3460 while (1) {
3461 llvm::BitstreamEntry Entry = Cursor.advance();
3462 switch (Entry.Kind) {
3463 case llvm::BitstreamEntry::Error:
3464 case llvm::BitstreamEntry::EndBlock:
3465 return true;
3466
3467 case llvm::BitstreamEntry::Record:
3468 // Ignore top-level records.
3469 Cursor.skipRecord(Entry.ID);
3470 break;
3471
3472 case llvm::BitstreamEntry::SubBlock:
3473 if (Entry.ID == BlockID) {
3474 if (Cursor.EnterSubBlock(BlockID))
3475 return true;
3476 // Found it!
3477 return false;
3478 }
3479
3480 if (Cursor.SkipBlock())
3481 return true;
3482 }
3483 }
3484}
3485
Guy Benyei11169dd2012-12-18 14:30:41 +00003486ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
3487 ModuleKind Type,
3488 SourceLocation ImportLoc,
3489 unsigned ClientLoadCapabilities) {
Argyrios Kyrtzidisdc9fdaf2013-05-24 05:44:08 +00003490 llvm::SaveAndRestore<SourceLocation>
3491 SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
3492
Richard Smithd1c46742014-04-30 02:24:17 +00003493 // Defer any pending actions until we get to the end of reading the AST file.
3494 Deserializing AnASTFile(this);
3495
Guy Benyei11169dd2012-12-18 14:30:41 +00003496 // Bump the generation number.
Richard Smith053f6c62014-05-16 23:01:30 +00003497 unsigned PreviousGeneration = incrementGeneration(Context);
Guy Benyei11169dd2012-12-18 14:30:41 +00003498
3499 unsigned NumModules = ModuleMgr.size();
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003500 SmallVector<ImportedModule, 4> Loaded;
Guy Benyei11169dd2012-12-18 14:30:41 +00003501 switch(ASTReadResult ReadResult = ReadASTCore(FileName, Type, ImportLoc,
Craig Toppera13603a2014-05-22 05:54:18 +00003502 /*ImportedBy=*/nullptr, Loaded,
Ben Langmuir487ea142014-10-23 18:05:36 +00003503 0, 0, 0,
Guy Benyei11169dd2012-12-18 14:30:41 +00003504 ClientLoadCapabilities)) {
3505 case Failure:
Douglas Gregor7029ce12013-03-19 00:28:20 +00003506 case Missing:
Guy Benyei11169dd2012-12-18 14:30:41 +00003507 case OutOfDate:
3508 case VersionMismatch:
3509 case ConfigurationMismatch:
Ben Langmuir9801b252014-06-20 00:24:56 +00003510 case HadErrors: {
3511 llvm::SmallPtrSet<ModuleFile *, 4> LoadedSet;
3512 for (const ImportedModule &IM : Loaded)
3513 LoadedSet.insert(IM.Mod);
3514
Douglas Gregor7029ce12013-03-19 00:28:20 +00003515 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, ModuleMgr.end(),
Ben Langmuir9801b252014-06-20 00:24:56 +00003516 LoadedSet,
Douglas Gregor7029ce12013-03-19 00:28:20 +00003517 Context.getLangOpts().Modules
3518 ? &PP.getHeaderSearchInfo().getModuleMap()
Craig Toppera13603a2014-05-22 05:54:18 +00003519 : nullptr);
Douglas Gregore060e572013-01-25 01:03:03 +00003520
3521 // If we find that any modules are unusable, the global index is going
3522 // to be out-of-date. Just remove it.
3523 GlobalIndex.reset();
Craig Toppera13603a2014-05-22 05:54:18 +00003524 ModuleMgr.setGlobalIndex(nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00003525 return ReadResult;
Ben Langmuir9801b252014-06-20 00:24:56 +00003526 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003527 case Success:
3528 break;
3529 }
3530
3531 // Here comes stuff that we only do once the entire chain is loaded.
3532
3533 // Load the AST blocks of all of the modules that we loaded.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003534 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
3535 MEnd = Loaded.end();
Guy Benyei11169dd2012-12-18 14:30:41 +00003536 M != MEnd; ++M) {
3537 ModuleFile &F = *M->Mod;
3538
3539 // Read the AST block.
Ben Langmuir2c9af442014-04-10 17:57:43 +00003540 if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities))
3541 return Result;
Guy Benyei11169dd2012-12-18 14:30:41 +00003542
Douglas Gregor6623e1f2015-11-03 18:33:07 +00003543 // Read the extension blocks.
3544 while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) {
3545 if (ASTReadResult Result = ReadExtensionBlock(F))
3546 return Result;
3547 }
3548
Guy Benyei11169dd2012-12-18 14:30:41 +00003549 // Once read, set the ModuleFile bit base offset and update the size in
3550 // bits of all files we've seen.
3551 F.GlobalBitOffset = TotalModulesSizeInBits;
3552 TotalModulesSizeInBits += F.SizeInBits;
3553 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
3554
3555 // Preload SLocEntries.
3556 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
3557 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
3558 // Load it through the SourceManager and don't call ReadSLocEntry()
3559 // directly because the entry may have already been loaded in which case
3560 // calling ReadSLocEntry() directly would trigger an assertion in
3561 // SourceManager.
3562 SourceMgr.getLoadedSLocEntryByID(Index);
3563 }
Richard Smith33e0f7e2015-07-22 02:08:40 +00003564
3565 // Preload all the pending interesting identifiers by marking them out of
3566 // date.
3567 for (auto Offset : F.PreloadIdentifierOffsets) {
3568 const unsigned char *Data = reinterpret_cast<const unsigned char *>(
3569 F.IdentifierTableData + Offset);
3570
3571 ASTIdentifierLookupTrait Trait(*this, F);
3572 auto KeyDataLen = Trait.ReadKeyDataLength(Data);
3573 auto Key = Trait.ReadKey(Data, KeyDataLen.first);
Richard Smith79bf9202015-08-24 03:33:22 +00003574 auto &II = PP.getIdentifierTable().getOwn(Key);
3575 II.setOutOfDate(true);
3576
3577 // Mark this identifier as being from an AST file so that we can track
3578 // whether we need to serialize it.
Richard Smitheb4b58f62016-02-05 01:40:54 +00003579 markIdentifierFromAST(*this, II);
Richard Smith79bf9202015-08-24 03:33:22 +00003580
3581 // Associate the ID with the identifier so that the writer can reuse it.
3582 auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first);
3583 SetIdentifierInfo(ID, &II);
Richard Smith33e0f7e2015-07-22 02:08:40 +00003584 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003585 }
3586
Douglas Gregor603cd862013-03-22 18:50:14 +00003587 // Setup the import locations and notify the module manager that we've
3588 // committed to these module files.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003589 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
3590 MEnd = Loaded.end();
Guy Benyei11169dd2012-12-18 14:30:41 +00003591 M != MEnd; ++M) {
3592 ModuleFile &F = *M->Mod;
Douglas Gregor603cd862013-03-22 18:50:14 +00003593
3594 ModuleMgr.moduleFileAccepted(&F);
3595
3596 // Set the import location.
Argyrios Kyrtzidis71c1af82013-02-01 16:36:14 +00003597 F.DirectImportLoc = ImportLoc;
Guy Benyei11169dd2012-12-18 14:30:41 +00003598 if (!M->ImportedBy)
3599 F.ImportLoc = M->ImportLoc;
3600 else
3601 F.ImportLoc = ReadSourceLocation(*M->ImportedBy,
3602 M->ImportLoc.getRawEncoding());
3603 }
3604
Richard Smith33e0f7e2015-07-22 02:08:40 +00003605 if (!Context.getLangOpts().CPlusPlus ||
3606 (Type != MK_ImplicitModule && Type != MK_ExplicitModule)) {
3607 // Mark all of the identifiers in the identifier table as being out of date,
3608 // so that various accessors know to check the loaded modules when the
3609 // identifier is used.
3610 //
3611 // For C++ modules, we don't need information on many identifiers (just
3612 // those that provide macros or are poisoned), so we mark all of
3613 // the interesting ones via PreloadIdentifierOffsets.
3614 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
3615 IdEnd = PP.getIdentifierTable().end();
3616 Id != IdEnd; ++Id)
3617 Id->second->setOutOfDate(true);
3618 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003619
3620 // Resolve any unresolved module exports.
Douglas Gregorfb912652013-03-20 21:10:35 +00003621 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
3622 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
Guy Benyei11169dd2012-12-18 14:30:41 +00003623 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
3624 Module *ResolvedMod = getSubmodule(GlobalID);
Douglas Gregorfb912652013-03-20 21:10:35 +00003625
3626 switch (Unresolved.Kind) {
3627 case UnresolvedModuleRef::Conflict:
3628 if (ResolvedMod) {
3629 Module::Conflict Conflict;
3630 Conflict.Other = ResolvedMod;
3631 Conflict.Message = Unresolved.String.str();
3632 Unresolved.Mod->Conflicts.push_back(Conflict);
3633 }
3634 continue;
3635
3636 case UnresolvedModuleRef::Import:
Guy Benyei11169dd2012-12-18 14:30:41 +00003637 if (ResolvedMod)
Richard Smith38477db2015-05-02 00:45:56 +00003638 Unresolved.Mod->Imports.insert(ResolvedMod);
Guy Benyei11169dd2012-12-18 14:30:41 +00003639 continue;
Guy Benyei11169dd2012-12-18 14:30:41 +00003640
Douglas Gregorfb912652013-03-20 21:10:35 +00003641 case UnresolvedModuleRef::Export:
3642 if (ResolvedMod || Unresolved.IsWildcard)
3643 Unresolved.Mod->Exports.push_back(
3644 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
3645 continue;
3646 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003647 }
Douglas Gregorfb912652013-03-20 21:10:35 +00003648 UnresolvedModuleRefs.clear();
Daniel Jasperba7f2f72013-09-24 09:14:14 +00003649
3650 // FIXME: How do we load the 'use'd modules? They may not be submodules.
3651 // Might be unnecessary as use declarations are only used to build the
3652 // module itself.
Guy Benyei11169dd2012-12-18 14:30:41 +00003653
3654 InitializeContext();
3655
Richard Smith3d8e97e2013-10-18 06:54:39 +00003656 if (SemaObj)
3657 UpdateSema();
3658
Guy Benyei11169dd2012-12-18 14:30:41 +00003659 if (DeserializationListener)
3660 DeserializationListener->ReaderInitialized(this);
3661
3662 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
Yaron Keren8b563662015-10-03 10:46:20 +00003663 if (PrimaryModule.OriginalSourceFileID.isValid()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003664 PrimaryModule.OriginalSourceFileID
3665 = FileID::get(PrimaryModule.SLocEntryBaseID
3666 + PrimaryModule.OriginalSourceFileID.getOpaqueValue() - 1);
3667
3668 // If this AST file is a precompiled preamble, then set the
3669 // preamble file ID of the source manager to the file source file
3670 // from which the preamble was built.
3671 if (Type == MK_Preamble) {
3672 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
3673 } else if (Type == MK_MainFile) {
3674 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
3675 }
3676 }
3677
3678 // For any Objective-C class definitions we have already loaded, make sure
3679 // that we load any additional categories.
3680 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
3681 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
3682 ObjCClassesLoaded[I],
3683 PreviousGeneration);
3684 }
Douglas Gregore060e572013-01-25 01:03:03 +00003685
Dmitri Gribenkof430da42014-02-12 10:33:14 +00003686 if (PP.getHeaderSearchInfo()
3687 .getHeaderSearchOpts()
3688 .ModulesValidateOncePerBuildSession) {
3689 // Now we are certain that the module and all modules it depends on are
3690 // up to date. Create or update timestamp files for modules that are
3691 // located in the module cache (not for PCH files that could be anywhere
3692 // in the filesystem).
3693 for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
3694 ImportedModule &M = Loaded[I];
Richard Smithe842a472014-10-22 02:05:46 +00003695 if (M.Mod->Kind == MK_ImplicitModule) {
Dmitri Gribenkof430da42014-02-12 10:33:14 +00003696 updateModuleTimestamp(*M.Mod);
3697 }
3698 }
3699 }
3700
Guy Benyei11169dd2012-12-18 14:30:41 +00003701 return Success;
3702}
3703
Ben Langmuir487ea142014-10-23 18:05:36 +00003704static ASTFileSignature readASTFileSignature(llvm::BitstreamReader &StreamFile);
3705
Ben Langmuir70a1b812015-03-24 04:43:52 +00003706/// \brief Whether \p Stream starts with the AST/PCH file magic number 'CPCH'.
3707static bool startsWithASTFileMagic(BitstreamCursor &Stream) {
3708 return Stream.Read(8) == 'C' &&
3709 Stream.Read(8) == 'P' &&
3710 Stream.Read(8) == 'C' &&
3711 Stream.Read(8) == 'H';
3712}
3713
Richard Smith0f99d6a2015-08-09 08:48:41 +00003714static unsigned moduleKindForDiagnostic(ModuleKind Kind) {
3715 switch (Kind) {
3716 case MK_PCH:
3717 return 0; // PCH
3718 case MK_ImplicitModule:
3719 case MK_ExplicitModule:
3720 return 1; // module
3721 case MK_MainFile:
3722 case MK_Preamble:
3723 return 2; // main source file
3724 }
3725 llvm_unreachable("unknown module kind");
3726}
3727
Guy Benyei11169dd2012-12-18 14:30:41 +00003728ASTReader::ASTReadResult
3729ASTReader::ReadASTCore(StringRef FileName,
3730 ModuleKind Type,
3731 SourceLocation ImportLoc,
3732 ModuleFile *ImportedBy,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003733 SmallVectorImpl<ImportedModule> &Loaded,
Douglas Gregor7029ce12013-03-19 00:28:20 +00003734 off_t ExpectedSize, time_t ExpectedModTime,
Ben Langmuir487ea142014-10-23 18:05:36 +00003735 ASTFileSignature ExpectedSignature,
Guy Benyei11169dd2012-12-18 14:30:41 +00003736 unsigned ClientLoadCapabilities) {
3737 ModuleFile *M;
Guy Benyei11169dd2012-12-18 14:30:41 +00003738 std::string ErrorStr;
Douglas Gregor7029ce12013-03-19 00:28:20 +00003739 ModuleManager::AddModuleResult AddResult
3740 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
Richard Smith053f6c62014-05-16 23:01:30 +00003741 getGeneration(), ExpectedSize, ExpectedModTime,
Ben Langmuir487ea142014-10-23 18:05:36 +00003742 ExpectedSignature, readASTFileSignature,
Douglas Gregor7029ce12013-03-19 00:28:20 +00003743 M, ErrorStr);
Guy Benyei11169dd2012-12-18 14:30:41 +00003744
Douglas Gregor7029ce12013-03-19 00:28:20 +00003745 switch (AddResult) {
3746 case ModuleManager::AlreadyLoaded:
3747 return Success;
3748
3749 case ModuleManager::NewlyLoaded:
3750 // Load module file below.
3751 break;
3752
3753 case ModuleManager::Missing:
Richard Smithe842a472014-10-22 02:05:46 +00003754 // The module file was missing; if the client can handle that, return
Douglas Gregor7029ce12013-03-19 00:28:20 +00003755 // it.
3756 if (ClientLoadCapabilities & ARR_Missing)
3757 return Missing;
3758
3759 // Otherwise, return an error.
Richard Smith0f99d6a2015-08-09 08:48:41 +00003760 Diag(diag::err_module_file_not_found) << moduleKindForDiagnostic(Type)
3761 << FileName << ErrorStr.empty()
3762 << ErrorStr;
Douglas Gregor7029ce12013-03-19 00:28:20 +00003763 return Failure;
3764
3765 case ModuleManager::OutOfDate:
3766 // We couldn't load the module file because it is out-of-date. If the
3767 // client can handle out-of-date, return it.
3768 if (ClientLoadCapabilities & ARR_OutOfDate)
3769 return OutOfDate;
3770
3771 // Otherwise, return an error.
Richard Smith0f99d6a2015-08-09 08:48:41 +00003772 Diag(diag::err_module_file_out_of_date) << moduleKindForDiagnostic(Type)
3773 << FileName << ErrorStr.empty()
3774 << ErrorStr;
Guy Benyei11169dd2012-12-18 14:30:41 +00003775 return Failure;
3776 }
3777
Douglas Gregor7029ce12013-03-19 00:28:20 +00003778 assert(M && "Missing module file");
Guy Benyei11169dd2012-12-18 14:30:41 +00003779
3780 // FIXME: This seems rather a hack. Should CurrentDir be part of the
3781 // module?
3782 if (FileName != "-") {
3783 CurrentDir = llvm::sys::path::parent_path(FileName);
3784 if (CurrentDir.empty()) CurrentDir = ".";
3785 }
3786
3787 ModuleFile &F = *M;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003788 BitstreamCursor &Stream = F.Stream;
Adrian Prantlfb2398d2015-07-17 01:19:54 +00003789 PCHContainerRdr.ExtractPCH(F.Buffer->getMemBufferRef(), F.StreamFile);
Rafael Espindolafd832392014-11-12 14:48:44 +00003790 Stream.init(&F.StreamFile);
Adrian Prantlcbc368c2015-02-25 02:44:04 +00003791 F.SizeInBits = F.Buffer->getBufferSize() * 8;
3792
Guy Benyei11169dd2012-12-18 14:30:41 +00003793 // Sniff for the signature.
Ben Langmuir70a1b812015-03-24 04:43:52 +00003794 if (!startsWithASTFileMagic(Stream)) {
Richard Smith0f99d6a2015-08-09 08:48:41 +00003795 Diag(diag::err_module_file_invalid) << moduleKindForDiagnostic(Type)
3796 << FileName;
Guy Benyei11169dd2012-12-18 14:30:41 +00003797 return Failure;
3798 }
3799
3800 // This is used for compatibility with older PCH formats.
3801 bool HaveReadControlBlock = false;
Chris Lattnerefa77172013-01-20 00:00:22 +00003802 while (1) {
3803 llvm::BitstreamEntry Entry = Stream.advance();
3804
3805 switch (Entry.Kind) {
3806 case llvm::BitstreamEntry::Error:
Chris Lattnerefa77172013-01-20 00:00:22 +00003807 case llvm::BitstreamEntry::Record:
Douglas Gregor6623e1f2015-11-03 18:33:07 +00003808 case llvm::BitstreamEntry::EndBlock:
Guy Benyei11169dd2012-12-18 14:30:41 +00003809 Error("invalid record at top-level of AST file");
3810 return Failure;
Chris Lattnerefa77172013-01-20 00:00:22 +00003811
3812 case llvm::BitstreamEntry::SubBlock:
3813 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003814 }
3815
Chris Lattnerefa77172013-01-20 00:00:22 +00003816 switch (Entry.ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003817 case CONTROL_BLOCK_ID:
3818 HaveReadControlBlock = true;
Ben Langmuirbeee15e2014-04-14 18:00:01 +00003819 switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003820 case Success:
Richard Smith0f99d6a2015-08-09 08:48:41 +00003821 // Check that we didn't try to load a non-module AST file as a module.
3822 //
3823 // FIXME: Should we also perform the converse check? Loading a module as
3824 // a PCH file sort of works, but it's a bit wonky.
3825 if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule) &&
3826 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,
Adrian Prantlbb165fb2015-06-20 18:53:08 +00004206 ASTReaderListener &Listener) {
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,
4246 Listener, IgnoredSuggestedPredefines) != Success)
4247 return true;
4248 break;
4249 }
4250
4251 case INPUT_FILES_BLOCK_ID:
4252 InputFilesCursor = Stream;
4253 if (Stream.SkipBlock() ||
4254 (NeedsInputFiles &&
4255 ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID)))
4256 return true;
4257 break;
4258
4259 default:
4260 if (Stream.SkipBlock())
4261 return true;
4262 break;
4263 }
4264
4265 continue;
4266 }
4267
4268 case llvm::BitstreamEntry::EndBlock:
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004269 DoneWithControlBlock = true;
4270 break;
Richard Smith0516b182015-09-08 19:40:14 +00004271
4272 case llvm::BitstreamEntry::Error:
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004273 return true;
Richard Smith0516b182015-09-08 19:40:14 +00004274
4275 case llvm::BitstreamEntry::Record:
4276 break;
4277 }
4278
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004279 if (DoneWithControlBlock) break;
4280
Guy Benyei11169dd2012-12-18 14:30:41 +00004281 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00004282 StringRef Blob;
4283 unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob);
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004284 switch ((ControlRecordTypes)RecCode) {
4285 case METADATA: {
4286 if (Record[0] != VERSION_MAJOR)
4287 return true;
Guy Benyei11169dd2012-12-18 14:30:41 +00004288
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +00004289 if (Listener.ReadFullVersionInformation(Blob))
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004290 return true;
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +00004291
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004292 break;
4293 }
Ben Langmuir4f5212a2014-04-14 22:12:44 +00004294 case MODULE_NAME:
4295 Listener.ReadModuleName(Blob);
4296 break;
Richard Smith7ed1bc92014-12-05 22:42:13 +00004297 case MODULE_DIRECTORY:
4298 ModuleDir = Blob;
4299 break;
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00004300 case MODULE_MAP_FILE: {
4301 unsigned Idx = 0;
Richard Smith7ed1bc92014-12-05 22:42:13 +00004302 auto Path = ReadString(Record, Idx);
4303 ResolveImportedPath(Path, ModuleDir);
4304 Listener.ReadModuleMapFile(Path);
Ben Langmuir4f5212a2014-04-14 22:12:44 +00004305 break;
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00004306 }
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004307 case INPUT_FILE_OFFSETS: {
4308 if (!NeedsInputFiles)
4309 break;
4310
4311 unsigned NumInputFiles = Record[0];
4312 unsigned NumUserFiles = Record[1];
Richard Smithec216502015-02-13 19:48:37 +00004313 const uint64_t *InputFileOffs = (const uint64_t *)Blob.data();
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004314 for (unsigned I = 0; I != NumInputFiles; ++I) {
4315 // Go find this input file.
4316 bool isSystemFile = I >= NumUserFiles;
Ben Langmuircb69b572014-03-07 06:40:32 +00004317
4318 if (isSystemFile && !NeedsSystemInputFiles)
4319 break; // the rest are system input files
4320
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004321 BitstreamCursor &Cursor = InputFilesCursor;
4322 SavedStreamPosition SavedPosition(Cursor);
4323 Cursor.JumpToBit(InputFileOffs[I]);
4324
4325 unsigned Code = Cursor.ReadCode();
4326 RecordData Record;
4327 StringRef Blob;
4328 bool shouldContinue = false;
4329 switch ((InputFileRecordTypes)Cursor.readRecord(Code, Record, &Blob)) {
4330 case INPUT_FILE:
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +00004331 bool Overridden = static_cast<bool>(Record[3]);
Richard Smith7ed1bc92014-12-05 22:42:13 +00004332 std::string Filename = Blob;
4333 ResolveImportedPath(Filename, ModuleDir);
Richard Smith216a3bd2015-08-13 17:57:10 +00004334 shouldContinue = Listener.visitInputFile(
4335 Filename, isSystemFile, Overridden, /*IsExplicitModule*/false);
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004336 break;
4337 }
4338 if (!shouldContinue)
4339 break;
4340 }
4341 break;
4342 }
4343
Richard Smithd4b230b2014-10-27 23:01:16 +00004344 case IMPORTS: {
4345 if (!NeedsImports)
4346 break;
4347
4348 unsigned Idx = 0, N = Record.size();
4349 while (Idx < N) {
4350 // Read information about the AST file.
Richard Smith79c98cc2014-10-27 23:25:15 +00004351 Idx += 5; // ImportLoc, Size, ModTime, Signature
Richard Smith7ed1bc92014-12-05 22:42:13 +00004352 std::string Filename = ReadString(Record, Idx);
4353 ResolveImportedPath(Filename, ModuleDir);
4354 Listener.visitImport(Filename);
Richard Smithd4b230b2014-10-27 23:01:16 +00004355 }
4356 break;
4357 }
4358
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004359 default:
4360 // No other validation to perform.
4361 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004362 }
4363 }
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004364
4365 // Look for module file extension blocks, if requested.
4366 if (FindModuleFileExtensions) {
4367 while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) {
4368 bool DoneWithExtensionBlock = false;
4369 while (!DoneWithExtensionBlock) {
4370 llvm::BitstreamEntry Entry = Stream.advance();
4371
4372 switch (Entry.Kind) {
4373 case llvm::BitstreamEntry::SubBlock:
4374 if (Stream.SkipBlock())
4375 return true;
4376
4377 continue;
4378
4379 case llvm::BitstreamEntry::EndBlock:
4380 DoneWithExtensionBlock = true;
4381 continue;
4382
4383 case llvm::BitstreamEntry::Error:
4384 return true;
4385
4386 case llvm::BitstreamEntry::Record:
4387 break;
4388 }
4389
4390 Record.clear();
4391 StringRef Blob;
4392 unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob);
4393 switch (RecCode) {
4394 case EXTENSION_METADATA: {
4395 ModuleFileExtensionMetadata Metadata;
4396 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
4397 return true;
4398
4399 Listener.readModuleFileExtension(Metadata);
4400 break;
4401 }
4402 }
4403 }
4404 }
4405 }
4406
4407 return false;
Guy Benyei11169dd2012-12-18 14:30:41 +00004408}
4409
Adrian Prantlbb165fb2015-06-20 18:53:08 +00004410bool ASTReader::isAcceptableASTFile(
4411 StringRef Filename, FileManager &FileMgr,
Adrian Prantlfb2398d2015-07-17 01:19:54 +00004412 const PCHContainerReader &PCHContainerRdr, const LangOptions &LangOpts,
Adrian Prantlbb165fb2015-06-20 18:53:08 +00004413 const TargetOptions &TargetOpts, const PreprocessorOptions &PPOpts,
4414 std::string ExistingModuleCachePath) {
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00004415 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts,
4416 ExistingModuleCachePath, FileMgr);
Adrian Prantlfb2398d2015-07-17 01:19:54 +00004417 return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr,
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004418 /*FindModuleFileExtensions=*/false,
Adrian Prantlbb165fb2015-06-20 18:53:08 +00004419 validator);
Guy Benyei11169dd2012-12-18 14:30:41 +00004420}
4421
Ben Langmuir2c9af442014-04-10 17:57:43 +00004422ASTReader::ASTReadResult
4423ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004424 // Enter the submodule block.
4425 if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
4426 Error("malformed submodule block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00004427 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00004428 }
4429
4430 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
4431 bool First = true;
Craig Toppera13603a2014-05-22 05:54:18 +00004432 Module *CurrentModule = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00004433 RecordData Record;
4434 while (true) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004435 llvm::BitstreamEntry Entry = F.Stream.advanceSkippingSubblocks();
4436
4437 switch (Entry.Kind) {
4438 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
4439 case llvm::BitstreamEntry::Error:
4440 Error("malformed block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00004441 return Failure;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004442 case llvm::BitstreamEntry::EndBlock:
Ben Langmuir2c9af442014-04-10 17:57:43 +00004443 return Success;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004444 case llvm::BitstreamEntry::Record:
4445 // The interesting case.
4446 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004447 }
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004448
Guy Benyei11169dd2012-12-18 14:30:41 +00004449 // Read a record.
Chris Lattner0e6c9402013-01-20 02:38:54 +00004450 StringRef Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00004451 Record.clear();
Richard Smith03478d92014-10-23 22:12:14 +00004452 auto Kind = F.Stream.readRecord(Entry.ID, Record, &Blob);
4453
4454 if ((Kind == SUBMODULE_METADATA) != First) {
4455 Error("submodule metadata record should be at beginning of block");
4456 return Failure;
4457 }
4458 First = false;
4459
4460 // Submodule information is only valid if we have a current module.
4461 // FIXME: Should we error on these cases?
4462 if (!CurrentModule && Kind != SUBMODULE_METADATA &&
4463 Kind != SUBMODULE_DEFINITION)
4464 continue;
4465
4466 switch (Kind) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004467 default: // Default behavior: ignore.
4468 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004469
Richard Smith03478d92014-10-23 22:12:14 +00004470 case SUBMODULE_DEFINITION: {
Douglas Gregor8d932422013-03-20 03:59:18 +00004471 if (Record.size() < 8) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004472 Error("malformed module definition");
Ben Langmuir2c9af442014-04-10 17:57:43 +00004473 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00004474 }
Richard Smith03478d92014-10-23 22:12:14 +00004475
Chris Lattner0e6c9402013-01-20 02:38:54 +00004476 StringRef Name = Blob;
Richard Smith9bca2982014-03-08 00:03:56 +00004477 unsigned Idx = 0;
4478 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
4479 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
4480 bool IsFramework = Record[Idx++];
4481 bool IsExplicit = Record[Idx++];
4482 bool IsSystem = Record[Idx++];
4483 bool IsExternC = Record[Idx++];
4484 bool InferSubmodules = Record[Idx++];
4485 bool InferExplicitSubmodules = Record[Idx++];
4486 bool InferExportWildcard = Record[Idx++];
4487 bool ConfigMacrosExhaustive = Record[Idx++];
Douglas Gregor8d932422013-03-20 03:59:18 +00004488
Ben Langmuirbeee15e2014-04-14 18:00:01 +00004489 Module *ParentModule = nullptr;
Ben Langmuir9d6448b2014-08-09 00:57:23 +00004490 if (Parent)
Guy Benyei11169dd2012-12-18 14:30:41 +00004491 ParentModule = getSubmodule(Parent);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00004492
Guy Benyei11169dd2012-12-18 14:30:41 +00004493 // Retrieve this (sub)module from the module map, creating it if
4494 // necessary.
Ben Langmuir9d6448b2014-08-09 00:57:23 +00004495 CurrentModule = ModMap.findOrCreateModule(Name, ParentModule, IsFramework,
Guy Benyei11169dd2012-12-18 14:30:41 +00004496 IsExplicit).first;
Ben Langmuir9d6448b2014-08-09 00:57:23 +00004497
4498 // FIXME: set the definition loc for CurrentModule, or call
4499 // ModMap.setInferredModuleAllowedBy()
4500
Guy Benyei11169dd2012-12-18 14:30:41 +00004501 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
4502 if (GlobalIndex >= SubmodulesLoaded.size() ||
4503 SubmodulesLoaded[GlobalIndex]) {
4504 Error("too many submodules");
Ben Langmuir2c9af442014-04-10 17:57:43 +00004505 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00004506 }
Douglas Gregor8a114ab2013-02-06 22:40:31 +00004507
Douglas Gregor7029ce12013-03-19 00:28:20 +00004508 if (!ParentModule) {
4509 if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
4510 if (CurFile != F.File) {
4511 if (!Diags.isDiagnosticInFlight()) {
4512 Diag(diag::err_module_file_conflict)
4513 << CurrentModule->getTopLevelModuleName()
4514 << CurFile->getName()
4515 << F.File->getName();
4516 }
Ben Langmuir2c9af442014-04-10 17:57:43 +00004517 return Failure;
Douglas Gregor8a114ab2013-02-06 22:40:31 +00004518 }
Douglas Gregor8a114ab2013-02-06 22:40:31 +00004519 }
Douglas Gregor7029ce12013-03-19 00:28:20 +00004520
4521 CurrentModule->setASTFile(F.File);
Douglas Gregor8a114ab2013-02-06 22:40:31 +00004522 }
Ben Langmuirbeee15e2014-04-14 18:00:01 +00004523
Adrian Prantl15bcf702015-06-30 17:39:43 +00004524 CurrentModule->Signature = F.Signature;
Guy Benyei11169dd2012-12-18 14:30:41 +00004525 CurrentModule->IsFromModuleFile = true;
4526 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
Richard Smith9bca2982014-03-08 00:03:56 +00004527 CurrentModule->IsExternC = IsExternC;
Guy Benyei11169dd2012-12-18 14:30:41 +00004528 CurrentModule->InferSubmodules = InferSubmodules;
4529 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
4530 CurrentModule->InferExportWildcard = InferExportWildcard;
Douglas Gregor8d932422013-03-20 03:59:18 +00004531 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
Guy Benyei11169dd2012-12-18 14:30:41 +00004532 if (DeserializationListener)
4533 DeserializationListener->ModuleRead(GlobalID, CurrentModule);
4534
4535 SubmodulesLoaded[GlobalIndex] = CurrentModule;
Douglas Gregor6ddfca92013-01-14 17:21:00 +00004536
Douglas Gregorfb912652013-03-20 21:10:35 +00004537 // Clear out data that will be replaced by what is the module file.
Douglas Gregor6ddfca92013-01-14 17:21:00 +00004538 CurrentModule->LinkLibraries.clear();
Douglas Gregor8d932422013-03-20 03:59:18 +00004539 CurrentModule->ConfigMacros.clear();
Douglas Gregorfb912652013-03-20 21:10:35 +00004540 CurrentModule->UnresolvedConflicts.clear();
4541 CurrentModule->Conflicts.clear();
Guy Benyei11169dd2012-12-18 14:30:41 +00004542 break;
4543 }
4544
4545 case SUBMODULE_UMBRELLA_HEADER: {
Richard Smith2b63d152015-05-16 02:28:53 +00004546 std::string Filename = Blob;
4547 ResolveImportedPath(F, Filename);
4548 if (auto *Umbrella = PP.getFileManager().getFile(Filename)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004549 if (!CurrentModule->getUmbrellaHeader())
Richard Smith2b63d152015-05-16 02:28:53 +00004550 ModMap.setUmbrellaHeader(CurrentModule, Umbrella, Blob);
4551 else if (CurrentModule->getUmbrellaHeader().Entry != Umbrella) {
Ben Langmuirbc35fbe2015-02-20 21:46:39 +00004552 // This can be a spurious difference caused by changing the VFS to
4553 // point to a different copy of the file, and it is too late to
4554 // to rebuild safely.
4555 // FIXME: If we wrote the virtual paths instead of the 'real' paths,
4556 // after input file validation only real problems would remain and we
4557 // could just error. For now, assume it's okay.
4558 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004559 }
4560 }
4561 break;
4562 }
4563
Richard Smith202210b2014-10-24 20:23:01 +00004564 case SUBMODULE_HEADER:
4565 case SUBMODULE_EXCLUDED_HEADER:
4566 case SUBMODULE_PRIVATE_HEADER:
4567 // We lazily associate headers with their modules via the HeaderInfo table.
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00004568 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
4569 // of complete filenames or remove it entirely.
Richard Smith202210b2014-10-24 20:23:01 +00004570 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004571
Richard Smith202210b2014-10-24 20:23:01 +00004572 case SUBMODULE_TEXTUAL_HEADER:
4573 case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
4574 // FIXME: Textual headers are not marked in the HeaderInfo table. Load
4575 // them here.
4576 break;
Lawrence Crowlb53e5482013-06-20 21:14:14 +00004577
Guy Benyei11169dd2012-12-18 14:30:41 +00004578 case SUBMODULE_TOPHEADER: {
Argyrios Kyrtzidis3c5305c2013-03-13 21:13:43 +00004579 CurrentModule->addTopHeaderFilename(Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +00004580 break;
4581 }
4582
4583 case SUBMODULE_UMBRELLA_DIR: {
Richard Smith2b63d152015-05-16 02:28:53 +00004584 std::string Dirname = Blob;
4585 ResolveImportedPath(F, Dirname);
4586 if (auto *Umbrella = PP.getFileManager().getDirectory(Dirname)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004587 if (!CurrentModule->getUmbrellaDir())
Richard Smith2b63d152015-05-16 02:28:53 +00004588 ModMap.setUmbrellaDir(CurrentModule, Umbrella, Blob);
4589 else if (CurrentModule->getUmbrellaDir().Entry != Umbrella) {
Ben Langmuir2c9af442014-04-10 17:57:43 +00004590 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
4591 Error("mismatched umbrella directories in submodule");
4592 return OutOfDate;
Guy Benyei11169dd2012-12-18 14:30:41 +00004593 }
4594 }
4595 break;
4596 }
4597
4598 case SUBMODULE_METADATA: {
Guy Benyei11169dd2012-12-18 14:30:41 +00004599 F.BaseSubmoduleID = getTotalNumSubmodules();
4600 F.LocalNumSubmodules = Record[0];
4601 unsigned LocalBaseSubmoduleID = Record[1];
4602 if (F.LocalNumSubmodules > 0) {
4603 // Introduce the global -> local mapping for submodules within this
4604 // module.
4605 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
4606
4607 // Introduce the local -> global mapping for submodules within this
4608 // module.
4609 F.SubmoduleRemap.insertOrReplace(
4610 std::make_pair(LocalBaseSubmoduleID,
4611 F.BaseSubmoduleID - LocalBaseSubmoduleID));
Ben Langmuirfe971d92014-08-16 04:54:18 +00004612
Ben Langmuir52ca6782014-10-20 16:27:32 +00004613 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
4614 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004615 break;
4616 }
4617
4618 case SUBMODULE_IMPORTS: {
Guy Benyei11169dd2012-12-18 14:30:41 +00004619 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
Douglas Gregorfb912652013-03-20 21:10:35 +00004620 UnresolvedModuleRef Unresolved;
Guy Benyei11169dd2012-12-18 14:30:41 +00004621 Unresolved.File = &F;
4622 Unresolved.Mod = CurrentModule;
4623 Unresolved.ID = Record[Idx];
Douglas Gregorfb912652013-03-20 21:10:35 +00004624 Unresolved.Kind = UnresolvedModuleRef::Import;
Guy Benyei11169dd2012-12-18 14:30:41 +00004625 Unresolved.IsWildcard = false;
Douglas Gregorfb912652013-03-20 21:10:35 +00004626 UnresolvedModuleRefs.push_back(Unresolved);
Guy Benyei11169dd2012-12-18 14:30:41 +00004627 }
4628 break;
4629 }
4630
4631 case SUBMODULE_EXPORTS: {
Guy Benyei11169dd2012-12-18 14:30:41 +00004632 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
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::Export;
Guy Benyei11169dd2012-12-18 14:30:41 +00004638 Unresolved.IsWildcard = Record[Idx + 1];
Douglas Gregorfb912652013-03-20 21:10:35 +00004639 UnresolvedModuleRefs.push_back(Unresolved);
Guy Benyei11169dd2012-12-18 14:30:41 +00004640 }
4641
4642 // Once we've loaded the set of exports, there's no reason to keep
4643 // the parsed, unresolved exports around.
4644 CurrentModule->UnresolvedExports.clear();
4645 break;
4646 }
4647 case SUBMODULE_REQUIRES: {
Richard Smitha3feee22013-10-28 22:18:19 +00004648 CurrentModule->addRequirement(Blob, Record[0], Context.getLangOpts(),
Guy Benyei11169dd2012-12-18 14:30:41 +00004649 Context.getTargetInfo());
4650 break;
4651 }
Douglas Gregor6ddfca92013-01-14 17:21:00 +00004652
4653 case SUBMODULE_LINK_LIBRARY:
Douglas Gregor6ddfca92013-01-14 17:21:00 +00004654 CurrentModule->LinkLibraries.push_back(
Chris Lattner0e6c9402013-01-20 02:38:54 +00004655 Module::LinkLibrary(Blob, Record[0]));
Douglas Gregor6ddfca92013-01-14 17:21:00 +00004656 break;
Douglas Gregor35b13ec2013-03-20 00:22:05 +00004657
4658 case SUBMODULE_CONFIG_MACRO:
Douglas Gregor35b13ec2013-03-20 00:22:05 +00004659 CurrentModule->ConfigMacros.push_back(Blob.str());
4660 break;
Douglas Gregorfb912652013-03-20 21:10:35 +00004661
4662 case SUBMODULE_CONFLICT: {
Douglas Gregorfb912652013-03-20 21:10:35 +00004663 UnresolvedModuleRef Unresolved;
4664 Unresolved.File = &F;
4665 Unresolved.Mod = CurrentModule;
4666 Unresolved.ID = Record[0];
4667 Unresolved.Kind = UnresolvedModuleRef::Conflict;
4668 Unresolved.IsWildcard = false;
4669 Unresolved.String = Blob;
4670 UnresolvedModuleRefs.push_back(Unresolved);
4671 break;
4672 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004673 }
4674 }
4675}
4676
4677/// \brief Parse the record that corresponds to a LangOptions data
4678/// structure.
4679///
4680/// This routine parses the language options from the AST file and then gives
4681/// them to the AST listener if one is set.
4682///
4683/// \returns true if the listener deems the file unacceptable, false otherwise.
4684bool ASTReader::ParseLanguageOptions(const RecordData &Record,
4685 bool Complain,
Richard Smith1e2cf0d2014-10-31 02:28:58 +00004686 ASTReaderListener &Listener,
4687 bool AllowCompatibleDifferences) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004688 LangOptions LangOpts;
4689 unsigned Idx = 0;
4690#define LANGOPT(Name, Bits, Default, Description) \
4691 LangOpts.Name = Record[Idx++];
4692#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
4693 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
4694#include "clang/Basic/LangOptions.def"
Alexey Samsonovedf99a92014-11-07 22:29:38 +00004695#define SANITIZER(NAME, ID) \
4696 LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
Will Dietzf54319c2013-01-18 11:30:38 +00004697#include "clang/Basic/Sanitizers.def"
Guy Benyei11169dd2012-12-18 14:30:41 +00004698
Ben Langmuircd98cb72015-06-23 18:20:18 +00004699 for (unsigned N = Record[Idx++]; N; --N)
4700 LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx));
4701
Guy Benyei11169dd2012-12-18 14:30:41 +00004702 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
4703 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
4704 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00004705
Ben Langmuird4a667a2015-06-23 18:20:23 +00004706 LangOpts.CurrentModule = ReadString(Record, Idx);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00004707
4708 // Comment options.
4709 for (unsigned N = Record[Idx++]; N; --N) {
4710 LangOpts.CommentOpts.BlockCommandNames.push_back(
4711 ReadString(Record, Idx));
4712 }
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00004713 LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00004714
Samuel Antaoee8fb302016-01-06 13:42:12 +00004715 // OpenMP offloading options.
4716 for (unsigned N = Record[Idx++]; N; --N) {
4717 LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx)));
4718 }
4719
4720 LangOpts.OMPHostIRFile = ReadString(Record, Idx);
4721
Richard Smith1e2cf0d2014-10-31 02:28:58 +00004722 return Listener.ReadLanguageOptions(LangOpts, Complain,
4723 AllowCompatibleDifferences);
Guy Benyei11169dd2012-12-18 14:30:41 +00004724}
4725
Chandler Carruth0d745bc2015-03-14 04:47:43 +00004726bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain,
4727 ASTReaderListener &Listener,
4728 bool AllowCompatibleDifferences) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004729 unsigned Idx = 0;
4730 TargetOptions TargetOpts;
4731 TargetOpts.Triple = ReadString(Record, Idx);
4732 TargetOpts.CPU = ReadString(Record, Idx);
4733 TargetOpts.ABI = ReadString(Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00004734 for (unsigned N = Record[Idx++]; N; --N) {
4735 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
4736 }
4737 for (unsigned N = Record[Idx++]; N; --N) {
4738 TargetOpts.Features.push_back(ReadString(Record, Idx));
4739 }
4740
Chandler Carruth0d745bc2015-03-14 04:47:43 +00004741 return Listener.ReadTargetOptions(TargetOpts, Complain,
4742 AllowCompatibleDifferences);
Guy Benyei11169dd2012-12-18 14:30:41 +00004743}
4744
4745bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
4746 ASTReaderListener &Listener) {
Ben Langmuirb92de022014-04-29 16:25:26 +00004747 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions);
Guy Benyei11169dd2012-12-18 14:30:41 +00004748 unsigned Idx = 0;
Ben Langmuirb92de022014-04-29 16:25:26 +00004749#define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00004750#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
Ben Langmuirb92de022014-04-29 16:25:26 +00004751 DiagOpts->set##Name(static_cast<Type>(Record[Idx++]));
Guy Benyei11169dd2012-12-18 14:30:41 +00004752#include "clang/Basic/DiagnosticOptions.def"
4753
Richard Smith3be1cb22014-08-07 00:24:21 +00004754 for (unsigned N = Record[Idx++]; N; --N)
Ben Langmuirb92de022014-04-29 16:25:26 +00004755 DiagOpts->Warnings.push_back(ReadString(Record, Idx));
Richard Smith3be1cb22014-08-07 00:24:21 +00004756 for (unsigned N = Record[Idx++]; N; --N)
4757 DiagOpts->Remarks.push_back(ReadString(Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00004758
4759 return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
4760}
4761
4762bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
4763 ASTReaderListener &Listener) {
4764 FileSystemOptions FSOpts;
4765 unsigned Idx = 0;
4766 FSOpts.WorkingDir = ReadString(Record, Idx);
4767 return Listener.ReadFileSystemOptions(FSOpts, Complain);
4768}
4769
4770bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
4771 bool Complain,
4772 ASTReaderListener &Listener) {
4773 HeaderSearchOptions HSOpts;
4774 unsigned Idx = 0;
4775 HSOpts.Sysroot = ReadString(Record, Idx);
4776
4777 // Include entries.
4778 for (unsigned N = Record[Idx++]; N; --N) {
4779 std::string Path = ReadString(Record, Idx);
4780 frontend::IncludeDirGroup Group
4781 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
Guy Benyei11169dd2012-12-18 14:30:41 +00004782 bool IsFramework = Record[Idx++];
4783 bool IgnoreSysRoot = Record[Idx++];
Benjamin Kramer3204b152015-05-29 19:42:19 +00004784 HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework,
4785 IgnoreSysRoot);
Guy Benyei11169dd2012-12-18 14:30:41 +00004786 }
4787
4788 // System header prefixes.
4789 for (unsigned N = Record[Idx++]; N; --N) {
4790 std::string Prefix = ReadString(Record, Idx);
4791 bool IsSystemHeader = Record[Idx++];
Benjamin Kramer3204b152015-05-29 19:42:19 +00004792 HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader);
Guy Benyei11169dd2012-12-18 14:30:41 +00004793 }
4794
4795 HSOpts.ResourceDir = ReadString(Record, Idx);
4796 HSOpts.ModuleCachePath = ReadString(Record, Idx);
Argyrios Kyrtzidis1594c152014-03-03 08:12:05 +00004797 HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00004798 HSOpts.DisableModuleHash = Record[Idx++];
4799 HSOpts.UseBuiltinIncludes = Record[Idx++];
4800 HSOpts.UseStandardSystemIncludes = Record[Idx++];
4801 HSOpts.UseStandardCXXIncludes = Record[Idx++];
4802 HSOpts.UseLibcxx = Record[Idx++];
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00004803 std::string SpecificModuleCachePath = ReadString(Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00004804
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00004805 return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
4806 Complain);
Guy Benyei11169dd2012-12-18 14:30:41 +00004807}
4808
4809bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
4810 bool Complain,
4811 ASTReaderListener &Listener,
4812 std::string &SuggestedPredefines) {
4813 PreprocessorOptions PPOpts;
4814 unsigned Idx = 0;
4815
4816 // Macro definitions/undefs
4817 for (unsigned N = Record[Idx++]; N; --N) {
4818 std::string Macro = ReadString(Record, Idx);
4819 bool IsUndef = Record[Idx++];
4820 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
4821 }
4822
4823 // Includes
4824 for (unsigned N = Record[Idx++]; N; --N) {
4825 PPOpts.Includes.push_back(ReadString(Record, Idx));
4826 }
4827
4828 // Macro Includes
4829 for (unsigned N = Record[Idx++]; N; --N) {
4830 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
4831 }
4832
4833 PPOpts.UsePredefines = Record[Idx++];
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00004834 PPOpts.DetailedRecord = Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00004835 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
4836 PPOpts.ImplicitPTHInclude = ReadString(Record, Idx);
4837 PPOpts.ObjCXXARCStandardLibrary =
4838 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
4839 SuggestedPredefines.clear();
4840 return Listener.ReadPreprocessorOptions(PPOpts, Complain,
4841 SuggestedPredefines);
4842}
4843
4844std::pair<ModuleFile *, unsigned>
4845ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
4846 GlobalPreprocessedEntityMapType::iterator
4847 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
4848 assert(I != GlobalPreprocessedEntityMap.end() &&
4849 "Corrupted global preprocessed entity map");
4850 ModuleFile *M = I->second;
4851 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
4852 return std::make_pair(M, LocalIndex);
4853}
4854
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00004855llvm::iterator_range<PreprocessingRecord::iterator>
Guy Benyei11169dd2012-12-18 14:30:41 +00004856ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
4857 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
4858 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
4859 Mod.NumPreprocessedEntities);
4860
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00004861 return llvm::make_range(PreprocessingRecord::iterator(),
4862 PreprocessingRecord::iterator());
Guy Benyei11169dd2012-12-18 14:30:41 +00004863}
4864
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00004865llvm::iterator_range<ASTReader::ModuleDeclIterator>
Guy Benyei11169dd2012-12-18 14:30:41 +00004866ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00004867 return llvm::make_range(
4868 ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
4869 ModuleDeclIterator(this, &Mod,
4870 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
Guy Benyei11169dd2012-12-18 14:30:41 +00004871}
4872
4873PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
4874 PreprocessedEntityID PPID = Index+1;
4875 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
4876 ModuleFile &M = *PPInfo.first;
4877 unsigned LocalIndex = PPInfo.second;
4878 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
4879
Guy Benyei11169dd2012-12-18 14:30:41 +00004880 if (!PP.getPreprocessingRecord()) {
4881 Error("no preprocessing record");
Craig Toppera13603a2014-05-22 05:54:18 +00004882 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00004883 }
4884
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004885 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
4886 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
4887
4888 llvm::BitstreamEntry Entry =
4889 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
4890 if (Entry.Kind != llvm::BitstreamEntry::Record)
Craig Toppera13603a2014-05-22 05:54:18 +00004891 return nullptr;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004892
Guy Benyei11169dd2012-12-18 14:30:41 +00004893 // Read the record.
4894 SourceRange Range(ReadSourceLocation(M, PPOffs.Begin),
4895 ReadSourceLocation(M, PPOffs.End));
4896 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
Chris Lattner0e6c9402013-01-20 02:38:54 +00004897 StringRef Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00004898 RecordData Record;
4899 PreprocessorDetailRecordTypes RecType =
Chris Lattner0e6c9402013-01-20 02:38:54 +00004900 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.readRecord(
4901 Entry.ID, Record, &Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +00004902 switch (RecType) {
4903 case PPD_MACRO_EXPANSION: {
4904 bool isBuiltin = Record[0];
Craig Toppera13603a2014-05-22 05:54:18 +00004905 IdentifierInfo *Name = nullptr;
Richard Smith66a81862015-05-04 02:25:31 +00004906 MacroDefinitionRecord *Def = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00004907 if (isBuiltin)
4908 Name = getLocalIdentifier(M, Record[1]);
4909 else {
Richard Smith66a81862015-05-04 02:25:31 +00004910 PreprocessedEntityID GlobalID =
4911 getGlobalPreprocessedEntityID(M, Record[1]);
4912 Def = cast<MacroDefinitionRecord>(
4913 PPRec.getLoadedPreprocessedEntity(GlobalID - 1));
Guy Benyei11169dd2012-12-18 14:30:41 +00004914 }
4915
4916 MacroExpansion *ME;
4917 if (isBuiltin)
4918 ME = new (PPRec) MacroExpansion(Name, Range);
4919 else
4920 ME = new (PPRec) MacroExpansion(Def, Range);
4921
4922 return ME;
4923 }
4924
4925 case PPD_MACRO_DEFINITION: {
4926 // Decode the identifier info and then check again; if the macro is
4927 // still defined and associated with the identifier,
4928 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
Richard Smith66a81862015-05-04 02:25:31 +00004929 MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
Guy Benyei11169dd2012-12-18 14:30:41 +00004930
4931 if (DeserializationListener)
4932 DeserializationListener->MacroDefinitionRead(PPID, MD);
4933
4934 return MD;
4935 }
4936
4937 case PPD_INCLUSION_DIRECTIVE: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00004938 const char *FullFileNameStart = Blob.data() + Record[0];
4939 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
Craig Toppera13603a2014-05-22 05:54:18 +00004940 const FileEntry *File = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00004941 if (!FullFileName.empty())
4942 File = PP.getFileManager().getFile(FullFileName);
4943
4944 // FIXME: Stable encoding
4945 InclusionDirective::InclusionKind Kind
4946 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
4947 InclusionDirective *ID
4948 = new (PPRec) InclusionDirective(PPRec, Kind,
Chris Lattner0e6c9402013-01-20 02:38:54 +00004949 StringRef(Blob.data(), Record[0]),
Guy Benyei11169dd2012-12-18 14:30:41 +00004950 Record[1], Record[3],
4951 File,
4952 Range);
4953 return ID;
4954 }
4955 }
4956
4957 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
4958}
4959
4960/// \brief \arg SLocMapI points at a chunk of a module that contains no
4961/// preprocessed entities or the entities it contains are not the ones we are
4962/// looking for. Find the next module that contains entities and return the ID
4963/// of the first entry.
4964PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
4965 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
4966 ++SLocMapI;
4967 for (GlobalSLocOffsetMapType::const_iterator
4968 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
4969 ModuleFile &M = *SLocMapI->second;
4970 if (M.NumPreprocessedEntities)
4971 return M.BasePreprocessedEntityID;
4972 }
4973
4974 return getTotalNumPreprocessedEntities();
4975}
4976
4977namespace {
4978
4979template <unsigned PPEntityOffset::*PPLoc>
4980struct PPEntityComp {
4981 const ASTReader &Reader;
4982 ModuleFile &M;
4983
4984 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) { }
4985
4986 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
4987 SourceLocation LHS = getLoc(L);
4988 SourceLocation RHS = getLoc(R);
4989 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4990 }
4991
4992 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
4993 SourceLocation LHS = getLoc(L);
4994 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4995 }
4996
4997 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
4998 SourceLocation RHS = getLoc(R);
4999 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5000 }
5001
5002 SourceLocation getLoc(const PPEntityOffset &PPE) const {
5003 return Reader.ReadSourceLocation(M, PPE.*PPLoc);
5004 }
5005};
5006
Alexander Kornienkoab9db512015-06-22 23:07:51 +00005007}
Guy Benyei11169dd2012-12-18 14:30:41 +00005008
Alp Toker2e9ce4c2014-05-16 18:59:21 +00005009PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
5010 bool EndsAfter) const {
5011 if (SourceMgr.isLocalSourceLocation(Loc))
Guy Benyei11169dd2012-12-18 14:30:41 +00005012 return getTotalNumPreprocessedEntities();
5013
Alp Toker2e9ce4c2014-05-16 18:59:21 +00005014 GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
5015 SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
Guy Benyei11169dd2012-12-18 14:30:41 +00005016 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
5017 "Corrupted global sloc offset map");
5018
5019 if (SLocMapI->second->NumPreprocessedEntities == 0)
5020 return findNextPreprocessedEntity(SLocMapI);
5021
5022 ModuleFile &M = *SLocMapI->second;
5023 typedef const PPEntityOffset *pp_iterator;
5024 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
5025 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
5026
5027 size_t Count = M.NumPreprocessedEntities;
5028 size_t Half;
5029 pp_iterator First = pp_begin;
5030 pp_iterator PPI;
5031
Alp Toker2e9ce4c2014-05-16 18:59:21 +00005032 if (EndsAfter) {
5033 PPI = std::upper_bound(pp_begin, pp_end, Loc,
5034 PPEntityComp<&PPEntityOffset::Begin>(*this, M));
5035 } else {
5036 // Do a binary search manually instead of using std::lower_bound because
5037 // The end locations of entities may be unordered (when a macro expansion
5038 // is inside another macro argument), but for this case it is not important
5039 // whether we get the first macro expansion or its containing macro.
5040 while (Count > 0) {
5041 Half = Count / 2;
5042 PPI = First;
5043 std::advance(PPI, Half);
5044 if (SourceMgr.isBeforeInTranslationUnit(ReadSourceLocation(M, PPI->End),
5045 Loc)) {
5046 First = PPI;
5047 ++First;
5048 Count = Count - Half - 1;
5049 } else
5050 Count = Half;
5051 }
Guy Benyei11169dd2012-12-18 14:30:41 +00005052 }
5053
5054 if (PPI == pp_end)
5055 return findNextPreprocessedEntity(SLocMapI);
5056
5057 return M.BasePreprocessedEntityID + (PPI - pp_begin);
5058}
5059
Guy Benyei11169dd2012-12-18 14:30:41 +00005060/// \brief Returns a pair of [Begin, End) indices of preallocated
5061/// preprocessed entities that \arg Range encompasses.
5062std::pair<unsigned, unsigned>
5063 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
5064 if (Range.isInvalid())
5065 return std::make_pair(0,0);
5066 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
5067
Alp Toker2e9ce4c2014-05-16 18:59:21 +00005068 PreprocessedEntityID BeginID =
5069 findPreprocessedEntity(Range.getBegin(), false);
5070 PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true);
Guy Benyei11169dd2012-12-18 14:30:41 +00005071 return std::make_pair(BeginID, EndID);
5072}
5073
5074/// \brief Optionally returns true or false if the preallocated preprocessed
5075/// entity with index \arg Index came from file \arg FID.
David Blaikie05785d12013-02-20 22:23:23 +00005076Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
Guy Benyei11169dd2012-12-18 14:30:41 +00005077 FileID FID) {
5078 if (FID.isInvalid())
5079 return false;
5080
5081 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
5082 ModuleFile &M = *PPInfo.first;
5083 unsigned LocalIndex = PPInfo.second;
5084 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
5085
5086 SourceLocation Loc = ReadSourceLocation(M, PPOffs.Begin);
5087 if (Loc.isInvalid())
5088 return false;
5089
5090 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
5091 return true;
5092 else
5093 return false;
5094}
5095
5096namespace {
5097 /// \brief Visitor used to search for information about a header file.
5098 class HeaderFileInfoVisitor {
Guy Benyei11169dd2012-12-18 14:30:41 +00005099 const FileEntry *FE;
5100
David Blaikie05785d12013-02-20 22:23:23 +00005101 Optional<HeaderFileInfo> HFI;
Guy Benyei11169dd2012-12-18 14:30:41 +00005102
5103 public:
Argyrios Kyrtzidis61a38962013-03-06 18:12:44 +00005104 explicit HeaderFileInfoVisitor(const FileEntry *FE)
5105 : FE(FE) { }
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00005106
5107 bool operator()(ModuleFile &M) {
Guy Benyei11169dd2012-12-18 14:30:41 +00005108 HeaderFileInfoLookupTable *Table
5109 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
5110 if (!Table)
5111 return false;
5112
5113 // Look in the on-disk hash table for an entry for this file name.
Richard Smithbdf2d932015-07-30 03:37:16 +00005114 HeaderFileInfoLookupTable::iterator Pos = Table->find(FE);
Guy Benyei11169dd2012-12-18 14:30:41 +00005115 if (Pos == Table->end())
5116 return false;
5117
Richard Smithbdf2d932015-07-30 03:37:16 +00005118 HFI = *Pos;
Guy Benyei11169dd2012-12-18 14:30:41 +00005119 return true;
5120 }
5121
David Blaikie05785d12013-02-20 22:23:23 +00005122 Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
Guy Benyei11169dd2012-12-18 14:30:41 +00005123 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +00005124}
Guy Benyei11169dd2012-12-18 14:30:41 +00005125
5126HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
Argyrios Kyrtzidis61a38962013-03-06 18:12:44 +00005127 HeaderFileInfoVisitor Visitor(FE);
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00005128 ModuleMgr.visit(Visitor);
Argyrios Kyrtzidis1054bbf2013-05-08 23:46:55 +00005129 if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
Guy Benyei11169dd2012-12-18 14:30:41 +00005130 return *HFI;
Guy Benyei11169dd2012-12-18 14:30:41 +00005131
5132 return HeaderFileInfo();
5133}
5134
5135void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
5136 // FIXME: Make it work properly with modules.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00005137 SmallVector<DiagnosticsEngine::DiagState *, 32> DiagStates;
Guy Benyei11169dd2012-12-18 14:30:41 +00005138 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
5139 ModuleFile &F = *(*I);
5140 unsigned Idx = 0;
5141 DiagStates.clear();
5142 assert(!Diag.DiagStates.empty());
5143 DiagStates.push_back(&Diag.DiagStates.front()); // the command-line one.
5144 while (Idx < F.PragmaDiagMappings.size()) {
5145 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
5146 unsigned DiagStateID = F.PragmaDiagMappings[Idx++];
5147 if (DiagStateID != 0) {
5148 Diag.DiagStatePoints.push_back(
5149 DiagnosticsEngine::DiagStatePoint(DiagStates[DiagStateID-1],
5150 FullSourceLoc(Loc, SourceMgr)));
5151 continue;
5152 }
5153
5154 assert(DiagStateID == 0);
5155 // A new DiagState was created here.
5156 Diag.DiagStates.push_back(*Diag.GetCurDiagState());
5157 DiagnosticsEngine::DiagState *NewState = &Diag.DiagStates.back();
5158 DiagStates.push_back(NewState);
5159 Diag.DiagStatePoints.push_back(
5160 DiagnosticsEngine::DiagStatePoint(NewState,
5161 FullSourceLoc(Loc, SourceMgr)));
5162 while (1) {
5163 assert(Idx < F.PragmaDiagMappings.size() &&
5164 "Invalid data, didn't find '-1' marking end of diag/map pairs");
5165 if (Idx >= F.PragmaDiagMappings.size()) {
5166 break; // Something is messed up but at least avoid infinite loop in
5167 // release build.
5168 }
5169 unsigned DiagID = F.PragmaDiagMappings[Idx++];
5170 if (DiagID == (unsigned)-1) {
5171 break; // no more diag/map pairs for this location.
5172 }
Alp Tokerc726c362014-06-10 09:31:37 +00005173 diag::Severity Map = (diag::Severity)F.PragmaDiagMappings[Idx++];
5174 DiagnosticMapping Mapping = Diag.makeUserMapping(Map, Loc);
5175 Diag.GetCurDiagState()->setMapping(DiagID, Mapping);
Guy Benyei11169dd2012-12-18 14:30:41 +00005176 }
5177 }
5178 }
5179}
5180
5181/// \brief Get the correct cursor and offset for loading a type.
5182ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
5183 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
5184 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
5185 ModuleFile *M = I->second;
5186 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
5187}
5188
5189/// \brief Read and return the type with the given index..
5190///
5191/// The index is the type ID, shifted and minus the number of predefs. This
5192/// routine actually reads the record corresponding to the type at the given
5193/// location. It is a helper routine for GetType, which deals with reading type
5194/// IDs.
5195QualType ASTReader::readTypeRecord(unsigned Index) {
5196 RecordLocation Loc = TypeCursorForIndex(Index);
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005197 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00005198
5199 // Keep track of where we are in the stream, then jump back there
5200 // after reading this type.
5201 SavedStreamPosition SavedPosition(DeclsCursor);
5202
5203 ReadingKindTracker ReadingKind(Read_Type, *this);
5204
5205 // Note that we are loading a type record.
5206 Deserializing AType(this);
5207
5208 unsigned Idx = 0;
5209 DeclsCursor.JumpToBit(Loc.Offset);
5210 RecordData Record;
5211 unsigned Code = DeclsCursor.ReadCode();
Chris Lattner0e6c9402013-01-20 02:38:54 +00005212 switch ((TypeCode)DeclsCursor.readRecord(Code, Record)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00005213 case TYPE_EXT_QUAL: {
5214 if (Record.size() != 2) {
5215 Error("Incorrect encoding of extended qualifier type");
5216 return QualType();
5217 }
5218 QualType Base = readType(*Loc.F, Record, Idx);
5219 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
5220 return Context.getQualifiedType(Base, Quals);
5221 }
5222
5223 case TYPE_COMPLEX: {
5224 if (Record.size() != 1) {
5225 Error("Incorrect encoding of complex type");
5226 return QualType();
5227 }
5228 QualType ElemType = readType(*Loc.F, Record, Idx);
5229 return Context.getComplexType(ElemType);
5230 }
5231
5232 case TYPE_POINTER: {
5233 if (Record.size() != 1) {
5234 Error("Incorrect encoding of pointer type");
5235 return QualType();
5236 }
5237 QualType PointeeType = readType(*Loc.F, Record, Idx);
5238 return Context.getPointerType(PointeeType);
5239 }
5240
Reid Kleckner8a365022013-06-24 17:51:48 +00005241 case TYPE_DECAYED: {
5242 if (Record.size() != 1) {
5243 Error("Incorrect encoding of decayed type");
5244 return QualType();
5245 }
5246 QualType OriginalType = readType(*Loc.F, Record, Idx);
5247 QualType DT = Context.getAdjustedParameterType(OriginalType);
5248 if (!isa<DecayedType>(DT))
5249 Error("Decayed type does not decay");
5250 return DT;
5251 }
5252
Reid Kleckner0503a872013-12-05 01:23:43 +00005253 case TYPE_ADJUSTED: {
5254 if (Record.size() != 2) {
5255 Error("Incorrect encoding of adjusted type");
5256 return QualType();
5257 }
5258 QualType OriginalTy = readType(*Loc.F, Record, Idx);
5259 QualType AdjustedTy = readType(*Loc.F, Record, Idx);
5260 return Context.getAdjustedType(OriginalTy, AdjustedTy);
5261 }
5262
Guy Benyei11169dd2012-12-18 14:30:41 +00005263 case TYPE_BLOCK_POINTER: {
5264 if (Record.size() != 1) {
5265 Error("Incorrect encoding of block pointer type");
5266 return QualType();
5267 }
5268 QualType PointeeType = readType(*Loc.F, Record, Idx);
5269 return Context.getBlockPointerType(PointeeType);
5270 }
5271
5272 case TYPE_LVALUE_REFERENCE: {
5273 if (Record.size() != 2) {
5274 Error("Incorrect encoding of lvalue reference type");
5275 return QualType();
5276 }
5277 QualType PointeeType = readType(*Loc.F, Record, Idx);
5278 return Context.getLValueReferenceType(PointeeType, Record[1]);
5279 }
5280
5281 case TYPE_RVALUE_REFERENCE: {
5282 if (Record.size() != 1) {
5283 Error("Incorrect encoding of rvalue reference type");
5284 return QualType();
5285 }
5286 QualType PointeeType = readType(*Loc.F, Record, Idx);
5287 return Context.getRValueReferenceType(PointeeType);
5288 }
5289
5290 case TYPE_MEMBER_POINTER: {
5291 if (Record.size() != 2) {
5292 Error("Incorrect encoding of member pointer type");
5293 return QualType();
5294 }
5295 QualType PointeeType = readType(*Loc.F, Record, Idx);
5296 QualType ClassType = readType(*Loc.F, Record, Idx);
5297 if (PointeeType.isNull() || ClassType.isNull())
5298 return QualType();
5299
5300 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
5301 }
5302
5303 case TYPE_CONSTANT_ARRAY: {
5304 QualType ElementType = readType(*Loc.F, Record, Idx);
5305 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
5306 unsigned IndexTypeQuals = Record[2];
5307 unsigned Idx = 3;
5308 llvm::APInt Size = ReadAPInt(Record, Idx);
5309 return Context.getConstantArrayType(ElementType, Size,
5310 ASM, IndexTypeQuals);
5311 }
5312
5313 case TYPE_INCOMPLETE_ARRAY: {
5314 QualType ElementType = readType(*Loc.F, Record, Idx);
5315 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
5316 unsigned IndexTypeQuals = Record[2];
5317 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
5318 }
5319
5320 case TYPE_VARIABLE_ARRAY: {
5321 QualType ElementType = readType(*Loc.F, Record, Idx);
5322 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
5323 unsigned IndexTypeQuals = Record[2];
5324 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
5325 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
5326 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
5327 ASM, IndexTypeQuals,
5328 SourceRange(LBLoc, RBLoc));
5329 }
5330
5331 case TYPE_VECTOR: {
5332 if (Record.size() != 3) {
5333 Error("incorrect encoding of vector type in AST file");
5334 return QualType();
5335 }
5336
5337 QualType ElementType = readType(*Loc.F, Record, Idx);
5338 unsigned NumElements = Record[1];
5339 unsigned VecKind = Record[2];
5340 return Context.getVectorType(ElementType, NumElements,
5341 (VectorType::VectorKind)VecKind);
5342 }
5343
5344 case TYPE_EXT_VECTOR: {
5345 if (Record.size() != 3) {
5346 Error("incorrect encoding of extended vector type in AST file");
5347 return QualType();
5348 }
5349
5350 QualType ElementType = readType(*Loc.F, Record, Idx);
5351 unsigned NumElements = Record[1];
5352 return Context.getExtVectorType(ElementType, NumElements);
5353 }
5354
5355 case TYPE_FUNCTION_NO_PROTO: {
5356 if (Record.size() != 6) {
5357 Error("incorrect encoding of no-proto function type");
5358 return QualType();
5359 }
5360 QualType ResultType = readType(*Loc.F, Record, Idx);
5361 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
5362 (CallingConv)Record[4], Record[5]);
5363 return Context.getFunctionNoProtoType(ResultType, Info);
5364 }
5365
5366 case TYPE_FUNCTION_PROTO: {
5367 QualType ResultType = readType(*Loc.F, Record, Idx);
5368
5369 FunctionProtoType::ExtProtoInfo EPI;
5370 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
5371 /*hasregparm*/ Record[2],
5372 /*regparm*/ Record[3],
5373 static_cast<CallingConv>(Record[4]),
5374 /*produces*/ Record[5]);
5375
5376 unsigned Idx = 6;
Guy Benyei11169dd2012-12-18 14:30:41 +00005377
5378 EPI.Variadic = Record[Idx++];
5379 EPI.HasTrailingReturn = Record[Idx++];
5380 EPI.TypeQuals = Record[Idx++];
5381 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
Richard Smith564417a2014-03-20 21:47:22 +00005382 SmallVector<QualType, 8> ExceptionStorage;
Richard Smith8acb4282014-07-31 21:57:55 +00005383 readExceptionSpec(*Loc.F, ExceptionStorage, EPI.ExceptionSpec, Record, Idx);
Richard Smith01b2cb42014-07-26 06:37:51 +00005384
5385 unsigned NumParams = Record[Idx++];
5386 SmallVector<QualType, 16> ParamTypes;
5387 for (unsigned I = 0; I != NumParams; ++I)
5388 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
5389
Jordan Rose5c382722013-03-08 21:51:21 +00005390 return Context.getFunctionType(ResultType, ParamTypes, EPI);
Guy Benyei11169dd2012-12-18 14:30:41 +00005391 }
5392
5393 case TYPE_UNRESOLVED_USING: {
5394 unsigned Idx = 0;
5395 return Context.getTypeDeclType(
5396 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
5397 }
5398
5399 case TYPE_TYPEDEF: {
5400 if (Record.size() != 2) {
5401 Error("incorrect encoding of typedef type");
5402 return QualType();
5403 }
5404 unsigned Idx = 0;
5405 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
5406 QualType Canonical = readType(*Loc.F, Record, Idx);
5407 if (!Canonical.isNull())
5408 Canonical = Context.getCanonicalType(Canonical);
5409 return Context.getTypedefType(Decl, Canonical);
5410 }
5411
5412 case TYPE_TYPEOF_EXPR:
5413 return Context.getTypeOfExprType(ReadExpr(*Loc.F));
5414
5415 case TYPE_TYPEOF: {
5416 if (Record.size() != 1) {
5417 Error("incorrect encoding of typeof(type) in AST file");
5418 return QualType();
5419 }
5420 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
5421 return Context.getTypeOfType(UnderlyingType);
5422 }
5423
5424 case TYPE_DECLTYPE: {
5425 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
5426 return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
5427 }
5428
5429 case TYPE_UNARY_TRANSFORM: {
5430 QualType BaseType = readType(*Loc.F, Record, Idx);
5431 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
5432 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
5433 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
5434 }
5435
Richard Smith74aeef52013-04-26 16:15:35 +00005436 case TYPE_AUTO: {
5437 QualType Deduced = readType(*Loc.F, Record, Idx);
Richard Smithe301ba22015-11-11 02:02:15 +00005438 AutoTypeKeyword Keyword = (AutoTypeKeyword)Record[Idx++];
Richard Smith27d807c2013-04-30 13:56:41 +00005439 bool IsDependent = Deduced.isNull() ? Record[Idx++] : false;
Richard Smithe301ba22015-11-11 02:02:15 +00005440 return Context.getAutoType(Deduced, Keyword, IsDependent);
Richard Smith74aeef52013-04-26 16:15:35 +00005441 }
Guy Benyei11169dd2012-12-18 14:30:41 +00005442
5443 case TYPE_RECORD: {
5444 if (Record.size() != 2) {
5445 Error("incorrect encoding of record type");
5446 return QualType();
5447 }
5448 unsigned Idx = 0;
5449 bool IsDependent = Record[Idx++];
5450 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
5451 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
5452 QualType T = Context.getRecordType(RD);
5453 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
5454 return T;
5455 }
5456
5457 case TYPE_ENUM: {
5458 if (Record.size() != 2) {
5459 Error("incorrect encoding of enum type");
5460 return QualType();
5461 }
5462 unsigned Idx = 0;
5463 bool IsDependent = Record[Idx++];
5464 QualType T
5465 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
5466 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
5467 return T;
5468 }
5469
5470 case TYPE_ATTRIBUTED: {
5471 if (Record.size() != 3) {
5472 Error("incorrect encoding of attributed type");
5473 return QualType();
5474 }
5475 QualType modifiedType = readType(*Loc.F, Record, Idx);
5476 QualType equivalentType = readType(*Loc.F, Record, Idx);
5477 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
5478 return Context.getAttributedType(kind, modifiedType, equivalentType);
5479 }
5480
5481 case TYPE_PAREN: {
5482 if (Record.size() != 1) {
5483 Error("incorrect encoding of paren type");
5484 return QualType();
5485 }
5486 QualType InnerType = readType(*Loc.F, Record, Idx);
5487 return Context.getParenType(InnerType);
5488 }
5489
5490 case TYPE_PACK_EXPANSION: {
5491 if (Record.size() != 2) {
5492 Error("incorrect encoding of pack expansion type");
5493 return QualType();
5494 }
5495 QualType Pattern = readType(*Loc.F, Record, Idx);
5496 if (Pattern.isNull())
5497 return QualType();
David Blaikie05785d12013-02-20 22:23:23 +00005498 Optional<unsigned> NumExpansions;
Guy Benyei11169dd2012-12-18 14:30:41 +00005499 if (Record[1])
5500 NumExpansions = Record[1] - 1;
5501 return Context.getPackExpansionType(Pattern, NumExpansions);
5502 }
5503
5504 case TYPE_ELABORATED: {
5505 unsigned Idx = 0;
5506 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
5507 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
5508 QualType NamedType = readType(*Loc.F, Record, Idx);
5509 return Context.getElaboratedType(Keyword, NNS, NamedType);
5510 }
5511
5512 case TYPE_OBJC_INTERFACE: {
5513 unsigned Idx = 0;
5514 ObjCInterfaceDecl *ItfD
5515 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
5516 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
5517 }
5518
5519 case TYPE_OBJC_OBJECT: {
5520 unsigned Idx = 0;
5521 QualType Base = readType(*Loc.F, Record, Idx);
Douglas Gregore9d95f12015-07-07 03:57:35 +00005522 unsigned NumTypeArgs = Record[Idx++];
5523 SmallVector<QualType, 4> TypeArgs;
5524 for (unsigned I = 0; I != NumTypeArgs; ++I)
5525 TypeArgs.push_back(readType(*Loc.F, Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00005526 unsigned NumProtos = Record[Idx++];
5527 SmallVector<ObjCProtocolDecl*, 4> Protos;
5528 for (unsigned I = 0; I != NumProtos; ++I)
5529 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
Douglas Gregorab209d82015-07-07 03:58:42 +00005530 bool IsKindOf = Record[Idx++];
5531 return Context.getObjCObjectType(Base, TypeArgs, Protos, IsKindOf);
Guy Benyei11169dd2012-12-18 14:30:41 +00005532 }
5533
5534 case TYPE_OBJC_OBJECT_POINTER: {
5535 unsigned Idx = 0;
5536 QualType Pointee = readType(*Loc.F, Record, Idx);
5537 return Context.getObjCObjectPointerType(Pointee);
5538 }
5539
5540 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
5541 unsigned Idx = 0;
5542 QualType Parm = readType(*Loc.F, Record, Idx);
5543 QualType Replacement = readType(*Loc.F, Record, Idx);
Stephan Tolksdorfe96f8b32014-03-15 10:23:27 +00005544 return Context.getSubstTemplateTypeParmType(
5545 cast<TemplateTypeParmType>(Parm),
5546 Context.getCanonicalType(Replacement));
Guy Benyei11169dd2012-12-18 14:30:41 +00005547 }
5548
5549 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
5550 unsigned Idx = 0;
5551 QualType Parm = readType(*Loc.F, Record, Idx);
5552 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
5553 return Context.getSubstTemplateTypeParmPackType(
5554 cast<TemplateTypeParmType>(Parm),
5555 ArgPack);
5556 }
5557
5558 case TYPE_INJECTED_CLASS_NAME: {
5559 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
5560 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
5561 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
5562 // for AST reading, too much interdependencies.
Richard Smith6377f8f2014-10-21 21:15:18 +00005563 const Type *T = nullptr;
5564 for (auto *DI = D; DI; DI = DI->getPreviousDecl()) {
5565 if (const Type *Existing = DI->getTypeForDecl()) {
5566 T = Existing;
5567 break;
5568 }
5569 }
5570 if (!T) {
Richard Smithf17fdbd2014-04-24 02:25:27 +00005571 T = new (Context, TypeAlignment) InjectedClassNameType(D, TST);
Richard Smith6377f8f2014-10-21 21:15:18 +00005572 for (auto *DI = D; DI; DI = DI->getPreviousDecl())
5573 DI->setTypeForDecl(T);
5574 }
Richard Smithf17fdbd2014-04-24 02:25:27 +00005575 return QualType(T, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00005576 }
5577
5578 case TYPE_TEMPLATE_TYPE_PARM: {
5579 unsigned Idx = 0;
5580 unsigned Depth = Record[Idx++];
5581 unsigned Index = Record[Idx++];
5582 bool Pack = Record[Idx++];
5583 TemplateTypeParmDecl *D
5584 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
5585 return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
5586 }
5587
5588 case TYPE_DEPENDENT_NAME: {
5589 unsigned Idx = 0;
5590 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
5591 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Richard Smithbdf2d932015-07-30 03:37:16 +00005592 const IdentifierInfo *Name = GetIdentifierInfo(*Loc.F, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00005593 QualType Canon = readType(*Loc.F, Record, Idx);
5594 if (!Canon.isNull())
5595 Canon = Context.getCanonicalType(Canon);
5596 return Context.getDependentNameType(Keyword, NNS, Name, Canon);
5597 }
5598
5599 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
5600 unsigned Idx = 0;
5601 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
5602 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Richard Smithbdf2d932015-07-30 03:37:16 +00005603 const IdentifierInfo *Name = GetIdentifierInfo(*Loc.F, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00005604 unsigned NumArgs = Record[Idx++];
5605 SmallVector<TemplateArgument, 8> Args;
5606 Args.reserve(NumArgs);
5607 while (NumArgs--)
5608 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
5609 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
5610 Args.size(), Args.data());
5611 }
5612
5613 case TYPE_DEPENDENT_SIZED_ARRAY: {
5614 unsigned Idx = 0;
5615
5616 // ArrayType
5617 QualType ElementType = readType(*Loc.F, Record, Idx);
5618 ArrayType::ArraySizeModifier ASM
5619 = (ArrayType::ArraySizeModifier)Record[Idx++];
5620 unsigned IndexTypeQuals = Record[Idx++];
5621
5622 // DependentSizedArrayType
5623 Expr *NumElts = ReadExpr(*Loc.F);
5624 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
5625
5626 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
5627 IndexTypeQuals, Brackets);
5628 }
5629
5630 case TYPE_TEMPLATE_SPECIALIZATION: {
5631 unsigned Idx = 0;
5632 bool IsDependent = Record[Idx++];
5633 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
5634 SmallVector<TemplateArgument, 8> Args;
5635 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
5636 QualType Underlying = readType(*Loc.F, Record, Idx);
5637 QualType T;
5638 if (Underlying.isNull())
5639 T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(),
5640 Args.size());
5641 else
5642 T = Context.getTemplateSpecializationType(Name, Args.data(),
5643 Args.size(), Underlying);
5644 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
5645 return T;
5646 }
5647
5648 case TYPE_ATOMIC: {
5649 if (Record.size() != 1) {
5650 Error("Incorrect encoding of atomic type");
5651 return QualType();
5652 }
5653 QualType ValueType = readType(*Loc.F, Record, Idx);
5654 return Context.getAtomicType(ValueType);
5655 }
Xiuli Pan9c14e282016-01-09 12:53:17 +00005656
5657 case TYPE_PIPE: {
5658 if (Record.size() != 1) {
5659 Error("Incorrect encoding of pipe type");
5660 return QualType();
5661 }
5662
5663 // Reading the pipe element type.
5664 QualType ElementType = readType(*Loc.F, Record, Idx);
5665 return Context.getPipeType(ElementType);
5666 }
Guy Benyei11169dd2012-12-18 14:30:41 +00005667 }
5668 llvm_unreachable("Invalid TypeCode!");
5669}
5670
Richard Smith564417a2014-03-20 21:47:22 +00005671void ASTReader::readExceptionSpec(ModuleFile &ModuleFile,
5672 SmallVectorImpl<QualType> &Exceptions,
Richard Smith8acb4282014-07-31 21:57:55 +00005673 FunctionProtoType::ExceptionSpecInfo &ESI,
Richard Smith564417a2014-03-20 21:47:22 +00005674 const RecordData &Record, unsigned &Idx) {
5675 ExceptionSpecificationType EST =
5676 static_cast<ExceptionSpecificationType>(Record[Idx++]);
Richard Smith8acb4282014-07-31 21:57:55 +00005677 ESI.Type = EST;
Richard Smith564417a2014-03-20 21:47:22 +00005678 if (EST == EST_Dynamic) {
Richard Smith8acb4282014-07-31 21:57:55 +00005679 for (unsigned I = 0, N = Record[Idx++]; I != N; ++I)
Richard Smith564417a2014-03-20 21:47:22 +00005680 Exceptions.push_back(readType(ModuleFile, Record, Idx));
Richard Smith8acb4282014-07-31 21:57:55 +00005681 ESI.Exceptions = Exceptions;
Richard Smith564417a2014-03-20 21:47:22 +00005682 } else if (EST == EST_ComputedNoexcept) {
Richard Smith8acb4282014-07-31 21:57:55 +00005683 ESI.NoexceptExpr = ReadExpr(ModuleFile);
Richard Smith564417a2014-03-20 21:47:22 +00005684 } else if (EST == EST_Uninstantiated) {
Richard Smith8acb4282014-07-31 21:57:55 +00005685 ESI.SourceDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
5686 ESI.SourceTemplate = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
Richard Smith564417a2014-03-20 21:47:22 +00005687 } else if (EST == EST_Unevaluated) {
Richard Smith8acb4282014-07-31 21:57:55 +00005688 ESI.SourceDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
Richard Smith564417a2014-03-20 21:47:22 +00005689 }
5690}
5691
Guy Benyei11169dd2012-12-18 14:30:41 +00005692class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
5693 ASTReader &Reader;
5694 ModuleFile &F;
5695 const ASTReader::RecordData &Record;
5696 unsigned &Idx;
5697
5698 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
5699 unsigned &I) {
5700 return Reader.ReadSourceLocation(F, R, I);
5701 }
5702
5703 template<typename T>
5704 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
5705 return Reader.ReadDeclAs<T>(F, Record, Idx);
5706 }
5707
5708public:
5709 TypeLocReader(ASTReader &Reader, ModuleFile &F,
5710 const ASTReader::RecordData &Record, unsigned &Idx)
5711 : Reader(Reader), F(F), Record(Record), Idx(Idx)
5712 { }
5713
5714 // We want compile-time assurance that we've enumerated all of
5715 // these, so unfortunately we have to declare them first, then
5716 // define them out-of-line.
5717#define ABSTRACT_TYPELOC(CLASS, PARENT)
5718#define TYPELOC(CLASS, PARENT) \
5719 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
5720#include "clang/AST/TypeLocNodes.def"
5721
5722 void VisitFunctionTypeLoc(FunctionTypeLoc);
5723 void VisitArrayTypeLoc(ArrayTypeLoc);
5724};
5725
5726void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
5727 // nothing to do
5728}
5729void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
5730 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
5731 if (TL.needsExtraLocalData()) {
5732 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
5733 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
5734 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
5735 TL.setModeAttr(Record[Idx++]);
5736 }
5737}
5738void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
5739 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5740}
5741void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
5742 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5743}
Reid Kleckner8a365022013-06-24 17:51:48 +00005744void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
5745 // nothing to do
5746}
Reid Kleckner0503a872013-12-05 01:23:43 +00005747void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
5748 // nothing to do
5749}
Guy Benyei11169dd2012-12-18 14:30:41 +00005750void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
5751 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
5752}
5753void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
5754 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
5755}
5756void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
5757 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
5758}
5759void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
5760 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5761 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5762}
5763void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
5764 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
5765 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
5766 if (Record[Idx++])
5767 TL.setSizeExpr(Reader.ReadExpr(F));
5768 else
Craig Toppera13603a2014-05-22 05:54:18 +00005769 TL.setSizeExpr(nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00005770}
5771void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
5772 VisitArrayTypeLoc(TL);
5773}
5774void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
5775 VisitArrayTypeLoc(TL);
5776}
5777void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
5778 VisitArrayTypeLoc(TL);
5779}
5780void TypeLocReader::VisitDependentSizedArrayTypeLoc(
5781 DependentSizedArrayTypeLoc TL) {
5782 VisitArrayTypeLoc(TL);
5783}
5784void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
5785 DependentSizedExtVectorTypeLoc TL) {
5786 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5787}
5788void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
5789 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5790}
5791void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
5792 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5793}
5794void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
5795 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
5796 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5797 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5798 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
Alp Tokerb3fd5cf2014-01-21 00:32:38 +00005799 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
5800 TL.setParam(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00005801 }
5802}
5803void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
5804 VisitFunctionTypeLoc(TL);
5805}
5806void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
5807 VisitFunctionTypeLoc(TL);
5808}
5809void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
5810 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5811}
5812void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
5813 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5814}
5815void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
5816 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
5817 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5818 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5819}
5820void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
5821 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
5822 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5823 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5824 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5825}
5826void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
5827 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5828}
5829void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
5830 TL.setKWLoc(ReadSourceLocation(Record, Idx));
5831 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5832 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5833 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5834}
5835void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
5836 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5837}
5838void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
5839 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5840}
5841void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
5842 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5843}
5844void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
5845 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
5846 if (TL.hasAttrOperand()) {
5847 SourceRange range;
5848 range.setBegin(ReadSourceLocation(Record, Idx));
5849 range.setEnd(ReadSourceLocation(Record, Idx));
5850 TL.setAttrOperandParensRange(range);
5851 }
5852 if (TL.hasAttrExprOperand()) {
5853 if (Record[Idx++])
5854 TL.setAttrExprOperand(Reader.ReadExpr(F));
5855 else
Craig Toppera13603a2014-05-22 05:54:18 +00005856 TL.setAttrExprOperand(nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00005857 } else if (TL.hasAttrEnumOperand())
5858 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
5859}
5860void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
5861 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5862}
5863void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
5864 SubstTemplateTypeParmTypeLoc TL) {
5865 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5866}
5867void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
5868 SubstTemplateTypeParmPackTypeLoc TL) {
5869 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5870}
5871void TypeLocReader::VisitTemplateSpecializationTypeLoc(
5872 TemplateSpecializationTypeLoc TL) {
5873 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
5874 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
5875 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5876 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5877 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
5878 TL.setArgLocInfo(i,
5879 Reader.GetTemplateArgumentLocInfo(F,
5880 TL.getTypePtr()->getArg(i).getKind(),
5881 Record, Idx));
5882}
5883void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
5884 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5885 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5886}
5887void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
5888 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5889 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5890}
5891void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
5892 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5893}
5894void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
5895 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5896 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5897 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5898}
5899void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
5900 DependentTemplateSpecializationTypeLoc TL) {
5901 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5902 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5903 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
5904 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
5905 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5906 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5907 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
5908 TL.setArgLocInfo(I,
5909 Reader.GetTemplateArgumentLocInfo(F,
5910 TL.getTypePtr()->getArg(I).getKind(),
5911 Record, Idx));
5912}
5913void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
5914 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
5915}
5916void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
5917 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5918}
5919void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
5920 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Douglas Gregore9d95f12015-07-07 03:57:35 +00005921 TL.setTypeArgsLAngleLoc(ReadSourceLocation(Record, Idx));
5922 TL.setTypeArgsRAngleLoc(ReadSourceLocation(Record, Idx));
5923 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
5924 TL.setTypeArgTInfo(i, Reader.GetTypeSourceInfo(F, Record, Idx));
5925 TL.setProtocolLAngleLoc(ReadSourceLocation(Record, Idx));
5926 TL.setProtocolRAngleLoc(ReadSourceLocation(Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00005927 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
5928 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
5929}
5930void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
5931 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5932}
5933void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
5934 TL.setKWLoc(ReadSourceLocation(Record, Idx));
5935 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5936 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5937}
Xiuli Pan9c14e282016-01-09 12:53:17 +00005938void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
5939 TL.setKWLoc(ReadSourceLocation(Record, Idx));
5940}
Guy Benyei11169dd2012-12-18 14:30:41 +00005941
5942TypeSourceInfo *ASTReader::GetTypeSourceInfo(ModuleFile &F,
5943 const RecordData &Record,
5944 unsigned &Idx) {
5945 QualType InfoTy = readType(F, Record, Idx);
5946 if (InfoTy.isNull())
Craig Toppera13603a2014-05-22 05:54:18 +00005947 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00005948
5949 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
5950 TypeLocReader TLR(*this, F, Record, Idx);
5951 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
5952 TLR.Visit(TL);
5953 return TInfo;
5954}
5955
5956QualType ASTReader::GetType(TypeID ID) {
5957 unsigned FastQuals = ID & Qualifiers::FastMask;
5958 unsigned Index = ID >> Qualifiers::FastWidth;
5959
5960 if (Index < NUM_PREDEF_TYPE_IDS) {
5961 QualType T;
5962 switch ((PredefinedTypeIDs)Index) {
Alexey Baderbdf7c842015-09-15 12:18:29 +00005963 case PREDEF_TYPE_NULL_ID:
5964 return QualType();
5965 case PREDEF_TYPE_VOID_ID:
5966 T = Context.VoidTy;
5967 break;
5968 case PREDEF_TYPE_BOOL_ID:
5969 T = Context.BoolTy;
5970 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00005971
5972 case PREDEF_TYPE_CHAR_U_ID:
5973 case PREDEF_TYPE_CHAR_S_ID:
5974 // FIXME: Check that the signedness of CharTy is correct!
5975 T = Context.CharTy;
5976 break;
5977
Alexey Baderbdf7c842015-09-15 12:18:29 +00005978 case PREDEF_TYPE_UCHAR_ID:
5979 T = Context.UnsignedCharTy;
5980 break;
5981 case PREDEF_TYPE_USHORT_ID:
5982 T = Context.UnsignedShortTy;
5983 break;
5984 case PREDEF_TYPE_UINT_ID:
5985 T = Context.UnsignedIntTy;
5986 break;
5987 case PREDEF_TYPE_ULONG_ID:
5988 T = Context.UnsignedLongTy;
5989 break;
5990 case PREDEF_TYPE_ULONGLONG_ID:
5991 T = Context.UnsignedLongLongTy;
5992 break;
5993 case PREDEF_TYPE_UINT128_ID:
5994 T = Context.UnsignedInt128Ty;
5995 break;
5996 case PREDEF_TYPE_SCHAR_ID:
5997 T = Context.SignedCharTy;
5998 break;
5999 case PREDEF_TYPE_WCHAR_ID:
6000 T = Context.WCharTy;
6001 break;
6002 case PREDEF_TYPE_SHORT_ID:
6003 T = Context.ShortTy;
6004 break;
6005 case PREDEF_TYPE_INT_ID:
6006 T = Context.IntTy;
6007 break;
6008 case PREDEF_TYPE_LONG_ID:
6009 T = Context.LongTy;
6010 break;
6011 case PREDEF_TYPE_LONGLONG_ID:
6012 T = Context.LongLongTy;
6013 break;
6014 case PREDEF_TYPE_INT128_ID:
6015 T = Context.Int128Ty;
6016 break;
6017 case PREDEF_TYPE_HALF_ID:
6018 T = Context.HalfTy;
6019 break;
6020 case PREDEF_TYPE_FLOAT_ID:
6021 T = Context.FloatTy;
6022 break;
6023 case PREDEF_TYPE_DOUBLE_ID:
6024 T = Context.DoubleTy;
6025 break;
6026 case PREDEF_TYPE_LONGDOUBLE_ID:
6027 T = Context.LongDoubleTy;
6028 break;
6029 case PREDEF_TYPE_OVERLOAD_ID:
6030 T = Context.OverloadTy;
6031 break;
6032 case PREDEF_TYPE_BOUND_MEMBER:
6033 T = Context.BoundMemberTy;
6034 break;
6035 case PREDEF_TYPE_PSEUDO_OBJECT:
6036 T = Context.PseudoObjectTy;
6037 break;
6038 case PREDEF_TYPE_DEPENDENT_ID:
6039 T = Context.DependentTy;
6040 break;
6041 case PREDEF_TYPE_UNKNOWN_ANY:
6042 T = Context.UnknownAnyTy;
6043 break;
6044 case PREDEF_TYPE_NULLPTR_ID:
6045 T = Context.NullPtrTy;
6046 break;
6047 case PREDEF_TYPE_CHAR16_ID:
6048 T = Context.Char16Ty;
6049 break;
6050 case PREDEF_TYPE_CHAR32_ID:
6051 T = Context.Char32Ty;
6052 break;
6053 case PREDEF_TYPE_OBJC_ID:
6054 T = Context.ObjCBuiltinIdTy;
6055 break;
6056 case PREDEF_TYPE_OBJC_CLASS:
6057 T = Context.ObjCBuiltinClassTy;
6058 break;
6059 case PREDEF_TYPE_OBJC_SEL:
6060 T = Context.ObjCBuiltinSelTy;
6061 break;
6062 case PREDEF_TYPE_IMAGE1D_ID:
6063 T = Context.OCLImage1dTy;
6064 break;
6065 case PREDEF_TYPE_IMAGE1D_ARR_ID:
6066 T = Context.OCLImage1dArrayTy;
6067 break;
6068 case PREDEF_TYPE_IMAGE1D_BUFF_ID:
6069 T = Context.OCLImage1dBufferTy;
6070 break;
6071 case PREDEF_TYPE_IMAGE2D_ID:
6072 T = Context.OCLImage2dTy;
6073 break;
6074 case PREDEF_TYPE_IMAGE2D_ARR_ID:
6075 T = Context.OCLImage2dArrayTy;
6076 break;
Alexey Bader9c8453f2015-09-15 11:18:52 +00006077 case PREDEF_TYPE_IMAGE2D_DEP_ID:
6078 T = Context.OCLImage2dDepthTy;
6079 break;
6080 case PREDEF_TYPE_IMAGE2D_ARR_DEP_ID:
6081 T = Context.OCLImage2dArrayDepthTy;
6082 break;
6083 case PREDEF_TYPE_IMAGE2D_MSAA_ID:
6084 T = Context.OCLImage2dMSAATy;
6085 break;
6086 case PREDEF_TYPE_IMAGE2D_ARR_MSAA_ID:
6087 T = Context.OCLImage2dArrayMSAATy;
6088 break;
6089 case PREDEF_TYPE_IMAGE2D_MSAA_DEP_ID:
6090 T = Context.OCLImage2dMSAADepthTy;
6091 break;
6092 case PREDEF_TYPE_IMAGE2D_ARR_MSAA_DEPTH_ID:
6093 T = Context.OCLImage2dArrayMSAADepthTy;
6094 break;
Alexey Baderbdf7c842015-09-15 12:18:29 +00006095 case PREDEF_TYPE_IMAGE3D_ID:
6096 T = Context.OCLImage3dTy;
6097 break;
6098 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 +00006301uint64_t ASTReader::ReadCXXCtorInitializersRef(ModuleFile &M,
6302 const RecordData &Record,
6303 unsigned &Idx) {
6304 if (Idx >= Record.size() || Record[Idx] > M.LocalNumCXXCtorInitializers) {
6305 Error("malformed AST file: missing C++ ctor initializers");
6306 return 0;
6307 }
6308
6309 unsigned LocalID = Record[Idx++];
6310 return getGlobalBitOffset(M, M.CXXCtorInitializersOffsets[LocalID - 1]);
6311}
6312
6313CXXCtorInitializer **
6314ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
6315 RecordLocation Loc = getLocalBitOffset(Offset);
6316 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
6317 SavedStreamPosition SavedPosition(Cursor);
6318 Cursor.JumpToBit(Loc.Offset);
6319 ReadingKindTracker ReadingKind(Read_Decl, *this);
6320
6321 RecordData Record;
6322 unsigned Code = Cursor.ReadCode();
6323 unsigned RecCode = Cursor.readRecord(Code, Record);
6324 if (RecCode != DECL_CXX_CTOR_INITIALIZERS) {
6325 Error("malformed AST file: missing C++ ctor initializers");
6326 return nullptr;
6327 }
6328
6329 unsigned Idx = 0;
6330 return ReadCXXCtorInitializers(*Loc.F, Record, Idx);
6331}
6332
Richard Smithcd45dbc2014-04-19 03:48:30 +00006333uint64_t ASTReader::readCXXBaseSpecifiers(ModuleFile &M,
6334 const RecordData &Record,
6335 unsigned &Idx) {
6336 if (Idx >= Record.size() || Record[Idx] > M.LocalNumCXXBaseSpecifiers) {
6337 Error("malformed AST file: missing C++ base specifier");
Guy Benyei11169dd2012-12-18 14:30:41 +00006338 return 0;
Richard Smithcd45dbc2014-04-19 03:48:30 +00006339 }
6340
Guy Benyei11169dd2012-12-18 14:30:41 +00006341 unsigned LocalID = Record[Idx++];
6342 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
6343}
6344
6345CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
6346 RecordLocation Loc = getLocalBitOffset(Offset);
Chris Lattner7fb3bef2013-01-20 00:56:42 +00006347 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00006348 SavedStreamPosition SavedPosition(Cursor);
6349 Cursor.JumpToBit(Loc.Offset);
6350 ReadingKindTracker ReadingKind(Read_Decl, *this);
6351 RecordData Record;
6352 unsigned Code = Cursor.ReadCode();
Chris Lattner0e6c9402013-01-20 02:38:54 +00006353 unsigned RecCode = Cursor.readRecord(Code, Record);
Guy Benyei11169dd2012-12-18 14:30:41 +00006354 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00006355 Error("malformed AST file: missing C++ base specifiers");
Craig Toppera13603a2014-05-22 05:54:18 +00006356 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00006357 }
6358
6359 unsigned Idx = 0;
6360 unsigned NumBases = Record[Idx++];
6361 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
6362 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
6363 for (unsigned I = 0; I != NumBases; ++I)
6364 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
6365 return Bases;
6366}
6367
6368serialization::DeclID
6369ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
6370 if (LocalID < NUM_PREDEF_DECL_IDS)
6371 return LocalID;
6372
6373 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6374 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
6375 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
6376
6377 return LocalID + I->second;
6378}
6379
6380bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
6381 ModuleFile &M) const {
Richard Smithfe620d22015-03-05 23:24:12 +00006382 // Predefined decls aren't from any module.
6383 if (ID < NUM_PREDEF_DECL_IDS)
6384 return false;
6385
Richard Smithbcda1a92015-07-12 23:51:20 +00006386 return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID &&
6387 ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls;
Guy Benyei11169dd2012-12-18 14:30:41 +00006388}
6389
Douglas Gregor9f782892013-01-21 15:25:38 +00006390ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
Guy Benyei11169dd2012-12-18 14:30:41 +00006391 if (!D->isFromASTFile())
Craig Toppera13603a2014-05-22 05:54:18 +00006392 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00006393 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
6394 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
6395 return I->second;
6396}
6397
6398SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
6399 if (ID < NUM_PREDEF_DECL_IDS)
6400 return SourceLocation();
Richard Smithcd45dbc2014-04-19 03:48:30 +00006401
Guy Benyei11169dd2012-12-18 14:30:41 +00006402 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
6403
6404 if (Index > DeclsLoaded.size()) {
6405 Error("declaration ID out-of-range for AST file");
6406 return SourceLocation();
6407 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00006408
Guy Benyei11169dd2012-12-18 14:30:41 +00006409 if (Decl *D = DeclsLoaded[Index])
6410 return D->getLocation();
6411
6412 unsigned RawLocation = 0;
6413 RecordLocation Rec = DeclCursorForID(ID, RawLocation);
6414 return ReadSourceLocation(*Rec.F, RawLocation);
6415}
6416
Richard Smithfe620d22015-03-05 23:24:12 +00006417static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) {
6418 switch (ID) {
6419 case PREDEF_DECL_NULL_ID:
6420 return nullptr;
6421
6422 case PREDEF_DECL_TRANSLATION_UNIT_ID:
6423 return Context.getTranslationUnitDecl();
6424
6425 case PREDEF_DECL_OBJC_ID_ID:
6426 return Context.getObjCIdDecl();
6427
6428 case PREDEF_DECL_OBJC_SEL_ID:
6429 return Context.getObjCSelDecl();
6430
6431 case PREDEF_DECL_OBJC_CLASS_ID:
6432 return Context.getObjCClassDecl();
6433
6434 case PREDEF_DECL_OBJC_PROTOCOL_ID:
6435 return Context.getObjCProtocolDecl();
6436
6437 case PREDEF_DECL_INT_128_ID:
6438 return Context.getInt128Decl();
6439
6440 case PREDEF_DECL_UNSIGNED_INT_128_ID:
6441 return Context.getUInt128Decl();
6442
6443 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
6444 return Context.getObjCInstanceTypeDecl();
6445
6446 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
6447 return Context.getBuiltinVaListDecl();
Richard Smithf19e1272015-03-07 00:04:49 +00006448
Richard Smith9b88a4c2015-07-27 05:40:23 +00006449 case PREDEF_DECL_VA_LIST_TAG:
6450 return Context.getVaListTagDecl();
6451
Charles Davisc7d5c942015-09-17 20:55:33 +00006452 case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
6453 return Context.getBuiltinMSVaListDecl();
6454
Richard Smithf19e1272015-03-07 00:04:49 +00006455 case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
6456 return Context.getExternCContextDecl();
David Majnemerd9b1a4f2015-11-04 03:40:30 +00006457
6458 case PREDEF_DECL_MAKE_INTEGER_SEQ_ID:
6459 return Context.getMakeIntegerSeqDecl();
Quentin Colombet043406b2016-02-03 22:41:00 +00006460
6461 case PREDEF_DECL_CF_CONSTANT_STRING_ID:
6462 return Context.getCFConstantStringDecl();
Ben Langmuirf5416742016-02-04 00:55:24 +00006463
6464 case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
6465 return Context.getCFConstantStringTagDecl();
Richard Smithfe620d22015-03-05 23:24:12 +00006466 }
Yaron Keren322bdad2015-03-06 07:49:14 +00006467 llvm_unreachable("PredefinedDeclIDs unknown enum value");
Richard Smithfe620d22015-03-05 23:24:12 +00006468}
6469
Richard Smithcd45dbc2014-04-19 03:48:30 +00006470Decl *ASTReader::GetExistingDecl(DeclID ID) {
6471 if (ID < NUM_PREDEF_DECL_IDS) {
Richard Smithfe620d22015-03-05 23:24:12 +00006472 Decl *D = getPredefinedDecl(Context, (PredefinedDeclIDs)ID);
6473 if (D) {
6474 // Track that we have merged the declaration with ID \p ID into the
6475 // pre-existing predefined declaration \p D.
Richard Smith5fc18a92015-07-12 23:43:21 +00006476 auto &Merged = KeyDecls[D->getCanonicalDecl()];
Richard Smithfe620d22015-03-05 23:24:12 +00006477 if (Merged.empty())
6478 Merged.push_back(ID);
Guy Benyei11169dd2012-12-18 14:30:41 +00006479 }
Richard Smithfe620d22015-03-05 23:24:12 +00006480 return D;
Guy Benyei11169dd2012-12-18 14:30:41 +00006481 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00006482
Guy Benyei11169dd2012-12-18 14:30:41 +00006483 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
6484
6485 if (Index >= DeclsLoaded.size()) {
6486 assert(0 && "declaration ID out-of-range for AST file");
6487 Error("declaration ID out-of-range for AST file");
Craig Toppera13603a2014-05-22 05:54:18 +00006488 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00006489 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00006490
6491 return DeclsLoaded[Index];
6492}
6493
6494Decl *ASTReader::GetDecl(DeclID ID) {
6495 if (ID < NUM_PREDEF_DECL_IDS)
6496 return GetExistingDecl(ID);
6497
6498 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
6499
6500 if (Index >= DeclsLoaded.size()) {
6501 assert(0 && "declaration ID out-of-range for AST file");
6502 Error("declaration ID out-of-range for AST file");
Craig Toppera13603a2014-05-22 05:54:18 +00006503 return nullptr;
Richard Smithcd45dbc2014-04-19 03:48:30 +00006504 }
6505
Guy Benyei11169dd2012-12-18 14:30:41 +00006506 if (!DeclsLoaded[Index]) {
6507 ReadDeclRecord(ID);
6508 if (DeserializationListener)
6509 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
6510 }
6511
6512 return DeclsLoaded[Index];
6513}
6514
6515DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
6516 DeclID GlobalID) {
6517 if (GlobalID < NUM_PREDEF_DECL_IDS)
6518 return GlobalID;
6519
6520 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
6521 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
6522 ModuleFile *Owner = I->second;
6523
6524 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
6525 = M.GlobalToLocalDeclIDs.find(Owner);
6526 if (Pos == M.GlobalToLocalDeclIDs.end())
6527 return 0;
6528
6529 return GlobalID - Owner->BaseDeclID + Pos->second;
6530}
6531
6532serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
6533 const RecordData &Record,
6534 unsigned &Idx) {
6535 if (Idx >= Record.size()) {
6536 Error("Corrupted AST file");
6537 return 0;
6538 }
6539
6540 return getGlobalDeclID(F, Record[Idx++]);
6541}
6542
6543/// \brief Resolve the offset of a statement into a statement.
6544///
6545/// This operation will read a new statement from the external
6546/// source each time it is called, and is meant to be used via a
6547/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
6548Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
6549 // Switch case IDs are per Decl.
6550 ClearSwitchCaseIDs();
6551
6552 // Offset here is a global offset across the entire chain.
6553 RecordLocation Loc = getLocalBitOffset(Offset);
6554 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
6555 return ReadStmtFromStream(*Loc.F);
6556}
6557
Richard Smith3cb15722015-08-05 22:41:45 +00006558void ASTReader::FindExternalLexicalDecls(
6559 const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
6560 SmallVectorImpl<Decl *> &Decls) {
Richard Smith82f8fcd2015-08-06 22:07:25 +00006561 bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
6562
Richard Smith9ccdd932015-08-06 22:14:12 +00006563 auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
Richard Smith82f8fcd2015-08-06 22:07:25 +00006564 assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");
6565 for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
6566 auto K = (Decl::Kind)+LexicalDecls[I];
6567 if (!IsKindWeWant(K))
6568 continue;
6569
6570 auto ID = (serialization::DeclID)+LexicalDecls[I + 1];
6571
6572 // Don't add predefined declarations to the lexical context more
6573 // than once.
6574 if (ID < NUM_PREDEF_DECL_IDS) {
6575 if (PredefsVisited[ID])
6576 continue;
6577
6578 PredefsVisited[ID] = true;
6579 }
6580
6581 if (Decl *D = GetLocalDecl(*M, ID)) {
Richard Smith2317a3e2015-08-11 21:21:20 +00006582 assert(D->getKind() == K && "wrong kind for lexical decl");
Richard Smith82f8fcd2015-08-06 22:07:25 +00006583 if (!DC->isDeclInLexicalTraversal(D))
6584 Decls.push_back(D);
6585 }
6586 }
6587 };
6588
6589 if (isa<TranslationUnitDecl>(DC)) {
6590 for (auto Lexical : TULexicalDecls)
6591 Visit(Lexical.first, Lexical.second);
6592 } else {
6593 auto I = LexicalDecls.find(DC);
6594 if (I != LexicalDecls.end())
Richard Smith9c9173d2015-08-11 22:00:24 +00006595 Visit(I->second.first, I->second.second);
Richard Smith82f8fcd2015-08-06 22:07:25 +00006596 }
6597
Guy Benyei11169dd2012-12-18 14:30:41 +00006598 ++NumLexicalDeclContextsRead;
Guy Benyei11169dd2012-12-18 14:30:41 +00006599}
6600
6601namespace {
6602
6603class DeclIDComp {
6604 ASTReader &Reader;
6605 ModuleFile &Mod;
6606
6607public:
6608 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
6609
6610 bool operator()(LocalDeclID L, LocalDeclID R) const {
6611 SourceLocation LHS = getLocation(L);
6612 SourceLocation RHS = getLocation(R);
6613 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6614 }
6615
6616 bool operator()(SourceLocation LHS, LocalDeclID R) const {
6617 SourceLocation RHS = getLocation(R);
6618 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6619 }
6620
6621 bool operator()(LocalDeclID L, SourceLocation RHS) const {
6622 SourceLocation LHS = getLocation(L);
6623 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6624 }
6625
6626 SourceLocation getLocation(LocalDeclID ID) const {
6627 return Reader.getSourceManager().getFileLoc(
6628 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
6629 }
6630};
6631
Alexander Kornienkoab9db512015-06-22 23:07:51 +00006632}
Guy Benyei11169dd2012-12-18 14:30:41 +00006633
6634void ASTReader::FindFileRegionDecls(FileID File,
6635 unsigned Offset, unsigned Length,
6636 SmallVectorImpl<Decl *> &Decls) {
6637 SourceManager &SM = getSourceManager();
6638
6639 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
6640 if (I == FileDeclIDs.end())
6641 return;
6642
6643 FileDeclsInfo &DInfo = I->second;
6644 if (DInfo.Decls.empty())
6645 return;
6646
6647 SourceLocation
6648 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
6649 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
6650
6651 DeclIDComp DIDComp(*this, *DInfo.Mod);
6652 ArrayRef<serialization::LocalDeclID>::iterator
6653 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
6654 BeginLoc, DIDComp);
6655 if (BeginIt != DInfo.Decls.begin())
6656 --BeginIt;
6657
6658 // If we are pointing at a top-level decl inside an objc container, we need
6659 // to backtrack until we find it otherwise we will fail to report that the
6660 // region overlaps with an objc container.
6661 while (BeginIt != DInfo.Decls.begin() &&
6662 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
6663 ->isTopLevelDeclInObjCContainer())
6664 --BeginIt;
6665
6666 ArrayRef<serialization::LocalDeclID>::iterator
6667 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
6668 EndLoc, DIDComp);
6669 if (EndIt != DInfo.Decls.end())
6670 ++EndIt;
6671
6672 for (ArrayRef<serialization::LocalDeclID>::iterator
6673 DIt = BeginIt; DIt != EndIt; ++DIt)
6674 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
6675}
6676
Richard Smith9ce12e32013-02-07 03:30:24 +00006677bool
Guy Benyei11169dd2012-12-18 14:30:41 +00006678ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
6679 DeclarationName Name) {
Richard Smithd88a7f12015-09-01 20:35:42 +00006680 assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
Guy Benyei11169dd2012-12-18 14:30:41 +00006681 "DeclContext has no visible decls in storage");
6682 if (!Name)
Richard Smith9ce12e32013-02-07 03:30:24 +00006683 return false;
Guy Benyei11169dd2012-12-18 14:30:41 +00006684
Richard Smithd88a7f12015-09-01 20:35:42 +00006685 auto It = Lookups.find(DC);
6686 if (It == Lookups.end())
6687 return false;
6688
Richard Smith8c913ec2014-08-14 02:21:01 +00006689 Deserializing LookupResults(this);
6690
Richard Smithd88a7f12015-09-01 20:35:42 +00006691 // Load the list of declarations.
Guy Benyei11169dd2012-12-18 14:30:41 +00006692 SmallVector<NamedDecl *, 64> Decls;
Richard Smithd88a7f12015-09-01 20:35:42 +00006693 for (DeclID ID : It->second.Table.find(Name)) {
6694 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
6695 if (ND->getDeclName() == Name)
6696 Decls.push_back(ND);
6697 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00006698
Guy Benyei11169dd2012-12-18 14:30:41 +00006699 ++NumVisibleDeclContextsRead;
6700 SetExternalVisibleDeclsForName(DC, Name, Decls);
Richard Smith9ce12e32013-02-07 03:30:24 +00006701 return !Decls.empty();
Guy Benyei11169dd2012-12-18 14:30:41 +00006702}
6703
Guy Benyei11169dd2012-12-18 14:30:41 +00006704void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
6705 if (!DC->hasExternalVisibleStorage())
6706 return;
Richard Smithd88a7f12015-09-01 20:35:42 +00006707
6708 auto It = Lookups.find(DC);
6709 assert(It != Lookups.end() &&
6710 "have external visible storage but no lookup tables");
6711
Craig Topper79be4cd2013-07-05 04:33:53 +00006712 DeclsMap Decls;
Guy Benyei11169dd2012-12-18 14:30:41 +00006713
Richard Smithd88a7f12015-09-01 20:35:42 +00006714 for (DeclID ID : It->second.Table.findAll()) {
6715 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
6716 Decls[ND->getDeclName()].push_back(ND);
Guy Benyei11169dd2012-12-18 14:30:41 +00006717 }
6718
Guy Benyei11169dd2012-12-18 14:30:41 +00006719 ++NumVisibleDeclContextsRead;
6720
Craig Topper79be4cd2013-07-05 04:33:53 +00006721 for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
Guy Benyei11169dd2012-12-18 14:30:41 +00006722 SetExternalVisibleDeclsForName(DC, I->first, I->second);
6723 }
6724 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
6725}
6726
Richard Smithd88a7f12015-09-01 20:35:42 +00006727const serialization::reader::DeclContextLookupTable *
6728ASTReader::getLoadedLookupTables(DeclContext *Primary) const {
6729 auto I = Lookups.find(Primary);
6730 return I == Lookups.end() ? nullptr : &I->second;
6731}
6732
Guy Benyei11169dd2012-12-18 14:30:41 +00006733/// \brief Under non-PCH compilation the consumer receives the objc methods
6734/// before receiving the implementation, and codegen depends on this.
6735/// We simulate this by deserializing and passing to consumer the methods of the
6736/// implementation before passing the deserialized implementation decl.
6737static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
6738 ASTConsumer *Consumer) {
6739 assert(ImplD && Consumer);
6740
Aaron Ballmanaff18c02014-03-13 19:03:34 +00006741 for (auto *I : ImplD->methods())
6742 Consumer->HandleInterestingDecl(DeclGroupRef(I));
Guy Benyei11169dd2012-12-18 14:30:41 +00006743
6744 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
6745}
6746
6747void ASTReader::PassInterestingDeclsToConsumer() {
6748 assert(Consumer);
Richard Smith04d05b52014-03-23 00:27:18 +00006749
6750 if (PassingDeclsToConsumer)
6751 return;
6752
6753 // Guard variable to avoid recursively redoing the process of passing
6754 // decls to consumer.
6755 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
6756 true);
6757
Richard Smith9e2341d2015-03-23 03:25:59 +00006758 // Ensure that we've loaded all potentially-interesting declarations
6759 // that need to be eagerly loaded.
6760 for (auto ID : EagerlyDeserializedDecls)
6761 GetDecl(ID);
6762 EagerlyDeserializedDecls.clear();
6763
Guy Benyei11169dd2012-12-18 14:30:41 +00006764 while (!InterestingDecls.empty()) {
6765 Decl *D = InterestingDecls.front();
6766 InterestingDecls.pop_front();
6767
6768 PassInterestingDeclToConsumer(D);
6769 }
6770}
6771
6772void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
6773 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
6774 PassObjCImplDeclToConsumer(ImplD, Consumer);
6775 else
6776 Consumer->HandleInterestingDecl(DeclGroupRef(D));
6777}
6778
6779void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
6780 this->Consumer = Consumer;
6781
Richard Smith9e2341d2015-03-23 03:25:59 +00006782 if (Consumer)
6783 PassInterestingDeclsToConsumer();
Richard Smith7f330cd2015-03-18 01:42:29 +00006784
6785 if (DeserializationListener)
6786 DeserializationListener->ReaderInitialized(this);
Guy Benyei11169dd2012-12-18 14:30:41 +00006787}
6788
6789void ASTReader::PrintStats() {
6790 std::fprintf(stderr, "*** AST File Statistics:\n");
6791
6792 unsigned NumTypesLoaded
6793 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
6794 QualType());
6795 unsigned NumDeclsLoaded
6796 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
Craig Toppera13603a2014-05-22 05:54:18 +00006797 (Decl *)nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00006798 unsigned NumIdentifiersLoaded
6799 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
6800 IdentifiersLoaded.end(),
Craig Toppera13603a2014-05-22 05:54:18 +00006801 (IdentifierInfo *)nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00006802 unsigned NumMacrosLoaded
6803 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
6804 MacrosLoaded.end(),
Craig Toppera13603a2014-05-22 05:54:18 +00006805 (MacroInfo *)nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00006806 unsigned NumSelectorsLoaded
6807 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
6808 SelectorsLoaded.end(),
6809 Selector());
6810
6811 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
6812 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
6813 NumSLocEntriesRead, TotalNumSLocEntries,
6814 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
6815 if (!TypesLoaded.empty())
6816 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
6817 NumTypesLoaded, (unsigned)TypesLoaded.size(),
6818 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
6819 if (!DeclsLoaded.empty())
6820 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
6821 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
6822 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
6823 if (!IdentifiersLoaded.empty())
6824 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
6825 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
6826 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
6827 if (!MacrosLoaded.empty())
6828 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
6829 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
6830 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
6831 if (!SelectorsLoaded.empty())
6832 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
6833 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
6834 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
6835 if (TotalNumStatements)
6836 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
6837 NumStatementsRead, TotalNumStatements,
6838 ((float)NumStatementsRead/TotalNumStatements * 100));
6839 if (TotalNumMacros)
6840 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
6841 NumMacrosRead, TotalNumMacros,
6842 ((float)NumMacrosRead/TotalNumMacros * 100));
6843 if (TotalLexicalDeclContexts)
6844 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
6845 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
6846 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
6847 * 100));
6848 if (TotalVisibleDeclContexts)
6849 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
6850 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
6851 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
6852 * 100));
6853 if (TotalNumMethodPoolEntries) {
6854 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
6855 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
6856 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
6857 * 100));
Guy Benyei11169dd2012-12-18 14:30:41 +00006858 }
Douglas Gregorad2f7a52013-01-28 17:54:36 +00006859 if (NumMethodPoolLookups) {
6860 std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n",
6861 NumMethodPoolHits, NumMethodPoolLookups,
6862 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
6863 }
6864 if (NumMethodPoolTableLookups) {
6865 std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n",
6866 NumMethodPoolTableHits, NumMethodPoolTableLookups,
6867 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
6868 * 100.0));
6869 }
6870
Douglas Gregor00a50f72013-01-25 00:38:33 +00006871 if (NumIdentifierLookupHits) {
6872 std::fprintf(stderr,
6873 " %u / %u identifier table lookups succeeded (%f%%)\n",
6874 NumIdentifierLookupHits, NumIdentifierLookups,
6875 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
6876 }
6877
Douglas Gregore060e572013-01-25 01:03:03 +00006878 if (GlobalIndex) {
6879 std::fprintf(stderr, "\n");
6880 GlobalIndex->printStats();
6881 }
6882
Guy Benyei11169dd2012-12-18 14:30:41 +00006883 std::fprintf(stderr, "\n");
6884 dump();
6885 std::fprintf(stderr, "\n");
6886}
6887
6888template<typename Key, typename ModuleFile, unsigned InitialCapacity>
6889static void
6890dumpModuleIDMap(StringRef Name,
6891 const ContinuousRangeMap<Key, ModuleFile *,
6892 InitialCapacity> &Map) {
6893 if (Map.begin() == Map.end())
6894 return;
6895
6896 typedef ContinuousRangeMap<Key, ModuleFile *, InitialCapacity> MapType;
6897 llvm::errs() << Name << ":\n";
6898 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
6899 I != IEnd; ++I) {
6900 llvm::errs() << " " << I->first << " -> " << I->second->FileName
6901 << "\n";
6902 }
6903}
6904
Yaron Kerencdae9412016-01-29 19:38:18 +00006905LLVM_DUMP_METHOD void ASTReader::dump() {
Guy Benyei11169dd2012-12-18 14:30:41 +00006906 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
6907 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
6908 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
6909 dumpModuleIDMap("Global type map", GlobalTypeMap);
6910 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
6911 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
6912 dumpModuleIDMap("Global macro map", GlobalMacroMap);
6913 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
6914 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
6915 dumpModuleIDMap("Global preprocessed entity map",
6916 GlobalPreprocessedEntityMap);
6917
6918 llvm::errs() << "\n*** PCH/Modules Loaded:";
6919 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
6920 MEnd = ModuleMgr.end();
6921 M != MEnd; ++M)
6922 (*M)->dump();
6923}
6924
6925/// Return the amount of memory used by memory buffers, breaking down
6926/// by heap-backed versus mmap'ed memory.
6927void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
6928 for (ModuleConstIterator I = ModuleMgr.begin(),
6929 E = ModuleMgr.end(); I != E; ++I) {
6930 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
6931 size_t bytes = buf->getBufferSize();
6932 switch (buf->getBufferKind()) {
6933 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
6934 sizes.malloc_bytes += bytes;
6935 break;
6936 case llvm::MemoryBuffer::MemoryBuffer_MMap:
6937 sizes.mmap_bytes += bytes;
6938 break;
6939 }
6940 }
6941 }
6942}
6943
6944void ASTReader::InitializeSema(Sema &S) {
6945 SemaObj = &S;
6946 S.addExternalSource(this);
6947
6948 // Makes sure any declarations that were deserialized "too early"
6949 // still get added to the identifier's declaration chains.
Ben Langmuir5418f402014-09-10 21:29:41 +00006950 for (uint64_t ID : PreloadedDeclIDs) {
6951 NamedDecl *D = cast<NamedDecl>(GetDecl(ID));
6952 pushExternalDeclIntoScope(D, D->getDeclName());
Guy Benyei11169dd2012-12-18 14:30:41 +00006953 }
Ben Langmuir5418f402014-09-10 21:29:41 +00006954 PreloadedDeclIDs.clear();
Guy Benyei11169dd2012-12-18 14:30:41 +00006955
Richard Smith3d8e97e2013-10-18 06:54:39 +00006956 // FIXME: What happens if these are changed by a module import?
Guy Benyei11169dd2012-12-18 14:30:41 +00006957 if (!FPPragmaOptions.empty()) {
6958 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
6959 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
6960 }
6961
Richard Smith3d8e97e2013-10-18 06:54:39 +00006962 // FIXME: What happens if these are changed by a module import?
Guy Benyei11169dd2012-12-18 14:30:41 +00006963 if (!OpenCLExtensions.empty()) {
6964 unsigned I = 0;
6965#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
6966#include "clang/Basic/OpenCLExtensions.def"
6967
6968 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
6969 }
Richard Smith3d8e97e2013-10-18 06:54:39 +00006970
6971 UpdateSema();
6972}
6973
6974void ASTReader::UpdateSema() {
6975 assert(SemaObj && "no Sema to update");
6976
6977 // Load the offsets of the declarations that Sema references.
6978 // They will be lazily deserialized when needed.
6979 if (!SemaDeclRefs.empty()) {
6980 assert(SemaDeclRefs.size() % 2 == 0);
6981 for (unsigned I = 0; I != SemaDeclRefs.size(); I += 2) {
6982 if (!SemaObj->StdNamespace)
6983 SemaObj->StdNamespace = SemaDeclRefs[I];
6984 if (!SemaObj->StdBadAlloc)
6985 SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
6986 }
6987 SemaDeclRefs.clear();
6988 }
Dario Domizioli13a0a382014-05-23 12:13:25 +00006989
6990 // Update the state of 'pragma clang optimize'. Use the same API as if we had
6991 // encountered the pragma in the source.
6992 if(OptimizeOffPragmaLocation.isValid())
6993 SemaObj->ActOnPragmaOptimize(/* IsOn = */ false, OptimizeOffPragmaLocation);
Guy Benyei11169dd2012-12-18 14:30:41 +00006994}
6995
Richard Smitha8d5b6a2015-07-17 19:51:03 +00006996IdentifierInfo *ASTReader::get(StringRef Name) {
Guy Benyei11169dd2012-12-18 14:30:41 +00006997 // Note that we are loading an identifier.
6998 Deserializing AnIdentifier(this);
Douglas Gregore060e572013-01-25 01:03:03 +00006999
Douglas Gregor7211ac12013-01-25 23:32:03 +00007000 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
Douglas Gregor00a50f72013-01-25 00:38:33 +00007001 NumIdentifierLookups,
7002 NumIdentifierLookupHits);
Richard Smith33e0f7e2015-07-22 02:08:40 +00007003
7004 // We don't need to do identifier table lookups in C++ modules (we preload
7005 // all interesting declarations, and don't need to use the scope for name
7006 // lookups). Perform the lookup in PCH files, though, since we don't build
7007 // a complete initial identifier table if we're carrying on from a PCH.
7008 if (Context.getLangOpts().CPlusPlus) {
7009 for (auto F : ModuleMgr.pch_modules())
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00007010 if (Visitor(*F))
Richard Smith33e0f7e2015-07-22 02:08:40 +00007011 break;
7012 } else {
7013 // If there is a global index, look there first to determine which modules
7014 // provably do not have any results for this identifier.
7015 GlobalModuleIndex::HitSet Hits;
7016 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
7017 if (!loadGlobalIndex()) {
7018 if (GlobalIndex->lookupIdentifier(Name, Hits)) {
7019 HitsPtr = &Hits;
7020 }
7021 }
7022
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00007023 ModuleMgr.visit(Visitor, HitsPtr);
Richard Smith33e0f7e2015-07-22 02:08:40 +00007024 }
7025
Guy Benyei11169dd2012-12-18 14:30:41 +00007026 IdentifierInfo *II = Visitor.getIdentifierInfo();
7027 markIdentifierUpToDate(II);
7028 return II;
7029}
7030
7031namespace clang {
7032 /// \brief An identifier-lookup iterator that enumerates all of the
7033 /// identifiers stored within a set of AST files.
7034 class ASTIdentifierIterator : public IdentifierIterator {
7035 /// \brief The AST reader whose identifiers are being enumerated.
7036 const ASTReader &Reader;
7037
7038 /// \brief The current index into the chain of AST files stored in
7039 /// the AST reader.
7040 unsigned Index;
7041
7042 /// \brief The current position within the identifier lookup table
7043 /// of the current AST file.
7044 ASTIdentifierLookupTable::key_iterator Current;
7045
7046 /// \brief The end position within the identifier lookup table of
7047 /// the current AST file.
7048 ASTIdentifierLookupTable::key_iterator End;
7049
7050 public:
7051 explicit ASTIdentifierIterator(const ASTReader &Reader);
7052
Craig Topper3e89dfe2014-03-13 02:13:41 +00007053 StringRef Next() override;
Guy Benyei11169dd2012-12-18 14:30:41 +00007054 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +00007055}
Guy Benyei11169dd2012-12-18 14:30:41 +00007056
7057ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
7058 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
7059 ASTIdentifierLookupTable *IdTable
7060 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
7061 Current = IdTable->key_begin();
7062 End = IdTable->key_end();
7063}
7064
7065StringRef ASTIdentifierIterator::Next() {
7066 while (Current == End) {
7067 // If we have exhausted all of our AST files, we're done.
7068 if (Index == 0)
7069 return StringRef();
7070
7071 --Index;
7072 ASTIdentifierLookupTable *IdTable
7073 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
7074 IdentifierLookupTable;
7075 Current = IdTable->key_begin();
7076 End = IdTable->key_end();
7077 }
7078
7079 // We have any identifiers remaining in the current AST file; return
7080 // the next one.
Douglas Gregorbfd73d72013-01-23 18:53:14 +00007081 StringRef Result = *Current;
Guy Benyei11169dd2012-12-18 14:30:41 +00007082 ++Current;
Douglas Gregorbfd73d72013-01-23 18:53:14 +00007083 return Result;
Guy Benyei11169dd2012-12-18 14:30:41 +00007084}
7085
Argyrios Kyrtzidis9aca3c62013-04-17 22:10:55 +00007086IdentifierIterator *ASTReader::getIdentifiers() {
7087 if (!loadGlobalIndex())
7088 return GlobalIndex->createIdentifierIterator();
7089
Guy Benyei11169dd2012-12-18 14:30:41 +00007090 return new ASTIdentifierIterator(*this);
7091}
7092
7093namespace clang { namespace serialization {
7094 class ReadMethodPoolVisitor {
7095 ASTReader &Reader;
7096 Selector Sel;
7097 unsigned PriorGeneration;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00007098 unsigned InstanceBits;
7099 unsigned FactoryBits;
Nico Weberff4b35e2014-12-27 22:14:15 +00007100 bool InstanceHasMoreThanOneDecl;
7101 bool FactoryHasMoreThanOneDecl;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00007102 SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
7103 SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
Guy Benyei11169dd2012-12-18 14:30:41 +00007104
7105 public:
Nico Weber2e0c8f72014-12-27 03:58:08 +00007106 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
Guy Benyei11169dd2012-12-18 14:30:41 +00007107 unsigned PriorGeneration)
Nico Weber2e0c8f72014-12-27 03:58:08 +00007108 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration),
Nico Weberff4b35e2014-12-27 22:14:15 +00007109 InstanceBits(0), FactoryBits(0), InstanceHasMoreThanOneDecl(false),
7110 FactoryHasMoreThanOneDecl(false) {}
Nico Weber2e0c8f72014-12-27 03:58:08 +00007111
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00007112 bool operator()(ModuleFile &M) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007113 if (!M.SelectorLookupTable)
7114 return false;
7115
7116 // If we've already searched this module file, skip it now.
Richard Smithbdf2d932015-07-30 03:37:16 +00007117 if (M.Generation <= PriorGeneration)
Guy Benyei11169dd2012-12-18 14:30:41 +00007118 return true;
7119
Richard Smithbdf2d932015-07-30 03:37:16 +00007120 ++Reader.NumMethodPoolTableLookups;
Guy Benyei11169dd2012-12-18 14:30:41 +00007121 ASTSelectorLookupTable *PoolTable
7122 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
Richard Smithbdf2d932015-07-30 03:37:16 +00007123 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
Guy Benyei11169dd2012-12-18 14:30:41 +00007124 if (Pos == PoolTable->end())
7125 return false;
Douglas Gregorad2f7a52013-01-28 17:54:36 +00007126
Richard Smithbdf2d932015-07-30 03:37:16 +00007127 ++Reader.NumMethodPoolTableHits;
7128 ++Reader.NumSelectorsRead;
Guy Benyei11169dd2012-12-18 14:30:41 +00007129 // FIXME: Not quite happy with the statistics here. We probably should
7130 // disable this tracking when called via LoadSelector.
7131 // Also, should entries without methods count as misses?
Richard Smithbdf2d932015-07-30 03:37:16 +00007132 ++Reader.NumMethodPoolEntriesRead;
Guy Benyei11169dd2012-12-18 14:30:41 +00007133 ASTSelectorLookupTrait::data_type Data = *Pos;
Richard Smithbdf2d932015-07-30 03:37:16 +00007134 if (Reader.DeserializationListener)
7135 Reader.DeserializationListener->SelectorRead(Data.ID, Sel);
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00007136
Richard Smithbdf2d932015-07-30 03:37:16 +00007137 InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
7138 FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
7139 InstanceBits = Data.InstanceBits;
7140 FactoryBits = Data.FactoryBits;
7141 InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
7142 FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00007143 return true;
7144 }
7145
7146 /// \brief Retrieve the instance methods found by this visitor.
7147 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
7148 return InstanceMethods;
7149 }
7150
7151 /// \brief Retrieve the instance methods found by this visitor.
7152 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
7153 return FactoryMethods;
7154 }
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00007155
7156 unsigned getInstanceBits() const { return InstanceBits; }
7157 unsigned getFactoryBits() const { return FactoryBits; }
Nico Weberff4b35e2014-12-27 22:14:15 +00007158 bool instanceHasMoreThanOneDecl() const {
7159 return InstanceHasMoreThanOneDecl;
7160 }
7161 bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
Guy Benyei11169dd2012-12-18 14:30:41 +00007162 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +00007163} } // end namespace clang::serialization
Guy Benyei11169dd2012-12-18 14:30:41 +00007164
7165/// \brief Add the given set of methods to the method list.
7166static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
7167 ObjCMethodList &List) {
7168 for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
7169 S.addMethodToGlobalList(&List, Methods[I]);
7170 }
7171}
7172
7173void ASTReader::ReadMethodPool(Selector Sel) {
7174 // Get the selector generation and update it to the current generation.
7175 unsigned &Generation = SelectorGeneration[Sel];
7176 unsigned PriorGeneration = Generation;
Richard Smith053f6c62014-05-16 23:01:30 +00007177 Generation = getGeneration();
Guy Benyei11169dd2012-12-18 14:30:41 +00007178
7179 // Search for methods defined with this selector.
Douglas Gregorad2f7a52013-01-28 17:54:36 +00007180 ++NumMethodPoolLookups;
Guy Benyei11169dd2012-12-18 14:30:41 +00007181 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
Benjamin Kramer9a9efba2015-07-25 12:14:04 +00007182 ModuleMgr.visit(Visitor);
7183
Guy Benyei11169dd2012-12-18 14:30:41 +00007184 if (Visitor.getInstanceMethods().empty() &&
Douglas Gregorad2f7a52013-01-28 17:54:36 +00007185 Visitor.getFactoryMethods().empty())
Guy Benyei11169dd2012-12-18 14:30:41 +00007186 return;
Douglas Gregorad2f7a52013-01-28 17:54:36 +00007187
7188 ++NumMethodPoolHits;
7189
Guy Benyei11169dd2012-12-18 14:30:41 +00007190 if (!getSema())
7191 return;
7192
7193 Sema &S = *getSema();
7194 Sema::GlobalMethodPool::iterator Pos
7195 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
Ben Langmuira0c32e92015-01-12 19:27:00 +00007196
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00007197 Pos->second.first.setBits(Visitor.getInstanceBits());
Nico Weberff4b35e2014-12-27 22:14:15 +00007198 Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00007199 Pos->second.second.setBits(Visitor.getFactoryBits());
Nico Weberff4b35e2014-12-27 22:14:15 +00007200 Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
Ben Langmuira0c32e92015-01-12 19:27:00 +00007201
7202 // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
7203 // when building a module we keep every method individually and may need to
7204 // update hasMoreThanOneDecl as we add the methods.
7205 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
7206 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
Guy Benyei11169dd2012-12-18 14:30:41 +00007207}
7208
7209void ASTReader::ReadKnownNamespaces(
7210 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
7211 Namespaces.clear();
7212
7213 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
7214 if (NamespaceDecl *Namespace
7215 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
7216 Namespaces.push_back(Namespace);
7217 }
7218}
7219
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00007220void ASTReader::ReadUndefinedButUsed(
Nick Lewyckyf0f56162013-01-31 03:23:57 +00007221 llvm::DenseMap<NamedDecl*, SourceLocation> &Undefined) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00007222 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
7223 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
Nick Lewycky8334af82013-01-26 00:35:08 +00007224 SourceLocation Loc =
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00007225 SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
Nick Lewycky8334af82013-01-26 00:35:08 +00007226 Undefined.insert(std::make_pair(D, Loc));
7227 }
7228}
Nick Lewycky8334af82013-01-26 00:35:08 +00007229
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00007230void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
7231 FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
7232 Exprs) {
7233 for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
7234 FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++]));
7235 uint64_t Count = DelayedDeleteExprs[Idx++];
7236 for (uint64_t C = 0; C < Count; ++C) {
7237 SourceLocation DeleteLoc =
7238 SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]);
7239 const bool IsArrayForm = DelayedDeleteExprs[Idx++];
7240 Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm));
7241 }
7242 }
7243}
7244
Guy Benyei11169dd2012-12-18 14:30:41 +00007245void ASTReader::ReadTentativeDefinitions(
7246 SmallVectorImpl<VarDecl *> &TentativeDefs) {
7247 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
7248 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
7249 if (Var)
7250 TentativeDefs.push_back(Var);
7251 }
7252 TentativeDefinitions.clear();
7253}
7254
7255void ASTReader::ReadUnusedFileScopedDecls(
7256 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
7257 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
7258 DeclaratorDecl *D
7259 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
7260 if (D)
7261 Decls.push_back(D);
7262 }
7263 UnusedFileScopedDecls.clear();
7264}
7265
7266void ASTReader::ReadDelegatingConstructors(
7267 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
7268 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
7269 CXXConstructorDecl *D
7270 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
7271 if (D)
7272 Decls.push_back(D);
7273 }
7274 DelegatingCtorDecls.clear();
7275}
7276
7277void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
7278 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
7279 TypedefNameDecl *D
7280 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
7281 if (D)
7282 Decls.push_back(D);
7283 }
7284 ExtVectorDecls.clear();
7285}
7286
Nico Weber72889432014-09-06 01:25:55 +00007287void ASTReader::ReadUnusedLocalTypedefNameCandidates(
7288 llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
7289 for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
7290 ++I) {
7291 TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
7292 GetDecl(UnusedLocalTypedefNameCandidates[I]));
7293 if (D)
7294 Decls.insert(D);
7295 }
7296 UnusedLocalTypedefNameCandidates.clear();
7297}
7298
Guy Benyei11169dd2012-12-18 14:30:41 +00007299void ASTReader::ReadReferencedSelectors(
7300 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
7301 if (ReferencedSelectorsData.empty())
7302 return;
7303
7304 // If there are @selector references added them to its pool. This is for
7305 // implementation of -Wselector.
7306 unsigned int DataSize = ReferencedSelectorsData.size()-1;
7307 unsigned I = 0;
7308 while (I < DataSize) {
7309 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
7310 SourceLocation SelLoc
7311 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
7312 Sels.push_back(std::make_pair(Sel, SelLoc));
7313 }
7314 ReferencedSelectorsData.clear();
7315}
7316
7317void ASTReader::ReadWeakUndeclaredIdentifiers(
7318 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
7319 if (WeakUndeclaredIdentifiers.empty())
7320 return;
7321
7322 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
7323 IdentifierInfo *WeakId
7324 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
7325 IdentifierInfo *AliasId
7326 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
7327 SourceLocation Loc
7328 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
7329 bool Used = WeakUndeclaredIdentifiers[I++];
7330 WeakInfo WI(AliasId, Loc);
7331 WI.setUsed(Used);
7332 WeakIDs.push_back(std::make_pair(WeakId, WI));
7333 }
7334 WeakUndeclaredIdentifiers.clear();
7335}
7336
7337void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
7338 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
7339 ExternalVTableUse VT;
7340 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
7341 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
7342 VT.DefinitionRequired = VTableUses[Idx++];
7343 VTables.push_back(VT);
7344 }
7345
7346 VTableUses.clear();
7347}
7348
7349void ASTReader::ReadPendingInstantiations(
7350 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
7351 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
7352 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
7353 SourceLocation Loc
7354 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
7355
7356 Pending.push_back(std::make_pair(D, Loc));
7357 }
7358 PendingInstantiations.clear();
7359}
7360
Richard Smithe40f2ba2013-08-07 21:41:30 +00007361void ASTReader::ReadLateParsedTemplates(
Chandler Carruth52cee4d2015-03-26 09:08:15 +00007362 llvm::MapVector<const FunctionDecl *, LateParsedTemplate *> &LPTMap) {
Richard Smithe40f2ba2013-08-07 21:41:30 +00007363 for (unsigned Idx = 0, N = LateParsedTemplates.size(); Idx < N;
7364 /* In loop */) {
7365 FunctionDecl *FD = cast<FunctionDecl>(GetDecl(LateParsedTemplates[Idx++]));
7366
7367 LateParsedTemplate *LT = new LateParsedTemplate;
7368 LT->D = GetDecl(LateParsedTemplates[Idx++]);
7369
7370 ModuleFile *F = getOwningModuleFile(LT->D);
7371 assert(F && "No module");
7372
7373 unsigned TokN = LateParsedTemplates[Idx++];
7374 LT->Toks.reserve(TokN);
7375 for (unsigned T = 0; T < TokN; ++T)
7376 LT->Toks.push_back(ReadToken(*F, LateParsedTemplates, Idx));
7377
Chandler Carruth52cee4d2015-03-26 09:08:15 +00007378 LPTMap.insert(std::make_pair(FD, LT));
Richard Smithe40f2ba2013-08-07 21:41:30 +00007379 }
7380
7381 LateParsedTemplates.clear();
7382}
7383
Guy Benyei11169dd2012-12-18 14:30:41 +00007384void ASTReader::LoadSelector(Selector Sel) {
7385 // It would be complicated to avoid reading the methods anyway. So don't.
7386 ReadMethodPool(Sel);
7387}
7388
7389void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
7390 assert(ID && "Non-zero identifier ID required");
7391 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
7392 IdentifiersLoaded[ID - 1] = II;
7393 if (DeserializationListener)
7394 DeserializationListener->IdentifierRead(ID, II);
7395}
7396
7397/// \brief Set the globally-visible declarations associated with the given
7398/// identifier.
7399///
7400/// If the AST reader is currently in a state where the given declaration IDs
7401/// cannot safely be resolved, they are queued until it is safe to resolve
7402/// them.
7403///
7404/// \param II an IdentifierInfo that refers to one or more globally-visible
7405/// declarations.
7406///
7407/// \param DeclIDs the set of declaration IDs with the name @p II that are
7408/// visible at global scope.
7409///
Douglas Gregor6168bd22013-02-18 15:53:43 +00007410/// \param Decls if non-null, this vector will be populated with the set of
7411/// deserialized declarations. These declarations will not be pushed into
7412/// scope.
Guy Benyei11169dd2012-12-18 14:30:41 +00007413void
7414ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
7415 const SmallVectorImpl<uint32_t> &DeclIDs,
Douglas Gregor6168bd22013-02-18 15:53:43 +00007416 SmallVectorImpl<Decl *> *Decls) {
7417 if (NumCurrentElementsDeserializing && !Decls) {
7418 PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
Guy Benyei11169dd2012-12-18 14:30:41 +00007419 return;
7420 }
7421
7422 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
Ben Langmuir5418f402014-09-10 21:29:41 +00007423 if (!SemaObj) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007424 // Queue this declaration so that it will be added to the
7425 // translation unit scope and identifier's declaration chain
7426 // once a Sema object is known.
Ben Langmuir5418f402014-09-10 21:29:41 +00007427 PreloadedDeclIDs.push_back(DeclIDs[I]);
7428 continue;
Guy Benyei11169dd2012-12-18 14:30:41 +00007429 }
Ben Langmuir5418f402014-09-10 21:29:41 +00007430
7431 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
7432
7433 // If we're simply supposed to record the declarations, do so now.
7434 if (Decls) {
7435 Decls->push_back(D);
7436 continue;
7437 }
7438
7439 // Introduce this declaration into the translation-unit scope
7440 // and add it to the declaration chain for this identifier, so
7441 // that (unqualified) name lookup will find it.
7442 pushExternalDeclIntoScope(D, II);
Guy Benyei11169dd2012-12-18 14:30:41 +00007443 }
7444}
7445
Douglas Gregorc8a992f2013-01-21 16:52:34 +00007446IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007447 if (ID == 0)
Craig Toppera13603a2014-05-22 05:54:18 +00007448 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007449
7450 if (IdentifiersLoaded.empty()) {
7451 Error("no identifier table in AST file");
Craig Toppera13603a2014-05-22 05:54:18 +00007452 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007453 }
7454
7455 ID -= 1;
7456 if (!IdentifiersLoaded[ID]) {
7457 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
7458 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
7459 ModuleFile *M = I->second;
7460 unsigned Index = ID - M->BaseIdentifierID;
7461 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
7462
7463 // All of the strings in the AST file are preceded by a 16-bit length.
7464 // Extract that 16-bit length to avoid having to execute strlen().
7465 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
7466 // unsigned integers. This is important to avoid integer overflow when
7467 // we cast them to 'unsigned'.
7468 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
7469 unsigned StrLen = (((unsigned) StrLenPtr[0])
7470 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Richard Smitheb4b58f62016-02-05 01:40:54 +00007471 auto &II = PP.getIdentifierTable().get(StringRef(Str, StrLen));
7472 IdentifiersLoaded[ID] = &II;
7473 markIdentifierFromAST(*this, II);
Guy Benyei11169dd2012-12-18 14:30:41 +00007474 if (DeserializationListener)
Richard Smitheb4b58f62016-02-05 01:40:54 +00007475 DeserializationListener->IdentifierRead(ID + 1, &II);
Guy Benyei11169dd2012-12-18 14:30:41 +00007476 }
7477
7478 return IdentifiersLoaded[ID];
7479}
7480
Douglas Gregorc8a992f2013-01-21 16:52:34 +00007481IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
7482 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
Guy Benyei11169dd2012-12-18 14:30:41 +00007483}
7484
7485IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
7486 if (LocalID < NUM_PREDEF_IDENT_IDS)
7487 return LocalID;
7488
7489 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7490 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
7491 assert(I != M.IdentifierRemap.end()
7492 && "Invalid index into identifier index remap");
7493
7494 return LocalID + I->second;
7495}
7496
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00007497MacroInfo *ASTReader::getMacro(MacroID ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007498 if (ID == 0)
Craig Toppera13603a2014-05-22 05:54:18 +00007499 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007500
7501 if (MacrosLoaded.empty()) {
7502 Error("no macro table in AST file");
Craig Toppera13603a2014-05-22 05:54:18 +00007503 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007504 }
7505
7506 ID -= NUM_PREDEF_MACRO_IDS;
7507 if (!MacrosLoaded[ID]) {
7508 GlobalMacroMapType::iterator I
7509 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
7510 assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
7511 ModuleFile *M = I->second;
7512 unsigned Index = ID - M->BaseMacroID;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00007513 MacrosLoaded[ID] = ReadMacroRecord(*M, M->MacroOffsets[Index]);
7514
7515 if (DeserializationListener)
7516 DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
7517 MacrosLoaded[ID]);
Guy Benyei11169dd2012-12-18 14:30:41 +00007518 }
7519
7520 return MacrosLoaded[ID];
7521}
7522
7523MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
7524 if (LocalID < NUM_PREDEF_MACRO_IDS)
7525 return LocalID;
7526
7527 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7528 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
7529 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
7530
7531 return LocalID + I->second;
7532}
7533
7534serialization::SubmoduleID
7535ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
7536 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
7537 return LocalID;
7538
7539 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7540 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
7541 assert(I != M.SubmoduleRemap.end()
7542 && "Invalid index into submodule index remap");
7543
7544 return LocalID + I->second;
7545}
7546
7547Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
7548 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
7549 assert(GlobalID == 0 && "Unhandled global submodule ID");
Craig Toppera13603a2014-05-22 05:54:18 +00007550 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007551 }
7552
7553 if (GlobalID > SubmodulesLoaded.size()) {
7554 Error("submodule ID out of range in AST file");
Craig Toppera13603a2014-05-22 05:54:18 +00007555 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007556 }
7557
7558 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
7559}
Douglas Gregorc147b0b2013-01-12 01:29:50 +00007560
7561Module *ASTReader::getModule(unsigned ID) {
7562 return getSubmodule(ID);
7563}
7564
Richard Smithd88a7f12015-09-01 20:35:42 +00007565ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) {
7566 if (ID & 1) {
7567 // It's a module, look it up by submodule ID.
7568 auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1));
7569 return I == GlobalSubmoduleMap.end() ? nullptr : I->second;
7570 } else {
7571 // It's a prefix (preamble, PCH, ...). Look it up by index.
7572 unsigned IndexFromEnd = ID >> 1;
7573 assert(IndexFromEnd && "got reference to unknown module file");
7574 return getModuleManager().pch_modules().end()[-IndexFromEnd];
7575 }
7576}
7577
7578unsigned ASTReader::getModuleFileID(ModuleFile *F) {
7579 if (!F)
7580 return 1;
7581
7582 // For a file representing a module, use the submodule ID of the top-level
7583 // module as the file ID. For any other kind of file, the number of such
7584 // files loaded beforehand will be the same on reload.
7585 // FIXME: Is this true even if we have an explicit module file and a PCH?
7586 if (F->isModule())
7587 return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;
7588
7589 auto PCHModules = getModuleManager().pch_modules();
7590 auto I = std::find(PCHModules.begin(), PCHModules.end(), F);
7591 assert(I != PCHModules.end() && "emitting reference to unknown file");
7592 return (I - PCHModules.end()) << 1;
7593}
7594
Adrian Prantl15bcf702015-06-30 17:39:43 +00007595llvm::Optional<ExternalASTSource::ASTSourceDescriptor>
7596ASTReader::getSourceDescriptor(unsigned ID) {
7597 if (const Module *M = getSubmodule(ID))
Adrian Prantlc6458d62015-09-19 00:10:32 +00007598 return ExternalASTSource::ASTSourceDescriptor(*M);
Adrian Prantl15bcf702015-06-30 17:39:43 +00007599
7600 // If there is only a single PCH, return it instead.
7601 // Chained PCH are not suported.
7602 if (ModuleMgr.size() == 1) {
7603 ModuleFile &MF = ModuleMgr.getPrimaryModule();
Adrian Prantl3a2d4942016-01-22 23:30:56 +00007604 StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName);
7605 return ASTReader::ASTSourceDescriptor(ModuleName, MF.OriginalDir,
7606 MF.FileName, MF.Signature);
Adrian Prantl15bcf702015-06-30 17:39:43 +00007607 }
7608 return None;
7609}
7610
Guy Benyei11169dd2012-12-18 14:30:41 +00007611Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
7612 return DecodeSelector(getGlobalSelectorID(M, LocalID));
7613}
7614
7615Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
7616 if (ID == 0)
7617 return Selector();
7618
7619 if (ID > SelectorsLoaded.size()) {
7620 Error("selector ID out of range in AST file");
7621 return Selector();
7622 }
7623
Craig Toppera13603a2014-05-22 05:54:18 +00007624 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007625 // Load this selector from the selector table.
7626 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
7627 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
7628 ModuleFile &M = *I->second;
7629 ASTSelectorLookupTrait Trait(*this, M);
7630 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
7631 SelectorsLoaded[ID - 1] =
7632 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
7633 if (DeserializationListener)
7634 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
7635 }
7636
7637 return SelectorsLoaded[ID - 1];
7638}
7639
7640Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
7641 return DecodeSelector(ID);
7642}
7643
7644uint32_t ASTReader::GetNumExternalSelectors() {
7645 // ID 0 (the null selector) is considered an external selector.
7646 return getTotalNumSelectors() + 1;
7647}
7648
7649serialization::SelectorID
7650ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
7651 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
7652 return LocalID;
7653
7654 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7655 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
7656 assert(I != M.SelectorRemap.end()
7657 && "Invalid index into selector index remap");
7658
7659 return LocalID + I->second;
7660}
7661
7662DeclarationName
7663ASTReader::ReadDeclarationName(ModuleFile &F,
7664 const RecordData &Record, unsigned &Idx) {
7665 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
7666 switch (Kind) {
7667 case DeclarationName::Identifier:
Douglas Gregorc8a992f2013-01-21 16:52:34 +00007668 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00007669
7670 case DeclarationName::ObjCZeroArgSelector:
7671 case DeclarationName::ObjCOneArgSelector:
7672 case DeclarationName::ObjCMultiArgSelector:
7673 return DeclarationName(ReadSelector(F, Record, Idx));
7674
7675 case DeclarationName::CXXConstructorName:
7676 return Context.DeclarationNames.getCXXConstructorName(
7677 Context.getCanonicalType(readType(F, Record, Idx)));
7678
7679 case DeclarationName::CXXDestructorName:
7680 return Context.DeclarationNames.getCXXDestructorName(
7681 Context.getCanonicalType(readType(F, Record, Idx)));
7682
7683 case DeclarationName::CXXConversionFunctionName:
7684 return Context.DeclarationNames.getCXXConversionFunctionName(
7685 Context.getCanonicalType(readType(F, Record, Idx)));
7686
7687 case DeclarationName::CXXOperatorName:
7688 return Context.DeclarationNames.getCXXOperatorName(
7689 (OverloadedOperatorKind)Record[Idx++]);
7690
7691 case DeclarationName::CXXLiteralOperatorName:
7692 return Context.DeclarationNames.getCXXLiteralOperatorName(
7693 GetIdentifierInfo(F, Record, Idx));
7694
7695 case DeclarationName::CXXUsingDirective:
7696 return DeclarationName::getUsingDirectiveName();
7697 }
7698
7699 llvm_unreachable("Invalid NameKind!");
7700}
7701
7702void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
7703 DeclarationNameLoc &DNLoc,
7704 DeclarationName Name,
7705 const RecordData &Record, unsigned &Idx) {
7706 switch (Name.getNameKind()) {
7707 case DeclarationName::CXXConstructorName:
7708 case DeclarationName::CXXDestructorName:
7709 case DeclarationName::CXXConversionFunctionName:
7710 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
7711 break;
7712
7713 case DeclarationName::CXXOperatorName:
7714 DNLoc.CXXOperatorName.BeginOpNameLoc
7715 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
7716 DNLoc.CXXOperatorName.EndOpNameLoc
7717 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
7718 break;
7719
7720 case DeclarationName::CXXLiteralOperatorName:
7721 DNLoc.CXXLiteralOperatorName.OpNameLoc
7722 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
7723 break;
7724
7725 case DeclarationName::Identifier:
7726 case DeclarationName::ObjCZeroArgSelector:
7727 case DeclarationName::ObjCOneArgSelector:
7728 case DeclarationName::ObjCMultiArgSelector:
7729 case DeclarationName::CXXUsingDirective:
7730 break;
7731 }
7732}
7733
7734void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
7735 DeclarationNameInfo &NameInfo,
7736 const RecordData &Record, unsigned &Idx) {
7737 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
7738 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
7739 DeclarationNameLoc DNLoc;
7740 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
7741 NameInfo.setInfo(DNLoc);
7742}
7743
7744void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
7745 const RecordData &Record, unsigned &Idx) {
7746 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
7747 unsigned NumTPLists = Record[Idx++];
7748 Info.NumTemplParamLists = NumTPLists;
7749 if (NumTPLists) {
7750 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
7751 for (unsigned i=0; i != NumTPLists; ++i)
7752 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
7753 }
7754}
7755
7756TemplateName
7757ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
7758 unsigned &Idx) {
7759 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
7760 switch (Kind) {
7761 case TemplateName::Template:
7762 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
7763
7764 case TemplateName::OverloadedTemplate: {
7765 unsigned size = Record[Idx++];
7766 UnresolvedSet<8> Decls;
7767 while (size--)
7768 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
7769
7770 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
7771 }
7772
7773 case TemplateName::QualifiedTemplate: {
7774 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
7775 bool hasTemplKeyword = Record[Idx++];
7776 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
7777 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
7778 }
7779
7780 case TemplateName::DependentTemplate: {
7781 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
7782 if (Record[Idx++]) // isIdentifier
7783 return Context.getDependentTemplateName(NNS,
7784 GetIdentifierInfo(F, Record,
7785 Idx));
7786 return Context.getDependentTemplateName(NNS,
7787 (OverloadedOperatorKind)Record[Idx++]);
7788 }
7789
7790 case TemplateName::SubstTemplateTemplateParm: {
7791 TemplateTemplateParmDecl *param
7792 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
7793 if (!param) return TemplateName();
7794 TemplateName replacement = ReadTemplateName(F, Record, Idx);
7795 return Context.getSubstTemplateTemplateParm(param, replacement);
7796 }
7797
7798 case TemplateName::SubstTemplateTemplateParmPack: {
7799 TemplateTemplateParmDecl *Param
7800 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
7801 if (!Param)
7802 return TemplateName();
7803
7804 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
7805 if (ArgPack.getKind() != TemplateArgument::Pack)
7806 return TemplateName();
7807
7808 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
7809 }
7810 }
7811
7812 llvm_unreachable("Unhandled template name kind!");
7813}
7814
Richard Smith2bb3c342015-08-09 01:05:31 +00007815TemplateArgument ASTReader::ReadTemplateArgument(ModuleFile &F,
7816 const RecordData &Record,
7817 unsigned &Idx,
7818 bool Canonicalize) {
7819 if (Canonicalize) {
7820 // The caller wants a canonical template argument. Sometimes the AST only
7821 // wants template arguments in canonical form (particularly as the template
7822 // argument lists of template specializations) so ensure we preserve that
7823 // canonical form across serialization.
7824 TemplateArgument Arg = ReadTemplateArgument(F, Record, Idx, false);
7825 return Context.getCanonicalTemplateArgument(Arg);
7826 }
7827
Guy Benyei11169dd2012-12-18 14:30:41 +00007828 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
7829 switch (Kind) {
7830 case TemplateArgument::Null:
7831 return TemplateArgument();
7832 case TemplateArgument::Type:
7833 return TemplateArgument(readType(F, Record, Idx));
7834 case TemplateArgument::Declaration: {
7835 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
David Blaikie0f62c8d2014-10-16 04:21:25 +00007836 return TemplateArgument(D, readType(F, Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00007837 }
7838 case TemplateArgument::NullPtr:
7839 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
7840 case TemplateArgument::Integral: {
7841 llvm::APSInt Value = ReadAPSInt(Record, Idx);
7842 QualType T = readType(F, Record, Idx);
7843 return TemplateArgument(Context, Value, T);
7844 }
7845 case TemplateArgument::Template:
7846 return TemplateArgument(ReadTemplateName(F, Record, Idx));
7847 case TemplateArgument::TemplateExpansion: {
7848 TemplateName Name = ReadTemplateName(F, Record, Idx);
David Blaikie05785d12013-02-20 22:23:23 +00007849 Optional<unsigned> NumTemplateExpansions;
Guy Benyei11169dd2012-12-18 14:30:41 +00007850 if (unsigned NumExpansions = Record[Idx++])
7851 NumTemplateExpansions = NumExpansions - 1;
7852 return TemplateArgument(Name, NumTemplateExpansions);
7853 }
7854 case TemplateArgument::Expression:
7855 return TemplateArgument(ReadExpr(F));
7856 case TemplateArgument::Pack: {
7857 unsigned NumArgs = Record[Idx++];
7858 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
7859 for (unsigned I = 0; I != NumArgs; ++I)
7860 Args[I] = ReadTemplateArgument(F, Record, Idx);
Benjamin Kramercce63472015-08-05 09:40:22 +00007861 return TemplateArgument(llvm::makeArrayRef(Args, NumArgs));
Guy Benyei11169dd2012-12-18 14:30:41 +00007862 }
7863 }
7864
7865 llvm_unreachable("Unhandled template argument kind!");
7866}
7867
7868TemplateParameterList *
7869ASTReader::ReadTemplateParameterList(ModuleFile &F,
7870 const RecordData &Record, unsigned &Idx) {
7871 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
7872 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
7873 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
7874
7875 unsigned NumParams = Record[Idx++];
7876 SmallVector<NamedDecl *, 16> Params;
7877 Params.reserve(NumParams);
7878 while (NumParams--)
7879 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
7880
7881 TemplateParameterList* TemplateParams =
7882 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
David Majnemer902f8c62015-12-27 07:16:27 +00007883 Params, RAngleLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00007884 return TemplateParams;
7885}
7886
7887void
7888ASTReader::
Craig Topper5603df42013-07-05 19:34:19 +00007889ReadTemplateArgumentList(SmallVectorImpl<TemplateArgument> &TemplArgs,
Guy Benyei11169dd2012-12-18 14:30:41 +00007890 ModuleFile &F, const RecordData &Record,
Richard Smith2bb3c342015-08-09 01:05:31 +00007891 unsigned &Idx, bool Canonicalize) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007892 unsigned NumTemplateArgs = Record[Idx++];
7893 TemplArgs.reserve(NumTemplateArgs);
7894 while (NumTemplateArgs--)
Richard Smith2bb3c342015-08-09 01:05:31 +00007895 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx, Canonicalize));
Guy Benyei11169dd2012-12-18 14:30:41 +00007896}
7897
7898/// \brief Read a UnresolvedSet structure.
Richard Smitha4ba74c2013-08-30 04:46:40 +00007899void ASTReader::ReadUnresolvedSet(ModuleFile &F, LazyASTUnresolvedSet &Set,
Guy Benyei11169dd2012-12-18 14:30:41 +00007900 const RecordData &Record, unsigned &Idx) {
7901 unsigned NumDecls = Record[Idx++];
7902 Set.reserve(Context, NumDecls);
7903 while (NumDecls--) {
Richard Smitha4ba74c2013-08-30 04:46:40 +00007904 DeclID ID = ReadDeclID(F, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00007905 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
Richard Smitha4ba74c2013-08-30 04:46:40 +00007906 Set.addLazyDecl(Context, ID, AS);
Guy Benyei11169dd2012-12-18 14:30:41 +00007907 }
7908}
7909
7910CXXBaseSpecifier
7911ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
7912 const RecordData &Record, unsigned &Idx) {
7913 bool isVirtual = static_cast<bool>(Record[Idx++]);
7914 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
7915 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
7916 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
7917 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
7918 SourceRange Range = ReadSourceRange(F, Record, Idx);
7919 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
7920 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
7921 EllipsisLoc);
7922 Result.setInheritConstructors(inheritConstructors);
7923 return Result;
7924}
7925
Richard Smithc2bb8182015-03-24 06:36:48 +00007926CXXCtorInitializer **
Guy Benyei11169dd2012-12-18 14:30:41 +00007927ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
7928 unsigned &Idx) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007929 unsigned NumInitializers = Record[Idx++];
Richard Smithc2bb8182015-03-24 06:36:48 +00007930 assert(NumInitializers && "wrote ctor initializers but have no inits");
7931 auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
7932 for (unsigned i = 0; i != NumInitializers; ++i) {
7933 TypeSourceInfo *TInfo = nullptr;
7934 bool IsBaseVirtual = false;
7935 FieldDecl *Member = nullptr;
7936 IndirectFieldDecl *IndirectMember = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007937
Richard Smithc2bb8182015-03-24 06:36:48 +00007938 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
7939 switch (Type) {
7940 case CTOR_INITIALIZER_BASE:
7941 TInfo = GetTypeSourceInfo(F, Record, Idx);
7942 IsBaseVirtual = Record[Idx++];
7943 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00007944
Richard Smithc2bb8182015-03-24 06:36:48 +00007945 case CTOR_INITIALIZER_DELEGATING:
7946 TInfo = GetTypeSourceInfo(F, Record, Idx);
7947 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00007948
Richard Smithc2bb8182015-03-24 06:36:48 +00007949 case CTOR_INITIALIZER_MEMBER:
7950 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
7951 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00007952
Richard Smithc2bb8182015-03-24 06:36:48 +00007953 case CTOR_INITIALIZER_INDIRECT_MEMBER:
7954 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
7955 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00007956 }
Richard Smithc2bb8182015-03-24 06:36:48 +00007957
7958 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
7959 Expr *Init = ReadExpr(F);
7960 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
7961 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
7962 bool IsWritten = Record[Idx++];
7963 unsigned SourceOrderOrNumArrayIndices;
7964 SmallVector<VarDecl *, 8> Indices;
7965 if (IsWritten) {
7966 SourceOrderOrNumArrayIndices = Record[Idx++];
7967 } else {
7968 SourceOrderOrNumArrayIndices = Record[Idx++];
7969 Indices.reserve(SourceOrderOrNumArrayIndices);
7970 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
7971 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
7972 }
7973
7974 CXXCtorInitializer *BOMInit;
7975 if (Type == CTOR_INITIALIZER_BASE) {
7976 BOMInit = new (Context)
7977 CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
7978 RParenLoc, MemberOrEllipsisLoc);
7979 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
7980 BOMInit = new (Context)
7981 CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
7982 } else if (IsWritten) {
7983 if (Member)
7984 BOMInit = new (Context) CXXCtorInitializer(
7985 Context, Member, MemberOrEllipsisLoc, LParenLoc, Init, RParenLoc);
7986 else
7987 BOMInit = new (Context)
7988 CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
7989 LParenLoc, Init, RParenLoc);
7990 } else {
7991 if (IndirectMember) {
7992 assert(Indices.empty() && "Indirect field improperly initialized");
7993 BOMInit = new (Context)
7994 CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
7995 LParenLoc, Init, RParenLoc);
7996 } else {
7997 BOMInit = CXXCtorInitializer::Create(
7998 Context, Member, MemberOrEllipsisLoc, LParenLoc, Init, RParenLoc,
7999 Indices.data(), Indices.size());
8000 }
8001 }
8002
8003 if (IsWritten)
8004 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
8005 CtorInitializers[i] = BOMInit;
Guy Benyei11169dd2012-12-18 14:30:41 +00008006 }
8007
Richard Smithc2bb8182015-03-24 06:36:48 +00008008 return CtorInitializers;
Guy Benyei11169dd2012-12-18 14:30:41 +00008009}
8010
8011NestedNameSpecifier *
8012ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
8013 const RecordData &Record, unsigned &Idx) {
8014 unsigned N = Record[Idx++];
Craig Toppera13603a2014-05-22 05:54:18 +00008015 NestedNameSpecifier *NNS = nullptr, *Prev = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00008016 for (unsigned I = 0; I != N; ++I) {
8017 NestedNameSpecifier::SpecifierKind Kind
8018 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
8019 switch (Kind) {
8020 case NestedNameSpecifier::Identifier: {
8021 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
8022 NNS = NestedNameSpecifier::Create(Context, Prev, II);
8023 break;
8024 }
8025
8026 case NestedNameSpecifier::Namespace: {
8027 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
8028 NNS = NestedNameSpecifier::Create(Context, Prev, NS);
8029 break;
8030 }
8031
8032 case NestedNameSpecifier::NamespaceAlias: {
8033 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
8034 NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
8035 break;
8036 }
8037
8038 case NestedNameSpecifier::TypeSpec:
8039 case NestedNameSpecifier::TypeSpecWithTemplate: {
8040 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
8041 if (!T)
Craig Toppera13603a2014-05-22 05:54:18 +00008042 return nullptr;
8043
Guy Benyei11169dd2012-12-18 14:30:41 +00008044 bool Template = Record[Idx++];
8045 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
8046 break;
8047 }
8048
8049 case NestedNameSpecifier::Global: {
8050 NNS = NestedNameSpecifier::GlobalSpecifier(Context);
8051 // No associated value, and there can't be a prefix.
8052 break;
8053 }
Nikola Smiljanic67860242014-09-26 00:28:20 +00008054
8055 case NestedNameSpecifier::Super: {
8056 CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(F, Record, Idx);
8057 NNS = NestedNameSpecifier::SuperSpecifier(Context, RD);
8058 break;
8059 }
Guy Benyei11169dd2012-12-18 14:30:41 +00008060 }
8061 Prev = NNS;
8062 }
8063 return NNS;
8064}
8065
8066NestedNameSpecifierLoc
8067ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
8068 unsigned &Idx) {
8069 unsigned N = Record[Idx++];
8070 NestedNameSpecifierLocBuilder Builder;
8071 for (unsigned I = 0; I != N; ++I) {
8072 NestedNameSpecifier::SpecifierKind Kind
8073 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
8074 switch (Kind) {
8075 case NestedNameSpecifier::Identifier: {
8076 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
8077 SourceRange Range = ReadSourceRange(F, Record, Idx);
8078 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
8079 break;
8080 }
8081
8082 case NestedNameSpecifier::Namespace: {
8083 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
8084 SourceRange Range = ReadSourceRange(F, Record, Idx);
8085 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
8086 break;
8087 }
8088
8089 case NestedNameSpecifier::NamespaceAlias: {
8090 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
8091 SourceRange Range = ReadSourceRange(F, Record, Idx);
8092 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
8093 break;
8094 }
8095
8096 case NestedNameSpecifier::TypeSpec:
8097 case NestedNameSpecifier::TypeSpecWithTemplate: {
8098 bool Template = Record[Idx++];
8099 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
8100 if (!T)
8101 return NestedNameSpecifierLoc();
8102 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
8103
8104 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
8105 Builder.Extend(Context,
8106 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
8107 T->getTypeLoc(), ColonColonLoc);
8108 break;
8109 }
8110
8111 case NestedNameSpecifier::Global: {
8112 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
8113 Builder.MakeGlobal(Context, ColonColonLoc);
8114 break;
8115 }
Nikola Smiljanic67860242014-09-26 00:28:20 +00008116
8117 case NestedNameSpecifier::Super: {
8118 CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(F, Record, Idx);
8119 SourceRange Range = ReadSourceRange(F, Record, Idx);
8120 Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd());
8121 break;
8122 }
Guy Benyei11169dd2012-12-18 14:30:41 +00008123 }
8124 }
Nikola Smiljanic67860242014-09-26 00:28:20 +00008125
Guy Benyei11169dd2012-12-18 14:30:41 +00008126 return Builder.getWithLocInContext(Context);
8127}
8128
8129SourceRange
8130ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
8131 unsigned &Idx) {
8132 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
8133 SourceLocation end = ReadSourceLocation(F, Record, Idx);
8134 return SourceRange(beg, end);
8135}
8136
8137/// \brief Read an integral value
8138llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
8139 unsigned BitWidth = Record[Idx++];
8140 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
8141 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
8142 Idx += NumWords;
8143 return Result;
8144}
8145
8146/// \brief Read a signed integral value
8147llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
8148 bool isUnsigned = Record[Idx++];
8149 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
8150}
8151
8152/// \brief Read a floating-point value
Tim Northover178723a2013-01-22 09:46:51 +00008153llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record,
8154 const llvm::fltSemantics &Sem,
8155 unsigned &Idx) {
8156 return llvm::APFloat(Sem, ReadAPInt(Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00008157}
8158
8159// \brief Read a string
8160std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
8161 unsigned Len = Record[Idx++];
8162 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
8163 Idx += Len;
8164 return Result;
8165}
8166
Richard Smith7ed1bc92014-12-05 22:42:13 +00008167std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
8168 unsigned &Idx) {
8169 std::string Filename = ReadString(Record, Idx);
8170 ResolveImportedPath(F, Filename);
8171 return Filename;
8172}
8173
Guy Benyei11169dd2012-12-18 14:30:41 +00008174VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
8175 unsigned &Idx) {
8176 unsigned Major = Record[Idx++];
8177 unsigned Minor = Record[Idx++];
8178 unsigned Subminor = Record[Idx++];
8179 if (Minor == 0)
8180 return VersionTuple(Major);
8181 if (Subminor == 0)
8182 return VersionTuple(Major, Minor - 1);
8183 return VersionTuple(Major, Minor - 1, Subminor - 1);
8184}
8185
8186CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
8187 const RecordData &Record,
8188 unsigned &Idx) {
8189 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
8190 return CXXTemporary::Create(Context, Decl);
8191}
8192
8193DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Argyrios Kyrtzidisdc9fdaf2013-05-24 05:44:08 +00008194 return Diag(CurrentImportLoc, DiagID);
Guy Benyei11169dd2012-12-18 14:30:41 +00008195}
8196
8197DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
8198 return Diags.Report(Loc, DiagID);
8199}
8200
8201/// \brief Retrieve the identifier table associated with the
8202/// preprocessor.
8203IdentifierTable &ASTReader::getIdentifierTable() {
8204 return PP.getIdentifierTable();
8205}
8206
8207/// \brief Record that the given ID maps to the given switch-case
8208/// statement.
8209void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Craig Toppera13603a2014-05-22 05:54:18 +00008210 assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
Guy Benyei11169dd2012-12-18 14:30:41 +00008211 "Already have a SwitchCase with this ID");
8212 (*CurrSwitchCaseStmts)[ID] = SC;
8213}
8214
8215/// \brief Retrieve the switch-case statement with the given ID.
8216SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Craig Toppera13603a2014-05-22 05:54:18 +00008217 assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
Guy Benyei11169dd2012-12-18 14:30:41 +00008218 return (*CurrSwitchCaseStmts)[ID];
8219}
8220
8221void ASTReader::ClearSwitchCaseIDs() {
8222 CurrSwitchCaseStmts->clear();
8223}
8224
8225void ASTReader::ReadComments() {
8226 std::vector<RawComment *> Comments;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00008227 for (SmallVectorImpl<std::pair<BitstreamCursor,
Guy Benyei11169dd2012-12-18 14:30:41 +00008228 serialization::ModuleFile *> >::iterator
8229 I = CommentsCursors.begin(),
8230 E = CommentsCursors.end();
8231 I != E; ++I) {
Dmitri Gribenko9ee0e302014-03-27 15:40:39 +00008232 Comments.clear();
Chris Lattner7fb3bef2013-01-20 00:56:42 +00008233 BitstreamCursor &Cursor = I->first;
Guy Benyei11169dd2012-12-18 14:30:41 +00008234 serialization::ModuleFile &F = *I->second;
8235 SavedStreamPosition SavedPosition(Cursor);
8236
8237 RecordData Record;
8238 while (true) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00008239 llvm::BitstreamEntry Entry =
8240 Cursor.advanceSkippingSubblocks(BitstreamCursor::AF_DontPopBlockAtEnd);
Dmitri Gribenko9ee0e302014-03-27 15:40:39 +00008241
Chris Lattner7fb3bef2013-01-20 00:56:42 +00008242 switch (Entry.Kind) {
8243 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
8244 case llvm::BitstreamEntry::Error:
8245 Error("malformed block record in AST file");
8246 return;
8247 case llvm::BitstreamEntry::EndBlock:
8248 goto NextCursor;
8249 case llvm::BitstreamEntry::Record:
8250 // The interesting case.
Guy Benyei11169dd2012-12-18 14:30:41 +00008251 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00008252 }
8253
8254 // Read a record.
8255 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00008256 switch ((CommentRecordTypes)Cursor.readRecord(Entry.ID, Record)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008257 case COMMENTS_RAW_COMMENT: {
8258 unsigned Idx = 0;
8259 SourceRange SR = ReadSourceRange(F, Record, Idx);
8260 RawComment::CommentKind Kind =
8261 (RawComment::CommentKind) Record[Idx++];
8262 bool IsTrailingComment = Record[Idx++];
8263 bool IsAlmostTrailingComment = Record[Idx++];
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00008264 Comments.push_back(new (Context) RawComment(
8265 SR, Kind, IsTrailingComment, IsAlmostTrailingComment,
8266 Context.getLangOpts().CommentOpts.ParseAllComments));
Guy Benyei11169dd2012-12-18 14:30:41 +00008267 break;
8268 }
8269 }
8270 }
Dmitri Gribenko9ee0e302014-03-27 15:40:39 +00008271 NextCursor:
8272 Context.Comments.addDeserializedComments(Comments);
Guy Benyei11169dd2012-12-18 14:30:41 +00008273 }
Guy Benyei11169dd2012-12-18 14:30:41 +00008274}
8275
Richard Smithcd45dbc2014-04-19 03:48:30 +00008276std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) {
8277 // If we know the owning module, use it.
Richard Smith42413142015-05-15 20:05:43 +00008278 if (Module *M = D->getImportedOwningModule())
Richard Smithcd45dbc2014-04-19 03:48:30 +00008279 return M->getFullModuleName();
8280
8281 // Otherwise, use the name of the top-level module the decl is within.
8282 if (ModuleFile *M = getOwningModuleFile(D))
8283 return M->ModuleName;
8284
8285 // Not from a module.
8286 return "";
8287}
8288
Guy Benyei11169dd2012-12-18 14:30:41 +00008289void ASTReader::finishPendingActions() {
Richard Smith851072e2014-05-19 20:59:20 +00008290 while (!PendingIdentifierInfos.empty() ||
8291 !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() ||
Richard Smith2b9e3e32013-10-18 06:05:18 +00008292 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
Richard Smitha0ce9c42014-07-29 23:23:27 +00008293 !PendingUpdateRecords.empty()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008294 // If any identifiers with corresponding top-level declarations have
8295 // been loaded, load those declarations now.
Craig Topper79be4cd2013-07-05 04:33:53 +00008296 typedef llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2> >
8297 TopLevelDeclsMap;
8298 TopLevelDeclsMap TopLevelDecls;
8299
Guy Benyei11169dd2012-12-18 14:30:41 +00008300 while (!PendingIdentifierInfos.empty()) {
Douglas Gregor6168bd22013-02-18 15:53:43 +00008301 IdentifierInfo *II = PendingIdentifierInfos.back().first;
Richard Smithf0ae3c2d2014-03-28 17:31:23 +00008302 SmallVector<uint32_t, 4> DeclIDs =
8303 std::move(PendingIdentifierInfos.back().second);
Douglas Gregorcb15f082013-02-19 18:26:28 +00008304 PendingIdentifierInfos.pop_back();
Douglas Gregor6168bd22013-02-18 15:53:43 +00008305
8306 SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
Guy Benyei11169dd2012-12-18 14:30:41 +00008307 }
Richard Smithf0ae3c2d2014-03-28 17:31:23 +00008308
Richard Smith851072e2014-05-19 20:59:20 +00008309 // For each decl chain that we wanted to complete while deserializing, mark
8310 // it as "still needs to be completed".
8311 for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
8312 markIncompleteDeclChain(PendingIncompleteDeclChains[I]);
8313 }
8314 PendingIncompleteDeclChains.clear();
8315
Guy Benyei11169dd2012-12-18 14:30:41 +00008316 // Load pending declaration chains.
Richard Smithd8a83712015-08-22 01:47:18 +00008317 for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
Richard Smithd61d4ac2015-08-22 20:13:39 +00008318 loadPendingDeclChain(PendingDeclChains[I].first, PendingDeclChains[I].second);
Guy Benyei11169dd2012-12-18 14:30:41 +00008319 PendingDeclChains.clear();
8320
Douglas Gregor6168bd22013-02-18 15:53:43 +00008321 // Make the most recent of the top-level declarations visible.
Craig Topper79be4cd2013-07-05 04:33:53 +00008322 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
8323 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
Douglas Gregor6168bd22013-02-18 15:53:43 +00008324 IdentifierInfo *II = TLD->first;
8325 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +00008326 pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
Douglas Gregor6168bd22013-02-18 15:53:43 +00008327 }
8328 }
8329
Guy Benyei11169dd2012-12-18 14:30:41 +00008330 // Load any pending macro definitions.
8331 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00008332 IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
8333 SmallVector<PendingMacroInfo, 2> GlobalIDs;
8334 GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
8335 // Initialize the macro history from chained-PCHs ahead of module imports.
Richard Smith49f906a2014-03-01 00:08:04 +00008336 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00008337 ++IDIdx) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00008338 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
Richard Smithe842a472014-10-22 02:05:46 +00008339 if (Info.M->Kind != MK_ImplicitModule &&
8340 Info.M->Kind != MK_ExplicitModule)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00008341 resolvePendingMacro(II, Info);
8342 }
8343 // Handle module imports.
Richard Smith49f906a2014-03-01 00:08:04 +00008344 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00008345 ++IDIdx) {
8346 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
Richard Smithe842a472014-10-22 02:05:46 +00008347 if (Info.M->Kind == MK_ImplicitModule ||
8348 Info.M->Kind == MK_ExplicitModule)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00008349 resolvePendingMacro(II, Info);
Guy Benyei11169dd2012-12-18 14:30:41 +00008350 }
8351 }
8352 PendingMacroIDs.clear();
Argyrios Kyrtzidis83a6e3b2013-02-16 00:48:59 +00008353
8354 // Wire up the DeclContexts for Decls that we delayed setting until
8355 // recursive loading is completed.
8356 while (!PendingDeclContextInfos.empty()) {
8357 PendingDeclContextInfo Info = PendingDeclContextInfos.front();
8358 PendingDeclContextInfos.pop_front();
8359 DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
8360 DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
8361 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
8362 }
Richard Smith2b9e3e32013-10-18 06:05:18 +00008363
Richard Smithd1c46742014-04-30 02:24:17 +00008364 // Perform any pending declaration updates.
Richard Smithd6db68c2014-08-07 20:58:41 +00008365 while (!PendingUpdateRecords.empty()) {
Richard Smithd1c46742014-04-30 02:24:17 +00008366 auto Update = PendingUpdateRecords.pop_back_val();
8367 ReadingKindTracker ReadingKind(Read_Decl, *this);
8368 loadDeclUpdateRecords(Update.first, Update.second);
8369 }
Guy Benyei11169dd2012-12-18 14:30:41 +00008370 }
Richard Smith8a639892015-01-24 01:07:20 +00008371
8372 // At this point, all update records for loaded decls are in place, so any
8373 // fake class definitions should have become real.
8374 assert(PendingFakeDefinitionData.empty() &&
8375 "faked up a class definition but never saw the real one");
8376
Guy Benyei11169dd2012-12-18 14:30:41 +00008377 // If we deserialized any C++ or Objective-C class definitions, any
8378 // Objective-C protocol definitions, or any redeclarable templates, make sure
8379 // that all redeclarations point to the definitions. Note that this can only
8380 // happen now, after the redeclaration chains have been fully wired.
Craig Topperc6914d02014-08-25 04:15:02 +00008381 for (Decl *D : PendingDefinitions) {
8382 if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
Richard Smith5b21db82014-04-23 18:20:42 +00008383 if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008384 // Make sure that the TagType points at the definition.
8385 const_cast<TagType*>(TagT)->decl = TD;
8386 }
Richard Smith8ce51082015-03-11 01:44:51 +00008387
Craig Topperc6914d02014-08-25 04:15:02 +00008388 if (auto RD = dyn_cast<CXXRecordDecl>(D)) {
Richard Smith8ce51082015-03-11 01:44:51 +00008389 for (auto *R = getMostRecentExistingDecl(RD); R;
8390 R = R->getPreviousDecl()) {
8391 assert((R == D) ==
8392 cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
Richard Smith2c381642014-08-27 23:11:59 +00008393 "declaration thinks it's the definition but it isn't");
Aaron Ballman86c93902014-03-06 23:45:36 +00008394 cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
Richard Smith2c381642014-08-27 23:11:59 +00008395 }
Guy Benyei11169dd2012-12-18 14:30:41 +00008396 }
8397
8398 continue;
8399 }
Richard Smith8ce51082015-03-11 01:44:51 +00008400
Craig Topperc6914d02014-08-25 04:15:02 +00008401 if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008402 // Make sure that the ObjCInterfaceType points at the definition.
8403 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
8404 ->Decl = ID;
Richard Smith8ce51082015-03-11 01:44:51 +00008405
8406 for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl())
8407 cast<ObjCInterfaceDecl>(R)->Data = ID->Data;
8408
Guy Benyei11169dd2012-12-18 14:30:41 +00008409 continue;
8410 }
Richard Smith8ce51082015-03-11 01:44:51 +00008411
Craig Topperc6914d02014-08-25 04:15:02 +00008412 if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) {
Richard Smith8ce51082015-03-11 01:44:51 +00008413 for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl())
8414 cast<ObjCProtocolDecl>(R)->Data = PD->Data;
8415
Guy Benyei11169dd2012-12-18 14:30:41 +00008416 continue;
8417 }
Richard Smith8ce51082015-03-11 01:44:51 +00008418
Craig Topperc6914d02014-08-25 04:15:02 +00008419 auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl();
Richard Smith8ce51082015-03-11 01:44:51 +00008420 for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl())
8421 cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common;
Guy Benyei11169dd2012-12-18 14:30:41 +00008422 }
8423 PendingDefinitions.clear();
8424
8425 // Load the bodies of any functions or methods we've encountered. We do
8426 // this now (delayed) so that we can be sure that the declaration chains
Richard Smithb9fa9962015-08-21 03:04:33 +00008427 // have been fully wired up (hasBody relies on this).
8428 // FIXME: We shouldn't require complete redeclaration chains here.
Guy Benyei11169dd2012-12-18 14:30:41 +00008429 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
8430 PBEnd = PendingBodies.end();
8431 PB != PBEnd; ++PB) {
8432 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
8433 // FIXME: Check for =delete/=default?
8434 // FIXME: Complain about ODR violations here?
8435 if (!getContext().getLangOpts().Modules || !FD->hasBody())
8436 FD->setLazyBody(PB->second);
8437 continue;
8438 }
8439
8440 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
8441 if (!getContext().getLangOpts().Modules || !MD->hasBody())
8442 MD->setLazyBody(PB->second);
8443 }
8444 PendingBodies.clear();
Richard Smith42413142015-05-15 20:05:43 +00008445
8446 // Do some cleanup.
8447 for (auto *ND : PendingMergedDefinitionsToDeduplicate)
8448 getContext().deduplicateMergedDefinitonsFor(ND);
8449 PendingMergedDefinitionsToDeduplicate.clear();
Richard Smitha0ce9c42014-07-29 23:23:27 +00008450}
8451
8452void ASTReader::diagnoseOdrViolations() {
Richard Smithbb853c72014-08-13 01:23:33 +00008453 if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty())
8454 return;
8455
Richard Smitha0ce9c42014-07-29 23:23:27 +00008456 // Trigger the import of the full definition of each class that had any
8457 // odr-merging problems, so we can produce better diagnostics for them.
Richard Smithbb853c72014-08-13 01:23:33 +00008458 // These updates may in turn find and diagnose some ODR failures, so take
8459 // ownership of the set first.
8460 auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
8461 PendingOdrMergeFailures.clear();
8462 for (auto &Merge : OdrMergeFailures) {
Richard Smitha0ce9c42014-07-29 23:23:27 +00008463 Merge.first->buildLookup();
8464 Merge.first->decls_begin();
8465 Merge.first->bases_begin();
8466 Merge.first->vbases_begin();
8467 for (auto *RD : Merge.second) {
8468 RD->decls_begin();
8469 RD->bases_begin();
8470 RD->vbases_begin();
8471 }
8472 }
8473
8474 // For each declaration from a merged context, check that the canonical
8475 // definition of that context also contains a declaration of the same
8476 // entity.
8477 //
8478 // Caution: this loop does things that might invalidate iterators into
8479 // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
8480 while (!PendingOdrMergeChecks.empty()) {
8481 NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
8482
8483 // FIXME: Skip over implicit declarations for now. This matters for things
8484 // like implicitly-declared special member functions. This isn't entirely
8485 // correct; we can end up with multiple unmerged declarations of the same
8486 // implicit entity.
8487 if (D->isImplicit())
8488 continue;
8489
8490 DeclContext *CanonDef = D->getDeclContext();
Richard Smitha0ce9c42014-07-29 23:23:27 +00008491
8492 bool Found = false;
8493 const Decl *DCanon = D->getCanonicalDecl();
8494
Richard Smith01bdb7a2014-08-28 05:44:07 +00008495 for (auto RI : D->redecls()) {
8496 if (RI->getLexicalDeclContext() == CanonDef) {
8497 Found = true;
8498 break;
8499 }
8500 }
8501 if (Found)
8502 continue;
8503
Richard Smith0f4e2c42015-08-06 04:23:48 +00008504 // Quick check failed, time to do the slow thing. Note, we can't just
8505 // look up the name of D in CanonDef here, because the member that is
8506 // in CanonDef might not be found by name lookup (it might have been
8507 // replaced by a more recent declaration in the lookup table), and we
8508 // can't necessarily find it in the redeclaration chain because it might
8509 // be merely mergeable, not redeclarable.
Richard Smitha0ce9c42014-07-29 23:23:27 +00008510 llvm::SmallVector<const NamedDecl*, 4> Candidates;
Richard Smith0f4e2c42015-08-06 04:23:48 +00008511 for (auto *CanonMember : CanonDef->decls()) {
8512 if (CanonMember->getCanonicalDecl() == DCanon) {
8513 // This can happen if the declaration is merely mergeable and not
8514 // actually redeclarable (we looked for redeclarations earlier).
8515 //
8516 // FIXME: We should be able to detect this more efficiently, without
8517 // pulling in all of the members of CanonDef.
8518 Found = true;
8519 break;
Richard Smitha0ce9c42014-07-29 23:23:27 +00008520 }
Richard Smith0f4e2c42015-08-06 04:23:48 +00008521 if (auto *ND = dyn_cast<NamedDecl>(CanonMember))
8522 if (ND->getDeclName() == D->getDeclName())
8523 Candidates.push_back(ND);
Richard Smitha0ce9c42014-07-29 23:23:27 +00008524 }
8525
8526 if (!Found) {
Richard Smithd08aeb62014-08-28 01:33:39 +00008527 // The AST doesn't like TagDecls becoming invalid after they've been
8528 // completed. We only really need to mark FieldDecls as invalid here.
8529 if (!isa<TagDecl>(D))
8530 D->setInvalidDecl();
Richard Smith4ab3dbd2015-02-13 22:43:51 +00008531
8532 // Ensure we don't accidentally recursively enter deserialization while
8533 // we're producing our diagnostic.
8534 Deserializing RecursionGuard(this);
Richard Smitha0ce9c42014-07-29 23:23:27 +00008535
8536 std::string CanonDefModule =
8537 getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef));
8538 Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
8539 << D << getOwningModuleNameForDiagnostic(D)
8540 << CanonDef << CanonDefModule.empty() << CanonDefModule;
8541
8542 if (Candidates.empty())
8543 Diag(cast<Decl>(CanonDef)->getLocation(),
8544 diag::note_module_odr_violation_no_possible_decls) << D;
8545 else {
8546 for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
8547 Diag(Candidates[I]->getLocation(),
8548 diag::note_module_odr_violation_possible_decl)
8549 << Candidates[I];
8550 }
8551
8552 DiagnosedOdrMergeFailures.insert(CanonDef);
8553 }
8554 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00008555
Richard Smith4ab3dbd2015-02-13 22:43:51 +00008556 if (OdrMergeFailures.empty())
8557 return;
8558
8559 // Ensure we don't accidentally recursively enter deserialization while
8560 // we're producing our diagnostics.
8561 Deserializing RecursionGuard(this);
8562
Richard Smithcd45dbc2014-04-19 03:48:30 +00008563 // Issue any pending ODR-failure diagnostics.
Richard Smithbb853c72014-08-13 01:23:33 +00008564 for (auto &Merge : OdrMergeFailures) {
Richard Smitha0ce9c42014-07-29 23:23:27 +00008565 // If we've already pointed out a specific problem with this class, don't
8566 // bother issuing a general "something's different" diagnostic.
David Blaikie82e95a32014-11-19 07:49:47 +00008567 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
Richard Smithcd45dbc2014-04-19 03:48:30 +00008568 continue;
8569
8570 bool Diagnosed = false;
8571 for (auto *RD : Merge.second) {
8572 // Multiple different declarations got merged together; tell the user
8573 // where they came from.
8574 if (Merge.first != RD) {
8575 // FIXME: Walk the definition, figure out what's different,
8576 // and diagnose that.
8577 if (!Diagnosed) {
8578 std::string Module = getOwningModuleNameForDiagnostic(Merge.first);
8579 Diag(Merge.first->getLocation(),
8580 diag::err_module_odr_violation_different_definitions)
8581 << Merge.first << Module.empty() << Module;
8582 Diagnosed = true;
8583 }
8584
8585 Diag(RD->getLocation(),
8586 diag::note_module_odr_violation_different_definitions)
8587 << getOwningModuleNameForDiagnostic(RD);
8588 }
8589 }
8590
8591 if (!Diagnosed) {
8592 // All definitions are updates to the same declaration. This happens if a
8593 // module instantiates the declaration of a class template specialization
8594 // and two or more other modules instantiate its definition.
8595 //
8596 // FIXME: Indicate which modules had instantiations of this definition.
8597 // FIXME: How can this even happen?
8598 Diag(Merge.first->getLocation(),
8599 diag::err_module_odr_violation_different_instantiations)
8600 << Merge.first;
8601 }
8602 }
Guy Benyei11169dd2012-12-18 14:30:41 +00008603}
8604
Richard Smithce18a182015-07-14 00:26:00 +00008605void ASTReader::StartedDeserializing() {
8606 if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get())
8607 ReadTimer->startTimer();
8608}
8609
Guy Benyei11169dd2012-12-18 14:30:41 +00008610void ASTReader::FinishedDeserializing() {
8611 assert(NumCurrentElementsDeserializing &&
8612 "FinishedDeserializing not paired with StartedDeserializing");
8613 if (NumCurrentElementsDeserializing == 1) {
8614 // We decrease NumCurrentElementsDeserializing only after pending actions
8615 // are finished, to avoid recursively re-calling finishPendingActions().
8616 finishPendingActions();
8617 }
8618 --NumCurrentElementsDeserializing;
8619
Richard Smitha0ce9c42014-07-29 23:23:27 +00008620 if (NumCurrentElementsDeserializing == 0) {
Richard Smith9e2341d2015-03-23 03:25:59 +00008621 // Propagate exception specification updates along redeclaration chains.
Richard Smith7226f2a2015-03-23 19:54:56 +00008622 while (!PendingExceptionSpecUpdates.empty()) {
8623 auto Updates = std::move(PendingExceptionSpecUpdates);
8624 PendingExceptionSpecUpdates.clear();
8625 for (auto Update : Updates) {
8626 auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
Richard Smith1d0f1992015-08-19 21:09:32 +00008627 auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
Richard Smithd88a7f12015-09-01 20:35:42 +00008628 if (auto *Listener = Context.getASTMutationListener())
8629 Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second));
Richard Smith1d0f1992015-08-19 21:09:32 +00008630 for (auto *Redecl : Update.second->redecls())
8631 Context.adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI);
Richard Smith7226f2a2015-03-23 19:54:56 +00008632 }
Richard Smith9e2341d2015-03-23 03:25:59 +00008633 }
8634
Richard Smithce18a182015-07-14 00:26:00 +00008635 if (ReadTimer)
8636 ReadTimer->stopTimer();
8637
Richard Smith0f4e2c42015-08-06 04:23:48 +00008638 diagnoseOdrViolations();
8639
Richard Smith04d05b52014-03-23 00:27:18 +00008640 // We are not in recursive loading, so it's safe to pass the "interesting"
8641 // decls to the consumer.
Richard Smitha0ce9c42014-07-29 23:23:27 +00008642 if (Consumer)
8643 PassInterestingDeclsToConsumer();
Guy Benyei11169dd2012-12-18 14:30:41 +00008644 }
8645}
8646
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +00008647void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
Richard Smith9e2341d2015-03-23 03:25:59 +00008648 if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
8649 // Remove any fake results before adding any real ones.
8650 auto It = PendingFakeLookupResults.find(II);
8651 if (It != PendingFakeLookupResults.end()) {
Richard Smitha534a312015-07-21 23:54:07 +00008652 for (auto *ND : It->second)
Richard Smith9e2341d2015-03-23 03:25:59 +00008653 SemaObj->IdResolver.RemoveDecl(ND);
Ben Langmuireb8bd2d2015-04-10 22:25:42 +00008654 // FIXME: this works around module+PCH performance issue.
8655 // Rather than erase the result from the map, which is O(n), just clear
8656 // the vector of NamedDecls.
8657 It->second.clear();
Richard Smith9e2341d2015-03-23 03:25:59 +00008658 }
8659 }
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +00008660
8661 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
8662 SemaObj->TUScope->AddDecl(D);
8663 } else if (SemaObj->TUScope) {
8664 // Adding the decl to IdResolver may have failed because it was already in
8665 // (even though it was not added in scope). If it is already in, make sure
8666 // it gets in the scope as well.
8667 if (std::find(SemaObj->IdResolver.begin(Name),
8668 SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
8669 SemaObj->TUScope->AddDecl(D);
8670 }
8671}
8672
Douglas Gregor6623e1f2015-11-03 18:33:07 +00008673ASTReader::ASTReader(
8674 Preprocessor &PP, ASTContext &Context,
8675 const PCHContainerReader &PCHContainerRdr,
8676 ArrayRef<IntrusiveRefCntPtr<ModuleFileExtension>> Extensions,
8677 StringRef isysroot, bool DisableValidation,
8678 bool AllowASTWithCompilerErrors,
8679 bool AllowConfigurationMismatch, bool ValidateSystemInputs,
8680 bool UseGlobalIndex,
8681 std::unique_ptr<llvm::Timer> ReadTimer)
Craig Toppera13603a2014-05-22 05:54:18 +00008682 : Listener(new PCHValidator(PP, *this)), DeserializationListener(nullptr),
Nico Weber824285e2014-05-08 04:26:47 +00008683 OwnsDeserializationListener(false), SourceMgr(PP.getSourceManager()),
Adrian Prantlfb2398d2015-07-17 01:19:54 +00008684 FileMgr(PP.getFileManager()), PCHContainerRdr(PCHContainerRdr),
Adrian Prantlbb165fb2015-06-20 18:53:08 +00008685 Diags(PP.getDiagnostics()), SemaObj(nullptr), PP(PP), Context(Context),
Adrian Prantlfb2398d2015-07-17 01:19:54 +00008686 Consumer(nullptr), ModuleMgr(PP.getFileManager(), PCHContainerRdr),
Richard Smithce18a182015-07-14 00:26:00 +00008687 ReadTimer(std::move(ReadTimer)),
Adrian Prantlbb165fb2015-06-20 18:53:08 +00008688 isysroot(isysroot), DisableValidation(DisableValidation),
Nico Weber824285e2014-05-08 04:26:47 +00008689 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
8690 AllowConfigurationMismatch(AllowConfigurationMismatch),
8691 ValidateSystemInputs(ValidateSystemInputs),
8692 UseGlobalIndex(UseGlobalIndex), TriedLoadingGlobalIndex(false),
Adrian Prantlbb165fb2015-06-20 18:53:08 +00008693 CurrSwitchCaseStmts(&SwitchCaseStmts), NumSLocEntriesRead(0),
8694 TotalNumSLocEntries(0), NumStatementsRead(0), TotalNumStatements(0),
8695 NumMacrosRead(0), TotalNumMacros(0), NumIdentifierLookups(0),
8696 NumIdentifierLookupHits(0), NumSelectorsRead(0),
Nico Weber824285e2014-05-08 04:26:47 +00008697 NumMethodPoolEntriesRead(0), NumMethodPoolLookups(0),
8698 NumMethodPoolHits(0), NumMethodPoolTableLookups(0),
8699 NumMethodPoolTableHits(0), TotalNumMethodPoolEntries(0),
8700 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
8701 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
8702 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
Richard Smithc2bb8182015-03-24 06:36:48 +00008703 PassingDeclsToConsumer(false), ReadingKind(Read_None) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008704 SourceMgr.setExternalSLocEntrySource(this);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00008705
8706 for (const auto &Ext : Extensions) {
8707 auto BlockName = Ext->getExtensionMetadata().BlockName;
8708 auto Known = ModuleFileExtensions.find(BlockName);
8709 if (Known != ModuleFileExtensions.end()) {
8710 Diags.Report(diag::warn_duplicate_module_file_extension)
8711 << BlockName;
8712 continue;
8713 }
8714
8715 ModuleFileExtensions.insert({BlockName, Ext});
8716 }
Guy Benyei11169dd2012-12-18 14:30:41 +00008717}
8718
8719ASTReader::~ASTReader() {
Nico Weber824285e2014-05-08 04:26:47 +00008720 if (OwnsDeserializationListener)
8721 delete DeserializationListener;
Guy Benyei11169dd2012-12-18 14:30:41 +00008722}