blob: 20d0404e4e8bd3d6a5f4412a568592bb99424061 [file] [log] [blame]
Nick Lewycky995e26b2013-01-31 03:23:57 +00001//===--- ASTReader.cpp - AST File Reader ----------------------------------===//
Guy Benyei7f92f2d2012-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 Benyei7f92f2d2012-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 Gregor1a49d972013-01-25 01:03:03 +000041#include "clang/Serialization/GlobalModuleIndex.h"
Guy Benyei7f92f2d2012-12-18 14:30:41 +000042#include "clang/Serialization/ModuleManager.h"
43#include "clang/Serialization/SerializationDiagnostic.h"
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +000044#include "llvm/ADT/Hashing.h"
Guy Benyei7f92f2d2012-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 Lattnere4e4a882013-01-20 00:57:52 +000054#include <cstdio>
Guy Benyei7f92f2d2012-12-18 14:30:41 +000055#include <iterator>
56
57using namespace clang;
58using namespace clang::serialization;
59using namespace clang::serialization::reader;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +000060using llvm::BitstreamCursor;
Guy Benyei7f92f2d2012-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 Gribenko6ebf0912013-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 Benyei7f92f2d2012-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 Gregor8222b892013-01-21 16:52:34 +0000409 = Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
Guy Benyei7f92f2d2012-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 Gregor8222b892013-01-21 16:52:34 +0000418 Args.push_back(Reader.getLocalIdentifier(F, ReadUnalignedLE32(d)));
Guy Benyei7f92f2d2012-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 Kyrtzidis2e3d8c02013-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 Benyei7f92f2d2012-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 Gregor479633c2013-01-23 18:53:14 +0000455unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
456 return llvm::HashString(a);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000457}
458
459std::pair<unsigned, unsigned>
Douglas Gregor479633c2013-01-23 18:53:14 +0000460ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
Guy Benyei7f92f2d2012-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 Gregor479633c2013-01-23 18:53:14 +0000467ASTIdentifierLookupTraitBase::internal_key_type
468ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000469 assert(n >= 2 && d[n-1] == '\0');
Douglas Gregor479633c2013-01-23 18:53:14 +0000470 return StringRef((const char*) d, n-1);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000471}
472
Douglas Gregorf4e955b2013-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 Benyei7f92f2d2012-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 Gregor479633c2013-01-23 18:53:14 +0000499 II = &Reader.getIdentifierTable().getOwn(k);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000500 KnownII = II;
501 }
502 Reader.SetIdentifierInfo(ID, II);
Douglas Gregorf4e955b2013-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 Benyei7f92f2d2012-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 Kyrtzidis9317ab92013-03-22 21:12:57 +0000523 bool hasSubmoduleMacros = Bits & 0x01;
524 Bits >>= 1;
Guy Benyei7f92f2d2012-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 Gregor479633c2013-01-23 18:53:14 +0000535 II = &Reader.getIdentifierTable().getOwn(StringRef(k));
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000536 KnownII = II;
537 }
538 Reader.markIdentifierUpToDate(II);
Douglas Gregorf4e955b2013-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 Benyei7f92f2d2012-12-18 14:30:41 +0000545
546 // Set or check the various bits in the IdentifierInfo structure.
547 // Token IDs are read-only.
Argyrios Kyrtzidis1ebefc72013-02-27 01:13:51 +0000548 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
Guy Benyei7f92f2d2012-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 Kyrtzidis9317ab92013-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 Kyrtzidisdc1088f2013-01-19 03:14:56 +0000571 DataLen -= 4;
572 }
Argyrios Kyrtzidis9317ab92013-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 Benyei7f92f2d2012-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 Kyrtzidise8b61cf2013-01-11 22:29:49 +0000706 LE32DeclID *Start = reinterpret_cast<LE32DeclID *>(
707 const_cast<unsigned char *>(d));
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000708 return std::make_pair(Start, Start + NumDecls);
709}
710
711bool ASTReader::ReadDeclContextStorage(ModuleFile &M,
Chris Lattner8f9a1eb2013-01-20 00:56:42 +0000712 BitstreamCursor &Cursor,
Guy Benyei7f92f2d2012-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 Lattnerb3ce3572013-01-20 02:38:54 +0000721 StringRef Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000722 unsigned Code = Cursor.ReadCode();
Chris Lattnerb3ce3572013-01-20 02:38:54 +0000723 unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000724 if (RecCode != DECL_CONTEXT_LEXICAL) {
725 Error("Expected lexical block");
726 return true;
727 }
728
Chris Lattnerb3ce3572013-01-20 02:38:54 +0000729 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob.data());
730 Info.NumLexicalDecls = Blob.size() / sizeof(KindDeclIDPair);
Guy Benyei7f92f2d2012-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 Lattnerb3ce3572013-01-20 02:38:54 +0000738 StringRef Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000739 unsigned Code = Cursor.ReadCode();
Chris Lattnerb3ce3572013-01-20 02:38:54 +0000740 unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
Guy Benyei7f92f2d2012-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 Lattnerb3ce3572013-01-20 02:38:54 +0000747 (const unsigned char *)Blob.data() + Record[0],
748 (const unsigned char *)Blob.data(),
Guy Benyei7f92f2d2012-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 Lattner8f9a1eb2013-01-20 00:56:42 +0000822 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Guy Benyei7f92f2d2012-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 Lattner88bde502013-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 Benyei7f92f2d2012-12-18 14:30:41 +0000852 return false;
Chris Lattner88bde502013-01-19 21:39:22 +0000853 case llvm::BitstreamEntry::Record:
854 // The interesting case.
855 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000856 }
Chris Lattner88bde502013-01-19 21:39:22 +0000857
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000858 // Read a record.
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000859 Record.clear();
Chris Lattner125eb3e2013-01-21 18:28:26 +0000860 StringRef Blob;
861 switch (SLocEntryCursor.readRecord(E.ID, Record, &Blob)) {
Guy Benyei7f92f2d2012-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 Lattner8f9a1eb2013-01-20 00:56:42 +0000918 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000919 unsigned BaseOffset = F->SLocEntryBaseOffset;
920
921 ++NumSLocEntriesRead;
Chris Lattner88bde502013-01-19 21:39:22 +0000922 llvm::BitstreamEntry Entry = SLocEntryCursor.advance();
923 if (Entry.Kind != llvm::BitstreamEntry::Record) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000924 Error("incorrectly-formatted source location entry in AST file");
925 return true;
926 }
Chris Lattner88bde502013-01-19 21:39:22 +0000927
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000928 RecordData Record;
Chris Lattnerb3ce3572013-01-20 02:38:54 +0000929 StringRef Blob;
930 switch (SLocEntryCursor.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei7f92f2d2012-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 Kyrtzidis8504b7b2013-03-01 03:26:04 +0000940 const FileEntry *File = IF.getFile();
941 bool OverriddenBuffer = IF.isOverridden();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000942
Argyrios Kyrtzidis8504b7b2013-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 Benyei7f92f2d2012-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 Lattnerb3ce3572013-01-20 02:38:54 +0000979 unsigned RecCode = SLocEntryCursor.readRecord(Code, Record, &Blob);
Guy Benyei7f92f2d2012-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 Lattnerb3ce3572013-01-20 02:38:54 +0000987 = llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), File->getName());
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000988 SourceMgr.overrideFileContents(File, Buffer);
989 }
990
991 break;
992 }
993
994 case SM_SLOC_BUFFER_ENTRY: {
Chris Lattnerb3ce3572013-01-20 02:38:54 +0000995 const char *Name = Blob.data();
Guy Benyei7f92f2d2012-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 Lattnerb3ce3572013-01-20 02:38:54 +00001006 = SLocEntryCursor.readRecord(Code, Record, &Blob);
Guy Benyei7f92f2d2012-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 Lattnerb3ce3572013-01-20 02:38:54 +00001014 = llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name);
Guy Benyei7f92f2d2012-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 Lattner8f9a1eb2013-01-20 00:56:42 +00001076bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID) {
Guy Benyei7f92f2d2012-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 Kyrtzidis9317ab92013-03-22 21:12:57 +00001095MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001096 BitstreamCursor &Stream = F.MacroCursor;
Guy Benyei7f92f2d2012-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 Benyei7f92f2d2012-12-18 14:30:41 +00001107 while (true) {
Chris Lattner99a5af02013-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 Lattner8f9a1eb2013-01-20 00:56:42 +00001111 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
Chris Lattner99a5af02013-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 Kyrtzidis9317ab92013-03-22 21:12:57 +00001118 return Macro;
Chris Lattner99a5af02013-01-20 00:00:22 +00001119 case llvm::BitstreamEntry::EndBlock:
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001120 return Macro;
Chris Lattner99a5af02013-01-20 00:00:22 +00001121 case llvm::BitstreamEntry::Record:
1122 // The interesting case.
1123 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001124 }
1125
1126 // Read a record.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001127 Record.clear();
1128 PreprocessorRecordTypes RecType =
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001129 (PreprocessorRecordTypes)Stream.readRecord(Entry.ID, Record);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001130 switch (RecType) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001131 case PP_MACRO_DIRECTIVE_HISTORY:
1132 return Macro;
1133
Guy Benyei7f92f2d2012-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 Kyrtzidis9317ab92013-03-22 21:12:57 +00001140 return Macro;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001141
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001142 unsigned NextIndex = 1; // Skip identifier ID.
1143 SubmoduleID SubModID = getGlobalSubmoduleID(F, Record[NextIndex++]);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001144 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001145 MacroInfo *MI = PP.AllocateDeserializedMacroInfo(Loc, SubModID);
Argyrios Kyrtzidis8169b672013-01-07 19:16:23 +00001146 MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001147 MI->setIsUsed(Record[NextIndex++]);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001148
Guy Benyei7f92f2d2012-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 Benyei7f92f2d2012-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 Kyrtzidis0b849d32013-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 Benyei7f92f2d2012-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 Kyrtzidised3802e2013-03-06 18:12:47 +00001220unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1221 return llvm::hash_combine(ikey.Size, ikey.ModTime);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001222}
1223
1224HeaderFileInfoTrait::internal_key_type
Argyrios Kyrtzidised3802e2013-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 Benyei7f92f2d2012-12-18 14:30:41 +00001230
Argyrios Kyrtzidised3802e2013-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 Benyei7f92f2d2012-12-18 14:30:41 +00001233 return false;
1234
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001235 if (strcmp(a.Filename, b.Filename) == 0)
1236 return true;
1237
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001238 // Determine whether the actual files are equivalent.
Argyrios Kyrtzidis1c1508b2013-03-04 20:33:40 +00001239 FileManager &FileMgr = Reader.getFileManager();
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001240 const FileEntry *FEA = FileMgr.getFile(a.Filename);
1241 const FileEntry *FEB = FileMgr.getFile(b.Filename);
Argyrios Kyrtzidis1c1508b2013-03-04 20:33:40 +00001242 return (FEA && FEA == FEB);
Guy Benyei7f92f2d2012-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 Kyrtzidised3802e2013-03-06 18:12:47 +00001249 return std::make_pair(KeyLen, DataLen);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001250}
Argyrios Kyrtzidised3802e2013-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 Benyei7f92f2d2012-12-18 14:30:41 +00001261HeaderFileInfoTrait::data_type
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001262HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
Guy Benyei7f92f2d2012-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 Kyrtzidis55ea75b2013-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 Benyei7f92f2d2012-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 Kyrtzidis9317ab92013-03-22 21:12:57 +00001306void ASTReader::addPendingMacroFromModule(IdentifierInfo *II,
1307 ModuleFile *M,
1308 GlobalMacroID GMacID,
1309 SourceLocation ImportLoc) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001310 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
Argyrios Kyrtzidis9317ab92013-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 Benyei7f92f2d2012-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 Lattner8f9a1eb2013-01-20 00:56:42 +00001327 BitstreamCursor &MacroCursor = (*I)->MacroCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001328
1329 // If there was no preprocessor block, skip this file.
1330 if (!MacroCursor.getBitStreamReader())
1331 continue;
1332
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001333 BitstreamCursor Cursor = MacroCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001334 Cursor.JumpToBit((*I)->MacroStartOffset);
1335
1336 RecordData Record;
1337 while (true) {
Chris Lattner88bde502013-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 Lattner88bde502013-01-19 21:39:22 +00001349 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001350 switch (Cursor.readRecord(E.ID, Record)) {
Chris Lattner88bde502013-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 Benyei7f92f2d2012-12-18 14:30:41 +00001362 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001363 break;
1364 }
1365 }
Chris Lattner88bde502013-01-19 21:39:22 +00001366 NextCursor: ;
Guy Benyei7f92f2d2012-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 Gregore1698072013-01-25 00:38:33 +00001375 unsigned &NumIdentifierLookups;
1376 unsigned &NumIdentifierLookupHits;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001377 IdentifierInfo *Found;
Douglas Gregore1698072013-01-25 00:38:33 +00001378
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001379 public:
Douglas Gregore1698072013-01-25 00:38:33 +00001380 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
1381 unsigned &NumIdentifierLookups,
1382 unsigned &NumIdentifierLookupHits)
Douglas Gregor188bdcd2013-01-25 23:32:03 +00001383 : Name(Name), PriorGeneration(PriorGeneration),
Douglas Gregore1698072013-01-25 00:38:33 +00001384 NumIdentifierLookups(NumIdentifierLookups),
1385 NumIdentifierLookupHits(NumIdentifierLookupHits),
1386 Found()
1387 {
1388 }
Guy Benyei7f92f2d2012-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 Gregor1a49d972013-01-25 01:03:03 +00001397
Guy Benyei7f92f2d2012-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 Gregore1698072013-01-25 00:38:33 +00001405 ++This->NumIdentifierLookups;
1406 ASTIdentifierLookupTable::iterator Pos = IdTable->find(This->Name,&Trait);
Guy Benyei7f92f2d2012-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 Gregore1698072013-01-25 00:38:33 +00001413 ++This->NumIdentifierLookupHits;
Guy Benyei7f92f2d2012-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 Gregor1a49d972013-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 Gregor188bdcd2013-01-25 23:32:03 +00001434 GlobalModuleIndex::HitSet Hits;
1435 GlobalModuleIndex::HitSet *HitsPtr = 0;
Douglas Gregor1a49d972013-01-25 01:03:03 +00001436 if (!loadGlobalIndex()) {
Douglas Gregor188bdcd2013-01-25 23:32:03 +00001437 if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
1438 HitsPtr = &Hits;
Douglas Gregor1a49d972013-01-25 01:03:03 +00001439 }
1440 }
1441
Douglas Gregor188bdcd2013-01-25 23:32:03 +00001442 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
Douglas Gregore1698072013-01-25 00:38:33 +00001443 NumIdentifierLookups,
1444 NumIdentifierLookupHits);
Douglas Gregor188bdcd2013-01-25 23:32:03 +00001445 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor, HitsPtr);
Guy Benyei7f92f2d2012-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 Kyrtzidis9317ab92013-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 Kyrtzidisc56fff72013-03-26 17:17:01 +00001483 MacroDirective *MD = PP.AllocateDefMacroDirective(MI, ImportLoc,
1484 /*isImported=*/true);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001485
1486 // Determine whether this macro definition is visible.
1487 bool Hidden = false;
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00001488 Module *Owner = 0;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001489 if (SubModID) {
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00001490 if ((Owner = getSubmodule(SubModID))) {
Argyrios Kyrtzidis9317ab92013-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 Kyrtzidis9317ab92013-03-22 21:12:57 +00001502
1503 if (!Hidden)
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00001504 installImportedMacro(II, MD, Owner);
Argyrios Kyrtzidis9317ab92013-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 Kyrtzidisc56fff72013-03-26 17:17:01 +00001534 MacroDirective *MD = 0;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001535 SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
Argyrios Kyrtzidisc56fff72013-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 Kyrtzidis9317ab92013-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 Kyrtzidis52151fd2013-03-27 01:25:34 +00001569/// \brief For the given macro definitions, check if they are both in system
Douglas Gregor7332ae42013-04-12 21:00:54 +00001570/// modules.
1571static bool areDefinedInSystemModules(MacroInfo *PrevMI, MacroInfo *NewMI,
1572 Module *NewOwner, ASTReader &Reader) {
Argyrios Kyrtzidis52151fd2013-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 Gregor7332ae42013-04-12 21:00:54 +00001583 return PrevOwner->IsSystem && NewOwner->IsSystem;
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00001584}
1585
1586void ASTReader::installImportedMacro(IdentifierInfo *II, MacroDirective *MD,
1587 Module *Owner) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001588 assert(II && MD);
1589
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001590 DefMacroDirective *DefMD = cast<DefMacroDirective>(MD);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001591 MacroDirective *Prev = PP.getMacroDirective(II);
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001592 if (Prev) {
1593 MacroDirective::DefInfo PrevDef = Prev->getDefinition();
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00001594 MacroInfo *PrevMI = PrevDef.getMacroInfo();
1595 MacroInfo *NewMI = DefMD->getInfo();
Argyrios Kyrtzidisbd25ff82013-04-03 17:39:30 +00001596 if (NewMI != PrevMI && !PrevMI->isIdenticalTo(*NewMI, PP,
1597 /*Syntactically=*/true)) {
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00001598 // Before marking the macros as ambiguous, check if this is a case where
Douglas Gregor7332ae42013-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 Kyrtzidis52151fd2013-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 Gregor7332ae42013-04-12 21:00:54 +00001604 if (!areDefinedInSystemModules(PrevMI, NewMI, Owner, *this)) {
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00001605 PrevDef.getDirective()->setAmbiguous(true);
1606 DefMD->setAmbiguous(true);
1607 }
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001608 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001609 }
1610
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001611 PP.appendMacroDirective(II, MD);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001612}
1613
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00001614InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
Guy Benyei7f92f2d2012-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 Kyrtzidis8504b7b2013-03-01 03:26:04 +00001620 if (F.InputFilesLoaded[ID-1].getFile())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001621 return F.InputFilesLoaded[ID-1];
1622
1623 // Go find this input file.
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001624 BitstreamCursor &Cursor = F.InputFilesCursor;
Guy Benyei7f92f2d2012-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 Lattnerb3ce3572013-01-20 02:38:54 +00001630 StringRef Blob;
1631 switch ((InputFileRecordTypes)Cursor.readRecord(Code, Record, &Blob)) {
Guy Benyei7f92f2d2012-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 Lattnerb3ce3572013-01-20 02:38:54 +00001641 StringRef OrigFilename = Blob;
Guy Benyei7f92f2d2012-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 Kyrtzidis8504b7b2013-03-01 03:26:04 +00001674
Guy Benyei7f92f2d2012-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 Kyrtzidis8504b7b2013-03-01 03:26:04 +00001681 if (Complain)
1682 Error(diag::err_fe_pch_file_overridden, Filename);
Guy Benyei7f92f2d2012-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 Kyrtzidis8504b7b2013-03-01 03:26:04 +00001693 bool IsOutOfDate = false;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001694
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00001695 // For an overridden file, there is nothing to validate.
1696 if (!Overridden && (StoredSize != File->getSize()
Guy Benyei7f92f2d2012-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 Gregor677e15f2013-03-19 00:28:20 +00001704 if (Complain) {
Argyrios Kyrtzidisf8f373f2013-03-08 20:42:38 +00001705 Error(diag::err_fe_pch_file_modified, Filename, F.FileName);
Douglas Gregor677e15f2013-03-19 00:28:20 +00001706 }
1707
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00001708 IsOutOfDate = true;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001709 }
1710
Argyrios Kyrtzidis8504b7b2013-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 Benyei7f92f2d2012-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 Gribenkocfa88f82013-01-12 19:30:44 +00001766 SmallVectorImpl<ImportedModule> &Loaded,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001767 unsigned ClientLoadCapabilities) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001768 BitstreamCursor &Stream = F.Stream;
Guy Benyei7f92f2d2012-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 Lattner88bde502013-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 Kyrtzidis398253a2013-03-06 18:12:50 +00001785 // Validate all of the non-system input files.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001786 if (!DisableValidation) {
1787 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
Argyrios Kyrtzidis398253a2013-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 Kyrtzidis8504b7b2013-03-01 03:26:04 +00001791 InputFile IF = getInputFile(F, I+1, Complain);
1792 if (!IF.getFile() || IF.isOutOfDate())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001793 return OutOfDate;
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00001794 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001795 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001796 return Success;
Chris Lattner88bde502013-01-19 21:39:22 +00001797
1798 case llvm::BitstreamEntry::SubBlock:
1799 switch (Entry.ID) {
Guy Benyei7f92f2d2012-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 Lattner88bde502013-01-19 21:39:22 +00001809
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001810 default:
Chris Lattner88bde502013-01-19 21:39:22 +00001811 if (Stream.SkipBlock()) {
1812 Error("malformed block record in AST file");
1813 return Failure;
1814 }
1815 continue;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001816 }
Chris Lattner88bde502013-01-19 21:39:22 +00001817
1818 case llvm::BitstreamEntry::Record:
1819 // The interesting case.
1820 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001821 }
1822
1823 // Read and process a record.
1824 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001825 StringRef Blob;
1826 switch ((ControlRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei7f92f2d2012-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 Lattnerb3ce3572013-01-20 02:38:54 +00001844 StringRef ASTBranch = Blob;
Guy Benyei7f92f2d2012-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 Gregor677e15f2013-03-19 00:28:20 +00001864 off_t StoredSize = (off_t)Record[Idx++];
1865 time_t StoredModTime = (time_t)Record[Idx++];
Guy Benyei7f92f2d2012-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 Gregor677e15f2013-03-19 00:28:20 +00001873 StoredSize, StoredModTime,
Guy Benyei7f92f2d2012-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 Gregorac39f132013-03-19 00:38:50 +00001877 case Missing:
Guy Benyei7f92f2d2012-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 Lattnerb3ce3572013-01-20 02:38:54 +00001945 F.ActualOriginalSourceFileName = Blob;
Guy Benyei7f92f2d2012-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 Lattnerb3ce3572013-01-20 02:38:54 +00001955 F.OriginalDir = Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001956 break;
1957
1958 case INPUT_FILE_OFFSETS:
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001959 F.InputFileOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001960 F.InputFilesLoaded.resize(Record[0]);
1961 break;
1962 }
1963 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001964}
1965
1966bool ASTReader::ReadASTBlock(ModuleFile &F) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001967 BitstreamCursor &Stream = F.Stream;
Guy Benyei7f92f2d2012-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 Lattner88bde502013-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 Smith43828672013-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 Benyei7f92f2d2012-12-18 14:30:41 +00001989 DeclContext *DC = Context.getTranslationUnitDecl();
Richard Smith43828672013-04-03 22:49:41 +00001990 if (DC->hasExternalLexicalStorage() &&
1991 !getContext().getLangOpts().CPlusPlus)
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001992 DC->setMustBuildLookupTable();
Chris Lattner88bde502013-01-19 21:39:22 +00001993
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001994 return false;
1995 }
Chris Lattner88bde502013-01-19 21:39:22 +00001996 case llvm::BitstreamEntry::SubBlock:
1997 switch (Entry.ID) {
Guy Benyei7f92f2d2012-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 Lattner88bde502013-01-19 21:39:22 +00002011
Guy Benyei7f92f2d2012-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 Lattner88bde502013-01-19 21:39:22 +00002018
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002019 case PREPROCESSOR_BLOCK_ID:
2020 F.MacroCursor = Stream;
2021 if (!PP.getExternalSource())
2022 PP.setExternalSource(this);
Chris Lattner88bde502013-01-19 21:39:22 +00002023
Guy Benyei7f92f2d2012-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 Lattner88bde502013-01-19 21:39:22 +00002031
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002032 case PREPROCESSOR_DETAIL_BLOCK_ID:
2033 F.PreprocessorDetailCursor = Stream;
2034 if (Stream.SkipBlock() ||
Chris Lattner88bde502013-01-19 21:39:22 +00002035 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002036 PREPROCESSOR_DETAIL_BLOCK_ID)) {
Chris Lattner88bde502013-01-19 21:39:22 +00002037 Error("malformed preprocessor detail record in AST file");
2038 return true;
2039 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002040 F.PreprocessorDetailStartOffset
Chris Lattner88bde502013-01-19 21:39:22 +00002041 = F.PreprocessorDetailCursor.GetCurrentBitNo();
2042
Guy Benyei7f92f2d2012-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 Lattner88bde502013-01-19 21:39:22 +00002053
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002054 case SUBMODULE_BLOCK_ID:
2055 if (ReadSubmoduleBlock(F))
2056 return true;
2057 break;
Chris Lattner88bde502013-01-19 21:39:22 +00002058
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002059 case COMMENTS_BLOCK_ID: {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00002060 BitstreamCursor C = Stream;
Guy Benyei7f92f2d2012-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 Lattner88bde502013-01-19 21:39:22 +00002069
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002070 default:
Chris Lattner88bde502013-01-19 21:39:22 +00002071 if (Stream.SkipBlock()) {
2072 Error("malformed block record in AST file");
2073 return true;
2074 }
2075 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002076 }
2077 continue;
Chris Lattner88bde502013-01-19 21:39:22 +00002078
2079 case llvm::BitstreamEntry::Record:
2080 // The interesting case.
2081 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002082 }
2083
2084 // Read and process a record.
2085 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002086 StringRef Blob;
2087 switch ((ASTRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei7f92f2d2012-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 Lattnerb3ce3572013-01-20 02:38:54 +00002096 F.TypeOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-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 Lattnerb3ce3572013-01-20 02:38:54 +00002120 F.DeclOffsets = (const DeclOffset *)Blob.data();
Guy Benyei7f92f2d2012-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 Lattnerb3ce3572013-01-20 02:38:54 +00002148 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(Blob.data());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002149 Info.NumLexicalDecls
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002150 = static_cast<unsigned int>(Blob.size() / sizeof(KindDeclIDPair));
Guy Benyei7f92f2d2012-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 Lattnerb3ce3572013-01-20 02:38:54 +00002160 (const unsigned char *)Blob.data() + Record[Idx++],
2161 (const unsigned char *)Blob.data(),
Guy Benyei7f92f2d2012-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 Lattnerb3ce3572013-01-20 02:38:54 +00002173 F.IdentifierTableData = Blob.data();
Guy Benyei7f92f2d2012-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 Lattnerb3ce3572013-01-20 02:38:54 +00002190 F.IdentifierOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-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 Gregorf5cfc892013-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 Benyei7f92f2d2012-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 Smith5ea6ef42013-01-10 23:43:47 +00002278 case LOCALLY_SCOPED_EXTERN_C_DECLS:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002279 for (unsigned I = 0, N = Record.size(); I != N; ++I)
Richard Smith5ea6ef42013-01-10 23:43:47 +00002280 LocallyScopedExternCDecls.push_back(getGlobalDeclID(F, Record[I]));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002281 break;
2282
2283 case SELECTOR_OFFSETS: {
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002284 F.SelectorOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-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 Lattnerb3ce3572013-01-20 02:38:54 +00002306 F.SelectorLookupTableData = (const unsigned char *)Blob.data();
Guy Benyei7f92f2d2012-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 Lattnerb3ce3572013-01-20 02:38:54 +00002333 F.FileSortedDecls = (const DeclID *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002334 F.NumFileSortedDecls = Record[0];
2335 break;
2336
2337 case SOURCE_LOCATION_OFFSETS: {
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002338 F.SLocEntryOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-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 Lattnerb3ce3572013-01-20 02:38:54 +00002371 const unsigned char *Data = (const unsigned char*)Blob.data();
2372 const unsigned char *DataEnd = Data + Blob.size();
Guy Benyei7f92f2d2012-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 Lattnerb3ce3572013-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 Benyei7f92f2d2012-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 Lattnerb3ce3572013-01-20 02:38:54 +00002568 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
Guy Benyei7f92f2d2012-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 Lattnerb3ce3572013-01-20 02:38:54 +00002583 F.CXXBaseSpecifiersOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-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 Lattnerb3ce3572013-01-20 02:38:54 +00002605 F.HeaderFileInfoTableData = Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002606 F.LocalNumHeaderFileInfos = Record[1];
Guy Benyei7f92f2d2012-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 Lattnerb3ce3572013-01-20 02:38:54 +00002614 Blob.data() + Record[2]));
Guy Benyei7f92f2d2012-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 Lewycky01a41142013-01-26 00:35:08 +00002642
Nick Lewyckycd0655b2013-02-01 08:13:20 +00002643 case UNDEFINED_BUT_USED:
2644 if (UndefinedButUsed.size() % 2 != 0) {
2645 Error("Invalid existing UndefinedButUsed");
Nick Lewycky01a41142013-01-26 00:35:08 +00002646 return true;
2647 }
2648
2649 if (Record.size() % 2 != 0) {
Nick Lewyckycd0655b2013-02-01 08:13:20 +00002650 Error("invalid undefined-but-used record");
Nick Lewycky01a41142013-01-26 00:35:08 +00002651 return true;
2652 }
2653 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
Nick Lewyckycd0655b2013-02-01 08:13:20 +00002654 UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
2655 UndefinedButUsed.push_back(
Nick Lewycky01a41142013-01-26 00:35:08 +00002656 ReadSourceLocation(F, Record, I).getRawEncoding());
2657 }
2658 break;
2659
Guy Benyei7f92f2d2012-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 Lattnerb3ce3572013-01-20 02:38:54 +00002685 F.RedeclarationsMap = (const LocalRedeclarationsInfo *)Blob.data();
Guy Benyei7f92f2d2012-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 Lattnerb3ce3572013-01-20 02:38:54 +00002704 F.MacroOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-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 Kyrtzidis9317ab92013-03-22 21:12:57 +00002723 case MACRO_TABLE: {
2724 // FIXME: Not used yet.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002725 break;
2726 }
2727 }
2728 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002729}
2730
Douglas Gregor2cbd4272013-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 Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002743 for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
Douglas Gregor2cbd4272013-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 Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002753 if (List->getNext())
2754 List->Method = List->getNext()->Method;
Douglas Gregor2cbd4272013-02-12 23:36:21 +00002755 else
2756 List->Method = Method;
2757 }
2758}
2759
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00002760void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002761 for (unsigned I = 0, N = Names.size(); I != N; ++I) {
2762 switch (Names[I].getKind()) {
Douglas Gregor2cbd4272013-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 Benyei7f92f2d2012-12-18 14:30:41 +00002767
Douglas Gregor2cbd4272013-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 Benyei7f92f2d2012-12-18 14:30:41 +00002775 case HiddenName::MacroVisibility: {
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00002776 std::pair<IdentifierInfo *, MacroDirective *> Macro = Names[I].getMacro();
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00002777 installImportedMacro(Macro.first, Macro.second, Owner);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002778 break;
2779 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002780 }
2781 }
2782}
2783
2784void ASTReader::makeModuleVisible(Module *Mod,
Argyrios Kyrtzidis5ebcb202013-02-01 16:36:12 +00002785 Module::NameVisibilityKind NameVisibility,
Douglas Gregor906d66a2013-03-20 21:10:35 +00002786 SourceLocation ImportLoc,
2787 bool Complain) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002788 llvm::SmallPtrSet<Module *, 4> Visited;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002789 SmallVector<Module *, 4> Stack;
Guy Benyei7f92f2d2012-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 Kyrtzidis52151fd2013-03-27 01:25:34 +00002813 makeNamesVisible(Hidden->second, Hidden->first);
Guy Benyei7f92f2d2012-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 Kyrtzidis21a00042013-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 Benyei7f92f2d2012-12-18 14:30:41 +00002834 }
Douglas Gregor906d66a2013-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 Benyei7f92f2d2012-12-18 14:30:41 +00002849 }
2850}
2851
Douglas Gregor1a49d972013-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 Gregor677e15f2013-03-19 00:28:20 +00002865 = GlobalModuleIndex::readIndex(ModuleCachePath);
Douglas Gregor1a49d972013-01-25 01:03:03 +00002866 if (!Result.first)
2867 return true;
2868
2869 GlobalIndex.reset(Result.first);
Douglas Gregor188bdcd2013-01-25 23:32:03 +00002870 ModuleMgr.setGlobalIndex(GlobalIndex.get());
Douglas Gregor1a49d972013-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 Benyei7f92f2d2012-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 Gribenkocfa88f82013-01-12 19:30:44 +00002887 SmallVector<ImportedModule, 4> Loaded;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002888 switch(ASTReadResult ReadResult = ReadASTCore(FileName, Type, ImportLoc,
2889 /*ImportedBy=*/0, Loaded,
Douglas Gregor677e15f2013-03-19 00:28:20 +00002890 0, 0,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002891 ClientLoadCapabilities)) {
2892 case Failure:
Douglas Gregor677e15f2013-03-19 00:28:20 +00002893 case Missing:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002894 case OutOfDate:
2895 case VersionMismatch:
2896 case ConfigurationMismatch:
2897 case HadErrors:
Douglas Gregor677e15f2013-03-19 00:28:20 +00002898 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, ModuleMgr.end(),
2899 Context.getLangOpts().Modules
2900 ? &PP.getHeaderSearchInfo().getModuleMap()
2901 : 0);
Douglas Gregor1a49d972013-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 Gregor188bdcd2013-01-25 23:32:03 +00002906 ModuleMgr.setGlobalIndex(0);
Guy Benyei7f92f2d2012-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 Gribenkocfa88f82013-01-12 19:30:44 +00002916 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
2917 MEnd = Loaded.end();
Guy Benyei7f92f2d2012-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 Gregorfa69fc12013-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 Gribenkocfa88f82013-01-12 19:30:44 +00002944 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
2945 MEnd = Loaded.end();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002946 M != MEnd; ++M) {
2947 ModuleFile &F = *M->Mod;
Douglas Gregorfa69fc12013-03-22 18:50:14 +00002948
2949 ModuleMgr.moduleFileAccepted(&F);
2950
2951 // Set the import location.
Argyrios Kyrtzidis8b136d82013-02-01 16:36:14 +00002952 F.DirectImportLoc = ImportLoc;
Guy Benyei7f92f2d2012-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 Gregor906d66a2013-03-20 21:10:35 +00002969 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
2970 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002971 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
2972 Module *ResolvedMod = getSubmodule(GlobalID);
Douglas Gregor906d66a2013-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 Benyei7f92f2d2012-12-18 14:30:41 +00002985 if (ResolvedMod)
2986 Unresolved.Mod->Imports.push_back(ResolvedMod);
2987 continue;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002988
Douglas Gregor906d66a2013-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 Benyei7f92f2d2012-12-18 14:30:41 +00002995 }
Douglas Gregor906d66a2013-03-20 21:10:35 +00002996 UnresolvedModuleRefs.clear();
Guy Benyei7f92f2d2012-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 Gregor1a49d972013-01-25 01:03:03 +00003026
Guy Benyei7f92f2d2012-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 Gribenkocfa88f82013-01-12 19:30:44 +00003035 SmallVectorImpl<ImportedModule> &Loaded,
Douglas Gregor677e15f2013-03-19 00:28:20 +00003036 off_t ExpectedSize, time_t ExpectedModTime,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003037 unsigned ClientLoadCapabilities) {
3038 ModuleFile *M;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003039 std::string ErrorStr;
Douglas Gregor677e15f2013-03-19 00:28:20 +00003040 ModuleManager::AddModuleResult AddResult
3041 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
3042 CurrentGeneration, ExpectedSize, ExpectedModTime,
3043 M, ErrorStr);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003044
Douglas Gregor677e15f2013-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 Benyei7f92f2d2012-12-18 14:30:41 +00003079 return Failure;
3080 }
3081
Douglas Gregor677e15f2013-03-19 00:28:20 +00003082 assert(M && "Missing module file");
Guy Benyei7f92f2d2012-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 Lattner8f9a1eb2013-01-20 00:56:42 +00003092 BitstreamCursor &Stream = F.Stream;
Guy Benyei7f92f2d2012-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 Lattner99a5af02013-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 Benyei7f92f2d2012-12-18 14:30:41 +00003115 Error("invalid record at top-level of AST file");
3116 return Failure;
Chris Lattner99a5af02013-01-20 00:00:22 +00003117
3118 case llvm::BitstreamEntry::SubBlock:
3119 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003120 }
3121
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003122 // We only know the control subblock ID.
Chris Lattner99a5af02013-01-20 00:00:22 +00003123 switch (Entry.ID) {
Guy Benyei7f92f2d2012-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 Gregor677e15f2013-03-19 00:28:20 +00003137 case Missing: return Missing;
Guy Benyei7f92f2d2012-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 Kyrtzidis5ebcb202013-02-01 16:36:12 +00003297 makeModuleVisible(Imported, Module::AllVisible,
Douglas Gregor906d66a2013-03-20 21:10:35 +00003298 /*ImportLoc=*/SourceLocation(),
3299 /*Complain=*/false);
Guy Benyei7f92f2d2012-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 Kyrtzidis52151fd2013-03-27 01:25:34 +00003308 makeNamesVisible(Hidden->second, Hidden->first);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003309 }
3310 HiddenNamesMap.clear();
3311}
3312
Chris Lattner8f9a1eb2013-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 Benyei7f92f2d2012-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 Lattner8f9a1eb2013-01-20 00:56:42 +00003360 BitstreamCursor Stream;
Guy Benyei7f92f2d2012-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 Lattner8f9a1eb2013-01-20 00:56:42 +00003373
Chris Lattner88bde502013-01-19 21:39:22 +00003374 // Scan for the CONTROL_BLOCK_ID block.
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003375 if (SkipCursorToControlBlock(Stream)) {
3376 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
3377 return std::string();
Chris Lattner88bde502013-01-19 21:39:22 +00003378 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003379
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003380 // Scan for ORIGINAL_FILE inside the control block.
3381 RecordData Record;
Chris Lattner88bde502013-01-19 21:39:22 +00003382 while (1) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003383 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Chris Lattner88bde502013-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 Benyei7f92f2d2012-12-18 14:30:41 +00003390 }
Chris Lattner88bde502013-01-19 21:39:22 +00003391
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003392 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003393 StringRef Blob;
3394 if (Stream.readRecord(Entry.ID, Record, &Blob) == ORIGINAL_FILE)
3395 return Blob.str();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003396 }
Guy Benyei7f92f2d2012-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 Lattner8f9a1eb2013-01-20 00:56:42 +00003448 BitstreamCursor Stream;
Guy Benyei7f92f2d2012-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 Lattner8f9a1eb2013-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 Benyei7f92f2d2012-12-18 14:30:41 +00003466 RecordData Record;
Chris Lattner8f9a1eb2013-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 Benyei7f92f2d2012-12-18 14:30:41 +00003475 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003476 StringRef Blob;
3477 unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob);
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003478 switch ((ControlRecordTypes)RecCode) {
3479 case METADATA: {
3480 if (Record[0] != VERSION_MAJOR)
3481 return true;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003482
Douglas Gregorc544ba02013-03-27 16:47:18 +00003483 if (Listener.ReadFullVersionInformation(Blob))
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003484 return true;
Douglas Gregorc544ba02013-03-27 16:47:18 +00003485
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003486 break;
3487 }
3488 case LANGUAGE_OPTIONS:
3489 if (ParseLanguageOptions(Record, false, Listener))
3490 return true;
3491 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003492
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003493 case TARGET_OPTIONS:
3494 if (ParseTargetOptions(Record, false, Listener))
3495 return true;
3496 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003497
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003498 case DIAGNOSTIC_OPTIONS:
3499 if (ParseDiagnosticOptions(Record, false, Listener))
3500 return true;
3501 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003502
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003503 case FILE_SYSTEM_OPTIONS:
3504 if (ParseFileSystemOptions(Record, false, Listener))
3505 return true;
3506 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003507
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003508 case HEADER_SEARCH_OPTIONS:
3509 if (ParseHeaderSearchOptions(Record, false, Listener))
3510 return true;
3511 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003512
Chris Lattner8f9a1eb2013-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 Benyei7f92f2d2012-12-18 14:30:41 +00003520
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003521 default:
3522 // No other validation to perform.
3523 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003524 }
3525 }
Guy Benyei7f92f2d2012-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 Lattner8f9a1eb2013-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 Benyei7f92f2d2012-12-18 14:30:41 +00003558 return false;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003559 case llvm::BitstreamEntry::Record:
3560 // The interesting case.
3561 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003562 }
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003563
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003564 // Read a record.
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003565 StringRef Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003566 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003567 switch (F.Stream.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei7f92f2d2012-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 Gregor970e4412013-03-20 03:59:18 +00003577 if (Record.size() < 8) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003578 Error("malformed module definition");
3579 return true;
3580 }
3581
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003582 StringRef Name = Blob;
Guy Benyei7f92f2d2012-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 Gregor970e4412013-03-20 03:59:18 +00003591 bool ConfigMacrosExhaustive = Record[8];
3592
Guy Benyei7f92f2d2012-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 Gregor8bf778e2013-02-06 22:40:31 +00003608
Douglas Gregor677e15f2013-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 Gregor8bf778e2013-02-06 22:40:31 +00003619 }
Douglas Gregor8bf778e2013-02-06 22:40:31 +00003620 }
Douglas Gregor677e15f2013-03-19 00:28:20 +00003621
3622 CurrentModule->setASTFile(F.File);
Douglas Gregor8bf778e2013-02-06 22:40:31 +00003623 }
Douglas Gregor677e15f2013-03-19 00:28:20 +00003624
Guy Benyei7f92f2d2012-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 Gregor970e4412013-03-20 03:59:18 +00003630 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003631 if (DeserializationListener)
3632 DeserializationListener->ModuleRead(GlobalID, CurrentModule);
3633
3634 SubmodulesLoaded[GlobalIndex] = CurrentModule;
Douglas Gregorb6cbe512013-01-14 17:21:00 +00003635
Douglas Gregor906d66a2013-03-20 21:10:35 +00003636 // Clear out data that will be replaced by what is the module file.
Douglas Gregorb6cbe512013-01-14 17:21:00 +00003637 CurrentModule->LinkLibraries.clear();
Douglas Gregor970e4412013-03-20 03:59:18 +00003638 CurrentModule->ConfigMacros.clear();
Douglas Gregor906d66a2013-03-20 21:10:35 +00003639 CurrentModule->UnresolvedConflicts.clear();
3640 CurrentModule->Conflicts.clear();
Guy Benyei7f92f2d2012-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 Lattnerb3ce3572013-01-20 02:38:54 +00003653 if (const FileEntry *Umbrella = PP.getFileManager().getFile(Blob)) {
Guy Benyei7f92f2d2012-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 Kyrtzidis55ea75b2013-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 Benyei7f92f2d2012-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 Kyrtzidis55ea75b2013-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 Benyei7f92f2d2012-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 Kyrtzidisc1d22392013-03-13 21:13:43 +00003703 CurrentModule->addTopHeaderFilename(Blob);
Guy Benyei7f92f2d2012-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 Benyei7f92f2d2012-12-18 14:30:41 +00003716 if (const DirectoryEntry *Umbrella
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003717 = PP.getFileManager().getDirectory(Blob)) {
Guy Benyei7f92f2d2012-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 Gregor906d66a2013-03-20 21:10:35 +00003764 UnresolvedModuleRef Unresolved;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003765 Unresolved.File = &F;
3766 Unresolved.Mod = CurrentModule;
3767 Unresolved.ID = Record[Idx];
Douglas Gregor906d66a2013-03-20 21:10:35 +00003768 Unresolved.Kind = UnresolvedModuleRef::Import;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003769 Unresolved.IsWildcard = false;
Douglas Gregor906d66a2013-03-20 21:10:35 +00003770 UnresolvedModuleRefs.push_back(Unresolved);
Guy Benyei7f92f2d2012-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 Gregor906d66a2013-03-20 21:10:35 +00003785 UnresolvedModuleRef Unresolved;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003786 Unresolved.File = &F;
3787 Unresolved.Mod = CurrentModule;
3788 Unresolved.ID = Record[Idx];
Douglas Gregor906d66a2013-03-20 21:10:35 +00003789 Unresolved.Kind = UnresolvedModuleRef::Export;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003790 Unresolved.IsWildcard = Record[Idx + 1];
Douglas Gregor906d66a2013-03-20 21:10:35 +00003791 UnresolvedModuleRefs.push_back(Unresolved);
Guy Benyei7f92f2d2012-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 Lattnerb3ce3572013-01-20 02:38:54 +00003808 CurrentModule->addRequirement(Blob, Context.getLangOpts(),
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003809 Context.getTargetInfo());
3810 break;
3811 }
Douglas Gregorb6cbe512013-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 Lattnerb3ce3572013-01-20 02:38:54 +00003823 Module::LinkLibrary(Blob, Record[0]));
Douglas Gregorb6cbe512013-01-14 17:21:00 +00003824 break;
Douglas Gregor63a72682013-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 Gregor906d66a2013-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 Benyei7f92f2d2012-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 Dietz4f45bc02013-01-18 11:30:38 +00003878#define SANITIZER(NAME, ID) LangOpts.Sanitize.ID = Record[Idx++];
3879#include "clang/Basic/Sanitizers.def"
Guy Benyei7f92f2d2012-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 Gribenko6ebf0912013-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 Gribenko6fd7d302013-04-10 15:35:17 +00003896 LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
Dmitri Gribenko6ebf0912013-02-22 14:21:27 +00003897
Guy Benyei7f92f2d2012-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 Benyei7f92f2d2012-12-18 14:30:41 +00003957 bool IsFramework = Record[Idx++];
3958 bool IgnoreSysRoot = Record[Idx++];
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003959 HSOpts.UserEntries.push_back(
Daniel Dunbar59fd6352013-01-30 00:34:26 +00003960 HeaderSearchOptions::Entry(Path, Group, IsFramework, IgnoreSysRoot));
Guy Benyei7f92f2d2012-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 Benyei7f92f2d2012-12-18 14:30:41 +00004051 if (!PP.getPreprocessingRecord()) {
4052 Error("no preprocessing record");
4053 return 0;
4054 }
4055
Chris Lattner8f9a1eb2013-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 Benyei7f92f2d2012-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 Lattnerb3ce3572013-01-20 02:38:54 +00004068 StringRef Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004069 RecordData Record;
4070 PreprocessorDetailRecordTypes RecType =
Chris Lattnerb3ce3572013-01-20 02:38:54 +00004071 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.readRecord(
4072 Entry.ID, Record, &Blob);
Guy Benyei7f92f2d2012-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 Lattnerb3ce3572013-01-20 02:38:54 +00004109 const char *FullFileNameStart = Blob.data() + Record[0];
4110 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
Guy Benyei7f92f2d2012-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 Lattnerb3ce3572013-01-20 02:38:54 +00004120 StringRef(Blob.data(), Record[0]),
Guy Benyei7f92f2d2012-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 Kyrtzidisee2d5fd2013-03-08 02:32:34 +00004188 BLoc.getOffset() - 1);
Guy Benyei7f92f2d2012-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 Kyrtzidisee2d5fd2013-03-08 02:32:34 +00004236 ELoc.getOffset() - 1);
Guy Benyei7f92f2d2012-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 Blaikiedc84cd52013-02-20 22:23:23 +00004272Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
Guy Benyei7f92f2d2012-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 Benyei7f92f2d2012-12-18 14:30:41 +00004295 const FileEntry *FE;
4296
David Blaikiedc84cd52013-02-20 22:23:23 +00004297 Optional<HeaderFileInfo> HFI;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004298
4299 public:
Argyrios Kyrtzidis36592b12013-03-06 18:12:44 +00004300 explicit HeaderFileInfoVisitor(const FileEntry *FE)
4301 : FE(FE) { }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004302
4303 static bool visit(ModuleFile &M, void *UserData) {
4304 HeaderFileInfoVisitor *This
4305 = static_cast<HeaderFileInfoVisitor *>(UserData);
4306
Guy Benyei7f92f2d2012-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 Kyrtzidised3802e2013-03-06 18:12:47 +00004313 HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004314 if (Pos == Table->end())
4315 return false;
4316
4317 This->HFI = *Pos;
4318 return true;
4319 }
4320
David Blaikiedc84cd52013-02-20 22:23:23 +00004321 Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004322 };
4323}
4324
4325HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
Argyrios Kyrtzidis36592b12013-03-06 18:12:44 +00004326 HeaderFileInfoVisitor Visitor(FE);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004327 ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor);
David Blaikiedc84cd52013-02-20 22:23:23 +00004328 if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) {
Guy Benyei7f92f2d2012-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 Gribenkocfa88f82013-01-12 19:30:44 +00004339 SmallVector<DiagnosticsEngine::DiagState *, 32> DiagStates;
Guy Benyei7f92f2d2012-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 Lattner8f9a1eb2013-01-20 00:56:42 +00004399 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Guy Benyei7f92f2d2012-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 Lattnerb3ce3572013-01-20 02:38:54 +00004414 switch ((TypeCode)DeclsCursor.readRecord(Code, Record)) {
Guy Benyei7f92f2d2012-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 Rosebea522f2013-03-08 21:51:21 +00004583 return Context.getFunctionType(ResultType, ParamTypes, EPI);
Guy Benyei7f92f2d2012-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
Richard Smitha2c36462013-04-26 16:15:35 +00004629 case TYPE_AUTO: {
4630 QualType Deduced = readType(*Loc.F, Record, Idx);
4631 bool IsDecltypeAuto = Record[Idx++];
4632 return Context.getAutoType(Deduced, IsDecltypeAuto);
4633 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004634
4635 case TYPE_RECORD: {
4636 if (Record.size() != 2) {
4637 Error("incorrect encoding of record type");
4638 return QualType();
4639 }
4640 unsigned Idx = 0;
4641 bool IsDependent = Record[Idx++];
4642 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
4643 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
4644 QualType T = Context.getRecordType(RD);
4645 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
4646 return T;
4647 }
4648
4649 case TYPE_ENUM: {
4650 if (Record.size() != 2) {
4651 Error("incorrect encoding of enum type");
4652 return QualType();
4653 }
4654 unsigned Idx = 0;
4655 bool IsDependent = Record[Idx++];
4656 QualType T
4657 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
4658 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
4659 return T;
4660 }
4661
4662 case TYPE_ATTRIBUTED: {
4663 if (Record.size() != 3) {
4664 Error("incorrect encoding of attributed type");
4665 return QualType();
4666 }
4667 QualType modifiedType = readType(*Loc.F, Record, Idx);
4668 QualType equivalentType = readType(*Loc.F, Record, Idx);
4669 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
4670 return Context.getAttributedType(kind, modifiedType, equivalentType);
4671 }
4672
4673 case TYPE_PAREN: {
4674 if (Record.size() != 1) {
4675 Error("incorrect encoding of paren type");
4676 return QualType();
4677 }
4678 QualType InnerType = readType(*Loc.F, Record, Idx);
4679 return Context.getParenType(InnerType);
4680 }
4681
4682 case TYPE_PACK_EXPANSION: {
4683 if (Record.size() != 2) {
4684 Error("incorrect encoding of pack expansion type");
4685 return QualType();
4686 }
4687 QualType Pattern = readType(*Loc.F, Record, Idx);
4688 if (Pattern.isNull())
4689 return QualType();
David Blaikiedc84cd52013-02-20 22:23:23 +00004690 Optional<unsigned> NumExpansions;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004691 if (Record[1])
4692 NumExpansions = Record[1] - 1;
4693 return Context.getPackExpansionType(Pattern, NumExpansions);
4694 }
4695
4696 case TYPE_ELABORATED: {
4697 unsigned Idx = 0;
4698 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
4699 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
4700 QualType NamedType = readType(*Loc.F, Record, Idx);
4701 return Context.getElaboratedType(Keyword, NNS, NamedType);
4702 }
4703
4704 case TYPE_OBJC_INTERFACE: {
4705 unsigned Idx = 0;
4706 ObjCInterfaceDecl *ItfD
4707 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
4708 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
4709 }
4710
4711 case TYPE_OBJC_OBJECT: {
4712 unsigned Idx = 0;
4713 QualType Base = readType(*Loc.F, Record, Idx);
4714 unsigned NumProtos = Record[Idx++];
4715 SmallVector<ObjCProtocolDecl*, 4> Protos;
4716 for (unsigned I = 0; I != NumProtos; ++I)
4717 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
4718 return Context.getObjCObjectType(Base, Protos.data(), NumProtos);
4719 }
4720
4721 case TYPE_OBJC_OBJECT_POINTER: {
4722 unsigned Idx = 0;
4723 QualType Pointee = readType(*Loc.F, Record, Idx);
4724 return Context.getObjCObjectPointerType(Pointee);
4725 }
4726
4727 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
4728 unsigned Idx = 0;
4729 QualType Parm = readType(*Loc.F, Record, Idx);
4730 QualType Replacement = readType(*Loc.F, Record, Idx);
4731 return
4732 Context.getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
4733 Replacement);
4734 }
4735
4736 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
4737 unsigned Idx = 0;
4738 QualType Parm = readType(*Loc.F, Record, Idx);
4739 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
4740 return Context.getSubstTemplateTypeParmPackType(
4741 cast<TemplateTypeParmType>(Parm),
4742 ArgPack);
4743 }
4744
4745 case TYPE_INJECTED_CLASS_NAME: {
4746 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
4747 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
4748 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
4749 // for AST reading, too much interdependencies.
4750 return
4751 QualType(new (Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
4752 }
4753
4754 case TYPE_TEMPLATE_TYPE_PARM: {
4755 unsigned Idx = 0;
4756 unsigned Depth = Record[Idx++];
4757 unsigned Index = Record[Idx++];
4758 bool Pack = Record[Idx++];
4759 TemplateTypeParmDecl *D
4760 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
4761 return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
4762 }
4763
4764 case TYPE_DEPENDENT_NAME: {
4765 unsigned Idx = 0;
4766 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
4767 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
4768 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
4769 QualType Canon = readType(*Loc.F, Record, Idx);
4770 if (!Canon.isNull())
4771 Canon = Context.getCanonicalType(Canon);
4772 return Context.getDependentNameType(Keyword, NNS, Name, Canon);
4773 }
4774
4775 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
4776 unsigned Idx = 0;
4777 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
4778 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
4779 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
4780 unsigned NumArgs = Record[Idx++];
4781 SmallVector<TemplateArgument, 8> Args;
4782 Args.reserve(NumArgs);
4783 while (NumArgs--)
4784 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
4785 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
4786 Args.size(), Args.data());
4787 }
4788
4789 case TYPE_DEPENDENT_SIZED_ARRAY: {
4790 unsigned Idx = 0;
4791
4792 // ArrayType
4793 QualType ElementType = readType(*Loc.F, Record, Idx);
4794 ArrayType::ArraySizeModifier ASM
4795 = (ArrayType::ArraySizeModifier)Record[Idx++];
4796 unsigned IndexTypeQuals = Record[Idx++];
4797
4798 // DependentSizedArrayType
4799 Expr *NumElts = ReadExpr(*Loc.F);
4800 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
4801
4802 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
4803 IndexTypeQuals, Brackets);
4804 }
4805
4806 case TYPE_TEMPLATE_SPECIALIZATION: {
4807 unsigned Idx = 0;
4808 bool IsDependent = Record[Idx++];
4809 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
4810 SmallVector<TemplateArgument, 8> Args;
4811 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
4812 QualType Underlying = readType(*Loc.F, Record, Idx);
4813 QualType T;
4814 if (Underlying.isNull())
4815 T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(),
4816 Args.size());
4817 else
4818 T = Context.getTemplateSpecializationType(Name, Args.data(),
4819 Args.size(), Underlying);
4820 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
4821 return T;
4822 }
4823
4824 case TYPE_ATOMIC: {
4825 if (Record.size() != 1) {
4826 Error("Incorrect encoding of atomic type");
4827 return QualType();
4828 }
4829 QualType ValueType = readType(*Loc.F, Record, Idx);
4830 return Context.getAtomicType(ValueType);
4831 }
4832 }
4833 llvm_unreachable("Invalid TypeCode!");
4834}
4835
4836class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
4837 ASTReader &Reader;
4838 ModuleFile &F;
4839 const ASTReader::RecordData &Record;
4840 unsigned &Idx;
4841
4842 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
4843 unsigned &I) {
4844 return Reader.ReadSourceLocation(F, R, I);
4845 }
4846
4847 template<typename T>
4848 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
4849 return Reader.ReadDeclAs<T>(F, Record, Idx);
4850 }
4851
4852public:
4853 TypeLocReader(ASTReader &Reader, ModuleFile &F,
4854 const ASTReader::RecordData &Record, unsigned &Idx)
4855 : Reader(Reader), F(F), Record(Record), Idx(Idx)
4856 { }
4857
4858 // We want compile-time assurance that we've enumerated all of
4859 // these, so unfortunately we have to declare them first, then
4860 // define them out-of-line.
4861#define ABSTRACT_TYPELOC(CLASS, PARENT)
4862#define TYPELOC(CLASS, PARENT) \
4863 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
4864#include "clang/AST/TypeLocNodes.def"
4865
4866 void VisitFunctionTypeLoc(FunctionTypeLoc);
4867 void VisitArrayTypeLoc(ArrayTypeLoc);
4868};
4869
4870void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
4871 // nothing to do
4872}
4873void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
4874 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
4875 if (TL.needsExtraLocalData()) {
4876 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
4877 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
4878 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
4879 TL.setModeAttr(Record[Idx++]);
4880 }
4881}
4882void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
4883 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4884}
4885void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
4886 TL.setStarLoc(ReadSourceLocation(Record, Idx));
4887}
4888void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
4889 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
4890}
4891void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
4892 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
4893}
4894void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
4895 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
4896}
4897void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
4898 TL.setStarLoc(ReadSourceLocation(Record, Idx));
4899 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
4900}
4901void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
4902 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
4903 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
4904 if (Record[Idx++])
4905 TL.setSizeExpr(Reader.ReadExpr(F));
4906 else
4907 TL.setSizeExpr(0);
4908}
4909void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
4910 VisitArrayTypeLoc(TL);
4911}
4912void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
4913 VisitArrayTypeLoc(TL);
4914}
4915void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
4916 VisitArrayTypeLoc(TL);
4917}
4918void TypeLocReader::VisitDependentSizedArrayTypeLoc(
4919 DependentSizedArrayTypeLoc TL) {
4920 VisitArrayTypeLoc(TL);
4921}
4922void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
4923 DependentSizedExtVectorTypeLoc TL) {
4924 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4925}
4926void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
4927 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4928}
4929void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
4930 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4931}
4932void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
4933 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
4934 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4935 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4936 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
4937 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
4938 TL.setArg(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
4939 }
4940}
4941void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
4942 VisitFunctionTypeLoc(TL);
4943}
4944void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
4945 VisitFunctionTypeLoc(TL);
4946}
4947void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
4948 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4949}
4950void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
4951 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4952}
4953void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
4954 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
4955 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4956 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4957}
4958void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
4959 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
4960 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4961 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4962 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
4963}
4964void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
4965 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4966}
4967void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
4968 TL.setKWLoc(ReadSourceLocation(Record, Idx));
4969 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4970 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4971 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
4972}
4973void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
4974 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4975}
4976void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
4977 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4978}
4979void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
4980 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4981}
4982void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
4983 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
4984 if (TL.hasAttrOperand()) {
4985 SourceRange range;
4986 range.setBegin(ReadSourceLocation(Record, Idx));
4987 range.setEnd(ReadSourceLocation(Record, Idx));
4988 TL.setAttrOperandParensRange(range);
4989 }
4990 if (TL.hasAttrExprOperand()) {
4991 if (Record[Idx++])
4992 TL.setAttrExprOperand(Reader.ReadExpr(F));
4993 else
4994 TL.setAttrExprOperand(0);
4995 } else if (TL.hasAttrEnumOperand())
4996 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
4997}
4998void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
4999 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5000}
5001void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
5002 SubstTemplateTypeParmTypeLoc TL) {
5003 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5004}
5005void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
5006 SubstTemplateTypeParmPackTypeLoc TL) {
5007 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5008}
5009void TypeLocReader::VisitTemplateSpecializationTypeLoc(
5010 TemplateSpecializationTypeLoc TL) {
5011 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
5012 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
5013 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5014 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5015 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
5016 TL.setArgLocInfo(i,
5017 Reader.GetTemplateArgumentLocInfo(F,
5018 TL.getTypePtr()->getArg(i).getKind(),
5019 Record, Idx));
5020}
5021void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
5022 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5023 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5024}
5025void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
5026 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5027 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5028}
5029void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
5030 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5031}
5032void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
5033 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5034 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5035 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5036}
5037void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
5038 DependentTemplateSpecializationTypeLoc TL) {
5039 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5040 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5041 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
5042 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
5043 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5044 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5045 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
5046 TL.setArgLocInfo(I,
5047 Reader.GetTemplateArgumentLocInfo(F,
5048 TL.getTypePtr()->getArg(I).getKind(),
5049 Record, Idx));
5050}
5051void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
5052 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
5053}
5054void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
5055 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5056}
5057void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
5058 TL.setHasBaseTypeAsWritten(Record[Idx++]);
5059 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5060 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5061 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
5062 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
5063}
5064void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
5065 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5066}
5067void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
5068 TL.setKWLoc(ReadSourceLocation(Record, Idx));
5069 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5070 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5071}
5072
5073TypeSourceInfo *ASTReader::GetTypeSourceInfo(ModuleFile &F,
5074 const RecordData &Record,
5075 unsigned &Idx) {
5076 QualType InfoTy = readType(F, Record, Idx);
5077 if (InfoTy.isNull())
5078 return 0;
5079
5080 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
5081 TypeLocReader TLR(*this, F, Record, Idx);
5082 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
5083 TLR.Visit(TL);
5084 return TInfo;
5085}
5086
5087QualType ASTReader::GetType(TypeID ID) {
5088 unsigned FastQuals = ID & Qualifiers::FastMask;
5089 unsigned Index = ID >> Qualifiers::FastWidth;
5090
5091 if (Index < NUM_PREDEF_TYPE_IDS) {
5092 QualType T;
5093 switch ((PredefinedTypeIDs)Index) {
5094 case PREDEF_TYPE_NULL_ID: return QualType();
5095 case PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
5096 case PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
5097
5098 case PREDEF_TYPE_CHAR_U_ID:
5099 case PREDEF_TYPE_CHAR_S_ID:
5100 // FIXME: Check that the signedness of CharTy is correct!
5101 T = Context.CharTy;
5102 break;
5103
5104 case PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
5105 case PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
5106 case PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
5107 case PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
5108 case PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
5109 case PREDEF_TYPE_UINT128_ID: T = Context.UnsignedInt128Ty; break;
5110 case PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
5111 case PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
5112 case PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
5113 case PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
5114 case PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
5115 case PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
5116 case PREDEF_TYPE_INT128_ID: T = Context.Int128Ty; break;
5117 case PREDEF_TYPE_HALF_ID: T = Context.HalfTy; break;
5118 case PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
5119 case PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
5120 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
5121 case PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
5122 case PREDEF_TYPE_BOUND_MEMBER: T = Context.BoundMemberTy; break;
5123 case PREDEF_TYPE_PSEUDO_OBJECT: T = Context.PseudoObjectTy; break;
5124 case PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
5125 case PREDEF_TYPE_UNKNOWN_ANY: T = Context.UnknownAnyTy; break;
5126 case PREDEF_TYPE_NULLPTR_ID: T = Context.NullPtrTy; break;
5127 case PREDEF_TYPE_CHAR16_ID: T = Context.Char16Ty; break;
5128 case PREDEF_TYPE_CHAR32_ID: T = Context.Char32Ty; break;
5129 case PREDEF_TYPE_OBJC_ID: T = Context.ObjCBuiltinIdTy; break;
5130 case PREDEF_TYPE_OBJC_CLASS: T = Context.ObjCBuiltinClassTy; break;
5131 case PREDEF_TYPE_OBJC_SEL: T = Context.ObjCBuiltinSelTy; break;
Guy Benyeib13621d2012-12-18 14:38:23 +00005132 case PREDEF_TYPE_IMAGE1D_ID: T = Context.OCLImage1dTy; break;
5133 case PREDEF_TYPE_IMAGE1D_ARR_ID: T = Context.OCLImage1dArrayTy; break;
5134 case PREDEF_TYPE_IMAGE1D_BUFF_ID: T = Context.OCLImage1dBufferTy; break;
5135 case PREDEF_TYPE_IMAGE2D_ID: T = Context.OCLImage2dTy; break;
5136 case PREDEF_TYPE_IMAGE2D_ARR_ID: T = Context.OCLImage2dArrayTy; break;
5137 case PREDEF_TYPE_IMAGE3D_ID: T = Context.OCLImage3dTy; break;
Guy Benyei21f18c42013-02-07 10:55:47 +00005138 case PREDEF_TYPE_SAMPLER_ID: T = Context.OCLSamplerTy; break;
Guy Benyeie6b9d802013-01-20 12:31:11 +00005139 case PREDEF_TYPE_EVENT_ID: T = Context.OCLEventTy; break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005140 case PREDEF_TYPE_AUTO_DEDUCT: T = Context.getAutoDeductType(); break;
5141
5142 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
5143 T = Context.getAutoRRefDeductType();
5144 break;
5145
5146 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
5147 T = Context.ARCUnbridgedCastTy;
5148 break;
5149
5150 case PREDEF_TYPE_VA_LIST_TAG:
5151 T = Context.getVaListTagType();
5152 break;
5153
5154 case PREDEF_TYPE_BUILTIN_FN:
5155 T = Context.BuiltinFnTy;
5156 break;
5157 }
5158
5159 assert(!T.isNull() && "Unknown predefined type");
5160 return T.withFastQualifiers(FastQuals);
5161 }
5162
5163 Index -= NUM_PREDEF_TYPE_IDS;
5164 assert(Index < TypesLoaded.size() && "Type index out-of-range");
5165 if (TypesLoaded[Index].isNull()) {
5166 TypesLoaded[Index] = readTypeRecord(Index);
5167 if (TypesLoaded[Index].isNull())
5168 return QualType();
5169
5170 TypesLoaded[Index]->setFromAST();
5171 if (DeserializationListener)
5172 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
5173 TypesLoaded[Index]);
5174 }
5175
5176 return TypesLoaded[Index].withFastQualifiers(FastQuals);
5177}
5178
5179QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
5180 return GetType(getGlobalTypeID(F, LocalID));
5181}
5182
5183serialization::TypeID
5184ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
5185 unsigned FastQuals = LocalID & Qualifiers::FastMask;
5186 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
5187
5188 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
5189 return LocalID;
5190
5191 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5192 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
5193 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
5194
5195 unsigned GlobalIndex = LocalIndex + I->second;
5196 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
5197}
5198
5199TemplateArgumentLocInfo
5200ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
5201 TemplateArgument::ArgKind Kind,
5202 const RecordData &Record,
5203 unsigned &Index) {
5204 switch (Kind) {
5205 case TemplateArgument::Expression:
5206 return ReadExpr(F);
5207 case TemplateArgument::Type:
5208 return GetTypeSourceInfo(F, Record, Index);
5209 case TemplateArgument::Template: {
5210 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
5211 Index);
5212 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
5213 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
5214 SourceLocation());
5215 }
5216 case TemplateArgument::TemplateExpansion: {
5217 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
5218 Index);
5219 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
5220 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
5221 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
5222 EllipsisLoc);
5223 }
5224 case TemplateArgument::Null:
5225 case TemplateArgument::Integral:
5226 case TemplateArgument::Declaration:
5227 case TemplateArgument::NullPtr:
5228 case TemplateArgument::Pack:
5229 // FIXME: Is this right?
5230 return TemplateArgumentLocInfo();
5231 }
5232 llvm_unreachable("unexpected template argument loc");
5233}
5234
5235TemplateArgumentLoc
5236ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
5237 const RecordData &Record, unsigned &Index) {
5238 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
5239
5240 if (Arg.getKind() == TemplateArgument::Expression) {
5241 if (Record[Index++]) // bool InfoHasSameExpr.
5242 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
5243 }
5244 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
5245 Record, Index));
5246}
5247
5248Decl *ASTReader::GetExternalDecl(uint32_t ID) {
5249 return GetDecl(ID);
5250}
5251
5252uint64_t ASTReader::readCXXBaseSpecifiers(ModuleFile &M, const RecordData &Record,
5253 unsigned &Idx){
5254 if (Idx >= Record.size())
5255 return 0;
5256
5257 unsigned LocalID = Record[Idx++];
5258 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
5259}
5260
5261CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
5262 RecordLocation Loc = getLocalBitOffset(Offset);
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00005263 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005264 SavedStreamPosition SavedPosition(Cursor);
5265 Cursor.JumpToBit(Loc.Offset);
5266 ReadingKindTracker ReadingKind(Read_Decl, *this);
5267 RecordData Record;
5268 unsigned Code = Cursor.ReadCode();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00005269 unsigned RecCode = Cursor.readRecord(Code, Record);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005270 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
5271 Error("Malformed AST file: missing C++ base specifiers");
5272 return 0;
5273 }
5274
5275 unsigned Idx = 0;
5276 unsigned NumBases = Record[Idx++];
5277 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
5278 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
5279 for (unsigned I = 0; I != NumBases; ++I)
5280 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
5281 return Bases;
5282}
5283
5284serialization::DeclID
5285ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
5286 if (LocalID < NUM_PREDEF_DECL_IDS)
5287 return LocalID;
5288
5289 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5290 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
5291 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
5292
5293 return LocalID + I->second;
5294}
5295
5296bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
5297 ModuleFile &M) const {
5298 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(ID);
5299 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5300 return &M == I->second;
5301}
5302
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00005303ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005304 if (!D->isFromASTFile())
5305 return 0;
5306 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
5307 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5308 return I->second;
5309}
5310
5311SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
5312 if (ID < NUM_PREDEF_DECL_IDS)
5313 return SourceLocation();
5314
5315 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
5316
5317 if (Index > DeclsLoaded.size()) {
5318 Error("declaration ID out-of-range for AST file");
5319 return SourceLocation();
5320 }
5321
5322 if (Decl *D = DeclsLoaded[Index])
5323 return D->getLocation();
5324
5325 unsigned RawLocation = 0;
5326 RecordLocation Rec = DeclCursorForID(ID, RawLocation);
5327 return ReadSourceLocation(*Rec.F, RawLocation);
5328}
5329
5330Decl *ASTReader::GetDecl(DeclID ID) {
5331 if (ID < NUM_PREDEF_DECL_IDS) {
5332 switch ((PredefinedDeclIDs)ID) {
5333 case PREDEF_DECL_NULL_ID:
5334 return 0;
5335
5336 case PREDEF_DECL_TRANSLATION_UNIT_ID:
5337 return Context.getTranslationUnitDecl();
5338
5339 case PREDEF_DECL_OBJC_ID_ID:
5340 return Context.getObjCIdDecl();
5341
5342 case PREDEF_DECL_OBJC_SEL_ID:
5343 return Context.getObjCSelDecl();
5344
5345 case PREDEF_DECL_OBJC_CLASS_ID:
5346 return Context.getObjCClassDecl();
5347
5348 case PREDEF_DECL_OBJC_PROTOCOL_ID:
5349 return Context.getObjCProtocolDecl();
5350
5351 case PREDEF_DECL_INT_128_ID:
5352 return Context.getInt128Decl();
5353
5354 case PREDEF_DECL_UNSIGNED_INT_128_ID:
5355 return Context.getUInt128Decl();
5356
5357 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
5358 return Context.getObjCInstanceTypeDecl();
5359
5360 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
5361 return Context.getBuiltinVaListDecl();
5362 }
5363 }
5364
5365 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
5366
5367 if (Index >= DeclsLoaded.size()) {
5368 assert(0 && "declaration ID out-of-range for AST file");
5369 Error("declaration ID out-of-range for AST file");
5370 return 0;
5371 }
5372
5373 if (!DeclsLoaded[Index]) {
5374 ReadDeclRecord(ID);
5375 if (DeserializationListener)
5376 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
5377 }
5378
5379 return DeclsLoaded[Index];
5380}
5381
5382DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
5383 DeclID GlobalID) {
5384 if (GlobalID < NUM_PREDEF_DECL_IDS)
5385 return GlobalID;
5386
5387 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
5388 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5389 ModuleFile *Owner = I->second;
5390
5391 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
5392 = M.GlobalToLocalDeclIDs.find(Owner);
5393 if (Pos == M.GlobalToLocalDeclIDs.end())
5394 return 0;
5395
5396 return GlobalID - Owner->BaseDeclID + Pos->second;
5397}
5398
5399serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
5400 const RecordData &Record,
5401 unsigned &Idx) {
5402 if (Idx >= Record.size()) {
5403 Error("Corrupted AST file");
5404 return 0;
5405 }
5406
5407 return getGlobalDeclID(F, Record[Idx++]);
5408}
5409
5410/// \brief Resolve the offset of a statement into a statement.
5411///
5412/// This operation will read a new statement from the external
5413/// source each time it is called, and is meant to be used via a
5414/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
5415Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
5416 // Switch case IDs are per Decl.
5417 ClearSwitchCaseIDs();
5418
5419 // Offset here is a global offset across the entire chain.
5420 RecordLocation Loc = getLocalBitOffset(Offset);
5421 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
5422 return ReadStmtFromStream(*Loc.F);
5423}
5424
5425namespace {
5426 class FindExternalLexicalDeclsVisitor {
5427 ASTReader &Reader;
5428 const DeclContext *DC;
5429 bool (*isKindWeWant)(Decl::Kind);
5430
5431 SmallVectorImpl<Decl*> &Decls;
5432 bool PredefsVisited[NUM_PREDEF_DECL_IDS];
5433
5434 public:
5435 FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC,
5436 bool (*isKindWeWant)(Decl::Kind),
5437 SmallVectorImpl<Decl*> &Decls)
5438 : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls)
5439 {
5440 for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I)
5441 PredefsVisited[I] = false;
5442 }
5443
5444 static bool visit(ModuleFile &M, bool Preorder, void *UserData) {
5445 if (Preorder)
5446 return false;
5447
5448 FindExternalLexicalDeclsVisitor *This
5449 = static_cast<FindExternalLexicalDeclsVisitor *>(UserData);
5450
5451 ModuleFile::DeclContextInfosMap::iterator Info
5452 = M.DeclContextInfos.find(This->DC);
5453 if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls)
5454 return false;
5455
5456 // Load all of the declaration IDs
5457 for (const KindDeclIDPair *ID = Info->second.LexicalDecls,
5458 *IDE = ID + Info->second.NumLexicalDecls;
5459 ID != IDE; ++ID) {
5460 if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first))
5461 continue;
5462
5463 // Don't add predefined declarations to the lexical context more
5464 // than once.
5465 if (ID->second < NUM_PREDEF_DECL_IDS) {
5466 if (This->PredefsVisited[ID->second])
5467 continue;
5468
5469 This->PredefsVisited[ID->second] = true;
5470 }
5471
5472 if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) {
5473 if (!This->DC->isDeclInLexicalTraversal(D))
5474 This->Decls.push_back(D);
5475 }
5476 }
5477
5478 return false;
5479 }
5480 };
5481}
5482
5483ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
5484 bool (*isKindWeWant)(Decl::Kind),
5485 SmallVectorImpl<Decl*> &Decls) {
5486 // There might be lexical decls in multiple modules, for the TU at
5487 // least. Walk all of the modules in the order they were loaded.
5488 FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls);
5489 ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor);
5490 ++NumLexicalDeclContextsRead;
5491 return ELR_Success;
5492}
5493
5494namespace {
5495
5496class DeclIDComp {
5497 ASTReader &Reader;
5498 ModuleFile &Mod;
5499
5500public:
5501 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
5502
5503 bool operator()(LocalDeclID L, LocalDeclID R) const {
5504 SourceLocation LHS = getLocation(L);
5505 SourceLocation RHS = getLocation(R);
5506 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5507 }
5508
5509 bool operator()(SourceLocation LHS, LocalDeclID R) const {
5510 SourceLocation RHS = getLocation(R);
5511 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5512 }
5513
5514 bool operator()(LocalDeclID L, SourceLocation RHS) const {
5515 SourceLocation LHS = getLocation(L);
5516 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5517 }
5518
5519 SourceLocation getLocation(LocalDeclID ID) const {
5520 return Reader.getSourceManager().getFileLoc(
5521 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
5522 }
5523};
5524
5525}
5526
5527void ASTReader::FindFileRegionDecls(FileID File,
5528 unsigned Offset, unsigned Length,
5529 SmallVectorImpl<Decl *> &Decls) {
5530 SourceManager &SM = getSourceManager();
5531
5532 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
5533 if (I == FileDeclIDs.end())
5534 return;
5535
5536 FileDeclsInfo &DInfo = I->second;
5537 if (DInfo.Decls.empty())
5538 return;
5539
5540 SourceLocation
5541 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
5542 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
5543
5544 DeclIDComp DIDComp(*this, *DInfo.Mod);
5545 ArrayRef<serialization::LocalDeclID>::iterator
5546 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
5547 BeginLoc, DIDComp);
5548 if (BeginIt != DInfo.Decls.begin())
5549 --BeginIt;
5550
5551 // If we are pointing at a top-level decl inside an objc container, we need
5552 // to backtrack until we find it otherwise we will fail to report that the
5553 // region overlaps with an objc container.
5554 while (BeginIt != DInfo.Decls.begin() &&
5555 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
5556 ->isTopLevelDeclInObjCContainer())
5557 --BeginIt;
5558
5559 ArrayRef<serialization::LocalDeclID>::iterator
5560 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
5561 EndLoc, DIDComp);
5562 if (EndIt != DInfo.Decls.end())
5563 ++EndIt;
5564
5565 for (ArrayRef<serialization::LocalDeclID>::iterator
5566 DIt = BeginIt; DIt != EndIt; ++DIt)
5567 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
5568}
5569
5570namespace {
5571 /// \brief ModuleFile visitor used to perform name lookup into a
5572 /// declaration context.
5573 class DeclContextNameLookupVisitor {
5574 ASTReader &Reader;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00005575 SmallVectorImpl<const DeclContext *> &Contexts;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005576 DeclarationName Name;
5577 SmallVectorImpl<NamedDecl *> &Decls;
5578
5579 public:
5580 DeclContextNameLookupVisitor(ASTReader &Reader,
5581 SmallVectorImpl<const DeclContext *> &Contexts,
5582 DeclarationName Name,
5583 SmallVectorImpl<NamedDecl *> &Decls)
5584 : Reader(Reader), Contexts(Contexts), Name(Name), Decls(Decls) { }
5585
5586 static bool visit(ModuleFile &M, void *UserData) {
5587 DeclContextNameLookupVisitor *This
5588 = static_cast<DeclContextNameLookupVisitor *>(UserData);
5589
5590 // Check whether we have any visible declaration information for
5591 // this context in this module.
5592 ModuleFile::DeclContextInfosMap::iterator Info;
5593 bool FoundInfo = false;
5594 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5595 Info = M.DeclContextInfos.find(This->Contexts[I]);
5596 if (Info != M.DeclContextInfos.end() &&
5597 Info->second.NameLookupTableData) {
5598 FoundInfo = true;
5599 break;
5600 }
5601 }
5602
5603 if (!FoundInfo)
5604 return false;
5605
5606 // Look for this name within this module.
5607 ASTDeclContextNameLookupTable *LookupTable =
5608 Info->second.NameLookupTableData;
5609 ASTDeclContextNameLookupTable::iterator Pos
5610 = LookupTable->find(This->Name);
5611 if (Pos == LookupTable->end())
5612 return false;
5613
5614 bool FoundAnything = false;
5615 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
5616 for (; Data.first != Data.second; ++Data.first) {
5617 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first);
5618 if (!ND)
5619 continue;
5620
5621 if (ND->getDeclName() != This->Name) {
5622 // A name might be null because the decl's redeclarable part is
5623 // currently read before reading its name. The lookup is triggered by
5624 // building that decl (likely indirectly), and so it is later in the
5625 // sense of "already existing" and can be ignored here.
5626 continue;
5627 }
5628
5629 // Record this declaration.
5630 FoundAnything = true;
5631 This->Decls.push_back(ND);
5632 }
5633
5634 return FoundAnything;
5635 }
5636 };
5637}
5638
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00005639/// \brief Retrieve the "definitive" module file for the definition of the
5640/// given declaration context, if there is one.
5641///
5642/// The "definitive" module file is the only place where we need to look to
5643/// find information about the declarations within the given declaration
5644/// context. For example, C++ and Objective-C classes, C structs/unions, and
5645/// Objective-C protocols, categories, and extensions are all defined in a
5646/// single place in the source code, so they have definitive module files
5647/// associated with them. C++ namespaces, on the other hand, can have
5648/// definitions in multiple different module files.
5649///
5650/// Note: this needs to be kept in sync with ASTWriter::AddedVisibleDecl's
5651/// NDEBUG checking.
5652static ModuleFile *getDefinitiveModuleFileFor(const DeclContext *DC,
5653 ASTReader &Reader) {
Douglas Gregore0d20662013-01-22 17:08:30 +00005654 if (const DeclContext *DefDC = getDefinitiveDeclContext(DC))
5655 return Reader.getOwningModuleFile(cast<Decl>(DefDC));
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00005656
5657 return 0;
5658}
5659
Richard Smith3646c682013-02-07 03:30:24 +00005660bool
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005661ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
5662 DeclarationName Name) {
5663 assert(DC->hasExternalVisibleStorage() &&
5664 "DeclContext has no visible decls in storage");
5665 if (!Name)
Richard Smith3646c682013-02-07 03:30:24 +00005666 return false;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005667
5668 SmallVector<NamedDecl *, 64> Decls;
5669
5670 // Compute the declaration contexts we need to look into. Multiple such
5671 // declaration contexts occur when two declaration contexts from disjoint
5672 // modules get merged, e.g., when two namespaces with the same name are
5673 // independently defined in separate modules.
5674 SmallVector<const DeclContext *, 2> Contexts;
5675 Contexts.push_back(DC);
5676
5677 if (DC->isNamespace()) {
5678 MergedDeclsMap::iterator Merged
5679 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5680 if (Merged != MergedDecls.end()) {
5681 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5682 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5683 }
5684 }
5685
5686 DeclContextNameLookupVisitor Visitor(*this, Contexts, Name, Decls);
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00005687
5688 // If we can definitively determine which module file to look into,
5689 // only look there. Otherwise, look in all module files.
5690 ModuleFile *Definitive;
5691 if (Contexts.size() == 1 &&
5692 (Definitive = getDefinitiveModuleFileFor(DC, *this))) {
5693 DeclContextNameLookupVisitor::visit(*Definitive, &Visitor);
5694 } else {
5695 ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor);
5696 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005697 ++NumVisibleDeclContextsRead;
5698 SetExternalVisibleDeclsForName(DC, Name, Decls);
Richard Smith3646c682013-02-07 03:30:24 +00005699 return !Decls.empty();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005700}
5701
5702namespace {
5703 /// \brief ModuleFile visitor used to retrieve all visible names in a
5704 /// declaration context.
5705 class DeclContextAllNamesVisitor {
5706 ASTReader &Reader;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00005707 SmallVectorImpl<const DeclContext *> &Contexts;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005708 llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> > &Decls;
Argyrios Kyrtzidisca40f302012-12-19 22:21:18 +00005709 bool VisitAll;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005710
5711 public:
5712 DeclContextAllNamesVisitor(ASTReader &Reader,
5713 SmallVectorImpl<const DeclContext *> &Contexts,
5714 llvm::DenseMap<DeclarationName,
Argyrios Kyrtzidisca40f302012-12-19 22:21:18 +00005715 SmallVector<NamedDecl *, 8> > &Decls,
5716 bool VisitAll)
5717 : Reader(Reader), Contexts(Contexts), Decls(Decls), VisitAll(VisitAll) { }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005718
5719 static bool visit(ModuleFile &M, void *UserData) {
5720 DeclContextAllNamesVisitor *This
5721 = static_cast<DeclContextAllNamesVisitor *>(UserData);
5722
5723 // Check whether we have any visible declaration information for
5724 // this context in this module.
5725 ModuleFile::DeclContextInfosMap::iterator Info;
5726 bool FoundInfo = false;
5727 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5728 Info = M.DeclContextInfos.find(This->Contexts[I]);
5729 if (Info != M.DeclContextInfos.end() &&
5730 Info->second.NameLookupTableData) {
5731 FoundInfo = true;
5732 break;
5733 }
5734 }
5735
5736 if (!FoundInfo)
5737 return false;
5738
5739 ASTDeclContextNameLookupTable *LookupTable =
5740 Info->second.NameLookupTableData;
5741 bool FoundAnything = false;
5742 for (ASTDeclContextNameLookupTable::data_iterator
Douglas Gregora6b00fc2013-01-23 22:38:11 +00005743 I = LookupTable->data_begin(), E = LookupTable->data_end();
5744 I != E;
5745 ++I) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005746 ASTDeclContextNameLookupTrait::data_type Data = *I;
5747 for (; Data.first != Data.second; ++Data.first) {
5748 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M,
5749 *Data.first);
5750 if (!ND)
5751 continue;
5752
5753 // Record this declaration.
5754 FoundAnything = true;
5755 This->Decls[ND->getDeclName()].push_back(ND);
5756 }
5757 }
5758
Argyrios Kyrtzidisca40f302012-12-19 22:21:18 +00005759 return FoundAnything && !This->VisitAll;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005760 }
5761 };
5762}
5763
5764void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
5765 if (!DC->hasExternalVisibleStorage())
5766 return;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00005767 llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> > Decls;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005768
5769 // Compute the declaration contexts we need to look into. Multiple such
5770 // declaration contexts occur when two declaration contexts from disjoint
5771 // modules get merged, e.g., when two namespaces with the same name are
5772 // independently defined in separate modules.
5773 SmallVector<const DeclContext *, 2> Contexts;
5774 Contexts.push_back(DC);
5775
5776 if (DC->isNamespace()) {
5777 MergedDeclsMap::iterator Merged
5778 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5779 if (Merged != MergedDecls.end()) {
5780 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5781 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5782 }
5783 }
5784
Argyrios Kyrtzidisca40f302012-12-19 22:21:18 +00005785 DeclContextAllNamesVisitor Visitor(*this, Contexts, Decls,
5786 /*VisitAll=*/DC->isFileContext());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005787 ModuleMgr.visit(&DeclContextAllNamesVisitor::visit, &Visitor);
5788 ++NumVisibleDeclContextsRead;
5789
5790 for (llvm::DenseMap<DeclarationName,
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00005791 SmallVector<NamedDecl *, 8> >::iterator
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005792 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
5793 SetExternalVisibleDeclsForName(DC, I->first, I->second);
5794 }
5795 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
5796}
5797
5798/// \brief Under non-PCH compilation the consumer receives the objc methods
5799/// before receiving the implementation, and codegen depends on this.
5800/// We simulate this by deserializing and passing to consumer the methods of the
5801/// implementation before passing the deserialized implementation decl.
5802static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
5803 ASTConsumer *Consumer) {
5804 assert(ImplD && Consumer);
5805
5806 for (ObjCImplDecl::method_iterator
5807 I = ImplD->meth_begin(), E = ImplD->meth_end(); I != E; ++I)
5808 Consumer->HandleInterestingDecl(DeclGroupRef(*I));
5809
5810 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
5811}
5812
5813void ASTReader::PassInterestingDeclsToConsumer() {
5814 assert(Consumer);
5815 while (!InterestingDecls.empty()) {
5816 Decl *D = InterestingDecls.front();
5817 InterestingDecls.pop_front();
5818
5819 PassInterestingDeclToConsumer(D);
5820 }
5821}
5822
5823void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
5824 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
5825 PassObjCImplDeclToConsumer(ImplD, Consumer);
5826 else
5827 Consumer->HandleInterestingDecl(DeclGroupRef(D));
5828}
5829
5830void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
5831 this->Consumer = Consumer;
5832
5833 if (!Consumer)
5834 return;
5835
5836 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
5837 // Force deserialization of this decl, which will cause it to be queued for
5838 // passing to the consumer.
5839 GetDecl(ExternalDefinitions[I]);
5840 }
5841 ExternalDefinitions.clear();
5842
5843 PassInterestingDeclsToConsumer();
5844}
5845
5846void ASTReader::PrintStats() {
5847 std::fprintf(stderr, "*** AST File Statistics:\n");
5848
5849 unsigned NumTypesLoaded
5850 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
5851 QualType());
5852 unsigned NumDeclsLoaded
5853 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
5854 (Decl *)0);
5855 unsigned NumIdentifiersLoaded
5856 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
5857 IdentifiersLoaded.end(),
5858 (IdentifierInfo *)0);
5859 unsigned NumMacrosLoaded
5860 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
5861 MacrosLoaded.end(),
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00005862 (MacroInfo *)0);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005863 unsigned NumSelectorsLoaded
5864 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
5865 SelectorsLoaded.end(),
5866 Selector());
5867
5868 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
5869 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
5870 NumSLocEntriesRead, TotalNumSLocEntries,
5871 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
5872 if (!TypesLoaded.empty())
5873 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
5874 NumTypesLoaded, (unsigned)TypesLoaded.size(),
5875 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
5876 if (!DeclsLoaded.empty())
5877 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
5878 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
5879 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
5880 if (!IdentifiersLoaded.empty())
5881 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
5882 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
5883 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
5884 if (!MacrosLoaded.empty())
5885 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
5886 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
5887 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
5888 if (!SelectorsLoaded.empty())
5889 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
5890 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
5891 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
5892 if (TotalNumStatements)
5893 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
5894 NumStatementsRead, TotalNumStatements,
5895 ((float)NumStatementsRead/TotalNumStatements * 100));
5896 if (TotalNumMacros)
5897 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
5898 NumMacrosRead, TotalNumMacros,
5899 ((float)NumMacrosRead/TotalNumMacros * 100));
5900 if (TotalLexicalDeclContexts)
5901 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
5902 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
5903 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
5904 * 100));
5905 if (TotalVisibleDeclContexts)
5906 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
5907 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
5908 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
5909 * 100));
5910 if (TotalNumMethodPoolEntries) {
5911 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
5912 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
5913 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
5914 * 100));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005915 }
Douglas Gregor95fb36e2013-01-28 17:54:36 +00005916 if (NumMethodPoolLookups) {
5917 std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n",
5918 NumMethodPoolHits, NumMethodPoolLookups,
5919 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
5920 }
5921 if (NumMethodPoolTableLookups) {
5922 std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n",
5923 NumMethodPoolTableHits, NumMethodPoolTableLookups,
5924 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
5925 * 100.0));
5926 }
5927
Douglas Gregore1698072013-01-25 00:38:33 +00005928 if (NumIdentifierLookupHits) {
5929 std::fprintf(stderr,
5930 " %u / %u identifier table lookups succeeded (%f%%)\n",
5931 NumIdentifierLookupHits, NumIdentifierLookups,
5932 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
5933 }
5934
Douglas Gregor1a49d972013-01-25 01:03:03 +00005935 if (GlobalIndex) {
5936 std::fprintf(stderr, "\n");
5937 GlobalIndex->printStats();
5938 }
5939
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005940 std::fprintf(stderr, "\n");
5941 dump();
5942 std::fprintf(stderr, "\n");
5943}
5944
5945template<typename Key, typename ModuleFile, unsigned InitialCapacity>
5946static void
5947dumpModuleIDMap(StringRef Name,
5948 const ContinuousRangeMap<Key, ModuleFile *,
5949 InitialCapacity> &Map) {
5950 if (Map.begin() == Map.end())
5951 return;
5952
5953 typedef ContinuousRangeMap<Key, ModuleFile *, InitialCapacity> MapType;
5954 llvm::errs() << Name << ":\n";
5955 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
5956 I != IEnd; ++I) {
5957 llvm::errs() << " " << I->first << " -> " << I->second->FileName
5958 << "\n";
5959 }
5960}
5961
5962void ASTReader::dump() {
5963 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
5964 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
5965 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
5966 dumpModuleIDMap("Global type map", GlobalTypeMap);
5967 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
5968 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
5969 dumpModuleIDMap("Global macro map", GlobalMacroMap);
5970 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
5971 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
5972 dumpModuleIDMap("Global preprocessed entity map",
5973 GlobalPreprocessedEntityMap);
5974
5975 llvm::errs() << "\n*** PCH/Modules Loaded:";
5976 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
5977 MEnd = ModuleMgr.end();
5978 M != MEnd; ++M)
5979 (*M)->dump();
5980}
5981
5982/// Return the amount of memory used by memory buffers, breaking down
5983/// by heap-backed versus mmap'ed memory.
5984void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
5985 for (ModuleConstIterator I = ModuleMgr.begin(),
5986 E = ModuleMgr.end(); I != E; ++I) {
5987 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
5988 size_t bytes = buf->getBufferSize();
5989 switch (buf->getBufferKind()) {
5990 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
5991 sizes.malloc_bytes += bytes;
5992 break;
5993 case llvm::MemoryBuffer::MemoryBuffer_MMap:
5994 sizes.mmap_bytes += bytes;
5995 break;
5996 }
5997 }
5998 }
5999}
6000
6001void ASTReader::InitializeSema(Sema &S) {
6002 SemaObj = &S;
6003 S.addExternalSource(this);
6004
6005 // Makes sure any declarations that were deserialized "too early"
6006 // still get added to the identifier's declaration chains.
6007 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
Argyrios Kyrtzidis0532df02013-04-26 21:33:35 +00006008 pushExternalDeclIntoScope(PreloadedDecls[I],
6009 PreloadedDecls[I]->getDeclName());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006010 }
6011 PreloadedDecls.clear();
6012
6013 // Load the offsets of the declarations that Sema references.
6014 // They will be lazily deserialized when needed.
6015 if (!SemaDeclRefs.empty()) {
6016 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
6017 if (!SemaObj->StdNamespace)
6018 SemaObj->StdNamespace = SemaDeclRefs[0];
6019 if (!SemaObj->StdBadAlloc)
6020 SemaObj->StdBadAlloc = SemaDeclRefs[1];
6021 }
6022
6023 if (!FPPragmaOptions.empty()) {
6024 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
6025 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
6026 }
6027
6028 if (!OpenCLExtensions.empty()) {
6029 unsigned I = 0;
6030#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
6031#include "clang/Basic/OpenCLExtensions.def"
6032
6033 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
6034 }
6035}
6036
6037IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
6038 // Note that we are loading an identifier.
6039 Deserializing AnIdentifier(this);
Douglas Gregor1a49d972013-01-25 01:03:03 +00006040 StringRef Name(NameStart, NameEnd - NameStart);
6041
6042 // If there is a global index, look there first to determine which modules
6043 // provably do not have any results for this identifier.
Douglas Gregor188bdcd2013-01-25 23:32:03 +00006044 GlobalModuleIndex::HitSet Hits;
6045 GlobalModuleIndex::HitSet *HitsPtr = 0;
Douglas Gregor1a49d972013-01-25 01:03:03 +00006046 if (!loadGlobalIndex()) {
Douglas Gregor188bdcd2013-01-25 23:32:03 +00006047 if (GlobalIndex->lookupIdentifier(Name, Hits)) {
6048 HitsPtr = &Hits;
Douglas Gregor1a49d972013-01-25 01:03:03 +00006049 }
6050 }
Douglas Gregor188bdcd2013-01-25 23:32:03 +00006051 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
Douglas Gregore1698072013-01-25 00:38:33 +00006052 NumIdentifierLookups,
6053 NumIdentifierLookupHits);
Douglas Gregor188bdcd2013-01-25 23:32:03 +00006054 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor, HitsPtr);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006055 IdentifierInfo *II = Visitor.getIdentifierInfo();
6056 markIdentifierUpToDate(II);
6057 return II;
6058}
6059
6060namespace clang {
6061 /// \brief An identifier-lookup iterator that enumerates all of the
6062 /// identifiers stored within a set of AST files.
6063 class ASTIdentifierIterator : public IdentifierIterator {
6064 /// \brief The AST reader whose identifiers are being enumerated.
6065 const ASTReader &Reader;
6066
6067 /// \brief The current index into the chain of AST files stored in
6068 /// the AST reader.
6069 unsigned Index;
6070
6071 /// \brief The current position within the identifier lookup table
6072 /// of the current AST file.
6073 ASTIdentifierLookupTable::key_iterator Current;
6074
6075 /// \brief The end position within the identifier lookup table of
6076 /// the current AST file.
6077 ASTIdentifierLookupTable::key_iterator End;
6078
6079 public:
6080 explicit ASTIdentifierIterator(const ASTReader &Reader);
6081
6082 virtual StringRef Next();
6083 };
6084}
6085
6086ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
6087 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
6088 ASTIdentifierLookupTable *IdTable
6089 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
6090 Current = IdTable->key_begin();
6091 End = IdTable->key_end();
6092}
6093
6094StringRef ASTIdentifierIterator::Next() {
6095 while (Current == End) {
6096 // If we have exhausted all of our AST files, we're done.
6097 if (Index == 0)
6098 return StringRef();
6099
6100 --Index;
6101 ASTIdentifierLookupTable *IdTable
6102 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
6103 IdentifierLookupTable;
6104 Current = IdTable->key_begin();
6105 End = IdTable->key_end();
6106 }
6107
6108 // We have any identifiers remaining in the current AST file; return
6109 // the next one.
Douglas Gregor479633c2013-01-23 18:53:14 +00006110 StringRef Result = *Current;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006111 ++Current;
Douglas Gregor479633c2013-01-23 18:53:14 +00006112 return Result;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006113}
6114
Argyrios Kyrtzidis87f9d812013-04-17 22:10:55 +00006115IdentifierIterator *ASTReader::getIdentifiers() {
6116 if (!loadGlobalIndex())
6117 return GlobalIndex->createIdentifierIterator();
6118
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006119 return new ASTIdentifierIterator(*this);
6120}
6121
6122namespace clang { namespace serialization {
6123 class ReadMethodPoolVisitor {
6124 ASTReader &Reader;
6125 Selector Sel;
6126 unsigned PriorGeneration;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00006127 unsigned InstanceBits;
6128 unsigned FactoryBits;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00006129 SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
6130 SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006131
6132 public:
6133 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
6134 unsigned PriorGeneration)
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00006135 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration),
6136 InstanceBits(0), FactoryBits(0) { }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006137
6138 static bool visit(ModuleFile &M, void *UserData) {
6139 ReadMethodPoolVisitor *This
6140 = static_cast<ReadMethodPoolVisitor *>(UserData);
6141
6142 if (!M.SelectorLookupTable)
6143 return false;
6144
6145 // If we've already searched this module file, skip it now.
6146 if (M.Generation <= This->PriorGeneration)
6147 return true;
6148
Douglas Gregor95fb36e2013-01-28 17:54:36 +00006149 ++This->Reader.NumMethodPoolTableLookups;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006150 ASTSelectorLookupTable *PoolTable
6151 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
6152 ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel);
6153 if (Pos == PoolTable->end())
6154 return false;
Douglas Gregor95fb36e2013-01-28 17:54:36 +00006155
6156 ++This->Reader.NumMethodPoolTableHits;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006157 ++This->Reader.NumSelectorsRead;
6158 // FIXME: Not quite happy with the statistics here. We probably should
6159 // disable this tracking when called via LoadSelector.
6160 // Also, should entries without methods count as misses?
6161 ++This->Reader.NumMethodPoolEntriesRead;
6162 ASTSelectorLookupTrait::data_type Data = *Pos;
6163 if (This->Reader.DeserializationListener)
6164 This->Reader.DeserializationListener->SelectorRead(Data.ID,
6165 This->Sel);
6166
6167 This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
6168 This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00006169 This->InstanceBits = Data.InstanceBits;
6170 This->FactoryBits = Data.FactoryBits;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006171 return true;
6172 }
6173
6174 /// \brief Retrieve the instance methods found by this visitor.
6175 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
6176 return InstanceMethods;
6177 }
6178
6179 /// \brief Retrieve the instance methods found by this visitor.
6180 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
6181 return FactoryMethods;
6182 }
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00006183
6184 unsigned getInstanceBits() const { return InstanceBits; }
6185 unsigned getFactoryBits() const { return FactoryBits; }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006186 };
6187} } // end namespace clang::serialization
6188
6189/// \brief Add the given set of methods to the method list.
6190static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
6191 ObjCMethodList &List) {
6192 for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
6193 S.addMethodToGlobalList(&List, Methods[I]);
6194 }
6195}
6196
6197void ASTReader::ReadMethodPool(Selector Sel) {
6198 // Get the selector generation and update it to the current generation.
6199 unsigned &Generation = SelectorGeneration[Sel];
6200 unsigned PriorGeneration = Generation;
6201 Generation = CurrentGeneration;
6202
6203 // Search for methods defined with this selector.
Douglas Gregor95fb36e2013-01-28 17:54:36 +00006204 ++NumMethodPoolLookups;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006205 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
6206 ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor);
6207
6208 if (Visitor.getInstanceMethods().empty() &&
Douglas Gregor95fb36e2013-01-28 17:54:36 +00006209 Visitor.getFactoryMethods().empty())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006210 return;
Douglas Gregor95fb36e2013-01-28 17:54:36 +00006211
6212 ++NumMethodPoolHits;
6213
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006214 if (!getSema())
6215 return;
6216
6217 Sema &S = *getSema();
6218 Sema::GlobalMethodPool::iterator Pos
6219 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
6220
6221 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
6222 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00006223 Pos->second.first.setBits(Visitor.getInstanceBits());
6224 Pos->second.second.setBits(Visitor.getFactoryBits());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006225}
6226
6227void ASTReader::ReadKnownNamespaces(
6228 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
6229 Namespaces.clear();
6230
6231 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
6232 if (NamespaceDecl *Namespace
6233 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
6234 Namespaces.push_back(Namespace);
6235 }
6236}
6237
Nick Lewyckycd0655b2013-02-01 08:13:20 +00006238void ASTReader::ReadUndefinedButUsed(
Nick Lewycky995e26b2013-01-31 03:23:57 +00006239 llvm::DenseMap<NamedDecl*, SourceLocation> &Undefined) {
Nick Lewyckycd0655b2013-02-01 08:13:20 +00006240 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
6241 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
Nick Lewycky01a41142013-01-26 00:35:08 +00006242 SourceLocation Loc =
Nick Lewyckycd0655b2013-02-01 08:13:20 +00006243 SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
Nick Lewycky01a41142013-01-26 00:35:08 +00006244 Undefined.insert(std::make_pair(D, Loc));
6245 }
6246}
Nick Lewycky01a41142013-01-26 00:35:08 +00006247
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006248void ASTReader::ReadTentativeDefinitions(
6249 SmallVectorImpl<VarDecl *> &TentativeDefs) {
6250 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
6251 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
6252 if (Var)
6253 TentativeDefs.push_back(Var);
6254 }
6255 TentativeDefinitions.clear();
6256}
6257
6258void ASTReader::ReadUnusedFileScopedDecls(
6259 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
6260 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
6261 DeclaratorDecl *D
6262 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
6263 if (D)
6264 Decls.push_back(D);
6265 }
6266 UnusedFileScopedDecls.clear();
6267}
6268
6269void ASTReader::ReadDelegatingConstructors(
6270 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
6271 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
6272 CXXConstructorDecl *D
6273 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
6274 if (D)
6275 Decls.push_back(D);
6276 }
6277 DelegatingCtorDecls.clear();
6278}
6279
6280void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
6281 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
6282 TypedefNameDecl *D
6283 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
6284 if (D)
6285 Decls.push_back(D);
6286 }
6287 ExtVectorDecls.clear();
6288}
6289
6290void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) {
6291 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
6292 CXXRecordDecl *D
6293 = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I]));
6294 if (D)
6295 Decls.push_back(D);
6296 }
6297 DynamicClasses.clear();
6298}
6299
6300void
Richard Smith5ea6ef42013-01-10 23:43:47 +00006301ASTReader::ReadLocallyScopedExternCDecls(SmallVectorImpl<NamedDecl *> &Decls) {
6302 for (unsigned I = 0, N = LocallyScopedExternCDecls.size(); I != N; ++I) {
6303 NamedDecl *D
6304 = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternCDecls[I]));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006305 if (D)
6306 Decls.push_back(D);
6307 }
Richard Smith5ea6ef42013-01-10 23:43:47 +00006308 LocallyScopedExternCDecls.clear();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006309}
6310
6311void ASTReader::ReadReferencedSelectors(
6312 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
6313 if (ReferencedSelectorsData.empty())
6314 return;
6315
6316 // If there are @selector references added them to its pool. This is for
6317 // implementation of -Wselector.
6318 unsigned int DataSize = ReferencedSelectorsData.size()-1;
6319 unsigned I = 0;
6320 while (I < DataSize) {
6321 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
6322 SourceLocation SelLoc
6323 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
6324 Sels.push_back(std::make_pair(Sel, SelLoc));
6325 }
6326 ReferencedSelectorsData.clear();
6327}
6328
6329void ASTReader::ReadWeakUndeclaredIdentifiers(
6330 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
6331 if (WeakUndeclaredIdentifiers.empty())
6332 return;
6333
6334 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
6335 IdentifierInfo *WeakId
6336 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
6337 IdentifierInfo *AliasId
6338 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
6339 SourceLocation Loc
6340 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
6341 bool Used = WeakUndeclaredIdentifiers[I++];
6342 WeakInfo WI(AliasId, Loc);
6343 WI.setUsed(Used);
6344 WeakIDs.push_back(std::make_pair(WeakId, WI));
6345 }
6346 WeakUndeclaredIdentifiers.clear();
6347}
6348
6349void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
6350 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
6351 ExternalVTableUse VT;
6352 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
6353 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
6354 VT.DefinitionRequired = VTableUses[Idx++];
6355 VTables.push_back(VT);
6356 }
6357
6358 VTableUses.clear();
6359}
6360
6361void ASTReader::ReadPendingInstantiations(
6362 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
6363 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
6364 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
6365 SourceLocation Loc
6366 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
6367
6368 Pending.push_back(std::make_pair(D, Loc));
6369 }
6370 PendingInstantiations.clear();
6371}
6372
6373void ASTReader::LoadSelector(Selector Sel) {
6374 // It would be complicated to avoid reading the methods anyway. So don't.
6375 ReadMethodPool(Sel);
6376}
6377
6378void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
6379 assert(ID && "Non-zero identifier ID required");
6380 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
6381 IdentifiersLoaded[ID - 1] = II;
6382 if (DeserializationListener)
6383 DeserializationListener->IdentifierRead(ID, II);
6384}
6385
6386/// \brief Set the globally-visible declarations associated with the given
6387/// identifier.
6388///
6389/// If the AST reader is currently in a state where the given declaration IDs
6390/// cannot safely be resolved, they are queued until it is safe to resolve
6391/// them.
6392///
6393/// \param II an IdentifierInfo that refers to one or more globally-visible
6394/// declarations.
6395///
6396/// \param DeclIDs the set of declaration IDs with the name @p II that are
6397/// visible at global scope.
6398///
Douglas Gregoraa945902013-02-18 15:53:43 +00006399/// \param Decls if non-null, this vector will be populated with the set of
6400/// deserialized declarations. These declarations will not be pushed into
6401/// scope.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006402void
6403ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
6404 const SmallVectorImpl<uint32_t> &DeclIDs,
Douglas Gregoraa945902013-02-18 15:53:43 +00006405 SmallVectorImpl<Decl *> *Decls) {
6406 if (NumCurrentElementsDeserializing && !Decls) {
6407 PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006408 return;
6409 }
6410
6411 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
6412 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
6413 if (SemaObj) {
Douglas Gregoraa945902013-02-18 15:53:43 +00006414 // If we're simply supposed to record the declarations, do so now.
6415 if (Decls) {
6416 Decls->push_back(D);
6417 continue;
6418 }
6419
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006420 // Introduce this declaration into the translation-unit scope
6421 // and add it to the declaration chain for this identifier, so
6422 // that (unqualified) name lookup will find it.
Argyrios Kyrtzidis0532df02013-04-26 21:33:35 +00006423 pushExternalDeclIntoScope(D, II);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006424 } else {
6425 // Queue this declaration so that it will be added to the
6426 // translation unit scope and identifier's declaration chain
6427 // once a Sema object is known.
6428 PreloadedDecls.push_back(D);
6429 }
6430 }
6431}
6432
Douglas Gregor8222b892013-01-21 16:52:34 +00006433IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006434 if (ID == 0)
6435 return 0;
6436
6437 if (IdentifiersLoaded.empty()) {
6438 Error("no identifier table in AST file");
6439 return 0;
6440 }
6441
6442 ID -= 1;
6443 if (!IdentifiersLoaded[ID]) {
6444 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
6445 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
6446 ModuleFile *M = I->second;
6447 unsigned Index = ID - M->BaseIdentifierID;
6448 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
6449
6450 // All of the strings in the AST file are preceded by a 16-bit length.
6451 // Extract that 16-bit length to avoid having to execute strlen().
6452 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
6453 // unsigned integers. This is important to avoid integer overflow when
6454 // we cast them to 'unsigned'.
6455 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
6456 unsigned StrLen = (((unsigned) StrLenPtr[0])
6457 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Douglas Gregor8222b892013-01-21 16:52:34 +00006458 IdentifiersLoaded[ID]
6459 = &PP.getIdentifierTable().get(StringRef(Str, StrLen));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006460 if (DeserializationListener)
6461 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
6462 }
6463
6464 return IdentifiersLoaded[ID];
6465}
6466
Douglas Gregor8222b892013-01-21 16:52:34 +00006467IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
6468 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006469}
6470
6471IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
6472 if (LocalID < NUM_PREDEF_IDENT_IDS)
6473 return LocalID;
6474
6475 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6476 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
6477 assert(I != M.IdentifierRemap.end()
6478 && "Invalid index into identifier index remap");
6479
6480 return LocalID + I->second;
6481}
6482
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00006483MacroInfo *ASTReader::getMacro(MacroID ID) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006484 if (ID == 0)
6485 return 0;
6486
6487 if (MacrosLoaded.empty()) {
6488 Error("no macro table in AST file");
6489 return 0;
6490 }
6491
6492 ID -= NUM_PREDEF_MACRO_IDS;
6493 if (!MacrosLoaded[ID]) {
6494 GlobalMacroMapType::iterator I
6495 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
6496 assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
6497 ModuleFile *M = I->second;
6498 unsigned Index = ID - M->BaseMacroID;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00006499 MacrosLoaded[ID] = ReadMacroRecord(*M, M->MacroOffsets[Index]);
6500
6501 if (DeserializationListener)
6502 DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
6503 MacrosLoaded[ID]);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006504 }
6505
6506 return MacrosLoaded[ID];
6507}
6508
6509MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
6510 if (LocalID < NUM_PREDEF_MACRO_IDS)
6511 return LocalID;
6512
6513 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6514 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
6515 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
6516
6517 return LocalID + I->second;
6518}
6519
6520serialization::SubmoduleID
6521ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
6522 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
6523 return LocalID;
6524
6525 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6526 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
6527 assert(I != M.SubmoduleRemap.end()
6528 && "Invalid index into submodule index remap");
6529
6530 return LocalID + I->second;
6531}
6532
6533Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
6534 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
6535 assert(GlobalID == 0 && "Unhandled global submodule ID");
6536 return 0;
6537 }
6538
6539 if (GlobalID > SubmodulesLoaded.size()) {
6540 Error("submodule ID out of range in AST file");
6541 return 0;
6542 }
6543
6544 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
6545}
Douglas Gregorca2ab452013-01-12 01:29:50 +00006546
6547Module *ASTReader::getModule(unsigned ID) {
6548 return getSubmodule(ID);
6549}
6550
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006551Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
6552 return DecodeSelector(getGlobalSelectorID(M, LocalID));
6553}
6554
6555Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
6556 if (ID == 0)
6557 return Selector();
6558
6559 if (ID > SelectorsLoaded.size()) {
6560 Error("selector ID out of range in AST file");
6561 return Selector();
6562 }
6563
6564 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
6565 // Load this selector from the selector table.
6566 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
6567 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
6568 ModuleFile &M = *I->second;
6569 ASTSelectorLookupTrait Trait(*this, M);
6570 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
6571 SelectorsLoaded[ID - 1] =
6572 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
6573 if (DeserializationListener)
6574 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
6575 }
6576
6577 return SelectorsLoaded[ID - 1];
6578}
6579
6580Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
6581 return DecodeSelector(ID);
6582}
6583
6584uint32_t ASTReader::GetNumExternalSelectors() {
6585 // ID 0 (the null selector) is considered an external selector.
6586 return getTotalNumSelectors() + 1;
6587}
6588
6589serialization::SelectorID
6590ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
6591 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
6592 return LocalID;
6593
6594 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6595 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
6596 assert(I != M.SelectorRemap.end()
6597 && "Invalid index into selector index remap");
6598
6599 return LocalID + I->second;
6600}
6601
6602DeclarationName
6603ASTReader::ReadDeclarationName(ModuleFile &F,
6604 const RecordData &Record, unsigned &Idx) {
6605 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
6606 switch (Kind) {
6607 case DeclarationName::Identifier:
Douglas Gregor8222b892013-01-21 16:52:34 +00006608 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006609
6610 case DeclarationName::ObjCZeroArgSelector:
6611 case DeclarationName::ObjCOneArgSelector:
6612 case DeclarationName::ObjCMultiArgSelector:
6613 return DeclarationName(ReadSelector(F, Record, Idx));
6614
6615 case DeclarationName::CXXConstructorName:
6616 return Context.DeclarationNames.getCXXConstructorName(
6617 Context.getCanonicalType(readType(F, Record, Idx)));
6618
6619 case DeclarationName::CXXDestructorName:
6620 return Context.DeclarationNames.getCXXDestructorName(
6621 Context.getCanonicalType(readType(F, Record, Idx)));
6622
6623 case DeclarationName::CXXConversionFunctionName:
6624 return Context.DeclarationNames.getCXXConversionFunctionName(
6625 Context.getCanonicalType(readType(F, Record, Idx)));
6626
6627 case DeclarationName::CXXOperatorName:
6628 return Context.DeclarationNames.getCXXOperatorName(
6629 (OverloadedOperatorKind)Record[Idx++]);
6630
6631 case DeclarationName::CXXLiteralOperatorName:
6632 return Context.DeclarationNames.getCXXLiteralOperatorName(
6633 GetIdentifierInfo(F, Record, Idx));
6634
6635 case DeclarationName::CXXUsingDirective:
6636 return DeclarationName::getUsingDirectiveName();
6637 }
6638
6639 llvm_unreachable("Invalid NameKind!");
6640}
6641
6642void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
6643 DeclarationNameLoc &DNLoc,
6644 DeclarationName Name,
6645 const RecordData &Record, unsigned &Idx) {
6646 switch (Name.getNameKind()) {
6647 case DeclarationName::CXXConstructorName:
6648 case DeclarationName::CXXDestructorName:
6649 case DeclarationName::CXXConversionFunctionName:
6650 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
6651 break;
6652
6653 case DeclarationName::CXXOperatorName:
6654 DNLoc.CXXOperatorName.BeginOpNameLoc
6655 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6656 DNLoc.CXXOperatorName.EndOpNameLoc
6657 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6658 break;
6659
6660 case DeclarationName::CXXLiteralOperatorName:
6661 DNLoc.CXXLiteralOperatorName.OpNameLoc
6662 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6663 break;
6664
6665 case DeclarationName::Identifier:
6666 case DeclarationName::ObjCZeroArgSelector:
6667 case DeclarationName::ObjCOneArgSelector:
6668 case DeclarationName::ObjCMultiArgSelector:
6669 case DeclarationName::CXXUsingDirective:
6670 break;
6671 }
6672}
6673
6674void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
6675 DeclarationNameInfo &NameInfo,
6676 const RecordData &Record, unsigned &Idx) {
6677 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
6678 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
6679 DeclarationNameLoc DNLoc;
6680 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
6681 NameInfo.setInfo(DNLoc);
6682}
6683
6684void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
6685 const RecordData &Record, unsigned &Idx) {
6686 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
6687 unsigned NumTPLists = Record[Idx++];
6688 Info.NumTemplParamLists = NumTPLists;
6689 if (NumTPLists) {
6690 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
6691 for (unsigned i=0; i != NumTPLists; ++i)
6692 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
6693 }
6694}
6695
6696TemplateName
6697ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
6698 unsigned &Idx) {
6699 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
6700 switch (Kind) {
6701 case TemplateName::Template:
6702 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
6703
6704 case TemplateName::OverloadedTemplate: {
6705 unsigned size = Record[Idx++];
6706 UnresolvedSet<8> Decls;
6707 while (size--)
6708 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
6709
6710 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
6711 }
6712
6713 case TemplateName::QualifiedTemplate: {
6714 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
6715 bool hasTemplKeyword = Record[Idx++];
6716 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
6717 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
6718 }
6719
6720 case TemplateName::DependentTemplate: {
6721 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
6722 if (Record[Idx++]) // isIdentifier
6723 return Context.getDependentTemplateName(NNS,
6724 GetIdentifierInfo(F, Record,
6725 Idx));
6726 return Context.getDependentTemplateName(NNS,
6727 (OverloadedOperatorKind)Record[Idx++]);
6728 }
6729
6730 case TemplateName::SubstTemplateTemplateParm: {
6731 TemplateTemplateParmDecl *param
6732 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
6733 if (!param) return TemplateName();
6734 TemplateName replacement = ReadTemplateName(F, Record, Idx);
6735 return Context.getSubstTemplateTemplateParm(param, replacement);
6736 }
6737
6738 case TemplateName::SubstTemplateTemplateParmPack: {
6739 TemplateTemplateParmDecl *Param
6740 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
6741 if (!Param)
6742 return TemplateName();
6743
6744 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
6745 if (ArgPack.getKind() != TemplateArgument::Pack)
6746 return TemplateName();
6747
6748 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
6749 }
6750 }
6751
6752 llvm_unreachable("Unhandled template name kind!");
6753}
6754
6755TemplateArgument
6756ASTReader::ReadTemplateArgument(ModuleFile &F,
6757 const RecordData &Record, unsigned &Idx) {
6758 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
6759 switch (Kind) {
6760 case TemplateArgument::Null:
6761 return TemplateArgument();
6762 case TemplateArgument::Type:
6763 return TemplateArgument(readType(F, Record, Idx));
6764 case TemplateArgument::Declaration: {
6765 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
6766 bool ForReferenceParam = Record[Idx++];
6767 return TemplateArgument(D, ForReferenceParam);
6768 }
6769 case TemplateArgument::NullPtr:
6770 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
6771 case TemplateArgument::Integral: {
6772 llvm::APSInt Value = ReadAPSInt(Record, Idx);
6773 QualType T = readType(F, Record, Idx);
6774 return TemplateArgument(Context, Value, T);
6775 }
6776 case TemplateArgument::Template:
6777 return TemplateArgument(ReadTemplateName(F, Record, Idx));
6778 case TemplateArgument::TemplateExpansion: {
6779 TemplateName Name = ReadTemplateName(F, Record, Idx);
David Blaikiedc84cd52013-02-20 22:23:23 +00006780 Optional<unsigned> NumTemplateExpansions;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006781 if (unsigned NumExpansions = Record[Idx++])
6782 NumTemplateExpansions = NumExpansions - 1;
6783 return TemplateArgument(Name, NumTemplateExpansions);
6784 }
6785 case TemplateArgument::Expression:
6786 return TemplateArgument(ReadExpr(F));
6787 case TemplateArgument::Pack: {
6788 unsigned NumArgs = Record[Idx++];
6789 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
6790 for (unsigned I = 0; I != NumArgs; ++I)
6791 Args[I] = ReadTemplateArgument(F, Record, Idx);
6792 return TemplateArgument(Args, NumArgs);
6793 }
6794 }
6795
6796 llvm_unreachable("Unhandled template argument kind!");
6797}
6798
6799TemplateParameterList *
6800ASTReader::ReadTemplateParameterList(ModuleFile &F,
6801 const RecordData &Record, unsigned &Idx) {
6802 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
6803 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
6804 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
6805
6806 unsigned NumParams = Record[Idx++];
6807 SmallVector<NamedDecl *, 16> Params;
6808 Params.reserve(NumParams);
6809 while (NumParams--)
6810 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
6811
6812 TemplateParameterList* TemplateParams =
6813 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
6814 Params.data(), Params.size(), RAngleLoc);
6815 return TemplateParams;
6816}
6817
6818void
6819ASTReader::
6820ReadTemplateArgumentList(SmallVector<TemplateArgument, 8> &TemplArgs,
6821 ModuleFile &F, const RecordData &Record,
6822 unsigned &Idx) {
6823 unsigned NumTemplateArgs = Record[Idx++];
6824 TemplArgs.reserve(NumTemplateArgs);
6825 while (NumTemplateArgs--)
6826 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
6827}
6828
6829/// \brief Read a UnresolvedSet structure.
6830void ASTReader::ReadUnresolvedSet(ModuleFile &F, ASTUnresolvedSet &Set,
6831 const RecordData &Record, unsigned &Idx) {
6832 unsigned NumDecls = Record[Idx++];
6833 Set.reserve(Context, NumDecls);
6834 while (NumDecls--) {
6835 NamedDecl *D = ReadDeclAs<NamedDecl>(F, Record, Idx);
6836 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
6837 Set.addDecl(Context, D, AS);
6838 }
6839}
6840
6841CXXBaseSpecifier
6842ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
6843 const RecordData &Record, unsigned &Idx) {
6844 bool isVirtual = static_cast<bool>(Record[Idx++]);
6845 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
6846 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
6847 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
6848 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
6849 SourceRange Range = ReadSourceRange(F, Record, Idx);
6850 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
6851 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
6852 EllipsisLoc);
6853 Result.setInheritConstructors(inheritConstructors);
6854 return Result;
6855}
6856
6857std::pair<CXXCtorInitializer **, unsigned>
6858ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
6859 unsigned &Idx) {
6860 CXXCtorInitializer **CtorInitializers = 0;
6861 unsigned NumInitializers = Record[Idx++];
6862 if (NumInitializers) {
6863 CtorInitializers
6864 = new (Context) CXXCtorInitializer*[NumInitializers];
6865 for (unsigned i=0; i != NumInitializers; ++i) {
6866 TypeSourceInfo *TInfo = 0;
6867 bool IsBaseVirtual = false;
6868 FieldDecl *Member = 0;
6869 IndirectFieldDecl *IndirectMember = 0;
6870
6871 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
6872 switch (Type) {
6873 case CTOR_INITIALIZER_BASE:
6874 TInfo = GetTypeSourceInfo(F, Record, Idx);
6875 IsBaseVirtual = Record[Idx++];
6876 break;
6877
6878 case CTOR_INITIALIZER_DELEGATING:
6879 TInfo = GetTypeSourceInfo(F, Record, Idx);
6880 break;
6881
6882 case CTOR_INITIALIZER_MEMBER:
6883 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
6884 break;
6885
6886 case CTOR_INITIALIZER_INDIRECT_MEMBER:
6887 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
6888 break;
6889 }
6890
6891 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
6892 Expr *Init = ReadExpr(F);
6893 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
6894 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
6895 bool IsWritten = Record[Idx++];
6896 unsigned SourceOrderOrNumArrayIndices;
6897 SmallVector<VarDecl *, 8> Indices;
6898 if (IsWritten) {
6899 SourceOrderOrNumArrayIndices = Record[Idx++];
6900 } else {
6901 SourceOrderOrNumArrayIndices = Record[Idx++];
6902 Indices.reserve(SourceOrderOrNumArrayIndices);
6903 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
6904 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
6905 }
6906
6907 CXXCtorInitializer *BOMInit;
6908 if (Type == CTOR_INITIALIZER_BASE) {
6909 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, IsBaseVirtual,
6910 LParenLoc, Init, RParenLoc,
6911 MemberOrEllipsisLoc);
6912 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
6913 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, LParenLoc,
6914 Init, RParenLoc);
6915 } else if (IsWritten) {
6916 if (Member)
6917 BOMInit = new (Context) CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc,
6918 LParenLoc, Init, RParenLoc);
6919 else
6920 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
6921 MemberOrEllipsisLoc, LParenLoc,
6922 Init, RParenLoc);
6923 } else {
6924 BOMInit = CXXCtorInitializer::Create(Context, Member, MemberOrEllipsisLoc,
6925 LParenLoc, Init, RParenLoc,
6926 Indices.data(), Indices.size());
6927 }
6928
6929 if (IsWritten)
6930 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
6931 CtorInitializers[i] = BOMInit;
6932 }
6933 }
6934
6935 return std::make_pair(CtorInitializers, NumInitializers);
6936}
6937
6938NestedNameSpecifier *
6939ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
6940 const RecordData &Record, unsigned &Idx) {
6941 unsigned N = Record[Idx++];
6942 NestedNameSpecifier *NNS = 0, *Prev = 0;
6943 for (unsigned I = 0; I != N; ++I) {
6944 NestedNameSpecifier::SpecifierKind Kind
6945 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
6946 switch (Kind) {
6947 case NestedNameSpecifier::Identifier: {
6948 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
6949 NNS = NestedNameSpecifier::Create(Context, Prev, II);
6950 break;
6951 }
6952
6953 case NestedNameSpecifier::Namespace: {
6954 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
6955 NNS = NestedNameSpecifier::Create(Context, Prev, NS);
6956 break;
6957 }
6958
6959 case NestedNameSpecifier::NamespaceAlias: {
6960 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
6961 NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
6962 break;
6963 }
6964
6965 case NestedNameSpecifier::TypeSpec:
6966 case NestedNameSpecifier::TypeSpecWithTemplate: {
6967 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
6968 if (!T)
6969 return 0;
6970
6971 bool Template = Record[Idx++];
6972 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
6973 break;
6974 }
6975
6976 case NestedNameSpecifier::Global: {
6977 NNS = NestedNameSpecifier::GlobalSpecifier(Context);
6978 // No associated value, and there can't be a prefix.
6979 break;
6980 }
6981 }
6982 Prev = NNS;
6983 }
6984 return NNS;
6985}
6986
6987NestedNameSpecifierLoc
6988ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
6989 unsigned &Idx) {
6990 unsigned N = Record[Idx++];
6991 NestedNameSpecifierLocBuilder Builder;
6992 for (unsigned I = 0; I != N; ++I) {
6993 NestedNameSpecifier::SpecifierKind Kind
6994 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
6995 switch (Kind) {
6996 case NestedNameSpecifier::Identifier: {
6997 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
6998 SourceRange Range = ReadSourceRange(F, Record, Idx);
6999 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
7000 break;
7001 }
7002
7003 case NestedNameSpecifier::Namespace: {
7004 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
7005 SourceRange Range = ReadSourceRange(F, Record, Idx);
7006 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
7007 break;
7008 }
7009
7010 case NestedNameSpecifier::NamespaceAlias: {
7011 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
7012 SourceRange Range = ReadSourceRange(F, Record, Idx);
7013 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
7014 break;
7015 }
7016
7017 case NestedNameSpecifier::TypeSpec:
7018 case NestedNameSpecifier::TypeSpecWithTemplate: {
7019 bool Template = Record[Idx++];
7020 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
7021 if (!T)
7022 return NestedNameSpecifierLoc();
7023 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
7024
7025 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
7026 Builder.Extend(Context,
7027 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
7028 T->getTypeLoc(), ColonColonLoc);
7029 break;
7030 }
7031
7032 case NestedNameSpecifier::Global: {
7033 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
7034 Builder.MakeGlobal(Context, ColonColonLoc);
7035 break;
7036 }
7037 }
7038 }
7039
7040 return Builder.getWithLocInContext(Context);
7041}
7042
7043SourceRange
7044ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
7045 unsigned &Idx) {
7046 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
7047 SourceLocation end = ReadSourceLocation(F, Record, Idx);
7048 return SourceRange(beg, end);
7049}
7050
7051/// \brief Read an integral value
7052llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
7053 unsigned BitWidth = Record[Idx++];
7054 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
7055 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
7056 Idx += NumWords;
7057 return Result;
7058}
7059
7060/// \brief Read a signed integral value
7061llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
7062 bool isUnsigned = Record[Idx++];
7063 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
7064}
7065
7066/// \brief Read a floating-point value
Tim Northover9ec55f22013-01-22 09:46:51 +00007067llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record,
7068 const llvm::fltSemantics &Sem,
7069 unsigned &Idx) {
7070 return llvm::APFloat(Sem, ReadAPInt(Record, Idx));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007071}
7072
7073// \brief Read a string
7074std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
7075 unsigned Len = Record[Idx++];
7076 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
7077 Idx += Len;
7078 return Result;
7079}
7080
7081VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
7082 unsigned &Idx) {
7083 unsigned Major = Record[Idx++];
7084 unsigned Minor = Record[Idx++];
7085 unsigned Subminor = Record[Idx++];
7086 if (Minor == 0)
7087 return VersionTuple(Major);
7088 if (Subminor == 0)
7089 return VersionTuple(Major, Minor - 1);
7090 return VersionTuple(Major, Minor - 1, Subminor - 1);
7091}
7092
7093CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
7094 const RecordData &Record,
7095 unsigned &Idx) {
7096 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
7097 return CXXTemporary::Create(Context, Decl);
7098}
7099
7100DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
7101 return Diag(SourceLocation(), DiagID);
7102}
7103
7104DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
7105 return Diags.Report(Loc, DiagID);
7106}
7107
7108/// \brief Retrieve the identifier table associated with the
7109/// preprocessor.
7110IdentifierTable &ASTReader::getIdentifierTable() {
7111 return PP.getIdentifierTable();
7112}
7113
7114/// \brief Record that the given ID maps to the given switch-case
7115/// statement.
7116void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
7117 assert((*CurrSwitchCaseStmts)[ID] == 0 &&
7118 "Already have a SwitchCase with this ID");
7119 (*CurrSwitchCaseStmts)[ID] = SC;
7120}
7121
7122/// \brief Retrieve the switch-case statement with the given ID.
7123SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
7124 assert((*CurrSwitchCaseStmts)[ID] != 0 && "No SwitchCase with this ID");
7125 return (*CurrSwitchCaseStmts)[ID];
7126}
7127
7128void ASTReader::ClearSwitchCaseIDs() {
7129 CurrSwitchCaseStmts->clear();
7130}
7131
7132void ASTReader::ReadComments() {
7133 std::vector<RawComment *> Comments;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00007134 for (SmallVectorImpl<std::pair<BitstreamCursor,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007135 serialization::ModuleFile *> >::iterator
7136 I = CommentsCursors.begin(),
7137 E = CommentsCursors.end();
7138 I != E; ++I) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00007139 BitstreamCursor &Cursor = I->first;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007140 serialization::ModuleFile &F = *I->second;
7141 SavedStreamPosition SavedPosition(Cursor);
7142
7143 RecordData Record;
7144 while (true) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00007145 llvm::BitstreamEntry Entry =
7146 Cursor.advanceSkippingSubblocks(BitstreamCursor::AF_DontPopBlockAtEnd);
7147
7148 switch (Entry.Kind) {
7149 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
7150 case llvm::BitstreamEntry::Error:
7151 Error("malformed block record in AST file");
7152 return;
7153 case llvm::BitstreamEntry::EndBlock:
7154 goto NextCursor;
7155 case llvm::BitstreamEntry::Record:
7156 // The interesting case.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007157 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007158 }
7159
7160 // Read a record.
7161 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00007162 switch ((CommentRecordTypes)Cursor.readRecord(Entry.ID, Record)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007163 case COMMENTS_RAW_COMMENT: {
7164 unsigned Idx = 0;
7165 SourceRange SR = ReadSourceRange(F, Record, Idx);
7166 RawComment::CommentKind Kind =
7167 (RawComment::CommentKind) Record[Idx++];
7168 bool IsTrailingComment = Record[Idx++];
7169 bool IsAlmostTrailingComment = Record[Idx++];
Dmitri Gribenko6fd7d302013-04-10 15:35:17 +00007170 Comments.push_back(new (Context) RawComment(
7171 SR, Kind, IsTrailingComment, IsAlmostTrailingComment,
7172 Context.getLangOpts().CommentOpts.ParseAllComments));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007173 break;
7174 }
7175 }
7176 }
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00007177 NextCursor:;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007178 }
7179 Context.Comments.addCommentsToFront(Comments);
7180}
7181
7182void ASTReader::finishPendingActions() {
7183 while (!PendingIdentifierInfos.empty() || !PendingDeclChains.empty() ||
Argyrios Kyrtzidis7640b022013-02-16 00:48:59 +00007184 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty()) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007185 // If any identifiers with corresponding top-level declarations have
7186 // been loaded, load those declarations now.
Douglas Gregoraa945902013-02-18 15:53:43 +00007187 llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2> > TopLevelDecls;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007188 while (!PendingIdentifierInfos.empty()) {
Douglas Gregoraa945902013-02-18 15:53:43 +00007189 // FIXME: std::move
7190 IdentifierInfo *II = PendingIdentifierInfos.back().first;
7191 SmallVector<uint32_t, 4> DeclIDs = PendingIdentifierInfos.back().second;
Douglas Gregorcc9bdcb2013-02-19 18:26:28 +00007192 PendingIdentifierInfos.pop_back();
Douglas Gregoraa945902013-02-18 15:53:43 +00007193
7194 SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007195 }
7196
7197 // Load pending declaration chains.
7198 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) {
7199 loadPendingDeclChain(PendingDeclChains[I]);
7200 PendingDeclChainsKnown.erase(PendingDeclChains[I]);
7201 }
7202 PendingDeclChains.clear();
7203
Douglas Gregoraa945902013-02-18 15:53:43 +00007204 // Make the most recent of the top-level declarations visible.
7205 for (llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2> >::iterator
7206 TLD = TopLevelDecls.begin(), TLDEnd = TopLevelDecls.end();
7207 TLD != TLDEnd; ++TLD) {
7208 IdentifierInfo *II = TLD->first;
7209 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
Argyrios Kyrtzidis0532df02013-04-26 21:33:35 +00007210 pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
Douglas Gregoraa945902013-02-18 15:53:43 +00007211 }
7212 }
7213
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007214 // Load any pending macro definitions.
7215 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00007216 IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
7217 SmallVector<PendingMacroInfo, 2> GlobalIDs;
7218 GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
7219 // Initialize the macro history from chained-PCHs ahead of module imports.
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00007220 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
7221 ++IDIdx) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00007222 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
7223 if (Info.M->Kind != MK_Module)
7224 resolvePendingMacro(II, Info);
7225 }
7226 // Handle module imports.
7227 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
7228 ++IDIdx) {
7229 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
7230 if (Info.M->Kind == MK_Module)
7231 resolvePendingMacro(II, Info);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007232 }
7233 }
7234 PendingMacroIDs.clear();
Argyrios Kyrtzidis7640b022013-02-16 00:48:59 +00007235
7236 // Wire up the DeclContexts for Decls that we delayed setting until
7237 // recursive loading is completed.
7238 while (!PendingDeclContextInfos.empty()) {
7239 PendingDeclContextInfo Info = PendingDeclContextInfos.front();
7240 PendingDeclContextInfos.pop_front();
7241 DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
7242 DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
7243 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
7244 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007245 }
7246
7247 // If we deserialized any C++ or Objective-C class definitions, any
7248 // Objective-C protocol definitions, or any redeclarable templates, make sure
7249 // that all redeclarations point to the definitions. Note that this can only
7250 // happen now, after the redeclaration chains have been fully wired.
7251 for (llvm::SmallPtrSet<Decl *, 4>::iterator D = PendingDefinitions.begin(),
7252 DEnd = PendingDefinitions.end();
7253 D != DEnd; ++D) {
7254 if (TagDecl *TD = dyn_cast<TagDecl>(*D)) {
7255 if (const TagType *TagT = dyn_cast<TagType>(TD->TypeForDecl)) {
7256 // Make sure that the TagType points at the definition.
7257 const_cast<TagType*>(TagT)->decl = TD;
7258 }
7259
7260 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(*D)) {
7261 for (CXXRecordDecl::redecl_iterator R = RD->redecls_begin(),
7262 REnd = RD->redecls_end();
7263 R != REnd; ++R)
7264 cast<CXXRecordDecl>(*R)->DefinitionData = RD->DefinitionData;
7265
7266 }
7267
7268 continue;
7269 }
7270
7271 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(*D)) {
7272 // Make sure that the ObjCInterfaceType points at the definition.
7273 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
7274 ->Decl = ID;
7275
7276 for (ObjCInterfaceDecl::redecl_iterator R = ID->redecls_begin(),
7277 REnd = ID->redecls_end();
7278 R != REnd; ++R)
7279 R->Data = ID->Data;
7280
7281 continue;
7282 }
7283
7284 if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(*D)) {
7285 for (ObjCProtocolDecl::redecl_iterator R = PD->redecls_begin(),
7286 REnd = PD->redecls_end();
7287 R != REnd; ++R)
7288 R->Data = PD->Data;
7289
7290 continue;
7291 }
7292
7293 RedeclarableTemplateDecl *RTD
7294 = cast<RedeclarableTemplateDecl>(*D)->getCanonicalDecl();
7295 for (RedeclarableTemplateDecl::redecl_iterator R = RTD->redecls_begin(),
7296 REnd = RTD->redecls_end();
7297 R != REnd; ++R)
7298 R->Common = RTD->Common;
7299 }
7300 PendingDefinitions.clear();
7301
7302 // Load the bodies of any functions or methods we've encountered. We do
7303 // this now (delayed) so that we can be sure that the declaration chains
7304 // have been fully wired up.
7305 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
7306 PBEnd = PendingBodies.end();
7307 PB != PBEnd; ++PB) {
7308 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
7309 // FIXME: Check for =delete/=default?
7310 // FIXME: Complain about ODR violations here?
7311 if (!getContext().getLangOpts().Modules || !FD->hasBody())
7312 FD->setLazyBody(PB->second);
7313 continue;
7314 }
7315
7316 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
7317 if (!getContext().getLangOpts().Modules || !MD->hasBody())
7318 MD->setLazyBody(PB->second);
7319 }
7320 PendingBodies.clear();
7321}
7322
7323void ASTReader::FinishedDeserializing() {
7324 assert(NumCurrentElementsDeserializing &&
7325 "FinishedDeserializing not paired with StartedDeserializing");
7326 if (NumCurrentElementsDeserializing == 1) {
7327 // We decrease NumCurrentElementsDeserializing only after pending actions
7328 // are finished, to avoid recursively re-calling finishPendingActions().
7329 finishPendingActions();
7330 }
7331 --NumCurrentElementsDeserializing;
7332
7333 if (NumCurrentElementsDeserializing == 0 &&
7334 Consumer && !PassingDeclsToConsumer) {
7335 // Guard variable to avoid recursively redoing the process of passing
7336 // decls to consumer.
7337 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
7338 true);
7339
7340 while (!InterestingDecls.empty()) {
7341 // We are not in recursive loading, so it's safe to pass the "interesting"
7342 // decls to the consumer.
7343 Decl *D = InterestingDecls.front();
7344 InterestingDecls.pop_front();
7345 PassInterestingDeclToConsumer(D);
7346 }
7347 }
7348}
7349
Argyrios Kyrtzidis0532df02013-04-26 21:33:35 +00007350void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
7351 D = cast<NamedDecl>(D->getMostRecentDecl());
7352
7353 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
7354 SemaObj->TUScope->AddDecl(D);
7355 } else if (SemaObj->TUScope) {
7356 // Adding the decl to IdResolver may have failed because it was already in
7357 // (even though it was not added in scope). If it is already in, make sure
7358 // it gets in the scope as well.
7359 if (std::find(SemaObj->IdResolver.begin(Name),
7360 SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
7361 SemaObj->TUScope->AddDecl(D);
7362 }
7363}
7364
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007365ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context,
7366 StringRef isysroot, bool DisableValidation,
Douglas Gregorf575d6e2013-01-25 00:45:27 +00007367 bool AllowASTWithCompilerErrors, bool UseGlobalIndex)
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007368 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
7369 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
7370 Diags(PP.getDiagnostics()), SemaObj(0), PP(PP), Context(Context),
7371 Consumer(0), ModuleMgr(PP.getFileManager()),
7372 isysroot(isysroot), DisableValidation(DisableValidation),
Douglas Gregore1698072013-01-25 00:38:33 +00007373 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
Douglas Gregorf575d6e2013-01-25 00:45:27 +00007374 UseGlobalIndex(UseGlobalIndex), TriedLoadingGlobalIndex(false),
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007375 CurrentGeneration(0), CurrSwitchCaseStmts(&SwitchCaseStmts),
7376 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Douglas Gregore1698072013-01-25 00:38:33 +00007377 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
7378 TotalNumMacros(0), NumIdentifierLookups(0), NumIdentifierLookupHits(0),
7379 NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
Douglas Gregor95fb36e2013-01-28 17:54:36 +00007380 NumMethodPoolLookups(0), NumMethodPoolHits(0),
7381 NumMethodPoolTableLookups(0), NumMethodPoolTableHits(0),
7382 TotalNumMethodPoolEntries(0),
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007383 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
7384 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
7385 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
7386 PassingDeclsToConsumer(false),
7387 NumCXXBaseSpecifiersLoaded(0)
7388{
7389 SourceMgr.setExternalSLocEntrySource(this);
7390}
7391
7392ASTReader::~ASTReader() {
7393 for (DeclContextVisibleUpdatesPending::iterator
7394 I = PendingVisibleUpdates.begin(),
7395 E = PendingVisibleUpdates.end();
7396 I != E; ++I) {
7397 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
7398 F = I->second.end();
7399 J != F; ++J)
7400 delete J->first;
7401 }
7402}