blob: d75b5eb73d51b38209270df2f61b542483655839 [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"
Guy Benyei11169dd2012-12-18 14:30:41 +000023#include "clang/AST/NestedNameSpecifier.h"
24#include "clang/AST/Type.h"
25#include "clang/AST/TypeLocVisitor.h"
Benjamin Kramerf3ca26982014-05-10 16:31:55 +000026#include "clang/Basic/DiagnosticOptions.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000027#include "clang/Basic/FileManager.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000028#include "clang/Basic/SourceManager.h"
29#include "clang/Basic/SourceManagerInternals.h"
30#include "clang/Basic/TargetInfo.h"
31#include "clang/Basic/TargetOptions.h"
32#include "clang/Basic/Version.h"
33#include "clang/Basic/VersionTuple.h"
Ben Langmuirb92de022014-04-29 16:25:26 +000034#include "clang/Frontend/Utils.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000035#include "clang/Lex/HeaderSearch.h"
36#include "clang/Lex/HeaderSearchOptions.h"
37#include "clang/Lex/MacroInfo.h"
38#include "clang/Lex/PreprocessingRecord.h"
39#include "clang/Lex/Preprocessor.h"
40#include "clang/Lex/PreprocessorOptions.h"
41#include "clang/Sema/Scope.h"
42#include "clang/Sema/Sema.h"
43#include "clang/Serialization/ASTDeserializationListener.h"
Douglas Gregore060e572013-01-25 01:03:03 +000044#include "clang/Serialization/GlobalModuleIndex.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000045#include "clang/Serialization/ModuleManager.h"
46#include "clang/Serialization/SerializationDiagnostic.h"
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +000047#include "llvm/ADT/Hashing.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000048#include "llvm/ADT/StringExtras.h"
49#include "llvm/Bitcode/BitstreamReader.h"
50#include "llvm/Support/ErrorHandling.h"
51#include "llvm/Support/FileSystem.h"
52#include "llvm/Support/MemoryBuffer.h"
53#include "llvm/Support/Path.h"
54#include "llvm/Support/SaveAndRestore.h"
Dmitri Gribenkof430da42014-02-12 10:33:14 +000055#include "llvm/Support/raw_ostream.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000056#include <algorithm>
Chris Lattner91f373e2013-01-20 00:57:52 +000057#include <cstdio>
Guy Benyei11169dd2012-12-18 14:30:41 +000058#include <iterator>
Rafael Espindola8a8e5542014-06-12 17:19:42 +000059#include <system_error>
Guy Benyei11169dd2012-12-18 14:30:41 +000060
61using namespace clang;
62using namespace clang::serialization;
63using namespace clang::serialization::reader;
Chris Lattner7fb3bef2013-01-20 00:56:42 +000064using llvm::BitstreamCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +000065
Ben Langmuircb69b572014-03-07 06:40:32 +000066
67//===----------------------------------------------------------------------===//
68// ChainedASTReaderListener implementation
69//===----------------------------------------------------------------------===//
70
71bool
72ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) {
73 return First->ReadFullVersionInformation(FullVersion) ||
74 Second->ReadFullVersionInformation(FullVersion);
75}
Ben Langmuir4f5212a2014-04-14 22:12:44 +000076void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) {
77 First->ReadModuleName(ModuleName);
78 Second->ReadModuleName(ModuleName);
79}
80void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) {
81 First->ReadModuleMapFile(ModuleMapPath);
82 Second->ReadModuleMapFile(ModuleMapPath);
83}
Richard Smith1e2cf0d2014-10-31 02:28:58 +000084bool
85ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts,
86 bool Complain,
87 bool AllowCompatibleDifferences) {
88 return First->ReadLanguageOptions(LangOpts, Complain,
89 AllowCompatibleDifferences) ||
90 Second->ReadLanguageOptions(LangOpts, Complain,
91 AllowCompatibleDifferences);
Ben Langmuircb69b572014-03-07 06:40:32 +000092}
Chandler Carruth0d745bc2015-03-14 04:47:43 +000093bool ChainedASTReaderListener::ReadTargetOptions(
94 const TargetOptions &TargetOpts, bool Complain,
95 bool AllowCompatibleDifferences) {
96 return First->ReadTargetOptions(TargetOpts, Complain,
97 AllowCompatibleDifferences) ||
98 Second->ReadTargetOptions(TargetOpts, Complain,
99 AllowCompatibleDifferences);
Ben Langmuircb69b572014-03-07 06:40:32 +0000100}
101bool ChainedASTReaderListener::ReadDiagnosticOptions(
Ben Langmuirb92de022014-04-29 16:25:26 +0000102 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
Ben Langmuircb69b572014-03-07 06:40:32 +0000103 return First->ReadDiagnosticOptions(DiagOpts, Complain) ||
104 Second->ReadDiagnosticOptions(DiagOpts, Complain);
105}
106bool
107ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts,
108 bool Complain) {
109 return First->ReadFileSystemOptions(FSOpts, Complain) ||
110 Second->ReadFileSystemOptions(FSOpts, Complain);
111}
112
113bool ChainedASTReaderListener::ReadHeaderSearchOptions(
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +0000114 const HeaderSearchOptions &HSOpts, StringRef SpecificModuleCachePath,
115 bool Complain) {
116 return First->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
117 Complain) ||
118 Second->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
119 Complain);
Ben Langmuircb69b572014-03-07 06:40:32 +0000120}
121bool ChainedASTReaderListener::ReadPreprocessorOptions(
122 const PreprocessorOptions &PPOpts, bool Complain,
123 std::string &SuggestedPredefines) {
124 return First->ReadPreprocessorOptions(PPOpts, Complain,
125 SuggestedPredefines) ||
126 Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines);
127}
128void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M,
129 unsigned Value) {
130 First->ReadCounter(M, Value);
131 Second->ReadCounter(M, Value);
132}
133bool ChainedASTReaderListener::needsInputFileVisitation() {
134 return First->needsInputFileVisitation() ||
135 Second->needsInputFileVisitation();
136}
137bool ChainedASTReaderListener::needsSystemInputFileVisitation() {
138 return First->needsSystemInputFileVisitation() ||
139 Second->needsSystemInputFileVisitation();
140}
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +0000141void ChainedASTReaderListener::visitModuleFile(StringRef Filename) {
142 First->visitModuleFile(Filename);
143 Second->visitModuleFile(Filename);
144}
Ben Langmuircb69b572014-03-07 06:40:32 +0000145bool ChainedASTReaderListener::visitInputFile(StringRef Filename,
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +0000146 bool isSystem,
147 bool isOverridden) {
Justin Bognerc65a66d2014-05-22 06:04:59 +0000148 bool Continue = false;
149 if (First->needsInputFileVisitation() &&
150 (!isSystem || First->needsSystemInputFileVisitation()))
151 Continue |= First->visitInputFile(Filename, isSystem, isOverridden);
152 if (Second->needsInputFileVisitation() &&
153 (!isSystem || Second->needsSystemInputFileVisitation()))
154 Continue |= Second->visitInputFile(Filename, isSystem, isOverridden);
155 return Continue;
Ben Langmuircb69b572014-03-07 06:40:32 +0000156}
157
Guy Benyei11169dd2012-12-18 14:30:41 +0000158//===----------------------------------------------------------------------===//
159// PCH validator implementation
160//===----------------------------------------------------------------------===//
161
162ASTReaderListener::~ASTReaderListener() {}
163
164/// \brief Compare the given set of language options against an existing set of
165/// language options.
166///
167/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
Richard Smith1e2cf0d2014-10-31 02:28:58 +0000168/// \param AllowCompatibleDifferences If true, differences between compatible
169/// language options will be permitted.
Guy Benyei11169dd2012-12-18 14:30:41 +0000170///
171/// \returns true if the languagae options mis-match, false otherwise.
172static bool checkLanguageOptions(const LangOptions &LangOpts,
173 const LangOptions &ExistingLangOpts,
Richard Smith1e2cf0d2014-10-31 02:28:58 +0000174 DiagnosticsEngine *Diags,
175 bool AllowCompatibleDifferences = true) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000176#define LANGOPT(Name, Bits, Default, Description) \
177 if (ExistingLangOpts.Name != LangOpts.Name) { \
178 if (Diags) \
179 Diags->Report(diag::err_pch_langopt_mismatch) \
180 << Description << LangOpts.Name << ExistingLangOpts.Name; \
181 return true; \
182 }
183
184#define VALUE_LANGOPT(Name, Bits, Default, Description) \
185 if (ExistingLangOpts.Name != LangOpts.Name) { \
186 if (Diags) \
187 Diags->Report(diag::err_pch_langopt_value_mismatch) \
188 << Description; \
189 return true; \
190 }
191
192#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
193 if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \
194 if (Diags) \
195 Diags->Report(diag::err_pch_langopt_value_mismatch) \
196 << Description; \
197 return true; \
198 }
199
Richard Smith1e2cf0d2014-10-31 02:28:58 +0000200#define COMPATIBLE_LANGOPT(Name, Bits, Default, Description) \
201 if (!AllowCompatibleDifferences) \
202 LANGOPT(Name, Bits, Default, Description)
203
204#define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description) \
205 if (!AllowCompatibleDifferences) \
206 ENUM_LANGOPT(Name, Bits, Default, Description)
207
Guy Benyei11169dd2012-12-18 14:30:41 +0000208#define BENIGN_LANGOPT(Name, Bits, Default, Description)
209#define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
210#include "clang/Basic/LangOptions.def"
211
212 if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
213 if (Diags)
214 Diags->Report(diag::err_pch_langopt_value_mismatch)
215 << "target Objective-C runtime";
216 return true;
217 }
218
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +0000219 if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
220 LangOpts.CommentOpts.BlockCommandNames) {
221 if (Diags)
222 Diags->Report(diag::err_pch_langopt_value_mismatch)
223 << "block command names";
224 return true;
225 }
226
Guy Benyei11169dd2012-12-18 14:30:41 +0000227 return false;
228}
229
230/// \brief Compare the given set of target options against an existing set of
231/// target options.
232///
233/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
234///
235/// \returns true if the target options mis-match, false otherwise.
236static bool checkTargetOptions(const TargetOptions &TargetOpts,
237 const TargetOptions &ExistingTargetOpts,
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000238 DiagnosticsEngine *Diags,
239 bool AllowCompatibleDifferences = true) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000240#define CHECK_TARGET_OPT(Field, Name) \
241 if (TargetOpts.Field != ExistingTargetOpts.Field) { \
242 if (Diags) \
243 Diags->Report(diag::err_pch_targetopt_mismatch) \
244 << Name << TargetOpts.Field << ExistingTargetOpts.Field; \
245 return true; \
246 }
247
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000248 // The triple and ABI must match exactly.
Guy Benyei11169dd2012-12-18 14:30:41 +0000249 CHECK_TARGET_OPT(Triple, "target");
Guy Benyei11169dd2012-12-18 14:30:41 +0000250 CHECK_TARGET_OPT(ABI, "target ABI");
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000251
252 // We can tolerate different CPUs in many cases, notably when one CPU
253 // supports a strict superset of another. When allowing compatible
254 // differences skip this check.
255 if (!AllowCompatibleDifferences)
256 CHECK_TARGET_OPT(CPU, "target CPU");
257
Guy Benyei11169dd2012-12-18 14:30:41 +0000258#undef CHECK_TARGET_OPT
259
260 // Compare feature sets.
261 SmallVector<StringRef, 4> ExistingFeatures(
262 ExistingTargetOpts.FeaturesAsWritten.begin(),
263 ExistingTargetOpts.FeaturesAsWritten.end());
264 SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
265 TargetOpts.FeaturesAsWritten.end());
266 std::sort(ExistingFeatures.begin(), ExistingFeatures.end());
267 std::sort(ReadFeatures.begin(), ReadFeatures.end());
268
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000269 // We compute the set difference in both directions explicitly so that we can
270 // diagnose the differences differently.
271 SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures;
272 std::set_difference(
273 ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(),
274 ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures));
275 std::set_difference(ReadFeatures.begin(), ReadFeatures.end(),
276 ExistingFeatures.begin(), ExistingFeatures.end(),
277 std::back_inserter(UnmatchedReadFeatures));
Guy Benyei11169dd2012-12-18 14:30:41 +0000278
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000279 // If we are allowing compatible differences and the read feature set is
280 // a strict subset of the existing feature set, there is nothing to diagnose.
281 if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty())
282 return false;
Guy Benyei11169dd2012-12-18 14:30:41 +0000283
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000284 if (Diags) {
285 for (StringRef Feature : UnmatchedReadFeatures)
Guy Benyei11169dd2012-12-18 14:30:41 +0000286 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000287 << /* is-existing-feature */ false << Feature;
288 for (StringRef Feature : UnmatchedExistingFeatures)
289 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
290 << /* is-existing-feature */ true << Feature;
Guy Benyei11169dd2012-12-18 14:30:41 +0000291 }
292
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000293 return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty();
Guy Benyei11169dd2012-12-18 14:30:41 +0000294}
295
296bool
297PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
Richard Smith1e2cf0d2014-10-31 02:28:58 +0000298 bool Complain,
299 bool AllowCompatibleDifferences) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000300 const LangOptions &ExistingLangOpts = PP.getLangOpts();
301 return checkLanguageOptions(LangOpts, ExistingLangOpts,
Richard Smith1e2cf0d2014-10-31 02:28:58 +0000302 Complain ? &Reader.Diags : nullptr,
303 AllowCompatibleDifferences);
Guy Benyei11169dd2012-12-18 14:30:41 +0000304}
305
306bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000307 bool Complain,
308 bool AllowCompatibleDifferences) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000309 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
310 return checkTargetOptions(TargetOpts, ExistingTargetOpts,
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000311 Complain ? &Reader.Diags : nullptr,
312 AllowCompatibleDifferences);
Guy Benyei11169dd2012-12-18 14:30:41 +0000313}
314
315namespace {
316 typedef llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> >
317 MacroDefinitionsMap;
Craig Topper3598eb72013-07-05 04:43:31 +0000318 typedef llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> >
319 DeclsMap;
Guy Benyei11169dd2012-12-18 14:30:41 +0000320}
321
Ben Langmuirb92de022014-04-29 16:25:26 +0000322static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags,
323 DiagnosticsEngine &Diags,
324 bool Complain) {
325 typedef DiagnosticsEngine::Level Level;
326
327 // Check current mappings for new -Werror mappings, and the stored mappings
328 // for cases that were explicitly mapped to *not* be errors that are now
329 // errors because of options like -Werror.
330 DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags };
331
332 for (DiagnosticsEngine *MappingSource : MappingSources) {
333 for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {
334 diag::kind DiagID = DiagIDMappingPair.first;
335 Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation());
336 if (CurLevel < DiagnosticsEngine::Error)
337 continue; // not significant
338 Level StoredLevel =
339 StoredDiags.getDiagnosticLevel(DiagID, SourceLocation());
340 if (StoredLevel < DiagnosticsEngine::Error) {
341 if (Complain)
342 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" +
343 Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str();
344 return true;
345 }
346 }
347 }
348
349 return false;
350}
351
Alp Tokerac4e8e52014-06-22 21:58:33 +0000352static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) {
353 diag::Severity Ext = Diags.getExtensionHandlingBehavior();
354 if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors())
355 return true;
356 return Ext >= diag::Severity::Error;
Ben Langmuirb92de022014-04-29 16:25:26 +0000357}
358
359static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags,
360 DiagnosticsEngine &Diags,
361 bool IsSystem, bool Complain) {
362 // Top-level options
363 if (IsSystem) {
364 if (Diags.getSuppressSystemWarnings())
365 return false;
366 // If -Wsystem-headers was not enabled before, be conservative
367 if (StoredDiags.getSuppressSystemWarnings()) {
368 if (Complain)
369 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers";
370 return true;
371 }
372 }
373
374 if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) {
375 if (Complain)
376 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror";
377 return true;
378 }
379
380 if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() &&
381 !StoredDiags.getEnableAllWarnings()) {
382 if (Complain)
383 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror";
384 return true;
385 }
386
387 if (isExtHandlingFromDiagsError(Diags) &&
388 !isExtHandlingFromDiagsError(StoredDiags)) {
389 if (Complain)
390 Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors";
391 return true;
392 }
393
394 return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain);
395}
396
397bool PCHValidator::ReadDiagnosticOptions(
398 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
399 DiagnosticsEngine &ExistingDiags = PP.getDiagnostics();
400 IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs());
401 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
Alp Tokerf994cef2014-07-05 03:08:06 +0000402 new DiagnosticsEngine(DiagIDs, DiagOpts.get()));
Ben Langmuirb92de022014-04-29 16:25:26 +0000403 // This should never fail, because we would have processed these options
404 // before writing them to an ASTFile.
405 ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false);
406
407 ModuleManager &ModuleMgr = Reader.getModuleManager();
408 assert(ModuleMgr.size() >= 1 && "what ASTFile is this then");
409
410 // If the original import came from a file explicitly generated by the user,
411 // don't check the diagnostic mappings.
412 // FIXME: currently this is approximated by checking whether this is not a
Richard Smithe842a472014-10-22 02:05:46 +0000413 // module import of an implicitly-loaded module file.
Ben Langmuirb92de022014-04-29 16:25:26 +0000414 // Note: ModuleMgr.rbegin() may not be the current module, but it must be in
415 // the transitive closure of its imports, since unrelated modules cannot be
416 // imported until after this module finishes validation.
417 ModuleFile *TopImport = *ModuleMgr.rbegin();
418 while (!TopImport->ImportedBy.empty())
419 TopImport = TopImport->ImportedBy[0];
Richard Smithe842a472014-10-22 02:05:46 +0000420 if (TopImport->Kind != MK_ImplicitModule)
Ben Langmuirb92de022014-04-29 16:25:26 +0000421 return false;
422
423 StringRef ModuleName = TopImport->ModuleName;
424 assert(!ModuleName.empty() && "diagnostic options read before module name");
425
426 Module *M = PP.getHeaderSearchInfo().lookupModule(ModuleName);
427 assert(M && "missing module");
428
429 // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that
430 // contains the union of their flags.
431 return checkDiagnosticMappings(*Diags, ExistingDiags, M->IsSystem, Complain);
432}
433
Guy Benyei11169dd2012-12-18 14:30:41 +0000434/// \brief Collect the macro definitions provided by the given preprocessor
435/// options.
Craig Toppera13603a2014-05-22 05:54:18 +0000436static void
437collectMacroDefinitions(const PreprocessorOptions &PPOpts,
438 MacroDefinitionsMap &Macros,
439 SmallVectorImpl<StringRef> *MacroNames = nullptr) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000440 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
441 StringRef Macro = PPOpts.Macros[I].first;
442 bool IsUndef = PPOpts.Macros[I].second;
443
444 std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
445 StringRef MacroName = MacroPair.first;
446 StringRef MacroBody = MacroPair.second;
447
448 // For an #undef'd macro, we only care about the name.
449 if (IsUndef) {
450 if (MacroNames && !Macros.count(MacroName))
451 MacroNames->push_back(MacroName);
452
453 Macros[MacroName] = std::make_pair("", true);
454 continue;
455 }
456
457 // For a #define'd macro, figure out the actual definition.
458 if (MacroName.size() == Macro.size())
459 MacroBody = "1";
460 else {
461 // Note: GCC drops anything following an end-of-line character.
462 StringRef::size_type End = MacroBody.find_first_of("\n\r");
463 MacroBody = MacroBody.substr(0, End);
464 }
465
466 if (MacroNames && !Macros.count(MacroName))
467 MacroNames->push_back(MacroName);
468 Macros[MacroName] = std::make_pair(MacroBody, false);
469 }
470}
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +0000471
Guy Benyei11169dd2012-12-18 14:30:41 +0000472/// \brief Check the preprocessor options deserialized from the control block
473/// against the preprocessor options in an existing preprocessor.
474///
475/// \param Diags If non-null, produce diagnostics for any mismatches incurred.
476static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts,
477 const PreprocessorOptions &ExistingPPOpts,
478 DiagnosticsEngine *Diags,
479 FileManager &FileMgr,
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +0000480 std::string &SuggestedPredefines,
481 const LangOptions &LangOpts) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000482 // Check macro definitions.
483 MacroDefinitionsMap ASTFileMacros;
484 collectMacroDefinitions(PPOpts, ASTFileMacros);
485 MacroDefinitionsMap ExistingMacros;
486 SmallVector<StringRef, 4> ExistingMacroNames;
487 collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames);
488
489 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
490 // Dig out the macro definition in the existing preprocessor options.
491 StringRef MacroName = ExistingMacroNames[I];
492 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
493
494 // Check whether we know anything about this macro name or not.
495 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> >::iterator Known
496 = ASTFileMacros.find(MacroName);
497 if (Known == ASTFileMacros.end()) {
498 // FIXME: Check whether this identifier was referenced anywhere in the
499 // AST file. If so, we should reject the AST file. Unfortunately, this
500 // information isn't in the control block. What shall we do about it?
501
502 if (Existing.second) {
503 SuggestedPredefines += "#undef ";
504 SuggestedPredefines += MacroName.str();
505 SuggestedPredefines += '\n';
506 } else {
507 SuggestedPredefines += "#define ";
508 SuggestedPredefines += MacroName.str();
509 SuggestedPredefines += ' ';
510 SuggestedPredefines += Existing.first.str();
511 SuggestedPredefines += '\n';
512 }
513 continue;
514 }
515
516 // If the macro was defined in one but undef'd in the other, we have a
517 // conflict.
518 if (Existing.second != Known->second.second) {
519 if (Diags) {
520 Diags->Report(diag::err_pch_macro_def_undef)
521 << MacroName << Known->second.second;
522 }
523 return true;
524 }
525
526 // If the macro was #undef'd in both, or if the macro bodies are identical,
527 // it's fine.
528 if (Existing.second || Existing.first == Known->second.first)
529 continue;
530
531 // The macro bodies differ; complain.
532 if (Diags) {
533 Diags->Report(diag::err_pch_macro_def_conflict)
534 << MacroName << Known->second.first << Existing.first;
535 }
536 return true;
537 }
538
539 // Check whether we're using predefines.
540 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines) {
541 if (Diags) {
542 Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
543 }
544 return true;
545 }
546
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +0000547 // Detailed record is important since it is used for the module cache hash.
548 if (LangOpts.Modules &&
549 PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord) {
550 if (Diags) {
551 Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord;
552 }
553 return true;
554 }
555
Guy Benyei11169dd2012-12-18 14:30:41 +0000556 // Compute the #include and #include_macros lines we need.
557 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
558 StringRef File = ExistingPPOpts.Includes[I];
559 if (File == ExistingPPOpts.ImplicitPCHInclude)
560 continue;
561
562 if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File)
563 != PPOpts.Includes.end())
564 continue;
565
566 SuggestedPredefines += "#include \"";
Manuel Klimek9af34ae2014-08-12 08:25:57 +0000567 SuggestedPredefines += File;
Guy Benyei11169dd2012-12-18 14:30:41 +0000568 SuggestedPredefines += "\"\n";
569 }
570
571 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
572 StringRef File = ExistingPPOpts.MacroIncludes[I];
573 if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(),
574 File)
575 != PPOpts.MacroIncludes.end())
576 continue;
577
578 SuggestedPredefines += "#__include_macros \"";
Manuel Klimek9af34ae2014-08-12 08:25:57 +0000579 SuggestedPredefines += File;
Guy Benyei11169dd2012-12-18 14:30:41 +0000580 SuggestedPredefines += "\"\n##\n";
581 }
582
583 return false;
584}
585
586bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
587 bool Complain,
588 std::string &SuggestedPredefines) {
589 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
590
591 return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
Craig Toppera13603a2014-05-22 05:54:18 +0000592 Complain? &Reader.Diags : nullptr,
Guy Benyei11169dd2012-12-18 14:30:41 +0000593 PP.getFileManager(),
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +0000594 SuggestedPredefines,
595 PP.getLangOpts());
Guy Benyei11169dd2012-12-18 14:30:41 +0000596}
597
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +0000598/// Check the header search options deserialized from the control block
599/// against the header search options in an existing preprocessor.
600///
601/// \param Diags If non-null, produce diagnostics for any mismatches incurred.
602static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
603 StringRef SpecificModuleCachePath,
604 StringRef ExistingModuleCachePath,
605 DiagnosticsEngine *Diags,
606 const LangOptions &LangOpts) {
607 if (LangOpts.Modules) {
608 if (SpecificModuleCachePath != ExistingModuleCachePath) {
609 if (Diags)
610 Diags->Report(diag::err_pch_modulecache_mismatch)
611 << SpecificModuleCachePath << ExistingModuleCachePath;
612 return true;
613 }
614 }
615
616 return false;
617}
618
619bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
620 StringRef SpecificModuleCachePath,
621 bool Complain) {
622 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
623 PP.getHeaderSearchInfo().getModuleCachePath(),
624 Complain ? &Reader.Diags : nullptr,
625 PP.getLangOpts());
626}
627
Guy Benyei11169dd2012-12-18 14:30:41 +0000628void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
629 PP.setCounterValue(Value);
630}
631
632//===----------------------------------------------------------------------===//
633// AST reader implementation
634//===----------------------------------------------------------------------===//
635
Nico Weber824285e2014-05-08 04:26:47 +0000636void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,
637 bool TakeOwnership) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000638 DeserializationListener = Listener;
Nico Weber824285e2014-05-08 04:26:47 +0000639 OwnsDeserializationListener = TakeOwnership;
Guy Benyei11169dd2012-12-18 14:30:41 +0000640}
641
642
643
644unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
645 return serialization::ComputeHash(Sel);
646}
647
648
649std::pair<unsigned, unsigned>
650ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000651 using namespace llvm::support;
652 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
653 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
Guy Benyei11169dd2012-12-18 14:30:41 +0000654 return std::make_pair(KeyLen, DataLen);
655}
656
657ASTSelectorLookupTrait::internal_key_type
658ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000659 using namespace llvm::support;
Guy Benyei11169dd2012-12-18 14:30:41 +0000660 SelectorTable &SelTable = Reader.getContext().Selectors;
Justin Bogner57ba0b22014-03-28 22:03:24 +0000661 unsigned N = endian::readNext<uint16_t, little, unaligned>(d);
662 IdentifierInfo *FirstII = Reader.getLocalIdentifier(
663 F, endian::readNext<uint32_t, little, unaligned>(d));
Guy Benyei11169dd2012-12-18 14:30:41 +0000664 if (N == 0)
665 return SelTable.getNullarySelector(FirstII);
666 else if (N == 1)
667 return SelTable.getUnarySelector(FirstII);
668
669 SmallVector<IdentifierInfo *, 16> Args;
670 Args.push_back(FirstII);
671 for (unsigned I = 1; I != N; ++I)
Justin Bogner57ba0b22014-03-28 22:03:24 +0000672 Args.push_back(Reader.getLocalIdentifier(
673 F, endian::readNext<uint32_t, little, unaligned>(d)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000674
675 return SelTable.getSelector(N, Args.data());
676}
677
678ASTSelectorLookupTrait::data_type
679ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
680 unsigned DataLen) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000681 using namespace llvm::support;
Guy Benyei11169dd2012-12-18 14:30:41 +0000682
683 data_type Result;
684
Justin Bogner57ba0b22014-03-28 22:03:24 +0000685 Result.ID = Reader.getGlobalSelectorID(
686 F, endian::readNext<uint32_t, little, unaligned>(d));
Nico Weberff4b35e2014-12-27 22:14:15 +0000687 unsigned FullInstanceBits = endian::readNext<uint16_t, little, unaligned>(d);
688 unsigned FullFactoryBits = endian::readNext<uint16_t, little, unaligned>(d);
689 Result.InstanceBits = FullInstanceBits & 0x3;
690 Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1;
691 Result.FactoryBits = FullFactoryBits & 0x3;
692 Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1;
693 unsigned NumInstanceMethods = FullInstanceBits >> 3;
694 unsigned NumFactoryMethods = FullFactoryBits >> 3;
Guy Benyei11169dd2012-12-18 14:30:41 +0000695
696 // Load instance methods
697 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000698 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
699 F, endian::readNext<uint32_t, little, unaligned>(d)))
Guy Benyei11169dd2012-12-18 14:30:41 +0000700 Result.Instance.push_back(Method);
701 }
702
703 // Load factory methods
704 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000705 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
706 F, endian::readNext<uint32_t, little, unaligned>(d)))
Guy Benyei11169dd2012-12-18 14:30:41 +0000707 Result.Factory.push_back(Method);
708 }
709
710 return Result;
711}
712
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000713unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
714 return llvm::HashString(a);
Guy Benyei11169dd2012-12-18 14:30:41 +0000715}
716
717std::pair<unsigned, unsigned>
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000718ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000719 using namespace llvm::support;
720 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
721 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
Guy Benyei11169dd2012-12-18 14:30:41 +0000722 return std::make_pair(KeyLen, DataLen);
723}
724
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000725ASTIdentifierLookupTraitBase::internal_key_type
726ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000727 assert(n >= 2 && d[n-1] == '\0');
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000728 return StringRef((const char*) d, n-1);
Guy Benyei11169dd2012-12-18 14:30:41 +0000729}
730
Douglas Gregordcf25082013-02-11 18:16:18 +0000731/// \brief Whether the given identifier is "interesting".
732static bool isInterestingIdentifier(IdentifierInfo &II) {
733 return II.isPoisoned() ||
734 II.isExtensionToken() ||
735 II.getObjCOrBuiltinID() ||
736 II.hasRevertedTokenIDToIdentifier() ||
737 II.hadMacroDefinition() ||
738 II.getFETokenInfo<void>();
739}
740
Guy Benyei11169dd2012-12-18 14:30:41 +0000741IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
742 const unsigned char* d,
743 unsigned DataLen) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000744 using namespace llvm::support;
745 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
Guy Benyei11169dd2012-12-18 14:30:41 +0000746 bool IsInteresting = RawID & 0x01;
747
748 // Wipe out the "is interesting" bit.
749 RawID = RawID >> 1;
750
751 IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
752 if (!IsInteresting) {
753 // For uninteresting identifiers, just build the IdentifierInfo
754 // and associate it with the persistent ID.
755 IdentifierInfo *II = KnownII;
756 if (!II) {
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000757 II = &Reader.getIdentifierTable().getOwn(k);
Guy Benyei11169dd2012-12-18 14:30:41 +0000758 KnownII = II;
759 }
760 Reader.SetIdentifierInfo(ID, II);
Douglas Gregordcf25082013-02-11 18:16:18 +0000761 if (!II->isFromAST()) {
762 bool WasInteresting = isInterestingIdentifier(*II);
763 II->setIsFromAST();
764 if (WasInteresting)
765 II->setChangedSinceDeserialization();
766 }
767 Reader.markIdentifierUpToDate(II);
Guy Benyei11169dd2012-12-18 14:30:41 +0000768 return II;
769 }
770
Justin Bogner57ba0b22014-03-28 22:03:24 +0000771 unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d);
772 unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d);
Guy Benyei11169dd2012-12-18 14:30:41 +0000773 bool CPlusPlusOperatorKeyword = Bits & 0x01;
774 Bits >>= 1;
775 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
776 Bits >>= 1;
777 bool Poisoned = Bits & 0x01;
778 Bits >>= 1;
779 bool ExtensionToken = Bits & 0x01;
780 Bits >>= 1;
781 bool hadMacroDefinition = Bits & 0x01;
782 Bits >>= 1;
783
784 assert(Bits == 0 && "Extra bits in the identifier?");
785 DataLen -= 8;
786
787 // Build the IdentifierInfo itself and link the identifier ID with
788 // the new IdentifierInfo.
789 IdentifierInfo *II = KnownII;
790 if (!II) {
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000791 II = &Reader.getIdentifierTable().getOwn(StringRef(k));
Guy Benyei11169dd2012-12-18 14:30:41 +0000792 KnownII = II;
793 }
794 Reader.markIdentifierUpToDate(II);
Douglas Gregordcf25082013-02-11 18:16:18 +0000795 if (!II->isFromAST()) {
796 bool WasInteresting = isInterestingIdentifier(*II);
797 II->setIsFromAST();
798 if (WasInteresting)
799 II->setChangedSinceDeserialization();
800 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000801
802 // Set or check the various bits in the IdentifierInfo structure.
803 // Token IDs are read-only.
Argyrios Kyrtzidisddee8c92013-02-27 01:13:51 +0000804 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
Guy Benyei11169dd2012-12-18 14:30:41 +0000805 II->RevertTokenIDToIdentifier();
806 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
807 assert(II->isExtensionToken() == ExtensionToken &&
808 "Incorrect extension token flag");
809 (void)ExtensionToken;
810 if (Poisoned)
811 II->setIsPoisoned(true);
812 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
813 "Incorrect C++ operator keyword flag");
814 (void)CPlusPlusOperatorKeyword;
815
816 // If this identifier is a macro, deserialize the macro
817 // definition.
818 if (hadMacroDefinition) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000819 uint32_t MacroDirectivesOffset =
820 endian::readNext<uint32_t, little, unaligned>(d);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +0000821 DataLen -= 4;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +0000822
Richard Smithd7329392015-04-21 21:46:32 +0000823 Reader.addPendingMacro(II, &F, MacroDirectivesOffset);
Guy Benyei11169dd2012-12-18 14:30:41 +0000824 }
825
826 Reader.SetIdentifierInfo(ID, II);
827
828 // Read all of the declarations visible at global scope with this
829 // name.
830 if (DataLen > 0) {
831 SmallVector<uint32_t, 4> DeclIDs;
832 for (; DataLen > 0; DataLen -= 4)
Justin Bogner57ba0b22014-03-28 22:03:24 +0000833 DeclIDs.push_back(Reader.getGlobalDeclID(
834 F, endian::readNext<uint32_t, little, unaligned>(d)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000835 Reader.SetGloballyVisibleDecls(II, DeclIDs);
836 }
837
838 return II;
839}
840
841unsigned
842ASTDeclContextNameLookupTrait::ComputeHash(const DeclNameKey &Key) const {
843 llvm::FoldingSetNodeID ID;
844 ID.AddInteger(Key.Kind);
845
846 switch (Key.Kind) {
847 case DeclarationName::Identifier:
848 case DeclarationName::CXXLiteralOperatorName:
849 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
850 break;
851 case DeclarationName::ObjCZeroArgSelector:
852 case DeclarationName::ObjCOneArgSelector:
853 case DeclarationName::ObjCMultiArgSelector:
854 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
855 break;
856 case DeclarationName::CXXOperatorName:
857 ID.AddInteger((OverloadedOperatorKind)Key.Data);
858 break;
859 case DeclarationName::CXXConstructorName:
860 case DeclarationName::CXXDestructorName:
861 case DeclarationName::CXXConversionFunctionName:
862 case DeclarationName::CXXUsingDirective:
863 break;
864 }
865
866 return ID.ComputeHash();
867}
868
869ASTDeclContextNameLookupTrait::internal_key_type
870ASTDeclContextNameLookupTrait::GetInternalKey(
871 const external_key_type& Name) const {
872 DeclNameKey Key;
873 Key.Kind = Name.getNameKind();
874 switch (Name.getNameKind()) {
875 case DeclarationName::Identifier:
876 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
877 break;
878 case DeclarationName::ObjCZeroArgSelector:
879 case DeclarationName::ObjCOneArgSelector:
880 case DeclarationName::ObjCMultiArgSelector:
881 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
882 break;
883 case DeclarationName::CXXOperatorName:
884 Key.Data = Name.getCXXOverloadedOperator();
885 break;
886 case DeclarationName::CXXLiteralOperatorName:
887 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
888 break;
889 case DeclarationName::CXXConstructorName:
890 case DeclarationName::CXXDestructorName:
891 case DeclarationName::CXXConversionFunctionName:
892 case DeclarationName::CXXUsingDirective:
893 Key.Data = 0;
894 break;
895 }
896
897 return Key;
898}
899
900std::pair<unsigned, unsigned>
901ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000902 using namespace llvm::support;
903 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
904 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
Guy Benyei11169dd2012-12-18 14:30:41 +0000905 return std::make_pair(KeyLen, DataLen);
906}
907
908ASTDeclContextNameLookupTrait::internal_key_type
909ASTDeclContextNameLookupTrait::ReadKey(const unsigned char* d, unsigned) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000910 using namespace llvm::support;
Guy Benyei11169dd2012-12-18 14:30:41 +0000911
912 DeclNameKey Key;
913 Key.Kind = (DeclarationName::NameKind)*d++;
914 switch (Key.Kind) {
915 case DeclarationName::Identifier:
Justin Bogner57ba0b22014-03-28 22:03:24 +0000916 Key.Data = (uint64_t)Reader.getLocalIdentifier(
917 F, endian::readNext<uint32_t, little, unaligned>(d));
Guy Benyei11169dd2012-12-18 14:30:41 +0000918 break;
919 case DeclarationName::ObjCZeroArgSelector:
920 case DeclarationName::ObjCOneArgSelector:
921 case DeclarationName::ObjCMultiArgSelector:
922 Key.Data =
Justin Bogner57ba0b22014-03-28 22:03:24 +0000923 (uint64_t)Reader.getLocalSelector(
924 F, endian::readNext<uint32_t, little, unaligned>(
925 d)).getAsOpaquePtr();
Guy Benyei11169dd2012-12-18 14:30:41 +0000926 break;
927 case DeclarationName::CXXOperatorName:
928 Key.Data = *d++; // OverloadedOperatorKind
929 break;
930 case DeclarationName::CXXLiteralOperatorName:
Justin Bogner57ba0b22014-03-28 22:03:24 +0000931 Key.Data = (uint64_t)Reader.getLocalIdentifier(
932 F, endian::readNext<uint32_t, little, unaligned>(d));
Guy Benyei11169dd2012-12-18 14:30:41 +0000933 break;
934 case DeclarationName::CXXConstructorName:
935 case DeclarationName::CXXDestructorName:
936 case DeclarationName::CXXConversionFunctionName:
937 case DeclarationName::CXXUsingDirective:
938 Key.Data = 0;
939 break;
940 }
941
942 return Key;
943}
944
945ASTDeclContextNameLookupTrait::data_type
946ASTDeclContextNameLookupTrait::ReadData(internal_key_type,
947 const unsigned char* d,
948 unsigned DataLen) {
Justin Bogner57ba0b22014-03-28 22:03:24 +0000949 using namespace llvm::support;
950 unsigned NumDecls = endian::readNext<uint16_t, little, unaligned>(d);
Argyrios Kyrtzidisc57e5032013-01-11 22:29:49 +0000951 LE32DeclID *Start = reinterpret_cast<LE32DeclID *>(
952 const_cast<unsigned char *>(d));
Guy Benyei11169dd2012-12-18 14:30:41 +0000953 return std::make_pair(Start, Start + NumDecls);
954}
955
956bool ASTReader::ReadDeclContextStorage(ModuleFile &M,
Chris Lattner7fb3bef2013-01-20 00:56:42 +0000957 BitstreamCursor &Cursor,
Guy Benyei11169dd2012-12-18 14:30:41 +0000958 const std::pair<uint64_t, uint64_t> &Offsets,
959 DeclContextInfo &Info) {
960 SavedStreamPosition SavedPosition(Cursor);
961 // First the lexical decls.
962 if (Offsets.first != 0) {
963 Cursor.JumpToBit(Offsets.first);
964
965 RecordData Record;
Chris Lattner0e6c9402013-01-20 02:38:54 +0000966 StringRef Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +0000967 unsigned Code = Cursor.ReadCode();
Chris Lattner0e6c9402013-01-20 02:38:54 +0000968 unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +0000969 if (RecCode != DECL_CONTEXT_LEXICAL) {
970 Error("Expected lexical block");
971 return true;
972 }
973
Chris Lattner0e6c9402013-01-20 02:38:54 +0000974 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob.data());
975 Info.NumLexicalDecls = Blob.size() / sizeof(KindDeclIDPair);
Guy Benyei11169dd2012-12-18 14:30:41 +0000976 }
977
978 // Now the lookup table.
979 if (Offsets.second != 0) {
980 Cursor.JumpToBit(Offsets.second);
981
982 RecordData Record;
Chris Lattner0e6c9402013-01-20 02:38:54 +0000983 StringRef Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +0000984 unsigned Code = Cursor.ReadCode();
Chris Lattner0e6c9402013-01-20 02:38:54 +0000985 unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +0000986 if (RecCode != DECL_CONTEXT_VISIBLE) {
987 Error("Expected visible lookup table block");
988 return true;
989 }
Justin Bognerda4e6502014-04-14 16:34:29 +0000990 Info.NameLookupTableData = ASTDeclContextNameLookupTable::Create(
991 (const unsigned char *)Blob.data() + Record[0],
992 (const unsigned char *)Blob.data() + sizeof(uint32_t),
993 (const unsigned char *)Blob.data(),
994 ASTDeclContextNameLookupTrait(*this, M));
Guy Benyei11169dd2012-12-18 14:30:41 +0000995 }
996
997 return false;
998}
999
1000void ASTReader::Error(StringRef Msg) {
1001 Error(diag::err_fe_pch_malformed, Msg);
Douglas Gregor940e8052013-05-10 22:15:13 +00001002 if (Context.getLangOpts().Modules && !Diags.isDiagnosticInFlight()) {
1003 Diag(diag::note_module_cache_path)
1004 << PP.getHeaderSearchInfo().getModuleCachePath();
1005 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001006}
1007
1008void ASTReader::Error(unsigned DiagID,
1009 StringRef Arg1, StringRef Arg2) {
1010 if (Diags.isDiagnosticInFlight())
1011 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
1012 else
1013 Diag(DiagID) << Arg1 << Arg2;
1014}
1015
1016//===----------------------------------------------------------------------===//
1017// Source Manager Deserialization
1018//===----------------------------------------------------------------------===//
1019
1020/// \brief Read the line table in the source manager block.
1021/// \returns true if there was an error.
1022bool ASTReader::ParseLineTable(ModuleFile &F,
Richard Smith7ed1bc92014-12-05 22:42:13 +00001023 const RecordData &Record) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001024 unsigned Idx = 0;
1025 LineTableInfo &LineTable = SourceMgr.getLineTable();
1026
1027 // Parse the file names
1028 std::map<int, int> FileIDs;
1029 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
1030 // Extract the file name
Richard Smith7ed1bc92014-12-05 22:42:13 +00001031 auto Filename = ReadPath(F, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00001032 FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
1033 }
1034
1035 // Parse the line entries
1036 std::vector<LineEntry> Entries;
1037 while (Idx < Record.size()) {
1038 int FID = Record[Idx++];
1039 assert(FID >= 0 && "Serialized line entries for non-local file.");
1040 // Remap FileID from 1-based old view.
1041 FID += F.SLocEntryBaseID - 1;
1042
1043 // Extract the line entries
1044 unsigned NumEntries = Record[Idx++];
1045 assert(NumEntries && "Numentries is 00000");
1046 Entries.clear();
1047 Entries.reserve(NumEntries);
1048 for (unsigned I = 0; I != NumEntries; ++I) {
1049 unsigned FileOffset = Record[Idx++];
1050 unsigned LineNo = Record[Idx++];
1051 int FilenameID = FileIDs[Record[Idx++]];
1052 SrcMgr::CharacteristicKind FileKind
1053 = (SrcMgr::CharacteristicKind)Record[Idx++];
1054 unsigned IncludeOffset = Record[Idx++];
1055 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1056 FileKind, IncludeOffset));
1057 }
1058 LineTable.AddEntry(FileID::get(FID), Entries);
1059 }
1060
1061 return false;
1062}
1063
1064/// \brief Read a source manager block
1065bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1066 using namespace SrcMgr;
1067
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001068 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001069
1070 // Set the source-location entry cursor to the current position in
1071 // the stream. This cursor will be used to read the contents of the
1072 // source manager block initially, and then lazily read
1073 // source-location entries as needed.
1074 SLocEntryCursor = F.Stream;
1075
1076 // The stream itself is going to skip over the source manager block.
1077 if (F.Stream.SkipBlock()) {
1078 Error("malformed block record in AST file");
1079 return true;
1080 }
1081
1082 // Enter the source manager block.
1083 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
1084 Error("malformed source manager block record in AST file");
1085 return true;
1086 }
1087
1088 RecordData Record;
1089 while (true) {
Chris Lattnere7b154b2013-01-19 21:39:22 +00001090 llvm::BitstreamEntry E = SLocEntryCursor.advanceSkippingSubblocks();
1091
1092 switch (E.Kind) {
1093 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1094 case llvm::BitstreamEntry::Error:
1095 Error("malformed block record in AST file");
1096 return true;
1097 case llvm::BitstreamEntry::EndBlock:
Guy Benyei11169dd2012-12-18 14:30:41 +00001098 return false;
Chris Lattnere7b154b2013-01-19 21:39:22 +00001099 case llvm::BitstreamEntry::Record:
1100 // The interesting case.
1101 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001102 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00001103
Guy Benyei11169dd2012-12-18 14:30:41 +00001104 // Read a record.
Guy Benyei11169dd2012-12-18 14:30:41 +00001105 Record.clear();
Chris Lattner15c3e7d2013-01-21 18:28:26 +00001106 StringRef Blob;
1107 switch (SLocEntryCursor.readRecord(E.ID, Record, &Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001108 default: // Default behavior: ignore.
1109 break;
1110
1111 case SM_SLOC_FILE_ENTRY:
1112 case SM_SLOC_BUFFER_ENTRY:
1113 case SM_SLOC_EXPANSION_ENTRY:
1114 // Once we hit one of the source location entries, we're done.
1115 return false;
1116 }
1117 }
1118}
1119
1120/// \brief If a header file is not found at the path that we expect it to be
1121/// and the PCH file was moved from its original location, try to resolve the
1122/// file by assuming that header+PCH were moved together and the header is in
1123/// the same place relative to the PCH.
1124static std::string
1125resolveFileRelativeToOriginalDir(const std::string &Filename,
1126 const std::string &OriginalDir,
1127 const std::string &CurrDir) {
1128 assert(OriginalDir != CurrDir &&
1129 "No point trying to resolve the file if the PCH dir didn't change");
1130 using namespace llvm::sys;
1131 SmallString<128> filePath(Filename);
1132 fs::make_absolute(filePath);
1133 assert(path::is_absolute(OriginalDir));
1134 SmallString<128> currPCHPath(CurrDir);
1135
1136 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1137 fileDirE = path::end(path::parent_path(filePath));
1138 path::const_iterator origDirI = path::begin(OriginalDir),
1139 origDirE = path::end(OriginalDir);
1140 // Skip the common path components from filePath and OriginalDir.
1141 while (fileDirI != fileDirE && origDirI != origDirE &&
1142 *fileDirI == *origDirI) {
1143 ++fileDirI;
1144 ++origDirI;
1145 }
1146 for (; origDirI != origDirE; ++origDirI)
1147 path::append(currPCHPath, "..");
1148 path::append(currPCHPath, fileDirI, fileDirE);
1149 path::append(currPCHPath, path::filename(Filename));
1150 return currPCHPath.str();
1151}
1152
1153bool ASTReader::ReadSLocEntry(int ID) {
1154 if (ID == 0)
1155 return false;
1156
1157 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1158 Error("source location entry ID out-of-range for AST file");
1159 return true;
1160 }
1161
1162 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
1163 F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]);
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001164 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001165 unsigned BaseOffset = F->SLocEntryBaseOffset;
1166
1167 ++NumSLocEntriesRead;
Chris Lattnere7b154b2013-01-19 21:39:22 +00001168 llvm::BitstreamEntry Entry = SLocEntryCursor.advance();
1169 if (Entry.Kind != llvm::BitstreamEntry::Record) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001170 Error("incorrectly-formatted source location entry in AST file");
1171 return true;
1172 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00001173
Guy Benyei11169dd2012-12-18 14:30:41 +00001174 RecordData Record;
Chris Lattner0e6c9402013-01-20 02:38:54 +00001175 StringRef Blob;
1176 switch (SLocEntryCursor.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001177 default:
1178 Error("incorrectly-formatted source location entry in AST file");
1179 return true;
1180
1181 case SM_SLOC_FILE_ENTRY: {
1182 // We will detect whether a file changed and return 'Failure' for it, but
1183 // we will also try to fail gracefully by setting up the SLocEntry.
1184 unsigned InputID = Record[4];
1185 InputFile IF = getInputFile(*F, InputID);
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001186 const FileEntry *File = IF.getFile();
1187 bool OverriddenBuffer = IF.isOverridden();
Guy Benyei11169dd2012-12-18 14:30:41 +00001188
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001189 // Note that we only check if a File was returned. If it was out-of-date
1190 // we have complained but we will continue creating a FileID to recover
1191 // gracefully.
1192 if (!File)
Guy Benyei11169dd2012-12-18 14:30:41 +00001193 return true;
1194
1195 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1196 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1197 // This is the module's main file.
1198 IncludeLoc = getImportLocation(F);
1199 }
1200 SrcMgr::CharacteristicKind
1201 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1202 FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
1203 ID, BaseOffset + Record[0]);
1204 SrcMgr::FileInfo &FileInfo =
1205 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1206 FileInfo.NumCreatedFIDs = Record[5];
1207 if (Record[3])
1208 FileInfo.setHasLineDirectives();
1209
1210 const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
1211 unsigned NumFileDecls = Record[7];
1212 if (NumFileDecls) {
1213 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1214 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1215 NumFileDecls));
1216 }
1217
1218 const SrcMgr::ContentCache *ContentCache
1219 = SourceMgr.getOrCreateContentCache(File,
1220 /*isSystemFile=*/FileCharacter != SrcMgr::C_User);
1221 if (OverriddenBuffer && !ContentCache->BufferOverridden &&
1222 ContentCache->ContentsEntry == ContentCache->OrigEntry) {
1223 unsigned Code = SLocEntryCursor.ReadCode();
1224 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00001225 unsigned RecCode = SLocEntryCursor.readRecord(Code, Record, &Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +00001226
1227 if (RecCode != SM_SLOC_BUFFER_BLOB) {
1228 Error("AST record has invalid code");
1229 return true;
1230 }
1231
Rafael Espindolad87f8d72014-08-27 20:03:29 +00001232 std::unique_ptr<llvm::MemoryBuffer> Buffer
Chris Lattner0e6c9402013-01-20 02:38:54 +00001233 = llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), File->getName());
David Blaikie49cc3182014-08-27 20:54:45 +00001234 SourceMgr.overrideFileContents(File, std::move(Buffer));
Guy Benyei11169dd2012-12-18 14:30:41 +00001235 }
1236
1237 break;
1238 }
1239
1240 case SM_SLOC_BUFFER_ENTRY: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00001241 const char *Name = Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00001242 unsigned Offset = Record[0];
1243 SrcMgr::CharacteristicKind
1244 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1245 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
Richard Smithe842a472014-10-22 02:05:46 +00001246 if (IncludeLoc.isInvalid() &&
1247 (F->Kind == MK_ImplicitModule || F->Kind == MK_ExplicitModule)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001248 IncludeLoc = getImportLocation(F);
1249 }
1250 unsigned Code = SLocEntryCursor.ReadCode();
1251 Record.clear();
1252 unsigned RecCode
Chris Lattner0e6c9402013-01-20 02:38:54 +00001253 = SLocEntryCursor.readRecord(Code, Record, &Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +00001254
1255 if (RecCode != SM_SLOC_BUFFER_BLOB) {
1256 Error("AST record has invalid code");
1257 return true;
1258 }
1259
Rafael Espindolad87f8d72014-08-27 20:03:29 +00001260 std::unique_ptr<llvm::MemoryBuffer> Buffer =
1261 llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name);
David Blaikie50a5f972014-08-29 07:59:55 +00001262 SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID,
Rafael Espindolad87f8d72014-08-27 20:03:29 +00001263 BaseOffset + Offset, IncludeLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001264 break;
1265 }
1266
1267 case SM_SLOC_EXPANSION_ENTRY: {
1268 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1269 SourceMgr.createExpansionLoc(SpellingLoc,
1270 ReadSourceLocation(*F, Record[2]),
1271 ReadSourceLocation(*F, Record[3]),
1272 Record[4],
1273 ID,
1274 BaseOffset + Record[0]);
1275 break;
1276 }
1277 }
1278
1279 return false;
1280}
1281
1282std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1283 if (ID == 0)
1284 return std::make_pair(SourceLocation(), "");
1285
1286 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1287 Error("source location entry ID out-of-range for AST file");
1288 return std::make_pair(SourceLocation(), "");
1289 }
1290
1291 // Find which module file this entry lands in.
1292 ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
Richard Smithe842a472014-10-22 02:05:46 +00001293 if (M->Kind != MK_ImplicitModule && M->Kind != MK_ExplicitModule)
Guy Benyei11169dd2012-12-18 14:30:41 +00001294 return std::make_pair(SourceLocation(), "");
1295
1296 // FIXME: Can we map this down to a particular submodule? That would be
1297 // ideal.
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001298 return std::make_pair(M->ImportLoc, StringRef(M->ModuleName));
Guy Benyei11169dd2012-12-18 14:30:41 +00001299}
1300
1301/// \brief Find the location where the module F is imported.
1302SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1303 if (F->ImportLoc.isValid())
1304 return F->ImportLoc;
1305
1306 // Otherwise we have a PCH. It's considered to be "imported" at the first
1307 // location of its includer.
1308 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001309 // Main file is the importer.
1310 assert(!SourceMgr.getMainFileID().isInvalid() && "missing main file");
1311 return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
Guy Benyei11169dd2012-12-18 14:30:41 +00001312 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001313 return F->ImportedBy[0]->FirstLoc;
1314}
1315
1316/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1317/// specified cursor. Read the abbreviations that are at the top of the block
1318/// and then leave the cursor pointing into the block.
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001319bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001320 if (Cursor.EnterSubBlock(BlockID)) {
1321 Error("malformed block record in AST file");
1322 return Failure;
1323 }
1324
1325 while (true) {
1326 uint64_t Offset = Cursor.GetCurrentBitNo();
1327 unsigned Code = Cursor.ReadCode();
1328
1329 // We expect all abbrevs to be at the start of the block.
1330 if (Code != llvm::bitc::DEFINE_ABBREV) {
1331 Cursor.JumpToBit(Offset);
1332 return false;
1333 }
1334 Cursor.ReadAbbrevRecord();
1335 }
1336}
1337
Richard Smithe40f2ba2013-08-07 21:41:30 +00001338Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record,
John McCallf413f5e2013-05-03 00:10:13 +00001339 unsigned &Idx) {
1340 Token Tok;
1341 Tok.startToken();
1342 Tok.setLocation(ReadSourceLocation(F, Record, Idx));
1343 Tok.setLength(Record[Idx++]);
1344 if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
1345 Tok.setIdentifierInfo(II);
1346 Tok.setKind((tok::TokenKind)Record[Idx++]);
1347 Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1348 return Tok;
1349}
1350
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001351MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001352 BitstreamCursor &Stream = F.MacroCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001353
1354 // Keep track of where we are in the stream, then jump back there
1355 // after reading this macro.
1356 SavedStreamPosition SavedPosition(Stream);
1357
1358 Stream.JumpToBit(Offset);
1359 RecordData Record;
1360 SmallVector<IdentifierInfo*, 16> MacroArgs;
Craig Toppera13603a2014-05-22 05:54:18 +00001361 MacroInfo *Macro = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00001362
Guy Benyei11169dd2012-12-18 14:30:41 +00001363 while (true) {
Chris Lattnerefa77172013-01-20 00:00:22 +00001364 // Advance to the next record, but if we get to the end of the block, don't
1365 // pop it (removing all the abbreviations from the cursor) since we want to
1366 // be able to reseek within the block and read entries.
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001367 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
Chris Lattnerefa77172013-01-20 00:00:22 +00001368 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks(Flags);
1369
1370 switch (Entry.Kind) {
1371 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1372 case llvm::BitstreamEntry::Error:
1373 Error("malformed block record in AST file");
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001374 return Macro;
Chris Lattnerefa77172013-01-20 00:00:22 +00001375 case llvm::BitstreamEntry::EndBlock:
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001376 return Macro;
Chris Lattnerefa77172013-01-20 00:00:22 +00001377 case llvm::BitstreamEntry::Record:
1378 // The interesting case.
1379 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001380 }
1381
1382 // Read a record.
Guy Benyei11169dd2012-12-18 14:30:41 +00001383 Record.clear();
1384 PreprocessorRecordTypes RecType =
Chris Lattner0e6c9402013-01-20 02:38:54 +00001385 (PreprocessorRecordTypes)Stream.readRecord(Entry.ID, Record);
Guy Benyei11169dd2012-12-18 14:30:41 +00001386 switch (RecType) {
Richard Smithd7329392015-04-21 21:46:32 +00001387 case PP_MODULE_MACRO:
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001388 case PP_MACRO_DIRECTIVE_HISTORY:
1389 return Macro;
1390
Guy Benyei11169dd2012-12-18 14:30:41 +00001391 case PP_MACRO_OBJECT_LIKE:
1392 case PP_MACRO_FUNCTION_LIKE: {
1393 // If we already have a macro, that means that we've hit the end
1394 // of the definition of the macro we were looking for. We're
1395 // done.
1396 if (Macro)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001397 return Macro;
Guy Benyei11169dd2012-12-18 14:30:41 +00001398
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001399 unsigned NextIndex = 1; // Skip identifier ID.
1400 SubmoduleID SubModID = getGlobalSubmoduleID(F, Record[NextIndex++]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001401 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001402 MacroInfo *MI = PP.AllocateDeserializedMacroInfo(Loc, SubModID);
Argyrios Kyrtzidis7572be22013-01-07 19:16:23 +00001403 MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
Guy Benyei11169dd2012-12-18 14:30:41 +00001404 MI->setIsUsed(Record[NextIndex++]);
Argyrios Kyrtzidis9ef53ce2014-04-09 18:21:23 +00001405 MI->setUsedForHeaderGuard(Record[NextIndex++]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001406
Guy Benyei11169dd2012-12-18 14:30:41 +00001407 if (RecType == PP_MACRO_FUNCTION_LIKE) {
1408 // Decode function-like macro info.
1409 bool isC99VarArgs = Record[NextIndex++];
1410 bool isGNUVarArgs = Record[NextIndex++];
1411 bool hasCommaPasting = Record[NextIndex++];
1412 MacroArgs.clear();
1413 unsigned NumArgs = Record[NextIndex++];
1414 for (unsigned i = 0; i != NumArgs; ++i)
1415 MacroArgs.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1416
1417 // Install function-like macro info.
1418 MI->setIsFunctionLike();
1419 if (isC99VarArgs) MI->setIsC99Varargs();
1420 if (isGNUVarArgs) MI->setIsGNUVarargs();
1421 if (hasCommaPasting) MI->setHasCommaPasting();
1422 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
1423 PP.getPreprocessorAllocator());
1424 }
1425
Guy Benyei11169dd2012-12-18 14:30:41 +00001426 // Remember that we saw this macro last so that we add the tokens that
1427 // form its body to it.
1428 Macro = MI;
1429
1430 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1431 Record[NextIndex]) {
1432 // We have a macro definition. Register the association
1433 PreprocessedEntityID
1434 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1435 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
Richard Smith66a81862015-05-04 02:25:31 +00001436 PreprocessingRecord::PPEntityID PPID =
1437 PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true);
1438 MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
1439 PPRec.getPreprocessedEntity(PPID));
Argyrios Kyrtzidis832de9f2013-02-22 18:35:59 +00001440 if (PPDef)
1441 PPRec.RegisterMacroDefinition(Macro, PPDef);
Guy Benyei11169dd2012-12-18 14:30:41 +00001442 }
1443
1444 ++NumMacrosRead;
1445 break;
1446 }
1447
1448 case PP_TOKEN: {
1449 // If we see a TOKEN before a PP_MACRO_*, then the file is
1450 // erroneous, just pretend we didn't see this.
Craig Toppera13603a2014-05-22 05:54:18 +00001451 if (!Macro) break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001452
John McCallf413f5e2013-05-03 00:10:13 +00001453 unsigned Idx = 0;
1454 Token Tok = ReadToken(F, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00001455 Macro->AddTokenToBody(Tok);
1456 break;
1457 }
1458 }
1459 }
1460}
1461
1462PreprocessedEntityID
1463ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const {
1464 ContinuousRangeMap<uint32_t, int, 2>::const_iterator
1465 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1466 assert(I != M.PreprocessedEntityRemap.end()
1467 && "Invalid index into preprocessed entity index remap");
1468
1469 return LocalID + I->second;
1470}
1471
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001472unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1473 return llvm::hash_combine(ikey.Size, ikey.ModTime);
Guy Benyei11169dd2012-12-18 14:30:41 +00001474}
Richard Smith7ed1bc92014-12-05 22:42:13 +00001475
Guy Benyei11169dd2012-12-18 14:30:41 +00001476HeaderFileInfoTrait::internal_key_type
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001477HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
1478 internal_key_type ikey = { FE->getSize(), FE->getModificationTime(),
Richard Smith7ed1bc92014-12-05 22:42:13 +00001479 FE->getName(), /*Imported*/false };
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001480 return ikey;
1481}
Guy Benyei11169dd2012-12-18 14:30:41 +00001482
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001483bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
1484 if (a.Size != b.Size || a.ModTime != b.ModTime)
Guy Benyei11169dd2012-12-18 14:30:41 +00001485 return false;
1486
Richard Smith7ed1bc92014-12-05 22:42:13 +00001487 if (llvm::sys::path::is_absolute(a.Filename) &&
1488 strcmp(a.Filename, b.Filename) == 0)
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001489 return true;
1490
Guy Benyei11169dd2012-12-18 14:30:41 +00001491 // Determine whether the actual files are equivalent.
Argyrios Kyrtzidis2a513e82013-03-04 20:33:40 +00001492 FileManager &FileMgr = Reader.getFileManager();
Richard Smith7ed1bc92014-12-05 22:42:13 +00001493 auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* {
1494 if (!Key.Imported)
1495 return FileMgr.getFile(Key.Filename);
1496
1497 std::string Resolved = Key.Filename;
1498 Reader.ResolveImportedPath(M, Resolved);
1499 return FileMgr.getFile(Resolved);
1500 };
1501
1502 const FileEntry *FEA = GetFile(a);
1503 const FileEntry *FEB = GetFile(b);
1504 return FEA && FEA == FEB;
Guy Benyei11169dd2012-12-18 14:30:41 +00001505}
1506
1507std::pair<unsigned, unsigned>
1508HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
Justin Bogner57ba0b22014-03-28 22:03:24 +00001509 using namespace llvm::support;
1510 unsigned KeyLen = (unsigned) endian::readNext<uint16_t, little, unaligned>(d);
Guy Benyei11169dd2012-12-18 14:30:41 +00001511 unsigned DataLen = (unsigned) *d++;
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001512 return std::make_pair(KeyLen, DataLen);
Guy Benyei11169dd2012-12-18 14:30:41 +00001513}
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001514
1515HeaderFileInfoTrait::internal_key_type
1516HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
Justin Bogner57ba0b22014-03-28 22:03:24 +00001517 using namespace llvm::support;
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001518 internal_key_type ikey;
Justin Bogner57ba0b22014-03-28 22:03:24 +00001519 ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d));
1520 ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d));
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001521 ikey.Filename = (const char *)d;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001522 ikey.Imported = true;
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001523 return ikey;
1524}
1525
Guy Benyei11169dd2012-12-18 14:30:41 +00001526HeaderFileInfoTrait::data_type
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001527HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
Guy Benyei11169dd2012-12-18 14:30:41 +00001528 unsigned DataLen) {
1529 const unsigned char *End = d + DataLen;
Justin Bogner57ba0b22014-03-28 22:03:24 +00001530 using namespace llvm::support;
Guy Benyei11169dd2012-12-18 14:30:41 +00001531 HeaderFileInfo HFI;
1532 unsigned Flags = *d++;
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001533 HFI.HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>
1534 ((Flags >> 6) & 0x03);
Guy Benyei11169dd2012-12-18 14:30:41 +00001535 HFI.isImport = (Flags >> 5) & 0x01;
1536 HFI.isPragmaOnce = (Flags >> 4) & 0x01;
1537 HFI.DirInfo = (Flags >> 2) & 0x03;
1538 HFI.Resolved = (Flags >> 1) & 0x01;
1539 HFI.IndexHeaderMapHeader = Flags & 0x01;
Justin Bogner57ba0b22014-03-28 22:03:24 +00001540 HFI.NumIncludes = endian::readNext<uint16_t, little, unaligned>(d);
1541 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(
1542 M, endian::readNext<uint32_t, little, unaligned>(d));
1543 if (unsigned FrameworkOffset =
1544 endian::readNext<uint32_t, little, unaligned>(d)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001545 // The framework offset is 1 greater than the actual offset,
1546 // since 0 is used as an indicator for "no framework name".
1547 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1548 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1549 }
1550
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001551 if (d != End) {
Justin Bogner57ba0b22014-03-28 22:03:24 +00001552 uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d);
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001553 if (LocalSMID) {
1554 // This header is part of a module. Associate it with the module to enable
1555 // implicit module import.
1556 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1557 Module *Mod = Reader.getSubmodule(GlobalSMID);
1558 HFI.isModuleHeader = true;
1559 FileManager &FileMgr = Reader.getFileManager();
1560 ModuleMap &ModMap =
1561 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
Richard Smith7ed1bc92014-12-05 22:42:13 +00001562 // FIXME: This information should be propagated through the
1563 // SUBMODULE_HEADER etc records rather than from here.
Richard Smith3c1a41a2014-12-02 00:08:08 +00001564 // FIXME: We don't ever mark excluded headers.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001565 std::string Filename = key.Filename;
1566 if (key.Imported)
1567 Reader.ResolveImportedPath(M, Filename);
1568 Module::Header H = { key.Filename, FileMgr.getFile(Filename) };
Hans Wennborg0101b542014-12-02 02:13:09 +00001569 ModMap.addHeader(Mod, H, HFI.getHeaderRole());
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001570 }
1571 }
1572
Guy Benyei11169dd2012-12-18 14:30:41 +00001573 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1574 (void)End;
1575
1576 // This HeaderFileInfo was externally loaded.
1577 HFI.External = true;
1578 return HFI;
1579}
1580
Richard Smithd7329392015-04-21 21:46:32 +00001581void ASTReader::addPendingMacro(IdentifierInfo *II,
1582 ModuleFile *M,
1583 uint64_t MacroDirectivesOffset) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001584 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1585 PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00001586}
1587
1588void ASTReader::ReadDefinedMacros() {
1589 // Note that we are loading defined macros.
1590 Deserializing Macros(this);
1591
1592 for (ModuleReverseIterator I = ModuleMgr.rbegin(),
1593 E = ModuleMgr.rend(); I != E; ++I) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001594 BitstreamCursor &MacroCursor = (*I)->MacroCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001595
1596 // If there was no preprocessor block, skip this file.
1597 if (!MacroCursor.getBitStreamReader())
1598 continue;
1599
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001600 BitstreamCursor Cursor = MacroCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001601 Cursor.JumpToBit((*I)->MacroStartOffset);
1602
1603 RecordData Record;
1604 while (true) {
Chris Lattnere7b154b2013-01-19 21:39:22 +00001605 llvm::BitstreamEntry E = Cursor.advanceSkippingSubblocks();
1606
1607 switch (E.Kind) {
1608 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1609 case llvm::BitstreamEntry::Error:
1610 Error("malformed block record in AST file");
1611 return;
1612 case llvm::BitstreamEntry::EndBlock:
1613 goto NextCursor;
1614
1615 case llvm::BitstreamEntry::Record:
Chris Lattnere7b154b2013-01-19 21:39:22 +00001616 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00001617 switch (Cursor.readRecord(E.ID, Record)) {
Chris Lattnere7b154b2013-01-19 21:39:22 +00001618 default: // Default behavior: ignore.
1619 break;
1620
1621 case PP_MACRO_OBJECT_LIKE:
1622 case PP_MACRO_FUNCTION_LIKE:
1623 getLocalIdentifier(**I, Record[0]);
1624 break;
1625
1626 case PP_TOKEN:
1627 // Ignore tokens.
1628 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001629 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001630 break;
1631 }
1632 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00001633 NextCursor: ;
Guy Benyei11169dd2012-12-18 14:30:41 +00001634 }
1635}
1636
1637namespace {
1638 /// \brief Visitor class used to look up identifirs in an AST file.
1639 class IdentifierLookupVisitor {
1640 StringRef Name;
1641 unsigned PriorGeneration;
Douglas Gregor00a50f72013-01-25 00:38:33 +00001642 unsigned &NumIdentifierLookups;
1643 unsigned &NumIdentifierLookupHits;
Guy Benyei11169dd2012-12-18 14:30:41 +00001644 IdentifierInfo *Found;
Douglas Gregor00a50f72013-01-25 00:38:33 +00001645
Guy Benyei11169dd2012-12-18 14:30:41 +00001646 public:
Douglas Gregor00a50f72013-01-25 00:38:33 +00001647 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
1648 unsigned &NumIdentifierLookups,
1649 unsigned &NumIdentifierLookupHits)
Douglas Gregor7211ac12013-01-25 23:32:03 +00001650 : Name(Name), PriorGeneration(PriorGeneration),
Douglas Gregor00a50f72013-01-25 00:38:33 +00001651 NumIdentifierLookups(NumIdentifierLookups),
1652 NumIdentifierLookupHits(NumIdentifierLookupHits),
1653 Found()
1654 {
1655 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001656
1657 static bool visit(ModuleFile &M, void *UserData) {
1658 IdentifierLookupVisitor *This
1659 = static_cast<IdentifierLookupVisitor *>(UserData);
1660
1661 // If we've already searched this module file, skip it now.
1662 if (M.Generation <= This->PriorGeneration)
1663 return true;
Douglas Gregore060e572013-01-25 01:03:03 +00001664
Guy Benyei11169dd2012-12-18 14:30:41 +00001665 ASTIdentifierLookupTable *IdTable
1666 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
1667 if (!IdTable)
1668 return false;
1669
1670 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(),
1671 M, This->Found);
Douglas Gregor00a50f72013-01-25 00:38:33 +00001672 ++This->NumIdentifierLookups;
1673 ASTIdentifierLookupTable::iterator Pos = IdTable->find(This->Name,&Trait);
Guy Benyei11169dd2012-12-18 14:30:41 +00001674 if (Pos == IdTable->end())
1675 return false;
1676
1677 // Dereferencing the iterator has the effect of building the
1678 // IdentifierInfo node and populating it with the various
1679 // declarations it needs.
Douglas Gregor00a50f72013-01-25 00:38:33 +00001680 ++This->NumIdentifierLookupHits;
Guy Benyei11169dd2012-12-18 14:30:41 +00001681 This->Found = *Pos;
1682 return true;
1683 }
1684
1685 // \brief Retrieve the identifier info found within the module
1686 // files.
1687 IdentifierInfo *getIdentifierInfo() const { return Found; }
1688 };
1689}
1690
1691void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
1692 // Note that we are loading an identifier.
1693 Deserializing AnIdentifier(this);
1694
1695 unsigned PriorGeneration = 0;
1696 if (getContext().getLangOpts().Modules)
1697 PriorGeneration = IdentifierGeneration[&II];
Douglas Gregore060e572013-01-25 01:03:03 +00001698
1699 // If there is a global index, look there first to determine which modules
1700 // provably do not have any results for this identifier.
Douglas Gregor7211ac12013-01-25 23:32:03 +00001701 GlobalModuleIndex::HitSet Hits;
Craig Toppera13603a2014-05-22 05:54:18 +00001702 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
Douglas Gregore060e572013-01-25 01:03:03 +00001703 if (!loadGlobalIndex()) {
Douglas Gregor7211ac12013-01-25 23:32:03 +00001704 if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
1705 HitsPtr = &Hits;
Douglas Gregore060e572013-01-25 01:03:03 +00001706 }
1707 }
1708
Douglas Gregor7211ac12013-01-25 23:32:03 +00001709 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
Douglas Gregor00a50f72013-01-25 00:38:33 +00001710 NumIdentifierLookups,
1711 NumIdentifierLookupHits);
Douglas Gregor7211ac12013-01-25 23:32:03 +00001712 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor, HitsPtr);
Guy Benyei11169dd2012-12-18 14:30:41 +00001713 markIdentifierUpToDate(&II);
1714}
1715
1716void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
1717 if (!II)
1718 return;
1719
1720 II->setOutOfDate(false);
1721
1722 // Update the generation for this identifier.
1723 if (getContext().getLangOpts().Modules)
Richard Smith053f6c62014-05-16 23:01:30 +00001724 IdentifierGeneration[II] = getGeneration();
Guy Benyei11169dd2012-12-18 14:30:41 +00001725}
1726
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001727void ASTReader::resolvePendingMacro(IdentifierInfo *II,
1728 const PendingMacroInfo &PMInfo) {
Richard Smithd7329392015-04-21 21:46:32 +00001729 ModuleFile &M = *PMInfo.M;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001730
1731 BitstreamCursor &Cursor = M.MacroCursor;
1732 SavedStreamPosition SavedPosition(Cursor);
Richard Smithd7329392015-04-21 21:46:32 +00001733 Cursor.JumpToBit(PMInfo.MacroDirectivesOffset);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001734
Richard Smith713369b2015-04-23 20:40:50 +00001735 struct ModuleMacroRecord {
1736 SubmoduleID SubModID;
1737 MacroInfo *MI;
1738 SmallVector<SubmoduleID, 8> Overrides;
1739 };
1740 llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001741
Richard Smithd7329392015-04-21 21:46:32 +00001742 // We expect to see a sequence of PP_MODULE_MACRO records listing exported
1743 // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
1744 // macro histroy.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001745 RecordData Record;
Richard Smithd7329392015-04-21 21:46:32 +00001746 while (true) {
1747 llvm::BitstreamEntry Entry =
1748 Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
1749 if (Entry.Kind != llvm::BitstreamEntry::Record) {
1750 Error("malformed block record in AST file");
1751 return;
1752 }
1753
1754 Record.clear();
Aaron Ballmanc75a1922015-04-22 15:25:05 +00001755 switch ((PreprocessorRecordTypes)Cursor.readRecord(Entry.ID, Record)) {
Richard Smithd7329392015-04-21 21:46:32 +00001756 case PP_MACRO_DIRECTIVE_HISTORY:
1757 break;
1758
1759 case PP_MODULE_MACRO: {
Richard Smith713369b2015-04-23 20:40:50 +00001760 ModuleMacros.push_back(ModuleMacroRecord());
1761 auto &Info = ModuleMacros.back();
Richard Smithe56c8bc2015-04-22 00:26:11 +00001762 Info.SubModID = getGlobalSubmoduleID(M, Record[0]);
1763 Info.MI = getMacro(getGlobalMacroID(M, Record[1]));
Richard Smith713369b2015-04-23 20:40:50 +00001764 for (int I = 2, N = Record.size(); I != N; ++I)
1765 Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I]));
Richard Smithd7329392015-04-21 21:46:32 +00001766 continue;
1767 }
1768
1769 default:
1770 Error("malformed block record in AST file");
1771 return;
1772 }
1773
1774 // We found the macro directive history; that's the last record
1775 // for this macro.
1776 break;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001777 }
1778
Richard Smithd7329392015-04-21 21:46:32 +00001779 // Module macros are listed in reverse dependency order.
Richard Smithe56c8bc2015-04-22 00:26:11 +00001780 {
1781 std::reverse(ModuleMacros.begin(), ModuleMacros.end());
Richard Smithe56c8bc2015-04-22 00:26:11 +00001782 llvm::SmallVector<ModuleMacro*, 8> Overrides;
Richard Smith713369b2015-04-23 20:40:50 +00001783 for (auto &MMR : ModuleMacros) {
Richard Smithe56c8bc2015-04-22 00:26:11 +00001784 Overrides.clear();
Richard Smith713369b2015-04-23 20:40:50 +00001785 for (unsigned ModID : MMR.Overrides) {
Richard Smithb8b2ed62015-04-23 18:18:26 +00001786 Module *Mod = getSubmodule(ModID);
1787 auto *Macro = PP.getModuleMacro(Mod, II);
Richard Smithe56c8bc2015-04-22 00:26:11 +00001788 assert(Macro && "missing definition for overridden macro");
Richard Smith5dbef922015-04-22 02:09:43 +00001789 Overrides.push_back(Macro);
Richard Smithe56c8bc2015-04-22 00:26:11 +00001790 }
1791
1792 bool Inserted = false;
Richard Smith713369b2015-04-23 20:40:50 +00001793 Module *Owner = getSubmodule(MMR.SubModID);
Richard Smith20e883e2015-04-29 23:20:19 +00001794 PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted);
Richard Smithd7329392015-04-21 21:46:32 +00001795 }
1796 }
1797
1798 // Don't read the directive history for a module; we don't have anywhere
1799 // to put it.
1800 if (M.Kind == MK_ImplicitModule || M.Kind == MK_ExplicitModule)
1801 return;
1802
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001803 // Deserialize the macro directives history in reverse source-order.
Craig Toppera13603a2014-05-22 05:54:18 +00001804 MacroDirective *Latest = nullptr, *Earliest = nullptr;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001805 unsigned Idx = 0, N = Record.size();
1806 while (Idx < N) {
Craig Toppera13603a2014-05-22 05:54:18 +00001807 MacroDirective *MD = nullptr;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001808 SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001809 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
1810 switch (K) {
1811 case MacroDirective::MD_Define: {
Richard Smith713369b2015-04-23 20:40:50 +00001812 MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++]));
Richard Smith3981b172015-04-30 02:16:23 +00001813 MD = PP.AllocateDefMacroDirective(MI, Loc);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001814 break;
1815 }
Richard Smithdaa69e02014-07-25 04:40:03 +00001816 case MacroDirective::MD_Undefine: {
Richard Smith3981b172015-04-30 02:16:23 +00001817 MD = PP.AllocateUndefMacroDirective(Loc);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001818 break;
Richard Smithdaa69e02014-07-25 04:40:03 +00001819 }
1820 case MacroDirective::MD_Visibility:
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001821 bool isPublic = Record[Idx++];
1822 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
1823 break;
1824 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001825
1826 if (!Latest)
1827 Latest = MD;
1828 if (Earliest)
1829 Earliest->setPrevious(MD);
1830 Earliest = MD;
1831 }
1832
Richard Smithd6e8c0d2015-05-04 19:58:00 +00001833 if (Latest)
1834 PP.setLoadedMacroDirective(II, Latest);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001835}
1836
Argyrios Kyrtzidisce9b49e2014-03-14 02:26:27 +00001837ASTReader::InputFileInfo
1838ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) {
Ben Langmuir198c1682014-03-07 07:27:49 +00001839 // Go find this input file.
1840 BitstreamCursor &Cursor = F.InputFilesCursor;
1841 SavedStreamPosition SavedPosition(Cursor);
1842 Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
1843
1844 unsigned Code = Cursor.ReadCode();
1845 RecordData Record;
1846 StringRef Blob;
1847
1848 unsigned Result = Cursor.readRecord(Code, Record, &Blob);
1849 assert(static_cast<InputFileRecordTypes>(Result) == INPUT_FILE &&
1850 "invalid record type for input file");
1851 (void)Result;
1852
Argyrios Kyrtzidisce9b49e2014-03-14 02:26:27 +00001853 std::string Filename;
1854 off_t StoredSize;
1855 time_t StoredTime;
1856 bool Overridden;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001857
Ben Langmuir198c1682014-03-07 07:27:49 +00001858 assert(Record[0] == ID && "Bogus stored ID or offset");
1859 StoredSize = static_cast<off_t>(Record[1]);
1860 StoredTime = static_cast<time_t>(Record[2]);
1861 Overridden = static_cast<bool>(Record[3]);
1862 Filename = Blob;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001863 ResolveImportedPath(F, Filename);
1864
Hans Wennborg73945142014-03-14 17:45:06 +00001865 InputFileInfo R = { std::move(Filename), StoredSize, StoredTime, Overridden };
1866 return R;
Ben Langmuir198c1682014-03-07 07:27:49 +00001867}
1868
1869std::string ASTReader::getInputFileName(ModuleFile &F, unsigned int ID) {
Argyrios Kyrtzidisce9b49e2014-03-14 02:26:27 +00001870 return readInputFileInfo(F, ID).Filename;
Ben Langmuir198c1682014-03-07 07:27:49 +00001871}
1872
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001873InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001874 // If this ID is bogus, just return an empty input file.
1875 if (ID == 0 || ID > F.InputFilesLoaded.size())
1876 return InputFile();
1877
1878 // If we've already loaded this input file, return it.
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001879 if (F.InputFilesLoaded[ID-1].getFile())
Guy Benyei11169dd2012-12-18 14:30:41 +00001880 return F.InputFilesLoaded[ID-1];
1881
Argyrios Kyrtzidis9308f0a2014-01-08 19:13:34 +00001882 if (F.InputFilesLoaded[ID-1].isNotFound())
1883 return InputFile();
1884
Guy Benyei11169dd2012-12-18 14:30:41 +00001885 // Go find this input file.
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001886 BitstreamCursor &Cursor = F.InputFilesCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001887 SavedStreamPosition SavedPosition(Cursor);
1888 Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
1889
Argyrios Kyrtzidisce9b49e2014-03-14 02:26:27 +00001890 InputFileInfo FI = readInputFileInfo(F, ID);
1891 off_t StoredSize = FI.StoredSize;
1892 time_t StoredTime = FI.StoredTime;
1893 bool Overridden = FI.Overridden;
1894 StringRef Filename = FI.Filename;
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001895
Ben Langmuir198c1682014-03-07 07:27:49 +00001896 const FileEntry *File
1897 = Overridden? FileMgr.getVirtualFile(Filename, StoredSize, StoredTime)
1898 : FileMgr.getFile(Filename, /*OpenFile=*/false);
1899
1900 // If we didn't find the file, resolve it relative to the
1901 // original directory from which this AST file was created.
Craig Toppera13603a2014-05-22 05:54:18 +00001902 if (File == nullptr && !F.OriginalDir.empty() && !CurrentDir.empty() &&
Ben Langmuir198c1682014-03-07 07:27:49 +00001903 F.OriginalDir != CurrentDir) {
1904 std::string Resolved = resolveFileRelativeToOriginalDir(Filename,
1905 F.OriginalDir,
1906 CurrentDir);
1907 if (!Resolved.empty())
1908 File = FileMgr.getFile(Resolved);
1909 }
1910
1911 // For an overridden file, create a virtual file with the stored
1912 // size/timestamp.
Craig Toppera13603a2014-05-22 05:54:18 +00001913 if (Overridden && File == nullptr) {
Ben Langmuir198c1682014-03-07 07:27:49 +00001914 File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
1915 }
1916
Craig Toppera13603a2014-05-22 05:54:18 +00001917 if (File == nullptr) {
Ben Langmuir198c1682014-03-07 07:27:49 +00001918 if (Complain) {
1919 std::string ErrorStr = "could not find file '";
1920 ErrorStr += Filename;
1921 ErrorStr += "' referenced by AST file";
1922 Error(ErrorStr.c_str());
Guy Benyei11169dd2012-12-18 14:30:41 +00001923 }
Ben Langmuir198c1682014-03-07 07:27:49 +00001924 // Record that we didn't find the file.
1925 F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
1926 return InputFile();
1927 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001928
Ben Langmuir198c1682014-03-07 07:27:49 +00001929 // Check if there was a request to override the contents of the file
1930 // that was part of the precompiled header. Overridding such a file
1931 // can lead to problems when lexing using the source locations from the
1932 // PCH.
1933 SourceManager &SM = getSourceManager();
1934 if (!Overridden && SM.isFileOverridden(File)) {
1935 if (Complain)
1936 Error(diag::err_fe_pch_file_overridden, Filename);
1937 // After emitting the diagnostic, recover by disabling the override so
1938 // that the original file will be used.
1939 SM.disableFileContentsOverride(File);
1940 // The FileEntry is a virtual file entry with the size of the contents
1941 // that would override the original contents. Set it to the original's
1942 // size/time.
1943 FileMgr.modifyFileEntry(const_cast<FileEntry*>(File),
1944 StoredSize, StoredTime);
1945 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001946
Ben Langmuir198c1682014-03-07 07:27:49 +00001947 bool IsOutOfDate = false;
1948
1949 // For an overridden file, there is nothing to validate.
Richard Smith96fdab62014-10-28 16:24:08 +00001950 if (!Overridden && //
1951 (StoredSize != File->getSize() ||
1952#if defined(LLVM_ON_WIN32)
1953 false
1954#else
Ben Langmuir198c1682014-03-07 07:27:49 +00001955 // In our regression testing, the Windows file system seems to
1956 // have inconsistent modification times that sometimes
1957 // erroneously trigger this error-handling path.
Richard Smith96fdab62014-10-28 16:24:08 +00001958 //
1959 // This also happens in networked file systems, so disable this
1960 // check if validation is disabled or if we have an explicitly
1961 // built PCM file.
1962 //
1963 // FIXME: Should we also do this for PCH files? They could also
1964 // reasonably get shared across a network during a distributed build.
1965 (StoredTime != File->getModificationTime() && !DisableValidation &&
1966 F.Kind != MK_ExplicitModule)
Guy Benyei11169dd2012-12-18 14:30:41 +00001967#endif
Ben Langmuir198c1682014-03-07 07:27:49 +00001968 )) {
1969 if (Complain) {
1970 // Build a list of the PCH imports that got us here (in reverse).
1971 SmallVector<ModuleFile *, 4> ImportStack(1, &F);
1972 while (ImportStack.back()->ImportedBy.size() > 0)
1973 ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
Ben Langmuire82630d2014-01-17 00:19:09 +00001974
Ben Langmuir198c1682014-03-07 07:27:49 +00001975 // The top-level PCH is stale.
1976 StringRef TopLevelPCHName(ImportStack.back()->FileName);
1977 Error(diag::err_fe_pch_file_modified, Filename, TopLevelPCHName);
Ben Langmuire82630d2014-01-17 00:19:09 +00001978
Ben Langmuir198c1682014-03-07 07:27:49 +00001979 // Print the import stack.
1980 if (ImportStack.size() > 1 && !Diags.isDiagnosticInFlight()) {
1981 Diag(diag::note_pch_required_by)
1982 << Filename << ImportStack[0]->FileName;
1983 for (unsigned I = 1; I < ImportStack.size(); ++I)
Ben Langmuire82630d2014-01-17 00:19:09 +00001984 Diag(diag::note_pch_required_by)
Ben Langmuir198c1682014-03-07 07:27:49 +00001985 << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
Douglas Gregor7029ce12013-03-19 00:28:20 +00001986 }
1987
Ben Langmuir198c1682014-03-07 07:27:49 +00001988 if (!Diags.isDiagnosticInFlight())
1989 Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
Guy Benyei11169dd2012-12-18 14:30:41 +00001990 }
1991
Ben Langmuir198c1682014-03-07 07:27:49 +00001992 IsOutOfDate = true;
Guy Benyei11169dd2012-12-18 14:30:41 +00001993 }
1994
Ben Langmuir198c1682014-03-07 07:27:49 +00001995 InputFile IF = InputFile(File, Overridden, IsOutOfDate);
1996
1997 // Note that we've loaded this input file.
1998 F.InputFilesLoaded[ID-1] = IF;
1999 return IF;
Guy Benyei11169dd2012-12-18 14:30:41 +00002000}
2001
Richard Smith7ed1bc92014-12-05 22:42:13 +00002002/// \brief If we are loading a relocatable PCH or module file, and the filename
2003/// is not an absolute path, add the system or module root to the beginning of
2004/// the file name.
2005void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) {
2006 // Resolve relative to the base directory, if we have one.
2007 if (!M.BaseDirectory.empty())
2008 return ResolveImportedPath(Filename, M.BaseDirectory);
Guy Benyei11169dd2012-12-18 14:30:41 +00002009}
2010
Richard Smith7ed1bc92014-12-05 22:42:13 +00002011void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002012 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
2013 return;
2014
Richard Smith7ed1bc92014-12-05 22:42:13 +00002015 SmallString<128> Buffer;
2016 llvm::sys::path::append(Buffer, Prefix, Filename);
2017 Filename.assign(Buffer.begin(), Buffer.end());
Guy Benyei11169dd2012-12-18 14:30:41 +00002018}
2019
2020ASTReader::ASTReadResult
2021ASTReader::ReadControlBlock(ModuleFile &F,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002022 SmallVectorImpl<ImportedModule> &Loaded,
Ben Langmuirbeee15e2014-04-14 18:00:01 +00002023 const ModuleFile *ImportedBy,
Guy Benyei11169dd2012-12-18 14:30:41 +00002024 unsigned ClientLoadCapabilities) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00002025 BitstreamCursor &Stream = F.Stream;
Guy Benyei11169dd2012-12-18 14:30:41 +00002026
2027 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
2028 Error("malformed block record in AST file");
2029 return Failure;
2030 }
2031
Richard Smith1e2cf0d2014-10-31 02:28:58 +00002032 // Should we allow the configuration of the module file to differ from the
2033 // configuration of the current translation unit in a compatible way?
2034 //
2035 // FIXME: Allow this for files explicitly specified with -include-pch too.
2036 bool AllowCompatibleConfigurationMismatch = F.Kind == MK_ExplicitModule;
2037
Guy Benyei11169dd2012-12-18 14:30:41 +00002038 // Read all of the records and blocks in the control block.
2039 RecordData Record;
Richard Smitha1825302014-10-23 22:18:29 +00002040 unsigned NumInputs = 0;
2041 unsigned NumUserInputs = 0;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002042 while (1) {
2043 llvm::BitstreamEntry Entry = Stream.advance();
2044
2045 switch (Entry.Kind) {
2046 case llvm::BitstreamEntry::Error:
2047 Error("malformed block record in AST file");
2048 return Failure;
Dmitri Gribenkof430da42014-02-12 10:33:14 +00002049 case llvm::BitstreamEntry::EndBlock: {
2050 // Validate input files.
2051 const HeaderSearchOptions &HSOpts =
2052 PP.getHeaderSearchInfo().getHeaderSearchOpts();
Ben Langmuircb69b572014-03-07 06:40:32 +00002053
Richard Smitha1825302014-10-23 22:18:29 +00002054 // All user input files reside at the index range [0, NumUserInputs), and
2055 // system input files reside at [NumUserInputs, NumInputs).
Ben Langmuiracb803e2014-11-10 22:13:10 +00002056 if (!DisableValidation) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002057 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
Ben Langmuircb69b572014-03-07 06:40:32 +00002058
Dmitri Gribenkof430da42014-02-12 10:33:14 +00002059 // If we are reading a module, we will create a verification timestamp,
2060 // so we verify all input files. Otherwise, verify only user input
2061 // files.
Ben Langmuircb69b572014-03-07 06:40:32 +00002062
2063 unsigned N = NumUserInputs;
2064 if (ValidateSystemInputs ||
Richard Smithe842a472014-10-22 02:05:46 +00002065 (HSOpts.ModulesValidateOncePerBuildSession &&
Ben Langmuiracb803e2014-11-10 22:13:10 +00002066 F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp &&
Richard Smithe842a472014-10-22 02:05:46 +00002067 F.Kind == MK_ImplicitModule))
Ben Langmuircb69b572014-03-07 06:40:32 +00002068 N = NumInputs;
2069
Ben Langmuir3d4417c2014-02-07 17:31:11 +00002070 for (unsigned I = 0; I < N; ++I) {
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00002071 InputFile IF = getInputFile(F, I+1, Complain);
2072 if (!IF.getFile() || IF.isOutOfDate())
Guy Benyei11169dd2012-12-18 14:30:41 +00002073 return OutOfDate;
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00002074 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002075 }
Ben Langmuircb69b572014-03-07 06:40:32 +00002076
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +00002077 if (Listener)
2078 Listener->visitModuleFile(F.FileName);
2079
Ben Langmuircb69b572014-03-07 06:40:32 +00002080 if (Listener && Listener->needsInputFileVisitation()) {
2081 unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
2082 : NumUserInputs;
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +00002083 for (unsigned I = 0; I < N; ++I) {
2084 bool IsSystem = I >= NumUserInputs;
2085 InputFileInfo FI = readInputFileInfo(F, I+1);
2086 Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden);
2087 }
Ben Langmuircb69b572014-03-07 06:40:32 +00002088 }
2089
Guy Benyei11169dd2012-12-18 14:30:41 +00002090 return Success;
Dmitri Gribenkof430da42014-02-12 10:33:14 +00002091 }
2092
Chris Lattnere7b154b2013-01-19 21:39:22 +00002093 case llvm::BitstreamEntry::SubBlock:
2094 switch (Entry.ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002095 case INPUT_FILES_BLOCK_ID:
2096 F.InputFilesCursor = Stream;
2097 if (Stream.SkipBlock() || // Skip with the main cursor
2098 // Read the abbreviations
2099 ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
2100 Error("malformed block record in AST file");
2101 return Failure;
2102 }
2103 continue;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002104
Guy Benyei11169dd2012-12-18 14:30:41 +00002105 default:
Chris Lattnere7b154b2013-01-19 21:39:22 +00002106 if (Stream.SkipBlock()) {
2107 Error("malformed block record in AST file");
2108 return Failure;
2109 }
2110 continue;
Guy Benyei11169dd2012-12-18 14:30:41 +00002111 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00002112
2113 case llvm::BitstreamEntry::Record:
2114 // The interesting case.
2115 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002116 }
2117
2118 // Read and process a record.
2119 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00002120 StringRef Blob;
2121 switch ((ControlRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002122 case METADATA: {
2123 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2124 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
Dmitri Gribenko2228cd32014-02-11 15:40:09 +00002125 Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
2126 : diag::err_pch_version_too_new);
Guy Benyei11169dd2012-12-18 14:30:41 +00002127 return VersionMismatch;
2128 }
2129
2130 bool hasErrors = Record[5];
2131 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
2132 Diag(diag::err_pch_with_compiler_errors);
2133 return HadErrors;
2134 }
2135
2136 F.RelocatablePCH = Record[4];
Richard Smith7ed1bc92014-12-05 22:42:13 +00002137 // Relative paths in a relocatable PCH are relative to our sysroot.
2138 if (F.RelocatablePCH)
2139 F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
Guy Benyei11169dd2012-12-18 14:30:41 +00002140
2141 const std::string &CurBranch = getClangFullRepositoryVersion();
Chris Lattner0e6c9402013-01-20 02:38:54 +00002142 StringRef ASTBranch = Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00002143 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2144 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
Dmitri Gribenko2228cd32014-02-11 15:40:09 +00002145 Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
Guy Benyei11169dd2012-12-18 14:30:41 +00002146 return VersionMismatch;
2147 }
2148 break;
2149 }
2150
Ben Langmuir487ea142014-10-23 18:05:36 +00002151 case SIGNATURE:
2152 assert((!F.Signature || F.Signature == Record[0]) && "signature changed");
2153 F.Signature = Record[0];
2154 break;
2155
Guy Benyei11169dd2012-12-18 14:30:41 +00002156 case IMPORTS: {
2157 // Load each of the imported PCH files.
2158 unsigned Idx = 0, N = Record.size();
2159 while (Idx < N) {
2160 // Read information about the AST file.
2161 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
2162 // The import location will be the local one for now; we will adjust
2163 // all import locations of module imports after the global source
2164 // location info are setup.
2165 SourceLocation ImportLoc =
2166 SourceLocation::getFromRawEncoding(Record[Idx++]);
Douglas Gregor7029ce12013-03-19 00:28:20 +00002167 off_t StoredSize = (off_t)Record[Idx++];
2168 time_t StoredModTime = (time_t)Record[Idx++];
Ben Langmuir487ea142014-10-23 18:05:36 +00002169 ASTFileSignature StoredSignature = Record[Idx++];
Richard Smith7ed1bc92014-12-05 22:42:13 +00002170 auto ImportedFile = ReadPath(F, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00002171
2172 // Load the AST file.
2173 switch(ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F, Loaded,
Ben Langmuir487ea142014-10-23 18:05:36 +00002174 StoredSize, StoredModTime, StoredSignature,
Guy Benyei11169dd2012-12-18 14:30:41 +00002175 ClientLoadCapabilities)) {
2176 case Failure: return Failure;
2177 // If we have to ignore the dependency, we'll have to ignore this too.
Douglas Gregor2f1806e2013-03-19 00:38:50 +00002178 case Missing:
Guy Benyei11169dd2012-12-18 14:30:41 +00002179 case OutOfDate: return OutOfDate;
2180 case VersionMismatch: return VersionMismatch;
2181 case ConfigurationMismatch: return ConfigurationMismatch;
2182 case HadErrors: return HadErrors;
2183 case Success: break;
2184 }
2185 }
2186 break;
2187 }
2188
Richard Smith7f330cd2015-03-18 01:42:29 +00002189 case KNOWN_MODULE_FILES:
2190 break;
2191
Guy Benyei11169dd2012-12-18 14:30:41 +00002192 case LANGUAGE_OPTIONS: {
2193 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
Richard Smith1e2cf0d2014-10-31 02:28:58 +00002194 // FIXME: The &F == *ModuleMgr.begin() check is wrong for modules.
Guy Benyei11169dd2012-12-18 14:30:41 +00002195 if (Listener && &F == *ModuleMgr.begin() &&
Richard Smith1e2cf0d2014-10-31 02:28:58 +00002196 ParseLanguageOptions(Record, Complain, *Listener,
2197 AllowCompatibleConfigurationMismatch) &&
Ben Langmuir2cb4a782014-02-05 22:21:15 +00002198 !DisableValidation && !AllowConfigurationMismatch)
Guy Benyei11169dd2012-12-18 14:30:41 +00002199 return ConfigurationMismatch;
2200 break;
2201 }
2202
2203 case TARGET_OPTIONS: {
2204 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2205 if (Listener && &F == *ModuleMgr.begin() &&
Chandler Carruth0d745bc2015-03-14 04:47:43 +00002206 ParseTargetOptions(Record, Complain, *Listener,
2207 AllowCompatibleConfigurationMismatch) &&
Ben Langmuir2cb4a782014-02-05 22:21:15 +00002208 !DisableValidation && !AllowConfigurationMismatch)
Guy Benyei11169dd2012-12-18 14:30:41 +00002209 return ConfigurationMismatch;
2210 break;
2211 }
2212
2213 case DIAGNOSTIC_OPTIONS: {
Ben Langmuirb92de022014-04-29 16:25:26 +00002214 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate)==0;
Guy Benyei11169dd2012-12-18 14:30:41 +00002215 if (Listener && &F == *ModuleMgr.begin() &&
Richard Smith1e2cf0d2014-10-31 02:28:58 +00002216 !AllowCompatibleConfigurationMismatch &&
Guy Benyei11169dd2012-12-18 14:30:41 +00002217 ParseDiagnosticOptions(Record, Complain, *Listener) &&
Ben Langmuirb92de022014-04-29 16:25:26 +00002218 !DisableValidation)
2219 return OutOfDate;
Guy Benyei11169dd2012-12-18 14:30:41 +00002220 break;
2221 }
2222
2223 case FILE_SYSTEM_OPTIONS: {
2224 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2225 if (Listener && &F == *ModuleMgr.begin() &&
Richard Smith1e2cf0d2014-10-31 02:28:58 +00002226 !AllowCompatibleConfigurationMismatch &&
Guy Benyei11169dd2012-12-18 14:30:41 +00002227 ParseFileSystemOptions(Record, Complain, *Listener) &&
Ben Langmuir2cb4a782014-02-05 22:21:15 +00002228 !DisableValidation && !AllowConfigurationMismatch)
Guy Benyei11169dd2012-12-18 14:30:41 +00002229 return ConfigurationMismatch;
2230 break;
2231 }
2232
2233 case HEADER_SEARCH_OPTIONS: {
2234 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2235 if (Listener && &F == *ModuleMgr.begin() &&
Richard Smith1e2cf0d2014-10-31 02:28:58 +00002236 !AllowCompatibleConfigurationMismatch &&
Guy Benyei11169dd2012-12-18 14:30:41 +00002237 ParseHeaderSearchOptions(Record, Complain, *Listener) &&
Ben Langmuir2cb4a782014-02-05 22:21:15 +00002238 !DisableValidation && !AllowConfigurationMismatch)
Guy Benyei11169dd2012-12-18 14:30:41 +00002239 return ConfigurationMismatch;
2240 break;
2241 }
2242
2243 case PREPROCESSOR_OPTIONS: {
2244 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2245 if (Listener && &F == *ModuleMgr.begin() &&
Richard Smith1e2cf0d2014-10-31 02:28:58 +00002246 !AllowCompatibleConfigurationMismatch &&
Guy Benyei11169dd2012-12-18 14:30:41 +00002247 ParsePreprocessorOptions(Record, Complain, *Listener,
2248 SuggestedPredefines) &&
Ben Langmuir2cb4a782014-02-05 22:21:15 +00002249 !DisableValidation && !AllowConfigurationMismatch)
Guy Benyei11169dd2012-12-18 14:30:41 +00002250 return ConfigurationMismatch;
2251 break;
2252 }
2253
2254 case ORIGINAL_FILE:
2255 F.OriginalSourceFileID = FileID::get(Record[0]);
Chris Lattner0e6c9402013-01-20 02:38:54 +00002256 F.ActualOriginalSourceFileName = Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00002257 F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
Richard Smith7ed1bc92014-12-05 22:42:13 +00002258 ResolveImportedPath(F, F.OriginalSourceFileName);
Guy Benyei11169dd2012-12-18 14:30:41 +00002259 break;
2260
2261 case ORIGINAL_FILE_ID:
2262 F.OriginalSourceFileID = FileID::get(Record[0]);
2263 break;
2264
2265 case ORIGINAL_PCH_DIR:
Chris Lattner0e6c9402013-01-20 02:38:54 +00002266 F.OriginalDir = Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00002267 break;
2268
Ben Langmuirbeee15e2014-04-14 18:00:01 +00002269 case MODULE_NAME:
2270 F.ModuleName = Blob;
Ben Langmuir4f5212a2014-04-14 22:12:44 +00002271 if (Listener)
2272 Listener->ReadModuleName(F.ModuleName);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00002273 break;
2274
Richard Smith223d3f22014-12-06 03:21:08 +00002275 case MODULE_DIRECTORY: {
2276 assert(!F.ModuleName.empty() &&
2277 "MODULE_DIRECTORY found before MODULE_NAME");
2278 // If we've already loaded a module map file covering this module, we may
2279 // have a better path for it (relative to the current build).
2280 Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
2281 if (M && M->Directory) {
2282 // If we're implicitly loading a module, the base directory can't
2283 // change between the build and use.
2284 if (F.Kind != MK_ExplicitModule) {
2285 const DirectoryEntry *BuildDir =
2286 PP.getFileManager().getDirectory(Blob);
2287 if (!BuildDir || BuildDir != M->Directory) {
2288 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2289 Diag(diag::err_imported_module_relocated)
2290 << F.ModuleName << Blob << M->Directory->getName();
2291 return OutOfDate;
2292 }
2293 }
2294 F.BaseDirectory = M->Directory->getName();
2295 } else {
2296 F.BaseDirectory = Blob;
2297 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00002298 break;
Richard Smith223d3f22014-12-06 03:21:08 +00002299 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00002300
Ben Langmuirbeee15e2014-04-14 18:00:01 +00002301 case MODULE_MAP_FILE:
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00002302 if (ASTReadResult Result =
2303 ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
2304 return Result;
Ben Langmuir264ea152014-11-08 00:06:39 +00002305 break;
2306
Guy Benyei11169dd2012-12-18 14:30:41 +00002307 case INPUT_FILE_OFFSETS:
Richard Smitha1825302014-10-23 22:18:29 +00002308 NumInputs = Record[0];
2309 NumUserInputs = Record[1];
Richard Smithec216502015-02-13 19:48:37 +00002310 F.InputFileOffsets = (const uint64_t *)Blob.data();
Richard Smitha1825302014-10-23 22:18:29 +00002311 F.InputFilesLoaded.resize(NumInputs);
Guy Benyei11169dd2012-12-18 14:30:41 +00002312 break;
2313 }
2314 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002315}
2316
Ben Langmuir2c9af442014-04-10 17:57:43 +00002317ASTReader::ASTReadResult
2318ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00002319 BitstreamCursor &Stream = F.Stream;
Guy Benyei11169dd2012-12-18 14:30:41 +00002320
2321 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
2322 Error("malformed block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002323 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002324 }
2325
2326 // Read all of the records and blocks for the AST file.
2327 RecordData Record;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002328 while (1) {
2329 llvm::BitstreamEntry Entry = Stream.advance();
2330
2331 switch (Entry.Kind) {
2332 case llvm::BitstreamEntry::Error:
2333 Error("error at end of module block in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002334 return Failure;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002335 case llvm::BitstreamEntry::EndBlock: {
Richard Smithc0fbba72013-04-03 22:49:41 +00002336 // Outside of C++, we do not store a lookup map for the translation unit.
2337 // Instead, mark it as needing a lookup map to be built if this module
2338 // contains any declarations lexically within it (which it always does!).
2339 // This usually has no cost, since we very rarely need the lookup map for
2340 // the translation unit outside C++.
Guy Benyei11169dd2012-12-18 14:30:41 +00002341 DeclContext *DC = Context.getTranslationUnitDecl();
Richard Smithc0fbba72013-04-03 22:49:41 +00002342 if (DC->hasExternalLexicalStorage() &&
2343 !getContext().getLangOpts().CPlusPlus)
Guy Benyei11169dd2012-12-18 14:30:41 +00002344 DC->setMustBuildLookupTable();
Chris Lattnere7b154b2013-01-19 21:39:22 +00002345
Ben Langmuir2c9af442014-04-10 17:57:43 +00002346 return Success;
Guy Benyei11169dd2012-12-18 14:30:41 +00002347 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00002348 case llvm::BitstreamEntry::SubBlock:
2349 switch (Entry.ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002350 case DECLTYPES_BLOCK_ID:
2351 // We lazily load the decls block, but we want to set up the
2352 // DeclsCursor cursor to point into it. Clone our current bitcode
2353 // cursor to it, enter the block and read the abbrevs in that block.
2354 // With the main cursor, we just skip over it.
2355 F.DeclsCursor = Stream;
2356 if (Stream.SkipBlock() || // Skip with the main cursor.
2357 // Read the abbrevs.
2358 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
2359 Error("malformed block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002360 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002361 }
2362 break;
Richard Smithb9eab6d2014-03-20 19:44:17 +00002363
Guy Benyei11169dd2012-12-18 14:30:41 +00002364 case PREPROCESSOR_BLOCK_ID:
2365 F.MacroCursor = Stream;
2366 if (!PP.getExternalSource())
2367 PP.setExternalSource(this);
Chris Lattnere7b154b2013-01-19 21:39:22 +00002368
Guy Benyei11169dd2012-12-18 14:30:41 +00002369 if (Stream.SkipBlock() ||
2370 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
2371 Error("malformed block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002372 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002373 }
2374 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
2375 break;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002376
Guy Benyei11169dd2012-12-18 14:30:41 +00002377 case PREPROCESSOR_DETAIL_BLOCK_ID:
2378 F.PreprocessorDetailCursor = Stream;
2379 if (Stream.SkipBlock() ||
Chris Lattnere7b154b2013-01-19 21:39:22 +00002380 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
Guy Benyei11169dd2012-12-18 14:30:41 +00002381 PREPROCESSOR_DETAIL_BLOCK_ID)) {
Chris Lattnere7b154b2013-01-19 21:39:22 +00002382 Error("malformed preprocessor detail record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002383 return Failure;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002384 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002385 F.PreprocessorDetailStartOffset
Chris Lattnere7b154b2013-01-19 21:39:22 +00002386 = F.PreprocessorDetailCursor.GetCurrentBitNo();
2387
Guy Benyei11169dd2012-12-18 14:30:41 +00002388 if (!PP.getPreprocessingRecord())
2389 PP.createPreprocessingRecord();
2390 if (!PP.getPreprocessingRecord()->getExternalSource())
2391 PP.getPreprocessingRecord()->SetExternalSource(*this);
2392 break;
2393
2394 case SOURCE_MANAGER_BLOCK_ID:
2395 if (ReadSourceManagerBlock(F))
Ben Langmuir2c9af442014-04-10 17:57:43 +00002396 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002397 break;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002398
Guy Benyei11169dd2012-12-18 14:30:41 +00002399 case SUBMODULE_BLOCK_ID:
Ben Langmuir2c9af442014-04-10 17:57:43 +00002400 if (ASTReadResult Result = ReadSubmoduleBlock(F, ClientLoadCapabilities))
2401 return Result;
Guy Benyei11169dd2012-12-18 14:30:41 +00002402 break;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002403
Guy Benyei11169dd2012-12-18 14:30:41 +00002404 case COMMENTS_BLOCK_ID: {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00002405 BitstreamCursor C = Stream;
Guy Benyei11169dd2012-12-18 14:30:41 +00002406 if (Stream.SkipBlock() ||
2407 ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
2408 Error("malformed comments block in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002409 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002410 }
2411 CommentsCursors.push_back(std::make_pair(C, &F));
2412 break;
2413 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00002414
Guy Benyei11169dd2012-12-18 14:30:41 +00002415 default:
Chris Lattnere7b154b2013-01-19 21:39:22 +00002416 if (Stream.SkipBlock()) {
2417 Error("malformed block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002418 return Failure;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002419 }
2420 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002421 }
2422 continue;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002423
2424 case llvm::BitstreamEntry::Record:
2425 // The interesting case.
2426 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002427 }
2428
2429 // Read and process a record.
2430 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00002431 StringRef Blob;
2432 switch ((ASTRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002433 default: // Default behavior: ignore.
2434 break;
2435
2436 case TYPE_OFFSET: {
2437 if (F.LocalNumTypes != 0) {
2438 Error("duplicate TYPE_OFFSET record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002439 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002440 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00002441 F.TypeOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002442 F.LocalNumTypes = Record[0];
2443 unsigned LocalBaseTypeIndex = Record[1];
2444 F.BaseTypeIndex = getTotalNumTypes();
2445
2446 if (F.LocalNumTypes > 0) {
2447 // Introduce the global -> local mapping for types within this module.
2448 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
2449
2450 // Introduce the local -> global mapping for types within this module.
2451 F.TypeRemap.insertOrReplace(
2452 std::make_pair(LocalBaseTypeIndex,
2453 F.BaseTypeIndex - LocalBaseTypeIndex));
Ben Langmuir52ca6782014-10-20 16:27:32 +00002454
2455 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
Guy Benyei11169dd2012-12-18 14:30:41 +00002456 }
2457 break;
2458 }
2459
2460 case DECL_OFFSET: {
2461 if (F.LocalNumDecls != 0) {
2462 Error("duplicate DECL_OFFSET record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002463 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002464 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00002465 F.DeclOffsets = (const DeclOffset *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002466 F.LocalNumDecls = Record[0];
2467 unsigned LocalBaseDeclID = Record[1];
2468 F.BaseDeclID = getTotalNumDecls();
2469
2470 if (F.LocalNumDecls > 0) {
2471 // Introduce the global -> local mapping for declarations within this
2472 // module.
2473 GlobalDeclMap.insert(
2474 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
2475
2476 // Introduce the local -> global mapping for declarations within this
2477 // module.
2478 F.DeclRemap.insertOrReplace(
2479 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
2480
2481 // Introduce the global -> local mapping for declarations within this
2482 // module.
2483 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
Ben Langmuirfe971d92014-08-16 04:54:18 +00002484
Ben Langmuir52ca6782014-10-20 16:27:32 +00002485 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
2486 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002487 break;
2488 }
2489
2490 case TU_UPDATE_LEXICAL: {
2491 DeclContext *TU = Context.getTranslationUnitDecl();
2492 DeclContextInfo &Info = F.DeclContextInfos[TU];
Chris Lattner0e6c9402013-01-20 02:38:54 +00002493 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(Blob.data());
Guy Benyei11169dd2012-12-18 14:30:41 +00002494 Info.NumLexicalDecls
Chris Lattner0e6c9402013-01-20 02:38:54 +00002495 = static_cast<unsigned int>(Blob.size() / sizeof(KindDeclIDPair));
Guy Benyei11169dd2012-12-18 14:30:41 +00002496 TU->setHasExternalLexicalStorage(true);
2497 break;
2498 }
2499
2500 case UPDATE_VISIBLE: {
2501 unsigned Idx = 0;
2502 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
2503 ASTDeclContextNameLookupTable *Table =
Justin Bognerda4e6502014-04-14 16:34:29 +00002504 ASTDeclContextNameLookupTable::Create(
2505 (const unsigned char *)Blob.data() + Record[Idx++],
2506 (const unsigned char *)Blob.data() + sizeof(uint32_t),
2507 (const unsigned char *)Blob.data(),
2508 ASTDeclContextNameLookupTrait(*this, F));
Richard Smithcd45dbc2014-04-19 03:48:30 +00002509 if (Decl *D = GetExistingDecl(ID)) {
Richard Smithd9174792014-03-11 03:10:46 +00002510 auto *DC = cast<DeclContext>(D);
2511 DC->getPrimaryContext()->setHasExternalVisibleStorage(true);
Richard Smith52e3fba2014-03-11 07:17:35 +00002512 auto *&LookupTable = F.DeclContextInfos[DC].NameLookupTableData;
2513 delete LookupTable;
2514 LookupTable = Table;
Guy Benyei11169dd2012-12-18 14:30:41 +00002515 } else
2516 PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F));
2517 break;
2518 }
2519
2520 case IDENTIFIER_TABLE:
Chris Lattner0e6c9402013-01-20 02:38:54 +00002521 F.IdentifierTableData = Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002522 if (Record[0]) {
Justin Bognerda4e6502014-04-14 16:34:29 +00002523 F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
2524 (const unsigned char *)F.IdentifierTableData + Record[0],
2525 (const unsigned char *)F.IdentifierTableData + sizeof(uint32_t),
2526 (const unsigned char *)F.IdentifierTableData,
2527 ASTIdentifierLookupTrait(*this, F));
Guy Benyei11169dd2012-12-18 14:30:41 +00002528
2529 PP.getIdentifierTable().setExternalIdentifierLookup(this);
2530 }
2531 break;
2532
2533 case IDENTIFIER_OFFSET: {
2534 if (F.LocalNumIdentifiers != 0) {
2535 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002536 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002537 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00002538 F.IdentifierOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002539 F.LocalNumIdentifiers = Record[0];
2540 unsigned LocalBaseIdentifierID = Record[1];
2541 F.BaseIdentifierID = getTotalNumIdentifiers();
2542
2543 if (F.LocalNumIdentifiers > 0) {
2544 // Introduce the global -> local mapping for identifiers within this
2545 // module.
2546 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
2547 &F));
2548
2549 // Introduce the local -> global mapping for identifiers within this
2550 // module.
2551 F.IdentifierRemap.insertOrReplace(
2552 std::make_pair(LocalBaseIdentifierID,
2553 F.BaseIdentifierID - LocalBaseIdentifierID));
Ben Langmuirfe971d92014-08-16 04:54:18 +00002554
Ben Langmuir52ca6782014-10-20 16:27:32 +00002555 IdentifiersLoaded.resize(IdentifiersLoaded.size()
2556 + F.LocalNumIdentifiers);
2557 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002558 break;
2559 }
2560
Ben Langmuir332aafe2014-01-31 01:06:56 +00002561 case EAGERLY_DESERIALIZED_DECLS:
Richard Smith9e2341d2015-03-23 03:25:59 +00002562 // FIXME: Skip reading this record if our ASTConsumer doesn't care
2563 // about "interesting" decls (for instance, if we're building a module).
Guy Benyei11169dd2012-12-18 14:30:41 +00002564 for (unsigned I = 0, N = Record.size(); I != N; ++I)
Ben Langmuir332aafe2014-01-31 01:06:56 +00002565 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
Guy Benyei11169dd2012-12-18 14:30:41 +00002566 break;
2567
2568 case SPECIAL_TYPES:
Douglas Gregor44180f82013-02-01 23:45:03 +00002569 if (SpecialTypes.empty()) {
2570 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2571 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
2572 break;
2573 }
2574
2575 if (SpecialTypes.size() != Record.size()) {
2576 Error("invalid special-types record");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002577 return Failure;
Douglas Gregor44180f82013-02-01 23:45:03 +00002578 }
2579
2580 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2581 serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
2582 if (!SpecialTypes[I])
2583 SpecialTypes[I] = ID;
2584 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
2585 // merge step?
2586 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002587 break;
2588
2589 case STATISTICS:
2590 TotalNumStatements += Record[0];
2591 TotalNumMacros += Record[1];
2592 TotalLexicalDeclContexts += Record[2];
2593 TotalVisibleDeclContexts += Record[3];
2594 break;
2595
2596 case UNUSED_FILESCOPED_DECLS:
2597 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2598 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
2599 break;
2600
2601 case DELEGATING_CTORS:
2602 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2603 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
2604 break;
2605
2606 case WEAK_UNDECLARED_IDENTIFIERS:
2607 if (Record.size() % 4 != 0) {
2608 Error("invalid weak identifiers record");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002609 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002610 }
2611
2612 // FIXME: Ignore weak undeclared identifiers from non-original PCH
2613 // files. This isn't the way to do it :)
2614 WeakUndeclaredIdentifiers.clear();
2615
2616 // Translate the weak, undeclared identifiers into global IDs.
2617 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
2618 WeakUndeclaredIdentifiers.push_back(
2619 getGlobalIdentifierID(F, Record[I++]));
2620 WeakUndeclaredIdentifiers.push_back(
2621 getGlobalIdentifierID(F, Record[I++]));
2622 WeakUndeclaredIdentifiers.push_back(
2623 ReadSourceLocation(F, Record, I).getRawEncoding());
2624 WeakUndeclaredIdentifiers.push_back(Record[I++]);
2625 }
2626 break;
2627
Guy Benyei11169dd2012-12-18 14:30:41 +00002628 case SELECTOR_OFFSETS: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00002629 F.SelectorOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002630 F.LocalNumSelectors = Record[0];
2631 unsigned LocalBaseSelectorID = Record[1];
2632 F.BaseSelectorID = getTotalNumSelectors();
2633
2634 if (F.LocalNumSelectors > 0) {
2635 // Introduce the global -> local mapping for selectors within this
2636 // module.
2637 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
2638
2639 // Introduce the local -> global mapping for selectors within this
2640 // module.
2641 F.SelectorRemap.insertOrReplace(
2642 std::make_pair(LocalBaseSelectorID,
2643 F.BaseSelectorID - LocalBaseSelectorID));
Ben Langmuir52ca6782014-10-20 16:27:32 +00002644
2645 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
Guy Benyei11169dd2012-12-18 14:30:41 +00002646 }
2647 break;
2648 }
2649
2650 case METHOD_POOL:
Chris Lattner0e6c9402013-01-20 02:38:54 +00002651 F.SelectorLookupTableData = (const unsigned char *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002652 if (Record[0])
2653 F.SelectorLookupTable
2654 = ASTSelectorLookupTable::Create(
2655 F.SelectorLookupTableData + Record[0],
2656 F.SelectorLookupTableData,
2657 ASTSelectorLookupTrait(*this, F));
2658 TotalNumMethodPoolEntries += Record[1];
2659 break;
2660
2661 case REFERENCED_SELECTOR_POOL:
2662 if (!Record.empty()) {
2663 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
2664 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
2665 Record[Idx++]));
2666 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
2667 getRawEncoding());
2668 }
2669 }
2670 break;
2671
2672 case PP_COUNTER_VALUE:
2673 if (!Record.empty() && Listener)
2674 Listener->ReadCounter(F, Record[0]);
2675 break;
2676
2677 case FILE_SORTED_DECLS:
Chris Lattner0e6c9402013-01-20 02:38:54 +00002678 F.FileSortedDecls = (const DeclID *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002679 F.NumFileSortedDecls = Record[0];
2680 break;
2681
2682 case SOURCE_LOCATION_OFFSETS: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00002683 F.SLocEntryOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002684 F.LocalNumSLocEntries = Record[0];
2685 unsigned SLocSpaceSize = Record[1];
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00002686 std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
Ben Langmuir52ca6782014-10-20 16:27:32 +00002687 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
Guy Benyei11169dd2012-12-18 14:30:41 +00002688 SLocSpaceSize);
2689 // Make our entry in the range map. BaseID is negative and growing, so
2690 // we invert it. Because we invert it, though, we need the other end of
2691 // the range.
2692 unsigned RangeStart =
2693 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
2694 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
2695 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
2696
2697 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
2698 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
2699 GlobalSLocOffsetMap.insert(
2700 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
2701 - SLocSpaceSize,&F));
2702
2703 // Initialize the remapping table.
2704 // Invalid stays invalid.
Richard Smithb9eab6d2014-03-20 19:44:17 +00002705 F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
Guy Benyei11169dd2012-12-18 14:30:41 +00002706 // This module. Base was 2 when being compiled.
Richard Smithb9eab6d2014-03-20 19:44:17 +00002707 F.SLocRemap.insertOrReplace(std::make_pair(2U,
Guy Benyei11169dd2012-12-18 14:30:41 +00002708 static_cast<int>(F.SLocEntryBaseOffset - 2)));
2709
2710 TotalNumSLocEntries += F.LocalNumSLocEntries;
2711 break;
2712 }
2713
2714 case MODULE_OFFSET_MAP: {
2715 // Additional remapping information.
Chris Lattner0e6c9402013-01-20 02:38:54 +00002716 const unsigned char *Data = (const unsigned char*)Blob.data();
2717 const unsigned char *DataEnd = Data + Blob.size();
Richard Smithb9eab6d2014-03-20 19:44:17 +00002718
2719 // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
2720 if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
2721 F.SLocRemap.insert(std::make_pair(0U, 0));
2722 F.SLocRemap.insert(std::make_pair(2U, 1));
2723 }
2724
Guy Benyei11169dd2012-12-18 14:30:41 +00002725 // Continuous range maps we may be updating in our module.
Ben Langmuir785180e2014-10-20 16:27:30 +00002726 typedef ContinuousRangeMap<uint32_t, int, 2>::Builder
2727 RemapBuilder;
2728 RemapBuilder SLocRemap(F.SLocRemap);
2729 RemapBuilder IdentifierRemap(F.IdentifierRemap);
2730 RemapBuilder MacroRemap(F.MacroRemap);
2731 RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
2732 RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
2733 RemapBuilder SelectorRemap(F.SelectorRemap);
2734 RemapBuilder DeclRemap(F.DeclRemap);
2735 RemapBuilder TypeRemap(F.TypeRemap);
Guy Benyei11169dd2012-12-18 14:30:41 +00002736
2737 while(Data < DataEnd) {
Justin Bogner57ba0b22014-03-28 22:03:24 +00002738 using namespace llvm::support;
2739 uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data);
Guy Benyei11169dd2012-12-18 14:30:41 +00002740 StringRef Name = StringRef((const char*)Data, Len);
2741 Data += Len;
2742 ModuleFile *OM = ModuleMgr.lookup(Name);
2743 if (!OM) {
2744 Error("SourceLocation remap refers to unknown module");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002745 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002746 }
2747
Justin Bogner57ba0b22014-03-28 22:03:24 +00002748 uint32_t SLocOffset =
2749 endian::readNext<uint32_t, little, unaligned>(Data);
2750 uint32_t IdentifierIDOffset =
2751 endian::readNext<uint32_t, little, unaligned>(Data);
2752 uint32_t MacroIDOffset =
2753 endian::readNext<uint32_t, little, unaligned>(Data);
2754 uint32_t PreprocessedEntityIDOffset =
2755 endian::readNext<uint32_t, little, unaligned>(Data);
2756 uint32_t SubmoduleIDOffset =
2757 endian::readNext<uint32_t, little, unaligned>(Data);
2758 uint32_t SelectorIDOffset =
2759 endian::readNext<uint32_t, little, unaligned>(Data);
2760 uint32_t DeclIDOffset =
2761 endian::readNext<uint32_t, little, unaligned>(Data);
2762 uint32_t TypeIndexOffset =
2763 endian::readNext<uint32_t, little, unaligned>(Data);
2764
Ben Langmuir785180e2014-10-20 16:27:30 +00002765 uint32_t None = std::numeric_limits<uint32_t>::max();
2766
2767 auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
2768 RemapBuilder &Remap) {
2769 if (Offset != None)
2770 Remap.insert(std::make_pair(Offset,
2771 static_cast<int>(BaseOffset - Offset)));
2772 };
2773 mapOffset(SLocOffset, OM->SLocEntryBaseOffset, SLocRemap);
2774 mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap);
2775 mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
2776 mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
2777 PreprocessedEntityRemap);
2778 mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
2779 mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
2780 mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap);
2781 mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap);
Guy Benyei11169dd2012-12-18 14:30:41 +00002782
2783 // Global -> local mappings.
2784 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
2785 }
2786 break;
2787 }
2788
2789 case SOURCE_MANAGER_LINE_TABLE:
2790 if (ParseLineTable(F, Record))
Ben Langmuir2c9af442014-04-10 17:57:43 +00002791 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002792 break;
2793
2794 case SOURCE_LOCATION_PRELOADS: {
2795 // Need to transform from the local view (1-based IDs) to the global view,
2796 // which is based off F.SLocEntryBaseID.
2797 if (!F.PreloadSLocEntries.empty()) {
2798 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002799 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002800 }
2801
2802 F.PreloadSLocEntries.swap(Record);
2803 break;
2804 }
2805
2806 case EXT_VECTOR_DECLS:
2807 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2808 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
2809 break;
2810
2811 case VTABLE_USES:
2812 if (Record.size() % 3 != 0) {
2813 Error("Invalid VTABLE_USES record");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002814 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002815 }
2816
2817 // Later tables overwrite earlier ones.
2818 // FIXME: Modules will have some trouble with this. This is clearly not
2819 // the right way to do this.
2820 VTableUses.clear();
2821
2822 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
2823 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
2824 VTableUses.push_back(
2825 ReadSourceLocation(F, Record, Idx).getRawEncoding());
2826 VTableUses.push_back(Record[Idx++]);
2827 }
2828 break;
2829
Guy Benyei11169dd2012-12-18 14:30:41 +00002830 case PENDING_IMPLICIT_INSTANTIATIONS:
2831 if (PendingInstantiations.size() % 2 != 0) {
2832 Error("Invalid existing PendingInstantiations");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002833 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002834 }
2835
2836 if (Record.size() % 2 != 0) {
2837 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002838 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002839 }
2840
2841 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2842 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
2843 PendingInstantiations.push_back(
2844 ReadSourceLocation(F, Record, I).getRawEncoding());
2845 }
2846 break;
2847
2848 case SEMA_DECL_REFS:
Richard Smith3d8e97e2013-10-18 06:54:39 +00002849 if (Record.size() != 2) {
2850 Error("Invalid SEMA_DECL_REFS block");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002851 return Failure;
Richard Smith3d8e97e2013-10-18 06:54:39 +00002852 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002853 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2854 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
2855 break;
2856
2857 case PPD_ENTITIES_OFFSETS: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00002858 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
2859 assert(Blob.size() % sizeof(PPEntityOffset) == 0);
2860 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
Guy Benyei11169dd2012-12-18 14:30:41 +00002861
2862 unsigned LocalBasePreprocessedEntityID = Record[0];
2863
2864 unsigned StartingID;
2865 if (!PP.getPreprocessingRecord())
2866 PP.createPreprocessingRecord();
2867 if (!PP.getPreprocessingRecord()->getExternalSource())
2868 PP.getPreprocessingRecord()->SetExternalSource(*this);
2869 StartingID
2870 = PP.getPreprocessingRecord()
Ben Langmuir52ca6782014-10-20 16:27:32 +00002871 ->allocateLoadedEntities(F.NumPreprocessedEntities);
Guy Benyei11169dd2012-12-18 14:30:41 +00002872 F.BasePreprocessedEntityID = StartingID;
2873
2874 if (F.NumPreprocessedEntities > 0) {
2875 // Introduce the global -> local mapping for preprocessed entities in
2876 // this module.
2877 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
2878
2879 // Introduce the local -> global mapping for preprocessed entities in
2880 // this module.
2881 F.PreprocessedEntityRemap.insertOrReplace(
2882 std::make_pair(LocalBasePreprocessedEntityID,
2883 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
2884 }
2885
2886 break;
2887 }
2888
2889 case DECL_UPDATE_OFFSETS: {
2890 if (Record.size() % 2 != 0) {
2891 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002892 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002893 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00002894 for (unsigned I = 0, N = Record.size(); I != N; I += 2) {
2895 GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
2896 DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1]));
2897
2898 // If we've already loaded the decl, perform the updates when we finish
2899 // loading this block.
2900 if (Decl *D = GetExistingDecl(ID))
2901 PendingUpdateRecords.push_back(std::make_pair(ID, D));
2902 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002903 break;
2904 }
2905
2906 case DECL_REPLACEMENTS: {
2907 if (Record.size() % 3 != 0) {
2908 Error("invalid DECL_REPLACEMENTS block in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002909 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002910 }
2911 for (unsigned I = 0, N = Record.size(); I != N; I += 3)
2912 ReplacedDecls[getGlobalDeclID(F, Record[I])]
2913 = ReplacedDeclInfo(&F, Record[I+1], Record[I+2]);
2914 break;
2915 }
2916
2917 case OBJC_CATEGORIES_MAP: {
2918 if (F.LocalNumObjCCategoriesInMap != 0) {
2919 Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002920 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002921 }
2922
2923 F.LocalNumObjCCategoriesInMap = Record[0];
Chris Lattner0e6c9402013-01-20 02:38:54 +00002924 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002925 break;
2926 }
2927
2928 case OBJC_CATEGORIES:
2929 F.ObjCCategories.swap(Record);
2930 break;
Richard Smithc2bb8182015-03-24 06:36:48 +00002931
Guy Benyei11169dd2012-12-18 14:30:41 +00002932 case CXX_BASE_SPECIFIER_OFFSETS: {
2933 if (F.LocalNumCXXBaseSpecifiers != 0) {
2934 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00002935 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00002936 }
Richard Smithc2bb8182015-03-24 06:36:48 +00002937
Guy Benyei11169dd2012-12-18 14:30:41 +00002938 F.LocalNumCXXBaseSpecifiers = Record[0];
Chris Lattner0e6c9402013-01-20 02:38:54 +00002939 F.CXXBaseSpecifiersOffsets = (const uint32_t *)Blob.data();
Richard Smithc2bb8182015-03-24 06:36:48 +00002940 break;
2941 }
2942
2943 case CXX_CTOR_INITIALIZERS_OFFSETS: {
2944 if (F.LocalNumCXXCtorInitializers != 0) {
2945 Error("duplicate CXX_CTOR_INITIALIZERS_OFFSETS record in AST file");
2946 return Failure;
2947 }
2948
2949 F.LocalNumCXXCtorInitializers = Record[0];
2950 F.CXXCtorInitializersOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002951 break;
2952 }
2953
2954 case DIAG_PRAGMA_MAPPINGS:
2955 if (F.PragmaDiagMappings.empty())
2956 F.PragmaDiagMappings.swap(Record);
2957 else
2958 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
2959 Record.begin(), Record.end());
2960 break;
2961
2962 case CUDA_SPECIAL_DECL_REFS:
2963 // Later tables overwrite earlier ones.
2964 // FIXME: Modules will have trouble with this.
2965 CUDASpecialDeclRefs.clear();
2966 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2967 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
2968 break;
2969
2970 case HEADER_SEARCH_TABLE: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00002971 F.HeaderFileInfoTableData = Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002972 F.LocalNumHeaderFileInfos = Record[1];
Guy Benyei11169dd2012-12-18 14:30:41 +00002973 if (Record[0]) {
2974 F.HeaderFileInfoTable
2975 = HeaderFileInfoLookupTable::Create(
2976 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
2977 (const unsigned char *)F.HeaderFileInfoTableData,
2978 HeaderFileInfoTrait(*this, F,
2979 &PP.getHeaderSearchInfo(),
Chris Lattner0e6c9402013-01-20 02:38:54 +00002980 Blob.data() + Record[2]));
Guy Benyei11169dd2012-12-18 14:30:41 +00002981
2982 PP.getHeaderSearchInfo().SetExternalSource(this);
2983 if (!PP.getHeaderSearchInfo().getExternalLookup())
2984 PP.getHeaderSearchInfo().SetExternalLookup(this);
2985 }
2986 break;
2987 }
2988
2989 case FP_PRAGMA_OPTIONS:
2990 // Later tables overwrite earlier ones.
2991 FPPragmaOptions.swap(Record);
2992 break;
2993
2994 case OPENCL_EXTENSIONS:
2995 // Later tables overwrite earlier ones.
2996 OpenCLExtensions.swap(Record);
2997 break;
2998
2999 case TENTATIVE_DEFINITIONS:
3000 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3001 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
3002 break;
3003
3004 case KNOWN_NAMESPACES:
3005 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3006 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
3007 break;
Nick Lewycky8334af82013-01-26 00:35:08 +00003008
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00003009 case UNDEFINED_BUT_USED:
3010 if (UndefinedButUsed.size() % 2 != 0) {
3011 Error("Invalid existing UndefinedButUsed");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003012 return Failure;
Nick Lewycky8334af82013-01-26 00:35:08 +00003013 }
3014
3015 if (Record.size() % 2 != 0) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00003016 Error("invalid undefined-but-used record");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003017 return Failure;
Nick Lewycky8334af82013-01-26 00:35:08 +00003018 }
3019 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00003020 UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
3021 UndefinedButUsed.push_back(
Nick Lewycky8334af82013-01-26 00:35:08 +00003022 ReadSourceLocation(F, Record, I).getRawEncoding());
3023 }
3024 break;
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00003025 case DELETE_EXPRS_TO_ANALYZE:
3026 for (unsigned I = 0, N = Record.size(); I != N;) {
3027 DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++]));
3028 const uint64_t Count = Record[I++];
3029 DelayedDeleteExprs.push_back(Count);
3030 for (uint64_t C = 0; C < Count; ++C) {
3031 DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding());
3032 bool IsArrayForm = Record[I++] == 1;
3033 DelayedDeleteExprs.push_back(IsArrayForm);
3034 }
3035 }
3036 break;
Nick Lewycky8334af82013-01-26 00:35:08 +00003037
Guy Benyei11169dd2012-12-18 14:30:41 +00003038 case IMPORTED_MODULES: {
Richard Smithe842a472014-10-22 02:05:46 +00003039 if (F.Kind != MK_ImplicitModule && F.Kind != MK_ExplicitModule) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003040 // If we aren't loading a module (which has its own exports), make
3041 // all of the imported modules visible.
3042 // FIXME: Deal with macros-only imports.
Richard Smith56be7542014-03-21 00:33:59 +00003043 for (unsigned I = 0, N = Record.size(); I != N; /**/) {
3044 unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
3045 SourceLocation Loc = ReadSourceLocation(F, Record, I);
3046 if (GlobalID)
Aaron Ballman4f45b712014-03-21 15:22:56 +00003047 ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
Guy Benyei11169dd2012-12-18 14:30:41 +00003048 }
3049 }
3050 break;
3051 }
3052
3053 case LOCAL_REDECLARATIONS: {
3054 F.RedeclarationChains.swap(Record);
3055 break;
3056 }
3057
3058 case LOCAL_REDECLARATIONS_MAP: {
3059 if (F.LocalNumRedeclarationsInMap != 0) {
3060 Error("duplicate LOCAL_REDECLARATIONS_MAP record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003061 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003062 }
3063
3064 F.LocalNumRedeclarationsInMap = Record[0];
Chris Lattner0e6c9402013-01-20 02:38:54 +00003065 F.RedeclarationsMap = (const LocalRedeclarationsInfo *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003066 break;
3067 }
3068
Guy Benyei11169dd2012-12-18 14:30:41 +00003069 case MACRO_OFFSET: {
3070 if (F.LocalNumMacros != 0) {
3071 Error("duplicate MACRO_OFFSET record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00003072 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00003073 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00003074 F.MacroOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003075 F.LocalNumMacros = Record[0];
3076 unsigned LocalBaseMacroID = Record[1];
3077 F.BaseMacroID = getTotalNumMacros();
3078
3079 if (F.LocalNumMacros > 0) {
3080 // Introduce the global -> local mapping for macros within this module.
3081 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3082
3083 // Introduce the local -> global mapping for macros within this module.
3084 F.MacroRemap.insertOrReplace(
3085 std::make_pair(LocalBaseMacroID,
3086 F.BaseMacroID - LocalBaseMacroID));
Ben Langmuir52ca6782014-10-20 16:27:32 +00003087
3088 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
Guy Benyei11169dd2012-12-18 14:30:41 +00003089 }
3090 break;
3091 }
3092
Richard Smithe40f2ba2013-08-07 21:41:30 +00003093 case LATE_PARSED_TEMPLATE: {
3094 LateParsedTemplates.append(Record.begin(), Record.end());
3095 break;
3096 }
Dario Domizioli13a0a382014-05-23 12:13:25 +00003097
3098 case OPTIMIZE_PRAGMA_OPTIONS:
3099 if (Record.size() != 1) {
3100 Error("invalid pragma optimize record");
3101 return Failure;
3102 }
3103 OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
3104 break;
Nico Weber72889432014-09-06 01:25:55 +00003105
3106 case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
3107 for (unsigned I = 0, N = Record.size(); I != N; ++I)
3108 UnusedLocalTypedefNameCandidates.push_back(
3109 getGlobalDeclID(F, Record[I]));
3110 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003111 }
3112 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003113}
3114
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003115ASTReader::ASTReadResult
3116ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
3117 const ModuleFile *ImportedBy,
3118 unsigned ClientLoadCapabilities) {
3119 unsigned Idx = 0;
Richard Smith7ed1bc92014-12-05 22:42:13 +00003120 F.ModuleMapPath = ReadPath(F, Record, Idx);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003121
Richard Smithe842a472014-10-22 02:05:46 +00003122 if (F.Kind == MK_ExplicitModule) {
3123 // For an explicitly-loaded module, we don't care whether the original
3124 // module map file exists or matches.
3125 return Success;
3126 }
3127
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003128 // Try to resolve ModuleName in the current header search context and
3129 // verify that it is found in the same module map file as we saved. If the
3130 // top-level AST file is a main file, skip this check because there is no
3131 // usable header search context.
3132 assert(!F.ModuleName.empty() &&
Richard Smithe842a472014-10-22 02:05:46 +00003133 "MODULE_NAME should come before MODULE_MAP_FILE");
3134 if (F.Kind == MK_ImplicitModule &&
3135 (*ModuleMgr.begin())->Kind != MK_MainFile) {
3136 // An implicitly-loaded module file should have its module listed in some
3137 // module map file that we've already loaded.
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003138 Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
Richard Smithe842a472014-10-22 02:05:46 +00003139 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
3140 const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr;
3141 if (!ModMap) {
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003142 assert(ImportedBy && "top-level import should be verified");
3143 if ((ClientLoadCapabilities & ARR_Missing) == 0)
Richard Smithe842a472014-10-22 02:05:46 +00003144 Diag(diag::err_imported_module_not_found) << F.ModuleName << F.FileName
3145 << ImportedBy->FileName
3146 << F.ModuleMapPath;
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003147 return Missing;
3148 }
3149
Richard Smithe842a472014-10-22 02:05:46 +00003150 assert(M->Name == F.ModuleName && "found module with different name");
3151
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003152 // Check the primary module map file.
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003153 const FileEntry *StoredModMap = FileMgr.getFile(F.ModuleMapPath);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003154 if (StoredModMap == nullptr || StoredModMap != ModMap) {
3155 assert(ModMap && "found module is missing module map file");
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003156 assert(ImportedBy && "top-level import should be verified");
3157 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3158 Diag(diag::err_imported_module_modmap_changed)
3159 << F.ModuleName << ImportedBy->FileName
3160 << ModMap->getName() << F.ModuleMapPath;
3161 return OutOfDate;
3162 }
3163
3164 llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps;
3165 for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
3166 // FIXME: we should use input files rather than storing names.
Richard Smith7ed1bc92014-12-05 22:42:13 +00003167 std::string Filename = ReadPath(F, Record, Idx);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00003168 const FileEntry *F =
3169 FileMgr.getFile(Filename, false, false);
3170 if (F == nullptr) {
3171 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3172 Error("could not find file '" + Filename +"' referenced by AST file");
3173 return OutOfDate;
3174 }
3175 AdditionalStoredMaps.insert(F);
3176 }
3177
3178 // Check any additional module map files (e.g. module.private.modulemap)
3179 // that are not in the pcm.
3180 if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
3181 for (const FileEntry *ModMap : *AdditionalModuleMaps) {
3182 // Remove files that match
3183 // Note: SmallPtrSet::erase is really remove
3184 if (!AdditionalStoredMaps.erase(ModMap)) {
3185 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3186 Diag(diag::err_module_different_modmap)
3187 << F.ModuleName << /*new*/0 << ModMap->getName();
3188 return OutOfDate;
3189 }
3190 }
3191 }
3192
3193 // Check any additional module map files that are in the pcm, but not
3194 // found in header search. Cases that match are already removed.
3195 for (const FileEntry *ModMap : AdditionalStoredMaps) {
3196 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3197 Diag(diag::err_module_different_modmap)
3198 << F.ModuleName << /*not new*/1 << ModMap->getName();
3199 return OutOfDate;
3200 }
3201 }
3202
3203 if (Listener)
3204 Listener->ReadModuleMapFile(F.ModuleMapPath);
3205 return Success;
3206}
3207
3208
Douglas Gregorc1489562013-02-12 23:36:21 +00003209/// \brief Move the given method to the back of the global list of methods.
3210static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
3211 // Find the entry for this selector in the method pool.
3212 Sema::GlobalMethodPool::iterator Known
3213 = S.MethodPool.find(Method->getSelector());
3214 if (Known == S.MethodPool.end())
3215 return;
3216
3217 // Retrieve the appropriate method list.
3218 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
3219 : Known->second.second;
3220 bool Found = false;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003221 for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
Douglas Gregorc1489562013-02-12 23:36:21 +00003222 if (!Found) {
Nico Weber2e0c8f72014-12-27 03:58:08 +00003223 if (List->getMethod() == Method) {
Douglas Gregorc1489562013-02-12 23:36:21 +00003224 Found = true;
3225 } else {
3226 // Keep searching.
3227 continue;
3228 }
3229 }
3230
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003231 if (List->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003232 List->setMethod(List->getNext()->getMethod());
Douglas Gregorc1489562013-02-12 23:36:21 +00003233 else
Nico Weber2e0c8f72014-12-27 03:58:08 +00003234 List->setMethod(Method);
Douglas Gregorc1489562013-02-12 23:36:21 +00003235 }
3236}
3237
Richard Smithde711422015-04-23 21:20:19 +00003238void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
Richard Smith10434f32015-05-02 02:08:26 +00003239 assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
Richard Smith20e883e2015-04-29 23:20:19 +00003240 for (Decl *D : Names) {
Richard Smith49f906a2014-03-01 00:08:04 +00003241 bool wasHidden = D->Hidden;
3242 D->Hidden = false;
Guy Benyei11169dd2012-12-18 14:30:41 +00003243
Richard Smith49f906a2014-03-01 00:08:04 +00003244 if (wasHidden && SemaObj) {
3245 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
3246 moveMethodToBackOfGlobalList(*SemaObj, Method);
Douglas Gregorc1489562013-02-12 23:36:21 +00003247 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003248 }
3249 }
3250}
3251
Richard Smith49f906a2014-03-01 00:08:04 +00003252void ASTReader::makeModuleVisible(Module *Mod,
Argyrios Kyrtzidis125df052013-02-01 16:36:12 +00003253 Module::NameVisibilityKind NameVisibility,
Richard Smitha7e2cc62015-05-01 01:53:09 +00003254 SourceLocation ImportLoc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003255 llvm::SmallPtrSet<Module *, 4> Visited;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003256 SmallVector<Module *, 4> Stack;
Robert Wilhelm25284cc2013-08-23 16:11:15 +00003257 Stack.push_back(Mod);
Guy Benyei11169dd2012-12-18 14:30:41 +00003258 while (!Stack.empty()) {
Robert Wilhelm25284cc2013-08-23 16:11:15 +00003259 Mod = Stack.pop_back_val();
Guy Benyei11169dd2012-12-18 14:30:41 +00003260
3261 if (NameVisibility <= Mod->NameVisibility) {
Robert Wilhelm25284cc2013-08-23 16:11:15 +00003262 // This module already has this level of visibility (or greater), so
Guy Benyei11169dd2012-12-18 14:30:41 +00003263 // there is nothing more to do.
3264 continue;
3265 }
Richard Smith49f906a2014-03-01 00:08:04 +00003266
Guy Benyei11169dd2012-12-18 14:30:41 +00003267 if (!Mod->isAvailable()) {
3268 // Modules that aren't available cannot be made visible.
3269 continue;
3270 }
3271
3272 // Update the module's name visibility.
3273 Mod->NameVisibility = NameVisibility;
Richard Smith49f906a2014-03-01 00:08:04 +00003274
Guy Benyei11169dd2012-12-18 14:30:41 +00003275 // If we've already deserialized any names from this module,
3276 // mark them as visible.
3277 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
3278 if (Hidden != HiddenNamesMap.end()) {
Richard Smith57721ac2014-07-21 04:10:40 +00003279 auto HiddenNames = std::move(*Hidden);
Guy Benyei11169dd2012-12-18 14:30:41 +00003280 HiddenNamesMap.erase(Hidden);
Richard Smithde711422015-04-23 21:20:19 +00003281 makeNamesVisible(HiddenNames.second, HiddenNames.first);
Richard Smith57721ac2014-07-21 04:10:40 +00003282 assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() &&
3283 "making names visible added hidden names");
Guy Benyei11169dd2012-12-18 14:30:41 +00003284 }
Dmitri Gribenkoe9bcf5b2013-11-04 21:51:33 +00003285
Guy Benyei11169dd2012-12-18 14:30:41 +00003286 // Push any exported modules onto the stack to be marked as visible.
Argyrios Kyrtzidis8739f7b2013-02-19 19:34:40 +00003287 SmallVector<Module *, 16> Exports;
3288 Mod->getExportedModules(Exports);
3289 for (SmallVectorImpl<Module *>::iterator
3290 I = Exports.begin(), E = Exports.end(); I != E; ++I) {
3291 Module *Exported = *I;
David Blaikie82e95a32014-11-19 07:49:47 +00003292 if (Visited.insert(Exported).second)
Argyrios Kyrtzidis8739f7b2013-02-19 19:34:40 +00003293 Stack.push_back(Exported);
Guy Benyei11169dd2012-12-18 14:30:41 +00003294 }
3295 }
3296}
3297
Douglas Gregore060e572013-01-25 01:03:03 +00003298bool ASTReader::loadGlobalIndex() {
3299 if (GlobalIndex)
3300 return false;
3301
3302 if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
3303 !Context.getLangOpts().Modules)
3304 return true;
3305
3306 // Try to load the global index.
3307 TriedLoadingGlobalIndex = true;
3308 StringRef ModuleCachePath
3309 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
3310 std::pair<GlobalModuleIndex *, GlobalModuleIndex::ErrorCode> Result
Douglas Gregor7029ce12013-03-19 00:28:20 +00003311 = GlobalModuleIndex::readIndex(ModuleCachePath);
Douglas Gregore060e572013-01-25 01:03:03 +00003312 if (!Result.first)
3313 return true;
3314
3315 GlobalIndex.reset(Result.first);
Douglas Gregor7211ac12013-01-25 23:32:03 +00003316 ModuleMgr.setGlobalIndex(GlobalIndex.get());
Douglas Gregore060e572013-01-25 01:03:03 +00003317 return false;
3318}
3319
3320bool ASTReader::isGlobalIndexUnavailable() const {
3321 return Context.getLangOpts().Modules && UseGlobalIndex &&
3322 !hasGlobalIndex() && TriedLoadingGlobalIndex;
3323}
3324
Dmitri Gribenkof430da42014-02-12 10:33:14 +00003325static void updateModuleTimestamp(ModuleFile &MF) {
3326 // Overwrite the timestamp file contents so that file's mtime changes.
3327 std::string TimestampFilename = MF.getTimestampFilename();
Rafael Espindoladae941a2014-08-25 18:17:04 +00003328 std::error_code EC;
3329 llvm::raw_fd_ostream OS(TimestampFilename, EC, llvm::sys::fs::F_Text);
3330 if (EC)
Dmitri Gribenkof430da42014-02-12 10:33:14 +00003331 return;
3332 OS << "Timestamp file\n";
3333}
3334
Guy Benyei11169dd2012-12-18 14:30:41 +00003335ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
3336 ModuleKind Type,
3337 SourceLocation ImportLoc,
3338 unsigned ClientLoadCapabilities) {
Argyrios Kyrtzidisdc9fdaf2013-05-24 05:44:08 +00003339 llvm::SaveAndRestore<SourceLocation>
3340 SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
3341
Richard Smithd1c46742014-04-30 02:24:17 +00003342 // Defer any pending actions until we get to the end of reading the AST file.
3343 Deserializing AnASTFile(this);
3344
Guy Benyei11169dd2012-12-18 14:30:41 +00003345 // Bump the generation number.
Richard Smith053f6c62014-05-16 23:01:30 +00003346 unsigned PreviousGeneration = incrementGeneration(Context);
Guy Benyei11169dd2012-12-18 14:30:41 +00003347
3348 unsigned NumModules = ModuleMgr.size();
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003349 SmallVector<ImportedModule, 4> Loaded;
Guy Benyei11169dd2012-12-18 14:30:41 +00003350 switch(ASTReadResult ReadResult = ReadASTCore(FileName, Type, ImportLoc,
Craig Toppera13603a2014-05-22 05:54:18 +00003351 /*ImportedBy=*/nullptr, Loaded,
Ben Langmuir487ea142014-10-23 18:05:36 +00003352 0, 0, 0,
Guy Benyei11169dd2012-12-18 14:30:41 +00003353 ClientLoadCapabilities)) {
3354 case Failure:
Douglas Gregor7029ce12013-03-19 00:28:20 +00003355 case Missing:
Guy Benyei11169dd2012-12-18 14:30:41 +00003356 case OutOfDate:
3357 case VersionMismatch:
3358 case ConfigurationMismatch:
Ben Langmuir9801b252014-06-20 00:24:56 +00003359 case HadErrors: {
3360 llvm::SmallPtrSet<ModuleFile *, 4> LoadedSet;
3361 for (const ImportedModule &IM : Loaded)
3362 LoadedSet.insert(IM.Mod);
3363
Douglas Gregor7029ce12013-03-19 00:28:20 +00003364 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, ModuleMgr.end(),
Ben Langmuir9801b252014-06-20 00:24:56 +00003365 LoadedSet,
Douglas Gregor7029ce12013-03-19 00:28:20 +00003366 Context.getLangOpts().Modules
3367 ? &PP.getHeaderSearchInfo().getModuleMap()
Craig Toppera13603a2014-05-22 05:54:18 +00003368 : nullptr);
Douglas Gregore060e572013-01-25 01:03:03 +00003369
3370 // If we find that any modules are unusable, the global index is going
3371 // to be out-of-date. Just remove it.
3372 GlobalIndex.reset();
Craig Toppera13603a2014-05-22 05:54:18 +00003373 ModuleMgr.setGlobalIndex(nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00003374 return ReadResult;
Ben Langmuir9801b252014-06-20 00:24:56 +00003375 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003376 case Success:
3377 break;
3378 }
3379
3380 // Here comes stuff that we only do once the entire chain is loaded.
3381
3382 // Load the AST blocks of all of the modules that we loaded.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003383 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
3384 MEnd = Loaded.end();
Guy Benyei11169dd2012-12-18 14:30:41 +00003385 M != MEnd; ++M) {
3386 ModuleFile &F = *M->Mod;
3387
3388 // Read the AST block.
Ben Langmuir2c9af442014-04-10 17:57:43 +00003389 if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities))
3390 return Result;
Guy Benyei11169dd2012-12-18 14:30:41 +00003391
3392 // Once read, set the ModuleFile bit base offset and update the size in
3393 // bits of all files we've seen.
3394 F.GlobalBitOffset = TotalModulesSizeInBits;
3395 TotalModulesSizeInBits += F.SizeInBits;
3396 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
3397
3398 // Preload SLocEntries.
3399 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
3400 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
3401 // Load it through the SourceManager and don't call ReadSLocEntry()
3402 // directly because the entry may have already been loaded in which case
3403 // calling ReadSLocEntry() directly would trigger an assertion in
3404 // SourceManager.
3405 SourceMgr.getLoadedSLocEntryByID(Index);
3406 }
3407 }
3408
Douglas Gregor603cd862013-03-22 18:50:14 +00003409 // Setup the import locations and notify the module manager that we've
3410 // committed to these module files.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003411 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
3412 MEnd = Loaded.end();
Guy Benyei11169dd2012-12-18 14:30:41 +00003413 M != MEnd; ++M) {
3414 ModuleFile &F = *M->Mod;
Douglas Gregor603cd862013-03-22 18:50:14 +00003415
3416 ModuleMgr.moduleFileAccepted(&F);
3417
3418 // Set the import location.
Argyrios Kyrtzidis71c1af82013-02-01 16:36:14 +00003419 F.DirectImportLoc = ImportLoc;
Guy Benyei11169dd2012-12-18 14:30:41 +00003420 if (!M->ImportedBy)
3421 F.ImportLoc = M->ImportLoc;
3422 else
3423 F.ImportLoc = ReadSourceLocation(*M->ImportedBy,
3424 M->ImportLoc.getRawEncoding());
3425 }
3426
3427 // Mark all of the identifiers in the identifier table as being out of date,
3428 // so that various accessors know to check the loaded modules when the
3429 // identifier is used.
3430 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
3431 IdEnd = PP.getIdentifierTable().end();
3432 Id != IdEnd; ++Id)
3433 Id->second->setOutOfDate(true);
3434
3435 // Resolve any unresolved module exports.
Douglas Gregorfb912652013-03-20 21:10:35 +00003436 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
3437 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
Guy Benyei11169dd2012-12-18 14:30:41 +00003438 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
3439 Module *ResolvedMod = getSubmodule(GlobalID);
Douglas Gregorfb912652013-03-20 21:10:35 +00003440
3441 switch (Unresolved.Kind) {
3442 case UnresolvedModuleRef::Conflict:
3443 if (ResolvedMod) {
3444 Module::Conflict Conflict;
3445 Conflict.Other = ResolvedMod;
3446 Conflict.Message = Unresolved.String.str();
3447 Unresolved.Mod->Conflicts.push_back(Conflict);
3448 }
3449 continue;
3450
3451 case UnresolvedModuleRef::Import:
Guy Benyei11169dd2012-12-18 14:30:41 +00003452 if (ResolvedMod)
Richard Smith38477db2015-05-02 00:45:56 +00003453 Unresolved.Mod->Imports.insert(ResolvedMod);
Guy Benyei11169dd2012-12-18 14:30:41 +00003454 continue;
Guy Benyei11169dd2012-12-18 14:30:41 +00003455
Douglas Gregorfb912652013-03-20 21:10:35 +00003456 case UnresolvedModuleRef::Export:
3457 if (ResolvedMod || Unresolved.IsWildcard)
3458 Unresolved.Mod->Exports.push_back(
3459 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
3460 continue;
3461 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003462 }
Douglas Gregorfb912652013-03-20 21:10:35 +00003463 UnresolvedModuleRefs.clear();
Daniel Jasperba7f2f72013-09-24 09:14:14 +00003464
3465 // FIXME: How do we load the 'use'd modules? They may not be submodules.
3466 // Might be unnecessary as use declarations are only used to build the
3467 // module itself.
Guy Benyei11169dd2012-12-18 14:30:41 +00003468
3469 InitializeContext();
3470
Richard Smith3d8e97e2013-10-18 06:54:39 +00003471 if (SemaObj)
3472 UpdateSema();
3473
Guy Benyei11169dd2012-12-18 14:30:41 +00003474 if (DeserializationListener)
3475 DeserializationListener->ReaderInitialized(this);
3476
3477 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
3478 if (!PrimaryModule.OriginalSourceFileID.isInvalid()) {
3479 PrimaryModule.OriginalSourceFileID
3480 = FileID::get(PrimaryModule.SLocEntryBaseID
3481 + PrimaryModule.OriginalSourceFileID.getOpaqueValue() - 1);
3482
3483 // If this AST file is a precompiled preamble, then set the
3484 // preamble file ID of the source manager to the file source file
3485 // from which the preamble was built.
3486 if (Type == MK_Preamble) {
3487 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
3488 } else if (Type == MK_MainFile) {
3489 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
3490 }
3491 }
3492
3493 // For any Objective-C class definitions we have already loaded, make sure
3494 // that we load any additional categories.
3495 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
3496 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
3497 ObjCClassesLoaded[I],
3498 PreviousGeneration);
3499 }
Douglas Gregore060e572013-01-25 01:03:03 +00003500
Dmitri Gribenkof430da42014-02-12 10:33:14 +00003501 if (PP.getHeaderSearchInfo()
3502 .getHeaderSearchOpts()
3503 .ModulesValidateOncePerBuildSession) {
3504 // Now we are certain that the module and all modules it depends on are
3505 // up to date. Create or update timestamp files for modules that are
3506 // located in the module cache (not for PCH files that could be anywhere
3507 // in the filesystem).
3508 for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
3509 ImportedModule &M = Loaded[I];
Richard Smithe842a472014-10-22 02:05:46 +00003510 if (M.Mod->Kind == MK_ImplicitModule) {
Dmitri Gribenkof430da42014-02-12 10:33:14 +00003511 updateModuleTimestamp(*M.Mod);
3512 }
3513 }
3514 }
3515
Guy Benyei11169dd2012-12-18 14:30:41 +00003516 return Success;
3517}
3518
Ben Langmuir487ea142014-10-23 18:05:36 +00003519static ASTFileSignature readASTFileSignature(llvm::BitstreamReader &StreamFile);
3520
Ben Langmuir70a1b812015-03-24 04:43:52 +00003521/// \brief Whether \p Stream starts with the AST/PCH file magic number 'CPCH'.
3522static bool startsWithASTFileMagic(BitstreamCursor &Stream) {
3523 return Stream.Read(8) == 'C' &&
3524 Stream.Read(8) == 'P' &&
3525 Stream.Read(8) == 'C' &&
3526 Stream.Read(8) == 'H';
3527}
3528
Guy Benyei11169dd2012-12-18 14:30:41 +00003529ASTReader::ASTReadResult
3530ASTReader::ReadASTCore(StringRef FileName,
3531 ModuleKind Type,
3532 SourceLocation ImportLoc,
3533 ModuleFile *ImportedBy,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003534 SmallVectorImpl<ImportedModule> &Loaded,
Douglas Gregor7029ce12013-03-19 00:28:20 +00003535 off_t ExpectedSize, time_t ExpectedModTime,
Ben Langmuir487ea142014-10-23 18:05:36 +00003536 ASTFileSignature ExpectedSignature,
Guy Benyei11169dd2012-12-18 14:30:41 +00003537 unsigned ClientLoadCapabilities) {
3538 ModuleFile *M;
Guy Benyei11169dd2012-12-18 14:30:41 +00003539 std::string ErrorStr;
Douglas Gregor7029ce12013-03-19 00:28:20 +00003540 ModuleManager::AddModuleResult AddResult
3541 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
Richard Smith053f6c62014-05-16 23:01:30 +00003542 getGeneration(), ExpectedSize, ExpectedModTime,
Ben Langmuir487ea142014-10-23 18:05:36 +00003543 ExpectedSignature, readASTFileSignature,
Douglas Gregor7029ce12013-03-19 00:28:20 +00003544 M, ErrorStr);
Guy Benyei11169dd2012-12-18 14:30:41 +00003545
Douglas Gregor7029ce12013-03-19 00:28:20 +00003546 switch (AddResult) {
3547 case ModuleManager::AlreadyLoaded:
3548 return Success;
3549
3550 case ModuleManager::NewlyLoaded:
3551 // Load module file below.
3552 break;
3553
3554 case ModuleManager::Missing:
Richard Smithe842a472014-10-22 02:05:46 +00003555 // The module file was missing; if the client can handle that, return
Douglas Gregor7029ce12013-03-19 00:28:20 +00003556 // it.
3557 if (ClientLoadCapabilities & ARR_Missing)
3558 return Missing;
3559
3560 // Otherwise, return an error.
3561 {
3562 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
3563 + ErrorStr;
3564 Error(Msg);
3565 }
3566 return Failure;
3567
3568 case ModuleManager::OutOfDate:
3569 // We couldn't load the module file because it is out-of-date. If the
3570 // client can handle out-of-date, return it.
3571 if (ClientLoadCapabilities & ARR_OutOfDate)
3572 return OutOfDate;
3573
3574 // Otherwise, return an error.
3575 {
3576 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
3577 + ErrorStr;
3578 Error(Msg);
3579 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003580 return Failure;
3581 }
3582
Douglas Gregor7029ce12013-03-19 00:28:20 +00003583 assert(M && "Missing module file");
Guy Benyei11169dd2012-12-18 14:30:41 +00003584
3585 // FIXME: This seems rather a hack. Should CurrentDir be part of the
3586 // module?
3587 if (FileName != "-") {
3588 CurrentDir = llvm::sys::path::parent_path(FileName);
3589 if (CurrentDir.empty()) CurrentDir = ".";
3590 }
3591
3592 ModuleFile &F = *M;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003593 BitstreamCursor &Stream = F.Stream;
Adrian Prantlbb165fb2015-06-20 18:53:08 +00003594 PCHContainerOps.ExtractPCH(F.Buffer->getMemBufferRef(), F.StreamFile);
Rafael Espindolafd832392014-11-12 14:48:44 +00003595 Stream.init(&F.StreamFile);
Adrian Prantlcbc368c2015-02-25 02:44:04 +00003596 F.SizeInBits = F.Buffer->getBufferSize() * 8;
3597
Guy Benyei11169dd2012-12-18 14:30:41 +00003598 // Sniff for the signature.
Ben Langmuir70a1b812015-03-24 04:43:52 +00003599 if (!startsWithASTFileMagic(Stream)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003600 Diag(diag::err_not_a_pch_file) << FileName;
3601 return Failure;
3602 }
3603
3604 // This is used for compatibility with older PCH formats.
3605 bool HaveReadControlBlock = false;
3606
Chris Lattnerefa77172013-01-20 00:00:22 +00003607 while (1) {
3608 llvm::BitstreamEntry Entry = Stream.advance();
3609
3610 switch (Entry.Kind) {
3611 case llvm::BitstreamEntry::Error:
3612 case llvm::BitstreamEntry::EndBlock:
3613 case llvm::BitstreamEntry::Record:
Guy Benyei11169dd2012-12-18 14:30:41 +00003614 Error("invalid record at top-level of AST file");
3615 return Failure;
Chris Lattnerefa77172013-01-20 00:00:22 +00003616
3617 case llvm::BitstreamEntry::SubBlock:
3618 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003619 }
3620
Guy Benyei11169dd2012-12-18 14:30:41 +00003621 // We only know the control subblock ID.
Chris Lattnerefa77172013-01-20 00:00:22 +00003622 switch (Entry.ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003623 case llvm::bitc::BLOCKINFO_BLOCK_ID:
3624 if (Stream.ReadBlockInfoBlock()) {
3625 Error("malformed BlockInfoBlock in AST file");
3626 return Failure;
3627 }
3628 break;
3629 case CONTROL_BLOCK_ID:
3630 HaveReadControlBlock = true;
Ben Langmuirbeee15e2014-04-14 18:00:01 +00003631 switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003632 case Success:
3633 break;
3634
3635 case Failure: return Failure;
Douglas Gregor7029ce12013-03-19 00:28:20 +00003636 case Missing: return Missing;
Guy Benyei11169dd2012-12-18 14:30:41 +00003637 case OutOfDate: return OutOfDate;
3638 case VersionMismatch: return VersionMismatch;
3639 case ConfigurationMismatch: return ConfigurationMismatch;
3640 case HadErrors: return HadErrors;
3641 }
3642 break;
3643 case AST_BLOCK_ID:
3644 if (!HaveReadControlBlock) {
3645 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
Dmitri Gribenko2228cd32014-02-11 15:40:09 +00003646 Diag(diag::err_pch_version_too_old);
Guy Benyei11169dd2012-12-18 14:30:41 +00003647 return VersionMismatch;
3648 }
3649
3650 // Record that we've loaded this module.
3651 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
3652 return Success;
3653
3654 default:
3655 if (Stream.SkipBlock()) {
3656 Error("malformed block record in AST file");
3657 return Failure;
3658 }
3659 break;
3660 }
3661 }
3662
3663 return Success;
3664}
3665
Richard Smitha7e2cc62015-05-01 01:53:09 +00003666void ASTReader::InitializeContext() {
Guy Benyei11169dd2012-12-18 14:30:41 +00003667 // If there's a listener, notify them that we "read" the translation unit.
3668 if (DeserializationListener)
3669 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
3670 Context.getTranslationUnitDecl());
3671
Guy Benyei11169dd2012-12-18 14:30:41 +00003672 // FIXME: Find a better way to deal with collisions between these
3673 // built-in types. Right now, we just ignore the problem.
3674
3675 // Load the special types.
3676 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
3677 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
3678 if (!Context.CFConstantStringTypeDecl)
3679 Context.setCFConstantStringType(GetType(String));
3680 }
3681
3682 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
3683 QualType FileType = GetType(File);
3684 if (FileType.isNull()) {
3685 Error("FILE type is NULL");
3686 return;
3687 }
3688
3689 if (!Context.FILEDecl) {
3690 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
3691 Context.setFILEDecl(Typedef->getDecl());
3692 else {
3693 const TagType *Tag = FileType->getAs<TagType>();
3694 if (!Tag) {
3695 Error("Invalid FILE type in AST file");
3696 return;
3697 }
3698 Context.setFILEDecl(Tag->getDecl());
3699 }
3700 }
3701 }
3702
3703 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
3704 QualType Jmp_bufType = GetType(Jmp_buf);
3705 if (Jmp_bufType.isNull()) {
3706 Error("jmp_buf type is NULL");
3707 return;
3708 }
3709
3710 if (!Context.jmp_bufDecl) {
3711 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
3712 Context.setjmp_bufDecl(Typedef->getDecl());
3713 else {
3714 const TagType *Tag = Jmp_bufType->getAs<TagType>();
3715 if (!Tag) {
3716 Error("Invalid jmp_buf type in AST file");
3717 return;
3718 }
3719 Context.setjmp_bufDecl(Tag->getDecl());
3720 }
3721 }
3722 }
3723
3724 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
3725 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
3726 if (Sigjmp_bufType.isNull()) {
3727 Error("sigjmp_buf type is NULL");
3728 return;
3729 }
3730
3731 if (!Context.sigjmp_bufDecl) {
3732 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
3733 Context.setsigjmp_bufDecl(Typedef->getDecl());
3734 else {
3735 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
3736 assert(Tag && "Invalid sigjmp_buf type in AST file");
3737 Context.setsigjmp_bufDecl(Tag->getDecl());
3738 }
3739 }
3740 }
3741
3742 if (unsigned ObjCIdRedef
3743 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
3744 if (Context.ObjCIdRedefinitionType.isNull())
3745 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
3746 }
3747
3748 if (unsigned ObjCClassRedef
3749 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
3750 if (Context.ObjCClassRedefinitionType.isNull())
3751 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
3752 }
3753
3754 if (unsigned ObjCSelRedef
3755 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
3756 if (Context.ObjCSelRedefinitionType.isNull())
3757 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
3758 }
3759
3760 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
3761 QualType Ucontext_tType = GetType(Ucontext_t);
3762 if (Ucontext_tType.isNull()) {
3763 Error("ucontext_t type is NULL");
3764 return;
3765 }
3766
3767 if (!Context.ucontext_tDecl) {
3768 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
3769 Context.setucontext_tDecl(Typedef->getDecl());
3770 else {
3771 const TagType *Tag = Ucontext_tType->getAs<TagType>();
3772 assert(Tag && "Invalid ucontext_t type in AST file");
3773 Context.setucontext_tDecl(Tag->getDecl());
3774 }
3775 }
3776 }
3777 }
3778
3779 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
3780
3781 // If there were any CUDA special declarations, deserialize them.
3782 if (!CUDASpecialDeclRefs.empty()) {
3783 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
3784 Context.setcudaConfigureCallDecl(
3785 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
3786 }
Richard Smith56be7542014-03-21 00:33:59 +00003787
Guy Benyei11169dd2012-12-18 14:30:41 +00003788 // Re-export any modules that were imported by a non-module AST file.
Richard Smitha7e2cc62015-05-01 01:53:09 +00003789 // FIXME: This does not make macro-only imports visible again.
Richard Smith56be7542014-03-21 00:33:59 +00003790 for (auto &Import : ImportedModules) {
Richard Smitha7e2cc62015-05-01 01:53:09 +00003791 if (Module *Imported = getSubmodule(Import.ID)) {
Argyrios Kyrtzidis125df052013-02-01 16:36:12 +00003792 makeModuleVisible(Imported, Module::AllVisible,
Richard Smitha7e2cc62015-05-01 01:53:09 +00003793 /*ImportLoc=*/Import.ImportLoc);
3794 PP.makeModuleVisible(Imported, Import.ImportLoc);
3795 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003796 }
3797 ImportedModules.clear();
3798}
3799
3800void ASTReader::finalizeForWriting() {
Richard Smithde711422015-04-23 21:20:19 +00003801 // Nothing to do for now.
Guy Benyei11169dd2012-12-18 14:30:41 +00003802}
3803
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00003804/// \brief Given a cursor at the start of an AST file, scan ahead and drop the
3805/// cursor into the start of the given block ID, returning false on success and
3806/// true on failure.
3807static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003808 while (1) {
3809 llvm::BitstreamEntry Entry = Cursor.advance();
3810 switch (Entry.Kind) {
3811 case llvm::BitstreamEntry::Error:
3812 case llvm::BitstreamEntry::EndBlock:
3813 return true;
3814
3815 case llvm::BitstreamEntry::Record:
3816 // Ignore top-level records.
3817 Cursor.skipRecord(Entry.ID);
3818 break;
3819
3820 case llvm::BitstreamEntry::SubBlock:
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00003821 if (Entry.ID == BlockID) {
3822 if (Cursor.EnterSubBlock(BlockID))
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003823 return true;
3824 // Found it!
3825 return false;
3826 }
3827
3828 if (Cursor.SkipBlock())
3829 return true;
3830 }
3831 }
3832}
3833
Ben Langmuir70a1b812015-03-24 04:43:52 +00003834/// \brief Reads and return the signature record from \p StreamFile's control
3835/// block, or else returns 0.
Ben Langmuir487ea142014-10-23 18:05:36 +00003836static ASTFileSignature readASTFileSignature(llvm::BitstreamReader &StreamFile){
3837 BitstreamCursor Stream(StreamFile);
Ben Langmuir70a1b812015-03-24 04:43:52 +00003838 if (!startsWithASTFileMagic(Stream))
Ben Langmuir487ea142014-10-23 18:05:36 +00003839 return 0;
Ben Langmuir487ea142014-10-23 18:05:36 +00003840
3841 // Scan for the CONTROL_BLOCK_ID block.
3842 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
3843 return 0;
3844
3845 // Scan for SIGNATURE inside the control block.
3846 ASTReader::RecordData Record;
3847 while (1) {
3848 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
3849 if (Entry.Kind == llvm::BitstreamEntry::EndBlock ||
3850 Entry.Kind != llvm::BitstreamEntry::Record)
3851 return 0;
3852
3853 Record.clear();
3854 StringRef Blob;
3855 if (SIGNATURE == Stream.readRecord(Entry.ID, Record, &Blob))
3856 return Record[0];
3857 }
3858}
3859
Guy Benyei11169dd2012-12-18 14:30:41 +00003860/// \brief Retrieve the name of the original source file name
3861/// directly from the AST file, without actually loading the AST
3862/// file.
Adrian Prantlbb165fb2015-06-20 18:53:08 +00003863std::string ASTReader::getOriginalSourceFile(
3864 const std::string &ASTFileName, FileManager &FileMgr,
3865 const PCHContainerOperations &PCHContainerOps, DiagnosticsEngine &Diags) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003866 // Open the AST file.
Benjamin Kramera8857962014-10-26 22:44:13 +00003867 auto Buffer = FileMgr.getBufferForFile(ASTFileName);
Guy Benyei11169dd2012-12-18 14:30:41 +00003868 if (!Buffer) {
Benjamin Kramera8857962014-10-26 22:44:13 +00003869 Diags.Report(diag::err_fe_unable_to_read_pch_file)
3870 << ASTFileName << Buffer.getError().message();
Guy Benyei11169dd2012-12-18 14:30:41 +00003871 return std::string();
3872 }
3873
3874 // Initialize the stream
3875 llvm::BitstreamReader StreamFile;
Adrian Prantlbb165fb2015-06-20 18:53:08 +00003876 PCHContainerOps.ExtractPCH((*Buffer)->getMemBufferRef(), StreamFile);
Rafael Espindolaaf0e40a2014-11-12 14:42:25 +00003877 BitstreamCursor Stream(StreamFile);
Guy Benyei11169dd2012-12-18 14:30:41 +00003878
3879 // Sniff for the signature.
Ben Langmuir70a1b812015-03-24 04:43:52 +00003880 if (!startsWithASTFileMagic(Stream)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003881 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
3882 return std::string();
3883 }
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003884
Chris Lattnere7b154b2013-01-19 21:39:22 +00003885 // Scan for the CONTROL_BLOCK_ID block.
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00003886 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003887 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
3888 return std::string();
Chris Lattnere7b154b2013-01-19 21:39:22 +00003889 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003890
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003891 // Scan for ORIGINAL_FILE inside the control block.
3892 RecordData Record;
Chris Lattnere7b154b2013-01-19 21:39:22 +00003893 while (1) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003894 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Chris Lattnere7b154b2013-01-19 21:39:22 +00003895 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
3896 return std::string();
3897
3898 if (Entry.Kind != llvm::BitstreamEntry::Record) {
3899 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
3900 return std::string();
Guy Benyei11169dd2012-12-18 14:30:41 +00003901 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00003902
Guy Benyei11169dd2012-12-18 14:30:41 +00003903 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00003904 StringRef Blob;
3905 if (Stream.readRecord(Entry.ID, Record, &Blob) == ORIGINAL_FILE)
3906 return Blob.str();
Guy Benyei11169dd2012-12-18 14:30:41 +00003907 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003908}
3909
3910namespace {
3911 class SimplePCHValidator : public ASTReaderListener {
3912 const LangOptions &ExistingLangOpts;
3913 const TargetOptions &ExistingTargetOpts;
3914 const PreprocessorOptions &ExistingPPOpts;
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00003915 std::string ExistingModuleCachePath;
Guy Benyei11169dd2012-12-18 14:30:41 +00003916 FileManager &FileMgr;
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00003917
Guy Benyei11169dd2012-12-18 14:30:41 +00003918 public:
3919 SimplePCHValidator(const LangOptions &ExistingLangOpts,
3920 const TargetOptions &ExistingTargetOpts,
3921 const PreprocessorOptions &ExistingPPOpts,
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00003922 StringRef ExistingModuleCachePath,
Guy Benyei11169dd2012-12-18 14:30:41 +00003923 FileManager &FileMgr)
3924 : ExistingLangOpts(ExistingLangOpts),
3925 ExistingTargetOpts(ExistingTargetOpts),
3926 ExistingPPOpts(ExistingPPOpts),
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00003927 ExistingModuleCachePath(ExistingModuleCachePath),
Guy Benyei11169dd2012-12-18 14:30:41 +00003928 FileMgr(FileMgr)
3929 {
3930 }
3931
Richard Smith1e2cf0d2014-10-31 02:28:58 +00003932 bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
3933 bool AllowCompatibleDifferences) override {
3934 return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr,
3935 AllowCompatibleDifferences);
Guy Benyei11169dd2012-12-18 14:30:41 +00003936 }
Chandler Carruth0d745bc2015-03-14 04:47:43 +00003937 bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
3938 bool AllowCompatibleDifferences) override {
3939 return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr,
3940 AllowCompatibleDifferences);
Guy Benyei11169dd2012-12-18 14:30:41 +00003941 }
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00003942 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
3943 StringRef SpecificModuleCachePath,
3944 bool Complain) override {
3945 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
3946 ExistingModuleCachePath,
3947 nullptr, ExistingLangOpts);
3948 }
Craig Topper3e89dfe2014-03-13 02:13:41 +00003949 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
3950 bool Complain,
3951 std::string &SuggestedPredefines) override {
Craig Toppera13603a2014-05-22 05:54:18 +00003952 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr,
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00003953 SuggestedPredefines, ExistingLangOpts);
Guy Benyei11169dd2012-12-18 14:30:41 +00003954 }
3955 };
3956}
3957
Adrian Prantlbb165fb2015-06-20 18:53:08 +00003958bool ASTReader::readASTFileControlBlock(
3959 StringRef Filename, FileManager &FileMgr,
3960 const PCHContainerOperations &PCHContainerOps,
3961 ASTReaderListener &Listener) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003962 // Open the AST file.
Richard Smith7f330cd2015-03-18 01:42:29 +00003963 // FIXME: This allows use of the VFS; we do not allow use of the
3964 // VFS when actually loading a module.
Benjamin Kramera8857962014-10-26 22:44:13 +00003965 auto Buffer = FileMgr.getBufferForFile(Filename);
Guy Benyei11169dd2012-12-18 14:30:41 +00003966 if (!Buffer) {
3967 return true;
3968 }
3969
3970 // Initialize the stream
3971 llvm::BitstreamReader StreamFile;
Adrian Prantlcbc368c2015-02-25 02:44:04 +00003972 StreamFile.init((const unsigned char *)(*Buffer)->getBufferStart(),
3973 (const unsigned char *)(*Buffer)->getBufferEnd());
Rafael Espindolaaf0e40a2014-11-12 14:42:25 +00003974 BitstreamCursor Stream(StreamFile);
Guy Benyei11169dd2012-12-18 14:30:41 +00003975
3976 // Sniff for the signature.
Ben Langmuir70a1b812015-03-24 04:43:52 +00003977 if (!startsWithASTFileMagic(Stream))
Guy Benyei11169dd2012-12-18 14:30:41 +00003978 return true;
Guy Benyei11169dd2012-12-18 14:30:41 +00003979
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003980 // Scan for the CONTROL_BLOCK_ID block.
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00003981 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003982 return true;
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00003983
3984 bool NeedsInputFiles = Listener.needsInputFileVisitation();
Ben Langmuircb69b572014-03-07 06:40:32 +00003985 bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
Richard Smithd4b230b2014-10-27 23:01:16 +00003986 bool NeedsImports = Listener.needsImportVisitation();
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00003987 BitstreamCursor InputFilesCursor;
3988 if (NeedsInputFiles) {
3989 InputFilesCursor = Stream;
3990 if (SkipCursorToBlock(InputFilesCursor, INPUT_FILES_BLOCK_ID))
3991 return true;
3992
3993 // Read the abbreviations
3994 while (true) {
3995 uint64_t Offset = InputFilesCursor.GetCurrentBitNo();
3996 unsigned Code = InputFilesCursor.ReadCode();
3997
3998 // We expect all abbrevs to be at the start of the block.
3999 if (Code != llvm::bitc::DEFINE_ABBREV) {
4000 InputFilesCursor.JumpToBit(Offset);
4001 break;
4002 }
4003 InputFilesCursor.ReadAbbrevRecord();
4004 }
4005 }
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004006
4007 // Scan for ORIGINAL_FILE inside the control block.
Guy Benyei11169dd2012-12-18 14:30:41 +00004008 RecordData Record;
Richard Smith7ed1bc92014-12-05 22:42:13 +00004009 std::string ModuleDir;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004010 while (1) {
4011 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
4012 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
4013 return false;
4014
4015 if (Entry.Kind != llvm::BitstreamEntry::Record)
4016 return true;
4017
Guy Benyei11169dd2012-12-18 14:30:41 +00004018 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00004019 StringRef Blob;
4020 unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob);
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004021 switch ((ControlRecordTypes)RecCode) {
4022 case METADATA: {
4023 if (Record[0] != VERSION_MAJOR)
4024 return true;
Guy Benyei11169dd2012-12-18 14:30:41 +00004025
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +00004026 if (Listener.ReadFullVersionInformation(Blob))
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004027 return true;
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +00004028
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004029 break;
4030 }
Ben Langmuir4f5212a2014-04-14 22:12:44 +00004031 case MODULE_NAME:
4032 Listener.ReadModuleName(Blob);
4033 break;
Richard Smith7ed1bc92014-12-05 22:42:13 +00004034 case MODULE_DIRECTORY:
4035 ModuleDir = Blob;
4036 break;
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00004037 case MODULE_MAP_FILE: {
4038 unsigned Idx = 0;
Richard Smith7ed1bc92014-12-05 22:42:13 +00004039 auto Path = ReadString(Record, Idx);
4040 ResolveImportedPath(Path, ModuleDir);
4041 Listener.ReadModuleMapFile(Path);
Ben Langmuir4f5212a2014-04-14 22:12:44 +00004042 break;
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00004043 }
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004044 case LANGUAGE_OPTIONS:
Richard Smith1e2cf0d2014-10-31 02:28:58 +00004045 if (ParseLanguageOptions(Record, false, Listener,
4046 /*AllowCompatibleConfigurationMismatch*/false))
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004047 return true;
4048 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004049
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004050 case TARGET_OPTIONS:
Chandler Carruth0d745bc2015-03-14 04:47:43 +00004051 if (ParseTargetOptions(Record, false, Listener,
4052 /*AllowCompatibleConfigurationMismatch*/ false))
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004053 return true;
4054 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004055
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004056 case DIAGNOSTIC_OPTIONS:
4057 if (ParseDiagnosticOptions(Record, false, Listener))
4058 return true;
4059 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004060
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004061 case FILE_SYSTEM_OPTIONS:
4062 if (ParseFileSystemOptions(Record, false, Listener))
4063 return true;
4064 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004065
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004066 case HEADER_SEARCH_OPTIONS:
4067 if (ParseHeaderSearchOptions(Record, false, Listener))
4068 return true;
4069 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004070
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004071 case PREPROCESSOR_OPTIONS: {
4072 std::string IgnoredSuggestedPredefines;
4073 if (ParsePreprocessorOptions(Record, false, Listener,
4074 IgnoredSuggestedPredefines))
4075 return true;
4076 break;
4077 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004078
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004079 case INPUT_FILE_OFFSETS: {
4080 if (!NeedsInputFiles)
4081 break;
4082
4083 unsigned NumInputFiles = Record[0];
4084 unsigned NumUserFiles = Record[1];
Richard Smithec216502015-02-13 19:48:37 +00004085 const uint64_t *InputFileOffs = (const uint64_t *)Blob.data();
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004086 for (unsigned I = 0; I != NumInputFiles; ++I) {
4087 // Go find this input file.
4088 bool isSystemFile = I >= NumUserFiles;
Ben Langmuircb69b572014-03-07 06:40:32 +00004089
4090 if (isSystemFile && !NeedsSystemInputFiles)
4091 break; // the rest are system input files
4092
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004093 BitstreamCursor &Cursor = InputFilesCursor;
4094 SavedStreamPosition SavedPosition(Cursor);
4095 Cursor.JumpToBit(InputFileOffs[I]);
4096
4097 unsigned Code = Cursor.ReadCode();
4098 RecordData Record;
4099 StringRef Blob;
4100 bool shouldContinue = false;
4101 switch ((InputFileRecordTypes)Cursor.readRecord(Code, Record, &Blob)) {
4102 case INPUT_FILE:
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +00004103 bool Overridden = static_cast<bool>(Record[3]);
Richard Smith7ed1bc92014-12-05 22:42:13 +00004104 std::string Filename = Blob;
4105 ResolveImportedPath(Filename, ModuleDir);
4106 shouldContinue =
4107 Listener.visitInputFile(Filename, isSystemFile, Overridden);
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00004108 break;
4109 }
4110 if (!shouldContinue)
4111 break;
4112 }
4113 break;
4114 }
4115
Richard Smithd4b230b2014-10-27 23:01:16 +00004116 case IMPORTS: {
4117 if (!NeedsImports)
4118 break;
4119
4120 unsigned Idx = 0, N = Record.size();
4121 while (Idx < N) {
4122 // Read information about the AST file.
Richard Smith79c98cc2014-10-27 23:25:15 +00004123 Idx += 5; // ImportLoc, Size, ModTime, Signature
Richard Smith7ed1bc92014-12-05 22:42:13 +00004124 std::string Filename = ReadString(Record, Idx);
4125 ResolveImportedPath(Filename, ModuleDir);
4126 Listener.visitImport(Filename);
Richard Smithd4b230b2014-10-27 23:01:16 +00004127 }
4128 break;
4129 }
4130
Richard Smith7f330cd2015-03-18 01:42:29 +00004131 case KNOWN_MODULE_FILES: {
4132 // Known-but-not-technically-used module files are treated as imports.
4133 if (!NeedsImports)
4134 break;
4135
4136 unsigned Idx = 0, N = Record.size();
4137 while (Idx < N) {
4138 std::string Filename = ReadString(Record, Idx);
4139 ResolveImportedPath(Filename, ModuleDir);
4140 Listener.visitImport(Filename);
4141 }
4142 break;
4143 }
4144
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004145 default:
4146 // No other validation to perform.
4147 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004148 }
4149 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004150}
4151
Adrian Prantlbb165fb2015-06-20 18:53:08 +00004152bool ASTReader::isAcceptableASTFile(
4153 StringRef Filename, FileManager &FileMgr,
4154 const PCHContainerOperations &PCHContainerOps, const LangOptions &LangOpts,
4155 const TargetOptions &TargetOpts, const PreprocessorOptions &PPOpts,
4156 std::string ExistingModuleCachePath) {
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00004157 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts,
4158 ExistingModuleCachePath, FileMgr);
Adrian Prantlbb165fb2015-06-20 18:53:08 +00004159 return !readASTFileControlBlock(Filename, FileMgr, PCHContainerOps,
4160 validator);
Guy Benyei11169dd2012-12-18 14:30:41 +00004161}
4162
Ben Langmuir2c9af442014-04-10 17:57:43 +00004163ASTReader::ASTReadResult
4164ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004165 // Enter the submodule block.
4166 if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
4167 Error("malformed submodule block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00004168 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00004169 }
4170
4171 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
4172 bool First = true;
Craig Toppera13603a2014-05-22 05:54:18 +00004173 Module *CurrentModule = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00004174 RecordData Record;
4175 while (true) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004176 llvm::BitstreamEntry Entry = F.Stream.advanceSkippingSubblocks();
4177
4178 switch (Entry.Kind) {
4179 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
4180 case llvm::BitstreamEntry::Error:
4181 Error("malformed block record in AST file");
Ben Langmuir2c9af442014-04-10 17:57:43 +00004182 return Failure;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004183 case llvm::BitstreamEntry::EndBlock:
Ben Langmuir2c9af442014-04-10 17:57:43 +00004184 return Success;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004185 case llvm::BitstreamEntry::Record:
4186 // The interesting case.
4187 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004188 }
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004189
Guy Benyei11169dd2012-12-18 14:30:41 +00004190 // Read a record.
Chris Lattner0e6c9402013-01-20 02:38:54 +00004191 StringRef Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00004192 Record.clear();
Richard Smith03478d92014-10-23 22:12:14 +00004193 auto Kind = F.Stream.readRecord(Entry.ID, Record, &Blob);
4194
4195 if ((Kind == SUBMODULE_METADATA) != First) {
4196 Error("submodule metadata record should be at beginning of block");
4197 return Failure;
4198 }
4199 First = false;
4200
4201 // Submodule information is only valid if we have a current module.
4202 // FIXME: Should we error on these cases?
4203 if (!CurrentModule && Kind != SUBMODULE_METADATA &&
4204 Kind != SUBMODULE_DEFINITION)
4205 continue;
4206
4207 switch (Kind) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004208 default: // Default behavior: ignore.
4209 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004210
Richard Smith03478d92014-10-23 22:12:14 +00004211 case SUBMODULE_DEFINITION: {
Douglas Gregor8d932422013-03-20 03:59:18 +00004212 if (Record.size() < 8) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004213 Error("malformed module definition");
Ben Langmuir2c9af442014-04-10 17:57:43 +00004214 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00004215 }
Richard Smith03478d92014-10-23 22:12:14 +00004216
Chris Lattner0e6c9402013-01-20 02:38:54 +00004217 StringRef Name = Blob;
Richard Smith9bca2982014-03-08 00:03:56 +00004218 unsigned Idx = 0;
4219 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
4220 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
4221 bool IsFramework = Record[Idx++];
4222 bool IsExplicit = Record[Idx++];
4223 bool IsSystem = Record[Idx++];
4224 bool IsExternC = Record[Idx++];
4225 bool InferSubmodules = Record[Idx++];
4226 bool InferExplicitSubmodules = Record[Idx++];
4227 bool InferExportWildcard = Record[Idx++];
4228 bool ConfigMacrosExhaustive = Record[Idx++];
Douglas Gregor8d932422013-03-20 03:59:18 +00004229
Ben Langmuirbeee15e2014-04-14 18:00:01 +00004230 Module *ParentModule = nullptr;
Ben Langmuir9d6448b2014-08-09 00:57:23 +00004231 if (Parent)
Guy Benyei11169dd2012-12-18 14:30:41 +00004232 ParentModule = getSubmodule(Parent);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00004233
Guy Benyei11169dd2012-12-18 14:30:41 +00004234 // Retrieve this (sub)module from the module map, creating it if
4235 // necessary.
Ben Langmuir9d6448b2014-08-09 00:57:23 +00004236 CurrentModule = ModMap.findOrCreateModule(Name, ParentModule, IsFramework,
Guy Benyei11169dd2012-12-18 14:30:41 +00004237 IsExplicit).first;
Ben Langmuir9d6448b2014-08-09 00:57:23 +00004238
4239 // FIXME: set the definition loc for CurrentModule, or call
4240 // ModMap.setInferredModuleAllowedBy()
4241
Guy Benyei11169dd2012-12-18 14:30:41 +00004242 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
4243 if (GlobalIndex >= SubmodulesLoaded.size() ||
4244 SubmodulesLoaded[GlobalIndex]) {
4245 Error("too many submodules");
Ben Langmuir2c9af442014-04-10 17:57:43 +00004246 return Failure;
Guy Benyei11169dd2012-12-18 14:30:41 +00004247 }
Douglas Gregor8a114ab2013-02-06 22:40:31 +00004248
Douglas Gregor7029ce12013-03-19 00:28:20 +00004249 if (!ParentModule) {
4250 if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
4251 if (CurFile != F.File) {
4252 if (!Diags.isDiagnosticInFlight()) {
4253 Diag(diag::err_module_file_conflict)
4254 << CurrentModule->getTopLevelModuleName()
4255 << CurFile->getName()
4256 << F.File->getName();
4257 }
Ben Langmuir2c9af442014-04-10 17:57:43 +00004258 return Failure;
Douglas Gregor8a114ab2013-02-06 22:40:31 +00004259 }
Douglas Gregor8a114ab2013-02-06 22:40:31 +00004260 }
Douglas Gregor7029ce12013-03-19 00:28:20 +00004261
4262 CurrentModule->setASTFile(F.File);
Douglas Gregor8a114ab2013-02-06 22:40:31 +00004263 }
Ben Langmuirbeee15e2014-04-14 18:00:01 +00004264
Guy Benyei11169dd2012-12-18 14:30:41 +00004265 CurrentModule->IsFromModuleFile = true;
4266 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
Richard Smith9bca2982014-03-08 00:03:56 +00004267 CurrentModule->IsExternC = IsExternC;
Guy Benyei11169dd2012-12-18 14:30:41 +00004268 CurrentModule->InferSubmodules = InferSubmodules;
4269 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
4270 CurrentModule->InferExportWildcard = InferExportWildcard;
Douglas Gregor8d932422013-03-20 03:59:18 +00004271 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
Guy Benyei11169dd2012-12-18 14:30:41 +00004272 if (DeserializationListener)
4273 DeserializationListener->ModuleRead(GlobalID, CurrentModule);
4274
4275 SubmodulesLoaded[GlobalIndex] = CurrentModule;
Douglas Gregor6ddfca92013-01-14 17:21:00 +00004276
Douglas Gregorfb912652013-03-20 21:10:35 +00004277 // Clear out data that will be replaced by what is the module file.
Douglas Gregor6ddfca92013-01-14 17:21:00 +00004278 CurrentModule->LinkLibraries.clear();
Douglas Gregor8d932422013-03-20 03:59:18 +00004279 CurrentModule->ConfigMacros.clear();
Douglas Gregorfb912652013-03-20 21:10:35 +00004280 CurrentModule->UnresolvedConflicts.clear();
4281 CurrentModule->Conflicts.clear();
Guy Benyei11169dd2012-12-18 14:30:41 +00004282 break;
4283 }
4284
4285 case SUBMODULE_UMBRELLA_HEADER: {
Richard Smith2b63d152015-05-16 02:28:53 +00004286 std::string Filename = Blob;
4287 ResolveImportedPath(F, Filename);
4288 if (auto *Umbrella = PP.getFileManager().getFile(Filename)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004289 if (!CurrentModule->getUmbrellaHeader())
Richard Smith2b63d152015-05-16 02:28:53 +00004290 ModMap.setUmbrellaHeader(CurrentModule, Umbrella, Blob);
4291 else if (CurrentModule->getUmbrellaHeader().Entry != Umbrella) {
Ben Langmuirbc35fbe2015-02-20 21:46:39 +00004292 // This can be a spurious difference caused by changing the VFS to
4293 // point to a different copy of the file, and it is too late to
4294 // to rebuild safely.
4295 // FIXME: If we wrote the virtual paths instead of the 'real' paths,
4296 // after input file validation only real problems would remain and we
4297 // could just error. For now, assume it's okay.
4298 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004299 }
4300 }
4301 break;
4302 }
4303
Richard Smith202210b2014-10-24 20:23:01 +00004304 case SUBMODULE_HEADER:
4305 case SUBMODULE_EXCLUDED_HEADER:
4306 case SUBMODULE_PRIVATE_HEADER:
4307 // We lazily associate headers with their modules via the HeaderInfo table.
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00004308 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
4309 // of complete filenames or remove it entirely.
Richard Smith202210b2014-10-24 20:23:01 +00004310 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00004311
Richard Smith202210b2014-10-24 20:23:01 +00004312 case SUBMODULE_TEXTUAL_HEADER:
4313 case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
4314 // FIXME: Textual headers are not marked in the HeaderInfo table. Load
4315 // them here.
4316 break;
Lawrence Crowlb53e5482013-06-20 21:14:14 +00004317
Guy Benyei11169dd2012-12-18 14:30:41 +00004318 case SUBMODULE_TOPHEADER: {
Argyrios Kyrtzidis3c5305c2013-03-13 21:13:43 +00004319 CurrentModule->addTopHeaderFilename(Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +00004320 break;
4321 }
4322
4323 case SUBMODULE_UMBRELLA_DIR: {
Richard Smith2b63d152015-05-16 02:28:53 +00004324 std::string Dirname = Blob;
4325 ResolveImportedPath(F, Dirname);
4326 if (auto *Umbrella = PP.getFileManager().getDirectory(Dirname)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004327 if (!CurrentModule->getUmbrellaDir())
Richard Smith2b63d152015-05-16 02:28:53 +00004328 ModMap.setUmbrellaDir(CurrentModule, Umbrella, Blob);
4329 else if (CurrentModule->getUmbrellaDir().Entry != Umbrella) {
Ben Langmuir2c9af442014-04-10 17:57:43 +00004330 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
4331 Error("mismatched umbrella directories in submodule");
4332 return OutOfDate;
Guy Benyei11169dd2012-12-18 14:30:41 +00004333 }
4334 }
4335 break;
4336 }
4337
4338 case SUBMODULE_METADATA: {
Guy Benyei11169dd2012-12-18 14:30:41 +00004339 F.BaseSubmoduleID = getTotalNumSubmodules();
4340 F.LocalNumSubmodules = Record[0];
4341 unsigned LocalBaseSubmoduleID = Record[1];
4342 if (F.LocalNumSubmodules > 0) {
4343 // Introduce the global -> local mapping for submodules within this
4344 // module.
4345 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
4346
4347 // Introduce the local -> global mapping for submodules within this
4348 // module.
4349 F.SubmoduleRemap.insertOrReplace(
4350 std::make_pair(LocalBaseSubmoduleID,
4351 F.BaseSubmoduleID - LocalBaseSubmoduleID));
Ben Langmuirfe971d92014-08-16 04:54:18 +00004352
Ben Langmuir52ca6782014-10-20 16:27:32 +00004353 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
4354 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004355 break;
4356 }
4357
4358 case SUBMODULE_IMPORTS: {
Guy Benyei11169dd2012-12-18 14:30:41 +00004359 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
Douglas Gregorfb912652013-03-20 21:10:35 +00004360 UnresolvedModuleRef Unresolved;
Guy Benyei11169dd2012-12-18 14:30:41 +00004361 Unresolved.File = &F;
4362 Unresolved.Mod = CurrentModule;
4363 Unresolved.ID = Record[Idx];
Douglas Gregorfb912652013-03-20 21:10:35 +00004364 Unresolved.Kind = UnresolvedModuleRef::Import;
Guy Benyei11169dd2012-12-18 14:30:41 +00004365 Unresolved.IsWildcard = false;
Douglas Gregorfb912652013-03-20 21:10:35 +00004366 UnresolvedModuleRefs.push_back(Unresolved);
Guy Benyei11169dd2012-12-18 14:30:41 +00004367 }
4368 break;
4369 }
4370
4371 case SUBMODULE_EXPORTS: {
Guy Benyei11169dd2012-12-18 14:30:41 +00004372 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
Douglas Gregorfb912652013-03-20 21:10:35 +00004373 UnresolvedModuleRef Unresolved;
Guy Benyei11169dd2012-12-18 14:30:41 +00004374 Unresolved.File = &F;
4375 Unresolved.Mod = CurrentModule;
4376 Unresolved.ID = Record[Idx];
Douglas Gregorfb912652013-03-20 21:10:35 +00004377 Unresolved.Kind = UnresolvedModuleRef::Export;
Guy Benyei11169dd2012-12-18 14:30:41 +00004378 Unresolved.IsWildcard = Record[Idx + 1];
Douglas Gregorfb912652013-03-20 21:10:35 +00004379 UnresolvedModuleRefs.push_back(Unresolved);
Guy Benyei11169dd2012-12-18 14:30:41 +00004380 }
4381
4382 // Once we've loaded the set of exports, there's no reason to keep
4383 // the parsed, unresolved exports around.
4384 CurrentModule->UnresolvedExports.clear();
4385 break;
4386 }
4387 case SUBMODULE_REQUIRES: {
Richard Smitha3feee22013-10-28 22:18:19 +00004388 CurrentModule->addRequirement(Blob, Record[0], Context.getLangOpts(),
Guy Benyei11169dd2012-12-18 14:30:41 +00004389 Context.getTargetInfo());
4390 break;
4391 }
Douglas Gregor6ddfca92013-01-14 17:21:00 +00004392
4393 case SUBMODULE_LINK_LIBRARY:
Douglas Gregor6ddfca92013-01-14 17:21:00 +00004394 CurrentModule->LinkLibraries.push_back(
Chris Lattner0e6c9402013-01-20 02:38:54 +00004395 Module::LinkLibrary(Blob, Record[0]));
Douglas Gregor6ddfca92013-01-14 17:21:00 +00004396 break;
Douglas Gregor35b13ec2013-03-20 00:22:05 +00004397
4398 case SUBMODULE_CONFIG_MACRO:
Douglas Gregor35b13ec2013-03-20 00:22:05 +00004399 CurrentModule->ConfigMacros.push_back(Blob.str());
4400 break;
Douglas Gregorfb912652013-03-20 21:10:35 +00004401
4402 case SUBMODULE_CONFLICT: {
Douglas Gregorfb912652013-03-20 21:10:35 +00004403 UnresolvedModuleRef Unresolved;
4404 Unresolved.File = &F;
4405 Unresolved.Mod = CurrentModule;
4406 Unresolved.ID = Record[0];
4407 Unresolved.Kind = UnresolvedModuleRef::Conflict;
4408 Unresolved.IsWildcard = false;
4409 Unresolved.String = Blob;
4410 UnresolvedModuleRefs.push_back(Unresolved);
4411 break;
4412 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004413 }
4414 }
4415}
4416
4417/// \brief Parse the record that corresponds to a LangOptions data
4418/// structure.
4419///
4420/// This routine parses the language options from the AST file and then gives
4421/// them to the AST listener if one is set.
4422///
4423/// \returns true if the listener deems the file unacceptable, false otherwise.
4424bool ASTReader::ParseLanguageOptions(const RecordData &Record,
4425 bool Complain,
Richard Smith1e2cf0d2014-10-31 02:28:58 +00004426 ASTReaderListener &Listener,
4427 bool AllowCompatibleDifferences) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004428 LangOptions LangOpts;
4429 unsigned Idx = 0;
4430#define LANGOPT(Name, Bits, Default, Description) \
4431 LangOpts.Name = Record[Idx++];
4432#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
4433 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
4434#include "clang/Basic/LangOptions.def"
Alexey Samsonovedf99a92014-11-07 22:29:38 +00004435#define SANITIZER(NAME, ID) \
4436 LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
Will Dietzf54319c2013-01-18 11:30:38 +00004437#include "clang/Basic/Sanitizers.def"
Guy Benyei11169dd2012-12-18 14:30:41 +00004438
4439 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
4440 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
4441 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
4442
4443 unsigned Length = Record[Idx++];
4444 LangOpts.CurrentModule.assign(Record.begin() + Idx,
4445 Record.begin() + Idx + Length);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00004446
4447 Idx += Length;
4448
4449 // Comment options.
4450 for (unsigned N = Record[Idx++]; N; --N) {
4451 LangOpts.CommentOpts.BlockCommandNames.push_back(
4452 ReadString(Record, Idx));
4453 }
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00004454 LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00004455
Richard Smith1e2cf0d2014-10-31 02:28:58 +00004456 return Listener.ReadLanguageOptions(LangOpts, Complain,
4457 AllowCompatibleDifferences);
Guy Benyei11169dd2012-12-18 14:30:41 +00004458}
4459
Chandler Carruth0d745bc2015-03-14 04:47:43 +00004460bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain,
4461 ASTReaderListener &Listener,
4462 bool AllowCompatibleDifferences) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004463 unsigned Idx = 0;
4464 TargetOptions TargetOpts;
4465 TargetOpts.Triple = ReadString(Record, Idx);
4466 TargetOpts.CPU = ReadString(Record, Idx);
4467 TargetOpts.ABI = ReadString(Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00004468 for (unsigned N = Record[Idx++]; N; --N) {
4469 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
4470 }
4471 for (unsigned N = Record[Idx++]; N; --N) {
4472 TargetOpts.Features.push_back(ReadString(Record, Idx));
4473 }
4474
Chandler Carruth0d745bc2015-03-14 04:47:43 +00004475 return Listener.ReadTargetOptions(TargetOpts, Complain,
4476 AllowCompatibleDifferences);
Guy Benyei11169dd2012-12-18 14:30:41 +00004477}
4478
4479bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
4480 ASTReaderListener &Listener) {
Ben Langmuirb92de022014-04-29 16:25:26 +00004481 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions);
Guy Benyei11169dd2012-12-18 14:30:41 +00004482 unsigned Idx = 0;
Ben Langmuirb92de022014-04-29 16:25:26 +00004483#define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00004484#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
Ben Langmuirb92de022014-04-29 16:25:26 +00004485 DiagOpts->set##Name(static_cast<Type>(Record[Idx++]));
Guy Benyei11169dd2012-12-18 14:30:41 +00004486#include "clang/Basic/DiagnosticOptions.def"
4487
Richard Smith3be1cb22014-08-07 00:24:21 +00004488 for (unsigned N = Record[Idx++]; N; --N)
Ben Langmuirb92de022014-04-29 16:25:26 +00004489 DiagOpts->Warnings.push_back(ReadString(Record, Idx));
Richard Smith3be1cb22014-08-07 00:24:21 +00004490 for (unsigned N = Record[Idx++]; N; --N)
4491 DiagOpts->Remarks.push_back(ReadString(Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00004492
4493 return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
4494}
4495
4496bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
4497 ASTReaderListener &Listener) {
4498 FileSystemOptions FSOpts;
4499 unsigned Idx = 0;
4500 FSOpts.WorkingDir = ReadString(Record, Idx);
4501 return Listener.ReadFileSystemOptions(FSOpts, Complain);
4502}
4503
4504bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
4505 bool Complain,
4506 ASTReaderListener &Listener) {
4507 HeaderSearchOptions HSOpts;
4508 unsigned Idx = 0;
4509 HSOpts.Sysroot = ReadString(Record, Idx);
4510
4511 // Include entries.
4512 for (unsigned N = Record[Idx++]; N; --N) {
4513 std::string Path = ReadString(Record, Idx);
4514 frontend::IncludeDirGroup Group
4515 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
Guy Benyei11169dd2012-12-18 14:30:41 +00004516 bool IsFramework = Record[Idx++];
4517 bool IgnoreSysRoot = Record[Idx++];
Benjamin Kramer3204b152015-05-29 19:42:19 +00004518 HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework,
4519 IgnoreSysRoot);
Guy Benyei11169dd2012-12-18 14:30:41 +00004520 }
4521
4522 // System header prefixes.
4523 for (unsigned N = Record[Idx++]; N; --N) {
4524 std::string Prefix = ReadString(Record, Idx);
4525 bool IsSystemHeader = Record[Idx++];
Benjamin Kramer3204b152015-05-29 19:42:19 +00004526 HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader);
Guy Benyei11169dd2012-12-18 14:30:41 +00004527 }
4528
4529 HSOpts.ResourceDir = ReadString(Record, Idx);
4530 HSOpts.ModuleCachePath = ReadString(Record, Idx);
Argyrios Kyrtzidis1594c152014-03-03 08:12:05 +00004531 HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00004532 HSOpts.DisableModuleHash = Record[Idx++];
4533 HSOpts.UseBuiltinIncludes = Record[Idx++];
4534 HSOpts.UseStandardSystemIncludes = Record[Idx++];
4535 HSOpts.UseStandardCXXIncludes = Record[Idx++];
4536 HSOpts.UseLibcxx = Record[Idx++];
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00004537 std::string SpecificModuleCachePath = ReadString(Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00004538
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00004539 return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
4540 Complain);
Guy Benyei11169dd2012-12-18 14:30:41 +00004541}
4542
4543bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
4544 bool Complain,
4545 ASTReaderListener &Listener,
4546 std::string &SuggestedPredefines) {
4547 PreprocessorOptions PPOpts;
4548 unsigned Idx = 0;
4549
4550 // Macro definitions/undefs
4551 for (unsigned N = Record[Idx++]; N; --N) {
4552 std::string Macro = ReadString(Record, Idx);
4553 bool IsUndef = Record[Idx++];
4554 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
4555 }
4556
4557 // Includes
4558 for (unsigned N = Record[Idx++]; N; --N) {
4559 PPOpts.Includes.push_back(ReadString(Record, Idx));
4560 }
4561
4562 // Macro Includes
4563 for (unsigned N = Record[Idx++]; N; --N) {
4564 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
4565 }
4566
4567 PPOpts.UsePredefines = Record[Idx++];
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00004568 PPOpts.DetailedRecord = Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00004569 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
4570 PPOpts.ImplicitPTHInclude = ReadString(Record, Idx);
4571 PPOpts.ObjCXXARCStandardLibrary =
4572 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
4573 SuggestedPredefines.clear();
4574 return Listener.ReadPreprocessorOptions(PPOpts, Complain,
4575 SuggestedPredefines);
4576}
4577
4578std::pair<ModuleFile *, unsigned>
4579ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
4580 GlobalPreprocessedEntityMapType::iterator
4581 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
4582 assert(I != GlobalPreprocessedEntityMap.end() &&
4583 "Corrupted global preprocessed entity map");
4584 ModuleFile *M = I->second;
4585 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
4586 return std::make_pair(M, LocalIndex);
4587}
4588
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00004589llvm::iterator_range<PreprocessingRecord::iterator>
Guy Benyei11169dd2012-12-18 14:30:41 +00004590ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
4591 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
4592 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
4593 Mod.NumPreprocessedEntities);
4594
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00004595 return llvm::make_range(PreprocessingRecord::iterator(),
4596 PreprocessingRecord::iterator());
Guy Benyei11169dd2012-12-18 14:30:41 +00004597}
4598
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00004599llvm::iterator_range<ASTReader::ModuleDeclIterator>
Guy Benyei11169dd2012-12-18 14:30:41 +00004600ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00004601 return llvm::make_range(
4602 ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
4603 ModuleDeclIterator(this, &Mod,
4604 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
Guy Benyei11169dd2012-12-18 14:30:41 +00004605}
4606
4607PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
4608 PreprocessedEntityID PPID = Index+1;
4609 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
4610 ModuleFile &M = *PPInfo.first;
4611 unsigned LocalIndex = PPInfo.second;
4612 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
4613
Guy Benyei11169dd2012-12-18 14:30:41 +00004614 if (!PP.getPreprocessingRecord()) {
4615 Error("no preprocessing record");
Craig Toppera13603a2014-05-22 05:54:18 +00004616 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00004617 }
4618
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004619 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
4620 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
4621
4622 llvm::BitstreamEntry Entry =
4623 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
4624 if (Entry.Kind != llvm::BitstreamEntry::Record)
Craig Toppera13603a2014-05-22 05:54:18 +00004625 return nullptr;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004626
Guy Benyei11169dd2012-12-18 14:30:41 +00004627 // Read the record.
4628 SourceRange Range(ReadSourceLocation(M, PPOffs.Begin),
4629 ReadSourceLocation(M, PPOffs.End));
4630 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
Chris Lattner0e6c9402013-01-20 02:38:54 +00004631 StringRef Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00004632 RecordData Record;
4633 PreprocessorDetailRecordTypes RecType =
Chris Lattner0e6c9402013-01-20 02:38:54 +00004634 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.readRecord(
4635 Entry.ID, Record, &Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +00004636 switch (RecType) {
4637 case PPD_MACRO_EXPANSION: {
4638 bool isBuiltin = Record[0];
Craig Toppera13603a2014-05-22 05:54:18 +00004639 IdentifierInfo *Name = nullptr;
Richard Smith66a81862015-05-04 02:25:31 +00004640 MacroDefinitionRecord *Def = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00004641 if (isBuiltin)
4642 Name = getLocalIdentifier(M, Record[1]);
4643 else {
Richard Smith66a81862015-05-04 02:25:31 +00004644 PreprocessedEntityID GlobalID =
4645 getGlobalPreprocessedEntityID(M, Record[1]);
4646 Def = cast<MacroDefinitionRecord>(
4647 PPRec.getLoadedPreprocessedEntity(GlobalID - 1));
Guy Benyei11169dd2012-12-18 14:30:41 +00004648 }
4649
4650 MacroExpansion *ME;
4651 if (isBuiltin)
4652 ME = new (PPRec) MacroExpansion(Name, Range);
4653 else
4654 ME = new (PPRec) MacroExpansion(Def, Range);
4655
4656 return ME;
4657 }
4658
4659 case PPD_MACRO_DEFINITION: {
4660 // Decode the identifier info and then check again; if the macro is
4661 // still defined and associated with the identifier,
4662 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
Richard Smith66a81862015-05-04 02:25:31 +00004663 MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
Guy Benyei11169dd2012-12-18 14:30:41 +00004664
4665 if (DeserializationListener)
4666 DeserializationListener->MacroDefinitionRead(PPID, MD);
4667
4668 return MD;
4669 }
4670
4671 case PPD_INCLUSION_DIRECTIVE: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00004672 const char *FullFileNameStart = Blob.data() + Record[0];
4673 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
Craig Toppera13603a2014-05-22 05:54:18 +00004674 const FileEntry *File = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00004675 if (!FullFileName.empty())
4676 File = PP.getFileManager().getFile(FullFileName);
4677
4678 // FIXME: Stable encoding
4679 InclusionDirective::InclusionKind Kind
4680 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
4681 InclusionDirective *ID
4682 = new (PPRec) InclusionDirective(PPRec, Kind,
Chris Lattner0e6c9402013-01-20 02:38:54 +00004683 StringRef(Blob.data(), Record[0]),
Guy Benyei11169dd2012-12-18 14:30:41 +00004684 Record[1], Record[3],
4685 File,
4686 Range);
4687 return ID;
4688 }
4689 }
4690
4691 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
4692}
4693
4694/// \brief \arg SLocMapI points at a chunk of a module that contains no
4695/// preprocessed entities or the entities it contains are not the ones we are
4696/// looking for. Find the next module that contains entities and return the ID
4697/// of the first entry.
4698PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
4699 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
4700 ++SLocMapI;
4701 for (GlobalSLocOffsetMapType::const_iterator
4702 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
4703 ModuleFile &M = *SLocMapI->second;
4704 if (M.NumPreprocessedEntities)
4705 return M.BasePreprocessedEntityID;
4706 }
4707
4708 return getTotalNumPreprocessedEntities();
4709}
4710
4711namespace {
4712
4713template <unsigned PPEntityOffset::*PPLoc>
4714struct PPEntityComp {
4715 const ASTReader &Reader;
4716 ModuleFile &M;
4717
4718 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) { }
4719
4720 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
4721 SourceLocation LHS = getLoc(L);
4722 SourceLocation RHS = getLoc(R);
4723 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4724 }
4725
4726 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
4727 SourceLocation LHS = getLoc(L);
4728 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4729 }
4730
4731 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
4732 SourceLocation RHS = getLoc(R);
4733 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4734 }
4735
4736 SourceLocation getLoc(const PPEntityOffset &PPE) const {
4737 return Reader.ReadSourceLocation(M, PPE.*PPLoc);
4738 }
4739};
4740
4741}
4742
Alp Toker2e9ce4c2014-05-16 18:59:21 +00004743PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
4744 bool EndsAfter) const {
4745 if (SourceMgr.isLocalSourceLocation(Loc))
Guy Benyei11169dd2012-12-18 14:30:41 +00004746 return getTotalNumPreprocessedEntities();
4747
Alp Toker2e9ce4c2014-05-16 18:59:21 +00004748 GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
4749 SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
Guy Benyei11169dd2012-12-18 14:30:41 +00004750 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
4751 "Corrupted global sloc offset map");
4752
4753 if (SLocMapI->second->NumPreprocessedEntities == 0)
4754 return findNextPreprocessedEntity(SLocMapI);
4755
4756 ModuleFile &M = *SLocMapI->second;
4757 typedef const PPEntityOffset *pp_iterator;
4758 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
4759 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
4760
4761 size_t Count = M.NumPreprocessedEntities;
4762 size_t Half;
4763 pp_iterator First = pp_begin;
4764 pp_iterator PPI;
4765
Alp Toker2e9ce4c2014-05-16 18:59:21 +00004766 if (EndsAfter) {
4767 PPI = std::upper_bound(pp_begin, pp_end, Loc,
4768 PPEntityComp<&PPEntityOffset::Begin>(*this, M));
4769 } else {
4770 // Do a binary search manually instead of using std::lower_bound because
4771 // The end locations of entities may be unordered (when a macro expansion
4772 // is inside another macro argument), but for this case it is not important
4773 // whether we get the first macro expansion or its containing macro.
4774 while (Count > 0) {
4775 Half = Count / 2;
4776 PPI = First;
4777 std::advance(PPI, Half);
4778 if (SourceMgr.isBeforeInTranslationUnit(ReadSourceLocation(M, PPI->End),
4779 Loc)) {
4780 First = PPI;
4781 ++First;
4782 Count = Count - Half - 1;
4783 } else
4784 Count = Half;
4785 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004786 }
4787
4788 if (PPI == pp_end)
4789 return findNextPreprocessedEntity(SLocMapI);
4790
4791 return M.BasePreprocessedEntityID + (PPI - pp_begin);
4792}
4793
Guy Benyei11169dd2012-12-18 14:30:41 +00004794/// \brief Returns a pair of [Begin, End) indices of preallocated
4795/// preprocessed entities that \arg Range encompasses.
4796std::pair<unsigned, unsigned>
4797 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
4798 if (Range.isInvalid())
4799 return std::make_pair(0,0);
4800 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
4801
Alp Toker2e9ce4c2014-05-16 18:59:21 +00004802 PreprocessedEntityID BeginID =
4803 findPreprocessedEntity(Range.getBegin(), false);
4804 PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true);
Guy Benyei11169dd2012-12-18 14:30:41 +00004805 return std::make_pair(BeginID, EndID);
4806}
4807
4808/// \brief Optionally returns true or false if the preallocated preprocessed
4809/// entity with index \arg Index came from file \arg FID.
David Blaikie05785d12013-02-20 22:23:23 +00004810Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
Guy Benyei11169dd2012-12-18 14:30:41 +00004811 FileID FID) {
4812 if (FID.isInvalid())
4813 return false;
4814
4815 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
4816 ModuleFile &M = *PPInfo.first;
4817 unsigned LocalIndex = PPInfo.second;
4818 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
4819
4820 SourceLocation Loc = ReadSourceLocation(M, PPOffs.Begin);
4821 if (Loc.isInvalid())
4822 return false;
4823
4824 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
4825 return true;
4826 else
4827 return false;
4828}
4829
4830namespace {
4831 /// \brief Visitor used to search for information about a header file.
4832 class HeaderFileInfoVisitor {
Guy Benyei11169dd2012-12-18 14:30:41 +00004833 const FileEntry *FE;
4834
David Blaikie05785d12013-02-20 22:23:23 +00004835 Optional<HeaderFileInfo> HFI;
Guy Benyei11169dd2012-12-18 14:30:41 +00004836
4837 public:
Argyrios Kyrtzidis61a38962013-03-06 18:12:44 +00004838 explicit HeaderFileInfoVisitor(const FileEntry *FE)
4839 : FE(FE) { }
Guy Benyei11169dd2012-12-18 14:30:41 +00004840
4841 static bool visit(ModuleFile &M, void *UserData) {
4842 HeaderFileInfoVisitor *This
4843 = static_cast<HeaderFileInfoVisitor *>(UserData);
4844
Guy Benyei11169dd2012-12-18 14:30:41 +00004845 HeaderFileInfoLookupTable *Table
4846 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
4847 if (!Table)
4848 return false;
4849
4850 // Look in the on-disk hash table for an entry for this file name.
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00004851 HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE);
Guy Benyei11169dd2012-12-18 14:30:41 +00004852 if (Pos == Table->end())
4853 return false;
4854
4855 This->HFI = *Pos;
4856 return true;
4857 }
4858
David Blaikie05785d12013-02-20 22:23:23 +00004859 Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
Guy Benyei11169dd2012-12-18 14:30:41 +00004860 };
4861}
4862
4863HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
Argyrios Kyrtzidis61a38962013-03-06 18:12:44 +00004864 HeaderFileInfoVisitor Visitor(FE);
Guy Benyei11169dd2012-12-18 14:30:41 +00004865 ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor);
Argyrios Kyrtzidis1054bbf2013-05-08 23:46:55 +00004866 if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
Guy Benyei11169dd2012-12-18 14:30:41 +00004867 return *HFI;
Guy Benyei11169dd2012-12-18 14:30:41 +00004868
4869 return HeaderFileInfo();
4870}
4871
4872void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
4873 // FIXME: Make it work properly with modules.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004874 SmallVector<DiagnosticsEngine::DiagState *, 32> DiagStates;
Guy Benyei11169dd2012-12-18 14:30:41 +00004875 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
4876 ModuleFile &F = *(*I);
4877 unsigned Idx = 0;
4878 DiagStates.clear();
4879 assert(!Diag.DiagStates.empty());
4880 DiagStates.push_back(&Diag.DiagStates.front()); // the command-line one.
4881 while (Idx < F.PragmaDiagMappings.size()) {
4882 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
4883 unsigned DiagStateID = F.PragmaDiagMappings[Idx++];
4884 if (DiagStateID != 0) {
4885 Diag.DiagStatePoints.push_back(
4886 DiagnosticsEngine::DiagStatePoint(DiagStates[DiagStateID-1],
4887 FullSourceLoc(Loc, SourceMgr)));
4888 continue;
4889 }
4890
4891 assert(DiagStateID == 0);
4892 // A new DiagState was created here.
4893 Diag.DiagStates.push_back(*Diag.GetCurDiagState());
4894 DiagnosticsEngine::DiagState *NewState = &Diag.DiagStates.back();
4895 DiagStates.push_back(NewState);
4896 Diag.DiagStatePoints.push_back(
4897 DiagnosticsEngine::DiagStatePoint(NewState,
4898 FullSourceLoc(Loc, SourceMgr)));
4899 while (1) {
4900 assert(Idx < F.PragmaDiagMappings.size() &&
4901 "Invalid data, didn't find '-1' marking end of diag/map pairs");
4902 if (Idx >= F.PragmaDiagMappings.size()) {
4903 break; // Something is messed up but at least avoid infinite loop in
4904 // release build.
4905 }
4906 unsigned DiagID = F.PragmaDiagMappings[Idx++];
4907 if (DiagID == (unsigned)-1) {
4908 break; // no more diag/map pairs for this location.
4909 }
Alp Tokerc726c362014-06-10 09:31:37 +00004910 diag::Severity Map = (diag::Severity)F.PragmaDiagMappings[Idx++];
4911 DiagnosticMapping Mapping = Diag.makeUserMapping(Map, Loc);
4912 Diag.GetCurDiagState()->setMapping(DiagID, Mapping);
Guy Benyei11169dd2012-12-18 14:30:41 +00004913 }
4914 }
4915 }
4916}
4917
4918/// \brief Get the correct cursor and offset for loading a type.
4919ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
4920 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
4921 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
4922 ModuleFile *M = I->second;
4923 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
4924}
4925
4926/// \brief Read and return the type with the given index..
4927///
4928/// The index is the type ID, shifted and minus the number of predefs. This
4929/// routine actually reads the record corresponding to the type at the given
4930/// location. It is a helper routine for GetType, which deals with reading type
4931/// IDs.
4932QualType ASTReader::readTypeRecord(unsigned Index) {
4933 RecordLocation Loc = TypeCursorForIndex(Index);
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004934 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00004935
4936 // Keep track of where we are in the stream, then jump back there
4937 // after reading this type.
4938 SavedStreamPosition SavedPosition(DeclsCursor);
4939
4940 ReadingKindTracker ReadingKind(Read_Type, *this);
4941
4942 // Note that we are loading a type record.
4943 Deserializing AType(this);
4944
4945 unsigned Idx = 0;
4946 DeclsCursor.JumpToBit(Loc.Offset);
4947 RecordData Record;
4948 unsigned Code = DeclsCursor.ReadCode();
Chris Lattner0e6c9402013-01-20 02:38:54 +00004949 switch ((TypeCode)DeclsCursor.readRecord(Code, Record)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004950 case TYPE_EXT_QUAL: {
4951 if (Record.size() != 2) {
4952 Error("Incorrect encoding of extended qualifier type");
4953 return QualType();
4954 }
4955 QualType Base = readType(*Loc.F, Record, Idx);
4956 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
4957 return Context.getQualifiedType(Base, Quals);
4958 }
4959
4960 case TYPE_COMPLEX: {
4961 if (Record.size() != 1) {
4962 Error("Incorrect encoding of complex type");
4963 return QualType();
4964 }
4965 QualType ElemType = readType(*Loc.F, Record, Idx);
4966 return Context.getComplexType(ElemType);
4967 }
4968
4969 case TYPE_POINTER: {
4970 if (Record.size() != 1) {
4971 Error("Incorrect encoding of pointer type");
4972 return QualType();
4973 }
4974 QualType PointeeType = readType(*Loc.F, Record, Idx);
4975 return Context.getPointerType(PointeeType);
4976 }
4977
Reid Kleckner8a365022013-06-24 17:51:48 +00004978 case TYPE_DECAYED: {
4979 if (Record.size() != 1) {
4980 Error("Incorrect encoding of decayed type");
4981 return QualType();
4982 }
4983 QualType OriginalType = readType(*Loc.F, Record, Idx);
4984 QualType DT = Context.getAdjustedParameterType(OriginalType);
4985 if (!isa<DecayedType>(DT))
4986 Error("Decayed type does not decay");
4987 return DT;
4988 }
4989
Reid Kleckner0503a872013-12-05 01:23:43 +00004990 case TYPE_ADJUSTED: {
4991 if (Record.size() != 2) {
4992 Error("Incorrect encoding of adjusted type");
4993 return QualType();
4994 }
4995 QualType OriginalTy = readType(*Loc.F, Record, Idx);
4996 QualType AdjustedTy = readType(*Loc.F, Record, Idx);
4997 return Context.getAdjustedType(OriginalTy, AdjustedTy);
4998 }
4999
Guy Benyei11169dd2012-12-18 14:30:41 +00005000 case TYPE_BLOCK_POINTER: {
5001 if (Record.size() != 1) {
5002 Error("Incorrect encoding of block pointer type");
5003 return QualType();
5004 }
5005 QualType PointeeType = readType(*Loc.F, Record, Idx);
5006 return Context.getBlockPointerType(PointeeType);
5007 }
5008
5009 case TYPE_LVALUE_REFERENCE: {
5010 if (Record.size() != 2) {
5011 Error("Incorrect encoding of lvalue reference type");
5012 return QualType();
5013 }
5014 QualType PointeeType = readType(*Loc.F, Record, Idx);
5015 return Context.getLValueReferenceType(PointeeType, Record[1]);
5016 }
5017
5018 case TYPE_RVALUE_REFERENCE: {
5019 if (Record.size() != 1) {
5020 Error("Incorrect encoding of rvalue reference type");
5021 return QualType();
5022 }
5023 QualType PointeeType = readType(*Loc.F, Record, Idx);
5024 return Context.getRValueReferenceType(PointeeType);
5025 }
5026
5027 case TYPE_MEMBER_POINTER: {
5028 if (Record.size() != 2) {
5029 Error("Incorrect encoding of member pointer type");
5030 return QualType();
5031 }
5032 QualType PointeeType = readType(*Loc.F, Record, Idx);
5033 QualType ClassType = readType(*Loc.F, Record, Idx);
5034 if (PointeeType.isNull() || ClassType.isNull())
5035 return QualType();
5036
5037 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
5038 }
5039
5040 case TYPE_CONSTANT_ARRAY: {
5041 QualType ElementType = readType(*Loc.F, Record, Idx);
5042 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
5043 unsigned IndexTypeQuals = Record[2];
5044 unsigned Idx = 3;
5045 llvm::APInt Size = ReadAPInt(Record, Idx);
5046 return Context.getConstantArrayType(ElementType, Size,
5047 ASM, IndexTypeQuals);
5048 }
5049
5050 case TYPE_INCOMPLETE_ARRAY: {
5051 QualType ElementType = readType(*Loc.F, Record, Idx);
5052 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
5053 unsigned IndexTypeQuals = Record[2];
5054 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
5055 }
5056
5057 case TYPE_VARIABLE_ARRAY: {
5058 QualType ElementType = readType(*Loc.F, Record, Idx);
5059 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
5060 unsigned IndexTypeQuals = Record[2];
5061 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
5062 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
5063 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
5064 ASM, IndexTypeQuals,
5065 SourceRange(LBLoc, RBLoc));
5066 }
5067
5068 case TYPE_VECTOR: {
5069 if (Record.size() != 3) {
5070 Error("incorrect encoding of vector type in AST file");
5071 return QualType();
5072 }
5073
5074 QualType ElementType = readType(*Loc.F, Record, Idx);
5075 unsigned NumElements = Record[1];
5076 unsigned VecKind = Record[2];
5077 return Context.getVectorType(ElementType, NumElements,
5078 (VectorType::VectorKind)VecKind);
5079 }
5080
5081 case TYPE_EXT_VECTOR: {
5082 if (Record.size() != 3) {
5083 Error("incorrect encoding of extended vector type in AST file");
5084 return QualType();
5085 }
5086
5087 QualType ElementType = readType(*Loc.F, Record, Idx);
5088 unsigned NumElements = Record[1];
5089 return Context.getExtVectorType(ElementType, NumElements);
5090 }
5091
5092 case TYPE_FUNCTION_NO_PROTO: {
5093 if (Record.size() != 6) {
5094 Error("incorrect encoding of no-proto function type");
5095 return QualType();
5096 }
5097 QualType ResultType = readType(*Loc.F, Record, Idx);
5098 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
5099 (CallingConv)Record[4], Record[5]);
5100 return Context.getFunctionNoProtoType(ResultType, Info);
5101 }
5102
5103 case TYPE_FUNCTION_PROTO: {
5104 QualType ResultType = readType(*Loc.F, Record, Idx);
5105
5106 FunctionProtoType::ExtProtoInfo EPI;
5107 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
5108 /*hasregparm*/ Record[2],
5109 /*regparm*/ Record[3],
5110 static_cast<CallingConv>(Record[4]),
5111 /*produces*/ Record[5]);
5112
5113 unsigned Idx = 6;
Guy Benyei11169dd2012-12-18 14:30:41 +00005114
5115 EPI.Variadic = Record[Idx++];
5116 EPI.HasTrailingReturn = Record[Idx++];
5117 EPI.TypeQuals = Record[Idx++];
5118 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
Richard Smith564417a2014-03-20 21:47:22 +00005119 SmallVector<QualType, 8> ExceptionStorage;
Richard Smith8acb4282014-07-31 21:57:55 +00005120 readExceptionSpec(*Loc.F, ExceptionStorage, EPI.ExceptionSpec, Record, Idx);
Richard Smith01b2cb42014-07-26 06:37:51 +00005121
5122 unsigned NumParams = Record[Idx++];
5123 SmallVector<QualType, 16> ParamTypes;
5124 for (unsigned I = 0; I != NumParams; ++I)
5125 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
5126
Jordan Rose5c382722013-03-08 21:51:21 +00005127 return Context.getFunctionType(ResultType, ParamTypes, EPI);
Guy Benyei11169dd2012-12-18 14:30:41 +00005128 }
5129
5130 case TYPE_UNRESOLVED_USING: {
5131 unsigned Idx = 0;
5132 return Context.getTypeDeclType(
5133 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
5134 }
5135
5136 case TYPE_TYPEDEF: {
5137 if (Record.size() != 2) {
5138 Error("incorrect encoding of typedef type");
5139 return QualType();
5140 }
5141 unsigned Idx = 0;
5142 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
5143 QualType Canonical = readType(*Loc.F, Record, Idx);
5144 if (!Canonical.isNull())
5145 Canonical = Context.getCanonicalType(Canonical);
5146 return Context.getTypedefType(Decl, Canonical);
5147 }
5148
5149 case TYPE_TYPEOF_EXPR:
5150 return Context.getTypeOfExprType(ReadExpr(*Loc.F));
5151
5152 case TYPE_TYPEOF: {
5153 if (Record.size() != 1) {
5154 Error("incorrect encoding of typeof(type) in AST file");
5155 return QualType();
5156 }
5157 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
5158 return Context.getTypeOfType(UnderlyingType);
5159 }
5160
5161 case TYPE_DECLTYPE: {
5162 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
5163 return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
5164 }
5165
5166 case TYPE_UNARY_TRANSFORM: {
5167 QualType BaseType = readType(*Loc.F, Record, Idx);
5168 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
5169 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
5170 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
5171 }
5172
Richard Smith74aeef52013-04-26 16:15:35 +00005173 case TYPE_AUTO: {
5174 QualType Deduced = readType(*Loc.F, Record, Idx);
5175 bool IsDecltypeAuto = Record[Idx++];
Richard Smith27d807c2013-04-30 13:56:41 +00005176 bool IsDependent = Deduced.isNull() ? Record[Idx++] : false;
Manuel Klimek2fdbea22013-08-22 12:12:24 +00005177 return Context.getAutoType(Deduced, IsDecltypeAuto, IsDependent);
Richard Smith74aeef52013-04-26 16:15:35 +00005178 }
Guy Benyei11169dd2012-12-18 14:30:41 +00005179
5180 case TYPE_RECORD: {
5181 if (Record.size() != 2) {
5182 Error("incorrect encoding of record type");
5183 return QualType();
5184 }
5185 unsigned Idx = 0;
5186 bool IsDependent = Record[Idx++];
5187 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
5188 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
5189 QualType T = Context.getRecordType(RD);
5190 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
5191 return T;
5192 }
5193
5194 case TYPE_ENUM: {
5195 if (Record.size() != 2) {
5196 Error("incorrect encoding of enum type");
5197 return QualType();
5198 }
5199 unsigned Idx = 0;
5200 bool IsDependent = Record[Idx++];
5201 QualType T
5202 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
5203 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
5204 return T;
5205 }
5206
5207 case TYPE_ATTRIBUTED: {
5208 if (Record.size() != 3) {
5209 Error("incorrect encoding of attributed type");
5210 return QualType();
5211 }
5212 QualType modifiedType = readType(*Loc.F, Record, Idx);
5213 QualType equivalentType = readType(*Loc.F, Record, Idx);
5214 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
5215 return Context.getAttributedType(kind, modifiedType, equivalentType);
5216 }
5217
5218 case TYPE_PAREN: {
5219 if (Record.size() != 1) {
5220 Error("incorrect encoding of paren type");
5221 return QualType();
5222 }
5223 QualType InnerType = readType(*Loc.F, Record, Idx);
5224 return Context.getParenType(InnerType);
5225 }
5226
5227 case TYPE_PACK_EXPANSION: {
5228 if (Record.size() != 2) {
5229 Error("incorrect encoding of pack expansion type");
5230 return QualType();
5231 }
5232 QualType Pattern = readType(*Loc.F, Record, Idx);
5233 if (Pattern.isNull())
5234 return QualType();
David Blaikie05785d12013-02-20 22:23:23 +00005235 Optional<unsigned> NumExpansions;
Guy Benyei11169dd2012-12-18 14:30:41 +00005236 if (Record[1])
5237 NumExpansions = Record[1] - 1;
5238 return Context.getPackExpansionType(Pattern, NumExpansions);
5239 }
5240
5241 case TYPE_ELABORATED: {
5242 unsigned Idx = 0;
5243 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
5244 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
5245 QualType NamedType = readType(*Loc.F, Record, Idx);
5246 return Context.getElaboratedType(Keyword, NNS, NamedType);
5247 }
5248
5249 case TYPE_OBJC_INTERFACE: {
5250 unsigned Idx = 0;
5251 ObjCInterfaceDecl *ItfD
5252 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
5253 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
5254 }
5255
5256 case TYPE_OBJC_OBJECT: {
5257 unsigned Idx = 0;
5258 QualType Base = readType(*Loc.F, Record, Idx);
5259 unsigned NumProtos = Record[Idx++];
5260 SmallVector<ObjCProtocolDecl*, 4> Protos;
5261 for (unsigned I = 0; I != NumProtos; ++I)
5262 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
5263 return Context.getObjCObjectType(Base, Protos.data(), NumProtos);
5264 }
5265
5266 case TYPE_OBJC_OBJECT_POINTER: {
5267 unsigned Idx = 0;
5268 QualType Pointee = readType(*Loc.F, Record, Idx);
5269 return Context.getObjCObjectPointerType(Pointee);
5270 }
5271
5272 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
5273 unsigned Idx = 0;
5274 QualType Parm = readType(*Loc.F, Record, Idx);
5275 QualType Replacement = readType(*Loc.F, Record, Idx);
Stephan Tolksdorfe96f8b32014-03-15 10:23:27 +00005276 return Context.getSubstTemplateTypeParmType(
5277 cast<TemplateTypeParmType>(Parm),
5278 Context.getCanonicalType(Replacement));
Guy Benyei11169dd2012-12-18 14:30:41 +00005279 }
5280
5281 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
5282 unsigned Idx = 0;
5283 QualType Parm = readType(*Loc.F, Record, Idx);
5284 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
5285 return Context.getSubstTemplateTypeParmPackType(
5286 cast<TemplateTypeParmType>(Parm),
5287 ArgPack);
5288 }
5289
5290 case TYPE_INJECTED_CLASS_NAME: {
5291 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
5292 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
5293 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
5294 // for AST reading, too much interdependencies.
Richard Smith6377f8f2014-10-21 21:15:18 +00005295 const Type *T = nullptr;
5296 for (auto *DI = D; DI; DI = DI->getPreviousDecl()) {
5297 if (const Type *Existing = DI->getTypeForDecl()) {
5298 T = Existing;
5299 break;
5300 }
5301 }
5302 if (!T) {
Richard Smithf17fdbd2014-04-24 02:25:27 +00005303 T = new (Context, TypeAlignment) InjectedClassNameType(D, TST);
Richard Smith6377f8f2014-10-21 21:15:18 +00005304 for (auto *DI = D; DI; DI = DI->getPreviousDecl())
5305 DI->setTypeForDecl(T);
5306 }
Richard Smithf17fdbd2014-04-24 02:25:27 +00005307 return QualType(T, 0);
Guy Benyei11169dd2012-12-18 14:30:41 +00005308 }
5309
5310 case TYPE_TEMPLATE_TYPE_PARM: {
5311 unsigned Idx = 0;
5312 unsigned Depth = Record[Idx++];
5313 unsigned Index = Record[Idx++];
5314 bool Pack = Record[Idx++];
5315 TemplateTypeParmDecl *D
5316 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
5317 return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
5318 }
5319
5320 case TYPE_DEPENDENT_NAME: {
5321 unsigned Idx = 0;
5322 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
5323 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
5324 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
5325 QualType Canon = readType(*Loc.F, Record, Idx);
5326 if (!Canon.isNull())
5327 Canon = Context.getCanonicalType(Canon);
5328 return Context.getDependentNameType(Keyword, NNS, Name, Canon);
5329 }
5330
5331 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
5332 unsigned Idx = 0;
5333 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
5334 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
5335 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
5336 unsigned NumArgs = Record[Idx++];
5337 SmallVector<TemplateArgument, 8> Args;
5338 Args.reserve(NumArgs);
5339 while (NumArgs--)
5340 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
5341 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
5342 Args.size(), Args.data());
5343 }
5344
5345 case TYPE_DEPENDENT_SIZED_ARRAY: {
5346 unsigned Idx = 0;
5347
5348 // ArrayType
5349 QualType ElementType = readType(*Loc.F, Record, Idx);
5350 ArrayType::ArraySizeModifier ASM
5351 = (ArrayType::ArraySizeModifier)Record[Idx++];
5352 unsigned IndexTypeQuals = Record[Idx++];
5353
5354 // DependentSizedArrayType
5355 Expr *NumElts = ReadExpr(*Loc.F);
5356 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
5357
5358 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
5359 IndexTypeQuals, Brackets);
5360 }
5361
5362 case TYPE_TEMPLATE_SPECIALIZATION: {
5363 unsigned Idx = 0;
5364 bool IsDependent = Record[Idx++];
5365 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
5366 SmallVector<TemplateArgument, 8> Args;
5367 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
5368 QualType Underlying = readType(*Loc.F, Record, Idx);
5369 QualType T;
5370 if (Underlying.isNull())
5371 T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(),
5372 Args.size());
5373 else
5374 T = Context.getTemplateSpecializationType(Name, Args.data(),
5375 Args.size(), Underlying);
5376 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
5377 return T;
5378 }
5379
5380 case TYPE_ATOMIC: {
5381 if (Record.size() != 1) {
5382 Error("Incorrect encoding of atomic type");
5383 return QualType();
5384 }
5385 QualType ValueType = readType(*Loc.F, Record, Idx);
5386 return Context.getAtomicType(ValueType);
5387 }
5388 }
5389 llvm_unreachable("Invalid TypeCode!");
5390}
5391
Richard Smith564417a2014-03-20 21:47:22 +00005392void ASTReader::readExceptionSpec(ModuleFile &ModuleFile,
5393 SmallVectorImpl<QualType> &Exceptions,
Richard Smith8acb4282014-07-31 21:57:55 +00005394 FunctionProtoType::ExceptionSpecInfo &ESI,
Richard Smith564417a2014-03-20 21:47:22 +00005395 const RecordData &Record, unsigned &Idx) {
5396 ExceptionSpecificationType EST =
5397 static_cast<ExceptionSpecificationType>(Record[Idx++]);
Richard Smith8acb4282014-07-31 21:57:55 +00005398 ESI.Type = EST;
Richard Smith564417a2014-03-20 21:47:22 +00005399 if (EST == EST_Dynamic) {
Richard Smith8acb4282014-07-31 21:57:55 +00005400 for (unsigned I = 0, N = Record[Idx++]; I != N; ++I)
Richard Smith564417a2014-03-20 21:47:22 +00005401 Exceptions.push_back(readType(ModuleFile, Record, Idx));
Richard Smith8acb4282014-07-31 21:57:55 +00005402 ESI.Exceptions = Exceptions;
Richard Smith564417a2014-03-20 21:47:22 +00005403 } else if (EST == EST_ComputedNoexcept) {
Richard Smith8acb4282014-07-31 21:57:55 +00005404 ESI.NoexceptExpr = ReadExpr(ModuleFile);
Richard Smith564417a2014-03-20 21:47:22 +00005405 } else if (EST == EST_Uninstantiated) {
Richard Smith8acb4282014-07-31 21:57:55 +00005406 ESI.SourceDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
5407 ESI.SourceTemplate = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
Richard Smith564417a2014-03-20 21:47:22 +00005408 } else if (EST == EST_Unevaluated) {
Richard Smith8acb4282014-07-31 21:57:55 +00005409 ESI.SourceDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
Richard Smith564417a2014-03-20 21:47:22 +00005410 }
5411}
5412
Guy Benyei11169dd2012-12-18 14:30:41 +00005413class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
5414 ASTReader &Reader;
5415 ModuleFile &F;
5416 const ASTReader::RecordData &Record;
5417 unsigned &Idx;
5418
5419 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
5420 unsigned &I) {
5421 return Reader.ReadSourceLocation(F, R, I);
5422 }
5423
5424 template<typename T>
5425 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
5426 return Reader.ReadDeclAs<T>(F, Record, Idx);
5427 }
5428
5429public:
5430 TypeLocReader(ASTReader &Reader, ModuleFile &F,
5431 const ASTReader::RecordData &Record, unsigned &Idx)
5432 : Reader(Reader), F(F), Record(Record), Idx(Idx)
5433 { }
5434
5435 // We want compile-time assurance that we've enumerated all of
5436 // these, so unfortunately we have to declare them first, then
5437 // define them out-of-line.
5438#define ABSTRACT_TYPELOC(CLASS, PARENT)
5439#define TYPELOC(CLASS, PARENT) \
5440 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
5441#include "clang/AST/TypeLocNodes.def"
5442
5443 void VisitFunctionTypeLoc(FunctionTypeLoc);
5444 void VisitArrayTypeLoc(ArrayTypeLoc);
5445};
5446
5447void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
5448 // nothing to do
5449}
5450void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
5451 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
5452 if (TL.needsExtraLocalData()) {
5453 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
5454 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
5455 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
5456 TL.setModeAttr(Record[Idx++]);
5457 }
5458}
5459void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
5460 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5461}
5462void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
5463 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5464}
Reid Kleckner8a365022013-06-24 17:51:48 +00005465void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
5466 // nothing to do
5467}
Reid Kleckner0503a872013-12-05 01:23:43 +00005468void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
5469 // nothing to do
5470}
Guy Benyei11169dd2012-12-18 14:30:41 +00005471void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
5472 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
5473}
5474void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
5475 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
5476}
5477void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
5478 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
5479}
5480void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
5481 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5482 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5483}
5484void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
5485 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
5486 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
5487 if (Record[Idx++])
5488 TL.setSizeExpr(Reader.ReadExpr(F));
5489 else
Craig Toppera13603a2014-05-22 05:54:18 +00005490 TL.setSizeExpr(nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00005491}
5492void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
5493 VisitArrayTypeLoc(TL);
5494}
5495void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
5496 VisitArrayTypeLoc(TL);
5497}
5498void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
5499 VisitArrayTypeLoc(TL);
5500}
5501void TypeLocReader::VisitDependentSizedArrayTypeLoc(
5502 DependentSizedArrayTypeLoc TL) {
5503 VisitArrayTypeLoc(TL);
5504}
5505void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
5506 DependentSizedExtVectorTypeLoc TL) {
5507 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5508}
5509void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
5510 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5511}
5512void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
5513 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5514}
5515void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
5516 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
5517 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5518 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5519 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
Alp Tokerb3fd5cf2014-01-21 00:32:38 +00005520 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
5521 TL.setParam(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00005522 }
5523}
5524void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
5525 VisitFunctionTypeLoc(TL);
5526}
5527void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
5528 VisitFunctionTypeLoc(TL);
5529}
5530void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
5531 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5532}
5533void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
5534 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5535}
5536void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
5537 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
5538 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5539 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5540}
5541void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
5542 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
5543 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5544 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5545 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5546}
5547void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
5548 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5549}
5550void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
5551 TL.setKWLoc(ReadSourceLocation(Record, Idx));
5552 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5553 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5554 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5555}
5556void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
5557 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5558}
5559void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
5560 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5561}
5562void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
5563 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5564}
5565void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
5566 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
5567 if (TL.hasAttrOperand()) {
5568 SourceRange range;
5569 range.setBegin(ReadSourceLocation(Record, Idx));
5570 range.setEnd(ReadSourceLocation(Record, Idx));
5571 TL.setAttrOperandParensRange(range);
5572 }
5573 if (TL.hasAttrExprOperand()) {
5574 if (Record[Idx++])
5575 TL.setAttrExprOperand(Reader.ReadExpr(F));
5576 else
Craig Toppera13603a2014-05-22 05:54:18 +00005577 TL.setAttrExprOperand(nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00005578 } else if (TL.hasAttrEnumOperand())
5579 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
5580}
5581void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
5582 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5583}
5584void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
5585 SubstTemplateTypeParmTypeLoc TL) {
5586 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5587}
5588void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
5589 SubstTemplateTypeParmPackTypeLoc TL) {
5590 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5591}
5592void TypeLocReader::VisitTemplateSpecializationTypeLoc(
5593 TemplateSpecializationTypeLoc TL) {
5594 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
5595 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
5596 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5597 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5598 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
5599 TL.setArgLocInfo(i,
5600 Reader.GetTemplateArgumentLocInfo(F,
5601 TL.getTypePtr()->getArg(i).getKind(),
5602 Record, Idx));
5603}
5604void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
5605 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5606 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5607}
5608void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
5609 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5610 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5611}
5612void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
5613 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5614}
5615void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
5616 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5617 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5618 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5619}
5620void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
5621 DependentTemplateSpecializationTypeLoc TL) {
5622 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5623 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5624 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
5625 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
5626 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5627 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5628 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
5629 TL.setArgLocInfo(I,
5630 Reader.GetTemplateArgumentLocInfo(F,
5631 TL.getTypePtr()->getArg(I).getKind(),
5632 Record, Idx));
5633}
5634void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
5635 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
5636}
5637void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
5638 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5639}
5640void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
5641 TL.setHasBaseTypeAsWritten(Record[Idx++]);
5642 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5643 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5644 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
5645 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
5646}
5647void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
5648 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5649}
5650void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
5651 TL.setKWLoc(ReadSourceLocation(Record, Idx));
5652 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5653 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5654}
5655
5656TypeSourceInfo *ASTReader::GetTypeSourceInfo(ModuleFile &F,
5657 const RecordData &Record,
5658 unsigned &Idx) {
5659 QualType InfoTy = readType(F, Record, Idx);
5660 if (InfoTy.isNull())
Craig Toppera13603a2014-05-22 05:54:18 +00005661 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00005662
5663 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
5664 TypeLocReader TLR(*this, F, Record, Idx);
5665 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
5666 TLR.Visit(TL);
5667 return TInfo;
5668}
5669
5670QualType ASTReader::GetType(TypeID ID) {
5671 unsigned FastQuals = ID & Qualifiers::FastMask;
5672 unsigned Index = ID >> Qualifiers::FastWidth;
5673
5674 if (Index < NUM_PREDEF_TYPE_IDS) {
5675 QualType T;
5676 switch ((PredefinedTypeIDs)Index) {
5677 case PREDEF_TYPE_NULL_ID: return QualType();
5678 case PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
5679 case PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
5680
5681 case PREDEF_TYPE_CHAR_U_ID:
5682 case PREDEF_TYPE_CHAR_S_ID:
5683 // FIXME: Check that the signedness of CharTy is correct!
5684 T = Context.CharTy;
5685 break;
5686
5687 case PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
5688 case PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
5689 case PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
5690 case PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
5691 case PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
5692 case PREDEF_TYPE_UINT128_ID: T = Context.UnsignedInt128Ty; break;
5693 case PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
5694 case PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
5695 case PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
5696 case PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
5697 case PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
5698 case PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
5699 case PREDEF_TYPE_INT128_ID: T = Context.Int128Ty; break;
5700 case PREDEF_TYPE_HALF_ID: T = Context.HalfTy; break;
5701 case PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
5702 case PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
5703 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
5704 case PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
5705 case PREDEF_TYPE_BOUND_MEMBER: T = Context.BoundMemberTy; break;
5706 case PREDEF_TYPE_PSEUDO_OBJECT: T = Context.PseudoObjectTy; break;
5707 case PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
5708 case PREDEF_TYPE_UNKNOWN_ANY: T = Context.UnknownAnyTy; break;
5709 case PREDEF_TYPE_NULLPTR_ID: T = Context.NullPtrTy; break;
5710 case PREDEF_TYPE_CHAR16_ID: T = Context.Char16Ty; break;
5711 case PREDEF_TYPE_CHAR32_ID: T = Context.Char32Ty; break;
5712 case PREDEF_TYPE_OBJC_ID: T = Context.ObjCBuiltinIdTy; break;
5713 case PREDEF_TYPE_OBJC_CLASS: T = Context.ObjCBuiltinClassTy; break;
5714 case PREDEF_TYPE_OBJC_SEL: T = Context.ObjCBuiltinSelTy; break;
Guy Benyeid8a08ea2012-12-18 14:38:23 +00005715 case PREDEF_TYPE_IMAGE1D_ID: T = Context.OCLImage1dTy; break;
5716 case PREDEF_TYPE_IMAGE1D_ARR_ID: T = Context.OCLImage1dArrayTy; break;
5717 case PREDEF_TYPE_IMAGE1D_BUFF_ID: T = Context.OCLImage1dBufferTy; break;
5718 case PREDEF_TYPE_IMAGE2D_ID: T = Context.OCLImage2dTy; break;
5719 case PREDEF_TYPE_IMAGE2D_ARR_ID: T = Context.OCLImage2dArrayTy; break;
5720 case PREDEF_TYPE_IMAGE3D_ID: T = Context.OCLImage3dTy; break;
Guy Benyei61054192013-02-07 10:55:47 +00005721 case PREDEF_TYPE_SAMPLER_ID: T = Context.OCLSamplerTy; break;
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00005722 case PREDEF_TYPE_EVENT_ID: T = Context.OCLEventTy; break;
Guy Benyei11169dd2012-12-18 14:30:41 +00005723 case PREDEF_TYPE_AUTO_DEDUCT: T = Context.getAutoDeductType(); break;
5724
5725 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
5726 T = Context.getAutoRRefDeductType();
5727 break;
5728
5729 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
5730 T = Context.ARCUnbridgedCastTy;
5731 break;
5732
5733 case PREDEF_TYPE_VA_LIST_TAG:
5734 T = Context.getVaListTagType();
5735 break;
5736
5737 case PREDEF_TYPE_BUILTIN_FN:
5738 T = Context.BuiltinFnTy;
5739 break;
5740 }
5741
5742 assert(!T.isNull() && "Unknown predefined type");
5743 return T.withFastQualifiers(FastQuals);
5744 }
5745
5746 Index -= NUM_PREDEF_TYPE_IDS;
5747 assert(Index < TypesLoaded.size() && "Type index out-of-range");
5748 if (TypesLoaded[Index].isNull()) {
5749 TypesLoaded[Index] = readTypeRecord(Index);
5750 if (TypesLoaded[Index].isNull())
5751 return QualType();
5752
5753 TypesLoaded[Index]->setFromAST();
5754 if (DeserializationListener)
5755 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
5756 TypesLoaded[Index]);
5757 }
5758
5759 return TypesLoaded[Index].withFastQualifiers(FastQuals);
5760}
5761
5762QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
5763 return GetType(getGlobalTypeID(F, LocalID));
5764}
5765
5766serialization::TypeID
5767ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
5768 unsigned FastQuals = LocalID & Qualifiers::FastMask;
5769 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
5770
5771 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
5772 return LocalID;
5773
5774 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5775 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
5776 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
5777
5778 unsigned GlobalIndex = LocalIndex + I->second;
5779 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
5780}
5781
5782TemplateArgumentLocInfo
5783ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
5784 TemplateArgument::ArgKind Kind,
5785 const RecordData &Record,
5786 unsigned &Index) {
5787 switch (Kind) {
5788 case TemplateArgument::Expression:
5789 return ReadExpr(F);
5790 case TemplateArgument::Type:
5791 return GetTypeSourceInfo(F, Record, Index);
5792 case TemplateArgument::Template: {
5793 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
5794 Index);
5795 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
5796 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
5797 SourceLocation());
5798 }
5799 case TemplateArgument::TemplateExpansion: {
5800 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
5801 Index);
5802 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
5803 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
5804 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
5805 EllipsisLoc);
5806 }
5807 case TemplateArgument::Null:
5808 case TemplateArgument::Integral:
5809 case TemplateArgument::Declaration:
5810 case TemplateArgument::NullPtr:
5811 case TemplateArgument::Pack:
5812 // FIXME: Is this right?
5813 return TemplateArgumentLocInfo();
5814 }
5815 llvm_unreachable("unexpected template argument loc");
5816}
5817
5818TemplateArgumentLoc
5819ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
5820 const RecordData &Record, unsigned &Index) {
5821 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
5822
5823 if (Arg.getKind() == TemplateArgument::Expression) {
5824 if (Record[Index++]) // bool InfoHasSameExpr.
5825 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
5826 }
5827 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
5828 Record, Index));
5829}
5830
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005831const ASTTemplateArgumentListInfo*
5832ASTReader::ReadASTTemplateArgumentListInfo(ModuleFile &F,
5833 const RecordData &Record,
5834 unsigned &Index) {
5835 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Index);
5836 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Index);
5837 unsigned NumArgsAsWritten = Record[Index++];
5838 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
5839 for (unsigned i = 0; i != NumArgsAsWritten; ++i)
5840 TemplArgsInfo.addArgument(ReadTemplateArgumentLoc(F, Record, Index));
5841 return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo);
5842}
5843
Guy Benyei11169dd2012-12-18 14:30:41 +00005844Decl *ASTReader::GetExternalDecl(uint32_t ID) {
5845 return GetDecl(ID);
5846}
5847
Richard Smith50895422015-01-31 03:04:55 +00005848template<typename TemplateSpecializationDecl>
5849static void completeRedeclChainForTemplateSpecialization(Decl *D) {
5850 if (auto *TSD = dyn_cast<TemplateSpecializationDecl>(D))
5851 TSD->getSpecializedTemplate()->LoadLazySpecializations();
5852}
5853
Richard Smith053f6c62014-05-16 23:01:30 +00005854void ASTReader::CompleteRedeclChain(const Decl *D) {
Richard Smith851072e2014-05-19 20:59:20 +00005855 if (NumCurrentElementsDeserializing) {
5856 // We arrange to not care about the complete redeclaration chain while we're
5857 // deserializing. Just remember that the AST has marked this one as complete
5858 // but that it's not actually complete yet, so we know we still need to
5859 // complete it later.
5860 PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D));
5861 return;
5862 }
5863
Richard Smith053f6c62014-05-16 23:01:30 +00005864 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
5865
Richard Smith053f6c62014-05-16 23:01:30 +00005866 // If this is a named declaration, complete it by looking it up
5867 // within its context.
5868 //
Richard Smith01bdb7a2014-08-28 05:44:07 +00005869 // FIXME: Merging a function definition should merge
Richard Smith053f6c62014-05-16 23:01:30 +00005870 // all mergeable entities within it.
5871 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) ||
5872 isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) {
5873 if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) {
5874 auto *II = Name.getAsIdentifierInfo();
5875 if (isa<TranslationUnitDecl>(DC) && II) {
5876 // Outside of C++, we don't have a lookup table for the TU, so update
5877 // the identifier instead. In C++, either way should work fine.
5878 if (II->isOutOfDate())
5879 updateOutOfDateIdentifier(*II);
5880 } else
5881 DC->lookup(Name);
Richard Smith01bdb7a2014-08-28 05:44:07 +00005882 } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) {
5883 // FIXME: It'd be nice to do something a bit more targeted here.
5884 D->getDeclContext()->decls_begin();
Richard Smith053f6c62014-05-16 23:01:30 +00005885 }
5886 }
Richard Smith50895422015-01-31 03:04:55 +00005887
5888 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D))
5889 CTSD->getSpecializedTemplate()->LoadLazySpecializations();
5890 if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D))
5891 VTSD->getSpecializedTemplate()->LoadLazySpecializations();
5892 if (auto *FD = dyn_cast<FunctionDecl>(D)) {
5893 if (auto *Template = FD->getPrimaryTemplate())
5894 Template->LoadLazySpecializations();
5895 }
Richard Smith053f6c62014-05-16 23:01:30 +00005896}
5897
Richard Smithc2bb8182015-03-24 06:36:48 +00005898uint64_t ASTReader::ReadCXXCtorInitializersRef(ModuleFile &M,
5899 const RecordData &Record,
5900 unsigned &Idx) {
5901 if (Idx >= Record.size() || Record[Idx] > M.LocalNumCXXCtorInitializers) {
5902 Error("malformed AST file: missing C++ ctor initializers");
5903 return 0;
5904 }
5905
5906 unsigned LocalID = Record[Idx++];
5907 return getGlobalBitOffset(M, M.CXXCtorInitializersOffsets[LocalID - 1]);
5908}
5909
5910CXXCtorInitializer **
5911ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
5912 RecordLocation Loc = getLocalBitOffset(Offset);
5913 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
5914 SavedStreamPosition SavedPosition(Cursor);
5915 Cursor.JumpToBit(Loc.Offset);
5916 ReadingKindTracker ReadingKind(Read_Decl, *this);
5917
5918 RecordData Record;
5919 unsigned Code = Cursor.ReadCode();
5920 unsigned RecCode = Cursor.readRecord(Code, Record);
5921 if (RecCode != DECL_CXX_CTOR_INITIALIZERS) {
5922 Error("malformed AST file: missing C++ ctor initializers");
5923 return nullptr;
5924 }
5925
5926 unsigned Idx = 0;
5927 return ReadCXXCtorInitializers(*Loc.F, Record, Idx);
5928}
5929
Richard Smithcd45dbc2014-04-19 03:48:30 +00005930uint64_t ASTReader::readCXXBaseSpecifiers(ModuleFile &M,
5931 const RecordData &Record,
5932 unsigned &Idx) {
5933 if (Idx >= Record.size() || Record[Idx] > M.LocalNumCXXBaseSpecifiers) {
5934 Error("malformed AST file: missing C++ base specifier");
Guy Benyei11169dd2012-12-18 14:30:41 +00005935 return 0;
Richard Smithcd45dbc2014-04-19 03:48:30 +00005936 }
5937
Guy Benyei11169dd2012-12-18 14:30:41 +00005938 unsigned LocalID = Record[Idx++];
5939 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
5940}
5941
5942CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
5943 RecordLocation Loc = getLocalBitOffset(Offset);
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005944 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00005945 SavedStreamPosition SavedPosition(Cursor);
5946 Cursor.JumpToBit(Loc.Offset);
5947 ReadingKindTracker ReadingKind(Read_Decl, *this);
5948 RecordData Record;
5949 unsigned Code = Cursor.ReadCode();
Chris Lattner0e6c9402013-01-20 02:38:54 +00005950 unsigned RecCode = Cursor.readRecord(Code, Record);
Guy Benyei11169dd2012-12-18 14:30:41 +00005951 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00005952 Error("malformed AST file: missing C++ base specifiers");
Craig Toppera13603a2014-05-22 05:54:18 +00005953 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00005954 }
5955
5956 unsigned Idx = 0;
5957 unsigned NumBases = Record[Idx++];
5958 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
5959 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
5960 for (unsigned I = 0; I != NumBases; ++I)
5961 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
5962 return Bases;
5963}
5964
5965serialization::DeclID
5966ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
5967 if (LocalID < NUM_PREDEF_DECL_IDS)
5968 return LocalID;
5969
5970 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5971 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
5972 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
5973
5974 return LocalID + I->second;
5975}
5976
5977bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
5978 ModuleFile &M) const {
Richard Smithfe620d22015-03-05 23:24:12 +00005979 // Predefined decls aren't from any module.
5980 if (ID < NUM_PREDEF_DECL_IDS)
5981 return false;
5982
Guy Benyei11169dd2012-12-18 14:30:41 +00005983 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(ID);
5984 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5985 return &M == I->second;
5986}
5987
Douglas Gregor9f782892013-01-21 15:25:38 +00005988ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
Guy Benyei11169dd2012-12-18 14:30:41 +00005989 if (!D->isFromASTFile())
Craig Toppera13603a2014-05-22 05:54:18 +00005990 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00005991 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
5992 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5993 return I->second;
5994}
5995
5996SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
5997 if (ID < NUM_PREDEF_DECL_IDS)
5998 return SourceLocation();
Richard Smithcd45dbc2014-04-19 03:48:30 +00005999
Guy Benyei11169dd2012-12-18 14:30:41 +00006000 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
6001
6002 if (Index > DeclsLoaded.size()) {
6003 Error("declaration ID out-of-range for AST file");
6004 return SourceLocation();
6005 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00006006
Guy Benyei11169dd2012-12-18 14:30:41 +00006007 if (Decl *D = DeclsLoaded[Index])
6008 return D->getLocation();
6009
6010 unsigned RawLocation = 0;
6011 RecordLocation Rec = DeclCursorForID(ID, RawLocation);
6012 return ReadSourceLocation(*Rec.F, RawLocation);
6013}
6014
Richard Smithfe620d22015-03-05 23:24:12 +00006015static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) {
6016 switch (ID) {
6017 case PREDEF_DECL_NULL_ID:
6018 return nullptr;
6019
6020 case PREDEF_DECL_TRANSLATION_UNIT_ID:
6021 return Context.getTranslationUnitDecl();
6022
6023 case PREDEF_DECL_OBJC_ID_ID:
6024 return Context.getObjCIdDecl();
6025
6026 case PREDEF_DECL_OBJC_SEL_ID:
6027 return Context.getObjCSelDecl();
6028
6029 case PREDEF_DECL_OBJC_CLASS_ID:
6030 return Context.getObjCClassDecl();
6031
6032 case PREDEF_DECL_OBJC_PROTOCOL_ID:
6033 return Context.getObjCProtocolDecl();
6034
6035 case PREDEF_DECL_INT_128_ID:
6036 return Context.getInt128Decl();
6037
6038 case PREDEF_DECL_UNSIGNED_INT_128_ID:
6039 return Context.getUInt128Decl();
6040
6041 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
6042 return Context.getObjCInstanceTypeDecl();
6043
6044 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
6045 return Context.getBuiltinVaListDecl();
Richard Smithf19e1272015-03-07 00:04:49 +00006046
6047 case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
6048 return Context.getExternCContextDecl();
Richard Smithfe620d22015-03-05 23:24:12 +00006049 }
Yaron Keren322bdad2015-03-06 07:49:14 +00006050 llvm_unreachable("PredefinedDeclIDs unknown enum value");
Richard Smithfe620d22015-03-05 23:24:12 +00006051}
6052
Richard Smithcd45dbc2014-04-19 03:48:30 +00006053Decl *ASTReader::GetExistingDecl(DeclID ID) {
6054 if (ID < NUM_PREDEF_DECL_IDS) {
Richard Smithfe620d22015-03-05 23:24:12 +00006055 Decl *D = getPredefinedDecl(Context, (PredefinedDeclIDs)ID);
6056 if (D) {
6057 // Track that we have merged the declaration with ID \p ID into the
6058 // pre-existing predefined declaration \p D.
6059 auto &Merged = MergedDecls[D->getCanonicalDecl()];
6060 if (Merged.empty())
6061 Merged.push_back(ID);
Guy Benyei11169dd2012-12-18 14:30:41 +00006062 }
Richard Smithfe620d22015-03-05 23:24:12 +00006063 return D;
Guy Benyei11169dd2012-12-18 14:30:41 +00006064 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00006065
Guy Benyei11169dd2012-12-18 14:30:41 +00006066 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
6067
6068 if (Index >= DeclsLoaded.size()) {
6069 assert(0 && "declaration ID out-of-range for AST file");
6070 Error("declaration ID out-of-range for AST file");
Craig Toppera13603a2014-05-22 05:54:18 +00006071 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00006072 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00006073
6074 return DeclsLoaded[Index];
6075}
6076
6077Decl *ASTReader::GetDecl(DeclID ID) {
6078 if (ID < NUM_PREDEF_DECL_IDS)
6079 return GetExistingDecl(ID);
6080
6081 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
6082
6083 if (Index >= DeclsLoaded.size()) {
6084 assert(0 && "declaration ID out-of-range for AST file");
6085 Error("declaration ID out-of-range for AST file");
Craig Toppera13603a2014-05-22 05:54:18 +00006086 return nullptr;
Richard Smithcd45dbc2014-04-19 03:48:30 +00006087 }
6088
Guy Benyei11169dd2012-12-18 14:30:41 +00006089 if (!DeclsLoaded[Index]) {
6090 ReadDeclRecord(ID);
6091 if (DeserializationListener)
6092 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
6093 }
6094
6095 return DeclsLoaded[Index];
6096}
6097
6098DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
6099 DeclID GlobalID) {
6100 if (GlobalID < NUM_PREDEF_DECL_IDS)
6101 return GlobalID;
6102
6103 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
6104 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
6105 ModuleFile *Owner = I->second;
6106
6107 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
6108 = M.GlobalToLocalDeclIDs.find(Owner);
6109 if (Pos == M.GlobalToLocalDeclIDs.end())
6110 return 0;
6111
6112 return GlobalID - Owner->BaseDeclID + Pos->second;
6113}
6114
6115serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
6116 const RecordData &Record,
6117 unsigned &Idx) {
6118 if (Idx >= Record.size()) {
6119 Error("Corrupted AST file");
6120 return 0;
6121 }
6122
6123 return getGlobalDeclID(F, Record[Idx++]);
6124}
6125
6126/// \brief Resolve the offset of a statement into a statement.
6127///
6128/// This operation will read a new statement from the external
6129/// source each time it is called, and is meant to be used via a
6130/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
6131Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
6132 // Switch case IDs are per Decl.
6133 ClearSwitchCaseIDs();
6134
6135 // Offset here is a global offset across the entire chain.
6136 RecordLocation Loc = getLocalBitOffset(Offset);
6137 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
6138 return ReadStmtFromStream(*Loc.F);
6139}
6140
6141namespace {
6142 class FindExternalLexicalDeclsVisitor {
6143 ASTReader &Reader;
6144 const DeclContext *DC;
6145 bool (*isKindWeWant)(Decl::Kind);
6146
6147 SmallVectorImpl<Decl*> &Decls;
6148 bool PredefsVisited[NUM_PREDEF_DECL_IDS];
6149
6150 public:
6151 FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC,
6152 bool (*isKindWeWant)(Decl::Kind),
6153 SmallVectorImpl<Decl*> &Decls)
6154 : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls)
6155 {
6156 for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I)
6157 PredefsVisited[I] = false;
6158 }
6159
Manuel Klimek9eff8b12015-05-20 10:29:23 +00006160 static bool visitPostorder(ModuleFile &M, void *UserData) {
Guy Benyei11169dd2012-12-18 14:30:41 +00006161 FindExternalLexicalDeclsVisitor *This
6162 = static_cast<FindExternalLexicalDeclsVisitor *>(UserData);
6163
6164 ModuleFile::DeclContextInfosMap::iterator Info
6165 = M.DeclContextInfos.find(This->DC);
6166 if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls)
6167 return false;
6168
6169 // Load all of the declaration IDs
6170 for (const KindDeclIDPair *ID = Info->second.LexicalDecls,
6171 *IDE = ID + Info->second.NumLexicalDecls;
6172 ID != IDE; ++ID) {
6173 if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first))
6174 continue;
6175
6176 // Don't add predefined declarations to the lexical context more
6177 // than once.
6178 if (ID->second < NUM_PREDEF_DECL_IDS) {
6179 if (This->PredefsVisited[ID->second])
6180 continue;
6181
6182 This->PredefsVisited[ID->second] = true;
6183 }
6184
6185 if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) {
6186 if (!This->DC->isDeclInLexicalTraversal(D))
6187 This->Decls.push_back(D);
6188 }
6189 }
6190
6191 return false;
6192 }
6193 };
6194}
6195
6196ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
6197 bool (*isKindWeWant)(Decl::Kind),
6198 SmallVectorImpl<Decl*> &Decls) {
6199 // There might be lexical decls in multiple modules, for the TU at
6200 // least. Walk all of the modules in the order they were loaded.
6201 FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls);
Manuel Klimek9eff8b12015-05-20 10:29:23 +00006202 ModuleMgr.visitDepthFirst(
6203 nullptr, &FindExternalLexicalDeclsVisitor::visitPostorder, &Visitor);
Guy Benyei11169dd2012-12-18 14:30:41 +00006204 ++NumLexicalDeclContextsRead;
6205 return ELR_Success;
6206}
6207
6208namespace {
6209
6210class DeclIDComp {
6211 ASTReader &Reader;
6212 ModuleFile &Mod;
6213
6214public:
6215 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
6216
6217 bool operator()(LocalDeclID L, LocalDeclID R) const {
6218 SourceLocation LHS = getLocation(L);
6219 SourceLocation RHS = getLocation(R);
6220 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6221 }
6222
6223 bool operator()(SourceLocation LHS, LocalDeclID R) const {
6224 SourceLocation RHS = getLocation(R);
6225 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6226 }
6227
6228 bool operator()(LocalDeclID L, SourceLocation RHS) const {
6229 SourceLocation LHS = getLocation(L);
6230 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6231 }
6232
6233 SourceLocation getLocation(LocalDeclID ID) const {
6234 return Reader.getSourceManager().getFileLoc(
6235 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
6236 }
6237};
6238
6239}
6240
6241void ASTReader::FindFileRegionDecls(FileID File,
6242 unsigned Offset, unsigned Length,
6243 SmallVectorImpl<Decl *> &Decls) {
6244 SourceManager &SM = getSourceManager();
6245
6246 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
6247 if (I == FileDeclIDs.end())
6248 return;
6249
6250 FileDeclsInfo &DInfo = I->second;
6251 if (DInfo.Decls.empty())
6252 return;
6253
6254 SourceLocation
6255 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
6256 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
6257
6258 DeclIDComp DIDComp(*this, *DInfo.Mod);
6259 ArrayRef<serialization::LocalDeclID>::iterator
6260 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
6261 BeginLoc, DIDComp);
6262 if (BeginIt != DInfo.Decls.begin())
6263 --BeginIt;
6264
6265 // If we are pointing at a top-level decl inside an objc container, we need
6266 // to backtrack until we find it otherwise we will fail to report that the
6267 // region overlaps with an objc container.
6268 while (BeginIt != DInfo.Decls.begin() &&
6269 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
6270 ->isTopLevelDeclInObjCContainer())
6271 --BeginIt;
6272
6273 ArrayRef<serialization::LocalDeclID>::iterator
6274 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
6275 EndLoc, DIDComp);
6276 if (EndIt != DInfo.Decls.end())
6277 ++EndIt;
6278
6279 for (ArrayRef<serialization::LocalDeclID>::iterator
6280 DIt = BeginIt; DIt != EndIt; ++DIt)
6281 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
6282}
6283
6284namespace {
6285 /// \brief ModuleFile visitor used to perform name lookup into a
6286 /// declaration context.
6287 class DeclContextNameLookupVisitor {
6288 ASTReader &Reader;
Richard Smith8c913ec2014-08-14 02:21:01 +00006289 ArrayRef<const DeclContext *> Contexts;
Guy Benyei11169dd2012-12-18 14:30:41 +00006290 DeclarationName Name;
6291 SmallVectorImpl<NamedDecl *> &Decls;
Richard Smith52874ec2015-02-13 20:17:14 +00006292 llvm::SmallPtrSetImpl<NamedDecl *> &DeclSet;
Guy Benyei11169dd2012-12-18 14:30:41 +00006293
6294 public:
Richard Smith8c913ec2014-08-14 02:21:01 +00006295 DeclContextNameLookupVisitor(ASTReader &Reader,
6296 ArrayRef<const DeclContext *> Contexts,
Guy Benyei11169dd2012-12-18 14:30:41 +00006297 DeclarationName Name,
Richard Smith52874ec2015-02-13 20:17:14 +00006298 SmallVectorImpl<NamedDecl *> &Decls,
6299 llvm::SmallPtrSetImpl<NamedDecl *> &DeclSet)
6300 : Reader(Reader), Contexts(Contexts), Name(Name), Decls(Decls),
6301 DeclSet(DeclSet) { }
Guy Benyei11169dd2012-12-18 14:30:41 +00006302
6303 static bool visit(ModuleFile &M, void *UserData) {
6304 DeclContextNameLookupVisitor *This
6305 = static_cast<DeclContextNameLookupVisitor *>(UserData);
6306
6307 // Check whether we have any visible declaration information for
6308 // this context in this module.
6309 ModuleFile::DeclContextInfosMap::iterator Info;
6310 bool FoundInfo = false;
Richard Smith8c913ec2014-08-14 02:21:01 +00006311 for (auto *DC : This->Contexts) {
6312 Info = M.DeclContextInfos.find(DC);
6313 if (Info != M.DeclContextInfos.end() &&
Guy Benyei11169dd2012-12-18 14:30:41 +00006314 Info->second.NameLookupTableData) {
6315 FoundInfo = true;
6316 break;
6317 }
6318 }
6319
6320 if (!FoundInfo)
6321 return false;
Richard Smith8c913ec2014-08-14 02:21:01 +00006322
Guy Benyei11169dd2012-12-18 14:30:41 +00006323 // Look for this name within this module.
Richard Smith52e3fba2014-03-11 07:17:35 +00006324 ASTDeclContextNameLookupTable *LookupTable =
Guy Benyei11169dd2012-12-18 14:30:41 +00006325 Info->second.NameLookupTableData;
6326 ASTDeclContextNameLookupTable::iterator Pos
6327 = LookupTable->find(This->Name);
6328 if (Pos == LookupTable->end())
6329 return false;
6330
6331 bool FoundAnything = false;
6332 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
6333 for (; Data.first != Data.second; ++Data.first) {
6334 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first);
6335 if (!ND)
6336 continue;
6337
6338 if (ND->getDeclName() != This->Name) {
6339 // A name might be null because the decl's redeclarable part is
6340 // currently read before reading its name. The lookup is triggered by
6341 // building that decl (likely indirectly), and so it is later in the
6342 // sense of "already existing" and can be ignored here.
Richard Smith8c913ec2014-08-14 02:21:01 +00006343 // FIXME: This should not happen; deserializing declarations should
6344 // not perform lookups since that can lead to deserialization cycles.
Guy Benyei11169dd2012-12-18 14:30:41 +00006345 continue;
6346 }
Richard Smith8c913ec2014-08-14 02:21:01 +00006347
Guy Benyei11169dd2012-12-18 14:30:41 +00006348 // Record this declaration.
6349 FoundAnything = true;
Richard Smith52874ec2015-02-13 20:17:14 +00006350 if (This->DeclSet.insert(ND).second)
6351 This->Decls.push_back(ND);
Guy Benyei11169dd2012-12-18 14:30:41 +00006352 }
6353
6354 return FoundAnything;
6355 }
6356 };
6357}
6358
Douglas Gregor9f782892013-01-21 15:25:38 +00006359/// \brief Retrieve the "definitive" module file for the definition of the
6360/// given declaration context, if there is one.
6361///
6362/// The "definitive" module file is the only place where we need to look to
6363/// find information about the declarations within the given declaration
6364/// context. For example, C++ and Objective-C classes, C structs/unions, and
6365/// Objective-C protocols, categories, and extensions are all defined in a
6366/// single place in the source code, so they have definitive module files
6367/// associated with them. C++ namespaces, on the other hand, can have
6368/// definitions in multiple different module files.
6369///
6370/// Note: this needs to be kept in sync with ASTWriter::AddedVisibleDecl's
6371/// NDEBUG checking.
6372static ModuleFile *getDefinitiveModuleFileFor(const DeclContext *DC,
6373 ASTReader &Reader) {
Douglas Gregor7a6e2002013-01-22 17:08:30 +00006374 if (const DeclContext *DefDC = getDefinitiveDeclContext(DC))
6375 return Reader.getOwningModuleFile(cast<Decl>(DefDC));
Douglas Gregor9f782892013-01-21 15:25:38 +00006376
Craig Toppera13603a2014-05-22 05:54:18 +00006377 return nullptr;
Douglas Gregor9f782892013-01-21 15:25:38 +00006378}
6379
Richard Smith9ce12e32013-02-07 03:30:24 +00006380bool
Guy Benyei11169dd2012-12-18 14:30:41 +00006381ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
6382 DeclarationName Name) {
6383 assert(DC->hasExternalVisibleStorage() &&
6384 "DeclContext has no visible decls in storage");
6385 if (!Name)
Richard Smith9ce12e32013-02-07 03:30:24 +00006386 return false;
Guy Benyei11169dd2012-12-18 14:30:41 +00006387
Richard Smith8c913ec2014-08-14 02:21:01 +00006388 Deserializing LookupResults(this);
6389
Guy Benyei11169dd2012-12-18 14:30:41 +00006390 SmallVector<NamedDecl *, 64> Decls;
Richard Smith52874ec2015-02-13 20:17:14 +00006391 llvm::SmallPtrSet<NamedDecl*, 64> DeclSet;
Richard Smith8c913ec2014-08-14 02:21:01 +00006392
Guy Benyei11169dd2012-12-18 14:30:41 +00006393 // Compute the declaration contexts we need to look into. Multiple such
6394 // declaration contexts occur when two declaration contexts from disjoint
6395 // modules get merged, e.g., when two namespaces with the same name are
6396 // independently defined in separate modules.
6397 SmallVector<const DeclContext *, 2> Contexts;
6398 Contexts.push_back(DC);
Richard Smith8c913ec2014-08-14 02:21:01 +00006399
Guy Benyei11169dd2012-12-18 14:30:41 +00006400 if (DC->isNamespace()) {
Richard Smithcd45dbc2014-04-19 03:48:30 +00006401 auto Merged = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
Guy Benyei11169dd2012-12-18 14:30:41 +00006402 if (Merged != MergedDecls.end()) {
6403 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
6404 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
6405 }
6406 }
Richard Smith8c913ec2014-08-14 02:21:01 +00006407
6408 auto LookUpInContexts = [&](ArrayRef<const DeclContext*> Contexts) {
Richard Smith52874ec2015-02-13 20:17:14 +00006409 DeclContextNameLookupVisitor Visitor(*this, Contexts, Name, Decls, DeclSet);
Richard Smith8c913ec2014-08-14 02:21:01 +00006410
6411 // If we can definitively determine which module file to look into,
6412 // only look there. Otherwise, look in all module files.
6413 ModuleFile *Definitive;
6414 if (Contexts.size() == 1 &&
6415 (Definitive = getDefinitiveModuleFileFor(Contexts[0], *this))) {
6416 DeclContextNameLookupVisitor::visit(*Definitive, &Visitor);
6417 } else {
6418 ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor);
6419 }
6420 };
6421
6422 LookUpInContexts(Contexts);
6423
6424 // If this might be an implicit special member function, then also search
6425 // all merged definitions of the surrounding class. We need to search them
6426 // individually, because finding an entity in one of them doesn't imply that
6427 // we can't find a different entity in another one.
Richard Smithcd45dbc2014-04-19 03:48:30 +00006428 if (isa<CXXRecordDecl>(DC)) {
Richard Smith02793752015-03-27 21:16:39 +00006429 auto Merged = MergedLookups.find(DC);
6430 if (Merged != MergedLookups.end()) {
6431 for (unsigned I = 0; I != Merged->second.size(); ++I) {
6432 const DeclContext *Context = Merged->second[I];
6433 LookUpInContexts(Context);
6434 // We might have just added some more merged lookups. If so, our
6435 // iterator is now invalid, so grab a fresh one before continuing.
6436 Merged = MergedLookups.find(DC);
Richard Smithe0612472014-11-21 05:16:13 +00006437 }
Richard Smith8c913ec2014-08-14 02:21:01 +00006438 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00006439 }
6440
Guy Benyei11169dd2012-12-18 14:30:41 +00006441 ++NumVisibleDeclContextsRead;
6442 SetExternalVisibleDeclsForName(DC, Name, Decls);
Richard Smith9ce12e32013-02-07 03:30:24 +00006443 return !Decls.empty();
Guy Benyei11169dd2012-12-18 14:30:41 +00006444}
6445
6446namespace {
6447 /// \brief ModuleFile visitor used to retrieve all visible names in a
6448 /// declaration context.
6449 class DeclContextAllNamesVisitor {
6450 ASTReader &Reader;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00006451 SmallVectorImpl<const DeclContext *> &Contexts;
Craig Topper3598eb72013-07-05 04:43:31 +00006452 DeclsMap &Decls;
Richard Smith52874ec2015-02-13 20:17:14 +00006453 llvm::SmallPtrSet<NamedDecl *, 256> DeclSet;
Argyrios Kyrtzidis2810e9d2012-12-19 22:21:18 +00006454 bool VisitAll;
Guy Benyei11169dd2012-12-18 14:30:41 +00006455
6456 public:
6457 DeclContextAllNamesVisitor(ASTReader &Reader,
6458 SmallVectorImpl<const DeclContext *> &Contexts,
Craig Topper3598eb72013-07-05 04:43:31 +00006459 DeclsMap &Decls, bool VisitAll)
Argyrios Kyrtzidis2810e9d2012-12-19 22:21:18 +00006460 : Reader(Reader), Contexts(Contexts), Decls(Decls), VisitAll(VisitAll) { }
Guy Benyei11169dd2012-12-18 14:30:41 +00006461
6462 static bool visit(ModuleFile &M, void *UserData) {
6463 DeclContextAllNamesVisitor *This
6464 = static_cast<DeclContextAllNamesVisitor *>(UserData);
6465
6466 // Check whether we have any visible declaration information for
6467 // this context in this module.
6468 ModuleFile::DeclContextInfosMap::iterator Info;
6469 bool FoundInfo = false;
6470 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
6471 Info = M.DeclContextInfos.find(This->Contexts[I]);
6472 if (Info != M.DeclContextInfos.end() &&
6473 Info->second.NameLookupTableData) {
6474 FoundInfo = true;
6475 break;
6476 }
6477 }
6478
6479 if (!FoundInfo)
6480 return false;
6481
Richard Smith52e3fba2014-03-11 07:17:35 +00006482 ASTDeclContextNameLookupTable *LookupTable =
Guy Benyei11169dd2012-12-18 14:30:41 +00006483 Info->second.NameLookupTableData;
6484 bool FoundAnything = false;
6485 for (ASTDeclContextNameLookupTable::data_iterator
Douglas Gregor5e306b12013-01-23 22:38:11 +00006486 I = LookupTable->data_begin(), E = LookupTable->data_end();
6487 I != E;
6488 ++I) {
Guy Benyei11169dd2012-12-18 14:30:41 +00006489 ASTDeclContextNameLookupTrait::data_type Data = *I;
6490 for (; Data.first != Data.second; ++Data.first) {
6491 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M,
6492 *Data.first);
6493 if (!ND)
6494 continue;
6495
6496 // Record this declaration.
6497 FoundAnything = true;
Richard Smith52874ec2015-02-13 20:17:14 +00006498 if (This->DeclSet.insert(ND).second)
6499 This->Decls[ND->getDeclName()].push_back(ND);
Guy Benyei11169dd2012-12-18 14:30:41 +00006500 }
6501 }
6502
Argyrios Kyrtzidis2810e9d2012-12-19 22:21:18 +00006503 return FoundAnything && !This->VisitAll;
Guy Benyei11169dd2012-12-18 14:30:41 +00006504 }
6505 };
6506}
6507
6508void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
6509 if (!DC->hasExternalVisibleStorage())
6510 return;
Craig Topper79be4cd2013-07-05 04:33:53 +00006511 DeclsMap Decls;
Guy Benyei11169dd2012-12-18 14:30:41 +00006512
6513 // Compute the declaration contexts we need to look into. Multiple such
6514 // declaration contexts occur when two declaration contexts from disjoint
6515 // modules get merged, e.g., when two namespaces with the same name are
6516 // independently defined in separate modules.
6517 SmallVector<const DeclContext *, 2> Contexts;
6518 Contexts.push_back(DC);
6519
6520 if (DC->isNamespace()) {
6521 MergedDeclsMap::iterator Merged
6522 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
6523 if (Merged != MergedDecls.end()) {
6524 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
6525 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
6526 }
6527 }
6528
Argyrios Kyrtzidis2810e9d2012-12-19 22:21:18 +00006529 DeclContextAllNamesVisitor Visitor(*this, Contexts, Decls,
6530 /*VisitAll=*/DC->isFileContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00006531 ModuleMgr.visit(&DeclContextAllNamesVisitor::visit, &Visitor);
6532 ++NumVisibleDeclContextsRead;
6533
Craig Topper79be4cd2013-07-05 04:33:53 +00006534 for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
Guy Benyei11169dd2012-12-18 14:30:41 +00006535 SetExternalVisibleDeclsForName(DC, I->first, I->second);
6536 }
6537 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
6538}
6539
6540/// \brief Under non-PCH compilation the consumer receives the objc methods
6541/// before receiving the implementation, and codegen depends on this.
6542/// We simulate this by deserializing and passing to consumer the methods of the
6543/// implementation before passing the deserialized implementation decl.
6544static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
6545 ASTConsumer *Consumer) {
6546 assert(ImplD && Consumer);
6547
Aaron Ballmanaff18c02014-03-13 19:03:34 +00006548 for (auto *I : ImplD->methods())
6549 Consumer->HandleInterestingDecl(DeclGroupRef(I));
Guy Benyei11169dd2012-12-18 14:30:41 +00006550
6551 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
6552}
6553
6554void ASTReader::PassInterestingDeclsToConsumer() {
6555 assert(Consumer);
Richard Smith04d05b52014-03-23 00:27:18 +00006556
6557 if (PassingDeclsToConsumer)
6558 return;
6559
6560 // Guard variable to avoid recursively redoing the process of passing
6561 // decls to consumer.
6562 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
6563 true);
6564
Richard Smith9e2341d2015-03-23 03:25:59 +00006565 // Ensure that we've loaded all potentially-interesting declarations
6566 // that need to be eagerly loaded.
6567 for (auto ID : EagerlyDeserializedDecls)
6568 GetDecl(ID);
6569 EagerlyDeserializedDecls.clear();
6570
Guy Benyei11169dd2012-12-18 14:30:41 +00006571 while (!InterestingDecls.empty()) {
6572 Decl *D = InterestingDecls.front();
6573 InterestingDecls.pop_front();
6574
6575 PassInterestingDeclToConsumer(D);
6576 }
6577}
6578
6579void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
6580 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
6581 PassObjCImplDeclToConsumer(ImplD, Consumer);
6582 else
6583 Consumer->HandleInterestingDecl(DeclGroupRef(D));
6584}
6585
6586void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
6587 this->Consumer = Consumer;
6588
Richard Smith9e2341d2015-03-23 03:25:59 +00006589 if (Consumer)
6590 PassInterestingDeclsToConsumer();
Richard Smith7f330cd2015-03-18 01:42:29 +00006591
6592 if (DeserializationListener)
6593 DeserializationListener->ReaderInitialized(this);
Guy Benyei11169dd2012-12-18 14:30:41 +00006594}
6595
6596void ASTReader::PrintStats() {
6597 std::fprintf(stderr, "*** AST File Statistics:\n");
6598
6599 unsigned NumTypesLoaded
6600 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
6601 QualType());
6602 unsigned NumDeclsLoaded
6603 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
Craig Toppera13603a2014-05-22 05:54:18 +00006604 (Decl *)nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00006605 unsigned NumIdentifiersLoaded
6606 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
6607 IdentifiersLoaded.end(),
Craig Toppera13603a2014-05-22 05:54:18 +00006608 (IdentifierInfo *)nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00006609 unsigned NumMacrosLoaded
6610 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
6611 MacrosLoaded.end(),
Craig Toppera13603a2014-05-22 05:54:18 +00006612 (MacroInfo *)nullptr);
Guy Benyei11169dd2012-12-18 14:30:41 +00006613 unsigned NumSelectorsLoaded
6614 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
6615 SelectorsLoaded.end(),
6616 Selector());
6617
6618 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
6619 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
6620 NumSLocEntriesRead, TotalNumSLocEntries,
6621 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
6622 if (!TypesLoaded.empty())
6623 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
6624 NumTypesLoaded, (unsigned)TypesLoaded.size(),
6625 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
6626 if (!DeclsLoaded.empty())
6627 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
6628 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
6629 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
6630 if (!IdentifiersLoaded.empty())
6631 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
6632 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
6633 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
6634 if (!MacrosLoaded.empty())
6635 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
6636 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
6637 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
6638 if (!SelectorsLoaded.empty())
6639 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
6640 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
6641 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
6642 if (TotalNumStatements)
6643 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
6644 NumStatementsRead, TotalNumStatements,
6645 ((float)NumStatementsRead/TotalNumStatements * 100));
6646 if (TotalNumMacros)
6647 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
6648 NumMacrosRead, TotalNumMacros,
6649 ((float)NumMacrosRead/TotalNumMacros * 100));
6650 if (TotalLexicalDeclContexts)
6651 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
6652 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
6653 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
6654 * 100));
6655 if (TotalVisibleDeclContexts)
6656 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
6657 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
6658 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
6659 * 100));
6660 if (TotalNumMethodPoolEntries) {
6661 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
6662 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
6663 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
6664 * 100));
Guy Benyei11169dd2012-12-18 14:30:41 +00006665 }
Douglas Gregorad2f7a52013-01-28 17:54:36 +00006666 if (NumMethodPoolLookups) {
6667 std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n",
6668 NumMethodPoolHits, NumMethodPoolLookups,
6669 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
6670 }
6671 if (NumMethodPoolTableLookups) {
6672 std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n",
6673 NumMethodPoolTableHits, NumMethodPoolTableLookups,
6674 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
6675 * 100.0));
6676 }
6677
Douglas Gregor00a50f72013-01-25 00:38:33 +00006678 if (NumIdentifierLookupHits) {
6679 std::fprintf(stderr,
6680 " %u / %u identifier table lookups succeeded (%f%%)\n",
6681 NumIdentifierLookupHits, NumIdentifierLookups,
6682 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
6683 }
6684
Douglas Gregore060e572013-01-25 01:03:03 +00006685 if (GlobalIndex) {
6686 std::fprintf(stderr, "\n");
6687 GlobalIndex->printStats();
6688 }
6689
Guy Benyei11169dd2012-12-18 14:30:41 +00006690 std::fprintf(stderr, "\n");
6691 dump();
6692 std::fprintf(stderr, "\n");
6693}
6694
6695template<typename Key, typename ModuleFile, unsigned InitialCapacity>
6696static void
6697dumpModuleIDMap(StringRef Name,
6698 const ContinuousRangeMap<Key, ModuleFile *,
6699 InitialCapacity> &Map) {
6700 if (Map.begin() == Map.end())
6701 return;
6702
6703 typedef ContinuousRangeMap<Key, ModuleFile *, InitialCapacity> MapType;
6704 llvm::errs() << Name << ":\n";
6705 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
6706 I != IEnd; ++I) {
6707 llvm::errs() << " " << I->first << " -> " << I->second->FileName
6708 << "\n";
6709 }
6710}
6711
6712void ASTReader::dump() {
6713 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
6714 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
6715 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
6716 dumpModuleIDMap("Global type map", GlobalTypeMap);
6717 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
6718 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
6719 dumpModuleIDMap("Global macro map", GlobalMacroMap);
6720 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
6721 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
6722 dumpModuleIDMap("Global preprocessed entity map",
6723 GlobalPreprocessedEntityMap);
6724
6725 llvm::errs() << "\n*** PCH/Modules Loaded:";
6726 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
6727 MEnd = ModuleMgr.end();
6728 M != MEnd; ++M)
6729 (*M)->dump();
6730}
6731
6732/// Return the amount of memory used by memory buffers, breaking down
6733/// by heap-backed versus mmap'ed memory.
6734void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
6735 for (ModuleConstIterator I = ModuleMgr.begin(),
6736 E = ModuleMgr.end(); I != E; ++I) {
6737 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
6738 size_t bytes = buf->getBufferSize();
6739 switch (buf->getBufferKind()) {
6740 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
6741 sizes.malloc_bytes += bytes;
6742 break;
6743 case llvm::MemoryBuffer::MemoryBuffer_MMap:
6744 sizes.mmap_bytes += bytes;
6745 break;
6746 }
6747 }
6748 }
6749}
6750
6751void ASTReader::InitializeSema(Sema &S) {
6752 SemaObj = &S;
6753 S.addExternalSource(this);
6754
6755 // Makes sure any declarations that were deserialized "too early"
6756 // still get added to the identifier's declaration chains.
Ben Langmuir5418f402014-09-10 21:29:41 +00006757 for (uint64_t ID : PreloadedDeclIDs) {
6758 NamedDecl *D = cast<NamedDecl>(GetDecl(ID));
6759 pushExternalDeclIntoScope(D, D->getDeclName());
Guy Benyei11169dd2012-12-18 14:30:41 +00006760 }
Ben Langmuir5418f402014-09-10 21:29:41 +00006761 PreloadedDeclIDs.clear();
Guy Benyei11169dd2012-12-18 14:30:41 +00006762
Richard Smith3d8e97e2013-10-18 06:54:39 +00006763 // FIXME: What happens if these are changed by a module import?
Guy Benyei11169dd2012-12-18 14:30:41 +00006764 if (!FPPragmaOptions.empty()) {
6765 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
6766 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
6767 }
6768
Richard Smith3d8e97e2013-10-18 06:54:39 +00006769 // FIXME: What happens if these are changed by a module import?
Guy Benyei11169dd2012-12-18 14:30:41 +00006770 if (!OpenCLExtensions.empty()) {
6771 unsigned I = 0;
6772#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
6773#include "clang/Basic/OpenCLExtensions.def"
6774
6775 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
6776 }
Richard Smith3d8e97e2013-10-18 06:54:39 +00006777
6778 UpdateSema();
6779}
6780
6781void ASTReader::UpdateSema() {
6782 assert(SemaObj && "no Sema to update");
6783
6784 // Load the offsets of the declarations that Sema references.
6785 // They will be lazily deserialized when needed.
6786 if (!SemaDeclRefs.empty()) {
6787 assert(SemaDeclRefs.size() % 2 == 0);
6788 for (unsigned I = 0; I != SemaDeclRefs.size(); I += 2) {
6789 if (!SemaObj->StdNamespace)
6790 SemaObj->StdNamespace = SemaDeclRefs[I];
6791 if (!SemaObj->StdBadAlloc)
6792 SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
6793 }
6794 SemaDeclRefs.clear();
6795 }
Dario Domizioli13a0a382014-05-23 12:13:25 +00006796
6797 // Update the state of 'pragma clang optimize'. Use the same API as if we had
6798 // encountered the pragma in the source.
6799 if(OptimizeOffPragmaLocation.isValid())
6800 SemaObj->ActOnPragmaOptimize(/* IsOn = */ false, OptimizeOffPragmaLocation);
Guy Benyei11169dd2012-12-18 14:30:41 +00006801}
6802
6803IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
6804 // Note that we are loading an identifier.
6805 Deserializing AnIdentifier(this);
Douglas Gregore060e572013-01-25 01:03:03 +00006806 StringRef Name(NameStart, NameEnd - NameStart);
6807
6808 // If there is a global index, look there first to determine which modules
6809 // provably do not have any results for this identifier.
Douglas Gregor7211ac12013-01-25 23:32:03 +00006810 GlobalModuleIndex::HitSet Hits;
Craig Toppera13603a2014-05-22 05:54:18 +00006811 GlobalModuleIndex::HitSet *HitsPtr = nullptr;
Douglas Gregore060e572013-01-25 01:03:03 +00006812 if (!loadGlobalIndex()) {
Douglas Gregor7211ac12013-01-25 23:32:03 +00006813 if (GlobalIndex->lookupIdentifier(Name, Hits)) {
6814 HitsPtr = &Hits;
Douglas Gregore060e572013-01-25 01:03:03 +00006815 }
6816 }
Douglas Gregor7211ac12013-01-25 23:32:03 +00006817 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
Douglas Gregor00a50f72013-01-25 00:38:33 +00006818 NumIdentifierLookups,
6819 NumIdentifierLookupHits);
Douglas Gregor7211ac12013-01-25 23:32:03 +00006820 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor, HitsPtr);
Guy Benyei11169dd2012-12-18 14:30:41 +00006821 IdentifierInfo *II = Visitor.getIdentifierInfo();
6822 markIdentifierUpToDate(II);
6823 return II;
6824}
6825
6826namespace clang {
6827 /// \brief An identifier-lookup iterator that enumerates all of the
6828 /// identifiers stored within a set of AST files.
6829 class ASTIdentifierIterator : public IdentifierIterator {
6830 /// \brief The AST reader whose identifiers are being enumerated.
6831 const ASTReader &Reader;
6832
6833 /// \brief The current index into the chain of AST files stored in
6834 /// the AST reader.
6835 unsigned Index;
6836
6837 /// \brief The current position within the identifier lookup table
6838 /// of the current AST file.
6839 ASTIdentifierLookupTable::key_iterator Current;
6840
6841 /// \brief The end position within the identifier lookup table of
6842 /// the current AST file.
6843 ASTIdentifierLookupTable::key_iterator End;
6844
6845 public:
6846 explicit ASTIdentifierIterator(const ASTReader &Reader);
6847
Craig Topper3e89dfe2014-03-13 02:13:41 +00006848 StringRef Next() override;
Guy Benyei11169dd2012-12-18 14:30:41 +00006849 };
6850}
6851
6852ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
6853 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
6854 ASTIdentifierLookupTable *IdTable
6855 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
6856 Current = IdTable->key_begin();
6857 End = IdTable->key_end();
6858}
6859
6860StringRef ASTIdentifierIterator::Next() {
6861 while (Current == End) {
6862 // If we have exhausted all of our AST files, we're done.
6863 if (Index == 0)
6864 return StringRef();
6865
6866 --Index;
6867 ASTIdentifierLookupTable *IdTable
6868 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
6869 IdentifierLookupTable;
6870 Current = IdTable->key_begin();
6871 End = IdTable->key_end();
6872 }
6873
6874 // We have any identifiers remaining in the current AST file; return
6875 // the next one.
Douglas Gregorbfd73d72013-01-23 18:53:14 +00006876 StringRef Result = *Current;
Guy Benyei11169dd2012-12-18 14:30:41 +00006877 ++Current;
Douglas Gregorbfd73d72013-01-23 18:53:14 +00006878 return Result;
Guy Benyei11169dd2012-12-18 14:30:41 +00006879}
6880
Argyrios Kyrtzidis9aca3c62013-04-17 22:10:55 +00006881IdentifierIterator *ASTReader::getIdentifiers() {
6882 if (!loadGlobalIndex())
6883 return GlobalIndex->createIdentifierIterator();
6884
Guy Benyei11169dd2012-12-18 14:30:41 +00006885 return new ASTIdentifierIterator(*this);
6886}
6887
6888namespace clang { namespace serialization {
6889 class ReadMethodPoolVisitor {
6890 ASTReader &Reader;
6891 Selector Sel;
6892 unsigned PriorGeneration;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00006893 unsigned InstanceBits;
6894 unsigned FactoryBits;
Nico Weberff4b35e2014-12-27 22:14:15 +00006895 bool InstanceHasMoreThanOneDecl;
6896 bool FactoryHasMoreThanOneDecl;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00006897 SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
6898 SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
Guy Benyei11169dd2012-12-18 14:30:41 +00006899
6900 public:
Nico Weber2e0c8f72014-12-27 03:58:08 +00006901 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
Guy Benyei11169dd2012-12-18 14:30:41 +00006902 unsigned PriorGeneration)
Nico Weber2e0c8f72014-12-27 03:58:08 +00006903 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration),
Nico Weberff4b35e2014-12-27 22:14:15 +00006904 InstanceBits(0), FactoryBits(0), InstanceHasMoreThanOneDecl(false),
6905 FactoryHasMoreThanOneDecl(false) {}
Nico Weber2e0c8f72014-12-27 03:58:08 +00006906
Guy Benyei11169dd2012-12-18 14:30:41 +00006907 static bool visit(ModuleFile &M, void *UserData) {
6908 ReadMethodPoolVisitor *This
6909 = static_cast<ReadMethodPoolVisitor *>(UserData);
6910
6911 if (!M.SelectorLookupTable)
6912 return false;
6913
6914 // If we've already searched this module file, skip it now.
6915 if (M.Generation <= This->PriorGeneration)
6916 return true;
6917
Douglas Gregorad2f7a52013-01-28 17:54:36 +00006918 ++This->Reader.NumMethodPoolTableLookups;
Guy Benyei11169dd2012-12-18 14:30:41 +00006919 ASTSelectorLookupTable *PoolTable
6920 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
6921 ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel);
6922 if (Pos == PoolTable->end())
6923 return false;
Douglas Gregorad2f7a52013-01-28 17:54:36 +00006924
6925 ++This->Reader.NumMethodPoolTableHits;
Guy Benyei11169dd2012-12-18 14:30:41 +00006926 ++This->Reader.NumSelectorsRead;
6927 // FIXME: Not quite happy with the statistics here. We probably should
6928 // disable this tracking when called via LoadSelector.
6929 // Also, should entries without methods count as misses?
6930 ++This->Reader.NumMethodPoolEntriesRead;
6931 ASTSelectorLookupTrait::data_type Data = *Pos;
6932 if (This->Reader.DeserializationListener)
6933 This->Reader.DeserializationListener->SelectorRead(Data.ID,
6934 This->Sel);
6935
6936 This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
6937 This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00006938 This->InstanceBits = Data.InstanceBits;
6939 This->FactoryBits = Data.FactoryBits;
Nico Weberff4b35e2014-12-27 22:14:15 +00006940 This->InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
6941 This->FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
Guy Benyei11169dd2012-12-18 14:30:41 +00006942 return true;
6943 }
6944
6945 /// \brief Retrieve the instance methods found by this visitor.
6946 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
6947 return InstanceMethods;
6948 }
6949
6950 /// \brief Retrieve the instance methods found by this visitor.
6951 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
6952 return FactoryMethods;
6953 }
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00006954
6955 unsigned getInstanceBits() const { return InstanceBits; }
6956 unsigned getFactoryBits() const { return FactoryBits; }
Nico Weberff4b35e2014-12-27 22:14:15 +00006957 bool instanceHasMoreThanOneDecl() const {
6958 return InstanceHasMoreThanOneDecl;
6959 }
6960 bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
Guy Benyei11169dd2012-12-18 14:30:41 +00006961 };
6962} } // end namespace clang::serialization
6963
6964/// \brief Add the given set of methods to the method list.
6965static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
6966 ObjCMethodList &List) {
6967 for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
6968 S.addMethodToGlobalList(&List, Methods[I]);
6969 }
6970}
6971
6972void ASTReader::ReadMethodPool(Selector Sel) {
6973 // Get the selector generation and update it to the current generation.
6974 unsigned &Generation = SelectorGeneration[Sel];
6975 unsigned PriorGeneration = Generation;
Richard Smith053f6c62014-05-16 23:01:30 +00006976 Generation = getGeneration();
Guy Benyei11169dd2012-12-18 14:30:41 +00006977
6978 // Search for methods defined with this selector.
Douglas Gregorad2f7a52013-01-28 17:54:36 +00006979 ++NumMethodPoolLookups;
Guy Benyei11169dd2012-12-18 14:30:41 +00006980 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
6981 ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor);
6982
6983 if (Visitor.getInstanceMethods().empty() &&
Douglas Gregorad2f7a52013-01-28 17:54:36 +00006984 Visitor.getFactoryMethods().empty())
Guy Benyei11169dd2012-12-18 14:30:41 +00006985 return;
Douglas Gregorad2f7a52013-01-28 17:54:36 +00006986
6987 ++NumMethodPoolHits;
6988
Guy Benyei11169dd2012-12-18 14:30:41 +00006989 if (!getSema())
6990 return;
6991
6992 Sema &S = *getSema();
6993 Sema::GlobalMethodPool::iterator Pos
6994 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
Ben Langmuira0c32e92015-01-12 19:27:00 +00006995
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00006996 Pos->second.first.setBits(Visitor.getInstanceBits());
Nico Weberff4b35e2014-12-27 22:14:15 +00006997 Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00006998 Pos->second.second.setBits(Visitor.getFactoryBits());
Nico Weberff4b35e2014-12-27 22:14:15 +00006999 Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
Ben Langmuira0c32e92015-01-12 19:27:00 +00007000
7001 // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
7002 // when building a module we keep every method individually and may need to
7003 // update hasMoreThanOneDecl as we add the methods.
7004 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
7005 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
Guy Benyei11169dd2012-12-18 14:30:41 +00007006}
7007
7008void ASTReader::ReadKnownNamespaces(
7009 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
7010 Namespaces.clear();
7011
7012 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
7013 if (NamespaceDecl *Namespace
7014 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
7015 Namespaces.push_back(Namespace);
7016 }
7017}
7018
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00007019void ASTReader::ReadUndefinedButUsed(
Nick Lewyckyf0f56162013-01-31 03:23:57 +00007020 llvm::DenseMap<NamedDecl*, SourceLocation> &Undefined) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00007021 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
7022 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
Nick Lewycky8334af82013-01-26 00:35:08 +00007023 SourceLocation Loc =
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00007024 SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
Nick Lewycky8334af82013-01-26 00:35:08 +00007025 Undefined.insert(std::make_pair(D, Loc));
7026 }
7027}
Nick Lewycky8334af82013-01-26 00:35:08 +00007028
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00007029void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
7030 FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
7031 Exprs) {
7032 for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
7033 FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++]));
7034 uint64_t Count = DelayedDeleteExprs[Idx++];
7035 for (uint64_t C = 0; C < Count; ++C) {
7036 SourceLocation DeleteLoc =
7037 SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]);
7038 const bool IsArrayForm = DelayedDeleteExprs[Idx++];
7039 Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm));
7040 }
7041 }
7042}
7043
Guy Benyei11169dd2012-12-18 14:30:41 +00007044void ASTReader::ReadTentativeDefinitions(
7045 SmallVectorImpl<VarDecl *> &TentativeDefs) {
7046 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
7047 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
7048 if (Var)
7049 TentativeDefs.push_back(Var);
7050 }
7051 TentativeDefinitions.clear();
7052}
7053
7054void ASTReader::ReadUnusedFileScopedDecls(
7055 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
7056 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
7057 DeclaratorDecl *D
7058 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
7059 if (D)
7060 Decls.push_back(D);
7061 }
7062 UnusedFileScopedDecls.clear();
7063}
7064
7065void ASTReader::ReadDelegatingConstructors(
7066 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
7067 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
7068 CXXConstructorDecl *D
7069 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
7070 if (D)
7071 Decls.push_back(D);
7072 }
7073 DelegatingCtorDecls.clear();
7074}
7075
7076void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
7077 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
7078 TypedefNameDecl *D
7079 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
7080 if (D)
7081 Decls.push_back(D);
7082 }
7083 ExtVectorDecls.clear();
7084}
7085
Nico Weber72889432014-09-06 01:25:55 +00007086void ASTReader::ReadUnusedLocalTypedefNameCandidates(
7087 llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
7088 for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
7089 ++I) {
7090 TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
7091 GetDecl(UnusedLocalTypedefNameCandidates[I]));
7092 if (D)
7093 Decls.insert(D);
7094 }
7095 UnusedLocalTypedefNameCandidates.clear();
7096}
7097
Guy Benyei11169dd2012-12-18 14:30:41 +00007098void ASTReader::ReadReferencedSelectors(
7099 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
7100 if (ReferencedSelectorsData.empty())
7101 return;
7102
7103 // If there are @selector references added them to its pool. This is for
7104 // implementation of -Wselector.
7105 unsigned int DataSize = ReferencedSelectorsData.size()-1;
7106 unsigned I = 0;
7107 while (I < DataSize) {
7108 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
7109 SourceLocation SelLoc
7110 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
7111 Sels.push_back(std::make_pair(Sel, SelLoc));
7112 }
7113 ReferencedSelectorsData.clear();
7114}
7115
7116void ASTReader::ReadWeakUndeclaredIdentifiers(
7117 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
7118 if (WeakUndeclaredIdentifiers.empty())
7119 return;
7120
7121 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
7122 IdentifierInfo *WeakId
7123 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
7124 IdentifierInfo *AliasId
7125 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
7126 SourceLocation Loc
7127 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
7128 bool Used = WeakUndeclaredIdentifiers[I++];
7129 WeakInfo WI(AliasId, Loc);
7130 WI.setUsed(Used);
7131 WeakIDs.push_back(std::make_pair(WeakId, WI));
7132 }
7133 WeakUndeclaredIdentifiers.clear();
7134}
7135
7136void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
7137 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
7138 ExternalVTableUse VT;
7139 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
7140 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
7141 VT.DefinitionRequired = VTableUses[Idx++];
7142 VTables.push_back(VT);
7143 }
7144
7145 VTableUses.clear();
7146}
7147
7148void ASTReader::ReadPendingInstantiations(
7149 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
7150 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
7151 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
7152 SourceLocation Loc
7153 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
7154
7155 Pending.push_back(std::make_pair(D, Loc));
7156 }
7157 PendingInstantiations.clear();
7158}
7159
Richard Smithe40f2ba2013-08-07 21:41:30 +00007160void ASTReader::ReadLateParsedTemplates(
Chandler Carruth52cee4d2015-03-26 09:08:15 +00007161 llvm::MapVector<const FunctionDecl *, LateParsedTemplate *> &LPTMap) {
Richard Smithe40f2ba2013-08-07 21:41:30 +00007162 for (unsigned Idx = 0, N = LateParsedTemplates.size(); Idx < N;
7163 /* In loop */) {
7164 FunctionDecl *FD = cast<FunctionDecl>(GetDecl(LateParsedTemplates[Idx++]));
7165
7166 LateParsedTemplate *LT = new LateParsedTemplate;
7167 LT->D = GetDecl(LateParsedTemplates[Idx++]);
7168
7169 ModuleFile *F = getOwningModuleFile(LT->D);
7170 assert(F && "No module");
7171
7172 unsigned TokN = LateParsedTemplates[Idx++];
7173 LT->Toks.reserve(TokN);
7174 for (unsigned T = 0; T < TokN; ++T)
7175 LT->Toks.push_back(ReadToken(*F, LateParsedTemplates, Idx));
7176
Chandler Carruth52cee4d2015-03-26 09:08:15 +00007177 LPTMap.insert(std::make_pair(FD, LT));
Richard Smithe40f2ba2013-08-07 21:41:30 +00007178 }
7179
7180 LateParsedTemplates.clear();
7181}
7182
Guy Benyei11169dd2012-12-18 14:30:41 +00007183void ASTReader::LoadSelector(Selector Sel) {
7184 // It would be complicated to avoid reading the methods anyway. So don't.
7185 ReadMethodPool(Sel);
7186}
7187
7188void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
7189 assert(ID && "Non-zero identifier ID required");
7190 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
7191 IdentifiersLoaded[ID - 1] = II;
7192 if (DeserializationListener)
7193 DeserializationListener->IdentifierRead(ID, II);
7194}
7195
7196/// \brief Set the globally-visible declarations associated with the given
7197/// identifier.
7198///
7199/// If the AST reader is currently in a state where the given declaration IDs
7200/// cannot safely be resolved, they are queued until it is safe to resolve
7201/// them.
7202///
7203/// \param II an IdentifierInfo that refers to one or more globally-visible
7204/// declarations.
7205///
7206/// \param DeclIDs the set of declaration IDs with the name @p II that are
7207/// visible at global scope.
7208///
Douglas Gregor6168bd22013-02-18 15:53:43 +00007209/// \param Decls if non-null, this vector will be populated with the set of
7210/// deserialized declarations. These declarations will not be pushed into
7211/// scope.
Guy Benyei11169dd2012-12-18 14:30:41 +00007212void
7213ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
7214 const SmallVectorImpl<uint32_t> &DeclIDs,
Douglas Gregor6168bd22013-02-18 15:53:43 +00007215 SmallVectorImpl<Decl *> *Decls) {
7216 if (NumCurrentElementsDeserializing && !Decls) {
7217 PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
Guy Benyei11169dd2012-12-18 14:30:41 +00007218 return;
7219 }
7220
7221 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
Ben Langmuir5418f402014-09-10 21:29:41 +00007222 if (!SemaObj) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007223 // Queue this declaration so that it will be added to the
7224 // translation unit scope and identifier's declaration chain
7225 // once a Sema object is known.
Ben Langmuir5418f402014-09-10 21:29:41 +00007226 PreloadedDeclIDs.push_back(DeclIDs[I]);
7227 continue;
Guy Benyei11169dd2012-12-18 14:30:41 +00007228 }
Ben Langmuir5418f402014-09-10 21:29:41 +00007229
7230 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
7231
7232 // If we're simply supposed to record the declarations, do so now.
7233 if (Decls) {
7234 Decls->push_back(D);
7235 continue;
7236 }
7237
7238 // Introduce this declaration into the translation-unit scope
7239 // and add it to the declaration chain for this identifier, so
7240 // that (unqualified) name lookup will find it.
7241 pushExternalDeclIntoScope(D, II);
Guy Benyei11169dd2012-12-18 14:30:41 +00007242 }
7243}
7244
Douglas Gregorc8a992f2013-01-21 16:52:34 +00007245IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007246 if (ID == 0)
Craig Toppera13603a2014-05-22 05:54:18 +00007247 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007248
7249 if (IdentifiersLoaded.empty()) {
7250 Error("no identifier table in AST file");
Craig Toppera13603a2014-05-22 05:54:18 +00007251 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007252 }
7253
7254 ID -= 1;
7255 if (!IdentifiersLoaded[ID]) {
7256 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
7257 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
7258 ModuleFile *M = I->second;
7259 unsigned Index = ID - M->BaseIdentifierID;
7260 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
7261
7262 // All of the strings in the AST file are preceded by a 16-bit length.
7263 // Extract that 16-bit length to avoid having to execute strlen().
7264 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
7265 // unsigned integers. This is important to avoid integer overflow when
7266 // we cast them to 'unsigned'.
7267 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
7268 unsigned StrLen = (((unsigned) StrLenPtr[0])
7269 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Douglas Gregorc8a992f2013-01-21 16:52:34 +00007270 IdentifiersLoaded[ID]
7271 = &PP.getIdentifierTable().get(StringRef(Str, StrLen));
Guy Benyei11169dd2012-12-18 14:30:41 +00007272 if (DeserializationListener)
7273 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
7274 }
7275
7276 return IdentifiersLoaded[ID];
7277}
7278
Douglas Gregorc8a992f2013-01-21 16:52:34 +00007279IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
7280 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
Guy Benyei11169dd2012-12-18 14:30:41 +00007281}
7282
7283IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
7284 if (LocalID < NUM_PREDEF_IDENT_IDS)
7285 return LocalID;
7286
7287 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7288 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
7289 assert(I != M.IdentifierRemap.end()
7290 && "Invalid index into identifier index remap");
7291
7292 return LocalID + I->second;
7293}
7294
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00007295MacroInfo *ASTReader::getMacro(MacroID ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007296 if (ID == 0)
Craig Toppera13603a2014-05-22 05:54:18 +00007297 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007298
7299 if (MacrosLoaded.empty()) {
7300 Error("no macro table in AST file");
Craig Toppera13603a2014-05-22 05:54:18 +00007301 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007302 }
7303
7304 ID -= NUM_PREDEF_MACRO_IDS;
7305 if (!MacrosLoaded[ID]) {
7306 GlobalMacroMapType::iterator I
7307 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
7308 assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
7309 ModuleFile *M = I->second;
7310 unsigned Index = ID - M->BaseMacroID;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00007311 MacrosLoaded[ID] = ReadMacroRecord(*M, M->MacroOffsets[Index]);
7312
7313 if (DeserializationListener)
7314 DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
7315 MacrosLoaded[ID]);
Guy Benyei11169dd2012-12-18 14:30:41 +00007316 }
7317
7318 return MacrosLoaded[ID];
7319}
7320
7321MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
7322 if (LocalID < NUM_PREDEF_MACRO_IDS)
7323 return LocalID;
7324
7325 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7326 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
7327 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
7328
7329 return LocalID + I->second;
7330}
7331
7332serialization::SubmoduleID
7333ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
7334 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
7335 return LocalID;
7336
7337 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7338 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
7339 assert(I != M.SubmoduleRemap.end()
7340 && "Invalid index into submodule index remap");
7341
7342 return LocalID + I->second;
7343}
7344
7345Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
7346 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
7347 assert(GlobalID == 0 && "Unhandled global submodule ID");
Craig Toppera13603a2014-05-22 05:54:18 +00007348 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007349 }
7350
7351 if (GlobalID > SubmodulesLoaded.size()) {
7352 Error("submodule ID out of range in AST file");
Craig Toppera13603a2014-05-22 05:54:18 +00007353 return nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007354 }
7355
7356 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
7357}
Douglas Gregorc147b0b2013-01-12 01:29:50 +00007358
7359Module *ASTReader::getModule(unsigned ID) {
7360 return getSubmodule(ID);
7361}
7362
Guy Benyei11169dd2012-12-18 14:30:41 +00007363Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
7364 return DecodeSelector(getGlobalSelectorID(M, LocalID));
7365}
7366
7367Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
7368 if (ID == 0)
7369 return Selector();
7370
7371 if (ID > SelectorsLoaded.size()) {
7372 Error("selector ID out of range in AST file");
7373 return Selector();
7374 }
7375
Craig Toppera13603a2014-05-22 05:54:18 +00007376 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007377 // Load this selector from the selector table.
7378 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
7379 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
7380 ModuleFile &M = *I->second;
7381 ASTSelectorLookupTrait Trait(*this, M);
7382 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
7383 SelectorsLoaded[ID - 1] =
7384 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
7385 if (DeserializationListener)
7386 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
7387 }
7388
7389 return SelectorsLoaded[ID - 1];
7390}
7391
7392Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
7393 return DecodeSelector(ID);
7394}
7395
7396uint32_t ASTReader::GetNumExternalSelectors() {
7397 // ID 0 (the null selector) is considered an external selector.
7398 return getTotalNumSelectors() + 1;
7399}
7400
7401serialization::SelectorID
7402ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
7403 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
7404 return LocalID;
7405
7406 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7407 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
7408 assert(I != M.SelectorRemap.end()
7409 && "Invalid index into selector index remap");
7410
7411 return LocalID + I->second;
7412}
7413
7414DeclarationName
7415ASTReader::ReadDeclarationName(ModuleFile &F,
7416 const RecordData &Record, unsigned &Idx) {
7417 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
7418 switch (Kind) {
7419 case DeclarationName::Identifier:
Douglas Gregorc8a992f2013-01-21 16:52:34 +00007420 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00007421
7422 case DeclarationName::ObjCZeroArgSelector:
7423 case DeclarationName::ObjCOneArgSelector:
7424 case DeclarationName::ObjCMultiArgSelector:
7425 return DeclarationName(ReadSelector(F, Record, Idx));
7426
7427 case DeclarationName::CXXConstructorName:
7428 return Context.DeclarationNames.getCXXConstructorName(
7429 Context.getCanonicalType(readType(F, Record, Idx)));
7430
7431 case DeclarationName::CXXDestructorName:
7432 return Context.DeclarationNames.getCXXDestructorName(
7433 Context.getCanonicalType(readType(F, Record, Idx)));
7434
7435 case DeclarationName::CXXConversionFunctionName:
7436 return Context.DeclarationNames.getCXXConversionFunctionName(
7437 Context.getCanonicalType(readType(F, Record, Idx)));
7438
7439 case DeclarationName::CXXOperatorName:
7440 return Context.DeclarationNames.getCXXOperatorName(
7441 (OverloadedOperatorKind)Record[Idx++]);
7442
7443 case DeclarationName::CXXLiteralOperatorName:
7444 return Context.DeclarationNames.getCXXLiteralOperatorName(
7445 GetIdentifierInfo(F, Record, Idx));
7446
7447 case DeclarationName::CXXUsingDirective:
7448 return DeclarationName::getUsingDirectiveName();
7449 }
7450
7451 llvm_unreachable("Invalid NameKind!");
7452}
7453
7454void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
7455 DeclarationNameLoc &DNLoc,
7456 DeclarationName Name,
7457 const RecordData &Record, unsigned &Idx) {
7458 switch (Name.getNameKind()) {
7459 case DeclarationName::CXXConstructorName:
7460 case DeclarationName::CXXDestructorName:
7461 case DeclarationName::CXXConversionFunctionName:
7462 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
7463 break;
7464
7465 case DeclarationName::CXXOperatorName:
7466 DNLoc.CXXOperatorName.BeginOpNameLoc
7467 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
7468 DNLoc.CXXOperatorName.EndOpNameLoc
7469 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
7470 break;
7471
7472 case DeclarationName::CXXLiteralOperatorName:
7473 DNLoc.CXXLiteralOperatorName.OpNameLoc
7474 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
7475 break;
7476
7477 case DeclarationName::Identifier:
7478 case DeclarationName::ObjCZeroArgSelector:
7479 case DeclarationName::ObjCOneArgSelector:
7480 case DeclarationName::ObjCMultiArgSelector:
7481 case DeclarationName::CXXUsingDirective:
7482 break;
7483 }
7484}
7485
7486void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
7487 DeclarationNameInfo &NameInfo,
7488 const RecordData &Record, unsigned &Idx) {
7489 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
7490 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
7491 DeclarationNameLoc DNLoc;
7492 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
7493 NameInfo.setInfo(DNLoc);
7494}
7495
7496void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
7497 const RecordData &Record, unsigned &Idx) {
7498 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
7499 unsigned NumTPLists = Record[Idx++];
7500 Info.NumTemplParamLists = NumTPLists;
7501 if (NumTPLists) {
7502 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
7503 for (unsigned i=0; i != NumTPLists; ++i)
7504 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
7505 }
7506}
7507
7508TemplateName
7509ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
7510 unsigned &Idx) {
7511 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
7512 switch (Kind) {
7513 case TemplateName::Template:
7514 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
7515
7516 case TemplateName::OverloadedTemplate: {
7517 unsigned size = Record[Idx++];
7518 UnresolvedSet<8> Decls;
7519 while (size--)
7520 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
7521
7522 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
7523 }
7524
7525 case TemplateName::QualifiedTemplate: {
7526 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
7527 bool hasTemplKeyword = Record[Idx++];
7528 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
7529 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
7530 }
7531
7532 case TemplateName::DependentTemplate: {
7533 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
7534 if (Record[Idx++]) // isIdentifier
7535 return Context.getDependentTemplateName(NNS,
7536 GetIdentifierInfo(F, Record,
7537 Idx));
7538 return Context.getDependentTemplateName(NNS,
7539 (OverloadedOperatorKind)Record[Idx++]);
7540 }
7541
7542 case TemplateName::SubstTemplateTemplateParm: {
7543 TemplateTemplateParmDecl *param
7544 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
7545 if (!param) return TemplateName();
7546 TemplateName replacement = ReadTemplateName(F, Record, Idx);
7547 return Context.getSubstTemplateTemplateParm(param, replacement);
7548 }
7549
7550 case TemplateName::SubstTemplateTemplateParmPack: {
7551 TemplateTemplateParmDecl *Param
7552 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
7553 if (!Param)
7554 return TemplateName();
7555
7556 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
7557 if (ArgPack.getKind() != TemplateArgument::Pack)
7558 return TemplateName();
7559
7560 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
7561 }
7562 }
7563
7564 llvm_unreachable("Unhandled template name kind!");
7565}
7566
7567TemplateArgument
7568ASTReader::ReadTemplateArgument(ModuleFile &F,
7569 const RecordData &Record, unsigned &Idx) {
7570 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
7571 switch (Kind) {
7572 case TemplateArgument::Null:
7573 return TemplateArgument();
7574 case TemplateArgument::Type:
7575 return TemplateArgument(readType(F, Record, Idx));
7576 case TemplateArgument::Declaration: {
7577 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
David Blaikie0f62c8d2014-10-16 04:21:25 +00007578 return TemplateArgument(D, readType(F, Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00007579 }
7580 case TemplateArgument::NullPtr:
7581 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
7582 case TemplateArgument::Integral: {
7583 llvm::APSInt Value = ReadAPSInt(Record, Idx);
7584 QualType T = readType(F, Record, Idx);
7585 return TemplateArgument(Context, Value, T);
7586 }
7587 case TemplateArgument::Template:
7588 return TemplateArgument(ReadTemplateName(F, Record, Idx));
7589 case TemplateArgument::TemplateExpansion: {
7590 TemplateName Name = ReadTemplateName(F, Record, Idx);
David Blaikie05785d12013-02-20 22:23:23 +00007591 Optional<unsigned> NumTemplateExpansions;
Guy Benyei11169dd2012-12-18 14:30:41 +00007592 if (unsigned NumExpansions = Record[Idx++])
7593 NumTemplateExpansions = NumExpansions - 1;
7594 return TemplateArgument(Name, NumTemplateExpansions);
7595 }
7596 case TemplateArgument::Expression:
7597 return TemplateArgument(ReadExpr(F));
7598 case TemplateArgument::Pack: {
7599 unsigned NumArgs = Record[Idx++];
7600 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
7601 for (unsigned I = 0; I != NumArgs; ++I)
7602 Args[I] = ReadTemplateArgument(F, Record, Idx);
7603 return TemplateArgument(Args, NumArgs);
7604 }
7605 }
7606
7607 llvm_unreachable("Unhandled template argument kind!");
7608}
7609
7610TemplateParameterList *
7611ASTReader::ReadTemplateParameterList(ModuleFile &F,
7612 const RecordData &Record, unsigned &Idx) {
7613 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
7614 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
7615 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
7616
7617 unsigned NumParams = Record[Idx++];
7618 SmallVector<NamedDecl *, 16> Params;
7619 Params.reserve(NumParams);
7620 while (NumParams--)
7621 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
7622
7623 TemplateParameterList* TemplateParams =
7624 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
7625 Params.data(), Params.size(), RAngleLoc);
7626 return TemplateParams;
7627}
7628
7629void
7630ASTReader::
Craig Topper5603df42013-07-05 19:34:19 +00007631ReadTemplateArgumentList(SmallVectorImpl<TemplateArgument> &TemplArgs,
Guy Benyei11169dd2012-12-18 14:30:41 +00007632 ModuleFile &F, const RecordData &Record,
7633 unsigned &Idx) {
7634 unsigned NumTemplateArgs = Record[Idx++];
7635 TemplArgs.reserve(NumTemplateArgs);
7636 while (NumTemplateArgs--)
7637 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
7638}
7639
7640/// \brief Read a UnresolvedSet structure.
Richard Smitha4ba74c2013-08-30 04:46:40 +00007641void ASTReader::ReadUnresolvedSet(ModuleFile &F, LazyASTUnresolvedSet &Set,
Guy Benyei11169dd2012-12-18 14:30:41 +00007642 const RecordData &Record, unsigned &Idx) {
7643 unsigned NumDecls = Record[Idx++];
7644 Set.reserve(Context, NumDecls);
7645 while (NumDecls--) {
Richard Smitha4ba74c2013-08-30 04:46:40 +00007646 DeclID ID = ReadDeclID(F, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00007647 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
Richard Smitha4ba74c2013-08-30 04:46:40 +00007648 Set.addLazyDecl(Context, ID, AS);
Guy Benyei11169dd2012-12-18 14:30:41 +00007649 }
7650}
7651
7652CXXBaseSpecifier
7653ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
7654 const RecordData &Record, unsigned &Idx) {
7655 bool isVirtual = static_cast<bool>(Record[Idx++]);
7656 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
7657 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
7658 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
7659 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
7660 SourceRange Range = ReadSourceRange(F, Record, Idx);
7661 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
7662 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
7663 EllipsisLoc);
7664 Result.setInheritConstructors(inheritConstructors);
7665 return Result;
7666}
7667
Richard Smithc2bb8182015-03-24 06:36:48 +00007668CXXCtorInitializer **
Guy Benyei11169dd2012-12-18 14:30:41 +00007669ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
7670 unsigned &Idx) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007671 unsigned NumInitializers = Record[Idx++];
Richard Smithc2bb8182015-03-24 06:36:48 +00007672 assert(NumInitializers && "wrote ctor initializers but have no inits");
7673 auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
7674 for (unsigned i = 0; i != NumInitializers; ++i) {
7675 TypeSourceInfo *TInfo = nullptr;
7676 bool IsBaseVirtual = false;
7677 FieldDecl *Member = nullptr;
7678 IndirectFieldDecl *IndirectMember = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007679
Richard Smithc2bb8182015-03-24 06:36:48 +00007680 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
7681 switch (Type) {
7682 case CTOR_INITIALIZER_BASE:
7683 TInfo = GetTypeSourceInfo(F, Record, Idx);
7684 IsBaseVirtual = Record[Idx++];
7685 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00007686
Richard Smithc2bb8182015-03-24 06:36:48 +00007687 case CTOR_INITIALIZER_DELEGATING:
7688 TInfo = GetTypeSourceInfo(F, Record, Idx);
7689 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00007690
Richard Smithc2bb8182015-03-24 06:36:48 +00007691 case CTOR_INITIALIZER_MEMBER:
7692 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
7693 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00007694
Richard Smithc2bb8182015-03-24 06:36:48 +00007695 case CTOR_INITIALIZER_INDIRECT_MEMBER:
7696 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
7697 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00007698 }
Richard Smithc2bb8182015-03-24 06:36:48 +00007699
7700 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
7701 Expr *Init = ReadExpr(F);
7702 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
7703 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
7704 bool IsWritten = Record[Idx++];
7705 unsigned SourceOrderOrNumArrayIndices;
7706 SmallVector<VarDecl *, 8> Indices;
7707 if (IsWritten) {
7708 SourceOrderOrNumArrayIndices = Record[Idx++];
7709 } else {
7710 SourceOrderOrNumArrayIndices = Record[Idx++];
7711 Indices.reserve(SourceOrderOrNumArrayIndices);
7712 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
7713 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
7714 }
7715
7716 CXXCtorInitializer *BOMInit;
7717 if (Type == CTOR_INITIALIZER_BASE) {
7718 BOMInit = new (Context)
7719 CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
7720 RParenLoc, MemberOrEllipsisLoc);
7721 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
7722 BOMInit = new (Context)
7723 CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
7724 } else if (IsWritten) {
7725 if (Member)
7726 BOMInit = new (Context) CXXCtorInitializer(
7727 Context, Member, MemberOrEllipsisLoc, LParenLoc, Init, RParenLoc);
7728 else
7729 BOMInit = new (Context)
7730 CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
7731 LParenLoc, Init, RParenLoc);
7732 } else {
7733 if (IndirectMember) {
7734 assert(Indices.empty() && "Indirect field improperly initialized");
7735 BOMInit = new (Context)
7736 CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
7737 LParenLoc, Init, RParenLoc);
7738 } else {
7739 BOMInit = CXXCtorInitializer::Create(
7740 Context, Member, MemberOrEllipsisLoc, LParenLoc, Init, RParenLoc,
7741 Indices.data(), Indices.size());
7742 }
7743 }
7744
7745 if (IsWritten)
7746 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
7747 CtorInitializers[i] = BOMInit;
Guy Benyei11169dd2012-12-18 14:30:41 +00007748 }
7749
Richard Smithc2bb8182015-03-24 06:36:48 +00007750 return CtorInitializers;
Guy Benyei11169dd2012-12-18 14:30:41 +00007751}
7752
7753NestedNameSpecifier *
7754ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
7755 const RecordData &Record, unsigned &Idx) {
7756 unsigned N = Record[Idx++];
Craig Toppera13603a2014-05-22 05:54:18 +00007757 NestedNameSpecifier *NNS = nullptr, *Prev = nullptr;
Guy Benyei11169dd2012-12-18 14:30:41 +00007758 for (unsigned I = 0; I != N; ++I) {
7759 NestedNameSpecifier::SpecifierKind Kind
7760 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
7761 switch (Kind) {
7762 case NestedNameSpecifier::Identifier: {
7763 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
7764 NNS = NestedNameSpecifier::Create(Context, Prev, II);
7765 break;
7766 }
7767
7768 case NestedNameSpecifier::Namespace: {
7769 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
7770 NNS = NestedNameSpecifier::Create(Context, Prev, NS);
7771 break;
7772 }
7773
7774 case NestedNameSpecifier::NamespaceAlias: {
7775 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
7776 NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
7777 break;
7778 }
7779
7780 case NestedNameSpecifier::TypeSpec:
7781 case NestedNameSpecifier::TypeSpecWithTemplate: {
7782 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
7783 if (!T)
Craig Toppera13603a2014-05-22 05:54:18 +00007784 return nullptr;
7785
Guy Benyei11169dd2012-12-18 14:30:41 +00007786 bool Template = Record[Idx++];
7787 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
7788 break;
7789 }
7790
7791 case NestedNameSpecifier::Global: {
7792 NNS = NestedNameSpecifier::GlobalSpecifier(Context);
7793 // No associated value, and there can't be a prefix.
7794 break;
7795 }
Nikola Smiljanic67860242014-09-26 00:28:20 +00007796
7797 case NestedNameSpecifier::Super: {
7798 CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(F, Record, Idx);
7799 NNS = NestedNameSpecifier::SuperSpecifier(Context, RD);
7800 break;
7801 }
Guy Benyei11169dd2012-12-18 14:30:41 +00007802 }
7803 Prev = NNS;
7804 }
7805 return NNS;
7806}
7807
7808NestedNameSpecifierLoc
7809ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
7810 unsigned &Idx) {
7811 unsigned N = Record[Idx++];
7812 NestedNameSpecifierLocBuilder Builder;
7813 for (unsigned I = 0; I != N; ++I) {
7814 NestedNameSpecifier::SpecifierKind Kind
7815 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
7816 switch (Kind) {
7817 case NestedNameSpecifier::Identifier: {
7818 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
7819 SourceRange Range = ReadSourceRange(F, Record, Idx);
7820 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
7821 break;
7822 }
7823
7824 case NestedNameSpecifier::Namespace: {
7825 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
7826 SourceRange Range = ReadSourceRange(F, Record, Idx);
7827 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
7828 break;
7829 }
7830
7831 case NestedNameSpecifier::NamespaceAlias: {
7832 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
7833 SourceRange Range = ReadSourceRange(F, Record, Idx);
7834 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
7835 break;
7836 }
7837
7838 case NestedNameSpecifier::TypeSpec:
7839 case NestedNameSpecifier::TypeSpecWithTemplate: {
7840 bool Template = Record[Idx++];
7841 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
7842 if (!T)
7843 return NestedNameSpecifierLoc();
7844 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
7845
7846 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
7847 Builder.Extend(Context,
7848 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
7849 T->getTypeLoc(), ColonColonLoc);
7850 break;
7851 }
7852
7853 case NestedNameSpecifier::Global: {
7854 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
7855 Builder.MakeGlobal(Context, ColonColonLoc);
7856 break;
7857 }
Nikola Smiljanic67860242014-09-26 00:28:20 +00007858
7859 case NestedNameSpecifier::Super: {
7860 CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(F, Record, Idx);
7861 SourceRange Range = ReadSourceRange(F, Record, Idx);
7862 Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd());
7863 break;
7864 }
Guy Benyei11169dd2012-12-18 14:30:41 +00007865 }
7866 }
Nikola Smiljanic67860242014-09-26 00:28:20 +00007867
Guy Benyei11169dd2012-12-18 14:30:41 +00007868 return Builder.getWithLocInContext(Context);
7869}
7870
7871SourceRange
7872ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
7873 unsigned &Idx) {
7874 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
7875 SourceLocation end = ReadSourceLocation(F, Record, Idx);
7876 return SourceRange(beg, end);
7877}
7878
7879/// \brief Read an integral value
7880llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
7881 unsigned BitWidth = Record[Idx++];
7882 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
7883 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
7884 Idx += NumWords;
7885 return Result;
7886}
7887
7888/// \brief Read a signed integral value
7889llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
7890 bool isUnsigned = Record[Idx++];
7891 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
7892}
7893
7894/// \brief Read a floating-point value
Tim Northover178723a2013-01-22 09:46:51 +00007895llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record,
7896 const llvm::fltSemantics &Sem,
7897 unsigned &Idx) {
7898 return llvm::APFloat(Sem, ReadAPInt(Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00007899}
7900
7901// \brief Read a string
7902std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
7903 unsigned Len = Record[Idx++];
7904 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
7905 Idx += Len;
7906 return Result;
7907}
7908
Richard Smith7ed1bc92014-12-05 22:42:13 +00007909std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
7910 unsigned &Idx) {
7911 std::string Filename = ReadString(Record, Idx);
7912 ResolveImportedPath(F, Filename);
7913 return Filename;
7914}
7915
Guy Benyei11169dd2012-12-18 14:30:41 +00007916VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
7917 unsigned &Idx) {
7918 unsigned Major = Record[Idx++];
7919 unsigned Minor = Record[Idx++];
7920 unsigned Subminor = Record[Idx++];
7921 if (Minor == 0)
7922 return VersionTuple(Major);
7923 if (Subminor == 0)
7924 return VersionTuple(Major, Minor - 1);
7925 return VersionTuple(Major, Minor - 1, Subminor - 1);
7926}
7927
7928CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
7929 const RecordData &Record,
7930 unsigned &Idx) {
7931 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
7932 return CXXTemporary::Create(Context, Decl);
7933}
7934
7935DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Argyrios Kyrtzidisdc9fdaf2013-05-24 05:44:08 +00007936 return Diag(CurrentImportLoc, DiagID);
Guy Benyei11169dd2012-12-18 14:30:41 +00007937}
7938
7939DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
7940 return Diags.Report(Loc, DiagID);
7941}
7942
7943/// \brief Retrieve the identifier table associated with the
7944/// preprocessor.
7945IdentifierTable &ASTReader::getIdentifierTable() {
7946 return PP.getIdentifierTable();
7947}
7948
7949/// \brief Record that the given ID maps to the given switch-case
7950/// statement.
7951void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Craig Toppera13603a2014-05-22 05:54:18 +00007952 assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
Guy Benyei11169dd2012-12-18 14:30:41 +00007953 "Already have a SwitchCase with this ID");
7954 (*CurrSwitchCaseStmts)[ID] = SC;
7955}
7956
7957/// \brief Retrieve the switch-case statement with the given ID.
7958SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Craig Toppera13603a2014-05-22 05:54:18 +00007959 assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
Guy Benyei11169dd2012-12-18 14:30:41 +00007960 return (*CurrSwitchCaseStmts)[ID];
7961}
7962
7963void ASTReader::ClearSwitchCaseIDs() {
7964 CurrSwitchCaseStmts->clear();
7965}
7966
7967void ASTReader::ReadComments() {
7968 std::vector<RawComment *> Comments;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00007969 for (SmallVectorImpl<std::pair<BitstreamCursor,
Guy Benyei11169dd2012-12-18 14:30:41 +00007970 serialization::ModuleFile *> >::iterator
7971 I = CommentsCursors.begin(),
7972 E = CommentsCursors.end();
7973 I != E; ++I) {
Dmitri Gribenko9ee0e302014-03-27 15:40:39 +00007974 Comments.clear();
Chris Lattner7fb3bef2013-01-20 00:56:42 +00007975 BitstreamCursor &Cursor = I->first;
Guy Benyei11169dd2012-12-18 14:30:41 +00007976 serialization::ModuleFile &F = *I->second;
7977 SavedStreamPosition SavedPosition(Cursor);
7978
7979 RecordData Record;
7980 while (true) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00007981 llvm::BitstreamEntry Entry =
7982 Cursor.advanceSkippingSubblocks(BitstreamCursor::AF_DontPopBlockAtEnd);
Dmitri Gribenko9ee0e302014-03-27 15:40:39 +00007983
Chris Lattner7fb3bef2013-01-20 00:56:42 +00007984 switch (Entry.Kind) {
7985 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
7986 case llvm::BitstreamEntry::Error:
7987 Error("malformed block record in AST file");
7988 return;
7989 case llvm::BitstreamEntry::EndBlock:
7990 goto NextCursor;
7991 case llvm::BitstreamEntry::Record:
7992 // The interesting case.
Guy Benyei11169dd2012-12-18 14:30:41 +00007993 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00007994 }
7995
7996 // Read a record.
7997 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00007998 switch ((CommentRecordTypes)Cursor.readRecord(Entry.ID, Record)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007999 case COMMENTS_RAW_COMMENT: {
8000 unsigned Idx = 0;
8001 SourceRange SR = ReadSourceRange(F, Record, Idx);
8002 RawComment::CommentKind Kind =
8003 (RawComment::CommentKind) Record[Idx++];
8004 bool IsTrailingComment = Record[Idx++];
8005 bool IsAlmostTrailingComment = Record[Idx++];
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00008006 Comments.push_back(new (Context) RawComment(
8007 SR, Kind, IsTrailingComment, IsAlmostTrailingComment,
8008 Context.getLangOpts().CommentOpts.ParseAllComments));
Guy Benyei11169dd2012-12-18 14:30:41 +00008009 break;
8010 }
8011 }
8012 }
Dmitri Gribenko9ee0e302014-03-27 15:40:39 +00008013 NextCursor:
8014 Context.Comments.addDeserializedComments(Comments);
Guy Benyei11169dd2012-12-18 14:30:41 +00008015 }
Guy Benyei11169dd2012-12-18 14:30:41 +00008016}
8017
Argyrios Kyrtzidis1bde1172014-11-18 05:24:18 +00008018void ASTReader::getInputFiles(ModuleFile &F,
8019 SmallVectorImpl<serialization::InputFile> &Files) {
8020 for (unsigned I = 0, E = F.InputFilesLoaded.size(); I != E; ++I) {
8021 unsigned ID = I+1;
8022 Files.push_back(getInputFile(F, ID));
8023 }
8024}
8025
Richard Smithcd45dbc2014-04-19 03:48:30 +00008026std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) {
8027 // If we know the owning module, use it.
Richard Smith42413142015-05-15 20:05:43 +00008028 if (Module *M = D->getImportedOwningModule())
Richard Smithcd45dbc2014-04-19 03:48:30 +00008029 return M->getFullModuleName();
8030
8031 // Otherwise, use the name of the top-level module the decl is within.
8032 if (ModuleFile *M = getOwningModuleFile(D))
8033 return M->ModuleName;
8034
8035 // Not from a module.
8036 return "";
8037}
8038
Guy Benyei11169dd2012-12-18 14:30:41 +00008039void ASTReader::finishPendingActions() {
Richard Smith851072e2014-05-19 20:59:20 +00008040 while (!PendingIdentifierInfos.empty() ||
8041 !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() ||
Richard Smith2b9e3e32013-10-18 06:05:18 +00008042 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
Richard Smitha0ce9c42014-07-29 23:23:27 +00008043 !PendingUpdateRecords.empty()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008044 // If any identifiers with corresponding top-level declarations have
8045 // been loaded, load those declarations now.
Craig Topper79be4cd2013-07-05 04:33:53 +00008046 typedef llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2> >
8047 TopLevelDeclsMap;
8048 TopLevelDeclsMap TopLevelDecls;
8049
Guy Benyei11169dd2012-12-18 14:30:41 +00008050 while (!PendingIdentifierInfos.empty()) {
Douglas Gregor6168bd22013-02-18 15:53:43 +00008051 IdentifierInfo *II = PendingIdentifierInfos.back().first;
Richard Smithf0ae3c2d2014-03-28 17:31:23 +00008052 SmallVector<uint32_t, 4> DeclIDs =
8053 std::move(PendingIdentifierInfos.back().second);
Douglas Gregorcb15f082013-02-19 18:26:28 +00008054 PendingIdentifierInfos.pop_back();
Douglas Gregor6168bd22013-02-18 15:53:43 +00008055
8056 SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
Guy Benyei11169dd2012-12-18 14:30:41 +00008057 }
Richard Smithf0ae3c2d2014-03-28 17:31:23 +00008058
Richard Smith851072e2014-05-19 20:59:20 +00008059 // For each decl chain that we wanted to complete while deserializing, mark
8060 // it as "still needs to be completed".
8061 for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
8062 markIncompleteDeclChain(PendingIncompleteDeclChains[I]);
8063 }
8064 PendingIncompleteDeclChains.clear();
8065
Guy Benyei11169dd2012-12-18 14:30:41 +00008066 // Load pending declaration chains.
Richard Smithfe620d22015-03-05 23:24:12 +00008067 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) {
Richard Smithfe620d22015-03-05 23:24:12 +00008068 PendingDeclChainsKnown.erase(PendingDeclChains[I]);
Richard Smithe687bf82015-03-16 20:54:07 +00008069 loadPendingDeclChain(PendingDeclChains[I]);
Richard Smithfe620d22015-03-05 23:24:12 +00008070 }
8071 assert(PendingDeclChainsKnown.empty());
Guy Benyei11169dd2012-12-18 14:30:41 +00008072 PendingDeclChains.clear();
8073
Douglas Gregor6168bd22013-02-18 15:53:43 +00008074 // Make the most recent of the top-level declarations visible.
Craig Topper79be4cd2013-07-05 04:33:53 +00008075 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
8076 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
Douglas Gregor6168bd22013-02-18 15:53:43 +00008077 IdentifierInfo *II = TLD->first;
8078 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +00008079 pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
Douglas Gregor6168bd22013-02-18 15:53:43 +00008080 }
8081 }
8082
Guy Benyei11169dd2012-12-18 14:30:41 +00008083 // Load any pending macro definitions.
8084 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00008085 IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
8086 SmallVector<PendingMacroInfo, 2> GlobalIDs;
8087 GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
8088 // Initialize the macro history from chained-PCHs ahead of module imports.
Richard Smith49f906a2014-03-01 00:08:04 +00008089 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00008090 ++IDIdx) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00008091 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
Richard Smithe842a472014-10-22 02:05:46 +00008092 if (Info.M->Kind != MK_ImplicitModule &&
8093 Info.M->Kind != MK_ExplicitModule)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00008094 resolvePendingMacro(II, Info);
8095 }
8096 // Handle module imports.
Richard Smith49f906a2014-03-01 00:08:04 +00008097 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00008098 ++IDIdx) {
8099 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
Richard Smithe842a472014-10-22 02:05:46 +00008100 if (Info.M->Kind == MK_ImplicitModule ||
8101 Info.M->Kind == MK_ExplicitModule)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00008102 resolvePendingMacro(II, Info);
Guy Benyei11169dd2012-12-18 14:30:41 +00008103 }
8104 }
8105 PendingMacroIDs.clear();
Argyrios Kyrtzidis83a6e3b2013-02-16 00:48:59 +00008106
8107 // Wire up the DeclContexts for Decls that we delayed setting until
8108 // recursive loading is completed.
8109 while (!PendingDeclContextInfos.empty()) {
8110 PendingDeclContextInfo Info = PendingDeclContextInfos.front();
8111 PendingDeclContextInfos.pop_front();
8112 DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
8113 DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
8114 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
8115 }
Richard Smith2b9e3e32013-10-18 06:05:18 +00008116
Richard Smithd1c46742014-04-30 02:24:17 +00008117 // Perform any pending declaration updates.
Richard Smithd6db68c2014-08-07 20:58:41 +00008118 while (!PendingUpdateRecords.empty()) {
Richard Smithd1c46742014-04-30 02:24:17 +00008119 auto Update = PendingUpdateRecords.pop_back_val();
8120 ReadingKindTracker ReadingKind(Read_Decl, *this);
8121 loadDeclUpdateRecords(Update.first, Update.second);
8122 }
Guy Benyei11169dd2012-12-18 14:30:41 +00008123 }
Richard Smith8a639892015-01-24 01:07:20 +00008124
8125 // At this point, all update records for loaded decls are in place, so any
8126 // fake class definitions should have become real.
8127 assert(PendingFakeDefinitionData.empty() &&
8128 "faked up a class definition but never saw the real one");
8129
Guy Benyei11169dd2012-12-18 14:30:41 +00008130 // If we deserialized any C++ or Objective-C class definitions, any
8131 // Objective-C protocol definitions, or any redeclarable templates, make sure
8132 // that all redeclarations point to the definitions. Note that this can only
8133 // happen now, after the redeclaration chains have been fully wired.
Craig Topperc6914d02014-08-25 04:15:02 +00008134 for (Decl *D : PendingDefinitions) {
8135 if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
Richard Smith5b21db82014-04-23 18:20:42 +00008136 if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008137 // Make sure that the TagType points at the definition.
8138 const_cast<TagType*>(TagT)->decl = TD;
8139 }
Richard Smith8ce51082015-03-11 01:44:51 +00008140
Craig Topperc6914d02014-08-25 04:15:02 +00008141 if (auto RD = dyn_cast<CXXRecordDecl>(D)) {
Richard Smith8ce51082015-03-11 01:44:51 +00008142 for (auto *R = getMostRecentExistingDecl(RD); R;
8143 R = R->getPreviousDecl()) {
8144 assert((R == D) ==
8145 cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
Richard Smith2c381642014-08-27 23:11:59 +00008146 "declaration thinks it's the definition but it isn't");
Aaron Ballman86c93902014-03-06 23:45:36 +00008147 cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
Richard Smith2c381642014-08-27 23:11:59 +00008148 }
Guy Benyei11169dd2012-12-18 14:30:41 +00008149 }
8150
8151 continue;
8152 }
Richard Smith8ce51082015-03-11 01:44:51 +00008153
Craig Topperc6914d02014-08-25 04:15:02 +00008154 if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008155 // Make sure that the ObjCInterfaceType points at the definition.
8156 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
8157 ->Decl = ID;
Richard Smith8ce51082015-03-11 01:44:51 +00008158
8159 for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl())
8160 cast<ObjCInterfaceDecl>(R)->Data = ID->Data;
8161
Guy Benyei11169dd2012-12-18 14:30:41 +00008162 continue;
8163 }
Richard Smith8ce51082015-03-11 01:44:51 +00008164
Craig Topperc6914d02014-08-25 04:15:02 +00008165 if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) {
Richard Smith8ce51082015-03-11 01:44:51 +00008166 for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl())
8167 cast<ObjCProtocolDecl>(R)->Data = PD->Data;
8168
Guy Benyei11169dd2012-12-18 14:30:41 +00008169 continue;
8170 }
Richard Smith8ce51082015-03-11 01:44:51 +00008171
Craig Topperc6914d02014-08-25 04:15:02 +00008172 auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl();
Richard Smith8ce51082015-03-11 01:44:51 +00008173 for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl())
8174 cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common;
Guy Benyei11169dd2012-12-18 14:30:41 +00008175 }
8176 PendingDefinitions.clear();
8177
8178 // Load the bodies of any functions or methods we've encountered. We do
8179 // this now (delayed) so that we can be sure that the declaration chains
8180 // have been fully wired up.
Richard Smith8ce51082015-03-11 01:44:51 +00008181 // FIXME: There seems to be no point in delaying this, it does not depend
8182 // on the redecl chains having been wired up.
Guy Benyei11169dd2012-12-18 14:30:41 +00008183 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
8184 PBEnd = PendingBodies.end();
8185 PB != PBEnd; ++PB) {
8186 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
8187 // FIXME: Check for =delete/=default?
8188 // FIXME: Complain about ODR violations here?
8189 if (!getContext().getLangOpts().Modules || !FD->hasBody())
8190 FD->setLazyBody(PB->second);
8191 continue;
8192 }
8193
8194 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
8195 if (!getContext().getLangOpts().Modules || !MD->hasBody())
8196 MD->setLazyBody(PB->second);
8197 }
8198 PendingBodies.clear();
Richard Smith42413142015-05-15 20:05:43 +00008199
8200 // Do some cleanup.
8201 for (auto *ND : PendingMergedDefinitionsToDeduplicate)
8202 getContext().deduplicateMergedDefinitonsFor(ND);
8203 PendingMergedDefinitionsToDeduplicate.clear();
Richard Smitha0ce9c42014-07-29 23:23:27 +00008204}
8205
8206void ASTReader::diagnoseOdrViolations() {
Richard Smithbb853c72014-08-13 01:23:33 +00008207 if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty())
8208 return;
8209
Richard Smitha0ce9c42014-07-29 23:23:27 +00008210 // Trigger the import of the full definition of each class that had any
8211 // odr-merging problems, so we can produce better diagnostics for them.
Richard Smithbb853c72014-08-13 01:23:33 +00008212 // These updates may in turn find and diagnose some ODR failures, so take
8213 // ownership of the set first.
8214 auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
8215 PendingOdrMergeFailures.clear();
8216 for (auto &Merge : OdrMergeFailures) {
Richard Smitha0ce9c42014-07-29 23:23:27 +00008217 Merge.first->buildLookup();
8218 Merge.first->decls_begin();
8219 Merge.first->bases_begin();
8220 Merge.first->vbases_begin();
8221 for (auto *RD : Merge.second) {
8222 RD->decls_begin();
8223 RD->bases_begin();
8224 RD->vbases_begin();
8225 }
8226 }
8227
8228 // For each declaration from a merged context, check that the canonical
8229 // definition of that context also contains a declaration of the same
8230 // entity.
8231 //
8232 // Caution: this loop does things that might invalidate iterators into
8233 // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
8234 while (!PendingOdrMergeChecks.empty()) {
8235 NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
8236
8237 // FIXME: Skip over implicit declarations for now. This matters for things
8238 // like implicitly-declared special member functions. This isn't entirely
8239 // correct; we can end up with multiple unmerged declarations of the same
8240 // implicit entity.
8241 if (D->isImplicit())
8242 continue;
8243
8244 DeclContext *CanonDef = D->getDeclContext();
Richard Smitha0ce9c42014-07-29 23:23:27 +00008245
8246 bool Found = false;
8247 const Decl *DCanon = D->getCanonicalDecl();
8248
Richard Smith01bdb7a2014-08-28 05:44:07 +00008249 for (auto RI : D->redecls()) {
8250 if (RI->getLexicalDeclContext() == CanonDef) {
8251 Found = true;
8252 break;
8253 }
8254 }
8255 if (Found)
8256 continue;
8257
Richard Smitha0ce9c42014-07-29 23:23:27 +00008258 llvm::SmallVector<const NamedDecl*, 4> Candidates;
Richard Smith01bdb7a2014-08-28 05:44:07 +00008259 DeclContext::lookup_result R = CanonDef->lookup(D->getDeclName());
Richard Smitha0ce9c42014-07-29 23:23:27 +00008260 for (DeclContext::lookup_iterator I = R.begin(), E = R.end();
8261 !Found && I != E; ++I) {
8262 for (auto RI : (*I)->redecls()) {
8263 if (RI->getLexicalDeclContext() == CanonDef) {
8264 // This declaration is present in the canonical definition. If it's
8265 // in the same redecl chain, it's the one we're looking for.
8266 if (RI->getCanonicalDecl() == DCanon)
8267 Found = true;
8268 else
8269 Candidates.push_back(cast<NamedDecl>(RI));
8270 break;
8271 }
8272 }
8273 }
8274
8275 if (!Found) {
Richard Smithd08aeb62014-08-28 01:33:39 +00008276 // The AST doesn't like TagDecls becoming invalid after they've been
8277 // completed. We only really need to mark FieldDecls as invalid here.
8278 if (!isa<TagDecl>(D))
8279 D->setInvalidDecl();
Richard Smith4ab3dbd2015-02-13 22:43:51 +00008280
8281 // Ensure we don't accidentally recursively enter deserialization while
8282 // we're producing our diagnostic.
8283 Deserializing RecursionGuard(this);
Richard Smitha0ce9c42014-07-29 23:23:27 +00008284
8285 std::string CanonDefModule =
8286 getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef));
8287 Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
8288 << D << getOwningModuleNameForDiagnostic(D)
8289 << CanonDef << CanonDefModule.empty() << CanonDefModule;
8290
8291 if (Candidates.empty())
8292 Diag(cast<Decl>(CanonDef)->getLocation(),
8293 diag::note_module_odr_violation_no_possible_decls) << D;
8294 else {
8295 for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
8296 Diag(Candidates[I]->getLocation(),
8297 diag::note_module_odr_violation_possible_decl)
8298 << Candidates[I];
8299 }
8300
8301 DiagnosedOdrMergeFailures.insert(CanonDef);
8302 }
8303 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00008304
Richard Smith4ab3dbd2015-02-13 22:43:51 +00008305 if (OdrMergeFailures.empty())
8306 return;
8307
8308 // Ensure we don't accidentally recursively enter deserialization while
8309 // we're producing our diagnostics.
8310 Deserializing RecursionGuard(this);
8311
Richard Smithcd45dbc2014-04-19 03:48:30 +00008312 // Issue any pending ODR-failure diagnostics.
Richard Smithbb853c72014-08-13 01:23:33 +00008313 for (auto &Merge : OdrMergeFailures) {
Richard Smitha0ce9c42014-07-29 23:23:27 +00008314 // If we've already pointed out a specific problem with this class, don't
8315 // bother issuing a general "something's different" diagnostic.
David Blaikie82e95a32014-11-19 07:49:47 +00008316 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
Richard Smithcd45dbc2014-04-19 03:48:30 +00008317 continue;
8318
8319 bool Diagnosed = false;
8320 for (auto *RD : Merge.second) {
8321 // Multiple different declarations got merged together; tell the user
8322 // where they came from.
8323 if (Merge.first != RD) {
8324 // FIXME: Walk the definition, figure out what's different,
8325 // and diagnose that.
8326 if (!Diagnosed) {
8327 std::string Module = getOwningModuleNameForDiagnostic(Merge.first);
8328 Diag(Merge.first->getLocation(),
8329 diag::err_module_odr_violation_different_definitions)
8330 << Merge.first << Module.empty() << Module;
8331 Diagnosed = true;
8332 }
8333
8334 Diag(RD->getLocation(),
8335 diag::note_module_odr_violation_different_definitions)
8336 << getOwningModuleNameForDiagnostic(RD);
8337 }
8338 }
8339
8340 if (!Diagnosed) {
8341 // All definitions are updates to the same declaration. This happens if a
8342 // module instantiates the declaration of a class template specialization
8343 // and two or more other modules instantiate its definition.
8344 //
8345 // FIXME: Indicate which modules had instantiations of this definition.
8346 // FIXME: How can this even happen?
8347 Diag(Merge.first->getLocation(),
8348 diag::err_module_odr_violation_different_instantiations)
8349 << Merge.first;
8350 }
8351 }
Guy Benyei11169dd2012-12-18 14:30:41 +00008352}
8353
8354void ASTReader::FinishedDeserializing() {
8355 assert(NumCurrentElementsDeserializing &&
8356 "FinishedDeserializing not paired with StartedDeserializing");
8357 if (NumCurrentElementsDeserializing == 1) {
8358 // We decrease NumCurrentElementsDeserializing only after pending actions
8359 // are finished, to avoid recursively re-calling finishPendingActions().
8360 finishPendingActions();
8361 }
8362 --NumCurrentElementsDeserializing;
8363
Richard Smitha0ce9c42014-07-29 23:23:27 +00008364 if (NumCurrentElementsDeserializing == 0) {
Richard Smith9e2341d2015-03-23 03:25:59 +00008365 // Propagate exception specification updates along redeclaration chains.
Richard Smith7226f2a2015-03-23 19:54:56 +00008366 while (!PendingExceptionSpecUpdates.empty()) {
8367 auto Updates = std::move(PendingExceptionSpecUpdates);
8368 PendingExceptionSpecUpdates.clear();
8369 for (auto Update : Updates) {
8370 auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
8371 SemaObj->UpdateExceptionSpec(Update.second,
8372 FPT->getExtProtoInfo().ExceptionSpec);
8373 }
Richard Smith9e2341d2015-03-23 03:25:59 +00008374 }
8375
Richard Smitha0ce9c42014-07-29 23:23:27 +00008376 diagnoseOdrViolations();
8377
Richard Smith04d05b52014-03-23 00:27:18 +00008378 // We are not in recursive loading, so it's safe to pass the "interesting"
8379 // decls to the consumer.
Richard Smitha0ce9c42014-07-29 23:23:27 +00008380 if (Consumer)
8381 PassInterestingDeclsToConsumer();
Guy Benyei11169dd2012-12-18 14:30:41 +00008382 }
8383}
8384
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +00008385void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
Richard Smith9e2341d2015-03-23 03:25:59 +00008386 if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
8387 // Remove any fake results before adding any real ones.
8388 auto It = PendingFakeLookupResults.find(II);
8389 if (It != PendingFakeLookupResults.end()) {
8390 for (auto *ND : PendingFakeLookupResults[II])
8391 SemaObj->IdResolver.RemoveDecl(ND);
Ben Langmuireb8bd2d2015-04-10 22:25:42 +00008392 // FIXME: this works around module+PCH performance issue.
8393 // Rather than erase the result from the map, which is O(n), just clear
8394 // the vector of NamedDecls.
8395 It->second.clear();
Richard Smith9e2341d2015-03-23 03:25:59 +00008396 }
8397 }
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +00008398
8399 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
8400 SemaObj->TUScope->AddDecl(D);
8401 } else if (SemaObj->TUScope) {
8402 // Adding the decl to IdResolver may have failed because it was already in
8403 // (even though it was not added in scope). If it is already in, make sure
8404 // it gets in the scope as well.
8405 if (std::find(SemaObj->IdResolver.begin(Name),
8406 SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
8407 SemaObj->TUScope->AddDecl(D);
8408 }
8409}
8410
Adrian Prantlbb165fb2015-06-20 18:53:08 +00008411ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context,
8412 const PCHContainerOperations &PCHContainerOps,
8413 StringRef isysroot, bool DisableValidation,
8414 bool AllowASTWithCompilerErrors,
Nico Weber824285e2014-05-08 04:26:47 +00008415 bool AllowConfigurationMismatch, bool ValidateSystemInputs,
Ben Langmuir2cb4a782014-02-05 22:21:15 +00008416 bool UseGlobalIndex)
Craig Toppera13603a2014-05-22 05:54:18 +00008417 : Listener(new PCHValidator(PP, *this)), DeserializationListener(nullptr),
Nico Weber824285e2014-05-08 04:26:47 +00008418 OwnsDeserializationListener(false), SourceMgr(PP.getSourceManager()),
Adrian Prantlbb165fb2015-06-20 18:53:08 +00008419 FileMgr(PP.getFileManager()), PCHContainerOps(PCHContainerOps),
8420 Diags(PP.getDiagnostics()), SemaObj(nullptr), PP(PP), Context(Context),
8421 Consumer(nullptr), ModuleMgr(PP.getFileManager(), PCHContainerOps),
8422 isysroot(isysroot), DisableValidation(DisableValidation),
Nico Weber824285e2014-05-08 04:26:47 +00008423 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
8424 AllowConfigurationMismatch(AllowConfigurationMismatch),
8425 ValidateSystemInputs(ValidateSystemInputs),
8426 UseGlobalIndex(UseGlobalIndex), TriedLoadingGlobalIndex(false),
Adrian Prantlbb165fb2015-06-20 18:53:08 +00008427 CurrSwitchCaseStmts(&SwitchCaseStmts), NumSLocEntriesRead(0),
8428 TotalNumSLocEntries(0), NumStatementsRead(0), TotalNumStatements(0),
8429 NumMacrosRead(0), TotalNumMacros(0), NumIdentifierLookups(0),
8430 NumIdentifierLookupHits(0), NumSelectorsRead(0),
Nico Weber824285e2014-05-08 04:26:47 +00008431 NumMethodPoolEntriesRead(0), NumMethodPoolLookups(0),
8432 NumMethodPoolHits(0), NumMethodPoolTableLookups(0),
8433 NumMethodPoolTableHits(0), TotalNumMethodPoolEntries(0),
8434 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
8435 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
8436 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
Richard Smithc2bb8182015-03-24 06:36:48 +00008437 PassingDeclsToConsumer(false), ReadingKind(Read_None) {
Guy Benyei11169dd2012-12-18 14:30:41 +00008438 SourceMgr.setExternalSLocEntrySource(this);
8439}
8440
8441ASTReader::~ASTReader() {
Nico Weber824285e2014-05-08 04:26:47 +00008442 if (OwnsDeserializationListener)
8443 delete DeserializationListener;
8444
Guy Benyei11169dd2012-12-18 14:30:41 +00008445 for (DeclContextVisibleUpdatesPending::iterator
8446 I = PendingVisibleUpdates.begin(),
8447 E = PendingVisibleUpdates.end();
8448 I != E; ++I) {
8449 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
8450 F = I->second.end();
8451 J != F; ++J)
8452 delete J->first;
8453 }
8454}