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