blob: 38ff3002bdb52863e9b3a68fbc09c2699ec72e4e [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 Gregor1a49d972013-01-25 01:03:03 +00001381 GlobalModuleIndex::SkipSet &SkipSet;
Douglas Gregore1698072013-01-25 00:38:33 +00001382 unsigned &NumIdentifierLookups;
1383 unsigned &NumIdentifierLookupHits;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001384 IdentifierInfo *Found;
Douglas Gregore1698072013-01-25 00:38:33 +00001385
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001386 public:
Douglas Gregore1698072013-01-25 00:38:33 +00001387 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
Douglas Gregor1a49d972013-01-25 01:03:03 +00001388 GlobalModuleIndex::SkipSet &SkipSet,
Douglas Gregore1698072013-01-25 00:38:33 +00001389 unsigned &NumIdentifierLookups,
1390 unsigned &NumIdentifierLookupHits)
Douglas Gregor1a49d972013-01-25 01:03:03 +00001391 : Name(Name), PriorGeneration(PriorGeneration), SkipSet(SkipSet),
Douglas Gregore1698072013-01-25 00:38:33 +00001392 NumIdentifierLookups(NumIdentifierLookups),
1393 NumIdentifierLookupHits(NumIdentifierLookupHits),
1394 Found()
1395 {
1396 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001397
1398 static bool visit(ModuleFile &M, void *UserData) {
1399 IdentifierLookupVisitor *This
1400 = static_cast<IdentifierLookupVisitor *>(UserData);
1401
1402 // If we've already searched this module file, skip it now.
1403 if (M.Generation <= This->PriorGeneration)
1404 return true;
Douglas Gregor1a49d972013-01-25 01:03:03 +00001405
1406 // If this module file is in the skip set, don't bother looking in it.
1407 if (This->SkipSet.count(M.File))
1408 return false;
1409
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001410 ASTIdentifierLookupTable *IdTable
1411 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
1412 if (!IdTable)
1413 return false;
1414
1415 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(),
1416 M, This->Found);
Douglas Gregore1698072013-01-25 00:38:33 +00001417 ++This->NumIdentifierLookups;
1418 ASTIdentifierLookupTable::iterator Pos = IdTable->find(This->Name,&Trait);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001419 if (Pos == IdTable->end())
1420 return false;
1421
1422 // Dereferencing the iterator has the effect of building the
1423 // IdentifierInfo node and populating it with the various
1424 // declarations it needs.
Douglas Gregore1698072013-01-25 00:38:33 +00001425 ++This->NumIdentifierLookupHits;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001426 This->Found = *Pos;
1427 return true;
1428 }
1429
1430 // \brief Retrieve the identifier info found within the module
1431 // files.
1432 IdentifierInfo *getIdentifierInfo() const { return Found; }
1433 };
1434}
1435
1436void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
1437 // Note that we are loading an identifier.
1438 Deserializing AnIdentifier(this);
1439
1440 unsigned PriorGeneration = 0;
1441 if (getContext().getLangOpts().Modules)
1442 PriorGeneration = IdentifierGeneration[&II];
Douglas Gregor1a49d972013-01-25 01:03:03 +00001443
1444 // If there is a global index, look there first to determine which modules
1445 // provably do not have any results for this identifier.
1446 GlobalModuleIndex::SkipSet SkipSet;
1447 if (!loadGlobalIndex()) {
1448 SmallVector<const FileEntry *, 4> ModuleFiles;
1449 if (GlobalIndex->lookupIdentifier(II.getName(), ModuleFiles)) {
1450 SkipSet = GlobalIndex->computeSkipSet(ModuleFiles);
1451 }
1452 }
1453
1454 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration, SkipSet,
Douglas Gregore1698072013-01-25 00:38:33 +00001455 NumIdentifierLookups,
1456 NumIdentifierLookupHits);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001457 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
1458 markIdentifierUpToDate(&II);
1459}
1460
1461void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
1462 if (!II)
1463 return;
1464
1465 II->setOutOfDate(false);
1466
1467 // Update the generation for this identifier.
1468 if (getContext().getLangOpts().Modules)
1469 IdentifierGeneration[II] = CurrentGeneration;
1470}
1471
1472llvm::PointerIntPair<const FileEntry *, 1, bool>
1473ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
1474 // If this ID is bogus, just return an empty input file.
1475 if (ID == 0 || ID > F.InputFilesLoaded.size())
1476 return InputFile();
1477
1478 // If we've already loaded this input file, return it.
1479 if (F.InputFilesLoaded[ID-1].getPointer())
1480 return F.InputFilesLoaded[ID-1];
1481
1482 // Go find this input file.
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001483 BitstreamCursor &Cursor = F.InputFilesCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001484 SavedStreamPosition SavedPosition(Cursor);
1485 Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
1486
1487 unsigned Code = Cursor.ReadCode();
1488 RecordData Record;
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001489 StringRef Blob;
1490 switch ((InputFileRecordTypes)Cursor.readRecord(Code, Record, &Blob)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001491 case INPUT_FILE: {
1492 unsigned StoredID = Record[0];
1493 assert(ID == StoredID && "Bogus stored ID or offset");
1494 (void)StoredID;
1495 off_t StoredSize = (off_t)Record[1];
1496 time_t StoredTime = (time_t)Record[2];
1497 bool Overridden = (bool)Record[3];
1498
1499 // Get the file entry for this input file.
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001500 StringRef OrigFilename = Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001501 std::string Filename = OrigFilename;
1502 MaybeAddSystemRootToFilename(F, Filename);
1503 const FileEntry *File
1504 = Overridden? FileMgr.getVirtualFile(Filename, StoredSize, StoredTime)
1505 : FileMgr.getFile(Filename, /*OpenFile=*/false);
1506
1507 // If we didn't find the file, resolve it relative to the
1508 // original directory from which this AST file was created.
1509 if (File == 0 && !F.OriginalDir.empty() && !CurrentDir.empty() &&
1510 F.OriginalDir != CurrentDir) {
1511 std::string Resolved = resolveFileRelativeToOriginalDir(Filename,
1512 F.OriginalDir,
1513 CurrentDir);
1514 if (!Resolved.empty())
1515 File = FileMgr.getFile(Resolved);
1516 }
1517
1518 // For an overridden file, create a virtual file with the stored
1519 // size/timestamp.
1520 if (Overridden && File == 0) {
1521 File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
1522 }
1523
1524 if (File == 0) {
1525 if (Complain) {
1526 std::string ErrorStr = "could not find file '";
1527 ErrorStr += Filename;
1528 ErrorStr += "' referenced by AST file";
1529 Error(ErrorStr.c_str());
1530 }
1531 return InputFile();
1532 }
1533
1534 // Note that we've loaded this input file.
1535 F.InputFilesLoaded[ID-1] = InputFile(File, Overridden);
1536
1537 // Check if there was a request to override the contents of the file
1538 // that was part of the precompiled header. Overridding such a file
1539 // can lead to problems when lexing using the source locations from the
1540 // PCH.
1541 SourceManager &SM = getSourceManager();
1542 if (!Overridden && SM.isFileOverridden(File)) {
1543 Error(diag::err_fe_pch_file_overridden, Filename);
1544 // After emitting the diagnostic, recover by disabling the override so
1545 // that the original file will be used.
1546 SM.disableFileContentsOverride(File);
1547 // The FileEntry is a virtual file entry with the size of the contents
1548 // that would override the original contents. Set it to the original's
1549 // size/time.
1550 FileMgr.modifyFileEntry(const_cast<FileEntry*>(File),
1551 StoredSize, StoredTime);
1552 }
1553
1554 // For an overridden file, there is nothing to validate.
1555 if (Overridden)
1556 return InputFile(File, Overridden);
1557
1558 if ((StoredSize != File->getSize()
1559#if !defined(LLVM_ON_WIN32)
1560 // In our regression testing, the Windows file system seems to
1561 // have inconsistent modification times that sometimes
1562 // erroneously trigger this error-handling path.
1563 || StoredTime != File->getModificationTime()
1564#endif
1565 )) {
1566 if (Complain)
1567 Error(diag::err_fe_pch_file_modified, Filename);
1568
1569 return InputFile();
1570 }
1571
1572 return InputFile(File, Overridden);
1573 }
1574 }
1575
1576 return InputFile();
1577}
1578
1579const FileEntry *ASTReader::getFileEntry(StringRef filenameStrRef) {
1580 ModuleFile &M = ModuleMgr.getPrimaryModule();
1581 std::string Filename = filenameStrRef;
1582 MaybeAddSystemRootToFilename(M, Filename);
1583 const FileEntry *File = FileMgr.getFile(Filename);
1584 if (File == 0 && !M.OriginalDir.empty() && !CurrentDir.empty() &&
1585 M.OriginalDir != CurrentDir) {
1586 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1587 M.OriginalDir,
1588 CurrentDir);
1589 if (!resolved.empty())
1590 File = FileMgr.getFile(resolved);
1591 }
1592
1593 return File;
1594}
1595
1596/// \brief If we are loading a relocatable PCH file, and the filename is
1597/// not an absolute path, add the system root to the beginning of the file
1598/// name.
1599void ASTReader::MaybeAddSystemRootToFilename(ModuleFile &M,
1600 std::string &Filename) {
1601 // If this is not a relocatable PCH file, there's nothing to do.
1602 if (!M.RelocatablePCH)
1603 return;
1604
1605 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
1606 return;
1607
1608 if (isysroot.empty()) {
1609 // If no system root was given, default to '/'
1610 Filename.insert(Filename.begin(), '/');
1611 return;
1612 }
1613
1614 unsigned Length = isysroot.size();
1615 if (isysroot[Length - 1] != '/')
1616 Filename.insert(Filename.begin(), '/');
1617
1618 Filename.insert(Filename.begin(), isysroot.begin(), isysroot.end());
1619}
1620
1621ASTReader::ASTReadResult
1622ASTReader::ReadControlBlock(ModuleFile &F,
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001623 SmallVectorImpl<ImportedModule> &Loaded,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001624 unsigned ClientLoadCapabilities) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001625 BitstreamCursor &Stream = F.Stream;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001626
1627 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
1628 Error("malformed block record in AST file");
1629 return Failure;
1630 }
1631
1632 // Read all of the records and blocks in the control block.
1633 RecordData Record;
Chris Lattner88bde502013-01-19 21:39:22 +00001634 while (1) {
1635 llvm::BitstreamEntry Entry = Stream.advance();
1636
1637 switch (Entry.Kind) {
1638 case llvm::BitstreamEntry::Error:
1639 Error("malformed block record in AST file");
1640 return Failure;
1641 case llvm::BitstreamEntry::EndBlock:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001642 // Validate all of the input files.
1643 if (!DisableValidation) {
1644 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
1645 for (unsigned I = 0, N = Record[0]; I < N; ++I)
1646 if (!getInputFile(F, I+1, Complain).getPointer())
1647 return OutOfDate;
1648 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001649 return Success;
Chris Lattner88bde502013-01-19 21:39:22 +00001650
1651 case llvm::BitstreamEntry::SubBlock:
1652 switch (Entry.ID) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001653 case INPUT_FILES_BLOCK_ID:
1654 F.InputFilesCursor = Stream;
1655 if (Stream.SkipBlock() || // Skip with the main cursor
1656 // Read the abbreviations
1657 ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
1658 Error("malformed block record in AST file");
1659 return Failure;
1660 }
1661 continue;
Chris Lattner88bde502013-01-19 21:39:22 +00001662
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001663 default:
Chris Lattner88bde502013-01-19 21:39:22 +00001664 if (Stream.SkipBlock()) {
1665 Error("malformed block record in AST file");
1666 return Failure;
1667 }
1668 continue;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001669 }
Chris Lattner88bde502013-01-19 21:39:22 +00001670
1671 case llvm::BitstreamEntry::Record:
1672 // The interesting case.
1673 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001674 }
1675
1676 // Read and process a record.
1677 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001678 StringRef Blob;
1679 switch ((ControlRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001680 case METADATA: {
1681 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1682 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
1683 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
1684 : diag::warn_pch_version_too_new);
1685 return VersionMismatch;
1686 }
1687
1688 bool hasErrors = Record[5];
1689 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
1690 Diag(diag::err_pch_with_compiler_errors);
1691 return HadErrors;
1692 }
1693
1694 F.RelocatablePCH = Record[4];
1695
1696 const std::string &CurBranch = getClangFullRepositoryVersion();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001697 StringRef ASTBranch = Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001698 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
1699 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
1700 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
1701 return VersionMismatch;
1702 }
1703 break;
1704 }
1705
1706 case IMPORTS: {
1707 // Load each of the imported PCH files.
1708 unsigned Idx = 0, N = Record.size();
1709 while (Idx < N) {
1710 // Read information about the AST file.
1711 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
1712 // The import location will be the local one for now; we will adjust
1713 // all import locations of module imports after the global source
1714 // location info are setup.
1715 SourceLocation ImportLoc =
1716 SourceLocation::getFromRawEncoding(Record[Idx++]);
1717 unsigned Length = Record[Idx++];
1718 SmallString<128> ImportedFile(Record.begin() + Idx,
1719 Record.begin() + Idx + Length);
1720 Idx += Length;
1721
1722 // Load the AST file.
1723 switch(ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F, Loaded,
1724 ClientLoadCapabilities)) {
1725 case Failure: return Failure;
1726 // If we have to ignore the dependency, we'll have to ignore this too.
1727 case OutOfDate: return OutOfDate;
1728 case VersionMismatch: return VersionMismatch;
1729 case ConfigurationMismatch: return ConfigurationMismatch;
1730 case HadErrors: return HadErrors;
1731 case Success: break;
1732 }
1733 }
1734 break;
1735 }
1736
1737 case LANGUAGE_OPTIONS: {
1738 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
1739 if (Listener && &F == *ModuleMgr.begin() &&
1740 ParseLanguageOptions(Record, Complain, *Listener) &&
1741 !DisableValidation)
1742 return ConfigurationMismatch;
1743 break;
1744 }
1745
1746 case TARGET_OPTIONS: {
1747 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1748 if (Listener && &F == *ModuleMgr.begin() &&
1749 ParseTargetOptions(Record, Complain, *Listener) &&
1750 !DisableValidation)
1751 return ConfigurationMismatch;
1752 break;
1753 }
1754
1755 case DIAGNOSTIC_OPTIONS: {
1756 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1757 if (Listener && &F == *ModuleMgr.begin() &&
1758 ParseDiagnosticOptions(Record, Complain, *Listener) &&
1759 !DisableValidation)
1760 return ConfigurationMismatch;
1761 break;
1762 }
1763
1764 case FILE_SYSTEM_OPTIONS: {
1765 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1766 if (Listener && &F == *ModuleMgr.begin() &&
1767 ParseFileSystemOptions(Record, Complain, *Listener) &&
1768 !DisableValidation)
1769 return ConfigurationMismatch;
1770 break;
1771 }
1772
1773 case HEADER_SEARCH_OPTIONS: {
1774 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1775 if (Listener && &F == *ModuleMgr.begin() &&
1776 ParseHeaderSearchOptions(Record, Complain, *Listener) &&
1777 !DisableValidation)
1778 return ConfigurationMismatch;
1779 break;
1780 }
1781
1782 case PREPROCESSOR_OPTIONS: {
1783 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1784 if (Listener && &F == *ModuleMgr.begin() &&
1785 ParsePreprocessorOptions(Record, Complain, *Listener,
1786 SuggestedPredefines) &&
1787 !DisableValidation)
1788 return ConfigurationMismatch;
1789 break;
1790 }
1791
1792 case ORIGINAL_FILE:
1793 F.OriginalSourceFileID = FileID::get(Record[0]);
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001794 F.ActualOriginalSourceFileName = Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001795 F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
1796 MaybeAddSystemRootToFilename(F, F.OriginalSourceFileName);
1797 break;
1798
1799 case ORIGINAL_FILE_ID:
1800 F.OriginalSourceFileID = FileID::get(Record[0]);
1801 break;
1802
1803 case ORIGINAL_PCH_DIR:
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001804 F.OriginalDir = Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001805 break;
1806
1807 case INPUT_FILE_OFFSETS:
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001808 F.InputFileOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001809 F.InputFilesLoaded.resize(Record[0]);
1810 break;
1811 }
1812 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001813}
1814
1815bool ASTReader::ReadASTBlock(ModuleFile &F) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001816 BitstreamCursor &Stream = F.Stream;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001817
1818 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
1819 Error("malformed block record in AST file");
1820 return true;
1821 }
1822
1823 // Read all of the records and blocks for the AST file.
1824 RecordData Record;
Chris Lattner88bde502013-01-19 21:39:22 +00001825 while (1) {
1826 llvm::BitstreamEntry Entry = Stream.advance();
1827
1828 switch (Entry.Kind) {
1829 case llvm::BitstreamEntry::Error:
1830 Error("error at end of module block in AST file");
1831 return true;
1832 case llvm::BitstreamEntry::EndBlock: {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001833 DeclContext *DC = Context.getTranslationUnitDecl();
1834 if (!DC->hasExternalVisibleStorage() && DC->hasExternalLexicalStorage())
1835 DC->setMustBuildLookupTable();
Chris Lattner88bde502013-01-19 21:39:22 +00001836
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001837 return false;
1838 }
Chris Lattner88bde502013-01-19 21:39:22 +00001839 case llvm::BitstreamEntry::SubBlock:
1840 switch (Entry.ID) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001841 case DECLTYPES_BLOCK_ID:
1842 // We lazily load the decls block, but we want to set up the
1843 // DeclsCursor cursor to point into it. Clone our current bitcode
1844 // cursor to it, enter the block and read the abbrevs in that block.
1845 // With the main cursor, we just skip over it.
1846 F.DeclsCursor = Stream;
1847 if (Stream.SkipBlock() || // Skip with the main cursor.
1848 // Read the abbrevs.
1849 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
1850 Error("malformed block record in AST file");
1851 return true;
1852 }
1853 break;
Chris Lattner88bde502013-01-19 21:39:22 +00001854
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001855 case DECL_UPDATES_BLOCK_ID:
1856 if (Stream.SkipBlock()) {
1857 Error("malformed block record in AST file");
1858 return true;
1859 }
1860 break;
Chris Lattner88bde502013-01-19 21:39:22 +00001861
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001862 case PREPROCESSOR_BLOCK_ID:
1863 F.MacroCursor = Stream;
1864 if (!PP.getExternalSource())
1865 PP.setExternalSource(this);
Chris Lattner88bde502013-01-19 21:39:22 +00001866
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001867 if (Stream.SkipBlock() ||
1868 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
1869 Error("malformed block record in AST file");
1870 return true;
1871 }
1872 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
1873 break;
Chris Lattner88bde502013-01-19 21:39:22 +00001874
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001875 case PREPROCESSOR_DETAIL_BLOCK_ID:
1876 F.PreprocessorDetailCursor = Stream;
1877 if (Stream.SkipBlock() ||
Chris Lattner88bde502013-01-19 21:39:22 +00001878 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001879 PREPROCESSOR_DETAIL_BLOCK_ID)) {
Chris Lattner88bde502013-01-19 21:39:22 +00001880 Error("malformed preprocessor detail record in AST file");
1881 return true;
1882 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001883 F.PreprocessorDetailStartOffset
Chris Lattner88bde502013-01-19 21:39:22 +00001884 = F.PreprocessorDetailCursor.GetCurrentBitNo();
1885
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001886 if (!PP.getPreprocessingRecord())
1887 PP.createPreprocessingRecord();
1888 if (!PP.getPreprocessingRecord()->getExternalSource())
1889 PP.getPreprocessingRecord()->SetExternalSource(*this);
1890 break;
1891
1892 case SOURCE_MANAGER_BLOCK_ID:
1893 if (ReadSourceManagerBlock(F))
1894 return true;
1895 break;
Chris Lattner88bde502013-01-19 21:39:22 +00001896
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001897 case SUBMODULE_BLOCK_ID:
1898 if (ReadSubmoduleBlock(F))
1899 return true;
1900 break;
Chris Lattner88bde502013-01-19 21:39:22 +00001901
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001902 case COMMENTS_BLOCK_ID: {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001903 BitstreamCursor C = Stream;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001904 if (Stream.SkipBlock() ||
1905 ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
1906 Error("malformed comments block in AST file");
1907 return true;
1908 }
1909 CommentsCursors.push_back(std::make_pair(C, &F));
1910 break;
1911 }
Chris Lattner88bde502013-01-19 21:39:22 +00001912
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001913 default:
Chris Lattner88bde502013-01-19 21:39:22 +00001914 if (Stream.SkipBlock()) {
1915 Error("malformed block record in AST file");
1916 return true;
1917 }
1918 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001919 }
1920 continue;
Chris Lattner88bde502013-01-19 21:39:22 +00001921
1922 case llvm::BitstreamEntry::Record:
1923 // The interesting case.
1924 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001925 }
1926
1927 // Read and process a record.
1928 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001929 StringRef Blob;
1930 switch ((ASTRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001931 default: // Default behavior: ignore.
1932 break;
1933
1934 case TYPE_OFFSET: {
1935 if (F.LocalNumTypes != 0) {
1936 Error("duplicate TYPE_OFFSET record in AST file");
1937 return true;
1938 }
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001939 F.TypeOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001940 F.LocalNumTypes = Record[0];
1941 unsigned LocalBaseTypeIndex = Record[1];
1942 F.BaseTypeIndex = getTotalNumTypes();
1943
1944 if (F.LocalNumTypes > 0) {
1945 // Introduce the global -> local mapping for types within this module.
1946 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
1947
1948 // Introduce the local -> global mapping for types within this module.
1949 F.TypeRemap.insertOrReplace(
1950 std::make_pair(LocalBaseTypeIndex,
1951 F.BaseTypeIndex - LocalBaseTypeIndex));
1952
1953 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
1954 }
1955 break;
1956 }
1957
1958 case DECL_OFFSET: {
1959 if (F.LocalNumDecls != 0) {
1960 Error("duplicate DECL_OFFSET record in AST file");
1961 return true;
1962 }
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001963 F.DeclOffsets = (const DeclOffset *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001964 F.LocalNumDecls = Record[0];
1965 unsigned LocalBaseDeclID = Record[1];
1966 F.BaseDeclID = getTotalNumDecls();
1967
1968 if (F.LocalNumDecls > 0) {
1969 // Introduce the global -> local mapping for declarations within this
1970 // module.
1971 GlobalDeclMap.insert(
1972 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
1973
1974 // Introduce the local -> global mapping for declarations within this
1975 // module.
1976 F.DeclRemap.insertOrReplace(
1977 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
1978
1979 // Introduce the global -> local mapping for declarations within this
1980 // module.
1981 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
1982
1983 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
1984 }
1985 break;
1986 }
1987
1988 case TU_UPDATE_LEXICAL: {
1989 DeclContext *TU = Context.getTranslationUnitDecl();
1990 DeclContextInfo &Info = F.DeclContextInfos[TU];
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001991 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(Blob.data());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001992 Info.NumLexicalDecls
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001993 = static_cast<unsigned int>(Blob.size() / sizeof(KindDeclIDPair));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001994 TU->setHasExternalLexicalStorage(true);
1995 break;
1996 }
1997
1998 case UPDATE_VISIBLE: {
1999 unsigned Idx = 0;
2000 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
2001 ASTDeclContextNameLookupTable *Table =
2002 ASTDeclContextNameLookupTable::Create(
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002003 (const unsigned char *)Blob.data() + Record[Idx++],
2004 (const unsigned char *)Blob.data(),
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002005 ASTDeclContextNameLookupTrait(*this, F));
2006 if (ID == PREDEF_DECL_TRANSLATION_UNIT_ID) { // Is it the TU?
2007 DeclContext *TU = Context.getTranslationUnitDecl();
2008 F.DeclContextInfos[TU].NameLookupTableData = Table;
2009 TU->setHasExternalVisibleStorage(true);
2010 } else
2011 PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F));
2012 break;
2013 }
2014
2015 case IDENTIFIER_TABLE:
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002016 F.IdentifierTableData = Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002017 if (Record[0]) {
2018 F.IdentifierLookupTable
2019 = ASTIdentifierLookupTable::Create(
2020 (const unsigned char *)F.IdentifierTableData + Record[0],
2021 (const unsigned char *)F.IdentifierTableData,
2022 ASTIdentifierLookupTrait(*this, F));
2023
2024 PP.getIdentifierTable().setExternalIdentifierLookup(this);
2025 }
2026 break;
2027
2028 case IDENTIFIER_OFFSET: {
2029 if (F.LocalNumIdentifiers != 0) {
2030 Error("duplicate IDENTIFIER_OFFSET record in AST file");
2031 return true;
2032 }
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002033 F.IdentifierOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002034 F.LocalNumIdentifiers = Record[0];
2035 unsigned LocalBaseIdentifierID = Record[1];
2036 F.BaseIdentifierID = getTotalNumIdentifiers();
2037
2038 if (F.LocalNumIdentifiers > 0) {
2039 // Introduce the global -> local mapping for identifiers within this
2040 // module.
2041 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
2042 &F));
2043
2044 // Introduce the local -> global mapping for identifiers within this
2045 // module.
2046 F.IdentifierRemap.insertOrReplace(
2047 std::make_pair(LocalBaseIdentifierID,
2048 F.BaseIdentifierID - LocalBaseIdentifierID));
2049
2050 IdentifiersLoaded.resize(IdentifiersLoaded.size()
2051 + F.LocalNumIdentifiers);
2052 }
2053 break;
2054 }
2055
2056 case EXTERNAL_DEFINITIONS:
2057 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2058 ExternalDefinitions.push_back(getGlobalDeclID(F, Record[I]));
2059 break;
2060
2061 case SPECIAL_TYPES:
2062 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2063 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
2064 break;
2065
2066 case STATISTICS:
2067 TotalNumStatements += Record[0];
2068 TotalNumMacros += Record[1];
2069 TotalLexicalDeclContexts += Record[2];
2070 TotalVisibleDeclContexts += Record[3];
2071 break;
2072
2073 case UNUSED_FILESCOPED_DECLS:
2074 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2075 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
2076 break;
2077
2078 case DELEGATING_CTORS:
2079 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2080 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
2081 break;
2082
2083 case WEAK_UNDECLARED_IDENTIFIERS:
2084 if (Record.size() % 4 != 0) {
2085 Error("invalid weak identifiers record");
2086 return true;
2087 }
2088
2089 // FIXME: Ignore weak undeclared identifiers from non-original PCH
2090 // files. This isn't the way to do it :)
2091 WeakUndeclaredIdentifiers.clear();
2092
2093 // Translate the weak, undeclared identifiers into global IDs.
2094 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
2095 WeakUndeclaredIdentifiers.push_back(
2096 getGlobalIdentifierID(F, Record[I++]));
2097 WeakUndeclaredIdentifiers.push_back(
2098 getGlobalIdentifierID(F, Record[I++]));
2099 WeakUndeclaredIdentifiers.push_back(
2100 ReadSourceLocation(F, Record, I).getRawEncoding());
2101 WeakUndeclaredIdentifiers.push_back(Record[I++]);
2102 }
2103 break;
2104
Richard Smith5ea6ef42013-01-10 23:43:47 +00002105 case LOCALLY_SCOPED_EXTERN_C_DECLS:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002106 for (unsigned I = 0, N = Record.size(); I != N; ++I)
Richard Smith5ea6ef42013-01-10 23:43:47 +00002107 LocallyScopedExternCDecls.push_back(getGlobalDeclID(F, Record[I]));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002108 break;
2109
2110 case SELECTOR_OFFSETS: {
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002111 F.SelectorOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002112 F.LocalNumSelectors = Record[0];
2113 unsigned LocalBaseSelectorID = Record[1];
2114 F.BaseSelectorID = getTotalNumSelectors();
2115
2116 if (F.LocalNumSelectors > 0) {
2117 // Introduce the global -> local mapping for selectors within this
2118 // module.
2119 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
2120
2121 // Introduce the local -> global mapping for selectors within this
2122 // module.
2123 F.SelectorRemap.insertOrReplace(
2124 std::make_pair(LocalBaseSelectorID,
2125 F.BaseSelectorID - LocalBaseSelectorID));
2126
2127 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
2128 }
2129 break;
2130 }
2131
2132 case METHOD_POOL:
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002133 F.SelectorLookupTableData = (const unsigned char *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002134 if (Record[0])
2135 F.SelectorLookupTable
2136 = ASTSelectorLookupTable::Create(
2137 F.SelectorLookupTableData + Record[0],
2138 F.SelectorLookupTableData,
2139 ASTSelectorLookupTrait(*this, F));
2140 TotalNumMethodPoolEntries += Record[1];
2141 break;
2142
2143 case REFERENCED_SELECTOR_POOL:
2144 if (!Record.empty()) {
2145 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
2146 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
2147 Record[Idx++]));
2148 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
2149 getRawEncoding());
2150 }
2151 }
2152 break;
2153
2154 case PP_COUNTER_VALUE:
2155 if (!Record.empty() && Listener)
2156 Listener->ReadCounter(F, Record[0]);
2157 break;
2158
2159 case FILE_SORTED_DECLS:
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002160 F.FileSortedDecls = (const DeclID *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002161 F.NumFileSortedDecls = Record[0];
2162 break;
2163
2164 case SOURCE_LOCATION_OFFSETS: {
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002165 F.SLocEntryOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002166 F.LocalNumSLocEntries = Record[0];
2167 unsigned SLocSpaceSize = Record[1];
2168 llvm::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
2169 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
2170 SLocSpaceSize);
2171 // Make our entry in the range map. BaseID is negative and growing, so
2172 // we invert it. Because we invert it, though, we need the other end of
2173 // the range.
2174 unsigned RangeStart =
2175 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
2176 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
2177 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
2178
2179 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
2180 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
2181 GlobalSLocOffsetMap.insert(
2182 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
2183 - SLocSpaceSize,&F));
2184
2185 // Initialize the remapping table.
2186 // Invalid stays invalid.
2187 F.SLocRemap.insert(std::make_pair(0U, 0));
2188 // This module. Base was 2 when being compiled.
2189 F.SLocRemap.insert(std::make_pair(2U,
2190 static_cast<int>(F.SLocEntryBaseOffset - 2)));
2191
2192 TotalNumSLocEntries += F.LocalNumSLocEntries;
2193 break;
2194 }
2195
2196 case MODULE_OFFSET_MAP: {
2197 // Additional remapping information.
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002198 const unsigned char *Data = (const unsigned char*)Blob.data();
2199 const unsigned char *DataEnd = Data + Blob.size();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002200
2201 // Continuous range maps we may be updating in our module.
2202 ContinuousRangeMap<uint32_t, int, 2>::Builder SLocRemap(F.SLocRemap);
2203 ContinuousRangeMap<uint32_t, int, 2>::Builder
2204 IdentifierRemap(F.IdentifierRemap);
2205 ContinuousRangeMap<uint32_t, int, 2>::Builder
2206 MacroRemap(F.MacroRemap);
2207 ContinuousRangeMap<uint32_t, int, 2>::Builder
2208 PreprocessedEntityRemap(F.PreprocessedEntityRemap);
2209 ContinuousRangeMap<uint32_t, int, 2>::Builder
2210 SubmoduleRemap(F.SubmoduleRemap);
2211 ContinuousRangeMap<uint32_t, int, 2>::Builder
2212 SelectorRemap(F.SelectorRemap);
2213 ContinuousRangeMap<uint32_t, int, 2>::Builder DeclRemap(F.DeclRemap);
2214 ContinuousRangeMap<uint32_t, int, 2>::Builder TypeRemap(F.TypeRemap);
2215
2216 while(Data < DataEnd) {
2217 uint16_t Len = io::ReadUnalignedLE16(Data);
2218 StringRef Name = StringRef((const char*)Data, Len);
2219 Data += Len;
2220 ModuleFile *OM = ModuleMgr.lookup(Name);
2221 if (!OM) {
2222 Error("SourceLocation remap refers to unknown module");
2223 return true;
2224 }
2225
2226 uint32_t SLocOffset = io::ReadUnalignedLE32(Data);
2227 uint32_t IdentifierIDOffset = io::ReadUnalignedLE32(Data);
2228 uint32_t MacroIDOffset = io::ReadUnalignedLE32(Data);
2229 uint32_t PreprocessedEntityIDOffset = io::ReadUnalignedLE32(Data);
2230 uint32_t SubmoduleIDOffset = io::ReadUnalignedLE32(Data);
2231 uint32_t SelectorIDOffset = io::ReadUnalignedLE32(Data);
2232 uint32_t DeclIDOffset = io::ReadUnalignedLE32(Data);
2233 uint32_t TypeIndexOffset = io::ReadUnalignedLE32(Data);
2234
2235 // Source location offset is mapped to OM->SLocEntryBaseOffset.
2236 SLocRemap.insert(std::make_pair(SLocOffset,
2237 static_cast<int>(OM->SLocEntryBaseOffset - SLocOffset)));
2238 IdentifierRemap.insert(
2239 std::make_pair(IdentifierIDOffset,
2240 OM->BaseIdentifierID - IdentifierIDOffset));
2241 MacroRemap.insert(std::make_pair(MacroIDOffset,
2242 OM->BaseMacroID - MacroIDOffset));
2243 PreprocessedEntityRemap.insert(
2244 std::make_pair(PreprocessedEntityIDOffset,
2245 OM->BasePreprocessedEntityID - PreprocessedEntityIDOffset));
2246 SubmoduleRemap.insert(std::make_pair(SubmoduleIDOffset,
2247 OM->BaseSubmoduleID - SubmoduleIDOffset));
2248 SelectorRemap.insert(std::make_pair(SelectorIDOffset,
2249 OM->BaseSelectorID - SelectorIDOffset));
2250 DeclRemap.insert(std::make_pair(DeclIDOffset,
2251 OM->BaseDeclID - DeclIDOffset));
2252
2253 TypeRemap.insert(std::make_pair(TypeIndexOffset,
2254 OM->BaseTypeIndex - TypeIndexOffset));
2255
2256 // Global -> local mappings.
2257 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
2258 }
2259 break;
2260 }
2261
2262 case SOURCE_MANAGER_LINE_TABLE:
2263 if (ParseLineTable(F, Record))
2264 return true;
2265 break;
2266
2267 case SOURCE_LOCATION_PRELOADS: {
2268 // Need to transform from the local view (1-based IDs) to the global view,
2269 // which is based off F.SLocEntryBaseID.
2270 if (!F.PreloadSLocEntries.empty()) {
2271 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
2272 return true;
2273 }
2274
2275 F.PreloadSLocEntries.swap(Record);
2276 break;
2277 }
2278
2279 case EXT_VECTOR_DECLS:
2280 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2281 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
2282 break;
2283
2284 case VTABLE_USES:
2285 if (Record.size() % 3 != 0) {
2286 Error("Invalid VTABLE_USES record");
2287 return true;
2288 }
2289
2290 // Later tables overwrite earlier ones.
2291 // FIXME: Modules will have some trouble with this. This is clearly not
2292 // the right way to do this.
2293 VTableUses.clear();
2294
2295 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
2296 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
2297 VTableUses.push_back(
2298 ReadSourceLocation(F, Record, Idx).getRawEncoding());
2299 VTableUses.push_back(Record[Idx++]);
2300 }
2301 break;
2302
2303 case DYNAMIC_CLASSES:
2304 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2305 DynamicClasses.push_back(getGlobalDeclID(F, Record[I]));
2306 break;
2307
2308 case PENDING_IMPLICIT_INSTANTIATIONS:
2309 if (PendingInstantiations.size() % 2 != 0) {
2310 Error("Invalid existing PendingInstantiations");
2311 return true;
2312 }
2313
2314 if (Record.size() % 2 != 0) {
2315 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
2316 return true;
2317 }
2318
2319 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2320 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
2321 PendingInstantiations.push_back(
2322 ReadSourceLocation(F, Record, I).getRawEncoding());
2323 }
2324 break;
2325
2326 case SEMA_DECL_REFS:
2327 // Later tables overwrite earlier ones.
2328 // FIXME: Modules will have some trouble with this.
2329 SemaDeclRefs.clear();
2330 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2331 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
2332 break;
2333
2334 case PPD_ENTITIES_OFFSETS: {
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002335 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
2336 assert(Blob.size() % sizeof(PPEntityOffset) == 0);
2337 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002338
2339 unsigned LocalBasePreprocessedEntityID = Record[0];
2340
2341 unsigned StartingID;
2342 if (!PP.getPreprocessingRecord())
2343 PP.createPreprocessingRecord();
2344 if (!PP.getPreprocessingRecord()->getExternalSource())
2345 PP.getPreprocessingRecord()->SetExternalSource(*this);
2346 StartingID
2347 = PP.getPreprocessingRecord()
2348 ->allocateLoadedEntities(F.NumPreprocessedEntities);
2349 F.BasePreprocessedEntityID = StartingID;
2350
2351 if (F.NumPreprocessedEntities > 0) {
2352 // Introduce the global -> local mapping for preprocessed entities in
2353 // this module.
2354 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
2355
2356 // Introduce the local -> global mapping for preprocessed entities in
2357 // this module.
2358 F.PreprocessedEntityRemap.insertOrReplace(
2359 std::make_pair(LocalBasePreprocessedEntityID,
2360 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
2361 }
2362
2363 break;
2364 }
2365
2366 case DECL_UPDATE_OFFSETS: {
2367 if (Record.size() % 2 != 0) {
2368 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2369 return true;
2370 }
2371 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
2372 DeclUpdateOffsets[getGlobalDeclID(F, Record[I])]
2373 .push_back(std::make_pair(&F, Record[I+1]));
2374 break;
2375 }
2376
2377 case DECL_REPLACEMENTS: {
2378 if (Record.size() % 3 != 0) {
2379 Error("invalid DECL_REPLACEMENTS block in AST file");
2380 return true;
2381 }
2382 for (unsigned I = 0, N = Record.size(); I != N; I += 3)
2383 ReplacedDecls[getGlobalDeclID(F, Record[I])]
2384 = ReplacedDeclInfo(&F, Record[I+1], Record[I+2]);
2385 break;
2386 }
2387
2388 case OBJC_CATEGORIES_MAP: {
2389 if (F.LocalNumObjCCategoriesInMap != 0) {
2390 Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
2391 return true;
2392 }
2393
2394 F.LocalNumObjCCategoriesInMap = Record[0];
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002395 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002396 break;
2397 }
2398
2399 case OBJC_CATEGORIES:
2400 F.ObjCCategories.swap(Record);
2401 break;
2402
2403 case CXX_BASE_SPECIFIER_OFFSETS: {
2404 if (F.LocalNumCXXBaseSpecifiers != 0) {
2405 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2406 return true;
2407 }
2408
2409 F.LocalNumCXXBaseSpecifiers = Record[0];
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002410 F.CXXBaseSpecifiersOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002411 NumCXXBaseSpecifiersLoaded += F.LocalNumCXXBaseSpecifiers;
2412 break;
2413 }
2414
2415 case DIAG_PRAGMA_MAPPINGS:
2416 if (F.PragmaDiagMappings.empty())
2417 F.PragmaDiagMappings.swap(Record);
2418 else
2419 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
2420 Record.begin(), Record.end());
2421 break;
2422
2423 case CUDA_SPECIAL_DECL_REFS:
2424 // Later tables overwrite earlier ones.
2425 // FIXME: Modules will have trouble with this.
2426 CUDASpecialDeclRefs.clear();
2427 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2428 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
2429 break;
2430
2431 case HEADER_SEARCH_TABLE: {
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002432 F.HeaderFileInfoTableData = Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002433 F.LocalNumHeaderFileInfos = Record[1];
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002434 F.HeaderFileFrameworkStrings = Blob.data() + Record[2];
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002435 if (Record[0]) {
2436 F.HeaderFileInfoTable
2437 = HeaderFileInfoLookupTable::Create(
2438 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
2439 (const unsigned char *)F.HeaderFileInfoTableData,
2440 HeaderFileInfoTrait(*this, F,
2441 &PP.getHeaderSearchInfo(),
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002442 Blob.data() + Record[2]));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002443
2444 PP.getHeaderSearchInfo().SetExternalSource(this);
2445 if (!PP.getHeaderSearchInfo().getExternalLookup())
2446 PP.getHeaderSearchInfo().SetExternalLookup(this);
2447 }
2448 break;
2449 }
2450
2451 case FP_PRAGMA_OPTIONS:
2452 // Later tables overwrite earlier ones.
2453 FPPragmaOptions.swap(Record);
2454 break;
2455
2456 case OPENCL_EXTENSIONS:
2457 // Later tables overwrite earlier ones.
2458 OpenCLExtensions.swap(Record);
2459 break;
2460
2461 case TENTATIVE_DEFINITIONS:
2462 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2463 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
2464 break;
2465
2466 case KNOWN_NAMESPACES:
2467 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2468 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
2469 break;
2470
2471 case IMPORTED_MODULES: {
2472 if (F.Kind != MK_Module) {
2473 // If we aren't loading a module (which has its own exports), make
2474 // all of the imported modules visible.
2475 // FIXME: Deal with macros-only imports.
2476 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2477 if (unsigned GlobalID = getGlobalSubmoduleID(F, Record[I]))
2478 ImportedModules.push_back(GlobalID);
2479 }
2480 }
2481 break;
2482 }
2483
2484 case LOCAL_REDECLARATIONS: {
2485 F.RedeclarationChains.swap(Record);
2486 break;
2487 }
2488
2489 case LOCAL_REDECLARATIONS_MAP: {
2490 if (F.LocalNumRedeclarationsInMap != 0) {
2491 Error("duplicate LOCAL_REDECLARATIONS_MAP record in AST file");
2492 return true;
2493 }
2494
2495 F.LocalNumRedeclarationsInMap = Record[0];
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002496 F.RedeclarationsMap = (const LocalRedeclarationsInfo *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002497 break;
2498 }
2499
2500 case MERGED_DECLARATIONS: {
2501 for (unsigned Idx = 0; Idx < Record.size(); /* increment in loop */) {
2502 GlobalDeclID CanonID = getGlobalDeclID(F, Record[Idx++]);
2503 SmallVectorImpl<GlobalDeclID> &Decls = StoredMergedDecls[CanonID];
2504 for (unsigned N = Record[Idx++]; N > 0; --N)
2505 Decls.push_back(getGlobalDeclID(F, Record[Idx++]));
2506 }
2507 break;
2508 }
2509
2510 case MACRO_OFFSET: {
2511 if (F.LocalNumMacros != 0) {
2512 Error("duplicate MACRO_OFFSET record in AST file");
2513 return true;
2514 }
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002515 F.MacroOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002516 F.LocalNumMacros = Record[0];
2517 unsigned LocalBaseMacroID = Record[1];
2518 F.BaseMacroID = getTotalNumMacros();
2519
2520 if (F.LocalNumMacros > 0) {
2521 // Introduce the global -> local mapping for macros within this module.
2522 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
2523
2524 // Introduce the local -> global mapping for macros within this module.
2525 F.MacroRemap.insertOrReplace(
2526 std::make_pair(LocalBaseMacroID,
2527 F.BaseMacroID - LocalBaseMacroID));
2528
2529 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
2530 }
2531 break;
2532 }
2533
2534 case MACRO_UPDATES: {
2535 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2536 MacroID ID = getGlobalMacroID(F, Record[I++]);
2537 if (I == N)
2538 break;
2539
2540 SourceLocation UndefLoc = ReadSourceLocation(F, Record, I);
2541 SubmoduleID SubmoduleID = getGlobalSubmoduleID(F, Record[I++]);;
2542 MacroUpdate Update;
2543 Update.UndefLoc = UndefLoc;
2544 MacroUpdates[ID].push_back(std::make_pair(SubmoduleID, Update));
2545 }
2546 break;
2547 }
2548 }
2549 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002550}
2551
2552void ASTReader::makeNamesVisible(const HiddenNames &Names) {
2553 for (unsigned I = 0, N = Names.size(); I != N; ++I) {
2554 switch (Names[I].getKind()) {
2555 case HiddenName::Declaration:
2556 Names[I].getDecl()->Hidden = false;
2557 break;
2558
2559 case HiddenName::MacroVisibility: {
2560 std::pair<IdentifierInfo *, MacroInfo *> Macro = Names[I].getMacro();
2561 Macro.second->setHidden(!Macro.second->isPublic());
2562 if (Macro.second->isDefined()) {
2563 PP.makeLoadedMacroInfoVisible(Macro.first, Macro.second);
2564 }
2565 break;
2566 }
2567
2568 case HiddenName::MacroUndef: {
2569 std::pair<IdentifierInfo *, MacroInfo *> Macro = Names[I].getMacro();
2570 if (Macro.second->isDefined()) {
2571 Macro.second->setUndefLoc(Names[I].getMacroUndefLoc());
2572 if (PPMutationListener *Listener = PP.getPPMutationListener())
2573 Listener->UndefinedMacro(Macro.second);
2574 PP.makeLoadedMacroInfoVisible(Macro.first, Macro.second);
2575 }
2576 break;
2577 }
2578 }
2579 }
2580}
2581
2582void ASTReader::makeModuleVisible(Module *Mod,
2583 Module::NameVisibilityKind NameVisibility) {
2584 llvm::SmallPtrSet<Module *, 4> Visited;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002585 SmallVector<Module *, 4> Stack;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002586 Stack.push_back(Mod);
2587 while (!Stack.empty()) {
2588 Mod = Stack.back();
2589 Stack.pop_back();
2590
2591 if (NameVisibility <= Mod->NameVisibility) {
2592 // This module already has this level of visibility (or greater), so
2593 // there is nothing more to do.
2594 continue;
2595 }
2596
2597 if (!Mod->isAvailable()) {
2598 // Modules that aren't available cannot be made visible.
2599 continue;
2600 }
2601
2602 // Update the module's name visibility.
2603 Mod->NameVisibility = NameVisibility;
2604
2605 // If we've already deserialized any names from this module,
2606 // mark them as visible.
2607 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
2608 if (Hidden != HiddenNamesMap.end()) {
2609 makeNamesVisible(Hidden->second);
2610 HiddenNamesMap.erase(Hidden);
2611 }
2612
2613 // Push any non-explicit submodules onto the stack to be marked as
2614 // visible.
2615 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2616 SubEnd = Mod->submodule_end();
2617 Sub != SubEnd; ++Sub) {
2618 if (!(*Sub)->IsExplicit && Visited.insert(*Sub))
2619 Stack.push_back(*Sub);
2620 }
2621
2622 // Push any exported modules onto the stack to be marked as visible.
2623 bool AnyWildcard = false;
2624 bool UnrestrictedWildcard = false;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002625 SmallVector<Module *, 4> WildcardRestrictions;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002626 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
2627 Module *Exported = Mod->Exports[I].getPointer();
2628 if (!Mod->Exports[I].getInt()) {
2629 // Export a named module directly; no wildcards involved.
2630 if (Visited.insert(Exported))
2631 Stack.push_back(Exported);
2632
2633 continue;
2634 }
2635
2636 // Wildcard export: export all of the imported modules that match
2637 // the given pattern.
2638 AnyWildcard = true;
2639 if (UnrestrictedWildcard)
2640 continue;
2641
2642 if (Module *Restriction = Mod->Exports[I].getPointer())
2643 WildcardRestrictions.push_back(Restriction);
2644 else {
2645 WildcardRestrictions.clear();
2646 UnrestrictedWildcard = true;
2647 }
2648 }
2649
2650 // If there were any wildcards, push any imported modules that were
2651 // re-exported by the wildcard restriction.
2652 if (!AnyWildcard)
2653 continue;
2654
2655 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
2656 Module *Imported = Mod->Imports[I];
2657 if (!Visited.insert(Imported))
2658 continue;
2659
2660 bool Acceptable = UnrestrictedWildcard;
2661 if (!Acceptable) {
2662 // Check whether this module meets one of the restrictions.
2663 for (unsigned R = 0, NR = WildcardRestrictions.size(); R != NR; ++R) {
2664 Module *Restriction = WildcardRestrictions[R];
2665 if (Imported == Restriction || Imported->isSubModuleOf(Restriction)) {
2666 Acceptable = true;
2667 break;
2668 }
2669 }
2670 }
2671
2672 if (!Acceptable)
2673 continue;
2674
2675 Stack.push_back(Imported);
2676 }
2677 }
2678}
2679
Douglas Gregor1a49d972013-01-25 01:03:03 +00002680bool ASTReader::loadGlobalIndex() {
2681 if (GlobalIndex)
2682 return false;
2683
2684 if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
2685 !Context.getLangOpts().Modules)
2686 return true;
2687
2688 // Try to load the global index.
2689 TriedLoadingGlobalIndex = true;
2690 StringRef ModuleCachePath
2691 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
2692 std::pair<GlobalModuleIndex *, GlobalModuleIndex::ErrorCode> Result
2693 = GlobalModuleIndex::readIndex(FileMgr, ModuleCachePath);
2694 if (!Result.first)
2695 return true;
2696
2697 GlobalIndex.reset(Result.first);
2698 return false;
2699}
2700
2701bool ASTReader::isGlobalIndexUnavailable() const {
2702 return Context.getLangOpts().Modules && UseGlobalIndex &&
2703 !hasGlobalIndex() && TriedLoadingGlobalIndex;
2704}
2705
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002706ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
2707 ModuleKind Type,
2708 SourceLocation ImportLoc,
2709 unsigned ClientLoadCapabilities) {
2710 // Bump the generation number.
2711 unsigned PreviousGeneration = CurrentGeneration++;
2712
2713 unsigned NumModules = ModuleMgr.size();
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002714 SmallVector<ImportedModule, 4> Loaded;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002715 switch(ASTReadResult ReadResult = ReadASTCore(FileName, Type, ImportLoc,
2716 /*ImportedBy=*/0, Loaded,
2717 ClientLoadCapabilities)) {
2718 case Failure:
2719 case OutOfDate:
2720 case VersionMismatch:
2721 case ConfigurationMismatch:
2722 case HadErrors:
2723 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, ModuleMgr.end());
Douglas Gregor1a49d972013-01-25 01:03:03 +00002724
2725 // If we find that any modules are unusable, the global index is going
2726 // to be out-of-date. Just remove it.
2727 GlobalIndex.reset();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002728 return ReadResult;
2729
2730 case Success:
2731 break;
2732 }
2733
2734 // Here comes stuff that we only do once the entire chain is loaded.
2735
2736 // Load the AST blocks of all of the modules that we loaded.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002737 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
2738 MEnd = Loaded.end();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002739 M != MEnd; ++M) {
2740 ModuleFile &F = *M->Mod;
2741
2742 // Read the AST block.
2743 if (ReadASTBlock(F))
2744 return Failure;
2745
2746 // Once read, set the ModuleFile bit base offset and update the size in
2747 // bits of all files we've seen.
2748 F.GlobalBitOffset = TotalModulesSizeInBits;
2749 TotalModulesSizeInBits += F.SizeInBits;
2750 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
2751
2752 // Preload SLocEntries.
2753 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
2754 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
2755 // Load it through the SourceManager and don't call ReadSLocEntry()
2756 // directly because the entry may have already been loaded in which case
2757 // calling ReadSLocEntry() directly would trigger an assertion in
2758 // SourceManager.
2759 SourceMgr.getLoadedSLocEntryByID(Index);
2760 }
2761 }
2762
2763 // Setup the import locations.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002764 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
2765 MEnd = Loaded.end();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002766 M != MEnd; ++M) {
2767 ModuleFile &F = *M->Mod;
2768 if (!M->ImportedBy)
2769 F.ImportLoc = M->ImportLoc;
2770 else
2771 F.ImportLoc = ReadSourceLocation(*M->ImportedBy,
2772 M->ImportLoc.getRawEncoding());
2773 }
2774
2775 // Mark all of the identifiers in the identifier table as being out of date,
2776 // so that various accessors know to check the loaded modules when the
2777 // identifier is used.
2778 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
2779 IdEnd = PP.getIdentifierTable().end();
2780 Id != IdEnd; ++Id)
2781 Id->second->setOutOfDate(true);
2782
2783 // Resolve any unresolved module exports.
2784 for (unsigned I = 0, N = UnresolvedModuleImportExports.size(); I != N; ++I) {
2785 UnresolvedModuleImportExport &Unresolved = UnresolvedModuleImportExports[I];
2786 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
2787 Module *ResolvedMod = getSubmodule(GlobalID);
2788
2789 if (Unresolved.IsImport) {
2790 if (ResolvedMod)
2791 Unresolved.Mod->Imports.push_back(ResolvedMod);
2792 continue;
2793 }
2794
2795 if (ResolvedMod || Unresolved.IsWildcard)
2796 Unresolved.Mod->Exports.push_back(
2797 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
2798 }
2799 UnresolvedModuleImportExports.clear();
2800
2801 InitializeContext();
2802
2803 if (DeserializationListener)
2804 DeserializationListener->ReaderInitialized(this);
2805
2806 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
2807 if (!PrimaryModule.OriginalSourceFileID.isInvalid()) {
2808 PrimaryModule.OriginalSourceFileID
2809 = FileID::get(PrimaryModule.SLocEntryBaseID
2810 + PrimaryModule.OriginalSourceFileID.getOpaqueValue() - 1);
2811
2812 // If this AST file is a precompiled preamble, then set the
2813 // preamble file ID of the source manager to the file source file
2814 // from which the preamble was built.
2815 if (Type == MK_Preamble) {
2816 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
2817 } else if (Type == MK_MainFile) {
2818 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
2819 }
2820 }
2821
2822 // For any Objective-C class definitions we have already loaded, make sure
2823 // that we load any additional categories.
2824 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
2825 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
2826 ObjCClassesLoaded[I],
2827 PreviousGeneration);
2828 }
Douglas Gregor1a49d972013-01-25 01:03:03 +00002829
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002830 return Success;
2831}
2832
2833ASTReader::ASTReadResult
2834ASTReader::ReadASTCore(StringRef FileName,
2835 ModuleKind Type,
2836 SourceLocation ImportLoc,
2837 ModuleFile *ImportedBy,
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002838 SmallVectorImpl<ImportedModule> &Loaded,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002839 unsigned ClientLoadCapabilities) {
2840 ModuleFile *M;
2841 bool NewModule;
2842 std::string ErrorStr;
2843 llvm::tie(M, NewModule) = ModuleMgr.addModule(FileName, Type, ImportLoc,
2844 ImportedBy, CurrentGeneration,
2845 ErrorStr);
2846
2847 if (!M) {
2848 // We couldn't load the module.
2849 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
2850 + ErrorStr;
2851 Error(Msg);
2852 return Failure;
2853 }
2854
2855 if (!NewModule) {
2856 // We've already loaded this module.
2857 return Success;
2858 }
2859
2860 // FIXME: This seems rather a hack. Should CurrentDir be part of the
2861 // module?
2862 if (FileName != "-") {
2863 CurrentDir = llvm::sys::path::parent_path(FileName);
2864 if (CurrentDir.empty()) CurrentDir = ".";
2865 }
2866
2867 ModuleFile &F = *M;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00002868 BitstreamCursor &Stream = F.Stream;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002869 Stream.init(F.StreamFile);
2870 F.SizeInBits = F.Buffer->getBufferSize() * 8;
2871
2872 // Sniff for the signature.
2873 if (Stream.Read(8) != 'C' ||
2874 Stream.Read(8) != 'P' ||
2875 Stream.Read(8) != 'C' ||
2876 Stream.Read(8) != 'H') {
2877 Diag(diag::err_not_a_pch_file) << FileName;
2878 return Failure;
2879 }
2880
2881 // This is used for compatibility with older PCH formats.
2882 bool HaveReadControlBlock = false;
2883
Chris Lattner99a5af02013-01-20 00:00:22 +00002884 while (1) {
2885 llvm::BitstreamEntry Entry = Stream.advance();
2886
2887 switch (Entry.Kind) {
2888 case llvm::BitstreamEntry::Error:
2889 case llvm::BitstreamEntry::EndBlock:
2890 case llvm::BitstreamEntry::Record:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002891 Error("invalid record at top-level of AST file");
2892 return Failure;
Chris Lattner99a5af02013-01-20 00:00:22 +00002893
2894 case llvm::BitstreamEntry::SubBlock:
2895 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002896 }
2897
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002898 // We only know the control subblock ID.
Chris Lattner99a5af02013-01-20 00:00:22 +00002899 switch (Entry.ID) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002900 case llvm::bitc::BLOCKINFO_BLOCK_ID:
2901 if (Stream.ReadBlockInfoBlock()) {
2902 Error("malformed BlockInfoBlock in AST file");
2903 return Failure;
2904 }
2905 break;
2906 case CONTROL_BLOCK_ID:
2907 HaveReadControlBlock = true;
2908 switch (ReadControlBlock(F, Loaded, ClientLoadCapabilities)) {
2909 case Success:
2910 break;
2911
2912 case Failure: return Failure;
2913 case OutOfDate: return OutOfDate;
2914 case VersionMismatch: return VersionMismatch;
2915 case ConfigurationMismatch: return ConfigurationMismatch;
2916 case HadErrors: return HadErrors;
2917 }
2918 break;
2919 case AST_BLOCK_ID:
2920 if (!HaveReadControlBlock) {
2921 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2922 Diag(diag::warn_pch_version_too_old);
2923 return VersionMismatch;
2924 }
2925
2926 // Record that we've loaded this module.
2927 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
2928 return Success;
2929
2930 default:
2931 if (Stream.SkipBlock()) {
2932 Error("malformed block record in AST file");
2933 return Failure;
2934 }
2935 break;
2936 }
2937 }
2938
2939 return Success;
2940}
2941
2942void ASTReader::InitializeContext() {
2943 // If there's a listener, notify them that we "read" the translation unit.
2944 if (DeserializationListener)
2945 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
2946 Context.getTranslationUnitDecl());
2947
2948 // Make sure we load the declaration update records for the translation unit,
2949 // if there are any.
2950 loadDeclUpdateRecords(PREDEF_DECL_TRANSLATION_UNIT_ID,
2951 Context.getTranslationUnitDecl());
2952
2953 // FIXME: Find a better way to deal with collisions between these
2954 // built-in types. Right now, we just ignore the problem.
2955
2956 // Load the special types.
2957 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
2958 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
2959 if (!Context.CFConstantStringTypeDecl)
2960 Context.setCFConstantStringType(GetType(String));
2961 }
2962
2963 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
2964 QualType FileType = GetType(File);
2965 if (FileType.isNull()) {
2966 Error("FILE type is NULL");
2967 return;
2968 }
2969
2970 if (!Context.FILEDecl) {
2971 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
2972 Context.setFILEDecl(Typedef->getDecl());
2973 else {
2974 const TagType *Tag = FileType->getAs<TagType>();
2975 if (!Tag) {
2976 Error("Invalid FILE type in AST file");
2977 return;
2978 }
2979 Context.setFILEDecl(Tag->getDecl());
2980 }
2981 }
2982 }
2983
2984 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
2985 QualType Jmp_bufType = GetType(Jmp_buf);
2986 if (Jmp_bufType.isNull()) {
2987 Error("jmp_buf type is NULL");
2988 return;
2989 }
2990
2991 if (!Context.jmp_bufDecl) {
2992 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
2993 Context.setjmp_bufDecl(Typedef->getDecl());
2994 else {
2995 const TagType *Tag = Jmp_bufType->getAs<TagType>();
2996 if (!Tag) {
2997 Error("Invalid jmp_buf type in AST file");
2998 return;
2999 }
3000 Context.setjmp_bufDecl(Tag->getDecl());
3001 }
3002 }
3003 }
3004
3005 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
3006 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
3007 if (Sigjmp_bufType.isNull()) {
3008 Error("sigjmp_buf type is NULL");
3009 return;
3010 }
3011
3012 if (!Context.sigjmp_bufDecl) {
3013 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
3014 Context.setsigjmp_bufDecl(Typedef->getDecl());
3015 else {
3016 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
3017 assert(Tag && "Invalid sigjmp_buf type in AST file");
3018 Context.setsigjmp_bufDecl(Tag->getDecl());
3019 }
3020 }
3021 }
3022
3023 if (unsigned ObjCIdRedef
3024 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
3025 if (Context.ObjCIdRedefinitionType.isNull())
3026 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
3027 }
3028
3029 if (unsigned ObjCClassRedef
3030 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
3031 if (Context.ObjCClassRedefinitionType.isNull())
3032 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
3033 }
3034
3035 if (unsigned ObjCSelRedef
3036 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
3037 if (Context.ObjCSelRedefinitionType.isNull())
3038 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
3039 }
3040
3041 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
3042 QualType Ucontext_tType = GetType(Ucontext_t);
3043 if (Ucontext_tType.isNull()) {
3044 Error("ucontext_t type is NULL");
3045 return;
3046 }
3047
3048 if (!Context.ucontext_tDecl) {
3049 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
3050 Context.setucontext_tDecl(Typedef->getDecl());
3051 else {
3052 const TagType *Tag = Ucontext_tType->getAs<TagType>();
3053 assert(Tag && "Invalid ucontext_t type in AST file");
3054 Context.setucontext_tDecl(Tag->getDecl());
3055 }
3056 }
3057 }
3058 }
3059
3060 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
3061
3062 // If there were any CUDA special declarations, deserialize them.
3063 if (!CUDASpecialDeclRefs.empty()) {
3064 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
3065 Context.setcudaConfigureCallDecl(
3066 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
3067 }
3068
3069 // Re-export any modules that were imported by a non-module AST file.
3070 for (unsigned I = 0, N = ImportedModules.size(); I != N; ++I) {
3071 if (Module *Imported = getSubmodule(ImportedModules[I]))
3072 makeModuleVisible(Imported, Module::AllVisible);
3073 }
3074 ImportedModules.clear();
3075}
3076
3077void ASTReader::finalizeForWriting() {
3078 for (HiddenNamesMapType::iterator Hidden = HiddenNamesMap.begin(),
3079 HiddenEnd = HiddenNamesMap.end();
3080 Hidden != HiddenEnd; ++Hidden) {
3081 makeNamesVisible(Hidden->second);
3082 }
3083 HiddenNamesMap.clear();
3084}
3085
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003086/// SkipCursorToControlBlock - Given a cursor at the start of an AST file, scan
3087/// ahead and drop the cursor into the start of the CONTROL_BLOCK, returning
3088/// false on success and true on failure.
3089static bool SkipCursorToControlBlock(BitstreamCursor &Cursor) {
3090 while (1) {
3091 llvm::BitstreamEntry Entry = Cursor.advance();
3092 switch (Entry.Kind) {
3093 case llvm::BitstreamEntry::Error:
3094 case llvm::BitstreamEntry::EndBlock:
3095 return true;
3096
3097 case llvm::BitstreamEntry::Record:
3098 // Ignore top-level records.
3099 Cursor.skipRecord(Entry.ID);
3100 break;
3101
3102 case llvm::BitstreamEntry::SubBlock:
3103 if (Entry.ID == CONTROL_BLOCK_ID) {
3104 if (Cursor.EnterSubBlock(CONTROL_BLOCK_ID))
3105 return true;
3106 // Found it!
3107 return false;
3108 }
3109
3110 if (Cursor.SkipBlock())
3111 return true;
3112 }
3113 }
3114}
3115
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003116/// \brief Retrieve the name of the original source file name
3117/// directly from the AST file, without actually loading the AST
3118/// file.
3119std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
3120 FileManager &FileMgr,
3121 DiagnosticsEngine &Diags) {
3122 // Open the AST file.
3123 std::string ErrStr;
3124 OwningPtr<llvm::MemoryBuffer> Buffer;
3125 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
3126 if (!Buffer) {
3127 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ASTFileName << ErrStr;
3128 return std::string();
3129 }
3130
3131 // Initialize the stream
3132 llvm::BitstreamReader StreamFile;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003133 BitstreamCursor Stream;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003134 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
3135 (const unsigned char *)Buffer->getBufferEnd());
3136 Stream.init(StreamFile);
3137
3138 // Sniff for the signature.
3139 if (Stream.Read(8) != 'C' ||
3140 Stream.Read(8) != 'P' ||
3141 Stream.Read(8) != 'C' ||
3142 Stream.Read(8) != 'H') {
3143 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
3144 return std::string();
3145 }
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003146
Chris Lattner88bde502013-01-19 21:39:22 +00003147 // Scan for the CONTROL_BLOCK_ID block.
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003148 if (SkipCursorToControlBlock(Stream)) {
3149 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
3150 return std::string();
Chris Lattner88bde502013-01-19 21:39:22 +00003151 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003152
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003153 // Scan for ORIGINAL_FILE inside the control block.
3154 RecordData Record;
Chris Lattner88bde502013-01-19 21:39:22 +00003155 while (1) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003156 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Chris Lattner88bde502013-01-19 21:39:22 +00003157 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
3158 return std::string();
3159
3160 if (Entry.Kind != llvm::BitstreamEntry::Record) {
3161 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
3162 return std::string();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003163 }
Chris Lattner88bde502013-01-19 21:39:22 +00003164
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003165 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003166 StringRef Blob;
3167 if (Stream.readRecord(Entry.ID, Record, &Blob) == ORIGINAL_FILE)
3168 return Blob.str();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003169 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003170}
3171
3172namespace {
3173 class SimplePCHValidator : public ASTReaderListener {
3174 const LangOptions &ExistingLangOpts;
3175 const TargetOptions &ExistingTargetOpts;
3176 const PreprocessorOptions &ExistingPPOpts;
3177 FileManager &FileMgr;
3178
3179 public:
3180 SimplePCHValidator(const LangOptions &ExistingLangOpts,
3181 const TargetOptions &ExistingTargetOpts,
3182 const PreprocessorOptions &ExistingPPOpts,
3183 FileManager &FileMgr)
3184 : ExistingLangOpts(ExistingLangOpts),
3185 ExistingTargetOpts(ExistingTargetOpts),
3186 ExistingPPOpts(ExistingPPOpts),
3187 FileMgr(FileMgr)
3188 {
3189 }
3190
3191 virtual bool ReadLanguageOptions(const LangOptions &LangOpts,
3192 bool Complain) {
3193 return checkLanguageOptions(ExistingLangOpts, LangOpts, 0);
3194 }
3195 virtual bool ReadTargetOptions(const TargetOptions &TargetOpts,
3196 bool Complain) {
3197 return checkTargetOptions(ExistingTargetOpts, TargetOpts, 0);
3198 }
3199 virtual bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
3200 bool Complain,
3201 std::string &SuggestedPredefines) {
3202 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, 0, FileMgr,
3203 SuggestedPredefines);
3204 }
3205 };
3206}
3207
3208bool ASTReader::readASTFileControlBlock(StringRef Filename,
3209 FileManager &FileMgr,
3210 ASTReaderListener &Listener) {
3211 // Open the AST file.
3212 std::string ErrStr;
3213 OwningPtr<llvm::MemoryBuffer> Buffer;
3214 Buffer.reset(FileMgr.getBufferForFile(Filename, &ErrStr));
3215 if (!Buffer) {
3216 return true;
3217 }
3218
3219 // Initialize the stream
3220 llvm::BitstreamReader StreamFile;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003221 BitstreamCursor Stream;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003222 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
3223 (const unsigned char *)Buffer->getBufferEnd());
3224 Stream.init(StreamFile);
3225
3226 // Sniff for the signature.
3227 if (Stream.Read(8) != 'C' ||
3228 Stream.Read(8) != 'P' ||
3229 Stream.Read(8) != 'C' ||
3230 Stream.Read(8) != 'H') {
3231 return true;
3232 }
3233
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003234 // Scan for the CONTROL_BLOCK_ID block.
3235 if (SkipCursorToControlBlock(Stream))
3236 return true;
3237
3238 // Scan for ORIGINAL_FILE inside the control block.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003239 RecordData Record;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003240 while (1) {
3241 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
3242 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
3243 return false;
3244
3245 if (Entry.Kind != llvm::BitstreamEntry::Record)
3246 return true;
3247
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003248 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003249 StringRef Blob;
3250 unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob);
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003251 switch ((ControlRecordTypes)RecCode) {
3252 case METADATA: {
3253 if (Record[0] != VERSION_MAJOR)
3254 return true;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003255
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003256 const std::string &CurBranch = getClangFullRepositoryVersion();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003257 if (StringRef(CurBranch) != Blob)
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003258 return true;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003259
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003260 break;
3261 }
3262 case LANGUAGE_OPTIONS:
3263 if (ParseLanguageOptions(Record, false, Listener))
3264 return true;
3265 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003266
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003267 case TARGET_OPTIONS:
3268 if (ParseTargetOptions(Record, false, Listener))
3269 return true;
3270 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003271
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003272 case DIAGNOSTIC_OPTIONS:
3273 if (ParseDiagnosticOptions(Record, false, Listener))
3274 return true;
3275 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003276
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003277 case FILE_SYSTEM_OPTIONS:
3278 if (ParseFileSystemOptions(Record, false, Listener))
3279 return true;
3280 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003281
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003282 case HEADER_SEARCH_OPTIONS:
3283 if (ParseHeaderSearchOptions(Record, false, Listener))
3284 return true;
3285 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003286
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003287 case PREPROCESSOR_OPTIONS: {
3288 std::string IgnoredSuggestedPredefines;
3289 if (ParsePreprocessorOptions(Record, false, Listener,
3290 IgnoredSuggestedPredefines))
3291 return true;
3292 break;
3293 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003294
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003295 default:
3296 // No other validation to perform.
3297 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003298 }
3299 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003300}
3301
3302
3303bool ASTReader::isAcceptableASTFile(StringRef Filename,
3304 FileManager &FileMgr,
3305 const LangOptions &LangOpts,
3306 const TargetOptions &TargetOpts,
3307 const PreprocessorOptions &PPOpts) {
3308 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts, FileMgr);
3309 return !readASTFileControlBlock(Filename, FileMgr, validator);
3310}
3311
3312bool ASTReader::ReadSubmoduleBlock(ModuleFile &F) {
3313 // Enter the submodule block.
3314 if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
3315 Error("malformed submodule block record in AST file");
3316 return true;
3317 }
3318
3319 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
3320 bool First = true;
3321 Module *CurrentModule = 0;
3322 RecordData Record;
3323 while (true) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003324 llvm::BitstreamEntry Entry = F.Stream.advanceSkippingSubblocks();
3325
3326 switch (Entry.Kind) {
3327 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
3328 case llvm::BitstreamEntry::Error:
3329 Error("malformed block record in AST file");
3330 return true;
3331 case llvm::BitstreamEntry::EndBlock:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003332 return false;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003333 case llvm::BitstreamEntry::Record:
3334 // The interesting case.
3335 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003336 }
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003337
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003338 // Read a record.
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003339 StringRef Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003340 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003341 switch (F.Stream.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003342 default: // Default behavior: ignore.
3343 break;
3344
3345 case SUBMODULE_DEFINITION: {
3346 if (First) {
3347 Error("missing submodule metadata record at beginning of block");
3348 return true;
3349 }
3350
3351 if (Record.size() < 7) {
3352 Error("malformed module definition");
3353 return true;
3354 }
3355
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003356 StringRef Name = Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003357 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[0]);
3358 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[1]);
3359 bool IsFramework = Record[2];
3360 bool IsExplicit = Record[3];
3361 bool IsSystem = Record[4];
3362 bool InferSubmodules = Record[5];
3363 bool InferExplicitSubmodules = Record[6];
3364 bool InferExportWildcard = Record[7];
3365
3366 Module *ParentModule = 0;
3367 if (Parent)
3368 ParentModule = getSubmodule(Parent);
3369
3370 // Retrieve this (sub)module from the module map, creating it if
3371 // necessary.
3372 CurrentModule = ModMap.findOrCreateModule(Name, ParentModule,
3373 IsFramework,
3374 IsExplicit).first;
3375 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
3376 if (GlobalIndex >= SubmodulesLoaded.size() ||
3377 SubmodulesLoaded[GlobalIndex]) {
3378 Error("too many submodules");
3379 return true;
3380 }
3381
3382 CurrentModule->setASTFile(F.File);
3383 CurrentModule->IsFromModuleFile = true;
3384 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
3385 CurrentModule->InferSubmodules = InferSubmodules;
3386 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
3387 CurrentModule->InferExportWildcard = InferExportWildcard;
3388 if (DeserializationListener)
3389 DeserializationListener->ModuleRead(GlobalID, CurrentModule);
3390
3391 SubmodulesLoaded[GlobalIndex] = CurrentModule;
Douglas Gregorb6cbe512013-01-14 17:21:00 +00003392
3393 // Clear out link libraries; the module file has them.
3394 CurrentModule->LinkLibraries.clear();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003395 break;
3396 }
3397
3398 case SUBMODULE_UMBRELLA_HEADER: {
3399 if (First) {
3400 Error("missing submodule metadata record at beginning of block");
3401 return true;
3402 }
3403
3404 if (!CurrentModule)
3405 break;
3406
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003407 if (const FileEntry *Umbrella = PP.getFileManager().getFile(Blob)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003408 if (!CurrentModule->getUmbrellaHeader())
3409 ModMap.setUmbrellaHeader(CurrentModule, Umbrella);
3410 else if (CurrentModule->getUmbrellaHeader() != Umbrella) {
3411 Error("mismatched umbrella headers in submodule");
3412 return true;
3413 }
3414 }
3415 break;
3416 }
3417
3418 case SUBMODULE_HEADER: {
3419 if (First) {
3420 Error("missing submodule metadata record at beginning of block");
3421 return true;
3422 }
3423
3424 if (!CurrentModule)
3425 break;
3426
3427 // FIXME: Be more lazy about this!
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003428 if (const FileEntry *File = PP.getFileManager().getFile(Blob)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003429 if (std::find(CurrentModule->Headers.begin(),
3430 CurrentModule->Headers.end(),
3431 File) == CurrentModule->Headers.end())
3432 ModMap.addHeader(CurrentModule, File, false);
3433 }
3434 break;
3435 }
3436
3437 case SUBMODULE_EXCLUDED_HEADER: {
3438 if (First) {
3439 Error("missing submodule metadata record at beginning of block");
3440 return true;
3441 }
3442
3443 if (!CurrentModule)
3444 break;
3445
3446 // FIXME: Be more lazy about this!
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003447 if (const FileEntry *File = PP.getFileManager().getFile(Blob)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003448 if (std::find(CurrentModule->Headers.begin(),
3449 CurrentModule->Headers.end(),
3450 File) == CurrentModule->Headers.end())
3451 ModMap.addHeader(CurrentModule, File, true);
3452 }
3453 break;
3454 }
3455
3456 case SUBMODULE_TOPHEADER: {
3457 if (First) {
3458 Error("missing submodule metadata record at beginning of block");
3459 return true;
3460 }
3461
3462 if (!CurrentModule)
3463 break;
3464
3465 // FIXME: Be more lazy about this!
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003466 if (const FileEntry *File = PP.getFileManager().getFile(Blob))
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003467 CurrentModule->TopHeaders.insert(File);
3468 break;
3469 }
3470
3471 case SUBMODULE_UMBRELLA_DIR: {
3472 if (First) {
3473 Error("missing submodule metadata record at beginning of block");
3474 return true;
3475 }
3476
3477 if (!CurrentModule)
3478 break;
3479
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003480 if (const DirectoryEntry *Umbrella
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003481 = PP.getFileManager().getDirectory(Blob)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003482 if (!CurrentModule->getUmbrellaDir())
3483 ModMap.setUmbrellaDir(CurrentModule, Umbrella);
3484 else if (CurrentModule->getUmbrellaDir() != Umbrella) {
3485 Error("mismatched umbrella directories in submodule");
3486 return true;
3487 }
3488 }
3489 break;
3490 }
3491
3492 case SUBMODULE_METADATA: {
3493 if (!First) {
3494 Error("submodule metadata record not at beginning of block");
3495 return true;
3496 }
3497 First = false;
3498
3499 F.BaseSubmoduleID = getTotalNumSubmodules();
3500 F.LocalNumSubmodules = Record[0];
3501 unsigned LocalBaseSubmoduleID = Record[1];
3502 if (F.LocalNumSubmodules > 0) {
3503 // Introduce the global -> local mapping for submodules within this
3504 // module.
3505 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
3506
3507 // Introduce the local -> global mapping for submodules within this
3508 // module.
3509 F.SubmoduleRemap.insertOrReplace(
3510 std::make_pair(LocalBaseSubmoduleID,
3511 F.BaseSubmoduleID - LocalBaseSubmoduleID));
3512
3513 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
3514 }
3515 break;
3516 }
3517
3518 case SUBMODULE_IMPORTS: {
3519 if (First) {
3520 Error("missing submodule metadata record at beginning of block");
3521 return true;
3522 }
3523
3524 if (!CurrentModule)
3525 break;
3526
3527 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
3528 UnresolvedModuleImportExport Unresolved;
3529 Unresolved.File = &F;
3530 Unresolved.Mod = CurrentModule;
3531 Unresolved.ID = Record[Idx];
3532 Unresolved.IsImport = true;
3533 Unresolved.IsWildcard = false;
3534 UnresolvedModuleImportExports.push_back(Unresolved);
3535 }
3536 break;
3537 }
3538
3539 case SUBMODULE_EXPORTS: {
3540 if (First) {
3541 Error("missing submodule metadata record at beginning of block");
3542 return true;
3543 }
3544
3545 if (!CurrentModule)
3546 break;
3547
3548 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
3549 UnresolvedModuleImportExport Unresolved;
3550 Unresolved.File = &F;
3551 Unresolved.Mod = CurrentModule;
3552 Unresolved.ID = Record[Idx];
3553 Unresolved.IsImport = false;
3554 Unresolved.IsWildcard = Record[Idx + 1];
3555 UnresolvedModuleImportExports.push_back(Unresolved);
3556 }
3557
3558 // Once we've loaded the set of exports, there's no reason to keep
3559 // the parsed, unresolved exports around.
3560 CurrentModule->UnresolvedExports.clear();
3561 break;
3562 }
3563 case SUBMODULE_REQUIRES: {
3564 if (First) {
3565 Error("missing submodule metadata record at beginning of block");
3566 return true;
3567 }
3568
3569 if (!CurrentModule)
3570 break;
3571
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003572 CurrentModule->addRequirement(Blob, Context.getLangOpts(),
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003573 Context.getTargetInfo());
3574 break;
3575 }
Douglas Gregorb6cbe512013-01-14 17:21:00 +00003576
3577 case SUBMODULE_LINK_LIBRARY:
3578 if (First) {
3579 Error("missing submodule metadata record at beginning of block");
3580 return true;
3581 }
3582
3583 if (!CurrentModule)
3584 break;
3585
3586 CurrentModule->LinkLibraries.push_back(
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003587 Module::LinkLibrary(Blob, Record[0]));
Douglas Gregorb6cbe512013-01-14 17:21:00 +00003588 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003589 }
3590 }
3591}
3592
3593/// \brief Parse the record that corresponds to a LangOptions data
3594/// structure.
3595///
3596/// This routine parses the language options from the AST file and then gives
3597/// them to the AST listener if one is set.
3598///
3599/// \returns true if the listener deems the file unacceptable, false otherwise.
3600bool ASTReader::ParseLanguageOptions(const RecordData &Record,
3601 bool Complain,
3602 ASTReaderListener &Listener) {
3603 LangOptions LangOpts;
3604 unsigned Idx = 0;
3605#define LANGOPT(Name, Bits, Default, Description) \
3606 LangOpts.Name = Record[Idx++];
3607#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
3608 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
3609#include "clang/Basic/LangOptions.def"
Will Dietz4f45bc02013-01-18 11:30:38 +00003610#define SANITIZER(NAME, ID) LangOpts.Sanitize.ID = Record[Idx++];
3611#include "clang/Basic/Sanitizers.def"
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003612
3613 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
3614 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
3615 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
3616
3617 unsigned Length = Record[Idx++];
3618 LangOpts.CurrentModule.assign(Record.begin() + Idx,
3619 Record.begin() + Idx + Length);
3620 return Listener.ReadLanguageOptions(LangOpts, Complain);
3621}
3622
3623bool ASTReader::ParseTargetOptions(const RecordData &Record,
3624 bool Complain,
3625 ASTReaderListener &Listener) {
3626 unsigned Idx = 0;
3627 TargetOptions TargetOpts;
3628 TargetOpts.Triple = ReadString(Record, Idx);
3629 TargetOpts.CPU = ReadString(Record, Idx);
3630 TargetOpts.ABI = ReadString(Record, Idx);
3631 TargetOpts.CXXABI = ReadString(Record, Idx);
3632 TargetOpts.LinkerVersion = ReadString(Record, Idx);
3633 for (unsigned N = Record[Idx++]; N; --N) {
3634 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
3635 }
3636 for (unsigned N = Record[Idx++]; N; --N) {
3637 TargetOpts.Features.push_back(ReadString(Record, Idx));
3638 }
3639
3640 return Listener.ReadTargetOptions(TargetOpts, Complain);
3641}
3642
3643bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
3644 ASTReaderListener &Listener) {
3645 DiagnosticOptions DiagOpts;
3646 unsigned Idx = 0;
3647#define DIAGOPT(Name, Bits, Default) DiagOpts.Name = Record[Idx++];
3648#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
3649 DiagOpts.set##Name(static_cast<Type>(Record[Idx++]));
3650#include "clang/Basic/DiagnosticOptions.def"
3651
3652 for (unsigned N = Record[Idx++]; N; --N) {
3653 DiagOpts.Warnings.push_back(ReadString(Record, Idx));
3654 }
3655
3656 return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
3657}
3658
3659bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
3660 ASTReaderListener &Listener) {
3661 FileSystemOptions FSOpts;
3662 unsigned Idx = 0;
3663 FSOpts.WorkingDir = ReadString(Record, Idx);
3664 return Listener.ReadFileSystemOptions(FSOpts, Complain);
3665}
3666
3667bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
3668 bool Complain,
3669 ASTReaderListener &Listener) {
3670 HeaderSearchOptions HSOpts;
3671 unsigned Idx = 0;
3672 HSOpts.Sysroot = ReadString(Record, Idx);
3673
3674 // Include entries.
3675 for (unsigned N = Record[Idx++]; N; --N) {
3676 std::string Path = ReadString(Record, Idx);
3677 frontend::IncludeDirGroup Group
3678 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
3679 bool IsUserSupplied = Record[Idx++];
3680 bool IsFramework = Record[Idx++];
3681 bool IgnoreSysRoot = Record[Idx++];
3682 bool IsInternal = Record[Idx++];
3683 bool ImplicitExternC = Record[Idx++];
3684 HSOpts.UserEntries.push_back(
3685 HeaderSearchOptions::Entry(Path, Group, IsUserSupplied, IsFramework,
3686 IgnoreSysRoot, IsInternal, ImplicitExternC));
3687 }
3688
3689 // System header prefixes.
3690 for (unsigned N = Record[Idx++]; N; --N) {
3691 std::string Prefix = ReadString(Record, Idx);
3692 bool IsSystemHeader = Record[Idx++];
3693 HSOpts.SystemHeaderPrefixes.push_back(
3694 HeaderSearchOptions::SystemHeaderPrefix(Prefix, IsSystemHeader));
3695 }
3696
3697 HSOpts.ResourceDir = ReadString(Record, Idx);
3698 HSOpts.ModuleCachePath = ReadString(Record, Idx);
3699 HSOpts.DisableModuleHash = Record[Idx++];
3700 HSOpts.UseBuiltinIncludes = Record[Idx++];
3701 HSOpts.UseStandardSystemIncludes = Record[Idx++];
3702 HSOpts.UseStandardCXXIncludes = Record[Idx++];
3703 HSOpts.UseLibcxx = Record[Idx++];
3704
3705 return Listener.ReadHeaderSearchOptions(HSOpts, Complain);
3706}
3707
3708bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
3709 bool Complain,
3710 ASTReaderListener &Listener,
3711 std::string &SuggestedPredefines) {
3712 PreprocessorOptions PPOpts;
3713 unsigned Idx = 0;
3714
3715 // Macro definitions/undefs
3716 for (unsigned N = Record[Idx++]; N; --N) {
3717 std::string Macro = ReadString(Record, Idx);
3718 bool IsUndef = Record[Idx++];
3719 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
3720 }
3721
3722 // Includes
3723 for (unsigned N = Record[Idx++]; N; --N) {
3724 PPOpts.Includes.push_back(ReadString(Record, Idx));
3725 }
3726
3727 // Macro Includes
3728 for (unsigned N = Record[Idx++]; N; --N) {
3729 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
3730 }
3731
3732 PPOpts.UsePredefines = Record[Idx++];
3733 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
3734 PPOpts.ImplicitPTHInclude = ReadString(Record, Idx);
3735 PPOpts.ObjCXXARCStandardLibrary =
3736 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
3737 SuggestedPredefines.clear();
3738 return Listener.ReadPreprocessorOptions(PPOpts, Complain,
3739 SuggestedPredefines);
3740}
3741
3742std::pair<ModuleFile *, unsigned>
3743ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
3744 GlobalPreprocessedEntityMapType::iterator
3745 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
3746 assert(I != GlobalPreprocessedEntityMap.end() &&
3747 "Corrupted global preprocessed entity map");
3748 ModuleFile *M = I->second;
3749 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
3750 return std::make_pair(M, LocalIndex);
3751}
3752
3753std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
3754ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
3755 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
3756 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
3757 Mod.NumPreprocessedEntities);
3758
3759 return std::make_pair(PreprocessingRecord::iterator(),
3760 PreprocessingRecord::iterator());
3761}
3762
3763std::pair<ASTReader::ModuleDeclIterator, ASTReader::ModuleDeclIterator>
3764ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
3765 return std::make_pair(ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
3766 ModuleDeclIterator(this, &Mod,
3767 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
3768}
3769
3770PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
3771 PreprocessedEntityID PPID = Index+1;
3772 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
3773 ModuleFile &M = *PPInfo.first;
3774 unsigned LocalIndex = PPInfo.second;
3775 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
3776
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003777 if (!PP.getPreprocessingRecord()) {
3778 Error("no preprocessing record");
3779 return 0;
3780 }
3781
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003782 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
3783 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
3784
3785 llvm::BitstreamEntry Entry =
3786 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
3787 if (Entry.Kind != llvm::BitstreamEntry::Record)
3788 return 0;
3789
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003790 // Read the record.
3791 SourceRange Range(ReadSourceLocation(M, PPOffs.Begin),
3792 ReadSourceLocation(M, PPOffs.End));
3793 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003794 StringRef Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003795 RecordData Record;
3796 PreprocessorDetailRecordTypes RecType =
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003797 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.readRecord(
3798 Entry.ID, Record, &Blob);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003799 switch (RecType) {
3800 case PPD_MACRO_EXPANSION: {
3801 bool isBuiltin = Record[0];
3802 IdentifierInfo *Name = 0;
3803 MacroDefinition *Def = 0;
3804 if (isBuiltin)
3805 Name = getLocalIdentifier(M, Record[1]);
3806 else {
3807 PreprocessedEntityID
3808 GlobalID = getGlobalPreprocessedEntityID(M, Record[1]);
3809 Def =cast<MacroDefinition>(PPRec.getLoadedPreprocessedEntity(GlobalID-1));
3810 }
3811
3812 MacroExpansion *ME;
3813 if (isBuiltin)
3814 ME = new (PPRec) MacroExpansion(Name, Range);
3815 else
3816 ME = new (PPRec) MacroExpansion(Def, Range);
3817
3818 return ME;
3819 }
3820
3821 case PPD_MACRO_DEFINITION: {
3822 // Decode the identifier info and then check again; if the macro is
3823 // still defined and associated with the identifier,
3824 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
3825 MacroDefinition *MD
3826 = new (PPRec) MacroDefinition(II, Range);
3827
3828 if (DeserializationListener)
3829 DeserializationListener->MacroDefinitionRead(PPID, MD);
3830
3831 return MD;
3832 }
3833
3834 case PPD_INCLUSION_DIRECTIVE: {
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003835 const char *FullFileNameStart = Blob.data() + Record[0];
3836 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003837 const FileEntry *File = 0;
3838 if (!FullFileName.empty())
3839 File = PP.getFileManager().getFile(FullFileName);
3840
3841 // FIXME: Stable encoding
3842 InclusionDirective::InclusionKind Kind
3843 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
3844 InclusionDirective *ID
3845 = new (PPRec) InclusionDirective(PPRec, Kind,
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003846 StringRef(Blob.data(), Record[0]),
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003847 Record[1], Record[3],
3848 File,
3849 Range);
3850 return ID;
3851 }
3852 }
3853
3854 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
3855}
3856
3857/// \brief \arg SLocMapI points at a chunk of a module that contains no
3858/// preprocessed entities or the entities it contains are not the ones we are
3859/// looking for. Find the next module that contains entities and return the ID
3860/// of the first entry.
3861PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
3862 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
3863 ++SLocMapI;
3864 for (GlobalSLocOffsetMapType::const_iterator
3865 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
3866 ModuleFile &M = *SLocMapI->second;
3867 if (M.NumPreprocessedEntities)
3868 return M.BasePreprocessedEntityID;
3869 }
3870
3871 return getTotalNumPreprocessedEntities();
3872}
3873
3874namespace {
3875
3876template <unsigned PPEntityOffset::*PPLoc>
3877struct PPEntityComp {
3878 const ASTReader &Reader;
3879 ModuleFile &M;
3880
3881 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) { }
3882
3883 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
3884 SourceLocation LHS = getLoc(L);
3885 SourceLocation RHS = getLoc(R);
3886 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
3887 }
3888
3889 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
3890 SourceLocation LHS = getLoc(L);
3891 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
3892 }
3893
3894 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
3895 SourceLocation RHS = getLoc(R);
3896 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
3897 }
3898
3899 SourceLocation getLoc(const PPEntityOffset &PPE) const {
3900 return Reader.ReadSourceLocation(M, PPE.*PPLoc);
3901 }
3902};
3903
3904}
3905
3906/// \brief Returns the first preprocessed entity ID that ends after \arg BLoc.
3907PreprocessedEntityID
3908ASTReader::findBeginPreprocessedEntity(SourceLocation BLoc) const {
3909 if (SourceMgr.isLocalSourceLocation(BLoc))
3910 return getTotalNumPreprocessedEntities();
3911
3912 GlobalSLocOffsetMapType::const_iterator
3913 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
3914 BLoc.getOffset());
3915 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
3916 "Corrupted global sloc offset map");
3917
3918 if (SLocMapI->second->NumPreprocessedEntities == 0)
3919 return findNextPreprocessedEntity(SLocMapI);
3920
3921 ModuleFile &M = *SLocMapI->second;
3922 typedef const PPEntityOffset *pp_iterator;
3923 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
3924 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
3925
3926 size_t Count = M.NumPreprocessedEntities;
3927 size_t Half;
3928 pp_iterator First = pp_begin;
3929 pp_iterator PPI;
3930
3931 // Do a binary search manually instead of using std::lower_bound because
3932 // The end locations of entities may be unordered (when a macro expansion
3933 // is inside another macro argument), but for this case it is not important
3934 // whether we get the first macro expansion or its containing macro.
3935 while (Count > 0) {
3936 Half = Count/2;
3937 PPI = First;
3938 std::advance(PPI, Half);
3939 if (SourceMgr.isBeforeInTranslationUnit(ReadSourceLocation(M, PPI->End),
3940 BLoc)){
3941 First = PPI;
3942 ++First;
3943 Count = Count - Half - 1;
3944 } else
3945 Count = Half;
3946 }
3947
3948 if (PPI == pp_end)
3949 return findNextPreprocessedEntity(SLocMapI);
3950
3951 return M.BasePreprocessedEntityID + (PPI - pp_begin);
3952}
3953
3954/// \brief Returns the first preprocessed entity ID that begins after \arg ELoc.
3955PreprocessedEntityID
3956ASTReader::findEndPreprocessedEntity(SourceLocation ELoc) const {
3957 if (SourceMgr.isLocalSourceLocation(ELoc))
3958 return getTotalNumPreprocessedEntities();
3959
3960 GlobalSLocOffsetMapType::const_iterator
3961 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
3962 ELoc.getOffset());
3963 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
3964 "Corrupted global sloc offset map");
3965
3966 if (SLocMapI->second->NumPreprocessedEntities == 0)
3967 return findNextPreprocessedEntity(SLocMapI);
3968
3969 ModuleFile &M = *SLocMapI->second;
3970 typedef const PPEntityOffset *pp_iterator;
3971 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
3972 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
3973 pp_iterator PPI =
3974 std::upper_bound(pp_begin, pp_end, ELoc,
3975 PPEntityComp<&PPEntityOffset::Begin>(*this, M));
3976
3977 if (PPI == pp_end)
3978 return findNextPreprocessedEntity(SLocMapI);
3979
3980 return M.BasePreprocessedEntityID + (PPI - pp_begin);
3981}
3982
3983/// \brief Returns a pair of [Begin, End) indices of preallocated
3984/// preprocessed entities that \arg Range encompasses.
3985std::pair<unsigned, unsigned>
3986 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
3987 if (Range.isInvalid())
3988 return std::make_pair(0,0);
3989 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
3990
3991 PreprocessedEntityID BeginID = findBeginPreprocessedEntity(Range.getBegin());
3992 PreprocessedEntityID EndID = findEndPreprocessedEntity(Range.getEnd());
3993 return std::make_pair(BeginID, EndID);
3994}
3995
3996/// \brief Optionally returns true or false if the preallocated preprocessed
3997/// entity with index \arg Index came from file \arg FID.
3998llvm::Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
3999 FileID FID) {
4000 if (FID.isInvalid())
4001 return false;
4002
4003 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
4004 ModuleFile &M = *PPInfo.first;
4005 unsigned LocalIndex = PPInfo.second;
4006 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
4007
4008 SourceLocation Loc = ReadSourceLocation(M, PPOffs.Begin);
4009 if (Loc.isInvalid())
4010 return false;
4011
4012 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
4013 return true;
4014 else
4015 return false;
4016}
4017
4018namespace {
4019 /// \brief Visitor used to search for information about a header file.
4020 class HeaderFileInfoVisitor {
4021 ASTReader &Reader;
4022 const FileEntry *FE;
4023
4024 llvm::Optional<HeaderFileInfo> HFI;
4025
4026 public:
4027 HeaderFileInfoVisitor(ASTReader &Reader, const FileEntry *FE)
4028 : Reader(Reader), FE(FE) { }
4029
4030 static bool visit(ModuleFile &M, void *UserData) {
4031 HeaderFileInfoVisitor *This
4032 = static_cast<HeaderFileInfoVisitor *>(UserData);
4033
4034 HeaderFileInfoTrait Trait(This->Reader, M,
4035 &This->Reader.getPreprocessor().getHeaderSearchInfo(),
4036 M.HeaderFileFrameworkStrings,
4037 This->FE->getName());
4038
4039 HeaderFileInfoLookupTable *Table
4040 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
4041 if (!Table)
4042 return false;
4043
4044 // Look in the on-disk hash table for an entry for this file name.
4045 HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE->getName(),
4046 &Trait);
4047 if (Pos == Table->end())
4048 return false;
4049
4050 This->HFI = *Pos;
4051 return true;
4052 }
4053
4054 llvm::Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
4055 };
4056}
4057
4058HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
4059 HeaderFileInfoVisitor Visitor(*this, FE);
4060 ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor);
4061 if (llvm::Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) {
4062 if (Listener)
4063 Listener->ReadHeaderFileInfo(*HFI, FE->getUID());
4064 return *HFI;
4065 }
4066
4067 return HeaderFileInfo();
4068}
4069
4070void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
4071 // FIXME: Make it work properly with modules.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00004072 SmallVector<DiagnosticsEngine::DiagState *, 32> DiagStates;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004073 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
4074 ModuleFile &F = *(*I);
4075 unsigned Idx = 0;
4076 DiagStates.clear();
4077 assert(!Diag.DiagStates.empty());
4078 DiagStates.push_back(&Diag.DiagStates.front()); // the command-line one.
4079 while (Idx < F.PragmaDiagMappings.size()) {
4080 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
4081 unsigned DiagStateID = F.PragmaDiagMappings[Idx++];
4082 if (DiagStateID != 0) {
4083 Diag.DiagStatePoints.push_back(
4084 DiagnosticsEngine::DiagStatePoint(DiagStates[DiagStateID-1],
4085 FullSourceLoc(Loc, SourceMgr)));
4086 continue;
4087 }
4088
4089 assert(DiagStateID == 0);
4090 // A new DiagState was created here.
4091 Diag.DiagStates.push_back(*Diag.GetCurDiagState());
4092 DiagnosticsEngine::DiagState *NewState = &Diag.DiagStates.back();
4093 DiagStates.push_back(NewState);
4094 Diag.DiagStatePoints.push_back(
4095 DiagnosticsEngine::DiagStatePoint(NewState,
4096 FullSourceLoc(Loc, SourceMgr)));
4097 while (1) {
4098 assert(Idx < F.PragmaDiagMappings.size() &&
4099 "Invalid data, didn't find '-1' marking end of diag/map pairs");
4100 if (Idx >= F.PragmaDiagMappings.size()) {
4101 break; // Something is messed up but at least avoid infinite loop in
4102 // release build.
4103 }
4104 unsigned DiagID = F.PragmaDiagMappings[Idx++];
4105 if (DiagID == (unsigned)-1) {
4106 break; // no more diag/map pairs for this location.
4107 }
4108 diag::Mapping Map = (diag::Mapping)F.PragmaDiagMappings[Idx++];
4109 DiagnosticMappingInfo MappingInfo = Diag.makeMappingInfo(Map, Loc);
4110 Diag.GetCurDiagState()->setMappingInfo(DiagID, MappingInfo);
4111 }
4112 }
4113 }
4114}
4115
4116/// \brief Get the correct cursor and offset for loading a type.
4117ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
4118 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
4119 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
4120 ModuleFile *M = I->second;
4121 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
4122}
4123
4124/// \brief Read and return the type with the given index..
4125///
4126/// The index is the type ID, shifted and minus the number of predefs. This
4127/// routine actually reads the record corresponding to the type at the given
4128/// location. It is a helper routine for GetType, which deals with reading type
4129/// IDs.
4130QualType ASTReader::readTypeRecord(unsigned Index) {
4131 RecordLocation Loc = TypeCursorForIndex(Index);
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004132 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004133
4134 // Keep track of where we are in the stream, then jump back there
4135 // after reading this type.
4136 SavedStreamPosition SavedPosition(DeclsCursor);
4137
4138 ReadingKindTracker ReadingKind(Read_Type, *this);
4139
4140 // Note that we are loading a type record.
4141 Deserializing AType(this);
4142
4143 unsigned Idx = 0;
4144 DeclsCursor.JumpToBit(Loc.Offset);
4145 RecordData Record;
4146 unsigned Code = DeclsCursor.ReadCode();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00004147 switch ((TypeCode)DeclsCursor.readRecord(Code, Record)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004148 case TYPE_EXT_QUAL: {
4149 if (Record.size() != 2) {
4150 Error("Incorrect encoding of extended qualifier type");
4151 return QualType();
4152 }
4153 QualType Base = readType(*Loc.F, Record, Idx);
4154 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
4155 return Context.getQualifiedType(Base, Quals);
4156 }
4157
4158 case TYPE_COMPLEX: {
4159 if (Record.size() != 1) {
4160 Error("Incorrect encoding of complex type");
4161 return QualType();
4162 }
4163 QualType ElemType = readType(*Loc.F, Record, Idx);
4164 return Context.getComplexType(ElemType);
4165 }
4166
4167 case TYPE_POINTER: {
4168 if (Record.size() != 1) {
4169 Error("Incorrect encoding of pointer type");
4170 return QualType();
4171 }
4172 QualType PointeeType = readType(*Loc.F, Record, Idx);
4173 return Context.getPointerType(PointeeType);
4174 }
4175
4176 case TYPE_BLOCK_POINTER: {
4177 if (Record.size() != 1) {
4178 Error("Incorrect encoding of block pointer type");
4179 return QualType();
4180 }
4181 QualType PointeeType = readType(*Loc.F, Record, Idx);
4182 return Context.getBlockPointerType(PointeeType);
4183 }
4184
4185 case TYPE_LVALUE_REFERENCE: {
4186 if (Record.size() != 2) {
4187 Error("Incorrect encoding of lvalue reference type");
4188 return QualType();
4189 }
4190 QualType PointeeType = readType(*Loc.F, Record, Idx);
4191 return Context.getLValueReferenceType(PointeeType, Record[1]);
4192 }
4193
4194 case TYPE_RVALUE_REFERENCE: {
4195 if (Record.size() != 1) {
4196 Error("Incorrect encoding of rvalue reference type");
4197 return QualType();
4198 }
4199 QualType PointeeType = readType(*Loc.F, Record, Idx);
4200 return Context.getRValueReferenceType(PointeeType);
4201 }
4202
4203 case TYPE_MEMBER_POINTER: {
4204 if (Record.size() != 2) {
4205 Error("Incorrect encoding of member pointer type");
4206 return QualType();
4207 }
4208 QualType PointeeType = readType(*Loc.F, Record, Idx);
4209 QualType ClassType = readType(*Loc.F, Record, Idx);
4210 if (PointeeType.isNull() || ClassType.isNull())
4211 return QualType();
4212
4213 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
4214 }
4215
4216 case TYPE_CONSTANT_ARRAY: {
4217 QualType ElementType = readType(*Loc.F, Record, Idx);
4218 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4219 unsigned IndexTypeQuals = Record[2];
4220 unsigned Idx = 3;
4221 llvm::APInt Size = ReadAPInt(Record, Idx);
4222 return Context.getConstantArrayType(ElementType, Size,
4223 ASM, IndexTypeQuals);
4224 }
4225
4226 case TYPE_INCOMPLETE_ARRAY: {
4227 QualType ElementType = readType(*Loc.F, Record, Idx);
4228 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4229 unsigned IndexTypeQuals = Record[2];
4230 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
4231 }
4232
4233 case TYPE_VARIABLE_ARRAY: {
4234 QualType ElementType = readType(*Loc.F, Record, Idx);
4235 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4236 unsigned IndexTypeQuals = Record[2];
4237 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
4238 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
4239 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
4240 ASM, IndexTypeQuals,
4241 SourceRange(LBLoc, RBLoc));
4242 }
4243
4244 case TYPE_VECTOR: {
4245 if (Record.size() != 3) {
4246 Error("incorrect encoding of vector type in AST file");
4247 return QualType();
4248 }
4249
4250 QualType ElementType = readType(*Loc.F, Record, Idx);
4251 unsigned NumElements = Record[1];
4252 unsigned VecKind = Record[2];
4253 return Context.getVectorType(ElementType, NumElements,
4254 (VectorType::VectorKind)VecKind);
4255 }
4256
4257 case TYPE_EXT_VECTOR: {
4258 if (Record.size() != 3) {
4259 Error("incorrect encoding of extended vector type in AST file");
4260 return QualType();
4261 }
4262
4263 QualType ElementType = readType(*Loc.F, Record, Idx);
4264 unsigned NumElements = Record[1];
4265 return Context.getExtVectorType(ElementType, NumElements);
4266 }
4267
4268 case TYPE_FUNCTION_NO_PROTO: {
4269 if (Record.size() != 6) {
4270 Error("incorrect encoding of no-proto function type");
4271 return QualType();
4272 }
4273 QualType ResultType = readType(*Loc.F, Record, Idx);
4274 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
4275 (CallingConv)Record[4], Record[5]);
4276 return Context.getFunctionNoProtoType(ResultType, Info);
4277 }
4278
4279 case TYPE_FUNCTION_PROTO: {
4280 QualType ResultType = readType(*Loc.F, Record, Idx);
4281
4282 FunctionProtoType::ExtProtoInfo EPI;
4283 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
4284 /*hasregparm*/ Record[2],
4285 /*regparm*/ Record[3],
4286 static_cast<CallingConv>(Record[4]),
4287 /*produces*/ Record[5]);
4288
4289 unsigned Idx = 6;
4290 unsigned NumParams = Record[Idx++];
4291 SmallVector<QualType, 16> ParamTypes;
4292 for (unsigned I = 0; I != NumParams; ++I)
4293 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
4294
4295 EPI.Variadic = Record[Idx++];
4296 EPI.HasTrailingReturn = Record[Idx++];
4297 EPI.TypeQuals = Record[Idx++];
4298 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
4299 ExceptionSpecificationType EST =
4300 static_cast<ExceptionSpecificationType>(Record[Idx++]);
4301 EPI.ExceptionSpecType = EST;
4302 SmallVector<QualType, 2> Exceptions;
4303 if (EST == EST_Dynamic) {
4304 EPI.NumExceptions = Record[Idx++];
4305 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
4306 Exceptions.push_back(readType(*Loc.F, Record, Idx));
4307 EPI.Exceptions = Exceptions.data();
4308 } else if (EST == EST_ComputedNoexcept) {
4309 EPI.NoexceptExpr = ReadExpr(*Loc.F);
4310 } else if (EST == EST_Uninstantiated) {
4311 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
4312 EPI.ExceptionSpecTemplate = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
4313 } else if (EST == EST_Unevaluated) {
4314 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
4315 }
4316 return Context.getFunctionType(ResultType, ParamTypes.data(), NumParams,
4317 EPI);
4318 }
4319
4320 case TYPE_UNRESOLVED_USING: {
4321 unsigned Idx = 0;
4322 return Context.getTypeDeclType(
4323 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
4324 }
4325
4326 case TYPE_TYPEDEF: {
4327 if (Record.size() != 2) {
4328 Error("incorrect encoding of typedef type");
4329 return QualType();
4330 }
4331 unsigned Idx = 0;
4332 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
4333 QualType Canonical = readType(*Loc.F, Record, Idx);
4334 if (!Canonical.isNull())
4335 Canonical = Context.getCanonicalType(Canonical);
4336 return Context.getTypedefType(Decl, Canonical);
4337 }
4338
4339 case TYPE_TYPEOF_EXPR:
4340 return Context.getTypeOfExprType(ReadExpr(*Loc.F));
4341
4342 case TYPE_TYPEOF: {
4343 if (Record.size() != 1) {
4344 Error("incorrect encoding of typeof(type) in AST file");
4345 return QualType();
4346 }
4347 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
4348 return Context.getTypeOfType(UnderlyingType);
4349 }
4350
4351 case TYPE_DECLTYPE: {
4352 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
4353 return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
4354 }
4355
4356 case TYPE_UNARY_TRANSFORM: {
4357 QualType BaseType = readType(*Loc.F, Record, Idx);
4358 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
4359 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
4360 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
4361 }
4362
4363 case TYPE_AUTO:
4364 return Context.getAutoType(readType(*Loc.F, Record, Idx));
4365
4366 case TYPE_RECORD: {
4367 if (Record.size() != 2) {
4368 Error("incorrect encoding of record type");
4369 return QualType();
4370 }
4371 unsigned Idx = 0;
4372 bool IsDependent = Record[Idx++];
4373 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
4374 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
4375 QualType T = Context.getRecordType(RD);
4376 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
4377 return T;
4378 }
4379
4380 case TYPE_ENUM: {
4381 if (Record.size() != 2) {
4382 Error("incorrect encoding of enum type");
4383 return QualType();
4384 }
4385 unsigned Idx = 0;
4386 bool IsDependent = Record[Idx++];
4387 QualType T
4388 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
4389 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
4390 return T;
4391 }
4392
4393 case TYPE_ATTRIBUTED: {
4394 if (Record.size() != 3) {
4395 Error("incorrect encoding of attributed type");
4396 return QualType();
4397 }
4398 QualType modifiedType = readType(*Loc.F, Record, Idx);
4399 QualType equivalentType = readType(*Loc.F, Record, Idx);
4400 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
4401 return Context.getAttributedType(kind, modifiedType, equivalentType);
4402 }
4403
4404 case TYPE_PAREN: {
4405 if (Record.size() != 1) {
4406 Error("incorrect encoding of paren type");
4407 return QualType();
4408 }
4409 QualType InnerType = readType(*Loc.F, Record, Idx);
4410 return Context.getParenType(InnerType);
4411 }
4412
4413 case TYPE_PACK_EXPANSION: {
4414 if (Record.size() != 2) {
4415 Error("incorrect encoding of pack expansion type");
4416 return QualType();
4417 }
4418 QualType Pattern = readType(*Loc.F, Record, Idx);
4419 if (Pattern.isNull())
4420 return QualType();
4421 llvm::Optional<unsigned> NumExpansions;
4422 if (Record[1])
4423 NumExpansions = Record[1] - 1;
4424 return Context.getPackExpansionType(Pattern, NumExpansions);
4425 }
4426
4427 case TYPE_ELABORATED: {
4428 unsigned Idx = 0;
4429 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
4430 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
4431 QualType NamedType = readType(*Loc.F, Record, Idx);
4432 return Context.getElaboratedType(Keyword, NNS, NamedType);
4433 }
4434
4435 case TYPE_OBJC_INTERFACE: {
4436 unsigned Idx = 0;
4437 ObjCInterfaceDecl *ItfD
4438 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
4439 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
4440 }
4441
4442 case TYPE_OBJC_OBJECT: {
4443 unsigned Idx = 0;
4444 QualType Base = readType(*Loc.F, Record, Idx);
4445 unsigned NumProtos = Record[Idx++];
4446 SmallVector<ObjCProtocolDecl*, 4> Protos;
4447 for (unsigned I = 0; I != NumProtos; ++I)
4448 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
4449 return Context.getObjCObjectType(Base, Protos.data(), NumProtos);
4450 }
4451
4452 case TYPE_OBJC_OBJECT_POINTER: {
4453 unsigned Idx = 0;
4454 QualType Pointee = readType(*Loc.F, Record, Idx);
4455 return Context.getObjCObjectPointerType(Pointee);
4456 }
4457
4458 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
4459 unsigned Idx = 0;
4460 QualType Parm = readType(*Loc.F, Record, Idx);
4461 QualType Replacement = readType(*Loc.F, Record, Idx);
4462 return
4463 Context.getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
4464 Replacement);
4465 }
4466
4467 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
4468 unsigned Idx = 0;
4469 QualType Parm = readType(*Loc.F, Record, Idx);
4470 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
4471 return Context.getSubstTemplateTypeParmPackType(
4472 cast<TemplateTypeParmType>(Parm),
4473 ArgPack);
4474 }
4475
4476 case TYPE_INJECTED_CLASS_NAME: {
4477 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
4478 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
4479 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
4480 // for AST reading, too much interdependencies.
4481 return
4482 QualType(new (Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
4483 }
4484
4485 case TYPE_TEMPLATE_TYPE_PARM: {
4486 unsigned Idx = 0;
4487 unsigned Depth = Record[Idx++];
4488 unsigned Index = Record[Idx++];
4489 bool Pack = Record[Idx++];
4490 TemplateTypeParmDecl *D
4491 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
4492 return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
4493 }
4494
4495 case TYPE_DEPENDENT_NAME: {
4496 unsigned Idx = 0;
4497 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
4498 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
4499 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
4500 QualType Canon = readType(*Loc.F, Record, Idx);
4501 if (!Canon.isNull())
4502 Canon = Context.getCanonicalType(Canon);
4503 return Context.getDependentNameType(Keyword, NNS, Name, Canon);
4504 }
4505
4506 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
4507 unsigned Idx = 0;
4508 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
4509 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
4510 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
4511 unsigned NumArgs = Record[Idx++];
4512 SmallVector<TemplateArgument, 8> Args;
4513 Args.reserve(NumArgs);
4514 while (NumArgs--)
4515 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
4516 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
4517 Args.size(), Args.data());
4518 }
4519
4520 case TYPE_DEPENDENT_SIZED_ARRAY: {
4521 unsigned Idx = 0;
4522
4523 // ArrayType
4524 QualType ElementType = readType(*Loc.F, Record, Idx);
4525 ArrayType::ArraySizeModifier ASM
4526 = (ArrayType::ArraySizeModifier)Record[Idx++];
4527 unsigned IndexTypeQuals = Record[Idx++];
4528
4529 // DependentSizedArrayType
4530 Expr *NumElts = ReadExpr(*Loc.F);
4531 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
4532
4533 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
4534 IndexTypeQuals, Brackets);
4535 }
4536
4537 case TYPE_TEMPLATE_SPECIALIZATION: {
4538 unsigned Idx = 0;
4539 bool IsDependent = Record[Idx++];
4540 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
4541 SmallVector<TemplateArgument, 8> Args;
4542 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
4543 QualType Underlying = readType(*Loc.F, Record, Idx);
4544 QualType T;
4545 if (Underlying.isNull())
4546 T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(),
4547 Args.size());
4548 else
4549 T = Context.getTemplateSpecializationType(Name, Args.data(),
4550 Args.size(), Underlying);
4551 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
4552 return T;
4553 }
4554
4555 case TYPE_ATOMIC: {
4556 if (Record.size() != 1) {
4557 Error("Incorrect encoding of atomic type");
4558 return QualType();
4559 }
4560 QualType ValueType = readType(*Loc.F, Record, Idx);
4561 return Context.getAtomicType(ValueType);
4562 }
4563 }
4564 llvm_unreachable("Invalid TypeCode!");
4565}
4566
4567class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
4568 ASTReader &Reader;
4569 ModuleFile &F;
4570 const ASTReader::RecordData &Record;
4571 unsigned &Idx;
4572
4573 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
4574 unsigned &I) {
4575 return Reader.ReadSourceLocation(F, R, I);
4576 }
4577
4578 template<typename T>
4579 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
4580 return Reader.ReadDeclAs<T>(F, Record, Idx);
4581 }
4582
4583public:
4584 TypeLocReader(ASTReader &Reader, ModuleFile &F,
4585 const ASTReader::RecordData &Record, unsigned &Idx)
4586 : Reader(Reader), F(F), Record(Record), Idx(Idx)
4587 { }
4588
4589 // We want compile-time assurance that we've enumerated all of
4590 // these, so unfortunately we have to declare them first, then
4591 // define them out-of-line.
4592#define ABSTRACT_TYPELOC(CLASS, PARENT)
4593#define TYPELOC(CLASS, PARENT) \
4594 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
4595#include "clang/AST/TypeLocNodes.def"
4596
4597 void VisitFunctionTypeLoc(FunctionTypeLoc);
4598 void VisitArrayTypeLoc(ArrayTypeLoc);
4599};
4600
4601void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
4602 // nothing to do
4603}
4604void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
4605 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
4606 if (TL.needsExtraLocalData()) {
4607 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
4608 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
4609 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
4610 TL.setModeAttr(Record[Idx++]);
4611 }
4612}
4613void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
4614 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4615}
4616void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
4617 TL.setStarLoc(ReadSourceLocation(Record, Idx));
4618}
4619void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
4620 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
4621}
4622void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
4623 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
4624}
4625void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
4626 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
4627}
4628void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
4629 TL.setStarLoc(ReadSourceLocation(Record, Idx));
4630 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
4631}
4632void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
4633 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
4634 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
4635 if (Record[Idx++])
4636 TL.setSizeExpr(Reader.ReadExpr(F));
4637 else
4638 TL.setSizeExpr(0);
4639}
4640void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
4641 VisitArrayTypeLoc(TL);
4642}
4643void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
4644 VisitArrayTypeLoc(TL);
4645}
4646void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
4647 VisitArrayTypeLoc(TL);
4648}
4649void TypeLocReader::VisitDependentSizedArrayTypeLoc(
4650 DependentSizedArrayTypeLoc TL) {
4651 VisitArrayTypeLoc(TL);
4652}
4653void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
4654 DependentSizedExtVectorTypeLoc TL) {
4655 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4656}
4657void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
4658 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4659}
4660void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
4661 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4662}
4663void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
4664 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
4665 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4666 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4667 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
4668 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
4669 TL.setArg(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
4670 }
4671}
4672void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
4673 VisitFunctionTypeLoc(TL);
4674}
4675void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
4676 VisitFunctionTypeLoc(TL);
4677}
4678void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
4679 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4680}
4681void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
4682 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4683}
4684void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
4685 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
4686 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4687 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4688}
4689void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
4690 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
4691 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4692 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4693 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
4694}
4695void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
4696 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4697}
4698void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
4699 TL.setKWLoc(ReadSourceLocation(Record, Idx));
4700 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4701 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4702 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
4703}
4704void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
4705 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4706}
4707void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
4708 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4709}
4710void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
4711 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4712}
4713void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
4714 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
4715 if (TL.hasAttrOperand()) {
4716 SourceRange range;
4717 range.setBegin(ReadSourceLocation(Record, Idx));
4718 range.setEnd(ReadSourceLocation(Record, Idx));
4719 TL.setAttrOperandParensRange(range);
4720 }
4721 if (TL.hasAttrExprOperand()) {
4722 if (Record[Idx++])
4723 TL.setAttrExprOperand(Reader.ReadExpr(F));
4724 else
4725 TL.setAttrExprOperand(0);
4726 } else if (TL.hasAttrEnumOperand())
4727 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
4728}
4729void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
4730 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4731}
4732void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
4733 SubstTemplateTypeParmTypeLoc TL) {
4734 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4735}
4736void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
4737 SubstTemplateTypeParmPackTypeLoc TL) {
4738 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4739}
4740void TypeLocReader::VisitTemplateSpecializationTypeLoc(
4741 TemplateSpecializationTypeLoc TL) {
4742 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
4743 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
4744 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4745 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
4746 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
4747 TL.setArgLocInfo(i,
4748 Reader.GetTemplateArgumentLocInfo(F,
4749 TL.getTypePtr()->getArg(i).getKind(),
4750 Record, Idx));
4751}
4752void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
4753 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4754 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4755}
4756void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
4757 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
4758 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
4759}
4760void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
4761 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4762}
4763void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
4764 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
4765 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
4766 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4767}
4768void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
4769 DependentTemplateSpecializationTypeLoc TL) {
4770 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
4771 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
4772 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
4773 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
4774 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4775 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
4776 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
4777 TL.setArgLocInfo(I,
4778 Reader.GetTemplateArgumentLocInfo(F,
4779 TL.getTypePtr()->getArg(I).getKind(),
4780 Record, Idx));
4781}
4782void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
4783 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
4784}
4785void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
4786 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4787}
4788void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
4789 TL.setHasBaseTypeAsWritten(Record[Idx++]);
4790 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4791 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
4792 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
4793 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
4794}
4795void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
4796 TL.setStarLoc(ReadSourceLocation(Record, Idx));
4797}
4798void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
4799 TL.setKWLoc(ReadSourceLocation(Record, Idx));
4800 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4801 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4802}
4803
4804TypeSourceInfo *ASTReader::GetTypeSourceInfo(ModuleFile &F,
4805 const RecordData &Record,
4806 unsigned &Idx) {
4807 QualType InfoTy = readType(F, Record, Idx);
4808 if (InfoTy.isNull())
4809 return 0;
4810
4811 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
4812 TypeLocReader TLR(*this, F, Record, Idx);
4813 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
4814 TLR.Visit(TL);
4815 return TInfo;
4816}
4817
4818QualType ASTReader::GetType(TypeID ID) {
4819 unsigned FastQuals = ID & Qualifiers::FastMask;
4820 unsigned Index = ID >> Qualifiers::FastWidth;
4821
4822 if (Index < NUM_PREDEF_TYPE_IDS) {
4823 QualType T;
4824 switch ((PredefinedTypeIDs)Index) {
4825 case PREDEF_TYPE_NULL_ID: return QualType();
4826 case PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
4827 case PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
4828
4829 case PREDEF_TYPE_CHAR_U_ID:
4830 case PREDEF_TYPE_CHAR_S_ID:
4831 // FIXME: Check that the signedness of CharTy is correct!
4832 T = Context.CharTy;
4833 break;
4834
4835 case PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
4836 case PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
4837 case PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
4838 case PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
4839 case PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
4840 case PREDEF_TYPE_UINT128_ID: T = Context.UnsignedInt128Ty; break;
4841 case PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
4842 case PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
4843 case PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
4844 case PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
4845 case PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
4846 case PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
4847 case PREDEF_TYPE_INT128_ID: T = Context.Int128Ty; break;
4848 case PREDEF_TYPE_HALF_ID: T = Context.HalfTy; break;
4849 case PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
4850 case PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
4851 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
4852 case PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
4853 case PREDEF_TYPE_BOUND_MEMBER: T = Context.BoundMemberTy; break;
4854 case PREDEF_TYPE_PSEUDO_OBJECT: T = Context.PseudoObjectTy; break;
4855 case PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
4856 case PREDEF_TYPE_UNKNOWN_ANY: T = Context.UnknownAnyTy; break;
4857 case PREDEF_TYPE_NULLPTR_ID: T = Context.NullPtrTy; break;
4858 case PREDEF_TYPE_CHAR16_ID: T = Context.Char16Ty; break;
4859 case PREDEF_TYPE_CHAR32_ID: T = Context.Char32Ty; break;
4860 case PREDEF_TYPE_OBJC_ID: T = Context.ObjCBuiltinIdTy; break;
4861 case PREDEF_TYPE_OBJC_CLASS: T = Context.ObjCBuiltinClassTy; break;
4862 case PREDEF_TYPE_OBJC_SEL: T = Context.ObjCBuiltinSelTy; break;
Guy Benyeib13621d2012-12-18 14:38:23 +00004863 case PREDEF_TYPE_IMAGE1D_ID: T = Context.OCLImage1dTy; break;
4864 case PREDEF_TYPE_IMAGE1D_ARR_ID: T = Context.OCLImage1dArrayTy; break;
4865 case PREDEF_TYPE_IMAGE1D_BUFF_ID: T = Context.OCLImage1dBufferTy; break;
4866 case PREDEF_TYPE_IMAGE2D_ID: T = Context.OCLImage2dTy; break;
4867 case PREDEF_TYPE_IMAGE2D_ARR_ID: T = Context.OCLImage2dArrayTy; break;
4868 case PREDEF_TYPE_IMAGE3D_ID: T = Context.OCLImage3dTy; break;
Guy Benyeie6b9d802013-01-20 12:31:11 +00004869 case PREDEF_TYPE_EVENT_ID: T = Context.OCLEventTy; break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004870 case PREDEF_TYPE_AUTO_DEDUCT: T = Context.getAutoDeductType(); break;
4871
4872 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
4873 T = Context.getAutoRRefDeductType();
4874 break;
4875
4876 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
4877 T = Context.ARCUnbridgedCastTy;
4878 break;
4879
4880 case PREDEF_TYPE_VA_LIST_TAG:
4881 T = Context.getVaListTagType();
4882 break;
4883
4884 case PREDEF_TYPE_BUILTIN_FN:
4885 T = Context.BuiltinFnTy;
4886 break;
4887 }
4888
4889 assert(!T.isNull() && "Unknown predefined type");
4890 return T.withFastQualifiers(FastQuals);
4891 }
4892
4893 Index -= NUM_PREDEF_TYPE_IDS;
4894 assert(Index < TypesLoaded.size() && "Type index out-of-range");
4895 if (TypesLoaded[Index].isNull()) {
4896 TypesLoaded[Index] = readTypeRecord(Index);
4897 if (TypesLoaded[Index].isNull())
4898 return QualType();
4899
4900 TypesLoaded[Index]->setFromAST();
4901 if (DeserializationListener)
4902 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
4903 TypesLoaded[Index]);
4904 }
4905
4906 return TypesLoaded[Index].withFastQualifiers(FastQuals);
4907}
4908
4909QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
4910 return GetType(getGlobalTypeID(F, LocalID));
4911}
4912
4913serialization::TypeID
4914ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
4915 unsigned FastQuals = LocalID & Qualifiers::FastMask;
4916 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
4917
4918 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
4919 return LocalID;
4920
4921 ContinuousRangeMap<uint32_t, int, 2>::iterator I
4922 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
4923 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
4924
4925 unsigned GlobalIndex = LocalIndex + I->second;
4926 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
4927}
4928
4929TemplateArgumentLocInfo
4930ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
4931 TemplateArgument::ArgKind Kind,
4932 const RecordData &Record,
4933 unsigned &Index) {
4934 switch (Kind) {
4935 case TemplateArgument::Expression:
4936 return ReadExpr(F);
4937 case TemplateArgument::Type:
4938 return GetTypeSourceInfo(F, Record, Index);
4939 case TemplateArgument::Template: {
4940 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
4941 Index);
4942 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
4943 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
4944 SourceLocation());
4945 }
4946 case TemplateArgument::TemplateExpansion: {
4947 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
4948 Index);
4949 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
4950 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
4951 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
4952 EllipsisLoc);
4953 }
4954 case TemplateArgument::Null:
4955 case TemplateArgument::Integral:
4956 case TemplateArgument::Declaration:
4957 case TemplateArgument::NullPtr:
4958 case TemplateArgument::Pack:
4959 // FIXME: Is this right?
4960 return TemplateArgumentLocInfo();
4961 }
4962 llvm_unreachable("unexpected template argument loc");
4963}
4964
4965TemplateArgumentLoc
4966ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
4967 const RecordData &Record, unsigned &Index) {
4968 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
4969
4970 if (Arg.getKind() == TemplateArgument::Expression) {
4971 if (Record[Index++]) // bool InfoHasSameExpr.
4972 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
4973 }
4974 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
4975 Record, Index));
4976}
4977
4978Decl *ASTReader::GetExternalDecl(uint32_t ID) {
4979 return GetDecl(ID);
4980}
4981
4982uint64_t ASTReader::readCXXBaseSpecifiers(ModuleFile &M, const RecordData &Record,
4983 unsigned &Idx){
4984 if (Idx >= Record.size())
4985 return 0;
4986
4987 unsigned LocalID = Record[Idx++];
4988 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
4989}
4990
4991CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
4992 RecordLocation Loc = getLocalBitOffset(Offset);
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004993 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004994 SavedStreamPosition SavedPosition(Cursor);
4995 Cursor.JumpToBit(Loc.Offset);
4996 ReadingKindTracker ReadingKind(Read_Decl, *this);
4997 RecordData Record;
4998 unsigned Code = Cursor.ReadCode();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00004999 unsigned RecCode = Cursor.readRecord(Code, Record);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005000 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
5001 Error("Malformed AST file: missing C++ base specifiers");
5002 return 0;
5003 }
5004
5005 unsigned Idx = 0;
5006 unsigned NumBases = Record[Idx++];
5007 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
5008 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
5009 for (unsigned I = 0; I != NumBases; ++I)
5010 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
5011 return Bases;
5012}
5013
5014serialization::DeclID
5015ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
5016 if (LocalID < NUM_PREDEF_DECL_IDS)
5017 return LocalID;
5018
5019 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5020 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
5021 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
5022
5023 return LocalID + I->second;
5024}
5025
5026bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
5027 ModuleFile &M) const {
5028 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(ID);
5029 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5030 return &M == I->second;
5031}
5032
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00005033ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005034 if (!D->isFromASTFile())
5035 return 0;
5036 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
5037 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5038 return I->second;
5039}
5040
5041SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
5042 if (ID < NUM_PREDEF_DECL_IDS)
5043 return SourceLocation();
5044
5045 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
5046
5047 if (Index > DeclsLoaded.size()) {
5048 Error("declaration ID out-of-range for AST file");
5049 return SourceLocation();
5050 }
5051
5052 if (Decl *D = DeclsLoaded[Index])
5053 return D->getLocation();
5054
5055 unsigned RawLocation = 0;
5056 RecordLocation Rec = DeclCursorForID(ID, RawLocation);
5057 return ReadSourceLocation(*Rec.F, RawLocation);
5058}
5059
5060Decl *ASTReader::GetDecl(DeclID ID) {
5061 if (ID < NUM_PREDEF_DECL_IDS) {
5062 switch ((PredefinedDeclIDs)ID) {
5063 case PREDEF_DECL_NULL_ID:
5064 return 0;
5065
5066 case PREDEF_DECL_TRANSLATION_UNIT_ID:
5067 return Context.getTranslationUnitDecl();
5068
5069 case PREDEF_DECL_OBJC_ID_ID:
5070 return Context.getObjCIdDecl();
5071
5072 case PREDEF_DECL_OBJC_SEL_ID:
5073 return Context.getObjCSelDecl();
5074
5075 case PREDEF_DECL_OBJC_CLASS_ID:
5076 return Context.getObjCClassDecl();
5077
5078 case PREDEF_DECL_OBJC_PROTOCOL_ID:
5079 return Context.getObjCProtocolDecl();
5080
5081 case PREDEF_DECL_INT_128_ID:
5082 return Context.getInt128Decl();
5083
5084 case PREDEF_DECL_UNSIGNED_INT_128_ID:
5085 return Context.getUInt128Decl();
5086
5087 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
5088 return Context.getObjCInstanceTypeDecl();
5089
5090 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
5091 return Context.getBuiltinVaListDecl();
5092 }
5093 }
5094
5095 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
5096
5097 if (Index >= DeclsLoaded.size()) {
5098 assert(0 && "declaration ID out-of-range for AST file");
5099 Error("declaration ID out-of-range for AST file");
5100 return 0;
5101 }
5102
5103 if (!DeclsLoaded[Index]) {
5104 ReadDeclRecord(ID);
5105 if (DeserializationListener)
5106 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
5107 }
5108
5109 return DeclsLoaded[Index];
5110}
5111
5112DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
5113 DeclID GlobalID) {
5114 if (GlobalID < NUM_PREDEF_DECL_IDS)
5115 return GlobalID;
5116
5117 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
5118 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5119 ModuleFile *Owner = I->second;
5120
5121 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
5122 = M.GlobalToLocalDeclIDs.find(Owner);
5123 if (Pos == M.GlobalToLocalDeclIDs.end())
5124 return 0;
5125
5126 return GlobalID - Owner->BaseDeclID + Pos->second;
5127}
5128
5129serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
5130 const RecordData &Record,
5131 unsigned &Idx) {
5132 if (Idx >= Record.size()) {
5133 Error("Corrupted AST file");
5134 return 0;
5135 }
5136
5137 return getGlobalDeclID(F, Record[Idx++]);
5138}
5139
5140/// \brief Resolve the offset of a statement into a statement.
5141///
5142/// This operation will read a new statement from the external
5143/// source each time it is called, and is meant to be used via a
5144/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
5145Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
5146 // Switch case IDs are per Decl.
5147 ClearSwitchCaseIDs();
5148
5149 // Offset here is a global offset across the entire chain.
5150 RecordLocation Loc = getLocalBitOffset(Offset);
5151 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
5152 return ReadStmtFromStream(*Loc.F);
5153}
5154
5155namespace {
5156 class FindExternalLexicalDeclsVisitor {
5157 ASTReader &Reader;
5158 const DeclContext *DC;
5159 bool (*isKindWeWant)(Decl::Kind);
5160
5161 SmallVectorImpl<Decl*> &Decls;
5162 bool PredefsVisited[NUM_PREDEF_DECL_IDS];
5163
5164 public:
5165 FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC,
5166 bool (*isKindWeWant)(Decl::Kind),
5167 SmallVectorImpl<Decl*> &Decls)
5168 : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls)
5169 {
5170 for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I)
5171 PredefsVisited[I] = false;
5172 }
5173
5174 static bool visit(ModuleFile &M, bool Preorder, void *UserData) {
5175 if (Preorder)
5176 return false;
5177
5178 FindExternalLexicalDeclsVisitor *This
5179 = static_cast<FindExternalLexicalDeclsVisitor *>(UserData);
5180
5181 ModuleFile::DeclContextInfosMap::iterator Info
5182 = M.DeclContextInfos.find(This->DC);
5183 if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls)
5184 return false;
5185
5186 // Load all of the declaration IDs
5187 for (const KindDeclIDPair *ID = Info->second.LexicalDecls,
5188 *IDE = ID + Info->second.NumLexicalDecls;
5189 ID != IDE; ++ID) {
5190 if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first))
5191 continue;
5192
5193 // Don't add predefined declarations to the lexical context more
5194 // than once.
5195 if (ID->second < NUM_PREDEF_DECL_IDS) {
5196 if (This->PredefsVisited[ID->second])
5197 continue;
5198
5199 This->PredefsVisited[ID->second] = true;
5200 }
5201
5202 if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) {
5203 if (!This->DC->isDeclInLexicalTraversal(D))
5204 This->Decls.push_back(D);
5205 }
5206 }
5207
5208 return false;
5209 }
5210 };
5211}
5212
5213ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
5214 bool (*isKindWeWant)(Decl::Kind),
5215 SmallVectorImpl<Decl*> &Decls) {
5216 // There might be lexical decls in multiple modules, for the TU at
5217 // least. Walk all of the modules in the order they were loaded.
5218 FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls);
5219 ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor);
5220 ++NumLexicalDeclContextsRead;
5221 return ELR_Success;
5222}
5223
5224namespace {
5225
5226class DeclIDComp {
5227 ASTReader &Reader;
5228 ModuleFile &Mod;
5229
5230public:
5231 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
5232
5233 bool operator()(LocalDeclID L, LocalDeclID R) const {
5234 SourceLocation LHS = getLocation(L);
5235 SourceLocation RHS = getLocation(R);
5236 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5237 }
5238
5239 bool operator()(SourceLocation LHS, LocalDeclID R) const {
5240 SourceLocation RHS = getLocation(R);
5241 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5242 }
5243
5244 bool operator()(LocalDeclID L, SourceLocation RHS) const {
5245 SourceLocation LHS = getLocation(L);
5246 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5247 }
5248
5249 SourceLocation getLocation(LocalDeclID ID) const {
5250 return Reader.getSourceManager().getFileLoc(
5251 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
5252 }
5253};
5254
5255}
5256
5257void ASTReader::FindFileRegionDecls(FileID File,
5258 unsigned Offset, unsigned Length,
5259 SmallVectorImpl<Decl *> &Decls) {
5260 SourceManager &SM = getSourceManager();
5261
5262 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
5263 if (I == FileDeclIDs.end())
5264 return;
5265
5266 FileDeclsInfo &DInfo = I->second;
5267 if (DInfo.Decls.empty())
5268 return;
5269
5270 SourceLocation
5271 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
5272 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
5273
5274 DeclIDComp DIDComp(*this, *DInfo.Mod);
5275 ArrayRef<serialization::LocalDeclID>::iterator
5276 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
5277 BeginLoc, DIDComp);
5278 if (BeginIt != DInfo.Decls.begin())
5279 --BeginIt;
5280
5281 // If we are pointing at a top-level decl inside an objc container, we need
5282 // to backtrack until we find it otherwise we will fail to report that the
5283 // region overlaps with an objc container.
5284 while (BeginIt != DInfo.Decls.begin() &&
5285 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
5286 ->isTopLevelDeclInObjCContainer())
5287 --BeginIt;
5288
5289 ArrayRef<serialization::LocalDeclID>::iterator
5290 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
5291 EndLoc, DIDComp);
5292 if (EndIt != DInfo.Decls.end())
5293 ++EndIt;
5294
5295 for (ArrayRef<serialization::LocalDeclID>::iterator
5296 DIt = BeginIt; DIt != EndIt; ++DIt)
5297 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
5298}
5299
5300namespace {
5301 /// \brief ModuleFile visitor used to perform name lookup into a
5302 /// declaration context.
5303 class DeclContextNameLookupVisitor {
5304 ASTReader &Reader;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00005305 SmallVectorImpl<const DeclContext *> &Contexts;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005306 DeclarationName Name;
5307 SmallVectorImpl<NamedDecl *> &Decls;
5308
5309 public:
5310 DeclContextNameLookupVisitor(ASTReader &Reader,
5311 SmallVectorImpl<const DeclContext *> &Contexts,
5312 DeclarationName Name,
5313 SmallVectorImpl<NamedDecl *> &Decls)
5314 : Reader(Reader), Contexts(Contexts), Name(Name), Decls(Decls) { }
5315
5316 static bool visit(ModuleFile &M, void *UserData) {
5317 DeclContextNameLookupVisitor *This
5318 = static_cast<DeclContextNameLookupVisitor *>(UserData);
5319
5320 // Check whether we have any visible declaration information for
5321 // this context in this module.
5322 ModuleFile::DeclContextInfosMap::iterator Info;
5323 bool FoundInfo = false;
5324 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5325 Info = M.DeclContextInfos.find(This->Contexts[I]);
5326 if (Info != M.DeclContextInfos.end() &&
5327 Info->second.NameLookupTableData) {
5328 FoundInfo = true;
5329 break;
5330 }
5331 }
5332
5333 if (!FoundInfo)
5334 return false;
5335
5336 // Look for this name within this module.
5337 ASTDeclContextNameLookupTable *LookupTable =
5338 Info->second.NameLookupTableData;
5339 ASTDeclContextNameLookupTable::iterator Pos
5340 = LookupTable->find(This->Name);
5341 if (Pos == LookupTable->end())
5342 return false;
5343
5344 bool FoundAnything = false;
5345 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
5346 for (; Data.first != Data.second; ++Data.first) {
5347 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first);
5348 if (!ND)
5349 continue;
5350
5351 if (ND->getDeclName() != This->Name) {
5352 // A name might be null because the decl's redeclarable part is
5353 // currently read before reading its name. The lookup is triggered by
5354 // building that decl (likely indirectly), and so it is later in the
5355 // sense of "already existing" and can be ignored here.
5356 continue;
5357 }
5358
5359 // Record this declaration.
5360 FoundAnything = true;
5361 This->Decls.push_back(ND);
5362 }
5363
5364 return FoundAnything;
5365 }
5366 };
5367}
5368
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00005369/// \brief Retrieve the "definitive" module file for the definition of the
5370/// given declaration context, if there is one.
5371///
5372/// The "definitive" module file is the only place where we need to look to
5373/// find information about the declarations within the given declaration
5374/// context. For example, C++ and Objective-C classes, C structs/unions, and
5375/// Objective-C protocols, categories, and extensions are all defined in a
5376/// single place in the source code, so they have definitive module files
5377/// associated with them. C++ namespaces, on the other hand, can have
5378/// definitions in multiple different module files.
5379///
5380/// Note: this needs to be kept in sync with ASTWriter::AddedVisibleDecl's
5381/// NDEBUG checking.
5382static ModuleFile *getDefinitiveModuleFileFor(const DeclContext *DC,
5383 ASTReader &Reader) {
Douglas Gregore0d20662013-01-22 17:08:30 +00005384 if (const DeclContext *DefDC = getDefinitiveDeclContext(DC))
5385 return Reader.getOwningModuleFile(cast<Decl>(DefDC));
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00005386
5387 return 0;
5388}
5389
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005390DeclContext::lookup_result
5391ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
5392 DeclarationName Name) {
5393 assert(DC->hasExternalVisibleStorage() &&
5394 "DeclContext has no visible decls in storage");
5395 if (!Name)
5396 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
5397 DeclContext::lookup_iterator(0));
5398
5399 SmallVector<NamedDecl *, 64> Decls;
5400
5401 // Compute the declaration contexts we need to look into. Multiple such
5402 // declaration contexts occur when two declaration contexts from disjoint
5403 // modules get merged, e.g., when two namespaces with the same name are
5404 // independently defined in separate modules.
5405 SmallVector<const DeclContext *, 2> Contexts;
5406 Contexts.push_back(DC);
5407
5408 if (DC->isNamespace()) {
5409 MergedDeclsMap::iterator Merged
5410 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5411 if (Merged != MergedDecls.end()) {
5412 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5413 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5414 }
5415 }
5416
5417 DeclContextNameLookupVisitor Visitor(*this, Contexts, Name, Decls);
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00005418
5419 // If we can definitively determine which module file to look into,
5420 // only look there. Otherwise, look in all module files.
5421 ModuleFile *Definitive;
5422 if (Contexts.size() == 1 &&
5423 (Definitive = getDefinitiveModuleFileFor(DC, *this))) {
5424 DeclContextNameLookupVisitor::visit(*Definitive, &Visitor);
5425 } else {
5426 ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor);
5427 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005428 ++NumVisibleDeclContextsRead;
5429 SetExternalVisibleDeclsForName(DC, Name, Decls);
5430 return const_cast<DeclContext*>(DC)->lookup(Name);
5431}
5432
5433namespace {
5434 /// \brief ModuleFile visitor used to retrieve all visible names in a
5435 /// declaration context.
5436 class DeclContextAllNamesVisitor {
5437 ASTReader &Reader;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00005438 SmallVectorImpl<const DeclContext *> &Contexts;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005439 llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> > &Decls;
Argyrios Kyrtzidisca40f302012-12-19 22:21:18 +00005440 bool VisitAll;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005441
5442 public:
5443 DeclContextAllNamesVisitor(ASTReader &Reader,
5444 SmallVectorImpl<const DeclContext *> &Contexts,
5445 llvm::DenseMap<DeclarationName,
Argyrios Kyrtzidisca40f302012-12-19 22:21:18 +00005446 SmallVector<NamedDecl *, 8> > &Decls,
5447 bool VisitAll)
5448 : Reader(Reader), Contexts(Contexts), Decls(Decls), VisitAll(VisitAll) { }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005449
5450 static bool visit(ModuleFile &M, void *UserData) {
5451 DeclContextAllNamesVisitor *This
5452 = static_cast<DeclContextAllNamesVisitor *>(UserData);
5453
5454 // Check whether we have any visible declaration information for
5455 // this context in this module.
5456 ModuleFile::DeclContextInfosMap::iterator Info;
5457 bool FoundInfo = false;
5458 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5459 Info = M.DeclContextInfos.find(This->Contexts[I]);
5460 if (Info != M.DeclContextInfos.end() &&
5461 Info->second.NameLookupTableData) {
5462 FoundInfo = true;
5463 break;
5464 }
5465 }
5466
5467 if (!FoundInfo)
5468 return false;
5469
5470 ASTDeclContextNameLookupTable *LookupTable =
5471 Info->second.NameLookupTableData;
5472 bool FoundAnything = false;
5473 for (ASTDeclContextNameLookupTable::data_iterator
Douglas Gregora6b00fc2013-01-23 22:38:11 +00005474 I = LookupTable->data_begin(), E = LookupTable->data_end();
5475 I != E;
5476 ++I) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005477 ASTDeclContextNameLookupTrait::data_type Data = *I;
5478 for (; Data.first != Data.second; ++Data.first) {
5479 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M,
5480 *Data.first);
5481 if (!ND)
5482 continue;
5483
5484 // Record this declaration.
5485 FoundAnything = true;
5486 This->Decls[ND->getDeclName()].push_back(ND);
5487 }
5488 }
5489
Argyrios Kyrtzidisca40f302012-12-19 22:21:18 +00005490 return FoundAnything && !This->VisitAll;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005491 }
5492 };
5493}
5494
5495void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
5496 if (!DC->hasExternalVisibleStorage())
5497 return;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00005498 llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> > Decls;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005499
5500 // Compute the declaration contexts we need to look into. Multiple such
5501 // declaration contexts occur when two declaration contexts from disjoint
5502 // modules get merged, e.g., when two namespaces with the same name are
5503 // independently defined in separate modules.
5504 SmallVector<const DeclContext *, 2> Contexts;
5505 Contexts.push_back(DC);
5506
5507 if (DC->isNamespace()) {
5508 MergedDeclsMap::iterator Merged
5509 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5510 if (Merged != MergedDecls.end()) {
5511 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5512 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5513 }
5514 }
5515
Argyrios Kyrtzidisca40f302012-12-19 22:21:18 +00005516 DeclContextAllNamesVisitor Visitor(*this, Contexts, Decls,
5517 /*VisitAll=*/DC->isFileContext());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005518 ModuleMgr.visit(&DeclContextAllNamesVisitor::visit, &Visitor);
5519 ++NumVisibleDeclContextsRead;
5520
5521 for (llvm::DenseMap<DeclarationName,
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00005522 SmallVector<NamedDecl *, 8> >::iterator
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005523 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
5524 SetExternalVisibleDeclsForName(DC, I->first, I->second);
5525 }
5526 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
5527}
5528
5529/// \brief Under non-PCH compilation the consumer receives the objc methods
5530/// before receiving the implementation, and codegen depends on this.
5531/// We simulate this by deserializing and passing to consumer the methods of the
5532/// implementation before passing the deserialized implementation decl.
5533static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
5534 ASTConsumer *Consumer) {
5535 assert(ImplD && Consumer);
5536
5537 for (ObjCImplDecl::method_iterator
5538 I = ImplD->meth_begin(), E = ImplD->meth_end(); I != E; ++I)
5539 Consumer->HandleInterestingDecl(DeclGroupRef(*I));
5540
5541 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
5542}
5543
5544void ASTReader::PassInterestingDeclsToConsumer() {
5545 assert(Consumer);
5546 while (!InterestingDecls.empty()) {
5547 Decl *D = InterestingDecls.front();
5548 InterestingDecls.pop_front();
5549
5550 PassInterestingDeclToConsumer(D);
5551 }
5552}
5553
5554void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
5555 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
5556 PassObjCImplDeclToConsumer(ImplD, Consumer);
5557 else
5558 Consumer->HandleInterestingDecl(DeclGroupRef(D));
5559}
5560
5561void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
5562 this->Consumer = Consumer;
5563
5564 if (!Consumer)
5565 return;
5566
5567 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
5568 // Force deserialization of this decl, which will cause it to be queued for
5569 // passing to the consumer.
5570 GetDecl(ExternalDefinitions[I]);
5571 }
5572 ExternalDefinitions.clear();
5573
5574 PassInterestingDeclsToConsumer();
5575}
5576
5577void ASTReader::PrintStats() {
5578 std::fprintf(stderr, "*** AST File Statistics:\n");
5579
5580 unsigned NumTypesLoaded
5581 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
5582 QualType());
5583 unsigned NumDeclsLoaded
5584 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
5585 (Decl *)0);
5586 unsigned NumIdentifiersLoaded
5587 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
5588 IdentifiersLoaded.end(),
5589 (IdentifierInfo *)0);
5590 unsigned NumMacrosLoaded
5591 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
5592 MacrosLoaded.end(),
5593 (MacroInfo *)0);
5594 unsigned NumSelectorsLoaded
5595 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
5596 SelectorsLoaded.end(),
5597 Selector());
5598
5599 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
5600 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
5601 NumSLocEntriesRead, TotalNumSLocEntries,
5602 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
5603 if (!TypesLoaded.empty())
5604 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
5605 NumTypesLoaded, (unsigned)TypesLoaded.size(),
5606 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
5607 if (!DeclsLoaded.empty())
5608 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
5609 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
5610 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
5611 if (!IdentifiersLoaded.empty())
5612 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
5613 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
5614 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
5615 if (!MacrosLoaded.empty())
5616 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
5617 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
5618 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
5619 if (!SelectorsLoaded.empty())
5620 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
5621 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
5622 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
5623 if (TotalNumStatements)
5624 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
5625 NumStatementsRead, TotalNumStatements,
5626 ((float)NumStatementsRead/TotalNumStatements * 100));
5627 if (TotalNumMacros)
5628 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
5629 NumMacrosRead, TotalNumMacros,
5630 ((float)NumMacrosRead/TotalNumMacros * 100));
5631 if (TotalLexicalDeclContexts)
5632 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
5633 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
5634 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
5635 * 100));
5636 if (TotalVisibleDeclContexts)
5637 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
5638 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
5639 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
5640 * 100));
5641 if (TotalNumMethodPoolEntries) {
5642 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
5643 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
5644 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
5645 * 100));
5646 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
5647 }
Douglas Gregore1698072013-01-25 00:38:33 +00005648 if (NumIdentifierLookupHits) {
5649 std::fprintf(stderr,
5650 " %u / %u identifier table lookups succeeded (%f%%)\n",
5651 NumIdentifierLookupHits, NumIdentifierLookups,
5652 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
5653 }
5654
Douglas Gregor1a49d972013-01-25 01:03:03 +00005655 if (GlobalIndex) {
5656 std::fprintf(stderr, "\n");
5657 GlobalIndex->printStats();
5658 }
5659
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005660 std::fprintf(stderr, "\n");
5661 dump();
5662 std::fprintf(stderr, "\n");
5663}
5664
5665template<typename Key, typename ModuleFile, unsigned InitialCapacity>
5666static void
5667dumpModuleIDMap(StringRef Name,
5668 const ContinuousRangeMap<Key, ModuleFile *,
5669 InitialCapacity> &Map) {
5670 if (Map.begin() == Map.end())
5671 return;
5672
5673 typedef ContinuousRangeMap<Key, ModuleFile *, InitialCapacity> MapType;
5674 llvm::errs() << Name << ":\n";
5675 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
5676 I != IEnd; ++I) {
5677 llvm::errs() << " " << I->first << " -> " << I->second->FileName
5678 << "\n";
5679 }
5680}
5681
5682void ASTReader::dump() {
5683 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
5684 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
5685 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
5686 dumpModuleIDMap("Global type map", GlobalTypeMap);
5687 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
5688 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
5689 dumpModuleIDMap("Global macro map", GlobalMacroMap);
5690 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
5691 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
5692 dumpModuleIDMap("Global preprocessed entity map",
5693 GlobalPreprocessedEntityMap);
5694
5695 llvm::errs() << "\n*** PCH/Modules Loaded:";
5696 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
5697 MEnd = ModuleMgr.end();
5698 M != MEnd; ++M)
5699 (*M)->dump();
5700}
5701
5702/// Return the amount of memory used by memory buffers, breaking down
5703/// by heap-backed versus mmap'ed memory.
5704void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
5705 for (ModuleConstIterator I = ModuleMgr.begin(),
5706 E = ModuleMgr.end(); I != E; ++I) {
5707 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
5708 size_t bytes = buf->getBufferSize();
5709 switch (buf->getBufferKind()) {
5710 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
5711 sizes.malloc_bytes += bytes;
5712 break;
5713 case llvm::MemoryBuffer::MemoryBuffer_MMap:
5714 sizes.mmap_bytes += bytes;
5715 break;
5716 }
5717 }
5718 }
5719}
5720
5721void ASTReader::InitializeSema(Sema &S) {
5722 SemaObj = &S;
5723 S.addExternalSource(this);
5724
5725 // Makes sure any declarations that were deserialized "too early"
5726 // still get added to the identifier's declaration chains.
5727 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
5728 SemaObj->pushExternalDeclIntoScope(PreloadedDecls[I],
5729 PreloadedDecls[I]->getDeclName());
5730 }
5731 PreloadedDecls.clear();
5732
5733 // Load the offsets of the declarations that Sema references.
5734 // They will be lazily deserialized when needed.
5735 if (!SemaDeclRefs.empty()) {
5736 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
5737 if (!SemaObj->StdNamespace)
5738 SemaObj->StdNamespace = SemaDeclRefs[0];
5739 if (!SemaObj->StdBadAlloc)
5740 SemaObj->StdBadAlloc = SemaDeclRefs[1];
5741 }
5742
5743 if (!FPPragmaOptions.empty()) {
5744 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
5745 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
5746 }
5747
5748 if (!OpenCLExtensions.empty()) {
5749 unsigned I = 0;
5750#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
5751#include "clang/Basic/OpenCLExtensions.def"
5752
5753 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
5754 }
5755}
5756
5757IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
5758 // Note that we are loading an identifier.
5759 Deserializing AnIdentifier(this);
Douglas Gregor1a49d972013-01-25 01:03:03 +00005760 StringRef Name(NameStart, NameEnd - NameStart);
5761
5762 // If there is a global index, look there first to determine which modules
5763 // provably do not have any results for this identifier.
5764 GlobalModuleIndex::SkipSet SkipSet;
5765 if (!loadGlobalIndex()) {
5766 SmallVector<const FileEntry *, 4> ModuleFiles;
5767 if (GlobalIndex->lookupIdentifier(Name, ModuleFiles)) {
5768 SkipSet = GlobalIndex->computeSkipSet(ModuleFiles);
5769 }
5770 }
5771 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0, SkipSet,
Douglas Gregore1698072013-01-25 00:38:33 +00005772 NumIdentifierLookups,
5773 NumIdentifierLookupHits);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005774 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
5775 IdentifierInfo *II = Visitor.getIdentifierInfo();
5776 markIdentifierUpToDate(II);
5777 return II;
5778}
5779
5780namespace clang {
5781 /// \brief An identifier-lookup iterator that enumerates all of the
5782 /// identifiers stored within a set of AST files.
5783 class ASTIdentifierIterator : public IdentifierIterator {
5784 /// \brief The AST reader whose identifiers are being enumerated.
5785 const ASTReader &Reader;
5786
5787 /// \brief The current index into the chain of AST files stored in
5788 /// the AST reader.
5789 unsigned Index;
5790
5791 /// \brief The current position within the identifier lookup table
5792 /// of the current AST file.
5793 ASTIdentifierLookupTable::key_iterator Current;
5794
5795 /// \brief The end position within the identifier lookup table of
5796 /// the current AST file.
5797 ASTIdentifierLookupTable::key_iterator End;
5798
5799 public:
5800 explicit ASTIdentifierIterator(const ASTReader &Reader);
5801
5802 virtual StringRef Next();
5803 };
5804}
5805
5806ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
5807 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
5808 ASTIdentifierLookupTable *IdTable
5809 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
5810 Current = IdTable->key_begin();
5811 End = IdTable->key_end();
5812}
5813
5814StringRef ASTIdentifierIterator::Next() {
5815 while (Current == End) {
5816 // If we have exhausted all of our AST files, we're done.
5817 if (Index == 0)
5818 return StringRef();
5819
5820 --Index;
5821 ASTIdentifierLookupTable *IdTable
5822 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
5823 IdentifierLookupTable;
5824 Current = IdTable->key_begin();
5825 End = IdTable->key_end();
5826 }
5827
5828 // We have any identifiers remaining in the current AST file; return
5829 // the next one.
Douglas Gregor479633c2013-01-23 18:53:14 +00005830 StringRef Result = *Current;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005831 ++Current;
Douglas Gregor479633c2013-01-23 18:53:14 +00005832 return Result;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005833}
5834
5835IdentifierIterator *ASTReader::getIdentifiers() const {
5836 return new ASTIdentifierIterator(*this);
5837}
5838
5839namespace clang { namespace serialization {
5840 class ReadMethodPoolVisitor {
5841 ASTReader &Reader;
5842 Selector Sel;
5843 unsigned PriorGeneration;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00005844 SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
5845 SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005846
5847 public:
5848 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
5849 unsigned PriorGeneration)
5850 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) { }
5851
5852 static bool visit(ModuleFile &M, void *UserData) {
5853 ReadMethodPoolVisitor *This
5854 = static_cast<ReadMethodPoolVisitor *>(UserData);
5855
5856 if (!M.SelectorLookupTable)
5857 return false;
5858
5859 // If we've already searched this module file, skip it now.
5860 if (M.Generation <= This->PriorGeneration)
5861 return true;
5862
5863 ASTSelectorLookupTable *PoolTable
5864 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
5865 ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel);
5866 if (Pos == PoolTable->end())
5867 return false;
5868
5869 ++This->Reader.NumSelectorsRead;
5870 // FIXME: Not quite happy with the statistics here. We probably should
5871 // disable this tracking when called via LoadSelector.
5872 // Also, should entries without methods count as misses?
5873 ++This->Reader.NumMethodPoolEntriesRead;
5874 ASTSelectorLookupTrait::data_type Data = *Pos;
5875 if (This->Reader.DeserializationListener)
5876 This->Reader.DeserializationListener->SelectorRead(Data.ID,
5877 This->Sel);
5878
5879 This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
5880 This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
5881 return true;
5882 }
5883
5884 /// \brief Retrieve the instance methods found by this visitor.
5885 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
5886 return InstanceMethods;
5887 }
5888
5889 /// \brief Retrieve the instance methods found by this visitor.
5890 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
5891 return FactoryMethods;
5892 }
5893 };
5894} } // end namespace clang::serialization
5895
5896/// \brief Add the given set of methods to the method list.
5897static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
5898 ObjCMethodList &List) {
5899 for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
5900 S.addMethodToGlobalList(&List, Methods[I]);
5901 }
5902}
5903
5904void ASTReader::ReadMethodPool(Selector Sel) {
5905 // Get the selector generation and update it to the current generation.
5906 unsigned &Generation = SelectorGeneration[Sel];
5907 unsigned PriorGeneration = Generation;
5908 Generation = CurrentGeneration;
5909
5910 // Search for methods defined with this selector.
5911 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
5912 ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor);
5913
5914 if (Visitor.getInstanceMethods().empty() &&
5915 Visitor.getFactoryMethods().empty()) {
5916 ++NumMethodPoolMisses;
5917 return;
5918 }
5919
5920 if (!getSema())
5921 return;
5922
5923 Sema &S = *getSema();
5924 Sema::GlobalMethodPool::iterator Pos
5925 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
5926
5927 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
5928 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
5929}
5930
5931void ASTReader::ReadKnownNamespaces(
5932 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
5933 Namespaces.clear();
5934
5935 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
5936 if (NamespaceDecl *Namespace
5937 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
5938 Namespaces.push_back(Namespace);
5939 }
5940}
5941
5942void ASTReader::ReadTentativeDefinitions(
5943 SmallVectorImpl<VarDecl *> &TentativeDefs) {
5944 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
5945 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
5946 if (Var)
5947 TentativeDefs.push_back(Var);
5948 }
5949 TentativeDefinitions.clear();
5950}
5951
5952void ASTReader::ReadUnusedFileScopedDecls(
5953 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
5954 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
5955 DeclaratorDecl *D
5956 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
5957 if (D)
5958 Decls.push_back(D);
5959 }
5960 UnusedFileScopedDecls.clear();
5961}
5962
5963void ASTReader::ReadDelegatingConstructors(
5964 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
5965 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
5966 CXXConstructorDecl *D
5967 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
5968 if (D)
5969 Decls.push_back(D);
5970 }
5971 DelegatingCtorDecls.clear();
5972}
5973
5974void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
5975 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
5976 TypedefNameDecl *D
5977 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
5978 if (D)
5979 Decls.push_back(D);
5980 }
5981 ExtVectorDecls.clear();
5982}
5983
5984void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) {
5985 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
5986 CXXRecordDecl *D
5987 = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I]));
5988 if (D)
5989 Decls.push_back(D);
5990 }
5991 DynamicClasses.clear();
5992}
5993
5994void
Richard Smith5ea6ef42013-01-10 23:43:47 +00005995ASTReader::ReadLocallyScopedExternCDecls(SmallVectorImpl<NamedDecl *> &Decls) {
5996 for (unsigned I = 0, N = LocallyScopedExternCDecls.size(); I != N; ++I) {
5997 NamedDecl *D
5998 = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternCDecls[I]));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005999 if (D)
6000 Decls.push_back(D);
6001 }
Richard Smith5ea6ef42013-01-10 23:43:47 +00006002 LocallyScopedExternCDecls.clear();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006003}
6004
6005void ASTReader::ReadReferencedSelectors(
6006 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
6007 if (ReferencedSelectorsData.empty())
6008 return;
6009
6010 // If there are @selector references added them to its pool. This is for
6011 // implementation of -Wselector.
6012 unsigned int DataSize = ReferencedSelectorsData.size()-1;
6013 unsigned I = 0;
6014 while (I < DataSize) {
6015 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
6016 SourceLocation SelLoc
6017 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
6018 Sels.push_back(std::make_pair(Sel, SelLoc));
6019 }
6020 ReferencedSelectorsData.clear();
6021}
6022
6023void ASTReader::ReadWeakUndeclaredIdentifiers(
6024 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
6025 if (WeakUndeclaredIdentifiers.empty())
6026 return;
6027
6028 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
6029 IdentifierInfo *WeakId
6030 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
6031 IdentifierInfo *AliasId
6032 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
6033 SourceLocation Loc
6034 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
6035 bool Used = WeakUndeclaredIdentifiers[I++];
6036 WeakInfo WI(AliasId, Loc);
6037 WI.setUsed(Used);
6038 WeakIDs.push_back(std::make_pair(WeakId, WI));
6039 }
6040 WeakUndeclaredIdentifiers.clear();
6041}
6042
6043void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
6044 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
6045 ExternalVTableUse VT;
6046 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
6047 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
6048 VT.DefinitionRequired = VTableUses[Idx++];
6049 VTables.push_back(VT);
6050 }
6051
6052 VTableUses.clear();
6053}
6054
6055void ASTReader::ReadPendingInstantiations(
6056 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
6057 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
6058 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
6059 SourceLocation Loc
6060 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
6061
6062 Pending.push_back(std::make_pair(D, Loc));
6063 }
6064 PendingInstantiations.clear();
6065}
6066
6067void ASTReader::LoadSelector(Selector Sel) {
6068 // It would be complicated to avoid reading the methods anyway. So don't.
6069 ReadMethodPool(Sel);
6070}
6071
6072void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
6073 assert(ID && "Non-zero identifier ID required");
6074 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
6075 IdentifiersLoaded[ID - 1] = II;
6076 if (DeserializationListener)
6077 DeserializationListener->IdentifierRead(ID, II);
6078}
6079
6080/// \brief Set the globally-visible declarations associated with the given
6081/// identifier.
6082///
6083/// If the AST reader is currently in a state where the given declaration IDs
6084/// cannot safely be resolved, they are queued until it is safe to resolve
6085/// them.
6086///
6087/// \param II an IdentifierInfo that refers to one or more globally-visible
6088/// declarations.
6089///
6090/// \param DeclIDs the set of declaration IDs with the name @p II that are
6091/// visible at global scope.
6092///
6093/// \param Nonrecursive should be true to indicate that the caller knows that
6094/// this call is non-recursive, and therefore the globally-visible declarations
6095/// will not be placed onto the pending queue.
6096void
6097ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
6098 const SmallVectorImpl<uint32_t> &DeclIDs,
6099 bool Nonrecursive) {
6100 if (NumCurrentElementsDeserializing && !Nonrecursive) {
6101 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
6102 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
6103 PII.II = II;
6104 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
6105 return;
6106 }
6107
6108 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
6109 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
6110 if (SemaObj) {
6111 // Introduce this declaration into the translation-unit scope
6112 // and add it to the declaration chain for this identifier, so
6113 // that (unqualified) name lookup will find it.
6114 SemaObj->pushExternalDeclIntoScope(D, II);
6115 } else {
6116 // Queue this declaration so that it will be added to the
6117 // translation unit scope and identifier's declaration chain
6118 // once a Sema object is known.
6119 PreloadedDecls.push_back(D);
6120 }
6121 }
6122}
6123
Douglas Gregor8222b892013-01-21 16:52:34 +00006124IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006125 if (ID == 0)
6126 return 0;
6127
6128 if (IdentifiersLoaded.empty()) {
6129 Error("no identifier table in AST file");
6130 return 0;
6131 }
6132
6133 ID -= 1;
6134 if (!IdentifiersLoaded[ID]) {
6135 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
6136 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
6137 ModuleFile *M = I->second;
6138 unsigned Index = ID - M->BaseIdentifierID;
6139 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
6140
6141 // All of the strings in the AST file are preceded by a 16-bit length.
6142 // Extract that 16-bit length to avoid having to execute strlen().
6143 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
6144 // unsigned integers. This is important to avoid integer overflow when
6145 // we cast them to 'unsigned'.
6146 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
6147 unsigned StrLen = (((unsigned) StrLenPtr[0])
6148 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Douglas Gregor8222b892013-01-21 16:52:34 +00006149 IdentifiersLoaded[ID]
6150 = &PP.getIdentifierTable().get(StringRef(Str, StrLen));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006151 if (DeserializationListener)
6152 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
6153 }
6154
6155 return IdentifiersLoaded[ID];
6156}
6157
Douglas Gregor8222b892013-01-21 16:52:34 +00006158IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
6159 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006160}
6161
6162IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
6163 if (LocalID < NUM_PREDEF_IDENT_IDS)
6164 return LocalID;
6165
6166 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6167 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
6168 assert(I != M.IdentifierRemap.end()
6169 && "Invalid index into identifier index remap");
6170
6171 return LocalID + I->second;
6172}
6173
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00006174MacroInfo *ASTReader::getMacro(MacroID ID, MacroInfo *Hint) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006175 if (ID == 0)
6176 return 0;
6177
6178 if (MacrosLoaded.empty()) {
6179 Error("no macro table in AST file");
6180 return 0;
6181 }
6182
6183 ID -= NUM_PREDEF_MACRO_IDS;
6184 if (!MacrosLoaded[ID]) {
6185 GlobalMacroMapType::iterator I
6186 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
6187 assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
6188 ModuleFile *M = I->second;
6189 unsigned Index = ID - M->BaseMacroID;
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00006190 ReadMacroRecord(*M, M->MacroOffsets[Index], Hint);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006191 }
6192
6193 return MacrosLoaded[ID];
6194}
6195
6196MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
6197 if (LocalID < NUM_PREDEF_MACRO_IDS)
6198 return LocalID;
6199
6200 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6201 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
6202 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
6203
6204 return LocalID + I->second;
6205}
6206
6207serialization::SubmoduleID
6208ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
6209 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
6210 return LocalID;
6211
6212 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6213 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
6214 assert(I != M.SubmoduleRemap.end()
6215 && "Invalid index into submodule index remap");
6216
6217 return LocalID + I->second;
6218}
6219
6220Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
6221 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
6222 assert(GlobalID == 0 && "Unhandled global submodule ID");
6223 return 0;
6224 }
6225
6226 if (GlobalID > SubmodulesLoaded.size()) {
6227 Error("submodule ID out of range in AST file");
6228 return 0;
6229 }
6230
6231 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
6232}
Douglas Gregorca2ab452013-01-12 01:29:50 +00006233
6234Module *ASTReader::getModule(unsigned ID) {
6235 return getSubmodule(ID);
6236}
6237
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006238Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
6239 return DecodeSelector(getGlobalSelectorID(M, LocalID));
6240}
6241
6242Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
6243 if (ID == 0)
6244 return Selector();
6245
6246 if (ID > SelectorsLoaded.size()) {
6247 Error("selector ID out of range in AST file");
6248 return Selector();
6249 }
6250
6251 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
6252 // Load this selector from the selector table.
6253 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
6254 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
6255 ModuleFile &M = *I->second;
6256 ASTSelectorLookupTrait Trait(*this, M);
6257 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
6258 SelectorsLoaded[ID - 1] =
6259 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
6260 if (DeserializationListener)
6261 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
6262 }
6263
6264 return SelectorsLoaded[ID - 1];
6265}
6266
6267Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
6268 return DecodeSelector(ID);
6269}
6270
6271uint32_t ASTReader::GetNumExternalSelectors() {
6272 // ID 0 (the null selector) is considered an external selector.
6273 return getTotalNumSelectors() + 1;
6274}
6275
6276serialization::SelectorID
6277ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
6278 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
6279 return LocalID;
6280
6281 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6282 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
6283 assert(I != M.SelectorRemap.end()
6284 && "Invalid index into selector index remap");
6285
6286 return LocalID + I->second;
6287}
6288
6289DeclarationName
6290ASTReader::ReadDeclarationName(ModuleFile &F,
6291 const RecordData &Record, unsigned &Idx) {
6292 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
6293 switch (Kind) {
6294 case DeclarationName::Identifier:
Douglas Gregor8222b892013-01-21 16:52:34 +00006295 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006296
6297 case DeclarationName::ObjCZeroArgSelector:
6298 case DeclarationName::ObjCOneArgSelector:
6299 case DeclarationName::ObjCMultiArgSelector:
6300 return DeclarationName(ReadSelector(F, Record, Idx));
6301
6302 case DeclarationName::CXXConstructorName:
6303 return Context.DeclarationNames.getCXXConstructorName(
6304 Context.getCanonicalType(readType(F, Record, Idx)));
6305
6306 case DeclarationName::CXXDestructorName:
6307 return Context.DeclarationNames.getCXXDestructorName(
6308 Context.getCanonicalType(readType(F, Record, Idx)));
6309
6310 case DeclarationName::CXXConversionFunctionName:
6311 return Context.DeclarationNames.getCXXConversionFunctionName(
6312 Context.getCanonicalType(readType(F, Record, Idx)));
6313
6314 case DeclarationName::CXXOperatorName:
6315 return Context.DeclarationNames.getCXXOperatorName(
6316 (OverloadedOperatorKind)Record[Idx++]);
6317
6318 case DeclarationName::CXXLiteralOperatorName:
6319 return Context.DeclarationNames.getCXXLiteralOperatorName(
6320 GetIdentifierInfo(F, Record, Idx));
6321
6322 case DeclarationName::CXXUsingDirective:
6323 return DeclarationName::getUsingDirectiveName();
6324 }
6325
6326 llvm_unreachable("Invalid NameKind!");
6327}
6328
6329void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
6330 DeclarationNameLoc &DNLoc,
6331 DeclarationName Name,
6332 const RecordData &Record, unsigned &Idx) {
6333 switch (Name.getNameKind()) {
6334 case DeclarationName::CXXConstructorName:
6335 case DeclarationName::CXXDestructorName:
6336 case DeclarationName::CXXConversionFunctionName:
6337 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
6338 break;
6339
6340 case DeclarationName::CXXOperatorName:
6341 DNLoc.CXXOperatorName.BeginOpNameLoc
6342 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6343 DNLoc.CXXOperatorName.EndOpNameLoc
6344 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6345 break;
6346
6347 case DeclarationName::CXXLiteralOperatorName:
6348 DNLoc.CXXLiteralOperatorName.OpNameLoc
6349 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6350 break;
6351
6352 case DeclarationName::Identifier:
6353 case DeclarationName::ObjCZeroArgSelector:
6354 case DeclarationName::ObjCOneArgSelector:
6355 case DeclarationName::ObjCMultiArgSelector:
6356 case DeclarationName::CXXUsingDirective:
6357 break;
6358 }
6359}
6360
6361void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
6362 DeclarationNameInfo &NameInfo,
6363 const RecordData &Record, unsigned &Idx) {
6364 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
6365 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
6366 DeclarationNameLoc DNLoc;
6367 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
6368 NameInfo.setInfo(DNLoc);
6369}
6370
6371void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
6372 const RecordData &Record, unsigned &Idx) {
6373 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
6374 unsigned NumTPLists = Record[Idx++];
6375 Info.NumTemplParamLists = NumTPLists;
6376 if (NumTPLists) {
6377 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
6378 for (unsigned i=0; i != NumTPLists; ++i)
6379 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
6380 }
6381}
6382
6383TemplateName
6384ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
6385 unsigned &Idx) {
6386 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
6387 switch (Kind) {
6388 case TemplateName::Template:
6389 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
6390
6391 case TemplateName::OverloadedTemplate: {
6392 unsigned size = Record[Idx++];
6393 UnresolvedSet<8> Decls;
6394 while (size--)
6395 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
6396
6397 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
6398 }
6399
6400 case TemplateName::QualifiedTemplate: {
6401 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
6402 bool hasTemplKeyword = Record[Idx++];
6403 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
6404 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
6405 }
6406
6407 case TemplateName::DependentTemplate: {
6408 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
6409 if (Record[Idx++]) // isIdentifier
6410 return Context.getDependentTemplateName(NNS,
6411 GetIdentifierInfo(F, Record,
6412 Idx));
6413 return Context.getDependentTemplateName(NNS,
6414 (OverloadedOperatorKind)Record[Idx++]);
6415 }
6416
6417 case TemplateName::SubstTemplateTemplateParm: {
6418 TemplateTemplateParmDecl *param
6419 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
6420 if (!param) return TemplateName();
6421 TemplateName replacement = ReadTemplateName(F, Record, Idx);
6422 return Context.getSubstTemplateTemplateParm(param, replacement);
6423 }
6424
6425 case TemplateName::SubstTemplateTemplateParmPack: {
6426 TemplateTemplateParmDecl *Param
6427 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
6428 if (!Param)
6429 return TemplateName();
6430
6431 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
6432 if (ArgPack.getKind() != TemplateArgument::Pack)
6433 return TemplateName();
6434
6435 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
6436 }
6437 }
6438
6439 llvm_unreachable("Unhandled template name kind!");
6440}
6441
6442TemplateArgument
6443ASTReader::ReadTemplateArgument(ModuleFile &F,
6444 const RecordData &Record, unsigned &Idx) {
6445 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
6446 switch (Kind) {
6447 case TemplateArgument::Null:
6448 return TemplateArgument();
6449 case TemplateArgument::Type:
6450 return TemplateArgument(readType(F, Record, Idx));
6451 case TemplateArgument::Declaration: {
6452 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
6453 bool ForReferenceParam = Record[Idx++];
6454 return TemplateArgument(D, ForReferenceParam);
6455 }
6456 case TemplateArgument::NullPtr:
6457 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
6458 case TemplateArgument::Integral: {
6459 llvm::APSInt Value = ReadAPSInt(Record, Idx);
6460 QualType T = readType(F, Record, Idx);
6461 return TemplateArgument(Context, Value, T);
6462 }
6463 case TemplateArgument::Template:
6464 return TemplateArgument(ReadTemplateName(F, Record, Idx));
6465 case TemplateArgument::TemplateExpansion: {
6466 TemplateName Name = ReadTemplateName(F, Record, Idx);
6467 llvm::Optional<unsigned> NumTemplateExpansions;
6468 if (unsigned NumExpansions = Record[Idx++])
6469 NumTemplateExpansions = NumExpansions - 1;
6470 return TemplateArgument(Name, NumTemplateExpansions);
6471 }
6472 case TemplateArgument::Expression:
6473 return TemplateArgument(ReadExpr(F));
6474 case TemplateArgument::Pack: {
6475 unsigned NumArgs = Record[Idx++];
6476 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
6477 for (unsigned I = 0; I != NumArgs; ++I)
6478 Args[I] = ReadTemplateArgument(F, Record, Idx);
6479 return TemplateArgument(Args, NumArgs);
6480 }
6481 }
6482
6483 llvm_unreachable("Unhandled template argument kind!");
6484}
6485
6486TemplateParameterList *
6487ASTReader::ReadTemplateParameterList(ModuleFile &F,
6488 const RecordData &Record, unsigned &Idx) {
6489 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
6490 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
6491 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
6492
6493 unsigned NumParams = Record[Idx++];
6494 SmallVector<NamedDecl *, 16> Params;
6495 Params.reserve(NumParams);
6496 while (NumParams--)
6497 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
6498
6499 TemplateParameterList* TemplateParams =
6500 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
6501 Params.data(), Params.size(), RAngleLoc);
6502 return TemplateParams;
6503}
6504
6505void
6506ASTReader::
6507ReadTemplateArgumentList(SmallVector<TemplateArgument, 8> &TemplArgs,
6508 ModuleFile &F, const RecordData &Record,
6509 unsigned &Idx) {
6510 unsigned NumTemplateArgs = Record[Idx++];
6511 TemplArgs.reserve(NumTemplateArgs);
6512 while (NumTemplateArgs--)
6513 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
6514}
6515
6516/// \brief Read a UnresolvedSet structure.
6517void ASTReader::ReadUnresolvedSet(ModuleFile &F, ASTUnresolvedSet &Set,
6518 const RecordData &Record, unsigned &Idx) {
6519 unsigned NumDecls = Record[Idx++];
6520 Set.reserve(Context, NumDecls);
6521 while (NumDecls--) {
6522 NamedDecl *D = ReadDeclAs<NamedDecl>(F, Record, Idx);
6523 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
6524 Set.addDecl(Context, D, AS);
6525 }
6526}
6527
6528CXXBaseSpecifier
6529ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
6530 const RecordData &Record, unsigned &Idx) {
6531 bool isVirtual = static_cast<bool>(Record[Idx++]);
6532 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
6533 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
6534 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
6535 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
6536 SourceRange Range = ReadSourceRange(F, Record, Idx);
6537 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
6538 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
6539 EllipsisLoc);
6540 Result.setInheritConstructors(inheritConstructors);
6541 return Result;
6542}
6543
6544std::pair<CXXCtorInitializer **, unsigned>
6545ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
6546 unsigned &Idx) {
6547 CXXCtorInitializer **CtorInitializers = 0;
6548 unsigned NumInitializers = Record[Idx++];
6549 if (NumInitializers) {
6550 CtorInitializers
6551 = new (Context) CXXCtorInitializer*[NumInitializers];
6552 for (unsigned i=0; i != NumInitializers; ++i) {
6553 TypeSourceInfo *TInfo = 0;
6554 bool IsBaseVirtual = false;
6555 FieldDecl *Member = 0;
6556 IndirectFieldDecl *IndirectMember = 0;
6557
6558 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
6559 switch (Type) {
6560 case CTOR_INITIALIZER_BASE:
6561 TInfo = GetTypeSourceInfo(F, Record, Idx);
6562 IsBaseVirtual = Record[Idx++];
6563 break;
6564
6565 case CTOR_INITIALIZER_DELEGATING:
6566 TInfo = GetTypeSourceInfo(F, Record, Idx);
6567 break;
6568
6569 case CTOR_INITIALIZER_MEMBER:
6570 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
6571 break;
6572
6573 case CTOR_INITIALIZER_INDIRECT_MEMBER:
6574 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
6575 break;
6576 }
6577
6578 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
6579 Expr *Init = ReadExpr(F);
6580 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
6581 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
6582 bool IsWritten = Record[Idx++];
6583 unsigned SourceOrderOrNumArrayIndices;
6584 SmallVector<VarDecl *, 8> Indices;
6585 if (IsWritten) {
6586 SourceOrderOrNumArrayIndices = Record[Idx++];
6587 } else {
6588 SourceOrderOrNumArrayIndices = Record[Idx++];
6589 Indices.reserve(SourceOrderOrNumArrayIndices);
6590 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
6591 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
6592 }
6593
6594 CXXCtorInitializer *BOMInit;
6595 if (Type == CTOR_INITIALIZER_BASE) {
6596 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, IsBaseVirtual,
6597 LParenLoc, Init, RParenLoc,
6598 MemberOrEllipsisLoc);
6599 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
6600 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, LParenLoc,
6601 Init, RParenLoc);
6602 } else if (IsWritten) {
6603 if (Member)
6604 BOMInit = new (Context) CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc,
6605 LParenLoc, Init, RParenLoc);
6606 else
6607 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
6608 MemberOrEllipsisLoc, LParenLoc,
6609 Init, RParenLoc);
6610 } else {
6611 BOMInit = CXXCtorInitializer::Create(Context, Member, MemberOrEllipsisLoc,
6612 LParenLoc, Init, RParenLoc,
6613 Indices.data(), Indices.size());
6614 }
6615
6616 if (IsWritten)
6617 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
6618 CtorInitializers[i] = BOMInit;
6619 }
6620 }
6621
6622 return std::make_pair(CtorInitializers, NumInitializers);
6623}
6624
6625NestedNameSpecifier *
6626ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
6627 const RecordData &Record, unsigned &Idx) {
6628 unsigned N = Record[Idx++];
6629 NestedNameSpecifier *NNS = 0, *Prev = 0;
6630 for (unsigned I = 0; I != N; ++I) {
6631 NestedNameSpecifier::SpecifierKind Kind
6632 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
6633 switch (Kind) {
6634 case NestedNameSpecifier::Identifier: {
6635 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
6636 NNS = NestedNameSpecifier::Create(Context, Prev, II);
6637 break;
6638 }
6639
6640 case NestedNameSpecifier::Namespace: {
6641 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
6642 NNS = NestedNameSpecifier::Create(Context, Prev, NS);
6643 break;
6644 }
6645
6646 case NestedNameSpecifier::NamespaceAlias: {
6647 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
6648 NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
6649 break;
6650 }
6651
6652 case NestedNameSpecifier::TypeSpec:
6653 case NestedNameSpecifier::TypeSpecWithTemplate: {
6654 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
6655 if (!T)
6656 return 0;
6657
6658 bool Template = Record[Idx++];
6659 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
6660 break;
6661 }
6662
6663 case NestedNameSpecifier::Global: {
6664 NNS = NestedNameSpecifier::GlobalSpecifier(Context);
6665 // No associated value, and there can't be a prefix.
6666 break;
6667 }
6668 }
6669 Prev = NNS;
6670 }
6671 return NNS;
6672}
6673
6674NestedNameSpecifierLoc
6675ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
6676 unsigned &Idx) {
6677 unsigned N = Record[Idx++];
6678 NestedNameSpecifierLocBuilder Builder;
6679 for (unsigned I = 0; I != N; ++I) {
6680 NestedNameSpecifier::SpecifierKind Kind
6681 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
6682 switch (Kind) {
6683 case NestedNameSpecifier::Identifier: {
6684 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
6685 SourceRange Range = ReadSourceRange(F, Record, Idx);
6686 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
6687 break;
6688 }
6689
6690 case NestedNameSpecifier::Namespace: {
6691 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
6692 SourceRange Range = ReadSourceRange(F, Record, Idx);
6693 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
6694 break;
6695 }
6696
6697 case NestedNameSpecifier::NamespaceAlias: {
6698 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
6699 SourceRange Range = ReadSourceRange(F, Record, Idx);
6700 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
6701 break;
6702 }
6703
6704 case NestedNameSpecifier::TypeSpec:
6705 case NestedNameSpecifier::TypeSpecWithTemplate: {
6706 bool Template = Record[Idx++];
6707 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
6708 if (!T)
6709 return NestedNameSpecifierLoc();
6710 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
6711
6712 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
6713 Builder.Extend(Context,
6714 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
6715 T->getTypeLoc(), ColonColonLoc);
6716 break;
6717 }
6718
6719 case NestedNameSpecifier::Global: {
6720 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
6721 Builder.MakeGlobal(Context, ColonColonLoc);
6722 break;
6723 }
6724 }
6725 }
6726
6727 return Builder.getWithLocInContext(Context);
6728}
6729
6730SourceRange
6731ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
6732 unsigned &Idx) {
6733 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
6734 SourceLocation end = ReadSourceLocation(F, Record, Idx);
6735 return SourceRange(beg, end);
6736}
6737
6738/// \brief Read an integral value
6739llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
6740 unsigned BitWidth = Record[Idx++];
6741 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
6742 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
6743 Idx += NumWords;
6744 return Result;
6745}
6746
6747/// \brief Read a signed integral value
6748llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
6749 bool isUnsigned = Record[Idx++];
6750 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
6751}
6752
6753/// \brief Read a floating-point value
Tim Northover9ec55f22013-01-22 09:46:51 +00006754llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record,
6755 const llvm::fltSemantics &Sem,
6756 unsigned &Idx) {
6757 return llvm::APFloat(Sem, ReadAPInt(Record, Idx));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006758}
6759
6760// \brief Read a string
6761std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
6762 unsigned Len = Record[Idx++];
6763 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
6764 Idx += Len;
6765 return Result;
6766}
6767
6768VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
6769 unsigned &Idx) {
6770 unsigned Major = Record[Idx++];
6771 unsigned Minor = Record[Idx++];
6772 unsigned Subminor = Record[Idx++];
6773 if (Minor == 0)
6774 return VersionTuple(Major);
6775 if (Subminor == 0)
6776 return VersionTuple(Major, Minor - 1);
6777 return VersionTuple(Major, Minor - 1, Subminor - 1);
6778}
6779
6780CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
6781 const RecordData &Record,
6782 unsigned &Idx) {
6783 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
6784 return CXXTemporary::Create(Context, Decl);
6785}
6786
6787DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
6788 return Diag(SourceLocation(), DiagID);
6789}
6790
6791DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
6792 return Diags.Report(Loc, DiagID);
6793}
6794
6795/// \brief Retrieve the identifier table associated with the
6796/// preprocessor.
6797IdentifierTable &ASTReader::getIdentifierTable() {
6798 return PP.getIdentifierTable();
6799}
6800
6801/// \brief Record that the given ID maps to the given switch-case
6802/// statement.
6803void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
6804 assert((*CurrSwitchCaseStmts)[ID] == 0 &&
6805 "Already have a SwitchCase with this ID");
6806 (*CurrSwitchCaseStmts)[ID] = SC;
6807}
6808
6809/// \brief Retrieve the switch-case statement with the given ID.
6810SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
6811 assert((*CurrSwitchCaseStmts)[ID] != 0 && "No SwitchCase with this ID");
6812 return (*CurrSwitchCaseStmts)[ID];
6813}
6814
6815void ASTReader::ClearSwitchCaseIDs() {
6816 CurrSwitchCaseStmts->clear();
6817}
6818
6819void ASTReader::ReadComments() {
6820 std::vector<RawComment *> Comments;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00006821 for (SmallVectorImpl<std::pair<BitstreamCursor,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006822 serialization::ModuleFile *> >::iterator
6823 I = CommentsCursors.begin(),
6824 E = CommentsCursors.end();
6825 I != E; ++I) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00006826 BitstreamCursor &Cursor = I->first;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006827 serialization::ModuleFile &F = *I->second;
6828 SavedStreamPosition SavedPosition(Cursor);
6829
6830 RecordData Record;
6831 while (true) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00006832 llvm::BitstreamEntry Entry =
6833 Cursor.advanceSkippingSubblocks(BitstreamCursor::AF_DontPopBlockAtEnd);
6834
6835 switch (Entry.Kind) {
6836 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
6837 case llvm::BitstreamEntry::Error:
6838 Error("malformed block record in AST file");
6839 return;
6840 case llvm::BitstreamEntry::EndBlock:
6841 goto NextCursor;
6842 case llvm::BitstreamEntry::Record:
6843 // The interesting case.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006844 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006845 }
6846
6847 // Read a record.
6848 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00006849 switch ((CommentRecordTypes)Cursor.readRecord(Entry.ID, Record)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006850 case COMMENTS_RAW_COMMENT: {
6851 unsigned Idx = 0;
6852 SourceRange SR = ReadSourceRange(F, Record, Idx);
6853 RawComment::CommentKind Kind =
6854 (RawComment::CommentKind) Record[Idx++];
6855 bool IsTrailingComment = Record[Idx++];
6856 bool IsAlmostTrailingComment = Record[Idx++];
6857 Comments.push_back(new (Context) RawComment(SR, Kind,
6858 IsTrailingComment,
6859 IsAlmostTrailingComment));
6860 break;
6861 }
6862 }
6863 }
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00006864 NextCursor:;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006865 }
6866 Context.Comments.addCommentsToFront(Comments);
6867}
6868
6869void ASTReader::finishPendingActions() {
6870 while (!PendingIdentifierInfos.empty() || !PendingDeclChains.empty() ||
6871 !PendingMacroIDs.empty()) {
6872 // If any identifiers with corresponding top-level declarations have
6873 // been loaded, load those declarations now.
6874 while (!PendingIdentifierInfos.empty()) {
6875 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
6876 PendingIdentifierInfos.front().DeclIDs, true);
6877 PendingIdentifierInfos.pop_front();
6878 }
6879
6880 // Load pending declaration chains.
6881 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) {
6882 loadPendingDeclChain(PendingDeclChains[I]);
6883 PendingDeclChainsKnown.erase(PendingDeclChains[I]);
6884 }
6885 PendingDeclChains.clear();
6886
6887 // Load any pending macro definitions.
6888 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00006889 // FIXME: std::move here
6890 SmallVector<MacroID, 2> GlobalIDs = PendingMacroIDs.begin()[I].second;
6891 MacroInfo *Hint = 0;
6892 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
6893 ++IDIdx) {
6894 Hint = getMacro(GlobalIDs[IDIdx], Hint);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006895 }
6896 }
6897 PendingMacroIDs.clear();
6898 }
6899
6900 // If we deserialized any C++ or Objective-C class definitions, any
6901 // Objective-C protocol definitions, or any redeclarable templates, make sure
6902 // that all redeclarations point to the definitions. Note that this can only
6903 // happen now, after the redeclaration chains have been fully wired.
6904 for (llvm::SmallPtrSet<Decl *, 4>::iterator D = PendingDefinitions.begin(),
6905 DEnd = PendingDefinitions.end();
6906 D != DEnd; ++D) {
6907 if (TagDecl *TD = dyn_cast<TagDecl>(*D)) {
6908 if (const TagType *TagT = dyn_cast<TagType>(TD->TypeForDecl)) {
6909 // Make sure that the TagType points at the definition.
6910 const_cast<TagType*>(TagT)->decl = TD;
6911 }
6912
6913 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(*D)) {
6914 for (CXXRecordDecl::redecl_iterator R = RD->redecls_begin(),
6915 REnd = RD->redecls_end();
6916 R != REnd; ++R)
6917 cast<CXXRecordDecl>(*R)->DefinitionData = RD->DefinitionData;
6918
6919 }
6920
6921 continue;
6922 }
6923
6924 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(*D)) {
6925 // Make sure that the ObjCInterfaceType points at the definition.
6926 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
6927 ->Decl = ID;
6928
6929 for (ObjCInterfaceDecl::redecl_iterator R = ID->redecls_begin(),
6930 REnd = ID->redecls_end();
6931 R != REnd; ++R)
6932 R->Data = ID->Data;
6933
6934 continue;
6935 }
6936
6937 if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(*D)) {
6938 for (ObjCProtocolDecl::redecl_iterator R = PD->redecls_begin(),
6939 REnd = PD->redecls_end();
6940 R != REnd; ++R)
6941 R->Data = PD->Data;
6942
6943 continue;
6944 }
6945
6946 RedeclarableTemplateDecl *RTD
6947 = cast<RedeclarableTemplateDecl>(*D)->getCanonicalDecl();
6948 for (RedeclarableTemplateDecl::redecl_iterator R = RTD->redecls_begin(),
6949 REnd = RTD->redecls_end();
6950 R != REnd; ++R)
6951 R->Common = RTD->Common;
6952 }
6953 PendingDefinitions.clear();
6954
6955 // Load the bodies of any functions or methods we've encountered. We do
6956 // this now (delayed) so that we can be sure that the declaration chains
6957 // have been fully wired up.
6958 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
6959 PBEnd = PendingBodies.end();
6960 PB != PBEnd; ++PB) {
6961 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
6962 // FIXME: Check for =delete/=default?
6963 // FIXME: Complain about ODR violations here?
6964 if (!getContext().getLangOpts().Modules || !FD->hasBody())
6965 FD->setLazyBody(PB->second);
6966 continue;
6967 }
6968
6969 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
6970 if (!getContext().getLangOpts().Modules || !MD->hasBody())
6971 MD->setLazyBody(PB->second);
6972 }
6973 PendingBodies.clear();
6974}
6975
6976void ASTReader::FinishedDeserializing() {
6977 assert(NumCurrentElementsDeserializing &&
6978 "FinishedDeserializing not paired with StartedDeserializing");
6979 if (NumCurrentElementsDeserializing == 1) {
6980 // We decrease NumCurrentElementsDeserializing only after pending actions
6981 // are finished, to avoid recursively re-calling finishPendingActions().
6982 finishPendingActions();
6983 }
6984 --NumCurrentElementsDeserializing;
6985
6986 if (NumCurrentElementsDeserializing == 0 &&
6987 Consumer && !PassingDeclsToConsumer) {
6988 // Guard variable to avoid recursively redoing the process of passing
6989 // decls to consumer.
6990 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
6991 true);
6992
6993 while (!InterestingDecls.empty()) {
6994 // We are not in recursive loading, so it's safe to pass the "interesting"
6995 // decls to the consumer.
6996 Decl *D = InterestingDecls.front();
6997 InterestingDecls.pop_front();
6998 PassInterestingDeclToConsumer(D);
6999 }
7000 }
7001}
7002
7003ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context,
7004 StringRef isysroot, bool DisableValidation,
Douglas Gregorf575d6e2013-01-25 00:45:27 +00007005 bool AllowASTWithCompilerErrors, bool UseGlobalIndex)
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007006 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
7007 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
7008 Diags(PP.getDiagnostics()), SemaObj(0), PP(PP), Context(Context),
7009 Consumer(0), ModuleMgr(PP.getFileManager()),
7010 isysroot(isysroot), DisableValidation(DisableValidation),
Douglas Gregore1698072013-01-25 00:38:33 +00007011 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
Douglas Gregorf575d6e2013-01-25 00:45:27 +00007012 UseGlobalIndex(UseGlobalIndex), TriedLoadingGlobalIndex(false),
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007013 CurrentGeneration(0), CurrSwitchCaseStmts(&SwitchCaseStmts),
7014 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Douglas Gregore1698072013-01-25 00:38:33 +00007015 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
7016 TotalNumMacros(0), NumIdentifierLookups(0), NumIdentifierLookupHits(0),
7017 NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007018 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
7019 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
7020 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
7021 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
7022 PassingDeclsToConsumer(false),
7023 NumCXXBaseSpecifiersLoaded(0)
7024{
7025 SourceMgr.setExternalSLocEntrySource(this);
7026}
7027
7028ASTReader::~ASTReader() {
7029 for (DeclContextVisibleUpdatesPending::iterator
7030 I = PendingVisibleUpdates.begin(),
7031 E = PendingVisibleUpdates.end();
7032 I != E; ++I) {
7033 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
7034 F = I->second.end();
7035 J != F; ++J)
7036 delete J->first;
7037 }
7038}