blob: 61576fc8b57328f2527c5232582d02815afa4aac [file] [log] [blame]
Nick Lewyckyf0f56162013-01-31 03:23:57 +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"
22#include "clang/AST/NestedNameSpecifier.h"
23#include "clang/AST/Type.h"
24#include "clang/AST/TypeLocVisitor.h"
25#include "clang/Basic/FileManager.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000026#include "clang/Basic/SourceManager.h"
27#include "clang/Basic/SourceManagerInternals.h"
28#include "clang/Basic/TargetInfo.h"
29#include "clang/Basic/TargetOptions.h"
30#include "clang/Basic/Version.h"
31#include "clang/Basic/VersionTuple.h"
32#include "clang/Lex/HeaderSearch.h"
33#include "clang/Lex/HeaderSearchOptions.h"
34#include "clang/Lex/MacroInfo.h"
35#include "clang/Lex/PreprocessingRecord.h"
36#include "clang/Lex/Preprocessor.h"
37#include "clang/Lex/PreprocessorOptions.h"
38#include "clang/Sema/Scope.h"
39#include "clang/Sema/Sema.h"
40#include "clang/Serialization/ASTDeserializationListener.h"
Douglas Gregore060e572013-01-25 01:03:03 +000041#include "clang/Serialization/GlobalModuleIndex.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000042#include "clang/Serialization/ModuleManager.h"
43#include "clang/Serialization/SerializationDiagnostic.h"
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +000044#include "llvm/ADT/Hashing.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000045#include "llvm/ADT/StringExtras.h"
46#include "llvm/Bitcode/BitstreamReader.h"
47#include "llvm/Support/ErrorHandling.h"
48#include "llvm/Support/FileSystem.h"
49#include "llvm/Support/MemoryBuffer.h"
50#include "llvm/Support/Path.h"
51#include "llvm/Support/SaveAndRestore.h"
Dmitri Gribenkof430da42014-02-12 10:33:14 +000052#include "llvm/Support/raw_ostream.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000053#include "llvm/Support/system_error.h"
54#include <algorithm>
Chris Lattner91f373e2013-01-20 00:57:52 +000055#include <cstdio>
Guy Benyei11169dd2012-12-18 14:30:41 +000056#include <iterator>
57
58using namespace clang;
59using namespace clang::serialization;
60using namespace clang::serialization::reader;
Chris Lattner7fb3bef2013-01-20 00:56:42 +000061using llvm::BitstreamCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +000062
Ben Langmuircb69b572014-03-07 06:40:32 +000063
64//===----------------------------------------------------------------------===//
65// ChainedASTReaderListener implementation
66//===----------------------------------------------------------------------===//
67
68bool
69ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) {
70 return First->ReadFullVersionInformation(FullVersion) ||
71 Second->ReadFullVersionInformation(FullVersion);
72}
73bool ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts,
74 bool Complain) {
75 return First->ReadLanguageOptions(LangOpts, Complain) ||
76 Second->ReadLanguageOptions(LangOpts, Complain);
77}
78bool
79ChainedASTReaderListener::ReadTargetOptions(const TargetOptions &TargetOpts,
80 bool Complain) {
81 return First->ReadTargetOptions(TargetOpts, Complain) ||
82 Second->ReadTargetOptions(TargetOpts, Complain);
83}
84bool ChainedASTReaderListener::ReadDiagnosticOptions(
85 const DiagnosticOptions &DiagOpts, bool Complain) {
86 return First->ReadDiagnosticOptions(DiagOpts, Complain) ||
87 Second->ReadDiagnosticOptions(DiagOpts, Complain);
88}
89bool
90ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts,
91 bool Complain) {
92 return First->ReadFileSystemOptions(FSOpts, Complain) ||
93 Second->ReadFileSystemOptions(FSOpts, Complain);
94}
95
96bool ChainedASTReaderListener::ReadHeaderSearchOptions(
97 const HeaderSearchOptions &HSOpts, bool Complain) {
98 return First->ReadHeaderSearchOptions(HSOpts, Complain) ||
99 Second->ReadHeaderSearchOptions(HSOpts, Complain);
100}
101bool ChainedASTReaderListener::ReadPreprocessorOptions(
102 const PreprocessorOptions &PPOpts, bool Complain,
103 std::string &SuggestedPredefines) {
104 return First->ReadPreprocessorOptions(PPOpts, Complain,
105 SuggestedPredefines) ||
106 Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines);
107}
108void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M,
109 unsigned Value) {
110 First->ReadCounter(M, Value);
111 Second->ReadCounter(M, Value);
112}
113bool ChainedASTReaderListener::needsInputFileVisitation() {
114 return First->needsInputFileVisitation() ||
115 Second->needsInputFileVisitation();
116}
117bool ChainedASTReaderListener::needsSystemInputFileVisitation() {
118 return First->needsSystemInputFileVisitation() ||
119 Second->needsSystemInputFileVisitation();
120}
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +0000121void ChainedASTReaderListener::visitModuleFile(StringRef Filename) {
122 First->visitModuleFile(Filename);
123 Second->visitModuleFile(Filename);
124}
Ben Langmuircb69b572014-03-07 06:40:32 +0000125bool ChainedASTReaderListener::visitInputFile(StringRef Filename,
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +0000126 bool isSystem,
127 bool isOverridden) {
128 return First->visitInputFile(Filename, isSystem, isOverridden) ||
129 Second->visitInputFile(Filename, isSystem, isOverridden);
Ben Langmuircb69b572014-03-07 06:40:32 +0000130}
131
Guy Benyei11169dd2012-12-18 14:30:41 +0000132//===----------------------------------------------------------------------===//
133// PCH validator implementation
134//===----------------------------------------------------------------------===//
135
136ASTReaderListener::~ASTReaderListener() {}
137
138/// \brief Compare the given set of language options against an existing set of
139/// language options.
140///
141/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
142///
143/// \returns true if the languagae options mis-match, false otherwise.
144static bool checkLanguageOptions(const LangOptions &LangOpts,
145 const LangOptions &ExistingLangOpts,
146 DiagnosticsEngine *Diags) {
147#define LANGOPT(Name, Bits, Default, Description) \
148 if (ExistingLangOpts.Name != LangOpts.Name) { \
149 if (Diags) \
150 Diags->Report(diag::err_pch_langopt_mismatch) \
151 << Description << LangOpts.Name << ExistingLangOpts.Name; \
152 return true; \
153 }
154
155#define VALUE_LANGOPT(Name, Bits, Default, Description) \
156 if (ExistingLangOpts.Name != LangOpts.Name) { \
157 if (Diags) \
158 Diags->Report(diag::err_pch_langopt_value_mismatch) \
159 << Description; \
160 return true; \
161 }
162
163#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
164 if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \
165 if (Diags) \
166 Diags->Report(diag::err_pch_langopt_value_mismatch) \
167 << Description; \
168 return true; \
169 }
170
171#define BENIGN_LANGOPT(Name, Bits, Default, Description)
172#define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
173#include "clang/Basic/LangOptions.def"
174
175 if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
176 if (Diags)
177 Diags->Report(diag::err_pch_langopt_value_mismatch)
178 << "target Objective-C runtime";
179 return true;
180 }
181
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +0000182 if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
183 LangOpts.CommentOpts.BlockCommandNames) {
184 if (Diags)
185 Diags->Report(diag::err_pch_langopt_value_mismatch)
186 << "block command names";
187 return true;
188 }
189
Guy Benyei11169dd2012-12-18 14:30:41 +0000190 return false;
191}
192
193/// \brief Compare the given set of target options against an existing set of
194/// target options.
195///
196/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
197///
198/// \returns true if the target options mis-match, false otherwise.
199static bool checkTargetOptions(const TargetOptions &TargetOpts,
200 const TargetOptions &ExistingTargetOpts,
201 DiagnosticsEngine *Diags) {
202#define CHECK_TARGET_OPT(Field, Name) \
203 if (TargetOpts.Field != ExistingTargetOpts.Field) { \
204 if (Diags) \
205 Diags->Report(diag::err_pch_targetopt_mismatch) \
206 << Name << TargetOpts.Field << ExistingTargetOpts.Field; \
207 return true; \
208 }
209
210 CHECK_TARGET_OPT(Triple, "target");
211 CHECK_TARGET_OPT(CPU, "target CPU");
212 CHECK_TARGET_OPT(ABI, "target ABI");
Guy Benyei11169dd2012-12-18 14:30:41 +0000213 CHECK_TARGET_OPT(LinkerVersion, "target linker version");
214#undef CHECK_TARGET_OPT
215
216 // Compare feature sets.
217 SmallVector<StringRef, 4> ExistingFeatures(
218 ExistingTargetOpts.FeaturesAsWritten.begin(),
219 ExistingTargetOpts.FeaturesAsWritten.end());
220 SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
221 TargetOpts.FeaturesAsWritten.end());
222 std::sort(ExistingFeatures.begin(), ExistingFeatures.end());
223 std::sort(ReadFeatures.begin(), ReadFeatures.end());
224
225 unsigned ExistingIdx = 0, ExistingN = ExistingFeatures.size();
226 unsigned ReadIdx = 0, ReadN = ReadFeatures.size();
227 while (ExistingIdx < ExistingN && ReadIdx < ReadN) {
228 if (ExistingFeatures[ExistingIdx] == ReadFeatures[ReadIdx]) {
229 ++ExistingIdx;
230 ++ReadIdx;
231 continue;
232 }
233
234 if (ReadFeatures[ReadIdx] < ExistingFeatures[ExistingIdx]) {
235 if (Diags)
236 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
237 << false << ReadFeatures[ReadIdx];
238 return true;
239 }
240
241 if (Diags)
242 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
243 << true << ExistingFeatures[ExistingIdx];
244 return true;
245 }
246
247 if (ExistingIdx < ExistingN) {
248 if (Diags)
249 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
250 << true << ExistingFeatures[ExistingIdx];
251 return true;
252 }
253
254 if (ReadIdx < ReadN) {
255 if (Diags)
256 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
257 << false << ReadFeatures[ReadIdx];
258 return true;
259 }
260
261 return false;
262}
263
264bool
265PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
266 bool Complain) {
267 const LangOptions &ExistingLangOpts = PP.getLangOpts();
268 return checkLanguageOptions(LangOpts, ExistingLangOpts,
269 Complain? &Reader.Diags : 0);
270}
271
272bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
273 bool Complain) {
274 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
275 return checkTargetOptions(TargetOpts, ExistingTargetOpts,
276 Complain? &Reader.Diags : 0);
277}
278
279namespace {
280 typedef llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> >
281 MacroDefinitionsMap;
Craig Topper3598eb72013-07-05 04:43:31 +0000282 typedef llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> >
283 DeclsMap;
Guy Benyei11169dd2012-12-18 14:30:41 +0000284}
285
286/// \brief Collect the macro definitions provided by the given preprocessor
287/// options.
288static void collectMacroDefinitions(const PreprocessorOptions &PPOpts,
289 MacroDefinitionsMap &Macros,
290 SmallVectorImpl<StringRef> *MacroNames = 0){
291 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
292 StringRef Macro = PPOpts.Macros[I].first;
293 bool IsUndef = PPOpts.Macros[I].second;
294
295 std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
296 StringRef MacroName = MacroPair.first;
297 StringRef MacroBody = MacroPair.second;
298
299 // For an #undef'd macro, we only care about the name.
300 if (IsUndef) {
301 if (MacroNames && !Macros.count(MacroName))
302 MacroNames->push_back(MacroName);
303
304 Macros[MacroName] = std::make_pair("", true);
305 continue;
306 }
307
308 // For a #define'd macro, figure out the actual definition.
309 if (MacroName.size() == Macro.size())
310 MacroBody = "1";
311 else {
312 // Note: GCC drops anything following an end-of-line character.
313 StringRef::size_type End = MacroBody.find_first_of("\n\r");
314 MacroBody = MacroBody.substr(0, End);
315 }
316
317 if (MacroNames && !Macros.count(MacroName))
318 MacroNames->push_back(MacroName);
319 Macros[MacroName] = std::make_pair(MacroBody, false);
320 }
321}
322
323/// \brief Check the preprocessor options deserialized from the control block
324/// against the preprocessor options in an existing preprocessor.
325///
326/// \param Diags If non-null, produce diagnostics for any mismatches incurred.
327static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts,
328 const PreprocessorOptions &ExistingPPOpts,
329 DiagnosticsEngine *Diags,
330 FileManager &FileMgr,
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +0000331 std::string &SuggestedPredefines,
332 const LangOptions &LangOpts) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000333 // Check macro definitions.
334 MacroDefinitionsMap ASTFileMacros;
335 collectMacroDefinitions(PPOpts, ASTFileMacros);
336 MacroDefinitionsMap ExistingMacros;
337 SmallVector<StringRef, 4> ExistingMacroNames;
338 collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames);
339
340 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
341 // Dig out the macro definition in the existing preprocessor options.
342 StringRef MacroName = ExistingMacroNames[I];
343 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
344
345 // Check whether we know anything about this macro name or not.
346 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> >::iterator Known
347 = ASTFileMacros.find(MacroName);
348 if (Known == ASTFileMacros.end()) {
349 // FIXME: Check whether this identifier was referenced anywhere in the
350 // AST file. If so, we should reject the AST file. Unfortunately, this
351 // information isn't in the control block. What shall we do about it?
352
353 if (Existing.second) {
354 SuggestedPredefines += "#undef ";
355 SuggestedPredefines += MacroName.str();
356 SuggestedPredefines += '\n';
357 } else {
358 SuggestedPredefines += "#define ";
359 SuggestedPredefines += MacroName.str();
360 SuggestedPredefines += ' ';
361 SuggestedPredefines += Existing.first.str();
362 SuggestedPredefines += '\n';
363 }
364 continue;
365 }
366
367 // If the macro was defined in one but undef'd in the other, we have a
368 // conflict.
369 if (Existing.second != Known->second.second) {
370 if (Diags) {
371 Diags->Report(diag::err_pch_macro_def_undef)
372 << MacroName << Known->second.second;
373 }
374 return true;
375 }
376
377 // If the macro was #undef'd in both, or if the macro bodies are identical,
378 // it's fine.
379 if (Existing.second || Existing.first == Known->second.first)
380 continue;
381
382 // The macro bodies differ; complain.
383 if (Diags) {
384 Diags->Report(diag::err_pch_macro_def_conflict)
385 << MacroName << Known->second.first << Existing.first;
386 }
387 return true;
388 }
389
390 // Check whether we're using predefines.
391 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines) {
392 if (Diags) {
393 Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
394 }
395 return true;
396 }
397
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +0000398 // Detailed record is important since it is used for the module cache hash.
399 if (LangOpts.Modules &&
400 PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord) {
401 if (Diags) {
402 Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord;
403 }
404 return true;
405 }
406
Guy Benyei11169dd2012-12-18 14:30:41 +0000407 // Compute the #include and #include_macros lines we need.
408 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
409 StringRef File = ExistingPPOpts.Includes[I];
410 if (File == ExistingPPOpts.ImplicitPCHInclude)
411 continue;
412
413 if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File)
414 != PPOpts.Includes.end())
415 continue;
416
417 SuggestedPredefines += "#include \"";
418 SuggestedPredefines +=
419 HeaderSearch::NormalizeDashIncludePath(File, FileMgr);
420 SuggestedPredefines += "\"\n";
421 }
422
423 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
424 StringRef File = ExistingPPOpts.MacroIncludes[I];
425 if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(),
426 File)
427 != PPOpts.MacroIncludes.end())
428 continue;
429
430 SuggestedPredefines += "#__include_macros \"";
431 SuggestedPredefines +=
432 HeaderSearch::NormalizeDashIncludePath(File, FileMgr);
433 SuggestedPredefines += "\"\n##\n";
434 }
435
436 return false;
437}
438
439bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
440 bool Complain,
441 std::string &SuggestedPredefines) {
442 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
443
444 return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
445 Complain? &Reader.Diags : 0,
446 PP.getFileManager(),
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +0000447 SuggestedPredefines,
448 PP.getLangOpts());
Guy Benyei11169dd2012-12-18 14:30:41 +0000449}
450
Guy Benyei11169dd2012-12-18 14:30:41 +0000451void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
452 PP.setCounterValue(Value);
453}
454
455//===----------------------------------------------------------------------===//
456// AST reader implementation
457//===----------------------------------------------------------------------===//
458
459void
460ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
461 DeserializationListener = Listener;
462}
463
464
465
466unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
467 return serialization::ComputeHash(Sel);
468}
469
470
471std::pair<unsigned, unsigned>
472ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
473 using namespace clang::io;
474 unsigned KeyLen = ReadUnalignedLE16(d);
475 unsigned DataLen = ReadUnalignedLE16(d);
476 return std::make_pair(KeyLen, DataLen);
477}
478
479ASTSelectorLookupTrait::internal_key_type
480ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
481 using namespace clang::io;
482 SelectorTable &SelTable = Reader.getContext().Selectors;
483 unsigned N = ReadUnalignedLE16(d);
484 IdentifierInfo *FirstII
Douglas Gregorc8a992f2013-01-21 16:52:34 +0000485 = Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
Guy Benyei11169dd2012-12-18 14:30:41 +0000486 if (N == 0)
487 return SelTable.getNullarySelector(FirstII);
488 else if (N == 1)
489 return SelTable.getUnarySelector(FirstII);
490
491 SmallVector<IdentifierInfo *, 16> Args;
492 Args.push_back(FirstII);
493 for (unsigned I = 1; I != N; ++I)
Douglas Gregorc8a992f2013-01-21 16:52:34 +0000494 Args.push_back(Reader.getLocalIdentifier(F, ReadUnalignedLE32(d)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000495
496 return SelTable.getSelector(N, Args.data());
497}
498
499ASTSelectorLookupTrait::data_type
500ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
501 unsigned DataLen) {
502 using namespace clang::io;
503
504 data_type Result;
505
506 Result.ID = Reader.getGlobalSelectorID(F, ReadUnalignedLE32(d));
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +0000507 unsigned NumInstanceMethodsAndBits = ReadUnalignedLE16(d);
508 unsigned NumFactoryMethodsAndBits = ReadUnalignedLE16(d);
509 Result.InstanceBits = NumInstanceMethodsAndBits & 0x3;
510 Result.FactoryBits = NumFactoryMethodsAndBits & 0x3;
511 unsigned NumInstanceMethods = NumInstanceMethodsAndBits >> 2;
512 unsigned NumFactoryMethods = NumFactoryMethodsAndBits >> 2;
Guy Benyei11169dd2012-12-18 14:30:41 +0000513
514 // Load instance methods
515 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
516 if (ObjCMethodDecl *Method
517 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d)))
518 Result.Instance.push_back(Method);
519 }
520
521 // Load factory methods
522 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
523 if (ObjCMethodDecl *Method
524 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d)))
525 Result.Factory.push_back(Method);
526 }
527
528 return Result;
529}
530
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000531unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
532 return llvm::HashString(a);
Guy Benyei11169dd2012-12-18 14:30:41 +0000533}
534
535std::pair<unsigned, unsigned>
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000536ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000537 using namespace clang::io;
538 unsigned DataLen = ReadUnalignedLE16(d);
539 unsigned KeyLen = ReadUnalignedLE16(d);
540 return std::make_pair(KeyLen, DataLen);
541}
542
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000543ASTIdentifierLookupTraitBase::internal_key_type
544ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000545 assert(n >= 2 && d[n-1] == '\0');
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000546 return StringRef((const char*) d, n-1);
Guy Benyei11169dd2012-12-18 14:30:41 +0000547}
548
Douglas Gregordcf25082013-02-11 18:16:18 +0000549/// \brief Whether the given identifier is "interesting".
550static bool isInterestingIdentifier(IdentifierInfo &II) {
551 return II.isPoisoned() ||
552 II.isExtensionToken() ||
553 II.getObjCOrBuiltinID() ||
554 II.hasRevertedTokenIDToIdentifier() ||
555 II.hadMacroDefinition() ||
556 II.getFETokenInfo<void>();
557}
558
Guy Benyei11169dd2012-12-18 14:30:41 +0000559IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
560 const unsigned char* d,
561 unsigned DataLen) {
562 using namespace clang::io;
563 unsigned RawID = ReadUnalignedLE32(d);
564 bool IsInteresting = RawID & 0x01;
565
566 // Wipe out the "is interesting" bit.
567 RawID = RawID >> 1;
568
569 IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
570 if (!IsInteresting) {
571 // For uninteresting identifiers, just build the IdentifierInfo
572 // and associate it with the persistent ID.
573 IdentifierInfo *II = KnownII;
574 if (!II) {
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000575 II = &Reader.getIdentifierTable().getOwn(k);
Guy Benyei11169dd2012-12-18 14:30:41 +0000576 KnownII = II;
577 }
578 Reader.SetIdentifierInfo(ID, II);
Douglas Gregordcf25082013-02-11 18:16:18 +0000579 if (!II->isFromAST()) {
580 bool WasInteresting = isInterestingIdentifier(*II);
581 II->setIsFromAST();
582 if (WasInteresting)
583 II->setChangedSinceDeserialization();
584 }
585 Reader.markIdentifierUpToDate(II);
Guy Benyei11169dd2012-12-18 14:30:41 +0000586 return II;
587 }
588
589 unsigned ObjCOrBuiltinID = ReadUnalignedLE16(d);
590 unsigned Bits = ReadUnalignedLE16(d);
591 bool CPlusPlusOperatorKeyword = Bits & 0x01;
592 Bits >>= 1;
593 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
594 Bits >>= 1;
595 bool Poisoned = Bits & 0x01;
596 Bits >>= 1;
597 bool ExtensionToken = Bits & 0x01;
598 Bits >>= 1;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +0000599 bool hasSubmoduleMacros = Bits & 0x01;
600 Bits >>= 1;
Guy Benyei11169dd2012-12-18 14:30:41 +0000601 bool hadMacroDefinition = Bits & 0x01;
602 Bits >>= 1;
603
604 assert(Bits == 0 && "Extra bits in the identifier?");
605 DataLen -= 8;
606
607 // Build the IdentifierInfo itself and link the identifier ID with
608 // the new IdentifierInfo.
609 IdentifierInfo *II = KnownII;
610 if (!II) {
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000611 II = &Reader.getIdentifierTable().getOwn(StringRef(k));
Guy Benyei11169dd2012-12-18 14:30:41 +0000612 KnownII = II;
613 }
614 Reader.markIdentifierUpToDate(II);
Douglas Gregordcf25082013-02-11 18:16:18 +0000615 if (!II->isFromAST()) {
616 bool WasInteresting = isInterestingIdentifier(*II);
617 II->setIsFromAST();
618 if (WasInteresting)
619 II->setChangedSinceDeserialization();
620 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000621
622 // Set or check the various bits in the IdentifierInfo structure.
623 // Token IDs are read-only.
Argyrios Kyrtzidisddee8c92013-02-27 01:13:51 +0000624 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
Guy Benyei11169dd2012-12-18 14:30:41 +0000625 II->RevertTokenIDToIdentifier();
626 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
627 assert(II->isExtensionToken() == ExtensionToken &&
628 "Incorrect extension token flag");
629 (void)ExtensionToken;
630 if (Poisoned)
631 II->setIsPoisoned(true);
632 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
633 "Incorrect C++ operator keyword flag");
634 (void)CPlusPlusOperatorKeyword;
635
636 // If this identifier is a macro, deserialize the macro
637 // definition.
638 if (hadMacroDefinition) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +0000639 uint32_t MacroDirectivesOffset = ReadUnalignedLE32(d);
640 DataLen -= 4;
641 SmallVector<uint32_t, 8> LocalMacroIDs;
642 if (hasSubmoduleMacros) {
643 while (uint32_t LocalMacroID = ReadUnalignedLE32(d)) {
644 DataLen -= 4;
645 LocalMacroIDs.push_back(LocalMacroID);
646 }
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +0000647 DataLen -= 4;
648 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +0000649
650 if (F.Kind == MK_Module) {
Richard Smith49f906a2014-03-01 00:08:04 +0000651 // Macro definitions are stored from newest to oldest, so reverse them
652 // before registering them.
653 llvm::SmallVector<unsigned, 8> MacroSizes;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +0000654 for (SmallVectorImpl<uint32_t>::iterator
Richard Smith49f906a2014-03-01 00:08:04 +0000655 I = LocalMacroIDs.begin(), E = LocalMacroIDs.end(); I != E; /**/) {
656 unsigned Size = 1;
657
658 static const uint32_t HasOverridesFlag = 0x80000000U;
659 if (I + 1 != E && (I[1] & HasOverridesFlag))
660 Size += 1 + (I[1] & ~HasOverridesFlag);
661
662 MacroSizes.push_back(Size);
663 I += Size;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +0000664 }
Richard Smith49f906a2014-03-01 00:08:04 +0000665
666 SmallVectorImpl<uint32_t>::iterator I = LocalMacroIDs.end();
667 for (SmallVectorImpl<unsigned>::reverse_iterator SI = MacroSizes.rbegin(),
668 SE = MacroSizes.rend();
669 SI != SE; ++SI) {
670 I -= *SI;
671
672 uint32_t LocalMacroID = *I;
673 llvm::ArrayRef<uint32_t> Overrides;
674 if (*SI != 1)
675 Overrides = llvm::makeArrayRef(&I[2], *SI - 2);
676 Reader.addPendingMacroFromModule(II, &F, LocalMacroID, Overrides);
677 }
678 assert(I == LocalMacroIDs.begin());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +0000679 } else {
680 Reader.addPendingMacroFromPCH(II, &F, MacroDirectivesOffset);
681 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000682 }
683
684 Reader.SetIdentifierInfo(ID, II);
685
686 // Read all of the declarations visible at global scope with this
687 // name.
688 if (DataLen > 0) {
689 SmallVector<uint32_t, 4> DeclIDs;
690 for (; DataLen > 0; DataLen -= 4)
691 DeclIDs.push_back(Reader.getGlobalDeclID(F, ReadUnalignedLE32(d)));
692 Reader.SetGloballyVisibleDecls(II, DeclIDs);
693 }
694
695 return II;
696}
697
698unsigned
699ASTDeclContextNameLookupTrait::ComputeHash(const DeclNameKey &Key) const {
700 llvm::FoldingSetNodeID ID;
701 ID.AddInteger(Key.Kind);
702
703 switch (Key.Kind) {
704 case DeclarationName::Identifier:
705 case DeclarationName::CXXLiteralOperatorName:
706 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
707 break;
708 case DeclarationName::ObjCZeroArgSelector:
709 case DeclarationName::ObjCOneArgSelector:
710 case DeclarationName::ObjCMultiArgSelector:
711 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
712 break;
713 case DeclarationName::CXXOperatorName:
714 ID.AddInteger((OverloadedOperatorKind)Key.Data);
715 break;
716 case DeclarationName::CXXConstructorName:
717 case DeclarationName::CXXDestructorName:
718 case DeclarationName::CXXConversionFunctionName:
719 case DeclarationName::CXXUsingDirective:
720 break;
721 }
722
723 return ID.ComputeHash();
724}
725
726ASTDeclContextNameLookupTrait::internal_key_type
727ASTDeclContextNameLookupTrait::GetInternalKey(
728 const external_key_type& Name) const {
729 DeclNameKey Key;
730 Key.Kind = Name.getNameKind();
731 switch (Name.getNameKind()) {
732 case DeclarationName::Identifier:
733 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
734 break;
735 case DeclarationName::ObjCZeroArgSelector:
736 case DeclarationName::ObjCOneArgSelector:
737 case DeclarationName::ObjCMultiArgSelector:
738 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
739 break;
740 case DeclarationName::CXXOperatorName:
741 Key.Data = Name.getCXXOverloadedOperator();
742 break;
743 case DeclarationName::CXXLiteralOperatorName:
744 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
745 break;
746 case DeclarationName::CXXConstructorName:
747 case DeclarationName::CXXDestructorName:
748 case DeclarationName::CXXConversionFunctionName:
749 case DeclarationName::CXXUsingDirective:
750 Key.Data = 0;
751 break;
752 }
753
754 return Key;
755}
756
757std::pair<unsigned, unsigned>
758ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
759 using namespace clang::io;
760 unsigned KeyLen = ReadUnalignedLE16(d);
761 unsigned DataLen = ReadUnalignedLE16(d);
762 return std::make_pair(KeyLen, DataLen);
763}
764
765ASTDeclContextNameLookupTrait::internal_key_type
766ASTDeclContextNameLookupTrait::ReadKey(const unsigned char* d, unsigned) {
767 using namespace clang::io;
768
769 DeclNameKey Key;
770 Key.Kind = (DeclarationName::NameKind)*d++;
771 switch (Key.Kind) {
772 case DeclarationName::Identifier:
773 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
774 break;
775 case DeclarationName::ObjCZeroArgSelector:
776 case DeclarationName::ObjCOneArgSelector:
777 case DeclarationName::ObjCMultiArgSelector:
778 Key.Data =
779 (uint64_t)Reader.getLocalSelector(F, ReadUnalignedLE32(d))
780 .getAsOpaquePtr();
781 break;
782 case DeclarationName::CXXOperatorName:
783 Key.Data = *d++; // OverloadedOperatorKind
784 break;
785 case DeclarationName::CXXLiteralOperatorName:
786 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
787 break;
788 case DeclarationName::CXXConstructorName:
789 case DeclarationName::CXXDestructorName:
790 case DeclarationName::CXXConversionFunctionName:
791 case DeclarationName::CXXUsingDirective:
792 Key.Data = 0;
793 break;
794 }
795
796 return Key;
797}
798
799ASTDeclContextNameLookupTrait::data_type
800ASTDeclContextNameLookupTrait::ReadData(internal_key_type,
801 const unsigned char* d,
802 unsigned DataLen) {
803 using namespace clang::io;
804 unsigned NumDecls = ReadUnalignedLE16(d);
Argyrios Kyrtzidisc57e5032013-01-11 22:29:49 +0000805 LE32DeclID *Start = reinterpret_cast<LE32DeclID *>(
806 const_cast<unsigned char *>(d));
Guy Benyei11169dd2012-12-18 14:30:41 +0000807 return std::make_pair(Start, Start + NumDecls);
808}
809
810bool ASTReader::ReadDeclContextStorage(ModuleFile &M,
Chris Lattner7fb3bef2013-01-20 00:56:42 +0000811 BitstreamCursor &Cursor,
Guy Benyei11169dd2012-12-18 14:30:41 +0000812 const std::pair<uint64_t, uint64_t> &Offsets,
813 DeclContextInfo &Info) {
814 SavedStreamPosition SavedPosition(Cursor);
815 // First the lexical decls.
816 if (Offsets.first != 0) {
817 Cursor.JumpToBit(Offsets.first);
818
819 RecordData Record;
Chris Lattner0e6c9402013-01-20 02:38:54 +0000820 StringRef Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +0000821 unsigned Code = Cursor.ReadCode();
Chris Lattner0e6c9402013-01-20 02:38:54 +0000822 unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +0000823 if (RecCode != DECL_CONTEXT_LEXICAL) {
824 Error("Expected lexical block");
825 return true;
826 }
827
Chris Lattner0e6c9402013-01-20 02:38:54 +0000828 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob.data());
829 Info.NumLexicalDecls = Blob.size() / sizeof(KindDeclIDPair);
Guy Benyei11169dd2012-12-18 14:30:41 +0000830 }
831
832 // Now the lookup table.
833 if (Offsets.second != 0) {
834 Cursor.JumpToBit(Offsets.second);
835
836 RecordData Record;
Chris Lattner0e6c9402013-01-20 02:38:54 +0000837 StringRef Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +0000838 unsigned Code = Cursor.ReadCode();
Chris Lattner0e6c9402013-01-20 02:38:54 +0000839 unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +0000840 if (RecCode != DECL_CONTEXT_VISIBLE) {
841 Error("Expected visible lookup table block");
842 return true;
843 }
Richard Smith52e3fba2014-03-11 07:17:35 +0000844 Info.NameLookupTableData
845 = ASTDeclContextNameLookupTable::Create(
846 (const unsigned char *)Blob.data() + Record[0],
847 (const unsigned char *)Blob.data(),
848 ASTDeclContextNameLookupTrait(*this, M));
Guy Benyei11169dd2012-12-18 14:30:41 +0000849 }
850
851 return false;
852}
853
854void ASTReader::Error(StringRef Msg) {
855 Error(diag::err_fe_pch_malformed, Msg);
Douglas Gregor940e8052013-05-10 22:15:13 +0000856 if (Context.getLangOpts().Modules && !Diags.isDiagnosticInFlight()) {
857 Diag(diag::note_module_cache_path)
858 << PP.getHeaderSearchInfo().getModuleCachePath();
859 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000860}
861
862void ASTReader::Error(unsigned DiagID,
863 StringRef Arg1, StringRef Arg2) {
864 if (Diags.isDiagnosticInFlight())
865 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
866 else
867 Diag(DiagID) << Arg1 << Arg2;
868}
869
870//===----------------------------------------------------------------------===//
871// Source Manager Deserialization
872//===----------------------------------------------------------------------===//
873
874/// \brief Read the line table in the source manager block.
875/// \returns true if there was an error.
876bool ASTReader::ParseLineTable(ModuleFile &F,
877 SmallVectorImpl<uint64_t> &Record) {
878 unsigned Idx = 0;
879 LineTableInfo &LineTable = SourceMgr.getLineTable();
880
881 // Parse the file names
882 std::map<int, int> FileIDs;
883 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
884 // Extract the file name
885 unsigned FilenameLen = Record[Idx++];
886 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
887 Idx += FilenameLen;
888 MaybeAddSystemRootToFilename(F, Filename);
889 FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
890 }
891
892 // Parse the line entries
893 std::vector<LineEntry> Entries;
894 while (Idx < Record.size()) {
895 int FID = Record[Idx++];
896 assert(FID >= 0 && "Serialized line entries for non-local file.");
897 // Remap FileID from 1-based old view.
898 FID += F.SLocEntryBaseID - 1;
899
900 // Extract the line entries
901 unsigned NumEntries = Record[Idx++];
902 assert(NumEntries && "Numentries is 00000");
903 Entries.clear();
904 Entries.reserve(NumEntries);
905 for (unsigned I = 0; I != NumEntries; ++I) {
906 unsigned FileOffset = Record[Idx++];
907 unsigned LineNo = Record[Idx++];
908 int FilenameID = FileIDs[Record[Idx++]];
909 SrcMgr::CharacteristicKind FileKind
910 = (SrcMgr::CharacteristicKind)Record[Idx++];
911 unsigned IncludeOffset = Record[Idx++];
912 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
913 FileKind, IncludeOffset));
914 }
915 LineTable.AddEntry(FileID::get(FID), Entries);
916 }
917
918 return false;
919}
920
921/// \brief Read a source manager block
922bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
923 using namespace SrcMgr;
924
Chris Lattner7fb3bef2013-01-20 00:56:42 +0000925 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +0000926
927 // Set the source-location entry cursor to the current position in
928 // the stream. This cursor will be used to read the contents of the
929 // source manager block initially, and then lazily read
930 // source-location entries as needed.
931 SLocEntryCursor = F.Stream;
932
933 // The stream itself is going to skip over the source manager block.
934 if (F.Stream.SkipBlock()) {
935 Error("malformed block record in AST file");
936 return true;
937 }
938
939 // Enter the source manager block.
940 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
941 Error("malformed source manager block record in AST file");
942 return true;
943 }
944
945 RecordData Record;
946 while (true) {
Chris Lattnere7b154b2013-01-19 21:39:22 +0000947 llvm::BitstreamEntry E = SLocEntryCursor.advanceSkippingSubblocks();
948
949 switch (E.Kind) {
950 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
951 case llvm::BitstreamEntry::Error:
952 Error("malformed block record in AST file");
953 return true;
954 case llvm::BitstreamEntry::EndBlock:
Guy Benyei11169dd2012-12-18 14:30:41 +0000955 return false;
Chris Lattnere7b154b2013-01-19 21:39:22 +0000956 case llvm::BitstreamEntry::Record:
957 // The interesting case.
958 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000959 }
Chris Lattnere7b154b2013-01-19 21:39:22 +0000960
Guy Benyei11169dd2012-12-18 14:30:41 +0000961 // Read a record.
Guy Benyei11169dd2012-12-18 14:30:41 +0000962 Record.clear();
Chris Lattner15c3e7d2013-01-21 18:28:26 +0000963 StringRef Blob;
964 switch (SLocEntryCursor.readRecord(E.ID, Record, &Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000965 default: // Default behavior: ignore.
966 break;
967
968 case SM_SLOC_FILE_ENTRY:
969 case SM_SLOC_BUFFER_ENTRY:
970 case SM_SLOC_EXPANSION_ENTRY:
971 // Once we hit one of the source location entries, we're done.
972 return false;
973 }
974 }
975}
976
977/// \brief If a header file is not found at the path that we expect it to be
978/// and the PCH file was moved from its original location, try to resolve the
979/// file by assuming that header+PCH were moved together and the header is in
980/// the same place relative to the PCH.
981static std::string
982resolveFileRelativeToOriginalDir(const std::string &Filename,
983 const std::string &OriginalDir,
984 const std::string &CurrDir) {
985 assert(OriginalDir != CurrDir &&
986 "No point trying to resolve the file if the PCH dir didn't change");
987 using namespace llvm::sys;
988 SmallString<128> filePath(Filename);
989 fs::make_absolute(filePath);
990 assert(path::is_absolute(OriginalDir));
991 SmallString<128> currPCHPath(CurrDir);
992
993 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
994 fileDirE = path::end(path::parent_path(filePath));
995 path::const_iterator origDirI = path::begin(OriginalDir),
996 origDirE = path::end(OriginalDir);
997 // Skip the common path components from filePath and OriginalDir.
998 while (fileDirI != fileDirE && origDirI != origDirE &&
999 *fileDirI == *origDirI) {
1000 ++fileDirI;
1001 ++origDirI;
1002 }
1003 for (; origDirI != origDirE; ++origDirI)
1004 path::append(currPCHPath, "..");
1005 path::append(currPCHPath, fileDirI, fileDirE);
1006 path::append(currPCHPath, path::filename(Filename));
1007 return currPCHPath.str();
1008}
1009
1010bool ASTReader::ReadSLocEntry(int ID) {
1011 if (ID == 0)
1012 return false;
1013
1014 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1015 Error("source location entry ID out-of-range for AST file");
1016 return true;
1017 }
1018
1019 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
1020 F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]);
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001021 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001022 unsigned BaseOffset = F->SLocEntryBaseOffset;
1023
1024 ++NumSLocEntriesRead;
Chris Lattnere7b154b2013-01-19 21:39:22 +00001025 llvm::BitstreamEntry Entry = SLocEntryCursor.advance();
1026 if (Entry.Kind != llvm::BitstreamEntry::Record) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001027 Error("incorrectly-formatted source location entry in AST file");
1028 return true;
1029 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00001030
Guy Benyei11169dd2012-12-18 14:30:41 +00001031 RecordData Record;
Chris Lattner0e6c9402013-01-20 02:38:54 +00001032 StringRef Blob;
1033 switch (SLocEntryCursor.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001034 default:
1035 Error("incorrectly-formatted source location entry in AST file");
1036 return true;
1037
1038 case SM_SLOC_FILE_ENTRY: {
1039 // We will detect whether a file changed and return 'Failure' for it, but
1040 // we will also try to fail gracefully by setting up the SLocEntry.
1041 unsigned InputID = Record[4];
1042 InputFile IF = getInputFile(*F, InputID);
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001043 const FileEntry *File = IF.getFile();
1044 bool OverriddenBuffer = IF.isOverridden();
Guy Benyei11169dd2012-12-18 14:30:41 +00001045
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001046 // Note that we only check if a File was returned. If it was out-of-date
1047 // we have complained but we will continue creating a FileID to recover
1048 // gracefully.
1049 if (!File)
Guy Benyei11169dd2012-12-18 14:30:41 +00001050 return true;
1051
1052 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1053 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1054 // This is the module's main file.
1055 IncludeLoc = getImportLocation(F);
1056 }
1057 SrcMgr::CharacteristicKind
1058 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1059 FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
1060 ID, BaseOffset + Record[0]);
1061 SrcMgr::FileInfo &FileInfo =
1062 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1063 FileInfo.NumCreatedFIDs = Record[5];
1064 if (Record[3])
1065 FileInfo.setHasLineDirectives();
1066
1067 const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
1068 unsigned NumFileDecls = Record[7];
1069 if (NumFileDecls) {
1070 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1071 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1072 NumFileDecls));
1073 }
1074
1075 const SrcMgr::ContentCache *ContentCache
1076 = SourceMgr.getOrCreateContentCache(File,
1077 /*isSystemFile=*/FileCharacter != SrcMgr::C_User);
1078 if (OverriddenBuffer && !ContentCache->BufferOverridden &&
1079 ContentCache->ContentsEntry == ContentCache->OrigEntry) {
1080 unsigned Code = SLocEntryCursor.ReadCode();
1081 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00001082 unsigned RecCode = SLocEntryCursor.readRecord(Code, Record, &Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +00001083
1084 if (RecCode != SM_SLOC_BUFFER_BLOB) {
1085 Error("AST record has invalid code");
1086 return true;
1087 }
1088
1089 llvm::MemoryBuffer *Buffer
Chris Lattner0e6c9402013-01-20 02:38:54 +00001090 = llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), File->getName());
Guy Benyei11169dd2012-12-18 14:30:41 +00001091 SourceMgr.overrideFileContents(File, Buffer);
1092 }
1093
1094 break;
1095 }
1096
1097 case SM_SLOC_BUFFER_ENTRY: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00001098 const char *Name = Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00001099 unsigned Offset = Record[0];
1100 SrcMgr::CharacteristicKind
1101 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1102 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1103 if (IncludeLoc.isInvalid() && F->Kind == MK_Module) {
1104 IncludeLoc = getImportLocation(F);
1105 }
1106 unsigned Code = SLocEntryCursor.ReadCode();
1107 Record.clear();
1108 unsigned RecCode
Chris Lattner0e6c9402013-01-20 02:38:54 +00001109 = SLocEntryCursor.readRecord(Code, Record, &Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +00001110
1111 if (RecCode != SM_SLOC_BUFFER_BLOB) {
1112 Error("AST record has invalid code");
1113 return true;
1114 }
1115
1116 llvm::MemoryBuffer *Buffer
Chris Lattner0e6c9402013-01-20 02:38:54 +00001117 = llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name);
Guy Benyei11169dd2012-12-18 14:30:41 +00001118 SourceMgr.createFileIDForMemBuffer(Buffer, FileCharacter, ID,
1119 BaseOffset + Offset, IncludeLoc);
1120 break;
1121 }
1122
1123 case SM_SLOC_EXPANSION_ENTRY: {
1124 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1125 SourceMgr.createExpansionLoc(SpellingLoc,
1126 ReadSourceLocation(*F, Record[2]),
1127 ReadSourceLocation(*F, Record[3]),
1128 Record[4],
1129 ID,
1130 BaseOffset + Record[0]);
1131 break;
1132 }
1133 }
1134
1135 return false;
1136}
1137
1138std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1139 if (ID == 0)
1140 return std::make_pair(SourceLocation(), "");
1141
1142 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1143 Error("source location entry ID out-of-range for AST file");
1144 return std::make_pair(SourceLocation(), "");
1145 }
1146
1147 // Find which module file this entry lands in.
1148 ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
1149 if (M->Kind != MK_Module)
1150 return std::make_pair(SourceLocation(), "");
1151
1152 // FIXME: Can we map this down to a particular submodule? That would be
1153 // ideal.
1154 return std::make_pair(M->ImportLoc, llvm::sys::path::stem(M->FileName));
1155}
1156
1157/// \brief Find the location where the module F is imported.
1158SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1159 if (F->ImportLoc.isValid())
1160 return F->ImportLoc;
1161
1162 // Otherwise we have a PCH. It's considered to be "imported" at the first
1163 // location of its includer.
1164 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
1165 // Main file is the importer. We assume that it is the first entry in the
1166 // entry table. We can't ask the manager, because at the time of PCH loading
1167 // the main file entry doesn't exist yet.
1168 // The very first entry is the invalid instantiation loc, which takes up
1169 // offsets 0 and 1.
1170 return SourceLocation::getFromRawEncoding(2U);
1171 }
1172 //return F->Loaders[0]->FirstLoc;
1173 return F->ImportedBy[0]->FirstLoc;
1174}
1175
1176/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1177/// specified cursor. Read the abbreviations that are at the top of the block
1178/// and then leave the cursor pointing into the block.
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001179bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001180 if (Cursor.EnterSubBlock(BlockID)) {
1181 Error("malformed block record in AST file");
1182 return Failure;
1183 }
1184
1185 while (true) {
1186 uint64_t Offset = Cursor.GetCurrentBitNo();
1187 unsigned Code = Cursor.ReadCode();
1188
1189 // We expect all abbrevs to be at the start of the block.
1190 if (Code != llvm::bitc::DEFINE_ABBREV) {
1191 Cursor.JumpToBit(Offset);
1192 return false;
1193 }
1194 Cursor.ReadAbbrevRecord();
1195 }
1196}
1197
Richard Smithe40f2ba2013-08-07 21:41:30 +00001198Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record,
John McCallf413f5e2013-05-03 00:10:13 +00001199 unsigned &Idx) {
1200 Token Tok;
1201 Tok.startToken();
1202 Tok.setLocation(ReadSourceLocation(F, Record, Idx));
1203 Tok.setLength(Record[Idx++]);
1204 if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
1205 Tok.setIdentifierInfo(II);
1206 Tok.setKind((tok::TokenKind)Record[Idx++]);
1207 Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1208 return Tok;
1209}
1210
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001211MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001212 BitstreamCursor &Stream = F.MacroCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001213
1214 // Keep track of where we are in the stream, then jump back there
1215 // after reading this macro.
1216 SavedStreamPosition SavedPosition(Stream);
1217
1218 Stream.JumpToBit(Offset);
1219 RecordData Record;
1220 SmallVector<IdentifierInfo*, 16> MacroArgs;
1221 MacroInfo *Macro = 0;
1222
Guy Benyei11169dd2012-12-18 14:30:41 +00001223 while (true) {
Chris Lattnerefa77172013-01-20 00:00:22 +00001224 // Advance to the next record, but if we get to the end of the block, don't
1225 // pop it (removing all the abbreviations from the cursor) since we want to
1226 // be able to reseek within the block and read entries.
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001227 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
Chris Lattnerefa77172013-01-20 00:00:22 +00001228 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks(Flags);
1229
1230 switch (Entry.Kind) {
1231 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1232 case llvm::BitstreamEntry::Error:
1233 Error("malformed block record in AST file");
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001234 return Macro;
Chris Lattnerefa77172013-01-20 00:00:22 +00001235 case llvm::BitstreamEntry::EndBlock:
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001236 return Macro;
Chris Lattnerefa77172013-01-20 00:00:22 +00001237 case llvm::BitstreamEntry::Record:
1238 // The interesting case.
1239 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001240 }
1241
1242 // Read a record.
Guy Benyei11169dd2012-12-18 14:30:41 +00001243 Record.clear();
1244 PreprocessorRecordTypes RecType =
Chris Lattner0e6c9402013-01-20 02:38:54 +00001245 (PreprocessorRecordTypes)Stream.readRecord(Entry.ID, Record);
Guy Benyei11169dd2012-12-18 14:30:41 +00001246 switch (RecType) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001247 case PP_MACRO_DIRECTIVE_HISTORY:
1248 return Macro;
1249
Guy Benyei11169dd2012-12-18 14:30:41 +00001250 case PP_MACRO_OBJECT_LIKE:
1251 case PP_MACRO_FUNCTION_LIKE: {
1252 // If we already have a macro, that means that we've hit the end
1253 // of the definition of the macro we were looking for. We're
1254 // done.
1255 if (Macro)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001256 return Macro;
Guy Benyei11169dd2012-12-18 14:30:41 +00001257
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001258 unsigned NextIndex = 1; // Skip identifier ID.
1259 SubmoduleID SubModID = getGlobalSubmoduleID(F, Record[NextIndex++]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001260 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001261 MacroInfo *MI = PP.AllocateDeserializedMacroInfo(Loc, SubModID);
Argyrios Kyrtzidis7572be22013-01-07 19:16:23 +00001262 MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
Guy Benyei11169dd2012-12-18 14:30:41 +00001263 MI->setIsUsed(Record[NextIndex++]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001264
Guy Benyei11169dd2012-12-18 14:30:41 +00001265 if (RecType == PP_MACRO_FUNCTION_LIKE) {
1266 // Decode function-like macro info.
1267 bool isC99VarArgs = Record[NextIndex++];
1268 bool isGNUVarArgs = Record[NextIndex++];
1269 bool hasCommaPasting = Record[NextIndex++];
1270 MacroArgs.clear();
1271 unsigned NumArgs = Record[NextIndex++];
1272 for (unsigned i = 0; i != NumArgs; ++i)
1273 MacroArgs.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1274
1275 // Install function-like macro info.
1276 MI->setIsFunctionLike();
1277 if (isC99VarArgs) MI->setIsC99Varargs();
1278 if (isGNUVarArgs) MI->setIsGNUVarargs();
1279 if (hasCommaPasting) MI->setHasCommaPasting();
1280 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
1281 PP.getPreprocessorAllocator());
1282 }
1283
Guy Benyei11169dd2012-12-18 14:30:41 +00001284 // Remember that we saw this macro last so that we add the tokens that
1285 // form its body to it.
1286 Macro = MI;
1287
1288 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1289 Record[NextIndex]) {
1290 // We have a macro definition. Register the association
1291 PreprocessedEntityID
1292 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1293 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
Argyrios Kyrtzidis832de9f2013-02-22 18:35:59 +00001294 PreprocessingRecord::PPEntityID
1295 PPID = PPRec.getPPEntityID(GlobalID-1, /*isLoaded=*/true);
1296 MacroDefinition *PPDef =
1297 cast_or_null<MacroDefinition>(PPRec.getPreprocessedEntity(PPID));
1298 if (PPDef)
1299 PPRec.RegisterMacroDefinition(Macro, PPDef);
Guy Benyei11169dd2012-12-18 14:30:41 +00001300 }
1301
1302 ++NumMacrosRead;
1303 break;
1304 }
1305
1306 case PP_TOKEN: {
1307 // If we see a TOKEN before a PP_MACRO_*, then the file is
1308 // erroneous, just pretend we didn't see this.
1309 if (Macro == 0) break;
1310
John McCallf413f5e2013-05-03 00:10:13 +00001311 unsigned Idx = 0;
1312 Token Tok = ReadToken(F, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00001313 Macro->AddTokenToBody(Tok);
1314 break;
1315 }
1316 }
1317 }
1318}
1319
1320PreprocessedEntityID
1321ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const {
1322 ContinuousRangeMap<uint32_t, int, 2>::const_iterator
1323 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1324 assert(I != M.PreprocessedEntityRemap.end()
1325 && "Invalid index into preprocessed entity index remap");
1326
1327 return LocalID + I->second;
1328}
1329
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001330unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1331 return llvm::hash_combine(ikey.Size, ikey.ModTime);
Guy Benyei11169dd2012-12-18 14:30:41 +00001332}
1333
1334HeaderFileInfoTrait::internal_key_type
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001335HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
1336 internal_key_type ikey = { FE->getSize(), FE->getModificationTime(),
1337 FE->getName() };
1338 return ikey;
1339}
Guy Benyei11169dd2012-12-18 14:30:41 +00001340
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001341bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
1342 if (a.Size != b.Size || a.ModTime != b.ModTime)
Guy Benyei11169dd2012-12-18 14:30:41 +00001343 return false;
1344
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001345 if (strcmp(a.Filename, b.Filename) == 0)
1346 return true;
1347
Guy Benyei11169dd2012-12-18 14:30:41 +00001348 // Determine whether the actual files are equivalent.
Argyrios Kyrtzidis2a513e82013-03-04 20:33:40 +00001349 FileManager &FileMgr = Reader.getFileManager();
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001350 const FileEntry *FEA = FileMgr.getFile(a.Filename);
1351 const FileEntry *FEB = FileMgr.getFile(b.Filename);
Argyrios Kyrtzidis2a513e82013-03-04 20:33:40 +00001352 return (FEA && FEA == FEB);
Guy Benyei11169dd2012-12-18 14:30:41 +00001353}
1354
1355std::pair<unsigned, unsigned>
1356HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1357 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1358 unsigned DataLen = (unsigned) *d++;
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001359 return std::make_pair(KeyLen, DataLen);
Guy Benyei11169dd2012-12-18 14:30:41 +00001360}
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001361
1362HeaderFileInfoTrait::internal_key_type
1363HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
1364 internal_key_type ikey;
1365 ikey.Size = off_t(clang::io::ReadUnalignedLE64(d));
1366 ikey.ModTime = time_t(clang::io::ReadUnalignedLE64(d));
1367 ikey.Filename = (const char *)d;
1368 return ikey;
1369}
1370
Guy Benyei11169dd2012-12-18 14:30:41 +00001371HeaderFileInfoTrait::data_type
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001372HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
Guy Benyei11169dd2012-12-18 14:30:41 +00001373 unsigned DataLen) {
1374 const unsigned char *End = d + DataLen;
1375 using namespace clang::io;
1376 HeaderFileInfo HFI;
1377 unsigned Flags = *d++;
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001378 HFI.HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>
1379 ((Flags >> 6) & 0x03);
Guy Benyei11169dd2012-12-18 14:30:41 +00001380 HFI.isImport = (Flags >> 5) & 0x01;
1381 HFI.isPragmaOnce = (Flags >> 4) & 0x01;
1382 HFI.DirInfo = (Flags >> 2) & 0x03;
1383 HFI.Resolved = (Flags >> 1) & 0x01;
1384 HFI.IndexHeaderMapHeader = Flags & 0x01;
1385 HFI.NumIncludes = ReadUnalignedLE16(d);
1386 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(M,
1387 ReadUnalignedLE32(d));
1388 if (unsigned FrameworkOffset = ReadUnalignedLE32(d)) {
1389 // The framework offset is 1 greater than the actual offset,
1390 // since 0 is used as an indicator for "no framework name".
1391 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1392 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1393 }
1394
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001395 if (d != End) {
1396 uint32_t LocalSMID = ReadUnalignedLE32(d);
1397 if (LocalSMID) {
1398 // This header is part of a module. Associate it with the module to enable
1399 // implicit module import.
1400 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1401 Module *Mod = Reader.getSubmodule(GlobalSMID);
1402 HFI.isModuleHeader = true;
1403 FileManager &FileMgr = Reader.getFileManager();
1404 ModuleMap &ModMap =
1405 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001406 ModMap.addHeader(Mod, FileMgr.getFile(key.Filename), HFI.getHeaderRole());
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001407 }
1408 }
1409
Guy Benyei11169dd2012-12-18 14:30:41 +00001410 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1411 (void)End;
1412
1413 // This HeaderFileInfo was externally loaded.
1414 HFI.External = true;
1415 return HFI;
1416}
1417
Richard Smith49f906a2014-03-01 00:08:04 +00001418void
1419ASTReader::addPendingMacroFromModule(IdentifierInfo *II, ModuleFile *M,
1420 GlobalMacroID GMacID,
1421 llvm::ArrayRef<SubmoduleID> Overrides) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001422 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
Richard Smith49f906a2014-03-01 00:08:04 +00001423 SubmoduleID *OverrideData = 0;
1424 if (!Overrides.empty()) {
1425 OverrideData = new (Context) SubmoduleID[Overrides.size() + 1];
1426 OverrideData[0] = Overrides.size();
1427 for (unsigned I = 0; I != Overrides.size(); ++I)
1428 OverrideData[I + 1] = getGlobalSubmoduleID(*M, Overrides[I]);
1429 }
1430 PendingMacroIDs[II].push_back(PendingMacroInfo(M, GMacID, OverrideData));
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001431}
1432
1433void ASTReader::addPendingMacroFromPCH(IdentifierInfo *II,
1434 ModuleFile *M,
1435 uint64_t MacroDirectivesOffset) {
1436 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1437 PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00001438}
1439
1440void ASTReader::ReadDefinedMacros() {
1441 // Note that we are loading defined macros.
1442 Deserializing Macros(this);
1443
1444 for (ModuleReverseIterator I = ModuleMgr.rbegin(),
1445 E = ModuleMgr.rend(); I != E; ++I) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001446 BitstreamCursor &MacroCursor = (*I)->MacroCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001447
1448 // If there was no preprocessor block, skip this file.
1449 if (!MacroCursor.getBitStreamReader())
1450 continue;
1451
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001452 BitstreamCursor Cursor = MacroCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001453 Cursor.JumpToBit((*I)->MacroStartOffset);
1454
1455 RecordData Record;
1456 while (true) {
Chris Lattnere7b154b2013-01-19 21:39:22 +00001457 llvm::BitstreamEntry E = Cursor.advanceSkippingSubblocks();
1458
1459 switch (E.Kind) {
1460 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1461 case llvm::BitstreamEntry::Error:
1462 Error("malformed block record in AST file");
1463 return;
1464 case llvm::BitstreamEntry::EndBlock:
1465 goto NextCursor;
1466
1467 case llvm::BitstreamEntry::Record:
Chris Lattnere7b154b2013-01-19 21:39:22 +00001468 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00001469 switch (Cursor.readRecord(E.ID, Record)) {
Chris Lattnere7b154b2013-01-19 21:39:22 +00001470 default: // Default behavior: ignore.
1471 break;
1472
1473 case PP_MACRO_OBJECT_LIKE:
1474 case PP_MACRO_FUNCTION_LIKE:
1475 getLocalIdentifier(**I, Record[0]);
1476 break;
1477
1478 case PP_TOKEN:
1479 // Ignore tokens.
1480 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001481 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001482 break;
1483 }
1484 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00001485 NextCursor: ;
Guy Benyei11169dd2012-12-18 14:30:41 +00001486 }
1487}
1488
1489namespace {
1490 /// \brief Visitor class used to look up identifirs in an AST file.
1491 class IdentifierLookupVisitor {
1492 StringRef Name;
1493 unsigned PriorGeneration;
Douglas Gregor00a50f72013-01-25 00:38:33 +00001494 unsigned &NumIdentifierLookups;
1495 unsigned &NumIdentifierLookupHits;
Guy Benyei11169dd2012-12-18 14:30:41 +00001496 IdentifierInfo *Found;
Douglas Gregor00a50f72013-01-25 00:38:33 +00001497
Guy Benyei11169dd2012-12-18 14:30:41 +00001498 public:
Douglas Gregor00a50f72013-01-25 00:38:33 +00001499 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
1500 unsigned &NumIdentifierLookups,
1501 unsigned &NumIdentifierLookupHits)
Douglas Gregor7211ac12013-01-25 23:32:03 +00001502 : Name(Name), PriorGeneration(PriorGeneration),
Douglas Gregor00a50f72013-01-25 00:38:33 +00001503 NumIdentifierLookups(NumIdentifierLookups),
1504 NumIdentifierLookupHits(NumIdentifierLookupHits),
1505 Found()
1506 {
1507 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001508
1509 static bool visit(ModuleFile &M, void *UserData) {
1510 IdentifierLookupVisitor *This
1511 = static_cast<IdentifierLookupVisitor *>(UserData);
1512
1513 // If we've already searched this module file, skip it now.
1514 if (M.Generation <= This->PriorGeneration)
1515 return true;
Douglas Gregore060e572013-01-25 01:03:03 +00001516
Guy Benyei11169dd2012-12-18 14:30:41 +00001517 ASTIdentifierLookupTable *IdTable
1518 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
1519 if (!IdTable)
1520 return false;
1521
1522 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(),
1523 M, This->Found);
Douglas Gregor00a50f72013-01-25 00:38:33 +00001524 ++This->NumIdentifierLookups;
1525 ASTIdentifierLookupTable::iterator Pos = IdTable->find(This->Name,&Trait);
Guy Benyei11169dd2012-12-18 14:30:41 +00001526 if (Pos == IdTable->end())
1527 return false;
1528
1529 // Dereferencing the iterator has the effect of building the
1530 // IdentifierInfo node and populating it with the various
1531 // declarations it needs.
Douglas Gregor00a50f72013-01-25 00:38:33 +00001532 ++This->NumIdentifierLookupHits;
Guy Benyei11169dd2012-12-18 14:30:41 +00001533 This->Found = *Pos;
1534 return true;
1535 }
1536
1537 // \brief Retrieve the identifier info found within the module
1538 // files.
1539 IdentifierInfo *getIdentifierInfo() const { return Found; }
1540 };
1541}
1542
1543void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
1544 // Note that we are loading an identifier.
1545 Deserializing AnIdentifier(this);
1546
1547 unsigned PriorGeneration = 0;
1548 if (getContext().getLangOpts().Modules)
1549 PriorGeneration = IdentifierGeneration[&II];
Douglas Gregore060e572013-01-25 01:03:03 +00001550
1551 // If there is a global index, look there first to determine which modules
1552 // provably do not have any results for this identifier.
Douglas Gregor7211ac12013-01-25 23:32:03 +00001553 GlobalModuleIndex::HitSet Hits;
1554 GlobalModuleIndex::HitSet *HitsPtr = 0;
Douglas Gregore060e572013-01-25 01:03:03 +00001555 if (!loadGlobalIndex()) {
Douglas Gregor7211ac12013-01-25 23:32:03 +00001556 if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
1557 HitsPtr = &Hits;
Douglas Gregore060e572013-01-25 01:03:03 +00001558 }
1559 }
1560
Douglas Gregor7211ac12013-01-25 23:32:03 +00001561 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
Douglas Gregor00a50f72013-01-25 00:38:33 +00001562 NumIdentifierLookups,
1563 NumIdentifierLookupHits);
Douglas Gregor7211ac12013-01-25 23:32:03 +00001564 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor, HitsPtr);
Guy Benyei11169dd2012-12-18 14:30:41 +00001565 markIdentifierUpToDate(&II);
1566}
1567
1568void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
1569 if (!II)
1570 return;
1571
1572 II->setOutOfDate(false);
1573
1574 // Update the generation for this identifier.
1575 if (getContext().getLangOpts().Modules)
1576 IdentifierGeneration[II] = CurrentGeneration;
1577}
1578
Richard Smith49f906a2014-03-01 00:08:04 +00001579struct ASTReader::ModuleMacroInfo {
1580 SubmoduleID SubModID;
1581 MacroInfo *MI;
1582 SubmoduleID *Overrides;
1583 // FIXME: Remove this.
1584 ModuleFile *F;
1585
1586 bool isDefine() const { return MI; }
1587
1588 SubmoduleID getSubmoduleID() const { return SubModID; }
1589
1590 llvm::ArrayRef<SubmoduleID> getOverriddenSubmodules() const {
1591 if (!Overrides)
1592 return llvm::ArrayRef<SubmoduleID>();
1593 return llvm::makeArrayRef(Overrides + 1, *Overrides);
1594 }
1595
1596 DefMacroDirective *import(Preprocessor &PP, SourceLocation ImportLoc) const {
1597 if (!MI)
1598 return 0;
1599 return PP.AllocateDefMacroDirective(MI, ImportLoc, /*isImported=*/true);
1600 }
1601};
1602
1603ASTReader::ModuleMacroInfo *
1604ASTReader::getModuleMacro(const PendingMacroInfo &PMInfo) {
1605 ModuleMacroInfo Info;
1606
1607 uint32_t ID = PMInfo.ModuleMacroData.MacID;
1608 if (ID & 1) {
1609 // Macro undefinition.
1610 Info.SubModID = getGlobalSubmoduleID(*PMInfo.M, ID >> 1);
1611 Info.MI = 0;
1612 } else {
1613 // Macro definition.
1614 GlobalMacroID GMacID = getGlobalMacroID(*PMInfo.M, ID >> 1);
1615 assert(GMacID);
1616
1617 // If this macro has already been loaded, don't do so again.
1618 // FIXME: This is highly dubious. Multiple macro definitions can have the
1619 // same MacroInfo (and hence the same GMacID) due to #pragma push_macro etc.
1620 if (MacrosLoaded[GMacID - NUM_PREDEF_MACRO_IDS])
1621 return 0;
1622
1623 Info.MI = getMacro(GMacID);
1624 Info.SubModID = Info.MI->getOwningModuleID();
1625 }
1626 Info.Overrides = PMInfo.ModuleMacroData.Overrides;
1627 Info.F = PMInfo.M;
1628
1629 return new (Context) ModuleMacroInfo(Info);
1630}
1631
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001632void ASTReader::resolvePendingMacro(IdentifierInfo *II,
1633 const PendingMacroInfo &PMInfo) {
1634 assert(II);
1635
1636 if (PMInfo.M->Kind != MK_Module) {
1637 installPCHMacroDirectives(II, *PMInfo.M,
1638 PMInfo.PCHMacroData.MacroDirectivesOffset);
1639 return;
1640 }
Richard Smith49f906a2014-03-01 00:08:04 +00001641
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001642 // Module Macro.
1643
Richard Smith49f906a2014-03-01 00:08:04 +00001644 ModuleMacroInfo *MMI = getModuleMacro(PMInfo);
1645 if (!MMI)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001646 return;
1647
Richard Smith49f906a2014-03-01 00:08:04 +00001648 Module *Owner = getSubmodule(MMI->getSubmoduleID());
1649 if (Owner && Owner->NameVisibility == Module::Hidden) {
1650 // Macros in the owning module are hidden. Just remember this macro to
1651 // install if we make this module visible.
1652 HiddenNamesMap[Owner].HiddenMacros.insert(std::make_pair(II, MMI));
1653 } else {
1654 installImportedMacro(II, MMI, Owner);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001655 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001656}
1657
1658void ASTReader::installPCHMacroDirectives(IdentifierInfo *II,
1659 ModuleFile &M, uint64_t Offset) {
1660 assert(M.Kind != MK_Module);
1661
1662 BitstreamCursor &Cursor = M.MacroCursor;
1663 SavedStreamPosition SavedPosition(Cursor);
1664 Cursor.JumpToBit(Offset);
1665
1666 llvm::BitstreamEntry Entry =
1667 Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
1668 if (Entry.Kind != llvm::BitstreamEntry::Record) {
1669 Error("malformed block record in AST file");
1670 return;
1671 }
1672
1673 RecordData Record;
1674 PreprocessorRecordTypes RecType =
1675 (PreprocessorRecordTypes)Cursor.readRecord(Entry.ID, Record);
1676 if (RecType != PP_MACRO_DIRECTIVE_HISTORY) {
1677 Error("malformed block record in AST file");
1678 return;
1679 }
1680
1681 // Deserialize the macro directives history in reverse source-order.
1682 MacroDirective *Latest = 0, *Earliest = 0;
1683 unsigned Idx = 0, N = Record.size();
1684 while (Idx < N) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001685 MacroDirective *MD = 0;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001686 SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001687 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
1688 switch (K) {
1689 case MacroDirective::MD_Define: {
1690 GlobalMacroID GMacID = getGlobalMacroID(M, Record[Idx++]);
1691 MacroInfo *MI = getMacro(GMacID);
1692 bool isImported = Record[Idx++];
1693 bool isAmbiguous = Record[Idx++];
1694 DefMacroDirective *DefMD =
1695 PP.AllocateDefMacroDirective(MI, Loc, isImported);
1696 DefMD->setAmbiguous(isAmbiguous);
1697 MD = DefMD;
1698 break;
1699 }
1700 case MacroDirective::MD_Undefine:
1701 MD = PP.AllocateUndefMacroDirective(Loc);
1702 break;
1703 case MacroDirective::MD_Visibility: {
1704 bool isPublic = Record[Idx++];
1705 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
1706 break;
1707 }
1708 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001709
1710 if (!Latest)
1711 Latest = MD;
1712 if (Earliest)
1713 Earliest->setPrevious(MD);
1714 Earliest = MD;
1715 }
1716
1717 PP.setLoadedMacroDirective(II, Latest);
1718}
1719
Argyrios Kyrtzidis3a9c42c2013-03-27 01:25:34 +00001720/// \brief For the given macro definitions, check if they are both in system
Douglas Gregor0b202052013-04-12 21:00:54 +00001721/// modules.
1722static bool areDefinedInSystemModules(MacroInfo *PrevMI, MacroInfo *NewMI,
Douglas Gregor5e461192013-06-07 22:56:11 +00001723 Module *NewOwner, ASTReader &Reader) {
Argyrios Kyrtzidis3a9c42c2013-03-27 01:25:34 +00001724 assert(PrevMI && NewMI);
Argyrios Kyrtzidis3a9c42c2013-03-27 01:25:34 +00001725 Module *PrevOwner = 0;
1726 if (SubmoduleID PrevModID = PrevMI->getOwningModuleID())
1727 PrevOwner = Reader.getSubmodule(PrevModID);
Douglas Gregor5e461192013-06-07 22:56:11 +00001728 SourceManager &SrcMgr = Reader.getSourceManager();
1729 bool PrevInSystem
1730 = PrevOwner? PrevOwner->IsSystem
1731 : SrcMgr.isInSystemHeader(PrevMI->getDefinitionLoc());
1732 bool NewInSystem
1733 = NewOwner? NewOwner->IsSystem
1734 : SrcMgr.isInSystemHeader(NewMI->getDefinitionLoc());
1735 if (PrevOwner && PrevOwner == NewOwner)
Argyrios Kyrtzidis3a9c42c2013-03-27 01:25:34 +00001736 return false;
Douglas Gregor5e461192013-06-07 22:56:11 +00001737 return PrevInSystem && NewInSystem;
Argyrios Kyrtzidis3a9c42c2013-03-27 01:25:34 +00001738}
1739
Richard Smith49f906a2014-03-01 00:08:04 +00001740void ASTReader::removeOverriddenMacros(IdentifierInfo *II,
1741 AmbiguousMacros &Ambig,
1742 llvm::ArrayRef<SubmoduleID> Overrides) {
1743 for (unsigned OI = 0, ON = Overrides.size(); OI != ON; ++OI) {
1744 SubmoduleID OwnerID = Overrides[OI];
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001745
Richard Smith49f906a2014-03-01 00:08:04 +00001746 // If this macro is not yet visible, remove it from the hidden names list.
1747 Module *Owner = getSubmodule(OwnerID);
1748 HiddenNames &Hidden = HiddenNamesMap[Owner];
1749 HiddenMacrosMap::iterator HI = Hidden.HiddenMacros.find(II);
1750 if (HI != Hidden.HiddenMacros.end()) {
Richard Smith9d100862014-03-06 03:16:27 +00001751 auto SubOverrides = HI->second->getOverriddenSubmodules();
Richard Smith49f906a2014-03-01 00:08:04 +00001752 Hidden.HiddenMacros.erase(HI);
Richard Smith9d100862014-03-06 03:16:27 +00001753 removeOverriddenMacros(II, Ambig, SubOverrides);
Richard Smith49f906a2014-03-01 00:08:04 +00001754 }
1755
1756 // If this macro is already in our list of conflicts, remove it from there.
Richard Smithbb29e512014-03-06 00:33:23 +00001757 Ambig.erase(
1758 std::remove_if(Ambig.begin(), Ambig.end(), [&](DefMacroDirective *MD) {
1759 return MD->getInfo()->getOwningModuleID() == OwnerID;
1760 }),
1761 Ambig.end());
Richard Smith49f906a2014-03-01 00:08:04 +00001762 }
1763}
1764
1765ASTReader::AmbiguousMacros *
1766ASTReader::removeOverriddenMacros(IdentifierInfo *II,
1767 llvm::ArrayRef<SubmoduleID> Overrides) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001768 MacroDirective *Prev = PP.getMacroDirective(II);
Richard Smith49f906a2014-03-01 00:08:04 +00001769 if (!Prev && Overrides.empty())
1770 return 0;
1771
1772 DefMacroDirective *PrevDef = Prev ? Prev->getDefinition().getDirective() : 0;
1773 if (PrevDef && PrevDef->isAmbiguous()) {
1774 // We had a prior ambiguity. Check whether we resolve it (or make it worse).
1775 AmbiguousMacros &Ambig = AmbiguousMacroDefs[II];
1776 Ambig.push_back(PrevDef);
1777
1778 removeOverriddenMacros(II, Ambig, Overrides);
1779
1780 if (!Ambig.empty())
1781 return &Ambig;
1782
1783 AmbiguousMacroDefs.erase(II);
1784 } else {
1785 // There's no ambiguity yet. Maybe we're introducing one.
1786 llvm::SmallVector<DefMacroDirective*, 1> Ambig;
1787 if (PrevDef)
1788 Ambig.push_back(PrevDef);
1789
1790 removeOverriddenMacros(II, Ambig, Overrides);
1791
1792 if (!Ambig.empty()) {
1793 AmbiguousMacros &Result = AmbiguousMacroDefs[II];
1794 Result.swap(Ambig);
1795 return &Result;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001796 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001797 }
Richard Smith49f906a2014-03-01 00:08:04 +00001798
1799 // We ended up with no ambiguity.
1800 return 0;
1801}
1802
1803void ASTReader::installImportedMacro(IdentifierInfo *II, ModuleMacroInfo *MMI,
1804 Module *Owner) {
1805 assert(II && Owner);
1806
1807 SourceLocation ImportLoc = Owner->MacroVisibilityLoc;
1808 if (ImportLoc.isInvalid()) {
1809 // FIXME: If we made macros from this module visible but didn't provide a
1810 // source location for the import, we don't have a location for the macro.
1811 // Use the location at which the containing module file was first imported
1812 // for now.
1813 ImportLoc = MMI->F->DirectImportLoc;
1814 }
1815
1816 llvm::SmallVectorImpl<DefMacroDirective*> *Prev =
1817 removeOverriddenMacros(II, MMI->getOverriddenSubmodules());
1818
1819
1820 // Create a synthetic macro definition corresponding to the import (or null
1821 // if this was an undefinition of the macro).
1822 DefMacroDirective *MD = MMI->import(PP, ImportLoc);
1823
1824 // If there's no ambiguity, just install the macro.
1825 if (!Prev) {
1826 if (MD)
1827 PP.appendMacroDirective(II, MD);
1828 else
1829 PP.appendMacroDirective(II, PP.AllocateUndefMacroDirective(ImportLoc));
1830 return;
1831 }
1832 assert(!Prev->empty());
1833
1834 if (!MD) {
1835 // We imported a #undef that didn't remove all prior definitions. The most
1836 // recent prior definition remains, and we install it in the place of the
1837 // imported directive.
1838 MacroInfo *NewMI = Prev->back()->getInfo();
1839 Prev->pop_back();
1840 MD = PP.AllocateDefMacroDirective(NewMI, ImportLoc, /*Imported*/true);
1841 }
1842
1843 // We're introducing a macro definition that creates or adds to an ambiguity.
1844 // We can resolve that ambiguity if this macro is token-for-token identical to
1845 // all of the existing definitions.
1846 MacroInfo *NewMI = MD->getInfo();
1847 assert(NewMI && "macro definition with no MacroInfo?");
1848 while (!Prev->empty()) {
1849 MacroInfo *PrevMI = Prev->back()->getInfo();
1850 assert(PrevMI && "macro definition with no MacroInfo?");
1851
1852 // Before marking the macros as ambiguous, check if this is a case where
1853 // both macros are in system headers. If so, we trust that the system
1854 // did not get it wrong. This also handles cases where Clang's own
1855 // headers have a different spelling of certain system macros:
1856 // #define LONG_MAX __LONG_MAX__ (clang's limits.h)
1857 // #define LONG_MAX 0x7fffffffffffffffL (system's limits.h)
1858 //
1859 // FIXME: Remove the defined-in-system-headers check. clang's limits.h
1860 // overrides the system limits.h's macros, so there's no conflict here.
1861 if (NewMI != PrevMI &&
1862 !PrevMI->isIdenticalTo(*NewMI, PP, /*Syntactically=*/true) &&
1863 !areDefinedInSystemModules(PrevMI, NewMI, Owner, *this))
1864 break;
1865
1866 // The previous definition is the same as this one (or both are defined in
1867 // system modules so we can assume they're equivalent); we don't need to
1868 // track it any more.
1869 Prev->pop_back();
1870 }
1871
1872 if (!Prev->empty())
1873 MD->setAmbiguous(true);
1874
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001875 PP.appendMacroDirective(II, MD);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001876}
1877
Argyrios Kyrtzidisce9b49e2014-03-14 02:26:27 +00001878ASTReader::InputFileInfo
1879ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) {
Ben Langmuir198c1682014-03-07 07:27:49 +00001880 // Go find this input file.
1881 BitstreamCursor &Cursor = F.InputFilesCursor;
1882 SavedStreamPosition SavedPosition(Cursor);
1883 Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
1884
1885 unsigned Code = Cursor.ReadCode();
1886 RecordData Record;
1887 StringRef Blob;
1888
1889 unsigned Result = Cursor.readRecord(Code, Record, &Blob);
1890 assert(static_cast<InputFileRecordTypes>(Result) == INPUT_FILE &&
1891 "invalid record type for input file");
1892 (void)Result;
1893
Argyrios Kyrtzidisce9b49e2014-03-14 02:26:27 +00001894 std::string Filename;
1895 off_t StoredSize;
1896 time_t StoredTime;
1897 bool Overridden;
1898
Ben Langmuir198c1682014-03-07 07:27:49 +00001899 assert(Record[0] == ID && "Bogus stored ID or offset");
1900 StoredSize = static_cast<off_t>(Record[1]);
1901 StoredTime = static_cast<time_t>(Record[2]);
1902 Overridden = static_cast<bool>(Record[3]);
1903 Filename = Blob;
1904 MaybeAddSystemRootToFilename(F, Filename);
Argyrios Kyrtzidisce9b49e2014-03-14 02:26:27 +00001905
Hans Wennborg73945142014-03-14 17:45:06 +00001906 InputFileInfo R = { std::move(Filename), StoredSize, StoredTime, Overridden };
1907 return R;
Ben Langmuir198c1682014-03-07 07:27:49 +00001908}
1909
1910std::string ASTReader::getInputFileName(ModuleFile &F, unsigned int ID) {
Argyrios Kyrtzidisce9b49e2014-03-14 02:26:27 +00001911 return readInputFileInfo(F, ID).Filename;
Ben Langmuir198c1682014-03-07 07:27:49 +00001912}
1913
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001914InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001915 // If this ID is bogus, just return an empty input file.
1916 if (ID == 0 || ID > F.InputFilesLoaded.size())
1917 return InputFile();
1918
1919 // If we've already loaded this input file, return it.
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001920 if (F.InputFilesLoaded[ID-1].getFile())
Guy Benyei11169dd2012-12-18 14:30:41 +00001921 return F.InputFilesLoaded[ID-1];
1922
Argyrios Kyrtzidis9308f0a2014-01-08 19:13:34 +00001923 if (F.InputFilesLoaded[ID-1].isNotFound())
1924 return InputFile();
1925
Guy Benyei11169dd2012-12-18 14:30:41 +00001926 // Go find this input file.
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001927 BitstreamCursor &Cursor = F.InputFilesCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001928 SavedStreamPosition SavedPosition(Cursor);
1929 Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
1930
Argyrios Kyrtzidisce9b49e2014-03-14 02:26:27 +00001931 InputFileInfo FI = readInputFileInfo(F, ID);
1932 off_t StoredSize = FI.StoredSize;
1933 time_t StoredTime = FI.StoredTime;
1934 bool Overridden = FI.Overridden;
1935 StringRef Filename = FI.Filename;
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001936
Ben Langmuir198c1682014-03-07 07:27:49 +00001937 const FileEntry *File
1938 = Overridden? FileMgr.getVirtualFile(Filename, StoredSize, StoredTime)
1939 : FileMgr.getFile(Filename, /*OpenFile=*/false);
1940
1941 // If we didn't find the file, resolve it relative to the
1942 // original directory from which this AST file was created.
1943 if (File == 0 && !F.OriginalDir.empty() && !CurrentDir.empty() &&
1944 F.OriginalDir != CurrentDir) {
1945 std::string Resolved = resolveFileRelativeToOriginalDir(Filename,
1946 F.OriginalDir,
1947 CurrentDir);
1948 if (!Resolved.empty())
1949 File = FileMgr.getFile(Resolved);
1950 }
1951
1952 // For an overridden file, create a virtual file with the stored
1953 // size/timestamp.
1954 if (Overridden && File == 0) {
1955 File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
1956 }
1957
1958 if (File == 0) {
1959 if (Complain) {
1960 std::string ErrorStr = "could not find file '";
1961 ErrorStr += Filename;
1962 ErrorStr += "' referenced by AST file";
1963 Error(ErrorStr.c_str());
Guy Benyei11169dd2012-12-18 14:30:41 +00001964 }
Ben Langmuir198c1682014-03-07 07:27:49 +00001965 // Record that we didn't find the file.
1966 F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
1967 return InputFile();
1968 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001969
Ben Langmuir198c1682014-03-07 07:27:49 +00001970 // Check if there was a request to override the contents of the file
1971 // that was part of the precompiled header. Overridding such a file
1972 // can lead to problems when lexing using the source locations from the
1973 // PCH.
1974 SourceManager &SM = getSourceManager();
1975 if (!Overridden && SM.isFileOverridden(File)) {
1976 if (Complain)
1977 Error(diag::err_fe_pch_file_overridden, Filename);
1978 // After emitting the diagnostic, recover by disabling the override so
1979 // that the original file will be used.
1980 SM.disableFileContentsOverride(File);
1981 // The FileEntry is a virtual file entry with the size of the contents
1982 // that would override the original contents. Set it to the original's
1983 // size/time.
1984 FileMgr.modifyFileEntry(const_cast<FileEntry*>(File),
1985 StoredSize, StoredTime);
1986 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001987
Ben Langmuir198c1682014-03-07 07:27:49 +00001988 bool IsOutOfDate = false;
1989
1990 // For an overridden file, there is nothing to validate.
1991 if (!Overridden && (StoredSize != File->getSize()
Guy Benyei11169dd2012-12-18 14:30:41 +00001992#if !defined(LLVM_ON_WIN32)
Ben Langmuir198c1682014-03-07 07:27:49 +00001993 // In our regression testing, the Windows file system seems to
1994 // have inconsistent modification times that sometimes
1995 // erroneously trigger this error-handling path.
1996 || StoredTime != File->getModificationTime()
Guy Benyei11169dd2012-12-18 14:30:41 +00001997#endif
Ben Langmuir198c1682014-03-07 07:27:49 +00001998 )) {
1999 if (Complain) {
2000 // Build a list of the PCH imports that got us here (in reverse).
2001 SmallVector<ModuleFile *, 4> ImportStack(1, &F);
2002 while (ImportStack.back()->ImportedBy.size() > 0)
2003 ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
Ben Langmuire82630d2014-01-17 00:19:09 +00002004
Ben Langmuir198c1682014-03-07 07:27:49 +00002005 // The top-level PCH is stale.
2006 StringRef TopLevelPCHName(ImportStack.back()->FileName);
2007 Error(diag::err_fe_pch_file_modified, Filename, TopLevelPCHName);
Ben Langmuire82630d2014-01-17 00:19:09 +00002008
Ben Langmuir198c1682014-03-07 07:27:49 +00002009 // Print the import stack.
2010 if (ImportStack.size() > 1 && !Diags.isDiagnosticInFlight()) {
2011 Diag(diag::note_pch_required_by)
2012 << Filename << ImportStack[0]->FileName;
2013 for (unsigned I = 1; I < ImportStack.size(); ++I)
Ben Langmuire82630d2014-01-17 00:19:09 +00002014 Diag(diag::note_pch_required_by)
Ben Langmuir198c1682014-03-07 07:27:49 +00002015 << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
Douglas Gregor7029ce12013-03-19 00:28:20 +00002016 }
2017
Ben Langmuir198c1682014-03-07 07:27:49 +00002018 if (!Diags.isDiagnosticInFlight())
2019 Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
Guy Benyei11169dd2012-12-18 14:30:41 +00002020 }
2021
Ben Langmuir198c1682014-03-07 07:27:49 +00002022 IsOutOfDate = true;
Guy Benyei11169dd2012-12-18 14:30:41 +00002023 }
2024
Ben Langmuir198c1682014-03-07 07:27:49 +00002025 InputFile IF = InputFile(File, Overridden, IsOutOfDate);
2026
2027 // Note that we've loaded this input file.
2028 F.InputFilesLoaded[ID-1] = IF;
2029 return IF;
Guy Benyei11169dd2012-12-18 14:30:41 +00002030}
2031
2032const FileEntry *ASTReader::getFileEntry(StringRef filenameStrRef) {
2033 ModuleFile &M = ModuleMgr.getPrimaryModule();
2034 std::string Filename = filenameStrRef;
2035 MaybeAddSystemRootToFilename(M, Filename);
2036 const FileEntry *File = FileMgr.getFile(Filename);
2037 if (File == 0 && !M.OriginalDir.empty() && !CurrentDir.empty() &&
2038 M.OriginalDir != CurrentDir) {
2039 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
2040 M.OriginalDir,
2041 CurrentDir);
2042 if (!resolved.empty())
2043 File = FileMgr.getFile(resolved);
2044 }
2045
2046 return File;
2047}
2048
2049/// \brief If we are loading a relocatable PCH file, and the filename is
2050/// not an absolute path, add the system root to the beginning of the file
2051/// name.
2052void ASTReader::MaybeAddSystemRootToFilename(ModuleFile &M,
2053 std::string &Filename) {
2054 // If this is not a relocatable PCH file, there's nothing to do.
2055 if (!M.RelocatablePCH)
2056 return;
2057
2058 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
2059 return;
2060
2061 if (isysroot.empty()) {
2062 // If no system root was given, default to '/'
2063 Filename.insert(Filename.begin(), '/');
2064 return;
2065 }
2066
2067 unsigned Length = isysroot.size();
2068 if (isysroot[Length - 1] != '/')
2069 Filename.insert(Filename.begin(), '/');
2070
2071 Filename.insert(Filename.begin(), isysroot.begin(), isysroot.end());
2072}
2073
2074ASTReader::ASTReadResult
2075ASTReader::ReadControlBlock(ModuleFile &F,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002076 SmallVectorImpl<ImportedModule> &Loaded,
Guy Benyei11169dd2012-12-18 14:30:41 +00002077 unsigned ClientLoadCapabilities) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00002078 BitstreamCursor &Stream = F.Stream;
Guy Benyei11169dd2012-12-18 14:30:41 +00002079
2080 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
2081 Error("malformed block record in AST file");
2082 return Failure;
2083 }
2084
2085 // Read all of the records and blocks in the control block.
2086 RecordData Record;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002087 while (1) {
2088 llvm::BitstreamEntry Entry = Stream.advance();
2089
2090 switch (Entry.Kind) {
2091 case llvm::BitstreamEntry::Error:
2092 Error("malformed block record in AST file");
2093 return Failure;
Dmitri Gribenkof430da42014-02-12 10:33:14 +00002094 case llvm::BitstreamEntry::EndBlock: {
2095 // Validate input files.
2096 const HeaderSearchOptions &HSOpts =
2097 PP.getHeaderSearchInfo().getHeaderSearchOpts();
Ben Langmuircb69b572014-03-07 06:40:32 +00002098
2099 // All user input files reside at the index range [0, Record[1]), and
2100 // system input files reside at [Record[1], Record[0]).
2101 // Record is the one from INPUT_FILE_OFFSETS.
2102 unsigned NumInputs = Record[0];
2103 unsigned NumUserInputs = Record[1];
2104
Dmitri Gribenkof430da42014-02-12 10:33:14 +00002105 if (!DisableValidation &&
2106 (!HSOpts.ModulesValidateOncePerBuildSession ||
2107 F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002108 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
Ben Langmuircb69b572014-03-07 06:40:32 +00002109
Dmitri Gribenkof430da42014-02-12 10:33:14 +00002110 // If we are reading a module, we will create a verification timestamp,
2111 // so we verify all input files. Otherwise, verify only user input
2112 // files.
Ben Langmuircb69b572014-03-07 06:40:32 +00002113
2114 unsigned N = NumUserInputs;
2115 if (ValidateSystemInputs ||
Ben Langmuircb69b572014-03-07 06:40:32 +00002116 (HSOpts.ModulesValidateOncePerBuildSession && F.Kind == MK_Module))
2117 N = NumInputs;
2118
Ben Langmuir3d4417c2014-02-07 17:31:11 +00002119 for (unsigned I = 0; I < N; ++I) {
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00002120 InputFile IF = getInputFile(F, I+1, Complain);
2121 if (!IF.getFile() || IF.isOutOfDate())
Guy Benyei11169dd2012-12-18 14:30:41 +00002122 return OutOfDate;
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00002123 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002124 }
Ben Langmuircb69b572014-03-07 06:40:32 +00002125
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +00002126 if (Listener)
2127 Listener->visitModuleFile(F.FileName);
2128
Ben Langmuircb69b572014-03-07 06:40:32 +00002129 if (Listener && Listener->needsInputFileVisitation()) {
2130 unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
2131 : NumUserInputs;
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +00002132 for (unsigned I = 0; I < N; ++I) {
2133 bool IsSystem = I >= NumUserInputs;
2134 InputFileInfo FI = readInputFileInfo(F, I+1);
2135 Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden);
2136 }
Ben Langmuircb69b572014-03-07 06:40:32 +00002137 }
2138
Guy Benyei11169dd2012-12-18 14:30:41 +00002139 return Success;
Dmitri Gribenkof430da42014-02-12 10:33:14 +00002140 }
2141
Chris Lattnere7b154b2013-01-19 21:39:22 +00002142 case llvm::BitstreamEntry::SubBlock:
2143 switch (Entry.ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002144 case INPUT_FILES_BLOCK_ID:
2145 F.InputFilesCursor = Stream;
2146 if (Stream.SkipBlock() || // Skip with the main cursor
2147 // Read the abbreviations
2148 ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
2149 Error("malformed block record in AST file");
2150 return Failure;
2151 }
2152 continue;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002153
Guy Benyei11169dd2012-12-18 14:30:41 +00002154 default:
Chris Lattnere7b154b2013-01-19 21:39:22 +00002155 if (Stream.SkipBlock()) {
2156 Error("malformed block record in AST file");
2157 return Failure;
2158 }
2159 continue;
Guy Benyei11169dd2012-12-18 14:30:41 +00002160 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00002161
2162 case llvm::BitstreamEntry::Record:
2163 // The interesting case.
2164 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002165 }
2166
2167 // Read and process a record.
2168 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00002169 StringRef Blob;
2170 switch ((ControlRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002171 case METADATA: {
2172 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2173 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
Dmitri Gribenko2228cd32014-02-11 15:40:09 +00002174 Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
2175 : diag::err_pch_version_too_new);
Guy Benyei11169dd2012-12-18 14:30:41 +00002176 return VersionMismatch;
2177 }
2178
2179 bool hasErrors = Record[5];
2180 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
2181 Diag(diag::err_pch_with_compiler_errors);
2182 return HadErrors;
2183 }
2184
2185 F.RelocatablePCH = Record[4];
2186
2187 const std::string &CurBranch = getClangFullRepositoryVersion();
Chris Lattner0e6c9402013-01-20 02:38:54 +00002188 StringRef ASTBranch = Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00002189 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2190 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
Dmitri Gribenko2228cd32014-02-11 15:40:09 +00002191 Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
Guy Benyei11169dd2012-12-18 14:30:41 +00002192 return VersionMismatch;
2193 }
2194 break;
2195 }
2196
2197 case IMPORTS: {
2198 // Load each of the imported PCH files.
2199 unsigned Idx = 0, N = Record.size();
2200 while (Idx < N) {
2201 // Read information about the AST file.
2202 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
2203 // The import location will be the local one for now; we will adjust
2204 // all import locations of module imports after the global source
2205 // location info are setup.
2206 SourceLocation ImportLoc =
2207 SourceLocation::getFromRawEncoding(Record[Idx++]);
Douglas Gregor7029ce12013-03-19 00:28:20 +00002208 off_t StoredSize = (off_t)Record[Idx++];
2209 time_t StoredModTime = (time_t)Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00002210 unsigned Length = Record[Idx++];
2211 SmallString<128> ImportedFile(Record.begin() + Idx,
2212 Record.begin() + Idx + Length);
2213 Idx += Length;
2214
2215 // Load the AST file.
2216 switch(ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F, Loaded,
Douglas Gregor7029ce12013-03-19 00:28:20 +00002217 StoredSize, StoredModTime,
Guy Benyei11169dd2012-12-18 14:30:41 +00002218 ClientLoadCapabilities)) {
2219 case Failure: return Failure;
2220 // If we have to ignore the dependency, we'll have to ignore this too.
Douglas Gregor2f1806e2013-03-19 00:38:50 +00002221 case Missing:
Guy Benyei11169dd2012-12-18 14:30:41 +00002222 case OutOfDate: return OutOfDate;
2223 case VersionMismatch: return VersionMismatch;
2224 case ConfigurationMismatch: return ConfigurationMismatch;
2225 case HadErrors: return HadErrors;
2226 case Success: break;
2227 }
2228 }
2229 break;
2230 }
2231
2232 case LANGUAGE_OPTIONS: {
2233 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2234 if (Listener && &F == *ModuleMgr.begin() &&
2235 ParseLanguageOptions(Record, Complain, *Listener) &&
Ben Langmuir2cb4a782014-02-05 22:21:15 +00002236 !DisableValidation && !AllowConfigurationMismatch)
Guy Benyei11169dd2012-12-18 14:30:41 +00002237 return ConfigurationMismatch;
2238 break;
2239 }
2240
2241 case TARGET_OPTIONS: {
2242 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2243 if (Listener && &F == *ModuleMgr.begin() &&
2244 ParseTargetOptions(Record, Complain, *Listener) &&
Ben Langmuir2cb4a782014-02-05 22:21:15 +00002245 !DisableValidation && !AllowConfigurationMismatch)
Guy Benyei11169dd2012-12-18 14:30:41 +00002246 return ConfigurationMismatch;
2247 break;
2248 }
2249
2250 case DIAGNOSTIC_OPTIONS: {
2251 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2252 if (Listener && &F == *ModuleMgr.begin() &&
2253 ParseDiagnosticOptions(Record, Complain, *Listener) &&
Ben Langmuir2cb4a782014-02-05 22:21:15 +00002254 !DisableValidation && !AllowConfigurationMismatch)
Guy Benyei11169dd2012-12-18 14:30:41 +00002255 return ConfigurationMismatch;
2256 break;
2257 }
2258
2259 case FILE_SYSTEM_OPTIONS: {
2260 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2261 if (Listener && &F == *ModuleMgr.begin() &&
2262 ParseFileSystemOptions(Record, Complain, *Listener) &&
Ben Langmuir2cb4a782014-02-05 22:21:15 +00002263 !DisableValidation && !AllowConfigurationMismatch)
Guy Benyei11169dd2012-12-18 14:30:41 +00002264 return ConfigurationMismatch;
2265 break;
2266 }
2267
2268 case HEADER_SEARCH_OPTIONS: {
2269 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2270 if (Listener && &F == *ModuleMgr.begin() &&
2271 ParseHeaderSearchOptions(Record, Complain, *Listener) &&
Ben Langmuir2cb4a782014-02-05 22:21:15 +00002272 !DisableValidation && !AllowConfigurationMismatch)
Guy Benyei11169dd2012-12-18 14:30:41 +00002273 return ConfigurationMismatch;
2274 break;
2275 }
2276
2277 case PREPROCESSOR_OPTIONS: {
2278 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2279 if (Listener && &F == *ModuleMgr.begin() &&
2280 ParsePreprocessorOptions(Record, Complain, *Listener,
2281 SuggestedPredefines) &&
Ben Langmuir2cb4a782014-02-05 22:21:15 +00002282 !DisableValidation && !AllowConfigurationMismatch)
Guy Benyei11169dd2012-12-18 14:30:41 +00002283 return ConfigurationMismatch;
2284 break;
2285 }
2286
2287 case ORIGINAL_FILE:
2288 F.OriginalSourceFileID = FileID::get(Record[0]);
Chris Lattner0e6c9402013-01-20 02:38:54 +00002289 F.ActualOriginalSourceFileName = Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00002290 F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
2291 MaybeAddSystemRootToFilename(F, F.OriginalSourceFileName);
2292 break;
2293
2294 case ORIGINAL_FILE_ID:
2295 F.OriginalSourceFileID = FileID::get(Record[0]);
2296 break;
2297
2298 case ORIGINAL_PCH_DIR:
Chris Lattner0e6c9402013-01-20 02:38:54 +00002299 F.OriginalDir = Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00002300 break;
2301
2302 case INPUT_FILE_OFFSETS:
Chris Lattner0e6c9402013-01-20 02:38:54 +00002303 F.InputFileOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002304 F.InputFilesLoaded.resize(Record[0]);
2305 break;
2306 }
2307 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002308}
2309
2310bool ASTReader::ReadASTBlock(ModuleFile &F) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00002311 BitstreamCursor &Stream = F.Stream;
Guy Benyei11169dd2012-12-18 14:30:41 +00002312
2313 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
2314 Error("malformed block record in AST file");
2315 return true;
2316 }
2317
2318 // Read all of the records and blocks for the AST file.
2319 RecordData Record;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002320 while (1) {
2321 llvm::BitstreamEntry Entry = Stream.advance();
2322
2323 switch (Entry.Kind) {
2324 case llvm::BitstreamEntry::Error:
2325 Error("error at end of module block in AST file");
2326 return true;
2327 case llvm::BitstreamEntry::EndBlock: {
Richard Smithc0fbba72013-04-03 22:49:41 +00002328 // Outside of C++, we do not store a lookup map for the translation unit.
2329 // Instead, mark it as needing a lookup map to be built if this module
2330 // contains any declarations lexically within it (which it always does!).
2331 // This usually has no cost, since we very rarely need the lookup map for
2332 // the translation unit outside C++.
Guy Benyei11169dd2012-12-18 14:30:41 +00002333 DeclContext *DC = Context.getTranslationUnitDecl();
Richard Smithc0fbba72013-04-03 22:49:41 +00002334 if (DC->hasExternalLexicalStorage() &&
2335 !getContext().getLangOpts().CPlusPlus)
Guy Benyei11169dd2012-12-18 14:30:41 +00002336 DC->setMustBuildLookupTable();
Chris Lattnere7b154b2013-01-19 21:39:22 +00002337
Guy Benyei11169dd2012-12-18 14:30:41 +00002338 return false;
2339 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00002340 case llvm::BitstreamEntry::SubBlock:
2341 switch (Entry.ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002342 case DECLTYPES_BLOCK_ID:
2343 // We lazily load the decls block, but we want to set up the
2344 // DeclsCursor cursor to point into it. Clone our current bitcode
2345 // cursor to it, enter the block and read the abbrevs in that block.
2346 // With the main cursor, we just skip over it.
2347 F.DeclsCursor = Stream;
2348 if (Stream.SkipBlock() || // Skip with the main cursor.
2349 // Read the abbrevs.
2350 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
2351 Error("malformed block record in AST file");
2352 return true;
2353 }
2354 break;
Richard Smithb9eab6d2014-03-20 19:44:17 +00002355
Guy Benyei11169dd2012-12-18 14:30:41 +00002356 case PREPROCESSOR_BLOCK_ID:
2357 F.MacroCursor = Stream;
2358 if (!PP.getExternalSource())
2359 PP.setExternalSource(this);
Chris Lattnere7b154b2013-01-19 21:39:22 +00002360
Guy Benyei11169dd2012-12-18 14:30:41 +00002361 if (Stream.SkipBlock() ||
2362 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
2363 Error("malformed block record in AST file");
2364 return true;
2365 }
2366 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
2367 break;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002368
Guy Benyei11169dd2012-12-18 14:30:41 +00002369 case PREPROCESSOR_DETAIL_BLOCK_ID:
2370 F.PreprocessorDetailCursor = Stream;
2371 if (Stream.SkipBlock() ||
Chris Lattnere7b154b2013-01-19 21:39:22 +00002372 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
Guy Benyei11169dd2012-12-18 14:30:41 +00002373 PREPROCESSOR_DETAIL_BLOCK_ID)) {
Chris Lattnere7b154b2013-01-19 21:39:22 +00002374 Error("malformed preprocessor detail record in AST file");
2375 return true;
2376 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002377 F.PreprocessorDetailStartOffset
Chris Lattnere7b154b2013-01-19 21:39:22 +00002378 = F.PreprocessorDetailCursor.GetCurrentBitNo();
2379
Guy Benyei11169dd2012-12-18 14:30:41 +00002380 if (!PP.getPreprocessingRecord())
2381 PP.createPreprocessingRecord();
2382 if (!PP.getPreprocessingRecord()->getExternalSource())
2383 PP.getPreprocessingRecord()->SetExternalSource(*this);
2384 break;
2385
2386 case SOURCE_MANAGER_BLOCK_ID:
2387 if (ReadSourceManagerBlock(F))
2388 return true;
2389 break;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002390
Guy Benyei11169dd2012-12-18 14:30:41 +00002391 case SUBMODULE_BLOCK_ID:
2392 if (ReadSubmoduleBlock(F))
2393 return true;
2394 break;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002395
Guy Benyei11169dd2012-12-18 14:30:41 +00002396 case COMMENTS_BLOCK_ID: {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00002397 BitstreamCursor C = Stream;
Guy Benyei11169dd2012-12-18 14:30:41 +00002398 if (Stream.SkipBlock() ||
2399 ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
2400 Error("malformed comments block in AST file");
2401 return true;
2402 }
2403 CommentsCursors.push_back(std::make_pair(C, &F));
2404 break;
2405 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00002406
Guy Benyei11169dd2012-12-18 14:30:41 +00002407 default:
Chris Lattnere7b154b2013-01-19 21:39:22 +00002408 if (Stream.SkipBlock()) {
2409 Error("malformed block record in AST file");
2410 return true;
2411 }
2412 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002413 }
2414 continue;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002415
2416 case llvm::BitstreamEntry::Record:
2417 // The interesting case.
2418 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002419 }
2420
2421 // Read and process a record.
2422 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00002423 StringRef Blob;
2424 switch ((ASTRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002425 default: // Default behavior: ignore.
2426 break;
2427
2428 case TYPE_OFFSET: {
2429 if (F.LocalNumTypes != 0) {
2430 Error("duplicate TYPE_OFFSET record in AST file");
2431 return true;
2432 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00002433 F.TypeOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002434 F.LocalNumTypes = Record[0];
2435 unsigned LocalBaseTypeIndex = Record[1];
2436 F.BaseTypeIndex = getTotalNumTypes();
2437
2438 if (F.LocalNumTypes > 0) {
2439 // Introduce the global -> local mapping for types within this module.
2440 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
2441
2442 // Introduce the local -> global mapping for types within this module.
2443 F.TypeRemap.insertOrReplace(
2444 std::make_pair(LocalBaseTypeIndex,
2445 F.BaseTypeIndex - LocalBaseTypeIndex));
2446
2447 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
2448 }
2449 break;
2450 }
2451
2452 case DECL_OFFSET: {
2453 if (F.LocalNumDecls != 0) {
2454 Error("duplicate DECL_OFFSET record in AST file");
2455 return true;
2456 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00002457 F.DeclOffsets = (const DeclOffset *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002458 F.LocalNumDecls = Record[0];
2459 unsigned LocalBaseDeclID = Record[1];
2460 F.BaseDeclID = getTotalNumDecls();
2461
2462 if (F.LocalNumDecls > 0) {
2463 // Introduce the global -> local mapping for declarations within this
2464 // module.
2465 GlobalDeclMap.insert(
2466 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
2467
2468 // Introduce the local -> global mapping for declarations within this
2469 // module.
2470 F.DeclRemap.insertOrReplace(
2471 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
2472
2473 // Introduce the global -> local mapping for declarations within this
2474 // module.
2475 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
2476
2477 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
2478 }
2479 break;
2480 }
2481
2482 case TU_UPDATE_LEXICAL: {
2483 DeclContext *TU = Context.getTranslationUnitDecl();
2484 DeclContextInfo &Info = F.DeclContextInfos[TU];
Chris Lattner0e6c9402013-01-20 02:38:54 +00002485 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(Blob.data());
Guy Benyei11169dd2012-12-18 14:30:41 +00002486 Info.NumLexicalDecls
Chris Lattner0e6c9402013-01-20 02:38:54 +00002487 = static_cast<unsigned int>(Blob.size() / sizeof(KindDeclIDPair));
Guy Benyei11169dd2012-12-18 14:30:41 +00002488 TU->setHasExternalLexicalStorage(true);
2489 break;
2490 }
2491
2492 case UPDATE_VISIBLE: {
2493 unsigned Idx = 0;
2494 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
2495 ASTDeclContextNameLookupTable *Table =
2496 ASTDeclContextNameLookupTable::Create(
Chris Lattner0e6c9402013-01-20 02:38:54 +00002497 (const unsigned char *)Blob.data() + Record[Idx++],
2498 (const unsigned char *)Blob.data(),
Guy Benyei11169dd2012-12-18 14:30:41 +00002499 ASTDeclContextNameLookupTrait(*this, F));
2500 if (ID == PREDEF_DECL_TRANSLATION_UNIT_ID) { // Is it the TU?
2501 DeclContext *TU = Context.getTranslationUnitDecl();
Richard Smith52e3fba2014-03-11 07:17:35 +00002502 F.DeclContextInfos[TU].NameLookupTableData = Table;
Guy Benyei11169dd2012-12-18 14:30:41 +00002503 TU->setHasExternalVisibleStorage(true);
Richard Smithd9174792014-03-11 03:10:46 +00002504 } else if (Decl *D = DeclsLoaded[ID - NUM_PREDEF_DECL_IDS]) {
2505 auto *DC = cast<DeclContext>(D);
2506 DC->getPrimaryContext()->setHasExternalVisibleStorage(true);
Richard Smith52e3fba2014-03-11 07:17:35 +00002507 auto *&LookupTable = F.DeclContextInfos[DC].NameLookupTableData;
2508 delete LookupTable;
2509 LookupTable = Table;
Guy Benyei11169dd2012-12-18 14:30:41 +00002510 } else
2511 PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F));
2512 break;
2513 }
2514
2515 case IDENTIFIER_TABLE:
Chris Lattner0e6c9402013-01-20 02:38:54 +00002516 F.IdentifierTableData = Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002517 if (Record[0]) {
2518 F.IdentifierLookupTable
2519 = ASTIdentifierLookupTable::Create(
2520 (const unsigned char *)F.IdentifierTableData + Record[0],
2521 (const unsigned char *)F.IdentifierTableData,
2522 ASTIdentifierLookupTrait(*this, F));
2523
2524 PP.getIdentifierTable().setExternalIdentifierLookup(this);
2525 }
2526 break;
2527
2528 case IDENTIFIER_OFFSET: {
2529 if (F.LocalNumIdentifiers != 0) {
2530 Error("duplicate IDENTIFIER_OFFSET record in AST file");
2531 return true;
2532 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00002533 F.IdentifierOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002534 F.LocalNumIdentifiers = Record[0];
2535 unsigned LocalBaseIdentifierID = Record[1];
2536 F.BaseIdentifierID = getTotalNumIdentifiers();
2537
2538 if (F.LocalNumIdentifiers > 0) {
2539 // Introduce the global -> local mapping for identifiers within this
2540 // module.
2541 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
2542 &F));
2543
2544 // Introduce the local -> global mapping for identifiers within this
2545 // module.
2546 F.IdentifierRemap.insertOrReplace(
2547 std::make_pair(LocalBaseIdentifierID,
2548 F.BaseIdentifierID - LocalBaseIdentifierID));
2549
2550 IdentifiersLoaded.resize(IdentifiersLoaded.size()
2551 + F.LocalNumIdentifiers);
2552 }
2553 break;
2554 }
2555
Ben Langmuir332aafe2014-01-31 01:06:56 +00002556 case EAGERLY_DESERIALIZED_DECLS:
Guy Benyei11169dd2012-12-18 14:30:41 +00002557 for (unsigned I = 0, N = Record.size(); I != N; ++I)
Ben Langmuir332aafe2014-01-31 01:06:56 +00002558 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
Guy Benyei11169dd2012-12-18 14:30:41 +00002559 break;
2560
2561 case SPECIAL_TYPES:
Douglas Gregor44180f82013-02-01 23:45:03 +00002562 if (SpecialTypes.empty()) {
2563 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2564 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
2565 break;
2566 }
2567
2568 if (SpecialTypes.size() != Record.size()) {
2569 Error("invalid special-types record");
2570 return true;
2571 }
2572
2573 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2574 serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
2575 if (!SpecialTypes[I])
2576 SpecialTypes[I] = ID;
2577 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
2578 // merge step?
2579 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002580 break;
2581
2582 case STATISTICS:
2583 TotalNumStatements += Record[0];
2584 TotalNumMacros += Record[1];
2585 TotalLexicalDeclContexts += Record[2];
2586 TotalVisibleDeclContexts += Record[3];
2587 break;
2588
2589 case UNUSED_FILESCOPED_DECLS:
2590 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2591 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
2592 break;
2593
2594 case DELEGATING_CTORS:
2595 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2596 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
2597 break;
2598
2599 case WEAK_UNDECLARED_IDENTIFIERS:
2600 if (Record.size() % 4 != 0) {
2601 Error("invalid weak identifiers record");
2602 return true;
2603 }
2604
2605 // FIXME: Ignore weak undeclared identifiers from non-original PCH
2606 // files. This isn't the way to do it :)
2607 WeakUndeclaredIdentifiers.clear();
2608
2609 // Translate the weak, undeclared identifiers into global IDs.
2610 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
2611 WeakUndeclaredIdentifiers.push_back(
2612 getGlobalIdentifierID(F, Record[I++]));
2613 WeakUndeclaredIdentifiers.push_back(
2614 getGlobalIdentifierID(F, Record[I++]));
2615 WeakUndeclaredIdentifiers.push_back(
2616 ReadSourceLocation(F, Record, I).getRawEncoding());
2617 WeakUndeclaredIdentifiers.push_back(Record[I++]);
2618 }
2619 break;
2620
Richard Smith78165b52013-01-10 23:43:47 +00002621 case LOCALLY_SCOPED_EXTERN_C_DECLS:
Guy Benyei11169dd2012-12-18 14:30:41 +00002622 for (unsigned I = 0, N = Record.size(); I != N; ++I)
Richard Smith78165b52013-01-10 23:43:47 +00002623 LocallyScopedExternCDecls.push_back(getGlobalDeclID(F, Record[I]));
Guy Benyei11169dd2012-12-18 14:30:41 +00002624 break;
2625
2626 case SELECTOR_OFFSETS: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00002627 F.SelectorOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002628 F.LocalNumSelectors = Record[0];
2629 unsigned LocalBaseSelectorID = Record[1];
2630 F.BaseSelectorID = getTotalNumSelectors();
2631
2632 if (F.LocalNumSelectors > 0) {
2633 // Introduce the global -> local mapping for selectors within this
2634 // module.
2635 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
2636
2637 // Introduce the local -> global mapping for selectors within this
2638 // module.
2639 F.SelectorRemap.insertOrReplace(
2640 std::make_pair(LocalBaseSelectorID,
2641 F.BaseSelectorID - LocalBaseSelectorID));
2642
2643 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
2644 }
2645 break;
2646 }
2647
2648 case METHOD_POOL:
Chris Lattner0e6c9402013-01-20 02:38:54 +00002649 F.SelectorLookupTableData = (const unsigned char *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002650 if (Record[0])
2651 F.SelectorLookupTable
2652 = ASTSelectorLookupTable::Create(
2653 F.SelectorLookupTableData + Record[0],
2654 F.SelectorLookupTableData,
2655 ASTSelectorLookupTrait(*this, F));
2656 TotalNumMethodPoolEntries += Record[1];
2657 break;
2658
2659 case REFERENCED_SELECTOR_POOL:
2660 if (!Record.empty()) {
2661 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
2662 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
2663 Record[Idx++]));
2664 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
2665 getRawEncoding());
2666 }
2667 }
2668 break;
2669
2670 case PP_COUNTER_VALUE:
2671 if (!Record.empty() && Listener)
2672 Listener->ReadCounter(F, Record[0]);
2673 break;
2674
2675 case FILE_SORTED_DECLS:
Chris Lattner0e6c9402013-01-20 02:38:54 +00002676 F.FileSortedDecls = (const DeclID *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002677 F.NumFileSortedDecls = Record[0];
2678 break;
2679
2680 case SOURCE_LOCATION_OFFSETS: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00002681 F.SLocEntryOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002682 F.LocalNumSLocEntries = Record[0];
2683 unsigned SLocSpaceSize = Record[1];
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00002684 std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
Guy Benyei11169dd2012-12-18 14:30:41 +00002685 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
2686 SLocSpaceSize);
2687 // Make our entry in the range map. BaseID is negative and growing, so
2688 // we invert it. Because we invert it, though, we need the other end of
2689 // the range.
2690 unsigned RangeStart =
2691 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
2692 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
2693 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
2694
2695 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
2696 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
2697 GlobalSLocOffsetMap.insert(
2698 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
2699 - SLocSpaceSize,&F));
2700
2701 // Initialize the remapping table.
2702 // Invalid stays invalid.
Richard Smithb9eab6d2014-03-20 19:44:17 +00002703 F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
Guy Benyei11169dd2012-12-18 14:30:41 +00002704 // This module. Base was 2 when being compiled.
Richard Smithb9eab6d2014-03-20 19:44:17 +00002705 F.SLocRemap.insertOrReplace(std::make_pair(2U,
Guy Benyei11169dd2012-12-18 14:30:41 +00002706 static_cast<int>(F.SLocEntryBaseOffset - 2)));
2707
2708 TotalNumSLocEntries += F.LocalNumSLocEntries;
2709 break;
2710 }
2711
2712 case MODULE_OFFSET_MAP: {
2713 // Additional remapping information.
Chris Lattner0e6c9402013-01-20 02:38:54 +00002714 const unsigned char *Data = (const unsigned char*)Blob.data();
2715 const unsigned char *DataEnd = Data + Blob.size();
Richard Smithb9eab6d2014-03-20 19:44:17 +00002716
2717 // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
2718 if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
2719 F.SLocRemap.insert(std::make_pair(0U, 0));
2720 F.SLocRemap.insert(std::make_pair(2U, 1));
2721 }
2722
Guy Benyei11169dd2012-12-18 14:30:41 +00002723 // Continuous range maps we may be updating in our module.
2724 ContinuousRangeMap<uint32_t, int, 2>::Builder SLocRemap(F.SLocRemap);
2725 ContinuousRangeMap<uint32_t, int, 2>::Builder
2726 IdentifierRemap(F.IdentifierRemap);
2727 ContinuousRangeMap<uint32_t, int, 2>::Builder
2728 MacroRemap(F.MacroRemap);
2729 ContinuousRangeMap<uint32_t, int, 2>::Builder
2730 PreprocessedEntityRemap(F.PreprocessedEntityRemap);
2731 ContinuousRangeMap<uint32_t, int, 2>::Builder
2732 SubmoduleRemap(F.SubmoduleRemap);
2733 ContinuousRangeMap<uint32_t, int, 2>::Builder
2734 SelectorRemap(F.SelectorRemap);
2735 ContinuousRangeMap<uint32_t, int, 2>::Builder DeclRemap(F.DeclRemap);
2736 ContinuousRangeMap<uint32_t, int, 2>::Builder TypeRemap(F.TypeRemap);
2737
2738 while(Data < DataEnd) {
2739 uint16_t Len = io::ReadUnalignedLE16(Data);
2740 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");
2745 return true;
2746 }
2747
2748 uint32_t SLocOffset = io::ReadUnalignedLE32(Data);
2749 uint32_t IdentifierIDOffset = io::ReadUnalignedLE32(Data);
2750 uint32_t MacroIDOffset = io::ReadUnalignedLE32(Data);
2751 uint32_t PreprocessedEntityIDOffset = io::ReadUnalignedLE32(Data);
2752 uint32_t SubmoduleIDOffset = io::ReadUnalignedLE32(Data);
2753 uint32_t SelectorIDOffset = io::ReadUnalignedLE32(Data);
2754 uint32_t DeclIDOffset = io::ReadUnalignedLE32(Data);
2755 uint32_t TypeIndexOffset = io::ReadUnalignedLE32(Data);
2756
2757 // Source location offset is mapped to OM->SLocEntryBaseOffset.
2758 SLocRemap.insert(std::make_pair(SLocOffset,
2759 static_cast<int>(OM->SLocEntryBaseOffset - SLocOffset)));
2760 IdentifierRemap.insert(
2761 std::make_pair(IdentifierIDOffset,
2762 OM->BaseIdentifierID - IdentifierIDOffset));
2763 MacroRemap.insert(std::make_pair(MacroIDOffset,
2764 OM->BaseMacroID - MacroIDOffset));
2765 PreprocessedEntityRemap.insert(
2766 std::make_pair(PreprocessedEntityIDOffset,
2767 OM->BasePreprocessedEntityID - PreprocessedEntityIDOffset));
2768 SubmoduleRemap.insert(std::make_pair(SubmoduleIDOffset,
2769 OM->BaseSubmoduleID - SubmoduleIDOffset));
2770 SelectorRemap.insert(std::make_pair(SelectorIDOffset,
2771 OM->BaseSelectorID - SelectorIDOffset));
2772 DeclRemap.insert(std::make_pair(DeclIDOffset,
2773 OM->BaseDeclID - DeclIDOffset));
2774
2775 TypeRemap.insert(std::make_pair(TypeIndexOffset,
2776 OM->BaseTypeIndex - TypeIndexOffset));
2777
2778 // Global -> local mappings.
2779 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
2780 }
2781 break;
2782 }
2783
2784 case SOURCE_MANAGER_LINE_TABLE:
2785 if (ParseLineTable(F, Record))
2786 return true;
2787 break;
2788
2789 case SOURCE_LOCATION_PRELOADS: {
2790 // Need to transform from the local view (1-based IDs) to the global view,
2791 // which is based off F.SLocEntryBaseID.
2792 if (!F.PreloadSLocEntries.empty()) {
2793 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
2794 return true;
2795 }
2796
2797 F.PreloadSLocEntries.swap(Record);
2798 break;
2799 }
2800
2801 case EXT_VECTOR_DECLS:
2802 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2803 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
2804 break;
2805
2806 case VTABLE_USES:
2807 if (Record.size() % 3 != 0) {
2808 Error("Invalid VTABLE_USES record");
2809 return true;
2810 }
2811
2812 // Later tables overwrite earlier ones.
2813 // FIXME: Modules will have some trouble with this. This is clearly not
2814 // the right way to do this.
2815 VTableUses.clear();
2816
2817 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
2818 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
2819 VTableUses.push_back(
2820 ReadSourceLocation(F, Record, Idx).getRawEncoding());
2821 VTableUses.push_back(Record[Idx++]);
2822 }
2823 break;
2824
2825 case DYNAMIC_CLASSES:
2826 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2827 DynamicClasses.push_back(getGlobalDeclID(F, Record[I]));
2828 break;
2829
2830 case PENDING_IMPLICIT_INSTANTIATIONS:
2831 if (PendingInstantiations.size() % 2 != 0) {
2832 Error("Invalid existing PendingInstantiations");
2833 return true;
2834 }
2835
2836 if (Record.size() % 2 != 0) {
2837 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
2838 return true;
2839 }
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");
2851 return true;
2852 }
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()
2871 ->allocateLoadedEntities(F.NumPreprocessedEntities);
2872 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");
2892 return true;
2893 }
2894 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
2895 DeclUpdateOffsets[getGlobalDeclID(F, Record[I])]
2896 .push_back(std::make_pair(&F, Record[I+1]));
2897 break;
2898 }
2899
2900 case DECL_REPLACEMENTS: {
2901 if (Record.size() % 3 != 0) {
2902 Error("invalid DECL_REPLACEMENTS block in AST file");
2903 return true;
2904 }
2905 for (unsigned I = 0, N = Record.size(); I != N; I += 3)
2906 ReplacedDecls[getGlobalDeclID(F, Record[I])]
2907 = ReplacedDeclInfo(&F, Record[I+1], Record[I+2]);
2908 break;
2909 }
2910
2911 case OBJC_CATEGORIES_MAP: {
2912 if (F.LocalNumObjCCategoriesInMap != 0) {
2913 Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
2914 return true;
2915 }
2916
2917 F.LocalNumObjCCategoriesInMap = Record[0];
Chris Lattner0e6c9402013-01-20 02:38:54 +00002918 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002919 break;
2920 }
2921
2922 case OBJC_CATEGORIES:
2923 F.ObjCCategories.swap(Record);
2924 break;
2925
2926 case CXX_BASE_SPECIFIER_OFFSETS: {
2927 if (F.LocalNumCXXBaseSpecifiers != 0) {
2928 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2929 return true;
2930 }
2931
2932 F.LocalNumCXXBaseSpecifiers = Record[0];
Chris Lattner0e6c9402013-01-20 02:38:54 +00002933 F.CXXBaseSpecifiersOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002934 NumCXXBaseSpecifiersLoaded += F.LocalNumCXXBaseSpecifiers;
2935 break;
2936 }
2937
2938 case DIAG_PRAGMA_MAPPINGS:
2939 if (F.PragmaDiagMappings.empty())
2940 F.PragmaDiagMappings.swap(Record);
2941 else
2942 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
2943 Record.begin(), Record.end());
2944 break;
2945
2946 case CUDA_SPECIAL_DECL_REFS:
2947 // Later tables overwrite earlier ones.
2948 // FIXME: Modules will have trouble with this.
2949 CUDASpecialDeclRefs.clear();
2950 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2951 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
2952 break;
2953
2954 case HEADER_SEARCH_TABLE: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00002955 F.HeaderFileInfoTableData = Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002956 F.LocalNumHeaderFileInfos = Record[1];
Guy Benyei11169dd2012-12-18 14:30:41 +00002957 if (Record[0]) {
2958 F.HeaderFileInfoTable
2959 = HeaderFileInfoLookupTable::Create(
2960 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
2961 (const unsigned char *)F.HeaderFileInfoTableData,
2962 HeaderFileInfoTrait(*this, F,
2963 &PP.getHeaderSearchInfo(),
Chris Lattner0e6c9402013-01-20 02:38:54 +00002964 Blob.data() + Record[2]));
Guy Benyei11169dd2012-12-18 14:30:41 +00002965
2966 PP.getHeaderSearchInfo().SetExternalSource(this);
2967 if (!PP.getHeaderSearchInfo().getExternalLookup())
2968 PP.getHeaderSearchInfo().SetExternalLookup(this);
2969 }
2970 break;
2971 }
2972
2973 case FP_PRAGMA_OPTIONS:
2974 // Later tables overwrite earlier ones.
2975 FPPragmaOptions.swap(Record);
2976 break;
2977
2978 case OPENCL_EXTENSIONS:
2979 // Later tables overwrite earlier ones.
2980 OpenCLExtensions.swap(Record);
2981 break;
2982
2983 case TENTATIVE_DEFINITIONS:
2984 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2985 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
2986 break;
2987
2988 case KNOWN_NAMESPACES:
2989 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2990 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
2991 break;
Nick Lewycky8334af82013-01-26 00:35:08 +00002992
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00002993 case UNDEFINED_BUT_USED:
2994 if (UndefinedButUsed.size() % 2 != 0) {
2995 Error("Invalid existing UndefinedButUsed");
Nick Lewycky8334af82013-01-26 00:35:08 +00002996 return true;
2997 }
2998
2999 if (Record.size() % 2 != 0) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00003000 Error("invalid undefined-but-used record");
Nick Lewycky8334af82013-01-26 00:35:08 +00003001 return true;
3002 }
3003 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00003004 UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
3005 UndefinedButUsed.push_back(
Nick Lewycky8334af82013-01-26 00:35:08 +00003006 ReadSourceLocation(F, Record, I).getRawEncoding());
3007 }
3008 break;
3009
Guy Benyei11169dd2012-12-18 14:30:41 +00003010 case IMPORTED_MODULES: {
3011 if (F.Kind != MK_Module) {
3012 // If we aren't loading a module (which has its own exports), make
3013 // all of the imported modules visible.
3014 // FIXME: Deal with macros-only imports.
3015 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
3016 if (unsigned GlobalID = getGlobalSubmoduleID(F, Record[I]))
3017 ImportedModules.push_back(GlobalID);
3018 }
3019 }
3020 break;
3021 }
3022
3023 case LOCAL_REDECLARATIONS: {
3024 F.RedeclarationChains.swap(Record);
3025 break;
3026 }
3027
3028 case LOCAL_REDECLARATIONS_MAP: {
3029 if (F.LocalNumRedeclarationsInMap != 0) {
3030 Error("duplicate LOCAL_REDECLARATIONS_MAP record in AST file");
3031 return true;
3032 }
3033
3034 F.LocalNumRedeclarationsInMap = Record[0];
Chris Lattner0e6c9402013-01-20 02:38:54 +00003035 F.RedeclarationsMap = (const LocalRedeclarationsInfo *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003036 break;
3037 }
3038
3039 case MERGED_DECLARATIONS: {
3040 for (unsigned Idx = 0; Idx < Record.size(); /* increment in loop */) {
3041 GlobalDeclID CanonID = getGlobalDeclID(F, Record[Idx++]);
3042 SmallVectorImpl<GlobalDeclID> &Decls = StoredMergedDecls[CanonID];
3043 for (unsigned N = Record[Idx++]; N > 0; --N)
3044 Decls.push_back(getGlobalDeclID(F, Record[Idx++]));
3045 }
3046 break;
3047 }
3048
3049 case MACRO_OFFSET: {
3050 if (F.LocalNumMacros != 0) {
3051 Error("duplicate MACRO_OFFSET record in AST file");
3052 return true;
3053 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00003054 F.MacroOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003055 F.LocalNumMacros = Record[0];
3056 unsigned LocalBaseMacroID = Record[1];
3057 F.BaseMacroID = getTotalNumMacros();
3058
3059 if (F.LocalNumMacros > 0) {
3060 // Introduce the global -> local mapping for macros within this module.
3061 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3062
3063 // Introduce the local -> global mapping for macros within this module.
3064 F.MacroRemap.insertOrReplace(
3065 std::make_pair(LocalBaseMacroID,
3066 F.BaseMacroID - LocalBaseMacroID));
3067
3068 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
3069 }
3070 break;
3071 }
3072
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003073 case MACRO_TABLE: {
3074 // FIXME: Not used yet.
Guy Benyei11169dd2012-12-18 14:30:41 +00003075 break;
3076 }
Richard Smithe40f2ba2013-08-07 21:41:30 +00003077
3078 case LATE_PARSED_TEMPLATE: {
3079 LateParsedTemplates.append(Record.begin(), Record.end());
3080 break;
3081 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003082 }
3083 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003084}
3085
Douglas Gregorc1489562013-02-12 23:36:21 +00003086/// \brief Move the given method to the back of the global list of methods.
3087static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
3088 // Find the entry for this selector in the method pool.
3089 Sema::GlobalMethodPool::iterator Known
3090 = S.MethodPool.find(Method->getSelector());
3091 if (Known == S.MethodPool.end())
3092 return;
3093
3094 // Retrieve the appropriate method list.
3095 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
3096 : Known->second.second;
3097 bool Found = false;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003098 for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
Douglas Gregorc1489562013-02-12 23:36:21 +00003099 if (!Found) {
3100 if (List->Method == Method) {
3101 Found = true;
3102 } else {
3103 // Keep searching.
3104 continue;
3105 }
3106 }
3107
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003108 if (List->getNext())
3109 List->Method = List->getNext()->Method;
Douglas Gregorc1489562013-02-12 23:36:21 +00003110 else
3111 List->Method = Method;
3112 }
3113}
3114
Argyrios Kyrtzidis3a9c42c2013-03-27 01:25:34 +00003115void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
Richard Smith49f906a2014-03-01 00:08:04 +00003116 for (unsigned I = 0, N = Names.HiddenDecls.size(); I != N; ++I) {
3117 Decl *D = Names.HiddenDecls[I];
3118 bool wasHidden = D->Hidden;
3119 D->Hidden = false;
Guy Benyei11169dd2012-12-18 14:30:41 +00003120
Richard Smith49f906a2014-03-01 00:08:04 +00003121 if (wasHidden && SemaObj) {
3122 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
3123 moveMethodToBackOfGlobalList(*SemaObj, Method);
Douglas Gregorc1489562013-02-12 23:36:21 +00003124 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003125 }
3126 }
Richard Smith49f906a2014-03-01 00:08:04 +00003127
3128 for (HiddenMacrosMap::const_iterator I = Names.HiddenMacros.begin(),
3129 E = Names.HiddenMacros.end();
3130 I != E; ++I)
3131 installImportedMacro(I->first, I->second, Owner);
Guy Benyei11169dd2012-12-18 14:30:41 +00003132}
3133
Richard Smith49f906a2014-03-01 00:08:04 +00003134void ASTReader::makeModuleVisible(Module *Mod,
Argyrios Kyrtzidis125df052013-02-01 16:36:12 +00003135 Module::NameVisibilityKind NameVisibility,
Douglas Gregorfb912652013-03-20 21:10:35 +00003136 SourceLocation ImportLoc,
3137 bool Complain) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003138 llvm::SmallPtrSet<Module *, 4> Visited;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003139 SmallVector<Module *, 4> Stack;
Robert Wilhelm25284cc2013-08-23 16:11:15 +00003140 Stack.push_back(Mod);
Guy Benyei11169dd2012-12-18 14:30:41 +00003141 while (!Stack.empty()) {
Robert Wilhelm25284cc2013-08-23 16:11:15 +00003142 Mod = Stack.pop_back_val();
Guy Benyei11169dd2012-12-18 14:30:41 +00003143
3144 if (NameVisibility <= Mod->NameVisibility) {
Robert Wilhelm25284cc2013-08-23 16:11:15 +00003145 // This module already has this level of visibility (or greater), so
Guy Benyei11169dd2012-12-18 14:30:41 +00003146 // there is nothing more to do.
3147 continue;
3148 }
Richard Smith49f906a2014-03-01 00:08:04 +00003149
Guy Benyei11169dd2012-12-18 14:30:41 +00003150 if (!Mod->isAvailable()) {
3151 // Modules that aren't available cannot be made visible.
3152 continue;
3153 }
3154
3155 // Update the module's name visibility.
Richard Smith49f906a2014-03-01 00:08:04 +00003156 if (NameVisibility >= Module::MacrosVisible &&
3157 Mod->NameVisibility < Module::MacrosVisible)
3158 Mod->MacroVisibilityLoc = ImportLoc;
Guy Benyei11169dd2012-12-18 14:30:41 +00003159 Mod->NameVisibility = NameVisibility;
Richard Smith49f906a2014-03-01 00:08:04 +00003160
Guy Benyei11169dd2012-12-18 14:30:41 +00003161 // If we've already deserialized any names from this module,
3162 // mark them as visible.
3163 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
3164 if (Hidden != HiddenNamesMap.end()) {
Argyrios Kyrtzidis3a9c42c2013-03-27 01:25:34 +00003165 makeNamesVisible(Hidden->second, Hidden->first);
Guy Benyei11169dd2012-12-18 14:30:41 +00003166 HiddenNamesMap.erase(Hidden);
3167 }
Dmitri Gribenkoe9bcf5b2013-11-04 21:51:33 +00003168
Guy Benyei11169dd2012-12-18 14:30:41 +00003169 // Push any exported modules onto the stack to be marked as visible.
Argyrios Kyrtzidis8739f7b2013-02-19 19:34:40 +00003170 SmallVector<Module *, 16> Exports;
3171 Mod->getExportedModules(Exports);
3172 for (SmallVectorImpl<Module *>::iterator
3173 I = Exports.begin(), E = Exports.end(); I != E; ++I) {
3174 Module *Exported = *I;
3175 if (Visited.insert(Exported))
3176 Stack.push_back(Exported);
Guy Benyei11169dd2012-12-18 14:30:41 +00003177 }
Douglas Gregorfb912652013-03-20 21:10:35 +00003178
3179 // Detect any conflicts.
3180 if (Complain) {
3181 assert(ImportLoc.isValid() && "Missing import location");
3182 for (unsigned I = 0, N = Mod->Conflicts.size(); I != N; ++I) {
3183 if (Mod->Conflicts[I].Other->NameVisibility >= NameVisibility) {
3184 Diag(ImportLoc, diag::warn_module_conflict)
3185 << Mod->getFullModuleName()
3186 << Mod->Conflicts[I].Other->getFullModuleName()
3187 << Mod->Conflicts[I].Message;
3188 // FIXME: Need note where the other module was imported.
3189 }
3190 }
3191 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003192 }
3193}
3194
Douglas Gregore060e572013-01-25 01:03:03 +00003195bool ASTReader::loadGlobalIndex() {
3196 if (GlobalIndex)
3197 return false;
3198
3199 if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
3200 !Context.getLangOpts().Modules)
3201 return true;
3202
3203 // Try to load the global index.
3204 TriedLoadingGlobalIndex = true;
3205 StringRef ModuleCachePath
3206 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
3207 std::pair<GlobalModuleIndex *, GlobalModuleIndex::ErrorCode> Result
Douglas Gregor7029ce12013-03-19 00:28:20 +00003208 = GlobalModuleIndex::readIndex(ModuleCachePath);
Douglas Gregore060e572013-01-25 01:03:03 +00003209 if (!Result.first)
3210 return true;
3211
3212 GlobalIndex.reset(Result.first);
Douglas Gregor7211ac12013-01-25 23:32:03 +00003213 ModuleMgr.setGlobalIndex(GlobalIndex.get());
Douglas Gregore060e572013-01-25 01:03:03 +00003214 return false;
3215}
3216
3217bool ASTReader::isGlobalIndexUnavailable() const {
3218 return Context.getLangOpts().Modules && UseGlobalIndex &&
3219 !hasGlobalIndex() && TriedLoadingGlobalIndex;
3220}
3221
Dmitri Gribenkof430da42014-02-12 10:33:14 +00003222static void updateModuleTimestamp(ModuleFile &MF) {
3223 // Overwrite the timestamp file contents so that file's mtime changes.
3224 std::string TimestampFilename = MF.getTimestampFilename();
3225 std::string ErrorInfo;
Rafael Espindola04a13be2014-02-24 15:06:52 +00003226 llvm::raw_fd_ostream OS(TimestampFilename.c_str(), ErrorInfo,
Rafael Espindola4fbd3732014-02-24 18:20:21 +00003227 llvm::sys::fs::F_Text);
Dmitri Gribenkof430da42014-02-12 10:33:14 +00003228 if (!ErrorInfo.empty())
3229 return;
3230 OS << "Timestamp file\n";
3231}
3232
Guy Benyei11169dd2012-12-18 14:30:41 +00003233ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
3234 ModuleKind Type,
3235 SourceLocation ImportLoc,
3236 unsigned ClientLoadCapabilities) {
Argyrios Kyrtzidisdc9fdaf2013-05-24 05:44:08 +00003237 llvm::SaveAndRestore<SourceLocation>
3238 SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
3239
Guy Benyei11169dd2012-12-18 14:30:41 +00003240 // Bump the generation number.
3241 unsigned PreviousGeneration = CurrentGeneration++;
3242
3243 unsigned NumModules = ModuleMgr.size();
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003244 SmallVector<ImportedModule, 4> Loaded;
Guy Benyei11169dd2012-12-18 14:30:41 +00003245 switch(ASTReadResult ReadResult = ReadASTCore(FileName, Type, ImportLoc,
3246 /*ImportedBy=*/0, Loaded,
Douglas Gregor7029ce12013-03-19 00:28:20 +00003247 0, 0,
Guy Benyei11169dd2012-12-18 14:30:41 +00003248 ClientLoadCapabilities)) {
3249 case Failure:
Douglas Gregor7029ce12013-03-19 00:28:20 +00003250 case Missing:
Guy Benyei11169dd2012-12-18 14:30:41 +00003251 case OutOfDate:
3252 case VersionMismatch:
3253 case ConfigurationMismatch:
3254 case HadErrors:
Douglas Gregor7029ce12013-03-19 00:28:20 +00003255 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, ModuleMgr.end(),
3256 Context.getLangOpts().Modules
3257 ? &PP.getHeaderSearchInfo().getModuleMap()
3258 : 0);
Douglas Gregore060e572013-01-25 01:03:03 +00003259
3260 // If we find that any modules are unusable, the global index is going
3261 // to be out-of-date. Just remove it.
3262 GlobalIndex.reset();
Douglas Gregor7211ac12013-01-25 23:32:03 +00003263 ModuleMgr.setGlobalIndex(0);
Guy Benyei11169dd2012-12-18 14:30:41 +00003264 return ReadResult;
3265
3266 case Success:
3267 break;
3268 }
3269
3270 // Here comes stuff that we only do once the entire chain is loaded.
3271
3272 // Load the AST blocks of all of the modules that we loaded.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003273 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
3274 MEnd = Loaded.end();
Guy Benyei11169dd2012-12-18 14:30:41 +00003275 M != MEnd; ++M) {
3276 ModuleFile &F = *M->Mod;
3277
3278 // Read the AST block.
3279 if (ReadASTBlock(F))
3280 return Failure;
3281
3282 // Once read, set the ModuleFile bit base offset and update the size in
3283 // bits of all files we've seen.
3284 F.GlobalBitOffset = TotalModulesSizeInBits;
3285 TotalModulesSizeInBits += F.SizeInBits;
3286 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
3287
3288 // Preload SLocEntries.
3289 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
3290 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
3291 // Load it through the SourceManager and don't call ReadSLocEntry()
3292 // directly because the entry may have already been loaded in which case
3293 // calling ReadSLocEntry() directly would trigger an assertion in
3294 // SourceManager.
3295 SourceMgr.getLoadedSLocEntryByID(Index);
3296 }
3297 }
3298
Douglas Gregor603cd862013-03-22 18:50:14 +00003299 // Setup the import locations and notify the module manager that we've
3300 // committed to these module files.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003301 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
3302 MEnd = Loaded.end();
Guy Benyei11169dd2012-12-18 14:30:41 +00003303 M != MEnd; ++M) {
3304 ModuleFile &F = *M->Mod;
Douglas Gregor603cd862013-03-22 18:50:14 +00003305
3306 ModuleMgr.moduleFileAccepted(&F);
3307
3308 // Set the import location.
Argyrios Kyrtzidis71c1af82013-02-01 16:36:14 +00003309 F.DirectImportLoc = ImportLoc;
Guy Benyei11169dd2012-12-18 14:30:41 +00003310 if (!M->ImportedBy)
3311 F.ImportLoc = M->ImportLoc;
3312 else
3313 F.ImportLoc = ReadSourceLocation(*M->ImportedBy,
3314 M->ImportLoc.getRawEncoding());
3315 }
3316
3317 // Mark all of the identifiers in the identifier table as being out of date,
3318 // so that various accessors know to check the loaded modules when the
3319 // identifier is used.
3320 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
3321 IdEnd = PP.getIdentifierTable().end();
3322 Id != IdEnd; ++Id)
3323 Id->second->setOutOfDate(true);
3324
3325 // Resolve any unresolved module exports.
Douglas Gregorfb912652013-03-20 21:10:35 +00003326 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
3327 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
Guy Benyei11169dd2012-12-18 14:30:41 +00003328 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
3329 Module *ResolvedMod = getSubmodule(GlobalID);
Douglas Gregorfb912652013-03-20 21:10:35 +00003330
3331 switch (Unresolved.Kind) {
3332 case UnresolvedModuleRef::Conflict:
3333 if (ResolvedMod) {
3334 Module::Conflict Conflict;
3335 Conflict.Other = ResolvedMod;
3336 Conflict.Message = Unresolved.String.str();
3337 Unresolved.Mod->Conflicts.push_back(Conflict);
3338 }
3339 continue;
3340
3341 case UnresolvedModuleRef::Import:
Guy Benyei11169dd2012-12-18 14:30:41 +00003342 if (ResolvedMod)
3343 Unresolved.Mod->Imports.push_back(ResolvedMod);
3344 continue;
Guy Benyei11169dd2012-12-18 14:30:41 +00003345
Douglas Gregorfb912652013-03-20 21:10:35 +00003346 case UnresolvedModuleRef::Export:
3347 if (ResolvedMod || Unresolved.IsWildcard)
3348 Unresolved.Mod->Exports.push_back(
3349 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
3350 continue;
3351 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003352 }
Douglas Gregorfb912652013-03-20 21:10:35 +00003353 UnresolvedModuleRefs.clear();
Daniel Jasperba7f2f72013-09-24 09:14:14 +00003354
3355 // FIXME: How do we load the 'use'd modules? They may not be submodules.
3356 // Might be unnecessary as use declarations are only used to build the
3357 // module itself.
Guy Benyei11169dd2012-12-18 14:30:41 +00003358
3359 InitializeContext();
3360
Richard Smith3d8e97e2013-10-18 06:54:39 +00003361 if (SemaObj)
3362 UpdateSema();
3363
Guy Benyei11169dd2012-12-18 14:30:41 +00003364 if (DeserializationListener)
3365 DeserializationListener->ReaderInitialized(this);
3366
3367 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
3368 if (!PrimaryModule.OriginalSourceFileID.isInvalid()) {
3369 PrimaryModule.OriginalSourceFileID
3370 = FileID::get(PrimaryModule.SLocEntryBaseID
3371 + PrimaryModule.OriginalSourceFileID.getOpaqueValue() - 1);
3372
3373 // If this AST file is a precompiled preamble, then set the
3374 // preamble file ID of the source manager to the file source file
3375 // from which the preamble was built.
3376 if (Type == MK_Preamble) {
3377 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
3378 } else if (Type == MK_MainFile) {
3379 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
3380 }
3381 }
3382
3383 // For any Objective-C class definitions we have already loaded, make sure
3384 // that we load any additional categories.
3385 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
3386 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
3387 ObjCClassesLoaded[I],
3388 PreviousGeneration);
3389 }
Douglas Gregore060e572013-01-25 01:03:03 +00003390
Dmitri Gribenkof430da42014-02-12 10:33:14 +00003391 if (PP.getHeaderSearchInfo()
3392 .getHeaderSearchOpts()
3393 .ModulesValidateOncePerBuildSession) {
3394 // Now we are certain that the module and all modules it depends on are
3395 // up to date. Create or update timestamp files for modules that are
3396 // located in the module cache (not for PCH files that could be anywhere
3397 // in the filesystem).
3398 for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
3399 ImportedModule &M = Loaded[I];
3400 if (M.Mod->Kind == MK_Module) {
3401 updateModuleTimestamp(*M.Mod);
3402 }
3403 }
3404 }
3405
Guy Benyei11169dd2012-12-18 14:30:41 +00003406 return Success;
3407}
3408
3409ASTReader::ASTReadResult
3410ASTReader::ReadASTCore(StringRef FileName,
3411 ModuleKind Type,
3412 SourceLocation ImportLoc,
3413 ModuleFile *ImportedBy,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003414 SmallVectorImpl<ImportedModule> &Loaded,
Douglas Gregor7029ce12013-03-19 00:28:20 +00003415 off_t ExpectedSize, time_t ExpectedModTime,
Guy Benyei11169dd2012-12-18 14:30:41 +00003416 unsigned ClientLoadCapabilities) {
3417 ModuleFile *M;
Guy Benyei11169dd2012-12-18 14:30:41 +00003418 std::string ErrorStr;
Douglas Gregor7029ce12013-03-19 00:28:20 +00003419 ModuleManager::AddModuleResult AddResult
3420 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
3421 CurrentGeneration, ExpectedSize, ExpectedModTime,
3422 M, ErrorStr);
Guy Benyei11169dd2012-12-18 14:30:41 +00003423
Douglas Gregor7029ce12013-03-19 00:28:20 +00003424 switch (AddResult) {
3425 case ModuleManager::AlreadyLoaded:
3426 return Success;
3427
3428 case ModuleManager::NewlyLoaded:
3429 // Load module file below.
3430 break;
3431
3432 case ModuleManager::Missing:
3433 // The module file was missing; if the client handle handle, that, return
3434 // it.
3435 if (ClientLoadCapabilities & ARR_Missing)
3436 return Missing;
3437
3438 // Otherwise, return an error.
3439 {
3440 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
3441 + ErrorStr;
3442 Error(Msg);
3443 }
3444 return Failure;
3445
3446 case ModuleManager::OutOfDate:
3447 // We couldn't load the module file because it is out-of-date. If the
3448 // client can handle out-of-date, return it.
3449 if (ClientLoadCapabilities & ARR_OutOfDate)
3450 return OutOfDate;
3451
3452 // Otherwise, return an error.
3453 {
3454 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
3455 + ErrorStr;
3456 Error(Msg);
3457 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003458 return Failure;
3459 }
3460
Douglas Gregor7029ce12013-03-19 00:28:20 +00003461 assert(M && "Missing module file");
Guy Benyei11169dd2012-12-18 14:30:41 +00003462
3463 // FIXME: This seems rather a hack. Should CurrentDir be part of the
3464 // module?
3465 if (FileName != "-") {
3466 CurrentDir = llvm::sys::path::parent_path(FileName);
3467 if (CurrentDir.empty()) CurrentDir = ".";
3468 }
3469
3470 ModuleFile &F = *M;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003471 BitstreamCursor &Stream = F.Stream;
Guy Benyei11169dd2012-12-18 14:30:41 +00003472 Stream.init(F.StreamFile);
3473 F.SizeInBits = F.Buffer->getBufferSize() * 8;
3474
3475 // Sniff for the signature.
3476 if (Stream.Read(8) != 'C' ||
3477 Stream.Read(8) != 'P' ||
3478 Stream.Read(8) != 'C' ||
3479 Stream.Read(8) != 'H') {
3480 Diag(diag::err_not_a_pch_file) << FileName;
3481 return Failure;
3482 }
3483
3484 // This is used for compatibility with older PCH formats.
3485 bool HaveReadControlBlock = false;
3486
Chris Lattnerefa77172013-01-20 00:00:22 +00003487 while (1) {
3488 llvm::BitstreamEntry Entry = Stream.advance();
3489
3490 switch (Entry.Kind) {
3491 case llvm::BitstreamEntry::Error:
3492 case llvm::BitstreamEntry::EndBlock:
3493 case llvm::BitstreamEntry::Record:
Guy Benyei11169dd2012-12-18 14:30:41 +00003494 Error("invalid record at top-level of AST file");
3495 return Failure;
Chris Lattnerefa77172013-01-20 00:00:22 +00003496
3497 case llvm::BitstreamEntry::SubBlock:
3498 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003499 }
3500
Guy Benyei11169dd2012-12-18 14:30:41 +00003501 // We only know the control subblock ID.
Chris Lattnerefa77172013-01-20 00:00:22 +00003502 switch (Entry.ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003503 case llvm::bitc::BLOCKINFO_BLOCK_ID:
3504 if (Stream.ReadBlockInfoBlock()) {
3505 Error("malformed BlockInfoBlock in AST file");
3506 return Failure;
3507 }
3508 break;
3509 case CONTROL_BLOCK_ID:
3510 HaveReadControlBlock = true;
3511 switch (ReadControlBlock(F, Loaded, ClientLoadCapabilities)) {
3512 case Success:
3513 break;
3514
3515 case Failure: return Failure;
Douglas Gregor7029ce12013-03-19 00:28:20 +00003516 case Missing: return Missing;
Guy Benyei11169dd2012-12-18 14:30:41 +00003517 case OutOfDate: return OutOfDate;
3518 case VersionMismatch: return VersionMismatch;
3519 case ConfigurationMismatch: return ConfigurationMismatch;
3520 case HadErrors: return HadErrors;
3521 }
3522 break;
3523 case AST_BLOCK_ID:
3524 if (!HaveReadControlBlock) {
3525 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
Dmitri Gribenko2228cd32014-02-11 15:40:09 +00003526 Diag(diag::err_pch_version_too_old);
Guy Benyei11169dd2012-12-18 14:30:41 +00003527 return VersionMismatch;
3528 }
3529
3530 // Record that we've loaded this module.
3531 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
3532 return Success;
3533
3534 default:
3535 if (Stream.SkipBlock()) {
3536 Error("malformed block record in AST file");
3537 return Failure;
3538 }
3539 break;
3540 }
3541 }
3542
3543 return Success;
3544}
3545
3546void ASTReader::InitializeContext() {
3547 // If there's a listener, notify them that we "read" the translation unit.
3548 if (DeserializationListener)
3549 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
3550 Context.getTranslationUnitDecl());
3551
3552 // Make sure we load the declaration update records for the translation unit,
3553 // if there are any.
3554 loadDeclUpdateRecords(PREDEF_DECL_TRANSLATION_UNIT_ID,
3555 Context.getTranslationUnitDecl());
3556
3557 // FIXME: Find a better way to deal with collisions between these
3558 // built-in types. Right now, we just ignore the problem.
3559
3560 // Load the special types.
3561 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
3562 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
3563 if (!Context.CFConstantStringTypeDecl)
3564 Context.setCFConstantStringType(GetType(String));
3565 }
3566
3567 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
3568 QualType FileType = GetType(File);
3569 if (FileType.isNull()) {
3570 Error("FILE type is NULL");
3571 return;
3572 }
3573
3574 if (!Context.FILEDecl) {
3575 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
3576 Context.setFILEDecl(Typedef->getDecl());
3577 else {
3578 const TagType *Tag = FileType->getAs<TagType>();
3579 if (!Tag) {
3580 Error("Invalid FILE type in AST file");
3581 return;
3582 }
3583 Context.setFILEDecl(Tag->getDecl());
3584 }
3585 }
3586 }
3587
3588 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
3589 QualType Jmp_bufType = GetType(Jmp_buf);
3590 if (Jmp_bufType.isNull()) {
3591 Error("jmp_buf type is NULL");
3592 return;
3593 }
3594
3595 if (!Context.jmp_bufDecl) {
3596 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
3597 Context.setjmp_bufDecl(Typedef->getDecl());
3598 else {
3599 const TagType *Tag = Jmp_bufType->getAs<TagType>();
3600 if (!Tag) {
3601 Error("Invalid jmp_buf type in AST file");
3602 return;
3603 }
3604 Context.setjmp_bufDecl(Tag->getDecl());
3605 }
3606 }
3607 }
3608
3609 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
3610 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
3611 if (Sigjmp_bufType.isNull()) {
3612 Error("sigjmp_buf type is NULL");
3613 return;
3614 }
3615
3616 if (!Context.sigjmp_bufDecl) {
3617 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
3618 Context.setsigjmp_bufDecl(Typedef->getDecl());
3619 else {
3620 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
3621 assert(Tag && "Invalid sigjmp_buf type in AST file");
3622 Context.setsigjmp_bufDecl(Tag->getDecl());
3623 }
3624 }
3625 }
3626
3627 if (unsigned ObjCIdRedef
3628 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
3629 if (Context.ObjCIdRedefinitionType.isNull())
3630 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
3631 }
3632
3633 if (unsigned ObjCClassRedef
3634 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
3635 if (Context.ObjCClassRedefinitionType.isNull())
3636 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
3637 }
3638
3639 if (unsigned ObjCSelRedef
3640 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
3641 if (Context.ObjCSelRedefinitionType.isNull())
3642 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
3643 }
3644
3645 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
3646 QualType Ucontext_tType = GetType(Ucontext_t);
3647 if (Ucontext_tType.isNull()) {
3648 Error("ucontext_t type is NULL");
3649 return;
3650 }
3651
3652 if (!Context.ucontext_tDecl) {
3653 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
3654 Context.setucontext_tDecl(Typedef->getDecl());
3655 else {
3656 const TagType *Tag = Ucontext_tType->getAs<TagType>();
3657 assert(Tag && "Invalid ucontext_t type in AST file");
3658 Context.setucontext_tDecl(Tag->getDecl());
3659 }
3660 }
3661 }
3662 }
3663
3664 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
3665
3666 // If there were any CUDA special declarations, deserialize them.
3667 if (!CUDASpecialDeclRefs.empty()) {
3668 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
3669 Context.setcudaConfigureCallDecl(
3670 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
3671 }
3672
3673 // Re-export any modules that were imported by a non-module AST file.
3674 for (unsigned I = 0, N = ImportedModules.size(); I != N; ++I) {
3675 if (Module *Imported = getSubmodule(ImportedModules[I]))
Argyrios Kyrtzidis125df052013-02-01 16:36:12 +00003676 makeModuleVisible(Imported, Module::AllVisible,
Douglas Gregorfb912652013-03-20 21:10:35 +00003677 /*ImportLoc=*/SourceLocation(),
3678 /*Complain=*/false);
Guy Benyei11169dd2012-12-18 14:30:41 +00003679 }
3680 ImportedModules.clear();
3681}
3682
3683void ASTReader::finalizeForWriting() {
3684 for (HiddenNamesMapType::iterator Hidden = HiddenNamesMap.begin(),
3685 HiddenEnd = HiddenNamesMap.end();
3686 Hidden != HiddenEnd; ++Hidden) {
Argyrios Kyrtzidis3a9c42c2013-03-27 01:25:34 +00003687 makeNamesVisible(Hidden->second, Hidden->first);
Guy Benyei11169dd2012-12-18 14:30:41 +00003688 }
3689 HiddenNamesMap.clear();
3690}
3691
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00003692/// \brief Given a cursor at the start of an AST file, scan ahead and drop the
3693/// cursor into the start of the given block ID, returning false on success and
3694/// true on failure.
3695static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003696 while (1) {
3697 llvm::BitstreamEntry Entry = Cursor.advance();
3698 switch (Entry.Kind) {
3699 case llvm::BitstreamEntry::Error:
3700 case llvm::BitstreamEntry::EndBlock:
3701 return true;
3702
3703 case llvm::BitstreamEntry::Record:
3704 // Ignore top-level records.
3705 Cursor.skipRecord(Entry.ID);
3706 break;
3707
3708 case llvm::BitstreamEntry::SubBlock:
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00003709 if (Entry.ID == BlockID) {
3710 if (Cursor.EnterSubBlock(BlockID))
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003711 return true;
3712 // Found it!
3713 return false;
3714 }
3715
3716 if (Cursor.SkipBlock())
3717 return true;
3718 }
3719 }
3720}
3721
Guy Benyei11169dd2012-12-18 14:30:41 +00003722/// \brief Retrieve the name of the original source file name
3723/// directly from the AST file, without actually loading the AST
3724/// file.
3725std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
3726 FileManager &FileMgr,
3727 DiagnosticsEngine &Diags) {
3728 // Open the AST file.
3729 std::string ErrStr;
Ahmed Charlesb8984322014-03-07 20:03:18 +00003730 std::unique_ptr<llvm::MemoryBuffer> Buffer;
Guy Benyei11169dd2012-12-18 14:30:41 +00003731 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
3732 if (!Buffer) {
3733 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ASTFileName << ErrStr;
3734 return std::string();
3735 }
3736
3737 // Initialize the stream
3738 llvm::BitstreamReader StreamFile;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003739 BitstreamCursor Stream;
Guy Benyei11169dd2012-12-18 14:30:41 +00003740 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
3741 (const unsigned char *)Buffer->getBufferEnd());
3742 Stream.init(StreamFile);
3743
3744 // Sniff for the signature.
3745 if (Stream.Read(8) != 'C' ||
3746 Stream.Read(8) != 'P' ||
3747 Stream.Read(8) != 'C' ||
3748 Stream.Read(8) != 'H') {
3749 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
3750 return std::string();
3751 }
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003752
Chris Lattnere7b154b2013-01-19 21:39:22 +00003753 // Scan for the CONTROL_BLOCK_ID block.
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00003754 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003755 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
3756 return std::string();
Chris Lattnere7b154b2013-01-19 21:39:22 +00003757 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003758
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003759 // Scan for ORIGINAL_FILE inside the control block.
3760 RecordData Record;
Chris Lattnere7b154b2013-01-19 21:39:22 +00003761 while (1) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003762 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Chris Lattnere7b154b2013-01-19 21:39:22 +00003763 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
3764 return std::string();
3765
3766 if (Entry.Kind != llvm::BitstreamEntry::Record) {
3767 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
3768 return std::string();
Guy Benyei11169dd2012-12-18 14:30:41 +00003769 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00003770
Guy Benyei11169dd2012-12-18 14:30:41 +00003771 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00003772 StringRef Blob;
3773 if (Stream.readRecord(Entry.ID, Record, &Blob) == ORIGINAL_FILE)
3774 return Blob.str();
Guy Benyei11169dd2012-12-18 14:30:41 +00003775 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003776}
3777
3778namespace {
3779 class SimplePCHValidator : public ASTReaderListener {
3780 const LangOptions &ExistingLangOpts;
3781 const TargetOptions &ExistingTargetOpts;
3782 const PreprocessorOptions &ExistingPPOpts;
3783 FileManager &FileMgr;
3784
3785 public:
3786 SimplePCHValidator(const LangOptions &ExistingLangOpts,
3787 const TargetOptions &ExistingTargetOpts,
3788 const PreprocessorOptions &ExistingPPOpts,
3789 FileManager &FileMgr)
3790 : ExistingLangOpts(ExistingLangOpts),
3791 ExistingTargetOpts(ExistingTargetOpts),
3792 ExistingPPOpts(ExistingPPOpts),
3793 FileMgr(FileMgr)
3794 {
3795 }
3796
Craig Topper3e89dfe2014-03-13 02:13:41 +00003797 bool ReadLanguageOptions(const LangOptions &LangOpts,
3798 bool Complain) override {
Guy Benyei11169dd2012-12-18 14:30:41 +00003799 return checkLanguageOptions(ExistingLangOpts, LangOpts, 0);
3800 }
Craig Topper3e89dfe2014-03-13 02:13:41 +00003801 bool ReadTargetOptions(const TargetOptions &TargetOpts,
3802 bool Complain) override {
Guy Benyei11169dd2012-12-18 14:30:41 +00003803 return checkTargetOptions(ExistingTargetOpts, TargetOpts, 0);
3804 }
Craig Topper3e89dfe2014-03-13 02:13:41 +00003805 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
3806 bool Complain,
3807 std::string &SuggestedPredefines) override {
Guy Benyei11169dd2012-12-18 14:30:41 +00003808 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, 0, FileMgr,
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00003809 SuggestedPredefines, ExistingLangOpts);
Guy Benyei11169dd2012-12-18 14:30:41 +00003810 }
3811 };
3812}
3813
3814bool ASTReader::readASTFileControlBlock(StringRef Filename,
3815 FileManager &FileMgr,
3816 ASTReaderListener &Listener) {
3817 // Open the AST file.
3818 std::string ErrStr;
Ahmed Charlesb8984322014-03-07 20:03:18 +00003819 std::unique_ptr<llvm::MemoryBuffer> Buffer;
Guy Benyei11169dd2012-12-18 14:30:41 +00003820 Buffer.reset(FileMgr.getBufferForFile(Filename, &ErrStr));
3821 if (!Buffer) {
3822 return true;
3823 }
3824
3825 // Initialize the stream
3826 llvm::BitstreamReader StreamFile;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003827 BitstreamCursor Stream;
Guy Benyei11169dd2012-12-18 14:30:41 +00003828 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
3829 (const unsigned char *)Buffer->getBufferEnd());
3830 Stream.init(StreamFile);
3831
3832 // Sniff for the signature.
3833 if (Stream.Read(8) != 'C' ||
3834 Stream.Read(8) != 'P' ||
3835 Stream.Read(8) != 'C' ||
3836 Stream.Read(8) != 'H') {
3837 return true;
3838 }
3839
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003840 // Scan for the CONTROL_BLOCK_ID block.
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00003841 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003842 return true;
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00003843
3844 bool NeedsInputFiles = Listener.needsInputFileVisitation();
Ben Langmuircb69b572014-03-07 06:40:32 +00003845 bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00003846 BitstreamCursor InputFilesCursor;
3847 if (NeedsInputFiles) {
3848 InputFilesCursor = Stream;
3849 if (SkipCursorToBlock(InputFilesCursor, INPUT_FILES_BLOCK_ID))
3850 return true;
3851
3852 // Read the abbreviations
3853 while (true) {
3854 uint64_t Offset = InputFilesCursor.GetCurrentBitNo();
3855 unsigned Code = InputFilesCursor.ReadCode();
3856
3857 // We expect all abbrevs to be at the start of the block.
3858 if (Code != llvm::bitc::DEFINE_ABBREV) {
3859 InputFilesCursor.JumpToBit(Offset);
3860 break;
3861 }
3862 InputFilesCursor.ReadAbbrevRecord();
3863 }
3864 }
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003865
3866 // Scan for ORIGINAL_FILE inside the control block.
Guy Benyei11169dd2012-12-18 14:30:41 +00003867 RecordData Record;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003868 while (1) {
3869 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
3870 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
3871 return false;
3872
3873 if (Entry.Kind != llvm::BitstreamEntry::Record)
3874 return true;
3875
Guy Benyei11169dd2012-12-18 14:30:41 +00003876 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00003877 StringRef Blob;
3878 unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob);
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003879 switch ((ControlRecordTypes)RecCode) {
3880 case METADATA: {
3881 if (Record[0] != VERSION_MAJOR)
3882 return true;
Guy Benyei11169dd2012-12-18 14:30:41 +00003883
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +00003884 if (Listener.ReadFullVersionInformation(Blob))
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003885 return true;
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +00003886
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003887 break;
3888 }
3889 case LANGUAGE_OPTIONS:
3890 if (ParseLanguageOptions(Record, false, Listener))
3891 return true;
3892 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003893
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003894 case TARGET_OPTIONS:
3895 if (ParseTargetOptions(Record, false, Listener))
3896 return true;
3897 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003898
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003899 case DIAGNOSTIC_OPTIONS:
3900 if (ParseDiagnosticOptions(Record, false, Listener))
3901 return true;
3902 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003903
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003904 case FILE_SYSTEM_OPTIONS:
3905 if (ParseFileSystemOptions(Record, false, Listener))
3906 return true;
3907 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003908
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003909 case HEADER_SEARCH_OPTIONS:
3910 if (ParseHeaderSearchOptions(Record, false, Listener))
3911 return true;
3912 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003913
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003914 case PREPROCESSOR_OPTIONS: {
3915 std::string IgnoredSuggestedPredefines;
3916 if (ParsePreprocessorOptions(Record, false, Listener,
3917 IgnoredSuggestedPredefines))
3918 return true;
3919 break;
3920 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003921
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00003922 case INPUT_FILE_OFFSETS: {
3923 if (!NeedsInputFiles)
3924 break;
3925
3926 unsigned NumInputFiles = Record[0];
3927 unsigned NumUserFiles = Record[1];
3928 const uint32_t *InputFileOffs = (const uint32_t *)Blob.data();
3929 for (unsigned I = 0; I != NumInputFiles; ++I) {
3930 // Go find this input file.
3931 bool isSystemFile = I >= NumUserFiles;
Ben Langmuircb69b572014-03-07 06:40:32 +00003932
3933 if (isSystemFile && !NeedsSystemInputFiles)
3934 break; // the rest are system input files
3935
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00003936 BitstreamCursor &Cursor = InputFilesCursor;
3937 SavedStreamPosition SavedPosition(Cursor);
3938 Cursor.JumpToBit(InputFileOffs[I]);
3939
3940 unsigned Code = Cursor.ReadCode();
3941 RecordData Record;
3942 StringRef Blob;
3943 bool shouldContinue = false;
3944 switch ((InputFileRecordTypes)Cursor.readRecord(Code, Record, &Blob)) {
3945 case INPUT_FILE:
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +00003946 bool Overridden = static_cast<bool>(Record[3]);
3947 shouldContinue = Listener.visitInputFile(Blob, isSystemFile, Overridden);
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00003948 break;
3949 }
3950 if (!shouldContinue)
3951 break;
3952 }
3953 break;
3954 }
3955
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003956 default:
3957 // No other validation to perform.
3958 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003959 }
3960 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003961}
3962
3963
3964bool ASTReader::isAcceptableASTFile(StringRef Filename,
3965 FileManager &FileMgr,
3966 const LangOptions &LangOpts,
3967 const TargetOptions &TargetOpts,
3968 const PreprocessorOptions &PPOpts) {
3969 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts, FileMgr);
3970 return !readASTFileControlBlock(Filename, FileMgr, validator);
3971}
3972
3973bool ASTReader::ReadSubmoduleBlock(ModuleFile &F) {
3974 // Enter the submodule block.
3975 if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
3976 Error("malformed submodule block record in AST file");
3977 return true;
3978 }
3979
3980 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
3981 bool First = true;
3982 Module *CurrentModule = 0;
3983 RecordData Record;
3984 while (true) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003985 llvm::BitstreamEntry Entry = F.Stream.advanceSkippingSubblocks();
3986
3987 switch (Entry.Kind) {
3988 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
3989 case llvm::BitstreamEntry::Error:
3990 Error("malformed block record in AST file");
3991 return true;
3992 case llvm::BitstreamEntry::EndBlock:
Guy Benyei11169dd2012-12-18 14:30:41 +00003993 return false;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003994 case llvm::BitstreamEntry::Record:
3995 // The interesting case.
3996 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003997 }
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003998
Guy Benyei11169dd2012-12-18 14:30:41 +00003999 // Read a record.
Chris Lattner0e6c9402013-01-20 02:38:54 +00004000 StringRef Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00004001 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00004002 switch (F.Stream.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004003 default: // Default behavior: ignore.
4004 break;
4005
4006 case SUBMODULE_DEFINITION: {
4007 if (First) {
4008 Error("missing submodule metadata record at beginning of block");
4009 return true;
4010 }
4011
Douglas Gregor8d932422013-03-20 03:59:18 +00004012 if (Record.size() < 8) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004013 Error("malformed module definition");
4014 return true;
4015 }
4016
Chris Lattner0e6c9402013-01-20 02:38:54 +00004017 StringRef Name = Blob;
Richard Smith9bca2982014-03-08 00:03:56 +00004018 unsigned Idx = 0;
4019 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
4020 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
4021 bool IsFramework = Record[Idx++];
4022 bool IsExplicit = Record[Idx++];
4023 bool IsSystem = Record[Idx++];
4024 bool IsExternC = Record[Idx++];
4025 bool InferSubmodules = Record[Idx++];
4026 bool InferExplicitSubmodules = Record[Idx++];
4027 bool InferExportWildcard = Record[Idx++];
4028 bool ConfigMacrosExhaustive = Record[Idx++];
Douglas Gregor8d932422013-03-20 03:59:18 +00004029
Guy Benyei11169dd2012-12-18 14:30:41 +00004030 Module *ParentModule = 0;
4031 if (Parent)
4032 ParentModule = getSubmodule(Parent);
4033
4034 // Retrieve this (sub)module from the module map, creating it if
4035 // necessary.
4036 CurrentModule = ModMap.findOrCreateModule(Name, ParentModule,
4037 IsFramework,
4038 IsExplicit).first;
4039 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
4040 if (GlobalIndex >= SubmodulesLoaded.size() ||
4041 SubmodulesLoaded[GlobalIndex]) {
4042 Error("too many submodules");
4043 return true;
4044 }
Douglas Gregor8a114ab2013-02-06 22:40:31 +00004045
Douglas Gregor7029ce12013-03-19 00:28:20 +00004046 if (!ParentModule) {
4047 if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
4048 if (CurFile != F.File) {
4049 if (!Diags.isDiagnosticInFlight()) {
4050 Diag(diag::err_module_file_conflict)
4051 << CurrentModule->getTopLevelModuleName()
4052 << CurFile->getName()
4053 << F.File->getName();
4054 }
4055 return true;
Douglas Gregor8a114ab2013-02-06 22:40:31 +00004056 }
Douglas Gregor8a114ab2013-02-06 22:40:31 +00004057 }
Douglas Gregor7029ce12013-03-19 00:28:20 +00004058
4059 CurrentModule->setASTFile(F.File);
Douglas Gregor8a114ab2013-02-06 22:40:31 +00004060 }
Douglas Gregor7029ce12013-03-19 00:28:20 +00004061
Guy Benyei11169dd2012-12-18 14:30:41 +00004062 CurrentModule->IsFromModuleFile = true;
4063 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
Richard Smith9bca2982014-03-08 00:03:56 +00004064 CurrentModule->IsExternC = IsExternC;
Guy Benyei11169dd2012-12-18 14:30:41 +00004065 CurrentModule->InferSubmodules = InferSubmodules;
4066 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
4067 CurrentModule->InferExportWildcard = InferExportWildcard;
Douglas Gregor8d932422013-03-20 03:59:18 +00004068 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
Guy Benyei11169dd2012-12-18 14:30:41 +00004069 if (DeserializationListener)
4070 DeserializationListener->ModuleRead(GlobalID, CurrentModule);
4071
4072 SubmodulesLoaded[GlobalIndex] = CurrentModule;
Douglas Gregor6ddfca92013-01-14 17:21:00 +00004073
Douglas Gregorfb912652013-03-20 21:10:35 +00004074 // Clear out data that will be replaced by what is the module file.
Douglas Gregor6ddfca92013-01-14 17:21:00 +00004075 CurrentModule->LinkLibraries.clear();
Douglas Gregor8d932422013-03-20 03:59:18 +00004076 CurrentModule->ConfigMacros.clear();
Douglas Gregorfb912652013-03-20 21:10:35 +00004077 CurrentModule->UnresolvedConflicts.clear();
4078 CurrentModule->Conflicts.clear();
Guy Benyei11169dd2012-12-18 14:30:41 +00004079 break;
4080 }
4081
4082 case SUBMODULE_UMBRELLA_HEADER: {
4083 if (First) {
4084 Error("missing submodule metadata record at beginning of block");
4085 return true;
4086 }
4087
4088 if (!CurrentModule)
4089 break;
4090
Chris Lattner0e6c9402013-01-20 02:38:54 +00004091 if (const FileEntry *Umbrella = PP.getFileManager().getFile(Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004092 if (!CurrentModule->getUmbrellaHeader())
4093 ModMap.setUmbrellaHeader(CurrentModule, Umbrella);
4094 else if (CurrentModule->getUmbrellaHeader() != Umbrella) {
4095 Error("mismatched umbrella headers in submodule");
4096 return true;
4097 }
4098 }
4099 break;
4100 }
4101
4102 case SUBMODULE_HEADER: {
4103 if (First) {
4104 Error("missing submodule metadata record at beginning of block");
4105 return true;
4106 }
4107
4108 if (!CurrentModule)
4109 break;
4110
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00004111 // We lazily associate headers with their modules via the HeaderInfoTable.
4112 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
4113 // of complete filenames or remove it entirely.
Guy Benyei11169dd2012-12-18 14:30:41 +00004114 break;
4115 }
4116
4117 case SUBMODULE_EXCLUDED_HEADER: {
4118 if (First) {
4119 Error("missing submodule metadata record at beginning of block");
4120 return true;
4121 }
4122
4123 if (!CurrentModule)
4124 break;
4125
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00004126 // We lazily associate headers with their modules via the HeaderInfoTable.
4127 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
4128 // of complete filenames or remove it entirely.
Guy Benyei11169dd2012-12-18 14:30:41 +00004129 break;
4130 }
4131
Lawrence Crowlb53e5482013-06-20 21:14:14 +00004132 case SUBMODULE_PRIVATE_HEADER: {
4133 if (First) {
4134 Error("missing submodule metadata record at beginning of block");
4135 return true;
4136 }
4137
4138 if (!CurrentModule)
4139 break;
4140
4141 // We lazily associate headers with their modules via the HeaderInfoTable.
4142 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
4143 // of complete filenames or remove it entirely.
4144 break;
4145 }
4146
Guy Benyei11169dd2012-12-18 14:30:41 +00004147 case SUBMODULE_TOPHEADER: {
4148 if (First) {
4149 Error("missing submodule metadata record at beginning of block");
4150 return true;
4151 }
4152
4153 if (!CurrentModule)
4154 break;
4155
Argyrios Kyrtzidis3c5305c2013-03-13 21:13:43 +00004156 CurrentModule->addTopHeaderFilename(Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +00004157 break;
4158 }
4159
4160 case SUBMODULE_UMBRELLA_DIR: {
4161 if (First) {
4162 Error("missing submodule metadata record at beginning of block");
4163 return true;
4164 }
4165
4166 if (!CurrentModule)
4167 break;
4168
Guy Benyei11169dd2012-12-18 14:30:41 +00004169 if (const DirectoryEntry *Umbrella
Chris Lattner0e6c9402013-01-20 02:38:54 +00004170 = PP.getFileManager().getDirectory(Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004171 if (!CurrentModule->getUmbrellaDir())
4172 ModMap.setUmbrellaDir(CurrentModule, Umbrella);
4173 else if (CurrentModule->getUmbrellaDir() != Umbrella) {
4174 Error("mismatched umbrella directories in submodule");
4175 return true;
4176 }
4177 }
4178 break;
4179 }
4180
4181 case SUBMODULE_METADATA: {
4182 if (!First) {
4183 Error("submodule metadata record not at beginning of block");
4184 return true;
4185 }
4186 First = false;
4187
4188 F.BaseSubmoduleID = getTotalNumSubmodules();
4189 F.LocalNumSubmodules = Record[0];
4190 unsigned LocalBaseSubmoduleID = Record[1];
4191 if (F.LocalNumSubmodules > 0) {
4192 // Introduce the global -> local mapping for submodules within this
4193 // module.
4194 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
4195
4196 // Introduce the local -> global mapping for submodules within this
4197 // module.
4198 F.SubmoduleRemap.insertOrReplace(
4199 std::make_pair(LocalBaseSubmoduleID,
4200 F.BaseSubmoduleID - LocalBaseSubmoduleID));
4201
4202 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
4203 }
4204 break;
4205 }
4206
4207 case SUBMODULE_IMPORTS: {
4208 if (First) {
4209 Error("missing submodule metadata record at beginning of block");
4210 return true;
4211 }
4212
4213 if (!CurrentModule)
4214 break;
4215
4216 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
Douglas Gregorfb912652013-03-20 21:10:35 +00004217 UnresolvedModuleRef Unresolved;
Guy Benyei11169dd2012-12-18 14:30:41 +00004218 Unresolved.File = &F;
4219 Unresolved.Mod = CurrentModule;
4220 Unresolved.ID = Record[Idx];
Douglas Gregorfb912652013-03-20 21:10:35 +00004221 Unresolved.Kind = UnresolvedModuleRef::Import;
Guy Benyei11169dd2012-12-18 14:30:41 +00004222 Unresolved.IsWildcard = false;
Douglas Gregorfb912652013-03-20 21:10:35 +00004223 UnresolvedModuleRefs.push_back(Unresolved);
Guy Benyei11169dd2012-12-18 14:30:41 +00004224 }
4225 break;
4226 }
4227
4228 case SUBMODULE_EXPORTS: {
4229 if (First) {
4230 Error("missing submodule metadata record at beginning of block");
4231 return true;
4232 }
4233
4234 if (!CurrentModule)
4235 break;
4236
4237 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
Douglas Gregorfb912652013-03-20 21:10:35 +00004238 UnresolvedModuleRef Unresolved;
Guy Benyei11169dd2012-12-18 14:30:41 +00004239 Unresolved.File = &F;
4240 Unresolved.Mod = CurrentModule;
4241 Unresolved.ID = Record[Idx];
Douglas Gregorfb912652013-03-20 21:10:35 +00004242 Unresolved.Kind = UnresolvedModuleRef::Export;
Guy Benyei11169dd2012-12-18 14:30:41 +00004243 Unresolved.IsWildcard = Record[Idx + 1];
Douglas Gregorfb912652013-03-20 21:10:35 +00004244 UnresolvedModuleRefs.push_back(Unresolved);
Guy Benyei11169dd2012-12-18 14:30:41 +00004245 }
4246
4247 // Once we've loaded the set of exports, there's no reason to keep
4248 // the parsed, unresolved exports around.
4249 CurrentModule->UnresolvedExports.clear();
4250 break;
4251 }
4252 case SUBMODULE_REQUIRES: {
4253 if (First) {
4254 Error("missing submodule metadata record at beginning of block");
4255 return true;
4256 }
4257
4258 if (!CurrentModule)
4259 break;
4260
Richard Smitha3feee22013-10-28 22:18:19 +00004261 CurrentModule->addRequirement(Blob, Record[0], Context.getLangOpts(),
Guy Benyei11169dd2012-12-18 14:30:41 +00004262 Context.getTargetInfo());
4263 break;
4264 }
Douglas Gregor6ddfca92013-01-14 17:21:00 +00004265
4266 case SUBMODULE_LINK_LIBRARY:
4267 if (First) {
4268 Error("missing submodule metadata record at beginning of block");
4269 return true;
4270 }
4271
4272 if (!CurrentModule)
4273 break;
4274
4275 CurrentModule->LinkLibraries.push_back(
Chris Lattner0e6c9402013-01-20 02:38:54 +00004276 Module::LinkLibrary(Blob, Record[0]));
Douglas Gregor6ddfca92013-01-14 17:21:00 +00004277 break;
Douglas Gregor35b13ec2013-03-20 00:22:05 +00004278
4279 case SUBMODULE_CONFIG_MACRO:
4280 if (First) {
4281 Error("missing submodule metadata record at beginning of block");
4282 return true;
4283 }
4284
4285 if (!CurrentModule)
4286 break;
4287
4288 CurrentModule->ConfigMacros.push_back(Blob.str());
4289 break;
Douglas Gregorfb912652013-03-20 21:10:35 +00004290
4291 case SUBMODULE_CONFLICT: {
4292 if (First) {
4293 Error("missing submodule metadata record at beginning of block");
4294 return true;
4295 }
4296
4297 if (!CurrentModule)
4298 break;
4299
4300 UnresolvedModuleRef Unresolved;
4301 Unresolved.File = &F;
4302 Unresolved.Mod = CurrentModule;
4303 Unresolved.ID = Record[0];
4304 Unresolved.Kind = UnresolvedModuleRef::Conflict;
4305 Unresolved.IsWildcard = false;
4306 Unresolved.String = Blob;
4307 UnresolvedModuleRefs.push_back(Unresolved);
4308 break;
4309 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004310 }
4311 }
4312}
4313
4314/// \brief Parse the record that corresponds to a LangOptions data
4315/// structure.
4316///
4317/// This routine parses the language options from the AST file and then gives
4318/// them to the AST listener if one is set.
4319///
4320/// \returns true if the listener deems the file unacceptable, false otherwise.
4321bool ASTReader::ParseLanguageOptions(const RecordData &Record,
4322 bool Complain,
4323 ASTReaderListener &Listener) {
4324 LangOptions LangOpts;
4325 unsigned Idx = 0;
4326#define LANGOPT(Name, Bits, Default, Description) \
4327 LangOpts.Name = Record[Idx++];
4328#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
4329 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
4330#include "clang/Basic/LangOptions.def"
Will Dietzf54319c2013-01-18 11:30:38 +00004331#define SANITIZER(NAME, ID) LangOpts.Sanitize.ID = Record[Idx++];
4332#include "clang/Basic/Sanitizers.def"
Guy Benyei11169dd2012-12-18 14:30:41 +00004333
4334 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
4335 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
4336 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
4337
4338 unsigned Length = Record[Idx++];
4339 LangOpts.CurrentModule.assign(Record.begin() + Idx,
4340 Record.begin() + Idx + Length);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00004341
4342 Idx += Length;
4343
4344 // Comment options.
4345 for (unsigned N = Record[Idx++]; N; --N) {
4346 LangOpts.CommentOpts.BlockCommandNames.push_back(
4347 ReadString(Record, Idx));
4348 }
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00004349 LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00004350
Guy Benyei11169dd2012-12-18 14:30:41 +00004351 return Listener.ReadLanguageOptions(LangOpts, Complain);
4352}
4353
4354bool ASTReader::ParseTargetOptions(const RecordData &Record,
4355 bool Complain,
4356 ASTReaderListener &Listener) {
4357 unsigned Idx = 0;
4358 TargetOptions TargetOpts;
4359 TargetOpts.Triple = ReadString(Record, Idx);
4360 TargetOpts.CPU = ReadString(Record, Idx);
4361 TargetOpts.ABI = ReadString(Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00004362 TargetOpts.LinkerVersion = ReadString(Record, Idx);
4363 for (unsigned N = Record[Idx++]; N; --N) {
4364 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
4365 }
4366 for (unsigned N = Record[Idx++]; N; --N) {
4367 TargetOpts.Features.push_back(ReadString(Record, Idx));
4368 }
4369
4370 return Listener.ReadTargetOptions(TargetOpts, Complain);
4371}
4372
4373bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
4374 ASTReaderListener &Listener) {
4375 DiagnosticOptions DiagOpts;
4376 unsigned Idx = 0;
4377#define DIAGOPT(Name, Bits, Default) DiagOpts.Name = Record[Idx++];
4378#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
4379 DiagOpts.set##Name(static_cast<Type>(Record[Idx++]));
4380#include "clang/Basic/DiagnosticOptions.def"
4381
4382 for (unsigned N = Record[Idx++]; N; --N) {
4383 DiagOpts.Warnings.push_back(ReadString(Record, Idx));
4384 }
4385
4386 return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
4387}
4388
4389bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
4390 ASTReaderListener &Listener) {
4391 FileSystemOptions FSOpts;
4392 unsigned Idx = 0;
4393 FSOpts.WorkingDir = ReadString(Record, Idx);
4394 return Listener.ReadFileSystemOptions(FSOpts, Complain);
4395}
4396
4397bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
4398 bool Complain,
4399 ASTReaderListener &Listener) {
4400 HeaderSearchOptions HSOpts;
4401 unsigned Idx = 0;
4402 HSOpts.Sysroot = ReadString(Record, Idx);
4403
4404 // Include entries.
4405 for (unsigned N = Record[Idx++]; N; --N) {
4406 std::string Path = ReadString(Record, Idx);
4407 frontend::IncludeDirGroup Group
4408 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
Guy Benyei11169dd2012-12-18 14:30:41 +00004409 bool IsFramework = Record[Idx++];
4410 bool IgnoreSysRoot = Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00004411 HSOpts.UserEntries.push_back(
Daniel Dunbar53681732013-01-30 00:34:26 +00004412 HeaderSearchOptions::Entry(Path, Group, IsFramework, IgnoreSysRoot));
Guy Benyei11169dd2012-12-18 14:30:41 +00004413 }
4414
4415 // System header prefixes.
4416 for (unsigned N = Record[Idx++]; N; --N) {
4417 std::string Prefix = ReadString(Record, Idx);
4418 bool IsSystemHeader = Record[Idx++];
4419 HSOpts.SystemHeaderPrefixes.push_back(
4420 HeaderSearchOptions::SystemHeaderPrefix(Prefix, IsSystemHeader));
4421 }
4422
4423 HSOpts.ResourceDir = ReadString(Record, Idx);
4424 HSOpts.ModuleCachePath = ReadString(Record, Idx);
Argyrios Kyrtzidis1594c152014-03-03 08:12:05 +00004425 HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00004426 HSOpts.DisableModuleHash = Record[Idx++];
4427 HSOpts.UseBuiltinIncludes = Record[Idx++];
4428 HSOpts.UseStandardSystemIncludes = Record[Idx++];
4429 HSOpts.UseStandardCXXIncludes = Record[Idx++];
4430 HSOpts.UseLibcxx = Record[Idx++];
4431
4432 return Listener.ReadHeaderSearchOptions(HSOpts, Complain);
4433}
4434
4435bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
4436 bool Complain,
4437 ASTReaderListener &Listener,
4438 std::string &SuggestedPredefines) {
4439 PreprocessorOptions PPOpts;
4440 unsigned Idx = 0;
4441
4442 // Macro definitions/undefs
4443 for (unsigned N = Record[Idx++]; N; --N) {
4444 std::string Macro = ReadString(Record, Idx);
4445 bool IsUndef = Record[Idx++];
4446 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
4447 }
4448
4449 // Includes
4450 for (unsigned N = Record[Idx++]; N; --N) {
4451 PPOpts.Includes.push_back(ReadString(Record, Idx));
4452 }
4453
4454 // Macro Includes
4455 for (unsigned N = Record[Idx++]; N; --N) {
4456 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
4457 }
4458
4459 PPOpts.UsePredefines = Record[Idx++];
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00004460 PPOpts.DetailedRecord = Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00004461 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
4462 PPOpts.ImplicitPTHInclude = ReadString(Record, Idx);
4463 PPOpts.ObjCXXARCStandardLibrary =
4464 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
4465 SuggestedPredefines.clear();
4466 return Listener.ReadPreprocessorOptions(PPOpts, Complain,
4467 SuggestedPredefines);
4468}
4469
4470std::pair<ModuleFile *, unsigned>
4471ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
4472 GlobalPreprocessedEntityMapType::iterator
4473 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
4474 assert(I != GlobalPreprocessedEntityMap.end() &&
4475 "Corrupted global preprocessed entity map");
4476 ModuleFile *M = I->second;
4477 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
4478 return std::make_pair(M, LocalIndex);
4479}
4480
4481std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
4482ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
4483 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
4484 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
4485 Mod.NumPreprocessedEntities);
4486
4487 return std::make_pair(PreprocessingRecord::iterator(),
4488 PreprocessingRecord::iterator());
4489}
4490
4491std::pair<ASTReader::ModuleDeclIterator, ASTReader::ModuleDeclIterator>
4492ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
4493 return std::make_pair(ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
4494 ModuleDeclIterator(this, &Mod,
4495 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
4496}
4497
4498PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
4499 PreprocessedEntityID PPID = Index+1;
4500 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
4501 ModuleFile &M = *PPInfo.first;
4502 unsigned LocalIndex = PPInfo.second;
4503 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
4504
Guy Benyei11169dd2012-12-18 14:30:41 +00004505 if (!PP.getPreprocessingRecord()) {
4506 Error("no preprocessing record");
4507 return 0;
4508 }
4509
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004510 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
4511 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
4512
4513 llvm::BitstreamEntry Entry =
4514 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
4515 if (Entry.Kind != llvm::BitstreamEntry::Record)
4516 return 0;
4517
Guy Benyei11169dd2012-12-18 14:30:41 +00004518 // Read the record.
4519 SourceRange Range(ReadSourceLocation(M, PPOffs.Begin),
4520 ReadSourceLocation(M, PPOffs.End));
4521 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
Chris Lattner0e6c9402013-01-20 02:38:54 +00004522 StringRef Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00004523 RecordData Record;
4524 PreprocessorDetailRecordTypes RecType =
Chris Lattner0e6c9402013-01-20 02:38:54 +00004525 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.readRecord(
4526 Entry.ID, Record, &Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +00004527 switch (RecType) {
4528 case PPD_MACRO_EXPANSION: {
4529 bool isBuiltin = Record[0];
4530 IdentifierInfo *Name = 0;
4531 MacroDefinition *Def = 0;
4532 if (isBuiltin)
4533 Name = getLocalIdentifier(M, Record[1]);
4534 else {
4535 PreprocessedEntityID
4536 GlobalID = getGlobalPreprocessedEntityID(M, Record[1]);
4537 Def =cast<MacroDefinition>(PPRec.getLoadedPreprocessedEntity(GlobalID-1));
4538 }
4539
4540 MacroExpansion *ME;
4541 if (isBuiltin)
4542 ME = new (PPRec) MacroExpansion(Name, Range);
4543 else
4544 ME = new (PPRec) MacroExpansion(Def, Range);
4545
4546 return ME;
4547 }
4548
4549 case PPD_MACRO_DEFINITION: {
4550 // Decode the identifier info and then check again; if the macro is
4551 // still defined and associated with the identifier,
4552 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
4553 MacroDefinition *MD
4554 = new (PPRec) MacroDefinition(II, Range);
4555
4556 if (DeserializationListener)
4557 DeserializationListener->MacroDefinitionRead(PPID, MD);
4558
4559 return MD;
4560 }
4561
4562 case PPD_INCLUSION_DIRECTIVE: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00004563 const char *FullFileNameStart = Blob.data() + Record[0];
4564 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
Guy Benyei11169dd2012-12-18 14:30:41 +00004565 const FileEntry *File = 0;
4566 if (!FullFileName.empty())
4567 File = PP.getFileManager().getFile(FullFileName);
4568
4569 // FIXME: Stable encoding
4570 InclusionDirective::InclusionKind Kind
4571 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
4572 InclusionDirective *ID
4573 = new (PPRec) InclusionDirective(PPRec, Kind,
Chris Lattner0e6c9402013-01-20 02:38:54 +00004574 StringRef(Blob.data(), Record[0]),
Guy Benyei11169dd2012-12-18 14:30:41 +00004575 Record[1], Record[3],
4576 File,
4577 Range);
4578 return ID;
4579 }
4580 }
4581
4582 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
4583}
4584
4585/// \brief \arg SLocMapI points at a chunk of a module that contains no
4586/// preprocessed entities or the entities it contains are not the ones we are
4587/// looking for. Find the next module that contains entities and return the ID
4588/// of the first entry.
4589PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
4590 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
4591 ++SLocMapI;
4592 for (GlobalSLocOffsetMapType::const_iterator
4593 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
4594 ModuleFile &M = *SLocMapI->second;
4595 if (M.NumPreprocessedEntities)
4596 return M.BasePreprocessedEntityID;
4597 }
4598
4599 return getTotalNumPreprocessedEntities();
4600}
4601
4602namespace {
4603
4604template <unsigned PPEntityOffset::*PPLoc>
4605struct PPEntityComp {
4606 const ASTReader &Reader;
4607 ModuleFile &M;
4608
4609 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) { }
4610
4611 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
4612 SourceLocation LHS = getLoc(L);
4613 SourceLocation RHS = getLoc(R);
4614 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4615 }
4616
4617 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
4618 SourceLocation LHS = getLoc(L);
4619 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4620 }
4621
4622 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
4623 SourceLocation RHS = getLoc(R);
4624 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4625 }
4626
4627 SourceLocation getLoc(const PPEntityOffset &PPE) const {
4628 return Reader.ReadSourceLocation(M, PPE.*PPLoc);
4629 }
4630};
4631
4632}
4633
4634/// \brief Returns the first preprocessed entity ID that ends after \arg BLoc.
4635PreprocessedEntityID
4636ASTReader::findBeginPreprocessedEntity(SourceLocation BLoc) const {
4637 if (SourceMgr.isLocalSourceLocation(BLoc))
4638 return getTotalNumPreprocessedEntities();
4639
4640 GlobalSLocOffsetMapType::const_iterator
4641 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
Argyrios Kyrtzidis503c83a2013-03-08 02:32:34 +00004642 BLoc.getOffset() - 1);
Guy Benyei11169dd2012-12-18 14:30:41 +00004643 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
4644 "Corrupted global sloc offset map");
4645
4646 if (SLocMapI->second->NumPreprocessedEntities == 0)
4647 return findNextPreprocessedEntity(SLocMapI);
4648
4649 ModuleFile &M = *SLocMapI->second;
4650 typedef const PPEntityOffset *pp_iterator;
4651 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
4652 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
4653
4654 size_t Count = M.NumPreprocessedEntities;
4655 size_t Half;
4656 pp_iterator First = pp_begin;
4657 pp_iterator PPI;
4658
4659 // Do a binary search manually instead of using std::lower_bound because
4660 // The end locations of entities may be unordered (when a macro expansion
4661 // is inside another macro argument), but for this case it is not important
4662 // whether we get the first macro expansion or its containing macro.
4663 while (Count > 0) {
4664 Half = Count/2;
4665 PPI = First;
4666 std::advance(PPI, Half);
4667 if (SourceMgr.isBeforeInTranslationUnit(ReadSourceLocation(M, PPI->End),
4668 BLoc)){
4669 First = PPI;
4670 ++First;
4671 Count = Count - Half - 1;
4672 } else
4673 Count = Half;
4674 }
4675
4676 if (PPI == pp_end)
4677 return findNextPreprocessedEntity(SLocMapI);
4678
4679 return M.BasePreprocessedEntityID + (PPI - pp_begin);
4680}
4681
4682/// \brief Returns the first preprocessed entity ID that begins after \arg ELoc.
4683PreprocessedEntityID
4684ASTReader::findEndPreprocessedEntity(SourceLocation ELoc) const {
4685 if (SourceMgr.isLocalSourceLocation(ELoc))
4686 return getTotalNumPreprocessedEntities();
4687
4688 GlobalSLocOffsetMapType::const_iterator
4689 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
Argyrios Kyrtzidis503c83a2013-03-08 02:32:34 +00004690 ELoc.getOffset() - 1);
Guy Benyei11169dd2012-12-18 14:30:41 +00004691 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
4692 "Corrupted global sloc offset map");
4693
4694 if (SLocMapI->second->NumPreprocessedEntities == 0)
4695 return findNextPreprocessedEntity(SLocMapI);
4696
4697 ModuleFile &M = *SLocMapI->second;
4698 typedef const PPEntityOffset *pp_iterator;
4699 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
4700 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
4701 pp_iterator PPI =
4702 std::upper_bound(pp_begin, pp_end, ELoc,
4703 PPEntityComp<&PPEntityOffset::Begin>(*this, M));
4704
4705 if (PPI == pp_end)
4706 return findNextPreprocessedEntity(SLocMapI);
4707
4708 return M.BasePreprocessedEntityID + (PPI - pp_begin);
4709}
4710
4711/// \brief Returns a pair of [Begin, End) indices of preallocated
4712/// preprocessed entities that \arg Range encompasses.
4713std::pair<unsigned, unsigned>
4714 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
4715 if (Range.isInvalid())
4716 return std::make_pair(0,0);
4717 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
4718
4719 PreprocessedEntityID BeginID = findBeginPreprocessedEntity(Range.getBegin());
4720 PreprocessedEntityID EndID = findEndPreprocessedEntity(Range.getEnd());
4721 return std::make_pair(BeginID, EndID);
4722}
4723
4724/// \brief Optionally returns true or false if the preallocated preprocessed
4725/// entity with index \arg Index came from file \arg FID.
David Blaikie05785d12013-02-20 22:23:23 +00004726Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
Guy Benyei11169dd2012-12-18 14:30:41 +00004727 FileID FID) {
4728 if (FID.isInvalid())
4729 return false;
4730
4731 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
4732 ModuleFile &M = *PPInfo.first;
4733 unsigned LocalIndex = PPInfo.second;
4734 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
4735
4736 SourceLocation Loc = ReadSourceLocation(M, PPOffs.Begin);
4737 if (Loc.isInvalid())
4738 return false;
4739
4740 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
4741 return true;
4742 else
4743 return false;
4744}
4745
4746namespace {
4747 /// \brief Visitor used to search for information about a header file.
4748 class HeaderFileInfoVisitor {
Guy Benyei11169dd2012-12-18 14:30:41 +00004749 const FileEntry *FE;
4750
David Blaikie05785d12013-02-20 22:23:23 +00004751 Optional<HeaderFileInfo> HFI;
Guy Benyei11169dd2012-12-18 14:30:41 +00004752
4753 public:
Argyrios Kyrtzidis61a38962013-03-06 18:12:44 +00004754 explicit HeaderFileInfoVisitor(const FileEntry *FE)
4755 : FE(FE) { }
Guy Benyei11169dd2012-12-18 14:30:41 +00004756
4757 static bool visit(ModuleFile &M, void *UserData) {
4758 HeaderFileInfoVisitor *This
4759 = static_cast<HeaderFileInfoVisitor *>(UserData);
4760
Guy Benyei11169dd2012-12-18 14:30:41 +00004761 HeaderFileInfoLookupTable *Table
4762 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
4763 if (!Table)
4764 return false;
4765
4766 // Look in the on-disk hash table for an entry for this file name.
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00004767 HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE);
Guy Benyei11169dd2012-12-18 14:30:41 +00004768 if (Pos == Table->end())
4769 return false;
4770
4771 This->HFI = *Pos;
4772 return true;
4773 }
4774
David Blaikie05785d12013-02-20 22:23:23 +00004775 Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
Guy Benyei11169dd2012-12-18 14:30:41 +00004776 };
4777}
4778
4779HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
Argyrios Kyrtzidis61a38962013-03-06 18:12:44 +00004780 HeaderFileInfoVisitor Visitor(FE);
Guy Benyei11169dd2012-12-18 14:30:41 +00004781 ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor);
Argyrios Kyrtzidis1054bbf2013-05-08 23:46:55 +00004782 if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
Guy Benyei11169dd2012-12-18 14:30:41 +00004783 return *HFI;
Guy Benyei11169dd2012-12-18 14:30:41 +00004784
4785 return HeaderFileInfo();
4786}
4787
4788void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
4789 // FIXME: Make it work properly with modules.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004790 SmallVector<DiagnosticsEngine::DiagState *, 32> DiagStates;
Guy Benyei11169dd2012-12-18 14:30:41 +00004791 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
4792 ModuleFile &F = *(*I);
4793 unsigned Idx = 0;
4794 DiagStates.clear();
4795 assert(!Diag.DiagStates.empty());
4796 DiagStates.push_back(&Diag.DiagStates.front()); // the command-line one.
4797 while (Idx < F.PragmaDiagMappings.size()) {
4798 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
4799 unsigned DiagStateID = F.PragmaDiagMappings[Idx++];
4800 if (DiagStateID != 0) {
4801 Diag.DiagStatePoints.push_back(
4802 DiagnosticsEngine::DiagStatePoint(DiagStates[DiagStateID-1],
4803 FullSourceLoc(Loc, SourceMgr)));
4804 continue;
4805 }
4806
4807 assert(DiagStateID == 0);
4808 // A new DiagState was created here.
4809 Diag.DiagStates.push_back(*Diag.GetCurDiagState());
4810 DiagnosticsEngine::DiagState *NewState = &Diag.DiagStates.back();
4811 DiagStates.push_back(NewState);
4812 Diag.DiagStatePoints.push_back(
4813 DiagnosticsEngine::DiagStatePoint(NewState,
4814 FullSourceLoc(Loc, SourceMgr)));
4815 while (1) {
4816 assert(Idx < F.PragmaDiagMappings.size() &&
4817 "Invalid data, didn't find '-1' marking end of diag/map pairs");
4818 if (Idx >= F.PragmaDiagMappings.size()) {
4819 break; // Something is messed up but at least avoid infinite loop in
4820 // release build.
4821 }
4822 unsigned DiagID = F.PragmaDiagMappings[Idx++];
4823 if (DiagID == (unsigned)-1) {
4824 break; // no more diag/map pairs for this location.
4825 }
4826 diag::Mapping Map = (diag::Mapping)F.PragmaDiagMappings[Idx++];
4827 DiagnosticMappingInfo MappingInfo = Diag.makeMappingInfo(Map, Loc);
4828 Diag.GetCurDiagState()->setMappingInfo(DiagID, MappingInfo);
4829 }
4830 }
4831 }
4832}
4833
4834/// \brief Get the correct cursor and offset for loading a type.
4835ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
4836 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
4837 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
4838 ModuleFile *M = I->second;
4839 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
4840}
4841
4842/// \brief Read and return the type with the given index..
4843///
4844/// The index is the type ID, shifted and minus the number of predefs. This
4845/// routine actually reads the record corresponding to the type at the given
4846/// location. It is a helper routine for GetType, which deals with reading type
4847/// IDs.
4848QualType ASTReader::readTypeRecord(unsigned Index) {
4849 RecordLocation Loc = TypeCursorForIndex(Index);
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004850 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00004851
4852 // Keep track of where we are in the stream, then jump back there
4853 // after reading this type.
4854 SavedStreamPosition SavedPosition(DeclsCursor);
4855
4856 ReadingKindTracker ReadingKind(Read_Type, *this);
4857
4858 // Note that we are loading a type record.
4859 Deserializing AType(this);
4860
4861 unsigned Idx = 0;
4862 DeclsCursor.JumpToBit(Loc.Offset);
4863 RecordData Record;
4864 unsigned Code = DeclsCursor.ReadCode();
Chris Lattner0e6c9402013-01-20 02:38:54 +00004865 switch ((TypeCode)DeclsCursor.readRecord(Code, Record)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004866 case TYPE_EXT_QUAL: {
4867 if (Record.size() != 2) {
4868 Error("Incorrect encoding of extended qualifier type");
4869 return QualType();
4870 }
4871 QualType Base = readType(*Loc.F, Record, Idx);
4872 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
4873 return Context.getQualifiedType(Base, Quals);
4874 }
4875
4876 case TYPE_COMPLEX: {
4877 if (Record.size() != 1) {
4878 Error("Incorrect encoding of complex type");
4879 return QualType();
4880 }
4881 QualType ElemType = readType(*Loc.F, Record, Idx);
4882 return Context.getComplexType(ElemType);
4883 }
4884
4885 case TYPE_POINTER: {
4886 if (Record.size() != 1) {
4887 Error("Incorrect encoding of pointer type");
4888 return QualType();
4889 }
4890 QualType PointeeType = readType(*Loc.F, Record, Idx);
4891 return Context.getPointerType(PointeeType);
4892 }
4893
Reid Kleckner8a365022013-06-24 17:51:48 +00004894 case TYPE_DECAYED: {
4895 if (Record.size() != 1) {
4896 Error("Incorrect encoding of decayed type");
4897 return QualType();
4898 }
4899 QualType OriginalType = readType(*Loc.F, Record, Idx);
4900 QualType DT = Context.getAdjustedParameterType(OriginalType);
4901 if (!isa<DecayedType>(DT))
4902 Error("Decayed type does not decay");
4903 return DT;
4904 }
4905
Reid Kleckner0503a872013-12-05 01:23:43 +00004906 case TYPE_ADJUSTED: {
4907 if (Record.size() != 2) {
4908 Error("Incorrect encoding of adjusted type");
4909 return QualType();
4910 }
4911 QualType OriginalTy = readType(*Loc.F, Record, Idx);
4912 QualType AdjustedTy = readType(*Loc.F, Record, Idx);
4913 return Context.getAdjustedType(OriginalTy, AdjustedTy);
4914 }
4915
Guy Benyei11169dd2012-12-18 14:30:41 +00004916 case TYPE_BLOCK_POINTER: {
4917 if (Record.size() != 1) {
4918 Error("Incorrect encoding of block pointer type");
4919 return QualType();
4920 }
4921 QualType PointeeType = readType(*Loc.F, Record, Idx);
4922 return Context.getBlockPointerType(PointeeType);
4923 }
4924
4925 case TYPE_LVALUE_REFERENCE: {
4926 if (Record.size() != 2) {
4927 Error("Incorrect encoding of lvalue reference type");
4928 return QualType();
4929 }
4930 QualType PointeeType = readType(*Loc.F, Record, Idx);
4931 return Context.getLValueReferenceType(PointeeType, Record[1]);
4932 }
4933
4934 case TYPE_RVALUE_REFERENCE: {
4935 if (Record.size() != 1) {
4936 Error("Incorrect encoding of rvalue reference type");
4937 return QualType();
4938 }
4939 QualType PointeeType = readType(*Loc.F, Record, Idx);
4940 return Context.getRValueReferenceType(PointeeType);
4941 }
4942
4943 case TYPE_MEMBER_POINTER: {
4944 if (Record.size() != 2) {
4945 Error("Incorrect encoding of member pointer type");
4946 return QualType();
4947 }
4948 QualType PointeeType = readType(*Loc.F, Record, Idx);
4949 QualType ClassType = readType(*Loc.F, Record, Idx);
4950 if (PointeeType.isNull() || ClassType.isNull())
4951 return QualType();
4952
4953 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
4954 }
4955
4956 case TYPE_CONSTANT_ARRAY: {
4957 QualType ElementType = readType(*Loc.F, Record, Idx);
4958 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4959 unsigned IndexTypeQuals = Record[2];
4960 unsigned Idx = 3;
4961 llvm::APInt Size = ReadAPInt(Record, Idx);
4962 return Context.getConstantArrayType(ElementType, Size,
4963 ASM, IndexTypeQuals);
4964 }
4965
4966 case TYPE_INCOMPLETE_ARRAY: {
4967 QualType ElementType = readType(*Loc.F, Record, Idx);
4968 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4969 unsigned IndexTypeQuals = Record[2];
4970 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
4971 }
4972
4973 case TYPE_VARIABLE_ARRAY: {
4974 QualType ElementType = readType(*Loc.F, Record, Idx);
4975 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4976 unsigned IndexTypeQuals = Record[2];
4977 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
4978 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
4979 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
4980 ASM, IndexTypeQuals,
4981 SourceRange(LBLoc, RBLoc));
4982 }
4983
4984 case TYPE_VECTOR: {
4985 if (Record.size() != 3) {
4986 Error("incorrect encoding of vector type in AST file");
4987 return QualType();
4988 }
4989
4990 QualType ElementType = readType(*Loc.F, Record, Idx);
4991 unsigned NumElements = Record[1];
4992 unsigned VecKind = Record[2];
4993 return Context.getVectorType(ElementType, NumElements,
4994 (VectorType::VectorKind)VecKind);
4995 }
4996
4997 case TYPE_EXT_VECTOR: {
4998 if (Record.size() != 3) {
4999 Error("incorrect encoding of extended vector type in AST file");
5000 return QualType();
5001 }
5002
5003 QualType ElementType = readType(*Loc.F, Record, Idx);
5004 unsigned NumElements = Record[1];
5005 return Context.getExtVectorType(ElementType, NumElements);
5006 }
5007
5008 case TYPE_FUNCTION_NO_PROTO: {
5009 if (Record.size() != 6) {
5010 Error("incorrect encoding of no-proto function type");
5011 return QualType();
5012 }
5013 QualType ResultType = readType(*Loc.F, Record, Idx);
5014 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
5015 (CallingConv)Record[4], Record[5]);
5016 return Context.getFunctionNoProtoType(ResultType, Info);
5017 }
5018
5019 case TYPE_FUNCTION_PROTO: {
5020 QualType ResultType = readType(*Loc.F, Record, Idx);
5021
5022 FunctionProtoType::ExtProtoInfo EPI;
5023 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
5024 /*hasregparm*/ Record[2],
5025 /*regparm*/ Record[3],
5026 static_cast<CallingConv>(Record[4]),
5027 /*produces*/ Record[5]);
5028
5029 unsigned Idx = 6;
5030 unsigned NumParams = Record[Idx++];
5031 SmallVector<QualType, 16> ParamTypes;
5032 for (unsigned I = 0; I != NumParams; ++I)
5033 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
5034
5035 EPI.Variadic = Record[Idx++];
5036 EPI.HasTrailingReturn = Record[Idx++];
5037 EPI.TypeQuals = Record[Idx++];
5038 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
Richard Smith564417a2014-03-20 21:47:22 +00005039 SmallVector<QualType, 8> ExceptionStorage;
5040 readExceptionSpec(*Loc.F, ExceptionStorage, EPI, Record, Idx);
Jordan Rose5c382722013-03-08 21:51:21 +00005041 return Context.getFunctionType(ResultType, ParamTypes, EPI);
Guy Benyei11169dd2012-12-18 14:30:41 +00005042 }
5043
5044 case TYPE_UNRESOLVED_USING: {
5045 unsigned Idx = 0;
5046 return Context.getTypeDeclType(
5047 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
5048 }
5049
5050 case TYPE_TYPEDEF: {
5051 if (Record.size() != 2) {
5052 Error("incorrect encoding of typedef type");
5053 return QualType();
5054 }
5055 unsigned Idx = 0;
5056 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
5057 QualType Canonical = readType(*Loc.F, Record, Idx);
5058 if (!Canonical.isNull())
5059 Canonical = Context.getCanonicalType(Canonical);
5060 return Context.getTypedefType(Decl, Canonical);
5061 }
5062
5063 case TYPE_TYPEOF_EXPR:
5064 return Context.getTypeOfExprType(ReadExpr(*Loc.F));
5065
5066 case TYPE_TYPEOF: {
5067 if (Record.size() != 1) {
5068 Error("incorrect encoding of typeof(type) in AST file");
5069 return QualType();
5070 }
5071 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
5072 return Context.getTypeOfType(UnderlyingType);
5073 }
5074
5075 case TYPE_DECLTYPE: {
5076 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
5077 return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
5078 }
5079
5080 case TYPE_UNARY_TRANSFORM: {
5081 QualType BaseType = readType(*Loc.F, Record, Idx);
5082 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
5083 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
5084 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
5085 }
5086
Richard Smith74aeef52013-04-26 16:15:35 +00005087 case TYPE_AUTO: {
5088 QualType Deduced = readType(*Loc.F, Record, Idx);
5089 bool IsDecltypeAuto = Record[Idx++];
Richard Smith27d807c2013-04-30 13:56:41 +00005090 bool IsDependent = Deduced.isNull() ? Record[Idx++] : false;
Manuel Klimek2fdbea22013-08-22 12:12:24 +00005091 return Context.getAutoType(Deduced, IsDecltypeAuto, IsDependent);
Richard Smith74aeef52013-04-26 16:15:35 +00005092 }
Guy Benyei11169dd2012-12-18 14:30:41 +00005093
5094 case TYPE_RECORD: {
5095 if (Record.size() != 2) {
5096 Error("incorrect encoding of record type");
5097 return QualType();
5098 }
5099 unsigned Idx = 0;
5100 bool IsDependent = Record[Idx++];
5101 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
5102 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
5103 QualType T = Context.getRecordType(RD);
5104 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
5105 return T;
5106 }
5107
5108 case TYPE_ENUM: {
5109 if (Record.size() != 2) {
5110 Error("incorrect encoding of enum type");
5111 return QualType();
5112 }
5113 unsigned Idx = 0;
5114 bool IsDependent = Record[Idx++];
5115 QualType T
5116 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
5117 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
5118 return T;
5119 }
5120
5121 case TYPE_ATTRIBUTED: {
5122 if (Record.size() != 3) {
5123 Error("incorrect encoding of attributed type");
5124 return QualType();
5125 }
5126 QualType modifiedType = readType(*Loc.F, Record, Idx);
5127 QualType equivalentType = readType(*Loc.F, Record, Idx);
5128 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
5129 return Context.getAttributedType(kind, modifiedType, equivalentType);
5130 }
5131
5132 case TYPE_PAREN: {
5133 if (Record.size() != 1) {
5134 Error("incorrect encoding of paren type");
5135 return QualType();
5136 }
5137 QualType InnerType = readType(*Loc.F, Record, Idx);
5138 return Context.getParenType(InnerType);
5139 }
5140
5141 case TYPE_PACK_EXPANSION: {
5142 if (Record.size() != 2) {
5143 Error("incorrect encoding of pack expansion type");
5144 return QualType();
5145 }
5146 QualType Pattern = readType(*Loc.F, Record, Idx);
5147 if (Pattern.isNull())
5148 return QualType();
David Blaikie05785d12013-02-20 22:23:23 +00005149 Optional<unsigned> NumExpansions;
Guy Benyei11169dd2012-12-18 14:30:41 +00005150 if (Record[1])
5151 NumExpansions = Record[1] - 1;
5152 return Context.getPackExpansionType(Pattern, NumExpansions);
5153 }
5154
5155 case TYPE_ELABORATED: {
5156 unsigned Idx = 0;
5157 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
5158 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
5159 QualType NamedType = readType(*Loc.F, Record, Idx);
5160 return Context.getElaboratedType(Keyword, NNS, NamedType);
5161 }
5162
5163 case TYPE_OBJC_INTERFACE: {
5164 unsigned Idx = 0;
5165 ObjCInterfaceDecl *ItfD
5166 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
5167 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
5168 }
5169
5170 case TYPE_OBJC_OBJECT: {
5171 unsigned Idx = 0;
5172 QualType Base = readType(*Loc.F, Record, Idx);
5173 unsigned NumProtos = Record[Idx++];
5174 SmallVector<ObjCProtocolDecl*, 4> Protos;
5175 for (unsigned I = 0; I != NumProtos; ++I)
5176 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
5177 return Context.getObjCObjectType(Base, Protos.data(), NumProtos);
5178 }
5179
5180 case TYPE_OBJC_OBJECT_POINTER: {
5181 unsigned Idx = 0;
5182 QualType Pointee = readType(*Loc.F, Record, Idx);
5183 return Context.getObjCObjectPointerType(Pointee);
5184 }
5185
5186 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
5187 unsigned Idx = 0;
5188 QualType Parm = readType(*Loc.F, Record, Idx);
5189 QualType Replacement = readType(*Loc.F, Record, Idx);
Stephan Tolksdorfe96f8b32014-03-15 10:23:27 +00005190 return Context.getSubstTemplateTypeParmType(
5191 cast<TemplateTypeParmType>(Parm),
5192 Context.getCanonicalType(Replacement));
Guy Benyei11169dd2012-12-18 14:30:41 +00005193 }
5194
5195 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
5196 unsigned Idx = 0;
5197 QualType Parm = readType(*Loc.F, Record, Idx);
5198 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
5199 return Context.getSubstTemplateTypeParmPackType(
5200 cast<TemplateTypeParmType>(Parm),
5201 ArgPack);
5202 }
5203
5204 case TYPE_INJECTED_CLASS_NAME: {
5205 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
5206 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
5207 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
5208 // for AST reading, too much interdependencies.
5209 return
5210 QualType(new (Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
5211 }
5212
5213 case TYPE_TEMPLATE_TYPE_PARM: {
5214 unsigned Idx = 0;
5215 unsigned Depth = Record[Idx++];
5216 unsigned Index = Record[Idx++];
5217 bool Pack = Record[Idx++];
5218 TemplateTypeParmDecl *D
5219 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
5220 return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
5221 }
5222
5223 case TYPE_DEPENDENT_NAME: {
5224 unsigned Idx = 0;
5225 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
5226 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
5227 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
5228 QualType Canon = readType(*Loc.F, Record, Idx);
5229 if (!Canon.isNull())
5230 Canon = Context.getCanonicalType(Canon);
5231 return Context.getDependentNameType(Keyword, NNS, Name, Canon);
5232 }
5233
5234 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
5235 unsigned Idx = 0;
5236 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
5237 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
5238 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
5239 unsigned NumArgs = Record[Idx++];
5240 SmallVector<TemplateArgument, 8> Args;
5241 Args.reserve(NumArgs);
5242 while (NumArgs--)
5243 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
5244 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
5245 Args.size(), Args.data());
5246 }
5247
5248 case TYPE_DEPENDENT_SIZED_ARRAY: {
5249 unsigned Idx = 0;
5250
5251 // ArrayType
5252 QualType ElementType = readType(*Loc.F, Record, Idx);
5253 ArrayType::ArraySizeModifier ASM
5254 = (ArrayType::ArraySizeModifier)Record[Idx++];
5255 unsigned IndexTypeQuals = Record[Idx++];
5256
5257 // DependentSizedArrayType
5258 Expr *NumElts = ReadExpr(*Loc.F);
5259 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
5260
5261 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
5262 IndexTypeQuals, Brackets);
5263 }
5264
5265 case TYPE_TEMPLATE_SPECIALIZATION: {
5266 unsigned Idx = 0;
5267 bool IsDependent = Record[Idx++];
5268 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
5269 SmallVector<TemplateArgument, 8> Args;
5270 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
5271 QualType Underlying = readType(*Loc.F, Record, Idx);
5272 QualType T;
5273 if (Underlying.isNull())
5274 T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(),
5275 Args.size());
5276 else
5277 T = Context.getTemplateSpecializationType(Name, Args.data(),
5278 Args.size(), Underlying);
5279 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
5280 return T;
5281 }
5282
5283 case TYPE_ATOMIC: {
5284 if (Record.size() != 1) {
5285 Error("Incorrect encoding of atomic type");
5286 return QualType();
5287 }
5288 QualType ValueType = readType(*Loc.F, Record, Idx);
5289 return Context.getAtomicType(ValueType);
5290 }
5291 }
5292 llvm_unreachable("Invalid TypeCode!");
5293}
5294
Richard Smith564417a2014-03-20 21:47:22 +00005295void ASTReader::readExceptionSpec(ModuleFile &ModuleFile,
5296 SmallVectorImpl<QualType> &Exceptions,
5297 FunctionProtoType::ExtProtoInfo &EPI,
5298 const RecordData &Record, unsigned &Idx) {
5299 ExceptionSpecificationType EST =
5300 static_cast<ExceptionSpecificationType>(Record[Idx++]);
5301 EPI.ExceptionSpecType = EST;
5302 if (EST == EST_Dynamic) {
5303 EPI.NumExceptions = Record[Idx++];
5304 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
5305 Exceptions.push_back(readType(ModuleFile, Record, Idx));
5306 EPI.Exceptions = Exceptions.data();
5307 } else if (EST == EST_ComputedNoexcept) {
5308 EPI.NoexceptExpr = ReadExpr(ModuleFile);
5309 } else if (EST == EST_Uninstantiated) {
5310 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
5311 EPI.ExceptionSpecTemplate =
5312 ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
5313 } else if (EST == EST_Unevaluated) {
5314 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx);
5315 }
5316}
5317
Guy Benyei11169dd2012-12-18 14:30:41 +00005318class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
5319 ASTReader &Reader;
5320 ModuleFile &F;
5321 const ASTReader::RecordData &Record;
5322 unsigned &Idx;
5323
5324 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
5325 unsigned &I) {
5326 return Reader.ReadSourceLocation(F, R, I);
5327 }
5328
5329 template<typename T>
5330 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
5331 return Reader.ReadDeclAs<T>(F, Record, Idx);
5332 }
5333
5334public:
5335 TypeLocReader(ASTReader &Reader, ModuleFile &F,
5336 const ASTReader::RecordData &Record, unsigned &Idx)
5337 : Reader(Reader), F(F), Record(Record), Idx(Idx)
5338 { }
5339
5340 // We want compile-time assurance that we've enumerated all of
5341 // these, so unfortunately we have to declare them first, then
5342 // define them out-of-line.
5343#define ABSTRACT_TYPELOC(CLASS, PARENT)
5344#define TYPELOC(CLASS, PARENT) \
5345 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
5346#include "clang/AST/TypeLocNodes.def"
5347
5348 void VisitFunctionTypeLoc(FunctionTypeLoc);
5349 void VisitArrayTypeLoc(ArrayTypeLoc);
5350};
5351
5352void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
5353 // nothing to do
5354}
5355void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
5356 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
5357 if (TL.needsExtraLocalData()) {
5358 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
5359 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
5360 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
5361 TL.setModeAttr(Record[Idx++]);
5362 }
5363}
5364void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
5365 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5366}
5367void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
5368 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5369}
Reid Kleckner8a365022013-06-24 17:51:48 +00005370void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
5371 // nothing to do
5372}
Reid Kleckner0503a872013-12-05 01:23:43 +00005373void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
5374 // nothing to do
5375}
Guy Benyei11169dd2012-12-18 14:30:41 +00005376void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
5377 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
5378}
5379void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
5380 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
5381}
5382void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
5383 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
5384}
5385void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
5386 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5387 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5388}
5389void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
5390 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
5391 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
5392 if (Record[Idx++])
5393 TL.setSizeExpr(Reader.ReadExpr(F));
5394 else
5395 TL.setSizeExpr(0);
5396}
5397void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
5398 VisitArrayTypeLoc(TL);
5399}
5400void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
5401 VisitArrayTypeLoc(TL);
5402}
5403void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
5404 VisitArrayTypeLoc(TL);
5405}
5406void TypeLocReader::VisitDependentSizedArrayTypeLoc(
5407 DependentSizedArrayTypeLoc TL) {
5408 VisitArrayTypeLoc(TL);
5409}
5410void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
5411 DependentSizedExtVectorTypeLoc TL) {
5412 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5413}
5414void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
5415 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5416}
5417void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
5418 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5419}
5420void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
5421 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
5422 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5423 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5424 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
Alp Tokerb3fd5cf2014-01-21 00:32:38 +00005425 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
5426 TL.setParam(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00005427 }
5428}
5429void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
5430 VisitFunctionTypeLoc(TL);
5431}
5432void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
5433 VisitFunctionTypeLoc(TL);
5434}
5435void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
5436 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5437}
5438void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
5439 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5440}
5441void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
5442 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
5443 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5444 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5445}
5446void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
5447 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
5448 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5449 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5450 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5451}
5452void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
5453 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5454}
5455void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
5456 TL.setKWLoc(ReadSourceLocation(Record, Idx));
5457 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5458 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5459 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5460}
5461void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
5462 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5463}
5464void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
5465 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5466}
5467void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
5468 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5469}
5470void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
5471 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
5472 if (TL.hasAttrOperand()) {
5473 SourceRange range;
5474 range.setBegin(ReadSourceLocation(Record, Idx));
5475 range.setEnd(ReadSourceLocation(Record, Idx));
5476 TL.setAttrOperandParensRange(range);
5477 }
5478 if (TL.hasAttrExprOperand()) {
5479 if (Record[Idx++])
5480 TL.setAttrExprOperand(Reader.ReadExpr(F));
5481 else
5482 TL.setAttrExprOperand(0);
5483 } else if (TL.hasAttrEnumOperand())
5484 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
5485}
5486void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
5487 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5488}
5489void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
5490 SubstTemplateTypeParmTypeLoc TL) {
5491 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5492}
5493void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
5494 SubstTemplateTypeParmPackTypeLoc TL) {
5495 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5496}
5497void TypeLocReader::VisitTemplateSpecializationTypeLoc(
5498 TemplateSpecializationTypeLoc TL) {
5499 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
5500 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
5501 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5502 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5503 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
5504 TL.setArgLocInfo(i,
5505 Reader.GetTemplateArgumentLocInfo(F,
5506 TL.getTypePtr()->getArg(i).getKind(),
5507 Record, Idx));
5508}
5509void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
5510 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5511 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5512}
5513void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
5514 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5515 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5516}
5517void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
5518 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5519}
5520void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
5521 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5522 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5523 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5524}
5525void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
5526 DependentTemplateSpecializationTypeLoc TL) {
5527 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5528 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5529 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
5530 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
5531 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5532 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5533 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
5534 TL.setArgLocInfo(I,
5535 Reader.GetTemplateArgumentLocInfo(F,
5536 TL.getTypePtr()->getArg(I).getKind(),
5537 Record, Idx));
5538}
5539void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
5540 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
5541}
5542void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
5543 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5544}
5545void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
5546 TL.setHasBaseTypeAsWritten(Record[Idx++]);
5547 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5548 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5549 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
5550 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
5551}
5552void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
5553 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5554}
5555void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
5556 TL.setKWLoc(ReadSourceLocation(Record, Idx));
5557 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5558 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5559}
5560
5561TypeSourceInfo *ASTReader::GetTypeSourceInfo(ModuleFile &F,
5562 const RecordData &Record,
5563 unsigned &Idx) {
5564 QualType InfoTy = readType(F, Record, Idx);
5565 if (InfoTy.isNull())
5566 return 0;
5567
5568 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
5569 TypeLocReader TLR(*this, F, Record, Idx);
5570 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
5571 TLR.Visit(TL);
5572 return TInfo;
5573}
5574
5575QualType ASTReader::GetType(TypeID ID) {
5576 unsigned FastQuals = ID & Qualifiers::FastMask;
5577 unsigned Index = ID >> Qualifiers::FastWidth;
5578
5579 if (Index < NUM_PREDEF_TYPE_IDS) {
5580 QualType T;
5581 switch ((PredefinedTypeIDs)Index) {
5582 case PREDEF_TYPE_NULL_ID: return QualType();
5583 case PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
5584 case PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
5585
5586 case PREDEF_TYPE_CHAR_U_ID:
5587 case PREDEF_TYPE_CHAR_S_ID:
5588 // FIXME: Check that the signedness of CharTy is correct!
5589 T = Context.CharTy;
5590 break;
5591
5592 case PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
5593 case PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
5594 case PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
5595 case PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
5596 case PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
5597 case PREDEF_TYPE_UINT128_ID: T = Context.UnsignedInt128Ty; break;
5598 case PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
5599 case PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
5600 case PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
5601 case PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
5602 case PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
5603 case PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
5604 case PREDEF_TYPE_INT128_ID: T = Context.Int128Ty; break;
5605 case PREDEF_TYPE_HALF_ID: T = Context.HalfTy; break;
5606 case PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
5607 case PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
5608 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
5609 case PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
5610 case PREDEF_TYPE_BOUND_MEMBER: T = Context.BoundMemberTy; break;
5611 case PREDEF_TYPE_PSEUDO_OBJECT: T = Context.PseudoObjectTy; break;
5612 case PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
5613 case PREDEF_TYPE_UNKNOWN_ANY: T = Context.UnknownAnyTy; break;
5614 case PREDEF_TYPE_NULLPTR_ID: T = Context.NullPtrTy; break;
5615 case PREDEF_TYPE_CHAR16_ID: T = Context.Char16Ty; break;
5616 case PREDEF_TYPE_CHAR32_ID: T = Context.Char32Ty; break;
5617 case PREDEF_TYPE_OBJC_ID: T = Context.ObjCBuiltinIdTy; break;
5618 case PREDEF_TYPE_OBJC_CLASS: T = Context.ObjCBuiltinClassTy; break;
5619 case PREDEF_TYPE_OBJC_SEL: T = Context.ObjCBuiltinSelTy; break;
Guy Benyeid8a08ea2012-12-18 14:38:23 +00005620 case PREDEF_TYPE_IMAGE1D_ID: T = Context.OCLImage1dTy; break;
5621 case PREDEF_TYPE_IMAGE1D_ARR_ID: T = Context.OCLImage1dArrayTy; break;
5622 case PREDEF_TYPE_IMAGE1D_BUFF_ID: T = Context.OCLImage1dBufferTy; break;
5623 case PREDEF_TYPE_IMAGE2D_ID: T = Context.OCLImage2dTy; break;
5624 case PREDEF_TYPE_IMAGE2D_ARR_ID: T = Context.OCLImage2dArrayTy; break;
5625 case PREDEF_TYPE_IMAGE3D_ID: T = Context.OCLImage3dTy; break;
Guy Benyei61054192013-02-07 10:55:47 +00005626 case PREDEF_TYPE_SAMPLER_ID: T = Context.OCLSamplerTy; break;
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00005627 case PREDEF_TYPE_EVENT_ID: T = Context.OCLEventTy; break;
Guy Benyei11169dd2012-12-18 14:30:41 +00005628 case PREDEF_TYPE_AUTO_DEDUCT: T = Context.getAutoDeductType(); break;
5629
5630 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
5631 T = Context.getAutoRRefDeductType();
5632 break;
5633
5634 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
5635 T = Context.ARCUnbridgedCastTy;
5636 break;
5637
5638 case PREDEF_TYPE_VA_LIST_TAG:
5639 T = Context.getVaListTagType();
5640 break;
5641
5642 case PREDEF_TYPE_BUILTIN_FN:
5643 T = Context.BuiltinFnTy;
5644 break;
5645 }
5646
5647 assert(!T.isNull() && "Unknown predefined type");
5648 return T.withFastQualifiers(FastQuals);
5649 }
5650
5651 Index -= NUM_PREDEF_TYPE_IDS;
5652 assert(Index < TypesLoaded.size() && "Type index out-of-range");
5653 if (TypesLoaded[Index].isNull()) {
5654 TypesLoaded[Index] = readTypeRecord(Index);
5655 if (TypesLoaded[Index].isNull())
5656 return QualType();
5657
5658 TypesLoaded[Index]->setFromAST();
5659 if (DeserializationListener)
5660 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
5661 TypesLoaded[Index]);
5662 }
5663
5664 return TypesLoaded[Index].withFastQualifiers(FastQuals);
5665}
5666
5667QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
5668 return GetType(getGlobalTypeID(F, LocalID));
5669}
5670
5671serialization::TypeID
5672ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
5673 unsigned FastQuals = LocalID & Qualifiers::FastMask;
5674 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
5675
5676 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
5677 return LocalID;
5678
5679 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5680 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
5681 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
5682
5683 unsigned GlobalIndex = LocalIndex + I->second;
5684 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
5685}
5686
5687TemplateArgumentLocInfo
5688ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
5689 TemplateArgument::ArgKind Kind,
5690 const RecordData &Record,
5691 unsigned &Index) {
5692 switch (Kind) {
5693 case TemplateArgument::Expression:
5694 return ReadExpr(F);
5695 case TemplateArgument::Type:
5696 return GetTypeSourceInfo(F, Record, Index);
5697 case TemplateArgument::Template: {
5698 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
5699 Index);
5700 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
5701 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
5702 SourceLocation());
5703 }
5704 case TemplateArgument::TemplateExpansion: {
5705 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
5706 Index);
5707 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
5708 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
5709 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
5710 EllipsisLoc);
5711 }
5712 case TemplateArgument::Null:
5713 case TemplateArgument::Integral:
5714 case TemplateArgument::Declaration:
5715 case TemplateArgument::NullPtr:
5716 case TemplateArgument::Pack:
5717 // FIXME: Is this right?
5718 return TemplateArgumentLocInfo();
5719 }
5720 llvm_unreachable("unexpected template argument loc");
5721}
5722
5723TemplateArgumentLoc
5724ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
5725 const RecordData &Record, unsigned &Index) {
5726 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
5727
5728 if (Arg.getKind() == TemplateArgument::Expression) {
5729 if (Record[Index++]) // bool InfoHasSameExpr.
5730 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
5731 }
5732 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
5733 Record, Index));
5734}
5735
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005736const ASTTemplateArgumentListInfo*
5737ASTReader::ReadASTTemplateArgumentListInfo(ModuleFile &F,
5738 const RecordData &Record,
5739 unsigned &Index) {
5740 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Index);
5741 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Index);
5742 unsigned NumArgsAsWritten = Record[Index++];
5743 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
5744 for (unsigned i = 0; i != NumArgsAsWritten; ++i)
5745 TemplArgsInfo.addArgument(ReadTemplateArgumentLoc(F, Record, Index));
5746 return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo);
5747}
5748
Guy Benyei11169dd2012-12-18 14:30:41 +00005749Decl *ASTReader::GetExternalDecl(uint32_t ID) {
5750 return GetDecl(ID);
5751}
5752
5753uint64_t ASTReader::readCXXBaseSpecifiers(ModuleFile &M, const RecordData &Record,
5754 unsigned &Idx){
5755 if (Idx >= Record.size())
5756 return 0;
5757
5758 unsigned LocalID = Record[Idx++];
5759 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
5760}
5761
5762CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
5763 RecordLocation Loc = getLocalBitOffset(Offset);
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005764 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00005765 SavedStreamPosition SavedPosition(Cursor);
5766 Cursor.JumpToBit(Loc.Offset);
5767 ReadingKindTracker ReadingKind(Read_Decl, *this);
5768 RecordData Record;
5769 unsigned Code = Cursor.ReadCode();
Chris Lattner0e6c9402013-01-20 02:38:54 +00005770 unsigned RecCode = Cursor.readRecord(Code, Record);
Guy Benyei11169dd2012-12-18 14:30:41 +00005771 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
5772 Error("Malformed AST file: missing C++ base specifiers");
5773 return 0;
5774 }
5775
5776 unsigned Idx = 0;
5777 unsigned NumBases = Record[Idx++];
5778 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
5779 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
5780 for (unsigned I = 0; I != NumBases; ++I)
5781 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
5782 return Bases;
5783}
5784
5785serialization::DeclID
5786ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
5787 if (LocalID < NUM_PREDEF_DECL_IDS)
5788 return LocalID;
5789
5790 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5791 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
5792 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
5793
5794 return LocalID + I->second;
5795}
5796
5797bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
5798 ModuleFile &M) const {
5799 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(ID);
5800 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5801 return &M == I->second;
5802}
5803
Douglas Gregor9f782892013-01-21 15:25:38 +00005804ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
Guy Benyei11169dd2012-12-18 14:30:41 +00005805 if (!D->isFromASTFile())
5806 return 0;
5807 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
5808 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5809 return I->second;
5810}
5811
5812SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
5813 if (ID < NUM_PREDEF_DECL_IDS)
5814 return SourceLocation();
5815
5816 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
5817
5818 if (Index > DeclsLoaded.size()) {
5819 Error("declaration ID out-of-range for AST file");
5820 return SourceLocation();
5821 }
5822
5823 if (Decl *D = DeclsLoaded[Index])
5824 return D->getLocation();
5825
5826 unsigned RawLocation = 0;
5827 RecordLocation Rec = DeclCursorForID(ID, RawLocation);
5828 return ReadSourceLocation(*Rec.F, RawLocation);
5829}
5830
5831Decl *ASTReader::GetDecl(DeclID ID) {
5832 if (ID < NUM_PREDEF_DECL_IDS) {
5833 switch ((PredefinedDeclIDs)ID) {
5834 case PREDEF_DECL_NULL_ID:
5835 return 0;
5836
5837 case PREDEF_DECL_TRANSLATION_UNIT_ID:
5838 return Context.getTranslationUnitDecl();
5839
5840 case PREDEF_DECL_OBJC_ID_ID:
5841 return Context.getObjCIdDecl();
5842
5843 case PREDEF_DECL_OBJC_SEL_ID:
5844 return Context.getObjCSelDecl();
5845
5846 case PREDEF_DECL_OBJC_CLASS_ID:
5847 return Context.getObjCClassDecl();
5848
5849 case PREDEF_DECL_OBJC_PROTOCOL_ID:
5850 return Context.getObjCProtocolDecl();
5851
5852 case PREDEF_DECL_INT_128_ID:
5853 return Context.getInt128Decl();
5854
5855 case PREDEF_DECL_UNSIGNED_INT_128_ID:
5856 return Context.getUInt128Decl();
5857
5858 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
5859 return Context.getObjCInstanceTypeDecl();
5860
5861 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
5862 return Context.getBuiltinVaListDecl();
5863 }
5864 }
5865
5866 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
5867
5868 if (Index >= DeclsLoaded.size()) {
5869 assert(0 && "declaration ID out-of-range for AST file");
5870 Error("declaration ID out-of-range for AST file");
5871 return 0;
5872 }
5873
5874 if (!DeclsLoaded[Index]) {
5875 ReadDeclRecord(ID);
5876 if (DeserializationListener)
5877 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
5878 }
5879
5880 return DeclsLoaded[Index];
5881}
5882
5883DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
5884 DeclID GlobalID) {
5885 if (GlobalID < NUM_PREDEF_DECL_IDS)
5886 return GlobalID;
5887
5888 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
5889 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5890 ModuleFile *Owner = I->second;
5891
5892 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
5893 = M.GlobalToLocalDeclIDs.find(Owner);
5894 if (Pos == M.GlobalToLocalDeclIDs.end())
5895 return 0;
5896
5897 return GlobalID - Owner->BaseDeclID + Pos->second;
5898}
5899
5900serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
5901 const RecordData &Record,
5902 unsigned &Idx) {
5903 if (Idx >= Record.size()) {
5904 Error("Corrupted AST file");
5905 return 0;
5906 }
5907
5908 return getGlobalDeclID(F, Record[Idx++]);
5909}
5910
5911/// \brief Resolve the offset of a statement into a statement.
5912///
5913/// This operation will read a new statement from the external
5914/// source each time it is called, and is meant to be used via a
5915/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
5916Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
5917 // Switch case IDs are per Decl.
5918 ClearSwitchCaseIDs();
5919
5920 // Offset here is a global offset across the entire chain.
5921 RecordLocation Loc = getLocalBitOffset(Offset);
5922 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
5923 return ReadStmtFromStream(*Loc.F);
5924}
5925
5926namespace {
5927 class FindExternalLexicalDeclsVisitor {
5928 ASTReader &Reader;
5929 const DeclContext *DC;
5930 bool (*isKindWeWant)(Decl::Kind);
5931
5932 SmallVectorImpl<Decl*> &Decls;
5933 bool PredefsVisited[NUM_PREDEF_DECL_IDS];
5934
5935 public:
5936 FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC,
5937 bool (*isKindWeWant)(Decl::Kind),
5938 SmallVectorImpl<Decl*> &Decls)
5939 : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls)
5940 {
5941 for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I)
5942 PredefsVisited[I] = false;
5943 }
5944
5945 static bool visit(ModuleFile &M, bool Preorder, void *UserData) {
5946 if (Preorder)
5947 return false;
5948
5949 FindExternalLexicalDeclsVisitor *This
5950 = static_cast<FindExternalLexicalDeclsVisitor *>(UserData);
5951
5952 ModuleFile::DeclContextInfosMap::iterator Info
5953 = M.DeclContextInfos.find(This->DC);
5954 if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls)
5955 return false;
5956
5957 // Load all of the declaration IDs
5958 for (const KindDeclIDPair *ID = Info->second.LexicalDecls,
5959 *IDE = ID + Info->second.NumLexicalDecls;
5960 ID != IDE; ++ID) {
5961 if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first))
5962 continue;
5963
5964 // Don't add predefined declarations to the lexical context more
5965 // than once.
5966 if (ID->second < NUM_PREDEF_DECL_IDS) {
5967 if (This->PredefsVisited[ID->second])
5968 continue;
5969
5970 This->PredefsVisited[ID->second] = true;
5971 }
5972
5973 if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) {
5974 if (!This->DC->isDeclInLexicalTraversal(D))
5975 This->Decls.push_back(D);
5976 }
5977 }
5978
5979 return false;
5980 }
5981 };
5982}
5983
5984ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
5985 bool (*isKindWeWant)(Decl::Kind),
5986 SmallVectorImpl<Decl*> &Decls) {
5987 // There might be lexical decls in multiple modules, for the TU at
5988 // least. Walk all of the modules in the order they were loaded.
5989 FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls);
5990 ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor);
5991 ++NumLexicalDeclContextsRead;
5992 return ELR_Success;
5993}
5994
5995namespace {
5996
5997class DeclIDComp {
5998 ASTReader &Reader;
5999 ModuleFile &Mod;
6000
6001public:
6002 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
6003
6004 bool operator()(LocalDeclID L, LocalDeclID R) const {
6005 SourceLocation LHS = getLocation(L);
6006 SourceLocation RHS = getLocation(R);
6007 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6008 }
6009
6010 bool operator()(SourceLocation LHS, LocalDeclID R) const {
6011 SourceLocation RHS = getLocation(R);
6012 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6013 }
6014
6015 bool operator()(LocalDeclID L, SourceLocation RHS) const {
6016 SourceLocation LHS = getLocation(L);
6017 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6018 }
6019
6020 SourceLocation getLocation(LocalDeclID ID) const {
6021 return Reader.getSourceManager().getFileLoc(
6022 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
6023 }
6024};
6025
6026}
6027
6028void ASTReader::FindFileRegionDecls(FileID File,
6029 unsigned Offset, unsigned Length,
6030 SmallVectorImpl<Decl *> &Decls) {
6031 SourceManager &SM = getSourceManager();
6032
6033 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
6034 if (I == FileDeclIDs.end())
6035 return;
6036
6037 FileDeclsInfo &DInfo = I->second;
6038 if (DInfo.Decls.empty())
6039 return;
6040
6041 SourceLocation
6042 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
6043 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
6044
6045 DeclIDComp DIDComp(*this, *DInfo.Mod);
6046 ArrayRef<serialization::LocalDeclID>::iterator
6047 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
6048 BeginLoc, DIDComp);
6049 if (BeginIt != DInfo.Decls.begin())
6050 --BeginIt;
6051
6052 // If we are pointing at a top-level decl inside an objc container, we need
6053 // to backtrack until we find it otherwise we will fail to report that the
6054 // region overlaps with an objc container.
6055 while (BeginIt != DInfo.Decls.begin() &&
6056 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
6057 ->isTopLevelDeclInObjCContainer())
6058 --BeginIt;
6059
6060 ArrayRef<serialization::LocalDeclID>::iterator
6061 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
6062 EndLoc, DIDComp);
6063 if (EndIt != DInfo.Decls.end())
6064 ++EndIt;
6065
6066 for (ArrayRef<serialization::LocalDeclID>::iterator
6067 DIt = BeginIt; DIt != EndIt; ++DIt)
6068 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
6069}
6070
6071namespace {
6072 /// \brief ModuleFile visitor used to perform name lookup into a
6073 /// declaration context.
6074 class DeclContextNameLookupVisitor {
6075 ASTReader &Reader;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00006076 SmallVectorImpl<const DeclContext *> &Contexts;
Guy Benyei11169dd2012-12-18 14:30:41 +00006077 DeclarationName Name;
6078 SmallVectorImpl<NamedDecl *> &Decls;
6079
6080 public:
6081 DeclContextNameLookupVisitor(ASTReader &Reader,
6082 SmallVectorImpl<const DeclContext *> &Contexts,
6083 DeclarationName Name,
6084 SmallVectorImpl<NamedDecl *> &Decls)
6085 : Reader(Reader), Contexts(Contexts), Name(Name), Decls(Decls) { }
6086
6087 static bool visit(ModuleFile &M, void *UserData) {
6088 DeclContextNameLookupVisitor *This
6089 = static_cast<DeclContextNameLookupVisitor *>(UserData);
6090
6091 // Check whether we have any visible declaration information for
6092 // this context in this module.
6093 ModuleFile::DeclContextInfosMap::iterator Info;
6094 bool FoundInfo = false;
6095 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
6096 Info = M.DeclContextInfos.find(This->Contexts[I]);
6097 if (Info != M.DeclContextInfos.end() &&
6098 Info->second.NameLookupTableData) {
6099 FoundInfo = true;
6100 break;
6101 }
6102 }
6103
6104 if (!FoundInfo)
6105 return false;
6106
6107 // Look for this name within this module.
Richard Smith52e3fba2014-03-11 07:17:35 +00006108 ASTDeclContextNameLookupTable *LookupTable =
Guy Benyei11169dd2012-12-18 14:30:41 +00006109 Info->second.NameLookupTableData;
6110 ASTDeclContextNameLookupTable::iterator Pos
6111 = LookupTable->find(This->Name);
6112 if (Pos == LookupTable->end())
6113 return false;
6114
6115 bool FoundAnything = false;
6116 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
6117 for (; Data.first != Data.second; ++Data.first) {
6118 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first);
6119 if (!ND)
6120 continue;
6121
6122 if (ND->getDeclName() != This->Name) {
6123 // A name might be null because the decl's redeclarable part is
6124 // currently read before reading its name. The lookup is triggered by
6125 // building that decl (likely indirectly), and so it is later in the
6126 // sense of "already existing" and can be ignored here.
6127 continue;
6128 }
6129
6130 // Record this declaration.
6131 FoundAnything = true;
6132 This->Decls.push_back(ND);
6133 }
6134
6135 return FoundAnything;
6136 }
6137 };
6138}
6139
Douglas Gregor9f782892013-01-21 15:25:38 +00006140/// \brief Retrieve the "definitive" module file for the definition of the
6141/// given declaration context, if there is one.
6142///
6143/// The "definitive" module file is the only place where we need to look to
6144/// find information about the declarations within the given declaration
6145/// context. For example, C++ and Objective-C classes, C structs/unions, and
6146/// Objective-C protocols, categories, and extensions are all defined in a
6147/// single place in the source code, so they have definitive module files
6148/// associated with them. C++ namespaces, on the other hand, can have
6149/// definitions in multiple different module files.
6150///
6151/// Note: this needs to be kept in sync with ASTWriter::AddedVisibleDecl's
6152/// NDEBUG checking.
6153static ModuleFile *getDefinitiveModuleFileFor(const DeclContext *DC,
6154 ASTReader &Reader) {
Douglas Gregor7a6e2002013-01-22 17:08:30 +00006155 if (const DeclContext *DefDC = getDefinitiveDeclContext(DC))
6156 return Reader.getOwningModuleFile(cast<Decl>(DefDC));
Douglas Gregor9f782892013-01-21 15:25:38 +00006157
6158 return 0;
6159}
6160
Richard Smith9ce12e32013-02-07 03:30:24 +00006161bool
Guy Benyei11169dd2012-12-18 14:30:41 +00006162ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
6163 DeclarationName Name) {
6164 assert(DC->hasExternalVisibleStorage() &&
6165 "DeclContext has no visible decls in storage");
6166 if (!Name)
Richard Smith9ce12e32013-02-07 03:30:24 +00006167 return false;
Guy Benyei11169dd2012-12-18 14:30:41 +00006168
6169 SmallVector<NamedDecl *, 64> Decls;
6170
6171 // Compute the declaration contexts we need to look into. Multiple such
6172 // declaration contexts occur when two declaration contexts from disjoint
6173 // modules get merged, e.g., when two namespaces with the same name are
6174 // independently defined in separate modules.
6175 SmallVector<const DeclContext *, 2> Contexts;
6176 Contexts.push_back(DC);
6177
6178 if (DC->isNamespace()) {
6179 MergedDeclsMap::iterator Merged
6180 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
6181 if (Merged != MergedDecls.end()) {
6182 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
6183 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
6184 }
6185 }
6186
6187 DeclContextNameLookupVisitor Visitor(*this, Contexts, Name, Decls);
Douglas Gregor9f782892013-01-21 15:25:38 +00006188
6189 // If we can definitively determine which module file to look into,
6190 // only look there. Otherwise, look in all module files.
6191 ModuleFile *Definitive;
6192 if (Contexts.size() == 1 &&
6193 (Definitive = getDefinitiveModuleFileFor(DC, *this))) {
6194 DeclContextNameLookupVisitor::visit(*Definitive, &Visitor);
6195 } else {
6196 ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor);
6197 }
Guy Benyei11169dd2012-12-18 14:30:41 +00006198 ++NumVisibleDeclContextsRead;
6199 SetExternalVisibleDeclsForName(DC, Name, Decls);
Richard Smith9ce12e32013-02-07 03:30:24 +00006200 return !Decls.empty();
Guy Benyei11169dd2012-12-18 14:30:41 +00006201}
6202
6203namespace {
6204 /// \brief ModuleFile visitor used to retrieve all visible names in a
6205 /// declaration context.
6206 class DeclContextAllNamesVisitor {
6207 ASTReader &Reader;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00006208 SmallVectorImpl<const DeclContext *> &Contexts;
Craig Topper3598eb72013-07-05 04:43:31 +00006209 DeclsMap &Decls;
Argyrios Kyrtzidis2810e9d2012-12-19 22:21:18 +00006210 bool VisitAll;
Guy Benyei11169dd2012-12-18 14:30:41 +00006211
6212 public:
6213 DeclContextAllNamesVisitor(ASTReader &Reader,
6214 SmallVectorImpl<const DeclContext *> &Contexts,
Craig Topper3598eb72013-07-05 04:43:31 +00006215 DeclsMap &Decls, bool VisitAll)
Argyrios Kyrtzidis2810e9d2012-12-19 22:21:18 +00006216 : Reader(Reader), Contexts(Contexts), Decls(Decls), VisitAll(VisitAll) { }
Guy Benyei11169dd2012-12-18 14:30:41 +00006217
6218 static bool visit(ModuleFile &M, void *UserData) {
6219 DeclContextAllNamesVisitor *This
6220 = static_cast<DeclContextAllNamesVisitor *>(UserData);
6221
6222 // Check whether we have any visible declaration information for
6223 // this context in this module.
6224 ModuleFile::DeclContextInfosMap::iterator Info;
6225 bool FoundInfo = false;
6226 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
6227 Info = M.DeclContextInfos.find(This->Contexts[I]);
6228 if (Info != M.DeclContextInfos.end() &&
6229 Info->second.NameLookupTableData) {
6230 FoundInfo = true;
6231 break;
6232 }
6233 }
6234
6235 if (!FoundInfo)
6236 return false;
6237
Richard Smith52e3fba2014-03-11 07:17:35 +00006238 ASTDeclContextNameLookupTable *LookupTable =
Guy Benyei11169dd2012-12-18 14:30:41 +00006239 Info->second.NameLookupTableData;
6240 bool FoundAnything = false;
6241 for (ASTDeclContextNameLookupTable::data_iterator
Douglas Gregor5e306b12013-01-23 22:38:11 +00006242 I = LookupTable->data_begin(), E = LookupTable->data_end();
6243 I != E;
6244 ++I) {
Guy Benyei11169dd2012-12-18 14:30:41 +00006245 ASTDeclContextNameLookupTrait::data_type Data = *I;
6246 for (; Data.first != Data.second; ++Data.first) {
6247 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M,
6248 *Data.first);
6249 if (!ND)
6250 continue;
6251
6252 // Record this declaration.
6253 FoundAnything = true;
6254 This->Decls[ND->getDeclName()].push_back(ND);
6255 }
6256 }
6257
Argyrios Kyrtzidis2810e9d2012-12-19 22:21:18 +00006258 return FoundAnything && !This->VisitAll;
Guy Benyei11169dd2012-12-18 14:30:41 +00006259 }
6260 };
6261}
6262
6263void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
6264 if (!DC->hasExternalVisibleStorage())
6265 return;
Craig Topper79be4cd2013-07-05 04:33:53 +00006266 DeclsMap Decls;
Guy Benyei11169dd2012-12-18 14:30:41 +00006267
6268 // Compute the declaration contexts we need to look into. Multiple such
6269 // declaration contexts occur when two declaration contexts from disjoint
6270 // modules get merged, e.g., when two namespaces with the same name are
6271 // independently defined in separate modules.
6272 SmallVector<const DeclContext *, 2> Contexts;
6273 Contexts.push_back(DC);
6274
6275 if (DC->isNamespace()) {
6276 MergedDeclsMap::iterator Merged
6277 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
6278 if (Merged != MergedDecls.end()) {
6279 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
6280 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
6281 }
6282 }
6283
Argyrios Kyrtzidis2810e9d2012-12-19 22:21:18 +00006284 DeclContextAllNamesVisitor Visitor(*this, Contexts, Decls,
6285 /*VisitAll=*/DC->isFileContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00006286 ModuleMgr.visit(&DeclContextAllNamesVisitor::visit, &Visitor);
6287 ++NumVisibleDeclContextsRead;
6288
Craig Topper79be4cd2013-07-05 04:33:53 +00006289 for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
Guy Benyei11169dd2012-12-18 14:30:41 +00006290 SetExternalVisibleDeclsForName(DC, I->first, I->second);
6291 }
6292 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
6293}
6294
6295/// \brief Under non-PCH compilation the consumer receives the objc methods
6296/// before receiving the implementation, and codegen depends on this.
6297/// We simulate this by deserializing and passing to consumer the methods of the
6298/// implementation before passing the deserialized implementation decl.
6299static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
6300 ASTConsumer *Consumer) {
6301 assert(ImplD && Consumer);
6302
Aaron Ballmanaff18c02014-03-13 19:03:34 +00006303 for (auto *I : ImplD->methods())
6304 Consumer->HandleInterestingDecl(DeclGroupRef(I));
Guy Benyei11169dd2012-12-18 14:30:41 +00006305
6306 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
6307}
6308
6309void ASTReader::PassInterestingDeclsToConsumer() {
6310 assert(Consumer);
6311 while (!InterestingDecls.empty()) {
6312 Decl *D = InterestingDecls.front();
6313 InterestingDecls.pop_front();
6314
6315 PassInterestingDeclToConsumer(D);
6316 }
6317}
6318
6319void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
6320 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
6321 PassObjCImplDeclToConsumer(ImplD, Consumer);
6322 else
6323 Consumer->HandleInterestingDecl(DeclGroupRef(D));
6324}
6325
6326void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
6327 this->Consumer = Consumer;
6328
6329 if (!Consumer)
6330 return;
6331
Ben Langmuir332aafe2014-01-31 01:06:56 +00006332 for (unsigned I = 0, N = EagerlyDeserializedDecls.size(); I != N; ++I) {
Guy Benyei11169dd2012-12-18 14:30:41 +00006333 // Force deserialization of this decl, which will cause it to be queued for
6334 // passing to the consumer.
Ben Langmuir332aafe2014-01-31 01:06:56 +00006335 GetDecl(EagerlyDeserializedDecls[I]);
Guy Benyei11169dd2012-12-18 14:30:41 +00006336 }
Ben Langmuir332aafe2014-01-31 01:06:56 +00006337 EagerlyDeserializedDecls.clear();
Guy Benyei11169dd2012-12-18 14:30:41 +00006338
6339 PassInterestingDeclsToConsumer();
6340}
6341
6342void ASTReader::PrintStats() {
6343 std::fprintf(stderr, "*** AST File Statistics:\n");
6344
6345 unsigned NumTypesLoaded
6346 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
6347 QualType());
6348 unsigned NumDeclsLoaded
6349 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
6350 (Decl *)0);
6351 unsigned NumIdentifiersLoaded
6352 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
6353 IdentifiersLoaded.end(),
6354 (IdentifierInfo *)0);
6355 unsigned NumMacrosLoaded
6356 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
6357 MacrosLoaded.end(),
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00006358 (MacroInfo *)0);
Guy Benyei11169dd2012-12-18 14:30:41 +00006359 unsigned NumSelectorsLoaded
6360 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
6361 SelectorsLoaded.end(),
6362 Selector());
6363
6364 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
6365 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
6366 NumSLocEntriesRead, TotalNumSLocEntries,
6367 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
6368 if (!TypesLoaded.empty())
6369 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
6370 NumTypesLoaded, (unsigned)TypesLoaded.size(),
6371 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
6372 if (!DeclsLoaded.empty())
6373 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
6374 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
6375 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
6376 if (!IdentifiersLoaded.empty())
6377 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
6378 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
6379 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
6380 if (!MacrosLoaded.empty())
6381 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
6382 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
6383 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
6384 if (!SelectorsLoaded.empty())
6385 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
6386 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
6387 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
6388 if (TotalNumStatements)
6389 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
6390 NumStatementsRead, TotalNumStatements,
6391 ((float)NumStatementsRead/TotalNumStatements * 100));
6392 if (TotalNumMacros)
6393 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
6394 NumMacrosRead, TotalNumMacros,
6395 ((float)NumMacrosRead/TotalNumMacros * 100));
6396 if (TotalLexicalDeclContexts)
6397 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
6398 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
6399 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
6400 * 100));
6401 if (TotalVisibleDeclContexts)
6402 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
6403 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
6404 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
6405 * 100));
6406 if (TotalNumMethodPoolEntries) {
6407 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
6408 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
6409 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
6410 * 100));
Guy Benyei11169dd2012-12-18 14:30:41 +00006411 }
Douglas Gregorad2f7a52013-01-28 17:54:36 +00006412 if (NumMethodPoolLookups) {
6413 std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n",
6414 NumMethodPoolHits, NumMethodPoolLookups,
6415 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
6416 }
6417 if (NumMethodPoolTableLookups) {
6418 std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n",
6419 NumMethodPoolTableHits, NumMethodPoolTableLookups,
6420 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
6421 * 100.0));
6422 }
6423
Douglas Gregor00a50f72013-01-25 00:38:33 +00006424 if (NumIdentifierLookupHits) {
6425 std::fprintf(stderr,
6426 " %u / %u identifier table lookups succeeded (%f%%)\n",
6427 NumIdentifierLookupHits, NumIdentifierLookups,
6428 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
6429 }
6430
Douglas Gregore060e572013-01-25 01:03:03 +00006431 if (GlobalIndex) {
6432 std::fprintf(stderr, "\n");
6433 GlobalIndex->printStats();
6434 }
6435
Guy Benyei11169dd2012-12-18 14:30:41 +00006436 std::fprintf(stderr, "\n");
6437 dump();
6438 std::fprintf(stderr, "\n");
6439}
6440
6441template<typename Key, typename ModuleFile, unsigned InitialCapacity>
6442static void
6443dumpModuleIDMap(StringRef Name,
6444 const ContinuousRangeMap<Key, ModuleFile *,
6445 InitialCapacity> &Map) {
6446 if (Map.begin() == Map.end())
6447 return;
6448
6449 typedef ContinuousRangeMap<Key, ModuleFile *, InitialCapacity> MapType;
6450 llvm::errs() << Name << ":\n";
6451 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
6452 I != IEnd; ++I) {
6453 llvm::errs() << " " << I->first << " -> " << I->second->FileName
6454 << "\n";
6455 }
6456}
6457
6458void ASTReader::dump() {
6459 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
6460 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
6461 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
6462 dumpModuleIDMap("Global type map", GlobalTypeMap);
6463 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
6464 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
6465 dumpModuleIDMap("Global macro map", GlobalMacroMap);
6466 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
6467 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
6468 dumpModuleIDMap("Global preprocessed entity map",
6469 GlobalPreprocessedEntityMap);
6470
6471 llvm::errs() << "\n*** PCH/Modules Loaded:";
6472 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
6473 MEnd = ModuleMgr.end();
6474 M != MEnd; ++M)
6475 (*M)->dump();
6476}
6477
6478/// Return the amount of memory used by memory buffers, breaking down
6479/// by heap-backed versus mmap'ed memory.
6480void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
6481 for (ModuleConstIterator I = ModuleMgr.begin(),
6482 E = ModuleMgr.end(); I != E; ++I) {
6483 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
6484 size_t bytes = buf->getBufferSize();
6485 switch (buf->getBufferKind()) {
6486 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
6487 sizes.malloc_bytes += bytes;
6488 break;
6489 case llvm::MemoryBuffer::MemoryBuffer_MMap:
6490 sizes.mmap_bytes += bytes;
6491 break;
6492 }
6493 }
6494 }
6495}
6496
6497void ASTReader::InitializeSema(Sema &S) {
6498 SemaObj = &S;
6499 S.addExternalSource(this);
6500
6501 // Makes sure any declarations that were deserialized "too early"
6502 // still get added to the identifier's declaration chains.
6503 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +00006504 pushExternalDeclIntoScope(PreloadedDecls[I],
6505 PreloadedDecls[I]->getDeclName());
Guy Benyei11169dd2012-12-18 14:30:41 +00006506 }
6507 PreloadedDecls.clear();
6508
Richard Smith3d8e97e2013-10-18 06:54:39 +00006509 // FIXME: What happens if these are changed by a module import?
Guy Benyei11169dd2012-12-18 14:30:41 +00006510 if (!FPPragmaOptions.empty()) {
6511 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
6512 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
6513 }
6514
Richard Smith3d8e97e2013-10-18 06:54:39 +00006515 // FIXME: What happens if these are changed by a module import?
Guy Benyei11169dd2012-12-18 14:30:41 +00006516 if (!OpenCLExtensions.empty()) {
6517 unsigned I = 0;
6518#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
6519#include "clang/Basic/OpenCLExtensions.def"
6520
6521 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
6522 }
Richard Smith3d8e97e2013-10-18 06:54:39 +00006523
6524 UpdateSema();
6525}
6526
6527void ASTReader::UpdateSema() {
6528 assert(SemaObj && "no Sema to update");
6529
6530 // Load the offsets of the declarations that Sema references.
6531 // They will be lazily deserialized when needed.
6532 if (!SemaDeclRefs.empty()) {
6533 assert(SemaDeclRefs.size() % 2 == 0);
6534 for (unsigned I = 0; I != SemaDeclRefs.size(); I += 2) {
6535 if (!SemaObj->StdNamespace)
6536 SemaObj->StdNamespace = SemaDeclRefs[I];
6537 if (!SemaObj->StdBadAlloc)
6538 SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
6539 }
6540 SemaDeclRefs.clear();
6541 }
Guy Benyei11169dd2012-12-18 14:30:41 +00006542}
6543
6544IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
6545 // Note that we are loading an identifier.
6546 Deserializing AnIdentifier(this);
Douglas Gregore060e572013-01-25 01:03:03 +00006547 StringRef Name(NameStart, NameEnd - NameStart);
6548
6549 // If there is a global index, look there first to determine which modules
6550 // provably do not have any results for this identifier.
Douglas Gregor7211ac12013-01-25 23:32:03 +00006551 GlobalModuleIndex::HitSet Hits;
6552 GlobalModuleIndex::HitSet *HitsPtr = 0;
Douglas Gregore060e572013-01-25 01:03:03 +00006553 if (!loadGlobalIndex()) {
Douglas Gregor7211ac12013-01-25 23:32:03 +00006554 if (GlobalIndex->lookupIdentifier(Name, Hits)) {
6555 HitsPtr = &Hits;
Douglas Gregore060e572013-01-25 01:03:03 +00006556 }
6557 }
Douglas Gregor7211ac12013-01-25 23:32:03 +00006558 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
Douglas Gregor00a50f72013-01-25 00:38:33 +00006559 NumIdentifierLookups,
6560 NumIdentifierLookupHits);
Douglas Gregor7211ac12013-01-25 23:32:03 +00006561 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor, HitsPtr);
Guy Benyei11169dd2012-12-18 14:30:41 +00006562 IdentifierInfo *II = Visitor.getIdentifierInfo();
6563 markIdentifierUpToDate(II);
6564 return II;
6565}
6566
6567namespace clang {
6568 /// \brief An identifier-lookup iterator that enumerates all of the
6569 /// identifiers stored within a set of AST files.
6570 class ASTIdentifierIterator : public IdentifierIterator {
6571 /// \brief The AST reader whose identifiers are being enumerated.
6572 const ASTReader &Reader;
6573
6574 /// \brief The current index into the chain of AST files stored in
6575 /// the AST reader.
6576 unsigned Index;
6577
6578 /// \brief The current position within the identifier lookup table
6579 /// of the current AST file.
6580 ASTIdentifierLookupTable::key_iterator Current;
6581
6582 /// \brief The end position within the identifier lookup table of
6583 /// the current AST file.
6584 ASTIdentifierLookupTable::key_iterator End;
6585
6586 public:
6587 explicit ASTIdentifierIterator(const ASTReader &Reader);
6588
Craig Topper3e89dfe2014-03-13 02:13:41 +00006589 StringRef Next() override;
Guy Benyei11169dd2012-12-18 14:30:41 +00006590 };
6591}
6592
6593ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
6594 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
6595 ASTIdentifierLookupTable *IdTable
6596 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
6597 Current = IdTable->key_begin();
6598 End = IdTable->key_end();
6599}
6600
6601StringRef ASTIdentifierIterator::Next() {
6602 while (Current == End) {
6603 // If we have exhausted all of our AST files, we're done.
6604 if (Index == 0)
6605 return StringRef();
6606
6607 --Index;
6608 ASTIdentifierLookupTable *IdTable
6609 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
6610 IdentifierLookupTable;
6611 Current = IdTable->key_begin();
6612 End = IdTable->key_end();
6613 }
6614
6615 // We have any identifiers remaining in the current AST file; return
6616 // the next one.
Douglas Gregorbfd73d72013-01-23 18:53:14 +00006617 StringRef Result = *Current;
Guy Benyei11169dd2012-12-18 14:30:41 +00006618 ++Current;
Douglas Gregorbfd73d72013-01-23 18:53:14 +00006619 return Result;
Guy Benyei11169dd2012-12-18 14:30:41 +00006620}
6621
Argyrios Kyrtzidis9aca3c62013-04-17 22:10:55 +00006622IdentifierIterator *ASTReader::getIdentifiers() {
6623 if (!loadGlobalIndex())
6624 return GlobalIndex->createIdentifierIterator();
6625
Guy Benyei11169dd2012-12-18 14:30:41 +00006626 return new ASTIdentifierIterator(*this);
6627}
6628
6629namespace clang { namespace serialization {
6630 class ReadMethodPoolVisitor {
6631 ASTReader &Reader;
6632 Selector Sel;
6633 unsigned PriorGeneration;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00006634 unsigned InstanceBits;
6635 unsigned FactoryBits;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00006636 SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
6637 SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
Guy Benyei11169dd2012-12-18 14:30:41 +00006638
6639 public:
6640 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
6641 unsigned PriorGeneration)
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00006642 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration),
6643 InstanceBits(0), FactoryBits(0) { }
Guy Benyei11169dd2012-12-18 14:30:41 +00006644
6645 static bool visit(ModuleFile &M, void *UserData) {
6646 ReadMethodPoolVisitor *This
6647 = static_cast<ReadMethodPoolVisitor *>(UserData);
6648
6649 if (!M.SelectorLookupTable)
6650 return false;
6651
6652 // If we've already searched this module file, skip it now.
6653 if (M.Generation <= This->PriorGeneration)
6654 return true;
6655
Douglas Gregorad2f7a52013-01-28 17:54:36 +00006656 ++This->Reader.NumMethodPoolTableLookups;
Guy Benyei11169dd2012-12-18 14:30:41 +00006657 ASTSelectorLookupTable *PoolTable
6658 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
6659 ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel);
6660 if (Pos == PoolTable->end())
6661 return false;
Douglas Gregorad2f7a52013-01-28 17:54:36 +00006662
6663 ++This->Reader.NumMethodPoolTableHits;
Guy Benyei11169dd2012-12-18 14:30:41 +00006664 ++This->Reader.NumSelectorsRead;
6665 // FIXME: Not quite happy with the statistics here. We probably should
6666 // disable this tracking when called via LoadSelector.
6667 // Also, should entries without methods count as misses?
6668 ++This->Reader.NumMethodPoolEntriesRead;
6669 ASTSelectorLookupTrait::data_type Data = *Pos;
6670 if (This->Reader.DeserializationListener)
6671 This->Reader.DeserializationListener->SelectorRead(Data.ID,
6672 This->Sel);
6673
6674 This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
6675 This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00006676 This->InstanceBits = Data.InstanceBits;
6677 This->FactoryBits = Data.FactoryBits;
Guy Benyei11169dd2012-12-18 14:30:41 +00006678 return true;
6679 }
6680
6681 /// \brief Retrieve the instance methods found by this visitor.
6682 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
6683 return InstanceMethods;
6684 }
6685
6686 /// \brief Retrieve the instance methods found by this visitor.
6687 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
6688 return FactoryMethods;
6689 }
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00006690
6691 unsigned getInstanceBits() const { return InstanceBits; }
6692 unsigned getFactoryBits() const { return FactoryBits; }
Guy Benyei11169dd2012-12-18 14:30:41 +00006693 };
6694} } // end namespace clang::serialization
6695
6696/// \brief Add the given set of methods to the method list.
6697static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
6698 ObjCMethodList &List) {
6699 for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
6700 S.addMethodToGlobalList(&List, Methods[I]);
6701 }
6702}
6703
6704void ASTReader::ReadMethodPool(Selector Sel) {
6705 // Get the selector generation and update it to the current generation.
6706 unsigned &Generation = SelectorGeneration[Sel];
6707 unsigned PriorGeneration = Generation;
6708 Generation = CurrentGeneration;
6709
6710 // Search for methods defined with this selector.
Douglas Gregorad2f7a52013-01-28 17:54:36 +00006711 ++NumMethodPoolLookups;
Guy Benyei11169dd2012-12-18 14:30:41 +00006712 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
6713 ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor);
6714
6715 if (Visitor.getInstanceMethods().empty() &&
Douglas Gregorad2f7a52013-01-28 17:54:36 +00006716 Visitor.getFactoryMethods().empty())
Guy Benyei11169dd2012-12-18 14:30:41 +00006717 return;
Douglas Gregorad2f7a52013-01-28 17:54:36 +00006718
6719 ++NumMethodPoolHits;
6720
Guy Benyei11169dd2012-12-18 14:30:41 +00006721 if (!getSema())
6722 return;
6723
6724 Sema &S = *getSema();
6725 Sema::GlobalMethodPool::iterator Pos
6726 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
6727
6728 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
6729 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00006730 Pos->second.first.setBits(Visitor.getInstanceBits());
6731 Pos->second.second.setBits(Visitor.getFactoryBits());
Guy Benyei11169dd2012-12-18 14:30:41 +00006732}
6733
6734void ASTReader::ReadKnownNamespaces(
6735 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
6736 Namespaces.clear();
6737
6738 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
6739 if (NamespaceDecl *Namespace
6740 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
6741 Namespaces.push_back(Namespace);
6742 }
6743}
6744
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00006745void ASTReader::ReadUndefinedButUsed(
Nick Lewyckyf0f56162013-01-31 03:23:57 +00006746 llvm::DenseMap<NamedDecl*, SourceLocation> &Undefined) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00006747 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
6748 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
Nick Lewycky8334af82013-01-26 00:35:08 +00006749 SourceLocation Loc =
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00006750 SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
Nick Lewycky8334af82013-01-26 00:35:08 +00006751 Undefined.insert(std::make_pair(D, Loc));
6752 }
6753}
Nick Lewycky8334af82013-01-26 00:35:08 +00006754
Guy Benyei11169dd2012-12-18 14:30:41 +00006755void ASTReader::ReadTentativeDefinitions(
6756 SmallVectorImpl<VarDecl *> &TentativeDefs) {
6757 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
6758 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
6759 if (Var)
6760 TentativeDefs.push_back(Var);
6761 }
6762 TentativeDefinitions.clear();
6763}
6764
6765void ASTReader::ReadUnusedFileScopedDecls(
6766 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
6767 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
6768 DeclaratorDecl *D
6769 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
6770 if (D)
6771 Decls.push_back(D);
6772 }
6773 UnusedFileScopedDecls.clear();
6774}
6775
6776void ASTReader::ReadDelegatingConstructors(
6777 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
6778 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
6779 CXXConstructorDecl *D
6780 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
6781 if (D)
6782 Decls.push_back(D);
6783 }
6784 DelegatingCtorDecls.clear();
6785}
6786
6787void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
6788 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
6789 TypedefNameDecl *D
6790 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
6791 if (D)
6792 Decls.push_back(D);
6793 }
6794 ExtVectorDecls.clear();
6795}
6796
6797void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) {
6798 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
6799 CXXRecordDecl *D
6800 = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I]));
6801 if (D)
6802 Decls.push_back(D);
6803 }
6804 DynamicClasses.clear();
6805}
6806
6807void
Richard Smith78165b52013-01-10 23:43:47 +00006808ASTReader::ReadLocallyScopedExternCDecls(SmallVectorImpl<NamedDecl *> &Decls) {
6809 for (unsigned I = 0, N = LocallyScopedExternCDecls.size(); I != N; ++I) {
6810 NamedDecl *D
6811 = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternCDecls[I]));
Guy Benyei11169dd2012-12-18 14:30:41 +00006812 if (D)
6813 Decls.push_back(D);
6814 }
Richard Smith78165b52013-01-10 23:43:47 +00006815 LocallyScopedExternCDecls.clear();
Guy Benyei11169dd2012-12-18 14:30:41 +00006816}
6817
6818void ASTReader::ReadReferencedSelectors(
6819 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
6820 if (ReferencedSelectorsData.empty())
6821 return;
6822
6823 // If there are @selector references added them to its pool. This is for
6824 // implementation of -Wselector.
6825 unsigned int DataSize = ReferencedSelectorsData.size()-1;
6826 unsigned I = 0;
6827 while (I < DataSize) {
6828 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
6829 SourceLocation SelLoc
6830 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
6831 Sels.push_back(std::make_pair(Sel, SelLoc));
6832 }
6833 ReferencedSelectorsData.clear();
6834}
6835
6836void ASTReader::ReadWeakUndeclaredIdentifiers(
6837 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
6838 if (WeakUndeclaredIdentifiers.empty())
6839 return;
6840
6841 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
6842 IdentifierInfo *WeakId
6843 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
6844 IdentifierInfo *AliasId
6845 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
6846 SourceLocation Loc
6847 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
6848 bool Used = WeakUndeclaredIdentifiers[I++];
6849 WeakInfo WI(AliasId, Loc);
6850 WI.setUsed(Used);
6851 WeakIDs.push_back(std::make_pair(WeakId, WI));
6852 }
6853 WeakUndeclaredIdentifiers.clear();
6854}
6855
6856void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
6857 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
6858 ExternalVTableUse VT;
6859 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
6860 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
6861 VT.DefinitionRequired = VTableUses[Idx++];
6862 VTables.push_back(VT);
6863 }
6864
6865 VTableUses.clear();
6866}
6867
6868void ASTReader::ReadPendingInstantiations(
6869 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
6870 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
6871 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
6872 SourceLocation Loc
6873 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
6874
6875 Pending.push_back(std::make_pair(D, Loc));
6876 }
6877 PendingInstantiations.clear();
6878}
6879
Richard Smithe40f2ba2013-08-07 21:41:30 +00006880void ASTReader::ReadLateParsedTemplates(
6881 llvm::DenseMap<const FunctionDecl *, LateParsedTemplate *> &LPTMap) {
6882 for (unsigned Idx = 0, N = LateParsedTemplates.size(); Idx < N;
6883 /* In loop */) {
6884 FunctionDecl *FD = cast<FunctionDecl>(GetDecl(LateParsedTemplates[Idx++]));
6885
6886 LateParsedTemplate *LT = new LateParsedTemplate;
6887 LT->D = GetDecl(LateParsedTemplates[Idx++]);
6888
6889 ModuleFile *F = getOwningModuleFile(LT->D);
6890 assert(F && "No module");
6891
6892 unsigned TokN = LateParsedTemplates[Idx++];
6893 LT->Toks.reserve(TokN);
6894 for (unsigned T = 0; T < TokN; ++T)
6895 LT->Toks.push_back(ReadToken(*F, LateParsedTemplates, Idx));
6896
6897 LPTMap[FD] = LT;
6898 }
6899
6900 LateParsedTemplates.clear();
6901}
6902
Guy Benyei11169dd2012-12-18 14:30:41 +00006903void ASTReader::LoadSelector(Selector Sel) {
6904 // It would be complicated to avoid reading the methods anyway. So don't.
6905 ReadMethodPool(Sel);
6906}
6907
6908void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
6909 assert(ID && "Non-zero identifier ID required");
6910 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
6911 IdentifiersLoaded[ID - 1] = II;
6912 if (DeserializationListener)
6913 DeserializationListener->IdentifierRead(ID, II);
6914}
6915
6916/// \brief Set the globally-visible declarations associated with the given
6917/// identifier.
6918///
6919/// If the AST reader is currently in a state where the given declaration IDs
6920/// cannot safely be resolved, they are queued until it is safe to resolve
6921/// them.
6922///
6923/// \param II an IdentifierInfo that refers to one or more globally-visible
6924/// declarations.
6925///
6926/// \param DeclIDs the set of declaration IDs with the name @p II that are
6927/// visible at global scope.
6928///
Douglas Gregor6168bd22013-02-18 15:53:43 +00006929/// \param Decls if non-null, this vector will be populated with the set of
6930/// deserialized declarations. These declarations will not be pushed into
6931/// scope.
Guy Benyei11169dd2012-12-18 14:30:41 +00006932void
6933ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
6934 const SmallVectorImpl<uint32_t> &DeclIDs,
Douglas Gregor6168bd22013-02-18 15:53:43 +00006935 SmallVectorImpl<Decl *> *Decls) {
6936 if (NumCurrentElementsDeserializing && !Decls) {
6937 PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
Guy Benyei11169dd2012-12-18 14:30:41 +00006938 return;
6939 }
6940
6941 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
6942 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
6943 if (SemaObj) {
Douglas Gregor6168bd22013-02-18 15:53:43 +00006944 // If we're simply supposed to record the declarations, do so now.
6945 if (Decls) {
6946 Decls->push_back(D);
6947 continue;
6948 }
6949
Guy Benyei11169dd2012-12-18 14:30:41 +00006950 // Introduce this declaration into the translation-unit scope
6951 // and add it to the declaration chain for this identifier, so
6952 // that (unqualified) name lookup will find it.
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +00006953 pushExternalDeclIntoScope(D, II);
Guy Benyei11169dd2012-12-18 14:30:41 +00006954 } else {
6955 // Queue this declaration so that it will be added to the
6956 // translation unit scope and identifier's declaration chain
6957 // once a Sema object is known.
6958 PreloadedDecls.push_back(D);
6959 }
6960 }
6961}
6962
Douglas Gregorc8a992f2013-01-21 16:52:34 +00006963IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00006964 if (ID == 0)
6965 return 0;
6966
6967 if (IdentifiersLoaded.empty()) {
6968 Error("no identifier table in AST file");
6969 return 0;
6970 }
6971
6972 ID -= 1;
6973 if (!IdentifiersLoaded[ID]) {
6974 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
6975 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
6976 ModuleFile *M = I->second;
6977 unsigned Index = ID - M->BaseIdentifierID;
6978 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
6979
6980 // All of the strings in the AST file are preceded by a 16-bit length.
6981 // Extract that 16-bit length to avoid having to execute strlen().
6982 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
6983 // unsigned integers. This is important to avoid integer overflow when
6984 // we cast them to 'unsigned'.
6985 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
6986 unsigned StrLen = (((unsigned) StrLenPtr[0])
6987 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Douglas Gregorc8a992f2013-01-21 16:52:34 +00006988 IdentifiersLoaded[ID]
6989 = &PP.getIdentifierTable().get(StringRef(Str, StrLen));
Guy Benyei11169dd2012-12-18 14:30:41 +00006990 if (DeserializationListener)
6991 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
6992 }
6993
6994 return IdentifiersLoaded[ID];
6995}
6996
Douglas Gregorc8a992f2013-01-21 16:52:34 +00006997IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
6998 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
Guy Benyei11169dd2012-12-18 14:30:41 +00006999}
7000
7001IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
7002 if (LocalID < NUM_PREDEF_IDENT_IDS)
7003 return LocalID;
7004
7005 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7006 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
7007 assert(I != M.IdentifierRemap.end()
7008 && "Invalid index into identifier index remap");
7009
7010 return LocalID + I->second;
7011}
7012
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00007013MacroInfo *ASTReader::getMacro(MacroID ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007014 if (ID == 0)
7015 return 0;
7016
7017 if (MacrosLoaded.empty()) {
7018 Error("no macro table in AST file");
7019 return 0;
7020 }
7021
7022 ID -= NUM_PREDEF_MACRO_IDS;
7023 if (!MacrosLoaded[ID]) {
7024 GlobalMacroMapType::iterator I
7025 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
7026 assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
7027 ModuleFile *M = I->second;
7028 unsigned Index = ID - M->BaseMacroID;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00007029 MacrosLoaded[ID] = ReadMacroRecord(*M, M->MacroOffsets[Index]);
7030
7031 if (DeserializationListener)
7032 DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
7033 MacrosLoaded[ID]);
Guy Benyei11169dd2012-12-18 14:30:41 +00007034 }
7035
7036 return MacrosLoaded[ID];
7037}
7038
7039MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
7040 if (LocalID < NUM_PREDEF_MACRO_IDS)
7041 return LocalID;
7042
7043 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7044 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
7045 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
7046
7047 return LocalID + I->second;
7048}
7049
7050serialization::SubmoduleID
7051ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
7052 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
7053 return LocalID;
7054
7055 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7056 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
7057 assert(I != M.SubmoduleRemap.end()
7058 && "Invalid index into submodule index remap");
7059
7060 return LocalID + I->second;
7061}
7062
7063Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
7064 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
7065 assert(GlobalID == 0 && "Unhandled global submodule ID");
7066 return 0;
7067 }
7068
7069 if (GlobalID > SubmodulesLoaded.size()) {
7070 Error("submodule ID out of range in AST file");
7071 return 0;
7072 }
7073
7074 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
7075}
Douglas Gregorc147b0b2013-01-12 01:29:50 +00007076
7077Module *ASTReader::getModule(unsigned ID) {
7078 return getSubmodule(ID);
7079}
7080
Guy Benyei11169dd2012-12-18 14:30:41 +00007081Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
7082 return DecodeSelector(getGlobalSelectorID(M, LocalID));
7083}
7084
7085Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
7086 if (ID == 0)
7087 return Selector();
7088
7089 if (ID > SelectorsLoaded.size()) {
7090 Error("selector ID out of range in AST file");
7091 return Selector();
7092 }
7093
7094 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
7095 // Load this selector from the selector table.
7096 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
7097 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
7098 ModuleFile &M = *I->second;
7099 ASTSelectorLookupTrait Trait(*this, M);
7100 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
7101 SelectorsLoaded[ID - 1] =
7102 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
7103 if (DeserializationListener)
7104 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
7105 }
7106
7107 return SelectorsLoaded[ID - 1];
7108}
7109
7110Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
7111 return DecodeSelector(ID);
7112}
7113
7114uint32_t ASTReader::GetNumExternalSelectors() {
7115 // ID 0 (the null selector) is considered an external selector.
7116 return getTotalNumSelectors() + 1;
7117}
7118
7119serialization::SelectorID
7120ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
7121 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
7122 return LocalID;
7123
7124 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7125 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
7126 assert(I != M.SelectorRemap.end()
7127 && "Invalid index into selector index remap");
7128
7129 return LocalID + I->second;
7130}
7131
7132DeclarationName
7133ASTReader::ReadDeclarationName(ModuleFile &F,
7134 const RecordData &Record, unsigned &Idx) {
7135 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
7136 switch (Kind) {
7137 case DeclarationName::Identifier:
Douglas Gregorc8a992f2013-01-21 16:52:34 +00007138 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00007139
7140 case DeclarationName::ObjCZeroArgSelector:
7141 case DeclarationName::ObjCOneArgSelector:
7142 case DeclarationName::ObjCMultiArgSelector:
7143 return DeclarationName(ReadSelector(F, Record, Idx));
7144
7145 case DeclarationName::CXXConstructorName:
7146 return Context.DeclarationNames.getCXXConstructorName(
7147 Context.getCanonicalType(readType(F, Record, Idx)));
7148
7149 case DeclarationName::CXXDestructorName:
7150 return Context.DeclarationNames.getCXXDestructorName(
7151 Context.getCanonicalType(readType(F, Record, Idx)));
7152
7153 case DeclarationName::CXXConversionFunctionName:
7154 return Context.DeclarationNames.getCXXConversionFunctionName(
7155 Context.getCanonicalType(readType(F, Record, Idx)));
7156
7157 case DeclarationName::CXXOperatorName:
7158 return Context.DeclarationNames.getCXXOperatorName(
7159 (OverloadedOperatorKind)Record[Idx++]);
7160
7161 case DeclarationName::CXXLiteralOperatorName:
7162 return Context.DeclarationNames.getCXXLiteralOperatorName(
7163 GetIdentifierInfo(F, Record, Idx));
7164
7165 case DeclarationName::CXXUsingDirective:
7166 return DeclarationName::getUsingDirectiveName();
7167 }
7168
7169 llvm_unreachable("Invalid NameKind!");
7170}
7171
7172void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
7173 DeclarationNameLoc &DNLoc,
7174 DeclarationName Name,
7175 const RecordData &Record, unsigned &Idx) {
7176 switch (Name.getNameKind()) {
7177 case DeclarationName::CXXConstructorName:
7178 case DeclarationName::CXXDestructorName:
7179 case DeclarationName::CXXConversionFunctionName:
7180 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
7181 break;
7182
7183 case DeclarationName::CXXOperatorName:
7184 DNLoc.CXXOperatorName.BeginOpNameLoc
7185 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
7186 DNLoc.CXXOperatorName.EndOpNameLoc
7187 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
7188 break;
7189
7190 case DeclarationName::CXXLiteralOperatorName:
7191 DNLoc.CXXLiteralOperatorName.OpNameLoc
7192 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
7193 break;
7194
7195 case DeclarationName::Identifier:
7196 case DeclarationName::ObjCZeroArgSelector:
7197 case DeclarationName::ObjCOneArgSelector:
7198 case DeclarationName::ObjCMultiArgSelector:
7199 case DeclarationName::CXXUsingDirective:
7200 break;
7201 }
7202}
7203
7204void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
7205 DeclarationNameInfo &NameInfo,
7206 const RecordData &Record, unsigned &Idx) {
7207 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
7208 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
7209 DeclarationNameLoc DNLoc;
7210 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
7211 NameInfo.setInfo(DNLoc);
7212}
7213
7214void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
7215 const RecordData &Record, unsigned &Idx) {
7216 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
7217 unsigned NumTPLists = Record[Idx++];
7218 Info.NumTemplParamLists = NumTPLists;
7219 if (NumTPLists) {
7220 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
7221 for (unsigned i=0; i != NumTPLists; ++i)
7222 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
7223 }
7224}
7225
7226TemplateName
7227ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
7228 unsigned &Idx) {
7229 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
7230 switch (Kind) {
7231 case TemplateName::Template:
7232 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
7233
7234 case TemplateName::OverloadedTemplate: {
7235 unsigned size = Record[Idx++];
7236 UnresolvedSet<8> Decls;
7237 while (size--)
7238 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
7239
7240 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
7241 }
7242
7243 case TemplateName::QualifiedTemplate: {
7244 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
7245 bool hasTemplKeyword = Record[Idx++];
7246 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
7247 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
7248 }
7249
7250 case TemplateName::DependentTemplate: {
7251 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
7252 if (Record[Idx++]) // isIdentifier
7253 return Context.getDependentTemplateName(NNS,
7254 GetIdentifierInfo(F, Record,
7255 Idx));
7256 return Context.getDependentTemplateName(NNS,
7257 (OverloadedOperatorKind)Record[Idx++]);
7258 }
7259
7260 case TemplateName::SubstTemplateTemplateParm: {
7261 TemplateTemplateParmDecl *param
7262 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
7263 if (!param) return TemplateName();
7264 TemplateName replacement = ReadTemplateName(F, Record, Idx);
7265 return Context.getSubstTemplateTemplateParm(param, replacement);
7266 }
7267
7268 case TemplateName::SubstTemplateTemplateParmPack: {
7269 TemplateTemplateParmDecl *Param
7270 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
7271 if (!Param)
7272 return TemplateName();
7273
7274 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
7275 if (ArgPack.getKind() != TemplateArgument::Pack)
7276 return TemplateName();
7277
7278 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
7279 }
7280 }
7281
7282 llvm_unreachable("Unhandled template name kind!");
7283}
7284
7285TemplateArgument
7286ASTReader::ReadTemplateArgument(ModuleFile &F,
7287 const RecordData &Record, unsigned &Idx) {
7288 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
7289 switch (Kind) {
7290 case TemplateArgument::Null:
7291 return TemplateArgument();
7292 case TemplateArgument::Type:
7293 return TemplateArgument(readType(F, Record, Idx));
7294 case TemplateArgument::Declaration: {
7295 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
7296 bool ForReferenceParam = Record[Idx++];
7297 return TemplateArgument(D, ForReferenceParam);
7298 }
7299 case TemplateArgument::NullPtr:
7300 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
7301 case TemplateArgument::Integral: {
7302 llvm::APSInt Value = ReadAPSInt(Record, Idx);
7303 QualType T = readType(F, Record, Idx);
7304 return TemplateArgument(Context, Value, T);
7305 }
7306 case TemplateArgument::Template:
7307 return TemplateArgument(ReadTemplateName(F, Record, Idx));
7308 case TemplateArgument::TemplateExpansion: {
7309 TemplateName Name = ReadTemplateName(F, Record, Idx);
David Blaikie05785d12013-02-20 22:23:23 +00007310 Optional<unsigned> NumTemplateExpansions;
Guy Benyei11169dd2012-12-18 14:30:41 +00007311 if (unsigned NumExpansions = Record[Idx++])
7312 NumTemplateExpansions = NumExpansions - 1;
7313 return TemplateArgument(Name, NumTemplateExpansions);
7314 }
7315 case TemplateArgument::Expression:
7316 return TemplateArgument(ReadExpr(F));
7317 case TemplateArgument::Pack: {
7318 unsigned NumArgs = Record[Idx++];
7319 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
7320 for (unsigned I = 0; I != NumArgs; ++I)
7321 Args[I] = ReadTemplateArgument(F, Record, Idx);
7322 return TemplateArgument(Args, NumArgs);
7323 }
7324 }
7325
7326 llvm_unreachable("Unhandled template argument kind!");
7327}
7328
7329TemplateParameterList *
7330ASTReader::ReadTemplateParameterList(ModuleFile &F,
7331 const RecordData &Record, unsigned &Idx) {
7332 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
7333 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
7334 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
7335
7336 unsigned NumParams = Record[Idx++];
7337 SmallVector<NamedDecl *, 16> Params;
7338 Params.reserve(NumParams);
7339 while (NumParams--)
7340 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
7341
7342 TemplateParameterList* TemplateParams =
7343 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
7344 Params.data(), Params.size(), RAngleLoc);
7345 return TemplateParams;
7346}
7347
7348void
7349ASTReader::
Craig Topper5603df42013-07-05 19:34:19 +00007350ReadTemplateArgumentList(SmallVectorImpl<TemplateArgument> &TemplArgs,
Guy Benyei11169dd2012-12-18 14:30:41 +00007351 ModuleFile &F, const RecordData &Record,
7352 unsigned &Idx) {
7353 unsigned NumTemplateArgs = Record[Idx++];
7354 TemplArgs.reserve(NumTemplateArgs);
7355 while (NumTemplateArgs--)
7356 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
7357}
7358
7359/// \brief Read a UnresolvedSet structure.
Richard Smitha4ba74c2013-08-30 04:46:40 +00007360void ASTReader::ReadUnresolvedSet(ModuleFile &F, LazyASTUnresolvedSet &Set,
Guy Benyei11169dd2012-12-18 14:30:41 +00007361 const RecordData &Record, unsigned &Idx) {
7362 unsigned NumDecls = Record[Idx++];
7363 Set.reserve(Context, NumDecls);
7364 while (NumDecls--) {
Richard Smitha4ba74c2013-08-30 04:46:40 +00007365 DeclID ID = ReadDeclID(F, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00007366 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
Richard Smitha4ba74c2013-08-30 04:46:40 +00007367 Set.addLazyDecl(Context, ID, AS);
Guy Benyei11169dd2012-12-18 14:30:41 +00007368 }
7369}
7370
7371CXXBaseSpecifier
7372ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
7373 const RecordData &Record, unsigned &Idx) {
7374 bool isVirtual = static_cast<bool>(Record[Idx++]);
7375 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
7376 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
7377 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
7378 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
7379 SourceRange Range = ReadSourceRange(F, Record, Idx);
7380 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
7381 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
7382 EllipsisLoc);
7383 Result.setInheritConstructors(inheritConstructors);
7384 return Result;
7385}
7386
7387std::pair<CXXCtorInitializer **, unsigned>
7388ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
7389 unsigned &Idx) {
7390 CXXCtorInitializer **CtorInitializers = 0;
7391 unsigned NumInitializers = Record[Idx++];
7392 if (NumInitializers) {
7393 CtorInitializers
7394 = new (Context) CXXCtorInitializer*[NumInitializers];
7395 for (unsigned i=0; i != NumInitializers; ++i) {
7396 TypeSourceInfo *TInfo = 0;
7397 bool IsBaseVirtual = false;
7398 FieldDecl *Member = 0;
7399 IndirectFieldDecl *IndirectMember = 0;
7400
7401 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
7402 switch (Type) {
7403 case CTOR_INITIALIZER_BASE:
7404 TInfo = GetTypeSourceInfo(F, Record, Idx);
7405 IsBaseVirtual = Record[Idx++];
7406 break;
7407
7408 case CTOR_INITIALIZER_DELEGATING:
7409 TInfo = GetTypeSourceInfo(F, Record, Idx);
7410 break;
7411
7412 case CTOR_INITIALIZER_MEMBER:
7413 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
7414 break;
7415
7416 case CTOR_INITIALIZER_INDIRECT_MEMBER:
7417 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
7418 break;
7419 }
7420
7421 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
7422 Expr *Init = ReadExpr(F);
7423 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
7424 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
7425 bool IsWritten = Record[Idx++];
7426 unsigned SourceOrderOrNumArrayIndices;
7427 SmallVector<VarDecl *, 8> Indices;
7428 if (IsWritten) {
7429 SourceOrderOrNumArrayIndices = Record[Idx++];
7430 } else {
7431 SourceOrderOrNumArrayIndices = Record[Idx++];
7432 Indices.reserve(SourceOrderOrNumArrayIndices);
7433 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
7434 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
7435 }
7436
7437 CXXCtorInitializer *BOMInit;
7438 if (Type == CTOR_INITIALIZER_BASE) {
7439 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, IsBaseVirtual,
7440 LParenLoc, Init, RParenLoc,
7441 MemberOrEllipsisLoc);
7442 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
7443 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, LParenLoc,
7444 Init, RParenLoc);
7445 } else if (IsWritten) {
7446 if (Member)
7447 BOMInit = new (Context) CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc,
7448 LParenLoc, Init, RParenLoc);
7449 else
7450 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
7451 MemberOrEllipsisLoc, LParenLoc,
7452 Init, RParenLoc);
7453 } else {
Argyrios Kyrtzidis794671d2013-05-30 23:59:46 +00007454 if (IndirectMember) {
7455 assert(Indices.empty() && "Indirect field improperly initialized");
7456 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
7457 MemberOrEllipsisLoc, LParenLoc,
7458 Init, RParenLoc);
7459 } else {
7460 BOMInit = CXXCtorInitializer::Create(Context, Member, MemberOrEllipsisLoc,
7461 LParenLoc, Init, RParenLoc,
7462 Indices.data(), Indices.size());
7463 }
Guy Benyei11169dd2012-12-18 14:30:41 +00007464 }
7465
7466 if (IsWritten)
7467 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
7468 CtorInitializers[i] = BOMInit;
7469 }
7470 }
7471
7472 return std::make_pair(CtorInitializers, NumInitializers);
7473}
7474
7475NestedNameSpecifier *
7476ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
7477 const RecordData &Record, unsigned &Idx) {
7478 unsigned N = Record[Idx++];
7479 NestedNameSpecifier *NNS = 0, *Prev = 0;
7480 for (unsigned I = 0; I != N; ++I) {
7481 NestedNameSpecifier::SpecifierKind Kind
7482 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
7483 switch (Kind) {
7484 case NestedNameSpecifier::Identifier: {
7485 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
7486 NNS = NestedNameSpecifier::Create(Context, Prev, II);
7487 break;
7488 }
7489
7490 case NestedNameSpecifier::Namespace: {
7491 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
7492 NNS = NestedNameSpecifier::Create(Context, Prev, NS);
7493 break;
7494 }
7495
7496 case NestedNameSpecifier::NamespaceAlias: {
7497 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
7498 NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
7499 break;
7500 }
7501
7502 case NestedNameSpecifier::TypeSpec:
7503 case NestedNameSpecifier::TypeSpecWithTemplate: {
7504 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
7505 if (!T)
7506 return 0;
7507
7508 bool Template = Record[Idx++];
7509 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
7510 break;
7511 }
7512
7513 case NestedNameSpecifier::Global: {
7514 NNS = NestedNameSpecifier::GlobalSpecifier(Context);
7515 // No associated value, and there can't be a prefix.
7516 break;
7517 }
7518 }
7519 Prev = NNS;
7520 }
7521 return NNS;
7522}
7523
7524NestedNameSpecifierLoc
7525ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
7526 unsigned &Idx) {
7527 unsigned N = Record[Idx++];
7528 NestedNameSpecifierLocBuilder Builder;
7529 for (unsigned I = 0; I != N; ++I) {
7530 NestedNameSpecifier::SpecifierKind Kind
7531 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
7532 switch (Kind) {
7533 case NestedNameSpecifier::Identifier: {
7534 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
7535 SourceRange Range = ReadSourceRange(F, Record, Idx);
7536 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
7537 break;
7538 }
7539
7540 case NestedNameSpecifier::Namespace: {
7541 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
7542 SourceRange Range = ReadSourceRange(F, Record, Idx);
7543 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
7544 break;
7545 }
7546
7547 case NestedNameSpecifier::NamespaceAlias: {
7548 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
7549 SourceRange Range = ReadSourceRange(F, Record, Idx);
7550 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
7551 break;
7552 }
7553
7554 case NestedNameSpecifier::TypeSpec:
7555 case NestedNameSpecifier::TypeSpecWithTemplate: {
7556 bool Template = Record[Idx++];
7557 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
7558 if (!T)
7559 return NestedNameSpecifierLoc();
7560 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
7561
7562 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
7563 Builder.Extend(Context,
7564 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
7565 T->getTypeLoc(), ColonColonLoc);
7566 break;
7567 }
7568
7569 case NestedNameSpecifier::Global: {
7570 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
7571 Builder.MakeGlobal(Context, ColonColonLoc);
7572 break;
7573 }
7574 }
7575 }
7576
7577 return Builder.getWithLocInContext(Context);
7578}
7579
7580SourceRange
7581ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
7582 unsigned &Idx) {
7583 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
7584 SourceLocation end = ReadSourceLocation(F, Record, Idx);
7585 return SourceRange(beg, end);
7586}
7587
7588/// \brief Read an integral value
7589llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
7590 unsigned BitWidth = Record[Idx++];
7591 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
7592 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
7593 Idx += NumWords;
7594 return Result;
7595}
7596
7597/// \brief Read a signed integral value
7598llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
7599 bool isUnsigned = Record[Idx++];
7600 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
7601}
7602
7603/// \brief Read a floating-point value
Tim Northover178723a2013-01-22 09:46:51 +00007604llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record,
7605 const llvm::fltSemantics &Sem,
7606 unsigned &Idx) {
7607 return llvm::APFloat(Sem, ReadAPInt(Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00007608}
7609
7610// \brief Read a string
7611std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
7612 unsigned Len = Record[Idx++];
7613 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
7614 Idx += Len;
7615 return Result;
7616}
7617
7618VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
7619 unsigned &Idx) {
7620 unsigned Major = Record[Idx++];
7621 unsigned Minor = Record[Idx++];
7622 unsigned Subminor = Record[Idx++];
7623 if (Minor == 0)
7624 return VersionTuple(Major);
7625 if (Subminor == 0)
7626 return VersionTuple(Major, Minor - 1);
7627 return VersionTuple(Major, Minor - 1, Subminor - 1);
7628}
7629
7630CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
7631 const RecordData &Record,
7632 unsigned &Idx) {
7633 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
7634 return CXXTemporary::Create(Context, Decl);
7635}
7636
7637DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Argyrios Kyrtzidisdc9fdaf2013-05-24 05:44:08 +00007638 return Diag(CurrentImportLoc, DiagID);
Guy Benyei11169dd2012-12-18 14:30:41 +00007639}
7640
7641DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
7642 return Diags.Report(Loc, DiagID);
7643}
7644
7645/// \brief Retrieve the identifier table associated with the
7646/// preprocessor.
7647IdentifierTable &ASTReader::getIdentifierTable() {
7648 return PP.getIdentifierTable();
7649}
7650
7651/// \brief Record that the given ID maps to the given switch-case
7652/// statement.
7653void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
7654 assert((*CurrSwitchCaseStmts)[ID] == 0 &&
7655 "Already have a SwitchCase with this ID");
7656 (*CurrSwitchCaseStmts)[ID] = SC;
7657}
7658
7659/// \brief Retrieve the switch-case statement with the given ID.
7660SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
7661 assert((*CurrSwitchCaseStmts)[ID] != 0 && "No SwitchCase with this ID");
7662 return (*CurrSwitchCaseStmts)[ID];
7663}
7664
7665void ASTReader::ClearSwitchCaseIDs() {
7666 CurrSwitchCaseStmts->clear();
7667}
7668
7669void ASTReader::ReadComments() {
7670 std::vector<RawComment *> Comments;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00007671 for (SmallVectorImpl<std::pair<BitstreamCursor,
Guy Benyei11169dd2012-12-18 14:30:41 +00007672 serialization::ModuleFile *> >::iterator
7673 I = CommentsCursors.begin(),
7674 E = CommentsCursors.end();
7675 I != E; ++I) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00007676 BitstreamCursor &Cursor = I->first;
Guy Benyei11169dd2012-12-18 14:30:41 +00007677 serialization::ModuleFile &F = *I->second;
7678 SavedStreamPosition SavedPosition(Cursor);
7679
7680 RecordData Record;
7681 while (true) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00007682 llvm::BitstreamEntry Entry =
7683 Cursor.advanceSkippingSubblocks(BitstreamCursor::AF_DontPopBlockAtEnd);
7684
7685 switch (Entry.Kind) {
7686 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
7687 case llvm::BitstreamEntry::Error:
7688 Error("malformed block record in AST file");
7689 return;
7690 case llvm::BitstreamEntry::EndBlock:
7691 goto NextCursor;
7692 case llvm::BitstreamEntry::Record:
7693 // The interesting case.
Guy Benyei11169dd2012-12-18 14:30:41 +00007694 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00007695 }
7696
7697 // Read a record.
7698 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00007699 switch ((CommentRecordTypes)Cursor.readRecord(Entry.ID, Record)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007700 case COMMENTS_RAW_COMMENT: {
7701 unsigned Idx = 0;
7702 SourceRange SR = ReadSourceRange(F, Record, Idx);
7703 RawComment::CommentKind Kind =
7704 (RawComment::CommentKind) Record[Idx++];
7705 bool IsTrailingComment = Record[Idx++];
7706 bool IsAlmostTrailingComment = Record[Idx++];
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00007707 Comments.push_back(new (Context) RawComment(
7708 SR, Kind, IsTrailingComment, IsAlmostTrailingComment,
7709 Context.getLangOpts().CommentOpts.ParseAllComments));
Guy Benyei11169dd2012-12-18 14:30:41 +00007710 break;
7711 }
7712 }
7713 }
Chris Lattner7fb3bef2013-01-20 00:56:42 +00007714 NextCursor:;
Guy Benyei11169dd2012-12-18 14:30:41 +00007715 }
7716 Context.Comments.addCommentsToFront(Comments);
7717}
7718
7719void ASTReader::finishPendingActions() {
7720 while (!PendingIdentifierInfos.empty() || !PendingDeclChains.empty() ||
Richard Smith2b9e3e32013-10-18 06:05:18 +00007721 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
7722 !PendingOdrMergeChecks.empty()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007723 // If any identifiers with corresponding top-level declarations have
7724 // been loaded, load those declarations now.
Craig Topper79be4cd2013-07-05 04:33:53 +00007725 typedef llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2> >
7726 TopLevelDeclsMap;
7727 TopLevelDeclsMap TopLevelDecls;
7728
Guy Benyei11169dd2012-12-18 14:30:41 +00007729 while (!PendingIdentifierInfos.empty()) {
Douglas Gregor6168bd22013-02-18 15:53:43 +00007730 // FIXME: std::move
7731 IdentifierInfo *II = PendingIdentifierInfos.back().first;
7732 SmallVector<uint32_t, 4> DeclIDs = PendingIdentifierInfos.back().second;
Douglas Gregorcb15f082013-02-19 18:26:28 +00007733 PendingIdentifierInfos.pop_back();
Douglas Gregor6168bd22013-02-18 15:53:43 +00007734
7735 SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
Guy Benyei11169dd2012-12-18 14:30:41 +00007736 }
7737
7738 // Load pending declaration chains.
7739 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) {
7740 loadPendingDeclChain(PendingDeclChains[I]);
7741 PendingDeclChainsKnown.erase(PendingDeclChains[I]);
7742 }
7743 PendingDeclChains.clear();
7744
Douglas Gregor6168bd22013-02-18 15:53:43 +00007745 // Make the most recent of the top-level declarations visible.
Craig Topper79be4cd2013-07-05 04:33:53 +00007746 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
7747 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
Douglas Gregor6168bd22013-02-18 15:53:43 +00007748 IdentifierInfo *II = TLD->first;
7749 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +00007750 pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
Douglas Gregor6168bd22013-02-18 15:53:43 +00007751 }
7752 }
7753
Guy Benyei11169dd2012-12-18 14:30:41 +00007754 // Load any pending macro definitions.
7755 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00007756 IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
7757 SmallVector<PendingMacroInfo, 2> GlobalIDs;
7758 GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
7759 // Initialize the macro history from chained-PCHs ahead of module imports.
Richard Smith49f906a2014-03-01 00:08:04 +00007760 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00007761 ++IDIdx) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00007762 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
7763 if (Info.M->Kind != MK_Module)
7764 resolvePendingMacro(II, Info);
7765 }
7766 // Handle module imports.
Richard Smith49f906a2014-03-01 00:08:04 +00007767 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00007768 ++IDIdx) {
7769 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
7770 if (Info.M->Kind == MK_Module)
7771 resolvePendingMacro(II, Info);
Guy Benyei11169dd2012-12-18 14:30:41 +00007772 }
7773 }
7774 PendingMacroIDs.clear();
Argyrios Kyrtzidis83a6e3b2013-02-16 00:48:59 +00007775
7776 // Wire up the DeclContexts for Decls that we delayed setting until
7777 // recursive loading is completed.
7778 while (!PendingDeclContextInfos.empty()) {
7779 PendingDeclContextInfo Info = PendingDeclContextInfos.front();
7780 PendingDeclContextInfos.pop_front();
7781 DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
7782 DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
7783 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
7784 }
Richard Smith2b9e3e32013-10-18 06:05:18 +00007785
7786 // For each declaration from a merged context, check that the canonical
7787 // definition of that context also contains a declaration of the same
7788 // entity.
7789 while (!PendingOdrMergeChecks.empty()) {
7790 NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
7791
7792 // FIXME: Skip over implicit declarations for now. This matters for things
7793 // like implicitly-declared special member functions. This isn't entirely
7794 // correct; we can end up with multiple unmerged declarations of the same
7795 // implicit entity.
7796 if (D->isImplicit())
7797 continue;
7798
7799 DeclContext *CanonDef = D->getDeclContext();
7800 DeclContext::lookup_result R = CanonDef->lookup(D->getDeclName());
7801
7802 bool Found = false;
7803 const Decl *DCanon = D->getCanonicalDecl();
7804
7805 llvm::SmallVector<const NamedDecl*, 4> Candidates;
7806 for (DeclContext::lookup_iterator I = R.begin(), E = R.end();
7807 !Found && I != E; ++I) {
Aaron Ballman86c93902014-03-06 23:45:36 +00007808 for (auto RI : (*I)->redecls()) {
7809 if (RI->getLexicalDeclContext() == CanonDef) {
Richard Smith2b9e3e32013-10-18 06:05:18 +00007810 // This declaration is present in the canonical definition. If it's
7811 // in the same redecl chain, it's the one we're looking for.
Aaron Ballman86c93902014-03-06 23:45:36 +00007812 if (RI->getCanonicalDecl() == DCanon)
Richard Smith2b9e3e32013-10-18 06:05:18 +00007813 Found = true;
7814 else
Aaron Ballman86c93902014-03-06 23:45:36 +00007815 Candidates.push_back(cast<NamedDecl>(RI));
Richard Smith2b9e3e32013-10-18 06:05:18 +00007816 break;
7817 }
7818 }
7819 }
7820
7821 if (!Found) {
7822 D->setInvalidDecl();
7823
7824 Module *CanonDefModule = cast<Decl>(CanonDef)->getOwningModule();
7825 Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
7826 << D << D->getOwningModule()->getFullModuleName()
7827 << CanonDef << !CanonDefModule
7828 << (CanonDefModule ? CanonDefModule->getFullModuleName() : "");
7829
7830 if (Candidates.empty())
7831 Diag(cast<Decl>(CanonDef)->getLocation(),
7832 diag::note_module_odr_violation_no_possible_decls) << D;
7833 else {
7834 for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
7835 Diag(Candidates[I]->getLocation(),
7836 diag::note_module_odr_violation_possible_decl)
7837 << Candidates[I];
7838 }
7839 }
7840 }
Guy Benyei11169dd2012-12-18 14:30:41 +00007841 }
7842
7843 // If we deserialized any C++ or Objective-C class definitions, any
7844 // Objective-C protocol definitions, or any redeclarable templates, make sure
7845 // that all redeclarations point to the definitions. Note that this can only
7846 // happen now, after the redeclaration chains have been fully wired.
7847 for (llvm::SmallPtrSet<Decl *, 4>::iterator D = PendingDefinitions.begin(),
7848 DEnd = PendingDefinitions.end();
7849 D != DEnd; ++D) {
7850 if (TagDecl *TD = dyn_cast<TagDecl>(*D)) {
7851 if (const TagType *TagT = dyn_cast<TagType>(TD->TypeForDecl)) {
7852 // Make sure that the TagType points at the definition.
7853 const_cast<TagType*>(TagT)->decl = TD;
7854 }
7855
Aaron Ballman86c93902014-03-06 23:45:36 +00007856 if (auto RD = dyn_cast<CXXRecordDecl>(*D)) {
7857 for (auto R : RD->redecls())
7858 cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
Guy Benyei11169dd2012-12-18 14:30:41 +00007859
7860 }
7861
7862 continue;
7863 }
7864
Aaron Ballman86c93902014-03-06 23:45:36 +00007865 if (auto ID = dyn_cast<ObjCInterfaceDecl>(*D)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007866 // Make sure that the ObjCInterfaceType points at the definition.
7867 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
7868 ->Decl = ID;
7869
Aaron Ballman86c93902014-03-06 23:45:36 +00007870 for (auto R : ID->redecls())
Guy Benyei11169dd2012-12-18 14:30:41 +00007871 R->Data = ID->Data;
7872
7873 continue;
7874 }
7875
Aaron Ballman86c93902014-03-06 23:45:36 +00007876 if (auto PD = dyn_cast<ObjCProtocolDecl>(*D)) {
7877 for (auto R : PD->redecls())
Guy Benyei11169dd2012-12-18 14:30:41 +00007878 R->Data = PD->Data;
7879
7880 continue;
7881 }
7882
Aaron Ballman86c93902014-03-06 23:45:36 +00007883 auto RTD = cast<RedeclarableTemplateDecl>(*D)->getCanonicalDecl();
7884 for (auto R : RTD->redecls())
Guy Benyei11169dd2012-12-18 14:30:41 +00007885 R->Common = RTD->Common;
7886 }
7887 PendingDefinitions.clear();
7888
7889 // Load the bodies of any functions or methods we've encountered. We do
7890 // this now (delayed) so that we can be sure that the declaration chains
7891 // have been fully wired up.
7892 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
7893 PBEnd = PendingBodies.end();
7894 PB != PBEnd; ++PB) {
7895 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
7896 // FIXME: Check for =delete/=default?
7897 // FIXME: Complain about ODR violations here?
7898 if (!getContext().getLangOpts().Modules || !FD->hasBody())
7899 FD->setLazyBody(PB->second);
7900 continue;
7901 }
7902
7903 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
7904 if (!getContext().getLangOpts().Modules || !MD->hasBody())
7905 MD->setLazyBody(PB->second);
7906 }
7907 PendingBodies.clear();
7908}
7909
7910void ASTReader::FinishedDeserializing() {
7911 assert(NumCurrentElementsDeserializing &&
7912 "FinishedDeserializing not paired with StartedDeserializing");
7913 if (NumCurrentElementsDeserializing == 1) {
7914 // We decrease NumCurrentElementsDeserializing only after pending actions
7915 // are finished, to avoid recursively re-calling finishPendingActions().
7916 finishPendingActions();
7917 }
7918 --NumCurrentElementsDeserializing;
7919
7920 if (NumCurrentElementsDeserializing == 0 &&
7921 Consumer && !PassingDeclsToConsumer) {
7922 // Guard variable to avoid recursively redoing the process of passing
7923 // decls to consumer.
7924 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
7925 true);
7926
7927 while (!InterestingDecls.empty()) {
7928 // We are not in recursive loading, so it's safe to pass the "interesting"
7929 // decls to the consumer.
7930 Decl *D = InterestingDecls.front();
7931 InterestingDecls.pop_front();
7932 PassInterestingDeclToConsumer(D);
7933 }
7934 }
7935}
7936
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +00007937void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
Rafael Espindola7b56f6c2013-10-19 16:55:03 +00007938 D = D->getMostRecentDecl();
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +00007939
7940 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
7941 SemaObj->TUScope->AddDecl(D);
7942 } else if (SemaObj->TUScope) {
7943 // Adding the decl to IdResolver may have failed because it was already in
7944 // (even though it was not added in scope). If it is already in, make sure
7945 // it gets in the scope as well.
7946 if (std::find(SemaObj->IdResolver.begin(Name),
7947 SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
7948 SemaObj->TUScope->AddDecl(D);
7949 }
7950}
7951
Guy Benyei11169dd2012-12-18 14:30:41 +00007952ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context,
7953 StringRef isysroot, bool DisableValidation,
Ben Langmuir2cb4a782014-02-05 22:21:15 +00007954 bool AllowASTWithCompilerErrors,
7955 bool AllowConfigurationMismatch,
Ben Langmuir3d4417c2014-02-07 17:31:11 +00007956 bool ValidateSystemInputs,
Ben Langmuir2cb4a782014-02-05 22:21:15 +00007957 bool UseGlobalIndex)
Guy Benyei11169dd2012-12-18 14:30:41 +00007958 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
7959 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
7960 Diags(PP.getDiagnostics()), SemaObj(0), PP(PP), Context(Context),
7961 Consumer(0), ModuleMgr(PP.getFileManager()),
7962 isysroot(isysroot), DisableValidation(DisableValidation),
Douglas Gregor00a50f72013-01-25 00:38:33 +00007963 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
Ben Langmuir2cb4a782014-02-05 22:21:15 +00007964 AllowConfigurationMismatch(AllowConfigurationMismatch),
Ben Langmuir3d4417c2014-02-07 17:31:11 +00007965 ValidateSystemInputs(ValidateSystemInputs),
Douglas Gregorc1bbec82013-01-25 00:45:27 +00007966 UseGlobalIndex(UseGlobalIndex), TriedLoadingGlobalIndex(false),
Guy Benyei11169dd2012-12-18 14:30:41 +00007967 CurrentGeneration(0), CurrSwitchCaseStmts(&SwitchCaseStmts),
7968 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Douglas Gregor00a50f72013-01-25 00:38:33 +00007969 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
7970 TotalNumMacros(0), NumIdentifierLookups(0), NumIdentifierLookupHits(0),
7971 NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
Douglas Gregorad2f7a52013-01-28 17:54:36 +00007972 NumMethodPoolLookups(0), NumMethodPoolHits(0),
7973 NumMethodPoolTableLookups(0), NumMethodPoolTableHits(0),
7974 TotalNumMethodPoolEntries(0),
Guy Benyei11169dd2012-12-18 14:30:41 +00007975 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
7976 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
7977 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
7978 PassingDeclsToConsumer(false),
Richard Smith629ff362013-07-31 00:26:46 +00007979 NumCXXBaseSpecifiersLoaded(0), ReadingKind(Read_None)
Guy Benyei11169dd2012-12-18 14:30:41 +00007980{
7981 SourceMgr.setExternalSLocEntrySource(this);
7982}
7983
7984ASTReader::~ASTReader() {
7985 for (DeclContextVisibleUpdatesPending::iterator
7986 I = PendingVisibleUpdates.begin(),
7987 E = PendingVisibleUpdates.end();
7988 I != E; ++I) {
7989 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
7990 F = I->second.end();
7991 J != F; ++J)
7992 delete J->first;
7993 }
7994}