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