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