blob: c378570ddb77a616228352411a40ff137e7f6592 [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 Benyei7f92f2d2012-12-18 14:30:41 +00004810 case PREDEF_TYPE_AUTO_DEDUCT: T = Context.getAutoDeductType(); break;
4811
4812 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
4813 T = Context.getAutoRRefDeductType();
4814 break;
4815
4816 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
4817 T = Context.ARCUnbridgedCastTy;
4818 break;
4819
4820 case PREDEF_TYPE_VA_LIST_TAG:
4821 T = Context.getVaListTagType();
4822 break;
4823
4824 case PREDEF_TYPE_BUILTIN_FN:
4825 T = Context.BuiltinFnTy;
4826 break;
4827 }
4828
4829 assert(!T.isNull() && "Unknown predefined type");
4830 return T.withFastQualifiers(FastQuals);
4831 }
4832
4833 Index -= NUM_PREDEF_TYPE_IDS;
4834 assert(Index < TypesLoaded.size() && "Type index out-of-range");
4835 if (TypesLoaded[Index].isNull()) {
4836 TypesLoaded[Index] = readTypeRecord(Index);
4837 if (TypesLoaded[Index].isNull())
4838 return QualType();
4839
4840 TypesLoaded[Index]->setFromAST();
4841 if (DeserializationListener)
4842 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
4843 TypesLoaded[Index]);
4844 }
4845
4846 return TypesLoaded[Index].withFastQualifiers(FastQuals);
4847}
4848
4849QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
4850 return GetType(getGlobalTypeID(F, LocalID));
4851}
4852
4853serialization::TypeID
4854ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
4855 unsigned FastQuals = LocalID & Qualifiers::FastMask;
4856 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
4857
4858 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
4859 return LocalID;
4860
4861 ContinuousRangeMap<uint32_t, int, 2>::iterator I
4862 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
4863 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
4864
4865 unsigned GlobalIndex = LocalIndex + I->second;
4866 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
4867}
4868
4869TemplateArgumentLocInfo
4870ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
4871 TemplateArgument::ArgKind Kind,
4872 const RecordData &Record,
4873 unsigned &Index) {
4874 switch (Kind) {
4875 case TemplateArgument::Expression:
4876 return ReadExpr(F);
4877 case TemplateArgument::Type:
4878 return GetTypeSourceInfo(F, Record, Index);
4879 case TemplateArgument::Template: {
4880 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
4881 Index);
4882 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
4883 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
4884 SourceLocation());
4885 }
4886 case TemplateArgument::TemplateExpansion: {
4887 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
4888 Index);
4889 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
4890 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
4891 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
4892 EllipsisLoc);
4893 }
4894 case TemplateArgument::Null:
4895 case TemplateArgument::Integral:
4896 case TemplateArgument::Declaration:
4897 case TemplateArgument::NullPtr:
4898 case TemplateArgument::Pack:
4899 // FIXME: Is this right?
4900 return TemplateArgumentLocInfo();
4901 }
4902 llvm_unreachable("unexpected template argument loc");
4903}
4904
4905TemplateArgumentLoc
4906ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
4907 const RecordData &Record, unsigned &Index) {
4908 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
4909
4910 if (Arg.getKind() == TemplateArgument::Expression) {
4911 if (Record[Index++]) // bool InfoHasSameExpr.
4912 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
4913 }
4914 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
4915 Record, Index));
4916}
4917
4918Decl *ASTReader::GetExternalDecl(uint32_t ID) {
4919 return GetDecl(ID);
4920}
4921
4922uint64_t ASTReader::readCXXBaseSpecifiers(ModuleFile &M, const RecordData &Record,
4923 unsigned &Idx){
4924 if (Idx >= Record.size())
4925 return 0;
4926
4927 unsigned LocalID = Record[Idx++];
4928 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
4929}
4930
4931CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
4932 RecordLocation Loc = getLocalBitOffset(Offset);
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004933 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004934 SavedStreamPosition SavedPosition(Cursor);
4935 Cursor.JumpToBit(Loc.Offset);
4936 ReadingKindTracker ReadingKind(Read_Decl, *this);
4937 RecordData Record;
4938 unsigned Code = Cursor.ReadCode();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00004939 unsigned RecCode = Cursor.readRecord(Code, Record);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004940 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
4941 Error("Malformed AST file: missing C++ base specifiers");
4942 return 0;
4943 }
4944
4945 unsigned Idx = 0;
4946 unsigned NumBases = Record[Idx++];
4947 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
4948 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
4949 for (unsigned I = 0; I != NumBases; ++I)
4950 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
4951 return Bases;
4952}
4953
4954serialization::DeclID
4955ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
4956 if (LocalID < NUM_PREDEF_DECL_IDS)
4957 return LocalID;
4958
4959 ContinuousRangeMap<uint32_t, int, 2>::iterator I
4960 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
4961 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
4962
4963 return LocalID + I->second;
4964}
4965
4966bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
4967 ModuleFile &M) const {
4968 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(ID);
4969 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
4970 return &M == I->second;
4971}
4972
4973ModuleFile *ASTReader::getOwningModuleFile(Decl *D) {
4974 if (!D->isFromASTFile())
4975 return 0;
4976 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
4977 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
4978 return I->second;
4979}
4980
4981SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
4982 if (ID < NUM_PREDEF_DECL_IDS)
4983 return SourceLocation();
4984
4985 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
4986
4987 if (Index > DeclsLoaded.size()) {
4988 Error("declaration ID out-of-range for AST file");
4989 return SourceLocation();
4990 }
4991
4992 if (Decl *D = DeclsLoaded[Index])
4993 return D->getLocation();
4994
4995 unsigned RawLocation = 0;
4996 RecordLocation Rec = DeclCursorForID(ID, RawLocation);
4997 return ReadSourceLocation(*Rec.F, RawLocation);
4998}
4999
5000Decl *ASTReader::GetDecl(DeclID ID) {
5001 if (ID < NUM_PREDEF_DECL_IDS) {
5002 switch ((PredefinedDeclIDs)ID) {
5003 case PREDEF_DECL_NULL_ID:
5004 return 0;
5005
5006 case PREDEF_DECL_TRANSLATION_UNIT_ID:
5007 return Context.getTranslationUnitDecl();
5008
5009 case PREDEF_DECL_OBJC_ID_ID:
5010 return Context.getObjCIdDecl();
5011
5012 case PREDEF_DECL_OBJC_SEL_ID:
5013 return Context.getObjCSelDecl();
5014
5015 case PREDEF_DECL_OBJC_CLASS_ID:
5016 return Context.getObjCClassDecl();
5017
5018 case PREDEF_DECL_OBJC_PROTOCOL_ID:
5019 return Context.getObjCProtocolDecl();
5020
5021 case PREDEF_DECL_INT_128_ID:
5022 return Context.getInt128Decl();
5023
5024 case PREDEF_DECL_UNSIGNED_INT_128_ID:
5025 return Context.getUInt128Decl();
5026
5027 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
5028 return Context.getObjCInstanceTypeDecl();
5029
5030 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
5031 return Context.getBuiltinVaListDecl();
5032 }
5033 }
5034
5035 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
5036
5037 if (Index >= DeclsLoaded.size()) {
5038 assert(0 && "declaration ID out-of-range for AST file");
5039 Error("declaration ID out-of-range for AST file");
5040 return 0;
5041 }
5042
5043 if (!DeclsLoaded[Index]) {
5044 ReadDeclRecord(ID);
5045 if (DeserializationListener)
5046 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
5047 }
5048
5049 return DeclsLoaded[Index];
5050}
5051
5052DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
5053 DeclID GlobalID) {
5054 if (GlobalID < NUM_PREDEF_DECL_IDS)
5055 return GlobalID;
5056
5057 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
5058 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5059 ModuleFile *Owner = I->second;
5060
5061 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
5062 = M.GlobalToLocalDeclIDs.find(Owner);
5063 if (Pos == M.GlobalToLocalDeclIDs.end())
5064 return 0;
5065
5066 return GlobalID - Owner->BaseDeclID + Pos->second;
5067}
5068
5069serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
5070 const RecordData &Record,
5071 unsigned &Idx) {
5072 if (Idx >= Record.size()) {
5073 Error("Corrupted AST file");
5074 return 0;
5075 }
5076
5077 return getGlobalDeclID(F, Record[Idx++]);
5078}
5079
5080/// \brief Resolve the offset of a statement into a statement.
5081///
5082/// This operation will read a new statement from the external
5083/// source each time it is called, and is meant to be used via a
5084/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
5085Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
5086 // Switch case IDs are per Decl.
5087 ClearSwitchCaseIDs();
5088
5089 // Offset here is a global offset across the entire chain.
5090 RecordLocation Loc = getLocalBitOffset(Offset);
5091 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
5092 return ReadStmtFromStream(*Loc.F);
5093}
5094
5095namespace {
5096 class FindExternalLexicalDeclsVisitor {
5097 ASTReader &Reader;
5098 const DeclContext *DC;
5099 bool (*isKindWeWant)(Decl::Kind);
5100
5101 SmallVectorImpl<Decl*> &Decls;
5102 bool PredefsVisited[NUM_PREDEF_DECL_IDS];
5103
5104 public:
5105 FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC,
5106 bool (*isKindWeWant)(Decl::Kind),
5107 SmallVectorImpl<Decl*> &Decls)
5108 : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls)
5109 {
5110 for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I)
5111 PredefsVisited[I] = false;
5112 }
5113
5114 static bool visit(ModuleFile &M, bool Preorder, void *UserData) {
5115 if (Preorder)
5116 return false;
5117
5118 FindExternalLexicalDeclsVisitor *This
5119 = static_cast<FindExternalLexicalDeclsVisitor *>(UserData);
5120
5121 ModuleFile::DeclContextInfosMap::iterator Info
5122 = M.DeclContextInfos.find(This->DC);
5123 if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls)
5124 return false;
5125
5126 // Load all of the declaration IDs
5127 for (const KindDeclIDPair *ID = Info->second.LexicalDecls,
5128 *IDE = ID + Info->second.NumLexicalDecls;
5129 ID != IDE; ++ID) {
5130 if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first))
5131 continue;
5132
5133 // Don't add predefined declarations to the lexical context more
5134 // than once.
5135 if (ID->second < NUM_PREDEF_DECL_IDS) {
5136 if (This->PredefsVisited[ID->second])
5137 continue;
5138
5139 This->PredefsVisited[ID->second] = true;
5140 }
5141
5142 if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) {
5143 if (!This->DC->isDeclInLexicalTraversal(D))
5144 This->Decls.push_back(D);
5145 }
5146 }
5147
5148 return false;
5149 }
5150 };
5151}
5152
5153ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
5154 bool (*isKindWeWant)(Decl::Kind),
5155 SmallVectorImpl<Decl*> &Decls) {
5156 // There might be lexical decls in multiple modules, for the TU at
5157 // least. Walk all of the modules in the order they were loaded.
5158 FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls);
5159 ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor);
5160 ++NumLexicalDeclContextsRead;
5161 return ELR_Success;
5162}
5163
5164namespace {
5165
5166class DeclIDComp {
5167 ASTReader &Reader;
5168 ModuleFile &Mod;
5169
5170public:
5171 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
5172
5173 bool operator()(LocalDeclID L, LocalDeclID R) const {
5174 SourceLocation LHS = getLocation(L);
5175 SourceLocation RHS = getLocation(R);
5176 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5177 }
5178
5179 bool operator()(SourceLocation LHS, LocalDeclID R) const {
5180 SourceLocation RHS = getLocation(R);
5181 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5182 }
5183
5184 bool operator()(LocalDeclID L, SourceLocation RHS) const {
5185 SourceLocation LHS = getLocation(L);
5186 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5187 }
5188
5189 SourceLocation getLocation(LocalDeclID ID) const {
5190 return Reader.getSourceManager().getFileLoc(
5191 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
5192 }
5193};
5194
5195}
5196
5197void ASTReader::FindFileRegionDecls(FileID File,
5198 unsigned Offset, unsigned Length,
5199 SmallVectorImpl<Decl *> &Decls) {
5200 SourceManager &SM = getSourceManager();
5201
5202 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
5203 if (I == FileDeclIDs.end())
5204 return;
5205
5206 FileDeclsInfo &DInfo = I->second;
5207 if (DInfo.Decls.empty())
5208 return;
5209
5210 SourceLocation
5211 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
5212 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
5213
5214 DeclIDComp DIDComp(*this, *DInfo.Mod);
5215 ArrayRef<serialization::LocalDeclID>::iterator
5216 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
5217 BeginLoc, DIDComp);
5218 if (BeginIt != DInfo.Decls.begin())
5219 --BeginIt;
5220
5221 // If we are pointing at a top-level decl inside an objc container, we need
5222 // to backtrack until we find it otherwise we will fail to report that the
5223 // region overlaps with an objc container.
5224 while (BeginIt != DInfo.Decls.begin() &&
5225 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
5226 ->isTopLevelDeclInObjCContainer())
5227 --BeginIt;
5228
5229 ArrayRef<serialization::LocalDeclID>::iterator
5230 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
5231 EndLoc, DIDComp);
5232 if (EndIt != DInfo.Decls.end())
5233 ++EndIt;
5234
5235 for (ArrayRef<serialization::LocalDeclID>::iterator
5236 DIt = BeginIt; DIt != EndIt; ++DIt)
5237 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
5238}
5239
5240namespace {
5241 /// \brief ModuleFile visitor used to perform name lookup into a
5242 /// declaration context.
5243 class DeclContextNameLookupVisitor {
5244 ASTReader &Reader;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00005245 SmallVectorImpl<const DeclContext *> &Contexts;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005246 DeclarationName Name;
5247 SmallVectorImpl<NamedDecl *> &Decls;
5248
5249 public:
5250 DeclContextNameLookupVisitor(ASTReader &Reader,
5251 SmallVectorImpl<const DeclContext *> &Contexts,
5252 DeclarationName Name,
5253 SmallVectorImpl<NamedDecl *> &Decls)
5254 : Reader(Reader), Contexts(Contexts), Name(Name), Decls(Decls) { }
5255
5256 static bool visit(ModuleFile &M, void *UserData) {
5257 DeclContextNameLookupVisitor *This
5258 = static_cast<DeclContextNameLookupVisitor *>(UserData);
5259
5260 // Check whether we have any visible declaration information for
5261 // this context in this module.
5262 ModuleFile::DeclContextInfosMap::iterator Info;
5263 bool FoundInfo = false;
5264 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5265 Info = M.DeclContextInfos.find(This->Contexts[I]);
5266 if (Info != M.DeclContextInfos.end() &&
5267 Info->second.NameLookupTableData) {
5268 FoundInfo = true;
5269 break;
5270 }
5271 }
5272
5273 if (!FoundInfo)
5274 return false;
5275
5276 // Look for this name within this module.
5277 ASTDeclContextNameLookupTable *LookupTable =
5278 Info->second.NameLookupTableData;
5279 ASTDeclContextNameLookupTable::iterator Pos
5280 = LookupTable->find(This->Name);
5281 if (Pos == LookupTable->end())
5282 return false;
5283
5284 bool FoundAnything = false;
5285 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
5286 for (; Data.first != Data.second; ++Data.first) {
5287 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first);
5288 if (!ND)
5289 continue;
5290
5291 if (ND->getDeclName() != This->Name) {
5292 // A name might be null because the decl's redeclarable part is
5293 // currently read before reading its name. The lookup is triggered by
5294 // building that decl (likely indirectly), and so it is later in the
5295 // sense of "already existing" and can be ignored here.
5296 continue;
5297 }
5298
5299 // Record this declaration.
5300 FoundAnything = true;
5301 This->Decls.push_back(ND);
5302 }
5303
5304 return FoundAnything;
5305 }
5306 };
5307}
5308
5309DeclContext::lookup_result
5310ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
5311 DeclarationName Name) {
5312 assert(DC->hasExternalVisibleStorage() &&
5313 "DeclContext has no visible decls in storage");
5314 if (!Name)
5315 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
5316 DeclContext::lookup_iterator(0));
5317
5318 SmallVector<NamedDecl *, 64> Decls;
5319
5320 // Compute the declaration contexts we need to look into. Multiple such
5321 // declaration contexts occur when two declaration contexts from disjoint
5322 // modules get merged, e.g., when two namespaces with the same name are
5323 // independently defined in separate modules.
5324 SmallVector<const DeclContext *, 2> Contexts;
5325 Contexts.push_back(DC);
5326
5327 if (DC->isNamespace()) {
5328 MergedDeclsMap::iterator Merged
5329 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5330 if (Merged != MergedDecls.end()) {
5331 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5332 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5333 }
5334 }
5335
5336 DeclContextNameLookupVisitor Visitor(*this, Contexts, Name, Decls);
5337 ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor);
5338 ++NumVisibleDeclContextsRead;
5339 SetExternalVisibleDeclsForName(DC, Name, Decls);
5340 return const_cast<DeclContext*>(DC)->lookup(Name);
5341}
5342
5343namespace {
5344 /// \brief ModuleFile visitor used to retrieve all visible names in a
5345 /// declaration context.
5346 class DeclContextAllNamesVisitor {
5347 ASTReader &Reader;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00005348 SmallVectorImpl<const DeclContext *> &Contexts;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005349 llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> > &Decls;
Argyrios Kyrtzidisca40f302012-12-19 22:21:18 +00005350 bool VisitAll;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005351
5352 public:
5353 DeclContextAllNamesVisitor(ASTReader &Reader,
5354 SmallVectorImpl<const DeclContext *> &Contexts,
5355 llvm::DenseMap<DeclarationName,
Argyrios Kyrtzidisca40f302012-12-19 22:21:18 +00005356 SmallVector<NamedDecl *, 8> > &Decls,
5357 bool VisitAll)
5358 : Reader(Reader), Contexts(Contexts), Decls(Decls), VisitAll(VisitAll) { }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005359
5360 static bool visit(ModuleFile &M, void *UserData) {
5361 DeclContextAllNamesVisitor *This
5362 = static_cast<DeclContextAllNamesVisitor *>(UserData);
5363
5364 // Check whether we have any visible declaration information for
5365 // this context in this module.
5366 ModuleFile::DeclContextInfosMap::iterator Info;
5367 bool FoundInfo = false;
5368 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5369 Info = M.DeclContextInfos.find(This->Contexts[I]);
5370 if (Info != M.DeclContextInfos.end() &&
5371 Info->second.NameLookupTableData) {
5372 FoundInfo = true;
5373 break;
5374 }
5375 }
5376
5377 if (!FoundInfo)
5378 return false;
5379
5380 ASTDeclContextNameLookupTable *LookupTable =
5381 Info->second.NameLookupTableData;
5382 bool FoundAnything = false;
5383 for (ASTDeclContextNameLookupTable::data_iterator
5384 I = LookupTable->data_begin(), E = LookupTable->data_end();
5385 I != E; ++I) {
5386 ASTDeclContextNameLookupTrait::data_type Data = *I;
5387 for (; Data.first != Data.second; ++Data.first) {
5388 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M,
5389 *Data.first);
5390 if (!ND)
5391 continue;
5392
5393 // Record this declaration.
5394 FoundAnything = true;
5395 This->Decls[ND->getDeclName()].push_back(ND);
5396 }
5397 }
5398
Argyrios Kyrtzidisca40f302012-12-19 22:21:18 +00005399 return FoundAnything && !This->VisitAll;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005400 }
5401 };
5402}
5403
5404void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
5405 if (!DC->hasExternalVisibleStorage())
5406 return;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00005407 llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> > Decls;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005408
5409 // Compute the declaration contexts we need to look into. Multiple such
5410 // declaration contexts occur when two declaration contexts from disjoint
5411 // modules get merged, e.g., when two namespaces with the same name are
5412 // independently defined in separate modules.
5413 SmallVector<const DeclContext *, 2> Contexts;
5414 Contexts.push_back(DC);
5415
5416 if (DC->isNamespace()) {
5417 MergedDeclsMap::iterator Merged
5418 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5419 if (Merged != MergedDecls.end()) {
5420 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5421 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5422 }
5423 }
5424
Argyrios Kyrtzidisca40f302012-12-19 22:21:18 +00005425 DeclContextAllNamesVisitor Visitor(*this, Contexts, Decls,
5426 /*VisitAll=*/DC->isFileContext());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005427 ModuleMgr.visit(&DeclContextAllNamesVisitor::visit, &Visitor);
5428 ++NumVisibleDeclContextsRead;
5429
5430 for (llvm::DenseMap<DeclarationName,
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00005431 SmallVector<NamedDecl *, 8> >::iterator
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005432 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
5433 SetExternalVisibleDeclsForName(DC, I->first, I->second);
5434 }
5435 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
5436}
5437
5438/// \brief Under non-PCH compilation the consumer receives the objc methods
5439/// before receiving the implementation, and codegen depends on this.
5440/// We simulate this by deserializing and passing to consumer the methods of the
5441/// implementation before passing the deserialized implementation decl.
5442static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
5443 ASTConsumer *Consumer) {
5444 assert(ImplD && Consumer);
5445
5446 for (ObjCImplDecl::method_iterator
5447 I = ImplD->meth_begin(), E = ImplD->meth_end(); I != E; ++I)
5448 Consumer->HandleInterestingDecl(DeclGroupRef(*I));
5449
5450 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
5451}
5452
5453void ASTReader::PassInterestingDeclsToConsumer() {
5454 assert(Consumer);
5455 while (!InterestingDecls.empty()) {
5456 Decl *D = InterestingDecls.front();
5457 InterestingDecls.pop_front();
5458
5459 PassInterestingDeclToConsumer(D);
5460 }
5461}
5462
5463void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
5464 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
5465 PassObjCImplDeclToConsumer(ImplD, Consumer);
5466 else
5467 Consumer->HandleInterestingDecl(DeclGroupRef(D));
5468}
5469
5470void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
5471 this->Consumer = Consumer;
5472
5473 if (!Consumer)
5474 return;
5475
5476 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
5477 // Force deserialization of this decl, which will cause it to be queued for
5478 // passing to the consumer.
5479 GetDecl(ExternalDefinitions[I]);
5480 }
5481 ExternalDefinitions.clear();
5482
5483 PassInterestingDeclsToConsumer();
5484}
5485
5486void ASTReader::PrintStats() {
5487 std::fprintf(stderr, "*** AST File Statistics:\n");
5488
5489 unsigned NumTypesLoaded
5490 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
5491 QualType());
5492 unsigned NumDeclsLoaded
5493 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
5494 (Decl *)0);
5495 unsigned NumIdentifiersLoaded
5496 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
5497 IdentifiersLoaded.end(),
5498 (IdentifierInfo *)0);
5499 unsigned NumMacrosLoaded
5500 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
5501 MacrosLoaded.end(),
5502 (MacroInfo *)0);
5503 unsigned NumSelectorsLoaded
5504 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
5505 SelectorsLoaded.end(),
5506 Selector());
5507
5508 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
5509 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
5510 NumSLocEntriesRead, TotalNumSLocEntries,
5511 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
5512 if (!TypesLoaded.empty())
5513 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
5514 NumTypesLoaded, (unsigned)TypesLoaded.size(),
5515 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
5516 if (!DeclsLoaded.empty())
5517 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
5518 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
5519 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
5520 if (!IdentifiersLoaded.empty())
5521 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
5522 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
5523 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
5524 if (!MacrosLoaded.empty())
5525 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
5526 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
5527 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
5528 if (!SelectorsLoaded.empty())
5529 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
5530 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
5531 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
5532 if (TotalNumStatements)
5533 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
5534 NumStatementsRead, TotalNumStatements,
5535 ((float)NumStatementsRead/TotalNumStatements * 100));
5536 if (TotalNumMacros)
5537 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
5538 NumMacrosRead, TotalNumMacros,
5539 ((float)NumMacrosRead/TotalNumMacros * 100));
5540 if (TotalLexicalDeclContexts)
5541 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
5542 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
5543 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
5544 * 100));
5545 if (TotalVisibleDeclContexts)
5546 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
5547 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
5548 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
5549 * 100));
5550 if (TotalNumMethodPoolEntries) {
5551 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
5552 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
5553 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
5554 * 100));
5555 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
5556 }
5557 std::fprintf(stderr, "\n");
5558 dump();
5559 std::fprintf(stderr, "\n");
5560}
5561
5562template<typename Key, typename ModuleFile, unsigned InitialCapacity>
5563static void
5564dumpModuleIDMap(StringRef Name,
5565 const ContinuousRangeMap<Key, ModuleFile *,
5566 InitialCapacity> &Map) {
5567 if (Map.begin() == Map.end())
5568 return;
5569
5570 typedef ContinuousRangeMap<Key, ModuleFile *, InitialCapacity> MapType;
5571 llvm::errs() << Name << ":\n";
5572 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
5573 I != IEnd; ++I) {
5574 llvm::errs() << " " << I->first << " -> " << I->second->FileName
5575 << "\n";
5576 }
5577}
5578
5579void ASTReader::dump() {
5580 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
5581 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
5582 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
5583 dumpModuleIDMap("Global type map", GlobalTypeMap);
5584 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
5585 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
5586 dumpModuleIDMap("Global macro map", GlobalMacroMap);
5587 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
5588 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
5589 dumpModuleIDMap("Global preprocessed entity map",
5590 GlobalPreprocessedEntityMap);
5591
5592 llvm::errs() << "\n*** PCH/Modules Loaded:";
5593 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
5594 MEnd = ModuleMgr.end();
5595 M != MEnd; ++M)
5596 (*M)->dump();
5597}
5598
5599/// Return the amount of memory used by memory buffers, breaking down
5600/// by heap-backed versus mmap'ed memory.
5601void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
5602 for (ModuleConstIterator I = ModuleMgr.begin(),
5603 E = ModuleMgr.end(); I != E; ++I) {
5604 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
5605 size_t bytes = buf->getBufferSize();
5606 switch (buf->getBufferKind()) {
5607 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
5608 sizes.malloc_bytes += bytes;
5609 break;
5610 case llvm::MemoryBuffer::MemoryBuffer_MMap:
5611 sizes.mmap_bytes += bytes;
5612 break;
5613 }
5614 }
5615 }
5616}
5617
5618void ASTReader::InitializeSema(Sema &S) {
5619 SemaObj = &S;
5620 S.addExternalSource(this);
5621
5622 // Makes sure any declarations that were deserialized "too early"
5623 // still get added to the identifier's declaration chains.
5624 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
5625 SemaObj->pushExternalDeclIntoScope(PreloadedDecls[I],
5626 PreloadedDecls[I]->getDeclName());
5627 }
5628 PreloadedDecls.clear();
5629
5630 // Load the offsets of the declarations that Sema references.
5631 // They will be lazily deserialized when needed.
5632 if (!SemaDeclRefs.empty()) {
5633 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
5634 if (!SemaObj->StdNamespace)
5635 SemaObj->StdNamespace = SemaDeclRefs[0];
5636 if (!SemaObj->StdBadAlloc)
5637 SemaObj->StdBadAlloc = SemaDeclRefs[1];
5638 }
5639
5640 if (!FPPragmaOptions.empty()) {
5641 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
5642 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
5643 }
5644
5645 if (!OpenCLExtensions.empty()) {
5646 unsigned I = 0;
5647#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
5648#include "clang/Basic/OpenCLExtensions.def"
5649
5650 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
5651 }
5652}
5653
5654IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
5655 // Note that we are loading an identifier.
5656 Deserializing AnIdentifier(this);
5657
5658 IdentifierLookupVisitor Visitor(StringRef(NameStart, NameEnd - NameStart),
5659 /*PriorGeneration=*/0);
5660 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
5661 IdentifierInfo *II = Visitor.getIdentifierInfo();
5662 markIdentifierUpToDate(II);
5663 return II;
5664}
5665
5666namespace clang {
5667 /// \brief An identifier-lookup iterator that enumerates all of the
5668 /// identifiers stored within a set of AST files.
5669 class ASTIdentifierIterator : public IdentifierIterator {
5670 /// \brief The AST reader whose identifiers are being enumerated.
5671 const ASTReader &Reader;
5672
5673 /// \brief The current index into the chain of AST files stored in
5674 /// the AST reader.
5675 unsigned Index;
5676
5677 /// \brief The current position within the identifier lookup table
5678 /// of the current AST file.
5679 ASTIdentifierLookupTable::key_iterator Current;
5680
5681 /// \brief The end position within the identifier lookup table of
5682 /// the current AST file.
5683 ASTIdentifierLookupTable::key_iterator End;
5684
5685 public:
5686 explicit ASTIdentifierIterator(const ASTReader &Reader);
5687
5688 virtual StringRef Next();
5689 };
5690}
5691
5692ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
5693 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
5694 ASTIdentifierLookupTable *IdTable
5695 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
5696 Current = IdTable->key_begin();
5697 End = IdTable->key_end();
5698}
5699
5700StringRef ASTIdentifierIterator::Next() {
5701 while (Current == End) {
5702 // If we have exhausted all of our AST files, we're done.
5703 if (Index == 0)
5704 return StringRef();
5705
5706 --Index;
5707 ASTIdentifierLookupTable *IdTable
5708 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
5709 IdentifierLookupTable;
5710 Current = IdTable->key_begin();
5711 End = IdTable->key_end();
5712 }
5713
5714 // We have any identifiers remaining in the current AST file; return
5715 // the next one.
5716 std::pair<const char*, unsigned> Key = *Current;
5717 ++Current;
5718 return StringRef(Key.first, Key.second);
5719}
5720
5721IdentifierIterator *ASTReader::getIdentifiers() const {
5722 return new ASTIdentifierIterator(*this);
5723}
5724
5725namespace clang { namespace serialization {
5726 class ReadMethodPoolVisitor {
5727 ASTReader &Reader;
5728 Selector Sel;
5729 unsigned PriorGeneration;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00005730 SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
5731 SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005732
5733 public:
5734 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
5735 unsigned PriorGeneration)
5736 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) { }
5737
5738 static bool visit(ModuleFile &M, void *UserData) {
5739 ReadMethodPoolVisitor *This
5740 = static_cast<ReadMethodPoolVisitor *>(UserData);
5741
5742 if (!M.SelectorLookupTable)
5743 return false;
5744
5745 // If we've already searched this module file, skip it now.
5746 if (M.Generation <= This->PriorGeneration)
5747 return true;
5748
5749 ASTSelectorLookupTable *PoolTable
5750 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
5751 ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel);
5752 if (Pos == PoolTable->end())
5753 return false;
5754
5755 ++This->Reader.NumSelectorsRead;
5756 // FIXME: Not quite happy with the statistics here. We probably should
5757 // disable this tracking when called via LoadSelector.
5758 // Also, should entries without methods count as misses?
5759 ++This->Reader.NumMethodPoolEntriesRead;
5760 ASTSelectorLookupTrait::data_type Data = *Pos;
5761 if (This->Reader.DeserializationListener)
5762 This->Reader.DeserializationListener->SelectorRead(Data.ID,
5763 This->Sel);
5764
5765 This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
5766 This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
5767 return true;
5768 }
5769
5770 /// \brief Retrieve the instance methods found by this visitor.
5771 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
5772 return InstanceMethods;
5773 }
5774
5775 /// \brief Retrieve the instance methods found by this visitor.
5776 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
5777 return FactoryMethods;
5778 }
5779 };
5780} } // end namespace clang::serialization
5781
5782/// \brief Add the given set of methods to the method list.
5783static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
5784 ObjCMethodList &List) {
5785 for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
5786 S.addMethodToGlobalList(&List, Methods[I]);
5787 }
5788}
5789
5790void ASTReader::ReadMethodPool(Selector Sel) {
5791 // Get the selector generation and update it to the current generation.
5792 unsigned &Generation = SelectorGeneration[Sel];
5793 unsigned PriorGeneration = Generation;
5794 Generation = CurrentGeneration;
5795
5796 // Search for methods defined with this selector.
5797 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
5798 ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor);
5799
5800 if (Visitor.getInstanceMethods().empty() &&
5801 Visitor.getFactoryMethods().empty()) {
5802 ++NumMethodPoolMisses;
5803 return;
5804 }
5805
5806 if (!getSema())
5807 return;
5808
5809 Sema &S = *getSema();
5810 Sema::GlobalMethodPool::iterator Pos
5811 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
5812
5813 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
5814 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
5815}
5816
5817void ASTReader::ReadKnownNamespaces(
5818 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
5819 Namespaces.clear();
5820
5821 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
5822 if (NamespaceDecl *Namespace
5823 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
5824 Namespaces.push_back(Namespace);
5825 }
5826}
5827
5828void ASTReader::ReadTentativeDefinitions(
5829 SmallVectorImpl<VarDecl *> &TentativeDefs) {
5830 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
5831 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
5832 if (Var)
5833 TentativeDefs.push_back(Var);
5834 }
5835 TentativeDefinitions.clear();
5836}
5837
5838void ASTReader::ReadUnusedFileScopedDecls(
5839 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
5840 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
5841 DeclaratorDecl *D
5842 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
5843 if (D)
5844 Decls.push_back(D);
5845 }
5846 UnusedFileScopedDecls.clear();
5847}
5848
5849void ASTReader::ReadDelegatingConstructors(
5850 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
5851 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
5852 CXXConstructorDecl *D
5853 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
5854 if (D)
5855 Decls.push_back(D);
5856 }
5857 DelegatingCtorDecls.clear();
5858}
5859
5860void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
5861 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
5862 TypedefNameDecl *D
5863 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
5864 if (D)
5865 Decls.push_back(D);
5866 }
5867 ExtVectorDecls.clear();
5868}
5869
5870void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) {
5871 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
5872 CXXRecordDecl *D
5873 = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I]));
5874 if (D)
5875 Decls.push_back(D);
5876 }
5877 DynamicClasses.clear();
5878}
5879
5880void
Richard Smith5ea6ef42013-01-10 23:43:47 +00005881ASTReader::ReadLocallyScopedExternCDecls(SmallVectorImpl<NamedDecl *> &Decls) {
5882 for (unsigned I = 0, N = LocallyScopedExternCDecls.size(); I != N; ++I) {
5883 NamedDecl *D
5884 = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternCDecls[I]));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005885 if (D)
5886 Decls.push_back(D);
5887 }
Richard Smith5ea6ef42013-01-10 23:43:47 +00005888 LocallyScopedExternCDecls.clear();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005889}
5890
5891void ASTReader::ReadReferencedSelectors(
5892 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
5893 if (ReferencedSelectorsData.empty())
5894 return;
5895
5896 // If there are @selector references added them to its pool. This is for
5897 // implementation of -Wselector.
5898 unsigned int DataSize = ReferencedSelectorsData.size()-1;
5899 unsigned I = 0;
5900 while (I < DataSize) {
5901 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
5902 SourceLocation SelLoc
5903 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
5904 Sels.push_back(std::make_pair(Sel, SelLoc));
5905 }
5906 ReferencedSelectorsData.clear();
5907}
5908
5909void ASTReader::ReadWeakUndeclaredIdentifiers(
5910 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
5911 if (WeakUndeclaredIdentifiers.empty())
5912 return;
5913
5914 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
5915 IdentifierInfo *WeakId
5916 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
5917 IdentifierInfo *AliasId
5918 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
5919 SourceLocation Loc
5920 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
5921 bool Used = WeakUndeclaredIdentifiers[I++];
5922 WeakInfo WI(AliasId, Loc);
5923 WI.setUsed(Used);
5924 WeakIDs.push_back(std::make_pair(WeakId, WI));
5925 }
5926 WeakUndeclaredIdentifiers.clear();
5927}
5928
5929void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
5930 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
5931 ExternalVTableUse VT;
5932 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
5933 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
5934 VT.DefinitionRequired = VTableUses[Idx++];
5935 VTables.push_back(VT);
5936 }
5937
5938 VTableUses.clear();
5939}
5940
5941void ASTReader::ReadPendingInstantiations(
5942 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
5943 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
5944 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
5945 SourceLocation Loc
5946 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
5947
5948 Pending.push_back(std::make_pair(D, Loc));
5949 }
5950 PendingInstantiations.clear();
5951}
5952
5953void ASTReader::LoadSelector(Selector Sel) {
5954 // It would be complicated to avoid reading the methods anyway. So don't.
5955 ReadMethodPool(Sel);
5956}
5957
5958void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
5959 assert(ID && "Non-zero identifier ID required");
5960 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
5961 IdentifiersLoaded[ID - 1] = II;
5962 if (DeserializationListener)
5963 DeserializationListener->IdentifierRead(ID, II);
5964}
5965
5966/// \brief Set the globally-visible declarations associated with the given
5967/// identifier.
5968///
5969/// If the AST reader is currently in a state where the given declaration IDs
5970/// cannot safely be resolved, they are queued until it is safe to resolve
5971/// them.
5972///
5973/// \param II an IdentifierInfo that refers to one or more globally-visible
5974/// declarations.
5975///
5976/// \param DeclIDs the set of declaration IDs with the name @p II that are
5977/// visible at global scope.
5978///
5979/// \param Nonrecursive should be true to indicate that the caller knows that
5980/// this call is non-recursive, and therefore the globally-visible declarations
5981/// will not be placed onto the pending queue.
5982void
5983ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
5984 const SmallVectorImpl<uint32_t> &DeclIDs,
5985 bool Nonrecursive) {
5986 if (NumCurrentElementsDeserializing && !Nonrecursive) {
5987 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
5988 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
5989 PII.II = II;
5990 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
5991 return;
5992 }
5993
5994 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
5995 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
5996 if (SemaObj) {
5997 // Introduce this declaration into the translation-unit scope
5998 // and add it to the declaration chain for this identifier, so
5999 // that (unqualified) name lookup will find it.
6000 SemaObj->pushExternalDeclIntoScope(D, II);
6001 } else {
6002 // Queue this declaration so that it will be added to the
6003 // translation unit scope and identifier's declaration chain
6004 // once a Sema object is known.
6005 PreloadedDecls.push_back(D);
6006 }
6007 }
6008}
6009
6010IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
6011 if (ID == 0)
6012 return 0;
6013
6014 if (IdentifiersLoaded.empty()) {
6015 Error("no identifier table in AST file");
6016 return 0;
6017 }
6018
6019 ID -= 1;
6020 if (!IdentifiersLoaded[ID]) {
6021 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
6022 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
6023 ModuleFile *M = I->second;
6024 unsigned Index = ID - M->BaseIdentifierID;
6025 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
6026
6027 // All of the strings in the AST file are preceded by a 16-bit length.
6028 // Extract that 16-bit length to avoid having to execute strlen().
6029 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
6030 // unsigned integers. This is important to avoid integer overflow when
6031 // we cast them to 'unsigned'.
6032 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
6033 unsigned StrLen = (((unsigned) StrLenPtr[0])
6034 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
6035 IdentifiersLoaded[ID]
6036 = &PP.getIdentifierTable().get(StringRef(Str, StrLen));
6037 if (DeserializationListener)
6038 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
6039 }
6040
6041 return IdentifiersLoaded[ID];
6042}
6043
6044IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
6045 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
6046}
6047
6048IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
6049 if (LocalID < NUM_PREDEF_IDENT_IDS)
6050 return LocalID;
6051
6052 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6053 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
6054 assert(I != M.IdentifierRemap.end()
6055 && "Invalid index into identifier index remap");
6056
6057 return LocalID + I->second;
6058}
6059
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00006060MacroInfo *ASTReader::getMacro(MacroID ID, MacroInfo *Hint) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006061 if (ID == 0)
6062 return 0;
6063
6064 if (MacrosLoaded.empty()) {
6065 Error("no macro table in AST file");
6066 return 0;
6067 }
6068
6069 ID -= NUM_PREDEF_MACRO_IDS;
6070 if (!MacrosLoaded[ID]) {
6071 GlobalMacroMapType::iterator I
6072 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
6073 assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
6074 ModuleFile *M = I->second;
6075 unsigned Index = ID - M->BaseMacroID;
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00006076 ReadMacroRecord(*M, M->MacroOffsets[Index], Hint);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006077 }
6078
6079 return MacrosLoaded[ID];
6080}
6081
6082MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
6083 if (LocalID < NUM_PREDEF_MACRO_IDS)
6084 return LocalID;
6085
6086 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6087 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
6088 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
6089
6090 return LocalID + I->second;
6091}
6092
6093serialization::SubmoduleID
6094ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
6095 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
6096 return LocalID;
6097
6098 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6099 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
6100 assert(I != M.SubmoduleRemap.end()
6101 && "Invalid index into submodule index remap");
6102
6103 return LocalID + I->second;
6104}
6105
6106Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
6107 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
6108 assert(GlobalID == 0 && "Unhandled global submodule ID");
6109 return 0;
6110 }
6111
6112 if (GlobalID > SubmodulesLoaded.size()) {
6113 Error("submodule ID out of range in AST file");
6114 return 0;
6115 }
6116
6117 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
6118}
Douglas Gregorca2ab452013-01-12 01:29:50 +00006119
6120Module *ASTReader::getModule(unsigned ID) {
6121 return getSubmodule(ID);
6122}
6123
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006124Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
6125 return DecodeSelector(getGlobalSelectorID(M, LocalID));
6126}
6127
6128Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
6129 if (ID == 0)
6130 return Selector();
6131
6132 if (ID > SelectorsLoaded.size()) {
6133 Error("selector ID out of range in AST file");
6134 return Selector();
6135 }
6136
6137 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
6138 // Load this selector from the selector table.
6139 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
6140 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
6141 ModuleFile &M = *I->second;
6142 ASTSelectorLookupTrait Trait(*this, M);
6143 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
6144 SelectorsLoaded[ID - 1] =
6145 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
6146 if (DeserializationListener)
6147 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
6148 }
6149
6150 return SelectorsLoaded[ID - 1];
6151}
6152
6153Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
6154 return DecodeSelector(ID);
6155}
6156
6157uint32_t ASTReader::GetNumExternalSelectors() {
6158 // ID 0 (the null selector) is considered an external selector.
6159 return getTotalNumSelectors() + 1;
6160}
6161
6162serialization::SelectorID
6163ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
6164 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
6165 return LocalID;
6166
6167 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6168 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
6169 assert(I != M.SelectorRemap.end()
6170 && "Invalid index into selector index remap");
6171
6172 return LocalID + I->second;
6173}
6174
6175DeclarationName
6176ASTReader::ReadDeclarationName(ModuleFile &F,
6177 const RecordData &Record, unsigned &Idx) {
6178 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
6179 switch (Kind) {
6180 case DeclarationName::Identifier:
6181 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
6182
6183 case DeclarationName::ObjCZeroArgSelector:
6184 case DeclarationName::ObjCOneArgSelector:
6185 case DeclarationName::ObjCMultiArgSelector:
6186 return DeclarationName(ReadSelector(F, Record, Idx));
6187
6188 case DeclarationName::CXXConstructorName:
6189 return Context.DeclarationNames.getCXXConstructorName(
6190 Context.getCanonicalType(readType(F, Record, Idx)));
6191
6192 case DeclarationName::CXXDestructorName:
6193 return Context.DeclarationNames.getCXXDestructorName(
6194 Context.getCanonicalType(readType(F, Record, Idx)));
6195
6196 case DeclarationName::CXXConversionFunctionName:
6197 return Context.DeclarationNames.getCXXConversionFunctionName(
6198 Context.getCanonicalType(readType(F, Record, Idx)));
6199
6200 case DeclarationName::CXXOperatorName:
6201 return Context.DeclarationNames.getCXXOperatorName(
6202 (OverloadedOperatorKind)Record[Idx++]);
6203
6204 case DeclarationName::CXXLiteralOperatorName:
6205 return Context.DeclarationNames.getCXXLiteralOperatorName(
6206 GetIdentifierInfo(F, Record, Idx));
6207
6208 case DeclarationName::CXXUsingDirective:
6209 return DeclarationName::getUsingDirectiveName();
6210 }
6211
6212 llvm_unreachable("Invalid NameKind!");
6213}
6214
6215void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
6216 DeclarationNameLoc &DNLoc,
6217 DeclarationName Name,
6218 const RecordData &Record, unsigned &Idx) {
6219 switch (Name.getNameKind()) {
6220 case DeclarationName::CXXConstructorName:
6221 case DeclarationName::CXXDestructorName:
6222 case DeclarationName::CXXConversionFunctionName:
6223 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
6224 break;
6225
6226 case DeclarationName::CXXOperatorName:
6227 DNLoc.CXXOperatorName.BeginOpNameLoc
6228 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6229 DNLoc.CXXOperatorName.EndOpNameLoc
6230 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6231 break;
6232
6233 case DeclarationName::CXXLiteralOperatorName:
6234 DNLoc.CXXLiteralOperatorName.OpNameLoc
6235 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6236 break;
6237
6238 case DeclarationName::Identifier:
6239 case DeclarationName::ObjCZeroArgSelector:
6240 case DeclarationName::ObjCOneArgSelector:
6241 case DeclarationName::ObjCMultiArgSelector:
6242 case DeclarationName::CXXUsingDirective:
6243 break;
6244 }
6245}
6246
6247void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
6248 DeclarationNameInfo &NameInfo,
6249 const RecordData &Record, unsigned &Idx) {
6250 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
6251 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
6252 DeclarationNameLoc DNLoc;
6253 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
6254 NameInfo.setInfo(DNLoc);
6255}
6256
6257void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
6258 const RecordData &Record, unsigned &Idx) {
6259 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
6260 unsigned NumTPLists = Record[Idx++];
6261 Info.NumTemplParamLists = NumTPLists;
6262 if (NumTPLists) {
6263 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
6264 for (unsigned i=0; i != NumTPLists; ++i)
6265 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
6266 }
6267}
6268
6269TemplateName
6270ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
6271 unsigned &Idx) {
6272 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
6273 switch (Kind) {
6274 case TemplateName::Template:
6275 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
6276
6277 case TemplateName::OverloadedTemplate: {
6278 unsigned size = Record[Idx++];
6279 UnresolvedSet<8> Decls;
6280 while (size--)
6281 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
6282
6283 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
6284 }
6285
6286 case TemplateName::QualifiedTemplate: {
6287 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
6288 bool hasTemplKeyword = Record[Idx++];
6289 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
6290 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
6291 }
6292
6293 case TemplateName::DependentTemplate: {
6294 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
6295 if (Record[Idx++]) // isIdentifier
6296 return Context.getDependentTemplateName(NNS,
6297 GetIdentifierInfo(F, Record,
6298 Idx));
6299 return Context.getDependentTemplateName(NNS,
6300 (OverloadedOperatorKind)Record[Idx++]);
6301 }
6302
6303 case TemplateName::SubstTemplateTemplateParm: {
6304 TemplateTemplateParmDecl *param
6305 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
6306 if (!param) return TemplateName();
6307 TemplateName replacement = ReadTemplateName(F, Record, Idx);
6308 return Context.getSubstTemplateTemplateParm(param, replacement);
6309 }
6310
6311 case TemplateName::SubstTemplateTemplateParmPack: {
6312 TemplateTemplateParmDecl *Param
6313 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
6314 if (!Param)
6315 return TemplateName();
6316
6317 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
6318 if (ArgPack.getKind() != TemplateArgument::Pack)
6319 return TemplateName();
6320
6321 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
6322 }
6323 }
6324
6325 llvm_unreachable("Unhandled template name kind!");
6326}
6327
6328TemplateArgument
6329ASTReader::ReadTemplateArgument(ModuleFile &F,
6330 const RecordData &Record, unsigned &Idx) {
6331 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
6332 switch (Kind) {
6333 case TemplateArgument::Null:
6334 return TemplateArgument();
6335 case TemplateArgument::Type:
6336 return TemplateArgument(readType(F, Record, Idx));
6337 case TemplateArgument::Declaration: {
6338 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
6339 bool ForReferenceParam = Record[Idx++];
6340 return TemplateArgument(D, ForReferenceParam);
6341 }
6342 case TemplateArgument::NullPtr:
6343 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
6344 case TemplateArgument::Integral: {
6345 llvm::APSInt Value = ReadAPSInt(Record, Idx);
6346 QualType T = readType(F, Record, Idx);
6347 return TemplateArgument(Context, Value, T);
6348 }
6349 case TemplateArgument::Template:
6350 return TemplateArgument(ReadTemplateName(F, Record, Idx));
6351 case TemplateArgument::TemplateExpansion: {
6352 TemplateName Name = ReadTemplateName(F, Record, Idx);
6353 llvm::Optional<unsigned> NumTemplateExpansions;
6354 if (unsigned NumExpansions = Record[Idx++])
6355 NumTemplateExpansions = NumExpansions - 1;
6356 return TemplateArgument(Name, NumTemplateExpansions);
6357 }
6358 case TemplateArgument::Expression:
6359 return TemplateArgument(ReadExpr(F));
6360 case TemplateArgument::Pack: {
6361 unsigned NumArgs = Record[Idx++];
6362 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
6363 for (unsigned I = 0; I != NumArgs; ++I)
6364 Args[I] = ReadTemplateArgument(F, Record, Idx);
6365 return TemplateArgument(Args, NumArgs);
6366 }
6367 }
6368
6369 llvm_unreachable("Unhandled template argument kind!");
6370}
6371
6372TemplateParameterList *
6373ASTReader::ReadTemplateParameterList(ModuleFile &F,
6374 const RecordData &Record, unsigned &Idx) {
6375 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
6376 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
6377 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
6378
6379 unsigned NumParams = Record[Idx++];
6380 SmallVector<NamedDecl *, 16> Params;
6381 Params.reserve(NumParams);
6382 while (NumParams--)
6383 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
6384
6385 TemplateParameterList* TemplateParams =
6386 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
6387 Params.data(), Params.size(), RAngleLoc);
6388 return TemplateParams;
6389}
6390
6391void
6392ASTReader::
6393ReadTemplateArgumentList(SmallVector<TemplateArgument, 8> &TemplArgs,
6394 ModuleFile &F, const RecordData &Record,
6395 unsigned &Idx) {
6396 unsigned NumTemplateArgs = Record[Idx++];
6397 TemplArgs.reserve(NumTemplateArgs);
6398 while (NumTemplateArgs--)
6399 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
6400}
6401
6402/// \brief Read a UnresolvedSet structure.
6403void ASTReader::ReadUnresolvedSet(ModuleFile &F, ASTUnresolvedSet &Set,
6404 const RecordData &Record, unsigned &Idx) {
6405 unsigned NumDecls = Record[Idx++];
6406 Set.reserve(Context, NumDecls);
6407 while (NumDecls--) {
6408 NamedDecl *D = ReadDeclAs<NamedDecl>(F, Record, Idx);
6409 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
6410 Set.addDecl(Context, D, AS);
6411 }
6412}
6413
6414CXXBaseSpecifier
6415ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
6416 const RecordData &Record, unsigned &Idx) {
6417 bool isVirtual = static_cast<bool>(Record[Idx++]);
6418 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
6419 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
6420 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
6421 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
6422 SourceRange Range = ReadSourceRange(F, Record, Idx);
6423 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
6424 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
6425 EllipsisLoc);
6426 Result.setInheritConstructors(inheritConstructors);
6427 return Result;
6428}
6429
6430std::pair<CXXCtorInitializer **, unsigned>
6431ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
6432 unsigned &Idx) {
6433 CXXCtorInitializer **CtorInitializers = 0;
6434 unsigned NumInitializers = Record[Idx++];
6435 if (NumInitializers) {
6436 CtorInitializers
6437 = new (Context) CXXCtorInitializer*[NumInitializers];
6438 for (unsigned i=0; i != NumInitializers; ++i) {
6439 TypeSourceInfo *TInfo = 0;
6440 bool IsBaseVirtual = false;
6441 FieldDecl *Member = 0;
6442 IndirectFieldDecl *IndirectMember = 0;
6443
6444 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
6445 switch (Type) {
6446 case CTOR_INITIALIZER_BASE:
6447 TInfo = GetTypeSourceInfo(F, Record, Idx);
6448 IsBaseVirtual = Record[Idx++];
6449 break;
6450
6451 case CTOR_INITIALIZER_DELEGATING:
6452 TInfo = GetTypeSourceInfo(F, Record, Idx);
6453 break;
6454
6455 case CTOR_INITIALIZER_MEMBER:
6456 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
6457 break;
6458
6459 case CTOR_INITIALIZER_INDIRECT_MEMBER:
6460 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
6461 break;
6462 }
6463
6464 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
6465 Expr *Init = ReadExpr(F);
6466 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
6467 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
6468 bool IsWritten = Record[Idx++];
6469 unsigned SourceOrderOrNumArrayIndices;
6470 SmallVector<VarDecl *, 8> Indices;
6471 if (IsWritten) {
6472 SourceOrderOrNumArrayIndices = Record[Idx++];
6473 } else {
6474 SourceOrderOrNumArrayIndices = Record[Idx++];
6475 Indices.reserve(SourceOrderOrNumArrayIndices);
6476 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
6477 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
6478 }
6479
6480 CXXCtorInitializer *BOMInit;
6481 if (Type == CTOR_INITIALIZER_BASE) {
6482 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, IsBaseVirtual,
6483 LParenLoc, Init, RParenLoc,
6484 MemberOrEllipsisLoc);
6485 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
6486 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, LParenLoc,
6487 Init, RParenLoc);
6488 } else if (IsWritten) {
6489 if (Member)
6490 BOMInit = new (Context) CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc,
6491 LParenLoc, Init, RParenLoc);
6492 else
6493 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
6494 MemberOrEllipsisLoc, LParenLoc,
6495 Init, RParenLoc);
6496 } else {
6497 BOMInit = CXXCtorInitializer::Create(Context, Member, MemberOrEllipsisLoc,
6498 LParenLoc, Init, RParenLoc,
6499 Indices.data(), Indices.size());
6500 }
6501
6502 if (IsWritten)
6503 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
6504 CtorInitializers[i] = BOMInit;
6505 }
6506 }
6507
6508 return std::make_pair(CtorInitializers, NumInitializers);
6509}
6510
6511NestedNameSpecifier *
6512ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
6513 const RecordData &Record, unsigned &Idx) {
6514 unsigned N = Record[Idx++];
6515 NestedNameSpecifier *NNS = 0, *Prev = 0;
6516 for (unsigned I = 0; I != N; ++I) {
6517 NestedNameSpecifier::SpecifierKind Kind
6518 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
6519 switch (Kind) {
6520 case NestedNameSpecifier::Identifier: {
6521 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
6522 NNS = NestedNameSpecifier::Create(Context, Prev, II);
6523 break;
6524 }
6525
6526 case NestedNameSpecifier::Namespace: {
6527 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
6528 NNS = NestedNameSpecifier::Create(Context, Prev, NS);
6529 break;
6530 }
6531
6532 case NestedNameSpecifier::NamespaceAlias: {
6533 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
6534 NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
6535 break;
6536 }
6537
6538 case NestedNameSpecifier::TypeSpec:
6539 case NestedNameSpecifier::TypeSpecWithTemplate: {
6540 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
6541 if (!T)
6542 return 0;
6543
6544 bool Template = Record[Idx++];
6545 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
6546 break;
6547 }
6548
6549 case NestedNameSpecifier::Global: {
6550 NNS = NestedNameSpecifier::GlobalSpecifier(Context);
6551 // No associated value, and there can't be a prefix.
6552 break;
6553 }
6554 }
6555 Prev = NNS;
6556 }
6557 return NNS;
6558}
6559
6560NestedNameSpecifierLoc
6561ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
6562 unsigned &Idx) {
6563 unsigned N = Record[Idx++];
6564 NestedNameSpecifierLocBuilder Builder;
6565 for (unsigned I = 0; I != N; ++I) {
6566 NestedNameSpecifier::SpecifierKind Kind
6567 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
6568 switch (Kind) {
6569 case NestedNameSpecifier::Identifier: {
6570 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
6571 SourceRange Range = ReadSourceRange(F, Record, Idx);
6572 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
6573 break;
6574 }
6575
6576 case NestedNameSpecifier::Namespace: {
6577 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
6578 SourceRange Range = ReadSourceRange(F, Record, Idx);
6579 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
6580 break;
6581 }
6582
6583 case NestedNameSpecifier::NamespaceAlias: {
6584 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
6585 SourceRange Range = ReadSourceRange(F, Record, Idx);
6586 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
6587 break;
6588 }
6589
6590 case NestedNameSpecifier::TypeSpec:
6591 case NestedNameSpecifier::TypeSpecWithTemplate: {
6592 bool Template = Record[Idx++];
6593 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
6594 if (!T)
6595 return NestedNameSpecifierLoc();
6596 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
6597
6598 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
6599 Builder.Extend(Context,
6600 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
6601 T->getTypeLoc(), ColonColonLoc);
6602 break;
6603 }
6604
6605 case NestedNameSpecifier::Global: {
6606 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
6607 Builder.MakeGlobal(Context, ColonColonLoc);
6608 break;
6609 }
6610 }
6611 }
6612
6613 return Builder.getWithLocInContext(Context);
6614}
6615
6616SourceRange
6617ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
6618 unsigned &Idx) {
6619 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
6620 SourceLocation end = ReadSourceLocation(F, Record, Idx);
6621 return SourceRange(beg, end);
6622}
6623
6624/// \brief Read an integral value
6625llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
6626 unsigned BitWidth = Record[Idx++];
6627 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
6628 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
6629 Idx += NumWords;
6630 return Result;
6631}
6632
6633/// \brief Read a signed integral value
6634llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
6635 bool isUnsigned = Record[Idx++];
6636 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
6637}
6638
6639/// \brief Read a floating-point value
6640llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
6641 return llvm::APFloat(ReadAPInt(Record, Idx));
6642}
6643
6644// \brief Read a string
6645std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
6646 unsigned Len = Record[Idx++];
6647 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
6648 Idx += Len;
6649 return Result;
6650}
6651
6652VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
6653 unsigned &Idx) {
6654 unsigned Major = Record[Idx++];
6655 unsigned Minor = Record[Idx++];
6656 unsigned Subminor = Record[Idx++];
6657 if (Minor == 0)
6658 return VersionTuple(Major);
6659 if (Subminor == 0)
6660 return VersionTuple(Major, Minor - 1);
6661 return VersionTuple(Major, Minor - 1, Subminor - 1);
6662}
6663
6664CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
6665 const RecordData &Record,
6666 unsigned &Idx) {
6667 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
6668 return CXXTemporary::Create(Context, Decl);
6669}
6670
6671DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
6672 return Diag(SourceLocation(), DiagID);
6673}
6674
6675DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
6676 return Diags.Report(Loc, DiagID);
6677}
6678
6679/// \brief Retrieve the identifier table associated with the
6680/// preprocessor.
6681IdentifierTable &ASTReader::getIdentifierTable() {
6682 return PP.getIdentifierTable();
6683}
6684
6685/// \brief Record that the given ID maps to the given switch-case
6686/// statement.
6687void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
6688 assert((*CurrSwitchCaseStmts)[ID] == 0 &&
6689 "Already have a SwitchCase with this ID");
6690 (*CurrSwitchCaseStmts)[ID] = SC;
6691}
6692
6693/// \brief Retrieve the switch-case statement with the given ID.
6694SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
6695 assert((*CurrSwitchCaseStmts)[ID] != 0 && "No SwitchCase with this ID");
6696 return (*CurrSwitchCaseStmts)[ID];
6697}
6698
6699void ASTReader::ClearSwitchCaseIDs() {
6700 CurrSwitchCaseStmts->clear();
6701}
6702
6703void ASTReader::ReadComments() {
6704 std::vector<RawComment *> Comments;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00006705 for (SmallVectorImpl<std::pair<BitstreamCursor,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006706 serialization::ModuleFile *> >::iterator
6707 I = CommentsCursors.begin(),
6708 E = CommentsCursors.end();
6709 I != E; ++I) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00006710 BitstreamCursor &Cursor = I->first;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006711 serialization::ModuleFile &F = *I->second;
6712 SavedStreamPosition SavedPosition(Cursor);
6713
6714 RecordData Record;
6715 while (true) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00006716 llvm::BitstreamEntry Entry =
6717 Cursor.advanceSkippingSubblocks(BitstreamCursor::AF_DontPopBlockAtEnd);
6718
6719 switch (Entry.Kind) {
6720 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
6721 case llvm::BitstreamEntry::Error:
6722 Error("malformed block record in AST file");
6723 return;
6724 case llvm::BitstreamEntry::EndBlock:
6725 goto NextCursor;
6726 case llvm::BitstreamEntry::Record:
6727 // The interesting case.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006728 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006729 }
6730
6731 // Read a record.
6732 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00006733 switch ((CommentRecordTypes)Cursor.readRecord(Entry.ID, Record)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006734 case COMMENTS_RAW_COMMENT: {
6735 unsigned Idx = 0;
6736 SourceRange SR = ReadSourceRange(F, Record, Idx);
6737 RawComment::CommentKind Kind =
6738 (RawComment::CommentKind) Record[Idx++];
6739 bool IsTrailingComment = Record[Idx++];
6740 bool IsAlmostTrailingComment = Record[Idx++];
6741 Comments.push_back(new (Context) RawComment(SR, Kind,
6742 IsTrailingComment,
6743 IsAlmostTrailingComment));
6744 break;
6745 }
6746 }
6747 }
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00006748 NextCursor:;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006749 }
6750 Context.Comments.addCommentsToFront(Comments);
6751}
6752
6753void ASTReader::finishPendingActions() {
6754 while (!PendingIdentifierInfos.empty() || !PendingDeclChains.empty() ||
6755 !PendingMacroIDs.empty()) {
6756 // If any identifiers with corresponding top-level declarations have
6757 // been loaded, load those declarations now.
6758 while (!PendingIdentifierInfos.empty()) {
6759 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
6760 PendingIdentifierInfos.front().DeclIDs, true);
6761 PendingIdentifierInfos.pop_front();
6762 }
6763
6764 // Load pending declaration chains.
6765 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) {
6766 loadPendingDeclChain(PendingDeclChains[I]);
6767 PendingDeclChainsKnown.erase(PendingDeclChains[I]);
6768 }
6769 PendingDeclChains.clear();
6770
6771 // Load any pending macro definitions.
6772 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00006773 // FIXME: std::move here
6774 SmallVector<MacroID, 2> GlobalIDs = PendingMacroIDs.begin()[I].second;
6775 MacroInfo *Hint = 0;
6776 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
6777 ++IDIdx) {
6778 Hint = getMacro(GlobalIDs[IDIdx], Hint);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006779 }
6780 }
6781 PendingMacroIDs.clear();
6782 }
6783
6784 // If we deserialized any C++ or Objective-C class definitions, any
6785 // Objective-C protocol definitions, or any redeclarable templates, make sure
6786 // that all redeclarations point to the definitions. Note that this can only
6787 // happen now, after the redeclaration chains have been fully wired.
6788 for (llvm::SmallPtrSet<Decl *, 4>::iterator D = PendingDefinitions.begin(),
6789 DEnd = PendingDefinitions.end();
6790 D != DEnd; ++D) {
6791 if (TagDecl *TD = dyn_cast<TagDecl>(*D)) {
6792 if (const TagType *TagT = dyn_cast<TagType>(TD->TypeForDecl)) {
6793 // Make sure that the TagType points at the definition.
6794 const_cast<TagType*>(TagT)->decl = TD;
6795 }
6796
6797 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(*D)) {
6798 for (CXXRecordDecl::redecl_iterator R = RD->redecls_begin(),
6799 REnd = RD->redecls_end();
6800 R != REnd; ++R)
6801 cast<CXXRecordDecl>(*R)->DefinitionData = RD->DefinitionData;
6802
6803 }
6804
6805 continue;
6806 }
6807
6808 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(*D)) {
6809 // Make sure that the ObjCInterfaceType points at the definition.
6810 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
6811 ->Decl = ID;
6812
6813 for (ObjCInterfaceDecl::redecl_iterator R = ID->redecls_begin(),
6814 REnd = ID->redecls_end();
6815 R != REnd; ++R)
6816 R->Data = ID->Data;
6817
6818 continue;
6819 }
6820
6821 if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(*D)) {
6822 for (ObjCProtocolDecl::redecl_iterator R = PD->redecls_begin(),
6823 REnd = PD->redecls_end();
6824 R != REnd; ++R)
6825 R->Data = PD->Data;
6826
6827 continue;
6828 }
6829
6830 RedeclarableTemplateDecl *RTD
6831 = cast<RedeclarableTemplateDecl>(*D)->getCanonicalDecl();
6832 for (RedeclarableTemplateDecl::redecl_iterator R = RTD->redecls_begin(),
6833 REnd = RTD->redecls_end();
6834 R != REnd; ++R)
6835 R->Common = RTD->Common;
6836 }
6837 PendingDefinitions.clear();
6838
6839 // Load the bodies of any functions or methods we've encountered. We do
6840 // this now (delayed) so that we can be sure that the declaration chains
6841 // have been fully wired up.
6842 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
6843 PBEnd = PendingBodies.end();
6844 PB != PBEnd; ++PB) {
6845 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
6846 // FIXME: Check for =delete/=default?
6847 // FIXME: Complain about ODR violations here?
6848 if (!getContext().getLangOpts().Modules || !FD->hasBody())
6849 FD->setLazyBody(PB->second);
6850 continue;
6851 }
6852
6853 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
6854 if (!getContext().getLangOpts().Modules || !MD->hasBody())
6855 MD->setLazyBody(PB->second);
6856 }
6857 PendingBodies.clear();
6858}
6859
6860void ASTReader::FinishedDeserializing() {
6861 assert(NumCurrentElementsDeserializing &&
6862 "FinishedDeserializing not paired with StartedDeserializing");
6863 if (NumCurrentElementsDeserializing == 1) {
6864 // We decrease NumCurrentElementsDeserializing only after pending actions
6865 // are finished, to avoid recursively re-calling finishPendingActions().
6866 finishPendingActions();
6867 }
6868 --NumCurrentElementsDeserializing;
6869
6870 if (NumCurrentElementsDeserializing == 0 &&
6871 Consumer && !PassingDeclsToConsumer) {
6872 // Guard variable to avoid recursively redoing the process of passing
6873 // decls to consumer.
6874 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
6875 true);
6876
6877 while (!InterestingDecls.empty()) {
6878 // We are not in recursive loading, so it's safe to pass the "interesting"
6879 // decls to the consumer.
6880 Decl *D = InterestingDecls.front();
6881 InterestingDecls.pop_front();
6882 PassInterestingDeclToConsumer(D);
6883 }
6884 }
6885}
6886
6887ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context,
6888 StringRef isysroot, bool DisableValidation,
6889 bool AllowASTWithCompilerErrors)
6890 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
6891 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
6892 Diags(PP.getDiagnostics()), SemaObj(0), PP(PP), Context(Context),
6893 Consumer(0), ModuleMgr(PP.getFileManager()),
6894 isysroot(isysroot), DisableValidation(DisableValidation),
6895 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
6896 CurrentGeneration(0), CurrSwitchCaseStmts(&SwitchCaseStmts),
6897 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
6898 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
6899 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
6900 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
6901 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
6902 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
6903 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
6904 PassingDeclsToConsumer(false),
6905 NumCXXBaseSpecifiersLoaded(0)
6906{
6907 SourceMgr.setExternalSLocEntrySource(this);
6908}
6909
6910ASTReader::~ASTReader() {
6911 for (DeclContextVisibleUpdatesPending::iterator
6912 I = PendingVisibleUpdates.begin(),
6913 E = PendingVisibleUpdates.end();
6914 I != E; ++I) {
6915 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
6916 F = I->second.end();
6917 J != F; ++J)
6918 delete J->first;
6919 }
6920}