blob: 660e6616981e9389702437253c6060ea09fba951 [file] [log] [blame]
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001//===--- ASTReader.cpp - AST File Reader ------------------------*- C++ -*-===//
2//
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"
26#include "clang/Basic/FileSystemStatCache.h"
27#include "clang/Basic/OnDiskHashTable.h"
28#include "clang/Basic/SourceManager.h"
29#include "clang/Basic/SourceManagerInternals.h"
30#include "clang/Basic/TargetInfo.h"
31#include "clang/Basic/TargetOptions.h"
32#include "clang/Basic/Version.h"
33#include "clang/Basic/VersionTuple.h"
34#include "clang/Lex/HeaderSearch.h"
35#include "clang/Lex/HeaderSearchOptions.h"
36#include "clang/Lex/MacroInfo.h"
37#include "clang/Lex/PreprocessingRecord.h"
38#include "clang/Lex/Preprocessor.h"
39#include "clang/Lex/PreprocessorOptions.h"
40#include "clang/Sema/Scope.h"
41#include "clang/Sema/Sema.h"
42#include "clang/Serialization/ASTDeserializationListener.h"
43#include "clang/Serialization/ModuleManager.h"
44#include "clang/Serialization/SerializationDiagnostic.h"
45#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"
52#include "llvm/Support/system_error.h"
53#include <algorithm>
Guy Benyei7f92f2d2012-12-18 14:30:41 +000054#include <iterator>
55
56using namespace clang;
57using namespace clang::serialization;
58using namespace clang::serialization::reader;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +000059using llvm::BitstreamCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +000060
61//===----------------------------------------------------------------------===//
62// PCH validator implementation
63//===----------------------------------------------------------------------===//
64
65ASTReaderListener::~ASTReaderListener() {}
66
67/// \brief Compare the given set of language options against an existing set of
68/// language options.
69///
70/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
71///
72/// \returns true if the languagae options mis-match, false otherwise.
73static bool checkLanguageOptions(const LangOptions &LangOpts,
74 const LangOptions &ExistingLangOpts,
75 DiagnosticsEngine *Diags) {
76#define LANGOPT(Name, Bits, Default, Description) \
77 if (ExistingLangOpts.Name != LangOpts.Name) { \
78 if (Diags) \
79 Diags->Report(diag::err_pch_langopt_mismatch) \
80 << Description << LangOpts.Name << ExistingLangOpts.Name; \
81 return true; \
82 }
83
84#define VALUE_LANGOPT(Name, Bits, Default, Description) \
85 if (ExistingLangOpts.Name != LangOpts.Name) { \
86 if (Diags) \
87 Diags->Report(diag::err_pch_langopt_value_mismatch) \
88 << Description; \
89 return true; \
90 }
91
92#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
93 if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \
94 if (Diags) \
95 Diags->Report(diag::err_pch_langopt_value_mismatch) \
96 << Description; \
97 return true; \
98 }
99
100#define BENIGN_LANGOPT(Name, Bits, Default, Description)
101#define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
102#include "clang/Basic/LangOptions.def"
103
104 if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
105 if (Diags)
106 Diags->Report(diag::err_pch_langopt_value_mismatch)
107 << "target Objective-C runtime";
108 return true;
109 }
110
111 return false;
112}
113
114/// \brief Compare the given set of target options against an existing set of
115/// target options.
116///
117/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
118///
119/// \returns true if the target options mis-match, false otherwise.
120static bool checkTargetOptions(const TargetOptions &TargetOpts,
121 const TargetOptions &ExistingTargetOpts,
122 DiagnosticsEngine *Diags) {
123#define CHECK_TARGET_OPT(Field, Name) \
124 if (TargetOpts.Field != ExistingTargetOpts.Field) { \
125 if (Diags) \
126 Diags->Report(diag::err_pch_targetopt_mismatch) \
127 << Name << TargetOpts.Field << ExistingTargetOpts.Field; \
128 return true; \
129 }
130
131 CHECK_TARGET_OPT(Triple, "target");
132 CHECK_TARGET_OPT(CPU, "target CPU");
133 CHECK_TARGET_OPT(ABI, "target ABI");
134 CHECK_TARGET_OPT(CXXABI, "target C++ ABI");
135 CHECK_TARGET_OPT(LinkerVersion, "target linker version");
136#undef CHECK_TARGET_OPT
137
138 // Compare feature sets.
139 SmallVector<StringRef, 4> ExistingFeatures(
140 ExistingTargetOpts.FeaturesAsWritten.begin(),
141 ExistingTargetOpts.FeaturesAsWritten.end());
142 SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
143 TargetOpts.FeaturesAsWritten.end());
144 std::sort(ExistingFeatures.begin(), ExistingFeatures.end());
145 std::sort(ReadFeatures.begin(), ReadFeatures.end());
146
147 unsigned ExistingIdx = 0, ExistingN = ExistingFeatures.size();
148 unsigned ReadIdx = 0, ReadN = ReadFeatures.size();
149 while (ExistingIdx < ExistingN && ReadIdx < ReadN) {
150 if (ExistingFeatures[ExistingIdx] == ReadFeatures[ReadIdx]) {
151 ++ExistingIdx;
152 ++ReadIdx;
153 continue;
154 }
155
156 if (ReadFeatures[ReadIdx] < ExistingFeatures[ExistingIdx]) {
157 if (Diags)
158 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
159 << false << ReadFeatures[ReadIdx];
160 return true;
161 }
162
163 if (Diags)
164 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
165 << true << ExistingFeatures[ExistingIdx];
166 return true;
167 }
168
169 if (ExistingIdx < ExistingN) {
170 if (Diags)
171 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
172 << true << ExistingFeatures[ExistingIdx];
173 return true;
174 }
175
176 if (ReadIdx < ReadN) {
177 if (Diags)
178 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
179 << false << ReadFeatures[ReadIdx];
180 return true;
181 }
182
183 return false;
184}
185
186bool
187PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
188 bool Complain) {
189 const LangOptions &ExistingLangOpts = PP.getLangOpts();
190 return checkLanguageOptions(LangOpts, ExistingLangOpts,
191 Complain? &Reader.Diags : 0);
192}
193
194bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
195 bool Complain) {
196 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
197 return checkTargetOptions(TargetOpts, ExistingTargetOpts,
198 Complain? &Reader.Diags : 0);
199}
200
201namespace {
202 typedef llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> >
203 MacroDefinitionsMap;
204}
205
206/// \brief Collect the macro definitions provided by the given preprocessor
207/// options.
208static void collectMacroDefinitions(const PreprocessorOptions &PPOpts,
209 MacroDefinitionsMap &Macros,
210 SmallVectorImpl<StringRef> *MacroNames = 0){
211 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
212 StringRef Macro = PPOpts.Macros[I].first;
213 bool IsUndef = PPOpts.Macros[I].second;
214
215 std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
216 StringRef MacroName = MacroPair.first;
217 StringRef MacroBody = MacroPair.second;
218
219 // For an #undef'd macro, we only care about the name.
220 if (IsUndef) {
221 if (MacroNames && !Macros.count(MacroName))
222 MacroNames->push_back(MacroName);
223
224 Macros[MacroName] = std::make_pair("", true);
225 continue;
226 }
227
228 // For a #define'd macro, figure out the actual definition.
229 if (MacroName.size() == Macro.size())
230 MacroBody = "1";
231 else {
232 // Note: GCC drops anything following an end-of-line character.
233 StringRef::size_type End = MacroBody.find_first_of("\n\r");
234 MacroBody = MacroBody.substr(0, End);
235 }
236
237 if (MacroNames && !Macros.count(MacroName))
238 MacroNames->push_back(MacroName);
239 Macros[MacroName] = std::make_pair(MacroBody, false);
240 }
241}
242
243/// \brief Check the preprocessor options deserialized from the control block
244/// against the preprocessor options in an existing preprocessor.
245///
246/// \param Diags If non-null, produce diagnostics for any mismatches incurred.
247static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts,
248 const PreprocessorOptions &ExistingPPOpts,
249 DiagnosticsEngine *Diags,
250 FileManager &FileMgr,
251 std::string &SuggestedPredefines) {
252 // Check macro definitions.
253 MacroDefinitionsMap ASTFileMacros;
254 collectMacroDefinitions(PPOpts, ASTFileMacros);
255 MacroDefinitionsMap ExistingMacros;
256 SmallVector<StringRef, 4> ExistingMacroNames;
257 collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames);
258
259 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
260 // Dig out the macro definition in the existing preprocessor options.
261 StringRef MacroName = ExistingMacroNames[I];
262 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
263
264 // Check whether we know anything about this macro name or not.
265 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> >::iterator Known
266 = ASTFileMacros.find(MacroName);
267 if (Known == ASTFileMacros.end()) {
268 // FIXME: Check whether this identifier was referenced anywhere in the
269 // AST file. If so, we should reject the AST file. Unfortunately, this
270 // information isn't in the control block. What shall we do about it?
271
272 if (Existing.second) {
273 SuggestedPredefines += "#undef ";
274 SuggestedPredefines += MacroName.str();
275 SuggestedPredefines += '\n';
276 } else {
277 SuggestedPredefines += "#define ";
278 SuggestedPredefines += MacroName.str();
279 SuggestedPredefines += ' ';
280 SuggestedPredefines += Existing.first.str();
281 SuggestedPredefines += '\n';
282 }
283 continue;
284 }
285
286 // If the macro was defined in one but undef'd in the other, we have a
287 // conflict.
288 if (Existing.second != Known->second.second) {
289 if (Diags) {
290 Diags->Report(diag::err_pch_macro_def_undef)
291 << MacroName << Known->second.second;
292 }
293 return true;
294 }
295
296 // If the macro was #undef'd in both, or if the macro bodies are identical,
297 // it's fine.
298 if (Existing.second || Existing.first == Known->second.first)
299 continue;
300
301 // The macro bodies differ; complain.
302 if (Diags) {
303 Diags->Report(diag::err_pch_macro_def_conflict)
304 << MacroName << Known->second.first << Existing.first;
305 }
306 return true;
307 }
308
309 // Check whether we're using predefines.
310 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines) {
311 if (Diags) {
312 Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
313 }
314 return true;
315 }
316
317 // Compute the #include and #include_macros lines we need.
318 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
319 StringRef File = ExistingPPOpts.Includes[I];
320 if (File == ExistingPPOpts.ImplicitPCHInclude)
321 continue;
322
323 if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File)
324 != PPOpts.Includes.end())
325 continue;
326
327 SuggestedPredefines += "#include \"";
328 SuggestedPredefines +=
329 HeaderSearch::NormalizeDashIncludePath(File, FileMgr);
330 SuggestedPredefines += "\"\n";
331 }
332
333 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
334 StringRef File = ExistingPPOpts.MacroIncludes[I];
335 if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(),
336 File)
337 != PPOpts.MacroIncludes.end())
338 continue;
339
340 SuggestedPredefines += "#__include_macros \"";
341 SuggestedPredefines +=
342 HeaderSearch::NormalizeDashIncludePath(File, FileMgr);
343 SuggestedPredefines += "\"\n##\n";
344 }
345
346 return false;
347}
348
349bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
350 bool Complain,
351 std::string &SuggestedPredefines) {
352 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
353
354 return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
355 Complain? &Reader.Diags : 0,
356 PP.getFileManager(),
357 SuggestedPredefines);
358}
359
360void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
361 unsigned ID) {
362 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
363 ++NumHeaderInfos;
364}
365
366void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
367 PP.setCounterValue(Value);
368}
369
370//===----------------------------------------------------------------------===//
371// AST reader implementation
372//===----------------------------------------------------------------------===//
373
374void
375ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
376 DeserializationListener = Listener;
377}
378
379
380
381unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
382 return serialization::ComputeHash(Sel);
383}
384
385
386std::pair<unsigned, unsigned>
387ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
388 using namespace clang::io;
389 unsigned KeyLen = ReadUnalignedLE16(d);
390 unsigned DataLen = ReadUnalignedLE16(d);
391 return std::make_pair(KeyLen, DataLen);
392}
393
394ASTSelectorLookupTrait::internal_key_type
395ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
396 using namespace clang::io;
397 SelectorTable &SelTable = Reader.getContext().Selectors;
398 unsigned N = ReadUnalignedLE16(d);
399 IdentifierInfo *FirstII
400 = Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
401 if (N == 0)
402 return SelTable.getNullarySelector(FirstII);
403 else if (N == 1)
404 return SelTable.getUnarySelector(FirstII);
405
406 SmallVector<IdentifierInfo *, 16> Args;
407 Args.push_back(FirstII);
408 for (unsigned I = 1; I != N; ++I)
409 Args.push_back(Reader.getLocalIdentifier(F, ReadUnalignedLE32(d)));
410
411 return SelTable.getSelector(N, Args.data());
412}
413
414ASTSelectorLookupTrait::data_type
415ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
416 unsigned DataLen) {
417 using namespace clang::io;
418
419 data_type Result;
420
421 Result.ID = Reader.getGlobalSelectorID(F, ReadUnalignedLE32(d));
422 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
423 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
424
425 // Load instance methods
426 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
427 if (ObjCMethodDecl *Method
428 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d)))
429 Result.Instance.push_back(Method);
430 }
431
432 // Load factory methods
433 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
434 if (ObjCMethodDecl *Method
435 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d)))
436 Result.Factory.push_back(Method);
437 }
438
439 return Result;
440}
441
442unsigned ASTIdentifierLookupTrait::ComputeHash(const internal_key_type& a) {
443 return llvm::HashString(StringRef(a.first, a.second));
444}
445
446std::pair<unsigned, unsigned>
447ASTIdentifierLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
448 using namespace clang::io;
449 unsigned DataLen = ReadUnalignedLE16(d);
450 unsigned KeyLen = ReadUnalignedLE16(d);
451 return std::make_pair(KeyLen, DataLen);
452}
453
454std::pair<const char*, unsigned>
455ASTIdentifierLookupTrait::ReadKey(const unsigned char* d, unsigned n) {
456 assert(n >= 2 && d[n-1] == '\0');
457 return std::make_pair((const char*) d, n-1);
458}
459
460IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
461 const unsigned char* d,
462 unsigned DataLen) {
463 using namespace clang::io;
464 unsigned RawID = ReadUnalignedLE32(d);
465 bool IsInteresting = RawID & 0x01;
466
467 // Wipe out the "is interesting" bit.
468 RawID = RawID >> 1;
469
470 IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
471 if (!IsInteresting) {
472 // For uninteresting identifiers, just build the IdentifierInfo
473 // and associate it with the persistent ID.
474 IdentifierInfo *II = KnownII;
475 if (!II) {
476 II = &Reader.getIdentifierTable().getOwn(StringRef(k.first, k.second));
477 KnownII = II;
478 }
479 Reader.SetIdentifierInfo(ID, II);
480 II->setIsFromAST();
481 Reader.markIdentifierUpToDate(II);
482 return II;
483 }
484
485 unsigned ObjCOrBuiltinID = ReadUnalignedLE16(d);
486 unsigned Bits = ReadUnalignedLE16(d);
487 bool CPlusPlusOperatorKeyword = Bits & 0x01;
488 Bits >>= 1;
489 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
490 Bits >>= 1;
491 bool Poisoned = Bits & 0x01;
492 Bits >>= 1;
493 bool ExtensionToken = Bits & 0x01;
494 Bits >>= 1;
495 bool hadMacroDefinition = Bits & 0x01;
496 Bits >>= 1;
497
498 assert(Bits == 0 && "Extra bits in the identifier?");
499 DataLen -= 8;
500
501 // Build the IdentifierInfo itself and link the identifier ID with
502 // the new IdentifierInfo.
503 IdentifierInfo *II = KnownII;
504 if (!II) {
505 II = &Reader.getIdentifierTable().getOwn(StringRef(k.first, k.second));
506 KnownII = II;
507 }
508 Reader.markIdentifierUpToDate(II);
509 II->setIsFromAST();
510
511 // Set or check the various bits in the IdentifierInfo structure.
512 // Token IDs are read-only.
513 if (HasRevertedTokenIDToIdentifier)
514 II->RevertTokenIDToIdentifier();
515 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
516 assert(II->isExtensionToken() == ExtensionToken &&
517 "Incorrect extension token flag");
518 (void)ExtensionToken;
519 if (Poisoned)
520 II->setIsPoisoned(true);
521 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
522 "Incorrect C++ operator keyword flag");
523 (void)CPlusPlusOperatorKeyword;
524
525 // If this identifier is a macro, deserialize the macro
526 // definition.
527 if (hadMacroDefinition) {
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +0000528 SmallVector<MacroID, 4> MacroIDs;
529 while (uint32_t LocalID = ReadUnalignedLE32(d)) {
530 MacroIDs.push_back(Reader.getGlobalMacroID(F, LocalID));
531 DataLen -= 4;
532 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000533 DataLen -= 4;
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +0000534 Reader.setIdentifierIsMacro(II, MacroIDs);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000535 }
536
537 Reader.SetIdentifierInfo(ID, II);
538
539 // Read all of the declarations visible at global scope with this
540 // name.
541 if (DataLen > 0) {
542 SmallVector<uint32_t, 4> DeclIDs;
543 for (; DataLen > 0; DataLen -= 4)
544 DeclIDs.push_back(Reader.getGlobalDeclID(F, ReadUnalignedLE32(d)));
545 Reader.SetGloballyVisibleDecls(II, DeclIDs);
546 }
547
548 return II;
549}
550
551unsigned
552ASTDeclContextNameLookupTrait::ComputeHash(const DeclNameKey &Key) const {
553 llvm::FoldingSetNodeID ID;
554 ID.AddInteger(Key.Kind);
555
556 switch (Key.Kind) {
557 case DeclarationName::Identifier:
558 case DeclarationName::CXXLiteralOperatorName:
559 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
560 break;
561 case DeclarationName::ObjCZeroArgSelector:
562 case DeclarationName::ObjCOneArgSelector:
563 case DeclarationName::ObjCMultiArgSelector:
564 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
565 break;
566 case DeclarationName::CXXOperatorName:
567 ID.AddInteger((OverloadedOperatorKind)Key.Data);
568 break;
569 case DeclarationName::CXXConstructorName:
570 case DeclarationName::CXXDestructorName:
571 case DeclarationName::CXXConversionFunctionName:
572 case DeclarationName::CXXUsingDirective:
573 break;
574 }
575
576 return ID.ComputeHash();
577}
578
579ASTDeclContextNameLookupTrait::internal_key_type
580ASTDeclContextNameLookupTrait::GetInternalKey(
581 const external_key_type& Name) const {
582 DeclNameKey Key;
583 Key.Kind = Name.getNameKind();
584 switch (Name.getNameKind()) {
585 case DeclarationName::Identifier:
586 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
587 break;
588 case DeclarationName::ObjCZeroArgSelector:
589 case DeclarationName::ObjCOneArgSelector:
590 case DeclarationName::ObjCMultiArgSelector:
591 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
592 break;
593 case DeclarationName::CXXOperatorName:
594 Key.Data = Name.getCXXOverloadedOperator();
595 break;
596 case DeclarationName::CXXLiteralOperatorName:
597 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
598 break;
599 case DeclarationName::CXXConstructorName:
600 case DeclarationName::CXXDestructorName:
601 case DeclarationName::CXXConversionFunctionName:
602 case DeclarationName::CXXUsingDirective:
603 Key.Data = 0;
604 break;
605 }
606
607 return Key;
608}
609
610std::pair<unsigned, unsigned>
611ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
612 using namespace clang::io;
613 unsigned KeyLen = ReadUnalignedLE16(d);
614 unsigned DataLen = ReadUnalignedLE16(d);
615 return std::make_pair(KeyLen, DataLen);
616}
617
618ASTDeclContextNameLookupTrait::internal_key_type
619ASTDeclContextNameLookupTrait::ReadKey(const unsigned char* d, unsigned) {
620 using namespace clang::io;
621
622 DeclNameKey Key;
623 Key.Kind = (DeclarationName::NameKind)*d++;
624 switch (Key.Kind) {
625 case DeclarationName::Identifier:
626 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
627 break;
628 case DeclarationName::ObjCZeroArgSelector:
629 case DeclarationName::ObjCOneArgSelector:
630 case DeclarationName::ObjCMultiArgSelector:
631 Key.Data =
632 (uint64_t)Reader.getLocalSelector(F, ReadUnalignedLE32(d))
633 .getAsOpaquePtr();
634 break;
635 case DeclarationName::CXXOperatorName:
636 Key.Data = *d++; // OverloadedOperatorKind
637 break;
638 case DeclarationName::CXXLiteralOperatorName:
639 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
640 break;
641 case DeclarationName::CXXConstructorName:
642 case DeclarationName::CXXDestructorName:
643 case DeclarationName::CXXConversionFunctionName:
644 case DeclarationName::CXXUsingDirective:
645 Key.Data = 0;
646 break;
647 }
648
649 return Key;
650}
651
652ASTDeclContextNameLookupTrait::data_type
653ASTDeclContextNameLookupTrait::ReadData(internal_key_type,
654 const unsigned char* d,
655 unsigned DataLen) {
656 using namespace clang::io;
657 unsigned NumDecls = ReadUnalignedLE16(d);
Argyrios Kyrtzidise8b61cf2013-01-11 22:29:49 +0000658 LE32DeclID *Start = reinterpret_cast<LE32DeclID *>(
659 const_cast<unsigned char *>(d));
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000660 return std::make_pair(Start, Start + NumDecls);
661}
662
663bool ASTReader::ReadDeclContextStorage(ModuleFile &M,
Chris Lattner8f9a1eb2013-01-20 00:56:42 +0000664 BitstreamCursor &Cursor,
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000665 const std::pair<uint64_t, uint64_t> &Offsets,
666 DeclContextInfo &Info) {
667 SavedStreamPosition SavedPosition(Cursor);
668 // First the lexical decls.
669 if (Offsets.first != 0) {
670 Cursor.JumpToBit(Offsets.first);
671
672 RecordData Record;
673 const char *Blob;
674 unsigned BlobLen;
675 unsigned Code = Cursor.ReadCode();
Chris Lattner99a5af02013-01-20 00:00:22 +0000676 unsigned RecCode = Cursor.ReadRecord(Code, Record, Blob, BlobLen);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000677 if (RecCode != DECL_CONTEXT_LEXICAL) {
678 Error("Expected lexical block");
679 return true;
680 }
681
682 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob);
683 Info.NumLexicalDecls = BlobLen / sizeof(KindDeclIDPair);
684 }
685
686 // Now the lookup table.
687 if (Offsets.second != 0) {
688 Cursor.JumpToBit(Offsets.second);
689
690 RecordData Record;
691 const char *Blob;
692 unsigned BlobLen;
693 unsigned Code = Cursor.ReadCode();
Chris Lattner99a5af02013-01-20 00:00:22 +0000694 unsigned RecCode = Cursor.ReadRecord(Code, Record, Blob, BlobLen);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000695 if (RecCode != DECL_CONTEXT_VISIBLE) {
696 Error("Expected visible lookup table block");
697 return true;
698 }
699 Info.NameLookupTableData
700 = ASTDeclContextNameLookupTable::Create(
701 (const unsigned char *)Blob + Record[0],
702 (const unsigned char *)Blob,
703 ASTDeclContextNameLookupTrait(*this, M));
704 }
705
706 return false;
707}
708
709void ASTReader::Error(StringRef Msg) {
710 Error(diag::err_fe_pch_malformed, Msg);
711}
712
713void ASTReader::Error(unsigned DiagID,
714 StringRef Arg1, StringRef Arg2) {
715 if (Diags.isDiagnosticInFlight())
716 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
717 else
718 Diag(DiagID) << Arg1 << Arg2;
719}
720
721//===----------------------------------------------------------------------===//
722// Source Manager Deserialization
723//===----------------------------------------------------------------------===//
724
725/// \brief Read the line table in the source manager block.
726/// \returns true if there was an error.
727bool ASTReader::ParseLineTable(ModuleFile &F,
728 SmallVectorImpl<uint64_t> &Record) {
729 unsigned Idx = 0;
730 LineTableInfo &LineTable = SourceMgr.getLineTable();
731
732 // Parse the file names
733 std::map<int, int> FileIDs;
734 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
735 // Extract the file name
736 unsigned FilenameLen = Record[Idx++];
737 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
738 Idx += FilenameLen;
739 MaybeAddSystemRootToFilename(F, Filename);
740 FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
741 }
742
743 // Parse the line entries
744 std::vector<LineEntry> Entries;
745 while (Idx < Record.size()) {
746 int FID = Record[Idx++];
747 assert(FID >= 0 && "Serialized line entries for non-local file.");
748 // Remap FileID from 1-based old view.
749 FID += F.SLocEntryBaseID - 1;
750
751 // Extract the line entries
752 unsigned NumEntries = Record[Idx++];
753 assert(NumEntries && "Numentries is 00000");
754 Entries.clear();
755 Entries.reserve(NumEntries);
756 for (unsigned I = 0; I != NumEntries; ++I) {
757 unsigned FileOffset = Record[Idx++];
758 unsigned LineNo = Record[Idx++];
759 int FilenameID = FileIDs[Record[Idx++]];
760 SrcMgr::CharacteristicKind FileKind
761 = (SrcMgr::CharacteristicKind)Record[Idx++];
762 unsigned IncludeOffset = Record[Idx++];
763 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
764 FileKind, IncludeOffset));
765 }
766 LineTable.AddEntry(FileID::get(FID), Entries);
767 }
768
769 return false;
770}
771
772/// \brief Read a source manager block
773bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
774 using namespace SrcMgr;
775
Chris Lattner8f9a1eb2013-01-20 00:56:42 +0000776 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000777
778 // Set the source-location entry cursor to the current position in
779 // the stream. This cursor will be used to read the contents of the
780 // source manager block initially, and then lazily read
781 // source-location entries as needed.
782 SLocEntryCursor = F.Stream;
783
784 // The stream itself is going to skip over the source manager block.
785 if (F.Stream.SkipBlock()) {
786 Error("malformed block record in AST file");
787 return true;
788 }
789
790 // Enter the source manager block.
791 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
792 Error("malformed source manager block record in AST file");
793 return true;
794 }
795
796 RecordData Record;
797 while (true) {
Chris Lattner88bde502013-01-19 21:39:22 +0000798 llvm::BitstreamEntry E = SLocEntryCursor.advanceSkippingSubblocks();
799
800 switch (E.Kind) {
801 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
802 case llvm::BitstreamEntry::Error:
803 Error("malformed block record in AST file");
804 return true;
805 case llvm::BitstreamEntry::EndBlock:
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000806 return false;
Chris Lattner88bde502013-01-19 21:39:22 +0000807 case llvm::BitstreamEntry::Record:
808 // The interesting case.
809 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000810 }
Chris Lattner88bde502013-01-19 21:39:22 +0000811
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000812 // Read a record.
813 const char *BlobStart;
814 unsigned BlobLen;
815 Record.clear();
Chris Lattner88bde502013-01-19 21:39:22 +0000816 switch (SLocEntryCursor.ReadRecord(E.ID, Record, BlobStart, BlobLen)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000817 default: // Default behavior: ignore.
818 break;
819
820 case SM_SLOC_FILE_ENTRY:
821 case SM_SLOC_BUFFER_ENTRY:
822 case SM_SLOC_EXPANSION_ENTRY:
823 // Once we hit one of the source location entries, we're done.
824 return false;
825 }
826 }
827}
828
829/// \brief If a header file is not found at the path that we expect it to be
830/// and the PCH file was moved from its original location, try to resolve the
831/// file by assuming that header+PCH were moved together and the header is in
832/// the same place relative to the PCH.
833static std::string
834resolveFileRelativeToOriginalDir(const std::string &Filename,
835 const std::string &OriginalDir,
836 const std::string &CurrDir) {
837 assert(OriginalDir != CurrDir &&
838 "No point trying to resolve the file if the PCH dir didn't change");
839 using namespace llvm::sys;
840 SmallString<128> filePath(Filename);
841 fs::make_absolute(filePath);
842 assert(path::is_absolute(OriginalDir));
843 SmallString<128> currPCHPath(CurrDir);
844
845 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
846 fileDirE = path::end(path::parent_path(filePath));
847 path::const_iterator origDirI = path::begin(OriginalDir),
848 origDirE = path::end(OriginalDir);
849 // Skip the common path components from filePath and OriginalDir.
850 while (fileDirI != fileDirE && origDirI != origDirE &&
851 *fileDirI == *origDirI) {
852 ++fileDirI;
853 ++origDirI;
854 }
855 for (; origDirI != origDirE; ++origDirI)
856 path::append(currPCHPath, "..");
857 path::append(currPCHPath, fileDirI, fileDirE);
858 path::append(currPCHPath, path::filename(Filename));
859 return currPCHPath.str();
860}
861
862bool ASTReader::ReadSLocEntry(int ID) {
863 if (ID == 0)
864 return false;
865
866 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
867 Error("source location entry ID out-of-range for AST file");
868 return true;
869 }
870
871 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
872 F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]);
Chris Lattner8f9a1eb2013-01-20 00:56:42 +0000873 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000874 unsigned BaseOffset = F->SLocEntryBaseOffset;
875
876 ++NumSLocEntriesRead;
Chris Lattner88bde502013-01-19 21:39:22 +0000877 llvm::BitstreamEntry Entry = SLocEntryCursor.advance();
878 if (Entry.Kind != llvm::BitstreamEntry::Record) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000879 Error("incorrectly-formatted source location entry in AST file");
880 return true;
881 }
Chris Lattner88bde502013-01-19 21:39:22 +0000882
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000883 RecordData Record;
884 const char *BlobStart;
885 unsigned BlobLen;
Chris Lattner88bde502013-01-19 21:39:22 +0000886 switch (SLocEntryCursor.ReadRecord(Entry.ID, Record, BlobStart, BlobLen)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000887 default:
888 Error("incorrectly-formatted source location entry in AST file");
889 return true;
890
891 case SM_SLOC_FILE_ENTRY: {
892 // We will detect whether a file changed and return 'Failure' for it, but
893 // we will also try to fail gracefully by setting up the SLocEntry.
894 unsigned InputID = Record[4];
895 InputFile IF = getInputFile(*F, InputID);
896 const FileEntry *File = IF.getPointer();
897 bool OverriddenBuffer = IF.getInt();
898
899 if (!IF.getPointer())
900 return true;
901
902 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
903 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
904 // This is the module's main file.
905 IncludeLoc = getImportLocation(F);
906 }
907 SrcMgr::CharacteristicKind
908 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
909 FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
910 ID, BaseOffset + Record[0]);
911 SrcMgr::FileInfo &FileInfo =
912 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
913 FileInfo.NumCreatedFIDs = Record[5];
914 if (Record[3])
915 FileInfo.setHasLineDirectives();
916
917 const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
918 unsigned NumFileDecls = Record[7];
919 if (NumFileDecls) {
920 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
921 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
922 NumFileDecls));
923 }
924
925 const SrcMgr::ContentCache *ContentCache
926 = SourceMgr.getOrCreateContentCache(File,
927 /*isSystemFile=*/FileCharacter != SrcMgr::C_User);
928 if (OverriddenBuffer && !ContentCache->BufferOverridden &&
929 ContentCache->ContentsEntry == ContentCache->OrigEntry) {
930 unsigned Code = SLocEntryCursor.ReadCode();
931 Record.clear();
932 unsigned RecCode
Chris Lattner99a5af02013-01-20 00:00:22 +0000933 = SLocEntryCursor.ReadRecord(Code, Record, BlobStart, BlobLen);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000934
935 if (RecCode != SM_SLOC_BUFFER_BLOB) {
936 Error("AST record has invalid code");
937 return true;
938 }
939
940 llvm::MemoryBuffer *Buffer
941 = llvm::MemoryBuffer::getMemBuffer(StringRef(BlobStart, BlobLen - 1),
942 File->getName());
943 SourceMgr.overrideFileContents(File, Buffer);
944 }
945
946 break;
947 }
948
949 case SM_SLOC_BUFFER_ENTRY: {
950 const char *Name = BlobStart;
951 unsigned Offset = Record[0];
952 SrcMgr::CharacteristicKind
953 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
954 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
955 if (IncludeLoc.isInvalid() && F->Kind == MK_Module) {
956 IncludeLoc = getImportLocation(F);
957 }
958 unsigned Code = SLocEntryCursor.ReadCode();
959 Record.clear();
960 unsigned RecCode
Chris Lattner99a5af02013-01-20 00:00:22 +0000961 = SLocEntryCursor.ReadRecord(Code, Record, BlobStart, BlobLen);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000962
963 if (RecCode != SM_SLOC_BUFFER_BLOB) {
964 Error("AST record has invalid code");
965 return true;
966 }
967
968 llvm::MemoryBuffer *Buffer
969 = llvm::MemoryBuffer::getMemBuffer(StringRef(BlobStart, BlobLen - 1),
970 Name);
971 SourceMgr.createFileIDForMemBuffer(Buffer, FileCharacter, ID,
972 BaseOffset + Offset, IncludeLoc);
973 break;
974 }
975
976 case SM_SLOC_EXPANSION_ENTRY: {
977 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
978 SourceMgr.createExpansionLoc(SpellingLoc,
979 ReadSourceLocation(*F, Record[2]),
980 ReadSourceLocation(*F, Record[3]),
981 Record[4],
982 ID,
983 BaseOffset + Record[0]);
984 break;
985 }
986 }
987
988 return false;
989}
990
991std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
992 if (ID == 0)
993 return std::make_pair(SourceLocation(), "");
994
995 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
996 Error("source location entry ID out-of-range for AST file");
997 return std::make_pair(SourceLocation(), "");
998 }
999
1000 // Find which module file this entry lands in.
1001 ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
1002 if (M->Kind != MK_Module)
1003 return std::make_pair(SourceLocation(), "");
1004
1005 // FIXME: Can we map this down to a particular submodule? That would be
1006 // ideal.
1007 return std::make_pair(M->ImportLoc, llvm::sys::path::stem(M->FileName));
1008}
1009
1010/// \brief Find the location where the module F is imported.
1011SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1012 if (F->ImportLoc.isValid())
1013 return F->ImportLoc;
1014
1015 // Otherwise we have a PCH. It's considered to be "imported" at the first
1016 // location of its includer.
1017 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
1018 // Main file is the importer. We assume that it is the first entry in the
1019 // entry table. We can't ask the manager, because at the time of PCH loading
1020 // the main file entry doesn't exist yet.
1021 // The very first entry is the invalid instantiation loc, which takes up
1022 // offsets 0 and 1.
1023 return SourceLocation::getFromRawEncoding(2U);
1024 }
1025 //return F->Loaders[0]->FirstLoc;
1026 return F->ImportedBy[0]->FirstLoc;
1027}
1028
1029/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1030/// specified cursor. Read the abbreviations that are at the top of the block
1031/// and then leave the cursor pointing into the block.
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001032bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001033 if (Cursor.EnterSubBlock(BlockID)) {
1034 Error("malformed block record in AST file");
1035 return Failure;
1036 }
1037
1038 while (true) {
1039 uint64_t Offset = Cursor.GetCurrentBitNo();
1040 unsigned Code = Cursor.ReadCode();
1041
1042 // We expect all abbrevs to be at the start of the block.
1043 if (Code != llvm::bitc::DEFINE_ABBREV) {
1044 Cursor.JumpToBit(Offset);
1045 return false;
1046 }
1047 Cursor.ReadAbbrevRecord();
1048 }
1049}
1050
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00001051void ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset,
1052 MacroInfo *Hint) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001053 BitstreamCursor &Stream = F.MacroCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001054
1055 // Keep track of where we are in the stream, then jump back there
1056 // after reading this macro.
1057 SavedStreamPosition SavedPosition(Stream);
1058
1059 Stream.JumpToBit(Offset);
1060 RecordData Record;
1061 SmallVector<IdentifierInfo*, 16> MacroArgs;
1062 MacroInfo *Macro = 0;
1063
Douglas Gregord3b036e2013-01-18 04:34:14 +00001064 // RAII object to add the loaded macro information once we're done
1065 // adding tokens.
1066 struct AddLoadedMacroInfoRAII {
1067 Preprocessor &PP;
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00001068 MacroInfo *Hint;
Douglas Gregord3b036e2013-01-18 04:34:14 +00001069 MacroInfo *MI;
1070 IdentifierInfo *II;
1071
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00001072 AddLoadedMacroInfoRAII(Preprocessor &PP, MacroInfo *Hint)
1073 : PP(PP), Hint(Hint), MI(), II() { }
Douglas Gregord3b036e2013-01-18 04:34:14 +00001074 ~AddLoadedMacroInfoRAII( ) {
1075 if (MI) {
1076 // Finally, install the macro.
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00001077 PP.addLoadedMacroInfo(II, MI, Hint);
Douglas Gregord3b036e2013-01-18 04:34:14 +00001078 }
1079 }
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00001080 } AddLoadedMacroInfo(PP, Hint);
Douglas Gregord3b036e2013-01-18 04:34:14 +00001081
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001082 while (true) {
Chris Lattner99a5af02013-01-20 00:00:22 +00001083 // Advance to the next record, but if we get to the end of the block, don't
1084 // pop it (removing all the abbreviations from the cursor) since we want to
1085 // be able to reseek within the block and read entries.
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001086 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
Chris Lattner99a5af02013-01-20 00:00:22 +00001087 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks(Flags);
1088
1089 switch (Entry.Kind) {
1090 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1091 case llvm::BitstreamEntry::Error:
1092 Error("malformed block record in AST file");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001093 return;
Chris Lattner99a5af02013-01-20 00:00:22 +00001094 case llvm::BitstreamEntry::EndBlock:
1095 return;
1096 case llvm::BitstreamEntry::Record:
1097 // The interesting case.
1098 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001099 }
1100
1101 // Read a record.
1102 const char *BlobStart = 0;
1103 unsigned BlobLen = 0;
1104 Record.clear();
1105 PreprocessorRecordTypes RecType =
Chris Lattner99a5af02013-01-20 00:00:22 +00001106 (PreprocessorRecordTypes)Stream.ReadRecord(Entry.ID, Record, BlobStart,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001107 BlobLen);
1108 switch (RecType) {
1109 case PP_MACRO_OBJECT_LIKE:
1110 case PP_MACRO_FUNCTION_LIKE: {
1111 // If we already have a macro, that means that we've hit the end
1112 // of the definition of the macro we were looking for. We're
1113 // done.
1114 if (Macro)
1115 return;
1116
1117 IdentifierInfo *II = getLocalIdentifier(F, Record[0]);
1118 if (II == 0) {
1119 Error("macro must have a name in AST file");
1120 return;
1121 }
1122
1123 unsigned GlobalID = getGlobalMacroID(F, Record[1]);
1124
1125 // If this macro has already been loaded, don't do so again.
1126 if (MacrosLoaded[GlobalID - NUM_PREDEF_MACRO_IDS])
1127 return;
1128
1129 SubmoduleID GlobalSubmoduleID = getGlobalSubmoduleID(F, Record[2]);
1130 unsigned NextIndex = 3;
1131 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
1132 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
Argyrios Kyrtzidis8169b672013-01-07 19:16:23 +00001133 MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001134
1135 // Record this macro.
1136 MacrosLoaded[GlobalID - NUM_PREDEF_MACRO_IDS] = MI;
1137
1138 SourceLocation UndefLoc = ReadSourceLocation(F, Record, NextIndex);
1139 if (UndefLoc.isValid())
1140 MI->setUndefLoc(UndefLoc);
1141
1142 MI->setIsUsed(Record[NextIndex++]);
1143 MI->setIsFromAST();
1144
1145 bool IsPublic = Record[NextIndex++];
1146 MI->setVisibility(IsPublic, ReadSourceLocation(F, Record, NextIndex));
1147
1148 if (RecType == PP_MACRO_FUNCTION_LIKE) {
1149 // Decode function-like macro info.
1150 bool isC99VarArgs = Record[NextIndex++];
1151 bool isGNUVarArgs = Record[NextIndex++];
1152 bool hasCommaPasting = Record[NextIndex++];
1153 MacroArgs.clear();
1154 unsigned NumArgs = Record[NextIndex++];
1155 for (unsigned i = 0; i != NumArgs; ++i)
1156 MacroArgs.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1157
1158 // Install function-like macro info.
1159 MI->setIsFunctionLike();
1160 if (isC99VarArgs) MI->setIsC99Varargs();
1161 if (isGNUVarArgs) MI->setIsGNUVarargs();
1162 if (hasCommaPasting) MI->setHasCommaPasting();
1163 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
1164 PP.getPreprocessorAllocator());
1165 }
1166
1167 if (DeserializationListener)
1168 DeserializationListener->MacroRead(GlobalID, MI);
1169
1170 // If an update record marked this as undefined, do so now.
1171 // FIXME: Only if the submodule this update came from is visible?
1172 MacroUpdatesMap::iterator Update = MacroUpdates.find(GlobalID);
1173 if (Update != MacroUpdates.end()) {
1174 if (MI->getUndefLoc().isInvalid()) {
1175 for (unsigned I = 0, N = Update->second.size(); I != N; ++I) {
1176 bool Hidden = false;
1177 if (unsigned SubmoduleID = Update->second[I].first) {
1178 if (Module *Owner = getSubmodule(SubmoduleID)) {
1179 if (Owner->NameVisibility == Module::Hidden) {
1180 // Note that this #undef is hidden.
1181 Hidden = true;
1182
1183 // Record this hiding for later.
1184 HiddenNamesMap[Owner].push_back(
1185 HiddenName(II, MI, Update->second[I].second.UndefLoc));
1186 }
1187 }
1188 }
1189
1190 if (!Hidden) {
1191 MI->setUndefLoc(Update->second[I].second.UndefLoc);
1192 if (PPMutationListener *Listener = PP.getPPMutationListener())
1193 Listener->UndefinedMacro(MI);
1194 break;
1195 }
1196 }
1197 }
1198 MacroUpdates.erase(Update);
1199 }
1200
1201 // Determine whether this macro definition is visible.
1202 bool Hidden = !MI->isPublic();
1203 if (!Hidden && GlobalSubmoduleID) {
1204 if (Module *Owner = getSubmodule(GlobalSubmoduleID)) {
1205 if (Owner->NameVisibility == Module::Hidden) {
1206 // The owning module is not visible, and this macro definition
1207 // should not be, either.
1208 Hidden = true;
1209
1210 // Note that this macro definition was hidden because its owning
1211 // module is not yet visible.
1212 HiddenNamesMap[Owner].push_back(HiddenName(II, MI));
1213 }
1214 }
1215 }
1216 MI->setHidden(Hidden);
1217
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00001218 // Make sure we install the macro once we're done.
1219 AddLoadedMacroInfo.MI = MI;
1220 AddLoadedMacroInfo.II = II;
Douglas Gregord3b036e2013-01-18 04:34:14 +00001221
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001222 // Remember that we saw this macro last so that we add the tokens that
1223 // form its body to it.
1224 Macro = MI;
1225
1226 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1227 Record[NextIndex]) {
1228 // We have a macro definition. Register the association
1229 PreprocessedEntityID
1230 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1231 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1232 PPRec.RegisterMacroDefinition(Macro,
1233 PPRec.getPPEntityID(GlobalID-1, /*isLoaded=*/true));
1234 }
1235
1236 ++NumMacrosRead;
1237 break;
1238 }
1239
1240 case PP_TOKEN: {
1241 // If we see a TOKEN before a PP_MACRO_*, then the file is
1242 // erroneous, just pretend we didn't see this.
1243 if (Macro == 0) break;
1244
1245 Token Tok;
1246 Tok.startToken();
1247 Tok.setLocation(ReadSourceLocation(F, Record[0]));
1248 Tok.setLength(Record[1]);
1249 if (IdentifierInfo *II = getLocalIdentifier(F, Record[2]))
1250 Tok.setIdentifierInfo(II);
1251 Tok.setKind((tok::TokenKind)Record[3]);
1252 Tok.setFlag((Token::TokenFlags)Record[4]);
1253 Macro->AddTokenToBody(Tok);
1254 break;
1255 }
1256 }
1257 }
1258}
1259
1260PreprocessedEntityID
1261ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const {
1262 ContinuousRangeMap<uint32_t, int, 2>::const_iterator
1263 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1264 assert(I != M.PreprocessedEntityRemap.end()
1265 && "Invalid index into preprocessed entity index remap");
1266
1267 return LocalID + I->second;
1268}
1269
1270unsigned HeaderFileInfoTrait::ComputeHash(const char *path) {
1271 return llvm::HashString(llvm::sys::path::filename(path));
1272}
1273
1274HeaderFileInfoTrait::internal_key_type
1275HeaderFileInfoTrait::GetInternalKey(const char *path) { return path; }
1276
1277bool HeaderFileInfoTrait::EqualKey(internal_key_type a, internal_key_type b) {
1278 if (strcmp(a, b) == 0)
1279 return true;
1280
1281 if (llvm::sys::path::filename(a) != llvm::sys::path::filename(b))
1282 return false;
1283
1284 // Determine whether the actual files are equivalent.
1285 bool Result = false;
1286 if (llvm::sys::fs::equivalent(a, b, Result))
1287 return false;
1288
1289 return Result;
1290}
1291
1292std::pair<unsigned, unsigned>
1293HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1294 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1295 unsigned DataLen = (unsigned) *d++;
1296 return std::make_pair(KeyLen + 1, DataLen);
1297}
1298
1299HeaderFileInfoTrait::data_type
1300HeaderFileInfoTrait::ReadData(const internal_key_type, const unsigned char *d,
1301 unsigned DataLen) {
1302 const unsigned char *End = d + DataLen;
1303 using namespace clang::io;
1304 HeaderFileInfo HFI;
1305 unsigned Flags = *d++;
1306 HFI.isImport = (Flags >> 5) & 0x01;
1307 HFI.isPragmaOnce = (Flags >> 4) & 0x01;
1308 HFI.DirInfo = (Flags >> 2) & 0x03;
1309 HFI.Resolved = (Flags >> 1) & 0x01;
1310 HFI.IndexHeaderMapHeader = Flags & 0x01;
1311 HFI.NumIncludes = ReadUnalignedLE16(d);
1312 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(M,
1313 ReadUnalignedLE32(d));
1314 if (unsigned FrameworkOffset = ReadUnalignedLE32(d)) {
1315 // The framework offset is 1 greater than the actual offset,
1316 // since 0 is used as an indicator for "no framework name".
1317 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1318 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1319 }
1320
1321 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1322 (void)End;
1323
1324 // This HeaderFileInfo was externally loaded.
1325 HFI.External = true;
1326 return HFI;
1327}
1328
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00001329void ASTReader::setIdentifierIsMacro(IdentifierInfo *II, ArrayRef<MacroID> IDs){
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001330 II->setHadMacroDefinition(true);
1331 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00001332 PendingMacroIDs[II].append(IDs.begin(), IDs.end());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001333}
1334
1335void ASTReader::ReadDefinedMacros() {
1336 // Note that we are loading defined macros.
1337 Deserializing Macros(this);
1338
1339 for (ModuleReverseIterator I = ModuleMgr.rbegin(),
1340 E = ModuleMgr.rend(); I != E; ++I) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001341 BitstreamCursor &MacroCursor = (*I)->MacroCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001342
1343 // If there was no preprocessor block, skip this file.
1344 if (!MacroCursor.getBitStreamReader())
1345 continue;
1346
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001347 BitstreamCursor Cursor = MacroCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001348 Cursor.JumpToBit((*I)->MacroStartOffset);
1349
1350 RecordData Record;
1351 while (true) {
Chris Lattner88bde502013-01-19 21:39:22 +00001352 llvm::BitstreamEntry E = Cursor.advanceSkippingSubblocks();
1353
1354 switch (E.Kind) {
1355 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1356 case llvm::BitstreamEntry::Error:
1357 Error("malformed block record in AST file");
1358 return;
1359 case llvm::BitstreamEntry::EndBlock:
1360 goto NextCursor;
1361
1362 case llvm::BitstreamEntry::Record:
1363 const char *BlobStart;
1364 unsigned BlobLen;
1365 Record.clear();
1366 switch (Cursor.ReadRecord(E.ID, Record, BlobStart, BlobLen)) {
1367 default: // Default behavior: ignore.
1368 break;
1369
1370 case PP_MACRO_OBJECT_LIKE:
1371 case PP_MACRO_FUNCTION_LIKE:
1372 getLocalIdentifier(**I, Record[0]);
1373 break;
1374
1375 case PP_TOKEN:
1376 // Ignore tokens.
1377 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001378 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001379 break;
1380 }
1381 }
Chris Lattner88bde502013-01-19 21:39:22 +00001382 NextCursor: ;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001383 }
1384}
1385
1386namespace {
1387 /// \brief Visitor class used to look up identifirs in an AST file.
1388 class IdentifierLookupVisitor {
1389 StringRef Name;
1390 unsigned PriorGeneration;
1391 IdentifierInfo *Found;
1392 public:
1393 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration)
1394 : Name(Name), PriorGeneration(PriorGeneration), Found() { }
1395
1396 static bool visit(ModuleFile &M, void *UserData) {
1397 IdentifierLookupVisitor *This
1398 = static_cast<IdentifierLookupVisitor *>(UserData);
1399
1400 // If we've already searched this module file, skip it now.
1401 if (M.Generation <= This->PriorGeneration)
1402 return true;
1403
1404 ASTIdentifierLookupTable *IdTable
1405 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
1406 if (!IdTable)
1407 return false;
1408
1409 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(),
1410 M, This->Found);
1411
1412 std::pair<const char*, unsigned> Key(This->Name.begin(),
1413 This->Name.size());
1414 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Trait);
1415 if (Pos == IdTable->end())
1416 return false;
1417
1418 // Dereferencing the iterator has the effect of building the
1419 // IdentifierInfo node and populating it with the various
1420 // declarations it needs.
1421 This->Found = *Pos;
1422 return true;
1423 }
1424
1425 // \brief Retrieve the identifier info found within the module
1426 // files.
1427 IdentifierInfo *getIdentifierInfo() const { return Found; }
1428 };
1429}
1430
1431void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
1432 // Note that we are loading an identifier.
1433 Deserializing AnIdentifier(this);
1434
1435 unsigned PriorGeneration = 0;
1436 if (getContext().getLangOpts().Modules)
1437 PriorGeneration = IdentifierGeneration[&II];
1438
1439 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration);
1440 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
1441 markIdentifierUpToDate(&II);
1442}
1443
1444void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
1445 if (!II)
1446 return;
1447
1448 II->setOutOfDate(false);
1449
1450 // Update the generation for this identifier.
1451 if (getContext().getLangOpts().Modules)
1452 IdentifierGeneration[II] = CurrentGeneration;
1453}
1454
1455llvm::PointerIntPair<const FileEntry *, 1, bool>
1456ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
1457 // If this ID is bogus, just return an empty input file.
1458 if (ID == 0 || ID > F.InputFilesLoaded.size())
1459 return InputFile();
1460
1461 // If we've already loaded this input file, return it.
1462 if (F.InputFilesLoaded[ID-1].getPointer())
1463 return F.InputFilesLoaded[ID-1];
1464
1465 // Go find this input file.
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001466 BitstreamCursor &Cursor = F.InputFilesCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001467 SavedStreamPosition SavedPosition(Cursor);
1468 Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
1469
1470 unsigned Code = Cursor.ReadCode();
1471 RecordData Record;
1472 const char *BlobStart = 0;
1473 unsigned BlobLen = 0;
1474 switch ((InputFileRecordTypes)Cursor.ReadRecord(Code, Record,
Chris Lattner88bde502013-01-19 21:39:22 +00001475 BlobStart, BlobLen)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001476 case INPUT_FILE: {
1477 unsigned StoredID = Record[0];
1478 assert(ID == StoredID && "Bogus stored ID or offset");
1479 (void)StoredID;
1480 off_t StoredSize = (off_t)Record[1];
1481 time_t StoredTime = (time_t)Record[2];
1482 bool Overridden = (bool)Record[3];
1483
1484 // Get the file entry for this input file.
1485 StringRef OrigFilename(BlobStart, BlobLen);
1486 std::string Filename = OrigFilename;
1487 MaybeAddSystemRootToFilename(F, Filename);
1488 const FileEntry *File
1489 = Overridden? FileMgr.getVirtualFile(Filename, StoredSize, StoredTime)
1490 : FileMgr.getFile(Filename, /*OpenFile=*/false);
1491
1492 // If we didn't find the file, resolve it relative to the
1493 // original directory from which this AST file was created.
1494 if (File == 0 && !F.OriginalDir.empty() && !CurrentDir.empty() &&
1495 F.OriginalDir != CurrentDir) {
1496 std::string Resolved = resolveFileRelativeToOriginalDir(Filename,
1497 F.OriginalDir,
1498 CurrentDir);
1499 if (!Resolved.empty())
1500 File = FileMgr.getFile(Resolved);
1501 }
1502
1503 // For an overridden file, create a virtual file with the stored
1504 // size/timestamp.
1505 if (Overridden && File == 0) {
1506 File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
1507 }
1508
1509 if (File == 0) {
1510 if (Complain) {
1511 std::string ErrorStr = "could not find file '";
1512 ErrorStr += Filename;
1513 ErrorStr += "' referenced by AST file";
1514 Error(ErrorStr.c_str());
1515 }
1516 return InputFile();
1517 }
1518
1519 // Note that we've loaded this input file.
1520 F.InputFilesLoaded[ID-1] = InputFile(File, Overridden);
1521
1522 // Check if there was a request to override the contents of the file
1523 // that was part of the precompiled header. Overridding such a file
1524 // can lead to problems when lexing using the source locations from the
1525 // PCH.
1526 SourceManager &SM = getSourceManager();
1527 if (!Overridden && SM.isFileOverridden(File)) {
1528 Error(diag::err_fe_pch_file_overridden, Filename);
1529 // After emitting the diagnostic, recover by disabling the override so
1530 // that the original file will be used.
1531 SM.disableFileContentsOverride(File);
1532 // The FileEntry is a virtual file entry with the size of the contents
1533 // that would override the original contents. Set it to the original's
1534 // size/time.
1535 FileMgr.modifyFileEntry(const_cast<FileEntry*>(File),
1536 StoredSize, StoredTime);
1537 }
1538
1539 // For an overridden file, there is nothing to validate.
1540 if (Overridden)
1541 return InputFile(File, Overridden);
1542
1543 if ((StoredSize != File->getSize()
1544#if !defined(LLVM_ON_WIN32)
1545 // In our regression testing, the Windows file system seems to
1546 // have inconsistent modification times that sometimes
1547 // erroneously trigger this error-handling path.
1548 || StoredTime != File->getModificationTime()
1549#endif
1550 )) {
1551 if (Complain)
1552 Error(diag::err_fe_pch_file_modified, Filename);
1553
1554 return InputFile();
1555 }
1556
1557 return InputFile(File, Overridden);
1558 }
1559 }
1560
1561 return InputFile();
1562}
1563
1564const FileEntry *ASTReader::getFileEntry(StringRef filenameStrRef) {
1565 ModuleFile &M = ModuleMgr.getPrimaryModule();
1566 std::string Filename = filenameStrRef;
1567 MaybeAddSystemRootToFilename(M, Filename);
1568 const FileEntry *File = FileMgr.getFile(Filename);
1569 if (File == 0 && !M.OriginalDir.empty() && !CurrentDir.empty() &&
1570 M.OriginalDir != CurrentDir) {
1571 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1572 M.OriginalDir,
1573 CurrentDir);
1574 if (!resolved.empty())
1575 File = FileMgr.getFile(resolved);
1576 }
1577
1578 return File;
1579}
1580
1581/// \brief If we are loading a relocatable PCH file, and the filename is
1582/// not an absolute path, add the system root to the beginning of the file
1583/// name.
1584void ASTReader::MaybeAddSystemRootToFilename(ModuleFile &M,
1585 std::string &Filename) {
1586 // If this is not a relocatable PCH file, there's nothing to do.
1587 if (!M.RelocatablePCH)
1588 return;
1589
1590 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
1591 return;
1592
1593 if (isysroot.empty()) {
1594 // If no system root was given, default to '/'
1595 Filename.insert(Filename.begin(), '/');
1596 return;
1597 }
1598
1599 unsigned Length = isysroot.size();
1600 if (isysroot[Length - 1] != '/')
1601 Filename.insert(Filename.begin(), '/');
1602
1603 Filename.insert(Filename.begin(), isysroot.begin(), isysroot.end());
1604}
1605
1606ASTReader::ASTReadResult
1607ASTReader::ReadControlBlock(ModuleFile &F,
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001608 SmallVectorImpl<ImportedModule> &Loaded,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001609 unsigned ClientLoadCapabilities) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001610 BitstreamCursor &Stream = F.Stream;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001611
1612 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
1613 Error("malformed block record in AST file");
1614 return Failure;
1615 }
1616
1617 // Read all of the records and blocks in the control block.
1618 RecordData Record;
Chris Lattner88bde502013-01-19 21:39:22 +00001619 while (1) {
1620 llvm::BitstreamEntry Entry = Stream.advance();
1621
1622 switch (Entry.Kind) {
1623 case llvm::BitstreamEntry::Error:
1624 Error("malformed block record in AST file");
1625 return Failure;
1626 case llvm::BitstreamEntry::EndBlock:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001627 // Validate all of the input files.
1628 if (!DisableValidation) {
1629 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
1630 for (unsigned I = 0, N = Record[0]; I < N; ++I)
1631 if (!getInputFile(F, I+1, Complain).getPointer())
1632 return OutOfDate;
1633 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001634 return Success;
Chris Lattner88bde502013-01-19 21:39:22 +00001635
1636 case llvm::BitstreamEntry::SubBlock:
1637 switch (Entry.ID) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001638 case INPUT_FILES_BLOCK_ID:
1639 F.InputFilesCursor = Stream;
1640 if (Stream.SkipBlock() || // Skip with the main cursor
1641 // Read the abbreviations
1642 ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
1643 Error("malformed block record in AST file");
1644 return Failure;
1645 }
1646 continue;
Chris Lattner88bde502013-01-19 21:39:22 +00001647
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001648 default:
Chris Lattner88bde502013-01-19 21:39:22 +00001649 if (Stream.SkipBlock()) {
1650 Error("malformed block record in AST file");
1651 return Failure;
1652 }
1653 continue;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001654 }
Chris Lattner88bde502013-01-19 21:39:22 +00001655
1656 case llvm::BitstreamEntry::Record:
1657 // The interesting case.
1658 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001659 }
1660
1661 // Read and process a record.
1662 Record.clear();
1663 const char *BlobStart = 0;
1664 unsigned BlobLen = 0;
Chris Lattner88bde502013-01-19 21:39:22 +00001665 switch ((ControlRecordTypes)Stream.ReadRecord(Entry.ID, Record,
1666 BlobStart, BlobLen)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001667 case METADATA: {
1668 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1669 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
1670 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
1671 : diag::warn_pch_version_too_new);
1672 return VersionMismatch;
1673 }
1674
1675 bool hasErrors = Record[5];
1676 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
1677 Diag(diag::err_pch_with_compiler_errors);
1678 return HadErrors;
1679 }
1680
1681 F.RelocatablePCH = Record[4];
1682
1683 const std::string &CurBranch = getClangFullRepositoryVersion();
1684 StringRef ASTBranch(BlobStart, BlobLen);
1685 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
1686 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
1687 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
1688 return VersionMismatch;
1689 }
1690 break;
1691 }
1692
1693 case IMPORTS: {
1694 // Load each of the imported PCH files.
1695 unsigned Idx = 0, N = Record.size();
1696 while (Idx < N) {
1697 // Read information about the AST file.
1698 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
1699 // The import location will be the local one for now; we will adjust
1700 // all import locations of module imports after the global source
1701 // location info are setup.
1702 SourceLocation ImportLoc =
1703 SourceLocation::getFromRawEncoding(Record[Idx++]);
1704 unsigned Length = Record[Idx++];
1705 SmallString<128> ImportedFile(Record.begin() + Idx,
1706 Record.begin() + Idx + Length);
1707 Idx += Length;
1708
1709 // Load the AST file.
1710 switch(ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F, Loaded,
1711 ClientLoadCapabilities)) {
1712 case Failure: return Failure;
1713 // If we have to ignore the dependency, we'll have to ignore this too.
1714 case OutOfDate: return OutOfDate;
1715 case VersionMismatch: return VersionMismatch;
1716 case ConfigurationMismatch: return ConfigurationMismatch;
1717 case HadErrors: return HadErrors;
1718 case Success: break;
1719 }
1720 }
1721 break;
1722 }
1723
1724 case LANGUAGE_OPTIONS: {
1725 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
1726 if (Listener && &F == *ModuleMgr.begin() &&
1727 ParseLanguageOptions(Record, Complain, *Listener) &&
1728 !DisableValidation)
1729 return ConfigurationMismatch;
1730 break;
1731 }
1732
1733 case TARGET_OPTIONS: {
1734 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1735 if (Listener && &F == *ModuleMgr.begin() &&
1736 ParseTargetOptions(Record, Complain, *Listener) &&
1737 !DisableValidation)
1738 return ConfigurationMismatch;
1739 break;
1740 }
1741
1742 case DIAGNOSTIC_OPTIONS: {
1743 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1744 if (Listener && &F == *ModuleMgr.begin() &&
1745 ParseDiagnosticOptions(Record, Complain, *Listener) &&
1746 !DisableValidation)
1747 return ConfigurationMismatch;
1748 break;
1749 }
1750
1751 case FILE_SYSTEM_OPTIONS: {
1752 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1753 if (Listener && &F == *ModuleMgr.begin() &&
1754 ParseFileSystemOptions(Record, Complain, *Listener) &&
1755 !DisableValidation)
1756 return ConfigurationMismatch;
1757 break;
1758 }
1759
1760 case HEADER_SEARCH_OPTIONS: {
1761 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1762 if (Listener && &F == *ModuleMgr.begin() &&
1763 ParseHeaderSearchOptions(Record, Complain, *Listener) &&
1764 !DisableValidation)
1765 return ConfigurationMismatch;
1766 break;
1767 }
1768
1769 case PREPROCESSOR_OPTIONS: {
1770 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1771 if (Listener && &F == *ModuleMgr.begin() &&
1772 ParsePreprocessorOptions(Record, Complain, *Listener,
1773 SuggestedPredefines) &&
1774 !DisableValidation)
1775 return ConfigurationMismatch;
1776 break;
1777 }
1778
1779 case ORIGINAL_FILE:
1780 F.OriginalSourceFileID = FileID::get(Record[0]);
1781 F.ActualOriginalSourceFileName.assign(BlobStart, BlobLen);
1782 F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
1783 MaybeAddSystemRootToFilename(F, F.OriginalSourceFileName);
1784 break;
1785
1786 case ORIGINAL_FILE_ID:
1787 F.OriginalSourceFileID = FileID::get(Record[0]);
1788 break;
1789
1790 case ORIGINAL_PCH_DIR:
1791 F.OriginalDir.assign(BlobStart, BlobLen);
1792 break;
1793
1794 case INPUT_FILE_OFFSETS:
1795 F.InputFileOffsets = (const uint32_t *)BlobStart;
1796 F.InputFilesLoaded.resize(Record[0]);
1797 break;
1798 }
1799 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001800}
1801
1802bool ASTReader::ReadASTBlock(ModuleFile &F) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001803 BitstreamCursor &Stream = F.Stream;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001804
1805 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
1806 Error("malformed block record in AST file");
1807 return true;
1808 }
1809
1810 // Read all of the records and blocks for the AST file.
1811 RecordData Record;
Chris Lattner88bde502013-01-19 21:39:22 +00001812 while (1) {
1813 llvm::BitstreamEntry Entry = Stream.advance();
1814
1815 switch (Entry.Kind) {
1816 case llvm::BitstreamEntry::Error:
1817 Error("error at end of module block in AST file");
1818 return true;
1819 case llvm::BitstreamEntry::EndBlock: {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001820 DeclContext *DC = Context.getTranslationUnitDecl();
1821 if (!DC->hasExternalVisibleStorage() && DC->hasExternalLexicalStorage())
1822 DC->setMustBuildLookupTable();
Chris Lattner88bde502013-01-19 21:39:22 +00001823
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001824 return false;
1825 }
Chris Lattner88bde502013-01-19 21:39:22 +00001826 case llvm::BitstreamEntry::SubBlock:
1827 switch (Entry.ID) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001828 case DECLTYPES_BLOCK_ID:
1829 // We lazily load the decls block, but we want to set up the
1830 // DeclsCursor cursor to point into it. Clone our current bitcode
1831 // cursor to it, enter the block and read the abbrevs in that block.
1832 // With the main cursor, we just skip over it.
1833 F.DeclsCursor = Stream;
1834 if (Stream.SkipBlock() || // Skip with the main cursor.
1835 // Read the abbrevs.
1836 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
1837 Error("malformed block record in AST file");
1838 return true;
1839 }
1840 break;
Chris Lattner88bde502013-01-19 21:39:22 +00001841
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001842 case DECL_UPDATES_BLOCK_ID:
1843 if (Stream.SkipBlock()) {
1844 Error("malformed block record in AST file");
1845 return true;
1846 }
1847 break;
Chris Lattner88bde502013-01-19 21:39:22 +00001848
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001849 case PREPROCESSOR_BLOCK_ID:
1850 F.MacroCursor = Stream;
1851 if (!PP.getExternalSource())
1852 PP.setExternalSource(this);
Chris Lattner88bde502013-01-19 21:39:22 +00001853
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001854 if (Stream.SkipBlock() ||
1855 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
1856 Error("malformed block record in AST file");
1857 return true;
1858 }
1859 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
1860 break;
Chris Lattner88bde502013-01-19 21:39:22 +00001861
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001862 case PREPROCESSOR_DETAIL_BLOCK_ID:
1863 F.PreprocessorDetailCursor = Stream;
1864 if (Stream.SkipBlock() ||
Chris Lattner88bde502013-01-19 21:39:22 +00001865 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001866 PREPROCESSOR_DETAIL_BLOCK_ID)) {
Chris Lattner88bde502013-01-19 21:39:22 +00001867 Error("malformed preprocessor detail record in AST file");
1868 return true;
1869 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001870 F.PreprocessorDetailStartOffset
Chris Lattner88bde502013-01-19 21:39:22 +00001871 = F.PreprocessorDetailCursor.GetCurrentBitNo();
1872
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001873 if (!PP.getPreprocessingRecord())
1874 PP.createPreprocessingRecord();
1875 if (!PP.getPreprocessingRecord()->getExternalSource())
1876 PP.getPreprocessingRecord()->SetExternalSource(*this);
1877 break;
1878
1879 case SOURCE_MANAGER_BLOCK_ID:
1880 if (ReadSourceManagerBlock(F))
1881 return true;
1882 break;
Chris Lattner88bde502013-01-19 21:39:22 +00001883
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001884 case SUBMODULE_BLOCK_ID:
1885 if (ReadSubmoduleBlock(F))
1886 return true;
1887 break;
Chris Lattner88bde502013-01-19 21:39:22 +00001888
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001889 case COMMENTS_BLOCK_ID: {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001890 BitstreamCursor C = Stream;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001891 if (Stream.SkipBlock() ||
1892 ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
1893 Error("malformed comments block in AST file");
1894 return true;
1895 }
1896 CommentsCursors.push_back(std::make_pair(C, &F));
1897 break;
1898 }
Chris Lattner88bde502013-01-19 21:39:22 +00001899
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001900 default:
Chris Lattner88bde502013-01-19 21:39:22 +00001901 if (Stream.SkipBlock()) {
1902 Error("malformed block record in AST file");
1903 return true;
1904 }
1905 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001906 }
1907 continue;
Chris Lattner88bde502013-01-19 21:39:22 +00001908
1909 case llvm::BitstreamEntry::Record:
1910 // The interesting case.
1911 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001912 }
1913
1914 // Read and process a record.
1915 Record.clear();
1916 const char *BlobStart = 0;
1917 unsigned BlobLen = 0;
Chris Lattner88bde502013-01-19 21:39:22 +00001918 switch ((ASTRecordTypes)Stream.ReadRecord(Entry.ID, Record,
1919 BlobStart, BlobLen)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001920 default: // Default behavior: ignore.
1921 break;
1922
1923 case TYPE_OFFSET: {
1924 if (F.LocalNumTypes != 0) {
1925 Error("duplicate TYPE_OFFSET record in AST file");
1926 return true;
1927 }
1928 F.TypeOffsets = (const uint32_t *)BlobStart;
1929 F.LocalNumTypes = Record[0];
1930 unsigned LocalBaseTypeIndex = Record[1];
1931 F.BaseTypeIndex = getTotalNumTypes();
1932
1933 if (F.LocalNumTypes > 0) {
1934 // Introduce the global -> local mapping for types within this module.
1935 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
1936
1937 // Introduce the local -> global mapping for types within this module.
1938 F.TypeRemap.insertOrReplace(
1939 std::make_pair(LocalBaseTypeIndex,
1940 F.BaseTypeIndex - LocalBaseTypeIndex));
1941
1942 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
1943 }
1944 break;
1945 }
1946
1947 case DECL_OFFSET: {
1948 if (F.LocalNumDecls != 0) {
1949 Error("duplicate DECL_OFFSET record in AST file");
1950 return true;
1951 }
1952 F.DeclOffsets = (const DeclOffset *)BlobStart;
1953 F.LocalNumDecls = Record[0];
1954 unsigned LocalBaseDeclID = Record[1];
1955 F.BaseDeclID = getTotalNumDecls();
1956
1957 if (F.LocalNumDecls > 0) {
1958 // Introduce the global -> local mapping for declarations within this
1959 // module.
1960 GlobalDeclMap.insert(
1961 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
1962
1963 // Introduce the local -> global mapping for declarations within this
1964 // module.
1965 F.DeclRemap.insertOrReplace(
1966 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
1967
1968 // Introduce the global -> local mapping for declarations within this
1969 // module.
1970 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
1971
1972 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
1973 }
1974 break;
1975 }
1976
1977 case TU_UPDATE_LEXICAL: {
1978 DeclContext *TU = Context.getTranslationUnitDecl();
1979 DeclContextInfo &Info = F.DeclContextInfos[TU];
1980 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(BlobStart);
1981 Info.NumLexicalDecls
1982 = static_cast<unsigned int>(BlobLen / sizeof(KindDeclIDPair));
1983 TU->setHasExternalLexicalStorage(true);
1984 break;
1985 }
1986
1987 case UPDATE_VISIBLE: {
1988 unsigned Idx = 0;
1989 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
1990 ASTDeclContextNameLookupTable *Table =
1991 ASTDeclContextNameLookupTable::Create(
1992 (const unsigned char *)BlobStart + Record[Idx++],
1993 (const unsigned char *)BlobStart,
1994 ASTDeclContextNameLookupTrait(*this, F));
1995 if (ID == PREDEF_DECL_TRANSLATION_UNIT_ID) { // Is it the TU?
1996 DeclContext *TU = Context.getTranslationUnitDecl();
1997 F.DeclContextInfos[TU].NameLookupTableData = Table;
1998 TU->setHasExternalVisibleStorage(true);
1999 } else
2000 PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F));
2001 break;
2002 }
2003
2004 case IDENTIFIER_TABLE:
2005 F.IdentifierTableData = BlobStart;
2006 if (Record[0]) {
2007 F.IdentifierLookupTable
2008 = ASTIdentifierLookupTable::Create(
2009 (const unsigned char *)F.IdentifierTableData + Record[0],
2010 (const unsigned char *)F.IdentifierTableData,
2011 ASTIdentifierLookupTrait(*this, F));
2012
2013 PP.getIdentifierTable().setExternalIdentifierLookup(this);
2014 }
2015 break;
2016
2017 case IDENTIFIER_OFFSET: {
2018 if (F.LocalNumIdentifiers != 0) {
2019 Error("duplicate IDENTIFIER_OFFSET record in AST file");
2020 return true;
2021 }
2022 F.IdentifierOffsets = (const uint32_t *)BlobStart;
2023 F.LocalNumIdentifiers = Record[0];
2024 unsigned LocalBaseIdentifierID = Record[1];
2025 F.BaseIdentifierID = getTotalNumIdentifiers();
2026
2027 if (F.LocalNumIdentifiers > 0) {
2028 // Introduce the global -> local mapping for identifiers within this
2029 // module.
2030 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
2031 &F));
2032
2033 // Introduce the local -> global mapping for identifiers within this
2034 // module.
2035 F.IdentifierRemap.insertOrReplace(
2036 std::make_pair(LocalBaseIdentifierID,
2037 F.BaseIdentifierID - LocalBaseIdentifierID));
2038
2039 IdentifiersLoaded.resize(IdentifiersLoaded.size()
2040 + F.LocalNumIdentifiers);
2041 }
2042 break;
2043 }
2044
2045 case EXTERNAL_DEFINITIONS:
2046 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2047 ExternalDefinitions.push_back(getGlobalDeclID(F, Record[I]));
2048 break;
2049
2050 case SPECIAL_TYPES:
2051 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2052 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
2053 break;
2054
2055 case STATISTICS:
2056 TotalNumStatements += Record[0];
2057 TotalNumMacros += Record[1];
2058 TotalLexicalDeclContexts += Record[2];
2059 TotalVisibleDeclContexts += Record[3];
2060 break;
2061
2062 case UNUSED_FILESCOPED_DECLS:
2063 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2064 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
2065 break;
2066
2067 case DELEGATING_CTORS:
2068 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2069 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
2070 break;
2071
2072 case WEAK_UNDECLARED_IDENTIFIERS:
2073 if (Record.size() % 4 != 0) {
2074 Error("invalid weak identifiers record");
2075 return true;
2076 }
2077
2078 // FIXME: Ignore weak undeclared identifiers from non-original PCH
2079 // files. This isn't the way to do it :)
2080 WeakUndeclaredIdentifiers.clear();
2081
2082 // Translate the weak, undeclared identifiers into global IDs.
2083 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
2084 WeakUndeclaredIdentifiers.push_back(
2085 getGlobalIdentifierID(F, Record[I++]));
2086 WeakUndeclaredIdentifiers.push_back(
2087 getGlobalIdentifierID(F, Record[I++]));
2088 WeakUndeclaredIdentifiers.push_back(
2089 ReadSourceLocation(F, Record, I).getRawEncoding());
2090 WeakUndeclaredIdentifiers.push_back(Record[I++]);
2091 }
2092 break;
2093
Richard Smith5ea6ef42013-01-10 23:43:47 +00002094 case LOCALLY_SCOPED_EXTERN_C_DECLS:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002095 for (unsigned I = 0, N = Record.size(); I != N; ++I)
Richard Smith5ea6ef42013-01-10 23:43:47 +00002096 LocallyScopedExternCDecls.push_back(getGlobalDeclID(F, Record[I]));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002097 break;
2098
2099 case SELECTOR_OFFSETS: {
2100 F.SelectorOffsets = (const uint32_t *)BlobStart;
2101 F.LocalNumSelectors = Record[0];
2102 unsigned LocalBaseSelectorID = Record[1];
2103 F.BaseSelectorID = getTotalNumSelectors();
2104
2105 if (F.LocalNumSelectors > 0) {
2106 // Introduce the global -> local mapping for selectors within this
2107 // module.
2108 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
2109
2110 // Introduce the local -> global mapping for selectors within this
2111 // module.
2112 F.SelectorRemap.insertOrReplace(
2113 std::make_pair(LocalBaseSelectorID,
2114 F.BaseSelectorID - LocalBaseSelectorID));
2115
2116 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
2117 }
2118 break;
2119 }
2120
2121 case METHOD_POOL:
2122 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
2123 if (Record[0])
2124 F.SelectorLookupTable
2125 = ASTSelectorLookupTable::Create(
2126 F.SelectorLookupTableData + Record[0],
2127 F.SelectorLookupTableData,
2128 ASTSelectorLookupTrait(*this, F));
2129 TotalNumMethodPoolEntries += Record[1];
2130 break;
2131
2132 case REFERENCED_SELECTOR_POOL:
2133 if (!Record.empty()) {
2134 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
2135 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
2136 Record[Idx++]));
2137 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
2138 getRawEncoding());
2139 }
2140 }
2141 break;
2142
2143 case PP_COUNTER_VALUE:
2144 if (!Record.empty() && Listener)
2145 Listener->ReadCounter(F, Record[0]);
2146 break;
2147
2148 case FILE_SORTED_DECLS:
2149 F.FileSortedDecls = (const DeclID *)BlobStart;
2150 F.NumFileSortedDecls = Record[0];
2151 break;
2152
2153 case SOURCE_LOCATION_OFFSETS: {
2154 F.SLocEntryOffsets = (const uint32_t *)BlobStart;
2155 F.LocalNumSLocEntries = Record[0];
2156 unsigned SLocSpaceSize = Record[1];
2157 llvm::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
2158 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
2159 SLocSpaceSize);
2160 // Make our entry in the range map. BaseID is negative and growing, so
2161 // we invert it. Because we invert it, though, we need the other end of
2162 // the range.
2163 unsigned RangeStart =
2164 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
2165 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
2166 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
2167
2168 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
2169 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
2170 GlobalSLocOffsetMap.insert(
2171 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
2172 - SLocSpaceSize,&F));
2173
2174 // Initialize the remapping table.
2175 // Invalid stays invalid.
2176 F.SLocRemap.insert(std::make_pair(0U, 0));
2177 // This module. Base was 2 when being compiled.
2178 F.SLocRemap.insert(std::make_pair(2U,
2179 static_cast<int>(F.SLocEntryBaseOffset - 2)));
2180
2181 TotalNumSLocEntries += F.LocalNumSLocEntries;
2182 break;
2183 }
2184
2185 case MODULE_OFFSET_MAP: {
2186 // Additional remapping information.
2187 const unsigned char *Data = (const unsigned char*)BlobStart;
2188 const unsigned char *DataEnd = Data + BlobLen;
2189
2190 // Continuous range maps we may be updating in our module.
2191 ContinuousRangeMap<uint32_t, int, 2>::Builder SLocRemap(F.SLocRemap);
2192 ContinuousRangeMap<uint32_t, int, 2>::Builder
2193 IdentifierRemap(F.IdentifierRemap);
2194 ContinuousRangeMap<uint32_t, int, 2>::Builder
2195 MacroRemap(F.MacroRemap);
2196 ContinuousRangeMap<uint32_t, int, 2>::Builder
2197 PreprocessedEntityRemap(F.PreprocessedEntityRemap);
2198 ContinuousRangeMap<uint32_t, int, 2>::Builder
2199 SubmoduleRemap(F.SubmoduleRemap);
2200 ContinuousRangeMap<uint32_t, int, 2>::Builder
2201 SelectorRemap(F.SelectorRemap);
2202 ContinuousRangeMap<uint32_t, int, 2>::Builder DeclRemap(F.DeclRemap);
2203 ContinuousRangeMap<uint32_t, int, 2>::Builder TypeRemap(F.TypeRemap);
2204
2205 while(Data < DataEnd) {
2206 uint16_t Len = io::ReadUnalignedLE16(Data);
2207 StringRef Name = StringRef((const char*)Data, Len);
2208 Data += Len;
2209 ModuleFile *OM = ModuleMgr.lookup(Name);
2210 if (!OM) {
2211 Error("SourceLocation remap refers to unknown module");
2212 return true;
2213 }
2214
2215 uint32_t SLocOffset = io::ReadUnalignedLE32(Data);
2216 uint32_t IdentifierIDOffset = io::ReadUnalignedLE32(Data);
2217 uint32_t MacroIDOffset = io::ReadUnalignedLE32(Data);
2218 uint32_t PreprocessedEntityIDOffset = io::ReadUnalignedLE32(Data);
2219 uint32_t SubmoduleIDOffset = io::ReadUnalignedLE32(Data);
2220 uint32_t SelectorIDOffset = io::ReadUnalignedLE32(Data);
2221 uint32_t DeclIDOffset = io::ReadUnalignedLE32(Data);
2222 uint32_t TypeIndexOffset = io::ReadUnalignedLE32(Data);
2223
2224 // Source location offset is mapped to OM->SLocEntryBaseOffset.
2225 SLocRemap.insert(std::make_pair(SLocOffset,
2226 static_cast<int>(OM->SLocEntryBaseOffset - SLocOffset)));
2227 IdentifierRemap.insert(
2228 std::make_pair(IdentifierIDOffset,
2229 OM->BaseIdentifierID - IdentifierIDOffset));
2230 MacroRemap.insert(std::make_pair(MacroIDOffset,
2231 OM->BaseMacroID - MacroIDOffset));
2232 PreprocessedEntityRemap.insert(
2233 std::make_pair(PreprocessedEntityIDOffset,
2234 OM->BasePreprocessedEntityID - PreprocessedEntityIDOffset));
2235 SubmoduleRemap.insert(std::make_pair(SubmoduleIDOffset,
2236 OM->BaseSubmoduleID - SubmoduleIDOffset));
2237 SelectorRemap.insert(std::make_pair(SelectorIDOffset,
2238 OM->BaseSelectorID - SelectorIDOffset));
2239 DeclRemap.insert(std::make_pair(DeclIDOffset,
2240 OM->BaseDeclID - DeclIDOffset));
2241
2242 TypeRemap.insert(std::make_pair(TypeIndexOffset,
2243 OM->BaseTypeIndex - TypeIndexOffset));
2244
2245 // Global -> local mappings.
2246 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
2247 }
2248 break;
2249 }
2250
2251 case SOURCE_MANAGER_LINE_TABLE:
2252 if (ParseLineTable(F, Record))
2253 return true;
2254 break;
2255
2256 case SOURCE_LOCATION_PRELOADS: {
2257 // Need to transform from the local view (1-based IDs) to the global view,
2258 // which is based off F.SLocEntryBaseID.
2259 if (!F.PreloadSLocEntries.empty()) {
2260 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
2261 return true;
2262 }
2263
2264 F.PreloadSLocEntries.swap(Record);
2265 break;
2266 }
2267
2268 case EXT_VECTOR_DECLS:
2269 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2270 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
2271 break;
2272
2273 case VTABLE_USES:
2274 if (Record.size() % 3 != 0) {
2275 Error("Invalid VTABLE_USES record");
2276 return true;
2277 }
2278
2279 // Later tables overwrite earlier ones.
2280 // FIXME: Modules will have some trouble with this. This is clearly not
2281 // the right way to do this.
2282 VTableUses.clear();
2283
2284 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
2285 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
2286 VTableUses.push_back(
2287 ReadSourceLocation(F, Record, Idx).getRawEncoding());
2288 VTableUses.push_back(Record[Idx++]);
2289 }
2290 break;
2291
2292 case DYNAMIC_CLASSES:
2293 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2294 DynamicClasses.push_back(getGlobalDeclID(F, Record[I]));
2295 break;
2296
2297 case PENDING_IMPLICIT_INSTANTIATIONS:
2298 if (PendingInstantiations.size() % 2 != 0) {
2299 Error("Invalid existing PendingInstantiations");
2300 return true;
2301 }
2302
2303 if (Record.size() % 2 != 0) {
2304 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
2305 return true;
2306 }
2307
2308 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2309 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
2310 PendingInstantiations.push_back(
2311 ReadSourceLocation(F, Record, I).getRawEncoding());
2312 }
2313 break;
2314
2315 case SEMA_DECL_REFS:
2316 // Later tables overwrite earlier ones.
2317 // FIXME: Modules will have some trouble with this.
2318 SemaDeclRefs.clear();
2319 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2320 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
2321 break;
2322
2323 case PPD_ENTITIES_OFFSETS: {
2324 F.PreprocessedEntityOffsets = (const PPEntityOffset *)BlobStart;
2325 assert(BlobLen % sizeof(PPEntityOffset) == 0);
2326 F.NumPreprocessedEntities = BlobLen / sizeof(PPEntityOffset);
2327
2328 unsigned LocalBasePreprocessedEntityID = Record[0];
2329
2330 unsigned StartingID;
2331 if (!PP.getPreprocessingRecord())
2332 PP.createPreprocessingRecord();
2333 if (!PP.getPreprocessingRecord()->getExternalSource())
2334 PP.getPreprocessingRecord()->SetExternalSource(*this);
2335 StartingID
2336 = PP.getPreprocessingRecord()
2337 ->allocateLoadedEntities(F.NumPreprocessedEntities);
2338 F.BasePreprocessedEntityID = StartingID;
2339
2340 if (F.NumPreprocessedEntities > 0) {
2341 // Introduce the global -> local mapping for preprocessed entities in
2342 // this module.
2343 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
2344
2345 // Introduce the local -> global mapping for preprocessed entities in
2346 // this module.
2347 F.PreprocessedEntityRemap.insertOrReplace(
2348 std::make_pair(LocalBasePreprocessedEntityID,
2349 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
2350 }
2351
2352 break;
2353 }
2354
2355 case DECL_UPDATE_OFFSETS: {
2356 if (Record.size() % 2 != 0) {
2357 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2358 return true;
2359 }
2360 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
2361 DeclUpdateOffsets[getGlobalDeclID(F, Record[I])]
2362 .push_back(std::make_pair(&F, Record[I+1]));
2363 break;
2364 }
2365
2366 case DECL_REPLACEMENTS: {
2367 if (Record.size() % 3 != 0) {
2368 Error("invalid DECL_REPLACEMENTS block in AST file");
2369 return true;
2370 }
2371 for (unsigned I = 0, N = Record.size(); I != N; I += 3)
2372 ReplacedDecls[getGlobalDeclID(F, Record[I])]
2373 = ReplacedDeclInfo(&F, Record[I+1], Record[I+2]);
2374 break;
2375 }
2376
2377 case OBJC_CATEGORIES_MAP: {
2378 if (F.LocalNumObjCCategoriesInMap != 0) {
2379 Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
2380 return true;
2381 }
2382
2383 F.LocalNumObjCCategoriesInMap = Record[0];
2384 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)BlobStart;
2385 break;
2386 }
2387
2388 case OBJC_CATEGORIES:
2389 F.ObjCCategories.swap(Record);
2390 break;
2391
2392 case CXX_BASE_SPECIFIER_OFFSETS: {
2393 if (F.LocalNumCXXBaseSpecifiers != 0) {
2394 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2395 return true;
2396 }
2397
2398 F.LocalNumCXXBaseSpecifiers = Record[0];
2399 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart;
2400 NumCXXBaseSpecifiersLoaded += F.LocalNumCXXBaseSpecifiers;
2401 break;
2402 }
2403
2404 case DIAG_PRAGMA_MAPPINGS:
2405 if (F.PragmaDiagMappings.empty())
2406 F.PragmaDiagMappings.swap(Record);
2407 else
2408 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
2409 Record.begin(), Record.end());
2410 break;
2411
2412 case CUDA_SPECIAL_DECL_REFS:
2413 // Later tables overwrite earlier ones.
2414 // FIXME: Modules will have trouble with this.
2415 CUDASpecialDeclRefs.clear();
2416 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2417 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
2418 break;
2419
2420 case HEADER_SEARCH_TABLE: {
2421 F.HeaderFileInfoTableData = BlobStart;
2422 F.LocalNumHeaderFileInfos = Record[1];
2423 F.HeaderFileFrameworkStrings = BlobStart + Record[2];
2424 if (Record[0]) {
2425 F.HeaderFileInfoTable
2426 = HeaderFileInfoLookupTable::Create(
2427 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
2428 (const unsigned char *)F.HeaderFileInfoTableData,
2429 HeaderFileInfoTrait(*this, F,
2430 &PP.getHeaderSearchInfo(),
2431 BlobStart + Record[2]));
2432
2433 PP.getHeaderSearchInfo().SetExternalSource(this);
2434 if (!PP.getHeaderSearchInfo().getExternalLookup())
2435 PP.getHeaderSearchInfo().SetExternalLookup(this);
2436 }
2437 break;
2438 }
2439
2440 case FP_PRAGMA_OPTIONS:
2441 // Later tables overwrite earlier ones.
2442 FPPragmaOptions.swap(Record);
2443 break;
2444
2445 case OPENCL_EXTENSIONS:
2446 // Later tables overwrite earlier ones.
2447 OpenCLExtensions.swap(Record);
2448 break;
2449
2450 case TENTATIVE_DEFINITIONS:
2451 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2452 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
2453 break;
2454
2455 case KNOWN_NAMESPACES:
2456 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2457 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
2458 break;
2459
2460 case IMPORTED_MODULES: {
2461 if (F.Kind != MK_Module) {
2462 // If we aren't loading a module (which has its own exports), make
2463 // all of the imported modules visible.
2464 // FIXME: Deal with macros-only imports.
2465 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2466 if (unsigned GlobalID = getGlobalSubmoduleID(F, Record[I]))
2467 ImportedModules.push_back(GlobalID);
2468 }
2469 }
2470 break;
2471 }
2472
2473 case LOCAL_REDECLARATIONS: {
2474 F.RedeclarationChains.swap(Record);
2475 break;
2476 }
2477
2478 case LOCAL_REDECLARATIONS_MAP: {
2479 if (F.LocalNumRedeclarationsInMap != 0) {
2480 Error("duplicate LOCAL_REDECLARATIONS_MAP record in AST file");
2481 return true;
2482 }
2483
2484 F.LocalNumRedeclarationsInMap = Record[0];
2485 F.RedeclarationsMap = (const LocalRedeclarationsInfo *)BlobStart;
2486 break;
2487 }
2488
2489 case MERGED_DECLARATIONS: {
2490 for (unsigned Idx = 0; Idx < Record.size(); /* increment in loop */) {
2491 GlobalDeclID CanonID = getGlobalDeclID(F, Record[Idx++]);
2492 SmallVectorImpl<GlobalDeclID> &Decls = StoredMergedDecls[CanonID];
2493 for (unsigned N = Record[Idx++]; N > 0; --N)
2494 Decls.push_back(getGlobalDeclID(F, Record[Idx++]));
2495 }
2496 break;
2497 }
2498
2499 case MACRO_OFFSET: {
2500 if (F.LocalNumMacros != 0) {
2501 Error("duplicate MACRO_OFFSET record in AST file");
2502 return true;
2503 }
2504 F.MacroOffsets = (const uint32_t *)BlobStart;
2505 F.LocalNumMacros = Record[0];
2506 unsigned LocalBaseMacroID = Record[1];
2507 F.BaseMacroID = getTotalNumMacros();
2508
2509 if (F.LocalNumMacros > 0) {
2510 // Introduce the global -> local mapping for macros within this module.
2511 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
2512
2513 // Introduce the local -> global mapping for macros within this module.
2514 F.MacroRemap.insertOrReplace(
2515 std::make_pair(LocalBaseMacroID,
2516 F.BaseMacroID - LocalBaseMacroID));
2517
2518 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
2519 }
2520 break;
2521 }
2522
2523 case MACRO_UPDATES: {
2524 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2525 MacroID ID = getGlobalMacroID(F, Record[I++]);
2526 if (I == N)
2527 break;
2528
2529 SourceLocation UndefLoc = ReadSourceLocation(F, Record, I);
2530 SubmoduleID SubmoduleID = getGlobalSubmoduleID(F, Record[I++]);;
2531 MacroUpdate Update;
2532 Update.UndefLoc = UndefLoc;
2533 MacroUpdates[ID].push_back(std::make_pair(SubmoduleID, Update));
2534 }
2535 break;
2536 }
2537 }
2538 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002539}
2540
2541void ASTReader::makeNamesVisible(const HiddenNames &Names) {
2542 for (unsigned I = 0, N = Names.size(); I != N; ++I) {
2543 switch (Names[I].getKind()) {
2544 case HiddenName::Declaration:
2545 Names[I].getDecl()->Hidden = false;
2546 break;
2547
2548 case HiddenName::MacroVisibility: {
2549 std::pair<IdentifierInfo *, MacroInfo *> Macro = Names[I].getMacro();
2550 Macro.second->setHidden(!Macro.second->isPublic());
2551 if (Macro.second->isDefined()) {
2552 PP.makeLoadedMacroInfoVisible(Macro.first, Macro.second);
2553 }
2554 break;
2555 }
2556
2557 case HiddenName::MacroUndef: {
2558 std::pair<IdentifierInfo *, MacroInfo *> Macro = Names[I].getMacro();
2559 if (Macro.second->isDefined()) {
2560 Macro.second->setUndefLoc(Names[I].getMacroUndefLoc());
2561 if (PPMutationListener *Listener = PP.getPPMutationListener())
2562 Listener->UndefinedMacro(Macro.second);
2563 PP.makeLoadedMacroInfoVisible(Macro.first, Macro.second);
2564 }
2565 break;
2566 }
2567 }
2568 }
2569}
2570
2571void ASTReader::makeModuleVisible(Module *Mod,
2572 Module::NameVisibilityKind NameVisibility) {
2573 llvm::SmallPtrSet<Module *, 4> Visited;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002574 SmallVector<Module *, 4> Stack;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002575 Stack.push_back(Mod);
2576 while (!Stack.empty()) {
2577 Mod = Stack.back();
2578 Stack.pop_back();
2579
2580 if (NameVisibility <= Mod->NameVisibility) {
2581 // This module already has this level of visibility (or greater), so
2582 // there is nothing more to do.
2583 continue;
2584 }
2585
2586 if (!Mod->isAvailable()) {
2587 // Modules that aren't available cannot be made visible.
2588 continue;
2589 }
2590
2591 // Update the module's name visibility.
2592 Mod->NameVisibility = NameVisibility;
2593
2594 // If we've already deserialized any names from this module,
2595 // mark them as visible.
2596 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
2597 if (Hidden != HiddenNamesMap.end()) {
2598 makeNamesVisible(Hidden->second);
2599 HiddenNamesMap.erase(Hidden);
2600 }
2601
2602 // Push any non-explicit submodules onto the stack to be marked as
2603 // visible.
2604 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2605 SubEnd = Mod->submodule_end();
2606 Sub != SubEnd; ++Sub) {
2607 if (!(*Sub)->IsExplicit && Visited.insert(*Sub))
2608 Stack.push_back(*Sub);
2609 }
2610
2611 // Push any exported modules onto the stack to be marked as visible.
2612 bool AnyWildcard = false;
2613 bool UnrestrictedWildcard = false;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002614 SmallVector<Module *, 4> WildcardRestrictions;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002615 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
2616 Module *Exported = Mod->Exports[I].getPointer();
2617 if (!Mod->Exports[I].getInt()) {
2618 // Export a named module directly; no wildcards involved.
2619 if (Visited.insert(Exported))
2620 Stack.push_back(Exported);
2621
2622 continue;
2623 }
2624
2625 // Wildcard export: export all of the imported modules that match
2626 // the given pattern.
2627 AnyWildcard = true;
2628 if (UnrestrictedWildcard)
2629 continue;
2630
2631 if (Module *Restriction = Mod->Exports[I].getPointer())
2632 WildcardRestrictions.push_back(Restriction);
2633 else {
2634 WildcardRestrictions.clear();
2635 UnrestrictedWildcard = true;
2636 }
2637 }
2638
2639 // If there were any wildcards, push any imported modules that were
2640 // re-exported by the wildcard restriction.
2641 if (!AnyWildcard)
2642 continue;
2643
2644 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
2645 Module *Imported = Mod->Imports[I];
2646 if (!Visited.insert(Imported))
2647 continue;
2648
2649 bool Acceptable = UnrestrictedWildcard;
2650 if (!Acceptable) {
2651 // Check whether this module meets one of the restrictions.
2652 for (unsigned R = 0, NR = WildcardRestrictions.size(); R != NR; ++R) {
2653 Module *Restriction = WildcardRestrictions[R];
2654 if (Imported == Restriction || Imported->isSubModuleOf(Restriction)) {
2655 Acceptable = true;
2656 break;
2657 }
2658 }
2659 }
2660
2661 if (!Acceptable)
2662 continue;
2663
2664 Stack.push_back(Imported);
2665 }
2666 }
2667}
2668
2669ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
2670 ModuleKind Type,
2671 SourceLocation ImportLoc,
2672 unsigned ClientLoadCapabilities) {
2673 // Bump the generation number.
2674 unsigned PreviousGeneration = CurrentGeneration++;
2675
2676 unsigned NumModules = ModuleMgr.size();
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002677 SmallVector<ImportedModule, 4> Loaded;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002678 switch(ASTReadResult ReadResult = ReadASTCore(FileName, Type, ImportLoc,
2679 /*ImportedBy=*/0, Loaded,
2680 ClientLoadCapabilities)) {
2681 case Failure:
2682 case OutOfDate:
2683 case VersionMismatch:
2684 case ConfigurationMismatch:
2685 case HadErrors:
2686 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, ModuleMgr.end());
2687 return ReadResult;
2688
2689 case Success:
2690 break;
2691 }
2692
2693 // Here comes stuff that we only do once the entire chain is loaded.
2694
2695 // Load the AST blocks of all of the modules that we loaded.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002696 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
2697 MEnd = Loaded.end();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002698 M != MEnd; ++M) {
2699 ModuleFile &F = *M->Mod;
2700
2701 // Read the AST block.
2702 if (ReadASTBlock(F))
2703 return Failure;
2704
2705 // Once read, set the ModuleFile bit base offset and update the size in
2706 // bits of all files we've seen.
2707 F.GlobalBitOffset = TotalModulesSizeInBits;
2708 TotalModulesSizeInBits += F.SizeInBits;
2709 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
2710
2711 // Preload SLocEntries.
2712 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
2713 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
2714 // Load it through the SourceManager and don't call ReadSLocEntry()
2715 // directly because the entry may have already been loaded in which case
2716 // calling ReadSLocEntry() directly would trigger an assertion in
2717 // SourceManager.
2718 SourceMgr.getLoadedSLocEntryByID(Index);
2719 }
2720 }
2721
2722 // Setup the import locations.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002723 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
2724 MEnd = Loaded.end();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002725 M != MEnd; ++M) {
2726 ModuleFile &F = *M->Mod;
2727 if (!M->ImportedBy)
2728 F.ImportLoc = M->ImportLoc;
2729 else
2730 F.ImportLoc = ReadSourceLocation(*M->ImportedBy,
2731 M->ImportLoc.getRawEncoding());
2732 }
2733
2734 // Mark all of the identifiers in the identifier table as being out of date,
2735 // so that various accessors know to check the loaded modules when the
2736 // identifier is used.
2737 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
2738 IdEnd = PP.getIdentifierTable().end();
2739 Id != IdEnd; ++Id)
2740 Id->second->setOutOfDate(true);
2741
2742 // Resolve any unresolved module exports.
2743 for (unsigned I = 0, N = UnresolvedModuleImportExports.size(); I != N; ++I) {
2744 UnresolvedModuleImportExport &Unresolved = UnresolvedModuleImportExports[I];
2745 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
2746 Module *ResolvedMod = getSubmodule(GlobalID);
2747
2748 if (Unresolved.IsImport) {
2749 if (ResolvedMod)
2750 Unresolved.Mod->Imports.push_back(ResolvedMod);
2751 continue;
2752 }
2753
2754 if (ResolvedMod || Unresolved.IsWildcard)
2755 Unresolved.Mod->Exports.push_back(
2756 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
2757 }
2758 UnresolvedModuleImportExports.clear();
2759
2760 InitializeContext();
2761
2762 if (DeserializationListener)
2763 DeserializationListener->ReaderInitialized(this);
2764
2765 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
2766 if (!PrimaryModule.OriginalSourceFileID.isInvalid()) {
2767 PrimaryModule.OriginalSourceFileID
2768 = FileID::get(PrimaryModule.SLocEntryBaseID
2769 + PrimaryModule.OriginalSourceFileID.getOpaqueValue() - 1);
2770
2771 // If this AST file is a precompiled preamble, then set the
2772 // preamble file ID of the source manager to the file source file
2773 // from which the preamble was built.
2774 if (Type == MK_Preamble) {
2775 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
2776 } else if (Type == MK_MainFile) {
2777 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
2778 }
2779 }
2780
2781 // For any Objective-C class definitions we have already loaded, make sure
2782 // that we load any additional categories.
2783 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
2784 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
2785 ObjCClassesLoaded[I],
2786 PreviousGeneration);
2787 }
2788
2789 return Success;
2790}
2791
2792ASTReader::ASTReadResult
2793ASTReader::ReadASTCore(StringRef FileName,
2794 ModuleKind Type,
2795 SourceLocation ImportLoc,
2796 ModuleFile *ImportedBy,
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002797 SmallVectorImpl<ImportedModule> &Loaded,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002798 unsigned ClientLoadCapabilities) {
2799 ModuleFile *M;
2800 bool NewModule;
2801 std::string ErrorStr;
2802 llvm::tie(M, NewModule) = ModuleMgr.addModule(FileName, Type, ImportLoc,
2803 ImportedBy, CurrentGeneration,
2804 ErrorStr);
2805
2806 if (!M) {
2807 // We couldn't load the module.
2808 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
2809 + ErrorStr;
2810 Error(Msg);
2811 return Failure;
2812 }
2813
2814 if (!NewModule) {
2815 // We've already loaded this module.
2816 return Success;
2817 }
2818
2819 // FIXME: This seems rather a hack. Should CurrentDir be part of the
2820 // module?
2821 if (FileName != "-") {
2822 CurrentDir = llvm::sys::path::parent_path(FileName);
2823 if (CurrentDir.empty()) CurrentDir = ".";
2824 }
2825
2826 ModuleFile &F = *M;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00002827 BitstreamCursor &Stream = F.Stream;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002828 Stream.init(F.StreamFile);
2829 F.SizeInBits = F.Buffer->getBufferSize() * 8;
2830
2831 // Sniff for the signature.
2832 if (Stream.Read(8) != 'C' ||
2833 Stream.Read(8) != 'P' ||
2834 Stream.Read(8) != 'C' ||
2835 Stream.Read(8) != 'H') {
2836 Diag(diag::err_not_a_pch_file) << FileName;
2837 return Failure;
2838 }
2839
2840 // This is used for compatibility with older PCH formats.
2841 bool HaveReadControlBlock = false;
2842
Chris Lattner99a5af02013-01-20 00:00:22 +00002843 while (1) {
2844 llvm::BitstreamEntry Entry = Stream.advance();
2845
2846 switch (Entry.Kind) {
2847 case llvm::BitstreamEntry::Error:
2848 case llvm::BitstreamEntry::EndBlock:
2849 case llvm::BitstreamEntry::Record:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002850 Error("invalid record at top-level of AST file");
2851 return Failure;
Chris Lattner99a5af02013-01-20 00:00:22 +00002852
2853 case llvm::BitstreamEntry::SubBlock:
2854 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002855 }
2856
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002857 // We only know the control subblock ID.
Chris Lattner99a5af02013-01-20 00:00:22 +00002858 switch (Entry.ID) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002859 case llvm::bitc::BLOCKINFO_BLOCK_ID:
2860 if (Stream.ReadBlockInfoBlock()) {
2861 Error("malformed BlockInfoBlock in AST file");
2862 return Failure;
2863 }
2864 break;
2865 case CONTROL_BLOCK_ID:
2866 HaveReadControlBlock = true;
2867 switch (ReadControlBlock(F, Loaded, ClientLoadCapabilities)) {
2868 case Success:
2869 break;
2870
2871 case Failure: return Failure;
2872 case OutOfDate: return OutOfDate;
2873 case VersionMismatch: return VersionMismatch;
2874 case ConfigurationMismatch: return ConfigurationMismatch;
2875 case HadErrors: return HadErrors;
2876 }
2877 break;
2878 case AST_BLOCK_ID:
2879 if (!HaveReadControlBlock) {
2880 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2881 Diag(diag::warn_pch_version_too_old);
2882 return VersionMismatch;
2883 }
2884
2885 // Record that we've loaded this module.
2886 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
2887 return Success;
2888
2889 default:
2890 if (Stream.SkipBlock()) {
2891 Error("malformed block record in AST file");
2892 return Failure;
2893 }
2894 break;
2895 }
2896 }
2897
2898 return Success;
2899}
2900
2901void ASTReader::InitializeContext() {
2902 // If there's a listener, notify them that we "read" the translation unit.
2903 if (DeserializationListener)
2904 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
2905 Context.getTranslationUnitDecl());
2906
2907 // Make sure we load the declaration update records for the translation unit,
2908 // if there are any.
2909 loadDeclUpdateRecords(PREDEF_DECL_TRANSLATION_UNIT_ID,
2910 Context.getTranslationUnitDecl());
2911
2912 // FIXME: Find a better way to deal with collisions between these
2913 // built-in types. Right now, we just ignore the problem.
2914
2915 // Load the special types.
2916 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
2917 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
2918 if (!Context.CFConstantStringTypeDecl)
2919 Context.setCFConstantStringType(GetType(String));
2920 }
2921
2922 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
2923 QualType FileType = GetType(File);
2924 if (FileType.isNull()) {
2925 Error("FILE type is NULL");
2926 return;
2927 }
2928
2929 if (!Context.FILEDecl) {
2930 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
2931 Context.setFILEDecl(Typedef->getDecl());
2932 else {
2933 const TagType *Tag = FileType->getAs<TagType>();
2934 if (!Tag) {
2935 Error("Invalid FILE type in AST file");
2936 return;
2937 }
2938 Context.setFILEDecl(Tag->getDecl());
2939 }
2940 }
2941 }
2942
2943 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
2944 QualType Jmp_bufType = GetType(Jmp_buf);
2945 if (Jmp_bufType.isNull()) {
2946 Error("jmp_buf type is NULL");
2947 return;
2948 }
2949
2950 if (!Context.jmp_bufDecl) {
2951 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
2952 Context.setjmp_bufDecl(Typedef->getDecl());
2953 else {
2954 const TagType *Tag = Jmp_bufType->getAs<TagType>();
2955 if (!Tag) {
2956 Error("Invalid jmp_buf type in AST file");
2957 return;
2958 }
2959 Context.setjmp_bufDecl(Tag->getDecl());
2960 }
2961 }
2962 }
2963
2964 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
2965 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
2966 if (Sigjmp_bufType.isNull()) {
2967 Error("sigjmp_buf type is NULL");
2968 return;
2969 }
2970
2971 if (!Context.sigjmp_bufDecl) {
2972 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
2973 Context.setsigjmp_bufDecl(Typedef->getDecl());
2974 else {
2975 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
2976 assert(Tag && "Invalid sigjmp_buf type in AST file");
2977 Context.setsigjmp_bufDecl(Tag->getDecl());
2978 }
2979 }
2980 }
2981
2982 if (unsigned ObjCIdRedef
2983 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
2984 if (Context.ObjCIdRedefinitionType.isNull())
2985 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
2986 }
2987
2988 if (unsigned ObjCClassRedef
2989 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
2990 if (Context.ObjCClassRedefinitionType.isNull())
2991 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
2992 }
2993
2994 if (unsigned ObjCSelRedef
2995 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
2996 if (Context.ObjCSelRedefinitionType.isNull())
2997 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
2998 }
2999
3000 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
3001 QualType Ucontext_tType = GetType(Ucontext_t);
3002 if (Ucontext_tType.isNull()) {
3003 Error("ucontext_t type is NULL");
3004 return;
3005 }
3006
3007 if (!Context.ucontext_tDecl) {
3008 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
3009 Context.setucontext_tDecl(Typedef->getDecl());
3010 else {
3011 const TagType *Tag = Ucontext_tType->getAs<TagType>();
3012 assert(Tag && "Invalid ucontext_t type in AST file");
3013 Context.setucontext_tDecl(Tag->getDecl());
3014 }
3015 }
3016 }
3017 }
3018
3019 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
3020
3021 // If there were any CUDA special declarations, deserialize them.
3022 if (!CUDASpecialDeclRefs.empty()) {
3023 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
3024 Context.setcudaConfigureCallDecl(
3025 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
3026 }
3027
3028 // Re-export any modules that were imported by a non-module AST file.
3029 for (unsigned I = 0, N = ImportedModules.size(); I != N; ++I) {
3030 if (Module *Imported = getSubmodule(ImportedModules[I]))
3031 makeModuleVisible(Imported, Module::AllVisible);
3032 }
3033 ImportedModules.clear();
3034}
3035
3036void ASTReader::finalizeForWriting() {
3037 for (HiddenNamesMapType::iterator Hidden = HiddenNamesMap.begin(),
3038 HiddenEnd = HiddenNamesMap.end();
3039 Hidden != HiddenEnd; ++Hidden) {
3040 makeNamesVisible(Hidden->second);
3041 }
3042 HiddenNamesMap.clear();
3043}
3044
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003045/// SkipCursorToControlBlock - Given a cursor at the start of an AST file, scan
3046/// ahead and drop the cursor into the start of the CONTROL_BLOCK, returning
3047/// false on success and true on failure.
3048static bool SkipCursorToControlBlock(BitstreamCursor &Cursor) {
3049 while (1) {
3050 llvm::BitstreamEntry Entry = Cursor.advance();
3051 switch (Entry.Kind) {
3052 case llvm::BitstreamEntry::Error:
3053 case llvm::BitstreamEntry::EndBlock:
3054 return true;
3055
3056 case llvm::BitstreamEntry::Record:
3057 // Ignore top-level records.
3058 Cursor.skipRecord(Entry.ID);
3059 break;
3060
3061 case llvm::BitstreamEntry::SubBlock:
3062 if (Entry.ID == CONTROL_BLOCK_ID) {
3063 if (Cursor.EnterSubBlock(CONTROL_BLOCK_ID))
3064 return true;
3065 // Found it!
3066 return false;
3067 }
3068
3069 if (Cursor.SkipBlock())
3070 return true;
3071 }
3072 }
3073}
3074
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003075/// \brief Retrieve the name of the original source file name
3076/// directly from the AST file, without actually loading the AST
3077/// file.
3078std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
3079 FileManager &FileMgr,
3080 DiagnosticsEngine &Diags) {
3081 // Open the AST file.
3082 std::string ErrStr;
3083 OwningPtr<llvm::MemoryBuffer> Buffer;
3084 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
3085 if (!Buffer) {
3086 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ASTFileName << ErrStr;
3087 return std::string();
3088 }
3089
3090 // Initialize the stream
3091 llvm::BitstreamReader StreamFile;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003092 BitstreamCursor Stream;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003093 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
3094 (const unsigned char *)Buffer->getBufferEnd());
3095 Stream.init(StreamFile);
3096
3097 // Sniff for the signature.
3098 if (Stream.Read(8) != 'C' ||
3099 Stream.Read(8) != 'P' ||
3100 Stream.Read(8) != 'C' ||
3101 Stream.Read(8) != 'H') {
3102 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
3103 return std::string();
3104 }
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003105
Chris Lattner88bde502013-01-19 21:39:22 +00003106 // Scan for the CONTROL_BLOCK_ID block.
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003107 if (SkipCursorToControlBlock(Stream)) {
3108 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
3109 return std::string();
Chris Lattner88bde502013-01-19 21:39:22 +00003110 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003111
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003112 // Scan for ORIGINAL_FILE inside the control block.
3113 RecordData Record;
Chris Lattner88bde502013-01-19 21:39:22 +00003114 while (1) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003115 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Chris Lattner88bde502013-01-19 21:39:22 +00003116 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
3117 return std::string();
3118
3119 if (Entry.Kind != llvm::BitstreamEntry::Record) {
3120 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
3121 return std::string();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003122 }
Chris Lattner88bde502013-01-19 21:39:22 +00003123
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003124 Record.clear();
3125 const char *BlobStart = 0;
3126 unsigned BlobLen = 0;
Chris Lattner88bde502013-01-19 21:39:22 +00003127 if (Stream.ReadRecord(Entry.ID, Record, BlobStart, BlobLen)
3128 == ORIGINAL_FILE)
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003129 return std::string(BlobStart, BlobLen);
3130 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003131}
3132
3133namespace {
3134 class SimplePCHValidator : public ASTReaderListener {
3135 const LangOptions &ExistingLangOpts;
3136 const TargetOptions &ExistingTargetOpts;
3137 const PreprocessorOptions &ExistingPPOpts;
3138 FileManager &FileMgr;
3139
3140 public:
3141 SimplePCHValidator(const LangOptions &ExistingLangOpts,
3142 const TargetOptions &ExistingTargetOpts,
3143 const PreprocessorOptions &ExistingPPOpts,
3144 FileManager &FileMgr)
3145 : ExistingLangOpts(ExistingLangOpts),
3146 ExistingTargetOpts(ExistingTargetOpts),
3147 ExistingPPOpts(ExistingPPOpts),
3148 FileMgr(FileMgr)
3149 {
3150 }
3151
3152 virtual bool ReadLanguageOptions(const LangOptions &LangOpts,
3153 bool Complain) {
3154 return checkLanguageOptions(ExistingLangOpts, LangOpts, 0);
3155 }
3156 virtual bool ReadTargetOptions(const TargetOptions &TargetOpts,
3157 bool Complain) {
3158 return checkTargetOptions(ExistingTargetOpts, TargetOpts, 0);
3159 }
3160 virtual bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
3161 bool Complain,
3162 std::string &SuggestedPredefines) {
3163 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, 0, FileMgr,
3164 SuggestedPredefines);
3165 }
3166 };
3167}
3168
3169bool ASTReader::readASTFileControlBlock(StringRef Filename,
3170 FileManager &FileMgr,
3171 ASTReaderListener &Listener) {
3172 // Open the AST file.
3173 std::string ErrStr;
3174 OwningPtr<llvm::MemoryBuffer> Buffer;
3175 Buffer.reset(FileMgr.getBufferForFile(Filename, &ErrStr));
3176 if (!Buffer) {
3177 return true;
3178 }
3179
3180 // Initialize the stream
3181 llvm::BitstreamReader StreamFile;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003182 BitstreamCursor Stream;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003183 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
3184 (const unsigned char *)Buffer->getBufferEnd());
3185 Stream.init(StreamFile);
3186
3187 // Sniff for the signature.
3188 if (Stream.Read(8) != 'C' ||
3189 Stream.Read(8) != 'P' ||
3190 Stream.Read(8) != 'C' ||
3191 Stream.Read(8) != 'H') {
3192 return true;
3193 }
3194
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003195 // Scan for the CONTROL_BLOCK_ID block.
3196 if (SkipCursorToControlBlock(Stream))
3197 return true;
3198
3199 // Scan for ORIGINAL_FILE inside the control block.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003200 RecordData Record;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003201 while (1) {
3202 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
3203 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
3204 return false;
3205
3206 if (Entry.Kind != llvm::BitstreamEntry::Record)
3207 return true;
3208
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003209 Record.clear();
3210 const char *BlobStart = 0;
3211 unsigned BlobLen = 0;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003212 unsigned RecCode = Stream.ReadRecord(Entry.ID, Record, BlobStart, BlobLen);
3213 switch ((ControlRecordTypes)RecCode) {
3214 case METADATA: {
3215 if (Record[0] != VERSION_MAJOR)
3216 return true;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003217
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003218 const std::string &CurBranch = getClangFullRepositoryVersion();
3219 StringRef ASTBranch(BlobStart, BlobLen);
3220 if (StringRef(CurBranch) != ASTBranch)
3221 return true;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003222
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003223 break;
3224 }
3225 case LANGUAGE_OPTIONS:
3226 if (ParseLanguageOptions(Record, false, Listener))
3227 return true;
3228 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003229
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003230 case TARGET_OPTIONS:
3231 if (ParseTargetOptions(Record, false, Listener))
3232 return true;
3233 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003234
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003235 case DIAGNOSTIC_OPTIONS:
3236 if (ParseDiagnosticOptions(Record, false, Listener))
3237 return true;
3238 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003239
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003240 case FILE_SYSTEM_OPTIONS:
3241 if (ParseFileSystemOptions(Record, false, Listener))
3242 return true;
3243 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003244
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003245 case HEADER_SEARCH_OPTIONS:
3246 if (ParseHeaderSearchOptions(Record, false, Listener))
3247 return true;
3248 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003249
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003250 case PREPROCESSOR_OPTIONS: {
3251 std::string IgnoredSuggestedPredefines;
3252 if (ParsePreprocessorOptions(Record, false, Listener,
3253 IgnoredSuggestedPredefines))
3254 return true;
3255 break;
3256 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003257
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003258 default:
3259 // No other validation to perform.
3260 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003261 }
3262 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003263}
3264
3265
3266bool ASTReader::isAcceptableASTFile(StringRef Filename,
3267 FileManager &FileMgr,
3268 const LangOptions &LangOpts,
3269 const TargetOptions &TargetOpts,
3270 const PreprocessorOptions &PPOpts) {
3271 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts, FileMgr);
3272 return !readASTFileControlBlock(Filename, FileMgr, validator);
3273}
3274
3275bool ASTReader::ReadSubmoduleBlock(ModuleFile &F) {
3276 // Enter the submodule block.
3277 if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
3278 Error("malformed submodule block record in AST file");
3279 return true;
3280 }
3281
3282 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
3283 bool First = true;
3284 Module *CurrentModule = 0;
3285 RecordData Record;
3286 while (true) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003287 llvm::BitstreamEntry Entry = F.Stream.advanceSkippingSubblocks();
3288
3289 switch (Entry.Kind) {
3290 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
3291 case llvm::BitstreamEntry::Error:
3292 Error("malformed block record in AST file");
3293 return true;
3294 case llvm::BitstreamEntry::EndBlock:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003295 return false;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003296 case llvm::BitstreamEntry::Record:
3297 // The interesting case.
3298 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003299 }
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003300
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003301 // Read a record.
3302 const char *BlobStart;
3303 unsigned BlobLen;
3304 Record.clear();
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003305 switch (F.Stream.ReadRecord(Entry.ID, Record, BlobStart, BlobLen)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003306 default: // Default behavior: ignore.
3307 break;
3308
3309 case SUBMODULE_DEFINITION: {
3310 if (First) {
3311 Error("missing submodule metadata record at beginning of block");
3312 return true;
3313 }
3314
3315 if (Record.size() < 7) {
3316 Error("malformed module definition");
3317 return true;
3318 }
3319
3320 StringRef Name(BlobStart, BlobLen);
3321 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[0]);
3322 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[1]);
3323 bool IsFramework = Record[2];
3324 bool IsExplicit = Record[3];
3325 bool IsSystem = Record[4];
3326 bool InferSubmodules = Record[5];
3327 bool InferExplicitSubmodules = Record[6];
3328 bool InferExportWildcard = Record[7];
3329
3330 Module *ParentModule = 0;
3331 if (Parent)
3332 ParentModule = getSubmodule(Parent);
3333
3334 // Retrieve this (sub)module from the module map, creating it if
3335 // necessary.
3336 CurrentModule = ModMap.findOrCreateModule(Name, ParentModule,
3337 IsFramework,
3338 IsExplicit).first;
3339 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
3340 if (GlobalIndex >= SubmodulesLoaded.size() ||
3341 SubmodulesLoaded[GlobalIndex]) {
3342 Error("too many submodules");
3343 return true;
3344 }
3345
3346 CurrentModule->setASTFile(F.File);
3347 CurrentModule->IsFromModuleFile = true;
3348 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
3349 CurrentModule->InferSubmodules = InferSubmodules;
3350 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
3351 CurrentModule->InferExportWildcard = InferExportWildcard;
3352 if (DeserializationListener)
3353 DeserializationListener->ModuleRead(GlobalID, CurrentModule);
3354
3355 SubmodulesLoaded[GlobalIndex] = CurrentModule;
Douglas Gregorb6cbe512013-01-14 17:21:00 +00003356
3357 // Clear out link libraries; the module file has them.
3358 CurrentModule->LinkLibraries.clear();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003359 break;
3360 }
3361
3362 case SUBMODULE_UMBRELLA_HEADER: {
3363 if (First) {
3364 Error("missing submodule metadata record at beginning of block");
3365 return true;
3366 }
3367
3368 if (!CurrentModule)
3369 break;
3370
3371 StringRef FileName(BlobStart, BlobLen);
3372 if (const FileEntry *Umbrella = PP.getFileManager().getFile(FileName)) {
3373 if (!CurrentModule->getUmbrellaHeader())
3374 ModMap.setUmbrellaHeader(CurrentModule, Umbrella);
3375 else if (CurrentModule->getUmbrellaHeader() != Umbrella) {
3376 Error("mismatched umbrella headers in submodule");
3377 return true;
3378 }
3379 }
3380 break;
3381 }
3382
3383 case SUBMODULE_HEADER: {
3384 if (First) {
3385 Error("missing submodule metadata record at beginning of block");
3386 return true;
3387 }
3388
3389 if (!CurrentModule)
3390 break;
3391
3392 // FIXME: Be more lazy about this!
3393 StringRef FileName(BlobStart, BlobLen);
3394 if (const FileEntry *File = PP.getFileManager().getFile(FileName)) {
3395 if (std::find(CurrentModule->Headers.begin(),
3396 CurrentModule->Headers.end(),
3397 File) == CurrentModule->Headers.end())
3398 ModMap.addHeader(CurrentModule, File, false);
3399 }
3400 break;
3401 }
3402
3403 case SUBMODULE_EXCLUDED_HEADER: {
3404 if (First) {
3405 Error("missing submodule metadata record at beginning of block");
3406 return true;
3407 }
3408
3409 if (!CurrentModule)
3410 break;
3411
3412 // FIXME: Be more lazy about this!
3413 StringRef FileName(BlobStart, BlobLen);
3414 if (const FileEntry *File = PP.getFileManager().getFile(FileName)) {
3415 if (std::find(CurrentModule->Headers.begin(),
3416 CurrentModule->Headers.end(),
3417 File) == CurrentModule->Headers.end())
3418 ModMap.addHeader(CurrentModule, File, true);
3419 }
3420 break;
3421 }
3422
3423 case SUBMODULE_TOPHEADER: {
3424 if (First) {
3425 Error("missing submodule metadata record at beginning of block");
3426 return true;
3427 }
3428
3429 if (!CurrentModule)
3430 break;
3431
3432 // FIXME: Be more lazy about this!
3433 StringRef FileName(BlobStart, BlobLen);
3434 if (const FileEntry *File = PP.getFileManager().getFile(FileName))
3435 CurrentModule->TopHeaders.insert(File);
3436 break;
3437 }
3438
3439 case SUBMODULE_UMBRELLA_DIR: {
3440 if (First) {
3441 Error("missing submodule metadata record at beginning of block");
3442 return true;
3443 }
3444
3445 if (!CurrentModule)
3446 break;
3447
3448 StringRef DirName(BlobStart, BlobLen);
3449 if (const DirectoryEntry *Umbrella
3450 = PP.getFileManager().getDirectory(DirName)) {
3451 if (!CurrentModule->getUmbrellaDir())
3452 ModMap.setUmbrellaDir(CurrentModule, Umbrella);
3453 else if (CurrentModule->getUmbrellaDir() != Umbrella) {
3454 Error("mismatched umbrella directories in submodule");
3455 return true;
3456 }
3457 }
3458 break;
3459 }
3460
3461 case SUBMODULE_METADATA: {
3462 if (!First) {
3463 Error("submodule metadata record not at beginning of block");
3464 return true;
3465 }
3466 First = false;
3467
3468 F.BaseSubmoduleID = getTotalNumSubmodules();
3469 F.LocalNumSubmodules = Record[0];
3470 unsigned LocalBaseSubmoduleID = Record[1];
3471 if (F.LocalNumSubmodules > 0) {
3472 // Introduce the global -> local mapping for submodules within this
3473 // module.
3474 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
3475
3476 // Introduce the local -> global mapping for submodules within this
3477 // module.
3478 F.SubmoduleRemap.insertOrReplace(
3479 std::make_pair(LocalBaseSubmoduleID,
3480 F.BaseSubmoduleID - LocalBaseSubmoduleID));
3481
3482 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
3483 }
3484 break;
3485 }
3486
3487 case SUBMODULE_IMPORTS: {
3488 if (First) {
3489 Error("missing submodule metadata record at beginning of block");
3490 return true;
3491 }
3492
3493 if (!CurrentModule)
3494 break;
3495
3496 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
3497 UnresolvedModuleImportExport Unresolved;
3498 Unresolved.File = &F;
3499 Unresolved.Mod = CurrentModule;
3500 Unresolved.ID = Record[Idx];
3501 Unresolved.IsImport = true;
3502 Unresolved.IsWildcard = false;
3503 UnresolvedModuleImportExports.push_back(Unresolved);
3504 }
3505 break;
3506 }
3507
3508 case SUBMODULE_EXPORTS: {
3509 if (First) {
3510 Error("missing submodule metadata record at beginning of block");
3511 return true;
3512 }
3513
3514 if (!CurrentModule)
3515 break;
3516
3517 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
3518 UnresolvedModuleImportExport Unresolved;
3519 Unresolved.File = &F;
3520 Unresolved.Mod = CurrentModule;
3521 Unresolved.ID = Record[Idx];
3522 Unresolved.IsImport = false;
3523 Unresolved.IsWildcard = Record[Idx + 1];
3524 UnresolvedModuleImportExports.push_back(Unresolved);
3525 }
3526
3527 // Once we've loaded the set of exports, there's no reason to keep
3528 // the parsed, unresolved exports around.
3529 CurrentModule->UnresolvedExports.clear();
3530 break;
3531 }
3532 case SUBMODULE_REQUIRES: {
3533 if (First) {
3534 Error("missing submodule metadata record at beginning of block");
3535 return true;
3536 }
3537
3538 if (!CurrentModule)
3539 break;
3540
3541 CurrentModule->addRequirement(StringRef(BlobStart, BlobLen),
3542 Context.getLangOpts(),
3543 Context.getTargetInfo());
3544 break;
3545 }
Douglas Gregorb6cbe512013-01-14 17:21:00 +00003546
3547 case SUBMODULE_LINK_LIBRARY:
3548 if (First) {
3549 Error("missing submodule metadata record at beginning of block");
3550 return true;
3551 }
3552
3553 if (!CurrentModule)
3554 break;
3555
3556 CurrentModule->LinkLibraries.push_back(
3557 Module::LinkLibrary(StringRef(BlobStart, BlobLen),
3558 Record[0]));
3559 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003560 }
3561 }
3562}
3563
3564/// \brief Parse the record that corresponds to a LangOptions data
3565/// structure.
3566///
3567/// This routine parses the language options from the AST file and then gives
3568/// them to the AST listener if one is set.
3569///
3570/// \returns true if the listener deems the file unacceptable, false otherwise.
3571bool ASTReader::ParseLanguageOptions(const RecordData &Record,
3572 bool Complain,
3573 ASTReaderListener &Listener) {
3574 LangOptions LangOpts;
3575 unsigned Idx = 0;
3576#define LANGOPT(Name, Bits, Default, Description) \
3577 LangOpts.Name = Record[Idx++];
3578#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
3579 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
3580#include "clang/Basic/LangOptions.def"
Will Dietz4f45bc02013-01-18 11:30:38 +00003581#define SANITIZER(NAME, ID) LangOpts.Sanitize.ID = Record[Idx++];
3582#include "clang/Basic/Sanitizers.def"
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003583
3584 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
3585 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
3586 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
3587
3588 unsigned Length = Record[Idx++];
3589 LangOpts.CurrentModule.assign(Record.begin() + Idx,
3590 Record.begin() + Idx + Length);
3591 return Listener.ReadLanguageOptions(LangOpts, Complain);
3592}
3593
3594bool ASTReader::ParseTargetOptions(const RecordData &Record,
3595 bool Complain,
3596 ASTReaderListener &Listener) {
3597 unsigned Idx = 0;
3598 TargetOptions TargetOpts;
3599 TargetOpts.Triple = ReadString(Record, Idx);
3600 TargetOpts.CPU = ReadString(Record, Idx);
3601 TargetOpts.ABI = ReadString(Record, Idx);
3602 TargetOpts.CXXABI = ReadString(Record, Idx);
3603 TargetOpts.LinkerVersion = ReadString(Record, Idx);
3604 for (unsigned N = Record[Idx++]; N; --N) {
3605 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
3606 }
3607 for (unsigned N = Record[Idx++]; N; --N) {
3608 TargetOpts.Features.push_back(ReadString(Record, Idx));
3609 }
3610
3611 return Listener.ReadTargetOptions(TargetOpts, Complain);
3612}
3613
3614bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
3615 ASTReaderListener &Listener) {
3616 DiagnosticOptions DiagOpts;
3617 unsigned Idx = 0;
3618#define DIAGOPT(Name, Bits, Default) DiagOpts.Name = Record[Idx++];
3619#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
3620 DiagOpts.set##Name(static_cast<Type>(Record[Idx++]));
3621#include "clang/Basic/DiagnosticOptions.def"
3622
3623 for (unsigned N = Record[Idx++]; N; --N) {
3624 DiagOpts.Warnings.push_back(ReadString(Record, Idx));
3625 }
3626
3627 return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
3628}
3629
3630bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
3631 ASTReaderListener &Listener) {
3632 FileSystemOptions FSOpts;
3633 unsigned Idx = 0;
3634 FSOpts.WorkingDir = ReadString(Record, Idx);
3635 return Listener.ReadFileSystemOptions(FSOpts, Complain);
3636}
3637
3638bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
3639 bool Complain,
3640 ASTReaderListener &Listener) {
3641 HeaderSearchOptions HSOpts;
3642 unsigned Idx = 0;
3643 HSOpts.Sysroot = ReadString(Record, Idx);
3644
3645 // Include entries.
3646 for (unsigned N = Record[Idx++]; N; --N) {
3647 std::string Path = ReadString(Record, Idx);
3648 frontend::IncludeDirGroup Group
3649 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
3650 bool IsUserSupplied = Record[Idx++];
3651 bool IsFramework = Record[Idx++];
3652 bool IgnoreSysRoot = Record[Idx++];
3653 bool IsInternal = Record[Idx++];
3654 bool ImplicitExternC = Record[Idx++];
3655 HSOpts.UserEntries.push_back(
3656 HeaderSearchOptions::Entry(Path, Group, IsUserSupplied, IsFramework,
3657 IgnoreSysRoot, IsInternal, ImplicitExternC));
3658 }
3659
3660 // System header prefixes.
3661 for (unsigned N = Record[Idx++]; N; --N) {
3662 std::string Prefix = ReadString(Record, Idx);
3663 bool IsSystemHeader = Record[Idx++];
3664 HSOpts.SystemHeaderPrefixes.push_back(
3665 HeaderSearchOptions::SystemHeaderPrefix(Prefix, IsSystemHeader));
3666 }
3667
3668 HSOpts.ResourceDir = ReadString(Record, Idx);
3669 HSOpts.ModuleCachePath = ReadString(Record, Idx);
3670 HSOpts.DisableModuleHash = Record[Idx++];
3671 HSOpts.UseBuiltinIncludes = Record[Idx++];
3672 HSOpts.UseStandardSystemIncludes = Record[Idx++];
3673 HSOpts.UseStandardCXXIncludes = Record[Idx++];
3674 HSOpts.UseLibcxx = Record[Idx++];
3675
3676 return Listener.ReadHeaderSearchOptions(HSOpts, Complain);
3677}
3678
3679bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
3680 bool Complain,
3681 ASTReaderListener &Listener,
3682 std::string &SuggestedPredefines) {
3683 PreprocessorOptions PPOpts;
3684 unsigned Idx = 0;
3685
3686 // Macro definitions/undefs
3687 for (unsigned N = Record[Idx++]; N; --N) {
3688 std::string Macro = ReadString(Record, Idx);
3689 bool IsUndef = Record[Idx++];
3690 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
3691 }
3692
3693 // Includes
3694 for (unsigned N = Record[Idx++]; N; --N) {
3695 PPOpts.Includes.push_back(ReadString(Record, Idx));
3696 }
3697
3698 // Macro Includes
3699 for (unsigned N = Record[Idx++]; N; --N) {
3700 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
3701 }
3702
3703 PPOpts.UsePredefines = Record[Idx++];
3704 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
3705 PPOpts.ImplicitPTHInclude = ReadString(Record, Idx);
3706 PPOpts.ObjCXXARCStandardLibrary =
3707 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
3708 SuggestedPredefines.clear();
3709 return Listener.ReadPreprocessorOptions(PPOpts, Complain,
3710 SuggestedPredefines);
3711}
3712
3713std::pair<ModuleFile *, unsigned>
3714ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
3715 GlobalPreprocessedEntityMapType::iterator
3716 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
3717 assert(I != GlobalPreprocessedEntityMap.end() &&
3718 "Corrupted global preprocessed entity map");
3719 ModuleFile *M = I->second;
3720 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
3721 return std::make_pair(M, LocalIndex);
3722}
3723
3724std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
3725ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
3726 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
3727 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
3728 Mod.NumPreprocessedEntities);
3729
3730 return std::make_pair(PreprocessingRecord::iterator(),
3731 PreprocessingRecord::iterator());
3732}
3733
3734std::pair<ASTReader::ModuleDeclIterator, ASTReader::ModuleDeclIterator>
3735ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
3736 return std::make_pair(ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
3737 ModuleDeclIterator(this, &Mod,
3738 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
3739}
3740
3741PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
3742 PreprocessedEntityID PPID = Index+1;
3743 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
3744 ModuleFile &M = *PPInfo.first;
3745 unsigned LocalIndex = PPInfo.second;
3746 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
3747
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003748 if (!PP.getPreprocessingRecord()) {
3749 Error("no preprocessing record");
3750 return 0;
3751 }
3752
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003753 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
3754 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
3755
3756 llvm::BitstreamEntry Entry =
3757 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
3758 if (Entry.Kind != llvm::BitstreamEntry::Record)
3759 return 0;
3760
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003761 // Read the record.
3762 SourceRange Range(ReadSourceLocation(M, PPOffs.Begin),
3763 ReadSourceLocation(M, PPOffs.End));
3764 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
3765 const char *BlobStart = 0;
3766 unsigned BlobLen = 0;
3767 RecordData Record;
3768 PreprocessorDetailRecordTypes RecType =
3769 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.ReadRecord(
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003770 Entry.ID, Record, BlobStart, BlobLen);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003771 switch (RecType) {
3772 case PPD_MACRO_EXPANSION: {
3773 bool isBuiltin = Record[0];
3774 IdentifierInfo *Name = 0;
3775 MacroDefinition *Def = 0;
3776 if (isBuiltin)
3777 Name = getLocalIdentifier(M, Record[1]);
3778 else {
3779 PreprocessedEntityID
3780 GlobalID = getGlobalPreprocessedEntityID(M, Record[1]);
3781 Def =cast<MacroDefinition>(PPRec.getLoadedPreprocessedEntity(GlobalID-1));
3782 }
3783
3784 MacroExpansion *ME;
3785 if (isBuiltin)
3786 ME = new (PPRec) MacroExpansion(Name, Range);
3787 else
3788 ME = new (PPRec) MacroExpansion(Def, Range);
3789
3790 return ME;
3791 }
3792
3793 case PPD_MACRO_DEFINITION: {
3794 // Decode the identifier info and then check again; if the macro is
3795 // still defined and associated with the identifier,
3796 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
3797 MacroDefinition *MD
3798 = new (PPRec) MacroDefinition(II, Range);
3799
3800 if (DeserializationListener)
3801 DeserializationListener->MacroDefinitionRead(PPID, MD);
3802
3803 return MD;
3804 }
3805
3806 case PPD_INCLUSION_DIRECTIVE: {
3807 const char *FullFileNameStart = BlobStart + Record[0];
3808 StringRef FullFileName(FullFileNameStart, BlobLen - Record[0]);
3809 const FileEntry *File = 0;
3810 if (!FullFileName.empty())
3811 File = PP.getFileManager().getFile(FullFileName);
3812
3813 // FIXME: Stable encoding
3814 InclusionDirective::InclusionKind Kind
3815 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
3816 InclusionDirective *ID
3817 = new (PPRec) InclusionDirective(PPRec, Kind,
3818 StringRef(BlobStart, Record[0]),
3819 Record[1], Record[3],
3820 File,
3821 Range);
3822 return ID;
3823 }
3824 }
3825
3826 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
3827}
3828
3829/// \brief \arg SLocMapI points at a chunk of a module that contains no
3830/// preprocessed entities or the entities it contains are not the ones we are
3831/// looking for. Find the next module that contains entities and return the ID
3832/// of the first entry.
3833PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
3834 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
3835 ++SLocMapI;
3836 for (GlobalSLocOffsetMapType::const_iterator
3837 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
3838 ModuleFile &M = *SLocMapI->second;
3839 if (M.NumPreprocessedEntities)
3840 return M.BasePreprocessedEntityID;
3841 }
3842
3843 return getTotalNumPreprocessedEntities();
3844}
3845
3846namespace {
3847
3848template <unsigned PPEntityOffset::*PPLoc>
3849struct PPEntityComp {
3850 const ASTReader &Reader;
3851 ModuleFile &M;
3852
3853 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) { }
3854
3855 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
3856 SourceLocation LHS = getLoc(L);
3857 SourceLocation RHS = getLoc(R);
3858 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
3859 }
3860
3861 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
3862 SourceLocation LHS = getLoc(L);
3863 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
3864 }
3865
3866 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
3867 SourceLocation RHS = getLoc(R);
3868 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
3869 }
3870
3871 SourceLocation getLoc(const PPEntityOffset &PPE) const {
3872 return Reader.ReadSourceLocation(M, PPE.*PPLoc);
3873 }
3874};
3875
3876}
3877
3878/// \brief Returns the first preprocessed entity ID that ends after \arg BLoc.
3879PreprocessedEntityID
3880ASTReader::findBeginPreprocessedEntity(SourceLocation BLoc) const {
3881 if (SourceMgr.isLocalSourceLocation(BLoc))
3882 return getTotalNumPreprocessedEntities();
3883
3884 GlobalSLocOffsetMapType::const_iterator
3885 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
3886 BLoc.getOffset());
3887 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
3888 "Corrupted global sloc offset map");
3889
3890 if (SLocMapI->second->NumPreprocessedEntities == 0)
3891 return findNextPreprocessedEntity(SLocMapI);
3892
3893 ModuleFile &M = *SLocMapI->second;
3894 typedef const PPEntityOffset *pp_iterator;
3895 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
3896 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
3897
3898 size_t Count = M.NumPreprocessedEntities;
3899 size_t Half;
3900 pp_iterator First = pp_begin;
3901 pp_iterator PPI;
3902
3903 // Do a binary search manually instead of using std::lower_bound because
3904 // The end locations of entities may be unordered (when a macro expansion
3905 // is inside another macro argument), but for this case it is not important
3906 // whether we get the first macro expansion or its containing macro.
3907 while (Count > 0) {
3908 Half = Count/2;
3909 PPI = First;
3910 std::advance(PPI, Half);
3911 if (SourceMgr.isBeforeInTranslationUnit(ReadSourceLocation(M, PPI->End),
3912 BLoc)){
3913 First = PPI;
3914 ++First;
3915 Count = Count - Half - 1;
3916 } else
3917 Count = Half;
3918 }
3919
3920 if (PPI == pp_end)
3921 return findNextPreprocessedEntity(SLocMapI);
3922
3923 return M.BasePreprocessedEntityID + (PPI - pp_begin);
3924}
3925
3926/// \brief Returns the first preprocessed entity ID that begins after \arg ELoc.
3927PreprocessedEntityID
3928ASTReader::findEndPreprocessedEntity(SourceLocation ELoc) const {
3929 if (SourceMgr.isLocalSourceLocation(ELoc))
3930 return getTotalNumPreprocessedEntities();
3931
3932 GlobalSLocOffsetMapType::const_iterator
3933 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
3934 ELoc.getOffset());
3935 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
3936 "Corrupted global sloc offset map");
3937
3938 if (SLocMapI->second->NumPreprocessedEntities == 0)
3939 return findNextPreprocessedEntity(SLocMapI);
3940
3941 ModuleFile &M = *SLocMapI->second;
3942 typedef const PPEntityOffset *pp_iterator;
3943 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
3944 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
3945 pp_iterator PPI =
3946 std::upper_bound(pp_begin, pp_end, ELoc,
3947 PPEntityComp<&PPEntityOffset::Begin>(*this, M));
3948
3949 if (PPI == pp_end)
3950 return findNextPreprocessedEntity(SLocMapI);
3951
3952 return M.BasePreprocessedEntityID + (PPI - pp_begin);
3953}
3954
3955/// \brief Returns a pair of [Begin, End) indices of preallocated
3956/// preprocessed entities that \arg Range encompasses.
3957std::pair<unsigned, unsigned>
3958 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
3959 if (Range.isInvalid())
3960 return std::make_pair(0,0);
3961 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
3962
3963 PreprocessedEntityID BeginID = findBeginPreprocessedEntity(Range.getBegin());
3964 PreprocessedEntityID EndID = findEndPreprocessedEntity(Range.getEnd());
3965 return std::make_pair(BeginID, EndID);
3966}
3967
3968/// \brief Optionally returns true or false if the preallocated preprocessed
3969/// entity with index \arg Index came from file \arg FID.
3970llvm::Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
3971 FileID FID) {
3972 if (FID.isInvalid())
3973 return false;
3974
3975 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
3976 ModuleFile &M = *PPInfo.first;
3977 unsigned LocalIndex = PPInfo.second;
3978 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
3979
3980 SourceLocation Loc = ReadSourceLocation(M, PPOffs.Begin);
3981 if (Loc.isInvalid())
3982 return false;
3983
3984 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
3985 return true;
3986 else
3987 return false;
3988}
3989
3990namespace {
3991 /// \brief Visitor used to search for information about a header file.
3992 class HeaderFileInfoVisitor {
3993 ASTReader &Reader;
3994 const FileEntry *FE;
3995
3996 llvm::Optional<HeaderFileInfo> HFI;
3997
3998 public:
3999 HeaderFileInfoVisitor(ASTReader &Reader, const FileEntry *FE)
4000 : Reader(Reader), FE(FE) { }
4001
4002 static bool visit(ModuleFile &M, void *UserData) {
4003 HeaderFileInfoVisitor *This
4004 = static_cast<HeaderFileInfoVisitor *>(UserData);
4005
4006 HeaderFileInfoTrait Trait(This->Reader, M,
4007 &This->Reader.getPreprocessor().getHeaderSearchInfo(),
4008 M.HeaderFileFrameworkStrings,
4009 This->FE->getName());
4010
4011 HeaderFileInfoLookupTable *Table
4012 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
4013 if (!Table)
4014 return false;
4015
4016 // Look in the on-disk hash table for an entry for this file name.
4017 HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE->getName(),
4018 &Trait);
4019 if (Pos == Table->end())
4020 return false;
4021
4022 This->HFI = *Pos;
4023 return true;
4024 }
4025
4026 llvm::Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
4027 };
4028}
4029
4030HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
4031 HeaderFileInfoVisitor Visitor(*this, FE);
4032 ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor);
4033 if (llvm::Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) {
4034 if (Listener)
4035 Listener->ReadHeaderFileInfo(*HFI, FE->getUID());
4036 return *HFI;
4037 }
4038
4039 return HeaderFileInfo();
4040}
4041
4042void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
4043 // FIXME: Make it work properly with modules.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00004044 SmallVector<DiagnosticsEngine::DiagState *, 32> DiagStates;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004045 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
4046 ModuleFile &F = *(*I);
4047 unsigned Idx = 0;
4048 DiagStates.clear();
4049 assert(!Diag.DiagStates.empty());
4050 DiagStates.push_back(&Diag.DiagStates.front()); // the command-line one.
4051 while (Idx < F.PragmaDiagMappings.size()) {
4052 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
4053 unsigned DiagStateID = F.PragmaDiagMappings[Idx++];
4054 if (DiagStateID != 0) {
4055 Diag.DiagStatePoints.push_back(
4056 DiagnosticsEngine::DiagStatePoint(DiagStates[DiagStateID-1],
4057 FullSourceLoc(Loc, SourceMgr)));
4058 continue;
4059 }
4060
4061 assert(DiagStateID == 0);
4062 // A new DiagState was created here.
4063 Diag.DiagStates.push_back(*Diag.GetCurDiagState());
4064 DiagnosticsEngine::DiagState *NewState = &Diag.DiagStates.back();
4065 DiagStates.push_back(NewState);
4066 Diag.DiagStatePoints.push_back(
4067 DiagnosticsEngine::DiagStatePoint(NewState,
4068 FullSourceLoc(Loc, SourceMgr)));
4069 while (1) {
4070 assert(Idx < F.PragmaDiagMappings.size() &&
4071 "Invalid data, didn't find '-1' marking end of diag/map pairs");
4072 if (Idx >= F.PragmaDiagMappings.size()) {
4073 break; // Something is messed up but at least avoid infinite loop in
4074 // release build.
4075 }
4076 unsigned DiagID = F.PragmaDiagMappings[Idx++];
4077 if (DiagID == (unsigned)-1) {
4078 break; // no more diag/map pairs for this location.
4079 }
4080 diag::Mapping Map = (diag::Mapping)F.PragmaDiagMappings[Idx++];
4081 DiagnosticMappingInfo MappingInfo = Diag.makeMappingInfo(Map, Loc);
4082 Diag.GetCurDiagState()->setMappingInfo(DiagID, MappingInfo);
4083 }
4084 }
4085 }
4086}
4087
4088/// \brief Get the correct cursor and offset for loading a type.
4089ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
4090 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
4091 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
4092 ModuleFile *M = I->second;
4093 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
4094}
4095
4096/// \brief Read and return the type with the given index..
4097///
4098/// The index is the type ID, shifted and minus the number of predefs. This
4099/// routine actually reads the record corresponding to the type at the given
4100/// location. It is a helper routine for GetType, which deals with reading type
4101/// IDs.
4102QualType ASTReader::readTypeRecord(unsigned Index) {
4103 RecordLocation Loc = TypeCursorForIndex(Index);
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004104 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004105
4106 // Keep track of where we are in the stream, then jump back there
4107 // after reading this type.
4108 SavedStreamPosition SavedPosition(DeclsCursor);
4109
4110 ReadingKindTracker ReadingKind(Read_Type, *this);
4111
4112 // Note that we are loading a type record.
4113 Deserializing AType(this);
4114
4115 unsigned Idx = 0;
4116 DeclsCursor.JumpToBit(Loc.Offset);
4117 RecordData Record;
4118 unsigned Code = DeclsCursor.ReadCode();
4119 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
4120 case TYPE_EXT_QUAL: {
4121 if (Record.size() != 2) {
4122 Error("Incorrect encoding of extended qualifier type");
4123 return QualType();
4124 }
4125 QualType Base = readType(*Loc.F, Record, Idx);
4126 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
4127 return Context.getQualifiedType(Base, Quals);
4128 }
4129
4130 case TYPE_COMPLEX: {
4131 if (Record.size() != 1) {
4132 Error("Incorrect encoding of complex type");
4133 return QualType();
4134 }
4135 QualType ElemType = readType(*Loc.F, Record, Idx);
4136 return Context.getComplexType(ElemType);
4137 }
4138
4139 case TYPE_POINTER: {
4140 if (Record.size() != 1) {
4141 Error("Incorrect encoding of pointer type");
4142 return QualType();
4143 }
4144 QualType PointeeType = readType(*Loc.F, Record, Idx);
4145 return Context.getPointerType(PointeeType);
4146 }
4147
4148 case TYPE_BLOCK_POINTER: {
4149 if (Record.size() != 1) {
4150 Error("Incorrect encoding of block pointer type");
4151 return QualType();
4152 }
4153 QualType PointeeType = readType(*Loc.F, Record, Idx);
4154 return Context.getBlockPointerType(PointeeType);
4155 }
4156
4157 case TYPE_LVALUE_REFERENCE: {
4158 if (Record.size() != 2) {
4159 Error("Incorrect encoding of lvalue reference type");
4160 return QualType();
4161 }
4162 QualType PointeeType = readType(*Loc.F, Record, Idx);
4163 return Context.getLValueReferenceType(PointeeType, Record[1]);
4164 }
4165
4166 case TYPE_RVALUE_REFERENCE: {
4167 if (Record.size() != 1) {
4168 Error("Incorrect encoding of rvalue reference type");
4169 return QualType();
4170 }
4171 QualType PointeeType = readType(*Loc.F, Record, Idx);
4172 return Context.getRValueReferenceType(PointeeType);
4173 }
4174
4175 case TYPE_MEMBER_POINTER: {
4176 if (Record.size() != 2) {
4177 Error("Incorrect encoding of member pointer type");
4178 return QualType();
4179 }
4180 QualType PointeeType = readType(*Loc.F, Record, Idx);
4181 QualType ClassType = readType(*Loc.F, Record, Idx);
4182 if (PointeeType.isNull() || ClassType.isNull())
4183 return QualType();
4184
4185 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
4186 }
4187
4188 case TYPE_CONSTANT_ARRAY: {
4189 QualType ElementType = readType(*Loc.F, Record, Idx);
4190 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4191 unsigned IndexTypeQuals = Record[2];
4192 unsigned Idx = 3;
4193 llvm::APInt Size = ReadAPInt(Record, Idx);
4194 return Context.getConstantArrayType(ElementType, Size,
4195 ASM, IndexTypeQuals);
4196 }
4197
4198 case TYPE_INCOMPLETE_ARRAY: {
4199 QualType ElementType = readType(*Loc.F, Record, Idx);
4200 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4201 unsigned IndexTypeQuals = Record[2];
4202 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
4203 }
4204
4205 case TYPE_VARIABLE_ARRAY: {
4206 QualType ElementType = readType(*Loc.F, Record, Idx);
4207 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4208 unsigned IndexTypeQuals = Record[2];
4209 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
4210 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
4211 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
4212 ASM, IndexTypeQuals,
4213 SourceRange(LBLoc, RBLoc));
4214 }
4215
4216 case TYPE_VECTOR: {
4217 if (Record.size() != 3) {
4218 Error("incorrect encoding of vector type in AST file");
4219 return QualType();
4220 }
4221
4222 QualType ElementType = readType(*Loc.F, Record, Idx);
4223 unsigned NumElements = Record[1];
4224 unsigned VecKind = Record[2];
4225 return Context.getVectorType(ElementType, NumElements,
4226 (VectorType::VectorKind)VecKind);
4227 }
4228
4229 case TYPE_EXT_VECTOR: {
4230 if (Record.size() != 3) {
4231 Error("incorrect encoding of extended vector type in AST file");
4232 return QualType();
4233 }
4234
4235 QualType ElementType = readType(*Loc.F, Record, Idx);
4236 unsigned NumElements = Record[1];
4237 return Context.getExtVectorType(ElementType, NumElements);
4238 }
4239
4240 case TYPE_FUNCTION_NO_PROTO: {
4241 if (Record.size() != 6) {
4242 Error("incorrect encoding of no-proto function type");
4243 return QualType();
4244 }
4245 QualType ResultType = readType(*Loc.F, Record, Idx);
4246 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
4247 (CallingConv)Record[4], Record[5]);
4248 return Context.getFunctionNoProtoType(ResultType, Info);
4249 }
4250
4251 case TYPE_FUNCTION_PROTO: {
4252 QualType ResultType = readType(*Loc.F, Record, Idx);
4253
4254 FunctionProtoType::ExtProtoInfo EPI;
4255 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
4256 /*hasregparm*/ Record[2],
4257 /*regparm*/ Record[3],
4258 static_cast<CallingConv>(Record[4]),
4259 /*produces*/ Record[5]);
4260
4261 unsigned Idx = 6;
4262 unsigned NumParams = Record[Idx++];
4263 SmallVector<QualType, 16> ParamTypes;
4264 for (unsigned I = 0; I != NumParams; ++I)
4265 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
4266
4267 EPI.Variadic = Record[Idx++];
4268 EPI.HasTrailingReturn = Record[Idx++];
4269 EPI.TypeQuals = Record[Idx++];
4270 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
4271 ExceptionSpecificationType EST =
4272 static_cast<ExceptionSpecificationType>(Record[Idx++]);
4273 EPI.ExceptionSpecType = EST;
4274 SmallVector<QualType, 2> Exceptions;
4275 if (EST == EST_Dynamic) {
4276 EPI.NumExceptions = Record[Idx++];
4277 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
4278 Exceptions.push_back(readType(*Loc.F, Record, Idx));
4279 EPI.Exceptions = Exceptions.data();
4280 } else if (EST == EST_ComputedNoexcept) {
4281 EPI.NoexceptExpr = ReadExpr(*Loc.F);
4282 } else if (EST == EST_Uninstantiated) {
4283 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
4284 EPI.ExceptionSpecTemplate = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
4285 } else if (EST == EST_Unevaluated) {
4286 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
4287 }
4288 return Context.getFunctionType(ResultType, ParamTypes.data(), NumParams,
4289 EPI);
4290 }
4291
4292 case TYPE_UNRESOLVED_USING: {
4293 unsigned Idx = 0;
4294 return Context.getTypeDeclType(
4295 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
4296 }
4297
4298 case TYPE_TYPEDEF: {
4299 if (Record.size() != 2) {
4300 Error("incorrect encoding of typedef type");
4301 return QualType();
4302 }
4303 unsigned Idx = 0;
4304 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
4305 QualType Canonical = readType(*Loc.F, Record, Idx);
4306 if (!Canonical.isNull())
4307 Canonical = Context.getCanonicalType(Canonical);
4308 return Context.getTypedefType(Decl, Canonical);
4309 }
4310
4311 case TYPE_TYPEOF_EXPR:
4312 return Context.getTypeOfExprType(ReadExpr(*Loc.F));
4313
4314 case TYPE_TYPEOF: {
4315 if (Record.size() != 1) {
4316 Error("incorrect encoding of typeof(type) in AST file");
4317 return QualType();
4318 }
4319 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
4320 return Context.getTypeOfType(UnderlyingType);
4321 }
4322
4323 case TYPE_DECLTYPE: {
4324 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
4325 return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
4326 }
4327
4328 case TYPE_UNARY_TRANSFORM: {
4329 QualType BaseType = readType(*Loc.F, Record, Idx);
4330 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
4331 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
4332 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
4333 }
4334
4335 case TYPE_AUTO:
4336 return Context.getAutoType(readType(*Loc.F, Record, Idx));
4337
4338 case TYPE_RECORD: {
4339 if (Record.size() != 2) {
4340 Error("incorrect encoding of record type");
4341 return QualType();
4342 }
4343 unsigned Idx = 0;
4344 bool IsDependent = Record[Idx++];
4345 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
4346 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
4347 QualType T = Context.getRecordType(RD);
4348 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
4349 return T;
4350 }
4351
4352 case TYPE_ENUM: {
4353 if (Record.size() != 2) {
4354 Error("incorrect encoding of enum type");
4355 return QualType();
4356 }
4357 unsigned Idx = 0;
4358 bool IsDependent = Record[Idx++];
4359 QualType T
4360 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
4361 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
4362 return T;
4363 }
4364
4365 case TYPE_ATTRIBUTED: {
4366 if (Record.size() != 3) {
4367 Error("incorrect encoding of attributed type");
4368 return QualType();
4369 }
4370 QualType modifiedType = readType(*Loc.F, Record, Idx);
4371 QualType equivalentType = readType(*Loc.F, Record, Idx);
4372 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
4373 return Context.getAttributedType(kind, modifiedType, equivalentType);
4374 }
4375
4376 case TYPE_PAREN: {
4377 if (Record.size() != 1) {
4378 Error("incorrect encoding of paren type");
4379 return QualType();
4380 }
4381 QualType InnerType = readType(*Loc.F, Record, Idx);
4382 return Context.getParenType(InnerType);
4383 }
4384
4385 case TYPE_PACK_EXPANSION: {
4386 if (Record.size() != 2) {
4387 Error("incorrect encoding of pack expansion type");
4388 return QualType();
4389 }
4390 QualType Pattern = readType(*Loc.F, Record, Idx);
4391 if (Pattern.isNull())
4392 return QualType();
4393 llvm::Optional<unsigned> NumExpansions;
4394 if (Record[1])
4395 NumExpansions = Record[1] - 1;
4396 return Context.getPackExpansionType(Pattern, NumExpansions);
4397 }
4398
4399 case TYPE_ELABORATED: {
4400 unsigned Idx = 0;
4401 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
4402 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
4403 QualType NamedType = readType(*Loc.F, Record, Idx);
4404 return Context.getElaboratedType(Keyword, NNS, NamedType);
4405 }
4406
4407 case TYPE_OBJC_INTERFACE: {
4408 unsigned Idx = 0;
4409 ObjCInterfaceDecl *ItfD
4410 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
4411 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
4412 }
4413
4414 case TYPE_OBJC_OBJECT: {
4415 unsigned Idx = 0;
4416 QualType Base = readType(*Loc.F, Record, Idx);
4417 unsigned NumProtos = Record[Idx++];
4418 SmallVector<ObjCProtocolDecl*, 4> Protos;
4419 for (unsigned I = 0; I != NumProtos; ++I)
4420 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
4421 return Context.getObjCObjectType(Base, Protos.data(), NumProtos);
4422 }
4423
4424 case TYPE_OBJC_OBJECT_POINTER: {
4425 unsigned Idx = 0;
4426 QualType Pointee = readType(*Loc.F, Record, Idx);
4427 return Context.getObjCObjectPointerType(Pointee);
4428 }
4429
4430 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
4431 unsigned Idx = 0;
4432 QualType Parm = readType(*Loc.F, Record, Idx);
4433 QualType Replacement = readType(*Loc.F, Record, Idx);
4434 return
4435 Context.getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
4436 Replacement);
4437 }
4438
4439 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
4440 unsigned Idx = 0;
4441 QualType Parm = readType(*Loc.F, Record, Idx);
4442 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
4443 return Context.getSubstTemplateTypeParmPackType(
4444 cast<TemplateTypeParmType>(Parm),
4445 ArgPack);
4446 }
4447
4448 case TYPE_INJECTED_CLASS_NAME: {
4449 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
4450 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
4451 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
4452 // for AST reading, too much interdependencies.
4453 return
4454 QualType(new (Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
4455 }
4456
4457 case TYPE_TEMPLATE_TYPE_PARM: {
4458 unsigned Idx = 0;
4459 unsigned Depth = Record[Idx++];
4460 unsigned Index = Record[Idx++];
4461 bool Pack = Record[Idx++];
4462 TemplateTypeParmDecl *D
4463 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
4464 return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
4465 }
4466
4467 case TYPE_DEPENDENT_NAME: {
4468 unsigned Idx = 0;
4469 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
4470 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
4471 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
4472 QualType Canon = readType(*Loc.F, Record, Idx);
4473 if (!Canon.isNull())
4474 Canon = Context.getCanonicalType(Canon);
4475 return Context.getDependentNameType(Keyword, NNS, Name, Canon);
4476 }
4477
4478 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
4479 unsigned Idx = 0;
4480 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
4481 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
4482 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
4483 unsigned NumArgs = Record[Idx++];
4484 SmallVector<TemplateArgument, 8> Args;
4485 Args.reserve(NumArgs);
4486 while (NumArgs--)
4487 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
4488 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
4489 Args.size(), Args.data());
4490 }
4491
4492 case TYPE_DEPENDENT_SIZED_ARRAY: {
4493 unsigned Idx = 0;
4494
4495 // ArrayType
4496 QualType ElementType = readType(*Loc.F, Record, Idx);
4497 ArrayType::ArraySizeModifier ASM
4498 = (ArrayType::ArraySizeModifier)Record[Idx++];
4499 unsigned IndexTypeQuals = Record[Idx++];
4500
4501 // DependentSizedArrayType
4502 Expr *NumElts = ReadExpr(*Loc.F);
4503 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
4504
4505 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
4506 IndexTypeQuals, Brackets);
4507 }
4508
4509 case TYPE_TEMPLATE_SPECIALIZATION: {
4510 unsigned Idx = 0;
4511 bool IsDependent = Record[Idx++];
4512 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
4513 SmallVector<TemplateArgument, 8> Args;
4514 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
4515 QualType Underlying = readType(*Loc.F, Record, Idx);
4516 QualType T;
4517 if (Underlying.isNull())
4518 T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(),
4519 Args.size());
4520 else
4521 T = Context.getTemplateSpecializationType(Name, Args.data(),
4522 Args.size(), Underlying);
4523 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
4524 return T;
4525 }
4526
4527 case TYPE_ATOMIC: {
4528 if (Record.size() != 1) {
4529 Error("Incorrect encoding of atomic type");
4530 return QualType();
4531 }
4532 QualType ValueType = readType(*Loc.F, Record, Idx);
4533 return Context.getAtomicType(ValueType);
4534 }
4535 }
4536 llvm_unreachable("Invalid TypeCode!");
4537}
4538
4539class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
4540 ASTReader &Reader;
4541 ModuleFile &F;
4542 const ASTReader::RecordData &Record;
4543 unsigned &Idx;
4544
4545 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
4546 unsigned &I) {
4547 return Reader.ReadSourceLocation(F, R, I);
4548 }
4549
4550 template<typename T>
4551 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
4552 return Reader.ReadDeclAs<T>(F, Record, Idx);
4553 }
4554
4555public:
4556 TypeLocReader(ASTReader &Reader, ModuleFile &F,
4557 const ASTReader::RecordData &Record, unsigned &Idx)
4558 : Reader(Reader), F(F), Record(Record), Idx(Idx)
4559 { }
4560
4561 // We want compile-time assurance that we've enumerated all of
4562 // these, so unfortunately we have to declare them first, then
4563 // define them out-of-line.
4564#define ABSTRACT_TYPELOC(CLASS, PARENT)
4565#define TYPELOC(CLASS, PARENT) \
4566 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
4567#include "clang/AST/TypeLocNodes.def"
4568
4569 void VisitFunctionTypeLoc(FunctionTypeLoc);
4570 void VisitArrayTypeLoc(ArrayTypeLoc);
4571};
4572
4573void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
4574 // nothing to do
4575}
4576void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
4577 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
4578 if (TL.needsExtraLocalData()) {
4579 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
4580 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
4581 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
4582 TL.setModeAttr(Record[Idx++]);
4583 }
4584}
4585void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
4586 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4587}
4588void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
4589 TL.setStarLoc(ReadSourceLocation(Record, Idx));
4590}
4591void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
4592 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
4593}
4594void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
4595 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
4596}
4597void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
4598 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
4599}
4600void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
4601 TL.setStarLoc(ReadSourceLocation(Record, Idx));
4602 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
4603}
4604void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
4605 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
4606 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
4607 if (Record[Idx++])
4608 TL.setSizeExpr(Reader.ReadExpr(F));
4609 else
4610 TL.setSizeExpr(0);
4611}
4612void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
4613 VisitArrayTypeLoc(TL);
4614}
4615void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
4616 VisitArrayTypeLoc(TL);
4617}
4618void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
4619 VisitArrayTypeLoc(TL);
4620}
4621void TypeLocReader::VisitDependentSizedArrayTypeLoc(
4622 DependentSizedArrayTypeLoc TL) {
4623 VisitArrayTypeLoc(TL);
4624}
4625void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
4626 DependentSizedExtVectorTypeLoc TL) {
4627 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4628}
4629void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
4630 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4631}
4632void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
4633 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4634}
4635void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
4636 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
4637 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4638 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4639 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
4640 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
4641 TL.setArg(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
4642 }
4643}
4644void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
4645 VisitFunctionTypeLoc(TL);
4646}
4647void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
4648 VisitFunctionTypeLoc(TL);
4649}
4650void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
4651 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4652}
4653void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
4654 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4655}
4656void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
4657 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
4658 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4659 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4660}
4661void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
4662 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
4663 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4664 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4665 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
4666}
4667void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
4668 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4669}
4670void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
4671 TL.setKWLoc(ReadSourceLocation(Record, Idx));
4672 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4673 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4674 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
4675}
4676void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
4677 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4678}
4679void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
4680 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4681}
4682void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
4683 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4684}
4685void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
4686 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
4687 if (TL.hasAttrOperand()) {
4688 SourceRange range;
4689 range.setBegin(ReadSourceLocation(Record, Idx));
4690 range.setEnd(ReadSourceLocation(Record, Idx));
4691 TL.setAttrOperandParensRange(range);
4692 }
4693 if (TL.hasAttrExprOperand()) {
4694 if (Record[Idx++])
4695 TL.setAttrExprOperand(Reader.ReadExpr(F));
4696 else
4697 TL.setAttrExprOperand(0);
4698 } else if (TL.hasAttrEnumOperand())
4699 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
4700}
4701void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
4702 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4703}
4704void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
4705 SubstTemplateTypeParmTypeLoc TL) {
4706 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4707}
4708void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
4709 SubstTemplateTypeParmPackTypeLoc TL) {
4710 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4711}
4712void TypeLocReader::VisitTemplateSpecializationTypeLoc(
4713 TemplateSpecializationTypeLoc TL) {
4714 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
4715 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
4716 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4717 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
4718 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
4719 TL.setArgLocInfo(i,
4720 Reader.GetTemplateArgumentLocInfo(F,
4721 TL.getTypePtr()->getArg(i).getKind(),
4722 Record, Idx));
4723}
4724void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
4725 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4726 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4727}
4728void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
4729 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
4730 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
4731}
4732void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
4733 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4734}
4735void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
4736 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
4737 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
4738 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4739}
4740void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
4741 DependentTemplateSpecializationTypeLoc TL) {
4742 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
4743 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
4744 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
4745 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
4746 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4747 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
4748 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
4749 TL.setArgLocInfo(I,
4750 Reader.GetTemplateArgumentLocInfo(F,
4751 TL.getTypePtr()->getArg(I).getKind(),
4752 Record, Idx));
4753}
4754void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
4755 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
4756}
4757void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
4758 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4759}
4760void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
4761 TL.setHasBaseTypeAsWritten(Record[Idx++]);
4762 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4763 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
4764 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
4765 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
4766}
4767void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
4768 TL.setStarLoc(ReadSourceLocation(Record, Idx));
4769}
4770void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
4771 TL.setKWLoc(ReadSourceLocation(Record, Idx));
4772 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4773 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4774}
4775
4776TypeSourceInfo *ASTReader::GetTypeSourceInfo(ModuleFile &F,
4777 const RecordData &Record,
4778 unsigned &Idx) {
4779 QualType InfoTy = readType(F, Record, Idx);
4780 if (InfoTy.isNull())
4781 return 0;
4782
4783 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
4784 TypeLocReader TLR(*this, F, Record, Idx);
4785 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
4786 TLR.Visit(TL);
4787 return TInfo;
4788}
4789
4790QualType ASTReader::GetType(TypeID ID) {
4791 unsigned FastQuals = ID & Qualifiers::FastMask;
4792 unsigned Index = ID >> Qualifiers::FastWidth;
4793
4794 if (Index < NUM_PREDEF_TYPE_IDS) {
4795 QualType T;
4796 switch ((PredefinedTypeIDs)Index) {
4797 case PREDEF_TYPE_NULL_ID: return QualType();
4798 case PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
4799 case PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
4800
4801 case PREDEF_TYPE_CHAR_U_ID:
4802 case PREDEF_TYPE_CHAR_S_ID:
4803 // FIXME: Check that the signedness of CharTy is correct!
4804 T = Context.CharTy;
4805 break;
4806
4807 case PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
4808 case PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
4809 case PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
4810 case PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
4811 case PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
4812 case PREDEF_TYPE_UINT128_ID: T = Context.UnsignedInt128Ty; break;
4813 case PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
4814 case PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
4815 case PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
4816 case PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
4817 case PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
4818 case PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
4819 case PREDEF_TYPE_INT128_ID: T = Context.Int128Ty; break;
4820 case PREDEF_TYPE_HALF_ID: T = Context.HalfTy; break;
4821 case PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
4822 case PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
4823 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
4824 case PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
4825 case PREDEF_TYPE_BOUND_MEMBER: T = Context.BoundMemberTy; break;
4826 case PREDEF_TYPE_PSEUDO_OBJECT: T = Context.PseudoObjectTy; break;
4827 case PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
4828 case PREDEF_TYPE_UNKNOWN_ANY: T = Context.UnknownAnyTy; break;
4829 case PREDEF_TYPE_NULLPTR_ID: T = Context.NullPtrTy; break;
4830 case PREDEF_TYPE_CHAR16_ID: T = Context.Char16Ty; break;
4831 case PREDEF_TYPE_CHAR32_ID: T = Context.Char32Ty; break;
4832 case PREDEF_TYPE_OBJC_ID: T = Context.ObjCBuiltinIdTy; break;
4833 case PREDEF_TYPE_OBJC_CLASS: T = Context.ObjCBuiltinClassTy; break;
4834 case PREDEF_TYPE_OBJC_SEL: T = Context.ObjCBuiltinSelTy; break;
Guy Benyeib13621d2012-12-18 14:38:23 +00004835 case PREDEF_TYPE_IMAGE1D_ID: T = Context.OCLImage1dTy; break;
4836 case PREDEF_TYPE_IMAGE1D_ARR_ID: T = Context.OCLImage1dArrayTy; break;
4837 case PREDEF_TYPE_IMAGE1D_BUFF_ID: T = Context.OCLImage1dBufferTy; break;
4838 case PREDEF_TYPE_IMAGE2D_ID: T = Context.OCLImage2dTy; break;
4839 case PREDEF_TYPE_IMAGE2D_ARR_ID: T = Context.OCLImage2dArrayTy; break;
4840 case PREDEF_TYPE_IMAGE3D_ID: T = Context.OCLImage3dTy; break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004841 case PREDEF_TYPE_AUTO_DEDUCT: T = Context.getAutoDeductType(); break;
4842
4843 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
4844 T = Context.getAutoRRefDeductType();
4845 break;
4846
4847 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
4848 T = Context.ARCUnbridgedCastTy;
4849 break;
4850
4851 case PREDEF_TYPE_VA_LIST_TAG:
4852 T = Context.getVaListTagType();
4853 break;
4854
4855 case PREDEF_TYPE_BUILTIN_FN:
4856 T = Context.BuiltinFnTy;
4857 break;
4858 }
4859
4860 assert(!T.isNull() && "Unknown predefined type");
4861 return T.withFastQualifiers(FastQuals);
4862 }
4863
4864 Index -= NUM_PREDEF_TYPE_IDS;
4865 assert(Index < TypesLoaded.size() && "Type index out-of-range");
4866 if (TypesLoaded[Index].isNull()) {
4867 TypesLoaded[Index] = readTypeRecord(Index);
4868 if (TypesLoaded[Index].isNull())
4869 return QualType();
4870
4871 TypesLoaded[Index]->setFromAST();
4872 if (DeserializationListener)
4873 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
4874 TypesLoaded[Index]);
4875 }
4876
4877 return TypesLoaded[Index].withFastQualifiers(FastQuals);
4878}
4879
4880QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
4881 return GetType(getGlobalTypeID(F, LocalID));
4882}
4883
4884serialization::TypeID
4885ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
4886 unsigned FastQuals = LocalID & Qualifiers::FastMask;
4887 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
4888
4889 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
4890 return LocalID;
4891
4892 ContinuousRangeMap<uint32_t, int, 2>::iterator I
4893 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
4894 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
4895
4896 unsigned GlobalIndex = LocalIndex + I->second;
4897 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
4898}
4899
4900TemplateArgumentLocInfo
4901ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
4902 TemplateArgument::ArgKind Kind,
4903 const RecordData &Record,
4904 unsigned &Index) {
4905 switch (Kind) {
4906 case TemplateArgument::Expression:
4907 return ReadExpr(F);
4908 case TemplateArgument::Type:
4909 return GetTypeSourceInfo(F, Record, Index);
4910 case TemplateArgument::Template: {
4911 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
4912 Index);
4913 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
4914 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
4915 SourceLocation());
4916 }
4917 case TemplateArgument::TemplateExpansion: {
4918 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
4919 Index);
4920 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
4921 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
4922 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
4923 EllipsisLoc);
4924 }
4925 case TemplateArgument::Null:
4926 case TemplateArgument::Integral:
4927 case TemplateArgument::Declaration:
4928 case TemplateArgument::NullPtr:
4929 case TemplateArgument::Pack:
4930 // FIXME: Is this right?
4931 return TemplateArgumentLocInfo();
4932 }
4933 llvm_unreachable("unexpected template argument loc");
4934}
4935
4936TemplateArgumentLoc
4937ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
4938 const RecordData &Record, unsigned &Index) {
4939 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
4940
4941 if (Arg.getKind() == TemplateArgument::Expression) {
4942 if (Record[Index++]) // bool InfoHasSameExpr.
4943 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
4944 }
4945 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
4946 Record, Index));
4947}
4948
4949Decl *ASTReader::GetExternalDecl(uint32_t ID) {
4950 return GetDecl(ID);
4951}
4952
4953uint64_t ASTReader::readCXXBaseSpecifiers(ModuleFile &M, const RecordData &Record,
4954 unsigned &Idx){
4955 if (Idx >= Record.size())
4956 return 0;
4957
4958 unsigned LocalID = Record[Idx++];
4959 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
4960}
4961
4962CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
4963 RecordLocation Loc = getLocalBitOffset(Offset);
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004964 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004965 SavedStreamPosition SavedPosition(Cursor);
4966 Cursor.JumpToBit(Loc.Offset);
4967 ReadingKindTracker ReadingKind(Read_Decl, *this);
4968 RecordData Record;
4969 unsigned Code = Cursor.ReadCode();
4970 unsigned RecCode = Cursor.ReadRecord(Code, Record);
4971 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
4972 Error("Malformed AST file: missing C++ base specifiers");
4973 return 0;
4974 }
4975
4976 unsigned Idx = 0;
4977 unsigned NumBases = Record[Idx++];
4978 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
4979 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
4980 for (unsigned I = 0; I != NumBases; ++I)
4981 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
4982 return Bases;
4983}
4984
4985serialization::DeclID
4986ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
4987 if (LocalID < NUM_PREDEF_DECL_IDS)
4988 return LocalID;
4989
4990 ContinuousRangeMap<uint32_t, int, 2>::iterator I
4991 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
4992 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
4993
4994 return LocalID + I->second;
4995}
4996
4997bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
4998 ModuleFile &M) const {
4999 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(ID);
5000 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5001 return &M == I->second;
5002}
5003
5004ModuleFile *ASTReader::getOwningModuleFile(Decl *D) {
5005 if (!D->isFromASTFile())
5006 return 0;
5007 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
5008 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5009 return I->second;
5010}
5011
5012SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
5013 if (ID < NUM_PREDEF_DECL_IDS)
5014 return SourceLocation();
5015
5016 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
5017
5018 if (Index > DeclsLoaded.size()) {
5019 Error("declaration ID out-of-range for AST file");
5020 return SourceLocation();
5021 }
5022
5023 if (Decl *D = DeclsLoaded[Index])
5024 return D->getLocation();
5025
5026 unsigned RawLocation = 0;
5027 RecordLocation Rec = DeclCursorForID(ID, RawLocation);
5028 return ReadSourceLocation(*Rec.F, RawLocation);
5029}
5030
5031Decl *ASTReader::GetDecl(DeclID ID) {
5032 if (ID < NUM_PREDEF_DECL_IDS) {
5033 switch ((PredefinedDeclIDs)ID) {
5034 case PREDEF_DECL_NULL_ID:
5035 return 0;
5036
5037 case PREDEF_DECL_TRANSLATION_UNIT_ID:
5038 return Context.getTranslationUnitDecl();
5039
5040 case PREDEF_DECL_OBJC_ID_ID:
5041 return Context.getObjCIdDecl();
5042
5043 case PREDEF_DECL_OBJC_SEL_ID:
5044 return Context.getObjCSelDecl();
5045
5046 case PREDEF_DECL_OBJC_CLASS_ID:
5047 return Context.getObjCClassDecl();
5048
5049 case PREDEF_DECL_OBJC_PROTOCOL_ID:
5050 return Context.getObjCProtocolDecl();
5051
5052 case PREDEF_DECL_INT_128_ID:
5053 return Context.getInt128Decl();
5054
5055 case PREDEF_DECL_UNSIGNED_INT_128_ID:
5056 return Context.getUInt128Decl();
5057
5058 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
5059 return Context.getObjCInstanceTypeDecl();
5060
5061 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
5062 return Context.getBuiltinVaListDecl();
5063 }
5064 }
5065
5066 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
5067
5068 if (Index >= DeclsLoaded.size()) {
5069 assert(0 && "declaration ID out-of-range for AST file");
5070 Error("declaration ID out-of-range for AST file");
5071 return 0;
5072 }
5073
5074 if (!DeclsLoaded[Index]) {
5075 ReadDeclRecord(ID);
5076 if (DeserializationListener)
5077 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
5078 }
5079
5080 return DeclsLoaded[Index];
5081}
5082
5083DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
5084 DeclID GlobalID) {
5085 if (GlobalID < NUM_PREDEF_DECL_IDS)
5086 return GlobalID;
5087
5088 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
5089 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5090 ModuleFile *Owner = I->second;
5091
5092 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
5093 = M.GlobalToLocalDeclIDs.find(Owner);
5094 if (Pos == M.GlobalToLocalDeclIDs.end())
5095 return 0;
5096
5097 return GlobalID - Owner->BaseDeclID + Pos->second;
5098}
5099
5100serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
5101 const RecordData &Record,
5102 unsigned &Idx) {
5103 if (Idx >= Record.size()) {
5104 Error("Corrupted AST file");
5105 return 0;
5106 }
5107
5108 return getGlobalDeclID(F, Record[Idx++]);
5109}
5110
5111/// \brief Resolve the offset of a statement into a statement.
5112///
5113/// This operation will read a new statement from the external
5114/// source each time it is called, and is meant to be used via a
5115/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
5116Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
5117 // Switch case IDs are per Decl.
5118 ClearSwitchCaseIDs();
5119
5120 // Offset here is a global offset across the entire chain.
5121 RecordLocation Loc = getLocalBitOffset(Offset);
5122 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
5123 return ReadStmtFromStream(*Loc.F);
5124}
5125
5126namespace {
5127 class FindExternalLexicalDeclsVisitor {
5128 ASTReader &Reader;
5129 const DeclContext *DC;
5130 bool (*isKindWeWant)(Decl::Kind);
5131
5132 SmallVectorImpl<Decl*> &Decls;
5133 bool PredefsVisited[NUM_PREDEF_DECL_IDS];
5134
5135 public:
5136 FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC,
5137 bool (*isKindWeWant)(Decl::Kind),
5138 SmallVectorImpl<Decl*> &Decls)
5139 : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls)
5140 {
5141 for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I)
5142 PredefsVisited[I] = false;
5143 }
5144
5145 static bool visit(ModuleFile &M, bool Preorder, void *UserData) {
5146 if (Preorder)
5147 return false;
5148
5149 FindExternalLexicalDeclsVisitor *This
5150 = static_cast<FindExternalLexicalDeclsVisitor *>(UserData);
5151
5152 ModuleFile::DeclContextInfosMap::iterator Info
5153 = M.DeclContextInfos.find(This->DC);
5154 if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls)
5155 return false;
5156
5157 // Load all of the declaration IDs
5158 for (const KindDeclIDPair *ID = Info->second.LexicalDecls,
5159 *IDE = ID + Info->second.NumLexicalDecls;
5160 ID != IDE; ++ID) {
5161 if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first))
5162 continue;
5163
5164 // Don't add predefined declarations to the lexical context more
5165 // than once.
5166 if (ID->second < NUM_PREDEF_DECL_IDS) {
5167 if (This->PredefsVisited[ID->second])
5168 continue;
5169
5170 This->PredefsVisited[ID->second] = true;
5171 }
5172
5173 if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) {
5174 if (!This->DC->isDeclInLexicalTraversal(D))
5175 This->Decls.push_back(D);
5176 }
5177 }
5178
5179 return false;
5180 }
5181 };
5182}
5183
5184ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
5185 bool (*isKindWeWant)(Decl::Kind),
5186 SmallVectorImpl<Decl*> &Decls) {
5187 // There might be lexical decls in multiple modules, for the TU at
5188 // least. Walk all of the modules in the order they were loaded.
5189 FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls);
5190 ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor);
5191 ++NumLexicalDeclContextsRead;
5192 return ELR_Success;
5193}
5194
5195namespace {
5196
5197class DeclIDComp {
5198 ASTReader &Reader;
5199 ModuleFile &Mod;
5200
5201public:
5202 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
5203
5204 bool operator()(LocalDeclID L, LocalDeclID R) const {
5205 SourceLocation LHS = getLocation(L);
5206 SourceLocation RHS = getLocation(R);
5207 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5208 }
5209
5210 bool operator()(SourceLocation LHS, LocalDeclID R) const {
5211 SourceLocation RHS = getLocation(R);
5212 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5213 }
5214
5215 bool operator()(LocalDeclID L, SourceLocation RHS) const {
5216 SourceLocation LHS = getLocation(L);
5217 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5218 }
5219
5220 SourceLocation getLocation(LocalDeclID ID) const {
5221 return Reader.getSourceManager().getFileLoc(
5222 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
5223 }
5224};
5225
5226}
5227
5228void ASTReader::FindFileRegionDecls(FileID File,
5229 unsigned Offset, unsigned Length,
5230 SmallVectorImpl<Decl *> &Decls) {
5231 SourceManager &SM = getSourceManager();
5232
5233 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
5234 if (I == FileDeclIDs.end())
5235 return;
5236
5237 FileDeclsInfo &DInfo = I->second;
5238 if (DInfo.Decls.empty())
5239 return;
5240
5241 SourceLocation
5242 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
5243 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
5244
5245 DeclIDComp DIDComp(*this, *DInfo.Mod);
5246 ArrayRef<serialization::LocalDeclID>::iterator
5247 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
5248 BeginLoc, DIDComp);
5249 if (BeginIt != DInfo.Decls.begin())
5250 --BeginIt;
5251
5252 // If we are pointing at a top-level decl inside an objc container, we need
5253 // to backtrack until we find it otherwise we will fail to report that the
5254 // region overlaps with an objc container.
5255 while (BeginIt != DInfo.Decls.begin() &&
5256 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
5257 ->isTopLevelDeclInObjCContainer())
5258 --BeginIt;
5259
5260 ArrayRef<serialization::LocalDeclID>::iterator
5261 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
5262 EndLoc, DIDComp);
5263 if (EndIt != DInfo.Decls.end())
5264 ++EndIt;
5265
5266 for (ArrayRef<serialization::LocalDeclID>::iterator
5267 DIt = BeginIt; DIt != EndIt; ++DIt)
5268 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
5269}
5270
5271namespace {
5272 /// \brief ModuleFile visitor used to perform name lookup into a
5273 /// declaration context.
5274 class DeclContextNameLookupVisitor {
5275 ASTReader &Reader;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00005276 SmallVectorImpl<const DeclContext *> &Contexts;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005277 DeclarationName Name;
5278 SmallVectorImpl<NamedDecl *> &Decls;
5279
5280 public:
5281 DeclContextNameLookupVisitor(ASTReader &Reader,
5282 SmallVectorImpl<const DeclContext *> &Contexts,
5283 DeclarationName Name,
5284 SmallVectorImpl<NamedDecl *> &Decls)
5285 : Reader(Reader), Contexts(Contexts), Name(Name), Decls(Decls) { }
5286
5287 static bool visit(ModuleFile &M, void *UserData) {
5288 DeclContextNameLookupVisitor *This
5289 = static_cast<DeclContextNameLookupVisitor *>(UserData);
5290
5291 // Check whether we have any visible declaration information for
5292 // this context in this module.
5293 ModuleFile::DeclContextInfosMap::iterator Info;
5294 bool FoundInfo = false;
5295 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5296 Info = M.DeclContextInfos.find(This->Contexts[I]);
5297 if (Info != M.DeclContextInfos.end() &&
5298 Info->second.NameLookupTableData) {
5299 FoundInfo = true;
5300 break;
5301 }
5302 }
5303
5304 if (!FoundInfo)
5305 return false;
5306
5307 // Look for this name within this module.
5308 ASTDeclContextNameLookupTable *LookupTable =
5309 Info->second.NameLookupTableData;
5310 ASTDeclContextNameLookupTable::iterator Pos
5311 = LookupTable->find(This->Name);
5312 if (Pos == LookupTable->end())
5313 return false;
5314
5315 bool FoundAnything = false;
5316 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
5317 for (; Data.first != Data.second; ++Data.first) {
5318 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first);
5319 if (!ND)
5320 continue;
5321
5322 if (ND->getDeclName() != This->Name) {
5323 // A name might be null because the decl's redeclarable part is
5324 // currently read before reading its name. The lookup is triggered by
5325 // building that decl (likely indirectly), and so it is later in the
5326 // sense of "already existing" and can be ignored here.
5327 continue;
5328 }
5329
5330 // Record this declaration.
5331 FoundAnything = true;
5332 This->Decls.push_back(ND);
5333 }
5334
5335 return FoundAnything;
5336 }
5337 };
5338}
5339
5340DeclContext::lookup_result
5341ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
5342 DeclarationName Name) {
5343 assert(DC->hasExternalVisibleStorage() &&
5344 "DeclContext has no visible decls in storage");
5345 if (!Name)
5346 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
5347 DeclContext::lookup_iterator(0));
5348
5349 SmallVector<NamedDecl *, 64> Decls;
5350
5351 // Compute the declaration contexts we need to look into. Multiple such
5352 // declaration contexts occur when two declaration contexts from disjoint
5353 // modules get merged, e.g., when two namespaces with the same name are
5354 // independently defined in separate modules.
5355 SmallVector<const DeclContext *, 2> Contexts;
5356 Contexts.push_back(DC);
5357
5358 if (DC->isNamespace()) {
5359 MergedDeclsMap::iterator Merged
5360 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5361 if (Merged != MergedDecls.end()) {
5362 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5363 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5364 }
5365 }
5366
5367 DeclContextNameLookupVisitor Visitor(*this, Contexts, Name, Decls);
5368 ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor);
5369 ++NumVisibleDeclContextsRead;
5370 SetExternalVisibleDeclsForName(DC, Name, Decls);
5371 return const_cast<DeclContext*>(DC)->lookup(Name);
5372}
5373
5374namespace {
5375 /// \brief ModuleFile visitor used to retrieve all visible names in a
5376 /// declaration context.
5377 class DeclContextAllNamesVisitor {
5378 ASTReader &Reader;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00005379 SmallVectorImpl<const DeclContext *> &Contexts;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005380 llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> > &Decls;
Argyrios Kyrtzidisca40f302012-12-19 22:21:18 +00005381 bool VisitAll;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005382
5383 public:
5384 DeclContextAllNamesVisitor(ASTReader &Reader,
5385 SmallVectorImpl<const DeclContext *> &Contexts,
5386 llvm::DenseMap<DeclarationName,
Argyrios Kyrtzidisca40f302012-12-19 22:21:18 +00005387 SmallVector<NamedDecl *, 8> > &Decls,
5388 bool VisitAll)
5389 : Reader(Reader), Contexts(Contexts), Decls(Decls), VisitAll(VisitAll) { }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005390
5391 static bool visit(ModuleFile &M, void *UserData) {
5392 DeclContextAllNamesVisitor *This
5393 = static_cast<DeclContextAllNamesVisitor *>(UserData);
5394
5395 // Check whether we have any visible declaration information for
5396 // this context in this module.
5397 ModuleFile::DeclContextInfosMap::iterator Info;
5398 bool FoundInfo = false;
5399 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5400 Info = M.DeclContextInfos.find(This->Contexts[I]);
5401 if (Info != M.DeclContextInfos.end() &&
5402 Info->second.NameLookupTableData) {
5403 FoundInfo = true;
5404 break;
5405 }
5406 }
5407
5408 if (!FoundInfo)
5409 return false;
5410
5411 ASTDeclContextNameLookupTable *LookupTable =
5412 Info->second.NameLookupTableData;
5413 bool FoundAnything = false;
5414 for (ASTDeclContextNameLookupTable::data_iterator
5415 I = LookupTable->data_begin(), E = LookupTable->data_end();
5416 I != E; ++I) {
5417 ASTDeclContextNameLookupTrait::data_type Data = *I;
5418 for (; Data.first != Data.second; ++Data.first) {
5419 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M,
5420 *Data.first);
5421 if (!ND)
5422 continue;
5423
5424 // Record this declaration.
5425 FoundAnything = true;
5426 This->Decls[ND->getDeclName()].push_back(ND);
5427 }
5428 }
5429
Argyrios Kyrtzidisca40f302012-12-19 22:21:18 +00005430 return FoundAnything && !This->VisitAll;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005431 }
5432 };
5433}
5434
5435void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
5436 if (!DC->hasExternalVisibleStorage())
5437 return;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00005438 llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> > Decls;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005439
5440 // Compute the declaration contexts we need to look into. Multiple such
5441 // declaration contexts occur when two declaration contexts from disjoint
5442 // modules get merged, e.g., when two namespaces with the same name are
5443 // independently defined in separate modules.
5444 SmallVector<const DeclContext *, 2> Contexts;
5445 Contexts.push_back(DC);
5446
5447 if (DC->isNamespace()) {
5448 MergedDeclsMap::iterator Merged
5449 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5450 if (Merged != MergedDecls.end()) {
5451 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5452 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5453 }
5454 }
5455
Argyrios Kyrtzidisca40f302012-12-19 22:21:18 +00005456 DeclContextAllNamesVisitor Visitor(*this, Contexts, Decls,
5457 /*VisitAll=*/DC->isFileContext());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005458 ModuleMgr.visit(&DeclContextAllNamesVisitor::visit, &Visitor);
5459 ++NumVisibleDeclContextsRead;
5460
5461 for (llvm::DenseMap<DeclarationName,
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00005462 SmallVector<NamedDecl *, 8> >::iterator
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005463 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
5464 SetExternalVisibleDeclsForName(DC, I->first, I->second);
5465 }
5466 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
5467}
5468
5469/// \brief Under non-PCH compilation the consumer receives the objc methods
5470/// before receiving the implementation, and codegen depends on this.
5471/// We simulate this by deserializing and passing to consumer the methods of the
5472/// implementation before passing the deserialized implementation decl.
5473static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
5474 ASTConsumer *Consumer) {
5475 assert(ImplD && Consumer);
5476
5477 for (ObjCImplDecl::method_iterator
5478 I = ImplD->meth_begin(), E = ImplD->meth_end(); I != E; ++I)
5479 Consumer->HandleInterestingDecl(DeclGroupRef(*I));
5480
5481 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
5482}
5483
5484void ASTReader::PassInterestingDeclsToConsumer() {
5485 assert(Consumer);
5486 while (!InterestingDecls.empty()) {
5487 Decl *D = InterestingDecls.front();
5488 InterestingDecls.pop_front();
5489
5490 PassInterestingDeclToConsumer(D);
5491 }
5492}
5493
5494void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
5495 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
5496 PassObjCImplDeclToConsumer(ImplD, Consumer);
5497 else
5498 Consumer->HandleInterestingDecl(DeclGroupRef(D));
5499}
5500
5501void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
5502 this->Consumer = Consumer;
5503
5504 if (!Consumer)
5505 return;
5506
5507 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
5508 // Force deserialization of this decl, which will cause it to be queued for
5509 // passing to the consumer.
5510 GetDecl(ExternalDefinitions[I]);
5511 }
5512 ExternalDefinitions.clear();
5513
5514 PassInterestingDeclsToConsumer();
5515}
5516
5517void ASTReader::PrintStats() {
5518 std::fprintf(stderr, "*** AST File Statistics:\n");
5519
5520 unsigned NumTypesLoaded
5521 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
5522 QualType());
5523 unsigned NumDeclsLoaded
5524 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
5525 (Decl *)0);
5526 unsigned NumIdentifiersLoaded
5527 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
5528 IdentifiersLoaded.end(),
5529 (IdentifierInfo *)0);
5530 unsigned NumMacrosLoaded
5531 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
5532 MacrosLoaded.end(),
5533 (MacroInfo *)0);
5534 unsigned NumSelectorsLoaded
5535 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
5536 SelectorsLoaded.end(),
5537 Selector());
5538
5539 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
5540 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
5541 NumSLocEntriesRead, TotalNumSLocEntries,
5542 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
5543 if (!TypesLoaded.empty())
5544 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
5545 NumTypesLoaded, (unsigned)TypesLoaded.size(),
5546 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
5547 if (!DeclsLoaded.empty())
5548 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
5549 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
5550 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
5551 if (!IdentifiersLoaded.empty())
5552 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
5553 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
5554 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
5555 if (!MacrosLoaded.empty())
5556 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
5557 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
5558 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
5559 if (!SelectorsLoaded.empty())
5560 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
5561 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
5562 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
5563 if (TotalNumStatements)
5564 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
5565 NumStatementsRead, TotalNumStatements,
5566 ((float)NumStatementsRead/TotalNumStatements * 100));
5567 if (TotalNumMacros)
5568 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
5569 NumMacrosRead, TotalNumMacros,
5570 ((float)NumMacrosRead/TotalNumMacros * 100));
5571 if (TotalLexicalDeclContexts)
5572 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
5573 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
5574 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
5575 * 100));
5576 if (TotalVisibleDeclContexts)
5577 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
5578 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
5579 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
5580 * 100));
5581 if (TotalNumMethodPoolEntries) {
5582 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
5583 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
5584 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
5585 * 100));
5586 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
5587 }
5588 std::fprintf(stderr, "\n");
5589 dump();
5590 std::fprintf(stderr, "\n");
5591}
5592
5593template<typename Key, typename ModuleFile, unsigned InitialCapacity>
5594static void
5595dumpModuleIDMap(StringRef Name,
5596 const ContinuousRangeMap<Key, ModuleFile *,
5597 InitialCapacity> &Map) {
5598 if (Map.begin() == Map.end())
5599 return;
5600
5601 typedef ContinuousRangeMap<Key, ModuleFile *, InitialCapacity> MapType;
5602 llvm::errs() << Name << ":\n";
5603 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
5604 I != IEnd; ++I) {
5605 llvm::errs() << " " << I->first << " -> " << I->second->FileName
5606 << "\n";
5607 }
5608}
5609
5610void ASTReader::dump() {
5611 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
5612 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
5613 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
5614 dumpModuleIDMap("Global type map", GlobalTypeMap);
5615 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
5616 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
5617 dumpModuleIDMap("Global macro map", GlobalMacroMap);
5618 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
5619 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
5620 dumpModuleIDMap("Global preprocessed entity map",
5621 GlobalPreprocessedEntityMap);
5622
5623 llvm::errs() << "\n*** PCH/Modules Loaded:";
5624 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
5625 MEnd = ModuleMgr.end();
5626 M != MEnd; ++M)
5627 (*M)->dump();
5628}
5629
5630/// Return the amount of memory used by memory buffers, breaking down
5631/// by heap-backed versus mmap'ed memory.
5632void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
5633 for (ModuleConstIterator I = ModuleMgr.begin(),
5634 E = ModuleMgr.end(); I != E; ++I) {
5635 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
5636 size_t bytes = buf->getBufferSize();
5637 switch (buf->getBufferKind()) {
5638 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
5639 sizes.malloc_bytes += bytes;
5640 break;
5641 case llvm::MemoryBuffer::MemoryBuffer_MMap:
5642 sizes.mmap_bytes += bytes;
5643 break;
5644 }
5645 }
5646 }
5647}
5648
5649void ASTReader::InitializeSema(Sema &S) {
5650 SemaObj = &S;
5651 S.addExternalSource(this);
5652
5653 // Makes sure any declarations that were deserialized "too early"
5654 // still get added to the identifier's declaration chains.
5655 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
5656 SemaObj->pushExternalDeclIntoScope(PreloadedDecls[I],
5657 PreloadedDecls[I]->getDeclName());
5658 }
5659 PreloadedDecls.clear();
5660
5661 // Load the offsets of the declarations that Sema references.
5662 // They will be lazily deserialized when needed.
5663 if (!SemaDeclRefs.empty()) {
5664 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
5665 if (!SemaObj->StdNamespace)
5666 SemaObj->StdNamespace = SemaDeclRefs[0];
5667 if (!SemaObj->StdBadAlloc)
5668 SemaObj->StdBadAlloc = SemaDeclRefs[1];
5669 }
5670
5671 if (!FPPragmaOptions.empty()) {
5672 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
5673 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
5674 }
5675
5676 if (!OpenCLExtensions.empty()) {
5677 unsigned I = 0;
5678#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
5679#include "clang/Basic/OpenCLExtensions.def"
5680
5681 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
5682 }
5683}
5684
5685IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
5686 // Note that we are loading an identifier.
5687 Deserializing AnIdentifier(this);
5688
5689 IdentifierLookupVisitor Visitor(StringRef(NameStart, NameEnd - NameStart),
5690 /*PriorGeneration=*/0);
5691 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
5692 IdentifierInfo *II = Visitor.getIdentifierInfo();
5693 markIdentifierUpToDate(II);
5694 return II;
5695}
5696
5697namespace clang {
5698 /// \brief An identifier-lookup iterator that enumerates all of the
5699 /// identifiers stored within a set of AST files.
5700 class ASTIdentifierIterator : public IdentifierIterator {
5701 /// \brief The AST reader whose identifiers are being enumerated.
5702 const ASTReader &Reader;
5703
5704 /// \brief The current index into the chain of AST files stored in
5705 /// the AST reader.
5706 unsigned Index;
5707
5708 /// \brief The current position within the identifier lookup table
5709 /// of the current AST file.
5710 ASTIdentifierLookupTable::key_iterator Current;
5711
5712 /// \brief The end position within the identifier lookup table of
5713 /// the current AST file.
5714 ASTIdentifierLookupTable::key_iterator End;
5715
5716 public:
5717 explicit ASTIdentifierIterator(const ASTReader &Reader);
5718
5719 virtual StringRef Next();
5720 };
5721}
5722
5723ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
5724 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
5725 ASTIdentifierLookupTable *IdTable
5726 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
5727 Current = IdTable->key_begin();
5728 End = IdTable->key_end();
5729}
5730
5731StringRef ASTIdentifierIterator::Next() {
5732 while (Current == End) {
5733 // If we have exhausted all of our AST files, we're done.
5734 if (Index == 0)
5735 return StringRef();
5736
5737 --Index;
5738 ASTIdentifierLookupTable *IdTable
5739 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
5740 IdentifierLookupTable;
5741 Current = IdTable->key_begin();
5742 End = IdTable->key_end();
5743 }
5744
5745 // We have any identifiers remaining in the current AST file; return
5746 // the next one.
5747 std::pair<const char*, unsigned> Key = *Current;
5748 ++Current;
5749 return StringRef(Key.first, Key.second);
5750}
5751
5752IdentifierIterator *ASTReader::getIdentifiers() const {
5753 return new ASTIdentifierIterator(*this);
5754}
5755
5756namespace clang { namespace serialization {
5757 class ReadMethodPoolVisitor {
5758 ASTReader &Reader;
5759 Selector Sel;
5760 unsigned PriorGeneration;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00005761 SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
5762 SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005763
5764 public:
5765 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
5766 unsigned PriorGeneration)
5767 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) { }
5768
5769 static bool visit(ModuleFile &M, void *UserData) {
5770 ReadMethodPoolVisitor *This
5771 = static_cast<ReadMethodPoolVisitor *>(UserData);
5772
5773 if (!M.SelectorLookupTable)
5774 return false;
5775
5776 // If we've already searched this module file, skip it now.
5777 if (M.Generation <= This->PriorGeneration)
5778 return true;
5779
5780 ASTSelectorLookupTable *PoolTable
5781 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
5782 ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel);
5783 if (Pos == PoolTable->end())
5784 return false;
5785
5786 ++This->Reader.NumSelectorsRead;
5787 // FIXME: Not quite happy with the statistics here. We probably should
5788 // disable this tracking when called via LoadSelector.
5789 // Also, should entries without methods count as misses?
5790 ++This->Reader.NumMethodPoolEntriesRead;
5791 ASTSelectorLookupTrait::data_type Data = *Pos;
5792 if (This->Reader.DeserializationListener)
5793 This->Reader.DeserializationListener->SelectorRead(Data.ID,
5794 This->Sel);
5795
5796 This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
5797 This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
5798 return true;
5799 }
5800
5801 /// \brief Retrieve the instance methods found by this visitor.
5802 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
5803 return InstanceMethods;
5804 }
5805
5806 /// \brief Retrieve the instance methods found by this visitor.
5807 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
5808 return FactoryMethods;
5809 }
5810 };
5811} } // end namespace clang::serialization
5812
5813/// \brief Add the given set of methods to the method list.
5814static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
5815 ObjCMethodList &List) {
5816 for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
5817 S.addMethodToGlobalList(&List, Methods[I]);
5818 }
5819}
5820
5821void ASTReader::ReadMethodPool(Selector Sel) {
5822 // Get the selector generation and update it to the current generation.
5823 unsigned &Generation = SelectorGeneration[Sel];
5824 unsigned PriorGeneration = Generation;
5825 Generation = CurrentGeneration;
5826
5827 // Search for methods defined with this selector.
5828 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
5829 ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor);
5830
5831 if (Visitor.getInstanceMethods().empty() &&
5832 Visitor.getFactoryMethods().empty()) {
5833 ++NumMethodPoolMisses;
5834 return;
5835 }
5836
5837 if (!getSema())
5838 return;
5839
5840 Sema &S = *getSema();
5841 Sema::GlobalMethodPool::iterator Pos
5842 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
5843
5844 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
5845 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
5846}
5847
5848void ASTReader::ReadKnownNamespaces(
5849 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
5850 Namespaces.clear();
5851
5852 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
5853 if (NamespaceDecl *Namespace
5854 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
5855 Namespaces.push_back(Namespace);
5856 }
5857}
5858
5859void ASTReader::ReadTentativeDefinitions(
5860 SmallVectorImpl<VarDecl *> &TentativeDefs) {
5861 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
5862 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
5863 if (Var)
5864 TentativeDefs.push_back(Var);
5865 }
5866 TentativeDefinitions.clear();
5867}
5868
5869void ASTReader::ReadUnusedFileScopedDecls(
5870 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
5871 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
5872 DeclaratorDecl *D
5873 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
5874 if (D)
5875 Decls.push_back(D);
5876 }
5877 UnusedFileScopedDecls.clear();
5878}
5879
5880void ASTReader::ReadDelegatingConstructors(
5881 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
5882 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
5883 CXXConstructorDecl *D
5884 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
5885 if (D)
5886 Decls.push_back(D);
5887 }
5888 DelegatingCtorDecls.clear();
5889}
5890
5891void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
5892 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
5893 TypedefNameDecl *D
5894 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
5895 if (D)
5896 Decls.push_back(D);
5897 }
5898 ExtVectorDecls.clear();
5899}
5900
5901void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) {
5902 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
5903 CXXRecordDecl *D
5904 = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I]));
5905 if (D)
5906 Decls.push_back(D);
5907 }
5908 DynamicClasses.clear();
5909}
5910
5911void
Richard Smith5ea6ef42013-01-10 23:43:47 +00005912ASTReader::ReadLocallyScopedExternCDecls(SmallVectorImpl<NamedDecl *> &Decls) {
5913 for (unsigned I = 0, N = LocallyScopedExternCDecls.size(); I != N; ++I) {
5914 NamedDecl *D
5915 = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternCDecls[I]));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005916 if (D)
5917 Decls.push_back(D);
5918 }
Richard Smith5ea6ef42013-01-10 23:43:47 +00005919 LocallyScopedExternCDecls.clear();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005920}
5921
5922void ASTReader::ReadReferencedSelectors(
5923 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
5924 if (ReferencedSelectorsData.empty())
5925 return;
5926
5927 // If there are @selector references added them to its pool. This is for
5928 // implementation of -Wselector.
5929 unsigned int DataSize = ReferencedSelectorsData.size()-1;
5930 unsigned I = 0;
5931 while (I < DataSize) {
5932 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
5933 SourceLocation SelLoc
5934 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
5935 Sels.push_back(std::make_pair(Sel, SelLoc));
5936 }
5937 ReferencedSelectorsData.clear();
5938}
5939
5940void ASTReader::ReadWeakUndeclaredIdentifiers(
5941 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
5942 if (WeakUndeclaredIdentifiers.empty())
5943 return;
5944
5945 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
5946 IdentifierInfo *WeakId
5947 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
5948 IdentifierInfo *AliasId
5949 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
5950 SourceLocation Loc
5951 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
5952 bool Used = WeakUndeclaredIdentifiers[I++];
5953 WeakInfo WI(AliasId, Loc);
5954 WI.setUsed(Used);
5955 WeakIDs.push_back(std::make_pair(WeakId, WI));
5956 }
5957 WeakUndeclaredIdentifiers.clear();
5958}
5959
5960void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
5961 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
5962 ExternalVTableUse VT;
5963 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
5964 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
5965 VT.DefinitionRequired = VTableUses[Idx++];
5966 VTables.push_back(VT);
5967 }
5968
5969 VTableUses.clear();
5970}
5971
5972void ASTReader::ReadPendingInstantiations(
5973 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
5974 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
5975 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
5976 SourceLocation Loc
5977 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
5978
5979 Pending.push_back(std::make_pair(D, Loc));
5980 }
5981 PendingInstantiations.clear();
5982}
5983
5984void ASTReader::LoadSelector(Selector Sel) {
5985 // It would be complicated to avoid reading the methods anyway. So don't.
5986 ReadMethodPool(Sel);
5987}
5988
5989void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
5990 assert(ID && "Non-zero identifier ID required");
5991 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
5992 IdentifiersLoaded[ID - 1] = II;
5993 if (DeserializationListener)
5994 DeserializationListener->IdentifierRead(ID, II);
5995}
5996
5997/// \brief Set the globally-visible declarations associated with the given
5998/// identifier.
5999///
6000/// If the AST reader is currently in a state where the given declaration IDs
6001/// cannot safely be resolved, they are queued until it is safe to resolve
6002/// them.
6003///
6004/// \param II an IdentifierInfo that refers to one or more globally-visible
6005/// declarations.
6006///
6007/// \param DeclIDs the set of declaration IDs with the name @p II that are
6008/// visible at global scope.
6009///
6010/// \param Nonrecursive should be true to indicate that the caller knows that
6011/// this call is non-recursive, and therefore the globally-visible declarations
6012/// will not be placed onto the pending queue.
6013void
6014ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
6015 const SmallVectorImpl<uint32_t> &DeclIDs,
6016 bool Nonrecursive) {
6017 if (NumCurrentElementsDeserializing && !Nonrecursive) {
6018 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
6019 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
6020 PII.II = II;
6021 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
6022 return;
6023 }
6024
6025 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
6026 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
6027 if (SemaObj) {
6028 // Introduce this declaration into the translation-unit scope
6029 // and add it to the declaration chain for this identifier, so
6030 // that (unqualified) name lookup will find it.
6031 SemaObj->pushExternalDeclIntoScope(D, II);
6032 } else {
6033 // Queue this declaration so that it will be added to the
6034 // translation unit scope and identifier's declaration chain
6035 // once a Sema object is known.
6036 PreloadedDecls.push_back(D);
6037 }
6038 }
6039}
6040
6041IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
6042 if (ID == 0)
6043 return 0;
6044
6045 if (IdentifiersLoaded.empty()) {
6046 Error("no identifier table in AST file");
6047 return 0;
6048 }
6049
6050 ID -= 1;
6051 if (!IdentifiersLoaded[ID]) {
6052 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
6053 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
6054 ModuleFile *M = I->second;
6055 unsigned Index = ID - M->BaseIdentifierID;
6056 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
6057
6058 // All of the strings in the AST file are preceded by a 16-bit length.
6059 // Extract that 16-bit length to avoid having to execute strlen().
6060 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
6061 // unsigned integers. This is important to avoid integer overflow when
6062 // we cast them to 'unsigned'.
6063 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
6064 unsigned StrLen = (((unsigned) StrLenPtr[0])
6065 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
6066 IdentifiersLoaded[ID]
6067 = &PP.getIdentifierTable().get(StringRef(Str, StrLen));
6068 if (DeserializationListener)
6069 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
6070 }
6071
6072 return IdentifiersLoaded[ID];
6073}
6074
6075IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
6076 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
6077}
6078
6079IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
6080 if (LocalID < NUM_PREDEF_IDENT_IDS)
6081 return LocalID;
6082
6083 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6084 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
6085 assert(I != M.IdentifierRemap.end()
6086 && "Invalid index into identifier index remap");
6087
6088 return LocalID + I->second;
6089}
6090
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00006091MacroInfo *ASTReader::getMacro(MacroID ID, MacroInfo *Hint) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006092 if (ID == 0)
6093 return 0;
6094
6095 if (MacrosLoaded.empty()) {
6096 Error("no macro table in AST file");
6097 return 0;
6098 }
6099
6100 ID -= NUM_PREDEF_MACRO_IDS;
6101 if (!MacrosLoaded[ID]) {
6102 GlobalMacroMapType::iterator I
6103 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
6104 assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
6105 ModuleFile *M = I->second;
6106 unsigned Index = ID - M->BaseMacroID;
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00006107 ReadMacroRecord(*M, M->MacroOffsets[Index], Hint);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006108 }
6109
6110 return MacrosLoaded[ID];
6111}
6112
6113MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
6114 if (LocalID < NUM_PREDEF_MACRO_IDS)
6115 return LocalID;
6116
6117 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6118 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
6119 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
6120
6121 return LocalID + I->second;
6122}
6123
6124serialization::SubmoduleID
6125ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
6126 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
6127 return LocalID;
6128
6129 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6130 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
6131 assert(I != M.SubmoduleRemap.end()
6132 && "Invalid index into submodule index remap");
6133
6134 return LocalID + I->second;
6135}
6136
6137Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
6138 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
6139 assert(GlobalID == 0 && "Unhandled global submodule ID");
6140 return 0;
6141 }
6142
6143 if (GlobalID > SubmodulesLoaded.size()) {
6144 Error("submodule ID out of range in AST file");
6145 return 0;
6146 }
6147
6148 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
6149}
Douglas Gregorca2ab452013-01-12 01:29:50 +00006150
6151Module *ASTReader::getModule(unsigned ID) {
6152 return getSubmodule(ID);
6153}
6154
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006155Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
6156 return DecodeSelector(getGlobalSelectorID(M, LocalID));
6157}
6158
6159Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
6160 if (ID == 0)
6161 return Selector();
6162
6163 if (ID > SelectorsLoaded.size()) {
6164 Error("selector ID out of range in AST file");
6165 return Selector();
6166 }
6167
6168 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
6169 // Load this selector from the selector table.
6170 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
6171 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
6172 ModuleFile &M = *I->second;
6173 ASTSelectorLookupTrait Trait(*this, M);
6174 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
6175 SelectorsLoaded[ID - 1] =
6176 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
6177 if (DeserializationListener)
6178 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
6179 }
6180
6181 return SelectorsLoaded[ID - 1];
6182}
6183
6184Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
6185 return DecodeSelector(ID);
6186}
6187
6188uint32_t ASTReader::GetNumExternalSelectors() {
6189 // ID 0 (the null selector) is considered an external selector.
6190 return getTotalNumSelectors() + 1;
6191}
6192
6193serialization::SelectorID
6194ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
6195 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
6196 return LocalID;
6197
6198 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6199 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
6200 assert(I != M.SelectorRemap.end()
6201 && "Invalid index into selector index remap");
6202
6203 return LocalID + I->second;
6204}
6205
6206DeclarationName
6207ASTReader::ReadDeclarationName(ModuleFile &F,
6208 const RecordData &Record, unsigned &Idx) {
6209 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
6210 switch (Kind) {
6211 case DeclarationName::Identifier:
6212 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
6213
6214 case DeclarationName::ObjCZeroArgSelector:
6215 case DeclarationName::ObjCOneArgSelector:
6216 case DeclarationName::ObjCMultiArgSelector:
6217 return DeclarationName(ReadSelector(F, Record, Idx));
6218
6219 case DeclarationName::CXXConstructorName:
6220 return Context.DeclarationNames.getCXXConstructorName(
6221 Context.getCanonicalType(readType(F, Record, Idx)));
6222
6223 case DeclarationName::CXXDestructorName:
6224 return Context.DeclarationNames.getCXXDestructorName(
6225 Context.getCanonicalType(readType(F, Record, Idx)));
6226
6227 case DeclarationName::CXXConversionFunctionName:
6228 return Context.DeclarationNames.getCXXConversionFunctionName(
6229 Context.getCanonicalType(readType(F, Record, Idx)));
6230
6231 case DeclarationName::CXXOperatorName:
6232 return Context.DeclarationNames.getCXXOperatorName(
6233 (OverloadedOperatorKind)Record[Idx++]);
6234
6235 case DeclarationName::CXXLiteralOperatorName:
6236 return Context.DeclarationNames.getCXXLiteralOperatorName(
6237 GetIdentifierInfo(F, Record, Idx));
6238
6239 case DeclarationName::CXXUsingDirective:
6240 return DeclarationName::getUsingDirectiveName();
6241 }
6242
6243 llvm_unreachable("Invalid NameKind!");
6244}
6245
6246void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
6247 DeclarationNameLoc &DNLoc,
6248 DeclarationName Name,
6249 const RecordData &Record, unsigned &Idx) {
6250 switch (Name.getNameKind()) {
6251 case DeclarationName::CXXConstructorName:
6252 case DeclarationName::CXXDestructorName:
6253 case DeclarationName::CXXConversionFunctionName:
6254 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
6255 break;
6256
6257 case DeclarationName::CXXOperatorName:
6258 DNLoc.CXXOperatorName.BeginOpNameLoc
6259 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6260 DNLoc.CXXOperatorName.EndOpNameLoc
6261 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6262 break;
6263
6264 case DeclarationName::CXXLiteralOperatorName:
6265 DNLoc.CXXLiteralOperatorName.OpNameLoc
6266 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6267 break;
6268
6269 case DeclarationName::Identifier:
6270 case DeclarationName::ObjCZeroArgSelector:
6271 case DeclarationName::ObjCOneArgSelector:
6272 case DeclarationName::ObjCMultiArgSelector:
6273 case DeclarationName::CXXUsingDirective:
6274 break;
6275 }
6276}
6277
6278void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
6279 DeclarationNameInfo &NameInfo,
6280 const RecordData &Record, unsigned &Idx) {
6281 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
6282 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
6283 DeclarationNameLoc DNLoc;
6284 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
6285 NameInfo.setInfo(DNLoc);
6286}
6287
6288void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
6289 const RecordData &Record, unsigned &Idx) {
6290 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
6291 unsigned NumTPLists = Record[Idx++];
6292 Info.NumTemplParamLists = NumTPLists;
6293 if (NumTPLists) {
6294 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
6295 for (unsigned i=0; i != NumTPLists; ++i)
6296 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
6297 }
6298}
6299
6300TemplateName
6301ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
6302 unsigned &Idx) {
6303 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
6304 switch (Kind) {
6305 case TemplateName::Template:
6306 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
6307
6308 case TemplateName::OverloadedTemplate: {
6309 unsigned size = Record[Idx++];
6310 UnresolvedSet<8> Decls;
6311 while (size--)
6312 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
6313
6314 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
6315 }
6316
6317 case TemplateName::QualifiedTemplate: {
6318 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
6319 bool hasTemplKeyword = Record[Idx++];
6320 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
6321 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
6322 }
6323
6324 case TemplateName::DependentTemplate: {
6325 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
6326 if (Record[Idx++]) // isIdentifier
6327 return Context.getDependentTemplateName(NNS,
6328 GetIdentifierInfo(F, Record,
6329 Idx));
6330 return Context.getDependentTemplateName(NNS,
6331 (OverloadedOperatorKind)Record[Idx++]);
6332 }
6333
6334 case TemplateName::SubstTemplateTemplateParm: {
6335 TemplateTemplateParmDecl *param
6336 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
6337 if (!param) return TemplateName();
6338 TemplateName replacement = ReadTemplateName(F, Record, Idx);
6339 return Context.getSubstTemplateTemplateParm(param, replacement);
6340 }
6341
6342 case TemplateName::SubstTemplateTemplateParmPack: {
6343 TemplateTemplateParmDecl *Param
6344 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
6345 if (!Param)
6346 return TemplateName();
6347
6348 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
6349 if (ArgPack.getKind() != TemplateArgument::Pack)
6350 return TemplateName();
6351
6352 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
6353 }
6354 }
6355
6356 llvm_unreachable("Unhandled template name kind!");
6357}
6358
6359TemplateArgument
6360ASTReader::ReadTemplateArgument(ModuleFile &F,
6361 const RecordData &Record, unsigned &Idx) {
6362 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
6363 switch (Kind) {
6364 case TemplateArgument::Null:
6365 return TemplateArgument();
6366 case TemplateArgument::Type:
6367 return TemplateArgument(readType(F, Record, Idx));
6368 case TemplateArgument::Declaration: {
6369 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
6370 bool ForReferenceParam = Record[Idx++];
6371 return TemplateArgument(D, ForReferenceParam);
6372 }
6373 case TemplateArgument::NullPtr:
6374 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
6375 case TemplateArgument::Integral: {
6376 llvm::APSInt Value = ReadAPSInt(Record, Idx);
6377 QualType T = readType(F, Record, Idx);
6378 return TemplateArgument(Context, Value, T);
6379 }
6380 case TemplateArgument::Template:
6381 return TemplateArgument(ReadTemplateName(F, Record, Idx));
6382 case TemplateArgument::TemplateExpansion: {
6383 TemplateName Name = ReadTemplateName(F, Record, Idx);
6384 llvm::Optional<unsigned> NumTemplateExpansions;
6385 if (unsigned NumExpansions = Record[Idx++])
6386 NumTemplateExpansions = NumExpansions - 1;
6387 return TemplateArgument(Name, NumTemplateExpansions);
6388 }
6389 case TemplateArgument::Expression:
6390 return TemplateArgument(ReadExpr(F));
6391 case TemplateArgument::Pack: {
6392 unsigned NumArgs = Record[Idx++];
6393 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
6394 for (unsigned I = 0; I != NumArgs; ++I)
6395 Args[I] = ReadTemplateArgument(F, Record, Idx);
6396 return TemplateArgument(Args, NumArgs);
6397 }
6398 }
6399
6400 llvm_unreachable("Unhandled template argument kind!");
6401}
6402
6403TemplateParameterList *
6404ASTReader::ReadTemplateParameterList(ModuleFile &F,
6405 const RecordData &Record, unsigned &Idx) {
6406 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
6407 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
6408 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
6409
6410 unsigned NumParams = Record[Idx++];
6411 SmallVector<NamedDecl *, 16> Params;
6412 Params.reserve(NumParams);
6413 while (NumParams--)
6414 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
6415
6416 TemplateParameterList* TemplateParams =
6417 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
6418 Params.data(), Params.size(), RAngleLoc);
6419 return TemplateParams;
6420}
6421
6422void
6423ASTReader::
6424ReadTemplateArgumentList(SmallVector<TemplateArgument, 8> &TemplArgs,
6425 ModuleFile &F, const RecordData &Record,
6426 unsigned &Idx) {
6427 unsigned NumTemplateArgs = Record[Idx++];
6428 TemplArgs.reserve(NumTemplateArgs);
6429 while (NumTemplateArgs--)
6430 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
6431}
6432
6433/// \brief Read a UnresolvedSet structure.
6434void ASTReader::ReadUnresolvedSet(ModuleFile &F, ASTUnresolvedSet &Set,
6435 const RecordData &Record, unsigned &Idx) {
6436 unsigned NumDecls = Record[Idx++];
6437 Set.reserve(Context, NumDecls);
6438 while (NumDecls--) {
6439 NamedDecl *D = ReadDeclAs<NamedDecl>(F, Record, Idx);
6440 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
6441 Set.addDecl(Context, D, AS);
6442 }
6443}
6444
6445CXXBaseSpecifier
6446ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
6447 const RecordData &Record, unsigned &Idx) {
6448 bool isVirtual = static_cast<bool>(Record[Idx++]);
6449 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
6450 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
6451 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
6452 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
6453 SourceRange Range = ReadSourceRange(F, Record, Idx);
6454 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
6455 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
6456 EllipsisLoc);
6457 Result.setInheritConstructors(inheritConstructors);
6458 return Result;
6459}
6460
6461std::pair<CXXCtorInitializer **, unsigned>
6462ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
6463 unsigned &Idx) {
6464 CXXCtorInitializer **CtorInitializers = 0;
6465 unsigned NumInitializers = Record[Idx++];
6466 if (NumInitializers) {
6467 CtorInitializers
6468 = new (Context) CXXCtorInitializer*[NumInitializers];
6469 for (unsigned i=0; i != NumInitializers; ++i) {
6470 TypeSourceInfo *TInfo = 0;
6471 bool IsBaseVirtual = false;
6472 FieldDecl *Member = 0;
6473 IndirectFieldDecl *IndirectMember = 0;
6474
6475 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
6476 switch (Type) {
6477 case CTOR_INITIALIZER_BASE:
6478 TInfo = GetTypeSourceInfo(F, Record, Idx);
6479 IsBaseVirtual = Record[Idx++];
6480 break;
6481
6482 case CTOR_INITIALIZER_DELEGATING:
6483 TInfo = GetTypeSourceInfo(F, Record, Idx);
6484 break;
6485
6486 case CTOR_INITIALIZER_MEMBER:
6487 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
6488 break;
6489
6490 case CTOR_INITIALIZER_INDIRECT_MEMBER:
6491 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
6492 break;
6493 }
6494
6495 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
6496 Expr *Init = ReadExpr(F);
6497 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
6498 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
6499 bool IsWritten = Record[Idx++];
6500 unsigned SourceOrderOrNumArrayIndices;
6501 SmallVector<VarDecl *, 8> Indices;
6502 if (IsWritten) {
6503 SourceOrderOrNumArrayIndices = Record[Idx++];
6504 } else {
6505 SourceOrderOrNumArrayIndices = Record[Idx++];
6506 Indices.reserve(SourceOrderOrNumArrayIndices);
6507 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
6508 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
6509 }
6510
6511 CXXCtorInitializer *BOMInit;
6512 if (Type == CTOR_INITIALIZER_BASE) {
6513 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, IsBaseVirtual,
6514 LParenLoc, Init, RParenLoc,
6515 MemberOrEllipsisLoc);
6516 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
6517 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, LParenLoc,
6518 Init, RParenLoc);
6519 } else if (IsWritten) {
6520 if (Member)
6521 BOMInit = new (Context) CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc,
6522 LParenLoc, Init, RParenLoc);
6523 else
6524 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
6525 MemberOrEllipsisLoc, LParenLoc,
6526 Init, RParenLoc);
6527 } else {
6528 BOMInit = CXXCtorInitializer::Create(Context, Member, MemberOrEllipsisLoc,
6529 LParenLoc, Init, RParenLoc,
6530 Indices.data(), Indices.size());
6531 }
6532
6533 if (IsWritten)
6534 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
6535 CtorInitializers[i] = BOMInit;
6536 }
6537 }
6538
6539 return std::make_pair(CtorInitializers, NumInitializers);
6540}
6541
6542NestedNameSpecifier *
6543ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
6544 const RecordData &Record, unsigned &Idx) {
6545 unsigned N = Record[Idx++];
6546 NestedNameSpecifier *NNS = 0, *Prev = 0;
6547 for (unsigned I = 0; I != N; ++I) {
6548 NestedNameSpecifier::SpecifierKind Kind
6549 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
6550 switch (Kind) {
6551 case NestedNameSpecifier::Identifier: {
6552 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
6553 NNS = NestedNameSpecifier::Create(Context, Prev, II);
6554 break;
6555 }
6556
6557 case NestedNameSpecifier::Namespace: {
6558 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
6559 NNS = NestedNameSpecifier::Create(Context, Prev, NS);
6560 break;
6561 }
6562
6563 case NestedNameSpecifier::NamespaceAlias: {
6564 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
6565 NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
6566 break;
6567 }
6568
6569 case NestedNameSpecifier::TypeSpec:
6570 case NestedNameSpecifier::TypeSpecWithTemplate: {
6571 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
6572 if (!T)
6573 return 0;
6574
6575 bool Template = Record[Idx++];
6576 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
6577 break;
6578 }
6579
6580 case NestedNameSpecifier::Global: {
6581 NNS = NestedNameSpecifier::GlobalSpecifier(Context);
6582 // No associated value, and there can't be a prefix.
6583 break;
6584 }
6585 }
6586 Prev = NNS;
6587 }
6588 return NNS;
6589}
6590
6591NestedNameSpecifierLoc
6592ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
6593 unsigned &Idx) {
6594 unsigned N = Record[Idx++];
6595 NestedNameSpecifierLocBuilder Builder;
6596 for (unsigned I = 0; I != N; ++I) {
6597 NestedNameSpecifier::SpecifierKind Kind
6598 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
6599 switch (Kind) {
6600 case NestedNameSpecifier::Identifier: {
6601 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
6602 SourceRange Range = ReadSourceRange(F, Record, Idx);
6603 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
6604 break;
6605 }
6606
6607 case NestedNameSpecifier::Namespace: {
6608 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
6609 SourceRange Range = ReadSourceRange(F, Record, Idx);
6610 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
6611 break;
6612 }
6613
6614 case NestedNameSpecifier::NamespaceAlias: {
6615 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
6616 SourceRange Range = ReadSourceRange(F, Record, Idx);
6617 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
6618 break;
6619 }
6620
6621 case NestedNameSpecifier::TypeSpec:
6622 case NestedNameSpecifier::TypeSpecWithTemplate: {
6623 bool Template = Record[Idx++];
6624 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
6625 if (!T)
6626 return NestedNameSpecifierLoc();
6627 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
6628
6629 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
6630 Builder.Extend(Context,
6631 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
6632 T->getTypeLoc(), ColonColonLoc);
6633 break;
6634 }
6635
6636 case NestedNameSpecifier::Global: {
6637 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
6638 Builder.MakeGlobal(Context, ColonColonLoc);
6639 break;
6640 }
6641 }
6642 }
6643
6644 return Builder.getWithLocInContext(Context);
6645}
6646
6647SourceRange
6648ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
6649 unsigned &Idx) {
6650 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
6651 SourceLocation end = ReadSourceLocation(F, Record, Idx);
6652 return SourceRange(beg, end);
6653}
6654
6655/// \brief Read an integral value
6656llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
6657 unsigned BitWidth = Record[Idx++];
6658 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
6659 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
6660 Idx += NumWords;
6661 return Result;
6662}
6663
6664/// \brief Read a signed integral value
6665llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
6666 bool isUnsigned = Record[Idx++];
6667 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
6668}
6669
6670/// \brief Read a floating-point value
6671llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
6672 return llvm::APFloat(ReadAPInt(Record, Idx));
6673}
6674
6675// \brief Read a string
6676std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
6677 unsigned Len = Record[Idx++];
6678 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
6679 Idx += Len;
6680 return Result;
6681}
6682
6683VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
6684 unsigned &Idx) {
6685 unsigned Major = Record[Idx++];
6686 unsigned Minor = Record[Idx++];
6687 unsigned Subminor = Record[Idx++];
6688 if (Minor == 0)
6689 return VersionTuple(Major);
6690 if (Subminor == 0)
6691 return VersionTuple(Major, Minor - 1);
6692 return VersionTuple(Major, Minor - 1, Subminor - 1);
6693}
6694
6695CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
6696 const RecordData &Record,
6697 unsigned &Idx) {
6698 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
6699 return CXXTemporary::Create(Context, Decl);
6700}
6701
6702DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
6703 return Diag(SourceLocation(), DiagID);
6704}
6705
6706DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
6707 return Diags.Report(Loc, DiagID);
6708}
6709
6710/// \brief Retrieve the identifier table associated with the
6711/// preprocessor.
6712IdentifierTable &ASTReader::getIdentifierTable() {
6713 return PP.getIdentifierTable();
6714}
6715
6716/// \brief Record that the given ID maps to the given switch-case
6717/// statement.
6718void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
6719 assert((*CurrSwitchCaseStmts)[ID] == 0 &&
6720 "Already have a SwitchCase with this ID");
6721 (*CurrSwitchCaseStmts)[ID] = SC;
6722}
6723
6724/// \brief Retrieve the switch-case statement with the given ID.
6725SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
6726 assert((*CurrSwitchCaseStmts)[ID] != 0 && "No SwitchCase with this ID");
6727 return (*CurrSwitchCaseStmts)[ID];
6728}
6729
6730void ASTReader::ClearSwitchCaseIDs() {
6731 CurrSwitchCaseStmts->clear();
6732}
6733
6734void ASTReader::ReadComments() {
6735 std::vector<RawComment *> Comments;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00006736 for (SmallVectorImpl<std::pair<BitstreamCursor,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006737 serialization::ModuleFile *> >::iterator
6738 I = CommentsCursors.begin(),
6739 E = CommentsCursors.end();
6740 I != E; ++I) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00006741 BitstreamCursor &Cursor = I->first;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006742 serialization::ModuleFile &F = *I->second;
6743 SavedStreamPosition SavedPosition(Cursor);
6744
6745 RecordData Record;
6746 while (true) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00006747 llvm::BitstreamEntry Entry =
6748 Cursor.advanceSkippingSubblocks(BitstreamCursor::AF_DontPopBlockAtEnd);
6749
6750 switch (Entry.Kind) {
6751 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
6752 case llvm::BitstreamEntry::Error:
6753 Error("malformed block record in AST file");
6754 return;
6755 case llvm::BitstreamEntry::EndBlock:
6756 goto NextCursor;
6757 case llvm::BitstreamEntry::Record:
6758 // The interesting case.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006759 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006760 }
6761
6762 // Read a record.
6763 Record.clear();
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00006764 switch ((CommentRecordTypes)Cursor.ReadRecord(Entry.ID, Record)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006765 case COMMENTS_RAW_COMMENT: {
6766 unsigned Idx = 0;
6767 SourceRange SR = ReadSourceRange(F, Record, Idx);
6768 RawComment::CommentKind Kind =
6769 (RawComment::CommentKind) Record[Idx++];
6770 bool IsTrailingComment = Record[Idx++];
6771 bool IsAlmostTrailingComment = Record[Idx++];
6772 Comments.push_back(new (Context) RawComment(SR, Kind,
6773 IsTrailingComment,
6774 IsAlmostTrailingComment));
6775 break;
6776 }
6777 }
6778 }
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00006779 NextCursor:;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006780 }
6781 Context.Comments.addCommentsToFront(Comments);
6782}
6783
6784void ASTReader::finishPendingActions() {
6785 while (!PendingIdentifierInfos.empty() || !PendingDeclChains.empty() ||
6786 !PendingMacroIDs.empty()) {
6787 // If any identifiers with corresponding top-level declarations have
6788 // been loaded, load those declarations now.
6789 while (!PendingIdentifierInfos.empty()) {
6790 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
6791 PendingIdentifierInfos.front().DeclIDs, true);
6792 PendingIdentifierInfos.pop_front();
6793 }
6794
6795 // Load pending declaration chains.
6796 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) {
6797 loadPendingDeclChain(PendingDeclChains[I]);
6798 PendingDeclChainsKnown.erase(PendingDeclChains[I]);
6799 }
6800 PendingDeclChains.clear();
6801
6802 // Load any pending macro definitions.
6803 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00006804 // FIXME: std::move here
6805 SmallVector<MacroID, 2> GlobalIDs = PendingMacroIDs.begin()[I].second;
6806 MacroInfo *Hint = 0;
6807 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
6808 ++IDIdx) {
6809 Hint = getMacro(GlobalIDs[IDIdx], Hint);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006810 }
6811 }
6812 PendingMacroIDs.clear();
6813 }
6814
6815 // If we deserialized any C++ or Objective-C class definitions, any
6816 // Objective-C protocol definitions, or any redeclarable templates, make sure
6817 // that all redeclarations point to the definitions. Note that this can only
6818 // happen now, after the redeclaration chains have been fully wired.
6819 for (llvm::SmallPtrSet<Decl *, 4>::iterator D = PendingDefinitions.begin(),
6820 DEnd = PendingDefinitions.end();
6821 D != DEnd; ++D) {
6822 if (TagDecl *TD = dyn_cast<TagDecl>(*D)) {
6823 if (const TagType *TagT = dyn_cast<TagType>(TD->TypeForDecl)) {
6824 // Make sure that the TagType points at the definition.
6825 const_cast<TagType*>(TagT)->decl = TD;
6826 }
6827
6828 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(*D)) {
6829 for (CXXRecordDecl::redecl_iterator R = RD->redecls_begin(),
6830 REnd = RD->redecls_end();
6831 R != REnd; ++R)
6832 cast<CXXRecordDecl>(*R)->DefinitionData = RD->DefinitionData;
6833
6834 }
6835
6836 continue;
6837 }
6838
6839 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(*D)) {
6840 // Make sure that the ObjCInterfaceType points at the definition.
6841 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
6842 ->Decl = ID;
6843
6844 for (ObjCInterfaceDecl::redecl_iterator R = ID->redecls_begin(),
6845 REnd = ID->redecls_end();
6846 R != REnd; ++R)
6847 R->Data = ID->Data;
6848
6849 continue;
6850 }
6851
6852 if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(*D)) {
6853 for (ObjCProtocolDecl::redecl_iterator R = PD->redecls_begin(),
6854 REnd = PD->redecls_end();
6855 R != REnd; ++R)
6856 R->Data = PD->Data;
6857
6858 continue;
6859 }
6860
6861 RedeclarableTemplateDecl *RTD
6862 = cast<RedeclarableTemplateDecl>(*D)->getCanonicalDecl();
6863 for (RedeclarableTemplateDecl::redecl_iterator R = RTD->redecls_begin(),
6864 REnd = RTD->redecls_end();
6865 R != REnd; ++R)
6866 R->Common = RTD->Common;
6867 }
6868 PendingDefinitions.clear();
6869
6870 // Load the bodies of any functions or methods we've encountered. We do
6871 // this now (delayed) so that we can be sure that the declaration chains
6872 // have been fully wired up.
6873 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
6874 PBEnd = PendingBodies.end();
6875 PB != PBEnd; ++PB) {
6876 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
6877 // FIXME: Check for =delete/=default?
6878 // FIXME: Complain about ODR violations here?
6879 if (!getContext().getLangOpts().Modules || !FD->hasBody())
6880 FD->setLazyBody(PB->second);
6881 continue;
6882 }
6883
6884 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
6885 if (!getContext().getLangOpts().Modules || !MD->hasBody())
6886 MD->setLazyBody(PB->second);
6887 }
6888 PendingBodies.clear();
6889}
6890
6891void ASTReader::FinishedDeserializing() {
6892 assert(NumCurrentElementsDeserializing &&
6893 "FinishedDeserializing not paired with StartedDeserializing");
6894 if (NumCurrentElementsDeserializing == 1) {
6895 // We decrease NumCurrentElementsDeserializing only after pending actions
6896 // are finished, to avoid recursively re-calling finishPendingActions().
6897 finishPendingActions();
6898 }
6899 --NumCurrentElementsDeserializing;
6900
6901 if (NumCurrentElementsDeserializing == 0 &&
6902 Consumer && !PassingDeclsToConsumer) {
6903 // Guard variable to avoid recursively redoing the process of passing
6904 // decls to consumer.
6905 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
6906 true);
6907
6908 while (!InterestingDecls.empty()) {
6909 // We are not in recursive loading, so it's safe to pass the "interesting"
6910 // decls to the consumer.
6911 Decl *D = InterestingDecls.front();
6912 InterestingDecls.pop_front();
6913 PassInterestingDeclToConsumer(D);
6914 }
6915 }
6916}
6917
6918ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context,
6919 StringRef isysroot, bool DisableValidation,
6920 bool AllowASTWithCompilerErrors)
6921 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
6922 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
6923 Diags(PP.getDiagnostics()), SemaObj(0), PP(PP), Context(Context),
6924 Consumer(0), ModuleMgr(PP.getFileManager()),
6925 isysroot(isysroot), DisableValidation(DisableValidation),
6926 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
6927 CurrentGeneration(0), CurrSwitchCaseStmts(&SwitchCaseStmts),
6928 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
6929 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
6930 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
6931 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
6932 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
6933 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
6934 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
6935 PassingDeclsToConsumer(false),
6936 NumCXXBaseSpecifiersLoaded(0)
6937{
6938 SourceMgr.setExternalSLocEntrySource(this);
6939}
6940
6941ASTReader::~ASTReader() {
6942 for (DeclContextVisibleUpdatesPending::iterator
6943 I = PendingVisibleUpdates.begin(),
6944 E = PendingVisibleUpdates.end();
6945 I != E; ++I) {
6946 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
6947 F = I->second.end();
6948 J != F; ++J)
6949 delete J->first;
6950 }
6951}