blob: 5123b79d2e94cb8a5a2f1110a49d22582deae92e [file] [log] [blame]
Nick Lewyckyf0f56162013-01-31 03:23:57 +00001//===--- ASTReader.cpp - AST File Reader ----------------------------------===//
Guy Benyei11169dd2012-12-18 14:30:41 +00002//
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"
Guy Benyei11169dd2012-12-18 14:30:41 +000026#include "clang/Basic/SourceManager.h"
27#include "clang/Basic/SourceManagerInternals.h"
28#include "clang/Basic/TargetInfo.h"
29#include "clang/Basic/TargetOptions.h"
30#include "clang/Basic/Version.h"
31#include "clang/Basic/VersionTuple.h"
32#include "clang/Lex/HeaderSearch.h"
33#include "clang/Lex/HeaderSearchOptions.h"
34#include "clang/Lex/MacroInfo.h"
35#include "clang/Lex/PreprocessingRecord.h"
36#include "clang/Lex/Preprocessor.h"
37#include "clang/Lex/PreprocessorOptions.h"
38#include "clang/Sema/Scope.h"
39#include "clang/Sema/Sema.h"
40#include "clang/Serialization/ASTDeserializationListener.h"
Douglas Gregore060e572013-01-25 01:03:03 +000041#include "clang/Serialization/GlobalModuleIndex.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000042#include "clang/Serialization/ModuleManager.h"
43#include "clang/Serialization/SerializationDiagnostic.h"
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +000044#include "llvm/ADT/Hashing.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000045#include "llvm/ADT/StringExtras.h"
46#include "llvm/Bitcode/BitstreamReader.h"
47#include "llvm/Support/ErrorHandling.h"
48#include "llvm/Support/FileSystem.h"
49#include "llvm/Support/MemoryBuffer.h"
50#include "llvm/Support/Path.h"
51#include "llvm/Support/SaveAndRestore.h"
52#include "llvm/Support/system_error.h"
53#include <algorithm>
Chris Lattner91f373e2013-01-20 00:57:52 +000054#include <cstdio>
Guy Benyei11169dd2012-12-18 14:30:41 +000055#include <iterator>
56
57using namespace clang;
58using namespace clang::serialization;
59using namespace clang::serialization::reader;
Chris Lattner7fb3bef2013-01-20 00:56:42 +000060using llvm::BitstreamCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +000061
62//===----------------------------------------------------------------------===//
63// PCH validator implementation
64//===----------------------------------------------------------------------===//
65
66ASTReaderListener::~ASTReaderListener() {}
67
68/// \brief Compare the given set of language options against an existing set of
69/// language options.
70///
71/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
72///
73/// \returns true if the languagae options mis-match, false otherwise.
74static bool checkLanguageOptions(const LangOptions &LangOpts,
75 const LangOptions &ExistingLangOpts,
76 DiagnosticsEngine *Diags) {
77#define LANGOPT(Name, Bits, Default, Description) \
78 if (ExistingLangOpts.Name != LangOpts.Name) { \
79 if (Diags) \
80 Diags->Report(diag::err_pch_langopt_mismatch) \
81 << Description << LangOpts.Name << ExistingLangOpts.Name; \
82 return true; \
83 }
84
85#define VALUE_LANGOPT(Name, Bits, Default, Description) \
86 if (ExistingLangOpts.Name != LangOpts.Name) { \
87 if (Diags) \
88 Diags->Report(diag::err_pch_langopt_value_mismatch) \
89 << Description; \
90 return true; \
91 }
92
93#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
94 if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \
95 if (Diags) \
96 Diags->Report(diag::err_pch_langopt_value_mismatch) \
97 << Description; \
98 return true; \
99 }
100
101#define BENIGN_LANGOPT(Name, Bits, Default, Description)
102#define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
103#include "clang/Basic/LangOptions.def"
104
105 if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
106 if (Diags)
107 Diags->Report(diag::err_pch_langopt_value_mismatch)
108 << "target Objective-C runtime";
109 return true;
110 }
111
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +0000112 if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
113 LangOpts.CommentOpts.BlockCommandNames) {
114 if (Diags)
115 Diags->Report(diag::err_pch_langopt_value_mismatch)
116 << "block command names";
117 return true;
118 }
119
Guy Benyei11169dd2012-12-18 14:30:41 +0000120 return false;
121}
122
123/// \brief Compare the given set of target options against an existing set of
124/// target options.
125///
126/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
127///
128/// \returns true if the target options mis-match, false otherwise.
129static bool checkTargetOptions(const TargetOptions &TargetOpts,
130 const TargetOptions &ExistingTargetOpts,
131 DiagnosticsEngine *Diags) {
132#define CHECK_TARGET_OPT(Field, Name) \
133 if (TargetOpts.Field != ExistingTargetOpts.Field) { \
134 if (Diags) \
135 Diags->Report(diag::err_pch_targetopt_mismatch) \
136 << Name << TargetOpts.Field << ExistingTargetOpts.Field; \
137 return true; \
138 }
139
140 CHECK_TARGET_OPT(Triple, "target");
141 CHECK_TARGET_OPT(CPU, "target CPU");
142 CHECK_TARGET_OPT(ABI, "target ABI");
143 CHECK_TARGET_OPT(CXXABI, "target C++ ABI");
144 CHECK_TARGET_OPT(LinkerVersion, "target linker version");
145#undef CHECK_TARGET_OPT
146
147 // Compare feature sets.
148 SmallVector<StringRef, 4> ExistingFeatures(
149 ExistingTargetOpts.FeaturesAsWritten.begin(),
150 ExistingTargetOpts.FeaturesAsWritten.end());
151 SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
152 TargetOpts.FeaturesAsWritten.end());
153 std::sort(ExistingFeatures.begin(), ExistingFeatures.end());
154 std::sort(ReadFeatures.begin(), ReadFeatures.end());
155
156 unsigned ExistingIdx = 0, ExistingN = ExistingFeatures.size();
157 unsigned ReadIdx = 0, ReadN = ReadFeatures.size();
158 while (ExistingIdx < ExistingN && ReadIdx < ReadN) {
159 if (ExistingFeatures[ExistingIdx] == ReadFeatures[ReadIdx]) {
160 ++ExistingIdx;
161 ++ReadIdx;
162 continue;
163 }
164
165 if (ReadFeatures[ReadIdx] < ExistingFeatures[ExistingIdx]) {
166 if (Diags)
167 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
168 << false << ReadFeatures[ReadIdx];
169 return true;
170 }
171
172 if (Diags)
173 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
174 << true << ExistingFeatures[ExistingIdx];
175 return true;
176 }
177
178 if (ExistingIdx < ExistingN) {
179 if (Diags)
180 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
181 << true << ExistingFeatures[ExistingIdx];
182 return true;
183 }
184
185 if (ReadIdx < ReadN) {
186 if (Diags)
187 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
188 << false << ReadFeatures[ReadIdx];
189 return true;
190 }
191
192 return false;
193}
194
195bool
196PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
197 bool Complain) {
198 const LangOptions &ExistingLangOpts = PP.getLangOpts();
199 return checkLanguageOptions(LangOpts, ExistingLangOpts,
200 Complain? &Reader.Diags : 0);
201}
202
203bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
204 bool Complain) {
205 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
206 return checkTargetOptions(TargetOpts, ExistingTargetOpts,
207 Complain? &Reader.Diags : 0);
208}
209
210namespace {
211 typedef llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> >
212 MacroDefinitionsMap;
213}
214
215/// \brief Collect the macro definitions provided by the given preprocessor
216/// options.
217static void collectMacroDefinitions(const PreprocessorOptions &PPOpts,
218 MacroDefinitionsMap &Macros,
219 SmallVectorImpl<StringRef> *MacroNames = 0){
220 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
221 StringRef Macro = PPOpts.Macros[I].first;
222 bool IsUndef = PPOpts.Macros[I].second;
223
224 std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
225 StringRef MacroName = MacroPair.first;
226 StringRef MacroBody = MacroPair.second;
227
228 // For an #undef'd macro, we only care about the name.
229 if (IsUndef) {
230 if (MacroNames && !Macros.count(MacroName))
231 MacroNames->push_back(MacroName);
232
233 Macros[MacroName] = std::make_pair("", true);
234 continue;
235 }
236
237 // For a #define'd macro, figure out the actual definition.
238 if (MacroName.size() == Macro.size())
239 MacroBody = "1";
240 else {
241 // Note: GCC drops anything following an end-of-line character.
242 StringRef::size_type End = MacroBody.find_first_of("\n\r");
243 MacroBody = MacroBody.substr(0, End);
244 }
245
246 if (MacroNames && !Macros.count(MacroName))
247 MacroNames->push_back(MacroName);
248 Macros[MacroName] = std::make_pair(MacroBody, false);
249 }
250}
251
252/// \brief Check the preprocessor options deserialized from the control block
253/// against the preprocessor options in an existing preprocessor.
254///
255/// \param Diags If non-null, produce diagnostics for any mismatches incurred.
256static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts,
257 const PreprocessorOptions &ExistingPPOpts,
258 DiagnosticsEngine *Diags,
259 FileManager &FileMgr,
260 std::string &SuggestedPredefines) {
261 // Check macro definitions.
262 MacroDefinitionsMap ASTFileMacros;
263 collectMacroDefinitions(PPOpts, ASTFileMacros);
264 MacroDefinitionsMap ExistingMacros;
265 SmallVector<StringRef, 4> ExistingMacroNames;
266 collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames);
267
268 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
269 // Dig out the macro definition in the existing preprocessor options.
270 StringRef MacroName = ExistingMacroNames[I];
271 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
272
273 // Check whether we know anything about this macro name or not.
274 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> >::iterator Known
275 = ASTFileMacros.find(MacroName);
276 if (Known == ASTFileMacros.end()) {
277 // FIXME: Check whether this identifier was referenced anywhere in the
278 // AST file. If so, we should reject the AST file. Unfortunately, this
279 // information isn't in the control block. What shall we do about it?
280
281 if (Existing.second) {
282 SuggestedPredefines += "#undef ";
283 SuggestedPredefines += MacroName.str();
284 SuggestedPredefines += '\n';
285 } else {
286 SuggestedPredefines += "#define ";
287 SuggestedPredefines += MacroName.str();
288 SuggestedPredefines += ' ';
289 SuggestedPredefines += Existing.first.str();
290 SuggestedPredefines += '\n';
291 }
292 continue;
293 }
294
295 // If the macro was defined in one but undef'd in the other, we have a
296 // conflict.
297 if (Existing.second != Known->second.second) {
298 if (Diags) {
299 Diags->Report(diag::err_pch_macro_def_undef)
300 << MacroName << Known->second.second;
301 }
302 return true;
303 }
304
305 // If the macro was #undef'd in both, or if the macro bodies are identical,
306 // it's fine.
307 if (Existing.second || Existing.first == Known->second.first)
308 continue;
309
310 // The macro bodies differ; complain.
311 if (Diags) {
312 Diags->Report(diag::err_pch_macro_def_conflict)
313 << MacroName << Known->second.first << Existing.first;
314 }
315 return true;
316 }
317
318 // Check whether we're using predefines.
319 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines) {
320 if (Diags) {
321 Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
322 }
323 return true;
324 }
325
326 // Compute the #include and #include_macros lines we need.
327 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
328 StringRef File = ExistingPPOpts.Includes[I];
329 if (File == ExistingPPOpts.ImplicitPCHInclude)
330 continue;
331
332 if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File)
333 != PPOpts.Includes.end())
334 continue;
335
336 SuggestedPredefines += "#include \"";
337 SuggestedPredefines +=
338 HeaderSearch::NormalizeDashIncludePath(File, FileMgr);
339 SuggestedPredefines += "\"\n";
340 }
341
342 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
343 StringRef File = ExistingPPOpts.MacroIncludes[I];
344 if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(),
345 File)
346 != PPOpts.MacroIncludes.end())
347 continue;
348
349 SuggestedPredefines += "#__include_macros \"";
350 SuggestedPredefines +=
351 HeaderSearch::NormalizeDashIncludePath(File, FileMgr);
352 SuggestedPredefines += "\"\n##\n";
353 }
354
355 return false;
356}
357
358bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
359 bool Complain,
360 std::string &SuggestedPredefines) {
361 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
362
363 return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
364 Complain? &Reader.Diags : 0,
365 PP.getFileManager(),
366 SuggestedPredefines);
367}
368
369void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
370 unsigned ID) {
371 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
372 ++NumHeaderInfos;
373}
374
375void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
376 PP.setCounterValue(Value);
377}
378
379//===----------------------------------------------------------------------===//
380// AST reader implementation
381//===----------------------------------------------------------------------===//
382
383void
384ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
385 DeserializationListener = Listener;
386}
387
388
389
390unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
391 return serialization::ComputeHash(Sel);
392}
393
394
395std::pair<unsigned, unsigned>
396ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
397 using namespace clang::io;
398 unsigned KeyLen = ReadUnalignedLE16(d);
399 unsigned DataLen = ReadUnalignedLE16(d);
400 return std::make_pair(KeyLen, DataLen);
401}
402
403ASTSelectorLookupTrait::internal_key_type
404ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
405 using namespace clang::io;
406 SelectorTable &SelTable = Reader.getContext().Selectors;
407 unsigned N = ReadUnalignedLE16(d);
408 IdentifierInfo *FirstII
Douglas Gregorc8a992f2013-01-21 16:52:34 +0000409 = Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
Guy Benyei11169dd2012-12-18 14:30:41 +0000410 if (N == 0)
411 return SelTable.getNullarySelector(FirstII);
412 else if (N == 1)
413 return SelTable.getUnarySelector(FirstII);
414
415 SmallVector<IdentifierInfo *, 16> Args;
416 Args.push_back(FirstII);
417 for (unsigned I = 1; I != N; ++I)
Douglas Gregorc8a992f2013-01-21 16:52:34 +0000418 Args.push_back(Reader.getLocalIdentifier(F, ReadUnalignedLE32(d)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000419
420 return SelTable.getSelector(N, Args.data());
421}
422
423ASTSelectorLookupTrait::data_type
424ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
425 unsigned DataLen) {
426 using namespace clang::io;
427
428 data_type Result;
429
430 Result.ID = Reader.getGlobalSelectorID(F, ReadUnalignedLE32(d));
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +0000431 unsigned NumInstanceMethodsAndBits = ReadUnalignedLE16(d);
432 unsigned NumFactoryMethodsAndBits = ReadUnalignedLE16(d);
433 Result.InstanceBits = NumInstanceMethodsAndBits & 0x3;
434 Result.FactoryBits = NumFactoryMethodsAndBits & 0x3;
435 unsigned NumInstanceMethods = NumInstanceMethodsAndBits >> 2;
436 unsigned NumFactoryMethods = NumFactoryMethodsAndBits >> 2;
Guy Benyei11169dd2012-12-18 14:30:41 +0000437
438 // Load instance methods
439 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
440 if (ObjCMethodDecl *Method
441 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d)))
442 Result.Instance.push_back(Method);
443 }
444
445 // Load factory methods
446 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
447 if (ObjCMethodDecl *Method
448 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d)))
449 Result.Factory.push_back(Method);
450 }
451
452 return Result;
453}
454
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000455unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
456 return llvm::HashString(a);
Guy Benyei11169dd2012-12-18 14:30:41 +0000457}
458
459std::pair<unsigned, unsigned>
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000460ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000461 using namespace clang::io;
462 unsigned DataLen = ReadUnalignedLE16(d);
463 unsigned KeyLen = ReadUnalignedLE16(d);
464 return std::make_pair(KeyLen, DataLen);
465}
466
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000467ASTIdentifierLookupTraitBase::internal_key_type
468ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000469 assert(n >= 2 && d[n-1] == '\0');
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000470 return StringRef((const char*) d, n-1);
Guy Benyei11169dd2012-12-18 14:30:41 +0000471}
472
Douglas Gregordcf25082013-02-11 18:16:18 +0000473/// \brief Whether the given identifier is "interesting".
474static bool isInterestingIdentifier(IdentifierInfo &II) {
475 return II.isPoisoned() ||
476 II.isExtensionToken() ||
477 II.getObjCOrBuiltinID() ||
478 II.hasRevertedTokenIDToIdentifier() ||
479 II.hadMacroDefinition() ||
480 II.getFETokenInfo<void>();
481}
482
Guy Benyei11169dd2012-12-18 14:30:41 +0000483IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
484 const unsigned char* d,
485 unsigned DataLen) {
486 using namespace clang::io;
487 unsigned RawID = ReadUnalignedLE32(d);
488 bool IsInteresting = RawID & 0x01;
489
490 // Wipe out the "is interesting" bit.
491 RawID = RawID >> 1;
492
493 IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
494 if (!IsInteresting) {
495 // For uninteresting identifiers, just build the IdentifierInfo
496 // and associate it with the persistent ID.
497 IdentifierInfo *II = KnownII;
498 if (!II) {
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000499 II = &Reader.getIdentifierTable().getOwn(k);
Guy Benyei11169dd2012-12-18 14:30:41 +0000500 KnownII = II;
501 }
502 Reader.SetIdentifierInfo(ID, II);
Douglas Gregordcf25082013-02-11 18:16:18 +0000503 if (!II->isFromAST()) {
504 bool WasInteresting = isInterestingIdentifier(*II);
505 II->setIsFromAST();
506 if (WasInteresting)
507 II->setChangedSinceDeserialization();
508 }
509 Reader.markIdentifierUpToDate(II);
Guy Benyei11169dd2012-12-18 14:30:41 +0000510 return II;
511 }
512
513 unsigned ObjCOrBuiltinID = ReadUnalignedLE16(d);
514 unsigned Bits = ReadUnalignedLE16(d);
515 bool CPlusPlusOperatorKeyword = Bits & 0x01;
516 Bits >>= 1;
517 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
518 Bits >>= 1;
519 bool Poisoned = Bits & 0x01;
520 Bits >>= 1;
521 bool ExtensionToken = Bits & 0x01;
522 Bits >>= 1;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +0000523 bool hasSubmoduleMacros = Bits & 0x01;
524 Bits >>= 1;
Guy Benyei11169dd2012-12-18 14:30:41 +0000525 bool hadMacroDefinition = Bits & 0x01;
526 Bits >>= 1;
527
528 assert(Bits == 0 && "Extra bits in the identifier?");
529 DataLen -= 8;
530
531 // Build the IdentifierInfo itself and link the identifier ID with
532 // the new IdentifierInfo.
533 IdentifierInfo *II = KnownII;
534 if (!II) {
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000535 II = &Reader.getIdentifierTable().getOwn(StringRef(k));
Guy Benyei11169dd2012-12-18 14:30:41 +0000536 KnownII = II;
537 }
538 Reader.markIdentifierUpToDate(II);
Douglas Gregordcf25082013-02-11 18:16:18 +0000539 if (!II->isFromAST()) {
540 bool WasInteresting = isInterestingIdentifier(*II);
541 II->setIsFromAST();
542 if (WasInteresting)
543 II->setChangedSinceDeserialization();
544 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000545
546 // Set or check the various bits in the IdentifierInfo structure.
547 // Token IDs are read-only.
Argyrios Kyrtzidisddee8c92013-02-27 01:13:51 +0000548 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
Guy Benyei11169dd2012-12-18 14:30:41 +0000549 II->RevertTokenIDToIdentifier();
550 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
551 assert(II->isExtensionToken() == ExtensionToken &&
552 "Incorrect extension token flag");
553 (void)ExtensionToken;
554 if (Poisoned)
555 II->setIsPoisoned(true);
556 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
557 "Incorrect C++ operator keyword flag");
558 (void)CPlusPlusOperatorKeyword;
559
560 // If this identifier is a macro, deserialize the macro
561 // definition.
562 if (hadMacroDefinition) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +0000563 uint32_t MacroDirectivesOffset = ReadUnalignedLE32(d);
564 DataLen -= 4;
565 SmallVector<uint32_t, 8> LocalMacroIDs;
566 if (hasSubmoduleMacros) {
567 while (uint32_t LocalMacroID = ReadUnalignedLE32(d)) {
568 DataLen -= 4;
569 LocalMacroIDs.push_back(LocalMacroID);
570 }
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +0000571 DataLen -= 4;
572 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +0000573
574 if (F.Kind == MK_Module) {
575 for (SmallVectorImpl<uint32_t>::iterator
576 I = LocalMacroIDs.begin(), E = LocalMacroIDs.end(); I != E; ++I) {
577 MacroID MacID = Reader.getGlobalMacroID(F, *I);
578 Reader.addPendingMacroFromModule(II, &F, MacID, F.DirectImportLoc);
579 }
580 } else {
581 Reader.addPendingMacroFromPCH(II, &F, MacroDirectivesOffset);
582 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000583 }
584
585 Reader.SetIdentifierInfo(ID, II);
586
587 // Read all of the declarations visible at global scope with this
588 // name.
589 if (DataLen > 0) {
590 SmallVector<uint32_t, 4> DeclIDs;
591 for (; DataLen > 0; DataLen -= 4)
592 DeclIDs.push_back(Reader.getGlobalDeclID(F, ReadUnalignedLE32(d)));
593 Reader.SetGloballyVisibleDecls(II, DeclIDs);
594 }
595
596 return II;
597}
598
599unsigned
600ASTDeclContextNameLookupTrait::ComputeHash(const DeclNameKey &Key) const {
601 llvm::FoldingSetNodeID ID;
602 ID.AddInteger(Key.Kind);
603
604 switch (Key.Kind) {
605 case DeclarationName::Identifier:
606 case DeclarationName::CXXLiteralOperatorName:
607 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
608 break;
609 case DeclarationName::ObjCZeroArgSelector:
610 case DeclarationName::ObjCOneArgSelector:
611 case DeclarationName::ObjCMultiArgSelector:
612 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
613 break;
614 case DeclarationName::CXXOperatorName:
615 ID.AddInteger((OverloadedOperatorKind)Key.Data);
616 break;
617 case DeclarationName::CXXConstructorName:
618 case DeclarationName::CXXDestructorName:
619 case DeclarationName::CXXConversionFunctionName:
620 case DeclarationName::CXXUsingDirective:
621 break;
622 }
623
624 return ID.ComputeHash();
625}
626
627ASTDeclContextNameLookupTrait::internal_key_type
628ASTDeclContextNameLookupTrait::GetInternalKey(
629 const external_key_type& Name) const {
630 DeclNameKey Key;
631 Key.Kind = Name.getNameKind();
632 switch (Name.getNameKind()) {
633 case DeclarationName::Identifier:
634 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
635 break;
636 case DeclarationName::ObjCZeroArgSelector:
637 case DeclarationName::ObjCOneArgSelector:
638 case DeclarationName::ObjCMultiArgSelector:
639 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
640 break;
641 case DeclarationName::CXXOperatorName:
642 Key.Data = Name.getCXXOverloadedOperator();
643 break;
644 case DeclarationName::CXXLiteralOperatorName:
645 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
646 break;
647 case DeclarationName::CXXConstructorName:
648 case DeclarationName::CXXDestructorName:
649 case DeclarationName::CXXConversionFunctionName:
650 case DeclarationName::CXXUsingDirective:
651 Key.Data = 0;
652 break;
653 }
654
655 return Key;
656}
657
658std::pair<unsigned, unsigned>
659ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
660 using namespace clang::io;
661 unsigned KeyLen = ReadUnalignedLE16(d);
662 unsigned DataLen = ReadUnalignedLE16(d);
663 return std::make_pair(KeyLen, DataLen);
664}
665
666ASTDeclContextNameLookupTrait::internal_key_type
667ASTDeclContextNameLookupTrait::ReadKey(const unsigned char* d, unsigned) {
668 using namespace clang::io;
669
670 DeclNameKey Key;
671 Key.Kind = (DeclarationName::NameKind)*d++;
672 switch (Key.Kind) {
673 case DeclarationName::Identifier:
674 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
675 break;
676 case DeclarationName::ObjCZeroArgSelector:
677 case DeclarationName::ObjCOneArgSelector:
678 case DeclarationName::ObjCMultiArgSelector:
679 Key.Data =
680 (uint64_t)Reader.getLocalSelector(F, ReadUnalignedLE32(d))
681 .getAsOpaquePtr();
682 break;
683 case DeclarationName::CXXOperatorName:
684 Key.Data = *d++; // OverloadedOperatorKind
685 break;
686 case DeclarationName::CXXLiteralOperatorName:
687 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
688 break;
689 case DeclarationName::CXXConstructorName:
690 case DeclarationName::CXXDestructorName:
691 case DeclarationName::CXXConversionFunctionName:
692 case DeclarationName::CXXUsingDirective:
693 Key.Data = 0;
694 break;
695 }
696
697 return Key;
698}
699
700ASTDeclContextNameLookupTrait::data_type
701ASTDeclContextNameLookupTrait::ReadData(internal_key_type,
702 const unsigned char* d,
703 unsigned DataLen) {
704 using namespace clang::io;
705 unsigned NumDecls = ReadUnalignedLE16(d);
Argyrios Kyrtzidisc57e5032013-01-11 22:29:49 +0000706 LE32DeclID *Start = reinterpret_cast<LE32DeclID *>(
707 const_cast<unsigned char *>(d));
Guy Benyei11169dd2012-12-18 14:30:41 +0000708 return std::make_pair(Start, Start + NumDecls);
709}
710
711bool ASTReader::ReadDeclContextStorage(ModuleFile &M,
Chris Lattner7fb3bef2013-01-20 00:56:42 +0000712 BitstreamCursor &Cursor,
Guy Benyei11169dd2012-12-18 14:30:41 +0000713 const std::pair<uint64_t, uint64_t> &Offsets,
714 DeclContextInfo &Info) {
715 SavedStreamPosition SavedPosition(Cursor);
716 // First the lexical decls.
717 if (Offsets.first != 0) {
718 Cursor.JumpToBit(Offsets.first);
719
720 RecordData Record;
Chris Lattner0e6c9402013-01-20 02:38:54 +0000721 StringRef Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +0000722 unsigned Code = Cursor.ReadCode();
Chris Lattner0e6c9402013-01-20 02:38:54 +0000723 unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +0000724 if (RecCode != DECL_CONTEXT_LEXICAL) {
725 Error("Expected lexical block");
726 return true;
727 }
728
Chris Lattner0e6c9402013-01-20 02:38:54 +0000729 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob.data());
730 Info.NumLexicalDecls = Blob.size() / sizeof(KindDeclIDPair);
Guy Benyei11169dd2012-12-18 14:30:41 +0000731 }
732
733 // Now the lookup table.
734 if (Offsets.second != 0) {
735 Cursor.JumpToBit(Offsets.second);
736
737 RecordData Record;
Chris Lattner0e6c9402013-01-20 02:38:54 +0000738 StringRef Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +0000739 unsigned Code = Cursor.ReadCode();
Chris Lattner0e6c9402013-01-20 02:38:54 +0000740 unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +0000741 if (RecCode != DECL_CONTEXT_VISIBLE) {
742 Error("Expected visible lookup table block");
743 return true;
744 }
745 Info.NameLookupTableData
746 = ASTDeclContextNameLookupTable::Create(
Chris Lattner0e6c9402013-01-20 02:38:54 +0000747 (const unsigned char *)Blob.data() + Record[0],
748 (const unsigned char *)Blob.data(),
Guy Benyei11169dd2012-12-18 14:30:41 +0000749 ASTDeclContextNameLookupTrait(*this, M));
750 }
751
752 return false;
753}
754
755void ASTReader::Error(StringRef Msg) {
756 Error(diag::err_fe_pch_malformed, Msg);
757}
758
759void ASTReader::Error(unsigned DiagID,
760 StringRef Arg1, StringRef Arg2) {
761 if (Diags.isDiagnosticInFlight())
762 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
763 else
764 Diag(DiagID) << Arg1 << Arg2;
765}
766
767//===----------------------------------------------------------------------===//
768// Source Manager Deserialization
769//===----------------------------------------------------------------------===//
770
771/// \brief Read the line table in the source manager block.
772/// \returns true if there was an error.
773bool ASTReader::ParseLineTable(ModuleFile &F,
774 SmallVectorImpl<uint64_t> &Record) {
775 unsigned Idx = 0;
776 LineTableInfo &LineTable = SourceMgr.getLineTable();
777
778 // Parse the file names
779 std::map<int, int> FileIDs;
780 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
781 // Extract the file name
782 unsigned FilenameLen = Record[Idx++];
783 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
784 Idx += FilenameLen;
785 MaybeAddSystemRootToFilename(F, Filename);
786 FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
787 }
788
789 // Parse the line entries
790 std::vector<LineEntry> Entries;
791 while (Idx < Record.size()) {
792 int FID = Record[Idx++];
793 assert(FID >= 0 && "Serialized line entries for non-local file.");
794 // Remap FileID from 1-based old view.
795 FID += F.SLocEntryBaseID - 1;
796
797 // Extract the line entries
798 unsigned NumEntries = Record[Idx++];
799 assert(NumEntries && "Numentries is 00000");
800 Entries.clear();
801 Entries.reserve(NumEntries);
802 for (unsigned I = 0; I != NumEntries; ++I) {
803 unsigned FileOffset = Record[Idx++];
804 unsigned LineNo = Record[Idx++];
805 int FilenameID = FileIDs[Record[Idx++]];
806 SrcMgr::CharacteristicKind FileKind
807 = (SrcMgr::CharacteristicKind)Record[Idx++];
808 unsigned IncludeOffset = Record[Idx++];
809 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
810 FileKind, IncludeOffset));
811 }
812 LineTable.AddEntry(FileID::get(FID), Entries);
813 }
814
815 return false;
816}
817
818/// \brief Read a source manager block
819bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
820 using namespace SrcMgr;
821
Chris Lattner7fb3bef2013-01-20 00:56:42 +0000822 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +0000823
824 // Set the source-location entry cursor to the current position in
825 // the stream. This cursor will be used to read the contents of the
826 // source manager block initially, and then lazily read
827 // source-location entries as needed.
828 SLocEntryCursor = F.Stream;
829
830 // The stream itself is going to skip over the source manager block.
831 if (F.Stream.SkipBlock()) {
832 Error("malformed block record in AST file");
833 return true;
834 }
835
836 // Enter the source manager block.
837 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
838 Error("malformed source manager block record in AST file");
839 return true;
840 }
841
842 RecordData Record;
843 while (true) {
Chris Lattnere7b154b2013-01-19 21:39:22 +0000844 llvm::BitstreamEntry E = SLocEntryCursor.advanceSkippingSubblocks();
845
846 switch (E.Kind) {
847 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
848 case llvm::BitstreamEntry::Error:
849 Error("malformed block record in AST file");
850 return true;
851 case llvm::BitstreamEntry::EndBlock:
Guy Benyei11169dd2012-12-18 14:30:41 +0000852 return false;
Chris Lattnere7b154b2013-01-19 21:39:22 +0000853 case llvm::BitstreamEntry::Record:
854 // The interesting case.
855 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000856 }
Chris Lattnere7b154b2013-01-19 21:39:22 +0000857
Guy Benyei11169dd2012-12-18 14:30:41 +0000858 // Read a record.
Guy Benyei11169dd2012-12-18 14:30:41 +0000859 Record.clear();
Chris Lattner15c3e7d2013-01-21 18:28:26 +0000860 StringRef Blob;
861 switch (SLocEntryCursor.readRecord(E.ID, Record, &Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000862 default: // Default behavior: ignore.
863 break;
864
865 case SM_SLOC_FILE_ENTRY:
866 case SM_SLOC_BUFFER_ENTRY:
867 case SM_SLOC_EXPANSION_ENTRY:
868 // Once we hit one of the source location entries, we're done.
869 return false;
870 }
871 }
872}
873
874/// \brief If a header file is not found at the path that we expect it to be
875/// and the PCH file was moved from its original location, try to resolve the
876/// file by assuming that header+PCH were moved together and the header is in
877/// the same place relative to the PCH.
878static std::string
879resolveFileRelativeToOriginalDir(const std::string &Filename,
880 const std::string &OriginalDir,
881 const std::string &CurrDir) {
882 assert(OriginalDir != CurrDir &&
883 "No point trying to resolve the file if the PCH dir didn't change");
884 using namespace llvm::sys;
885 SmallString<128> filePath(Filename);
886 fs::make_absolute(filePath);
887 assert(path::is_absolute(OriginalDir));
888 SmallString<128> currPCHPath(CurrDir);
889
890 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
891 fileDirE = path::end(path::parent_path(filePath));
892 path::const_iterator origDirI = path::begin(OriginalDir),
893 origDirE = path::end(OriginalDir);
894 // Skip the common path components from filePath and OriginalDir.
895 while (fileDirI != fileDirE && origDirI != origDirE &&
896 *fileDirI == *origDirI) {
897 ++fileDirI;
898 ++origDirI;
899 }
900 for (; origDirI != origDirE; ++origDirI)
901 path::append(currPCHPath, "..");
902 path::append(currPCHPath, fileDirI, fileDirE);
903 path::append(currPCHPath, path::filename(Filename));
904 return currPCHPath.str();
905}
906
907bool ASTReader::ReadSLocEntry(int ID) {
908 if (ID == 0)
909 return false;
910
911 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
912 Error("source location entry ID out-of-range for AST file");
913 return true;
914 }
915
916 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
917 F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]);
Chris Lattner7fb3bef2013-01-20 00:56:42 +0000918 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +0000919 unsigned BaseOffset = F->SLocEntryBaseOffset;
920
921 ++NumSLocEntriesRead;
Chris Lattnere7b154b2013-01-19 21:39:22 +0000922 llvm::BitstreamEntry Entry = SLocEntryCursor.advance();
923 if (Entry.Kind != llvm::BitstreamEntry::Record) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000924 Error("incorrectly-formatted source location entry in AST file");
925 return true;
926 }
Chris Lattnere7b154b2013-01-19 21:39:22 +0000927
Guy Benyei11169dd2012-12-18 14:30:41 +0000928 RecordData Record;
Chris Lattner0e6c9402013-01-20 02:38:54 +0000929 StringRef Blob;
930 switch (SLocEntryCursor.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000931 default:
932 Error("incorrectly-formatted source location entry in AST file");
933 return true;
934
935 case SM_SLOC_FILE_ENTRY: {
936 // We will detect whether a file changed and return 'Failure' for it, but
937 // we will also try to fail gracefully by setting up the SLocEntry.
938 unsigned InputID = Record[4];
939 InputFile IF = getInputFile(*F, InputID);
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +0000940 const FileEntry *File = IF.getFile();
941 bool OverriddenBuffer = IF.isOverridden();
Guy Benyei11169dd2012-12-18 14:30:41 +0000942
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +0000943 // Note that we only check if a File was returned. If it was out-of-date
944 // we have complained but we will continue creating a FileID to recover
945 // gracefully.
946 if (!File)
Guy Benyei11169dd2012-12-18 14:30:41 +0000947 return true;
948
949 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
950 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
951 // This is the module's main file.
952 IncludeLoc = getImportLocation(F);
953 }
954 SrcMgr::CharacteristicKind
955 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
956 FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
957 ID, BaseOffset + Record[0]);
958 SrcMgr::FileInfo &FileInfo =
959 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
960 FileInfo.NumCreatedFIDs = Record[5];
961 if (Record[3])
962 FileInfo.setHasLineDirectives();
963
964 const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
965 unsigned NumFileDecls = Record[7];
966 if (NumFileDecls) {
967 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
968 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
969 NumFileDecls));
970 }
971
972 const SrcMgr::ContentCache *ContentCache
973 = SourceMgr.getOrCreateContentCache(File,
974 /*isSystemFile=*/FileCharacter != SrcMgr::C_User);
975 if (OverriddenBuffer && !ContentCache->BufferOverridden &&
976 ContentCache->ContentsEntry == ContentCache->OrigEntry) {
977 unsigned Code = SLocEntryCursor.ReadCode();
978 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +0000979 unsigned RecCode = SLocEntryCursor.readRecord(Code, Record, &Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +0000980
981 if (RecCode != SM_SLOC_BUFFER_BLOB) {
982 Error("AST record has invalid code");
983 return true;
984 }
985
986 llvm::MemoryBuffer *Buffer
Chris Lattner0e6c9402013-01-20 02:38:54 +0000987 = llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), File->getName());
Guy Benyei11169dd2012-12-18 14:30:41 +0000988 SourceMgr.overrideFileContents(File, Buffer);
989 }
990
991 break;
992 }
993
994 case SM_SLOC_BUFFER_ENTRY: {
Chris Lattner0e6c9402013-01-20 02:38:54 +0000995 const char *Name = Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +0000996 unsigned Offset = Record[0];
997 SrcMgr::CharacteristicKind
998 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
999 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1000 if (IncludeLoc.isInvalid() && F->Kind == MK_Module) {
1001 IncludeLoc = getImportLocation(F);
1002 }
1003 unsigned Code = SLocEntryCursor.ReadCode();
1004 Record.clear();
1005 unsigned RecCode
Chris Lattner0e6c9402013-01-20 02:38:54 +00001006 = SLocEntryCursor.readRecord(Code, Record, &Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +00001007
1008 if (RecCode != SM_SLOC_BUFFER_BLOB) {
1009 Error("AST record has invalid code");
1010 return true;
1011 }
1012
1013 llvm::MemoryBuffer *Buffer
Chris Lattner0e6c9402013-01-20 02:38:54 +00001014 = llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name);
Guy Benyei11169dd2012-12-18 14:30:41 +00001015 SourceMgr.createFileIDForMemBuffer(Buffer, FileCharacter, ID,
1016 BaseOffset + Offset, IncludeLoc);
1017 break;
1018 }
1019
1020 case SM_SLOC_EXPANSION_ENTRY: {
1021 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1022 SourceMgr.createExpansionLoc(SpellingLoc,
1023 ReadSourceLocation(*F, Record[2]),
1024 ReadSourceLocation(*F, Record[3]),
1025 Record[4],
1026 ID,
1027 BaseOffset + Record[0]);
1028 break;
1029 }
1030 }
1031
1032 return false;
1033}
1034
1035std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1036 if (ID == 0)
1037 return std::make_pair(SourceLocation(), "");
1038
1039 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1040 Error("source location entry ID out-of-range for AST file");
1041 return std::make_pair(SourceLocation(), "");
1042 }
1043
1044 // Find which module file this entry lands in.
1045 ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
1046 if (M->Kind != MK_Module)
1047 return std::make_pair(SourceLocation(), "");
1048
1049 // FIXME: Can we map this down to a particular submodule? That would be
1050 // ideal.
1051 return std::make_pair(M->ImportLoc, llvm::sys::path::stem(M->FileName));
1052}
1053
1054/// \brief Find the location where the module F is imported.
1055SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1056 if (F->ImportLoc.isValid())
1057 return F->ImportLoc;
1058
1059 // Otherwise we have a PCH. It's considered to be "imported" at the first
1060 // location of its includer.
1061 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
1062 // Main file is the importer. We assume that it is the first entry in the
1063 // entry table. We can't ask the manager, because at the time of PCH loading
1064 // the main file entry doesn't exist yet.
1065 // The very first entry is the invalid instantiation loc, which takes up
1066 // offsets 0 and 1.
1067 return SourceLocation::getFromRawEncoding(2U);
1068 }
1069 //return F->Loaders[0]->FirstLoc;
1070 return F->ImportedBy[0]->FirstLoc;
1071}
1072
1073/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1074/// specified cursor. Read the abbreviations that are at the top of the block
1075/// and then leave the cursor pointing into the block.
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001076bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001077 if (Cursor.EnterSubBlock(BlockID)) {
1078 Error("malformed block record in AST file");
1079 return Failure;
1080 }
1081
1082 while (true) {
1083 uint64_t Offset = Cursor.GetCurrentBitNo();
1084 unsigned Code = Cursor.ReadCode();
1085
1086 // We expect all abbrevs to be at the start of the block.
1087 if (Code != llvm::bitc::DEFINE_ABBREV) {
1088 Cursor.JumpToBit(Offset);
1089 return false;
1090 }
1091 Cursor.ReadAbbrevRecord();
1092 }
1093}
1094
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001095MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001096 BitstreamCursor &Stream = F.MacroCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001097
1098 // Keep track of where we are in the stream, then jump back there
1099 // after reading this macro.
1100 SavedStreamPosition SavedPosition(Stream);
1101
1102 Stream.JumpToBit(Offset);
1103 RecordData Record;
1104 SmallVector<IdentifierInfo*, 16> MacroArgs;
1105 MacroInfo *Macro = 0;
1106
Guy Benyei11169dd2012-12-18 14:30:41 +00001107 while (true) {
Chris Lattnerefa77172013-01-20 00:00:22 +00001108 // Advance to the next record, but if we get to the end of the block, don't
1109 // pop it (removing all the abbreviations from the cursor) since we want to
1110 // be able to reseek within the block and read entries.
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001111 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
Chris Lattnerefa77172013-01-20 00:00:22 +00001112 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks(Flags);
1113
1114 switch (Entry.Kind) {
1115 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1116 case llvm::BitstreamEntry::Error:
1117 Error("malformed block record in AST file");
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001118 return Macro;
Chris Lattnerefa77172013-01-20 00:00:22 +00001119 case llvm::BitstreamEntry::EndBlock:
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001120 return Macro;
Chris Lattnerefa77172013-01-20 00:00:22 +00001121 case llvm::BitstreamEntry::Record:
1122 // The interesting case.
1123 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001124 }
1125
1126 // Read a record.
Guy Benyei11169dd2012-12-18 14:30:41 +00001127 Record.clear();
1128 PreprocessorRecordTypes RecType =
Chris Lattner0e6c9402013-01-20 02:38:54 +00001129 (PreprocessorRecordTypes)Stream.readRecord(Entry.ID, Record);
Guy Benyei11169dd2012-12-18 14:30:41 +00001130 switch (RecType) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001131 case PP_MACRO_DIRECTIVE_HISTORY:
1132 return Macro;
1133
Guy Benyei11169dd2012-12-18 14:30:41 +00001134 case PP_MACRO_OBJECT_LIKE:
1135 case PP_MACRO_FUNCTION_LIKE: {
1136 // If we already have a macro, that means that we've hit the end
1137 // of the definition of the macro we were looking for. We're
1138 // done.
1139 if (Macro)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001140 return Macro;
Guy Benyei11169dd2012-12-18 14:30:41 +00001141
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001142 unsigned NextIndex = 1; // Skip identifier ID.
1143 SubmoduleID SubModID = getGlobalSubmoduleID(F, Record[NextIndex++]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001144 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001145 MacroInfo *MI = PP.AllocateDeserializedMacroInfo(Loc, SubModID);
Argyrios Kyrtzidis7572be22013-01-07 19:16:23 +00001146 MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
Guy Benyei11169dd2012-12-18 14:30:41 +00001147 MI->setIsUsed(Record[NextIndex++]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001148
Guy Benyei11169dd2012-12-18 14:30:41 +00001149 if (RecType == PP_MACRO_FUNCTION_LIKE) {
1150 // Decode function-like macro info.
1151 bool isC99VarArgs = Record[NextIndex++];
1152 bool isGNUVarArgs = Record[NextIndex++];
1153 bool hasCommaPasting = Record[NextIndex++];
1154 MacroArgs.clear();
1155 unsigned NumArgs = Record[NextIndex++];
1156 for (unsigned i = 0; i != NumArgs; ++i)
1157 MacroArgs.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1158
1159 // Install function-like macro info.
1160 MI->setIsFunctionLike();
1161 if (isC99VarArgs) MI->setIsC99Varargs();
1162 if (isGNUVarArgs) MI->setIsGNUVarargs();
1163 if (hasCommaPasting) MI->setHasCommaPasting();
1164 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
1165 PP.getPreprocessorAllocator());
1166 }
1167
Guy Benyei11169dd2012-12-18 14:30:41 +00001168 // Remember that we saw this macro last so that we add the tokens that
1169 // form its body to it.
1170 Macro = MI;
1171
1172 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1173 Record[NextIndex]) {
1174 // We have a macro definition. Register the association
1175 PreprocessedEntityID
1176 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1177 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
Argyrios Kyrtzidis832de9f2013-02-22 18:35:59 +00001178 PreprocessingRecord::PPEntityID
1179 PPID = PPRec.getPPEntityID(GlobalID-1, /*isLoaded=*/true);
1180 MacroDefinition *PPDef =
1181 cast_or_null<MacroDefinition>(PPRec.getPreprocessedEntity(PPID));
1182 if (PPDef)
1183 PPRec.RegisterMacroDefinition(Macro, PPDef);
Guy Benyei11169dd2012-12-18 14:30:41 +00001184 }
1185
1186 ++NumMacrosRead;
1187 break;
1188 }
1189
1190 case PP_TOKEN: {
1191 // If we see a TOKEN before a PP_MACRO_*, then the file is
1192 // erroneous, just pretend we didn't see this.
1193 if (Macro == 0) break;
1194
1195 Token Tok;
1196 Tok.startToken();
1197 Tok.setLocation(ReadSourceLocation(F, Record[0]));
1198 Tok.setLength(Record[1]);
1199 if (IdentifierInfo *II = getLocalIdentifier(F, Record[2]))
1200 Tok.setIdentifierInfo(II);
1201 Tok.setKind((tok::TokenKind)Record[3]);
1202 Tok.setFlag((Token::TokenFlags)Record[4]);
1203 Macro->AddTokenToBody(Tok);
1204 break;
1205 }
1206 }
1207 }
1208}
1209
1210PreprocessedEntityID
1211ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const {
1212 ContinuousRangeMap<uint32_t, int, 2>::const_iterator
1213 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1214 assert(I != M.PreprocessedEntityRemap.end()
1215 && "Invalid index into preprocessed entity index remap");
1216
1217 return LocalID + I->second;
1218}
1219
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001220unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1221 return llvm::hash_combine(ikey.Size, ikey.ModTime);
Guy Benyei11169dd2012-12-18 14:30:41 +00001222}
1223
1224HeaderFileInfoTrait::internal_key_type
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001225HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
1226 internal_key_type ikey = { FE->getSize(), FE->getModificationTime(),
1227 FE->getName() };
1228 return ikey;
1229}
Guy Benyei11169dd2012-12-18 14:30:41 +00001230
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001231bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
1232 if (a.Size != b.Size || a.ModTime != b.ModTime)
Guy Benyei11169dd2012-12-18 14:30:41 +00001233 return false;
1234
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001235 if (strcmp(a.Filename, b.Filename) == 0)
1236 return true;
1237
Guy Benyei11169dd2012-12-18 14:30:41 +00001238 // Determine whether the actual files are equivalent.
Argyrios Kyrtzidis2a513e82013-03-04 20:33:40 +00001239 FileManager &FileMgr = Reader.getFileManager();
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001240 const FileEntry *FEA = FileMgr.getFile(a.Filename);
1241 const FileEntry *FEB = FileMgr.getFile(b.Filename);
Argyrios Kyrtzidis2a513e82013-03-04 20:33:40 +00001242 return (FEA && FEA == FEB);
Guy Benyei11169dd2012-12-18 14:30:41 +00001243}
1244
1245std::pair<unsigned, unsigned>
1246HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1247 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1248 unsigned DataLen = (unsigned) *d++;
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001249 return std::make_pair(KeyLen, DataLen);
Guy Benyei11169dd2012-12-18 14:30:41 +00001250}
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001251
1252HeaderFileInfoTrait::internal_key_type
1253HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
1254 internal_key_type ikey;
1255 ikey.Size = off_t(clang::io::ReadUnalignedLE64(d));
1256 ikey.ModTime = time_t(clang::io::ReadUnalignedLE64(d));
1257 ikey.Filename = (const char *)d;
1258 return ikey;
1259}
1260
Guy Benyei11169dd2012-12-18 14:30:41 +00001261HeaderFileInfoTrait::data_type
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001262HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
Guy Benyei11169dd2012-12-18 14:30:41 +00001263 unsigned DataLen) {
1264 const unsigned char *End = d + DataLen;
1265 using namespace clang::io;
1266 HeaderFileInfo HFI;
1267 unsigned Flags = *d++;
1268 HFI.isImport = (Flags >> 5) & 0x01;
1269 HFI.isPragmaOnce = (Flags >> 4) & 0x01;
1270 HFI.DirInfo = (Flags >> 2) & 0x03;
1271 HFI.Resolved = (Flags >> 1) & 0x01;
1272 HFI.IndexHeaderMapHeader = Flags & 0x01;
1273 HFI.NumIncludes = ReadUnalignedLE16(d);
1274 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(M,
1275 ReadUnalignedLE32(d));
1276 if (unsigned FrameworkOffset = ReadUnalignedLE32(d)) {
1277 // The framework offset is 1 greater than the actual offset,
1278 // since 0 is used as an indicator for "no framework name".
1279 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1280 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1281 }
1282
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001283 if (d != End) {
1284 uint32_t LocalSMID = ReadUnalignedLE32(d);
1285 if (LocalSMID) {
1286 // This header is part of a module. Associate it with the module to enable
1287 // implicit module import.
1288 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1289 Module *Mod = Reader.getSubmodule(GlobalSMID);
1290 HFI.isModuleHeader = true;
1291 FileManager &FileMgr = Reader.getFileManager();
1292 ModuleMap &ModMap =
1293 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
1294 ModMap.addHeader(Mod, FileMgr.getFile(key.Filename), /*Excluded=*/false);
1295 }
1296 }
1297
Guy Benyei11169dd2012-12-18 14:30:41 +00001298 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1299 (void)End;
1300
1301 // This HeaderFileInfo was externally loaded.
1302 HFI.External = true;
1303 return HFI;
1304}
1305
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001306void ASTReader::addPendingMacroFromModule(IdentifierInfo *II,
1307 ModuleFile *M,
1308 GlobalMacroID GMacID,
1309 SourceLocation ImportLoc) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001310 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001311 PendingMacroIDs[II].push_back(PendingMacroInfo(M, GMacID, ImportLoc));
1312}
1313
1314void ASTReader::addPendingMacroFromPCH(IdentifierInfo *II,
1315 ModuleFile *M,
1316 uint64_t MacroDirectivesOffset) {
1317 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1318 PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00001319}
1320
1321void ASTReader::ReadDefinedMacros() {
1322 // Note that we are loading defined macros.
1323 Deserializing Macros(this);
1324
1325 for (ModuleReverseIterator I = ModuleMgr.rbegin(),
1326 E = ModuleMgr.rend(); I != E; ++I) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001327 BitstreamCursor &MacroCursor = (*I)->MacroCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001328
1329 // If there was no preprocessor block, skip this file.
1330 if (!MacroCursor.getBitStreamReader())
1331 continue;
1332
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001333 BitstreamCursor Cursor = MacroCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001334 Cursor.JumpToBit((*I)->MacroStartOffset);
1335
1336 RecordData Record;
1337 while (true) {
Chris Lattnere7b154b2013-01-19 21:39:22 +00001338 llvm::BitstreamEntry E = Cursor.advanceSkippingSubblocks();
1339
1340 switch (E.Kind) {
1341 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1342 case llvm::BitstreamEntry::Error:
1343 Error("malformed block record in AST file");
1344 return;
1345 case llvm::BitstreamEntry::EndBlock:
1346 goto NextCursor;
1347
1348 case llvm::BitstreamEntry::Record:
Chris Lattnere7b154b2013-01-19 21:39:22 +00001349 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00001350 switch (Cursor.readRecord(E.ID, Record)) {
Chris Lattnere7b154b2013-01-19 21:39:22 +00001351 default: // Default behavior: ignore.
1352 break;
1353
1354 case PP_MACRO_OBJECT_LIKE:
1355 case PP_MACRO_FUNCTION_LIKE:
1356 getLocalIdentifier(**I, Record[0]);
1357 break;
1358
1359 case PP_TOKEN:
1360 // Ignore tokens.
1361 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001362 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001363 break;
1364 }
1365 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00001366 NextCursor: ;
Guy Benyei11169dd2012-12-18 14:30:41 +00001367 }
1368}
1369
1370namespace {
1371 /// \brief Visitor class used to look up identifirs in an AST file.
1372 class IdentifierLookupVisitor {
1373 StringRef Name;
1374 unsigned PriorGeneration;
Douglas Gregor00a50f72013-01-25 00:38:33 +00001375 unsigned &NumIdentifierLookups;
1376 unsigned &NumIdentifierLookupHits;
Guy Benyei11169dd2012-12-18 14:30:41 +00001377 IdentifierInfo *Found;
Douglas Gregor00a50f72013-01-25 00:38:33 +00001378
Guy Benyei11169dd2012-12-18 14:30:41 +00001379 public:
Douglas Gregor00a50f72013-01-25 00:38:33 +00001380 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
1381 unsigned &NumIdentifierLookups,
1382 unsigned &NumIdentifierLookupHits)
Douglas Gregor7211ac12013-01-25 23:32:03 +00001383 : Name(Name), PriorGeneration(PriorGeneration),
Douglas Gregor00a50f72013-01-25 00:38:33 +00001384 NumIdentifierLookups(NumIdentifierLookups),
1385 NumIdentifierLookupHits(NumIdentifierLookupHits),
1386 Found()
1387 {
1388 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001389
1390 static bool visit(ModuleFile &M, void *UserData) {
1391 IdentifierLookupVisitor *This
1392 = static_cast<IdentifierLookupVisitor *>(UserData);
1393
1394 // If we've already searched this module file, skip it now.
1395 if (M.Generation <= This->PriorGeneration)
1396 return true;
Douglas Gregore060e572013-01-25 01:03:03 +00001397
Guy Benyei11169dd2012-12-18 14:30:41 +00001398 ASTIdentifierLookupTable *IdTable
1399 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
1400 if (!IdTable)
1401 return false;
1402
1403 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(),
1404 M, This->Found);
Douglas Gregor00a50f72013-01-25 00:38:33 +00001405 ++This->NumIdentifierLookups;
1406 ASTIdentifierLookupTable::iterator Pos = IdTable->find(This->Name,&Trait);
Guy Benyei11169dd2012-12-18 14:30:41 +00001407 if (Pos == IdTable->end())
1408 return false;
1409
1410 // Dereferencing the iterator has the effect of building the
1411 // IdentifierInfo node and populating it with the various
1412 // declarations it needs.
Douglas Gregor00a50f72013-01-25 00:38:33 +00001413 ++This->NumIdentifierLookupHits;
Guy Benyei11169dd2012-12-18 14:30:41 +00001414 This->Found = *Pos;
1415 return true;
1416 }
1417
1418 // \brief Retrieve the identifier info found within the module
1419 // files.
1420 IdentifierInfo *getIdentifierInfo() const { return Found; }
1421 };
1422}
1423
1424void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
1425 // Note that we are loading an identifier.
1426 Deserializing AnIdentifier(this);
1427
1428 unsigned PriorGeneration = 0;
1429 if (getContext().getLangOpts().Modules)
1430 PriorGeneration = IdentifierGeneration[&II];
Douglas Gregore060e572013-01-25 01:03:03 +00001431
1432 // If there is a global index, look there first to determine which modules
1433 // provably do not have any results for this identifier.
Douglas Gregor7211ac12013-01-25 23:32:03 +00001434 GlobalModuleIndex::HitSet Hits;
1435 GlobalModuleIndex::HitSet *HitsPtr = 0;
Douglas Gregore060e572013-01-25 01:03:03 +00001436 if (!loadGlobalIndex()) {
Douglas Gregor7211ac12013-01-25 23:32:03 +00001437 if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
1438 HitsPtr = &Hits;
Douglas Gregore060e572013-01-25 01:03:03 +00001439 }
1440 }
1441
Douglas Gregor7211ac12013-01-25 23:32:03 +00001442 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
Douglas Gregor00a50f72013-01-25 00:38:33 +00001443 NumIdentifierLookups,
1444 NumIdentifierLookupHits);
Douglas Gregor7211ac12013-01-25 23:32:03 +00001445 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor, HitsPtr);
Guy Benyei11169dd2012-12-18 14:30:41 +00001446 markIdentifierUpToDate(&II);
1447}
1448
1449void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
1450 if (!II)
1451 return;
1452
1453 II->setOutOfDate(false);
1454
1455 // Update the generation for this identifier.
1456 if (getContext().getLangOpts().Modules)
1457 IdentifierGeneration[II] = CurrentGeneration;
1458}
1459
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001460void ASTReader::resolvePendingMacro(IdentifierInfo *II,
1461 const PendingMacroInfo &PMInfo) {
1462 assert(II);
1463
1464 if (PMInfo.M->Kind != MK_Module) {
1465 installPCHMacroDirectives(II, *PMInfo.M,
1466 PMInfo.PCHMacroData.MacroDirectivesOffset);
1467 return;
1468 }
1469
1470 // Module Macro.
1471
1472 GlobalMacroID GMacID = PMInfo.ModuleMacroData.GMacID;
1473 SourceLocation ImportLoc =
1474 SourceLocation::getFromRawEncoding(PMInfo.ModuleMacroData.ImportLoc);
1475
1476 assert(GMacID);
1477 // If this macro has already been loaded, don't do so again.
1478 if (MacrosLoaded[GMacID - NUM_PREDEF_MACRO_IDS])
1479 return;
1480
1481 MacroInfo *MI = getMacro(GMacID);
1482 SubmoduleID SubModID = MI->getOwningModuleID();
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001483 MacroDirective *MD = PP.AllocateDefMacroDirective(MI, ImportLoc,
1484 /*isImported=*/true);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001485
1486 // Determine whether this macro definition is visible.
1487 bool Hidden = false;
Argyrios Kyrtzidis3a9c42c2013-03-27 01:25:34 +00001488 Module *Owner = 0;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001489 if (SubModID) {
Argyrios Kyrtzidis3a9c42c2013-03-27 01:25:34 +00001490 if ((Owner = getSubmodule(SubModID))) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001491 if (Owner->NameVisibility == Module::Hidden) {
1492 // The owning module is not visible, and this macro definition
1493 // should not be, either.
1494 Hidden = true;
1495
1496 // Note that this macro definition was hidden because its owning
1497 // module is not yet visible.
1498 HiddenNamesMap[Owner].push_back(HiddenName(II, MD));
1499 }
1500 }
1501 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001502
1503 if (!Hidden)
Argyrios Kyrtzidis3a9c42c2013-03-27 01:25:34 +00001504 installImportedMacro(II, MD, Owner);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001505}
1506
1507void ASTReader::installPCHMacroDirectives(IdentifierInfo *II,
1508 ModuleFile &M, uint64_t Offset) {
1509 assert(M.Kind != MK_Module);
1510
1511 BitstreamCursor &Cursor = M.MacroCursor;
1512 SavedStreamPosition SavedPosition(Cursor);
1513 Cursor.JumpToBit(Offset);
1514
1515 llvm::BitstreamEntry Entry =
1516 Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
1517 if (Entry.Kind != llvm::BitstreamEntry::Record) {
1518 Error("malformed block record in AST file");
1519 return;
1520 }
1521
1522 RecordData Record;
1523 PreprocessorRecordTypes RecType =
1524 (PreprocessorRecordTypes)Cursor.readRecord(Entry.ID, Record);
1525 if (RecType != PP_MACRO_DIRECTIVE_HISTORY) {
1526 Error("malformed block record in AST file");
1527 return;
1528 }
1529
1530 // Deserialize the macro directives history in reverse source-order.
1531 MacroDirective *Latest = 0, *Earliest = 0;
1532 unsigned Idx = 0, N = Record.size();
1533 while (Idx < N) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001534 MacroDirective *MD = 0;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001535 SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001536 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
1537 switch (K) {
1538 case MacroDirective::MD_Define: {
1539 GlobalMacroID GMacID = getGlobalMacroID(M, Record[Idx++]);
1540 MacroInfo *MI = getMacro(GMacID);
1541 bool isImported = Record[Idx++];
1542 bool isAmbiguous = Record[Idx++];
1543 DefMacroDirective *DefMD =
1544 PP.AllocateDefMacroDirective(MI, Loc, isImported);
1545 DefMD->setAmbiguous(isAmbiguous);
1546 MD = DefMD;
1547 break;
1548 }
1549 case MacroDirective::MD_Undefine:
1550 MD = PP.AllocateUndefMacroDirective(Loc);
1551 break;
1552 case MacroDirective::MD_Visibility: {
1553 bool isPublic = Record[Idx++];
1554 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
1555 break;
1556 }
1557 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001558
1559 if (!Latest)
1560 Latest = MD;
1561 if (Earliest)
1562 Earliest->setPrevious(MD);
1563 Earliest = MD;
1564 }
1565
1566 PP.setLoadedMacroDirective(II, Latest);
1567}
1568
Argyrios Kyrtzidis3a9c42c2013-03-27 01:25:34 +00001569/// \brief For the given macro definitions, check if they are both in system
Douglas Gregor0b202052013-04-12 21:00:54 +00001570/// modules.
1571static bool areDefinedInSystemModules(MacroInfo *PrevMI, MacroInfo *NewMI,
1572 Module *NewOwner, ASTReader &Reader) {
Argyrios Kyrtzidis3a9c42c2013-03-27 01:25:34 +00001573 assert(PrevMI && NewMI);
1574 if (!NewOwner)
1575 return false;
1576 Module *PrevOwner = 0;
1577 if (SubmoduleID PrevModID = PrevMI->getOwningModuleID())
1578 PrevOwner = Reader.getSubmodule(PrevModID);
1579 if (!PrevOwner)
1580 return false;
1581 if (PrevOwner == NewOwner)
1582 return false;
Douglas Gregor0b202052013-04-12 21:00:54 +00001583 return PrevOwner->IsSystem && NewOwner->IsSystem;
Argyrios Kyrtzidis3a9c42c2013-03-27 01:25:34 +00001584}
1585
1586void ASTReader::installImportedMacro(IdentifierInfo *II, MacroDirective *MD,
1587 Module *Owner) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001588 assert(II && MD);
1589
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001590 DefMacroDirective *DefMD = cast<DefMacroDirective>(MD);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001591 MacroDirective *Prev = PP.getMacroDirective(II);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001592 if (Prev) {
1593 MacroDirective::DefInfo PrevDef = Prev->getDefinition();
Argyrios Kyrtzidis3a9c42c2013-03-27 01:25:34 +00001594 MacroInfo *PrevMI = PrevDef.getMacroInfo();
1595 MacroInfo *NewMI = DefMD->getInfo();
Argyrios Kyrtzidis0c2f30b2013-04-03 17:39:30 +00001596 if (NewMI != PrevMI && !PrevMI->isIdenticalTo(*NewMI, PP,
1597 /*Syntactically=*/true)) {
Argyrios Kyrtzidis3a9c42c2013-03-27 01:25:34 +00001598 // Before marking the macros as ambiguous, check if this is a case where
Douglas Gregor0b202052013-04-12 21:00:54 +00001599 // both macros are in system headers. If so, we trust that the system
1600 // did not get it wrong. This also handles cases where Clang's own
1601 // headers have a different spelling of certain system macros:
Argyrios Kyrtzidis3a9c42c2013-03-27 01:25:34 +00001602 // #define LONG_MAX __LONG_MAX__ (clang's limits.h)
1603 // #define LONG_MAX 0x7fffffffffffffffL (system's limits.h)
Douglas Gregor0b202052013-04-12 21:00:54 +00001604 if (!areDefinedInSystemModules(PrevMI, NewMI, Owner, *this)) {
Argyrios Kyrtzidis3a9c42c2013-03-27 01:25:34 +00001605 PrevDef.getDirective()->setAmbiguous(true);
1606 DefMD->setAmbiguous(true);
1607 }
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001608 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001609 }
1610
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001611 PP.appendMacroDirective(II, MD);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001612}
1613
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001614InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001615 // If this ID is bogus, just return an empty input file.
1616 if (ID == 0 || ID > F.InputFilesLoaded.size())
1617 return InputFile();
1618
1619 // If we've already loaded this input file, return it.
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001620 if (F.InputFilesLoaded[ID-1].getFile())
Guy Benyei11169dd2012-12-18 14:30:41 +00001621 return F.InputFilesLoaded[ID-1];
1622
1623 // Go find this input file.
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001624 BitstreamCursor &Cursor = F.InputFilesCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001625 SavedStreamPosition SavedPosition(Cursor);
1626 Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
1627
1628 unsigned Code = Cursor.ReadCode();
1629 RecordData Record;
Chris Lattner0e6c9402013-01-20 02:38:54 +00001630 StringRef Blob;
1631 switch ((InputFileRecordTypes)Cursor.readRecord(Code, Record, &Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001632 case INPUT_FILE: {
1633 unsigned StoredID = Record[0];
1634 assert(ID == StoredID && "Bogus stored ID or offset");
1635 (void)StoredID;
1636 off_t StoredSize = (off_t)Record[1];
1637 time_t StoredTime = (time_t)Record[2];
1638 bool Overridden = (bool)Record[3];
1639
1640 // Get the file entry for this input file.
Chris Lattner0e6c9402013-01-20 02:38:54 +00001641 StringRef OrigFilename = Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00001642 std::string Filename = OrigFilename;
1643 MaybeAddSystemRootToFilename(F, Filename);
1644 const FileEntry *File
1645 = Overridden? FileMgr.getVirtualFile(Filename, StoredSize, StoredTime)
1646 : FileMgr.getFile(Filename, /*OpenFile=*/false);
1647
1648 // If we didn't find the file, resolve it relative to the
1649 // original directory from which this AST file was created.
1650 if (File == 0 && !F.OriginalDir.empty() && !CurrentDir.empty() &&
1651 F.OriginalDir != CurrentDir) {
1652 std::string Resolved = resolveFileRelativeToOriginalDir(Filename,
1653 F.OriginalDir,
1654 CurrentDir);
1655 if (!Resolved.empty())
1656 File = FileMgr.getFile(Resolved);
1657 }
1658
1659 // For an overridden file, create a virtual file with the stored
1660 // size/timestamp.
1661 if (Overridden && File == 0) {
1662 File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
1663 }
1664
1665 if (File == 0) {
1666 if (Complain) {
1667 std::string ErrorStr = "could not find file '";
1668 ErrorStr += Filename;
1669 ErrorStr += "' referenced by AST file";
1670 Error(ErrorStr.c_str());
1671 }
1672 return InputFile();
1673 }
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001674
Guy Benyei11169dd2012-12-18 14:30:41 +00001675 // Check if there was a request to override the contents of the file
1676 // that was part of the precompiled header. Overridding such a file
1677 // can lead to problems when lexing using the source locations from the
1678 // PCH.
1679 SourceManager &SM = getSourceManager();
1680 if (!Overridden && SM.isFileOverridden(File)) {
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001681 if (Complain)
1682 Error(diag::err_fe_pch_file_overridden, Filename);
Guy Benyei11169dd2012-12-18 14:30:41 +00001683 // After emitting the diagnostic, recover by disabling the override so
1684 // that the original file will be used.
1685 SM.disableFileContentsOverride(File);
1686 // The FileEntry is a virtual file entry with the size of the contents
1687 // that would override the original contents. Set it to the original's
1688 // size/time.
1689 FileMgr.modifyFileEntry(const_cast<FileEntry*>(File),
1690 StoredSize, StoredTime);
1691 }
1692
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001693 bool IsOutOfDate = false;
Guy Benyei11169dd2012-12-18 14:30:41 +00001694
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001695 // For an overridden file, there is nothing to validate.
1696 if (!Overridden && (StoredSize != File->getSize()
Guy Benyei11169dd2012-12-18 14:30:41 +00001697#if !defined(LLVM_ON_WIN32)
1698 // In our regression testing, the Windows file system seems to
1699 // have inconsistent modification times that sometimes
1700 // erroneously trigger this error-handling path.
1701 || StoredTime != File->getModificationTime()
1702#endif
1703 )) {
Douglas Gregor7029ce12013-03-19 00:28:20 +00001704 if (Complain) {
Argyrios Kyrtzidisc4720392013-03-08 20:42:38 +00001705 Error(diag::err_fe_pch_file_modified, Filename, F.FileName);
Douglas Gregor7029ce12013-03-19 00:28:20 +00001706 }
1707
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001708 IsOutOfDate = true;
Guy Benyei11169dd2012-12-18 14:30:41 +00001709 }
1710
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001711 InputFile IF = InputFile(File, Overridden, IsOutOfDate);
1712
1713 // Note that we've loaded this input file.
1714 F.InputFilesLoaded[ID-1] = IF;
1715 return IF;
Guy Benyei11169dd2012-12-18 14:30:41 +00001716 }
1717 }
1718
1719 return InputFile();
1720}
1721
1722const FileEntry *ASTReader::getFileEntry(StringRef filenameStrRef) {
1723 ModuleFile &M = ModuleMgr.getPrimaryModule();
1724 std::string Filename = filenameStrRef;
1725 MaybeAddSystemRootToFilename(M, Filename);
1726 const FileEntry *File = FileMgr.getFile(Filename);
1727 if (File == 0 && !M.OriginalDir.empty() && !CurrentDir.empty() &&
1728 M.OriginalDir != CurrentDir) {
1729 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1730 M.OriginalDir,
1731 CurrentDir);
1732 if (!resolved.empty())
1733 File = FileMgr.getFile(resolved);
1734 }
1735
1736 return File;
1737}
1738
1739/// \brief If we are loading a relocatable PCH file, and the filename is
1740/// not an absolute path, add the system root to the beginning of the file
1741/// name.
1742void ASTReader::MaybeAddSystemRootToFilename(ModuleFile &M,
1743 std::string &Filename) {
1744 // If this is not a relocatable PCH file, there's nothing to do.
1745 if (!M.RelocatablePCH)
1746 return;
1747
1748 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
1749 return;
1750
1751 if (isysroot.empty()) {
1752 // If no system root was given, default to '/'
1753 Filename.insert(Filename.begin(), '/');
1754 return;
1755 }
1756
1757 unsigned Length = isysroot.size();
1758 if (isysroot[Length - 1] != '/')
1759 Filename.insert(Filename.begin(), '/');
1760
1761 Filename.insert(Filename.begin(), isysroot.begin(), isysroot.end());
1762}
1763
1764ASTReader::ASTReadResult
1765ASTReader::ReadControlBlock(ModuleFile &F,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001766 SmallVectorImpl<ImportedModule> &Loaded,
Guy Benyei11169dd2012-12-18 14:30:41 +00001767 unsigned ClientLoadCapabilities) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001768 BitstreamCursor &Stream = F.Stream;
Guy Benyei11169dd2012-12-18 14:30:41 +00001769
1770 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
1771 Error("malformed block record in AST file");
1772 return Failure;
1773 }
1774
1775 // Read all of the records and blocks in the control block.
1776 RecordData Record;
Chris Lattnere7b154b2013-01-19 21:39:22 +00001777 while (1) {
1778 llvm::BitstreamEntry Entry = Stream.advance();
1779
1780 switch (Entry.Kind) {
1781 case llvm::BitstreamEntry::Error:
1782 Error("malformed block record in AST file");
1783 return Failure;
1784 case llvm::BitstreamEntry::EndBlock:
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001785 // Validate all of the non-system input files.
Guy Benyei11169dd2012-12-18 14:30:41 +00001786 if (!DisableValidation) {
1787 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001788 // All user input files reside at the index range [0, Record[1]).
1789 // Record is the one from INPUT_FILE_OFFSETS.
1790 for (unsigned I = 0, N = Record[1]; I < N; ++I) {
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001791 InputFile IF = getInputFile(F, I+1, Complain);
1792 if (!IF.getFile() || IF.isOutOfDate())
Guy Benyei11169dd2012-12-18 14:30:41 +00001793 return OutOfDate;
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001794 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001795 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001796 return Success;
Chris Lattnere7b154b2013-01-19 21:39:22 +00001797
1798 case llvm::BitstreamEntry::SubBlock:
1799 switch (Entry.ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001800 case INPUT_FILES_BLOCK_ID:
1801 F.InputFilesCursor = Stream;
1802 if (Stream.SkipBlock() || // Skip with the main cursor
1803 // Read the abbreviations
1804 ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
1805 Error("malformed block record in AST file");
1806 return Failure;
1807 }
1808 continue;
Chris Lattnere7b154b2013-01-19 21:39:22 +00001809
Guy Benyei11169dd2012-12-18 14:30:41 +00001810 default:
Chris Lattnere7b154b2013-01-19 21:39:22 +00001811 if (Stream.SkipBlock()) {
1812 Error("malformed block record in AST file");
1813 return Failure;
1814 }
1815 continue;
Guy Benyei11169dd2012-12-18 14:30:41 +00001816 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00001817
1818 case llvm::BitstreamEntry::Record:
1819 // The interesting case.
1820 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001821 }
1822
1823 // Read and process a record.
1824 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00001825 StringRef Blob;
1826 switch ((ControlRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001827 case METADATA: {
1828 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1829 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
1830 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
1831 : diag::warn_pch_version_too_new);
1832 return VersionMismatch;
1833 }
1834
1835 bool hasErrors = Record[5];
1836 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
1837 Diag(diag::err_pch_with_compiler_errors);
1838 return HadErrors;
1839 }
1840
1841 F.RelocatablePCH = Record[4];
1842
1843 const std::string &CurBranch = getClangFullRepositoryVersion();
Chris Lattner0e6c9402013-01-20 02:38:54 +00001844 StringRef ASTBranch = Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00001845 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
1846 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
1847 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
1848 return VersionMismatch;
1849 }
1850 break;
1851 }
1852
1853 case IMPORTS: {
1854 // Load each of the imported PCH files.
1855 unsigned Idx = 0, N = Record.size();
1856 while (Idx < N) {
1857 // Read information about the AST file.
1858 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
1859 // The import location will be the local one for now; we will adjust
1860 // all import locations of module imports after the global source
1861 // location info are setup.
1862 SourceLocation ImportLoc =
1863 SourceLocation::getFromRawEncoding(Record[Idx++]);
Douglas Gregor7029ce12013-03-19 00:28:20 +00001864 off_t StoredSize = (off_t)Record[Idx++];
1865 time_t StoredModTime = (time_t)Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00001866 unsigned Length = Record[Idx++];
1867 SmallString<128> ImportedFile(Record.begin() + Idx,
1868 Record.begin() + Idx + Length);
1869 Idx += Length;
1870
1871 // Load the AST file.
1872 switch(ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F, Loaded,
Douglas Gregor7029ce12013-03-19 00:28:20 +00001873 StoredSize, StoredModTime,
Guy Benyei11169dd2012-12-18 14:30:41 +00001874 ClientLoadCapabilities)) {
1875 case Failure: return Failure;
1876 // If we have to ignore the dependency, we'll have to ignore this too.
Douglas Gregor2f1806e2013-03-19 00:38:50 +00001877 case Missing:
Guy Benyei11169dd2012-12-18 14:30:41 +00001878 case OutOfDate: return OutOfDate;
1879 case VersionMismatch: return VersionMismatch;
1880 case ConfigurationMismatch: return ConfigurationMismatch;
1881 case HadErrors: return HadErrors;
1882 case Success: break;
1883 }
1884 }
1885 break;
1886 }
1887
1888 case LANGUAGE_OPTIONS: {
1889 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
1890 if (Listener && &F == *ModuleMgr.begin() &&
1891 ParseLanguageOptions(Record, Complain, *Listener) &&
1892 !DisableValidation)
1893 return ConfigurationMismatch;
1894 break;
1895 }
1896
1897 case TARGET_OPTIONS: {
1898 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1899 if (Listener && &F == *ModuleMgr.begin() &&
1900 ParseTargetOptions(Record, Complain, *Listener) &&
1901 !DisableValidation)
1902 return ConfigurationMismatch;
1903 break;
1904 }
1905
1906 case DIAGNOSTIC_OPTIONS: {
1907 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1908 if (Listener && &F == *ModuleMgr.begin() &&
1909 ParseDiagnosticOptions(Record, Complain, *Listener) &&
1910 !DisableValidation)
1911 return ConfigurationMismatch;
1912 break;
1913 }
1914
1915 case FILE_SYSTEM_OPTIONS: {
1916 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1917 if (Listener && &F == *ModuleMgr.begin() &&
1918 ParseFileSystemOptions(Record, Complain, *Listener) &&
1919 !DisableValidation)
1920 return ConfigurationMismatch;
1921 break;
1922 }
1923
1924 case HEADER_SEARCH_OPTIONS: {
1925 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1926 if (Listener && &F == *ModuleMgr.begin() &&
1927 ParseHeaderSearchOptions(Record, Complain, *Listener) &&
1928 !DisableValidation)
1929 return ConfigurationMismatch;
1930 break;
1931 }
1932
1933 case PREPROCESSOR_OPTIONS: {
1934 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1935 if (Listener && &F == *ModuleMgr.begin() &&
1936 ParsePreprocessorOptions(Record, Complain, *Listener,
1937 SuggestedPredefines) &&
1938 !DisableValidation)
1939 return ConfigurationMismatch;
1940 break;
1941 }
1942
1943 case ORIGINAL_FILE:
1944 F.OriginalSourceFileID = FileID::get(Record[0]);
Chris Lattner0e6c9402013-01-20 02:38:54 +00001945 F.ActualOriginalSourceFileName = Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00001946 F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
1947 MaybeAddSystemRootToFilename(F, F.OriginalSourceFileName);
1948 break;
1949
1950 case ORIGINAL_FILE_ID:
1951 F.OriginalSourceFileID = FileID::get(Record[0]);
1952 break;
1953
1954 case ORIGINAL_PCH_DIR:
Chris Lattner0e6c9402013-01-20 02:38:54 +00001955 F.OriginalDir = Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00001956 break;
1957
1958 case INPUT_FILE_OFFSETS:
Chris Lattner0e6c9402013-01-20 02:38:54 +00001959 F.InputFileOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00001960 F.InputFilesLoaded.resize(Record[0]);
1961 break;
1962 }
1963 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001964}
1965
1966bool ASTReader::ReadASTBlock(ModuleFile &F) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001967 BitstreamCursor &Stream = F.Stream;
Guy Benyei11169dd2012-12-18 14:30:41 +00001968
1969 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
1970 Error("malformed block record in AST file");
1971 return true;
1972 }
1973
1974 // Read all of the records and blocks for the AST file.
1975 RecordData Record;
Chris Lattnere7b154b2013-01-19 21:39:22 +00001976 while (1) {
1977 llvm::BitstreamEntry Entry = Stream.advance();
1978
1979 switch (Entry.Kind) {
1980 case llvm::BitstreamEntry::Error:
1981 Error("error at end of module block in AST file");
1982 return true;
1983 case llvm::BitstreamEntry::EndBlock: {
Richard Smithc0fbba72013-04-03 22:49:41 +00001984 // Outside of C++, we do not store a lookup map for the translation unit.
1985 // Instead, mark it as needing a lookup map to be built if this module
1986 // contains any declarations lexically within it (which it always does!).
1987 // This usually has no cost, since we very rarely need the lookup map for
1988 // the translation unit outside C++.
Guy Benyei11169dd2012-12-18 14:30:41 +00001989 DeclContext *DC = Context.getTranslationUnitDecl();
Richard Smithc0fbba72013-04-03 22:49:41 +00001990 if (DC->hasExternalLexicalStorage() &&
1991 !getContext().getLangOpts().CPlusPlus)
Guy Benyei11169dd2012-12-18 14:30:41 +00001992 DC->setMustBuildLookupTable();
Chris Lattnere7b154b2013-01-19 21:39:22 +00001993
Guy Benyei11169dd2012-12-18 14:30:41 +00001994 return false;
1995 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00001996 case llvm::BitstreamEntry::SubBlock:
1997 switch (Entry.ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001998 case DECLTYPES_BLOCK_ID:
1999 // We lazily load the decls block, but we want to set up the
2000 // DeclsCursor cursor to point into it. Clone our current bitcode
2001 // cursor to it, enter the block and read the abbrevs in that block.
2002 // With the main cursor, we just skip over it.
2003 F.DeclsCursor = Stream;
2004 if (Stream.SkipBlock() || // Skip with the main cursor.
2005 // Read the abbrevs.
2006 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
2007 Error("malformed block record in AST file");
2008 return true;
2009 }
2010 break;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002011
Guy Benyei11169dd2012-12-18 14:30:41 +00002012 case DECL_UPDATES_BLOCK_ID:
2013 if (Stream.SkipBlock()) {
2014 Error("malformed block record in AST file");
2015 return true;
2016 }
2017 break;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002018
Guy Benyei11169dd2012-12-18 14:30:41 +00002019 case PREPROCESSOR_BLOCK_ID:
2020 F.MacroCursor = Stream;
2021 if (!PP.getExternalSource())
2022 PP.setExternalSource(this);
Chris Lattnere7b154b2013-01-19 21:39:22 +00002023
Guy Benyei11169dd2012-12-18 14:30:41 +00002024 if (Stream.SkipBlock() ||
2025 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
2026 Error("malformed block record in AST file");
2027 return true;
2028 }
2029 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
2030 break;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002031
Guy Benyei11169dd2012-12-18 14:30:41 +00002032 case PREPROCESSOR_DETAIL_BLOCK_ID:
2033 F.PreprocessorDetailCursor = Stream;
2034 if (Stream.SkipBlock() ||
Chris Lattnere7b154b2013-01-19 21:39:22 +00002035 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
Guy Benyei11169dd2012-12-18 14:30:41 +00002036 PREPROCESSOR_DETAIL_BLOCK_ID)) {
Chris Lattnere7b154b2013-01-19 21:39:22 +00002037 Error("malformed preprocessor detail record in AST file");
2038 return true;
2039 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002040 F.PreprocessorDetailStartOffset
Chris Lattnere7b154b2013-01-19 21:39:22 +00002041 = F.PreprocessorDetailCursor.GetCurrentBitNo();
2042
Guy Benyei11169dd2012-12-18 14:30:41 +00002043 if (!PP.getPreprocessingRecord())
2044 PP.createPreprocessingRecord();
2045 if (!PP.getPreprocessingRecord()->getExternalSource())
2046 PP.getPreprocessingRecord()->SetExternalSource(*this);
2047 break;
2048
2049 case SOURCE_MANAGER_BLOCK_ID:
2050 if (ReadSourceManagerBlock(F))
2051 return true;
2052 break;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002053
Guy Benyei11169dd2012-12-18 14:30:41 +00002054 case SUBMODULE_BLOCK_ID:
2055 if (ReadSubmoduleBlock(F))
2056 return true;
2057 break;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002058
Guy Benyei11169dd2012-12-18 14:30:41 +00002059 case COMMENTS_BLOCK_ID: {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00002060 BitstreamCursor C = Stream;
Guy Benyei11169dd2012-12-18 14:30:41 +00002061 if (Stream.SkipBlock() ||
2062 ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
2063 Error("malformed comments block in AST file");
2064 return true;
2065 }
2066 CommentsCursors.push_back(std::make_pair(C, &F));
2067 break;
2068 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00002069
Guy Benyei11169dd2012-12-18 14:30:41 +00002070 default:
Chris Lattnere7b154b2013-01-19 21:39:22 +00002071 if (Stream.SkipBlock()) {
2072 Error("malformed block record in AST file");
2073 return true;
2074 }
2075 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002076 }
2077 continue;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002078
2079 case llvm::BitstreamEntry::Record:
2080 // The interesting case.
2081 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002082 }
2083
2084 // Read and process a record.
2085 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00002086 StringRef Blob;
2087 switch ((ASTRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002088 default: // Default behavior: ignore.
2089 break;
2090
2091 case TYPE_OFFSET: {
2092 if (F.LocalNumTypes != 0) {
2093 Error("duplicate TYPE_OFFSET record in AST file");
2094 return true;
2095 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00002096 F.TypeOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002097 F.LocalNumTypes = Record[0];
2098 unsigned LocalBaseTypeIndex = Record[1];
2099 F.BaseTypeIndex = getTotalNumTypes();
2100
2101 if (F.LocalNumTypes > 0) {
2102 // Introduce the global -> local mapping for types within this module.
2103 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
2104
2105 // Introduce the local -> global mapping for types within this module.
2106 F.TypeRemap.insertOrReplace(
2107 std::make_pair(LocalBaseTypeIndex,
2108 F.BaseTypeIndex - LocalBaseTypeIndex));
2109
2110 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
2111 }
2112 break;
2113 }
2114
2115 case DECL_OFFSET: {
2116 if (F.LocalNumDecls != 0) {
2117 Error("duplicate DECL_OFFSET record in AST file");
2118 return true;
2119 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00002120 F.DeclOffsets = (const DeclOffset *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002121 F.LocalNumDecls = Record[0];
2122 unsigned LocalBaseDeclID = Record[1];
2123 F.BaseDeclID = getTotalNumDecls();
2124
2125 if (F.LocalNumDecls > 0) {
2126 // Introduce the global -> local mapping for declarations within this
2127 // module.
2128 GlobalDeclMap.insert(
2129 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
2130
2131 // Introduce the local -> global mapping for declarations within this
2132 // module.
2133 F.DeclRemap.insertOrReplace(
2134 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
2135
2136 // Introduce the global -> local mapping for declarations within this
2137 // module.
2138 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
2139
2140 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
2141 }
2142 break;
2143 }
2144
2145 case TU_UPDATE_LEXICAL: {
2146 DeclContext *TU = Context.getTranslationUnitDecl();
2147 DeclContextInfo &Info = F.DeclContextInfos[TU];
Chris Lattner0e6c9402013-01-20 02:38:54 +00002148 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(Blob.data());
Guy Benyei11169dd2012-12-18 14:30:41 +00002149 Info.NumLexicalDecls
Chris Lattner0e6c9402013-01-20 02:38:54 +00002150 = static_cast<unsigned int>(Blob.size() / sizeof(KindDeclIDPair));
Guy Benyei11169dd2012-12-18 14:30:41 +00002151 TU->setHasExternalLexicalStorage(true);
2152 break;
2153 }
2154
2155 case UPDATE_VISIBLE: {
2156 unsigned Idx = 0;
2157 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
2158 ASTDeclContextNameLookupTable *Table =
2159 ASTDeclContextNameLookupTable::Create(
Chris Lattner0e6c9402013-01-20 02:38:54 +00002160 (const unsigned char *)Blob.data() + Record[Idx++],
2161 (const unsigned char *)Blob.data(),
Guy Benyei11169dd2012-12-18 14:30:41 +00002162 ASTDeclContextNameLookupTrait(*this, F));
2163 if (ID == PREDEF_DECL_TRANSLATION_UNIT_ID) { // Is it the TU?
2164 DeclContext *TU = Context.getTranslationUnitDecl();
2165 F.DeclContextInfos[TU].NameLookupTableData = Table;
2166 TU->setHasExternalVisibleStorage(true);
2167 } else
2168 PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F));
2169 break;
2170 }
2171
2172 case IDENTIFIER_TABLE:
Chris Lattner0e6c9402013-01-20 02:38:54 +00002173 F.IdentifierTableData = Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002174 if (Record[0]) {
2175 F.IdentifierLookupTable
2176 = ASTIdentifierLookupTable::Create(
2177 (const unsigned char *)F.IdentifierTableData + Record[0],
2178 (const unsigned char *)F.IdentifierTableData,
2179 ASTIdentifierLookupTrait(*this, F));
2180
2181 PP.getIdentifierTable().setExternalIdentifierLookup(this);
2182 }
2183 break;
2184
2185 case IDENTIFIER_OFFSET: {
2186 if (F.LocalNumIdentifiers != 0) {
2187 Error("duplicate IDENTIFIER_OFFSET record in AST file");
2188 return true;
2189 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00002190 F.IdentifierOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002191 F.LocalNumIdentifiers = Record[0];
2192 unsigned LocalBaseIdentifierID = Record[1];
2193 F.BaseIdentifierID = getTotalNumIdentifiers();
2194
2195 if (F.LocalNumIdentifiers > 0) {
2196 // Introduce the global -> local mapping for identifiers within this
2197 // module.
2198 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
2199 &F));
2200
2201 // Introduce the local -> global mapping for identifiers within this
2202 // module.
2203 F.IdentifierRemap.insertOrReplace(
2204 std::make_pair(LocalBaseIdentifierID,
2205 F.BaseIdentifierID - LocalBaseIdentifierID));
2206
2207 IdentifiersLoaded.resize(IdentifiersLoaded.size()
2208 + F.LocalNumIdentifiers);
2209 }
2210 break;
2211 }
2212
2213 case EXTERNAL_DEFINITIONS:
2214 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2215 ExternalDefinitions.push_back(getGlobalDeclID(F, Record[I]));
2216 break;
2217
2218 case SPECIAL_TYPES:
Douglas Gregor44180f82013-02-01 23:45:03 +00002219 if (SpecialTypes.empty()) {
2220 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2221 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
2222 break;
2223 }
2224
2225 if (SpecialTypes.size() != Record.size()) {
2226 Error("invalid special-types record");
2227 return true;
2228 }
2229
2230 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2231 serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
2232 if (!SpecialTypes[I])
2233 SpecialTypes[I] = ID;
2234 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
2235 // merge step?
2236 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002237 break;
2238
2239 case STATISTICS:
2240 TotalNumStatements += Record[0];
2241 TotalNumMacros += Record[1];
2242 TotalLexicalDeclContexts += Record[2];
2243 TotalVisibleDeclContexts += Record[3];
2244 break;
2245
2246 case UNUSED_FILESCOPED_DECLS:
2247 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2248 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
2249 break;
2250
2251 case DELEGATING_CTORS:
2252 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2253 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
2254 break;
2255
2256 case WEAK_UNDECLARED_IDENTIFIERS:
2257 if (Record.size() % 4 != 0) {
2258 Error("invalid weak identifiers record");
2259 return true;
2260 }
2261
2262 // FIXME: Ignore weak undeclared identifiers from non-original PCH
2263 // files. This isn't the way to do it :)
2264 WeakUndeclaredIdentifiers.clear();
2265
2266 // Translate the weak, undeclared identifiers into global IDs.
2267 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
2268 WeakUndeclaredIdentifiers.push_back(
2269 getGlobalIdentifierID(F, Record[I++]));
2270 WeakUndeclaredIdentifiers.push_back(
2271 getGlobalIdentifierID(F, Record[I++]));
2272 WeakUndeclaredIdentifiers.push_back(
2273 ReadSourceLocation(F, Record, I).getRawEncoding());
2274 WeakUndeclaredIdentifiers.push_back(Record[I++]);
2275 }
2276 break;
2277
Richard Smith78165b52013-01-10 23:43:47 +00002278 case LOCALLY_SCOPED_EXTERN_C_DECLS:
Guy Benyei11169dd2012-12-18 14:30:41 +00002279 for (unsigned I = 0, N = Record.size(); I != N; ++I)
Richard Smith78165b52013-01-10 23:43:47 +00002280 LocallyScopedExternCDecls.push_back(getGlobalDeclID(F, Record[I]));
Guy Benyei11169dd2012-12-18 14:30:41 +00002281 break;
2282
2283 case SELECTOR_OFFSETS: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00002284 F.SelectorOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002285 F.LocalNumSelectors = Record[0];
2286 unsigned LocalBaseSelectorID = Record[1];
2287 F.BaseSelectorID = getTotalNumSelectors();
2288
2289 if (F.LocalNumSelectors > 0) {
2290 // Introduce the global -> local mapping for selectors within this
2291 // module.
2292 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
2293
2294 // Introduce the local -> global mapping for selectors within this
2295 // module.
2296 F.SelectorRemap.insertOrReplace(
2297 std::make_pair(LocalBaseSelectorID,
2298 F.BaseSelectorID - LocalBaseSelectorID));
2299
2300 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
2301 }
2302 break;
2303 }
2304
2305 case METHOD_POOL:
Chris Lattner0e6c9402013-01-20 02:38:54 +00002306 F.SelectorLookupTableData = (const unsigned char *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002307 if (Record[0])
2308 F.SelectorLookupTable
2309 = ASTSelectorLookupTable::Create(
2310 F.SelectorLookupTableData + Record[0],
2311 F.SelectorLookupTableData,
2312 ASTSelectorLookupTrait(*this, F));
2313 TotalNumMethodPoolEntries += Record[1];
2314 break;
2315
2316 case REFERENCED_SELECTOR_POOL:
2317 if (!Record.empty()) {
2318 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
2319 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
2320 Record[Idx++]));
2321 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
2322 getRawEncoding());
2323 }
2324 }
2325 break;
2326
2327 case PP_COUNTER_VALUE:
2328 if (!Record.empty() && Listener)
2329 Listener->ReadCounter(F, Record[0]);
2330 break;
2331
2332 case FILE_SORTED_DECLS:
Chris Lattner0e6c9402013-01-20 02:38:54 +00002333 F.FileSortedDecls = (const DeclID *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002334 F.NumFileSortedDecls = Record[0];
2335 break;
2336
2337 case SOURCE_LOCATION_OFFSETS: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00002338 F.SLocEntryOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002339 F.LocalNumSLocEntries = Record[0];
2340 unsigned SLocSpaceSize = Record[1];
2341 llvm::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
2342 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
2343 SLocSpaceSize);
2344 // Make our entry in the range map. BaseID is negative and growing, so
2345 // we invert it. Because we invert it, though, we need the other end of
2346 // the range.
2347 unsigned RangeStart =
2348 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
2349 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
2350 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
2351
2352 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
2353 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
2354 GlobalSLocOffsetMap.insert(
2355 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
2356 - SLocSpaceSize,&F));
2357
2358 // Initialize the remapping table.
2359 // Invalid stays invalid.
2360 F.SLocRemap.insert(std::make_pair(0U, 0));
2361 // This module. Base was 2 when being compiled.
2362 F.SLocRemap.insert(std::make_pair(2U,
2363 static_cast<int>(F.SLocEntryBaseOffset - 2)));
2364
2365 TotalNumSLocEntries += F.LocalNumSLocEntries;
2366 break;
2367 }
2368
2369 case MODULE_OFFSET_MAP: {
2370 // Additional remapping information.
Chris Lattner0e6c9402013-01-20 02:38:54 +00002371 const unsigned char *Data = (const unsigned char*)Blob.data();
2372 const unsigned char *DataEnd = Data + Blob.size();
Guy Benyei11169dd2012-12-18 14:30:41 +00002373
2374 // Continuous range maps we may be updating in our module.
2375 ContinuousRangeMap<uint32_t, int, 2>::Builder SLocRemap(F.SLocRemap);
2376 ContinuousRangeMap<uint32_t, int, 2>::Builder
2377 IdentifierRemap(F.IdentifierRemap);
2378 ContinuousRangeMap<uint32_t, int, 2>::Builder
2379 MacroRemap(F.MacroRemap);
2380 ContinuousRangeMap<uint32_t, int, 2>::Builder
2381 PreprocessedEntityRemap(F.PreprocessedEntityRemap);
2382 ContinuousRangeMap<uint32_t, int, 2>::Builder
2383 SubmoduleRemap(F.SubmoduleRemap);
2384 ContinuousRangeMap<uint32_t, int, 2>::Builder
2385 SelectorRemap(F.SelectorRemap);
2386 ContinuousRangeMap<uint32_t, int, 2>::Builder DeclRemap(F.DeclRemap);
2387 ContinuousRangeMap<uint32_t, int, 2>::Builder TypeRemap(F.TypeRemap);
2388
2389 while(Data < DataEnd) {
2390 uint16_t Len = io::ReadUnalignedLE16(Data);
2391 StringRef Name = StringRef((const char*)Data, Len);
2392 Data += Len;
2393 ModuleFile *OM = ModuleMgr.lookup(Name);
2394 if (!OM) {
2395 Error("SourceLocation remap refers to unknown module");
2396 return true;
2397 }
2398
2399 uint32_t SLocOffset = io::ReadUnalignedLE32(Data);
2400 uint32_t IdentifierIDOffset = io::ReadUnalignedLE32(Data);
2401 uint32_t MacroIDOffset = io::ReadUnalignedLE32(Data);
2402 uint32_t PreprocessedEntityIDOffset = io::ReadUnalignedLE32(Data);
2403 uint32_t SubmoduleIDOffset = io::ReadUnalignedLE32(Data);
2404 uint32_t SelectorIDOffset = io::ReadUnalignedLE32(Data);
2405 uint32_t DeclIDOffset = io::ReadUnalignedLE32(Data);
2406 uint32_t TypeIndexOffset = io::ReadUnalignedLE32(Data);
2407
2408 // Source location offset is mapped to OM->SLocEntryBaseOffset.
2409 SLocRemap.insert(std::make_pair(SLocOffset,
2410 static_cast<int>(OM->SLocEntryBaseOffset - SLocOffset)));
2411 IdentifierRemap.insert(
2412 std::make_pair(IdentifierIDOffset,
2413 OM->BaseIdentifierID - IdentifierIDOffset));
2414 MacroRemap.insert(std::make_pair(MacroIDOffset,
2415 OM->BaseMacroID - MacroIDOffset));
2416 PreprocessedEntityRemap.insert(
2417 std::make_pair(PreprocessedEntityIDOffset,
2418 OM->BasePreprocessedEntityID - PreprocessedEntityIDOffset));
2419 SubmoduleRemap.insert(std::make_pair(SubmoduleIDOffset,
2420 OM->BaseSubmoduleID - SubmoduleIDOffset));
2421 SelectorRemap.insert(std::make_pair(SelectorIDOffset,
2422 OM->BaseSelectorID - SelectorIDOffset));
2423 DeclRemap.insert(std::make_pair(DeclIDOffset,
2424 OM->BaseDeclID - DeclIDOffset));
2425
2426 TypeRemap.insert(std::make_pair(TypeIndexOffset,
2427 OM->BaseTypeIndex - TypeIndexOffset));
2428
2429 // Global -> local mappings.
2430 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
2431 }
2432 break;
2433 }
2434
2435 case SOURCE_MANAGER_LINE_TABLE:
2436 if (ParseLineTable(F, Record))
2437 return true;
2438 break;
2439
2440 case SOURCE_LOCATION_PRELOADS: {
2441 // Need to transform from the local view (1-based IDs) to the global view,
2442 // which is based off F.SLocEntryBaseID.
2443 if (!F.PreloadSLocEntries.empty()) {
2444 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
2445 return true;
2446 }
2447
2448 F.PreloadSLocEntries.swap(Record);
2449 break;
2450 }
2451
2452 case EXT_VECTOR_DECLS:
2453 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2454 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
2455 break;
2456
2457 case VTABLE_USES:
2458 if (Record.size() % 3 != 0) {
2459 Error("Invalid VTABLE_USES record");
2460 return true;
2461 }
2462
2463 // Later tables overwrite earlier ones.
2464 // FIXME: Modules will have some trouble with this. This is clearly not
2465 // the right way to do this.
2466 VTableUses.clear();
2467
2468 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
2469 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
2470 VTableUses.push_back(
2471 ReadSourceLocation(F, Record, Idx).getRawEncoding());
2472 VTableUses.push_back(Record[Idx++]);
2473 }
2474 break;
2475
2476 case DYNAMIC_CLASSES:
2477 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2478 DynamicClasses.push_back(getGlobalDeclID(F, Record[I]));
2479 break;
2480
2481 case PENDING_IMPLICIT_INSTANTIATIONS:
2482 if (PendingInstantiations.size() % 2 != 0) {
2483 Error("Invalid existing PendingInstantiations");
2484 return true;
2485 }
2486
2487 if (Record.size() % 2 != 0) {
2488 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
2489 return true;
2490 }
2491
2492 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2493 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
2494 PendingInstantiations.push_back(
2495 ReadSourceLocation(F, Record, I).getRawEncoding());
2496 }
2497 break;
2498
2499 case SEMA_DECL_REFS:
2500 // Later tables overwrite earlier ones.
2501 // FIXME: Modules will have some trouble with this.
2502 SemaDeclRefs.clear();
2503 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2504 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
2505 break;
2506
2507 case PPD_ENTITIES_OFFSETS: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00002508 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
2509 assert(Blob.size() % sizeof(PPEntityOffset) == 0);
2510 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
Guy Benyei11169dd2012-12-18 14:30:41 +00002511
2512 unsigned LocalBasePreprocessedEntityID = Record[0];
2513
2514 unsigned StartingID;
2515 if (!PP.getPreprocessingRecord())
2516 PP.createPreprocessingRecord();
2517 if (!PP.getPreprocessingRecord()->getExternalSource())
2518 PP.getPreprocessingRecord()->SetExternalSource(*this);
2519 StartingID
2520 = PP.getPreprocessingRecord()
2521 ->allocateLoadedEntities(F.NumPreprocessedEntities);
2522 F.BasePreprocessedEntityID = StartingID;
2523
2524 if (F.NumPreprocessedEntities > 0) {
2525 // Introduce the global -> local mapping for preprocessed entities in
2526 // this module.
2527 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
2528
2529 // Introduce the local -> global mapping for preprocessed entities in
2530 // this module.
2531 F.PreprocessedEntityRemap.insertOrReplace(
2532 std::make_pair(LocalBasePreprocessedEntityID,
2533 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
2534 }
2535
2536 break;
2537 }
2538
2539 case DECL_UPDATE_OFFSETS: {
2540 if (Record.size() % 2 != 0) {
2541 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2542 return true;
2543 }
2544 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
2545 DeclUpdateOffsets[getGlobalDeclID(F, Record[I])]
2546 .push_back(std::make_pair(&F, Record[I+1]));
2547 break;
2548 }
2549
2550 case DECL_REPLACEMENTS: {
2551 if (Record.size() % 3 != 0) {
2552 Error("invalid DECL_REPLACEMENTS block in AST file");
2553 return true;
2554 }
2555 for (unsigned I = 0, N = Record.size(); I != N; I += 3)
2556 ReplacedDecls[getGlobalDeclID(F, Record[I])]
2557 = ReplacedDeclInfo(&F, Record[I+1], Record[I+2]);
2558 break;
2559 }
2560
2561 case OBJC_CATEGORIES_MAP: {
2562 if (F.LocalNumObjCCategoriesInMap != 0) {
2563 Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
2564 return true;
2565 }
2566
2567 F.LocalNumObjCCategoriesInMap = Record[0];
Chris Lattner0e6c9402013-01-20 02:38:54 +00002568 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002569 break;
2570 }
2571
2572 case OBJC_CATEGORIES:
2573 F.ObjCCategories.swap(Record);
2574 break;
2575
2576 case CXX_BASE_SPECIFIER_OFFSETS: {
2577 if (F.LocalNumCXXBaseSpecifiers != 0) {
2578 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2579 return true;
2580 }
2581
2582 F.LocalNumCXXBaseSpecifiers = Record[0];
Chris Lattner0e6c9402013-01-20 02:38:54 +00002583 F.CXXBaseSpecifiersOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002584 NumCXXBaseSpecifiersLoaded += F.LocalNumCXXBaseSpecifiers;
2585 break;
2586 }
2587
2588 case DIAG_PRAGMA_MAPPINGS:
2589 if (F.PragmaDiagMappings.empty())
2590 F.PragmaDiagMappings.swap(Record);
2591 else
2592 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
2593 Record.begin(), Record.end());
2594 break;
2595
2596 case CUDA_SPECIAL_DECL_REFS:
2597 // Later tables overwrite earlier ones.
2598 // FIXME: Modules will have trouble with this.
2599 CUDASpecialDeclRefs.clear();
2600 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2601 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
2602 break;
2603
2604 case HEADER_SEARCH_TABLE: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00002605 F.HeaderFileInfoTableData = Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002606 F.LocalNumHeaderFileInfos = Record[1];
Guy Benyei11169dd2012-12-18 14:30:41 +00002607 if (Record[0]) {
2608 F.HeaderFileInfoTable
2609 = HeaderFileInfoLookupTable::Create(
2610 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
2611 (const unsigned char *)F.HeaderFileInfoTableData,
2612 HeaderFileInfoTrait(*this, F,
2613 &PP.getHeaderSearchInfo(),
Chris Lattner0e6c9402013-01-20 02:38:54 +00002614 Blob.data() + Record[2]));
Guy Benyei11169dd2012-12-18 14:30:41 +00002615
2616 PP.getHeaderSearchInfo().SetExternalSource(this);
2617 if (!PP.getHeaderSearchInfo().getExternalLookup())
2618 PP.getHeaderSearchInfo().SetExternalLookup(this);
2619 }
2620 break;
2621 }
2622
2623 case FP_PRAGMA_OPTIONS:
2624 // Later tables overwrite earlier ones.
2625 FPPragmaOptions.swap(Record);
2626 break;
2627
2628 case OPENCL_EXTENSIONS:
2629 // Later tables overwrite earlier ones.
2630 OpenCLExtensions.swap(Record);
2631 break;
2632
2633 case TENTATIVE_DEFINITIONS:
2634 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2635 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
2636 break;
2637
2638 case KNOWN_NAMESPACES:
2639 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2640 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
2641 break;
Nick Lewycky8334af82013-01-26 00:35:08 +00002642
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00002643 case UNDEFINED_BUT_USED:
2644 if (UndefinedButUsed.size() % 2 != 0) {
2645 Error("Invalid existing UndefinedButUsed");
Nick Lewycky8334af82013-01-26 00:35:08 +00002646 return true;
2647 }
2648
2649 if (Record.size() % 2 != 0) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00002650 Error("invalid undefined-but-used record");
Nick Lewycky8334af82013-01-26 00:35:08 +00002651 return true;
2652 }
2653 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00002654 UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
2655 UndefinedButUsed.push_back(
Nick Lewycky8334af82013-01-26 00:35:08 +00002656 ReadSourceLocation(F, Record, I).getRawEncoding());
2657 }
2658 break;
2659
Guy Benyei11169dd2012-12-18 14:30:41 +00002660 case IMPORTED_MODULES: {
2661 if (F.Kind != MK_Module) {
2662 // If we aren't loading a module (which has its own exports), make
2663 // all of the imported modules visible.
2664 // FIXME: Deal with macros-only imports.
2665 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2666 if (unsigned GlobalID = getGlobalSubmoduleID(F, Record[I]))
2667 ImportedModules.push_back(GlobalID);
2668 }
2669 }
2670 break;
2671 }
2672
2673 case LOCAL_REDECLARATIONS: {
2674 F.RedeclarationChains.swap(Record);
2675 break;
2676 }
2677
2678 case LOCAL_REDECLARATIONS_MAP: {
2679 if (F.LocalNumRedeclarationsInMap != 0) {
2680 Error("duplicate LOCAL_REDECLARATIONS_MAP record in AST file");
2681 return true;
2682 }
2683
2684 F.LocalNumRedeclarationsInMap = Record[0];
Chris Lattner0e6c9402013-01-20 02:38:54 +00002685 F.RedeclarationsMap = (const LocalRedeclarationsInfo *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002686 break;
2687 }
2688
2689 case MERGED_DECLARATIONS: {
2690 for (unsigned Idx = 0; Idx < Record.size(); /* increment in loop */) {
2691 GlobalDeclID CanonID = getGlobalDeclID(F, Record[Idx++]);
2692 SmallVectorImpl<GlobalDeclID> &Decls = StoredMergedDecls[CanonID];
2693 for (unsigned N = Record[Idx++]; N > 0; --N)
2694 Decls.push_back(getGlobalDeclID(F, Record[Idx++]));
2695 }
2696 break;
2697 }
2698
2699 case MACRO_OFFSET: {
2700 if (F.LocalNumMacros != 0) {
2701 Error("duplicate MACRO_OFFSET record in AST file");
2702 return true;
2703 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00002704 F.MacroOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002705 F.LocalNumMacros = Record[0];
2706 unsigned LocalBaseMacroID = Record[1];
2707 F.BaseMacroID = getTotalNumMacros();
2708
2709 if (F.LocalNumMacros > 0) {
2710 // Introduce the global -> local mapping for macros within this module.
2711 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
2712
2713 // Introduce the local -> global mapping for macros within this module.
2714 F.MacroRemap.insertOrReplace(
2715 std::make_pair(LocalBaseMacroID,
2716 F.BaseMacroID - LocalBaseMacroID));
2717
2718 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
2719 }
2720 break;
2721 }
2722
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002723 case MACRO_TABLE: {
2724 // FIXME: Not used yet.
Guy Benyei11169dd2012-12-18 14:30:41 +00002725 break;
2726 }
2727 }
2728 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002729}
2730
Douglas Gregorc1489562013-02-12 23:36:21 +00002731/// \brief Move the given method to the back of the global list of methods.
2732static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
2733 // Find the entry for this selector in the method pool.
2734 Sema::GlobalMethodPool::iterator Known
2735 = S.MethodPool.find(Method->getSelector());
2736 if (Known == S.MethodPool.end())
2737 return;
2738
2739 // Retrieve the appropriate method list.
2740 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
2741 : Known->second.second;
2742 bool Found = false;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002743 for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
Douglas Gregorc1489562013-02-12 23:36:21 +00002744 if (!Found) {
2745 if (List->Method == Method) {
2746 Found = true;
2747 } else {
2748 // Keep searching.
2749 continue;
2750 }
2751 }
2752
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002753 if (List->getNext())
2754 List->Method = List->getNext()->Method;
Douglas Gregorc1489562013-02-12 23:36:21 +00002755 else
2756 List->Method = Method;
2757 }
2758}
2759
Argyrios Kyrtzidis3a9c42c2013-03-27 01:25:34 +00002760void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002761 for (unsigned I = 0, N = Names.size(); I != N; ++I) {
2762 switch (Names[I].getKind()) {
Douglas Gregorc1489562013-02-12 23:36:21 +00002763 case HiddenName::Declaration: {
2764 Decl *D = Names[I].getDecl();
2765 bool wasHidden = D->Hidden;
2766 D->Hidden = false;
Guy Benyei11169dd2012-12-18 14:30:41 +00002767
Douglas Gregorc1489562013-02-12 23:36:21 +00002768 if (wasHidden && SemaObj) {
2769 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
2770 moveMethodToBackOfGlobalList(*SemaObj, Method);
2771 }
2772 }
2773 break;
2774 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002775 case HiddenName::MacroVisibility: {
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00002776 std::pair<IdentifierInfo *, MacroDirective *> Macro = Names[I].getMacro();
Argyrios Kyrtzidis3a9c42c2013-03-27 01:25:34 +00002777 installImportedMacro(Macro.first, Macro.second, Owner);
Guy Benyei11169dd2012-12-18 14:30:41 +00002778 break;
2779 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002780 }
2781 }
2782}
2783
2784void ASTReader::makeModuleVisible(Module *Mod,
Argyrios Kyrtzidis125df052013-02-01 16:36:12 +00002785 Module::NameVisibilityKind NameVisibility,
Douglas Gregorfb912652013-03-20 21:10:35 +00002786 SourceLocation ImportLoc,
2787 bool Complain) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002788 llvm::SmallPtrSet<Module *, 4> Visited;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002789 SmallVector<Module *, 4> Stack;
Guy Benyei11169dd2012-12-18 14:30:41 +00002790 Stack.push_back(Mod);
2791 while (!Stack.empty()) {
2792 Mod = Stack.back();
2793 Stack.pop_back();
2794
2795 if (NameVisibility <= Mod->NameVisibility) {
2796 // This module already has this level of visibility (or greater), so
2797 // there is nothing more to do.
2798 continue;
2799 }
2800
2801 if (!Mod->isAvailable()) {
2802 // Modules that aren't available cannot be made visible.
2803 continue;
2804 }
2805
2806 // Update the module's name visibility.
2807 Mod->NameVisibility = NameVisibility;
2808
2809 // If we've already deserialized any names from this module,
2810 // mark them as visible.
2811 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
2812 if (Hidden != HiddenNamesMap.end()) {
Argyrios Kyrtzidis3a9c42c2013-03-27 01:25:34 +00002813 makeNamesVisible(Hidden->second, Hidden->first);
Guy Benyei11169dd2012-12-18 14:30:41 +00002814 HiddenNamesMap.erase(Hidden);
2815 }
2816
2817 // Push any non-explicit submodules onto the stack to be marked as
2818 // visible.
2819 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2820 SubEnd = Mod->submodule_end();
2821 Sub != SubEnd; ++Sub) {
2822 if (!(*Sub)->IsExplicit && Visited.insert(*Sub))
2823 Stack.push_back(*Sub);
2824 }
2825
2826 // Push any exported modules onto the stack to be marked as visible.
Argyrios Kyrtzidis8739f7b2013-02-19 19:34:40 +00002827 SmallVector<Module *, 16> Exports;
2828 Mod->getExportedModules(Exports);
2829 for (SmallVectorImpl<Module *>::iterator
2830 I = Exports.begin(), E = Exports.end(); I != E; ++I) {
2831 Module *Exported = *I;
2832 if (Visited.insert(Exported))
2833 Stack.push_back(Exported);
Guy Benyei11169dd2012-12-18 14:30:41 +00002834 }
Douglas Gregorfb912652013-03-20 21:10:35 +00002835
2836 // Detect any conflicts.
2837 if (Complain) {
2838 assert(ImportLoc.isValid() && "Missing import location");
2839 for (unsigned I = 0, N = Mod->Conflicts.size(); I != N; ++I) {
2840 if (Mod->Conflicts[I].Other->NameVisibility >= NameVisibility) {
2841 Diag(ImportLoc, diag::warn_module_conflict)
2842 << Mod->getFullModuleName()
2843 << Mod->Conflicts[I].Other->getFullModuleName()
2844 << Mod->Conflicts[I].Message;
2845 // FIXME: Need note where the other module was imported.
2846 }
2847 }
2848 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002849 }
2850}
2851
Douglas Gregore060e572013-01-25 01:03:03 +00002852bool ASTReader::loadGlobalIndex() {
2853 if (GlobalIndex)
2854 return false;
2855
2856 if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
2857 !Context.getLangOpts().Modules)
2858 return true;
2859
2860 // Try to load the global index.
2861 TriedLoadingGlobalIndex = true;
2862 StringRef ModuleCachePath
2863 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
2864 std::pair<GlobalModuleIndex *, GlobalModuleIndex::ErrorCode> Result
Douglas Gregor7029ce12013-03-19 00:28:20 +00002865 = GlobalModuleIndex::readIndex(ModuleCachePath);
Douglas Gregore060e572013-01-25 01:03:03 +00002866 if (!Result.first)
2867 return true;
2868
2869 GlobalIndex.reset(Result.first);
Douglas Gregor7211ac12013-01-25 23:32:03 +00002870 ModuleMgr.setGlobalIndex(GlobalIndex.get());
Douglas Gregore060e572013-01-25 01:03:03 +00002871 return false;
2872}
2873
2874bool ASTReader::isGlobalIndexUnavailable() const {
2875 return Context.getLangOpts().Modules && UseGlobalIndex &&
2876 !hasGlobalIndex() && TriedLoadingGlobalIndex;
2877}
2878
Guy Benyei11169dd2012-12-18 14:30:41 +00002879ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
2880 ModuleKind Type,
2881 SourceLocation ImportLoc,
2882 unsigned ClientLoadCapabilities) {
2883 // Bump the generation number.
2884 unsigned PreviousGeneration = CurrentGeneration++;
2885
2886 unsigned NumModules = ModuleMgr.size();
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002887 SmallVector<ImportedModule, 4> Loaded;
Guy Benyei11169dd2012-12-18 14:30:41 +00002888 switch(ASTReadResult ReadResult = ReadASTCore(FileName, Type, ImportLoc,
2889 /*ImportedBy=*/0, Loaded,
Douglas Gregor7029ce12013-03-19 00:28:20 +00002890 0, 0,
Guy Benyei11169dd2012-12-18 14:30:41 +00002891 ClientLoadCapabilities)) {
2892 case Failure:
Douglas Gregor7029ce12013-03-19 00:28:20 +00002893 case Missing:
Guy Benyei11169dd2012-12-18 14:30:41 +00002894 case OutOfDate:
2895 case VersionMismatch:
2896 case ConfigurationMismatch:
2897 case HadErrors:
Douglas Gregor7029ce12013-03-19 00:28:20 +00002898 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, ModuleMgr.end(),
2899 Context.getLangOpts().Modules
2900 ? &PP.getHeaderSearchInfo().getModuleMap()
2901 : 0);
Douglas Gregore060e572013-01-25 01:03:03 +00002902
2903 // If we find that any modules are unusable, the global index is going
2904 // to be out-of-date. Just remove it.
2905 GlobalIndex.reset();
Douglas Gregor7211ac12013-01-25 23:32:03 +00002906 ModuleMgr.setGlobalIndex(0);
Guy Benyei11169dd2012-12-18 14:30:41 +00002907 return ReadResult;
2908
2909 case Success:
2910 break;
2911 }
2912
2913 // Here comes stuff that we only do once the entire chain is loaded.
2914
2915 // Load the AST blocks of all of the modules that we loaded.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002916 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
2917 MEnd = Loaded.end();
Guy Benyei11169dd2012-12-18 14:30:41 +00002918 M != MEnd; ++M) {
2919 ModuleFile &F = *M->Mod;
2920
2921 // Read the AST block.
2922 if (ReadASTBlock(F))
2923 return Failure;
2924
2925 // Once read, set the ModuleFile bit base offset and update the size in
2926 // bits of all files we've seen.
2927 F.GlobalBitOffset = TotalModulesSizeInBits;
2928 TotalModulesSizeInBits += F.SizeInBits;
2929 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
2930
2931 // Preload SLocEntries.
2932 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
2933 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
2934 // Load it through the SourceManager and don't call ReadSLocEntry()
2935 // directly because the entry may have already been loaded in which case
2936 // calling ReadSLocEntry() directly would trigger an assertion in
2937 // SourceManager.
2938 SourceMgr.getLoadedSLocEntryByID(Index);
2939 }
2940 }
2941
Douglas Gregor603cd862013-03-22 18:50:14 +00002942 // Setup the import locations and notify the module manager that we've
2943 // committed to these module files.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002944 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
2945 MEnd = Loaded.end();
Guy Benyei11169dd2012-12-18 14:30:41 +00002946 M != MEnd; ++M) {
2947 ModuleFile &F = *M->Mod;
Douglas Gregor603cd862013-03-22 18:50:14 +00002948
2949 ModuleMgr.moduleFileAccepted(&F);
2950
2951 // Set the import location.
Argyrios Kyrtzidis71c1af82013-02-01 16:36:14 +00002952 F.DirectImportLoc = ImportLoc;
Guy Benyei11169dd2012-12-18 14:30:41 +00002953 if (!M->ImportedBy)
2954 F.ImportLoc = M->ImportLoc;
2955 else
2956 F.ImportLoc = ReadSourceLocation(*M->ImportedBy,
2957 M->ImportLoc.getRawEncoding());
2958 }
2959
2960 // Mark all of the identifiers in the identifier table as being out of date,
2961 // so that various accessors know to check the loaded modules when the
2962 // identifier is used.
2963 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
2964 IdEnd = PP.getIdentifierTable().end();
2965 Id != IdEnd; ++Id)
2966 Id->second->setOutOfDate(true);
2967
2968 // Resolve any unresolved module exports.
Douglas Gregorfb912652013-03-20 21:10:35 +00002969 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
2970 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
Guy Benyei11169dd2012-12-18 14:30:41 +00002971 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
2972 Module *ResolvedMod = getSubmodule(GlobalID);
Douglas Gregorfb912652013-03-20 21:10:35 +00002973
2974 switch (Unresolved.Kind) {
2975 case UnresolvedModuleRef::Conflict:
2976 if (ResolvedMod) {
2977 Module::Conflict Conflict;
2978 Conflict.Other = ResolvedMod;
2979 Conflict.Message = Unresolved.String.str();
2980 Unresolved.Mod->Conflicts.push_back(Conflict);
2981 }
2982 continue;
2983
2984 case UnresolvedModuleRef::Import:
Guy Benyei11169dd2012-12-18 14:30:41 +00002985 if (ResolvedMod)
2986 Unresolved.Mod->Imports.push_back(ResolvedMod);
2987 continue;
Guy Benyei11169dd2012-12-18 14:30:41 +00002988
Douglas Gregorfb912652013-03-20 21:10:35 +00002989 case UnresolvedModuleRef::Export:
2990 if (ResolvedMod || Unresolved.IsWildcard)
2991 Unresolved.Mod->Exports.push_back(
2992 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
2993 continue;
2994 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002995 }
Douglas Gregorfb912652013-03-20 21:10:35 +00002996 UnresolvedModuleRefs.clear();
Guy Benyei11169dd2012-12-18 14:30:41 +00002997
2998 InitializeContext();
2999
3000 if (DeserializationListener)
3001 DeserializationListener->ReaderInitialized(this);
3002
3003 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
3004 if (!PrimaryModule.OriginalSourceFileID.isInvalid()) {
3005 PrimaryModule.OriginalSourceFileID
3006 = FileID::get(PrimaryModule.SLocEntryBaseID
3007 + PrimaryModule.OriginalSourceFileID.getOpaqueValue() - 1);
3008
3009 // If this AST file is a precompiled preamble, then set the
3010 // preamble file ID of the source manager to the file source file
3011 // from which the preamble was built.
3012 if (Type == MK_Preamble) {
3013 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
3014 } else if (Type == MK_MainFile) {
3015 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
3016 }
3017 }
3018
3019 // For any Objective-C class definitions we have already loaded, make sure
3020 // that we load any additional categories.
3021 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
3022 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
3023 ObjCClassesLoaded[I],
3024 PreviousGeneration);
3025 }
Douglas Gregore060e572013-01-25 01:03:03 +00003026
Guy Benyei11169dd2012-12-18 14:30:41 +00003027 return Success;
3028}
3029
3030ASTReader::ASTReadResult
3031ASTReader::ReadASTCore(StringRef FileName,
3032 ModuleKind Type,
3033 SourceLocation ImportLoc,
3034 ModuleFile *ImportedBy,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003035 SmallVectorImpl<ImportedModule> &Loaded,
Douglas Gregor7029ce12013-03-19 00:28:20 +00003036 off_t ExpectedSize, time_t ExpectedModTime,
Guy Benyei11169dd2012-12-18 14:30:41 +00003037 unsigned ClientLoadCapabilities) {
3038 ModuleFile *M;
Guy Benyei11169dd2012-12-18 14:30:41 +00003039 std::string ErrorStr;
Douglas Gregor7029ce12013-03-19 00:28:20 +00003040 ModuleManager::AddModuleResult AddResult
3041 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
3042 CurrentGeneration, ExpectedSize, ExpectedModTime,
3043 M, ErrorStr);
Guy Benyei11169dd2012-12-18 14:30:41 +00003044
Douglas Gregor7029ce12013-03-19 00:28:20 +00003045 switch (AddResult) {
3046 case ModuleManager::AlreadyLoaded:
3047 return Success;
3048
3049 case ModuleManager::NewlyLoaded:
3050 // Load module file below.
3051 break;
3052
3053 case ModuleManager::Missing:
3054 // The module file was missing; if the client handle handle, that, return
3055 // it.
3056 if (ClientLoadCapabilities & ARR_Missing)
3057 return Missing;
3058
3059 // Otherwise, return an error.
3060 {
3061 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
3062 + ErrorStr;
3063 Error(Msg);
3064 }
3065 return Failure;
3066
3067 case ModuleManager::OutOfDate:
3068 // We couldn't load the module file because it is out-of-date. If the
3069 // client can handle out-of-date, return it.
3070 if (ClientLoadCapabilities & ARR_OutOfDate)
3071 return OutOfDate;
3072
3073 // Otherwise, return an error.
3074 {
3075 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
3076 + ErrorStr;
3077 Error(Msg);
3078 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003079 return Failure;
3080 }
3081
Douglas Gregor7029ce12013-03-19 00:28:20 +00003082 assert(M && "Missing module file");
Guy Benyei11169dd2012-12-18 14:30:41 +00003083
3084 // FIXME: This seems rather a hack. Should CurrentDir be part of the
3085 // module?
3086 if (FileName != "-") {
3087 CurrentDir = llvm::sys::path::parent_path(FileName);
3088 if (CurrentDir.empty()) CurrentDir = ".";
3089 }
3090
3091 ModuleFile &F = *M;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003092 BitstreamCursor &Stream = F.Stream;
Guy Benyei11169dd2012-12-18 14:30:41 +00003093 Stream.init(F.StreamFile);
3094 F.SizeInBits = F.Buffer->getBufferSize() * 8;
3095
3096 // Sniff for the signature.
3097 if (Stream.Read(8) != 'C' ||
3098 Stream.Read(8) != 'P' ||
3099 Stream.Read(8) != 'C' ||
3100 Stream.Read(8) != 'H') {
3101 Diag(diag::err_not_a_pch_file) << FileName;
3102 return Failure;
3103 }
3104
3105 // This is used for compatibility with older PCH formats.
3106 bool HaveReadControlBlock = false;
3107
Chris Lattnerefa77172013-01-20 00:00:22 +00003108 while (1) {
3109 llvm::BitstreamEntry Entry = Stream.advance();
3110
3111 switch (Entry.Kind) {
3112 case llvm::BitstreamEntry::Error:
3113 case llvm::BitstreamEntry::EndBlock:
3114 case llvm::BitstreamEntry::Record:
Guy Benyei11169dd2012-12-18 14:30:41 +00003115 Error("invalid record at top-level of AST file");
3116 return Failure;
Chris Lattnerefa77172013-01-20 00:00:22 +00003117
3118 case llvm::BitstreamEntry::SubBlock:
3119 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003120 }
3121
Guy Benyei11169dd2012-12-18 14:30:41 +00003122 // We only know the control subblock ID.
Chris Lattnerefa77172013-01-20 00:00:22 +00003123 switch (Entry.ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003124 case llvm::bitc::BLOCKINFO_BLOCK_ID:
3125 if (Stream.ReadBlockInfoBlock()) {
3126 Error("malformed BlockInfoBlock in AST file");
3127 return Failure;
3128 }
3129 break;
3130 case CONTROL_BLOCK_ID:
3131 HaveReadControlBlock = true;
3132 switch (ReadControlBlock(F, Loaded, ClientLoadCapabilities)) {
3133 case Success:
3134 break;
3135
3136 case Failure: return Failure;
Douglas Gregor7029ce12013-03-19 00:28:20 +00003137 case Missing: return Missing;
Guy Benyei11169dd2012-12-18 14:30:41 +00003138 case OutOfDate: return OutOfDate;
3139 case VersionMismatch: return VersionMismatch;
3140 case ConfigurationMismatch: return ConfigurationMismatch;
3141 case HadErrors: return HadErrors;
3142 }
3143 break;
3144 case AST_BLOCK_ID:
3145 if (!HaveReadControlBlock) {
3146 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
3147 Diag(diag::warn_pch_version_too_old);
3148 return VersionMismatch;
3149 }
3150
3151 // Record that we've loaded this module.
3152 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
3153 return Success;
3154
3155 default:
3156 if (Stream.SkipBlock()) {
3157 Error("malformed block record in AST file");
3158 return Failure;
3159 }
3160 break;
3161 }
3162 }
3163
3164 return Success;
3165}
3166
3167void ASTReader::InitializeContext() {
3168 // If there's a listener, notify them that we "read" the translation unit.
3169 if (DeserializationListener)
3170 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
3171 Context.getTranslationUnitDecl());
3172
3173 // Make sure we load the declaration update records for the translation unit,
3174 // if there are any.
3175 loadDeclUpdateRecords(PREDEF_DECL_TRANSLATION_UNIT_ID,
3176 Context.getTranslationUnitDecl());
3177
3178 // FIXME: Find a better way to deal with collisions between these
3179 // built-in types. Right now, we just ignore the problem.
3180
3181 // Load the special types.
3182 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
3183 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
3184 if (!Context.CFConstantStringTypeDecl)
3185 Context.setCFConstantStringType(GetType(String));
3186 }
3187
3188 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
3189 QualType FileType = GetType(File);
3190 if (FileType.isNull()) {
3191 Error("FILE type is NULL");
3192 return;
3193 }
3194
3195 if (!Context.FILEDecl) {
3196 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
3197 Context.setFILEDecl(Typedef->getDecl());
3198 else {
3199 const TagType *Tag = FileType->getAs<TagType>();
3200 if (!Tag) {
3201 Error("Invalid FILE type in AST file");
3202 return;
3203 }
3204 Context.setFILEDecl(Tag->getDecl());
3205 }
3206 }
3207 }
3208
3209 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
3210 QualType Jmp_bufType = GetType(Jmp_buf);
3211 if (Jmp_bufType.isNull()) {
3212 Error("jmp_buf type is NULL");
3213 return;
3214 }
3215
3216 if (!Context.jmp_bufDecl) {
3217 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
3218 Context.setjmp_bufDecl(Typedef->getDecl());
3219 else {
3220 const TagType *Tag = Jmp_bufType->getAs<TagType>();
3221 if (!Tag) {
3222 Error("Invalid jmp_buf type in AST file");
3223 return;
3224 }
3225 Context.setjmp_bufDecl(Tag->getDecl());
3226 }
3227 }
3228 }
3229
3230 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
3231 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
3232 if (Sigjmp_bufType.isNull()) {
3233 Error("sigjmp_buf type is NULL");
3234 return;
3235 }
3236
3237 if (!Context.sigjmp_bufDecl) {
3238 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
3239 Context.setsigjmp_bufDecl(Typedef->getDecl());
3240 else {
3241 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
3242 assert(Tag && "Invalid sigjmp_buf type in AST file");
3243 Context.setsigjmp_bufDecl(Tag->getDecl());
3244 }
3245 }
3246 }
3247
3248 if (unsigned ObjCIdRedef
3249 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
3250 if (Context.ObjCIdRedefinitionType.isNull())
3251 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
3252 }
3253
3254 if (unsigned ObjCClassRedef
3255 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
3256 if (Context.ObjCClassRedefinitionType.isNull())
3257 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
3258 }
3259
3260 if (unsigned ObjCSelRedef
3261 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
3262 if (Context.ObjCSelRedefinitionType.isNull())
3263 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
3264 }
3265
3266 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
3267 QualType Ucontext_tType = GetType(Ucontext_t);
3268 if (Ucontext_tType.isNull()) {
3269 Error("ucontext_t type is NULL");
3270 return;
3271 }
3272
3273 if (!Context.ucontext_tDecl) {
3274 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
3275 Context.setucontext_tDecl(Typedef->getDecl());
3276 else {
3277 const TagType *Tag = Ucontext_tType->getAs<TagType>();
3278 assert(Tag && "Invalid ucontext_t type in AST file");
3279 Context.setucontext_tDecl(Tag->getDecl());
3280 }
3281 }
3282 }
3283 }
3284
3285 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
3286
3287 // If there were any CUDA special declarations, deserialize them.
3288 if (!CUDASpecialDeclRefs.empty()) {
3289 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
3290 Context.setcudaConfigureCallDecl(
3291 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
3292 }
3293
3294 // Re-export any modules that were imported by a non-module AST file.
3295 for (unsigned I = 0, N = ImportedModules.size(); I != N; ++I) {
3296 if (Module *Imported = getSubmodule(ImportedModules[I]))
Argyrios Kyrtzidis125df052013-02-01 16:36:12 +00003297 makeModuleVisible(Imported, Module::AllVisible,
Douglas Gregorfb912652013-03-20 21:10:35 +00003298 /*ImportLoc=*/SourceLocation(),
3299 /*Complain=*/false);
Guy Benyei11169dd2012-12-18 14:30:41 +00003300 }
3301 ImportedModules.clear();
3302}
3303
3304void ASTReader::finalizeForWriting() {
3305 for (HiddenNamesMapType::iterator Hidden = HiddenNamesMap.begin(),
3306 HiddenEnd = HiddenNamesMap.end();
3307 Hidden != HiddenEnd; ++Hidden) {
Argyrios Kyrtzidis3a9c42c2013-03-27 01:25:34 +00003308 makeNamesVisible(Hidden->second, Hidden->first);
Guy Benyei11169dd2012-12-18 14:30:41 +00003309 }
3310 HiddenNamesMap.clear();
3311}
3312
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003313/// SkipCursorToControlBlock - Given a cursor at the start of an AST file, scan
3314/// ahead and drop the cursor into the start of the CONTROL_BLOCK, returning
3315/// false on success and true on failure.
3316static bool SkipCursorToControlBlock(BitstreamCursor &Cursor) {
3317 while (1) {
3318 llvm::BitstreamEntry Entry = Cursor.advance();
3319 switch (Entry.Kind) {
3320 case llvm::BitstreamEntry::Error:
3321 case llvm::BitstreamEntry::EndBlock:
3322 return true;
3323
3324 case llvm::BitstreamEntry::Record:
3325 // Ignore top-level records.
3326 Cursor.skipRecord(Entry.ID);
3327 break;
3328
3329 case llvm::BitstreamEntry::SubBlock:
3330 if (Entry.ID == CONTROL_BLOCK_ID) {
3331 if (Cursor.EnterSubBlock(CONTROL_BLOCK_ID))
3332 return true;
3333 // Found it!
3334 return false;
3335 }
3336
3337 if (Cursor.SkipBlock())
3338 return true;
3339 }
3340 }
3341}
3342
Guy Benyei11169dd2012-12-18 14:30:41 +00003343/// \brief Retrieve the name of the original source file name
3344/// directly from the AST file, without actually loading the AST
3345/// file.
3346std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
3347 FileManager &FileMgr,
3348 DiagnosticsEngine &Diags) {
3349 // Open the AST file.
3350 std::string ErrStr;
3351 OwningPtr<llvm::MemoryBuffer> Buffer;
3352 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
3353 if (!Buffer) {
3354 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ASTFileName << ErrStr;
3355 return std::string();
3356 }
3357
3358 // Initialize the stream
3359 llvm::BitstreamReader StreamFile;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003360 BitstreamCursor Stream;
Guy Benyei11169dd2012-12-18 14:30:41 +00003361 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
3362 (const unsigned char *)Buffer->getBufferEnd());
3363 Stream.init(StreamFile);
3364
3365 // Sniff for the signature.
3366 if (Stream.Read(8) != 'C' ||
3367 Stream.Read(8) != 'P' ||
3368 Stream.Read(8) != 'C' ||
3369 Stream.Read(8) != 'H') {
3370 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
3371 return std::string();
3372 }
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003373
Chris Lattnere7b154b2013-01-19 21:39:22 +00003374 // Scan for the CONTROL_BLOCK_ID block.
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003375 if (SkipCursorToControlBlock(Stream)) {
3376 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
3377 return std::string();
Chris Lattnere7b154b2013-01-19 21:39:22 +00003378 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003379
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003380 // Scan for ORIGINAL_FILE inside the control block.
3381 RecordData Record;
Chris Lattnere7b154b2013-01-19 21:39:22 +00003382 while (1) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003383 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Chris Lattnere7b154b2013-01-19 21:39:22 +00003384 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
3385 return std::string();
3386
3387 if (Entry.Kind != llvm::BitstreamEntry::Record) {
3388 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
3389 return std::string();
Guy Benyei11169dd2012-12-18 14:30:41 +00003390 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00003391
Guy Benyei11169dd2012-12-18 14:30:41 +00003392 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00003393 StringRef Blob;
3394 if (Stream.readRecord(Entry.ID, Record, &Blob) == ORIGINAL_FILE)
3395 return Blob.str();
Guy Benyei11169dd2012-12-18 14:30:41 +00003396 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003397}
3398
3399namespace {
3400 class SimplePCHValidator : public ASTReaderListener {
3401 const LangOptions &ExistingLangOpts;
3402 const TargetOptions &ExistingTargetOpts;
3403 const PreprocessorOptions &ExistingPPOpts;
3404 FileManager &FileMgr;
3405
3406 public:
3407 SimplePCHValidator(const LangOptions &ExistingLangOpts,
3408 const TargetOptions &ExistingTargetOpts,
3409 const PreprocessorOptions &ExistingPPOpts,
3410 FileManager &FileMgr)
3411 : ExistingLangOpts(ExistingLangOpts),
3412 ExistingTargetOpts(ExistingTargetOpts),
3413 ExistingPPOpts(ExistingPPOpts),
3414 FileMgr(FileMgr)
3415 {
3416 }
3417
3418 virtual bool ReadLanguageOptions(const LangOptions &LangOpts,
3419 bool Complain) {
3420 return checkLanguageOptions(ExistingLangOpts, LangOpts, 0);
3421 }
3422 virtual bool ReadTargetOptions(const TargetOptions &TargetOpts,
3423 bool Complain) {
3424 return checkTargetOptions(ExistingTargetOpts, TargetOpts, 0);
3425 }
3426 virtual bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
3427 bool Complain,
3428 std::string &SuggestedPredefines) {
3429 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, 0, FileMgr,
3430 SuggestedPredefines);
3431 }
3432 };
3433}
3434
3435bool ASTReader::readASTFileControlBlock(StringRef Filename,
3436 FileManager &FileMgr,
3437 ASTReaderListener &Listener) {
3438 // Open the AST file.
3439 std::string ErrStr;
3440 OwningPtr<llvm::MemoryBuffer> Buffer;
3441 Buffer.reset(FileMgr.getBufferForFile(Filename, &ErrStr));
3442 if (!Buffer) {
3443 return true;
3444 }
3445
3446 // Initialize the stream
3447 llvm::BitstreamReader StreamFile;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003448 BitstreamCursor Stream;
Guy Benyei11169dd2012-12-18 14:30:41 +00003449 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
3450 (const unsigned char *)Buffer->getBufferEnd());
3451 Stream.init(StreamFile);
3452
3453 // Sniff for the signature.
3454 if (Stream.Read(8) != 'C' ||
3455 Stream.Read(8) != 'P' ||
3456 Stream.Read(8) != 'C' ||
3457 Stream.Read(8) != 'H') {
3458 return true;
3459 }
3460
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003461 // Scan for the CONTROL_BLOCK_ID block.
3462 if (SkipCursorToControlBlock(Stream))
3463 return true;
3464
3465 // Scan for ORIGINAL_FILE inside the control block.
Guy Benyei11169dd2012-12-18 14:30:41 +00003466 RecordData Record;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003467 while (1) {
3468 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
3469 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
3470 return false;
3471
3472 if (Entry.Kind != llvm::BitstreamEntry::Record)
3473 return true;
3474
Guy Benyei11169dd2012-12-18 14:30:41 +00003475 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00003476 StringRef Blob;
3477 unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob);
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003478 switch ((ControlRecordTypes)RecCode) {
3479 case METADATA: {
3480 if (Record[0] != VERSION_MAJOR)
3481 return true;
Guy Benyei11169dd2012-12-18 14:30:41 +00003482
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +00003483 if (Listener.ReadFullVersionInformation(Blob))
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003484 return true;
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +00003485
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003486 break;
3487 }
3488 case LANGUAGE_OPTIONS:
3489 if (ParseLanguageOptions(Record, false, Listener))
3490 return true;
3491 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003492
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003493 case TARGET_OPTIONS:
3494 if (ParseTargetOptions(Record, false, Listener))
3495 return true;
3496 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003497
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003498 case DIAGNOSTIC_OPTIONS:
3499 if (ParseDiagnosticOptions(Record, false, Listener))
3500 return true;
3501 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003502
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003503 case FILE_SYSTEM_OPTIONS:
3504 if (ParseFileSystemOptions(Record, false, Listener))
3505 return true;
3506 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003507
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003508 case HEADER_SEARCH_OPTIONS:
3509 if (ParseHeaderSearchOptions(Record, false, Listener))
3510 return true;
3511 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003512
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003513 case PREPROCESSOR_OPTIONS: {
3514 std::string IgnoredSuggestedPredefines;
3515 if (ParsePreprocessorOptions(Record, false, Listener,
3516 IgnoredSuggestedPredefines))
3517 return true;
3518 break;
3519 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003520
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003521 default:
3522 // No other validation to perform.
3523 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003524 }
3525 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003526}
3527
3528
3529bool ASTReader::isAcceptableASTFile(StringRef Filename,
3530 FileManager &FileMgr,
3531 const LangOptions &LangOpts,
3532 const TargetOptions &TargetOpts,
3533 const PreprocessorOptions &PPOpts) {
3534 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts, FileMgr);
3535 return !readASTFileControlBlock(Filename, FileMgr, validator);
3536}
3537
3538bool ASTReader::ReadSubmoduleBlock(ModuleFile &F) {
3539 // Enter the submodule block.
3540 if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
3541 Error("malformed submodule block record in AST file");
3542 return true;
3543 }
3544
3545 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
3546 bool First = true;
3547 Module *CurrentModule = 0;
3548 RecordData Record;
3549 while (true) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003550 llvm::BitstreamEntry Entry = F.Stream.advanceSkippingSubblocks();
3551
3552 switch (Entry.Kind) {
3553 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
3554 case llvm::BitstreamEntry::Error:
3555 Error("malformed block record in AST file");
3556 return true;
3557 case llvm::BitstreamEntry::EndBlock:
Guy Benyei11169dd2012-12-18 14:30:41 +00003558 return false;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003559 case llvm::BitstreamEntry::Record:
3560 // The interesting case.
3561 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003562 }
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003563
Guy Benyei11169dd2012-12-18 14:30:41 +00003564 // Read a record.
Chris Lattner0e6c9402013-01-20 02:38:54 +00003565 StringRef Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00003566 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00003567 switch (F.Stream.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003568 default: // Default behavior: ignore.
3569 break;
3570
3571 case SUBMODULE_DEFINITION: {
3572 if (First) {
3573 Error("missing submodule metadata record at beginning of block");
3574 return true;
3575 }
3576
Douglas Gregor8d932422013-03-20 03:59:18 +00003577 if (Record.size() < 8) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003578 Error("malformed module definition");
3579 return true;
3580 }
3581
Chris Lattner0e6c9402013-01-20 02:38:54 +00003582 StringRef Name = Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00003583 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[0]);
3584 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[1]);
3585 bool IsFramework = Record[2];
3586 bool IsExplicit = Record[3];
3587 bool IsSystem = Record[4];
3588 bool InferSubmodules = Record[5];
3589 bool InferExplicitSubmodules = Record[6];
3590 bool InferExportWildcard = Record[7];
Douglas Gregor8d932422013-03-20 03:59:18 +00003591 bool ConfigMacrosExhaustive = Record[8];
3592
Guy Benyei11169dd2012-12-18 14:30:41 +00003593 Module *ParentModule = 0;
3594 if (Parent)
3595 ParentModule = getSubmodule(Parent);
3596
3597 // Retrieve this (sub)module from the module map, creating it if
3598 // necessary.
3599 CurrentModule = ModMap.findOrCreateModule(Name, ParentModule,
3600 IsFramework,
3601 IsExplicit).first;
3602 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
3603 if (GlobalIndex >= SubmodulesLoaded.size() ||
3604 SubmodulesLoaded[GlobalIndex]) {
3605 Error("too many submodules");
3606 return true;
3607 }
Douglas Gregor8a114ab2013-02-06 22:40:31 +00003608
Douglas Gregor7029ce12013-03-19 00:28:20 +00003609 if (!ParentModule) {
3610 if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
3611 if (CurFile != F.File) {
3612 if (!Diags.isDiagnosticInFlight()) {
3613 Diag(diag::err_module_file_conflict)
3614 << CurrentModule->getTopLevelModuleName()
3615 << CurFile->getName()
3616 << F.File->getName();
3617 }
3618 return true;
Douglas Gregor8a114ab2013-02-06 22:40:31 +00003619 }
Douglas Gregor8a114ab2013-02-06 22:40:31 +00003620 }
Douglas Gregor7029ce12013-03-19 00:28:20 +00003621
3622 CurrentModule->setASTFile(F.File);
Douglas Gregor8a114ab2013-02-06 22:40:31 +00003623 }
Douglas Gregor7029ce12013-03-19 00:28:20 +00003624
Guy Benyei11169dd2012-12-18 14:30:41 +00003625 CurrentModule->IsFromModuleFile = true;
3626 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
3627 CurrentModule->InferSubmodules = InferSubmodules;
3628 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
3629 CurrentModule->InferExportWildcard = InferExportWildcard;
Douglas Gregor8d932422013-03-20 03:59:18 +00003630 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
Guy Benyei11169dd2012-12-18 14:30:41 +00003631 if (DeserializationListener)
3632 DeserializationListener->ModuleRead(GlobalID, CurrentModule);
3633
3634 SubmodulesLoaded[GlobalIndex] = CurrentModule;
Douglas Gregor6ddfca92013-01-14 17:21:00 +00003635
Douglas Gregorfb912652013-03-20 21:10:35 +00003636 // Clear out data that will be replaced by what is the module file.
Douglas Gregor6ddfca92013-01-14 17:21:00 +00003637 CurrentModule->LinkLibraries.clear();
Douglas Gregor8d932422013-03-20 03:59:18 +00003638 CurrentModule->ConfigMacros.clear();
Douglas Gregorfb912652013-03-20 21:10:35 +00003639 CurrentModule->UnresolvedConflicts.clear();
3640 CurrentModule->Conflicts.clear();
Guy Benyei11169dd2012-12-18 14:30:41 +00003641 break;
3642 }
3643
3644 case SUBMODULE_UMBRELLA_HEADER: {
3645 if (First) {
3646 Error("missing submodule metadata record at beginning of block");
3647 return true;
3648 }
3649
3650 if (!CurrentModule)
3651 break;
3652
Chris Lattner0e6c9402013-01-20 02:38:54 +00003653 if (const FileEntry *Umbrella = PP.getFileManager().getFile(Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003654 if (!CurrentModule->getUmbrellaHeader())
3655 ModMap.setUmbrellaHeader(CurrentModule, Umbrella);
3656 else if (CurrentModule->getUmbrellaHeader() != Umbrella) {
3657 Error("mismatched umbrella headers in submodule");
3658 return true;
3659 }
3660 }
3661 break;
3662 }
3663
3664 case SUBMODULE_HEADER: {
3665 if (First) {
3666 Error("missing submodule metadata record at beginning of block");
3667 return true;
3668 }
3669
3670 if (!CurrentModule)
3671 break;
3672
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00003673 // We lazily associate headers with their modules via the HeaderInfoTable.
3674 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
3675 // of complete filenames or remove it entirely.
Guy Benyei11169dd2012-12-18 14:30:41 +00003676 break;
3677 }
3678
3679 case SUBMODULE_EXCLUDED_HEADER: {
3680 if (First) {
3681 Error("missing submodule metadata record at beginning of block");
3682 return true;
3683 }
3684
3685 if (!CurrentModule)
3686 break;
3687
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00003688 // We lazily associate headers with their modules via the HeaderInfoTable.
3689 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
3690 // of complete filenames or remove it entirely.
Guy Benyei11169dd2012-12-18 14:30:41 +00003691 break;
3692 }
3693
3694 case SUBMODULE_TOPHEADER: {
3695 if (First) {
3696 Error("missing submodule metadata record at beginning of block");
3697 return true;
3698 }
3699
3700 if (!CurrentModule)
3701 break;
3702
Argyrios Kyrtzidis3c5305c2013-03-13 21:13:43 +00003703 CurrentModule->addTopHeaderFilename(Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +00003704 break;
3705 }
3706
3707 case SUBMODULE_UMBRELLA_DIR: {
3708 if (First) {
3709 Error("missing submodule metadata record at beginning of block");
3710 return true;
3711 }
3712
3713 if (!CurrentModule)
3714 break;
3715
Guy Benyei11169dd2012-12-18 14:30:41 +00003716 if (const DirectoryEntry *Umbrella
Chris Lattner0e6c9402013-01-20 02:38:54 +00003717 = PP.getFileManager().getDirectory(Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003718 if (!CurrentModule->getUmbrellaDir())
3719 ModMap.setUmbrellaDir(CurrentModule, Umbrella);
3720 else if (CurrentModule->getUmbrellaDir() != Umbrella) {
3721 Error("mismatched umbrella directories in submodule");
3722 return true;
3723 }
3724 }
3725 break;
3726 }
3727
3728 case SUBMODULE_METADATA: {
3729 if (!First) {
3730 Error("submodule metadata record not at beginning of block");
3731 return true;
3732 }
3733 First = false;
3734
3735 F.BaseSubmoduleID = getTotalNumSubmodules();
3736 F.LocalNumSubmodules = Record[0];
3737 unsigned LocalBaseSubmoduleID = Record[1];
3738 if (F.LocalNumSubmodules > 0) {
3739 // Introduce the global -> local mapping for submodules within this
3740 // module.
3741 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
3742
3743 // Introduce the local -> global mapping for submodules within this
3744 // module.
3745 F.SubmoduleRemap.insertOrReplace(
3746 std::make_pair(LocalBaseSubmoduleID,
3747 F.BaseSubmoduleID - LocalBaseSubmoduleID));
3748
3749 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
3750 }
3751 break;
3752 }
3753
3754 case SUBMODULE_IMPORTS: {
3755 if (First) {
3756 Error("missing submodule metadata record at beginning of block");
3757 return true;
3758 }
3759
3760 if (!CurrentModule)
3761 break;
3762
3763 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
Douglas Gregorfb912652013-03-20 21:10:35 +00003764 UnresolvedModuleRef Unresolved;
Guy Benyei11169dd2012-12-18 14:30:41 +00003765 Unresolved.File = &F;
3766 Unresolved.Mod = CurrentModule;
3767 Unresolved.ID = Record[Idx];
Douglas Gregorfb912652013-03-20 21:10:35 +00003768 Unresolved.Kind = UnresolvedModuleRef::Import;
Guy Benyei11169dd2012-12-18 14:30:41 +00003769 Unresolved.IsWildcard = false;
Douglas Gregorfb912652013-03-20 21:10:35 +00003770 UnresolvedModuleRefs.push_back(Unresolved);
Guy Benyei11169dd2012-12-18 14:30:41 +00003771 }
3772 break;
3773 }
3774
3775 case SUBMODULE_EXPORTS: {
3776 if (First) {
3777 Error("missing submodule metadata record at beginning of block");
3778 return true;
3779 }
3780
3781 if (!CurrentModule)
3782 break;
3783
3784 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
Douglas Gregorfb912652013-03-20 21:10:35 +00003785 UnresolvedModuleRef Unresolved;
Guy Benyei11169dd2012-12-18 14:30:41 +00003786 Unresolved.File = &F;
3787 Unresolved.Mod = CurrentModule;
3788 Unresolved.ID = Record[Idx];
Douglas Gregorfb912652013-03-20 21:10:35 +00003789 Unresolved.Kind = UnresolvedModuleRef::Export;
Guy Benyei11169dd2012-12-18 14:30:41 +00003790 Unresolved.IsWildcard = Record[Idx + 1];
Douglas Gregorfb912652013-03-20 21:10:35 +00003791 UnresolvedModuleRefs.push_back(Unresolved);
Guy Benyei11169dd2012-12-18 14:30:41 +00003792 }
3793
3794 // Once we've loaded the set of exports, there's no reason to keep
3795 // the parsed, unresolved exports around.
3796 CurrentModule->UnresolvedExports.clear();
3797 break;
3798 }
3799 case SUBMODULE_REQUIRES: {
3800 if (First) {
3801 Error("missing submodule metadata record at beginning of block");
3802 return true;
3803 }
3804
3805 if (!CurrentModule)
3806 break;
3807
Chris Lattner0e6c9402013-01-20 02:38:54 +00003808 CurrentModule->addRequirement(Blob, Context.getLangOpts(),
Guy Benyei11169dd2012-12-18 14:30:41 +00003809 Context.getTargetInfo());
3810 break;
3811 }
Douglas Gregor6ddfca92013-01-14 17:21:00 +00003812
3813 case SUBMODULE_LINK_LIBRARY:
3814 if (First) {
3815 Error("missing submodule metadata record at beginning of block");
3816 return true;
3817 }
3818
3819 if (!CurrentModule)
3820 break;
3821
3822 CurrentModule->LinkLibraries.push_back(
Chris Lattner0e6c9402013-01-20 02:38:54 +00003823 Module::LinkLibrary(Blob, Record[0]));
Douglas Gregor6ddfca92013-01-14 17:21:00 +00003824 break;
Douglas Gregor35b13ec2013-03-20 00:22:05 +00003825
3826 case SUBMODULE_CONFIG_MACRO:
3827 if (First) {
3828 Error("missing submodule metadata record at beginning of block");
3829 return true;
3830 }
3831
3832 if (!CurrentModule)
3833 break;
3834
3835 CurrentModule->ConfigMacros.push_back(Blob.str());
3836 break;
Douglas Gregorfb912652013-03-20 21:10:35 +00003837
3838 case SUBMODULE_CONFLICT: {
3839 if (First) {
3840 Error("missing submodule metadata record at beginning of block");
3841 return true;
3842 }
3843
3844 if (!CurrentModule)
3845 break;
3846
3847 UnresolvedModuleRef Unresolved;
3848 Unresolved.File = &F;
3849 Unresolved.Mod = CurrentModule;
3850 Unresolved.ID = Record[0];
3851 Unresolved.Kind = UnresolvedModuleRef::Conflict;
3852 Unresolved.IsWildcard = false;
3853 Unresolved.String = Blob;
3854 UnresolvedModuleRefs.push_back(Unresolved);
3855 break;
3856 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003857 }
3858 }
3859}
3860
3861/// \brief Parse the record that corresponds to a LangOptions data
3862/// structure.
3863///
3864/// This routine parses the language options from the AST file and then gives
3865/// them to the AST listener if one is set.
3866///
3867/// \returns true if the listener deems the file unacceptable, false otherwise.
3868bool ASTReader::ParseLanguageOptions(const RecordData &Record,
3869 bool Complain,
3870 ASTReaderListener &Listener) {
3871 LangOptions LangOpts;
3872 unsigned Idx = 0;
3873#define LANGOPT(Name, Bits, Default, Description) \
3874 LangOpts.Name = Record[Idx++];
3875#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
3876 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
3877#include "clang/Basic/LangOptions.def"
Will Dietzf54319c2013-01-18 11:30:38 +00003878#define SANITIZER(NAME, ID) LangOpts.Sanitize.ID = Record[Idx++];
3879#include "clang/Basic/Sanitizers.def"
Guy Benyei11169dd2012-12-18 14:30:41 +00003880
3881 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
3882 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
3883 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
3884
3885 unsigned Length = Record[Idx++];
3886 LangOpts.CurrentModule.assign(Record.begin() + Idx,
3887 Record.begin() + Idx + Length);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00003888
3889 Idx += Length;
3890
3891 // Comment options.
3892 for (unsigned N = Record[Idx++]; N; --N) {
3893 LangOpts.CommentOpts.BlockCommandNames.push_back(
3894 ReadString(Record, Idx));
3895 }
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00003896 LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00003897
Guy Benyei11169dd2012-12-18 14:30:41 +00003898 return Listener.ReadLanguageOptions(LangOpts, Complain);
3899}
3900
3901bool ASTReader::ParseTargetOptions(const RecordData &Record,
3902 bool Complain,
3903 ASTReaderListener &Listener) {
3904 unsigned Idx = 0;
3905 TargetOptions TargetOpts;
3906 TargetOpts.Triple = ReadString(Record, Idx);
3907 TargetOpts.CPU = ReadString(Record, Idx);
3908 TargetOpts.ABI = ReadString(Record, Idx);
3909 TargetOpts.CXXABI = ReadString(Record, Idx);
3910 TargetOpts.LinkerVersion = ReadString(Record, Idx);
3911 for (unsigned N = Record[Idx++]; N; --N) {
3912 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
3913 }
3914 for (unsigned N = Record[Idx++]; N; --N) {
3915 TargetOpts.Features.push_back(ReadString(Record, Idx));
3916 }
3917
3918 return Listener.ReadTargetOptions(TargetOpts, Complain);
3919}
3920
3921bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
3922 ASTReaderListener &Listener) {
3923 DiagnosticOptions DiagOpts;
3924 unsigned Idx = 0;
3925#define DIAGOPT(Name, Bits, Default) DiagOpts.Name = Record[Idx++];
3926#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
3927 DiagOpts.set##Name(static_cast<Type>(Record[Idx++]));
3928#include "clang/Basic/DiagnosticOptions.def"
3929
3930 for (unsigned N = Record[Idx++]; N; --N) {
3931 DiagOpts.Warnings.push_back(ReadString(Record, Idx));
3932 }
3933
3934 return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
3935}
3936
3937bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
3938 ASTReaderListener &Listener) {
3939 FileSystemOptions FSOpts;
3940 unsigned Idx = 0;
3941 FSOpts.WorkingDir = ReadString(Record, Idx);
3942 return Listener.ReadFileSystemOptions(FSOpts, Complain);
3943}
3944
3945bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
3946 bool Complain,
3947 ASTReaderListener &Listener) {
3948 HeaderSearchOptions HSOpts;
3949 unsigned Idx = 0;
3950 HSOpts.Sysroot = ReadString(Record, Idx);
3951
3952 // Include entries.
3953 for (unsigned N = Record[Idx++]; N; --N) {
3954 std::string Path = ReadString(Record, Idx);
3955 frontend::IncludeDirGroup Group
3956 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
Guy Benyei11169dd2012-12-18 14:30:41 +00003957 bool IsFramework = Record[Idx++];
3958 bool IgnoreSysRoot = Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00003959 HSOpts.UserEntries.push_back(
Daniel Dunbar53681732013-01-30 00:34:26 +00003960 HeaderSearchOptions::Entry(Path, Group, IsFramework, IgnoreSysRoot));
Guy Benyei11169dd2012-12-18 14:30:41 +00003961 }
3962
3963 // System header prefixes.
3964 for (unsigned N = Record[Idx++]; N; --N) {
3965 std::string Prefix = ReadString(Record, Idx);
3966 bool IsSystemHeader = Record[Idx++];
3967 HSOpts.SystemHeaderPrefixes.push_back(
3968 HeaderSearchOptions::SystemHeaderPrefix(Prefix, IsSystemHeader));
3969 }
3970
3971 HSOpts.ResourceDir = ReadString(Record, Idx);
3972 HSOpts.ModuleCachePath = ReadString(Record, Idx);
3973 HSOpts.DisableModuleHash = Record[Idx++];
3974 HSOpts.UseBuiltinIncludes = Record[Idx++];
3975 HSOpts.UseStandardSystemIncludes = Record[Idx++];
3976 HSOpts.UseStandardCXXIncludes = Record[Idx++];
3977 HSOpts.UseLibcxx = Record[Idx++];
3978
3979 return Listener.ReadHeaderSearchOptions(HSOpts, Complain);
3980}
3981
3982bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
3983 bool Complain,
3984 ASTReaderListener &Listener,
3985 std::string &SuggestedPredefines) {
3986 PreprocessorOptions PPOpts;
3987 unsigned Idx = 0;
3988
3989 // Macro definitions/undefs
3990 for (unsigned N = Record[Idx++]; N; --N) {
3991 std::string Macro = ReadString(Record, Idx);
3992 bool IsUndef = Record[Idx++];
3993 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
3994 }
3995
3996 // Includes
3997 for (unsigned N = Record[Idx++]; N; --N) {
3998 PPOpts.Includes.push_back(ReadString(Record, Idx));
3999 }
4000
4001 // Macro Includes
4002 for (unsigned N = Record[Idx++]; N; --N) {
4003 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
4004 }
4005
4006 PPOpts.UsePredefines = Record[Idx++];
4007 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
4008 PPOpts.ImplicitPTHInclude = ReadString(Record, Idx);
4009 PPOpts.ObjCXXARCStandardLibrary =
4010 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
4011 SuggestedPredefines.clear();
4012 return Listener.ReadPreprocessorOptions(PPOpts, Complain,
4013 SuggestedPredefines);
4014}
4015
4016std::pair<ModuleFile *, unsigned>
4017ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
4018 GlobalPreprocessedEntityMapType::iterator
4019 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
4020 assert(I != GlobalPreprocessedEntityMap.end() &&
4021 "Corrupted global preprocessed entity map");
4022 ModuleFile *M = I->second;
4023 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
4024 return std::make_pair(M, LocalIndex);
4025}
4026
4027std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
4028ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
4029 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
4030 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
4031 Mod.NumPreprocessedEntities);
4032
4033 return std::make_pair(PreprocessingRecord::iterator(),
4034 PreprocessingRecord::iterator());
4035}
4036
4037std::pair<ASTReader::ModuleDeclIterator, ASTReader::ModuleDeclIterator>
4038ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
4039 return std::make_pair(ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
4040 ModuleDeclIterator(this, &Mod,
4041 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
4042}
4043
4044PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
4045 PreprocessedEntityID PPID = Index+1;
4046 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
4047 ModuleFile &M = *PPInfo.first;
4048 unsigned LocalIndex = PPInfo.second;
4049 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
4050
Guy Benyei11169dd2012-12-18 14:30:41 +00004051 if (!PP.getPreprocessingRecord()) {
4052 Error("no preprocessing record");
4053 return 0;
4054 }
4055
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004056 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
4057 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
4058
4059 llvm::BitstreamEntry Entry =
4060 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
4061 if (Entry.Kind != llvm::BitstreamEntry::Record)
4062 return 0;
4063
Guy Benyei11169dd2012-12-18 14:30:41 +00004064 // Read the record.
4065 SourceRange Range(ReadSourceLocation(M, PPOffs.Begin),
4066 ReadSourceLocation(M, PPOffs.End));
4067 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
Chris Lattner0e6c9402013-01-20 02:38:54 +00004068 StringRef Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00004069 RecordData Record;
4070 PreprocessorDetailRecordTypes RecType =
Chris Lattner0e6c9402013-01-20 02:38:54 +00004071 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.readRecord(
4072 Entry.ID, Record, &Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +00004073 switch (RecType) {
4074 case PPD_MACRO_EXPANSION: {
4075 bool isBuiltin = Record[0];
4076 IdentifierInfo *Name = 0;
4077 MacroDefinition *Def = 0;
4078 if (isBuiltin)
4079 Name = getLocalIdentifier(M, Record[1]);
4080 else {
4081 PreprocessedEntityID
4082 GlobalID = getGlobalPreprocessedEntityID(M, Record[1]);
4083 Def =cast<MacroDefinition>(PPRec.getLoadedPreprocessedEntity(GlobalID-1));
4084 }
4085
4086 MacroExpansion *ME;
4087 if (isBuiltin)
4088 ME = new (PPRec) MacroExpansion(Name, Range);
4089 else
4090 ME = new (PPRec) MacroExpansion(Def, Range);
4091
4092 return ME;
4093 }
4094
4095 case PPD_MACRO_DEFINITION: {
4096 // Decode the identifier info and then check again; if the macro is
4097 // still defined and associated with the identifier,
4098 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
4099 MacroDefinition *MD
4100 = new (PPRec) MacroDefinition(II, Range);
4101
4102 if (DeserializationListener)
4103 DeserializationListener->MacroDefinitionRead(PPID, MD);
4104
4105 return MD;
4106 }
4107
4108 case PPD_INCLUSION_DIRECTIVE: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00004109 const char *FullFileNameStart = Blob.data() + Record[0];
4110 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
Guy Benyei11169dd2012-12-18 14:30:41 +00004111 const FileEntry *File = 0;
4112 if (!FullFileName.empty())
4113 File = PP.getFileManager().getFile(FullFileName);
4114
4115 // FIXME: Stable encoding
4116 InclusionDirective::InclusionKind Kind
4117 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
4118 InclusionDirective *ID
4119 = new (PPRec) InclusionDirective(PPRec, Kind,
Chris Lattner0e6c9402013-01-20 02:38:54 +00004120 StringRef(Blob.data(), Record[0]),
Guy Benyei11169dd2012-12-18 14:30:41 +00004121 Record[1], Record[3],
4122 File,
4123 Range);
4124 return ID;
4125 }
4126 }
4127
4128 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
4129}
4130
4131/// \brief \arg SLocMapI points at a chunk of a module that contains no
4132/// preprocessed entities or the entities it contains are not the ones we are
4133/// looking for. Find the next module that contains entities and return the ID
4134/// of the first entry.
4135PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
4136 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
4137 ++SLocMapI;
4138 for (GlobalSLocOffsetMapType::const_iterator
4139 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
4140 ModuleFile &M = *SLocMapI->second;
4141 if (M.NumPreprocessedEntities)
4142 return M.BasePreprocessedEntityID;
4143 }
4144
4145 return getTotalNumPreprocessedEntities();
4146}
4147
4148namespace {
4149
4150template <unsigned PPEntityOffset::*PPLoc>
4151struct PPEntityComp {
4152 const ASTReader &Reader;
4153 ModuleFile &M;
4154
4155 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) { }
4156
4157 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
4158 SourceLocation LHS = getLoc(L);
4159 SourceLocation RHS = getLoc(R);
4160 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4161 }
4162
4163 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
4164 SourceLocation LHS = getLoc(L);
4165 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4166 }
4167
4168 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
4169 SourceLocation RHS = getLoc(R);
4170 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4171 }
4172
4173 SourceLocation getLoc(const PPEntityOffset &PPE) const {
4174 return Reader.ReadSourceLocation(M, PPE.*PPLoc);
4175 }
4176};
4177
4178}
4179
4180/// \brief Returns the first preprocessed entity ID that ends after \arg BLoc.
4181PreprocessedEntityID
4182ASTReader::findBeginPreprocessedEntity(SourceLocation BLoc) const {
4183 if (SourceMgr.isLocalSourceLocation(BLoc))
4184 return getTotalNumPreprocessedEntities();
4185
4186 GlobalSLocOffsetMapType::const_iterator
4187 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
Argyrios Kyrtzidis503c83a2013-03-08 02:32:34 +00004188 BLoc.getOffset() - 1);
Guy Benyei11169dd2012-12-18 14:30:41 +00004189 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
4190 "Corrupted global sloc offset map");
4191
4192 if (SLocMapI->second->NumPreprocessedEntities == 0)
4193 return findNextPreprocessedEntity(SLocMapI);
4194
4195 ModuleFile &M = *SLocMapI->second;
4196 typedef const PPEntityOffset *pp_iterator;
4197 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
4198 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
4199
4200 size_t Count = M.NumPreprocessedEntities;
4201 size_t Half;
4202 pp_iterator First = pp_begin;
4203 pp_iterator PPI;
4204
4205 // Do a binary search manually instead of using std::lower_bound because
4206 // The end locations of entities may be unordered (when a macro expansion
4207 // is inside another macro argument), but for this case it is not important
4208 // whether we get the first macro expansion or its containing macro.
4209 while (Count > 0) {
4210 Half = Count/2;
4211 PPI = First;
4212 std::advance(PPI, Half);
4213 if (SourceMgr.isBeforeInTranslationUnit(ReadSourceLocation(M, PPI->End),
4214 BLoc)){
4215 First = PPI;
4216 ++First;
4217 Count = Count - Half - 1;
4218 } else
4219 Count = Half;
4220 }
4221
4222 if (PPI == pp_end)
4223 return findNextPreprocessedEntity(SLocMapI);
4224
4225 return M.BasePreprocessedEntityID + (PPI - pp_begin);
4226}
4227
4228/// \brief Returns the first preprocessed entity ID that begins after \arg ELoc.
4229PreprocessedEntityID
4230ASTReader::findEndPreprocessedEntity(SourceLocation ELoc) const {
4231 if (SourceMgr.isLocalSourceLocation(ELoc))
4232 return getTotalNumPreprocessedEntities();
4233
4234 GlobalSLocOffsetMapType::const_iterator
4235 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
Argyrios Kyrtzidis503c83a2013-03-08 02:32:34 +00004236 ELoc.getOffset() - 1);
Guy Benyei11169dd2012-12-18 14:30:41 +00004237 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
4238 "Corrupted global sloc offset map");
4239
4240 if (SLocMapI->second->NumPreprocessedEntities == 0)
4241 return findNextPreprocessedEntity(SLocMapI);
4242
4243 ModuleFile &M = *SLocMapI->second;
4244 typedef const PPEntityOffset *pp_iterator;
4245 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
4246 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
4247 pp_iterator PPI =
4248 std::upper_bound(pp_begin, pp_end, ELoc,
4249 PPEntityComp<&PPEntityOffset::Begin>(*this, M));
4250
4251 if (PPI == pp_end)
4252 return findNextPreprocessedEntity(SLocMapI);
4253
4254 return M.BasePreprocessedEntityID + (PPI - pp_begin);
4255}
4256
4257/// \brief Returns a pair of [Begin, End) indices of preallocated
4258/// preprocessed entities that \arg Range encompasses.
4259std::pair<unsigned, unsigned>
4260 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
4261 if (Range.isInvalid())
4262 return std::make_pair(0,0);
4263 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
4264
4265 PreprocessedEntityID BeginID = findBeginPreprocessedEntity(Range.getBegin());
4266 PreprocessedEntityID EndID = findEndPreprocessedEntity(Range.getEnd());
4267 return std::make_pair(BeginID, EndID);
4268}
4269
4270/// \brief Optionally returns true or false if the preallocated preprocessed
4271/// entity with index \arg Index came from file \arg FID.
David Blaikie05785d12013-02-20 22:23:23 +00004272Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
Guy Benyei11169dd2012-12-18 14:30:41 +00004273 FileID FID) {
4274 if (FID.isInvalid())
4275 return false;
4276
4277 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
4278 ModuleFile &M = *PPInfo.first;
4279 unsigned LocalIndex = PPInfo.second;
4280 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
4281
4282 SourceLocation Loc = ReadSourceLocation(M, PPOffs.Begin);
4283 if (Loc.isInvalid())
4284 return false;
4285
4286 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
4287 return true;
4288 else
4289 return false;
4290}
4291
4292namespace {
4293 /// \brief Visitor used to search for information about a header file.
4294 class HeaderFileInfoVisitor {
Guy Benyei11169dd2012-12-18 14:30:41 +00004295 const FileEntry *FE;
4296
David Blaikie05785d12013-02-20 22:23:23 +00004297 Optional<HeaderFileInfo> HFI;
Guy Benyei11169dd2012-12-18 14:30:41 +00004298
4299 public:
Argyrios Kyrtzidis61a38962013-03-06 18:12:44 +00004300 explicit HeaderFileInfoVisitor(const FileEntry *FE)
4301 : FE(FE) { }
Guy Benyei11169dd2012-12-18 14:30:41 +00004302
4303 static bool visit(ModuleFile &M, void *UserData) {
4304 HeaderFileInfoVisitor *This
4305 = static_cast<HeaderFileInfoVisitor *>(UserData);
4306
Guy Benyei11169dd2012-12-18 14:30:41 +00004307 HeaderFileInfoLookupTable *Table
4308 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
4309 if (!Table)
4310 return false;
4311
4312 // Look in the on-disk hash table for an entry for this file name.
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00004313 HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE);
Guy Benyei11169dd2012-12-18 14:30:41 +00004314 if (Pos == Table->end())
4315 return false;
4316
4317 This->HFI = *Pos;
4318 return true;
4319 }
4320
David Blaikie05785d12013-02-20 22:23:23 +00004321 Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
Guy Benyei11169dd2012-12-18 14:30:41 +00004322 };
4323}
4324
4325HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
Argyrios Kyrtzidis61a38962013-03-06 18:12:44 +00004326 HeaderFileInfoVisitor Visitor(FE);
Guy Benyei11169dd2012-12-18 14:30:41 +00004327 ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor);
David Blaikie05785d12013-02-20 22:23:23 +00004328 if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004329 if (Listener)
4330 Listener->ReadHeaderFileInfo(*HFI, FE->getUID());
4331 return *HFI;
4332 }
4333
4334 return HeaderFileInfo();
4335}
4336
4337void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
4338 // FIXME: Make it work properly with modules.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004339 SmallVector<DiagnosticsEngine::DiagState *, 32> DiagStates;
Guy Benyei11169dd2012-12-18 14:30:41 +00004340 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
4341 ModuleFile &F = *(*I);
4342 unsigned Idx = 0;
4343 DiagStates.clear();
4344 assert(!Diag.DiagStates.empty());
4345 DiagStates.push_back(&Diag.DiagStates.front()); // the command-line one.
4346 while (Idx < F.PragmaDiagMappings.size()) {
4347 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
4348 unsigned DiagStateID = F.PragmaDiagMappings[Idx++];
4349 if (DiagStateID != 0) {
4350 Diag.DiagStatePoints.push_back(
4351 DiagnosticsEngine::DiagStatePoint(DiagStates[DiagStateID-1],
4352 FullSourceLoc(Loc, SourceMgr)));
4353 continue;
4354 }
4355
4356 assert(DiagStateID == 0);
4357 // A new DiagState was created here.
4358 Diag.DiagStates.push_back(*Diag.GetCurDiagState());
4359 DiagnosticsEngine::DiagState *NewState = &Diag.DiagStates.back();
4360 DiagStates.push_back(NewState);
4361 Diag.DiagStatePoints.push_back(
4362 DiagnosticsEngine::DiagStatePoint(NewState,
4363 FullSourceLoc(Loc, SourceMgr)));
4364 while (1) {
4365 assert(Idx < F.PragmaDiagMappings.size() &&
4366 "Invalid data, didn't find '-1' marking end of diag/map pairs");
4367 if (Idx >= F.PragmaDiagMappings.size()) {
4368 break; // Something is messed up but at least avoid infinite loop in
4369 // release build.
4370 }
4371 unsigned DiagID = F.PragmaDiagMappings[Idx++];
4372 if (DiagID == (unsigned)-1) {
4373 break; // no more diag/map pairs for this location.
4374 }
4375 diag::Mapping Map = (diag::Mapping)F.PragmaDiagMappings[Idx++];
4376 DiagnosticMappingInfo MappingInfo = Diag.makeMappingInfo(Map, Loc);
4377 Diag.GetCurDiagState()->setMappingInfo(DiagID, MappingInfo);
4378 }
4379 }
4380 }
4381}
4382
4383/// \brief Get the correct cursor and offset for loading a type.
4384ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
4385 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
4386 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
4387 ModuleFile *M = I->second;
4388 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
4389}
4390
4391/// \brief Read and return the type with the given index..
4392///
4393/// The index is the type ID, shifted and minus the number of predefs. This
4394/// routine actually reads the record corresponding to the type at the given
4395/// location. It is a helper routine for GetType, which deals with reading type
4396/// IDs.
4397QualType ASTReader::readTypeRecord(unsigned Index) {
4398 RecordLocation Loc = TypeCursorForIndex(Index);
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004399 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00004400
4401 // Keep track of where we are in the stream, then jump back there
4402 // after reading this type.
4403 SavedStreamPosition SavedPosition(DeclsCursor);
4404
4405 ReadingKindTracker ReadingKind(Read_Type, *this);
4406
4407 // Note that we are loading a type record.
4408 Deserializing AType(this);
4409
4410 unsigned Idx = 0;
4411 DeclsCursor.JumpToBit(Loc.Offset);
4412 RecordData Record;
4413 unsigned Code = DeclsCursor.ReadCode();
Chris Lattner0e6c9402013-01-20 02:38:54 +00004414 switch ((TypeCode)DeclsCursor.readRecord(Code, Record)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004415 case TYPE_EXT_QUAL: {
4416 if (Record.size() != 2) {
4417 Error("Incorrect encoding of extended qualifier type");
4418 return QualType();
4419 }
4420 QualType Base = readType(*Loc.F, Record, Idx);
4421 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
4422 return Context.getQualifiedType(Base, Quals);
4423 }
4424
4425 case TYPE_COMPLEX: {
4426 if (Record.size() != 1) {
4427 Error("Incorrect encoding of complex type");
4428 return QualType();
4429 }
4430 QualType ElemType = readType(*Loc.F, Record, Idx);
4431 return Context.getComplexType(ElemType);
4432 }
4433
4434 case TYPE_POINTER: {
4435 if (Record.size() != 1) {
4436 Error("Incorrect encoding of pointer type");
4437 return QualType();
4438 }
4439 QualType PointeeType = readType(*Loc.F, Record, Idx);
4440 return Context.getPointerType(PointeeType);
4441 }
4442
4443 case TYPE_BLOCK_POINTER: {
4444 if (Record.size() != 1) {
4445 Error("Incorrect encoding of block pointer type");
4446 return QualType();
4447 }
4448 QualType PointeeType = readType(*Loc.F, Record, Idx);
4449 return Context.getBlockPointerType(PointeeType);
4450 }
4451
4452 case TYPE_LVALUE_REFERENCE: {
4453 if (Record.size() != 2) {
4454 Error("Incorrect encoding of lvalue reference type");
4455 return QualType();
4456 }
4457 QualType PointeeType = readType(*Loc.F, Record, Idx);
4458 return Context.getLValueReferenceType(PointeeType, Record[1]);
4459 }
4460
4461 case TYPE_RVALUE_REFERENCE: {
4462 if (Record.size() != 1) {
4463 Error("Incorrect encoding of rvalue reference type");
4464 return QualType();
4465 }
4466 QualType PointeeType = readType(*Loc.F, Record, Idx);
4467 return Context.getRValueReferenceType(PointeeType);
4468 }
4469
4470 case TYPE_MEMBER_POINTER: {
4471 if (Record.size() != 2) {
4472 Error("Incorrect encoding of member pointer type");
4473 return QualType();
4474 }
4475 QualType PointeeType = readType(*Loc.F, Record, Idx);
4476 QualType ClassType = readType(*Loc.F, Record, Idx);
4477 if (PointeeType.isNull() || ClassType.isNull())
4478 return QualType();
4479
4480 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
4481 }
4482
4483 case TYPE_CONSTANT_ARRAY: {
4484 QualType ElementType = readType(*Loc.F, Record, Idx);
4485 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4486 unsigned IndexTypeQuals = Record[2];
4487 unsigned Idx = 3;
4488 llvm::APInt Size = ReadAPInt(Record, Idx);
4489 return Context.getConstantArrayType(ElementType, Size,
4490 ASM, IndexTypeQuals);
4491 }
4492
4493 case TYPE_INCOMPLETE_ARRAY: {
4494 QualType ElementType = readType(*Loc.F, Record, Idx);
4495 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4496 unsigned IndexTypeQuals = Record[2];
4497 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
4498 }
4499
4500 case TYPE_VARIABLE_ARRAY: {
4501 QualType ElementType = readType(*Loc.F, Record, Idx);
4502 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4503 unsigned IndexTypeQuals = Record[2];
4504 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
4505 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
4506 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
4507 ASM, IndexTypeQuals,
4508 SourceRange(LBLoc, RBLoc));
4509 }
4510
4511 case TYPE_VECTOR: {
4512 if (Record.size() != 3) {
4513 Error("incorrect encoding of vector type in AST file");
4514 return QualType();
4515 }
4516
4517 QualType ElementType = readType(*Loc.F, Record, Idx);
4518 unsigned NumElements = Record[1];
4519 unsigned VecKind = Record[2];
4520 return Context.getVectorType(ElementType, NumElements,
4521 (VectorType::VectorKind)VecKind);
4522 }
4523
4524 case TYPE_EXT_VECTOR: {
4525 if (Record.size() != 3) {
4526 Error("incorrect encoding of extended vector type in AST file");
4527 return QualType();
4528 }
4529
4530 QualType ElementType = readType(*Loc.F, Record, Idx);
4531 unsigned NumElements = Record[1];
4532 return Context.getExtVectorType(ElementType, NumElements);
4533 }
4534
4535 case TYPE_FUNCTION_NO_PROTO: {
4536 if (Record.size() != 6) {
4537 Error("incorrect encoding of no-proto function type");
4538 return QualType();
4539 }
4540 QualType ResultType = readType(*Loc.F, Record, Idx);
4541 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
4542 (CallingConv)Record[4], Record[5]);
4543 return Context.getFunctionNoProtoType(ResultType, Info);
4544 }
4545
4546 case TYPE_FUNCTION_PROTO: {
4547 QualType ResultType = readType(*Loc.F, Record, Idx);
4548
4549 FunctionProtoType::ExtProtoInfo EPI;
4550 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
4551 /*hasregparm*/ Record[2],
4552 /*regparm*/ Record[3],
4553 static_cast<CallingConv>(Record[4]),
4554 /*produces*/ Record[5]);
4555
4556 unsigned Idx = 6;
4557 unsigned NumParams = Record[Idx++];
4558 SmallVector<QualType, 16> ParamTypes;
4559 for (unsigned I = 0; I != NumParams; ++I)
4560 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
4561
4562 EPI.Variadic = Record[Idx++];
4563 EPI.HasTrailingReturn = Record[Idx++];
4564 EPI.TypeQuals = Record[Idx++];
4565 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
4566 ExceptionSpecificationType EST =
4567 static_cast<ExceptionSpecificationType>(Record[Idx++]);
4568 EPI.ExceptionSpecType = EST;
4569 SmallVector<QualType, 2> Exceptions;
4570 if (EST == EST_Dynamic) {
4571 EPI.NumExceptions = Record[Idx++];
4572 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
4573 Exceptions.push_back(readType(*Loc.F, Record, Idx));
4574 EPI.Exceptions = Exceptions.data();
4575 } else if (EST == EST_ComputedNoexcept) {
4576 EPI.NoexceptExpr = ReadExpr(*Loc.F);
4577 } else if (EST == EST_Uninstantiated) {
4578 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
4579 EPI.ExceptionSpecTemplate = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
4580 } else if (EST == EST_Unevaluated) {
4581 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
4582 }
Jordan Rose5c382722013-03-08 21:51:21 +00004583 return Context.getFunctionType(ResultType, ParamTypes, EPI);
Guy Benyei11169dd2012-12-18 14:30:41 +00004584 }
4585
4586 case TYPE_UNRESOLVED_USING: {
4587 unsigned Idx = 0;
4588 return Context.getTypeDeclType(
4589 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
4590 }
4591
4592 case TYPE_TYPEDEF: {
4593 if (Record.size() != 2) {
4594 Error("incorrect encoding of typedef type");
4595 return QualType();
4596 }
4597 unsigned Idx = 0;
4598 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
4599 QualType Canonical = readType(*Loc.F, Record, Idx);
4600 if (!Canonical.isNull())
4601 Canonical = Context.getCanonicalType(Canonical);
4602 return Context.getTypedefType(Decl, Canonical);
4603 }
4604
4605 case TYPE_TYPEOF_EXPR:
4606 return Context.getTypeOfExprType(ReadExpr(*Loc.F));
4607
4608 case TYPE_TYPEOF: {
4609 if (Record.size() != 1) {
4610 Error("incorrect encoding of typeof(type) in AST file");
4611 return QualType();
4612 }
4613 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
4614 return Context.getTypeOfType(UnderlyingType);
4615 }
4616
4617 case TYPE_DECLTYPE: {
4618 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
4619 return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
4620 }
4621
4622 case TYPE_UNARY_TRANSFORM: {
4623 QualType BaseType = readType(*Loc.F, Record, Idx);
4624 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
4625 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
4626 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
4627 }
4628
4629 case TYPE_AUTO:
4630 return Context.getAutoType(readType(*Loc.F, Record, Idx));
4631
4632 case TYPE_RECORD: {
4633 if (Record.size() != 2) {
4634 Error("incorrect encoding of record type");
4635 return QualType();
4636 }
4637 unsigned Idx = 0;
4638 bool IsDependent = Record[Idx++];
4639 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
4640 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
4641 QualType T = Context.getRecordType(RD);
4642 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
4643 return T;
4644 }
4645
4646 case TYPE_ENUM: {
4647 if (Record.size() != 2) {
4648 Error("incorrect encoding of enum type");
4649 return QualType();
4650 }
4651 unsigned Idx = 0;
4652 bool IsDependent = Record[Idx++];
4653 QualType T
4654 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
4655 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
4656 return T;
4657 }
4658
4659 case TYPE_ATTRIBUTED: {
4660 if (Record.size() != 3) {
4661 Error("incorrect encoding of attributed type");
4662 return QualType();
4663 }
4664 QualType modifiedType = readType(*Loc.F, Record, Idx);
4665 QualType equivalentType = readType(*Loc.F, Record, Idx);
4666 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
4667 return Context.getAttributedType(kind, modifiedType, equivalentType);
4668 }
4669
4670 case TYPE_PAREN: {
4671 if (Record.size() != 1) {
4672 Error("incorrect encoding of paren type");
4673 return QualType();
4674 }
4675 QualType InnerType = readType(*Loc.F, Record, Idx);
4676 return Context.getParenType(InnerType);
4677 }
4678
4679 case TYPE_PACK_EXPANSION: {
4680 if (Record.size() != 2) {
4681 Error("incorrect encoding of pack expansion type");
4682 return QualType();
4683 }
4684 QualType Pattern = readType(*Loc.F, Record, Idx);
4685 if (Pattern.isNull())
4686 return QualType();
David Blaikie05785d12013-02-20 22:23:23 +00004687 Optional<unsigned> NumExpansions;
Guy Benyei11169dd2012-12-18 14:30:41 +00004688 if (Record[1])
4689 NumExpansions = Record[1] - 1;
4690 return Context.getPackExpansionType(Pattern, NumExpansions);
4691 }
4692
4693 case TYPE_ELABORATED: {
4694 unsigned Idx = 0;
4695 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
4696 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
4697 QualType NamedType = readType(*Loc.F, Record, Idx);
4698 return Context.getElaboratedType(Keyword, NNS, NamedType);
4699 }
4700
4701 case TYPE_OBJC_INTERFACE: {
4702 unsigned Idx = 0;
4703 ObjCInterfaceDecl *ItfD
4704 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
4705 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
4706 }
4707
4708 case TYPE_OBJC_OBJECT: {
4709 unsigned Idx = 0;
4710 QualType Base = readType(*Loc.F, Record, Idx);
4711 unsigned NumProtos = Record[Idx++];
4712 SmallVector<ObjCProtocolDecl*, 4> Protos;
4713 for (unsigned I = 0; I != NumProtos; ++I)
4714 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
4715 return Context.getObjCObjectType(Base, Protos.data(), NumProtos);
4716 }
4717
4718 case TYPE_OBJC_OBJECT_POINTER: {
4719 unsigned Idx = 0;
4720 QualType Pointee = readType(*Loc.F, Record, Idx);
4721 return Context.getObjCObjectPointerType(Pointee);
4722 }
4723
4724 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
4725 unsigned Idx = 0;
4726 QualType Parm = readType(*Loc.F, Record, Idx);
4727 QualType Replacement = readType(*Loc.F, Record, Idx);
4728 return
4729 Context.getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
4730 Replacement);
4731 }
4732
4733 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
4734 unsigned Idx = 0;
4735 QualType Parm = readType(*Loc.F, Record, Idx);
4736 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
4737 return Context.getSubstTemplateTypeParmPackType(
4738 cast<TemplateTypeParmType>(Parm),
4739 ArgPack);
4740 }
4741
4742 case TYPE_INJECTED_CLASS_NAME: {
4743 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
4744 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
4745 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
4746 // for AST reading, too much interdependencies.
4747 return
4748 QualType(new (Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
4749 }
4750
4751 case TYPE_TEMPLATE_TYPE_PARM: {
4752 unsigned Idx = 0;
4753 unsigned Depth = Record[Idx++];
4754 unsigned Index = Record[Idx++];
4755 bool Pack = Record[Idx++];
4756 TemplateTypeParmDecl *D
4757 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
4758 return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
4759 }
4760
4761 case TYPE_DEPENDENT_NAME: {
4762 unsigned Idx = 0;
4763 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
4764 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
4765 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
4766 QualType Canon = readType(*Loc.F, Record, Idx);
4767 if (!Canon.isNull())
4768 Canon = Context.getCanonicalType(Canon);
4769 return Context.getDependentNameType(Keyword, NNS, Name, Canon);
4770 }
4771
4772 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
4773 unsigned Idx = 0;
4774 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
4775 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
4776 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
4777 unsigned NumArgs = Record[Idx++];
4778 SmallVector<TemplateArgument, 8> Args;
4779 Args.reserve(NumArgs);
4780 while (NumArgs--)
4781 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
4782 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
4783 Args.size(), Args.data());
4784 }
4785
4786 case TYPE_DEPENDENT_SIZED_ARRAY: {
4787 unsigned Idx = 0;
4788
4789 // ArrayType
4790 QualType ElementType = readType(*Loc.F, Record, Idx);
4791 ArrayType::ArraySizeModifier ASM
4792 = (ArrayType::ArraySizeModifier)Record[Idx++];
4793 unsigned IndexTypeQuals = Record[Idx++];
4794
4795 // DependentSizedArrayType
4796 Expr *NumElts = ReadExpr(*Loc.F);
4797 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
4798
4799 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
4800 IndexTypeQuals, Brackets);
4801 }
4802
4803 case TYPE_TEMPLATE_SPECIALIZATION: {
4804 unsigned Idx = 0;
4805 bool IsDependent = Record[Idx++];
4806 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
4807 SmallVector<TemplateArgument, 8> Args;
4808 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
4809 QualType Underlying = readType(*Loc.F, Record, Idx);
4810 QualType T;
4811 if (Underlying.isNull())
4812 T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(),
4813 Args.size());
4814 else
4815 T = Context.getTemplateSpecializationType(Name, Args.data(),
4816 Args.size(), Underlying);
4817 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
4818 return T;
4819 }
4820
4821 case TYPE_ATOMIC: {
4822 if (Record.size() != 1) {
4823 Error("Incorrect encoding of atomic type");
4824 return QualType();
4825 }
4826 QualType ValueType = readType(*Loc.F, Record, Idx);
4827 return Context.getAtomicType(ValueType);
4828 }
4829 }
4830 llvm_unreachable("Invalid TypeCode!");
4831}
4832
4833class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
4834 ASTReader &Reader;
4835 ModuleFile &F;
4836 const ASTReader::RecordData &Record;
4837 unsigned &Idx;
4838
4839 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
4840 unsigned &I) {
4841 return Reader.ReadSourceLocation(F, R, I);
4842 }
4843
4844 template<typename T>
4845 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
4846 return Reader.ReadDeclAs<T>(F, Record, Idx);
4847 }
4848
4849public:
4850 TypeLocReader(ASTReader &Reader, ModuleFile &F,
4851 const ASTReader::RecordData &Record, unsigned &Idx)
4852 : Reader(Reader), F(F), Record(Record), Idx(Idx)
4853 { }
4854
4855 // We want compile-time assurance that we've enumerated all of
4856 // these, so unfortunately we have to declare them first, then
4857 // define them out-of-line.
4858#define ABSTRACT_TYPELOC(CLASS, PARENT)
4859#define TYPELOC(CLASS, PARENT) \
4860 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
4861#include "clang/AST/TypeLocNodes.def"
4862
4863 void VisitFunctionTypeLoc(FunctionTypeLoc);
4864 void VisitArrayTypeLoc(ArrayTypeLoc);
4865};
4866
4867void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
4868 // nothing to do
4869}
4870void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
4871 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
4872 if (TL.needsExtraLocalData()) {
4873 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
4874 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
4875 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
4876 TL.setModeAttr(Record[Idx++]);
4877 }
4878}
4879void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
4880 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4881}
4882void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
4883 TL.setStarLoc(ReadSourceLocation(Record, Idx));
4884}
4885void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
4886 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
4887}
4888void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
4889 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
4890}
4891void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
4892 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
4893}
4894void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
4895 TL.setStarLoc(ReadSourceLocation(Record, Idx));
4896 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
4897}
4898void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
4899 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
4900 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
4901 if (Record[Idx++])
4902 TL.setSizeExpr(Reader.ReadExpr(F));
4903 else
4904 TL.setSizeExpr(0);
4905}
4906void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
4907 VisitArrayTypeLoc(TL);
4908}
4909void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
4910 VisitArrayTypeLoc(TL);
4911}
4912void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
4913 VisitArrayTypeLoc(TL);
4914}
4915void TypeLocReader::VisitDependentSizedArrayTypeLoc(
4916 DependentSizedArrayTypeLoc TL) {
4917 VisitArrayTypeLoc(TL);
4918}
4919void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
4920 DependentSizedExtVectorTypeLoc TL) {
4921 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4922}
4923void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
4924 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4925}
4926void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
4927 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4928}
4929void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
4930 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
4931 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4932 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4933 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
4934 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
4935 TL.setArg(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
4936 }
4937}
4938void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
4939 VisitFunctionTypeLoc(TL);
4940}
4941void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
4942 VisitFunctionTypeLoc(TL);
4943}
4944void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
4945 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4946}
4947void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
4948 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4949}
4950void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
4951 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
4952 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4953 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4954}
4955void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
4956 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
4957 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4958 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4959 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
4960}
4961void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
4962 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4963}
4964void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
4965 TL.setKWLoc(ReadSourceLocation(Record, Idx));
4966 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4967 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4968 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
4969}
4970void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
4971 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4972}
4973void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
4974 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4975}
4976void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
4977 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4978}
4979void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
4980 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
4981 if (TL.hasAttrOperand()) {
4982 SourceRange range;
4983 range.setBegin(ReadSourceLocation(Record, Idx));
4984 range.setEnd(ReadSourceLocation(Record, Idx));
4985 TL.setAttrOperandParensRange(range);
4986 }
4987 if (TL.hasAttrExprOperand()) {
4988 if (Record[Idx++])
4989 TL.setAttrExprOperand(Reader.ReadExpr(F));
4990 else
4991 TL.setAttrExprOperand(0);
4992 } else if (TL.hasAttrEnumOperand())
4993 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
4994}
4995void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
4996 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4997}
4998void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
4999 SubstTemplateTypeParmTypeLoc TL) {
5000 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5001}
5002void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
5003 SubstTemplateTypeParmPackTypeLoc TL) {
5004 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5005}
5006void TypeLocReader::VisitTemplateSpecializationTypeLoc(
5007 TemplateSpecializationTypeLoc TL) {
5008 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
5009 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
5010 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5011 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5012 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
5013 TL.setArgLocInfo(i,
5014 Reader.GetTemplateArgumentLocInfo(F,
5015 TL.getTypePtr()->getArg(i).getKind(),
5016 Record, Idx));
5017}
5018void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
5019 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5020 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5021}
5022void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
5023 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5024 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5025}
5026void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
5027 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5028}
5029void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
5030 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5031 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5032 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5033}
5034void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
5035 DependentTemplateSpecializationTypeLoc TL) {
5036 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5037 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5038 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
5039 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
5040 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5041 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5042 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
5043 TL.setArgLocInfo(I,
5044 Reader.GetTemplateArgumentLocInfo(F,
5045 TL.getTypePtr()->getArg(I).getKind(),
5046 Record, Idx));
5047}
5048void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
5049 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
5050}
5051void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
5052 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5053}
5054void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
5055 TL.setHasBaseTypeAsWritten(Record[Idx++]);
5056 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5057 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5058 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
5059 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
5060}
5061void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
5062 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5063}
5064void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
5065 TL.setKWLoc(ReadSourceLocation(Record, Idx));
5066 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5067 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5068}
5069
5070TypeSourceInfo *ASTReader::GetTypeSourceInfo(ModuleFile &F,
5071 const RecordData &Record,
5072 unsigned &Idx) {
5073 QualType InfoTy = readType(F, Record, Idx);
5074 if (InfoTy.isNull())
5075 return 0;
5076
5077 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
5078 TypeLocReader TLR(*this, F, Record, Idx);
5079 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
5080 TLR.Visit(TL);
5081 return TInfo;
5082}
5083
5084QualType ASTReader::GetType(TypeID ID) {
5085 unsigned FastQuals = ID & Qualifiers::FastMask;
5086 unsigned Index = ID >> Qualifiers::FastWidth;
5087
5088 if (Index < NUM_PREDEF_TYPE_IDS) {
5089 QualType T;
5090 switch ((PredefinedTypeIDs)Index) {
5091 case PREDEF_TYPE_NULL_ID: return QualType();
5092 case PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
5093 case PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
5094
5095 case PREDEF_TYPE_CHAR_U_ID:
5096 case PREDEF_TYPE_CHAR_S_ID:
5097 // FIXME: Check that the signedness of CharTy is correct!
5098 T = Context.CharTy;
5099 break;
5100
5101 case PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
5102 case PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
5103 case PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
5104 case PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
5105 case PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
5106 case PREDEF_TYPE_UINT128_ID: T = Context.UnsignedInt128Ty; break;
5107 case PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
5108 case PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
5109 case PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
5110 case PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
5111 case PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
5112 case PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
5113 case PREDEF_TYPE_INT128_ID: T = Context.Int128Ty; break;
5114 case PREDEF_TYPE_HALF_ID: T = Context.HalfTy; break;
5115 case PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
5116 case PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
5117 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
5118 case PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
5119 case PREDEF_TYPE_BOUND_MEMBER: T = Context.BoundMemberTy; break;
5120 case PREDEF_TYPE_PSEUDO_OBJECT: T = Context.PseudoObjectTy; break;
5121 case PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
5122 case PREDEF_TYPE_UNKNOWN_ANY: T = Context.UnknownAnyTy; break;
5123 case PREDEF_TYPE_NULLPTR_ID: T = Context.NullPtrTy; break;
5124 case PREDEF_TYPE_CHAR16_ID: T = Context.Char16Ty; break;
5125 case PREDEF_TYPE_CHAR32_ID: T = Context.Char32Ty; break;
5126 case PREDEF_TYPE_OBJC_ID: T = Context.ObjCBuiltinIdTy; break;
5127 case PREDEF_TYPE_OBJC_CLASS: T = Context.ObjCBuiltinClassTy; break;
5128 case PREDEF_TYPE_OBJC_SEL: T = Context.ObjCBuiltinSelTy; break;
Guy Benyeid8a08ea2012-12-18 14:38:23 +00005129 case PREDEF_TYPE_IMAGE1D_ID: T = Context.OCLImage1dTy; break;
5130 case PREDEF_TYPE_IMAGE1D_ARR_ID: T = Context.OCLImage1dArrayTy; break;
5131 case PREDEF_TYPE_IMAGE1D_BUFF_ID: T = Context.OCLImage1dBufferTy; break;
5132 case PREDEF_TYPE_IMAGE2D_ID: T = Context.OCLImage2dTy; break;
5133 case PREDEF_TYPE_IMAGE2D_ARR_ID: T = Context.OCLImage2dArrayTy; break;
5134 case PREDEF_TYPE_IMAGE3D_ID: T = Context.OCLImage3dTy; break;
Guy Benyei61054192013-02-07 10:55:47 +00005135 case PREDEF_TYPE_SAMPLER_ID: T = Context.OCLSamplerTy; break;
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00005136 case PREDEF_TYPE_EVENT_ID: T = Context.OCLEventTy; break;
Guy Benyei11169dd2012-12-18 14:30:41 +00005137 case PREDEF_TYPE_AUTO_DEDUCT: T = Context.getAutoDeductType(); break;
5138
5139 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
5140 T = Context.getAutoRRefDeductType();
5141 break;
5142
5143 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
5144 T = Context.ARCUnbridgedCastTy;
5145 break;
5146
5147 case PREDEF_TYPE_VA_LIST_TAG:
5148 T = Context.getVaListTagType();
5149 break;
5150
5151 case PREDEF_TYPE_BUILTIN_FN:
5152 T = Context.BuiltinFnTy;
5153 break;
5154 }
5155
5156 assert(!T.isNull() && "Unknown predefined type");
5157 return T.withFastQualifiers(FastQuals);
5158 }
5159
5160 Index -= NUM_PREDEF_TYPE_IDS;
5161 assert(Index < TypesLoaded.size() && "Type index out-of-range");
5162 if (TypesLoaded[Index].isNull()) {
5163 TypesLoaded[Index] = readTypeRecord(Index);
5164 if (TypesLoaded[Index].isNull())
5165 return QualType();
5166
5167 TypesLoaded[Index]->setFromAST();
5168 if (DeserializationListener)
5169 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
5170 TypesLoaded[Index]);
5171 }
5172
5173 return TypesLoaded[Index].withFastQualifiers(FastQuals);
5174}
5175
5176QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
5177 return GetType(getGlobalTypeID(F, LocalID));
5178}
5179
5180serialization::TypeID
5181ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
5182 unsigned FastQuals = LocalID & Qualifiers::FastMask;
5183 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
5184
5185 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
5186 return LocalID;
5187
5188 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5189 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
5190 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
5191
5192 unsigned GlobalIndex = LocalIndex + I->second;
5193 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
5194}
5195
5196TemplateArgumentLocInfo
5197ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
5198 TemplateArgument::ArgKind Kind,
5199 const RecordData &Record,
5200 unsigned &Index) {
5201 switch (Kind) {
5202 case TemplateArgument::Expression:
5203 return ReadExpr(F);
5204 case TemplateArgument::Type:
5205 return GetTypeSourceInfo(F, Record, Index);
5206 case TemplateArgument::Template: {
5207 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
5208 Index);
5209 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
5210 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
5211 SourceLocation());
5212 }
5213 case TemplateArgument::TemplateExpansion: {
5214 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
5215 Index);
5216 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
5217 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
5218 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
5219 EllipsisLoc);
5220 }
5221 case TemplateArgument::Null:
5222 case TemplateArgument::Integral:
5223 case TemplateArgument::Declaration:
5224 case TemplateArgument::NullPtr:
5225 case TemplateArgument::Pack:
5226 // FIXME: Is this right?
5227 return TemplateArgumentLocInfo();
5228 }
5229 llvm_unreachable("unexpected template argument loc");
5230}
5231
5232TemplateArgumentLoc
5233ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
5234 const RecordData &Record, unsigned &Index) {
5235 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
5236
5237 if (Arg.getKind() == TemplateArgument::Expression) {
5238 if (Record[Index++]) // bool InfoHasSameExpr.
5239 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
5240 }
5241 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
5242 Record, Index));
5243}
5244
5245Decl *ASTReader::GetExternalDecl(uint32_t ID) {
5246 return GetDecl(ID);
5247}
5248
5249uint64_t ASTReader::readCXXBaseSpecifiers(ModuleFile &M, const RecordData &Record,
5250 unsigned &Idx){
5251 if (Idx >= Record.size())
5252 return 0;
5253
5254 unsigned LocalID = Record[Idx++];
5255 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
5256}
5257
5258CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
5259 RecordLocation Loc = getLocalBitOffset(Offset);
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005260 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00005261 SavedStreamPosition SavedPosition(Cursor);
5262 Cursor.JumpToBit(Loc.Offset);
5263 ReadingKindTracker ReadingKind(Read_Decl, *this);
5264 RecordData Record;
5265 unsigned Code = Cursor.ReadCode();
Chris Lattner0e6c9402013-01-20 02:38:54 +00005266 unsigned RecCode = Cursor.readRecord(Code, Record);
Guy Benyei11169dd2012-12-18 14:30:41 +00005267 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
5268 Error("Malformed AST file: missing C++ base specifiers");
5269 return 0;
5270 }
5271
5272 unsigned Idx = 0;
5273 unsigned NumBases = Record[Idx++];
5274 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
5275 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
5276 for (unsigned I = 0; I != NumBases; ++I)
5277 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
5278 return Bases;
5279}
5280
5281serialization::DeclID
5282ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
5283 if (LocalID < NUM_PREDEF_DECL_IDS)
5284 return LocalID;
5285
5286 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5287 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
5288 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
5289
5290 return LocalID + I->second;
5291}
5292
5293bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
5294 ModuleFile &M) const {
5295 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(ID);
5296 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5297 return &M == I->second;
5298}
5299
Douglas Gregor9f782892013-01-21 15:25:38 +00005300ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
Guy Benyei11169dd2012-12-18 14:30:41 +00005301 if (!D->isFromASTFile())
5302 return 0;
5303 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
5304 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5305 return I->second;
5306}
5307
5308SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
5309 if (ID < NUM_PREDEF_DECL_IDS)
5310 return SourceLocation();
5311
5312 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
5313
5314 if (Index > DeclsLoaded.size()) {
5315 Error("declaration ID out-of-range for AST file");
5316 return SourceLocation();
5317 }
5318
5319 if (Decl *D = DeclsLoaded[Index])
5320 return D->getLocation();
5321
5322 unsigned RawLocation = 0;
5323 RecordLocation Rec = DeclCursorForID(ID, RawLocation);
5324 return ReadSourceLocation(*Rec.F, RawLocation);
5325}
5326
5327Decl *ASTReader::GetDecl(DeclID ID) {
5328 if (ID < NUM_PREDEF_DECL_IDS) {
5329 switch ((PredefinedDeclIDs)ID) {
5330 case PREDEF_DECL_NULL_ID:
5331 return 0;
5332
5333 case PREDEF_DECL_TRANSLATION_UNIT_ID:
5334 return Context.getTranslationUnitDecl();
5335
5336 case PREDEF_DECL_OBJC_ID_ID:
5337 return Context.getObjCIdDecl();
5338
5339 case PREDEF_DECL_OBJC_SEL_ID:
5340 return Context.getObjCSelDecl();
5341
5342 case PREDEF_DECL_OBJC_CLASS_ID:
5343 return Context.getObjCClassDecl();
5344
5345 case PREDEF_DECL_OBJC_PROTOCOL_ID:
5346 return Context.getObjCProtocolDecl();
5347
5348 case PREDEF_DECL_INT_128_ID:
5349 return Context.getInt128Decl();
5350
5351 case PREDEF_DECL_UNSIGNED_INT_128_ID:
5352 return Context.getUInt128Decl();
5353
5354 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
5355 return Context.getObjCInstanceTypeDecl();
5356
5357 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
5358 return Context.getBuiltinVaListDecl();
5359 }
5360 }
5361
5362 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
5363
5364 if (Index >= DeclsLoaded.size()) {
5365 assert(0 && "declaration ID out-of-range for AST file");
5366 Error("declaration ID out-of-range for AST file");
5367 return 0;
5368 }
5369
5370 if (!DeclsLoaded[Index]) {
5371 ReadDeclRecord(ID);
5372 if (DeserializationListener)
5373 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
5374 }
5375
5376 return DeclsLoaded[Index];
5377}
5378
5379DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
5380 DeclID GlobalID) {
5381 if (GlobalID < NUM_PREDEF_DECL_IDS)
5382 return GlobalID;
5383
5384 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
5385 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5386 ModuleFile *Owner = I->second;
5387
5388 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
5389 = M.GlobalToLocalDeclIDs.find(Owner);
5390 if (Pos == M.GlobalToLocalDeclIDs.end())
5391 return 0;
5392
5393 return GlobalID - Owner->BaseDeclID + Pos->second;
5394}
5395
5396serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
5397 const RecordData &Record,
5398 unsigned &Idx) {
5399 if (Idx >= Record.size()) {
5400 Error("Corrupted AST file");
5401 return 0;
5402 }
5403
5404 return getGlobalDeclID(F, Record[Idx++]);
5405}
5406
5407/// \brief Resolve the offset of a statement into a statement.
5408///
5409/// This operation will read a new statement from the external
5410/// source each time it is called, and is meant to be used via a
5411/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
5412Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
5413 // Switch case IDs are per Decl.
5414 ClearSwitchCaseIDs();
5415
5416 // Offset here is a global offset across the entire chain.
5417 RecordLocation Loc = getLocalBitOffset(Offset);
5418 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
5419 return ReadStmtFromStream(*Loc.F);
5420}
5421
5422namespace {
5423 class FindExternalLexicalDeclsVisitor {
5424 ASTReader &Reader;
5425 const DeclContext *DC;
5426 bool (*isKindWeWant)(Decl::Kind);
5427
5428 SmallVectorImpl<Decl*> &Decls;
5429 bool PredefsVisited[NUM_PREDEF_DECL_IDS];
5430
5431 public:
5432 FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC,
5433 bool (*isKindWeWant)(Decl::Kind),
5434 SmallVectorImpl<Decl*> &Decls)
5435 : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls)
5436 {
5437 for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I)
5438 PredefsVisited[I] = false;
5439 }
5440
5441 static bool visit(ModuleFile &M, bool Preorder, void *UserData) {
5442 if (Preorder)
5443 return false;
5444
5445 FindExternalLexicalDeclsVisitor *This
5446 = static_cast<FindExternalLexicalDeclsVisitor *>(UserData);
5447
5448 ModuleFile::DeclContextInfosMap::iterator Info
5449 = M.DeclContextInfos.find(This->DC);
5450 if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls)
5451 return false;
5452
5453 // Load all of the declaration IDs
5454 for (const KindDeclIDPair *ID = Info->second.LexicalDecls,
5455 *IDE = ID + Info->second.NumLexicalDecls;
5456 ID != IDE; ++ID) {
5457 if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first))
5458 continue;
5459
5460 // Don't add predefined declarations to the lexical context more
5461 // than once.
5462 if (ID->second < NUM_PREDEF_DECL_IDS) {
5463 if (This->PredefsVisited[ID->second])
5464 continue;
5465
5466 This->PredefsVisited[ID->second] = true;
5467 }
5468
5469 if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) {
5470 if (!This->DC->isDeclInLexicalTraversal(D))
5471 This->Decls.push_back(D);
5472 }
5473 }
5474
5475 return false;
5476 }
5477 };
5478}
5479
5480ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
5481 bool (*isKindWeWant)(Decl::Kind),
5482 SmallVectorImpl<Decl*> &Decls) {
5483 // There might be lexical decls in multiple modules, for the TU at
5484 // least. Walk all of the modules in the order they were loaded.
5485 FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls);
5486 ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor);
5487 ++NumLexicalDeclContextsRead;
5488 return ELR_Success;
5489}
5490
5491namespace {
5492
5493class DeclIDComp {
5494 ASTReader &Reader;
5495 ModuleFile &Mod;
5496
5497public:
5498 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
5499
5500 bool operator()(LocalDeclID L, LocalDeclID R) const {
5501 SourceLocation LHS = getLocation(L);
5502 SourceLocation RHS = getLocation(R);
5503 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5504 }
5505
5506 bool operator()(SourceLocation LHS, LocalDeclID R) const {
5507 SourceLocation RHS = getLocation(R);
5508 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5509 }
5510
5511 bool operator()(LocalDeclID L, SourceLocation RHS) const {
5512 SourceLocation LHS = getLocation(L);
5513 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5514 }
5515
5516 SourceLocation getLocation(LocalDeclID ID) const {
5517 return Reader.getSourceManager().getFileLoc(
5518 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
5519 }
5520};
5521
5522}
5523
5524void ASTReader::FindFileRegionDecls(FileID File,
5525 unsigned Offset, unsigned Length,
5526 SmallVectorImpl<Decl *> &Decls) {
5527 SourceManager &SM = getSourceManager();
5528
5529 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
5530 if (I == FileDeclIDs.end())
5531 return;
5532
5533 FileDeclsInfo &DInfo = I->second;
5534 if (DInfo.Decls.empty())
5535 return;
5536
5537 SourceLocation
5538 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
5539 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
5540
5541 DeclIDComp DIDComp(*this, *DInfo.Mod);
5542 ArrayRef<serialization::LocalDeclID>::iterator
5543 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
5544 BeginLoc, DIDComp);
5545 if (BeginIt != DInfo.Decls.begin())
5546 --BeginIt;
5547
5548 // If we are pointing at a top-level decl inside an objc container, we need
5549 // to backtrack until we find it otherwise we will fail to report that the
5550 // region overlaps with an objc container.
5551 while (BeginIt != DInfo.Decls.begin() &&
5552 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
5553 ->isTopLevelDeclInObjCContainer())
5554 --BeginIt;
5555
5556 ArrayRef<serialization::LocalDeclID>::iterator
5557 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
5558 EndLoc, DIDComp);
5559 if (EndIt != DInfo.Decls.end())
5560 ++EndIt;
5561
5562 for (ArrayRef<serialization::LocalDeclID>::iterator
5563 DIt = BeginIt; DIt != EndIt; ++DIt)
5564 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
5565}
5566
5567namespace {
5568 /// \brief ModuleFile visitor used to perform name lookup into a
5569 /// declaration context.
5570 class DeclContextNameLookupVisitor {
5571 ASTReader &Reader;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00005572 SmallVectorImpl<const DeclContext *> &Contexts;
Guy Benyei11169dd2012-12-18 14:30:41 +00005573 DeclarationName Name;
5574 SmallVectorImpl<NamedDecl *> &Decls;
5575
5576 public:
5577 DeclContextNameLookupVisitor(ASTReader &Reader,
5578 SmallVectorImpl<const DeclContext *> &Contexts,
5579 DeclarationName Name,
5580 SmallVectorImpl<NamedDecl *> &Decls)
5581 : Reader(Reader), Contexts(Contexts), Name(Name), Decls(Decls) { }
5582
5583 static bool visit(ModuleFile &M, void *UserData) {
5584 DeclContextNameLookupVisitor *This
5585 = static_cast<DeclContextNameLookupVisitor *>(UserData);
5586
5587 // Check whether we have any visible declaration information for
5588 // this context in this module.
5589 ModuleFile::DeclContextInfosMap::iterator Info;
5590 bool FoundInfo = false;
5591 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5592 Info = M.DeclContextInfos.find(This->Contexts[I]);
5593 if (Info != M.DeclContextInfos.end() &&
5594 Info->second.NameLookupTableData) {
5595 FoundInfo = true;
5596 break;
5597 }
5598 }
5599
5600 if (!FoundInfo)
5601 return false;
5602
5603 // Look for this name within this module.
5604 ASTDeclContextNameLookupTable *LookupTable =
5605 Info->second.NameLookupTableData;
5606 ASTDeclContextNameLookupTable::iterator Pos
5607 = LookupTable->find(This->Name);
5608 if (Pos == LookupTable->end())
5609 return false;
5610
5611 bool FoundAnything = false;
5612 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
5613 for (; Data.first != Data.second; ++Data.first) {
5614 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first);
5615 if (!ND)
5616 continue;
5617
5618 if (ND->getDeclName() != This->Name) {
5619 // A name might be null because the decl's redeclarable part is
5620 // currently read before reading its name. The lookup is triggered by
5621 // building that decl (likely indirectly), and so it is later in the
5622 // sense of "already existing" and can be ignored here.
5623 continue;
5624 }
5625
5626 // Record this declaration.
5627 FoundAnything = true;
5628 This->Decls.push_back(ND);
5629 }
5630
5631 return FoundAnything;
5632 }
5633 };
5634}
5635
Douglas Gregor9f782892013-01-21 15:25:38 +00005636/// \brief Retrieve the "definitive" module file for the definition of the
5637/// given declaration context, if there is one.
5638///
5639/// The "definitive" module file is the only place where we need to look to
5640/// find information about the declarations within the given declaration
5641/// context. For example, C++ and Objective-C classes, C structs/unions, and
5642/// Objective-C protocols, categories, and extensions are all defined in a
5643/// single place in the source code, so they have definitive module files
5644/// associated with them. C++ namespaces, on the other hand, can have
5645/// definitions in multiple different module files.
5646///
5647/// Note: this needs to be kept in sync with ASTWriter::AddedVisibleDecl's
5648/// NDEBUG checking.
5649static ModuleFile *getDefinitiveModuleFileFor(const DeclContext *DC,
5650 ASTReader &Reader) {
Douglas Gregor7a6e2002013-01-22 17:08:30 +00005651 if (const DeclContext *DefDC = getDefinitiveDeclContext(DC))
5652 return Reader.getOwningModuleFile(cast<Decl>(DefDC));
Douglas Gregor9f782892013-01-21 15:25:38 +00005653
5654 return 0;
5655}
5656
Richard Smith9ce12e32013-02-07 03:30:24 +00005657bool
Guy Benyei11169dd2012-12-18 14:30:41 +00005658ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
5659 DeclarationName Name) {
5660 assert(DC->hasExternalVisibleStorage() &&
5661 "DeclContext has no visible decls in storage");
5662 if (!Name)
Richard Smith9ce12e32013-02-07 03:30:24 +00005663 return false;
Guy Benyei11169dd2012-12-18 14:30:41 +00005664
5665 SmallVector<NamedDecl *, 64> Decls;
5666
5667 // Compute the declaration contexts we need to look into. Multiple such
5668 // declaration contexts occur when two declaration contexts from disjoint
5669 // modules get merged, e.g., when two namespaces with the same name are
5670 // independently defined in separate modules.
5671 SmallVector<const DeclContext *, 2> Contexts;
5672 Contexts.push_back(DC);
5673
5674 if (DC->isNamespace()) {
5675 MergedDeclsMap::iterator Merged
5676 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5677 if (Merged != MergedDecls.end()) {
5678 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5679 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5680 }
5681 }
5682
5683 DeclContextNameLookupVisitor Visitor(*this, Contexts, Name, Decls);
Douglas Gregor9f782892013-01-21 15:25:38 +00005684
5685 // If we can definitively determine which module file to look into,
5686 // only look there. Otherwise, look in all module files.
5687 ModuleFile *Definitive;
5688 if (Contexts.size() == 1 &&
5689 (Definitive = getDefinitiveModuleFileFor(DC, *this))) {
5690 DeclContextNameLookupVisitor::visit(*Definitive, &Visitor);
5691 } else {
5692 ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor);
5693 }
Guy Benyei11169dd2012-12-18 14:30:41 +00005694 ++NumVisibleDeclContextsRead;
5695 SetExternalVisibleDeclsForName(DC, Name, Decls);
Richard Smith9ce12e32013-02-07 03:30:24 +00005696 return !Decls.empty();
Guy Benyei11169dd2012-12-18 14:30:41 +00005697}
5698
5699namespace {
5700 /// \brief ModuleFile visitor used to retrieve all visible names in a
5701 /// declaration context.
5702 class DeclContextAllNamesVisitor {
5703 ASTReader &Reader;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00005704 SmallVectorImpl<const DeclContext *> &Contexts;
Guy Benyei11169dd2012-12-18 14:30:41 +00005705 llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> > &Decls;
Argyrios Kyrtzidis2810e9d2012-12-19 22:21:18 +00005706 bool VisitAll;
Guy Benyei11169dd2012-12-18 14:30:41 +00005707
5708 public:
5709 DeclContextAllNamesVisitor(ASTReader &Reader,
5710 SmallVectorImpl<const DeclContext *> &Contexts,
5711 llvm::DenseMap<DeclarationName,
Argyrios Kyrtzidis2810e9d2012-12-19 22:21:18 +00005712 SmallVector<NamedDecl *, 8> > &Decls,
5713 bool VisitAll)
5714 : Reader(Reader), Contexts(Contexts), Decls(Decls), VisitAll(VisitAll) { }
Guy Benyei11169dd2012-12-18 14:30:41 +00005715
5716 static bool visit(ModuleFile &M, void *UserData) {
5717 DeclContextAllNamesVisitor *This
5718 = static_cast<DeclContextAllNamesVisitor *>(UserData);
5719
5720 // Check whether we have any visible declaration information for
5721 // this context in this module.
5722 ModuleFile::DeclContextInfosMap::iterator Info;
5723 bool FoundInfo = false;
5724 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5725 Info = M.DeclContextInfos.find(This->Contexts[I]);
5726 if (Info != M.DeclContextInfos.end() &&
5727 Info->second.NameLookupTableData) {
5728 FoundInfo = true;
5729 break;
5730 }
5731 }
5732
5733 if (!FoundInfo)
5734 return false;
5735
5736 ASTDeclContextNameLookupTable *LookupTable =
5737 Info->second.NameLookupTableData;
5738 bool FoundAnything = false;
5739 for (ASTDeclContextNameLookupTable::data_iterator
Douglas Gregor5e306b12013-01-23 22:38:11 +00005740 I = LookupTable->data_begin(), E = LookupTable->data_end();
5741 I != E;
5742 ++I) {
Guy Benyei11169dd2012-12-18 14:30:41 +00005743 ASTDeclContextNameLookupTrait::data_type Data = *I;
5744 for (; Data.first != Data.second; ++Data.first) {
5745 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M,
5746 *Data.first);
5747 if (!ND)
5748 continue;
5749
5750 // Record this declaration.
5751 FoundAnything = true;
5752 This->Decls[ND->getDeclName()].push_back(ND);
5753 }
5754 }
5755
Argyrios Kyrtzidis2810e9d2012-12-19 22:21:18 +00005756 return FoundAnything && !This->VisitAll;
Guy Benyei11169dd2012-12-18 14:30:41 +00005757 }
5758 };
5759}
5760
5761void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
5762 if (!DC->hasExternalVisibleStorage())
5763 return;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00005764 llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> > Decls;
Guy Benyei11169dd2012-12-18 14:30:41 +00005765
5766 // Compute the declaration contexts we need to look into. Multiple such
5767 // declaration contexts occur when two declaration contexts from disjoint
5768 // modules get merged, e.g., when two namespaces with the same name are
5769 // independently defined in separate modules.
5770 SmallVector<const DeclContext *, 2> Contexts;
5771 Contexts.push_back(DC);
5772
5773 if (DC->isNamespace()) {
5774 MergedDeclsMap::iterator Merged
5775 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5776 if (Merged != MergedDecls.end()) {
5777 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5778 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5779 }
5780 }
5781
Argyrios Kyrtzidis2810e9d2012-12-19 22:21:18 +00005782 DeclContextAllNamesVisitor Visitor(*this, Contexts, Decls,
5783 /*VisitAll=*/DC->isFileContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00005784 ModuleMgr.visit(&DeclContextAllNamesVisitor::visit, &Visitor);
5785 ++NumVisibleDeclContextsRead;
5786
5787 for (llvm::DenseMap<DeclarationName,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00005788 SmallVector<NamedDecl *, 8> >::iterator
Guy Benyei11169dd2012-12-18 14:30:41 +00005789 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
5790 SetExternalVisibleDeclsForName(DC, I->first, I->second);
5791 }
5792 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
5793}
5794
5795/// \brief Under non-PCH compilation the consumer receives the objc methods
5796/// before receiving the implementation, and codegen depends on this.
5797/// We simulate this by deserializing and passing to consumer the methods of the
5798/// implementation before passing the deserialized implementation decl.
5799static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
5800 ASTConsumer *Consumer) {
5801 assert(ImplD && Consumer);
5802
5803 for (ObjCImplDecl::method_iterator
5804 I = ImplD->meth_begin(), E = ImplD->meth_end(); I != E; ++I)
5805 Consumer->HandleInterestingDecl(DeclGroupRef(*I));
5806
5807 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
5808}
5809
5810void ASTReader::PassInterestingDeclsToConsumer() {
5811 assert(Consumer);
5812 while (!InterestingDecls.empty()) {
5813 Decl *D = InterestingDecls.front();
5814 InterestingDecls.pop_front();
5815
5816 PassInterestingDeclToConsumer(D);
5817 }
5818}
5819
5820void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
5821 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
5822 PassObjCImplDeclToConsumer(ImplD, Consumer);
5823 else
5824 Consumer->HandleInterestingDecl(DeclGroupRef(D));
5825}
5826
5827void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
5828 this->Consumer = Consumer;
5829
5830 if (!Consumer)
5831 return;
5832
5833 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
5834 // Force deserialization of this decl, which will cause it to be queued for
5835 // passing to the consumer.
5836 GetDecl(ExternalDefinitions[I]);
5837 }
5838 ExternalDefinitions.clear();
5839
5840 PassInterestingDeclsToConsumer();
5841}
5842
5843void ASTReader::PrintStats() {
5844 std::fprintf(stderr, "*** AST File Statistics:\n");
5845
5846 unsigned NumTypesLoaded
5847 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
5848 QualType());
5849 unsigned NumDeclsLoaded
5850 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
5851 (Decl *)0);
5852 unsigned NumIdentifiersLoaded
5853 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
5854 IdentifiersLoaded.end(),
5855 (IdentifierInfo *)0);
5856 unsigned NumMacrosLoaded
5857 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
5858 MacrosLoaded.end(),
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005859 (MacroInfo *)0);
Guy Benyei11169dd2012-12-18 14:30:41 +00005860 unsigned NumSelectorsLoaded
5861 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
5862 SelectorsLoaded.end(),
5863 Selector());
5864
5865 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
5866 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
5867 NumSLocEntriesRead, TotalNumSLocEntries,
5868 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
5869 if (!TypesLoaded.empty())
5870 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
5871 NumTypesLoaded, (unsigned)TypesLoaded.size(),
5872 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
5873 if (!DeclsLoaded.empty())
5874 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
5875 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
5876 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
5877 if (!IdentifiersLoaded.empty())
5878 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
5879 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
5880 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
5881 if (!MacrosLoaded.empty())
5882 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
5883 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
5884 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
5885 if (!SelectorsLoaded.empty())
5886 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
5887 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
5888 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
5889 if (TotalNumStatements)
5890 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
5891 NumStatementsRead, TotalNumStatements,
5892 ((float)NumStatementsRead/TotalNumStatements * 100));
5893 if (TotalNumMacros)
5894 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
5895 NumMacrosRead, TotalNumMacros,
5896 ((float)NumMacrosRead/TotalNumMacros * 100));
5897 if (TotalLexicalDeclContexts)
5898 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
5899 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
5900 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
5901 * 100));
5902 if (TotalVisibleDeclContexts)
5903 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
5904 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
5905 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
5906 * 100));
5907 if (TotalNumMethodPoolEntries) {
5908 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
5909 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
5910 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
5911 * 100));
Guy Benyei11169dd2012-12-18 14:30:41 +00005912 }
Douglas Gregorad2f7a52013-01-28 17:54:36 +00005913 if (NumMethodPoolLookups) {
5914 std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n",
5915 NumMethodPoolHits, NumMethodPoolLookups,
5916 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
5917 }
5918 if (NumMethodPoolTableLookups) {
5919 std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n",
5920 NumMethodPoolTableHits, NumMethodPoolTableLookups,
5921 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
5922 * 100.0));
5923 }
5924
Douglas Gregor00a50f72013-01-25 00:38:33 +00005925 if (NumIdentifierLookupHits) {
5926 std::fprintf(stderr,
5927 " %u / %u identifier table lookups succeeded (%f%%)\n",
5928 NumIdentifierLookupHits, NumIdentifierLookups,
5929 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
5930 }
5931
Douglas Gregore060e572013-01-25 01:03:03 +00005932 if (GlobalIndex) {
5933 std::fprintf(stderr, "\n");
5934 GlobalIndex->printStats();
5935 }
5936
Guy Benyei11169dd2012-12-18 14:30:41 +00005937 std::fprintf(stderr, "\n");
5938 dump();
5939 std::fprintf(stderr, "\n");
5940}
5941
5942template<typename Key, typename ModuleFile, unsigned InitialCapacity>
5943static void
5944dumpModuleIDMap(StringRef Name,
5945 const ContinuousRangeMap<Key, ModuleFile *,
5946 InitialCapacity> &Map) {
5947 if (Map.begin() == Map.end())
5948 return;
5949
5950 typedef ContinuousRangeMap<Key, ModuleFile *, InitialCapacity> MapType;
5951 llvm::errs() << Name << ":\n";
5952 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
5953 I != IEnd; ++I) {
5954 llvm::errs() << " " << I->first << " -> " << I->second->FileName
5955 << "\n";
5956 }
5957}
5958
5959void ASTReader::dump() {
5960 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
5961 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
5962 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
5963 dumpModuleIDMap("Global type map", GlobalTypeMap);
5964 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
5965 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
5966 dumpModuleIDMap("Global macro map", GlobalMacroMap);
5967 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
5968 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
5969 dumpModuleIDMap("Global preprocessed entity map",
5970 GlobalPreprocessedEntityMap);
5971
5972 llvm::errs() << "\n*** PCH/Modules Loaded:";
5973 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
5974 MEnd = ModuleMgr.end();
5975 M != MEnd; ++M)
5976 (*M)->dump();
5977}
5978
5979/// Return the amount of memory used by memory buffers, breaking down
5980/// by heap-backed versus mmap'ed memory.
5981void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
5982 for (ModuleConstIterator I = ModuleMgr.begin(),
5983 E = ModuleMgr.end(); I != E; ++I) {
5984 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
5985 size_t bytes = buf->getBufferSize();
5986 switch (buf->getBufferKind()) {
5987 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
5988 sizes.malloc_bytes += bytes;
5989 break;
5990 case llvm::MemoryBuffer::MemoryBuffer_MMap:
5991 sizes.mmap_bytes += bytes;
5992 break;
5993 }
5994 }
5995 }
5996}
5997
5998void ASTReader::InitializeSema(Sema &S) {
5999 SemaObj = &S;
6000 S.addExternalSource(this);
6001
6002 // Makes sure any declarations that were deserialized "too early"
6003 // still get added to the identifier's declaration chains.
6004 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
Douglas Gregor6168bd22013-02-18 15:53:43 +00006005 NamedDecl *ND = cast<NamedDecl>(PreloadedDecls[I]->getMostRecentDecl());
6006 SemaObj->pushExternalDeclIntoScope(ND, PreloadedDecls[I]->getDeclName());
Guy Benyei11169dd2012-12-18 14:30:41 +00006007 }
6008 PreloadedDecls.clear();
6009
6010 // Load the offsets of the declarations that Sema references.
6011 // They will be lazily deserialized when needed.
6012 if (!SemaDeclRefs.empty()) {
6013 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
6014 if (!SemaObj->StdNamespace)
6015 SemaObj->StdNamespace = SemaDeclRefs[0];
6016 if (!SemaObj->StdBadAlloc)
6017 SemaObj->StdBadAlloc = SemaDeclRefs[1];
6018 }
6019
6020 if (!FPPragmaOptions.empty()) {
6021 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
6022 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
6023 }
6024
6025 if (!OpenCLExtensions.empty()) {
6026 unsigned I = 0;
6027#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
6028#include "clang/Basic/OpenCLExtensions.def"
6029
6030 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
6031 }
6032}
6033
6034IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
6035 // Note that we are loading an identifier.
6036 Deserializing AnIdentifier(this);
Douglas Gregore060e572013-01-25 01:03:03 +00006037 StringRef Name(NameStart, NameEnd - NameStart);
6038
6039 // If there is a global index, look there first to determine which modules
6040 // provably do not have any results for this identifier.
Douglas Gregor7211ac12013-01-25 23:32:03 +00006041 GlobalModuleIndex::HitSet Hits;
6042 GlobalModuleIndex::HitSet *HitsPtr = 0;
Douglas Gregore060e572013-01-25 01:03:03 +00006043 if (!loadGlobalIndex()) {
Douglas Gregor7211ac12013-01-25 23:32:03 +00006044 if (GlobalIndex->lookupIdentifier(Name, Hits)) {
6045 HitsPtr = &Hits;
Douglas Gregore060e572013-01-25 01:03:03 +00006046 }
6047 }
Douglas Gregor7211ac12013-01-25 23:32:03 +00006048 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
Douglas Gregor00a50f72013-01-25 00:38:33 +00006049 NumIdentifierLookups,
6050 NumIdentifierLookupHits);
Douglas Gregor7211ac12013-01-25 23:32:03 +00006051 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor, HitsPtr);
Guy Benyei11169dd2012-12-18 14:30:41 +00006052 IdentifierInfo *II = Visitor.getIdentifierInfo();
6053 markIdentifierUpToDate(II);
6054 return II;
6055}
6056
6057namespace clang {
6058 /// \brief An identifier-lookup iterator that enumerates all of the
6059 /// identifiers stored within a set of AST files.
6060 class ASTIdentifierIterator : public IdentifierIterator {
6061 /// \brief The AST reader whose identifiers are being enumerated.
6062 const ASTReader &Reader;
6063
6064 /// \brief The current index into the chain of AST files stored in
6065 /// the AST reader.
6066 unsigned Index;
6067
6068 /// \brief The current position within the identifier lookup table
6069 /// of the current AST file.
6070 ASTIdentifierLookupTable::key_iterator Current;
6071
6072 /// \brief The end position within the identifier lookup table of
6073 /// the current AST file.
6074 ASTIdentifierLookupTable::key_iterator End;
6075
6076 public:
6077 explicit ASTIdentifierIterator(const ASTReader &Reader);
6078
6079 virtual StringRef Next();
6080 };
6081}
6082
6083ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
6084 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
6085 ASTIdentifierLookupTable *IdTable
6086 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
6087 Current = IdTable->key_begin();
6088 End = IdTable->key_end();
6089}
6090
6091StringRef ASTIdentifierIterator::Next() {
6092 while (Current == End) {
6093 // If we have exhausted all of our AST files, we're done.
6094 if (Index == 0)
6095 return StringRef();
6096
6097 --Index;
6098 ASTIdentifierLookupTable *IdTable
6099 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
6100 IdentifierLookupTable;
6101 Current = IdTable->key_begin();
6102 End = IdTable->key_end();
6103 }
6104
6105 // We have any identifiers remaining in the current AST file; return
6106 // the next one.
Douglas Gregorbfd73d72013-01-23 18:53:14 +00006107 StringRef Result = *Current;
Guy Benyei11169dd2012-12-18 14:30:41 +00006108 ++Current;
Douglas Gregorbfd73d72013-01-23 18:53:14 +00006109 return Result;
Guy Benyei11169dd2012-12-18 14:30:41 +00006110}
6111
Argyrios Kyrtzidis9aca3c62013-04-17 22:10:55 +00006112IdentifierIterator *ASTReader::getIdentifiers() {
6113 if (!loadGlobalIndex())
6114 return GlobalIndex->createIdentifierIterator();
6115
Guy Benyei11169dd2012-12-18 14:30:41 +00006116 return new ASTIdentifierIterator(*this);
6117}
6118
6119namespace clang { namespace serialization {
6120 class ReadMethodPoolVisitor {
6121 ASTReader &Reader;
6122 Selector Sel;
6123 unsigned PriorGeneration;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00006124 unsigned InstanceBits;
6125 unsigned FactoryBits;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00006126 SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
6127 SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
Guy Benyei11169dd2012-12-18 14:30:41 +00006128
6129 public:
6130 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
6131 unsigned PriorGeneration)
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00006132 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration),
6133 InstanceBits(0), FactoryBits(0) { }
Guy Benyei11169dd2012-12-18 14:30:41 +00006134
6135 static bool visit(ModuleFile &M, void *UserData) {
6136 ReadMethodPoolVisitor *This
6137 = static_cast<ReadMethodPoolVisitor *>(UserData);
6138
6139 if (!M.SelectorLookupTable)
6140 return false;
6141
6142 // If we've already searched this module file, skip it now.
6143 if (M.Generation <= This->PriorGeneration)
6144 return true;
6145
Douglas Gregorad2f7a52013-01-28 17:54:36 +00006146 ++This->Reader.NumMethodPoolTableLookups;
Guy Benyei11169dd2012-12-18 14:30:41 +00006147 ASTSelectorLookupTable *PoolTable
6148 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
6149 ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel);
6150 if (Pos == PoolTable->end())
6151 return false;
Douglas Gregorad2f7a52013-01-28 17:54:36 +00006152
6153 ++This->Reader.NumMethodPoolTableHits;
Guy Benyei11169dd2012-12-18 14:30:41 +00006154 ++This->Reader.NumSelectorsRead;
6155 // FIXME: Not quite happy with the statistics here. We probably should
6156 // disable this tracking when called via LoadSelector.
6157 // Also, should entries without methods count as misses?
6158 ++This->Reader.NumMethodPoolEntriesRead;
6159 ASTSelectorLookupTrait::data_type Data = *Pos;
6160 if (This->Reader.DeserializationListener)
6161 This->Reader.DeserializationListener->SelectorRead(Data.ID,
6162 This->Sel);
6163
6164 This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
6165 This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00006166 This->InstanceBits = Data.InstanceBits;
6167 This->FactoryBits = Data.FactoryBits;
Guy Benyei11169dd2012-12-18 14:30:41 +00006168 return true;
6169 }
6170
6171 /// \brief Retrieve the instance methods found by this visitor.
6172 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
6173 return InstanceMethods;
6174 }
6175
6176 /// \brief Retrieve the instance methods found by this visitor.
6177 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
6178 return FactoryMethods;
6179 }
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00006180
6181 unsigned getInstanceBits() const { return InstanceBits; }
6182 unsigned getFactoryBits() const { return FactoryBits; }
Guy Benyei11169dd2012-12-18 14:30:41 +00006183 };
6184} } // end namespace clang::serialization
6185
6186/// \brief Add the given set of methods to the method list.
6187static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
6188 ObjCMethodList &List) {
6189 for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
6190 S.addMethodToGlobalList(&List, Methods[I]);
6191 }
6192}
6193
6194void ASTReader::ReadMethodPool(Selector Sel) {
6195 // Get the selector generation and update it to the current generation.
6196 unsigned &Generation = SelectorGeneration[Sel];
6197 unsigned PriorGeneration = Generation;
6198 Generation = CurrentGeneration;
6199
6200 // Search for methods defined with this selector.
Douglas Gregorad2f7a52013-01-28 17:54:36 +00006201 ++NumMethodPoolLookups;
Guy Benyei11169dd2012-12-18 14:30:41 +00006202 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
6203 ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor);
6204
6205 if (Visitor.getInstanceMethods().empty() &&
Douglas Gregorad2f7a52013-01-28 17:54:36 +00006206 Visitor.getFactoryMethods().empty())
Guy Benyei11169dd2012-12-18 14:30:41 +00006207 return;
Douglas Gregorad2f7a52013-01-28 17:54:36 +00006208
6209 ++NumMethodPoolHits;
6210
Guy Benyei11169dd2012-12-18 14:30:41 +00006211 if (!getSema())
6212 return;
6213
6214 Sema &S = *getSema();
6215 Sema::GlobalMethodPool::iterator Pos
6216 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
6217
6218 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
6219 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00006220 Pos->second.first.setBits(Visitor.getInstanceBits());
6221 Pos->second.second.setBits(Visitor.getFactoryBits());
Guy Benyei11169dd2012-12-18 14:30:41 +00006222}
6223
6224void ASTReader::ReadKnownNamespaces(
6225 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
6226 Namespaces.clear();
6227
6228 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
6229 if (NamespaceDecl *Namespace
6230 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
6231 Namespaces.push_back(Namespace);
6232 }
6233}
6234
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00006235void ASTReader::ReadUndefinedButUsed(
Nick Lewyckyf0f56162013-01-31 03:23:57 +00006236 llvm::DenseMap<NamedDecl*, SourceLocation> &Undefined) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00006237 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
6238 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
Nick Lewycky8334af82013-01-26 00:35:08 +00006239 SourceLocation Loc =
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00006240 SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
Nick Lewycky8334af82013-01-26 00:35:08 +00006241 Undefined.insert(std::make_pair(D, Loc));
6242 }
6243}
Nick Lewycky8334af82013-01-26 00:35:08 +00006244
Guy Benyei11169dd2012-12-18 14:30:41 +00006245void ASTReader::ReadTentativeDefinitions(
6246 SmallVectorImpl<VarDecl *> &TentativeDefs) {
6247 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
6248 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
6249 if (Var)
6250 TentativeDefs.push_back(Var);
6251 }
6252 TentativeDefinitions.clear();
6253}
6254
6255void ASTReader::ReadUnusedFileScopedDecls(
6256 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
6257 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
6258 DeclaratorDecl *D
6259 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
6260 if (D)
6261 Decls.push_back(D);
6262 }
6263 UnusedFileScopedDecls.clear();
6264}
6265
6266void ASTReader::ReadDelegatingConstructors(
6267 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
6268 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
6269 CXXConstructorDecl *D
6270 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
6271 if (D)
6272 Decls.push_back(D);
6273 }
6274 DelegatingCtorDecls.clear();
6275}
6276
6277void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
6278 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
6279 TypedefNameDecl *D
6280 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
6281 if (D)
6282 Decls.push_back(D);
6283 }
6284 ExtVectorDecls.clear();
6285}
6286
6287void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) {
6288 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
6289 CXXRecordDecl *D
6290 = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I]));
6291 if (D)
6292 Decls.push_back(D);
6293 }
6294 DynamicClasses.clear();
6295}
6296
6297void
Richard Smith78165b52013-01-10 23:43:47 +00006298ASTReader::ReadLocallyScopedExternCDecls(SmallVectorImpl<NamedDecl *> &Decls) {
6299 for (unsigned I = 0, N = LocallyScopedExternCDecls.size(); I != N; ++I) {
6300 NamedDecl *D
6301 = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternCDecls[I]));
Guy Benyei11169dd2012-12-18 14:30:41 +00006302 if (D)
6303 Decls.push_back(D);
6304 }
Richard Smith78165b52013-01-10 23:43:47 +00006305 LocallyScopedExternCDecls.clear();
Guy Benyei11169dd2012-12-18 14:30:41 +00006306}
6307
6308void ASTReader::ReadReferencedSelectors(
6309 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
6310 if (ReferencedSelectorsData.empty())
6311 return;
6312
6313 // If there are @selector references added them to its pool. This is for
6314 // implementation of -Wselector.
6315 unsigned int DataSize = ReferencedSelectorsData.size()-1;
6316 unsigned I = 0;
6317 while (I < DataSize) {
6318 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
6319 SourceLocation SelLoc
6320 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
6321 Sels.push_back(std::make_pair(Sel, SelLoc));
6322 }
6323 ReferencedSelectorsData.clear();
6324}
6325
6326void ASTReader::ReadWeakUndeclaredIdentifiers(
6327 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
6328 if (WeakUndeclaredIdentifiers.empty())
6329 return;
6330
6331 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
6332 IdentifierInfo *WeakId
6333 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
6334 IdentifierInfo *AliasId
6335 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
6336 SourceLocation Loc
6337 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
6338 bool Used = WeakUndeclaredIdentifiers[I++];
6339 WeakInfo WI(AliasId, Loc);
6340 WI.setUsed(Used);
6341 WeakIDs.push_back(std::make_pair(WeakId, WI));
6342 }
6343 WeakUndeclaredIdentifiers.clear();
6344}
6345
6346void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
6347 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
6348 ExternalVTableUse VT;
6349 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
6350 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
6351 VT.DefinitionRequired = VTableUses[Idx++];
6352 VTables.push_back(VT);
6353 }
6354
6355 VTableUses.clear();
6356}
6357
6358void ASTReader::ReadPendingInstantiations(
6359 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
6360 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
6361 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
6362 SourceLocation Loc
6363 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
6364
6365 Pending.push_back(std::make_pair(D, Loc));
6366 }
6367 PendingInstantiations.clear();
6368}
6369
6370void ASTReader::LoadSelector(Selector Sel) {
6371 // It would be complicated to avoid reading the methods anyway. So don't.
6372 ReadMethodPool(Sel);
6373}
6374
6375void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
6376 assert(ID && "Non-zero identifier ID required");
6377 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
6378 IdentifiersLoaded[ID - 1] = II;
6379 if (DeserializationListener)
6380 DeserializationListener->IdentifierRead(ID, II);
6381}
6382
6383/// \brief Set the globally-visible declarations associated with the given
6384/// identifier.
6385///
6386/// If the AST reader is currently in a state where the given declaration IDs
6387/// cannot safely be resolved, they are queued until it is safe to resolve
6388/// them.
6389///
6390/// \param II an IdentifierInfo that refers to one or more globally-visible
6391/// declarations.
6392///
6393/// \param DeclIDs the set of declaration IDs with the name @p II that are
6394/// visible at global scope.
6395///
Douglas Gregor6168bd22013-02-18 15:53:43 +00006396/// \param Decls if non-null, this vector will be populated with the set of
6397/// deserialized declarations. These declarations will not be pushed into
6398/// scope.
Guy Benyei11169dd2012-12-18 14:30:41 +00006399void
6400ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
6401 const SmallVectorImpl<uint32_t> &DeclIDs,
Douglas Gregor6168bd22013-02-18 15:53:43 +00006402 SmallVectorImpl<Decl *> *Decls) {
6403 if (NumCurrentElementsDeserializing && !Decls) {
6404 PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
Guy Benyei11169dd2012-12-18 14:30:41 +00006405 return;
6406 }
6407
6408 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
6409 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
6410 if (SemaObj) {
Douglas Gregor6168bd22013-02-18 15:53:43 +00006411 // If we're simply supposed to record the declarations, do so now.
6412 if (Decls) {
6413 Decls->push_back(D);
6414 continue;
6415 }
6416
Guy Benyei11169dd2012-12-18 14:30:41 +00006417 // Introduce this declaration into the translation-unit scope
6418 // and add it to the declaration chain for this identifier, so
6419 // that (unqualified) name lookup will find it.
Douglas Gregor6168bd22013-02-18 15:53:43 +00006420 NamedDecl *ND = cast<NamedDecl>(D->getMostRecentDecl());
6421 SemaObj->pushExternalDeclIntoScope(ND, II);
Guy Benyei11169dd2012-12-18 14:30:41 +00006422 } else {
6423 // Queue this declaration so that it will be added to the
6424 // translation unit scope and identifier's declaration chain
6425 // once a Sema object is known.
6426 PreloadedDecls.push_back(D);
6427 }
6428 }
6429}
6430
Douglas Gregorc8a992f2013-01-21 16:52:34 +00006431IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00006432 if (ID == 0)
6433 return 0;
6434
6435 if (IdentifiersLoaded.empty()) {
6436 Error("no identifier table in AST file");
6437 return 0;
6438 }
6439
6440 ID -= 1;
6441 if (!IdentifiersLoaded[ID]) {
6442 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
6443 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
6444 ModuleFile *M = I->second;
6445 unsigned Index = ID - M->BaseIdentifierID;
6446 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
6447
6448 // All of the strings in the AST file are preceded by a 16-bit length.
6449 // Extract that 16-bit length to avoid having to execute strlen().
6450 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
6451 // unsigned integers. This is important to avoid integer overflow when
6452 // we cast them to 'unsigned'.
6453 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
6454 unsigned StrLen = (((unsigned) StrLenPtr[0])
6455 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Douglas Gregorc8a992f2013-01-21 16:52:34 +00006456 IdentifiersLoaded[ID]
6457 = &PP.getIdentifierTable().get(StringRef(Str, StrLen));
Guy Benyei11169dd2012-12-18 14:30:41 +00006458 if (DeserializationListener)
6459 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
6460 }
6461
6462 return IdentifiersLoaded[ID];
6463}
6464
Douglas Gregorc8a992f2013-01-21 16:52:34 +00006465IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
6466 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
Guy Benyei11169dd2012-12-18 14:30:41 +00006467}
6468
6469IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
6470 if (LocalID < NUM_PREDEF_IDENT_IDS)
6471 return LocalID;
6472
6473 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6474 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
6475 assert(I != M.IdentifierRemap.end()
6476 && "Invalid index into identifier index remap");
6477
6478 return LocalID + I->second;
6479}
6480
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00006481MacroInfo *ASTReader::getMacro(MacroID ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00006482 if (ID == 0)
6483 return 0;
6484
6485 if (MacrosLoaded.empty()) {
6486 Error("no macro table in AST file");
6487 return 0;
6488 }
6489
6490 ID -= NUM_PREDEF_MACRO_IDS;
6491 if (!MacrosLoaded[ID]) {
6492 GlobalMacroMapType::iterator I
6493 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
6494 assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
6495 ModuleFile *M = I->second;
6496 unsigned Index = ID - M->BaseMacroID;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00006497 MacrosLoaded[ID] = ReadMacroRecord(*M, M->MacroOffsets[Index]);
6498
6499 if (DeserializationListener)
6500 DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
6501 MacrosLoaded[ID]);
Guy Benyei11169dd2012-12-18 14:30:41 +00006502 }
6503
6504 return MacrosLoaded[ID];
6505}
6506
6507MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
6508 if (LocalID < NUM_PREDEF_MACRO_IDS)
6509 return LocalID;
6510
6511 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6512 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
6513 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
6514
6515 return LocalID + I->second;
6516}
6517
6518serialization::SubmoduleID
6519ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
6520 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
6521 return LocalID;
6522
6523 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6524 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
6525 assert(I != M.SubmoduleRemap.end()
6526 && "Invalid index into submodule index remap");
6527
6528 return LocalID + I->second;
6529}
6530
6531Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
6532 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
6533 assert(GlobalID == 0 && "Unhandled global submodule ID");
6534 return 0;
6535 }
6536
6537 if (GlobalID > SubmodulesLoaded.size()) {
6538 Error("submodule ID out of range in AST file");
6539 return 0;
6540 }
6541
6542 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
6543}
Douglas Gregorc147b0b2013-01-12 01:29:50 +00006544
6545Module *ASTReader::getModule(unsigned ID) {
6546 return getSubmodule(ID);
6547}
6548
Guy Benyei11169dd2012-12-18 14:30:41 +00006549Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
6550 return DecodeSelector(getGlobalSelectorID(M, LocalID));
6551}
6552
6553Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
6554 if (ID == 0)
6555 return Selector();
6556
6557 if (ID > SelectorsLoaded.size()) {
6558 Error("selector ID out of range in AST file");
6559 return Selector();
6560 }
6561
6562 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
6563 // Load this selector from the selector table.
6564 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
6565 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
6566 ModuleFile &M = *I->second;
6567 ASTSelectorLookupTrait Trait(*this, M);
6568 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
6569 SelectorsLoaded[ID - 1] =
6570 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
6571 if (DeserializationListener)
6572 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
6573 }
6574
6575 return SelectorsLoaded[ID - 1];
6576}
6577
6578Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
6579 return DecodeSelector(ID);
6580}
6581
6582uint32_t ASTReader::GetNumExternalSelectors() {
6583 // ID 0 (the null selector) is considered an external selector.
6584 return getTotalNumSelectors() + 1;
6585}
6586
6587serialization::SelectorID
6588ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
6589 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
6590 return LocalID;
6591
6592 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6593 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
6594 assert(I != M.SelectorRemap.end()
6595 && "Invalid index into selector index remap");
6596
6597 return LocalID + I->second;
6598}
6599
6600DeclarationName
6601ASTReader::ReadDeclarationName(ModuleFile &F,
6602 const RecordData &Record, unsigned &Idx) {
6603 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
6604 switch (Kind) {
6605 case DeclarationName::Identifier:
Douglas Gregorc8a992f2013-01-21 16:52:34 +00006606 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00006607
6608 case DeclarationName::ObjCZeroArgSelector:
6609 case DeclarationName::ObjCOneArgSelector:
6610 case DeclarationName::ObjCMultiArgSelector:
6611 return DeclarationName(ReadSelector(F, Record, Idx));
6612
6613 case DeclarationName::CXXConstructorName:
6614 return Context.DeclarationNames.getCXXConstructorName(
6615 Context.getCanonicalType(readType(F, Record, Idx)));
6616
6617 case DeclarationName::CXXDestructorName:
6618 return Context.DeclarationNames.getCXXDestructorName(
6619 Context.getCanonicalType(readType(F, Record, Idx)));
6620
6621 case DeclarationName::CXXConversionFunctionName:
6622 return Context.DeclarationNames.getCXXConversionFunctionName(
6623 Context.getCanonicalType(readType(F, Record, Idx)));
6624
6625 case DeclarationName::CXXOperatorName:
6626 return Context.DeclarationNames.getCXXOperatorName(
6627 (OverloadedOperatorKind)Record[Idx++]);
6628
6629 case DeclarationName::CXXLiteralOperatorName:
6630 return Context.DeclarationNames.getCXXLiteralOperatorName(
6631 GetIdentifierInfo(F, Record, Idx));
6632
6633 case DeclarationName::CXXUsingDirective:
6634 return DeclarationName::getUsingDirectiveName();
6635 }
6636
6637 llvm_unreachable("Invalid NameKind!");
6638}
6639
6640void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
6641 DeclarationNameLoc &DNLoc,
6642 DeclarationName Name,
6643 const RecordData &Record, unsigned &Idx) {
6644 switch (Name.getNameKind()) {
6645 case DeclarationName::CXXConstructorName:
6646 case DeclarationName::CXXDestructorName:
6647 case DeclarationName::CXXConversionFunctionName:
6648 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
6649 break;
6650
6651 case DeclarationName::CXXOperatorName:
6652 DNLoc.CXXOperatorName.BeginOpNameLoc
6653 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6654 DNLoc.CXXOperatorName.EndOpNameLoc
6655 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6656 break;
6657
6658 case DeclarationName::CXXLiteralOperatorName:
6659 DNLoc.CXXLiteralOperatorName.OpNameLoc
6660 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6661 break;
6662
6663 case DeclarationName::Identifier:
6664 case DeclarationName::ObjCZeroArgSelector:
6665 case DeclarationName::ObjCOneArgSelector:
6666 case DeclarationName::ObjCMultiArgSelector:
6667 case DeclarationName::CXXUsingDirective:
6668 break;
6669 }
6670}
6671
6672void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
6673 DeclarationNameInfo &NameInfo,
6674 const RecordData &Record, unsigned &Idx) {
6675 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
6676 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
6677 DeclarationNameLoc DNLoc;
6678 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
6679 NameInfo.setInfo(DNLoc);
6680}
6681
6682void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
6683 const RecordData &Record, unsigned &Idx) {
6684 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
6685 unsigned NumTPLists = Record[Idx++];
6686 Info.NumTemplParamLists = NumTPLists;
6687 if (NumTPLists) {
6688 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
6689 for (unsigned i=0; i != NumTPLists; ++i)
6690 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
6691 }
6692}
6693
6694TemplateName
6695ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
6696 unsigned &Idx) {
6697 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
6698 switch (Kind) {
6699 case TemplateName::Template:
6700 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
6701
6702 case TemplateName::OverloadedTemplate: {
6703 unsigned size = Record[Idx++];
6704 UnresolvedSet<8> Decls;
6705 while (size--)
6706 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
6707
6708 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
6709 }
6710
6711 case TemplateName::QualifiedTemplate: {
6712 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
6713 bool hasTemplKeyword = Record[Idx++];
6714 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
6715 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
6716 }
6717
6718 case TemplateName::DependentTemplate: {
6719 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
6720 if (Record[Idx++]) // isIdentifier
6721 return Context.getDependentTemplateName(NNS,
6722 GetIdentifierInfo(F, Record,
6723 Idx));
6724 return Context.getDependentTemplateName(NNS,
6725 (OverloadedOperatorKind)Record[Idx++]);
6726 }
6727
6728 case TemplateName::SubstTemplateTemplateParm: {
6729 TemplateTemplateParmDecl *param
6730 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
6731 if (!param) return TemplateName();
6732 TemplateName replacement = ReadTemplateName(F, Record, Idx);
6733 return Context.getSubstTemplateTemplateParm(param, replacement);
6734 }
6735
6736 case TemplateName::SubstTemplateTemplateParmPack: {
6737 TemplateTemplateParmDecl *Param
6738 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
6739 if (!Param)
6740 return TemplateName();
6741
6742 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
6743 if (ArgPack.getKind() != TemplateArgument::Pack)
6744 return TemplateName();
6745
6746 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
6747 }
6748 }
6749
6750 llvm_unreachable("Unhandled template name kind!");
6751}
6752
6753TemplateArgument
6754ASTReader::ReadTemplateArgument(ModuleFile &F,
6755 const RecordData &Record, unsigned &Idx) {
6756 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
6757 switch (Kind) {
6758 case TemplateArgument::Null:
6759 return TemplateArgument();
6760 case TemplateArgument::Type:
6761 return TemplateArgument(readType(F, Record, Idx));
6762 case TemplateArgument::Declaration: {
6763 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
6764 bool ForReferenceParam = Record[Idx++];
6765 return TemplateArgument(D, ForReferenceParam);
6766 }
6767 case TemplateArgument::NullPtr:
6768 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
6769 case TemplateArgument::Integral: {
6770 llvm::APSInt Value = ReadAPSInt(Record, Idx);
6771 QualType T = readType(F, Record, Idx);
6772 return TemplateArgument(Context, Value, T);
6773 }
6774 case TemplateArgument::Template:
6775 return TemplateArgument(ReadTemplateName(F, Record, Idx));
6776 case TemplateArgument::TemplateExpansion: {
6777 TemplateName Name = ReadTemplateName(F, Record, Idx);
David Blaikie05785d12013-02-20 22:23:23 +00006778 Optional<unsigned> NumTemplateExpansions;
Guy Benyei11169dd2012-12-18 14:30:41 +00006779 if (unsigned NumExpansions = Record[Idx++])
6780 NumTemplateExpansions = NumExpansions - 1;
6781 return TemplateArgument(Name, NumTemplateExpansions);
6782 }
6783 case TemplateArgument::Expression:
6784 return TemplateArgument(ReadExpr(F));
6785 case TemplateArgument::Pack: {
6786 unsigned NumArgs = Record[Idx++];
6787 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
6788 for (unsigned I = 0; I != NumArgs; ++I)
6789 Args[I] = ReadTemplateArgument(F, Record, Idx);
6790 return TemplateArgument(Args, NumArgs);
6791 }
6792 }
6793
6794 llvm_unreachable("Unhandled template argument kind!");
6795}
6796
6797TemplateParameterList *
6798ASTReader::ReadTemplateParameterList(ModuleFile &F,
6799 const RecordData &Record, unsigned &Idx) {
6800 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
6801 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
6802 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
6803
6804 unsigned NumParams = Record[Idx++];
6805 SmallVector<NamedDecl *, 16> Params;
6806 Params.reserve(NumParams);
6807 while (NumParams--)
6808 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
6809
6810 TemplateParameterList* TemplateParams =
6811 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
6812 Params.data(), Params.size(), RAngleLoc);
6813 return TemplateParams;
6814}
6815
6816void
6817ASTReader::
6818ReadTemplateArgumentList(SmallVector<TemplateArgument, 8> &TemplArgs,
6819 ModuleFile &F, const RecordData &Record,
6820 unsigned &Idx) {
6821 unsigned NumTemplateArgs = Record[Idx++];
6822 TemplArgs.reserve(NumTemplateArgs);
6823 while (NumTemplateArgs--)
6824 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
6825}
6826
6827/// \brief Read a UnresolvedSet structure.
6828void ASTReader::ReadUnresolvedSet(ModuleFile &F, ASTUnresolvedSet &Set,
6829 const RecordData &Record, unsigned &Idx) {
6830 unsigned NumDecls = Record[Idx++];
6831 Set.reserve(Context, NumDecls);
6832 while (NumDecls--) {
6833 NamedDecl *D = ReadDeclAs<NamedDecl>(F, Record, Idx);
6834 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
6835 Set.addDecl(Context, D, AS);
6836 }
6837}
6838
6839CXXBaseSpecifier
6840ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
6841 const RecordData &Record, unsigned &Idx) {
6842 bool isVirtual = static_cast<bool>(Record[Idx++]);
6843 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
6844 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
6845 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
6846 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
6847 SourceRange Range = ReadSourceRange(F, Record, Idx);
6848 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
6849 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
6850 EllipsisLoc);
6851 Result.setInheritConstructors(inheritConstructors);
6852 return Result;
6853}
6854
6855std::pair<CXXCtorInitializer **, unsigned>
6856ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
6857 unsigned &Idx) {
6858 CXXCtorInitializer **CtorInitializers = 0;
6859 unsigned NumInitializers = Record[Idx++];
6860 if (NumInitializers) {
6861 CtorInitializers
6862 = new (Context) CXXCtorInitializer*[NumInitializers];
6863 for (unsigned i=0; i != NumInitializers; ++i) {
6864 TypeSourceInfo *TInfo = 0;
6865 bool IsBaseVirtual = false;
6866 FieldDecl *Member = 0;
6867 IndirectFieldDecl *IndirectMember = 0;
6868
6869 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
6870 switch (Type) {
6871 case CTOR_INITIALIZER_BASE:
6872 TInfo = GetTypeSourceInfo(F, Record, Idx);
6873 IsBaseVirtual = Record[Idx++];
6874 break;
6875
6876 case CTOR_INITIALIZER_DELEGATING:
6877 TInfo = GetTypeSourceInfo(F, Record, Idx);
6878 break;
6879
6880 case CTOR_INITIALIZER_MEMBER:
6881 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
6882 break;
6883
6884 case CTOR_INITIALIZER_INDIRECT_MEMBER:
6885 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
6886 break;
6887 }
6888
6889 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
6890 Expr *Init = ReadExpr(F);
6891 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
6892 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
6893 bool IsWritten = Record[Idx++];
6894 unsigned SourceOrderOrNumArrayIndices;
6895 SmallVector<VarDecl *, 8> Indices;
6896 if (IsWritten) {
6897 SourceOrderOrNumArrayIndices = Record[Idx++];
6898 } else {
6899 SourceOrderOrNumArrayIndices = Record[Idx++];
6900 Indices.reserve(SourceOrderOrNumArrayIndices);
6901 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
6902 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
6903 }
6904
6905 CXXCtorInitializer *BOMInit;
6906 if (Type == CTOR_INITIALIZER_BASE) {
6907 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, IsBaseVirtual,
6908 LParenLoc, Init, RParenLoc,
6909 MemberOrEllipsisLoc);
6910 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
6911 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, LParenLoc,
6912 Init, RParenLoc);
6913 } else if (IsWritten) {
6914 if (Member)
6915 BOMInit = new (Context) CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc,
6916 LParenLoc, Init, RParenLoc);
6917 else
6918 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
6919 MemberOrEllipsisLoc, LParenLoc,
6920 Init, RParenLoc);
6921 } else {
6922 BOMInit = CXXCtorInitializer::Create(Context, Member, MemberOrEllipsisLoc,
6923 LParenLoc, Init, RParenLoc,
6924 Indices.data(), Indices.size());
6925 }
6926
6927 if (IsWritten)
6928 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
6929 CtorInitializers[i] = BOMInit;
6930 }
6931 }
6932
6933 return std::make_pair(CtorInitializers, NumInitializers);
6934}
6935
6936NestedNameSpecifier *
6937ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
6938 const RecordData &Record, unsigned &Idx) {
6939 unsigned N = Record[Idx++];
6940 NestedNameSpecifier *NNS = 0, *Prev = 0;
6941 for (unsigned I = 0; I != N; ++I) {
6942 NestedNameSpecifier::SpecifierKind Kind
6943 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
6944 switch (Kind) {
6945 case NestedNameSpecifier::Identifier: {
6946 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
6947 NNS = NestedNameSpecifier::Create(Context, Prev, II);
6948 break;
6949 }
6950
6951 case NestedNameSpecifier::Namespace: {
6952 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
6953 NNS = NestedNameSpecifier::Create(Context, Prev, NS);
6954 break;
6955 }
6956
6957 case NestedNameSpecifier::NamespaceAlias: {
6958 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
6959 NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
6960 break;
6961 }
6962
6963 case NestedNameSpecifier::TypeSpec:
6964 case NestedNameSpecifier::TypeSpecWithTemplate: {
6965 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
6966 if (!T)
6967 return 0;
6968
6969 bool Template = Record[Idx++];
6970 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
6971 break;
6972 }
6973
6974 case NestedNameSpecifier::Global: {
6975 NNS = NestedNameSpecifier::GlobalSpecifier(Context);
6976 // No associated value, and there can't be a prefix.
6977 break;
6978 }
6979 }
6980 Prev = NNS;
6981 }
6982 return NNS;
6983}
6984
6985NestedNameSpecifierLoc
6986ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
6987 unsigned &Idx) {
6988 unsigned N = Record[Idx++];
6989 NestedNameSpecifierLocBuilder Builder;
6990 for (unsigned I = 0; I != N; ++I) {
6991 NestedNameSpecifier::SpecifierKind Kind
6992 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
6993 switch (Kind) {
6994 case NestedNameSpecifier::Identifier: {
6995 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
6996 SourceRange Range = ReadSourceRange(F, Record, Idx);
6997 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
6998 break;
6999 }
7000
7001 case NestedNameSpecifier::Namespace: {
7002 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
7003 SourceRange Range = ReadSourceRange(F, Record, Idx);
7004 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
7005 break;
7006 }
7007
7008 case NestedNameSpecifier::NamespaceAlias: {
7009 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
7010 SourceRange Range = ReadSourceRange(F, Record, Idx);
7011 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
7012 break;
7013 }
7014
7015 case NestedNameSpecifier::TypeSpec:
7016 case NestedNameSpecifier::TypeSpecWithTemplate: {
7017 bool Template = Record[Idx++];
7018 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
7019 if (!T)
7020 return NestedNameSpecifierLoc();
7021 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
7022
7023 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
7024 Builder.Extend(Context,
7025 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
7026 T->getTypeLoc(), ColonColonLoc);
7027 break;
7028 }
7029
7030 case NestedNameSpecifier::Global: {
7031 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
7032 Builder.MakeGlobal(Context, ColonColonLoc);
7033 break;
7034 }
7035 }
7036 }
7037
7038 return Builder.getWithLocInContext(Context);
7039}
7040
7041SourceRange
7042ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
7043 unsigned &Idx) {
7044 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
7045 SourceLocation end = ReadSourceLocation(F, Record, Idx);
7046 return SourceRange(beg, end);
7047}
7048
7049/// \brief Read an integral value
7050llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
7051 unsigned BitWidth = Record[Idx++];
7052 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
7053 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
7054 Idx += NumWords;
7055 return Result;
7056}
7057
7058/// \brief Read a signed integral value
7059llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
7060 bool isUnsigned = Record[Idx++];
7061 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
7062}
7063
7064/// \brief Read a floating-point value
Tim Northover178723a2013-01-22 09:46:51 +00007065llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record,
7066 const llvm::fltSemantics &Sem,
7067 unsigned &Idx) {
7068 return llvm::APFloat(Sem, ReadAPInt(Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00007069}
7070
7071// \brief Read a string
7072std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
7073 unsigned Len = Record[Idx++];
7074 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
7075 Idx += Len;
7076 return Result;
7077}
7078
7079VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
7080 unsigned &Idx) {
7081 unsigned Major = Record[Idx++];
7082 unsigned Minor = Record[Idx++];
7083 unsigned Subminor = Record[Idx++];
7084 if (Minor == 0)
7085 return VersionTuple(Major);
7086 if (Subminor == 0)
7087 return VersionTuple(Major, Minor - 1);
7088 return VersionTuple(Major, Minor - 1, Subminor - 1);
7089}
7090
7091CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
7092 const RecordData &Record,
7093 unsigned &Idx) {
7094 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
7095 return CXXTemporary::Create(Context, Decl);
7096}
7097
7098DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
7099 return Diag(SourceLocation(), DiagID);
7100}
7101
7102DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
7103 return Diags.Report(Loc, DiagID);
7104}
7105
7106/// \brief Retrieve the identifier table associated with the
7107/// preprocessor.
7108IdentifierTable &ASTReader::getIdentifierTable() {
7109 return PP.getIdentifierTable();
7110}
7111
7112/// \brief Record that the given ID maps to the given switch-case
7113/// statement.
7114void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
7115 assert((*CurrSwitchCaseStmts)[ID] == 0 &&
7116 "Already have a SwitchCase with this ID");
7117 (*CurrSwitchCaseStmts)[ID] = SC;
7118}
7119
7120/// \brief Retrieve the switch-case statement with the given ID.
7121SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
7122 assert((*CurrSwitchCaseStmts)[ID] != 0 && "No SwitchCase with this ID");
7123 return (*CurrSwitchCaseStmts)[ID];
7124}
7125
7126void ASTReader::ClearSwitchCaseIDs() {
7127 CurrSwitchCaseStmts->clear();
7128}
7129
7130void ASTReader::ReadComments() {
7131 std::vector<RawComment *> Comments;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00007132 for (SmallVectorImpl<std::pair<BitstreamCursor,
Guy Benyei11169dd2012-12-18 14:30:41 +00007133 serialization::ModuleFile *> >::iterator
7134 I = CommentsCursors.begin(),
7135 E = CommentsCursors.end();
7136 I != E; ++I) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00007137 BitstreamCursor &Cursor = I->first;
Guy Benyei11169dd2012-12-18 14:30:41 +00007138 serialization::ModuleFile &F = *I->second;
7139 SavedStreamPosition SavedPosition(Cursor);
7140
7141 RecordData Record;
7142 while (true) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00007143 llvm::BitstreamEntry Entry =
7144 Cursor.advanceSkippingSubblocks(BitstreamCursor::AF_DontPopBlockAtEnd);
7145
7146 switch (Entry.Kind) {
7147 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
7148 case llvm::BitstreamEntry::Error:
7149 Error("malformed block record in AST file");
7150 return;
7151 case llvm::BitstreamEntry::EndBlock:
7152 goto NextCursor;
7153 case llvm::BitstreamEntry::Record:
7154 // The interesting case.
Guy Benyei11169dd2012-12-18 14:30:41 +00007155 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00007156 }
7157
7158 // Read a record.
7159 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00007160 switch ((CommentRecordTypes)Cursor.readRecord(Entry.ID, Record)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007161 case COMMENTS_RAW_COMMENT: {
7162 unsigned Idx = 0;
7163 SourceRange SR = ReadSourceRange(F, Record, Idx);
7164 RawComment::CommentKind Kind =
7165 (RawComment::CommentKind) Record[Idx++];
7166 bool IsTrailingComment = Record[Idx++];
7167 bool IsAlmostTrailingComment = Record[Idx++];
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00007168 Comments.push_back(new (Context) RawComment(
7169 SR, Kind, IsTrailingComment, IsAlmostTrailingComment,
7170 Context.getLangOpts().CommentOpts.ParseAllComments));
Guy Benyei11169dd2012-12-18 14:30:41 +00007171 break;
7172 }
7173 }
7174 }
Chris Lattner7fb3bef2013-01-20 00:56:42 +00007175 NextCursor:;
Guy Benyei11169dd2012-12-18 14:30:41 +00007176 }
7177 Context.Comments.addCommentsToFront(Comments);
7178}
7179
7180void ASTReader::finishPendingActions() {
7181 while (!PendingIdentifierInfos.empty() || !PendingDeclChains.empty() ||
Argyrios Kyrtzidis83a6e3b2013-02-16 00:48:59 +00007182 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007183 // If any identifiers with corresponding top-level declarations have
7184 // been loaded, load those declarations now.
Douglas Gregor6168bd22013-02-18 15:53:43 +00007185 llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2> > TopLevelDecls;
Guy Benyei11169dd2012-12-18 14:30:41 +00007186 while (!PendingIdentifierInfos.empty()) {
Douglas Gregor6168bd22013-02-18 15:53:43 +00007187 // FIXME: std::move
7188 IdentifierInfo *II = PendingIdentifierInfos.back().first;
7189 SmallVector<uint32_t, 4> DeclIDs = PendingIdentifierInfos.back().second;
Douglas Gregorcb15f082013-02-19 18:26:28 +00007190 PendingIdentifierInfos.pop_back();
Douglas Gregor6168bd22013-02-18 15:53:43 +00007191
7192 SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
Guy Benyei11169dd2012-12-18 14:30:41 +00007193 }
7194
7195 // Load pending declaration chains.
7196 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) {
7197 loadPendingDeclChain(PendingDeclChains[I]);
7198 PendingDeclChainsKnown.erase(PendingDeclChains[I]);
7199 }
7200 PendingDeclChains.clear();
7201
Douglas Gregor6168bd22013-02-18 15:53:43 +00007202 // Make the most recent of the top-level declarations visible.
7203 for (llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2> >::iterator
7204 TLD = TopLevelDecls.begin(), TLDEnd = TopLevelDecls.end();
7205 TLD != TLDEnd; ++TLD) {
7206 IdentifierInfo *II = TLD->first;
7207 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
7208 NamedDecl *ND = cast<NamedDecl>(TLD->second[I]->getMostRecentDecl());
7209 SemaObj->pushExternalDeclIntoScope(ND, II);
7210 }
7211 }
7212
Guy Benyei11169dd2012-12-18 14:30:41 +00007213 // Load any pending macro definitions.
7214 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00007215 IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
7216 SmallVector<PendingMacroInfo, 2> GlobalIDs;
7217 GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
7218 // Initialize the macro history from chained-PCHs ahead of module imports.
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00007219 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
7220 ++IDIdx) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00007221 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
7222 if (Info.M->Kind != MK_Module)
7223 resolvePendingMacro(II, Info);
7224 }
7225 // Handle module imports.
7226 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
7227 ++IDIdx) {
7228 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
7229 if (Info.M->Kind == MK_Module)
7230 resolvePendingMacro(II, Info);
Guy Benyei11169dd2012-12-18 14:30:41 +00007231 }
7232 }
7233 PendingMacroIDs.clear();
Argyrios Kyrtzidis83a6e3b2013-02-16 00:48:59 +00007234
7235 // Wire up the DeclContexts for Decls that we delayed setting until
7236 // recursive loading is completed.
7237 while (!PendingDeclContextInfos.empty()) {
7238 PendingDeclContextInfo Info = PendingDeclContextInfos.front();
7239 PendingDeclContextInfos.pop_front();
7240 DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
7241 DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
7242 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
7243 }
Guy Benyei11169dd2012-12-18 14:30:41 +00007244 }
7245
7246 // If we deserialized any C++ or Objective-C class definitions, any
7247 // Objective-C protocol definitions, or any redeclarable templates, make sure
7248 // that all redeclarations point to the definitions. Note that this can only
7249 // happen now, after the redeclaration chains have been fully wired.
7250 for (llvm::SmallPtrSet<Decl *, 4>::iterator D = PendingDefinitions.begin(),
7251 DEnd = PendingDefinitions.end();
7252 D != DEnd; ++D) {
7253 if (TagDecl *TD = dyn_cast<TagDecl>(*D)) {
7254 if (const TagType *TagT = dyn_cast<TagType>(TD->TypeForDecl)) {
7255 // Make sure that the TagType points at the definition.
7256 const_cast<TagType*>(TagT)->decl = TD;
7257 }
7258
7259 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(*D)) {
7260 for (CXXRecordDecl::redecl_iterator R = RD->redecls_begin(),
7261 REnd = RD->redecls_end();
7262 R != REnd; ++R)
7263 cast<CXXRecordDecl>(*R)->DefinitionData = RD->DefinitionData;
7264
7265 }
7266
7267 continue;
7268 }
7269
7270 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(*D)) {
7271 // Make sure that the ObjCInterfaceType points at the definition.
7272 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
7273 ->Decl = ID;
7274
7275 for (ObjCInterfaceDecl::redecl_iterator R = ID->redecls_begin(),
7276 REnd = ID->redecls_end();
7277 R != REnd; ++R)
7278 R->Data = ID->Data;
7279
7280 continue;
7281 }
7282
7283 if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(*D)) {
7284 for (ObjCProtocolDecl::redecl_iterator R = PD->redecls_begin(),
7285 REnd = PD->redecls_end();
7286 R != REnd; ++R)
7287 R->Data = PD->Data;
7288
7289 continue;
7290 }
7291
7292 RedeclarableTemplateDecl *RTD
7293 = cast<RedeclarableTemplateDecl>(*D)->getCanonicalDecl();
7294 for (RedeclarableTemplateDecl::redecl_iterator R = RTD->redecls_begin(),
7295 REnd = RTD->redecls_end();
7296 R != REnd; ++R)
7297 R->Common = RTD->Common;
7298 }
7299 PendingDefinitions.clear();
7300
7301 // Load the bodies of any functions or methods we've encountered. We do
7302 // this now (delayed) so that we can be sure that the declaration chains
7303 // have been fully wired up.
7304 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
7305 PBEnd = PendingBodies.end();
7306 PB != PBEnd; ++PB) {
7307 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
7308 // FIXME: Check for =delete/=default?
7309 // FIXME: Complain about ODR violations here?
7310 if (!getContext().getLangOpts().Modules || !FD->hasBody())
7311 FD->setLazyBody(PB->second);
7312 continue;
7313 }
7314
7315 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
7316 if (!getContext().getLangOpts().Modules || !MD->hasBody())
7317 MD->setLazyBody(PB->second);
7318 }
7319 PendingBodies.clear();
7320}
7321
7322void ASTReader::FinishedDeserializing() {
7323 assert(NumCurrentElementsDeserializing &&
7324 "FinishedDeserializing not paired with StartedDeserializing");
7325 if (NumCurrentElementsDeserializing == 1) {
7326 // We decrease NumCurrentElementsDeserializing only after pending actions
7327 // are finished, to avoid recursively re-calling finishPendingActions().
7328 finishPendingActions();
7329 }
7330 --NumCurrentElementsDeserializing;
7331
7332 if (NumCurrentElementsDeserializing == 0 &&
7333 Consumer && !PassingDeclsToConsumer) {
7334 // Guard variable to avoid recursively redoing the process of passing
7335 // decls to consumer.
7336 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
7337 true);
7338
7339 while (!InterestingDecls.empty()) {
7340 // We are not in recursive loading, so it's safe to pass the "interesting"
7341 // decls to the consumer.
7342 Decl *D = InterestingDecls.front();
7343 InterestingDecls.pop_front();
7344 PassInterestingDeclToConsumer(D);
7345 }
7346 }
7347}
7348
7349ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context,
7350 StringRef isysroot, bool DisableValidation,
Douglas Gregorc1bbec82013-01-25 00:45:27 +00007351 bool AllowASTWithCompilerErrors, bool UseGlobalIndex)
Guy Benyei11169dd2012-12-18 14:30:41 +00007352 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
7353 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
7354 Diags(PP.getDiagnostics()), SemaObj(0), PP(PP), Context(Context),
7355 Consumer(0), ModuleMgr(PP.getFileManager()),
7356 isysroot(isysroot), DisableValidation(DisableValidation),
Douglas Gregor00a50f72013-01-25 00:38:33 +00007357 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
Douglas Gregorc1bbec82013-01-25 00:45:27 +00007358 UseGlobalIndex(UseGlobalIndex), TriedLoadingGlobalIndex(false),
Guy Benyei11169dd2012-12-18 14:30:41 +00007359 CurrentGeneration(0), CurrSwitchCaseStmts(&SwitchCaseStmts),
7360 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Douglas Gregor00a50f72013-01-25 00:38:33 +00007361 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
7362 TotalNumMacros(0), NumIdentifierLookups(0), NumIdentifierLookupHits(0),
7363 NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
Douglas Gregorad2f7a52013-01-28 17:54:36 +00007364 NumMethodPoolLookups(0), NumMethodPoolHits(0),
7365 NumMethodPoolTableLookups(0), NumMethodPoolTableHits(0),
7366 TotalNumMethodPoolEntries(0),
Guy Benyei11169dd2012-12-18 14:30:41 +00007367 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
7368 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
7369 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
7370 PassingDeclsToConsumer(false),
7371 NumCXXBaseSpecifiersLoaded(0)
7372{
7373 SourceMgr.setExternalSLocEntrySource(this);
7374}
7375
7376ASTReader::~ASTReader() {
7377 for (DeclContextVisibleUpdatesPending::iterator
7378 I = PendingVisibleUpdates.begin(),
7379 E = PendingVisibleUpdates.end();
7380 I != E; ++I) {
7381 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
7382 F = I->second.end();
7383 J != F; ++J)
7384 delete J->first;
7385 }
7386}