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