blob: 0261ad8f48307a9a064612ac7f308ed846a00fcf [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++];
3694 bool IsInternal = Record[Idx++];
3695 bool ImplicitExternC = Record[Idx++];
3696 HSOpts.UserEntries.push_back(
Daniel Dunbareab66522013-01-25 01:50:47 +00003697 HeaderSearchOptions::Entry(Path, Group, IsFramework, IgnoreSysRoot,
3698 IsInternal, ImplicitExternC));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003699 }
3700
3701 // System header prefixes.
3702 for (unsigned N = Record[Idx++]; N; --N) {
3703 std::string Prefix = ReadString(Record, Idx);
3704 bool IsSystemHeader = Record[Idx++];
3705 HSOpts.SystemHeaderPrefixes.push_back(
3706 HeaderSearchOptions::SystemHeaderPrefix(Prefix, IsSystemHeader));
3707 }
3708
3709 HSOpts.ResourceDir = ReadString(Record, Idx);
3710 HSOpts.ModuleCachePath = ReadString(Record, Idx);
3711 HSOpts.DisableModuleHash = Record[Idx++];
3712 HSOpts.UseBuiltinIncludes = Record[Idx++];
3713 HSOpts.UseStandardSystemIncludes = Record[Idx++];
3714 HSOpts.UseStandardCXXIncludes = Record[Idx++];
3715 HSOpts.UseLibcxx = Record[Idx++];
3716
3717 return Listener.ReadHeaderSearchOptions(HSOpts, Complain);
3718}
3719
3720bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
3721 bool Complain,
3722 ASTReaderListener &Listener,
3723 std::string &SuggestedPredefines) {
3724 PreprocessorOptions PPOpts;
3725 unsigned Idx = 0;
3726
3727 // Macro definitions/undefs
3728 for (unsigned N = Record[Idx++]; N; --N) {
3729 std::string Macro = ReadString(Record, Idx);
3730 bool IsUndef = Record[Idx++];
3731 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
3732 }
3733
3734 // Includes
3735 for (unsigned N = Record[Idx++]; N; --N) {
3736 PPOpts.Includes.push_back(ReadString(Record, Idx));
3737 }
3738
3739 // Macro Includes
3740 for (unsigned N = Record[Idx++]; N; --N) {
3741 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
3742 }
3743
3744 PPOpts.UsePredefines = Record[Idx++];
3745 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
3746 PPOpts.ImplicitPTHInclude = ReadString(Record, Idx);
3747 PPOpts.ObjCXXARCStandardLibrary =
3748 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
3749 SuggestedPredefines.clear();
3750 return Listener.ReadPreprocessorOptions(PPOpts, Complain,
3751 SuggestedPredefines);
3752}
3753
3754std::pair<ModuleFile *, unsigned>
3755ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
3756 GlobalPreprocessedEntityMapType::iterator
3757 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
3758 assert(I != GlobalPreprocessedEntityMap.end() &&
3759 "Corrupted global preprocessed entity map");
3760 ModuleFile *M = I->second;
3761 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
3762 return std::make_pair(M, LocalIndex);
3763}
3764
3765std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
3766ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
3767 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
3768 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
3769 Mod.NumPreprocessedEntities);
3770
3771 return std::make_pair(PreprocessingRecord::iterator(),
3772 PreprocessingRecord::iterator());
3773}
3774
3775std::pair<ASTReader::ModuleDeclIterator, ASTReader::ModuleDeclIterator>
3776ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
3777 return std::make_pair(ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
3778 ModuleDeclIterator(this, &Mod,
3779 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
3780}
3781
3782PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
3783 PreprocessedEntityID PPID = Index+1;
3784 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
3785 ModuleFile &M = *PPInfo.first;
3786 unsigned LocalIndex = PPInfo.second;
3787 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
3788
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003789 if (!PP.getPreprocessingRecord()) {
3790 Error("no preprocessing record");
3791 return 0;
3792 }
3793
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003794 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
3795 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
3796
3797 llvm::BitstreamEntry Entry =
3798 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
3799 if (Entry.Kind != llvm::BitstreamEntry::Record)
3800 return 0;
3801
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003802 // Read the record.
3803 SourceRange Range(ReadSourceLocation(M, PPOffs.Begin),
3804 ReadSourceLocation(M, PPOffs.End));
3805 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003806 StringRef Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003807 RecordData Record;
3808 PreprocessorDetailRecordTypes RecType =
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003809 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.readRecord(
3810 Entry.ID, Record, &Blob);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003811 switch (RecType) {
3812 case PPD_MACRO_EXPANSION: {
3813 bool isBuiltin = Record[0];
3814 IdentifierInfo *Name = 0;
3815 MacroDefinition *Def = 0;
3816 if (isBuiltin)
3817 Name = getLocalIdentifier(M, Record[1]);
3818 else {
3819 PreprocessedEntityID
3820 GlobalID = getGlobalPreprocessedEntityID(M, Record[1]);
3821 Def =cast<MacroDefinition>(PPRec.getLoadedPreprocessedEntity(GlobalID-1));
3822 }
3823
3824 MacroExpansion *ME;
3825 if (isBuiltin)
3826 ME = new (PPRec) MacroExpansion(Name, Range);
3827 else
3828 ME = new (PPRec) MacroExpansion(Def, Range);
3829
3830 return ME;
3831 }
3832
3833 case PPD_MACRO_DEFINITION: {
3834 // Decode the identifier info and then check again; if the macro is
3835 // still defined and associated with the identifier,
3836 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
3837 MacroDefinition *MD
3838 = new (PPRec) MacroDefinition(II, Range);
3839
3840 if (DeserializationListener)
3841 DeserializationListener->MacroDefinitionRead(PPID, MD);
3842
3843 return MD;
3844 }
3845
3846 case PPD_INCLUSION_DIRECTIVE: {
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003847 const char *FullFileNameStart = Blob.data() + Record[0];
3848 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003849 const FileEntry *File = 0;
3850 if (!FullFileName.empty())
3851 File = PP.getFileManager().getFile(FullFileName);
3852
3853 // FIXME: Stable encoding
3854 InclusionDirective::InclusionKind Kind
3855 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
3856 InclusionDirective *ID
3857 = new (PPRec) InclusionDirective(PPRec, Kind,
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003858 StringRef(Blob.data(), Record[0]),
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003859 Record[1], Record[3],
3860 File,
3861 Range);
3862 return ID;
3863 }
3864 }
3865
3866 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
3867}
3868
3869/// \brief \arg SLocMapI points at a chunk of a module that contains no
3870/// preprocessed entities or the entities it contains are not the ones we are
3871/// looking for. Find the next module that contains entities and return the ID
3872/// of the first entry.
3873PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
3874 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
3875 ++SLocMapI;
3876 for (GlobalSLocOffsetMapType::const_iterator
3877 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
3878 ModuleFile &M = *SLocMapI->second;
3879 if (M.NumPreprocessedEntities)
3880 return M.BasePreprocessedEntityID;
3881 }
3882
3883 return getTotalNumPreprocessedEntities();
3884}
3885
3886namespace {
3887
3888template <unsigned PPEntityOffset::*PPLoc>
3889struct PPEntityComp {
3890 const ASTReader &Reader;
3891 ModuleFile &M;
3892
3893 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) { }
3894
3895 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
3896 SourceLocation LHS = getLoc(L);
3897 SourceLocation RHS = getLoc(R);
3898 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
3899 }
3900
3901 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
3902 SourceLocation LHS = getLoc(L);
3903 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
3904 }
3905
3906 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
3907 SourceLocation RHS = getLoc(R);
3908 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
3909 }
3910
3911 SourceLocation getLoc(const PPEntityOffset &PPE) const {
3912 return Reader.ReadSourceLocation(M, PPE.*PPLoc);
3913 }
3914};
3915
3916}
3917
3918/// \brief Returns the first preprocessed entity ID that ends after \arg BLoc.
3919PreprocessedEntityID
3920ASTReader::findBeginPreprocessedEntity(SourceLocation BLoc) const {
3921 if (SourceMgr.isLocalSourceLocation(BLoc))
3922 return getTotalNumPreprocessedEntities();
3923
3924 GlobalSLocOffsetMapType::const_iterator
3925 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
3926 BLoc.getOffset());
3927 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
3928 "Corrupted global sloc offset map");
3929
3930 if (SLocMapI->second->NumPreprocessedEntities == 0)
3931 return findNextPreprocessedEntity(SLocMapI);
3932
3933 ModuleFile &M = *SLocMapI->second;
3934 typedef const PPEntityOffset *pp_iterator;
3935 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
3936 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
3937
3938 size_t Count = M.NumPreprocessedEntities;
3939 size_t Half;
3940 pp_iterator First = pp_begin;
3941 pp_iterator PPI;
3942
3943 // Do a binary search manually instead of using std::lower_bound because
3944 // The end locations of entities may be unordered (when a macro expansion
3945 // is inside another macro argument), but for this case it is not important
3946 // whether we get the first macro expansion or its containing macro.
3947 while (Count > 0) {
3948 Half = Count/2;
3949 PPI = First;
3950 std::advance(PPI, Half);
3951 if (SourceMgr.isBeforeInTranslationUnit(ReadSourceLocation(M, PPI->End),
3952 BLoc)){
3953 First = PPI;
3954 ++First;
3955 Count = Count - Half - 1;
3956 } else
3957 Count = Half;
3958 }
3959
3960 if (PPI == pp_end)
3961 return findNextPreprocessedEntity(SLocMapI);
3962
3963 return M.BasePreprocessedEntityID + (PPI - pp_begin);
3964}
3965
3966/// \brief Returns the first preprocessed entity ID that begins after \arg ELoc.
3967PreprocessedEntityID
3968ASTReader::findEndPreprocessedEntity(SourceLocation ELoc) const {
3969 if (SourceMgr.isLocalSourceLocation(ELoc))
3970 return getTotalNumPreprocessedEntities();
3971
3972 GlobalSLocOffsetMapType::const_iterator
3973 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
3974 ELoc.getOffset());
3975 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
3976 "Corrupted global sloc offset map");
3977
3978 if (SLocMapI->second->NumPreprocessedEntities == 0)
3979 return findNextPreprocessedEntity(SLocMapI);
3980
3981 ModuleFile &M = *SLocMapI->second;
3982 typedef const PPEntityOffset *pp_iterator;
3983 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
3984 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
3985 pp_iterator PPI =
3986 std::upper_bound(pp_begin, pp_end, ELoc,
3987 PPEntityComp<&PPEntityOffset::Begin>(*this, M));
3988
3989 if (PPI == pp_end)
3990 return findNextPreprocessedEntity(SLocMapI);
3991
3992 return M.BasePreprocessedEntityID + (PPI - pp_begin);
3993}
3994
3995/// \brief Returns a pair of [Begin, End) indices of preallocated
3996/// preprocessed entities that \arg Range encompasses.
3997std::pair<unsigned, unsigned>
3998 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
3999 if (Range.isInvalid())
4000 return std::make_pair(0,0);
4001 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
4002
4003 PreprocessedEntityID BeginID = findBeginPreprocessedEntity(Range.getBegin());
4004 PreprocessedEntityID EndID = findEndPreprocessedEntity(Range.getEnd());
4005 return std::make_pair(BeginID, EndID);
4006}
4007
4008/// \brief Optionally returns true or false if the preallocated preprocessed
4009/// entity with index \arg Index came from file \arg FID.
4010llvm::Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
4011 FileID FID) {
4012 if (FID.isInvalid())
4013 return false;
4014
4015 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
4016 ModuleFile &M = *PPInfo.first;
4017 unsigned LocalIndex = PPInfo.second;
4018 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
4019
4020 SourceLocation Loc = ReadSourceLocation(M, PPOffs.Begin);
4021 if (Loc.isInvalid())
4022 return false;
4023
4024 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
4025 return true;
4026 else
4027 return false;
4028}
4029
4030namespace {
4031 /// \brief Visitor used to search for information about a header file.
4032 class HeaderFileInfoVisitor {
4033 ASTReader &Reader;
4034 const FileEntry *FE;
4035
4036 llvm::Optional<HeaderFileInfo> HFI;
4037
4038 public:
4039 HeaderFileInfoVisitor(ASTReader &Reader, const FileEntry *FE)
4040 : Reader(Reader), FE(FE) { }
4041
4042 static bool visit(ModuleFile &M, void *UserData) {
4043 HeaderFileInfoVisitor *This
4044 = static_cast<HeaderFileInfoVisitor *>(UserData);
4045
4046 HeaderFileInfoTrait Trait(This->Reader, M,
4047 &This->Reader.getPreprocessor().getHeaderSearchInfo(),
4048 M.HeaderFileFrameworkStrings,
4049 This->FE->getName());
4050
4051 HeaderFileInfoLookupTable *Table
4052 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
4053 if (!Table)
4054 return false;
4055
4056 // Look in the on-disk hash table for an entry for this file name.
4057 HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE->getName(),
4058 &Trait);
4059 if (Pos == Table->end())
4060 return false;
4061
4062 This->HFI = *Pos;
4063 return true;
4064 }
4065
4066 llvm::Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
4067 };
4068}
4069
4070HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
4071 HeaderFileInfoVisitor Visitor(*this, FE);
4072 ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor);
4073 if (llvm::Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) {
4074 if (Listener)
4075 Listener->ReadHeaderFileInfo(*HFI, FE->getUID());
4076 return *HFI;
4077 }
4078
4079 return HeaderFileInfo();
4080}
4081
4082void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
4083 // FIXME: Make it work properly with modules.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00004084 SmallVector<DiagnosticsEngine::DiagState *, 32> DiagStates;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004085 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
4086 ModuleFile &F = *(*I);
4087 unsigned Idx = 0;
4088 DiagStates.clear();
4089 assert(!Diag.DiagStates.empty());
4090 DiagStates.push_back(&Diag.DiagStates.front()); // the command-line one.
4091 while (Idx < F.PragmaDiagMappings.size()) {
4092 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
4093 unsigned DiagStateID = F.PragmaDiagMappings[Idx++];
4094 if (DiagStateID != 0) {
4095 Diag.DiagStatePoints.push_back(
4096 DiagnosticsEngine::DiagStatePoint(DiagStates[DiagStateID-1],
4097 FullSourceLoc(Loc, SourceMgr)));
4098 continue;
4099 }
4100
4101 assert(DiagStateID == 0);
4102 // A new DiagState was created here.
4103 Diag.DiagStates.push_back(*Diag.GetCurDiagState());
4104 DiagnosticsEngine::DiagState *NewState = &Diag.DiagStates.back();
4105 DiagStates.push_back(NewState);
4106 Diag.DiagStatePoints.push_back(
4107 DiagnosticsEngine::DiagStatePoint(NewState,
4108 FullSourceLoc(Loc, SourceMgr)));
4109 while (1) {
4110 assert(Idx < F.PragmaDiagMappings.size() &&
4111 "Invalid data, didn't find '-1' marking end of diag/map pairs");
4112 if (Idx >= F.PragmaDiagMappings.size()) {
4113 break; // Something is messed up but at least avoid infinite loop in
4114 // release build.
4115 }
4116 unsigned DiagID = F.PragmaDiagMappings[Idx++];
4117 if (DiagID == (unsigned)-1) {
4118 break; // no more diag/map pairs for this location.
4119 }
4120 diag::Mapping Map = (diag::Mapping)F.PragmaDiagMappings[Idx++];
4121 DiagnosticMappingInfo MappingInfo = Diag.makeMappingInfo(Map, Loc);
4122 Diag.GetCurDiagState()->setMappingInfo(DiagID, MappingInfo);
4123 }
4124 }
4125 }
4126}
4127
4128/// \brief Get the correct cursor and offset for loading a type.
4129ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
4130 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
4131 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
4132 ModuleFile *M = I->second;
4133 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
4134}
4135
4136/// \brief Read and return the type with the given index..
4137///
4138/// The index is the type ID, shifted and minus the number of predefs. This
4139/// routine actually reads the record corresponding to the type at the given
4140/// location. It is a helper routine for GetType, which deals with reading type
4141/// IDs.
4142QualType ASTReader::readTypeRecord(unsigned Index) {
4143 RecordLocation Loc = TypeCursorForIndex(Index);
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004144 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004145
4146 // Keep track of where we are in the stream, then jump back there
4147 // after reading this type.
4148 SavedStreamPosition SavedPosition(DeclsCursor);
4149
4150 ReadingKindTracker ReadingKind(Read_Type, *this);
4151
4152 // Note that we are loading a type record.
4153 Deserializing AType(this);
4154
4155 unsigned Idx = 0;
4156 DeclsCursor.JumpToBit(Loc.Offset);
4157 RecordData Record;
4158 unsigned Code = DeclsCursor.ReadCode();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00004159 switch ((TypeCode)DeclsCursor.readRecord(Code, Record)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004160 case TYPE_EXT_QUAL: {
4161 if (Record.size() != 2) {
4162 Error("Incorrect encoding of extended qualifier type");
4163 return QualType();
4164 }
4165 QualType Base = readType(*Loc.F, Record, Idx);
4166 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
4167 return Context.getQualifiedType(Base, Quals);
4168 }
4169
4170 case TYPE_COMPLEX: {
4171 if (Record.size() != 1) {
4172 Error("Incorrect encoding of complex type");
4173 return QualType();
4174 }
4175 QualType ElemType = readType(*Loc.F, Record, Idx);
4176 return Context.getComplexType(ElemType);
4177 }
4178
4179 case TYPE_POINTER: {
4180 if (Record.size() != 1) {
4181 Error("Incorrect encoding of pointer type");
4182 return QualType();
4183 }
4184 QualType PointeeType = readType(*Loc.F, Record, Idx);
4185 return Context.getPointerType(PointeeType);
4186 }
4187
4188 case TYPE_BLOCK_POINTER: {
4189 if (Record.size() != 1) {
4190 Error("Incorrect encoding of block pointer type");
4191 return QualType();
4192 }
4193 QualType PointeeType = readType(*Loc.F, Record, Idx);
4194 return Context.getBlockPointerType(PointeeType);
4195 }
4196
4197 case TYPE_LVALUE_REFERENCE: {
4198 if (Record.size() != 2) {
4199 Error("Incorrect encoding of lvalue reference type");
4200 return QualType();
4201 }
4202 QualType PointeeType = readType(*Loc.F, Record, Idx);
4203 return Context.getLValueReferenceType(PointeeType, Record[1]);
4204 }
4205
4206 case TYPE_RVALUE_REFERENCE: {
4207 if (Record.size() != 1) {
4208 Error("Incorrect encoding of rvalue reference type");
4209 return QualType();
4210 }
4211 QualType PointeeType = readType(*Loc.F, Record, Idx);
4212 return Context.getRValueReferenceType(PointeeType);
4213 }
4214
4215 case TYPE_MEMBER_POINTER: {
4216 if (Record.size() != 2) {
4217 Error("Incorrect encoding of member pointer type");
4218 return QualType();
4219 }
4220 QualType PointeeType = readType(*Loc.F, Record, Idx);
4221 QualType ClassType = readType(*Loc.F, Record, Idx);
4222 if (PointeeType.isNull() || ClassType.isNull())
4223 return QualType();
4224
4225 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
4226 }
4227
4228 case TYPE_CONSTANT_ARRAY: {
4229 QualType ElementType = readType(*Loc.F, Record, Idx);
4230 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4231 unsigned IndexTypeQuals = Record[2];
4232 unsigned Idx = 3;
4233 llvm::APInt Size = ReadAPInt(Record, Idx);
4234 return Context.getConstantArrayType(ElementType, Size,
4235 ASM, IndexTypeQuals);
4236 }
4237
4238 case TYPE_INCOMPLETE_ARRAY: {
4239 QualType ElementType = readType(*Loc.F, Record, Idx);
4240 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4241 unsigned IndexTypeQuals = Record[2];
4242 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
4243 }
4244
4245 case TYPE_VARIABLE_ARRAY: {
4246 QualType ElementType = readType(*Loc.F, Record, Idx);
4247 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4248 unsigned IndexTypeQuals = Record[2];
4249 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
4250 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
4251 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
4252 ASM, IndexTypeQuals,
4253 SourceRange(LBLoc, RBLoc));
4254 }
4255
4256 case TYPE_VECTOR: {
4257 if (Record.size() != 3) {
4258 Error("incorrect encoding of vector type in AST file");
4259 return QualType();
4260 }
4261
4262 QualType ElementType = readType(*Loc.F, Record, Idx);
4263 unsigned NumElements = Record[1];
4264 unsigned VecKind = Record[2];
4265 return Context.getVectorType(ElementType, NumElements,
4266 (VectorType::VectorKind)VecKind);
4267 }
4268
4269 case TYPE_EXT_VECTOR: {
4270 if (Record.size() != 3) {
4271 Error("incorrect encoding of extended vector type in AST file");
4272 return QualType();
4273 }
4274
4275 QualType ElementType = readType(*Loc.F, Record, Idx);
4276 unsigned NumElements = Record[1];
4277 return Context.getExtVectorType(ElementType, NumElements);
4278 }
4279
4280 case TYPE_FUNCTION_NO_PROTO: {
4281 if (Record.size() != 6) {
4282 Error("incorrect encoding of no-proto function type");
4283 return QualType();
4284 }
4285 QualType ResultType = readType(*Loc.F, Record, Idx);
4286 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
4287 (CallingConv)Record[4], Record[5]);
4288 return Context.getFunctionNoProtoType(ResultType, Info);
4289 }
4290
4291 case TYPE_FUNCTION_PROTO: {
4292 QualType ResultType = readType(*Loc.F, Record, Idx);
4293
4294 FunctionProtoType::ExtProtoInfo EPI;
4295 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
4296 /*hasregparm*/ Record[2],
4297 /*regparm*/ Record[3],
4298 static_cast<CallingConv>(Record[4]),
4299 /*produces*/ Record[5]);
4300
4301 unsigned Idx = 6;
4302 unsigned NumParams = Record[Idx++];
4303 SmallVector<QualType, 16> ParamTypes;
4304 for (unsigned I = 0; I != NumParams; ++I)
4305 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
4306
4307 EPI.Variadic = Record[Idx++];
4308 EPI.HasTrailingReturn = Record[Idx++];
4309 EPI.TypeQuals = Record[Idx++];
4310 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
4311 ExceptionSpecificationType EST =
4312 static_cast<ExceptionSpecificationType>(Record[Idx++]);
4313 EPI.ExceptionSpecType = EST;
4314 SmallVector<QualType, 2> Exceptions;
4315 if (EST == EST_Dynamic) {
4316 EPI.NumExceptions = Record[Idx++];
4317 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
4318 Exceptions.push_back(readType(*Loc.F, Record, Idx));
4319 EPI.Exceptions = Exceptions.data();
4320 } else if (EST == EST_ComputedNoexcept) {
4321 EPI.NoexceptExpr = ReadExpr(*Loc.F);
4322 } else if (EST == EST_Uninstantiated) {
4323 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
4324 EPI.ExceptionSpecTemplate = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
4325 } else if (EST == EST_Unevaluated) {
4326 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
4327 }
4328 return Context.getFunctionType(ResultType, ParamTypes.data(), NumParams,
4329 EPI);
4330 }
4331
4332 case TYPE_UNRESOLVED_USING: {
4333 unsigned Idx = 0;
4334 return Context.getTypeDeclType(
4335 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
4336 }
4337
4338 case TYPE_TYPEDEF: {
4339 if (Record.size() != 2) {
4340 Error("incorrect encoding of typedef type");
4341 return QualType();
4342 }
4343 unsigned Idx = 0;
4344 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
4345 QualType Canonical = readType(*Loc.F, Record, Idx);
4346 if (!Canonical.isNull())
4347 Canonical = Context.getCanonicalType(Canonical);
4348 return Context.getTypedefType(Decl, Canonical);
4349 }
4350
4351 case TYPE_TYPEOF_EXPR:
4352 return Context.getTypeOfExprType(ReadExpr(*Loc.F));
4353
4354 case TYPE_TYPEOF: {
4355 if (Record.size() != 1) {
4356 Error("incorrect encoding of typeof(type) in AST file");
4357 return QualType();
4358 }
4359 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
4360 return Context.getTypeOfType(UnderlyingType);
4361 }
4362
4363 case TYPE_DECLTYPE: {
4364 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
4365 return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
4366 }
4367
4368 case TYPE_UNARY_TRANSFORM: {
4369 QualType BaseType = readType(*Loc.F, Record, Idx);
4370 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
4371 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
4372 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
4373 }
4374
4375 case TYPE_AUTO:
4376 return Context.getAutoType(readType(*Loc.F, Record, Idx));
4377
4378 case TYPE_RECORD: {
4379 if (Record.size() != 2) {
4380 Error("incorrect encoding of record type");
4381 return QualType();
4382 }
4383 unsigned Idx = 0;
4384 bool IsDependent = Record[Idx++];
4385 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
4386 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
4387 QualType T = Context.getRecordType(RD);
4388 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
4389 return T;
4390 }
4391
4392 case TYPE_ENUM: {
4393 if (Record.size() != 2) {
4394 Error("incorrect encoding of enum type");
4395 return QualType();
4396 }
4397 unsigned Idx = 0;
4398 bool IsDependent = Record[Idx++];
4399 QualType T
4400 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
4401 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
4402 return T;
4403 }
4404
4405 case TYPE_ATTRIBUTED: {
4406 if (Record.size() != 3) {
4407 Error("incorrect encoding of attributed type");
4408 return QualType();
4409 }
4410 QualType modifiedType = readType(*Loc.F, Record, Idx);
4411 QualType equivalentType = readType(*Loc.F, Record, Idx);
4412 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
4413 return Context.getAttributedType(kind, modifiedType, equivalentType);
4414 }
4415
4416 case TYPE_PAREN: {
4417 if (Record.size() != 1) {
4418 Error("incorrect encoding of paren type");
4419 return QualType();
4420 }
4421 QualType InnerType = readType(*Loc.F, Record, Idx);
4422 return Context.getParenType(InnerType);
4423 }
4424
4425 case TYPE_PACK_EXPANSION: {
4426 if (Record.size() != 2) {
4427 Error("incorrect encoding of pack expansion type");
4428 return QualType();
4429 }
4430 QualType Pattern = readType(*Loc.F, Record, Idx);
4431 if (Pattern.isNull())
4432 return QualType();
4433 llvm::Optional<unsigned> NumExpansions;
4434 if (Record[1])
4435 NumExpansions = Record[1] - 1;
4436 return Context.getPackExpansionType(Pattern, NumExpansions);
4437 }
4438
4439 case TYPE_ELABORATED: {
4440 unsigned Idx = 0;
4441 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
4442 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
4443 QualType NamedType = readType(*Loc.F, Record, Idx);
4444 return Context.getElaboratedType(Keyword, NNS, NamedType);
4445 }
4446
4447 case TYPE_OBJC_INTERFACE: {
4448 unsigned Idx = 0;
4449 ObjCInterfaceDecl *ItfD
4450 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
4451 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
4452 }
4453
4454 case TYPE_OBJC_OBJECT: {
4455 unsigned Idx = 0;
4456 QualType Base = readType(*Loc.F, Record, Idx);
4457 unsigned NumProtos = Record[Idx++];
4458 SmallVector<ObjCProtocolDecl*, 4> Protos;
4459 for (unsigned I = 0; I != NumProtos; ++I)
4460 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
4461 return Context.getObjCObjectType(Base, Protos.data(), NumProtos);
4462 }
4463
4464 case TYPE_OBJC_OBJECT_POINTER: {
4465 unsigned Idx = 0;
4466 QualType Pointee = readType(*Loc.F, Record, Idx);
4467 return Context.getObjCObjectPointerType(Pointee);
4468 }
4469
4470 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
4471 unsigned Idx = 0;
4472 QualType Parm = readType(*Loc.F, Record, Idx);
4473 QualType Replacement = readType(*Loc.F, Record, Idx);
4474 return
4475 Context.getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
4476 Replacement);
4477 }
4478
4479 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
4480 unsigned Idx = 0;
4481 QualType Parm = readType(*Loc.F, Record, Idx);
4482 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
4483 return Context.getSubstTemplateTypeParmPackType(
4484 cast<TemplateTypeParmType>(Parm),
4485 ArgPack);
4486 }
4487
4488 case TYPE_INJECTED_CLASS_NAME: {
4489 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
4490 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
4491 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
4492 // for AST reading, too much interdependencies.
4493 return
4494 QualType(new (Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
4495 }
4496
4497 case TYPE_TEMPLATE_TYPE_PARM: {
4498 unsigned Idx = 0;
4499 unsigned Depth = Record[Idx++];
4500 unsigned Index = Record[Idx++];
4501 bool Pack = Record[Idx++];
4502 TemplateTypeParmDecl *D
4503 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
4504 return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
4505 }
4506
4507 case TYPE_DEPENDENT_NAME: {
4508 unsigned Idx = 0;
4509 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
4510 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
4511 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
4512 QualType Canon = readType(*Loc.F, Record, Idx);
4513 if (!Canon.isNull())
4514 Canon = Context.getCanonicalType(Canon);
4515 return Context.getDependentNameType(Keyword, NNS, Name, Canon);
4516 }
4517
4518 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
4519 unsigned Idx = 0;
4520 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
4521 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
4522 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
4523 unsigned NumArgs = Record[Idx++];
4524 SmallVector<TemplateArgument, 8> Args;
4525 Args.reserve(NumArgs);
4526 while (NumArgs--)
4527 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
4528 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
4529 Args.size(), Args.data());
4530 }
4531
4532 case TYPE_DEPENDENT_SIZED_ARRAY: {
4533 unsigned Idx = 0;
4534
4535 // ArrayType
4536 QualType ElementType = readType(*Loc.F, Record, Idx);
4537 ArrayType::ArraySizeModifier ASM
4538 = (ArrayType::ArraySizeModifier)Record[Idx++];
4539 unsigned IndexTypeQuals = Record[Idx++];
4540
4541 // DependentSizedArrayType
4542 Expr *NumElts = ReadExpr(*Loc.F);
4543 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
4544
4545 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
4546 IndexTypeQuals, Brackets);
4547 }
4548
4549 case TYPE_TEMPLATE_SPECIALIZATION: {
4550 unsigned Idx = 0;
4551 bool IsDependent = Record[Idx++];
4552 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
4553 SmallVector<TemplateArgument, 8> Args;
4554 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
4555 QualType Underlying = readType(*Loc.F, Record, Idx);
4556 QualType T;
4557 if (Underlying.isNull())
4558 T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(),
4559 Args.size());
4560 else
4561 T = Context.getTemplateSpecializationType(Name, Args.data(),
4562 Args.size(), Underlying);
4563 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
4564 return T;
4565 }
4566
4567 case TYPE_ATOMIC: {
4568 if (Record.size() != 1) {
4569 Error("Incorrect encoding of atomic type");
4570 return QualType();
4571 }
4572 QualType ValueType = readType(*Loc.F, Record, Idx);
4573 return Context.getAtomicType(ValueType);
4574 }
4575 }
4576 llvm_unreachable("Invalid TypeCode!");
4577}
4578
4579class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
4580 ASTReader &Reader;
4581 ModuleFile &F;
4582 const ASTReader::RecordData &Record;
4583 unsigned &Idx;
4584
4585 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
4586 unsigned &I) {
4587 return Reader.ReadSourceLocation(F, R, I);
4588 }
4589
4590 template<typename T>
4591 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
4592 return Reader.ReadDeclAs<T>(F, Record, Idx);
4593 }
4594
4595public:
4596 TypeLocReader(ASTReader &Reader, ModuleFile &F,
4597 const ASTReader::RecordData &Record, unsigned &Idx)
4598 : Reader(Reader), F(F), Record(Record), Idx(Idx)
4599 { }
4600
4601 // We want compile-time assurance that we've enumerated all of
4602 // these, so unfortunately we have to declare them first, then
4603 // define them out-of-line.
4604#define ABSTRACT_TYPELOC(CLASS, PARENT)
4605#define TYPELOC(CLASS, PARENT) \
4606 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
4607#include "clang/AST/TypeLocNodes.def"
4608
4609 void VisitFunctionTypeLoc(FunctionTypeLoc);
4610 void VisitArrayTypeLoc(ArrayTypeLoc);
4611};
4612
4613void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
4614 // nothing to do
4615}
4616void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
4617 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
4618 if (TL.needsExtraLocalData()) {
4619 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
4620 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
4621 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
4622 TL.setModeAttr(Record[Idx++]);
4623 }
4624}
4625void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
4626 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4627}
4628void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
4629 TL.setStarLoc(ReadSourceLocation(Record, Idx));
4630}
4631void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
4632 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
4633}
4634void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
4635 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
4636}
4637void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
4638 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
4639}
4640void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
4641 TL.setStarLoc(ReadSourceLocation(Record, Idx));
4642 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
4643}
4644void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
4645 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
4646 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
4647 if (Record[Idx++])
4648 TL.setSizeExpr(Reader.ReadExpr(F));
4649 else
4650 TL.setSizeExpr(0);
4651}
4652void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
4653 VisitArrayTypeLoc(TL);
4654}
4655void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
4656 VisitArrayTypeLoc(TL);
4657}
4658void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
4659 VisitArrayTypeLoc(TL);
4660}
4661void TypeLocReader::VisitDependentSizedArrayTypeLoc(
4662 DependentSizedArrayTypeLoc TL) {
4663 VisitArrayTypeLoc(TL);
4664}
4665void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
4666 DependentSizedExtVectorTypeLoc TL) {
4667 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4668}
4669void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
4670 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4671}
4672void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
4673 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4674}
4675void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
4676 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
4677 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4678 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4679 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
4680 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
4681 TL.setArg(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
4682 }
4683}
4684void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
4685 VisitFunctionTypeLoc(TL);
4686}
4687void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
4688 VisitFunctionTypeLoc(TL);
4689}
4690void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
4691 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4692}
4693void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
4694 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4695}
4696void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
4697 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
4698 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4699 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4700}
4701void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
4702 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
4703 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4704 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4705 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
4706}
4707void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
4708 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4709}
4710void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
4711 TL.setKWLoc(ReadSourceLocation(Record, Idx));
4712 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4713 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4714 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
4715}
4716void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
4717 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4718}
4719void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
4720 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4721}
4722void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
4723 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4724}
4725void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
4726 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
4727 if (TL.hasAttrOperand()) {
4728 SourceRange range;
4729 range.setBegin(ReadSourceLocation(Record, Idx));
4730 range.setEnd(ReadSourceLocation(Record, Idx));
4731 TL.setAttrOperandParensRange(range);
4732 }
4733 if (TL.hasAttrExprOperand()) {
4734 if (Record[Idx++])
4735 TL.setAttrExprOperand(Reader.ReadExpr(F));
4736 else
4737 TL.setAttrExprOperand(0);
4738 } else if (TL.hasAttrEnumOperand())
4739 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
4740}
4741void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
4742 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4743}
4744void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
4745 SubstTemplateTypeParmTypeLoc TL) {
4746 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4747}
4748void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
4749 SubstTemplateTypeParmPackTypeLoc TL) {
4750 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4751}
4752void TypeLocReader::VisitTemplateSpecializationTypeLoc(
4753 TemplateSpecializationTypeLoc TL) {
4754 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
4755 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
4756 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4757 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
4758 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
4759 TL.setArgLocInfo(i,
4760 Reader.GetTemplateArgumentLocInfo(F,
4761 TL.getTypePtr()->getArg(i).getKind(),
4762 Record, Idx));
4763}
4764void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
4765 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4766 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4767}
4768void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
4769 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
4770 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
4771}
4772void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
4773 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4774}
4775void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
4776 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
4777 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
4778 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4779}
4780void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
4781 DependentTemplateSpecializationTypeLoc TL) {
4782 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
4783 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
4784 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
4785 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
4786 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4787 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
4788 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
4789 TL.setArgLocInfo(I,
4790 Reader.GetTemplateArgumentLocInfo(F,
4791 TL.getTypePtr()->getArg(I).getKind(),
4792 Record, Idx));
4793}
4794void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
4795 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
4796}
4797void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
4798 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4799}
4800void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
4801 TL.setHasBaseTypeAsWritten(Record[Idx++]);
4802 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4803 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
4804 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
4805 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
4806}
4807void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
4808 TL.setStarLoc(ReadSourceLocation(Record, Idx));
4809}
4810void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
4811 TL.setKWLoc(ReadSourceLocation(Record, Idx));
4812 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4813 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4814}
4815
4816TypeSourceInfo *ASTReader::GetTypeSourceInfo(ModuleFile &F,
4817 const RecordData &Record,
4818 unsigned &Idx) {
4819 QualType InfoTy = readType(F, Record, Idx);
4820 if (InfoTy.isNull())
4821 return 0;
4822
4823 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
4824 TypeLocReader TLR(*this, F, Record, Idx);
4825 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
4826 TLR.Visit(TL);
4827 return TInfo;
4828}
4829
4830QualType ASTReader::GetType(TypeID ID) {
4831 unsigned FastQuals = ID & Qualifiers::FastMask;
4832 unsigned Index = ID >> Qualifiers::FastWidth;
4833
4834 if (Index < NUM_PREDEF_TYPE_IDS) {
4835 QualType T;
4836 switch ((PredefinedTypeIDs)Index) {
4837 case PREDEF_TYPE_NULL_ID: return QualType();
4838 case PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
4839 case PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
4840
4841 case PREDEF_TYPE_CHAR_U_ID:
4842 case PREDEF_TYPE_CHAR_S_ID:
4843 // FIXME: Check that the signedness of CharTy is correct!
4844 T = Context.CharTy;
4845 break;
4846
4847 case PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
4848 case PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
4849 case PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
4850 case PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
4851 case PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
4852 case PREDEF_TYPE_UINT128_ID: T = Context.UnsignedInt128Ty; break;
4853 case PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
4854 case PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
4855 case PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
4856 case PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
4857 case PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
4858 case PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
4859 case PREDEF_TYPE_INT128_ID: T = Context.Int128Ty; break;
4860 case PREDEF_TYPE_HALF_ID: T = Context.HalfTy; break;
4861 case PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
4862 case PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
4863 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
4864 case PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
4865 case PREDEF_TYPE_BOUND_MEMBER: T = Context.BoundMemberTy; break;
4866 case PREDEF_TYPE_PSEUDO_OBJECT: T = Context.PseudoObjectTy; break;
4867 case PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
4868 case PREDEF_TYPE_UNKNOWN_ANY: T = Context.UnknownAnyTy; break;
4869 case PREDEF_TYPE_NULLPTR_ID: T = Context.NullPtrTy; break;
4870 case PREDEF_TYPE_CHAR16_ID: T = Context.Char16Ty; break;
4871 case PREDEF_TYPE_CHAR32_ID: T = Context.Char32Ty; break;
4872 case PREDEF_TYPE_OBJC_ID: T = Context.ObjCBuiltinIdTy; break;
4873 case PREDEF_TYPE_OBJC_CLASS: T = Context.ObjCBuiltinClassTy; break;
4874 case PREDEF_TYPE_OBJC_SEL: T = Context.ObjCBuiltinSelTy; break;
Guy Benyeib13621d2012-12-18 14:38:23 +00004875 case PREDEF_TYPE_IMAGE1D_ID: T = Context.OCLImage1dTy; break;
4876 case PREDEF_TYPE_IMAGE1D_ARR_ID: T = Context.OCLImage1dArrayTy; break;
4877 case PREDEF_TYPE_IMAGE1D_BUFF_ID: T = Context.OCLImage1dBufferTy; break;
4878 case PREDEF_TYPE_IMAGE2D_ID: T = Context.OCLImage2dTy; break;
4879 case PREDEF_TYPE_IMAGE2D_ARR_ID: T = Context.OCLImage2dArrayTy; break;
4880 case PREDEF_TYPE_IMAGE3D_ID: T = Context.OCLImage3dTy; break;
Guy Benyeie6b9d802013-01-20 12:31:11 +00004881 case PREDEF_TYPE_EVENT_ID: T = Context.OCLEventTy; break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004882 case PREDEF_TYPE_AUTO_DEDUCT: T = Context.getAutoDeductType(); break;
4883
4884 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
4885 T = Context.getAutoRRefDeductType();
4886 break;
4887
4888 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
4889 T = Context.ARCUnbridgedCastTy;
4890 break;
4891
4892 case PREDEF_TYPE_VA_LIST_TAG:
4893 T = Context.getVaListTagType();
4894 break;
4895
4896 case PREDEF_TYPE_BUILTIN_FN:
4897 T = Context.BuiltinFnTy;
4898 break;
4899 }
4900
4901 assert(!T.isNull() && "Unknown predefined type");
4902 return T.withFastQualifiers(FastQuals);
4903 }
4904
4905 Index -= NUM_PREDEF_TYPE_IDS;
4906 assert(Index < TypesLoaded.size() && "Type index out-of-range");
4907 if (TypesLoaded[Index].isNull()) {
4908 TypesLoaded[Index] = readTypeRecord(Index);
4909 if (TypesLoaded[Index].isNull())
4910 return QualType();
4911
4912 TypesLoaded[Index]->setFromAST();
4913 if (DeserializationListener)
4914 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
4915 TypesLoaded[Index]);
4916 }
4917
4918 return TypesLoaded[Index].withFastQualifiers(FastQuals);
4919}
4920
4921QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
4922 return GetType(getGlobalTypeID(F, LocalID));
4923}
4924
4925serialization::TypeID
4926ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
4927 unsigned FastQuals = LocalID & Qualifiers::FastMask;
4928 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
4929
4930 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
4931 return LocalID;
4932
4933 ContinuousRangeMap<uint32_t, int, 2>::iterator I
4934 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
4935 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
4936
4937 unsigned GlobalIndex = LocalIndex + I->second;
4938 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
4939}
4940
4941TemplateArgumentLocInfo
4942ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
4943 TemplateArgument::ArgKind Kind,
4944 const RecordData &Record,
4945 unsigned &Index) {
4946 switch (Kind) {
4947 case TemplateArgument::Expression:
4948 return ReadExpr(F);
4949 case TemplateArgument::Type:
4950 return GetTypeSourceInfo(F, Record, Index);
4951 case TemplateArgument::Template: {
4952 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
4953 Index);
4954 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
4955 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
4956 SourceLocation());
4957 }
4958 case TemplateArgument::TemplateExpansion: {
4959 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
4960 Index);
4961 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
4962 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
4963 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
4964 EllipsisLoc);
4965 }
4966 case TemplateArgument::Null:
4967 case TemplateArgument::Integral:
4968 case TemplateArgument::Declaration:
4969 case TemplateArgument::NullPtr:
4970 case TemplateArgument::Pack:
4971 // FIXME: Is this right?
4972 return TemplateArgumentLocInfo();
4973 }
4974 llvm_unreachable("unexpected template argument loc");
4975}
4976
4977TemplateArgumentLoc
4978ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
4979 const RecordData &Record, unsigned &Index) {
4980 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
4981
4982 if (Arg.getKind() == TemplateArgument::Expression) {
4983 if (Record[Index++]) // bool InfoHasSameExpr.
4984 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
4985 }
4986 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
4987 Record, Index));
4988}
4989
4990Decl *ASTReader::GetExternalDecl(uint32_t ID) {
4991 return GetDecl(ID);
4992}
4993
4994uint64_t ASTReader::readCXXBaseSpecifiers(ModuleFile &M, const RecordData &Record,
4995 unsigned &Idx){
4996 if (Idx >= Record.size())
4997 return 0;
4998
4999 unsigned LocalID = Record[Idx++];
5000 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
5001}
5002
5003CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
5004 RecordLocation Loc = getLocalBitOffset(Offset);
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00005005 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005006 SavedStreamPosition SavedPosition(Cursor);
5007 Cursor.JumpToBit(Loc.Offset);
5008 ReadingKindTracker ReadingKind(Read_Decl, *this);
5009 RecordData Record;
5010 unsigned Code = Cursor.ReadCode();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00005011 unsigned RecCode = Cursor.readRecord(Code, Record);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005012 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
5013 Error("Malformed AST file: missing C++ base specifiers");
5014 return 0;
5015 }
5016
5017 unsigned Idx = 0;
5018 unsigned NumBases = Record[Idx++];
5019 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
5020 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
5021 for (unsigned I = 0; I != NumBases; ++I)
5022 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
5023 return Bases;
5024}
5025
5026serialization::DeclID
5027ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
5028 if (LocalID < NUM_PREDEF_DECL_IDS)
5029 return LocalID;
5030
5031 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5032 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
5033 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
5034
5035 return LocalID + I->second;
5036}
5037
5038bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
5039 ModuleFile &M) const {
5040 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(ID);
5041 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5042 return &M == I->second;
5043}
5044
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00005045ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005046 if (!D->isFromASTFile())
5047 return 0;
5048 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
5049 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5050 return I->second;
5051}
5052
5053SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
5054 if (ID < NUM_PREDEF_DECL_IDS)
5055 return SourceLocation();
5056
5057 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
5058
5059 if (Index > DeclsLoaded.size()) {
5060 Error("declaration ID out-of-range for AST file");
5061 return SourceLocation();
5062 }
5063
5064 if (Decl *D = DeclsLoaded[Index])
5065 return D->getLocation();
5066
5067 unsigned RawLocation = 0;
5068 RecordLocation Rec = DeclCursorForID(ID, RawLocation);
5069 return ReadSourceLocation(*Rec.F, RawLocation);
5070}
5071
5072Decl *ASTReader::GetDecl(DeclID ID) {
5073 if (ID < NUM_PREDEF_DECL_IDS) {
5074 switch ((PredefinedDeclIDs)ID) {
5075 case PREDEF_DECL_NULL_ID:
5076 return 0;
5077
5078 case PREDEF_DECL_TRANSLATION_UNIT_ID:
5079 return Context.getTranslationUnitDecl();
5080
5081 case PREDEF_DECL_OBJC_ID_ID:
5082 return Context.getObjCIdDecl();
5083
5084 case PREDEF_DECL_OBJC_SEL_ID:
5085 return Context.getObjCSelDecl();
5086
5087 case PREDEF_DECL_OBJC_CLASS_ID:
5088 return Context.getObjCClassDecl();
5089
5090 case PREDEF_DECL_OBJC_PROTOCOL_ID:
5091 return Context.getObjCProtocolDecl();
5092
5093 case PREDEF_DECL_INT_128_ID:
5094 return Context.getInt128Decl();
5095
5096 case PREDEF_DECL_UNSIGNED_INT_128_ID:
5097 return Context.getUInt128Decl();
5098
5099 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
5100 return Context.getObjCInstanceTypeDecl();
5101
5102 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
5103 return Context.getBuiltinVaListDecl();
5104 }
5105 }
5106
5107 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
5108
5109 if (Index >= DeclsLoaded.size()) {
5110 assert(0 && "declaration ID out-of-range for AST file");
5111 Error("declaration ID out-of-range for AST file");
5112 return 0;
5113 }
5114
5115 if (!DeclsLoaded[Index]) {
5116 ReadDeclRecord(ID);
5117 if (DeserializationListener)
5118 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
5119 }
5120
5121 return DeclsLoaded[Index];
5122}
5123
5124DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
5125 DeclID GlobalID) {
5126 if (GlobalID < NUM_PREDEF_DECL_IDS)
5127 return GlobalID;
5128
5129 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
5130 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5131 ModuleFile *Owner = I->second;
5132
5133 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
5134 = M.GlobalToLocalDeclIDs.find(Owner);
5135 if (Pos == M.GlobalToLocalDeclIDs.end())
5136 return 0;
5137
5138 return GlobalID - Owner->BaseDeclID + Pos->second;
5139}
5140
5141serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
5142 const RecordData &Record,
5143 unsigned &Idx) {
5144 if (Idx >= Record.size()) {
5145 Error("Corrupted AST file");
5146 return 0;
5147 }
5148
5149 return getGlobalDeclID(F, Record[Idx++]);
5150}
5151
5152/// \brief Resolve the offset of a statement into a statement.
5153///
5154/// This operation will read a new statement from the external
5155/// source each time it is called, and is meant to be used via a
5156/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
5157Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
5158 // Switch case IDs are per Decl.
5159 ClearSwitchCaseIDs();
5160
5161 // Offset here is a global offset across the entire chain.
5162 RecordLocation Loc = getLocalBitOffset(Offset);
5163 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
5164 return ReadStmtFromStream(*Loc.F);
5165}
5166
5167namespace {
5168 class FindExternalLexicalDeclsVisitor {
5169 ASTReader &Reader;
5170 const DeclContext *DC;
5171 bool (*isKindWeWant)(Decl::Kind);
5172
5173 SmallVectorImpl<Decl*> &Decls;
5174 bool PredefsVisited[NUM_PREDEF_DECL_IDS];
5175
5176 public:
5177 FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC,
5178 bool (*isKindWeWant)(Decl::Kind),
5179 SmallVectorImpl<Decl*> &Decls)
5180 : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls)
5181 {
5182 for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I)
5183 PredefsVisited[I] = false;
5184 }
5185
5186 static bool visit(ModuleFile &M, bool Preorder, void *UserData) {
5187 if (Preorder)
5188 return false;
5189
5190 FindExternalLexicalDeclsVisitor *This
5191 = static_cast<FindExternalLexicalDeclsVisitor *>(UserData);
5192
5193 ModuleFile::DeclContextInfosMap::iterator Info
5194 = M.DeclContextInfos.find(This->DC);
5195 if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls)
5196 return false;
5197
5198 // Load all of the declaration IDs
5199 for (const KindDeclIDPair *ID = Info->second.LexicalDecls,
5200 *IDE = ID + Info->second.NumLexicalDecls;
5201 ID != IDE; ++ID) {
5202 if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first))
5203 continue;
5204
5205 // Don't add predefined declarations to the lexical context more
5206 // than once.
5207 if (ID->second < NUM_PREDEF_DECL_IDS) {
5208 if (This->PredefsVisited[ID->second])
5209 continue;
5210
5211 This->PredefsVisited[ID->second] = true;
5212 }
5213
5214 if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) {
5215 if (!This->DC->isDeclInLexicalTraversal(D))
5216 This->Decls.push_back(D);
5217 }
5218 }
5219
5220 return false;
5221 }
5222 };
5223}
5224
5225ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
5226 bool (*isKindWeWant)(Decl::Kind),
5227 SmallVectorImpl<Decl*> &Decls) {
5228 // There might be lexical decls in multiple modules, for the TU at
5229 // least. Walk all of the modules in the order they were loaded.
5230 FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls);
5231 ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor);
5232 ++NumLexicalDeclContextsRead;
5233 return ELR_Success;
5234}
5235
5236namespace {
5237
5238class DeclIDComp {
5239 ASTReader &Reader;
5240 ModuleFile &Mod;
5241
5242public:
5243 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
5244
5245 bool operator()(LocalDeclID L, LocalDeclID R) const {
5246 SourceLocation LHS = getLocation(L);
5247 SourceLocation RHS = getLocation(R);
5248 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5249 }
5250
5251 bool operator()(SourceLocation LHS, LocalDeclID R) const {
5252 SourceLocation RHS = getLocation(R);
5253 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5254 }
5255
5256 bool operator()(LocalDeclID L, SourceLocation RHS) const {
5257 SourceLocation LHS = getLocation(L);
5258 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5259 }
5260
5261 SourceLocation getLocation(LocalDeclID ID) const {
5262 return Reader.getSourceManager().getFileLoc(
5263 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
5264 }
5265};
5266
5267}
5268
5269void ASTReader::FindFileRegionDecls(FileID File,
5270 unsigned Offset, unsigned Length,
5271 SmallVectorImpl<Decl *> &Decls) {
5272 SourceManager &SM = getSourceManager();
5273
5274 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
5275 if (I == FileDeclIDs.end())
5276 return;
5277
5278 FileDeclsInfo &DInfo = I->second;
5279 if (DInfo.Decls.empty())
5280 return;
5281
5282 SourceLocation
5283 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
5284 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
5285
5286 DeclIDComp DIDComp(*this, *DInfo.Mod);
5287 ArrayRef<serialization::LocalDeclID>::iterator
5288 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
5289 BeginLoc, DIDComp);
5290 if (BeginIt != DInfo.Decls.begin())
5291 --BeginIt;
5292
5293 // If we are pointing at a top-level decl inside an objc container, we need
5294 // to backtrack until we find it otherwise we will fail to report that the
5295 // region overlaps with an objc container.
5296 while (BeginIt != DInfo.Decls.begin() &&
5297 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
5298 ->isTopLevelDeclInObjCContainer())
5299 --BeginIt;
5300
5301 ArrayRef<serialization::LocalDeclID>::iterator
5302 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
5303 EndLoc, DIDComp);
5304 if (EndIt != DInfo.Decls.end())
5305 ++EndIt;
5306
5307 for (ArrayRef<serialization::LocalDeclID>::iterator
5308 DIt = BeginIt; DIt != EndIt; ++DIt)
5309 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
5310}
5311
5312namespace {
5313 /// \brief ModuleFile visitor used to perform name lookup into a
5314 /// declaration context.
5315 class DeclContextNameLookupVisitor {
5316 ASTReader &Reader;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00005317 SmallVectorImpl<const DeclContext *> &Contexts;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005318 DeclarationName Name;
5319 SmallVectorImpl<NamedDecl *> &Decls;
5320
5321 public:
5322 DeclContextNameLookupVisitor(ASTReader &Reader,
5323 SmallVectorImpl<const DeclContext *> &Contexts,
5324 DeclarationName Name,
5325 SmallVectorImpl<NamedDecl *> &Decls)
5326 : Reader(Reader), Contexts(Contexts), Name(Name), Decls(Decls) { }
5327
5328 static bool visit(ModuleFile &M, void *UserData) {
5329 DeclContextNameLookupVisitor *This
5330 = static_cast<DeclContextNameLookupVisitor *>(UserData);
5331
5332 // Check whether we have any visible declaration information for
5333 // this context in this module.
5334 ModuleFile::DeclContextInfosMap::iterator Info;
5335 bool FoundInfo = false;
5336 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5337 Info = M.DeclContextInfos.find(This->Contexts[I]);
5338 if (Info != M.DeclContextInfos.end() &&
5339 Info->second.NameLookupTableData) {
5340 FoundInfo = true;
5341 break;
5342 }
5343 }
5344
5345 if (!FoundInfo)
5346 return false;
5347
5348 // Look for this name within this module.
5349 ASTDeclContextNameLookupTable *LookupTable =
5350 Info->second.NameLookupTableData;
5351 ASTDeclContextNameLookupTable::iterator Pos
5352 = LookupTable->find(This->Name);
5353 if (Pos == LookupTable->end())
5354 return false;
5355
5356 bool FoundAnything = false;
5357 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
5358 for (; Data.first != Data.second; ++Data.first) {
5359 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first);
5360 if (!ND)
5361 continue;
5362
5363 if (ND->getDeclName() != This->Name) {
5364 // A name might be null because the decl's redeclarable part is
5365 // currently read before reading its name. The lookup is triggered by
5366 // building that decl (likely indirectly), and so it is later in the
5367 // sense of "already existing" and can be ignored here.
5368 continue;
5369 }
5370
5371 // Record this declaration.
5372 FoundAnything = true;
5373 This->Decls.push_back(ND);
5374 }
5375
5376 return FoundAnything;
5377 }
5378 };
5379}
5380
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00005381/// \brief Retrieve the "definitive" module file for the definition of the
5382/// given declaration context, if there is one.
5383///
5384/// The "definitive" module file is the only place where we need to look to
5385/// find information about the declarations within the given declaration
5386/// context. For example, C++ and Objective-C classes, C structs/unions, and
5387/// Objective-C protocols, categories, and extensions are all defined in a
5388/// single place in the source code, so they have definitive module files
5389/// associated with them. C++ namespaces, on the other hand, can have
5390/// definitions in multiple different module files.
5391///
5392/// Note: this needs to be kept in sync with ASTWriter::AddedVisibleDecl's
5393/// NDEBUG checking.
5394static ModuleFile *getDefinitiveModuleFileFor(const DeclContext *DC,
5395 ASTReader &Reader) {
Douglas Gregore0d20662013-01-22 17:08:30 +00005396 if (const DeclContext *DefDC = getDefinitiveDeclContext(DC))
5397 return Reader.getOwningModuleFile(cast<Decl>(DefDC));
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00005398
5399 return 0;
5400}
5401
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005402DeclContext::lookup_result
5403ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
5404 DeclarationName Name) {
5405 assert(DC->hasExternalVisibleStorage() &&
5406 "DeclContext has no visible decls in storage");
5407 if (!Name)
5408 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
5409 DeclContext::lookup_iterator(0));
5410
5411 SmallVector<NamedDecl *, 64> Decls;
5412
5413 // Compute the declaration contexts we need to look into. Multiple such
5414 // declaration contexts occur when two declaration contexts from disjoint
5415 // modules get merged, e.g., when two namespaces with the same name are
5416 // independently defined in separate modules.
5417 SmallVector<const DeclContext *, 2> Contexts;
5418 Contexts.push_back(DC);
5419
5420 if (DC->isNamespace()) {
5421 MergedDeclsMap::iterator Merged
5422 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5423 if (Merged != MergedDecls.end()) {
5424 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5425 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5426 }
5427 }
5428
5429 DeclContextNameLookupVisitor Visitor(*this, Contexts, Name, Decls);
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00005430
5431 // If we can definitively determine which module file to look into,
5432 // only look there. Otherwise, look in all module files.
5433 ModuleFile *Definitive;
5434 if (Contexts.size() == 1 &&
5435 (Definitive = getDefinitiveModuleFileFor(DC, *this))) {
5436 DeclContextNameLookupVisitor::visit(*Definitive, &Visitor);
5437 } else {
5438 ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor);
5439 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005440 ++NumVisibleDeclContextsRead;
5441 SetExternalVisibleDeclsForName(DC, Name, Decls);
5442 return const_cast<DeclContext*>(DC)->lookup(Name);
5443}
5444
5445namespace {
5446 /// \brief ModuleFile visitor used to retrieve all visible names in a
5447 /// declaration context.
5448 class DeclContextAllNamesVisitor {
5449 ASTReader &Reader;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00005450 SmallVectorImpl<const DeclContext *> &Contexts;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005451 llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> > &Decls;
Argyrios Kyrtzidisca40f302012-12-19 22:21:18 +00005452 bool VisitAll;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005453
5454 public:
5455 DeclContextAllNamesVisitor(ASTReader &Reader,
5456 SmallVectorImpl<const DeclContext *> &Contexts,
5457 llvm::DenseMap<DeclarationName,
Argyrios Kyrtzidisca40f302012-12-19 22:21:18 +00005458 SmallVector<NamedDecl *, 8> > &Decls,
5459 bool VisitAll)
5460 : Reader(Reader), Contexts(Contexts), Decls(Decls), VisitAll(VisitAll) { }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005461
5462 static bool visit(ModuleFile &M, void *UserData) {
5463 DeclContextAllNamesVisitor *This
5464 = static_cast<DeclContextAllNamesVisitor *>(UserData);
5465
5466 // Check whether we have any visible declaration information for
5467 // this context in this module.
5468 ModuleFile::DeclContextInfosMap::iterator Info;
5469 bool FoundInfo = false;
5470 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5471 Info = M.DeclContextInfos.find(This->Contexts[I]);
5472 if (Info != M.DeclContextInfos.end() &&
5473 Info->second.NameLookupTableData) {
5474 FoundInfo = true;
5475 break;
5476 }
5477 }
5478
5479 if (!FoundInfo)
5480 return false;
5481
5482 ASTDeclContextNameLookupTable *LookupTable =
5483 Info->second.NameLookupTableData;
5484 bool FoundAnything = false;
5485 for (ASTDeclContextNameLookupTable::data_iterator
Douglas Gregora6b00fc2013-01-23 22:38:11 +00005486 I = LookupTable->data_begin(), E = LookupTable->data_end();
5487 I != E;
5488 ++I) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005489 ASTDeclContextNameLookupTrait::data_type Data = *I;
5490 for (; Data.first != Data.second; ++Data.first) {
5491 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M,
5492 *Data.first);
5493 if (!ND)
5494 continue;
5495
5496 // Record this declaration.
5497 FoundAnything = true;
5498 This->Decls[ND->getDeclName()].push_back(ND);
5499 }
5500 }
5501
Argyrios Kyrtzidisca40f302012-12-19 22:21:18 +00005502 return FoundAnything && !This->VisitAll;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005503 }
5504 };
5505}
5506
5507void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
5508 if (!DC->hasExternalVisibleStorage())
5509 return;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00005510 llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> > Decls;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005511
5512 // Compute the declaration contexts we need to look into. Multiple such
5513 // declaration contexts occur when two declaration contexts from disjoint
5514 // modules get merged, e.g., when two namespaces with the same name are
5515 // independently defined in separate modules.
5516 SmallVector<const DeclContext *, 2> Contexts;
5517 Contexts.push_back(DC);
5518
5519 if (DC->isNamespace()) {
5520 MergedDeclsMap::iterator Merged
5521 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5522 if (Merged != MergedDecls.end()) {
5523 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5524 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5525 }
5526 }
5527
Argyrios Kyrtzidisca40f302012-12-19 22:21:18 +00005528 DeclContextAllNamesVisitor Visitor(*this, Contexts, Decls,
5529 /*VisitAll=*/DC->isFileContext());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005530 ModuleMgr.visit(&DeclContextAllNamesVisitor::visit, &Visitor);
5531 ++NumVisibleDeclContextsRead;
5532
5533 for (llvm::DenseMap<DeclarationName,
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00005534 SmallVector<NamedDecl *, 8> >::iterator
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005535 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
5536 SetExternalVisibleDeclsForName(DC, I->first, I->second);
5537 }
5538 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
5539}
5540
5541/// \brief Under non-PCH compilation the consumer receives the objc methods
5542/// before receiving the implementation, and codegen depends on this.
5543/// We simulate this by deserializing and passing to consumer the methods of the
5544/// implementation before passing the deserialized implementation decl.
5545static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
5546 ASTConsumer *Consumer) {
5547 assert(ImplD && Consumer);
5548
5549 for (ObjCImplDecl::method_iterator
5550 I = ImplD->meth_begin(), E = ImplD->meth_end(); I != E; ++I)
5551 Consumer->HandleInterestingDecl(DeclGroupRef(*I));
5552
5553 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
5554}
5555
5556void ASTReader::PassInterestingDeclsToConsumer() {
5557 assert(Consumer);
5558 while (!InterestingDecls.empty()) {
5559 Decl *D = InterestingDecls.front();
5560 InterestingDecls.pop_front();
5561
5562 PassInterestingDeclToConsumer(D);
5563 }
5564}
5565
5566void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
5567 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
5568 PassObjCImplDeclToConsumer(ImplD, Consumer);
5569 else
5570 Consumer->HandleInterestingDecl(DeclGroupRef(D));
5571}
5572
5573void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
5574 this->Consumer = Consumer;
5575
5576 if (!Consumer)
5577 return;
5578
5579 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
5580 // Force deserialization of this decl, which will cause it to be queued for
5581 // passing to the consumer.
5582 GetDecl(ExternalDefinitions[I]);
5583 }
5584 ExternalDefinitions.clear();
5585
5586 PassInterestingDeclsToConsumer();
5587}
5588
5589void ASTReader::PrintStats() {
5590 std::fprintf(stderr, "*** AST File Statistics:\n");
5591
5592 unsigned NumTypesLoaded
5593 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
5594 QualType());
5595 unsigned NumDeclsLoaded
5596 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
5597 (Decl *)0);
5598 unsigned NumIdentifiersLoaded
5599 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
5600 IdentifiersLoaded.end(),
5601 (IdentifierInfo *)0);
5602 unsigned NumMacrosLoaded
5603 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
5604 MacrosLoaded.end(),
5605 (MacroInfo *)0);
5606 unsigned NumSelectorsLoaded
5607 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
5608 SelectorsLoaded.end(),
5609 Selector());
5610
5611 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
5612 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
5613 NumSLocEntriesRead, TotalNumSLocEntries,
5614 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
5615 if (!TypesLoaded.empty())
5616 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
5617 NumTypesLoaded, (unsigned)TypesLoaded.size(),
5618 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
5619 if (!DeclsLoaded.empty())
5620 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
5621 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
5622 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
5623 if (!IdentifiersLoaded.empty())
5624 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
5625 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
5626 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
5627 if (!MacrosLoaded.empty())
5628 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
5629 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
5630 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
5631 if (!SelectorsLoaded.empty())
5632 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
5633 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
5634 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
5635 if (TotalNumStatements)
5636 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
5637 NumStatementsRead, TotalNumStatements,
5638 ((float)NumStatementsRead/TotalNumStatements * 100));
5639 if (TotalNumMacros)
5640 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
5641 NumMacrosRead, TotalNumMacros,
5642 ((float)NumMacrosRead/TotalNumMacros * 100));
5643 if (TotalLexicalDeclContexts)
5644 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
5645 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
5646 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
5647 * 100));
5648 if (TotalVisibleDeclContexts)
5649 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
5650 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
5651 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
5652 * 100));
5653 if (TotalNumMethodPoolEntries) {
5654 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
5655 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
5656 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
5657 * 100));
5658 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
5659 }
Douglas Gregore1698072013-01-25 00:38:33 +00005660 if (NumIdentifierLookupHits) {
5661 std::fprintf(stderr,
5662 " %u / %u identifier table lookups succeeded (%f%%)\n",
5663 NumIdentifierLookupHits, NumIdentifierLookups,
5664 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
5665 }
5666
Douglas Gregor1a49d972013-01-25 01:03:03 +00005667 if (GlobalIndex) {
5668 std::fprintf(stderr, "\n");
5669 GlobalIndex->printStats();
5670 }
5671
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005672 std::fprintf(stderr, "\n");
5673 dump();
5674 std::fprintf(stderr, "\n");
5675}
5676
5677template<typename Key, typename ModuleFile, unsigned InitialCapacity>
5678static void
5679dumpModuleIDMap(StringRef Name,
5680 const ContinuousRangeMap<Key, ModuleFile *,
5681 InitialCapacity> &Map) {
5682 if (Map.begin() == Map.end())
5683 return;
5684
5685 typedef ContinuousRangeMap<Key, ModuleFile *, InitialCapacity> MapType;
5686 llvm::errs() << Name << ":\n";
5687 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
5688 I != IEnd; ++I) {
5689 llvm::errs() << " " << I->first << " -> " << I->second->FileName
5690 << "\n";
5691 }
5692}
5693
5694void ASTReader::dump() {
5695 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
5696 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
5697 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
5698 dumpModuleIDMap("Global type map", GlobalTypeMap);
5699 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
5700 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
5701 dumpModuleIDMap("Global macro map", GlobalMacroMap);
5702 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
5703 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
5704 dumpModuleIDMap("Global preprocessed entity map",
5705 GlobalPreprocessedEntityMap);
5706
5707 llvm::errs() << "\n*** PCH/Modules Loaded:";
5708 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
5709 MEnd = ModuleMgr.end();
5710 M != MEnd; ++M)
5711 (*M)->dump();
5712}
5713
5714/// Return the amount of memory used by memory buffers, breaking down
5715/// by heap-backed versus mmap'ed memory.
5716void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
5717 for (ModuleConstIterator I = ModuleMgr.begin(),
5718 E = ModuleMgr.end(); I != E; ++I) {
5719 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
5720 size_t bytes = buf->getBufferSize();
5721 switch (buf->getBufferKind()) {
5722 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
5723 sizes.malloc_bytes += bytes;
5724 break;
5725 case llvm::MemoryBuffer::MemoryBuffer_MMap:
5726 sizes.mmap_bytes += bytes;
5727 break;
5728 }
5729 }
5730 }
5731}
5732
5733void ASTReader::InitializeSema(Sema &S) {
5734 SemaObj = &S;
5735 S.addExternalSource(this);
5736
5737 // Makes sure any declarations that were deserialized "too early"
5738 // still get added to the identifier's declaration chains.
5739 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
5740 SemaObj->pushExternalDeclIntoScope(PreloadedDecls[I],
5741 PreloadedDecls[I]->getDeclName());
5742 }
5743 PreloadedDecls.clear();
5744
5745 // Load the offsets of the declarations that Sema references.
5746 // They will be lazily deserialized when needed.
5747 if (!SemaDeclRefs.empty()) {
5748 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
5749 if (!SemaObj->StdNamespace)
5750 SemaObj->StdNamespace = SemaDeclRefs[0];
5751 if (!SemaObj->StdBadAlloc)
5752 SemaObj->StdBadAlloc = SemaDeclRefs[1];
5753 }
5754
5755 if (!FPPragmaOptions.empty()) {
5756 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
5757 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
5758 }
5759
5760 if (!OpenCLExtensions.empty()) {
5761 unsigned I = 0;
5762#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
5763#include "clang/Basic/OpenCLExtensions.def"
5764
5765 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
5766 }
5767}
5768
5769IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
5770 // Note that we are loading an identifier.
5771 Deserializing AnIdentifier(this);
Douglas Gregor1a49d972013-01-25 01:03:03 +00005772 StringRef Name(NameStart, NameEnd - NameStart);
5773
5774 // If there is a global index, look there first to determine which modules
5775 // provably do not have any results for this identifier.
Douglas Gregor188bdcd2013-01-25 23:32:03 +00005776 GlobalModuleIndex::HitSet Hits;
5777 GlobalModuleIndex::HitSet *HitsPtr = 0;
Douglas Gregor1a49d972013-01-25 01:03:03 +00005778 if (!loadGlobalIndex()) {
Douglas Gregor188bdcd2013-01-25 23:32:03 +00005779 if (GlobalIndex->lookupIdentifier(Name, Hits)) {
5780 HitsPtr = &Hits;
Douglas Gregor1a49d972013-01-25 01:03:03 +00005781 }
5782 }
Douglas Gregor188bdcd2013-01-25 23:32:03 +00005783 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
Douglas Gregore1698072013-01-25 00:38:33 +00005784 NumIdentifierLookups,
5785 NumIdentifierLookupHits);
Douglas Gregor188bdcd2013-01-25 23:32:03 +00005786 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor, HitsPtr);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005787 IdentifierInfo *II = Visitor.getIdentifierInfo();
5788 markIdentifierUpToDate(II);
5789 return II;
5790}
5791
5792namespace clang {
5793 /// \brief An identifier-lookup iterator that enumerates all of the
5794 /// identifiers stored within a set of AST files.
5795 class ASTIdentifierIterator : public IdentifierIterator {
5796 /// \brief The AST reader whose identifiers are being enumerated.
5797 const ASTReader &Reader;
5798
5799 /// \brief The current index into the chain of AST files stored in
5800 /// the AST reader.
5801 unsigned Index;
5802
5803 /// \brief The current position within the identifier lookup table
5804 /// of the current AST file.
5805 ASTIdentifierLookupTable::key_iterator Current;
5806
5807 /// \brief The end position within the identifier lookup table of
5808 /// the current AST file.
5809 ASTIdentifierLookupTable::key_iterator End;
5810
5811 public:
5812 explicit ASTIdentifierIterator(const ASTReader &Reader);
5813
5814 virtual StringRef Next();
5815 };
5816}
5817
5818ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
5819 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
5820 ASTIdentifierLookupTable *IdTable
5821 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
5822 Current = IdTable->key_begin();
5823 End = IdTable->key_end();
5824}
5825
5826StringRef ASTIdentifierIterator::Next() {
5827 while (Current == End) {
5828 // If we have exhausted all of our AST files, we're done.
5829 if (Index == 0)
5830 return StringRef();
5831
5832 --Index;
5833 ASTIdentifierLookupTable *IdTable
5834 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
5835 IdentifierLookupTable;
5836 Current = IdTable->key_begin();
5837 End = IdTable->key_end();
5838 }
5839
5840 // We have any identifiers remaining in the current AST file; return
5841 // the next one.
Douglas Gregor479633c2013-01-23 18:53:14 +00005842 StringRef Result = *Current;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005843 ++Current;
Douglas Gregor479633c2013-01-23 18:53:14 +00005844 return Result;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005845}
5846
5847IdentifierIterator *ASTReader::getIdentifiers() const {
5848 return new ASTIdentifierIterator(*this);
5849}
5850
5851namespace clang { namespace serialization {
5852 class ReadMethodPoolVisitor {
5853 ASTReader &Reader;
5854 Selector Sel;
5855 unsigned PriorGeneration;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00005856 SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
5857 SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005858
5859 public:
5860 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
5861 unsigned PriorGeneration)
5862 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) { }
5863
5864 static bool visit(ModuleFile &M, void *UserData) {
5865 ReadMethodPoolVisitor *This
5866 = static_cast<ReadMethodPoolVisitor *>(UserData);
5867
5868 if (!M.SelectorLookupTable)
5869 return false;
5870
5871 // If we've already searched this module file, skip it now.
5872 if (M.Generation <= This->PriorGeneration)
5873 return true;
5874
5875 ASTSelectorLookupTable *PoolTable
5876 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
5877 ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel);
5878 if (Pos == PoolTable->end())
5879 return false;
5880
5881 ++This->Reader.NumSelectorsRead;
5882 // FIXME: Not quite happy with the statistics here. We probably should
5883 // disable this tracking when called via LoadSelector.
5884 // Also, should entries without methods count as misses?
5885 ++This->Reader.NumMethodPoolEntriesRead;
5886 ASTSelectorLookupTrait::data_type Data = *Pos;
5887 if (This->Reader.DeserializationListener)
5888 This->Reader.DeserializationListener->SelectorRead(Data.ID,
5889 This->Sel);
5890
5891 This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
5892 This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
5893 return true;
5894 }
5895
5896 /// \brief Retrieve the instance methods found by this visitor.
5897 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
5898 return InstanceMethods;
5899 }
5900
5901 /// \brief Retrieve the instance methods found by this visitor.
5902 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
5903 return FactoryMethods;
5904 }
5905 };
5906} } // end namespace clang::serialization
5907
5908/// \brief Add the given set of methods to the method list.
5909static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
5910 ObjCMethodList &List) {
5911 for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
5912 S.addMethodToGlobalList(&List, Methods[I]);
5913 }
5914}
5915
5916void ASTReader::ReadMethodPool(Selector Sel) {
5917 // Get the selector generation and update it to the current generation.
5918 unsigned &Generation = SelectorGeneration[Sel];
5919 unsigned PriorGeneration = Generation;
5920 Generation = CurrentGeneration;
5921
5922 // Search for methods defined with this selector.
5923 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
5924 ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor);
5925
5926 if (Visitor.getInstanceMethods().empty() &&
5927 Visitor.getFactoryMethods().empty()) {
5928 ++NumMethodPoolMisses;
5929 return;
5930 }
5931
5932 if (!getSema())
5933 return;
5934
5935 Sema &S = *getSema();
5936 Sema::GlobalMethodPool::iterator Pos
5937 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
5938
5939 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
5940 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
5941}
5942
5943void ASTReader::ReadKnownNamespaces(
5944 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
5945 Namespaces.clear();
5946
5947 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
5948 if (NamespaceDecl *Namespace
5949 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
5950 Namespaces.push_back(Namespace);
5951 }
5952}
5953
Nick Lewycky01a41142013-01-26 00:35:08 +00005954void ASTReader::ReadUndefinedInternals(
5955 llvm::MapVector<NamedDecl*, SourceLocation> &Undefined) {
5956 for (unsigned Idx = 0, N = UndefinedInternals.size(); Idx != N;) {
5957 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedInternals[Idx++]));
5958 SourceLocation Loc =
5959 SourceLocation::getFromRawEncoding(UndefinedInternals[Idx++]);
5960 Undefined.insert(std::make_pair(D, Loc));
5961 }
5962}
5963
5964
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005965void ASTReader::ReadTentativeDefinitions(
5966 SmallVectorImpl<VarDecl *> &TentativeDefs) {
5967 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
5968 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
5969 if (Var)
5970 TentativeDefs.push_back(Var);
5971 }
5972 TentativeDefinitions.clear();
5973}
5974
5975void ASTReader::ReadUnusedFileScopedDecls(
5976 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
5977 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
5978 DeclaratorDecl *D
5979 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
5980 if (D)
5981 Decls.push_back(D);
5982 }
5983 UnusedFileScopedDecls.clear();
5984}
5985
5986void ASTReader::ReadDelegatingConstructors(
5987 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
5988 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
5989 CXXConstructorDecl *D
5990 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
5991 if (D)
5992 Decls.push_back(D);
5993 }
5994 DelegatingCtorDecls.clear();
5995}
5996
5997void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
5998 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
5999 TypedefNameDecl *D
6000 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
6001 if (D)
6002 Decls.push_back(D);
6003 }
6004 ExtVectorDecls.clear();
6005}
6006
6007void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) {
6008 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
6009 CXXRecordDecl *D
6010 = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I]));
6011 if (D)
6012 Decls.push_back(D);
6013 }
6014 DynamicClasses.clear();
6015}
6016
6017void
Richard Smith5ea6ef42013-01-10 23:43:47 +00006018ASTReader::ReadLocallyScopedExternCDecls(SmallVectorImpl<NamedDecl *> &Decls) {
6019 for (unsigned I = 0, N = LocallyScopedExternCDecls.size(); I != N; ++I) {
6020 NamedDecl *D
6021 = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternCDecls[I]));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006022 if (D)
6023 Decls.push_back(D);
6024 }
Richard Smith5ea6ef42013-01-10 23:43:47 +00006025 LocallyScopedExternCDecls.clear();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006026}
6027
6028void ASTReader::ReadReferencedSelectors(
6029 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
6030 if (ReferencedSelectorsData.empty())
6031 return;
6032
6033 // If there are @selector references added them to its pool. This is for
6034 // implementation of -Wselector.
6035 unsigned int DataSize = ReferencedSelectorsData.size()-1;
6036 unsigned I = 0;
6037 while (I < DataSize) {
6038 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
6039 SourceLocation SelLoc
6040 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
6041 Sels.push_back(std::make_pair(Sel, SelLoc));
6042 }
6043 ReferencedSelectorsData.clear();
6044}
6045
6046void ASTReader::ReadWeakUndeclaredIdentifiers(
6047 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
6048 if (WeakUndeclaredIdentifiers.empty())
6049 return;
6050
6051 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
6052 IdentifierInfo *WeakId
6053 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
6054 IdentifierInfo *AliasId
6055 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
6056 SourceLocation Loc
6057 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
6058 bool Used = WeakUndeclaredIdentifiers[I++];
6059 WeakInfo WI(AliasId, Loc);
6060 WI.setUsed(Used);
6061 WeakIDs.push_back(std::make_pair(WeakId, WI));
6062 }
6063 WeakUndeclaredIdentifiers.clear();
6064}
6065
6066void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
6067 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
6068 ExternalVTableUse VT;
6069 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
6070 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
6071 VT.DefinitionRequired = VTableUses[Idx++];
6072 VTables.push_back(VT);
6073 }
6074
6075 VTableUses.clear();
6076}
6077
6078void ASTReader::ReadPendingInstantiations(
6079 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
6080 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
6081 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
6082 SourceLocation Loc
6083 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
6084
6085 Pending.push_back(std::make_pair(D, Loc));
6086 }
6087 PendingInstantiations.clear();
6088}
6089
6090void ASTReader::LoadSelector(Selector Sel) {
6091 // It would be complicated to avoid reading the methods anyway. So don't.
6092 ReadMethodPool(Sel);
6093}
6094
6095void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
6096 assert(ID && "Non-zero identifier ID required");
6097 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
6098 IdentifiersLoaded[ID - 1] = II;
6099 if (DeserializationListener)
6100 DeserializationListener->IdentifierRead(ID, II);
6101}
6102
6103/// \brief Set the globally-visible declarations associated with the given
6104/// identifier.
6105///
6106/// If the AST reader is currently in a state where the given declaration IDs
6107/// cannot safely be resolved, they are queued until it is safe to resolve
6108/// them.
6109///
6110/// \param II an IdentifierInfo that refers to one or more globally-visible
6111/// declarations.
6112///
6113/// \param DeclIDs the set of declaration IDs with the name @p II that are
6114/// visible at global scope.
6115///
6116/// \param Nonrecursive should be true to indicate that the caller knows that
6117/// this call is non-recursive, and therefore the globally-visible declarations
6118/// will not be placed onto the pending queue.
6119void
6120ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
6121 const SmallVectorImpl<uint32_t> &DeclIDs,
6122 bool Nonrecursive) {
6123 if (NumCurrentElementsDeserializing && !Nonrecursive) {
6124 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
6125 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
6126 PII.II = II;
6127 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
6128 return;
6129 }
6130
6131 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
6132 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
6133 if (SemaObj) {
6134 // Introduce this declaration into the translation-unit scope
6135 // and add it to the declaration chain for this identifier, so
6136 // that (unqualified) name lookup will find it.
6137 SemaObj->pushExternalDeclIntoScope(D, II);
6138 } else {
6139 // Queue this declaration so that it will be added to the
6140 // translation unit scope and identifier's declaration chain
6141 // once a Sema object is known.
6142 PreloadedDecls.push_back(D);
6143 }
6144 }
6145}
6146
Douglas Gregor8222b892013-01-21 16:52:34 +00006147IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006148 if (ID == 0)
6149 return 0;
6150
6151 if (IdentifiersLoaded.empty()) {
6152 Error("no identifier table in AST file");
6153 return 0;
6154 }
6155
6156 ID -= 1;
6157 if (!IdentifiersLoaded[ID]) {
6158 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
6159 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
6160 ModuleFile *M = I->second;
6161 unsigned Index = ID - M->BaseIdentifierID;
6162 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
6163
6164 // All of the strings in the AST file are preceded by a 16-bit length.
6165 // Extract that 16-bit length to avoid having to execute strlen().
6166 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
6167 // unsigned integers. This is important to avoid integer overflow when
6168 // we cast them to 'unsigned'.
6169 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
6170 unsigned StrLen = (((unsigned) StrLenPtr[0])
6171 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Douglas Gregor8222b892013-01-21 16:52:34 +00006172 IdentifiersLoaded[ID]
6173 = &PP.getIdentifierTable().get(StringRef(Str, StrLen));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006174 if (DeserializationListener)
6175 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
6176 }
6177
6178 return IdentifiersLoaded[ID];
6179}
6180
Douglas Gregor8222b892013-01-21 16:52:34 +00006181IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
6182 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006183}
6184
6185IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
6186 if (LocalID < NUM_PREDEF_IDENT_IDS)
6187 return LocalID;
6188
6189 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6190 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
6191 assert(I != M.IdentifierRemap.end()
6192 && "Invalid index into identifier index remap");
6193
6194 return LocalID + I->second;
6195}
6196
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00006197MacroInfo *ASTReader::getMacro(MacroID ID, MacroInfo *Hint) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006198 if (ID == 0)
6199 return 0;
6200
6201 if (MacrosLoaded.empty()) {
6202 Error("no macro table in AST file");
6203 return 0;
6204 }
6205
6206 ID -= NUM_PREDEF_MACRO_IDS;
6207 if (!MacrosLoaded[ID]) {
6208 GlobalMacroMapType::iterator I
6209 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
6210 assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
6211 ModuleFile *M = I->second;
6212 unsigned Index = ID - M->BaseMacroID;
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00006213 ReadMacroRecord(*M, M->MacroOffsets[Index], Hint);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006214 }
6215
6216 return MacrosLoaded[ID];
6217}
6218
6219MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
6220 if (LocalID < NUM_PREDEF_MACRO_IDS)
6221 return LocalID;
6222
6223 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6224 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
6225 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
6226
6227 return LocalID + I->second;
6228}
6229
6230serialization::SubmoduleID
6231ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
6232 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
6233 return LocalID;
6234
6235 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6236 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
6237 assert(I != M.SubmoduleRemap.end()
6238 && "Invalid index into submodule index remap");
6239
6240 return LocalID + I->second;
6241}
6242
6243Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
6244 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
6245 assert(GlobalID == 0 && "Unhandled global submodule ID");
6246 return 0;
6247 }
6248
6249 if (GlobalID > SubmodulesLoaded.size()) {
6250 Error("submodule ID out of range in AST file");
6251 return 0;
6252 }
6253
6254 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
6255}
Douglas Gregorca2ab452013-01-12 01:29:50 +00006256
6257Module *ASTReader::getModule(unsigned ID) {
6258 return getSubmodule(ID);
6259}
6260
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006261Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
6262 return DecodeSelector(getGlobalSelectorID(M, LocalID));
6263}
6264
6265Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
6266 if (ID == 0)
6267 return Selector();
6268
6269 if (ID > SelectorsLoaded.size()) {
6270 Error("selector ID out of range in AST file");
6271 return Selector();
6272 }
6273
6274 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
6275 // Load this selector from the selector table.
6276 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
6277 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
6278 ModuleFile &M = *I->second;
6279 ASTSelectorLookupTrait Trait(*this, M);
6280 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
6281 SelectorsLoaded[ID - 1] =
6282 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
6283 if (DeserializationListener)
6284 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
6285 }
6286
6287 return SelectorsLoaded[ID - 1];
6288}
6289
6290Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
6291 return DecodeSelector(ID);
6292}
6293
6294uint32_t ASTReader::GetNumExternalSelectors() {
6295 // ID 0 (the null selector) is considered an external selector.
6296 return getTotalNumSelectors() + 1;
6297}
6298
6299serialization::SelectorID
6300ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
6301 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
6302 return LocalID;
6303
6304 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6305 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
6306 assert(I != M.SelectorRemap.end()
6307 && "Invalid index into selector index remap");
6308
6309 return LocalID + I->second;
6310}
6311
6312DeclarationName
6313ASTReader::ReadDeclarationName(ModuleFile &F,
6314 const RecordData &Record, unsigned &Idx) {
6315 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
6316 switch (Kind) {
6317 case DeclarationName::Identifier:
Douglas Gregor8222b892013-01-21 16:52:34 +00006318 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006319
6320 case DeclarationName::ObjCZeroArgSelector:
6321 case DeclarationName::ObjCOneArgSelector:
6322 case DeclarationName::ObjCMultiArgSelector:
6323 return DeclarationName(ReadSelector(F, Record, Idx));
6324
6325 case DeclarationName::CXXConstructorName:
6326 return Context.DeclarationNames.getCXXConstructorName(
6327 Context.getCanonicalType(readType(F, Record, Idx)));
6328
6329 case DeclarationName::CXXDestructorName:
6330 return Context.DeclarationNames.getCXXDestructorName(
6331 Context.getCanonicalType(readType(F, Record, Idx)));
6332
6333 case DeclarationName::CXXConversionFunctionName:
6334 return Context.DeclarationNames.getCXXConversionFunctionName(
6335 Context.getCanonicalType(readType(F, Record, Idx)));
6336
6337 case DeclarationName::CXXOperatorName:
6338 return Context.DeclarationNames.getCXXOperatorName(
6339 (OverloadedOperatorKind)Record[Idx++]);
6340
6341 case DeclarationName::CXXLiteralOperatorName:
6342 return Context.DeclarationNames.getCXXLiteralOperatorName(
6343 GetIdentifierInfo(F, Record, Idx));
6344
6345 case DeclarationName::CXXUsingDirective:
6346 return DeclarationName::getUsingDirectiveName();
6347 }
6348
6349 llvm_unreachable("Invalid NameKind!");
6350}
6351
6352void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
6353 DeclarationNameLoc &DNLoc,
6354 DeclarationName Name,
6355 const RecordData &Record, unsigned &Idx) {
6356 switch (Name.getNameKind()) {
6357 case DeclarationName::CXXConstructorName:
6358 case DeclarationName::CXXDestructorName:
6359 case DeclarationName::CXXConversionFunctionName:
6360 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
6361 break;
6362
6363 case DeclarationName::CXXOperatorName:
6364 DNLoc.CXXOperatorName.BeginOpNameLoc
6365 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6366 DNLoc.CXXOperatorName.EndOpNameLoc
6367 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6368 break;
6369
6370 case DeclarationName::CXXLiteralOperatorName:
6371 DNLoc.CXXLiteralOperatorName.OpNameLoc
6372 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6373 break;
6374
6375 case DeclarationName::Identifier:
6376 case DeclarationName::ObjCZeroArgSelector:
6377 case DeclarationName::ObjCOneArgSelector:
6378 case DeclarationName::ObjCMultiArgSelector:
6379 case DeclarationName::CXXUsingDirective:
6380 break;
6381 }
6382}
6383
6384void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
6385 DeclarationNameInfo &NameInfo,
6386 const RecordData &Record, unsigned &Idx) {
6387 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
6388 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
6389 DeclarationNameLoc DNLoc;
6390 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
6391 NameInfo.setInfo(DNLoc);
6392}
6393
6394void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
6395 const RecordData &Record, unsigned &Idx) {
6396 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
6397 unsigned NumTPLists = Record[Idx++];
6398 Info.NumTemplParamLists = NumTPLists;
6399 if (NumTPLists) {
6400 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
6401 for (unsigned i=0; i != NumTPLists; ++i)
6402 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
6403 }
6404}
6405
6406TemplateName
6407ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
6408 unsigned &Idx) {
6409 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
6410 switch (Kind) {
6411 case TemplateName::Template:
6412 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
6413
6414 case TemplateName::OverloadedTemplate: {
6415 unsigned size = Record[Idx++];
6416 UnresolvedSet<8> Decls;
6417 while (size--)
6418 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
6419
6420 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
6421 }
6422
6423 case TemplateName::QualifiedTemplate: {
6424 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
6425 bool hasTemplKeyword = Record[Idx++];
6426 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
6427 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
6428 }
6429
6430 case TemplateName::DependentTemplate: {
6431 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
6432 if (Record[Idx++]) // isIdentifier
6433 return Context.getDependentTemplateName(NNS,
6434 GetIdentifierInfo(F, Record,
6435 Idx));
6436 return Context.getDependentTemplateName(NNS,
6437 (OverloadedOperatorKind)Record[Idx++]);
6438 }
6439
6440 case TemplateName::SubstTemplateTemplateParm: {
6441 TemplateTemplateParmDecl *param
6442 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
6443 if (!param) return TemplateName();
6444 TemplateName replacement = ReadTemplateName(F, Record, Idx);
6445 return Context.getSubstTemplateTemplateParm(param, replacement);
6446 }
6447
6448 case TemplateName::SubstTemplateTemplateParmPack: {
6449 TemplateTemplateParmDecl *Param
6450 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
6451 if (!Param)
6452 return TemplateName();
6453
6454 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
6455 if (ArgPack.getKind() != TemplateArgument::Pack)
6456 return TemplateName();
6457
6458 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
6459 }
6460 }
6461
6462 llvm_unreachable("Unhandled template name kind!");
6463}
6464
6465TemplateArgument
6466ASTReader::ReadTemplateArgument(ModuleFile &F,
6467 const RecordData &Record, unsigned &Idx) {
6468 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
6469 switch (Kind) {
6470 case TemplateArgument::Null:
6471 return TemplateArgument();
6472 case TemplateArgument::Type:
6473 return TemplateArgument(readType(F, Record, Idx));
6474 case TemplateArgument::Declaration: {
6475 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
6476 bool ForReferenceParam = Record[Idx++];
6477 return TemplateArgument(D, ForReferenceParam);
6478 }
6479 case TemplateArgument::NullPtr:
6480 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
6481 case TemplateArgument::Integral: {
6482 llvm::APSInt Value = ReadAPSInt(Record, Idx);
6483 QualType T = readType(F, Record, Idx);
6484 return TemplateArgument(Context, Value, T);
6485 }
6486 case TemplateArgument::Template:
6487 return TemplateArgument(ReadTemplateName(F, Record, Idx));
6488 case TemplateArgument::TemplateExpansion: {
6489 TemplateName Name = ReadTemplateName(F, Record, Idx);
6490 llvm::Optional<unsigned> NumTemplateExpansions;
6491 if (unsigned NumExpansions = Record[Idx++])
6492 NumTemplateExpansions = NumExpansions - 1;
6493 return TemplateArgument(Name, NumTemplateExpansions);
6494 }
6495 case TemplateArgument::Expression:
6496 return TemplateArgument(ReadExpr(F));
6497 case TemplateArgument::Pack: {
6498 unsigned NumArgs = Record[Idx++];
6499 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
6500 for (unsigned I = 0; I != NumArgs; ++I)
6501 Args[I] = ReadTemplateArgument(F, Record, Idx);
6502 return TemplateArgument(Args, NumArgs);
6503 }
6504 }
6505
6506 llvm_unreachable("Unhandled template argument kind!");
6507}
6508
6509TemplateParameterList *
6510ASTReader::ReadTemplateParameterList(ModuleFile &F,
6511 const RecordData &Record, unsigned &Idx) {
6512 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
6513 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
6514 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
6515
6516 unsigned NumParams = Record[Idx++];
6517 SmallVector<NamedDecl *, 16> Params;
6518 Params.reserve(NumParams);
6519 while (NumParams--)
6520 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
6521
6522 TemplateParameterList* TemplateParams =
6523 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
6524 Params.data(), Params.size(), RAngleLoc);
6525 return TemplateParams;
6526}
6527
6528void
6529ASTReader::
6530ReadTemplateArgumentList(SmallVector<TemplateArgument, 8> &TemplArgs,
6531 ModuleFile &F, const RecordData &Record,
6532 unsigned &Idx) {
6533 unsigned NumTemplateArgs = Record[Idx++];
6534 TemplArgs.reserve(NumTemplateArgs);
6535 while (NumTemplateArgs--)
6536 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
6537}
6538
6539/// \brief Read a UnresolvedSet structure.
6540void ASTReader::ReadUnresolvedSet(ModuleFile &F, ASTUnresolvedSet &Set,
6541 const RecordData &Record, unsigned &Idx) {
6542 unsigned NumDecls = Record[Idx++];
6543 Set.reserve(Context, NumDecls);
6544 while (NumDecls--) {
6545 NamedDecl *D = ReadDeclAs<NamedDecl>(F, Record, Idx);
6546 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
6547 Set.addDecl(Context, D, AS);
6548 }
6549}
6550
6551CXXBaseSpecifier
6552ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
6553 const RecordData &Record, unsigned &Idx) {
6554 bool isVirtual = static_cast<bool>(Record[Idx++]);
6555 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
6556 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
6557 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
6558 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
6559 SourceRange Range = ReadSourceRange(F, Record, Idx);
6560 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
6561 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
6562 EllipsisLoc);
6563 Result.setInheritConstructors(inheritConstructors);
6564 return Result;
6565}
6566
6567std::pair<CXXCtorInitializer **, unsigned>
6568ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
6569 unsigned &Idx) {
6570 CXXCtorInitializer **CtorInitializers = 0;
6571 unsigned NumInitializers = Record[Idx++];
6572 if (NumInitializers) {
6573 CtorInitializers
6574 = new (Context) CXXCtorInitializer*[NumInitializers];
6575 for (unsigned i=0; i != NumInitializers; ++i) {
6576 TypeSourceInfo *TInfo = 0;
6577 bool IsBaseVirtual = false;
6578 FieldDecl *Member = 0;
6579 IndirectFieldDecl *IndirectMember = 0;
6580
6581 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
6582 switch (Type) {
6583 case CTOR_INITIALIZER_BASE:
6584 TInfo = GetTypeSourceInfo(F, Record, Idx);
6585 IsBaseVirtual = Record[Idx++];
6586 break;
6587
6588 case CTOR_INITIALIZER_DELEGATING:
6589 TInfo = GetTypeSourceInfo(F, Record, Idx);
6590 break;
6591
6592 case CTOR_INITIALIZER_MEMBER:
6593 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
6594 break;
6595
6596 case CTOR_INITIALIZER_INDIRECT_MEMBER:
6597 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
6598 break;
6599 }
6600
6601 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
6602 Expr *Init = ReadExpr(F);
6603 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
6604 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
6605 bool IsWritten = Record[Idx++];
6606 unsigned SourceOrderOrNumArrayIndices;
6607 SmallVector<VarDecl *, 8> Indices;
6608 if (IsWritten) {
6609 SourceOrderOrNumArrayIndices = Record[Idx++];
6610 } else {
6611 SourceOrderOrNumArrayIndices = Record[Idx++];
6612 Indices.reserve(SourceOrderOrNumArrayIndices);
6613 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
6614 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
6615 }
6616
6617 CXXCtorInitializer *BOMInit;
6618 if (Type == CTOR_INITIALIZER_BASE) {
6619 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, IsBaseVirtual,
6620 LParenLoc, Init, RParenLoc,
6621 MemberOrEllipsisLoc);
6622 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
6623 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, LParenLoc,
6624 Init, RParenLoc);
6625 } else if (IsWritten) {
6626 if (Member)
6627 BOMInit = new (Context) CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc,
6628 LParenLoc, Init, RParenLoc);
6629 else
6630 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
6631 MemberOrEllipsisLoc, LParenLoc,
6632 Init, RParenLoc);
6633 } else {
6634 BOMInit = CXXCtorInitializer::Create(Context, Member, MemberOrEllipsisLoc,
6635 LParenLoc, Init, RParenLoc,
6636 Indices.data(), Indices.size());
6637 }
6638
6639 if (IsWritten)
6640 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
6641 CtorInitializers[i] = BOMInit;
6642 }
6643 }
6644
6645 return std::make_pair(CtorInitializers, NumInitializers);
6646}
6647
6648NestedNameSpecifier *
6649ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
6650 const RecordData &Record, unsigned &Idx) {
6651 unsigned N = Record[Idx++];
6652 NestedNameSpecifier *NNS = 0, *Prev = 0;
6653 for (unsigned I = 0; I != N; ++I) {
6654 NestedNameSpecifier::SpecifierKind Kind
6655 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
6656 switch (Kind) {
6657 case NestedNameSpecifier::Identifier: {
6658 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
6659 NNS = NestedNameSpecifier::Create(Context, Prev, II);
6660 break;
6661 }
6662
6663 case NestedNameSpecifier::Namespace: {
6664 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
6665 NNS = NestedNameSpecifier::Create(Context, Prev, NS);
6666 break;
6667 }
6668
6669 case NestedNameSpecifier::NamespaceAlias: {
6670 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
6671 NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
6672 break;
6673 }
6674
6675 case NestedNameSpecifier::TypeSpec:
6676 case NestedNameSpecifier::TypeSpecWithTemplate: {
6677 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
6678 if (!T)
6679 return 0;
6680
6681 bool Template = Record[Idx++];
6682 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
6683 break;
6684 }
6685
6686 case NestedNameSpecifier::Global: {
6687 NNS = NestedNameSpecifier::GlobalSpecifier(Context);
6688 // No associated value, and there can't be a prefix.
6689 break;
6690 }
6691 }
6692 Prev = NNS;
6693 }
6694 return NNS;
6695}
6696
6697NestedNameSpecifierLoc
6698ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
6699 unsigned &Idx) {
6700 unsigned N = Record[Idx++];
6701 NestedNameSpecifierLocBuilder Builder;
6702 for (unsigned I = 0; I != N; ++I) {
6703 NestedNameSpecifier::SpecifierKind Kind
6704 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
6705 switch (Kind) {
6706 case NestedNameSpecifier::Identifier: {
6707 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
6708 SourceRange Range = ReadSourceRange(F, Record, Idx);
6709 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
6710 break;
6711 }
6712
6713 case NestedNameSpecifier::Namespace: {
6714 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
6715 SourceRange Range = ReadSourceRange(F, Record, Idx);
6716 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
6717 break;
6718 }
6719
6720 case NestedNameSpecifier::NamespaceAlias: {
6721 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
6722 SourceRange Range = ReadSourceRange(F, Record, Idx);
6723 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
6724 break;
6725 }
6726
6727 case NestedNameSpecifier::TypeSpec:
6728 case NestedNameSpecifier::TypeSpecWithTemplate: {
6729 bool Template = Record[Idx++];
6730 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
6731 if (!T)
6732 return NestedNameSpecifierLoc();
6733 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
6734
6735 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
6736 Builder.Extend(Context,
6737 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
6738 T->getTypeLoc(), ColonColonLoc);
6739 break;
6740 }
6741
6742 case NestedNameSpecifier::Global: {
6743 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
6744 Builder.MakeGlobal(Context, ColonColonLoc);
6745 break;
6746 }
6747 }
6748 }
6749
6750 return Builder.getWithLocInContext(Context);
6751}
6752
6753SourceRange
6754ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
6755 unsigned &Idx) {
6756 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
6757 SourceLocation end = ReadSourceLocation(F, Record, Idx);
6758 return SourceRange(beg, end);
6759}
6760
6761/// \brief Read an integral value
6762llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
6763 unsigned BitWidth = Record[Idx++];
6764 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
6765 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
6766 Idx += NumWords;
6767 return Result;
6768}
6769
6770/// \brief Read a signed integral value
6771llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
6772 bool isUnsigned = Record[Idx++];
6773 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
6774}
6775
6776/// \brief Read a floating-point value
Tim Northover9ec55f22013-01-22 09:46:51 +00006777llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record,
6778 const llvm::fltSemantics &Sem,
6779 unsigned &Idx) {
6780 return llvm::APFloat(Sem, ReadAPInt(Record, Idx));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006781}
6782
6783// \brief Read a string
6784std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
6785 unsigned Len = Record[Idx++];
6786 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
6787 Idx += Len;
6788 return Result;
6789}
6790
6791VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
6792 unsigned &Idx) {
6793 unsigned Major = Record[Idx++];
6794 unsigned Minor = Record[Idx++];
6795 unsigned Subminor = Record[Idx++];
6796 if (Minor == 0)
6797 return VersionTuple(Major);
6798 if (Subminor == 0)
6799 return VersionTuple(Major, Minor - 1);
6800 return VersionTuple(Major, Minor - 1, Subminor - 1);
6801}
6802
6803CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
6804 const RecordData &Record,
6805 unsigned &Idx) {
6806 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
6807 return CXXTemporary::Create(Context, Decl);
6808}
6809
6810DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
6811 return Diag(SourceLocation(), DiagID);
6812}
6813
6814DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
6815 return Diags.Report(Loc, DiagID);
6816}
6817
6818/// \brief Retrieve the identifier table associated with the
6819/// preprocessor.
6820IdentifierTable &ASTReader::getIdentifierTable() {
6821 return PP.getIdentifierTable();
6822}
6823
6824/// \brief Record that the given ID maps to the given switch-case
6825/// statement.
6826void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
6827 assert((*CurrSwitchCaseStmts)[ID] == 0 &&
6828 "Already have a SwitchCase with this ID");
6829 (*CurrSwitchCaseStmts)[ID] = SC;
6830}
6831
6832/// \brief Retrieve the switch-case statement with the given ID.
6833SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
6834 assert((*CurrSwitchCaseStmts)[ID] != 0 && "No SwitchCase with this ID");
6835 return (*CurrSwitchCaseStmts)[ID];
6836}
6837
6838void ASTReader::ClearSwitchCaseIDs() {
6839 CurrSwitchCaseStmts->clear();
6840}
6841
6842void ASTReader::ReadComments() {
6843 std::vector<RawComment *> Comments;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00006844 for (SmallVectorImpl<std::pair<BitstreamCursor,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006845 serialization::ModuleFile *> >::iterator
6846 I = CommentsCursors.begin(),
6847 E = CommentsCursors.end();
6848 I != E; ++I) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00006849 BitstreamCursor &Cursor = I->first;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006850 serialization::ModuleFile &F = *I->second;
6851 SavedStreamPosition SavedPosition(Cursor);
6852
6853 RecordData Record;
6854 while (true) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00006855 llvm::BitstreamEntry Entry =
6856 Cursor.advanceSkippingSubblocks(BitstreamCursor::AF_DontPopBlockAtEnd);
6857
6858 switch (Entry.Kind) {
6859 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
6860 case llvm::BitstreamEntry::Error:
6861 Error("malformed block record in AST file");
6862 return;
6863 case llvm::BitstreamEntry::EndBlock:
6864 goto NextCursor;
6865 case llvm::BitstreamEntry::Record:
6866 // The interesting case.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006867 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006868 }
6869
6870 // Read a record.
6871 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00006872 switch ((CommentRecordTypes)Cursor.readRecord(Entry.ID, Record)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006873 case COMMENTS_RAW_COMMENT: {
6874 unsigned Idx = 0;
6875 SourceRange SR = ReadSourceRange(F, Record, Idx);
6876 RawComment::CommentKind Kind =
6877 (RawComment::CommentKind) Record[Idx++];
6878 bool IsTrailingComment = Record[Idx++];
6879 bool IsAlmostTrailingComment = Record[Idx++];
6880 Comments.push_back(new (Context) RawComment(SR, Kind,
6881 IsTrailingComment,
6882 IsAlmostTrailingComment));
6883 break;
6884 }
6885 }
6886 }
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00006887 NextCursor:;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006888 }
6889 Context.Comments.addCommentsToFront(Comments);
6890}
6891
6892void ASTReader::finishPendingActions() {
6893 while (!PendingIdentifierInfos.empty() || !PendingDeclChains.empty() ||
6894 !PendingMacroIDs.empty()) {
6895 // If any identifiers with corresponding top-level declarations have
6896 // been loaded, load those declarations now.
6897 while (!PendingIdentifierInfos.empty()) {
6898 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
6899 PendingIdentifierInfos.front().DeclIDs, true);
6900 PendingIdentifierInfos.pop_front();
6901 }
6902
6903 // Load pending declaration chains.
6904 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) {
6905 loadPendingDeclChain(PendingDeclChains[I]);
6906 PendingDeclChainsKnown.erase(PendingDeclChains[I]);
6907 }
6908 PendingDeclChains.clear();
6909
6910 // Load any pending macro definitions.
6911 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00006912 // FIXME: std::move here
6913 SmallVector<MacroID, 2> GlobalIDs = PendingMacroIDs.begin()[I].second;
6914 MacroInfo *Hint = 0;
6915 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
6916 ++IDIdx) {
6917 Hint = getMacro(GlobalIDs[IDIdx], Hint);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006918 }
6919 }
6920 PendingMacroIDs.clear();
6921 }
6922
6923 // If we deserialized any C++ or Objective-C class definitions, any
6924 // Objective-C protocol definitions, or any redeclarable templates, make sure
6925 // that all redeclarations point to the definitions. Note that this can only
6926 // happen now, after the redeclaration chains have been fully wired.
6927 for (llvm::SmallPtrSet<Decl *, 4>::iterator D = PendingDefinitions.begin(),
6928 DEnd = PendingDefinitions.end();
6929 D != DEnd; ++D) {
6930 if (TagDecl *TD = dyn_cast<TagDecl>(*D)) {
6931 if (const TagType *TagT = dyn_cast<TagType>(TD->TypeForDecl)) {
6932 // Make sure that the TagType points at the definition.
6933 const_cast<TagType*>(TagT)->decl = TD;
6934 }
6935
6936 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(*D)) {
6937 for (CXXRecordDecl::redecl_iterator R = RD->redecls_begin(),
6938 REnd = RD->redecls_end();
6939 R != REnd; ++R)
6940 cast<CXXRecordDecl>(*R)->DefinitionData = RD->DefinitionData;
6941
6942 }
6943
6944 continue;
6945 }
6946
6947 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(*D)) {
6948 // Make sure that the ObjCInterfaceType points at the definition.
6949 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
6950 ->Decl = ID;
6951
6952 for (ObjCInterfaceDecl::redecl_iterator R = ID->redecls_begin(),
6953 REnd = ID->redecls_end();
6954 R != REnd; ++R)
6955 R->Data = ID->Data;
6956
6957 continue;
6958 }
6959
6960 if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(*D)) {
6961 for (ObjCProtocolDecl::redecl_iterator R = PD->redecls_begin(),
6962 REnd = PD->redecls_end();
6963 R != REnd; ++R)
6964 R->Data = PD->Data;
6965
6966 continue;
6967 }
6968
6969 RedeclarableTemplateDecl *RTD
6970 = cast<RedeclarableTemplateDecl>(*D)->getCanonicalDecl();
6971 for (RedeclarableTemplateDecl::redecl_iterator R = RTD->redecls_begin(),
6972 REnd = RTD->redecls_end();
6973 R != REnd; ++R)
6974 R->Common = RTD->Common;
6975 }
6976 PendingDefinitions.clear();
6977
6978 // Load the bodies of any functions or methods we've encountered. We do
6979 // this now (delayed) so that we can be sure that the declaration chains
6980 // have been fully wired up.
6981 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
6982 PBEnd = PendingBodies.end();
6983 PB != PBEnd; ++PB) {
6984 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
6985 // FIXME: Check for =delete/=default?
6986 // FIXME: Complain about ODR violations here?
6987 if (!getContext().getLangOpts().Modules || !FD->hasBody())
6988 FD->setLazyBody(PB->second);
6989 continue;
6990 }
6991
6992 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
6993 if (!getContext().getLangOpts().Modules || !MD->hasBody())
6994 MD->setLazyBody(PB->second);
6995 }
6996 PendingBodies.clear();
6997}
6998
6999void ASTReader::FinishedDeserializing() {
7000 assert(NumCurrentElementsDeserializing &&
7001 "FinishedDeserializing not paired with StartedDeserializing");
7002 if (NumCurrentElementsDeserializing == 1) {
7003 // We decrease NumCurrentElementsDeserializing only after pending actions
7004 // are finished, to avoid recursively re-calling finishPendingActions().
7005 finishPendingActions();
7006 }
7007 --NumCurrentElementsDeserializing;
7008
7009 if (NumCurrentElementsDeserializing == 0 &&
7010 Consumer && !PassingDeclsToConsumer) {
7011 // Guard variable to avoid recursively redoing the process of passing
7012 // decls to consumer.
7013 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
7014 true);
7015
7016 while (!InterestingDecls.empty()) {
7017 // We are not in recursive loading, so it's safe to pass the "interesting"
7018 // decls to the consumer.
7019 Decl *D = InterestingDecls.front();
7020 InterestingDecls.pop_front();
7021 PassInterestingDeclToConsumer(D);
7022 }
7023 }
7024}
7025
7026ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context,
7027 StringRef isysroot, bool DisableValidation,
Douglas Gregorf575d6e2013-01-25 00:45:27 +00007028 bool AllowASTWithCompilerErrors, bool UseGlobalIndex)
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007029 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
7030 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
7031 Diags(PP.getDiagnostics()), SemaObj(0), PP(PP), Context(Context),
7032 Consumer(0), ModuleMgr(PP.getFileManager()),
7033 isysroot(isysroot), DisableValidation(DisableValidation),
Douglas Gregore1698072013-01-25 00:38:33 +00007034 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
Douglas Gregorf575d6e2013-01-25 00:45:27 +00007035 UseGlobalIndex(UseGlobalIndex), TriedLoadingGlobalIndex(false),
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007036 CurrentGeneration(0), CurrSwitchCaseStmts(&SwitchCaseStmts),
7037 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Douglas Gregore1698072013-01-25 00:38:33 +00007038 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
7039 TotalNumMacros(0), NumIdentifierLookups(0), NumIdentifierLookupHits(0),
7040 NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007041 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
7042 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
7043 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
7044 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
7045 PassingDeclsToConsumer(false),
7046 NumCXXBaseSpecifiersLoaded(0)
7047{
7048 SourceMgr.setExternalSLocEntrySource(this);
7049}
7050
7051ASTReader::~ASTReader() {
7052 for (DeclContextVisibleUpdatesPending::iterator
7053 I = PendingVisibleUpdates.begin(),
7054 E = PendingVisibleUpdates.end();
7055 I != E; ++I) {
7056 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
7057 F = I->second.end();
7058 J != F; ++J)
7059 delete J->first;
7060 }
7061}