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