blob: 837524dec886e33a17b3156842816eb96c2c5276 [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));
431 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
432 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
433
434 // Load instance methods
435 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
436 if (ObjCMethodDecl *Method
437 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d)))
438 Result.Instance.push_back(Method);
439 }
440
441 // Load factory methods
442 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
443 if (ObjCMethodDecl *Method
444 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d)))
445 Result.Factory.push_back(Method);
446 }
447
448 return Result;
449}
450
Douglas Gregor479633c2013-01-23 18:53:14 +0000451unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
452 return llvm::HashString(a);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000453}
454
455std::pair<unsigned, unsigned>
Douglas Gregor479633c2013-01-23 18:53:14 +0000456ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000457 using namespace clang::io;
458 unsigned DataLen = ReadUnalignedLE16(d);
459 unsigned KeyLen = ReadUnalignedLE16(d);
460 return std::make_pair(KeyLen, DataLen);
461}
462
Douglas Gregor479633c2013-01-23 18:53:14 +0000463ASTIdentifierLookupTraitBase::internal_key_type
464ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000465 assert(n >= 2 && d[n-1] == '\0');
Douglas Gregor479633c2013-01-23 18:53:14 +0000466 return StringRef((const char*) d, n-1);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000467}
468
Douglas Gregorf4e955b2013-02-11 18:16:18 +0000469/// \brief Whether the given identifier is "interesting".
470static bool isInterestingIdentifier(IdentifierInfo &II) {
471 return II.isPoisoned() ||
472 II.isExtensionToken() ||
473 II.getObjCOrBuiltinID() ||
474 II.hasRevertedTokenIDToIdentifier() ||
475 II.hadMacroDefinition() ||
476 II.getFETokenInfo<void>();
477}
478
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000479IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
480 const unsigned char* d,
481 unsigned DataLen) {
482 using namespace clang::io;
483 unsigned RawID = ReadUnalignedLE32(d);
484 bool IsInteresting = RawID & 0x01;
485
486 // Wipe out the "is interesting" bit.
487 RawID = RawID >> 1;
488
489 IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
490 if (!IsInteresting) {
491 // For uninteresting identifiers, just build the IdentifierInfo
492 // and associate it with the persistent ID.
493 IdentifierInfo *II = KnownII;
494 if (!II) {
Douglas Gregor479633c2013-01-23 18:53:14 +0000495 II = &Reader.getIdentifierTable().getOwn(k);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000496 KnownII = II;
497 }
498 Reader.SetIdentifierInfo(ID, II);
Douglas Gregorf4e955b2013-02-11 18:16:18 +0000499 if (!II->isFromAST()) {
500 bool WasInteresting = isInterestingIdentifier(*II);
501 II->setIsFromAST();
502 if (WasInteresting)
503 II->setChangedSinceDeserialization();
504 }
505 Reader.markIdentifierUpToDate(II);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000506 return II;
507 }
508
509 unsigned ObjCOrBuiltinID = ReadUnalignedLE16(d);
510 unsigned Bits = ReadUnalignedLE16(d);
511 bool CPlusPlusOperatorKeyword = Bits & 0x01;
512 Bits >>= 1;
513 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
514 Bits >>= 1;
515 bool Poisoned = Bits & 0x01;
516 Bits >>= 1;
517 bool ExtensionToken = Bits & 0x01;
518 Bits >>= 1;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +0000519 bool hasSubmoduleMacros = Bits & 0x01;
520 Bits >>= 1;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000521 bool hadMacroDefinition = Bits & 0x01;
522 Bits >>= 1;
523
524 assert(Bits == 0 && "Extra bits in the identifier?");
525 DataLen -= 8;
526
527 // Build the IdentifierInfo itself and link the identifier ID with
528 // the new IdentifierInfo.
529 IdentifierInfo *II = KnownII;
530 if (!II) {
Douglas Gregor479633c2013-01-23 18:53:14 +0000531 II = &Reader.getIdentifierTable().getOwn(StringRef(k));
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000532 KnownII = II;
533 }
534 Reader.markIdentifierUpToDate(II);
Douglas Gregorf4e955b2013-02-11 18:16:18 +0000535 if (!II->isFromAST()) {
536 bool WasInteresting = isInterestingIdentifier(*II);
537 II->setIsFromAST();
538 if (WasInteresting)
539 II->setChangedSinceDeserialization();
540 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000541
542 // Set or check the various bits in the IdentifierInfo structure.
543 // Token IDs are read-only.
Argyrios Kyrtzidis1ebefc72013-02-27 01:13:51 +0000544 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000545 II->RevertTokenIDToIdentifier();
546 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
547 assert(II->isExtensionToken() == ExtensionToken &&
548 "Incorrect extension token flag");
549 (void)ExtensionToken;
550 if (Poisoned)
551 II->setIsPoisoned(true);
552 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
553 "Incorrect C++ operator keyword flag");
554 (void)CPlusPlusOperatorKeyword;
555
556 // If this identifier is a macro, deserialize the macro
557 // definition.
558 if (hadMacroDefinition) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +0000559 uint32_t MacroDirectivesOffset = ReadUnalignedLE32(d);
560 DataLen -= 4;
561 SmallVector<uint32_t, 8> LocalMacroIDs;
562 if (hasSubmoduleMacros) {
563 while (uint32_t LocalMacroID = ReadUnalignedLE32(d)) {
564 DataLen -= 4;
565 LocalMacroIDs.push_back(LocalMacroID);
566 }
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +0000567 DataLen -= 4;
568 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +0000569
570 if (F.Kind == MK_Module) {
571 for (SmallVectorImpl<uint32_t>::iterator
572 I = LocalMacroIDs.begin(), E = LocalMacroIDs.end(); I != E; ++I) {
573 MacroID MacID = Reader.getGlobalMacroID(F, *I);
574 Reader.addPendingMacroFromModule(II, &F, MacID, F.DirectImportLoc);
575 }
576 } else {
577 Reader.addPendingMacroFromPCH(II, &F, MacroDirectivesOffset);
578 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000579 }
580
581 Reader.SetIdentifierInfo(ID, II);
582
583 // Read all of the declarations visible at global scope with this
584 // name.
585 if (DataLen > 0) {
586 SmallVector<uint32_t, 4> DeclIDs;
587 for (; DataLen > 0; DataLen -= 4)
588 DeclIDs.push_back(Reader.getGlobalDeclID(F, ReadUnalignedLE32(d)));
589 Reader.SetGloballyVisibleDecls(II, DeclIDs);
590 }
591
592 return II;
593}
594
595unsigned
596ASTDeclContextNameLookupTrait::ComputeHash(const DeclNameKey &Key) const {
597 llvm::FoldingSetNodeID ID;
598 ID.AddInteger(Key.Kind);
599
600 switch (Key.Kind) {
601 case DeclarationName::Identifier:
602 case DeclarationName::CXXLiteralOperatorName:
603 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
604 break;
605 case DeclarationName::ObjCZeroArgSelector:
606 case DeclarationName::ObjCOneArgSelector:
607 case DeclarationName::ObjCMultiArgSelector:
608 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
609 break;
610 case DeclarationName::CXXOperatorName:
611 ID.AddInteger((OverloadedOperatorKind)Key.Data);
612 break;
613 case DeclarationName::CXXConstructorName:
614 case DeclarationName::CXXDestructorName:
615 case DeclarationName::CXXConversionFunctionName:
616 case DeclarationName::CXXUsingDirective:
617 break;
618 }
619
620 return ID.ComputeHash();
621}
622
623ASTDeclContextNameLookupTrait::internal_key_type
624ASTDeclContextNameLookupTrait::GetInternalKey(
625 const external_key_type& Name) const {
626 DeclNameKey Key;
627 Key.Kind = Name.getNameKind();
628 switch (Name.getNameKind()) {
629 case DeclarationName::Identifier:
630 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
631 break;
632 case DeclarationName::ObjCZeroArgSelector:
633 case DeclarationName::ObjCOneArgSelector:
634 case DeclarationName::ObjCMultiArgSelector:
635 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
636 break;
637 case DeclarationName::CXXOperatorName:
638 Key.Data = Name.getCXXOverloadedOperator();
639 break;
640 case DeclarationName::CXXLiteralOperatorName:
641 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
642 break;
643 case DeclarationName::CXXConstructorName:
644 case DeclarationName::CXXDestructorName:
645 case DeclarationName::CXXConversionFunctionName:
646 case DeclarationName::CXXUsingDirective:
647 Key.Data = 0;
648 break;
649 }
650
651 return Key;
652}
653
654std::pair<unsigned, unsigned>
655ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
656 using namespace clang::io;
657 unsigned KeyLen = ReadUnalignedLE16(d);
658 unsigned DataLen = ReadUnalignedLE16(d);
659 return std::make_pair(KeyLen, DataLen);
660}
661
662ASTDeclContextNameLookupTrait::internal_key_type
663ASTDeclContextNameLookupTrait::ReadKey(const unsigned char* d, unsigned) {
664 using namespace clang::io;
665
666 DeclNameKey Key;
667 Key.Kind = (DeclarationName::NameKind)*d++;
668 switch (Key.Kind) {
669 case DeclarationName::Identifier:
670 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
671 break;
672 case DeclarationName::ObjCZeroArgSelector:
673 case DeclarationName::ObjCOneArgSelector:
674 case DeclarationName::ObjCMultiArgSelector:
675 Key.Data =
676 (uint64_t)Reader.getLocalSelector(F, ReadUnalignedLE32(d))
677 .getAsOpaquePtr();
678 break;
679 case DeclarationName::CXXOperatorName:
680 Key.Data = *d++; // OverloadedOperatorKind
681 break;
682 case DeclarationName::CXXLiteralOperatorName:
683 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
684 break;
685 case DeclarationName::CXXConstructorName:
686 case DeclarationName::CXXDestructorName:
687 case DeclarationName::CXXConversionFunctionName:
688 case DeclarationName::CXXUsingDirective:
689 Key.Data = 0;
690 break;
691 }
692
693 return Key;
694}
695
696ASTDeclContextNameLookupTrait::data_type
697ASTDeclContextNameLookupTrait::ReadData(internal_key_type,
698 const unsigned char* d,
699 unsigned DataLen) {
700 using namespace clang::io;
701 unsigned NumDecls = ReadUnalignedLE16(d);
Argyrios Kyrtzidise8b61cf2013-01-11 22:29:49 +0000702 LE32DeclID *Start = reinterpret_cast<LE32DeclID *>(
703 const_cast<unsigned char *>(d));
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000704 return std::make_pair(Start, Start + NumDecls);
705}
706
707bool ASTReader::ReadDeclContextStorage(ModuleFile &M,
Chris Lattner8f9a1eb2013-01-20 00:56:42 +0000708 BitstreamCursor &Cursor,
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000709 const std::pair<uint64_t, uint64_t> &Offsets,
710 DeclContextInfo &Info) {
711 SavedStreamPosition SavedPosition(Cursor);
712 // First the lexical decls.
713 if (Offsets.first != 0) {
714 Cursor.JumpToBit(Offsets.first);
715
716 RecordData Record;
Chris Lattnerb3ce3572013-01-20 02:38:54 +0000717 StringRef Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000718 unsigned Code = Cursor.ReadCode();
Chris Lattnerb3ce3572013-01-20 02:38:54 +0000719 unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000720 if (RecCode != DECL_CONTEXT_LEXICAL) {
721 Error("Expected lexical block");
722 return true;
723 }
724
Chris Lattnerb3ce3572013-01-20 02:38:54 +0000725 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob.data());
726 Info.NumLexicalDecls = Blob.size() / sizeof(KindDeclIDPair);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000727 }
728
729 // Now the lookup table.
730 if (Offsets.second != 0) {
731 Cursor.JumpToBit(Offsets.second);
732
733 RecordData Record;
Chris Lattnerb3ce3572013-01-20 02:38:54 +0000734 StringRef Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000735 unsigned Code = Cursor.ReadCode();
Chris Lattnerb3ce3572013-01-20 02:38:54 +0000736 unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000737 if (RecCode != DECL_CONTEXT_VISIBLE) {
738 Error("Expected visible lookup table block");
739 return true;
740 }
741 Info.NameLookupTableData
742 = ASTDeclContextNameLookupTable::Create(
Chris Lattnerb3ce3572013-01-20 02:38:54 +0000743 (const unsigned char *)Blob.data() + Record[0],
744 (const unsigned char *)Blob.data(),
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000745 ASTDeclContextNameLookupTrait(*this, M));
746 }
747
748 return false;
749}
750
751void ASTReader::Error(StringRef Msg) {
752 Error(diag::err_fe_pch_malformed, Msg);
753}
754
755void ASTReader::Error(unsigned DiagID,
756 StringRef Arg1, StringRef Arg2) {
757 if (Diags.isDiagnosticInFlight())
758 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
759 else
760 Diag(DiagID) << Arg1 << Arg2;
761}
762
763//===----------------------------------------------------------------------===//
764// Source Manager Deserialization
765//===----------------------------------------------------------------------===//
766
767/// \brief Read the line table in the source manager block.
768/// \returns true if there was an error.
769bool ASTReader::ParseLineTable(ModuleFile &F,
770 SmallVectorImpl<uint64_t> &Record) {
771 unsigned Idx = 0;
772 LineTableInfo &LineTable = SourceMgr.getLineTable();
773
774 // Parse the file names
775 std::map<int, int> FileIDs;
776 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
777 // Extract the file name
778 unsigned FilenameLen = Record[Idx++];
779 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
780 Idx += FilenameLen;
781 MaybeAddSystemRootToFilename(F, Filename);
782 FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
783 }
784
785 // Parse the line entries
786 std::vector<LineEntry> Entries;
787 while (Idx < Record.size()) {
788 int FID = Record[Idx++];
789 assert(FID >= 0 && "Serialized line entries for non-local file.");
790 // Remap FileID from 1-based old view.
791 FID += F.SLocEntryBaseID - 1;
792
793 // Extract the line entries
794 unsigned NumEntries = Record[Idx++];
795 assert(NumEntries && "Numentries is 00000");
796 Entries.clear();
797 Entries.reserve(NumEntries);
798 for (unsigned I = 0; I != NumEntries; ++I) {
799 unsigned FileOffset = Record[Idx++];
800 unsigned LineNo = Record[Idx++];
801 int FilenameID = FileIDs[Record[Idx++]];
802 SrcMgr::CharacteristicKind FileKind
803 = (SrcMgr::CharacteristicKind)Record[Idx++];
804 unsigned IncludeOffset = Record[Idx++];
805 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
806 FileKind, IncludeOffset));
807 }
808 LineTable.AddEntry(FileID::get(FID), Entries);
809 }
810
811 return false;
812}
813
814/// \brief Read a source manager block
815bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
816 using namespace SrcMgr;
817
Chris Lattner8f9a1eb2013-01-20 00:56:42 +0000818 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000819
820 // Set the source-location entry cursor to the current position in
821 // the stream. This cursor will be used to read the contents of the
822 // source manager block initially, and then lazily read
823 // source-location entries as needed.
824 SLocEntryCursor = F.Stream;
825
826 // The stream itself is going to skip over the source manager block.
827 if (F.Stream.SkipBlock()) {
828 Error("malformed block record in AST file");
829 return true;
830 }
831
832 // Enter the source manager block.
833 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
834 Error("malformed source manager block record in AST file");
835 return true;
836 }
837
838 RecordData Record;
839 while (true) {
Chris Lattner88bde502013-01-19 21:39:22 +0000840 llvm::BitstreamEntry E = SLocEntryCursor.advanceSkippingSubblocks();
841
842 switch (E.Kind) {
843 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
844 case llvm::BitstreamEntry::Error:
845 Error("malformed block record in AST file");
846 return true;
847 case llvm::BitstreamEntry::EndBlock:
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000848 return false;
Chris Lattner88bde502013-01-19 21:39:22 +0000849 case llvm::BitstreamEntry::Record:
850 // The interesting case.
851 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000852 }
Chris Lattner88bde502013-01-19 21:39:22 +0000853
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000854 // Read a record.
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000855 Record.clear();
Chris Lattner125eb3e2013-01-21 18:28:26 +0000856 StringRef Blob;
857 switch (SLocEntryCursor.readRecord(E.ID, Record, &Blob)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000858 default: // Default behavior: ignore.
859 break;
860
861 case SM_SLOC_FILE_ENTRY:
862 case SM_SLOC_BUFFER_ENTRY:
863 case SM_SLOC_EXPANSION_ENTRY:
864 // Once we hit one of the source location entries, we're done.
865 return false;
866 }
867 }
868}
869
870/// \brief If a header file is not found at the path that we expect it to be
871/// and the PCH file was moved from its original location, try to resolve the
872/// file by assuming that header+PCH were moved together and the header is in
873/// the same place relative to the PCH.
874static std::string
875resolveFileRelativeToOriginalDir(const std::string &Filename,
876 const std::string &OriginalDir,
877 const std::string &CurrDir) {
878 assert(OriginalDir != CurrDir &&
879 "No point trying to resolve the file if the PCH dir didn't change");
880 using namespace llvm::sys;
881 SmallString<128> filePath(Filename);
882 fs::make_absolute(filePath);
883 assert(path::is_absolute(OriginalDir));
884 SmallString<128> currPCHPath(CurrDir);
885
886 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
887 fileDirE = path::end(path::parent_path(filePath));
888 path::const_iterator origDirI = path::begin(OriginalDir),
889 origDirE = path::end(OriginalDir);
890 // Skip the common path components from filePath and OriginalDir.
891 while (fileDirI != fileDirE && origDirI != origDirE &&
892 *fileDirI == *origDirI) {
893 ++fileDirI;
894 ++origDirI;
895 }
896 for (; origDirI != origDirE; ++origDirI)
897 path::append(currPCHPath, "..");
898 path::append(currPCHPath, fileDirI, fileDirE);
899 path::append(currPCHPath, path::filename(Filename));
900 return currPCHPath.str();
901}
902
903bool ASTReader::ReadSLocEntry(int ID) {
904 if (ID == 0)
905 return false;
906
907 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
908 Error("source location entry ID out-of-range for AST file");
909 return true;
910 }
911
912 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
913 F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]);
Chris Lattner8f9a1eb2013-01-20 00:56:42 +0000914 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000915 unsigned BaseOffset = F->SLocEntryBaseOffset;
916
917 ++NumSLocEntriesRead;
Chris Lattner88bde502013-01-19 21:39:22 +0000918 llvm::BitstreamEntry Entry = SLocEntryCursor.advance();
919 if (Entry.Kind != llvm::BitstreamEntry::Record) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000920 Error("incorrectly-formatted source location entry in AST file");
921 return true;
922 }
Chris Lattner88bde502013-01-19 21:39:22 +0000923
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000924 RecordData Record;
Chris Lattnerb3ce3572013-01-20 02:38:54 +0000925 StringRef Blob;
926 switch (SLocEntryCursor.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000927 default:
928 Error("incorrectly-formatted source location entry in AST file");
929 return true;
930
931 case SM_SLOC_FILE_ENTRY: {
932 // We will detect whether a file changed and return 'Failure' for it, but
933 // we will also try to fail gracefully by setting up the SLocEntry.
934 unsigned InputID = Record[4];
935 InputFile IF = getInputFile(*F, InputID);
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +0000936 const FileEntry *File = IF.getFile();
937 bool OverriddenBuffer = IF.isOverridden();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000938
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +0000939 // Note that we only check if a File was returned. If it was out-of-date
940 // we have complained but we will continue creating a FileID to recover
941 // gracefully.
942 if (!File)
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000943 return true;
944
945 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
946 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
947 // This is the module's main file.
948 IncludeLoc = getImportLocation(F);
949 }
950 SrcMgr::CharacteristicKind
951 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
952 FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
953 ID, BaseOffset + Record[0]);
954 SrcMgr::FileInfo &FileInfo =
955 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
956 FileInfo.NumCreatedFIDs = Record[5];
957 if (Record[3])
958 FileInfo.setHasLineDirectives();
959
960 const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
961 unsigned NumFileDecls = Record[7];
962 if (NumFileDecls) {
963 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
964 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
965 NumFileDecls));
966 }
967
968 const SrcMgr::ContentCache *ContentCache
969 = SourceMgr.getOrCreateContentCache(File,
970 /*isSystemFile=*/FileCharacter != SrcMgr::C_User);
971 if (OverriddenBuffer && !ContentCache->BufferOverridden &&
972 ContentCache->ContentsEntry == ContentCache->OrigEntry) {
973 unsigned Code = SLocEntryCursor.ReadCode();
974 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +0000975 unsigned RecCode = SLocEntryCursor.readRecord(Code, Record, &Blob);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000976
977 if (RecCode != SM_SLOC_BUFFER_BLOB) {
978 Error("AST record has invalid code");
979 return true;
980 }
981
982 llvm::MemoryBuffer *Buffer
Chris Lattnerb3ce3572013-01-20 02:38:54 +0000983 = llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), File->getName());
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000984 SourceMgr.overrideFileContents(File, Buffer);
985 }
986
987 break;
988 }
989
990 case SM_SLOC_BUFFER_ENTRY: {
Chris Lattnerb3ce3572013-01-20 02:38:54 +0000991 const char *Name = Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000992 unsigned Offset = Record[0];
993 SrcMgr::CharacteristicKind
994 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
995 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
996 if (IncludeLoc.isInvalid() && F->Kind == MK_Module) {
997 IncludeLoc = getImportLocation(F);
998 }
999 unsigned Code = SLocEntryCursor.ReadCode();
1000 Record.clear();
1001 unsigned RecCode
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001002 = SLocEntryCursor.readRecord(Code, Record, &Blob);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001003
1004 if (RecCode != SM_SLOC_BUFFER_BLOB) {
1005 Error("AST record has invalid code");
1006 return true;
1007 }
1008
1009 llvm::MemoryBuffer *Buffer
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001010 = llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001011 SourceMgr.createFileIDForMemBuffer(Buffer, FileCharacter, ID,
1012 BaseOffset + Offset, IncludeLoc);
1013 break;
1014 }
1015
1016 case SM_SLOC_EXPANSION_ENTRY: {
1017 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1018 SourceMgr.createExpansionLoc(SpellingLoc,
1019 ReadSourceLocation(*F, Record[2]),
1020 ReadSourceLocation(*F, Record[3]),
1021 Record[4],
1022 ID,
1023 BaseOffset + Record[0]);
1024 break;
1025 }
1026 }
1027
1028 return false;
1029}
1030
1031std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1032 if (ID == 0)
1033 return std::make_pair(SourceLocation(), "");
1034
1035 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1036 Error("source location entry ID out-of-range for AST file");
1037 return std::make_pair(SourceLocation(), "");
1038 }
1039
1040 // Find which module file this entry lands in.
1041 ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
1042 if (M->Kind != MK_Module)
1043 return std::make_pair(SourceLocation(), "");
1044
1045 // FIXME: Can we map this down to a particular submodule? That would be
1046 // ideal.
1047 return std::make_pair(M->ImportLoc, llvm::sys::path::stem(M->FileName));
1048}
1049
1050/// \brief Find the location where the module F is imported.
1051SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1052 if (F->ImportLoc.isValid())
1053 return F->ImportLoc;
1054
1055 // Otherwise we have a PCH. It's considered to be "imported" at the first
1056 // location of its includer.
1057 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
1058 // Main file is the importer. We assume that it is the first entry in the
1059 // entry table. We can't ask the manager, because at the time of PCH loading
1060 // the main file entry doesn't exist yet.
1061 // The very first entry is the invalid instantiation loc, which takes up
1062 // offsets 0 and 1.
1063 return SourceLocation::getFromRawEncoding(2U);
1064 }
1065 //return F->Loaders[0]->FirstLoc;
1066 return F->ImportedBy[0]->FirstLoc;
1067}
1068
1069/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1070/// specified cursor. Read the abbreviations that are at the top of the block
1071/// and then leave the cursor pointing into the block.
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001072bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001073 if (Cursor.EnterSubBlock(BlockID)) {
1074 Error("malformed block record in AST file");
1075 return Failure;
1076 }
1077
1078 while (true) {
1079 uint64_t Offset = Cursor.GetCurrentBitNo();
1080 unsigned Code = Cursor.ReadCode();
1081
1082 // We expect all abbrevs to be at the start of the block.
1083 if (Code != llvm::bitc::DEFINE_ABBREV) {
1084 Cursor.JumpToBit(Offset);
1085 return false;
1086 }
1087 Cursor.ReadAbbrevRecord();
1088 }
1089}
1090
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001091MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001092 BitstreamCursor &Stream = F.MacroCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001093
1094 // Keep track of where we are in the stream, then jump back there
1095 // after reading this macro.
1096 SavedStreamPosition SavedPosition(Stream);
1097
1098 Stream.JumpToBit(Offset);
1099 RecordData Record;
1100 SmallVector<IdentifierInfo*, 16> MacroArgs;
1101 MacroInfo *Macro = 0;
1102
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001103 while (true) {
Chris Lattner99a5af02013-01-20 00:00:22 +00001104 // Advance to the next record, but if we get to the end of the block, don't
1105 // pop it (removing all the abbreviations from the cursor) since we want to
1106 // be able to reseek within the block and read entries.
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001107 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
Chris Lattner99a5af02013-01-20 00:00:22 +00001108 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks(Flags);
1109
1110 switch (Entry.Kind) {
1111 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1112 case llvm::BitstreamEntry::Error:
1113 Error("malformed block record in AST file");
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001114 return Macro;
Chris Lattner99a5af02013-01-20 00:00:22 +00001115 case llvm::BitstreamEntry::EndBlock:
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001116 return Macro;
Chris Lattner99a5af02013-01-20 00:00:22 +00001117 case llvm::BitstreamEntry::Record:
1118 // The interesting case.
1119 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001120 }
1121
1122 // Read a record.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001123 Record.clear();
1124 PreprocessorRecordTypes RecType =
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001125 (PreprocessorRecordTypes)Stream.readRecord(Entry.ID, Record);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001126 switch (RecType) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001127 case PP_MACRO_DIRECTIVE_HISTORY:
1128 return Macro;
1129
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001130 case PP_MACRO_OBJECT_LIKE:
1131 case PP_MACRO_FUNCTION_LIKE: {
1132 // If we already have a macro, that means that we've hit the end
1133 // of the definition of the macro we were looking for. We're
1134 // done.
1135 if (Macro)
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001136 return Macro;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001137
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001138 unsigned NextIndex = 1; // Skip identifier ID.
1139 SubmoduleID SubModID = getGlobalSubmoduleID(F, Record[NextIndex++]);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001140 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001141 MacroInfo *MI = PP.AllocateDeserializedMacroInfo(Loc, SubModID);
Argyrios Kyrtzidis8169b672013-01-07 19:16:23 +00001142 MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001143 MI->setIsUsed(Record[NextIndex++]);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001144
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001145 if (RecType == PP_MACRO_FUNCTION_LIKE) {
1146 // Decode function-like macro info.
1147 bool isC99VarArgs = Record[NextIndex++];
1148 bool isGNUVarArgs = Record[NextIndex++];
1149 bool hasCommaPasting = Record[NextIndex++];
1150 MacroArgs.clear();
1151 unsigned NumArgs = Record[NextIndex++];
1152 for (unsigned i = 0; i != NumArgs; ++i)
1153 MacroArgs.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1154
1155 // Install function-like macro info.
1156 MI->setIsFunctionLike();
1157 if (isC99VarArgs) MI->setIsC99Varargs();
1158 if (isGNUVarArgs) MI->setIsGNUVarargs();
1159 if (hasCommaPasting) MI->setHasCommaPasting();
1160 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
1161 PP.getPreprocessorAllocator());
1162 }
1163
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001164 // Remember that we saw this macro last so that we add the tokens that
1165 // form its body to it.
1166 Macro = MI;
1167
1168 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1169 Record[NextIndex]) {
1170 // We have a macro definition. Register the association
1171 PreprocessedEntityID
1172 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1173 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
Argyrios Kyrtzidis0b849d32013-02-22 18:35:59 +00001174 PreprocessingRecord::PPEntityID
1175 PPID = PPRec.getPPEntityID(GlobalID-1, /*isLoaded=*/true);
1176 MacroDefinition *PPDef =
1177 cast_or_null<MacroDefinition>(PPRec.getPreprocessedEntity(PPID));
1178 if (PPDef)
1179 PPRec.RegisterMacroDefinition(Macro, PPDef);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001180 }
1181
1182 ++NumMacrosRead;
1183 break;
1184 }
1185
1186 case PP_TOKEN: {
1187 // If we see a TOKEN before a PP_MACRO_*, then the file is
1188 // erroneous, just pretend we didn't see this.
1189 if (Macro == 0) break;
1190
1191 Token Tok;
1192 Tok.startToken();
1193 Tok.setLocation(ReadSourceLocation(F, Record[0]));
1194 Tok.setLength(Record[1]);
1195 if (IdentifierInfo *II = getLocalIdentifier(F, Record[2]))
1196 Tok.setIdentifierInfo(II);
1197 Tok.setKind((tok::TokenKind)Record[3]);
1198 Tok.setFlag((Token::TokenFlags)Record[4]);
1199 Macro->AddTokenToBody(Tok);
1200 break;
1201 }
1202 }
1203 }
1204}
1205
1206PreprocessedEntityID
1207ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const {
1208 ContinuousRangeMap<uint32_t, int, 2>::const_iterator
1209 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1210 assert(I != M.PreprocessedEntityRemap.end()
1211 && "Invalid index into preprocessed entity index remap");
1212
1213 return LocalID + I->second;
1214}
1215
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001216unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1217 return llvm::hash_combine(ikey.Size, ikey.ModTime);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001218}
1219
1220HeaderFileInfoTrait::internal_key_type
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001221HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
1222 internal_key_type ikey = { FE->getSize(), FE->getModificationTime(),
1223 FE->getName() };
1224 return ikey;
1225}
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001226
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001227bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
1228 if (a.Size != b.Size || a.ModTime != b.ModTime)
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001229 return false;
1230
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001231 if (strcmp(a.Filename, b.Filename) == 0)
1232 return true;
1233
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001234 // Determine whether the actual files are equivalent.
Argyrios Kyrtzidis1c1508b2013-03-04 20:33:40 +00001235 FileManager &FileMgr = Reader.getFileManager();
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001236 const FileEntry *FEA = FileMgr.getFile(a.Filename);
1237 const FileEntry *FEB = FileMgr.getFile(b.Filename);
Argyrios Kyrtzidis1c1508b2013-03-04 20:33:40 +00001238 return (FEA && FEA == FEB);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001239}
1240
1241std::pair<unsigned, unsigned>
1242HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1243 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1244 unsigned DataLen = (unsigned) *d++;
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001245 return std::make_pair(KeyLen, DataLen);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001246}
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001247
1248HeaderFileInfoTrait::internal_key_type
1249HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
1250 internal_key_type ikey;
1251 ikey.Size = off_t(clang::io::ReadUnalignedLE64(d));
1252 ikey.ModTime = time_t(clang::io::ReadUnalignedLE64(d));
1253 ikey.Filename = (const char *)d;
1254 return ikey;
1255}
1256
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001257HeaderFileInfoTrait::data_type
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001258HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001259 unsigned DataLen) {
1260 const unsigned char *End = d + DataLen;
1261 using namespace clang::io;
1262 HeaderFileInfo HFI;
1263 unsigned Flags = *d++;
1264 HFI.isImport = (Flags >> 5) & 0x01;
1265 HFI.isPragmaOnce = (Flags >> 4) & 0x01;
1266 HFI.DirInfo = (Flags >> 2) & 0x03;
1267 HFI.Resolved = (Flags >> 1) & 0x01;
1268 HFI.IndexHeaderMapHeader = Flags & 0x01;
1269 HFI.NumIncludes = ReadUnalignedLE16(d);
1270 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(M,
1271 ReadUnalignedLE32(d));
1272 if (unsigned FrameworkOffset = ReadUnalignedLE32(d)) {
1273 // The framework offset is 1 greater than the actual offset,
1274 // since 0 is used as an indicator for "no framework name".
1275 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1276 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1277 }
1278
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001279 if (d != End) {
1280 uint32_t LocalSMID = ReadUnalignedLE32(d);
1281 if (LocalSMID) {
1282 // This header is part of a module. Associate it with the module to enable
1283 // implicit module import.
1284 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1285 Module *Mod = Reader.getSubmodule(GlobalSMID);
1286 HFI.isModuleHeader = true;
1287 FileManager &FileMgr = Reader.getFileManager();
1288 ModuleMap &ModMap =
1289 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
1290 ModMap.addHeader(Mod, FileMgr.getFile(key.Filename), /*Excluded=*/false);
1291 }
1292 }
1293
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001294 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1295 (void)End;
1296
1297 // This HeaderFileInfo was externally loaded.
1298 HFI.External = true;
1299 return HFI;
1300}
1301
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001302void ASTReader::addPendingMacroFromModule(IdentifierInfo *II,
1303 ModuleFile *M,
1304 GlobalMacroID GMacID,
1305 SourceLocation ImportLoc) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001306 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001307 PendingMacroIDs[II].push_back(PendingMacroInfo(M, GMacID, ImportLoc));
1308}
1309
1310void ASTReader::addPendingMacroFromPCH(IdentifierInfo *II,
1311 ModuleFile *M,
1312 uint64_t MacroDirectivesOffset) {
1313 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1314 PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001315}
1316
1317void ASTReader::ReadDefinedMacros() {
1318 // Note that we are loading defined macros.
1319 Deserializing Macros(this);
1320
1321 for (ModuleReverseIterator I = ModuleMgr.rbegin(),
1322 E = ModuleMgr.rend(); I != E; ++I) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001323 BitstreamCursor &MacroCursor = (*I)->MacroCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001324
1325 // If there was no preprocessor block, skip this file.
1326 if (!MacroCursor.getBitStreamReader())
1327 continue;
1328
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001329 BitstreamCursor Cursor = MacroCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001330 Cursor.JumpToBit((*I)->MacroStartOffset);
1331
1332 RecordData Record;
1333 while (true) {
Chris Lattner88bde502013-01-19 21:39:22 +00001334 llvm::BitstreamEntry E = Cursor.advanceSkippingSubblocks();
1335
1336 switch (E.Kind) {
1337 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1338 case llvm::BitstreamEntry::Error:
1339 Error("malformed block record in AST file");
1340 return;
1341 case llvm::BitstreamEntry::EndBlock:
1342 goto NextCursor;
1343
1344 case llvm::BitstreamEntry::Record:
Chris Lattner88bde502013-01-19 21:39:22 +00001345 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001346 switch (Cursor.readRecord(E.ID, Record)) {
Chris Lattner88bde502013-01-19 21:39:22 +00001347 default: // Default behavior: ignore.
1348 break;
1349
1350 case PP_MACRO_OBJECT_LIKE:
1351 case PP_MACRO_FUNCTION_LIKE:
1352 getLocalIdentifier(**I, Record[0]);
1353 break;
1354
1355 case PP_TOKEN:
1356 // Ignore tokens.
1357 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001358 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001359 break;
1360 }
1361 }
Chris Lattner88bde502013-01-19 21:39:22 +00001362 NextCursor: ;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001363 }
1364}
1365
1366namespace {
1367 /// \brief Visitor class used to look up identifirs in an AST file.
1368 class IdentifierLookupVisitor {
1369 StringRef Name;
1370 unsigned PriorGeneration;
Douglas Gregore1698072013-01-25 00:38:33 +00001371 unsigned &NumIdentifierLookups;
1372 unsigned &NumIdentifierLookupHits;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001373 IdentifierInfo *Found;
Douglas Gregore1698072013-01-25 00:38:33 +00001374
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001375 public:
Douglas Gregore1698072013-01-25 00:38:33 +00001376 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
1377 unsigned &NumIdentifierLookups,
1378 unsigned &NumIdentifierLookupHits)
Douglas Gregor188bdcd2013-01-25 23:32:03 +00001379 : Name(Name), PriorGeneration(PriorGeneration),
Douglas Gregore1698072013-01-25 00:38:33 +00001380 NumIdentifierLookups(NumIdentifierLookups),
1381 NumIdentifierLookupHits(NumIdentifierLookupHits),
1382 Found()
1383 {
1384 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001385
1386 static bool visit(ModuleFile &M, void *UserData) {
1387 IdentifierLookupVisitor *This
1388 = static_cast<IdentifierLookupVisitor *>(UserData);
1389
1390 // If we've already searched this module file, skip it now.
1391 if (M.Generation <= This->PriorGeneration)
1392 return true;
Douglas Gregor1a49d972013-01-25 01:03:03 +00001393
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001394 ASTIdentifierLookupTable *IdTable
1395 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
1396 if (!IdTable)
1397 return false;
1398
1399 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(),
1400 M, This->Found);
Douglas Gregore1698072013-01-25 00:38:33 +00001401 ++This->NumIdentifierLookups;
1402 ASTIdentifierLookupTable::iterator Pos = IdTable->find(This->Name,&Trait);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001403 if (Pos == IdTable->end())
1404 return false;
1405
1406 // Dereferencing the iterator has the effect of building the
1407 // IdentifierInfo node and populating it with the various
1408 // declarations it needs.
Douglas Gregore1698072013-01-25 00:38:33 +00001409 ++This->NumIdentifierLookupHits;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001410 This->Found = *Pos;
1411 return true;
1412 }
1413
1414 // \brief Retrieve the identifier info found within the module
1415 // files.
1416 IdentifierInfo *getIdentifierInfo() const { return Found; }
1417 };
1418}
1419
1420void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
1421 // Note that we are loading an identifier.
1422 Deserializing AnIdentifier(this);
1423
1424 unsigned PriorGeneration = 0;
1425 if (getContext().getLangOpts().Modules)
1426 PriorGeneration = IdentifierGeneration[&II];
Douglas Gregor1a49d972013-01-25 01:03:03 +00001427
1428 // If there is a global index, look there first to determine which modules
1429 // provably do not have any results for this identifier.
Douglas Gregor188bdcd2013-01-25 23:32:03 +00001430 GlobalModuleIndex::HitSet Hits;
1431 GlobalModuleIndex::HitSet *HitsPtr = 0;
Douglas Gregor1a49d972013-01-25 01:03:03 +00001432 if (!loadGlobalIndex()) {
Douglas Gregor188bdcd2013-01-25 23:32:03 +00001433 if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
1434 HitsPtr = &Hits;
Douglas Gregor1a49d972013-01-25 01:03:03 +00001435 }
1436 }
1437
Douglas Gregor188bdcd2013-01-25 23:32:03 +00001438 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
Douglas Gregore1698072013-01-25 00:38:33 +00001439 NumIdentifierLookups,
1440 NumIdentifierLookupHits);
Douglas Gregor188bdcd2013-01-25 23:32:03 +00001441 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor, HitsPtr);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001442 markIdentifierUpToDate(&II);
1443}
1444
1445void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
1446 if (!II)
1447 return;
1448
1449 II->setOutOfDate(false);
1450
1451 // Update the generation for this identifier.
1452 if (getContext().getLangOpts().Modules)
1453 IdentifierGeneration[II] = CurrentGeneration;
1454}
1455
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001456void ASTReader::resolvePendingMacro(IdentifierInfo *II,
1457 const PendingMacroInfo &PMInfo) {
1458 assert(II);
1459
1460 if (PMInfo.M->Kind != MK_Module) {
1461 installPCHMacroDirectives(II, *PMInfo.M,
1462 PMInfo.PCHMacroData.MacroDirectivesOffset);
1463 return;
1464 }
1465
1466 // Module Macro.
1467
1468 GlobalMacroID GMacID = PMInfo.ModuleMacroData.GMacID;
1469 SourceLocation ImportLoc =
1470 SourceLocation::getFromRawEncoding(PMInfo.ModuleMacroData.ImportLoc);
1471
1472 assert(GMacID);
1473 // If this macro has already been loaded, don't do so again.
1474 if (MacrosLoaded[GMacID - NUM_PREDEF_MACRO_IDS])
1475 return;
1476
1477 MacroInfo *MI = getMacro(GMacID);
1478 SubmoduleID SubModID = MI->getOwningModuleID();
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001479 MacroDirective *MD = PP.AllocateDefMacroDirective(MI, ImportLoc,
1480 /*isImported=*/true);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001481
1482 // Determine whether this macro definition is visible.
1483 bool Hidden = false;
1484 if (SubModID) {
1485 if (Module *Owner = getSubmodule(SubModID)) {
1486 if (Owner->NameVisibility == Module::Hidden) {
1487 // The owning module is not visible, and this macro definition
1488 // should not be, either.
1489 Hidden = true;
1490
1491 // Note that this macro definition was hidden because its owning
1492 // module is not yet visible.
1493 HiddenNamesMap[Owner].push_back(HiddenName(II, MD));
1494 }
1495 }
1496 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001497
1498 if (!Hidden)
1499 installImportedMacro(II, MD);
1500}
1501
1502void ASTReader::installPCHMacroDirectives(IdentifierInfo *II,
1503 ModuleFile &M, uint64_t Offset) {
1504 assert(M.Kind != MK_Module);
1505
1506 BitstreamCursor &Cursor = M.MacroCursor;
1507 SavedStreamPosition SavedPosition(Cursor);
1508 Cursor.JumpToBit(Offset);
1509
1510 llvm::BitstreamEntry Entry =
1511 Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
1512 if (Entry.Kind != llvm::BitstreamEntry::Record) {
1513 Error("malformed block record in AST file");
1514 return;
1515 }
1516
1517 RecordData Record;
1518 PreprocessorRecordTypes RecType =
1519 (PreprocessorRecordTypes)Cursor.readRecord(Entry.ID, Record);
1520 if (RecType != PP_MACRO_DIRECTIVE_HISTORY) {
1521 Error("malformed block record in AST file");
1522 return;
1523 }
1524
1525 // Deserialize the macro directives history in reverse source-order.
1526 MacroDirective *Latest = 0, *Earliest = 0;
1527 unsigned Idx = 0, N = Record.size();
1528 while (Idx < N) {
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001529 MacroDirective *MD = 0;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001530 SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001531 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
1532 switch (K) {
1533 case MacroDirective::MD_Define: {
1534 GlobalMacroID GMacID = getGlobalMacroID(M, Record[Idx++]);
1535 MacroInfo *MI = getMacro(GMacID);
1536 bool isImported = Record[Idx++];
1537 bool isAmbiguous = Record[Idx++];
1538 DefMacroDirective *DefMD =
1539 PP.AllocateDefMacroDirective(MI, Loc, isImported);
1540 DefMD->setAmbiguous(isAmbiguous);
1541 MD = DefMD;
1542 break;
1543 }
1544 case MacroDirective::MD_Undefine:
1545 MD = PP.AllocateUndefMacroDirective(Loc);
1546 break;
1547 case MacroDirective::MD_Visibility: {
1548 bool isPublic = Record[Idx++];
1549 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
1550 break;
1551 }
1552 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001553
1554 if (!Latest)
1555 Latest = MD;
1556 if (Earliest)
1557 Earliest->setPrevious(MD);
1558 Earliest = MD;
1559 }
1560
1561 PP.setLoadedMacroDirective(II, Latest);
1562}
1563
1564void ASTReader::installImportedMacro(IdentifierInfo *II, MacroDirective *MD) {
1565 assert(II && MD);
1566
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001567 DefMacroDirective *DefMD = cast<DefMacroDirective>(MD);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001568 MacroDirective *Prev = PP.getMacroDirective(II);
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001569 if (Prev) {
1570 MacroDirective::DefInfo PrevDef = Prev->getDefinition();
1571 if (DefMD->getInfo() != PrevDef.getMacroInfo() &&
1572 !PrevDef.getMacroInfo()->isIdenticalTo(*DefMD->getInfo(), PP)) {
1573 PrevDef.getDirective()->setAmbiguous(true);
1574 DefMD->setAmbiguous(true);
1575 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001576 }
1577
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001578 PP.appendMacroDirective(II, MD);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001579}
1580
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00001581InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001582 // If this ID is bogus, just return an empty input file.
1583 if (ID == 0 || ID > F.InputFilesLoaded.size())
1584 return InputFile();
1585
1586 // If we've already loaded this input file, return it.
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00001587 if (F.InputFilesLoaded[ID-1].getFile())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001588 return F.InputFilesLoaded[ID-1];
1589
1590 // Go find this input file.
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001591 BitstreamCursor &Cursor = F.InputFilesCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001592 SavedStreamPosition SavedPosition(Cursor);
1593 Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
1594
1595 unsigned Code = Cursor.ReadCode();
1596 RecordData Record;
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001597 StringRef Blob;
1598 switch ((InputFileRecordTypes)Cursor.readRecord(Code, Record, &Blob)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001599 case INPUT_FILE: {
1600 unsigned StoredID = Record[0];
1601 assert(ID == StoredID && "Bogus stored ID or offset");
1602 (void)StoredID;
1603 off_t StoredSize = (off_t)Record[1];
1604 time_t StoredTime = (time_t)Record[2];
1605 bool Overridden = (bool)Record[3];
1606
1607 // Get the file entry for this input file.
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001608 StringRef OrigFilename = Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001609 std::string Filename = OrigFilename;
1610 MaybeAddSystemRootToFilename(F, Filename);
1611 const FileEntry *File
1612 = Overridden? FileMgr.getVirtualFile(Filename, StoredSize, StoredTime)
1613 : FileMgr.getFile(Filename, /*OpenFile=*/false);
1614
1615 // If we didn't find the file, resolve it relative to the
1616 // original directory from which this AST file was created.
1617 if (File == 0 && !F.OriginalDir.empty() && !CurrentDir.empty() &&
1618 F.OriginalDir != CurrentDir) {
1619 std::string Resolved = resolveFileRelativeToOriginalDir(Filename,
1620 F.OriginalDir,
1621 CurrentDir);
1622 if (!Resolved.empty())
1623 File = FileMgr.getFile(Resolved);
1624 }
1625
1626 // For an overridden file, create a virtual file with the stored
1627 // size/timestamp.
1628 if (Overridden && File == 0) {
1629 File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
1630 }
1631
1632 if (File == 0) {
1633 if (Complain) {
1634 std::string ErrorStr = "could not find file '";
1635 ErrorStr += Filename;
1636 ErrorStr += "' referenced by AST file";
1637 Error(ErrorStr.c_str());
1638 }
1639 return InputFile();
1640 }
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00001641
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001642 // Check if there was a request to override the contents of the file
1643 // that was part of the precompiled header. Overridding such a file
1644 // can lead to problems when lexing using the source locations from the
1645 // PCH.
1646 SourceManager &SM = getSourceManager();
1647 if (!Overridden && SM.isFileOverridden(File)) {
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00001648 if (Complain)
1649 Error(diag::err_fe_pch_file_overridden, Filename);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001650 // After emitting the diagnostic, recover by disabling the override so
1651 // that the original file will be used.
1652 SM.disableFileContentsOverride(File);
1653 // The FileEntry is a virtual file entry with the size of the contents
1654 // that would override the original contents. Set it to the original's
1655 // size/time.
1656 FileMgr.modifyFileEntry(const_cast<FileEntry*>(File),
1657 StoredSize, StoredTime);
1658 }
1659
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00001660 bool IsOutOfDate = false;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001661
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00001662 // For an overridden file, there is nothing to validate.
1663 if (!Overridden && (StoredSize != File->getSize()
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001664#if !defined(LLVM_ON_WIN32)
1665 // In our regression testing, the Windows file system seems to
1666 // have inconsistent modification times that sometimes
1667 // erroneously trigger this error-handling path.
1668 || StoredTime != File->getModificationTime()
1669#endif
1670 )) {
Douglas Gregor677e15f2013-03-19 00:28:20 +00001671 if (Complain) {
Argyrios Kyrtzidisf8f373f2013-03-08 20:42:38 +00001672 Error(diag::err_fe_pch_file_modified, Filename, F.FileName);
Douglas Gregor677e15f2013-03-19 00:28:20 +00001673 }
1674
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00001675 IsOutOfDate = true;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001676 }
1677
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00001678 InputFile IF = InputFile(File, Overridden, IsOutOfDate);
1679
1680 // Note that we've loaded this input file.
1681 F.InputFilesLoaded[ID-1] = IF;
1682 return IF;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001683 }
1684 }
1685
1686 return InputFile();
1687}
1688
1689const FileEntry *ASTReader::getFileEntry(StringRef filenameStrRef) {
1690 ModuleFile &M = ModuleMgr.getPrimaryModule();
1691 std::string Filename = filenameStrRef;
1692 MaybeAddSystemRootToFilename(M, Filename);
1693 const FileEntry *File = FileMgr.getFile(Filename);
1694 if (File == 0 && !M.OriginalDir.empty() && !CurrentDir.empty() &&
1695 M.OriginalDir != CurrentDir) {
1696 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1697 M.OriginalDir,
1698 CurrentDir);
1699 if (!resolved.empty())
1700 File = FileMgr.getFile(resolved);
1701 }
1702
1703 return File;
1704}
1705
1706/// \brief If we are loading a relocatable PCH file, and the filename is
1707/// not an absolute path, add the system root to the beginning of the file
1708/// name.
1709void ASTReader::MaybeAddSystemRootToFilename(ModuleFile &M,
1710 std::string &Filename) {
1711 // If this is not a relocatable PCH file, there's nothing to do.
1712 if (!M.RelocatablePCH)
1713 return;
1714
1715 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
1716 return;
1717
1718 if (isysroot.empty()) {
1719 // If no system root was given, default to '/'
1720 Filename.insert(Filename.begin(), '/');
1721 return;
1722 }
1723
1724 unsigned Length = isysroot.size();
1725 if (isysroot[Length - 1] != '/')
1726 Filename.insert(Filename.begin(), '/');
1727
1728 Filename.insert(Filename.begin(), isysroot.begin(), isysroot.end());
1729}
1730
1731ASTReader::ASTReadResult
1732ASTReader::ReadControlBlock(ModuleFile &F,
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001733 SmallVectorImpl<ImportedModule> &Loaded,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001734 unsigned ClientLoadCapabilities) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001735 BitstreamCursor &Stream = F.Stream;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001736
1737 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
1738 Error("malformed block record in AST file");
1739 return Failure;
1740 }
1741
1742 // Read all of the records and blocks in the control block.
1743 RecordData Record;
Chris Lattner88bde502013-01-19 21:39:22 +00001744 while (1) {
1745 llvm::BitstreamEntry Entry = Stream.advance();
1746
1747 switch (Entry.Kind) {
1748 case llvm::BitstreamEntry::Error:
1749 Error("malformed block record in AST file");
1750 return Failure;
1751 case llvm::BitstreamEntry::EndBlock:
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001752 // Validate all of the non-system input files.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001753 if (!DisableValidation) {
1754 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001755 // All user input files reside at the index range [0, Record[1]).
1756 // Record is the one from INPUT_FILE_OFFSETS.
1757 for (unsigned I = 0, N = Record[1]; I < N; ++I) {
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00001758 InputFile IF = getInputFile(F, I+1, Complain);
1759 if (!IF.getFile() || IF.isOutOfDate())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001760 return OutOfDate;
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00001761 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001762 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001763 return Success;
Chris Lattner88bde502013-01-19 21:39:22 +00001764
1765 case llvm::BitstreamEntry::SubBlock:
1766 switch (Entry.ID) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001767 case INPUT_FILES_BLOCK_ID:
1768 F.InputFilesCursor = Stream;
1769 if (Stream.SkipBlock() || // Skip with the main cursor
1770 // Read the abbreviations
1771 ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
1772 Error("malformed block record in AST file");
1773 return Failure;
1774 }
1775 continue;
Chris Lattner88bde502013-01-19 21:39:22 +00001776
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001777 default:
Chris Lattner88bde502013-01-19 21:39:22 +00001778 if (Stream.SkipBlock()) {
1779 Error("malformed block record in AST file");
1780 return Failure;
1781 }
1782 continue;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001783 }
Chris Lattner88bde502013-01-19 21:39:22 +00001784
1785 case llvm::BitstreamEntry::Record:
1786 // The interesting case.
1787 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001788 }
1789
1790 // Read and process a record.
1791 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001792 StringRef Blob;
1793 switch ((ControlRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001794 case METADATA: {
1795 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1796 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
1797 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
1798 : diag::warn_pch_version_too_new);
1799 return VersionMismatch;
1800 }
1801
1802 bool hasErrors = Record[5];
1803 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
1804 Diag(diag::err_pch_with_compiler_errors);
1805 return HadErrors;
1806 }
1807
1808 F.RelocatablePCH = Record[4];
1809
1810 const std::string &CurBranch = getClangFullRepositoryVersion();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001811 StringRef ASTBranch = Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001812 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
1813 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
1814 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
1815 return VersionMismatch;
1816 }
1817 break;
1818 }
1819
1820 case IMPORTS: {
1821 // Load each of the imported PCH files.
1822 unsigned Idx = 0, N = Record.size();
1823 while (Idx < N) {
1824 // Read information about the AST file.
1825 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
1826 // The import location will be the local one for now; we will adjust
1827 // all import locations of module imports after the global source
1828 // location info are setup.
1829 SourceLocation ImportLoc =
1830 SourceLocation::getFromRawEncoding(Record[Idx++]);
Douglas Gregor677e15f2013-03-19 00:28:20 +00001831 off_t StoredSize = (off_t)Record[Idx++];
1832 time_t StoredModTime = (time_t)Record[Idx++];
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001833 unsigned Length = Record[Idx++];
1834 SmallString<128> ImportedFile(Record.begin() + Idx,
1835 Record.begin() + Idx + Length);
1836 Idx += Length;
1837
1838 // Load the AST file.
1839 switch(ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F, Loaded,
Douglas Gregor677e15f2013-03-19 00:28:20 +00001840 StoredSize, StoredModTime,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001841 ClientLoadCapabilities)) {
1842 case Failure: return Failure;
1843 // If we have to ignore the dependency, we'll have to ignore this too.
Douglas Gregorac39f132013-03-19 00:38:50 +00001844 case Missing:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001845 case OutOfDate: return OutOfDate;
1846 case VersionMismatch: return VersionMismatch;
1847 case ConfigurationMismatch: return ConfigurationMismatch;
1848 case HadErrors: return HadErrors;
1849 case Success: break;
1850 }
1851 }
1852 break;
1853 }
1854
1855 case LANGUAGE_OPTIONS: {
1856 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
1857 if (Listener && &F == *ModuleMgr.begin() &&
1858 ParseLanguageOptions(Record, Complain, *Listener) &&
1859 !DisableValidation)
1860 return ConfigurationMismatch;
1861 break;
1862 }
1863
1864 case TARGET_OPTIONS: {
1865 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1866 if (Listener && &F == *ModuleMgr.begin() &&
1867 ParseTargetOptions(Record, Complain, *Listener) &&
1868 !DisableValidation)
1869 return ConfigurationMismatch;
1870 break;
1871 }
1872
1873 case DIAGNOSTIC_OPTIONS: {
1874 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1875 if (Listener && &F == *ModuleMgr.begin() &&
1876 ParseDiagnosticOptions(Record, Complain, *Listener) &&
1877 !DisableValidation)
1878 return ConfigurationMismatch;
1879 break;
1880 }
1881
1882 case FILE_SYSTEM_OPTIONS: {
1883 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1884 if (Listener && &F == *ModuleMgr.begin() &&
1885 ParseFileSystemOptions(Record, Complain, *Listener) &&
1886 !DisableValidation)
1887 return ConfigurationMismatch;
1888 break;
1889 }
1890
1891 case HEADER_SEARCH_OPTIONS: {
1892 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1893 if (Listener && &F == *ModuleMgr.begin() &&
1894 ParseHeaderSearchOptions(Record, Complain, *Listener) &&
1895 !DisableValidation)
1896 return ConfigurationMismatch;
1897 break;
1898 }
1899
1900 case PREPROCESSOR_OPTIONS: {
1901 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1902 if (Listener && &F == *ModuleMgr.begin() &&
1903 ParsePreprocessorOptions(Record, Complain, *Listener,
1904 SuggestedPredefines) &&
1905 !DisableValidation)
1906 return ConfigurationMismatch;
1907 break;
1908 }
1909
1910 case ORIGINAL_FILE:
1911 F.OriginalSourceFileID = FileID::get(Record[0]);
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001912 F.ActualOriginalSourceFileName = Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001913 F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
1914 MaybeAddSystemRootToFilename(F, F.OriginalSourceFileName);
1915 break;
1916
1917 case ORIGINAL_FILE_ID:
1918 F.OriginalSourceFileID = FileID::get(Record[0]);
1919 break;
1920
1921 case ORIGINAL_PCH_DIR:
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001922 F.OriginalDir = Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001923 break;
1924
1925 case INPUT_FILE_OFFSETS:
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001926 F.InputFileOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001927 F.InputFilesLoaded.resize(Record[0]);
1928 break;
1929 }
1930 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001931}
1932
1933bool ASTReader::ReadASTBlock(ModuleFile &F) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001934 BitstreamCursor &Stream = F.Stream;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001935
1936 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
1937 Error("malformed block record in AST file");
1938 return true;
1939 }
1940
1941 // Read all of the records and blocks for the AST file.
1942 RecordData Record;
Chris Lattner88bde502013-01-19 21:39:22 +00001943 while (1) {
1944 llvm::BitstreamEntry Entry = Stream.advance();
1945
1946 switch (Entry.Kind) {
1947 case llvm::BitstreamEntry::Error:
1948 Error("error at end of module block in AST file");
1949 return true;
1950 case llvm::BitstreamEntry::EndBlock: {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001951 DeclContext *DC = Context.getTranslationUnitDecl();
1952 if (!DC->hasExternalVisibleStorage() && DC->hasExternalLexicalStorage())
1953 DC->setMustBuildLookupTable();
Chris Lattner88bde502013-01-19 21:39:22 +00001954
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001955 return false;
1956 }
Chris Lattner88bde502013-01-19 21:39:22 +00001957 case llvm::BitstreamEntry::SubBlock:
1958 switch (Entry.ID) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001959 case DECLTYPES_BLOCK_ID:
1960 // We lazily load the decls block, but we want to set up the
1961 // DeclsCursor cursor to point into it. Clone our current bitcode
1962 // cursor to it, enter the block and read the abbrevs in that block.
1963 // With the main cursor, we just skip over it.
1964 F.DeclsCursor = Stream;
1965 if (Stream.SkipBlock() || // Skip with the main cursor.
1966 // Read the abbrevs.
1967 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
1968 Error("malformed block record in AST file");
1969 return true;
1970 }
1971 break;
Chris Lattner88bde502013-01-19 21:39:22 +00001972
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001973 case DECL_UPDATES_BLOCK_ID:
1974 if (Stream.SkipBlock()) {
1975 Error("malformed block record in AST file");
1976 return true;
1977 }
1978 break;
Chris Lattner88bde502013-01-19 21:39:22 +00001979
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001980 case PREPROCESSOR_BLOCK_ID:
1981 F.MacroCursor = Stream;
1982 if (!PP.getExternalSource())
1983 PP.setExternalSource(this);
Chris Lattner88bde502013-01-19 21:39:22 +00001984
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001985 if (Stream.SkipBlock() ||
1986 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
1987 Error("malformed block record in AST file");
1988 return true;
1989 }
1990 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
1991 break;
Chris Lattner88bde502013-01-19 21:39:22 +00001992
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001993 case PREPROCESSOR_DETAIL_BLOCK_ID:
1994 F.PreprocessorDetailCursor = Stream;
1995 if (Stream.SkipBlock() ||
Chris Lattner88bde502013-01-19 21:39:22 +00001996 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001997 PREPROCESSOR_DETAIL_BLOCK_ID)) {
Chris Lattner88bde502013-01-19 21:39:22 +00001998 Error("malformed preprocessor detail record in AST file");
1999 return true;
2000 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002001 F.PreprocessorDetailStartOffset
Chris Lattner88bde502013-01-19 21:39:22 +00002002 = F.PreprocessorDetailCursor.GetCurrentBitNo();
2003
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002004 if (!PP.getPreprocessingRecord())
2005 PP.createPreprocessingRecord();
2006 if (!PP.getPreprocessingRecord()->getExternalSource())
2007 PP.getPreprocessingRecord()->SetExternalSource(*this);
2008 break;
2009
2010 case SOURCE_MANAGER_BLOCK_ID:
2011 if (ReadSourceManagerBlock(F))
2012 return true;
2013 break;
Chris Lattner88bde502013-01-19 21:39:22 +00002014
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002015 case SUBMODULE_BLOCK_ID:
2016 if (ReadSubmoduleBlock(F))
2017 return true;
2018 break;
Chris Lattner88bde502013-01-19 21:39:22 +00002019
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002020 case COMMENTS_BLOCK_ID: {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00002021 BitstreamCursor C = Stream;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002022 if (Stream.SkipBlock() ||
2023 ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
2024 Error("malformed comments block in AST file");
2025 return true;
2026 }
2027 CommentsCursors.push_back(std::make_pair(C, &F));
2028 break;
2029 }
Chris Lattner88bde502013-01-19 21:39:22 +00002030
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002031 default:
Chris Lattner88bde502013-01-19 21:39:22 +00002032 if (Stream.SkipBlock()) {
2033 Error("malformed block record in AST file");
2034 return true;
2035 }
2036 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002037 }
2038 continue;
Chris Lattner88bde502013-01-19 21:39:22 +00002039
2040 case llvm::BitstreamEntry::Record:
2041 // The interesting case.
2042 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002043 }
2044
2045 // Read and process a record.
2046 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002047 StringRef Blob;
2048 switch ((ASTRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002049 default: // Default behavior: ignore.
2050 break;
2051
2052 case TYPE_OFFSET: {
2053 if (F.LocalNumTypes != 0) {
2054 Error("duplicate TYPE_OFFSET record in AST file");
2055 return true;
2056 }
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002057 F.TypeOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002058 F.LocalNumTypes = Record[0];
2059 unsigned LocalBaseTypeIndex = Record[1];
2060 F.BaseTypeIndex = getTotalNumTypes();
2061
2062 if (F.LocalNumTypes > 0) {
2063 // Introduce the global -> local mapping for types within this module.
2064 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
2065
2066 // Introduce the local -> global mapping for types within this module.
2067 F.TypeRemap.insertOrReplace(
2068 std::make_pair(LocalBaseTypeIndex,
2069 F.BaseTypeIndex - LocalBaseTypeIndex));
2070
2071 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
2072 }
2073 break;
2074 }
2075
2076 case DECL_OFFSET: {
2077 if (F.LocalNumDecls != 0) {
2078 Error("duplicate DECL_OFFSET record in AST file");
2079 return true;
2080 }
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002081 F.DeclOffsets = (const DeclOffset *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002082 F.LocalNumDecls = Record[0];
2083 unsigned LocalBaseDeclID = Record[1];
2084 F.BaseDeclID = getTotalNumDecls();
2085
2086 if (F.LocalNumDecls > 0) {
2087 // Introduce the global -> local mapping for declarations within this
2088 // module.
2089 GlobalDeclMap.insert(
2090 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
2091
2092 // Introduce the local -> global mapping for declarations within this
2093 // module.
2094 F.DeclRemap.insertOrReplace(
2095 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
2096
2097 // Introduce the global -> local mapping for declarations within this
2098 // module.
2099 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
2100
2101 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
2102 }
2103 break;
2104 }
2105
2106 case TU_UPDATE_LEXICAL: {
2107 DeclContext *TU = Context.getTranslationUnitDecl();
2108 DeclContextInfo &Info = F.DeclContextInfos[TU];
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002109 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(Blob.data());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002110 Info.NumLexicalDecls
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002111 = static_cast<unsigned int>(Blob.size() / sizeof(KindDeclIDPair));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002112 TU->setHasExternalLexicalStorage(true);
2113 break;
2114 }
2115
2116 case UPDATE_VISIBLE: {
2117 unsigned Idx = 0;
2118 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
2119 ASTDeclContextNameLookupTable *Table =
2120 ASTDeclContextNameLookupTable::Create(
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002121 (const unsigned char *)Blob.data() + Record[Idx++],
2122 (const unsigned char *)Blob.data(),
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002123 ASTDeclContextNameLookupTrait(*this, F));
2124 if (ID == PREDEF_DECL_TRANSLATION_UNIT_ID) { // Is it the TU?
2125 DeclContext *TU = Context.getTranslationUnitDecl();
2126 F.DeclContextInfos[TU].NameLookupTableData = Table;
2127 TU->setHasExternalVisibleStorage(true);
2128 } else
2129 PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F));
2130 break;
2131 }
2132
2133 case IDENTIFIER_TABLE:
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002134 F.IdentifierTableData = Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002135 if (Record[0]) {
2136 F.IdentifierLookupTable
2137 = ASTIdentifierLookupTable::Create(
2138 (const unsigned char *)F.IdentifierTableData + Record[0],
2139 (const unsigned char *)F.IdentifierTableData,
2140 ASTIdentifierLookupTrait(*this, F));
2141
2142 PP.getIdentifierTable().setExternalIdentifierLookup(this);
2143 }
2144 break;
2145
2146 case IDENTIFIER_OFFSET: {
2147 if (F.LocalNumIdentifiers != 0) {
2148 Error("duplicate IDENTIFIER_OFFSET record in AST file");
2149 return true;
2150 }
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002151 F.IdentifierOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002152 F.LocalNumIdentifiers = Record[0];
2153 unsigned LocalBaseIdentifierID = Record[1];
2154 F.BaseIdentifierID = getTotalNumIdentifiers();
2155
2156 if (F.LocalNumIdentifiers > 0) {
2157 // Introduce the global -> local mapping for identifiers within this
2158 // module.
2159 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
2160 &F));
2161
2162 // Introduce the local -> global mapping for identifiers within this
2163 // module.
2164 F.IdentifierRemap.insertOrReplace(
2165 std::make_pair(LocalBaseIdentifierID,
2166 F.BaseIdentifierID - LocalBaseIdentifierID));
2167
2168 IdentifiersLoaded.resize(IdentifiersLoaded.size()
2169 + F.LocalNumIdentifiers);
2170 }
2171 break;
2172 }
2173
2174 case EXTERNAL_DEFINITIONS:
2175 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2176 ExternalDefinitions.push_back(getGlobalDeclID(F, Record[I]));
2177 break;
2178
2179 case SPECIAL_TYPES:
Douglas Gregorf5cfc892013-02-01 23:45:03 +00002180 if (SpecialTypes.empty()) {
2181 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2182 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
2183 break;
2184 }
2185
2186 if (SpecialTypes.size() != Record.size()) {
2187 Error("invalid special-types record");
2188 return true;
2189 }
2190
2191 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2192 serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
2193 if (!SpecialTypes[I])
2194 SpecialTypes[I] = ID;
2195 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
2196 // merge step?
2197 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002198 break;
2199
2200 case STATISTICS:
2201 TotalNumStatements += Record[0];
2202 TotalNumMacros += Record[1];
2203 TotalLexicalDeclContexts += Record[2];
2204 TotalVisibleDeclContexts += Record[3];
2205 break;
2206
2207 case UNUSED_FILESCOPED_DECLS:
2208 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2209 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
2210 break;
2211
2212 case DELEGATING_CTORS:
2213 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2214 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
2215 break;
2216
2217 case WEAK_UNDECLARED_IDENTIFIERS:
2218 if (Record.size() % 4 != 0) {
2219 Error("invalid weak identifiers record");
2220 return true;
2221 }
2222
2223 // FIXME: Ignore weak undeclared identifiers from non-original PCH
2224 // files. This isn't the way to do it :)
2225 WeakUndeclaredIdentifiers.clear();
2226
2227 // Translate the weak, undeclared identifiers into global IDs.
2228 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
2229 WeakUndeclaredIdentifiers.push_back(
2230 getGlobalIdentifierID(F, Record[I++]));
2231 WeakUndeclaredIdentifiers.push_back(
2232 getGlobalIdentifierID(F, Record[I++]));
2233 WeakUndeclaredIdentifiers.push_back(
2234 ReadSourceLocation(F, Record, I).getRawEncoding());
2235 WeakUndeclaredIdentifiers.push_back(Record[I++]);
2236 }
2237 break;
2238
Richard Smith5ea6ef42013-01-10 23:43:47 +00002239 case LOCALLY_SCOPED_EXTERN_C_DECLS:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002240 for (unsigned I = 0, N = Record.size(); I != N; ++I)
Richard Smith5ea6ef42013-01-10 23:43:47 +00002241 LocallyScopedExternCDecls.push_back(getGlobalDeclID(F, Record[I]));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002242 break;
2243
2244 case SELECTOR_OFFSETS: {
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002245 F.SelectorOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002246 F.LocalNumSelectors = Record[0];
2247 unsigned LocalBaseSelectorID = Record[1];
2248 F.BaseSelectorID = getTotalNumSelectors();
2249
2250 if (F.LocalNumSelectors > 0) {
2251 // Introduce the global -> local mapping for selectors within this
2252 // module.
2253 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
2254
2255 // Introduce the local -> global mapping for selectors within this
2256 // module.
2257 F.SelectorRemap.insertOrReplace(
2258 std::make_pair(LocalBaseSelectorID,
2259 F.BaseSelectorID - LocalBaseSelectorID));
2260
2261 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
2262 }
2263 break;
2264 }
2265
2266 case METHOD_POOL:
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002267 F.SelectorLookupTableData = (const unsigned char *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002268 if (Record[0])
2269 F.SelectorLookupTable
2270 = ASTSelectorLookupTable::Create(
2271 F.SelectorLookupTableData + Record[0],
2272 F.SelectorLookupTableData,
2273 ASTSelectorLookupTrait(*this, F));
2274 TotalNumMethodPoolEntries += Record[1];
2275 break;
2276
2277 case REFERENCED_SELECTOR_POOL:
2278 if (!Record.empty()) {
2279 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
2280 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
2281 Record[Idx++]));
2282 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
2283 getRawEncoding());
2284 }
2285 }
2286 break;
2287
2288 case PP_COUNTER_VALUE:
2289 if (!Record.empty() && Listener)
2290 Listener->ReadCounter(F, Record[0]);
2291 break;
2292
2293 case FILE_SORTED_DECLS:
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002294 F.FileSortedDecls = (const DeclID *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002295 F.NumFileSortedDecls = Record[0];
2296 break;
2297
2298 case SOURCE_LOCATION_OFFSETS: {
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002299 F.SLocEntryOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002300 F.LocalNumSLocEntries = Record[0];
2301 unsigned SLocSpaceSize = Record[1];
2302 llvm::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
2303 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
2304 SLocSpaceSize);
2305 // Make our entry in the range map. BaseID is negative and growing, so
2306 // we invert it. Because we invert it, though, we need the other end of
2307 // the range.
2308 unsigned RangeStart =
2309 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
2310 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
2311 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
2312
2313 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
2314 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
2315 GlobalSLocOffsetMap.insert(
2316 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
2317 - SLocSpaceSize,&F));
2318
2319 // Initialize the remapping table.
2320 // Invalid stays invalid.
2321 F.SLocRemap.insert(std::make_pair(0U, 0));
2322 // This module. Base was 2 when being compiled.
2323 F.SLocRemap.insert(std::make_pair(2U,
2324 static_cast<int>(F.SLocEntryBaseOffset - 2)));
2325
2326 TotalNumSLocEntries += F.LocalNumSLocEntries;
2327 break;
2328 }
2329
2330 case MODULE_OFFSET_MAP: {
2331 // Additional remapping information.
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002332 const unsigned char *Data = (const unsigned char*)Blob.data();
2333 const unsigned char *DataEnd = Data + Blob.size();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002334
2335 // Continuous range maps we may be updating in our module.
2336 ContinuousRangeMap<uint32_t, int, 2>::Builder SLocRemap(F.SLocRemap);
2337 ContinuousRangeMap<uint32_t, int, 2>::Builder
2338 IdentifierRemap(F.IdentifierRemap);
2339 ContinuousRangeMap<uint32_t, int, 2>::Builder
2340 MacroRemap(F.MacroRemap);
2341 ContinuousRangeMap<uint32_t, int, 2>::Builder
2342 PreprocessedEntityRemap(F.PreprocessedEntityRemap);
2343 ContinuousRangeMap<uint32_t, int, 2>::Builder
2344 SubmoduleRemap(F.SubmoduleRemap);
2345 ContinuousRangeMap<uint32_t, int, 2>::Builder
2346 SelectorRemap(F.SelectorRemap);
2347 ContinuousRangeMap<uint32_t, int, 2>::Builder DeclRemap(F.DeclRemap);
2348 ContinuousRangeMap<uint32_t, int, 2>::Builder TypeRemap(F.TypeRemap);
2349
2350 while(Data < DataEnd) {
2351 uint16_t Len = io::ReadUnalignedLE16(Data);
2352 StringRef Name = StringRef((const char*)Data, Len);
2353 Data += Len;
2354 ModuleFile *OM = ModuleMgr.lookup(Name);
2355 if (!OM) {
2356 Error("SourceLocation remap refers to unknown module");
2357 return true;
2358 }
2359
2360 uint32_t SLocOffset = io::ReadUnalignedLE32(Data);
2361 uint32_t IdentifierIDOffset = io::ReadUnalignedLE32(Data);
2362 uint32_t MacroIDOffset = io::ReadUnalignedLE32(Data);
2363 uint32_t PreprocessedEntityIDOffset = io::ReadUnalignedLE32(Data);
2364 uint32_t SubmoduleIDOffset = io::ReadUnalignedLE32(Data);
2365 uint32_t SelectorIDOffset = io::ReadUnalignedLE32(Data);
2366 uint32_t DeclIDOffset = io::ReadUnalignedLE32(Data);
2367 uint32_t TypeIndexOffset = io::ReadUnalignedLE32(Data);
2368
2369 // Source location offset is mapped to OM->SLocEntryBaseOffset.
2370 SLocRemap.insert(std::make_pair(SLocOffset,
2371 static_cast<int>(OM->SLocEntryBaseOffset - SLocOffset)));
2372 IdentifierRemap.insert(
2373 std::make_pair(IdentifierIDOffset,
2374 OM->BaseIdentifierID - IdentifierIDOffset));
2375 MacroRemap.insert(std::make_pair(MacroIDOffset,
2376 OM->BaseMacroID - MacroIDOffset));
2377 PreprocessedEntityRemap.insert(
2378 std::make_pair(PreprocessedEntityIDOffset,
2379 OM->BasePreprocessedEntityID - PreprocessedEntityIDOffset));
2380 SubmoduleRemap.insert(std::make_pair(SubmoduleIDOffset,
2381 OM->BaseSubmoduleID - SubmoduleIDOffset));
2382 SelectorRemap.insert(std::make_pair(SelectorIDOffset,
2383 OM->BaseSelectorID - SelectorIDOffset));
2384 DeclRemap.insert(std::make_pair(DeclIDOffset,
2385 OM->BaseDeclID - DeclIDOffset));
2386
2387 TypeRemap.insert(std::make_pair(TypeIndexOffset,
2388 OM->BaseTypeIndex - TypeIndexOffset));
2389
2390 // Global -> local mappings.
2391 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
2392 }
2393 break;
2394 }
2395
2396 case SOURCE_MANAGER_LINE_TABLE:
2397 if (ParseLineTable(F, Record))
2398 return true;
2399 break;
2400
2401 case SOURCE_LOCATION_PRELOADS: {
2402 // Need to transform from the local view (1-based IDs) to the global view,
2403 // which is based off F.SLocEntryBaseID.
2404 if (!F.PreloadSLocEntries.empty()) {
2405 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
2406 return true;
2407 }
2408
2409 F.PreloadSLocEntries.swap(Record);
2410 break;
2411 }
2412
2413 case EXT_VECTOR_DECLS:
2414 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2415 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
2416 break;
2417
2418 case VTABLE_USES:
2419 if (Record.size() % 3 != 0) {
2420 Error("Invalid VTABLE_USES record");
2421 return true;
2422 }
2423
2424 // Later tables overwrite earlier ones.
2425 // FIXME: Modules will have some trouble with this. This is clearly not
2426 // the right way to do this.
2427 VTableUses.clear();
2428
2429 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
2430 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
2431 VTableUses.push_back(
2432 ReadSourceLocation(F, Record, Idx).getRawEncoding());
2433 VTableUses.push_back(Record[Idx++]);
2434 }
2435 break;
2436
2437 case DYNAMIC_CLASSES:
2438 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2439 DynamicClasses.push_back(getGlobalDeclID(F, Record[I]));
2440 break;
2441
2442 case PENDING_IMPLICIT_INSTANTIATIONS:
2443 if (PendingInstantiations.size() % 2 != 0) {
2444 Error("Invalid existing PendingInstantiations");
2445 return true;
2446 }
2447
2448 if (Record.size() % 2 != 0) {
2449 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
2450 return true;
2451 }
2452
2453 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2454 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
2455 PendingInstantiations.push_back(
2456 ReadSourceLocation(F, Record, I).getRawEncoding());
2457 }
2458 break;
2459
2460 case SEMA_DECL_REFS:
2461 // Later tables overwrite earlier ones.
2462 // FIXME: Modules will have some trouble with this.
2463 SemaDeclRefs.clear();
2464 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2465 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
2466 break;
2467
2468 case PPD_ENTITIES_OFFSETS: {
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002469 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
2470 assert(Blob.size() % sizeof(PPEntityOffset) == 0);
2471 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002472
2473 unsigned LocalBasePreprocessedEntityID = Record[0];
2474
2475 unsigned StartingID;
2476 if (!PP.getPreprocessingRecord())
2477 PP.createPreprocessingRecord();
2478 if (!PP.getPreprocessingRecord()->getExternalSource())
2479 PP.getPreprocessingRecord()->SetExternalSource(*this);
2480 StartingID
2481 = PP.getPreprocessingRecord()
2482 ->allocateLoadedEntities(F.NumPreprocessedEntities);
2483 F.BasePreprocessedEntityID = StartingID;
2484
2485 if (F.NumPreprocessedEntities > 0) {
2486 // Introduce the global -> local mapping for preprocessed entities in
2487 // this module.
2488 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
2489
2490 // Introduce the local -> global mapping for preprocessed entities in
2491 // this module.
2492 F.PreprocessedEntityRemap.insertOrReplace(
2493 std::make_pair(LocalBasePreprocessedEntityID,
2494 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
2495 }
2496
2497 break;
2498 }
2499
2500 case DECL_UPDATE_OFFSETS: {
2501 if (Record.size() % 2 != 0) {
2502 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2503 return true;
2504 }
2505 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
2506 DeclUpdateOffsets[getGlobalDeclID(F, Record[I])]
2507 .push_back(std::make_pair(&F, Record[I+1]));
2508 break;
2509 }
2510
2511 case DECL_REPLACEMENTS: {
2512 if (Record.size() % 3 != 0) {
2513 Error("invalid DECL_REPLACEMENTS block in AST file");
2514 return true;
2515 }
2516 for (unsigned I = 0, N = Record.size(); I != N; I += 3)
2517 ReplacedDecls[getGlobalDeclID(F, Record[I])]
2518 = ReplacedDeclInfo(&F, Record[I+1], Record[I+2]);
2519 break;
2520 }
2521
2522 case OBJC_CATEGORIES_MAP: {
2523 if (F.LocalNumObjCCategoriesInMap != 0) {
2524 Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
2525 return true;
2526 }
2527
2528 F.LocalNumObjCCategoriesInMap = Record[0];
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002529 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002530 break;
2531 }
2532
2533 case OBJC_CATEGORIES:
2534 F.ObjCCategories.swap(Record);
2535 break;
2536
2537 case CXX_BASE_SPECIFIER_OFFSETS: {
2538 if (F.LocalNumCXXBaseSpecifiers != 0) {
2539 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2540 return true;
2541 }
2542
2543 F.LocalNumCXXBaseSpecifiers = Record[0];
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002544 F.CXXBaseSpecifiersOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002545 NumCXXBaseSpecifiersLoaded += F.LocalNumCXXBaseSpecifiers;
2546 break;
2547 }
2548
2549 case DIAG_PRAGMA_MAPPINGS:
2550 if (F.PragmaDiagMappings.empty())
2551 F.PragmaDiagMappings.swap(Record);
2552 else
2553 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
2554 Record.begin(), Record.end());
2555 break;
2556
2557 case CUDA_SPECIAL_DECL_REFS:
2558 // Later tables overwrite earlier ones.
2559 // FIXME: Modules will have trouble with this.
2560 CUDASpecialDeclRefs.clear();
2561 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2562 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
2563 break;
2564
2565 case HEADER_SEARCH_TABLE: {
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002566 F.HeaderFileInfoTableData = Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002567 F.LocalNumHeaderFileInfos = Record[1];
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002568 if (Record[0]) {
2569 F.HeaderFileInfoTable
2570 = HeaderFileInfoLookupTable::Create(
2571 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
2572 (const unsigned char *)F.HeaderFileInfoTableData,
2573 HeaderFileInfoTrait(*this, F,
2574 &PP.getHeaderSearchInfo(),
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002575 Blob.data() + Record[2]));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002576
2577 PP.getHeaderSearchInfo().SetExternalSource(this);
2578 if (!PP.getHeaderSearchInfo().getExternalLookup())
2579 PP.getHeaderSearchInfo().SetExternalLookup(this);
2580 }
2581 break;
2582 }
2583
2584 case FP_PRAGMA_OPTIONS:
2585 // Later tables overwrite earlier ones.
2586 FPPragmaOptions.swap(Record);
2587 break;
2588
2589 case OPENCL_EXTENSIONS:
2590 // Later tables overwrite earlier ones.
2591 OpenCLExtensions.swap(Record);
2592 break;
2593
2594 case TENTATIVE_DEFINITIONS:
2595 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2596 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
2597 break;
2598
2599 case KNOWN_NAMESPACES:
2600 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2601 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
2602 break;
Nick Lewycky01a41142013-01-26 00:35:08 +00002603
Nick Lewyckycd0655b2013-02-01 08:13:20 +00002604 case UNDEFINED_BUT_USED:
2605 if (UndefinedButUsed.size() % 2 != 0) {
2606 Error("Invalid existing UndefinedButUsed");
Nick Lewycky01a41142013-01-26 00:35:08 +00002607 return true;
2608 }
2609
2610 if (Record.size() % 2 != 0) {
Nick Lewyckycd0655b2013-02-01 08:13:20 +00002611 Error("invalid undefined-but-used record");
Nick Lewycky01a41142013-01-26 00:35:08 +00002612 return true;
2613 }
2614 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
Nick Lewyckycd0655b2013-02-01 08:13:20 +00002615 UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
2616 UndefinedButUsed.push_back(
Nick Lewycky01a41142013-01-26 00:35:08 +00002617 ReadSourceLocation(F, Record, I).getRawEncoding());
2618 }
2619 break;
2620
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002621 case IMPORTED_MODULES: {
2622 if (F.Kind != MK_Module) {
2623 // If we aren't loading a module (which has its own exports), make
2624 // all of the imported modules visible.
2625 // FIXME: Deal with macros-only imports.
2626 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2627 if (unsigned GlobalID = getGlobalSubmoduleID(F, Record[I]))
2628 ImportedModules.push_back(GlobalID);
2629 }
2630 }
2631 break;
2632 }
2633
2634 case LOCAL_REDECLARATIONS: {
2635 F.RedeclarationChains.swap(Record);
2636 break;
2637 }
2638
2639 case LOCAL_REDECLARATIONS_MAP: {
2640 if (F.LocalNumRedeclarationsInMap != 0) {
2641 Error("duplicate LOCAL_REDECLARATIONS_MAP record in AST file");
2642 return true;
2643 }
2644
2645 F.LocalNumRedeclarationsInMap = Record[0];
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002646 F.RedeclarationsMap = (const LocalRedeclarationsInfo *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002647 break;
2648 }
2649
2650 case MERGED_DECLARATIONS: {
2651 for (unsigned Idx = 0; Idx < Record.size(); /* increment in loop */) {
2652 GlobalDeclID CanonID = getGlobalDeclID(F, Record[Idx++]);
2653 SmallVectorImpl<GlobalDeclID> &Decls = StoredMergedDecls[CanonID];
2654 for (unsigned N = Record[Idx++]; N > 0; --N)
2655 Decls.push_back(getGlobalDeclID(F, Record[Idx++]));
2656 }
2657 break;
2658 }
2659
2660 case MACRO_OFFSET: {
2661 if (F.LocalNumMacros != 0) {
2662 Error("duplicate MACRO_OFFSET record in AST file");
2663 return true;
2664 }
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002665 F.MacroOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002666 F.LocalNumMacros = Record[0];
2667 unsigned LocalBaseMacroID = Record[1];
2668 F.BaseMacroID = getTotalNumMacros();
2669
2670 if (F.LocalNumMacros > 0) {
2671 // Introduce the global -> local mapping for macros within this module.
2672 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
2673
2674 // Introduce the local -> global mapping for macros within this module.
2675 F.MacroRemap.insertOrReplace(
2676 std::make_pair(LocalBaseMacroID,
2677 F.BaseMacroID - LocalBaseMacroID));
2678
2679 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
2680 }
2681 break;
2682 }
2683
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002684 case MACRO_TABLE: {
2685 // FIXME: Not used yet.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002686 break;
2687 }
2688 }
2689 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002690}
2691
Douglas Gregor2cbd4272013-02-12 23:36:21 +00002692/// \brief Move the given method to the back of the global list of methods.
2693static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
2694 // Find the entry for this selector in the method pool.
2695 Sema::GlobalMethodPool::iterator Known
2696 = S.MethodPool.find(Method->getSelector());
2697 if (Known == S.MethodPool.end())
2698 return;
2699
2700 // Retrieve the appropriate method list.
2701 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
2702 : Known->second.second;
2703 bool Found = false;
2704 for (ObjCMethodList *List = &Start; List; List = List->Next) {
2705 if (!Found) {
2706 if (List->Method == Method) {
2707 Found = true;
2708 } else {
2709 // Keep searching.
2710 continue;
2711 }
2712 }
2713
2714 if (List->Next)
2715 List->Method = List->Next->Method;
2716 else
2717 List->Method = Method;
2718 }
2719}
2720
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002721void ASTReader::makeNamesVisible(const HiddenNames &Names) {
2722 for (unsigned I = 0, N = Names.size(); I != N; ++I) {
2723 switch (Names[I].getKind()) {
Douglas Gregor2cbd4272013-02-12 23:36:21 +00002724 case HiddenName::Declaration: {
2725 Decl *D = Names[I].getDecl();
2726 bool wasHidden = D->Hidden;
2727 D->Hidden = false;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002728
Douglas Gregor2cbd4272013-02-12 23:36:21 +00002729 if (wasHidden && SemaObj) {
2730 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
2731 moveMethodToBackOfGlobalList(*SemaObj, Method);
2732 }
2733 }
2734 break;
2735 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002736 case HiddenName::MacroVisibility: {
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00002737 std::pair<IdentifierInfo *, MacroDirective *> Macro = Names[I].getMacro();
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002738 installImportedMacro(Macro.first, Macro.second);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002739 break;
2740 }
2741
2742 case HiddenName::MacroUndef: {
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002743 // FIXME: Remove HiddenName::MacroUndef and PPMutationListener.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002744 break;
2745 }
2746 }
2747 }
2748}
2749
2750void ASTReader::makeModuleVisible(Module *Mod,
Argyrios Kyrtzidis5ebcb202013-02-01 16:36:12 +00002751 Module::NameVisibilityKind NameVisibility,
Douglas Gregor906d66a2013-03-20 21:10:35 +00002752 SourceLocation ImportLoc,
2753 bool Complain) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002754 llvm::SmallPtrSet<Module *, 4> Visited;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002755 SmallVector<Module *, 4> Stack;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002756 Stack.push_back(Mod);
2757 while (!Stack.empty()) {
2758 Mod = Stack.back();
2759 Stack.pop_back();
2760
2761 if (NameVisibility <= Mod->NameVisibility) {
2762 // This module already has this level of visibility (or greater), so
2763 // there is nothing more to do.
2764 continue;
2765 }
2766
2767 if (!Mod->isAvailable()) {
2768 // Modules that aren't available cannot be made visible.
2769 continue;
2770 }
2771
2772 // Update the module's name visibility.
2773 Mod->NameVisibility = NameVisibility;
2774
2775 // If we've already deserialized any names from this module,
2776 // mark them as visible.
2777 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
2778 if (Hidden != HiddenNamesMap.end()) {
2779 makeNamesVisible(Hidden->second);
2780 HiddenNamesMap.erase(Hidden);
2781 }
2782
2783 // Push any non-explicit submodules onto the stack to be marked as
2784 // visible.
2785 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2786 SubEnd = Mod->submodule_end();
2787 Sub != SubEnd; ++Sub) {
2788 if (!(*Sub)->IsExplicit && Visited.insert(*Sub))
2789 Stack.push_back(*Sub);
2790 }
2791
2792 // Push any exported modules onto the stack to be marked as visible.
Argyrios Kyrtzidis21a00042013-02-19 19:34:40 +00002793 SmallVector<Module *, 16> Exports;
2794 Mod->getExportedModules(Exports);
2795 for (SmallVectorImpl<Module *>::iterator
2796 I = Exports.begin(), E = Exports.end(); I != E; ++I) {
2797 Module *Exported = *I;
2798 if (Visited.insert(Exported))
2799 Stack.push_back(Exported);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002800 }
Douglas Gregor906d66a2013-03-20 21:10:35 +00002801
2802 // Detect any conflicts.
2803 if (Complain) {
2804 assert(ImportLoc.isValid() && "Missing import location");
2805 for (unsigned I = 0, N = Mod->Conflicts.size(); I != N; ++I) {
2806 if (Mod->Conflicts[I].Other->NameVisibility >= NameVisibility) {
2807 Diag(ImportLoc, diag::warn_module_conflict)
2808 << Mod->getFullModuleName()
2809 << Mod->Conflicts[I].Other->getFullModuleName()
2810 << Mod->Conflicts[I].Message;
2811 // FIXME: Need note where the other module was imported.
2812 }
2813 }
2814 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002815 }
2816}
2817
Douglas Gregor1a49d972013-01-25 01:03:03 +00002818bool ASTReader::loadGlobalIndex() {
2819 if (GlobalIndex)
2820 return false;
2821
2822 if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
2823 !Context.getLangOpts().Modules)
2824 return true;
2825
2826 // Try to load the global index.
2827 TriedLoadingGlobalIndex = true;
2828 StringRef ModuleCachePath
2829 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
2830 std::pair<GlobalModuleIndex *, GlobalModuleIndex::ErrorCode> Result
Douglas Gregor677e15f2013-03-19 00:28:20 +00002831 = GlobalModuleIndex::readIndex(ModuleCachePath);
Douglas Gregor1a49d972013-01-25 01:03:03 +00002832 if (!Result.first)
2833 return true;
2834
2835 GlobalIndex.reset(Result.first);
Douglas Gregor188bdcd2013-01-25 23:32:03 +00002836 ModuleMgr.setGlobalIndex(GlobalIndex.get());
Douglas Gregor1a49d972013-01-25 01:03:03 +00002837 return false;
2838}
2839
2840bool ASTReader::isGlobalIndexUnavailable() const {
2841 return Context.getLangOpts().Modules && UseGlobalIndex &&
2842 !hasGlobalIndex() && TriedLoadingGlobalIndex;
2843}
2844
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002845ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
2846 ModuleKind Type,
2847 SourceLocation ImportLoc,
2848 unsigned ClientLoadCapabilities) {
2849 // Bump the generation number.
2850 unsigned PreviousGeneration = CurrentGeneration++;
2851
2852 unsigned NumModules = ModuleMgr.size();
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002853 SmallVector<ImportedModule, 4> Loaded;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002854 switch(ASTReadResult ReadResult = ReadASTCore(FileName, Type, ImportLoc,
2855 /*ImportedBy=*/0, Loaded,
Douglas Gregor677e15f2013-03-19 00:28:20 +00002856 0, 0,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002857 ClientLoadCapabilities)) {
2858 case Failure:
Douglas Gregor677e15f2013-03-19 00:28:20 +00002859 case Missing:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002860 case OutOfDate:
2861 case VersionMismatch:
2862 case ConfigurationMismatch:
2863 case HadErrors:
Douglas Gregor677e15f2013-03-19 00:28:20 +00002864 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, ModuleMgr.end(),
2865 Context.getLangOpts().Modules
2866 ? &PP.getHeaderSearchInfo().getModuleMap()
2867 : 0);
Douglas Gregor1a49d972013-01-25 01:03:03 +00002868
2869 // If we find that any modules are unusable, the global index is going
2870 // to be out-of-date. Just remove it.
2871 GlobalIndex.reset();
Douglas Gregor188bdcd2013-01-25 23:32:03 +00002872 ModuleMgr.setGlobalIndex(0);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002873 return ReadResult;
2874
2875 case Success:
2876 break;
2877 }
2878
2879 // Here comes stuff that we only do once the entire chain is loaded.
2880
2881 // Load the AST blocks of all of the modules that we loaded.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002882 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
2883 MEnd = Loaded.end();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002884 M != MEnd; ++M) {
2885 ModuleFile &F = *M->Mod;
2886
2887 // Read the AST block.
2888 if (ReadASTBlock(F))
2889 return Failure;
2890
2891 // Once read, set the ModuleFile bit base offset and update the size in
2892 // bits of all files we've seen.
2893 F.GlobalBitOffset = TotalModulesSizeInBits;
2894 TotalModulesSizeInBits += F.SizeInBits;
2895 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
2896
2897 // Preload SLocEntries.
2898 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
2899 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
2900 // Load it through the SourceManager and don't call ReadSLocEntry()
2901 // directly because the entry may have already been loaded in which case
2902 // calling ReadSLocEntry() directly would trigger an assertion in
2903 // SourceManager.
2904 SourceMgr.getLoadedSLocEntryByID(Index);
2905 }
2906 }
2907
Douglas Gregorfa69fc12013-03-22 18:50:14 +00002908 // Setup the import locations and notify the module manager that we've
2909 // committed to these module files.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002910 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
2911 MEnd = Loaded.end();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002912 M != MEnd; ++M) {
2913 ModuleFile &F = *M->Mod;
Douglas Gregorfa69fc12013-03-22 18:50:14 +00002914
2915 ModuleMgr.moduleFileAccepted(&F);
2916
2917 // Set the import location.
Argyrios Kyrtzidis8b136d82013-02-01 16:36:14 +00002918 F.DirectImportLoc = ImportLoc;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002919 if (!M->ImportedBy)
2920 F.ImportLoc = M->ImportLoc;
2921 else
2922 F.ImportLoc = ReadSourceLocation(*M->ImportedBy,
2923 M->ImportLoc.getRawEncoding());
2924 }
2925
2926 // Mark all of the identifiers in the identifier table as being out of date,
2927 // so that various accessors know to check the loaded modules when the
2928 // identifier is used.
2929 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
2930 IdEnd = PP.getIdentifierTable().end();
2931 Id != IdEnd; ++Id)
2932 Id->second->setOutOfDate(true);
2933
2934 // Resolve any unresolved module exports.
Douglas Gregor906d66a2013-03-20 21:10:35 +00002935 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
2936 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002937 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
2938 Module *ResolvedMod = getSubmodule(GlobalID);
Douglas Gregor906d66a2013-03-20 21:10:35 +00002939
2940 switch (Unresolved.Kind) {
2941 case UnresolvedModuleRef::Conflict:
2942 if (ResolvedMod) {
2943 Module::Conflict Conflict;
2944 Conflict.Other = ResolvedMod;
2945 Conflict.Message = Unresolved.String.str();
2946 Unresolved.Mod->Conflicts.push_back(Conflict);
2947 }
2948 continue;
2949
2950 case UnresolvedModuleRef::Import:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002951 if (ResolvedMod)
2952 Unresolved.Mod->Imports.push_back(ResolvedMod);
2953 continue;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002954
Douglas Gregor906d66a2013-03-20 21:10:35 +00002955 case UnresolvedModuleRef::Export:
2956 if (ResolvedMod || Unresolved.IsWildcard)
2957 Unresolved.Mod->Exports.push_back(
2958 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
2959 continue;
2960 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002961 }
Douglas Gregor906d66a2013-03-20 21:10:35 +00002962 UnresolvedModuleRefs.clear();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002963
2964 InitializeContext();
2965
2966 if (DeserializationListener)
2967 DeserializationListener->ReaderInitialized(this);
2968
2969 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
2970 if (!PrimaryModule.OriginalSourceFileID.isInvalid()) {
2971 PrimaryModule.OriginalSourceFileID
2972 = FileID::get(PrimaryModule.SLocEntryBaseID
2973 + PrimaryModule.OriginalSourceFileID.getOpaqueValue() - 1);
2974
2975 // If this AST file is a precompiled preamble, then set the
2976 // preamble file ID of the source manager to the file source file
2977 // from which the preamble was built.
2978 if (Type == MK_Preamble) {
2979 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
2980 } else if (Type == MK_MainFile) {
2981 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
2982 }
2983 }
2984
2985 // For any Objective-C class definitions we have already loaded, make sure
2986 // that we load any additional categories.
2987 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
2988 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
2989 ObjCClassesLoaded[I],
2990 PreviousGeneration);
2991 }
Douglas Gregor1a49d972013-01-25 01:03:03 +00002992
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002993 return Success;
2994}
2995
2996ASTReader::ASTReadResult
2997ASTReader::ReadASTCore(StringRef FileName,
2998 ModuleKind Type,
2999 SourceLocation ImportLoc,
3000 ModuleFile *ImportedBy,
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003001 SmallVectorImpl<ImportedModule> &Loaded,
Douglas Gregor677e15f2013-03-19 00:28:20 +00003002 off_t ExpectedSize, time_t ExpectedModTime,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003003 unsigned ClientLoadCapabilities) {
3004 ModuleFile *M;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003005 std::string ErrorStr;
Douglas Gregor677e15f2013-03-19 00:28:20 +00003006 ModuleManager::AddModuleResult AddResult
3007 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
3008 CurrentGeneration, ExpectedSize, ExpectedModTime,
3009 M, ErrorStr);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003010
Douglas Gregor677e15f2013-03-19 00:28:20 +00003011 switch (AddResult) {
3012 case ModuleManager::AlreadyLoaded:
3013 return Success;
3014
3015 case ModuleManager::NewlyLoaded:
3016 // Load module file below.
3017 break;
3018
3019 case ModuleManager::Missing:
3020 // The module file was missing; if the client handle handle, that, return
3021 // it.
3022 if (ClientLoadCapabilities & ARR_Missing)
3023 return Missing;
3024
3025 // Otherwise, return an error.
3026 {
3027 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
3028 + ErrorStr;
3029 Error(Msg);
3030 }
3031 return Failure;
3032
3033 case ModuleManager::OutOfDate:
3034 // We couldn't load the module file because it is out-of-date. If the
3035 // client can handle out-of-date, return it.
3036 if (ClientLoadCapabilities & ARR_OutOfDate)
3037 return OutOfDate;
3038
3039 // Otherwise, return an error.
3040 {
3041 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
3042 + ErrorStr;
3043 Error(Msg);
3044 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003045 return Failure;
3046 }
3047
Douglas Gregor677e15f2013-03-19 00:28:20 +00003048 assert(M && "Missing module file");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003049
3050 // FIXME: This seems rather a hack. Should CurrentDir be part of the
3051 // module?
3052 if (FileName != "-") {
3053 CurrentDir = llvm::sys::path::parent_path(FileName);
3054 if (CurrentDir.empty()) CurrentDir = ".";
3055 }
3056
3057 ModuleFile &F = *M;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003058 BitstreamCursor &Stream = F.Stream;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003059 Stream.init(F.StreamFile);
3060 F.SizeInBits = F.Buffer->getBufferSize() * 8;
3061
3062 // Sniff for the signature.
3063 if (Stream.Read(8) != 'C' ||
3064 Stream.Read(8) != 'P' ||
3065 Stream.Read(8) != 'C' ||
3066 Stream.Read(8) != 'H') {
3067 Diag(diag::err_not_a_pch_file) << FileName;
3068 return Failure;
3069 }
3070
3071 // This is used for compatibility with older PCH formats.
3072 bool HaveReadControlBlock = false;
3073
Chris Lattner99a5af02013-01-20 00:00:22 +00003074 while (1) {
3075 llvm::BitstreamEntry Entry = Stream.advance();
3076
3077 switch (Entry.Kind) {
3078 case llvm::BitstreamEntry::Error:
3079 case llvm::BitstreamEntry::EndBlock:
3080 case llvm::BitstreamEntry::Record:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003081 Error("invalid record at top-level of AST file");
3082 return Failure;
Chris Lattner99a5af02013-01-20 00:00:22 +00003083
3084 case llvm::BitstreamEntry::SubBlock:
3085 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003086 }
3087
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003088 // We only know the control subblock ID.
Chris Lattner99a5af02013-01-20 00:00:22 +00003089 switch (Entry.ID) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003090 case llvm::bitc::BLOCKINFO_BLOCK_ID:
3091 if (Stream.ReadBlockInfoBlock()) {
3092 Error("malformed BlockInfoBlock in AST file");
3093 return Failure;
3094 }
3095 break;
3096 case CONTROL_BLOCK_ID:
3097 HaveReadControlBlock = true;
3098 switch (ReadControlBlock(F, Loaded, ClientLoadCapabilities)) {
3099 case Success:
3100 break;
3101
3102 case Failure: return Failure;
Douglas Gregor677e15f2013-03-19 00:28:20 +00003103 case Missing: return Missing;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003104 case OutOfDate: return OutOfDate;
3105 case VersionMismatch: return VersionMismatch;
3106 case ConfigurationMismatch: return ConfigurationMismatch;
3107 case HadErrors: return HadErrors;
3108 }
3109 break;
3110 case AST_BLOCK_ID:
3111 if (!HaveReadControlBlock) {
3112 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
3113 Diag(diag::warn_pch_version_too_old);
3114 return VersionMismatch;
3115 }
3116
3117 // Record that we've loaded this module.
3118 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
3119 return Success;
3120
3121 default:
3122 if (Stream.SkipBlock()) {
3123 Error("malformed block record in AST file");
3124 return Failure;
3125 }
3126 break;
3127 }
3128 }
3129
3130 return Success;
3131}
3132
3133void ASTReader::InitializeContext() {
3134 // If there's a listener, notify them that we "read" the translation unit.
3135 if (DeserializationListener)
3136 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
3137 Context.getTranslationUnitDecl());
3138
3139 // Make sure we load the declaration update records for the translation unit,
3140 // if there are any.
3141 loadDeclUpdateRecords(PREDEF_DECL_TRANSLATION_UNIT_ID,
3142 Context.getTranslationUnitDecl());
3143
3144 // FIXME: Find a better way to deal with collisions between these
3145 // built-in types. Right now, we just ignore the problem.
3146
3147 // Load the special types.
3148 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
3149 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
3150 if (!Context.CFConstantStringTypeDecl)
3151 Context.setCFConstantStringType(GetType(String));
3152 }
3153
3154 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
3155 QualType FileType = GetType(File);
3156 if (FileType.isNull()) {
3157 Error("FILE type is NULL");
3158 return;
3159 }
3160
3161 if (!Context.FILEDecl) {
3162 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
3163 Context.setFILEDecl(Typedef->getDecl());
3164 else {
3165 const TagType *Tag = FileType->getAs<TagType>();
3166 if (!Tag) {
3167 Error("Invalid FILE type in AST file");
3168 return;
3169 }
3170 Context.setFILEDecl(Tag->getDecl());
3171 }
3172 }
3173 }
3174
3175 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
3176 QualType Jmp_bufType = GetType(Jmp_buf);
3177 if (Jmp_bufType.isNull()) {
3178 Error("jmp_buf type is NULL");
3179 return;
3180 }
3181
3182 if (!Context.jmp_bufDecl) {
3183 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
3184 Context.setjmp_bufDecl(Typedef->getDecl());
3185 else {
3186 const TagType *Tag = Jmp_bufType->getAs<TagType>();
3187 if (!Tag) {
3188 Error("Invalid jmp_buf type in AST file");
3189 return;
3190 }
3191 Context.setjmp_bufDecl(Tag->getDecl());
3192 }
3193 }
3194 }
3195
3196 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
3197 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
3198 if (Sigjmp_bufType.isNull()) {
3199 Error("sigjmp_buf type is NULL");
3200 return;
3201 }
3202
3203 if (!Context.sigjmp_bufDecl) {
3204 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
3205 Context.setsigjmp_bufDecl(Typedef->getDecl());
3206 else {
3207 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
3208 assert(Tag && "Invalid sigjmp_buf type in AST file");
3209 Context.setsigjmp_bufDecl(Tag->getDecl());
3210 }
3211 }
3212 }
3213
3214 if (unsigned ObjCIdRedef
3215 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
3216 if (Context.ObjCIdRedefinitionType.isNull())
3217 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
3218 }
3219
3220 if (unsigned ObjCClassRedef
3221 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
3222 if (Context.ObjCClassRedefinitionType.isNull())
3223 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
3224 }
3225
3226 if (unsigned ObjCSelRedef
3227 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
3228 if (Context.ObjCSelRedefinitionType.isNull())
3229 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
3230 }
3231
3232 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
3233 QualType Ucontext_tType = GetType(Ucontext_t);
3234 if (Ucontext_tType.isNull()) {
3235 Error("ucontext_t type is NULL");
3236 return;
3237 }
3238
3239 if (!Context.ucontext_tDecl) {
3240 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
3241 Context.setucontext_tDecl(Typedef->getDecl());
3242 else {
3243 const TagType *Tag = Ucontext_tType->getAs<TagType>();
3244 assert(Tag && "Invalid ucontext_t type in AST file");
3245 Context.setucontext_tDecl(Tag->getDecl());
3246 }
3247 }
3248 }
3249 }
3250
3251 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
3252
3253 // If there were any CUDA special declarations, deserialize them.
3254 if (!CUDASpecialDeclRefs.empty()) {
3255 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
3256 Context.setcudaConfigureCallDecl(
3257 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
3258 }
3259
3260 // Re-export any modules that were imported by a non-module AST file.
3261 for (unsigned I = 0, N = ImportedModules.size(); I != N; ++I) {
3262 if (Module *Imported = getSubmodule(ImportedModules[I]))
Argyrios Kyrtzidis5ebcb202013-02-01 16:36:12 +00003263 makeModuleVisible(Imported, Module::AllVisible,
Douglas Gregor906d66a2013-03-20 21:10:35 +00003264 /*ImportLoc=*/SourceLocation(),
3265 /*Complain=*/false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003266 }
3267 ImportedModules.clear();
3268}
3269
3270void ASTReader::finalizeForWriting() {
3271 for (HiddenNamesMapType::iterator Hidden = HiddenNamesMap.begin(),
3272 HiddenEnd = HiddenNamesMap.end();
3273 Hidden != HiddenEnd; ++Hidden) {
3274 makeNamesVisible(Hidden->second);
3275 }
3276 HiddenNamesMap.clear();
3277}
3278
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003279/// SkipCursorToControlBlock - Given a cursor at the start of an AST file, scan
3280/// ahead and drop the cursor into the start of the CONTROL_BLOCK, returning
3281/// false on success and true on failure.
3282static bool SkipCursorToControlBlock(BitstreamCursor &Cursor) {
3283 while (1) {
3284 llvm::BitstreamEntry Entry = Cursor.advance();
3285 switch (Entry.Kind) {
3286 case llvm::BitstreamEntry::Error:
3287 case llvm::BitstreamEntry::EndBlock:
3288 return true;
3289
3290 case llvm::BitstreamEntry::Record:
3291 // Ignore top-level records.
3292 Cursor.skipRecord(Entry.ID);
3293 break;
3294
3295 case llvm::BitstreamEntry::SubBlock:
3296 if (Entry.ID == CONTROL_BLOCK_ID) {
3297 if (Cursor.EnterSubBlock(CONTROL_BLOCK_ID))
3298 return true;
3299 // Found it!
3300 return false;
3301 }
3302
3303 if (Cursor.SkipBlock())
3304 return true;
3305 }
3306 }
3307}
3308
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003309/// \brief Retrieve the name of the original source file name
3310/// directly from the AST file, without actually loading the AST
3311/// file.
3312std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
3313 FileManager &FileMgr,
3314 DiagnosticsEngine &Diags) {
3315 // Open the AST file.
3316 std::string ErrStr;
3317 OwningPtr<llvm::MemoryBuffer> Buffer;
3318 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
3319 if (!Buffer) {
3320 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ASTFileName << ErrStr;
3321 return std::string();
3322 }
3323
3324 // Initialize the stream
3325 llvm::BitstreamReader StreamFile;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003326 BitstreamCursor Stream;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003327 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
3328 (const unsigned char *)Buffer->getBufferEnd());
3329 Stream.init(StreamFile);
3330
3331 // Sniff for the signature.
3332 if (Stream.Read(8) != 'C' ||
3333 Stream.Read(8) != 'P' ||
3334 Stream.Read(8) != 'C' ||
3335 Stream.Read(8) != 'H') {
3336 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
3337 return std::string();
3338 }
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003339
Chris Lattner88bde502013-01-19 21:39:22 +00003340 // Scan for the CONTROL_BLOCK_ID block.
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003341 if (SkipCursorToControlBlock(Stream)) {
3342 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
3343 return std::string();
Chris Lattner88bde502013-01-19 21:39:22 +00003344 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003345
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003346 // Scan for ORIGINAL_FILE inside the control block.
3347 RecordData Record;
Chris Lattner88bde502013-01-19 21:39:22 +00003348 while (1) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003349 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Chris Lattner88bde502013-01-19 21:39:22 +00003350 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
3351 return std::string();
3352
3353 if (Entry.Kind != llvm::BitstreamEntry::Record) {
3354 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
3355 return std::string();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003356 }
Chris Lattner88bde502013-01-19 21:39:22 +00003357
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003358 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003359 StringRef Blob;
3360 if (Stream.readRecord(Entry.ID, Record, &Blob) == ORIGINAL_FILE)
3361 return Blob.str();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003362 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003363}
3364
3365namespace {
3366 class SimplePCHValidator : public ASTReaderListener {
3367 const LangOptions &ExistingLangOpts;
3368 const TargetOptions &ExistingTargetOpts;
3369 const PreprocessorOptions &ExistingPPOpts;
3370 FileManager &FileMgr;
3371
3372 public:
3373 SimplePCHValidator(const LangOptions &ExistingLangOpts,
3374 const TargetOptions &ExistingTargetOpts,
3375 const PreprocessorOptions &ExistingPPOpts,
3376 FileManager &FileMgr)
3377 : ExistingLangOpts(ExistingLangOpts),
3378 ExistingTargetOpts(ExistingTargetOpts),
3379 ExistingPPOpts(ExistingPPOpts),
3380 FileMgr(FileMgr)
3381 {
3382 }
3383
3384 virtual bool ReadLanguageOptions(const LangOptions &LangOpts,
3385 bool Complain) {
3386 return checkLanguageOptions(ExistingLangOpts, LangOpts, 0);
3387 }
3388 virtual bool ReadTargetOptions(const TargetOptions &TargetOpts,
3389 bool Complain) {
3390 return checkTargetOptions(ExistingTargetOpts, TargetOpts, 0);
3391 }
3392 virtual bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
3393 bool Complain,
3394 std::string &SuggestedPredefines) {
3395 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, 0, FileMgr,
3396 SuggestedPredefines);
3397 }
3398 };
3399}
3400
3401bool ASTReader::readASTFileControlBlock(StringRef Filename,
3402 FileManager &FileMgr,
3403 ASTReaderListener &Listener) {
3404 // Open the AST file.
3405 std::string ErrStr;
3406 OwningPtr<llvm::MemoryBuffer> Buffer;
3407 Buffer.reset(FileMgr.getBufferForFile(Filename, &ErrStr));
3408 if (!Buffer) {
3409 return true;
3410 }
3411
3412 // Initialize the stream
3413 llvm::BitstreamReader StreamFile;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003414 BitstreamCursor Stream;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003415 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
3416 (const unsigned char *)Buffer->getBufferEnd());
3417 Stream.init(StreamFile);
3418
3419 // Sniff for the signature.
3420 if (Stream.Read(8) != 'C' ||
3421 Stream.Read(8) != 'P' ||
3422 Stream.Read(8) != 'C' ||
3423 Stream.Read(8) != 'H') {
3424 return true;
3425 }
3426
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003427 // Scan for the CONTROL_BLOCK_ID block.
3428 if (SkipCursorToControlBlock(Stream))
3429 return true;
3430
3431 // Scan for ORIGINAL_FILE inside the control block.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003432 RecordData Record;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003433 while (1) {
3434 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
3435 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
3436 return false;
3437
3438 if (Entry.Kind != llvm::BitstreamEntry::Record)
3439 return true;
3440
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003441 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003442 StringRef Blob;
3443 unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob);
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003444 switch ((ControlRecordTypes)RecCode) {
3445 case METADATA: {
3446 if (Record[0] != VERSION_MAJOR)
3447 return true;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003448
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003449 const std::string &CurBranch = getClangFullRepositoryVersion();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003450 if (StringRef(CurBranch) != Blob)
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003451 return true;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003452
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003453 break;
3454 }
3455 case LANGUAGE_OPTIONS:
3456 if (ParseLanguageOptions(Record, false, Listener))
3457 return true;
3458 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003459
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003460 case TARGET_OPTIONS:
3461 if (ParseTargetOptions(Record, false, Listener))
3462 return true;
3463 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003464
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003465 case DIAGNOSTIC_OPTIONS:
3466 if (ParseDiagnosticOptions(Record, false, Listener))
3467 return true;
3468 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003469
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003470 case FILE_SYSTEM_OPTIONS:
3471 if (ParseFileSystemOptions(Record, false, Listener))
3472 return true;
3473 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003474
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003475 case HEADER_SEARCH_OPTIONS:
3476 if (ParseHeaderSearchOptions(Record, false, Listener))
3477 return true;
3478 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003479
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003480 case PREPROCESSOR_OPTIONS: {
3481 std::string IgnoredSuggestedPredefines;
3482 if (ParsePreprocessorOptions(Record, false, Listener,
3483 IgnoredSuggestedPredefines))
3484 return true;
3485 break;
3486 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003487
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003488 default:
3489 // No other validation to perform.
3490 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003491 }
3492 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003493}
3494
3495
3496bool ASTReader::isAcceptableASTFile(StringRef Filename,
3497 FileManager &FileMgr,
3498 const LangOptions &LangOpts,
3499 const TargetOptions &TargetOpts,
3500 const PreprocessorOptions &PPOpts) {
3501 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts, FileMgr);
3502 return !readASTFileControlBlock(Filename, FileMgr, validator);
3503}
3504
3505bool ASTReader::ReadSubmoduleBlock(ModuleFile &F) {
3506 // Enter the submodule block.
3507 if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
3508 Error("malformed submodule block record in AST file");
3509 return true;
3510 }
3511
3512 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
3513 bool First = true;
3514 Module *CurrentModule = 0;
3515 RecordData Record;
3516 while (true) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003517 llvm::BitstreamEntry Entry = F.Stream.advanceSkippingSubblocks();
3518
3519 switch (Entry.Kind) {
3520 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
3521 case llvm::BitstreamEntry::Error:
3522 Error("malformed block record in AST file");
3523 return true;
3524 case llvm::BitstreamEntry::EndBlock:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003525 return false;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003526 case llvm::BitstreamEntry::Record:
3527 // The interesting case.
3528 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003529 }
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003530
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003531 // Read a record.
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003532 StringRef Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003533 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003534 switch (F.Stream.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003535 default: // Default behavior: ignore.
3536 break;
3537
3538 case SUBMODULE_DEFINITION: {
3539 if (First) {
3540 Error("missing submodule metadata record at beginning of block");
3541 return true;
3542 }
3543
Douglas Gregor970e4412013-03-20 03:59:18 +00003544 if (Record.size() < 8) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003545 Error("malformed module definition");
3546 return true;
3547 }
3548
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003549 StringRef Name = Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003550 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[0]);
3551 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[1]);
3552 bool IsFramework = Record[2];
3553 bool IsExplicit = Record[3];
3554 bool IsSystem = Record[4];
3555 bool InferSubmodules = Record[5];
3556 bool InferExplicitSubmodules = Record[6];
3557 bool InferExportWildcard = Record[7];
Douglas Gregor970e4412013-03-20 03:59:18 +00003558 bool ConfigMacrosExhaustive = Record[8];
3559
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003560 Module *ParentModule = 0;
3561 if (Parent)
3562 ParentModule = getSubmodule(Parent);
3563
3564 // Retrieve this (sub)module from the module map, creating it if
3565 // necessary.
3566 CurrentModule = ModMap.findOrCreateModule(Name, ParentModule,
3567 IsFramework,
3568 IsExplicit).first;
3569 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
3570 if (GlobalIndex >= SubmodulesLoaded.size() ||
3571 SubmodulesLoaded[GlobalIndex]) {
3572 Error("too many submodules");
3573 return true;
3574 }
Douglas Gregor8bf778e2013-02-06 22:40:31 +00003575
Douglas Gregor677e15f2013-03-19 00:28:20 +00003576 if (!ParentModule) {
3577 if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
3578 if (CurFile != F.File) {
3579 if (!Diags.isDiagnosticInFlight()) {
3580 Diag(diag::err_module_file_conflict)
3581 << CurrentModule->getTopLevelModuleName()
3582 << CurFile->getName()
3583 << F.File->getName();
3584 }
3585 return true;
Douglas Gregor8bf778e2013-02-06 22:40:31 +00003586 }
Douglas Gregor8bf778e2013-02-06 22:40:31 +00003587 }
Douglas Gregor677e15f2013-03-19 00:28:20 +00003588
3589 CurrentModule->setASTFile(F.File);
Douglas Gregor8bf778e2013-02-06 22:40:31 +00003590 }
Douglas Gregor677e15f2013-03-19 00:28:20 +00003591
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003592 CurrentModule->IsFromModuleFile = true;
3593 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
3594 CurrentModule->InferSubmodules = InferSubmodules;
3595 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
3596 CurrentModule->InferExportWildcard = InferExportWildcard;
Douglas Gregor970e4412013-03-20 03:59:18 +00003597 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003598 if (DeserializationListener)
3599 DeserializationListener->ModuleRead(GlobalID, CurrentModule);
3600
3601 SubmodulesLoaded[GlobalIndex] = CurrentModule;
Douglas Gregorb6cbe512013-01-14 17:21:00 +00003602
Douglas Gregor906d66a2013-03-20 21:10:35 +00003603 // Clear out data that will be replaced by what is the module file.
Douglas Gregorb6cbe512013-01-14 17:21:00 +00003604 CurrentModule->LinkLibraries.clear();
Douglas Gregor970e4412013-03-20 03:59:18 +00003605 CurrentModule->ConfigMacros.clear();
Douglas Gregor906d66a2013-03-20 21:10:35 +00003606 CurrentModule->UnresolvedConflicts.clear();
3607 CurrentModule->Conflicts.clear();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003608 break;
3609 }
3610
3611 case SUBMODULE_UMBRELLA_HEADER: {
3612 if (First) {
3613 Error("missing submodule metadata record at beginning of block");
3614 return true;
3615 }
3616
3617 if (!CurrentModule)
3618 break;
3619
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003620 if (const FileEntry *Umbrella = PP.getFileManager().getFile(Blob)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003621 if (!CurrentModule->getUmbrellaHeader())
3622 ModMap.setUmbrellaHeader(CurrentModule, Umbrella);
3623 else if (CurrentModule->getUmbrellaHeader() != Umbrella) {
3624 Error("mismatched umbrella headers in submodule");
3625 return true;
3626 }
3627 }
3628 break;
3629 }
3630
3631 case SUBMODULE_HEADER: {
3632 if (First) {
3633 Error("missing submodule metadata record at beginning of block");
3634 return true;
3635 }
3636
3637 if (!CurrentModule)
3638 break;
3639
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00003640 // We lazily associate headers with their modules via the HeaderInfoTable.
3641 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
3642 // of complete filenames or remove it entirely.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003643 break;
3644 }
3645
3646 case SUBMODULE_EXCLUDED_HEADER: {
3647 if (First) {
3648 Error("missing submodule metadata record at beginning of block");
3649 return true;
3650 }
3651
3652 if (!CurrentModule)
3653 break;
3654
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00003655 // We lazily associate headers with their modules via the HeaderInfoTable.
3656 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
3657 // of complete filenames or remove it entirely.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003658 break;
3659 }
3660
3661 case SUBMODULE_TOPHEADER: {
3662 if (First) {
3663 Error("missing submodule metadata record at beginning of block");
3664 return true;
3665 }
3666
3667 if (!CurrentModule)
3668 break;
3669
Argyrios Kyrtzidisc1d22392013-03-13 21:13:43 +00003670 CurrentModule->addTopHeaderFilename(Blob);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003671 break;
3672 }
3673
3674 case SUBMODULE_UMBRELLA_DIR: {
3675 if (First) {
3676 Error("missing submodule metadata record at beginning of block");
3677 return true;
3678 }
3679
3680 if (!CurrentModule)
3681 break;
3682
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003683 if (const DirectoryEntry *Umbrella
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003684 = PP.getFileManager().getDirectory(Blob)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003685 if (!CurrentModule->getUmbrellaDir())
3686 ModMap.setUmbrellaDir(CurrentModule, Umbrella);
3687 else if (CurrentModule->getUmbrellaDir() != Umbrella) {
3688 Error("mismatched umbrella directories in submodule");
3689 return true;
3690 }
3691 }
3692 break;
3693 }
3694
3695 case SUBMODULE_METADATA: {
3696 if (!First) {
3697 Error("submodule metadata record not at beginning of block");
3698 return true;
3699 }
3700 First = false;
3701
3702 F.BaseSubmoduleID = getTotalNumSubmodules();
3703 F.LocalNumSubmodules = Record[0];
3704 unsigned LocalBaseSubmoduleID = Record[1];
3705 if (F.LocalNumSubmodules > 0) {
3706 // Introduce the global -> local mapping for submodules within this
3707 // module.
3708 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
3709
3710 // Introduce the local -> global mapping for submodules within this
3711 // module.
3712 F.SubmoduleRemap.insertOrReplace(
3713 std::make_pair(LocalBaseSubmoduleID,
3714 F.BaseSubmoduleID - LocalBaseSubmoduleID));
3715
3716 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
3717 }
3718 break;
3719 }
3720
3721 case SUBMODULE_IMPORTS: {
3722 if (First) {
3723 Error("missing submodule metadata record at beginning of block");
3724 return true;
3725 }
3726
3727 if (!CurrentModule)
3728 break;
3729
3730 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
Douglas Gregor906d66a2013-03-20 21:10:35 +00003731 UnresolvedModuleRef Unresolved;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003732 Unresolved.File = &F;
3733 Unresolved.Mod = CurrentModule;
3734 Unresolved.ID = Record[Idx];
Douglas Gregor906d66a2013-03-20 21:10:35 +00003735 Unresolved.Kind = UnresolvedModuleRef::Import;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003736 Unresolved.IsWildcard = false;
Douglas Gregor906d66a2013-03-20 21:10:35 +00003737 UnresolvedModuleRefs.push_back(Unresolved);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003738 }
3739 break;
3740 }
3741
3742 case SUBMODULE_EXPORTS: {
3743 if (First) {
3744 Error("missing submodule metadata record at beginning of block");
3745 return true;
3746 }
3747
3748 if (!CurrentModule)
3749 break;
3750
3751 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
Douglas Gregor906d66a2013-03-20 21:10:35 +00003752 UnresolvedModuleRef Unresolved;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003753 Unresolved.File = &F;
3754 Unresolved.Mod = CurrentModule;
3755 Unresolved.ID = Record[Idx];
Douglas Gregor906d66a2013-03-20 21:10:35 +00003756 Unresolved.Kind = UnresolvedModuleRef::Export;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003757 Unresolved.IsWildcard = Record[Idx + 1];
Douglas Gregor906d66a2013-03-20 21:10:35 +00003758 UnresolvedModuleRefs.push_back(Unresolved);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003759 }
3760
3761 // Once we've loaded the set of exports, there's no reason to keep
3762 // the parsed, unresolved exports around.
3763 CurrentModule->UnresolvedExports.clear();
3764 break;
3765 }
3766 case SUBMODULE_REQUIRES: {
3767 if (First) {
3768 Error("missing submodule metadata record at beginning of block");
3769 return true;
3770 }
3771
3772 if (!CurrentModule)
3773 break;
3774
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003775 CurrentModule->addRequirement(Blob, Context.getLangOpts(),
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003776 Context.getTargetInfo());
3777 break;
3778 }
Douglas Gregorb6cbe512013-01-14 17:21:00 +00003779
3780 case SUBMODULE_LINK_LIBRARY:
3781 if (First) {
3782 Error("missing submodule metadata record at beginning of block");
3783 return true;
3784 }
3785
3786 if (!CurrentModule)
3787 break;
3788
3789 CurrentModule->LinkLibraries.push_back(
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003790 Module::LinkLibrary(Blob, Record[0]));
Douglas Gregorb6cbe512013-01-14 17:21:00 +00003791 break;
Douglas Gregor63a72682013-03-20 00:22:05 +00003792
3793 case SUBMODULE_CONFIG_MACRO:
3794 if (First) {
3795 Error("missing submodule metadata record at beginning of block");
3796 return true;
3797 }
3798
3799 if (!CurrentModule)
3800 break;
3801
3802 CurrentModule->ConfigMacros.push_back(Blob.str());
3803 break;
Douglas Gregor906d66a2013-03-20 21:10:35 +00003804
3805 case SUBMODULE_CONFLICT: {
3806 if (First) {
3807 Error("missing submodule metadata record at beginning of block");
3808 return true;
3809 }
3810
3811 if (!CurrentModule)
3812 break;
3813
3814 UnresolvedModuleRef Unresolved;
3815 Unresolved.File = &F;
3816 Unresolved.Mod = CurrentModule;
3817 Unresolved.ID = Record[0];
3818 Unresolved.Kind = UnresolvedModuleRef::Conflict;
3819 Unresolved.IsWildcard = false;
3820 Unresolved.String = Blob;
3821 UnresolvedModuleRefs.push_back(Unresolved);
3822 break;
3823 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003824 }
3825 }
3826}
3827
3828/// \brief Parse the record that corresponds to a LangOptions data
3829/// structure.
3830///
3831/// This routine parses the language options from the AST file and then gives
3832/// them to the AST listener if one is set.
3833///
3834/// \returns true if the listener deems the file unacceptable, false otherwise.
3835bool ASTReader::ParseLanguageOptions(const RecordData &Record,
3836 bool Complain,
3837 ASTReaderListener &Listener) {
3838 LangOptions LangOpts;
3839 unsigned Idx = 0;
3840#define LANGOPT(Name, Bits, Default, Description) \
3841 LangOpts.Name = Record[Idx++];
3842#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
3843 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
3844#include "clang/Basic/LangOptions.def"
Will Dietz4f45bc02013-01-18 11:30:38 +00003845#define SANITIZER(NAME, ID) LangOpts.Sanitize.ID = Record[Idx++];
3846#include "clang/Basic/Sanitizers.def"
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003847
3848 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
3849 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
3850 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
3851
3852 unsigned Length = Record[Idx++];
3853 LangOpts.CurrentModule.assign(Record.begin() + Idx,
3854 Record.begin() + Idx + Length);
Dmitri Gribenko6ebf0912013-02-22 14:21:27 +00003855
3856 Idx += Length;
3857
3858 // Comment options.
3859 for (unsigned N = Record[Idx++]; N; --N) {
3860 LangOpts.CommentOpts.BlockCommandNames.push_back(
3861 ReadString(Record, Idx));
3862 }
3863
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003864 return Listener.ReadLanguageOptions(LangOpts, Complain);
3865}
3866
3867bool ASTReader::ParseTargetOptions(const RecordData &Record,
3868 bool Complain,
3869 ASTReaderListener &Listener) {
3870 unsigned Idx = 0;
3871 TargetOptions TargetOpts;
3872 TargetOpts.Triple = ReadString(Record, Idx);
3873 TargetOpts.CPU = ReadString(Record, Idx);
3874 TargetOpts.ABI = ReadString(Record, Idx);
3875 TargetOpts.CXXABI = ReadString(Record, Idx);
3876 TargetOpts.LinkerVersion = ReadString(Record, Idx);
3877 for (unsigned N = Record[Idx++]; N; --N) {
3878 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
3879 }
3880 for (unsigned N = Record[Idx++]; N; --N) {
3881 TargetOpts.Features.push_back(ReadString(Record, Idx));
3882 }
3883
3884 return Listener.ReadTargetOptions(TargetOpts, Complain);
3885}
3886
3887bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
3888 ASTReaderListener &Listener) {
3889 DiagnosticOptions DiagOpts;
3890 unsigned Idx = 0;
3891#define DIAGOPT(Name, Bits, Default) DiagOpts.Name = Record[Idx++];
3892#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
3893 DiagOpts.set##Name(static_cast<Type>(Record[Idx++]));
3894#include "clang/Basic/DiagnosticOptions.def"
3895
3896 for (unsigned N = Record[Idx++]; N; --N) {
3897 DiagOpts.Warnings.push_back(ReadString(Record, Idx));
3898 }
3899
3900 return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
3901}
3902
3903bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
3904 ASTReaderListener &Listener) {
3905 FileSystemOptions FSOpts;
3906 unsigned Idx = 0;
3907 FSOpts.WorkingDir = ReadString(Record, Idx);
3908 return Listener.ReadFileSystemOptions(FSOpts, Complain);
3909}
3910
3911bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
3912 bool Complain,
3913 ASTReaderListener &Listener) {
3914 HeaderSearchOptions HSOpts;
3915 unsigned Idx = 0;
3916 HSOpts.Sysroot = ReadString(Record, Idx);
3917
3918 // Include entries.
3919 for (unsigned N = Record[Idx++]; N; --N) {
3920 std::string Path = ReadString(Record, Idx);
3921 frontend::IncludeDirGroup Group
3922 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003923 bool IsFramework = Record[Idx++];
3924 bool IgnoreSysRoot = Record[Idx++];
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003925 HSOpts.UserEntries.push_back(
Daniel Dunbar59fd6352013-01-30 00:34:26 +00003926 HeaderSearchOptions::Entry(Path, Group, IsFramework, IgnoreSysRoot));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003927 }
3928
3929 // System header prefixes.
3930 for (unsigned N = Record[Idx++]; N; --N) {
3931 std::string Prefix = ReadString(Record, Idx);
3932 bool IsSystemHeader = Record[Idx++];
3933 HSOpts.SystemHeaderPrefixes.push_back(
3934 HeaderSearchOptions::SystemHeaderPrefix(Prefix, IsSystemHeader));
3935 }
3936
3937 HSOpts.ResourceDir = ReadString(Record, Idx);
3938 HSOpts.ModuleCachePath = ReadString(Record, Idx);
3939 HSOpts.DisableModuleHash = Record[Idx++];
3940 HSOpts.UseBuiltinIncludes = Record[Idx++];
3941 HSOpts.UseStandardSystemIncludes = Record[Idx++];
3942 HSOpts.UseStandardCXXIncludes = Record[Idx++];
3943 HSOpts.UseLibcxx = Record[Idx++];
3944
3945 return Listener.ReadHeaderSearchOptions(HSOpts, Complain);
3946}
3947
3948bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
3949 bool Complain,
3950 ASTReaderListener &Listener,
3951 std::string &SuggestedPredefines) {
3952 PreprocessorOptions PPOpts;
3953 unsigned Idx = 0;
3954
3955 // Macro definitions/undefs
3956 for (unsigned N = Record[Idx++]; N; --N) {
3957 std::string Macro = ReadString(Record, Idx);
3958 bool IsUndef = Record[Idx++];
3959 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
3960 }
3961
3962 // Includes
3963 for (unsigned N = Record[Idx++]; N; --N) {
3964 PPOpts.Includes.push_back(ReadString(Record, Idx));
3965 }
3966
3967 // Macro Includes
3968 for (unsigned N = Record[Idx++]; N; --N) {
3969 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
3970 }
3971
3972 PPOpts.UsePredefines = Record[Idx++];
3973 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
3974 PPOpts.ImplicitPTHInclude = ReadString(Record, Idx);
3975 PPOpts.ObjCXXARCStandardLibrary =
3976 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
3977 SuggestedPredefines.clear();
3978 return Listener.ReadPreprocessorOptions(PPOpts, Complain,
3979 SuggestedPredefines);
3980}
3981
3982std::pair<ModuleFile *, unsigned>
3983ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
3984 GlobalPreprocessedEntityMapType::iterator
3985 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
3986 assert(I != GlobalPreprocessedEntityMap.end() &&
3987 "Corrupted global preprocessed entity map");
3988 ModuleFile *M = I->second;
3989 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
3990 return std::make_pair(M, LocalIndex);
3991}
3992
3993std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
3994ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
3995 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
3996 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
3997 Mod.NumPreprocessedEntities);
3998
3999 return std::make_pair(PreprocessingRecord::iterator(),
4000 PreprocessingRecord::iterator());
4001}
4002
4003std::pair<ASTReader::ModuleDeclIterator, ASTReader::ModuleDeclIterator>
4004ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
4005 return std::make_pair(ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
4006 ModuleDeclIterator(this, &Mod,
4007 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
4008}
4009
4010PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
4011 PreprocessedEntityID PPID = Index+1;
4012 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
4013 ModuleFile &M = *PPInfo.first;
4014 unsigned LocalIndex = PPInfo.second;
4015 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
4016
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004017 if (!PP.getPreprocessingRecord()) {
4018 Error("no preprocessing record");
4019 return 0;
4020 }
4021
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004022 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
4023 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
4024
4025 llvm::BitstreamEntry Entry =
4026 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
4027 if (Entry.Kind != llvm::BitstreamEntry::Record)
4028 return 0;
4029
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004030 // Read the record.
4031 SourceRange Range(ReadSourceLocation(M, PPOffs.Begin),
4032 ReadSourceLocation(M, PPOffs.End));
4033 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00004034 StringRef Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004035 RecordData Record;
4036 PreprocessorDetailRecordTypes RecType =
Chris Lattnerb3ce3572013-01-20 02:38:54 +00004037 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.readRecord(
4038 Entry.ID, Record, &Blob);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004039 switch (RecType) {
4040 case PPD_MACRO_EXPANSION: {
4041 bool isBuiltin = Record[0];
4042 IdentifierInfo *Name = 0;
4043 MacroDefinition *Def = 0;
4044 if (isBuiltin)
4045 Name = getLocalIdentifier(M, Record[1]);
4046 else {
4047 PreprocessedEntityID
4048 GlobalID = getGlobalPreprocessedEntityID(M, Record[1]);
4049 Def =cast<MacroDefinition>(PPRec.getLoadedPreprocessedEntity(GlobalID-1));
4050 }
4051
4052 MacroExpansion *ME;
4053 if (isBuiltin)
4054 ME = new (PPRec) MacroExpansion(Name, Range);
4055 else
4056 ME = new (PPRec) MacroExpansion(Def, Range);
4057
4058 return ME;
4059 }
4060
4061 case PPD_MACRO_DEFINITION: {
4062 // Decode the identifier info and then check again; if the macro is
4063 // still defined and associated with the identifier,
4064 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
4065 MacroDefinition *MD
4066 = new (PPRec) MacroDefinition(II, Range);
4067
4068 if (DeserializationListener)
4069 DeserializationListener->MacroDefinitionRead(PPID, MD);
4070
4071 return MD;
4072 }
4073
4074 case PPD_INCLUSION_DIRECTIVE: {
Chris Lattnerb3ce3572013-01-20 02:38:54 +00004075 const char *FullFileNameStart = Blob.data() + Record[0];
4076 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004077 const FileEntry *File = 0;
4078 if (!FullFileName.empty())
4079 File = PP.getFileManager().getFile(FullFileName);
4080
4081 // FIXME: Stable encoding
4082 InclusionDirective::InclusionKind Kind
4083 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
4084 InclusionDirective *ID
4085 = new (PPRec) InclusionDirective(PPRec, Kind,
Chris Lattnerb3ce3572013-01-20 02:38:54 +00004086 StringRef(Blob.data(), Record[0]),
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004087 Record[1], Record[3],
4088 File,
4089 Range);
4090 return ID;
4091 }
4092 }
4093
4094 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
4095}
4096
4097/// \brief \arg SLocMapI points at a chunk of a module that contains no
4098/// preprocessed entities or the entities it contains are not the ones we are
4099/// looking for. Find the next module that contains entities and return the ID
4100/// of the first entry.
4101PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
4102 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
4103 ++SLocMapI;
4104 for (GlobalSLocOffsetMapType::const_iterator
4105 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
4106 ModuleFile &M = *SLocMapI->second;
4107 if (M.NumPreprocessedEntities)
4108 return M.BasePreprocessedEntityID;
4109 }
4110
4111 return getTotalNumPreprocessedEntities();
4112}
4113
4114namespace {
4115
4116template <unsigned PPEntityOffset::*PPLoc>
4117struct PPEntityComp {
4118 const ASTReader &Reader;
4119 ModuleFile &M;
4120
4121 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) { }
4122
4123 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
4124 SourceLocation LHS = getLoc(L);
4125 SourceLocation RHS = getLoc(R);
4126 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4127 }
4128
4129 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
4130 SourceLocation LHS = getLoc(L);
4131 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4132 }
4133
4134 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
4135 SourceLocation RHS = getLoc(R);
4136 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4137 }
4138
4139 SourceLocation getLoc(const PPEntityOffset &PPE) const {
4140 return Reader.ReadSourceLocation(M, PPE.*PPLoc);
4141 }
4142};
4143
4144}
4145
4146/// \brief Returns the first preprocessed entity ID that ends after \arg BLoc.
4147PreprocessedEntityID
4148ASTReader::findBeginPreprocessedEntity(SourceLocation BLoc) const {
4149 if (SourceMgr.isLocalSourceLocation(BLoc))
4150 return getTotalNumPreprocessedEntities();
4151
4152 GlobalSLocOffsetMapType::const_iterator
4153 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
Argyrios Kyrtzidisee2d5fd2013-03-08 02:32:34 +00004154 BLoc.getOffset() - 1);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004155 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
4156 "Corrupted global sloc offset map");
4157
4158 if (SLocMapI->second->NumPreprocessedEntities == 0)
4159 return findNextPreprocessedEntity(SLocMapI);
4160
4161 ModuleFile &M = *SLocMapI->second;
4162 typedef const PPEntityOffset *pp_iterator;
4163 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
4164 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
4165
4166 size_t Count = M.NumPreprocessedEntities;
4167 size_t Half;
4168 pp_iterator First = pp_begin;
4169 pp_iterator PPI;
4170
4171 // Do a binary search manually instead of using std::lower_bound because
4172 // The end locations of entities may be unordered (when a macro expansion
4173 // is inside another macro argument), but for this case it is not important
4174 // whether we get the first macro expansion or its containing macro.
4175 while (Count > 0) {
4176 Half = Count/2;
4177 PPI = First;
4178 std::advance(PPI, Half);
4179 if (SourceMgr.isBeforeInTranslationUnit(ReadSourceLocation(M, PPI->End),
4180 BLoc)){
4181 First = PPI;
4182 ++First;
4183 Count = Count - Half - 1;
4184 } else
4185 Count = Half;
4186 }
4187
4188 if (PPI == pp_end)
4189 return findNextPreprocessedEntity(SLocMapI);
4190
4191 return M.BasePreprocessedEntityID + (PPI - pp_begin);
4192}
4193
4194/// \brief Returns the first preprocessed entity ID that begins after \arg ELoc.
4195PreprocessedEntityID
4196ASTReader::findEndPreprocessedEntity(SourceLocation ELoc) const {
4197 if (SourceMgr.isLocalSourceLocation(ELoc))
4198 return getTotalNumPreprocessedEntities();
4199
4200 GlobalSLocOffsetMapType::const_iterator
4201 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
Argyrios Kyrtzidisee2d5fd2013-03-08 02:32:34 +00004202 ELoc.getOffset() - 1);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004203 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
4204 "Corrupted global sloc offset map");
4205
4206 if (SLocMapI->second->NumPreprocessedEntities == 0)
4207 return findNextPreprocessedEntity(SLocMapI);
4208
4209 ModuleFile &M = *SLocMapI->second;
4210 typedef const PPEntityOffset *pp_iterator;
4211 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
4212 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
4213 pp_iterator PPI =
4214 std::upper_bound(pp_begin, pp_end, ELoc,
4215 PPEntityComp<&PPEntityOffset::Begin>(*this, M));
4216
4217 if (PPI == pp_end)
4218 return findNextPreprocessedEntity(SLocMapI);
4219
4220 return M.BasePreprocessedEntityID + (PPI - pp_begin);
4221}
4222
4223/// \brief Returns a pair of [Begin, End) indices of preallocated
4224/// preprocessed entities that \arg Range encompasses.
4225std::pair<unsigned, unsigned>
4226 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
4227 if (Range.isInvalid())
4228 return std::make_pair(0,0);
4229 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
4230
4231 PreprocessedEntityID BeginID = findBeginPreprocessedEntity(Range.getBegin());
4232 PreprocessedEntityID EndID = findEndPreprocessedEntity(Range.getEnd());
4233 return std::make_pair(BeginID, EndID);
4234}
4235
4236/// \brief Optionally returns true or false if the preallocated preprocessed
4237/// entity with index \arg Index came from file \arg FID.
David Blaikiedc84cd52013-02-20 22:23:23 +00004238Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004239 FileID FID) {
4240 if (FID.isInvalid())
4241 return false;
4242
4243 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
4244 ModuleFile &M = *PPInfo.first;
4245 unsigned LocalIndex = PPInfo.second;
4246 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
4247
4248 SourceLocation Loc = ReadSourceLocation(M, PPOffs.Begin);
4249 if (Loc.isInvalid())
4250 return false;
4251
4252 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
4253 return true;
4254 else
4255 return false;
4256}
4257
4258namespace {
4259 /// \brief Visitor used to search for information about a header file.
4260 class HeaderFileInfoVisitor {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004261 const FileEntry *FE;
4262
David Blaikiedc84cd52013-02-20 22:23:23 +00004263 Optional<HeaderFileInfo> HFI;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004264
4265 public:
Argyrios Kyrtzidis36592b12013-03-06 18:12:44 +00004266 explicit HeaderFileInfoVisitor(const FileEntry *FE)
4267 : FE(FE) { }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004268
4269 static bool visit(ModuleFile &M, void *UserData) {
4270 HeaderFileInfoVisitor *This
4271 = static_cast<HeaderFileInfoVisitor *>(UserData);
4272
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004273 HeaderFileInfoLookupTable *Table
4274 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
4275 if (!Table)
4276 return false;
4277
4278 // Look in the on-disk hash table for an entry for this file name.
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00004279 HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004280 if (Pos == Table->end())
4281 return false;
4282
4283 This->HFI = *Pos;
4284 return true;
4285 }
4286
David Blaikiedc84cd52013-02-20 22:23:23 +00004287 Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004288 };
4289}
4290
4291HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
Argyrios Kyrtzidis36592b12013-03-06 18:12:44 +00004292 HeaderFileInfoVisitor Visitor(FE);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004293 ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor);
David Blaikiedc84cd52013-02-20 22:23:23 +00004294 if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004295 if (Listener)
4296 Listener->ReadHeaderFileInfo(*HFI, FE->getUID());
4297 return *HFI;
4298 }
4299
4300 return HeaderFileInfo();
4301}
4302
4303void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
4304 // FIXME: Make it work properly with modules.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00004305 SmallVector<DiagnosticsEngine::DiagState *, 32> DiagStates;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004306 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
4307 ModuleFile &F = *(*I);
4308 unsigned Idx = 0;
4309 DiagStates.clear();
4310 assert(!Diag.DiagStates.empty());
4311 DiagStates.push_back(&Diag.DiagStates.front()); // the command-line one.
4312 while (Idx < F.PragmaDiagMappings.size()) {
4313 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
4314 unsigned DiagStateID = F.PragmaDiagMappings[Idx++];
4315 if (DiagStateID != 0) {
4316 Diag.DiagStatePoints.push_back(
4317 DiagnosticsEngine::DiagStatePoint(DiagStates[DiagStateID-1],
4318 FullSourceLoc(Loc, SourceMgr)));
4319 continue;
4320 }
4321
4322 assert(DiagStateID == 0);
4323 // A new DiagState was created here.
4324 Diag.DiagStates.push_back(*Diag.GetCurDiagState());
4325 DiagnosticsEngine::DiagState *NewState = &Diag.DiagStates.back();
4326 DiagStates.push_back(NewState);
4327 Diag.DiagStatePoints.push_back(
4328 DiagnosticsEngine::DiagStatePoint(NewState,
4329 FullSourceLoc(Loc, SourceMgr)));
4330 while (1) {
4331 assert(Idx < F.PragmaDiagMappings.size() &&
4332 "Invalid data, didn't find '-1' marking end of diag/map pairs");
4333 if (Idx >= F.PragmaDiagMappings.size()) {
4334 break; // Something is messed up but at least avoid infinite loop in
4335 // release build.
4336 }
4337 unsigned DiagID = F.PragmaDiagMappings[Idx++];
4338 if (DiagID == (unsigned)-1) {
4339 break; // no more diag/map pairs for this location.
4340 }
4341 diag::Mapping Map = (diag::Mapping)F.PragmaDiagMappings[Idx++];
4342 DiagnosticMappingInfo MappingInfo = Diag.makeMappingInfo(Map, Loc);
4343 Diag.GetCurDiagState()->setMappingInfo(DiagID, MappingInfo);
4344 }
4345 }
4346 }
4347}
4348
4349/// \brief Get the correct cursor and offset for loading a type.
4350ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
4351 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
4352 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
4353 ModuleFile *M = I->second;
4354 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
4355}
4356
4357/// \brief Read and return the type with the given index..
4358///
4359/// The index is the type ID, shifted and minus the number of predefs. This
4360/// routine actually reads the record corresponding to the type at the given
4361/// location. It is a helper routine for GetType, which deals with reading type
4362/// IDs.
4363QualType ASTReader::readTypeRecord(unsigned Index) {
4364 RecordLocation Loc = TypeCursorForIndex(Index);
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004365 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004366
4367 // Keep track of where we are in the stream, then jump back there
4368 // after reading this type.
4369 SavedStreamPosition SavedPosition(DeclsCursor);
4370
4371 ReadingKindTracker ReadingKind(Read_Type, *this);
4372
4373 // Note that we are loading a type record.
4374 Deserializing AType(this);
4375
4376 unsigned Idx = 0;
4377 DeclsCursor.JumpToBit(Loc.Offset);
4378 RecordData Record;
4379 unsigned Code = DeclsCursor.ReadCode();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00004380 switch ((TypeCode)DeclsCursor.readRecord(Code, Record)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004381 case TYPE_EXT_QUAL: {
4382 if (Record.size() != 2) {
4383 Error("Incorrect encoding of extended qualifier type");
4384 return QualType();
4385 }
4386 QualType Base = readType(*Loc.F, Record, Idx);
4387 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
4388 return Context.getQualifiedType(Base, Quals);
4389 }
4390
4391 case TYPE_COMPLEX: {
4392 if (Record.size() != 1) {
4393 Error("Incorrect encoding of complex type");
4394 return QualType();
4395 }
4396 QualType ElemType = readType(*Loc.F, Record, Idx);
4397 return Context.getComplexType(ElemType);
4398 }
4399
4400 case TYPE_POINTER: {
4401 if (Record.size() != 1) {
4402 Error("Incorrect encoding of pointer type");
4403 return QualType();
4404 }
4405 QualType PointeeType = readType(*Loc.F, Record, Idx);
4406 return Context.getPointerType(PointeeType);
4407 }
4408
4409 case TYPE_BLOCK_POINTER: {
4410 if (Record.size() != 1) {
4411 Error("Incorrect encoding of block pointer type");
4412 return QualType();
4413 }
4414 QualType PointeeType = readType(*Loc.F, Record, Idx);
4415 return Context.getBlockPointerType(PointeeType);
4416 }
4417
4418 case TYPE_LVALUE_REFERENCE: {
4419 if (Record.size() != 2) {
4420 Error("Incorrect encoding of lvalue reference type");
4421 return QualType();
4422 }
4423 QualType PointeeType = readType(*Loc.F, Record, Idx);
4424 return Context.getLValueReferenceType(PointeeType, Record[1]);
4425 }
4426
4427 case TYPE_RVALUE_REFERENCE: {
4428 if (Record.size() != 1) {
4429 Error("Incorrect encoding of rvalue reference type");
4430 return QualType();
4431 }
4432 QualType PointeeType = readType(*Loc.F, Record, Idx);
4433 return Context.getRValueReferenceType(PointeeType);
4434 }
4435
4436 case TYPE_MEMBER_POINTER: {
4437 if (Record.size() != 2) {
4438 Error("Incorrect encoding of member pointer type");
4439 return QualType();
4440 }
4441 QualType PointeeType = readType(*Loc.F, Record, Idx);
4442 QualType ClassType = readType(*Loc.F, Record, Idx);
4443 if (PointeeType.isNull() || ClassType.isNull())
4444 return QualType();
4445
4446 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
4447 }
4448
4449 case TYPE_CONSTANT_ARRAY: {
4450 QualType ElementType = readType(*Loc.F, Record, Idx);
4451 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4452 unsigned IndexTypeQuals = Record[2];
4453 unsigned Idx = 3;
4454 llvm::APInt Size = ReadAPInt(Record, Idx);
4455 return Context.getConstantArrayType(ElementType, Size,
4456 ASM, IndexTypeQuals);
4457 }
4458
4459 case TYPE_INCOMPLETE_ARRAY: {
4460 QualType ElementType = readType(*Loc.F, Record, Idx);
4461 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4462 unsigned IndexTypeQuals = Record[2];
4463 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
4464 }
4465
4466 case TYPE_VARIABLE_ARRAY: {
4467 QualType ElementType = readType(*Loc.F, Record, Idx);
4468 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4469 unsigned IndexTypeQuals = Record[2];
4470 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
4471 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
4472 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
4473 ASM, IndexTypeQuals,
4474 SourceRange(LBLoc, RBLoc));
4475 }
4476
4477 case TYPE_VECTOR: {
4478 if (Record.size() != 3) {
4479 Error("incorrect encoding of vector type in AST file");
4480 return QualType();
4481 }
4482
4483 QualType ElementType = readType(*Loc.F, Record, Idx);
4484 unsigned NumElements = Record[1];
4485 unsigned VecKind = Record[2];
4486 return Context.getVectorType(ElementType, NumElements,
4487 (VectorType::VectorKind)VecKind);
4488 }
4489
4490 case TYPE_EXT_VECTOR: {
4491 if (Record.size() != 3) {
4492 Error("incorrect encoding of extended vector type in AST file");
4493 return QualType();
4494 }
4495
4496 QualType ElementType = readType(*Loc.F, Record, Idx);
4497 unsigned NumElements = Record[1];
4498 return Context.getExtVectorType(ElementType, NumElements);
4499 }
4500
4501 case TYPE_FUNCTION_NO_PROTO: {
4502 if (Record.size() != 6) {
4503 Error("incorrect encoding of no-proto function type");
4504 return QualType();
4505 }
4506 QualType ResultType = readType(*Loc.F, Record, Idx);
4507 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
4508 (CallingConv)Record[4], Record[5]);
4509 return Context.getFunctionNoProtoType(ResultType, Info);
4510 }
4511
4512 case TYPE_FUNCTION_PROTO: {
4513 QualType ResultType = readType(*Loc.F, Record, Idx);
4514
4515 FunctionProtoType::ExtProtoInfo EPI;
4516 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
4517 /*hasregparm*/ Record[2],
4518 /*regparm*/ Record[3],
4519 static_cast<CallingConv>(Record[4]),
4520 /*produces*/ Record[5]);
4521
4522 unsigned Idx = 6;
4523 unsigned NumParams = Record[Idx++];
4524 SmallVector<QualType, 16> ParamTypes;
4525 for (unsigned I = 0; I != NumParams; ++I)
4526 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
4527
4528 EPI.Variadic = Record[Idx++];
4529 EPI.HasTrailingReturn = Record[Idx++];
4530 EPI.TypeQuals = Record[Idx++];
4531 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
4532 ExceptionSpecificationType EST =
4533 static_cast<ExceptionSpecificationType>(Record[Idx++]);
4534 EPI.ExceptionSpecType = EST;
4535 SmallVector<QualType, 2> Exceptions;
4536 if (EST == EST_Dynamic) {
4537 EPI.NumExceptions = Record[Idx++];
4538 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
4539 Exceptions.push_back(readType(*Loc.F, Record, Idx));
4540 EPI.Exceptions = Exceptions.data();
4541 } else if (EST == EST_ComputedNoexcept) {
4542 EPI.NoexceptExpr = ReadExpr(*Loc.F);
4543 } else if (EST == EST_Uninstantiated) {
4544 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
4545 EPI.ExceptionSpecTemplate = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
4546 } else if (EST == EST_Unevaluated) {
4547 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
4548 }
Jordan Rosebea522f2013-03-08 21:51:21 +00004549 return Context.getFunctionType(ResultType, ParamTypes, EPI);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004550 }
4551
4552 case TYPE_UNRESOLVED_USING: {
4553 unsigned Idx = 0;
4554 return Context.getTypeDeclType(
4555 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
4556 }
4557
4558 case TYPE_TYPEDEF: {
4559 if (Record.size() != 2) {
4560 Error("incorrect encoding of typedef type");
4561 return QualType();
4562 }
4563 unsigned Idx = 0;
4564 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
4565 QualType Canonical = readType(*Loc.F, Record, Idx);
4566 if (!Canonical.isNull())
4567 Canonical = Context.getCanonicalType(Canonical);
4568 return Context.getTypedefType(Decl, Canonical);
4569 }
4570
4571 case TYPE_TYPEOF_EXPR:
4572 return Context.getTypeOfExprType(ReadExpr(*Loc.F));
4573
4574 case TYPE_TYPEOF: {
4575 if (Record.size() != 1) {
4576 Error("incorrect encoding of typeof(type) in AST file");
4577 return QualType();
4578 }
4579 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
4580 return Context.getTypeOfType(UnderlyingType);
4581 }
4582
4583 case TYPE_DECLTYPE: {
4584 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
4585 return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
4586 }
4587
4588 case TYPE_UNARY_TRANSFORM: {
4589 QualType BaseType = readType(*Loc.F, Record, Idx);
4590 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
4591 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
4592 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
4593 }
4594
4595 case TYPE_AUTO:
4596 return Context.getAutoType(readType(*Loc.F, Record, Idx));
4597
4598 case TYPE_RECORD: {
4599 if (Record.size() != 2) {
4600 Error("incorrect encoding of record type");
4601 return QualType();
4602 }
4603 unsigned Idx = 0;
4604 bool IsDependent = Record[Idx++];
4605 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
4606 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
4607 QualType T = Context.getRecordType(RD);
4608 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
4609 return T;
4610 }
4611
4612 case TYPE_ENUM: {
4613 if (Record.size() != 2) {
4614 Error("incorrect encoding of enum type");
4615 return QualType();
4616 }
4617 unsigned Idx = 0;
4618 bool IsDependent = Record[Idx++];
4619 QualType T
4620 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
4621 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
4622 return T;
4623 }
4624
4625 case TYPE_ATTRIBUTED: {
4626 if (Record.size() != 3) {
4627 Error("incorrect encoding of attributed type");
4628 return QualType();
4629 }
4630 QualType modifiedType = readType(*Loc.F, Record, Idx);
4631 QualType equivalentType = readType(*Loc.F, Record, Idx);
4632 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
4633 return Context.getAttributedType(kind, modifiedType, equivalentType);
4634 }
4635
4636 case TYPE_PAREN: {
4637 if (Record.size() != 1) {
4638 Error("incorrect encoding of paren type");
4639 return QualType();
4640 }
4641 QualType InnerType = readType(*Loc.F, Record, Idx);
4642 return Context.getParenType(InnerType);
4643 }
4644
4645 case TYPE_PACK_EXPANSION: {
4646 if (Record.size() != 2) {
4647 Error("incorrect encoding of pack expansion type");
4648 return QualType();
4649 }
4650 QualType Pattern = readType(*Loc.F, Record, Idx);
4651 if (Pattern.isNull())
4652 return QualType();
David Blaikiedc84cd52013-02-20 22:23:23 +00004653 Optional<unsigned> NumExpansions;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004654 if (Record[1])
4655 NumExpansions = Record[1] - 1;
4656 return Context.getPackExpansionType(Pattern, NumExpansions);
4657 }
4658
4659 case TYPE_ELABORATED: {
4660 unsigned Idx = 0;
4661 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
4662 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
4663 QualType NamedType = readType(*Loc.F, Record, Idx);
4664 return Context.getElaboratedType(Keyword, NNS, NamedType);
4665 }
4666
4667 case TYPE_OBJC_INTERFACE: {
4668 unsigned Idx = 0;
4669 ObjCInterfaceDecl *ItfD
4670 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
4671 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
4672 }
4673
4674 case TYPE_OBJC_OBJECT: {
4675 unsigned Idx = 0;
4676 QualType Base = readType(*Loc.F, Record, Idx);
4677 unsigned NumProtos = Record[Idx++];
4678 SmallVector<ObjCProtocolDecl*, 4> Protos;
4679 for (unsigned I = 0; I != NumProtos; ++I)
4680 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
4681 return Context.getObjCObjectType(Base, Protos.data(), NumProtos);
4682 }
4683
4684 case TYPE_OBJC_OBJECT_POINTER: {
4685 unsigned Idx = 0;
4686 QualType Pointee = readType(*Loc.F, Record, Idx);
4687 return Context.getObjCObjectPointerType(Pointee);
4688 }
4689
4690 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
4691 unsigned Idx = 0;
4692 QualType Parm = readType(*Loc.F, Record, Idx);
4693 QualType Replacement = readType(*Loc.F, Record, Idx);
4694 return
4695 Context.getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
4696 Replacement);
4697 }
4698
4699 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
4700 unsigned Idx = 0;
4701 QualType Parm = readType(*Loc.F, Record, Idx);
4702 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
4703 return Context.getSubstTemplateTypeParmPackType(
4704 cast<TemplateTypeParmType>(Parm),
4705 ArgPack);
4706 }
4707
4708 case TYPE_INJECTED_CLASS_NAME: {
4709 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
4710 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
4711 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
4712 // for AST reading, too much interdependencies.
4713 return
4714 QualType(new (Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
4715 }
4716
4717 case TYPE_TEMPLATE_TYPE_PARM: {
4718 unsigned Idx = 0;
4719 unsigned Depth = Record[Idx++];
4720 unsigned Index = Record[Idx++];
4721 bool Pack = Record[Idx++];
4722 TemplateTypeParmDecl *D
4723 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
4724 return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
4725 }
4726
4727 case TYPE_DEPENDENT_NAME: {
4728 unsigned Idx = 0;
4729 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
4730 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
4731 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
4732 QualType Canon = readType(*Loc.F, Record, Idx);
4733 if (!Canon.isNull())
4734 Canon = Context.getCanonicalType(Canon);
4735 return Context.getDependentNameType(Keyword, NNS, Name, Canon);
4736 }
4737
4738 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
4739 unsigned Idx = 0;
4740 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
4741 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
4742 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
4743 unsigned NumArgs = Record[Idx++];
4744 SmallVector<TemplateArgument, 8> Args;
4745 Args.reserve(NumArgs);
4746 while (NumArgs--)
4747 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
4748 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
4749 Args.size(), Args.data());
4750 }
4751
4752 case TYPE_DEPENDENT_SIZED_ARRAY: {
4753 unsigned Idx = 0;
4754
4755 // ArrayType
4756 QualType ElementType = readType(*Loc.F, Record, Idx);
4757 ArrayType::ArraySizeModifier ASM
4758 = (ArrayType::ArraySizeModifier)Record[Idx++];
4759 unsigned IndexTypeQuals = Record[Idx++];
4760
4761 // DependentSizedArrayType
4762 Expr *NumElts = ReadExpr(*Loc.F);
4763 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
4764
4765 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
4766 IndexTypeQuals, Brackets);
4767 }
4768
4769 case TYPE_TEMPLATE_SPECIALIZATION: {
4770 unsigned Idx = 0;
4771 bool IsDependent = Record[Idx++];
4772 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
4773 SmallVector<TemplateArgument, 8> Args;
4774 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
4775 QualType Underlying = readType(*Loc.F, Record, Idx);
4776 QualType T;
4777 if (Underlying.isNull())
4778 T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(),
4779 Args.size());
4780 else
4781 T = Context.getTemplateSpecializationType(Name, Args.data(),
4782 Args.size(), Underlying);
4783 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
4784 return T;
4785 }
4786
4787 case TYPE_ATOMIC: {
4788 if (Record.size() != 1) {
4789 Error("Incorrect encoding of atomic type");
4790 return QualType();
4791 }
4792 QualType ValueType = readType(*Loc.F, Record, Idx);
4793 return Context.getAtomicType(ValueType);
4794 }
4795 }
4796 llvm_unreachable("Invalid TypeCode!");
4797}
4798
4799class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
4800 ASTReader &Reader;
4801 ModuleFile &F;
4802 const ASTReader::RecordData &Record;
4803 unsigned &Idx;
4804
4805 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
4806 unsigned &I) {
4807 return Reader.ReadSourceLocation(F, R, I);
4808 }
4809
4810 template<typename T>
4811 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
4812 return Reader.ReadDeclAs<T>(F, Record, Idx);
4813 }
4814
4815public:
4816 TypeLocReader(ASTReader &Reader, ModuleFile &F,
4817 const ASTReader::RecordData &Record, unsigned &Idx)
4818 : Reader(Reader), F(F), Record(Record), Idx(Idx)
4819 { }
4820
4821 // We want compile-time assurance that we've enumerated all of
4822 // these, so unfortunately we have to declare them first, then
4823 // define them out-of-line.
4824#define ABSTRACT_TYPELOC(CLASS, PARENT)
4825#define TYPELOC(CLASS, PARENT) \
4826 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
4827#include "clang/AST/TypeLocNodes.def"
4828
4829 void VisitFunctionTypeLoc(FunctionTypeLoc);
4830 void VisitArrayTypeLoc(ArrayTypeLoc);
4831};
4832
4833void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
4834 // nothing to do
4835}
4836void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
4837 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
4838 if (TL.needsExtraLocalData()) {
4839 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
4840 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
4841 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
4842 TL.setModeAttr(Record[Idx++]);
4843 }
4844}
4845void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
4846 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4847}
4848void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
4849 TL.setStarLoc(ReadSourceLocation(Record, Idx));
4850}
4851void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
4852 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
4853}
4854void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
4855 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
4856}
4857void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
4858 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
4859}
4860void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
4861 TL.setStarLoc(ReadSourceLocation(Record, Idx));
4862 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
4863}
4864void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
4865 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
4866 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
4867 if (Record[Idx++])
4868 TL.setSizeExpr(Reader.ReadExpr(F));
4869 else
4870 TL.setSizeExpr(0);
4871}
4872void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
4873 VisitArrayTypeLoc(TL);
4874}
4875void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
4876 VisitArrayTypeLoc(TL);
4877}
4878void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
4879 VisitArrayTypeLoc(TL);
4880}
4881void TypeLocReader::VisitDependentSizedArrayTypeLoc(
4882 DependentSizedArrayTypeLoc TL) {
4883 VisitArrayTypeLoc(TL);
4884}
4885void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
4886 DependentSizedExtVectorTypeLoc TL) {
4887 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4888}
4889void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
4890 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4891}
4892void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
4893 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4894}
4895void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
4896 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
4897 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4898 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4899 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
4900 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
4901 TL.setArg(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
4902 }
4903}
4904void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
4905 VisitFunctionTypeLoc(TL);
4906}
4907void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
4908 VisitFunctionTypeLoc(TL);
4909}
4910void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
4911 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4912}
4913void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
4914 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4915}
4916void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
4917 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
4918 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4919 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4920}
4921void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
4922 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
4923 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4924 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4925 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
4926}
4927void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
4928 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4929}
4930void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
4931 TL.setKWLoc(ReadSourceLocation(Record, Idx));
4932 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4933 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4934 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
4935}
4936void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
4937 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4938}
4939void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
4940 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4941}
4942void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
4943 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4944}
4945void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
4946 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
4947 if (TL.hasAttrOperand()) {
4948 SourceRange range;
4949 range.setBegin(ReadSourceLocation(Record, Idx));
4950 range.setEnd(ReadSourceLocation(Record, Idx));
4951 TL.setAttrOperandParensRange(range);
4952 }
4953 if (TL.hasAttrExprOperand()) {
4954 if (Record[Idx++])
4955 TL.setAttrExprOperand(Reader.ReadExpr(F));
4956 else
4957 TL.setAttrExprOperand(0);
4958 } else if (TL.hasAttrEnumOperand())
4959 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
4960}
4961void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
4962 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4963}
4964void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
4965 SubstTemplateTypeParmTypeLoc TL) {
4966 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4967}
4968void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
4969 SubstTemplateTypeParmPackTypeLoc TL) {
4970 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4971}
4972void TypeLocReader::VisitTemplateSpecializationTypeLoc(
4973 TemplateSpecializationTypeLoc TL) {
4974 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
4975 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
4976 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4977 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
4978 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
4979 TL.setArgLocInfo(i,
4980 Reader.GetTemplateArgumentLocInfo(F,
4981 TL.getTypePtr()->getArg(i).getKind(),
4982 Record, Idx));
4983}
4984void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
4985 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4986 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4987}
4988void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
4989 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
4990 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
4991}
4992void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
4993 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4994}
4995void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
4996 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
4997 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
4998 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4999}
5000void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
5001 DependentTemplateSpecializationTypeLoc TL) {
5002 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5003 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5004 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
5005 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
5006 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5007 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5008 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
5009 TL.setArgLocInfo(I,
5010 Reader.GetTemplateArgumentLocInfo(F,
5011 TL.getTypePtr()->getArg(I).getKind(),
5012 Record, Idx));
5013}
5014void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
5015 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
5016}
5017void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
5018 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5019}
5020void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
5021 TL.setHasBaseTypeAsWritten(Record[Idx++]);
5022 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5023 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5024 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
5025 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
5026}
5027void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
5028 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5029}
5030void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
5031 TL.setKWLoc(ReadSourceLocation(Record, Idx));
5032 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5033 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5034}
5035
5036TypeSourceInfo *ASTReader::GetTypeSourceInfo(ModuleFile &F,
5037 const RecordData &Record,
5038 unsigned &Idx) {
5039 QualType InfoTy = readType(F, Record, Idx);
5040 if (InfoTy.isNull())
5041 return 0;
5042
5043 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
5044 TypeLocReader TLR(*this, F, Record, Idx);
5045 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
5046 TLR.Visit(TL);
5047 return TInfo;
5048}
5049
5050QualType ASTReader::GetType(TypeID ID) {
5051 unsigned FastQuals = ID & Qualifiers::FastMask;
5052 unsigned Index = ID >> Qualifiers::FastWidth;
5053
5054 if (Index < NUM_PREDEF_TYPE_IDS) {
5055 QualType T;
5056 switch ((PredefinedTypeIDs)Index) {
5057 case PREDEF_TYPE_NULL_ID: return QualType();
5058 case PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
5059 case PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
5060
5061 case PREDEF_TYPE_CHAR_U_ID:
5062 case PREDEF_TYPE_CHAR_S_ID:
5063 // FIXME: Check that the signedness of CharTy is correct!
5064 T = Context.CharTy;
5065 break;
5066
5067 case PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
5068 case PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
5069 case PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
5070 case PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
5071 case PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
5072 case PREDEF_TYPE_UINT128_ID: T = Context.UnsignedInt128Ty; break;
5073 case PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
5074 case PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
5075 case PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
5076 case PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
5077 case PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
5078 case PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
5079 case PREDEF_TYPE_INT128_ID: T = Context.Int128Ty; break;
5080 case PREDEF_TYPE_HALF_ID: T = Context.HalfTy; break;
5081 case PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
5082 case PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
5083 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
5084 case PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
5085 case PREDEF_TYPE_BOUND_MEMBER: T = Context.BoundMemberTy; break;
5086 case PREDEF_TYPE_PSEUDO_OBJECT: T = Context.PseudoObjectTy; break;
5087 case PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
5088 case PREDEF_TYPE_UNKNOWN_ANY: T = Context.UnknownAnyTy; break;
5089 case PREDEF_TYPE_NULLPTR_ID: T = Context.NullPtrTy; break;
5090 case PREDEF_TYPE_CHAR16_ID: T = Context.Char16Ty; break;
5091 case PREDEF_TYPE_CHAR32_ID: T = Context.Char32Ty; break;
5092 case PREDEF_TYPE_OBJC_ID: T = Context.ObjCBuiltinIdTy; break;
5093 case PREDEF_TYPE_OBJC_CLASS: T = Context.ObjCBuiltinClassTy; break;
5094 case PREDEF_TYPE_OBJC_SEL: T = Context.ObjCBuiltinSelTy; break;
Guy Benyeib13621d2012-12-18 14:38:23 +00005095 case PREDEF_TYPE_IMAGE1D_ID: T = Context.OCLImage1dTy; break;
5096 case PREDEF_TYPE_IMAGE1D_ARR_ID: T = Context.OCLImage1dArrayTy; break;
5097 case PREDEF_TYPE_IMAGE1D_BUFF_ID: T = Context.OCLImage1dBufferTy; break;
5098 case PREDEF_TYPE_IMAGE2D_ID: T = Context.OCLImage2dTy; break;
5099 case PREDEF_TYPE_IMAGE2D_ARR_ID: T = Context.OCLImage2dArrayTy; break;
5100 case PREDEF_TYPE_IMAGE3D_ID: T = Context.OCLImage3dTy; break;
Guy Benyei21f18c42013-02-07 10:55:47 +00005101 case PREDEF_TYPE_SAMPLER_ID: T = Context.OCLSamplerTy; break;
Guy Benyeie6b9d802013-01-20 12:31:11 +00005102 case PREDEF_TYPE_EVENT_ID: T = Context.OCLEventTy; break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005103 case PREDEF_TYPE_AUTO_DEDUCT: T = Context.getAutoDeductType(); break;
5104
5105 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
5106 T = Context.getAutoRRefDeductType();
5107 break;
5108
5109 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
5110 T = Context.ARCUnbridgedCastTy;
5111 break;
5112
5113 case PREDEF_TYPE_VA_LIST_TAG:
5114 T = Context.getVaListTagType();
5115 break;
5116
5117 case PREDEF_TYPE_BUILTIN_FN:
5118 T = Context.BuiltinFnTy;
5119 break;
5120 }
5121
5122 assert(!T.isNull() && "Unknown predefined type");
5123 return T.withFastQualifiers(FastQuals);
5124 }
5125
5126 Index -= NUM_PREDEF_TYPE_IDS;
5127 assert(Index < TypesLoaded.size() && "Type index out-of-range");
5128 if (TypesLoaded[Index].isNull()) {
5129 TypesLoaded[Index] = readTypeRecord(Index);
5130 if (TypesLoaded[Index].isNull())
5131 return QualType();
5132
5133 TypesLoaded[Index]->setFromAST();
5134 if (DeserializationListener)
5135 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
5136 TypesLoaded[Index]);
5137 }
5138
5139 return TypesLoaded[Index].withFastQualifiers(FastQuals);
5140}
5141
5142QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
5143 return GetType(getGlobalTypeID(F, LocalID));
5144}
5145
5146serialization::TypeID
5147ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
5148 unsigned FastQuals = LocalID & Qualifiers::FastMask;
5149 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
5150
5151 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
5152 return LocalID;
5153
5154 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5155 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
5156 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
5157
5158 unsigned GlobalIndex = LocalIndex + I->second;
5159 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
5160}
5161
5162TemplateArgumentLocInfo
5163ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
5164 TemplateArgument::ArgKind Kind,
5165 const RecordData &Record,
5166 unsigned &Index) {
5167 switch (Kind) {
5168 case TemplateArgument::Expression:
5169 return ReadExpr(F);
5170 case TemplateArgument::Type:
5171 return GetTypeSourceInfo(F, Record, Index);
5172 case TemplateArgument::Template: {
5173 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
5174 Index);
5175 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
5176 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
5177 SourceLocation());
5178 }
5179 case TemplateArgument::TemplateExpansion: {
5180 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
5181 Index);
5182 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
5183 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
5184 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
5185 EllipsisLoc);
5186 }
5187 case TemplateArgument::Null:
5188 case TemplateArgument::Integral:
5189 case TemplateArgument::Declaration:
5190 case TemplateArgument::NullPtr:
5191 case TemplateArgument::Pack:
5192 // FIXME: Is this right?
5193 return TemplateArgumentLocInfo();
5194 }
5195 llvm_unreachable("unexpected template argument loc");
5196}
5197
5198TemplateArgumentLoc
5199ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
5200 const RecordData &Record, unsigned &Index) {
5201 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
5202
5203 if (Arg.getKind() == TemplateArgument::Expression) {
5204 if (Record[Index++]) // bool InfoHasSameExpr.
5205 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
5206 }
5207 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
5208 Record, Index));
5209}
5210
5211Decl *ASTReader::GetExternalDecl(uint32_t ID) {
5212 return GetDecl(ID);
5213}
5214
5215uint64_t ASTReader::readCXXBaseSpecifiers(ModuleFile &M, const RecordData &Record,
5216 unsigned &Idx){
5217 if (Idx >= Record.size())
5218 return 0;
5219
5220 unsigned LocalID = Record[Idx++];
5221 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
5222}
5223
5224CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
5225 RecordLocation Loc = getLocalBitOffset(Offset);
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00005226 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005227 SavedStreamPosition SavedPosition(Cursor);
5228 Cursor.JumpToBit(Loc.Offset);
5229 ReadingKindTracker ReadingKind(Read_Decl, *this);
5230 RecordData Record;
5231 unsigned Code = Cursor.ReadCode();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00005232 unsigned RecCode = Cursor.readRecord(Code, Record);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005233 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
5234 Error("Malformed AST file: missing C++ base specifiers");
5235 return 0;
5236 }
5237
5238 unsigned Idx = 0;
5239 unsigned NumBases = Record[Idx++];
5240 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
5241 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
5242 for (unsigned I = 0; I != NumBases; ++I)
5243 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
5244 return Bases;
5245}
5246
5247serialization::DeclID
5248ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
5249 if (LocalID < NUM_PREDEF_DECL_IDS)
5250 return LocalID;
5251
5252 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5253 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
5254 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
5255
5256 return LocalID + I->second;
5257}
5258
5259bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
5260 ModuleFile &M) const {
5261 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(ID);
5262 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5263 return &M == I->second;
5264}
5265
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00005266ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005267 if (!D->isFromASTFile())
5268 return 0;
5269 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
5270 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5271 return I->second;
5272}
5273
5274SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
5275 if (ID < NUM_PREDEF_DECL_IDS)
5276 return SourceLocation();
5277
5278 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
5279
5280 if (Index > DeclsLoaded.size()) {
5281 Error("declaration ID out-of-range for AST file");
5282 return SourceLocation();
5283 }
5284
5285 if (Decl *D = DeclsLoaded[Index])
5286 return D->getLocation();
5287
5288 unsigned RawLocation = 0;
5289 RecordLocation Rec = DeclCursorForID(ID, RawLocation);
5290 return ReadSourceLocation(*Rec.F, RawLocation);
5291}
5292
5293Decl *ASTReader::GetDecl(DeclID ID) {
5294 if (ID < NUM_PREDEF_DECL_IDS) {
5295 switch ((PredefinedDeclIDs)ID) {
5296 case PREDEF_DECL_NULL_ID:
5297 return 0;
5298
5299 case PREDEF_DECL_TRANSLATION_UNIT_ID:
5300 return Context.getTranslationUnitDecl();
5301
5302 case PREDEF_DECL_OBJC_ID_ID:
5303 return Context.getObjCIdDecl();
5304
5305 case PREDEF_DECL_OBJC_SEL_ID:
5306 return Context.getObjCSelDecl();
5307
5308 case PREDEF_DECL_OBJC_CLASS_ID:
5309 return Context.getObjCClassDecl();
5310
5311 case PREDEF_DECL_OBJC_PROTOCOL_ID:
5312 return Context.getObjCProtocolDecl();
5313
5314 case PREDEF_DECL_INT_128_ID:
5315 return Context.getInt128Decl();
5316
5317 case PREDEF_DECL_UNSIGNED_INT_128_ID:
5318 return Context.getUInt128Decl();
5319
5320 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
5321 return Context.getObjCInstanceTypeDecl();
5322
5323 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
5324 return Context.getBuiltinVaListDecl();
5325 }
5326 }
5327
5328 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
5329
5330 if (Index >= DeclsLoaded.size()) {
5331 assert(0 && "declaration ID out-of-range for AST file");
5332 Error("declaration ID out-of-range for AST file");
5333 return 0;
5334 }
5335
5336 if (!DeclsLoaded[Index]) {
5337 ReadDeclRecord(ID);
5338 if (DeserializationListener)
5339 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
5340 }
5341
5342 return DeclsLoaded[Index];
5343}
5344
5345DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
5346 DeclID GlobalID) {
5347 if (GlobalID < NUM_PREDEF_DECL_IDS)
5348 return GlobalID;
5349
5350 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
5351 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5352 ModuleFile *Owner = I->second;
5353
5354 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
5355 = M.GlobalToLocalDeclIDs.find(Owner);
5356 if (Pos == M.GlobalToLocalDeclIDs.end())
5357 return 0;
5358
5359 return GlobalID - Owner->BaseDeclID + Pos->second;
5360}
5361
5362serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
5363 const RecordData &Record,
5364 unsigned &Idx) {
5365 if (Idx >= Record.size()) {
5366 Error("Corrupted AST file");
5367 return 0;
5368 }
5369
5370 return getGlobalDeclID(F, Record[Idx++]);
5371}
5372
5373/// \brief Resolve the offset of a statement into a statement.
5374///
5375/// This operation will read a new statement from the external
5376/// source each time it is called, and is meant to be used via a
5377/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
5378Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
5379 // Switch case IDs are per Decl.
5380 ClearSwitchCaseIDs();
5381
5382 // Offset here is a global offset across the entire chain.
5383 RecordLocation Loc = getLocalBitOffset(Offset);
5384 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
5385 return ReadStmtFromStream(*Loc.F);
5386}
5387
5388namespace {
5389 class FindExternalLexicalDeclsVisitor {
5390 ASTReader &Reader;
5391 const DeclContext *DC;
5392 bool (*isKindWeWant)(Decl::Kind);
5393
5394 SmallVectorImpl<Decl*> &Decls;
5395 bool PredefsVisited[NUM_PREDEF_DECL_IDS];
5396
5397 public:
5398 FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC,
5399 bool (*isKindWeWant)(Decl::Kind),
5400 SmallVectorImpl<Decl*> &Decls)
5401 : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls)
5402 {
5403 for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I)
5404 PredefsVisited[I] = false;
5405 }
5406
5407 static bool visit(ModuleFile &M, bool Preorder, void *UserData) {
5408 if (Preorder)
5409 return false;
5410
5411 FindExternalLexicalDeclsVisitor *This
5412 = static_cast<FindExternalLexicalDeclsVisitor *>(UserData);
5413
5414 ModuleFile::DeclContextInfosMap::iterator Info
5415 = M.DeclContextInfos.find(This->DC);
5416 if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls)
5417 return false;
5418
5419 // Load all of the declaration IDs
5420 for (const KindDeclIDPair *ID = Info->second.LexicalDecls,
5421 *IDE = ID + Info->second.NumLexicalDecls;
5422 ID != IDE; ++ID) {
5423 if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first))
5424 continue;
5425
5426 // Don't add predefined declarations to the lexical context more
5427 // than once.
5428 if (ID->second < NUM_PREDEF_DECL_IDS) {
5429 if (This->PredefsVisited[ID->second])
5430 continue;
5431
5432 This->PredefsVisited[ID->second] = true;
5433 }
5434
5435 if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) {
5436 if (!This->DC->isDeclInLexicalTraversal(D))
5437 This->Decls.push_back(D);
5438 }
5439 }
5440
5441 return false;
5442 }
5443 };
5444}
5445
5446ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
5447 bool (*isKindWeWant)(Decl::Kind),
5448 SmallVectorImpl<Decl*> &Decls) {
5449 // There might be lexical decls in multiple modules, for the TU at
5450 // least. Walk all of the modules in the order they were loaded.
5451 FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls);
5452 ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor);
5453 ++NumLexicalDeclContextsRead;
5454 return ELR_Success;
5455}
5456
5457namespace {
5458
5459class DeclIDComp {
5460 ASTReader &Reader;
5461 ModuleFile &Mod;
5462
5463public:
5464 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
5465
5466 bool operator()(LocalDeclID L, LocalDeclID R) const {
5467 SourceLocation LHS = getLocation(L);
5468 SourceLocation RHS = getLocation(R);
5469 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5470 }
5471
5472 bool operator()(SourceLocation LHS, LocalDeclID R) const {
5473 SourceLocation RHS = getLocation(R);
5474 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5475 }
5476
5477 bool operator()(LocalDeclID L, SourceLocation RHS) const {
5478 SourceLocation LHS = getLocation(L);
5479 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5480 }
5481
5482 SourceLocation getLocation(LocalDeclID ID) const {
5483 return Reader.getSourceManager().getFileLoc(
5484 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
5485 }
5486};
5487
5488}
5489
5490void ASTReader::FindFileRegionDecls(FileID File,
5491 unsigned Offset, unsigned Length,
5492 SmallVectorImpl<Decl *> &Decls) {
5493 SourceManager &SM = getSourceManager();
5494
5495 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
5496 if (I == FileDeclIDs.end())
5497 return;
5498
5499 FileDeclsInfo &DInfo = I->second;
5500 if (DInfo.Decls.empty())
5501 return;
5502
5503 SourceLocation
5504 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
5505 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
5506
5507 DeclIDComp DIDComp(*this, *DInfo.Mod);
5508 ArrayRef<serialization::LocalDeclID>::iterator
5509 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
5510 BeginLoc, DIDComp);
5511 if (BeginIt != DInfo.Decls.begin())
5512 --BeginIt;
5513
5514 // If we are pointing at a top-level decl inside an objc container, we need
5515 // to backtrack until we find it otherwise we will fail to report that the
5516 // region overlaps with an objc container.
5517 while (BeginIt != DInfo.Decls.begin() &&
5518 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
5519 ->isTopLevelDeclInObjCContainer())
5520 --BeginIt;
5521
5522 ArrayRef<serialization::LocalDeclID>::iterator
5523 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
5524 EndLoc, DIDComp);
5525 if (EndIt != DInfo.Decls.end())
5526 ++EndIt;
5527
5528 for (ArrayRef<serialization::LocalDeclID>::iterator
5529 DIt = BeginIt; DIt != EndIt; ++DIt)
5530 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
5531}
5532
5533namespace {
5534 /// \brief ModuleFile visitor used to perform name lookup into a
5535 /// declaration context.
5536 class DeclContextNameLookupVisitor {
5537 ASTReader &Reader;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00005538 SmallVectorImpl<const DeclContext *> &Contexts;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005539 DeclarationName Name;
5540 SmallVectorImpl<NamedDecl *> &Decls;
5541
5542 public:
5543 DeclContextNameLookupVisitor(ASTReader &Reader,
5544 SmallVectorImpl<const DeclContext *> &Contexts,
5545 DeclarationName Name,
5546 SmallVectorImpl<NamedDecl *> &Decls)
5547 : Reader(Reader), Contexts(Contexts), Name(Name), Decls(Decls) { }
5548
5549 static bool visit(ModuleFile &M, void *UserData) {
5550 DeclContextNameLookupVisitor *This
5551 = static_cast<DeclContextNameLookupVisitor *>(UserData);
5552
5553 // Check whether we have any visible declaration information for
5554 // this context in this module.
5555 ModuleFile::DeclContextInfosMap::iterator Info;
5556 bool FoundInfo = false;
5557 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5558 Info = M.DeclContextInfos.find(This->Contexts[I]);
5559 if (Info != M.DeclContextInfos.end() &&
5560 Info->second.NameLookupTableData) {
5561 FoundInfo = true;
5562 break;
5563 }
5564 }
5565
5566 if (!FoundInfo)
5567 return false;
5568
5569 // Look for this name within this module.
5570 ASTDeclContextNameLookupTable *LookupTable =
5571 Info->second.NameLookupTableData;
5572 ASTDeclContextNameLookupTable::iterator Pos
5573 = LookupTable->find(This->Name);
5574 if (Pos == LookupTable->end())
5575 return false;
5576
5577 bool FoundAnything = false;
5578 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
5579 for (; Data.first != Data.second; ++Data.first) {
5580 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first);
5581 if (!ND)
5582 continue;
5583
5584 if (ND->getDeclName() != This->Name) {
5585 // A name might be null because the decl's redeclarable part is
5586 // currently read before reading its name. The lookup is triggered by
5587 // building that decl (likely indirectly), and so it is later in the
5588 // sense of "already existing" and can be ignored here.
5589 continue;
5590 }
5591
5592 // Record this declaration.
5593 FoundAnything = true;
5594 This->Decls.push_back(ND);
5595 }
5596
5597 return FoundAnything;
5598 }
5599 };
5600}
5601
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00005602/// \brief Retrieve the "definitive" module file for the definition of the
5603/// given declaration context, if there is one.
5604///
5605/// The "definitive" module file is the only place where we need to look to
5606/// find information about the declarations within the given declaration
5607/// context. For example, C++ and Objective-C classes, C structs/unions, and
5608/// Objective-C protocols, categories, and extensions are all defined in a
5609/// single place in the source code, so they have definitive module files
5610/// associated with them. C++ namespaces, on the other hand, can have
5611/// definitions in multiple different module files.
5612///
5613/// Note: this needs to be kept in sync with ASTWriter::AddedVisibleDecl's
5614/// NDEBUG checking.
5615static ModuleFile *getDefinitiveModuleFileFor(const DeclContext *DC,
5616 ASTReader &Reader) {
Douglas Gregore0d20662013-01-22 17:08:30 +00005617 if (const DeclContext *DefDC = getDefinitiveDeclContext(DC))
5618 return Reader.getOwningModuleFile(cast<Decl>(DefDC));
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00005619
5620 return 0;
5621}
5622
Richard Smith3646c682013-02-07 03:30:24 +00005623bool
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005624ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
5625 DeclarationName Name) {
5626 assert(DC->hasExternalVisibleStorage() &&
5627 "DeclContext has no visible decls in storage");
5628 if (!Name)
Richard Smith3646c682013-02-07 03:30:24 +00005629 return false;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005630
5631 SmallVector<NamedDecl *, 64> Decls;
5632
5633 // Compute the declaration contexts we need to look into. Multiple such
5634 // declaration contexts occur when two declaration contexts from disjoint
5635 // modules get merged, e.g., when two namespaces with the same name are
5636 // independently defined in separate modules.
5637 SmallVector<const DeclContext *, 2> Contexts;
5638 Contexts.push_back(DC);
5639
5640 if (DC->isNamespace()) {
5641 MergedDeclsMap::iterator Merged
5642 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5643 if (Merged != MergedDecls.end()) {
5644 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5645 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5646 }
5647 }
5648
5649 DeclContextNameLookupVisitor Visitor(*this, Contexts, Name, Decls);
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00005650
5651 // If we can definitively determine which module file to look into,
5652 // only look there. Otherwise, look in all module files.
5653 ModuleFile *Definitive;
5654 if (Contexts.size() == 1 &&
5655 (Definitive = getDefinitiveModuleFileFor(DC, *this))) {
5656 DeclContextNameLookupVisitor::visit(*Definitive, &Visitor);
5657 } else {
5658 ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor);
5659 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005660 ++NumVisibleDeclContextsRead;
5661 SetExternalVisibleDeclsForName(DC, Name, Decls);
Richard Smith3646c682013-02-07 03:30:24 +00005662 return !Decls.empty();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005663}
5664
5665namespace {
5666 /// \brief ModuleFile visitor used to retrieve all visible names in a
5667 /// declaration context.
5668 class DeclContextAllNamesVisitor {
5669 ASTReader &Reader;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00005670 SmallVectorImpl<const DeclContext *> &Contexts;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005671 llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> > &Decls;
Argyrios Kyrtzidisca40f302012-12-19 22:21:18 +00005672 bool VisitAll;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005673
5674 public:
5675 DeclContextAllNamesVisitor(ASTReader &Reader,
5676 SmallVectorImpl<const DeclContext *> &Contexts,
5677 llvm::DenseMap<DeclarationName,
Argyrios Kyrtzidisca40f302012-12-19 22:21:18 +00005678 SmallVector<NamedDecl *, 8> > &Decls,
5679 bool VisitAll)
5680 : Reader(Reader), Contexts(Contexts), Decls(Decls), VisitAll(VisitAll) { }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005681
5682 static bool visit(ModuleFile &M, void *UserData) {
5683 DeclContextAllNamesVisitor *This
5684 = static_cast<DeclContextAllNamesVisitor *>(UserData);
5685
5686 // Check whether we have any visible declaration information for
5687 // this context in this module.
5688 ModuleFile::DeclContextInfosMap::iterator Info;
5689 bool FoundInfo = false;
5690 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5691 Info = M.DeclContextInfos.find(This->Contexts[I]);
5692 if (Info != M.DeclContextInfos.end() &&
5693 Info->second.NameLookupTableData) {
5694 FoundInfo = true;
5695 break;
5696 }
5697 }
5698
5699 if (!FoundInfo)
5700 return false;
5701
5702 ASTDeclContextNameLookupTable *LookupTable =
5703 Info->second.NameLookupTableData;
5704 bool FoundAnything = false;
5705 for (ASTDeclContextNameLookupTable::data_iterator
Douglas Gregora6b00fc2013-01-23 22:38:11 +00005706 I = LookupTable->data_begin(), E = LookupTable->data_end();
5707 I != E;
5708 ++I) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005709 ASTDeclContextNameLookupTrait::data_type Data = *I;
5710 for (; Data.first != Data.second; ++Data.first) {
5711 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M,
5712 *Data.first);
5713 if (!ND)
5714 continue;
5715
5716 // Record this declaration.
5717 FoundAnything = true;
5718 This->Decls[ND->getDeclName()].push_back(ND);
5719 }
5720 }
5721
Argyrios Kyrtzidisca40f302012-12-19 22:21:18 +00005722 return FoundAnything && !This->VisitAll;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005723 }
5724 };
5725}
5726
5727void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
5728 if (!DC->hasExternalVisibleStorage())
5729 return;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00005730 llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> > Decls;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005731
5732 // Compute the declaration contexts we need to look into. Multiple such
5733 // declaration contexts occur when two declaration contexts from disjoint
5734 // modules get merged, e.g., when two namespaces with the same name are
5735 // independently defined in separate modules.
5736 SmallVector<const DeclContext *, 2> Contexts;
5737 Contexts.push_back(DC);
5738
5739 if (DC->isNamespace()) {
5740 MergedDeclsMap::iterator Merged
5741 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5742 if (Merged != MergedDecls.end()) {
5743 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5744 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5745 }
5746 }
5747
Argyrios Kyrtzidisca40f302012-12-19 22:21:18 +00005748 DeclContextAllNamesVisitor Visitor(*this, Contexts, Decls,
5749 /*VisitAll=*/DC->isFileContext());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005750 ModuleMgr.visit(&DeclContextAllNamesVisitor::visit, &Visitor);
5751 ++NumVisibleDeclContextsRead;
5752
5753 for (llvm::DenseMap<DeclarationName,
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00005754 SmallVector<NamedDecl *, 8> >::iterator
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005755 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
5756 SetExternalVisibleDeclsForName(DC, I->first, I->second);
5757 }
5758 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
5759}
5760
5761/// \brief Under non-PCH compilation the consumer receives the objc methods
5762/// before receiving the implementation, and codegen depends on this.
5763/// We simulate this by deserializing and passing to consumer the methods of the
5764/// implementation before passing the deserialized implementation decl.
5765static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
5766 ASTConsumer *Consumer) {
5767 assert(ImplD && Consumer);
5768
5769 for (ObjCImplDecl::method_iterator
5770 I = ImplD->meth_begin(), E = ImplD->meth_end(); I != E; ++I)
5771 Consumer->HandleInterestingDecl(DeclGroupRef(*I));
5772
5773 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
5774}
5775
5776void ASTReader::PassInterestingDeclsToConsumer() {
5777 assert(Consumer);
5778 while (!InterestingDecls.empty()) {
5779 Decl *D = InterestingDecls.front();
5780 InterestingDecls.pop_front();
5781
5782 PassInterestingDeclToConsumer(D);
5783 }
5784}
5785
5786void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
5787 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
5788 PassObjCImplDeclToConsumer(ImplD, Consumer);
5789 else
5790 Consumer->HandleInterestingDecl(DeclGroupRef(D));
5791}
5792
5793void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
5794 this->Consumer = Consumer;
5795
5796 if (!Consumer)
5797 return;
5798
5799 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
5800 // Force deserialization of this decl, which will cause it to be queued for
5801 // passing to the consumer.
5802 GetDecl(ExternalDefinitions[I]);
5803 }
5804 ExternalDefinitions.clear();
5805
5806 PassInterestingDeclsToConsumer();
5807}
5808
5809void ASTReader::PrintStats() {
5810 std::fprintf(stderr, "*** AST File Statistics:\n");
5811
5812 unsigned NumTypesLoaded
5813 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
5814 QualType());
5815 unsigned NumDeclsLoaded
5816 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
5817 (Decl *)0);
5818 unsigned NumIdentifiersLoaded
5819 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
5820 IdentifiersLoaded.end(),
5821 (IdentifierInfo *)0);
5822 unsigned NumMacrosLoaded
5823 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
5824 MacrosLoaded.end(),
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00005825 (MacroInfo *)0);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005826 unsigned NumSelectorsLoaded
5827 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
5828 SelectorsLoaded.end(),
5829 Selector());
5830
5831 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
5832 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
5833 NumSLocEntriesRead, TotalNumSLocEntries,
5834 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
5835 if (!TypesLoaded.empty())
5836 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
5837 NumTypesLoaded, (unsigned)TypesLoaded.size(),
5838 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
5839 if (!DeclsLoaded.empty())
5840 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
5841 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
5842 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
5843 if (!IdentifiersLoaded.empty())
5844 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
5845 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
5846 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
5847 if (!MacrosLoaded.empty())
5848 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
5849 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
5850 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
5851 if (!SelectorsLoaded.empty())
5852 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
5853 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
5854 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
5855 if (TotalNumStatements)
5856 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
5857 NumStatementsRead, TotalNumStatements,
5858 ((float)NumStatementsRead/TotalNumStatements * 100));
5859 if (TotalNumMacros)
5860 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
5861 NumMacrosRead, TotalNumMacros,
5862 ((float)NumMacrosRead/TotalNumMacros * 100));
5863 if (TotalLexicalDeclContexts)
5864 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
5865 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
5866 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
5867 * 100));
5868 if (TotalVisibleDeclContexts)
5869 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
5870 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
5871 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
5872 * 100));
5873 if (TotalNumMethodPoolEntries) {
5874 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
5875 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
5876 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
5877 * 100));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005878 }
Douglas Gregor95fb36e2013-01-28 17:54:36 +00005879 if (NumMethodPoolLookups) {
5880 std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n",
5881 NumMethodPoolHits, NumMethodPoolLookups,
5882 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
5883 }
5884 if (NumMethodPoolTableLookups) {
5885 std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n",
5886 NumMethodPoolTableHits, NumMethodPoolTableLookups,
5887 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
5888 * 100.0));
5889 }
5890
Douglas Gregore1698072013-01-25 00:38:33 +00005891 if (NumIdentifierLookupHits) {
5892 std::fprintf(stderr,
5893 " %u / %u identifier table lookups succeeded (%f%%)\n",
5894 NumIdentifierLookupHits, NumIdentifierLookups,
5895 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
5896 }
5897
Douglas Gregor1a49d972013-01-25 01:03:03 +00005898 if (GlobalIndex) {
5899 std::fprintf(stderr, "\n");
5900 GlobalIndex->printStats();
5901 }
5902
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005903 std::fprintf(stderr, "\n");
5904 dump();
5905 std::fprintf(stderr, "\n");
5906}
5907
5908template<typename Key, typename ModuleFile, unsigned InitialCapacity>
5909static void
5910dumpModuleIDMap(StringRef Name,
5911 const ContinuousRangeMap<Key, ModuleFile *,
5912 InitialCapacity> &Map) {
5913 if (Map.begin() == Map.end())
5914 return;
5915
5916 typedef ContinuousRangeMap<Key, ModuleFile *, InitialCapacity> MapType;
5917 llvm::errs() << Name << ":\n";
5918 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
5919 I != IEnd; ++I) {
5920 llvm::errs() << " " << I->first << " -> " << I->second->FileName
5921 << "\n";
5922 }
5923}
5924
5925void ASTReader::dump() {
5926 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
5927 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
5928 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
5929 dumpModuleIDMap("Global type map", GlobalTypeMap);
5930 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
5931 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
5932 dumpModuleIDMap("Global macro map", GlobalMacroMap);
5933 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
5934 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
5935 dumpModuleIDMap("Global preprocessed entity map",
5936 GlobalPreprocessedEntityMap);
5937
5938 llvm::errs() << "\n*** PCH/Modules Loaded:";
5939 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
5940 MEnd = ModuleMgr.end();
5941 M != MEnd; ++M)
5942 (*M)->dump();
5943}
5944
5945/// Return the amount of memory used by memory buffers, breaking down
5946/// by heap-backed versus mmap'ed memory.
5947void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
5948 for (ModuleConstIterator I = ModuleMgr.begin(),
5949 E = ModuleMgr.end(); I != E; ++I) {
5950 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
5951 size_t bytes = buf->getBufferSize();
5952 switch (buf->getBufferKind()) {
5953 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
5954 sizes.malloc_bytes += bytes;
5955 break;
5956 case llvm::MemoryBuffer::MemoryBuffer_MMap:
5957 sizes.mmap_bytes += bytes;
5958 break;
5959 }
5960 }
5961 }
5962}
5963
5964void ASTReader::InitializeSema(Sema &S) {
5965 SemaObj = &S;
5966 S.addExternalSource(this);
5967
5968 // Makes sure any declarations that were deserialized "too early"
5969 // still get added to the identifier's declaration chains.
5970 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
Douglas Gregoraa945902013-02-18 15:53:43 +00005971 NamedDecl *ND = cast<NamedDecl>(PreloadedDecls[I]->getMostRecentDecl());
5972 SemaObj->pushExternalDeclIntoScope(ND, PreloadedDecls[I]->getDeclName());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005973 }
5974 PreloadedDecls.clear();
5975
5976 // Load the offsets of the declarations that Sema references.
5977 // They will be lazily deserialized when needed.
5978 if (!SemaDeclRefs.empty()) {
5979 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
5980 if (!SemaObj->StdNamespace)
5981 SemaObj->StdNamespace = SemaDeclRefs[0];
5982 if (!SemaObj->StdBadAlloc)
5983 SemaObj->StdBadAlloc = SemaDeclRefs[1];
5984 }
5985
5986 if (!FPPragmaOptions.empty()) {
5987 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
5988 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
5989 }
5990
5991 if (!OpenCLExtensions.empty()) {
5992 unsigned I = 0;
5993#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
5994#include "clang/Basic/OpenCLExtensions.def"
5995
5996 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
5997 }
5998}
5999
6000IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
6001 // Note that we are loading an identifier.
6002 Deserializing AnIdentifier(this);
Douglas Gregor1a49d972013-01-25 01:03:03 +00006003 StringRef Name(NameStart, NameEnd - NameStart);
6004
6005 // If there is a global index, look there first to determine which modules
6006 // provably do not have any results for this identifier.
Douglas Gregor188bdcd2013-01-25 23:32:03 +00006007 GlobalModuleIndex::HitSet Hits;
6008 GlobalModuleIndex::HitSet *HitsPtr = 0;
Douglas Gregor1a49d972013-01-25 01:03:03 +00006009 if (!loadGlobalIndex()) {
Douglas Gregor188bdcd2013-01-25 23:32:03 +00006010 if (GlobalIndex->lookupIdentifier(Name, Hits)) {
6011 HitsPtr = &Hits;
Douglas Gregor1a49d972013-01-25 01:03:03 +00006012 }
6013 }
Douglas Gregor188bdcd2013-01-25 23:32:03 +00006014 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
Douglas Gregore1698072013-01-25 00:38:33 +00006015 NumIdentifierLookups,
6016 NumIdentifierLookupHits);
Douglas Gregor188bdcd2013-01-25 23:32:03 +00006017 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor, HitsPtr);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006018 IdentifierInfo *II = Visitor.getIdentifierInfo();
6019 markIdentifierUpToDate(II);
6020 return II;
6021}
6022
6023namespace clang {
6024 /// \brief An identifier-lookup iterator that enumerates all of the
6025 /// identifiers stored within a set of AST files.
6026 class ASTIdentifierIterator : public IdentifierIterator {
6027 /// \brief The AST reader whose identifiers are being enumerated.
6028 const ASTReader &Reader;
6029
6030 /// \brief The current index into the chain of AST files stored in
6031 /// the AST reader.
6032 unsigned Index;
6033
6034 /// \brief The current position within the identifier lookup table
6035 /// of the current AST file.
6036 ASTIdentifierLookupTable::key_iterator Current;
6037
6038 /// \brief The end position within the identifier lookup table of
6039 /// the current AST file.
6040 ASTIdentifierLookupTable::key_iterator End;
6041
6042 public:
6043 explicit ASTIdentifierIterator(const ASTReader &Reader);
6044
6045 virtual StringRef Next();
6046 };
6047}
6048
6049ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
6050 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
6051 ASTIdentifierLookupTable *IdTable
6052 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
6053 Current = IdTable->key_begin();
6054 End = IdTable->key_end();
6055}
6056
6057StringRef ASTIdentifierIterator::Next() {
6058 while (Current == End) {
6059 // If we have exhausted all of our AST files, we're done.
6060 if (Index == 0)
6061 return StringRef();
6062
6063 --Index;
6064 ASTIdentifierLookupTable *IdTable
6065 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
6066 IdentifierLookupTable;
6067 Current = IdTable->key_begin();
6068 End = IdTable->key_end();
6069 }
6070
6071 // We have any identifiers remaining in the current AST file; return
6072 // the next one.
Douglas Gregor479633c2013-01-23 18:53:14 +00006073 StringRef Result = *Current;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006074 ++Current;
Douglas Gregor479633c2013-01-23 18:53:14 +00006075 return Result;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006076}
6077
6078IdentifierIterator *ASTReader::getIdentifiers() const {
6079 return new ASTIdentifierIterator(*this);
6080}
6081
6082namespace clang { namespace serialization {
6083 class ReadMethodPoolVisitor {
6084 ASTReader &Reader;
6085 Selector Sel;
6086 unsigned PriorGeneration;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00006087 SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
6088 SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006089
6090 public:
6091 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
6092 unsigned PriorGeneration)
6093 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) { }
6094
6095 static bool visit(ModuleFile &M, void *UserData) {
6096 ReadMethodPoolVisitor *This
6097 = static_cast<ReadMethodPoolVisitor *>(UserData);
6098
6099 if (!M.SelectorLookupTable)
6100 return false;
6101
6102 // If we've already searched this module file, skip it now.
6103 if (M.Generation <= This->PriorGeneration)
6104 return true;
6105
Douglas Gregor95fb36e2013-01-28 17:54:36 +00006106 ++This->Reader.NumMethodPoolTableLookups;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006107 ASTSelectorLookupTable *PoolTable
6108 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
6109 ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel);
6110 if (Pos == PoolTable->end())
6111 return false;
Douglas Gregor95fb36e2013-01-28 17:54:36 +00006112
6113 ++This->Reader.NumMethodPoolTableHits;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006114 ++This->Reader.NumSelectorsRead;
6115 // FIXME: Not quite happy with the statistics here. We probably should
6116 // disable this tracking when called via LoadSelector.
6117 // Also, should entries without methods count as misses?
6118 ++This->Reader.NumMethodPoolEntriesRead;
6119 ASTSelectorLookupTrait::data_type Data = *Pos;
6120 if (This->Reader.DeserializationListener)
6121 This->Reader.DeserializationListener->SelectorRead(Data.ID,
6122 This->Sel);
6123
6124 This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
6125 This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
6126 return true;
6127 }
6128
6129 /// \brief Retrieve the instance methods found by this visitor.
6130 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
6131 return InstanceMethods;
6132 }
6133
6134 /// \brief Retrieve the instance methods found by this visitor.
6135 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
6136 return FactoryMethods;
6137 }
6138 };
6139} } // end namespace clang::serialization
6140
6141/// \brief Add the given set of methods to the method list.
6142static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
6143 ObjCMethodList &List) {
6144 for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
6145 S.addMethodToGlobalList(&List, Methods[I]);
6146 }
6147}
6148
6149void ASTReader::ReadMethodPool(Selector Sel) {
6150 // Get the selector generation and update it to the current generation.
6151 unsigned &Generation = SelectorGeneration[Sel];
6152 unsigned PriorGeneration = Generation;
6153 Generation = CurrentGeneration;
6154
6155 // Search for methods defined with this selector.
Douglas Gregor95fb36e2013-01-28 17:54:36 +00006156 ++NumMethodPoolLookups;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006157 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
6158 ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor);
6159
6160 if (Visitor.getInstanceMethods().empty() &&
Douglas Gregor95fb36e2013-01-28 17:54:36 +00006161 Visitor.getFactoryMethods().empty())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006162 return;
Douglas Gregor95fb36e2013-01-28 17:54:36 +00006163
6164 ++NumMethodPoolHits;
6165
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006166 if (!getSema())
6167 return;
6168
6169 Sema &S = *getSema();
6170 Sema::GlobalMethodPool::iterator Pos
6171 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
6172
6173 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
6174 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
6175}
6176
6177void ASTReader::ReadKnownNamespaces(
6178 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
6179 Namespaces.clear();
6180
6181 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
6182 if (NamespaceDecl *Namespace
6183 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
6184 Namespaces.push_back(Namespace);
6185 }
6186}
6187
Nick Lewyckycd0655b2013-02-01 08:13:20 +00006188void ASTReader::ReadUndefinedButUsed(
Nick Lewycky995e26b2013-01-31 03:23:57 +00006189 llvm::DenseMap<NamedDecl*, SourceLocation> &Undefined) {
Nick Lewyckycd0655b2013-02-01 08:13:20 +00006190 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
6191 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
Nick Lewycky01a41142013-01-26 00:35:08 +00006192 SourceLocation Loc =
Nick Lewyckycd0655b2013-02-01 08:13:20 +00006193 SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
Nick Lewycky01a41142013-01-26 00:35:08 +00006194 Undefined.insert(std::make_pair(D, Loc));
6195 }
6196}
Nick Lewycky01a41142013-01-26 00:35:08 +00006197
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006198void ASTReader::ReadTentativeDefinitions(
6199 SmallVectorImpl<VarDecl *> &TentativeDefs) {
6200 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
6201 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
6202 if (Var)
6203 TentativeDefs.push_back(Var);
6204 }
6205 TentativeDefinitions.clear();
6206}
6207
6208void ASTReader::ReadUnusedFileScopedDecls(
6209 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
6210 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
6211 DeclaratorDecl *D
6212 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
6213 if (D)
6214 Decls.push_back(D);
6215 }
6216 UnusedFileScopedDecls.clear();
6217}
6218
6219void ASTReader::ReadDelegatingConstructors(
6220 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
6221 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
6222 CXXConstructorDecl *D
6223 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
6224 if (D)
6225 Decls.push_back(D);
6226 }
6227 DelegatingCtorDecls.clear();
6228}
6229
6230void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
6231 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
6232 TypedefNameDecl *D
6233 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
6234 if (D)
6235 Decls.push_back(D);
6236 }
6237 ExtVectorDecls.clear();
6238}
6239
6240void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) {
6241 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
6242 CXXRecordDecl *D
6243 = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I]));
6244 if (D)
6245 Decls.push_back(D);
6246 }
6247 DynamicClasses.clear();
6248}
6249
6250void
Richard Smith5ea6ef42013-01-10 23:43:47 +00006251ASTReader::ReadLocallyScopedExternCDecls(SmallVectorImpl<NamedDecl *> &Decls) {
6252 for (unsigned I = 0, N = LocallyScopedExternCDecls.size(); I != N; ++I) {
6253 NamedDecl *D
6254 = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternCDecls[I]));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006255 if (D)
6256 Decls.push_back(D);
6257 }
Richard Smith5ea6ef42013-01-10 23:43:47 +00006258 LocallyScopedExternCDecls.clear();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006259}
6260
6261void ASTReader::ReadReferencedSelectors(
6262 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
6263 if (ReferencedSelectorsData.empty())
6264 return;
6265
6266 // If there are @selector references added them to its pool. This is for
6267 // implementation of -Wselector.
6268 unsigned int DataSize = ReferencedSelectorsData.size()-1;
6269 unsigned I = 0;
6270 while (I < DataSize) {
6271 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
6272 SourceLocation SelLoc
6273 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
6274 Sels.push_back(std::make_pair(Sel, SelLoc));
6275 }
6276 ReferencedSelectorsData.clear();
6277}
6278
6279void ASTReader::ReadWeakUndeclaredIdentifiers(
6280 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
6281 if (WeakUndeclaredIdentifiers.empty())
6282 return;
6283
6284 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
6285 IdentifierInfo *WeakId
6286 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
6287 IdentifierInfo *AliasId
6288 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
6289 SourceLocation Loc
6290 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
6291 bool Used = WeakUndeclaredIdentifiers[I++];
6292 WeakInfo WI(AliasId, Loc);
6293 WI.setUsed(Used);
6294 WeakIDs.push_back(std::make_pair(WeakId, WI));
6295 }
6296 WeakUndeclaredIdentifiers.clear();
6297}
6298
6299void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
6300 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
6301 ExternalVTableUse VT;
6302 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
6303 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
6304 VT.DefinitionRequired = VTableUses[Idx++];
6305 VTables.push_back(VT);
6306 }
6307
6308 VTableUses.clear();
6309}
6310
6311void ASTReader::ReadPendingInstantiations(
6312 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
6313 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
6314 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
6315 SourceLocation Loc
6316 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
6317
6318 Pending.push_back(std::make_pair(D, Loc));
6319 }
6320 PendingInstantiations.clear();
6321}
6322
6323void ASTReader::LoadSelector(Selector Sel) {
6324 // It would be complicated to avoid reading the methods anyway. So don't.
6325 ReadMethodPool(Sel);
6326}
6327
6328void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
6329 assert(ID && "Non-zero identifier ID required");
6330 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
6331 IdentifiersLoaded[ID - 1] = II;
6332 if (DeserializationListener)
6333 DeserializationListener->IdentifierRead(ID, II);
6334}
6335
6336/// \brief Set the globally-visible declarations associated with the given
6337/// identifier.
6338///
6339/// If the AST reader is currently in a state where the given declaration IDs
6340/// cannot safely be resolved, they are queued until it is safe to resolve
6341/// them.
6342///
6343/// \param II an IdentifierInfo that refers to one or more globally-visible
6344/// declarations.
6345///
6346/// \param DeclIDs the set of declaration IDs with the name @p II that are
6347/// visible at global scope.
6348///
Douglas Gregoraa945902013-02-18 15:53:43 +00006349/// \param Decls if non-null, this vector will be populated with the set of
6350/// deserialized declarations. These declarations will not be pushed into
6351/// scope.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006352void
6353ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
6354 const SmallVectorImpl<uint32_t> &DeclIDs,
Douglas Gregoraa945902013-02-18 15:53:43 +00006355 SmallVectorImpl<Decl *> *Decls) {
6356 if (NumCurrentElementsDeserializing && !Decls) {
6357 PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006358 return;
6359 }
6360
6361 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
6362 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
6363 if (SemaObj) {
Douglas Gregoraa945902013-02-18 15:53:43 +00006364 // If we're simply supposed to record the declarations, do so now.
6365 if (Decls) {
6366 Decls->push_back(D);
6367 continue;
6368 }
6369
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006370 // Introduce this declaration into the translation-unit scope
6371 // and add it to the declaration chain for this identifier, so
6372 // that (unqualified) name lookup will find it.
Douglas Gregoraa945902013-02-18 15:53:43 +00006373 NamedDecl *ND = cast<NamedDecl>(D->getMostRecentDecl());
6374 SemaObj->pushExternalDeclIntoScope(ND, II);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006375 } else {
6376 // Queue this declaration so that it will be added to the
6377 // translation unit scope and identifier's declaration chain
6378 // once a Sema object is known.
6379 PreloadedDecls.push_back(D);
6380 }
6381 }
6382}
6383
Douglas Gregor8222b892013-01-21 16:52:34 +00006384IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006385 if (ID == 0)
6386 return 0;
6387
6388 if (IdentifiersLoaded.empty()) {
6389 Error("no identifier table in AST file");
6390 return 0;
6391 }
6392
6393 ID -= 1;
6394 if (!IdentifiersLoaded[ID]) {
6395 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
6396 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
6397 ModuleFile *M = I->second;
6398 unsigned Index = ID - M->BaseIdentifierID;
6399 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
6400
6401 // All of the strings in the AST file are preceded by a 16-bit length.
6402 // Extract that 16-bit length to avoid having to execute strlen().
6403 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
6404 // unsigned integers. This is important to avoid integer overflow when
6405 // we cast them to 'unsigned'.
6406 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
6407 unsigned StrLen = (((unsigned) StrLenPtr[0])
6408 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Douglas Gregor8222b892013-01-21 16:52:34 +00006409 IdentifiersLoaded[ID]
6410 = &PP.getIdentifierTable().get(StringRef(Str, StrLen));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006411 if (DeserializationListener)
6412 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
6413 }
6414
6415 return IdentifiersLoaded[ID];
6416}
6417
Douglas Gregor8222b892013-01-21 16:52:34 +00006418IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
6419 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006420}
6421
6422IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
6423 if (LocalID < NUM_PREDEF_IDENT_IDS)
6424 return LocalID;
6425
6426 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6427 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
6428 assert(I != M.IdentifierRemap.end()
6429 && "Invalid index into identifier index remap");
6430
6431 return LocalID + I->second;
6432}
6433
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00006434MacroInfo *ASTReader::getMacro(MacroID ID) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006435 if (ID == 0)
6436 return 0;
6437
6438 if (MacrosLoaded.empty()) {
6439 Error("no macro table in AST file");
6440 return 0;
6441 }
6442
6443 ID -= NUM_PREDEF_MACRO_IDS;
6444 if (!MacrosLoaded[ID]) {
6445 GlobalMacroMapType::iterator I
6446 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
6447 assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
6448 ModuleFile *M = I->second;
6449 unsigned Index = ID - M->BaseMacroID;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00006450 MacrosLoaded[ID] = ReadMacroRecord(*M, M->MacroOffsets[Index]);
6451
6452 if (DeserializationListener)
6453 DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
6454 MacrosLoaded[ID]);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006455 }
6456
6457 return MacrosLoaded[ID];
6458}
6459
6460MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
6461 if (LocalID < NUM_PREDEF_MACRO_IDS)
6462 return LocalID;
6463
6464 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6465 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
6466 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
6467
6468 return LocalID + I->second;
6469}
6470
6471serialization::SubmoduleID
6472ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
6473 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
6474 return LocalID;
6475
6476 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6477 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
6478 assert(I != M.SubmoduleRemap.end()
6479 && "Invalid index into submodule index remap");
6480
6481 return LocalID + I->second;
6482}
6483
6484Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
6485 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
6486 assert(GlobalID == 0 && "Unhandled global submodule ID");
6487 return 0;
6488 }
6489
6490 if (GlobalID > SubmodulesLoaded.size()) {
6491 Error("submodule ID out of range in AST file");
6492 return 0;
6493 }
6494
6495 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
6496}
Douglas Gregorca2ab452013-01-12 01:29:50 +00006497
6498Module *ASTReader::getModule(unsigned ID) {
6499 return getSubmodule(ID);
6500}
6501
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006502Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
6503 return DecodeSelector(getGlobalSelectorID(M, LocalID));
6504}
6505
6506Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
6507 if (ID == 0)
6508 return Selector();
6509
6510 if (ID > SelectorsLoaded.size()) {
6511 Error("selector ID out of range in AST file");
6512 return Selector();
6513 }
6514
6515 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
6516 // Load this selector from the selector table.
6517 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
6518 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
6519 ModuleFile &M = *I->second;
6520 ASTSelectorLookupTrait Trait(*this, M);
6521 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
6522 SelectorsLoaded[ID - 1] =
6523 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
6524 if (DeserializationListener)
6525 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
6526 }
6527
6528 return SelectorsLoaded[ID - 1];
6529}
6530
6531Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
6532 return DecodeSelector(ID);
6533}
6534
6535uint32_t ASTReader::GetNumExternalSelectors() {
6536 // ID 0 (the null selector) is considered an external selector.
6537 return getTotalNumSelectors() + 1;
6538}
6539
6540serialization::SelectorID
6541ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
6542 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
6543 return LocalID;
6544
6545 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6546 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
6547 assert(I != M.SelectorRemap.end()
6548 && "Invalid index into selector index remap");
6549
6550 return LocalID + I->second;
6551}
6552
6553DeclarationName
6554ASTReader::ReadDeclarationName(ModuleFile &F,
6555 const RecordData &Record, unsigned &Idx) {
6556 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
6557 switch (Kind) {
6558 case DeclarationName::Identifier:
Douglas Gregor8222b892013-01-21 16:52:34 +00006559 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006560
6561 case DeclarationName::ObjCZeroArgSelector:
6562 case DeclarationName::ObjCOneArgSelector:
6563 case DeclarationName::ObjCMultiArgSelector:
6564 return DeclarationName(ReadSelector(F, Record, Idx));
6565
6566 case DeclarationName::CXXConstructorName:
6567 return Context.DeclarationNames.getCXXConstructorName(
6568 Context.getCanonicalType(readType(F, Record, Idx)));
6569
6570 case DeclarationName::CXXDestructorName:
6571 return Context.DeclarationNames.getCXXDestructorName(
6572 Context.getCanonicalType(readType(F, Record, Idx)));
6573
6574 case DeclarationName::CXXConversionFunctionName:
6575 return Context.DeclarationNames.getCXXConversionFunctionName(
6576 Context.getCanonicalType(readType(F, Record, Idx)));
6577
6578 case DeclarationName::CXXOperatorName:
6579 return Context.DeclarationNames.getCXXOperatorName(
6580 (OverloadedOperatorKind)Record[Idx++]);
6581
6582 case DeclarationName::CXXLiteralOperatorName:
6583 return Context.DeclarationNames.getCXXLiteralOperatorName(
6584 GetIdentifierInfo(F, Record, Idx));
6585
6586 case DeclarationName::CXXUsingDirective:
6587 return DeclarationName::getUsingDirectiveName();
6588 }
6589
6590 llvm_unreachable("Invalid NameKind!");
6591}
6592
6593void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
6594 DeclarationNameLoc &DNLoc,
6595 DeclarationName Name,
6596 const RecordData &Record, unsigned &Idx) {
6597 switch (Name.getNameKind()) {
6598 case DeclarationName::CXXConstructorName:
6599 case DeclarationName::CXXDestructorName:
6600 case DeclarationName::CXXConversionFunctionName:
6601 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
6602 break;
6603
6604 case DeclarationName::CXXOperatorName:
6605 DNLoc.CXXOperatorName.BeginOpNameLoc
6606 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6607 DNLoc.CXXOperatorName.EndOpNameLoc
6608 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6609 break;
6610
6611 case DeclarationName::CXXLiteralOperatorName:
6612 DNLoc.CXXLiteralOperatorName.OpNameLoc
6613 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6614 break;
6615
6616 case DeclarationName::Identifier:
6617 case DeclarationName::ObjCZeroArgSelector:
6618 case DeclarationName::ObjCOneArgSelector:
6619 case DeclarationName::ObjCMultiArgSelector:
6620 case DeclarationName::CXXUsingDirective:
6621 break;
6622 }
6623}
6624
6625void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
6626 DeclarationNameInfo &NameInfo,
6627 const RecordData &Record, unsigned &Idx) {
6628 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
6629 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
6630 DeclarationNameLoc DNLoc;
6631 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
6632 NameInfo.setInfo(DNLoc);
6633}
6634
6635void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
6636 const RecordData &Record, unsigned &Idx) {
6637 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
6638 unsigned NumTPLists = Record[Idx++];
6639 Info.NumTemplParamLists = NumTPLists;
6640 if (NumTPLists) {
6641 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
6642 for (unsigned i=0; i != NumTPLists; ++i)
6643 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
6644 }
6645}
6646
6647TemplateName
6648ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
6649 unsigned &Idx) {
6650 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
6651 switch (Kind) {
6652 case TemplateName::Template:
6653 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
6654
6655 case TemplateName::OverloadedTemplate: {
6656 unsigned size = Record[Idx++];
6657 UnresolvedSet<8> Decls;
6658 while (size--)
6659 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
6660
6661 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
6662 }
6663
6664 case TemplateName::QualifiedTemplate: {
6665 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
6666 bool hasTemplKeyword = Record[Idx++];
6667 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
6668 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
6669 }
6670
6671 case TemplateName::DependentTemplate: {
6672 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
6673 if (Record[Idx++]) // isIdentifier
6674 return Context.getDependentTemplateName(NNS,
6675 GetIdentifierInfo(F, Record,
6676 Idx));
6677 return Context.getDependentTemplateName(NNS,
6678 (OverloadedOperatorKind)Record[Idx++]);
6679 }
6680
6681 case TemplateName::SubstTemplateTemplateParm: {
6682 TemplateTemplateParmDecl *param
6683 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
6684 if (!param) return TemplateName();
6685 TemplateName replacement = ReadTemplateName(F, Record, Idx);
6686 return Context.getSubstTemplateTemplateParm(param, replacement);
6687 }
6688
6689 case TemplateName::SubstTemplateTemplateParmPack: {
6690 TemplateTemplateParmDecl *Param
6691 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
6692 if (!Param)
6693 return TemplateName();
6694
6695 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
6696 if (ArgPack.getKind() != TemplateArgument::Pack)
6697 return TemplateName();
6698
6699 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
6700 }
6701 }
6702
6703 llvm_unreachable("Unhandled template name kind!");
6704}
6705
6706TemplateArgument
6707ASTReader::ReadTemplateArgument(ModuleFile &F,
6708 const RecordData &Record, unsigned &Idx) {
6709 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
6710 switch (Kind) {
6711 case TemplateArgument::Null:
6712 return TemplateArgument();
6713 case TemplateArgument::Type:
6714 return TemplateArgument(readType(F, Record, Idx));
6715 case TemplateArgument::Declaration: {
6716 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
6717 bool ForReferenceParam = Record[Idx++];
6718 return TemplateArgument(D, ForReferenceParam);
6719 }
6720 case TemplateArgument::NullPtr:
6721 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
6722 case TemplateArgument::Integral: {
6723 llvm::APSInt Value = ReadAPSInt(Record, Idx);
6724 QualType T = readType(F, Record, Idx);
6725 return TemplateArgument(Context, Value, T);
6726 }
6727 case TemplateArgument::Template:
6728 return TemplateArgument(ReadTemplateName(F, Record, Idx));
6729 case TemplateArgument::TemplateExpansion: {
6730 TemplateName Name = ReadTemplateName(F, Record, Idx);
David Blaikiedc84cd52013-02-20 22:23:23 +00006731 Optional<unsigned> NumTemplateExpansions;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006732 if (unsigned NumExpansions = Record[Idx++])
6733 NumTemplateExpansions = NumExpansions - 1;
6734 return TemplateArgument(Name, NumTemplateExpansions);
6735 }
6736 case TemplateArgument::Expression:
6737 return TemplateArgument(ReadExpr(F));
6738 case TemplateArgument::Pack: {
6739 unsigned NumArgs = Record[Idx++];
6740 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
6741 for (unsigned I = 0; I != NumArgs; ++I)
6742 Args[I] = ReadTemplateArgument(F, Record, Idx);
6743 return TemplateArgument(Args, NumArgs);
6744 }
6745 }
6746
6747 llvm_unreachable("Unhandled template argument kind!");
6748}
6749
6750TemplateParameterList *
6751ASTReader::ReadTemplateParameterList(ModuleFile &F,
6752 const RecordData &Record, unsigned &Idx) {
6753 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
6754 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
6755 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
6756
6757 unsigned NumParams = Record[Idx++];
6758 SmallVector<NamedDecl *, 16> Params;
6759 Params.reserve(NumParams);
6760 while (NumParams--)
6761 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
6762
6763 TemplateParameterList* TemplateParams =
6764 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
6765 Params.data(), Params.size(), RAngleLoc);
6766 return TemplateParams;
6767}
6768
6769void
6770ASTReader::
6771ReadTemplateArgumentList(SmallVector<TemplateArgument, 8> &TemplArgs,
6772 ModuleFile &F, const RecordData &Record,
6773 unsigned &Idx) {
6774 unsigned NumTemplateArgs = Record[Idx++];
6775 TemplArgs.reserve(NumTemplateArgs);
6776 while (NumTemplateArgs--)
6777 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
6778}
6779
6780/// \brief Read a UnresolvedSet structure.
6781void ASTReader::ReadUnresolvedSet(ModuleFile &F, ASTUnresolvedSet &Set,
6782 const RecordData &Record, unsigned &Idx) {
6783 unsigned NumDecls = Record[Idx++];
6784 Set.reserve(Context, NumDecls);
6785 while (NumDecls--) {
6786 NamedDecl *D = ReadDeclAs<NamedDecl>(F, Record, Idx);
6787 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
6788 Set.addDecl(Context, D, AS);
6789 }
6790}
6791
6792CXXBaseSpecifier
6793ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
6794 const RecordData &Record, unsigned &Idx) {
6795 bool isVirtual = static_cast<bool>(Record[Idx++]);
6796 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
6797 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
6798 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
6799 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
6800 SourceRange Range = ReadSourceRange(F, Record, Idx);
6801 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
6802 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
6803 EllipsisLoc);
6804 Result.setInheritConstructors(inheritConstructors);
6805 return Result;
6806}
6807
6808std::pair<CXXCtorInitializer **, unsigned>
6809ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
6810 unsigned &Idx) {
6811 CXXCtorInitializer **CtorInitializers = 0;
6812 unsigned NumInitializers = Record[Idx++];
6813 if (NumInitializers) {
6814 CtorInitializers
6815 = new (Context) CXXCtorInitializer*[NumInitializers];
6816 for (unsigned i=0; i != NumInitializers; ++i) {
6817 TypeSourceInfo *TInfo = 0;
6818 bool IsBaseVirtual = false;
6819 FieldDecl *Member = 0;
6820 IndirectFieldDecl *IndirectMember = 0;
6821
6822 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
6823 switch (Type) {
6824 case CTOR_INITIALIZER_BASE:
6825 TInfo = GetTypeSourceInfo(F, Record, Idx);
6826 IsBaseVirtual = Record[Idx++];
6827 break;
6828
6829 case CTOR_INITIALIZER_DELEGATING:
6830 TInfo = GetTypeSourceInfo(F, Record, Idx);
6831 break;
6832
6833 case CTOR_INITIALIZER_MEMBER:
6834 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
6835 break;
6836
6837 case CTOR_INITIALIZER_INDIRECT_MEMBER:
6838 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
6839 break;
6840 }
6841
6842 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
6843 Expr *Init = ReadExpr(F);
6844 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
6845 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
6846 bool IsWritten = Record[Idx++];
6847 unsigned SourceOrderOrNumArrayIndices;
6848 SmallVector<VarDecl *, 8> Indices;
6849 if (IsWritten) {
6850 SourceOrderOrNumArrayIndices = Record[Idx++];
6851 } else {
6852 SourceOrderOrNumArrayIndices = Record[Idx++];
6853 Indices.reserve(SourceOrderOrNumArrayIndices);
6854 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
6855 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
6856 }
6857
6858 CXXCtorInitializer *BOMInit;
6859 if (Type == CTOR_INITIALIZER_BASE) {
6860 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, IsBaseVirtual,
6861 LParenLoc, Init, RParenLoc,
6862 MemberOrEllipsisLoc);
6863 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
6864 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, LParenLoc,
6865 Init, RParenLoc);
6866 } else if (IsWritten) {
6867 if (Member)
6868 BOMInit = new (Context) CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc,
6869 LParenLoc, Init, RParenLoc);
6870 else
6871 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
6872 MemberOrEllipsisLoc, LParenLoc,
6873 Init, RParenLoc);
6874 } else {
6875 BOMInit = CXXCtorInitializer::Create(Context, Member, MemberOrEllipsisLoc,
6876 LParenLoc, Init, RParenLoc,
6877 Indices.data(), Indices.size());
6878 }
6879
6880 if (IsWritten)
6881 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
6882 CtorInitializers[i] = BOMInit;
6883 }
6884 }
6885
6886 return std::make_pair(CtorInitializers, NumInitializers);
6887}
6888
6889NestedNameSpecifier *
6890ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
6891 const RecordData &Record, unsigned &Idx) {
6892 unsigned N = Record[Idx++];
6893 NestedNameSpecifier *NNS = 0, *Prev = 0;
6894 for (unsigned I = 0; I != N; ++I) {
6895 NestedNameSpecifier::SpecifierKind Kind
6896 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
6897 switch (Kind) {
6898 case NestedNameSpecifier::Identifier: {
6899 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
6900 NNS = NestedNameSpecifier::Create(Context, Prev, II);
6901 break;
6902 }
6903
6904 case NestedNameSpecifier::Namespace: {
6905 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
6906 NNS = NestedNameSpecifier::Create(Context, Prev, NS);
6907 break;
6908 }
6909
6910 case NestedNameSpecifier::NamespaceAlias: {
6911 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
6912 NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
6913 break;
6914 }
6915
6916 case NestedNameSpecifier::TypeSpec:
6917 case NestedNameSpecifier::TypeSpecWithTemplate: {
6918 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
6919 if (!T)
6920 return 0;
6921
6922 bool Template = Record[Idx++];
6923 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
6924 break;
6925 }
6926
6927 case NestedNameSpecifier::Global: {
6928 NNS = NestedNameSpecifier::GlobalSpecifier(Context);
6929 // No associated value, and there can't be a prefix.
6930 break;
6931 }
6932 }
6933 Prev = NNS;
6934 }
6935 return NNS;
6936}
6937
6938NestedNameSpecifierLoc
6939ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
6940 unsigned &Idx) {
6941 unsigned N = Record[Idx++];
6942 NestedNameSpecifierLocBuilder Builder;
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 SourceRange Range = ReadSourceRange(F, Record, Idx);
6950 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
6951 break;
6952 }
6953
6954 case NestedNameSpecifier::Namespace: {
6955 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
6956 SourceRange Range = ReadSourceRange(F, Record, Idx);
6957 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
6958 break;
6959 }
6960
6961 case NestedNameSpecifier::NamespaceAlias: {
6962 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
6963 SourceRange Range = ReadSourceRange(F, Record, Idx);
6964 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
6965 break;
6966 }
6967
6968 case NestedNameSpecifier::TypeSpec:
6969 case NestedNameSpecifier::TypeSpecWithTemplate: {
6970 bool Template = Record[Idx++];
6971 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
6972 if (!T)
6973 return NestedNameSpecifierLoc();
6974 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
6975
6976 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
6977 Builder.Extend(Context,
6978 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
6979 T->getTypeLoc(), ColonColonLoc);
6980 break;
6981 }
6982
6983 case NestedNameSpecifier::Global: {
6984 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
6985 Builder.MakeGlobal(Context, ColonColonLoc);
6986 break;
6987 }
6988 }
6989 }
6990
6991 return Builder.getWithLocInContext(Context);
6992}
6993
6994SourceRange
6995ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
6996 unsigned &Idx) {
6997 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
6998 SourceLocation end = ReadSourceLocation(F, Record, Idx);
6999 return SourceRange(beg, end);
7000}
7001
7002/// \brief Read an integral value
7003llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
7004 unsigned BitWidth = Record[Idx++];
7005 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
7006 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
7007 Idx += NumWords;
7008 return Result;
7009}
7010
7011/// \brief Read a signed integral value
7012llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
7013 bool isUnsigned = Record[Idx++];
7014 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
7015}
7016
7017/// \brief Read a floating-point value
Tim Northover9ec55f22013-01-22 09:46:51 +00007018llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record,
7019 const llvm::fltSemantics &Sem,
7020 unsigned &Idx) {
7021 return llvm::APFloat(Sem, ReadAPInt(Record, Idx));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007022}
7023
7024// \brief Read a string
7025std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
7026 unsigned Len = Record[Idx++];
7027 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
7028 Idx += Len;
7029 return Result;
7030}
7031
7032VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
7033 unsigned &Idx) {
7034 unsigned Major = Record[Idx++];
7035 unsigned Minor = Record[Idx++];
7036 unsigned Subminor = Record[Idx++];
7037 if (Minor == 0)
7038 return VersionTuple(Major);
7039 if (Subminor == 0)
7040 return VersionTuple(Major, Minor - 1);
7041 return VersionTuple(Major, Minor - 1, Subminor - 1);
7042}
7043
7044CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
7045 const RecordData &Record,
7046 unsigned &Idx) {
7047 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
7048 return CXXTemporary::Create(Context, Decl);
7049}
7050
7051DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
7052 return Diag(SourceLocation(), DiagID);
7053}
7054
7055DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
7056 return Diags.Report(Loc, DiagID);
7057}
7058
7059/// \brief Retrieve the identifier table associated with the
7060/// preprocessor.
7061IdentifierTable &ASTReader::getIdentifierTable() {
7062 return PP.getIdentifierTable();
7063}
7064
7065/// \brief Record that the given ID maps to the given switch-case
7066/// statement.
7067void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
7068 assert((*CurrSwitchCaseStmts)[ID] == 0 &&
7069 "Already have a SwitchCase with this ID");
7070 (*CurrSwitchCaseStmts)[ID] = SC;
7071}
7072
7073/// \brief Retrieve the switch-case statement with the given ID.
7074SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
7075 assert((*CurrSwitchCaseStmts)[ID] != 0 && "No SwitchCase with this ID");
7076 return (*CurrSwitchCaseStmts)[ID];
7077}
7078
7079void ASTReader::ClearSwitchCaseIDs() {
7080 CurrSwitchCaseStmts->clear();
7081}
7082
7083void ASTReader::ReadComments() {
7084 std::vector<RawComment *> Comments;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00007085 for (SmallVectorImpl<std::pair<BitstreamCursor,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007086 serialization::ModuleFile *> >::iterator
7087 I = CommentsCursors.begin(),
7088 E = CommentsCursors.end();
7089 I != E; ++I) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00007090 BitstreamCursor &Cursor = I->first;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007091 serialization::ModuleFile &F = *I->second;
7092 SavedStreamPosition SavedPosition(Cursor);
7093
7094 RecordData Record;
7095 while (true) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00007096 llvm::BitstreamEntry Entry =
7097 Cursor.advanceSkippingSubblocks(BitstreamCursor::AF_DontPopBlockAtEnd);
7098
7099 switch (Entry.Kind) {
7100 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
7101 case llvm::BitstreamEntry::Error:
7102 Error("malformed block record in AST file");
7103 return;
7104 case llvm::BitstreamEntry::EndBlock:
7105 goto NextCursor;
7106 case llvm::BitstreamEntry::Record:
7107 // The interesting case.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007108 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007109 }
7110
7111 // Read a record.
7112 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00007113 switch ((CommentRecordTypes)Cursor.readRecord(Entry.ID, Record)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007114 case COMMENTS_RAW_COMMENT: {
7115 unsigned Idx = 0;
7116 SourceRange SR = ReadSourceRange(F, Record, Idx);
7117 RawComment::CommentKind Kind =
7118 (RawComment::CommentKind) Record[Idx++];
7119 bool IsTrailingComment = Record[Idx++];
7120 bool IsAlmostTrailingComment = Record[Idx++];
7121 Comments.push_back(new (Context) RawComment(SR, Kind,
7122 IsTrailingComment,
7123 IsAlmostTrailingComment));
7124 break;
7125 }
7126 }
7127 }
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00007128 NextCursor:;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007129 }
7130 Context.Comments.addCommentsToFront(Comments);
7131}
7132
7133void ASTReader::finishPendingActions() {
7134 while (!PendingIdentifierInfos.empty() || !PendingDeclChains.empty() ||
Argyrios Kyrtzidis7640b022013-02-16 00:48:59 +00007135 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty()) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007136 // If any identifiers with corresponding top-level declarations have
7137 // been loaded, load those declarations now.
Douglas Gregoraa945902013-02-18 15:53:43 +00007138 llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2> > TopLevelDecls;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007139 while (!PendingIdentifierInfos.empty()) {
Douglas Gregoraa945902013-02-18 15:53:43 +00007140 // FIXME: std::move
7141 IdentifierInfo *II = PendingIdentifierInfos.back().first;
7142 SmallVector<uint32_t, 4> DeclIDs = PendingIdentifierInfos.back().second;
Douglas Gregorcc9bdcb2013-02-19 18:26:28 +00007143 PendingIdentifierInfos.pop_back();
Douglas Gregoraa945902013-02-18 15:53:43 +00007144
7145 SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007146 }
7147
7148 // Load pending declaration chains.
7149 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) {
7150 loadPendingDeclChain(PendingDeclChains[I]);
7151 PendingDeclChainsKnown.erase(PendingDeclChains[I]);
7152 }
7153 PendingDeclChains.clear();
7154
Douglas Gregoraa945902013-02-18 15:53:43 +00007155 // Make the most recent of the top-level declarations visible.
7156 for (llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2> >::iterator
7157 TLD = TopLevelDecls.begin(), TLDEnd = TopLevelDecls.end();
7158 TLD != TLDEnd; ++TLD) {
7159 IdentifierInfo *II = TLD->first;
7160 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
7161 NamedDecl *ND = cast<NamedDecl>(TLD->second[I]->getMostRecentDecl());
7162 SemaObj->pushExternalDeclIntoScope(ND, II);
7163 }
7164 }
7165
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007166 // Load any pending macro definitions.
7167 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00007168 IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
7169 SmallVector<PendingMacroInfo, 2> GlobalIDs;
7170 GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
7171 // Initialize the macro history from chained-PCHs ahead of module imports.
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00007172 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
7173 ++IDIdx) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00007174 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
7175 if (Info.M->Kind != MK_Module)
7176 resolvePendingMacro(II, Info);
7177 }
7178 // Handle module imports.
7179 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
7180 ++IDIdx) {
7181 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
7182 if (Info.M->Kind == MK_Module)
7183 resolvePendingMacro(II, Info);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007184 }
7185 }
7186 PendingMacroIDs.clear();
Argyrios Kyrtzidis7640b022013-02-16 00:48:59 +00007187
7188 // Wire up the DeclContexts for Decls that we delayed setting until
7189 // recursive loading is completed.
7190 while (!PendingDeclContextInfos.empty()) {
7191 PendingDeclContextInfo Info = PendingDeclContextInfos.front();
7192 PendingDeclContextInfos.pop_front();
7193 DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
7194 DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
7195 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
7196 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007197 }
7198
7199 // If we deserialized any C++ or Objective-C class definitions, any
7200 // Objective-C protocol definitions, or any redeclarable templates, make sure
7201 // that all redeclarations point to the definitions. Note that this can only
7202 // happen now, after the redeclaration chains have been fully wired.
7203 for (llvm::SmallPtrSet<Decl *, 4>::iterator D = PendingDefinitions.begin(),
7204 DEnd = PendingDefinitions.end();
7205 D != DEnd; ++D) {
7206 if (TagDecl *TD = dyn_cast<TagDecl>(*D)) {
7207 if (const TagType *TagT = dyn_cast<TagType>(TD->TypeForDecl)) {
7208 // Make sure that the TagType points at the definition.
7209 const_cast<TagType*>(TagT)->decl = TD;
7210 }
7211
7212 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(*D)) {
7213 for (CXXRecordDecl::redecl_iterator R = RD->redecls_begin(),
7214 REnd = RD->redecls_end();
7215 R != REnd; ++R)
7216 cast<CXXRecordDecl>(*R)->DefinitionData = RD->DefinitionData;
7217
7218 }
7219
7220 continue;
7221 }
7222
7223 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(*D)) {
7224 // Make sure that the ObjCInterfaceType points at the definition.
7225 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
7226 ->Decl = ID;
7227
7228 for (ObjCInterfaceDecl::redecl_iterator R = ID->redecls_begin(),
7229 REnd = ID->redecls_end();
7230 R != REnd; ++R)
7231 R->Data = ID->Data;
7232
7233 continue;
7234 }
7235
7236 if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(*D)) {
7237 for (ObjCProtocolDecl::redecl_iterator R = PD->redecls_begin(),
7238 REnd = PD->redecls_end();
7239 R != REnd; ++R)
7240 R->Data = PD->Data;
7241
7242 continue;
7243 }
7244
7245 RedeclarableTemplateDecl *RTD
7246 = cast<RedeclarableTemplateDecl>(*D)->getCanonicalDecl();
7247 for (RedeclarableTemplateDecl::redecl_iterator R = RTD->redecls_begin(),
7248 REnd = RTD->redecls_end();
7249 R != REnd; ++R)
7250 R->Common = RTD->Common;
7251 }
7252 PendingDefinitions.clear();
7253
7254 // Load the bodies of any functions or methods we've encountered. We do
7255 // this now (delayed) so that we can be sure that the declaration chains
7256 // have been fully wired up.
7257 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
7258 PBEnd = PendingBodies.end();
7259 PB != PBEnd; ++PB) {
7260 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
7261 // FIXME: Check for =delete/=default?
7262 // FIXME: Complain about ODR violations here?
7263 if (!getContext().getLangOpts().Modules || !FD->hasBody())
7264 FD->setLazyBody(PB->second);
7265 continue;
7266 }
7267
7268 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
7269 if (!getContext().getLangOpts().Modules || !MD->hasBody())
7270 MD->setLazyBody(PB->second);
7271 }
7272 PendingBodies.clear();
7273}
7274
7275void ASTReader::FinishedDeserializing() {
7276 assert(NumCurrentElementsDeserializing &&
7277 "FinishedDeserializing not paired with StartedDeserializing");
7278 if (NumCurrentElementsDeserializing == 1) {
7279 // We decrease NumCurrentElementsDeserializing only after pending actions
7280 // are finished, to avoid recursively re-calling finishPendingActions().
7281 finishPendingActions();
7282 }
7283 --NumCurrentElementsDeserializing;
7284
7285 if (NumCurrentElementsDeserializing == 0 &&
7286 Consumer && !PassingDeclsToConsumer) {
7287 // Guard variable to avoid recursively redoing the process of passing
7288 // decls to consumer.
7289 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
7290 true);
7291
7292 while (!InterestingDecls.empty()) {
7293 // We are not in recursive loading, so it's safe to pass the "interesting"
7294 // decls to the consumer.
7295 Decl *D = InterestingDecls.front();
7296 InterestingDecls.pop_front();
7297 PassInterestingDeclToConsumer(D);
7298 }
7299 }
7300}
7301
7302ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context,
7303 StringRef isysroot, bool DisableValidation,
Douglas Gregorf575d6e2013-01-25 00:45:27 +00007304 bool AllowASTWithCompilerErrors, bool UseGlobalIndex)
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007305 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
7306 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
7307 Diags(PP.getDiagnostics()), SemaObj(0), PP(PP), Context(Context),
7308 Consumer(0), ModuleMgr(PP.getFileManager()),
7309 isysroot(isysroot), DisableValidation(DisableValidation),
Douglas Gregore1698072013-01-25 00:38:33 +00007310 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
Douglas Gregorf575d6e2013-01-25 00:45:27 +00007311 UseGlobalIndex(UseGlobalIndex), TriedLoadingGlobalIndex(false),
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007312 CurrentGeneration(0), CurrSwitchCaseStmts(&SwitchCaseStmts),
7313 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Douglas Gregore1698072013-01-25 00:38:33 +00007314 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
7315 TotalNumMacros(0), NumIdentifierLookups(0), NumIdentifierLookupHits(0),
7316 NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
Douglas Gregor95fb36e2013-01-28 17:54:36 +00007317 NumMethodPoolLookups(0), NumMethodPoolHits(0),
7318 NumMethodPoolTableLookups(0), NumMethodPoolTableHits(0),
7319 TotalNumMethodPoolEntries(0),
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007320 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
7321 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
7322 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
7323 PassingDeclsToConsumer(false),
7324 NumCXXBaseSpecifiersLoaded(0)
7325{
7326 SourceMgr.setExternalSLocEntrySource(this);
7327}
7328
7329ASTReader::~ASTReader() {
7330 for (DeclContextVisibleUpdatesPending::iterator
7331 I = PendingVisibleUpdates.begin(),
7332 E = PendingVisibleUpdates.end();
7333 I != E; ++I) {
7334 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
7335 F = I->second.end();
7336 J != F; ++J)
7337 delete J->first;
7338 }
7339}